1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
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 /* [temp.over.link] Two lambda-expressions are never considered
1820 So just hash the closure type. */
1821 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1824 case IMPLICIT_CONV_EXPR
:
1825 case STATIC_CAST_EXPR
:
1826 case REINTERPRET_CAST_EXPR
:
1827 case CONST_CAST_EXPR
:
1828 case DYNAMIC_CAST_EXPR
:
1830 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1831 /* Now hash operands as usual. */
1841 if (alias_template_specialization_p (arg
))
1843 // We want an alias specialization that survived strip_typedefs
1844 // to hash differently from its TYPE_CANONICAL, to avoid hash
1845 // collisions that compare as different in template_args_equal.
1846 // These could be dependent specializations that strip_typedefs
1847 // left alone, or untouched specializations because
1848 // coerce_template_parms returns the unconverted template
1849 // arguments if it sees incomplete argument packs.
1850 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1851 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1853 if (TYPE_CANONICAL (arg
))
1854 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1856 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1857 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1858 /* Otherwise just compare the types during lookup. */
1861 case tcc_declaration
:
1863 return iterative_hash_expr (arg
, val
);
1866 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1868 unsigned n
= cp_tree_operand_length (arg
);
1869 for (i
= 0; i
< n
; ++i
)
1870 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1878 /* Unregister the specialization SPEC as a specialization of TMPL.
1879 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1880 if the SPEC was listed as a specialization of TMPL.
1882 Note that SPEC has been ggc_freed, so we can't look inside it. */
1885 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1890 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1891 elt
.args
= TI_ARGS (tinfo
);
1892 elt
.spec
= NULL_TREE
;
1894 entry
= decl_specializations
->find (&elt
);
1897 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1898 gcc_assert (new_spec
!= NULL_TREE
);
1899 entry
->spec
= new_spec
;
1906 /* Like register_specialization, but for local declarations. We are
1907 registering SPEC, an instantiation of TMPL. */
1910 register_local_specialization (tree spec
, tree tmpl
)
1912 gcc_assert (tmpl
!= spec
);
1913 local_specializations
->put (tmpl
, spec
);
1916 /* TYPE is a class type. Returns true if TYPE is an explicitly
1917 specialized class. */
1920 explicit_class_specialization_p (tree type
)
1922 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1924 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1927 /* Print the list of functions at FNS, going through all the overloads
1928 for each element of the list. Alternatively, FNS cannot be a
1929 TREE_LIST, in which case it will be printed together with all the
1932 MORE and *STR should respectively be FALSE and NULL when the function
1933 is called from the outside. They are used internally on recursive
1934 calls. print_candidates manages the two parameters and leaves NULL
1935 in *STR when it ends. */
1938 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1940 if (TREE_CODE (fns
) == TREE_LIST
)
1941 for (; fns
; fns
= TREE_CHAIN (fns
))
1942 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1944 for (lkp_iterator
iter (fns
); iter
;)
1949 const char *pfx
= *str
;
1953 pfx
= _("candidates are:");
1955 pfx
= _("candidate is:");
1956 *str
= get_spaces (pfx
);
1958 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1962 /* Print the list of candidate FNS in an error message. FNS can also
1963 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1966 print_candidates (tree fns
)
1969 print_candidates_1 (fns
, &str
);
1973 /* Get a (possibly) constrained template declaration for the
1974 purpose of ordering candidates. */
1976 get_template_for_ordering (tree list
)
1978 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1979 tree f
= TREE_VALUE (list
);
1980 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1981 return TI_TEMPLATE (ti
);
1985 /* Among candidates having the same signature, return the
1986 most constrained or NULL_TREE if there is no best candidate.
1987 If the signatures of candidates vary (e.g., template
1988 specialization vs. member function), then there can be no
1991 Note that we don't compare constraints on the functions
1992 themselves, but rather those of their templates. */
1994 most_constrained_function (tree candidates
)
1996 // Try to find the best candidate in a first pass.
1997 tree champ
= candidates
;
1998 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2000 int winner
= more_constrained (get_template_for_ordering (champ
),
2001 get_template_for_ordering (c
));
2003 champ
= c
; // The candidate is more constrained
2004 else if (winner
== 0)
2005 return NULL_TREE
; // Neither is more constrained
2008 // Verify that the champ is better than previous candidates.
2009 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2010 if (!more_constrained (get_template_for_ordering (champ
),
2011 get_template_for_ordering (c
)))
2019 /* Returns the template (one of the functions given by TEMPLATE_ID)
2020 which can be specialized to match the indicated DECL with the
2021 explicit template args given in TEMPLATE_ID. The DECL may be
2022 NULL_TREE if none is available. In that case, the functions in
2023 TEMPLATE_ID are non-members.
2025 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2026 specialization of a member template.
2028 The TEMPLATE_COUNT is the number of references to qualifying
2029 template classes that appeared in the name of the function. See
2030 check_explicit_specialization for a more accurate description.
2032 TSK indicates what kind of template declaration (if any) is being
2033 declared. TSK_TEMPLATE indicates that the declaration given by
2034 DECL, though a FUNCTION_DECL, has template parameters, and is
2035 therefore a template function.
2037 The template args (those explicitly specified and those deduced)
2038 are output in a newly created vector *TARGS_OUT.
2040 If it is impossible to determine the result, an error message is
2041 issued. The error_mark_node is returned to indicate failure. */
2044 determine_specialization (tree template_id
,
2047 int need_member_template
,
2053 tree explicit_targs
;
2054 tree candidates
= NULL_TREE
;
2056 /* A TREE_LIST of templates of which DECL may be a specialization.
2057 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2058 corresponding TREE_PURPOSE is the set of template arguments that,
2059 when used to instantiate the template, would produce a function
2060 with the signature of DECL. */
2061 tree templates
= NULL_TREE
;
2063 cp_binding_level
*b
;
2065 *targs_out
= NULL_TREE
;
2067 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2068 return error_mark_node
;
2070 /* We shouldn't be specializing a member template of an
2071 unspecialized class template; we already gave an error in
2072 check_specialization_scope, now avoid crashing. */
2074 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2075 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2077 gcc_assert (errorcount
);
2078 return error_mark_node
;
2081 fns
= TREE_OPERAND (template_id
, 0);
2082 explicit_targs
= TREE_OPERAND (template_id
, 1);
2084 if (fns
== error_mark_node
)
2085 return error_mark_node
;
2087 /* Check for baselinks. */
2088 if (BASELINK_P (fns
))
2089 fns
= BASELINK_FUNCTIONS (fns
);
2091 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2093 error ("%qD is not a function template", fns
);
2094 return error_mark_node
;
2096 else if (VAR_P (decl
) && !variable_template_p (fns
))
2098 error ("%qD is not a variable template", fns
);
2099 return error_mark_node
;
2102 /* Count the number of template headers specified for this
2105 for (b
= current_binding_level
;
2106 b
->kind
== sk_template_parms
;
2110 tree orig_fns
= fns
;
2112 if (variable_template_p (fns
))
2114 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2115 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2116 tf_warning_or_error
,
2117 /*req_all*/true, /*use_defarg*/true);
2118 if (targs
!= error_mark_node
)
2119 templates
= tree_cons (targs
, fns
, templates
);
2121 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2125 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2127 tree decl_arg_types
;
2131 /* In case of explicit specialization, we need to check if
2132 the number of template headers appearing in the specialization
2133 is correct. This is usually done in check_explicit_specialization,
2134 but the check done there cannot be exhaustive when specializing
2135 member functions. Consider the following code:
2137 template <> void A<int>::f(int);
2138 template <> template <> void A<int>::f(int);
2140 Assuming that A<int> is not itself an explicit specialization
2141 already, the first line specializes "f" which is a non-template
2142 member function, whilst the second line specializes "f" which
2143 is a template member function. So both lines are syntactically
2144 correct, and check_explicit_specialization does not reject
2147 Here, we can do better, as we are matching the specialization
2148 against the declarations. We count the number of template
2149 headers, and we check if they match TEMPLATE_COUNT + 1
2150 (TEMPLATE_COUNT is the number of qualifying template classes,
2151 plus there must be another header for the member template
2154 Notice that if header_count is zero, this is not a
2155 specialization but rather a template instantiation, so there
2156 is no check we can perform here. */
2157 if (header_count
&& header_count
!= template_count
+ 1)
2160 /* Check that the number of template arguments at the
2161 innermost level for DECL is the same as for FN. */
2162 if (current_binding_level
->kind
== sk_template_parms
2163 && !current_binding_level
->explicit_spec_p
2164 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2165 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2166 (current_template_parms
))))
2169 /* DECL might be a specialization of FN. */
2170 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2171 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2173 /* For a non-static member function, we need to make sure
2174 that the const qualification is the same. Since
2175 get_bindings does not try to merge the "this" parameter,
2176 we must do the comparison explicitly. */
2177 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2179 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2180 TREE_VALUE (decl_arg_types
)))
2183 /* And the ref-qualification. */
2184 if (type_memfn_rqual (TREE_TYPE (decl
))
2185 != type_memfn_rqual (TREE_TYPE (fn
)))
2189 /* Skip the "this" parameter and, for constructors of
2190 classes with virtual bases, the VTT parameter. A
2191 full specialization of a constructor will have a VTT
2192 parameter, but a template never will. */
2194 = skip_artificial_parms_for (decl
, decl_arg_types
);
2196 = skip_artificial_parms_for (fn
, fn_arg_types
);
2198 /* Function templates cannot be specializations; there are
2199 no partial specializations of functions. Therefore, if
2200 the type of DECL does not match FN, there is no
2203 Note that it should never be the case that we have both
2204 candidates added here, and for regular member functions
2206 if (tsk
== tsk_template
)
2208 if (compparms (fn_arg_types
, decl_arg_types
))
2209 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2213 /* See whether this function might be a specialization of this
2214 template. Suppress access control because we might be trying
2215 to make this specialization a friend, and we have already done
2216 access control for the declaration of the specialization. */
2217 push_deferring_access_checks (dk_no_check
);
2218 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2219 pop_deferring_access_checks ();
2222 /* We cannot deduce template arguments that when used to
2223 specialize TMPL will produce DECL. */
2226 if (uses_template_parms (targs
))
2227 /* We deduced something involving 'auto', which isn't a valid
2228 template argument. */
2231 /* Remove, from the set of candidates, all those functions
2232 whose constraints are not satisfied. */
2233 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2236 // Then, try to form the new function type.
2237 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2238 if (insttype
== error_mark_node
)
2241 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2242 if (!compparms (fn_arg_types
, decl_arg_types
))
2245 /* Save this template, and the arguments deduced. */
2246 templates
= tree_cons (targs
, fn
, templates
);
2248 else if (need_member_template
)
2249 /* FN is an ordinary member function, and we need a
2250 specialization of a member template. */
2252 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2253 /* We can get IDENTIFIER_NODEs here in certain erroneous
2256 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2257 /* This is just an ordinary non-member function. Nothing can
2258 be a specialization of that. */
2260 else if (DECL_ARTIFICIAL (fn
))
2261 /* Cannot specialize functions that are created implicitly. */
2265 tree decl_arg_types
;
2267 /* This is an ordinary member function. However, since
2268 we're here, we can assume its enclosing class is a
2269 template class. For example,
2271 template <typename T> struct S { void f(); };
2272 template <> void S<int>::f() {}
2274 Here, S<int>::f is a non-template, but S<int> is a
2275 template class. If FN has the same type as DECL, we
2276 might be in business. */
2278 if (!DECL_TEMPLATE_INFO (fn
))
2279 /* Its enclosing class is an explicit specialization
2280 of a template class. This is not a candidate. */
2283 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2284 TREE_TYPE (TREE_TYPE (fn
))))
2285 /* The return types differ. */
2288 /* Adjust the type of DECL in case FN is a static member. */
2289 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2290 if (DECL_STATIC_FUNCTION_P (fn
)
2291 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2292 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2294 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2298 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2299 && (type_memfn_rqual (TREE_TYPE (decl
))
2300 != type_memfn_rqual (TREE_TYPE (fn
))))
2303 // If the deduced arguments do not satisfy the constraints,
2304 // this is not a candidate.
2305 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2308 // Add the candidate.
2309 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2313 if (templates
&& TREE_CHAIN (templates
))
2319 It is possible for a specialization with a given function
2320 signature to be instantiated from more than one function
2321 template. In such cases, explicit specification of the
2322 template arguments must be used to uniquely identify the
2323 function template specialization being specialized.
2325 Note that here, there's no suggestion that we're supposed to
2326 determine which of the candidate templates is most
2327 specialized. However, we, also have:
2331 Partial ordering of overloaded function template
2332 declarations is used in the following contexts to select
2333 the function template to which a function template
2334 specialization refers:
2336 -- when an explicit specialization refers to a function
2339 So, we do use the partial ordering rules, at least for now.
2340 This extension can only serve to make invalid programs valid,
2341 so it's safe. And, there is strong anecdotal evidence that
2342 the committee intended the partial ordering rules to apply;
2343 the EDG front end has that behavior, and John Spicer claims
2344 that the committee simply forgot to delete the wording in
2345 [temp.expl.spec]. */
2346 tree tmpl
= most_specialized_instantiation (templates
);
2347 if (tmpl
!= error_mark_node
)
2350 TREE_CHAIN (templates
) = NULL_TREE
;
2354 // Concepts allows multiple declarations of member functions
2355 // with the same signature. Like above, we need to rely on
2356 // on the partial ordering of those candidates to determine which
2358 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2360 if (tree cand
= most_constrained_function (candidates
))
2363 TREE_CHAIN (cand
) = NULL_TREE
;
2367 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2369 error ("template-id %qD for %q+D does not match any template "
2370 "declaration", template_id
, decl
);
2371 if (header_count
&& header_count
!= template_count
+ 1)
2372 inform (input_location
, "saw %d %<template<>%>, need %d for "
2373 "specializing a member function template",
2374 header_count
, template_count
+ 1);
2376 print_candidates (orig_fns
);
2377 return error_mark_node
;
2379 else if ((templates
&& TREE_CHAIN (templates
))
2380 || (candidates
&& TREE_CHAIN (candidates
))
2381 || (templates
&& candidates
))
2383 error ("ambiguous template specialization %qD for %q+D",
2385 candidates
= chainon (candidates
, templates
);
2386 print_candidates (candidates
);
2387 return error_mark_node
;
2390 /* We have one, and exactly one, match. */
2393 tree fn
= TREE_VALUE (candidates
);
2394 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2396 // Propagate the candidate's constraints to the declaration.
2397 set_constraints (decl
, get_constraints (fn
));
2399 /* DECL is a re-declaration or partial instantiation of a template
2401 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2403 /* It was a specialization of an ordinary member function in a
2405 return DECL_TI_TEMPLATE (fn
);
2408 /* It was a specialization of a template. */
2409 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2410 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2412 *targs_out
= copy_node (targs
);
2413 SET_TMPL_ARGS_LEVEL (*targs_out
,
2414 TMPL_ARGS_DEPTH (*targs_out
),
2415 TREE_PURPOSE (templates
));
2418 *targs_out
= TREE_PURPOSE (templates
);
2419 return TREE_VALUE (templates
);
2422 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2423 but with the default argument values filled in from those in the
2427 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2430 tree new_spec_types
;
2435 if (spec_types
== void_list_node
)
2436 return void_list_node
;
2438 /* Substitute into the rest of the list. */
2440 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2441 TREE_CHAIN (tmpl_types
));
2443 /* Add the default argument for this parameter. */
2444 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2445 TREE_VALUE (spec_types
),
2449 /* DECL is an explicit specialization. Replicate default arguments
2450 from the template it specializes. (That way, code like:
2452 template <class T> void f(T = 3);
2453 template <> void f(double);
2456 works, as required.) An alternative approach would be to look up
2457 the correct default arguments at the call-site, but this approach
2458 is consistent with how implicit instantiations are handled. */
2461 copy_default_args_to_explicit_spec (tree decl
)
2466 tree new_spec_types
;
2470 tree object_type
= NULL_TREE
;
2471 tree in_charge
= NULL_TREE
;
2472 tree vtt
= NULL_TREE
;
2474 /* See if there's anything we need to do. */
2475 tmpl
= DECL_TI_TEMPLATE (decl
);
2476 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2477 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2478 if (TREE_PURPOSE (t
))
2483 old_type
= TREE_TYPE (decl
);
2484 spec_types
= TYPE_ARG_TYPES (old_type
);
2486 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2488 /* Remove the this pointer, but remember the object's type for
2490 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2491 spec_types
= TREE_CHAIN (spec_types
);
2492 tmpl_types
= TREE_CHAIN (tmpl_types
);
2494 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2496 /* DECL may contain more parameters than TMPL due to the extra
2497 in-charge parameter in constructors and destructors. */
2498 in_charge
= spec_types
;
2499 spec_types
= TREE_CHAIN (spec_types
);
2501 if (DECL_HAS_VTT_PARM_P (decl
))
2504 spec_types
= TREE_CHAIN (spec_types
);
2508 /* Compute the merged default arguments. */
2510 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2512 /* Compute the new FUNCTION_TYPE. */
2516 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2521 /* Put the in-charge parameter back. */
2522 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2523 TREE_VALUE (in_charge
),
2526 new_type
= build_method_type_directly (object_type
,
2527 TREE_TYPE (old_type
),
2531 new_type
= build_function_type (TREE_TYPE (old_type
),
2533 new_type
= cp_build_type_attribute_variant (new_type
,
2534 TYPE_ATTRIBUTES (old_type
));
2535 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2537 TREE_TYPE (decl
) = new_type
;
2540 /* Return the number of template headers we expect to see for a definition
2541 or specialization of CTYPE or one of its non-template members. */
2544 num_template_headers_for_class (tree ctype
)
2546 int num_templates
= 0;
2548 while (ctype
&& CLASS_TYPE_P (ctype
))
2550 /* You're supposed to have one `template <...>' for every
2551 template class, but you don't need one for a full
2552 specialization. For example:
2554 template <class T> struct S{};
2555 template <> struct S<int> { void f(); };
2556 void S<int>::f () {}
2558 is correct; there shouldn't be a `template <>' for the
2559 definition of `S<int>::f'. */
2560 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2561 /* If CTYPE does not have template information of any
2562 kind, then it is not a template, nor is it nested
2563 within a template. */
2565 if (explicit_class_specialization_p (ctype
))
2567 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2570 ctype
= TYPE_CONTEXT (ctype
);
2573 return num_templates
;
2576 /* Do a simple sanity check on the template headers that precede the
2577 variable declaration DECL. */
2580 check_template_variable (tree decl
)
2582 tree ctx
= CP_DECL_CONTEXT (decl
);
2583 int wanted
= num_template_headers_for_class (ctx
);
2584 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2585 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2587 if (cxx_dialect
< cxx14
)
2588 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2589 "variable templates only available with "
2590 "-std=c++14 or -std=gnu++14");
2592 // Namespace-scope variable templates should have a template header.
2595 if (template_header_count
> wanted
)
2597 auto_diagnostic_group d
;
2598 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2599 "too many template headers for %qD "
2602 if (warned
&& CLASS_TYPE_P (ctx
)
2603 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2604 inform (DECL_SOURCE_LOCATION (decl
),
2605 "members of an explicitly specialized class are defined "
2606 "without a template header");
2610 /* An explicit specialization whose declarator-id or class-head-name is not
2611 qualified shall be declared in the nearest enclosing namespace of the
2612 template, or, if the namespace is inline (7.3.1), any namespace from its
2613 enclosing namespace set.
2615 If the name declared in the explicit instantiation is an unqualified name,
2616 the explicit instantiation shall appear in the namespace where its template
2617 is declared or, if that namespace is inline (7.3.1), any namespace from its
2618 enclosing namespace set. */
2621 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2623 tree tmpl
= most_general_template (t
);
2624 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2625 && !is_nested_namespace (current_namespace
,
2626 CP_DECL_CONTEXT (tmpl
), true))
2628 if (processing_specialization
)
2629 permerror (loc
, "explicit specialization of %qD outside its "
2630 "namespace must use a nested-name-specifier", tmpl
);
2631 else if (processing_explicit_instantiation
2632 && cxx_dialect
>= cxx11
)
2633 /* This was allowed in C++98, so only pedwarn. */
2634 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2635 "outside its namespace must use a nested-name-"
2640 /* Warn for a template specialization SPEC that is missing some of a set
2641 of function or type attributes that the template TEMPL is declared with.
2642 ATTRLIST is a list of additional attributes that SPEC should be taken
2643 to ultimately be declared with. */
2646 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2648 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2649 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2651 /* Avoid warning if the difference between the primary and
2652 the specialization is not in one of the attributes below. */
2653 const char* const blacklist
[] = {
2654 "alloc_align", "alloc_size", "assume_aligned", "format",
2655 "format_arg", "malloc", "nonnull", NULL
2658 /* Put together a list of the black listed attributes that the primary
2659 template is declared with that the specialization is not, in case
2660 it's not apparent from the most recent declaration of the primary. */
2662 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2668 auto_diagnostic_group d
;
2669 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2670 "explicit specialization %q#D may be missing attributes",
2672 inform (DECL_SOURCE_LOCATION (tmpl
),
2674 ? G_("missing primary template attributes %s")
2675 : G_("missing primary template attribute %s"),
2676 pp_formatted_text (&str
));
2679 /* Check to see if the function just declared, as indicated in
2680 DECLARATOR, and in DECL, is a specialization of a function
2681 template. We may also discover that the declaration is an explicit
2682 instantiation at this point.
2684 Returns DECL, or an equivalent declaration that should be used
2685 instead if all goes well. Issues an error message if something is
2686 amiss. Returns error_mark_node if the error is not easily
2689 FLAGS is a bitmask consisting of the following flags:
2691 2: The function has a definition.
2692 4: The function is a friend.
2694 The TEMPLATE_COUNT is the number of references to qualifying
2695 template classes that appeared in the name of the function. For
2698 template <class T> struct S { void f(); };
2701 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2702 classes are not counted in the TEMPLATE_COUNT, so that in
2704 template <class T> struct S {};
2705 template <> struct S<int> { void f(); }
2706 template <> void S<int>::f();
2708 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2709 invalid; there should be no template <>.)
2711 If the function is a specialization, it is marked as such via
2712 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2713 is set up correctly, and it is added to the list of specializations
2714 for that template. */
2717 check_explicit_specialization (tree declarator
,
2723 int have_def
= flags
& 2;
2724 int is_friend
= flags
& 4;
2725 bool is_concept
= flags
& 8;
2726 int specialization
= 0;
2727 int explicit_instantiation
= 0;
2728 int member_specialization
= 0;
2729 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2730 tree dname
= DECL_NAME (decl
);
2735 if (!processing_specialization
)
2738 tsk
= tsk_excessive_parms
;
2741 tsk
= current_tmpl_spec_kind (template_count
);
2746 if (processing_specialization
&& !VAR_P (decl
))
2749 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2751 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2754 /* This could be something like:
2756 template <class T> void f(T);
2757 class S { friend void f<>(int); } */
2761 /* This case handles bogus declarations like template <>
2762 template <class T> void f<int>(); */
2764 error ("template-id %qD in declaration of primary template",
2771 case tsk_invalid_member_spec
:
2772 /* The error has already been reported in
2773 check_specialization_scope. */
2774 return error_mark_node
;
2776 case tsk_invalid_expl_inst
:
2777 error ("template parameter list used in explicit instantiation");
2783 error ("definition provided for explicit instantiation");
2785 explicit_instantiation
= 1;
2788 case tsk_excessive_parms
:
2789 case tsk_insufficient_parms
:
2790 if (tsk
== tsk_excessive_parms
)
2791 error ("too many template parameter lists in declaration of %qD",
2793 else if (template_header_count
)
2794 error("too few template parameter lists in declaration of %qD", decl
);
2796 error("explicit specialization of %qD must be introduced by "
2797 "%<template <>%>", decl
);
2802 error ("explicit specialization declared %<concept%>");
2804 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2805 /* In cases like template<> constexpr bool v = true;
2806 We'll give an error in check_template_variable. */
2809 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2811 member_specialization
= 1;
2817 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2819 /* This case handles bogus declarations like template <>
2820 template <class T> void f<int>(); */
2822 if (!uses_template_parms (declarator
))
2823 error ("template-id %qD in declaration of primary template",
2825 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2827 /* Partial specialization of variable template. */
2828 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2832 else if (cxx_dialect
< cxx14
)
2833 error ("non-type partial specialization %qD "
2834 "is not allowed", declarator
);
2836 error ("non-class, non-variable partial specialization %qD "
2837 "is not allowed", declarator
);
2842 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2843 /* This is a specialization of a member template, without
2844 specialization the containing class. Something like:
2846 template <class T> struct S {
2847 template <class U> void f (U);
2849 template <> template <class U> void S<int>::f(U) {}
2851 That's a specialization -- but of the entire template. */
2859 if ((specialization
|| member_specialization
)
2860 /* This doesn't apply to variable templates. */
2861 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2862 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2864 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2865 for (; t
; t
= TREE_CHAIN (t
))
2866 if (TREE_PURPOSE (t
))
2868 permerror (input_location
,
2869 "default argument specified in explicit specialization");
2874 if (specialization
|| member_specialization
|| explicit_instantiation
)
2876 tree tmpl
= NULL_TREE
;
2877 tree targs
= NULL_TREE
;
2878 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2880 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2881 if (!was_template_id
)
2885 gcc_assert (identifier_p (declarator
));
2890 /* If there is no class context, the explicit instantiation
2891 must be at namespace scope. */
2892 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2894 /* Find the namespace binding, using the declaration
2896 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2898 if (fns
== error_mark_node
)
2899 /* If lookup fails, look for a friend declaration so we can
2900 give a better diagnostic. */
2901 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2902 /*type*/false, /*complain*/true,
2905 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2907 error ("%qD is not a template function", dname
);
2908 fns
= error_mark_node
;
2912 declarator
= lookup_template_function (fns
, NULL_TREE
);
2915 if (declarator
== error_mark_node
)
2916 return error_mark_node
;
2918 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2920 if (!explicit_instantiation
)
2921 /* A specialization in class scope. This is invalid,
2922 but the error will already have been flagged by
2923 check_specialization_scope. */
2924 return error_mark_node
;
2927 /* It's not valid to write an explicit instantiation in
2930 class C { template void f(); }
2932 This case is caught by the parser. However, on
2935 template class C { void f(); };
2937 (which is invalid) we can get here. The error will be
2944 else if (ctype
!= NULL_TREE
2945 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2947 // We'll match variable templates in start_decl.
2951 /* Find the list of functions in ctype that have the same
2952 name as the declared function. */
2953 tree name
= TREE_OPERAND (declarator
, 0);
2955 if (constructor_name_p (name
, ctype
))
2957 if (DECL_CONSTRUCTOR_P (decl
)
2958 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2959 : !CLASSTYPE_DESTRUCTOR (ctype
))
2961 /* From [temp.expl.spec]:
2963 If such an explicit specialization for the member
2964 of a class template names an implicitly-declared
2965 special member function (clause _special_), the
2966 program is ill-formed.
2968 Similar language is found in [temp.explicit]. */
2969 error ("specialization of implicitly-declared special member function");
2970 return error_mark_node
;
2973 name
= DECL_NAME (decl
);
2976 /* For a type-conversion operator, We might be looking for
2977 `operator int' which will be a specialization of
2978 `operator T'. Grab all the conversion operators, and
2979 then select from them. */
2980 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
2981 ? conv_op_identifier
: name
);
2983 if (fns
== NULL_TREE
)
2985 error ("no member function %qD declared in %qT", name
, ctype
);
2986 return error_mark_node
;
2989 TREE_OPERAND (declarator
, 0) = fns
;
2992 /* Figure out what exactly is being specialized at this point.
2993 Note that for an explicit instantiation, even one for a
2994 member function, we cannot tell a priori whether the
2995 instantiation is for a member template, or just a member
2996 function of a template class. Even if a member template is
2997 being instantiated, the member template arguments may be
2998 elided if they can be deduced from the rest of the
3000 tmpl
= determine_specialization (declarator
, decl
,
3002 member_specialization
,
3006 if (!tmpl
|| tmpl
== error_mark_node
)
3007 /* We couldn't figure out what this declaration was
3009 return error_mark_node
;
3012 if (TREE_CODE (decl
) == FUNCTION_DECL
3013 && DECL_HIDDEN_FRIEND_P (tmpl
))
3015 auto_diagnostic_group d
;
3016 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3017 "friend declaration %qD is not visible to "
3018 "explicit specialization", tmpl
))
3019 inform (DECL_SOURCE_LOCATION (tmpl
),
3020 "friend declaration here");
3022 else if (!ctype
&& !is_friend
3023 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3024 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3026 tree gen_tmpl
= most_general_template (tmpl
);
3028 if (explicit_instantiation
)
3030 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3031 is done by do_decl_instantiation later. */
3033 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3034 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3036 if (arg_depth
> parm_depth
)
3038 /* If TMPL is not the most general template (for
3039 example, if TMPL is a friend template that is
3040 injected into namespace scope), then there will
3041 be too many levels of TARGS. Remove some of them
3046 new_targs
= make_tree_vec (parm_depth
);
3047 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3048 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3049 = TREE_VEC_ELT (targs
, i
);
3053 return instantiate_template (tmpl
, targs
, tf_error
);
3056 /* If we thought that the DECL was a member function, but it
3057 turns out to be specializing a static member function,
3058 make DECL a static member function as well. */
3059 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3060 && DECL_STATIC_FUNCTION_P (tmpl
)
3061 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3062 revert_static_member_fn (decl
);
3064 /* If this is a specialization of a member template of a
3065 template class, we want to return the TEMPLATE_DECL, not
3066 the specialization of it. */
3067 if (tsk
== tsk_template
&& !was_template_id
)
3069 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3070 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3071 DECL_INITIAL (result
) = NULL_TREE
;
3075 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3076 DECL_SOURCE_LOCATION (result
)
3077 = DECL_SOURCE_LOCATION (decl
);
3078 /* We want to use the argument list specified in the
3079 definition, not in the original declaration. */
3080 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3081 for (parm
= DECL_ARGUMENTS (result
); parm
;
3082 parm
= DECL_CHAIN (parm
))
3083 DECL_CONTEXT (parm
) = result
;
3085 return register_specialization (tmpl
, gen_tmpl
, targs
,
3089 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3090 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3092 if (was_template_id
)
3093 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3095 /* Inherit default function arguments from the template
3096 DECL is specializing. */
3097 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3098 copy_default_args_to_explicit_spec (decl
);
3100 /* This specialization has the same protection as the
3101 template it specializes. */
3102 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3103 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3105 /* 7.1.1-1 [dcl.stc]
3107 A storage-class-specifier shall not be specified in an
3108 explicit specialization...
3110 The parser rejects these, so unless action is taken here,
3111 explicit function specializations will always appear with
3114 The action recommended by the C++ CWG in response to C++
3115 defect report 605 is to make the storage class and linkage
3116 of the explicit specialization match the templated function:
3118 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3120 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3122 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3123 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3125 /* A concept cannot be specialized. */
3126 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3128 error ("explicit specialization of function concept %qD",
3130 return error_mark_node
;
3133 /* This specialization has the same linkage and visibility as
3134 the function template it specializes. */
3135 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3136 if (! TREE_PUBLIC (decl
))
3138 DECL_INTERFACE_KNOWN (decl
) = 1;
3139 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3141 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3142 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3144 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3145 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3149 /* If DECL is a friend declaration, declared using an
3150 unqualified name, the namespace associated with DECL may
3151 have been set incorrectly. For example, in:
3153 template <typename T> void f(T);
3155 struct S { friend void f<int>(int); }
3158 we will have set the DECL_CONTEXT for the friend
3159 declaration to N, rather than to the global namespace. */
3160 if (DECL_NAMESPACE_SCOPE_P (decl
))
3161 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3163 if (is_friend
&& !have_def
)
3164 /* This is not really a declaration of a specialization.
3165 It's just the name of an instantiation. But, it's not
3166 a request for an instantiation, either. */
3167 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3168 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3169 /* A specialization is not necessarily COMDAT. */
3170 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3171 && DECL_DECLARED_INLINE_P (decl
));
3172 else if (VAR_P (decl
))
3173 DECL_COMDAT (decl
) = false;
3175 /* If this is a full specialization, register it so that we can find
3176 it again. Partial specializations will be registered in
3177 process_partial_specialization. */
3178 if (!processing_template_decl
)
3180 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3182 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3187 /* A 'structor should already have clones. */
3188 gcc_assert (decl
== error_mark_node
3189 || variable_template_p (tmpl
)
3190 || !(DECL_CONSTRUCTOR_P (decl
)
3191 || DECL_DESTRUCTOR_P (decl
))
3192 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3199 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3200 parameters. These are represented in the same format used for
3201 DECL_TEMPLATE_PARMS. */
3204 comp_template_parms (const_tree parms1
, const_tree parms2
)
3209 if (parms1
== parms2
)
3212 for (p1
= parms1
, p2
= parms2
;
3213 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3214 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3216 tree t1
= TREE_VALUE (p1
);
3217 tree t2
= TREE_VALUE (p2
);
3220 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3221 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3223 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3226 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3228 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3229 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3231 /* If either of the template parameters are invalid, assume
3232 they match for the sake of error recovery. */
3233 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3236 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3239 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3240 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3241 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3243 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3248 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3249 /* One set of parameters has more parameters lists than the
3256 /* Determine whether PARM is a parameter pack. */
3259 template_parameter_pack_p (const_tree parm
)
3261 /* Determine if we have a non-type template parameter pack. */
3262 if (TREE_CODE (parm
) == PARM_DECL
)
3263 return (DECL_TEMPLATE_PARM_P (parm
)
3264 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3265 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3266 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3268 /* If this is a list of template parameters, we could get a
3269 TYPE_DECL or a TEMPLATE_DECL. */
3270 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3271 parm
= TREE_TYPE (parm
);
3273 /* Otherwise it must be a type template parameter. */
3274 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3275 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3276 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3279 /* Determine if T is a function parameter pack. */
3282 function_parameter_pack_p (const_tree t
)
3284 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3285 return DECL_PACK_P (t
);
3289 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3290 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3293 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3295 if (! primary_func_tmpl_inst
3296 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3297 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3300 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3303 /* Return true iff the function parameter PARAM_DECL was expanded
3304 from the function parameter pack PACK. */
3307 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3309 if (DECL_ARTIFICIAL (param_decl
)
3310 || !function_parameter_pack_p (pack
))
3313 /* The parameter pack and its pack arguments have the same
3315 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3318 /* Determine whether ARGS describes a variadic template args list,
3319 i.e., one that is terminated by a template argument pack. */
3322 template_args_variadic_p (tree args
)
3327 if (args
== NULL_TREE
)
3330 args
= INNERMOST_TEMPLATE_ARGS (args
);
3331 nargs
= TREE_VEC_LENGTH (args
);
3336 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3338 return ARGUMENT_PACK_P (last_parm
);
3341 /* Generate a new name for the parameter pack name NAME (an
3342 IDENTIFIER_NODE) that incorporates its */
3345 make_ith_pack_parameter_name (tree name
, int i
)
3347 /* Munge the name to include the parameter index. */
3348 #define NUMBUF_LEN 128
3349 char numbuf
[NUMBUF_LEN
];
3353 if (name
== NULL_TREE
)
3355 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3356 newname_len
= IDENTIFIER_LENGTH (name
)
3357 + strlen (numbuf
) + 2;
3358 newname
= (char*)alloca (newname_len
);
3359 snprintf (newname
, newname_len
,
3360 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3361 return get_identifier (newname
);
3364 /* Return true if T is a primary function, class or alias template
3365 specialization, not including the template pattern. */
3368 primary_template_specialization_p (const_tree t
)
3373 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3374 return (DECL_LANG_SPECIFIC (t
)
3375 && DECL_USE_TEMPLATE (t
)
3376 && DECL_TEMPLATE_INFO (t
)
3377 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3378 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3379 return (CLASSTYPE_TEMPLATE_INFO (t
)
3380 && CLASSTYPE_USE_TEMPLATE (t
)
3381 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3382 else if (alias_template_specialization_p (t
))
3387 /* Return true if PARM is a template template parameter. */
3390 template_template_parameter_p (const_tree parm
)
3392 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3395 /* Return true iff PARM is a DECL representing a type template
3399 template_type_parameter_p (const_tree parm
)
3402 && (TREE_CODE (parm
) == TYPE_DECL
3403 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3404 && DECL_TEMPLATE_PARM_P (parm
));
3407 /* Return the template parameters of T if T is a
3408 primary template instantiation, NULL otherwise. */
3411 get_primary_template_innermost_parameters (const_tree t
)
3413 tree parms
= NULL
, template_info
= NULL
;
3415 if ((template_info
= get_template_info (t
))
3416 && primary_template_specialization_p (t
))
3417 parms
= INNERMOST_TEMPLATE_PARMS
3418 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3423 /* Return the template parameters of the LEVELth level from the full list
3424 of template parameters PARMS. */
3427 get_template_parms_at_level (tree parms
, int level
)
3431 || TREE_CODE (parms
) != TREE_LIST
3432 || level
> TMPL_PARMS_DEPTH (parms
))
3435 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3436 if (TMPL_PARMS_DEPTH (p
) == level
)
3442 /* Returns the template arguments of T if T is a template instantiation,
3446 get_template_innermost_arguments (const_tree t
)
3448 tree args
= NULL
, template_info
= NULL
;
3450 if ((template_info
= get_template_info (t
))
3451 && TI_ARGS (template_info
))
3452 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3457 /* Return the argument pack elements of T if T is a template argument pack,
3461 get_template_argument_pack_elems (const_tree t
)
3463 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3464 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3467 return ARGUMENT_PACK_ARGS (t
);
3470 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3471 ARGUMENT_PACK_SELECT represents. */
3474 argument_pack_select_arg (tree t
)
3476 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3477 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3479 /* If the selected argument is an expansion E, that most likely means we were
3480 called from gen_elem_of_pack_expansion_instantiation during the
3481 substituting of an argument pack (of which the Ith element is a pack
3482 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3483 In this case, the Ith element resulting from this substituting is going to
3484 be a pack expansion, which pattern is the pattern of E. Let's return the
3485 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3486 resulting pack expansion from it. */
3487 if (PACK_EXPANSION_P (arg
))
3489 /* Make sure we aren't throwing away arg info. */
3490 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3491 arg
= PACK_EXPANSION_PATTERN (arg
);
3498 /* True iff FN is a function representing a built-in variadic parameter
3502 builtin_pack_fn_p (tree fn
)
3505 || TREE_CODE (fn
) != FUNCTION_DECL
3506 || !DECL_IS_BUILTIN (fn
))
3509 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3515 /* True iff CALL is a call to a function representing a built-in variadic
3519 builtin_pack_call_p (tree call
)
3521 if (TREE_CODE (call
) != CALL_EXPR
)
3523 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3526 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3529 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3532 tree ohi
= CALL_EXPR_ARG (call
, 0);
3533 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3534 false/*fn*/, true/*int_cst*/);
3536 if (value_dependent_expression_p (hi
))
3540 call
= copy_node (call
);
3541 CALL_EXPR_ARG (call
, 0) = hi
;
3543 tree ex
= make_pack_expansion (call
, complain
);
3544 tree vec
= make_tree_vec (1);
3545 TREE_VEC_ELT (vec
, 0) = ex
;
3550 hi
= cxx_constant_value (hi
);
3551 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3553 /* Calculate the largest value of len that won't make the size of the vec
3554 overflow an int. The compiler will exceed resource limits long before
3555 this, but it seems a decent place to diagnose. */
3556 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3558 if (len
< 0 || len
> max
)
3560 if ((complain
& tf_error
)
3561 && hi
!= error_mark_node
)
3562 error ("argument to __integer_pack must be between 0 and %d", max
);
3563 return error_mark_node
;
3566 tree vec
= make_tree_vec (len
);
3568 for (int i
= 0; i
< len
; ++i
)
3569 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3575 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3579 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3582 if (!builtin_pack_call_p (call
))
3585 tree fn
= CALL_EXPR_FN (call
);
3587 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3588 return expand_integer_pack (call
, args
, complain
, in_decl
);
3593 /* Structure used to track the progress of find_parameter_packs_r. */
3594 struct find_parameter_pack_data
3596 /* TREE_LIST that will contain all of the parameter packs found by
3598 tree
* parameter_packs
;
3600 /* Set of AST nodes that have been visited by the traversal. */
3601 hash_set
<tree
> *visited
;
3603 /* True iff we're making a type pack expansion. */
3604 bool type_pack_expansion_p
;
3607 /* Identifies all of the argument packs that occur in a template
3608 argument and appends them to the TREE_LIST inside DATA, which is a
3609 find_parameter_pack_data structure. This is a subroutine of
3610 make_pack_expansion and uses_parameter_packs. */
3612 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3615 struct find_parameter_pack_data
* ppd
=
3616 (struct find_parameter_pack_data
*)data
;
3617 bool parameter_pack_p
= false;
3619 /* Handle type aliases/typedefs. */
3620 if (TYPE_ALIAS_P (t
))
3622 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3623 cp_walk_tree (&TI_ARGS (tinfo
),
3624 &find_parameter_packs_r
,
3630 /* Identify whether this is a parameter pack or not. */
3631 switch (TREE_CODE (t
))
3633 case TEMPLATE_PARM_INDEX
:
3634 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3635 parameter_pack_p
= true;
3638 case TEMPLATE_TYPE_PARM
:
3639 t
= TYPE_MAIN_VARIANT (t
);
3641 case TEMPLATE_TEMPLATE_PARM
:
3642 /* If the placeholder appears in the decl-specifier-seq of a function
3643 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3644 is a pack expansion, the invented template parameter is a template
3646 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3647 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3648 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3649 parameter_pack_p
= true;
3654 if (DECL_PACK_P (t
))
3656 /* We don't want to walk into the type of a PARM_DECL,
3657 because we don't want to see the type parameter pack. */
3659 parameter_pack_p
= true;
3664 if (DECL_PACK_P (t
))
3666 /* We don't want to walk into the type of a variadic capture proxy,
3667 because we don't want to see the type parameter pack. */
3669 parameter_pack_p
= true;
3671 else if (variable_template_specialization_p (t
))
3673 cp_walk_tree (&DECL_TI_ARGS (t
),
3674 find_parameter_packs_r
,
3681 if (builtin_pack_call_p (t
))
3682 parameter_pack_p
= true;
3686 parameter_pack_p
= true;
3689 /* Not a parameter pack. */
3693 if (parameter_pack_p
)
3695 /* Add this parameter pack to the list. */
3696 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3700 cp_walk_tree (&TYPE_CONTEXT (t
),
3701 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3703 /* This switch statement will return immediately if we don't find a
3705 switch (TREE_CODE (t
))
3707 case TEMPLATE_PARM_INDEX
:
3710 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3711 /* Check the template itself. */
3712 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3713 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3714 /* Check the template arguments. */
3715 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3720 case TEMPLATE_TYPE_PARM
:
3721 case TEMPLATE_TEMPLATE_PARM
:
3728 /* Ignore the declaration of a capture proxy for a parameter pack. */
3729 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3734 if (TYPE_PTRMEMFUNC_P (t
))
3740 if (TYPE_TEMPLATE_INFO (t
))
3741 cp_walk_tree (&TYPE_TI_ARGS (t
),
3742 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3748 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3753 cp_walk_tree (&TREE_TYPE (t
),
3754 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3758 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3763 case TYPE_PACK_EXPANSION
:
3764 case EXPR_PACK_EXPANSION
:
3769 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3774 case IDENTIFIER_NODE
:
3775 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3782 /* Look at explicit captures. */
3783 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3784 cap
; cap
= TREE_CHAIN (cap
))
3785 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3787 /* Since we defer implicit capture, look in the parms and body. */
3788 tree fn
= lambda_function (t
);
3789 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3791 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3799 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3800 type_pack_expansion_p to false so that any placeholders
3801 within the expression don't get marked as parameter packs. */
3802 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3803 ppd
->type_pack_expansion_p
= false;
3804 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3806 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3812 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3814 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3816 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3818 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3829 /* Determines if the expression or type T uses any parameter packs. */
3831 uses_parameter_packs (tree t
)
3833 tree parameter_packs
= NULL_TREE
;
3834 struct find_parameter_pack_data ppd
;
3835 ppd
.parameter_packs
= ¶meter_packs
;
3836 ppd
.visited
= new hash_set
<tree
>;
3837 ppd
.type_pack_expansion_p
= false;
3838 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3840 return parameter_packs
!= NULL_TREE
;
3843 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3844 representation a base-class initializer into a parameter pack
3845 expansion. If all goes well, the resulting node will be an
3846 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3849 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3852 tree parameter_packs
= NULL_TREE
;
3853 bool for_types
= false;
3854 struct find_parameter_pack_data ppd
;
3856 if (!arg
|| arg
== error_mark_node
)
3859 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3861 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3862 class initializer. In this case, the TREE_PURPOSE will be a
3863 _TYPE node (representing the base class expansion we're
3864 initializing) and the TREE_VALUE will be a TREE_LIST
3865 containing the initialization arguments.
3867 The resulting expansion looks somewhat different from most
3868 expansions. Rather than returning just one _EXPANSION, we
3869 return a TREE_LIST whose TREE_PURPOSE is a
3870 TYPE_PACK_EXPANSION containing the bases that will be
3871 initialized. The TREE_VALUE will be identical to the
3872 original TREE_VALUE, which is a list of arguments that will
3873 be passed to each base. We do not introduce any new pack
3874 expansion nodes into the TREE_VALUE (although it is possible
3875 that some already exist), because the TREE_PURPOSE and
3876 TREE_VALUE all need to be expanded together with the same
3877 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3878 resulting TREE_PURPOSE will mention the parameter packs in
3879 both the bases and the arguments to the bases. */
3882 tree parameter_packs
= NULL_TREE
;
3884 /* Determine which parameter packs will be used by the base
3886 ppd
.visited
= new hash_set
<tree
>;
3887 ppd
.parameter_packs
= ¶meter_packs
;
3888 ppd
.type_pack_expansion_p
= true;
3889 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3890 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3893 if (parameter_packs
== NULL_TREE
)
3895 if (complain
& tf_error
)
3896 error ("base initializer expansion %qT contains no parameter packs",
3899 return error_mark_node
;
3902 if (TREE_VALUE (arg
) != void_type_node
)
3904 /* Collect the sets of parameter packs used in each of the
3905 initialization arguments. */
3906 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3908 /* Determine which parameter packs will be expanded in this
3910 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3917 /* Create the pack expansion type for the base type. */
3918 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3919 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3920 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3921 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3923 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3924 they will rarely be compared to anything. */
3925 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3927 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3930 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3933 /* Build the PACK_EXPANSION_* node. */
3935 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3936 : make_node (EXPR_PACK_EXPANSION
);
3937 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3938 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3940 /* Propagate type and const-expression information. */
3941 TREE_TYPE (result
) = TREE_TYPE (arg
);
3942 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3943 /* Mark this read now, since the expansion might be length 0. */
3944 mark_exp_read (arg
);
3947 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3948 they will rarely be compared to anything. */
3949 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3951 /* Determine which parameter packs will be expanded. */
3952 ppd
.parameter_packs
= ¶meter_packs
;
3953 ppd
.visited
= new hash_set
<tree
>;
3954 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3955 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3958 /* Make sure we found some parameter packs. */
3959 if (parameter_packs
== NULL_TREE
)
3961 if (complain
& tf_error
)
3964 error ("expansion pattern %qT contains no parameter packs", arg
);
3966 error ("expansion pattern %qE contains no parameter packs", arg
);
3968 return error_mark_node
;
3970 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3972 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3977 /* Checks T for any "bare" parameter packs, which have not yet been
3978 expanded, and issues an error if any are found. This operation can
3979 only be done on full expressions or types (e.g., an expression
3980 statement, "if" condition, etc.), because we could have expressions like:
3982 foo(f(g(h(args)))...)
3984 where "args" is a parameter pack. check_for_bare_parameter_packs
3985 should not be called for the subexpressions args, h(args),
3986 g(h(args)), or f(g(h(args))), because we would produce erroneous
3989 Returns TRUE and emits an error if there were bare parameter packs,
3990 returns FALSE otherwise. */
3992 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
3994 tree parameter_packs
= NULL_TREE
;
3995 struct find_parameter_pack_data ppd
;
3997 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4000 /* A lambda might use a parameter pack from the containing context. */
4001 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4002 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4005 if (TREE_CODE (t
) == TYPE_DECL
)
4008 ppd
.parameter_packs
= ¶meter_packs
;
4009 ppd
.visited
= new hash_set
<tree
>;
4010 ppd
.type_pack_expansion_p
= false;
4011 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4014 if (parameter_packs
)
4016 if (loc
== UNKNOWN_LOCATION
)
4017 loc
= cp_expr_loc_or_loc (t
, input_location
);
4018 error_at (loc
, "parameter packs not expanded with %<...%>:");
4019 while (parameter_packs
)
4021 tree pack
= TREE_VALUE (parameter_packs
);
4022 tree name
= NULL_TREE
;
4024 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4025 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4026 name
= TYPE_NAME (pack
);
4027 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4028 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4029 else if (TREE_CODE (pack
) == CALL_EXPR
)
4030 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4032 name
= DECL_NAME (pack
);
4035 inform (loc
, " %qD", name
);
4037 inform (loc
, " <anonymous>");
4039 parameter_packs
= TREE_CHAIN (parameter_packs
);
4048 /* Expand any parameter packs that occur in the template arguments in
4051 expand_template_argument_pack (tree args
)
4053 if (args
== error_mark_node
)
4054 return error_mark_node
;
4056 tree result_args
= NULL_TREE
;
4057 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4058 int num_result_args
= -1;
4059 int non_default_args_count
= -1;
4061 /* First, determine if we need to expand anything, and the number of
4062 slots we'll need. */
4063 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4065 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4066 if (arg
== NULL_TREE
)
4068 if (ARGUMENT_PACK_P (arg
))
4070 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4071 if (num_result_args
< 0)
4072 num_result_args
= in_arg
+ num_packed
;
4074 num_result_args
+= num_packed
;
4078 if (num_result_args
>= 0)
4083 /* If no expansion is necessary, we're done. */
4084 if (num_result_args
< 0)
4087 /* Expand arguments. */
4088 result_args
= make_tree_vec (num_result_args
);
4089 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4090 non_default_args_count
=
4091 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4092 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4094 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4095 if (ARGUMENT_PACK_P (arg
))
4097 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4098 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4099 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4100 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4101 if (non_default_args_count
> 0)
4102 non_default_args_count
+= num_packed
- 1;
4106 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4110 if (non_default_args_count
>= 0)
4111 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4115 /* Checks if DECL shadows a template parameter.
4117 [temp.local]: A template-parameter shall not be redeclared within its
4118 scope (including nested scopes).
4120 Emits an error and returns TRUE if the DECL shadows a parameter,
4121 returns FALSE otherwise. */
4124 check_template_shadow (tree decl
)
4128 /* If we're not in a template, we can't possibly shadow a template
4130 if (!current_template_parms
)
4133 /* Figure out what we're shadowing. */
4134 decl
= OVL_FIRST (decl
);
4135 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4137 /* If there's no previous binding for this name, we're not shadowing
4138 anything, let alone a template parameter. */
4142 /* If we're not shadowing a template parameter, we're done. Note
4143 that OLDDECL might be an OVERLOAD (or perhaps even an
4144 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4146 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4149 /* We check for decl != olddecl to avoid bogus errors for using a
4150 name inside a class. We check TPFI to avoid duplicate errors for
4151 inline member templates. */
4153 || (DECL_TEMPLATE_PARM_P (decl
)
4154 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4157 /* Don't complain about the injected class name, as we've already
4158 complained about the class itself. */
4159 if (DECL_SELF_REFERENCE_P (decl
))
4162 if (DECL_TEMPLATE_PARM_P (decl
))
4163 error ("declaration of template parameter %q+D shadows "
4164 "template parameter", decl
);
4166 error ("declaration of %q+#D shadows template parameter", decl
);
4167 inform (DECL_SOURCE_LOCATION (olddecl
),
4168 "template parameter %qD declared here", olddecl
);
4172 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4173 ORIG_LEVEL, DECL, and TYPE. */
4176 build_template_parm_index (int index
,
4182 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4183 TEMPLATE_PARM_IDX (t
) = index
;
4184 TEMPLATE_PARM_LEVEL (t
) = level
;
4185 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4186 TEMPLATE_PARM_DECL (t
) = decl
;
4187 TREE_TYPE (t
) = type
;
4188 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4189 TREE_READONLY (t
) = TREE_READONLY (decl
);
4194 /* Find the canonical type parameter for the given template type
4195 parameter. Returns the canonical type parameter, which may be TYPE
4196 if no such parameter existed. */
4199 canonical_type_parameter (tree type
)
4202 int idx
= TEMPLATE_TYPE_IDX (type
);
4203 if (!canonical_template_parms
)
4204 vec_alloc (canonical_template_parms
, idx
+ 1);
4206 if (canonical_template_parms
->length () <= (unsigned) idx
)
4207 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4209 list
= (*canonical_template_parms
)[idx
];
4210 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4211 list
= TREE_CHAIN (list
);
4214 return TREE_VALUE (list
);
4217 (*canonical_template_parms
)[idx
]
4218 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4223 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4224 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4225 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4226 new one is created. */
4229 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4230 tsubst_flags_t complain
)
4232 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4233 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4234 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4235 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4237 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4240 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4241 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4242 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4243 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4244 DECL_ARTIFICIAL (decl
) = 1;
4245 SET_DECL_TEMPLATE_PARM_P (decl
);
4247 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4248 TEMPLATE_PARM_LEVEL (index
) - levels
,
4249 TEMPLATE_PARM_ORIG_LEVEL (index
),
4251 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4252 TEMPLATE_PARM_PARAMETER_PACK (t
)
4253 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4255 /* Template template parameters need this. */
4256 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4258 DECL_TEMPLATE_RESULT (decl
)
4259 = build_decl (DECL_SOURCE_LOCATION (decl
),
4260 TYPE_DECL
, DECL_NAME (decl
), type
);
4261 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4262 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4263 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4267 return TEMPLATE_PARM_DESCENDANTS (index
);
4270 /* Process information from new template parameter PARM and append it
4271 to the LIST being built. This new parameter is a non-type
4272 parameter iff IS_NON_TYPE is true. This new parameter is a
4273 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4277 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4278 bool is_non_type
, bool is_parameter_pack
)
4283 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4284 tree defval
= TREE_PURPOSE (parm
);
4285 tree constr
= TREE_TYPE (parm
);
4289 tree p
= tree_last (list
);
4291 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4294 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4295 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4297 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4305 parm
= TREE_VALUE (parm
);
4307 SET_DECL_TEMPLATE_PARM_P (parm
);
4309 if (TREE_TYPE (parm
) != error_mark_node
)
4313 The top-level cv-qualifiers on the template-parameter are
4314 ignored when determining its type. */
4315 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4316 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4317 TREE_TYPE (parm
) = error_mark_node
;
4318 else if (uses_parameter_packs (TREE_TYPE (parm
))
4319 && !is_parameter_pack
4320 /* If we're in a nested template parameter list, the template
4321 template parameter could be a parameter pack. */
4322 && processing_template_parmlist
== 1)
4324 /* This template parameter is not a parameter pack, but it
4325 should be. Complain about "bare" parameter packs. */
4326 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4328 /* Recover by calling this a parameter pack. */
4329 is_parameter_pack
= true;
4333 /* A template parameter is not modifiable. */
4334 TREE_CONSTANT (parm
) = 1;
4335 TREE_READONLY (parm
) = 1;
4336 decl
= build_decl (parm_loc
,
4337 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4338 TREE_CONSTANT (decl
) = 1;
4339 TREE_READONLY (decl
) = 1;
4340 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4341 = build_template_parm_index (idx
, processing_template_decl
,
4342 processing_template_decl
,
4343 decl
, TREE_TYPE (parm
));
4345 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4346 = is_parameter_pack
;
4351 parm
= TREE_VALUE (TREE_VALUE (parm
));
4353 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4355 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4356 /* This is for distinguishing between real templates and template
4357 template parameters */
4358 TREE_TYPE (parm
) = t
;
4359 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4364 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4365 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4366 decl
= build_decl (parm_loc
,
4367 TYPE_DECL
, parm
, t
);
4370 TYPE_NAME (t
) = decl
;
4371 TYPE_STUB_DECL (t
) = decl
;
4373 TEMPLATE_TYPE_PARM_INDEX (t
)
4374 = build_template_parm_index (idx
, processing_template_decl
,
4375 processing_template_decl
,
4376 decl
, TREE_TYPE (parm
));
4377 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4378 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4380 DECL_ARTIFICIAL (decl
) = 1;
4381 SET_DECL_TEMPLATE_PARM_P (decl
);
4383 /* Build requirements for the type/template parameter.
4384 This must be done after SET_DECL_TEMPLATE_PARM_P or
4385 process_template_parm could fail. */
4386 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4390 /* Build the parameter node linking the parameter declaration,
4391 its default argument (if any), and its constraints (if any). */
4392 parm
= build_tree_list (defval
, parm
);
4393 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4395 return chainon (list
, parm
);
4398 /* The end of a template parameter list has been reached. Process the
4399 tree list into a parameter vector, converting each parameter into a more
4400 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4404 end_template_parm_list (tree parms
)
4408 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4410 /* Pop the dummy parameter level and add the real one. */
4411 current_template_parms
= TREE_CHAIN (current_template_parms
);
4413 current_template_parms
4414 = tree_cons (size_int (processing_template_decl
),
4415 saved_parmlist
, current_template_parms
);
4417 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4419 next
= TREE_CHAIN (parm
);
4420 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4421 TREE_CHAIN (parm
) = NULL_TREE
;
4424 --processing_template_parmlist
;
4426 return saved_parmlist
;
4429 // Explicitly indicate the end of the template parameter list. We assume
4430 // that the current template parameters have been constructed and/or
4431 // managed explicitly, as when creating new template template parameters
4432 // from a shorthand constraint.
4434 end_template_parm_list ()
4436 --processing_template_parmlist
;
4439 /* end_template_decl is called after a template declaration is seen. */
4442 end_template_decl (void)
4444 reset_specialization ();
4446 if (! processing_template_decl
)
4449 /* This matches the pushlevel in begin_template_parm_list. */
4452 --processing_template_decl
;
4453 current_template_parms
= TREE_CHAIN (current_template_parms
);
4456 /* Takes a TREE_LIST representing a template parameter and convert it
4457 into an argument suitable to be passed to the type substitution
4458 functions. Note that If the TREE_LIST contains an error_mark
4459 node, the returned argument is error_mark_node. */
4462 template_parm_to_arg (tree t
)
4466 || TREE_CODE (t
) != TREE_LIST
)
4469 if (error_operand_p (TREE_VALUE (t
)))
4470 return error_mark_node
;
4474 if (TREE_CODE (t
) == TYPE_DECL
4475 || TREE_CODE (t
) == TEMPLATE_DECL
)
4479 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4481 /* Turn this argument into a TYPE_ARGUMENT_PACK
4482 with a single element, which expands T. */
4483 tree vec
= make_tree_vec (1);
4485 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4487 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4489 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4490 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4495 t
= DECL_INITIAL (t
);
4497 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4499 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4500 with a single element, which expands T. */
4501 tree vec
= make_tree_vec (1);
4503 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4505 t
= convert_from_reference (t
);
4506 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4508 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4509 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4512 t
= convert_from_reference (t
);
4517 /* Given a single level of template parameters (a TREE_VEC), return it
4518 as a set of template arguments. */
4521 template_parms_level_to_args (tree parms
)
4523 tree a
= copy_node (parms
);
4524 TREE_TYPE (a
) = NULL_TREE
;
4525 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4526 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4529 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4534 /* Given a set of template parameters, return them as a set of template
4535 arguments. The template parameters are represented as a TREE_VEC, in
4536 the form documented in cp-tree.h for template arguments. */
4539 template_parms_to_args (tree parms
)
4542 tree args
= NULL_TREE
;
4543 int length
= TMPL_PARMS_DEPTH (parms
);
4546 /* If there is only one level of template parameters, we do not
4547 create a TREE_VEC of TREE_VECs. Instead, we return a single
4548 TREE_VEC containing the arguments. */
4550 args
= make_tree_vec (length
);
4552 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4554 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4557 TREE_VEC_ELT (args
, --l
) = a
;
4565 /* Within the declaration of a template, return the currently active
4566 template parameters as an argument TREE_VEC. */
4569 current_template_args (void)
4571 return template_parms_to_args (current_template_parms
);
4574 /* Update the declared TYPE by doing any lookups which were thought to be
4575 dependent, but are not now that we know the SCOPE of the declarator. */
4578 maybe_update_decl_type (tree orig_type
, tree scope
)
4580 tree type
= orig_type
;
4582 if (type
== NULL_TREE
)
4585 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4586 type
= TREE_TYPE (type
);
4588 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4589 && dependent_type_p (type
)
4590 /* Don't bother building up the args in this case. */
4591 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4593 /* tsubst in the args corresponding to the template parameters,
4594 including auto if present. Most things will be unchanged, but
4595 make_typename_type and tsubst_qualified_id will resolve
4596 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4597 tree args
= current_template_args ();
4598 tree auto_node
= type_uses_auto (type
);
4602 tree auto_vec
= make_tree_vec (1);
4603 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4604 args
= add_to_template_args (args
, auto_vec
);
4606 pushed
= push_scope (scope
);
4607 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4612 if (type
== error_mark_node
)
4615 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4617 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4620 type
= TYPE_NAME (type
);
4625 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4626 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4627 the new template is a member template. */
4630 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4632 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4633 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4634 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4635 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4636 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4637 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4642 struct template_parm_data
4644 /* The level of the template parameters we are currently
4648 /* The index of the specialization argument we are currently
4652 /* An array whose size is the number of template parameters. The
4653 elements are nonzero if the parameter has been used in any one
4654 of the arguments processed so far. */
4657 /* An array whose size is the number of template arguments. The
4658 elements are nonzero if the argument makes use of template
4659 parameters of this level. */
4660 int* arg_uses_template_parms
;
4663 /* Subroutine of push_template_decl used to see if each template
4664 parameter in a partial specialization is used in the explicit
4665 argument list. If T is of the LEVEL given in DATA (which is
4666 treated as a template_parm_data*), then DATA->PARMS is marked
4670 mark_template_parm (tree t
, void* data
)
4674 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4676 template_parm_level_and_index (t
, &level
, &idx
);
4678 if (level
== tpd
->level
)
4680 tpd
->parms
[idx
] = 1;
4681 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4684 /* In C++17 the type of a non-type argument is a deduced context. */
4685 if (cxx_dialect
>= cxx17
4686 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4687 for_each_template_parm (TREE_TYPE (t
),
4688 &mark_template_parm
,
4691 /*include_nondeduced_p=*/false);
4693 /* Return zero so that for_each_template_parm will continue the
4694 traversal of the tree; we want to mark *every* template parm. */
4698 /* Process the partial specialization DECL. */
4701 process_partial_specialization (tree decl
)
4703 tree type
= TREE_TYPE (decl
);
4704 tree tinfo
= get_template_info (decl
);
4705 tree maintmpl
= TI_TEMPLATE (tinfo
);
4706 tree specargs
= TI_ARGS (tinfo
);
4707 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4708 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4711 int nargs
= TREE_VEC_LENGTH (inner_args
);
4714 bool did_error_intro
= false;
4715 struct template_parm_data tpd
;
4716 struct template_parm_data tpd2
;
4718 gcc_assert (current_template_parms
);
4720 /* A concept cannot be specialized. */
4721 if (flag_concepts
&& variable_concept_p (maintmpl
))
4723 error ("specialization of variable concept %q#D", maintmpl
);
4724 return error_mark_node
;
4727 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4728 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4730 /* We check that each of the template parameters given in the
4731 partial specialization is used in the argument list to the
4732 specialization. For example:
4734 template <class T> struct S;
4735 template <class T> struct S<T*>;
4737 The second declaration is OK because `T*' uses the template
4738 parameter T, whereas
4740 template <class T> struct S<int>;
4742 is no good. Even trickier is:
4753 The S2<T> declaration is actually invalid; it is a
4754 full-specialization. Of course,
4757 struct S2<T (*)(U)>;
4759 or some such would have been OK. */
4760 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4761 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4762 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4764 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4765 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4766 for (i
= 0; i
< nargs
; ++i
)
4768 tpd
.current_arg
= i
;
4769 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4770 &mark_template_parm
,
4773 /*include_nondeduced_p=*/false);
4775 for (i
= 0; i
< ntparms
; ++i
)
4776 if (tpd
.parms
[i
] == 0)
4778 /* One of the template parms was not used in a deduced context in the
4780 if (!did_error_intro
)
4782 error ("template parameters not deducible in "
4783 "partial specialization:");
4784 did_error_intro
= true;
4787 inform (input_location
, " %qD",
4788 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4791 if (did_error_intro
)
4792 return error_mark_node
;
4794 /* [temp.class.spec]
4796 The argument list of the specialization shall not be identical to
4797 the implicit argument list of the primary template. */
4799 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4800 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4802 || !strictly_subsumes (current_template_constraints (),
4803 get_constraints (maintmpl
))))
4806 error ("partial specialization %q+D does not specialize "
4807 "any template arguments; to define the primary template, "
4808 "remove the template argument list", decl
);
4810 error ("partial specialization %q+D does not specialize any "
4811 "template arguments and is not more constrained than "
4812 "the primary template; to define the primary template, "
4813 "remove the template argument list", decl
);
4814 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4817 /* A partial specialization that replaces multiple parameters of the
4818 primary template with a pack expansion is less specialized for those
4820 if (nargs
< DECL_NTPARMS (maintmpl
))
4822 error ("partial specialization is not more specialized than the "
4823 "primary template because it replaces multiple parameters "
4824 "with a pack expansion");
4825 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4826 /* Avoid crash in process_partial_specialization. */
4830 /* If we aren't in a dependent class, we can actually try deduction. */
4831 else if (tpd
.level
== 1
4832 /* FIXME we should be able to handle a partial specialization of a
4833 partial instantiation, but currently we can't (c++/41727). */
4834 && TMPL_ARGS_DEPTH (specargs
) == 1
4835 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4837 auto_diagnostic_group d
;
4838 if (permerror (input_location
, "partial specialization %qD is not "
4839 "more specialized than", decl
))
4840 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4844 /* [temp.class.spec]
4846 A partially specialized non-type argument expression shall not
4847 involve template parameters of the partial specialization except
4848 when the argument expression is a simple identifier.
4850 The type of a template parameter corresponding to a specialized
4851 non-type argument shall not be dependent on a parameter of the
4854 Also, we verify that pack expansions only occur at the
4855 end of the argument list. */
4856 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4858 for (i
= 0; i
< nargs
; ++i
)
4860 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4861 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4862 tree packed_args
= NULL_TREE
;
4865 if (ARGUMENT_PACK_P (arg
))
4867 /* Extract the arguments from the argument pack. We'll be
4868 iterating over these in the following loop. */
4869 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4870 len
= TREE_VEC_LENGTH (packed_args
);
4873 for (j
= 0; j
< len
; j
++)
4876 /* Get the Jth argument in the parameter pack. */
4877 arg
= TREE_VEC_ELT (packed_args
, j
);
4879 if (PACK_EXPANSION_P (arg
))
4881 /* Pack expansions must come at the end of the
4883 if ((packed_args
&& j
< len
- 1)
4884 || (!packed_args
&& i
< nargs
- 1))
4886 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4887 error ("parameter pack argument %qE must be at the "
4888 "end of the template argument list", arg
);
4890 error ("parameter pack argument %qT must be at the "
4891 "end of the template argument list", arg
);
4895 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4896 /* We only care about the pattern. */
4897 arg
= PACK_EXPANSION_PATTERN (arg
);
4899 if (/* These first two lines are the `non-type' bit. */
4901 && TREE_CODE (arg
) != TEMPLATE_DECL
4902 /* This next two lines are the `argument expression is not just a
4903 simple identifier' condition and also the `specialized
4904 non-type argument' bit. */
4905 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4906 && !(REFERENCE_REF_P (arg
)
4907 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4909 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4910 || (packed_args
&& uses_template_parms (arg
)))
4911 error ("template argument %qE involves template parameter(s)",
4915 /* Look at the corresponding template parameter,
4916 marking which template parameters its type depends
4918 tree type
= TREE_TYPE (parm
);
4922 /* We haven't yet initialized TPD2. Do so now. */
4923 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4924 /* The number of parameters here is the number in the
4925 main template, which, as checked in the assertion
4927 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4929 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4932 /* Mark the template parameters. But this time, we're
4933 looking for the template parameters of the main
4934 template, not in the specialization. */
4935 tpd2
.current_arg
= i
;
4936 tpd2
.arg_uses_template_parms
[i
] = 0;
4937 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4938 for_each_template_parm (type
,
4939 &mark_template_parm
,
4942 /*include_nondeduced_p=*/false);
4944 if (tpd2
.arg_uses_template_parms
[i
])
4946 /* The type depended on some template parameters.
4947 If they are fully specialized in the
4948 specialization, that's OK. */
4951 for (j
= 0; j
< nargs
; ++j
)
4952 if (tpd2
.parms
[j
] != 0
4953 && tpd
.arg_uses_template_parms
[j
])
4956 error_n (input_location
, count
,
4957 "type %qT of template argument %qE depends "
4958 "on a template parameter",
4959 "type %qT of template argument %qE depends "
4960 "on template parameters",
4969 /* We should only get here once. */
4970 if (TREE_CODE (decl
) == TYPE_DECL
)
4971 gcc_assert (!COMPLETE_TYPE_P (type
));
4973 // Build the template decl.
4974 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4975 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4976 TREE_TYPE (tmpl
) = type
;
4977 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4978 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4979 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4980 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4982 /* Give template template parms a DECL_CONTEXT of the template
4983 for which they are a parameter. */
4984 for (i
= 0; i
< ntparms
; ++i
)
4986 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4987 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4988 DECL_CONTEXT (parm
) = tmpl
;
4992 /* We didn't register this in check_explicit_specialization so we could
4993 wait until the constraints were set. */
4994 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4996 associate_classtype_constraints (type
);
4998 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4999 = tree_cons (specargs
, tmpl
,
5000 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5001 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5003 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5004 inst
= TREE_CHAIN (inst
))
5006 tree instance
= TREE_VALUE (inst
);
5007 if (TYPE_P (instance
)
5008 ? (COMPLETE_TYPE_P (instance
)
5009 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5010 : DECL_TEMPLATE_INSTANTIATION (instance
))
5012 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5013 tree inst_decl
= (DECL_P (instance
)
5014 ? instance
: TYPE_NAME (instance
));
5017 else if (spec
== error_mark_node
)
5018 permerror (input_location
,
5019 "declaration of %qD ambiguates earlier template "
5020 "instantiation for %qD", decl
, inst_decl
);
5021 else if (TREE_VALUE (spec
) == tmpl
)
5022 permerror (input_location
,
5023 "partial specialization of %qD after instantiation "
5024 "of %qD", decl
, inst_decl
);
5031 /* PARM is a template parameter of some form; return the corresponding
5032 TEMPLATE_PARM_INDEX. */
5035 get_template_parm_index (tree parm
)
5037 if (TREE_CODE (parm
) == PARM_DECL
5038 || TREE_CODE (parm
) == CONST_DECL
)
5039 parm
= DECL_INITIAL (parm
);
5040 else if (TREE_CODE (parm
) == TYPE_DECL
5041 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5042 parm
= TREE_TYPE (parm
);
5043 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5044 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5045 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5046 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5047 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5051 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5052 parameter packs used by the template parameter PARM. */
5055 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5057 /* A type parm can't refer to another parm. */
5058 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5060 else if (TREE_CODE (parm
) == PARM_DECL
)
5062 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5067 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5069 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5070 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5071 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5074 /* PARM is a template parameter pack. Return any parameter packs used in
5075 its type or the type of any of its template parameters. If there are
5076 any such packs, it will be instantiated into a fixed template parameter
5077 list by partial instantiation rather than be fully deduced. */
5080 fixed_parameter_pack_p (tree parm
)
5082 /* This can only be true in a member template. */
5083 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5085 /* This can only be true for a parameter pack. */
5086 if (!template_parameter_pack_p (parm
))
5088 /* A type parm can't refer to another parm. */
5089 if (TREE_CODE (parm
) == TYPE_DECL
)
5092 tree parameter_packs
= NULL_TREE
;
5093 struct find_parameter_pack_data ppd
;
5094 ppd
.parameter_packs
= ¶meter_packs
;
5095 ppd
.visited
= new hash_set
<tree
>;
5096 ppd
.type_pack_expansion_p
= false;
5098 fixed_parameter_pack_p_1 (parm
, &ppd
);
5101 return parameter_packs
;
5104 /* Check that a template declaration's use of default arguments and
5105 parameter packs is not invalid. Here, PARMS are the template
5106 parameters. IS_PRIMARY is true if DECL is the thing declared by
5107 a primary template. IS_PARTIAL is true if DECL is a partial
5110 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5111 function template declaration or a friend class template
5112 declaration. In the function case, 1 indicates a declaration, 2
5113 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5114 emitted for extraneous default arguments.
5116 Returns TRUE if there were no errors found, FALSE otherwise. */
5119 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5120 bool is_partial
, int is_friend_decl
)
5123 int last_level_to_check
;
5125 bool no_errors
= true;
5129 A default template-argument shall not be specified in a
5130 function template declaration or a function template definition, nor
5131 in the template-parameter-list of the definition of a member of a
5134 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5135 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5136 /* You can't have a function template declaration in a local
5137 scope, nor you can you define a member of a class template in a
5141 if ((TREE_CODE (decl
) == TYPE_DECL
5143 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5144 || (TREE_CODE (decl
) == FUNCTION_DECL
5145 && LAMBDA_FUNCTION_P (decl
)))
5146 /* A lambda doesn't have an explicit declaration; don't complain
5147 about the parms of the enclosing class. */
5150 if (current_class_type
5151 && !TYPE_BEING_DEFINED (current_class_type
)
5152 && DECL_LANG_SPECIFIC (decl
)
5153 && DECL_DECLARES_FUNCTION_P (decl
)
5154 /* If this is either a friend defined in the scope of the class
5155 or a member function. */
5156 && (DECL_FUNCTION_MEMBER_P (decl
)
5157 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5158 : DECL_FRIEND_CONTEXT (decl
)
5159 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5161 /* And, if it was a member function, it really was defined in
5162 the scope of the class. */
5163 && (!DECL_FUNCTION_MEMBER_P (decl
)
5164 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5165 /* We already checked these parameters when the template was
5166 declared, so there's no need to do it again now. This function
5167 was defined in class scope, but we're processing its body now
5168 that the class is complete. */
5171 /* Core issue 226 (C++0x only): the following only applies to class
5174 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5178 If a template-parameter has a default template-argument, all
5179 subsequent template-parameters shall have a default
5180 template-argument supplied. */
5181 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5183 tree inner_parms
= TREE_VALUE (parm_level
);
5184 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5185 int seen_def_arg_p
= 0;
5188 for (i
= 0; i
< ntparms
; ++i
)
5190 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5192 if (parm
== error_mark_node
)
5195 if (TREE_PURPOSE (parm
))
5197 else if (seen_def_arg_p
5198 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5200 error ("no default argument for %qD", TREE_VALUE (parm
));
5201 /* For better subsequent error-recovery, we indicate that
5202 there should have been a default argument. */
5203 TREE_PURPOSE (parm
) = error_mark_node
;
5206 else if (!is_partial
5208 /* Don't complain about an enclosing partial
5210 && parm_level
== parms
5211 && TREE_CODE (decl
) == TYPE_DECL
5213 && template_parameter_pack_p (TREE_VALUE (parm
))
5214 /* A fixed parameter pack will be partially
5215 instantiated into a fixed length list. */
5216 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5218 /* A primary class template can only have one
5219 parameter pack, at the end of the template
5222 error ("parameter pack %q+D must be at the end of the"
5223 " template parameter list", TREE_VALUE (parm
));
5225 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5233 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5237 /* For an ordinary class template, default template arguments are
5238 allowed at the innermost level, e.g.:
5239 template <class T = int>
5241 but, in a partial specialization, they're not allowed even
5242 there, as we have in [temp.class.spec]:
5244 The template parameter list of a specialization shall not
5245 contain default template argument values.
5247 So, for a partial specialization, or for a function template
5248 (in C++98/C++03), we look at all of them. */
5251 /* But, for a primary class template that is not a partial
5252 specialization we look at all template parameters except the
5254 parms
= TREE_CHAIN (parms
);
5256 /* Figure out what error message to issue. */
5257 if (is_friend_decl
== 2)
5258 msg
= G_("default template arguments may not be used in function template "
5259 "friend re-declaration");
5260 else if (is_friend_decl
)
5261 msg
= G_("default template arguments may not be used in template "
5262 "friend declarations");
5263 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5264 msg
= G_("default template arguments may not be used in function templates "
5265 "without -std=c++11 or -std=gnu++11");
5266 else if (is_partial
)
5267 msg
= G_("default template arguments may not be used in "
5268 "partial specializations");
5269 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5270 msg
= G_("default argument for template parameter for class enclosing %qD");
5272 /* Per [temp.param]/9, "A default template-argument shall not be
5273 specified in the template-parameter-lists of the definition of
5274 a member of a class template that appears outside of the member's
5275 class.", thus if we aren't handling a member of a class template
5276 there is no need to examine the parameters. */
5279 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5280 /* If we're inside a class definition, there's no need to
5281 examine the parameters to the class itself. On the one
5282 hand, they will be checked when the class is defined, and,
5283 on the other, default arguments are valid in things like:
5284 template <class T = double>
5285 struct S { template <class U> void f(U); };
5286 Here the default argument for `S' has no bearing on the
5287 declaration of `f'. */
5288 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5290 /* Check everything. */
5291 last_level_to_check
= 0;
5293 for (parm_level
= parms
;
5294 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5295 parm_level
= TREE_CHAIN (parm_level
))
5297 tree inner_parms
= TREE_VALUE (parm_level
);
5301 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5302 for (i
= 0; i
< ntparms
; ++i
)
5304 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5307 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5312 if (is_friend_decl
== 2)
5319 /* Clear out the default argument so that we are not
5321 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5325 /* At this point, if we're still interested in issuing messages,
5326 they must apply to classes surrounding the object declared. */
5328 msg
= G_("default argument for template parameter for class "
5335 /* Worker for push_template_decl_real, called via
5336 for_each_template_parm. DATA is really an int, indicating the
5337 level of the parameters we are interested in. If T is a template
5338 parameter of that level, return nonzero. */
5341 template_parm_this_level_p (tree t
, void* data
)
5343 int this_level
= *(int *)data
;
5346 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5347 level
= TEMPLATE_PARM_LEVEL (t
);
5349 level
= TEMPLATE_TYPE_LEVEL (t
);
5350 return level
== this_level
;
5353 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5354 DATA is really an int, indicating the innermost outer level of parameters.
5355 If T is a template parameter of that level or further out, return
5359 template_parm_outer_level (tree t
, void *data
)
5361 int this_level
= *(int *)data
;
5364 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5365 level
= TEMPLATE_PARM_LEVEL (t
);
5367 level
= TEMPLATE_TYPE_LEVEL (t
);
5368 return level
<= this_level
;
5371 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5372 parameters given by current_template_args, or reuses a
5373 previously existing one, if appropriate. Returns the DECL, or an
5374 equivalent one, if it is replaced via a call to duplicate_decls.
5376 If IS_FRIEND is true, DECL is a friend declaration. */
5379 push_template_decl_real (tree decl
, bool is_friend
)
5387 int new_template_p
= 0;
5388 /* True if the template is a member template, in the sense of
5390 bool member_template_p
= false;
5392 if (decl
== error_mark_node
|| !current_template_parms
)
5393 return error_mark_node
;
5395 /* See if this is a partial specialization. */
5396 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5397 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5398 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5400 && DECL_LANG_SPECIFIC (decl
)
5401 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5402 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5404 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5408 /* For a friend, we want the context of the friend, not
5409 the type of which it is a friend. */
5410 ctx
= CP_DECL_CONTEXT (decl
);
5411 else if (CP_DECL_CONTEXT (decl
)
5412 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5413 /* In the case of a virtual function, we want the class in which
5415 ctx
= CP_DECL_CONTEXT (decl
);
5417 /* Otherwise, if we're currently defining some class, the DECL
5418 is assumed to be a member of the class. */
5419 ctx
= current_scope ();
5421 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5424 if (!DECL_CONTEXT (decl
))
5425 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5427 /* See if this is a primary template. */
5428 if (is_friend
&& ctx
5429 && uses_template_parms_level (ctx
, processing_template_decl
))
5430 /* A friend template that specifies a class context, i.e.
5431 template <typename T> friend void A<T>::f();
5434 else if (TREE_CODE (decl
) == TYPE_DECL
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5438 is_primary
= template_parm_scope_p ();
5442 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5444 if (DECL_CLASS_SCOPE_P (decl
))
5445 member_template_p
= true;
5446 if (TREE_CODE (decl
) == TYPE_DECL
5447 && anon_aggrname_p (DECL_NAME (decl
)))
5449 error ("template class without a name");
5450 return error_mark_node
;
5452 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5454 if (member_template_p
)
5456 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5457 error ("member template %qD may not have virt-specifiers", decl
);
5459 if (DECL_DESTRUCTOR_P (decl
))
5463 A destructor shall not be a member template. */
5464 error ("destructor %qD declared as member template", decl
);
5465 return error_mark_node
;
5467 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5468 && (!prototype_p (TREE_TYPE (decl
))
5469 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5470 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5471 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5472 == void_list_node
)))
5474 /* [basic.stc.dynamic.allocation]
5476 An allocation function can be a function
5477 template. ... Template allocation functions shall
5478 have two or more parameters. */
5479 error ("invalid template declaration of %qD", decl
);
5480 return error_mark_node
;
5483 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5484 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5486 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5487 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5488 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5490 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5491 if (TREE_CODE (t
) == TYPE_DECL
)
5493 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5494 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5497 else if (TREE_CODE (decl
) == TYPE_DECL
5498 && TYPE_DECL_ALIAS_P (decl
))
5499 /* alias-declaration */
5500 gcc_assert (!DECL_ARTIFICIAL (decl
));
5501 else if (VAR_P (decl
))
5502 /* C++14 variable template. */;
5505 error ("template declaration of %q#D", decl
);
5506 return error_mark_node
;
5510 /* Check to see that the rules regarding the use of default
5511 arguments are not being violated. We check args for a friend
5512 functions when we know whether it's a definition, introducing
5513 declaration or re-declaration. */
5514 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5515 check_default_tmpl_args (decl
, current_template_parms
,
5516 is_primary
, is_partial
, is_friend
);
5518 /* Ensure that there are no parameter packs in the type of this
5519 declaration that have not been expanded. */
5520 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5522 /* Check each of the arguments individually to see if there are
5523 any bare parameter packs. */
5524 tree type
= TREE_TYPE (decl
);
5525 tree arg
= DECL_ARGUMENTS (decl
);
5526 tree argtype
= TYPE_ARG_TYPES (type
);
5528 while (arg
&& argtype
)
5530 if (!DECL_PACK_P (arg
)
5531 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5533 /* This is a PARM_DECL that contains unexpanded parameter
5534 packs. We have already complained about this in the
5535 check_for_bare_parameter_packs call, so just replace
5536 these types with ERROR_MARK_NODE. */
5537 TREE_TYPE (arg
) = error_mark_node
;
5538 TREE_VALUE (argtype
) = error_mark_node
;
5541 arg
= DECL_CHAIN (arg
);
5542 argtype
= TREE_CHAIN (argtype
);
5545 /* Check for bare parameter packs in the return type and the
5546 exception specifiers. */
5547 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5548 /* Errors were already issued, set return type to int
5549 as the frontend doesn't expect error_mark_node as
5551 TREE_TYPE (type
) = integer_type_node
;
5552 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5553 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5555 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5556 && TYPE_DECL_ALIAS_P (decl
))
5557 ? DECL_ORIGINAL_TYPE (decl
)
5558 : TREE_TYPE (decl
)))
5560 TREE_TYPE (decl
) = error_mark_node
;
5561 return error_mark_node
;
5565 return process_partial_specialization (decl
);
5567 args
= current_template_args ();
5570 || TREE_CODE (ctx
) == FUNCTION_DECL
5571 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5572 || (TREE_CODE (decl
) == TYPE_DECL
5573 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5574 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5576 if (DECL_LANG_SPECIFIC (decl
)
5577 && DECL_TEMPLATE_INFO (decl
)
5578 && DECL_TI_TEMPLATE (decl
))
5579 tmpl
= DECL_TI_TEMPLATE (decl
);
5580 /* If DECL is a TYPE_DECL for a class-template, then there won't
5581 be DECL_LANG_SPECIFIC. The information equivalent to
5582 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5583 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5584 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5585 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5587 /* Since a template declaration already existed for this
5588 class-type, we must be redeclaring it here. Make sure
5589 that the redeclaration is valid. */
5590 redeclare_class_template (TREE_TYPE (decl
),
5591 current_template_parms
,
5592 current_template_constraints ());
5593 /* We don't need to create a new TEMPLATE_DECL; just use the
5594 one we already had. */
5595 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5599 tmpl
= build_template_decl (decl
, current_template_parms
,
5603 if (DECL_LANG_SPECIFIC (decl
)
5604 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5606 /* A specialization of a member template of a template
5608 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5609 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5610 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5616 tree a
, t
, current
, parms
;
5618 tree tinfo
= get_template_info (decl
);
5622 error ("template definition of non-template %q#D", decl
);
5623 return error_mark_node
;
5626 tmpl
= TI_TEMPLATE (tinfo
);
5628 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5629 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5630 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5631 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5635 /* The declaration is a specialization of a member
5636 template, declared outside the class. Therefore, the
5637 innermost template arguments will be NULL, so we
5638 replace them with the arguments determined by the
5639 earlier call to check_explicit_specialization. */
5640 args
= DECL_TI_ARGS (decl
);
5643 = build_template_decl (decl
, current_template_parms
,
5645 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5646 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5647 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5648 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5649 DECL_TEMPLATE_INFO (new_tmpl
)
5650 = build_template_info (tmpl
, args
);
5652 register_specialization (new_tmpl
,
5653 most_general_template (tmpl
),
5659 /* Make sure the template headers we got make sense. */
5661 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5662 i
= TMPL_PARMS_DEPTH (parms
);
5663 if (TMPL_ARGS_DEPTH (args
) != i
)
5665 error ("expected %d levels of template parms for %q#D, got %d",
5666 i
, decl
, TMPL_ARGS_DEPTH (args
));
5667 DECL_INTERFACE_KNOWN (decl
) = 1;
5668 return error_mark_node
;
5671 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5673 a
= TMPL_ARGS_LEVEL (args
, i
);
5674 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5676 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5678 if (current
== decl
)
5679 error ("got %d template parameters for %q#D",
5680 TREE_VEC_LENGTH (a
), decl
);
5682 error ("got %d template parameters for %q#T",
5683 TREE_VEC_LENGTH (a
), current
);
5684 error (" but %d required", TREE_VEC_LENGTH (t
));
5685 /* Avoid crash in import_export_decl. */
5686 DECL_INTERFACE_KNOWN (decl
) = 1;
5687 return error_mark_node
;
5690 if (current
== decl
)
5692 else if (current
== NULL_TREE
)
5693 /* Can happen in erroneous input. */
5696 current
= get_containing_scope (current
);
5699 /* Check that the parms are used in the appropriate qualifying scopes
5700 in the declarator. */
5701 if (!comp_template_args
5703 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5705 error ("template arguments to %qD do not match original "
5706 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5707 if (!uses_template_parms (TI_ARGS (tinfo
)))
5708 inform (input_location
, "use %<template<>%> for"
5709 " an explicit specialization");
5710 /* Avoid crash in import_export_decl. */
5711 DECL_INTERFACE_KNOWN (decl
) = 1;
5712 return error_mark_node
;
5716 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5717 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5719 /* Push template declarations for global functions and types. Note
5720 that we do not try to push a global template friend declared in a
5721 template class; such a thing may well depend on the template
5722 parameters of the class. */
5723 if (new_template_p
&& !ctx
5724 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5726 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5727 if (tmpl
== error_mark_node
)
5728 return error_mark_node
;
5730 /* Hide template friend classes that haven't been declared yet. */
5731 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5733 DECL_ANTICIPATED (tmpl
) = 1;
5734 DECL_FRIEND_P (tmpl
) = 1;
5740 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5742 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5744 /* Give template template parms a DECL_CONTEXT of the template
5745 for which they are a parameter. */
5746 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5747 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5749 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5750 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5751 DECL_CONTEXT (parm
) = tmpl
;
5754 if (TREE_CODE (decl
) == TYPE_DECL
5755 && TYPE_DECL_ALIAS_P (decl
)
5756 && complex_alias_template_p (tmpl
))
5757 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5760 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5761 back to its most general template. If TMPL is a specialization,
5762 ARGS may only have the innermost set of arguments. Add the missing
5763 argument levels if necessary. */
5764 if (DECL_TEMPLATE_INFO (tmpl
))
5765 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5767 info
= build_template_info (tmpl
, args
);
5769 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5770 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5774 retrofit_lang_decl (decl
);
5775 if (DECL_LANG_SPECIFIC (decl
))
5776 DECL_TEMPLATE_INFO (decl
) = info
;
5779 if (flag_implicit_templates
5781 && TREE_PUBLIC (decl
)
5782 && VAR_OR_FUNCTION_DECL_P (decl
))
5783 /* Set DECL_COMDAT on template instantiations; if we force
5784 them to be emitted by explicit instantiation or -frepo,
5785 mark_needed will tell cgraph to do the right thing. */
5786 DECL_COMDAT (decl
) = true;
5788 return DECL_TEMPLATE_RESULT (tmpl
);
5792 push_template_decl (tree decl
)
5794 return push_template_decl_real (decl
, false);
5797 /* FN is an inheriting constructor that inherits from the constructor
5798 template INHERITED; turn FN into a constructor template with a matching
5802 add_inherited_template_parms (tree fn
, tree inherited
)
5805 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5806 inner_parms
= copy_node (inner_parms
);
5808 = tree_cons (size_int (processing_template_decl
+ 1),
5809 inner_parms
, current_template_parms
);
5810 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5811 tree args
= template_parms_to_args (parms
);
5812 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5813 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5814 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5815 DECL_ARTIFICIAL (tmpl
) = true;
5816 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5820 /* Called when a class template TYPE is redeclared with the indicated
5821 template PARMS, e.g.:
5823 template <class T> struct S;
5824 template <class T> struct S {}; */
5827 redeclare_class_template (tree type
, tree parms
, tree cons
)
5833 if (!TYPE_TEMPLATE_INFO (type
))
5835 error ("%qT is not a template type", type
);
5839 tmpl
= TYPE_TI_TEMPLATE (type
);
5840 if (!PRIMARY_TEMPLATE_P (tmpl
))
5841 /* The type is nested in some template class. Nothing to worry
5842 about here; there are no new template parameters for the nested
5848 error ("template specifiers not specified in declaration of %qD",
5853 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5854 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5856 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5858 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5859 "redeclared with %d template parameter",
5860 "redeclared with %d template parameters",
5861 TREE_VEC_LENGTH (parms
));
5862 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5863 "previous declaration %qD used %d template parameter",
5864 "previous declaration %qD used %d template parameters",
5865 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5869 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5876 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5877 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5880 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5881 if (error_operand_p (tmpl_parm
))
5884 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5885 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5886 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5888 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5890 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5891 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5892 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5893 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5894 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5895 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5896 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5897 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5898 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5900 error ("template parameter %q+#D", tmpl_parm
);
5901 error ("redeclared here as %q#D", parm
);
5905 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5907 /* We have in [temp.param]:
5909 A template-parameter may not be given default arguments
5910 by two different declarations in the same scope. */
5911 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5912 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5913 "original definition appeared here");
5917 if (parm_default
!= NULL_TREE
)
5918 /* Update the previous template parameters (which are the ones
5919 that will really count) with the new default value. */
5920 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5921 else if (tmpl_default
!= NULL_TREE
)
5922 /* Update the new parameters, too; they'll be used as the
5923 parameters for any members. */
5924 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5926 /* Give each template template parm in this redeclaration a
5927 DECL_CONTEXT of the template for which they are a parameter. */
5928 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5930 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5931 DECL_CONTEXT (parm
) = tmpl
;
5934 if (TREE_CODE (parm
) == TYPE_DECL
)
5935 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5938 // Cannot redeclare a class template with a different set of constraints.
5939 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5941 error_at (input_location
, "redeclaration %q#D with different "
5942 "constraints", tmpl
);
5943 inform (DECL_SOURCE_LOCATION (tmpl
),
5944 "original declaration appeared here");
5950 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5951 to be used when the caller has already checked
5952 (processing_template_decl
5953 && !instantiation_dependent_expression_p (expr)
5954 && potential_constant_expression (expr))
5955 and cleared processing_template_decl. */
5958 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5960 return tsubst_copy_and_build (expr
,
5963 /*in_decl=*/NULL_TREE
,
5964 /*function_p=*/false,
5965 /*integral_constant_expression_p=*/true);
5968 /* Simplify EXPR if it is a non-dependent expression. Returns the
5969 (possibly simplified) expression. */
5972 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5974 if (expr
== NULL_TREE
)
5977 /* If we're in a template, but EXPR isn't value dependent, simplify
5978 it. We're supposed to treat:
5980 template <typename T> void f(T[1 + 1]);
5981 template <typename T> void f(T[2]);
5983 as two declarations of the same function, for example. */
5984 if (processing_template_decl
5985 && is_nondependent_constant_expression (expr
))
5987 processing_template_decl_sentinel s
;
5988 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5994 instantiate_non_dependent_expr (tree expr
)
5996 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5999 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6000 an uninstantiated expression. */
6003 instantiate_non_dependent_or_null (tree expr
)
6005 if (expr
== NULL_TREE
)
6007 if (processing_template_decl
)
6009 if (!is_nondependent_constant_expression (expr
))
6013 processing_template_decl_sentinel s
;
6014 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6020 /* True iff T is a specialization of a variable template. */
6023 variable_template_specialization_p (tree t
)
6025 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6027 tree tmpl
= DECL_TI_TEMPLATE (t
);
6028 return variable_template_p (tmpl
);
6031 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6032 template declaration, or a TYPE_DECL for an alias declaration. */
6035 alias_type_or_template_p (tree t
)
6039 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6042 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6043 || DECL_ALIAS_TEMPLATE_P (t
));
6046 /* Return TRUE iff T is a specialization of an alias template. */
6049 alias_template_specialization_p (const_tree t
)
6051 /* It's an alias template specialization if it's an alias and its
6052 TYPE_NAME is a specialization of a primary template. */
6053 if (TYPE_ALIAS_P (t
))
6054 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6055 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6060 /* An alias template is complex from a SFINAE perspective if a template-id
6061 using that alias can be ill-formed when the expansion is not, as with
6062 the void_t template. We determine this by checking whether the
6063 expansion for the alias template uses all its template parameters. */
6065 struct uses_all_template_parms_data
6072 uses_all_template_parms_r (tree t
, void *data_
)
6074 struct uses_all_template_parms_data
&data
6075 = *(struct uses_all_template_parms_data
*)data_
;
6076 tree idx
= get_template_parm_index (t
);
6078 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6079 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6084 complex_alias_template_p (const_tree tmpl
)
6086 struct uses_all_template_parms_data data
;
6087 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6088 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6089 data
.level
= TMPL_PARMS_DEPTH (parms
);
6090 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6091 data
.seen
= XALLOCAVEC (bool, len
);
6092 for (int i
= 0; i
< len
; ++i
)
6093 data
.seen
[i
] = false;
6095 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6096 for (int i
= 0; i
< len
; ++i
)
6102 /* Return TRUE iff T is a specialization of a complex alias template with
6103 dependent template-arguments. */
6106 dependent_alias_template_spec_p (const_tree t
)
6108 if (!alias_template_specialization_p (t
))
6111 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6112 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6115 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6116 if (!any_dependent_template_arguments_p (args
))
6122 /* Return the number of innermost template parameters in TMPL. */
6125 num_innermost_template_parms (tree tmpl
)
6127 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6128 return TREE_VEC_LENGTH (parms
);
6131 /* Return either TMPL or another template that it is equivalent to under DR
6132 1286: An alias that just changes the name of a template is equivalent to
6133 the other template. */
6136 get_underlying_template (tree tmpl
)
6138 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6139 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6141 /* Determine if the alias is equivalent to an underlying template. */
6142 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6143 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6147 tree underlying
= TI_TEMPLATE (tinfo
);
6148 if (!PRIMARY_TEMPLATE_P (underlying
)
6149 || (num_innermost_template_parms (tmpl
)
6150 != num_innermost_template_parms (underlying
)))
6153 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6154 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6155 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6158 /* Alias is equivalent. Strip it and repeat. */
6165 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6166 must be a reference-to-function or a pointer-to-function type, as specified
6167 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6168 and check that the resulting function has external linkage. */
6171 convert_nontype_argument_function (tree type
, tree expr
,
6172 tsubst_flags_t complain
)
6176 linkage_kind linkage
;
6178 fn
= instantiate_type (type
, fns
, tf_none
);
6179 if (fn
== error_mark_node
)
6180 return error_mark_node
;
6182 if (value_dependent_expression_p (fn
))
6185 fn_no_ptr
= strip_fnptr_conv (fn
);
6186 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6187 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6188 if (BASELINK_P (fn_no_ptr
))
6189 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6191 /* [temp.arg.nontype]/1
6193 A template-argument for a non-type, non-template template-parameter
6196 -- the address of an object or function with external [C++11: or
6197 internal] linkage. */
6199 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6200 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6202 if (complain
& tf_error
)
6204 error ("%qE is not a valid template argument for type %qT",
6206 if (TYPE_PTR_P (type
))
6207 inform (input_location
, "it must be the address of a function "
6208 "with external linkage");
6210 inform (input_location
, "it must be the name of a function with "
6211 "external linkage");
6216 linkage
= decl_linkage (fn_no_ptr
);
6217 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6219 if (complain
& tf_error
)
6221 if (cxx_dialect
>= cxx11
)
6222 error ("%qE is not a valid template argument for type %qT "
6223 "because %qD has no linkage",
6224 expr
, type
, fn_no_ptr
);
6226 error ("%qE is not a valid template argument for type %qT "
6227 "because %qD does not have external linkage",
6228 expr
, type
, fn_no_ptr
);
6234 if (TYPE_REF_P (type
))
6236 if (REFERENCE_REF_P (fn
))
6237 fn
= TREE_OPERAND (fn
, 0);
6239 fn
= build_address (fn
);
6241 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6242 fn
= build_nop (type
, fn
);
6247 /* Subroutine of convert_nontype_argument.
6248 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6249 Emit an error otherwise. */
6252 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6253 tsubst_flags_t complain
)
6255 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6256 tree orig_expr
= expr
;
6258 if (null_ptr_cst_p (expr
))
6260 if (TREE_CODE (expr
) == PTRMEM_CST
6261 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6262 PTRMEM_CST_CLASS (expr
)))
6264 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6266 if (processing_template_decl
6267 && TREE_CODE (expr
) == ADDR_EXPR
6268 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6270 if (complain
& tf_error
)
6272 error_at (loc
, "%qE is not a valid template argument for type %qT",
6274 if (TREE_CODE (expr
) != PTRMEM_CST
)
6275 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6277 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6282 /* Returns TRUE iff the address of OP is value-dependent.
6284 14.6.2.4 [temp.dep.temp]:
6285 A non-integral non-type template-argument is dependent if its type is
6286 dependent or it has either of the following forms
6289 and contains a nested-name-specifier which specifies a class-name that
6290 names a dependent type.
6292 We generalize this to just say that the address of a member of a
6293 dependent class is value-dependent; the above doesn't cover the
6294 address of a static data member named with an unqualified-id. */
6297 has_value_dependent_address (tree op
)
6299 /* We could use get_inner_reference here, but there's no need;
6300 this is only relevant for template non-type arguments, which
6301 can only be expressed as &id-expression. */
6304 tree ctx
= CP_DECL_CONTEXT (op
);
6305 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6312 /* The next set of functions are used for providing helpful explanatory
6313 diagnostics for failed overload resolution. Their messages should be
6314 indented by two spaces for consistency with the messages in
6318 unify_success (bool /*explain_p*/)
6323 /* Other failure functions should call this one, to provide a single function
6324 for setting a breakpoint on. */
6327 unify_invalid (bool /*explain_p*/)
6333 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6336 inform (input_location
,
6337 " couldn't deduce template parameter %qD", parm
);
6338 return unify_invalid (explain_p
);
6342 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6345 inform (input_location
,
6346 " types %qT and %qT have incompatible cv-qualifiers",
6348 return unify_invalid (explain_p
);
6352 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6355 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6356 return unify_invalid (explain_p
);
6360 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6363 inform (input_location
,
6364 " template parameter %qD is not a parameter pack, but "
6367 return unify_invalid (explain_p
);
6371 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6374 inform (input_location
,
6375 " template argument %qE does not match "
6376 "pointer-to-member constant %qE",
6378 return unify_invalid (explain_p
);
6382 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6385 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6386 return unify_invalid (explain_p
);
6390 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6393 inform (input_location
,
6394 " inconsistent parameter pack deduction with %qT and %qT",
6396 return unify_invalid (explain_p
);
6400 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6405 inform (input_location
,
6406 " deduced conflicting types for parameter %qT (%qT and %qT)",
6407 parm
, first
, second
);
6409 inform (input_location
,
6410 " deduced conflicting values for non-type parameter "
6411 "%qE (%qE and %qE)", parm
, first
, second
);
6413 return unify_invalid (explain_p
);
6417 unify_vla_arg (bool explain_p
, tree arg
)
6420 inform (input_location
,
6421 " variable-sized array type %qT is not "
6422 "a valid template argument",
6424 return unify_invalid (explain_p
);
6428 unify_method_type_error (bool explain_p
, tree arg
)
6431 inform (input_location
,
6432 " member function type %qT is not a valid template argument",
6434 return unify_invalid (explain_p
);
6438 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6443 inform_n (input_location
, wanted
,
6444 " candidate expects at least %d argument, %d provided",
6445 " candidate expects at least %d arguments, %d provided",
6448 inform_n (input_location
, wanted
,
6449 " candidate expects %d argument, %d provided",
6450 " candidate expects %d arguments, %d provided",
6453 return unify_invalid (explain_p
);
6457 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6459 return unify_arity (explain_p
, have
, wanted
);
6463 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6464 bool least_p
= false)
6466 return unify_arity (explain_p
, have
, wanted
, least_p
);
6470 unify_arg_conversion (bool explain_p
, tree to_type
,
6471 tree from_type
, tree arg
)
6474 inform (cp_expr_loc_or_loc (arg
, input_location
),
6475 " cannot convert %qE (type %qT) to type %qT",
6476 arg
, from_type
, to_type
);
6477 return unify_invalid (explain_p
);
6481 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6482 tree parm
, tree arg
)
6487 case tbr_ambiguous_baseclass
:
6488 inform (input_location
, " %qT is an ambiguous base class of %qT",
6492 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6495 return unify_invalid (explain_p
);
6499 unify_inconsistent_template_template_parameters (bool explain_p
)
6502 inform (input_location
,
6503 " template parameters of a template template argument are "
6504 "inconsistent with other deduced template arguments");
6505 return unify_invalid (explain_p
);
6509 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6512 inform (input_location
,
6513 " can't deduce a template for %qT from non-template type %qT",
6515 return unify_invalid (explain_p
);
6519 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6522 inform (input_location
,
6523 " template argument %qE does not match %qE", arg
, parm
);
6524 return unify_invalid (explain_p
);
6527 /* True if T is a C++20 template parameter object to store the argument for a
6528 template parameter of class type. */
6531 template_parm_object_p (const_tree t
)
6533 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6534 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6537 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6538 argument for TYPE, points to an unsuitable object. */
6541 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6543 switch (TREE_CODE (expr
))
6546 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6550 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6555 unsigned i
; tree elt
;
6556 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6557 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6564 tree decl
= TREE_OPERAND (expr
, 0);
6568 if (complain
& tf_error
)
6569 error ("%qE is not a valid template argument of type %qT "
6570 "because %qE is not a variable", expr
, type
, decl
);
6573 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6575 if (complain
& tf_error
)
6576 error ("%qE is not a valid template argument of type %qT "
6577 "in C++98 because %qD does not have external linkage",
6581 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6582 && decl_linkage (decl
) == lk_none
)
6584 if (complain
& tf_error
)
6585 error ("%qE is not a valid template argument of type %qT "
6586 "because %qD has no linkage", expr
, type
, decl
);
6589 /* C++17: For a non-type template-parameter of reference or pointer
6590 type, the value of the constant expression shall not refer to (or
6591 for a pointer type, shall not be the address of):
6592 * a subobject (4.5),
6593 * a temporary object (15.2),
6594 * a string literal (5.13.5),
6595 * the result of a typeid expression (8.2.8), or
6596 * a predefined __func__ variable (11.4.1). */
6597 else if (DECL_ARTIFICIAL (decl
))
6599 if (complain
& tf_error
)
6600 error ("the address of %qD is not a valid template argument",
6604 else if (!same_type_ignoring_top_level_qualifiers_p
6605 (strip_array_types (TREE_TYPE (type
)),
6606 strip_array_types (TREE_TYPE (decl
))))
6608 if (complain
& tf_error
)
6609 error ("the address of the %qT subobject of %qD is not a "
6610 "valid template argument", TREE_TYPE (type
), decl
);
6613 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6615 if (complain
& tf_error
)
6616 error ("the address of %qD is not a valid template argument "
6617 "because it does not have static storage duration",
6625 if (!INDIRECT_TYPE_P (type
))
6626 /* We're only concerned about pointers and references here. */;
6627 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6628 /* Null pointer values are OK in C++11. */;
6633 if (complain
& tf_error
)
6634 error ("%qD is not a valid template argument "
6635 "because %qD is a variable, not the address of "
6636 "a variable", expr
, expr
);
6641 if (complain
& tf_error
)
6642 error ("%qE is not a valid template argument for %qT "
6643 "because it is not the address of a variable",
6653 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6654 template argument EXPR. */
6657 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6659 if (TREE_CODE (expr
) == TARGET_EXPR
)
6660 expr
= TARGET_EXPR_INITIAL (expr
);
6662 if (!TREE_CONSTANT (expr
))
6664 if ((complain
& tf_error
)
6665 && require_rvalue_constant_expression (expr
))
6666 cxx_constant_value (expr
);
6667 return error_mark_node
;
6669 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6670 return error_mark_node
;
6672 tree name
= mangle_template_parm_object (expr
);
6673 tree decl
= get_global_binding (name
);
6677 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6678 decl
= create_temporary_var (type
);
6679 TREE_STATIC (decl
) = true;
6680 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6681 TREE_READONLY (decl
) = true;
6682 DECL_NAME (decl
) = name
;
6683 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6684 DECL_CONTEXT (decl
) = global_namespace
;
6685 comdat_linkage (decl
);
6686 pushdecl_top_level_and_finish (decl
, expr
);
6690 /* Attempt to convert the non-type template parameter EXPR to the
6691 indicated TYPE. If the conversion is successful, return the
6692 converted value. If the conversion is unsuccessful, return
6693 NULL_TREE if we issued an error message, or error_mark_node if we
6694 did not. We issue error messages for out-and-out bad template
6695 parameters, but not simply because the conversion failed, since we
6696 might be just trying to do argument deduction. Both TYPE and EXPR
6697 must be non-dependent.
6699 The conversion follows the special rules described in
6700 [temp.arg.nontype], and it is much more strict than an implicit
6703 This function is called twice for each template argument (see
6704 lookup_template_class for a more accurate description of this
6705 problem). This means that we need to handle expressions which
6706 are not valid in a C++ source, but can be created from the
6707 first call (for instance, casts to perform conversions). These
6708 hacks can go away after we fix the double coercion problem. */
6711 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6714 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6716 /* Detect immediately string literals as invalid non-type argument.
6717 This special-case is not needed for correctness (we would easily
6718 catch this later), but only to provide better diagnostic for this
6719 common user mistake. As suggested by DR 100, we do not mention
6720 linkage issues in the diagnostic as this is not the point. */
6721 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6723 if (complain
& tf_error
)
6724 error ("%qE is not a valid template argument for type %qT "
6725 "because string literals can never be used in this context",
6730 /* Add the ADDR_EXPR now for the benefit of
6731 value_dependent_expression_p. */
6732 if (TYPE_PTROBV_P (type
)
6733 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6735 expr
= decay_conversion (expr
, complain
);
6736 if (expr
== error_mark_node
)
6737 return error_mark_node
;
6740 /* If we are in a template, EXPR may be non-dependent, but still
6741 have a syntactic, rather than semantic, form. For example, EXPR
6742 might be a SCOPE_REF, rather than the VAR_DECL to which the
6743 SCOPE_REF refers. Preserving the qualifying scope is necessary
6744 so that access checking can be performed when the template is
6745 instantiated -- but here we need the resolved form so that we can
6746 convert the argument. */
6747 bool non_dep
= false;
6748 if (TYPE_REF_OBJ_P (type
)
6749 && has_value_dependent_address (expr
))
6750 /* If we want the address and it's value-dependent, don't fold. */;
6751 else if (processing_template_decl
6752 && is_nondependent_constant_expression (expr
))
6754 if (error_operand_p (expr
))
6755 return error_mark_node
;
6756 expr_type
= TREE_TYPE (expr
);
6758 /* If the argument is non-dependent, perform any conversions in
6759 non-dependent context as well. */
6760 processing_template_decl_sentinel
s (non_dep
);
6762 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6764 if (value_dependent_expression_p (expr
))
6765 expr
= canonicalize_expr_argument (expr
, complain
);
6767 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6768 to a non-type argument of "nullptr". */
6769 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6770 expr
= fold_simple (convert (type
, expr
));
6772 /* In C++11, integral or enumeration non-type template arguments can be
6773 arbitrary constant expressions. Pointer and pointer to
6774 member arguments can be general constant expressions that evaluate
6775 to a null value, but otherwise still need to be of a specific form. */
6776 if (cxx_dialect
>= cxx11
)
6778 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
6779 /* A PTRMEM_CST is already constant, and a valid template
6780 argument for a parameter of pointer to member type, we just want
6781 to leave it in that form rather than lower it to a
6783 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6784 || cxx_dialect
>= cxx17
)
6786 /* C++17: A template-argument for a non-type template-parameter shall
6787 be a converted constant expression (8.20) of the type of the
6788 template-parameter. */
6789 expr
= build_converted_constant_expr (type
, expr
, complain
);
6790 if (expr
== error_mark_node
)
6791 /* Make sure we return NULL_TREE only if we have really issued
6792 an error, as described above. */
6793 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
6794 expr
= maybe_constant_value (expr
);
6795 expr
= convert_from_reference (expr
);
6797 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6799 tree folded
= maybe_constant_value (expr
);
6800 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6801 : null_member_pointer_value_p (folded
))
6806 if (TYPE_REF_P (type
))
6807 expr
= mark_lvalue_use (expr
);
6809 expr
= mark_rvalue_use (expr
);
6811 /* HACK: Due to double coercion, we can get a
6812 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6813 which is the tree that we built on the first call (see
6814 below when coercing to reference to object or to reference to
6815 function). We just strip everything and get to the arg.
6816 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6818 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6820 tree probe_type
, probe
= expr
;
6821 if (REFERENCE_REF_P (probe
))
6822 probe
= TREE_OPERAND (probe
, 0);
6823 probe_type
= TREE_TYPE (probe
);
6824 if (TREE_CODE (probe
) == NOP_EXPR
)
6826 /* ??? Maybe we could use convert_from_reference here, but we
6827 would need to relax its constraints because the NOP_EXPR
6828 could actually change the type to something more cv-qualified,
6829 and this is not folded by convert_from_reference. */
6830 tree addr
= TREE_OPERAND (probe
, 0);
6831 if (TYPE_REF_P (probe_type
)
6832 && TREE_CODE (addr
) == ADDR_EXPR
6833 && TYPE_PTR_P (TREE_TYPE (addr
))
6834 && (same_type_ignoring_top_level_qualifiers_p
6835 (TREE_TYPE (probe_type
),
6836 TREE_TYPE (TREE_TYPE (addr
)))))
6838 expr
= TREE_OPERAND (addr
, 0);
6839 expr_type
= TREE_TYPE (probe_type
);
6844 /* [temp.arg.nontype]/5, bullet 1
6846 For a non-type template-parameter of integral or enumeration type,
6847 integral promotions (_conv.prom_) and integral conversions
6848 (_conv.integral_) are applied. */
6849 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6851 if (cxx_dialect
< cxx11
)
6853 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6854 t
= maybe_constant_value (t
);
6855 if (t
!= error_mark_node
)
6859 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6860 return error_mark_node
;
6862 /* Notice that there are constant expressions like '4 % 0' which
6863 do not fold into integer constants. */
6864 if (TREE_CODE (expr
) != INTEGER_CST
6865 && !value_dependent_expression_p (expr
))
6867 if (complain
& tf_error
)
6869 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6870 if (!require_potential_constant_expression (expr
))
6871 expr
= error_mark_node
;
6873 expr
= cxx_constant_value (expr
);
6874 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6875 inform (loc
, "in template argument for type %qT", type
);
6876 if (expr
== error_mark_node
)
6878 /* else cxx_constant_value complained but gave us
6879 a real constant, so go ahead. */
6880 if (TREE_CODE (expr
) != INTEGER_CST
)
6882 /* Some assemble time constant expressions like
6883 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6884 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6885 as we can emit them into .rodata initializers of
6886 variables, yet they can't fold into an INTEGER_CST at
6887 compile time. Refuse them here. */
6888 gcc_checking_assert (reduced_constant_expression_p (expr
));
6889 error_at (loc
, "template argument %qE for type %qT not "
6890 "a constant integer", expr
, type
);
6898 /* Avoid typedef problems. */
6899 if (TREE_TYPE (expr
) != type
)
6900 expr
= fold_convert (type
, expr
);
6902 /* [temp.arg.nontype]/5, bullet 2
6904 For a non-type template-parameter of type pointer to object,
6905 qualification conversions (_conv.qual_) and the array-to-pointer
6906 conversion (_conv.array_) are applied. */
6907 else if (TYPE_PTROBV_P (type
))
6909 tree decayed
= expr
;
6911 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6912 decay_conversion or an explicit cast. If it's a problematic cast,
6913 we'll complain about it below. */
6914 if (TREE_CODE (expr
) == NOP_EXPR
)
6918 if (TREE_CODE (probe
) == ADDR_EXPR
6919 && TYPE_PTR_P (TREE_TYPE (probe
)))
6922 expr_type
= TREE_TYPE (expr
);
6926 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6928 A template-argument for a non-type, non-template template-parameter
6929 shall be one of: [...]
6931 -- the name of a non-type template-parameter;
6932 -- the address of an object or function with external linkage, [...]
6933 expressed as "& id-expression" where the & is optional if the name
6934 refers to a function or array, or if the corresponding
6935 template-parameter is a reference.
6937 Here, we do not care about functions, as they are invalid anyway
6938 for a parameter of type pointer-to-object. */
6940 if (value_dependent_expression_p (expr
))
6941 /* Non-type template parameters are OK. */
6943 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6944 /* Null pointer values are OK in C++11. */;
6945 else if (TREE_CODE (expr
) != ADDR_EXPR
6946 && !INDIRECT_TYPE_P (expr_type
))
6947 /* Other values, like integer constants, might be valid
6948 non-type arguments of some other type. */
6949 return error_mark_node
;
6950 else if (invalid_tparm_referent_p (type
, expr
, complain
))
6955 expr
= perform_qualification_conversions (type
, expr
);
6956 if (expr
== error_mark_node
)
6957 return error_mark_node
;
6959 /* [temp.arg.nontype]/5, bullet 3
6961 For a non-type template-parameter of type reference to object, no
6962 conversions apply. The type referred to by the reference may be more
6963 cv-qualified than the (otherwise identical) type of the
6964 template-argument. The template-parameter is bound directly to the
6965 template-argument, which must be an lvalue. */
6966 else if (TYPE_REF_OBJ_P (type
))
6968 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6970 return error_mark_node
;
6972 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6974 if (complain
& tf_error
)
6975 error ("%qE is not a valid template argument for type %qT "
6976 "because of conflicts in cv-qualification", expr
, type
);
6980 if (!lvalue_p (expr
))
6982 if (complain
& tf_error
)
6983 error ("%qE is not a valid template argument for type %qT "
6984 "because it is not an lvalue", expr
, type
);
6988 /* [temp.arg.nontype]/1
6990 A template-argument for a non-type, non-template template-parameter
6991 shall be one of: [...]
6993 -- the address of an object or function with external linkage. */
6994 if (INDIRECT_REF_P (expr
)
6995 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6997 expr
= TREE_OPERAND (expr
, 0);
7000 if (complain
& tf_error
)
7001 error ("%q#D is not a valid template argument for type %qT "
7002 "because a reference variable does not have a constant "
7003 "address", expr
, type
);
7008 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7009 && value_dependent_expression_p (expr
))
7010 /* OK, dependent reference. We don't want to ask whether a DECL is
7011 itself value-dependent, since what we want here is its address. */;
7014 expr
= build_address (expr
);
7016 if (invalid_tparm_referent_p (type
, expr
, complain
))
7020 if (!same_type_p (type
, TREE_TYPE (expr
)))
7021 expr
= build_nop (type
, expr
);
7023 /* [temp.arg.nontype]/5, bullet 4
7025 For a non-type template-parameter of type pointer to function, only
7026 the function-to-pointer conversion (_conv.func_) is applied. If the
7027 template-argument represents a set of overloaded functions (or a
7028 pointer to such), the matching function is selected from the set
7030 else if (TYPE_PTRFN_P (type
))
7032 /* If the argument is a template-id, we might not have enough
7033 context information to decay the pointer. */
7034 if (!type_unknown_p (expr_type
))
7036 expr
= decay_conversion (expr
, complain
);
7037 if (expr
== error_mark_node
)
7038 return error_mark_node
;
7041 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7042 /* Null pointer values are OK in C++11. */
7043 return perform_qualification_conversions (type
, expr
);
7045 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7046 if (!expr
|| expr
== error_mark_node
)
7049 /* [temp.arg.nontype]/5, bullet 5
7051 For a non-type template-parameter of type reference to function, no
7052 conversions apply. If the template-argument represents a set of
7053 overloaded functions, the matching function is selected from the set
7055 else if (TYPE_REFFN_P (type
))
7057 if (TREE_CODE (expr
) == ADDR_EXPR
)
7059 if (complain
& tf_error
)
7061 error ("%qE is not a valid template argument for type %qT "
7062 "because it is a pointer", expr
, type
);
7063 inform (input_location
, "try using %qE instead",
7064 TREE_OPERAND (expr
, 0));
7069 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7070 if (!expr
|| expr
== error_mark_node
)
7073 /* [temp.arg.nontype]/5, bullet 6
7075 For a non-type template-parameter of type pointer to member function,
7076 no conversions apply. If the template-argument represents a set of
7077 overloaded member functions, the matching member function is selected
7078 from the set (_over.over_). */
7079 else if (TYPE_PTRMEMFUNC_P (type
))
7081 expr
= instantiate_type (type
, expr
, tf_none
);
7082 if (expr
== error_mark_node
)
7083 return error_mark_node
;
7085 /* [temp.arg.nontype] bullet 1 says the pointer to member
7086 expression must be a pointer-to-member constant. */
7087 if (!value_dependent_expression_p (expr
)
7088 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7091 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7092 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7093 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7094 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7096 /* [temp.arg.nontype]/5, bullet 7
7098 For a non-type template-parameter of type pointer to data member,
7099 qualification conversions (_conv.qual_) are applied. */
7100 else if (TYPE_PTRDATAMEM_P (type
))
7102 /* [temp.arg.nontype] bullet 1 says the pointer to member
7103 expression must be a pointer-to-member constant. */
7104 if (!value_dependent_expression_p (expr
)
7105 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7108 expr
= perform_qualification_conversions (type
, expr
);
7109 if (expr
== error_mark_node
)
7112 else if (NULLPTR_TYPE_P (type
))
7114 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7116 if (complain
& tf_error
)
7117 error ("%qE is not a valid template argument for type %qT "
7118 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7123 else if (CLASS_TYPE_P (type
))
7125 /* Replace the argument with a reference to the corresponding template
7126 parameter object. */
7127 if (!value_dependent_expression_p (expr
))
7128 expr
= get_template_parm_object (expr
, complain
);
7129 if (expr
== error_mark_node
)
7132 /* A template non-type parameter must be one of the above. */
7136 /* Sanity check: did we actually convert the argument to the
7138 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7139 (type
, TREE_TYPE (expr
)));
7140 return convert_from_reference (expr
);
7143 /* Subroutine of coerce_template_template_parms, which returns 1 if
7144 PARM_PARM and ARG_PARM match using the rule for the template
7145 parameters of template template parameters. Both PARM and ARG are
7146 template parameters; the rest of the arguments are the same as for
7147 coerce_template_template_parms.
7150 coerce_template_template_parm (tree parm
,
7152 tsubst_flags_t complain
,
7156 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7157 || parm
== NULL_TREE
|| error_operand_p (parm
))
7160 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7163 switch (TREE_CODE (parm
))
7166 /* We encounter instantiations of templates like
7167 template <template <template <class> class> class TT>
7170 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7171 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7173 if (!coerce_template_template_parms
7174 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7180 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7181 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7182 /* Argument is a parameter pack but parameter is not. */
7187 /* The tsubst call is used to handle cases such as
7189 template <int> class C {};
7190 template <class T, template <T> class TT> class D {};
7193 i.e. the parameter list of TT depends on earlier parameters. */
7194 if (!uses_template_parms (TREE_TYPE (arg
)))
7196 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7197 if (!uses_template_parms (t
)
7198 && !same_type_p (t
, TREE_TYPE (arg
)))
7202 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7203 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7204 /* Argument is a parameter pack but parameter is not. */
7216 /* Coerce template argument list ARGLIST for use with template
7217 template-parameter TEMPL. */
7220 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7221 tsubst_flags_t complain
)
7223 /* Consider an example where a template template parameter declared as
7225 template <class T, class U = std::allocator<T> > class TT
7227 The template parameter level of T and U are one level larger than
7228 of TT. To proper process the default argument of U, say when an
7229 instantiation `TT<int>' is seen, we need to build the full
7230 arguments containing {int} as the innermost level. Outer levels,
7231 available when not appearing as default template argument, can be
7232 obtained from the arguments of the enclosing template.
7234 Suppose that TT is later substituted with std::vector. The above
7235 instantiation is `TT<int, std::allocator<T> >' with TT at
7236 level 1, and T at level 2, while the template arguments at level 1
7237 becomes {std::vector} and the inner level 2 is {int}. */
7239 tree outer
= DECL_CONTEXT (templ
);
7242 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7243 /* We want arguments for the partial specialization, not arguments for
7244 the primary template. */
7245 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7247 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7249 else if (current_template_parms
)
7251 /* This is an argument of the current template, so we haven't set
7252 DECL_CONTEXT yet. */
7253 tree relevant_template_parms
;
7255 /* Parameter levels that are greater than the level of the given
7256 template template parm are irrelevant. */
7257 relevant_template_parms
= current_template_parms
;
7258 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7259 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7260 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7262 outer
= template_parms_to_args (relevant_template_parms
);
7266 arglist
= add_to_template_args (outer
, arglist
);
7268 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7269 return coerce_template_parms (parmlist
, arglist
, templ
,
7271 /*require_all_args=*/true,
7272 /*use_default_args=*/true);
7275 /* A cache of template template parameters with match-all default
7277 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7279 store_defaulted_ttp (tree v
, tree t
)
7281 if (!defaulted_ttp_cache
)
7282 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7283 defaulted_ttp_cache
->put (v
, t
);
7286 lookup_defaulted_ttp (tree v
)
7288 if (defaulted_ttp_cache
)
7289 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7294 /* T is a bound template template-parameter. Copy its arguments into default
7295 arguments of the template template-parameter's template parameters. */
7298 add_defaults_to_ttp (tree otmpl
)
7300 if (tree c
= lookup_defaulted_ttp (otmpl
))
7303 tree ntmpl
= copy_node (otmpl
);
7305 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7306 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7307 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7308 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7309 TYPE_NAME (ntype
) = ntmpl
;
7310 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7312 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7313 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7314 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7315 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7317 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7318 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7319 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7320 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7321 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7323 tree o
= TREE_VEC_ELT (vec
, i
);
7324 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7326 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7327 TREE_PURPOSE (n
) = any_targ_node
;
7331 store_defaulted_ttp (otmpl
, ntmpl
);
7335 /* ARG is a bound potential template template-argument, and PARGS is a list
7336 of arguments for the corresponding template template-parameter. Adjust
7337 PARGS as appropriate for application to ARG's template, and if ARG is a
7338 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7339 arguments to the template template parameter. */
7342 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7344 ++processing_template_decl
;
7345 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7346 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7348 /* When comparing two template template-parameters in partial ordering,
7349 rewrite the one currently being used as an argument to have default
7350 arguments for all parameters. */
7351 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7352 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7353 if (pargs
!= error_mark_node
)
7354 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7355 TYPE_TI_ARGS (arg
));
7360 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7361 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7362 /*require_all*/true,
7363 /*use_default*/true);
7365 --processing_template_decl
;
7369 /* Subroutine of unify for the case when PARM is a
7370 BOUND_TEMPLATE_TEMPLATE_PARM. */
7373 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7376 tree parmvec
= TYPE_TI_ARGS (parm
);
7377 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7379 /* The template template parm might be variadic and the argument
7380 not, so flatten both argument lists. */
7381 parmvec
= expand_template_argument_pack (parmvec
);
7382 argvec
= expand_template_argument_pack (argvec
);
7386 /* In keeping with P0522R0, adjust P's template arguments
7387 to apply to A's template; then flatten it again. */
7388 tree nparmvec
= parmvec
;
7389 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7390 nparmvec
= expand_template_argument_pack (nparmvec
);
7392 if (unify (tparms
, targs
, nparmvec
, argvec
,
7393 UNIFY_ALLOW_NONE
, explain_p
))
7396 /* If the P0522 adjustment eliminated a pack expansion, deduce
7399 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7400 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7401 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7406 /* Deduce arguments T, i from TT<T> or TT<i>.
7407 We check each element of PARMVEC and ARGVEC individually
7408 rather than the whole TREE_VEC since they can have
7409 different number of elements, which is allowed under N2555. */
7411 int len
= TREE_VEC_LENGTH (parmvec
);
7413 /* Check if the parameters end in a pack, making them
7415 int parm_variadic_p
= 0;
7417 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7418 parm_variadic_p
= 1;
7420 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7421 /* If the template argument list of P contains a pack
7422 expansion that is not the last template argument, the
7423 entire template argument list is a non-deduced
7425 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7426 return unify_success (explain_p
);
7428 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7429 return unify_too_few_arguments (explain_p
,
7430 TREE_VEC_LENGTH (argvec
), len
);
7432 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7433 if (unify (tparms
, targs
,
7434 TREE_VEC_ELT (parmvec
, i
),
7435 TREE_VEC_ELT (argvec
, i
),
7436 UNIFY_ALLOW_NONE
, explain_p
))
7440 && unify_pack_expansion (tparms
, targs
,
7443 /*subr=*/true, explain_p
))
7450 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7451 template template parameters. Both PARM_PARMS and ARG_PARMS are
7452 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7455 Consider the example:
7456 template <class T> class A;
7457 template<template <class U> class TT> class B;
7459 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7460 the parameters to A, and OUTER_ARGS contains A. */
7463 coerce_template_template_parms (tree parm_parms
,
7465 tsubst_flags_t complain
,
7469 int nparms
, nargs
, i
;
7473 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7474 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7476 nparms
= TREE_VEC_LENGTH (parm_parms
);
7477 nargs
= TREE_VEC_LENGTH (arg_parms
);
7481 /* P0522R0: A template template-parameter P is at least as specialized as
7482 a template template-argument A if, given the following rewrite to two
7483 function templates, the function template corresponding to P is at
7484 least as specialized as the function template corresponding to A
7485 according to the partial ordering rules for function templates
7486 ([temp.func.order]). Given an invented class template X with the
7487 template parameter list of A (including default arguments):
7489 * Each of the two function templates has the same template parameters,
7490 respectively, as P or A.
7492 * Each function template has a single function parameter whose type is
7493 a specialization of X with template arguments corresponding to the
7494 template parameters from the respective function template where, for
7495 each template parameter PP in the template parameter list of the
7496 function template, a corresponding template argument AA is formed. If
7497 PP declares a parameter pack, then AA is the pack expansion
7498 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7500 If the rewrite produces an invalid type, then P is not at least as
7501 specialized as A. */
7503 /* So coerce P's args to apply to A's parms, and then deduce between A's
7504 args and the converted args. If that succeeds, A is at least as
7505 specialized as P, so they match.*/
7506 tree pargs
= template_parms_level_to_args (parm_parms
);
7507 ++processing_template_decl
;
7508 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7509 /*require_all*/true, /*use_default*/true);
7510 --processing_template_decl
;
7511 if (pargs
!= error_mark_node
)
7513 tree targs
= make_tree_vec (nargs
);
7514 tree aargs
= template_parms_level_to_args (arg_parms
);
7515 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7521 /* Determine whether we have a parameter pack at the end of the
7522 template template parameter's template parameter list. */
7523 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7525 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7527 if (error_operand_p (parm
))
7530 switch (TREE_CODE (parm
))
7534 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7539 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7549 && !(variadic_p
&& nargs
>= nparms
- 1))
7552 /* Check all of the template parameters except the parameter pack at
7553 the end (if any). */
7554 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7556 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7557 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7560 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7561 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7563 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7571 /* Check each of the template parameters in the template
7572 argument against the template parameter pack at the end of
7573 the template template parameter. */
7574 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7577 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7579 for (; i
< nargs
; ++i
)
7581 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7584 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7586 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7595 /* Verifies that the deduced template arguments (in TARGS) for the
7596 template template parameters (in TPARMS) represent valid bindings,
7597 by comparing the template parameter list of each template argument
7598 to the template parameter list of its corresponding template
7599 template parameter, in accordance with DR150. This
7600 routine can only be called after all template arguments have been
7601 deduced. It will return TRUE if all of the template template
7602 parameter bindings are okay, FALSE otherwise. */
7604 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7606 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7609 /* We're dealing with template parms in this process. */
7610 ++processing_template_decl
;
7612 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7614 for (i
= 0; i
< ntparms
; ++i
)
7616 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7617 tree targ
= TREE_VEC_ELT (targs
, i
);
7619 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7621 tree packed_args
= NULL_TREE
;
7624 if (ARGUMENT_PACK_P (targ
))
7626 /* Look inside the argument pack. */
7627 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7628 len
= TREE_VEC_LENGTH (packed_args
);
7631 for (idx
= 0; idx
< len
; ++idx
)
7633 tree targ_parms
= NULL_TREE
;
7636 /* Extract the next argument from the argument
7638 targ
= TREE_VEC_ELT (packed_args
, idx
);
7640 if (PACK_EXPANSION_P (targ
))
7641 /* Look at the pattern of the pack expansion. */
7642 targ
= PACK_EXPANSION_PATTERN (targ
);
7644 /* Extract the template parameters from the template
7646 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7647 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7648 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7649 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7651 /* Verify that we can coerce the template template
7652 parameters from the template argument to the template
7653 parameter. This requires an exact match. */
7655 && !coerce_template_template_parms
7656 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7671 --processing_template_decl
;
7675 /* Since type attributes aren't mangled, we need to strip them from
7676 template type arguments. */
7679 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7681 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7683 bool removed_attributes
= false;
7684 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7685 if (removed_attributes
7686 && (complain
& tf_warning
))
7687 warning (OPT_Wignored_attributes
,
7688 "ignoring attributes on template argument %qT", arg
);
7692 /* And from inside dependent non-type arguments like sizeof(Type). */
7695 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7697 if (!arg
|| arg
== error_mark_node
)
7699 bool removed_attributes
= false;
7700 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7701 if (removed_attributes
7702 && (complain
& tf_warning
))
7703 warning (OPT_Wignored_attributes
,
7704 "ignoring attributes in template argument %qE", arg
);
7708 // A template declaration can be substituted for a constrained
7709 // template template parameter only when the argument is more
7710 // constrained than the parameter.
7712 is_compatible_template_arg (tree parm
, tree arg
)
7714 tree parm_cons
= get_constraints (parm
);
7716 /* For now, allow constrained template template arguments
7717 and unconstrained template template parameters. */
7718 if (parm_cons
== NULL_TREE
)
7721 tree arg_cons
= get_constraints (arg
);
7723 // If the template parameter is constrained, we need to rewrite its
7724 // constraints in terms of the ARG's template parameters. This ensures
7725 // that all of the template parameter types will have the same depth.
7727 // Note that this is only valid when coerce_template_template_parm is
7728 // true for the innermost template parameters of PARM and ARG. In other
7729 // words, because coercion is successful, this conversion will be valid.
7732 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7733 parm_cons
= tsubst_constraint_info (parm_cons
,
7734 INNERMOST_TEMPLATE_ARGS (args
),
7735 tf_none
, NULL_TREE
);
7736 if (parm_cons
== error_mark_node
)
7740 return subsumes (parm_cons
, arg_cons
);
7743 // Convert a placeholder argument into a binding to the original
7744 // parameter. The original parameter is saved as the TREE_TYPE of
7747 convert_wildcard_argument (tree parm
, tree arg
)
7749 TREE_TYPE (arg
) = parm
;
7753 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7754 because one of them is dependent. But we need to represent the
7755 conversion for the benefit of cp_tree_equal. */
7758 maybe_convert_nontype_argument (tree type
, tree arg
)
7760 /* Auto parms get no conversion. */
7761 if (type_uses_auto (type
))
7763 /* We don't need or want to add this conversion now if we're going to use the
7764 argument for deduction. */
7765 if (value_dependent_expression_p (arg
))
7768 type
= cv_unqualified (type
);
7769 tree argtype
= TREE_TYPE (arg
);
7770 if (same_type_p (type
, argtype
))
7773 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7774 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7778 /* Convert the indicated template ARG as necessary to match the
7779 indicated template PARM. Returns the converted ARG, or
7780 error_mark_node if the conversion was unsuccessful. Error and
7781 warning messages are issued under control of COMPLAIN. This
7782 conversion is for the Ith parameter in the parameter list. ARGS is
7783 the full set of template arguments deduced so far. */
7786 convert_template_argument (tree parm
,
7789 tsubst_flags_t complain
,
7795 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7797 if (parm
== error_mark_node
|| error_operand_p (arg
))
7798 return error_mark_node
;
7800 /* Trivially convert placeholders. */
7801 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7802 return convert_wildcard_argument (parm
, arg
);
7804 if (arg
== any_targ_node
)
7807 if (TREE_CODE (arg
) == TREE_LIST
7808 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7810 /* The template argument was the name of some
7811 member function. That's usually
7812 invalid, but static members are OK. In any
7813 case, grab the underlying fields/functions
7814 and issue an error later if required. */
7815 orig_arg
= TREE_VALUE (arg
);
7816 TREE_TYPE (arg
) = unknown_type_node
;
7821 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7822 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7823 || requires_tmpl_type
);
7825 /* When determining whether an argument pack expansion is a template,
7826 look at the pattern. */
7827 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7828 arg
= PACK_EXPANSION_PATTERN (arg
);
7830 /* Deal with an injected-class-name used as a template template arg. */
7831 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7833 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7834 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7836 if (cxx_dialect
>= cxx11
)
7837 /* OK under DR 1004. */;
7838 else if (complain
& tf_warning_or_error
)
7839 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7840 " used as template template argument", TYPE_NAME (arg
));
7841 else if (flag_pedantic_errors
)
7849 ((TREE_CODE (arg
) == TEMPLATE_DECL
7850 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7851 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7852 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7853 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7856 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7857 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7858 arg
= TYPE_STUB_DECL (arg
);
7860 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7862 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7863 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7865 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7867 if (complain
& tf_error
)
7868 error ("invalid use of destructor %qE as a type", orig_arg
);
7869 return error_mark_node
;
7872 permerror (input_location
,
7873 "to refer to a type member of a template parameter, "
7874 "use %<typename %E%>", orig_arg
);
7876 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7877 TREE_OPERAND (arg
, 1),
7883 if (is_type
!= requires_type
)
7887 if (complain
& tf_error
)
7889 error ("type/value mismatch at argument %d in template "
7890 "parameter list for %qD",
7893 inform (input_location
,
7894 " expected a constant of type %qT, got %qT",
7896 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7897 else if (requires_tmpl_type
)
7898 inform (input_location
,
7899 " expected a class template, got %qE", orig_arg
);
7901 inform (input_location
,
7902 " expected a type, got %qE", orig_arg
);
7905 return error_mark_node
;
7907 if (is_tmpl_type
^ requires_tmpl_type
)
7909 if (in_decl
&& (complain
& tf_error
))
7911 error ("type/value mismatch at argument %d in template "
7912 "parameter list for %qD",
7915 inform (input_location
,
7916 " expected a type, got %qT", DECL_NAME (arg
));
7918 inform (input_location
,
7919 " expected a class template, got %qT", orig_arg
);
7921 return error_mark_node
;
7924 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7925 /* We already did the appropriate conversion when packing args. */
7929 if (requires_tmpl_type
)
7931 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7932 /* The number of argument required is not known yet.
7933 Just accept it for now. */
7937 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7940 /* Strip alias templates that are equivalent to another
7942 arg
= get_underlying_template (arg
);
7943 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7945 if (coerce_template_template_parms (parmparm
, argparm
,
7951 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7953 if (val
!= error_mark_node
)
7955 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7956 val
= TREE_TYPE (val
);
7957 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7958 val
= make_pack_expansion (val
, complain
);
7963 if (in_decl
&& (complain
& tf_error
))
7965 error ("type/value mismatch at argument %d in "
7966 "template parameter list for %qD",
7968 inform (input_location
,
7969 " expected a template of type %qD, got %qT",
7973 val
= error_mark_node
;
7976 // Check that the constraints are compatible before allowing the
7978 if (val
!= error_mark_node
)
7979 if (!is_compatible_template_arg (parm
, arg
))
7981 if (in_decl
&& (complain
& tf_error
))
7983 error ("constraint mismatch at argument %d in "
7984 "template parameter list for %qD",
7986 inform (input_location
, " expected %qD but got %qD",
7989 val
= error_mark_node
;
7995 /* We only form one instance of each template specialization.
7996 Therefore, if we use a non-canonical variant (i.e., a
7997 typedef), any future messages referring to the type will use
7998 the typedef, which is confusing if those future uses do not
7999 themselves also use the typedef. */
8001 val
= canonicalize_type_argument (val
, complain
);
8005 tree t
= TREE_TYPE (parm
);
8007 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8008 > TMPL_ARGS_DEPTH (args
))
8009 /* We don't have enough levels of args to do any substitution. This
8010 can happen in the context of -fnew-ttp-matching. */;
8011 else if (tree a
= type_uses_auto (t
))
8013 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8014 if (t
== error_mark_node
)
8015 return error_mark_node
;
8018 t
= tsubst (t
, args
, complain
, in_decl
);
8020 if (invalid_nontype_parm_type_p (t
, complain
))
8021 return error_mark_node
;
8023 if (t
!= TREE_TYPE (parm
))
8024 t
= canonicalize_type_argument (t
, complain
);
8026 if (!type_dependent_expression_p (orig_arg
)
8027 && !uses_template_parms (t
))
8028 /* We used to call digest_init here. However, digest_init
8029 will report errors, which we don't want when complain
8030 is zero. More importantly, digest_init will try too
8031 hard to convert things: for example, `0' should not be
8032 converted to pointer type at this point according to
8033 the standard. Accepting this is not merely an
8034 extension, since deciding whether or not these
8035 conversions can occur is part of determining which
8036 function template to call, or whether a given explicit
8037 argument specification is valid. */
8038 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8041 val
= canonicalize_expr_argument (orig_arg
, complain
);
8042 val
= maybe_convert_nontype_argument (t
, val
);
8046 if (val
== NULL_TREE
)
8047 val
= error_mark_node
;
8048 else if (val
== error_mark_node
&& (complain
& tf_error
))
8049 error ("could not convert template argument %qE from %qT to %qT",
8050 orig_arg
, TREE_TYPE (orig_arg
), t
);
8052 if (INDIRECT_REF_P (val
))
8054 /* Reject template arguments that are references to built-in
8055 functions with no library fallbacks. */
8056 const_tree inner
= TREE_OPERAND (val
, 0);
8057 const_tree innertype
= TREE_TYPE (inner
);
8059 && TYPE_REF_P (innertype
)
8060 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8061 && TREE_OPERAND_LENGTH (inner
) > 0
8062 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8063 return error_mark_node
;
8066 if (TREE_CODE (val
) == SCOPE_REF
)
8068 /* Strip typedefs from the SCOPE_REF. */
8069 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8070 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8072 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8073 QUALIFIED_NAME_IS_TEMPLATE (val
));
8080 /* Coerces the remaining template arguments in INNER_ARGS (from
8081 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8082 Returns the coerced argument pack. PARM_IDX is the position of this
8083 parameter in the template parameter list. ARGS is the original
8084 template argument list. */
8086 coerce_template_parameter_pack (tree parms
,
8094 tsubst_flags_t complain
)
8096 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8097 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8100 tree packed_parms
= NULL_TREE
;
8102 if (arg_idx
> nargs
)
8105 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8107 /* When the template parameter is a non-type template parameter pack
8108 or template template parameter pack whose type or template
8109 parameters use parameter packs, we know exactly how many arguments
8110 we are looking for. Build a vector of the instantiated decls for
8111 these template parameters in PACKED_PARMS. */
8112 /* We can't use make_pack_expansion here because it would interpret a
8113 _DECL as a use rather than a declaration. */
8114 tree decl
= TREE_VALUE (parm
);
8115 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8116 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8117 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8118 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8120 TREE_VEC_LENGTH (args
)--;
8121 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8122 TREE_VEC_LENGTH (args
)++;
8124 if (packed_parms
== error_mark_node
)
8125 return error_mark_node
;
8127 /* If we're doing a partial instantiation of a member template,
8128 verify that all of the types used for the non-type
8129 template parameter pack are, in fact, valid for non-type
8130 template parameters. */
8132 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8134 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8135 for (j
= 0; j
< len
; ++j
)
8137 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8138 if (invalid_nontype_parm_type_p (t
, complain
))
8139 return error_mark_node
;
8141 /* We don't know how many args we have yet, just
8142 use the unconverted ones for now. */
8146 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8148 /* Check if we have a placeholder pack, which indicates we're
8149 in the context of a introduction list. In that case we want
8150 to match this pack to the single placeholder. */
8151 else if (arg_idx
< nargs
8152 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8153 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8155 nargs
= arg_idx
+ 1;
8156 packed_args
= make_tree_vec (1);
8159 packed_args
= make_tree_vec (nargs
- arg_idx
);
8161 /* Convert the remaining arguments, which will be a part of the
8162 parameter pack "parm". */
8163 int first_pack_arg
= arg_idx
;
8164 for (; arg_idx
< nargs
; ++arg_idx
)
8166 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8167 tree actual_parm
= TREE_VALUE (parm
);
8168 int pack_idx
= arg_idx
- first_pack_arg
;
8172 /* Once we've packed as many args as we have types, stop. */
8173 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8175 else if (PACK_EXPANSION_P (arg
))
8176 /* We don't know how many args we have yet, just
8177 use the unconverted ones for now. */
8180 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8183 if (arg
== error_mark_node
)
8185 if (complain
& tf_error
)
8186 error ("template argument %d is invalid", arg_idx
+ 1);
8189 arg
= convert_template_argument (actual_parm
,
8190 arg
, new_args
, complain
, parm_idx
,
8192 if (arg
== error_mark_node
)
8194 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8197 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8198 && TREE_VEC_LENGTH (packed_args
) > 0)
8200 if (complain
& tf_error
)
8201 error ("wrong number of template arguments (%d, should be %d)",
8202 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8203 return error_mark_node
;
8206 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8207 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8208 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8211 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8212 TREE_CONSTANT (argument_pack
) = 1;
8215 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8218 TREE_VEC_LENGTH (packed_args
));
8219 return argument_pack
;
8222 /* Returns the number of pack expansions in the template argument vector
8226 pack_expansion_args_count (tree args
)
8231 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8233 tree elt
= TREE_VEC_ELT (args
, i
);
8234 if (elt
&& PACK_EXPANSION_P (elt
))
8240 /* Convert all template arguments to their appropriate types, and
8241 return a vector containing the innermost resulting template
8242 arguments. If any error occurs, return error_mark_node. Error and
8243 warning messages are issued under control of COMPLAIN.
8245 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8246 for arguments not specified in ARGS. Otherwise, if
8247 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8248 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8249 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8253 coerce_template_parms (tree parms
,
8256 tsubst_flags_t complain
,
8257 bool require_all_args
,
8258 bool use_default_args
)
8260 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8261 tree orig_inner_args
;
8264 tree new_inner_args
;
8266 /* When used as a boolean value, indicates whether this is a
8267 variadic template parameter list. Since it's an int, we can also
8268 subtract it from nparms to get the number of non-variadic
8271 int variadic_args_p
= 0;
8272 int post_variadic_parms
= 0;
8274 /* Adjustment to nparms for fixed parameter packs. */
8275 int fixed_pack_adjust
= 0;
8276 int fixed_packs
= 0;
8279 /* Likewise for parameters with default arguments. */
8282 if (args
== error_mark_node
)
8283 return error_mark_node
;
8285 nparms
= TREE_VEC_LENGTH (parms
);
8287 /* Determine if there are any parameter packs or default arguments. */
8288 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8290 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8292 ++post_variadic_parms
;
8293 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8295 if (TREE_PURPOSE (parm
))
8299 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8300 /* If there are no parameters that follow a parameter pack, we need to
8301 expand any argument packs so that we can deduce a parameter pack from
8302 some non-packed args followed by an argument pack, as in variadic85.C.
8303 If there are such parameters, we need to leave argument packs intact
8304 so the arguments are assigned properly. This can happen when dealing
8305 with a nested class inside a partial specialization of a class
8306 template, as in variadic92.C, or when deducing a template parameter pack
8307 from a sub-declarator, as in variadic114.C. */
8308 if (!post_variadic_parms
)
8309 inner_args
= expand_template_argument_pack (inner_args
);
8311 /* Count any pack expansion args. */
8312 variadic_args_p
= pack_expansion_args_count (inner_args
);
8314 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8315 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8316 || (nargs
< nparms
- variadic_p
8319 && (!use_default_args
8320 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8321 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8324 if (complain
& tf_error
)
8326 if (variadic_p
|| default_p
)
8328 nparms
-= variadic_p
+ default_p
;
8329 error ("wrong number of template arguments "
8330 "(%d, should be at least %d)", nargs
, nparms
);
8333 error ("wrong number of template arguments "
8334 "(%d, should be %d)", nargs
, nparms
);
8337 inform (DECL_SOURCE_LOCATION (in_decl
),
8338 "provided for %qD", in_decl
);
8341 return error_mark_node
;
8343 /* We can't pass a pack expansion to a non-pack parameter of an alias
8344 template (DR 1430). */
8346 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8347 || concept_template_p (in_decl
))
8349 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8351 if (complain
& tf_error
)
8353 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8355 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8356 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8358 if (PACK_EXPANSION_P (arg
)
8359 && !template_parameter_pack_p (parm
))
8361 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8362 error_at (location_of (arg
),
8363 "pack expansion argument for non-pack parameter "
8364 "%qD of alias template %qD", parm
, in_decl
);
8366 error_at (location_of (arg
),
8367 "pack expansion argument for non-pack parameter "
8368 "%qD of concept %qD", parm
, in_decl
);
8369 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8376 return error_mark_node
;
8379 /* We need to evaluate the template arguments, even though this
8380 template-id may be nested within a "sizeof". */
8383 new_inner_args
= make_tree_vec (nparms
);
8384 new_args
= add_outermost_template_args (args
, new_inner_args
);
8385 int pack_adjust
= 0;
8386 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8391 /* Get the Ith template parameter. */
8392 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8394 if (parm
== error_mark_node
)
8396 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8400 /* Calculate the next argument. */
8401 if (arg_idx
< nargs
)
8402 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8406 if (template_parameter_pack_p (TREE_VALUE (parm
))
8407 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8409 /* Some arguments will be placed in the
8410 template parameter pack PARM. */
8411 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8412 inner_args
, arg_idx
,
8416 if (arg
== NULL_TREE
)
8418 /* We don't know how many args we have yet, just use the
8419 unconverted (and still packed) ones for now. */
8420 new_inner_args
= orig_inner_args
;
8425 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8427 /* Store this argument. */
8428 if (arg
== error_mark_node
)
8431 /* We are done with all of the arguments. */
8437 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8438 arg_idx
+= pack_adjust
;
8439 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8442 fixed_pack_adjust
+= pack_adjust
;
8450 if (PACK_EXPANSION_P (arg
))
8452 /* "If every valid specialization of a variadic template
8453 requires an empty template parameter pack, the template is
8454 ill-formed, no diagnostic required." So check that the
8455 pattern works with this parameter. */
8456 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8457 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8461 if (conv
== error_mark_node
)
8463 if (complain
& tf_error
)
8464 inform (input_location
, "so any instantiation with a "
8465 "non-empty parameter pack would be ill-formed");
8468 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8469 /* Recover from missing typename. */
8470 TREE_VEC_ELT (inner_args
, arg_idx
)
8471 = make_pack_expansion (conv
, complain
);
8473 /* We don't know how many args we have yet, just
8474 use the unconverted ones for now. */
8475 new_inner_args
= inner_args
;
8480 else if (require_all_args
)
8482 /* There must be a default arg in this case. */
8483 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8485 /* The position of the first default template argument,
8486 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8488 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8489 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8490 arg_idx
- pack_adjust
);
8495 if (arg
== error_mark_node
)
8497 if (complain
& tf_error
)
8498 error ("template argument %d is invalid", arg_idx
+ 1);
8502 /* This can occur if there was an error in the template
8503 parameter list itself (which we would already have
8504 reported) that we are trying to recover from, e.g., a class
8505 template with a parameter list such as
8506 template<typename..., typename> (cpp0x/variadic150.C). */
8509 /* This can also happen with a fixed parameter pack (71834). */
8510 if (arg_idx
>= nargs
)
8514 arg
= convert_template_argument (TREE_VALUE (parm
),
8515 arg
, new_args
, complain
,
8518 if (arg
== error_mark_node
)
8520 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8523 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8525 /* If we had fixed parameter packs, we didn't know how many arguments we
8526 actually needed earlier; now we do. */
8527 nparms
+= fixed_pack_adjust
;
8528 variadic_p
-= fixed_packs
;
8532 if (arg_idx
< nargs
)
8534 /* We had some pack expansion arguments that will only work if the packs
8535 are empty, but wait until instantiation time to complain.
8536 See variadic-ttp3.C. */
8537 int len
= nparms
+ (nargs
- arg_idx
);
8538 tree args
= make_tree_vec (len
);
8540 for (; i
< nparms
; ++i
)
8541 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8542 for (; i
< len
; ++i
, ++arg_idx
)
8543 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8544 arg_idx
- pack_adjust
);
8545 new_inner_args
= args
;
8550 gcc_assert (!(complain
& tf_error
) || seen_error ());
8551 return error_mark_node
;
8554 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8555 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8556 TREE_VEC_LENGTH (new_inner_args
));
8558 return new_inner_args
;
8561 /* Convert all template arguments to their appropriate types, and
8562 return a vector containing the innermost resulting template
8563 arguments. If any error occurs, return error_mark_node. Error and
8564 warning messages are not issued.
8566 Note that no function argument deduction is performed, and default
8567 arguments are used to fill in unspecified arguments. */
8569 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8571 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8574 /* Convert all template arguments to their appropriate type, and
8575 instantiate default arguments as needed. This returns a vector
8576 containing the innermost resulting template arguments, or
8577 error_mark_node if unsuccessful. */
8579 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8580 tsubst_flags_t complain
)
8582 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8585 /* Like coerce_template_parms. If PARMS represents all template
8586 parameters levels, this function returns a vector of vectors
8587 representing all the resulting argument levels. Note that in this
8588 case, only the innermost arguments are coerced because the
8589 outermost ones are supposed to have been coerced already.
8591 Otherwise, if PARMS represents only (the innermost) vector of
8592 parameters, this function returns a vector containing just the
8593 innermost resulting arguments. */
8596 coerce_innermost_template_parms (tree parms
,
8599 tsubst_flags_t complain
,
8600 bool require_all_args
,
8601 bool use_default_args
)
8603 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8604 int args_depth
= TMPL_ARGS_DEPTH (args
);
8607 if (parms_depth
> 1)
8609 coerced_args
= make_tree_vec (parms_depth
);
8613 for (level
= parms
, cur_depth
= parms_depth
;
8614 parms_depth
> 0 && level
!= NULL_TREE
;
8615 level
= TREE_CHAIN (level
), --cur_depth
)
8618 if (cur_depth
== args_depth
)
8619 l
= coerce_template_parms (TREE_VALUE (level
),
8620 args
, in_decl
, complain
,
8624 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8626 if (l
== error_mark_node
)
8627 return error_mark_node
;
8629 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8633 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8634 args
, in_decl
, complain
,
8637 return coerced_args
;
8640 /* Returns 1 if template args OT and NT are equivalent. */
8643 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8647 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8649 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8652 if (TREE_CODE (nt
) == TREE_VEC
)
8653 /* For member templates */
8654 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8655 else if (PACK_EXPANSION_P (ot
))
8656 return (PACK_EXPANSION_P (nt
)
8657 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8658 PACK_EXPANSION_PATTERN (nt
))
8659 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8660 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8661 else if (ARGUMENT_PACK_P (ot
))
8666 if (!ARGUMENT_PACK_P (nt
))
8669 opack
= ARGUMENT_PACK_ARGS (ot
);
8670 npack
= ARGUMENT_PACK_ARGS (nt
);
8671 len
= TREE_VEC_LENGTH (opack
);
8672 if (TREE_VEC_LENGTH (npack
) != len
)
8674 for (i
= 0; i
< len
; ++i
)
8675 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8676 TREE_VEC_ELT (npack
, i
)))
8680 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8682 else if (TYPE_P (nt
))
8686 /* Don't treat an alias template specialization with dependent
8687 arguments as equivalent to its underlying type when used as a
8688 template argument; we need them to be distinct so that we
8689 substitute into the specialization arguments at instantiation
8690 time. And aliases can't be equivalent without being ==, so
8691 we don't need to look any deeper.
8693 During partial ordering, however, we need to treat them normally so
8694 that we can order uses of the same alias with different
8695 cv-qualification (79960). */
8697 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8700 return same_type_p (ot
, nt
);
8702 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8706 /* Try to treat a template non-type argument that has been converted
8707 to the parameter type as equivalent to one that hasn't yet. */
8708 for (enum tree_code code1
= TREE_CODE (ot
);
8709 CONVERT_EXPR_CODE_P (code1
)
8710 || code1
== NON_LVALUE_EXPR
;
8711 code1
= TREE_CODE (ot
))
8712 ot
= TREE_OPERAND (ot
, 0);
8713 for (enum tree_code code2
= TREE_CODE (nt
);
8714 CONVERT_EXPR_CODE_P (code2
)
8715 || code2
== NON_LVALUE_EXPR
;
8716 code2
= TREE_CODE (nt
))
8717 nt
= TREE_OPERAND (nt
, 0);
8719 return cp_tree_equal (ot
, nt
);
8723 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8724 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8725 NEWARG_PTR with the offending arguments if they are non-NULL. */
8728 comp_template_args (tree oldargs
, tree newargs
,
8729 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8734 if (oldargs
== newargs
)
8737 if (!oldargs
|| !newargs
)
8740 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8743 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8745 tree nt
= TREE_VEC_ELT (newargs
, i
);
8746 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8748 if (! template_args_equal (ot
, nt
, partial_order
))
8750 if (oldarg_ptr
!= NULL
)
8752 if (newarg_ptr
!= NULL
)
8761 comp_template_args_porder (tree oargs
, tree nargs
)
8763 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8766 /* Implement a freelist interface for objects of type T.
8768 Head is a separate object, rather than a regular member, so that we
8769 can define it as a GTY deletable pointer, which is highly
8770 desirable. A data member could be declared that way, but then the
8771 containing object would implicitly get GTY((user)), which would
8772 prevent us from instantiating freelists as global objects.
8773 Although this way we can create freelist global objects, they're
8774 such thin wrappers that instantiating temporaries at every use
8775 loses nothing and saves permanent storage for the freelist object.
8777 Member functions next, anew, poison and reinit have default
8778 implementations that work for most of the types we're interested
8779 in, but if they don't work for some type, they should be explicitly
8780 specialized. See the comments before them for requirements, and
8781 the example specializations for the tree_list_freelist. */
8782 template <typename T
>
8785 /* Return the next object in a chain. We could just do type
8786 punning, but if we access the object with its underlying type, we
8787 avoid strict-aliasing trouble. This needs only work between
8788 poison and reinit. */
8789 static T
*&next (T
*obj
) { return obj
->next
; }
8791 /* Return a newly allocated, uninitialized or minimally-initialized
8792 object of type T. Any initialization performed by anew should
8793 either remain across the life of the object and the execution of
8794 poison, or be redone by reinit. */
8795 static T
*anew () { return ggc_alloc
<T
> (); }
8797 /* Optionally scribble all over the bits holding the object, so that
8798 they become (mostly?) uninitialized memory. This is called while
8799 preparing to make the object part of the free list. */
8800 static void poison (T
*obj
) {
8801 T
*p ATTRIBUTE_UNUSED
= obj
;
8802 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8804 #ifdef ENABLE_GC_CHECKING
8805 /* Poison the data, to indicate the data is garbage. */
8806 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8807 memset (p
, 0xa5, sizeof (*p
));
8809 /* Let valgrind know the object is free. */
8810 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8812 /* Let valgrind know the next portion of the object is available,
8813 but uninitialized. */
8814 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8817 /* Bring an object that underwent at least one lifecycle after anew
8818 and before the most recent free and poison, back to a usable
8819 state, reinitializing whatever is needed for it to be
8820 functionally equivalent to an object just allocated and returned
8821 by anew. This may poison or clear the next field, used by
8822 freelist housekeeping after poison was called. */
8823 static void reinit (T
*obj
) {
8824 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8826 #ifdef ENABLE_GC_CHECKING
8827 memset (q
, 0xa5, sizeof (*q
));
8829 /* Let valgrind know the entire object is available, but
8831 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8834 /* Reference a GTY-deletable pointer that points to the first object
8835 in the free list proper. */
8838 /* Construct a freelist object chaining objects off of HEAD. */
8839 freelist (T
*&head
) : head(head
) {}
8841 /* Add OBJ to the free object list. The former head becomes OBJ's
8850 /* Take an object from the free list, if one is available, or
8851 allocate a new one. Objects taken from the free list should be
8852 regarded as filled with garbage, except for bits that are
8853 configured to be preserved across free and alloc. */
8868 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8869 want to allocate a TREE_LIST using the usual interface, and ensure
8870 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8871 build_tree_list logic in reinit, so this could go out of sync. */
8874 freelist
<tree_node
>::next (tree obj
)
8876 return TREE_CHAIN (obj
);
8880 freelist
<tree_node
>::anew ()
8882 return build_tree_list (NULL
, NULL
);
8886 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8888 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8889 tree p ATTRIBUTE_UNUSED
= obj
;
8890 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8891 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8893 #ifdef ENABLE_GC_CHECKING
8894 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8896 /* Poison the data, to indicate the data is garbage. */
8897 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8898 memset (p
, 0xa5, size
);
8900 /* Let valgrind know the object is free. */
8901 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8902 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8903 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8904 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8906 #ifdef ENABLE_GC_CHECKING
8907 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
8908 /* Keep TREE_CHAIN functional. */
8909 TREE_SET_CODE (obj
, TREE_LIST
);
8911 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8916 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
8918 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8920 #ifdef ENABLE_GC_CHECKING
8921 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8922 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8923 memset (obj
, 0, sizeof (tree_list
));
8926 /* Let valgrind know the entire object is available, but
8928 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8930 #ifdef ENABLE_GC_CHECKING
8931 TREE_SET_CODE (obj
, TREE_LIST
);
8933 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8937 /* Point to the first object in the TREE_LIST freelist. */
8938 static GTY((deletable
)) tree tree_list_freelist_head
;
8939 /* Return the/an actual TREE_LIST freelist. */
8940 static inline freelist
<tree_node
>
8941 tree_list_freelist ()
8943 return tree_list_freelist_head
;
8946 /* Point to the first object in the tinst_level freelist. */
8947 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
8948 /* Return the/an actual tinst_level freelist. */
8949 static inline freelist
<tinst_level
>
8950 tinst_level_freelist ()
8952 return tinst_level_freelist_head
;
8955 /* Point to the first object in the pending_template freelist. */
8956 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
8957 /* Return the/an actual pending_template freelist. */
8958 static inline freelist
<pending_template
>
8959 pending_template_freelist ()
8961 return pending_template_freelist_head
;
8964 /* Build the TREE_LIST object out of a split list, store it
8965 permanently, and return it. */
8967 tinst_level::to_list ()
8969 gcc_assert (split_list_p ());
8970 tree ret
= tree_list_freelist ().alloc ();
8971 TREE_PURPOSE (ret
) = tldcl
;
8972 TREE_VALUE (ret
) = targs
;
8975 gcc_assert (tree_list_p ());
8979 const unsigned short tinst_level::refcount_infinity
;
8981 /* Increment OBJ's refcount unless it is already infinite. */
8982 static tinst_level
*
8983 inc_refcount_use (tinst_level
*obj
)
8985 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
8990 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8992 tinst_level::free (tinst_level
*obj
)
8994 if (obj
->tree_list_p ())
8995 tree_list_freelist ().free (obj
->get_node ());
8996 tinst_level_freelist ().free (obj
);
8999 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9000 OBJ's DECL and OBJ, and start over with the tinst_level object that
9001 used to be referenced by OBJ's NEXT. */
9003 dec_refcount_use (tinst_level
*obj
)
9006 && obj
->refcount
!= tinst_level::refcount_infinity
9007 && !--obj
->refcount
)
9009 tinst_level
*next
= obj
->next
;
9010 tinst_level::free (obj
);
9015 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9016 and of the former PTR. Omitting the second argument is equivalent
9017 to passing (T*)NULL; this is allowed because passing the
9018 zero-valued integral constant NULL confuses type deduction and/or
9019 overload resolution. */
9020 template <typename T
>
9022 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9025 ptr
= inc_refcount_use (obj
);
9026 dec_refcount_use (save
);
9030 add_pending_template (tree d
)
9032 tree ti
= (TYPE_P (d
)
9033 ? CLASSTYPE_TEMPLATE_INFO (d
)
9034 : DECL_TEMPLATE_INFO (d
));
9035 struct pending_template
*pt
;
9038 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9041 /* We are called both from instantiate_decl, where we've already had a
9042 tinst_level pushed, and instantiate_template, where we haven't.
9044 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9045 level
= !current_tinst_level
9046 || current_tinst_level
->maybe_get_node () != d
;
9049 push_tinst_level (d
);
9051 pt
= pending_template_freelist ().alloc ();
9054 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9055 if (last_pending_template
)
9056 last_pending_template
->next
= pt
;
9058 pending_templates
= pt
;
9060 last_pending_template
= pt
;
9062 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9069 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9070 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9071 documentation for TEMPLATE_ID_EXPR. */
9074 lookup_template_function (tree fns
, tree arglist
)
9076 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9077 return error_mark_node
;
9079 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9081 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9083 error ("%q#D is not a function template", fns
);
9084 return error_mark_node
;
9087 if (BASELINK_P (fns
))
9089 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9091 BASELINK_FUNCTIONS (fns
),
9096 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9099 /* Within the scope of a template class S<T>, the name S gets bound
9100 (in build_self_reference) to a TYPE_DECL for the class, not a
9101 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9102 or one of its enclosing classes, and that type is a template,
9103 return the associated TEMPLATE_DECL. Otherwise, the original
9106 Also handle the case when DECL is a TREE_LIST of ambiguous
9107 injected-class-names from different bases. */
9110 maybe_get_template_decl_from_type_decl (tree decl
)
9112 if (decl
== NULL_TREE
)
9115 /* DR 176: A lookup that finds an injected-class-name (10.2
9116 [class.member.lookup]) can result in an ambiguity in certain cases
9117 (for example, if it is found in more than one base class). If all of
9118 the injected-class-names that are found refer to specializations of
9119 the same class template, and if the name is followed by a
9120 template-argument-list, the reference refers to the class template
9121 itself and not a specialization thereof, and is not ambiguous. */
9122 if (TREE_CODE (decl
) == TREE_LIST
)
9124 tree t
, tmpl
= NULL_TREE
;
9125 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9127 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9130 else if (tmpl
!= elt
)
9133 if (tmpl
&& t
== NULL_TREE
)
9139 return (decl
!= NULL_TREE
9140 && DECL_SELF_REFERENCE_P (decl
)
9141 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9142 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9145 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9146 parameters, find the desired type.
9148 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9150 IN_DECL, if non-NULL, is the template declaration we are trying to
9153 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9154 the class we are looking up.
9156 Issue error and warning messages under control of COMPLAIN.
9158 If the template class is really a local class in a template
9159 function, then the FUNCTION_CONTEXT is the function in which it is
9162 ??? Note that this function is currently called *twice* for each
9163 template-id: the first time from the parser, while creating the
9164 incomplete type (finish_template_type), and the second type during the
9165 real instantiation (instantiate_template_class). This is surely something
9166 that we want to avoid. It also causes some problems with argument
9167 coercion (see convert_nontype_argument for more information on this). */
9170 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9171 int entering_scope
, tsubst_flags_t complain
)
9173 tree templ
= NULL_TREE
, parmlist
;
9180 if (identifier_p (d1
))
9182 tree value
= innermost_non_namespace_value (d1
);
9183 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9188 push_decl_namespace (context
);
9189 templ
= lookup_name (d1
);
9190 templ
= maybe_get_template_decl_from_type_decl (templ
);
9192 pop_decl_namespace ();
9195 context
= DECL_CONTEXT (templ
);
9197 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9199 tree type
= TREE_TYPE (d1
);
9201 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9202 an implicit typename for the second A. Deal with it. */
9203 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9204 type
= TREE_TYPE (type
);
9206 if (CLASSTYPE_TEMPLATE_INFO (type
))
9208 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9209 d1
= DECL_NAME (templ
);
9212 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9213 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9215 templ
= TYPE_TI_TEMPLATE (d1
);
9216 d1
= DECL_NAME (templ
);
9218 else if (DECL_TYPE_TEMPLATE_P (d1
))
9221 d1
= DECL_NAME (templ
);
9222 context
= DECL_CONTEXT (templ
);
9224 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9227 d1
= DECL_NAME (templ
);
9230 /* Issue an error message if we didn't find a template. */
9233 if (complain
& tf_error
)
9234 error ("%qT is not a template", d1
);
9235 return error_mark_node
;
9238 if (TREE_CODE (templ
) != TEMPLATE_DECL
9239 /* Make sure it's a user visible template, if it was named by
9241 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9242 && !PRIMARY_TEMPLATE_P (templ
)))
9244 if (complain
& tf_error
)
9246 error ("non-template type %qT used as a template", d1
);
9248 error ("for template declaration %q+D", in_decl
);
9250 return error_mark_node
;
9253 complain
&= ~tf_user
;
9255 /* An alias that just changes the name of a template is equivalent to the
9256 other template, so if any of the arguments are pack expansions, strip
9257 the alias to avoid problems with a pack expansion passed to a non-pack
9258 alias template parameter (DR 1430). */
9259 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9260 templ
= get_underlying_template (templ
);
9262 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9265 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9266 if (arglist2
== error_mark_node
9267 || (!uses_template_parms (arglist2
)
9268 && check_instantiated_args (templ
, arglist2
, complain
)))
9269 return error_mark_node
;
9271 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9276 tree template_type
= TREE_TYPE (templ
);
9279 tree found
= NULL_TREE
;
9282 int is_dependent_type
;
9283 int use_partial_inst_tmpl
= false;
9285 if (template_type
== error_mark_node
)
9286 /* An error occurred while building the template TEMPL, and a
9287 diagnostic has most certainly been emitted for that
9288 already. Let's propagate that error. */
9289 return error_mark_node
;
9291 gen_tmpl
= most_general_template (templ
);
9292 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9293 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9294 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9296 if (arg_depth
== 1 && parm_depth
> 1)
9298 /* We've been given an incomplete set of template arguments.
9301 template <class T> struct S1 {
9302 template <class U> struct S2 {};
9303 template <class U> struct S2<U*> {};
9306 we will be called with an ARGLIST of `U*', but the
9307 TEMPLATE will be `template <class T> template
9308 <class U> struct S1<T>::S2'. We must fill in the missing
9310 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9311 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9312 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9315 /* Now we should have enough arguments. */
9316 gcc_assert (parm_depth
== arg_depth
);
9318 /* From here on, we're only interested in the most general
9321 /* Calculate the BOUND_ARGS. These will be the args that are
9322 actually tsubst'd into the definition to create the
9324 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9326 /*require_all_args=*/true,
9327 /*use_default_args=*/true);
9329 if (arglist
== error_mark_node
)
9330 /* We were unable to bind the arguments. */
9331 return error_mark_node
;
9333 /* In the scope of a template class, explicit references to the
9334 template class refer to the type of the template, not any
9335 instantiation of it. For example, in:
9337 template <class T> class C { void f(C<T>); }
9339 the `C<T>' is just the same as `C'. Outside of the
9340 class, however, such a reference is an instantiation. */
9342 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9343 || currently_open_class (template_type
))
9345 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9347 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9348 return template_type
;
9351 /* If we already have this specialization, return it. */
9352 elt
.tmpl
= gen_tmpl
;
9354 elt
.spec
= NULL_TREE
;
9355 hash
= spec_hasher::hash (&elt
);
9356 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9361 /* If the the template's constraints are not satisfied,
9362 then we cannot form a valid type.
9364 Note that the check is deferred until after the hash
9365 lookup. This prevents redundant checks on previously
9366 instantiated specializations. */
9367 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9369 if (complain
& tf_error
)
9371 auto_diagnostic_group d
;
9372 error ("template constraint failure");
9373 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9375 return error_mark_node
;
9378 is_dependent_type
= uses_template_parms (arglist
);
9380 /* If the deduced arguments are invalid, then the binding
9382 if (!is_dependent_type
9383 && check_instantiated_args (gen_tmpl
,
9384 INNERMOST_TEMPLATE_ARGS (arglist
),
9386 return error_mark_node
;
9388 if (!is_dependent_type
9389 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9390 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9391 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9393 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9394 DECL_NAME (gen_tmpl
),
9395 /*tag_scope=*/ts_global
);
9399 context
= DECL_CONTEXT (gen_tmpl
);
9400 if (context
&& TYPE_P (context
))
9402 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9403 context
= complete_type (context
);
9406 context
= tsubst (context
, arglist
, complain
, in_decl
);
9408 if (context
== error_mark_node
)
9409 return error_mark_node
;
9412 context
= global_namespace
;
9414 /* Create the type. */
9415 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9417 /* The user referred to a specialization of an alias
9418 template represented by GEN_TMPL.
9420 [temp.alias]/2 says:
9422 When a template-id refers to the specialization of an
9423 alias template, it is equivalent to the associated
9424 type obtained by substitution of its
9425 template-arguments for the template-parameters in the
9426 type-id of the alias template. */
9428 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9429 /* Note that the call above (by indirectly calling
9430 register_specialization in tsubst_decl) registers the
9431 TYPE_DECL representing the specialization of the alias
9432 template. So next time someone substitutes ARGLIST for
9433 the template parms into the alias template (GEN_TMPL),
9434 she'll get that TYPE_DECL back. */
9436 if (t
== error_mark_node
)
9439 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9441 if (!is_dependent_type
)
9443 set_current_access_from_decl (TYPE_NAME (template_type
));
9444 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9445 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9446 arglist
, complain
, in_decl
),
9447 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9448 arglist
, complain
, in_decl
),
9449 SCOPED_ENUM_P (template_type
), NULL
);
9451 if (t
== error_mark_node
)
9456 /* We don't want to call start_enum for this type, since
9457 the values for the enumeration constants may involve
9458 template parameters. And, no one should be interested
9459 in the enumeration constants for such a type. */
9460 t
= cxx_make_type (ENUMERAL_TYPE
);
9461 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9463 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9464 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9465 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9467 else if (CLASS_TYPE_P (template_type
))
9469 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9470 instantiated here. */
9471 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9473 t
= make_class_type (TREE_CODE (template_type
));
9474 CLASSTYPE_DECLARED_CLASS (t
)
9475 = CLASSTYPE_DECLARED_CLASS (template_type
);
9476 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9478 /* A local class. Make sure the decl gets registered properly. */
9479 if (context
== current_function_decl
)
9480 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9482 return error_mark_node
;
9484 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9485 /* This instantiation is another name for the primary
9486 template type. Set the TYPE_CANONICAL field
9488 TYPE_CANONICAL (t
) = template_type
;
9489 else if (any_template_arguments_need_structural_equality_p (arglist
))
9490 /* Some of the template arguments require structural
9491 equality testing, so this template class requires
9492 structural equality testing. */
9493 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9498 /* If we called start_enum or pushtag above, this information
9499 will already be set up. */
9502 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9504 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9505 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9506 DECL_SOURCE_LOCATION (type_decl
)
9507 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9510 type_decl
= TYPE_NAME (t
);
9512 if (CLASS_TYPE_P (template_type
))
9514 TREE_PRIVATE (type_decl
)
9515 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9516 TREE_PROTECTED (type_decl
)
9517 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9518 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9520 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9521 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9525 if (OVERLOAD_TYPE_P (t
)
9526 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9528 static const char *tags
[] = {"abi_tag", "may_alias"};
9530 for (unsigned ix
= 0; ix
!= 2; ix
++)
9533 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9537 = tree_cons (TREE_PURPOSE (attributes
),
9538 TREE_VALUE (attributes
),
9539 TYPE_ATTRIBUTES (t
));
9543 /* Let's consider the explicit specialization of a member
9544 of a class template specialization that is implicitly instantiated,
9549 template<class U> struct M {}; //#0
9554 struct S<int>::M<char> //#1
9558 [temp.expl.spec]/4 says this is valid.
9560 In this case, when we write:
9563 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9566 When we encounter #1, we want to store the partial instantiation
9567 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9569 For all cases other than this "explicit specialization of member of a
9570 class template", we just want to store the most general template into
9571 the CLASSTYPE_TI_TEMPLATE of M.
9573 This case of "explicit specialization of member of a class template"
9575 1/ the enclosing class is an instantiation of, and therefore not
9576 the same as, the context of the most general template, and
9577 2/ we aren't looking at the partial instantiation itself, i.e.
9578 the innermost arguments are not the same as the innermost parms of
9579 the most general template.
9581 So it's only when 1/ and 2/ happens that we want to use the partial
9582 instantiation of the member template in lieu of its most general
9585 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9586 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9587 /* the enclosing class must be an instantiation... */
9588 && CLASS_TYPE_P (context
)
9589 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9591 TREE_VEC_LENGTH (arglist
)--;
9592 ++processing_template_decl
;
9593 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9594 tree partial_inst_args
=
9595 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9596 arglist
, complain
, NULL_TREE
);
9597 --processing_template_decl
;
9598 TREE_VEC_LENGTH (arglist
)++;
9599 if (partial_inst_args
== error_mark_node
)
9600 return error_mark_node
;
9601 use_partial_inst_tmpl
=
9602 /*...and we must not be looking at the partial instantiation
9604 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9608 if (!use_partial_inst_tmpl
)
9609 /* This case is easy; there are no member templates involved. */
9613 /* This is a full instantiation of a member template. Find
9614 the partial instantiation of which this is an instance. */
9616 /* Temporarily reduce by one the number of levels in the ARGLIST
9617 so as to avoid comparing the last set of arguments. */
9618 TREE_VEC_LENGTH (arglist
)--;
9619 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9620 TREE_VEC_LENGTH (arglist
)++;
9621 /* FOUND is either a proper class type, or an alias
9622 template specialization. In the later case, it's a
9623 TYPE_DECL, resulting from the substituting of arguments
9624 for parameters in the TYPE_DECL of the alias template
9625 done earlier. So be careful while getting the template
9627 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9629 : (TREE_CODE (found
) == TYPE_DECL
9630 ? DECL_TI_TEMPLATE (found
)
9631 : CLASSTYPE_TI_TEMPLATE (found
)));
9634 // Build template info for the new specialization.
9635 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9638 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9639 entry
= ggc_alloc
<spec_entry
> ();
9643 /* Note this use of the partial instantiation so we can check it
9644 later in maybe_process_partial_specialization. */
9645 DECL_TEMPLATE_INSTANTIATIONS (found
)
9646 = tree_cons (arglist
, t
,
9647 DECL_TEMPLATE_INSTANTIATIONS (found
));
9649 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9650 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9651 /* Now that the type has been registered on the instantiations
9652 list, we set up the enumerators. Because the enumeration
9653 constants may involve the enumeration type itself, we make
9654 sure to register the type first, and then create the
9655 constants. That way, doing tsubst_expr for the enumeration
9656 constants won't result in recursive calls here; we'll find
9657 the instantiation and exit above. */
9658 tsubst_enum (template_type
, t
, arglist
);
9660 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9661 /* If the type makes use of template parameters, the
9662 code that generates debugging information will crash. */
9663 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9665 /* Possibly limit visibility based on template args. */
9666 TREE_PUBLIC (type_decl
) = 1;
9667 determine_visibility (type_decl
);
9669 inherit_targ_abi_tags (t
);
9675 /* Wrapper for lookup_template_class_1. */
9678 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9679 int entering_scope
, tsubst_flags_t complain
)
9682 timevar_push (TV_TEMPLATE_INST
);
9683 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9684 entering_scope
, complain
);
9685 timevar_pop (TV_TEMPLATE_INST
);
9689 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9692 lookup_template_variable (tree templ
, tree arglist
)
9694 /* The type of the expression is NULL_TREE since the template-id could refer
9695 to an explicit or partial specialization. */
9696 tree type
= NULL_TREE
;
9697 if (flag_concepts
&& variable_concept_p (templ
))
9698 /* Except that concepts are always bool. */
9699 type
= boolean_type_node
;
9700 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9703 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9706 finish_template_variable (tree var
, tsubst_flags_t complain
)
9708 tree templ
= TREE_OPERAND (var
, 0);
9709 tree arglist
= TREE_OPERAND (var
, 1);
9711 /* We never want to return a VAR_DECL for a variable concept, since they
9712 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9713 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9714 if (concept_p
&& processing_template_decl
)
9717 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9718 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9720 templ
= most_general_template (templ
);
9721 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9722 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9724 /*use_default*/true);
9726 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9728 if (complain
& tf_error
)
9730 auto_diagnostic_group d
;
9731 error ("use of invalid variable template %qE", var
);
9732 diagnose_constraints (location_of (var
), templ
, arglist
);
9734 return error_mark_node
;
9737 /* If a template-id refers to a specialization of a variable
9738 concept, then the expression is true if and only if the
9739 concept's constraints are satisfied by the given template
9742 NOTE: This is an extension of Concepts Lite TS that
9743 allows constraints to be used in expressions. */
9746 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9747 return evaluate_variable_concept (decl
, arglist
);
9750 return instantiate_template (templ
, arglist
, complain
);
9753 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9754 TARGS template args, and instantiate it if it's not dependent. */
9757 lookup_and_finish_template_variable (tree templ
, tree targs
,
9758 tsubst_flags_t complain
)
9760 templ
= lookup_template_variable (templ
, targs
);
9761 if (!any_dependent_template_arguments_p (targs
))
9763 templ
= finish_template_variable (templ
, complain
);
9767 return convert_from_reference (templ
);
9776 /* True when we should also visit template parameters that occur in
9777 non-deduced contexts. */
9778 bool include_nondeduced_p
;
9779 hash_set
<tree
> *visited
;
9782 /* Called from for_each_template_parm via walk_tree. */
9785 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9788 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9789 tree_fn_t fn
= pfd
->fn
;
9790 void *data
= pfd
->data
;
9791 tree result
= NULL_TREE
;
9793 #define WALK_SUBTREE(NODE) \
9796 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9797 pfd->include_nondeduced_p, \
9799 if (result) goto out; \
9803 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9807 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9808 WALK_SUBTREE (TYPE_CONTEXT (t
));
9810 switch (TREE_CODE (t
))
9813 if (TYPE_PTRMEMFUNC_P (t
))
9819 if (!TYPE_TEMPLATE_INFO (t
))
9822 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9826 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9827 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9831 /* Since we're not going to walk subtrees, we have to do this
9833 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9837 /* Check the return type. */
9838 WALK_SUBTREE (TREE_TYPE (t
));
9840 /* Check the parameter types. Since default arguments are not
9841 instantiated until they are needed, the TYPE_ARG_TYPES may
9842 contain expressions that involve template parameters. But,
9843 no-one should be looking at them yet. And, once they're
9844 instantiated, they don't contain template parameters, so
9845 there's no point in looking at them then, either. */
9849 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9850 WALK_SUBTREE (TREE_VALUE (parm
));
9852 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9853 want walk_tree walking into them itself. */
9857 if (flag_noexcept_type
)
9859 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9861 WALK_SUBTREE (TREE_PURPOSE (spec
));
9867 case UNDERLYING_TYPE
:
9868 if (pfd
->include_nondeduced_p
9869 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9871 pfd
->include_nondeduced_p
,
9873 return error_mark_node
;
9874 *walk_subtrees
= false;
9879 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9880 WALK_SUBTREE (DECL_TI_ARGS (t
));
9885 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9886 WALK_SUBTREE (DECL_INITIAL (t
));
9887 if (DECL_CONTEXT (t
)
9888 && pfd
->include_nondeduced_p
)
9889 WALK_SUBTREE (DECL_CONTEXT (t
));
9892 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9893 /* Record template parameters such as `T' inside `TT<T>'. */
9894 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9897 case TEMPLATE_TEMPLATE_PARM
:
9898 case TEMPLATE_TYPE_PARM
:
9899 case TEMPLATE_PARM_INDEX
:
9900 if (fn
&& (*fn
)(t
, data
))
9907 /* A template template parameter is encountered. */
9908 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9909 WALK_SUBTREE (TREE_TYPE (t
));
9911 /* Already substituted template template parameter */
9916 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9917 partial instantiation. */
9918 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9922 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9923 && pfd
->include_nondeduced_p
)
9924 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9929 /* If there's no type, then this thing must be some expression
9930 involving template parameters. */
9931 if (!fn
&& !TREE_TYPE (t
))
9932 return error_mark_node
;
9937 case IMPLICIT_CONV_EXPR
:
9938 case REINTERPRET_CAST_EXPR
:
9939 case CONST_CAST_EXPR
:
9940 case STATIC_CAST_EXPR
:
9941 case DYNAMIC_CAST_EXPR
:
9945 case PSEUDO_DTOR_EXPR
:
9947 return error_mark_node
;
9956 /* We didn't find any template parameters we liked. */
9961 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9962 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9963 call FN with the parameter and the DATA.
9964 If FN returns nonzero, the iteration is terminated, and
9965 for_each_template_parm returns 1. Otherwise, the iteration
9966 continues. If FN never returns a nonzero value, the value
9967 returned by for_each_template_parm is 0. If FN is NULL, it is
9968 considered to be the function which always returns 1.
9970 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9971 parameters that occur in non-deduced contexts. When false, only
9972 visits those template parameters that can be deduced. */
9975 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9976 hash_set
<tree
> *visited
,
9977 bool include_nondeduced_p
,
9980 struct pair_fn_data pfd
;
9985 pfd
.any_fn
= any_fn
;
9987 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9989 /* Walk the tree. (Conceptually, we would like to walk without
9990 duplicates, but for_each_template_parm_r recursively calls
9991 for_each_template_parm, so we would need to reorganize a fair
9992 bit to use walk_tree_without_duplicates, so we keep our own
9995 pfd
.visited
= visited
;
9997 pfd
.visited
= new hash_set
<tree
>;
9998 result
= cp_walk_tree (&t
,
9999 for_each_template_parm_r
,
10006 delete pfd
.visited
;
10013 /* Returns true if T depends on any template parameter. */
10016 uses_template_parms (tree t
)
10018 if (t
== NULL_TREE
)
10022 int saved_processing_template_decl
;
10024 saved_processing_template_decl
= processing_template_decl
;
10025 if (!saved_processing_template_decl
)
10026 processing_template_decl
= 1;
10028 dependent_p
= dependent_type_p (t
);
10029 else if (TREE_CODE (t
) == TREE_VEC
)
10030 dependent_p
= any_dependent_template_arguments_p (t
);
10031 else if (TREE_CODE (t
) == TREE_LIST
)
10032 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10033 || uses_template_parms (TREE_CHAIN (t
)));
10034 else if (TREE_CODE (t
) == TYPE_DECL
)
10035 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10036 else if (DECL_P (t
)
10038 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10039 || TREE_CODE (t
) == OVERLOAD
10041 || identifier_p (t
)
10042 || TREE_CODE (t
) == TRAIT_EXPR
10043 || TREE_CODE (t
) == CONSTRUCTOR
10044 || CONSTANT_CLASS_P (t
))
10045 dependent_p
= (type_dependent_expression_p (t
)
10046 || value_dependent_expression_p (t
));
10049 gcc_assert (t
== error_mark_node
);
10050 dependent_p
= false;
10053 processing_template_decl
= saved_processing_template_decl
;
10055 return dependent_p
;
10058 /* Returns true iff current_function_decl is an incompletely instantiated
10059 template. Useful instead of processing_template_decl because the latter
10060 is set to 0 during instantiate_non_dependent_expr. */
10063 in_template_function (void)
10065 tree fn
= current_function_decl
;
10067 ++processing_template_decl
;
10068 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10069 && DECL_TEMPLATE_INFO (fn
)
10070 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10071 --processing_template_decl
;
10075 /* Returns true if T depends on any template parameter with level LEVEL. */
10078 uses_template_parms_level (tree t
, int level
)
10080 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10081 /*include_nondeduced_p=*/true);
10084 /* Returns true if the signature of DECL depends on any template parameter from
10085 its enclosing class. */
10088 uses_outer_template_parms (tree decl
)
10090 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10093 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10094 &depth
, NULL
, /*include_nondeduced_p=*/true))
10096 if (PRIMARY_TEMPLATE_P (decl
)
10097 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10098 (DECL_TEMPLATE_PARMS (decl
)),
10099 template_parm_outer_level
,
10100 &depth
, NULL
, /*include_nondeduced_p=*/true))
10102 tree ci
= get_constraints (decl
);
10104 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10105 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10106 &depth
, NULL
, /*nondeduced*/true))
10111 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10112 ill-formed translation unit, i.e. a variable or function that isn't
10113 usable in a constant expression. */
10116 neglectable_inst_p (tree d
)
10118 return (d
&& DECL_P (d
)
10119 && !undeduced_auto_decl (d
)
10120 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10121 : decl_maybe_constant_var_p (d
)));
10124 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10125 neglectable and instantiated from within an erroneous instantiation. */
10128 limit_bad_template_recursion (tree decl
)
10130 struct tinst_level
*lev
= current_tinst_level
;
10131 int errs
= errorcount
+ sorrycount
;
10132 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10135 for (; lev
; lev
= lev
->next
)
10136 if (neglectable_inst_p (lev
->maybe_get_node ()))
10139 return (lev
&& errs
> lev
->errors
);
10142 static int tinst_depth
;
10143 extern int max_tinst_depth
;
10146 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10148 /* We're starting to instantiate D; record the template instantiation context
10149 at LOC for diagnostics and to restore it later. */
10152 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10154 struct tinst_level
*new_level
;
10156 if (tinst_depth
>= max_tinst_depth
)
10158 /* Tell error.c not to try to instantiate any templates. */
10160 fatal_error (input_location
,
10161 "template instantiation depth exceeds maximum of %d"
10162 " (use -ftemplate-depth= to increase the maximum)",
10167 /* If the current instantiation caused problems, don't let it instantiate
10168 anything else. Do allow deduction substitution and decls usable in
10169 constant expressions. */
10170 if (!targs
&& limit_bad_template_recursion (tldcl
))
10173 /* When not -quiet, dump template instantiations other than functions, since
10174 announce_function will take care of those. */
10175 if (!quiet_flag
&& !targs
10176 && TREE_CODE (tldcl
) != TREE_LIST
10177 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10178 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10180 new_level
= tinst_level_freelist ().alloc ();
10181 new_level
->tldcl
= tldcl
;
10182 new_level
->targs
= targs
;
10183 new_level
->locus
= loc
;
10184 new_level
->errors
= errorcount
+ sorrycount
;
10185 new_level
->next
= NULL
;
10186 new_level
->refcount
= 0;
10187 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10188 set_refcount_ptr (current_tinst_level
, new_level
);
10191 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10192 depth_reached
= tinst_depth
;
10197 /* We're starting substitution of TMPL<ARGS>; record the template
10198 substitution context for diagnostics and to restore it later. */
10201 push_tinst_level (tree tmpl
, tree args
)
10203 return push_tinst_level_loc (tmpl
, args
, input_location
);
10206 /* We're starting to instantiate D; record INPUT_LOCATION and the
10207 template instantiation context for diagnostics and to restore it
10211 push_tinst_level (tree d
)
10213 return push_tinst_level_loc (d
, input_location
);
10216 /* Likewise, but record LOC as the program location. */
10219 push_tinst_level_loc (tree d
, location_t loc
)
10221 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10222 return push_tinst_level_loc (d
, NULL
, loc
);
10225 /* We're done instantiating this template; return to the instantiation
10229 pop_tinst_level (void)
10231 /* Restore the filename and line number stashed away when we started
10232 this instantiation. */
10233 input_location
= current_tinst_level
->locus
;
10234 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10238 /* We're instantiating a deferred template; restore the template
10239 instantiation context in which the instantiation was requested, which
10240 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10243 reopen_tinst_level (struct tinst_level
*level
)
10245 struct tinst_level
*t
;
10248 for (t
= level
; t
; t
= t
->next
)
10251 set_refcount_ptr (current_tinst_level
, level
);
10252 pop_tinst_level ();
10253 if (current_tinst_level
)
10254 current_tinst_level
->errors
= errorcount
+sorrycount
;
10255 return level
->maybe_get_node ();
10258 /* Returns the TINST_LEVEL which gives the original instantiation
10261 struct tinst_level
*
10262 outermost_tinst_level (void)
10264 struct tinst_level
*level
= current_tinst_level
;
10266 while (level
->next
)
10267 level
= level
->next
;
10271 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10272 vector of template arguments, as for tsubst.
10274 Returns an appropriate tsubst'd friend declaration. */
10277 tsubst_friend_function (tree decl
, tree args
)
10281 if (TREE_CODE (decl
) == FUNCTION_DECL
10282 && DECL_TEMPLATE_INSTANTIATION (decl
)
10283 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10284 /* This was a friend declared with an explicit template
10285 argument list, e.g.:
10287 friend void f<>(T);
10289 to indicate that f was a template instantiation, not a new
10290 function declaration. Now, we have to figure out what
10291 instantiation of what template. */
10293 tree template_id
, arglist
, fns
;
10296 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10298 /* Friend functions are looked up in the containing namespace scope.
10299 We must enter that scope, to avoid finding member functions of the
10300 current class with same name. */
10301 push_nested_namespace (ns
);
10302 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10303 tf_warning_or_error
, NULL_TREE
,
10304 /*integral_constant_expression_p=*/false);
10305 pop_nested_namespace (ns
);
10306 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10307 tf_warning_or_error
, NULL_TREE
);
10308 template_id
= lookup_template_function (fns
, arglist
);
10310 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10311 tmpl
= determine_specialization (template_id
, new_friend
,
10313 /*need_member_template=*/0,
10314 TREE_VEC_LENGTH (args
),
10316 return instantiate_template (tmpl
, new_args
, tf_error
);
10319 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10321 /* The NEW_FRIEND will look like an instantiation, to the
10322 compiler, but is not an instantiation from the point of view of
10323 the language. For example, we might have had:
10325 template <class T> struct S {
10326 template <class U> friend void f(T, U);
10329 Then, in S<int>, template <class U> void f(int, U) is not an
10330 instantiation of anything. */
10331 if (new_friend
== error_mark_node
)
10332 return error_mark_node
;
10334 DECL_USE_TEMPLATE (new_friend
) = 0;
10335 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10337 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10338 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10339 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10342 /* The mangled name for the NEW_FRIEND is incorrect. The function
10343 is not a template instantiation and should not be mangled like
10344 one. Therefore, we forget the mangling here; we'll recompute it
10345 later if we need it. */
10346 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10348 SET_DECL_RTL (new_friend
, NULL
);
10349 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10352 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10355 tree new_friend_template_info
;
10356 tree new_friend_result_template_info
;
10358 int new_friend_is_defn
;
10360 /* We must save some information from NEW_FRIEND before calling
10361 duplicate decls since that function will free NEW_FRIEND if
10363 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10364 new_friend_is_defn
=
10365 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10366 (template_for_substitution (new_friend
)))
10368 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10370 /* This declaration is a `primary' template. */
10371 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10373 new_friend_result_template_info
10374 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10377 new_friend_result_template_info
= NULL_TREE
;
10379 /* Inside pushdecl_namespace_level, we will push into the
10380 current namespace. However, the friend function should go
10381 into the namespace of the template. */
10382 ns
= decl_namespace_context (new_friend
);
10383 push_nested_namespace (ns
);
10384 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10385 pop_nested_namespace (ns
);
10387 if (old_decl
== error_mark_node
)
10388 return error_mark_node
;
10390 if (old_decl
!= new_friend
)
10392 /* This new friend declaration matched an existing
10393 declaration. For example, given:
10395 template <class T> void f(T);
10396 template <class U> class C {
10397 template <class T> friend void f(T) {}
10400 the friend declaration actually provides the definition
10401 of `f', once C has been instantiated for some type. So,
10402 old_decl will be the out-of-class template declaration,
10403 while new_friend is the in-class definition.
10405 But, if `f' was called before this point, the
10406 instantiation of `f' will have DECL_TI_ARGS corresponding
10407 to `T' but not to `U', references to which might appear
10408 in the definition of `f'. Previously, the most general
10409 template for an instantiation of `f' was the out-of-class
10410 version; now it is the in-class version. Therefore, we
10411 run through all specialization of `f', adding to their
10412 DECL_TI_ARGS appropriately. In particular, they need a
10413 new set of outer arguments, corresponding to the
10414 arguments for this class instantiation.
10416 The same situation can arise with something like this:
10418 friend void f(int);
10419 template <class T> class C {
10420 friend void f(T) {}
10423 when `C<int>' is instantiated. Now, `f(int)' is defined
10426 if (!new_friend_is_defn
)
10427 /* On the other hand, if the in-class declaration does
10428 *not* provide a definition, then we don't want to alter
10429 existing definitions. We can just leave everything
10434 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10435 tree new_args
= TI_ARGS (new_friend_template_info
);
10437 /* Overwrite whatever template info was there before, if
10438 any, with the new template information pertaining to
10439 the declaration. */
10440 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10442 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10444 /* We should have called reregister_specialization in
10445 duplicate_decls. */
10446 gcc_assert (retrieve_specialization (new_template
,
10450 /* Instantiate it if the global has already been used. */
10451 if (DECL_ODR_USED (old_decl
))
10452 instantiate_decl (old_decl
, /*defer_ok=*/true,
10453 /*expl_inst_class_mem_p=*/false);
10459 /* Indicate that the old function template is a partial
10461 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10462 = new_friend_result_template_info
;
10464 gcc_assert (new_template
10465 == most_general_template (new_template
));
10466 gcc_assert (new_template
!= old_decl
);
10468 /* Reassign any specializations already in the hash table
10469 to the new more general template, and add the
10470 additional template args. */
10471 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10473 t
= TREE_CHAIN (t
))
10475 tree spec
= TREE_VALUE (t
);
10478 elt
.tmpl
= old_decl
;
10479 elt
.args
= DECL_TI_ARGS (spec
);
10480 elt
.spec
= NULL_TREE
;
10482 decl_specializations
->remove_elt (&elt
);
10484 DECL_TI_ARGS (spec
)
10485 = add_outermost_template_args (new_args
,
10486 DECL_TI_ARGS (spec
));
10488 register_specialization
10489 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10492 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10496 /* The information from NEW_FRIEND has been merged into OLD_DECL
10497 by duplicate_decls. */
10498 new_friend
= old_decl
;
10503 tree context
= DECL_CONTEXT (new_friend
);
10507 template <class T> class C {
10508 template <class U> friend void C1<U>::f (); // case 1
10509 friend void C2<T>::f (); // case 2
10511 we only need to make sure CONTEXT is a complete type for
10512 case 2. To distinguish between the two cases, we note that
10513 CONTEXT of case 1 remains dependent type after tsubst while
10514 this isn't true for case 2. */
10515 ++processing_template_decl
;
10516 dependent_p
= dependent_type_p (context
);
10517 --processing_template_decl
;
10520 && !complete_type_or_else (context
, NULL_TREE
))
10521 return error_mark_node
;
10523 if (COMPLETE_TYPE_P (context
))
10525 tree fn
= new_friend
;
10526 /* do_friend adds the TEMPLATE_DECL for any member friend
10527 template even if it isn't a member template, i.e.
10528 template <class T> friend A<T>::f();
10529 Look through it in that case. */
10530 if (TREE_CODE (fn
) == TEMPLATE_DECL
10531 && !PRIMARY_TEMPLATE_P (fn
))
10532 fn
= DECL_TEMPLATE_RESULT (fn
);
10533 /* Check to see that the declaration is really present, and,
10534 possibly obtain an improved declaration. */
10535 fn
= check_classfn (context
, fn
, NULL_TREE
);
10545 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10546 template arguments, as for tsubst.
10548 Returns an appropriate tsubst'd friend type or error_mark_node on
10552 tsubst_friend_class (tree friend_tmpl
, tree args
)
10556 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10558 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10559 return TREE_TYPE (tmpl
);
10562 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10563 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10564 push_nested_namespace (context
);
10567 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
10568 push_nested_class (context
);
10571 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10572 /*non_class=*/false, /*block_p=*/false,
10573 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10575 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10577 /* The friend template has already been declared. Just
10578 check to see that the declarations match, and install any new
10579 default parameters. We must tsubst the default parameters,
10580 of course. We only need the innermost template parameters
10581 because that is all that redeclare_class_template will look
10583 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10584 > TMPL_ARGS_DEPTH (args
))
10586 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10587 args
, tf_warning_or_error
);
10588 location_t saved_input_location
= input_location
;
10589 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10590 tree cons
= get_constraints (tmpl
);
10591 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10592 input_location
= saved_input_location
;
10597 /* The friend template has not already been declared. In this
10598 case, the instantiation of the template class will cause the
10599 injection of this template into the namespace scope. */
10600 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10602 if (tmpl
!= error_mark_node
)
10604 /* The new TMPL is not an instantiation of anything, so we
10605 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10606 for the new type because that is supposed to be the
10607 corresponding template decl, i.e., TMPL. */
10608 DECL_USE_TEMPLATE (tmpl
) = 0;
10609 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10610 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10611 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10612 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10614 /* It is hidden. */
10615 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10616 DECL_ANTICIPATED (tmpl
)
10617 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10619 /* Inject this template into the enclosing namspace scope. */
10620 tmpl
= pushdecl_namespace_level (tmpl
, true);
10624 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10625 pop_nested_namespace (context
);
10627 pop_nested_class ();
10629 return TREE_TYPE (tmpl
);
10632 /* Returns zero if TYPE cannot be completed later due to circularity.
10633 Otherwise returns one. */
10636 can_complete_type_without_circularity (tree type
)
10638 if (type
== NULL_TREE
|| type
== error_mark_node
)
10640 else if (COMPLETE_TYPE_P (type
))
10642 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10643 return can_complete_type_without_circularity (TREE_TYPE (type
));
10644 else if (CLASS_TYPE_P (type
)
10645 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10651 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10652 tsubst_flags_t
, tree
);
10654 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10655 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10658 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10659 tsubst_flags_t complain
, tree in_decl
)
10661 gcc_assert (ATTR_IS_DEPENDENT (t
));
10663 tree val
= TREE_VALUE (t
);
10664 if (val
== NULL_TREE
)
10665 /* Nothing to do. */;
10666 else if ((flag_openmp
|| flag_openmp_simd
)
10667 && is_attribute_p ("omp declare simd",
10668 get_attribute_name (t
)))
10670 tree clauses
= TREE_VALUE (val
);
10671 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10672 complain
, in_decl
);
10673 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10674 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10675 tree parms
= DECL_ARGUMENTS (*decl_p
);
10677 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10679 val
= build_tree_list (NULL_TREE
, clauses
);
10683 /* If the first attribute argument is an identifier, don't
10684 pass it through tsubst. Attributes like mode, format,
10685 cleanup and several target specific attributes expect it
10687 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10690 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10691 /*integral_constant_expression_p=*/false);
10692 if (chain
!= TREE_CHAIN (val
))
10693 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10695 else if (PACK_EXPANSION_P (val
))
10697 /* An attribute pack expansion. */
10698 tree purp
= TREE_PURPOSE (t
);
10699 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10700 if (pack
== error_mark_node
)
10701 return error_mark_node
;
10702 int len
= TREE_VEC_LENGTH (pack
);
10703 tree list
= NULL_TREE
;
10705 for (int i
= 0; i
< len
; ++i
)
10707 tree elt
= TREE_VEC_ELT (pack
, i
);
10708 *q
= build_tree_list (purp
, elt
);
10709 q
= &TREE_CHAIN (*q
);
10714 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10715 /*integral_constant_expression_p=*/false);
10717 if (val
!= TREE_VALUE (t
))
10718 return build_tree_list (TREE_PURPOSE (t
), val
);
10722 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10723 unchanged or a new TREE_LIST chain. */
10726 tsubst_attributes (tree attributes
, tree args
,
10727 tsubst_flags_t complain
, tree in_decl
)
10729 tree last_dep
= NULL_TREE
;
10731 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10732 if (ATTR_IS_DEPENDENT (t
))
10735 attributes
= copy_list (attributes
);
10740 for (tree
*p
= &attributes
; *p
; )
10743 if (ATTR_IS_DEPENDENT (t
))
10745 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10750 p
= &TREE_CHAIN (*p
);
10751 *p
= TREE_CHAIN (t
);
10755 p
= &TREE_CHAIN (*p
);
10761 /* Apply any attributes which had to be deferred until instantiation
10762 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10763 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10766 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10767 tree args
, tsubst_flags_t complain
, tree in_decl
)
10769 tree last_dep
= NULL_TREE
;
10773 if (attributes
== NULL_TREE
)
10776 if (DECL_P (*decl_p
))
10778 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10780 p
= &DECL_ATTRIBUTES (*decl_p
);
10781 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10782 to our attributes parameter. */
10783 gcc_assert (*p
== attributes
);
10787 p
= &TYPE_ATTRIBUTES (*decl_p
);
10788 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10789 lookup_template_class_1, and should be preserved. */
10790 gcc_assert (*p
!= attributes
);
10792 p
= &TREE_CHAIN (*p
);
10795 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10796 if (ATTR_IS_DEPENDENT (t
))
10799 attributes
= copy_list (attributes
);
10806 tree late_attrs
= NULL_TREE
;
10807 tree
*q
= &late_attrs
;
10812 if (ATTR_IS_DEPENDENT (t
))
10814 *p
= TREE_CHAIN (t
);
10815 TREE_CHAIN (t
) = NULL_TREE
;
10816 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10818 q
= &TREE_CHAIN (*q
);
10821 p
= &TREE_CHAIN (t
);
10824 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10828 /* Perform (or defer) access check for typedefs that were referenced
10829 from within the template TMPL code.
10830 This is a subroutine of instantiate_decl and instantiate_class_template.
10831 TMPL is the template to consider and TARGS is the list of arguments of
10835 perform_typedefs_access_check (tree tmpl
, tree targs
)
10837 location_t saved_location
;
10839 qualified_typedef_usage_t
*iter
;
10842 || (!CLASS_TYPE_P (tmpl
)
10843 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10846 saved_location
= input_location
;
10847 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10849 tree type_decl
= iter
->typedef_decl
;
10850 tree type_scope
= iter
->context
;
10852 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10855 if (uses_template_parms (type_decl
))
10856 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10857 if (uses_template_parms (type_scope
))
10858 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10860 /* Make access check error messages point to the location
10861 of the use of the typedef. */
10862 input_location
= iter
->locus
;
10863 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10864 type_decl
, type_decl
,
10865 tf_warning_or_error
);
10867 input_location
= saved_location
;
10871 instantiate_class_template_1 (tree type
)
10873 tree templ
, args
, pattern
, t
, member
;
10877 unsigned int saved_maximum_field_alignment
;
10880 if (type
== error_mark_node
)
10881 return error_mark_node
;
10883 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10884 || uses_template_parms (type
))
10887 /* Figure out which template is being instantiated. */
10888 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10889 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10891 /* Mark the type as in the process of being defined. */
10892 TYPE_BEING_DEFINED (type
) = 1;
10894 /* We may be in the middle of deferred access check. Disable
10896 deferring_access_check_sentinel
acs (dk_no_deferred
);
10898 /* Determine what specialization of the original template to
10900 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10901 if (t
== error_mark_node
)
10902 return error_mark_node
;
10905 /* This TYPE is actually an instantiation of a partial
10906 specialization. We replace the innermost set of ARGS with
10907 the arguments appropriate for substitution. For example,
10910 template <class T> struct S {};
10911 template <class T> struct S<T*> {};
10913 and supposing that we are instantiating S<int*>, ARGS will
10914 presently be {int*} -- but we need {int}. */
10915 pattern
= TREE_TYPE (t
);
10916 args
= TREE_PURPOSE (t
);
10920 pattern
= TREE_TYPE (templ
);
10921 args
= CLASSTYPE_TI_ARGS (type
);
10924 /* If the template we're instantiating is incomplete, then clearly
10925 there's nothing we can do. */
10926 if (!COMPLETE_TYPE_P (pattern
))
10928 /* We can try again later. */
10929 TYPE_BEING_DEFINED (type
) = 0;
10933 /* If we've recursively instantiated too many templates, stop. */
10934 if (! push_tinst_level (type
))
10937 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10938 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10940 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10941 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10942 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10943 fn_context
= error_mark_node
;
10945 push_to_top_level ();
10948 cp_unevaluated_operand
= 0;
10949 c_inhibit_evaluation_warnings
= 0;
10951 /* Use #pragma pack from the template context. */
10952 saved_maximum_field_alignment
= maximum_field_alignment
;
10953 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10955 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10957 /* Set the input location to the most specialized template definition.
10958 This is needed if tsubsting causes an error. */
10959 typedecl
= TYPE_MAIN_DECL (pattern
);
10960 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10961 DECL_SOURCE_LOCATION (typedecl
);
10963 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10964 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10965 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10966 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10967 if (ANON_AGGR_TYPE_P (pattern
))
10968 SET_ANON_AGGR_TYPE_P (type
);
10969 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10971 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10972 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10973 /* Adjust visibility for template arguments. */
10974 determine_visibility (TYPE_MAIN_DECL (type
));
10976 if (CLASS_TYPE_P (type
))
10977 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10979 pbinfo
= TYPE_BINFO (pattern
);
10981 /* We should never instantiate a nested class before its enclosing
10982 class; we need to look up the nested class by name before we can
10983 instantiate it, and that lookup should instantiate the enclosing
10985 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10986 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10988 base_list
= NULL_TREE
;
10989 if (BINFO_N_BASE_BINFOS (pbinfo
))
10995 /* We must enter the scope containing the type, as that is where
10996 the accessibility of types named in dependent bases are
10998 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11000 /* Substitute into each of the bases to determine the actual
11002 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11005 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11006 tree expanded_bases
= NULL_TREE
;
11009 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11012 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11013 args
, tf_error
, NULL_TREE
);
11014 if (expanded_bases
== error_mark_node
)
11017 len
= TREE_VEC_LENGTH (expanded_bases
);
11020 for (idx
= 0; idx
< len
; idx
++)
11022 if (expanded_bases
)
11023 /* Extract the already-expanded base class. */
11024 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11026 /* Substitute to figure out the base class. */
11027 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11030 if (base
== error_mark_node
)
11033 base_list
= tree_cons (access
, base
, base_list
);
11034 if (BINFO_VIRTUAL_P (pbase_binfo
))
11035 TREE_TYPE (base_list
) = integer_type_node
;
11039 /* The list is now in reverse order; correct that. */
11040 base_list
= nreverse (base_list
);
11043 pop_scope (pushed_scope
);
11045 /* Now call xref_basetypes to set up all the base-class
11047 xref_basetypes (type
, base_list
);
11049 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11050 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11051 args
, tf_error
, NULL_TREE
);
11052 fixup_attribute_variants (type
);
11054 /* Now that our base classes are set up, enter the scope of the
11055 class, so that name lookups into base classes, etc. will work
11056 correctly. This is precisely analogous to what we do in
11057 begin_class_definition when defining an ordinary non-template
11058 class, except we also need to push the enclosing classes. */
11059 push_nested_class (type
);
11061 /* Now members are processed in the order of declaration. */
11062 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11063 member
; member
= TREE_CHAIN (member
))
11065 tree t
= TREE_VALUE (member
);
11067 if (TREE_PURPOSE (member
))
11071 if (LAMBDA_TYPE_P (t
))
11072 /* A closure type for a lambda in an NSDMI or default argument.
11073 Ignore it; it will be regenerated when needed. */
11076 /* Build new CLASSTYPE_NESTED_UTDS. */
11079 bool class_template_p
;
11081 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11082 && TYPE_LANG_SPECIFIC (t
)
11083 && CLASSTYPE_IS_TEMPLATE (t
));
11084 /* If the member is a class template, then -- even after
11085 substitution -- there may be dependent types in the
11086 template argument list for the class. We increment
11087 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11088 that function will assume that no types are dependent
11089 when outside of a template. */
11090 if (class_template_p
)
11091 ++processing_template_decl
;
11092 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11093 if (class_template_p
)
11094 --processing_template_decl
;
11095 if (newtag
== error_mark_node
)
11098 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11100 tree name
= TYPE_IDENTIFIER (t
);
11102 if (class_template_p
)
11103 /* Unfortunately, lookup_template_class sets
11104 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11105 instantiation (i.e., for the type of a member
11106 template class nested within a template class.)
11107 This behavior is required for
11108 maybe_process_partial_specialization to work
11109 correctly, but is not accurate in this case;
11110 the TAG is not an instantiation of anything.
11111 (The corresponding TEMPLATE_DECL is an
11112 instantiation, but the TYPE is not.) */
11113 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11115 /* Now, we call pushtag to put this NEWTAG into the scope of
11116 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11117 pushtag calling push_template_decl. We don't have to do
11118 this for enums because it will already have been done in
11121 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11122 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11125 else if (DECL_DECLARES_FUNCTION_P (t
))
11129 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11130 ++processing_template_decl
;
11131 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11132 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11133 --processing_template_decl
;
11134 set_current_access_from_decl (r
);
11135 finish_member_declaration (r
);
11136 /* Instantiate members marked with attribute used. */
11137 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11139 if (TREE_CODE (r
) == FUNCTION_DECL
11140 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11141 cp_check_omp_declare_reduction (r
);
11143 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11144 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11145 /* A closure type for a lambda in an NSDMI or default argument.
11146 Ignore it; it will be regenerated when needed. */;
11149 /* Build new TYPE_FIELDS. */
11150 if (TREE_CODE (t
) == STATIC_ASSERT
)
11154 ++c_inhibit_evaluation_warnings
;
11156 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11157 tf_warning_or_error
, NULL_TREE
,
11158 /*integral_constant_expression_p=*/true);
11159 --c_inhibit_evaluation_warnings
;
11161 finish_static_assert (condition
,
11162 STATIC_ASSERT_MESSAGE (t
),
11163 STATIC_ASSERT_SOURCE_LOCATION (t
),
11164 /*member_p=*/true);
11166 else if (TREE_CODE (t
) != CONST_DECL
)
11169 tree vec
= NULL_TREE
;
11172 /* The file and line for this declaration, to
11173 assist in error message reporting. Since we
11174 called push_tinst_level above, we don't need to
11176 input_location
= DECL_SOURCE_LOCATION (t
);
11178 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11179 ++processing_template_decl
;
11180 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11181 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11182 --processing_template_decl
;
11184 if (TREE_CODE (r
) == TREE_VEC
)
11186 /* A capture pack became multiple fields. */
11188 len
= TREE_VEC_LENGTH (vec
);
11191 for (int i
= 0; i
< len
; ++i
)
11194 r
= TREE_VEC_ELT (vec
, i
);
11199 [t]he initialization (and any associated
11200 side-effects) of a static data member does
11201 not occur unless the static data member is
11202 itself used in a way that requires the
11203 definition of the static data member to
11206 Therefore, we do not substitute into the
11207 initialized for the static data member here. */
11208 finish_static_data_member_decl
11210 /*init=*/NULL_TREE
,
11211 /*init_const_expr_p=*/false,
11212 /*asmspec_tree=*/NULL_TREE
,
11214 /* Instantiate members marked with attribute used. */
11215 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11218 else if (TREE_CODE (r
) == FIELD_DECL
)
11220 /* Determine whether R has a valid type and can be
11221 completed later. If R is invalid, then its type
11222 is replaced by error_mark_node. */
11223 tree rtype
= TREE_TYPE (r
);
11224 if (can_complete_type_without_circularity (rtype
))
11225 complete_type (rtype
);
11227 if (!complete_or_array_type_p (rtype
))
11229 /* If R's type couldn't be completed and
11230 it isn't a flexible array member (whose
11231 type is incomplete by definition) give
11233 cxx_incomplete_type_error (r
, rtype
);
11234 TREE_TYPE (r
) = error_mark_node
;
11236 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11237 && TYPE_DOMAIN (rtype
) == NULL_TREE
11238 && (TREE_CODE (type
) == UNION_TYPE
11239 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11241 error ("flexible array member %qD in union", r
);
11242 TREE_TYPE (r
) = error_mark_node
;
11246 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11247 such a thing will already have been added to the field
11248 list by tsubst_enum in finish_member_declaration in the
11249 CLASSTYPE_NESTED_UTDS case above. */
11250 if (!(TREE_CODE (r
) == TYPE_DECL
11251 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11252 && DECL_ARTIFICIAL (r
)))
11254 set_current_access_from_decl (r
);
11255 finish_member_declaration (r
);
11263 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11264 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11266 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11268 tree friend_type
= t
;
11269 bool adjust_processing_template_decl
= false;
11271 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11273 /* template <class T> friend class C; */
11274 friend_type
= tsubst_friend_class (friend_type
, args
);
11275 adjust_processing_template_decl
= true;
11277 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11279 /* template <class T> friend class C::D; */
11280 friend_type
= tsubst (friend_type
, args
,
11281 tf_warning_or_error
, NULL_TREE
);
11282 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11283 friend_type
= TREE_TYPE (friend_type
);
11284 adjust_processing_template_decl
= true;
11286 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11287 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11289 /* This could be either
11293 when dependent_type_p is false or
11295 template <class U> friend class T::C;
11298 /* Bump processing_template_decl in case this is something like
11299 template <class T> friend struct A<T>::B. */
11300 ++processing_template_decl
;
11301 friend_type
= tsubst (friend_type
, args
,
11302 tf_warning_or_error
, NULL_TREE
);
11303 if (dependent_type_p (friend_type
))
11304 adjust_processing_template_decl
= true;
11305 --processing_template_decl
;
11307 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11308 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11309 && TYPE_HIDDEN_P (friend_type
))
11313 where C hasn't been declared yet. Let's lookup name
11314 from namespace scope directly, bypassing any name that
11315 come from dependent base class. */
11316 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11318 /* The call to xref_tag_from_type does injection for friend
11320 push_nested_namespace (ns
);
11322 xref_tag_from_type (friend_type
, NULL_TREE
,
11323 /*tag_scope=*/ts_current
);
11324 pop_nested_namespace (ns
);
11326 else if (uses_template_parms (friend_type
))
11327 /* friend class C<T>; */
11328 friend_type
= tsubst (friend_type
, args
,
11329 tf_warning_or_error
, NULL_TREE
);
11334 where C is already declared or
11336 friend class C<int>;
11338 We don't have to do anything in these cases. */
11340 if (adjust_processing_template_decl
)
11341 /* Trick make_friend_class into realizing that the friend
11342 we're adding is a template, not an ordinary class. It's
11343 important that we use make_friend_class since it will
11344 perform some error-checking and output cross-reference
11346 ++processing_template_decl
;
11348 if (friend_type
!= error_mark_node
)
11349 make_friend_class (type
, friend_type
, /*complain=*/false);
11351 if (adjust_processing_template_decl
)
11352 --processing_template_decl
;
11356 /* Build new DECL_FRIENDLIST. */
11359 /* The file and line for this declaration, to
11360 assist in error message reporting. Since we
11361 called push_tinst_level above, we don't need to
11363 input_location
= DECL_SOURCE_LOCATION (t
);
11365 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11367 ++processing_template_decl
;
11368 push_deferring_access_checks (dk_no_check
);
11371 r
= tsubst_friend_function (t
, args
);
11372 add_friend (type
, r
, /*complain=*/false);
11373 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11375 pop_deferring_access_checks ();
11376 --processing_template_decl
;
11384 /* Restore these before substituting into the lambda capture
11386 cp_unevaluated_operand
= saved_unevaluated_operand
;
11387 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11390 /* Set the file and line number information to whatever is given for
11391 the class itself. This puts error messages involving generated
11392 implicit functions at a predictable point, and the same point
11393 that would be used for non-template classes. */
11394 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11396 unreverse_member_declarations (type
);
11397 finish_struct_1 (type
);
11398 TYPE_BEING_DEFINED (type
) = 0;
11400 /* We don't instantiate default arguments for member functions. 14.7.1:
11402 The implicit instantiation of a class template specialization causes
11403 the implicit instantiation of the declarations, but not of the
11404 definitions or default arguments, of the class member functions,
11405 member classes, static data members and member templates.... */
11407 /* Some typedefs referenced from within the template code need to be access
11408 checked at template instantiation time, i.e now. These types were
11409 added to the template at parsing time. Let's get those and perform
11410 the access checks then. */
11411 perform_typedefs_access_check (pattern
, args
);
11412 perform_deferred_access_checks (tf_warning_or_error
);
11413 pop_nested_class ();
11414 maximum_field_alignment
= saved_maximum_field_alignment
;
11416 pop_from_top_level ();
11417 pop_tinst_level ();
11419 /* The vtable for a template class can be emitted in any translation
11420 unit in which the class is instantiated. When there is no key
11421 method, however, finish_struct_1 will already have added TYPE to
11422 the keyed_classes. */
11423 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11424 vec_safe_push (keyed_classes
, type
);
11429 /* Wrapper for instantiate_class_template_1. */
11432 instantiate_class_template (tree type
)
11435 timevar_push (TV_TEMPLATE_INST
);
11436 ret
= instantiate_class_template_1 (type
);
11437 timevar_pop (TV_TEMPLATE_INST
);
11442 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11448 else if (TYPE_P (t
))
11449 r
= tsubst (t
, args
, complain
, in_decl
);
11452 if (!(complain
& tf_warning
))
11453 ++c_inhibit_evaluation_warnings
;
11454 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11455 /*integral_constant_expression_p=*/true);
11456 if (!(complain
& tf_warning
))
11457 --c_inhibit_evaluation_warnings
;
11462 /* Given a function parameter pack TMPL_PARM and some function parameters
11463 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11464 and set *SPEC_P to point at the next point in the list. */
11467 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11469 /* Collect all of the extra "packed" parameters into an
11472 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11473 tree spec_parm
= *spec_p
;
11476 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11478 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11481 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11482 parmvec
= make_tree_vec (len
);
11483 spec_parm
= *spec_p
;
11484 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11486 tree elt
= spec_parm
;
11487 if (DECL_PACK_P (elt
))
11488 elt
= make_pack_expansion (elt
);
11489 TREE_VEC_ELT (parmvec
, i
) = elt
;
11492 /* Build the argument packs. */
11493 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11494 *spec_p
= spec_parm
;
11499 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11500 NONTYPE_ARGUMENT_PACK. */
11503 make_fnparm_pack (tree spec_parm
)
11505 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11508 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11509 pack expansion with no extra args, 2 if it has extra args, or 0
11510 if it is not a pack expansion. */
11513 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11515 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11516 if (i
>= TREE_VEC_LENGTH (vec
))
11518 tree elt
= TREE_VEC_ELT (vec
, i
);
11520 /* A decl pack is itself an expansion. */
11521 elt
= TREE_TYPE (elt
);
11522 if (!PACK_EXPANSION_P (elt
))
11524 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11530 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11533 make_argument_pack_select (tree arg_pack
, unsigned index
)
11535 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11537 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11538 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11543 /* This is a subroutine of tsubst_pack_expansion.
11545 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11546 mechanism to store the (non complete list of) arguments of the
11547 substitution and return a non substituted pack expansion, in order
11548 to wait for when we have enough arguments to really perform the
11552 use_pack_expansion_extra_args_p (tree parm_packs
,
11554 bool has_empty_arg
)
11556 /* If one pack has an expansion and another pack has a normal
11557 argument or if one pack has an empty argument and an another
11558 one hasn't then tsubst_pack_expansion cannot perform the
11559 substitution and need to fall back on the
11560 PACK_EXPANSION_EXTRA mechanism. */
11561 if (parm_packs
== NULL_TREE
)
11563 else if (has_empty_arg
)
11566 bool has_expansion_arg
= false;
11567 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11569 bool has_non_expansion_arg
= false;
11570 for (tree parm_pack
= parm_packs
;
11572 parm_pack
= TREE_CHAIN (parm_pack
))
11574 tree arg
= TREE_VALUE (parm_pack
);
11576 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11578 /* We can't substitute a pack expansion with extra args into
11582 has_expansion_arg
= true;
11584 has_non_expansion_arg
= true;
11587 if (has_expansion_arg
&& has_non_expansion_arg
)
11593 /* [temp.variadic]/6 says that:
11595 The instantiation of a pack expansion [...]
11596 produces a list E1,E2, ..., En, where N is the number of elements
11597 in the pack expansion parameters.
11599 This subroutine of tsubst_pack_expansion produces one of these Ei.
11601 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11602 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11603 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11604 INDEX is the index 'i' of the element Ei to produce. ARGS,
11605 COMPLAIN, and IN_DECL are the same parameters as for the
11606 tsubst_pack_expansion function.
11608 The function returns the resulting Ei upon successful completion,
11609 or error_mark_node.
11611 Note that this function possibly modifies the ARGS parameter, so
11612 it's the responsibility of the caller to restore it. */
11615 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11618 tree args
/* This parm gets
11620 tsubst_flags_t complain
,
11624 bool ith_elem_is_expansion
= false;
11626 /* For each parameter pack, change the substitution of the parameter
11627 pack to the ith argument in its argument pack, then expand the
11629 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11631 tree parm
= TREE_PURPOSE (pack
);
11632 tree arg_pack
= TREE_VALUE (pack
);
11633 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11635 ith_elem_is_expansion
|=
11636 argument_pack_element_is_expansion_p (arg_pack
, index
);
11638 /* Select the Ith argument from the pack. */
11639 if (TREE_CODE (parm
) == PARM_DECL
11641 || TREE_CODE (parm
) == FIELD_DECL
)
11645 aps
= make_argument_pack_select (arg_pack
, index
);
11646 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11647 return error_mark_node
;
11648 register_local_specialization (aps
, parm
);
11651 aps
= retrieve_local_specialization (parm
);
11656 template_parm_level_and_index (parm
, &level
, &idx
);
11660 aps
= make_argument_pack_select (arg_pack
, index
);
11661 /* Update the corresponding argument. */
11662 TMPL_ARG (args
, level
, idx
) = aps
;
11665 /* Re-use the ARGUMENT_PACK_SELECT. */
11666 aps
= TMPL_ARG (args
, level
, idx
);
11668 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11671 /* Substitute into the PATTERN with the (possibly altered)
11673 if (pattern
== in_decl
)
11674 /* Expanding a fixed parameter pack from
11675 coerce_template_parameter_pack. */
11676 t
= tsubst_decl (pattern
, args
, complain
);
11677 else if (pattern
== error_mark_node
)
11678 t
= error_mark_node
;
11679 else if (constraint_p (pattern
))
11681 if (processing_template_decl
)
11682 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11684 t
= (constraints_satisfied_p (pattern
, args
)
11685 ? boolean_true_node
: boolean_false_node
);
11687 else if (!TYPE_P (pattern
))
11688 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11689 /*integral_constant_expression_p=*/false);
11691 t
= tsubst (pattern
, args
, complain
, in_decl
);
11693 /* If the Ith argument pack element is a pack expansion, then
11694 the Ith element resulting from the substituting is going to
11695 be a pack expansion as well. */
11696 if (ith_elem_is_expansion
)
11697 t
= make_pack_expansion (t
, complain
);
11702 /* When the unexpanded parameter pack in a fold expression expands to an empty
11703 sequence, the value of the expression is as follows; the program is
11704 ill-formed if the operator is not listed in this table.
11711 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11713 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11714 if (!FOLD_EXPR_MODIFY_P (t
))
11717 case TRUTH_ANDIF_EXPR
:
11718 return boolean_true_node
;
11719 case TRUTH_ORIF_EXPR
:
11720 return boolean_false_node
;
11721 case COMPOUND_EXPR
:
11727 if (complain
& tf_error
)
11728 error_at (location_of (t
),
11729 "fold of empty expansion over %O", code
);
11730 return error_mark_node
;
11733 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11734 form an expression that combines the two terms using the
11738 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11740 tree op
= FOLD_EXPR_OP (t
);
11741 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11743 // Handle compound assignment operators.
11744 if (FOLD_EXPR_MODIFY_P (t
))
11745 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11749 case COMPOUND_EXPR
:
11750 return build_x_compound_expr (input_location
, left
, right
, complain
);
11752 return build_m_component_ref (left
, right
, complain
);
11754 return build_x_binary_op (input_location
, code
,
11755 left
, TREE_CODE (left
),
11756 right
, TREE_CODE (right
),
11762 /* Substitute ARGS into the pack of a fold expression T. */
11765 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11767 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11770 /* Substitute ARGS into the pack of a fold expression T. */
11773 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11775 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11778 /* Expand a PACK of arguments into a grouped as left fold.
11779 Given a pack containing elements A0, A1, ..., An and an
11780 operator @, this builds the expression:
11782 ((A0 @ A1) @ A2) ... @ An
11784 Note that PACK must not be empty.
11786 The operator is defined by the original fold expression T. */
11789 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11791 tree left
= TREE_VEC_ELT (pack
, 0);
11792 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11794 tree right
= TREE_VEC_ELT (pack
, i
);
11795 left
= fold_expression (t
, left
, right
, complain
);
11800 /* Substitute into a unary left fold expression. */
11803 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11806 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11807 if (pack
== error_mark_node
)
11808 return error_mark_node
;
11809 if (PACK_EXPANSION_P (pack
))
11811 tree r
= copy_node (t
);
11812 FOLD_EXPR_PACK (r
) = pack
;
11815 if (TREE_VEC_LENGTH (pack
) == 0)
11816 return expand_empty_fold (t
, complain
);
11818 return expand_left_fold (t
, pack
, complain
);
11821 /* Substitute into a binary left fold expression.
11823 Do ths by building a single (non-empty) vector of argumnts and
11824 building the expression from those elements. */
11827 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11830 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11831 if (pack
== error_mark_node
)
11832 return error_mark_node
;
11833 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11834 if (init
== error_mark_node
)
11835 return error_mark_node
;
11837 if (PACK_EXPANSION_P (pack
))
11839 tree r
= copy_node (t
);
11840 FOLD_EXPR_PACK (r
) = pack
;
11841 FOLD_EXPR_INIT (r
) = init
;
11845 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11846 TREE_VEC_ELT (vec
, 0) = init
;
11847 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11848 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11850 return expand_left_fold (t
, vec
, complain
);
11853 /* Expand a PACK of arguments into a grouped as right fold.
11854 Given a pack containing elementns A0, A1, ..., and an
11855 operator @, this builds the expression:
11857 A0@ ... (An-2 @ (An-1 @ An))
11859 Note that PACK must not be empty.
11861 The operator is defined by the original fold expression T. */
11864 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11866 // Build the expression.
11867 int n
= TREE_VEC_LENGTH (pack
);
11868 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11869 for (--n
; n
!= 0; --n
)
11871 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11872 right
= fold_expression (t
, left
, right
, complain
);
11877 /* Substitute into a unary right fold expression. */
11880 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11883 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11884 if (pack
== error_mark_node
)
11885 return error_mark_node
;
11886 if (PACK_EXPANSION_P (pack
))
11888 tree r
= copy_node (t
);
11889 FOLD_EXPR_PACK (r
) = pack
;
11892 if (TREE_VEC_LENGTH (pack
) == 0)
11893 return expand_empty_fold (t
, complain
);
11895 return expand_right_fold (t
, pack
, complain
);
11898 /* Substitute into a binary right fold expression.
11900 Do ths by building a single (non-empty) vector of arguments and
11901 building the expression from those elements. */
11904 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11907 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11908 if (pack
== error_mark_node
)
11909 return error_mark_node
;
11910 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11911 if (init
== error_mark_node
)
11912 return error_mark_node
;
11914 if (PACK_EXPANSION_P (pack
))
11916 tree r
= copy_node (t
);
11917 FOLD_EXPR_PACK (r
) = pack
;
11918 FOLD_EXPR_INIT (r
) = init
;
11922 int n
= TREE_VEC_LENGTH (pack
);
11923 tree vec
= make_tree_vec (n
+ 1);
11924 for (int i
= 0; i
< n
; ++i
)
11925 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11926 TREE_VEC_ELT (vec
, n
) = init
;
11928 return expand_right_fold (t
, vec
, complain
);
11931 /* Walk through the pattern of a pack expansion, adding everything in
11932 local_specializations to a list. */
11936 hash_set
<tree
> internal
;
11938 tsubst_flags_t complain
;
11940 el_data (tsubst_flags_t c
)
11941 : extra (NULL_TREE
), complain (c
) {}
11944 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11946 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11947 tree
*extra
= &data
.extra
;
11948 tsubst_flags_t complain
= data
.complain
;
11950 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
11951 /* Remember local typedefs (85214). */
11952 tp
= &TYPE_NAME (*tp
);
11954 if (TREE_CODE (*tp
) == DECL_EXPR
)
11955 data
.internal
.add (DECL_EXPR_DECL (*tp
));
11956 else if (tree spec
= retrieve_local_specialization (*tp
))
11958 if (data
.internal
.contains (*tp
))
11959 /* Don't mess with variables declared within the pattern. */
11961 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11963 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11964 tree args
= ARGUMENT_PACK_ARGS (spec
);
11965 if (TREE_VEC_LENGTH (args
) == 1)
11967 tree elt
= TREE_VEC_ELT (args
, 0);
11968 if (PACK_EXPANSION_P (elt
))
11969 elt
= PACK_EXPANSION_PATTERN (elt
);
11970 if (DECL_PACK_P (elt
))
11973 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11975 /* Handle lambda capture here, since we aren't doing any
11976 substitution now, and so tsubst_copy won't call
11977 process_outer_var_ref. */
11978 tree args
= ARGUMENT_PACK_ARGS (spec
);
11979 int len
= TREE_VEC_LENGTH (args
);
11980 for (int i
= 0; i
< len
; ++i
)
11982 tree arg
= TREE_VEC_ELT (args
, i
);
11984 if (outer_automatic_var_p (arg
))
11985 carg
= process_outer_var_ref (arg
, complain
);
11988 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11992 spec
= copy_node (spec
);
11993 args
= copy_node (args
);
11994 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11995 register_local_specialization (spec
, *tp
);
11997 TREE_VEC_ELT (args
, i
) = carg
;
12002 if (outer_automatic_var_p (spec
))
12003 spec
= process_outer_var_ref (spec
, complain
);
12004 *extra
= tree_cons (*tp
, spec
, *extra
);
12009 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12011 el_data
data (complain
);
12012 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12016 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12017 for use in PACK_EXPANSION_EXTRA_ARGS. */
12020 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12023 if (local_specializations
)
12024 if (tree locals
= extract_local_specs (pattern
, complain
))
12025 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12029 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12030 normal template args to ARGS. */
12033 add_extra_args (tree extra
, tree args
)
12035 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12037 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12039 /* The partial instantiation involved local declarations collected in
12040 extract_local_specs; map from the general template to our local
12042 tree gen
= TREE_PURPOSE (elt
);
12043 tree inst
= TREE_VALUE (elt
);
12045 if (tree local
= retrieve_local_specialization (inst
))
12047 /* else inst is already a full instantiation of the pack. */
12048 register_local_specialization (inst
, gen
);
12050 gcc_assert (!TREE_PURPOSE (extra
));
12051 extra
= TREE_VALUE (extra
);
12053 return add_to_template_args (extra
, args
);
12056 /* Substitute ARGS into T, which is an pack expansion
12057 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12058 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12059 (if only a partial substitution could be performed) or
12060 ERROR_MARK_NODE if there was an error. */
12062 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12066 tree pack
, packs
= NULL_TREE
;
12067 bool unsubstituted_packs
= false;
12068 bool unsubstituted_fn_pack
= false;
12071 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12072 bool need_local_specializations
= false;
12075 gcc_assert (PACK_EXPANSION_P (t
));
12076 pattern
= PACK_EXPANSION_PATTERN (t
);
12078 /* Add in any args remembered from an earlier partial instantiation. */
12079 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12081 levels
= TMPL_ARGS_DEPTH (args
);
12083 /* Determine the argument packs that will instantiate the parameter
12084 packs used in the expansion expression. While we're at it,
12085 compute the number of arguments to be expanded and make sure it
12087 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12088 pack
= TREE_CHAIN (pack
))
12090 tree parm_pack
= TREE_VALUE (pack
);
12091 tree arg_pack
= NULL_TREE
;
12092 tree orig_arg
= NULL_TREE
;
12095 if (TREE_CODE (parm_pack
) == BASES
)
12097 gcc_assert (parm_pack
== pattern
);
12098 if (BASES_DIRECT (parm_pack
))
12099 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12104 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12105 args
, complain
, in_decl
,
12108 else if (builtin_pack_call_p (parm_pack
))
12110 if (parm_pack
!= pattern
)
12112 if (complain
& tf_error
)
12113 sorry ("%qE is not the entire pattern of the pack expansion",
12115 return error_mark_node
;
12117 return expand_builtin_pack_call (parm_pack
, args
,
12118 complain
, in_decl
);
12120 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12122 /* We know we have correct local_specializations if this
12123 expansion is at function scope, or if we're dealing with a
12124 local parameter in a requires expression; for the latter,
12125 tsubst_requires_expr set it up appropriately. */
12126 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12127 arg_pack
= retrieve_local_specialization (parm_pack
);
12129 /* We can't rely on local_specializations for a parameter
12130 name used later in a function declaration (such as in a
12131 late-specified return type). Even if it exists, it might
12132 have the wrong value for a recursive call. */
12133 need_local_specializations
= true;
12137 /* This parameter pack was used in an unevaluated context. Just
12138 make a dummy decl, since it's only used for its type. */
12139 ++cp_unevaluated_operand
;
12140 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12141 --cp_unevaluated_operand
;
12142 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12143 /* Partial instantiation of the parm_pack, we can't build
12144 up an argument pack yet. */
12145 arg_pack
= NULL_TREE
;
12147 arg_pack
= make_fnparm_pack (arg_pack
);
12149 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12150 /* This argument pack isn't fully instantiated yet. We set this
12151 flag rather than clear arg_pack because we do want to do the
12152 optimization below, and we don't want to substitute directly
12153 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12154 where it isn't expected). */
12155 unsubstituted_fn_pack
= true;
12157 else if (is_capture_proxy (parm_pack
))
12159 arg_pack
= retrieve_local_specialization (parm_pack
);
12160 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12161 unsubstituted_fn_pack
= true;
12166 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12168 if (level
<= levels
)
12169 arg_pack
= TMPL_ARG (args
, level
, idx
);
12172 orig_arg
= arg_pack
;
12173 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12174 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12176 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12177 /* This can only happen if we forget to expand an argument
12178 pack somewhere else. Just return an error, silently. */
12180 result
= make_tree_vec (1);
12181 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12188 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12190 /* Don't bother trying to do a partial substitution with
12191 incomplete packs; we'll try again after deduction. */
12192 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12197 else if (len
!= my_len
12198 && !unsubstituted_fn_pack
)
12200 if (!(complain
& tf_error
))
12201 /* Fail quietly. */;
12202 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12203 error ("mismatched argument pack lengths while expanding %qT",
12206 error ("mismatched argument pack lengths while expanding %qE",
12208 return error_mark_node
;
12211 /* Keep track of the parameter packs and their corresponding
12213 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12214 TREE_TYPE (packs
) = orig_arg
;
12218 /* We can't substitute for this parameter pack. We use a flag as
12219 well as the missing_level counter because function parameter
12220 packs don't have a level. */
12221 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12222 unsubstituted_packs
= true;
12226 /* If the expansion is just T..., return the matching argument pack, unless
12227 we need to call convert_from_reference on all the elements. This is an
12228 important optimization; see c++/68422. */
12229 if (!unsubstituted_packs
12230 && TREE_PURPOSE (packs
) == pattern
)
12232 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12234 /* If the argument pack is a single pack expansion, pull it out. */
12235 if (TREE_VEC_LENGTH (args
) == 1
12236 && pack_expansion_args_count (args
))
12237 return TREE_VEC_ELT (args
, 0);
12239 /* Types need no adjustment, nor does sizeof..., and if we still have
12240 some pack expansion args we won't do anything yet. */
12241 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12242 || PACK_EXPANSION_SIZEOF_P (t
)
12243 || pack_expansion_args_count (args
))
12245 /* Also optimize expression pack expansions if we can tell that the
12246 elements won't have reference type. */
12247 tree type
= TREE_TYPE (pattern
);
12248 if (type
&& !TYPE_REF_P (type
)
12249 && !PACK_EXPANSION_P (type
)
12250 && !WILDCARD_TYPE_P (type
))
12252 /* Otherwise use the normal path so we get convert_from_reference. */
12255 /* We cannot expand this expansion expression, because we don't have
12256 all of the argument packs we need. */
12257 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12259 /* We got some full packs, but we can't substitute them in until we
12260 have values for all the packs. So remember these until then. */
12262 t
= make_pack_expansion (pattern
, complain
);
12263 PACK_EXPANSION_EXTRA_ARGS (t
)
12264 = build_extra_args (pattern
, args
, complain
);
12267 else if (unsubstituted_packs
)
12269 /* There were no real arguments, we're just replacing a parameter
12270 pack with another version of itself. Substitute into the
12271 pattern and return a PACK_EXPANSION_*. The caller will need to
12273 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12274 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12275 /*integral_constant_expression_p=*/false);
12277 t
= tsubst (pattern
, args
, complain
, in_decl
);
12278 t
= make_pack_expansion (t
, complain
);
12282 gcc_assert (len
>= 0);
12284 if (need_local_specializations
)
12286 /* We're in a late-specified return type, so create our own local
12287 specializations map; the current map is either NULL or (in the
12288 case of recursive unification) might have bindings that we don't
12289 want to use or alter. */
12290 saved_local_specializations
= local_specializations
;
12291 local_specializations
= new hash_map
<tree
, tree
>;
12294 /* For each argument in each argument pack, substitute into the
12296 result
= make_tree_vec (len
);
12297 tree elem_args
= copy_template_args (args
);
12298 for (i
= 0; i
< len
; ++i
)
12300 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12302 elem_args
, complain
,
12304 TREE_VEC_ELT (result
, i
) = t
;
12305 if (t
== error_mark_node
)
12307 result
= error_mark_node
;
12312 /* Update ARGS to restore the substitution from parameter packs to
12313 their argument packs. */
12314 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12316 tree parm
= TREE_PURPOSE (pack
);
12318 if (TREE_CODE (parm
) == PARM_DECL
12320 || TREE_CODE (parm
) == FIELD_DECL
)
12321 register_local_specialization (TREE_TYPE (pack
), parm
);
12326 if (TREE_VALUE (pack
) == NULL_TREE
)
12329 template_parm_level_and_index (parm
, &level
, &idx
);
12331 /* Update the corresponding argument. */
12332 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12333 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12336 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12340 if (need_local_specializations
)
12342 delete local_specializations
;
12343 local_specializations
= saved_local_specializations
;
12346 /* If the dependent pack arguments were such that we end up with only a
12347 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12348 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12349 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12350 return TREE_VEC_ELT (result
, 0);
12355 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12356 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12357 parameter packs; all parms generated from a function parameter pack will
12358 have the same DECL_PARM_INDEX. */
12361 get_pattern_parm (tree parm
, tree tmpl
)
12363 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12366 if (DECL_ARTIFICIAL (parm
))
12368 for (patparm
= DECL_ARGUMENTS (pattern
);
12369 patparm
; patparm
= DECL_CHAIN (patparm
))
12370 if (DECL_ARTIFICIAL (patparm
)
12371 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12376 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12377 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12378 gcc_assert (DECL_PARM_INDEX (patparm
)
12379 == DECL_PARM_INDEX (parm
));
12385 /* Make an argument pack out of the TREE_VEC VEC. */
12388 make_argument_pack (tree vec
)
12391 tree elt
= TREE_VEC_ELT (vec
, 0);
12393 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12396 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12397 TREE_CONSTANT (pack
) = 1;
12399 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12403 /* Return an exact copy of template args T that can be modified
12407 copy_template_args (tree t
)
12409 if (t
== error_mark_node
)
12412 int len
= TREE_VEC_LENGTH (t
);
12413 tree new_vec
= make_tree_vec (len
);
12415 for (int i
= 0; i
< len
; ++i
)
12417 tree elt
= TREE_VEC_ELT (t
, i
);
12418 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12419 elt
= copy_template_args (elt
);
12420 TREE_VEC_ELT (new_vec
, i
) = elt
;
12423 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12424 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12429 /* Substitute ARGS into the vector or list of template arguments T. */
12432 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12435 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12438 if (t
== error_mark_node
)
12439 return error_mark_node
;
12441 len
= TREE_VEC_LENGTH (t
);
12442 elts
= XALLOCAVEC (tree
, len
);
12444 for (i
= 0; i
< len
; i
++)
12446 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12449 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12450 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12451 else if (PACK_EXPANSION_P (orig_arg
))
12453 /* Substitute into an expansion expression. */
12454 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12456 if (TREE_CODE (new_arg
) == TREE_VEC
)
12457 /* Add to the expanded length adjustment the number of
12458 expanded arguments. We subtract one from this
12459 measurement, because the argument pack expression
12460 itself is already counted as 1 in
12461 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12462 the argument pack is empty. */
12463 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12465 else if (ARGUMENT_PACK_P (orig_arg
))
12467 /* Substitute into each of the arguments. */
12468 new_arg
= TYPE_P (orig_arg
)
12469 ? cxx_make_type (TREE_CODE (orig_arg
))
12470 : make_node (TREE_CODE (orig_arg
));
12472 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12473 args
, complain
, in_decl
);
12474 if (pack_args
== error_mark_node
)
12475 new_arg
= error_mark_node
;
12477 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12479 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12480 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12483 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12485 if (new_arg
== error_mark_node
)
12486 return error_mark_node
;
12489 if (new_arg
!= orig_arg
)
12496 /* Make space for the expanded arguments coming from template
12498 t
= make_tree_vec (len
+ expanded_len_adjust
);
12499 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12500 arguments for a member template.
12501 In that case each TREE_VEC in ORIG_T represents a level of template
12502 arguments, and ORIG_T won't carry any non defaulted argument count.
12503 It will rather be the nested TREE_VECs that will carry one.
12504 In other words, ORIG_T carries a non defaulted argument count only
12505 if it doesn't contain any nested TREE_VEC. */
12506 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12508 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12509 count
+= expanded_len_adjust
;
12510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12512 for (i
= 0, out
= 0; i
< len
; i
++)
12514 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12515 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12516 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12520 /* Now expand the template argument pack "in place". */
12521 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12522 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12526 TREE_VEC_ELT (t
, out
) = elts
[i
];
12534 /* Substitute ARGS into one level PARMS of template parameters. */
12537 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12539 if (parms
== error_mark_node
)
12540 return error_mark_node
;
12542 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12544 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12546 tree tuple
= TREE_VEC_ELT (parms
, i
);
12548 if (tuple
== error_mark_node
)
12551 TREE_VEC_ELT (new_vec
, i
) =
12552 tsubst_template_parm (tuple
, args
, complain
);
12558 /* Return the result of substituting ARGS into the template parameters
12559 given by PARMS. If there are m levels of ARGS and m + n levels of
12560 PARMS, then the result will contain n levels of PARMS. For
12561 example, if PARMS is `template <class T> template <class U>
12562 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12563 result will be `template <int*, double, class V>'. */
12566 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12568 tree r
= NULL_TREE
;
12571 /* When substituting into a template, we must set
12572 PROCESSING_TEMPLATE_DECL as the template parameters may be
12573 dependent if they are based on one-another, and the dependency
12574 predicates are short-circuit outside of templates. */
12575 ++processing_template_decl
;
12577 for (new_parms
= &r
;
12578 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12579 new_parms
= &(TREE_CHAIN (*new_parms
)),
12580 parms
= TREE_CHAIN (parms
))
12582 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12585 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12586 - TMPL_ARGS_DEPTH (args
)),
12587 new_vec
, NULL_TREE
);
12590 --processing_template_decl
;
12595 /* Return the result of substituting ARGS into one template parameter
12596 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12597 parameter and which TREE_PURPOSE is the default argument of the
12598 template parameter. */
12601 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12603 tree default_value
, parm_decl
;
12605 if (args
== NULL_TREE
12607 || t
== error_mark_node
)
12610 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12612 default_value
= TREE_PURPOSE (t
);
12613 parm_decl
= TREE_VALUE (t
);
12615 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12616 if (TREE_CODE (parm_decl
) == PARM_DECL
12617 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12618 parm_decl
= error_mark_node
;
12619 default_value
= tsubst_template_arg (default_value
, args
,
12620 complain
, NULL_TREE
);
12622 return build_tree_list (default_value
, parm_decl
);
12625 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12626 type T. If T is not an aggregate or enumeration type, it is
12627 handled as if by tsubst. IN_DECL is as for tsubst. If
12628 ENTERING_SCOPE is nonzero, T is the context for a template which
12629 we are presently tsubst'ing. Return the substituted value. */
12632 tsubst_aggr_type (tree t
,
12634 tsubst_flags_t complain
,
12636 int entering_scope
)
12638 if (t
== NULL_TREE
)
12641 switch (TREE_CODE (t
))
12644 if (TYPE_PTRMEMFUNC_P (t
))
12645 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12647 /* Fall through. */
12648 case ENUMERAL_TYPE
:
12650 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12656 /* In "sizeof(X<I>)" we need to evaluate "I". */
12659 /* First, determine the context for the type we are looking
12661 context
= TYPE_CONTEXT (t
);
12662 if (context
&& TYPE_P (context
))
12664 context
= tsubst_aggr_type (context
, args
, complain
,
12665 in_decl
, /*entering_scope=*/1);
12666 /* If context is a nested class inside a class template,
12667 it may still need to be instantiated (c++/33959). */
12668 context
= complete_type (context
);
12671 /* Then, figure out what arguments are appropriate for the
12672 type we are trying to find. For example, given:
12674 template <class T> struct S;
12675 template <class T, class U> void f(T, U) { S<U> su; }
12677 and supposing that we are instantiating f<int, double>,
12678 then our ARGS will be {int, double}, but, when looking up
12679 S we only want {double}. */
12680 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12681 complain
, in_decl
);
12682 if (argvec
== error_mark_node
)
12683 r
= error_mark_node
;
12686 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12687 entering_scope
, complain
);
12688 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12694 /* This is not a template type, so there's nothing to do. */
12698 return tsubst (t
, args
, complain
, in_decl
);
12702 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12704 /* Substitute into the default argument ARG (a default argument for
12705 FN), which has the indicated TYPE. */
12708 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12709 tsubst_flags_t complain
)
12711 int errs
= errorcount
+ sorrycount
;
12713 /* This can happen in invalid code. */
12714 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12717 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12718 parm
= chain_index (parmnum
, parm
);
12719 tree parmtype
= TREE_TYPE (parm
);
12720 if (DECL_BY_REFERENCE (parm
))
12721 parmtype
= TREE_TYPE (parmtype
);
12722 if (parmtype
== error_mark_node
)
12723 return error_mark_node
;
12725 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12728 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12731 /* This default argument came from a template. Instantiate the
12732 default argument here, not in tsubst. In the case of
12741 we must be careful to do name lookup in the scope of S<T>,
12742 rather than in the current class. */
12743 push_to_top_level ();
12744 push_access_scope (fn
);
12745 start_lambda_scope (parm
);
12747 /* The default argument expression may cause implicitly defined
12748 member functions to be synthesized, which will result in garbage
12749 collection. We must treat this situation as if we were within
12750 the body of function so as to avoid collecting live data on the
12753 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12754 complain
, NULL_TREE
,
12755 /*integral_constant_expression_p=*/false);
12758 finish_lambda_scope ();
12760 /* Make sure the default argument is reasonable. */
12761 arg
= check_default_argument (type
, arg
, complain
);
12763 if (errorcount
+sorrycount
> errs
12764 && (complain
& tf_warning_or_error
))
12765 inform (input_location
,
12766 " when instantiating default argument for call to %qD", fn
);
12768 pop_access_scope (fn
);
12769 pop_from_top_level ();
12771 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12774 defarg_inst
= tree_cache_map::create_ggc (37);
12775 defarg_inst
->put (parm
, arg
);
12781 /* Substitute into all the default arguments for FN. */
12784 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12789 tmpl_args
= DECL_TI_ARGS (fn
);
12791 /* If this function is not yet instantiated, we certainly don't need
12792 its default arguments. */
12793 if (uses_template_parms (tmpl_args
))
12795 /* Don't do this again for clones. */
12796 if (DECL_CLONED_FUNCTION_P (fn
))
12800 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12802 arg
= TREE_CHAIN (arg
), ++i
)
12803 if (TREE_PURPOSE (arg
))
12804 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12806 TREE_PURPOSE (arg
),
12810 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12811 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
12813 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12816 store_explicit_specifier (tree v
, tree t
)
12818 if (!explicit_specifier_map
)
12819 explicit_specifier_map
= tree_cache_map::create_ggc (37);
12820 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
12821 explicit_specifier_map
->put (v
, t
);
12824 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12827 lookup_explicit_specifier (tree v
)
12829 return *explicit_specifier_map
->get (v
);
12832 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12835 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12836 tree lambda_fntype
)
12838 tree gen_tmpl
, argvec
;
12839 hashval_t hash
= 0;
12842 /* Nobody should be tsubst'ing into non-template functions. */
12843 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12845 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12847 /* If T is not dependent, just return it. */
12848 if (!uses_template_parms (DECL_TI_ARGS (t
))
12849 && !LAMBDA_FUNCTION_P (t
))
12852 /* Calculate the most general template of which R is a
12854 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12856 /* We're substituting a lambda function under tsubst_lambda_expr but not
12857 directly from it; find the matching function we're already inside.
12858 But don't do this if T is a generic lambda with a single level of
12859 template parms, as in that case we're doing a normal instantiation. */
12860 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12861 && (!generic_lambda_fn_p (t
)
12862 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12863 return enclosing_instantiation_of (t
);
12865 /* Calculate the complete set of arguments used to
12867 argvec
= tsubst_template_args (DECL_TI_ARGS
12868 (DECL_TEMPLATE_RESULT
12869 (DECL_TI_TEMPLATE (t
))),
12870 args
, complain
, in_decl
);
12871 if (argvec
== error_mark_node
)
12872 return error_mark_node
;
12874 /* Check to see if we already have this specialization. */
12875 if (!lambda_fntype
)
12877 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12878 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12882 /* We can see more levels of arguments than parameters if
12883 there was a specialization of a member template, like
12886 template <class T> struct S { template <class U> void f(); }
12887 template <> template <class U> void S<int>::f(U);
12889 Here, we'll be substituting into the specialization,
12890 because that's where we can find the code we actually
12891 want to generate, but we'll have enough arguments for
12892 the most general template.
12894 We also deal with the peculiar case:
12896 template <class T> struct S {
12897 template <class U> friend void f();
12899 template <class U> void f() {}
12901 template void f<double>();
12903 Here, the ARGS for the instantiation of will be {int,
12904 double}. But, we only need as many ARGS as there are
12905 levels of template parameters in CODE_PATTERN. We are
12906 careful not to get fooled into reducing the ARGS in
12909 template <class T> struct S { template <class U> void f(U); }
12910 template <class T> template <> void S<T>::f(int) {}
12912 which we can spot because the pattern will be a
12913 specialization in this case. */
12914 int args_depth
= TMPL_ARGS_DEPTH (args
);
12916 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12918 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12919 args
= get_innermost_template_args (args
, parms_depth
);
12923 /* This special case arises when we have something like this:
12925 template <class T> struct S {
12926 friend void f<int>(int, double);
12929 Here, the DECL_TI_TEMPLATE for the friend declaration
12930 will be an IDENTIFIER_NODE. We are being called from
12931 tsubst_friend_function, and we want only to create a
12932 new decl (R) with appropriate types so that we can call
12933 determine_specialization. */
12934 gen_tmpl
= NULL_TREE
;
12935 argvec
= NULL_TREE
;
12938 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12940 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12941 bool member
= ctx
&& TYPE_P (ctx
);
12943 if (member
&& !closure
)
12944 ctx
= tsubst_aggr_type (ctx
, args
,
12945 complain
, t
, /*entering_scope=*/1);
12947 tree type
= (lambda_fntype
? lambda_fntype
12948 : tsubst (TREE_TYPE (t
), args
,
12949 complain
| tf_fndecl_type
, in_decl
));
12950 if (type
== error_mark_node
)
12951 return error_mark_node
;
12953 /* If we hit excessive deduction depth, the type is bogus even if
12954 it isn't error_mark_node, so don't build a decl. */
12955 if (excessive_deduction_depth
)
12956 return error_mark_node
;
12958 /* We do NOT check for matching decls pushed separately at this
12959 point, as they may not represent instantiations of this
12960 template, and in any case are considered separate under the
12962 tree r
= copy_decl (t
);
12963 DECL_USE_TEMPLATE (r
) = 0;
12964 TREE_TYPE (r
) = type
;
12965 /* Clear out the mangled name and RTL for the instantiation. */
12966 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12967 SET_DECL_RTL (r
, NULL
);
12968 /* Leave DECL_INITIAL set on deleted instantiations. */
12969 if (!DECL_DELETED_FN (r
))
12970 DECL_INITIAL (r
) = NULL_TREE
;
12971 DECL_CONTEXT (r
) = ctx
;
12973 /* Handle explicit(dependent-expr). */
12974 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
12976 tree spec
= lookup_explicit_specifier (t
);
12977 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
12978 /*function_p=*/false,
12980 spec
= build_explicit_specifier (spec
, complain
);
12981 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
12984 /* OpenMP UDRs have the only argument a reference to the declared
12985 type. We want to diagnose if the declared type is a reference,
12986 which is invalid, but as references to references are usually
12987 quietly merged, diagnose it here. */
12988 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12991 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12992 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12993 if (TYPE_REF_P (argtype
))
12994 error_at (DECL_SOURCE_LOCATION (t
),
12995 "reference type %qT in "
12996 "%<#pragma omp declare reduction%>", argtype
);
12997 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12998 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13002 if (member
&& DECL_CONV_FN_P (r
))
13003 /* Type-conversion operator. Reconstruct the name, in
13004 case it's the name of one of the template's parameters. */
13005 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13007 tree parms
= DECL_ARGUMENTS (t
);
13009 parms
= DECL_CHAIN (parms
);
13010 parms
= tsubst (parms
, args
, complain
, t
);
13011 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13012 DECL_CONTEXT (parm
) = r
;
13015 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13016 DECL_CHAIN (tparm
) = parms
;
13019 DECL_ARGUMENTS (r
) = parms
;
13020 DECL_RESULT (r
) = NULL_TREE
;
13022 TREE_STATIC (r
) = 0;
13023 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13024 DECL_EXTERNAL (r
) = 1;
13025 /* If this is an instantiation of a function with internal
13026 linkage, we already know what object file linkage will be
13027 assigned to the instantiation. */
13028 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13029 DECL_DEFER_OUTPUT (r
) = 0;
13030 DECL_CHAIN (r
) = NULL_TREE
;
13031 DECL_PENDING_INLINE_INFO (r
) = 0;
13032 DECL_PENDING_INLINE_P (r
) = 0;
13033 DECL_SAVED_TREE (r
) = NULL_TREE
;
13034 DECL_STRUCT_FUNCTION (r
) = NULL
;
13036 /* We'll re-clone as appropriate in instantiate_template. */
13037 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13039 /* If we aren't complaining now, return on error before we register
13040 the specialization so that we'll complain eventually. */
13041 if ((complain
& tf_error
) == 0
13042 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13043 && !grok_op_properties (r
, /*complain=*/false))
13044 return error_mark_node
;
13046 /* When instantiating a constrained member, substitute
13047 into the constraints to create a new constraint. */
13048 if (tree ci
= get_constraints (t
))
13051 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13052 set_constraints (r
, ci
);
13055 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13056 this in the special friend case mentioned above where
13057 GEN_TMPL is NULL. */
13058 if (gen_tmpl
&& !closure
)
13060 DECL_TEMPLATE_INFO (r
)
13061 = build_template_info (gen_tmpl
, argvec
);
13062 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13065 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13067 /* We instantiated this while substituting into
13068 the type earlier (template/friend54.C). */
13071 /* We're not supposed to instantiate default arguments
13072 until they are called, for a template. But, for a
13075 template <class T> void f ()
13076 { extern void g(int i = T()); }
13078 we should do the substitution when the template is
13079 instantiated. We handle the member function case in
13080 instantiate_class_template since the default arguments
13081 might refer to other members of the class. */
13083 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13084 && !uses_template_parms (argvec
))
13085 tsubst_default_arguments (r
, complain
);
13088 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13090 /* Copy the list of befriending classes. */
13091 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13093 friends
= &TREE_CHAIN (*friends
))
13095 *friends
= copy_node (*friends
);
13096 TREE_VALUE (*friends
)
13097 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13100 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13102 maybe_retrofit_in_chrg (r
);
13103 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13104 return error_mark_node
;
13105 /* If this is an instantiation of a member template, clone it.
13106 If it isn't, that'll be handled by
13107 clone_constructors_and_destructors. */
13108 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13109 clone_function_decl (r
, /*update_methods=*/false);
13111 else if ((complain
& tf_error
) != 0
13112 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13113 && !grok_op_properties (r
, /*complain=*/true))
13114 return error_mark_node
;
13116 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13117 SET_DECL_FRIEND_CONTEXT (r
,
13118 tsubst (DECL_FRIEND_CONTEXT (t
),
13119 args
, complain
, in_decl
));
13121 /* Possibly limit visibility based on template args. */
13122 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13123 if (DECL_VISIBILITY_SPECIFIED (t
))
13125 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13126 DECL_ATTRIBUTES (r
)
13127 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13129 determine_visibility (r
);
13130 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13131 && !processing_template_decl
)
13132 defaulted_late_check (r
);
13134 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13135 args
, complain
, in_decl
);
13139 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13142 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13143 tree lambda_fntype
)
13145 /* We can get here when processing a member function template,
13146 member class template, or template template parameter. */
13147 tree decl
= DECL_TEMPLATE_RESULT (t
);
13153 hashval_t hash
= 0;
13155 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13157 /* Template template parameter is treated here. */
13158 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13159 if (new_type
== error_mark_node
)
13160 r
= error_mark_node
;
13161 /* If we get a real template back, return it. This can happen in
13162 the context of most_specialized_partial_spec. */
13163 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13166 /* The new TEMPLATE_DECL was built in
13167 reduce_template_parm_level. */
13168 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13172 if (!lambda_fntype
)
13174 /* We might already have an instance of this template.
13175 The ARGS are for the surrounding class type, so the
13176 full args contain the tsubst'd args for the context,
13177 plus the innermost args from the template decl. */
13178 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13179 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13180 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13181 /* Because this is a template, the arguments will still be
13182 dependent, even after substitution. If
13183 PROCESSING_TEMPLATE_DECL is not set, the dependency
13184 predicates will short-circuit. */
13185 ++processing_template_decl
;
13186 full_args
= tsubst_template_args (tmpl_args
, args
,
13187 complain
, in_decl
);
13188 --processing_template_decl
;
13189 if (full_args
== error_mark_node
)
13190 return error_mark_node
;
13192 /* If this is a default template template argument,
13193 tsubst might not have changed anything. */
13194 if (full_args
== tmpl_args
)
13197 hash
= hash_tmpl_and_args (t
, full_args
);
13198 spec
= retrieve_specialization (t
, full_args
, hash
);
13199 if (spec
!= NULL_TREE
)
13203 /* Make a new template decl. It will be similar to the
13204 original, but will record the current template arguments.
13205 We also create a new function declaration, which is just
13206 like the old one, but points to this new template, rather
13207 than the old one. */
13209 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13210 DECL_CHAIN (r
) = NULL_TREE
;
13212 // Build new template info linking to the original template decl.
13213 if (!lambda_fntype
)
13215 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13216 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13219 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13221 /* The template parameters for this new template are all the
13222 template parameters for the old template, except the
13223 outermost level of parameters. */
13224 DECL_TEMPLATE_PARMS (r
)
13225 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13228 if (TREE_CODE (decl
) == TYPE_DECL
13229 && !TYPE_DECL_ALIAS_P (decl
))
13232 ++processing_template_decl
;
13233 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13234 --processing_template_decl
;
13235 if (new_type
== error_mark_node
)
13236 return error_mark_node
;
13238 TREE_TYPE (r
) = new_type
;
13239 /* For a partial specialization, we need to keep pointing to
13240 the primary template. */
13241 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13242 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13243 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13244 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13245 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13250 ++processing_template_decl
;
13251 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13252 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13254 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13255 --processing_template_decl
;
13256 if (new_decl
== error_mark_node
)
13257 return error_mark_node
;
13259 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13260 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13261 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13264 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13265 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13269 DECL_TI_TEMPLATE (new_decl
) = r
;
13270 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13274 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13275 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13277 if (PRIMARY_TEMPLATE_P (t
))
13278 DECL_PRIMARY_TEMPLATE (r
) = r
;
13280 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13282 /* Record this non-type partial instantiation. */
13283 register_specialization (r
, t
,
13284 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13290 /* True if FN is the op() for a lambda in an uninstantiated template. */
13293 lambda_fn_in_template_p (tree fn
)
13295 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13297 tree closure
= DECL_CONTEXT (fn
);
13298 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13301 /* We're instantiating a variable from template function TCTX. Return the
13302 corresponding current enclosing scope. This gets complicated because lambda
13303 functions in templates are regenerated rather than instantiated, but generic
13304 lambda functions are subsequently instantiated. */
13307 enclosing_instantiation_of (tree otctx
)
13310 tree fn
= current_function_decl
;
13311 int lambda_count
= 0;
13313 for (; tctx
&& lambda_fn_in_template_p (tctx
);
13314 tctx
= decl_function_context (tctx
))
13316 for (; fn
; fn
= decl_function_context (fn
))
13319 int flambda_count
= 0;
13320 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
13321 fn
= decl_function_context (fn
))
13323 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13324 ? most_general_template (fn
) != most_general_template (tctx
)
13327 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13328 || DECL_CONV_FN_P (ofn
));
13331 gcc_unreachable ();
13334 /* Substitute the ARGS into the T, which is a _DECL. Return the
13335 result of the substitution. Issue error and warning messages under
13336 control of COMPLAIN. */
13339 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13341 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13342 location_t saved_loc
;
13343 tree r
= NULL_TREE
;
13345 hashval_t hash
= 0;
13347 /* Set the filename and linenumber to improve error-reporting. */
13348 saved_loc
= input_location
;
13349 input_location
= DECL_SOURCE_LOCATION (t
);
13351 switch (TREE_CODE (t
))
13353 case TEMPLATE_DECL
:
13354 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13357 case FUNCTION_DECL
:
13358 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13363 tree type
= NULL_TREE
;
13365 tree expanded_types
= NULL_TREE
;
13366 tree prev_r
= NULL_TREE
;
13367 tree first_r
= NULL_TREE
;
13369 if (DECL_PACK_P (t
))
13371 /* If there is a local specialization that isn't a
13372 parameter pack, it means that we're doing a "simple"
13373 substitution from inside tsubst_pack_expansion. Just
13374 return the local specialization (which will be a single
13376 tree spec
= retrieve_local_specialization (t
);
13378 && TREE_CODE (spec
) == PARM_DECL
13379 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13383 the parameters in this function parameter pack. */
13384 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13385 complain
, in_decl
);
13386 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13388 len
= TREE_VEC_LENGTH (expanded_types
);
13390 /* Zero-length parameter packs are boring. Just substitute
13393 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13398 /* All we did was update the type. Make a note of that. */
13399 type
= expanded_types
;
13400 expanded_types
= NULL_TREE
;
13404 /* Loop through all of the parameters we'll build. When T is
13405 a function parameter pack, LEN is the number of expanded
13406 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13408 for (i
= 0; i
< len
; ++i
)
13412 if (DECL_TEMPLATE_PARM_P (t
))
13413 SET_DECL_TEMPLATE_PARM_P (r
);
13415 if (expanded_types
)
13416 /* We're on the Ith parameter of the function parameter
13419 /* Get the Ith type. */
13420 type
= TREE_VEC_ELT (expanded_types
, i
);
13422 /* Rename the parameter to include the index. */
13424 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13427 /* We're dealing with a normal parameter. */
13428 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13430 type
= type_decays_to (type
);
13431 TREE_TYPE (r
) = type
;
13432 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13434 if (DECL_INITIAL (r
))
13436 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13437 DECL_INITIAL (r
) = TREE_TYPE (r
);
13439 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13440 complain
, in_decl
);
13443 DECL_CONTEXT (r
) = NULL_TREE
;
13445 if (!DECL_TEMPLATE_PARM_P (r
))
13446 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13448 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13449 args
, complain
, in_decl
);
13451 /* Keep track of the first new parameter we
13452 generate. That's what will be returned to the
13457 /* Build a proper chain of parameters when substituting
13458 into a function parameter pack. */
13460 DECL_CHAIN (prev_r
) = r
;
13463 /* If cp_unevaluated_operand is set, we're just looking for a
13464 single dummy parameter, so don't keep going. */
13465 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13466 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13467 complain
, DECL_CHAIN (t
));
13469 /* FIRST_R contains the start of the chain we've built. */
13476 tree type
= NULL_TREE
;
13477 tree vec
= NULL_TREE
;
13478 tree expanded_types
= NULL_TREE
;
13481 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13483 /* This field is a lambda capture pack. Return a TREE_VEC of
13484 the expanded fields to instantiate_class_template_1. */
13485 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13486 complain
, in_decl
);
13487 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13489 len
= TREE_VEC_LENGTH (expanded_types
);
13490 vec
= make_tree_vec (len
);
13494 /* All we did was update the type. Make a note of that. */
13495 type
= expanded_types
;
13496 expanded_types
= NULL_TREE
;
13500 for (int i
= 0; i
< len
; ++i
)
13503 if (expanded_types
)
13505 type
= TREE_VEC_ELT (expanded_types
, i
);
13507 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13510 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13512 if (type
== error_mark_node
)
13513 RETURN (error_mark_node
);
13514 TREE_TYPE (r
) = type
;
13515 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13517 if (DECL_C_BIT_FIELD (r
))
13518 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13520 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13521 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13523 /*integral_constant_expression_p=*/true);
13524 if (DECL_INITIAL (t
))
13526 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13527 NSDMI in perform_member_init. Still set DECL_INITIAL
13528 so that we know there is one. */
13529 DECL_INITIAL (r
) = void_node
;
13530 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13531 retrofit_lang_decl (r
);
13532 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13534 /* We don't have to set DECL_CONTEXT here; it is set by
13535 finish_member_declaration. */
13536 DECL_CHAIN (r
) = NULL_TREE
;
13538 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13539 args
, complain
, in_decl
);
13542 TREE_VEC_ELT (vec
, i
) = r
;
13551 /* We reach here only for member using decls. We also need to check
13552 uses_template_parms because DECL_DEPENDENT_P is not set for a
13553 using-declaration that designates a member of the current
13554 instantiation (c++/53549). */
13555 if (DECL_DEPENDENT_P (t
)
13556 || uses_template_parms (USING_DECL_SCOPE (t
)))
13558 tree scope
= USING_DECL_SCOPE (t
);
13559 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13560 if (PACK_EXPANSION_P (scope
))
13562 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13563 int len
= TREE_VEC_LENGTH (vec
);
13564 r
= make_tree_vec (len
);
13565 for (int i
= 0; i
< len
; ++i
)
13567 tree escope
= TREE_VEC_ELT (vec
, i
);
13568 tree elt
= do_class_using_decl (escope
, name
);
13571 r
= error_mark_node
;
13576 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13577 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13579 TREE_VEC_ELT (r
, i
) = elt
;
13584 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13585 complain
, in_decl
);
13586 r
= do_class_using_decl (inst_scope
, name
);
13588 r
= error_mark_node
;
13591 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13592 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13599 DECL_CHAIN (r
) = NULL_TREE
;
13606 tree argvec
= NULL_TREE
;
13607 tree gen_tmpl
= NULL_TREE
;
13609 tree tmpl
= NULL_TREE
;
13611 tree type
= NULL_TREE
;
13614 if (TREE_TYPE (t
) == error_mark_node
)
13615 RETURN (error_mark_node
);
13617 if (TREE_CODE (t
) == TYPE_DECL
13618 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13620 /* If this is the canonical decl, we don't have to
13621 mess with instantiations, and often we can't (for
13622 typename, template type parms and such). Note that
13623 TYPE_NAME is not correct for the above test if
13624 we've copied the type for a typedef. */
13625 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13626 if (type
== error_mark_node
)
13627 RETURN (error_mark_node
);
13628 r
= TYPE_NAME (type
);
13632 /* Check to see if we already have the specialization we
13635 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13637 /* T is a static data member or namespace-scope entity.
13638 We have to substitute into namespace-scope variables
13639 (not just variable templates) because of cases like:
13641 template <class T> void f() { extern T t; }
13643 where the entity referenced is not known until
13644 instantiation time. */
13646 ctx
= DECL_CONTEXT (t
);
13647 if (DECL_CLASS_SCOPE_P (t
))
13649 ctx
= tsubst_aggr_type (ctx
, args
,
13651 in_decl
, /*entering_scope=*/1);
13652 /* If CTX is unchanged, then T is in fact the
13653 specialization we want. That situation occurs when
13654 referencing a static data member within in its own
13655 class. We can use pointer equality, rather than
13656 same_type_p, because DECL_CONTEXT is always
13658 if (ctx
== DECL_CONTEXT (t
)
13659 /* ... unless T is a member template; in which
13660 case our caller can be willing to create a
13661 specialization of that template represented
13663 && !(DECL_TI_TEMPLATE (t
)
13664 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13670 tmpl
= DECL_TI_TEMPLATE (t
);
13671 gen_tmpl
= most_general_template (tmpl
);
13672 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13673 if (argvec
!= error_mark_node
)
13674 argvec
= (coerce_innermost_template_parms
13675 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13676 argvec
, t
, complain
,
13677 /*all*/true, /*defarg*/true));
13678 if (argvec
== error_mark_node
)
13679 RETURN (error_mark_node
);
13680 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13681 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13686 /* A local variable. */
13688 /* Subsequent calls to pushdecl will fill this in. */
13690 /* Unless this is a reference to a static variable from an
13691 enclosing function, in which case we need to fill it in now. */
13692 if (TREE_STATIC (t
))
13694 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13695 if (fn
!= current_function_decl
)
13698 spec
= retrieve_local_specialization (t
);
13700 /* If we already have the specialization we need, there is
13701 nothing more to do. */
13708 /* Create a new node for the specialization we need. */
13709 if (type
== NULL_TREE
)
13711 if (is_typedef_decl (t
))
13712 type
= DECL_ORIGINAL_TYPE (t
);
13714 type
= TREE_TYPE (t
);
13716 && VAR_HAD_UNKNOWN_BOUND (t
)
13717 && type
!= error_mark_node
)
13718 type
= strip_array_domain (type
);
13719 tree sub_args
= args
;
13720 if (tree auto_node
= type_uses_auto (type
))
13722 /* Mask off any template args past the variable's context so we
13723 don't replace the auto with an unrelated argument. */
13724 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13725 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13727 /* This should never happen with the new lambda instantiation
13728 model, but keep the handling just in case. */
13729 gcc_assert (!CHECKING_P
),
13730 sub_args
= strip_innermost_template_args (args
, extra
);
13732 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13733 /* Substituting the type might have recursively instantiated this
13734 same alias (c++/86171). */
13735 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13736 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13745 DECL_INITIALIZED_P (r
) = 0;
13746 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13747 if (type
== error_mark_node
)
13748 RETURN (error_mark_node
);
13749 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13751 /* It may seem that this case cannot occur, since:
13756 declares a function, not a variable. However:
13759 template <typename T> void g() { T t; }
13760 template void g<f>();
13762 is an attempt to declare a variable with function
13764 error ("variable %qD has function type",
13765 /* R is not yet sufficiently initialized, so we
13766 just use its name. */
13768 RETURN (error_mark_node
);
13770 type
= complete_type (type
);
13771 /* Wait until cp_finish_decl to set this again, to handle
13772 circular dependency (template/instantiate6.C). */
13773 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13774 type
= check_var_type (DECL_NAME (r
), type
);
13776 if (DECL_HAS_VALUE_EXPR_P (t
))
13778 tree ve
= DECL_VALUE_EXPR (t
);
13779 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13780 /*constant_expression_p=*/false);
13781 if (REFERENCE_REF_P (ve
))
13783 gcc_assert (TYPE_REF_P (type
));
13784 ve
= TREE_OPERAND (ve
, 0);
13786 SET_DECL_VALUE_EXPR (r
, ve
);
13788 if (CP_DECL_THREAD_LOCAL_P (r
)
13789 && !processing_template_decl
)
13790 set_decl_tls_model (r
, decl_default_tls_model (r
));
13792 else if (DECL_SELF_REFERENCE_P (t
))
13793 SET_DECL_SELF_REFERENCE_P (r
);
13794 TREE_TYPE (r
) = type
;
13795 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13796 DECL_CONTEXT (r
) = ctx
;
13797 /* Clear out the mangled name and RTL for the instantiation. */
13798 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13799 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13800 SET_DECL_RTL (r
, NULL
);
13801 /* The initializer must not be expanded until it is required;
13802 see [temp.inst]. */
13803 DECL_INITIAL (r
) = NULL_TREE
;
13804 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13807 if (DECL_LANG_SPECIFIC (r
))
13808 SET_DECL_DEPENDENT_INIT_P (r
, false);
13810 SET_DECL_MODE (r
, VOIDmode
);
13812 /* Possibly limit visibility based on template args. */
13813 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13814 if (DECL_VISIBILITY_SPECIFIED (t
))
13816 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13817 DECL_ATTRIBUTES (r
)
13818 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13820 determine_visibility (r
);
13825 /* A static data member declaration is always marked
13826 external when it is declared in-class, even if an
13827 initializer is present. We mimic the non-template
13828 processing here. */
13829 DECL_EXTERNAL (r
) = 1;
13830 if (DECL_NAMESPACE_SCOPE_P (t
))
13831 DECL_NOT_REALLY_EXTERN (r
) = 1;
13833 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13834 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13835 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13839 if (DECL_LANG_SPECIFIC (r
))
13840 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13841 if (!cp_unevaluated_operand
)
13842 register_local_specialization (r
, t
);
13845 DECL_CHAIN (r
) = NULL_TREE
;
13847 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13849 args
, complain
, in_decl
);
13851 /* Preserve a typedef that names a type. */
13852 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13854 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13855 set_underlying_type (r
);
13856 if (TYPE_DECL_ALIAS_P (r
))
13857 /* An alias template specialization can be dependent
13858 even if its underlying type is not. */
13859 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13862 layout_decl (r
, 0);
13867 gcc_unreachable ();
13872 /* Restore the file and line information. */
13873 input_location
= saved_loc
;
13878 /* Substitute into the ARG_TYPES of a function type.
13879 If END is a TREE_CHAIN, leave it and any following types
13883 tsubst_arg_types (tree arg_types
,
13886 tsubst_flags_t complain
,
13889 tree remaining_arg_types
;
13890 tree type
= NULL_TREE
;
13892 tree expanded_args
= NULL_TREE
;
13895 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13898 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13899 args
, end
, complain
, in_decl
);
13900 if (remaining_arg_types
== error_mark_node
)
13901 return error_mark_node
;
13903 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13905 /* For a pack expansion, perform substitution on the
13906 entire expression. Later on, we'll handle the arguments
13908 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13909 args
, complain
, in_decl
);
13911 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13912 /* So that we'll spin through the parameters, one by one. */
13913 i
= TREE_VEC_LENGTH (expanded_args
);
13916 /* We only partially substituted into the parameter
13917 pack. Our type is TYPE_PACK_EXPANSION. */
13918 type
= expanded_args
;
13919 expanded_args
= NULL_TREE
;
13927 type
= TREE_VEC_ELT (expanded_args
, i
);
13929 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13931 if (type
== error_mark_node
)
13932 return error_mark_node
;
13933 if (VOID_TYPE_P (type
))
13935 if (complain
& tf_error
)
13937 error ("invalid parameter type %qT", type
);
13939 error ("in declaration %q+D", in_decl
);
13941 return error_mark_node
;
13944 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13945 return error_mark_node
;
13947 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13948 top-level qualifiers as required. */
13949 type
= cv_unqualified (type_decays_to (type
));
13951 /* We do not substitute into default arguments here. The standard
13952 mandates that they be instantiated only when needed, which is
13953 done in build_over_call. */
13954 default_arg
= TREE_PURPOSE (arg_types
);
13956 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13957 since the new op() won't have any associated template arguments for us
13958 to refer to later. */
13959 if (lambda_fn_in_template_p (in_decl
))
13960 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13961 false/*fn*/, false/*constexpr*/);
13963 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13965 /* We've instantiated a template before its default arguments
13966 have been parsed. This can happen for a nested template
13967 class, and is not an error unless we require the default
13968 argument in a call of this function. */
13969 remaining_arg_types
=
13970 tree_cons (default_arg
, type
, remaining_arg_types
);
13971 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13974 remaining_arg_types
=
13975 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13978 return remaining_arg_types
;
13981 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13982 *not* handle the exception-specification for FNTYPE, because the
13983 initial substitution of explicitly provided template parameters
13984 during argument deduction forbids substitution into the
13985 exception-specification:
13989 All references in the function type of the function template to the
13990 corresponding template parameters are replaced by the specified tem-
13991 plate argument values. If a substitution in a template parameter or
13992 in the function type of the function template results in an invalid
13993 type, type deduction fails. [Note: The equivalent substitution in
13994 exception specifications is done only when the function is instanti-
13995 ated, at which point a program is ill-formed if the substitution
13996 results in an invalid type.] */
13999 tsubst_function_type (tree t
,
14001 tsubst_flags_t complain
,
14005 tree arg_types
= NULL_TREE
;
14008 /* The TYPE_CONTEXT is not used for function/method types. */
14009 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14011 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14013 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14015 if (late_return_type_p
)
14017 /* Substitute the argument types. */
14018 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14019 complain
, in_decl
);
14020 if (arg_types
== error_mark_node
)
14021 return error_mark_node
;
14023 tree save_ccp
= current_class_ptr
;
14024 tree save_ccr
= current_class_ref
;
14025 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14026 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14027 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14030 /* DR 1207: 'this' is in scope in the trailing return type. */
14031 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14034 /* Substitute the return type. */
14035 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14039 current_class_ptr
= save_ccp
;
14040 current_class_ref
= save_ccr
;
14044 /* Substitute the return type. */
14045 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14047 if (return_type
== error_mark_node
)
14048 return error_mark_node
;
14049 /* DR 486 clarifies that creation of a function type with an
14050 invalid return type is a deduction failure. */
14051 if (TREE_CODE (return_type
) == ARRAY_TYPE
14052 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14054 if (complain
& tf_error
)
14056 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14057 error ("function returning an array");
14059 error ("function returning a function");
14061 return error_mark_node
;
14064 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14065 return error_mark_node
;
14067 if (!late_return_type_p
)
14069 /* Substitute the argument types. */
14070 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14071 complain
, in_decl
);
14072 if (arg_types
== error_mark_node
)
14073 return error_mark_node
;
14076 /* Construct a new type node and return it. */
14077 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14079 fntype
= build_function_type (return_type
, arg_types
);
14080 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14084 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14085 /* Don't pick up extra function qualifiers from the basetype. */
14086 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14087 if (! MAYBE_CLASS_TYPE_P (r
))
14091 Type deduction may fail for any of the following
14094 -- Attempting to create "pointer to member of T" when T
14095 is not a class type. */
14096 if (complain
& tf_error
)
14097 error ("creating pointer to member function of non-class type %qT",
14099 return error_mark_node
;
14102 fntype
= build_method_type_directly (r
, return_type
,
14103 TREE_CHAIN (arg_types
));
14105 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14107 /* See comment above. */
14108 tree raises
= NULL_TREE
;
14109 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14110 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14115 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14116 ARGS into that specification, and return the substituted
14117 specification. If there is no specification, return NULL_TREE. */
14120 tsubst_exception_specification (tree fntype
,
14122 tsubst_flags_t complain
,
14129 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14130 new_specs
= NULL_TREE
;
14131 if (specs
&& TREE_PURPOSE (specs
))
14133 /* A noexcept-specifier. */
14134 tree expr
= TREE_PURPOSE (specs
);
14135 if (TREE_CODE (expr
) == INTEGER_CST
)
14139 /* Defer instantiation of noexcept-specifiers to avoid
14140 excessive instantiations (c++/49107). */
14141 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14142 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14144 /* We already partially instantiated this member template,
14145 so combine the new args with the old. */
14146 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14147 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14148 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14149 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14153 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14154 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14159 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14161 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14163 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14165 new_specs
= tsubst_copy_and_build
14166 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14167 /*integral_constant_expression_p=*/true);
14169 new_specs
= build_noexcept_spec (new_specs
, complain
);
14173 if (! TREE_VALUE (specs
))
14180 tree expanded_specs
= NULL_TREE
;
14182 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14184 /* Expand the pack expansion type. */
14185 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14189 if (expanded_specs
== error_mark_node
)
14190 return error_mark_node
;
14191 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14192 len
= TREE_VEC_LENGTH (expanded_specs
);
14195 /* We're substituting into a member template, so
14196 we got a TYPE_PACK_EXPANSION back. Add that
14197 expansion and move on. */
14198 gcc_assert (TREE_CODE (expanded_specs
)
14199 == TYPE_PACK_EXPANSION
);
14200 new_specs
= add_exception_specifier (new_specs
,
14203 specs
= TREE_CHAIN (specs
);
14208 for (i
= 0; i
< len
; ++i
)
14210 if (expanded_specs
)
14211 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14213 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14214 if (spec
== error_mark_node
)
14216 new_specs
= add_exception_specifier (new_specs
, spec
,
14220 specs
= TREE_CHAIN (specs
);
14226 /* Take the tree structure T and replace template parameters used
14227 therein with the argument vector ARGS. IN_DECL is an associated
14228 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14229 Issue error and warning messages under control of COMPLAIN. Note
14230 that we must be relatively non-tolerant of extensions here, in
14231 order to preserve conformance; if we allow substitutions that
14232 should not be allowed, we may allow argument deductions that should
14233 not succeed, and therefore report ambiguous overload situations
14234 where there are none. In theory, we could allow the substitution,
14235 but indicate that it should have failed, and allow our caller to
14236 make sure that the right thing happens, but we don't try to do this
14239 This function is used for dealing with types, decls and the like;
14240 for expressions, use tsubst_expr or tsubst_copy. */
14243 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14245 enum tree_code code
;
14246 tree type
, r
= NULL_TREE
;
14248 if (t
== NULL_TREE
|| t
== error_mark_node
14249 || t
== integer_type_node
14250 || t
== void_type_node
14251 || t
== char_type_node
14252 || t
== unknown_type_node
14253 || TREE_CODE (t
) == NAMESPACE_DECL
14254 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14258 return tsubst_decl (t
, args
, complain
);
14260 if (args
== NULL_TREE
)
14263 code
= TREE_CODE (t
);
14265 if (code
== IDENTIFIER_NODE
)
14266 type
= IDENTIFIER_TYPE_VALUE (t
);
14268 type
= TREE_TYPE (t
);
14270 gcc_assert (type
!= unknown_type_node
);
14272 /* Reuse typedefs. We need to do this to handle dependent attributes,
14273 such as attribute aligned. */
14275 && typedef_variant_p (t
))
14277 tree decl
= TYPE_NAME (t
);
14279 if (alias_template_specialization_p (t
))
14281 /* DECL represents an alias template and we want to
14283 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14284 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14285 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14287 else if (DECL_CLASS_SCOPE_P (decl
)
14288 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14289 && uses_template_parms (DECL_CONTEXT (decl
)))
14291 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14292 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14293 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14295 else if (DECL_FUNCTION_SCOPE_P (decl
)
14296 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14297 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14298 r
= retrieve_local_specialization (decl
);
14300 /* The typedef is from a non-template context. */
14306 r
= cp_build_qualified_type_real
14307 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14308 complain
| tf_ignore_bad_quals
);
14313 /* We don't have an instantiation yet, so drop the typedef. */
14314 int quals
= cp_type_quals (t
);
14315 t
= DECL_ORIGINAL_TYPE (decl
);
14316 t
= cp_build_qualified_type_real (t
, quals
,
14317 complain
| tf_ignore_bad_quals
);
14321 bool fndecl_type
= (complain
& tf_fndecl_type
);
14322 complain
&= ~tf_fndecl_type
;
14325 && code
!= TYPENAME_TYPE
14326 && code
!= TEMPLATE_TYPE_PARM
14327 && code
!= TEMPLATE_PARM_INDEX
14328 && code
!= IDENTIFIER_NODE
14329 && code
!= FUNCTION_TYPE
14330 && code
!= METHOD_TYPE
)
14331 type
= tsubst (type
, args
, complain
, in_decl
);
14332 if (type
== error_mark_node
)
14333 return error_mark_node
;
14339 case ENUMERAL_TYPE
:
14340 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14341 /*entering_scope=*/0);
14344 case IDENTIFIER_NODE
:
14355 if (t
== integer_type_node
)
14358 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14359 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14363 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14365 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14366 /*integral_constant_expression_p=*/false);
14368 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14370 if (TREE_CODE (max
) == NOP_EXPR
14371 && TREE_SIDE_EFFECTS (omax
)
14372 && !TREE_TYPE (max
))
14373 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14375 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14376 with TREE_SIDE_EFFECTS that indicates this is not an integral
14377 constant expression. */
14378 if (processing_template_decl
14379 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14381 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14382 TREE_SIDE_EFFECTS (max
) = 1;
14385 return compute_array_index_type (NULL_TREE
, max
, complain
);
14388 case TEMPLATE_TYPE_PARM
:
14389 case TEMPLATE_TEMPLATE_PARM
:
14390 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14391 case TEMPLATE_PARM_INDEX
:
14396 tree arg
= NULL_TREE
;
14398 /* Early in template argument deduction substitution, we don't
14399 want to reduce the level of 'auto', or it will be confused
14400 with a normal template parm in subsequent deduction. */
14401 if (is_auto (t
) && (complain
& tf_partial
))
14406 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14407 template_parm_level_and_index (t
, &level
, &idx
);
14409 levels
= TMPL_ARGS_DEPTH (args
);
14410 if (level
<= levels
14411 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14413 arg
= TMPL_ARG (args
, level
, idx
);
14415 /* See through ARGUMENT_PACK_SELECT arguments. */
14416 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14417 arg
= argument_pack_select_arg (arg
);
14420 if (arg
== error_mark_node
)
14421 return error_mark_node
;
14422 else if (arg
!= NULL_TREE
)
14424 if (ARGUMENT_PACK_P (arg
))
14425 /* If ARG is an argument pack, we don't actually want to
14426 perform a substitution here, because substitutions
14427 for argument packs are only done
14428 element-by-element. We can get to this point when
14429 substituting the type of a non-type template
14430 parameter pack, when that type actually contains
14431 template parameter packs from an outer template, e.g.,
14433 template<typename... Types> struct A {
14434 template<Types... Values> struct B { };
14438 if (code
== TEMPLATE_TYPE_PARM
)
14441 gcc_assert (TYPE_P (arg
));
14443 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14445 return cp_build_qualified_type_real
14446 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14448 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14450 /* We are processing a type constructed from a
14451 template template parameter. */
14452 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14453 args
, complain
, in_decl
);
14454 if (argvec
== error_mark_node
)
14455 return error_mark_node
;
14457 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14458 || TREE_CODE (arg
) == TEMPLATE_DECL
14459 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14461 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14462 /* Consider this code:
14464 template <template <class> class Template>
14466 template <class Arg> using Bind = Template<Arg>;
14469 template <template <class> class Template, class Arg>
14470 using Instantiate = Template<Arg>; //#0
14472 template <template <class> class Template,
14475 Instantiate<Internal<Template>::template Bind,
14478 When #1 is parsed, the
14479 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14480 parameter `Template' in #0 matches the
14481 UNBOUND_CLASS_TEMPLATE representing the argument
14482 `Internal<Template>::template Bind'; We then want
14483 to assemble the type `Bind<Argument>' that can't
14484 be fully created right now, because
14485 `Internal<Template>' not being complete, the Bind
14486 template cannot be looked up in that context. So
14487 we need to "store" `Bind<Argument>' for later
14488 when the context of Bind becomes complete. Let's
14489 store that in a TYPENAME_TYPE. */
14490 return make_typename_type (TYPE_CONTEXT (arg
),
14491 build_nt (TEMPLATE_ID_EXPR
,
14492 TYPE_IDENTIFIER (arg
),
14497 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14498 are resolving nested-types in the signature of a
14499 member function templates. Otherwise ARG is a
14500 TEMPLATE_DECL and is the real template to be
14502 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14503 arg
= TYPE_NAME (arg
);
14505 r
= lookup_template_class (arg
,
14507 DECL_CONTEXT (arg
),
14508 /*entering_scope=*/0,
14510 return cp_build_qualified_type_real
14511 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14513 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14516 /* TEMPLATE_PARM_INDEX. */
14517 return convert_from_reference (unshare_expr (arg
));
14521 /* This can happen during the attempted tsubst'ing in
14522 unify. This means that we don't yet have any information
14523 about the template parameter in question. */
14526 /* If we get here, we must have been looking at a parm for a
14527 more deeply nested template. Make a new version of this
14528 template parameter, but with a lower level. */
14531 case TEMPLATE_TYPE_PARM
:
14532 case TEMPLATE_TEMPLATE_PARM
:
14533 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14534 if (cp_type_quals (t
))
14536 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14537 r
= cp_build_qualified_type_real
14538 (r
, cp_type_quals (t
),
14539 complain
| (code
== TEMPLATE_TYPE_PARM
14540 ? tf_ignore_bad_quals
: 0));
14542 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14543 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14544 && (r
= (TEMPLATE_PARM_DESCENDANTS
14545 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14546 && (r
= TREE_TYPE (r
))
14547 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14548 /* Break infinite recursion when substituting the constraints
14549 of a constrained placeholder. */;
14550 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14551 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14552 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14553 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14554 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14555 && (TEMPLATE_PARM_LEVEL (r
)
14556 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14557 /* Cache the simple case of lowering a type parameter. */
14562 TEMPLATE_TYPE_PARM_INDEX (r
)
14563 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14564 r
, levels
, args
, complain
);
14565 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14566 TYPE_MAIN_VARIANT (r
) = r
;
14567 TYPE_POINTER_TO (r
) = NULL_TREE
;
14568 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14570 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14572 /* Propagate constraints on placeholders. */
14573 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14574 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14575 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14576 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14578 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14579 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14583 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14584 /* We have reduced the level of the template
14585 template parameter, but not the levels of its
14586 template parameters, so canonical_type_parameter
14587 will not be able to find the canonical template
14588 template parameter for this level. Thus, we
14589 require structural equality checking to compare
14590 TEMPLATE_TEMPLATE_PARMs. */
14591 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14592 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14593 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14595 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14597 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14599 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14600 /* We might need to substitute into the types of non-type
14601 template parameters. */
14602 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14603 complain
, in_decl
);
14604 if (tmpl
== error_mark_node
)
14605 return error_mark_node
;
14606 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14607 complain
, in_decl
);
14608 if (argvec
== error_mark_node
)
14609 return error_mark_node
;
14611 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14612 = build_template_info (tmpl
, argvec
);
14617 case TEMPLATE_PARM_INDEX
:
14618 /* OK, now substitute the type of the non-type parameter. We
14619 couldn't do it earlier because it might be an auto parameter,
14620 and we wouldn't need to if we had an argument. */
14621 type
= tsubst (type
, args
, complain
, in_decl
);
14622 if (type
== error_mark_node
)
14623 return error_mark_node
;
14624 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14628 gcc_unreachable ();
14636 tree purpose
, value
, chain
;
14638 if (t
== void_list_node
)
14641 purpose
= TREE_PURPOSE (t
);
14644 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14645 if (purpose
== error_mark_node
)
14646 return error_mark_node
;
14648 value
= TREE_VALUE (t
);
14651 value
= tsubst (value
, args
, complain
, in_decl
);
14652 if (value
== error_mark_node
)
14653 return error_mark_node
;
14655 chain
= TREE_CHAIN (t
);
14656 if (chain
&& chain
!= void_type_node
)
14658 chain
= tsubst (chain
, args
, complain
, in_decl
);
14659 if (chain
== error_mark_node
)
14660 return error_mark_node
;
14662 if (purpose
== TREE_PURPOSE (t
)
14663 && value
== TREE_VALUE (t
)
14664 && chain
== TREE_CHAIN (t
))
14666 return hash_tree_cons (purpose
, value
, chain
);
14670 /* We should never be tsubsting a binfo. */
14671 gcc_unreachable ();
14674 /* A vector of template arguments. */
14675 gcc_assert (!type
);
14676 return tsubst_template_args (t
, args
, complain
, in_decl
);
14679 case REFERENCE_TYPE
:
14681 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14686 Type deduction may fail for any of the following
14689 -- Attempting to create a pointer to reference type.
14690 -- Attempting to create a reference to a reference type or
14691 a reference to void.
14693 Core issue 106 says that creating a reference to a reference
14694 during instantiation is no longer a cause for failure. We
14695 only enforce this check in strict C++98 mode. */
14696 if ((TYPE_REF_P (type
)
14697 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14698 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14700 static location_t last_loc
;
14702 /* We keep track of the last time we issued this error
14703 message to avoid spewing a ton of messages during a
14704 single bad template instantiation. */
14705 if (complain
& tf_error
14706 && last_loc
!= input_location
)
14708 if (VOID_TYPE_P (type
))
14709 error ("forming reference to void");
14710 else if (code
== POINTER_TYPE
)
14711 error ("forming pointer to reference type %qT", type
);
14713 error ("forming reference to reference type %qT", type
);
14714 last_loc
= input_location
;
14717 return error_mark_node
;
14719 else if (TREE_CODE (type
) == FUNCTION_TYPE
14720 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14721 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14723 if (complain
& tf_error
)
14725 if (code
== POINTER_TYPE
)
14726 error ("forming pointer to qualified function type %qT",
14729 error ("forming reference to qualified function type %qT",
14732 return error_mark_node
;
14734 else if (code
== POINTER_TYPE
)
14736 r
= build_pointer_type (type
);
14737 if (TREE_CODE (type
) == METHOD_TYPE
)
14738 r
= build_ptrmemfunc_type (r
);
14740 else if (TYPE_REF_P (type
))
14741 /* In C++0x, during template argument substitution, when there is an
14742 attempt to create a reference to a reference type, reference
14743 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14745 "If a template-argument for a template-parameter T names a type
14746 that is a reference to a type A, an attempt to create the type
14747 'lvalue reference to cv T' creates the type 'lvalue reference to
14748 A,' while an attempt to create the type type rvalue reference to
14749 cv T' creates the type T"
14751 r
= cp_build_reference_type
14753 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14755 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14756 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14758 if (r
!= error_mark_node
)
14759 /* Will this ever be needed for TYPE_..._TO values? */
14766 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14767 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14771 Type deduction may fail for any of the following
14774 -- Attempting to create "pointer to member of T" when T
14775 is not a class type. */
14776 if (complain
& tf_error
)
14777 error ("creating pointer to member of non-class type %qT", r
);
14778 return error_mark_node
;
14780 if (TYPE_REF_P (type
))
14782 if (complain
& tf_error
)
14783 error ("creating pointer to member reference type %qT", type
);
14784 return error_mark_node
;
14786 if (VOID_TYPE_P (type
))
14788 if (complain
& tf_error
)
14789 error ("creating pointer to member of type void");
14790 return error_mark_node
;
14792 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14793 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14795 /* The type of the implicit object parameter gets its
14796 cv-qualifiers from the FUNCTION_TYPE. */
14799 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14800 type_memfn_rqual (type
));
14801 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14802 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14806 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14810 case FUNCTION_TYPE
:
14815 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14816 if (fntype
== error_mark_node
)
14817 return error_mark_node
;
14819 /* Substitute the exception specification. */
14820 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14821 /*defer_ok*/fndecl_type
);
14822 if (specs
== error_mark_node
)
14823 return error_mark_node
;
14825 fntype
= build_exception_variant (fntype
, specs
);
14830 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14831 if (domain
== error_mark_node
)
14832 return error_mark_node
;
14834 /* As an optimization, we avoid regenerating the array type if
14835 it will obviously be the same as T. */
14836 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14839 /* These checks should match the ones in create_array_type_for_decl.
14843 The deduction may fail for any of the following reasons:
14845 -- Attempting to create an array with an element type that
14846 is void, a function type, or a reference type, or [DR337]
14847 an abstract class type. */
14848 if (VOID_TYPE_P (type
)
14849 || TREE_CODE (type
) == FUNCTION_TYPE
14850 || (TREE_CODE (type
) == ARRAY_TYPE
14851 && TYPE_DOMAIN (type
) == NULL_TREE
)
14852 || TYPE_REF_P (type
))
14854 if (complain
& tf_error
)
14855 error ("creating array of %qT", type
);
14856 return error_mark_node
;
14859 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14860 return error_mark_node
;
14862 r
= build_cplus_array_type (type
, domain
);
14864 if (!valid_array_size_p (input_location
, r
, in_decl
,
14865 (complain
& tf_error
)))
14866 return error_mark_node
;
14868 if (TYPE_USER_ALIGN (t
))
14870 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14871 TYPE_USER_ALIGN (r
) = 1;
14877 case TYPENAME_TYPE
:
14879 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14880 in_decl
, /*entering_scope=*/1);
14881 if (ctx
== error_mark_node
)
14882 return error_mark_node
;
14884 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14885 complain
, in_decl
);
14886 if (f
== error_mark_node
)
14887 return error_mark_node
;
14889 if (!MAYBE_CLASS_TYPE_P (ctx
))
14891 if (complain
& tf_error
)
14892 error ("%qT is not a class, struct, or union type", ctx
);
14893 return error_mark_node
;
14895 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14897 /* Normally, make_typename_type does not require that the CTX
14898 have complete type in order to allow things like:
14900 template <class T> struct S { typename S<T>::X Y; };
14902 But, such constructs have already been resolved by this
14903 point, so here CTX really should have complete type, unless
14904 it's a partial instantiation. */
14905 ctx
= complete_type (ctx
);
14906 if (!COMPLETE_TYPE_P (ctx
))
14908 if (complain
& tf_error
)
14909 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14910 return error_mark_node
;
14914 f
= make_typename_type (ctx
, f
, typename_type
,
14915 complain
| tf_keep_type_decl
);
14916 if (f
== error_mark_node
)
14918 if (TREE_CODE (f
) == TYPE_DECL
)
14920 complain
|= tf_ignore_bad_quals
;
14924 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14926 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14928 if (complain
& tf_error
)
14929 error ("%qT resolves to %qT, which is not an enumeration type",
14932 return error_mark_node
;
14934 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14936 if (complain
& tf_error
)
14937 error ("%qT resolves to %qT, which is is not a class type",
14940 return error_mark_node
;
14944 return cp_build_qualified_type_real
14945 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14948 case UNBOUND_CLASS_TEMPLATE
:
14950 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14951 in_decl
, /*entering_scope=*/1);
14952 tree name
= TYPE_IDENTIFIER (t
);
14953 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14955 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14956 return error_mark_node
;
14959 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14960 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14967 ++cp_unevaluated_operand
;
14968 ++c_inhibit_evaluation_warnings
;
14970 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14972 /*integral_constant_expression_p=*/false);
14974 --cp_unevaluated_operand
;
14975 --c_inhibit_evaluation_warnings
;
14977 type
= finish_typeof (type
);
14978 return cp_build_qualified_type_real (type
,
14980 | cp_type_quals (type
),
14984 case DECLTYPE_TYPE
:
14988 ++cp_unevaluated_operand
;
14989 ++c_inhibit_evaluation_warnings
;
14991 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14992 complain
|tf_decltype
, in_decl
,
14993 /*function_p*/false,
14994 /*integral_constant_expression*/false);
14996 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14998 if (type
== NULL_TREE
)
15000 if (complain
& tf_error
)
15001 error ("empty initializer in lambda init-capture");
15002 type
= error_mark_node
;
15004 else if (TREE_CODE (type
) == TREE_LIST
)
15005 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
15008 --cp_unevaluated_operand
;
15009 --c_inhibit_evaluation_warnings
;
15011 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15012 type
= lambda_capture_field_type (type
,
15013 DECLTYPE_FOR_INIT_CAPTURE (t
),
15014 DECLTYPE_FOR_REF_CAPTURE (t
));
15015 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15016 type
= lambda_proxy_type (type
);
15019 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15020 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15022 /* In a template ~id could be either a complement expression
15023 or an unqualified-id naming a destructor; if instantiating
15024 it produces an expression, it's not an id-expression or
15027 type
= finish_decltype_type (type
, id
, complain
);
15029 return cp_build_qualified_type_real (type
,
15031 | cp_type_quals (type
),
15032 complain
| tf_ignore_bad_quals
);
15035 case UNDERLYING_TYPE
:
15037 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15038 complain
, in_decl
);
15039 return finish_underlying_type (type
);
15042 case TYPE_ARGUMENT_PACK
:
15043 case NONTYPE_ARGUMENT_PACK
:
15047 if (code
== NONTYPE_ARGUMENT_PACK
)
15048 r
= make_node (code
);
15050 r
= cxx_make_type (code
);
15052 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15053 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15054 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15072 /* We should use one of the expression tsubsts for these codes. */
15073 gcc_unreachable ();
15076 sorry ("use of %qs in template", get_tree_code_name (code
));
15077 return error_mark_node
;
15081 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15082 expression on the left-hand side of the "." or "->" operator. We
15083 only do the lookup if we had a dependent BASELINK. Otherwise we
15084 adjust it onto the instantiated heirarchy. */
15087 tsubst_baselink (tree baselink
, tree object_type
,
15088 tree args
, tsubst_flags_t complain
, tree in_decl
)
15090 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15091 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15092 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15094 tree optype
= BASELINK_OPTYPE (baselink
);
15095 optype
= tsubst (optype
, args
, complain
, in_decl
);
15097 tree template_args
= NULL_TREE
;
15098 bool template_id_p
= false;
15099 tree fns
= BASELINK_FUNCTIONS (baselink
);
15100 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15102 template_id_p
= true;
15103 template_args
= TREE_OPERAND (fns
, 1);
15104 fns
= TREE_OPERAND (fns
, 0);
15106 template_args
= tsubst_template_args (template_args
, args
,
15107 complain
, in_decl
);
15110 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15111 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15112 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15116 tree name
= OVL_NAME (fns
);
15117 if (IDENTIFIER_CONV_OP_P (name
))
15118 name
= make_conv_op_name (optype
);
15120 if (name
== complete_dtor_identifier
)
15121 /* Treat as-if non-dependent below. */
15122 dependent_p
= false;
15124 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15127 if ((complain
& tf_error
)
15128 && constructor_name_p (name
, qualifying_scope
))
15129 error ("cannot call constructor %<%T::%D%> directly",
15130 qualifying_scope
, name
);
15131 return error_mark_node
;
15134 if (BASELINK_P (baselink
))
15135 fns
= BASELINK_FUNCTIONS (baselink
);
15138 /* We're going to overwrite pieces below, make a duplicate. */
15139 baselink
= copy_node (baselink
);
15141 /* If lookup found a single function, mark it as used at this point.
15142 (If lookup found multiple functions the one selected later by
15143 overload resolution will be marked as used at that point.) */
15144 if (!template_id_p
&& !really_overloaded_fn (fns
))
15146 tree fn
= OVL_FIRST (fns
);
15147 bool ok
= mark_used (fn
, complain
);
15148 if (!ok
&& !(complain
& tf_error
))
15149 return error_mark_node
;
15150 if (ok
&& BASELINK_P (baselink
))
15151 /* We might have instantiated an auto function. */
15152 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15155 if (BASELINK_P (baselink
))
15157 /* Add back the template arguments, if present. */
15159 BASELINK_FUNCTIONS (baselink
)
15160 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15162 /* Update the conversion operator type. */
15163 BASELINK_OPTYPE (baselink
) = optype
;
15167 object_type
= current_class_type
;
15169 if (qualified_p
|| !dependent_p
)
15171 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15175 /* We need to call adjust_result_of_qualified_name_lookup in case the
15176 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15177 so that we still get virtual function binding. */
15178 BASELINK_QUALIFIED_P (baselink
) = false;
15184 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15185 true if the qualified-id will be a postfix-expression in-and-of
15186 itself; false if more of the postfix-expression follows the
15187 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15191 tsubst_qualified_id (tree qualified_id
, tree args
,
15192 tsubst_flags_t complain
, tree in_decl
,
15193 bool done
, bool address_p
)
15199 tree template_args
;
15200 location_t loc
= UNKNOWN_LOCATION
;
15202 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15204 /* Figure out what name to look up. */
15205 name
= TREE_OPERAND (qualified_id
, 1);
15206 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15208 is_template
= true;
15209 loc
= EXPR_LOCATION (name
);
15210 template_args
= TREE_OPERAND (name
, 1);
15212 template_args
= tsubst_template_args (template_args
, args
,
15213 complain
, in_decl
);
15214 if (template_args
== error_mark_node
)
15215 return error_mark_node
;
15216 name
= TREE_OPERAND (name
, 0);
15220 is_template
= false;
15221 template_args
= NULL_TREE
;
15224 /* Substitute into the qualifying scope. When there are no ARGS, we
15225 are just trying to simplify a non-dependent expression. In that
15226 case the qualifying scope may be dependent, and, in any case,
15227 substituting will not help. */
15228 scope
= TREE_OPERAND (qualified_id
, 0);
15231 scope
= tsubst (scope
, args
, complain
, in_decl
);
15232 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15237 if (dependent_scope_p (scope
))
15240 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15241 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15242 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15243 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15247 if (!BASELINK_P (name
) && !DECL_P (expr
))
15249 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15251 /* A BIT_NOT_EXPR is used to represent a destructor. */
15252 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15254 error ("qualifying type %qT does not match destructor name ~%qT",
15255 scope
, TREE_OPERAND (expr
, 0));
15256 expr
= error_mark_node
;
15259 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15260 /*is_type_p=*/0, false);
15263 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15264 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15265 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15267 if (complain
& tf_error
)
15269 error ("dependent-name %qE is parsed as a non-type, but "
15270 "instantiation yields a type", qualified_id
);
15271 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15273 return error_mark_node
;
15279 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15281 /* Remember that there was a reference to this entity. */
15282 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15283 return error_mark_node
;
15286 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15288 if (complain
& tf_error
)
15289 qualified_name_lookup_error (scope
,
15290 TREE_OPERAND (qualified_id
, 1),
15291 expr
, input_location
);
15292 return error_mark_node
;
15297 /* We may be repeating a check already done during parsing, but
15298 if it was well-formed and passed then, it will pass again
15299 now, and if it didn't, we wouldn't have got here. The case
15300 we want to catch is when we couldn't tell then, and can now,
15301 namely when templ prior to substitution was an
15303 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15304 return error_mark_node
;
15306 if (variable_template_p (expr
))
15307 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15310 expr
= lookup_template_function (expr
, template_args
);
15313 if (expr
== error_mark_node
&& complain
& tf_error
)
15314 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15315 expr
, input_location
);
15316 else if (TYPE_P (scope
))
15318 expr
= (adjust_result_of_qualified_name_lookup
15319 (expr
, scope
, current_nonlambda_class_type ()));
15320 expr
= (finish_qualified_id_expr
15321 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15322 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15323 /*template_arg_p=*/false, complain
));
15326 /* Expressions do not generally have reference type. */
15327 if (TREE_CODE (expr
) != SCOPE_REF
15328 /* However, if we're about to form a pointer-to-member, we just
15329 want the referenced member referenced. */
15330 && TREE_CODE (expr
) != OFFSET_REF
)
15331 expr
= convert_from_reference (expr
);
15333 if (REF_PARENTHESIZED_P (qualified_id
))
15334 expr
= force_paren_expr (expr
);
15339 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15340 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15344 tsubst_init (tree init
, tree decl
, tree args
,
15345 tsubst_flags_t complain
, tree in_decl
)
15350 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15352 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
15354 /* If we had an initializer but it
15355 instantiated to nothing,
15356 value-initialize the object. This will
15357 only occur when the initializer was a
15358 pack expansion where the parameter packs
15359 used in that expansion were of length
15361 init
= build_value_init (TREE_TYPE (decl
),
15363 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15364 init
= get_target_expr_sfinae (init
, complain
);
15365 if (TREE_CODE (init
) == TARGET_EXPR
)
15366 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15372 /* Like tsubst, but deals with expressions. This function just replaces
15373 template parms; to finish processing the resultant expression, use
15374 tsubst_copy_and_build or tsubst_expr. */
15377 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15379 enum tree_code code
;
15382 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15385 code
= TREE_CODE (t
);
15390 r
= retrieve_local_specialization (t
);
15392 if (r
== NULL_TREE
)
15394 /* We get here for a use of 'this' in an NSDMI. */
15395 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15396 return current_class_ptr
;
15398 /* This can happen for a parameter name used later in a function
15399 declaration (such as in a late-specified return type). Just
15400 make a dummy decl, since it's only used for its type. */
15401 gcc_assert (cp_unevaluated_operand
!= 0);
15402 r
= tsubst_decl (t
, args
, complain
);
15403 /* Give it the template pattern as its context; its true context
15404 hasn't been instantiated yet and this is good enough for
15406 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15409 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15410 r
= argument_pack_select_arg (r
);
15411 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15412 return error_mark_node
;
15420 if (DECL_TEMPLATE_PARM_P (t
))
15421 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15422 /* There is no need to substitute into namespace-scope
15424 if (DECL_NAMESPACE_SCOPE_P (t
))
15426 /* If ARGS is NULL, then T is known to be non-dependent. */
15427 if (args
== NULL_TREE
)
15428 return scalar_constant_value (t
);
15430 /* Unfortunately, we cannot just call lookup_name here.
15433 template <int I> int f() {
15435 struct S { void g() { E e = a; } };
15438 When we instantiate f<7>::S::g(), say, lookup_name is not
15439 clever enough to find f<7>::a. */
15441 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15442 /*entering_scope=*/0);
15444 for (v
= TYPE_VALUES (enum_type
);
15446 v
= TREE_CHAIN (v
))
15447 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15448 return TREE_VALUE (v
);
15450 /* We didn't find the name. That should never happen; if
15451 name-lookup found it during preliminary parsing, we
15452 should find it again here during instantiation. */
15453 gcc_unreachable ();
15458 if (DECL_CONTEXT (t
))
15462 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15463 /*entering_scope=*/1);
15464 if (ctx
!= DECL_CONTEXT (t
))
15466 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15469 if (complain
& tf_error
)
15470 error ("using invalid field %qD", t
);
15471 return error_mark_node
;
15480 case FUNCTION_DECL
:
15481 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15482 r
= tsubst (t
, args
, complain
, in_decl
);
15483 else if (local_variable_p (t
)
15484 && uses_template_parms (DECL_CONTEXT (t
)))
15486 r
= retrieve_local_specialization (t
);
15487 if (r
== NULL_TREE
)
15489 /* First try name lookup to find the instantiation. */
15490 r
= lookup_name (DECL_NAME (t
));
15491 if (r
&& !is_capture_proxy (r
))
15493 /* Make sure that the one we found is the one we want. */
15494 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15495 if (ctx
!= DECL_CONTEXT (r
))
15503 /* This can happen for a variable used in a
15504 late-specified return type of a local lambda, or for a
15505 local static or constant. Building a new VAR_DECL
15506 should be OK in all those cases. */
15507 r
= tsubst_decl (t
, args
, complain
);
15508 if (local_specializations
)
15509 /* Avoid infinite recursion (79640). */
15510 register_local_specialization (r
, t
);
15511 if (decl_maybe_constant_var_p (r
))
15513 /* We can't call cp_finish_decl, so handle the
15514 initializer by hand. */
15515 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15516 complain
, in_decl
);
15517 if (!processing_template_decl
)
15518 init
= maybe_constant_init (init
);
15519 if (processing_template_decl
15520 ? potential_constant_expression (init
)
15521 : reduced_constant_expression_p (init
))
15522 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15523 = TREE_CONSTANT (r
) = true;
15524 DECL_INITIAL (r
) = init
;
15525 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15527 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15528 complain
, adc_variable_type
);
15530 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15531 || decl_constant_var_p (r
)
15532 || errorcount
|| sorrycount
);
15533 if (!processing_template_decl
15534 && !TREE_STATIC (r
))
15535 r
= process_outer_var_ref (r
, complain
);
15537 /* Remember this for subsequent uses. */
15538 if (local_specializations
)
15539 register_local_specialization (r
, t
);
15541 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15542 r
= argument_pack_select_arg (r
);
15546 if (!mark_used (r
, complain
))
15547 return error_mark_node
;
15550 case NAMESPACE_DECL
:
15557 return tsubst_baselink (t
, current_nonlambda_class_type (),
15558 args
, complain
, in_decl
);
15560 case TEMPLATE_DECL
:
15561 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15562 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15563 args
, complain
, in_decl
);
15564 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15565 return tsubst (t
, args
, complain
, in_decl
);
15566 else if (DECL_CLASS_SCOPE_P (t
)
15567 && uses_template_parms (DECL_CONTEXT (t
)))
15569 /* Template template argument like the following example need
15572 template <template <class> class TT> struct C {};
15573 template <class T> struct D {
15574 template <class U> struct E {};
15579 We are processing the template argument `E' in #1 for
15580 the template instantiation #2. Originally, `E' is a
15581 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15582 have to substitute this with one having context `D<int>'. */
15584 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15585 if (dependent_scope_p (context
))
15587 /* When rewriting a constructor into a deduction guide, a
15588 non-dependent name can become dependent, so memtmpl<args>
15589 becomes context::template memtmpl<args>. */
15590 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15591 return build_qualified_name (type
, context
, DECL_NAME (t
),
15594 return lookup_field (context
, DECL_NAME(t
), 0, false);
15597 /* Ordinary template template argument. */
15600 case NON_LVALUE_EXPR
:
15601 case VIEW_CONVERT_EXPR
:
15603 /* Handle location wrappers by substituting the wrapped node
15604 first, *then* reusing the resulting type. Doing the type
15605 first ensures that we handle template parameters and
15606 parameter pack expansions. */
15607 if (location_wrapper_p (t
))
15609 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
15610 complain
, in_decl
);
15611 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15613 tree op
= TREE_OPERAND (t
, 0);
15614 if (code
== VIEW_CONVERT_EXPR
15615 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15617 /* Wrapper to make a C++20 template parameter object const. */
15618 op
= tsubst_copy (op
, args
, complain
, in_decl
);
15619 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15621 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15622 return build1 (code
, type
, op
);
15626 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
15630 /* We shouldn't see any other uses of these in templates. */
15631 gcc_unreachable ();
15635 case REINTERPRET_CAST_EXPR
:
15636 case CONST_CAST_EXPR
:
15637 case STATIC_CAST_EXPR
:
15638 case DYNAMIC_CAST_EXPR
:
15639 case IMPLICIT_CONV_EXPR
:
15643 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15644 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15645 return build1 (code
, type
, op0
);
15649 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15650 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15652 tree expanded
, op
= TREE_OPERAND (t
, 0);
15655 if (SIZEOF_EXPR_TYPE_P (t
))
15656 op
= TREE_TYPE (op
);
15658 ++cp_unevaluated_operand
;
15659 ++c_inhibit_evaluation_warnings
;
15660 /* We only want to compute the number of arguments. */
15661 if (PACK_EXPANSION_P (op
))
15662 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15664 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15665 args
, complain
, in_decl
);
15666 --cp_unevaluated_operand
;
15667 --c_inhibit_evaluation_warnings
;
15669 if (TREE_CODE (expanded
) == TREE_VEC
)
15671 len
= TREE_VEC_LENGTH (expanded
);
15672 /* Set TREE_USED for the benefit of -Wunused. */
15673 for (int i
= 0; i
< len
; i
++)
15674 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15675 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15678 if (expanded
== error_mark_node
)
15679 return error_mark_node
;
15680 else if (PACK_EXPANSION_P (expanded
)
15681 || (TREE_CODE (expanded
) == TREE_VEC
15682 && pack_expansion_args_count (expanded
)))
15685 if (PACK_EXPANSION_P (expanded
))
15687 else if (TREE_VEC_LENGTH (expanded
) == 1)
15688 expanded
= TREE_VEC_ELT (expanded
, 0);
15690 expanded
= make_argument_pack (expanded
);
15692 if (TYPE_P (expanded
))
15693 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15695 complain
& tf_error
);
15697 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15698 complain
& tf_error
);
15701 return build_int_cst (size_type_node
, len
);
15703 if (SIZEOF_EXPR_TYPE_P (t
))
15705 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15706 args
, complain
, in_decl
);
15707 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15708 r
= build1 (SIZEOF_EXPR
,
15709 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15710 SIZEOF_EXPR_TYPE_P (r
) = 1;
15717 case TRUTH_NOT_EXPR
:
15720 case UNARY_PLUS_EXPR
: /* Unary + */
15722 case AT_ENCODE_EXPR
:
15726 case REALPART_EXPR
:
15727 case IMAGPART_EXPR
:
15730 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15731 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15732 r
= build1 (code
, type
, op0
);
15733 if (code
== ALIGNOF_EXPR
)
15734 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15738 case COMPONENT_REF
:
15743 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15744 name
= TREE_OPERAND (t
, 1);
15745 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15747 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15748 complain
, in_decl
);
15749 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15751 else if (TREE_CODE (name
) == SCOPE_REF
15752 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15754 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15755 complain
, in_decl
);
15756 name
= TREE_OPERAND (name
, 1);
15757 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15758 complain
, in_decl
);
15759 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15760 name
= build_qualified_name (/*type=*/NULL_TREE
,
15762 /*template_p=*/false);
15764 else if (BASELINK_P (name
))
15765 name
= tsubst_baselink (name
,
15766 non_reference (TREE_TYPE (object
)),
15770 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15771 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15777 case TRUNC_DIV_EXPR
:
15778 case CEIL_DIV_EXPR
:
15779 case FLOOR_DIV_EXPR
:
15780 case ROUND_DIV_EXPR
:
15781 case EXACT_DIV_EXPR
:
15785 case TRUNC_MOD_EXPR
:
15786 case FLOOR_MOD_EXPR
:
15787 case TRUTH_ANDIF_EXPR
:
15788 case TRUTH_ORIF_EXPR
:
15789 case TRUTH_AND_EXPR
:
15790 case TRUTH_OR_EXPR
:
15803 case COMPOUND_EXPR
:
15806 case PREDECREMENT_EXPR
:
15807 case PREINCREMENT_EXPR
:
15808 case POSTDECREMENT_EXPR
:
15809 case POSTINCREMENT_EXPR
:
15811 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15812 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15813 return build_nt (code
, op0
, op1
);
15818 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15819 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15820 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15821 QUALIFIED_NAME_IS_TEMPLATE (t
));
15826 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15827 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15828 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15833 int n
= VL_EXP_OPERAND_LENGTH (t
);
15834 tree result
= build_vl_exp (CALL_EXPR
, n
);
15836 for (i
= 0; i
< n
; i
++)
15837 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15838 complain
, in_decl
);
15844 case PSEUDO_DTOR_EXPR
:
15845 case VEC_PERM_EXPR
:
15847 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15848 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15849 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15850 r
= build_nt (code
, op0
, op1
, op2
);
15851 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15857 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15858 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15859 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15860 r
= build_nt (code
, op0
, op1
, op2
);
15861 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15867 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15868 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15869 r
= build_nt (code
, op0
, op1
);
15870 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15871 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15875 case TEMPLATE_ID_EXPR
:
15877 /* Substituted template arguments */
15878 tree fn
= TREE_OPERAND (t
, 0);
15879 tree targs
= TREE_OPERAND (t
, 1);
15881 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15883 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15885 return lookup_template_function (fn
, targs
);
15890 tree purpose
, value
, chain
;
15892 if (t
== void_list_node
)
15895 purpose
= TREE_PURPOSE (t
);
15897 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15898 value
= TREE_VALUE (t
);
15900 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15901 chain
= TREE_CHAIN (t
);
15902 if (chain
&& chain
!= void_type_node
)
15903 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15904 if (purpose
== TREE_PURPOSE (t
)
15905 && value
== TREE_VALUE (t
)
15906 && chain
== TREE_CHAIN (t
))
15908 return tree_cons (purpose
, value
, chain
);
15913 case ENUMERAL_TYPE
:
15915 case TEMPLATE_TYPE_PARM
:
15916 case TEMPLATE_TEMPLATE_PARM
:
15917 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15918 case TEMPLATE_PARM_INDEX
:
15920 case REFERENCE_TYPE
:
15922 case FUNCTION_TYPE
:
15925 case TYPENAME_TYPE
:
15926 case UNBOUND_CLASS_TEMPLATE
:
15928 case DECLTYPE_TYPE
:
15930 return tsubst (t
, args
, complain
, in_decl
);
15934 /* Fall through. */
15935 case IDENTIFIER_NODE
:
15936 if (IDENTIFIER_CONV_OP_P (t
))
15938 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15939 return make_conv_op_name (new_type
);
15945 /* This is handled by tsubst_copy_and_build. */
15946 gcc_unreachable ();
15950 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15951 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15952 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15955 case CLEANUP_POINT_EXPR
:
15956 /* We shouldn't have built any of these during initial template
15957 generation. Instead, they should be built during instantiation
15958 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15959 gcc_unreachable ();
15963 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15964 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15965 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15966 r
= build2 (code
, type
, op0
, op1
);
15967 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15968 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15969 && !(complain
& tf_error
))
15970 return error_mark_node
;
15974 case EXPR_PACK_EXPANSION
:
15975 error ("invalid use of pack expansion expression");
15976 return error_mark_node
;
15978 case NONTYPE_ARGUMENT_PACK
:
15979 error ("use %<...%> to expand argument pack");
15980 return error_mark_node
;
15983 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15991 /* Instantiate any typedefs in the type. */
15992 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15993 r
= fold_convert (type
, t
);
15994 gcc_assert (TREE_CODE (r
) == code
);
15999 /* These can sometimes show up in a partial instantiation, but never
16000 involve template parms. */
16001 gcc_assert (!uses_template_parms (t
));
16004 case UNARY_LEFT_FOLD_EXPR
:
16005 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16006 case UNARY_RIGHT_FOLD_EXPR
:
16007 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16008 case BINARY_LEFT_FOLD_EXPR
:
16009 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16010 case BINARY_RIGHT_FOLD_EXPR
:
16011 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16015 case DEBUG_BEGIN_STMT
:
16016 /* ??? There's no point in copying it for now, but maybe some
16017 day it will contain more information, such as a pointer back
16018 to the containing function, inlined copy or so. */
16022 /* We shouldn't get here, but keep going if !flag_checking. */
16024 gcc_unreachable ();
16029 /* Helper function for tsubst_omp_clauses, used for instantiation of
16030 OMP_CLAUSE_DECL of clauses. */
16033 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16034 tree in_decl
, tree
*iterator_cache
)
16036 if (decl
== NULL_TREE
)
16039 /* Handle OpenMP iterators. */
16040 if (TREE_CODE (decl
) == TREE_LIST
16041 && TREE_PURPOSE (decl
)
16042 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16045 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16046 ret
= iterator_cache
[1];
16050 begin_scope (sk_omp
, NULL
);
16051 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16053 *tp
= copy_node (it
);
16054 TREE_VEC_ELT (*tp
, 0)
16055 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16056 TREE_VEC_ELT (*tp
, 1)
16057 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16058 /*integral_constant_expression_p=*/false);
16059 TREE_VEC_ELT (*tp
, 2)
16060 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16061 /*integral_constant_expression_p=*/false);
16062 TREE_VEC_ELT (*tp
, 3)
16063 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16064 /*integral_constant_expression_p=*/false);
16065 TREE_CHAIN (*tp
) = NULL_TREE
;
16066 tp
= &TREE_CHAIN (*tp
);
16068 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16069 iterator_cache
[0] = TREE_PURPOSE (decl
);
16070 iterator_cache
[1] = ret
;
16072 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16077 /* Handle an OpenMP array section represented as a TREE_LIST (or
16078 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16079 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16080 TREE_LIST. We can handle it exactly the same as an array section
16081 (purpose, value, and a chain), even though the nomenclature
16082 (low_bound, length, etc) is different. */
16083 if (TREE_CODE (decl
) == TREE_LIST
)
16086 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16087 /*integral_constant_expression_p=*/false);
16088 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16089 /*integral_constant_expression_p=*/false);
16090 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16092 if (TREE_PURPOSE (decl
) == low_bound
16093 && TREE_VALUE (decl
) == length
16094 && TREE_CHAIN (decl
) == chain
)
16096 tree ret
= tree_cons (low_bound
, length
, chain
);
16097 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16098 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16101 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16102 /*integral_constant_expression_p=*/false);
16103 /* Undo convert_from_reference tsubst_expr could have called. */
16105 && REFERENCE_REF_P (ret
)
16106 && !REFERENCE_REF_P (decl
))
16107 ret
= TREE_OPERAND (ret
, 0);
16111 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16114 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16115 tree args
, tsubst_flags_t complain
, tree in_decl
)
16117 tree new_clauses
= NULL_TREE
, nc
, oc
;
16118 tree linear_no_step
= NULL_TREE
;
16119 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16121 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16123 nc
= copy_node (oc
);
16124 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16127 switch (OMP_CLAUSE_CODE (nc
))
16129 case OMP_CLAUSE_LASTPRIVATE
:
16130 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16132 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16133 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16134 in_decl
, /*integral_constant_expression_p=*/false);
16135 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16136 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16139 case OMP_CLAUSE_PRIVATE
:
16140 case OMP_CLAUSE_SHARED
:
16141 case OMP_CLAUSE_FIRSTPRIVATE
:
16142 case OMP_CLAUSE_COPYIN
:
16143 case OMP_CLAUSE_COPYPRIVATE
:
16144 case OMP_CLAUSE_UNIFORM
:
16145 case OMP_CLAUSE_DEPEND
:
16146 case OMP_CLAUSE_FROM
:
16147 case OMP_CLAUSE_TO
:
16148 case OMP_CLAUSE_MAP
:
16149 case OMP_CLAUSE_NONTEMPORAL
:
16150 case OMP_CLAUSE_USE_DEVICE_PTR
:
16151 case OMP_CLAUSE_IS_DEVICE_PTR
:
16152 OMP_CLAUSE_DECL (nc
)
16153 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16154 in_decl
, iterator_cache
);
16156 case OMP_CLAUSE_TILE
:
16157 case OMP_CLAUSE_IF
:
16158 case OMP_CLAUSE_NUM_THREADS
:
16159 case OMP_CLAUSE_SCHEDULE
:
16160 case OMP_CLAUSE_COLLAPSE
:
16161 case OMP_CLAUSE_FINAL
:
16162 case OMP_CLAUSE_DEVICE
:
16163 case OMP_CLAUSE_DIST_SCHEDULE
:
16164 case OMP_CLAUSE_NUM_TEAMS
:
16165 case OMP_CLAUSE_THREAD_LIMIT
:
16166 case OMP_CLAUSE_SAFELEN
:
16167 case OMP_CLAUSE_SIMDLEN
:
16168 case OMP_CLAUSE_NUM_TASKS
:
16169 case OMP_CLAUSE_GRAINSIZE
:
16170 case OMP_CLAUSE_PRIORITY
:
16171 case OMP_CLAUSE_ORDERED
:
16172 case OMP_CLAUSE_HINT
:
16173 case OMP_CLAUSE_NUM_GANGS
:
16174 case OMP_CLAUSE_NUM_WORKERS
:
16175 case OMP_CLAUSE_VECTOR_LENGTH
:
16176 case OMP_CLAUSE_WORKER
:
16177 case OMP_CLAUSE_VECTOR
:
16178 case OMP_CLAUSE_ASYNC
:
16179 case OMP_CLAUSE_WAIT
:
16180 OMP_CLAUSE_OPERAND (nc
, 0)
16181 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16182 in_decl
, /*integral_constant_expression_p=*/false);
16184 case OMP_CLAUSE_REDUCTION
:
16185 case OMP_CLAUSE_IN_REDUCTION
:
16186 case OMP_CLAUSE_TASK_REDUCTION
:
16187 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16189 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16190 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16192 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16193 complain
, in_decl
);
16194 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16195 = build_qualified_name (NULL_TREE
, scope
,
16196 TREE_OPERAND (placeholder
, 1),
16200 gcc_assert (identifier_p (placeholder
));
16202 OMP_CLAUSE_DECL (nc
)
16203 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16206 case OMP_CLAUSE_GANG
:
16207 case OMP_CLAUSE_ALIGNED
:
16208 OMP_CLAUSE_DECL (nc
)
16209 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16211 OMP_CLAUSE_OPERAND (nc
, 1)
16212 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16213 in_decl
, /*integral_constant_expression_p=*/false);
16215 case OMP_CLAUSE_LINEAR
:
16216 OMP_CLAUSE_DECL (nc
)
16217 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16219 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16221 gcc_assert (!linear_no_step
);
16222 linear_no_step
= nc
;
16224 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16225 OMP_CLAUSE_LINEAR_STEP (nc
)
16226 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16227 complain
, in_decl
, NULL
);
16229 OMP_CLAUSE_LINEAR_STEP (nc
)
16230 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16232 /*integral_constant_expression_p=*/false);
16234 case OMP_CLAUSE_NOWAIT
:
16235 case OMP_CLAUSE_DEFAULT
:
16236 case OMP_CLAUSE_UNTIED
:
16237 case OMP_CLAUSE_MERGEABLE
:
16238 case OMP_CLAUSE_INBRANCH
:
16239 case OMP_CLAUSE_NOTINBRANCH
:
16240 case OMP_CLAUSE_PROC_BIND
:
16241 case OMP_CLAUSE_FOR
:
16242 case OMP_CLAUSE_PARALLEL
:
16243 case OMP_CLAUSE_SECTIONS
:
16244 case OMP_CLAUSE_TASKGROUP
:
16245 case OMP_CLAUSE_NOGROUP
:
16246 case OMP_CLAUSE_THREADS
:
16247 case OMP_CLAUSE_SIMD
:
16248 case OMP_CLAUSE_DEFAULTMAP
:
16249 case OMP_CLAUSE_INDEPENDENT
:
16250 case OMP_CLAUSE_AUTO
:
16251 case OMP_CLAUSE_SEQ
:
16252 case OMP_CLAUSE_IF_PRESENT
:
16253 case OMP_CLAUSE_FINALIZE
:
16256 gcc_unreachable ();
16258 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16259 switch (OMP_CLAUSE_CODE (nc
))
16261 case OMP_CLAUSE_SHARED
:
16262 case OMP_CLAUSE_PRIVATE
:
16263 case OMP_CLAUSE_FIRSTPRIVATE
:
16264 case OMP_CLAUSE_LASTPRIVATE
:
16265 case OMP_CLAUSE_COPYPRIVATE
:
16266 case OMP_CLAUSE_LINEAR
:
16267 case OMP_CLAUSE_REDUCTION
:
16268 case OMP_CLAUSE_IN_REDUCTION
:
16269 case OMP_CLAUSE_TASK_REDUCTION
:
16270 case OMP_CLAUSE_USE_DEVICE_PTR
:
16271 case OMP_CLAUSE_IS_DEVICE_PTR
:
16272 /* tsubst_expr on SCOPE_REF results in returning
16273 finish_non_static_data_member result. Undo that here. */
16274 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16275 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16276 == IDENTIFIER_NODE
))
16278 tree t
= OMP_CLAUSE_DECL (nc
);
16281 switch (TREE_CODE (v
))
16283 case COMPONENT_REF
:
16287 case POINTER_PLUS_EXPR
:
16288 v
= TREE_OPERAND (v
, 0);
16291 if (DECL_CONTEXT (v
) == current_function_decl
16292 && DECL_ARTIFICIAL (v
)
16293 && DECL_NAME (v
) == this_identifier
)
16294 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16301 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16302 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16303 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16304 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16305 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16307 tree decl
= OMP_CLAUSE_DECL (nc
);
16310 retrofit_lang_decl (decl
);
16311 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16320 new_clauses
= nreverse (new_clauses
);
16321 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16323 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16324 if (linear_no_step
)
16325 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16326 if (nc
== linear_no_step
)
16328 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16332 return new_clauses
;
16335 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16338 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16341 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16343 tree purpose
, value
, chain
;
16348 if (TREE_CODE (t
) != TREE_LIST
)
16349 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16350 /*function_p=*/false,
16351 /*integral_constant_expression_p=*/false);
16353 if (t
== void_list_node
)
16356 purpose
= TREE_PURPOSE (t
);
16358 purpose
= RECUR (purpose
);
16359 value
= TREE_VALUE (t
);
16362 if (TREE_CODE (value
) != LABEL_DECL
)
16363 value
= RECUR (value
);
16366 value
= lookup_label (DECL_NAME (value
));
16367 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16368 TREE_USED (value
) = 1;
16371 chain
= TREE_CHAIN (t
);
16372 if (chain
&& chain
!= void_type_node
)
16373 chain
= RECUR (chain
);
16374 return tree_cons (purpose
, value
, chain
);
16378 /* Used to temporarily communicate the list of #pragma omp parallel
16379 clauses to #pragma omp for instantiation if they are combined
16382 static tree
*omp_parallel_combined_clauses
;
16384 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
16385 tree
*, unsigned int *);
16387 /* Substitute one OMP_FOR iterator. */
16390 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
16391 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16392 tree args
, tsubst_flags_t complain
, tree in_decl
,
16393 bool integral_constant_expression_p
)
16395 #define RECUR(NODE) \
16396 tsubst_expr ((NODE), args, complain, in_decl, \
16397 integral_constant_expression_p)
16398 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
16401 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16402 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16404 decl
= TREE_OPERAND (init
, 0);
16405 init
= TREE_OPERAND (init
, 1);
16406 tree decl_expr
= NULL_TREE
;
16407 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
16410 bool decomp
= false;
16411 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
16413 tree v
= DECL_VALUE_EXPR (decl
);
16414 if (TREE_CODE (v
) == ARRAY_REF
16415 && VAR_P (TREE_OPERAND (v
, 0))
16416 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
16418 tree decomp_first
= NULL_TREE
;
16419 unsigned decomp_cnt
= 0;
16420 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
16421 maybe_push_decl (d
);
16422 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
16423 in_decl
, &decomp_first
, &decomp_cnt
);
16425 if (d
== error_mark_node
)
16426 decl
= error_mark_node
;
16428 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
16430 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
16432 tree v
= build_nt (ARRAY_REF
, d
,
16433 size_int (decomp_cnt
- i
- 1),
16434 NULL_TREE
, NULL_TREE
);
16435 SET_DECL_VALUE_EXPR (decomp_first
, v
);
16436 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
16438 fit_decomposition_lang_decl (decomp_first
, d
);
16439 decomp_first
= DECL_CHAIN (decomp_first
);
16443 decl
= tsubst_decl (decl
, args
, complain
);
16445 maybe_push_decl (decl
);
16447 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16449 /* We need to jump through some hoops to handle declarations in the
16450 init-statement, since we might need to handle auto deduction,
16451 but we need to keep control of initialization. */
16453 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16454 decl
= tsubst_decl (decl
, args
, complain
);
16458 if (TREE_CODE (decl
) == SCOPE_REF
)
16460 decl
= RECUR (decl
);
16461 if (TREE_CODE (decl
) == COMPONENT_REF
)
16465 switch (TREE_CODE (v
))
16467 case COMPONENT_REF
:
16471 case POINTER_PLUS_EXPR
:
16472 v
= TREE_OPERAND (v
, 0);
16475 if (DECL_CONTEXT (v
) == current_function_decl
16476 && DECL_ARTIFICIAL (v
)
16477 && DECL_NAME (v
) == this_identifier
)
16479 decl
= TREE_OPERAND (decl
, 1);
16480 decl
= omp_privatize_field (decl
, false);
16490 decl
= RECUR (decl
);
16492 init
= RECUR (init
);
16494 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16496 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16497 if (TREE_CODE (o
) == TREE_LIST
)
16498 TREE_VEC_ELT (orig_declv
, i
)
16499 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16500 RECUR (TREE_VALUE (o
)),
16503 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16508 tree this_pre_body
= NULL_TREE
;
16509 tree orig_init
= NULL_TREE
;
16510 tree orig_decl
= NULL_TREE
;
16511 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
16512 orig_init
, cond
, incr
);
16515 if (orig_declv
== NULL_TREE
)
16516 orig_declv
= copy_node (declv
);
16517 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
16520 else if (orig_declv
)
16521 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16524 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16525 if (!range_for
&& auto_node
&& init
)
16527 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16529 gcc_assert (!type_dependent_expression_p (decl
));
16531 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
16535 /* Declare the variable, but don't let that initialize it. */
16536 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16537 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16539 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16544 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16545 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16546 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16548 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16549 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16550 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16551 NOP_EXPR
, rhs
, complain
);
16554 incr
= RECUR (incr
);
16555 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16556 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16558 TREE_VEC_ELT (declv
, i
) = decl
;
16559 TREE_VEC_ELT (initv
, i
) = init
;
16560 TREE_VEC_ELT (condv
, i
) = cond
;
16561 TREE_VEC_ELT (incrv
, i
) = incr
;
16567 /* Declare and initialize the variable. */
16575 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16577 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16579 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16580 && OMP_CLAUSE_DECL (*pc
) == decl
)
16582 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16583 && OMP_CLAUSE_DECL (*pc
) == decl
)
16587 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16589 *pc
= OMP_CLAUSE_CHAIN (c
);
16590 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16593 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16594 && OMP_CLAUSE_DECL (*pc
) == decl
)
16596 error ("iteration variable %qD should not be firstprivate",
16598 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16600 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16601 && OMP_CLAUSE_DECL (*pc
) == decl
)
16603 error ("iteration variable %qD should not be reduction",
16605 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16608 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16613 if (*pc
== NULL_TREE
)
16615 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16616 OMP_CLAUSE_DECL (c
) = decl
;
16617 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16620 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16625 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16626 if (COMPARISON_CLASS_P (cond
))
16628 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16629 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16630 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16633 cond
= RECUR (cond
);
16634 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16635 switch (TREE_CODE (incr
))
16637 case PREINCREMENT_EXPR
:
16638 case PREDECREMENT_EXPR
:
16639 case POSTINCREMENT_EXPR
:
16640 case POSTDECREMENT_EXPR
:
16641 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16642 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16645 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16646 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16648 tree rhs
= TREE_OPERAND (incr
, 1);
16649 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16650 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16651 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16652 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16653 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16657 incr
= RECUR (incr
);
16660 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16661 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16663 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16664 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16665 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16666 TREE_TYPE (decl
), lhs
,
16667 RECUR (TREE_OPERAND (incr
, 2))));
16669 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16670 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16671 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16673 tree rhs
= TREE_OPERAND (incr
, 2);
16674 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16675 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16676 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16677 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16678 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16682 incr
= RECUR (incr
);
16685 incr
= RECUR (incr
);
16689 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16690 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16691 TREE_VEC_ELT (declv
, i
) = decl
;
16692 TREE_VEC_ELT (initv
, i
) = init
;
16693 TREE_VEC_ELT (condv
, i
) = cond
;
16694 TREE_VEC_ELT (incrv
, i
) = incr
;
16699 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16700 of OMP_TARGET's body. */
16703 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16705 *walk_subtrees
= 0;
16706 switch (TREE_CODE (*tp
))
16711 case STATEMENT_LIST
:
16712 *walk_subtrees
= 1;
16720 /* Helper function for tsubst_expr. For decomposition declaration
16721 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16722 also the corresponding decls representing the identifiers
16723 of the decomposition declaration. Return DECL if successful
16724 or error_mark_node otherwise, set *FIRST to the first decl
16725 in the list chained through DECL_CHAIN and *CNT to the number
16729 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16730 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16733 tree decl2
, decl3
, prev
= decl
;
16735 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16736 for (decl2
= DECL_CHAIN (pattern_decl
);
16739 && DECL_DECOMPOSITION_P (decl2
)
16740 && DECL_NAME (decl2
);
16741 decl2
= DECL_CHAIN (decl2
))
16743 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16745 gcc_assert (errorcount
);
16746 return error_mark_node
;
16749 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16750 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16751 tree v
= DECL_VALUE_EXPR (decl2
);
16752 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16753 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16754 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16755 SET_DECL_VALUE_EXPR (decl2
, v
);
16756 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16758 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16761 gcc_assert (errorcount
);
16762 decl
= error_mark_node
;
16765 maybe_push_decl (decl3
);
16766 if (error_operand_p (decl3
))
16767 decl
= error_mark_node
;
16768 else if (decl
!= error_mark_node
16769 && DECL_CHAIN (decl3
) != prev
16772 gcc_assert (errorcount
);
16773 decl
= error_mark_node
;
16782 /* Return the proper local_specialization for init-capture pack DECL. */
16785 lookup_init_capture_pack (tree decl
)
16787 /* We handle normal pack captures by forwarding to the specialization of the
16788 captured parameter. We can't do that for pack init-captures; we need them
16789 to have their own local_specialization. We created the individual
16790 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16791 when we process the DECL_EXPR for the pack init-capture in the template.
16792 So, how do we find them? We don't know the capture proxy pack when
16793 building the individual resulting proxies, and we don't know the
16794 individual proxies when instantiating the pack. What we have in common is
16797 So...when we instantiate the FIELD_DECL, we stick the result in
16798 local_specializations. Then at the DECL_EXPR we look up that result, see
16799 how many elements it has, synthesize the names, and look them up. */
16801 tree cname
= DECL_NAME (decl
);
16802 tree val
= DECL_VALUE_EXPR (decl
);
16803 tree field
= TREE_OPERAND (val
, 1);
16804 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
16805 tree fpack
= retrieve_local_specialization (field
);
16806 if (fpack
== error_mark_node
)
16807 return error_mark_node
;
16810 tree vec
= NULL_TREE
;
16811 tree r
= NULL_TREE
;
16812 if (TREE_CODE (fpack
) == TREE_VEC
)
16814 len
= TREE_VEC_LENGTH (fpack
);
16815 vec
= make_tree_vec (len
);
16816 r
= make_node (NONTYPE_ARGUMENT_PACK
);
16817 SET_ARGUMENT_PACK_ARGS (r
, vec
);
16819 for (int i
= 0; i
< len
; ++i
)
16821 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
16822 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
16824 TREE_VEC_ELT (vec
, i
) = elt
;
16831 /* Like tsubst_copy for expressions, etc. but also does semantic
16835 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16836 bool integral_constant_expression_p
)
16838 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16839 #define RECUR(NODE) \
16840 tsubst_expr ((NODE), args, complain, in_decl, \
16841 integral_constant_expression_p)
16847 if (t
== NULL_TREE
|| t
== error_mark_node
)
16850 loc
= input_location
;
16851 if (location_t eloc
= cp_expr_location (t
))
16852 input_location
= eloc
;
16853 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16854 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16856 switch (TREE_CODE (t
))
16858 case STATEMENT_LIST
:
16860 tree_stmt_iterator i
;
16861 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16862 RECUR (tsi_stmt (i
));
16866 case CTOR_INITIALIZER
:
16867 finish_mem_initializers (tsubst_initializer_list
16868 (TREE_OPERAND (t
, 0), args
));
16872 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16876 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16877 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16878 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16880 finish_expr_stmt (tmp
);
16884 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16885 /*attribs=*/NULL_TREE
);
16890 tree decl
, pattern_decl
;
16893 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16894 if (TREE_CODE (decl
) == LABEL_DECL
)
16895 finish_label_decl (DECL_NAME (decl
));
16896 else if (TREE_CODE (decl
) == USING_DECL
)
16898 tree scope
= USING_DECL_SCOPE (decl
);
16899 tree name
= DECL_NAME (decl
);
16901 scope
= tsubst (scope
, args
, complain
, in_decl
);
16902 decl
= lookup_qualified_name (scope
, name
,
16903 /*is_type_p=*/false,
16904 /*complain=*/false);
16905 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16906 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16908 finish_local_using_decl (decl
, scope
, name
);
16910 else if (is_capture_proxy (decl
)
16911 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16913 /* We're in tsubst_lambda_expr, we've already inserted a new
16914 capture proxy, so look it up and register it. */
16916 if (!DECL_PACK_P (decl
))
16918 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
16919 /*nonclass*/1, /*block_p=*/true,
16920 /*ns_only*/0, LOOKUP_HIDDEN
);
16921 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16923 else if (is_normal_capture_proxy (decl
))
16925 inst
= (retrieve_local_specialization
16926 (DECL_CAPTURED_VARIABLE (decl
)));
16927 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16930 inst
= lookup_init_capture_pack (decl
);
16932 register_local_specialization (inst
, decl
);
16935 else if (DECL_PRETTY_FUNCTION_P (decl
))
16936 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
16938 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16939 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16940 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16941 /* Don't copy the old closure; we'll create a new one in
16942 tsubst_lambda_expr. */
16946 init
= DECL_INITIAL (decl
);
16947 decl
= tsubst (decl
, args
, complain
, in_decl
);
16948 if (decl
!= error_mark_node
)
16950 /* By marking the declaration as instantiated, we avoid
16951 trying to instantiate it. Since instantiate_decl can't
16952 handle local variables, and since we've already done
16953 all that needs to be done, that's the right thing to
16956 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16957 if (VAR_P (decl
) && !DECL_NAME (decl
)
16958 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16959 /* Anonymous aggregates are a special case. */
16960 finish_anon_union (decl
);
16961 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16963 DECL_CONTEXT (decl
) = current_function_decl
;
16964 if (DECL_NAME (decl
) == this_identifier
)
16966 tree lam
= DECL_CONTEXT (current_function_decl
);
16967 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16968 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16970 insert_capture_proxy (decl
);
16972 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16973 /* We already did a pushtag. */;
16974 else if (TREE_CODE (decl
) == FUNCTION_DECL
16975 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16976 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16978 DECL_CONTEXT (decl
) = NULL_TREE
;
16980 DECL_CONTEXT (decl
) = current_function_decl
;
16981 cp_check_omp_declare_reduction (decl
);
16985 int const_init
= false;
16986 unsigned int cnt
= 0;
16987 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
16988 maybe_push_decl (decl
);
16991 && DECL_DECOMPOSITION_P (decl
)
16992 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16993 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
16994 complain
, in_decl
, &first
,
16997 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17000 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17003 if (ndecl
!= error_mark_node
)
17004 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17006 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
17008 if (ndecl
!= error_mark_node
)
17009 cp_finish_decomp (ndecl
, first
, cnt
);
17018 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17019 RECUR (FOR_INIT_STMT (t
));
17020 finish_init_stmt (stmt
);
17021 tmp
= RECUR (FOR_COND (t
));
17022 finish_for_cond (tmp
, stmt
, false, 0);
17023 tmp
= RECUR (FOR_EXPR (t
));
17024 finish_for_expr (tmp
, stmt
);
17026 bool prev
= note_iteration_stmt_body_start ();
17027 RECUR (FOR_BODY (t
));
17028 note_iteration_stmt_body_end (prev
);
17030 finish_for_stmt (stmt
);
17033 case RANGE_FOR_STMT
:
17035 /* Construct another range_for, if this is not a final
17036 substitution (for inside inside a generic lambda of a
17037 template). Otherwise convert to a regular for. */
17039 stmt
= (processing_template_decl
17040 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17041 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17042 RECUR (RANGE_FOR_INIT_STMT (t
));
17043 decl
= RANGE_FOR_DECL (t
);
17044 decl
= tsubst (decl
, args
, complain
, in_decl
);
17045 maybe_push_decl (decl
);
17046 expr
= RECUR (RANGE_FOR_EXPR (t
));
17048 tree decomp_first
= NULL_TREE
;
17049 unsigned decomp_cnt
= 0;
17050 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17051 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17053 &decomp_first
, &decomp_cnt
);
17055 if (processing_template_decl
)
17057 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17058 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17059 finish_range_for_decl (stmt
, decl
, expr
);
17060 if (decomp_first
&& decl
!= error_mark_node
)
17061 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17065 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17066 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17067 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17068 decomp_first
, decomp_cnt
,
17069 RANGE_FOR_IVDEP (t
), unroll
);
17072 bool prev
= note_iteration_stmt_body_start ();
17073 RECUR (RANGE_FOR_BODY (t
));
17074 note_iteration_stmt_body_end (prev
);
17075 finish_for_stmt (stmt
);
17080 stmt
= begin_while_stmt ();
17081 tmp
= RECUR (WHILE_COND (t
));
17082 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17084 bool prev
= note_iteration_stmt_body_start ();
17085 RECUR (WHILE_BODY (t
));
17086 note_iteration_stmt_body_end (prev
);
17088 finish_while_stmt (stmt
);
17092 stmt
= begin_do_stmt ();
17094 bool prev
= note_iteration_stmt_body_start ();
17095 RECUR (DO_BODY (t
));
17096 note_iteration_stmt_body_end (prev
);
17098 finish_do_body (stmt
);
17099 tmp
= RECUR (DO_COND (t
));
17100 finish_do_stmt (tmp
, stmt
, false, 0);
17104 stmt
= begin_if_stmt ();
17105 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17106 if (IF_STMT_CONSTEXPR_P (t
))
17107 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17108 tmp
= RECUR (IF_COND (t
));
17109 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17110 if (IF_STMT_CONSTEXPR_P (t
)
17111 && instantiation_dependent_expression_p (tmp
))
17113 /* We're partially instantiating a generic lambda, but the condition
17114 of the constexpr if is still dependent. Don't substitute into the
17115 branches now, just remember the template arguments. */
17116 do_poplevel (IF_SCOPE (stmt
));
17117 IF_COND (stmt
) = IF_COND (t
);
17118 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17119 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17120 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17124 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17125 /* Don't instantiate the THEN_CLAUSE. */;
17128 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17129 bool inhibit
= integer_zerop (folded
);
17131 ++c_inhibit_evaluation_warnings
;
17132 RECUR (THEN_CLAUSE (t
));
17134 --c_inhibit_evaluation_warnings
;
17136 finish_then_clause (stmt
);
17138 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17139 /* Don't instantiate the ELSE_CLAUSE. */;
17140 else if (ELSE_CLAUSE (t
))
17142 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17143 bool inhibit
= integer_nonzerop (folded
);
17144 begin_else_clause (stmt
);
17146 ++c_inhibit_evaluation_warnings
;
17147 RECUR (ELSE_CLAUSE (t
));
17149 --c_inhibit_evaluation_warnings
;
17150 finish_else_clause (stmt
);
17153 finish_if_stmt (stmt
);
17157 if (BIND_EXPR_BODY_BLOCK (t
))
17158 stmt
= begin_function_body ();
17160 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17161 ? BCS_TRY_BLOCK
: 0);
17163 RECUR (BIND_EXPR_BODY (t
));
17165 if (BIND_EXPR_BODY_BLOCK (t
))
17166 finish_function_body (stmt
);
17168 finish_compound_stmt (stmt
);
17172 finish_break_stmt ();
17175 case CONTINUE_STMT
:
17176 finish_continue_stmt ();
17180 stmt
= begin_switch_stmt ();
17181 tmp
= RECUR (SWITCH_STMT_COND (t
));
17182 finish_switch_cond (tmp
, stmt
);
17183 RECUR (SWITCH_STMT_BODY (t
));
17184 finish_switch_stmt (stmt
);
17187 case CASE_LABEL_EXPR
:
17189 tree decl
= CASE_LABEL (t
);
17190 tree low
= RECUR (CASE_LOW (t
));
17191 tree high
= RECUR (CASE_HIGH (t
));
17192 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17193 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17195 tree label
= CASE_LABEL (l
);
17196 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17197 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17198 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17205 tree decl
= LABEL_EXPR_LABEL (t
);
17208 label
= finish_label_stmt (DECL_NAME (decl
));
17209 if (TREE_CODE (label
) == LABEL_DECL
)
17210 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17211 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17212 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17217 tmp
= GOTO_DESTINATION (t
);
17218 if (TREE_CODE (tmp
) != LABEL_DECL
)
17219 /* Computed goto's must be tsubst'd into. On the other hand,
17220 non-computed gotos must not be; the identifier in question
17221 will have no binding. */
17224 tmp
= DECL_NAME (tmp
);
17225 finish_goto_stmt (tmp
);
17230 tree string
= RECUR (ASM_STRING (t
));
17231 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17232 complain
, in_decl
);
17233 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17234 complain
, in_decl
);
17235 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17236 complain
, in_decl
);
17237 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17238 complain
, in_decl
);
17239 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
17240 clobbers
, labels
, ASM_INLINE_P (t
));
17241 tree asm_expr
= tmp
;
17242 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17243 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17244 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17251 stmt
= begin_try_block ();
17252 RECUR (TRY_STMTS (t
));
17253 finish_cleanup_try_block (stmt
);
17254 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17258 tree compound_stmt
= NULL_TREE
;
17260 if (FN_TRY_BLOCK_P (t
))
17261 stmt
= begin_function_try_block (&compound_stmt
);
17263 stmt
= begin_try_block ();
17265 RECUR (TRY_STMTS (t
));
17267 if (FN_TRY_BLOCK_P (t
))
17268 finish_function_try_block (stmt
);
17270 finish_try_block (stmt
);
17272 RECUR (TRY_HANDLERS (t
));
17273 if (FN_TRY_BLOCK_P (t
))
17274 finish_function_handler_sequence (stmt
, compound_stmt
);
17276 finish_handler_sequence (stmt
);
17282 tree decl
= HANDLER_PARMS (t
);
17286 decl
= tsubst (decl
, args
, complain
, in_decl
);
17287 /* Prevent instantiate_decl from trying to instantiate
17288 this variable. We've already done all that needs to be
17290 if (decl
!= error_mark_node
)
17291 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17293 stmt
= begin_handler ();
17294 finish_handler_parms (decl
, stmt
);
17295 RECUR (HANDLER_BODY (t
));
17296 finish_handler (stmt
);
17301 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17302 if (CLASS_TYPE_P (tmp
))
17304 /* Local classes are not independent templates; they are
17305 instantiated along with their containing function. And this
17306 way we don't have to deal with pushing out of one local class
17307 to instantiate a member of another local class. */
17308 /* Closures are handled by the LAMBDA_EXPR. */
17309 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17310 complete_type (tmp
);
17311 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17313 || (TREE_CODE (fld
) == FUNCTION_DECL
17314 && !DECL_ARTIFICIAL (fld
)))
17315 && DECL_TEMPLATE_INSTANTIATION (fld
))
17316 instantiate_decl (fld
, /*defer_ok=*/false,
17317 /*expl_inst_class=*/false);
17321 case STATIC_ASSERT
:
17325 ++c_inhibit_evaluation_warnings
;
17327 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17330 /*integral_constant_expression_p=*/true);
17331 --c_inhibit_evaluation_warnings
;
17333 finish_static_assert (condition
,
17334 STATIC_ASSERT_MESSAGE (t
),
17335 STATIC_ASSERT_SOURCE_LOCATION (t
),
17336 /*member_p=*/false);
17341 case OACC_PARALLEL
:
17342 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17344 stmt
= begin_omp_parallel ();
17345 RECUR (OMP_BODY (t
));
17346 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17350 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17351 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17352 complain
, in_decl
);
17353 if (OMP_PARALLEL_COMBINED (t
))
17354 omp_parallel_combined_clauses
= &tmp
;
17355 stmt
= begin_omp_parallel ();
17356 RECUR (OMP_PARALLEL_BODY (t
));
17357 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17358 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17359 = OMP_PARALLEL_COMBINED (t
);
17360 pop_omp_privatization_clauses (r
);
17364 if (OMP_TASK_BODY (t
) == NULL_TREE
)
17366 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17367 complain
, in_decl
);
17369 OMP_TASK_CLAUSES (t
) = tmp
;
17373 r
= push_omp_privatization_clauses (false);
17374 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17375 complain
, in_decl
);
17376 stmt
= begin_omp_task ();
17377 RECUR (OMP_TASK_BODY (t
));
17378 finish_omp_task (tmp
, stmt
);
17379 pop_omp_privatization_clauses (r
);
17384 case OMP_DISTRIBUTE
:
17388 tree clauses
, body
, pre_body
;
17389 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17390 tree orig_declv
= NULL_TREE
;
17391 tree incrv
= NULL_TREE
;
17392 enum c_omp_region_type ort
= C_ORT_OMP
;
17393 bool any_range_for
= false;
17396 if (TREE_CODE (t
) == OACC_LOOP
)
17399 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17400 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17402 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17404 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17405 if (OMP_FOR_ORIG_DECLS (t
))
17406 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17407 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17408 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17409 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17412 keep_next_level (true);
17413 stmt
= begin_omp_structured_block ();
17415 pre_body
= push_stmt_list ();
17416 RECUR (OMP_FOR_PRE_BODY (t
));
17417 pre_body
= pop_stmt_list (pre_body
);
17419 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17420 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17422 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
17423 condv
, incrv
, &clauses
, args
,
17425 integral_constant_expression_p
);
17426 omp_parallel_combined_clauses
= NULL
;
17430 gcc_assert (orig_declv
);
17431 body
= begin_omp_structured_block ();
17432 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17433 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
17434 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
17435 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
17436 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
17437 TREE_VEC_ELT (declv
, i
));
17440 body
= push_stmt_list ();
17441 RECUR (OMP_FOR_BODY (t
));
17443 body
= finish_omp_structured_block (body
);
17445 body
= pop_stmt_list (body
);
17447 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17448 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17449 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17453 t
= make_node (TREE_CODE (t
));
17454 TREE_TYPE (t
) = void_type_node
;
17455 OMP_FOR_BODY (t
) = body
;
17456 OMP_FOR_PRE_BODY (t
) = pre_body
;
17457 OMP_FOR_CLAUSES (t
) = clauses
;
17458 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17462 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
17464 pop_omp_privatization_clauses (r
);
17469 omp_parallel_combined_clauses
= NULL
;
17474 case OMP_TASKGROUP
:
17475 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17476 && OMP_TEAMS_COMBINED (t
));
17477 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17479 if (TREE_CODE (t
) == OMP_TEAMS
)
17481 keep_next_level (true);
17482 stmt
= begin_omp_structured_block ();
17483 RECUR (OMP_BODY (t
));
17484 stmt
= finish_omp_structured_block (stmt
);
17488 stmt
= push_stmt_list ();
17489 RECUR (OMP_BODY (t
));
17490 stmt
= pop_stmt_list (stmt
);
17494 OMP_BODY (t
) = stmt
;
17495 OMP_CLAUSES (t
) = tmp
;
17497 pop_omp_privatization_clauses (r
);
17501 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
17502 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
17504 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
17505 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
17507 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
17508 args
, complain
, in_decl
);
17509 if (tmp
== NULL_TREE
)
17510 tmp
= error_mark_node
;
17514 kind
= (enum omp_clause_depend_kind
)
17515 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
17518 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
17521 finish_omp_depobj (EXPR_LOCATION (t
), r
,
17522 OMP_CLAUSE_DEPEND_SOURCE
,
17523 OMP_DEPOBJ_CLAUSES (t
));
17527 case OMP_TARGET_DATA
:
17529 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17530 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17532 keep_next_level (true);
17533 stmt
= begin_omp_structured_block ();
17535 RECUR (OMP_BODY (t
));
17536 stmt
= finish_omp_structured_block (stmt
);
17539 OMP_BODY (t
) = stmt
;
17540 OMP_CLAUSES (t
) = tmp
;
17541 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17543 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17546 /* For combined target teams, ensure the num_teams and
17547 thread_limit clause expressions are evaluated on the host,
17548 before entering the target construct. */
17550 for (c
= OMP_TEAMS_CLAUSES (teams
);
17551 c
; c
= OMP_CLAUSE_CHAIN (c
))
17552 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17553 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17554 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17556 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17557 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17558 if (expr
== error_mark_node
)
17560 tmp
= TARGET_EXPR_SLOT (expr
);
17562 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17563 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17564 OMP_CLAUSE_FIRSTPRIVATE
);
17565 OMP_CLAUSE_DECL (tc
) = tmp
;
17566 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17567 OMP_TARGET_CLAUSES (t
) = tc
;
17576 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17577 complain
, in_decl
);
17578 OACC_DECLARE_CLAUSES (t
) = tmp
;
17582 case OMP_TARGET_UPDATE
:
17583 case OMP_TARGET_ENTER_DATA
:
17584 case OMP_TARGET_EXIT_DATA
:
17585 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17586 complain
, in_decl
);
17588 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17592 case OACC_ENTER_DATA
:
17593 case OACC_EXIT_DATA
:
17595 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17596 complain
, in_decl
);
17598 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17603 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17604 complain
, in_decl
);
17605 stmt
= push_stmt_list ();
17606 RECUR (OMP_BODY (t
));
17607 stmt
= pop_stmt_list (stmt
);
17610 OMP_BODY (t
) = stmt
;
17611 OMP_ORDERED_CLAUSES (t
) = tmp
;
17617 stmt
= push_stmt_list ();
17618 RECUR (OMP_BODY (t
));
17619 stmt
= pop_stmt_list (stmt
);
17622 OMP_BODY (t
) = stmt
;
17627 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17629 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
17630 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
17631 complain
, in_decl
);
17632 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17634 tree op1
= TREE_OPERAND (t
, 1);
17635 tree rhs1
= NULL_TREE
;
17637 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17639 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17640 op1
= TREE_OPERAND (op1
, 1);
17642 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17643 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17644 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
17645 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
17646 OMP_ATOMIC_MEMORY_ORDER (t
));
17650 tree op1
= TREE_OPERAND (t
, 1);
17651 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17652 tree rhs1
= NULL_TREE
;
17653 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17654 enum tree_code opcode
= NOP_EXPR
;
17655 if (code
== OMP_ATOMIC_READ
)
17657 v
= RECUR (TREE_OPERAND (op1
, 0));
17658 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17660 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17661 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17663 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17664 v
= RECUR (TREE_OPERAND (op1
, 0));
17665 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17666 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17668 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17669 op11
= TREE_OPERAND (op11
, 1);
17671 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17672 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17673 opcode
= TREE_CODE (op11
);
17674 if (opcode
== MODIFY_EXPR
)
17680 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17681 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17683 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
17684 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
17688 case TRANSACTION_EXPR
:
17691 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17692 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17694 if (TRANSACTION_EXPR_IS_STMT (t
))
17696 tree body
= TRANSACTION_EXPR_BODY (t
);
17697 tree noex
= NULL_TREE
;
17698 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17700 noex
= MUST_NOT_THROW_COND (body
);
17701 if (noex
== NULL_TREE
)
17702 noex
= boolean_true_node
;
17703 body
= TREE_OPERAND (body
, 0);
17705 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17707 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17711 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17712 RECUR (TRANSACTION_EXPR_BODY (t
)),
17719 case MUST_NOT_THROW_EXPR
:
17721 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17722 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17723 RETURN (build_must_not_throw_expr (op0
, cond
));
17726 case EXPR_PACK_EXPANSION
:
17727 error ("invalid use of pack expansion expression");
17728 RETURN (error_mark_node
);
17730 case NONTYPE_ARGUMENT_PACK
:
17731 error ("use %<...%> to expand argument pack");
17732 RETURN (error_mark_node
);
17734 case COMPOUND_EXPR
:
17735 tmp
= RECUR (TREE_OPERAND (t
, 0));
17736 if (tmp
== NULL_TREE
)
17737 /* If the first operand was a statement, we're done with it. */
17738 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17739 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17740 RECUR (TREE_OPERAND (t
, 1)),
17743 case ANNOTATE_EXPR
:
17744 tmp
= RECUR (TREE_OPERAND (t
, 0));
17745 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17746 TREE_TYPE (tmp
), tmp
,
17747 RECUR (TREE_OPERAND (t
, 1)),
17748 RECUR (TREE_OPERAND (t
, 2))));
17751 RETURN (add_stmt (copy_node (t
)));
17754 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17756 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17757 /*function_p=*/false,
17758 integral_constant_expression_p
));
17761 RETURN (NULL_TREE
);
17763 input_location
= loc
;
17769 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17770 function. For description of the body see comment above
17771 cp_parser_omp_declare_reduction_exprs. */
17774 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17776 if (t
== NULL_TREE
|| t
== error_mark_node
)
17779 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17781 tree_stmt_iterator tsi
;
17784 memset (stmts
, 0, sizeof stmts
);
17785 for (i
= 0, tsi
= tsi_start (t
);
17786 i
< 7 && !tsi_end_p (tsi
);
17787 i
++, tsi_next (&tsi
))
17788 stmts
[i
] = tsi_stmt (tsi
);
17789 gcc_assert (tsi_end_p (tsi
));
17793 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17794 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17795 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17796 args
, complain
, in_decl
);
17797 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17798 args
, complain
, in_decl
);
17799 DECL_CONTEXT (omp_out
) = current_function_decl
;
17800 DECL_CONTEXT (omp_in
) = current_function_decl
;
17801 keep_next_level (true);
17802 tree block
= begin_omp_structured_block ();
17803 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17804 block
= finish_omp_structured_block (block
);
17805 block
= maybe_cleanup_point_expr_void (block
);
17806 add_decl_expr (omp_out
);
17807 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17808 TREE_NO_WARNING (omp_out
) = 1;
17809 add_decl_expr (omp_in
);
17810 finish_expr_stmt (block
);
17814 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17815 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17816 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17817 args
, complain
, in_decl
);
17818 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17819 args
, complain
, in_decl
);
17820 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17821 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17822 keep_next_level (true);
17823 tree block
= begin_omp_structured_block ();
17824 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17825 block
= finish_omp_structured_block (block
);
17826 block
= maybe_cleanup_point_expr_void (block
);
17827 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17828 add_decl_expr (omp_priv
);
17829 add_decl_expr (omp_orig
);
17830 finish_expr_stmt (block
);
17832 add_decl_expr (omp_orig
);
17836 /* T is a postfix-expression that is not being used in a function
17837 call. Return the substituted version of T. */
17840 tsubst_non_call_postfix_expression (tree t
, tree args
,
17841 tsubst_flags_t complain
,
17844 if (TREE_CODE (t
) == SCOPE_REF
)
17845 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17846 /*done=*/false, /*address_p=*/false);
17848 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17849 /*function_p=*/false,
17850 /*integral_constant_expression_p=*/false);
17855 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17856 instantiation context. Instantiating a pack expansion containing a lambda
17857 might result in multiple lambdas all based on the same lambda in the
17861 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17863 tree oldfn
= lambda_function (t
);
17866 tree r
= build_lambda_expr ();
17868 LAMBDA_EXPR_LOCATION (r
)
17869 = LAMBDA_EXPR_LOCATION (t
);
17870 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17871 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17872 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17874 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17875 /* A lambda in a default argument outside a class gets no
17876 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17877 tsubst_default_argument calls start_lambda_scope, so we need to
17878 specifically ignore it here, and use the global scope. */
17879 record_null_lambda_scope (r
);
17881 record_lambda_scope (r
);
17883 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17884 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17886 vec
<tree
,va_gc
>* field_packs
= NULL
;
17888 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17889 cap
= TREE_CHAIN (cap
))
17891 tree ofield
= TREE_PURPOSE (cap
);
17892 if (PACK_EXPANSION_P (ofield
))
17893 ofield
= PACK_EXPANSION_PATTERN (ofield
);
17894 tree field
= tsubst_decl (ofield
, args
, complain
);
17896 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
17898 /* Remember these for when we've pushed local_specializations. */
17899 vec_safe_push (field_packs
, ofield
);
17900 vec_safe_push (field_packs
, field
);
17903 if (field
== error_mark_node
)
17904 return error_mark_node
;
17906 tree init
= TREE_VALUE (cap
);
17907 if (PACK_EXPANSION_P (init
))
17908 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17910 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17911 /*fn*/false, /*constexpr*/false);
17913 if (TREE_CODE (field
) == TREE_VEC
)
17915 int len
= TREE_VEC_LENGTH (field
);
17916 gcc_assert (TREE_CODE (init
) == TREE_VEC
17917 && TREE_VEC_LENGTH (init
) == len
);
17918 for (int i
= 0; i
< len
; ++i
)
17919 LAMBDA_EXPR_CAPTURE_LIST (r
)
17920 = tree_cons (TREE_VEC_ELT (field
, i
),
17921 TREE_VEC_ELT (init
, i
),
17922 LAMBDA_EXPR_CAPTURE_LIST (r
));
17926 LAMBDA_EXPR_CAPTURE_LIST (r
)
17927 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17929 if (id_equal (DECL_NAME (field
), "__this"))
17930 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17934 tree type
= begin_lambda_type (r
);
17935 if (type
== error_mark_node
)
17936 return error_mark_node
;
17938 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17939 determine_visibility (TYPE_NAME (type
));
17941 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17943 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17944 ? DECL_TI_TEMPLATE (oldfn
)
17947 tree fntype
= static_fn_type (oldfn
);
17949 ++processing_template_decl
;
17950 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17952 --processing_template_decl
;
17954 if (fntype
== error_mark_node
)
17955 r
= error_mark_node
;
17958 /* Fix the type of 'this'. */
17959 fntype
= build_memfn_type (fntype
, type
,
17960 type_memfn_quals (fntype
),
17961 type_memfn_rqual (fntype
));
17965 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17966 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17967 finish_member_declaration (tmpl
);
17972 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17973 finish_member_declaration (fn
);
17976 /* Let finish_function set this. */
17977 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17979 /* The body of a lambda-expression is not a subexpression of the
17980 enclosing expression. */
17983 bool nested
= cfun
;
17985 push_function_context ();
17987 /* Still increment function_depth so that we don't GC in the
17988 middle of an expression. */
17991 local_specialization_stack
s (lss_copy
);
17993 tree body
= start_lambda_function (fn
, r
);
17995 /* Now record them for lookup_init_capture_pack. */
17996 int fplen
= vec_safe_length (field_packs
);
17997 for (int i
= 0; i
< fplen
; )
17999 tree pack
= (*field_packs
)[i
++];
18000 tree inst
= (*field_packs
)[i
++];
18001 register_local_specialization (inst
, pack
);
18003 release_tree_vector (field_packs
);
18005 register_parameter_specializations (oldfn
, fn
);
18009 /* We might not partially instantiate some parts of the function, so
18010 copy these flags from the original template. */
18011 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18012 current_function_returns_value
= ol
->returns_value
;
18013 current_function_returns_null
= ol
->returns_null
;
18014 current_function_returns_abnormally
= ol
->returns_abnormally
;
18015 current_function_infinite_loop
= ol
->infinite_loop
;
18018 /* [temp.deduct] A lambda-expression appearing in a function type or a
18019 template parameter is not considered part of the immediate context for
18020 the purposes of template argument deduction. */
18021 complain
= tf_warning_or_error
;
18023 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18024 /*constexpr*/false);
18026 finish_lambda_function (body
);
18029 pop_function_context ();
18033 /* The capture list was built up in reverse order; fix that now. */
18034 LAMBDA_EXPR_CAPTURE_LIST (r
)
18035 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18037 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18039 maybe_add_lambda_conv_op (type
);
18042 finish_struct (type
, /*attr*/NULL_TREE
);
18044 insert_pending_capture_proxies ();
18049 /* Like tsubst but deals with expressions and performs semantic
18050 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18053 tsubst_copy_and_build (tree t
,
18055 tsubst_flags_t complain
,
18058 bool integral_constant_expression_p
)
18060 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18061 #define RECUR(NODE) \
18062 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18063 /*function_p=*/false, \
18064 integral_constant_expression_p)
18069 if (t
== NULL_TREE
|| t
== error_mark_node
)
18072 loc
= input_location
;
18073 if (location_t eloc
= cp_expr_location (t
))
18074 input_location
= eloc
;
18076 /* N3276 decltype magic only applies to calls at the top level or on the
18077 right side of a comma. */
18078 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18079 complain
&= ~tf_decltype
;
18081 switch (TREE_CODE (t
))
18085 /* Fall through. */
18086 case IDENTIFIER_NODE
:
18090 bool non_integral_constant_expression_p
;
18091 const char *error_msg
;
18093 if (IDENTIFIER_CONV_OP_P (t
))
18095 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18096 t
= make_conv_op_name (new_type
);
18099 /* Look up the name. */
18100 decl
= lookup_name (t
);
18102 /* By convention, expressions use ERROR_MARK_NODE to indicate
18103 failure, not NULL_TREE. */
18104 if (decl
== NULL_TREE
)
18105 decl
= error_mark_node
;
18107 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18109 integral_constant_expression_p
,
18110 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18111 &non_integral_constant_expression_p
,
18112 /*template_p=*/false,
18114 /*address_p=*/false,
18115 /*template_arg_p=*/false,
18120 if (!function_p
&& identifier_p (decl
))
18122 if (complain
& tf_error
)
18123 unqualified_name_lookup_error (decl
);
18124 decl
= error_mark_node
;
18129 case TEMPLATE_ID_EXPR
:
18132 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18133 tree targs
= TREE_OPERAND (t
, 1);
18136 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18137 if (targs
== error_mark_node
)
18138 RETURN (error_mark_node
);
18140 if (TREE_CODE (templ
) == SCOPE_REF
)
18142 tree name
= TREE_OPERAND (templ
, 1);
18143 tree tid
= lookup_template_function (name
, targs
);
18144 TREE_OPERAND (templ
, 1) = tid
;
18148 if (variable_template_p (templ
))
18149 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18151 if (TREE_CODE (templ
) == COMPONENT_REF
)
18153 object
= TREE_OPERAND (templ
, 0);
18154 templ
= TREE_OPERAND (templ
, 1);
18157 object
= NULL_TREE
;
18158 templ
= lookup_template_function (templ
, targs
);
18161 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18162 object
, templ
, NULL_TREE
));
18164 RETURN (baselink_for_fns (templ
));
18169 tree r
= RECUR (TREE_OPERAND (t
, 0));
18171 if (REFERENCE_REF_P (t
))
18173 /* A type conversion to reference type will be enclosed in
18174 such an indirect ref, but the substitution of the cast
18175 will have also added such an indirect ref. */
18176 r
= convert_from_reference (r
);
18179 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18180 complain
|decltype_flag
);
18182 if (REF_PARENTHESIZED_P (t
))
18183 r
= force_paren_expr (r
);
18190 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18191 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18192 RETURN (build_nop (type
, op0
));
18195 case IMPLICIT_CONV_EXPR
:
18197 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18198 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18199 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18201 retval
= copy_node (t
);
18202 TREE_TYPE (retval
) = type
;
18203 TREE_OPERAND (retval
, 0) = expr
;
18206 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18207 /* We'll pass this to convert_nontype_argument again, we don't need
18208 to actually perform any conversion here. */
18210 int flags
= LOOKUP_IMPLICIT
;
18211 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18212 flags
= LOOKUP_NORMAL
;
18213 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18219 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18220 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18221 if (op0
== error_mark_node
)
18222 RETURN (error_mark_node
);
18223 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18227 case REINTERPRET_CAST_EXPR
:
18228 case CONST_CAST_EXPR
:
18229 case DYNAMIC_CAST_EXPR
:
18230 case STATIC_CAST_EXPR
:
18233 tree op
, r
= NULL_TREE
;
18235 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18236 if (integral_constant_expression_p
18237 && !cast_valid_in_integral_constant_expression_p (type
))
18239 if (complain
& tf_error
)
18240 error ("a cast to a type other than an integral or "
18241 "enumeration type cannot appear in a constant-expression");
18242 RETURN (error_mark_node
);
18245 op
= RECUR (TREE_OPERAND (t
, 0));
18247 warning_sentinel
s(warn_useless_cast
);
18248 warning_sentinel
s2(warn_ignored_qualifiers
);
18249 switch (TREE_CODE (t
))
18252 r
= build_functional_cast (type
, op
, complain
);
18254 case REINTERPRET_CAST_EXPR
:
18255 r
= build_reinterpret_cast (type
, op
, complain
);
18257 case CONST_CAST_EXPR
:
18258 r
= build_const_cast (type
, op
, complain
);
18260 case DYNAMIC_CAST_EXPR
:
18261 r
= build_dynamic_cast (type
, op
, complain
);
18263 case STATIC_CAST_EXPR
:
18264 r
= build_static_cast (type
, op
, complain
);
18267 gcc_unreachable ();
18273 case POSTDECREMENT_EXPR
:
18274 case POSTINCREMENT_EXPR
:
18275 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18276 args
, complain
, in_decl
);
18277 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
18278 complain
|decltype_flag
));
18280 case PREDECREMENT_EXPR
:
18281 case PREINCREMENT_EXPR
:
18285 case TRUTH_NOT_EXPR
:
18286 case UNARY_PLUS_EXPR
: /* Unary + */
18287 case REALPART_EXPR
:
18288 case IMAGPART_EXPR
:
18289 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
18290 RECUR (TREE_OPERAND (t
, 0)),
18291 complain
|decltype_flag
));
18293 case FIX_TRUNC_EXPR
:
18294 gcc_unreachable ();
18297 op1
= TREE_OPERAND (t
, 0);
18298 if (TREE_CODE (op1
) == LABEL_DECL
)
18299 RETURN (finish_label_address_expr (DECL_NAME (op1
),
18300 EXPR_LOCATION (op1
)));
18301 if (TREE_CODE (op1
) == SCOPE_REF
)
18302 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
18303 /*done=*/true, /*address_p=*/true);
18305 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
18307 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
18308 complain
|decltype_flag
));
18313 case TRUNC_DIV_EXPR
:
18314 case CEIL_DIV_EXPR
:
18315 case FLOOR_DIV_EXPR
:
18316 case ROUND_DIV_EXPR
:
18317 case EXACT_DIV_EXPR
:
18321 case TRUNC_MOD_EXPR
:
18322 case FLOOR_MOD_EXPR
:
18323 case TRUTH_ANDIF_EXPR
:
18324 case TRUTH_ORIF_EXPR
:
18325 case TRUTH_AND_EXPR
:
18326 case TRUTH_OR_EXPR
:
18342 warning_sentinel
s1(warn_type_limits
);
18343 warning_sentinel
s2(warn_div_by_zero
);
18344 warning_sentinel
s3(warn_logical_op
);
18345 warning_sentinel
s4(warn_tautological_compare
);
18346 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18347 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18348 tree r
= build_x_binary_op
18349 (input_location
, TREE_CODE (t
),
18351 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18353 : TREE_CODE (TREE_OPERAND (t
, 0))),
18355 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18357 : TREE_CODE (TREE_OPERAND (t
, 1))),
18359 complain
|decltype_flag
);
18360 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18361 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18366 case POINTER_PLUS_EXPR
:
18368 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18369 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18370 RETURN (fold_build_pointer_plus (op0
, op1
));
18374 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18375 /*address_p=*/false));
18377 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18378 args
, complain
, in_decl
);
18379 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18380 RECUR (TREE_OPERAND (t
, 1)),
18381 complain
|decltype_flag
));
18384 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18385 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18386 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18393 op1
= TREE_OPERAND (t
, 0);
18394 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18395 op1
= TREE_TYPE (op1
);
18396 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18397 && ALIGNOF_EXPR_STD_P (t
));
18400 /* When there are no ARGS, we are trying to evaluate a
18401 non-dependent expression from the parser. Trying to do
18402 the substitutions may not work. */
18404 op1
= TREE_TYPE (op1
);
18408 ++cp_unevaluated_operand
;
18409 ++c_inhibit_evaluation_warnings
;
18411 op1
= tsubst (op1
, args
, complain
, in_decl
);
18413 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18414 /*function_p=*/false,
18415 /*integral_constant_expression_p=*/
18417 --cp_unevaluated_operand
;
18418 --c_inhibit_evaluation_warnings
;
18421 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18422 complain
& tf_error
);
18424 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18425 complain
& tf_error
);
18426 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18428 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18430 if (!processing_template_decl
&& TYPE_P (op1
))
18432 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18433 build1 (NOP_EXPR
, op1
, error_mark_node
));
18434 SIZEOF_EXPR_TYPE_P (r
) = 1;
18437 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18438 TREE_SIDE_EFFECTS (r
) = 0;
18439 TREE_READONLY (r
) = 1;
18441 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18446 case AT_ENCODE_EXPR
:
18448 op1
= TREE_OPERAND (t
, 0);
18449 ++cp_unevaluated_operand
;
18450 ++c_inhibit_evaluation_warnings
;
18451 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18452 /*function_p=*/false,
18453 /*integral_constant_expression_p=*/false);
18454 --cp_unevaluated_operand
;
18455 --c_inhibit_evaluation_warnings
;
18456 RETURN (objc_build_encode_expr (op1
));
18459 case NOEXCEPT_EXPR
:
18460 op1
= TREE_OPERAND (t
, 0);
18461 ++cp_unevaluated_operand
;
18462 ++c_inhibit_evaluation_warnings
;
18463 ++cp_noexcept_operand
;
18464 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18465 /*function_p=*/false,
18466 /*integral_constant_expression_p=*/false);
18467 --cp_unevaluated_operand
;
18468 --c_inhibit_evaluation_warnings
;
18469 --cp_noexcept_operand
;
18470 RETURN (finish_noexcept_expr (op1
, complain
));
18474 warning_sentinel
s(warn_div_by_zero
);
18475 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18476 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18477 tree r
= build_x_modify_expr
18478 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18479 complain
|decltype_flag
);
18480 /* TREE_NO_WARNING must be set if either the expression was
18481 parenthesized or it uses an operator such as >>= rather
18482 than plain assignment. In the former case, it was already
18483 set and must be copied. In the latter case,
18484 build_x_modify_expr sets it and it must not be reset
18486 if (TREE_NO_WARNING (t
))
18487 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18493 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18494 args
, complain
, in_decl
);
18495 /* Remember that there was a reference to this entity. */
18497 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18498 RETURN (error_mark_node
);
18499 RETURN (build_x_arrow (input_location
, op1
, complain
));
18503 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18504 tree init
= RECUR (TREE_OPERAND (t
, 3));
18505 vec
<tree
, va_gc
> *placement_vec
;
18506 vec
<tree
, va_gc
> *init_vec
;
18509 if (placement
== NULL_TREE
)
18510 placement_vec
= NULL
;
18513 placement_vec
= make_tree_vector ();
18514 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18515 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18518 /* If there was an initializer in the original tree, but it
18519 instantiated to an empty list, then we should pass a
18520 non-NULL empty vector to tell build_new that it was an
18521 empty initializer() rather than no initializer. This can
18522 only happen when the initializer is a pack expansion whose
18523 parameter packs are of length zero. */
18524 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18528 init_vec
= make_tree_vector ();
18529 if (init
== void_node
)
18530 gcc_assert (init_vec
!= NULL
);
18533 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18534 vec_safe_push (init_vec
, TREE_VALUE (init
));
18538 /* Avoid passing an enclosing decl to valid_array_size_p. */
18539 in_decl
= NULL_TREE
;
18541 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18542 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18543 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18544 NEW_EXPR_USE_GLOBAL (t
),
18547 if (placement_vec
!= NULL
)
18548 release_tree_vector (placement_vec
);
18549 if (init_vec
!= NULL
)
18550 release_tree_vector (init_vec
);
18557 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18558 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18559 RETURN (delete_sanity (op0
, op1
,
18560 DELETE_EXPR_USE_VEC (t
),
18561 DELETE_EXPR_USE_GLOBAL (t
),
18565 case COMPOUND_EXPR
:
18567 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18568 complain
& ~tf_decltype
, in_decl
,
18569 /*function_p=*/false,
18570 integral_constant_expression_p
);
18571 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18573 RECUR (TREE_OPERAND (t
, 1)),
18574 complain
|decltype_flag
));
18580 vec
<tree
, va_gc
> *call_args
;
18581 unsigned int nargs
, i
;
18586 function
= CALL_EXPR_FN (t
);
18587 /* Internal function with no arguments. */
18588 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18591 /* When we parsed the expression, we determined whether or
18592 not Koenig lookup should be performed. */
18593 koenig_p
= KOENIG_LOOKUP_P (t
);
18594 if (function
== NULL_TREE
)
18597 qualified_p
= false;
18599 else if (TREE_CODE (function
) == SCOPE_REF
)
18601 qualified_p
= true;
18602 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18604 /*address_p=*/false);
18606 else if (koenig_p
&& identifier_p (function
))
18608 /* Do nothing; calling tsubst_copy_and_build on an identifier
18609 would incorrectly perform unqualified lookup again.
18611 Note that we can also have an IDENTIFIER_NODE if the earlier
18612 unqualified lookup found a member function; in that case
18613 koenig_p will be false and we do want to do the lookup
18614 again to find the instantiated member function.
18616 FIXME but doing that causes c++/15272, so we need to stop
18617 using IDENTIFIER_NODE in that situation. */
18618 qualified_p
= false;
18622 if (TREE_CODE (function
) == COMPONENT_REF
)
18624 tree op
= TREE_OPERAND (function
, 1);
18626 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18627 || (BASELINK_P (op
)
18628 && BASELINK_QUALIFIED_P (op
)));
18631 qualified_p
= false;
18633 if (TREE_CODE (function
) == ADDR_EXPR
18634 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18635 /* Avoid error about taking the address of a constructor. */
18636 function
= TREE_OPERAND (function
, 0);
18638 function
= tsubst_copy_and_build (function
, args
, complain
,
18641 integral_constant_expression_p
);
18643 if (BASELINK_P (function
))
18644 qualified_p
= true;
18647 nargs
= call_expr_nargs (t
);
18648 call_args
= make_tree_vector ();
18649 for (i
= 0; i
< nargs
; ++i
)
18651 tree arg
= CALL_EXPR_ARG (t
, i
);
18653 if (!PACK_EXPANSION_P (arg
))
18654 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18657 /* Expand the pack expansion and push each entry onto
18659 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18660 if (TREE_CODE (arg
) == TREE_VEC
)
18662 unsigned int len
, j
;
18664 len
= TREE_VEC_LENGTH (arg
);
18665 for (j
= 0; j
< len
; ++j
)
18667 tree value
= TREE_VEC_ELT (arg
, j
);
18668 if (value
!= NULL_TREE
)
18669 value
= convert_from_reference (value
);
18670 vec_safe_push (call_args
, value
);
18675 /* A partial substitution. Add one entry. */
18676 vec_safe_push (call_args
, arg
);
18681 /* We do not perform argument-dependent lookup if normal
18682 lookup finds a non-function, in accordance with the
18683 expected resolution of DR 218. */
18685 && ((is_overloaded_fn (function
)
18686 /* If lookup found a member function, the Koenig lookup is
18687 not appropriate, even if an unqualified-name was used
18688 to denote the function. */
18689 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18690 || identifier_p (function
))
18691 /* Only do this when substitution turns a dependent call
18692 into a non-dependent call. */
18693 && type_dependent_expression_p_push (t
)
18694 && !any_type_dependent_arguments_p (call_args
))
18695 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18697 if (function
!= NULL_TREE
18698 && identifier_p (function
)
18699 && !any_type_dependent_arguments_p (call_args
))
18701 if (koenig_p
&& (complain
& tf_warning_or_error
))
18703 /* For backwards compatibility and good diagnostics, try
18704 the unqualified lookup again if we aren't in SFINAE
18706 tree unq
= (tsubst_copy_and_build
18707 (function
, args
, complain
, in_decl
, true,
18708 integral_constant_expression_p
));
18709 if (unq
== error_mark_node
)
18711 release_tree_vector (call_args
);
18712 RETURN (error_mark_node
);
18715 if (unq
!= function
)
18717 /* In a lambda fn, we have to be careful to not
18718 introduce new this captures. Legacy code can't
18719 be using lambdas anyway, so it's ok to be
18721 bool in_lambda
= (current_class_type
18722 && LAMBDA_TYPE_P (current_class_type
));
18723 char const *const msg
18724 = G_("%qD was not declared in this scope, "
18725 "and no declarations were found by "
18726 "argument-dependent lookup at the point "
18727 "of instantiation");
18731 error_at (cp_expr_loc_or_loc (t
, input_location
),
18734 diag
= permerror (cp_expr_loc_or_loc (t
, input_location
),
18740 if (INDIRECT_REF_P (fn
))
18741 fn
= TREE_OPERAND (fn
, 0);
18742 if (is_overloaded_fn (fn
))
18743 fn
= get_first_fn (fn
);
18746 /* Can't say anything more. */;
18747 else if (DECL_CLASS_SCOPE_P (fn
))
18749 location_t loc
= cp_expr_loc_or_loc (t
,
18752 "declarations in dependent base %qT are "
18753 "not found by unqualified lookup",
18754 DECL_CLASS_CONTEXT (fn
));
18755 if (current_class_ptr
)
18757 "use %<this->%D%> instead", function
);
18760 "use %<%T::%D%> instead",
18761 current_class_name
, function
);
18764 inform (DECL_SOURCE_LOCATION (fn
),
18765 "%qD declared here, later in the "
18766 "translation unit", fn
);
18769 release_tree_vector (call_args
);
18770 RETURN (error_mark_node
);
18777 if (identifier_p (function
))
18779 if (complain
& tf_error
)
18780 unqualified_name_lookup_error (function
);
18781 release_tree_vector (call_args
);
18782 RETURN (error_mark_node
);
18786 /* Remember that there was a reference to this entity. */
18787 if (function
!= NULL_TREE
18788 && DECL_P (function
)
18789 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18791 release_tree_vector (call_args
);
18792 RETURN (error_mark_node
);
18795 /* Put back tf_decltype for the actual call. */
18796 complain
|= decltype_flag
;
18798 if (function
== NULL_TREE
)
18799 switch (CALL_EXPR_IFN (t
))
18802 gcc_assert (nargs
== 1);
18803 if (vec_safe_length (call_args
) != 1)
18805 error_at (cp_expr_loc_or_loc (t
, input_location
),
18806 "wrong number of arguments to "
18807 "%<__builtin_launder%>");
18808 ret
= error_mark_node
;
18811 ret
= finish_builtin_launder (cp_expr_loc_or_loc (t
,
18813 (*call_args
)[0], complain
);
18816 case IFN_VEC_CONVERT
:
18817 gcc_assert (nargs
== 1);
18818 if (vec_safe_length (call_args
) != 1)
18820 error_at (cp_expr_loc_or_loc (t
, input_location
),
18821 "wrong number of arguments to "
18822 "%<__builtin_convertvector%>");
18823 ret
= error_mark_node
;
18826 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
18827 tsubst (TREE_TYPE (t
), args
,
18828 complain
, in_decl
),
18830 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
18832 release_tree_vector (call_args
);
18838 /* Unsupported internal function with arguments. */
18839 gcc_unreachable ();
18841 else if (TREE_CODE (function
) == OFFSET_REF
18842 || TREE_CODE (function
) == DOTSTAR_EXPR
18843 || TREE_CODE (function
) == MEMBER_REF
)
18844 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18846 else if (TREE_CODE (function
) == COMPONENT_REF
)
18848 tree instance
= TREE_OPERAND (function
, 0);
18849 tree fn
= TREE_OPERAND (function
, 1);
18851 if (processing_template_decl
18852 && (type_dependent_expression_p (instance
)
18853 || (!BASELINK_P (fn
)
18854 && TREE_CODE (fn
) != FIELD_DECL
)
18855 || type_dependent_expression_p (fn
)
18856 || any_type_dependent_arguments_p (call_args
)))
18857 ret
= build_min_nt_call_vec (function
, call_args
);
18858 else if (!BASELINK_P (fn
))
18859 ret
= finish_call_expr (function
, &call_args
,
18860 /*disallow_virtual=*/false,
18861 /*koenig_p=*/false,
18864 ret
= (build_new_method_call
18866 &call_args
, NULL_TREE
,
18867 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18872 ret
= finish_call_expr (function
, &call_args
,
18873 /*disallow_virtual=*/qualified_p
,
18877 release_tree_vector (call_args
);
18879 if (ret
!= error_mark_node
)
18881 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18882 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18883 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18884 bool thk
= CALL_FROM_THUNK_P (t
);
18885 if (op
|| ord
|| rev
|| thk
)
18887 function
= extract_call_expr (ret
);
18888 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18889 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18890 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18893 if (TREE_CODE (function
) == CALL_EXPR
)
18894 CALL_FROM_THUNK_P (function
) = true;
18896 AGGR_INIT_FROM_THUNK_P (function
) = true;
18897 /* The thunk location is not interesting. */
18898 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18908 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18909 cond
= mark_rvalue_use (cond
);
18910 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
18913 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18915 if (integer_zerop (folded_cond
))
18917 ++c_inhibit_evaluation_warnings
;
18918 exp1
= RECUR (TREE_OPERAND (t
, 1));
18919 --c_inhibit_evaluation_warnings
;
18920 exp2
= RECUR (TREE_OPERAND (t
, 2));
18924 exp1
= RECUR (TREE_OPERAND (t
, 1));
18925 ++c_inhibit_evaluation_warnings
;
18926 exp2
= RECUR (TREE_OPERAND (t
, 2));
18927 --c_inhibit_evaluation_warnings
;
18929 cond
= folded_cond
;
18933 exp1
= RECUR (TREE_OPERAND (t
, 1));
18934 exp2
= RECUR (TREE_OPERAND (t
, 2));
18937 warning_sentinel
s(warn_duplicated_branches
);
18938 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18939 cond
, exp1
, exp2
, complain
));
18942 case PSEUDO_DTOR_EXPR
:
18944 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18945 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18946 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18947 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18953 tree purpose
, value
, chain
;
18955 if (t
== void_list_node
)
18958 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18959 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18961 /* We have pack expansions, so expand those and
18962 create a new list out of it. */
18963 tree purposevec
= NULL_TREE
;
18964 tree valuevec
= NULL_TREE
;
18968 /* Expand the argument expressions. */
18969 if (TREE_PURPOSE (t
))
18970 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18971 complain
, in_decl
);
18972 if (TREE_VALUE (t
))
18973 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18974 complain
, in_decl
);
18976 /* Build the rest of the list. */
18977 chain
= TREE_CHAIN (t
);
18978 if (chain
&& chain
!= void_type_node
)
18979 chain
= RECUR (chain
);
18981 /* Determine the number of arguments. */
18982 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18984 len
= TREE_VEC_LENGTH (purposevec
);
18985 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18987 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18988 len
= TREE_VEC_LENGTH (valuevec
);
18991 /* Since we only performed a partial substitution into
18992 the argument pack, we only RETURN (a single list
18994 if (purposevec
== TREE_PURPOSE (t
)
18995 && valuevec
== TREE_VALUE (t
)
18996 && chain
== TREE_CHAIN (t
))
18999 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19002 /* Convert the argument vectors into a TREE_LIST */
19006 /* Grab the Ith values. */
19008 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19011 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19014 /* Build the list (backwards). */
19015 chain
= tree_cons (purpose
, value
, chain
);
19021 purpose
= TREE_PURPOSE (t
);
19023 purpose
= RECUR (purpose
);
19024 value
= TREE_VALUE (t
);
19026 value
= RECUR (value
);
19027 chain
= TREE_CHAIN (t
);
19028 if (chain
&& chain
!= void_type_node
)
19029 chain
= RECUR (chain
);
19030 if (purpose
== TREE_PURPOSE (t
)
19031 && value
== TREE_VALUE (t
)
19032 && chain
== TREE_CHAIN (t
))
19034 RETURN (tree_cons (purpose
, value
, chain
));
19037 case COMPONENT_REF
:
19044 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19045 args
, complain
, in_decl
);
19046 /* Remember that there was a reference to this entity. */
19047 if (DECL_P (object
)
19048 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19049 RETURN (error_mark_node
);
19050 object_type
= TREE_TYPE (object
);
19052 member
= TREE_OPERAND (t
, 1);
19053 if (BASELINK_P (member
))
19054 member
= tsubst_baselink (member
,
19055 non_reference (TREE_TYPE (object
)),
19056 args
, complain
, in_decl
);
19058 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19059 if (member
== error_mark_node
)
19060 RETURN (error_mark_node
);
19062 if (TREE_CODE (member
) == FIELD_DECL
)
19064 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19065 if (TREE_CODE (r
) == COMPONENT_REF
)
19066 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19069 else if (type_dependent_expression_p (object
))
19070 /* We can't do much here. */;
19071 else if (!CLASS_TYPE_P (object_type
))
19073 if (scalarish_type_p (object_type
))
19075 tree s
= NULL_TREE
;
19076 tree dtor
= member
;
19078 if (TREE_CODE (dtor
) == SCOPE_REF
)
19080 s
= TREE_OPERAND (dtor
, 0);
19081 dtor
= TREE_OPERAND (dtor
, 1);
19083 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19085 dtor
= TREE_OPERAND (dtor
, 0);
19087 RETURN (finish_pseudo_destructor_expr
19088 (object
, s
, dtor
, input_location
));
19092 else if (TREE_CODE (member
) == SCOPE_REF
19093 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19095 /* Lookup the template functions now that we know what the
19097 tree scope
= TREE_OPERAND (member
, 0);
19098 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19099 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19100 member
= lookup_qualified_name (scope
, tmpl
,
19101 /*is_type_p=*/false,
19102 /*complain=*/false);
19103 if (BASELINK_P (member
))
19105 BASELINK_FUNCTIONS (member
)
19106 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19108 member
= (adjust_result_of_qualified_name_lookup
19109 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19114 qualified_name_lookup_error (scope
, tmpl
, member
,
19116 RETURN (error_mark_node
);
19119 else if (TREE_CODE (member
) == SCOPE_REF
19120 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19121 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19123 if (complain
& tf_error
)
19125 if (TYPE_P (TREE_OPERAND (member
, 0)))
19126 error ("%qT is not a class or namespace",
19127 TREE_OPERAND (member
, 0));
19129 error ("%qD is not a class or namespace",
19130 TREE_OPERAND (member
, 0));
19132 RETURN (error_mark_node
);
19135 r
= finish_class_member_access_expr (object
, member
,
19136 /*template_p=*/false,
19138 if (TREE_CODE (r
) == COMPONENT_REF
)
19139 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19144 RETURN (build_throw
19145 (RECUR (TREE_OPERAND (t
, 0))));
19149 vec
<constructor_elt
, va_gc
> *n
;
19150 constructor_elt
*ce
;
19151 unsigned HOST_WIDE_INT idx
;
19152 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19153 bool process_index_p
;
19155 bool need_copy_p
= false;
19158 if (type
== error_mark_node
)
19159 RETURN (error_mark_node
);
19161 /* We do not want to process the index of aggregate
19162 initializers as they are identifier nodes which will be
19163 looked up by digest_init. */
19164 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19166 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19167 newlen
= vec_safe_length (n
);
19168 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19170 if (ce
->index
&& process_index_p
19171 /* An identifier index is looked up in the type
19172 being initialized, not the current scope. */
19173 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19174 ce
->index
= RECUR (ce
->index
);
19176 if (PACK_EXPANSION_P (ce
->value
))
19178 /* Substitute into the pack expansion. */
19179 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19182 if (ce
->value
== error_mark_node
19183 || PACK_EXPANSION_P (ce
->value
))
19185 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19186 /* Just move the argument into place. */
19187 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19190 /* Update the length of the final CONSTRUCTOR
19191 arguments vector, and note that we will need to
19193 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19194 need_copy_p
= true;
19198 ce
->value
= RECUR (ce
->value
);
19203 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19205 vec_alloc (n
, newlen
);
19206 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19208 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19210 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19211 for (i
= 0; i
< len
; ++i
)
19212 CONSTRUCTOR_APPEND_ELT (n
, 0,
19213 TREE_VEC_ELT (ce
->value
, i
));
19216 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19220 r
= build_constructor (init_list_type_node
, n
);
19221 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19223 if (TREE_HAS_CONSTRUCTOR (t
))
19225 fcl_t cl
= fcl_functional
;
19226 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
19228 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
19231 TREE_TYPE (r
) = type
;
19237 tree operand_0
= TREE_OPERAND (t
, 0);
19238 if (TYPE_P (operand_0
))
19240 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
19241 RETURN (get_typeid (operand_0
, complain
));
19245 operand_0
= RECUR (operand_0
);
19246 RETURN (build_typeid (operand_0
, complain
));
19257 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
19258 /* ??? We're doing a subset of finish_id_expression here. */
19260 && !processing_template_decl
19261 && !cp_unevaluated_operand
19262 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
19263 && CP_DECL_THREAD_LOCAL_P (r
))
19265 if (tree wrap
= get_tls_wrapper_fn (r
))
19266 /* Replace an evaluated use of the thread_local variable with
19267 a call to its wrapper. */
19268 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
19270 else if (outer_automatic_var_p (r
))
19271 r
= process_outer_var_ref (r
, complain
);
19273 if (!TYPE_REF_P (TREE_TYPE (t
)))
19274 /* If the original type was a reference, we'll be wrapped in
19275 the appropriate INDIRECT_REF. */
19276 r
= convert_from_reference (r
);
19282 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19283 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19284 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
19287 case OFFSETOF_EXPR
:
19290 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
19291 in_decl
, /*function_p=*/false,
19292 /*integral_constant_expression_p=*/false);
19293 RETURN (finish_offsetof (object_ptr
,
19294 RECUR (TREE_OPERAND (t
, 0)),
19295 EXPR_LOCATION (t
)));
19298 case ADDRESSOF_EXPR
:
19299 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
19300 RECUR (TREE_OPERAND (t
, 0)), complain
));
19304 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
19305 complain
, in_decl
);
19307 tree type2
= TRAIT_EXPR_TYPE2 (t
);
19308 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
19309 type2
= RECUR (type2
);
19311 type2
= tsubst (type2
, args
, complain
, in_decl
);
19313 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
19318 tree old_stmt_expr
= cur_stmt_expr
;
19319 tree stmt_expr
= begin_stmt_expr ();
19321 cur_stmt_expr
= stmt_expr
;
19322 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
19323 integral_constant_expression_p
);
19324 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
19325 cur_stmt_expr
= old_stmt_expr
;
19327 /* If the resulting list of expression statement is empty,
19328 fold it further into void_node. */
19329 if (empty_expr_stmt_p (stmt_expr
))
19330 stmt_expr
= void_node
;
19332 RETURN (stmt_expr
);
19337 if (complain
& tf_partial
)
19339 /* We don't have a full set of template arguments yet; don't touch
19340 the lambda at all. */
19341 gcc_assert (processing_template_decl
);
19344 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
19346 RETURN (build_lambda_object (r
));
19350 /* We can get here for a constant initializer of non-dependent type.
19351 FIXME stop folding in cp_parser_initializer_clause. */
19353 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
19358 case TRANSACTION_EXPR
:
19359 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
19360 integral_constant_expression_p
));
19363 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
19365 case VEC_PERM_EXPR
:
19367 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19368 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19369 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19370 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19374 case REQUIRES_EXPR
:
19375 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19377 case NON_LVALUE_EXPR
:
19378 case VIEW_CONVERT_EXPR
:
19379 if (location_wrapper_p (t
))
19380 /* We need to do this here as well as in tsubst_copy so we get the
19381 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19382 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19383 EXPR_LOCATION (t
)));
19387 /* Handle Objective-C++ constructs, if appropriate. */
19390 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19391 in_decl
, /*function_p=*/false);
19395 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19401 input_location
= loc
;
19405 /* Verify that the instantiated ARGS are valid. For type arguments,
19406 make sure that the type's linkage is ok. For non-type arguments,
19407 make sure they are constants if they are integral or enumerations.
19408 Emit an error under control of COMPLAIN, and return TRUE on error. */
19411 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19413 if (dependent_template_arg_p (t
))
19415 if (ARGUMENT_PACK_P (t
))
19417 tree vec
= ARGUMENT_PACK_ARGS (t
);
19418 int len
= TREE_VEC_LENGTH (vec
);
19419 bool result
= false;
19422 for (i
= 0; i
< len
; ++i
)
19423 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19427 else if (TYPE_P (t
))
19429 /* [basic.link]: A name with no linkage (notably, the name
19430 of a class or enumeration declared in a local scope)
19431 shall not be used to declare an entity with linkage.
19432 This implies that names with no linkage cannot be used as
19435 DR 757 relaxes this restriction for C++0x. */
19436 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19437 : no_linkage_check (t
, /*relaxed_p=*/false));
19441 /* DR 488 makes use of a type with no linkage cause
19442 type deduction to fail. */
19443 if (complain
& tf_error
)
19445 if (TYPE_UNNAMED_P (nt
))
19446 error ("%qT is/uses unnamed type", t
);
19448 error ("template argument for %qD uses local type %qT",
19453 /* In order to avoid all sorts of complications, we do not
19454 allow variably-modified types as template arguments. */
19455 else if (variably_modified_type_p (t
, NULL_TREE
))
19457 if (complain
& tf_error
)
19458 error ("%qT is a variably modified type", t
);
19462 /* Class template and alias template arguments should be OK. */
19463 else if (DECL_TYPE_TEMPLATE_P (t
))
19465 /* A non-type argument of integral or enumerated type must be a
19467 else if (TREE_TYPE (t
)
19468 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19469 && !REFERENCE_REF_P (t
)
19470 && !TREE_CONSTANT (t
))
19472 if (complain
& tf_error
)
19473 error ("integral expression %qE is not constant", t
);
19480 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19482 int ix
, len
= DECL_NTPARMS (tmpl
);
19483 bool result
= false;
19485 for (ix
= 0; ix
!= len
; ix
++)
19487 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19490 if (result
&& (complain
& tf_error
))
19491 error (" trying to instantiate %qD", tmpl
);
19495 /* We're out of SFINAE context now, so generate diagnostics for the access
19496 errors we saw earlier when instantiating D from TMPL and ARGS. */
19499 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19501 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19502 tree type
= TREE_TYPE (pattern
);
19503 location_t loc
= input_location
;
19505 push_access_scope (d
);
19506 push_deferring_access_checks (dk_no_deferred
);
19507 input_location
= DECL_SOURCE_LOCATION (pattern
);
19508 tsubst (type
, args
, tf_warning_or_error
, d
);
19509 input_location
= loc
;
19510 pop_deferring_access_checks ();
19511 pop_access_scope (d
);
19514 /* Instantiate the indicated variable, function, or alias template TMPL with
19515 the template arguments in TARG_PTR. */
19518 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19520 tree targ_ptr
= orig_args
;
19524 bool access_ok
= true;
19526 if (tmpl
== error_mark_node
)
19527 return error_mark_node
;
19529 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19531 /* If this function is a clone, handle it specially. */
19532 if (DECL_CLONED_FUNCTION_P (tmpl
))
19537 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19538 DECL_CLONED_FUNCTION. */
19539 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19540 targ_ptr
, complain
);
19541 if (spec
== error_mark_node
)
19542 return error_mark_node
;
19544 /* Look for the clone. */
19545 FOR_EACH_CLONE (clone
, spec
)
19546 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19548 /* We should always have found the clone by now. */
19549 gcc_unreachable ();
19553 if (targ_ptr
== error_mark_node
)
19554 return error_mark_node
;
19556 /* Check to see if we already have this specialization. */
19557 gen_tmpl
= most_general_template (tmpl
);
19558 if (TMPL_ARGS_DEPTH (targ_ptr
)
19559 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19560 /* targ_ptr only has the innermost template args, so add the outer ones
19561 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19562 the case of a non-dependent call within a template definition). */
19563 targ_ptr
= (add_outermost_template_args
19564 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19567 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19568 but it doesn't seem to be on the hot path. */
19569 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19571 gcc_assert (tmpl
== gen_tmpl
19572 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19574 || fndecl
== NULL_TREE
);
19576 if (spec
!= NULL_TREE
)
19578 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19580 if (complain
& tf_error
)
19581 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19582 return error_mark_node
;
19587 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19589 return error_mark_node
;
19591 /* We are building a FUNCTION_DECL, during which the access of its
19592 parameters and return types have to be checked. However this
19593 FUNCTION_DECL which is the desired context for access checking
19594 is not built yet. We solve this chicken-and-egg problem by
19595 deferring all checks until we have the FUNCTION_DECL. */
19596 push_deferring_access_checks (dk_deferred
);
19598 /* Instantiation of the function happens in the context of the function
19599 template, not the context of the overload resolution we're doing. */
19600 push_to_top_level ();
19601 /* If there are dependent arguments, e.g. because we're doing partial
19602 ordering, make sure processing_template_decl stays set. */
19603 if (uses_template_parms (targ_ptr
))
19604 ++processing_template_decl
;
19605 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19607 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19608 complain
, gen_tmpl
, true);
19609 push_nested_class (ctx
);
19612 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19614 fndecl
= NULL_TREE
;
19615 if (VAR_P (pattern
))
19617 /* We need to determine if we're using a partial or explicit
19618 specialization now, because the type of the variable could be
19620 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19621 tree elt
= most_specialized_partial_spec (tid
, complain
);
19622 if (elt
== error_mark_node
)
19623 pattern
= error_mark_node
;
19626 tree partial_tmpl
= TREE_VALUE (elt
);
19627 tree partial_args
= TREE_PURPOSE (elt
);
19628 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19629 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19633 /* Substitute template parameters to obtain the specialization. */
19634 if (fndecl
== NULL_TREE
)
19635 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19636 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19637 pop_nested_class ();
19638 pop_from_top_level ();
19640 if (fndecl
== error_mark_node
)
19642 pop_deferring_access_checks ();
19643 return error_mark_node
;
19646 /* The DECL_TI_TEMPLATE should always be the immediate parent
19647 template, not the most general template. */
19648 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19649 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19651 /* Now we know the specialization, compute access previously
19652 deferred. Do no access control for inheriting constructors,
19653 as we already checked access for the inherited constructor. */
19654 if (!(flag_new_inheriting_ctors
19655 && DECL_INHERITED_CTOR (fndecl
)))
19657 push_access_scope (fndecl
);
19658 if (!perform_deferred_access_checks (complain
))
19660 pop_access_scope (fndecl
);
19662 pop_deferring_access_checks ();
19664 /* If we've just instantiated the main entry point for a function,
19665 instantiate all the alternate entry points as well. We do this
19666 by cloning the instantiation of the main entry point, not by
19667 instantiating the template clones. */
19668 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
19669 clone_function_decl (fndecl
, /*update_methods=*/false);
19673 if (!(complain
& tf_error
))
19675 /* Remember to reinstantiate when we're out of SFINAE so the user
19676 can see the errors. */
19677 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19679 return error_mark_node
;
19684 /* Wrapper for instantiate_template_1. */
19687 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19690 timevar_push (TV_TEMPLATE_INST
);
19691 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19692 timevar_pop (TV_TEMPLATE_INST
);
19696 /* Instantiate the alias template TMPL with ARGS. Also push a template
19697 instantiation level, which instantiate_template doesn't do because
19698 functions and variables have sufficient context established by the
19702 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19704 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19705 return error_mark_node
;
19706 if (!push_tinst_level (tmpl
, args
))
19707 return error_mark_node
;
19710 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19711 args
, tmpl
, complain
,
19712 /*require_all_args=*/true,
19713 /*use_default_args=*/true);
19715 tree r
= instantiate_template (tmpl
, args
, complain
);
19716 pop_tinst_level ();
19721 /* PARM is a template parameter pack for FN. Returns true iff
19722 PARM is used in a deducible way in the argument list of FN. */
19725 pack_deducible_p (tree parm
, tree fn
)
19727 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19728 for (; t
; t
= TREE_CHAIN (t
))
19730 tree type
= TREE_VALUE (t
);
19732 if (!PACK_EXPANSION_P (type
))
19734 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19735 packs
; packs
= TREE_CHAIN (packs
))
19736 if (template_args_equal (TREE_VALUE (packs
), parm
))
19738 /* The template parameter pack is used in a function parameter
19739 pack. If this is the end of the parameter list, the
19740 template parameter pack is deducible. */
19741 if (TREE_CHAIN (t
) == void_list_node
)
19744 /* Otherwise, not. Well, it could be deduced from
19745 a non-pack parameter, but doing so would end up with
19746 a deduction mismatch, so don't bother. */
19750 /* The template parameter pack isn't used in any function parameter
19751 packs, but it might be used deeper, e.g. tuple<Args...>. */
19755 /* Subroutine of fn_type_unification: check non-dependent parms for
19759 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
19760 tree fn
, unification_kind_t strict
, int flags
,
19761 struct conversion
**convs
, bool explain_p
)
19763 /* Non-constructor methods need to leave a conversion for 'this', which
19764 isn't included in nargs here. */
19765 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19766 && !DECL_CONSTRUCTOR_P (fn
));
19768 for (unsigned ia
= 0;
19769 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19771 tree parm
= TREE_VALUE (parms
);
19773 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19774 && (!TREE_CHAIN (parms
)
19775 || TREE_CHAIN (parms
) == void_list_node
))
19776 /* For a function parameter pack that occurs at the end of the
19777 parameter-declaration-list, the type A of each remaining
19778 argument of the call is compared with the type P of the
19779 declarator-id of the function parameter pack. */
19782 parms
= TREE_CHAIN (parms
);
19784 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19785 /* For a function parameter pack that does not occur at the
19786 end of the parameter-declaration-list, the type of the
19787 parameter pack is a non-deduced context. */
19790 if (!uses_template_parms (parm
))
19792 tree arg
= args
[ia
];
19793 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
19794 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
19796 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
19797 conv_p
, explain_p
))
19807 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19808 NARGS elements of the arguments that are being used when calling
19809 it. TARGS is a vector into which the deduced template arguments
19812 Returns either a FUNCTION_DECL for the matching specialization of FN or
19813 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19814 true, diagnostics will be printed to explain why it failed.
19816 If FN is a conversion operator, or we are trying to produce a specific
19817 specialization, RETURN_TYPE is the return type desired.
19819 The EXPLICIT_TARGS are explicit template arguments provided via a
19822 The parameter STRICT is one of:
19825 We are deducing arguments for a function call, as in
19826 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19827 deducing arguments for a call to the result of a conversion
19828 function template, as in [over.call.object].
19831 We are deducing arguments for a conversion function, as in
19832 [temp.deduct.conv].
19835 We are deducing arguments when doing an explicit instantiation
19836 as in [temp.explicit], when determining an explicit specialization
19837 as in [temp.expl.spec], or when taking the address of a function
19838 template, as in [temp.deduct.funcaddr]. */
19841 fn_type_unification (tree fn
,
19842 tree explicit_targs
,
19845 unsigned int nargs
,
19847 unification_kind_t strict
,
19849 struct conversion
**convs
,
19855 tree decl
= NULL_TREE
;
19856 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19858 static int deduction_depth
;
19859 /* type_unification_real will pass back any access checks from default
19860 template argument substitution. */
19861 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
19862 /* We don't have all the template args yet. */
19863 bool incomplete
= true;
19866 if (flag_new_inheriting_ctors
)
19867 fn
= strip_inheriting_ctors (fn
);
19869 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
19870 tree r
= error_mark_node
;
19872 tree full_targs
= targs
;
19873 if (TMPL_ARGS_DEPTH (targs
)
19874 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
19875 full_targs
= (add_outermost_template_args
19876 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
19880 complain
|= tf_decltype
;
19882 /* In C++0x, it's possible to have a function template whose type depends
19883 on itself recursively. This is most obvious with decltype, but can also
19884 occur with enumeration scope (c++/48969). So we need to catch infinite
19885 recursion and reject the substitution at deduction time; this function
19886 will return error_mark_node for any repeated substitution.
19888 This also catches excessive recursion such as when f<N> depends on
19889 f<N-1> across all integers, and returns error_mark_node for all the
19890 substitutions back up to the initial one.
19892 This is, of course, not reentrant. */
19893 if (excessive_deduction_depth
)
19894 return error_mark_node
;
19897 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19899 fntype
= TREE_TYPE (fn
);
19900 if (explicit_targs
)
19904 The specified template arguments must match the template
19905 parameters in kind (i.e., type, nontype, template), and there
19906 must not be more arguments than there are parameters;
19907 otherwise type deduction fails.
19909 Nontype arguments must match the types of the corresponding
19910 nontype template parameters, or must be convertible to the
19911 types of the corresponding nontype parameters as specified in
19912 _temp.arg.nontype_, otherwise type deduction fails.
19914 All references in the function type of the function template
19915 to the corresponding template parameters are replaced by the
19916 specified template argument values. If a substitution in a
19917 template parameter or in the function type of the function
19918 template results in an invalid type, type deduction fails. */
19919 int i
, len
= TREE_VEC_LENGTH (tparms
);
19920 location_t loc
= input_location
;
19921 incomplete
= false;
19923 if (explicit_targs
== error_mark_node
)
19926 if (TMPL_ARGS_DEPTH (explicit_targs
)
19927 < TMPL_ARGS_DEPTH (full_targs
))
19928 explicit_targs
= add_outermost_template_args (full_targs
,
19931 /* Adjust any explicit template arguments before entering the
19932 substitution context. */
19934 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19936 /*require_all_args=*/false,
19937 /*use_default_args=*/false));
19938 if (explicit_targs
== error_mark_node
)
19941 /* Substitute the explicit args into the function type. This is
19942 necessary so that, for instance, explicitly declared function
19943 arguments can match null pointed constants. If we were given
19944 an incomplete set of explicit args, we must not do semantic
19945 processing during substitution as we could create partial
19947 for (i
= 0; i
< len
; i
++)
19949 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19950 bool parameter_pack
= false;
19951 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19953 /* Dig out the actual parm. */
19954 if (TREE_CODE (parm
) == TYPE_DECL
19955 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19957 parm
= TREE_TYPE (parm
);
19958 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19960 else if (TREE_CODE (parm
) == PARM_DECL
)
19962 parm
= DECL_INITIAL (parm
);
19963 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19966 if (!parameter_pack
&& targ
== NULL_TREE
)
19967 /* No explicit argument for this template parameter. */
19970 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19972 /* Mark the argument pack as "incomplete". We could
19973 still deduce more arguments during unification.
19974 We remove this mark in type_unification_real. */
19977 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19978 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19979 = ARGUMENT_PACK_ARGS (targ
);
19982 /* We have some incomplete argument packs. */
19989 if (!push_tinst_level (fn
, explicit_targs
))
19991 excessive_deduction_depth
= true;
19994 ++processing_template_decl
;
19995 input_location
= DECL_SOURCE_LOCATION (fn
);
19996 /* Ignore any access checks; we'll see them again in
19997 instantiate_template and they might have the wrong
19998 access path at this point. */
19999 push_deferring_access_checks (dk_deferred
);
20000 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20001 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20002 pop_deferring_access_checks ();
20003 input_location
= loc
;
20004 --processing_template_decl
;
20005 pop_tinst_level ();
20007 if (fntype
== error_mark_node
)
20011 /* Place the explicitly specified arguments in TARGS. */
20012 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20013 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20014 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20015 if (!incomplete
&& CHECKING_P
20016 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20017 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20018 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20021 if (return_type
&& strict
!= DEDUCE_CALL
)
20023 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20024 new_args
[0] = return_type
;
20025 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20033 /* Never do unification on the 'this' parameter. */
20034 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20036 if (return_type
&& strict
== DEDUCE_CALL
)
20038 /* We're deducing for a call to the result of a template conversion
20039 function. The parms we really want are in return_type. */
20040 if (INDIRECT_TYPE_P (return_type
))
20041 return_type
= TREE_TYPE (return_type
);
20042 parms
= TYPE_ARG_TYPES (return_type
);
20044 else if (return_type
)
20046 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20049 /* We allow incomplete unification without an error message here
20050 because the standard doesn't seem to explicitly prohibit it. Our
20051 callers must be ready to deal with unification failures in any
20054 /* If we aren't explaining yet, push tinst context so we can see where
20055 any errors (e.g. from class instantiations triggered by instantiation
20056 of default template arguments) come from. If we are explaining, this
20057 context is redundant. */
20058 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20060 excessive_deduction_depth
= true;
20064 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20065 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20066 strict
, &checks
, explain_p
);
20068 pop_tinst_level ();
20072 /* Now that we have bindings for all of the template arguments,
20073 ensure that the arguments deduced for the template template
20074 parameters have compatible template parameter lists. We cannot
20075 check this property before we have deduced all template
20076 arguments, because the template parameter types of a template
20077 template parameter might depend on prior template parameters
20078 deduced after the template template parameter. The following
20079 ill-formed example illustrates this issue:
20081 template<typename T, template<T> class C> void f(C<5>, T);
20083 template<int N> struct X {};
20086 f(X<5>(), 5l); // error: template argument deduction fails
20089 The template parameter list of 'C' depends on the template type
20090 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20091 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20092 time that we deduce 'C'. */
20093 if (!template_template_parm_bindings_ok_p
20094 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20096 unify_inconsistent_template_template_parameters (explain_p
);
20100 /* DR 1391: All parameters have args, now check non-dependent parms for
20102 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20107 /* All is well so far. Now, check:
20111 When all template arguments have been deduced, all uses of
20112 template parameters in nondeduced contexts are replaced with
20113 the corresponding deduced argument values. If the
20114 substitution results in an invalid type, as described above,
20115 type deduction fails. */
20116 if (!push_tinst_level (fn
, targs
))
20118 excessive_deduction_depth
= true;
20122 /* Also collect access checks from the instantiation. */
20123 reopen_deferring_access_checks (checks
);
20125 decl
= instantiate_template (fn
, targs
, complain
);
20127 checks
= get_deferred_access_checks ();
20128 pop_deferring_access_checks ();
20130 pop_tinst_level ();
20132 if (decl
== error_mark_node
)
20135 /* Now perform any access checks encountered during substitution. */
20136 push_access_scope (decl
);
20137 ok
= perform_access_checks (checks
, complain
);
20138 pop_access_scope (decl
);
20142 /* If we're looking for an exact match, check that what we got
20143 is indeed an exact match. It might not be if some template
20144 parameters are used in non-deduced contexts. But don't check
20145 for an exact match if we have dependent template arguments;
20146 in that case we're doing partial ordering, and we already know
20147 that we have two candidates that will provide the actual type. */
20148 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20150 tree substed
= TREE_TYPE (decl
);
20154 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20156 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20157 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20158 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20160 unify_type_mismatch (explain_p
, args
[i
],
20161 TREE_VALUE (sarg
));
20166 /* After doing deduction with the inherited constructor, actually return an
20167 instantiation of the inheriting constructor. */
20169 decl
= instantiate_template (orig_fn
, targs
, complain
);
20175 if (excessive_deduction_depth
)
20177 if (deduction_depth
== 0)
20178 /* Reset once we're all the way out. */
20179 excessive_deduction_depth
= false;
20185 /* Adjust types before performing type deduction, as described in
20186 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20187 sections are symmetric. PARM is the type of a function parameter
20188 or the return type of the conversion function. ARG is the type of
20189 the argument passed to the call, or the type of the value
20190 initialized with the result of the conversion function.
20191 ARG_EXPR is the original argument expression, which may be null. */
20194 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20207 /* Swap PARM and ARG throughout the remainder of this
20208 function; the handling is precisely symmetric since PARM
20209 will initialize ARG rather than vice versa. */
20210 std::swap (parm
, arg
);
20214 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20215 too, but here handle it by stripping the reference from PARM
20216 rather than by adding it to ARG. */
20217 if (TYPE_REF_P (*parm
)
20218 && TYPE_REF_IS_RVALUE (*parm
)
20219 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20220 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20221 && TYPE_REF_P (*arg
)
20222 && !TYPE_REF_IS_RVALUE (*arg
))
20223 *parm
= TREE_TYPE (*parm
);
20224 /* Nothing else to do in this case. */
20228 gcc_unreachable ();
20231 if (!TYPE_REF_P (*parm
))
20233 /* [temp.deduct.call]
20235 If P is not a reference type:
20237 --If A is an array type, the pointer type produced by the
20238 array-to-pointer standard conversion (_conv.array_) is
20239 used in place of A for type deduction; otherwise,
20241 --If A is a function type, the pointer type produced by
20242 the function-to-pointer standard conversion
20243 (_conv.func_) is used in place of A for type deduction;
20246 --If A is a cv-qualified type, the top level
20247 cv-qualifiers of A's type are ignored for type
20249 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
20250 *arg
= build_pointer_type (TREE_TYPE (*arg
));
20251 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
20252 *arg
= build_pointer_type (*arg
);
20254 *arg
= TYPE_MAIN_VARIANT (*arg
);
20257 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20258 reference to a cv-unqualified template parameter that does not represent a
20259 template parameter of a class template (during class template argument
20260 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20261 an lvalue, the type "lvalue reference to A" is used in place of A for type
20263 if (TYPE_REF_P (*parm
)
20264 && TYPE_REF_IS_RVALUE (*parm
)
20265 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20266 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
20267 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20268 && (arg_expr
? lvalue_p (arg_expr
)
20269 /* try_one_overload doesn't provide an arg_expr, but
20270 functions are always lvalues. */
20271 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
20272 *arg
= build_reference_type (*arg
);
20274 /* [temp.deduct.call]
20276 If P is a cv-qualified type, the top level cv-qualifiers
20277 of P's type are ignored for type deduction. If P is a
20278 reference type, the type referred to by P is used for
20280 *parm
= TYPE_MAIN_VARIANT (*parm
);
20281 if (TYPE_REF_P (*parm
))
20283 *parm
= TREE_TYPE (*parm
);
20284 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20287 /* DR 322. For conversion deduction, remove a reference type on parm
20288 too (which has been swapped into ARG). */
20289 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
20290 *arg
= TREE_TYPE (*arg
);
20295 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20296 template which doesn't contain any deducible template parameters; check if
20297 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20298 unify_one_argument. */
20301 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
20302 int flags
, struct conversion
**conv_p
,
20308 type
= TREE_TYPE (arg
);
20312 if (same_type_p (parm
, type
))
20313 return unify_success (explain_p
);
20315 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20316 if (strict
== DEDUCE_CONV
)
20318 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
20319 return unify_success (explain_p
);
20321 else if (strict
!= DEDUCE_EXACT
)
20324 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
20326 /* Avoid recalculating this in add_function_candidate. */
20328 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
20330 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
20332 return unify_success (explain_p
);
20335 if (strict
== DEDUCE_EXACT
)
20336 return unify_type_mismatch (explain_p
, parm
, arg
);
20338 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
20341 static bool uses_deducible_template_parms (tree type
);
20343 /* Returns true iff the expression EXPR is one from which a template
20344 argument can be deduced. In other words, if it's an undecorated
20345 use of a template non-type parameter. */
20348 deducible_expression (tree expr
)
20350 /* Strip implicit conversions. */
20351 while (CONVERT_EXPR_P (expr
))
20352 expr
= TREE_OPERAND (expr
, 0);
20353 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
20356 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20357 deducible way; that is, if it has a max value of <PARM> - 1. */
20360 deducible_array_bound (tree domain
)
20362 if (domain
== NULL_TREE
)
20365 tree max
= TYPE_MAX_VALUE (domain
);
20366 if (TREE_CODE (max
) != MINUS_EXPR
)
20369 return deducible_expression (TREE_OPERAND (max
, 0));
20372 /* Returns true iff the template arguments ARGS use a template parameter
20373 in a deducible way. */
20376 deducible_template_args (tree args
)
20378 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20381 tree elt
= TREE_VEC_ELT (args
, i
);
20382 if (ARGUMENT_PACK_P (elt
))
20383 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20386 if (PACK_EXPANSION_P (elt
))
20387 elt
= PACK_EXPANSION_PATTERN (elt
);
20388 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20390 else if (TYPE_P (elt
))
20391 deducible
= uses_deducible_template_parms (elt
);
20393 deducible
= deducible_expression (elt
);
20401 /* Returns true iff TYPE contains any deducible references to template
20402 parameters, as per 14.8.2.5. */
20405 uses_deducible_template_parms (tree type
)
20407 if (PACK_EXPANSION_P (type
))
20408 type
= PACK_EXPANSION_PATTERN (type
);
20415 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20416 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20422 if (INDIRECT_TYPE_P (type
))
20423 return uses_deducible_template_parms (TREE_TYPE (type
));
20425 /* T[integer-constant ]
20427 if (TREE_CODE (type
) == ARRAY_TYPE
)
20428 return (uses_deducible_template_parms (TREE_TYPE (type
))
20429 || deducible_array_bound (TYPE_DOMAIN (type
)));
20441 if (TYPE_PTRMEM_P (type
))
20442 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20443 || (uses_deducible_template_parms
20444 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20446 /* template-name <T> (where template-name refers to a class template)
20447 template-name <i> (where template-name refers to a class template) */
20448 if (CLASS_TYPE_P (type
)
20449 && CLASSTYPE_TEMPLATE_INFO (type
)
20450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20451 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20452 (CLASSTYPE_TI_ARGS (type
)));
20457 if (TREE_CODE (type
) == FUNCTION_TYPE
20458 || TREE_CODE (type
) == METHOD_TYPE
)
20460 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20462 tree parm
= TYPE_ARG_TYPES (type
);
20463 if (TREE_CODE (type
) == METHOD_TYPE
)
20464 parm
= TREE_CHAIN (parm
);
20465 for (; parm
; parm
= TREE_CHAIN (parm
))
20466 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20473 /* Subroutine of type_unification_real and unify_pack_expansion to
20474 handle unification of a single P/A pair. Parameters are as
20475 for those functions. */
20478 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20479 int subr
, unification_kind_t strict
,
20482 tree arg_expr
= NULL_TREE
;
20485 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20486 return unify_invalid (explain_p
);
20487 if (arg
== unknown_type_node
)
20488 /* We can't deduce anything from this, but we might get all the
20489 template args from other function args. */
20490 return unify_success (explain_p
);
20492 /* Implicit conversions (Clause 4) will be performed on a function
20493 argument to convert it to the type of the corresponding function
20494 parameter if the parameter type contains no template-parameters that
20495 participate in template argument deduction. */
20496 if (strict
!= DEDUCE_EXACT
20497 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20498 /* For function parameters with no deducible template parameters,
20499 just return. We'll check non-dependent conversions later. */
20500 return unify_success (explain_p
);
20505 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20506 | UNIFY_ALLOW_MORE_CV_QUAL
20507 | UNIFY_ALLOW_DERIVED
);
20511 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20515 arg_strict
= UNIFY_ALLOW_NONE
;
20519 gcc_unreachable ();
20522 /* We only do these transformations if this is the top-level
20523 parameter_type_list in a call or declaration matching; in other
20524 situations (nested function declarators, template argument lists) we
20525 won't be comparing a type to an expression, and we don't do any type
20531 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20532 if (type_unknown_p (arg
))
20534 /* [temp.deduct.type] A template-argument can be
20535 deduced from a pointer to function or pointer
20536 to member function argument if the set of
20537 overloaded functions does not contain function
20538 templates and at most one of a set of
20539 overloaded functions provides a unique
20541 resolve_overloaded_unification (tparms
, targs
, parm
,
20543 arg_strict
, explain_p
);
20544 /* If a unique match was not found, this is a
20545 non-deduced context, so we still succeed. */
20546 return unify_success (explain_p
);
20550 arg
= unlowered_expr_type (arg
);
20551 if (arg
== error_mark_node
)
20552 return unify_invalid (explain_p
);
20556 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20559 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20560 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20561 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20563 /* For deduction from an init-list we need the actual list. */
20564 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20566 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20569 /* for_each_template_parm callback that always returns 0. */
20572 zero_r (tree
, void *)
20577 /* for_each_template_parm any_fn callback to handle deduction of a template
20578 type argument from the type of an array bound. */
20581 array_deduction_r (tree t
, void *data
)
20583 tree_pair_p d
= (tree_pair_p
)data
;
20584 tree
&tparms
= d
->purpose
;
20585 tree
&targs
= d
->value
;
20587 if (TREE_CODE (t
) == ARRAY_TYPE
)
20588 if (tree dom
= TYPE_DOMAIN (t
))
20589 if (tree max
= TYPE_MAX_VALUE (dom
))
20591 if (TREE_CODE (max
) == MINUS_EXPR
)
20592 max
= TREE_OPERAND (max
, 0);
20593 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20594 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20595 UNIFY_ALLOW_NONE
, /*explain*/false);
20598 /* Keep walking. */
20602 /* Try to deduce any not-yet-deduced template type arguments from the type of
20603 an array bound. This is handled separately from unify because 14.8.2.5 says
20604 "The type of a type parameter is only deduced from an array bound if it is
20605 not otherwise deduced." */
20608 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20610 tree_pair_s data
= { tparms
, targs
};
20611 hash_set
<tree
> visited
;
20612 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20613 /*nondeduced*/false, array_deduction_r
);
20616 /* Most parms like fn_type_unification.
20618 If SUBR is 1, we're being called recursively (to unify the
20619 arguments of a function or method parameter of a function
20622 CHECKS is a pointer to a vector of access checks encountered while
20623 substituting default template arguments. */
20626 type_unification_real (tree tparms
,
20630 unsigned int xnargs
,
20632 unification_kind_t strict
,
20633 vec
<deferred_access_check
, va_gc
> **checks
,
20638 int ntparms
= TREE_VEC_LENGTH (tparms
);
20639 int saw_undeduced
= 0;
20642 unsigned int nargs
;
20645 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20646 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20647 gcc_assert (ntparms
> 0);
20649 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20651 /* Reset the number of non-defaulted template arguments contained
20653 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20661 while (parms
&& parms
!= void_list_node
20664 parm
= TREE_VALUE (parms
);
20666 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20667 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20668 /* For a function parameter pack that occurs at the end of the
20669 parameter-declaration-list, the type A of each remaining
20670 argument of the call is compared with the type P of the
20671 declarator-id of the function parameter pack. */
20674 parms
= TREE_CHAIN (parms
);
20676 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20677 /* For a function parameter pack that does not occur at the
20678 end of the parameter-declaration-list, the type of the
20679 parameter pack is a non-deduced context. */
20685 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20691 && parms
!= void_list_node
20692 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20694 /* Unify the remaining arguments with the pack expansion type. */
20696 tree parmvec
= make_tree_vec (1);
20698 /* Allocate a TREE_VEC and copy in all of the arguments */
20699 argvec
= make_tree_vec (nargs
- ia
);
20700 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20701 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20703 /* Copy the parameter into parmvec. */
20704 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20705 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20706 /*subr=*/subr
, explain_p
))
20709 /* Advance to the end of the list of parameters. */
20710 parms
= TREE_CHAIN (parms
);
20713 /* Fail if we've reached the end of the parm list, and more args
20714 are present, and the parm list isn't variadic. */
20715 if (ia
< nargs
&& parms
== void_list_node
)
20716 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20717 /* Fail if parms are left and they don't have default values and
20718 they aren't all deduced as empty packs (c++/57397). This is
20719 consistent with sufficient_parms_p. */
20720 if (parms
&& parms
!= void_list_node
20721 && TREE_PURPOSE (parms
) == NULL_TREE
)
20723 unsigned int count
= nargs
;
20728 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20731 p
= TREE_CHAIN (p
);
20733 while (p
&& p
!= void_list_node
);
20734 if (count
!= nargs
)
20735 return unify_too_few_arguments (explain_p
, ia
, count
,
20741 tsubst_flags_t complain
= (explain_p
20742 ? tf_warning_or_error
20744 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20746 for (i
= 0; i
< ntparms
; i
++)
20748 tree targ
= TREE_VEC_ELT (targs
, i
);
20749 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20751 /* Clear the "incomplete" flags on all argument packs now so that
20752 substituting them into later default arguments works. */
20753 if (targ
&& ARGUMENT_PACK_P (targ
))
20755 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
20756 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
20759 if (targ
|| tparm
== error_mark_node
)
20761 tparm
= TREE_VALUE (tparm
);
20763 if (TREE_CODE (tparm
) == TYPE_DECL
20764 && !tried_array_deduction
)
20766 try_array_deduction (tparms
, targs
, xparms
);
20767 tried_array_deduction
= true;
20768 if (TREE_VEC_ELT (targs
, i
))
20772 /* If this is an undeduced nontype parameter that depends on
20773 a type parameter, try another pass; its type may have been
20774 deduced from a later argument than the one from which
20775 this parameter can be deduced. */
20776 if (TREE_CODE (tparm
) == PARM_DECL
20777 && uses_template_parms (TREE_TYPE (tparm
))
20778 && saw_undeduced
< 2)
20784 /* Core issue #226 (C++0x) [temp.deduct]:
20786 If a template argument has not been deduced, its
20787 default template argument, if any, is used.
20789 When we are in C++98 mode, TREE_PURPOSE will either
20790 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20791 to explicitly check cxx_dialect here. */
20792 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20793 /* OK, there is a default argument. Wait until after the
20794 conversion check to do substitution. */
20797 /* If the type parameter is a parameter pack, then it will
20798 be deduced to an empty parameter pack. */
20799 if (template_parameter_pack_p (tparm
))
20803 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20805 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20806 TREE_CONSTANT (arg
) = 1;
20809 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20811 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
20813 TREE_VEC_ELT (targs
, i
) = arg
;
20817 return unify_parameter_deduction_failure (explain_p
, tparm
);
20820 /* Now substitute into the default template arguments. */
20821 for (i
= 0; i
< ntparms
; i
++)
20823 tree targ
= TREE_VEC_ELT (targs
, i
);
20824 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20826 if (targ
|| tparm
== error_mark_node
)
20828 tree parm
= TREE_VALUE (tparm
);
20829 tree arg
= TREE_PURPOSE (tparm
);
20830 reopen_deferring_access_checks (*checks
);
20831 location_t save_loc
= input_location
;
20833 input_location
= DECL_SOURCE_LOCATION (parm
);
20835 if (saw_undeduced
== 1
20836 && TREE_CODE (parm
) == PARM_DECL
20837 && uses_template_parms (TREE_TYPE (parm
)))
20839 /* The type of this non-type parameter depends on undeduced
20840 parameters. Don't try to use its default argument yet,
20841 since we might deduce an argument for it on the next pass,
20842 but do check whether the arguments we already have cause
20843 substitution failure, so that that happens before we try
20844 later default arguments (78489). */
20845 ++processing_template_decl
;
20846 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
20848 --processing_template_decl
;
20849 if (type
== error_mark_node
)
20850 arg
= error_mark_node
;
20856 tree substed
= NULL_TREE
;
20857 if (saw_undeduced
== 1 && processing_template_decl
== 0)
20859 /* First instatiate in template context, in case we still
20860 depend on undeduced template parameters. */
20861 ++processing_template_decl
;
20862 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20864 --processing_template_decl
;
20865 if (substed
!= error_mark_node
20866 && !uses_template_parms (substed
))
20867 /* We replaced all the tparms, substitute again out of
20868 template context. */
20869 substed
= NULL_TREE
;
20872 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20875 if (!uses_template_parms (substed
))
20876 arg
= convert_template_argument (parm
, substed
, full_targs
,
20877 complain
, i
, NULL_TREE
);
20878 else if (saw_undeduced
== 1)
20881 arg
= error_mark_node
;
20884 input_location
= save_loc
;
20885 *checks
= get_deferred_access_checks ();
20886 pop_deferring_access_checks ();
20888 if (arg
== error_mark_node
)
20892 TREE_VEC_ELT (targs
, i
) = arg
;
20893 /* The position of the first default template argument,
20894 is also the number of non-defaulted arguments in TARGS.
20896 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20897 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
20901 if (saw_undeduced
++ == 1)
20905 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
20908 return unify_success (explain_p
);
20911 /* Subroutine of type_unification_real. Args are like the variables
20912 at the call site. ARG is an overloaded function (or template-id);
20913 we try deducing template args from each of the overloads, and if
20914 only one succeeds, we go with that. Modifies TARGS and returns
20915 true on success. */
20918 resolve_overloaded_unification (tree tparms
,
20922 unification_kind_t strict
,
20926 tree tempargs
= copy_node (targs
);
20928 tree goodfn
= NULL_TREE
;
20931 if (TREE_CODE (arg
) == ADDR_EXPR
)
20933 arg
= TREE_OPERAND (arg
, 0);
20939 if (TREE_CODE (arg
) == COMPONENT_REF
)
20940 /* Handle `&x' where `x' is some static or non-static member
20942 arg
= TREE_OPERAND (arg
, 1);
20944 if (TREE_CODE (arg
) == OFFSET_REF
)
20945 arg
= TREE_OPERAND (arg
, 1);
20947 /* Strip baselink information. */
20948 if (BASELINK_P (arg
))
20949 arg
= BASELINK_FUNCTIONS (arg
);
20951 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20953 /* If we got some explicit template args, we need to plug them into
20954 the affected templates before we try to unify, in case the
20955 explicit args will completely resolve the templates in question. */
20958 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20959 arg
= TREE_OPERAND (arg
, 0);
20961 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20964 tree subargs
, elem
;
20966 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20969 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20970 expl_subargs
, NULL_TREE
, tf_none
,
20971 /*require_all_args=*/true,
20972 /*use_default_args=*/true);
20973 if (subargs
!= error_mark_node
20974 && !any_dependent_template_arguments_p (subargs
))
20976 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20977 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20978 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20979 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20988 /* If no templates (or more than one) are fully resolved by the
20989 explicit arguments, this template-id is a non-deduced context; it
20990 could still be OK if we deduce all template arguments for the
20991 enclosing call through other arguments. */
20995 else if (TREE_CODE (arg
) != OVERLOAD
20996 && TREE_CODE (arg
) != FUNCTION_DECL
)
20997 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20998 -- but the deduction does not succeed because the expression is
20999 not just the function on its own. */
21002 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21005 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21006 strict
, sub_strict
, addr_p
, explain_p
)
21007 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21014 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21015 to function or pointer to member function argument if the set of
21016 overloaded functions does not contain function templates and at most
21017 one of a set of overloaded functions provides a unique match.
21019 So if we found multiple possibilities, we return success but don't
21020 deduce anything. */
21024 int i
= TREE_VEC_LENGTH (targs
);
21026 if (TREE_VEC_ELT (tempargs
, i
))
21028 tree old
= TREE_VEC_ELT (targs
, i
);
21029 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21030 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21031 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21032 /* Don't forget explicit template arguments in a pack. */
21033 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21034 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21035 TREE_VEC_ELT (targs
, i
) = new_
;
21044 /* Core DR 115: In contexts where deduction is done and fails, or in
21045 contexts where deduction is not done, if a template argument list is
21046 specified and it, along with any default template arguments, identifies
21047 a single function template specialization, then the template-id is an
21048 lvalue for the function template specialization. */
21051 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21053 tree expr
, offset
, baselink
;
21056 if (!type_unknown_p (orig_expr
))
21061 offset
= NULL_TREE
;
21062 baselink
= NULL_TREE
;
21064 if (TREE_CODE (expr
) == ADDR_EXPR
)
21066 expr
= TREE_OPERAND (expr
, 0);
21069 if (TREE_CODE (expr
) == OFFSET_REF
)
21072 expr
= TREE_OPERAND (expr
, 1);
21074 if (BASELINK_P (expr
))
21077 expr
= BASELINK_FUNCTIONS (expr
);
21080 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21083 tree goodfn
= NULL_TREE
;
21085 /* If we got some explicit template args, we need to plug them into
21086 the affected templates before we try to unify, in case the
21087 explicit args will completely resolve the templates in question. */
21089 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21090 tree arg
= TREE_OPERAND (expr
, 0);
21091 tree badfn
= NULL_TREE
;
21092 tree badargs
= NULL_TREE
;
21094 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21097 tree subargs
, elem
;
21099 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21102 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21103 expl_subargs
, NULL_TREE
, tf_none
,
21104 /*require_all_args=*/true,
21105 /*use_default_args=*/true);
21106 if (subargs
!= error_mark_node
21107 && !any_dependent_template_arguments_p (subargs
))
21109 elem
= instantiate_template (fn
, subargs
, tf_none
);
21110 if (elem
== error_mark_node
)
21115 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21124 mark_used (goodfn
);
21127 expr
= build_baselink (BASELINK_BINFO (baselink
),
21128 BASELINK_ACCESS_BINFO (baselink
),
21129 expr
, BASELINK_OPTYPE (baselink
));
21133 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21134 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21137 expr
= cp_build_addr_expr (expr
, complain
);
21140 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21141 /* There were no good options and at least one bad one, so let the
21142 user know what the problem is. */
21143 instantiate_template (badfn
, badargs
, complain
);
21148 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21149 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21150 different overloads deduce different arguments for a given parm.
21151 ADDR_P is true if the expression for which deduction is being
21152 performed was of the form "& fn" rather than simply "fn".
21154 Returns 1 on success. */
21157 try_one_overload (tree tparms
,
21162 unification_kind_t strict
,
21171 if (arg
== error_mark_node
)
21174 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21175 to function or pointer to member function argument if the set of
21176 overloaded functions does not contain function templates and at most
21177 one of a set of overloaded functions provides a unique match.
21179 So if this is a template, just return success. */
21181 if (uses_template_parms (arg
))
21184 if (TREE_CODE (arg
) == METHOD_TYPE
)
21185 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21187 arg
= build_pointer_type (arg
);
21189 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21191 /* We don't copy orig_targs for this because if we have already deduced
21192 some template args from previous args, unify would complain when we
21193 try to deduce a template parameter for the same argument, even though
21194 there isn't really a conflict. */
21195 nargs
= TREE_VEC_LENGTH (targs
);
21196 tempargs
= make_tree_vec (nargs
);
21198 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
21201 /* First make sure we didn't deduce anything that conflicts with
21202 explicitly specified args. */
21203 for (i
= nargs
; i
--; )
21205 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21206 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
21210 else if (uses_template_parms (elt
))
21211 /* Since we're unifying against ourselves, we will fill in
21212 template args used in the function parm list with our own
21213 template parms. Discard them. */
21214 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
21215 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
21217 /* Check that the argument at each index of the deduced argument pack
21218 is equivalent to the corresponding explicitly specified argument.
21219 We may have deduced more arguments than were explicitly specified,
21222 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21223 that's wrong if we deduce the same argument pack from multiple
21224 function arguments: it's only incomplete the first time. */
21226 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
21227 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
21229 if (TREE_VEC_LENGTH (deduced_pack
)
21230 < TREE_VEC_LENGTH (explicit_pack
))
21233 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
21234 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
21235 TREE_VEC_ELT (deduced_pack
, j
)))
21238 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
21242 for (i
= nargs
; i
--; )
21244 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21247 TREE_VEC_ELT (targs
, i
) = elt
;
21253 /* PARM is a template class (perhaps with unbound template
21254 parameters). ARG is a fully instantiated type. If ARG can be
21255 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21256 TARGS are as for unify. */
21259 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
21262 tree copy_of_targs
;
21264 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21266 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21267 /* Matches anything. */;
21268 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
21269 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
21272 /* We need to make a new template argument vector for the call to
21273 unify. If we used TARGS, we'd clutter it up with the result of
21274 the attempted unification, even if this class didn't work out.
21275 We also don't want to commit ourselves to all the unifications
21276 we've already done, since unification is supposed to be done on
21277 an argument-by-argument basis. In other words, consider the
21278 following pathological case:
21280 template <int I, int J, int K>
21283 template <int I, int J>
21284 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21286 template <int I, int J, int K>
21287 void f(S<I, J, K>, S<I, I, I>);
21296 Now, by the time we consider the unification involving `s2', we
21297 already know that we must have `f<0, 0, 0>'. But, even though
21298 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21299 because there are two ways to unify base classes of S<0, 1, 2>
21300 with S<I, I, I>. If we kept the already deduced knowledge, we
21301 would reject the possibility I=1. */
21302 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
21304 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21306 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
21311 /* If unification failed, we're done. */
21312 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
21313 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
21319 /* Given a template type PARM and a class type ARG, find the unique
21320 base type in ARG that is an instance of PARM. We do not examine
21321 ARG itself; only its base-classes. If there is not exactly one
21322 appropriate base class, return NULL_TREE. PARM may be the type of
21323 a partial specialization, as well as a plain template type. Used
21326 static enum template_base_result
21327 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
21328 bool explain_p
, tree
*result
)
21330 tree rval
= NULL_TREE
;
21333 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
21335 binfo
= TYPE_BINFO (complete_type (arg
));
21338 /* The type could not be completed. */
21339 *result
= NULL_TREE
;
21340 return tbr_incomplete_type
;
21343 /* Walk in inheritance graph order. The search order is not
21344 important, and this avoids multiple walks of virtual bases. */
21345 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
21347 tree r
= try_class_unification (tparms
, targs
, parm
,
21348 BINFO_TYPE (binfo
), explain_p
);
21352 /* If there is more than one satisfactory baseclass, then:
21356 If they yield more than one possible deduced A, the type
21360 if (rval
&& !same_type_p (r
, rval
))
21362 *result
= NULL_TREE
;
21363 return tbr_ambiguous_baseclass
;
21371 return tbr_success
;
21374 /* Returns the level of DECL, which declares a template parameter. */
21377 template_decl_level (tree decl
)
21379 switch (TREE_CODE (decl
))
21382 case TEMPLATE_DECL
:
21383 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21386 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21389 gcc_unreachable ();
21394 /* Decide whether ARG can be unified with PARM, considering only the
21395 cv-qualifiers of each type, given STRICT as documented for unify.
21396 Returns nonzero iff the unification is OK on that basis. */
21399 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21401 int arg_quals
= cp_type_quals (arg
);
21402 int parm_quals
= cp_type_quals (parm
);
21404 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21405 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21407 /* Although a CVR qualifier is ignored when being applied to a
21408 substituted template parameter ([8.3.2]/1 for example), that
21409 does not allow us to unify "const T" with "int&" because both
21410 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21411 It is ok when we're allowing additional CV qualifiers
21412 at the outer level [14.8.2.1]/3,1st bullet. */
21413 if ((TYPE_REF_P (arg
)
21414 || TREE_CODE (arg
) == FUNCTION_TYPE
21415 || TREE_CODE (arg
) == METHOD_TYPE
)
21416 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21419 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21420 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21424 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21425 && (arg_quals
& parm_quals
) != parm_quals
)
21428 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21429 && (parm_quals
& arg_quals
) != arg_quals
)
21435 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21437 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21439 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21440 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21441 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21443 *index
= TEMPLATE_TYPE_IDX (parm
);
21444 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21448 *index
= TEMPLATE_PARM_IDX (parm
);
21449 *level
= TEMPLATE_PARM_LEVEL (parm
);
21453 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21455 if (unify (TP, TA, P, A, S, EP)) \
21459 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21460 expansion at the end of PACKED_PARMS. Returns 0 if the type
21461 deduction succeeds, 1 otherwise. STRICT is the same as in
21462 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21463 function call argument list. We'll need to adjust the arguments to make them
21464 types. SUBR tells us if this is from a recursive call to
21465 type_unification_real, or for comparing two template argument
21469 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21470 tree packed_args
, unification_kind_t strict
,
21471 bool subr
, bool explain_p
)
21474 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21475 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21476 tree pack
, packs
= NULL_TREE
;
21477 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21479 /* Add in any args remembered from an earlier partial instantiation. */
21480 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21481 int levels
= TMPL_ARGS_DEPTH (targs
);
21483 packed_args
= expand_template_argument_pack (packed_args
);
21485 int len
= TREE_VEC_LENGTH (packed_args
);
21487 /* Determine the parameter packs we will be deducing from the
21488 pattern, and record their current deductions. */
21489 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21490 pack
; pack
= TREE_CHAIN (pack
))
21492 tree parm_pack
= TREE_VALUE (pack
);
21495 /* Only template parameter packs can be deduced, not e.g. function
21496 parameter packs or __bases or __integer_pack. */
21497 if (!TEMPLATE_PARM_P (parm_pack
))
21500 /* Determine the index and level of this parameter pack. */
21501 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21502 if (level
< levels
)
21505 /* Keep track of the parameter packs and their corresponding
21507 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21508 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21511 /* Loop through all of the arguments that have not yet been
21512 unified and unify each with the pattern. */
21513 for (i
= start
; i
< len
; i
++)
21516 bool any_explicit
= false;
21517 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21519 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21520 or the element of its argument pack at the current index if
21521 this argument was explicitly specified. */
21522 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21526 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21529 if (TREE_VALUE (pack
)
21530 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21531 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21533 any_explicit
= true;
21534 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21536 TMPL_ARG (targs
, level
, idx
) = arg
;
21539 /* If we had explicit template arguments, substitute them into the
21540 pattern before deduction. */
21543 /* Some arguments might still be unspecified or dependent. */
21545 ++processing_template_decl
;
21546 dependent
= any_dependent_template_arguments_p (targs
);
21548 --processing_template_decl
;
21549 parm
= tsubst (pattern
, targs
,
21550 explain_p
? tf_warning_or_error
: tf_none
,
21553 --processing_template_decl
;
21554 if (parm
== error_mark_node
)
21560 /* Unify the pattern with the current argument. */
21561 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21565 /* For each parameter pack, collect the deduced value. */
21566 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21569 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21571 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21572 TMPL_ARG (targs
, level
, idx
);
21576 /* Verify that the results of unification with the parameter packs
21577 produce results consistent with what we've seen before, and make
21578 the deduced argument packs available. */
21579 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21581 tree old_pack
= TREE_VALUE (pack
);
21582 tree new_args
= TREE_TYPE (pack
);
21583 int i
, len
= TREE_VEC_LENGTH (new_args
);
21585 bool nondeduced_p
= false;
21587 /* By default keep the original deduced argument pack.
21588 If necessary, more specific code is going to update the
21589 resulting deduced argument later down in this function. */
21590 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21591 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21593 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21594 actually deduce anything. */
21595 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21596 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21597 nondeduced_p
= true;
21601 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21603 /* If we had fewer function args than explicit template args,
21604 just use the explicits. */
21605 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21606 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21607 if (len
< explicit_len
)
21608 new_args
= explicit_args
;
21614 /* Build the deduced *_ARGUMENT_PACK. */
21615 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21617 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21618 TREE_CONSTANT (result
) = 1;
21621 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21623 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21625 /* Note the deduced argument packs for this parameter
21627 TMPL_ARG (targs
, level
, idx
) = result
;
21629 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21630 && (ARGUMENT_PACK_ARGS (old_pack
)
21631 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21633 /* We only had the explicitly-provided arguments before, but
21634 now we have a complete set of arguments. */
21635 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21637 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21638 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21639 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21643 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21644 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21646 if (!comp_template_args (old_args
, new_args
,
21647 &bad_old_arg
, &bad_new_arg
))
21648 /* Inconsistent unification of this parameter pack. */
21649 return unify_parameter_pack_inconsistent (explain_p
,
21655 return unify_success (explain_p
);
21658 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21659 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21660 parameters and return value are as for unify. */
21663 unify_array_domain (tree tparms
, tree targs
,
21664 tree parm_dom
, tree arg_dom
,
21672 /* Our representation of array types uses "N - 1" as the
21673 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21674 not an integer constant. We cannot unify arbitrarily
21675 complex expressions, so we eliminate the MINUS_EXPRs
21677 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21678 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21681 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21682 parm_max
= TREE_OPERAND (parm_max
, 0);
21684 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21685 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21688 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21689 trying to unify the type of a variable with the type
21690 of a template parameter. For example:
21692 template <unsigned int N>
21693 void f (char (&) [N]);
21700 Here, the type of the ARG will be "int [g(i)]", and
21701 may be a SAVE_EXPR, etc. */
21702 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21703 return unify_vla_arg (explain_p
, arg_dom
);
21704 arg_max
= TREE_OPERAND (arg_max
, 0);
21707 /* If only one of the bounds used a MINUS_EXPR, compensate
21708 by adding one to the other bound. */
21709 if (parm_cst
&& !arg_cst
)
21710 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21714 else if (arg_cst
&& !parm_cst
)
21715 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21720 return unify (tparms
, targs
, parm_max
, arg_max
,
21721 UNIFY_ALLOW_INTEGER
, explain_p
);
21724 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21726 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21731 if (PACK_EXPANSION_P (t
))
21732 t
= PACK_EXPANSION_PATTERN (t
);
21733 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
21734 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
21735 || DECL_TYPE_TEMPLATE_P (t
))
21737 else if (TYPE_P (t
))
21743 /* Deduce the value of template parameters. TPARMS is the (innermost)
21744 set of template parameters to a template. TARGS is the bindings
21745 for those template parameters, as determined thus far; TARGS may
21746 include template arguments for outer levels of template parameters
21747 as well. PARM is a parameter to a template function, or a
21748 subcomponent of that parameter; ARG is the corresponding argument.
21749 This function attempts to match PARM with ARG in a manner
21750 consistent with the existing assignments in TARGS. If more values
21751 are deduced, then TARGS is updated.
21753 Returns 0 if the type deduction succeeds, 1 otherwise. The
21754 parameter STRICT is a bitwise or of the following flags:
21757 Require an exact match between PARM and ARG.
21758 UNIFY_ALLOW_MORE_CV_QUAL:
21759 Allow the deduced ARG to be more cv-qualified (by qualification
21760 conversion) than ARG.
21761 UNIFY_ALLOW_LESS_CV_QUAL:
21762 Allow the deduced ARG to be less cv-qualified than ARG.
21763 UNIFY_ALLOW_DERIVED:
21764 Allow the deduced ARG to be a template base class of ARG,
21765 or a pointer to a template base class of the type pointed to by
21767 UNIFY_ALLOW_INTEGER:
21768 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21769 case for more information.
21770 UNIFY_ALLOW_OUTER_LEVEL:
21771 This is the outermost level of a deduction. Used to determine validity
21772 of qualification conversions. A valid qualification conversion must
21773 have const qualified pointers leading up to the inner type which
21774 requires additional CV quals, except at the outer level, where const
21775 is not required [conv.qual]. It would be normal to set this flag in
21776 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21777 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21778 This is the outermost level of a deduction, and PARM can be more CV
21779 qualified at this point.
21780 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21781 This is the outermost level of a deduction, and PARM can be less CV
21782 qualified at this point. */
21785 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
21791 int strict_in
= strict
;
21792 tsubst_flags_t complain
= (explain_p
21793 ? tf_warning_or_error
21796 /* I don't think this will do the right thing with respect to types.
21797 But the only case I've seen it in so far has been array bounds, where
21798 signedness is the only information lost, and I think that will be
21800 while (CONVERT_EXPR_P (parm
))
21801 parm
= TREE_OPERAND (parm
, 0);
21803 if (arg
== error_mark_node
)
21804 return unify_invalid (explain_p
);
21805 if (arg
== unknown_type_node
21806 || arg
== init_list_type_node
)
21807 /* We can't deduce anything from this, but we might get all the
21808 template args from other function args. */
21809 return unify_success (explain_p
);
21811 if (parm
== any_targ_node
|| arg
== any_targ_node
)
21812 return unify_success (explain_p
);
21814 /* If PARM uses template parameters, then we can't bail out here,
21815 even if ARG == PARM, since we won't record unifications for the
21816 template parameters. We might need them if we're trying to
21817 figure out which of two things is more specialized. */
21818 if (arg
== parm
&& !uses_template_parms (parm
))
21819 return unify_success (explain_p
);
21821 /* Handle init lists early, so the rest of the function can assume
21822 we're dealing with a type. */
21823 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
21827 tree orig_parm
= parm
;
21829 /* Replace T with std::initializer_list<T> for deduction. */
21830 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21831 && flag_deduce_init_list
)
21832 parm
= listify (parm
);
21834 if (!is_std_init_list (parm
)
21835 && TREE_CODE (parm
) != ARRAY_TYPE
)
21836 /* We can only deduce from an initializer list argument if the
21837 parameter is std::initializer_list or an array; otherwise this
21838 is a non-deduced context. */
21839 return unify_success (explain_p
);
21841 if (TREE_CODE (parm
) == ARRAY_TYPE
)
21842 elttype
= TREE_TYPE (parm
);
21845 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
21846 /* Deduction is defined in terms of a single type, so just punt
21847 on the (bizarre) std::initializer_list<T...>. */
21848 if (PACK_EXPANSION_P (elttype
))
21849 return unify_success (explain_p
);
21852 if (strict
!= DEDUCE_EXACT
21853 && TYPE_P (elttype
)
21854 && !uses_deducible_template_parms (elttype
))
21855 /* If ELTTYPE has no deducible template parms, skip deduction from
21856 the list elements. */;
21858 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
21860 int elt_strict
= strict
;
21862 if (elt
== error_mark_node
)
21863 return unify_invalid (explain_p
);
21865 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
21867 tree type
= TREE_TYPE (elt
);
21868 if (type
== error_mark_node
)
21869 return unify_invalid (explain_p
);
21870 /* It should only be possible to get here for a call. */
21871 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
21872 elt_strict
|= maybe_adjust_types_for_deduction
21873 (DEDUCE_CALL
, &elttype
, &type
, elt
);
21877 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
21881 if (TREE_CODE (parm
) == ARRAY_TYPE
21882 && deducible_array_bound (TYPE_DOMAIN (parm
)))
21884 /* Also deduce from the length of the initializer list. */
21885 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
21886 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
21887 if (idx
== error_mark_node
)
21888 return unify_invalid (explain_p
);
21889 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21893 /* If the std::initializer_list<T> deduction worked, replace the
21894 deduced A with std::initializer_list<A>. */
21895 if (orig_parm
!= parm
)
21897 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
21898 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21899 targ
= listify (targ
);
21900 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
21902 return unify_success (explain_p
);
21905 /* If parm and arg aren't the same kind of thing (template, type, or
21906 expression), fail early. */
21907 if (pa_kind (parm
) != pa_kind (arg
))
21908 return unify_invalid (explain_p
);
21910 /* Immediately reject some pairs that won't unify because of
21911 cv-qualification mismatches. */
21912 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21914 /* It is the elements of the array which hold the cv quals of an array
21915 type, and the elements might be template type parms. We'll check
21916 when we recurse. */
21917 && TREE_CODE (arg
) != ARRAY_TYPE
21918 /* We check the cv-qualifiers when unifying with template type
21919 parameters below. We want to allow ARG `const T' to unify with
21920 PARM `T' for example, when computing which of two templates
21921 is more specialized, for example. */
21922 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
21923 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21924 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21926 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21927 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21928 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21929 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21930 strict
&= ~UNIFY_ALLOW_DERIVED
;
21931 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21932 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21934 switch (TREE_CODE (parm
))
21936 case TYPENAME_TYPE
:
21938 case UNBOUND_CLASS_TEMPLATE
:
21939 /* In a type which contains a nested-name-specifier, template
21940 argument values cannot be deduced for template parameters used
21941 within the nested-name-specifier. */
21942 return unify_success (explain_p
);
21944 case TEMPLATE_TYPE_PARM
:
21945 case TEMPLATE_TEMPLATE_PARM
:
21946 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21947 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21948 if (error_operand_p (tparm
))
21949 return unify_invalid (explain_p
);
21951 if (TEMPLATE_TYPE_LEVEL (parm
)
21952 != template_decl_level (tparm
))
21953 /* The PARM is not one we're trying to unify. Just check
21954 to see if it matches ARG. */
21956 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21957 && (is_auto (parm
) ? is_auto (arg
)
21958 : same_type_p (parm
, arg
)))
21959 return unify_success (explain_p
);
21961 return unify_type_mismatch (explain_p
, parm
, arg
);
21963 idx
= TEMPLATE_TYPE_IDX (parm
);
21964 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21965 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21966 if (error_operand_p (tparm
))
21967 return unify_invalid (explain_p
);
21969 /* Check for mixed types and values. */
21970 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21971 && TREE_CODE (tparm
) != TYPE_DECL
)
21972 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21973 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21974 gcc_unreachable ();
21976 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21978 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21979 && CLASS_TYPE_P (arg
))
21981 /* First try to match ARG directly. */
21982 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21986 /* Otherwise, look for a suitable base of ARG, as below. */
21987 enum template_base_result r
;
21988 r
= get_template_base (tparms
, targs
, parm
, arg
,
21991 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21995 /* ARG must be constructed from a template class or a template
21996 template parameter. */
21997 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21998 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21999 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22001 /* Deduce arguments T, i from TT<T> or TT<i>. */
22002 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22005 arg
= TYPE_TI_TEMPLATE (arg
);
22007 /* Fall through to deduce template name. */
22010 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22011 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22013 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22015 /* Simple cases: Value already set, does match or doesn't. */
22016 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22017 return unify_success (explain_p
);
22019 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22023 /* If PARM is `const T' and ARG is only `int', we don't have
22024 a match unless we are allowing additional qualification.
22025 If ARG is `const int' and PARM is just `T' that's OK;
22026 that binds `const int' to `T'. */
22027 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22029 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22031 /* Consider the case where ARG is `const volatile int' and
22032 PARM is `const T'. Then, T should be `volatile int'. */
22033 arg
= cp_build_qualified_type_real
22034 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22035 if (arg
== error_mark_node
)
22036 return unify_invalid (explain_p
);
22038 /* Simple cases: Value already set, does match or doesn't. */
22039 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22040 return unify_success (explain_p
);
22042 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22044 /* Make sure that ARG is not a variable-sized array. (Note
22045 that were talking about variable-sized arrays (like
22046 `int[n]'), rather than arrays of unknown size (like
22047 `int[]').) We'll get very confused by such a type since
22048 the bound of the array is not constant, and therefore
22049 not mangleable. Besides, such types are not allowed in
22050 ISO C++, so we can do as we please here. We do allow
22051 them for 'auto' deduction, since that isn't ABI-exposed. */
22052 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22053 return unify_vla_arg (explain_p
, arg
);
22055 /* Strip typedefs as in convert_template_argument. */
22056 arg
= canonicalize_type_argument (arg
, tf_none
);
22059 /* If ARG is a parameter pack or an expansion, we cannot unify
22060 against it unless PARM is also a parameter pack. */
22061 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22062 && !template_parameter_pack_p (parm
))
22063 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22065 /* If the argument deduction results is a METHOD_TYPE,
22066 then there is a problem.
22067 METHOD_TYPE doesn't map to any real C++ type the result of
22068 the deduction cannot be of that type. */
22069 if (TREE_CODE (arg
) == METHOD_TYPE
)
22070 return unify_method_type_error (explain_p
, arg
);
22072 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22073 return unify_success (explain_p
);
22075 case TEMPLATE_PARM_INDEX
:
22076 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22077 if (error_operand_p (tparm
))
22078 return unify_invalid (explain_p
);
22080 if (TEMPLATE_PARM_LEVEL (parm
)
22081 != template_decl_level (tparm
))
22083 /* The PARM is not one we're trying to unify. Just check
22084 to see if it matches ARG. */
22085 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22086 && cp_tree_equal (parm
, arg
));
22088 unify_expression_unequal (explain_p
, parm
, arg
);
22092 idx
= TEMPLATE_PARM_IDX (parm
);
22093 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22097 if ((strict
& UNIFY_ALLOW_INTEGER
)
22098 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22099 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22100 /* We're deducing from an array bound, the type doesn't matter. */
22101 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22102 int x
= !cp_tree_equal (targ
, arg
);
22104 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22108 /* [temp.deduct.type] If, in the declaration of a function template
22109 with a non-type template-parameter, the non-type
22110 template-parameter is used in an expression in the function
22111 parameter-list and, if the corresponding template-argument is
22112 deduced, the template-argument type shall match the type of the
22113 template-parameter exactly, except that a template-argument
22114 deduced from an array bound may be of any integral type.
22115 The non-type parameter might use already deduced type parameters. */
22116 tparm
= TREE_TYPE (parm
);
22117 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22118 /* We don't have enough levels of args to do any substitution. This
22119 can happen in the context of -fnew-ttp-matching. */;
22122 ++processing_template_decl
;
22123 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22124 --processing_template_decl
;
22126 if (tree a
= type_uses_auto (tparm
))
22128 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22129 if (tparm
== error_mark_node
)
22134 if (!TREE_TYPE (arg
))
22135 /* Template-parameter dependent expression. Just accept it for now.
22136 It will later be processed in convert_template_argument. */
22138 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
22139 non_reference (tparm
)))
22141 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22142 && CP_INTEGRAL_TYPE_P (tparm
))
22143 /* Convert the ARG to the type of PARM; the deduced non-type
22144 template argument must exactly match the types of the
22145 corresponding parameter. */
22146 arg
= fold (build_nop (tparm
, arg
));
22147 else if (uses_template_parms (tparm
))
22149 /* We haven't deduced the type of this parameter yet. */
22150 if (cxx_dialect
>= cxx17
22151 /* We deduce from array bounds in try_array_deduction. */
22152 && !(strict
& UNIFY_ALLOW_INTEGER
))
22154 /* Deduce it from the non-type argument. */
22155 tree atype
= TREE_TYPE (arg
);
22156 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22158 UNIFY_ALLOW_NONE
, explain_p
);
22161 /* Try again later. */
22162 return unify_success (explain_p
);
22165 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22167 /* If ARG is a parameter pack or an expansion, we cannot unify
22168 against it unless PARM is also a parameter pack. */
22169 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22170 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22171 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22174 bool removed_attr
= false;
22175 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22177 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22178 return unify_success (explain_p
);
22182 /* A pointer-to-member constant can be unified only with
22183 another constant. */
22184 if (TREE_CODE (arg
) != PTRMEM_CST
)
22185 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22187 /* Just unify the class member. It would be useless (and possibly
22188 wrong, depending on the strict flags) to unify also
22189 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22190 arg refer to the same variable, even if through different
22191 classes. For instance:
22193 struct A { int x; };
22196 Unification of &A::x and &B::x must succeed. */
22197 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22198 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
22203 if (!TYPE_PTR_P (arg
))
22204 return unify_type_mismatch (explain_p
, parm
, arg
);
22206 /* [temp.deduct.call]
22208 A can be another pointer or pointer to member type that can
22209 be converted to the deduced A via a qualification
22210 conversion (_conv.qual_).
22212 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22213 This will allow for additional cv-qualification of the
22214 pointed-to types if appropriate. */
22216 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
22217 /* The derived-to-base conversion only persists through one
22218 level of pointers. */
22219 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
22221 return unify (tparms
, targs
, TREE_TYPE (parm
),
22222 TREE_TYPE (arg
), strict
, explain_p
);
22225 case REFERENCE_TYPE
:
22226 if (!TYPE_REF_P (arg
))
22227 return unify_type_mismatch (explain_p
, parm
, arg
);
22228 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22229 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22232 if (TREE_CODE (arg
) != ARRAY_TYPE
)
22233 return unify_type_mismatch (explain_p
, parm
, arg
);
22234 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
22235 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
22236 return unify_type_mismatch (explain_p
, parm
, arg
);
22237 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22238 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22239 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
22240 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22241 TYPE_DOMAIN (arg
), explain_p
);
22242 return unify_success (explain_p
);
22249 case ENUMERAL_TYPE
:
22252 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22253 return unify_type_mismatch (explain_p
, parm
, arg
);
22255 /* We have already checked cv-qualification at the top of the
22257 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
22258 return unify_type_mismatch (explain_p
, parm
, arg
);
22260 /* As far as unification is concerned, this wins. Later checks
22261 will invalidate it if necessary. */
22262 return unify_success (explain_p
);
22264 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22265 /* Type INTEGER_CST can come from ordinary constant template args. */
22267 while (CONVERT_EXPR_P (arg
))
22268 arg
= TREE_OPERAND (arg
, 0);
22270 if (TREE_CODE (arg
) != INTEGER_CST
)
22271 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22272 return (tree_int_cst_equal (parm
, arg
)
22273 ? unify_success (explain_p
)
22274 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
22278 int i
, len
, argslen
;
22279 int parm_variadic_p
= 0;
22281 if (TREE_CODE (arg
) != TREE_VEC
)
22282 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22284 len
= TREE_VEC_LENGTH (parm
);
22285 argslen
= TREE_VEC_LENGTH (arg
);
22287 /* Check for pack expansions in the parameters. */
22288 for (i
= 0; i
< len
; ++i
)
22290 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
22293 /* We can unify against something with a trailing
22295 parm_variadic_p
= 1;
22297 /* [temp.deduct.type]/9: If the template argument list of
22298 P contains a pack expansion that is not the last
22299 template argument, the entire template argument list
22300 is a non-deduced context. */
22301 return unify_success (explain_p
);
22305 /* If we don't have enough arguments to satisfy the parameters
22306 (not counting the pack expression at the end), or we have
22307 too many arguments for a parameter list that doesn't end in
22308 a pack expression, we can't unify. */
22309 if (parm_variadic_p
22310 ? argslen
< len
- parm_variadic_p
22312 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
22314 /* Unify all of the parameters that precede the (optional)
22315 pack expression. */
22316 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
22318 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22319 TREE_VEC_ELT (parm
, i
),
22320 TREE_VEC_ELT (arg
, i
),
22321 UNIFY_ALLOW_NONE
, explain_p
);
22323 if (parm_variadic_p
)
22324 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
22326 /*subr=*/true, explain_p
);
22327 return unify_success (explain_p
);
22332 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22333 return unify_type_mismatch (explain_p
, parm
, arg
);
22335 if (TYPE_PTRMEMFUNC_P (parm
))
22337 if (!TYPE_PTRMEMFUNC_P (arg
))
22338 return unify_type_mismatch (explain_p
, parm
, arg
);
22340 return unify (tparms
, targs
,
22341 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
22342 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
22343 strict
, explain_p
);
22345 else if (TYPE_PTRMEMFUNC_P (arg
))
22346 return unify_type_mismatch (explain_p
, parm
, arg
);
22348 if (CLASSTYPE_TEMPLATE_INFO (parm
))
22350 tree t
= NULL_TREE
;
22352 if (strict_in
& UNIFY_ALLOW_DERIVED
)
22354 /* First, we try to unify the PARM and ARG directly. */
22355 t
= try_class_unification (tparms
, targs
,
22356 parm
, arg
, explain_p
);
22360 /* Fallback to the special case allowed in
22361 [temp.deduct.call]:
22363 If P is a class, and P has the form
22364 template-id, then A can be a derived class of
22365 the deduced A. Likewise, if P is a pointer to
22366 a class of the form template-id, A can be a
22367 pointer to a derived class pointed to by the
22369 enum template_base_result r
;
22370 r
= get_template_base (tparms
, targs
, parm
, arg
,
22375 /* Don't give the derived diagnostic if we're
22376 already dealing with the same template. */
22378 = (CLASSTYPE_TEMPLATE_INFO (arg
)
22379 && (CLASSTYPE_TI_TEMPLATE (parm
)
22380 == CLASSTYPE_TI_TEMPLATE (arg
)));
22381 return unify_no_common_base (explain_p
&& !same_template
,
22386 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22387 && (CLASSTYPE_TI_TEMPLATE (parm
)
22388 == CLASSTYPE_TI_TEMPLATE (arg
)))
22389 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22390 Then, we should unify `int' and `U'. */
22393 /* There's no chance of unification succeeding. */
22394 return unify_type_mismatch (explain_p
, parm
, arg
);
22396 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22397 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22399 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22400 return unify_type_mismatch (explain_p
, parm
, arg
);
22401 return unify_success (explain_p
);
22404 case FUNCTION_TYPE
:
22406 unsigned int nargs
;
22411 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22412 return unify_type_mismatch (explain_p
, parm
, arg
);
22414 /* CV qualifications for methods can never be deduced, they must
22415 match exactly. We need to check them explicitly here,
22416 because type_unification_real treats them as any other
22417 cv-qualified parameter. */
22418 if (TREE_CODE (parm
) == METHOD_TYPE
22419 && (!check_cv_quals_for_unify
22421 class_of_this_parm (arg
),
22422 class_of_this_parm (parm
))))
22423 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22424 if (TREE_CODE (arg
) == FUNCTION_TYPE
22425 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22426 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22427 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22428 return unify_type_mismatch (explain_p
, parm
, arg
);
22430 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22431 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22433 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22434 args
= XALLOCAVEC (tree
, nargs
);
22435 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22436 a
!= NULL_TREE
&& a
!= void_list_node
;
22437 a
= TREE_CHAIN (a
), ++i
)
22438 args
[i
] = TREE_VALUE (a
);
22441 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22442 args
, nargs
, 1, DEDUCE_EXACT
,
22446 if (flag_noexcept_type
)
22448 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22449 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22450 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22451 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22452 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22453 && uses_template_parms (TREE_PURPOSE (pspec
)))
22454 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22455 TREE_PURPOSE (aspec
),
22456 UNIFY_ALLOW_NONE
, explain_p
);
22457 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22458 return unify_type_mismatch (explain_p
, parm
, arg
);
22465 /* Unify a pointer to member with a pointer to member function, which
22466 deduces the type of the member as a function type. */
22467 if (TYPE_PTRMEMFUNC_P (arg
))
22469 /* Check top-level cv qualifiers */
22470 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22471 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22473 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22474 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22475 UNIFY_ALLOW_NONE
, explain_p
);
22477 /* Determine the type of the function we are unifying against. */
22478 tree fntype
= static_fn_type (arg
);
22480 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22483 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22484 return unify_type_mismatch (explain_p
, parm
, arg
);
22485 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22486 TYPE_OFFSET_BASETYPE (arg
),
22487 UNIFY_ALLOW_NONE
, explain_p
);
22488 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22489 strict
, explain_p
);
22492 if (DECL_TEMPLATE_PARM_P (parm
))
22493 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22494 if (arg
!= scalar_constant_value (parm
))
22495 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22496 return unify_success (explain_p
);
22499 case TEMPLATE_DECL
:
22500 /* Matched cases are handled by the ARG == PARM test above. */
22501 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22504 /* We might get a variable as a non-type template argument in parm if the
22505 corresponding parameter is type-dependent. Make any necessary
22506 adjustments based on whether arg is a reference. */
22507 if (CONSTANT_CLASS_P (arg
))
22508 parm
= fold_non_dependent_expr (parm
, complain
);
22509 else if (REFERENCE_REF_P (arg
))
22511 tree sub
= TREE_OPERAND (arg
, 0);
22513 if (TREE_CODE (sub
) == ADDR_EXPR
)
22514 arg
= TREE_OPERAND (sub
, 0);
22516 /* Now use the normal expression code to check whether they match. */
22519 case TYPE_ARGUMENT_PACK
:
22520 case NONTYPE_ARGUMENT_PACK
:
22521 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22522 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22525 case DECLTYPE_TYPE
:
22526 case UNDERLYING_TYPE
:
22527 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22528 or UNDERLYING_TYPE nodes. */
22529 return unify_success (explain_p
);
22532 /* Unification fails if we hit an error node. */
22533 return unify_invalid (explain_p
);
22536 if (REFERENCE_REF_P (parm
))
22538 bool pexp
= PACK_EXPANSION_P (arg
);
22540 arg
= PACK_EXPANSION_PATTERN (arg
);
22541 if (REFERENCE_REF_P (arg
))
22542 arg
= TREE_OPERAND (arg
, 0);
22544 arg
= make_pack_expansion (arg
, complain
);
22545 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22546 strict
, explain_p
);
22551 /* An unresolved overload is a nondeduced context. */
22552 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22553 return unify_success (explain_p
);
22554 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
22556 /* We must be looking at an expression. This can happen with
22560 void foo(S<I>, S<I + 2>);
22562 This is a "nondeduced context":
22566 The nondeduced contexts are:
22568 --A type that is a template-id in which one or more of
22569 the template-arguments is an expression that references
22570 a template-parameter.
22572 In these cases, we assume deduction succeeded, but don't
22573 actually infer any unifications. */
22575 if (!uses_template_parms (parm
)
22576 && !template_args_equal (parm
, arg
))
22577 return unify_expression_unequal (explain_p
, parm
, arg
);
22579 return unify_success (explain_p
);
22582 #undef RECUR_AND_CHECK_FAILURE
22584 /* Note that DECL can be defined in this translation unit, if
22588 mark_definable (tree decl
)
22591 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22592 FOR_EACH_CLONE (clone
, decl
)
22593 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22596 /* Called if RESULT is explicitly instantiated, or is a member of an
22597 explicitly instantiated class. */
22600 mark_decl_instantiated (tree result
, int extern_p
)
22602 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22604 /* If this entity has already been written out, it's too late to
22605 make any modifications. */
22606 if (TREE_ASM_WRITTEN (result
))
22609 /* For anonymous namespace we don't need to do anything. */
22610 if (decl_anon_ns_mem_p (result
))
22612 gcc_assert (!TREE_PUBLIC (result
));
22616 if (TREE_CODE (result
) != FUNCTION_DECL
)
22617 /* The TREE_PUBLIC flag for function declarations will have been
22618 set correctly by tsubst. */
22619 TREE_PUBLIC (result
) = 1;
22621 /* This might have been set by an earlier implicit instantiation. */
22622 DECL_COMDAT (result
) = 0;
22625 DECL_NOT_REALLY_EXTERN (result
) = 0;
22628 mark_definable (result
);
22629 mark_needed (result
);
22630 /* Always make artificials weak. */
22631 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22632 comdat_linkage (result
);
22633 /* For WIN32 we also want to put explicit instantiations in
22634 linkonce sections. */
22635 else if (TREE_PUBLIC (result
))
22636 maybe_make_one_only (result
);
22637 if (TREE_CODE (result
) == FUNCTION_DECL
22638 && DECL_TEMPLATE_INSTANTIATED (result
))
22639 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22640 since start_preparsed_function wouldn't have if we had an earlier
22641 extern explicit instantiation. */
22642 DECL_EXTERNAL (result
) = 0;
22645 /* If EXTERN_P, then this function will not be emitted -- unless
22646 followed by an explicit instantiation, at which point its linkage
22647 will be adjusted. If !EXTERN_P, then this function will be
22648 emitted here. In neither circumstance do we want
22649 import_export_decl to adjust the linkage. */
22650 DECL_INTERFACE_KNOWN (result
) = 1;
22653 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22654 important template arguments. If any are missing, we check whether
22655 they're important by using error_mark_node for substituting into any
22656 args that were used for partial ordering (the ones between ARGS and END)
22657 and seeing if it bubbles up. */
22660 check_undeduced_parms (tree targs
, tree args
, tree end
)
22662 bool found
= false;
22664 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22665 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22668 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22672 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22673 if (substed
== error_mark_node
)
22679 /* Given two function templates PAT1 and PAT2, return:
22681 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22682 -1 if PAT2 is more specialized than PAT1.
22683 0 if neither is more specialized.
22685 LEN indicates the number of parameters we should consider
22686 (defaulted parameters should not be considered).
22688 The 1998 std underspecified function template partial ordering, and
22689 DR214 addresses the issue. We take pairs of arguments, one from
22690 each of the templates, and deduce them against each other. One of
22691 the templates will be more specialized if all the *other*
22692 template's arguments deduce against its arguments and at least one
22693 of its arguments *does* *not* deduce against the other template's
22694 corresponding argument. Deduction is done as for class templates.
22695 The arguments used in deduction have reference and top level cv
22696 qualifiers removed. Iff both arguments were originally reference
22697 types *and* deduction succeeds in both directions, an lvalue reference
22698 wins against an rvalue reference and otherwise the template
22699 with the more cv-qualified argument wins for that pairing (if
22700 neither is more cv-qualified, they both are equal). Unlike regular
22701 deduction, after all the arguments have been deduced in this way,
22702 we do *not* verify the deduced template argument values can be
22703 substituted into non-deduced contexts.
22705 The logic can be a bit confusing here, because we look at deduce1 and
22706 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22707 can find template arguments for pat1 to make arg1 look like arg2, that
22708 means that arg2 is at least as specialized as arg1. */
22711 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22713 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22714 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22715 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22716 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22717 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22718 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22719 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22720 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22721 tree origs1
, origs2
;
22722 bool lose1
= false;
22723 bool lose2
= false;
22725 /* Remove the this parameter from non-static member functions. If
22726 one is a non-static member function and the other is not a static
22727 member function, remove the first parameter from that function
22728 also. This situation occurs for operator functions where we
22729 locate both a member function (with this pointer) and non-member
22730 operator (with explicit first operand). */
22731 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
22733 len
--; /* LEN is the number of significant arguments for DECL1 */
22734 args1
= TREE_CHAIN (args1
);
22735 if (!DECL_STATIC_FUNCTION_P (decl2
))
22736 args2
= TREE_CHAIN (args2
);
22738 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
22740 args2
= TREE_CHAIN (args2
);
22741 if (!DECL_STATIC_FUNCTION_P (decl1
))
22744 args1
= TREE_CHAIN (args1
);
22748 /* If only one is a conversion operator, they are unordered. */
22749 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
22752 /* Consider the return type for a conversion function */
22753 if (DECL_CONV_FN_P (decl1
))
22755 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
22756 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
22760 processing_template_decl
++;
22766 /* Stop when an ellipsis is seen. */
22767 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
22769 tree arg1
= TREE_VALUE (args1
);
22770 tree arg2
= TREE_VALUE (args2
);
22771 int deduce1
, deduce2
;
22777 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22778 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22780 /* When both arguments are pack expansions, we need only
22781 unify the patterns themselves. */
22782 arg1
= PACK_EXPANSION_PATTERN (arg1
);
22783 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22785 /* This is the last comparison we need to do. */
22789 /* DR 1847: If a particular P contains no template-parameters that
22790 participate in template argument deduction, that P is not used to
22791 determine the ordering. */
22792 if (!uses_deducible_template_parms (arg1
)
22793 && !uses_deducible_template_parms (arg2
))
22796 if (TYPE_REF_P (arg1
))
22798 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
22799 arg1
= TREE_TYPE (arg1
);
22800 quals1
= cp_type_quals (arg1
);
22803 if (TYPE_REF_P (arg2
))
22805 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
22806 arg2
= TREE_TYPE (arg2
);
22807 quals2
= cp_type_quals (arg2
);
22810 arg1
= TYPE_MAIN_VARIANT (arg1
);
22811 arg2
= TYPE_MAIN_VARIANT (arg2
);
22813 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
22815 int i
, len2
= remaining_arguments (args2
);
22816 tree parmvec
= make_tree_vec (1);
22817 tree argvec
= make_tree_vec (len2
);
22820 /* Setup the parameter vector, which contains only ARG1. */
22821 TREE_VEC_ELT (parmvec
, 0) = arg1
;
22823 /* Setup the argument vector, which contains the remaining
22825 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
22826 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22828 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
22829 argvec
, DEDUCE_EXACT
,
22830 /*subr=*/true, /*explain_p=*/false)
22833 /* We cannot deduce in the other direction, because ARG1 is
22834 a pack expansion but ARG2 is not. */
22837 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22839 int i
, len1
= remaining_arguments (args1
);
22840 tree parmvec
= make_tree_vec (1);
22841 tree argvec
= make_tree_vec (len1
);
22844 /* Setup the parameter vector, which contains only ARG1. */
22845 TREE_VEC_ELT (parmvec
, 0) = arg2
;
22847 /* Setup the argument vector, which contains the remaining
22849 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
22850 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22852 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
22853 argvec
, DEDUCE_EXACT
,
22854 /*subr=*/true, /*explain_p=*/false)
22857 /* We cannot deduce in the other direction, because ARG2 is
22858 a pack expansion but ARG1 is not.*/
22864 /* The normal case, where neither argument is a pack
22866 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
22867 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22869 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
22870 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22874 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22875 arg2, then arg2 is not as specialized as arg1. */
22881 /* "If, for a given type, deduction succeeds in both directions
22882 (i.e., the types are identical after the transformations above)
22883 and both P and A were reference types (before being replaced with
22884 the type referred to above):
22885 - if the type from the argument template was an lvalue reference and
22886 the type from the parameter template was not, the argument type is
22887 considered to be more specialized than the other; otherwise,
22888 - if the type from the argument template is more cv-qualified
22889 than the type from the parameter template (as described above),
22890 the argument type is considered to be more specialized than the other;
22892 - neither type is more specialized than the other." */
22894 if (deduce1
&& deduce2
)
22896 if (ref1
&& ref2
&& ref1
!= ref2
)
22903 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
22905 if ((quals1
& quals2
) == quals2
)
22907 if ((quals1
& quals2
) == quals1
)
22912 if (lose1
&& lose2
)
22913 /* We've failed to deduce something in either direction.
22914 These must be unordered. */
22919 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22920 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22921 /* We have already processed all of the arguments in our
22922 handing of the pack expansion type. */
22925 args1
= TREE_CHAIN (args1
);
22926 args2
= TREE_CHAIN (args2
);
22929 /* "In most cases, all template parameters must have values in order for
22930 deduction to succeed, but for partial ordering purposes a template
22931 parameter may remain without a value provided it is not used in the
22932 types being used for partial ordering."
22934 Thus, if we are missing any of the targs1 we need to substitute into
22935 origs1, then pat2 is not as specialized as pat1. This can happen when
22936 there is a nondeduced context. */
22937 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22939 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22942 processing_template_decl
--;
22944 /* If both deductions succeed, the partial ordering selects the more
22945 constrained template. */
22946 if (!lose1
&& !lose2
)
22948 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22949 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22950 lose1
= !subsumes_constraints (c1
, c2
);
22951 lose2
= !subsumes_constraints (c2
, c1
);
22954 /* All things being equal, if the next argument is a pack expansion
22955 for one function but not for the other, prefer the
22956 non-variadic function. FIXME this is bogus; see c++/41958. */
22958 && args1
&& TREE_VALUE (args1
)
22959 && args2
&& TREE_VALUE (args2
))
22961 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22962 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22965 if (lose1
== lose2
)
22973 /* Determine which of two partial specializations of TMPL is more
22976 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22977 to the first partial specialization. The TREE_PURPOSE is the
22978 innermost set of template parameters for the partial
22979 specialization. PAT2 is similar, but for the second template.
22981 Return 1 if the first partial specialization is more specialized;
22982 -1 if the second is more specialized; 0 if neither is more
22985 See [temp.class.order] for information about determining which of
22986 two templates is more specialized. */
22989 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22993 bool any_deductions
= false;
22995 tree tmpl1
= TREE_VALUE (pat1
);
22996 tree tmpl2
= TREE_VALUE (pat2
);
22997 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22998 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23000 /* Just like what happens for functions, if we are ordering between
23001 different template specializations, we may encounter dependent
23002 types in the arguments, and we need our dependency check functions
23003 to behave correctly. */
23004 ++processing_template_decl
;
23005 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23009 any_deductions
= true;
23012 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23016 any_deductions
= true;
23018 --processing_template_decl
;
23020 /* If both deductions succeed, the partial ordering selects the more
23021 constrained template. */
23022 if (!winner
&& any_deductions
)
23023 return more_constrained (tmpl1
, tmpl2
);
23025 /* In the case of a tie where at least one of the templates
23026 has a parameter pack at the end, the template with the most
23027 non-packed parameters wins. */
23030 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23031 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23033 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23034 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23035 int len1
= TREE_VEC_LENGTH (args1
);
23036 int len2
= TREE_VEC_LENGTH (args2
);
23038 /* We don't count the pack expansion at the end. */
23039 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23041 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23046 else if (len1
< len2
)
23053 /* Return the template arguments that will produce the function signature
23054 DECL from the function template FN, with the explicit template
23055 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23056 also match. Return NULL_TREE if no satisfactory arguments could be
23060 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23062 int ntparms
= DECL_NTPARMS (fn
);
23063 tree targs
= make_tree_vec (ntparms
);
23064 tree decl_type
= TREE_TYPE (decl
);
23065 tree decl_arg_types
;
23067 unsigned int nargs
, ix
;
23070 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23072 /* Never do unification on the 'this' parameter. */
23073 decl_arg_types
= skip_artificial_parms_for (decl
,
23074 TYPE_ARG_TYPES (decl_type
));
23076 nargs
= list_length (decl_arg_types
);
23077 args
= XALLOCAVEC (tree
, nargs
);
23078 for (arg
= decl_arg_types
, ix
= 0;
23079 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23080 arg
= TREE_CHAIN (arg
), ++ix
)
23081 args
[ix
] = TREE_VALUE (arg
);
23083 if (fn_type_unification (fn
, explicit_args
, targs
,
23085 (check_rettype
|| DECL_CONV_FN_P (fn
)
23086 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23087 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23088 /*explain_p=*/false,
23090 == error_mark_node
)
23096 /* Return the innermost template arguments that, when applied to a partial
23097 specialization SPEC_TMPL of TMPL, yield the ARGS.
23099 For example, suppose we have:
23101 template <class T, class U> struct S {};
23102 template <class T> struct S<T*, int> {};
23104 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23105 partial specialization and the ARGS will be {double*, int}. The resulting
23106 vector will be {double}, indicating that `T' is bound to `double'. */
23109 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23111 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23113 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23114 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23116 tree innermost_deduced_args
;
23118 innermost_deduced_args
= make_tree_vec (ntparms
);
23119 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23121 deduced_args
= copy_node (args
);
23122 SET_TMPL_ARGS_LEVEL (deduced_args
,
23123 TMPL_ARGS_DEPTH (deduced_args
),
23124 innermost_deduced_args
);
23127 deduced_args
= innermost_deduced_args
;
23129 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23131 if (unify (tparms
, deduced_args
,
23132 INNERMOST_TEMPLATE_ARGS (spec_args
),
23133 INNERMOST_TEMPLATE_ARGS (args
),
23134 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23137 for (i
= 0; i
< ntparms
; ++i
)
23138 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23140 if (!tried_array_deduction
)
23142 try_array_deduction (tparms
, innermost_deduced_args
,
23143 INNERMOST_TEMPLATE_ARGS (spec_args
));
23144 tried_array_deduction
= true;
23145 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23151 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23153 excessive_deduction_depth
= true;
23157 /* Verify that nondeduced template arguments agree with the type
23158 obtained from argument deduction.
23162 struct A { typedef int X; };
23163 template <class T, class U> struct C {};
23164 template <class T> struct C<T, typename T::X> {};
23166 Then with the instantiation `C<A, int>', we can deduce that
23167 `T' is `A' but unify () does not check whether `typename T::X'
23169 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23171 if (spec_args
!= error_mark_node
)
23172 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23173 INNERMOST_TEMPLATE_ARGS (spec_args
),
23174 tmpl
, tf_none
, false, false);
23176 pop_tinst_level ();
23178 if (spec_args
== error_mark_node
23179 /* We only need to check the innermost arguments; the other
23180 arguments will always agree. */
23181 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23182 INNERMOST_TEMPLATE_ARGS (args
)))
23185 /* Now that we have bindings for all of the template arguments,
23186 ensure that the arguments deduced for the template template
23187 parameters have compatible template parameter lists. See the use
23188 of template_template_parm_bindings_ok_p in fn_type_unification
23189 for more information. */
23190 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23193 return deduced_args
;
23196 // Compare two function templates T1 and T2 by deducing bindings
23197 // from one against the other. If both deductions succeed, compare
23198 // constraints to see which is more constrained.
23200 more_specialized_inst (tree t1
, tree t2
)
23205 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
23211 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
23217 // If both deductions succeed, then one may be more constrained.
23218 if (count
== 2 && fate
== 0)
23219 fate
= more_constrained (t1
, t2
);
23224 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23225 Return the TREE_LIST node with the most specialized template, if
23226 any. If there is no most specialized template, the error_mark_node
23229 Note that this function does not look at, or modify, the
23230 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23231 returned is one of the elements of INSTANTIATIONS, callers may
23232 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23233 and retrieve it from the value returned. */
23236 most_specialized_instantiation (tree templates
)
23240 ++processing_template_decl
;
23243 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
23245 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
23246 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
23251 /* Equally specialized, move to next function. If there
23252 is no next function, nothing's most specialized. */
23253 fn
= TREE_CHAIN (fn
);
23261 /* Now verify that champ is better than everything earlier in the
23262 instantiation list. */
23263 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
23264 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
23271 processing_template_decl
--;
23274 return error_mark_node
;
23279 /* If DECL is a specialization of some template, return the most
23280 general such template. Otherwise, returns NULL_TREE.
23282 For example, given:
23284 template <class T> struct S { template <class U> void f(U); };
23286 if TMPL is `template <class U> void S<int>::f(U)' this will return
23287 the full template. This function will not trace past partial
23288 specializations, however. For example, given in addition:
23290 template <class T> struct S<T*> { template <class U> void f(U); };
23292 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23293 `template <class T> template <class U> S<T*>::f(U)'. */
23296 most_general_template (tree decl
)
23298 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23300 if (tree tinfo
= get_template_info (decl
))
23301 decl
= TI_TEMPLATE (tinfo
);
23302 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23303 template friend, or a FIELD_DECL for a capture pack. */
23304 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23308 /* Look for more and more general templates. */
23309 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
23311 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23312 (See cp-tree.h for details.) */
23313 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
23316 if (CLASS_TYPE_P (TREE_TYPE (decl
))
23317 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
23318 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
23321 /* Stop if we run into an explicitly specialized class template. */
23322 if (!DECL_NAMESPACE_SCOPE_P (decl
)
23323 && DECL_CONTEXT (decl
)
23324 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
23327 decl
= DECL_TI_TEMPLATE (decl
);
23333 /* Return the most specialized of the template partial specializations
23334 which can produce TARGET, a specialization of some class or variable
23335 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23336 a TEMPLATE_DECL node corresponding to the partial specialization, while
23337 the TREE_PURPOSE is the set of template arguments that must be
23338 substituted into the template pattern in order to generate TARGET.
23340 If the choice of partial specialization is ambiguous, a diagnostic
23341 is issued, and the error_mark_node is returned. If there are no
23342 partial specializations matching TARGET, then NULL_TREE is
23343 returned, indicating that the primary template should be used. */
23346 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
23348 tree list
= NULL_TREE
;
23353 tree outer_args
= NULL_TREE
;
23356 if (TYPE_P (target
))
23358 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
23359 tmpl
= TI_TEMPLATE (tinfo
);
23360 args
= TI_ARGS (tinfo
);
23362 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
23364 tmpl
= TREE_OPERAND (target
, 0);
23365 args
= TREE_OPERAND (target
, 1);
23367 else if (VAR_P (target
))
23369 tree tinfo
= DECL_TEMPLATE_INFO (target
);
23370 tmpl
= TI_TEMPLATE (tinfo
);
23371 args
= TI_ARGS (tinfo
);
23374 gcc_unreachable ();
23376 tree main_tmpl
= most_general_template (tmpl
);
23378 /* For determining which partial specialization to use, only the
23379 innermost args are interesting. */
23380 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23382 outer_args
= strip_innermost_template_args (args
, 1);
23383 args
= INNERMOST_TEMPLATE_ARGS (args
);
23386 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23389 tree spec_tmpl
= TREE_VALUE (t
);
23393 /* Substitute in the template args from the enclosing class. */
23394 ++processing_template_decl
;
23395 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23396 --processing_template_decl
;
23399 if (spec_tmpl
== error_mark_node
)
23400 return error_mark_node
;
23402 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23406 spec_args
= add_to_template_args (outer_args
, spec_args
);
23408 /* Keep the candidate only if the constraints are satisfied,
23409 or if we're not compiling with concepts. */
23411 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23413 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23414 TREE_TYPE (list
) = TREE_TYPE (t
);
23422 ambiguous_p
= false;
23425 t
= TREE_CHAIN (t
);
23426 for (; t
; t
= TREE_CHAIN (t
))
23428 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23435 t
= TREE_CHAIN (t
);
23438 ambiguous_p
= true;
23447 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23449 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23452 ambiguous_p
= true;
23460 char *spaces
= NULL
;
23461 if (!(complain
& tf_error
))
23462 return error_mark_node
;
23463 if (TYPE_P (target
))
23464 error ("ambiguous template instantiation for %q#T", target
);
23466 error ("ambiguous template instantiation for %q#D", target
);
23467 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23468 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23470 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23471 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23472 "%s %#qS", spaces
? spaces
: str
, subst
);
23473 spaces
= spaces
? spaces
: get_spaces (str
);
23476 return error_mark_node
;
23482 /* Explicitly instantiate DECL. */
23485 do_decl_instantiation (tree decl
, tree storage
)
23487 tree result
= NULL_TREE
;
23490 if (!decl
|| decl
== error_mark_node
)
23491 /* An error occurred, for which grokdeclarator has already issued
23492 an appropriate message. */
23494 else if (! DECL_LANG_SPECIFIC (decl
))
23496 error ("explicit instantiation of non-template %q#D", decl
);
23499 else if (DECL_DECLARED_CONCEPT_P (decl
))
23502 error ("explicit instantiation of variable concept %q#D", decl
);
23504 error ("explicit instantiation of function concept %q#D", decl
);
23508 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23509 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23511 if (VAR_P (decl
) && !var_templ
)
23513 /* There is an asymmetry here in the way VAR_DECLs and
23514 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23515 the latter, the DECL we get back will be marked as a
23516 template instantiation, and the appropriate
23517 DECL_TEMPLATE_INFO will be set up. This does not happen for
23518 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23519 should handle VAR_DECLs as it currently handles
23521 if (!DECL_CLASS_SCOPE_P (decl
))
23523 error ("%qD is not a static data member of a class template", decl
);
23526 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23527 if (!result
|| !VAR_P (result
))
23529 error ("no matching template for %qD found", decl
);
23532 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23534 error ("type %qT for explicit instantiation %qD does not match "
23535 "declared type %qT", TREE_TYPE (result
), decl
,
23540 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23542 error ("explicit instantiation of %q#D", decl
);
23548 /* Check for various error cases. Note that if the explicit
23549 instantiation is valid the RESULT will currently be marked as an
23550 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23551 until we get here. */
23553 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23555 /* DR 259 [temp.spec].
23557 Both an explicit instantiation and a declaration of an explicit
23558 specialization shall not appear in a program unless the explicit
23559 instantiation follows a declaration of the explicit specialization.
23561 For a given set of template parameters, if an explicit
23562 instantiation of a template appears after a declaration of an
23563 explicit specialization for that template, the explicit
23564 instantiation has no effect. */
23567 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23571 No program shall explicitly instantiate any template more
23574 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23575 the first instantiation was `extern' and the second is not,
23576 and EXTERN_P for the opposite case. */
23577 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23578 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23579 /* If an "extern" explicit instantiation follows an ordinary
23580 explicit instantiation, the template is instantiated. */
23584 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23586 error ("no matching template for %qD found", result
);
23589 else if (!DECL_TEMPLATE_INFO (result
))
23591 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23595 if (storage
== NULL_TREE
)
23597 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23599 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23600 pedwarn (input_location
, OPT_Wpedantic
,
23601 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23606 error ("storage class %qD applied to template instantiation", storage
);
23608 check_explicit_instantiation_namespace (result
);
23609 mark_decl_instantiated (result
, extern_p
);
23611 instantiate_decl (result
, /*defer_ok=*/true,
23612 /*expl_inst_class_mem_p=*/false);
23616 mark_class_instantiated (tree t
, int extern_p
)
23618 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23619 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23620 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23621 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23624 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23625 rest_of_type_compilation (t
, 1);
23629 /* Called from do_type_instantiation through binding_table_foreach to
23630 do recursive instantiation for the type bound in ENTRY. */
23632 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23634 tree storage
= *(tree
*) data
;
23636 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23637 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23638 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23639 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23642 /* Perform an explicit instantiation of template class T. STORAGE, if
23643 non-null, is the RID for extern, inline or static. COMPLAIN is
23644 nonzero if this is called from the parser, zero if called recursively,
23645 since the standard is unclear (as detailed below). */
23648 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23653 int previous_instantiation_extern_p
= 0;
23655 if (TREE_CODE (t
) == TYPE_DECL
)
23658 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23661 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23663 error ("explicit instantiation of non-class template %qD", tmpl
);
23665 error ("explicit instantiation of non-template type %qT", t
);
23671 if (!COMPLETE_TYPE_P (t
))
23673 if (complain
& tf_error
)
23674 error ("explicit instantiation of %q#T before definition of template",
23679 if (storage
!= NULL_TREE
)
23681 if (!in_system_header_at (input_location
))
23683 if (storage
== ridpointers
[(int) RID_EXTERN
])
23685 if (cxx_dialect
== cxx98
)
23686 pedwarn (input_location
, OPT_Wpedantic
,
23687 "ISO C++ 1998 forbids the use of %<extern%> on "
23688 "explicit instantiations");
23691 pedwarn (input_location
, OPT_Wpedantic
,
23692 "ISO C++ forbids the use of %qE"
23693 " on explicit instantiations", storage
);
23696 if (storage
== ridpointers
[(int) RID_INLINE
])
23698 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23700 else if (storage
== ridpointers
[(int) RID_STATIC
])
23704 error ("storage class %qD applied to template instantiation",
23710 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23712 /* DR 259 [temp.spec].
23714 Both an explicit instantiation and a declaration of an explicit
23715 specialization shall not appear in a program unless the explicit
23716 instantiation follows a declaration of the explicit specialization.
23718 For a given set of template parameters, if an explicit
23719 instantiation of a template appears after a declaration of an
23720 explicit specialization for that template, the explicit
23721 instantiation has no effect. */
23724 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
23728 No program shall explicitly instantiate any template more
23731 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23732 instantiation was `extern'. If EXTERN_P then the second is.
23733 These cases are OK. */
23734 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
23736 if (!previous_instantiation_extern_p
&& !extern_p
23737 && (complain
& tf_error
))
23738 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
23740 /* If we've already instantiated the template, just return now. */
23741 if (!CLASSTYPE_INTERFACE_ONLY (t
))
23745 check_explicit_instantiation_namespace (TYPE_NAME (t
));
23746 mark_class_instantiated (t
, extern_p
);
23751 /* In contrast to implicit instantiation, where only the
23752 declarations, and not the definitions, of members are
23753 instantiated, we have here:
23757 The explicit instantiation of a class template specialization
23758 implies the instantiation of all of its members not
23759 previously explicitly specialized in the translation unit
23760 containing the explicit instantiation.
23762 Of course, we can't instantiate member template classes, since we
23763 don't have any arguments for them. Note that the standard is
23764 unclear on whether the instantiation of the members are
23765 *explicit* instantiations or not. However, the most natural
23766 interpretation is that it should be an explicit
23768 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
23770 || (TREE_CODE (fld
) == FUNCTION_DECL
23772 && user_provided_p (fld
)))
23773 && DECL_TEMPLATE_INSTANTIATION (fld
))
23775 mark_decl_instantiated (fld
, extern_p
);
23777 instantiate_decl (fld
, /*defer_ok=*/true,
23778 /*expl_inst_class_mem_p=*/true);
23781 if (CLASSTYPE_NESTED_UTDS (t
))
23782 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
23783 bt_instantiate_type_proc
, &storage
);
23786 /* Given a function DECL, which is a specialization of TMPL, modify
23787 DECL to be a re-instantiation of TMPL with the same template
23788 arguments. TMPL should be the template into which tsubst'ing
23789 should occur for DECL, not the most general template.
23791 One reason for doing this is a scenario like this:
23794 void f(const T&, int i);
23796 void g() { f(3, 7); }
23799 void f(const T& t, const int i) { }
23801 Note that when the template is first instantiated, with
23802 instantiate_template, the resulting DECL will have no name for the
23803 first parameter, and the wrong type for the second. So, when we go
23804 to instantiate the DECL, we regenerate it. */
23807 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
23809 /* The arguments used to instantiate DECL, from the most general
23813 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
23815 /* Make sure that we can see identifiers, and compute access
23817 push_access_scope (decl
);
23819 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23827 args_depth
= TMPL_ARGS_DEPTH (args
);
23828 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
23829 if (args_depth
> parms_depth
)
23830 args
= get_innermost_template_args (args
, parms_depth
);
23832 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
23833 args
, tf_error
, NULL_TREE
,
23834 /*defer_ok*/false);
23835 if (specs
&& specs
!= error_mark_node
)
23836 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
23839 /* Merge parameter declarations. */
23840 decl_parm
= skip_artificial_parms_for (decl
,
23841 DECL_ARGUMENTS (decl
));
23843 = skip_artificial_parms_for (code_pattern
,
23844 DECL_ARGUMENTS (code_pattern
));
23845 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
23850 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23851 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
23852 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
23854 parm_type
= type_decays_to (parm_type
);
23855 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23856 TREE_TYPE (decl_parm
) = parm_type
;
23857 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23858 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23860 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23861 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23863 decl_parm
= DECL_CHAIN (decl_parm
);
23864 pattern_parm
= DECL_CHAIN (pattern_parm
);
23866 /* Merge any parameters that match with the function parameter
23868 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
23871 tree expanded_types
;
23872 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23873 the parameters in this function parameter pack. */
23874 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
23875 args
, tf_error
, NULL_TREE
);
23876 len
= TREE_VEC_LENGTH (expanded_types
);
23877 for (i
= 0; i
< len
; i
++)
23882 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23883 /* Rename the parameter to include the index. */
23884 DECL_NAME (decl_parm
) =
23885 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
23886 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
23887 parm_type
= type_decays_to (parm_type
);
23888 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23889 TREE_TYPE (decl_parm
) = parm_type
;
23890 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23891 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23893 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23894 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23896 decl_parm
= DECL_CHAIN (decl_parm
);
23899 /* Merge additional specifiers from the CODE_PATTERN. */
23900 if (DECL_DECLARED_INLINE_P (code_pattern
)
23901 && !DECL_DECLARED_INLINE_P (decl
))
23902 DECL_DECLARED_INLINE_P (decl
) = 1;
23904 else if (VAR_P (decl
))
23906 start_lambda_scope (decl
);
23907 DECL_INITIAL (decl
) =
23908 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
23909 tf_error
, DECL_TI_TEMPLATE (decl
),
23910 /*integral_constant_expression_p=*/false);
23911 finish_lambda_scope ();
23912 if (VAR_HAD_UNKNOWN_BOUND (decl
))
23913 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
23914 tf_error
, DECL_TI_TEMPLATE (decl
));
23917 gcc_unreachable ();
23919 pop_access_scope (decl
);
23922 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23923 substituted to get DECL. */
23926 template_for_substitution (tree decl
)
23928 tree tmpl
= DECL_TI_TEMPLATE (decl
);
23930 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23931 for the instantiation. This is not always the most general
23932 template. Consider, for example:
23935 struct S { template <class U> void f();
23936 template <> void f<int>(); };
23938 and an instantiation of S<double>::f<int>. We want TD to be the
23939 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23940 while (/* An instantiation cannot have a definition, so we need a
23941 more general template. */
23942 DECL_TEMPLATE_INSTANTIATION (tmpl
)
23943 /* We must also deal with friend templates. Given:
23945 template <class T> struct S {
23946 template <class U> friend void f() {};
23949 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23950 so far as the language is concerned, but that's still
23951 where we get the pattern for the instantiation from. On
23952 other hand, if the definition comes outside the class, say:
23954 template <class T> struct S {
23955 template <class U> friend void f();
23957 template <class U> friend void f() {}
23959 we don't need to look any further. That's what the check for
23960 DECL_INITIAL is for. */
23961 || (TREE_CODE (decl
) == FUNCTION_DECL
23962 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23963 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23965 /* The present template, TD, should not be a definition. If it
23966 were a definition, we should be using it! Note that we
23967 cannot restructure the loop to just keep going until we find
23968 a template with a definition, since that might go too far if
23969 a specialization was declared, but not defined. */
23971 /* Fetch the more general template. */
23972 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23978 /* Returns true if we need to instantiate this template instance even if we
23979 know we aren't going to emit it. */
23982 always_instantiate_p (tree decl
)
23984 /* We always instantiate inline functions so that we can inline them. An
23985 explicit instantiation declaration prohibits implicit instantiation of
23986 non-inline functions. With high levels of optimization, we would
23987 normally inline non-inline functions -- but we're not allowed to do
23988 that for "extern template" functions. Therefore, we check
23989 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23990 return ((TREE_CODE (decl
) == FUNCTION_DECL
23991 && (DECL_DECLARED_INLINE_P (decl
)
23992 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23993 /* And we need to instantiate static data members so that
23994 their initializers are available in integral constant
23997 && decl_maybe_constant_var_p (decl
)));
24000 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24001 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24002 error, true otherwise. */
24005 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24007 tree fntype
, spec
, noex
, clone
;
24009 /* Don't instantiate a noexcept-specification from template context. */
24010 if (processing_template_decl
24011 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24014 if (DECL_CLONED_FUNCTION_P (fn
))
24015 fn
= DECL_CLONED_FUNCTION (fn
);
24016 fntype
= TREE_TYPE (fn
);
24017 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24019 if (!spec
|| !TREE_PURPOSE (spec
))
24022 noex
= TREE_PURPOSE (spec
);
24024 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24026 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24027 bool added
= false;
24028 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24029 spec
= get_defaulted_eh_spec (fn
, complain
);
24030 else if (!(added
= !fns
->add (fn
)))
24032 /* If hash_set::add returns true, the element was already there. */
24033 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24034 DECL_SOURCE_LOCATION (fn
));
24036 "exception specification of %qD depends on itself",
24038 spec
= noexcept_false_spec
;
24040 else if (push_tinst_level (fn
))
24042 push_access_scope (fn
);
24043 push_deferring_access_checks (dk_no_deferred
);
24044 input_location
= DECL_SOURCE_LOCATION (fn
);
24045 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24046 DEFERRED_NOEXCEPT_ARGS (noex
),
24047 tf_warning_or_error
, fn
,
24048 /*function_p=*/false,
24049 /*integral_constant_expression_p=*/true);
24050 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24051 pop_deferring_access_checks ();
24052 pop_access_scope (fn
);
24053 pop_tinst_level ();
24054 if (spec
== error_mark_node
)
24055 spec
= noexcept_false_spec
;
24058 spec
= noexcept_false_spec
;
24063 if (spec
== error_mark_node
)
24066 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24069 FOR_EACH_CLONE (clone
, fn
)
24071 if (TREE_TYPE (clone
) == fntype
)
24072 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24074 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24080 /* We're starting to process the function INST, an instantiation of PATTERN;
24081 add their parameters to local_specializations. */
24084 register_parameter_specializations (tree pattern
, tree inst
)
24086 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24087 tree spec_parm
= DECL_ARGUMENTS (inst
);
24088 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24090 register_local_specialization (spec_parm
, tmpl_parm
);
24091 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24092 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24094 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24096 if (!DECL_PACK_P (tmpl_parm
))
24098 register_local_specialization (spec_parm
, tmpl_parm
);
24099 spec_parm
= DECL_CHAIN (spec_parm
);
24103 /* Register the (value) argument pack as a specialization of
24104 TMPL_PARM, then move on. */
24105 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24106 register_local_specialization (argpack
, tmpl_parm
);
24109 gcc_assert (!spec_parm
);
24112 /* Produce the definition of D, a _DECL generated from a template. If
24113 DEFER_OK is true, then we don't have to actually do the
24114 instantiation now; we just have to do it sometime. Normally it is
24115 an error if this is an explicit instantiation but D is undefined.
24116 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24117 instantiated class template. */
24120 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24122 tree tmpl
= DECL_TI_TEMPLATE (d
);
24129 bool pattern_defined
;
24130 location_t saved_loc
= input_location
;
24131 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24132 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24136 /* This function should only be used to instantiate templates for
24137 functions and static member variables. */
24138 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
24140 /* A concept is never instantiated. */
24141 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
24143 /* Variables are never deferred; if instantiation is required, they
24144 are instantiated right away. That allows for better code in the
24145 case that an expression refers to the value of the variable --
24146 if the variable has a constant value the referring expression can
24147 take advantage of that fact. */
24151 /* Don't instantiate cloned functions. Instead, instantiate the
24152 functions they cloned. */
24153 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
24154 d
= DECL_CLONED_FUNCTION (d
);
24156 if (DECL_TEMPLATE_INSTANTIATED (d
)
24157 || (TREE_CODE (d
) == FUNCTION_DECL
24158 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
24159 || DECL_TEMPLATE_SPECIALIZATION (d
))
24160 /* D has already been instantiated or explicitly specialized, so
24161 there's nothing for us to do here.
24163 It might seem reasonable to check whether or not D is an explicit
24164 instantiation, and, if so, stop here. But when an explicit
24165 instantiation is deferred until the end of the compilation,
24166 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24167 the instantiation. */
24170 /* Check to see whether we know that this template will be
24171 instantiated in some other file, as with "extern template"
24173 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
24175 /* In general, we do not instantiate such templates. */
24176 if (external_p
&& !always_instantiate_p (d
))
24179 gen_tmpl
= most_general_template (tmpl
);
24180 gen_args
= DECL_TI_ARGS (d
);
24182 if (tmpl
!= gen_tmpl
)
24183 /* We should already have the extra args. */
24184 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
24185 == TMPL_ARGS_DEPTH (gen_args
));
24186 /* And what's in the hash table should match D. */
24187 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
24188 || spec
== NULL_TREE
);
24190 /* This needs to happen before any tsubsting. */
24191 if (! push_tinst_level (d
))
24194 timevar_push (TV_TEMPLATE_INST
);
24196 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24197 for the instantiation. */
24198 td
= template_for_substitution (d
);
24203 /* Look up an explicit specialization, if any. */
24204 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
24205 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
24206 if (elt
&& elt
!= error_mark_node
)
24208 td
= TREE_VALUE (elt
);
24209 args
= TREE_PURPOSE (elt
);
24213 code_pattern
= DECL_TEMPLATE_RESULT (td
);
24215 /* We should never be trying to instantiate a member of a class
24216 template or partial specialization. */
24217 gcc_assert (d
!= code_pattern
);
24219 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
24220 || DECL_TEMPLATE_SPECIALIZATION (td
))
24221 /* In the case of a friend template whose definition is provided
24222 outside the class, we may have too many arguments. Drop the
24223 ones we don't need. The same is true for specializations. */
24224 args
= get_innermost_template_args
24225 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
24227 if (TREE_CODE (d
) == FUNCTION_DECL
)
24229 deleted_p
= DECL_DELETED_FN (code_pattern
);
24230 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
24231 && DECL_INITIAL (code_pattern
) != error_mark_node
)
24232 || DECL_DEFAULTED_FN (code_pattern
)
24238 if (DECL_CLASS_SCOPE_P (code_pattern
))
24239 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
24240 || DECL_INLINE_VAR_P (code_pattern
));
24242 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
24245 /* We may be in the middle of deferred access check. Disable it now. */
24246 push_deferring_access_checks (dk_no_deferred
);
24248 /* Unless an explicit instantiation directive has already determined
24249 the linkage of D, remember that a definition is available for
24251 if (pattern_defined
24252 && !DECL_INTERFACE_KNOWN (d
)
24253 && !DECL_NOT_REALLY_EXTERN (d
))
24254 mark_definable (d
);
24256 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
24257 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
24258 input_location
= DECL_SOURCE_LOCATION (d
);
24260 /* If D is a member of an explicitly instantiated class template,
24261 and no definition is available, treat it like an implicit
24263 if (!pattern_defined
&& expl_inst_class_mem_p
24264 && DECL_EXPLICIT_INSTANTIATION (d
))
24266 /* Leave linkage flags alone on instantiations with anonymous
24268 if (TREE_PUBLIC (d
))
24270 DECL_NOT_REALLY_EXTERN (d
) = 0;
24271 DECL_INTERFACE_KNOWN (d
) = 0;
24273 SET_DECL_IMPLICIT_INSTANTIATION (d
);
24276 /* Defer all other templates, unless we have been explicitly
24277 forbidden from doing so. */
24278 if (/* If there is no definition, we cannot instantiate the
24281 /* If it's OK to postpone instantiation, do so. */
24283 /* If this is a static data member that will be defined
24284 elsewhere, we don't want to instantiate the entire data
24285 member, but we do want to instantiate the initializer so that
24286 we can substitute that elsewhere. */
24287 || (external_p
&& VAR_P (d
))
24288 /* Handle here a deleted function too, avoid generating
24289 its body (c++/61080). */
24292 /* The definition of the static data member is now required so
24293 we must substitute the initializer. */
24295 && !DECL_INITIAL (d
)
24296 && DECL_INITIAL (code_pattern
))
24300 bool const_init
= false;
24301 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24303 ns
= decl_namespace_context (d
);
24304 push_nested_namespace (ns
);
24306 push_nested_class (DECL_CONTEXT (d
));
24307 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
24309 tf_warning_or_error
, NULL_TREE
,
24310 /*integral_constant_expression_p=*/false);
24311 /* If instantiating the initializer involved instantiating this
24312 again, don't call cp_finish_decl twice. */
24313 if (!DECL_INITIAL (d
))
24315 /* Make sure the initializer is still constant, in case of
24316 circular dependency (template/instantiate6.C). */
24318 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24319 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
24320 /*asmspec_tree=*/NULL_TREE
,
24321 LOOKUP_ONLYCONVERTING
);
24324 pop_nested_class ();
24325 pop_nested_namespace (ns
);
24328 /* We restore the source position here because it's used by
24329 add_pending_template. */
24330 input_location
= saved_loc
;
24332 if (at_eof
&& !pattern_defined
24333 && DECL_EXPLICIT_INSTANTIATION (d
)
24334 && DECL_NOT_REALLY_EXTERN (d
))
24337 The definition of a non-exported function template, a
24338 non-exported member function template, or a non-exported
24339 member function or static data member of a class template
24340 shall be present in every translation unit in which it is
24341 explicitly instantiated. */
24342 permerror (input_location
, "explicit instantiation of %qD "
24343 "but no definition available", d
);
24345 /* If we're in unevaluated context, we just wanted to get the
24346 constant value; this isn't an odr use, so don't queue
24347 a full instantiation. */
24348 if (cp_unevaluated_operand
!= 0)
24350 /* ??? Historically, we have instantiated inline functions, even
24351 when marked as "extern template". */
24352 if (!(external_p
&& VAR_P (d
)))
24353 add_pending_template (d
);
24356 /* Tell the repository that D is available in this translation unit
24357 -- and see if it is supposed to be instantiated here. */
24358 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
24360 /* In a PCH file, despite the fact that the repository hasn't
24361 requested instantiation in the PCH it is still possible that
24362 an instantiation will be required in a file that includes the
24365 add_pending_template (d
);
24366 /* Instantiate inline functions so that the inliner can do its
24367 job, even though we'll not be emitting a copy of this
24369 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
24373 bool push_to_top
, nested
;
24375 fn_context
= decl_function_context (d
);
24376 if (LAMBDA_FUNCTION_P (d
))
24377 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24378 fn_context
= NULL_TREE
;
24379 nested
= current_function_decl
!= NULL_TREE
;
24380 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
24382 vec
<tree
> omp_privatization_save
;
24384 save_omp_privatization_clauses (omp_privatization_save
);
24387 push_to_top_level ();
24390 gcc_assert (!processing_template_decl
);
24391 push_function_context ();
24392 cp_unevaluated_operand
= 0;
24393 c_inhibit_evaluation_warnings
= 0;
24396 /* Mark D as instantiated so that recursive calls to
24397 instantiate_decl do not try to instantiate it again. */
24398 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24400 /* Regenerate the declaration in case the template has been modified
24401 by a subsequent redeclaration. */
24402 regenerate_decl_from_template (d
, td
, args
);
24404 /* We already set the file and line above. Reset them now in case
24405 they changed as a result of calling regenerate_decl_from_template. */
24406 input_location
= DECL_SOURCE_LOCATION (d
);
24411 bool const_init
= false;
24413 /* Clear out DECL_RTL; whatever was there before may not be right
24414 since we've reset the type of the declaration. */
24415 SET_DECL_RTL (d
, NULL
);
24416 DECL_IN_AGGR_P (d
) = 0;
24418 /* The initializer is placed in DECL_INITIAL by
24419 regenerate_decl_from_template so we don't need to
24420 push/pop_access_scope again here. Pull it out so that
24421 cp_finish_decl can process it. */
24422 init
= DECL_INITIAL (d
);
24423 DECL_INITIAL (d
) = NULL_TREE
;
24424 DECL_INITIALIZED_P (d
) = 0;
24426 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24427 initializer. That function will defer actual emission until
24428 we have a chance to determine linkage. */
24429 DECL_EXTERNAL (d
) = 0;
24431 /* Enter the scope of D so that access-checking works correctly. */
24432 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24434 push_nested_class (DECL_CONTEXT (d
));
24436 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24437 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
24440 pop_nested_class ();
24442 if (variable_template_p (gen_tmpl
))
24443 note_variable_template_instantiation (d
);
24445 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24446 synthesize_method (d
);
24447 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24449 /* Set up the list of local specializations. */
24450 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24451 tree block
= NULL_TREE
;
24453 /* Set up context. */
24454 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24455 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24456 block
= push_stmt_list ();
24458 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24460 /* Some typedefs referenced from within the template code need to be
24461 access checked at template instantiation time, i.e now. These
24462 types were added to the template at parsing time. Let's get those
24463 and perform the access checks then. */
24464 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24467 /* Create substitution entries for the parameters. */
24468 register_parameter_specializations (code_pattern
, d
);
24470 /* Substitute into the body of the function. */
24471 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24472 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24473 tf_warning_or_error
, tmpl
);
24476 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24477 tf_warning_or_error
, tmpl
,
24478 /*integral_constant_expression_p=*/false);
24480 /* Set the current input_location to the end of the function
24481 so that finish_function knows where we are. */
24483 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24485 /* Remember if we saw an infinite loop in the template. */
24486 current_function_infinite_loop
24487 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24490 /* Finish the function. */
24491 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24492 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24493 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24496 d
= finish_function (/*inline_p=*/false);
24497 expand_or_defer_fn (d
);
24500 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24501 cp_check_omp_declare_reduction (d
);
24504 /* We're not deferring instantiation any more. */
24505 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24508 pop_from_top_level ();
24510 pop_function_context ();
24513 restore_omp_privatization_clauses (omp_privatization_save
);
24516 pop_deferring_access_checks ();
24517 timevar_pop (TV_TEMPLATE_INST
);
24518 pop_tinst_level ();
24519 input_location
= saved_loc
;
24520 cp_unevaluated_operand
= saved_unevaluated_operand
;
24521 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24526 /* Run through the list of templates that we wish we could
24527 instantiate, and instantiate any we can. RETRIES is the
24528 number of times we retry pending template instantiation. */
24531 instantiate_pending_templates (int retries
)
24534 location_t saved_loc
= input_location
;
24536 /* Instantiating templates may trigger vtable generation. This in turn
24537 may require further template instantiations. We place a limit here
24538 to avoid infinite loop. */
24539 if (pending_templates
&& retries
>= max_tinst_depth
)
24541 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24543 fatal_error (input_location
,
24544 "template instantiation depth exceeds maximum of %d"
24545 " instantiating %q+D, possibly from virtual table generation"
24546 " (use -ftemplate-depth= to increase the maximum)",
24547 max_tinst_depth
, decl
);
24548 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24549 /* Pretend that we defined it. */
24550 DECL_INITIAL (decl
) = error_mark_node
;
24556 struct pending_template
**t
= &pending_templates
;
24557 struct pending_template
*last
= NULL
;
24561 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24562 bool complete
= false;
24564 if (TYPE_P (instantiation
))
24566 if (!COMPLETE_TYPE_P (instantiation
))
24568 instantiate_class_template (instantiation
);
24569 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24570 for (tree fld
= TYPE_FIELDS (instantiation
);
24571 fld
; fld
= TREE_CHAIN (fld
))
24573 || (TREE_CODE (fld
) == FUNCTION_DECL
24574 && !DECL_ARTIFICIAL (fld
)))
24575 && DECL_TEMPLATE_INSTANTIATION (fld
))
24576 instantiate_decl (fld
,
24577 /*defer_ok=*/false,
24578 /*expl_inst_class_mem_p=*/false);
24580 if (COMPLETE_TYPE_P (instantiation
))
24584 complete
= COMPLETE_TYPE_P (instantiation
);
24588 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24589 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24592 = instantiate_decl (instantiation
,
24593 /*defer_ok=*/false,
24594 /*expl_inst_class_mem_p=*/false);
24595 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24599 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24600 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24605 /* If INSTANTIATION has been instantiated, then we don't
24606 need to consider it again in the future. */
24607 struct pending_template
*drop
= *t
;
24609 set_refcount_ptr (drop
->tinst
);
24610 pending_template_freelist ().free (drop
);
24618 set_refcount_ptr (current_tinst_level
);
24620 last_pending_template
= last
;
24622 while (reconsider
);
24624 input_location
= saved_loc
;
24627 /* Substitute ARGVEC into T, which is a list of initializers for
24628 either base class or a non-static data member. The TREE_PURPOSEs
24629 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24630 instantiate_decl. */
24633 tsubst_initializer_list (tree t
, tree argvec
)
24635 tree inits
= NULL_TREE
;
24636 tree target_ctor
= error_mark_node
;
24638 for (; t
; t
= TREE_CHAIN (t
))
24642 tree expanded_bases
= NULL_TREE
;
24643 tree expanded_arguments
= NULL_TREE
;
24646 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24651 /* Expand the base class expansion type into separate base
24653 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24654 tf_warning_or_error
,
24656 if (expanded_bases
== error_mark_node
)
24659 /* We'll be building separate TREE_LISTs of arguments for
24661 len
= TREE_VEC_LENGTH (expanded_bases
);
24662 expanded_arguments
= make_tree_vec (len
);
24663 for (i
= 0; i
< len
; i
++)
24664 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24666 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24667 expand each argument in the TREE_VALUE of t. */
24668 expr
= make_node (EXPR_PACK_EXPANSION
);
24669 PACK_EXPANSION_LOCAL_P (expr
) = true;
24670 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24671 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24673 if (TREE_VALUE (t
) == void_type_node
)
24674 /* VOID_TYPE_NODE is used to indicate
24675 value-initialization. */
24677 for (i
= 0; i
< len
; i
++)
24678 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24682 /* Substitute parameter packs into each argument in the
24684 in_base_initializer
= 1;
24685 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
24687 tree expanded_exprs
;
24689 /* Expand the argument. */
24690 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
24692 = tsubst_pack_expansion (expr
, argvec
,
24693 tf_warning_or_error
,
24695 if (expanded_exprs
== error_mark_node
)
24698 /* Prepend each of the expanded expressions to the
24699 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24700 for (i
= 0; i
< len
; i
++)
24702 TREE_VEC_ELT (expanded_arguments
, i
) =
24703 tree_cons (NULL_TREE
,
24704 TREE_VEC_ELT (expanded_exprs
, i
),
24705 TREE_VEC_ELT (expanded_arguments
, i
));
24708 in_base_initializer
= 0;
24710 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24711 since we built them backwards. */
24712 for (i
= 0; i
< len
; i
++)
24714 TREE_VEC_ELT (expanded_arguments
, i
) =
24715 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
24720 for (i
= 0; i
< len
; ++i
)
24722 if (expanded_bases
)
24724 decl
= TREE_VEC_ELT (expanded_bases
, i
);
24725 decl
= expand_member_init (decl
);
24726 init
= TREE_VEC_ELT (expanded_arguments
, i
);
24731 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
24732 tf_warning_or_error
, NULL_TREE
);
24734 decl
= expand_member_init (decl
);
24735 if (decl
&& !DECL_P (decl
))
24736 in_base_initializer
= 1;
24738 init
= TREE_VALUE (t
);
24740 if (init
!= void_type_node
)
24741 init
= tsubst_expr (init
, argvec
,
24742 tf_warning_or_error
, NULL_TREE
,
24743 /*integral_constant_expression_p=*/false);
24744 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
24745 /* If we had an initializer but it instantiated to nothing,
24746 value-initialize the object. This will only occur when
24747 the initializer was a pack expansion where the parameter
24748 packs used in that expansion were of length zero. */
24749 init
= void_type_node
;
24750 in_base_initializer
= 0;
24753 if (target_ctor
!= error_mark_node
24754 && init
!= error_mark_node
)
24756 error ("mem-initializer for %qD follows constructor delegation",
24760 /* Look for a target constructor. */
24761 if (init
!= error_mark_node
24762 && decl
&& CLASS_TYPE_P (decl
)
24763 && same_type_p (decl
, current_class_type
))
24765 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
24768 error ("constructor delegation follows mem-initializer for %qD",
24769 TREE_PURPOSE (inits
));
24772 target_ctor
= init
;
24777 init
= build_tree_list (decl
, init
);
24778 TREE_CHAIN (init
) = inits
;
24786 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24789 set_current_access_from_decl (tree decl
)
24791 if (TREE_PRIVATE (decl
))
24792 current_access_specifier
= access_private_node
;
24793 else if (TREE_PROTECTED (decl
))
24794 current_access_specifier
= access_protected_node
;
24796 current_access_specifier
= access_public_node
;
24799 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24800 is the instantiation (which should have been created with
24801 start_enum) and ARGS are the template arguments to use. */
24804 tsubst_enum (tree tag
, tree newtag
, tree args
)
24808 if (SCOPED_ENUM_P (newtag
))
24809 begin_scope (sk_scoped_enum
, newtag
);
24811 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
24816 decl
= TREE_VALUE (e
);
24817 /* Note that in a template enum, the TREE_VALUE is the
24818 CONST_DECL, not the corresponding INTEGER_CST. */
24819 value
= tsubst_expr (DECL_INITIAL (decl
),
24820 args
, tf_warning_or_error
, NULL_TREE
,
24821 /*integral_constant_expression_p=*/true);
24823 /* Give this enumeration constant the correct access. */
24824 set_current_access_from_decl (decl
);
24826 /* Actually build the enumerator itself. Here we're assuming that
24827 enumerators can't have dependent attributes. */
24828 build_enumerator (DECL_NAME (decl
), value
, newtag
,
24829 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
24832 if (SCOPED_ENUM_P (newtag
))
24835 finish_enum_value_list (newtag
);
24836 finish_enum (newtag
);
24838 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
24839 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
24842 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24843 its type -- but without substituting the innermost set of template
24844 arguments. So, innermost set of template parameters will appear in
24848 get_mostly_instantiated_function_type (tree decl
)
24850 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24851 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
24854 /* Return truthvalue if we're processing a template different from
24855 the last one involved in diagnostics. */
24857 problematic_instantiation_changed (void)
24859 return current_tinst_level
!= last_error_tinst_level
;
24862 /* Remember current template involved in diagnostics. */
24864 record_last_problematic_instantiation (void)
24866 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
24869 struct tinst_level
*
24870 current_instantiation (void)
24872 return current_tinst_level
;
24875 /* Return TRUE if current_function_decl is being instantiated, false
24879 instantiating_current_function_p (void)
24881 return (current_instantiation ()
24882 && (current_instantiation ()->maybe_get_node ()
24883 == current_function_decl
));
24886 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24887 type. Return false for ok, true for disallowed. Issue error and
24888 inform messages under control of COMPLAIN. */
24891 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
24893 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
24895 else if (TYPE_PTR_P (type
))
24897 else if (TYPE_REF_P (type
)
24898 && !TYPE_REF_IS_RVALUE (type
))
24900 else if (TYPE_PTRMEM_P (type
))
24902 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
24904 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
24906 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
24908 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
24910 /* A bound template template parm could later be instantiated to have a valid
24911 nontype parm type via an alias template. */
24912 else if (cxx_dialect
>= cxx11
24913 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24915 else if (CLASS_TYPE_P (type
))
24917 if (cxx_dialect
< cxx2a
)
24919 error ("non-type template parameters of class type only available "
24920 "with -std=c++2a or -std=gnu++2a");
24923 if (!complete_type_or_else (type
, NULL_TREE
))
24925 if (!literal_type_p (type
))
24927 error ("%qT is not a valid type for a template non-type parameter "
24928 "because it is not literal", type
);
24929 explain_non_literal_class (type
);
24932 if (cp_has_mutable_p (type
))
24934 error ("%qT is not a valid type for a template non-type parameter "
24935 "because it has a mutable member", type
);
24938 /* FIXME check op<=> and strong structural equality once spaceship is
24943 if (complain
& tf_error
)
24945 if (type
== error_mark_node
)
24946 inform (input_location
, "invalid template non-type parameter");
24948 error ("%q#T is not a valid type for a template non-type parameter",
24954 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24955 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24958 dependent_type_p_r (tree type
)
24964 A type is dependent if it is:
24966 -- a template parameter. Template template parameters are types
24967 for us (since TYPE_P holds true for them) so we handle
24969 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
24970 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
24972 /* -- a qualified-id with a nested-name-specifier which contains a
24973 class-name that names a dependent type or whose unqualified-id
24974 names a dependent type. */
24975 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24978 /* An alias template specialization can be dependent even if the
24979 resulting type is not. */
24980 if (dependent_alias_template_spec_p (type
))
24983 /* -- a cv-qualified type where the cv-unqualified type is
24985 No code is necessary for this bullet; the code below handles
24986 cv-qualified types, and we don't want to strip aliases with
24987 TYPE_MAIN_VARIANT because of DR 1558. */
24988 /* -- a compound type constructed from any dependent type. */
24989 if (TYPE_PTRMEM_P (type
))
24990 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24991 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24993 else if (INDIRECT_TYPE_P (type
))
24994 return dependent_type_p (TREE_TYPE (type
));
24995 else if (TREE_CODE (type
) == FUNCTION_TYPE
24996 || TREE_CODE (type
) == METHOD_TYPE
)
25000 if (dependent_type_p (TREE_TYPE (type
)))
25002 for (arg_type
= TYPE_ARG_TYPES (type
);
25004 arg_type
= TREE_CHAIN (arg_type
))
25005 if (dependent_type_p (TREE_VALUE (arg_type
)))
25007 if (cxx_dialect
>= cxx17
)
25008 /* A value-dependent noexcept-specifier makes the type dependent. */
25009 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25010 if (tree noex
= TREE_PURPOSE (spec
))
25011 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25012 affect overload resolution and treating it as dependent breaks
25014 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25015 && value_dependent_expression_p (noex
))
25019 /* -- an array type constructed from any dependent type or whose
25020 size is specified by a constant expression that is
25023 We checked for type- and value-dependence of the bounds in
25024 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25025 if (TREE_CODE (type
) == ARRAY_TYPE
)
25027 if (TYPE_DOMAIN (type
)
25028 && dependent_type_p (TYPE_DOMAIN (type
)))
25030 return dependent_type_p (TREE_TYPE (type
));
25033 /* -- a template-id in which either the template name is a template
25035 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25037 /* ... or any of the template arguments is a dependent type or
25038 an expression that is type-dependent or value-dependent. */
25039 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25040 && (any_dependent_template_arguments_p
25041 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25044 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25045 dependent; if the argument of the `typeof' expression is not
25046 type-dependent, then it should already been have resolved. */
25047 if (TREE_CODE (type
) == TYPEOF_TYPE
25048 || TREE_CODE (type
) == DECLTYPE_TYPE
25049 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25052 /* A template argument pack is dependent if any of its packed
25054 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25056 tree args
= ARGUMENT_PACK_ARGS (type
);
25057 int i
, len
= TREE_VEC_LENGTH (args
);
25058 for (i
= 0; i
< len
; ++i
)
25059 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25063 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25064 be template parameters. */
25065 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25068 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25071 /* The standard does not specifically mention types that are local
25072 to template functions or local classes, but they should be
25073 considered dependent too. For example:
25075 template <int I> void f() {
25080 The size of `E' cannot be known until the value of `I' has been
25081 determined. Therefore, `E' must be considered dependent. */
25082 scope
= TYPE_CONTEXT (type
);
25083 if (scope
&& TYPE_P (scope
))
25084 return dependent_type_p (scope
);
25085 /* Don't use type_dependent_expression_p here, as it can lead
25086 to infinite recursion trying to determine whether a lambda
25087 nested in a lambda is dependent (c++/47687). */
25088 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25089 && DECL_LANG_SPECIFIC (scope
)
25090 && DECL_TEMPLATE_INFO (scope
)
25091 && (any_dependent_template_arguments_p
25092 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25095 /* Other types are non-dependent. */
25099 /* Returns TRUE if TYPE is dependent, in the sense of
25100 [temp.dep.type]. Note that a NULL type is considered dependent. */
25103 dependent_type_p (tree type
)
25105 /* If there are no template parameters in scope, then there can't be
25106 any dependent types. */
25107 if (!processing_template_decl
)
25109 /* If we are not processing a template, then nobody should be
25110 providing us with a dependent type. */
25112 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25116 /* If the type is NULL, we have not computed a type for the entity
25117 in question; in that case, the type is dependent. */
25121 /* Erroneous types can be considered non-dependent. */
25122 if (type
== error_mark_node
)
25125 /* Getting here with global_type_node means we improperly called this
25126 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25127 gcc_checking_assert (type
!= global_type_node
);
25129 /* If we have not already computed the appropriate value for TYPE,
25131 if (!TYPE_DEPENDENT_P_VALID (type
))
25133 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25134 TYPE_DEPENDENT_P_VALID (type
) = 1;
25137 return TYPE_DEPENDENT_P (type
);
25140 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25141 lookup. In other words, a dependent type that is not the current
25145 dependent_scope_p (tree scope
)
25147 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25148 && !currently_open_class (scope
));
25151 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25152 an unknown base of 'this' (and is therefore instantiation-dependent). */
25155 unknown_base_ref_p (tree t
)
25157 if (!current_class_ptr
)
25160 tree mem
= TREE_OPERAND (t
, 1);
25161 if (shared_member_p (mem
))
25164 tree cur
= current_nonlambda_class_type ();
25165 if (!any_dependent_bases_p (cur
))
25168 tree ctx
= TREE_OPERAND (t
, 0);
25169 if (DERIVED_FROM_P (ctx
, cur
))
25175 /* T is a SCOPE_REF; return whether we need to consider it
25176 instantiation-dependent so that we can check access at instantiation
25177 time even though we know which member it resolves to. */
25180 instantiation_dependent_scope_ref_p (tree t
)
25182 if (DECL_P (TREE_OPERAND (t
, 1))
25183 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
25184 && !unknown_base_ref_p (t
)
25185 && accessible_in_template_p (TREE_OPERAND (t
, 0),
25186 TREE_OPERAND (t
, 1)))
25192 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25193 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25196 /* Note that this predicate is not appropriate for general expressions;
25197 only constant expressions (that satisfy potential_constant_expression)
25198 can be tested for value dependence. */
25201 value_dependent_expression_p (tree expression
)
25203 if (!processing_template_decl
|| expression
== NULL_TREE
)
25206 /* A type-dependent expression is also value-dependent. */
25207 if (type_dependent_expression_p (expression
))
25210 switch (TREE_CODE (expression
))
25213 /* A dependent member function of the current instantiation. */
25214 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
25216 case FUNCTION_DECL
:
25217 /* A dependent member function of the current instantiation. */
25218 if (DECL_CLASS_SCOPE_P (expression
)
25219 && dependent_type_p (DECL_CONTEXT (expression
)))
25223 case IDENTIFIER_NODE
:
25224 /* A name that has not been looked up -- must be dependent. */
25227 case TEMPLATE_PARM_INDEX
:
25228 /* A non-type template parm. */
25232 /* A non-type template parm. */
25233 if (DECL_TEMPLATE_PARM_P (expression
))
25235 return value_dependent_expression_p (DECL_INITIAL (expression
));
25238 /* A constant with literal type and is initialized
25239 with an expression that is value-dependent. */
25240 if (DECL_DEPENDENT_INIT_P (expression
)
25241 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25242 || TYPE_REF_P (TREE_TYPE (expression
)))
25244 if (DECL_HAS_VALUE_EXPR_P (expression
))
25246 tree value_expr
= DECL_VALUE_EXPR (expression
);
25247 if (value_dependent_expression_p (value_expr
))
25252 case DYNAMIC_CAST_EXPR
:
25253 case STATIC_CAST_EXPR
:
25254 case CONST_CAST_EXPR
:
25255 case REINTERPRET_CAST_EXPR
:
25257 case IMPLICIT_CONV_EXPR
:
25258 /* These expressions are value-dependent if the type to which
25259 the cast occurs is dependent or the expression being casted
25260 is value-dependent. */
25262 tree type
= TREE_TYPE (expression
);
25264 if (dependent_type_p (type
))
25267 /* A functional cast has a list of operands. */
25268 expression
= TREE_OPERAND (expression
, 0);
25271 /* If there are no operands, it must be an expression such
25272 as "int()". This should not happen for aggregate types
25273 because it would form non-constant expressions. */
25274 gcc_assert (cxx_dialect
>= cxx11
25275 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
25280 if (TREE_CODE (expression
) == TREE_LIST
)
25281 return any_value_dependent_elements_p (expression
);
25283 return value_dependent_expression_p (expression
);
25287 if (SIZEOF_EXPR_TYPE_P (expression
))
25288 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
25292 /* A `sizeof' expression is value-dependent if the operand is
25293 type-dependent or is a pack expansion. */
25294 expression
= TREE_OPERAND (expression
, 0);
25295 if (PACK_EXPANSION_P (expression
))
25297 else if (TYPE_P (expression
))
25298 return dependent_type_p (expression
);
25299 return instantiation_dependent_uneval_expression_p (expression
);
25301 case AT_ENCODE_EXPR
:
25302 /* An 'encode' expression is value-dependent if the operand is
25304 expression
= TREE_OPERAND (expression
, 0);
25305 return dependent_type_p (expression
);
25307 case NOEXCEPT_EXPR
:
25308 expression
= TREE_OPERAND (expression
, 0);
25309 return instantiation_dependent_uneval_expression_p (expression
);
25312 /* All instantiation-dependent expressions should also be considered
25313 value-dependent. */
25314 return instantiation_dependent_scope_ref_p (expression
);
25316 case COMPONENT_REF
:
25317 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
25318 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
25320 case NONTYPE_ARGUMENT_PACK
:
25321 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25322 is value-dependent. */
25324 tree values
= ARGUMENT_PACK_ARGS (expression
);
25325 int i
, len
= TREE_VEC_LENGTH (values
);
25327 for (i
= 0; i
< len
; ++i
)
25328 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
25336 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
25338 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
25344 if (TREE_CODE (type2
) != TREE_LIST
)
25345 return dependent_type_p (type2
);
25347 for (; type2
; type2
= TREE_CHAIN (type2
))
25348 if (dependent_type_p (TREE_VALUE (type2
)))
25355 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25356 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
25359 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25360 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
25364 tree op
= TREE_OPERAND (expression
, 0);
25365 return (value_dependent_expression_p (op
)
25366 || has_value_dependent_address (op
));
25369 case REQUIRES_EXPR
:
25370 /* Treat all requires-expressions as value-dependent so
25371 we don't try to fold them. */
25375 return dependent_type_p (TREE_OPERAND (expression
, 0));
25379 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
25381 tree fn
= get_callee_fndecl (expression
);
25383 nargs
= call_expr_nargs (expression
);
25384 for (i
= 0; i
< nargs
; ++i
)
25386 tree op
= CALL_EXPR_ARG (expression
, i
);
25387 /* In a call to a constexpr member function, look through the
25388 implicit ADDR_EXPR on the object argument so that it doesn't
25389 cause the call to be considered value-dependent. We also
25390 look through it in potential_constant_expression. */
25391 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
25392 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
25393 && TREE_CODE (op
) == ADDR_EXPR
)
25394 op
= TREE_OPERAND (op
, 0);
25395 if (value_dependent_expression_p (op
))
25401 case TEMPLATE_ID_EXPR
:
25402 return variable_concept_p (TREE_OPERAND (expression
, 0));
25408 if (dependent_type_p (TREE_TYPE (expression
)))
25410 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
25411 if (value_dependent_expression_p (val
))
25417 /* Treat a GNU statement expression as dependent to avoid crashing
25418 under instantiate_non_dependent_expr; it can't be constant. */
25422 /* A constant expression is value-dependent if any subexpression is
25423 value-dependent. */
25424 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25426 case tcc_reference
:
25428 case tcc_comparison
:
25430 case tcc_expression
:
25433 int i
, len
= cp_tree_operand_length (expression
);
25435 for (i
= 0; i
< len
; i
++)
25437 tree t
= TREE_OPERAND (expression
, i
);
25439 /* In some cases, some of the operands may be missing.
25440 (For example, in the case of PREDECREMENT_EXPR, the
25441 amount to increment by may be missing.) That doesn't
25442 make the expression dependent. */
25443 if (t
&& value_dependent_expression_p (t
))
25454 /* The expression is not value-dependent. */
25458 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25459 [temp.dep.expr]. Note that an expression with no type is
25460 considered dependent. Other parts of the compiler arrange for an
25461 expression with type-dependent subexpressions to have no type, so
25462 this function doesn't have to be fully recursive. */
25465 type_dependent_expression_p (tree expression
)
25467 if (!processing_template_decl
)
25470 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25473 STRIP_ANY_LOCATION_WRAPPER (expression
);
25475 /* An unresolved name is always dependent. */
25476 if (identifier_p (expression
)
25477 || TREE_CODE (expression
) == USING_DECL
25478 || TREE_CODE (expression
) == WILDCARD_DECL
)
25481 /* A fold expression is type-dependent. */
25482 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25483 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25484 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25485 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25488 /* Some expression forms are never type-dependent. */
25489 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25490 || TREE_CODE (expression
) == SIZEOF_EXPR
25491 || TREE_CODE (expression
) == ALIGNOF_EXPR
25492 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25493 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25494 || TREE_CODE (expression
) == TRAIT_EXPR
25495 || TREE_CODE (expression
) == TYPEID_EXPR
25496 || TREE_CODE (expression
) == DELETE_EXPR
25497 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25498 || TREE_CODE (expression
) == THROW_EXPR
25499 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25502 /* The types of these expressions depends only on the type to which
25503 the cast occurs. */
25504 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25505 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25506 || TREE_CODE (expression
) == CONST_CAST_EXPR
25507 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25508 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25509 || TREE_CODE (expression
) == CAST_EXPR
)
25510 return dependent_type_p (TREE_TYPE (expression
));
25512 /* The types of these expressions depends only on the type created
25513 by the expression. */
25514 if (TREE_CODE (expression
) == NEW_EXPR
25515 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25517 /* For NEW_EXPR tree nodes created inside a template, either
25518 the object type itself or a TREE_LIST may appear as the
25520 tree type
= TREE_OPERAND (expression
, 1);
25521 if (TREE_CODE (type
) == TREE_LIST
)
25522 /* This is an array type. We need to check array dimensions
25524 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25525 || value_dependent_expression_p
25526 (TREE_OPERAND (TREE_VALUE (type
), 1));
25528 return dependent_type_p (type
);
25531 if (TREE_CODE (expression
) == SCOPE_REF
)
25533 tree scope
= TREE_OPERAND (expression
, 0);
25534 tree name
= TREE_OPERAND (expression
, 1);
25536 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25537 contains an identifier associated by name lookup with one or more
25538 declarations declared with a dependent type, or...a
25539 nested-name-specifier or qualified-id that names a member of an
25540 unknown specialization. */
25541 return (type_dependent_expression_p (name
)
25542 || dependent_scope_p (scope
));
25545 if (TREE_CODE (expression
) == TEMPLATE_DECL
25546 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25547 return uses_outer_template_parms (expression
);
25549 if (TREE_CODE (expression
) == STMT_EXPR
)
25550 expression
= stmt_expr_value_expr (expression
);
25552 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25557 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25559 if (type_dependent_expression_p (elt
))
25565 /* A static data member of the current instantiation with incomplete
25566 array type is type-dependent, as the definition and specializations
25567 can have different bounds. */
25568 if (VAR_P (expression
)
25569 && DECL_CLASS_SCOPE_P (expression
)
25570 && dependent_type_p (DECL_CONTEXT (expression
))
25571 && VAR_HAD_UNKNOWN_BOUND (expression
))
25574 /* An array of unknown bound depending on a variadic parameter, eg:
25576 template<typename... Args>
25577 void foo (Args... args)
25579 int arr[] = { args... };
25582 template<int... vals>
25585 int arr[] = { vals... };
25588 If the array has no length and has an initializer, it must be that
25589 we couldn't determine its length in cp_complete_array_type because
25590 it is dependent. */
25591 if (VAR_P (expression
)
25592 && TREE_TYPE (expression
) != NULL_TREE
25593 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25594 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25595 && DECL_INITIAL (expression
))
25598 /* A function or variable template-id is type-dependent if it has any
25599 dependent template arguments. */
25600 if (VAR_OR_FUNCTION_DECL_P (expression
)
25601 && DECL_LANG_SPECIFIC (expression
)
25602 && DECL_TEMPLATE_INFO (expression
))
25604 /* Consider the innermost template arguments, since those are the ones
25605 that come from the template-id; the template arguments for the
25606 enclosing class do not make it type-dependent unless they are used in
25607 the type of the decl. */
25608 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
25609 && (any_dependent_template_arguments_p
25610 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25614 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25615 type-dependent. Checking this is important for functions with auto return
25616 type, which looks like a dependent type. */
25617 if (TREE_CODE (expression
) == FUNCTION_DECL
25618 && !(DECL_CLASS_SCOPE_P (expression
)
25619 && dependent_type_p (DECL_CONTEXT (expression
)))
25620 && !(DECL_LANG_SPECIFIC (expression
)
25621 && DECL_FRIEND_P (expression
)
25622 && (!DECL_FRIEND_CONTEXT (expression
)
25623 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25624 && !DECL_LOCAL_FUNCTION_P (expression
))
25626 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25627 || undeduced_auto_decl (expression
));
25631 /* Always dependent, on the number of arguments if nothing else. */
25632 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25635 if (TREE_TYPE (expression
) == unknown_type_node
)
25637 if (TREE_CODE (expression
) == ADDR_EXPR
)
25638 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25639 if (TREE_CODE (expression
) == COMPONENT_REF
25640 || TREE_CODE (expression
) == OFFSET_REF
)
25642 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25644 expression
= TREE_OPERAND (expression
, 1);
25645 if (identifier_p (expression
))
25648 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25649 if (TREE_CODE (expression
) == SCOPE_REF
)
25652 if (BASELINK_P (expression
))
25654 if (BASELINK_OPTYPE (expression
)
25655 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25657 expression
= BASELINK_FUNCTIONS (expression
);
25660 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25662 if (any_dependent_template_arguments_p
25663 (TREE_OPERAND (expression
, 1)))
25665 expression
= TREE_OPERAND (expression
, 0);
25666 if (identifier_p (expression
))
25670 gcc_assert (TREE_CODE (expression
) == OVERLOAD
25671 || TREE_CODE (expression
) == FUNCTION_DECL
);
25673 for (lkp_iterator
iter (expression
); iter
; ++iter
)
25674 if (type_dependent_expression_p (*iter
))
25680 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
25682 /* Dependent type attributes might not have made it from the decl to
25684 if (DECL_P (expression
)
25685 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
25688 return (dependent_type_p (TREE_TYPE (expression
)));
25691 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25692 type-dependent if the expression refers to a member of the current
25693 instantiation and the type of the referenced member is dependent, or the
25694 class member access expression refers to a member of an unknown
25697 This function returns true if the OBJECT in such a class member access
25698 expression is of an unknown specialization. */
25701 type_dependent_object_expression_p (tree object
)
25703 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25705 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
25707 tree scope
= TREE_TYPE (object
);
25708 return (!scope
|| dependent_scope_p (scope
));
25711 /* walk_tree callback function for instantiation_dependent_expression_p,
25712 below. Returns non-zero if a dependent subexpression is found. */
25715 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
25720 /* We don't have to worry about decltype currently because decltype
25721 of an instantiation-dependent expr is a dependent type. This
25722 might change depending on the resolution of DR 1172. */
25723 *walk_subtrees
= false;
25726 enum tree_code code
= TREE_CODE (*tp
);
25729 /* Don't treat an argument list as dependent just because it has no
25733 case NONTYPE_ARGUMENT_PACK
:
25736 case TEMPLATE_PARM_INDEX
:
25737 if (dependent_type_p (TREE_TYPE (*tp
)))
25739 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
25741 /* We'll check value-dependence separately. */
25744 /* Handle expressions with type operands. */
25748 case AT_ENCODE_EXPR
:
25750 tree op
= TREE_OPERAND (*tp
, 0);
25751 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
25752 op
= TREE_TYPE (op
);
25755 if (dependent_type_p (op
))
25759 *walk_subtrees
= false;
25766 case COMPONENT_REF
:
25767 if (identifier_p (TREE_OPERAND (*tp
, 1)))
25768 /* In a template, finish_class_member_access_expr creates a
25769 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25770 type-dependent, so that we can check access control at
25771 instantiation time (PR 42277). See also Core issue 1273. */
25776 if (instantiation_dependent_scope_ref_p (*tp
))
25781 /* Treat statement-expressions as dependent. */
25785 /* Treat requires-expressions as dependent. */
25786 case REQUIRES_EXPR
:
25790 /* Treat calls to function concepts as dependent. */
25791 if (function_concept_check_p (*tp
))
25795 case TEMPLATE_ID_EXPR
:
25796 /* And variable concepts. */
25797 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
25805 if (type_dependent_expression_p (*tp
))
25811 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25812 sense defined by the ABI:
25814 "An expression is instantiation-dependent if it is type-dependent
25815 or value-dependent, or it has a subexpression that is type-dependent
25816 or value-dependent."
25818 Except don't actually check value-dependence for unevaluated expressions,
25819 because in sizeof(i) we don't care about the value of i. Checking
25820 type-dependence will in turn check value-dependence of array bounds/template
25821 arguments as needed. */
25824 instantiation_dependent_uneval_expression_p (tree expression
)
25828 if (!processing_template_decl
)
25831 if (expression
== error_mark_node
)
25834 result
= cp_walk_tree_without_duplicates (&expression
,
25835 instantiation_dependent_r
, NULL
);
25836 return result
!= NULL_TREE
;
25839 /* As above, but also check value-dependence of the expression as a whole. */
25842 instantiation_dependent_expression_p (tree expression
)
25844 return (instantiation_dependent_uneval_expression_p (expression
)
25845 || value_dependent_expression_p (expression
));
25848 /* Like type_dependent_expression_p, but it also works while not processing
25849 a template definition, i.e. during substitution or mangling. */
25852 type_dependent_expression_p_push (tree expr
)
25855 ++processing_template_decl
;
25856 b
= type_dependent_expression_p (expr
);
25857 --processing_template_decl
;
25861 /* Returns TRUE if ARGS contains a type-dependent expression. */
25864 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
25869 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
25871 if (type_dependent_expression_p (arg
))
25877 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25878 expressions) contains any type-dependent expressions. */
25881 any_type_dependent_elements_p (const_tree list
)
25883 for (; list
; list
= TREE_CHAIN (list
))
25884 if (type_dependent_expression_p (TREE_VALUE (list
)))
25890 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25891 expressions) contains any value-dependent expressions. */
25894 any_value_dependent_elements_p (const_tree list
)
25896 for (; list
; list
= TREE_CHAIN (list
))
25897 if (value_dependent_expression_p (TREE_VALUE (list
)))
25903 /* Returns TRUE if the ARG (a template argument) is dependent. */
25906 dependent_template_arg_p (tree arg
)
25908 if (!processing_template_decl
)
25911 /* Assume a template argument that was wrongly written by the user
25912 is dependent. This is consistent with what
25913 any_dependent_template_arguments_p [that calls this function]
25915 if (!arg
|| arg
== error_mark_node
)
25918 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
25919 arg
= argument_pack_select_arg (arg
);
25921 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
25923 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25925 if (DECL_TEMPLATE_PARM_P (arg
))
25927 /* A member template of a dependent class is not necessarily
25928 type-dependent, but it is a dependent template argument because it
25929 will be a member of an unknown specialization to that template. */
25930 tree scope
= CP_DECL_CONTEXT (arg
);
25931 return TYPE_P (scope
) && dependent_type_p (scope
);
25933 else if (ARGUMENT_PACK_P (arg
))
25935 tree args
= ARGUMENT_PACK_ARGS (arg
);
25936 int i
, len
= TREE_VEC_LENGTH (args
);
25937 for (i
= 0; i
< len
; ++i
)
25939 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25945 else if (TYPE_P (arg
))
25946 return dependent_type_p (arg
);
25948 return (type_dependent_expression_p (arg
)
25949 || value_dependent_expression_p (arg
));
25952 /* Returns true if ARGS (a collection of template arguments) contains
25953 any types that require structural equality testing. */
25956 any_template_arguments_need_structural_equality_p (tree args
)
25963 if (args
== error_mark_node
)
25966 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25968 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25969 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25971 tree arg
= TREE_VEC_ELT (level
, j
);
25972 tree packed_args
= NULL_TREE
;
25975 if (ARGUMENT_PACK_P (arg
))
25977 /* Look inside the argument pack. */
25978 packed_args
= ARGUMENT_PACK_ARGS (arg
);
25979 len
= TREE_VEC_LENGTH (packed_args
);
25982 for (k
= 0; k
< len
; ++k
)
25985 arg
= TREE_VEC_ELT (packed_args
, k
);
25987 if (error_operand_p (arg
))
25989 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25991 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
25993 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25994 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26003 /* Returns true if ARGS (a collection of template arguments) contains
26004 any dependent arguments. */
26007 any_dependent_template_arguments_p (const_tree args
)
26014 if (args
== error_mark_node
)
26017 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26019 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26020 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26021 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26028 /* Returns true if ARGS contains any errors. */
26031 any_erroneous_template_args_p (const_tree args
)
26036 if (args
== error_mark_node
)
26039 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26041 if (tree ti
= get_template_info (args
))
26042 args
= TI_ARGS (ti
);
26050 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26052 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26053 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26054 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26061 /* Returns TRUE if the template TMPL is type-dependent. */
26064 dependent_template_p (tree tmpl
)
26066 if (TREE_CODE (tmpl
) == OVERLOAD
)
26068 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26069 if (dependent_template_p (*iter
))
26074 /* Template template parameters are dependent. */
26075 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26076 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26078 /* So are names that have not been looked up. */
26079 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26084 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26087 dependent_template_id_p (tree tmpl
, tree args
)
26089 return (dependent_template_p (tmpl
)
26090 || any_dependent_template_arguments_p (args
));
26093 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26097 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26101 if (!processing_template_decl
)
26104 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26106 tree decl
= TREE_VEC_ELT (declv
, i
);
26107 tree init
= TREE_VEC_ELT (initv
, i
);
26108 tree cond
= TREE_VEC_ELT (condv
, i
);
26109 tree incr
= TREE_VEC_ELT (incrv
, i
);
26111 if (type_dependent_expression_p (decl
)
26112 || TREE_CODE (decl
) == SCOPE_REF
)
26115 if (init
&& type_dependent_expression_p (init
))
26118 if (cond
== global_namespace
)
26121 if (type_dependent_expression_p (cond
))
26124 if (COMPARISON_CLASS_P (cond
)
26125 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26126 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26129 if (TREE_CODE (incr
) == MODOP_EXPR
)
26131 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
26132 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
26135 else if (type_dependent_expression_p (incr
))
26137 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
26139 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
26141 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
26143 tree t
= TREE_OPERAND (incr
, 1);
26144 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
26145 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
26148 /* If this loop has a class iterator with != comparison
26149 with increment other than i++/++i/i--/--i, make sure the
26150 increment is constant. */
26151 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26152 && TREE_CODE (cond
) == NE_EXPR
)
26154 if (TREE_OPERAND (t
, 0) == decl
)
26155 t
= TREE_OPERAND (t
, 1);
26157 t
= TREE_OPERAND (t
, 0);
26158 if (TREE_CODE (t
) != INTEGER_CST
)
26168 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26169 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26170 no such TYPE can be found. Note that this function peers inside
26171 uninstantiated templates and therefore should be used only in
26172 extremely limited situations. ONLY_CURRENT_P restricts this
26173 peering to the currently open classes hierarchy (which is required
26174 when comparing types). */
26177 resolve_typename_type (tree type
, bool only_current_p
)
26186 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
26188 scope
= TYPE_CONTEXT (type
);
26189 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26190 gcc_checking_assert (uses_template_parms (scope
));
26192 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26193 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26194 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26195 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26196 identifier of the TYPENAME_TYPE anymore.
26197 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26198 TYPENAME_TYPE instead, we avoid messing up with a possible
26199 typedef variant case. */
26200 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
26202 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26203 it first before we can figure out what NAME refers to. */
26204 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
26206 if (TYPENAME_IS_RESOLVING_P (scope
))
26207 /* Given a class template A with a dependent base with nested type C,
26208 typedef typename A::C::C C will land us here, as trying to resolve
26209 the initial A::C leads to the local C typedef, which leads back to
26210 A::C::C. So we break the recursion now. */
26213 scope
= resolve_typename_type (scope
, only_current_p
);
26215 /* If we don't know what SCOPE refers to, then we cannot resolve the
26217 if (!CLASS_TYPE_P (scope
))
26219 /* If this is a typedef, we don't want to look inside (c++/11987). */
26220 if (typedef_variant_p (type
))
26222 /* If SCOPE isn't the template itself, it will not have a valid
26223 TYPE_FIELDS list. */
26224 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
26225 /* scope is either the template itself or a compatible instantiation
26226 like X<T>, so look up the name in the original template. */
26227 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
26228 /* If scope has no fields, it can't be a current instantiation. Check this
26229 before currently_open_class to avoid infinite recursion (71515). */
26230 if (!TYPE_FIELDS (scope
))
26232 /* If the SCOPE is not the current instantiation, there's no reason
26233 to look inside it. */
26234 if (only_current_p
&& !currently_open_class (scope
))
26236 /* Enter the SCOPE so that name lookup will be resolved as if we
26237 were in the class definition. In particular, SCOPE will no
26238 longer be considered a dependent type. */
26239 pushed_scope
= push_scope (scope
);
26240 /* Look up the declaration. */
26241 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
26242 tf_warning_or_error
);
26244 result
= NULL_TREE
;
26246 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26247 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26248 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
26251 else if (identifier_p (fullname
)
26252 && TREE_CODE (decl
) == TYPE_DECL
)
26254 result
= TREE_TYPE (decl
);
26255 if (result
== error_mark_node
)
26256 result
= NULL_TREE
;
26258 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
26259 && DECL_CLASS_TEMPLATE_P (decl
))
26261 /* Obtain the template and the arguments. */
26262 tree tmpl
= TREE_OPERAND (fullname
, 0);
26263 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
26265 /* We get here with a plain identifier because a previous tentative
26266 parse of the nested-name-specifier as part of a ptr-operator saw
26267 ::template X<A>. The use of ::template is necessary in a
26268 ptr-operator, but wrong in a declarator-id.
26270 [temp.names]: In a qualified-id of a declarator-id, the keyword
26271 template shall not appear at the top level. */
26272 pedwarn (cp_expr_loc_or_loc (fullname
, input_location
), OPT_Wpedantic
,
26273 "keyword %<template%> not allowed in declarator-id");
26276 tree args
= TREE_OPERAND (fullname
, 1);
26277 /* Instantiate the template. */
26278 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
26279 /*entering_scope=*/true,
26280 tf_error
| tf_user
);
26281 if (result
== error_mark_node
)
26282 result
= NULL_TREE
;
26285 /* Leave the SCOPE. */
26287 pop_scope (pushed_scope
);
26289 /* If we failed to resolve it, return the original typename. */
26293 /* If lookup found a typename type, resolve that too. */
26294 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
26296 /* Ill-formed programs can cause infinite recursion here, so we
26297 must catch that. */
26298 TYPENAME_IS_RESOLVING_P (result
) = 1;
26299 result
= resolve_typename_type (result
, only_current_p
);
26300 TYPENAME_IS_RESOLVING_P (result
) = 0;
26303 /* Qualify the resulting type. */
26304 quals
= cp_type_quals (type
);
26306 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
26311 /* EXPR is an expression which is not type-dependent. Return a proxy
26312 for EXPR that can be used to compute the types of larger
26313 expressions containing EXPR. */
26316 build_non_dependent_expr (tree expr
)
26318 tree orig_expr
= expr
;
26321 /* When checking, try to get a constant value for all non-dependent
26322 expressions in order to expose bugs in *_dependent_expression_p
26323 and constexpr. This can affect code generation, see PR70704, so
26324 only do this for -fchecking=2. */
26325 if (flag_checking
> 1
26326 && cxx_dialect
>= cxx11
26327 /* Don't do this during nsdmi parsing as it can lead to
26328 unexpected recursive instantiations. */
26329 && !parsing_nsdmi ()
26330 /* Don't do this during concept expansion either and for
26331 the same reason. */
26332 && !expanding_concept ())
26333 fold_non_dependent_expr (expr
, tf_none
);
26335 STRIP_ANY_LOCATION_WRAPPER (expr
);
26337 /* Preserve OVERLOADs; the functions must be available to resolve
26340 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
26341 inner_expr
= stmt_expr_value_expr (inner_expr
);
26342 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
26343 inner_expr
= TREE_OPERAND (inner_expr
, 0);
26344 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
26345 inner_expr
= TREE_OPERAND (inner_expr
, 1);
26346 if (is_overloaded_fn (inner_expr
)
26347 || TREE_CODE (inner_expr
) == OFFSET_REF
)
26349 /* There is no need to return a proxy for a variable or enumerator. */
26350 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
26352 /* Preserve string constants; conversions from string constants to
26353 "char *" are allowed, even though normally a "const char *"
26354 cannot be used to initialize a "char *". */
26355 if (TREE_CODE (expr
) == STRING_CST
)
26357 /* Preserve void and arithmetic constants, as an optimization -- there is no
26358 reason to create a new node. */
26359 if (TREE_CODE (expr
) == VOID_CST
26360 || TREE_CODE (expr
) == INTEGER_CST
26361 || TREE_CODE (expr
) == REAL_CST
)
26363 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26364 There is at least one place where we want to know that a
26365 particular expression is a throw-expression: when checking a ?:
26366 expression, there are special rules if the second or third
26367 argument is a throw-expression. */
26368 if (TREE_CODE (expr
) == THROW_EXPR
)
26371 /* Don't wrap an initializer list, we need to be able to look inside. */
26372 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
26375 /* Don't wrap a dummy object, we need to be able to test for it. */
26376 if (is_dummy_object (expr
))
26379 if (TREE_CODE (expr
) == COND_EXPR
)
26380 return build3 (COND_EXPR
,
26382 TREE_OPERAND (expr
, 0),
26383 (TREE_OPERAND (expr
, 1)
26384 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
26385 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
26386 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
26387 if (TREE_CODE (expr
) == COMPOUND_EXPR
26388 && !COMPOUND_EXPR_OVERLOADED (expr
))
26389 return build2 (COMPOUND_EXPR
,
26391 TREE_OPERAND (expr
, 0),
26392 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
26394 /* If the type is unknown, it can't really be non-dependent */
26395 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
26397 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26398 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
26399 TREE_TYPE (expr
), expr
);
26402 /* ARGS is a vector of expressions as arguments to a function call.
26403 Replace the arguments with equivalent non-dependent expressions.
26404 This modifies ARGS in place. */
26407 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
26412 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
26414 tree newarg
= build_non_dependent_expr (arg
);
26416 (*args
)[ix
] = newarg
;
26420 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26421 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26422 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26425 make_auto_1 (tree name
, bool set_canonical
)
26427 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
26428 TYPE_NAME (au
) = build_decl (input_location
,
26429 TYPE_DECL
, name
, au
);
26430 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
26431 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
26432 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
26433 TYPE_NAME (au
), NULL_TREE
);
26435 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
26436 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
26437 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26443 make_decltype_auto (void)
26445 return make_auto_1 (decltype_auto_identifier
, true);
26451 return make_auto_1 (auto_identifier
, true);
26454 /* Return a C++17 deduction placeholder for class template TMPL. */
26457 make_template_placeholder (tree tmpl
)
26459 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
26460 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26464 /* True iff T is a C++17 class template deduction placeholder. */
26467 template_placeholder_p (tree t
)
26469 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26472 /* Make a "constrained auto" type-specifier. This is an
26473 auto type with constraints that must be associated after
26474 deduction. The constraint is formed from the given
26475 CONC and its optional sequence of arguments, which are
26476 non-null if written as partial-concept-id. */
26479 make_constrained_auto (tree con
, tree args
)
26481 tree type
= make_auto_1 (auto_identifier
, false);
26483 /* Build the constraint. */
26484 tree tmpl
= DECL_TI_TEMPLATE (con
);
26485 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26486 expr
= build_concept_check (expr
, type
, args
);
26488 tree constr
= normalize_expression (expr
);
26489 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26491 /* Our canonical type depends on the constraint. */
26492 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26494 /* Attach the constraint to the type declaration. */
26495 tree decl
= TYPE_NAME (type
);
26499 /* Given type ARG, return std::initializer_list<ARG>. */
26504 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26506 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26508 gcc_rich_location
richloc (input_location
);
26509 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
26510 error_at (&richloc
,
26511 "deducing from brace-enclosed initializer list"
26512 " requires %<#include <initializer_list>%>");
26514 return error_mark_node
;
26516 tree argvec
= make_tree_vec (1);
26517 TREE_VEC_ELT (argvec
, 0) = arg
;
26519 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26520 NULL_TREE
, 0, tf_warning_or_error
);
26523 /* Replace auto in TYPE with std::initializer_list<auto>. */
26526 listify_autos (tree type
, tree auto_node
)
26528 tree init_auto
= listify (auto_node
);
26529 tree argvec
= make_tree_vec (1);
26530 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26531 if (processing_template_decl
)
26532 argvec
= add_to_template_args (current_template_args (), argvec
);
26533 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26536 /* Hash traits for hashing possibly constrained 'auto'
26537 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26539 struct auto_hash
: default_hash_traits
<tree
>
26541 static inline hashval_t
hash (tree
);
26542 static inline bool equal (tree
, tree
);
26545 /* Hash the 'auto' T. */
26548 auto_hash::hash (tree t
)
26550 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
26551 /* Matching constrained-type-specifiers denote the same template
26552 parameter, so hash the constraint. */
26553 return hash_placeholder_constraint (c
);
26555 /* But unconstrained autos are all separate, so just hash the pointer. */
26556 return iterative_hash_object (t
, 0);
26559 /* Compare two 'auto's. */
26562 auto_hash::equal (tree t1
, tree t2
)
26567 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26568 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26570 /* Two unconstrained autos are distinct. */
26574 return equivalent_placeholder_constraints (c1
, c2
);
26577 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26578 constrained) auto, add it to the vector. */
26581 extract_autos_r (tree t
, void *data
)
26583 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26586 /* All the autos were built with index 0; fix that up now. */
26587 tree
*p
= hash
.find_slot (t
, INSERT
);
26590 /* If this is a repeated constrained-type-specifier, use the index we
26592 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26595 /* Otherwise this is new, so use the current count. */
26597 idx
= hash
.elements () - 1;
26599 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26602 /* Always keep walking. */
26606 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26607 says they can appear anywhere in the type. */
26610 extract_autos (tree type
)
26612 hash_set
<tree
> visited
;
26613 hash_table
<auto_hash
> hash (2);
26615 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26617 tree tree_vec
= make_tree_vec (hash
.elements());
26618 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26619 iter
!= hash
.end(); ++iter
)
26622 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26623 TREE_VEC_ELT (tree_vec
, i
)
26624 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26630 /* The stem for deduction guide names. */
26631 const char *const dguide_base
= "__dguide_";
26633 /* Return the name for a deduction guide for class template TMPL. */
26636 dguide_name (tree tmpl
)
26638 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26639 tree tname
= TYPE_IDENTIFIER (type
);
26640 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26641 + IDENTIFIER_LENGTH (tname
));
26642 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26643 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26644 IDENTIFIER_LENGTH (tname
) + 1);
26645 tree dname
= get_identifier (buf
);
26646 TREE_TYPE (dname
) = type
;
26650 /* True if NAME is the name of a deduction guide. */
26653 dguide_name_p (tree name
)
26655 return (TREE_CODE (name
) == IDENTIFIER_NODE
26656 && TREE_TYPE (name
)
26657 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
26658 strlen (dguide_base
)));
26661 /* True if FN is a deduction guide. */
26664 deduction_guide_p (const_tree fn
)
26667 if (tree name
= DECL_NAME (fn
))
26668 return dguide_name_p (name
);
26672 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26675 copy_guide_p (const_tree fn
)
26677 gcc_assert (deduction_guide_p (fn
));
26678 if (!DECL_ARTIFICIAL (fn
))
26680 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
26681 return (TREE_CHAIN (parms
) == void_list_node
26682 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
26685 /* True if FN is a guide generated from a constructor template. */
26688 template_guide_p (const_tree fn
)
26690 gcc_assert (deduction_guide_p (fn
));
26691 if (!DECL_ARTIFICIAL (fn
))
26693 tree tmpl
= DECL_TI_TEMPLATE (fn
);
26694 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
26695 return PRIMARY_TEMPLATE_P (org
);
26699 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26700 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26701 template parameter types. Note that the handling of template template
26702 parameters relies on current_template_parms being set appropriately for the
26706 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
26707 tree tsubst_args
, tsubst_flags_t complain
)
26709 if (olddecl
== error_mark_node
)
26710 return error_mark_node
;
26712 tree oldidx
= get_template_parm_index (olddecl
);
26715 if (TREE_CODE (olddecl
) == TYPE_DECL
26716 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26718 tree oldtype
= TREE_TYPE (olddecl
);
26719 newtype
= cxx_make_type (TREE_CODE (oldtype
));
26720 TYPE_MAIN_VARIANT (newtype
) = newtype
;
26721 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
26722 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
26723 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
26727 newtype
= TREE_TYPE (olddecl
);
26728 if (type_uses_auto (newtype
))
26730 // Substitute once to fix references to other template parameters.
26731 newtype
= tsubst (newtype
, tsubst_args
,
26732 complain
|tf_partial
, NULL_TREE
);
26733 // Now substitute again to reduce the level of the auto.
26734 newtype
= tsubst (newtype
, current_template_args (),
26735 complain
, NULL_TREE
);
26738 newtype
= tsubst (newtype
, tsubst_args
,
26739 complain
, NULL_TREE
);
26743 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
26744 DECL_NAME (olddecl
), newtype
);
26745 SET_DECL_TEMPLATE_PARM_P (newdecl
);
26748 if (TREE_CODE (olddecl
) == TYPE_DECL
26749 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26751 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
26752 = build_template_parm_index (index
, level
, level
,
26754 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26755 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26756 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
26757 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
26759 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26761 DECL_TEMPLATE_RESULT (newdecl
)
26762 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
26763 DECL_NAME (olddecl
), newtype
);
26764 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
26765 // First create a copy (ttargs) of tsubst_args with an
26766 // additional level for the template template parameter's own
26767 // template parameters (ttparms).
26768 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
26769 (DECL_TEMPLATE_PARMS (olddecl
)));
26770 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
26771 tree ttargs
= make_tree_vec (depth
+ 1);
26772 for (int i
= 0; i
< depth
; ++i
)
26773 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
26774 TREE_VEC_ELT (ttargs
, depth
)
26775 = template_parms_level_to_args (ttparms
);
26776 // Substitute ttargs into ttparms to fix references to
26777 // other template parameters.
26778 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26779 complain
|tf_partial
);
26780 // Now substitute again with args based on tparms, to reduce
26781 // the level of the ttparms.
26782 ttargs
= current_template_args ();
26783 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26785 // Finally, tack the adjusted parms onto tparms.
26786 ttparms
= tree_cons (size_int (depth
), ttparms
,
26787 current_template_parms
);
26788 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
26793 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
26795 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
26796 TREE_CODE (oldconst
),
26797 DECL_NAME (oldconst
), newtype
);
26798 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
26799 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
26800 SET_DECL_TEMPLATE_PARM_P (newconst
);
26801 newidx
= build_template_parm_index (index
, level
, level
,
26802 newconst
, newtype
);
26803 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26804 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26805 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
26811 /* Returns a C++17 class deduction guide template based on the constructor
26812 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26813 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26816 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
26818 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
26819 bool memtmpl
= false;
26822 tree fn_tmpl
= NULL_TREE
;
26827 bool copy_p
= TYPE_REF_P (type
);
26830 type
= TREE_TYPE (type
);
26831 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
26834 fparms
= void_list_node
;
26836 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
26837 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
26838 targs
= CLASSTYPE_TI_ARGS (type
);
26841 loc
= DECL_SOURCE_LOCATION (ctmpl
);
26842 explicit_p
= false;
26846 ++processing_template_decl
;
26850 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
26851 : DECL_TI_TEMPLATE (ctor
));
26853 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
26854 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
26856 type
= DECL_CONTEXT (ctor
);
26858 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
26859 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26860 fully specialized args for the enclosing class. Strip those off, as
26861 the deduction guide won't have those template parameters. */
26862 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
26863 TMPL_PARMS_DEPTH (tparms
));
26864 /* Discard the 'this' parameter. */
26865 fparms
= FUNCTION_ARG_CHAIN (ctor
);
26866 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
26867 ci
= get_constraints (ctor
);
26868 loc
= DECL_SOURCE_LOCATION (ctor
);
26869 explicit_p
= DECL_NONCONVERTING_P (ctor
);
26871 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
26875 /* For a member template constructor, we need to flatten the two
26876 template parameter lists into one, and then adjust the function
26877 signature accordingly. This gets...complicated. */
26878 tree save_parms
= current_template_parms
;
26880 /* For a member template we should have two levels of parms/args, one
26881 for the class and one for the constructor. We stripped
26882 specialized args for further enclosing classes above. */
26883 const int depth
= 2;
26884 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
26886 /* Template args for translating references to the two-level template
26887 parameters into references to the one-level template parameters we
26889 tree tsubst_args
= copy_node (targs
);
26890 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
26891 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
26893 /* Template parms for the constructor template. */
26894 tree ftparms
= TREE_VALUE (tparms
);
26895 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
26896 /* Template parms for the class template. */
26897 tparms
= TREE_CHAIN (tparms
);
26898 tree ctparms
= TREE_VALUE (tparms
);
26899 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
26900 /* Template parms for the deduction guide start as a copy of the
26901 template parms for the class. We set current_template_parms for
26902 lookup_template_class_1. */
26903 current_template_parms
= tparms
= copy_node (tparms
);
26904 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
26905 for (unsigned i
= 0; i
< clen
; ++i
)
26906 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
26908 /* Now we need to rewrite the constructor parms to append them to the
26910 for (unsigned i
= 0; i
< flen
; ++i
)
26912 unsigned index
= i
+ clen
;
26913 unsigned level
= 1;
26914 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
26915 tree olddecl
= TREE_VALUE (oldelt
);
26916 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
26917 tsubst_args
, complain
);
26918 if (newdecl
== error_mark_node
)
26920 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
26921 tsubst_args
, complain
, ctor
);
26922 tree list
= build_tree_list (newdef
, newdecl
);
26923 TEMPLATE_PARM_CONSTRAINTS (list
)
26924 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
26925 tsubst_args
, complain
, ctor
);
26926 TREE_VEC_ELT (new_vec
, index
) = list
;
26927 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
26930 /* Now we have a final set of template parms to substitute into the
26931 function signature. */
26932 targs
= template_parms_to_args (tparms
);
26933 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
26935 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
26937 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
26939 current_template_parms
= save_parms
;
26942 --processing_template_decl
;
26944 return error_mark_node
;
26949 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26950 tparms
= copy_node (tparms
);
26951 INNERMOST_TEMPLATE_PARMS (tparms
)
26952 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
26955 tree fntype
= build_function_type (type
, fparms
);
26956 tree ded_fn
= build_lang_decl_loc (loc
,
26958 dguide_name (type
), fntype
);
26959 DECL_ARGUMENTS (ded_fn
) = fargs
;
26960 DECL_ARTIFICIAL (ded_fn
) = true;
26961 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
26962 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
26963 DECL_ARTIFICIAL (ded_tmpl
) = true;
26964 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
26965 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
26966 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
26967 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
26969 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
26971 set_constraints (ded_tmpl
, ci
);
26976 /* Deduce template arguments for the class template placeholder PTYPE for
26977 template TMPL based on the initializer INIT, and return the resulting
26981 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
26982 tsubst_flags_t complain
)
26984 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
26986 /* We should have handled this in the caller. */
26987 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26989 if (complain
& tf_error
)
26990 error ("non-class template %qT used without template arguments", tmpl
);
26991 return error_mark_node
;
26994 tree type
= TREE_TYPE (tmpl
);
26996 bool try_list_ctor
= false;
26998 vec
<tree
,va_gc
> *args
;
26999 if (init
== NULL_TREE
27000 || TREE_CODE (init
) == TREE_LIST
)
27001 args
= make_tree_vector_from_list (init
);
27002 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27004 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
27005 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
27007 /* As an exception, the first phase in 16.3.1.7 (considering the
27008 initializer list as a single argument) is omitted if the
27009 initializer list consists of a single expression of type cv U,
27010 where U is a specialization of C or a class derived from a
27011 specialization of C. */
27012 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
27013 tree etype
= TREE_TYPE (elt
);
27015 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
27016 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27017 int err
= unify (tparms
, targs
, type
, etype
,
27018 UNIFY_ALLOW_DERIVED
, /*explain*/false);
27020 try_list_ctor
= false;
27023 if (try_list_ctor
|| is_std_init_list (type
))
27024 args
= make_tree_vector_single (init
);
27026 args
= make_tree_vector_from_ctor (init
);
27029 args
= make_tree_vector_single (init
);
27031 tree dname
= dguide_name (tmpl
);
27032 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
27033 /*type*/false, /*complain*/false,
27035 bool elided
= false;
27036 if (cands
== error_mark_node
)
27039 /* Prune explicit deduction guides in copy-initialization context. */
27040 if (flags
& LOOKUP_ONLYCONVERTING
)
27042 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
27043 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27048 /* Found a nonconverting guide, prune the candidates. */
27049 tree pruned
= NULL_TREE
;
27050 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27051 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27052 pruned
= lookup_add (*iter
, pruned
);
27058 tree outer_args
= NULL_TREE
;
27059 if (DECL_CLASS_SCOPE_P (tmpl
)
27060 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
27062 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
27063 type
= TREE_TYPE (most_general_template (tmpl
));
27066 bool saw_ctor
= false;
27067 // FIXME cache artificial deduction guides
27068 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
27070 /* Skip inherited constructors. */
27071 if (iter
.using_p ())
27074 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
27075 if (guide
== error_mark_node
)
27076 return error_mark_node
;
27077 if ((flags
& LOOKUP_ONLYCONVERTING
)
27078 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
27081 cands
= lookup_add (guide
, cands
);
27086 tree call
= error_mark_node
;
27088 /* If this is list-initialization and the class has a list constructor, first
27089 try deducing from the list as a single argument, as [over.match.list]. */
27090 tree list_cands
= NULL_TREE
;
27091 if (try_list_ctor
&& cands
)
27092 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27095 if (is_list_ctor (dg
))
27096 list_cands
= lookup_add (dg
, list_cands
);
27100 ++cp_unevaluated_operand
;
27101 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
27102 --cp_unevaluated_operand
;
27104 if (call
== error_mark_node
)
27106 /* That didn't work, now try treating the list as a sequence of
27108 release_tree_vector (args
);
27109 args
= make_tree_vector_from_ctor (init
);
27113 /* Maybe generate an implicit deduction guide. */
27114 if (call
== error_mark_node
&& args
->length () < 2)
27116 tree gtype
= NULL_TREE
;
27118 if (args
->length () == 1)
27119 /* Generate a copy guide. */
27120 gtype
= build_reference_type (type
);
27121 else if (!saw_ctor
)
27122 /* Generate a default guide. */
27127 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
27128 if (guide
== error_mark_node
)
27129 return error_mark_node
;
27130 cands
= lookup_add (guide
, cands
);
27134 if (elided
&& !cands
)
27136 error ("cannot deduce template arguments for copy-initialization"
27137 " of %qT, as it has no non-explicit deduction guides or "
27138 "user-declared constructors", type
);
27139 return error_mark_node
;
27141 else if (!cands
&& call
== error_mark_node
)
27143 error ("cannot deduce template arguments of %qT, as it has no viable "
27144 "deduction guides", type
);
27145 return error_mark_node
;
27148 if (call
== error_mark_node
)
27150 ++cp_unevaluated_operand
;
27151 call
= build_new_function_call (cands
, &args
, tf_decltype
);
27152 --cp_unevaluated_operand
;
27155 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
27157 error ("class template argument deduction failed:");
27159 ++cp_unevaluated_operand
;
27160 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
27161 --cp_unevaluated_operand
;
27164 inform (input_location
, "explicit deduction guides not considered "
27165 "for copy-initialization");
27168 release_tree_vector (args
);
27170 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
27173 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27174 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27175 The CONTEXT determines the context in which auto deduction is performed
27176 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27177 OUTER_TARGS are used during template argument deduction
27178 (context == adc_unify) to properly substitute the result, and is ignored
27181 For partial-concept-ids, extra args may be appended to the list of deduced
27182 template arguments prior to determining constraint satisfaction. */
27185 do_auto_deduction (tree type
, tree init
, tree auto_node
,
27186 tsubst_flags_t complain
, auto_deduction_context context
,
27187 tree outer_targs
, int flags
)
27191 if (init
== error_mark_node
)
27192 return error_mark_node
;
27194 if (init
&& type_dependent_expression_p (init
)
27195 && context
!= adc_unify
)
27196 /* Defining a subset of type-dependent expressions that we can deduce
27197 from ahead of time isn't worth the trouble. */
27200 /* Similarly, we can't deduce from another undeduced decl. */
27201 if (init
&& undeduced_auto_decl (init
))
27204 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
27205 /* C++17 class template argument deduction. */
27206 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
27208 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
27209 /* Nothing we can do with this, even in deduction context. */
27212 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27213 with either a new invented type template parameter U or, if the
27214 initializer is a braced-init-list (8.5.4), with
27215 std::initializer_list<U>. */
27216 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27218 if (!DIRECT_LIST_INIT_P (init
))
27219 type
= listify_autos (type
, auto_node
);
27220 else if (CONSTRUCTOR_NELTS (init
) == 1)
27221 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
27224 if (complain
& tf_warning_or_error
)
27226 if (permerror (input_location
, "direct-list-initialization of "
27227 "%<auto%> requires exactly one element"))
27228 inform (input_location
,
27229 "for deduction to %<std::initializer_list%>, use copy-"
27230 "list-initialization (i.e. add %<=%> before the %<{%>)");
27232 type
= listify_autos (type
, auto_node
);
27236 if (type
== error_mark_node
)
27237 return error_mark_node
;
27239 init
= resolve_nondeduced_context (init
, complain
);
27241 if (context
== adc_decomp_type
27242 && auto_node
== type
27243 && init
!= error_mark_node
27244 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
27245 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27246 and initializer has array type, deduce cv-qualified array type. */
27247 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
27249 else if (AUTO_IS_DECLTYPE (auto_node
))
27251 tree stripped_init
= tree_strip_any_location_wrapper (init
);
27252 bool id
= (DECL_P (stripped_init
)
27253 || ((TREE_CODE (init
) == COMPONENT_REF
27254 || TREE_CODE (init
) == SCOPE_REF
)
27255 && !REF_PARENTHESIZED_P (init
)));
27256 targs
= make_tree_vec (1);
27257 TREE_VEC_ELT (targs
, 0)
27258 = finish_decltype_type (init
, id
, tf_warning_or_error
);
27259 if (type
!= auto_node
)
27261 if (complain
& tf_error
)
27262 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
27263 return error_mark_node
;
27268 tree parms
= build_tree_list (NULL_TREE
, type
);
27272 tparms
= extract_autos (type
);
27275 tparms
= make_tree_vec (1);
27276 TREE_VEC_ELT (tparms
, 0)
27277 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
27280 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27281 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27283 NULL
, /*explain_p=*/false);
27286 if (processing_template_decl
)
27287 /* Try again at instantiation time. */
27289 if (type
&& type
!= error_mark_node
27290 && (complain
& tf_error
))
27291 /* If type is error_mark_node a diagnostic must have been
27292 emitted by now. Also, having a mention to '<type error>'
27293 in the diagnostic is not really useful to the user. */
27295 if (cfun
&& auto_node
== current_function_auto_return_pattern
27296 && LAMBDA_FUNCTION_P (current_function_decl
))
27297 error ("unable to deduce lambda return type from %qE", init
);
27299 error ("unable to deduce %qT from %qE", type
, init
);
27300 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27302 NULL
, /*explain_p=*/true);
27304 return error_mark_node
;
27308 /* Check any placeholder constraints against the deduced type. */
27309 if (flag_concepts
&& !processing_template_decl
)
27310 if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
27312 /* Use the deduced type to check the associated constraints. If we
27313 have a partial-concept-id, rebuild the argument list so that
27314 we check using the extra arguments. */
27315 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
27316 tree cargs
= CHECK_CONSTR_ARGS (constr
);
27317 if (TREE_VEC_LENGTH (cargs
) > 1)
27319 cargs
= copy_node (cargs
);
27320 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
27324 if (!constraints_satisfied_p (constr
, cargs
))
27326 if (complain
& tf_warning_or_error
)
27328 auto_diagnostic_group d
;
27331 case adc_unspecified
:
27333 error("placeholder constraints not satisfied");
27335 case adc_variable_type
:
27336 case adc_decomp_type
:
27337 error ("deduced initializer does not satisfy "
27338 "placeholder constraints");
27340 case adc_return_type
:
27341 error ("deduced return type does not satisfy "
27342 "placeholder constraints");
27344 case adc_requirement
:
27345 error ("deduced expression type does not satisfy "
27346 "placeholder constraints");
27349 diagnose_constraints (input_location
, constr
, targs
);
27351 return error_mark_node
;
27355 if (processing_template_decl
&& context
!= adc_unify
)
27356 outer_targs
= current_template_args ();
27357 targs
= add_to_template_args (outer_targs
, targs
);
27358 return tsubst (type
, targs
, complain
, NULL_TREE
);
27361 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27365 splice_late_return_type (tree type
, tree late_return_type
)
27367 if (is_auto (type
))
27369 if (late_return_type
)
27370 return late_return_type
;
27372 tree idx
= get_template_parm_index (type
);
27373 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
27374 /* In an abbreviated function template we didn't know we were dealing
27375 with a function template when we saw the auto return type, so update
27376 it to have the correct level. */
27377 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
27382 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27383 'decltype(auto)' or a deduced class template. */
27386 is_auto (const_tree type
)
27388 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27389 && (TYPE_IDENTIFIER (type
) == auto_identifier
27390 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
27391 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
27397 /* for_each_template_parm callback for type_uses_auto. */
27400 is_auto_r (tree tp
, void */
*data*/
)
27402 return is_auto (tp
);
27405 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27406 a use of `auto'. Returns NULL_TREE otherwise. */
27409 type_uses_auto (tree type
)
27411 if (type
== NULL_TREE
)
27413 else if (flag_concepts
)
27415 /* The Concepts TS allows multiple autos in one type-specifier; just
27416 return the first one we find, do_auto_deduction will collect all of
27418 if (uses_template_parms (type
))
27419 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
27420 /*visited*/NULL
, /*nondeduced*/false);
27425 return find_type_usage (type
, is_auto
);
27428 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27429 concepts are enabled, auto is acceptable in template arguments, but
27430 only when TEMPL identifies a template class. Return TRUE if any
27431 such errors were reported. */
27434 check_auto_in_tmpl_args (tree tmpl
, tree args
)
27436 /* If there were previous errors, nevermind. */
27437 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27440 /* If TMPL is an identifier, we're parsing and we can't tell yet
27441 whether TMPL is supposed to be a type, a function or a variable.
27442 We'll only be able to tell during template substitution, so we
27443 expect to be called again then. If concepts are enabled and we
27444 know we have a type, we're ok. */
27446 && (identifier_p (tmpl
)
27448 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27449 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27452 /* Quickly search for any occurrences of auto; usually there won't
27453 be any, and then we'll avoid allocating the vector. */
27454 if (!type_uses_auto (args
))
27457 bool errors
= false;
27459 tree vec
= extract_autos (args
);
27460 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27462 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27463 error_at (DECL_SOURCE_LOCATION (xauto
),
27464 "invalid use of %qT in template argument", xauto
);
27471 /* For a given template T, return the vector of typedefs referenced
27472 in T for which access check is needed at T instantiation time.
27473 T is either a FUNCTION_DECL or a RECORD_TYPE.
27474 Those typedefs were added to T by the function
27475 append_type_to_template_for_access_check. */
27477 vec
<qualified_typedef_usage_t
, va_gc
> *
27478 get_types_needing_access_check (tree t
)
27481 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27483 if (!t
|| t
== error_mark_node
)
27486 if (!(ti
= get_template_info (t
)))
27489 if (CLASS_TYPE_P (t
)
27490 || TREE_CODE (t
) == FUNCTION_DECL
)
27492 if (!TI_TEMPLATE (ti
))
27495 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27501 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27502 tied to T. That list of typedefs will be access checked at
27503 T instantiation time.
27504 T is either a FUNCTION_DECL or a RECORD_TYPE.
27505 TYPE_DECL is a TYPE_DECL node representing a typedef.
27506 SCOPE is the scope through which TYPE_DECL is accessed.
27507 LOCATION is the location of the usage point of TYPE_DECL.
27509 This function is a subroutine of
27510 append_type_to_template_for_access_check. */
27513 append_type_to_template_for_access_check_1 (tree t
,
27516 location_t location
)
27518 qualified_typedef_usage_t typedef_usage
;
27521 if (!t
|| t
== error_mark_node
)
27524 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27525 || CLASS_TYPE_P (t
))
27527 && TREE_CODE (type_decl
) == TYPE_DECL
27530 if (!(ti
= get_template_info (t
)))
27533 gcc_assert (TI_TEMPLATE (ti
));
27535 typedef_usage
.typedef_decl
= type_decl
;
27536 typedef_usage
.context
= scope
;
27537 typedef_usage
.locus
= location
;
27539 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27542 /* Append TYPE_DECL to the template TEMPL.
27543 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27544 At TEMPL instanciation time, TYPE_DECL will be checked to see
27545 if it can be accessed through SCOPE.
27546 LOCATION is the location of the usage point of TYPE_DECL.
27548 e.g. consider the following code snippet:
27555 template<class U> struct S
27557 C::myint mi; // <-- usage point of the typedef C::myint
27562 At S<char> instantiation time, we need to check the access of C::myint
27563 In other words, we need to check the access of the myint typedef through
27564 the C scope. For that purpose, this function will add the myint typedef
27565 and the scope C through which its being accessed to a list of typedefs
27566 tied to the template S. That list will be walked at template instantiation
27567 time and access check performed on each typedefs it contains.
27568 Note that this particular code snippet should yield an error because
27569 myint is private to C. */
27572 append_type_to_template_for_access_check (tree templ
,
27575 location_t location
)
27577 qualified_typedef_usage_t
*iter
;
27580 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27582 /* Make sure we don't append the type to the template twice. */
27583 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27584 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27587 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27591 /* Convert the generic type parameters in PARM that match the types given in the
27592 range [START_IDX, END_IDX) from the current_template_parms into generic type
27596 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27598 tree current
= current_template_parms
;
27599 int depth
= TMPL_PARMS_DEPTH (current
);
27600 current
= INNERMOST_TEMPLATE_PARMS (current
);
27601 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27603 for (int i
= 0; i
< start_idx
; ++i
)
27604 TREE_VEC_ELT (replacement
, i
)
27605 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27607 for (int i
= start_idx
; i
< end_idx
; ++i
)
27609 /* Create a distinct parameter pack type from the current parm and add it
27610 to the replacement args to tsubst below into the generic function
27613 tree o
= TREE_TYPE (TREE_VALUE
27614 (TREE_VEC_ELT (current
, i
)));
27615 tree t
= copy_type (o
);
27616 TEMPLATE_TYPE_PARM_INDEX (t
)
27617 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27619 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27620 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27621 TYPE_MAIN_VARIANT (t
) = t
;
27622 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27623 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27624 TREE_VEC_ELT (replacement
, i
) = t
;
27625 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27628 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27629 TREE_VEC_ELT (replacement
, i
)
27630 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27632 /* If there are more levels then build up the replacement with the outer
27635 replacement
= add_to_template_args (template_parms_to_args
27636 (TREE_CHAIN (current_template_parms
)),
27639 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
27642 /* Entries in the decl_constraint hash table. */
27643 struct GTY((for_user
)) constr_entry
27649 /* Hashing function and equality for constraint entries. */
27650 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
27652 static hashval_t
hash (constr_entry
*e
)
27654 return (hashval_t
)DECL_UID (e
->decl
);
27657 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
27659 return e1
->decl
== e2
->decl
;
27663 /* A mapping from declarations to constraint information. Note that
27664 both templates and their underlying declarations are mapped to the
27665 same constraint information.
27667 FIXME: This is defined in pt.c because garbage collection
27668 code is not being generated for constraint.cc. */
27670 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
27672 /* Returns the template constraints of declaration T. If T is not
27673 constrained, return NULL_TREE. Note that T must be non-null. */
27676 get_constraints (tree t
)
27678 if (!flag_concepts
)
27681 gcc_assert (DECL_P (t
));
27682 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27683 t
= DECL_TEMPLATE_RESULT (t
);
27684 constr_entry elt
= { t
, NULL_TREE
};
27685 constr_entry
* found
= decl_constraints
->find (&elt
);
27692 /* Associate the given constraint information CI with the declaration
27693 T. If T is a template, then the constraints are associated with
27694 its underlying declaration. Don't build associations if CI is
27698 set_constraints (tree t
, tree ci
)
27702 gcc_assert (t
&& flag_concepts
);
27703 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27704 t
= DECL_TEMPLATE_RESULT (t
);
27705 gcc_assert (!get_constraints (t
));
27706 constr_entry elt
= {t
, ci
};
27707 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
27708 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
27713 /* Remove the associated constraints of the declaration T. */
27716 remove_constraints (tree t
)
27718 gcc_assert (DECL_P (t
));
27719 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27720 t
= DECL_TEMPLATE_RESULT (t
);
27722 constr_entry elt
= {t
, NULL_TREE
};
27723 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
27725 decl_constraints
->clear_slot (slot
);
27728 /* Memoized satisfaction results for declarations. This
27729 maps the pair (constraint_info, arguments) to the result computed
27730 by constraints_satisfied_p. */
27732 struct GTY((for_user
)) constraint_sat_entry
27739 /* Hashing function and equality for constraint entries. */
27741 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
27743 static hashval_t
hash (constraint_sat_entry
*e
)
27745 hashval_t val
= iterative_hash_object(e
->ci
, 0);
27746 return iterative_hash_template_arg (e
->args
, val
);
27749 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
27751 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
27755 /* Memoized satisfaction results for concept checks. */
27757 struct GTY((for_user
)) concept_spec_entry
27764 /* Hashing function and equality for constraint entries. */
27766 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
27768 static hashval_t
hash (concept_spec_entry
*e
)
27770 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
27773 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
27775 ++comparing_specializations
;
27776 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
27777 --comparing_specializations
;
27782 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
27783 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
27785 /* Search for a memoized satisfaction result. Returns one of the
27786 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27789 lookup_constraint_satisfaction (tree ci
, tree args
)
27791 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
27792 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
27794 return found
->result
;
27799 /* Memoize the result of a satisfication test. Returns the saved result. */
27802 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
27804 constraint_sat_entry elt
= {ci
, args
, result
};
27805 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
27806 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
27812 /* Search for a memoized satisfaction result for a concept. */
27815 lookup_concept_satisfaction (tree tmpl
, tree args
)
27817 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27818 concept_spec_entry
* found
= concept_memos
->find (&elt
);
27820 return found
->result
;
27825 /* Memoize the result of a concept check. Returns the saved result. */
27828 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
27830 concept_spec_entry elt
= {tmpl
, args
, result
};
27831 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
27832 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27838 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
27840 /* Returns a prior concept specialization. This returns the substituted
27841 and normalized constraints defined by the concept. */
27844 get_concept_expansion (tree tmpl
, tree args
)
27846 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27847 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
27849 return found
->result
;
27854 /* Save a concept expansion for later. */
27857 save_concept_expansion (tree tmpl
, tree args
, tree def
)
27859 concept_spec_entry elt
= {tmpl
, args
, def
};
27860 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
27861 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27868 hash_subsumption_args (tree t1
, tree t2
)
27870 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
27871 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
27873 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
27874 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
27875 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
27876 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
27880 /* Compare the constraints of two subsumption entries. The LEFT1 and
27881 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27882 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27885 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
27887 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
27888 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
27889 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
27890 CHECK_CONSTR_ARGS (right1
)))
27891 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
27892 CHECK_CONSTR_ARGS (right2
));
27896 /* Key/value pair for learning and memoizing subsumption results. This
27897 associates a pair of check constraints (including arguments) with
27898 a boolean value indicating the result. */
27900 struct GTY((for_user
)) subsumption_entry
27907 /* Hashing function and equality for constraint entries. */
27909 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
27911 static hashval_t
hash (subsumption_entry
*e
)
27913 return hash_subsumption_args (e
->t1
, e
->t2
);
27916 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
27918 ++comparing_specializations
;
27919 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
27920 --comparing_specializations
;
27925 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
27927 /* Search for a previously cached subsumption result. */
27930 lookup_subsumption_result (tree t1
, tree t2
)
27932 subsumption_entry elt
= { t1
, t2
, false };
27933 subsumption_entry
* found
= subsumption_table
->find (&elt
);
27935 return &found
->result
;
27940 /* Save a subsumption result. */
27943 save_subsumption_result (tree t1
, tree t2
, bool result
)
27945 subsumption_entry elt
= {t1
, t2
, result
};
27946 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
27947 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
27953 /* Set up the hash table for constraint association. */
27956 init_constraint_processing (void)
27958 if (!flag_concepts
)
27961 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
27962 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
27963 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27964 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27965 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
27968 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27972 declare_integer_pack (void)
27974 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
27975 build_function_type_list (integer_type_node
,
27978 NULL_TREE
, ECF_CONST
);
27979 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
27980 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
27981 DECL_FUNCTION_CODE (ipfn
)
27982 = (enum built_in_function
) (int) CP_BUILT_IN_INTEGER_PACK
;
27985 /* Set up the hash tables for template instantiations. */
27988 init_template_processing (void)
27990 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27991 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27993 if (cxx_dialect
>= cxx11
)
27994 declare_integer_pack ();
27997 /* Print stats about the template hash tables for -fstats. */
28000 print_template_statistics (void)
28002 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28003 "%f collisions\n", (long) decl_specializations
->size (),
28004 (long) decl_specializations
->elements (),
28005 decl_specializations
->collisions ());
28006 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28007 "%f collisions\n", (long) type_specializations
->size (),
28008 (long) type_specializations
->elements (),
28009 type_specializations
->collisions ());
28014 namespace selftest
{
28016 /* Verify that build_non_dependent_expr () works, for various expressions,
28017 and that location wrappers don't affect the results. */
28020 test_build_non_dependent_expr ()
28022 location_t loc
= BUILTINS_LOCATION
;
28024 /* Verify constants, without and with location wrappers. */
28025 tree int_cst
= build_int_cst (integer_type_node
, 42);
28026 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28028 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28029 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28030 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28032 tree string_lit
= build_string (4, "foo");
28033 TREE_TYPE (string_lit
) = char_array_type_node
;
28034 string_lit
= fix_string_type (string_lit
);
28035 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28037 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28038 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28039 ASSERT_EQ (wrapped_string_lit
,
28040 build_non_dependent_expr (wrapped_string_lit
));
28043 /* Verify that type_dependent_expression_p () works correctly, even
28044 in the presence of location wrapper nodes. */
28047 test_type_dependent_expression_p ()
28049 location_t loc
= BUILTINS_LOCATION
;
28051 tree name
= get_identifier ("foo");
28053 /* If no templates are involved, nothing is type-dependent. */
28054 gcc_assert (!processing_template_decl
);
28055 ASSERT_FALSE (type_dependent_expression_p (name
));
28057 ++processing_template_decl
;
28059 /* Within a template, an unresolved name is always type-dependent. */
28060 ASSERT_TRUE (type_dependent_expression_p (name
));
28062 /* Ensure it copes with NULL_TREE and errors. */
28063 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28064 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28066 /* A USING_DECL in a template should be type-dependent, even if wrapped
28067 with a location wrapper (PR c++/83799). */
28068 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28069 TREE_TYPE (using_decl
) = integer_type_node
;
28070 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28071 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28072 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28073 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28075 --processing_template_decl
;
28078 /* Run all of the selftests within this file. */
28083 test_build_non_dependent_expr ();
28084 test_type_dependent_expression_p ();
28087 } // namespace selftest
28089 #endif /* #if CHECKING_P */
28091 #include "gt-cp-pt.h"