1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static void push_access_scope (tree
);
139 static void pop_access_scope (tree
);
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static tree
add_outermost_template_args (tree
, tree
);
157 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
158 static int check_non_deducible_conversion (tree
, tree
, int, int,
159 struct conversion
**, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
162 static int type_unification_real (tree
, tree
, tree
, const tree
*,
163 unsigned int, int, unification_kind_t
,
164 vec
<deferred_access_check
, va_gc
> **,
166 static void note_template_header (int);
167 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
168 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
169 static tree
convert_template_argument (tree
, tree
, tree
,
170 tsubst_flags_t
, int, tree
);
171 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
172 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
173 static tree
expand_template_argument_pack (tree
);
174 static tree
build_template_parm_index (int, int, int, tree
, tree
);
175 static bool inline_needs_template_parms (tree
, bool);
176 static void push_inline_template_parms_recursive (tree
, int);
177 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
178 static int mark_template_parm (tree
, void *);
179 static int template_parm_this_level_p (tree
, void *);
180 static tree
tsubst_friend_function (tree
, tree
);
181 static tree
tsubst_friend_class (tree
, tree
);
182 static int can_complete_type_without_circularity (tree
);
183 static tree
get_bindings (tree
, tree
, tree
, bool);
184 static int template_decl_level (tree
);
185 static int check_cv_quals_for_unify (int, tree
, tree
);
186 static void template_parm_level_and_index (tree
, int*, int*);
187 static int unify_pack_expansion (tree
, tree
, tree
,
188 tree
, unification_kind_t
, bool, bool);
189 static tree
copy_template_args (tree
);
190 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
193 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
194 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
195 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
197 static bool check_specialization_scope (void);
198 static tree
process_partial_specialization (tree
);
199 static void set_current_access_from_decl (tree
);
200 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
202 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
203 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
205 static bool template_template_parm_bindings_ok_p (tree
, tree
);
206 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
207 static tree
for_each_template_parm_r (tree
*, int *, void *);
208 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
209 static void copy_default_args_to_explicit_spec (tree
);
210 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
211 static bool dependent_template_arg_p (tree
);
212 static bool any_template_arguments_need_structural_equality_p (tree
);
213 static bool dependent_type_p_r (tree
);
214 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
215 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
216 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
217 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
225 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
226 static tree
make_argument_pack (tree
);
227 static void register_parameter_specializations (tree
, tree
);
228 static tree
enclosing_instantiation_of (tree tctx
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_CLASS_SCOPE_P (t
))
244 push_nested_class (DECL_CONTEXT (t
));
246 push_to_top_level ();
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 saved_access_scope
= tree_cons
251 (NULL_TREE
, current_function_decl
, saved_access_scope
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= TREE_VALUE (saved_access_scope
);
265 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
268 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
271 pop_from_top_level ();
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
280 finish_member_template_decl (tree decl
)
282 if (decl
== error_mark_node
)
283 return error_mark_node
;
285 gcc_assert (DECL_P (decl
));
287 if (TREE_CODE (decl
) == TYPE_DECL
)
291 type
= TREE_TYPE (decl
);
292 if (type
== error_mark_node
)
293 return error_mark_node
;
294 if (MAYBE_CLASS_TYPE_P (type
)
295 && CLASSTYPE_TEMPLATE_INFO (type
)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
298 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
299 check_member_template (tmpl
);
304 else if (TREE_CODE (decl
) == FIELD_DECL
)
305 error ("data member %qD cannot be a member template", decl
);
306 else if (DECL_TEMPLATE_INFO (decl
))
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
310 check_member_template (DECL_TI_TEMPLATE (decl
));
311 return DECL_TI_TEMPLATE (decl
);
317 error ("invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
393 type
= CP_DECL_CONTEXT (type
);
394 else if (LAMBDA_TYPE_P (type
))
395 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
397 type
= CP_TYPE_CONTEXT (type
);
403 /* Subroutine of maybe_begin_member_template_processing.
404 Returns true if processing DECL needs us to push template parms. */
407 inline_needs_template_parms (tree decl
, bool nsdmi
)
409 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
412 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
413 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
416 /* Subroutine of maybe_begin_member_template_processing.
417 Push the template parms in PARMS, starting from LEVELS steps into the
418 chain, and ending at the beginning, since template parms are listed
422 push_inline_template_parms_recursive (tree parmlist
, int levels
)
424 tree parms
= TREE_VALUE (parmlist
);
428 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
430 ++processing_template_decl
;
431 current_template_parms
432 = tree_cons (size_int (processing_template_decl
),
433 parms
, current_template_parms
);
434 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
436 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
438 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
440 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
442 if (error_operand_p (parm
))
445 gcc_assert (DECL_P (parm
));
447 switch (TREE_CODE (parm
))
455 /* Push the CONST_DECL. */
456 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
465 /* Restore the template parameter context for a member template, a
466 friend template defined in a class definition, or a non-template
467 member of template class. */
470 maybe_begin_member_template_processing (tree decl
)
474 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
478 tree ctx
= DECL_CONTEXT (decl
);
479 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
480 /* Disregard full specializations (c++/60999). */
481 && uses_template_parms (ctx
)
482 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
485 if (inline_needs_template_parms (decl
, nsdmi
))
487 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
488 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
490 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
493 parms
= TREE_CHAIN (parms
);
496 push_inline_template_parms_recursive (parms
, levels
);
499 /* Remember how many levels of template parameters we pushed so that
500 we can pop them later. */
501 inline_parm_levels
.safe_push (levels
);
504 /* Undo the effects of maybe_begin_member_template_processing. */
507 maybe_end_member_template_processing (void)
512 if (inline_parm_levels
.length () == 0)
515 last
= inline_parm_levels
.pop ();
516 for (i
= 0; i
< last
; ++i
)
518 --processing_template_decl
;
519 current_template_parms
= TREE_CHAIN (current_template_parms
);
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. */
528 add_to_template_args (tree args
, tree extra_args
)
535 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
538 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
539 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
541 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
542 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
544 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
545 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
550 /* Like add_to_template_args, but only the outermost ARGS are added to
551 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
552 (EXTRA_ARGS) levels are added. This function is used to combine
553 the template arguments from a partial instantiation with the
554 template arguments used to attain the full instantiation from the
555 partial instantiation. */
558 add_outermost_template_args (tree args
, tree extra_args
)
562 /* If there are more levels of EXTRA_ARGS than there are ARGS,
563 something very fishy is going on. */
564 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
566 /* If *all* the new arguments will be the EXTRA_ARGS, just return
568 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
571 /* For the moment, we make ARGS look like it contains fewer levels. */
572 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
574 new_args
= add_to_template_args (args
, extra_args
);
576 /* Now, we restore ARGS to its full dimensions. */
577 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
582 /* Return the N levels of innermost template arguments from the ARGS. */
585 get_innermost_template_args (tree args
, int n
)
593 /* If N is 1, just return the innermost set of template arguments. */
595 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
597 /* If we're not removing anything, just return the arguments we were
599 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
600 gcc_assert (extra_levels
>= 0);
601 if (extra_levels
== 0)
604 /* Make a new set of arguments, not containing the outer arguments. */
605 new_args
= make_tree_vec (n
);
606 for (i
= 1; i
<= n
; ++i
)
607 SET_TMPL_ARGS_LEVEL (new_args
, i
,
608 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
613 /* The inverse of get_innermost_template_args: Return all but the innermost
614 EXTRA_LEVELS levels of template arguments from the ARGS. */
617 strip_innermost_template_args (tree args
, int extra_levels
)
620 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
625 /* If N is 1, just return the outermost set of template arguments. */
627 return TMPL_ARGS_LEVEL (args
, 1);
629 /* If we're not removing anything, just return the arguments we were
631 gcc_assert (extra_levels
>= 0);
632 if (extra_levels
== 0)
635 /* Make a new set of arguments, not containing the inner arguments. */
636 new_args
= make_tree_vec (n
);
637 for (i
= 1; i
<= n
; ++i
)
638 SET_TMPL_ARGS_LEVEL (new_args
, i
,
639 TMPL_ARGS_LEVEL (args
, i
));
644 /* We've got a template header coming up; push to a new level for storing
648 begin_template_parm_list (void)
650 /* We use a non-tag-transparent scope here, which causes pushtag to
651 put tags in this scope, rather than in the enclosing class or
652 namespace scope. This is the right thing, since we want
653 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
654 global template class, push_template_decl handles putting the
655 TEMPLATE_DECL into top-level scope. For a nested template class,
658 template <class T> struct S1 {
659 template <class T> struct S2 {};
662 pushtag contains special code to insert the TEMPLATE_DECL for S2
663 at the right scope. */
664 begin_scope (sk_template_parms
, NULL
);
665 ++processing_template_decl
;
666 ++processing_template_parmlist
;
667 note_template_header (0);
669 /* Add a dummy parameter level while we process the parameter list. */
670 current_template_parms
671 = tree_cons (size_int (processing_template_decl
),
673 current_template_parms
);
676 /* This routine is called when a specialization is declared. If it is
677 invalid to declare a specialization here, an error is reported and
678 false is returned, otherwise this routine will return true. */
681 check_specialization_scope (void)
683 tree scope
= current_scope ();
687 An explicit specialization shall be declared in the namespace of
688 which the template is a member, or, for member templates, in the
689 namespace of which the enclosing class or enclosing class
690 template is a member. An explicit specialization of a member
691 function, member class or static data member of a class template
692 shall be declared in the namespace of which the class template
694 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
696 error ("explicit specialization in non-namespace scope %qD", scope
);
702 In an explicit specialization declaration for a member of a class
703 template or a member template that appears in namespace scope,
704 the member template and some of its enclosing class templates may
705 remain unspecialized, except that the declaration shall not
706 explicitly specialize a class member template if its enclosing
707 class templates are not explicitly specialized as well. */
708 if (current_template_parms
)
710 error ("enclosing class templates are not explicitly specialized");
717 /* We've just seen template <>. */
720 begin_specialization (void)
722 begin_scope (sk_template_spec
, NULL
);
723 note_template_header (1);
724 return check_specialization_scope ();
727 /* Called at then end of processing a declaration preceded by
731 end_specialization (void)
734 reset_specialization ();
737 /* Any template <>'s that we have seen thus far are not referring to a
738 function specialization. */
741 reset_specialization (void)
743 processing_specialization
= 0;
744 template_header_count
= 0;
747 /* We've just seen a template header. If SPECIALIZATION is nonzero,
748 it was of the form template <>. */
751 note_template_header (int specialization
)
753 processing_specialization
= specialization
;
754 template_header_count
++;
757 /* We're beginning an explicit instantiation. */
760 begin_explicit_instantiation (void)
762 gcc_assert (!processing_explicit_instantiation
);
763 processing_explicit_instantiation
= true;
768 end_explicit_instantiation (void)
770 gcc_assert (processing_explicit_instantiation
);
771 processing_explicit_instantiation
= false;
774 /* An explicit specialization or partial specialization of TMPL is being
775 declared. Check that the namespace in which the specialization is
776 occurring is permissible. Returns false iff it is invalid to
777 specialize TMPL in the current namespace. */
780 check_specialization_namespace (tree tmpl
)
782 tree tpl_ns
= decl_namespace_context (tmpl
);
786 An explicit specialization shall be declared in a namespace enclosing the
787 specialized template. An explicit specialization whose declarator-id is
788 not qualified shall be declared in the nearest enclosing namespace of the
789 template, or, if the namespace is inline (7.3.1), any namespace from its
790 enclosing namespace set. */
791 if (current_scope() != DECL_CONTEXT (tmpl
)
792 && !at_namespace_scope_p ())
794 error ("specialization of %qD must appear at namespace scope", tmpl
);
798 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
799 /* Same or enclosing namespace. */
803 if (permerror (input_location
,
804 "specialization of %qD in different namespace", tmpl
))
805 inform (DECL_SOURCE_LOCATION (tmpl
),
806 " from definition of %q#D", tmpl
);
811 /* SPEC is an explicit instantiation. Check that it is valid to
812 perform this explicit instantiation in the current namespace. */
815 check_explicit_instantiation_namespace (tree spec
)
819 /* DR 275: An explicit instantiation shall appear in an enclosing
820 namespace of its template. */
821 ns
= decl_namespace_context (spec
);
822 if (!is_nested_namespace (current_namespace
, ns
))
823 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
824 "(which does not enclose namespace %qD)",
825 spec
, current_namespace
, ns
);
828 // Returns the type of a template specialization only if that
829 // specialization needs to be defined. Otherwise (e.g., if the type has
830 // already been defined), the function returns NULL_TREE.
832 maybe_new_partial_specialization (tree type
)
834 // An implicit instantiation of an incomplete type implies
835 // the definition of a new class template.
837 // template<typename T>
840 // template<typename T>
843 // Here, S<T*> is an implicit instantiation of S whose type
845 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
848 // It can also be the case that TYPE is a completed specialization.
849 // Continuing the previous example, suppose we also declare:
851 // template<typename T>
852 // requires Integral<T>
855 // Here, S<T*> refers to the specialization S<T*> defined
856 // above. However, we need to differentiate definitions because
857 // we intend to define a new partial specialization. In this case,
858 // we rely on the fact that the constraints are different for
859 // this declaration than that above.
861 // Note that we also get here for injected class names and
862 // late-parsed template definitions. We must ensure that we
863 // do not create new type declarations for those cases.
864 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
866 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
867 tree args
= CLASSTYPE_TI_ARGS (type
);
869 // If there are no template parameters, this cannot be a new
870 // partial template specializtion?
871 if (!current_template_parms
)
874 // The injected-class-name is not a new partial specialization.
875 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
878 // If the constraints are not the same as those of the primary
879 // then, we can probably create a new specialization.
880 tree type_constr
= current_template_constraints ();
882 if (type
== TREE_TYPE (tmpl
))
884 tree main_constr
= get_constraints (tmpl
);
885 if (equivalent_constraints (type_constr
, main_constr
))
889 // Also, if there's a pre-existing specialization with matching
890 // constraints, then this also isn't new.
891 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
894 tree spec_tmpl
= TREE_VALUE (specs
);
895 tree spec_args
= TREE_PURPOSE (specs
);
896 tree spec_constr
= get_constraints (spec_tmpl
);
897 if (comp_template_args (args
, spec_args
)
898 && equivalent_constraints (type_constr
, spec_constr
))
900 specs
= TREE_CHAIN (specs
);
903 // Create a new type node (and corresponding type decl)
904 // for the newly declared specialization.
905 tree t
= make_class_type (TREE_CODE (type
));
906 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
907 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
909 /* We only need a separate type node for storing the definition of this
910 partial specialization; uses of S<T*> are unconstrained, so all are
911 equivalent. So keep TYPE_CANONICAL the same. */
912 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
914 // Build the corresponding type decl.
915 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
916 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
917 DECL_SOURCE_LOCATION (d
) = input_location
;
925 /* The TYPE is being declared. If it is a template type, that means it
926 is a partial specialization. Do appropriate error-checking. */
929 maybe_process_partial_specialization (tree type
)
933 if (type
== error_mark_node
)
934 return error_mark_node
;
936 /* A lambda that appears in specialization context is not itself a
938 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
941 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
943 error ("name of class shadows template template parameter %qD",
945 return error_mark_node
;
948 context
= TYPE_CONTEXT (type
);
950 if (TYPE_ALIAS_P (type
))
952 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
954 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
955 error ("specialization of alias template %qD",
956 TI_TEMPLATE (tinfo
));
958 error ("explicit specialization of non-template %qT", type
);
959 return error_mark_node
;
961 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
963 /* This is for ordinary explicit specialization and partial
964 specialization of a template class such as:
966 template <> class C<int>;
970 template <class T> class C<T*>;
972 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
974 if (tree t
= maybe_new_partial_specialization (type
))
976 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
977 && !at_namespace_scope_p ())
978 return error_mark_node
;
979 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
980 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
981 if (processing_template_decl
)
983 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
984 if (decl
== error_mark_node
)
985 return error_mark_node
;
986 return TREE_TYPE (decl
);
989 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
990 error ("specialization of %qT after instantiation", type
);
991 else if (errorcount
&& !processing_specialization
992 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
993 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
994 /* Trying to define a specialization either without a template<> header
995 or in an inappropriate place. We've already given an error, so just
996 bail now so we don't actually define the specialization. */
997 return error_mark_node
;
999 else if (CLASS_TYPE_P (type
)
1000 && !CLASSTYPE_USE_TEMPLATE (type
)
1001 && CLASSTYPE_TEMPLATE_INFO (type
)
1002 && context
&& CLASS_TYPE_P (context
)
1003 && CLASSTYPE_TEMPLATE_INFO (context
))
1005 /* This is for an explicit specialization of member class
1006 template according to [temp.expl.spec/18]:
1008 template <> template <class U> class C<int>::D;
1010 The context `C<int>' must be an implicit instantiation.
1011 Otherwise this is just a member class template declared
1014 template <> class C<int> { template <class U> class D; };
1015 template <> template <class U> class C<int>::D;
1017 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1018 while in the second case, `C<int>::D' is a primary template
1019 and `C<T>::D' may not exist. */
1021 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1022 && !COMPLETE_TYPE_P (type
))
1025 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1027 if (current_namespace
1028 != decl_namespace_context (tmpl
))
1030 permerror (input_location
,
1031 "specializing %q#T in different namespace", type
);
1032 permerror (DECL_SOURCE_LOCATION (tmpl
),
1033 " from definition of %q#D", tmpl
);
1036 /* Check for invalid specialization after instantiation:
1038 template <> template <> class C<int>::D<int>;
1039 template <> template <class U> class C<int>::D; */
1041 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1042 t
; t
= TREE_CHAIN (t
))
1044 tree inst
= TREE_VALUE (t
);
1045 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1046 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1048 /* We already have a full specialization of this partial
1049 instantiation, or a full specialization has been
1050 looked up but not instantiated. Reassign it to the
1051 new member specialization template. */
1055 elt
.tmpl
= most_general_template (tmpl
);
1056 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1059 type_specializations
->remove_elt (&elt
);
1062 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1065 = type_specializations
->find_slot (&elt
, INSERT
);
1066 entry
= ggc_alloc
<spec_entry
> ();
1071 /* But if we've had an implicit instantiation, that's a
1072 problem ([temp.expl.spec]/6). */
1073 error ("specialization %qT after instantiation %qT",
1077 /* Mark TYPE as a specialization. And as a result, we only
1078 have one level of template argument for the innermost
1080 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1081 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1082 CLASSTYPE_TI_ARGS (type
)
1083 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1086 else if (processing_specialization
)
1088 /* Someday C++0x may allow for enum template specialization. */
1089 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1090 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1091 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1092 "of %qD not allowed by ISO C++", type
);
1095 error ("explicit specialization of non-template %qT", type
);
1096 return error_mark_node
;
1103 /* Returns nonzero if we can optimize the retrieval of specializations
1104 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1105 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1108 optimize_specialization_lookup_p (tree tmpl
)
1110 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1111 && DECL_CLASS_SCOPE_P (tmpl
)
1112 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1114 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1115 /* The optimized lookup depends on the fact that the
1116 template arguments for the member function template apply
1117 purely to the containing class, which is not true if the
1118 containing class is an explicit or partial
1120 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1121 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1122 && !DECL_CONV_FN_P (tmpl
)
1123 /* It is possible to have a template that is not a member
1124 template and is not a member of a template class:
1126 template <typename T>
1127 struct S { friend A::f(); };
1129 Here, the friend function is a template, but the context does
1130 not have template information. The optimized lookup relies
1131 on having ARGS be the template arguments for both the class
1132 and the function template. */
1133 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1136 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1137 gone through coerce_template_parms by now. */
1140 verify_unstripped_args_1 (tree inner
)
1142 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1144 tree arg
= TREE_VEC_ELT (inner
, i
);
1145 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1147 else if (TYPE_P (arg
))
1148 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1149 else if (ARGUMENT_PACK_P (arg
))
1150 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1151 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1152 /* Allow typedefs on the type of a non-type argument, since a
1153 parameter can have them. */;
1155 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1160 verify_unstripped_args (tree args
)
1162 ++processing_template_decl
;
1163 if (!any_dependent_template_arguments_p (args
))
1164 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1165 --processing_template_decl
;
1168 /* Retrieve the specialization (in the sense of [temp.spec] - a
1169 specialization is either an instantiation or an explicit
1170 specialization) of TMPL for the given template ARGS. If there is
1171 no such specialization, return NULL_TREE. The ARGS are a vector of
1172 arguments, or a vector of vectors of arguments, in the case of
1173 templates with more than one level of parameters.
1175 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1176 then we search for a partial specialization matching ARGS. This
1177 parameter is ignored if TMPL is not a class template.
1179 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1180 result is a NONTYPE_ARGUMENT_PACK. */
1183 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1185 if (tmpl
== NULL_TREE
)
1188 if (args
== error_mark_node
)
1191 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1192 || TREE_CODE (tmpl
) == FIELD_DECL
);
1194 /* There should be as many levels of arguments as there are
1195 levels of parameters. */
1196 gcc_assert (TMPL_ARGS_DEPTH (args
)
1197 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1198 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1199 : template_class_depth (DECL_CONTEXT (tmpl
))));
1202 verify_unstripped_args (args
);
1204 /* Lambda functions in templates aren't instantiated normally, but through
1205 tsubst_lambda_expr. */
1206 if (lambda_fn_in_template_p (tmpl
))
1209 if (optimize_specialization_lookup_p (tmpl
))
1211 /* The template arguments actually apply to the containing
1212 class. Find the class specialization with those
1214 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1215 tree class_specialization
1216 = retrieve_specialization (class_template
, args
, 0);
1217 if (!class_specialization
)
1220 /* Find the instance of TMPL. */
1221 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1222 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1225 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1226 /* using-declarations can add base methods to the method vec,
1227 and we don't want those here. */
1228 && DECL_CONTEXT (fn
) == class_specialization
)
1237 hash_table
<spec_hasher
> *specializations
;
1241 elt
.spec
= NULL_TREE
;
1243 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1244 specializations
= type_specializations
;
1246 specializations
= decl_specializations
;
1249 hash
= spec_hasher::hash (&elt
);
1250 found
= specializations
->find_with_hash (&elt
, hash
);
1258 /* Like retrieve_specialization, but for local declarations. */
1261 retrieve_local_specialization (tree tmpl
)
1263 if (local_specializations
== NULL
)
1266 tree
*slot
= local_specializations
->get (tmpl
);
1267 return slot
? *slot
: NULL_TREE
;
1270 /* Returns nonzero iff DECL is a specialization of TMPL. */
1273 is_specialization_of (tree decl
, tree tmpl
)
1277 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1281 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1287 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1289 for (t
= TREE_TYPE (decl
);
1291 t
= CLASSTYPE_USE_TEMPLATE (t
)
1292 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1293 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1300 /* Returns nonzero iff DECL is a specialization of friend declaration
1301 FRIEND_DECL according to [temp.friend]. */
1304 is_specialization_of_friend (tree decl
, tree friend_decl
)
1306 bool need_template
= true;
1309 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1310 || TREE_CODE (decl
) == TYPE_DECL
);
1312 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1313 of a template class, we want to check if DECL is a specialization
1315 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1316 && DECL_TEMPLATE_INFO (friend_decl
)
1317 && !DECL_USE_TEMPLATE (friend_decl
))
1319 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1320 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1321 need_template
= false;
1323 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1324 && !PRIMARY_TEMPLATE_P (friend_decl
))
1325 need_template
= false;
1327 /* There is nothing to do if this is not a template friend. */
1328 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1331 if (is_specialization_of (decl
, friend_decl
))
1335 A member of a class template may be declared to be a friend of a
1336 non-template class. In this case, the corresponding member of
1337 every specialization of the class template is a friend of the
1338 class granting friendship.
1340 For example, given a template friend declaration
1342 template <class T> friend void A<T>::f();
1344 the member function below is considered a friend
1346 template <> struct A<int> {
1350 For this type of template friend, TEMPLATE_DEPTH below will be
1351 nonzero. To determine if DECL is a friend of FRIEND, we first
1352 check if the enclosing class is a specialization of another. */
1354 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1356 && DECL_CLASS_SCOPE_P (decl
)
1357 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1358 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1360 /* Next, we check the members themselves. In order to handle
1361 a few tricky cases, such as when FRIEND_DECL's are
1363 template <class T> friend void A<T>::g(T t);
1364 template <class T> template <T t> friend void A<T>::h();
1368 void A<int>::g(int);
1369 template <int> void A<int>::h();
1371 we need to figure out ARGS, the template arguments from
1372 the context of DECL. This is required for template substitution
1373 of `T' in the function parameter of `g' and template parameter
1374 of `h' in the above examples. Here ARGS corresponds to `int'. */
1376 tree context
= DECL_CONTEXT (decl
);
1377 tree args
= NULL_TREE
;
1378 int current_depth
= 0;
1380 while (current_depth
< template_depth
)
1382 if (CLASSTYPE_TEMPLATE_INFO (context
))
1384 if (current_depth
== 0)
1385 args
= TYPE_TI_ARGS (context
);
1387 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1390 context
= TYPE_CONTEXT (context
);
1393 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1398 tree friend_args_type
;
1399 tree decl_args_type
;
1401 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 is_template
= DECL_TEMPLATE_INFO (decl
)
1404 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1405 if (need_template
^ is_template
)
1407 else if (is_template
)
1409 /* If both are templates, check template parameter list. */
1411 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1413 if (!comp_template_parms
1414 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1418 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1421 decl_type
= TREE_TYPE (decl
);
1423 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1424 tf_none
, NULL_TREE
);
1425 if (friend_type
== error_mark_node
)
1428 /* Check if return types match. */
1429 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1432 /* Check if function parameter types match, ignoring the
1433 `this' parameter. */
1434 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1435 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1437 friend_args_type
= TREE_CHAIN (friend_args_type
);
1438 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1439 decl_args_type
= TREE_CHAIN (decl_args_type
);
1441 return compparms (decl_args_type
, friend_args_type
);
1445 /* DECL is a TYPE_DECL */
1447 tree decl_type
= TREE_TYPE (decl
);
1449 /* Make sure that both DECL and FRIEND_DECL are templates or
1452 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1453 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1455 if (need_template
^ is_template
)
1457 else if (is_template
)
1460 /* If both are templates, check the name of the two
1461 TEMPLATE_DECL's first because is_friend didn't. */
1462 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1463 != DECL_NAME (friend_decl
))
1466 /* Now check template parameter list. */
1468 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1470 return comp_template_parms
1471 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1475 return (DECL_NAME (decl
)
1476 == DECL_NAME (friend_decl
));
1482 /* Register the specialization SPEC as a specialization of TMPL with
1483 the indicated ARGS. IS_FRIEND indicates whether the specialization
1484 is actually just a friend declaration. ATTRLIST is the list of
1485 attributes that the specialization is declared with or NULL when
1486 it isn't. Returns SPEC, or an equivalent prior declaration, if
1489 We also store instantiations of field packs in the hash table, even
1490 though they are not themselves templates, to make lookup easier. */
1493 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1497 spec_entry
**slot
= NULL
;
1500 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1501 || (TREE_CODE (tmpl
) == FIELD_DECL
1502 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1504 if (TREE_CODE (spec
) == FUNCTION_DECL
1505 && uses_template_parms (DECL_TI_ARGS (spec
)))
1506 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1507 register it; we want the corresponding TEMPLATE_DECL instead.
1508 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1509 the more obvious `uses_template_parms (spec)' to avoid problems
1510 with default function arguments. In particular, given
1511 something like this:
1513 template <class T> void f(T t1, T t = T())
1515 the default argument expression is not substituted for in an
1516 instantiation unless and until it is actually needed. */
1519 if (optimize_specialization_lookup_p (tmpl
))
1520 /* We don't put these specializations in the hash table, but we might
1521 want to give an error about a mismatch. */
1522 fn
= retrieve_specialization (tmpl
, args
, 0);
1530 hash
= spec_hasher::hash (&elt
);
1533 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1535 fn
= ((spec_entry
*) *slot
)->spec
;
1540 /* We can sometimes try to re-register a specialization that we've
1541 already got. In particular, regenerate_decl_from_template calls
1542 duplicate_decls which will update the specialization list. But,
1543 we'll still get called again here anyhow. It's more convenient
1544 to simply allow this than to try to prevent it. */
1547 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1549 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1551 if (DECL_ODR_USED (fn
)
1552 || DECL_EXPLICIT_INSTANTIATION (fn
))
1554 error ("specialization of %qD after instantiation",
1556 return error_mark_node
;
1561 /* This situation should occur only if the first
1562 specialization is an implicit instantiation, the
1563 second is an explicit specialization, and the
1564 implicit instantiation has not yet been used. That
1565 situation can occur if we have implicitly
1566 instantiated a member function and then specialized
1569 We can also wind up here if a friend declaration that
1570 looked like an instantiation turns out to be a
1573 template <class T> void foo(T);
1574 class S { friend void foo<>(int) };
1575 template <> void foo(int);
1577 We transform the existing DECL in place so that any
1578 pointers to it become pointers to the updated
1581 If there was a definition for the template, but not
1582 for the specialization, we want this to look as if
1583 there were no definition, and vice versa. */
1584 DECL_INITIAL (fn
) = NULL_TREE
;
1585 duplicate_decls (spec
, fn
, is_friend
);
1586 /* The call to duplicate_decls will have applied
1589 An explicit specialization of a function template
1590 is inline only if it is explicitly declared to be,
1591 and independently of whether its function template
1594 to the primary function; now copy the inline bits to
1595 the various clones. */
1596 FOR_EACH_CLONE (clone
, fn
)
1598 DECL_DECLARED_INLINE_P (clone
)
1599 = DECL_DECLARED_INLINE_P (fn
);
1600 DECL_SOURCE_LOCATION (clone
)
1601 = DECL_SOURCE_LOCATION (fn
);
1602 DECL_DELETED_FN (clone
)
1603 = DECL_DELETED_FN (fn
);
1605 check_specialization_namespace (tmpl
);
1610 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1612 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1613 if (dd
== error_mark_node
)
1614 /* We've already complained in duplicate_decls. */
1615 return error_mark_node
;
1617 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1618 /* Dup decl failed, but this is a new definition. Set the
1619 line number so any errors match this new
1621 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1627 return duplicate_decls (spec
, fn
, is_friend
);
1629 /* A specialization must be declared in the same namespace as the
1630 template it is specializing. */
1631 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1632 && !check_specialization_namespace (tmpl
))
1633 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1635 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1637 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1638 gcc_assert (tmpl
&& args
&& spec
);
1641 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1642 && PRIMARY_TEMPLATE_P (tmpl
)
1643 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1644 || variable_template_p (tmpl
))
1645 /* If TMPL is a forward declaration of a template function, keep a list
1646 of all specializations in case we need to reassign them to a friend
1647 template later in tsubst_friend_function.
1649 Also keep a list of all variable template instantiations so that
1650 process_partial_specialization can check whether a later partial
1651 specialization would have used it. */
1652 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1653 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1659 /* Returns true iff two spec_entry nodes are equivalent. */
1661 int comparing_specializations
;
1664 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1668 ++comparing_specializations
;
1669 equal
= (e1
->tmpl
== e2
->tmpl
1670 && comp_template_args (e1
->args
, e2
->args
));
1671 if (equal
&& flag_concepts
1672 /* tmpl could be a FIELD_DECL for a capture pack. */
1673 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1674 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1675 && uses_template_parms (e1
->args
))
1677 /* Partial specializations of a variable template can be distinguished by
1679 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1680 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1681 equal
= equivalent_constraints (c1
, c2
);
1683 --comparing_specializations
;
1688 /* Returns a hash for a template TMPL and template arguments ARGS. */
1691 hash_tmpl_and_args (tree tmpl
, tree args
)
1693 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1694 return iterative_hash_template_arg (args
, val
);
1697 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1701 spec_hasher::hash (spec_entry
*e
)
1703 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1706 /* Recursively calculate a hash value for a template argument ARG, for use
1707 in the hash tables of template specializations. */
1710 iterative_hash_template_arg (tree arg
, hashval_t val
)
1712 unsigned HOST_WIDE_INT i
;
1713 enum tree_code code
;
1716 if (arg
== NULL_TREE
)
1717 return iterative_hash_object (arg
, val
);
1722 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1725 code
= TREE_CODE (arg
);
1726 tclass
= TREE_CODE_CLASS (code
);
1728 val
= iterative_hash_object (code
, val
);
1735 case IDENTIFIER_NODE
:
1736 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1740 int i
, len
= TREE_VEC_LENGTH (arg
);
1741 for (i
= 0; i
< len
; ++i
)
1742 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1746 case TYPE_PACK_EXPANSION
:
1747 case EXPR_PACK_EXPANSION
:
1748 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1749 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1751 case TYPE_ARGUMENT_PACK
:
1752 case NONTYPE_ARGUMENT_PACK
:
1753 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1756 for (; arg
; arg
= TREE_CHAIN (arg
))
1757 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1761 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1762 val
= iterative_hash_template_arg (*iter
, val
);
1768 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1769 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1771 val
= iterative_hash_template_arg (field
, val
);
1772 val
= iterative_hash_template_arg (value
, val
);
1778 if (!DECL_ARTIFICIAL (arg
))
1780 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1781 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1783 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1786 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1789 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1790 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1792 case TEMPLATE_PARM_INDEX
:
1793 val
= iterative_hash_template_arg
1794 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1795 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1796 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1799 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1800 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1801 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1804 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1806 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1810 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1811 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1812 val
= iterative_hash_object (code
, val
);
1813 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1816 /* A lambda can't appear in a template arg, but don't crash on
1818 gcc_assert (seen_error ());
1822 case IMPLICIT_CONV_EXPR
:
1823 case STATIC_CAST_EXPR
:
1824 case REINTERPRET_CAST_EXPR
:
1825 case CONST_CAST_EXPR
:
1826 case DYNAMIC_CAST_EXPR
:
1828 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1829 /* Now hash operands as usual. */
1839 if (alias_template_specialization_p (arg
))
1841 // We want an alias specialization that survived strip_typedefs
1842 // to hash differently from its TYPE_CANONICAL, to avoid hash
1843 // collisions that compare as different in template_args_equal.
1844 // These could be dependent specializations that strip_typedefs
1845 // left alone, or untouched specializations because
1846 // coerce_template_parms returns the unconverted template
1847 // arguments if it sees incomplete argument packs.
1848 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1849 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1851 if (TYPE_CANONICAL (arg
))
1852 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1854 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1855 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1856 /* Otherwise just compare the types during lookup. */
1859 case tcc_declaration
:
1861 return iterative_hash_expr (arg
, val
);
1864 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1866 unsigned n
= cp_tree_operand_length (arg
);
1867 for (i
= 0; i
< n
; ++i
)
1868 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1876 /* Unregister the specialization SPEC as a specialization of TMPL.
1877 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1878 if the SPEC was listed as a specialization of TMPL.
1880 Note that SPEC has been ggc_freed, so we can't look inside it. */
1883 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1888 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1889 elt
.args
= TI_ARGS (tinfo
);
1890 elt
.spec
= NULL_TREE
;
1892 entry
= decl_specializations
->find (&elt
);
1895 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1896 gcc_assert (new_spec
!= NULL_TREE
);
1897 entry
->spec
= new_spec
;
1904 /* Like register_specialization, but for local declarations. We are
1905 registering SPEC, an instantiation of TMPL. */
1908 register_local_specialization (tree spec
, tree tmpl
)
1910 gcc_assert (tmpl
!= spec
);
1911 local_specializations
->put (tmpl
, spec
);
1914 /* TYPE is a class type. Returns true if TYPE is an explicitly
1915 specialized class. */
1918 explicit_class_specialization_p (tree type
)
1920 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1922 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1925 /* Print the list of functions at FNS, going through all the overloads
1926 for each element of the list. Alternatively, FNS can not be a
1927 TREE_LIST, in which case it will be printed together with all the
1930 MORE and *STR should respectively be FALSE and NULL when the function
1931 is called from the outside. They are used internally on recursive
1932 calls. print_candidates manages the two parameters and leaves NULL
1933 in *STR when it ends. */
1936 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1938 if (TREE_CODE (fns
) == TREE_LIST
)
1939 for (; fns
; fns
= TREE_CHAIN (fns
))
1940 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1942 for (lkp_iterator
iter (fns
); iter
;)
1947 const char *pfx
= *str
;
1951 pfx
= _("candidates are:");
1953 pfx
= _("candidate is:");
1954 *str
= get_spaces (pfx
);
1956 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1960 /* Print the list of candidate FNS in an error message. FNS can also
1961 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1964 print_candidates (tree fns
)
1967 print_candidates_1 (fns
, &str
);
1971 /* Get a (possibly) constrained template declaration for the
1972 purpose of ordering candidates. */
1974 get_template_for_ordering (tree list
)
1976 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1977 tree f
= TREE_VALUE (list
);
1978 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1979 return TI_TEMPLATE (ti
);
1983 /* Among candidates having the same signature, return the
1984 most constrained or NULL_TREE if there is no best candidate.
1985 If the signatures of candidates vary (e.g., template
1986 specialization vs. member function), then there can be no
1989 Note that we don't compare constraints on the functions
1990 themselves, but rather those of their templates. */
1992 most_constrained_function (tree candidates
)
1994 // Try to find the best candidate in a first pass.
1995 tree champ
= candidates
;
1996 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1998 int winner
= more_constrained (get_template_for_ordering (champ
),
1999 get_template_for_ordering (c
));
2001 champ
= c
; // The candidate is more constrained
2002 else if (winner
== 0)
2003 return NULL_TREE
; // Neither is more constrained
2006 // Verify that the champ is better than previous candidates.
2007 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2008 if (!more_constrained (get_template_for_ordering (champ
),
2009 get_template_for_ordering (c
)))
2017 /* Returns the template (one of the functions given by TEMPLATE_ID)
2018 which can be specialized to match the indicated DECL with the
2019 explicit template args given in TEMPLATE_ID. The DECL may be
2020 NULL_TREE if none is available. In that case, the functions in
2021 TEMPLATE_ID are non-members.
2023 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2024 specialization of a member template.
2026 The TEMPLATE_COUNT is the number of references to qualifying
2027 template classes that appeared in the name of the function. See
2028 check_explicit_specialization for a more accurate description.
2030 TSK indicates what kind of template declaration (if any) is being
2031 declared. TSK_TEMPLATE indicates that the declaration given by
2032 DECL, though a FUNCTION_DECL, has template parameters, and is
2033 therefore a template function.
2035 The template args (those explicitly specified and those deduced)
2036 are output in a newly created vector *TARGS_OUT.
2038 If it is impossible to determine the result, an error message is
2039 issued. The error_mark_node is returned to indicate failure. */
2042 determine_specialization (tree template_id
,
2045 int need_member_template
,
2051 tree explicit_targs
;
2052 tree candidates
= NULL_TREE
;
2054 /* A TREE_LIST of templates of which DECL may be a specialization.
2055 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2056 corresponding TREE_PURPOSE is the set of template arguments that,
2057 when used to instantiate the template, would produce a function
2058 with the signature of DECL. */
2059 tree templates
= NULL_TREE
;
2061 cp_binding_level
*b
;
2063 *targs_out
= NULL_TREE
;
2065 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2066 return error_mark_node
;
2068 /* We shouldn't be specializing a member template of an
2069 unspecialized class template; we already gave an error in
2070 check_specialization_scope, now avoid crashing. */
2072 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2073 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2075 gcc_assert (errorcount
);
2076 return error_mark_node
;
2079 fns
= TREE_OPERAND (template_id
, 0);
2080 explicit_targs
= TREE_OPERAND (template_id
, 1);
2082 if (fns
== error_mark_node
)
2083 return error_mark_node
;
2085 /* Check for baselinks. */
2086 if (BASELINK_P (fns
))
2087 fns
= BASELINK_FUNCTIONS (fns
);
2089 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2091 error ("%qD is not a function template", fns
);
2092 return error_mark_node
;
2094 else if (VAR_P (decl
) && !variable_template_p (fns
))
2096 error ("%qD is not a variable template", fns
);
2097 return error_mark_node
;
2100 /* Count the number of template headers specified for this
2103 for (b
= current_binding_level
;
2104 b
->kind
== sk_template_parms
;
2108 tree orig_fns
= fns
;
2110 if (variable_template_p (fns
))
2112 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2113 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2114 tf_warning_or_error
,
2115 /*req_all*/true, /*use_defarg*/true);
2116 if (targs
!= error_mark_node
)
2117 templates
= tree_cons (targs
, fns
, templates
);
2119 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2123 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2125 tree decl_arg_types
;
2129 /* In case of explicit specialization, we need to check if
2130 the number of template headers appearing in the specialization
2131 is correct. This is usually done in check_explicit_specialization,
2132 but the check done there cannot be exhaustive when specializing
2133 member functions. Consider the following code:
2135 template <> void A<int>::f(int);
2136 template <> template <> void A<int>::f(int);
2138 Assuming that A<int> is not itself an explicit specialization
2139 already, the first line specializes "f" which is a non-template
2140 member function, whilst the second line specializes "f" which
2141 is a template member function. So both lines are syntactically
2142 correct, and check_explicit_specialization does not reject
2145 Here, we can do better, as we are matching the specialization
2146 against the declarations. We count the number of template
2147 headers, and we check if they match TEMPLATE_COUNT + 1
2148 (TEMPLATE_COUNT is the number of qualifying template classes,
2149 plus there must be another header for the member template
2152 Notice that if header_count is zero, this is not a
2153 specialization but rather a template instantiation, so there
2154 is no check we can perform here. */
2155 if (header_count
&& header_count
!= template_count
+ 1)
2158 /* Check that the number of template arguments at the
2159 innermost level for DECL is the same as for FN. */
2160 if (current_binding_level
->kind
== sk_template_parms
2161 && !current_binding_level
->explicit_spec_p
2162 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2163 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2164 (current_template_parms
))))
2167 /* DECL might be a specialization of FN. */
2168 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2169 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2171 /* For a non-static member function, we need to make sure
2172 that the const qualification is the same. Since
2173 get_bindings does not try to merge the "this" parameter,
2174 we must do the comparison explicitly. */
2175 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2177 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2178 TREE_VALUE (decl_arg_types
)))
2181 /* And the ref-qualification. */
2182 if (type_memfn_rqual (TREE_TYPE (decl
))
2183 != type_memfn_rqual (TREE_TYPE (fn
)))
2187 /* Skip the "this" parameter and, for constructors of
2188 classes with virtual bases, the VTT parameter. A
2189 full specialization of a constructor will have a VTT
2190 parameter, but a template never will. */
2192 = skip_artificial_parms_for (decl
, decl_arg_types
);
2194 = skip_artificial_parms_for (fn
, fn_arg_types
);
2196 /* Function templates cannot be specializations; there are
2197 no partial specializations of functions. Therefore, if
2198 the type of DECL does not match FN, there is no
2201 Note that it should never be the case that we have both
2202 candidates added here, and for regular member functions
2204 if (tsk
== tsk_template
)
2206 if (compparms (fn_arg_types
, decl_arg_types
))
2207 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2211 /* See whether this function might be a specialization of this
2212 template. Suppress access control because we might be trying
2213 to make this specialization a friend, and we have already done
2214 access control for the declaration of the specialization. */
2215 push_deferring_access_checks (dk_no_check
);
2216 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2217 pop_deferring_access_checks ();
2220 /* We cannot deduce template arguments that when used to
2221 specialize TMPL will produce DECL. */
2224 if (uses_template_parms (targs
))
2225 /* We deduced something involving 'auto', which isn't a valid
2226 template argument. */
2229 /* Remove, from the set of candidates, all those functions
2230 whose constraints are not satisfied. */
2231 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2234 // Then, try to form the new function type.
2235 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2236 if (insttype
== error_mark_node
)
2239 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2240 if (!compparms (fn_arg_types
, decl_arg_types
))
2243 /* Save this template, and the arguments deduced. */
2244 templates
= tree_cons (targs
, fn
, templates
);
2246 else if (need_member_template
)
2247 /* FN is an ordinary member function, and we need a
2248 specialization of a member template. */
2250 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2251 /* We can get IDENTIFIER_NODEs here in certain erroneous
2254 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2255 /* This is just an ordinary non-member function. Nothing can
2256 be a specialization of that. */
2258 else if (DECL_ARTIFICIAL (fn
))
2259 /* Cannot specialize functions that are created implicitly. */
2263 tree decl_arg_types
;
2265 /* This is an ordinary member function. However, since
2266 we're here, we can assume its enclosing class is a
2267 template class. For example,
2269 template <typename T> struct S { void f(); };
2270 template <> void S<int>::f() {}
2272 Here, S<int>::f is a non-template, but S<int> is a
2273 template class. If FN has the same type as DECL, we
2274 might be in business. */
2276 if (!DECL_TEMPLATE_INFO (fn
))
2277 /* Its enclosing class is an explicit specialization
2278 of a template class. This is not a candidate. */
2281 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2282 TREE_TYPE (TREE_TYPE (fn
))))
2283 /* The return types differ. */
2286 /* Adjust the type of DECL in case FN is a static member. */
2287 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2288 if (DECL_STATIC_FUNCTION_P (fn
)
2289 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2290 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2292 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2296 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2297 && (type_memfn_rqual (TREE_TYPE (decl
))
2298 != type_memfn_rqual (TREE_TYPE (fn
))))
2301 // If the deduced arguments do not satisfy the constraints,
2302 // this is not a candidate.
2303 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2306 // Add the candidate.
2307 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2311 if (templates
&& TREE_CHAIN (templates
))
2317 It is possible for a specialization with a given function
2318 signature to be instantiated from more than one function
2319 template. In such cases, explicit specification of the
2320 template arguments must be used to uniquely identify the
2321 function template specialization being specialized.
2323 Note that here, there's no suggestion that we're supposed to
2324 determine which of the candidate templates is most
2325 specialized. However, we, also have:
2329 Partial ordering of overloaded function template
2330 declarations is used in the following contexts to select
2331 the function template to which a function template
2332 specialization refers:
2334 -- when an explicit specialization refers to a function
2337 So, we do use the partial ordering rules, at least for now.
2338 This extension can only serve to make invalid programs valid,
2339 so it's safe. And, there is strong anecdotal evidence that
2340 the committee intended the partial ordering rules to apply;
2341 the EDG front end has that behavior, and John Spicer claims
2342 that the committee simply forgot to delete the wording in
2343 [temp.expl.spec]. */
2344 tree tmpl
= most_specialized_instantiation (templates
);
2345 if (tmpl
!= error_mark_node
)
2348 TREE_CHAIN (templates
) = NULL_TREE
;
2352 // Concepts allows multiple declarations of member functions
2353 // with the same signature. Like above, we need to rely on
2354 // on the partial ordering of those candidates to determine which
2356 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2358 if (tree cand
= most_constrained_function (candidates
))
2361 TREE_CHAIN (cand
) = NULL_TREE
;
2365 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2367 error ("template-id %qD for %q+D does not match any template "
2368 "declaration", template_id
, decl
);
2369 if (header_count
&& header_count
!= template_count
+ 1)
2370 inform (input_location
, "saw %d %<template<>%>, need %d for "
2371 "specializing a member function template",
2372 header_count
, template_count
+ 1);
2374 print_candidates (orig_fns
);
2375 return error_mark_node
;
2377 else if ((templates
&& TREE_CHAIN (templates
))
2378 || (candidates
&& TREE_CHAIN (candidates
))
2379 || (templates
&& candidates
))
2381 error ("ambiguous template specialization %qD for %q+D",
2383 candidates
= chainon (candidates
, templates
);
2384 print_candidates (candidates
);
2385 return error_mark_node
;
2388 /* We have one, and exactly one, match. */
2391 tree fn
= TREE_VALUE (candidates
);
2392 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2394 // Propagate the candidate's constraints to the declaration.
2395 set_constraints (decl
, get_constraints (fn
));
2397 /* DECL is a re-declaration or partial instantiation of a template
2399 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2401 /* It was a specialization of an ordinary member function in a
2403 return DECL_TI_TEMPLATE (fn
);
2406 /* It was a specialization of a template. */
2407 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2408 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2410 *targs_out
= copy_node (targs
);
2411 SET_TMPL_ARGS_LEVEL (*targs_out
,
2412 TMPL_ARGS_DEPTH (*targs_out
),
2413 TREE_PURPOSE (templates
));
2416 *targs_out
= TREE_PURPOSE (templates
);
2417 return TREE_VALUE (templates
);
2420 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2421 but with the default argument values filled in from those in the
2425 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2428 tree new_spec_types
;
2433 if (spec_types
== void_list_node
)
2434 return void_list_node
;
2436 /* Substitute into the rest of the list. */
2438 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2439 TREE_CHAIN (tmpl_types
));
2441 /* Add the default argument for this parameter. */
2442 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2443 TREE_VALUE (spec_types
),
2447 /* DECL is an explicit specialization. Replicate default arguments
2448 from the template it specializes. (That way, code like:
2450 template <class T> void f(T = 3);
2451 template <> void f(double);
2454 works, as required.) An alternative approach would be to look up
2455 the correct default arguments at the call-site, but this approach
2456 is consistent with how implicit instantiations are handled. */
2459 copy_default_args_to_explicit_spec (tree decl
)
2464 tree new_spec_types
;
2468 tree object_type
= NULL_TREE
;
2469 tree in_charge
= NULL_TREE
;
2470 tree vtt
= NULL_TREE
;
2472 /* See if there's anything we need to do. */
2473 tmpl
= DECL_TI_TEMPLATE (decl
);
2474 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2475 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2476 if (TREE_PURPOSE (t
))
2481 old_type
= TREE_TYPE (decl
);
2482 spec_types
= TYPE_ARG_TYPES (old_type
);
2484 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2486 /* Remove the this pointer, but remember the object's type for
2488 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2489 spec_types
= TREE_CHAIN (spec_types
);
2490 tmpl_types
= TREE_CHAIN (tmpl_types
);
2492 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2494 /* DECL may contain more parameters than TMPL due to the extra
2495 in-charge parameter in constructors and destructors. */
2496 in_charge
= spec_types
;
2497 spec_types
= TREE_CHAIN (spec_types
);
2499 if (DECL_HAS_VTT_PARM_P (decl
))
2502 spec_types
= TREE_CHAIN (spec_types
);
2506 /* Compute the merged default arguments. */
2508 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2510 /* Compute the new FUNCTION_TYPE. */
2514 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2519 /* Put the in-charge parameter back. */
2520 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2521 TREE_VALUE (in_charge
),
2524 new_type
= build_method_type_directly (object_type
,
2525 TREE_TYPE (old_type
),
2529 new_type
= build_function_type (TREE_TYPE (old_type
),
2531 new_type
= cp_build_type_attribute_variant (new_type
,
2532 TYPE_ATTRIBUTES (old_type
));
2533 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2535 TREE_TYPE (decl
) = new_type
;
2538 /* Return the number of template headers we expect to see for a definition
2539 or specialization of CTYPE or one of its non-template members. */
2542 num_template_headers_for_class (tree ctype
)
2544 int num_templates
= 0;
2546 while (ctype
&& CLASS_TYPE_P (ctype
))
2548 /* You're supposed to have one `template <...>' for every
2549 template class, but you don't need one for a full
2550 specialization. For example:
2552 template <class T> struct S{};
2553 template <> struct S<int> { void f(); };
2554 void S<int>::f () {}
2556 is correct; there shouldn't be a `template <>' for the
2557 definition of `S<int>::f'. */
2558 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2559 /* If CTYPE does not have template information of any
2560 kind, then it is not a template, nor is it nested
2561 within a template. */
2563 if (explicit_class_specialization_p (ctype
))
2565 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2568 ctype
= TYPE_CONTEXT (ctype
);
2571 return num_templates
;
2574 /* Do a simple sanity check on the template headers that precede the
2575 variable declaration DECL. */
2578 check_template_variable (tree decl
)
2580 tree ctx
= CP_DECL_CONTEXT (decl
);
2581 int wanted
= num_template_headers_for_class (ctx
);
2582 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2583 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2585 if (cxx_dialect
< cxx14
)
2586 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2587 "variable templates only available with "
2588 "-std=c++14 or -std=gnu++14");
2590 // Namespace-scope variable templates should have a template header.
2593 if (template_header_count
> wanted
)
2595 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2596 "too many template headers for %qD "
2599 if (warned
&& CLASS_TYPE_P (ctx
)
2600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2601 inform (DECL_SOURCE_LOCATION (decl
),
2602 "members of an explicitly specialized class are defined "
2603 "without a template header");
2607 /* An explicit specialization whose declarator-id or class-head-name is not
2608 qualified shall be declared in the nearest enclosing namespace of the
2609 template, or, if the namespace is inline (7.3.1), any namespace from its
2610 enclosing namespace set.
2612 If the name declared in the explicit instantiation is an unqualified name,
2613 the explicit instantiation shall appear in the namespace where its template
2614 is declared or, if that namespace is inline (7.3.1), any namespace from its
2615 enclosing namespace set. */
2618 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2620 tree tmpl
= most_general_template (t
);
2621 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2622 && !is_nested_namespace (current_namespace
,
2623 CP_DECL_CONTEXT (tmpl
), true))
2625 if (processing_specialization
)
2626 permerror (loc
, "explicit specialization of %qD outside its "
2627 "namespace must use a nested-name-specifier", tmpl
);
2628 else if (processing_explicit_instantiation
2629 && cxx_dialect
>= cxx11
)
2630 /* This was allowed in C++98, so only pedwarn. */
2631 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2632 "outside its namespace must use a nested-name-"
2637 /* Warn for a template specialization SPEC that is missing some of a set
2638 of function or type attributes that the template TEMPL is declared with.
2639 ATTRLIST is a list of additional attributes that SPEC should be taken
2640 to ultimately be declared with. */
2643 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2645 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2646 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2648 if (TREE_CODE (tmpl
) != FUNCTION_DECL
)
2651 /* Avoid warning if either declaration or its type is deprecated. */
2652 if (TREE_DEPRECATED (tmpl
)
2653 || TREE_DEPRECATED (spec
))
2656 tree tmpl_type
= TREE_TYPE (tmpl
);
2657 tree spec_type
= TREE_TYPE (spec
);
2659 if (TREE_DEPRECATED (tmpl_type
)
2660 || TREE_DEPRECATED (spec_type
)
2661 || TREE_DEPRECATED (TREE_TYPE (tmpl_type
))
2662 || TREE_DEPRECATED (TREE_TYPE (spec_type
)))
2665 tree tmpl_attrs
[] = { DECL_ATTRIBUTES (tmpl
), TYPE_ATTRIBUTES (tmpl_type
) };
2666 tree spec_attrs
[] = { DECL_ATTRIBUTES (spec
), TYPE_ATTRIBUTES (spec_type
) };
2669 spec_attrs
[0] = attrlist
;
2670 else if (!spec_attrs
[1])
2671 spec_attrs
[1] = attrlist
;
2673 /* Avoid warning if the primary has no attributes. */
2674 if (!tmpl_attrs
[0] && !tmpl_attrs
[1])
2677 /* Avoid warning if either declaration contains an attribute on
2678 the white list below. */
2679 const char* const whitelist
[] = {
2683 for (unsigned i
= 0; i
!= 2; ++i
)
2684 for (unsigned j
= 0; j
!= sizeof whitelist
/ sizeof *whitelist
; ++j
)
2685 if (lookup_attribute (whitelist
[j
], tmpl_attrs
[i
])
2686 || lookup_attribute (whitelist
[j
], spec_attrs
[i
]))
2689 /* Avoid warning if the difference between the primary and
2690 the specialization is not in one of the attributes below. */
2691 const char* const blacklist
[] = {
2692 "alloc_align", "alloc_size", "assume_aligned", "format",
2693 "format_arg", "malloc", "nonnull"
2696 /* Put together a list of the black listed attributes that the primary
2697 template is declared with that the specialization is not, in case
2698 it's not apparent from the most recent declaration of the primary. */
2699 unsigned nattrs
= 0;
2702 for (unsigned i
= 0; i
!= sizeof blacklist
/ sizeof *blacklist
; ++i
)
2704 for (unsigned j
= 0; j
!= 2; ++j
)
2706 if (!lookup_attribute (blacklist
[i
], tmpl_attrs
[j
]))
2709 for (unsigned k
= 0; k
!= 1 + !!spec_attrs
[1]; ++k
)
2711 if (lookup_attribute (blacklist
[i
], spec_attrs
[k
]))
2715 pp_string (&str
, ", ");
2716 pp_begin_quote (&str
, pp_show_color (global_dc
->printer
));
2717 pp_string (&str
, blacklist
[i
]);
2718 pp_end_quote (&str
, pp_show_color (global_dc
->printer
));
2727 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2728 "explicit specialization %q#D may be missing attributes",
2730 inform (DECL_SOURCE_LOCATION (tmpl
),
2732 ? G_("missing primary template attributes %s")
2733 : G_("missing primary template attribute %s"),
2734 pp_formatted_text (&str
));
2737 /* Check to see if the function just declared, as indicated in
2738 DECLARATOR, and in DECL, is a specialization of a function
2739 template. We may also discover that the declaration is an explicit
2740 instantiation at this point.
2742 Returns DECL, or an equivalent declaration that should be used
2743 instead if all goes well. Issues an error message if something is
2744 amiss. Returns error_mark_node if the error is not easily
2747 FLAGS is a bitmask consisting of the following flags:
2749 2: The function has a definition.
2750 4: The function is a friend.
2752 The TEMPLATE_COUNT is the number of references to qualifying
2753 template classes that appeared in the name of the function. For
2756 template <class T> struct S { void f(); };
2759 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2760 classes are not counted in the TEMPLATE_COUNT, so that in
2762 template <class T> struct S {};
2763 template <> struct S<int> { void f(); }
2764 template <> void S<int>::f();
2766 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2767 invalid; there should be no template <>.)
2769 If the function is a specialization, it is marked as such via
2770 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2771 is set up correctly, and it is added to the list of specializations
2772 for that template. */
2775 check_explicit_specialization (tree declarator
,
2781 int have_def
= flags
& 2;
2782 int is_friend
= flags
& 4;
2783 bool is_concept
= flags
& 8;
2784 int specialization
= 0;
2785 int explicit_instantiation
= 0;
2786 int member_specialization
= 0;
2787 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2788 tree dname
= DECL_NAME (decl
);
2793 if (!processing_specialization
)
2796 tsk
= tsk_excessive_parms
;
2799 tsk
= current_tmpl_spec_kind (template_count
);
2804 if (processing_specialization
&& !VAR_P (decl
))
2807 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2809 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2812 /* This could be something like:
2814 template <class T> void f(T);
2815 class S { friend void f<>(int); } */
2819 /* This case handles bogus declarations like template <>
2820 template <class T> void f<int>(); */
2822 error ("template-id %qD in declaration of primary template",
2829 case tsk_invalid_member_spec
:
2830 /* The error has already been reported in
2831 check_specialization_scope. */
2832 return error_mark_node
;
2834 case tsk_invalid_expl_inst
:
2835 error ("template parameter list used in explicit instantiation");
2841 error ("definition provided for explicit instantiation");
2843 explicit_instantiation
= 1;
2846 case tsk_excessive_parms
:
2847 case tsk_insufficient_parms
:
2848 if (tsk
== tsk_excessive_parms
)
2849 error ("too many template parameter lists in declaration of %qD",
2851 else if (template_header_count
)
2852 error("too few template parameter lists in declaration of %qD", decl
);
2854 error("explicit specialization of %qD must be introduced by "
2855 "%<template <>%>", decl
);
2860 error ("explicit specialization declared %<concept%>");
2862 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2863 /* In cases like template<> constexpr bool v = true;
2864 We'll give an error in check_template_variable. */
2867 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2869 member_specialization
= 1;
2875 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2877 /* This case handles bogus declarations like template <>
2878 template <class T> void f<int>(); */
2880 if (!uses_template_parms (declarator
))
2881 error ("template-id %qD in declaration of primary template",
2883 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2885 /* Partial specialization of variable template. */
2886 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2890 else if (cxx_dialect
< cxx14
)
2891 error ("non-type partial specialization %qD "
2892 "is not allowed", declarator
);
2894 error ("non-class, non-variable partial specialization %qD "
2895 "is not allowed", declarator
);
2900 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2901 /* This is a specialization of a member template, without
2902 specialization the containing class. Something like:
2904 template <class T> struct S {
2905 template <class U> void f (U);
2907 template <> template <class U> void S<int>::f(U) {}
2909 That's a specialization -- but of the entire template. */
2917 if ((specialization
|| member_specialization
)
2918 /* This doesn't apply to variable templates. */
2919 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2920 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2922 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2923 for (; t
; t
= TREE_CHAIN (t
))
2924 if (TREE_PURPOSE (t
))
2926 permerror (input_location
,
2927 "default argument specified in explicit specialization");
2932 if (specialization
|| member_specialization
|| explicit_instantiation
)
2934 tree tmpl
= NULL_TREE
;
2935 tree targs
= NULL_TREE
;
2936 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2938 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2939 if (!was_template_id
)
2943 gcc_assert (identifier_p (declarator
));
2948 /* If there is no class context, the explicit instantiation
2949 must be at namespace scope. */
2950 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2952 /* Find the namespace binding, using the declaration
2954 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2956 if (fns
== error_mark_node
)
2957 /* If lookup fails, look for a friend declaration so we can
2958 give a better diagnostic. */
2959 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2960 /*type*/false, /*complain*/true,
2963 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2965 error ("%qD is not a template function", dname
);
2966 fns
= error_mark_node
;
2970 declarator
= lookup_template_function (fns
, NULL_TREE
);
2973 if (declarator
== error_mark_node
)
2974 return error_mark_node
;
2976 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2978 if (!explicit_instantiation
)
2979 /* A specialization in class scope. This is invalid,
2980 but the error will already have been flagged by
2981 check_specialization_scope. */
2982 return error_mark_node
;
2985 /* It's not valid to write an explicit instantiation in
2988 class C { template void f(); }
2990 This case is caught by the parser. However, on
2993 template class C { void f(); };
2995 (which is invalid) we can get here. The error will be
3002 else if (ctype
!= NULL_TREE
3003 && (identifier_p (TREE_OPERAND (declarator
, 0))))
3005 // We'll match variable templates in start_decl.
3009 /* Find the list of functions in ctype that have the same
3010 name as the declared function. */
3011 tree name
= TREE_OPERAND (declarator
, 0);
3013 if (constructor_name_p (name
, ctype
))
3015 if (DECL_CONSTRUCTOR_P (decl
)
3016 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3017 : !CLASSTYPE_DESTRUCTOR (ctype
))
3019 /* From [temp.expl.spec]:
3021 If such an explicit specialization for the member
3022 of a class template names an implicitly-declared
3023 special member function (clause _special_), the
3024 program is ill-formed.
3026 Similar language is found in [temp.explicit]. */
3027 error ("specialization of implicitly-declared special member function");
3028 return error_mark_node
;
3031 name
= DECL_NAME (decl
);
3034 /* For a type-conversion operator, We might be looking for
3035 `operator int' which will be a specialization of
3036 `operator T'. Grab all the conversion operators, and
3037 then select from them. */
3038 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3039 ? conv_op_identifier
: name
);
3041 if (fns
== NULL_TREE
)
3043 error ("no member function %qD declared in %qT", name
, ctype
);
3044 return error_mark_node
;
3047 TREE_OPERAND (declarator
, 0) = fns
;
3050 /* Figure out what exactly is being specialized at this point.
3051 Note that for an explicit instantiation, even one for a
3052 member function, we cannot tell a priori whether the
3053 instantiation is for a member template, or just a member
3054 function of a template class. Even if a member template is
3055 being instantiated, the member template arguments may be
3056 elided if they can be deduced from the rest of the
3058 tmpl
= determine_specialization (declarator
, decl
,
3060 member_specialization
,
3064 if (!tmpl
|| tmpl
== error_mark_node
)
3065 /* We couldn't figure out what this declaration was
3067 return error_mark_node
;
3070 if (TREE_CODE (decl
) == FUNCTION_DECL
3071 && DECL_HIDDEN_FRIEND_P (tmpl
))
3073 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3074 "friend declaration %qD is not visible to "
3075 "explicit specialization", tmpl
))
3076 inform (DECL_SOURCE_LOCATION (tmpl
),
3077 "friend declaration here");
3079 else if (!ctype
&& !is_friend
3080 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3081 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3083 tree gen_tmpl
= most_general_template (tmpl
);
3085 if (explicit_instantiation
)
3087 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3088 is done by do_decl_instantiation later. */
3090 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3091 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3093 if (arg_depth
> parm_depth
)
3095 /* If TMPL is not the most general template (for
3096 example, if TMPL is a friend template that is
3097 injected into namespace scope), then there will
3098 be too many levels of TARGS. Remove some of them
3103 new_targs
= make_tree_vec (parm_depth
);
3104 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3105 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3106 = TREE_VEC_ELT (targs
, i
);
3110 return instantiate_template (tmpl
, targs
, tf_error
);
3113 /* If we thought that the DECL was a member function, but it
3114 turns out to be specializing a static member function,
3115 make DECL a static member function as well. */
3116 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3117 && DECL_STATIC_FUNCTION_P (tmpl
)
3118 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3119 revert_static_member_fn (decl
);
3121 /* If this is a specialization of a member template of a
3122 template class, we want to return the TEMPLATE_DECL, not
3123 the specialization of it. */
3124 if (tsk
== tsk_template
&& !was_template_id
)
3126 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3127 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3128 DECL_INITIAL (result
) = NULL_TREE
;
3132 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3133 DECL_SOURCE_LOCATION (result
)
3134 = DECL_SOURCE_LOCATION (decl
);
3135 /* We want to use the argument list specified in the
3136 definition, not in the original declaration. */
3137 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3138 for (parm
= DECL_ARGUMENTS (result
); parm
;
3139 parm
= DECL_CHAIN (parm
))
3140 DECL_CONTEXT (parm
) = result
;
3142 return register_specialization (tmpl
, gen_tmpl
, targs
,
3146 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3147 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3149 if (was_template_id
)
3150 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3152 /* Inherit default function arguments from the template
3153 DECL is specializing. */
3154 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3155 copy_default_args_to_explicit_spec (decl
);
3157 /* This specialization has the same protection as the
3158 template it specializes. */
3159 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3160 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3162 /* 7.1.1-1 [dcl.stc]
3164 A storage-class-specifier shall not be specified in an
3165 explicit specialization...
3167 The parser rejects these, so unless action is taken here,
3168 explicit function specializations will always appear with
3171 The action recommended by the C++ CWG in response to C++
3172 defect report 605 is to make the storage class and linkage
3173 of the explicit specialization match the templated function:
3175 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3177 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3179 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3180 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3182 /* A concept cannot be specialized. */
3183 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3185 error ("explicit specialization of function concept %qD",
3187 return error_mark_node
;
3190 /* This specialization has the same linkage and visibility as
3191 the function template it specializes. */
3192 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3193 if (! TREE_PUBLIC (decl
))
3195 DECL_INTERFACE_KNOWN (decl
) = 1;
3196 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3198 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3199 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3201 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3202 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3206 /* If DECL is a friend declaration, declared using an
3207 unqualified name, the namespace associated with DECL may
3208 have been set incorrectly. For example, in:
3210 template <typename T> void f(T);
3212 struct S { friend void f<int>(int); }
3215 we will have set the DECL_CONTEXT for the friend
3216 declaration to N, rather than to the global namespace. */
3217 if (DECL_NAMESPACE_SCOPE_P (decl
))
3218 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3220 if (is_friend
&& !have_def
)
3221 /* This is not really a declaration of a specialization.
3222 It's just the name of an instantiation. But, it's not
3223 a request for an instantiation, either. */
3224 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3225 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3226 /* A specialization is not necessarily COMDAT. */
3227 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3228 && DECL_DECLARED_INLINE_P (decl
));
3229 else if (VAR_P (decl
))
3230 DECL_COMDAT (decl
) = false;
3232 /* If this is a full specialization, register it so that we can find
3233 it again. Partial specializations will be registered in
3234 process_partial_specialization. */
3235 if (!processing_template_decl
)
3237 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3239 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3244 /* A 'structor should already have clones. */
3245 gcc_assert (decl
== error_mark_node
3246 || variable_template_p (tmpl
)
3247 || !(DECL_CONSTRUCTOR_P (decl
)
3248 || DECL_DESTRUCTOR_P (decl
))
3249 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3256 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3257 parameters. These are represented in the same format used for
3258 DECL_TEMPLATE_PARMS. */
3261 comp_template_parms (const_tree parms1
, const_tree parms2
)
3266 if (parms1
== parms2
)
3269 for (p1
= parms1
, p2
= parms2
;
3270 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3271 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3273 tree t1
= TREE_VALUE (p1
);
3274 tree t2
= TREE_VALUE (p2
);
3277 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3278 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3280 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3283 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3285 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3286 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3288 /* If either of the template parameters are invalid, assume
3289 they match for the sake of error recovery. */
3290 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3293 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3296 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3297 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3298 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3300 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3305 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3306 /* One set of parameters has more parameters lists than the
3313 /* Determine whether PARM is a parameter pack. */
3316 template_parameter_pack_p (const_tree parm
)
3318 /* Determine if we have a non-type template parameter pack. */
3319 if (TREE_CODE (parm
) == PARM_DECL
)
3320 return (DECL_TEMPLATE_PARM_P (parm
)
3321 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3322 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3323 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3325 /* If this is a list of template parameters, we could get a
3326 TYPE_DECL or a TEMPLATE_DECL. */
3327 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3328 parm
= TREE_TYPE (parm
);
3330 /* Otherwise it must be a type template parameter. */
3331 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3332 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3333 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3336 /* Determine if T is a function parameter pack. */
3339 function_parameter_pack_p (const_tree t
)
3341 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3342 return DECL_PACK_P (t
);
3346 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3347 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3350 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3352 if (! primary_func_tmpl_inst
3353 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3354 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3357 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3360 /* Return true iff the function parameter PARAM_DECL was expanded
3361 from the function parameter pack PACK. */
3364 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3366 if (DECL_ARTIFICIAL (param_decl
)
3367 || !function_parameter_pack_p (pack
))
3370 /* The parameter pack and its pack arguments have the same
3372 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3375 /* Determine whether ARGS describes a variadic template args list,
3376 i.e., one that is terminated by a template argument pack. */
3379 template_args_variadic_p (tree args
)
3384 if (args
== NULL_TREE
)
3387 args
= INNERMOST_TEMPLATE_ARGS (args
);
3388 nargs
= TREE_VEC_LENGTH (args
);
3393 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3395 return ARGUMENT_PACK_P (last_parm
);
3398 /* Generate a new name for the parameter pack name NAME (an
3399 IDENTIFIER_NODE) that incorporates its */
3402 make_ith_pack_parameter_name (tree name
, int i
)
3404 /* Munge the name to include the parameter index. */
3405 #define NUMBUF_LEN 128
3406 char numbuf
[NUMBUF_LEN
];
3410 if (name
== NULL_TREE
)
3412 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3413 newname_len
= IDENTIFIER_LENGTH (name
)
3414 + strlen (numbuf
) + 2;
3415 newname
= (char*)alloca (newname_len
);
3416 snprintf (newname
, newname_len
,
3417 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3418 return get_identifier (newname
);
3421 /* Return true if T is a primary function, class or alias template
3422 specialization, not including the template pattern. */
3425 primary_template_specialization_p (const_tree t
)
3430 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3431 return (DECL_LANG_SPECIFIC (t
)
3432 && DECL_USE_TEMPLATE (t
)
3433 && DECL_TEMPLATE_INFO (t
)
3434 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3435 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3436 return (CLASSTYPE_TEMPLATE_INFO (t
)
3437 && CLASSTYPE_USE_TEMPLATE (t
)
3438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3439 else if (alias_template_specialization_p (t
))
3444 /* Return true if PARM is a template template parameter. */
3447 template_template_parameter_p (const_tree parm
)
3449 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3452 /* Return true iff PARM is a DECL representing a type template
3456 template_type_parameter_p (const_tree parm
)
3459 && (TREE_CODE (parm
) == TYPE_DECL
3460 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3461 && DECL_TEMPLATE_PARM_P (parm
));
3464 /* Return the template parameters of T if T is a
3465 primary template instantiation, NULL otherwise. */
3468 get_primary_template_innermost_parameters (const_tree t
)
3470 tree parms
= NULL
, template_info
= NULL
;
3472 if ((template_info
= get_template_info (t
))
3473 && primary_template_specialization_p (t
))
3474 parms
= INNERMOST_TEMPLATE_PARMS
3475 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3480 /* Return the template parameters of the LEVELth level from the full list
3481 of template parameters PARMS. */
3484 get_template_parms_at_level (tree parms
, int level
)
3488 || TREE_CODE (parms
) != TREE_LIST
3489 || level
> TMPL_PARMS_DEPTH (parms
))
3492 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3493 if (TMPL_PARMS_DEPTH (p
) == level
)
3499 /* Returns the template arguments of T if T is a template instantiation,
3503 get_template_innermost_arguments (const_tree t
)
3505 tree args
= NULL
, template_info
= NULL
;
3507 if ((template_info
= get_template_info (t
))
3508 && TI_ARGS (template_info
))
3509 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3514 /* Return the argument pack elements of T if T is a template argument pack,
3518 get_template_argument_pack_elems (const_tree t
)
3520 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3521 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3524 return ARGUMENT_PACK_ARGS (t
);
3527 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3528 ARGUMENT_PACK_SELECT represents. */
3531 argument_pack_select_arg (tree t
)
3533 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3534 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3536 /* If the selected argument is an expansion E, that most likely means we were
3537 called from gen_elem_of_pack_expansion_instantiation during the
3538 substituting of an argument pack (of which the Ith element is a pack
3539 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3540 In this case, the Ith element resulting from this substituting is going to
3541 be a pack expansion, which pattern is the pattern of E. Let's return the
3542 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3543 resulting pack expansion from it. */
3544 if (PACK_EXPANSION_P (arg
))
3546 /* Make sure we aren't throwing away arg info. */
3547 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3548 arg
= PACK_EXPANSION_PATTERN (arg
);
3555 /* True iff FN is a function representing a built-in variadic parameter
3559 builtin_pack_fn_p (tree fn
)
3562 || TREE_CODE (fn
) != FUNCTION_DECL
3563 || !DECL_IS_BUILTIN (fn
))
3566 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3572 /* True iff CALL is a call to a function representing a built-in variadic
3576 builtin_pack_call_p (tree call
)
3578 if (TREE_CODE (call
) != CALL_EXPR
)
3580 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3583 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3586 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3589 tree ohi
= CALL_EXPR_ARG (call
, 0);
3590 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3591 false/*fn*/, true/*int_cst*/);
3593 if (value_dependent_expression_p (hi
))
3597 call
= copy_node (call
);
3598 CALL_EXPR_ARG (call
, 0) = hi
;
3600 tree ex
= make_pack_expansion (call
, complain
);
3601 tree vec
= make_tree_vec (1);
3602 TREE_VEC_ELT (vec
, 0) = ex
;
3607 hi
= cxx_constant_value (hi
);
3608 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3610 /* Calculate the largest value of len that won't make the size of the vec
3611 overflow an int. The compiler will exceed resource limits long before
3612 this, but it seems a decent place to diagnose. */
3613 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3615 if (len
< 0 || len
> max
)
3617 if ((complain
& tf_error
)
3618 && hi
!= error_mark_node
)
3619 error ("argument to __integer_pack must be between 0 and %d", max
);
3620 return error_mark_node
;
3623 tree vec
= make_tree_vec (len
);
3625 for (int i
= 0; i
< len
; ++i
)
3626 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3632 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3636 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3639 if (!builtin_pack_call_p (call
))
3642 tree fn
= CALL_EXPR_FN (call
);
3644 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3645 return expand_integer_pack (call
, args
, complain
, in_decl
);
3650 /* Structure used to track the progress of find_parameter_packs_r. */
3651 struct find_parameter_pack_data
3653 /* TREE_LIST that will contain all of the parameter packs found by
3655 tree
* parameter_packs
;
3657 /* Set of AST nodes that have been visited by the traversal. */
3658 hash_set
<tree
> *visited
;
3660 /* True iff we're making a type pack expansion. */
3661 bool type_pack_expansion_p
;
3664 /* Identifies all of the argument packs that occur in a template
3665 argument and appends them to the TREE_LIST inside DATA, which is a
3666 find_parameter_pack_data structure. This is a subroutine of
3667 make_pack_expansion and uses_parameter_packs. */
3669 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3672 struct find_parameter_pack_data
* ppd
=
3673 (struct find_parameter_pack_data
*)data
;
3674 bool parameter_pack_p
= false;
3676 /* Handle type aliases/typedefs. */
3677 if (TYPE_ALIAS_P (t
))
3679 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3680 cp_walk_tree (&TI_ARGS (tinfo
),
3681 &find_parameter_packs_r
,
3687 /* Identify whether this is a parameter pack or not. */
3688 switch (TREE_CODE (t
))
3690 case TEMPLATE_PARM_INDEX
:
3691 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3692 parameter_pack_p
= true;
3695 case TEMPLATE_TYPE_PARM
:
3696 t
= TYPE_MAIN_VARIANT (t
);
3698 case TEMPLATE_TEMPLATE_PARM
:
3699 /* If the placeholder appears in the decl-specifier-seq of a function
3700 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3701 is a pack expansion, the invented template parameter is a template
3703 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3704 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3705 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3706 parameter_pack_p
= true;
3711 if (DECL_PACK_P (t
))
3713 /* We don't want to walk into the type of a PARM_DECL,
3714 because we don't want to see the type parameter pack. */
3716 parameter_pack_p
= true;
3721 if (DECL_PACK_P (t
))
3723 /* We don't want to walk into the type of a variadic capture proxy,
3724 because we don't want to see the type parameter pack. */
3726 parameter_pack_p
= true;
3728 else if (variable_template_specialization_p (t
))
3730 cp_walk_tree (&DECL_TI_ARGS (t
),
3731 find_parameter_packs_r
,
3738 if (builtin_pack_call_p (t
))
3739 parameter_pack_p
= true;
3743 parameter_pack_p
= true;
3746 /* Not a parameter pack. */
3750 if (parameter_pack_p
)
3752 /* Add this parameter pack to the list. */
3753 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3757 cp_walk_tree (&TYPE_CONTEXT (t
),
3758 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3760 /* This switch statement will return immediately if we don't find a
3762 switch (TREE_CODE (t
))
3764 case TEMPLATE_PARM_INDEX
:
3767 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3768 /* Check the template itself. */
3769 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3770 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3771 /* Check the template arguments. */
3772 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3777 case TEMPLATE_TYPE_PARM
:
3778 case TEMPLATE_TEMPLATE_PARM
:
3785 /* Ignore the declaration of a capture proxy for a parameter pack. */
3786 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3791 if (TYPE_PTRMEMFUNC_P (t
))
3797 if (TYPE_TEMPLATE_INFO (t
))
3798 cp_walk_tree (&TYPE_TI_ARGS (t
),
3799 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3805 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3810 cp_walk_tree (&TREE_TYPE (t
),
3811 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3815 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3820 case TYPE_PACK_EXPANSION
:
3821 case EXPR_PACK_EXPANSION
:
3826 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3831 case IDENTIFIER_NODE
:
3832 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3839 /* Look at explicit captures. */
3840 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3841 cap
; cap
= TREE_CHAIN (cap
))
3842 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3844 /* Since we defer implicit capture, look in the parms and body. */
3845 tree fn
= lambda_function (t
);
3846 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3848 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3856 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3857 type_pack_expansion_p to false so that any placeholders
3858 within the expression don't get marked as parameter packs. */
3859 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3860 ppd
->type_pack_expansion_p
= false;
3861 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3863 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3869 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3871 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3873 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3875 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3886 /* Determines if the expression or type T uses any parameter packs. */
3888 uses_parameter_packs (tree t
)
3890 tree parameter_packs
= NULL_TREE
;
3891 struct find_parameter_pack_data ppd
;
3892 ppd
.parameter_packs
= ¶meter_packs
;
3893 ppd
.visited
= new hash_set
<tree
>;
3894 ppd
.type_pack_expansion_p
= false;
3895 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3897 return parameter_packs
!= NULL_TREE
;
3900 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3901 representation a base-class initializer into a parameter pack
3902 expansion. If all goes well, the resulting node will be an
3903 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3906 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3909 tree parameter_packs
= NULL_TREE
;
3910 bool for_types
= false;
3911 struct find_parameter_pack_data ppd
;
3913 if (!arg
|| arg
== error_mark_node
)
3916 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3918 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3919 class initializer. In this case, the TREE_PURPOSE will be a
3920 _TYPE node (representing the base class expansion we're
3921 initializing) and the TREE_VALUE will be a TREE_LIST
3922 containing the initialization arguments.
3924 The resulting expansion looks somewhat different from most
3925 expansions. Rather than returning just one _EXPANSION, we
3926 return a TREE_LIST whose TREE_PURPOSE is a
3927 TYPE_PACK_EXPANSION containing the bases that will be
3928 initialized. The TREE_VALUE will be identical to the
3929 original TREE_VALUE, which is a list of arguments that will
3930 be passed to each base. We do not introduce any new pack
3931 expansion nodes into the TREE_VALUE (although it is possible
3932 that some already exist), because the TREE_PURPOSE and
3933 TREE_VALUE all need to be expanded together with the same
3934 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3935 resulting TREE_PURPOSE will mention the parameter packs in
3936 both the bases and the arguments to the bases. */
3939 tree parameter_packs
= NULL_TREE
;
3941 /* Determine which parameter packs will be used by the base
3943 ppd
.visited
= new hash_set
<tree
>;
3944 ppd
.parameter_packs
= ¶meter_packs
;
3945 ppd
.type_pack_expansion_p
= true;
3946 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3947 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3950 if (parameter_packs
== NULL_TREE
)
3952 if (complain
& tf_error
)
3953 error ("base initializer expansion %qT contains no parameter packs",
3956 return error_mark_node
;
3959 if (TREE_VALUE (arg
) != void_type_node
)
3961 /* Collect the sets of parameter packs used in each of the
3962 initialization arguments. */
3963 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3965 /* Determine which parameter packs will be expanded in this
3967 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3974 /* Create the pack expansion type for the base type. */
3975 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3976 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3977 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3978 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3980 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3981 they will rarely be compared to anything. */
3982 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3984 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3987 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3990 /* Build the PACK_EXPANSION_* node. */
3992 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3993 : make_node (EXPR_PACK_EXPANSION
);
3994 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3995 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3997 /* Propagate type and const-expression information. */
3998 TREE_TYPE (result
) = TREE_TYPE (arg
);
3999 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
4000 /* Mark this read now, since the expansion might be length 0. */
4001 mark_exp_read (arg
);
4004 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4005 they will rarely be compared to anything. */
4006 SET_TYPE_STRUCTURAL_EQUALITY (result
);
4008 /* Determine which parameter packs will be expanded. */
4009 ppd
.parameter_packs
= ¶meter_packs
;
4010 ppd
.visited
= new hash_set
<tree
>;
4011 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4012 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4015 /* Make sure we found some parameter packs. */
4016 if (parameter_packs
== NULL_TREE
)
4018 if (complain
& tf_error
)
4021 error ("expansion pattern %qT contains no parameter packs", arg
);
4023 error ("expansion pattern %qE contains no parameter packs", arg
);
4025 return error_mark_node
;
4027 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4029 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4034 /* Checks T for any "bare" parameter packs, which have not yet been
4035 expanded, and issues an error if any are found. This operation can
4036 only be done on full expressions or types (e.g., an expression
4037 statement, "if" condition, etc.), because we could have expressions like:
4039 foo(f(g(h(args)))...)
4041 where "args" is a parameter pack. check_for_bare_parameter_packs
4042 should not be called for the subexpressions args, h(args),
4043 g(h(args)), or f(g(h(args))), because we would produce erroneous
4046 Returns TRUE and emits an error if there were bare parameter packs,
4047 returns FALSE otherwise. */
4049 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4051 tree parameter_packs
= NULL_TREE
;
4052 struct find_parameter_pack_data ppd
;
4054 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4057 /* A lambda might use a parameter pack from the containing context. */
4058 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4059 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4062 if (TREE_CODE (t
) == TYPE_DECL
)
4065 ppd
.parameter_packs
= ¶meter_packs
;
4066 ppd
.visited
= new hash_set
<tree
>;
4067 ppd
.type_pack_expansion_p
= false;
4068 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4071 if (parameter_packs
)
4073 if (loc
== UNKNOWN_LOCATION
)
4074 loc
= cp_expr_loc_or_loc (t
, input_location
);
4075 error_at (loc
, "parameter packs not expanded with %<...%>:");
4076 while (parameter_packs
)
4078 tree pack
= TREE_VALUE (parameter_packs
);
4079 tree name
= NULL_TREE
;
4081 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4082 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4083 name
= TYPE_NAME (pack
);
4084 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4085 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4086 else if (TREE_CODE (pack
) == CALL_EXPR
)
4087 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4089 name
= DECL_NAME (pack
);
4092 inform (loc
, " %qD", name
);
4094 inform (loc
, " <anonymous>");
4096 parameter_packs
= TREE_CHAIN (parameter_packs
);
4105 /* Expand any parameter packs that occur in the template arguments in
4108 expand_template_argument_pack (tree args
)
4110 if (args
== error_mark_node
)
4111 return error_mark_node
;
4113 tree result_args
= NULL_TREE
;
4114 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4115 int num_result_args
= -1;
4116 int non_default_args_count
= -1;
4118 /* First, determine if we need to expand anything, and the number of
4119 slots we'll need. */
4120 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4122 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4123 if (arg
== NULL_TREE
)
4125 if (ARGUMENT_PACK_P (arg
))
4127 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4128 if (num_result_args
< 0)
4129 num_result_args
= in_arg
+ num_packed
;
4131 num_result_args
+= num_packed
;
4135 if (num_result_args
>= 0)
4140 /* If no expansion is necessary, we're done. */
4141 if (num_result_args
< 0)
4144 /* Expand arguments. */
4145 result_args
= make_tree_vec (num_result_args
);
4146 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4147 non_default_args_count
=
4148 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4149 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4151 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4152 if (ARGUMENT_PACK_P (arg
))
4154 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4155 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4156 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4157 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4158 if (non_default_args_count
> 0)
4159 non_default_args_count
+= num_packed
- 1;
4163 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4167 if (non_default_args_count
>= 0)
4168 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4172 /* Checks if DECL shadows a template parameter.
4174 [temp.local]: A template-parameter shall not be redeclared within its
4175 scope (including nested scopes).
4177 Emits an error and returns TRUE if the DECL shadows a parameter,
4178 returns FALSE otherwise. */
4181 check_template_shadow (tree decl
)
4185 /* If we're not in a template, we can't possibly shadow a template
4187 if (!current_template_parms
)
4190 /* Figure out what we're shadowing. */
4191 decl
= OVL_FIRST (decl
);
4192 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4194 /* If there's no previous binding for this name, we're not shadowing
4195 anything, let alone a template parameter. */
4199 /* If we're not shadowing a template parameter, we're done. Note
4200 that OLDDECL might be an OVERLOAD (or perhaps even an
4201 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4203 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4206 /* We check for decl != olddecl to avoid bogus errors for using a
4207 name inside a class. We check TPFI to avoid duplicate errors for
4208 inline member templates. */
4210 || (DECL_TEMPLATE_PARM_P (decl
)
4211 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4214 /* Don't complain about the injected class name, as we've already
4215 complained about the class itself. */
4216 if (DECL_SELF_REFERENCE_P (decl
))
4219 if (DECL_TEMPLATE_PARM_P (decl
))
4220 error ("declaration of template parameter %q+D shadows "
4221 "template parameter", decl
);
4223 error ("declaration of %q+#D shadows template parameter", decl
);
4224 inform (DECL_SOURCE_LOCATION (olddecl
),
4225 "template parameter %qD declared here", olddecl
);
4229 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4230 ORIG_LEVEL, DECL, and TYPE. */
4233 build_template_parm_index (int index
,
4239 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4240 TEMPLATE_PARM_IDX (t
) = index
;
4241 TEMPLATE_PARM_LEVEL (t
) = level
;
4242 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4243 TEMPLATE_PARM_DECL (t
) = decl
;
4244 TREE_TYPE (t
) = type
;
4245 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4246 TREE_READONLY (t
) = TREE_READONLY (decl
);
4251 /* Find the canonical type parameter for the given template type
4252 parameter. Returns the canonical type parameter, which may be TYPE
4253 if no such parameter existed. */
4256 canonical_type_parameter (tree type
)
4259 int idx
= TEMPLATE_TYPE_IDX (type
);
4260 if (!canonical_template_parms
)
4261 vec_alloc (canonical_template_parms
, idx
+ 1);
4263 if (canonical_template_parms
->length () <= (unsigned) idx
)
4264 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4266 list
= (*canonical_template_parms
)[idx
];
4267 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4268 list
= TREE_CHAIN (list
);
4271 return TREE_VALUE (list
);
4274 (*canonical_template_parms
)[idx
]
4275 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4280 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4281 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4282 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4283 new one is created. */
4286 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4287 tsubst_flags_t complain
)
4289 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4290 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4291 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4292 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4294 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4297 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4298 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4299 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4300 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4301 DECL_ARTIFICIAL (decl
) = 1;
4302 SET_DECL_TEMPLATE_PARM_P (decl
);
4304 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4305 TEMPLATE_PARM_LEVEL (index
) - levels
,
4306 TEMPLATE_PARM_ORIG_LEVEL (index
),
4308 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4309 TEMPLATE_PARM_PARAMETER_PACK (t
)
4310 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4312 /* Template template parameters need this. */
4313 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4315 DECL_TEMPLATE_RESULT (decl
)
4316 = build_decl (DECL_SOURCE_LOCATION (decl
),
4317 TYPE_DECL
, DECL_NAME (decl
), type
);
4318 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4319 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4320 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4324 return TEMPLATE_PARM_DESCENDANTS (index
);
4327 /* Process information from new template parameter PARM and append it
4328 to the LIST being built. This new parameter is a non-type
4329 parameter iff IS_NON_TYPE is true. This new parameter is a
4330 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4334 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4335 bool is_non_type
, bool is_parameter_pack
)
4340 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4341 tree defval
= TREE_PURPOSE (parm
);
4342 tree constr
= TREE_TYPE (parm
);
4346 tree p
= tree_last (list
);
4348 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4351 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4352 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4354 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4362 parm
= TREE_VALUE (parm
);
4364 SET_DECL_TEMPLATE_PARM_P (parm
);
4366 if (TREE_TYPE (parm
) != error_mark_node
)
4370 The top-level cv-qualifiers on the template-parameter are
4371 ignored when determining its type. */
4372 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4373 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4374 TREE_TYPE (parm
) = error_mark_node
;
4375 else if (uses_parameter_packs (TREE_TYPE (parm
))
4376 && !is_parameter_pack
4377 /* If we're in a nested template parameter list, the template
4378 template parameter could be a parameter pack. */
4379 && processing_template_parmlist
== 1)
4381 /* This template parameter is not a parameter pack, but it
4382 should be. Complain about "bare" parameter packs. */
4383 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4385 /* Recover by calling this a parameter pack. */
4386 is_parameter_pack
= true;
4390 /* A template parameter is not modifiable. */
4391 TREE_CONSTANT (parm
) = 1;
4392 TREE_READONLY (parm
) = 1;
4393 decl
= build_decl (parm_loc
,
4394 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4395 TREE_CONSTANT (decl
) = 1;
4396 TREE_READONLY (decl
) = 1;
4397 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4398 = build_template_parm_index (idx
, processing_template_decl
,
4399 processing_template_decl
,
4400 decl
, TREE_TYPE (parm
));
4402 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4403 = is_parameter_pack
;
4408 parm
= TREE_VALUE (TREE_VALUE (parm
));
4410 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4412 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4413 /* This is for distinguishing between real templates and template
4414 template parameters */
4415 TREE_TYPE (parm
) = t
;
4416 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4421 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4422 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4423 decl
= build_decl (parm_loc
,
4424 TYPE_DECL
, parm
, t
);
4427 TYPE_NAME (t
) = decl
;
4428 TYPE_STUB_DECL (t
) = decl
;
4430 TEMPLATE_TYPE_PARM_INDEX (t
)
4431 = build_template_parm_index (idx
, processing_template_decl
,
4432 processing_template_decl
,
4433 decl
, TREE_TYPE (parm
));
4434 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4435 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4437 DECL_ARTIFICIAL (decl
) = 1;
4438 SET_DECL_TEMPLATE_PARM_P (decl
);
4440 /* Build requirements for the type/template parameter.
4441 This must be done after SET_DECL_TEMPLATE_PARM_P or
4442 process_template_parm could fail. */
4443 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4447 /* Build the parameter node linking the parameter declaration,
4448 its default argument (if any), and its constraints (if any). */
4449 parm
= build_tree_list (defval
, parm
);
4450 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4452 return chainon (list
, parm
);
4455 /* The end of a template parameter list has been reached. Process the
4456 tree list into a parameter vector, converting each parameter into a more
4457 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4461 end_template_parm_list (tree parms
)
4465 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4467 /* Pop the dummy parameter level and add the real one. */
4468 current_template_parms
= TREE_CHAIN (current_template_parms
);
4470 current_template_parms
4471 = tree_cons (size_int (processing_template_decl
),
4472 saved_parmlist
, current_template_parms
);
4474 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4476 next
= TREE_CHAIN (parm
);
4477 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4478 TREE_CHAIN (parm
) = NULL_TREE
;
4481 --processing_template_parmlist
;
4483 return saved_parmlist
;
4486 // Explicitly indicate the end of the template parameter list. We assume
4487 // that the current template parameters have been constructed and/or
4488 // managed explicitly, as when creating new template template parameters
4489 // from a shorthand constraint.
4491 end_template_parm_list ()
4493 --processing_template_parmlist
;
4496 /* end_template_decl is called after a template declaration is seen. */
4499 end_template_decl (void)
4501 reset_specialization ();
4503 if (! processing_template_decl
)
4506 /* This matches the pushlevel in begin_template_parm_list. */
4509 --processing_template_decl
;
4510 current_template_parms
= TREE_CHAIN (current_template_parms
);
4513 /* Takes a TREE_LIST representing a template parameter and convert it
4514 into an argument suitable to be passed to the type substitution
4515 functions. Note that If the TREE_LIST contains an error_mark
4516 node, the returned argument is error_mark_node. */
4519 template_parm_to_arg (tree t
)
4523 || TREE_CODE (t
) != TREE_LIST
)
4526 if (error_operand_p (TREE_VALUE (t
)))
4527 return error_mark_node
;
4531 if (TREE_CODE (t
) == TYPE_DECL
4532 || TREE_CODE (t
) == TEMPLATE_DECL
)
4536 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4538 /* Turn this argument into a TYPE_ARGUMENT_PACK
4539 with a single element, which expands T. */
4540 tree vec
= make_tree_vec (1);
4542 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4544 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4546 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4547 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4552 t
= DECL_INITIAL (t
);
4554 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4556 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4557 with a single element, which expands T. */
4558 tree vec
= make_tree_vec (1);
4560 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4562 t
= convert_from_reference (t
);
4563 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4565 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4566 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4569 t
= convert_from_reference (t
);
4574 /* Given a single level of template parameters (a TREE_VEC), return it
4575 as a set of template arguments. */
4578 template_parms_level_to_args (tree parms
)
4580 tree a
= copy_node (parms
);
4581 TREE_TYPE (a
) = NULL_TREE
;
4582 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4583 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4586 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4591 /* Given a set of template parameters, return them as a set of template
4592 arguments. The template parameters are represented as a TREE_VEC, in
4593 the form documented in cp-tree.h for template arguments. */
4596 template_parms_to_args (tree parms
)
4599 tree args
= NULL_TREE
;
4600 int length
= TMPL_PARMS_DEPTH (parms
);
4603 /* If there is only one level of template parameters, we do not
4604 create a TREE_VEC of TREE_VECs. Instead, we return a single
4605 TREE_VEC containing the arguments. */
4607 args
= make_tree_vec (length
);
4609 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4611 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4614 TREE_VEC_ELT (args
, --l
) = a
;
4622 /* Within the declaration of a template, return the currently active
4623 template parameters as an argument TREE_VEC. */
4626 current_template_args (void)
4628 return template_parms_to_args (current_template_parms
);
4631 /* Update the declared TYPE by doing any lookups which were thought to be
4632 dependent, but are not now that we know the SCOPE of the declarator. */
4635 maybe_update_decl_type (tree orig_type
, tree scope
)
4637 tree type
= orig_type
;
4639 if (type
== NULL_TREE
)
4642 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4643 type
= TREE_TYPE (type
);
4645 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4646 && dependent_type_p (type
)
4647 /* Don't bother building up the args in this case. */
4648 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4650 /* tsubst in the args corresponding to the template parameters,
4651 including auto if present. Most things will be unchanged, but
4652 make_typename_type and tsubst_qualified_id will resolve
4653 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4654 tree args
= current_template_args ();
4655 tree auto_node
= type_uses_auto (type
);
4659 tree auto_vec
= make_tree_vec (1);
4660 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4661 args
= add_to_template_args (args
, auto_vec
);
4663 pushed
= push_scope (scope
);
4664 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4669 if (type
== error_mark_node
)
4672 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4674 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4677 type
= TYPE_NAME (type
);
4682 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4683 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4684 the new template is a member template. */
4687 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4689 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4690 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4691 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4692 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4693 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4694 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4699 struct template_parm_data
4701 /* The level of the template parameters we are currently
4705 /* The index of the specialization argument we are currently
4709 /* An array whose size is the number of template parameters. The
4710 elements are nonzero if the parameter has been used in any one
4711 of the arguments processed so far. */
4714 /* An array whose size is the number of template arguments. The
4715 elements are nonzero if the argument makes use of template
4716 parameters of this level. */
4717 int* arg_uses_template_parms
;
4720 /* Subroutine of push_template_decl used to see if each template
4721 parameter in a partial specialization is used in the explicit
4722 argument list. If T is of the LEVEL given in DATA (which is
4723 treated as a template_parm_data*), then DATA->PARMS is marked
4727 mark_template_parm (tree t
, void* data
)
4731 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4733 template_parm_level_and_index (t
, &level
, &idx
);
4735 if (level
== tpd
->level
)
4737 tpd
->parms
[idx
] = 1;
4738 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4741 /* In C++17 the type of a non-type argument is a deduced context. */
4742 if (cxx_dialect
>= cxx17
4743 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4744 for_each_template_parm (TREE_TYPE (t
),
4745 &mark_template_parm
,
4748 /*include_nondeduced_p=*/false);
4750 /* Return zero so that for_each_template_parm will continue the
4751 traversal of the tree; we want to mark *every* template parm. */
4755 /* Process the partial specialization DECL. */
4758 process_partial_specialization (tree decl
)
4760 tree type
= TREE_TYPE (decl
);
4761 tree tinfo
= get_template_info (decl
);
4762 tree maintmpl
= TI_TEMPLATE (tinfo
);
4763 tree specargs
= TI_ARGS (tinfo
);
4764 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4765 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4768 int nargs
= TREE_VEC_LENGTH (inner_args
);
4771 bool did_error_intro
= false;
4772 struct template_parm_data tpd
;
4773 struct template_parm_data tpd2
;
4775 gcc_assert (current_template_parms
);
4777 /* A concept cannot be specialized. */
4778 if (flag_concepts
&& variable_concept_p (maintmpl
))
4780 error ("specialization of variable concept %q#D", maintmpl
);
4781 return error_mark_node
;
4784 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4785 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4787 /* We check that each of the template parameters given in the
4788 partial specialization is used in the argument list to the
4789 specialization. For example:
4791 template <class T> struct S;
4792 template <class T> struct S<T*>;
4794 The second declaration is OK because `T*' uses the template
4795 parameter T, whereas
4797 template <class T> struct S<int>;
4799 is no good. Even trickier is:
4810 The S2<T> declaration is actually invalid; it is a
4811 full-specialization. Of course,
4814 struct S2<T (*)(U)>;
4816 or some such would have been OK. */
4817 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4818 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4819 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4821 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4822 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4823 for (i
= 0; i
< nargs
; ++i
)
4825 tpd
.current_arg
= i
;
4826 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4827 &mark_template_parm
,
4830 /*include_nondeduced_p=*/false);
4832 for (i
= 0; i
< ntparms
; ++i
)
4833 if (tpd
.parms
[i
] == 0)
4835 /* One of the template parms was not used in a deduced context in the
4837 if (!did_error_intro
)
4839 error ("template parameters not deducible in "
4840 "partial specialization:");
4841 did_error_intro
= true;
4844 inform (input_location
, " %qD",
4845 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4848 if (did_error_intro
)
4849 return error_mark_node
;
4851 /* [temp.class.spec]
4853 The argument list of the specialization shall not be identical to
4854 the implicit argument list of the primary template. */
4856 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4857 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4859 || !strictly_subsumes (current_template_constraints (),
4860 get_constraints (maintmpl
))))
4863 error ("partial specialization %q+D does not specialize "
4864 "any template arguments; to define the primary template, "
4865 "remove the template argument list", decl
);
4867 error ("partial specialization %q+D does not specialize any "
4868 "template arguments and is not more constrained than "
4869 "the primary template; to define the primary template, "
4870 "remove the template argument list", decl
);
4871 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4874 /* A partial specialization that replaces multiple parameters of the
4875 primary template with a pack expansion is less specialized for those
4877 if (nargs
< DECL_NTPARMS (maintmpl
))
4879 error ("partial specialization is not more specialized than the "
4880 "primary template because it replaces multiple parameters "
4881 "with a pack expansion");
4882 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4883 /* Avoid crash in process_partial_specialization. */
4887 /* If we aren't in a dependent class, we can actually try deduction. */
4888 else if (tpd
.level
== 1
4889 /* FIXME we should be able to handle a partial specialization of a
4890 partial instantiation, but currently we can't (c++/41727). */
4891 && TMPL_ARGS_DEPTH (specargs
) == 1
4892 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4894 if (permerror (input_location
, "partial specialization %qD is not "
4895 "more specialized than", decl
))
4896 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4900 /* [temp.class.spec]
4902 A partially specialized non-type argument expression shall not
4903 involve template parameters of the partial specialization except
4904 when the argument expression is a simple identifier.
4906 The type of a template parameter corresponding to a specialized
4907 non-type argument shall not be dependent on a parameter of the
4910 Also, we verify that pack expansions only occur at the
4911 end of the argument list. */
4912 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4914 for (i
= 0; i
< nargs
; ++i
)
4916 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4917 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4918 tree packed_args
= NULL_TREE
;
4921 if (ARGUMENT_PACK_P (arg
))
4923 /* Extract the arguments from the argument pack. We'll be
4924 iterating over these in the following loop. */
4925 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4926 len
= TREE_VEC_LENGTH (packed_args
);
4929 for (j
= 0; j
< len
; j
++)
4932 /* Get the Jth argument in the parameter pack. */
4933 arg
= TREE_VEC_ELT (packed_args
, j
);
4935 if (PACK_EXPANSION_P (arg
))
4937 /* Pack expansions must come at the end of the
4939 if ((packed_args
&& j
< len
- 1)
4940 || (!packed_args
&& i
< nargs
- 1))
4942 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4943 error ("parameter pack argument %qE must be at the "
4944 "end of the template argument list", arg
);
4946 error ("parameter pack argument %qT must be at the "
4947 "end of the template argument list", arg
);
4951 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4952 /* We only care about the pattern. */
4953 arg
= PACK_EXPANSION_PATTERN (arg
);
4955 if (/* These first two lines are the `non-type' bit. */
4957 && TREE_CODE (arg
) != TEMPLATE_DECL
4958 /* This next two lines are the `argument expression is not just a
4959 simple identifier' condition and also the `specialized
4960 non-type argument' bit. */
4961 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4962 && !(REFERENCE_REF_P (arg
)
4963 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4965 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4966 || (packed_args
&& uses_template_parms (arg
)))
4967 error ("template argument %qE involves template parameter(s)",
4971 /* Look at the corresponding template parameter,
4972 marking which template parameters its type depends
4974 tree type
= TREE_TYPE (parm
);
4978 /* We haven't yet initialized TPD2. Do so now. */
4979 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4980 /* The number of parameters here is the number in the
4981 main template, which, as checked in the assertion
4983 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4985 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4988 /* Mark the template parameters. But this time, we're
4989 looking for the template parameters of the main
4990 template, not in the specialization. */
4991 tpd2
.current_arg
= i
;
4992 tpd2
.arg_uses_template_parms
[i
] = 0;
4993 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4994 for_each_template_parm (type
,
4995 &mark_template_parm
,
4998 /*include_nondeduced_p=*/false);
5000 if (tpd2
.arg_uses_template_parms
[i
])
5002 /* The type depended on some template parameters.
5003 If they are fully specialized in the
5004 specialization, that's OK. */
5007 for (j
= 0; j
< nargs
; ++j
)
5008 if (tpd2
.parms
[j
] != 0
5009 && tpd
.arg_uses_template_parms
[j
])
5012 error_n (input_location
, count
,
5013 "type %qT of template argument %qE depends "
5014 "on a template parameter",
5015 "type %qT of template argument %qE depends "
5016 "on template parameters",
5025 /* We should only get here once. */
5026 if (TREE_CODE (decl
) == TYPE_DECL
)
5027 gcc_assert (!COMPLETE_TYPE_P (type
));
5029 // Build the template decl.
5030 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5031 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5032 TREE_TYPE (tmpl
) = type
;
5033 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5034 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5035 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5036 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5038 /* Give template template parms a DECL_CONTEXT of the template
5039 for which they are a parameter. */
5040 for (i
= 0; i
< ntparms
; ++i
)
5042 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5043 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5044 DECL_CONTEXT (parm
) = tmpl
;
5048 /* We didn't register this in check_explicit_specialization so we could
5049 wait until the constraints were set. */
5050 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5052 associate_classtype_constraints (type
);
5054 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5055 = tree_cons (specargs
, tmpl
,
5056 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5057 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5059 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5060 inst
= TREE_CHAIN (inst
))
5062 tree instance
= TREE_VALUE (inst
);
5063 if (TYPE_P (instance
)
5064 ? (COMPLETE_TYPE_P (instance
)
5065 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5066 : DECL_TEMPLATE_INSTANTIATION (instance
))
5068 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5069 tree inst_decl
= (DECL_P (instance
)
5070 ? instance
: TYPE_NAME (instance
));
5073 else if (spec
== error_mark_node
)
5074 permerror (input_location
,
5075 "declaration of %qD ambiguates earlier template "
5076 "instantiation for %qD", decl
, inst_decl
);
5077 else if (TREE_VALUE (spec
) == tmpl
)
5078 permerror (input_location
,
5079 "partial specialization of %qD after instantiation "
5080 "of %qD", decl
, inst_decl
);
5087 /* PARM is a template parameter of some form; return the corresponding
5088 TEMPLATE_PARM_INDEX. */
5091 get_template_parm_index (tree parm
)
5093 if (TREE_CODE (parm
) == PARM_DECL
5094 || TREE_CODE (parm
) == CONST_DECL
)
5095 parm
= DECL_INITIAL (parm
);
5096 else if (TREE_CODE (parm
) == TYPE_DECL
5097 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5098 parm
= TREE_TYPE (parm
);
5099 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5100 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5101 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5102 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5103 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5107 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5108 parameter packs used by the template parameter PARM. */
5111 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5113 /* A type parm can't refer to another parm. */
5114 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5116 else if (TREE_CODE (parm
) == PARM_DECL
)
5118 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5123 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5125 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5126 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5127 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5130 /* PARM is a template parameter pack. Return any parameter packs used in
5131 its type or the type of any of its template parameters. If there are
5132 any such packs, it will be instantiated into a fixed template parameter
5133 list by partial instantiation rather than be fully deduced. */
5136 fixed_parameter_pack_p (tree parm
)
5138 /* This can only be true in a member template. */
5139 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5141 /* This can only be true for a parameter pack. */
5142 if (!template_parameter_pack_p (parm
))
5144 /* A type parm can't refer to another parm. */
5145 if (TREE_CODE (parm
) == TYPE_DECL
)
5148 tree parameter_packs
= NULL_TREE
;
5149 struct find_parameter_pack_data ppd
;
5150 ppd
.parameter_packs
= ¶meter_packs
;
5151 ppd
.visited
= new hash_set
<tree
>;
5152 ppd
.type_pack_expansion_p
= false;
5154 fixed_parameter_pack_p_1 (parm
, &ppd
);
5157 return parameter_packs
;
5160 /* Check that a template declaration's use of default arguments and
5161 parameter packs is not invalid. Here, PARMS are the template
5162 parameters. IS_PRIMARY is true if DECL is the thing declared by
5163 a primary template. IS_PARTIAL is true if DECL is a partial
5166 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5167 function template declaration or a friend class template
5168 declaration. In the function case, 1 indicates a declaration, 2
5169 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5170 emitted for extraneous default arguments.
5172 Returns TRUE if there were no errors found, FALSE otherwise. */
5175 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5176 bool is_partial
, int is_friend_decl
)
5179 int last_level_to_check
;
5181 bool no_errors
= true;
5185 A default template-argument shall not be specified in a
5186 function template declaration or a function template definition, nor
5187 in the template-parameter-list of the definition of a member of a
5190 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5191 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5192 /* You can't have a function template declaration in a local
5193 scope, nor you can you define a member of a class template in a
5197 if ((TREE_CODE (decl
) == TYPE_DECL
5199 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5200 || (TREE_CODE (decl
) == FUNCTION_DECL
5201 && LAMBDA_FUNCTION_P (decl
)))
5202 /* A lambda doesn't have an explicit declaration; don't complain
5203 about the parms of the enclosing class. */
5206 if (current_class_type
5207 && !TYPE_BEING_DEFINED (current_class_type
)
5208 && DECL_LANG_SPECIFIC (decl
)
5209 && DECL_DECLARES_FUNCTION_P (decl
)
5210 /* If this is either a friend defined in the scope of the class
5211 or a member function. */
5212 && (DECL_FUNCTION_MEMBER_P (decl
)
5213 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5214 : DECL_FRIEND_CONTEXT (decl
)
5215 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5217 /* And, if it was a member function, it really was defined in
5218 the scope of the class. */
5219 && (!DECL_FUNCTION_MEMBER_P (decl
)
5220 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5221 /* We already checked these parameters when the template was
5222 declared, so there's no need to do it again now. This function
5223 was defined in class scope, but we're processing its body now
5224 that the class is complete. */
5227 /* Core issue 226 (C++0x only): the following only applies to class
5230 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5234 If a template-parameter has a default template-argument, all
5235 subsequent template-parameters shall have a default
5236 template-argument supplied. */
5237 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5239 tree inner_parms
= TREE_VALUE (parm_level
);
5240 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5241 int seen_def_arg_p
= 0;
5244 for (i
= 0; i
< ntparms
; ++i
)
5246 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5248 if (parm
== error_mark_node
)
5251 if (TREE_PURPOSE (parm
))
5253 else if (seen_def_arg_p
5254 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5256 error ("no default argument for %qD", TREE_VALUE (parm
));
5257 /* For better subsequent error-recovery, we indicate that
5258 there should have been a default argument. */
5259 TREE_PURPOSE (parm
) = error_mark_node
;
5262 else if (!is_partial
5264 /* Don't complain about an enclosing partial
5266 && parm_level
== parms
5267 && TREE_CODE (decl
) == TYPE_DECL
5269 && template_parameter_pack_p (TREE_VALUE (parm
))
5270 /* A fixed parameter pack will be partially
5271 instantiated into a fixed length list. */
5272 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5274 /* A primary class template can only have one
5275 parameter pack, at the end of the template
5278 error ("parameter pack %q+D must be at the end of the"
5279 " template parameter list", TREE_VALUE (parm
));
5281 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5289 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5293 /* For an ordinary class template, default template arguments are
5294 allowed at the innermost level, e.g.:
5295 template <class T = int>
5297 but, in a partial specialization, they're not allowed even
5298 there, as we have in [temp.class.spec]:
5300 The template parameter list of a specialization shall not
5301 contain default template argument values.
5303 So, for a partial specialization, or for a function template
5304 (in C++98/C++03), we look at all of them. */
5307 /* But, for a primary class template that is not a partial
5308 specialization we look at all template parameters except the
5310 parms
= TREE_CHAIN (parms
);
5312 /* Figure out what error message to issue. */
5313 if (is_friend_decl
== 2)
5314 msg
= G_("default template arguments may not be used in function template "
5315 "friend re-declaration");
5316 else if (is_friend_decl
)
5317 msg
= G_("default template arguments may not be used in template "
5318 "friend declarations");
5319 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5320 msg
= G_("default template arguments may not be used in function templates "
5321 "without -std=c++11 or -std=gnu++11");
5322 else if (is_partial
)
5323 msg
= G_("default template arguments may not be used in "
5324 "partial specializations");
5325 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5326 msg
= G_("default argument for template parameter for class enclosing %qD");
5328 /* Per [temp.param]/9, "A default template-argument shall not be
5329 specified in the template-parameter-lists of the definition of
5330 a member of a class template that appears outside of the member's
5331 class.", thus if we aren't handling a member of a class template
5332 there is no need to examine the parameters. */
5335 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5336 /* If we're inside a class definition, there's no need to
5337 examine the parameters to the class itself. On the one
5338 hand, they will be checked when the class is defined, and,
5339 on the other, default arguments are valid in things like:
5340 template <class T = double>
5341 struct S { template <class U> void f(U); };
5342 Here the default argument for `S' has no bearing on the
5343 declaration of `f'. */
5344 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5346 /* Check everything. */
5347 last_level_to_check
= 0;
5349 for (parm_level
= parms
;
5350 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5351 parm_level
= TREE_CHAIN (parm_level
))
5353 tree inner_parms
= TREE_VALUE (parm_level
);
5357 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5358 for (i
= 0; i
< ntparms
; ++i
)
5360 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5363 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5368 if (is_friend_decl
== 2)
5375 /* Clear out the default argument so that we are not
5377 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5381 /* At this point, if we're still interested in issuing messages,
5382 they must apply to classes surrounding the object declared. */
5384 msg
= G_("default argument for template parameter for class "
5391 /* Worker for push_template_decl_real, called via
5392 for_each_template_parm. DATA is really an int, indicating the
5393 level of the parameters we are interested in. If T is a template
5394 parameter of that level, return nonzero. */
5397 template_parm_this_level_p (tree t
, void* data
)
5399 int this_level
= *(int *)data
;
5402 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5403 level
= TEMPLATE_PARM_LEVEL (t
);
5405 level
= TEMPLATE_TYPE_LEVEL (t
);
5406 return level
== this_level
;
5409 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5410 DATA is really an int, indicating the innermost outer level of parameters.
5411 If T is a template parameter of that level or further out, return
5415 template_parm_outer_level (tree t
, void *data
)
5417 int this_level
= *(int *)data
;
5420 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5421 level
= TEMPLATE_PARM_LEVEL (t
);
5423 level
= TEMPLATE_TYPE_LEVEL (t
);
5424 return level
<= this_level
;
5427 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5428 parameters given by current_template_args, or reuses a
5429 previously existing one, if appropriate. Returns the DECL, or an
5430 equivalent one, if it is replaced via a call to duplicate_decls.
5432 If IS_FRIEND is true, DECL is a friend declaration. */
5435 push_template_decl_real (tree decl
, bool is_friend
)
5443 int new_template_p
= 0;
5444 /* True if the template is a member template, in the sense of
5446 bool member_template_p
= false;
5448 if (decl
== error_mark_node
|| !current_template_parms
)
5449 return error_mark_node
;
5451 /* See if this is a partial specialization. */
5452 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5453 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5454 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5456 && DECL_LANG_SPECIFIC (decl
)
5457 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5458 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5460 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5464 /* For a friend, we want the context of the friend, not
5465 the type of which it is a friend. */
5466 ctx
= CP_DECL_CONTEXT (decl
);
5467 else if (CP_DECL_CONTEXT (decl
)
5468 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5469 /* In the case of a virtual function, we want the class in which
5471 ctx
= CP_DECL_CONTEXT (decl
);
5473 /* Otherwise, if we're currently defining some class, the DECL
5474 is assumed to be a member of the class. */
5475 ctx
= current_scope ();
5477 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5480 if (!DECL_CONTEXT (decl
))
5481 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5483 /* See if this is a primary template. */
5484 if (is_friend
&& ctx
5485 && uses_template_parms_level (ctx
, processing_template_decl
))
5486 /* A friend template that specifies a class context, i.e.
5487 template <typename T> friend void A<T>::f();
5490 else if (TREE_CODE (decl
) == TYPE_DECL
5491 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5494 is_primary
= template_parm_scope_p ();
5498 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5500 if (DECL_CLASS_SCOPE_P (decl
))
5501 member_template_p
= true;
5502 if (TREE_CODE (decl
) == TYPE_DECL
5503 && anon_aggrname_p (DECL_NAME (decl
)))
5505 error ("template class without a name");
5506 return error_mark_node
;
5508 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5510 if (member_template_p
)
5512 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5513 error ("member template %qD may not have virt-specifiers", decl
);
5515 if (DECL_DESTRUCTOR_P (decl
))
5519 A destructor shall not be a member template. */
5520 error ("destructor %qD declared as member template", decl
);
5521 return error_mark_node
;
5523 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5524 && (!prototype_p (TREE_TYPE (decl
))
5525 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5526 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5527 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5528 == void_list_node
)))
5530 /* [basic.stc.dynamic.allocation]
5532 An allocation function can be a function
5533 template. ... Template allocation functions shall
5534 have two or more parameters. */
5535 error ("invalid template declaration of %qD", decl
);
5536 return error_mark_node
;
5539 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5540 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5542 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5543 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5544 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5546 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5547 if (TREE_CODE (t
) == TYPE_DECL
)
5549 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5550 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5553 else if (TREE_CODE (decl
) == TYPE_DECL
5554 && TYPE_DECL_ALIAS_P (decl
))
5555 /* alias-declaration */
5556 gcc_assert (!DECL_ARTIFICIAL (decl
));
5557 else if (VAR_P (decl
))
5558 /* C++14 variable template. */;
5561 error ("template declaration of %q#D", decl
);
5562 return error_mark_node
;
5566 /* Check to see that the rules regarding the use of default
5567 arguments are not being violated. We check args for a friend
5568 functions when we know whether it's a definition, introducing
5569 declaration or re-declaration. */
5570 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5571 check_default_tmpl_args (decl
, current_template_parms
,
5572 is_primary
, is_partial
, is_friend
);
5574 /* Ensure that there are no parameter packs in the type of this
5575 declaration that have not been expanded. */
5576 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5578 /* Check each of the arguments individually to see if there are
5579 any bare parameter packs. */
5580 tree type
= TREE_TYPE (decl
);
5581 tree arg
= DECL_ARGUMENTS (decl
);
5582 tree argtype
= TYPE_ARG_TYPES (type
);
5584 while (arg
&& argtype
)
5586 if (!DECL_PACK_P (arg
)
5587 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5589 /* This is a PARM_DECL that contains unexpanded parameter
5590 packs. We have already complained about this in the
5591 check_for_bare_parameter_packs call, so just replace
5592 these types with ERROR_MARK_NODE. */
5593 TREE_TYPE (arg
) = error_mark_node
;
5594 TREE_VALUE (argtype
) = error_mark_node
;
5597 arg
= DECL_CHAIN (arg
);
5598 argtype
= TREE_CHAIN (argtype
);
5601 /* Check for bare parameter packs in the return type and the
5602 exception specifiers. */
5603 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5604 /* Errors were already issued, set return type to int
5605 as the frontend doesn't expect error_mark_node as
5607 TREE_TYPE (type
) = integer_type_node
;
5608 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5609 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5611 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5612 && TYPE_DECL_ALIAS_P (decl
))
5613 ? DECL_ORIGINAL_TYPE (decl
)
5614 : TREE_TYPE (decl
)))
5616 TREE_TYPE (decl
) = error_mark_node
;
5617 return error_mark_node
;
5621 return process_partial_specialization (decl
);
5623 args
= current_template_args ();
5626 || TREE_CODE (ctx
) == FUNCTION_DECL
5627 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5628 || (TREE_CODE (decl
) == TYPE_DECL
5629 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5630 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5632 if (DECL_LANG_SPECIFIC (decl
)
5633 && DECL_TEMPLATE_INFO (decl
)
5634 && DECL_TI_TEMPLATE (decl
))
5635 tmpl
= DECL_TI_TEMPLATE (decl
);
5636 /* If DECL is a TYPE_DECL for a class-template, then there won't
5637 be DECL_LANG_SPECIFIC. The information equivalent to
5638 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5639 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5640 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5641 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5643 /* Since a template declaration already existed for this
5644 class-type, we must be redeclaring it here. Make sure
5645 that the redeclaration is valid. */
5646 redeclare_class_template (TREE_TYPE (decl
),
5647 current_template_parms
,
5648 current_template_constraints ());
5649 /* We don't need to create a new TEMPLATE_DECL; just use the
5650 one we already had. */
5651 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5655 tmpl
= build_template_decl (decl
, current_template_parms
,
5659 if (DECL_LANG_SPECIFIC (decl
)
5660 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5662 /* A specialization of a member template of a template
5664 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5665 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5666 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5672 tree a
, t
, current
, parms
;
5674 tree tinfo
= get_template_info (decl
);
5678 error ("template definition of non-template %q#D", decl
);
5679 return error_mark_node
;
5682 tmpl
= TI_TEMPLATE (tinfo
);
5684 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5685 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5686 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5687 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5691 /* The declaration is a specialization of a member
5692 template, declared outside the class. Therefore, the
5693 innermost template arguments will be NULL, so we
5694 replace them with the arguments determined by the
5695 earlier call to check_explicit_specialization. */
5696 args
= DECL_TI_ARGS (decl
);
5699 = build_template_decl (decl
, current_template_parms
,
5701 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5702 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5703 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5704 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5705 DECL_TEMPLATE_INFO (new_tmpl
)
5706 = build_template_info (tmpl
, args
);
5708 register_specialization (new_tmpl
,
5709 most_general_template (tmpl
),
5715 /* Make sure the template headers we got make sense. */
5717 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5718 i
= TMPL_PARMS_DEPTH (parms
);
5719 if (TMPL_ARGS_DEPTH (args
) != i
)
5721 error ("expected %d levels of template parms for %q#D, got %d",
5722 i
, decl
, TMPL_ARGS_DEPTH (args
));
5723 DECL_INTERFACE_KNOWN (decl
) = 1;
5724 return error_mark_node
;
5727 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5729 a
= TMPL_ARGS_LEVEL (args
, i
);
5730 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5732 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5734 if (current
== decl
)
5735 error ("got %d template parameters for %q#D",
5736 TREE_VEC_LENGTH (a
), decl
);
5738 error ("got %d template parameters for %q#T",
5739 TREE_VEC_LENGTH (a
), current
);
5740 error (" but %d required", TREE_VEC_LENGTH (t
));
5741 /* Avoid crash in import_export_decl. */
5742 DECL_INTERFACE_KNOWN (decl
) = 1;
5743 return error_mark_node
;
5746 if (current
== decl
)
5748 else if (current
== NULL_TREE
)
5749 /* Can happen in erroneous input. */
5752 current
= get_containing_scope (current
);
5755 /* Check that the parms are used in the appropriate qualifying scopes
5756 in the declarator. */
5757 if (!comp_template_args
5759 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5761 error ("template arguments to %qD do not match original "
5762 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5763 if (!uses_template_parms (TI_ARGS (tinfo
)))
5764 inform (input_location
, "use %<template<>%> for"
5765 " an explicit specialization");
5766 /* Avoid crash in import_export_decl. */
5767 DECL_INTERFACE_KNOWN (decl
) = 1;
5768 return error_mark_node
;
5772 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5773 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5775 /* Push template declarations for global functions and types. Note
5776 that we do not try to push a global template friend declared in a
5777 template class; such a thing may well depend on the template
5778 parameters of the class. */
5779 if (new_template_p
&& !ctx
5780 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5782 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5783 if (tmpl
== error_mark_node
)
5784 return error_mark_node
;
5786 /* Hide template friend classes that haven't been declared yet. */
5787 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5789 DECL_ANTICIPATED (tmpl
) = 1;
5790 DECL_FRIEND_P (tmpl
) = 1;
5796 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5798 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5800 /* Give template template parms a DECL_CONTEXT of the template
5801 for which they are a parameter. */
5802 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5803 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5805 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5806 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5807 DECL_CONTEXT (parm
) = tmpl
;
5810 if (TREE_CODE (decl
) == TYPE_DECL
5811 && TYPE_DECL_ALIAS_P (decl
)
5812 && complex_alias_template_p (tmpl
))
5813 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5816 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5817 back to its most general template. If TMPL is a specialization,
5818 ARGS may only have the innermost set of arguments. Add the missing
5819 argument levels if necessary. */
5820 if (DECL_TEMPLATE_INFO (tmpl
))
5821 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5823 info
= build_template_info (tmpl
, args
);
5825 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5826 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5830 retrofit_lang_decl (decl
);
5831 if (DECL_LANG_SPECIFIC (decl
))
5832 DECL_TEMPLATE_INFO (decl
) = info
;
5835 if (flag_implicit_templates
5837 && TREE_PUBLIC (decl
)
5838 && VAR_OR_FUNCTION_DECL_P (decl
))
5839 /* Set DECL_COMDAT on template instantiations; if we force
5840 them to be emitted by explicit instantiation or -frepo,
5841 mark_needed will tell cgraph to do the right thing. */
5842 DECL_COMDAT (decl
) = true;
5844 return DECL_TEMPLATE_RESULT (tmpl
);
5848 push_template_decl (tree decl
)
5850 return push_template_decl_real (decl
, false);
5853 /* FN is an inheriting constructor that inherits from the constructor
5854 template INHERITED; turn FN into a constructor template with a matching
5858 add_inherited_template_parms (tree fn
, tree inherited
)
5861 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5862 inner_parms
= copy_node (inner_parms
);
5864 = tree_cons (size_int (processing_template_decl
+ 1),
5865 inner_parms
, current_template_parms
);
5866 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5867 tree args
= template_parms_to_args (parms
);
5868 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5869 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5870 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5871 DECL_ARTIFICIAL (tmpl
) = true;
5872 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5876 /* Called when a class template TYPE is redeclared with the indicated
5877 template PARMS, e.g.:
5879 template <class T> struct S;
5880 template <class T> struct S {}; */
5883 redeclare_class_template (tree type
, tree parms
, tree cons
)
5889 if (!TYPE_TEMPLATE_INFO (type
))
5891 error ("%qT is not a template type", type
);
5895 tmpl
= TYPE_TI_TEMPLATE (type
);
5896 if (!PRIMARY_TEMPLATE_P (tmpl
))
5897 /* The type is nested in some template class. Nothing to worry
5898 about here; there are no new template parameters for the nested
5904 error ("template specifiers not specified in declaration of %qD",
5909 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5910 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5912 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5914 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5915 "redeclared with %d template parameter",
5916 "redeclared with %d template parameters",
5917 TREE_VEC_LENGTH (parms
));
5918 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5919 "previous declaration %qD used %d template parameter",
5920 "previous declaration %qD used %d template parameters",
5921 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5925 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5932 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5933 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5936 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5937 if (error_operand_p (tmpl_parm
))
5940 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5941 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5942 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5944 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5946 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5947 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5948 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5949 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5950 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5951 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5952 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5953 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5954 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5956 error ("template parameter %q+#D", tmpl_parm
);
5957 error ("redeclared here as %q#D", parm
);
5961 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5963 /* We have in [temp.param]:
5965 A template-parameter may not be given default arguments
5966 by two different declarations in the same scope. */
5967 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5968 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5969 "original definition appeared here");
5973 if (parm_default
!= NULL_TREE
)
5974 /* Update the previous template parameters (which are the ones
5975 that will really count) with the new default value. */
5976 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5977 else if (tmpl_default
!= NULL_TREE
)
5978 /* Update the new parameters, too; they'll be used as the
5979 parameters for any members. */
5980 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5982 /* Give each template template parm in this redeclaration a
5983 DECL_CONTEXT of the template for which they are a parameter. */
5984 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5986 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5987 DECL_CONTEXT (parm
) = tmpl
;
5990 if (TREE_CODE (parm
) == TYPE_DECL
)
5991 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5994 // Cannot redeclare a class template with a different set of constraints.
5995 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5997 error_at (input_location
, "redeclaration %q#D with different "
5998 "constraints", tmpl
);
5999 inform (DECL_SOURCE_LOCATION (tmpl
),
6000 "original declaration appeared here");
6006 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6007 to be used when the caller has already checked
6008 (processing_template_decl
6009 && !instantiation_dependent_expression_p (expr)
6010 && potential_constant_expression (expr))
6011 and cleared processing_template_decl. */
6014 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6016 return tsubst_copy_and_build (expr
,
6019 /*in_decl=*/NULL_TREE
,
6020 /*function_p=*/false,
6021 /*integral_constant_expression_p=*/true);
6024 /* Simplify EXPR if it is a non-dependent expression. Returns the
6025 (possibly simplified) expression. */
6028 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6030 if (expr
== NULL_TREE
)
6033 /* If we're in a template, but EXPR isn't value dependent, simplify
6034 it. We're supposed to treat:
6036 template <typename T> void f(T[1 + 1]);
6037 template <typename T> void f(T[2]);
6039 as two declarations of the same function, for example. */
6040 if (processing_template_decl
6041 && is_nondependent_constant_expression (expr
))
6043 processing_template_decl_sentinel s
;
6044 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6050 instantiate_non_dependent_expr (tree expr
)
6052 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6055 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6056 an uninstantiated expression. */
6059 instantiate_non_dependent_or_null (tree expr
)
6061 if (expr
== NULL_TREE
)
6063 if (processing_template_decl
)
6065 if (!is_nondependent_constant_expression (expr
))
6069 processing_template_decl_sentinel s
;
6070 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6076 /* True iff T is a specialization of a variable template. */
6079 variable_template_specialization_p (tree t
)
6081 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6083 tree tmpl
= DECL_TI_TEMPLATE (t
);
6084 return variable_template_p (tmpl
);
6087 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6088 template declaration, or a TYPE_DECL for an alias declaration. */
6091 alias_type_or_template_p (tree t
)
6095 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6098 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6099 || DECL_ALIAS_TEMPLATE_P (t
));
6102 /* Return TRUE iff T is a specialization of an alias template. */
6105 alias_template_specialization_p (const_tree t
)
6107 /* It's an alias template specialization if it's an alias and its
6108 TYPE_NAME is a specialization of a primary template. */
6109 if (TYPE_ALIAS_P (t
))
6110 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6111 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6116 /* An alias template is complex from a SFINAE perspective if a template-id
6117 using that alias can be ill-formed when the expansion is not, as with
6118 the void_t template. We determine this by checking whether the
6119 expansion for the alias template uses all its template parameters. */
6121 struct uses_all_template_parms_data
6128 uses_all_template_parms_r (tree t
, void *data_
)
6130 struct uses_all_template_parms_data
&data
6131 = *(struct uses_all_template_parms_data
*)data_
;
6132 tree idx
= get_template_parm_index (t
);
6134 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6135 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6140 complex_alias_template_p (const_tree tmpl
)
6142 struct uses_all_template_parms_data data
;
6143 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6144 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6145 data
.level
= TMPL_PARMS_DEPTH (parms
);
6146 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6147 data
.seen
= XALLOCAVEC (bool, len
);
6148 for (int i
= 0; i
< len
; ++i
)
6149 data
.seen
[i
] = false;
6151 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6152 for (int i
= 0; i
< len
; ++i
)
6158 /* Return TRUE iff T is a specialization of a complex alias template with
6159 dependent template-arguments. */
6162 dependent_alias_template_spec_p (const_tree t
)
6164 if (!alias_template_specialization_p (t
))
6167 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6168 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6171 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6172 if (!any_dependent_template_arguments_p (args
))
6178 /* Return the number of innermost template parameters in TMPL. */
6181 num_innermost_template_parms (tree tmpl
)
6183 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6184 return TREE_VEC_LENGTH (parms
);
6187 /* Return either TMPL or another template that it is equivalent to under DR
6188 1286: An alias that just changes the name of a template is equivalent to
6189 the other template. */
6192 get_underlying_template (tree tmpl
)
6194 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6195 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6197 /* Determine if the alias is equivalent to an underlying template. */
6198 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6199 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6203 tree underlying
= TI_TEMPLATE (tinfo
);
6204 if (!PRIMARY_TEMPLATE_P (underlying
)
6205 || (num_innermost_template_parms (tmpl
)
6206 != num_innermost_template_parms (underlying
)))
6209 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6210 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6211 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6214 /* Alias is equivalent. Strip it and repeat. */
6221 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6222 must be a reference-to-function or a pointer-to-function type, as specified
6223 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6224 and check that the resulting function has external linkage. */
6227 convert_nontype_argument_function (tree type
, tree expr
,
6228 tsubst_flags_t complain
)
6232 linkage_kind linkage
;
6234 fn
= instantiate_type (type
, fns
, tf_none
);
6235 if (fn
== error_mark_node
)
6236 return error_mark_node
;
6238 if (value_dependent_expression_p (fn
))
6241 fn_no_ptr
= strip_fnptr_conv (fn
);
6242 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6243 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6244 if (BASELINK_P (fn_no_ptr
))
6245 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6247 /* [temp.arg.nontype]/1
6249 A template-argument for a non-type, non-template template-parameter
6252 -- the address of an object or function with external [C++11: or
6253 internal] linkage. */
6255 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6257 if (complain
& tf_error
)
6259 error ("%qE is not a valid template argument for type %qT",
6261 if (TYPE_PTR_P (type
))
6262 inform (input_location
, "it must be the address of a function "
6263 "with external linkage");
6265 inform (input_location
, "it must be the name of a function with "
6266 "external linkage");
6271 linkage
= decl_linkage (fn_no_ptr
);
6272 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6274 if (complain
& tf_error
)
6276 if (cxx_dialect
>= cxx11
)
6277 error ("%qE is not a valid template argument for type %qT "
6278 "because %qD has no linkage",
6279 expr
, type
, fn_no_ptr
);
6281 error ("%qE is not a valid template argument for type %qT "
6282 "because %qD does not have external linkage",
6283 expr
, type
, fn_no_ptr
);
6289 if (TYPE_REF_P (type
))
6291 if (REFERENCE_REF_P (fn
))
6292 fn
= TREE_OPERAND (fn
, 0);
6294 fn
= build_address (fn
);
6296 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6297 fn
= build_nop (type
, fn
);
6302 /* Subroutine of convert_nontype_argument.
6303 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6304 Emit an error otherwise. */
6307 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6308 tsubst_flags_t complain
)
6310 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6311 tree orig_expr
= expr
;
6313 if (null_ptr_cst_p (expr
))
6315 if (TREE_CODE (expr
) == PTRMEM_CST
6316 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6317 PTRMEM_CST_CLASS (expr
)))
6319 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6321 if (processing_template_decl
6322 && TREE_CODE (expr
) == ADDR_EXPR
6323 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6325 if (complain
& tf_error
)
6327 error_at (loc
, "%qE is not a valid template argument for type %qT",
6329 if (TREE_CODE (expr
) != PTRMEM_CST
)
6330 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6332 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6337 /* Returns TRUE iff the address of OP is value-dependent.
6339 14.6.2.4 [temp.dep.temp]:
6340 A non-integral non-type template-argument is dependent if its type is
6341 dependent or it has either of the following forms
6344 and contains a nested-name-specifier which specifies a class-name that
6345 names a dependent type.
6347 We generalize this to just say that the address of a member of a
6348 dependent class is value-dependent; the above doesn't cover the
6349 address of a static data member named with an unqualified-id. */
6352 has_value_dependent_address (tree op
)
6354 /* We could use get_inner_reference here, but there's no need;
6355 this is only relevant for template non-type arguments, which
6356 can only be expressed as &id-expression. */
6359 tree ctx
= CP_DECL_CONTEXT (op
);
6360 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6367 /* The next set of functions are used for providing helpful explanatory
6368 diagnostics for failed overload resolution. Their messages should be
6369 indented by two spaces for consistency with the messages in
6373 unify_success (bool /*explain_p*/)
6378 /* Other failure functions should call this one, to provide a single function
6379 for setting a breakpoint on. */
6382 unify_invalid (bool /*explain_p*/)
6388 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6391 inform (input_location
,
6392 " couldn't deduce template parameter %qD", parm
);
6393 return unify_invalid (explain_p
);
6397 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6400 inform (input_location
,
6401 " types %qT and %qT have incompatible cv-qualifiers",
6403 return unify_invalid (explain_p
);
6407 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6410 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6411 return unify_invalid (explain_p
);
6415 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6418 inform (input_location
,
6419 " template parameter %qD is not a parameter pack, but "
6422 return unify_invalid (explain_p
);
6426 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6429 inform (input_location
,
6430 " template argument %qE does not match "
6431 "pointer-to-member constant %qE",
6433 return unify_invalid (explain_p
);
6437 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6440 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6441 return unify_invalid (explain_p
);
6445 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6448 inform (input_location
,
6449 " inconsistent parameter pack deduction with %qT and %qT",
6451 return unify_invalid (explain_p
);
6455 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6460 inform (input_location
,
6461 " deduced conflicting types for parameter %qT (%qT and %qT)",
6462 parm
, first
, second
);
6464 inform (input_location
,
6465 " deduced conflicting values for non-type parameter "
6466 "%qE (%qE and %qE)", parm
, first
, second
);
6468 return unify_invalid (explain_p
);
6472 unify_vla_arg (bool explain_p
, tree arg
)
6475 inform (input_location
,
6476 " variable-sized array type %qT is not "
6477 "a valid template argument",
6479 return unify_invalid (explain_p
);
6483 unify_method_type_error (bool explain_p
, tree arg
)
6486 inform (input_location
,
6487 " member function type %qT is not a valid template argument",
6489 return unify_invalid (explain_p
);
6493 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6498 inform_n (input_location
, wanted
,
6499 " candidate expects at least %d argument, %d provided",
6500 " candidate expects at least %d arguments, %d provided",
6503 inform_n (input_location
, wanted
,
6504 " candidate expects %d argument, %d provided",
6505 " candidate expects %d arguments, %d provided",
6508 return unify_invalid (explain_p
);
6512 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6514 return unify_arity (explain_p
, have
, wanted
);
6518 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6519 bool least_p
= false)
6521 return unify_arity (explain_p
, have
, wanted
, least_p
);
6525 unify_arg_conversion (bool explain_p
, tree to_type
,
6526 tree from_type
, tree arg
)
6529 inform (cp_expr_loc_or_loc (arg
, input_location
),
6530 " cannot convert %qE (type %qT) to type %qT",
6531 arg
, from_type
, to_type
);
6532 return unify_invalid (explain_p
);
6536 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6537 tree parm
, tree arg
)
6542 case tbr_ambiguous_baseclass
:
6543 inform (input_location
, " %qT is an ambiguous base class of %qT",
6547 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6550 return unify_invalid (explain_p
);
6554 unify_inconsistent_template_template_parameters (bool explain_p
)
6557 inform (input_location
,
6558 " template parameters of a template template argument are "
6559 "inconsistent with other deduced template arguments");
6560 return unify_invalid (explain_p
);
6564 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6567 inform (input_location
,
6568 " can't deduce a template for %qT from non-template type %qT",
6570 return unify_invalid (explain_p
);
6574 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6577 inform (input_location
,
6578 " template argument %qE does not match %qE", arg
, parm
);
6579 return unify_invalid (explain_p
);
6582 /* Attempt to convert the non-type template parameter EXPR to the
6583 indicated TYPE. If the conversion is successful, return the
6584 converted value. If the conversion is unsuccessful, return
6585 NULL_TREE if we issued an error message, or error_mark_node if we
6586 did not. We issue error messages for out-and-out bad template
6587 parameters, but not simply because the conversion failed, since we
6588 might be just trying to do argument deduction. Both TYPE and EXPR
6589 must be non-dependent.
6591 The conversion follows the special rules described in
6592 [temp.arg.nontype], and it is much more strict than an implicit
6595 This function is called twice for each template argument (see
6596 lookup_template_class for a more accurate description of this
6597 problem). This means that we need to handle expressions which
6598 are not valid in a C++ source, but can be created from the
6599 first call (for instance, casts to perform conversions). These
6600 hacks can go away after we fix the double coercion problem. */
6603 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6606 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6607 tree orig_expr
= expr
;
6609 /* Detect immediately string literals as invalid non-type argument.
6610 This special-case is not needed for correctness (we would easily
6611 catch this later), but only to provide better diagnostic for this
6612 common user mistake. As suggested by DR 100, we do not mention
6613 linkage issues in the diagnostic as this is not the point. */
6614 /* FIXME we're making this OK. */
6615 if (TREE_CODE (expr
) == STRING_CST
)
6617 if (complain
& tf_error
)
6618 error ("%qE is not a valid template argument for type %qT "
6619 "because string literals can never be used in this context",
6624 /* Add the ADDR_EXPR now for the benefit of
6625 value_dependent_expression_p. */
6626 if (TYPE_PTROBV_P (type
)
6627 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6629 expr
= decay_conversion (expr
, complain
);
6630 if (expr
== error_mark_node
)
6631 return error_mark_node
;
6634 /* If we are in a template, EXPR may be non-dependent, but still
6635 have a syntactic, rather than semantic, form. For example, EXPR
6636 might be a SCOPE_REF, rather than the VAR_DECL to which the
6637 SCOPE_REF refers. Preserving the qualifying scope is necessary
6638 so that access checking can be performed when the template is
6639 instantiated -- but here we need the resolved form so that we can
6640 convert the argument. */
6641 bool non_dep
= false;
6642 if (TYPE_REF_OBJ_P (type
)
6643 && has_value_dependent_address (expr
))
6644 /* If we want the address and it's value-dependent, don't fold. */;
6645 else if (processing_template_decl
6646 && is_nondependent_constant_expression (expr
))
6648 if (error_operand_p (expr
))
6649 return error_mark_node
;
6650 expr_type
= TREE_TYPE (expr
);
6652 /* If the argument is non-dependent, perform any conversions in
6653 non-dependent context as well. */
6654 processing_template_decl_sentinel
s (non_dep
);
6656 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6658 if (value_dependent_expression_p (expr
))
6659 expr
= canonicalize_expr_argument (expr
, complain
);
6661 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6662 to a non-type argument of "nullptr". */
6663 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6664 expr
= fold_simple (convert (type
, expr
));
6666 /* In C++11, integral or enumeration non-type template arguments can be
6667 arbitrary constant expressions. Pointer and pointer to
6668 member arguments can be general constant expressions that evaluate
6669 to a null value, but otherwise still need to be of a specific form. */
6670 if (cxx_dialect
>= cxx11
)
6672 if (TREE_CODE (expr
) == PTRMEM_CST
)
6673 /* A PTRMEM_CST is already constant, and a valid template
6674 argument for a parameter of pointer to member type, we just want
6675 to leave it in that form rather than lower it to a
6677 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6678 || cxx_dialect
>= cxx17
)
6680 /* C++17: A template-argument for a non-type template-parameter shall
6681 be a converted constant expression (8.20) of the type of the
6682 template-parameter. */
6683 expr
= build_converted_constant_expr (type
, expr
, complain
);
6684 if (expr
== error_mark_node
)
6685 return error_mark_node
;
6686 expr
= maybe_constant_value (expr
);
6687 expr
= convert_from_reference (expr
);
6689 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6691 tree folded
= maybe_constant_value (expr
);
6692 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6693 : null_member_pointer_value_p (folded
))
6698 if (TYPE_REF_P (type
))
6699 expr
= mark_lvalue_use (expr
);
6701 expr
= mark_rvalue_use (expr
);
6703 /* HACK: Due to double coercion, we can get a
6704 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6705 which is the tree that we built on the first call (see
6706 below when coercing to reference to object or to reference to
6707 function). We just strip everything and get to the arg.
6708 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6710 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6712 tree probe_type
, probe
= expr
;
6713 if (REFERENCE_REF_P (probe
))
6714 probe
= TREE_OPERAND (probe
, 0);
6715 probe_type
= TREE_TYPE (probe
);
6716 if (TREE_CODE (probe
) == NOP_EXPR
)
6718 /* ??? Maybe we could use convert_from_reference here, but we
6719 would need to relax its constraints because the NOP_EXPR
6720 could actually change the type to something more cv-qualified,
6721 and this is not folded by convert_from_reference. */
6722 tree addr
= TREE_OPERAND (probe
, 0);
6723 if (TYPE_REF_P (probe_type
)
6724 && TREE_CODE (addr
) == ADDR_EXPR
6725 && TYPE_PTR_P (TREE_TYPE (addr
))
6726 && (same_type_ignoring_top_level_qualifiers_p
6727 (TREE_TYPE (probe_type
),
6728 TREE_TYPE (TREE_TYPE (addr
)))))
6730 expr
= TREE_OPERAND (addr
, 0);
6731 expr_type
= TREE_TYPE (probe_type
);
6736 /* [temp.arg.nontype]/5, bullet 1
6738 For a non-type template-parameter of integral or enumeration type,
6739 integral promotions (_conv.prom_) and integral conversions
6740 (_conv.integral_) are applied. */
6741 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6743 if (cxx_dialect
< cxx11
)
6745 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6746 t
= maybe_constant_value (t
);
6747 if (t
!= error_mark_node
)
6751 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6752 return error_mark_node
;
6754 /* Notice that there are constant expressions like '4 % 0' which
6755 do not fold into integer constants. */
6756 if (TREE_CODE (expr
) != INTEGER_CST
6757 && !value_dependent_expression_p (expr
))
6759 if (complain
& tf_error
)
6761 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6762 if (!require_potential_constant_expression (expr
))
6763 expr
= error_mark_node
;
6765 expr
= cxx_constant_value (expr
);
6766 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6767 inform (loc
, "in template argument for type %qT", type
);
6768 if (expr
== error_mark_node
)
6770 /* else cxx_constant_value complained but gave us
6771 a real constant, so go ahead. */
6772 if (TREE_CODE (expr
) != INTEGER_CST
)
6774 /* Some assemble time constant expressions like
6775 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6776 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6777 as we can emit them into .rodata initializers of
6778 variables, yet they can't fold into an INTEGER_CST at
6779 compile time. Refuse them here. */
6780 gcc_checking_assert (reduced_constant_expression_p (expr
));
6781 error_at (loc
, "template argument %qE for type %qT not "
6782 "a constant integer", expr
, type
);
6790 /* Avoid typedef problems. */
6791 if (TREE_TYPE (expr
) != type
)
6792 expr
= fold_convert (type
, expr
);
6794 /* [temp.arg.nontype]/5, bullet 2
6796 For a non-type template-parameter of type pointer to object,
6797 qualification conversions (_conv.qual_) and the array-to-pointer
6798 conversion (_conv.array_) are applied. */
6799 else if (TYPE_PTROBV_P (type
))
6801 tree decayed
= expr
;
6803 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6804 decay_conversion or an explicit cast. If it's a problematic cast,
6805 we'll complain about it below. */
6806 if (TREE_CODE (expr
) == NOP_EXPR
)
6810 if (TREE_CODE (probe
) == ADDR_EXPR
6811 && TYPE_PTR_P (TREE_TYPE (probe
)))
6814 expr_type
= TREE_TYPE (expr
);
6818 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6820 A template-argument for a non-type, non-template template-parameter
6821 shall be one of: [...]
6823 -- the name of a non-type template-parameter;
6824 -- the address of an object or function with external linkage, [...]
6825 expressed as "& id-expression" where the & is optional if the name
6826 refers to a function or array, or if the corresponding
6827 template-parameter is a reference.
6829 Here, we do not care about functions, as they are invalid anyway
6830 for a parameter of type pointer-to-object. */
6832 if (value_dependent_expression_p (expr
))
6833 /* Non-type template parameters are OK. */
6835 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6836 /* Null pointer values are OK in C++11. */;
6837 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6841 if (complain
& tf_error
)
6842 error ("%qD is not a valid template argument "
6843 "because %qD is a variable, not the address of "
6844 "a variable", orig_expr
, expr
);
6847 if (INDIRECT_TYPE_P (expr_type
))
6849 if (complain
& tf_error
)
6850 error ("%qE is not a valid template argument for %qT "
6851 "because it is not the address of a variable",
6855 /* Other values, like integer constants, might be valid
6856 non-type arguments of some other type. */
6857 return error_mark_node
;
6861 tree decl
= TREE_OPERAND (expr
, 0);
6865 if (complain
& tf_error
)
6866 error ("%qE is not a valid template argument of type %qT "
6867 "because %qE is not a variable", orig_expr
, type
, decl
);
6870 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6872 if (complain
& tf_error
)
6873 error ("%qE is not a valid template argument of type %qT "
6874 "because %qD does not have external linkage",
6875 orig_expr
, type
, decl
);
6878 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6879 && decl_linkage (decl
) == lk_none
)
6881 if (complain
& tf_error
)
6882 error ("%qE is not a valid template argument of type %qT "
6883 "because %qD has no linkage", orig_expr
, type
, decl
);
6886 /* C++17: For a non-type template-parameter of reference or pointer
6887 type, the value of the constant expression shall not refer to (or
6888 for a pointer type, shall not be the address of):
6889 * a subobject (4.5),
6890 * a temporary object (15.2),
6891 * a string literal (5.13.5),
6892 * the result of a typeid expression (8.2.8), or
6893 * a predefined __func__ variable (11.4.1). */
6894 else if (DECL_ARTIFICIAL (decl
))
6896 if (complain
& tf_error
)
6897 error ("the address of %qD is not a valid template argument",
6901 else if (!same_type_ignoring_top_level_qualifiers_p
6902 (strip_array_types (TREE_TYPE (type
)),
6903 strip_array_types (TREE_TYPE (decl
))))
6905 if (complain
& tf_error
)
6906 error ("the address of the %qT subobject of %qD is not a "
6907 "valid template argument", TREE_TYPE (type
), decl
);
6910 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6912 if (complain
& tf_error
)
6913 error ("the address of %qD is not a valid template argument "
6914 "because it does not have static storage duration",
6922 expr
= perform_qualification_conversions (type
, expr
);
6923 if (expr
== error_mark_node
)
6924 return error_mark_node
;
6926 /* [temp.arg.nontype]/5, bullet 3
6928 For a non-type template-parameter of type reference to object, no
6929 conversions apply. The type referred to by the reference may be more
6930 cv-qualified than the (otherwise identical) type of the
6931 template-argument. The template-parameter is bound directly to the
6932 template-argument, which must be an lvalue. */
6933 else if (TYPE_REF_OBJ_P (type
))
6935 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6937 return error_mark_node
;
6939 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6941 if (complain
& tf_error
)
6942 error ("%qE is not a valid template argument for type %qT "
6943 "because of conflicts in cv-qualification", expr
, type
);
6947 if (!lvalue_p (expr
))
6949 if (complain
& tf_error
)
6950 error ("%qE is not a valid template argument for type %qT "
6951 "because it is not an lvalue", expr
, type
);
6955 /* [temp.arg.nontype]/1
6957 A template-argument for a non-type, non-template template-parameter
6958 shall be one of: [...]
6960 -- the address of an object or function with external linkage. */
6961 if (INDIRECT_REF_P (expr
)
6962 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6964 expr
= TREE_OPERAND (expr
, 0);
6967 if (complain
& tf_error
)
6968 error ("%q#D is not a valid template argument for type %qT "
6969 "because a reference variable does not have a constant "
6970 "address", expr
, type
);
6975 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6976 && value_dependent_expression_p (expr
))
6977 /* OK, dependent reference. We don't want to ask whether a DECL is
6978 itself value-dependent, since what we want here is its address. */;
6983 if (complain
& tf_error
)
6984 error ("%qE is not a valid template argument for type %qT "
6985 "because it is not an object with linkage",
6990 /* DR 1155 allows internal linkage in C++11 and up. */
6991 linkage_kind linkage
= decl_linkage (expr
);
6992 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6994 if (complain
& tf_error
)
6995 error ("%qE is not a valid template argument for type %qT "
6996 "because object %qD does not have linkage",
7001 expr
= build_address (expr
);
7004 if (!same_type_p (type
, TREE_TYPE (expr
)))
7005 expr
= build_nop (type
, expr
);
7007 /* [temp.arg.nontype]/5, bullet 4
7009 For a non-type template-parameter of type pointer to function, only
7010 the function-to-pointer conversion (_conv.func_) is applied. If the
7011 template-argument represents a set of overloaded functions (or a
7012 pointer to such), the matching function is selected from the set
7014 else if (TYPE_PTRFN_P (type
))
7016 /* If the argument is a template-id, we might not have enough
7017 context information to decay the pointer. */
7018 if (!type_unknown_p (expr_type
))
7020 expr
= decay_conversion (expr
, complain
);
7021 if (expr
== error_mark_node
)
7022 return error_mark_node
;
7025 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7026 /* Null pointer values are OK in C++11. */
7027 return perform_qualification_conversions (type
, expr
);
7029 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7030 if (!expr
|| expr
== error_mark_node
)
7033 /* [temp.arg.nontype]/5, bullet 5
7035 For a non-type template-parameter of type reference to function, no
7036 conversions apply. If the template-argument represents a set of
7037 overloaded functions, the matching function is selected from the set
7039 else if (TYPE_REFFN_P (type
))
7041 if (TREE_CODE (expr
) == ADDR_EXPR
)
7043 if (complain
& tf_error
)
7045 error ("%qE is not a valid template argument for type %qT "
7046 "because it is a pointer", expr
, type
);
7047 inform (input_location
, "try using %qE instead",
7048 TREE_OPERAND (expr
, 0));
7053 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7054 if (!expr
|| expr
== error_mark_node
)
7057 /* [temp.arg.nontype]/5, bullet 6
7059 For a non-type template-parameter of type pointer to member function,
7060 no conversions apply. If the template-argument represents a set of
7061 overloaded member functions, the matching member function is selected
7062 from the set (_over.over_). */
7063 else if (TYPE_PTRMEMFUNC_P (type
))
7065 expr
= instantiate_type (type
, expr
, tf_none
);
7066 if (expr
== error_mark_node
)
7067 return error_mark_node
;
7069 /* [temp.arg.nontype] bullet 1 says the pointer to member
7070 expression must be a pointer-to-member constant. */
7071 if (!value_dependent_expression_p (expr
)
7072 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7075 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7076 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7077 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7078 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7080 /* [temp.arg.nontype]/5, bullet 7
7082 For a non-type template-parameter of type pointer to data member,
7083 qualification conversions (_conv.qual_) are applied. */
7084 else if (TYPE_PTRDATAMEM_P (type
))
7086 /* [temp.arg.nontype] bullet 1 says the pointer to member
7087 expression must be a pointer-to-member constant. */
7088 if (!value_dependent_expression_p (expr
)
7089 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7092 expr
= perform_qualification_conversions (type
, expr
);
7093 if (expr
== error_mark_node
)
7096 else if (NULLPTR_TYPE_P (type
))
7098 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7100 if (complain
& tf_error
)
7101 error ("%qE is not a valid template argument for type %qT "
7102 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7107 /* A template non-type parameter must be one of the above. */
7111 /* Sanity check: did we actually convert the argument to the
7113 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7114 (type
, TREE_TYPE (expr
)));
7115 return convert_from_reference (expr
);
7118 /* Subroutine of coerce_template_template_parms, which returns 1 if
7119 PARM_PARM and ARG_PARM match using the rule for the template
7120 parameters of template template parameters. Both PARM and ARG are
7121 template parameters; the rest of the arguments are the same as for
7122 coerce_template_template_parms.
7125 coerce_template_template_parm (tree parm
,
7127 tsubst_flags_t complain
,
7131 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7132 || parm
== NULL_TREE
|| error_operand_p (parm
))
7135 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7138 switch (TREE_CODE (parm
))
7141 /* We encounter instantiations of templates like
7142 template <template <template <class> class> class TT>
7145 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7146 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7148 if (!coerce_template_template_parms
7149 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7155 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7156 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7157 /* Argument is a parameter pack but parameter is not. */
7162 /* The tsubst call is used to handle cases such as
7164 template <int> class C {};
7165 template <class T, template <T> class TT> class D {};
7168 i.e. the parameter list of TT depends on earlier parameters. */
7169 if (!uses_template_parms (TREE_TYPE (arg
)))
7171 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7172 if (!uses_template_parms (t
)
7173 && !same_type_p (t
, TREE_TYPE (arg
)))
7177 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7178 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7179 /* Argument is a parameter pack but parameter is not. */
7191 /* Coerce template argument list ARGLIST for use with template
7192 template-parameter TEMPL. */
7195 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7196 tsubst_flags_t complain
)
7198 /* Consider an example where a template template parameter declared as
7200 template <class T, class U = std::allocator<T> > class TT
7202 The template parameter level of T and U are one level larger than
7203 of TT. To proper process the default argument of U, say when an
7204 instantiation `TT<int>' is seen, we need to build the full
7205 arguments containing {int} as the innermost level. Outer levels,
7206 available when not appearing as default template argument, can be
7207 obtained from the arguments of the enclosing template.
7209 Suppose that TT is later substituted with std::vector. The above
7210 instantiation is `TT<int, std::allocator<T> >' with TT at
7211 level 1, and T at level 2, while the template arguments at level 1
7212 becomes {std::vector} and the inner level 2 is {int}. */
7214 tree outer
= DECL_CONTEXT (templ
);
7217 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7218 /* We want arguments for the partial specialization, not arguments for
7219 the primary template. */
7220 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7222 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7224 else if (current_template_parms
)
7226 /* This is an argument of the current template, so we haven't set
7227 DECL_CONTEXT yet. */
7228 tree relevant_template_parms
;
7230 /* Parameter levels that are greater than the level of the given
7231 template template parm are irrelevant. */
7232 relevant_template_parms
= current_template_parms
;
7233 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7234 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7235 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7237 outer
= template_parms_to_args (relevant_template_parms
);
7241 arglist
= add_to_template_args (outer
, arglist
);
7243 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7244 return coerce_template_parms (parmlist
, arglist
, templ
,
7246 /*require_all_args=*/true,
7247 /*use_default_args=*/true);
7250 /* A cache of template template parameters with match-all default
7252 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7254 store_defaulted_ttp (tree v
, tree t
)
7256 if (!defaulted_ttp_cache
)
7257 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7258 defaulted_ttp_cache
->put (v
, t
);
7261 lookup_defaulted_ttp (tree v
)
7263 if (defaulted_ttp_cache
)
7264 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7269 /* T is a bound template template-parameter. Copy its arguments into default
7270 arguments of the template template-parameter's template parameters. */
7273 add_defaults_to_ttp (tree otmpl
)
7275 if (tree c
= lookup_defaulted_ttp (otmpl
))
7278 tree ntmpl
= copy_node (otmpl
);
7280 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7281 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7282 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7283 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7284 TYPE_NAME (ntype
) = ntmpl
;
7285 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7287 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7288 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7289 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7290 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7292 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7293 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7294 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7295 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7296 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7298 tree o
= TREE_VEC_ELT (vec
, i
);
7299 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7301 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7302 TREE_PURPOSE (n
) = any_targ_node
;
7306 store_defaulted_ttp (otmpl
, ntmpl
);
7310 /* ARG is a bound potential template template-argument, and PARGS is a list
7311 of arguments for the corresponding template template-parameter. Adjust
7312 PARGS as appropriate for application to ARG's template, and if ARG is a
7313 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7314 arguments to the template template parameter. */
7317 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7319 ++processing_template_decl
;
7320 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7321 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7323 /* When comparing two template template-parameters in partial ordering,
7324 rewrite the one currently being used as an argument to have default
7325 arguments for all parameters. */
7326 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7327 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7328 if (pargs
!= error_mark_node
)
7329 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7330 TYPE_TI_ARGS (arg
));
7335 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7336 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7337 /*require_all*/true,
7338 /*use_default*/true);
7340 --processing_template_decl
;
7344 /* Subroutine of unify for the case when PARM is a
7345 BOUND_TEMPLATE_TEMPLATE_PARM. */
7348 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7351 tree parmvec
= TYPE_TI_ARGS (parm
);
7352 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7354 /* The template template parm might be variadic and the argument
7355 not, so flatten both argument lists. */
7356 parmvec
= expand_template_argument_pack (parmvec
);
7357 argvec
= expand_template_argument_pack (argvec
);
7361 /* In keeping with P0522R0, adjust P's template arguments
7362 to apply to A's template; then flatten it again. */
7363 tree nparmvec
= parmvec
;
7364 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7365 nparmvec
= expand_template_argument_pack (nparmvec
);
7367 if (unify (tparms
, targs
, nparmvec
, argvec
,
7368 UNIFY_ALLOW_NONE
, explain_p
))
7371 /* If the P0522 adjustment eliminated a pack expansion, deduce
7374 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7375 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7376 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7381 /* Deduce arguments T, i from TT<T> or TT<i>.
7382 We check each element of PARMVEC and ARGVEC individually
7383 rather than the whole TREE_VEC since they can have
7384 different number of elements, which is allowed under N2555. */
7386 int len
= TREE_VEC_LENGTH (parmvec
);
7388 /* Check if the parameters end in a pack, making them
7390 int parm_variadic_p
= 0;
7392 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7393 parm_variadic_p
= 1;
7395 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7396 /* If the template argument list of P contains a pack
7397 expansion that is not the last template argument, the
7398 entire template argument list is a non-deduced
7400 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7401 return unify_success (explain_p
);
7403 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7404 return unify_too_few_arguments (explain_p
,
7405 TREE_VEC_LENGTH (argvec
), len
);
7407 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7408 if (unify (tparms
, targs
,
7409 TREE_VEC_ELT (parmvec
, i
),
7410 TREE_VEC_ELT (argvec
, i
),
7411 UNIFY_ALLOW_NONE
, explain_p
))
7415 && unify_pack_expansion (tparms
, targs
,
7418 /*subr=*/true, explain_p
))
7425 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7426 template template parameters. Both PARM_PARMS and ARG_PARMS are
7427 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7430 Consider the example:
7431 template <class T> class A;
7432 template<template <class U> class TT> class B;
7434 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7435 the parameters to A, and OUTER_ARGS contains A. */
7438 coerce_template_template_parms (tree parm_parms
,
7440 tsubst_flags_t complain
,
7444 int nparms
, nargs
, i
;
7448 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7449 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7451 nparms
= TREE_VEC_LENGTH (parm_parms
);
7452 nargs
= TREE_VEC_LENGTH (arg_parms
);
7456 /* P0522R0: A template template-parameter P is at least as specialized as
7457 a template template-argument A if, given the following rewrite to two
7458 function templates, the function template corresponding to P is at
7459 least as specialized as the function template corresponding to A
7460 according to the partial ordering rules for function templates
7461 ([temp.func.order]). Given an invented class template X with the
7462 template parameter list of A (including default arguments):
7464 * Each of the two function templates has the same template parameters,
7465 respectively, as P or A.
7467 * Each function template has a single function parameter whose type is
7468 a specialization of X with template arguments corresponding to the
7469 template parameters from the respective function template where, for
7470 each template parameter PP in the template parameter list of the
7471 function template, a corresponding template argument AA is formed. If
7472 PP declares a parameter pack, then AA is the pack expansion
7473 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7475 If the rewrite produces an invalid type, then P is not at least as
7476 specialized as A. */
7478 /* So coerce P's args to apply to A's parms, and then deduce between A's
7479 args and the converted args. If that succeeds, A is at least as
7480 specialized as P, so they match.*/
7481 tree pargs
= template_parms_level_to_args (parm_parms
);
7482 ++processing_template_decl
;
7483 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7484 /*require_all*/true, /*use_default*/true);
7485 --processing_template_decl
;
7486 if (pargs
!= error_mark_node
)
7488 tree targs
= make_tree_vec (nargs
);
7489 tree aargs
= template_parms_level_to_args (arg_parms
);
7490 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7496 /* Determine whether we have a parameter pack at the end of the
7497 template template parameter's template parameter list. */
7498 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7500 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7502 if (error_operand_p (parm
))
7505 switch (TREE_CODE (parm
))
7509 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7514 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7524 && !(variadic_p
&& nargs
>= nparms
- 1))
7527 /* Check all of the template parameters except the parameter pack at
7528 the end (if any). */
7529 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7531 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7532 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7535 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7536 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7538 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7546 /* Check each of the template parameters in the template
7547 argument against the template parameter pack at the end of
7548 the template template parameter. */
7549 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7552 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7554 for (; i
< nargs
; ++i
)
7556 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7559 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7561 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7570 /* Verifies that the deduced template arguments (in TARGS) for the
7571 template template parameters (in TPARMS) represent valid bindings,
7572 by comparing the template parameter list of each template argument
7573 to the template parameter list of its corresponding template
7574 template parameter, in accordance with DR150. This
7575 routine can only be called after all template arguments have been
7576 deduced. It will return TRUE if all of the template template
7577 parameter bindings are okay, FALSE otherwise. */
7579 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7581 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7584 /* We're dealing with template parms in this process. */
7585 ++processing_template_decl
;
7587 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7589 for (i
= 0; i
< ntparms
; ++i
)
7591 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7592 tree targ
= TREE_VEC_ELT (targs
, i
);
7594 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7596 tree packed_args
= NULL_TREE
;
7599 if (ARGUMENT_PACK_P (targ
))
7601 /* Look inside the argument pack. */
7602 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7603 len
= TREE_VEC_LENGTH (packed_args
);
7606 for (idx
= 0; idx
< len
; ++idx
)
7608 tree targ_parms
= NULL_TREE
;
7611 /* Extract the next argument from the argument
7613 targ
= TREE_VEC_ELT (packed_args
, idx
);
7615 if (PACK_EXPANSION_P (targ
))
7616 /* Look at the pattern of the pack expansion. */
7617 targ
= PACK_EXPANSION_PATTERN (targ
);
7619 /* Extract the template parameters from the template
7621 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7622 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7623 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7624 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7626 /* Verify that we can coerce the template template
7627 parameters from the template argument to the template
7628 parameter. This requires an exact match. */
7630 && !coerce_template_template_parms
7631 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7646 --processing_template_decl
;
7650 /* Since type attributes aren't mangled, we need to strip them from
7651 template type arguments. */
7654 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7656 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7658 bool removed_attributes
= false;
7659 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7660 if (removed_attributes
7661 && (complain
& tf_warning
))
7662 warning (OPT_Wignored_attributes
,
7663 "ignoring attributes on template argument %qT", arg
);
7667 /* And from inside dependent non-type arguments like sizeof(Type). */
7670 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7672 if (!arg
|| arg
== error_mark_node
)
7674 bool removed_attributes
= false;
7675 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7676 if (removed_attributes
7677 && (complain
& tf_warning
))
7678 warning (OPT_Wignored_attributes
,
7679 "ignoring attributes in template argument %qE", arg
);
7683 // A template declaration can be substituted for a constrained
7684 // template template parameter only when the argument is more
7685 // constrained than the parameter.
7687 is_compatible_template_arg (tree parm
, tree arg
)
7689 tree parm_cons
= get_constraints (parm
);
7691 /* For now, allow constrained template template arguments
7692 and unconstrained template template parameters. */
7693 if (parm_cons
== NULL_TREE
)
7696 tree arg_cons
= get_constraints (arg
);
7698 // If the template parameter is constrained, we need to rewrite its
7699 // constraints in terms of the ARG's template parameters. This ensures
7700 // that all of the template parameter types will have the same depth.
7702 // Note that this is only valid when coerce_template_template_parm is
7703 // true for the innermost template parameters of PARM and ARG. In other
7704 // words, because coercion is successful, this conversion will be valid.
7707 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7708 parm_cons
= tsubst_constraint_info (parm_cons
,
7709 INNERMOST_TEMPLATE_ARGS (args
),
7710 tf_none
, NULL_TREE
);
7711 if (parm_cons
== error_mark_node
)
7715 return subsumes (parm_cons
, arg_cons
);
7718 // Convert a placeholder argument into a binding to the original
7719 // parameter. The original parameter is saved as the TREE_TYPE of
7722 convert_wildcard_argument (tree parm
, tree arg
)
7724 TREE_TYPE (arg
) = parm
;
7728 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7729 because one of them is dependent. But we need to represent the
7730 conversion for the benefit of cp_tree_equal. */
7733 maybe_convert_nontype_argument (tree type
, tree arg
)
7735 /* Auto parms get no conversion. */
7736 if (type_uses_auto (type
))
7738 /* We don't need or want to add this conversion now if we're going to use the
7739 argument for deduction. */
7740 if (value_dependent_expression_p (arg
))
7743 type
= cv_unqualified (type
);
7744 tree argtype
= TREE_TYPE (arg
);
7745 if (same_type_p (type
, argtype
))
7748 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7749 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7753 /* Convert the indicated template ARG as necessary to match the
7754 indicated template PARM. Returns the converted ARG, or
7755 error_mark_node if the conversion was unsuccessful. Error and
7756 warning messages are issued under control of COMPLAIN. This
7757 conversion is for the Ith parameter in the parameter list. ARGS is
7758 the full set of template arguments deduced so far. */
7761 convert_template_argument (tree parm
,
7764 tsubst_flags_t complain
,
7770 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7772 if (parm
== error_mark_node
)
7773 return error_mark_node
;
7775 /* Trivially convert placeholders. */
7776 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7777 return convert_wildcard_argument (parm
, arg
);
7779 if (arg
== any_targ_node
)
7782 if (TREE_CODE (arg
) == TREE_LIST
7783 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7785 /* The template argument was the name of some
7786 member function. That's usually
7787 invalid, but static members are OK. In any
7788 case, grab the underlying fields/functions
7789 and issue an error later if required. */
7790 orig_arg
= TREE_VALUE (arg
);
7791 TREE_TYPE (arg
) = unknown_type_node
;
7796 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7797 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7798 || requires_tmpl_type
);
7800 /* When determining whether an argument pack expansion is a template,
7801 look at the pattern. */
7802 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7803 arg
= PACK_EXPANSION_PATTERN (arg
);
7805 /* Deal with an injected-class-name used as a template template arg. */
7806 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7808 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7809 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7811 if (cxx_dialect
>= cxx11
)
7812 /* OK under DR 1004. */;
7813 else if (complain
& tf_warning_or_error
)
7814 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7815 " used as template template argument", TYPE_NAME (arg
));
7816 else if (flag_pedantic_errors
)
7824 ((TREE_CODE (arg
) == TEMPLATE_DECL
7825 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7826 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7827 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7828 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7831 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7832 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7833 arg
= TYPE_STUB_DECL (arg
);
7835 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7837 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7838 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7840 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7842 if (complain
& tf_error
)
7843 error ("invalid use of destructor %qE as a type", orig_arg
);
7844 return error_mark_node
;
7847 permerror (input_location
,
7848 "to refer to a type member of a template parameter, "
7849 "use %<typename %E%>", orig_arg
);
7851 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7852 TREE_OPERAND (arg
, 1),
7858 if (is_type
!= requires_type
)
7862 if (complain
& tf_error
)
7864 error ("type/value mismatch at argument %d in template "
7865 "parameter list for %qD",
7868 inform (input_location
,
7869 " expected a constant of type %qT, got %qT",
7871 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7872 else if (requires_tmpl_type
)
7873 inform (input_location
,
7874 " expected a class template, got %qE", orig_arg
);
7876 inform (input_location
,
7877 " expected a type, got %qE", orig_arg
);
7880 return error_mark_node
;
7882 if (is_tmpl_type
^ requires_tmpl_type
)
7884 if (in_decl
&& (complain
& tf_error
))
7886 error ("type/value mismatch at argument %d in template "
7887 "parameter list for %qD",
7890 inform (input_location
,
7891 " expected a type, got %qT", DECL_NAME (arg
));
7893 inform (input_location
,
7894 " expected a class template, got %qT", orig_arg
);
7896 return error_mark_node
;
7899 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7900 /* We already did the appropriate conversion when packing args. */
7904 if (requires_tmpl_type
)
7906 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7907 /* The number of argument required is not known yet.
7908 Just accept it for now. */
7912 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7915 /* Strip alias templates that are equivalent to another
7917 arg
= get_underlying_template (arg
);
7918 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7920 if (coerce_template_template_parms (parmparm
, argparm
,
7926 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7928 if (val
!= error_mark_node
)
7930 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7931 val
= TREE_TYPE (val
);
7932 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7933 val
= make_pack_expansion (val
, complain
);
7938 if (in_decl
&& (complain
& tf_error
))
7940 error ("type/value mismatch at argument %d in "
7941 "template parameter list for %qD",
7943 inform (input_location
,
7944 " expected a template of type %qD, got %qT",
7948 val
= error_mark_node
;
7951 // Check that the constraints are compatible before allowing the
7953 if (val
!= error_mark_node
)
7954 if (!is_compatible_template_arg (parm
, arg
))
7956 if (in_decl
&& (complain
& tf_error
))
7958 error ("constraint mismatch at argument %d in "
7959 "template parameter list for %qD",
7961 inform (input_location
, " expected %qD but got %qD",
7964 val
= error_mark_node
;
7970 /* We only form one instance of each template specialization.
7971 Therefore, if we use a non-canonical variant (i.e., a
7972 typedef), any future messages referring to the type will use
7973 the typedef, which is confusing if those future uses do not
7974 themselves also use the typedef. */
7976 val
= canonicalize_type_argument (val
, complain
);
7980 tree t
= TREE_TYPE (parm
);
7982 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
7983 > TMPL_ARGS_DEPTH (args
))
7984 /* We don't have enough levels of args to do any substitution. This
7985 can happen in the context of -fnew-ttp-matching. */;
7986 else if (tree a
= type_uses_auto (t
))
7988 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7989 if (t
== error_mark_node
)
7990 return error_mark_node
;
7993 t
= tsubst (t
, args
, complain
, in_decl
);
7995 if (invalid_nontype_parm_type_p (t
, complain
))
7996 return error_mark_node
;
7998 if (!type_dependent_expression_p (orig_arg
)
7999 && !uses_template_parms (t
))
8000 /* We used to call digest_init here. However, digest_init
8001 will report errors, which we don't want when complain
8002 is zero. More importantly, digest_init will try too
8003 hard to convert things: for example, `0' should not be
8004 converted to pointer type at this point according to
8005 the standard. Accepting this is not merely an
8006 extension, since deciding whether or not these
8007 conversions can occur is part of determining which
8008 function template to call, or whether a given explicit
8009 argument specification is valid. */
8010 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8013 val
= canonicalize_expr_argument (orig_arg
, complain
);
8014 val
= maybe_convert_nontype_argument (t
, val
);
8018 if (val
== NULL_TREE
)
8019 val
= error_mark_node
;
8020 else if (val
== error_mark_node
&& (complain
& tf_error
))
8021 error ("could not convert template argument %qE from %qT to %qT",
8022 orig_arg
, TREE_TYPE (orig_arg
), t
);
8024 if (INDIRECT_REF_P (val
))
8026 /* Reject template arguments that are references to built-in
8027 functions with no library fallbacks. */
8028 const_tree inner
= TREE_OPERAND (val
, 0);
8029 const_tree innertype
= TREE_TYPE (inner
);
8031 && TYPE_REF_P (innertype
)
8032 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8033 && TREE_OPERAND_LENGTH (inner
) > 0
8034 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8035 return error_mark_node
;
8038 if (TREE_CODE (val
) == SCOPE_REF
)
8040 /* Strip typedefs from the SCOPE_REF. */
8041 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8042 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8044 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8045 QUALIFIED_NAME_IS_TEMPLATE (val
));
8052 /* Coerces the remaining template arguments in INNER_ARGS (from
8053 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8054 Returns the coerced argument pack. PARM_IDX is the position of this
8055 parameter in the template parameter list. ARGS is the original
8056 template argument list. */
8058 coerce_template_parameter_pack (tree parms
,
8066 tsubst_flags_t complain
)
8068 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8069 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8072 tree packed_parms
= NULL_TREE
;
8074 if (arg_idx
> nargs
)
8077 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8079 /* When the template parameter is a non-type template parameter pack
8080 or template template parameter pack whose type or template
8081 parameters use parameter packs, we know exactly how many arguments
8082 we are looking for. Build a vector of the instantiated decls for
8083 these template parameters in PACKED_PARMS. */
8084 /* We can't use make_pack_expansion here because it would interpret a
8085 _DECL as a use rather than a declaration. */
8086 tree decl
= TREE_VALUE (parm
);
8087 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8088 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8089 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8090 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8092 TREE_VEC_LENGTH (args
)--;
8093 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8094 TREE_VEC_LENGTH (args
)++;
8096 if (packed_parms
== error_mark_node
)
8097 return error_mark_node
;
8099 /* If we're doing a partial instantiation of a member template,
8100 verify that all of the types used for the non-type
8101 template parameter pack are, in fact, valid for non-type
8102 template parameters. */
8104 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8106 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8107 for (j
= 0; j
< len
; ++j
)
8109 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8110 if (invalid_nontype_parm_type_p (t
, complain
))
8111 return error_mark_node
;
8113 /* We don't know how many args we have yet, just
8114 use the unconverted ones for now. */
8118 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8120 /* Check if we have a placeholder pack, which indicates we're
8121 in the context of a introduction list. In that case we want
8122 to match this pack to the single placeholder. */
8123 else if (arg_idx
< nargs
8124 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8125 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8127 nargs
= arg_idx
+ 1;
8128 packed_args
= make_tree_vec (1);
8131 packed_args
= make_tree_vec (nargs
- arg_idx
);
8133 /* Convert the remaining arguments, which will be a part of the
8134 parameter pack "parm". */
8135 int first_pack_arg
= arg_idx
;
8136 for (; arg_idx
< nargs
; ++arg_idx
)
8138 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8139 tree actual_parm
= TREE_VALUE (parm
);
8140 int pack_idx
= arg_idx
- first_pack_arg
;
8144 /* Once we've packed as many args as we have types, stop. */
8145 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8147 else if (PACK_EXPANSION_P (arg
))
8148 /* We don't know how many args we have yet, just
8149 use the unconverted ones for now. */
8152 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8155 if (arg
== error_mark_node
)
8157 if (complain
& tf_error
)
8158 error ("template argument %d is invalid", arg_idx
+ 1);
8161 arg
= convert_template_argument (actual_parm
,
8162 arg
, new_args
, complain
, parm_idx
,
8164 if (arg
== error_mark_node
)
8166 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8169 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8170 && TREE_VEC_LENGTH (packed_args
) > 0)
8172 if (complain
& tf_error
)
8173 error ("wrong number of template arguments (%d, should be %d)",
8174 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8175 return error_mark_node
;
8178 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8179 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8180 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8183 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8184 TREE_CONSTANT (argument_pack
) = 1;
8187 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8189 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8190 TREE_VEC_LENGTH (packed_args
));
8191 return argument_pack
;
8194 /* Returns the number of pack expansions in the template argument vector
8198 pack_expansion_args_count (tree args
)
8203 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8205 tree elt
= TREE_VEC_ELT (args
, i
);
8206 if (elt
&& PACK_EXPANSION_P (elt
))
8212 /* Convert all template arguments to their appropriate types, and
8213 return a vector containing the innermost resulting template
8214 arguments. If any error occurs, return error_mark_node. Error and
8215 warning messages are issued under control of COMPLAIN.
8217 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8218 for arguments not specified in ARGS. Otherwise, if
8219 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8220 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8221 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8225 coerce_template_parms (tree parms
,
8228 tsubst_flags_t complain
,
8229 bool require_all_args
,
8230 bool use_default_args
)
8232 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8233 tree orig_inner_args
;
8236 tree new_inner_args
;
8237 int saved_unevaluated_operand
;
8238 int saved_inhibit_evaluation_warnings
;
8240 /* When used as a boolean value, indicates whether this is a
8241 variadic template parameter list. Since it's an int, we can also
8242 subtract it from nparms to get the number of non-variadic
8245 int variadic_args_p
= 0;
8246 int post_variadic_parms
= 0;
8248 /* Adjustment to nparms for fixed parameter packs. */
8249 int fixed_pack_adjust
= 0;
8250 int fixed_packs
= 0;
8253 /* Likewise for parameters with default arguments. */
8256 if (args
== error_mark_node
)
8257 return error_mark_node
;
8259 nparms
= TREE_VEC_LENGTH (parms
);
8261 /* Determine if there are any parameter packs or default arguments. */
8262 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8264 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8266 ++post_variadic_parms
;
8267 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8269 if (TREE_PURPOSE (parm
))
8273 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8274 /* If there are no parameters that follow a parameter pack, we need to
8275 expand any argument packs so that we can deduce a parameter pack from
8276 some non-packed args followed by an argument pack, as in variadic85.C.
8277 If there are such parameters, we need to leave argument packs intact
8278 so the arguments are assigned properly. This can happen when dealing
8279 with a nested class inside a partial specialization of a class
8280 template, as in variadic92.C, or when deducing a template parameter pack
8281 from a sub-declarator, as in variadic114.C. */
8282 if (!post_variadic_parms
)
8283 inner_args
= expand_template_argument_pack (inner_args
);
8285 /* Count any pack expansion args. */
8286 variadic_args_p
= pack_expansion_args_count (inner_args
);
8288 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8289 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8290 || (nargs
< nparms
- variadic_p
8293 && (!use_default_args
8294 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8295 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8298 if (complain
& tf_error
)
8300 if (variadic_p
|| default_p
)
8302 nparms
-= variadic_p
+ default_p
;
8303 error ("wrong number of template arguments "
8304 "(%d, should be at least %d)", nargs
, nparms
);
8307 error ("wrong number of template arguments "
8308 "(%d, should be %d)", nargs
, nparms
);
8311 inform (DECL_SOURCE_LOCATION (in_decl
),
8312 "provided for %qD", in_decl
);
8315 return error_mark_node
;
8317 /* We can't pass a pack expansion to a non-pack parameter of an alias
8318 template (DR 1430). */
8320 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8321 || concept_template_p (in_decl
))
8323 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8325 if (complain
& tf_error
)
8327 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8329 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8330 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8332 if (PACK_EXPANSION_P (arg
)
8333 && !template_parameter_pack_p (parm
))
8335 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8336 error_at (location_of (arg
),
8337 "pack expansion argument for non-pack parameter "
8338 "%qD of alias template %qD", parm
, in_decl
);
8340 error_at (location_of (arg
),
8341 "pack expansion argument for non-pack parameter "
8342 "%qD of concept %qD", parm
, in_decl
);
8343 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8350 return error_mark_node
;
8353 /* We need to evaluate the template arguments, even though this
8354 template-id may be nested within a "sizeof". */
8355 saved_unevaluated_operand
= cp_unevaluated_operand
;
8356 cp_unevaluated_operand
= 0;
8357 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8358 c_inhibit_evaluation_warnings
= 0;
8359 new_inner_args
= make_tree_vec (nparms
);
8360 new_args
= add_outermost_template_args (args
, new_inner_args
);
8361 int pack_adjust
= 0;
8362 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8367 /* Get the Ith template parameter. */
8368 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8370 if (parm
== error_mark_node
)
8372 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8376 /* Calculate the next argument. */
8377 if (arg_idx
< nargs
)
8378 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8382 if (template_parameter_pack_p (TREE_VALUE (parm
))
8383 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8385 /* Some arguments will be placed in the
8386 template parameter pack PARM. */
8387 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8388 inner_args
, arg_idx
,
8392 if (arg
== NULL_TREE
)
8394 /* We don't know how many args we have yet, just use the
8395 unconverted (and still packed) ones for now. */
8396 new_inner_args
= orig_inner_args
;
8401 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8403 /* Store this argument. */
8404 if (arg
== error_mark_node
)
8407 /* We are done with all of the arguments. */
8413 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8414 arg_idx
+= pack_adjust
;
8415 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8418 fixed_pack_adjust
+= pack_adjust
;
8426 if (PACK_EXPANSION_P (arg
))
8428 /* "If every valid specialization of a variadic template
8429 requires an empty template parameter pack, the template is
8430 ill-formed, no diagnostic required." So check that the
8431 pattern works with this parameter. */
8432 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8433 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8437 if (conv
== error_mark_node
)
8439 if (complain
& tf_error
)
8440 inform (input_location
, "so any instantiation with a "
8441 "non-empty parameter pack would be ill-formed");
8444 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8445 /* Recover from missing typename. */
8446 TREE_VEC_ELT (inner_args
, arg_idx
)
8447 = make_pack_expansion (conv
, complain
);
8449 /* We don't know how many args we have yet, just
8450 use the unconverted ones for now. */
8451 new_inner_args
= inner_args
;
8456 else if (require_all_args
)
8458 /* There must be a default arg in this case. */
8459 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8461 /* The position of the first default template argument,
8462 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8464 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8465 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8466 arg_idx
- pack_adjust
);
8471 if (arg
== error_mark_node
)
8473 if (complain
& tf_error
)
8474 error ("template argument %d is invalid", arg_idx
+ 1);
8478 /* This can occur if there was an error in the template
8479 parameter list itself (which we would already have
8480 reported) that we are trying to recover from, e.g., a class
8481 template with a parameter list such as
8482 template<typename..., typename> (cpp0x/variadic150.C). */
8485 /* This can also happen with a fixed parameter pack (71834). */
8486 if (arg_idx
>= nargs
)
8490 arg
= convert_template_argument (TREE_VALUE (parm
),
8491 arg
, new_args
, complain
,
8494 if (arg
== error_mark_node
)
8496 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8498 cp_unevaluated_operand
= saved_unevaluated_operand
;
8499 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8501 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8503 /* If we had fixed parameter packs, we didn't know how many arguments we
8504 actually needed earlier; now we do. */
8505 nparms
+= fixed_pack_adjust
;
8506 variadic_p
-= fixed_packs
;
8510 if (arg_idx
< nargs
)
8512 /* We had some pack expansion arguments that will only work if the packs
8513 are empty, but wait until instantiation time to complain.
8514 See variadic-ttp3.C. */
8515 int len
= nparms
+ (nargs
- arg_idx
);
8516 tree args
= make_tree_vec (len
);
8518 for (; i
< nparms
; ++i
)
8519 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8520 for (; i
< len
; ++i
, ++arg_idx
)
8521 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8522 arg_idx
- pack_adjust
);
8523 new_inner_args
= args
;
8528 gcc_assert (!(complain
& tf_error
) || seen_error ());
8529 return error_mark_node
;
8532 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8534 TREE_VEC_LENGTH (new_inner_args
));
8536 return new_inner_args
;
8539 /* Convert all template arguments to their appropriate types, and
8540 return a vector containing the innermost resulting template
8541 arguments. If any error occurs, return error_mark_node. Error and
8542 warning messages are not issued.
8544 Note that no function argument deduction is performed, and default
8545 arguments are used to fill in unspecified arguments. */
8547 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8549 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8552 /* Convert all template arguments to their appropriate type, and
8553 instantiate default arguments as needed. This returns a vector
8554 containing the innermost resulting template arguments, or
8555 error_mark_node if unsuccessful. */
8557 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8558 tsubst_flags_t complain
)
8560 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8563 /* Like coerce_template_parms. If PARMS represents all template
8564 parameters levels, this function returns a vector of vectors
8565 representing all the resulting argument levels. Note that in this
8566 case, only the innermost arguments are coerced because the
8567 outermost ones are supposed to have been coerced already.
8569 Otherwise, if PARMS represents only (the innermost) vector of
8570 parameters, this function returns a vector containing just the
8571 innermost resulting arguments. */
8574 coerce_innermost_template_parms (tree parms
,
8577 tsubst_flags_t complain
,
8578 bool require_all_args
,
8579 bool use_default_args
)
8581 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8582 int args_depth
= TMPL_ARGS_DEPTH (args
);
8585 if (parms_depth
> 1)
8587 coerced_args
= make_tree_vec (parms_depth
);
8591 for (level
= parms
, cur_depth
= parms_depth
;
8592 parms_depth
> 0 && level
!= NULL_TREE
;
8593 level
= TREE_CHAIN (level
), --cur_depth
)
8596 if (cur_depth
== args_depth
)
8597 l
= coerce_template_parms (TREE_VALUE (level
),
8598 args
, in_decl
, complain
,
8602 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8604 if (l
== error_mark_node
)
8605 return error_mark_node
;
8607 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8611 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8612 args
, in_decl
, complain
,
8615 return coerced_args
;
8618 /* Returns 1 if template args OT and NT are equivalent. */
8621 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8625 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8627 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8630 if (TREE_CODE (nt
) == TREE_VEC
)
8631 /* For member templates */
8632 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8633 else if (PACK_EXPANSION_P (ot
))
8634 return (PACK_EXPANSION_P (nt
)
8635 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8636 PACK_EXPANSION_PATTERN (nt
))
8637 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8638 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8639 else if (ARGUMENT_PACK_P (ot
))
8644 if (!ARGUMENT_PACK_P (nt
))
8647 opack
= ARGUMENT_PACK_ARGS (ot
);
8648 npack
= ARGUMENT_PACK_ARGS (nt
);
8649 len
= TREE_VEC_LENGTH (opack
);
8650 if (TREE_VEC_LENGTH (npack
) != len
)
8652 for (i
= 0; i
< len
; ++i
)
8653 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8654 TREE_VEC_ELT (npack
, i
)))
8658 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8660 else if (TYPE_P (nt
))
8664 /* Don't treat an alias template specialization with dependent
8665 arguments as equivalent to its underlying type when used as a
8666 template argument; we need them to be distinct so that we
8667 substitute into the specialization arguments at instantiation
8668 time. And aliases can't be equivalent without being ==, so
8669 we don't need to look any deeper.
8671 During partial ordering, however, we need to treat them normally so
8672 that we can order uses of the same alias with different
8673 cv-qualification (79960). */
8675 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8678 return same_type_p (ot
, nt
);
8680 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8684 /* Try to treat a template non-type argument that has been converted
8685 to the parameter type as equivalent to one that hasn't yet. */
8686 for (enum tree_code code1
= TREE_CODE (ot
);
8687 CONVERT_EXPR_CODE_P (code1
)
8688 || code1
== NON_LVALUE_EXPR
;
8689 code1
= TREE_CODE (ot
))
8690 ot
= TREE_OPERAND (ot
, 0);
8691 for (enum tree_code code2
= TREE_CODE (nt
);
8692 CONVERT_EXPR_CODE_P (code2
)
8693 || code2
== NON_LVALUE_EXPR
;
8694 code2
= TREE_CODE (nt
))
8695 nt
= TREE_OPERAND (nt
, 0);
8697 return cp_tree_equal (ot
, nt
);
8701 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8702 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8703 NEWARG_PTR with the offending arguments if they are non-NULL. */
8706 comp_template_args (tree oldargs
, tree newargs
,
8707 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8712 if (oldargs
== newargs
)
8715 if (!oldargs
|| !newargs
)
8718 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8721 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8723 tree nt
= TREE_VEC_ELT (newargs
, i
);
8724 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8726 if (! template_args_equal (ot
, nt
, partial_order
))
8728 if (oldarg_ptr
!= NULL
)
8730 if (newarg_ptr
!= NULL
)
8739 comp_template_args_porder (tree oargs
, tree nargs
)
8741 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8744 /* Implement a freelist interface for objects of type T.
8746 Head is a separate object, rather than a regular member, so that we
8747 can define it as a GTY deletable pointer, which is highly
8748 desirable. A data member could be declared that way, but then the
8749 containing object would implicitly get GTY((user)), which would
8750 prevent us from instantiating freelists as global objects.
8751 Although this way we can create freelist global objects, they're
8752 such thin wrappers that instantiating temporaries at every use
8753 loses nothing and saves permanent storage for the freelist object.
8755 Member functions next, anew, poison and reinit have default
8756 implementations that work for most of the types we're interested
8757 in, but if they don't work for some type, they should be explicitly
8758 specialized. See the comments before them for requirements, and
8759 the example specializations for the tree_list_freelist. */
8760 template <typename T
>
8763 /* Return the next object in a chain. We could just do type
8764 punning, but if we access the object with its underlying type, we
8765 avoid strict-aliasing trouble. This needs only work between
8766 poison and reinit. */
8767 static T
*&next (T
*obj
) { return obj
->next
; }
8769 /* Return a newly allocated, uninitialized or minimally-initialized
8770 object of type T. Any initialization performed by anew should
8771 either remain across the life of the object and the execution of
8772 poison, or be redone by reinit. */
8773 static T
*anew () { return ggc_alloc
<T
> (); }
8775 /* Optionally scribble all over the bits holding the object, so that
8776 they become (mostly?) uninitialized memory. This is called while
8777 preparing to make the object part of the free list. */
8778 static void poison (T
*obj
) {
8779 T
*p ATTRIBUTE_UNUSED
= obj
;
8780 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8782 #ifdef ENABLE_GC_CHECKING
8783 /* Poison the data, to indicate the data is garbage. */
8784 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8785 memset (p
, 0xa5, sizeof (*p
));
8787 /* Let valgrind know the object is free. */
8788 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8790 /* Let valgrind know the next portion of the object is available,
8791 but uninitialized. */
8792 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8795 /* Bring an object that underwent at least one lifecycle after anew
8796 and before the most recent free and poison, back to a usable
8797 state, reinitializing whatever is needed for it to be
8798 functionally equivalent to an object just allocated and returned
8799 by anew. This may poison or clear the next field, used by
8800 freelist housekeeping after poison was called. */
8801 static void reinit (T
*obj
) {
8802 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8804 #ifdef ENABLE_GC_CHECKING
8805 memset (q
, 0xa5, sizeof (*q
));
8807 /* Let valgrind know the entire object is available, but
8809 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8812 /* Reference a GTY-deletable pointer that points to the first object
8813 in the free list proper. */
8816 /* Construct a freelist object chaining objects off of HEAD. */
8817 freelist (T
*&head
) : head(head
) {}
8819 /* Add OBJ to the free object list. The former head becomes OBJ's
8828 /* Take an object from the free list, if one is available, or
8829 allocate a new one. Objects taken from the free list should be
8830 regarded as filled with garbage, except for bits that are
8831 configured to be preserved across free and alloc. */
8846 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8847 want to allocate a TREE_LIST using the usual interface, and ensure
8848 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8849 build_tree_list logic in reinit, so this could go out of sync. */
8852 freelist
<tree_node
>::next (tree obj
)
8854 return TREE_CHAIN (obj
);
8858 freelist
<tree_node
>::anew ()
8860 return build_tree_list (NULL
, NULL
);
8864 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8866 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8867 tree p ATTRIBUTE_UNUSED
= obj
;
8868 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8869 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8871 #ifdef ENABLE_GC_CHECKING
8872 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8874 /* Poison the data, to indicate the data is garbage. */
8875 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8876 memset (p
, 0xa5, size
);
8878 /* Let valgrind know the object is free. */
8879 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8880 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8881 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8882 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8884 #ifdef ENABLE_GC_CHECKING
8885 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
8886 /* Keep TREE_CHAIN functional. */
8887 TREE_SET_CODE (obj
, TREE_LIST
);
8889 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8894 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
8896 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8898 #ifdef ENABLE_GC_CHECKING
8899 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8900 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8901 memset (obj
, 0, sizeof (tree_list
));
8904 /* Let valgrind know the entire object is available, but
8906 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8908 #ifdef ENABLE_GC_CHECKING
8909 TREE_SET_CODE (obj
, TREE_LIST
);
8911 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8915 /* Point to the first object in the TREE_LIST freelist. */
8916 static GTY((deletable
)) tree tree_list_freelist_head
;
8917 /* Return the/an actual TREE_LIST freelist. */
8918 static inline freelist
<tree_node
>
8919 tree_list_freelist ()
8921 return tree_list_freelist_head
;
8924 /* Point to the first object in the tinst_level freelist. */
8925 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
8926 /* Return the/an actual tinst_level freelist. */
8927 static inline freelist
<tinst_level
>
8928 tinst_level_freelist ()
8930 return tinst_level_freelist_head
;
8933 /* Point to the first object in the pending_template freelist. */
8934 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
8935 /* Return the/an actual pending_template freelist. */
8936 static inline freelist
<pending_template
>
8937 pending_template_freelist ()
8939 return pending_template_freelist_head
;
8942 /* Build the TREE_LIST object out of a split list, store it
8943 permanently, and return it. */
8945 tinst_level::to_list ()
8947 gcc_assert (split_list_p ());
8948 tree ret
= tree_list_freelist ().alloc ();
8949 TREE_PURPOSE (ret
) = tldcl
;
8950 TREE_VALUE (ret
) = targs
;
8953 gcc_assert (tree_list_p ());
8957 const unsigned short tinst_level::refcount_infinity
;
8959 /* Increment OBJ's refcount unless it is already infinite. */
8960 static tinst_level
*
8961 inc_refcount_use (tinst_level
*obj
)
8963 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
8968 /* Release storage for OBJ and node, if it's a TREE_LIST. */
8970 tinst_level::free (tinst_level
*obj
)
8972 if (obj
->tree_list_p ())
8973 tree_list_freelist ().free (obj
->get_node ());
8974 tinst_level_freelist ().free (obj
);
8977 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
8978 OBJ's DECL and OBJ, and start over with the tinst_level object that
8979 used to be referenced by OBJ's NEXT. */
8981 dec_refcount_use (tinst_level
*obj
)
8984 && obj
->refcount
!= tinst_level::refcount_infinity
8985 && !--obj
->refcount
)
8987 tinst_level
*next
= obj
->next
;
8988 tinst_level::free (obj
);
8993 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
8994 and of the former PTR. Omitting the second argument is equivalent
8995 to passing (T*)NULL; this is allowed because passing the
8996 zero-valued integral constant NULL confuses type deduction and/or
8997 overload resolution. */
8998 template <typename T
>
9000 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9003 ptr
= inc_refcount_use (obj
);
9004 dec_refcount_use (save
);
9008 add_pending_template (tree d
)
9010 tree ti
= (TYPE_P (d
)
9011 ? CLASSTYPE_TEMPLATE_INFO (d
)
9012 : DECL_TEMPLATE_INFO (d
));
9013 struct pending_template
*pt
;
9016 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9019 /* We are called both from instantiate_decl, where we've already had a
9020 tinst_level pushed, and instantiate_template, where we haven't.
9022 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9023 level
= !current_tinst_level
9024 || current_tinst_level
->maybe_get_node () != d
;
9027 push_tinst_level (d
);
9029 pt
= pending_template_freelist ().alloc ();
9032 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9033 if (last_pending_template
)
9034 last_pending_template
->next
= pt
;
9036 pending_templates
= pt
;
9038 last_pending_template
= pt
;
9040 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9047 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9048 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9049 documentation for TEMPLATE_ID_EXPR. */
9052 lookup_template_function (tree fns
, tree arglist
)
9056 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9057 return error_mark_node
;
9059 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9061 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9063 error ("%q#D is not a function template", fns
);
9064 return error_mark_node
;
9067 if (BASELINK_P (fns
))
9069 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9071 BASELINK_FUNCTIONS (fns
),
9076 type
= TREE_TYPE (fns
);
9077 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
9078 type
= unknown_type_node
;
9080 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
9083 /* Within the scope of a template class S<T>, the name S gets bound
9084 (in build_self_reference) to a TYPE_DECL for the class, not a
9085 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9086 or one of its enclosing classes, and that type is a template,
9087 return the associated TEMPLATE_DECL. Otherwise, the original
9090 Also handle the case when DECL is a TREE_LIST of ambiguous
9091 injected-class-names from different bases. */
9094 maybe_get_template_decl_from_type_decl (tree decl
)
9096 if (decl
== NULL_TREE
)
9099 /* DR 176: A lookup that finds an injected-class-name (10.2
9100 [class.member.lookup]) can result in an ambiguity in certain cases
9101 (for example, if it is found in more than one base class). If all of
9102 the injected-class-names that are found refer to specializations of
9103 the same class template, and if the name is followed by a
9104 template-argument-list, the reference refers to the class template
9105 itself and not a specialization thereof, and is not ambiguous. */
9106 if (TREE_CODE (decl
) == TREE_LIST
)
9108 tree t
, tmpl
= NULL_TREE
;
9109 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9111 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9114 else if (tmpl
!= elt
)
9117 if (tmpl
&& t
== NULL_TREE
)
9123 return (decl
!= NULL_TREE
9124 && DECL_SELF_REFERENCE_P (decl
)
9125 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9126 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9129 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9130 parameters, find the desired type.
9132 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9134 IN_DECL, if non-NULL, is the template declaration we are trying to
9137 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9138 the class we are looking up.
9140 Issue error and warning messages under control of COMPLAIN.
9142 If the template class is really a local class in a template
9143 function, then the FUNCTION_CONTEXT is the function in which it is
9146 ??? Note that this function is currently called *twice* for each
9147 template-id: the first time from the parser, while creating the
9148 incomplete type (finish_template_type), and the second type during the
9149 real instantiation (instantiate_template_class). This is surely something
9150 that we want to avoid. It also causes some problems with argument
9151 coercion (see convert_nontype_argument for more information on this). */
9154 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9155 int entering_scope
, tsubst_flags_t complain
)
9157 tree templ
= NULL_TREE
, parmlist
;
9164 if (identifier_p (d1
))
9166 tree value
= innermost_non_namespace_value (d1
);
9167 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9172 push_decl_namespace (context
);
9173 templ
= lookup_name (d1
);
9174 templ
= maybe_get_template_decl_from_type_decl (templ
);
9176 pop_decl_namespace ();
9179 context
= DECL_CONTEXT (templ
);
9181 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9183 tree type
= TREE_TYPE (d1
);
9185 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9186 an implicit typename for the second A. Deal with it. */
9187 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9188 type
= TREE_TYPE (type
);
9190 if (CLASSTYPE_TEMPLATE_INFO (type
))
9192 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9193 d1
= DECL_NAME (templ
);
9196 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9197 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9199 templ
= TYPE_TI_TEMPLATE (d1
);
9200 d1
= DECL_NAME (templ
);
9202 else if (DECL_TYPE_TEMPLATE_P (d1
))
9205 d1
= DECL_NAME (templ
);
9206 context
= DECL_CONTEXT (templ
);
9208 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9211 d1
= DECL_NAME (templ
);
9214 /* Issue an error message if we didn't find a template. */
9217 if (complain
& tf_error
)
9218 error ("%qT is not a template", d1
);
9219 return error_mark_node
;
9222 if (TREE_CODE (templ
) != TEMPLATE_DECL
9223 /* Make sure it's a user visible template, if it was named by
9225 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9226 && !PRIMARY_TEMPLATE_P (templ
)))
9228 if (complain
& tf_error
)
9230 error ("non-template type %qT used as a template", d1
);
9232 error ("for template declaration %q+D", in_decl
);
9234 return error_mark_node
;
9237 complain
&= ~tf_user
;
9239 /* An alias that just changes the name of a template is equivalent to the
9240 other template, so if any of the arguments are pack expansions, strip
9241 the alias to avoid problems with a pack expansion passed to a non-pack
9242 alias template parameter (DR 1430). */
9243 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9244 templ
= get_underlying_template (templ
);
9246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9249 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9250 if (arglist2
== error_mark_node
9251 || (!uses_template_parms (arglist2
)
9252 && check_instantiated_args (templ
, arglist2
, complain
)))
9253 return error_mark_node
;
9255 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9260 tree template_type
= TREE_TYPE (templ
);
9263 tree found
= NULL_TREE
;
9266 int is_dependent_type
;
9267 int use_partial_inst_tmpl
= false;
9269 if (template_type
== error_mark_node
)
9270 /* An error occurred while building the template TEMPL, and a
9271 diagnostic has most certainly been emitted for that
9272 already. Let's propagate that error. */
9273 return error_mark_node
;
9275 gen_tmpl
= most_general_template (templ
);
9276 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9277 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9278 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9280 if (arg_depth
== 1 && parm_depth
> 1)
9282 /* We've been given an incomplete set of template arguments.
9285 template <class T> struct S1 {
9286 template <class U> struct S2 {};
9287 template <class U> struct S2<U*> {};
9290 we will be called with an ARGLIST of `U*', but the
9291 TEMPLATE will be `template <class T> template
9292 <class U> struct S1<T>::S2'. We must fill in the missing
9294 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9295 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9296 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9299 /* Now we should have enough arguments. */
9300 gcc_assert (parm_depth
== arg_depth
);
9302 /* From here on, we're only interested in the most general
9305 /* Calculate the BOUND_ARGS. These will be the args that are
9306 actually tsubst'd into the definition to create the
9308 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9310 /*require_all_args=*/true,
9311 /*use_default_args=*/true);
9313 if (arglist
== error_mark_node
)
9314 /* We were unable to bind the arguments. */
9315 return error_mark_node
;
9317 /* In the scope of a template class, explicit references to the
9318 template class refer to the type of the template, not any
9319 instantiation of it. For example, in:
9321 template <class T> class C { void f(C<T>); }
9323 the `C<T>' is just the same as `C'. Outside of the
9324 class, however, such a reference is an instantiation. */
9326 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9327 || currently_open_class (template_type
))
9329 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9331 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9332 return template_type
;
9335 /* If we already have this specialization, return it. */
9336 elt
.tmpl
= gen_tmpl
;
9338 elt
.spec
= NULL_TREE
;
9339 hash
= spec_hasher::hash (&elt
);
9340 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9345 /* If the the template's constraints are not satisfied,
9346 then we cannot form a valid type.
9348 Note that the check is deferred until after the hash
9349 lookup. This prevents redundant checks on previously
9350 instantiated specializations. */
9351 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9353 if (complain
& tf_error
)
9355 error ("template constraint failure");
9356 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9358 return error_mark_node
;
9361 is_dependent_type
= uses_template_parms (arglist
);
9363 /* If the deduced arguments are invalid, then the binding
9365 if (!is_dependent_type
9366 && check_instantiated_args (gen_tmpl
,
9367 INNERMOST_TEMPLATE_ARGS (arglist
),
9369 return error_mark_node
;
9371 if (!is_dependent_type
9372 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9373 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9374 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9376 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9377 DECL_NAME (gen_tmpl
),
9378 /*tag_scope=*/ts_global
);
9382 context
= DECL_CONTEXT (gen_tmpl
);
9383 if (context
&& TYPE_P (context
))
9385 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9386 context
= complete_type (context
);
9389 context
= tsubst (context
, arglist
, complain
, in_decl
);
9391 if (context
== error_mark_node
)
9392 return error_mark_node
;
9395 context
= global_namespace
;
9397 /* Create the type. */
9398 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9400 /* The user referred to a specialization of an alias
9401 template represented by GEN_TMPL.
9403 [temp.alias]/2 says:
9405 When a template-id refers to the specialization of an
9406 alias template, it is equivalent to the associated
9407 type obtained by substitution of its
9408 template-arguments for the template-parameters in the
9409 type-id of the alias template. */
9411 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9412 /* Note that the call above (by indirectly calling
9413 register_specialization in tsubst_decl) registers the
9414 TYPE_DECL representing the specialization of the alias
9415 template. So next time someone substitutes ARGLIST for
9416 the template parms into the alias template (GEN_TMPL),
9417 she'll get that TYPE_DECL back. */
9419 if (t
== error_mark_node
)
9422 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9424 if (!is_dependent_type
)
9426 set_current_access_from_decl (TYPE_NAME (template_type
));
9427 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9428 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9429 arglist
, complain
, in_decl
),
9430 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9431 arglist
, complain
, in_decl
),
9432 SCOPED_ENUM_P (template_type
), NULL
);
9434 if (t
== error_mark_node
)
9439 /* We don't want to call start_enum for this type, since
9440 the values for the enumeration constants may involve
9441 template parameters. And, no one should be interested
9442 in the enumeration constants for such a type. */
9443 t
= cxx_make_type (ENUMERAL_TYPE
);
9444 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9446 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9447 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9448 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9450 else if (CLASS_TYPE_P (template_type
))
9452 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9453 instantiated here. */
9454 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9456 t
= make_class_type (TREE_CODE (template_type
));
9457 CLASSTYPE_DECLARED_CLASS (t
)
9458 = CLASSTYPE_DECLARED_CLASS (template_type
);
9459 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9461 /* A local class. Make sure the decl gets registered properly. */
9462 if (context
== current_function_decl
)
9463 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9465 return error_mark_node
;
9467 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9468 /* This instantiation is another name for the primary
9469 template type. Set the TYPE_CANONICAL field
9471 TYPE_CANONICAL (t
) = template_type
;
9472 else if (any_template_arguments_need_structural_equality_p (arglist
))
9473 /* Some of the template arguments require structural
9474 equality testing, so this template class requires
9475 structural equality testing. */
9476 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9481 /* If we called start_enum or pushtag above, this information
9482 will already be set up. */
9485 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9487 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9488 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9489 DECL_SOURCE_LOCATION (type_decl
)
9490 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9493 type_decl
= TYPE_NAME (t
);
9495 if (CLASS_TYPE_P (template_type
))
9497 TREE_PRIVATE (type_decl
)
9498 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9499 TREE_PROTECTED (type_decl
)
9500 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9501 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9503 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9504 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9508 if (OVERLOAD_TYPE_P (t
)
9509 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9511 static const char *tags
[] = {"abi_tag", "may_alias"};
9513 for (unsigned ix
= 0; ix
!= 2; ix
++)
9516 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9520 = tree_cons (TREE_PURPOSE (attributes
),
9521 TREE_VALUE (attributes
),
9522 TYPE_ATTRIBUTES (t
));
9526 /* Let's consider the explicit specialization of a member
9527 of a class template specialization that is implicitly instantiated,
9532 template<class U> struct M {}; //#0
9537 struct S<int>::M<char> //#1
9541 [temp.expl.spec]/4 says this is valid.
9543 In this case, when we write:
9546 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9549 When we encounter #1, we want to store the partial instantiation
9550 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9552 For all cases other than this "explicit specialization of member of a
9553 class template", we just want to store the most general template into
9554 the CLASSTYPE_TI_TEMPLATE of M.
9556 This case of "explicit specialization of member of a class template"
9558 1/ the enclosing class is an instantiation of, and therefore not
9559 the same as, the context of the most general template, and
9560 2/ we aren't looking at the partial instantiation itself, i.e.
9561 the innermost arguments are not the same as the innermost parms of
9562 the most general template.
9564 So it's only when 1/ and 2/ happens that we want to use the partial
9565 instantiation of the member template in lieu of its most general
9568 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9569 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9570 /* the enclosing class must be an instantiation... */
9571 && CLASS_TYPE_P (context
)
9572 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9574 TREE_VEC_LENGTH (arglist
)--;
9575 ++processing_template_decl
;
9576 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9577 tree partial_inst_args
=
9578 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9579 arglist
, complain
, NULL_TREE
);
9580 --processing_template_decl
;
9581 TREE_VEC_LENGTH (arglist
)++;
9582 if (partial_inst_args
== error_mark_node
)
9583 return error_mark_node
;
9584 use_partial_inst_tmpl
=
9585 /*...and we must not be looking at the partial instantiation
9587 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9591 if (!use_partial_inst_tmpl
)
9592 /* This case is easy; there are no member templates involved. */
9596 /* This is a full instantiation of a member template. Find
9597 the partial instantiation of which this is an instance. */
9599 /* Temporarily reduce by one the number of levels in the ARGLIST
9600 so as to avoid comparing the last set of arguments. */
9601 TREE_VEC_LENGTH (arglist
)--;
9602 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9603 TREE_VEC_LENGTH (arglist
)++;
9604 /* FOUND is either a proper class type, or an alias
9605 template specialization. In the later case, it's a
9606 TYPE_DECL, resulting from the substituting of arguments
9607 for parameters in the TYPE_DECL of the alias template
9608 done earlier. So be careful while getting the template
9610 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9612 : (TREE_CODE (found
) == TYPE_DECL
9613 ? DECL_TI_TEMPLATE (found
)
9614 : CLASSTYPE_TI_TEMPLATE (found
)));
9617 // Build template info for the new specialization.
9618 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9621 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9622 entry
= ggc_alloc
<spec_entry
> ();
9626 /* Note this use of the partial instantiation so we can check it
9627 later in maybe_process_partial_specialization. */
9628 DECL_TEMPLATE_INSTANTIATIONS (found
)
9629 = tree_cons (arglist
, t
,
9630 DECL_TEMPLATE_INSTANTIATIONS (found
));
9632 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9633 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9634 /* Now that the type has been registered on the instantiations
9635 list, we set up the enumerators. Because the enumeration
9636 constants may involve the enumeration type itself, we make
9637 sure to register the type first, and then create the
9638 constants. That way, doing tsubst_expr for the enumeration
9639 constants won't result in recursive calls here; we'll find
9640 the instantiation and exit above. */
9641 tsubst_enum (template_type
, t
, arglist
);
9643 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9644 /* If the type makes use of template parameters, the
9645 code that generates debugging information will crash. */
9646 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9648 /* Possibly limit visibility based on template args. */
9649 TREE_PUBLIC (type_decl
) = 1;
9650 determine_visibility (type_decl
);
9652 inherit_targ_abi_tags (t
);
9658 /* Wrapper for lookup_template_class_1. */
9661 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9662 int entering_scope
, tsubst_flags_t complain
)
9665 timevar_push (TV_TEMPLATE_INST
);
9666 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9667 entering_scope
, complain
);
9668 timevar_pop (TV_TEMPLATE_INST
);
9672 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9675 lookup_template_variable (tree templ
, tree arglist
)
9677 /* The type of the expression is NULL_TREE since the template-id could refer
9678 to an explicit or partial specialization. */
9679 tree type
= NULL_TREE
;
9680 if (flag_concepts
&& variable_concept_p (templ
))
9681 /* Except that concepts are always bool. */
9682 type
= boolean_type_node
;
9683 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9686 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9689 finish_template_variable (tree var
, tsubst_flags_t complain
)
9691 tree templ
= TREE_OPERAND (var
, 0);
9692 tree arglist
= TREE_OPERAND (var
, 1);
9694 /* We never want to return a VAR_DECL for a variable concept, since they
9695 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9696 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9697 if (concept_p
&& processing_template_decl
)
9700 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9701 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9703 templ
= most_general_template (templ
);
9704 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9705 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9707 /*use_default*/true);
9709 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9711 if (complain
& tf_error
)
9713 error ("use of invalid variable template %qE", var
);
9714 diagnose_constraints (location_of (var
), templ
, arglist
);
9716 return error_mark_node
;
9719 /* If a template-id refers to a specialization of a variable
9720 concept, then the expression is true if and only if the
9721 concept's constraints are satisfied by the given template
9724 NOTE: This is an extension of Concepts Lite TS that
9725 allows constraints to be used in expressions. */
9728 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9729 return evaluate_variable_concept (decl
, arglist
);
9732 return instantiate_template (templ
, arglist
, complain
);
9735 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9736 TARGS template args, and instantiate it if it's not dependent. */
9739 lookup_and_finish_template_variable (tree templ
, tree targs
,
9740 tsubst_flags_t complain
)
9742 templ
= lookup_template_variable (templ
, targs
);
9743 if (!any_dependent_template_arguments_p (targs
))
9745 templ
= finish_template_variable (templ
, complain
);
9749 return convert_from_reference (templ
);
9758 /* True when we should also visit template parameters that occur in
9759 non-deduced contexts. */
9760 bool include_nondeduced_p
;
9761 hash_set
<tree
> *visited
;
9764 /* Called from for_each_template_parm via walk_tree. */
9767 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9770 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9771 tree_fn_t fn
= pfd
->fn
;
9772 void *data
= pfd
->data
;
9773 tree result
= NULL_TREE
;
9775 #define WALK_SUBTREE(NODE) \
9778 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9779 pfd->include_nondeduced_p, \
9781 if (result) goto out; \
9785 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9789 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9790 WALK_SUBTREE (TYPE_CONTEXT (t
));
9792 switch (TREE_CODE (t
))
9795 if (TYPE_PTRMEMFUNC_P (t
))
9801 if (!TYPE_TEMPLATE_INFO (t
))
9804 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9808 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9809 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9813 /* Since we're not going to walk subtrees, we have to do this
9815 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9819 /* Check the return type. */
9820 WALK_SUBTREE (TREE_TYPE (t
));
9822 /* Check the parameter types. Since default arguments are not
9823 instantiated until they are needed, the TYPE_ARG_TYPES may
9824 contain expressions that involve template parameters. But,
9825 no-one should be looking at them yet. And, once they're
9826 instantiated, they don't contain template parameters, so
9827 there's no point in looking at them then, either. */
9831 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9832 WALK_SUBTREE (TREE_VALUE (parm
));
9834 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9835 want walk_tree walking into them itself. */
9839 if (flag_noexcept_type
)
9841 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9843 WALK_SUBTREE (TREE_PURPOSE (spec
));
9849 case UNDERLYING_TYPE
:
9850 if (pfd
->include_nondeduced_p
9851 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9853 pfd
->include_nondeduced_p
,
9855 return error_mark_node
;
9856 *walk_subtrees
= false;
9861 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9862 WALK_SUBTREE (DECL_TI_ARGS (t
));
9867 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9868 WALK_SUBTREE (DECL_INITIAL (t
));
9869 if (DECL_CONTEXT (t
)
9870 && pfd
->include_nondeduced_p
)
9871 WALK_SUBTREE (DECL_CONTEXT (t
));
9874 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9875 /* Record template parameters such as `T' inside `TT<T>'. */
9876 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9879 case TEMPLATE_TEMPLATE_PARM
:
9880 case TEMPLATE_TYPE_PARM
:
9881 case TEMPLATE_PARM_INDEX
:
9882 if (fn
&& (*fn
)(t
, data
))
9889 /* A template template parameter is encountered. */
9890 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9891 WALK_SUBTREE (TREE_TYPE (t
));
9893 /* Already substituted template template parameter */
9898 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9899 partial instantiation. */
9900 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9904 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9905 && pfd
->include_nondeduced_p
)
9906 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9911 /* If there's no type, then this thing must be some expression
9912 involving template parameters. */
9913 if (!fn
&& !TREE_TYPE (t
))
9914 return error_mark_node
;
9919 case IMPLICIT_CONV_EXPR
:
9920 case REINTERPRET_CAST_EXPR
:
9921 case CONST_CAST_EXPR
:
9922 case STATIC_CAST_EXPR
:
9923 case DYNAMIC_CAST_EXPR
:
9927 case PSEUDO_DTOR_EXPR
:
9929 return error_mark_node
;
9938 /* We didn't find any template parameters we liked. */
9943 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9944 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9945 call FN with the parameter and the DATA.
9946 If FN returns nonzero, the iteration is terminated, and
9947 for_each_template_parm returns 1. Otherwise, the iteration
9948 continues. If FN never returns a nonzero value, the value
9949 returned by for_each_template_parm is 0. If FN is NULL, it is
9950 considered to be the function which always returns 1.
9952 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9953 parameters that occur in non-deduced contexts. When false, only
9954 visits those template parameters that can be deduced. */
9957 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9958 hash_set
<tree
> *visited
,
9959 bool include_nondeduced_p
,
9962 struct pair_fn_data pfd
;
9967 pfd
.any_fn
= any_fn
;
9969 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9971 /* Walk the tree. (Conceptually, we would like to walk without
9972 duplicates, but for_each_template_parm_r recursively calls
9973 for_each_template_parm, so we would need to reorganize a fair
9974 bit to use walk_tree_without_duplicates, so we keep our own
9977 pfd
.visited
= visited
;
9979 pfd
.visited
= new hash_set
<tree
>;
9980 result
= cp_walk_tree (&t
,
9981 for_each_template_parm_r
,
9995 /* Returns true if T depends on any template parameter. */
9998 uses_template_parms (tree t
)
10000 if (t
== NULL_TREE
)
10004 int saved_processing_template_decl
;
10006 saved_processing_template_decl
= processing_template_decl
;
10007 if (!saved_processing_template_decl
)
10008 processing_template_decl
= 1;
10010 dependent_p
= dependent_type_p (t
);
10011 else if (TREE_CODE (t
) == TREE_VEC
)
10012 dependent_p
= any_dependent_template_arguments_p (t
);
10013 else if (TREE_CODE (t
) == TREE_LIST
)
10014 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10015 || uses_template_parms (TREE_CHAIN (t
)));
10016 else if (TREE_CODE (t
) == TYPE_DECL
)
10017 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10018 else if (DECL_P (t
)
10020 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10021 || TREE_CODE (t
) == OVERLOAD
10023 || identifier_p (t
)
10024 || TREE_CODE (t
) == TRAIT_EXPR
10025 || TREE_CODE (t
) == CONSTRUCTOR
10026 || CONSTANT_CLASS_P (t
))
10027 dependent_p
= (type_dependent_expression_p (t
)
10028 || value_dependent_expression_p (t
));
10031 gcc_assert (t
== error_mark_node
);
10032 dependent_p
= false;
10035 processing_template_decl
= saved_processing_template_decl
;
10037 return dependent_p
;
10040 /* Returns true iff current_function_decl is an incompletely instantiated
10041 template. Useful instead of processing_template_decl because the latter
10042 is set to 0 during instantiate_non_dependent_expr. */
10045 in_template_function (void)
10047 tree fn
= current_function_decl
;
10049 ++processing_template_decl
;
10050 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10051 && DECL_TEMPLATE_INFO (fn
)
10052 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10053 --processing_template_decl
;
10057 /* Returns true if T depends on any template parameter with level LEVEL. */
10060 uses_template_parms_level (tree t
, int level
)
10062 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10063 /*include_nondeduced_p=*/true);
10066 /* Returns true if the signature of DECL depends on any template parameter from
10067 its enclosing class. */
10070 uses_outer_template_parms (tree decl
)
10072 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10075 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10076 &depth
, NULL
, /*include_nondeduced_p=*/true))
10078 if (PRIMARY_TEMPLATE_P (decl
)
10079 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10080 (DECL_TEMPLATE_PARMS (decl
)),
10081 template_parm_outer_level
,
10082 &depth
, NULL
, /*include_nondeduced_p=*/true))
10084 tree ci
= get_constraints (decl
);
10086 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10087 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10088 &depth
, NULL
, /*nondeduced*/true))
10093 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10094 ill-formed translation unit, i.e. a variable or function that isn't
10095 usable in a constant expression. */
10098 neglectable_inst_p (tree d
)
10100 return (d
&& DECL_P (d
)
10101 && !undeduced_auto_decl (d
)
10102 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10103 : decl_maybe_constant_var_p (d
)));
10106 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10107 neglectable and instantiated from within an erroneous instantiation. */
10110 limit_bad_template_recursion (tree decl
)
10112 struct tinst_level
*lev
= current_tinst_level
;
10113 int errs
= errorcount
+ sorrycount
;
10114 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10117 for (; lev
; lev
= lev
->next
)
10118 if (neglectable_inst_p (lev
->maybe_get_node ()))
10121 return (lev
&& errs
> lev
->errors
);
10124 static int tinst_depth
;
10125 extern int max_tinst_depth
;
10128 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10130 /* We're starting to instantiate D; record the template instantiation context
10131 at LOC for diagnostics and to restore it later. */
10134 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10136 struct tinst_level
*new_level
;
10138 if (tinst_depth
>= max_tinst_depth
)
10140 /* Tell error.c not to try to instantiate any templates. */
10142 fatal_error (input_location
,
10143 "template instantiation depth exceeds maximum of %d"
10144 " (use -ftemplate-depth= to increase the maximum)",
10149 /* If the current instantiation caused problems, don't let it instantiate
10150 anything else. Do allow deduction substitution and decls usable in
10151 constant expressions. */
10152 if (!targs
&& limit_bad_template_recursion (tldcl
))
10155 /* When not -quiet, dump template instantiations other than functions, since
10156 announce_function will take care of those. */
10157 if (!quiet_flag
&& !targs
10158 && TREE_CODE (tldcl
) != TREE_LIST
10159 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10160 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10162 new_level
= tinst_level_freelist ().alloc ();
10163 new_level
->tldcl
= tldcl
;
10164 new_level
->targs
= targs
;
10165 new_level
->locus
= loc
;
10166 new_level
->errors
= errorcount
+ sorrycount
;
10167 new_level
->next
= NULL
;
10168 new_level
->refcount
= 0;
10169 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10170 set_refcount_ptr (current_tinst_level
, new_level
);
10173 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10174 depth_reached
= tinst_depth
;
10179 /* We're starting substitution of TMPL<ARGS>; record the template
10180 substitution context for diagnostics and to restore it later. */
10183 push_tinst_level (tree tmpl
, tree args
)
10185 return push_tinst_level_loc (tmpl
, args
, input_location
);
10188 /* We're starting to instantiate D; record INPUT_LOCATION and the
10189 template instantiation context for diagnostics and to restore it
10193 push_tinst_level (tree d
)
10195 return push_tinst_level_loc (d
, input_location
);
10198 /* Likewise, but record LOC as the program location. */
10201 push_tinst_level_loc (tree d
, location_t loc
)
10203 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10204 return push_tinst_level_loc (d
, NULL
, loc
);
10207 /* We're done instantiating this template; return to the instantiation
10211 pop_tinst_level (void)
10213 /* Restore the filename and line number stashed away when we started
10214 this instantiation. */
10215 input_location
= current_tinst_level
->locus
;
10216 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10220 /* We're instantiating a deferred template; restore the template
10221 instantiation context in which the instantiation was requested, which
10222 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10225 reopen_tinst_level (struct tinst_level
*level
)
10227 struct tinst_level
*t
;
10230 for (t
= level
; t
; t
= t
->next
)
10233 set_refcount_ptr (current_tinst_level
, level
);
10234 pop_tinst_level ();
10235 if (current_tinst_level
)
10236 current_tinst_level
->errors
= errorcount
+sorrycount
;
10237 return level
->maybe_get_node ();
10240 /* Returns the TINST_LEVEL which gives the original instantiation
10243 struct tinst_level
*
10244 outermost_tinst_level (void)
10246 struct tinst_level
*level
= current_tinst_level
;
10248 while (level
->next
)
10249 level
= level
->next
;
10253 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10254 vector of template arguments, as for tsubst.
10256 Returns an appropriate tsubst'd friend declaration. */
10259 tsubst_friend_function (tree decl
, tree args
)
10263 if (TREE_CODE (decl
) == FUNCTION_DECL
10264 && DECL_TEMPLATE_INSTANTIATION (decl
)
10265 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10266 /* This was a friend declared with an explicit template
10267 argument list, e.g.:
10269 friend void f<>(T);
10271 to indicate that f was a template instantiation, not a new
10272 function declaration. Now, we have to figure out what
10273 instantiation of what template. */
10275 tree template_id
, arglist
, fns
;
10278 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10280 /* Friend functions are looked up in the containing namespace scope.
10281 We must enter that scope, to avoid finding member functions of the
10282 current class with same name. */
10283 push_nested_namespace (ns
);
10284 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10285 tf_warning_or_error
, NULL_TREE
,
10286 /*integral_constant_expression_p=*/false);
10287 pop_nested_namespace (ns
);
10288 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10289 tf_warning_or_error
, NULL_TREE
);
10290 template_id
= lookup_template_function (fns
, arglist
);
10292 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10293 tmpl
= determine_specialization (template_id
, new_friend
,
10295 /*need_member_template=*/0,
10296 TREE_VEC_LENGTH (args
),
10298 return instantiate_template (tmpl
, new_args
, tf_error
);
10301 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10303 /* The NEW_FRIEND will look like an instantiation, to the
10304 compiler, but is not an instantiation from the point of view of
10305 the language. For example, we might have had:
10307 template <class T> struct S {
10308 template <class U> friend void f(T, U);
10311 Then, in S<int>, template <class U> void f(int, U) is not an
10312 instantiation of anything. */
10313 if (new_friend
== error_mark_node
)
10314 return error_mark_node
;
10316 DECL_USE_TEMPLATE (new_friend
) = 0;
10317 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10319 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10320 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10321 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10324 /* The mangled name for the NEW_FRIEND is incorrect. The function
10325 is not a template instantiation and should not be mangled like
10326 one. Therefore, we forget the mangling here; we'll recompute it
10327 later if we need it. */
10328 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10330 SET_DECL_RTL (new_friend
, NULL
);
10331 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10334 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10337 tree new_friend_template_info
;
10338 tree new_friend_result_template_info
;
10340 int new_friend_is_defn
;
10342 /* We must save some information from NEW_FRIEND before calling
10343 duplicate decls since that function will free NEW_FRIEND if
10345 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10346 new_friend_is_defn
=
10347 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10348 (template_for_substitution (new_friend
)))
10350 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10352 /* This declaration is a `primary' template. */
10353 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10355 new_friend_result_template_info
10356 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10359 new_friend_result_template_info
= NULL_TREE
;
10361 /* Inside pushdecl_namespace_level, we will push into the
10362 current namespace. However, the friend function should go
10363 into the namespace of the template. */
10364 ns
= decl_namespace_context (new_friend
);
10365 push_nested_namespace (ns
);
10366 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10367 pop_nested_namespace (ns
);
10369 if (old_decl
== error_mark_node
)
10370 return error_mark_node
;
10372 if (old_decl
!= new_friend
)
10374 /* This new friend declaration matched an existing
10375 declaration. For example, given:
10377 template <class T> void f(T);
10378 template <class U> class C {
10379 template <class T> friend void f(T) {}
10382 the friend declaration actually provides the definition
10383 of `f', once C has been instantiated for some type. So,
10384 old_decl will be the out-of-class template declaration,
10385 while new_friend is the in-class definition.
10387 But, if `f' was called before this point, the
10388 instantiation of `f' will have DECL_TI_ARGS corresponding
10389 to `T' but not to `U', references to which might appear
10390 in the definition of `f'. Previously, the most general
10391 template for an instantiation of `f' was the out-of-class
10392 version; now it is the in-class version. Therefore, we
10393 run through all specialization of `f', adding to their
10394 DECL_TI_ARGS appropriately. In particular, they need a
10395 new set of outer arguments, corresponding to the
10396 arguments for this class instantiation.
10398 The same situation can arise with something like this:
10400 friend void f(int);
10401 template <class T> class C {
10402 friend void f(T) {}
10405 when `C<int>' is instantiated. Now, `f(int)' is defined
10408 if (!new_friend_is_defn
)
10409 /* On the other hand, if the in-class declaration does
10410 *not* provide a definition, then we don't want to alter
10411 existing definitions. We can just leave everything
10416 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10417 tree new_args
= TI_ARGS (new_friend_template_info
);
10419 /* Overwrite whatever template info was there before, if
10420 any, with the new template information pertaining to
10421 the declaration. */
10422 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10424 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10426 /* We should have called reregister_specialization in
10427 duplicate_decls. */
10428 gcc_assert (retrieve_specialization (new_template
,
10432 /* Instantiate it if the global has already been used. */
10433 if (DECL_ODR_USED (old_decl
))
10434 instantiate_decl (old_decl
, /*defer_ok=*/true,
10435 /*expl_inst_class_mem_p=*/false);
10441 /* Indicate that the old function template is a partial
10443 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10444 = new_friend_result_template_info
;
10446 gcc_assert (new_template
10447 == most_general_template (new_template
));
10448 gcc_assert (new_template
!= old_decl
);
10450 /* Reassign any specializations already in the hash table
10451 to the new more general template, and add the
10452 additional template args. */
10453 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10455 t
= TREE_CHAIN (t
))
10457 tree spec
= TREE_VALUE (t
);
10460 elt
.tmpl
= old_decl
;
10461 elt
.args
= DECL_TI_ARGS (spec
);
10462 elt
.spec
= NULL_TREE
;
10464 decl_specializations
->remove_elt (&elt
);
10466 DECL_TI_ARGS (spec
)
10467 = add_outermost_template_args (new_args
,
10468 DECL_TI_ARGS (spec
));
10470 register_specialization
10471 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10474 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10478 /* The information from NEW_FRIEND has been merged into OLD_DECL
10479 by duplicate_decls. */
10480 new_friend
= old_decl
;
10485 tree context
= DECL_CONTEXT (new_friend
);
10489 template <class T> class C {
10490 template <class U> friend void C1<U>::f (); // case 1
10491 friend void C2<T>::f (); // case 2
10493 we only need to make sure CONTEXT is a complete type for
10494 case 2. To distinguish between the two cases, we note that
10495 CONTEXT of case 1 remains dependent type after tsubst while
10496 this isn't true for case 2. */
10497 ++processing_template_decl
;
10498 dependent_p
= dependent_type_p (context
);
10499 --processing_template_decl
;
10502 && !complete_type_or_else (context
, NULL_TREE
))
10503 return error_mark_node
;
10505 if (COMPLETE_TYPE_P (context
))
10507 tree fn
= new_friend
;
10508 /* do_friend adds the TEMPLATE_DECL for any member friend
10509 template even if it isn't a member template, i.e.
10510 template <class T> friend A<T>::f();
10511 Look through it in that case. */
10512 if (TREE_CODE (fn
) == TEMPLATE_DECL
10513 && !PRIMARY_TEMPLATE_P (fn
))
10514 fn
= DECL_TEMPLATE_RESULT (fn
);
10515 /* Check to see that the declaration is really present, and,
10516 possibly obtain an improved declaration. */
10517 fn
= check_classfn (context
, fn
, NULL_TREE
);
10527 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10528 template arguments, as for tsubst.
10530 Returns an appropriate tsubst'd friend type or error_mark_node on
10534 tsubst_friend_class (tree friend_tmpl
, tree args
)
10538 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10540 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10541 return TREE_TYPE (tmpl
);
10544 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10545 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10546 push_nested_namespace (context
);
10548 push_nested_class (context
);
10550 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10551 /*non_class=*/false, /*block_p=*/false,
10552 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10554 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10556 /* The friend template has already been declared. Just
10557 check to see that the declarations match, and install any new
10558 default parameters. We must tsubst the default parameters,
10559 of course. We only need the innermost template parameters
10560 because that is all that redeclare_class_template will look
10562 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10563 > TMPL_ARGS_DEPTH (args
))
10565 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10566 args
, tf_warning_or_error
);
10567 location_t saved_input_location
= input_location
;
10568 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10569 tree cons
= get_constraints (tmpl
);
10570 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10571 input_location
= saved_input_location
;
10576 /* The friend template has not already been declared. In this
10577 case, the instantiation of the template class will cause the
10578 injection of this template into the namespace scope. */
10579 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10581 if (tmpl
!= error_mark_node
)
10583 /* The new TMPL is not an instantiation of anything, so we
10584 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10585 for the new type because that is supposed to be the
10586 corresponding template decl, i.e., TMPL. */
10587 DECL_USE_TEMPLATE (tmpl
) = 0;
10588 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10589 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10590 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10591 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10593 /* It is hidden. */
10594 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10595 DECL_ANTICIPATED (tmpl
)
10596 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10598 /* Inject this template into the enclosing namspace scope. */
10599 tmpl
= pushdecl_namespace_level (tmpl
, true);
10603 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10604 pop_nested_namespace (context
);
10606 pop_nested_class ();
10608 return TREE_TYPE (tmpl
);
10611 /* Returns zero if TYPE cannot be completed later due to circularity.
10612 Otherwise returns one. */
10615 can_complete_type_without_circularity (tree type
)
10617 if (type
== NULL_TREE
|| type
== error_mark_node
)
10619 else if (COMPLETE_TYPE_P (type
))
10621 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10622 return can_complete_type_without_circularity (TREE_TYPE (type
));
10623 else if (CLASS_TYPE_P (type
)
10624 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10630 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10631 tsubst_flags_t
, tree
);
10633 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10634 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10637 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10638 tsubst_flags_t complain
, tree in_decl
)
10640 gcc_assert (ATTR_IS_DEPENDENT (t
));
10642 tree val
= TREE_VALUE (t
);
10643 if (val
== NULL_TREE
)
10644 /* Nothing to do. */;
10645 else if ((flag_openmp
|| flag_openmp_simd
)
10646 && is_attribute_p ("omp declare simd",
10647 get_attribute_name (t
)))
10649 tree clauses
= TREE_VALUE (val
);
10650 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10651 complain
, in_decl
);
10652 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10653 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10654 tree parms
= DECL_ARGUMENTS (*decl_p
);
10656 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10658 val
= build_tree_list (NULL_TREE
, clauses
);
10662 /* If the first attribute argument is an identifier, don't
10663 pass it through tsubst. Attributes like mode, format,
10664 cleanup and several target specific attributes expect it
10666 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10669 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10670 /*integral_constant_expression_p=*/false);
10671 if (chain
!= TREE_CHAIN (val
))
10672 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10674 else if (PACK_EXPANSION_P (val
))
10676 /* An attribute pack expansion. */
10677 tree purp
= TREE_PURPOSE (t
);
10678 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10679 if (pack
== error_mark_node
)
10680 return error_mark_node
;
10681 int len
= TREE_VEC_LENGTH (pack
);
10682 tree list
= NULL_TREE
;
10684 for (int i
= 0; i
< len
; ++i
)
10686 tree elt
= TREE_VEC_ELT (pack
, i
);
10687 *q
= build_tree_list (purp
, elt
);
10688 q
= &TREE_CHAIN (*q
);
10693 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10694 /*integral_constant_expression_p=*/false);
10696 if (val
!= TREE_VALUE (t
))
10697 return build_tree_list (TREE_PURPOSE (t
), val
);
10701 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10702 unchanged or a new TREE_LIST chain. */
10705 tsubst_attributes (tree attributes
, tree args
,
10706 tsubst_flags_t complain
, tree in_decl
)
10708 tree last_dep
= NULL_TREE
;
10710 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10711 if (ATTR_IS_DEPENDENT (t
))
10714 attributes
= copy_list (attributes
);
10719 for (tree
*p
= &attributes
; *p
; )
10722 if (ATTR_IS_DEPENDENT (t
))
10724 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10729 p
= &TREE_CHAIN (*p
);
10730 *p
= TREE_CHAIN (t
);
10734 p
= &TREE_CHAIN (*p
);
10740 /* Apply any attributes which had to be deferred until instantiation
10741 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10742 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10745 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10746 tree args
, tsubst_flags_t complain
, tree in_decl
)
10748 tree last_dep
= NULL_TREE
;
10752 if (attributes
== NULL_TREE
)
10755 if (DECL_P (*decl_p
))
10757 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10759 p
= &DECL_ATTRIBUTES (*decl_p
);
10760 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10761 to our attributes parameter. */
10762 gcc_assert (*p
== attributes
);
10766 p
= &TYPE_ATTRIBUTES (*decl_p
);
10767 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10768 lookup_template_class_1, and should be preserved. */
10769 gcc_assert (*p
!= attributes
);
10771 p
= &TREE_CHAIN (*p
);
10774 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10775 if (ATTR_IS_DEPENDENT (t
))
10778 attributes
= copy_list (attributes
);
10785 tree late_attrs
= NULL_TREE
;
10786 tree
*q
= &late_attrs
;
10791 if (ATTR_IS_DEPENDENT (t
))
10793 *p
= TREE_CHAIN (t
);
10794 TREE_CHAIN (t
) = NULL_TREE
;
10795 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10797 q
= &TREE_CHAIN (*q
);
10800 p
= &TREE_CHAIN (t
);
10803 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10807 /* Perform (or defer) access check for typedefs that were referenced
10808 from within the template TMPL code.
10809 This is a subroutine of instantiate_decl and instantiate_class_template.
10810 TMPL is the template to consider and TARGS is the list of arguments of
10814 perform_typedefs_access_check (tree tmpl
, tree targs
)
10816 location_t saved_location
;
10818 qualified_typedef_usage_t
*iter
;
10821 || (!CLASS_TYPE_P (tmpl
)
10822 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10825 saved_location
= input_location
;
10826 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10828 tree type_decl
= iter
->typedef_decl
;
10829 tree type_scope
= iter
->context
;
10831 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10834 if (uses_template_parms (type_decl
))
10835 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10836 if (uses_template_parms (type_scope
))
10837 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10839 /* Make access check error messages point to the location
10840 of the use of the typedef. */
10841 input_location
= iter
->locus
;
10842 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10843 type_decl
, type_decl
,
10844 tf_warning_or_error
);
10846 input_location
= saved_location
;
10850 instantiate_class_template_1 (tree type
)
10852 tree templ
, args
, pattern
, t
, member
;
10856 unsigned int saved_maximum_field_alignment
;
10859 if (type
== error_mark_node
)
10860 return error_mark_node
;
10862 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10863 || uses_template_parms (type
))
10866 /* Figure out which template is being instantiated. */
10867 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10868 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10870 /* Mark the type as in the process of being defined. */
10871 TYPE_BEING_DEFINED (type
) = 1;
10873 /* We may be in the middle of deferred access check. Disable
10875 deferring_access_check_sentinel
acs (dk_no_deferred
);
10877 /* Determine what specialization of the original template to
10879 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10880 if (t
== error_mark_node
)
10881 return error_mark_node
;
10884 /* This TYPE is actually an instantiation of a partial
10885 specialization. We replace the innermost set of ARGS with
10886 the arguments appropriate for substitution. For example,
10889 template <class T> struct S {};
10890 template <class T> struct S<T*> {};
10892 and supposing that we are instantiating S<int*>, ARGS will
10893 presently be {int*} -- but we need {int}. */
10894 pattern
= TREE_TYPE (t
);
10895 args
= TREE_PURPOSE (t
);
10899 pattern
= TREE_TYPE (templ
);
10900 args
= CLASSTYPE_TI_ARGS (type
);
10903 /* If the template we're instantiating is incomplete, then clearly
10904 there's nothing we can do. */
10905 if (!COMPLETE_TYPE_P (pattern
))
10907 /* We can try again later. */
10908 TYPE_BEING_DEFINED (type
) = 0;
10912 /* If we've recursively instantiated too many templates, stop. */
10913 if (! push_tinst_level (type
))
10916 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10917 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10919 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10920 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10921 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10922 fn_context
= error_mark_node
;
10924 push_to_top_level ();
10927 cp_unevaluated_operand
= 0;
10928 c_inhibit_evaluation_warnings
= 0;
10930 /* Use #pragma pack from the template context. */
10931 saved_maximum_field_alignment
= maximum_field_alignment
;
10932 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10934 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10936 /* Set the input location to the most specialized template definition.
10937 This is needed if tsubsting causes an error. */
10938 typedecl
= TYPE_MAIN_DECL (pattern
);
10939 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10940 DECL_SOURCE_LOCATION (typedecl
);
10942 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10943 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10944 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10945 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10946 if (ANON_AGGR_TYPE_P (pattern
))
10947 SET_ANON_AGGR_TYPE_P (type
);
10948 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10950 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10951 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10952 /* Adjust visibility for template arguments. */
10953 determine_visibility (TYPE_MAIN_DECL (type
));
10955 if (CLASS_TYPE_P (type
))
10956 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10958 pbinfo
= TYPE_BINFO (pattern
);
10960 /* We should never instantiate a nested class before its enclosing
10961 class; we need to look up the nested class by name before we can
10962 instantiate it, and that lookup should instantiate the enclosing
10964 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10965 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10967 base_list
= NULL_TREE
;
10968 if (BINFO_N_BASE_BINFOS (pbinfo
))
10974 /* We must enter the scope containing the type, as that is where
10975 the accessibility of types named in dependent bases are
10977 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10979 /* Substitute into each of the bases to determine the actual
10981 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10984 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10985 tree expanded_bases
= NULL_TREE
;
10988 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10991 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10992 args
, tf_error
, NULL_TREE
);
10993 if (expanded_bases
== error_mark_node
)
10996 len
= TREE_VEC_LENGTH (expanded_bases
);
10999 for (idx
= 0; idx
< len
; idx
++)
11001 if (expanded_bases
)
11002 /* Extract the already-expanded base class. */
11003 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11005 /* Substitute to figure out the base class. */
11006 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11009 if (base
== error_mark_node
)
11012 base_list
= tree_cons (access
, base
, base_list
);
11013 if (BINFO_VIRTUAL_P (pbase_binfo
))
11014 TREE_TYPE (base_list
) = integer_type_node
;
11018 /* The list is now in reverse order; correct that. */
11019 base_list
= nreverse (base_list
);
11022 pop_scope (pushed_scope
);
11024 /* Now call xref_basetypes to set up all the base-class
11026 xref_basetypes (type
, base_list
);
11028 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11029 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11030 args
, tf_error
, NULL_TREE
);
11031 fixup_attribute_variants (type
);
11033 /* Now that our base classes are set up, enter the scope of the
11034 class, so that name lookups into base classes, etc. will work
11035 correctly. This is precisely analogous to what we do in
11036 begin_class_definition when defining an ordinary non-template
11037 class, except we also need to push the enclosing classes. */
11038 push_nested_class (type
);
11040 /* Now members are processed in the order of declaration. */
11041 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11042 member
; member
= TREE_CHAIN (member
))
11044 tree t
= TREE_VALUE (member
);
11046 if (TREE_PURPOSE (member
))
11050 if (LAMBDA_TYPE_P (t
))
11051 /* A closure type for a lambda in an NSDMI or default argument.
11052 Ignore it; it will be regenerated when needed. */
11055 /* Build new CLASSTYPE_NESTED_UTDS. */
11058 bool class_template_p
;
11060 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11061 && TYPE_LANG_SPECIFIC (t
)
11062 && CLASSTYPE_IS_TEMPLATE (t
));
11063 /* If the member is a class template, then -- even after
11064 substitution -- there may be dependent types in the
11065 template argument list for the class. We increment
11066 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11067 that function will assume that no types are dependent
11068 when outside of a template. */
11069 if (class_template_p
)
11070 ++processing_template_decl
;
11071 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11072 if (class_template_p
)
11073 --processing_template_decl
;
11074 if (newtag
== error_mark_node
)
11077 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11079 tree name
= TYPE_IDENTIFIER (t
);
11081 if (class_template_p
)
11082 /* Unfortunately, lookup_template_class sets
11083 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11084 instantiation (i.e., for the type of a member
11085 template class nested within a template class.)
11086 This behavior is required for
11087 maybe_process_partial_specialization to work
11088 correctly, but is not accurate in this case;
11089 the TAG is not an instantiation of anything.
11090 (The corresponding TEMPLATE_DECL is an
11091 instantiation, but the TYPE is not.) */
11092 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11094 /* Now, we call pushtag to put this NEWTAG into the scope of
11095 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11096 pushtag calling push_template_decl. We don't have to do
11097 this for enums because it will already have been done in
11100 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11101 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11104 else if (DECL_DECLARES_FUNCTION_P (t
))
11108 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11109 ++processing_template_decl
;
11110 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11111 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11112 --processing_template_decl
;
11113 set_current_access_from_decl (r
);
11114 finish_member_declaration (r
);
11115 /* Instantiate members marked with attribute used. */
11116 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11118 if (TREE_CODE (r
) == FUNCTION_DECL
11119 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11120 cp_check_omp_declare_reduction (r
);
11122 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11123 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11124 /* A closure type for a lambda in an NSDMI or default argument.
11125 Ignore it; it will be regenerated when needed. */;
11128 /* Build new TYPE_FIELDS. */
11129 if (TREE_CODE (t
) == STATIC_ASSERT
)
11133 ++c_inhibit_evaluation_warnings
;
11135 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11136 tf_warning_or_error
, NULL_TREE
,
11137 /*integral_constant_expression_p=*/true);
11138 --c_inhibit_evaluation_warnings
;
11140 finish_static_assert (condition
,
11141 STATIC_ASSERT_MESSAGE (t
),
11142 STATIC_ASSERT_SOURCE_LOCATION (t
),
11143 /*member_p=*/true);
11145 else if (TREE_CODE (t
) != CONST_DECL
)
11148 tree vec
= NULL_TREE
;
11151 /* The file and line for this declaration, to
11152 assist in error message reporting. Since we
11153 called push_tinst_level above, we don't need to
11155 input_location
= DECL_SOURCE_LOCATION (t
);
11157 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11158 ++processing_template_decl
;
11159 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11160 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11161 --processing_template_decl
;
11163 if (TREE_CODE (r
) == TREE_VEC
)
11165 /* A capture pack became multiple fields. */
11167 len
= TREE_VEC_LENGTH (vec
);
11170 for (int i
= 0; i
< len
; ++i
)
11173 r
= TREE_VEC_ELT (vec
, i
);
11178 [t]he initialization (and any associated
11179 side-effects) of a static data member does
11180 not occur unless the static data member is
11181 itself used in a way that requires the
11182 definition of the static data member to
11185 Therefore, we do not substitute into the
11186 initialized for the static data member here. */
11187 finish_static_data_member_decl
11189 /*init=*/NULL_TREE
,
11190 /*init_const_expr_p=*/false,
11191 /*asmspec_tree=*/NULL_TREE
,
11193 /* Instantiate members marked with attribute used. */
11194 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11197 else if (TREE_CODE (r
) == FIELD_DECL
)
11199 /* Determine whether R has a valid type and can be
11200 completed later. If R is invalid, then its type
11201 is replaced by error_mark_node. */
11202 tree rtype
= TREE_TYPE (r
);
11203 if (can_complete_type_without_circularity (rtype
))
11204 complete_type (rtype
);
11206 if (!complete_or_array_type_p (rtype
))
11208 /* If R's type couldn't be completed and
11209 it isn't a flexible array member (whose
11210 type is incomplete by definition) give
11212 cxx_incomplete_type_error (r
, rtype
);
11213 TREE_TYPE (r
) = error_mark_node
;
11215 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11216 && TYPE_DOMAIN (rtype
) == NULL_TREE
11217 && (TREE_CODE (type
) == UNION_TYPE
11218 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11220 error ("flexible array member %qD in union", r
);
11221 TREE_TYPE (r
) = error_mark_node
;
11225 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11226 such a thing will already have been added to the field
11227 list by tsubst_enum in finish_member_declaration in the
11228 CLASSTYPE_NESTED_UTDS case above. */
11229 if (!(TREE_CODE (r
) == TYPE_DECL
11230 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11231 && DECL_ARTIFICIAL (r
)))
11233 set_current_access_from_decl (r
);
11234 finish_member_declaration (r
);
11242 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11243 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11245 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11247 tree friend_type
= t
;
11248 bool adjust_processing_template_decl
= false;
11250 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11252 /* template <class T> friend class C; */
11253 friend_type
= tsubst_friend_class (friend_type
, args
);
11254 adjust_processing_template_decl
= true;
11256 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11258 /* template <class T> friend class C::D; */
11259 friend_type
= tsubst (friend_type
, args
,
11260 tf_warning_or_error
, NULL_TREE
);
11261 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11262 friend_type
= TREE_TYPE (friend_type
);
11263 adjust_processing_template_decl
= true;
11265 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11266 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11268 /* This could be either
11272 when dependent_type_p is false or
11274 template <class U> friend class T::C;
11277 /* Bump processing_template_decl in case this is something like
11278 template <class T> friend struct A<T>::B. */
11279 ++processing_template_decl
;
11280 friend_type
= tsubst (friend_type
, args
,
11281 tf_warning_or_error
, NULL_TREE
);
11282 if (dependent_type_p (friend_type
))
11283 adjust_processing_template_decl
= true;
11284 --processing_template_decl
;
11286 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11287 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11288 && TYPE_HIDDEN_P (friend_type
))
11292 where C hasn't been declared yet. Let's lookup name
11293 from namespace scope directly, bypassing any name that
11294 come from dependent base class. */
11295 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11297 /* The call to xref_tag_from_type does injection for friend
11299 push_nested_namespace (ns
);
11301 xref_tag_from_type (friend_type
, NULL_TREE
,
11302 /*tag_scope=*/ts_current
);
11303 pop_nested_namespace (ns
);
11305 else if (uses_template_parms (friend_type
))
11306 /* friend class C<T>; */
11307 friend_type
= tsubst (friend_type
, args
,
11308 tf_warning_or_error
, NULL_TREE
);
11313 where C is already declared or
11315 friend class C<int>;
11317 We don't have to do anything in these cases. */
11319 if (adjust_processing_template_decl
)
11320 /* Trick make_friend_class into realizing that the friend
11321 we're adding is a template, not an ordinary class. It's
11322 important that we use make_friend_class since it will
11323 perform some error-checking and output cross-reference
11325 ++processing_template_decl
;
11327 if (friend_type
!= error_mark_node
)
11328 make_friend_class (type
, friend_type
, /*complain=*/false);
11330 if (adjust_processing_template_decl
)
11331 --processing_template_decl
;
11335 /* Build new DECL_FRIENDLIST. */
11338 /* The file and line for this declaration, to
11339 assist in error message reporting. Since we
11340 called push_tinst_level above, we don't need to
11342 input_location
= DECL_SOURCE_LOCATION (t
);
11344 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11346 ++processing_template_decl
;
11347 push_deferring_access_checks (dk_no_check
);
11350 r
= tsubst_friend_function (t
, args
);
11351 add_friend (type
, r
, /*complain=*/false);
11352 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11354 pop_deferring_access_checks ();
11355 --processing_template_decl
;
11363 /* Restore these before substituting into the lambda capture
11365 cp_unevaluated_operand
= saved_unevaluated_operand
;
11366 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11369 /* Set the file and line number information to whatever is given for
11370 the class itself. This puts error messages involving generated
11371 implicit functions at a predictable point, and the same point
11372 that would be used for non-template classes. */
11373 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11375 unreverse_member_declarations (type
);
11376 finish_struct_1 (type
);
11377 TYPE_BEING_DEFINED (type
) = 0;
11379 /* We don't instantiate default arguments for member functions. 14.7.1:
11381 The implicit instantiation of a class template specialization causes
11382 the implicit instantiation of the declarations, but not of the
11383 definitions or default arguments, of the class member functions,
11384 member classes, static data members and member templates.... */
11386 /* Some typedefs referenced from within the template code need to be access
11387 checked at template instantiation time, i.e now. These types were
11388 added to the template at parsing time. Let's get those and perform
11389 the access checks then. */
11390 perform_typedefs_access_check (pattern
, args
);
11391 perform_deferred_access_checks (tf_warning_or_error
);
11392 pop_nested_class ();
11393 maximum_field_alignment
= saved_maximum_field_alignment
;
11395 pop_from_top_level ();
11396 pop_tinst_level ();
11398 /* The vtable for a template class can be emitted in any translation
11399 unit in which the class is instantiated. When there is no key
11400 method, however, finish_struct_1 will already have added TYPE to
11401 the keyed_classes. */
11402 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11403 vec_safe_push (keyed_classes
, type
);
11408 /* Wrapper for instantiate_class_template_1. */
11411 instantiate_class_template (tree type
)
11414 timevar_push (TV_TEMPLATE_INST
);
11415 ret
= instantiate_class_template_1 (type
);
11416 timevar_pop (TV_TEMPLATE_INST
);
11421 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11427 else if (TYPE_P (t
))
11428 r
= tsubst (t
, args
, complain
, in_decl
);
11431 if (!(complain
& tf_warning
))
11432 ++c_inhibit_evaluation_warnings
;
11433 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11434 /*integral_constant_expression_p=*/true);
11435 if (!(complain
& tf_warning
))
11436 --c_inhibit_evaluation_warnings
;
11441 /* Given a function parameter pack TMPL_PARM and some function parameters
11442 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11443 and set *SPEC_P to point at the next point in the list. */
11446 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11448 /* Collect all of the extra "packed" parameters into an
11451 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11452 tree spec_parm
= *spec_p
;
11455 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11457 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11460 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11461 parmvec
= make_tree_vec (len
);
11462 spec_parm
= *spec_p
;
11463 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11465 tree elt
= spec_parm
;
11466 if (DECL_PACK_P (elt
))
11467 elt
= make_pack_expansion (elt
);
11468 TREE_VEC_ELT (parmvec
, i
) = elt
;
11471 /* Build the argument packs. */
11472 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11473 *spec_p
= spec_parm
;
11478 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11479 NONTYPE_ARGUMENT_PACK. */
11482 make_fnparm_pack (tree spec_parm
)
11484 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11487 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11488 pack expansion with no extra args, 2 if it has extra args, or 0
11489 if it is not a pack expansion. */
11492 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11494 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11495 if (i
>= TREE_VEC_LENGTH (vec
))
11497 tree elt
= TREE_VEC_ELT (vec
, i
);
11499 /* A decl pack is itself an expansion. */
11500 elt
= TREE_TYPE (elt
);
11501 if (!PACK_EXPANSION_P (elt
))
11503 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11509 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11512 make_argument_pack_select (tree arg_pack
, unsigned index
)
11514 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11516 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11517 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11522 /* This is a subroutine of tsubst_pack_expansion.
11524 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11525 mechanism to store the (non complete list of) arguments of the
11526 substitution and return a non substituted pack expansion, in order
11527 to wait for when we have enough arguments to really perform the
11531 use_pack_expansion_extra_args_p (tree parm_packs
,
11533 bool has_empty_arg
)
11535 /* If one pack has an expansion and another pack has a normal
11536 argument or if one pack has an empty argument and an another
11537 one hasn't then tsubst_pack_expansion cannot perform the
11538 substitution and need to fall back on the
11539 PACK_EXPANSION_EXTRA mechanism. */
11540 if (parm_packs
== NULL_TREE
)
11542 else if (has_empty_arg
)
11545 bool has_expansion_arg
= false;
11546 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11548 bool has_non_expansion_arg
= false;
11549 for (tree parm_pack
= parm_packs
;
11551 parm_pack
= TREE_CHAIN (parm_pack
))
11553 tree arg
= TREE_VALUE (parm_pack
);
11555 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11557 /* We can't substitute a pack expansion with extra args into
11561 has_expansion_arg
= true;
11563 has_non_expansion_arg
= true;
11566 if (has_expansion_arg
&& has_non_expansion_arg
)
11572 /* [temp.variadic]/6 says that:
11574 The instantiation of a pack expansion [...]
11575 produces a list E1,E2, ..., En, where N is the number of elements
11576 in the pack expansion parameters.
11578 This subroutine of tsubst_pack_expansion produces one of these Ei.
11580 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11581 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11582 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11583 INDEX is the index 'i' of the element Ei to produce. ARGS,
11584 COMPLAIN, and IN_DECL are the same parameters as for the
11585 tsubst_pack_expansion function.
11587 The function returns the resulting Ei upon successful completion,
11588 or error_mark_node.
11590 Note that this function possibly modifies the ARGS parameter, so
11591 it's the responsibility of the caller to restore it. */
11594 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11597 tree args
/* This parm gets
11599 tsubst_flags_t complain
,
11603 bool ith_elem_is_expansion
= false;
11605 /* For each parameter pack, change the substitution of the parameter
11606 pack to the ith argument in its argument pack, then expand the
11608 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11610 tree parm
= TREE_PURPOSE (pack
);
11611 tree arg_pack
= TREE_VALUE (pack
);
11612 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11614 ith_elem_is_expansion
|=
11615 argument_pack_element_is_expansion_p (arg_pack
, index
);
11617 /* Select the Ith argument from the pack. */
11618 if (TREE_CODE (parm
) == PARM_DECL
11620 || TREE_CODE (parm
) == FIELD_DECL
)
11624 aps
= make_argument_pack_select (arg_pack
, index
);
11625 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11626 return error_mark_node
;
11627 register_local_specialization (aps
, parm
);
11630 aps
= retrieve_local_specialization (parm
);
11635 template_parm_level_and_index (parm
, &level
, &idx
);
11639 aps
= make_argument_pack_select (arg_pack
, index
);
11640 /* Update the corresponding argument. */
11641 TMPL_ARG (args
, level
, idx
) = aps
;
11644 /* Re-use the ARGUMENT_PACK_SELECT. */
11645 aps
= TMPL_ARG (args
, level
, idx
);
11647 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11650 /* Substitute into the PATTERN with the (possibly altered)
11652 if (pattern
== in_decl
)
11653 /* Expanding a fixed parameter pack from
11654 coerce_template_parameter_pack. */
11655 t
= tsubst_decl (pattern
, args
, complain
);
11656 else if (pattern
== error_mark_node
)
11657 t
= error_mark_node
;
11658 else if (constraint_p (pattern
))
11660 if (processing_template_decl
)
11661 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11663 t
= (constraints_satisfied_p (pattern
, args
)
11664 ? boolean_true_node
: boolean_false_node
);
11666 else if (!TYPE_P (pattern
))
11667 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11668 /*integral_constant_expression_p=*/false);
11670 t
= tsubst (pattern
, args
, complain
, in_decl
);
11672 /* If the Ith argument pack element is a pack expansion, then
11673 the Ith element resulting from the substituting is going to
11674 be a pack expansion as well. */
11675 if (ith_elem_is_expansion
)
11676 t
= make_pack_expansion (t
, complain
);
11681 /* When the unexpanded parameter pack in a fold expression expands to an empty
11682 sequence, the value of the expression is as follows; the program is
11683 ill-formed if the operator is not listed in this table.
11690 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11692 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11693 if (!FOLD_EXPR_MODIFY_P (t
))
11696 case TRUTH_ANDIF_EXPR
:
11697 return boolean_true_node
;
11698 case TRUTH_ORIF_EXPR
:
11699 return boolean_false_node
;
11700 case COMPOUND_EXPR
:
11706 if (complain
& tf_error
)
11707 error_at (location_of (t
),
11708 "fold of empty expansion over %O", code
);
11709 return error_mark_node
;
11712 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11713 form an expression that combines the two terms using the
11717 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11719 tree op
= FOLD_EXPR_OP (t
);
11720 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11722 // Handle compound assignment operators.
11723 if (FOLD_EXPR_MODIFY_P (t
))
11724 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11728 case COMPOUND_EXPR
:
11729 return build_x_compound_expr (input_location
, left
, right
, complain
);
11731 return build_m_component_ref (left
, right
, complain
);
11733 return build_x_binary_op (input_location
, code
,
11734 left
, TREE_CODE (left
),
11735 right
, TREE_CODE (right
),
11741 /* Substitute ARGS into the pack of a fold expression T. */
11744 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11746 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11749 /* Substitute ARGS into the pack of a fold expression T. */
11752 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11754 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11757 /* Expand a PACK of arguments into a grouped as left fold.
11758 Given a pack containing elements A0, A1, ..., An and an
11759 operator @, this builds the expression:
11761 ((A0 @ A1) @ A2) ... @ An
11763 Note that PACK must not be empty.
11765 The operator is defined by the original fold expression T. */
11768 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11770 tree left
= TREE_VEC_ELT (pack
, 0);
11771 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11773 tree right
= TREE_VEC_ELT (pack
, i
);
11774 left
= fold_expression (t
, left
, right
, complain
);
11779 /* Substitute into a unary left fold expression. */
11782 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11785 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11786 if (pack
== error_mark_node
)
11787 return error_mark_node
;
11788 if (PACK_EXPANSION_P (pack
))
11790 tree r
= copy_node (t
);
11791 FOLD_EXPR_PACK (r
) = pack
;
11794 if (TREE_VEC_LENGTH (pack
) == 0)
11795 return expand_empty_fold (t
, complain
);
11797 return expand_left_fold (t
, pack
, complain
);
11800 /* Substitute into a binary left fold expression.
11802 Do ths by building a single (non-empty) vector of argumnts and
11803 building the expression from those elements. */
11806 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11809 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11810 if (pack
== error_mark_node
)
11811 return error_mark_node
;
11812 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11813 if (init
== error_mark_node
)
11814 return error_mark_node
;
11816 if (PACK_EXPANSION_P (pack
))
11818 tree r
= copy_node (t
);
11819 FOLD_EXPR_PACK (r
) = pack
;
11820 FOLD_EXPR_INIT (r
) = init
;
11824 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11825 TREE_VEC_ELT (vec
, 0) = init
;
11826 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11827 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11829 return expand_left_fold (t
, vec
, complain
);
11832 /* Expand a PACK of arguments into a grouped as right fold.
11833 Given a pack containing elementns A0, A1, ..., and an
11834 operator @, this builds the expression:
11836 A0@ ... (An-2 @ (An-1 @ An))
11838 Note that PACK must not be empty.
11840 The operator is defined by the original fold expression T. */
11843 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11845 // Build the expression.
11846 int n
= TREE_VEC_LENGTH (pack
);
11847 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11848 for (--n
; n
!= 0; --n
)
11850 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11851 right
= fold_expression (t
, left
, right
, complain
);
11856 /* Substitute into a unary right fold expression. */
11859 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11862 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11863 if (pack
== error_mark_node
)
11864 return error_mark_node
;
11865 if (PACK_EXPANSION_P (pack
))
11867 tree r
= copy_node (t
);
11868 FOLD_EXPR_PACK (r
) = pack
;
11871 if (TREE_VEC_LENGTH (pack
) == 0)
11872 return expand_empty_fold (t
, complain
);
11874 return expand_right_fold (t
, pack
, complain
);
11877 /* Substitute into a binary right fold expression.
11879 Do ths by building a single (non-empty) vector of arguments and
11880 building the expression from those elements. */
11883 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11886 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11887 if (pack
== error_mark_node
)
11888 return error_mark_node
;
11889 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11890 if (init
== error_mark_node
)
11891 return error_mark_node
;
11893 if (PACK_EXPANSION_P (pack
))
11895 tree r
= copy_node (t
);
11896 FOLD_EXPR_PACK (r
) = pack
;
11897 FOLD_EXPR_INIT (r
) = init
;
11901 int n
= TREE_VEC_LENGTH (pack
);
11902 tree vec
= make_tree_vec (n
+ 1);
11903 for (int i
= 0; i
< n
; ++i
)
11904 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11905 TREE_VEC_ELT (vec
, n
) = init
;
11907 return expand_right_fold (t
, vec
, complain
);
11910 /* Walk through the pattern of a pack expansion, adding everything in
11911 local_specializations to a list. */
11915 hash_set
<tree
> internal
;
11917 tsubst_flags_t complain
;
11919 el_data (tsubst_flags_t c
)
11920 : extra (NULL_TREE
), complain (c
) {}
11923 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11925 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11926 tree
*extra
= &data
.extra
;
11927 tsubst_flags_t complain
= data
.complain
;
11929 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
11930 /* Remember local typedefs (85214). */
11931 tp
= &TYPE_NAME (*tp
);
11933 if (TREE_CODE (*tp
) == DECL_EXPR
)
11934 data
.internal
.add (DECL_EXPR_DECL (*tp
));
11935 else if (tree spec
= retrieve_local_specialization (*tp
))
11937 if (data
.internal
.contains (*tp
))
11938 /* Don't mess with variables declared within the pattern. */
11940 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11942 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11943 tree args
= ARGUMENT_PACK_ARGS (spec
);
11944 if (TREE_VEC_LENGTH (args
) == 1)
11946 tree elt
= TREE_VEC_ELT (args
, 0);
11947 if (PACK_EXPANSION_P (elt
))
11948 elt
= PACK_EXPANSION_PATTERN (elt
);
11949 if (DECL_PACK_P (elt
))
11952 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11954 /* Handle lambda capture here, since we aren't doing any
11955 substitution now, and so tsubst_copy won't call
11956 process_outer_var_ref. */
11957 tree args
= ARGUMENT_PACK_ARGS (spec
);
11958 int len
= TREE_VEC_LENGTH (args
);
11959 for (int i
= 0; i
< len
; ++i
)
11961 tree arg
= TREE_VEC_ELT (args
, i
);
11963 if (outer_automatic_var_p (arg
))
11964 carg
= process_outer_var_ref (arg
, complain
);
11967 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11971 spec
= copy_node (spec
);
11972 args
= copy_node (args
);
11973 SET_ARGUMENT_PACK_ARGS (spec
, args
);
11974 register_local_specialization (spec
, *tp
);
11976 TREE_VEC_ELT (args
, i
) = carg
;
11981 if (outer_automatic_var_p (spec
))
11982 spec
= process_outer_var_ref (spec
, complain
);
11983 *extra
= tree_cons (*tp
, spec
, *extra
);
11988 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
11990 el_data
data (complain
);
11991 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
11995 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
11996 for use in PACK_EXPANSION_EXTRA_ARGS. */
11999 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12002 if (local_specializations
)
12003 if (tree locals
= extract_local_specs (pattern
, complain
))
12004 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12008 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12009 normal template args to ARGS. */
12012 add_extra_args (tree extra
, tree args
)
12014 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12016 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12018 /* The partial instantiation involved local declarations collected in
12019 extract_local_specs; map from the general template to our local
12021 tree gen
= TREE_PURPOSE (elt
);
12022 tree inst
= TREE_VALUE (elt
);
12024 if (tree local
= retrieve_local_specialization (inst
))
12026 /* else inst is already a full instantiation of the pack. */
12027 register_local_specialization (inst
, gen
);
12029 gcc_assert (!TREE_PURPOSE (extra
));
12030 extra
= TREE_VALUE (extra
);
12032 return add_to_template_args (extra
, args
);
12035 /* Substitute ARGS into T, which is an pack expansion
12036 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12037 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12038 (if only a partial substitution could be performed) or
12039 ERROR_MARK_NODE if there was an error. */
12041 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12045 tree pack
, packs
= NULL_TREE
;
12046 bool unsubstituted_packs
= false;
12047 bool unsubstituted_fn_pack
= false;
12050 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12051 bool need_local_specializations
= false;
12054 gcc_assert (PACK_EXPANSION_P (t
));
12055 pattern
= PACK_EXPANSION_PATTERN (t
);
12057 /* Add in any args remembered from an earlier partial instantiation. */
12058 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12060 levels
= TMPL_ARGS_DEPTH (args
);
12062 /* Determine the argument packs that will instantiate the parameter
12063 packs used in the expansion expression. While we're at it,
12064 compute the number of arguments to be expanded and make sure it
12066 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12067 pack
= TREE_CHAIN (pack
))
12069 tree parm_pack
= TREE_VALUE (pack
);
12070 tree arg_pack
= NULL_TREE
;
12071 tree orig_arg
= NULL_TREE
;
12074 if (TREE_CODE (parm_pack
) == BASES
)
12076 gcc_assert (parm_pack
== pattern
);
12077 if (BASES_DIRECT (parm_pack
))
12078 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12083 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12084 args
, complain
, in_decl
,
12087 else if (builtin_pack_call_p (parm_pack
))
12089 if (parm_pack
!= pattern
)
12091 if (complain
& tf_error
)
12092 sorry ("%qE is not the entire pattern of the pack expansion",
12094 return error_mark_node
;
12096 return expand_builtin_pack_call (parm_pack
, args
,
12097 complain
, in_decl
);
12099 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12101 /* We know we have correct local_specializations if this
12102 expansion is at function scope, or if we're dealing with a
12103 local parameter in a requires expression; for the latter,
12104 tsubst_requires_expr set it up appropriately. */
12105 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12106 arg_pack
= retrieve_local_specialization (parm_pack
);
12108 /* We can't rely on local_specializations for a parameter
12109 name used later in a function declaration (such as in a
12110 late-specified return type). Even if it exists, it might
12111 have the wrong value for a recursive call. */
12112 need_local_specializations
= true;
12116 /* This parameter pack was used in an unevaluated context. Just
12117 make a dummy decl, since it's only used for its type. */
12118 ++cp_unevaluated_operand
;
12119 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12120 --cp_unevaluated_operand
;
12121 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12122 /* Partial instantiation of the parm_pack, we can't build
12123 up an argument pack yet. */
12124 arg_pack
= NULL_TREE
;
12126 arg_pack
= make_fnparm_pack (arg_pack
);
12128 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12129 /* This argument pack isn't fully instantiated yet. We set this
12130 flag rather than clear arg_pack because we do want to do the
12131 optimization below, and we don't want to substitute directly
12132 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12133 where it isn't expected). */
12134 unsubstituted_fn_pack
= true;
12136 else if (is_normal_capture_proxy (parm_pack
))
12138 arg_pack
= retrieve_local_specialization (parm_pack
);
12139 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12140 unsubstituted_fn_pack
= true;
12145 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12147 if (level
<= levels
)
12148 arg_pack
= TMPL_ARG (args
, level
, idx
);
12151 orig_arg
= arg_pack
;
12152 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12153 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12155 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12156 /* This can only happen if we forget to expand an argument
12157 pack somewhere else. Just return an error, silently. */
12159 result
= make_tree_vec (1);
12160 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12167 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12169 /* Don't bother trying to do a partial substitution with
12170 incomplete packs; we'll try again after deduction. */
12171 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12176 else if (len
!= my_len
12177 && !unsubstituted_fn_pack
)
12179 if (!(complain
& tf_error
))
12180 /* Fail quietly. */;
12181 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12182 error ("mismatched argument pack lengths while expanding %qT",
12185 error ("mismatched argument pack lengths while expanding %qE",
12187 return error_mark_node
;
12190 /* Keep track of the parameter packs and their corresponding
12192 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12193 TREE_TYPE (packs
) = orig_arg
;
12197 /* We can't substitute for this parameter pack. We use a flag as
12198 well as the missing_level counter because function parameter
12199 packs don't have a level. */
12200 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12201 unsubstituted_packs
= true;
12205 /* If the expansion is just T..., return the matching argument pack, unless
12206 we need to call convert_from_reference on all the elements. This is an
12207 important optimization; see c++/68422. */
12208 if (!unsubstituted_packs
12209 && TREE_PURPOSE (packs
) == pattern
)
12211 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12213 /* If the argument pack is a single pack expansion, pull it out. */
12214 if (TREE_VEC_LENGTH (args
) == 1
12215 && pack_expansion_args_count (args
))
12216 return TREE_VEC_ELT (args
, 0);
12218 /* Types need no adjustment, nor does sizeof..., and if we still have
12219 some pack expansion args we won't do anything yet. */
12220 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12221 || PACK_EXPANSION_SIZEOF_P (t
)
12222 || pack_expansion_args_count (args
))
12224 /* Also optimize expression pack expansions if we can tell that the
12225 elements won't have reference type. */
12226 tree type
= TREE_TYPE (pattern
);
12227 if (type
&& !TYPE_REF_P (type
)
12228 && !PACK_EXPANSION_P (type
)
12229 && !WILDCARD_TYPE_P (type
))
12231 /* Otherwise use the normal path so we get convert_from_reference. */
12234 /* We cannot expand this expansion expression, because we don't have
12235 all of the argument packs we need. */
12236 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12238 /* We got some full packs, but we can't substitute them in until we
12239 have values for all the packs. So remember these until then. */
12241 t
= make_pack_expansion (pattern
, complain
);
12242 PACK_EXPANSION_EXTRA_ARGS (t
)
12243 = build_extra_args (pattern
, args
, complain
);
12246 else if (unsubstituted_packs
)
12248 /* There were no real arguments, we're just replacing a parameter
12249 pack with another version of itself. Substitute into the
12250 pattern and return a PACK_EXPANSION_*. The caller will need to
12252 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12253 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12254 /*integral_constant_expression_p=*/false);
12256 t
= tsubst (pattern
, args
, complain
, in_decl
);
12257 t
= make_pack_expansion (t
, complain
);
12261 gcc_assert (len
>= 0);
12263 if (need_local_specializations
)
12265 /* We're in a late-specified return type, so create our own local
12266 specializations map; the current map is either NULL or (in the
12267 case of recursive unification) might have bindings that we don't
12268 want to use or alter. */
12269 saved_local_specializations
= local_specializations
;
12270 local_specializations
= new hash_map
<tree
, tree
>;
12273 /* For each argument in each argument pack, substitute into the
12275 result
= make_tree_vec (len
);
12276 tree elem_args
= copy_template_args (args
);
12277 for (i
= 0; i
< len
; ++i
)
12279 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12281 elem_args
, complain
,
12283 TREE_VEC_ELT (result
, i
) = t
;
12284 if (t
== error_mark_node
)
12286 result
= error_mark_node
;
12291 /* Update ARGS to restore the substitution from parameter packs to
12292 their argument packs. */
12293 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12295 tree parm
= TREE_PURPOSE (pack
);
12297 if (TREE_CODE (parm
) == PARM_DECL
12299 || TREE_CODE (parm
) == FIELD_DECL
)
12300 register_local_specialization (TREE_TYPE (pack
), parm
);
12305 if (TREE_VALUE (pack
) == NULL_TREE
)
12308 template_parm_level_and_index (parm
, &level
, &idx
);
12310 /* Update the corresponding argument. */
12311 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12312 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12315 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12319 if (need_local_specializations
)
12321 delete local_specializations
;
12322 local_specializations
= saved_local_specializations
;
12325 /* If the dependent pack arguments were such that we end up with only a
12326 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12327 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12328 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12329 return TREE_VEC_ELT (result
, 0);
12334 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12335 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12336 parameter packs; all parms generated from a function parameter pack will
12337 have the same DECL_PARM_INDEX. */
12340 get_pattern_parm (tree parm
, tree tmpl
)
12342 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12345 if (DECL_ARTIFICIAL (parm
))
12347 for (patparm
= DECL_ARGUMENTS (pattern
);
12348 patparm
; patparm
= DECL_CHAIN (patparm
))
12349 if (DECL_ARTIFICIAL (patparm
)
12350 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12355 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12356 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12357 gcc_assert (DECL_PARM_INDEX (patparm
)
12358 == DECL_PARM_INDEX (parm
));
12364 /* Make an argument pack out of the TREE_VEC VEC. */
12367 make_argument_pack (tree vec
)
12370 tree elt
= TREE_VEC_ELT (vec
, 0);
12372 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12375 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12376 TREE_CONSTANT (pack
) = 1;
12378 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12382 /* Return an exact copy of template args T that can be modified
12386 copy_template_args (tree t
)
12388 if (t
== error_mark_node
)
12391 int len
= TREE_VEC_LENGTH (t
);
12392 tree new_vec
= make_tree_vec (len
);
12394 for (int i
= 0; i
< len
; ++i
)
12396 tree elt
= TREE_VEC_ELT (t
, i
);
12397 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12398 elt
= copy_template_args (elt
);
12399 TREE_VEC_ELT (new_vec
, i
) = elt
;
12402 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12403 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12408 /* Substitute ARGS into the vector or list of template arguments T. */
12411 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12414 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12417 if (t
== error_mark_node
)
12418 return error_mark_node
;
12420 len
= TREE_VEC_LENGTH (t
);
12421 elts
= XALLOCAVEC (tree
, len
);
12423 for (i
= 0; i
< len
; i
++)
12425 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12428 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12429 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12430 else if (PACK_EXPANSION_P (orig_arg
))
12432 /* Substitute into an expansion expression. */
12433 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12435 if (TREE_CODE (new_arg
) == TREE_VEC
)
12436 /* Add to the expanded length adjustment the number of
12437 expanded arguments. We subtract one from this
12438 measurement, because the argument pack expression
12439 itself is already counted as 1 in
12440 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12441 the argument pack is empty. */
12442 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12444 else if (ARGUMENT_PACK_P (orig_arg
))
12446 /* Substitute into each of the arguments. */
12447 new_arg
= TYPE_P (orig_arg
)
12448 ? cxx_make_type (TREE_CODE (orig_arg
))
12449 : make_node (TREE_CODE (orig_arg
));
12451 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12452 args
, complain
, in_decl
);
12453 if (pack_args
== error_mark_node
)
12454 new_arg
= error_mark_node
;
12456 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12458 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12459 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12462 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12464 if (new_arg
== error_mark_node
)
12465 return error_mark_node
;
12468 if (new_arg
!= orig_arg
)
12475 /* Make space for the expanded arguments coming from template
12477 t
= make_tree_vec (len
+ expanded_len_adjust
);
12478 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12479 arguments for a member template.
12480 In that case each TREE_VEC in ORIG_T represents a level of template
12481 arguments, and ORIG_T won't carry any non defaulted argument count.
12482 It will rather be the nested TREE_VECs that will carry one.
12483 In other words, ORIG_T carries a non defaulted argument count only
12484 if it doesn't contain any nested TREE_VEC. */
12485 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12487 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12488 count
+= expanded_len_adjust
;
12489 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12491 for (i
= 0, out
= 0; i
< len
; i
++)
12493 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12494 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12495 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12499 /* Now expand the template argument pack "in place". */
12500 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12501 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12505 TREE_VEC_ELT (t
, out
) = elts
[i
];
12513 /* Substitute ARGS into one level PARMS of template parameters. */
12516 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12518 if (parms
== error_mark_node
)
12519 return error_mark_node
;
12521 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12523 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12525 tree tuple
= TREE_VEC_ELT (parms
, i
);
12527 if (tuple
== error_mark_node
)
12530 TREE_VEC_ELT (new_vec
, i
) =
12531 tsubst_template_parm (tuple
, args
, complain
);
12537 /* Return the result of substituting ARGS into the template parameters
12538 given by PARMS. If there are m levels of ARGS and m + n levels of
12539 PARMS, then the result will contain n levels of PARMS. For
12540 example, if PARMS is `template <class T> template <class U>
12541 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12542 result will be `template <int*, double, class V>'. */
12545 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12547 tree r
= NULL_TREE
;
12550 /* When substituting into a template, we must set
12551 PROCESSING_TEMPLATE_DECL as the template parameters may be
12552 dependent if they are based on one-another, and the dependency
12553 predicates are short-circuit outside of templates. */
12554 ++processing_template_decl
;
12556 for (new_parms
= &r
;
12557 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12558 new_parms
= &(TREE_CHAIN (*new_parms
)),
12559 parms
= TREE_CHAIN (parms
))
12561 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12564 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12565 - TMPL_ARGS_DEPTH (args
)),
12566 new_vec
, NULL_TREE
);
12569 --processing_template_decl
;
12574 /* Return the result of substituting ARGS into one template parameter
12575 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12576 parameter and which TREE_PURPOSE is the default argument of the
12577 template parameter. */
12580 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12582 tree default_value
, parm_decl
;
12584 if (args
== NULL_TREE
12586 || t
== error_mark_node
)
12589 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12591 default_value
= TREE_PURPOSE (t
);
12592 parm_decl
= TREE_VALUE (t
);
12594 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12595 if (TREE_CODE (parm_decl
) == PARM_DECL
12596 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12597 parm_decl
= error_mark_node
;
12598 default_value
= tsubst_template_arg (default_value
, args
,
12599 complain
, NULL_TREE
);
12601 return build_tree_list (default_value
, parm_decl
);
12604 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12605 type T. If T is not an aggregate or enumeration type, it is
12606 handled as if by tsubst. IN_DECL is as for tsubst. If
12607 ENTERING_SCOPE is nonzero, T is the context for a template which
12608 we are presently tsubst'ing. Return the substituted value. */
12611 tsubst_aggr_type (tree t
,
12613 tsubst_flags_t complain
,
12615 int entering_scope
)
12617 if (t
== NULL_TREE
)
12620 switch (TREE_CODE (t
))
12623 if (TYPE_PTRMEMFUNC_P (t
))
12624 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12626 /* Fall through. */
12627 case ENUMERAL_TYPE
:
12629 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12634 int saved_unevaluated_operand
;
12635 int saved_inhibit_evaluation_warnings
;
12637 /* In "sizeof(X<I>)" we need to evaluate "I". */
12638 saved_unevaluated_operand
= cp_unevaluated_operand
;
12639 cp_unevaluated_operand
= 0;
12640 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
12641 c_inhibit_evaluation_warnings
= 0;
12643 /* First, determine the context for the type we are looking
12645 context
= TYPE_CONTEXT (t
);
12646 if (context
&& TYPE_P (context
))
12648 context
= tsubst_aggr_type (context
, args
, complain
,
12649 in_decl
, /*entering_scope=*/1);
12650 /* If context is a nested class inside a class template,
12651 it may still need to be instantiated (c++/33959). */
12652 context
= complete_type (context
);
12655 /* Then, figure out what arguments are appropriate for the
12656 type we are trying to find. For example, given:
12658 template <class T> struct S;
12659 template <class T, class U> void f(T, U) { S<U> su; }
12661 and supposing that we are instantiating f<int, double>,
12662 then our ARGS will be {int, double}, but, when looking up
12663 S we only want {double}. */
12664 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12665 complain
, in_decl
);
12666 if (argvec
== error_mark_node
)
12667 r
= error_mark_node
;
12670 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12671 entering_scope
, complain
);
12672 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12675 cp_unevaluated_operand
= saved_unevaluated_operand
;
12676 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
12681 /* This is not a template type, so there's nothing to do. */
12685 return tsubst (t
, args
, complain
, in_decl
);
12689 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12691 /* Substitute into the default argument ARG (a default argument for
12692 FN), which has the indicated TYPE. */
12695 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12696 tsubst_flags_t complain
)
12698 int errs
= errorcount
+ sorrycount
;
12700 /* This can happen in invalid code. */
12701 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12704 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12705 parm
= chain_index (parmnum
, parm
);
12706 tree parmtype
= TREE_TYPE (parm
);
12707 if (DECL_BY_REFERENCE (parm
))
12708 parmtype
= TREE_TYPE (parmtype
);
12709 if (parmtype
== error_mark_node
)
12710 return error_mark_node
;
12712 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12715 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12718 /* This default argument came from a template. Instantiate the
12719 default argument here, not in tsubst. In the case of
12728 we must be careful to do name lookup in the scope of S<T>,
12729 rather than in the current class. */
12730 push_to_top_level ();
12731 push_access_scope (fn
);
12732 start_lambda_scope (parm
);
12734 /* The default argument expression may cause implicitly defined
12735 member functions to be synthesized, which will result in garbage
12736 collection. We must treat this situation as if we were within
12737 the body of function so as to avoid collecting live data on the
12740 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12741 complain
, NULL_TREE
,
12742 /*integral_constant_expression_p=*/false);
12745 finish_lambda_scope ();
12747 if (errorcount
+sorrycount
> errs
12748 && (complain
& tf_warning_or_error
))
12749 inform (input_location
,
12750 " when instantiating default argument for call to %qD", fn
);
12752 /* Make sure the default argument is reasonable. */
12753 arg
= check_default_argument (type
, arg
, complain
);
12755 pop_access_scope (fn
);
12756 pop_from_top_level ();
12758 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12761 defarg_inst
= tree_cache_map::create_ggc (37);
12762 defarg_inst
->put (parm
, arg
);
12768 /* Substitute into all the default arguments for FN. */
12771 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12776 tmpl_args
= DECL_TI_ARGS (fn
);
12778 /* If this function is not yet instantiated, we certainly don't need
12779 its default arguments. */
12780 if (uses_template_parms (tmpl_args
))
12782 /* Don't do this again for clones. */
12783 if (DECL_CLONED_FUNCTION_P (fn
))
12787 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12789 arg
= TREE_CHAIN (arg
), ++i
)
12790 if (TREE_PURPOSE (arg
))
12791 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12793 TREE_PURPOSE (arg
),
12797 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12800 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12801 tree lambda_fntype
)
12803 tree gen_tmpl
, argvec
;
12804 hashval_t hash
= 0;
12807 /* Nobody should be tsubst'ing into non-template functions. */
12808 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12810 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12812 /* If T is not dependent, just return it. */
12813 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12816 /* Calculate the most general template of which R is a
12818 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12820 /* We're substituting a lambda function under tsubst_lambda_expr but not
12821 directly from it; find the matching function we're already inside.
12822 But don't do this if T is a generic lambda with a single level of
12823 template parms, as in that case we're doing a normal instantiation. */
12824 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12825 && (!generic_lambda_fn_p (t
)
12826 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12827 return enclosing_instantiation_of (t
);
12829 /* Calculate the complete set of arguments used to
12831 argvec
= tsubst_template_args (DECL_TI_ARGS
12832 (DECL_TEMPLATE_RESULT
12833 (DECL_TI_TEMPLATE (t
))),
12834 args
, complain
, in_decl
);
12835 if (argvec
== error_mark_node
)
12836 return error_mark_node
;
12838 /* Check to see if we already have this specialization. */
12839 if (!lambda_fntype
)
12841 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12842 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12846 /* We can see more levels of arguments than parameters if
12847 there was a specialization of a member template, like
12850 template <class T> struct S { template <class U> void f(); }
12851 template <> template <class U> void S<int>::f(U);
12853 Here, we'll be substituting into the specialization,
12854 because that's where we can find the code we actually
12855 want to generate, but we'll have enough arguments for
12856 the most general template.
12858 We also deal with the peculiar case:
12860 template <class T> struct S {
12861 template <class U> friend void f();
12863 template <class U> void f() {}
12865 template void f<double>();
12867 Here, the ARGS for the instantiation of will be {int,
12868 double}. But, we only need as many ARGS as there are
12869 levels of template parameters in CODE_PATTERN. We are
12870 careful not to get fooled into reducing the ARGS in
12873 template <class T> struct S { template <class U> void f(U); }
12874 template <class T> template <> void S<T>::f(int) {}
12876 which we can spot because the pattern will be a
12877 specialization in this case. */
12878 int args_depth
= TMPL_ARGS_DEPTH (args
);
12880 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12882 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12883 args
= get_innermost_template_args (args
, parms_depth
);
12887 /* This special case arises when we have something like this:
12889 template <class T> struct S {
12890 friend void f<int>(int, double);
12893 Here, the DECL_TI_TEMPLATE for the friend declaration
12894 will be an IDENTIFIER_NODE. We are being called from
12895 tsubst_friend_function, and we want only to create a
12896 new decl (R) with appropriate types so that we can call
12897 determine_specialization. */
12898 gen_tmpl
= NULL_TREE
;
12899 argvec
= NULL_TREE
;
12902 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12904 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12905 bool member
= ctx
&& TYPE_P (ctx
);
12907 if (member
&& !closure
)
12908 ctx
= tsubst_aggr_type (ctx
, args
,
12909 complain
, t
, /*entering_scope=*/1);
12911 tree type
= (lambda_fntype
? lambda_fntype
12912 : tsubst (TREE_TYPE (t
), args
,
12913 complain
| tf_fndecl_type
, in_decl
));
12914 if (type
== error_mark_node
)
12915 return error_mark_node
;
12917 /* If we hit excessive deduction depth, the type is bogus even if
12918 it isn't error_mark_node, so don't build a decl. */
12919 if (excessive_deduction_depth
)
12920 return error_mark_node
;
12922 /* We do NOT check for matching decls pushed separately at this
12923 point, as they may not represent instantiations of this
12924 template, and in any case are considered separate under the
12926 tree r
= copy_decl (t
);
12927 DECL_USE_TEMPLATE (r
) = 0;
12928 TREE_TYPE (r
) = type
;
12929 /* Clear out the mangled name and RTL for the instantiation. */
12930 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12931 SET_DECL_RTL (r
, NULL
);
12932 /* Leave DECL_INITIAL set on deleted instantiations. */
12933 if (!DECL_DELETED_FN (r
))
12934 DECL_INITIAL (r
) = NULL_TREE
;
12935 DECL_CONTEXT (r
) = ctx
;
12937 /* OpenMP UDRs have the only argument a reference to the declared
12938 type. We want to diagnose if the declared type is a reference,
12939 which is invalid, but as references to references are usually
12940 quietly merged, diagnose it here. */
12941 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12944 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12945 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12946 if (TYPE_REF_P (argtype
))
12947 error_at (DECL_SOURCE_LOCATION (t
),
12948 "reference type %qT in "
12949 "%<#pragma omp declare reduction%>", argtype
);
12950 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12951 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12955 if (member
&& DECL_CONV_FN_P (r
))
12956 /* Type-conversion operator. Reconstruct the name, in
12957 case it's the name of one of the template's parameters. */
12958 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
12960 tree parms
= DECL_ARGUMENTS (t
);
12962 parms
= DECL_CHAIN (parms
);
12963 parms
= tsubst (parms
, args
, complain
, t
);
12964 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
12965 DECL_CONTEXT (parm
) = r
;
12968 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
12969 DECL_CHAIN (tparm
) = parms
;
12972 DECL_ARGUMENTS (r
) = parms
;
12973 DECL_RESULT (r
) = NULL_TREE
;
12975 TREE_STATIC (r
) = 0;
12976 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12977 DECL_EXTERNAL (r
) = 1;
12978 /* If this is an instantiation of a function with internal
12979 linkage, we already know what object file linkage will be
12980 assigned to the instantiation. */
12981 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12982 DECL_DEFER_OUTPUT (r
) = 0;
12983 DECL_CHAIN (r
) = NULL_TREE
;
12984 DECL_PENDING_INLINE_INFO (r
) = 0;
12985 DECL_PENDING_INLINE_P (r
) = 0;
12986 DECL_SAVED_TREE (r
) = NULL_TREE
;
12987 DECL_STRUCT_FUNCTION (r
) = NULL
;
12989 /* We'll re-clone as appropriate in instantiate_template. */
12990 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12992 /* If we aren't complaining now, return on error before we register
12993 the specialization so that we'll complain eventually. */
12994 if ((complain
& tf_error
) == 0
12995 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
12996 && !grok_op_properties (r
, /*complain=*/false))
12997 return error_mark_node
;
12999 /* When instantiating a constrained member, substitute
13000 into the constraints to create a new constraint. */
13001 if (tree ci
= get_constraints (t
))
13004 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13005 set_constraints (r
, ci
);
13008 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13009 this in the special friend case mentioned above where
13010 GEN_TMPL is NULL. */
13011 if (gen_tmpl
&& !closure
)
13013 DECL_TEMPLATE_INFO (r
)
13014 = build_template_info (gen_tmpl
, argvec
);
13015 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13018 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13020 /* We instantiated this while substituting into
13021 the type earlier (template/friend54.C). */
13024 /* We're not supposed to instantiate default arguments
13025 until they are called, for a template. But, for a
13028 template <class T> void f ()
13029 { extern void g(int i = T()); }
13031 we should do the substitution when the template is
13032 instantiated. We handle the member function case in
13033 instantiate_class_template since the default arguments
13034 might refer to other members of the class. */
13036 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13037 && !uses_template_parms (argvec
))
13038 tsubst_default_arguments (r
, complain
);
13041 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13043 /* Copy the list of befriending classes. */
13044 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13046 friends
= &TREE_CHAIN (*friends
))
13048 *friends
= copy_node (*friends
);
13049 TREE_VALUE (*friends
)
13050 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13053 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13055 maybe_retrofit_in_chrg (r
);
13056 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13057 return error_mark_node
;
13058 /* If this is an instantiation of a member template, clone it.
13059 If it isn't, that'll be handled by
13060 clone_constructors_and_destructors. */
13061 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13062 clone_function_decl (r
, /*update_methods=*/false);
13064 else if ((complain
& tf_error
) != 0
13065 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13066 && !grok_op_properties (r
, /*complain=*/true))
13067 return error_mark_node
;
13069 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13070 SET_DECL_FRIEND_CONTEXT (r
,
13071 tsubst (DECL_FRIEND_CONTEXT (t
),
13072 args
, complain
, in_decl
));
13074 /* Possibly limit visibility based on template args. */
13075 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13076 if (DECL_VISIBILITY_SPECIFIED (t
))
13078 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13079 DECL_ATTRIBUTES (r
)
13080 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13082 determine_visibility (r
);
13083 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13084 && !processing_template_decl
)
13085 defaulted_late_check (r
);
13087 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13088 args
, complain
, in_decl
);
13092 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13095 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13096 tree lambda_fntype
)
13098 /* We can get here when processing a member function template,
13099 member class template, or template template parameter. */
13100 tree decl
= DECL_TEMPLATE_RESULT (t
);
13106 hashval_t hash
= 0;
13108 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13110 /* Template template parameter is treated here. */
13111 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13112 if (new_type
== error_mark_node
)
13113 r
= error_mark_node
;
13114 /* If we get a real template back, return it. This can happen in
13115 the context of most_specialized_partial_spec. */
13116 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13119 /* The new TEMPLATE_DECL was built in
13120 reduce_template_parm_level. */
13121 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13125 if (!lambda_fntype
)
13127 /* We might already have an instance of this template.
13128 The ARGS are for the surrounding class type, so the
13129 full args contain the tsubst'd args for the context,
13130 plus the innermost args from the template decl. */
13131 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13132 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13133 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13134 /* Because this is a template, the arguments will still be
13135 dependent, even after substitution. If
13136 PROCESSING_TEMPLATE_DECL is not set, the dependency
13137 predicates will short-circuit. */
13138 ++processing_template_decl
;
13139 full_args
= tsubst_template_args (tmpl_args
, args
,
13140 complain
, in_decl
);
13141 --processing_template_decl
;
13142 if (full_args
== error_mark_node
)
13143 return error_mark_node
;
13145 /* If this is a default template template argument,
13146 tsubst might not have changed anything. */
13147 if (full_args
== tmpl_args
)
13150 hash
= hash_tmpl_and_args (t
, full_args
);
13151 spec
= retrieve_specialization (t
, full_args
, hash
);
13152 if (spec
!= NULL_TREE
)
13156 /* Make a new template decl. It will be similar to the
13157 original, but will record the current template arguments.
13158 We also create a new function declaration, which is just
13159 like the old one, but points to this new template, rather
13160 than the old one. */
13162 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13163 DECL_CHAIN (r
) = NULL_TREE
;
13165 // Build new template info linking to the original template decl.
13166 if (!lambda_fntype
)
13168 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13169 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13172 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13174 /* The template parameters for this new template are all the
13175 template parameters for the old template, except the
13176 outermost level of parameters. */
13177 DECL_TEMPLATE_PARMS (r
)
13178 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13181 if (TREE_CODE (decl
) == TYPE_DECL
13182 && !TYPE_DECL_ALIAS_P (decl
))
13185 ++processing_template_decl
;
13186 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13187 --processing_template_decl
;
13188 if (new_type
== error_mark_node
)
13189 return error_mark_node
;
13191 TREE_TYPE (r
) = new_type
;
13192 /* For a partial specialization, we need to keep pointing to
13193 the primary template. */
13194 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13195 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13196 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13197 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13198 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13203 ++processing_template_decl
;
13204 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13205 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13207 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13208 --processing_template_decl
;
13209 if (new_decl
== error_mark_node
)
13210 return error_mark_node
;
13212 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13213 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13214 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13217 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13218 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13222 DECL_TI_TEMPLATE (new_decl
) = r
;
13223 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13227 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13228 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13230 if (PRIMARY_TEMPLATE_P (t
))
13231 DECL_PRIMARY_TEMPLATE (r
) = r
;
13233 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13235 /* Record this non-type partial instantiation. */
13236 register_specialization (r
, t
,
13237 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13243 /* True if FN is the op() for a lambda in an uninstantiated template. */
13246 lambda_fn_in_template_p (tree fn
)
13248 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13250 tree closure
= DECL_CONTEXT (fn
);
13251 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13254 /* We're instantiating a variable from template function TCTX. Return the
13255 corresponding current enclosing scope. This gets complicated because lambda
13256 functions in templates are regenerated rather than instantiated, but generic
13257 lambda functions are subsequently instantiated. */
13260 enclosing_instantiation_of (tree otctx
)
13263 tree fn
= current_function_decl
;
13264 int lambda_count
= 0;
13266 for (; tctx
&& lambda_fn_in_template_p (tctx
);
13267 tctx
= decl_function_context (tctx
))
13269 for (; fn
; fn
= decl_function_context (fn
))
13272 int flambda_count
= 0;
13273 for (; flambda_count
< lambda_count
&& fn
&& LAMBDA_FUNCTION_P (fn
);
13274 fn
= decl_function_context (fn
))
13276 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13277 ? most_general_template (fn
) != most_general_template (tctx
)
13280 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13281 || DECL_CONV_FN_P (ofn
));
13284 gcc_unreachable ();
13287 /* Substitute the ARGS into the T, which is a _DECL. Return the
13288 result of the substitution. Issue error and warning messages under
13289 control of COMPLAIN. */
13292 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13294 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13295 location_t saved_loc
;
13296 tree r
= NULL_TREE
;
13298 hashval_t hash
= 0;
13300 /* Set the filename and linenumber to improve error-reporting. */
13301 saved_loc
= input_location
;
13302 input_location
= DECL_SOURCE_LOCATION (t
);
13304 switch (TREE_CODE (t
))
13306 case TEMPLATE_DECL
:
13307 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13310 case FUNCTION_DECL
:
13311 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13316 tree type
= NULL_TREE
;
13318 tree expanded_types
= NULL_TREE
;
13319 tree prev_r
= NULL_TREE
;
13320 tree first_r
= NULL_TREE
;
13322 if (DECL_PACK_P (t
))
13324 /* If there is a local specialization that isn't a
13325 parameter pack, it means that we're doing a "simple"
13326 substitution from inside tsubst_pack_expansion. Just
13327 return the local specialization (which will be a single
13329 tree spec
= retrieve_local_specialization (t
);
13331 && TREE_CODE (spec
) == PARM_DECL
13332 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13335 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13336 the parameters in this function parameter pack. */
13337 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13338 complain
, in_decl
);
13339 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13341 len
= TREE_VEC_LENGTH (expanded_types
);
13343 /* Zero-length parameter packs are boring. Just substitute
13346 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13351 /* All we did was update the type. Make a note of that. */
13352 type
= expanded_types
;
13353 expanded_types
= NULL_TREE
;
13357 /* Loop through all of the parameters we'll build. When T is
13358 a function parameter pack, LEN is the number of expanded
13359 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13361 for (i
= 0; i
< len
; ++i
)
13365 if (DECL_TEMPLATE_PARM_P (t
))
13366 SET_DECL_TEMPLATE_PARM_P (r
);
13368 if (expanded_types
)
13369 /* We're on the Ith parameter of the function parameter
13372 /* Get the Ith type. */
13373 type
= TREE_VEC_ELT (expanded_types
, i
);
13375 /* Rename the parameter to include the index. */
13377 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13380 /* We're dealing with a normal parameter. */
13381 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13383 type
= type_decays_to (type
);
13384 TREE_TYPE (r
) = type
;
13385 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13387 if (DECL_INITIAL (r
))
13389 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13390 DECL_INITIAL (r
) = TREE_TYPE (r
);
13392 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13393 complain
, in_decl
);
13396 DECL_CONTEXT (r
) = NULL_TREE
;
13398 if (!DECL_TEMPLATE_PARM_P (r
))
13399 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13401 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13402 args
, complain
, in_decl
);
13404 /* Keep track of the first new parameter we
13405 generate. That's what will be returned to the
13410 /* Build a proper chain of parameters when substituting
13411 into a function parameter pack. */
13413 DECL_CHAIN (prev_r
) = r
;
13416 /* If cp_unevaluated_operand is set, we're just looking for a
13417 single dummy parameter, so don't keep going. */
13418 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13419 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13420 complain
, DECL_CHAIN (t
));
13422 /* FIRST_R contains the start of the chain we've built. */
13429 tree type
= NULL_TREE
;
13430 tree vec
= NULL_TREE
;
13431 tree expanded_types
= NULL_TREE
;
13434 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13436 /* This field is a lambda capture pack. Return a TREE_VEC of
13437 the expanded fields to instantiate_class_template_1. */
13438 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13439 complain
, in_decl
);
13440 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13442 len
= TREE_VEC_LENGTH (expanded_types
);
13443 vec
= make_tree_vec (len
);
13447 /* All we did was update the type. Make a note of that. */
13448 type
= expanded_types
;
13449 expanded_types
= NULL_TREE
;
13453 for (int i
= 0; i
< len
; ++i
)
13456 if (expanded_types
)
13458 type
= TREE_VEC_ELT (expanded_types
, i
);
13460 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13463 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13465 if (type
== error_mark_node
)
13466 RETURN (error_mark_node
);
13467 TREE_TYPE (r
) = type
;
13468 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13470 if (DECL_C_BIT_FIELD (r
))
13471 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13473 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13474 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13476 /*integral_constant_expression_p=*/true);
13477 if (DECL_INITIAL (t
))
13479 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13480 NSDMI in perform_member_init. Still set DECL_INITIAL
13481 so that we know there is one. */
13482 DECL_INITIAL (r
) = void_node
;
13483 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13484 retrofit_lang_decl (r
);
13485 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13487 /* We don't have to set DECL_CONTEXT here; it is set by
13488 finish_member_declaration. */
13489 DECL_CHAIN (r
) = NULL_TREE
;
13491 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13492 args
, complain
, in_decl
);
13495 TREE_VEC_ELT (vec
, i
) = r
;
13504 /* We reach here only for member using decls. We also need to check
13505 uses_template_parms because DECL_DEPENDENT_P is not set for a
13506 using-declaration that designates a member of the current
13507 instantiation (c++/53549). */
13508 if (DECL_DEPENDENT_P (t
)
13509 || uses_template_parms (USING_DECL_SCOPE (t
)))
13511 tree scope
= USING_DECL_SCOPE (t
);
13512 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13513 if (PACK_EXPANSION_P (scope
))
13515 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13516 int len
= TREE_VEC_LENGTH (vec
);
13517 r
= make_tree_vec (len
);
13518 for (int i
= 0; i
< len
; ++i
)
13520 tree escope
= TREE_VEC_ELT (vec
, i
);
13521 tree elt
= do_class_using_decl (escope
, name
);
13524 r
= error_mark_node
;
13529 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13530 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13532 TREE_VEC_ELT (r
, i
) = elt
;
13537 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13538 complain
, in_decl
);
13539 r
= do_class_using_decl (inst_scope
, name
);
13541 r
= error_mark_node
;
13544 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13545 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13552 DECL_CHAIN (r
) = NULL_TREE
;
13559 tree argvec
= NULL_TREE
;
13560 tree gen_tmpl
= NULL_TREE
;
13562 tree tmpl
= NULL_TREE
;
13564 tree type
= NULL_TREE
;
13567 if (TREE_TYPE (t
) == error_mark_node
)
13568 RETURN (error_mark_node
);
13570 if (TREE_CODE (t
) == TYPE_DECL
13571 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13573 /* If this is the canonical decl, we don't have to
13574 mess with instantiations, and often we can't (for
13575 typename, template type parms and such). Note that
13576 TYPE_NAME is not correct for the above test if
13577 we've copied the type for a typedef. */
13578 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13579 if (type
== error_mark_node
)
13580 RETURN (error_mark_node
);
13581 r
= TYPE_NAME (type
);
13585 /* Check to see if we already have the specialization we
13588 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13590 /* T is a static data member or namespace-scope entity.
13591 We have to substitute into namespace-scope variables
13592 (not just variable templates) because of cases like:
13594 template <class T> void f() { extern T t; }
13596 where the entity referenced is not known until
13597 instantiation time. */
13599 ctx
= DECL_CONTEXT (t
);
13600 if (DECL_CLASS_SCOPE_P (t
))
13602 ctx
= tsubst_aggr_type (ctx
, args
,
13604 in_decl
, /*entering_scope=*/1);
13605 /* If CTX is unchanged, then T is in fact the
13606 specialization we want. That situation occurs when
13607 referencing a static data member within in its own
13608 class. We can use pointer equality, rather than
13609 same_type_p, because DECL_CONTEXT is always
13611 if (ctx
== DECL_CONTEXT (t
)
13612 /* ... unless T is a member template; in which
13613 case our caller can be willing to create a
13614 specialization of that template represented
13616 && !(DECL_TI_TEMPLATE (t
)
13617 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13623 tmpl
= DECL_TI_TEMPLATE (t
);
13624 gen_tmpl
= most_general_template (tmpl
);
13625 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13626 if (argvec
!= error_mark_node
)
13627 argvec
= (coerce_innermost_template_parms
13628 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13629 argvec
, t
, complain
,
13630 /*all*/true, /*defarg*/true));
13631 if (argvec
== error_mark_node
)
13632 RETURN (error_mark_node
);
13633 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13634 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13639 /* A local variable. */
13641 /* Subsequent calls to pushdecl will fill this in. */
13643 /* Unless this is a reference to a static variable from an
13644 enclosing function, in which case we need to fill it in now. */
13645 if (TREE_STATIC (t
))
13647 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13648 if (fn
!= current_function_decl
)
13651 spec
= retrieve_local_specialization (t
);
13653 /* If we already have the specialization we need, there is
13654 nothing more to do. */
13661 /* Create a new node for the specialization we need. */
13662 if (type
== NULL_TREE
)
13664 if (is_typedef_decl (t
))
13665 type
= DECL_ORIGINAL_TYPE (t
);
13667 type
= TREE_TYPE (t
);
13669 && VAR_HAD_UNKNOWN_BOUND (t
)
13670 && type
!= error_mark_node
)
13671 type
= strip_array_domain (type
);
13672 tree sub_args
= args
;
13673 if (tree auto_node
= type_uses_auto (type
))
13675 /* Mask off any template args past the variable's context so we
13676 don't replace the auto with an unrelated argument. */
13677 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13678 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13680 /* This should never happen with the new lambda instantiation
13681 model, but keep the handling just in case. */
13682 gcc_assert (!CHECKING_P
),
13683 sub_args
= strip_innermost_template_args (args
, extra
);
13685 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13686 /* Substituting the type might have recursively instantiated this
13687 same alias (c++/86171). */
13688 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13689 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13698 DECL_INITIALIZED_P (r
) = 0;
13699 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13700 if (type
== error_mark_node
)
13701 RETURN (error_mark_node
);
13702 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13704 /* It may seem that this case cannot occur, since:
13709 declares a function, not a variable. However:
13712 template <typename T> void g() { T t; }
13713 template void g<f>();
13715 is an attempt to declare a variable with function
13717 error ("variable %qD has function type",
13718 /* R is not yet sufficiently initialized, so we
13719 just use its name. */
13721 RETURN (error_mark_node
);
13723 type
= complete_type (type
);
13724 /* Wait until cp_finish_decl to set this again, to handle
13725 circular dependency (template/instantiate6.C). */
13726 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13727 type
= check_var_type (DECL_NAME (r
), type
);
13729 if (DECL_HAS_VALUE_EXPR_P (t
))
13731 tree ve
= DECL_VALUE_EXPR (t
);
13732 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13733 /*constant_expression_p=*/false);
13734 if (REFERENCE_REF_P (ve
))
13736 gcc_assert (TYPE_REF_P (type
));
13737 ve
= TREE_OPERAND (ve
, 0);
13739 SET_DECL_VALUE_EXPR (r
, ve
);
13741 if (CP_DECL_THREAD_LOCAL_P (r
)
13742 && !processing_template_decl
)
13743 set_decl_tls_model (r
, decl_default_tls_model (r
));
13745 else if (DECL_SELF_REFERENCE_P (t
))
13746 SET_DECL_SELF_REFERENCE_P (r
);
13747 TREE_TYPE (r
) = type
;
13748 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13749 DECL_CONTEXT (r
) = ctx
;
13750 /* Clear out the mangled name and RTL for the instantiation. */
13751 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13752 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13753 SET_DECL_RTL (r
, NULL
);
13754 /* The initializer must not be expanded until it is required;
13755 see [temp.inst]. */
13756 DECL_INITIAL (r
) = NULL_TREE
;
13757 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13760 if (DECL_LANG_SPECIFIC (r
))
13761 SET_DECL_DEPENDENT_INIT_P (r
, false);
13763 SET_DECL_MODE (r
, VOIDmode
);
13765 /* Possibly limit visibility based on template args. */
13766 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13767 if (DECL_VISIBILITY_SPECIFIED (t
))
13769 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13770 DECL_ATTRIBUTES (r
)
13771 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13773 determine_visibility (r
);
13778 /* A static data member declaration is always marked
13779 external when it is declared in-class, even if an
13780 initializer is present. We mimic the non-template
13781 processing here. */
13782 DECL_EXTERNAL (r
) = 1;
13783 if (DECL_NAMESPACE_SCOPE_P (t
))
13784 DECL_NOT_REALLY_EXTERN (r
) = 1;
13786 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13787 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13788 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13792 if (DECL_LANG_SPECIFIC (r
))
13793 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13794 if (!cp_unevaluated_operand
)
13795 register_local_specialization (r
, t
);
13798 DECL_CHAIN (r
) = NULL_TREE
;
13800 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13802 args
, complain
, in_decl
);
13804 /* Preserve a typedef that names a type. */
13805 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13807 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13808 set_underlying_type (r
);
13809 if (TYPE_DECL_ALIAS_P (r
))
13810 /* An alias template specialization can be dependent
13811 even if its underlying type is not. */
13812 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13815 layout_decl (r
, 0);
13820 gcc_unreachable ();
13825 /* Restore the file and line information. */
13826 input_location
= saved_loc
;
13831 /* Substitute into the ARG_TYPES of a function type.
13832 If END is a TREE_CHAIN, leave it and any following types
13836 tsubst_arg_types (tree arg_types
,
13839 tsubst_flags_t complain
,
13842 tree remaining_arg_types
;
13843 tree type
= NULL_TREE
;
13845 tree expanded_args
= NULL_TREE
;
13848 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13851 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13852 args
, end
, complain
, in_decl
);
13853 if (remaining_arg_types
== error_mark_node
)
13854 return error_mark_node
;
13856 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13858 /* For a pack expansion, perform substitution on the
13859 entire expression. Later on, we'll handle the arguments
13861 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13862 args
, complain
, in_decl
);
13864 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13865 /* So that we'll spin through the parameters, one by one. */
13866 i
= TREE_VEC_LENGTH (expanded_args
);
13869 /* We only partially substituted into the parameter
13870 pack. Our type is TYPE_PACK_EXPANSION. */
13871 type
= expanded_args
;
13872 expanded_args
= NULL_TREE
;
13880 type
= TREE_VEC_ELT (expanded_args
, i
);
13882 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13884 if (type
== error_mark_node
)
13885 return error_mark_node
;
13886 if (VOID_TYPE_P (type
))
13888 if (complain
& tf_error
)
13890 error ("invalid parameter type %qT", type
);
13892 error ("in declaration %q+D", in_decl
);
13894 return error_mark_node
;
13897 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13898 return error_mark_node
;
13900 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13901 top-level qualifiers as required. */
13902 type
= cv_unqualified (type_decays_to (type
));
13904 /* We do not substitute into default arguments here. The standard
13905 mandates that they be instantiated only when needed, which is
13906 done in build_over_call. */
13907 default_arg
= TREE_PURPOSE (arg_types
);
13909 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13910 since the new op() won't have any associated template arguments for us
13911 to refer to later. */
13912 if (lambda_fn_in_template_p (in_decl
))
13913 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
13914 false/*fn*/, false/*constexpr*/);
13916 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13918 /* We've instantiated a template before its default arguments
13919 have been parsed. This can happen for a nested template
13920 class, and is not an error unless we require the default
13921 argument in a call of this function. */
13922 remaining_arg_types
=
13923 tree_cons (default_arg
, type
, remaining_arg_types
);
13924 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13927 remaining_arg_types
=
13928 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13931 return remaining_arg_types
;
13934 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13935 *not* handle the exception-specification for FNTYPE, because the
13936 initial substitution of explicitly provided template parameters
13937 during argument deduction forbids substitution into the
13938 exception-specification:
13942 All references in the function type of the function template to the
13943 corresponding template parameters are replaced by the specified tem-
13944 plate argument values. If a substitution in a template parameter or
13945 in the function type of the function template results in an invalid
13946 type, type deduction fails. [Note: The equivalent substitution in
13947 exception specifications is done only when the function is instanti-
13948 ated, at which point a program is ill-formed if the substitution
13949 results in an invalid type.] */
13952 tsubst_function_type (tree t
,
13954 tsubst_flags_t complain
,
13958 tree arg_types
= NULL_TREE
;
13961 /* The TYPE_CONTEXT is not used for function/method types. */
13962 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13964 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13966 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13968 if (late_return_type_p
)
13970 /* Substitute the argument types. */
13971 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13972 complain
, in_decl
);
13973 if (arg_types
== error_mark_node
)
13974 return error_mark_node
;
13976 tree save_ccp
= current_class_ptr
;
13977 tree save_ccr
= current_class_ref
;
13978 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13979 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13980 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13983 /* DR 1207: 'this' is in scope in the trailing return type. */
13984 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13987 /* Substitute the return type. */
13988 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13992 current_class_ptr
= save_ccp
;
13993 current_class_ref
= save_ccr
;
13997 /* Substitute the return type. */
13998 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14000 if (return_type
== error_mark_node
)
14001 return error_mark_node
;
14002 /* DR 486 clarifies that creation of a function type with an
14003 invalid return type is a deduction failure. */
14004 if (TREE_CODE (return_type
) == ARRAY_TYPE
14005 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14007 if (complain
& tf_error
)
14009 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14010 error ("function returning an array");
14012 error ("function returning a function");
14014 return error_mark_node
;
14017 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14018 return error_mark_node
;
14020 if (!late_return_type_p
)
14022 /* Substitute the argument types. */
14023 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14024 complain
, in_decl
);
14025 if (arg_types
== error_mark_node
)
14026 return error_mark_node
;
14029 /* Construct a new type node and return it. */
14030 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14032 fntype
= build_function_type (return_type
, arg_types
);
14033 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14037 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14038 /* Don't pick up extra function qualifiers from the basetype. */
14039 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14040 if (! MAYBE_CLASS_TYPE_P (r
))
14044 Type deduction may fail for any of the following
14047 -- Attempting to create "pointer to member of T" when T
14048 is not a class type. */
14049 if (complain
& tf_error
)
14050 error ("creating pointer to member function of non-class type %qT",
14052 return error_mark_node
;
14055 fntype
= build_method_type_directly (r
, return_type
,
14056 TREE_CHAIN (arg_types
));
14058 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14060 /* See comment above. */
14061 tree raises
= NULL_TREE
;
14062 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14063 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14068 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14069 ARGS into that specification, and return the substituted
14070 specification. If there is no specification, return NULL_TREE. */
14073 tsubst_exception_specification (tree fntype
,
14075 tsubst_flags_t complain
,
14082 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14083 new_specs
= NULL_TREE
;
14084 if (specs
&& TREE_PURPOSE (specs
))
14086 /* A noexcept-specifier. */
14087 tree expr
= TREE_PURPOSE (specs
);
14088 if (TREE_CODE (expr
) == INTEGER_CST
)
14092 /* Defer instantiation of noexcept-specifiers to avoid
14093 excessive instantiations (c++/49107). */
14094 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14095 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14097 /* We already partially instantiated this member template,
14098 so combine the new args with the old. */
14099 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14100 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14101 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14102 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14106 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14107 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14111 new_specs
= tsubst_copy_and_build
14112 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14113 /*integral_constant_expression_p=*/true);
14114 new_specs
= build_noexcept_spec (new_specs
, complain
);
14118 if (! TREE_VALUE (specs
))
14125 tree expanded_specs
= NULL_TREE
;
14127 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14129 /* Expand the pack expansion type. */
14130 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14134 if (expanded_specs
== error_mark_node
)
14135 return error_mark_node
;
14136 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14137 len
= TREE_VEC_LENGTH (expanded_specs
);
14140 /* We're substituting into a member template, so
14141 we got a TYPE_PACK_EXPANSION back. Add that
14142 expansion and move on. */
14143 gcc_assert (TREE_CODE (expanded_specs
)
14144 == TYPE_PACK_EXPANSION
);
14145 new_specs
= add_exception_specifier (new_specs
,
14148 specs
= TREE_CHAIN (specs
);
14153 for (i
= 0; i
< len
; ++i
)
14155 if (expanded_specs
)
14156 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14158 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14159 if (spec
== error_mark_node
)
14161 new_specs
= add_exception_specifier (new_specs
, spec
,
14165 specs
= TREE_CHAIN (specs
);
14171 /* Take the tree structure T and replace template parameters used
14172 therein with the argument vector ARGS. IN_DECL is an associated
14173 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14174 Issue error and warning messages under control of COMPLAIN. Note
14175 that we must be relatively non-tolerant of extensions here, in
14176 order to preserve conformance; if we allow substitutions that
14177 should not be allowed, we may allow argument deductions that should
14178 not succeed, and therefore report ambiguous overload situations
14179 where there are none. In theory, we could allow the substitution,
14180 but indicate that it should have failed, and allow our caller to
14181 make sure that the right thing happens, but we don't try to do this
14184 This function is used for dealing with types, decls and the like;
14185 for expressions, use tsubst_expr or tsubst_copy. */
14188 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14190 enum tree_code code
;
14191 tree type
, r
= NULL_TREE
;
14193 if (t
== NULL_TREE
|| t
== error_mark_node
14194 || t
== integer_type_node
14195 || t
== void_type_node
14196 || t
== char_type_node
14197 || t
== unknown_type_node
14198 || TREE_CODE (t
) == NAMESPACE_DECL
14199 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14203 return tsubst_decl (t
, args
, complain
);
14205 if (args
== NULL_TREE
)
14208 code
= TREE_CODE (t
);
14210 if (code
== IDENTIFIER_NODE
)
14211 type
= IDENTIFIER_TYPE_VALUE (t
);
14213 type
= TREE_TYPE (t
);
14215 gcc_assert (type
!= unknown_type_node
);
14217 /* Reuse typedefs. We need to do this to handle dependent attributes,
14218 such as attribute aligned. */
14220 && typedef_variant_p (t
))
14222 tree decl
= TYPE_NAME (t
);
14224 if (alias_template_specialization_p (t
))
14226 /* DECL represents an alias template and we want to
14228 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14229 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14230 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14232 else if (DECL_CLASS_SCOPE_P (decl
)
14233 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14234 && uses_template_parms (DECL_CONTEXT (decl
)))
14236 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14237 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14238 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14240 else if (DECL_FUNCTION_SCOPE_P (decl
)
14241 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14242 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14243 r
= retrieve_local_specialization (decl
);
14245 /* The typedef is from a non-template context. */
14251 r
= cp_build_qualified_type_real
14252 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14253 complain
| tf_ignore_bad_quals
);
14258 /* We don't have an instantiation yet, so drop the typedef. */
14259 int quals
= cp_type_quals (t
);
14260 t
= DECL_ORIGINAL_TYPE (decl
);
14261 t
= cp_build_qualified_type_real (t
, quals
,
14262 complain
| tf_ignore_bad_quals
);
14266 bool fndecl_type
= (complain
& tf_fndecl_type
);
14267 complain
&= ~tf_fndecl_type
;
14270 && code
!= TYPENAME_TYPE
14271 && code
!= TEMPLATE_TYPE_PARM
14272 && code
!= TEMPLATE_PARM_INDEX
14273 && code
!= IDENTIFIER_NODE
14274 && code
!= FUNCTION_TYPE
14275 && code
!= METHOD_TYPE
)
14276 type
= tsubst (type
, args
, complain
, in_decl
);
14277 if (type
== error_mark_node
)
14278 return error_mark_node
;
14284 case ENUMERAL_TYPE
:
14285 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14286 /*entering_scope=*/0);
14289 case IDENTIFIER_NODE
:
14300 if (t
== integer_type_node
)
14303 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14304 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14308 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14310 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14311 /*integral_constant_expression_p=*/false);
14313 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14315 if (TREE_CODE (max
) == NOP_EXPR
14316 && TREE_SIDE_EFFECTS (omax
)
14317 && !TREE_TYPE (max
))
14318 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14320 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14321 with TREE_SIDE_EFFECTS that indicates this is not an integral
14322 constant expression. */
14323 if (processing_template_decl
14324 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14326 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14327 TREE_SIDE_EFFECTS (max
) = 1;
14330 return compute_array_index_type (NULL_TREE
, max
, complain
);
14333 case TEMPLATE_TYPE_PARM
:
14334 case TEMPLATE_TEMPLATE_PARM
:
14335 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14336 case TEMPLATE_PARM_INDEX
:
14341 tree arg
= NULL_TREE
;
14343 /* Early in template argument deduction substitution, we don't
14344 want to reduce the level of 'auto', or it will be confused
14345 with a normal template parm in subsequent deduction. */
14346 if (is_auto (t
) && (complain
& tf_partial
))
14351 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14352 template_parm_level_and_index (t
, &level
, &idx
);
14354 levels
= TMPL_ARGS_DEPTH (args
);
14355 if (level
<= levels
14356 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14358 arg
= TMPL_ARG (args
, level
, idx
);
14360 /* See through ARGUMENT_PACK_SELECT arguments. */
14361 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14362 arg
= argument_pack_select_arg (arg
);
14365 if (arg
== error_mark_node
)
14366 return error_mark_node
;
14367 else if (arg
!= NULL_TREE
)
14369 if (ARGUMENT_PACK_P (arg
))
14370 /* If ARG is an argument pack, we don't actually want to
14371 perform a substitution here, because substitutions
14372 for argument packs are only done
14373 element-by-element. We can get to this point when
14374 substituting the type of a non-type template
14375 parameter pack, when that type actually contains
14376 template parameter packs from an outer template, e.g.,
14378 template<typename... Types> struct A {
14379 template<Types... Values> struct B { };
14383 if (code
== TEMPLATE_TYPE_PARM
)
14386 gcc_assert (TYPE_P (arg
));
14388 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14390 return cp_build_qualified_type_real
14391 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14393 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14395 /* We are processing a type constructed from a
14396 template template parameter. */
14397 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14398 args
, complain
, in_decl
);
14399 if (argvec
== error_mark_node
)
14400 return error_mark_node
;
14402 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14403 || TREE_CODE (arg
) == TEMPLATE_DECL
14404 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14406 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14407 /* Consider this code:
14409 template <template <class> class Template>
14411 template <class Arg> using Bind = Template<Arg>;
14414 template <template <class> class Template, class Arg>
14415 using Instantiate = Template<Arg>; //#0
14417 template <template <class> class Template,
14420 Instantiate<Internal<Template>::template Bind,
14423 When #1 is parsed, the
14424 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14425 parameter `Template' in #0 matches the
14426 UNBOUND_CLASS_TEMPLATE representing the argument
14427 `Internal<Template>::template Bind'; We then want
14428 to assemble the type `Bind<Argument>' that can't
14429 be fully created right now, because
14430 `Internal<Template>' not being complete, the Bind
14431 template cannot be looked up in that context. So
14432 we need to "store" `Bind<Argument>' for later
14433 when the context of Bind becomes complete. Let's
14434 store that in a TYPENAME_TYPE. */
14435 return make_typename_type (TYPE_CONTEXT (arg
),
14436 build_nt (TEMPLATE_ID_EXPR
,
14437 TYPE_IDENTIFIER (arg
),
14442 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14443 are resolving nested-types in the signature of a
14444 member function templates. Otherwise ARG is a
14445 TEMPLATE_DECL and is the real template to be
14447 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14448 arg
= TYPE_NAME (arg
);
14450 r
= lookup_template_class (arg
,
14452 DECL_CONTEXT (arg
),
14453 /*entering_scope=*/0,
14455 return cp_build_qualified_type_real
14456 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14458 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14461 /* TEMPLATE_PARM_INDEX. */
14462 return convert_from_reference (unshare_expr (arg
));
14466 /* This can happen during the attempted tsubst'ing in
14467 unify. This means that we don't yet have any information
14468 about the template parameter in question. */
14471 /* If we get here, we must have been looking at a parm for a
14472 more deeply nested template. Make a new version of this
14473 template parameter, but with a lower level. */
14476 case TEMPLATE_TYPE_PARM
:
14477 case TEMPLATE_TEMPLATE_PARM
:
14478 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14479 if (cp_type_quals (t
))
14481 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14482 r
= cp_build_qualified_type_real
14483 (r
, cp_type_quals (t
),
14484 complain
| (code
== TEMPLATE_TYPE_PARM
14485 ? tf_ignore_bad_quals
: 0));
14487 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14488 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14489 && (r
= (TEMPLATE_PARM_DESCENDANTS
14490 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14491 && (r
= TREE_TYPE (r
))
14492 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14493 /* Break infinite recursion when substituting the constraints
14494 of a constrained placeholder. */;
14495 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14496 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14497 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14498 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14499 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14500 && (TEMPLATE_PARM_LEVEL (r
)
14501 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14502 /* Cache the simple case of lowering a type parameter. */
14507 TEMPLATE_TYPE_PARM_INDEX (r
)
14508 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14509 r
, levels
, args
, complain
);
14510 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14511 TYPE_MAIN_VARIANT (r
) = r
;
14512 TYPE_POINTER_TO (r
) = NULL_TREE
;
14513 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14515 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14517 /* Propagate constraints on placeholders. */
14518 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14519 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14520 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14521 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14523 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14524 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14528 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14529 /* We have reduced the level of the template
14530 template parameter, but not the levels of its
14531 template parameters, so canonical_type_parameter
14532 will not be able to find the canonical template
14533 template parameter for this level. Thus, we
14534 require structural equality checking to compare
14535 TEMPLATE_TEMPLATE_PARMs. */
14536 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14537 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14538 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14540 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14542 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14544 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14545 /* We might need to substitute into the types of non-type
14546 template parameters. */
14547 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14548 complain
, in_decl
);
14549 if (tmpl
== error_mark_node
)
14550 return error_mark_node
;
14551 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14552 complain
, in_decl
);
14553 if (argvec
== error_mark_node
)
14554 return error_mark_node
;
14556 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14557 = build_template_info (tmpl
, argvec
);
14562 case TEMPLATE_PARM_INDEX
:
14563 /* OK, now substitute the type of the non-type parameter. We
14564 couldn't do it earlier because it might be an auto parameter,
14565 and we wouldn't need to if we had an argument. */
14566 type
= tsubst (type
, args
, complain
, in_decl
);
14567 if (type
== error_mark_node
)
14568 return error_mark_node
;
14569 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14573 gcc_unreachable ();
14581 tree purpose
, value
, chain
;
14583 if (t
== void_list_node
)
14586 purpose
= TREE_PURPOSE (t
);
14589 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14590 if (purpose
== error_mark_node
)
14591 return error_mark_node
;
14593 value
= TREE_VALUE (t
);
14596 value
= tsubst (value
, args
, complain
, in_decl
);
14597 if (value
== error_mark_node
)
14598 return error_mark_node
;
14600 chain
= TREE_CHAIN (t
);
14601 if (chain
&& chain
!= void_type_node
)
14603 chain
= tsubst (chain
, args
, complain
, in_decl
);
14604 if (chain
== error_mark_node
)
14605 return error_mark_node
;
14607 if (purpose
== TREE_PURPOSE (t
)
14608 && value
== TREE_VALUE (t
)
14609 && chain
== TREE_CHAIN (t
))
14611 return hash_tree_cons (purpose
, value
, chain
);
14615 /* We should never be tsubsting a binfo. */
14616 gcc_unreachable ();
14619 /* A vector of template arguments. */
14620 gcc_assert (!type
);
14621 return tsubst_template_args (t
, args
, complain
, in_decl
);
14624 case REFERENCE_TYPE
:
14626 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14631 Type deduction may fail for any of the following
14634 -- Attempting to create a pointer to reference type.
14635 -- Attempting to create a reference to a reference type or
14636 a reference to void.
14638 Core issue 106 says that creating a reference to a reference
14639 during instantiation is no longer a cause for failure. We
14640 only enforce this check in strict C++98 mode. */
14641 if ((TYPE_REF_P (type
)
14642 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14643 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14645 static location_t last_loc
;
14647 /* We keep track of the last time we issued this error
14648 message to avoid spewing a ton of messages during a
14649 single bad template instantiation. */
14650 if (complain
& tf_error
14651 && last_loc
!= input_location
)
14653 if (VOID_TYPE_P (type
))
14654 error ("forming reference to void");
14655 else if (code
== POINTER_TYPE
)
14656 error ("forming pointer to reference type %qT", type
);
14658 error ("forming reference to reference type %qT", type
);
14659 last_loc
= input_location
;
14662 return error_mark_node
;
14664 else if (TREE_CODE (type
) == FUNCTION_TYPE
14665 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14666 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14668 if (complain
& tf_error
)
14670 if (code
== POINTER_TYPE
)
14671 error ("forming pointer to qualified function type %qT",
14674 error ("forming reference to qualified function type %qT",
14677 return error_mark_node
;
14679 else if (code
== POINTER_TYPE
)
14681 r
= build_pointer_type (type
);
14682 if (TREE_CODE (type
) == METHOD_TYPE
)
14683 r
= build_ptrmemfunc_type (r
);
14685 else if (TYPE_REF_P (type
))
14686 /* In C++0x, during template argument substitution, when there is an
14687 attempt to create a reference to a reference type, reference
14688 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14690 "If a template-argument for a template-parameter T names a type
14691 that is a reference to a type A, an attempt to create the type
14692 'lvalue reference to cv T' creates the type 'lvalue reference to
14693 A,' while an attempt to create the type type rvalue reference to
14694 cv T' creates the type T"
14696 r
= cp_build_reference_type
14698 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14700 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14701 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14703 if (r
!= error_mark_node
)
14704 /* Will this ever be needed for TYPE_..._TO values? */
14711 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14712 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14716 Type deduction may fail for any of the following
14719 -- Attempting to create "pointer to member of T" when T
14720 is not a class type. */
14721 if (complain
& tf_error
)
14722 error ("creating pointer to member of non-class type %qT", r
);
14723 return error_mark_node
;
14725 if (TYPE_REF_P (type
))
14727 if (complain
& tf_error
)
14728 error ("creating pointer to member reference type %qT", type
);
14729 return error_mark_node
;
14731 if (VOID_TYPE_P (type
))
14733 if (complain
& tf_error
)
14734 error ("creating pointer to member of type void");
14735 return error_mark_node
;
14737 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14738 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14740 /* The type of the implicit object parameter gets its
14741 cv-qualifiers from the FUNCTION_TYPE. */
14744 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14745 type_memfn_rqual (type
));
14746 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14747 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14751 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14755 case FUNCTION_TYPE
:
14760 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14761 if (fntype
== error_mark_node
)
14762 return error_mark_node
;
14764 /* Substitute the exception specification. */
14765 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14766 /*defer_ok*/fndecl_type
);
14767 if (specs
== error_mark_node
)
14768 return error_mark_node
;
14770 fntype
= build_exception_variant (fntype
, specs
);
14775 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14776 if (domain
== error_mark_node
)
14777 return error_mark_node
;
14779 /* As an optimization, we avoid regenerating the array type if
14780 it will obviously be the same as T. */
14781 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14784 /* These checks should match the ones in create_array_type_for_decl.
14788 The deduction may fail for any of the following reasons:
14790 -- Attempting to create an array with an element type that
14791 is void, a function type, or a reference type, or [DR337]
14792 an abstract class type. */
14793 if (VOID_TYPE_P (type
)
14794 || TREE_CODE (type
) == FUNCTION_TYPE
14795 || (TREE_CODE (type
) == ARRAY_TYPE
14796 && TYPE_DOMAIN (type
) == NULL_TREE
)
14797 || TYPE_REF_P (type
))
14799 if (complain
& tf_error
)
14800 error ("creating array of %qT", type
);
14801 return error_mark_node
;
14804 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14805 return error_mark_node
;
14807 r
= build_cplus_array_type (type
, domain
);
14809 if (!valid_array_size_p (input_location
, r
, in_decl
,
14810 (complain
& tf_error
)))
14811 return error_mark_node
;
14813 if (TYPE_USER_ALIGN (t
))
14815 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14816 TYPE_USER_ALIGN (r
) = 1;
14822 case TYPENAME_TYPE
:
14824 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14825 in_decl
, /*entering_scope=*/1);
14826 if (ctx
== error_mark_node
)
14827 return error_mark_node
;
14829 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14830 complain
, in_decl
);
14831 if (f
== error_mark_node
)
14832 return error_mark_node
;
14834 if (!MAYBE_CLASS_TYPE_P (ctx
))
14836 if (complain
& tf_error
)
14837 error ("%qT is not a class, struct, or union type", ctx
);
14838 return error_mark_node
;
14840 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14842 /* Normally, make_typename_type does not require that the CTX
14843 have complete type in order to allow things like:
14845 template <class T> struct S { typename S<T>::X Y; };
14847 But, such constructs have already been resolved by this
14848 point, so here CTX really should have complete type, unless
14849 it's a partial instantiation. */
14850 ctx
= complete_type (ctx
);
14851 if (!COMPLETE_TYPE_P (ctx
))
14853 if (complain
& tf_error
)
14854 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14855 return error_mark_node
;
14859 f
= make_typename_type (ctx
, f
, typename_type
,
14860 complain
| tf_keep_type_decl
);
14861 if (f
== error_mark_node
)
14863 if (TREE_CODE (f
) == TYPE_DECL
)
14865 complain
|= tf_ignore_bad_quals
;
14869 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14871 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14873 if (complain
& tf_error
)
14874 error ("%qT resolves to %qT, which is not an enumeration type",
14877 return error_mark_node
;
14879 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14881 if (complain
& tf_error
)
14882 error ("%qT resolves to %qT, which is is not a class type",
14885 return error_mark_node
;
14889 return cp_build_qualified_type_real
14890 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14893 case UNBOUND_CLASS_TEMPLATE
:
14895 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14896 in_decl
, /*entering_scope=*/1);
14897 tree name
= TYPE_IDENTIFIER (t
);
14898 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14900 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14901 return error_mark_node
;
14904 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14905 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14912 ++cp_unevaluated_operand
;
14913 ++c_inhibit_evaluation_warnings
;
14915 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14917 /*integral_constant_expression_p=*/false);
14919 --cp_unevaluated_operand
;
14920 --c_inhibit_evaluation_warnings
;
14922 type
= finish_typeof (type
);
14923 return cp_build_qualified_type_real (type
,
14925 | cp_type_quals (type
),
14929 case DECLTYPE_TYPE
:
14933 ++cp_unevaluated_operand
;
14934 ++c_inhibit_evaluation_warnings
;
14936 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14937 complain
|tf_decltype
, in_decl
,
14938 /*function_p*/false,
14939 /*integral_constant_expression*/false);
14941 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14943 if (type
== NULL_TREE
)
14945 if (complain
& tf_error
)
14946 error ("empty initializer in lambda init-capture");
14947 type
= error_mark_node
;
14949 else if (TREE_CODE (type
) == TREE_LIST
)
14950 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14953 --cp_unevaluated_operand
;
14954 --c_inhibit_evaluation_warnings
;
14956 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14957 type
= lambda_capture_field_type (type
,
14958 DECLTYPE_FOR_INIT_CAPTURE (t
),
14959 DECLTYPE_FOR_REF_CAPTURE (t
));
14960 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14961 type
= lambda_proxy_type (type
);
14964 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14965 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14967 /* In a template ~id could be either a complement expression
14968 or an unqualified-id naming a destructor; if instantiating
14969 it produces an expression, it's not an id-expression or
14972 type
= finish_decltype_type (type
, id
, complain
);
14974 return cp_build_qualified_type_real (type
,
14976 | cp_type_quals (type
),
14977 complain
| tf_ignore_bad_quals
);
14980 case UNDERLYING_TYPE
:
14982 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14983 complain
, in_decl
);
14984 return finish_underlying_type (type
);
14987 case TYPE_ARGUMENT_PACK
:
14988 case NONTYPE_ARGUMENT_PACK
:
14992 if (code
== NONTYPE_ARGUMENT_PACK
)
14993 r
= make_node (code
);
14995 r
= cxx_make_type (code
);
14997 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14998 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14999 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15017 /* We should use one of the expression tsubsts for these codes. */
15018 gcc_unreachable ();
15021 sorry ("use of %qs in template", get_tree_code_name (code
));
15022 return error_mark_node
;
15026 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15027 expression on the left-hand side of the "." or "->" operator. We
15028 only do the lookup if we had a dependent BASELINK. Otherwise we
15029 adjust it onto the instantiated heirarchy. */
15032 tsubst_baselink (tree baselink
, tree object_type
,
15033 tree args
, tsubst_flags_t complain
, tree in_decl
)
15035 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15036 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15037 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15039 tree optype
= BASELINK_OPTYPE (baselink
);
15040 optype
= tsubst (optype
, args
, complain
, in_decl
);
15042 tree template_args
= NULL_TREE
;
15043 bool template_id_p
= false;
15044 tree fns
= BASELINK_FUNCTIONS (baselink
);
15045 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15047 template_id_p
= true;
15048 template_args
= TREE_OPERAND (fns
, 1);
15049 fns
= TREE_OPERAND (fns
, 0);
15051 template_args
= tsubst_template_args (template_args
, args
,
15052 complain
, in_decl
);
15055 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15056 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15057 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15061 tree name
= OVL_NAME (fns
);
15062 if (IDENTIFIER_CONV_OP_P (name
))
15063 name
= make_conv_op_name (optype
);
15065 if (name
== complete_dtor_identifier
)
15066 /* Treat as-if non-dependent below. */
15067 dependent_p
= false;
15069 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15072 if ((complain
& tf_error
)
15073 && constructor_name_p (name
, qualifying_scope
))
15074 error ("cannot call constructor %<%T::%D%> directly",
15075 qualifying_scope
, name
);
15076 return error_mark_node
;
15079 if (BASELINK_P (baselink
))
15080 fns
= BASELINK_FUNCTIONS (baselink
);
15083 /* We're going to overwrite pieces below, make a duplicate. */
15084 baselink
= copy_node (baselink
);
15086 /* If lookup found a single function, mark it as used at this point.
15087 (If lookup found multiple functions the one selected later by
15088 overload resolution will be marked as used at that point.) */
15089 if (!template_id_p
&& !really_overloaded_fn (fns
))
15091 tree fn
= OVL_FIRST (fns
);
15092 bool ok
= mark_used (fn
, complain
);
15093 if (!ok
&& !(complain
& tf_error
))
15094 return error_mark_node
;
15095 if (ok
&& BASELINK_P (baselink
))
15096 /* We might have instantiated an auto function. */
15097 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15100 if (BASELINK_P (baselink
))
15102 /* Add back the template arguments, if present. */
15104 BASELINK_FUNCTIONS (baselink
)
15105 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15107 /* Update the conversion operator type. */
15108 BASELINK_OPTYPE (baselink
) = optype
;
15112 object_type
= current_class_type
;
15114 if (qualified_p
|| !dependent_p
)
15116 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15120 /* We need to call adjust_result_of_qualified_name_lookup in case the
15121 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15122 so that we still get virtual function binding. */
15123 BASELINK_QUALIFIED_P (baselink
) = false;
15129 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15130 true if the qualified-id will be a postfix-expression in-and-of
15131 itself; false if more of the postfix-expression follows the
15132 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15136 tsubst_qualified_id (tree qualified_id
, tree args
,
15137 tsubst_flags_t complain
, tree in_decl
,
15138 bool done
, bool address_p
)
15144 tree template_args
;
15145 location_t loc
= UNKNOWN_LOCATION
;
15147 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15149 /* Figure out what name to look up. */
15150 name
= TREE_OPERAND (qualified_id
, 1);
15151 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15153 is_template
= true;
15154 loc
= EXPR_LOCATION (name
);
15155 template_args
= TREE_OPERAND (name
, 1);
15157 template_args
= tsubst_template_args (template_args
, args
,
15158 complain
, in_decl
);
15159 if (template_args
== error_mark_node
)
15160 return error_mark_node
;
15161 name
= TREE_OPERAND (name
, 0);
15165 is_template
= false;
15166 template_args
= NULL_TREE
;
15169 /* Substitute into the qualifying scope. When there are no ARGS, we
15170 are just trying to simplify a non-dependent expression. In that
15171 case the qualifying scope may be dependent, and, in any case,
15172 substituting will not help. */
15173 scope
= TREE_OPERAND (qualified_id
, 0);
15176 scope
= tsubst (scope
, args
, complain
, in_decl
);
15177 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15182 if (dependent_scope_p (scope
))
15185 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15186 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15187 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15188 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15192 if (!BASELINK_P (name
) && !DECL_P (expr
))
15194 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15196 /* A BIT_NOT_EXPR is used to represent a destructor. */
15197 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15199 error ("qualifying type %qT does not match destructor name ~%qT",
15200 scope
, TREE_OPERAND (expr
, 0));
15201 expr
= error_mark_node
;
15204 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15205 /*is_type_p=*/0, false);
15208 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15209 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15210 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15212 if (complain
& tf_error
)
15214 error ("dependent-name %qE is parsed as a non-type, but "
15215 "instantiation yields a type", qualified_id
);
15216 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15218 return error_mark_node
;
15224 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15226 /* Remember that there was a reference to this entity. */
15227 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15228 return error_mark_node
;
15231 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15233 if (complain
& tf_error
)
15234 qualified_name_lookup_error (scope
,
15235 TREE_OPERAND (qualified_id
, 1),
15236 expr
, input_location
);
15237 return error_mark_node
;
15242 /* We may be repeating a check already done during parsing, but
15243 if it was well-formed and passed then, it will pass again
15244 now, and if it didn't, we wouldn't have got here. The case
15245 we want to catch is when we couldn't tell then, and can now,
15246 namely when templ prior to substitution was an
15248 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15249 return error_mark_node
;
15251 if (variable_template_p (expr
))
15252 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15255 expr
= lookup_template_function (expr
, template_args
);
15258 if (expr
== error_mark_node
&& complain
& tf_error
)
15259 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15260 expr
, input_location
);
15261 else if (TYPE_P (scope
))
15263 expr
= (adjust_result_of_qualified_name_lookup
15264 (expr
, scope
, current_nonlambda_class_type ()));
15265 expr
= (finish_qualified_id_expr
15266 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15267 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15268 /*template_arg_p=*/false, complain
));
15271 /* Expressions do not generally have reference type. */
15272 if (TREE_CODE (expr
) != SCOPE_REF
15273 /* However, if we're about to form a pointer-to-member, we just
15274 want the referenced member referenced. */
15275 && TREE_CODE (expr
) != OFFSET_REF
)
15276 expr
= convert_from_reference (expr
);
15278 if (REF_PARENTHESIZED_P (qualified_id
))
15279 expr
= force_paren_expr (expr
);
15284 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15285 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15289 tsubst_init (tree init
, tree decl
, tree args
,
15290 tsubst_flags_t complain
, tree in_decl
)
15295 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15297 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
15299 /* If we had an initializer but it
15300 instantiated to nothing,
15301 value-initialize the object. This will
15302 only occur when the initializer was a
15303 pack expansion where the parameter packs
15304 used in that expansion were of length
15306 init
= build_value_init (TREE_TYPE (decl
),
15308 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15309 init
= get_target_expr_sfinae (init
, complain
);
15310 if (TREE_CODE (init
) == TARGET_EXPR
)
15311 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15317 /* Like tsubst, but deals with expressions. This function just replaces
15318 template parms; to finish processing the resultant expression, use
15319 tsubst_copy_and_build or tsubst_expr. */
15322 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15324 enum tree_code code
;
15327 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15330 code
= TREE_CODE (t
);
15335 r
= retrieve_local_specialization (t
);
15337 if (r
== NULL_TREE
)
15339 /* We get here for a use of 'this' in an NSDMI. */
15340 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15341 return current_class_ptr
;
15343 /* This can happen for a parameter name used later in a function
15344 declaration (such as in a late-specified return type). Just
15345 make a dummy decl, since it's only used for its type. */
15346 gcc_assert (cp_unevaluated_operand
!= 0);
15347 r
= tsubst_decl (t
, args
, complain
);
15348 /* Give it the template pattern as its context; its true context
15349 hasn't been instantiated yet and this is good enough for
15351 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15354 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15355 r
= argument_pack_select_arg (r
);
15356 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15357 return error_mark_node
;
15365 if (DECL_TEMPLATE_PARM_P (t
))
15366 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15367 /* There is no need to substitute into namespace-scope
15369 if (DECL_NAMESPACE_SCOPE_P (t
))
15371 /* If ARGS is NULL, then T is known to be non-dependent. */
15372 if (args
== NULL_TREE
)
15373 return scalar_constant_value (t
);
15375 /* Unfortunately, we cannot just call lookup_name here.
15378 template <int I> int f() {
15380 struct S { void g() { E e = a; } };
15383 When we instantiate f<7>::S::g(), say, lookup_name is not
15384 clever enough to find f<7>::a. */
15386 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15387 /*entering_scope=*/0);
15389 for (v
= TYPE_VALUES (enum_type
);
15391 v
= TREE_CHAIN (v
))
15392 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15393 return TREE_VALUE (v
);
15395 /* We didn't find the name. That should never happen; if
15396 name-lookup found it during preliminary parsing, we
15397 should find it again here during instantiation. */
15398 gcc_unreachable ();
15403 if (DECL_CONTEXT (t
))
15407 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15408 /*entering_scope=*/1);
15409 if (ctx
!= DECL_CONTEXT (t
))
15411 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15414 if (complain
& tf_error
)
15415 error ("using invalid field %qD", t
);
15416 return error_mark_node
;
15425 case FUNCTION_DECL
:
15426 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15427 r
= tsubst (t
, args
, complain
, in_decl
);
15428 else if (local_variable_p (t
)
15429 && uses_template_parms (DECL_CONTEXT (t
)))
15431 r
= retrieve_local_specialization (t
);
15432 if (r
== NULL_TREE
)
15434 /* First try name lookup to find the instantiation. */
15435 r
= lookup_name (DECL_NAME (t
));
15436 if (r
&& !is_capture_proxy (r
))
15438 /* Make sure that the one we found is the one we want. */
15439 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15440 if (ctx
!= DECL_CONTEXT (r
))
15448 /* This can happen for a variable used in a
15449 late-specified return type of a local lambda, or for a
15450 local static or constant. Building a new VAR_DECL
15451 should be OK in all those cases. */
15452 r
= tsubst_decl (t
, args
, complain
);
15453 if (local_specializations
)
15454 /* Avoid infinite recursion (79640). */
15455 register_local_specialization (r
, t
);
15456 if (decl_maybe_constant_var_p (r
))
15458 /* We can't call cp_finish_decl, so handle the
15459 initializer by hand. */
15460 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15461 complain
, in_decl
);
15462 if (!processing_template_decl
)
15463 init
= maybe_constant_init (init
);
15464 if (processing_template_decl
15465 ? potential_constant_expression (init
)
15466 : reduced_constant_expression_p (init
))
15467 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15468 = TREE_CONSTANT (r
) = true;
15469 DECL_INITIAL (r
) = init
;
15470 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15472 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15473 complain
, adc_variable_type
);
15475 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15476 || decl_constant_var_p (r
)
15477 || errorcount
|| sorrycount
);
15478 if (!processing_template_decl
15479 && !TREE_STATIC (r
))
15480 r
= process_outer_var_ref (r
, complain
);
15482 /* Remember this for subsequent uses. */
15483 if (local_specializations
)
15484 register_local_specialization (r
, t
);
15486 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15487 r
= argument_pack_select_arg (r
);
15491 if (!mark_used (r
, complain
))
15492 return error_mark_node
;
15495 case NAMESPACE_DECL
:
15499 /* An OVERLOAD will always be a non-dependent overload set; an
15500 overload set from function scope will just be represented with an
15501 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15502 gcc_assert (!uses_template_parms (t
));
15503 /* We must have marked any lookups as persistent. */
15504 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
15508 return tsubst_baselink (t
, current_nonlambda_class_type (),
15509 args
, complain
, in_decl
);
15511 case TEMPLATE_DECL
:
15512 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15513 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15514 args
, complain
, in_decl
);
15515 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15516 return tsubst (t
, args
, complain
, in_decl
);
15517 else if (DECL_CLASS_SCOPE_P (t
)
15518 && uses_template_parms (DECL_CONTEXT (t
)))
15520 /* Template template argument like the following example need
15523 template <template <class> class TT> struct C {};
15524 template <class T> struct D {
15525 template <class U> struct E {};
15530 We are processing the template argument `E' in #1 for
15531 the template instantiation #2. Originally, `E' is a
15532 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15533 have to substitute this with one having context `D<int>'. */
15535 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15536 if (dependent_scope_p (context
))
15538 /* When rewriting a constructor into a deduction guide, a
15539 non-dependent name can become dependent, so memtmpl<args>
15540 becomes context::template memtmpl<args>. */
15541 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15542 return build_qualified_name (type
, context
, DECL_NAME (t
),
15545 return lookup_field (context
, DECL_NAME(t
), 0, false);
15548 /* Ordinary template template argument. */
15551 case NON_LVALUE_EXPR
:
15552 case VIEW_CONVERT_EXPR
:
15554 /* Handle location wrappers by substituting the wrapped node
15555 first, *then* reusing the resulting type. Doing the type
15556 first ensures that we handle template parameters and
15557 parameter pack expansions. */
15558 gcc_assert (location_wrapper_p (t
));
15559 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15560 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15564 case REINTERPRET_CAST_EXPR
:
15565 case CONST_CAST_EXPR
:
15566 case STATIC_CAST_EXPR
:
15567 case DYNAMIC_CAST_EXPR
:
15568 case IMPLICIT_CONV_EXPR
:
15572 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15573 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15574 return build1 (code
, type
, op0
);
15578 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15579 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15581 tree expanded
, op
= TREE_OPERAND (t
, 0);
15584 if (SIZEOF_EXPR_TYPE_P (t
))
15585 op
= TREE_TYPE (op
);
15587 ++cp_unevaluated_operand
;
15588 ++c_inhibit_evaluation_warnings
;
15589 /* We only want to compute the number of arguments. */
15590 if (PACK_EXPANSION_P (op
))
15591 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15593 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15594 args
, complain
, in_decl
);
15595 --cp_unevaluated_operand
;
15596 --c_inhibit_evaluation_warnings
;
15598 if (TREE_CODE (expanded
) == TREE_VEC
)
15600 len
= TREE_VEC_LENGTH (expanded
);
15601 /* Set TREE_USED for the benefit of -Wunused. */
15602 for (int i
= 0; i
< len
; i
++)
15603 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15604 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15607 if (expanded
== error_mark_node
)
15608 return error_mark_node
;
15609 else if (PACK_EXPANSION_P (expanded
)
15610 || (TREE_CODE (expanded
) == TREE_VEC
15611 && pack_expansion_args_count (expanded
)))
15614 if (PACK_EXPANSION_P (expanded
))
15616 else if (TREE_VEC_LENGTH (expanded
) == 1)
15617 expanded
= TREE_VEC_ELT (expanded
, 0);
15619 expanded
= make_argument_pack (expanded
);
15621 if (TYPE_P (expanded
))
15622 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15624 complain
& tf_error
);
15626 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15627 complain
& tf_error
);
15630 return build_int_cst (size_type_node
, len
);
15632 if (SIZEOF_EXPR_TYPE_P (t
))
15634 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15635 args
, complain
, in_decl
);
15636 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15637 r
= build1 (SIZEOF_EXPR
,
15638 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15639 SIZEOF_EXPR_TYPE_P (r
) = 1;
15646 case TRUTH_NOT_EXPR
:
15649 case UNARY_PLUS_EXPR
: /* Unary + */
15651 case AT_ENCODE_EXPR
:
15655 case REALPART_EXPR
:
15656 case IMAGPART_EXPR
:
15659 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15660 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15661 r
= build1 (code
, type
, op0
);
15662 if (code
== ALIGNOF_EXPR
)
15663 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15667 case COMPONENT_REF
:
15672 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15673 name
= TREE_OPERAND (t
, 1);
15674 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15676 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15677 complain
, in_decl
);
15678 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15680 else if (TREE_CODE (name
) == SCOPE_REF
15681 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15683 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15684 complain
, in_decl
);
15685 name
= TREE_OPERAND (name
, 1);
15686 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15687 complain
, in_decl
);
15688 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15689 name
= build_qualified_name (/*type=*/NULL_TREE
,
15691 /*template_p=*/false);
15693 else if (BASELINK_P (name
))
15694 name
= tsubst_baselink (name
,
15695 non_reference (TREE_TYPE (object
)),
15699 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15700 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15706 case TRUNC_DIV_EXPR
:
15707 case CEIL_DIV_EXPR
:
15708 case FLOOR_DIV_EXPR
:
15709 case ROUND_DIV_EXPR
:
15710 case EXACT_DIV_EXPR
:
15714 case TRUNC_MOD_EXPR
:
15715 case FLOOR_MOD_EXPR
:
15716 case TRUTH_ANDIF_EXPR
:
15717 case TRUTH_ORIF_EXPR
:
15718 case TRUTH_AND_EXPR
:
15719 case TRUTH_OR_EXPR
:
15732 case COMPOUND_EXPR
:
15735 case PREDECREMENT_EXPR
:
15736 case PREINCREMENT_EXPR
:
15737 case POSTDECREMENT_EXPR
:
15738 case POSTINCREMENT_EXPR
:
15740 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15741 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15742 return build_nt (code
, op0
, op1
);
15747 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15748 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15749 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15750 QUALIFIED_NAME_IS_TEMPLATE (t
));
15755 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15756 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15757 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15762 int n
= VL_EXP_OPERAND_LENGTH (t
);
15763 tree result
= build_vl_exp (CALL_EXPR
, n
);
15765 for (i
= 0; i
< n
; i
++)
15766 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15767 complain
, in_decl
);
15773 case PSEUDO_DTOR_EXPR
:
15774 case VEC_PERM_EXPR
:
15776 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15777 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15778 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15779 r
= build_nt (code
, op0
, op1
, op2
);
15780 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15786 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15787 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15788 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15789 r
= build_nt (code
, op0
, op1
, op2
);
15790 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15796 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15797 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15798 r
= build_nt (code
, op0
, op1
);
15799 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15800 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15804 case TEMPLATE_ID_EXPR
:
15806 /* Substituted template arguments */
15807 tree fn
= TREE_OPERAND (t
, 0);
15808 tree targs
= TREE_OPERAND (t
, 1);
15810 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15812 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15814 return lookup_template_function (fn
, targs
);
15819 tree purpose
, value
, chain
;
15821 if (t
== void_list_node
)
15824 purpose
= TREE_PURPOSE (t
);
15826 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15827 value
= TREE_VALUE (t
);
15829 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15830 chain
= TREE_CHAIN (t
);
15831 if (chain
&& chain
!= void_type_node
)
15832 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15833 if (purpose
== TREE_PURPOSE (t
)
15834 && value
== TREE_VALUE (t
)
15835 && chain
== TREE_CHAIN (t
))
15837 return tree_cons (purpose
, value
, chain
);
15842 case ENUMERAL_TYPE
:
15844 case TEMPLATE_TYPE_PARM
:
15845 case TEMPLATE_TEMPLATE_PARM
:
15846 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15847 case TEMPLATE_PARM_INDEX
:
15849 case REFERENCE_TYPE
:
15851 case FUNCTION_TYPE
:
15854 case TYPENAME_TYPE
:
15855 case UNBOUND_CLASS_TEMPLATE
:
15857 case DECLTYPE_TYPE
:
15859 return tsubst (t
, args
, complain
, in_decl
);
15863 /* Fall through. */
15864 case IDENTIFIER_NODE
:
15865 if (IDENTIFIER_CONV_OP_P (t
))
15867 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15868 return make_conv_op_name (new_type
);
15874 /* This is handled by tsubst_copy_and_build. */
15875 gcc_unreachable ();
15879 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15880 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15881 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15884 case CLEANUP_POINT_EXPR
:
15885 /* We shouldn't have built any of these during initial template
15886 generation. Instead, they should be built during instantiation
15887 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15888 gcc_unreachable ();
15892 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15893 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15894 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15895 r
= build2 (code
, type
, op0
, op1
);
15896 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15897 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15898 && !(complain
& tf_error
))
15899 return error_mark_node
;
15903 case EXPR_PACK_EXPANSION
:
15904 error ("invalid use of pack expansion expression");
15905 return error_mark_node
;
15907 case NONTYPE_ARGUMENT_PACK
:
15908 error ("use %<...%> to expand argument pack");
15909 return error_mark_node
;
15912 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15920 /* Instantiate any typedefs in the type. */
15921 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15922 r
= fold_convert (type
, t
);
15923 gcc_assert (TREE_CODE (r
) == code
);
15928 /* These can sometimes show up in a partial instantiation, but never
15929 involve template parms. */
15930 gcc_assert (!uses_template_parms (t
));
15933 case UNARY_LEFT_FOLD_EXPR
:
15934 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15935 case UNARY_RIGHT_FOLD_EXPR
:
15936 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15937 case BINARY_LEFT_FOLD_EXPR
:
15938 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15939 case BINARY_RIGHT_FOLD_EXPR
:
15940 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15944 case DEBUG_BEGIN_STMT
:
15945 /* ??? There's no point in copying it for now, but maybe some
15946 day it will contain more information, such as a pointer back
15947 to the containing function, inlined copy or so. */
15951 /* We shouldn't get here, but keep going if !flag_checking. */
15953 gcc_unreachable ();
15958 /* Helper function for tsubst_omp_clauses, used for instantiation of
15959 OMP_CLAUSE_DECL of clauses. */
15962 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15965 if (decl
== NULL_TREE
)
15968 /* Handle an OpenMP array section represented as a TREE_LIST (or
15969 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15970 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15971 TREE_LIST. We can handle it exactly the same as an array section
15972 (purpose, value, and a chain), even though the nomenclature
15973 (low_bound, length, etc) is different. */
15974 if (TREE_CODE (decl
) == TREE_LIST
)
15977 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15978 /*integral_constant_expression_p=*/false);
15979 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15980 /*integral_constant_expression_p=*/false);
15981 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15983 if (TREE_PURPOSE (decl
) == low_bound
15984 && TREE_VALUE (decl
) == length
15985 && TREE_CHAIN (decl
) == chain
)
15987 tree ret
= tree_cons (low_bound
, length
, chain
);
15988 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15989 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15992 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15993 /*integral_constant_expression_p=*/false);
15994 /* Undo convert_from_reference tsubst_expr could have called. */
15996 && REFERENCE_REF_P (ret
)
15997 && !REFERENCE_REF_P (decl
))
15998 ret
= TREE_OPERAND (ret
, 0);
16002 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16005 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16006 tree args
, tsubst_flags_t complain
, tree in_decl
)
16008 tree new_clauses
= NULL_TREE
, nc
, oc
;
16009 tree linear_no_step
= NULL_TREE
;
16011 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16013 nc
= copy_node (oc
);
16014 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16017 switch (OMP_CLAUSE_CODE (nc
))
16019 case OMP_CLAUSE_LASTPRIVATE
:
16020 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16022 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16023 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16024 in_decl
, /*integral_constant_expression_p=*/false);
16025 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16026 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16029 case OMP_CLAUSE_PRIVATE
:
16030 case OMP_CLAUSE_SHARED
:
16031 case OMP_CLAUSE_FIRSTPRIVATE
:
16032 case OMP_CLAUSE_COPYIN
:
16033 case OMP_CLAUSE_COPYPRIVATE
:
16034 case OMP_CLAUSE_UNIFORM
:
16035 case OMP_CLAUSE_DEPEND
:
16036 case OMP_CLAUSE_FROM
:
16037 case OMP_CLAUSE_TO
:
16038 case OMP_CLAUSE_MAP
:
16039 case OMP_CLAUSE_USE_DEVICE_PTR
:
16040 case OMP_CLAUSE_IS_DEVICE_PTR
:
16041 OMP_CLAUSE_DECL (nc
)
16042 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16045 case OMP_CLAUSE_TILE
:
16046 case OMP_CLAUSE_IF
:
16047 case OMP_CLAUSE_NUM_THREADS
:
16048 case OMP_CLAUSE_SCHEDULE
:
16049 case OMP_CLAUSE_COLLAPSE
:
16050 case OMP_CLAUSE_FINAL
:
16051 case OMP_CLAUSE_DEVICE
:
16052 case OMP_CLAUSE_DIST_SCHEDULE
:
16053 case OMP_CLAUSE_NUM_TEAMS
:
16054 case OMP_CLAUSE_THREAD_LIMIT
:
16055 case OMP_CLAUSE_SAFELEN
:
16056 case OMP_CLAUSE_SIMDLEN
:
16057 case OMP_CLAUSE_NUM_TASKS
:
16058 case OMP_CLAUSE_GRAINSIZE
:
16059 case OMP_CLAUSE_PRIORITY
:
16060 case OMP_CLAUSE_ORDERED
:
16061 case OMP_CLAUSE_HINT
:
16062 case OMP_CLAUSE_NUM_GANGS
:
16063 case OMP_CLAUSE_NUM_WORKERS
:
16064 case OMP_CLAUSE_VECTOR_LENGTH
:
16065 case OMP_CLAUSE_WORKER
:
16066 case OMP_CLAUSE_VECTOR
:
16067 case OMP_CLAUSE_ASYNC
:
16068 case OMP_CLAUSE_WAIT
:
16069 OMP_CLAUSE_OPERAND (nc
, 0)
16070 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16071 in_decl
, /*integral_constant_expression_p=*/false);
16073 case OMP_CLAUSE_REDUCTION
:
16074 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16076 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16077 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16079 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16080 complain
, in_decl
);
16081 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16082 = build_qualified_name (NULL_TREE
, scope
,
16083 TREE_OPERAND (placeholder
, 1),
16087 gcc_assert (identifier_p (placeholder
));
16089 OMP_CLAUSE_DECL (nc
)
16090 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16093 case OMP_CLAUSE_GANG
:
16094 case OMP_CLAUSE_ALIGNED
:
16095 OMP_CLAUSE_DECL (nc
)
16096 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16098 OMP_CLAUSE_OPERAND (nc
, 1)
16099 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16100 in_decl
, /*integral_constant_expression_p=*/false);
16102 case OMP_CLAUSE_LINEAR
:
16103 OMP_CLAUSE_DECL (nc
)
16104 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16106 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16108 gcc_assert (!linear_no_step
);
16109 linear_no_step
= nc
;
16111 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16112 OMP_CLAUSE_LINEAR_STEP (nc
)
16113 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16114 complain
, in_decl
);
16116 OMP_CLAUSE_LINEAR_STEP (nc
)
16117 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16119 /*integral_constant_expression_p=*/false);
16121 case OMP_CLAUSE_NOWAIT
:
16122 case OMP_CLAUSE_DEFAULT
:
16123 case OMP_CLAUSE_UNTIED
:
16124 case OMP_CLAUSE_MERGEABLE
:
16125 case OMP_CLAUSE_INBRANCH
:
16126 case OMP_CLAUSE_NOTINBRANCH
:
16127 case OMP_CLAUSE_PROC_BIND
:
16128 case OMP_CLAUSE_FOR
:
16129 case OMP_CLAUSE_PARALLEL
:
16130 case OMP_CLAUSE_SECTIONS
:
16131 case OMP_CLAUSE_TASKGROUP
:
16132 case OMP_CLAUSE_NOGROUP
:
16133 case OMP_CLAUSE_THREADS
:
16134 case OMP_CLAUSE_SIMD
:
16135 case OMP_CLAUSE_DEFAULTMAP
:
16136 case OMP_CLAUSE_INDEPENDENT
:
16137 case OMP_CLAUSE_AUTO
:
16138 case OMP_CLAUSE_SEQ
:
16139 case OMP_CLAUSE_IF_PRESENT
:
16140 case OMP_CLAUSE_FINALIZE
:
16143 gcc_unreachable ();
16145 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16146 switch (OMP_CLAUSE_CODE (nc
))
16148 case OMP_CLAUSE_SHARED
:
16149 case OMP_CLAUSE_PRIVATE
:
16150 case OMP_CLAUSE_FIRSTPRIVATE
:
16151 case OMP_CLAUSE_LASTPRIVATE
:
16152 case OMP_CLAUSE_COPYPRIVATE
:
16153 case OMP_CLAUSE_LINEAR
:
16154 case OMP_CLAUSE_REDUCTION
:
16155 case OMP_CLAUSE_USE_DEVICE_PTR
:
16156 case OMP_CLAUSE_IS_DEVICE_PTR
:
16157 /* tsubst_expr on SCOPE_REF results in returning
16158 finish_non_static_data_member result. Undo that here. */
16159 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16160 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16161 == IDENTIFIER_NODE
))
16163 tree t
= OMP_CLAUSE_DECL (nc
);
16166 switch (TREE_CODE (v
))
16168 case COMPONENT_REF
:
16172 case POINTER_PLUS_EXPR
:
16173 v
= TREE_OPERAND (v
, 0);
16176 if (DECL_CONTEXT (v
) == current_function_decl
16177 && DECL_ARTIFICIAL (v
)
16178 && DECL_NAME (v
) == this_identifier
)
16179 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16186 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16187 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16188 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16189 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16190 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16192 tree decl
= OMP_CLAUSE_DECL (nc
);
16195 retrofit_lang_decl (decl
);
16196 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16205 new_clauses
= nreverse (new_clauses
);
16206 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16208 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16209 if (linear_no_step
)
16210 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16211 if (nc
== linear_no_step
)
16213 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16217 return new_clauses
;
16220 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16223 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16226 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16228 tree purpose
, value
, chain
;
16233 if (TREE_CODE (t
) != TREE_LIST
)
16234 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16235 /*function_p=*/false,
16236 /*integral_constant_expression_p=*/false);
16238 if (t
== void_list_node
)
16241 purpose
= TREE_PURPOSE (t
);
16243 purpose
= RECUR (purpose
);
16244 value
= TREE_VALUE (t
);
16247 if (TREE_CODE (value
) != LABEL_DECL
)
16248 value
= RECUR (value
);
16251 value
= lookup_label (DECL_NAME (value
));
16252 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16253 TREE_USED (value
) = 1;
16256 chain
= TREE_CHAIN (t
);
16257 if (chain
&& chain
!= void_type_node
)
16258 chain
= RECUR (chain
);
16259 return tree_cons (purpose
, value
, chain
);
16263 /* Used to temporarily communicate the list of #pragma omp parallel
16264 clauses to #pragma omp for instantiation if they are combined
16267 static tree
*omp_parallel_combined_clauses
;
16269 /* Substitute one OMP_FOR iterator. */
16272 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
16273 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16274 tree args
, tsubst_flags_t complain
, tree in_decl
,
16275 bool integral_constant_expression_p
)
16277 #define RECUR(NODE) \
16278 tsubst_expr ((NODE), args, complain, in_decl, \
16279 integral_constant_expression_p)
16280 tree decl
, init
, cond
, incr
;
16282 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16283 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16285 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16287 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16288 if (TREE_CODE (o
) == TREE_LIST
)
16289 TREE_VEC_ELT (orig_declv
, i
)
16290 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16291 RECUR (TREE_VALUE (o
)), NULL_TREE
);
16293 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16296 decl
= TREE_OPERAND (init
, 0);
16297 init
= TREE_OPERAND (init
, 1);
16298 tree decl_expr
= NULL_TREE
;
16299 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16301 /* We need to jump through some hoops to handle declarations in the
16302 init-statement, since we might need to handle auto deduction,
16303 but we need to keep control of initialization. */
16305 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16306 decl
= tsubst_decl (decl
, args
, complain
);
16310 if (TREE_CODE (decl
) == SCOPE_REF
)
16312 decl
= RECUR (decl
);
16313 if (TREE_CODE (decl
) == COMPONENT_REF
)
16317 switch (TREE_CODE (v
))
16319 case COMPONENT_REF
:
16323 case POINTER_PLUS_EXPR
:
16324 v
= TREE_OPERAND (v
, 0);
16327 if (DECL_CONTEXT (v
) == current_function_decl
16328 && DECL_ARTIFICIAL (v
)
16329 && DECL_NAME (v
) == this_identifier
)
16331 decl
= TREE_OPERAND (decl
, 1);
16332 decl
= omp_privatize_field (decl
, false);
16342 decl
= RECUR (decl
);
16344 init
= RECUR (init
);
16346 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16347 if (auto_node
&& init
)
16349 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16351 gcc_assert (!type_dependent_expression_p (decl
));
16353 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
16357 /* Declare the variable, but don't let that initialize it. */
16358 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16359 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16361 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16364 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16365 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16366 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16368 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16369 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16370 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16371 NOP_EXPR
, rhs
, complain
);
16374 incr
= RECUR (incr
);
16375 TREE_VEC_ELT (declv
, i
) = decl
;
16376 TREE_VEC_ELT (initv
, i
) = init
;
16377 TREE_VEC_ELT (condv
, i
) = cond
;
16378 TREE_VEC_ELT (incrv
, i
) = incr
;
16384 /* Declare and initialize the variable. */
16392 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16394 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16396 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16397 && OMP_CLAUSE_DECL (*pc
) == decl
)
16399 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16400 && OMP_CLAUSE_DECL (*pc
) == decl
)
16404 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16406 *pc
= OMP_CLAUSE_CHAIN (c
);
16407 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16410 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16411 && OMP_CLAUSE_DECL (*pc
) == decl
)
16413 error ("iteration variable %qD should not be firstprivate",
16415 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16417 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16418 && OMP_CLAUSE_DECL (*pc
) == decl
)
16420 error ("iteration variable %qD should not be reduction",
16422 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16425 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16430 if (*pc
== NULL_TREE
)
16432 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16433 OMP_CLAUSE_DECL (c
) = decl
;
16434 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16437 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16442 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16443 if (COMPARISON_CLASS_P (cond
))
16445 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16446 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16447 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16450 cond
= RECUR (cond
);
16451 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16452 switch (TREE_CODE (incr
))
16454 case PREINCREMENT_EXPR
:
16455 case PREDECREMENT_EXPR
:
16456 case POSTINCREMENT_EXPR
:
16457 case POSTDECREMENT_EXPR
:
16458 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16459 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16462 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16463 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16465 tree rhs
= TREE_OPERAND (incr
, 1);
16466 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16467 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16468 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16469 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16470 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16474 incr
= RECUR (incr
);
16477 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16478 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16480 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16481 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16482 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16483 TREE_TYPE (decl
), lhs
,
16484 RECUR (TREE_OPERAND (incr
, 2))));
16486 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16487 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16488 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16490 tree rhs
= TREE_OPERAND (incr
, 2);
16491 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16492 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16493 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16494 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16495 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16499 incr
= RECUR (incr
);
16502 incr
= RECUR (incr
);
16506 TREE_VEC_ELT (declv
, i
) = decl
;
16507 TREE_VEC_ELT (initv
, i
) = init
;
16508 TREE_VEC_ELT (condv
, i
) = cond
;
16509 TREE_VEC_ELT (incrv
, i
) = incr
;
16513 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16514 of OMP_TARGET's body. */
16517 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16519 *walk_subtrees
= 0;
16520 switch (TREE_CODE (*tp
))
16525 case STATEMENT_LIST
:
16526 *walk_subtrees
= 1;
16534 /* Helper function for tsubst_expr. For decomposition declaration
16535 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16536 also the corresponding decls representing the identifiers
16537 of the decomposition declaration. Return DECL if successful
16538 or error_mark_node otherwise, set *FIRST to the first decl
16539 in the list chained through DECL_CHAIN and *CNT to the number
16543 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16544 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16547 tree decl2
, decl3
, prev
= decl
;
16549 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16550 for (decl2
= DECL_CHAIN (pattern_decl
);
16553 && DECL_DECOMPOSITION_P (decl2
)
16554 && DECL_NAME (decl2
);
16555 decl2
= DECL_CHAIN (decl2
))
16557 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16559 gcc_assert (errorcount
);
16560 return error_mark_node
;
16563 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16564 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16565 tree v
= DECL_VALUE_EXPR (decl2
);
16566 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16567 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16568 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16569 SET_DECL_VALUE_EXPR (decl2
, v
);
16570 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16572 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16575 gcc_assert (errorcount
);
16576 decl
= error_mark_node
;
16579 maybe_push_decl (decl3
);
16580 if (error_operand_p (decl3
))
16581 decl
= error_mark_node
;
16582 else if (decl
!= error_mark_node
16583 && DECL_CHAIN (decl3
) != prev
16586 gcc_assert (errorcount
);
16587 decl
= error_mark_node
;
16596 /* Like tsubst_copy for expressions, etc. but also does semantic
16600 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16601 bool integral_constant_expression_p
)
16603 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16604 #define RECUR(NODE) \
16605 tsubst_expr ((NODE), args, complain, in_decl, \
16606 integral_constant_expression_p)
16612 if (t
== NULL_TREE
|| t
== error_mark_node
)
16615 loc
= input_location
;
16616 if (location_t eloc
= cp_expr_location (t
))
16617 input_location
= eloc
;
16618 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16619 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16621 switch (TREE_CODE (t
))
16623 case STATEMENT_LIST
:
16625 tree_stmt_iterator i
;
16626 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16627 RECUR (tsi_stmt (i
));
16631 case CTOR_INITIALIZER
:
16632 finish_mem_initializers (tsubst_initializer_list
16633 (TREE_OPERAND (t
, 0), args
));
16637 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16641 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16642 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16643 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16645 finish_expr_stmt (tmp
);
16649 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16650 /*attribs=*/NULL_TREE
);
16655 tree decl
, pattern_decl
;
16658 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16659 if (TREE_CODE (decl
) == LABEL_DECL
)
16660 finish_label_decl (DECL_NAME (decl
));
16661 else if (TREE_CODE (decl
) == USING_DECL
)
16663 tree scope
= USING_DECL_SCOPE (decl
);
16664 tree name
= DECL_NAME (decl
);
16666 scope
= tsubst (scope
, args
, complain
, in_decl
);
16667 decl
= lookup_qualified_name (scope
, name
,
16668 /*is_type_p=*/false,
16669 /*complain=*/false);
16670 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16671 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16673 finish_local_using_decl (decl
, scope
, name
);
16675 else if (is_capture_proxy (decl
)
16676 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16678 /* We're in tsubst_lambda_expr, we've already inserted a new
16679 capture proxy, so look it up and register it. */
16681 if (DECL_PACK_P (decl
))
16683 inst
= (retrieve_local_specialization
16684 (DECL_CAPTURED_VARIABLE (decl
)));
16685 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16689 inst
= lookup_name_real (DECL_NAME (decl
), 0, 0,
16690 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
16691 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16693 register_local_specialization (inst
, decl
);
16696 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16697 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16698 /* Don't copy the old closure; we'll create a new one in
16699 tsubst_lambda_expr. */
16703 init
= DECL_INITIAL (decl
);
16704 decl
= tsubst (decl
, args
, complain
, in_decl
);
16705 if (decl
!= error_mark_node
)
16707 /* By marking the declaration as instantiated, we avoid
16708 trying to instantiate it. Since instantiate_decl can't
16709 handle local variables, and since we've already done
16710 all that needs to be done, that's the right thing to
16713 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16714 if (VAR_P (decl
) && !DECL_NAME (decl
)
16715 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
16716 /* Anonymous aggregates are a special case. */
16717 finish_anon_union (decl
);
16718 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
16720 DECL_CONTEXT (decl
) = current_function_decl
;
16721 if (DECL_NAME (decl
) == this_identifier
)
16723 tree lam
= DECL_CONTEXT (current_function_decl
);
16724 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
16725 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
16727 insert_capture_proxy (decl
);
16729 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
16730 /* We already did a pushtag. */;
16731 else if (TREE_CODE (decl
) == FUNCTION_DECL
16732 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
16733 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
16735 DECL_CONTEXT (decl
) = NULL_TREE
;
16737 DECL_CONTEXT (decl
) = current_function_decl
;
16738 cp_check_omp_declare_reduction (decl
);
16742 int const_init
= false;
16743 unsigned int cnt
= 0;
16744 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
16745 maybe_push_decl (decl
);
16748 && DECL_DECOMPOSITION_P (decl
)
16749 && TREE_TYPE (pattern_decl
) != error_mark_node
)
16750 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
16751 complain
, in_decl
, &first
,
16755 && DECL_PRETTY_FUNCTION_P (decl
))
16757 /* For __PRETTY_FUNCTION__ we have to adjust the
16759 const char *const name
16760 = cxx_printable_name (current_function_decl
, 2);
16761 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
16764 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
16767 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16770 if (ndecl
!= error_mark_node
)
16771 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
16773 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
16775 if (ndecl
!= error_mark_node
)
16776 cp_finish_decomp (ndecl
, first
, cnt
);
16785 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
16786 RECUR (FOR_INIT_STMT (t
));
16787 finish_init_stmt (stmt
);
16788 tmp
= RECUR (FOR_COND (t
));
16789 finish_for_cond (tmp
, stmt
, false, 0);
16790 tmp
= RECUR (FOR_EXPR (t
));
16791 finish_for_expr (tmp
, stmt
);
16793 bool prev
= note_iteration_stmt_body_start ();
16794 RECUR (FOR_BODY (t
));
16795 note_iteration_stmt_body_end (prev
);
16797 finish_for_stmt (stmt
);
16800 case RANGE_FOR_STMT
:
16802 /* Construct another range_for, if this is not a final
16803 substitution (for inside inside a generic lambda of a
16804 template). Otherwise convert to a regular for. */
16806 stmt
= (processing_template_decl
16807 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
16808 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
16809 decl
= RANGE_FOR_DECL (t
);
16810 decl
= tsubst (decl
, args
, complain
, in_decl
);
16811 maybe_push_decl (decl
);
16812 expr
= RECUR (RANGE_FOR_EXPR (t
));
16814 tree decomp_first
= NULL_TREE
;
16815 unsigned decomp_cnt
= 0;
16816 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
16817 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
16819 &decomp_first
, &decomp_cnt
);
16821 if (processing_template_decl
)
16823 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
16824 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
16825 finish_range_for_decl (stmt
, decl
, expr
);
16829 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
16830 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
16831 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
16832 decomp_first
, decomp_cnt
,
16833 RANGE_FOR_IVDEP (t
), unroll
);
16836 bool prev
= note_iteration_stmt_body_start ();
16837 RECUR (RANGE_FOR_BODY (t
));
16838 note_iteration_stmt_body_end (prev
);
16839 finish_for_stmt (stmt
);
16844 stmt
= begin_while_stmt ();
16845 tmp
= RECUR (WHILE_COND (t
));
16846 finish_while_stmt_cond (tmp
, stmt
, false, 0);
16848 bool prev
= note_iteration_stmt_body_start ();
16849 RECUR (WHILE_BODY (t
));
16850 note_iteration_stmt_body_end (prev
);
16852 finish_while_stmt (stmt
);
16856 stmt
= begin_do_stmt ();
16858 bool prev
= note_iteration_stmt_body_start ();
16859 RECUR (DO_BODY (t
));
16860 note_iteration_stmt_body_end (prev
);
16862 finish_do_body (stmt
);
16863 tmp
= RECUR (DO_COND (t
));
16864 finish_do_stmt (tmp
, stmt
, false, 0);
16868 stmt
= begin_if_stmt ();
16869 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
16870 if (IF_STMT_CONSTEXPR_P (t
))
16871 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
16872 tmp
= RECUR (IF_COND (t
));
16873 tmp
= finish_if_stmt_cond (tmp
, stmt
);
16874 if (IF_STMT_CONSTEXPR_P (t
)
16875 && instantiation_dependent_expression_p (tmp
))
16877 /* We're partially instantiating a generic lambda, but the condition
16878 of the constexpr if is still dependent. Don't substitute into the
16879 branches now, just remember the template arguments. */
16880 do_poplevel (IF_SCOPE (stmt
));
16881 IF_COND (stmt
) = IF_COND (t
);
16882 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
16883 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
16884 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
16888 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
16889 /* Don't instantiate the THEN_CLAUSE. */;
16892 tree folded
= fold_non_dependent_expr (tmp
, complain
);
16893 bool inhibit
= integer_zerop (folded
);
16895 ++c_inhibit_evaluation_warnings
;
16896 RECUR (THEN_CLAUSE (t
));
16898 --c_inhibit_evaluation_warnings
;
16900 finish_then_clause (stmt
);
16902 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
16903 /* Don't instantiate the ELSE_CLAUSE. */;
16904 else if (ELSE_CLAUSE (t
))
16906 tree folded
= fold_non_dependent_expr (tmp
, complain
);
16907 bool inhibit
= integer_nonzerop (folded
);
16908 begin_else_clause (stmt
);
16910 ++c_inhibit_evaluation_warnings
;
16911 RECUR (ELSE_CLAUSE (t
));
16913 --c_inhibit_evaluation_warnings
;
16914 finish_else_clause (stmt
);
16917 finish_if_stmt (stmt
);
16921 if (BIND_EXPR_BODY_BLOCK (t
))
16922 stmt
= begin_function_body ();
16924 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16925 ? BCS_TRY_BLOCK
: 0);
16927 RECUR (BIND_EXPR_BODY (t
));
16929 if (BIND_EXPR_BODY_BLOCK (t
))
16930 finish_function_body (stmt
);
16932 finish_compound_stmt (stmt
);
16936 finish_break_stmt ();
16939 case CONTINUE_STMT
:
16940 finish_continue_stmt ();
16944 stmt
= begin_switch_stmt ();
16945 tmp
= RECUR (SWITCH_STMT_COND (t
));
16946 finish_switch_cond (tmp
, stmt
);
16947 RECUR (SWITCH_STMT_BODY (t
));
16948 finish_switch_stmt (stmt
);
16951 case CASE_LABEL_EXPR
:
16953 tree low
= RECUR (CASE_LOW (t
));
16954 tree high
= RECUR (CASE_HIGH (t
));
16955 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16956 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16957 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16958 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16964 tree decl
= LABEL_EXPR_LABEL (t
);
16967 label
= finish_label_stmt (DECL_NAME (decl
));
16968 if (TREE_CODE (label
) == LABEL_DECL
)
16969 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16970 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16971 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16976 tmp
= GOTO_DESTINATION (t
);
16977 if (TREE_CODE (tmp
) != LABEL_DECL
)
16978 /* Computed goto's must be tsubst'd into. On the other hand,
16979 non-computed gotos must not be; the identifier in question
16980 will have no binding. */
16983 tmp
= DECL_NAME (tmp
);
16984 finish_goto_stmt (tmp
);
16989 tree string
= RECUR (ASM_STRING (t
));
16990 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16991 complain
, in_decl
);
16992 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16993 complain
, in_decl
);
16994 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16995 complain
, in_decl
);
16996 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16997 complain
, in_decl
);
16998 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
17000 tree asm_expr
= tmp
;
17001 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17002 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17003 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17010 stmt
= begin_try_block ();
17011 RECUR (TRY_STMTS (t
));
17012 finish_cleanup_try_block (stmt
);
17013 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17017 tree compound_stmt
= NULL_TREE
;
17019 if (FN_TRY_BLOCK_P (t
))
17020 stmt
= begin_function_try_block (&compound_stmt
);
17022 stmt
= begin_try_block ();
17024 RECUR (TRY_STMTS (t
));
17026 if (FN_TRY_BLOCK_P (t
))
17027 finish_function_try_block (stmt
);
17029 finish_try_block (stmt
);
17031 RECUR (TRY_HANDLERS (t
));
17032 if (FN_TRY_BLOCK_P (t
))
17033 finish_function_handler_sequence (stmt
, compound_stmt
);
17035 finish_handler_sequence (stmt
);
17041 tree decl
= HANDLER_PARMS (t
);
17045 decl
= tsubst (decl
, args
, complain
, in_decl
);
17046 /* Prevent instantiate_decl from trying to instantiate
17047 this variable. We've already done all that needs to be
17049 if (decl
!= error_mark_node
)
17050 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17052 stmt
= begin_handler ();
17053 finish_handler_parms (decl
, stmt
);
17054 RECUR (HANDLER_BODY (t
));
17055 finish_handler (stmt
);
17060 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17061 if (CLASS_TYPE_P (tmp
))
17063 /* Local classes are not independent templates; they are
17064 instantiated along with their containing function. And this
17065 way we don't have to deal with pushing out of one local class
17066 to instantiate a member of another local class. */
17067 /* Closures are handled by the LAMBDA_EXPR. */
17068 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17069 complete_type (tmp
);
17070 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17072 || (TREE_CODE (fld
) == FUNCTION_DECL
17073 && !DECL_ARTIFICIAL (fld
)))
17074 && DECL_TEMPLATE_INSTANTIATION (fld
))
17075 instantiate_decl (fld
, /*defer_ok=*/false,
17076 /*expl_inst_class=*/false);
17080 case STATIC_ASSERT
:
17084 ++c_inhibit_evaluation_warnings
;
17086 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17089 /*integral_constant_expression_p=*/true);
17090 --c_inhibit_evaluation_warnings
;
17092 finish_static_assert (condition
,
17093 STATIC_ASSERT_MESSAGE (t
),
17094 STATIC_ASSERT_SOURCE_LOCATION (t
),
17095 /*member_p=*/false);
17100 case OACC_PARALLEL
:
17101 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17103 stmt
= begin_omp_parallel ();
17104 RECUR (OMP_BODY (t
));
17105 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17109 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17110 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17111 complain
, in_decl
);
17112 if (OMP_PARALLEL_COMBINED (t
))
17113 omp_parallel_combined_clauses
= &tmp
;
17114 stmt
= begin_omp_parallel ();
17115 RECUR (OMP_PARALLEL_BODY (t
));
17116 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17117 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17118 = OMP_PARALLEL_COMBINED (t
);
17119 pop_omp_privatization_clauses (r
);
17123 r
= push_omp_privatization_clauses (false);
17124 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17125 complain
, in_decl
);
17126 stmt
= begin_omp_task ();
17127 RECUR (OMP_TASK_BODY (t
));
17128 finish_omp_task (tmp
, stmt
);
17129 pop_omp_privatization_clauses (r
);
17134 case OMP_DISTRIBUTE
:
17138 tree clauses
, body
, pre_body
;
17139 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17140 tree orig_declv
= NULL_TREE
;
17141 tree incrv
= NULL_TREE
;
17142 enum c_omp_region_type ort
= C_ORT_OMP
;
17145 if (TREE_CODE (t
) == OACC_LOOP
)
17148 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17149 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17151 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17153 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17154 if (OMP_FOR_ORIG_DECLS (t
))
17155 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17156 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17157 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17158 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17161 stmt
= begin_omp_structured_block ();
17163 pre_body
= push_stmt_list ();
17164 RECUR (OMP_FOR_PRE_BODY (t
));
17165 pre_body
= pop_stmt_list (pre_body
);
17167 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17168 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17169 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
17170 incrv
, &clauses
, args
, complain
, in_decl
,
17171 integral_constant_expression_p
);
17172 omp_parallel_combined_clauses
= NULL
;
17174 body
= push_stmt_list ();
17175 RECUR (OMP_FOR_BODY (t
));
17176 body
= pop_stmt_list (body
);
17178 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17179 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17180 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17184 t
= make_node (TREE_CODE (t
));
17185 TREE_TYPE (t
) = void_type_node
;
17186 OMP_FOR_BODY (t
) = body
;
17187 OMP_FOR_PRE_BODY (t
) = pre_body
;
17188 OMP_FOR_CLAUSES (t
) = clauses
;
17189 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17193 add_stmt (finish_omp_structured_block (stmt
));
17194 pop_omp_privatization_clauses (r
);
17199 omp_parallel_combined_clauses
= NULL
;
17204 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17205 && OMP_TEAMS_COMBINED (t
));
17206 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17208 stmt
= push_stmt_list ();
17209 RECUR (OMP_BODY (t
));
17210 stmt
= pop_stmt_list (stmt
);
17213 OMP_BODY (t
) = stmt
;
17214 OMP_CLAUSES (t
) = tmp
;
17216 pop_omp_privatization_clauses (r
);
17220 case OMP_TARGET_DATA
:
17222 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17223 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17225 keep_next_level (true);
17226 stmt
= begin_omp_structured_block ();
17228 RECUR (OMP_BODY (t
));
17229 stmt
= finish_omp_structured_block (stmt
);
17232 OMP_BODY (t
) = stmt
;
17233 OMP_CLAUSES (t
) = tmp
;
17234 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17236 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17239 /* For combined target teams, ensure the num_teams and
17240 thread_limit clause expressions are evaluated on the host,
17241 before entering the target construct. */
17243 for (c
= OMP_TEAMS_CLAUSES (teams
);
17244 c
; c
= OMP_CLAUSE_CHAIN (c
))
17245 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17246 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17247 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17249 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17250 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17251 if (expr
== error_mark_node
)
17253 tmp
= TARGET_EXPR_SLOT (expr
);
17255 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17256 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17257 OMP_CLAUSE_FIRSTPRIVATE
);
17258 OMP_CLAUSE_DECL (tc
) = tmp
;
17259 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17260 OMP_TARGET_CLAUSES (t
) = tc
;
17269 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17270 complain
, in_decl
);
17271 OACC_DECLARE_CLAUSES (t
) = tmp
;
17275 case OMP_TARGET_UPDATE
:
17276 case OMP_TARGET_ENTER_DATA
:
17277 case OMP_TARGET_EXIT_DATA
:
17278 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17279 complain
, in_decl
);
17281 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17285 case OACC_ENTER_DATA
:
17286 case OACC_EXIT_DATA
:
17288 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17289 complain
, in_decl
);
17291 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17296 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17297 complain
, in_decl
);
17298 stmt
= push_stmt_list ();
17299 RECUR (OMP_BODY (t
));
17300 stmt
= pop_stmt_list (stmt
);
17303 OMP_BODY (t
) = stmt
;
17304 OMP_ORDERED_CLAUSES (t
) = tmp
;
17310 case OMP_TASKGROUP
:
17311 stmt
= push_stmt_list ();
17312 RECUR (OMP_BODY (t
));
17313 stmt
= pop_stmt_list (stmt
);
17316 OMP_BODY (t
) = stmt
;
17321 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17322 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17324 tree op1
= TREE_OPERAND (t
, 1);
17325 tree rhs1
= NULL_TREE
;
17327 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17329 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17330 op1
= TREE_OPERAND (op1
, 1);
17332 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17333 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17334 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
17335 NULL_TREE
, NULL_TREE
, rhs1
,
17336 OMP_ATOMIC_SEQ_CST (t
));
17340 tree op1
= TREE_OPERAND (t
, 1);
17341 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17342 tree rhs1
= NULL_TREE
;
17343 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17344 enum tree_code opcode
= NOP_EXPR
;
17345 if (code
== OMP_ATOMIC_READ
)
17347 v
= RECUR (TREE_OPERAND (op1
, 0));
17348 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17350 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17351 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17353 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17354 v
= RECUR (TREE_OPERAND (op1
, 0));
17355 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17356 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17358 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17359 op11
= TREE_OPERAND (op11
, 1);
17361 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17362 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17363 opcode
= TREE_CODE (op11
);
17364 if (opcode
== MODIFY_EXPR
)
17370 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17371 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17373 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
17374 OMP_ATOMIC_SEQ_CST (t
));
17378 case TRANSACTION_EXPR
:
17381 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17382 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17384 if (TRANSACTION_EXPR_IS_STMT (t
))
17386 tree body
= TRANSACTION_EXPR_BODY (t
);
17387 tree noex
= NULL_TREE
;
17388 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17390 noex
= MUST_NOT_THROW_COND (body
);
17391 if (noex
== NULL_TREE
)
17392 noex
= boolean_true_node
;
17393 body
= TREE_OPERAND (body
, 0);
17395 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17397 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17401 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17402 RECUR (TRANSACTION_EXPR_BODY (t
)),
17409 case MUST_NOT_THROW_EXPR
:
17411 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17412 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17413 RETURN (build_must_not_throw_expr (op0
, cond
));
17416 case EXPR_PACK_EXPANSION
:
17417 error ("invalid use of pack expansion expression");
17418 RETURN (error_mark_node
);
17420 case NONTYPE_ARGUMENT_PACK
:
17421 error ("use %<...%> to expand argument pack");
17422 RETURN (error_mark_node
);
17424 case COMPOUND_EXPR
:
17425 tmp
= RECUR (TREE_OPERAND (t
, 0));
17426 if (tmp
== NULL_TREE
)
17427 /* If the first operand was a statement, we're done with it. */
17428 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17429 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17430 RECUR (TREE_OPERAND (t
, 1)),
17433 case ANNOTATE_EXPR
:
17434 tmp
= RECUR (TREE_OPERAND (t
, 0));
17435 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17436 TREE_TYPE (tmp
), tmp
,
17437 RECUR (TREE_OPERAND (t
, 1)),
17438 RECUR (TREE_OPERAND (t
, 2))));
17441 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17443 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17444 /*function_p=*/false,
17445 integral_constant_expression_p
));
17448 RETURN (NULL_TREE
);
17450 input_location
= loc
;
17456 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17457 function. For description of the body see comment above
17458 cp_parser_omp_declare_reduction_exprs. */
17461 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17463 if (t
== NULL_TREE
|| t
== error_mark_node
)
17466 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17468 tree_stmt_iterator tsi
;
17471 memset (stmts
, 0, sizeof stmts
);
17472 for (i
= 0, tsi
= tsi_start (t
);
17473 i
< 7 && !tsi_end_p (tsi
);
17474 i
++, tsi_next (&tsi
))
17475 stmts
[i
] = tsi_stmt (tsi
);
17476 gcc_assert (tsi_end_p (tsi
));
17480 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17481 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17482 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17483 args
, complain
, in_decl
);
17484 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17485 args
, complain
, in_decl
);
17486 DECL_CONTEXT (omp_out
) = current_function_decl
;
17487 DECL_CONTEXT (omp_in
) = current_function_decl
;
17488 keep_next_level (true);
17489 tree block
= begin_omp_structured_block ();
17490 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17491 block
= finish_omp_structured_block (block
);
17492 block
= maybe_cleanup_point_expr_void (block
);
17493 add_decl_expr (omp_out
);
17494 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17495 TREE_NO_WARNING (omp_out
) = 1;
17496 add_decl_expr (omp_in
);
17497 finish_expr_stmt (block
);
17501 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17502 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17503 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17504 args
, complain
, in_decl
);
17505 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17506 args
, complain
, in_decl
);
17507 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17508 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17509 keep_next_level (true);
17510 tree block
= begin_omp_structured_block ();
17511 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17512 block
= finish_omp_structured_block (block
);
17513 block
= maybe_cleanup_point_expr_void (block
);
17514 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17515 add_decl_expr (omp_priv
);
17516 add_decl_expr (omp_orig
);
17517 finish_expr_stmt (block
);
17519 add_decl_expr (omp_orig
);
17523 /* T is a postfix-expression that is not being used in a function
17524 call. Return the substituted version of T. */
17527 tsubst_non_call_postfix_expression (tree t
, tree args
,
17528 tsubst_flags_t complain
,
17531 if (TREE_CODE (t
) == SCOPE_REF
)
17532 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17533 /*done=*/false, /*address_p=*/false);
17535 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17536 /*function_p=*/false,
17537 /*integral_constant_expression_p=*/false);
17542 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17543 instantiation context. Instantiating a pack expansion containing a lambda
17544 might result in multiple lambdas all based on the same lambda in the
17548 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17550 tree oldfn
= lambda_function (t
);
17553 tree r
= build_lambda_expr ();
17555 LAMBDA_EXPR_LOCATION (r
)
17556 = LAMBDA_EXPR_LOCATION (t
);
17557 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17558 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17559 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17561 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17562 /* A lambda in a default argument outside a class gets no
17563 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17564 tsubst_default_argument calls start_lambda_scope, so we need to
17565 specifically ignore it here, and use the global scope. */
17566 record_null_lambda_scope (r
);
17568 record_lambda_scope (r
);
17570 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17571 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17573 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17574 cap
= TREE_CHAIN (cap
))
17576 tree field
= TREE_PURPOSE (cap
);
17577 if (PACK_EXPANSION_P (field
))
17578 field
= PACK_EXPANSION_PATTERN (field
);
17579 field
= tsubst_decl (field
, args
, complain
);
17581 if (field
== error_mark_node
)
17582 return error_mark_node
;
17584 tree init
= TREE_VALUE (cap
);
17585 if (PACK_EXPANSION_P (init
))
17586 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17588 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17589 /*fn*/false, /*constexpr*/false);
17591 if (TREE_CODE (field
) == TREE_VEC
)
17593 int len
= TREE_VEC_LENGTH (field
);
17594 gcc_assert (TREE_CODE (init
) == TREE_VEC
17595 && TREE_VEC_LENGTH (init
) == len
);
17596 for (int i
= 0; i
< len
; ++i
)
17597 LAMBDA_EXPR_CAPTURE_LIST (r
)
17598 = tree_cons (TREE_VEC_ELT (field
, i
),
17599 TREE_VEC_ELT (init
, i
),
17600 LAMBDA_EXPR_CAPTURE_LIST (r
));
17604 LAMBDA_EXPR_CAPTURE_LIST (r
)
17605 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17607 if (id_equal (DECL_NAME (field
), "__this"))
17608 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17612 tree type
= begin_lambda_type (r
);
17613 if (type
== error_mark_node
)
17614 return error_mark_node
;
17616 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17617 determine_visibility (TYPE_NAME (type
));
17619 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17621 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17622 ? DECL_TI_TEMPLATE (oldfn
)
17625 tree fntype
= static_fn_type (oldfn
);
17627 ++processing_template_decl
;
17628 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
17630 --processing_template_decl
;
17632 if (fntype
== error_mark_node
)
17633 r
= error_mark_node
;
17636 /* Fix the type of 'this'. */
17637 fntype
= build_memfn_type (fntype
, type
,
17638 type_memfn_quals (fntype
),
17639 type_memfn_rqual (fntype
));
17643 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
17644 fn
= DECL_TEMPLATE_RESULT (tmpl
);
17645 finish_member_declaration (tmpl
);
17650 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
17651 finish_member_declaration (fn
);
17654 /* Let finish_function set this. */
17655 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
17657 bool nested
= cfun
;
17659 push_function_context ();
17661 /* Still increment function_depth so that we don't GC in the
17662 middle of an expression. */
17665 local_specialization_stack
s (lss_copy
);
17667 tree body
= start_lambda_function (fn
, r
);
17669 register_parameter_specializations (oldfn
, fn
);
17673 /* We might not partially instantiate some parts of the function, so
17674 copy these flags from the original template. */
17675 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
17676 current_function_returns_value
= ol
->returns_value
;
17677 current_function_returns_null
= ol
->returns_null
;
17678 current_function_returns_abnormally
= ol
->returns_abnormally
;
17679 current_function_infinite_loop
= ol
->infinite_loop
;
17682 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
17683 /*constexpr*/false);
17685 finish_lambda_function (body
);
17688 pop_function_context ();
17692 /* The capture list was built up in reverse order; fix that now. */
17693 LAMBDA_EXPR_CAPTURE_LIST (r
)
17694 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
17696 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17698 maybe_add_lambda_conv_op (type
);
17701 finish_struct (type
, /*attr*/NULL_TREE
);
17703 insert_pending_capture_proxies ();
17708 /* Like tsubst but deals with expressions and performs semantic
17709 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17712 tsubst_copy_and_build (tree t
,
17714 tsubst_flags_t complain
,
17717 bool integral_constant_expression_p
)
17719 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17720 #define RECUR(NODE) \
17721 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17722 /*function_p=*/false, \
17723 integral_constant_expression_p)
17728 if (t
== NULL_TREE
|| t
== error_mark_node
)
17731 loc
= input_location
;
17732 if (location_t eloc
= cp_expr_location (t
))
17733 input_location
= eloc
;
17735 /* N3276 decltype magic only applies to calls at the top level or on the
17736 right side of a comma. */
17737 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
17738 complain
&= ~tf_decltype
;
17740 switch (TREE_CODE (t
))
17744 /* Fall through. */
17745 case IDENTIFIER_NODE
:
17749 bool non_integral_constant_expression_p
;
17750 const char *error_msg
;
17752 if (IDENTIFIER_CONV_OP_P (t
))
17754 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17755 t
= make_conv_op_name (new_type
);
17758 /* Look up the name. */
17759 decl
= lookup_name (t
);
17761 /* By convention, expressions use ERROR_MARK_NODE to indicate
17762 failure, not NULL_TREE. */
17763 if (decl
== NULL_TREE
)
17764 decl
= error_mark_node
;
17766 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
17768 integral_constant_expression_p
,
17769 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
17770 &non_integral_constant_expression_p
,
17771 /*template_p=*/false,
17773 /*address_p=*/false,
17774 /*template_arg_p=*/false,
17779 if (!function_p
&& identifier_p (decl
))
17781 if (complain
& tf_error
)
17782 unqualified_name_lookup_error (decl
);
17783 decl
= error_mark_node
;
17788 case TEMPLATE_ID_EXPR
:
17791 tree templ
= RECUR (TREE_OPERAND (t
, 0));
17792 tree targs
= TREE_OPERAND (t
, 1);
17795 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
17796 if (targs
== error_mark_node
)
17797 RETURN (error_mark_node
);
17799 if (TREE_CODE (templ
) == SCOPE_REF
)
17801 tree name
= TREE_OPERAND (templ
, 1);
17802 tree tid
= lookup_template_function (name
, targs
);
17803 TREE_OPERAND (templ
, 1) = tid
;
17807 if (variable_template_p (templ
))
17808 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
17810 if (TREE_CODE (templ
) == COMPONENT_REF
)
17812 object
= TREE_OPERAND (templ
, 0);
17813 templ
= TREE_OPERAND (templ
, 1);
17816 object
= NULL_TREE
;
17817 templ
= lookup_template_function (templ
, targs
);
17820 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
17821 object
, templ
, NULL_TREE
));
17823 RETURN (baselink_for_fns (templ
));
17828 tree r
= RECUR (TREE_OPERAND (t
, 0));
17830 if (REFERENCE_REF_P (t
))
17832 /* A type conversion to reference type will be enclosed in
17833 such an indirect ref, but the substitution of the cast
17834 will have also added such an indirect ref. */
17835 r
= convert_from_reference (r
);
17838 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
17839 complain
|decltype_flag
);
17841 if (REF_PARENTHESIZED_P (t
))
17842 r
= force_paren_expr (r
);
17849 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17850 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17851 RETURN (build_nop (type
, op0
));
17854 case IMPLICIT_CONV_EXPR
:
17856 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17857 tree expr
= RECUR (TREE_OPERAND (t
, 0));
17858 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
17860 retval
= copy_node (t
);
17861 TREE_TYPE (retval
) = type
;
17862 TREE_OPERAND (retval
, 0) = expr
;
17865 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
17866 /* We'll pass this to convert_nontype_argument again, we don't need
17867 to actually perform any conversion here. */
17869 int flags
= LOOKUP_IMPLICIT
;
17870 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
17871 flags
= LOOKUP_NORMAL
;
17872 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
17878 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17879 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17880 if (op0
== error_mark_node
)
17881 RETURN (error_mark_node
);
17882 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
17886 case REINTERPRET_CAST_EXPR
:
17887 case CONST_CAST_EXPR
:
17888 case DYNAMIC_CAST_EXPR
:
17889 case STATIC_CAST_EXPR
:
17892 tree op
, r
= NULL_TREE
;
17894 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17895 if (integral_constant_expression_p
17896 && !cast_valid_in_integral_constant_expression_p (type
))
17898 if (complain
& tf_error
)
17899 error ("a cast to a type other than an integral or "
17900 "enumeration type cannot appear in a constant-expression");
17901 RETURN (error_mark_node
);
17904 op
= RECUR (TREE_OPERAND (t
, 0));
17906 warning_sentinel
s(warn_useless_cast
);
17907 warning_sentinel
s2(warn_ignored_qualifiers
);
17908 switch (TREE_CODE (t
))
17911 r
= build_functional_cast (type
, op
, complain
);
17913 case REINTERPRET_CAST_EXPR
:
17914 r
= build_reinterpret_cast (type
, op
, complain
);
17916 case CONST_CAST_EXPR
:
17917 r
= build_const_cast (type
, op
, complain
);
17919 case DYNAMIC_CAST_EXPR
:
17920 r
= build_dynamic_cast (type
, op
, complain
);
17922 case STATIC_CAST_EXPR
:
17923 r
= build_static_cast (type
, op
, complain
);
17926 gcc_unreachable ();
17932 case POSTDECREMENT_EXPR
:
17933 case POSTINCREMENT_EXPR
:
17934 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17935 args
, complain
, in_decl
);
17936 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
17937 complain
|decltype_flag
));
17939 case PREDECREMENT_EXPR
:
17940 case PREINCREMENT_EXPR
:
17944 case TRUTH_NOT_EXPR
:
17945 case UNARY_PLUS_EXPR
: /* Unary + */
17946 case REALPART_EXPR
:
17947 case IMAGPART_EXPR
:
17948 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
17949 RECUR (TREE_OPERAND (t
, 0)),
17950 complain
|decltype_flag
));
17952 case FIX_TRUNC_EXPR
:
17953 gcc_unreachable ();
17956 op1
= TREE_OPERAND (t
, 0);
17957 if (TREE_CODE (op1
) == LABEL_DECL
)
17958 RETURN (finish_label_address_expr (DECL_NAME (op1
),
17959 EXPR_LOCATION (op1
)));
17960 if (TREE_CODE (op1
) == SCOPE_REF
)
17961 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
17962 /*done=*/true, /*address_p=*/true);
17964 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
17966 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
17967 complain
|decltype_flag
));
17972 case TRUNC_DIV_EXPR
:
17973 case CEIL_DIV_EXPR
:
17974 case FLOOR_DIV_EXPR
:
17975 case ROUND_DIV_EXPR
:
17976 case EXACT_DIV_EXPR
:
17980 case TRUNC_MOD_EXPR
:
17981 case FLOOR_MOD_EXPR
:
17982 case TRUTH_ANDIF_EXPR
:
17983 case TRUTH_ORIF_EXPR
:
17984 case TRUTH_AND_EXPR
:
17985 case TRUTH_OR_EXPR
:
18001 warning_sentinel
s1(warn_type_limits
);
18002 warning_sentinel
s2(warn_div_by_zero
);
18003 warning_sentinel
s3(warn_logical_op
);
18004 warning_sentinel
s4(warn_tautological_compare
);
18005 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18006 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18007 tree r
= build_x_binary_op
18008 (input_location
, TREE_CODE (t
),
18010 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18012 : TREE_CODE (TREE_OPERAND (t
, 0))),
18014 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18016 : TREE_CODE (TREE_OPERAND (t
, 1))),
18018 complain
|decltype_flag
);
18019 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18020 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18025 case POINTER_PLUS_EXPR
:
18027 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18028 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18029 RETURN (fold_build_pointer_plus (op0
, op1
));
18033 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18034 /*address_p=*/false));
18036 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18037 args
, complain
, in_decl
);
18038 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18039 RECUR (TREE_OPERAND (t
, 1)),
18040 complain
|decltype_flag
));
18043 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18044 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18045 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18052 op1
= TREE_OPERAND (t
, 0);
18053 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18054 op1
= TREE_TYPE (op1
);
18055 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18056 && ALIGNOF_EXPR_STD_P (t
));
18059 /* When there are no ARGS, we are trying to evaluate a
18060 non-dependent expression from the parser. Trying to do
18061 the substitutions may not work. */
18063 op1
= TREE_TYPE (op1
);
18067 ++cp_unevaluated_operand
;
18068 ++c_inhibit_evaluation_warnings
;
18070 op1
= tsubst (op1
, args
, complain
, in_decl
);
18072 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18073 /*function_p=*/false,
18074 /*integral_constant_expression_p=*/
18076 --cp_unevaluated_operand
;
18077 --c_inhibit_evaluation_warnings
;
18080 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18081 complain
& tf_error
);
18083 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18084 complain
& tf_error
);
18085 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18087 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18089 if (!processing_template_decl
&& TYPE_P (op1
))
18091 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18092 build1 (NOP_EXPR
, op1
, error_mark_node
));
18093 SIZEOF_EXPR_TYPE_P (r
) = 1;
18096 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18097 TREE_SIDE_EFFECTS (r
) = 0;
18098 TREE_READONLY (r
) = 1;
18100 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18105 case AT_ENCODE_EXPR
:
18107 op1
= TREE_OPERAND (t
, 0);
18108 ++cp_unevaluated_operand
;
18109 ++c_inhibit_evaluation_warnings
;
18110 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18111 /*function_p=*/false,
18112 /*integral_constant_expression_p=*/false);
18113 --cp_unevaluated_operand
;
18114 --c_inhibit_evaluation_warnings
;
18115 RETURN (objc_build_encode_expr (op1
));
18118 case NOEXCEPT_EXPR
:
18119 op1
= TREE_OPERAND (t
, 0);
18120 ++cp_unevaluated_operand
;
18121 ++c_inhibit_evaluation_warnings
;
18122 ++cp_noexcept_operand
;
18123 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18124 /*function_p=*/false,
18125 /*integral_constant_expression_p=*/false);
18126 --cp_unevaluated_operand
;
18127 --c_inhibit_evaluation_warnings
;
18128 --cp_noexcept_operand
;
18129 RETURN (finish_noexcept_expr (op1
, complain
));
18133 warning_sentinel
s(warn_div_by_zero
);
18134 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18135 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18136 tree r
= build_x_modify_expr
18137 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18138 complain
|decltype_flag
);
18139 /* TREE_NO_WARNING must be set if either the expression was
18140 parenthesized or it uses an operator such as >>= rather
18141 than plain assignment. In the former case, it was already
18142 set and must be copied. In the latter case,
18143 build_x_modify_expr sets it and it must not be reset
18145 if (TREE_NO_WARNING (t
))
18146 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18152 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18153 args
, complain
, in_decl
);
18154 /* Remember that there was a reference to this entity. */
18156 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18157 RETURN (error_mark_node
);
18158 RETURN (build_x_arrow (input_location
, op1
, complain
));
18162 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18163 tree init
= RECUR (TREE_OPERAND (t
, 3));
18164 vec
<tree
, va_gc
> *placement_vec
;
18165 vec
<tree
, va_gc
> *init_vec
;
18168 if (placement
== NULL_TREE
)
18169 placement_vec
= NULL
;
18172 placement_vec
= make_tree_vector ();
18173 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18174 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18177 /* If there was an initializer in the original tree, but it
18178 instantiated to an empty list, then we should pass a
18179 non-NULL empty vector to tell build_new that it was an
18180 empty initializer() rather than no initializer. This can
18181 only happen when the initializer is a pack expansion whose
18182 parameter packs are of length zero. */
18183 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18187 init_vec
= make_tree_vector ();
18188 if (init
== void_node
)
18189 gcc_assert (init_vec
!= NULL
);
18192 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18193 vec_safe_push (init_vec
, TREE_VALUE (init
));
18197 /* Avoid passing an enclosing decl to valid_array_size_p. */
18198 in_decl
= NULL_TREE
;
18200 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18201 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18202 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18203 NEW_EXPR_USE_GLOBAL (t
),
18206 if (placement_vec
!= NULL
)
18207 release_tree_vector (placement_vec
);
18208 if (init_vec
!= NULL
)
18209 release_tree_vector (init_vec
);
18216 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18217 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18218 RETURN (delete_sanity (op0
, op1
,
18219 DELETE_EXPR_USE_VEC (t
),
18220 DELETE_EXPR_USE_GLOBAL (t
),
18224 case COMPOUND_EXPR
:
18226 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18227 complain
& ~tf_decltype
, in_decl
,
18228 /*function_p=*/false,
18229 integral_constant_expression_p
);
18230 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18232 RECUR (TREE_OPERAND (t
, 1)),
18233 complain
|decltype_flag
));
18239 vec
<tree
, va_gc
> *call_args
;
18240 unsigned int nargs
, i
;
18245 function
= CALL_EXPR_FN (t
);
18246 /* Internal function with no arguments. */
18247 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18250 /* When we parsed the expression, we determined whether or
18251 not Koenig lookup should be performed. */
18252 koenig_p
= KOENIG_LOOKUP_P (t
);
18253 if (function
== NULL_TREE
)
18256 qualified_p
= false;
18258 else if (TREE_CODE (function
) == SCOPE_REF
)
18260 qualified_p
= true;
18261 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18263 /*address_p=*/false);
18265 else if (koenig_p
&& identifier_p (function
))
18267 /* Do nothing; calling tsubst_copy_and_build on an identifier
18268 would incorrectly perform unqualified lookup again.
18270 Note that we can also have an IDENTIFIER_NODE if the earlier
18271 unqualified lookup found a member function; in that case
18272 koenig_p will be false and we do want to do the lookup
18273 again to find the instantiated member function.
18275 FIXME but doing that causes c++/15272, so we need to stop
18276 using IDENTIFIER_NODE in that situation. */
18277 qualified_p
= false;
18281 if (TREE_CODE (function
) == COMPONENT_REF
)
18283 tree op
= TREE_OPERAND (function
, 1);
18285 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18286 || (BASELINK_P (op
)
18287 && BASELINK_QUALIFIED_P (op
)));
18290 qualified_p
= false;
18292 if (TREE_CODE (function
) == ADDR_EXPR
18293 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18294 /* Avoid error about taking the address of a constructor. */
18295 function
= TREE_OPERAND (function
, 0);
18297 function
= tsubst_copy_and_build (function
, args
, complain
,
18300 integral_constant_expression_p
);
18302 if (BASELINK_P (function
))
18303 qualified_p
= true;
18306 nargs
= call_expr_nargs (t
);
18307 call_args
= make_tree_vector ();
18308 for (i
= 0; i
< nargs
; ++i
)
18310 tree arg
= CALL_EXPR_ARG (t
, i
);
18312 if (!PACK_EXPANSION_P (arg
))
18313 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18316 /* Expand the pack expansion and push each entry onto
18318 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18319 if (TREE_CODE (arg
) == TREE_VEC
)
18321 unsigned int len
, j
;
18323 len
= TREE_VEC_LENGTH (arg
);
18324 for (j
= 0; j
< len
; ++j
)
18326 tree value
= TREE_VEC_ELT (arg
, j
);
18327 if (value
!= NULL_TREE
)
18328 value
= convert_from_reference (value
);
18329 vec_safe_push (call_args
, value
);
18334 /* A partial substitution. Add one entry. */
18335 vec_safe_push (call_args
, arg
);
18340 /* We do not perform argument-dependent lookup if normal
18341 lookup finds a non-function, in accordance with the
18342 expected resolution of DR 218. */
18344 && ((is_overloaded_fn (function
)
18345 /* If lookup found a member function, the Koenig lookup is
18346 not appropriate, even if an unqualified-name was used
18347 to denote the function. */
18348 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18349 || identifier_p (function
))
18350 /* Only do this when substitution turns a dependent call
18351 into a non-dependent call. */
18352 && type_dependent_expression_p_push (t
)
18353 && !any_type_dependent_arguments_p (call_args
))
18354 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18356 if (function
!= NULL_TREE
18357 && identifier_p (function
)
18358 && !any_type_dependent_arguments_p (call_args
))
18360 if (koenig_p
&& (complain
& tf_warning_or_error
))
18362 /* For backwards compatibility and good diagnostics, try
18363 the unqualified lookup again if we aren't in SFINAE
18365 tree unq
= (tsubst_copy_and_build
18366 (function
, args
, complain
, in_decl
, true,
18367 integral_constant_expression_p
));
18368 if (unq
== error_mark_node
)
18370 release_tree_vector (call_args
);
18371 RETURN (error_mark_node
);
18374 if (unq
!= function
)
18376 /* In a lambda fn, we have to be careful to not
18377 introduce new this captures. Legacy code can't
18378 be using lambdas anyway, so it's ok to be
18380 bool in_lambda
= (current_class_type
18381 && LAMBDA_TYPE_P (current_class_type
));
18382 char const *const msg
18383 = G_("%qD was not declared in this scope, "
18384 "and no declarations were found by "
18385 "argument-dependent lookup at the point "
18386 "of instantiation");
18390 error_at (cp_expr_loc_or_loc (t
, input_location
),
18393 diag
= permerror (cp_expr_loc_or_loc (t
, input_location
),
18399 if (INDIRECT_REF_P (fn
))
18400 fn
= TREE_OPERAND (fn
, 0);
18401 if (is_overloaded_fn (fn
))
18402 fn
= get_first_fn (fn
);
18405 /* Can't say anything more. */;
18406 else if (DECL_CLASS_SCOPE_P (fn
))
18408 location_t loc
= cp_expr_loc_or_loc (t
,
18411 "declarations in dependent base %qT are "
18412 "not found by unqualified lookup",
18413 DECL_CLASS_CONTEXT (fn
));
18414 if (current_class_ptr
)
18416 "use %<this->%D%> instead", function
);
18419 "use %<%T::%D%> instead",
18420 current_class_name
, function
);
18423 inform (DECL_SOURCE_LOCATION (fn
),
18424 "%qD declared here, later in the "
18425 "translation unit", fn
);
18428 release_tree_vector (call_args
);
18429 RETURN (error_mark_node
);
18436 if (identifier_p (function
))
18438 if (complain
& tf_error
)
18439 unqualified_name_lookup_error (function
);
18440 release_tree_vector (call_args
);
18441 RETURN (error_mark_node
);
18445 /* Remember that there was a reference to this entity. */
18446 if (function
!= NULL_TREE
18447 && DECL_P (function
)
18448 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18450 release_tree_vector (call_args
);
18451 RETURN (error_mark_node
);
18454 /* Put back tf_decltype for the actual call. */
18455 complain
|= decltype_flag
;
18457 if (function
== NULL_TREE
)
18458 switch (CALL_EXPR_IFN (t
))
18461 gcc_assert (nargs
== 1);
18462 if (vec_safe_length (call_args
) != 1)
18464 error_at (cp_expr_loc_or_loc (t
, input_location
),
18465 "wrong number of arguments to "
18466 "%<__builtin_launder%>");
18467 ret
= error_mark_node
;
18470 ret
= finish_builtin_launder (cp_expr_loc_or_loc (t
,
18472 (*call_args
)[0], complain
);
18476 /* Unsupported internal function with arguments. */
18477 gcc_unreachable ();
18479 else if (TREE_CODE (function
) == OFFSET_REF
18480 || TREE_CODE (function
) == DOTSTAR_EXPR
18481 || TREE_CODE (function
) == MEMBER_REF
)
18482 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18484 else if (TREE_CODE (function
) == COMPONENT_REF
)
18486 tree instance
= TREE_OPERAND (function
, 0);
18487 tree fn
= TREE_OPERAND (function
, 1);
18489 if (processing_template_decl
18490 && (type_dependent_expression_p (instance
)
18491 || (!BASELINK_P (fn
)
18492 && TREE_CODE (fn
) != FIELD_DECL
)
18493 || type_dependent_expression_p (fn
)
18494 || any_type_dependent_arguments_p (call_args
)))
18495 ret
= build_min_nt_call_vec (function
, call_args
);
18496 else if (!BASELINK_P (fn
))
18497 ret
= finish_call_expr (function
, &call_args
,
18498 /*disallow_virtual=*/false,
18499 /*koenig_p=*/false,
18502 ret
= (build_new_method_call
18504 &call_args
, NULL_TREE
,
18505 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18510 ret
= finish_call_expr (function
, &call_args
,
18511 /*disallow_virtual=*/qualified_p
,
18515 release_tree_vector (call_args
);
18517 if (ret
!= error_mark_node
)
18519 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18520 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18521 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18522 bool thk
= CALL_FROM_THUNK_P (t
);
18523 if (op
|| ord
|| rev
|| thk
)
18525 function
= extract_call_expr (ret
);
18526 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18527 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18528 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18531 if (TREE_CODE (function
) == CALL_EXPR
)
18532 CALL_FROM_THUNK_P (function
) = true;
18534 AGGR_INIT_FROM_THUNK_P (function
) = true;
18535 /* The thunk location is not interesting. */
18536 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
18546 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18547 cond
= mark_rvalue_use (cond
);
18548 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
18551 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
18553 if (integer_zerop (folded_cond
))
18555 ++c_inhibit_evaluation_warnings
;
18556 exp1
= RECUR (TREE_OPERAND (t
, 1));
18557 --c_inhibit_evaluation_warnings
;
18558 exp2
= RECUR (TREE_OPERAND (t
, 2));
18562 exp1
= RECUR (TREE_OPERAND (t
, 1));
18563 ++c_inhibit_evaluation_warnings
;
18564 exp2
= RECUR (TREE_OPERAND (t
, 2));
18565 --c_inhibit_evaluation_warnings
;
18567 cond
= folded_cond
;
18571 exp1
= RECUR (TREE_OPERAND (t
, 1));
18572 exp2
= RECUR (TREE_OPERAND (t
, 2));
18575 warning_sentinel
s(warn_duplicated_branches
);
18576 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
18577 cond
, exp1
, exp2
, complain
));
18580 case PSEUDO_DTOR_EXPR
:
18582 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18583 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18584 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
18585 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
18591 tree purpose
, value
, chain
;
18593 if (t
== void_list_node
)
18596 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
18597 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
18599 /* We have pack expansions, so expand those and
18600 create a new list out of it. */
18601 tree purposevec
= NULL_TREE
;
18602 tree valuevec
= NULL_TREE
;
18606 /* Expand the argument expressions. */
18607 if (TREE_PURPOSE (t
))
18608 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
18609 complain
, in_decl
);
18610 if (TREE_VALUE (t
))
18611 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
18612 complain
, in_decl
);
18614 /* Build the rest of the list. */
18615 chain
= TREE_CHAIN (t
);
18616 if (chain
&& chain
!= void_type_node
)
18617 chain
= RECUR (chain
);
18619 /* Determine the number of arguments. */
18620 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
18622 len
= TREE_VEC_LENGTH (purposevec
);
18623 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
18625 else if (TREE_CODE (valuevec
) == TREE_VEC
)
18626 len
= TREE_VEC_LENGTH (valuevec
);
18629 /* Since we only performed a partial substitution into
18630 the argument pack, we only RETURN (a single list
18632 if (purposevec
== TREE_PURPOSE (t
)
18633 && valuevec
== TREE_VALUE (t
)
18634 && chain
== TREE_CHAIN (t
))
18637 RETURN (tree_cons (purposevec
, valuevec
, chain
));
18640 /* Convert the argument vectors into a TREE_LIST */
18644 /* Grab the Ith values. */
18646 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
18649 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
18652 /* Build the list (backwards). */
18653 chain
= tree_cons (purpose
, value
, chain
);
18659 purpose
= TREE_PURPOSE (t
);
18661 purpose
= RECUR (purpose
);
18662 value
= TREE_VALUE (t
);
18664 value
= RECUR (value
);
18665 chain
= TREE_CHAIN (t
);
18666 if (chain
&& chain
!= void_type_node
)
18667 chain
= RECUR (chain
);
18668 if (purpose
== TREE_PURPOSE (t
)
18669 && value
== TREE_VALUE (t
)
18670 && chain
== TREE_CHAIN (t
))
18672 RETURN (tree_cons (purpose
, value
, chain
));
18675 case COMPONENT_REF
:
18682 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18683 args
, complain
, in_decl
);
18684 /* Remember that there was a reference to this entity. */
18685 if (DECL_P (object
)
18686 && !mark_used (object
, complain
) && !(complain
& tf_error
))
18687 RETURN (error_mark_node
);
18688 object_type
= TREE_TYPE (object
);
18690 member
= TREE_OPERAND (t
, 1);
18691 if (BASELINK_P (member
))
18692 member
= tsubst_baselink (member
,
18693 non_reference (TREE_TYPE (object
)),
18694 args
, complain
, in_decl
);
18696 member
= tsubst_copy (member
, args
, complain
, in_decl
);
18697 if (member
== error_mark_node
)
18698 RETURN (error_mark_node
);
18700 if (TREE_CODE (member
) == FIELD_DECL
)
18702 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
18703 if (TREE_CODE (r
) == COMPONENT_REF
)
18704 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18707 else if (type_dependent_expression_p (object
))
18708 /* We can't do much here. */;
18709 else if (!CLASS_TYPE_P (object_type
))
18711 if (scalarish_type_p (object_type
))
18713 tree s
= NULL_TREE
;
18714 tree dtor
= member
;
18716 if (TREE_CODE (dtor
) == SCOPE_REF
)
18718 s
= TREE_OPERAND (dtor
, 0);
18719 dtor
= TREE_OPERAND (dtor
, 1);
18721 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
18723 dtor
= TREE_OPERAND (dtor
, 0);
18725 RETURN (finish_pseudo_destructor_expr
18726 (object
, s
, dtor
, input_location
));
18730 else if (TREE_CODE (member
) == SCOPE_REF
18731 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
18733 /* Lookup the template functions now that we know what the
18735 tree scope
= TREE_OPERAND (member
, 0);
18736 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
18737 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
18738 member
= lookup_qualified_name (scope
, tmpl
,
18739 /*is_type_p=*/false,
18740 /*complain=*/false);
18741 if (BASELINK_P (member
))
18743 BASELINK_FUNCTIONS (member
)
18744 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
18746 member
= (adjust_result_of_qualified_name_lookup
18747 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
18752 qualified_name_lookup_error (scope
, tmpl
, member
,
18754 RETURN (error_mark_node
);
18757 else if (TREE_CODE (member
) == SCOPE_REF
18758 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
18759 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
18761 if (complain
& tf_error
)
18763 if (TYPE_P (TREE_OPERAND (member
, 0)))
18764 error ("%qT is not a class or namespace",
18765 TREE_OPERAND (member
, 0));
18767 error ("%qD is not a class or namespace",
18768 TREE_OPERAND (member
, 0));
18770 RETURN (error_mark_node
);
18773 r
= finish_class_member_access_expr (object
, member
,
18774 /*template_p=*/false,
18776 if (TREE_CODE (r
) == COMPONENT_REF
)
18777 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
18782 RETURN (build_throw
18783 (RECUR (TREE_OPERAND (t
, 0))));
18787 vec
<constructor_elt
, va_gc
> *n
;
18788 constructor_elt
*ce
;
18789 unsigned HOST_WIDE_INT idx
;
18790 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18791 bool process_index_p
;
18793 bool need_copy_p
= false;
18796 if (type
== error_mark_node
)
18797 RETURN (error_mark_node
);
18799 /* We do not want to process the index of aggregate
18800 initializers as they are identifier nodes which will be
18801 looked up by digest_init. */
18802 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
18804 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
18805 newlen
= vec_safe_length (n
);
18806 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
18808 if (ce
->index
&& process_index_p
18809 /* An identifier index is looked up in the type
18810 being initialized, not the current scope. */
18811 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
18812 ce
->index
= RECUR (ce
->index
);
18814 if (PACK_EXPANSION_P (ce
->value
))
18816 /* Substitute into the pack expansion. */
18817 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
18820 if (ce
->value
== error_mark_node
18821 || PACK_EXPANSION_P (ce
->value
))
18823 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
18824 /* Just move the argument into place. */
18825 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
18828 /* Update the length of the final CONSTRUCTOR
18829 arguments vector, and note that we will need to
18831 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
18832 need_copy_p
= true;
18836 ce
->value
= RECUR (ce
->value
);
18841 vec
<constructor_elt
, va_gc
> *old_n
= n
;
18843 vec_alloc (n
, newlen
);
18844 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
18846 if (TREE_CODE (ce
->value
) == TREE_VEC
)
18848 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
18849 for (i
= 0; i
< len
; ++i
)
18850 CONSTRUCTOR_APPEND_ELT (n
, 0,
18851 TREE_VEC_ELT (ce
->value
, i
));
18854 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
18858 r
= build_constructor (init_list_type_node
, n
);
18859 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
18861 if (TREE_HAS_CONSTRUCTOR (t
))
18863 fcl_t cl
= fcl_functional
;
18864 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
18866 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
18869 TREE_TYPE (r
) = type
;
18875 tree operand_0
= TREE_OPERAND (t
, 0);
18876 if (TYPE_P (operand_0
))
18878 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
18879 RETURN (get_typeid (operand_0
, complain
));
18883 operand_0
= RECUR (operand_0
);
18884 RETURN (build_typeid (operand_0
, complain
));
18895 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
18896 /* ??? We're doing a subset of finish_id_expression here. */
18898 && !processing_template_decl
18899 && !cp_unevaluated_operand
18900 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
18901 && CP_DECL_THREAD_LOCAL_P (r
))
18903 if (tree wrap
= get_tls_wrapper_fn (r
))
18904 /* Replace an evaluated use of the thread_local variable with
18905 a call to its wrapper. */
18906 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
18908 else if (outer_automatic_var_p (r
))
18909 r
= process_outer_var_ref (r
, complain
);
18911 if (!TYPE_REF_P (TREE_TYPE (t
)))
18912 /* If the original type was a reference, we'll be wrapped in
18913 the appropriate INDIRECT_REF. */
18914 r
= convert_from_reference (r
);
18920 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18921 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18922 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
18925 case OFFSETOF_EXPR
:
18928 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
18929 in_decl
, /*function_p=*/false,
18930 /*integral_constant_expression_p=*/false);
18931 RETURN (finish_offsetof (object_ptr
,
18932 RECUR (TREE_OPERAND (t
, 0)),
18933 EXPR_LOCATION (t
)));
18936 case ADDRESSOF_EXPR
:
18937 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
18938 RECUR (TREE_OPERAND (t
, 0)), complain
));
18942 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
18943 complain
, in_decl
);
18945 tree type2
= TRAIT_EXPR_TYPE2 (t
);
18946 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
18947 type2
= RECUR (type2
);
18949 type2
= tsubst (type2
, args
, complain
, in_decl
);
18951 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
18956 tree old_stmt_expr
= cur_stmt_expr
;
18957 tree stmt_expr
= begin_stmt_expr ();
18959 cur_stmt_expr
= stmt_expr
;
18960 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
18961 integral_constant_expression_p
);
18962 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
18963 cur_stmt_expr
= old_stmt_expr
;
18965 /* If the resulting list of expression statement is empty,
18966 fold it further into void_node. */
18967 if (empty_expr_stmt_p (stmt_expr
))
18968 stmt_expr
= void_node
;
18970 RETURN (stmt_expr
);
18975 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
18977 RETURN (build_lambda_object (r
));
18981 /* We can get here for a constant initializer of non-dependent type.
18982 FIXME stop folding in cp_parser_initializer_clause. */
18984 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
18989 case TRANSACTION_EXPR
:
18990 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
18991 integral_constant_expression_p
));
18994 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
18996 case VEC_PERM_EXPR
:
18998 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18999 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19000 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19001 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19005 case REQUIRES_EXPR
:
19006 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19008 case NON_LVALUE_EXPR
:
19009 case VIEW_CONVERT_EXPR
:
19010 /* We should only see these for location wrapper nodes, or within
19011 instantiate_non_dependent_expr (when args is NULL_TREE). */
19012 gcc_assert (location_wrapper_p (t
) || args
== NULL_TREE
);
19013 if (location_wrapper_p (t
))
19014 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19015 EXPR_LOCATION (t
)));
19019 /* Handle Objective-C++ constructs, if appropriate. */
19022 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19023 in_decl
, /*function_p=*/false);
19027 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19033 input_location
= loc
;
19037 /* Verify that the instantiated ARGS are valid. For type arguments,
19038 make sure that the type's linkage is ok. For non-type arguments,
19039 make sure they are constants if they are integral or enumerations.
19040 Emit an error under control of COMPLAIN, and return TRUE on error. */
19043 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19045 if (dependent_template_arg_p (t
))
19047 if (ARGUMENT_PACK_P (t
))
19049 tree vec
= ARGUMENT_PACK_ARGS (t
);
19050 int len
= TREE_VEC_LENGTH (vec
);
19051 bool result
= false;
19054 for (i
= 0; i
< len
; ++i
)
19055 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19059 else if (TYPE_P (t
))
19061 /* [basic.link]: A name with no linkage (notably, the name
19062 of a class or enumeration declared in a local scope)
19063 shall not be used to declare an entity with linkage.
19064 This implies that names with no linkage cannot be used as
19067 DR 757 relaxes this restriction for C++0x. */
19068 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19069 : no_linkage_check (t
, /*relaxed_p=*/false));
19073 /* DR 488 makes use of a type with no linkage cause
19074 type deduction to fail. */
19075 if (complain
& tf_error
)
19077 if (TYPE_UNNAMED_P (nt
))
19078 error ("%qT is/uses unnamed type", t
);
19080 error ("template argument for %qD uses local type %qT",
19085 /* In order to avoid all sorts of complications, we do not
19086 allow variably-modified types as template arguments. */
19087 else if (variably_modified_type_p (t
, NULL_TREE
))
19089 if (complain
& tf_error
)
19090 error ("%qT is a variably modified type", t
);
19094 /* Class template and alias template arguments should be OK. */
19095 else if (DECL_TYPE_TEMPLATE_P (t
))
19097 /* A non-type argument of integral or enumerated type must be a
19099 else if (TREE_TYPE (t
)
19100 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19101 && !REFERENCE_REF_P (t
)
19102 && !TREE_CONSTANT (t
))
19104 if (complain
& tf_error
)
19105 error ("integral expression %qE is not constant", t
);
19112 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19114 int ix
, len
= DECL_NTPARMS (tmpl
);
19115 bool result
= false;
19117 for (ix
= 0; ix
!= len
; ix
++)
19119 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19122 if (result
&& (complain
& tf_error
))
19123 error (" trying to instantiate %qD", tmpl
);
19127 /* We're out of SFINAE context now, so generate diagnostics for the access
19128 errors we saw earlier when instantiating D from TMPL and ARGS. */
19131 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19133 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19134 tree type
= TREE_TYPE (pattern
);
19135 location_t loc
= input_location
;
19137 push_access_scope (d
);
19138 push_deferring_access_checks (dk_no_deferred
);
19139 input_location
= DECL_SOURCE_LOCATION (pattern
);
19140 tsubst (type
, args
, tf_warning_or_error
, d
);
19141 input_location
= loc
;
19142 pop_deferring_access_checks ();
19143 pop_access_scope (d
);
19146 /* Instantiate the indicated variable, function, or alias template TMPL with
19147 the template arguments in TARG_PTR. */
19150 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19152 tree targ_ptr
= orig_args
;
19156 bool access_ok
= true;
19158 if (tmpl
== error_mark_node
)
19159 return error_mark_node
;
19161 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19163 /* If this function is a clone, handle it specially. */
19164 if (DECL_CLONED_FUNCTION_P (tmpl
))
19169 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19170 DECL_CLONED_FUNCTION. */
19171 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19172 targ_ptr
, complain
);
19173 if (spec
== error_mark_node
)
19174 return error_mark_node
;
19176 /* Look for the clone. */
19177 FOR_EACH_CLONE (clone
, spec
)
19178 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19180 /* We should always have found the clone by now. */
19181 gcc_unreachable ();
19185 if (targ_ptr
== error_mark_node
)
19186 return error_mark_node
;
19188 /* Check to see if we already have this specialization. */
19189 gen_tmpl
= most_general_template (tmpl
);
19190 if (TMPL_ARGS_DEPTH (targ_ptr
)
19191 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19192 /* targ_ptr only has the innermost template args, so add the outer ones
19193 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19194 the case of a non-dependent call within a template definition). */
19195 targ_ptr
= (add_outermost_template_args
19196 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19199 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19200 but it doesn't seem to be on the hot path. */
19201 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19203 gcc_assert (tmpl
== gen_tmpl
19204 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19206 || fndecl
== NULL_TREE
);
19208 if (spec
!= NULL_TREE
)
19210 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19212 if (complain
& tf_error
)
19213 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19214 return error_mark_node
;
19219 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19221 return error_mark_node
;
19223 /* We are building a FUNCTION_DECL, during which the access of its
19224 parameters and return types have to be checked. However this
19225 FUNCTION_DECL which is the desired context for access checking
19226 is not built yet. We solve this chicken-and-egg problem by
19227 deferring all checks until we have the FUNCTION_DECL. */
19228 push_deferring_access_checks (dk_deferred
);
19230 /* Instantiation of the function happens in the context of the function
19231 template, not the context of the overload resolution we're doing. */
19232 push_to_top_level ();
19233 /* If there are dependent arguments, e.g. because we're doing partial
19234 ordering, make sure processing_template_decl stays set. */
19235 if (uses_template_parms (targ_ptr
))
19236 ++processing_template_decl
;
19237 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19239 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19240 complain
, gen_tmpl
, true);
19241 push_nested_class (ctx
);
19244 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19246 fndecl
= NULL_TREE
;
19247 if (VAR_P (pattern
))
19249 /* We need to determine if we're using a partial or explicit
19250 specialization now, because the type of the variable could be
19252 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19253 tree elt
= most_specialized_partial_spec (tid
, complain
);
19254 if (elt
== error_mark_node
)
19255 pattern
= error_mark_node
;
19258 tree partial_tmpl
= TREE_VALUE (elt
);
19259 tree partial_args
= TREE_PURPOSE (elt
);
19260 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19261 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19265 /* Substitute template parameters to obtain the specialization. */
19266 if (fndecl
== NULL_TREE
)
19267 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19268 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19269 pop_nested_class ();
19270 pop_from_top_level ();
19272 if (fndecl
== error_mark_node
)
19274 pop_deferring_access_checks ();
19275 return error_mark_node
;
19278 /* The DECL_TI_TEMPLATE should always be the immediate parent
19279 template, not the most general template. */
19280 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19281 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19283 /* Now we know the specialization, compute access previously
19284 deferred. Do no access control for inheriting constructors,
19285 as we already checked access for the inherited constructor. */
19286 if (!(flag_new_inheriting_ctors
19287 && DECL_INHERITED_CTOR (fndecl
)))
19289 push_access_scope (fndecl
);
19290 if (!perform_deferred_access_checks (complain
))
19292 pop_access_scope (fndecl
);
19294 pop_deferring_access_checks ();
19296 /* If we've just instantiated the main entry point for a function,
19297 instantiate all the alternate entry points as well. We do this
19298 by cloning the instantiation of the main entry point, not by
19299 instantiating the template clones. */
19300 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
19301 clone_function_decl (fndecl
, /*update_methods=*/false);
19305 if (!(complain
& tf_error
))
19307 /* Remember to reinstantiate when we're out of SFINAE so the user
19308 can see the errors. */
19309 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19311 return error_mark_node
;
19316 /* Wrapper for instantiate_template_1. */
19319 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19322 timevar_push (TV_TEMPLATE_INST
);
19323 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19324 timevar_pop (TV_TEMPLATE_INST
);
19328 /* Instantiate the alias template TMPL with ARGS. Also push a template
19329 instantiation level, which instantiate_template doesn't do because
19330 functions and variables have sufficient context established by the
19334 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19336 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19337 return error_mark_node
;
19338 if (!push_tinst_level (tmpl
, args
))
19339 return error_mark_node
;
19342 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19343 args
, tmpl
, complain
,
19344 /*require_all_args=*/true,
19345 /*use_default_args=*/true);
19347 tree r
= instantiate_template (tmpl
, args
, complain
);
19348 pop_tinst_level ();
19353 /* PARM is a template parameter pack for FN. Returns true iff
19354 PARM is used in a deducible way in the argument list of FN. */
19357 pack_deducible_p (tree parm
, tree fn
)
19359 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19360 for (; t
; t
= TREE_CHAIN (t
))
19362 tree type
= TREE_VALUE (t
);
19364 if (!PACK_EXPANSION_P (type
))
19366 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19367 packs
; packs
= TREE_CHAIN (packs
))
19368 if (template_args_equal (TREE_VALUE (packs
), parm
))
19370 /* The template parameter pack is used in a function parameter
19371 pack. If this is the end of the parameter list, the
19372 template parameter pack is deducible. */
19373 if (TREE_CHAIN (t
) == void_list_node
)
19376 /* Otherwise, not. Well, it could be deduced from
19377 a non-pack parameter, but doing so would end up with
19378 a deduction mismatch, so don't bother. */
19382 /* The template parameter pack isn't used in any function parameter
19383 packs, but it might be used deeper, e.g. tuple<Args...>. */
19387 /* Subroutine of fn_type_unification: check non-dependent parms for
19391 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
19392 tree fn
, unification_kind_t strict
, int flags
,
19393 struct conversion
**convs
, bool explain_p
)
19395 /* Non-constructor methods need to leave a conversion for 'this', which
19396 isn't included in nargs here. */
19397 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19398 && !DECL_CONSTRUCTOR_P (fn
));
19400 for (unsigned ia
= 0;
19401 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19403 tree parm
= TREE_VALUE (parms
);
19405 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19406 && (!TREE_CHAIN (parms
)
19407 || TREE_CHAIN (parms
) == void_list_node
))
19408 /* For a function parameter pack that occurs at the end of the
19409 parameter-declaration-list, the type A of each remaining
19410 argument of the call is compared with the type P of the
19411 declarator-id of the function parameter pack. */
19414 parms
= TREE_CHAIN (parms
);
19416 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19417 /* For a function parameter pack that does not occur at the
19418 end of the parameter-declaration-list, the type of the
19419 parameter pack is a non-deduced context. */
19422 if (!uses_template_parms (parm
))
19424 tree arg
= args
[ia
];
19425 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
19426 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
19428 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
19429 conv_p
, explain_p
))
19439 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19440 NARGS elements of the arguments that are being used when calling
19441 it. TARGS is a vector into which the deduced template arguments
19444 Returns either a FUNCTION_DECL for the matching specialization of FN or
19445 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19446 true, diagnostics will be printed to explain why it failed.
19448 If FN is a conversion operator, or we are trying to produce a specific
19449 specialization, RETURN_TYPE is the return type desired.
19451 The EXPLICIT_TARGS are explicit template arguments provided via a
19454 The parameter STRICT is one of:
19457 We are deducing arguments for a function call, as in
19458 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19459 deducing arguments for a call to the result of a conversion
19460 function template, as in [over.call.object].
19463 We are deducing arguments for a conversion function, as in
19464 [temp.deduct.conv].
19467 We are deducing arguments when doing an explicit instantiation
19468 as in [temp.explicit], when determining an explicit specialization
19469 as in [temp.expl.spec], or when taking the address of a function
19470 template, as in [temp.deduct.funcaddr]. */
19473 fn_type_unification (tree fn
,
19474 tree explicit_targs
,
19477 unsigned int nargs
,
19479 unification_kind_t strict
,
19481 struct conversion
**convs
,
19487 tree decl
= NULL_TREE
;
19488 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19490 static int deduction_depth
;
19493 if (flag_new_inheriting_ctors
)
19494 fn
= strip_inheriting_ctors (fn
);
19496 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
19497 tree r
= error_mark_node
;
19499 tree full_targs
= targs
;
19500 if (TMPL_ARGS_DEPTH (targs
)
19501 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
19502 full_targs
= (add_outermost_template_args
19503 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
19507 complain
|= tf_decltype
;
19509 /* In C++0x, it's possible to have a function template whose type depends
19510 on itself recursively. This is most obvious with decltype, but can also
19511 occur with enumeration scope (c++/48969). So we need to catch infinite
19512 recursion and reject the substitution at deduction time; this function
19513 will return error_mark_node for any repeated substitution.
19515 This also catches excessive recursion such as when f<N> depends on
19516 f<N-1> across all integers, and returns error_mark_node for all the
19517 substitutions back up to the initial one.
19519 This is, of course, not reentrant. */
19520 if (excessive_deduction_depth
)
19521 return error_mark_node
;
19524 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19526 fntype
= TREE_TYPE (fn
);
19527 if (explicit_targs
)
19531 The specified template arguments must match the template
19532 parameters in kind (i.e., type, nontype, template), and there
19533 must not be more arguments than there are parameters;
19534 otherwise type deduction fails.
19536 Nontype arguments must match the types of the corresponding
19537 nontype template parameters, or must be convertible to the
19538 types of the corresponding nontype parameters as specified in
19539 _temp.arg.nontype_, otherwise type deduction fails.
19541 All references in the function type of the function template
19542 to the corresponding template parameters are replaced by the
19543 specified template argument values. If a substitution in a
19544 template parameter or in the function type of the function
19545 template results in an invalid type, type deduction fails. */
19546 int i
, len
= TREE_VEC_LENGTH (tparms
);
19547 location_t loc
= input_location
;
19548 bool incomplete
= false;
19550 if (explicit_targs
== error_mark_node
)
19553 if (TMPL_ARGS_DEPTH (explicit_targs
)
19554 < TMPL_ARGS_DEPTH (full_targs
))
19555 explicit_targs
= add_outermost_template_args (full_targs
,
19558 /* Adjust any explicit template arguments before entering the
19559 substitution context. */
19561 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
19563 /*require_all_args=*/false,
19564 /*use_default_args=*/false));
19565 if (explicit_targs
== error_mark_node
)
19568 /* Substitute the explicit args into the function type. This is
19569 necessary so that, for instance, explicitly declared function
19570 arguments can match null pointed constants. If we were given
19571 an incomplete set of explicit args, we must not do semantic
19572 processing during substitution as we could create partial
19574 for (i
= 0; i
< len
; i
++)
19576 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
19577 bool parameter_pack
= false;
19578 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
19580 /* Dig out the actual parm. */
19581 if (TREE_CODE (parm
) == TYPE_DECL
19582 || TREE_CODE (parm
) == TEMPLATE_DECL
)
19584 parm
= TREE_TYPE (parm
);
19585 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
19587 else if (TREE_CODE (parm
) == PARM_DECL
)
19589 parm
= DECL_INITIAL (parm
);
19590 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
19593 if (!parameter_pack
&& targ
== NULL_TREE
)
19594 /* No explicit argument for this template parameter. */
19597 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
19599 /* Mark the argument pack as "incomplete". We could
19600 still deduce more arguments during unification.
19601 We remove this mark in type_unification_real. */
19604 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
19605 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
19606 = ARGUMENT_PACK_ARGS (targ
);
19609 /* We have some incomplete argument packs. */
19614 if (!push_tinst_level (fn
, explicit_targs
))
19616 excessive_deduction_depth
= true;
19619 processing_template_decl
+= incomplete
;
19620 input_location
= DECL_SOURCE_LOCATION (fn
);
19621 /* Ignore any access checks; we'll see them again in
19622 instantiate_template and they might have the wrong
19623 access path at this point. */
19624 push_deferring_access_checks (dk_deferred
);
19625 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
19626 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
19627 pop_deferring_access_checks ();
19628 input_location
= loc
;
19629 processing_template_decl
-= incomplete
;
19630 pop_tinst_level ();
19632 if (fntype
== error_mark_node
)
19635 /* Place the explicitly specified arguments in TARGS. */
19636 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
19637 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
19638 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
19641 /* Never do unification on the 'this' parameter. */
19642 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
19644 if (return_type
&& strict
== DEDUCE_CALL
)
19646 /* We're deducing for a call to the result of a template conversion
19647 function. The parms we really want are in return_type. */
19648 if (INDIRECT_TYPE_P (return_type
))
19649 return_type
= TREE_TYPE (return_type
);
19650 parms
= TYPE_ARG_TYPES (return_type
);
19652 else if (return_type
)
19656 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
19657 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
19658 new_args
[0] = return_type
;
19659 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
19664 /* We allow incomplete unification without an error message here
19665 because the standard doesn't seem to explicitly prohibit it. Our
19666 callers must be ready to deal with unification failures in any
19669 /* If we aren't explaining yet, push tinst context so we can see where
19670 any errors (e.g. from class instantiations triggered by instantiation
19671 of default template arguments) come from. If we are explaining, this
19672 context is redundant. */
19673 if (!explain_p
&& !push_tinst_level (fn
, targs
))
19675 excessive_deduction_depth
= true;
19679 /* type_unification_real will pass back any access checks from default
19680 template argument substitution. */
19681 vec
<deferred_access_check
, va_gc
> *checks
;
19684 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19685 full_targs
, parms
, args
, nargs
, /*subr=*/0,
19686 strict
, &checks
, explain_p
);
19688 pop_tinst_level ();
19692 /* Now that we have bindings for all of the template arguments,
19693 ensure that the arguments deduced for the template template
19694 parameters have compatible template parameter lists. We cannot
19695 check this property before we have deduced all template
19696 arguments, because the template parameter types of a template
19697 template parameter might depend on prior template parameters
19698 deduced after the template template parameter. The following
19699 ill-formed example illustrates this issue:
19701 template<typename T, template<T> class C> void f(C<5>, T);
19703 template<int N> struct X {};
19706 f(X<5>(), 5l); // error: template argument deduction fails
19709 The template parameter list of 'C' depends on the template type
19710 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19711 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19712 time that we deduce 'C'. */
19713 if (!template_template_parm_bindings_ok_p
19714 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
19716 unify_inconsistent_template_template_parameters (explain_p
);
19720 /* DR 1391: All parameters have args, now check non-dependent parms for
19722 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
19726 /* All is well so far. Now, check:
19730 When all template arguments have been deduced, all uses of
19731 template parameters in nondeduced contexts are replaced with
19732 the corresponding deduced argument values. If the
19733 substitution results in an invalid type, as described above,
19734 type deduction fails. */
19735 if (!push_tinst_level (fn
, targs
))
19737 excessive_deduction_depth
= true;
19741 /* Also collect access checks from the instantiation. */
19742 reopen_deferring_access_checks (checks
);
19744 decl
= instantiate_template (fn
, targs
, complain
);
19746 checks
= get_deferred_access_checks ();
19747 pop_deferring_access_checks ();
19749 pop_tinst_level ();
19751 if (decl
== error_mark_node
)
19754 /* Now perform any access checks encountered during substitution. */
19755 push_access_scope (decl
);
19756 ok
= perform_access_checks (checks
, complain
);
19757 pop_access_scope (decl
);
19761 /* If we're looking for an exact match, check that what we got
19762 is indeed an exact match. It might not be if some template
19763 parameters are used in non-deduced contexts. But don't check
19764 for an exact match if we have dependent template arguments;
19765 in that case we're doing partial ordering, and we already know
19766 that we have two candidates that will provide the actual type. */
19767 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
19769 tree substed
= TREE_TYPE (decl
);
19773 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
19775 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
19776 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
19777 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
19779 unify_type_mismatch (explain_p
, args
[i
],
19780 TREE_VALUE (sarg
));
19785 /* After doing deduction with the inherited constructor, actually return an
19786 instantiation of the inheriting constructor. */
19788 decl
= instantiate_template (orig_fn
, targs
, complain
);
19794 if (excessive_deduction_depth
)
19796 if (deduction_depth
== 0)
19797 /* Reset once we're all the way out. */
19798 excessive_deduction_depth
= false;
19804 /* Adjust types before performing type deduction, as described in
19805 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19806 sections are symmetric. PARM is the type of a function parameter
19807 or the return type of the conversion function. ARG is the type of
19808 the argument passed to the call, or the type of the value
19809 initialized with the result of the conversion function.
19810 ARG_EXPR is the original argument expression, which may be null. */
19813 maybe_adjust_types_for_deduction (unification_kind_t strict
,
19826 /* Swap PARM and ARG throughout the remainder of this
19827 function; the handling is precisely symmetric since PARM
19828 will initialize ARG rather than vice versa. */
19829 std::swap (parm
, arg
);
19833 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19834 too, but here handle it by stripping the reference from PARM
19835 rather than by adding it to ARG. */
19836 if (TYPE_REF_P (*parm
)
19837 && TYPE_REF_IS_RVALUE (*parm
)
19838 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19839 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19840 && TYPE_REF_P (*arg
)
19841 && !TYPE_REF_IS_RVALUE (*arg
))
19842 *parm
= TREE_TYPE (*parm
);
19843 /* Nothing else to do in this case. */
19847 gcc_unreachable ();
19850 if (!TYPE_REF_P (*parm
))
19852 /* [temp.deduct.call]
19854 If P is not a reference type:
19856 --If A is an array type, the pointer type produced by the
19857 array-to-pointer standard conversion (_conv.array_) is
19858 used in place of A for type deduction; otherwise,
19860 --If A is a function type, the pointer type produced by
19861 the function-to-pointer standard conversion
19862 (_conv.func_) is used in place of A for type deduction;
19865 --If A is a cv-qualified type, the top level
19866 cv-qualifiers of A's type are ignored for type
19868 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
19869 *arg
= build_pointer_type (TREE_TYPE (*arg
));
19870 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
19871 *arg
= build_pointer_type (*arg
);
19873 *arg
= TYPE_MAIN_VARIANT (*arg
);
19876 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19877 reference to a cv-unqualified template parameter that does not represent a
19878 template parameter of a class template (during class template argument
19879 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19880 an lvalue, the type "lvalue reference to A" is used in place of A for type
19882 if (TYPE_REF_P (*parm
)
19883 && TYPE_REF_IS_RVALUE (*parm
)
19884 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
19885 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
19886 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
19887 && (arg_expr
? lvalue_p (arg_expr
)
19888 /* try_one_overload doesn't provide an arg_expr, but
19889 functions are always lvalues. */
19890 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
19891 *arg
= build_reference_type (*arg
);
19893 /* [temp.deduct.call]
19895 If P is a cv-qualified type, the top level cv-qualifiers
19896 of P's type are ignored for type deduction. If P is a
19897 reference type, the type referred to by P is used for
19899 *parm
= TYPE_MAIN_VARIANT (*parm
);
19900 if (TYPE_REF_P (*parm
))
19902 *parm
= TREE_TYPE (*parm
);
19903 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19906 /* DR 322. For conversion deduction, remove a reference type on parm
19907 too (which has been swapped into ARG). */
19908 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
19909 *arg
= TREE_TYPE (*arg
);
19914 /* Subroutine of fn_type_unification. PARM is a function parameter of a
19915 template which doesn't contain any deducible template parameters; check if
19916 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19917 unify_one_argument. */
19920 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
19921 int flags
, struct conversion
**conv_p
,
19927 type
= TREE_TYPE (arg
);
19931 if (same_type_p (parm
, type
))
19932 return unify_success (explain_p
);
19934 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19935 if (strict
== DEDUCE_CONV
)
19937 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
19938 return unify_success (explain_p
);
19940 else if (strict
!= DEDUCE_EXACT
)
19943 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
19945 /* Avoid recalculating this in add_function_candidate. */
19947 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
19949 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
19951 return unify_success (explain_p
);
19954 if (strict
== DEDUCE_EXACT
)
19955 return unify_type_mismatch (explain_p
, parm
, arg
);
19957 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
19960 static bool uses_deducible_template_parms (tree type
);
19962 /* Returns true iff the expression EXPR is one from which a template
19963 argument can be deduced. In other words, if it's an undecorated
19964 use of a template non-type parameter. */
19967 deducible_expression (tree expr
)
19969 /* Strip implicit conversions. */
19970 while (CONVERT_EXPR_P (expr
))
19971 expr
= TREE_OPERAND (expr
, 0);
19972 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
19975 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19976 deducible way; that is, if it has a max value of <PARM> - 1. */
19979 deducible_array_bound (tree domain
)
19981 if (domain
== NULL_TREE
)
19984 tree max
= TYPE_MAX_VALUE (domain
);
19985 if (TREE_CODE (max
) != MINUS_EXPR
)
19988 return deducible_expression (TREE_OPERAND (max
, 0));
19991 /* Returns true iff the template arguments ARGS use a template parameter
19992 in a deducible way. */
19995 deducible_template_args (tree args
)
19997 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20000 tree elt
= TREE_VEC_ELT (args
, i
);
20001 if (ARGUMENT_PACK_P (elt
))
20002 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20005 if (PACK_EXPANSION_P (elt
))
20006 elt
= PACK_EXPANSION_PATTERN (elt
);
20007 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20009 else if (TYPE_P (elt
))
20010 deducible
= uses_deducible_template_parms (elt
);
20012 deducible
= deducible_expression (elt
);
20020 /* Returns true iff TYPE contains any deducible references to template
20021 parameters, as per 14.8.2.5. */
20024 uses_deducible_template_parms (tree type
)
20026 if (PACK_EXPANSION_P (type
))
20027 type
= PACK_EXPANSION_PATTERN (type
);
20034 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20035 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20041 if (INDIRECT_TYPE_P (type
))
20042 return uses_deducible_template_parms (TREE_TYPE (type
));
20044 /* T[integer-constant ]
20046 if (TREE_CODE (type
) == ARRAY_TYPE
)
20047 return (uses_deducible_template_parms (TREE_TYPE (type
))
20048 || deducible_array_bound (TYPE_DOMAIN (type
)));
20060 if (TYPE_PTRMEM_P (type
))
20061 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20062 || (uses_deducible_template_parms
20063 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20065 /* template-name <T> (where template-name refers to a class template)
20066 template-name <i> (where template-name refers to a class template) */
20067 if (CLASS_TYPE_P (type
)
20068 && CLASSTYPE_TEMPLATE_INFO (type
)
20069 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20070 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20071 (CLASSTYPE_TI_ARGS (type
)));
20076 if (TREE_CODE (type
) == FUNCTION_TYPE
20077 || TREE_CODE (type
) == METHOD_TYPE
)
20079 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20081 tree parm
= TYPE_ARG_TYPES (type
);
20082 if (TREE_CODE (type
) == METHOD_TYPE
)
20083 parm
= TREE_CHAIN (parm
);
20084 for (; parm
; parm
= TREE_CHAIN (parm
))
20085 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20092 /* Subroutine of type_unification_real and unify_pack_expansion to
20093 handle unification of a single P/A pair. Parameters are as
20094 for those functions. */
20097 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20098 int subr
, unification_kind_t strict
,
20101 tree arg_expr
= NULL_TREE
;
20104 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20105 return unify_invalid (explain_p
);
20106 if (arg
== unknown_type_node
)
20107 /* We can't deduce anything from this, but we might get all the
20108 template args from other function args. */
20109 return unify_success (explain_p
);
20111 /* Implicit conversions (Clause 4) will be performed on a function
20112 argument to convert it to the type of the corresponding function
20113 parameter if the parameter type contains no template-parameters that
20114 participate in template argument deduction. */
20115 if (strict
!= DEDUCE_EXACT
20116 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20117 /* For function parameters with no deducible template parameters,
20118 just return. We'll check non-dependent conversions later. */
20119 return unify_success (explain_p
);
20124 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20125 | UNIFY_ALLOW_MORE_CV_QUAL
20126 | UNIFY_ALLOW_DERIVED
);
20130 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20134 arg_strict
= UNIFY_ALLOW_NONE
;
20138 gcc_unreachable ();
20141 /* We only do these transformations if this is the top-level
20142 parameter_type_list in a call or declaration matching; in other
20143 situations (nested function declarators, template argument lists) we
20144 won't be comparing a type to an expression, and we don't do any type
20150 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20151 if (type_unknown_p (arg
))
20153 /* [temp.deduct.type] A template-argument can be
20154 deduced from a pointer to function or pointer
20155 to member function argument if the set of
20156 overloaded functions does not contain function
20157 templates and at most one of a set of
20158 overloaded functions provides a unique
20160 resolve_overloaded_unification (tparms
, targs
, parm
,
20162 arg_strict
, explain_p
);
20163 /* If a unique match was not found, this is a
20164 non-deduced context, so we still succeed. */
20165 return unify_success (explain_p
);
20169 arg
= unlowered_expr_type (arg
);
20170 if (arg
== error_mark_node
)
20171 return unify_invalid (explain_p
);
20175 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20178 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20179 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20180 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20182 /* For deduction from an init-list we need the actual list. */
20183 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20185 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20188 /* for_each_template_parm callback that always returns 0. */
20191 zero_r (tree
, void *)
20196 /* for_each_template_parm any_fn callback to handle deduction of a template
20197 type argument from the type of an array bound. */
20200 array_deduction_r (tree t
, void *data
)
20202 tree_pair_p d
= (tree_pair_p
)data
;
20203 tree
&tparms
= d
->purpose
;
20204 tree
&targs
= d
->value
;
20206 if (TREE_CODE (t
) == ARRAY_TYPE
)
20207 if (tree dom
= TYPE_DOMAIN (t
))
20208 if (tree max
= TYPE_MAX_VALUE (dom
))
20210 if (TREE_CODE (max
) == MINUS_EXPR
)
20211 max
= TREE_OPERAND (max
, 0);
20212 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20213 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20214 UNIFY_ALLOW_NONE
, /*explain*/false);
20217 /* Keep walking. */
20221 /* Try to deduce any not-yet-deduced template type arguments from the type of
20222 an array bound. This is handled separately from unify because 14.8.2.5 says
20223 "The type of a type parameter is only deduced from an array bound if it is
20224 not otherwise deduced." */
20227 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20229 tree_pair_s data
= { tparms
, targs
};
20230 hash_set
<tree
> visited
;
20231 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20232 /*nondeduced*/false, array_deduction_r
);
20235 /* Most parms like fn_type_unification.
20237 If SUBR is 1, we're being called recursively (to unify the
20238 arguments of a function or method parameter of a function
20241 CHECKS is a pointer to a vector of access checks encountered while
20242 substituting default template arguments. */
20245 type_unification_real (tree tparms
,
20249 unsigned int xnargs
,
20251 unification_kind_t strict
,
20252 vec
<deferred_access_check
, va_gc
> **checks
,
20257 int ntparms
= TREE_VEC_LENGTH (tparms
);
20258 int saw_undeduced
= 0;
20261 unsigned int nargs
;
20264 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20265 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20266 gcc_assert (ntparms
> 0);
20268 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20270 /* Reset the number of non-defaulted template arguments contained
20272 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20280 while (parms
&& parms
!= void_list_node
20283 parm
= TREE_VALUE (parms
);
20285 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20286 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20287 /* For a function parameter pack that occurs at the end of the
20288 parameter-declaration-list, the type A of each remaining
20289 argument of the call is compared with the type P of the
20290 declarator-id of the function parameter pack. */
20293 parms
= TREE_CHAIN (parms
);
20295 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20296 /* For a function parameter pack that does not occur at the
20297 end of the parameter-declaration-list, the type of the
20298 parameter pack is a non-deduced context. */
20304 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20310 && parms
!= void_list_node
20311 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20313 /* Unify the remaining arguments with the pack expansion type. */
20315 tree parmvec
= make_tree_vec (1);
20317 /* Allocate a TREE_VEC and copy in all of the arguments */
20318 argvec
= make_tree_vec (nargs
- ia
);
20319 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20320 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20322 /* Copy the parameter into parmvec. */
20323 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20324 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20325 /*subr=*/subr
, explain_p
))
20328 /* Advance to the end of the list of parameters. */
20329 parms
= TREE_CHAIN (parms
);
20332 /* Fail if we've reached the end of the parm list, and more args
20333 are present, and the parm list isn't variadic. */
20334 if (ia
< nargs
&& parms
== void_list_node
)
20335 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20336 /* Fail if parms are left and they don't have default values and
20337 they aren't all deduced as empty packs (c++/57397). This is
20338 consistent with sufficient_parms_p. */
20339 if (parms
&& parms
!= void_list_node
20340 && TREE_PURPOSE (parms
) == NULL_TREE
)
20342 unsigned int count
= nargs
;
20347 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20350 p
= TREE_CHAIN (p
);
20352 while (p
&& p
!= void_list_node
);
20353 if (count
!= nargs
)
20354 return unify_too_few_arguments (explain_p
, ia
, count
,
20360 tsubst_flags_t complain
= (explain_p
20361 ? tf_warning_or_error
20363 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20365 for (i
= 0; i
< ntparms
; i
++)
20367 tree targ
= TREE_VEC_ELT (targs
, i
);
20368 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20370 /* Clear the "incomplete" flags on all argument packs now so that
20371 substituting them into later default arguments works. */
20372 if (targ
&& ARGUMENT_PACK_P (targ
))
20374 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
20375 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
20378 if (targ
|| tparm
== error_mark_node
)
20380 tparm
= TREE_VALUE (tparm
);
20382 if (TREE_CODE (tparm
) == TYPE_DECL
20383 && !tried_array_deduction
)
20385 try_array_deduction (tparms
, targs
, xparms
);
20386 tried_array_deduction
= true;
20387 if (TREE_VEC_ELT (targs
, i
))
20391 /* If this is an undeduced nontype parameter that depends on
20392 a type parameter, try another pass; its type may have been
20393 deduced from a later argument than the one from which
20394 this parameter can be deduced. */
20395 if (TREE_CODE (tparm
) == PARM_DECL
20396 && uses_template_parms (TREE_TYPE (tparm
))
20397 && saw_undeduced
< 2)
20403 /* Core issue #226 (C++0x) [temp.deduct]:
20405 If a template argument has not been deduced, its
20406 default template argument, if any, is used.
20408 When we are in C++98 mode, TREE_PURPOSE will either
20409 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20410 to explicitly check cxx_dialect here. */
20411 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20412 /* OK, there is a default argument. Wait until after the
20413 conversion check to do substitution. */
20416 /* If the type parameter is a parameter pack, then it will
20417 be deduced to an empty parameter pack. */
20418 if (template_parameter_pack_p (tparm
))
20422 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20424 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20425 TREE_CONSTANT (arg
) = 1;
20428 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20430 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
20432 TREE_VEC_ELT (targs
, i
) = arg
;
20436 return unify_parameter_deduction_failure (explain_p
, tparm
);
20439 /* Now substitute into the default template arguments. */
20440 for (i
= 0; i
< ntparms
; i
++)
20442 tree targ
= TREE_VEC_ELT (targs
, i
);
20443 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20445 if (targ
|| tparm
== error_mark_node
)
20447 tree parm
= TREE_VALUE (tparm
);
20448 tree arg
= TREE_PURPOSE (tparm
);
20449 reopen_deferring_access_checks (*checks
);
20450 location_t save_loc
= input_location
;
20452 input_location
= DECL_SOURCE_LOCATION (parm
);
20454 if (saw_undeduced
== 1
20455 && TREE_CODE (parm
) == PARM_DECL
20456 && uses_template_parms (TREE_TYPE (parm
)))
20458 /* The type of this non-type parameter depends on undeduced
20459 parameters. Don't try to use its default argument yet,
20460 since we might deduce an argument for it on the next pass,
20461 but do check whether the arguments we already have cause
20462 substitution failure, so that that happens before we try
20463 later default arguments (78489). */
20464 ++processing_template_decl
;
20465 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
20467 --processing_template_decl
;
20468 if (type
== error_mark_node
)
20469 arg
= error_mark_node
;
20475 tree substed
= NULL_TREE
;
20476 if (saw_undeduced
== 1 && processing_template_decl
== 0)
20478 /* First instatiate in template context, in case we still
20479 depend on undeduced template parameters. */
20480 ++processing_template_decl
;
20481 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20483 --processing_template_decl
;
20484 if (substed
!= error_mark_node
20485 && !uses_template_parms (substed
))
20486 /* We replaced all the tparms, substitute again out of
20487 template context. */
20488 substed
= NULL_TREE
;
20491 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20494 if (!uses_template_parms (substed
))
20495 arg
= convert_template_argument (parm
, substed
, full_targs
,
20496 complain
, i
, NULL_TREE
);
20497 else if (saw_undeduced
== 1)
20500 arg
= error_mark_node
;
20503 input_location
= save_loc
;
20504 *checks
= get_deferred_access_checks ();
20505 pop_deferring_access_checks ();
20507 if (arg
== error_mark_node
)
20511 TREE_VEC_ELT (targs
, i
) = arg
;
20512 /* The position of the first default template argument,
20513 is also the number of non-defaulted arguments in TARGS.
20515 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20516 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
20520 if (saw_undeduced
++ == 1)
20524 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20525 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
20527 return unify_success (explain_p
);
20530 /* Subroutine of type_unification_real. Args are like the variables
20531 at the call site. ARG is an overloaded function (or template-id);
20532 we try deducing template args from each of the overloads, and if
20533 only one succeeds, we go with that. Modifies TARGS and returns
20534 true on success. */
20537 resolve_overloaded_unification (tree tparms
,
20541 unification_kind_t strict
,
20545 tree tempargs
= copy_node (targs
);
20547 tree goodfn
= NULL_TREE
;
20550 if (TREE_CODE (arg
) == ADDR_EXPR
)
20552 arg
= TREE_OPERAND (arg
, 0);
20558 if (TREE_CODE (arg
) == COMPONENT_REF
)
20559 /* Handle `&x' where `x' is some static or non-static member
20561 arg
= TREE_OPERAND (arg
, 1);
20563 if (TREE_CODE (arg
) == OFFSET_REF
)
20564 arg
= TREE_OPERAND (arg
, 1);
20566 /* Strip baselink information. */
20567 if (BASELINK_P (arg
))
20568 arg
= BASELINK_FUNCTIONS (arg
);
20570 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
20572 /* If we got some explicit template args, we need to plug them into
20573 the affected templates before we try to unify, in case the
20574 explicit args will completely resolve the templates in question. */
20577 tree expl_subargs
= TREE_OPERAND (arg
, 1);
20578 arg
= TREE_OPERAND (arg
, 0);
20580 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20583 tree subargs
, elem
;
20585 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20588 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20589 expl_subargs
, NULL_TREE
, tf_none
,
20590 /*require_all_args=*/true,
20591 /*use_default_args=*/true);
20592 if (subargs
!= error_mark_node
20593 && !any_dependent_template_arguments_p (subargs
))
20595 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
20596 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
20597 elem
, strict
, sub_strict
, addr_p
, explain_p
)
20598 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
20607 /* If no templates (or more than one) are fully resolved by the
20608 explicit arguments, this template-id is a non-deduced context; it
20609 could still be OK if we deduce all template arguments for the
20610 enclosing call through other arguments. */
20614 else if (TREE_CODE (arg
) != OVERLOAD
20615 && TREE_CODE (arg
) != FUNCTION_DECL
)
20616 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20617 -- but the deduction does not succeed because the expression is
20618 not just the function on its own. */
20621 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20624 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
20625 strict
, sub_strict
, addr_p
, explain_p
)
20626 && (!goodfn
|| !decls_match (goodfn
, fn
)))
20633 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20634 to function or pointer to member function argument if the set of
20635 overloaded functions does not contain function templates and at most
20636 one of a set of overloaded functions provides a unique match.
20638 So if we found multiple possibilities, we return success but don't
20639 deduce anything. */
20643 int i
= TREE_VEC_LENGTH (targs
);
20645 if (TREE_VEC_ELT (tempargs
, i
))
20647 tree old
= TREE_VEC_ELT (targs
, i
);
20648 tree new_
= TREE_VEC_ELT (tempargs
, i
);
20649 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
20650 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
20651 /* Don't forget explicit template arguments in a pack. */
20652 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
20653 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
20654 TREE_VEC_ELT (targs
, i
) = new_
;
20663 /* Core DR 115: In contexts where deduction is done and fails, or in
20664 contexts where deduction is not done, if a template argument list is
20665 specified and it, along with any default template arguments, identifies
20666 a single function template specialization, then the template-id is an
20667 lvalue for the function template specialization. */
20670 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
20672 tree expr
, offset
, baselink
;
20675 if (!type_unknown_p (orig_expr
))
20680 offset
= NULL_TREE
;
20681 baselink
= NULL_TREE
;
20683 if (TREE_CODE (expr
) == ADDR_EXPR
)
20685 expr
= TREE_OPERAND (expr
, 0);
20688 if (TREE_CODE (expr
) == OFFSET_REF
)
20691 expr
= TREE_OPERAND (expr
, 1);
20693 if (BASELINK_P (expr
))
20696 expr
= BASELINK_FUNCTIONS (expr
);
20699 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
20702 tree goodfn
= NULL_TREE
;
20704 /* If we got some explicit template args, we need to plug them into
20705 the affected templates before we try to unify, in case the
20706 explicit args will completely resolve the templates in question. */
20708 tree expl_subargs
= TREE_OPERAND (expr
, 1);
20709 tree arg
= TREE_OPERAND (expr
, 0);
20710 tree badfn
= NULL_TREE
;
20711 tree badargs
= NULL_TREE
;
20713 for (lkp_iterator
iter (arg
); iter
; ++iter
)
20716 tree subargs
, elem
;
20718 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
20721 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20722 expl_subargs
, NULL_TREE
, tf_none
,
20723 /*require_all_args=*/true,
20724 /*use_default_args=*/true);
20725 if (subargs
!= error_mark_node
20726 && !any_dependent_template_arguments_p (subargs
))
20728 elem
= instantiate_template (fn
, subargs
, tf_none
);
20729 if (elem
== error_mark_node
)
20734 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
20743 mark_used (goodfn
);
20746 expr
= build_baselink (BASELINK_BINFO (baselink
),
20747 BASELINK_ACCESS_BINFO (baselink
),
20748 expr
, BASELINK_OPTYPE (baselink
));
20752 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
20753 expr
= build_offset_ref (base
, expr
, addr
, complain
);
20756 expr
= cp_build_addr_expr (expr
, complain
);
20759 else if (good
== 0 && badargs
&& (complain
& tf_error
))
20760 /* There were no good options and at least one bad one, so let the
20761 user know what the problem is. */
20762 instantiate_template (badfn
, badargs
, complain
);
20767 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20768 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20769 different overloads deduce different arguments for a given parm.
20770 ADDR_P is true if the expression for which deduction is being
20771 performed was of the form "& fn" rather than simply "fn".
20773 Returns 1 on success. */
20776 try_one_overload (tree tparms
,
20781 unification_kind_t strict
,
20790 if (arg
== error_mark_node
)
20793 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20794 to function or pointer to member function argument if the set of
20795 overloaded functions does not contain function templates and at most
20796 one of a set of overloaded functions provides a unique match.
20798 So if this is a template, just return success. */
20800 if (uses_template_parms (arg
))
20803 if (TREE_CODE (arg
) == METHOD_TYPE
)
20804 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
20806 arg
= build_pointer_type (arg
);
20808 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
20810 /* We don't copy orig_targs for this because if we have already deduced
20811 some template args from previous args, unify would complain when we
20812 try to deduce a template parameter for the same argument, even though
20813 there isn't really a conflict. */
20814 nargs
= TREE_VEC_LENGTH (targs
);
20815 tempargs
= make_tree_vec (nargs
);
20817 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
20820 /* First make sure we didn't deduce anything that conflicts with
20821 explicitly specified args. */
20822 for (i
= nargs
; i
--; )
20824 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20825 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
20829 else if (uses_template_parms (elt
))
20830 /* Since we're unifying against ourselves, we will fill in
20831 template args used in the function parm list with our own
20832 template parms. Discard them. */
20833 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
20834 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
20836 /* Check that the argument at each index of the deduced argument pack
20837 is equivalent to the corresponding explicitly specified argument.
20838 We may have deduced more arguments than were explicitly specified,
20841 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20842 that's wrong if we deduce the same argument pack from multiple
20843 function arguments: it's only incomplete the first time. */
20845 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
20846 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
20848 if (TREE_VEC_LENGTH (deduced_pack
)
20849 < TREE_VEC_LENGTH (explicit_pack
))
20852 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
20853 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
20854 TREE_VEC_ELT (deduced_pack
, j
)))
20857 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
20861 for (i
= nargs
; i
--; )
20863 tree elt
= TREE_VEC_ELT (tempargs
, i
);
20866 TREE_VEC_ELT (targs
, i
) = elt
;
20872 /* PARM is a template class (perhaps with unbound template
20873 parameters). ARG is a fully instantiated type. If ARG can be
20874 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20875 TARGS are as for unify. */
20878 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
20881 tree copy_of_targs
;
20883 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20885 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20886 /* Matches anything. */;
20887 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
20888 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
20891 /* We need to make a new template argument vector for the call to
20892 unify. If we used TARGS, we'd clutter it up with the result of
20893 the attempted unification, even if this class didn't work out.
20894 We also don't want to commit ourselves to all the unifications
20895 we've already done, since unification is supposed to be done on
20896 an argument-by-argument basis. In other words, consider the
20897 following pathological case:
20899 template <int I, int J, int K>
20902 template <int I, int J>
20903 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20905 template <int I, int J, int K>
20906 void f(S<I, J, K>, S<I, I, I>);
20915 Now, by the time we consider the unification involving `s2', we
20916 already know that we must have `f<0, 0, 0>'. But, even though
20917 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20918 because there are two ways to unify base classes of S<0, 1, 2>
20919 with S<I, I, I>. If we kept the already deduced knowledge, we
20920 would reject the possibility I=1. */
20921 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
20923 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20925 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
20930 /* If unification failed, we're done. */
20931 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
20932 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
20938 /* Given a template type PARM and a class type ARG, find the unique
20939 base type in ARG that is an instance of PARM. We do not examine
20940 ARG itself; only its base-classes. If there is not exactly one
20941 appropriate base class, return NULL_TREE. PARM may be the type of
20942 a partial specialization, as well as a plain template type. Used
20945 static enum template_base_result
20946 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
20947 bool explain_p
, tree
*result
)
20949 tree rval
= NULL_TREE
;
20952 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
20954 binfo
= TYPE_BINFO (complete_type (arg
));
20957 /* The type could not be completed. */
20958 *result
= NULL_TREE
;
20959 return tbr_incomplete_type
;
20962 /* Walk in inheritance graph order. The search order is not
20963 important, and this avoids multiple walks of virtual bases. */
20964 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
20966 tree r
= try_class_unification (tparms
, targs
, parm
,
20967 BINFO_TYPE (binfo
), explain_p
);
20971 /* If there is more than one satisfactory baseclass, then:
20975 If they yield more than one possible deduced A, the type
20979 if (rval
&& !same_type_p (r
, rval
))
20981 *result
= NULL_TREE
;
20982 return tbr_ambiguous_baseclass
;
20990 return tbr_success
;
20993 /* Returns the level of DECL, which declares a template parameter. */
20996 template_decl_level (tree decl
)
20998 switch (TREE_CODE (decl
))
21001 case TEMPLATE_DECL
:
21002 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21005 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21008 gcc_unreachable ();
21013 /* Decide whether ARG can be unified with PARM, considering only the
21014 cv-qualifiers of each type, given STRICT as documented for unify.
21015 Returns nonzero iff the unification is OK on that basis. */
21018 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21020 int arg_quals
= cp_type_quals (arg
);
21021 int parm_quals
= cp_type_quals (parm
);
21023 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21024 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21026 /* Although a CVR qualifier is ignored when being applied to a
21027 substituted template parameter ([8.3.2]/1 for example), that
21028 does not allow us to unify "const T" with "int&" because both
21029 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21030 It is ok when we're allowing additional CV qualifiers
21031 at the outer level [14.8.2.1]/3,1st bullet. */
21032 if ((TYPE_REF_P (arg
)
21033 || TREE_CODE (arg
) == FUNCTION_TYPE
21034 || TREE_CODE (arg
) == METHOD_TYPE
)
21035 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21038 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21039 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21043 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21044 && (arg_quals
& parm_quals
) != parm_quals
)
21047 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21048 && (parm_quals
& arg_quals
) != arg_quals
)
21054 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21056 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21058 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21059 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21060 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21062 *index
= TEMPLATE_TYPE_IDX (parm
);
21063 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21067 *index
= TEMPLATE_PARM_IDX (parm
);
21068 *level
= TEMPLATE_PARM_LEVEL (parm
);
21072 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21074 if (unify (TP, TA, P, A, S, EP)) \
21078 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21079 expansion at the end of PACKED_PARMS. Returns 0 if the type
21080 deduction succeeds, 1 otherwise. STRICT is the same as in
21081 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21082 function call argument list. We'll need to adjust the arguments to make them
21083 types. SUBR tells us if this is from a recursive call to
21084 type_unification_real, or for comparing two template argument
21088 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21089 tree packed_args
, unification_kind_t strict
,
21090 bool subr
, bool explain_p
)
21093 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21094 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21095 tree pack
, packs
= NULL_TREE
;
21096 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21098 /* Add in any args remembered from an earlier partial instantiation. */
21099 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21100 int levels
= TMPL_ARGS_DEPTH (targs
);
21102 packed_args
= expand_template_argument_pack (packed_args
);
21104 int len
= TREE_VEC_LENGTH (packed_args
);
21106 /* Determine the parameter packs we will be deducing from the
21107 pattern, and record their current deductions. */
21108 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21109 pack
; pack
= TREE_CHAIN (pack
))
21111 tree parm_pack
= TREE_VALUE (pack
);
21114 /* Only template parameter packs can be deduced, not e.g. function
21115 parameter packs or __bases or __integer_pack. */
21116 if (!TEMPLATE_PARM_P (parm_pack
))
21119 /* Determine the index and level of this parameter pack. */
21120 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21121 if (level
< levels
)
21124 /* Keep track of the parameter packs and their corresponding
21126 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21127 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21130 /* Loop through all of the arguments that have not yet been
21131 unified and unify each with the pattern. */
21132 for (i
= start
; i
< len
; i
++)
21135 bool any_explicit
= false;
21136 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21138 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21139 or the element of its argument pack at the current index if
21140 this argument was explicitly specified. */
21141 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21145 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21148 if (TREE_VALUE (pack
)
21149 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21150 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21152 any_explicit
= true;
21153 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21155 TMPL_ARG (targs
, level
, idx
) = arg
;
21158 /* If we had explicit template arguments, substitute them into the
21159 pattern before deduction. */
21162 /* Some arguments might still be unspecified or dependent. */
21164 ++processing_template_decl
;
21165 dependent
= any_dependent_template_arguments_p (targs
);
21167 --processing_template_decl
;
21168 parm
= tsubst (pattern
, targs
,
21169 explain_p
? tf_warning_or_error
: tf_none
,
21172 --processing_template_decl
;
21173 if (parm
== error_mark_node
)
21179 /* Unify the pattern with the current argument. */
21180 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21184 /* For each parameter pack, collect the deduced value. */
21185 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21188 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21190 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21191 TMPL_ARG (targs
, level
, idx
);
21195 /* Verify that the results of unification with the parameter packs
21196 produce results consistent with what we've seen before, and make
21197 the deduced argument packs available. */
21198 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21200 tree old_pack
= TREE_VALUE (pack
);
21201 tree new_args
= TREE_TYPE (pack
);
21202 int i
, len
= TREE_VEC_LENGTH (new_args
);
21204 bool nondeduced_p
= false;
21206 /* By default keep the original deduced argument pack.
21207 If necessary, more specific code is going to update the
21208 resulting deduced argument later down in this function. */
21209 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21210 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21212 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21213 actually deduce anything. */
21214 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21215 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21216 nondeduced_p
= true;
21220 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21222 /* If we had fewer function args than explicit template args,
21223 just use the explicits. */
21224 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21225 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21226 if (len
< explicit_len
)
21227 new_args
= explicit_args
;
21233 /* Build the deduced *_ARGUMENT_PACK. */
21234 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21236 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21237 TREE_CONSTANT (result
) = 1;
21240 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21242 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21244 /* Note the deduced argument packs for this parameter
21246 TMPL_ARG (targs
, level
, idx
) = result
;
21248 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21249 && (ARGUMENT_PACK_ARGS (old_pack
)
21250 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21252 /* We only had the explicitly-provided arguments before, but
21253 now we have a complete set of arguments. */
21254 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21256 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21257 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21258 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21262 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21263 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21265 if (!comp_template_args (old_args
, new_args
,
21266 &bad_old_arg
, &bad_new_arg
))
21267 /* Inconsistent unification of this parameter pack. */
21268 return unify_parameter_pack_inconsistent (explain_p
,
21274 return unify_success (explain_p
);
21277 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21278 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21279 parameters and return value are as for unify. */
21282 unify_array_domain (tree tparms
, tree targs
,
21283 tree parm_dom
, tree arg_dom
,
21291 /* Our representation of array types uses "N - 1" as the
21292 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21293 not an integer constant. We cannot unify arbitrarily
21294 complex expressions, so we eliminate the MINUS_EXPRs
21296 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21297 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21300 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21301 parm_max
= TREE_OPERAND (parm_max
, 0);
21303 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21304 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21307 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21308 trying to unify the type of a variable with the type
21309 of a template parameter. For example:
21311 template <unsigned int N>
21312 void f (char (&) [N]);
21319 Here, the type of the ARG will be "int [g(i)]", and
21320 may be a SAVE_EXPR, etc. */
21321 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21322 return unify_vla_arg (explain_p
, arg_dom
);
21323 arg_max
= TREE_OPERAND (arg_max
, 0);
21326 /* If only one of the bounds used a MINUS_EXPR, compensate
21327 by adding one to the other bound. */
21328 if (parm_cst
&& !arg_cst
)
21329 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21333 else if (arg_cst
&& !parm_cst
)
21334 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21339 return unify (tparms
, targs
, parm_max
, arg_max
,
21340 UNIFY_ALLOW_INTEGER
, explain_p
);
21343 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21345 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21350 if (PACK_EXPANSION_P (t
))
21351 t
= PACK_EXPANSION_PATTERN (t
);
21352 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
21353 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
21354 || DECL_TYPE_TEMPLATE_P (t
))
21356 else if (TYPE_P (t
))
21362 /* Deduce the value of template parameters. TPARMS is the (innermost)
21363 set of template parameters to a template. TARGS is the bindings
21364 for those template parameters, as determined thus far; TARGS may
21365 include template arguments for outer levels of template parameters
21366 as well. PARM is a parameter to a template function, or a
21367 subcomponent of that parameter; ARG is the corresponding argument.
21368 This function attempts to match PARM with ARG in a manner
21369 consistent with the existing assignments in TARGS. If more values
21370 are deduced, then TARGS is updated.
21372 Returns 0 if the type deduction succeeds, 1 otherwise. The
21373 parameter STRICT is a bitwise or of the following flags:
21376 Require an exact match between PARM and ARG.
21377 UNIFY_ALLOW_MORE_CV_QUAL:
21378 Allow the deduced ARG to be more cv-qualified (by qualification
21379 conversion) than ARG.
21380 UNIFY_ALLOW_LESS_CV_QUAL:
21381 Allow the deduced ARG to be less cv-qualified than ARG.
21382 UNIFY_ALLOW_DERIVED:
21383 Allow the deduced ARG to be a template base class of ARG,
21384 or a pointer to a template base class of the type pointed to by
21386 UNIFY_ALLOW_INTEGER:
21387 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21388 case for more information.
21389 UNIFY_ALLOW_OUTER_LEVEL:
21390 This is the outermost level of a deduction. Used to determine validity
21391 of qualification conversions. A valid qualification conversion must
21392 have const qualified pointers leading up to the inner type which
21393 requires additional CV quals, except at the outer level, where const
21394 is not required [conv.qual]. It would be normal to set this flag in
21395 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21396 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21397 This is the outermost level of a deduction, and PARM can be more CV
21398 qualified at this point.
21399 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21400 This is the outermost level of a deduction, and PARM can be less CV
21401 qualified at this point. */
21404 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
21410 int strict_in
= strict
;
21411 tsubst_flags_t complain
= (explain_p
21412 ? tf_warning_or_error
21415 /* I don't think this will do the right thing with respect to types.
21416 But the only case I've seen it in so far has been array bounds, where
21417 signedness is the only information lost, and I think that will be
21419 while (CONVERT_EXPR_P (parm
))
21420 parm
= TREE_OPERAND (parm
, 0);
21422 if (arg
== error_mark_node
)
21423 return unify_invalid (explain_p
);
21424 if (arg
== unknown_type_node
21425 || arg
== init_list_type_node
)
21426 /* We can't deduce anything from this, but we might get all the
21427 template args from other function args. */
21428 return unify_success (explain_p
);
21430 if (parm
== any_targ_node
|| arg
== any_targ_node
)
21431 return unify_success (explain_p
);
21433 /* If PARM uses template parameters, then we can't bail out here,
21434 even if ARG == PARM, since we won't record unifications for the
21435 template parameters. We might need them if we're trying to
21436 figure out which of two things is more specialized. */
21437 if (arg
== parm
&& !uses_template_parms (parm
))
21438 return unify_success (explain_p
);
21440 /* Handle init lists early, so the rest of the function can assume
21441 we're dealing with a type. */
21442 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
21446 tree orig_parm
= parm
;
21448 /* Replace T with std::initializer_list<T> for deduction. */
21449 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21450 && flag_deduce_init_list
)
21451 parm
= listify (parm
);
21453 if (!is_std_init_list (parm
)
21454 && TREE_CODE (parm
) != ARRAY_TYPE
)
21455 /* We can only deduce from an initializer list argument if the
21456 parameter is std::initializer_list or an array; otherwise this
21457 is a non-deduced context. */
21458 return unify_success (explain_p
);
21460 if (TREE_CODE (parm
) == ARRAY_TYPE
)
21461 elttype
= TREE_TYPE (parm
);
21464 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
21465 /* Deduction is defined in terms of a single type, so just punt
21466 on the (bizarre) std::initializer_list<T...>. */
21467 if (PACK_EXPANSION_P (elttype
))
21468 return unify_success (explain_p
);
21471 if (strict
!= DEDUCE_EXACT
21472 && TYPE_P (elttype
)
21473 && !uses_deducible_template_parms (elttype
))
21474 /* If ELTTYPE has no deducible template parms, skip deduction from
21475 the list elements. */;
21477 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
21479 int elt_strict
= strict
;
21481 if (elt
== error_mark_node
)
21482 return unify_invalid (explain_p
);
21484 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
21486 tree type
= TREE_TYPE (elt
);
21487 if (type
== error_mark_node
)
21488 return unify_invalid (explain_p
);
21489 /* It should only be possible to get here for a call. */
21490 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
21491 elt_strict
|= maybe_adjust_types_for_deduction
21492 (DEDUCE_CALL
, &elttype
, &type
, elt
);
21496 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
21500 if (TREE_CODE (parm
) == ARRAY_TYPE
21501 && deducible_array_bound (TYPE_DOMAIN (parm
)))
21503 /* Also deduce from the length of the initializer list. */
21504 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
21505 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
21506 if (idx
== error_mark_node
)
21507 return unify_invalid (explain_p
);
21508 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21512 /* If the std::initializer_list<T> deduction worked, replace the
21513 deduced A with std::initializer_list<A>. */
21514 if (orig_parm
!= parm
)
21516 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
21517 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21518 targ
= listify (targ
);
21519 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
21521 return unify_success (explain_p
);
21524 /* If parm and arg aren't the same kind of thing (template, type, or
21525 expression), fail early. */
21526 if (pa_kind (parm
) != pa_kind (arg
))
21527 return unify_invalid (explain_p
);
21529 /* Immediately reject some pairs that won't unify because of
21530 cv-qualification mismatches. */
21531 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21533 /* It is the elements of the array which hold the cv quals of an array
21534 type, and the elements might be template type parms. We'll check
21535 when we recurse. */
21536 && TREE_CODE (arg
) != ARRAY_TYPE
21537 /* We check the cv-qualifiers when unifying with template type
21538 parameters below. We want to allow ARG `const T' to unify with
21539 PARM `T' for example, when computing which of two templates
21540 is more specialized, for example. */
21541 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
21542 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
21543 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21545 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
21546 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
21547 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
21548 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
21549 strict
&= ~UNIFY_ALLOW_DERIVED
;
21550 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
21551 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
21553 switch (TREE_CODE (parm
))
21555 case TYPENAME_TYPE
:
21557 case UNBOUND_CLASS_TEMPLATE
:
21558 /* In a type which contains a nested-name-specifier, template
21559 argument values cannot be deduced for template parameters used
21560 within the nested-name-specifier. */
21561 return unify_success (explain_p
);
21563 case TEMPLATE_TYPE_PARM
:
21564 case TEMPLATE_TEMPLATE_PARM
:
21565 case BOUND_TEMPLATE_TEMPLATE_PARM
:
21566 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21567 if (error_operand_p (tparm
))
21568 return unify_invalid (explain_p
);
21570 if (TEMPLATE_TYPE_LEVEL (parm
)
21571 != template_decl_level (tparm
))
21572 /* The PARM is not one we're trying to unify. Just check
21573 to see if it matches ARG. */
21575 if (TREE_CODE (arg
) == TREE_CODE (parm
)
21576 && (is_auto (parm
) ? is_auto (arg
)
21577 : same_type_p (parm
, arg
)))
21578 return unify_success (explain_p
);
21580 return unify_type_mismatch (explain_p
, parm
, arg
);
21582 idx
= TEMPLATE_TYPE_IDX (parm
);
21583 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21584 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
21585 if (error_operand_p (tparm
))
21586 return unify_invalid (explain_p
);
21588 /* Check for mixed types and values. */
21589 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21590 && TREE_CODE (tparm
) != TYPE_DECL
)
21591 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21592 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
21593 gcc_unreachable ();
21595 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21597 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
21598 && CLASS_TYPE_P (arg
))
21600 /* First try to match ARG directly. */
21601 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
21605 /* Otherwise, look for a suitable base of ARG, as below. */
21606 enum template_base_result r
;
21607 r
= get_template_base (tparms
, targs
, parm
, arg
,
21610 return unify_no_common_base (explain_p
, r
, parm
, arg
);
21614 /* ARG must be constructed from a template class or a template
21615 template parameter. */
21616 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
21617 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21618 return unify_template_deduction_failure (explain_p
, parm
, arg
);
21620 /* Deduce arguments T, i from TT<T> or TT<i>. */
21621 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
21624 arg
= TYPE_TI_TEMPLATE (arg
);
21626 /* Fall through to deduce template name. */
21629 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21630 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21632 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21634 /* Simple cases: Value already set, does match or doesn't. */
21635 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
21636 return unify_success (explain_p
);
21638 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21642 /* If PARM is `const T' and ARG is only `int', we don't have
21643 a match unless we are allowing additional qualification.
21644 If ARG is `const int' and PARM is just `T' that's OK;
21645 that binds `const int' to `T'. */
21646 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
21648 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
21650 /* Consider the case where ARG is `const volatile int' and
21651 PARM is `const T'. Then, T should be `volatile int'. */
21652 arg
= cp_build_qualified_type_real
21653 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
21654 if (arg
== error_mark_node
)
21655 return unify_invalid (explain_p
);
21657 /* Simple cases: Value already set, does match or doesn't. */
21658 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
21659 return unify_success (explain_p
);
21661 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
21663 /* Make sure that ARG is not a variable-sized array. (Note
21664 that were talking about variable-sized arrays (like
21665 `int[n]'), rather than arrays of unknown size (like
21666 `int[]').) We'll get very confused by such a type since
21667 the bound of the array is not constant, and therefore
21668 not mangleable. Besides, such types are not allowed in
21669 ISO C++, so we can do as we please here. We do allow
21670 them for 'auto' deduction, since that isn't ABI-exposed. */
21671 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
21672 return unify_vla_arg (explain_p
, arg
);
21674 /* Strip typedefs as in convert_template_argument. */
21675 arg
= canonicalize_type_argument (arg
, tf_none
);
21678 /* If ARG is a parameter pack or an expansion, we cannot unify
21679 against it unless PARM is also a parameter pack. */
21680 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21681 && !template_parameter_pack_p (parm
))
21682 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21684 /* If the argument deduction results is a METHOD_TYPE,
21685 then there is a problem.
21686 METHOD_TYPE doesn't map to any real C++ type the result of
21687 the deduction can not be of that type. */
21688 if (TREE_CODE (arg
) == METHOD_TYPE
)
21689 return unify_method_type_error (explain_p
, arg
);
21691 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21692 return unify_success (explain_p
);
21694 case TEMPLATE_PARM_INDEX
:
21695 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
21696 if (error_operand_p (tparm
))
21697 return unify_invalid (explain_p
);
21699 if (TEMPLATE_PARM_LEVEL (parm
)
21700 != template_decl_level (tparm
))
21702 /* The PARM is not one we're trying to unify. Just check
21703 to see if it matches ARG. */
21704 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
21705 && cp_tree_equal (parm
, arg
));
21707 unify_expression_unequal (explain_p
, parm
, arg
);
21711 idx
= TEMPLATE_PARM_IDX (parm
);
21712 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
21716 if ((strict
& UNIFY_ALLOW_INTEGER
)
21717 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
21718 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
21719 /* We're deducing from an array bound, the type doesn't matter. */
21720 arg
= fold_convert (TREE_TYPE (targ
), arg
);
21721 int x
= !cp_tree_equal (targ
, arg
);
21723 unify_inconsistency (explain_p
, parm
, targ
, arg
);
21727 /* [temp.deduct.type] If, in the declaration of a function template
21728 with a non-type template-parameter, the non-type
21729 template-parameter is used in an expression in the function
21730 parameter-list and, if the corresponding template-argument is
21731 deduced, the template-argument type shall match the type of the
21732 template-parameter exactly, except that a template-argument
21733 deduced from an array bound may be of any integral type.
21734 The non-type parameter might use already deduced type parameters. */
21735 tparm
= TREE_TYPE (parm
);
21736 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
21737 /* We don't have enough levels of args to do any substitution. This
21738 can happen in the context of -fnew-ttp-matching. */;
21741 ++processing_template_decl
;
21742 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
21743 --processing_template_decl
;
21745 if (tree a
= type_uses_auto (tparm
))
21747 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
21748 if (tparm
== error_mark_node
)
21753 if (!TREE_TYPE (arg
))
21754 /* Template-parameter dependent expression. Just accept it for now.
21755 It will later be processed in convert_template_argument. */
21757 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
21758 non_reference (tparm
)))
21760 else if ((strict
& UNIFY_ALLOW_INTEGER
)
21761 && CP_INTEGRAL_TYPE_P (tparm
))
21762 /* Convert the ARG to the type of PARM; the deduced non-type
21763 template argument must exactly match the types of the
21764 corresponding parameter. */
21765 arg
= fold (build_nop (tparm
, arg
));
21766 else if (uses_template_parms (tparm
))
21768 /* We haven't deduced the type of this parameter yet. */
21769 if (cxx_dialect
>= cxx17
21770 /* We deduce from array bounds in try_array_deduction. */
21771 && !(strict
& UNIFY_ALLOW_INTEGER
))
21773 /* Deduce it from the non-type argument. */
21774 tree atype
= TREE_TYPE (arg
);
21775 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21777 UNIFY_ALLOW_NONE
, explain_p
);
21780 /* Try again later. */
21781 return unify_success (explain_p
);
21784 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
21786 /* If ARG is a parameter pack or an expansion, we cannot unify
21787 against it unless PARM is also a parameter pack. */
21788 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
21789 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
21790 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
21793 bool removed_attr
= false;
21794 arg
= strip_typedefs_expr (arg
, &removed_attr
);
21796 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
21797 return unify_success (explain_p
);
21801 /* A pointer-to-member constant can be unified only with
21802 another constant. */
21803 if (TREE_CODE (arg
) != PTRMEM_CST
)
21804 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
21806 /* Just unify the class member. It would be useless (and possibly
21807 wrong, depending on the strict flags) to unify also
21808 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21809 arg refer to the same variable, even if through different
21810 classes. For instance:
21812 struct A { int x; };
21815 Unification of &A::x and &B::x must succeed. */
21816 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
21817 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
21822 if (!TYPE_PTR_P (arg
))
21823 return unify_type_mismatch (explain_p
, parm
, arg
);
21825 /* [temp.deduct.call]
21827 A can be another pointer or pointer to member type that can
21828 be converted to the deduced A via a qualification
21829 conversion (_conv.qual_).
21831 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21832 This will allow for additional cv-qualification of the
21833 pointed-to types if appropriate. */
21835 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
21836 /* The derived-to-base conversion only persists through one
21837 level of pointers. */
21838 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
21840 return unify (tparms
, targs
, TREE_TYPE (parm
),
21841 TREE_TYPE (arg
), strict
, explain_p
);
21844 case REFERENCE_TYPE
:
21845 if (!TYPE_REF_P (arg
))
21846 return unify_type_mismatch (explain_p
, parm
, arg
);
21847 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21848 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21851 if (TREE_CODE (arg
) != ARRAY_TYPE
)
21852 return unify_type_mismatch (explain_p
, parm
, arg
);
21853 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
21854 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
21855 return unify_type_mismatch (explain_p
, parm
, arg
);
21856 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21857 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
21858 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
21859 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
21860 TYPE_DOMAIN (arg
), explain_p
);
21861 return unify_success (explain_p
);
21868 case ENUMERAL_TYPE
:
21871 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21872 return unify_type_mismatch (explain_p
, parm
, arg
);
21874 /* We have already checked cv-qualification at the top of the
21876 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
21877 return unify_type_mismatch (explain_p
, parm
, arg
);
21879 /* As far as unification is concerned, this wins. Later checks
21880 will invalidate it if necessary. */
21881 return unify_success (explain_p
);
21883 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21884 /* Type INTEGER_CST can come from ordinary constant template args. */
21886 while (CONVERT_EXPR_P (arg
))
21887 arg
= TREE_OPERAND (arg
, 0);
21889 if (TREE_CODE (arg
) != INTEGER_CST
)
21890 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21891 return (tree_int_cst_equal (parm
, arg
)
21892 ? unify_success (explain_p
)
21893 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
21897 int i
, len
, argslen
;
21898 int parm_variadic_p
= 0;
21900 if (TREE_CODE (arg
) != TREE_VEC
)
21901 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21903 len
= TREE_VEC_LENGTH (parm
);
21904 argslen
= TREE_VEC_LENGTH (arg
);
21906 /* Check for pack expansions in the parameters. */
21907 for (i
= 0; i
< len
; ++i
)
21909 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
21912 /* We can unify against something with a trailing
21914 parm_variadic_p
= 1;
21916 /* [temp.deduct.type]/9: If the template argument list of
21917 P contains a pack expansion that is not the last
21918 template argument, the entire template argument list
21919 is a non-deduced context. */
21920 return unify_success (explain_p
);
21924 /* If we don't have enough arguments to satisfy the parameters
21925 (not counting the pack expression at the end), or we have
21926 too many arguments for a parameter list that doesn't end in
21927 a pack expression, we can't unify. */
21928 if (parm_variadic_p
21929 ? argslen
< len
- parm_variadic_p
21931 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
21933 /* Unify all of the parameters that precede the (optional)
21934 pack expression. */
21935 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
21937 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
21938 TREE_VEC_ELT (parm
, i
),
21939 TREE_VEC_ELT (arg
, i
),
21940 UNIFY_ALLOW_NONE
, explain_p
);
21942 if (parm_variadic_p
)
21943 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
21945 /*subr=*/true, explain_p
);
21946 return unify_success (explain_p
);
21951 if (TREE_CODE (arg
) != TREE_CODE (parm
))
21952 return unify_type_mismatch (explain_p
, parm
, arg
);
21954 if (TYPE_PTRMEMFUNC_P (parm
))
21956 if (!TYPE_PTRMEMFUNC_P (arg
))
21957 return unify_type_mismatch (explain_p
, parm
, arg
);
21959 return unify (tparms
, targs
,
21960 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
21961 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
21962 strict
, explain_p
);
21964 else if (TYPE_PTRMEMFUNC_P (arg
))
21965 return unify_type_mismatch (explain_p
, parm
, arg
);
21967 if (CLASSTYPE_TEMPLATE_INFO (parm
))
21969 tree t
= NULL_TREE
;
21971 if (strict_in
& UNIFY_ALLOW_DERIVED
)
21973 /* First, we try to unify the PARM and ARG directly. */
21974 t
= try_class_unification (tparms
, targs
,
21975 parm
, arg
, explain_p
);
21979 /* Fallback to the special case allowed in
21980 [temp.deduct.call]:
21982 If P is a class, and P has the form
21983 template-id, then A can be a derived class of
21984 the deduced A. Likewise, if P is a pointer to
21985 a class of the form template-id, A can be a
21986 pointer to a derived class pointed to by the
21988 enum template_base_result r
;
21989 r
= get_template_base (tparms
, targs
, parm
, arg
,
21994 /* Don't give the derived diagnostic if we're
21995 already dealing with the same template. */
21997 = (CLASSTYPE_TEMPLATE_INFO (arg
)
21998 && (CLASSTYPE_TI_TEMPLATE (parm
)
21999 == CLASSTYPE_TI_TEMPLATE (arg
)));
22000 return unify_no_common_base (explain_p
&& !same_template
,
22005 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22006 && (CLASSTYPE_TI_TEMPLATE (parm
)
22007 == CLASSTYPE_TI_TEMPLATE (arg
)))
22008 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22009 Then, we should unify `int' and `U'. */
22012 /* There's no chance of unification succeeding. */
22013 return unify_type_mismatch (explain_p
, parm
, arg
);
22015 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22016 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22018 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22019 return unify_type_mismatch (explain_p
, parm
, arg
);
22020 return unify_success (explain_p
);
22023 case FUNCTION_TYPE
:
22025 unsigned int nargs
;
22030 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22031 return unify_type_mismatch (explain_p
, parm
, arg
);
22033 /* CV qualifications for methods can never be deduced, they must
22034 match exactly. We need to check them explicitly here,
22035 because type_unification_real treats them as any other
22036 cv-qualified parameter. */
22037 if (TREE_CODE (parm
) == METHOD_TYPE
22038 && (!check_cv_quals_for_unify
22040 class_of_this_parm (arg
),
22041 class_of_this_parm (parm
))))
22042 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22043 if (TREE_CODE (arg
) == FUNCTION_TYPE
22044 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22045 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22046 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22047 return unify_type_mismatch (explain_p
, parm
, arg
);
22049 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22050 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22052 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22053 args
= XALLOCAVEC (tree
, nargs
);
22054 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22055 a
!= NULL_TREE
&& a
!= void_list_node
;
22056 a
= TREE_CHAIN (a
), ++i
)
22057 args
[i
] = TREE_VALUE (a
);
22060 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22061 args
, nargs
, 1, DEDUCE_EXACT
,
22065 if (flag_noexcept_type
)
22067 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22068 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22069 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22070 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22071 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22072 && uses_template_parms (TREE_PURPOSE (pspec
)))
22073 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22074 TREE_PURPOSE (aspec
),
22075 UNIFY_ALLOW_NONE
, explain_p
);
22076 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22077 return unify_type_mismatch (explain_p
, parm
, arg
);
22084 /* Unify a pointer to member with a pointer to member function, which
22085 deduces the type of the member as a function type. */
22086 if (TYPE_PTRMEMFUNC_P (arg
))
22088 /* Check top-level cv qualifiers */
22089 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22090 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22092 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22093 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22094 UNIFY_ALLOW_NONE
, explain_p
);
22096 /* Determine the type of the function we are unifying against. */
22097 tree fntype
= static_fn_type (arg
);
22099 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22102 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22103 return unify_type_mismatch (explain_p
, parm
, arg
);
22104 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22105 TYPE_OFFSET_BASETYPE (arg
),
22106 UNIFY_ALLOW_NONE
, explain_p
);
22107 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22108 strict
, explain_p
);
22111 if (DECL_TEMPLATE_PARM_P (parm
))
22112 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22113 if (arg
!= scalar_constant_value (parm
))
22114 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22115 return unify_success (explain_p
);
22118 case TEMPLATE_DECL
:
22119 /* Matched cases are handled by the ARG == PARM test above. */
22120 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22123 /* We might get a variable as a non-type template argument in parm if the
22124 corresponding parameter is type-dependent. Make any necessary
22125 adjustments based on whether arg is a reference. */
22126 if (CONSTANT_CLASS_P (arg
))
22127 parm
= fold_non_dependent_expr (parm
, complain
);
22128 else if (REFERENCE_REF_P (arg
))
22130 tree sub
= TREE_OPERAND (arg
, 0);
22132 if (TREE_CODE (sub
) == ADDR_EXPR
)
22133 arg
= TREE_OPERAND (sub
, 0);
22135 /* Now use the normal expression code to check whether they match. */
22138 case TYPE_ARGUMENT_PACK
:
22139 case NONTYPE_ARGUMENT_PACK
:
22140 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22141 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22144 case DECLTYPE_TYPE
:
22145 case UNDERLYING_TYPE
:
22146 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22147 or UNDERLYING_TYPE nodes. */
22148 return unify_success (explain_p
);
22151 /* Unification fails if we hit an error node. */
22152 return unify_invalid (explain_p
);
22155 if (REFERENCE_REF_P (parm
))
22157 bool pexp
= PACK_EXPANSION_P (arg
);
22159 arg
= PACK_EXPANSION_PATTERN (arg
);
22160 if (REFERENCE_REF_P (arg
))
22161 arg
= TREE_OPERAND (arg
, 0);
22163 arg
= make_pack_expansion (arg
, complain
);
22164 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22165 strict
, explain_p
);
22170 /* An unresolved overload is a nondeduced context. */
22171 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22172 return unify_success (explain_p
);
22173 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
22175 /* We must be looking at an expression. This can happen with
22179 void foo(S<I>, S<I + 2>);
22181 This is a "nondeduced context":
22185 The nondeduced contexts are:
22187 --A type that is a template-id in which one or more of
22188 the template-arguments is an expression that references
22189 a template-parameter.
22191 In these cases, we assume deduction succeeded, but don't
22192 actually infer any unifications. */
22194 if (!uses_template_parms (parm
)
22195 && !template_args_equal (parm
, arg
))
22196 return unify_expression_unequal (explain_p
, parm
, arg
);
22198 return unify_success (explain_p
);
22201 #undef RECUR_AND_CHECK_FAILURE
22203 /* Note that DECL can be defined in this translation unit, if
22207 mark_definable (tree decl
)
22210 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22211 FOR_EACH_CLONE (clone
, decl
)
22212 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22215 /* Called if RESULT is explicitly instantiated, or is a member of an
22216 explicitly instantiated class. */
22219 mark_decl_instantiated (tree result
, int extern_p
)
22221 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22223 /* If this entity has already been written out, it's too late to
22224 make any modifications. */
22225 if (TREE_ASM_WRITTEN (result
))
22228 /* For anonymous namespace we don't need to do anything. */
22229 if (decl_anon_ns_mem_p (result
))
22231 gcc_assert (!TREE_PUBLIC (result
));
22235 if (TREE_CODE (result
) != FUNCTION_DECL
)
22236 /* The TREE_PUBLIC flag for function declarations will have been
22237 set correctly by tsubst. */
22238 TREE_PUBLIC (result
) = 1;
22240 /* This might have been set by an earlier implicit instantiation. */
22241 DECL_COMDAT (result
) = 0;
22244 DECL_NOT_REALLY_EXTERN (result
) = 0;
22247 mark_definable (result
);
22248 mark_needed (result
);
22249 /* Always make artificials weak. */
22250 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22251 comdat_linkage (result
);
22252 /* For WIN32 we also want to put explicit instantiations in
22253 linkonce sections. */
22254 else if (TREE_PUBLIC (result
))
22255 maybe_make_one_only (result
);
22256 if (TREE_CODE (result
) == FUNCTION_DECL
22257 && DECL_TEMPLATE_INSTANTIATED (result
))
22258 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22259 since start_preparsed_function wouldn't have if we had an earlier
22260 extern explicit instantiation. */
22261 DECL_EXTERNAL (result
) = 0;
22264 /* If EXTERN_P, then this function will not be emitted -- unless
22265 followed by an explicit instantiation, at which point its linkage
22266 will be adjusted. If !EXTERN_P, then this function will be
22267 emitted here. In neither circumstance do we want
22268 import_export_decl to adjust the linkage. */
22269 DECL_INTERFACE_KNOWN (result
) = 1;
22272 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22273 important template arguments. If any are missing, we check whether
22274 they're important by using error_mark_node for substituting into any
22275 args that were used for partial ordering (the ones between ARGS and END)
22276 and seeing if it bubbles up. */
22279 check_undeduced_parms (tree targs
, tree args
, tree end
)
22281 bool found
= false;
22283 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22284 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22287 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22291 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22292 if (substed
== error_mark_node
)
22298 /* Given two function templates PAT1 and PAT2, return:
22300 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22301 -1 if PAT2 is more specialized than PAT1.
22302 0 if neither is more specialized.
22304 LEN indicates the number of parameters we should consider
22305 (defaulted parameters should not be considered).
22307 The 1998 std underspecified function template partial ordering, and
22308 DR214 addresses the issue. We take pairs of arguments, one from
22309 each of the templates, and deduce them against each other. One of
22310 the templates will be more specialized if all the *other*
22311 template's arguments deduce against its arguments and at least one
22312 of its arguments *does* *not* deduce against the other template's
22313 corresponding argument. Deduction is done as for class templates.
22314 The arguments used in deduction have reference and top level cv
22315 qualifiers removed. Iff both arguments were originally reference
22316 types *and* deduction succeeds in both directions, an lvalue reference
22317 wins against an rvalue reference and otherwise the template
22318 with the more cv-qualified argument wins for that pairing (if
22319 neither is more cv-qualified, they both are equal). Unlike regular
22320 deduction, after all the arguments have been deduced in this way,
22321 we do *not* verify the deduced template argument values can be
22322 substituted into non-deduced contexts.
22324 The logic can be a bit confusing here, because we look at deduce1 and
22325 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22326 can find template arguments for pat1 to make arg1 look like arg2, that
22327 means that arg2 is at least as specialized as arg1. */
22330 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22332 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22333 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22334 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22335 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22336 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22337 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22338 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22339 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22340 tree origs1
, origs2
;
22341 bool lose1
= false;
22342 bool lose2
= false;
22344 /* Remove the this parameter from non-static member functions. If
22345 one is a non-static member function and the other is not a static
22346 member function, remove the first parameter from that function
22347 also. This situation occurs for operator functions where we
22348 locate both a member function (with this pointer) and non-member
22349 operator (with explicit first operand). */
22350 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
22352 len
--; /* LEN is the number of significant arguments for DECL1 */
22353 args1
= TREE_CHAIN (args1
);
22354 if (!DECL_STATIC_FUNCTION_P (decl2
))
22355 args2
= TREE_CHAIN (args2
);
22357 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
22359 args2
= TREE_CHAIN (args2
);
22360 if (!DECL_STATIC_FUNCTION_P (decl1
))
22363 args1
= TREE_CHAIN (args1
);
22367 /* If only one is a conversion operator, they are unordered. */
22368 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
22371 /* Consider the return type for a conversion function */
22372 if (DECL_CONV_FN_P (decl1
))
22374 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
22375 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
22379 processing_template_decl
++;
22385 /* Stop when an ellipsis is seen. */
22386 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
22388 tree arg1
= TREE_VALUE (args1
);
22389 tree arg2
= TREE_VALUE (args2
);
22390 int deduce1
, deduce2
;
22396 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22397 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22399 /* When both arguments are pack expansions, we need only
22400 unify the patterns themselves. */
22401 arg1
= PACK_EXPANSION_PATTERN (arg1
);
22402 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22404 /* This is the last comparison we need to do. */
22408 /* DR 1847: If a particular P contains no template-parameters that
22409 participate in template argument deduction, that P is not used to
22410 determine the ordering. */
22411 if (!uses_deducible_template_parms (arg1
)
22412 && !uses_deducible_template_parms (arg2
))
22415 if (TYPE_REF_P (arg1
))
22417 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
22418 arg1
= TREE_TYPE (arg1
);
22419 quals1
= cp_type_quals (arg1
);
22422 if (TYPE_REF_P (arg2
))
22424 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
22425 arg2
= TREE_TYPE (arg2
);
22426 quals2
= cp_type_quals (arg2
);
22429 arg1
= TYPE_MAIN_VARIANT (arg1
);
22430 arg2
= TYPE_MAIN_VARIANT (arg2
);
22432 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
22434 int i
, len2
= remaining_arguments (args2
);
22435 tree parmvec
= make_tree_vec (1);
22436 tree argvec
= make_tree_vec (len2
);
22439 /* Setup the parameter vector, which contains only ARG1. */
22440 TREE_VEC_ELT (parmvec
, 0) = arg1
;
22442 /* Setup the argument vector, which contains the remaining
22444 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
22445 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22447 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
22448 argvec
, DEDUCE_EXACT
,
22449 /*subr=*/true, /*explain_p=*/false)
22452 /* We cannot deduce in the other direction, because ARG1 is
22453 a pack expansion but ARG2 is not. */
22456 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22458 int i
, len1
= remaining_arguments (args1
);
22459 tree parmvec
= make_tree_vec (1);
22460 tree argvec
= make_tree_vec (len1
);
22463 /* Setup the parameter vector, which contains only ARG1. */
22464 TREE_VEC_ELT (parmvec
, 0) = arg2
;
22466 /* Setup the argument vector, which contains the remaining
22468 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
22469 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22471 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
22472 argvec
, DEDUCE_EXACT
,
22473 /*subr=*/true, /*explain_p=*/false)
22476 /* We cannot deduce in the other direction, because ARG2 is
22477 a pack expansion but ARG1 is not.*/
22483 /* The normal case, where neither argument is a pack
22485 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
22486 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22488 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
22489 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22493 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22494 arg2, then arg2 is not as specialized as arg1. */
22500 /* "If, for a given type, deduction succeeds in both directions
22501 (i.e., the types are identical after the transformations above)
22502 and both P and A were reference types (before being replaced with
22503 the type referred to above):
22504 - if the type from the argument template was an lvalue reference and
22505 the type from the parameter template was not, the argument type is
22506 considered to be more specialized than the other; otherwise,
22507 - if the type from the argument template is more cv-qualified
22508 than the type from the parameter template (as described above),
22509 the argument type is considered to be more specialized than the other;
22511 - neither type is more specialized than the other." */
22513 if (deduce1
&& deduce2
)
22515 if (ref1
&& ref2
&& ref1
!= ref2
)
22522 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
22524 if ((quals1
& quals2
) == quals2
)
22526 if ((quals1
& quals2
) == quals1
)
22531 if (lose1
&& lose2
)
22532 /* We've failed to deduce something in either direction.
22533 These must be unordered. */
22538 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22539 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22540 /* We have already processed all of the arguments in our
22541 handing of the pack expansion type. */
22544 args1
= TREE_CHAIN (args1
);
22545 args2
= TREE_CHAIN (args2
);
22548 /* "In most cases, all template parameters must have values in order for
22549 deduction to succeed, but for partial ordering purposes a template
22550 parameter may remain without a value provided it is not used in the
22551 types being used for partial ordering."
22553 Thus, if we are missing any of the targs1 we need to substitute into
22554 origs1, then pat2 is not as specialized as pat1. This can happen when
22555 there is a nondeduced context. */
22556 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
22558 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
22561 processing_template_decl
--;
22563 /* If both deductions succeed, the partial ordering selects the more
22564 constrained template. */
22565 if (!lose1
&& !lose2
)
22567 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
22568 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
22569 lose1
= !subsumes_constraints (c1
, c2
);
22570 lose2
= !subsumes_constraints (c2
, c1
);
22573 /* All things being equal, if the next argument is a pack expansion
22574 for one function but not for the other, prefer the
22575 non-variadic function. FIXME this is bogus; see c++/41958. */
22577 && args1
&& TREE_VALUE (args1
)
22578 && args2
&& TREE_VALUE (args2
))
22580 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
22581 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
22584 if (lose1
== lose2
)
22592 /* Determine which of two partial specializations of TMPL is more
22595 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22596 to the first partial specialization. The TREE_PURPOSE is the
22597 innermost set of template parameters for the partial
22598 specialization. PAT2 is similar, but for the second template.
22600 Return 1 if the first partial specialization is more specialized;
22601 -1 if the second is more specialized; 0 if neither is more
22604 See [temp.class.order] for information about determining which of
22605 two templates is more specialized. */
22608 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
22612 bool any_deductions
= false;
22614 tree tmpl1
= TREE_VALUE (pat1
);
22615 tree tmpl2
= TREE_VALUE (pat2
);
22616 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
22617 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
22619 /* Just like what happens for functions, if we are ordering between
22620 different template specializations, we may encounter dependent
22621 types in the arguments, and we need our dependency check functions
22622 to behave correctly. */
22623 ++processing_template_decl
;
22624 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
22628 any_deductions
= true;
22631 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
22635 any_deductions
= true;
22637 --processing_template_decl
;
22639 /* If both deductions succeed, the partial ordering selects the more
22640 constrained template. */
22641 if (!winner
&& any_deductions
)
22642 return more_constrained (tmpl1
, tmpl2
);
22644 /* In the case of a tie where at least one of the templates
22645 has a parameter pack at the end, the template with the most
22646 non-packed parameters wins. */
22649 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
22650 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
22652 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
22653 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
22654 int len1
= TREE_VEC_LENGTH (args1
);
22655 int len2
= TREE_VEC_LENGTH (args2
);
22657 /* We don't count the pack expansion at the end. */
22658 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
22660 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
22665 else if (len1
< len2
)
22672 /* Return the template arguments that will produce the function signature
22673 DECL from the function template FN, with the explicit template
22674 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22675 also match. Return NULL_TREE if no satisfactory arguments could be
22679 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
22681 int ntparms
= DECL_NTPARMS (fn
);
22682 tree targs
= make_tree_vec (ntparms
);
22683 tree decl_type
= TREE_TYPE (decl
);
22684 tree decl_arg_types
;
22686 unsigned int nargs
, ix
;
22689 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
22691 /* Never do unification on the 'this' parameter. */
22692 decl_arg_types
= skip_artificial_parms_for (decl
,
22693 TYPE_ARG_TYPES (decl_type
));
22695 nargs
= list_length (decl_arg_types
);
22696 args
= XALLOCAVEC (tree
, nargs
);
22697 for (arg
= decl_arg_types
, ix
= 0;
22698 arg
!= NULL_TREE
&& arg
!= void_list_node
;
22699 arg
= TREE_CHAIN (arg
), ++ix
)
22700 args
[ix
] = TREE_VALUE (arg
);
22702 if (fn_type_unification (fn
, explicit_args
, targs
,
22704 (check_rettype
|| DECL_CONV_FN_P (fn
)
22705 ? TREE_TYPE (decl_type
) : NULL_TREE
),
22706 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
22707 /*explain_p=*/false,
22709 == error_mark_node
)
22715 /* Return the innermost template arguments that, when applied to a partial
22716 specialization SPEC_TMPL of TMPL, yield the ARGS.
22718 For example, suppose we have:
22720 template <class T, class U> struct S {};
22721 template <class T> struct S<T*, int> {};
22723 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22724 partial specialization and the ARGS will be {double*, int}. The resulting
22725 vector will be {double}, indicating that `T' is bound to `double'. */
22728 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
22730 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
22732 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
22733 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
22735 tree innermost_deduced_args
;
22737 innermost_deduced_args
= make_tree_vec (ntparms
);
22738 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
22740 deduced_args
= copy_node (args
);
22741 SET_TMPL_ARGS_LEVEL (deduced_args
,
22742 TMPL_ARGS_DEPTH (deduced_args
),
22743 innermost_deduced_args
);
22746 deduced_args
= innermost_deduced_args
;
22748 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
22750 if (unify (tparms
, deduced_args
,
22751 INNERMOST_TEMPLATE_ARGS (spec_args
),
22752 INNERMOST_TEMPLATE_ARGS (args
),
22753 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
22756 for (i
= 0; i
< ntparms
; ++i
)
22757 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
22759 if (!tried_array_deduction
)
22761 try_array_deduction (tparms
, innermost_deduced_args
,
22762 INNERMOST_TEMPLATE_ARGS (spec_args
));
22763 tried_array_deduction
= true;
22764 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
22770 if (!push_tinst_level (spec_tmpl
, deduced_args
))
22772 excessive_deduction_depth
= true;
22776 /* Verify that nondeduced template arguments agree with the type
22777 obtained from argument deduction.
22781 struct A { typedef int X; };
22782 template <class T, class U> struct C {};
22783 template <class T> struct C<T, typename T::X> {};
22785 Then with the instantiation `C<A, int>', we can deduce that
22786 `T' is `A' but unify () does not check whether `typename T::X'
22788 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
22790 if (spec_args
!= error_mark_node
)
22791 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
22792 INNERMOST_TEMPLATE_ARGS (spec_args
),
22793 tmpl
, tf_none
, false, false);
22795 pop_tinst_level ();
22797 if (spec_args
== error_mark_node
22798 /* We only need to check the innermost arguments; the other
22799 arguments will always agree. */
22800 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
22801 INNERMOST_TEMPLATE_ARGS (args
)))
22804 /* Now that we have bindings for all of the template arguments,
22805 ensure that the arguments deduced for the template template
22806 parameters have compatible template parameter lists. See the use
22807 of template_template_parm_bindings_ok_p in fn_type_unification
22808 for more information. */
22809 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
22812 return deduced_args
;
22815 // Compare two function templates T1 and T2 by deducing bindings
22816 // from one against the other. If both deductions succeed, compare
22817 // constraints to see which is more constrained.
22819 more_specialized_inst (tree t1
, tree t2
)
22824 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
22830 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
22836 // If both deductions succeed, then one may be more constrained.
22837 if (count
== 2 && fate
== 0)
22838 fate
= more_constrained (t1
, t2
);
22843 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22844 Return the TREE_LIST node with the most specialized template, if
22845 any. If there is no most specialized template, the error_mark_node
22848 Note that this function does not look at, or modify, the
22849 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22850 returned is one of the elements of INSTANTIATIONS, callers may
22851 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22852 and retrieve it from the value returned. */
22855 most_specialized_instantiation (tree templates
)
22859 ++processing_template_decl
;
22862 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
22864 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
22865 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
22870 /* Equally specialized, move to next function. If there
22871 is no next function, nothing's most specialized. */
22872 fn
= TREE_CHAIN (fn
);
22880 /* Now verify that champ is better than everything earlier in the
22881 instantiation list. */
22882 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
22883 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
22890 processing_template_decl
--;
22893 return error_mark_node
;
22898 /* If DECL is a specialization of some template, return the most
22899 general such template. Otherwise, returns NULL_TREE.
22901 For example, given:
22903 template <class T> struct S { template <class U> void f(U); };
22905 if TMPL is `template <class U> void S<int>::f(U)' this will return
22906 the full template. This function will not trace past partial
22907 specializations, however. For example, given in addition:
22909 template <class T> struct S<T*> { template <class U> void f(U); };
22911 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22912 `template <class T> template <class U> S<T*>::f(U)'. */
22915 most_general_template (tree decl
)
22917 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22919 if (tree tinfo
= get_template_info (decl
))
22920 decl
= TI_TEMPLATE (tinfo
);
22921 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22922 template friend, or a FIELD_DECL for a capture pack. */
22923 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
22927 /* Look for more and more general templates. */
22928 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
22930 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22931 (See cp-tree.h for details.) */
22932 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
22935 if (CLASS_TYPE_P (TREE_TYPE (decl
))
22936 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
22937 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
22940 /* Stop if we run into an explicitly specialized class template. */
22941 if (!DECL_NAMESPACE_SCOPE_P (decl
)
22942 && DECL_CONTEXT (decl
)
22943 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
22946 decl
= DECL_TI_TEMPLATE (decl
);
22952 /* Return the most specialized of the template partial specializations
22953 which can produce TARGET, a specialization of some class or variable
22954 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22955 a TEMPLATE_DECL node corresponding to the partial specialization, while
22956 the TREE_PURPOSE is the set of template arguments that must be
22957 substituted into the template pattern in order to generate TARGET.
22959 If the choice of partial specialization is ambiguous, a diagnostic
22960 is issued, and the error_mark_node is returned. If there are no
22961 partial specializations matching TARGET, then NULL_TREE is
22962 returned, indicating that the primary template should be used. */
22965 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
22967 tree list
= NULL_TREE
;
22972 tree outer_args
= NULL_TREE
;
22975 if (TYPE_P (target
))
22977 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
22978 tmpl
= TI_TEMPLATE (tinfo
);
22979 args
= TI_ARGS (tinfo
);
22981 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
22983 tmpl
= TREE_OPERAND (target
, 0);
22984 args
= TREE_OPERAND (target
, 1);
22986 else if (VAR_P (target
))
22988 tree tinfo
= DECL_TEMPLATE_INFO (target
);
22989 tmpl
= TI_TEMPLATE (tinfo
);
22990 args
= TI_ARGS (tinfo
);
22993 gcc_unreachable ();
22995 tree main_tmpl
= most_general_template (tmpl
);
22997 /* For determining which partial specialization to use, only the
22998 innermost args are interesting. */
22999 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23001 outer_args
= strip_innermost_template_args (args
, 1);
23002 args
= INNERMOST_TEMPLATE_ARGS (args
);
23005 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23008 tree spec_tmpl
= TREE_VALUE (t
);
23012 /* Substitute in the template args from the enclosing class. */
23013 ++processing_template_decl
;
23014 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23015 --processing_template_decl
;
23018 if (spec_tmpl
== error_mark_node
)
23019 return error_mark_node
;
23021 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23025 spec_args
= add_to_template_args (outer_args
, spec_args
);
23027 /* Keep the candidate only if the constraints are satisfied,
23028 or if we're not compiling with concepts. */
23030 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23032 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23033 TREE_TYPE (list
) = TREE_TYPE (t
);
23041 ambiguous_p
= false;
23044 t
= TREE_CHAIN (t
);
23045 for (; t
; t
= TREE_CHAIN (t
))
23047 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23054 t
= TREE_CHAIN (t
);
23057 ambiguous_p
= true;
23066 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23068 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23071 ambiguous_p
= true;
23079 char *spaces
= NULL
;
23080 if (!(complain
& tf_error
))
23081 return error_mark_node
;
23082 if (TYPE_P (target
))
23083 error ("ambiguous template instantiation for %q#T", target
);
23085 error ("ambiguous template instantiation for %q#D", target
);
23086 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23087 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23089 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23090 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23091 "%s %#qS", spaces
? spaces
: str
, subst
);
23092 spaces
= spaces
? spaces
: get_spaces (str
);
23095 return error_mark_node
;
23101 /* Explicitly instantiate DECL. */
23104 do_decl_instantiation (tree decl
, tree storage
)
23106 tree result
= NULL_TREE
;
23109 if (!decl
|| decl
== error_mark_node
)
23110 /* An error occurred, for which grokdeclarator has already issued
23111 an appropriate message. */
23113 else if (! DECL_LANG_SPECIFIC (decl
))
23115 error ("explicit instantiation of non-template %q#D", decl
);
23119 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23120 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23122 if (VAR_P (decl
) && !var_templ
)
23124 /* There is an asymmetry here in the way VAR_DECLs and
23125 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23126 the latter, the DECL we get back will be marked as a
23127 template instantiation, and the appropriate
23128 DECL_TEMPLATE_INFO will be set up. This does not happen for
23129 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23130 should handle VAR_DECLs as it currently handles
23132 if (!DECL_CLASS_SCOPE_P (decl
))
23134 error ("%qD is not a static data member of a class template", decl
);
23137 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23138 if (!result
|| !VAR_P (result
))
23140 error ("no matching template for %qD found", decl
);
23143 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23145 error ("type %qT for explicit instantiation %qD does not match "
23146 "declared type %qT", TREE_TYPE (result
), decl
,
23151 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23153 error ("explicit instantiation of %q#D", decl
);
23159 /* Check for various error cases. Note that if the explicit
23160 instantiation is valid the RESULT will currently be marked as an
23161 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23162 until we get here. */
23164 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23166 /* DR 259 [temp.spec].
23168 Both an explicit instantiation and a declaration of an explicit
23169 specialization shall not appear in a program unless the explicit
23170 instantiation follows a declaration of the explicit specialization.
23172 For a given set of template parameters, if an explicit
23173 instantiation of a template appears after a declaration of an
23174 explicit specialization for that template, the explicit
23175 instantiation has no effect. */
23178 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23182 No program shall explicitly instantiate any template more
23185 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23186 the first instantiation was `extern' and the second is not,
23187 and EXTERN_P for the opposite case. */
23188 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23189 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23190 /* If an "extern" explicit instantiation follows an ordinary
23191 explicit instantiation, the template is instantiated. */
23195 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23197 error ("no matching template for %qD found", result
);
23200 else if (!DECL_TEMPLATE_INFO (result
))
23202 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23206 if (storage
== NULL_TREE
)
23208 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23210 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23211 pedwarn (input_location
, OPT_Wpedantic
,
23212 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23217 error ("storage class %qD applied to template instantiation", storage
);
23219 check_explicit_instantiation_namespace (result
);
23220 mark_decl_instantiated (result
, extern_p
);
23222 instantiate_decl (result
, /*defer_ok=*/true,
23223 /*expl_inst_class_mem_p=*/false);
23227 mark_class_instantiated (tree t
, int extern_p
)
23229 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23230 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23231 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23232 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23235 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23236 rest_of_type_compilation (t
, 1);
23240 /* Called from do_type_instantiation through binding_table_foreach to
23241 do recursive instantiation for the type bound in ENTRY. */
23243 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23245 tree storage
= *(tree
*) data
;
23247 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23248 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23249 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23250 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23253 /* Perform an explicit instantiation of template class T. STORAGE, if
23254 non-null, is the RID for extern, inline or static. COMPLAIN is
23255 nonzero if this is called from the parser, zero if called recursively,
23256 since the standard is unclear (as detailed below). */
23259 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23264 int previous_instantiation_extern_p
= 0;
23266 if (TREE_CODE (t
) == TYPE_DECL
)
23269 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23272 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23274 error ("explicit instantiation of non-class template %qD", tmpl
);
23276 error ("explicit instantiation of non-template type %qT", t
);
23282 if (!COMPLETE_TYPE_P (t
))
23284 if (complain
& tf_error
)
23285 error ("explicit instantiation of %q#T before definition of template",
23290 if (storage
!= NULL_TREE
)
23292 if (!in_system_header_at (input_location
))
23294 if (storage
== ridpointers
[(int) RID_EXTERN
])
23296 if (cxx_dialect
== cxx98
)
23297 pedwarn (input_location
, OPT_Wpedantic
,
23298 "ISO C++ 1998 forbids the use of %<extern%> on "
23299 "explicit instantiations");
23302 pedwarn (input_location
, OPT_Wpedantic
,
23303 "ISO C++ forbids the use of %qE"
23304 " on explicit instantiations", storage
);
23307 if (storage
== ridpointers
[(int) RID_INLINE
])
23309 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23311 else if (storage
== ridpointers
[(int) RID_STATIC
])
23315 error ("storage class %qD applied to template instantiation",
23321 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23323 /* DR 259 [temp.spec].
23325 Both an explicit instantiation and a declaration of an explicit
23326 specialization shall not appear in a program unless the explicit
23327 instantiation follows a declaration of the explicit specialization.
23329 For a given set of template parameters, if an explicit
23330 instantiation of a template appears after a declaration of an
23331 explicit specialization for that template, the explicit
23332 instantiation has no effect. */
23335 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
23339 No program shall explicitly instantiate any template more
23342 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23343 instantiation was `extern'. If EXTERN_P then the second is.
23344 These cases are OK. */
23345 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
23347 if (!previous_instantiation_extern_p
&& !extern_p
23348 && (complain
& tf_error
))
23349 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
23351 /* If we've already instantiated the template, just return now. */
23352 if (!CLASSTYPE_INTERFACE_ONLY (t
))
23356 check_explicit_instantiation_namespace (TYPE_NAME (t
));
23357 mark_class_instantiated (t
, extern_p
);
23362 /* In contrast to implicit instantiation, where only the
23363 declarations, and not the definitions, of members are
23364 instantiated, we have here:
23368 The explicit instantiation of a class template specialization
23369 implies the instantiation of all of its members not
23370 previously explicitly specialized in the translation unit
23371 containing the explicit instantiation.
23373 Of course, we can't instantiate member template classes, since we
23374 don't have any arguments for them. Note that the standard is
23375 unclear on whether the instantiation of the members are
23376 *explicit* instantiations or not. However, the most natural
23377 interpretation is that it should be an explicit
23379 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
23381 || (TREE_CODE (fld
) == FUNCTION_DECL
23383 && user_provided_p (fld
)))
23384 && DECL_TEMPLATE_INSTANTIATION (fld
))
23386 mark_decl_instantiated (fld
, extern_p
);
23388 instantiate_decl (fld
, /*defer_ok=*/true,
23389 /*expl_inst_class_mem_p=*/true);
23392 if (CLASSTYPE_NESTED_UTDS (t
))
23393 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
23394 bt_instantiate_type_proc
, &storage
);
23397 /* Given a function DECL, which is a specialization of TMPL, modify
23398 DECL to be a re-instantiation of TMPL with the same template
23399 arguments. TMPL should be the template into which tsubst'ing
23400 should occur for DECL, not the most general template.
23402 One reason for doing this is a scenario like this:
23405 void f(const T&, int i);
23407 void g() { f(3, 7); }
23410 void f(const T& t, const int i) { }
23412 Note that when the template is first instantiated, with
23413 instantiate_template, the resulting DECL will have no name for the
23414 first parameter, and the wrong type for the second. So, when we go
23415 to instantiate the DECL, we regenerate it. */
23418 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
23420 /* The arguments used to instantiate DECL, from the most general
23424 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
23426 /* Make sure that we can see identifiers, and compute access
23428 push_access_scope (decl
);
23430 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23438 args_depth
= TMPL_ARGS_DEPTH (args
);
23439 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
23440 if (args_depth
> parms_depth
)
23441 args
= get_innermost_template_args (args
, parms_depth
);
23443 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
23444 args
, tf_error
, NULL_TREE
,
23445 /*defer_ok*/false);
23446 if (specs
&& specs
!= error_mark_node
)
23447 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
23450 /* Merge parameter declarations. */
23451 decl_parm
= skip_artificial_parms_for (decl
,
23452 DECL_ARGUMENTS (decl
));
23454 = skip_artificial_parms_for (code_pattern
,
23455 DECL_ARGUMENTS (code_pattern
));
23456 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
23461 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23462 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
23463 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
23465 parm_type
= type_decays_to (parm_type
);
23466 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23467 TREE_TYPE (decl_parm
) = parm_type
;
23468 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23469 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23471 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23472 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23474 decl_parm
= DECL_CHAIN (decl_parm
);
23475 pattern_parm
= DECL_CHAIN (pattern_parm
);
23477 /* Merge any parameters that match with the function parameter
23479 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
23482 tree expanded_types
;
23483 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23484 the parameters in this function parameter pack. */
23485 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
23486 args
, tf_error
, NULL_TREE
);
23487 len
= TREE_VEC_LENGTH (expanded_types
);
23488 for (i
= 0; i
< len
; i
++)
23493 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23494 /* Rename the parameter to include the index. */
23495 DECL_NAME (decl_parm
) =
23496 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
23497 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
23498 parm_type
= type_decays_to (parm_type
);
23499 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23500 TREE_TYPE (decl_parm
) = parm_type
;
23501 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23502 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23504 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23505 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23507 decl_parm
= DECL_CHAIN (decl_parm
);
23510 /* Merge additional specifiers from the CODE_PATTERN. */
23511 if (DECL_DECLARED_INLINE_P (code_pattern
)
23512 && !DECL_DECLARED_INLINE_P (decl
))
23513 DECL_DECLARED_INLINE_P (decl
) = 1;
23515 else if (VAR_P (decl
))
23517 start_lambda_scope (decl
);
23518 DECL_INITIAL (decl
) =
23519 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
23520 tf_error
, DECL_TI_TEMPLATE (decl
),
23521 /*integral_constant_expression_p=*/false);
23522 finish_lambda_scope ();
23523 if (VAR_HAD_UNKNOWN_BOUND (decl
))
23524 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
23525 tf_error
, DECL_TI_TEMPLATE (decl
));
23528 gcc_unreachable ();
23530 pop_access_scope (decl
);
23533 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23534 substituted to get DECL. */
23537 template_for_substitution (tree decl
)
23539 tree tmpl
= DECL_TI_TEMPLATE (decl
);
23541 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23542 for the instantiation. This is not always the most general
23543 template. Consider, for example:
23546 struct S { template <class U> void f();
23547 template <> void f<int>(); };
23549 and an instantiation of S<double>::f<int>. We want TD to be the
23550 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23551 while (/* An instantiation cannot have a definition, so we need a
23552 more general template. */
23553 DECL_TEMPLATE_INSTANTIATION (tmpl
)
23554 /* We must also deal with friend templates. Given:
23556 template <class T> struct S {
23557 template <class U> friend void f() {};
23560 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23561 so far as the language is concerned, but that's still
23562 where we get the pattern for the instantiation from. On
23563 other hand, if the definition comes outside the class, say:
23565 template <class T> struct S {
23566 template <class U> friend void f();
23568 template <class U> friend void f() {}
23570 we don't need to look any further. That's what the check for
23571 DECL_INITIAL is for. */
23572 || (TREE_CODE (decl
) == FUNCTION_DECL
23573 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
23574 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
23576 /* The present template, TD, should not be a definition. If it
23577 were a definition, we should be using it! Note that we
23578 cannot restructure the loop to just keep going until we find
23579 a template with a definition, since that might go too far if
23580 a specialization was declared, but not defined. */
23582 /* Fetch the more general template. */
23583 tmpl
= DECL_TI_TEMPLATE (tmpl
);
23589 /* Returns true if we need to instantiate this template instance even if we
23590 know we aren't going to emit it. */
23593 always_instantiate_p (tree decl
)
23595 /* We always instantiate inline functions so that we can inline them. An
23596 explicit instantiation declaration prohibits implicit instantiation of
23597 non-inline functions. With high levels of optimization, we would
23598 normally inline non-inline functions -- but we're not allowed to do
23599 that for "extern template" functions. Therefore, we check
23600 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23601 return ((TREE_CODE (decl
) == FUNCTION_DECL
23602 && (DECL_DECLARED_INLINE_P (decl
)
23603 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
23604 /* And we need to instantiate static data members so that
23605 their initializers are available in integral constant
23608 && decl_maybe_constant_var_p (decl
)));
23611 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23612 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23613 error, true otherwise. */
23616 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
23618 tree fntype
, spec
, noex
, clone
;
23620 /* Don't instantiate a noexcept-specification from template context. */
23621 if (processing_template_decl
23622 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
23625 if (DECL_CLONED_FUNCTION_P (fn
))
23626 fn
= DECL_CLONED_FUNCTION (fn
);
23627 fntype
= TREE_TYPE (fn
);
23628 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
23630 if (!spec
|| !TREE_PURPOSE (spec
))
23633 noex
= TREE_PURPOSE (spec
);
23635 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
23637 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
23638 bool added
= false;
23639 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
23640 spec
= get_defaulted_eh_spec (fn
, complain
);
23641 else if (!(added
= !fns
->add (fn
)))
23643 /* If hash_set::add returns true, the element was already there. */
23644 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
23645 DECL_SOURCE_LOCATION (fn
));
23647 "exception specification of %qD depends on itself",
23649 spec
= noexcept_false_spec
;
23651 else if (push_tinst_level (fn
))
23653 push_access_scope (fn
);
23654 push_deferring_access_checks (dk_no_deferred
);
23655 input_location
= DECL_SOURCE_LOCATION (fn
);
23656 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
23657 DEFERRED_NOEXCEPT_ARGS (noex
),
23658 tf_warning_or_error
, fn
,
23659 /*function_p=*/false,
23660 /*integral_constant_expression_p=*/true);
23661 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
23662 pop_deferring_access_checks ();
23663 pop_access_scope (fn
);
23664 pop_tinst_level ();
23665 if (spec
== error_mark_node
)
23666 spec
= noexcept_false_spec
;
23669 spec
= noexcept_false_spec
;
23674 if (spec
== error_mark_node
)
23677 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
23680 FOR_EACH_CLONE (clone
, fn
)
23682 if (TREE_TYPE (clone
) == fntype
)
23683 TREE_TYPE (clone
) = TREE_TYPE (fn
);
23685 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
23691 /* We're starting to process the function INST, an instantiation of PATTERN;
23692 add their parameters to local_specializations. */
23695 register_parameter_specializations (tree pattern
, tree inst
)
23697 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
23698 tree spec_parm
= DECL_ARGUMENTS (inst
);
23699 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
23701 register_local_specialization (spec_parm
, tmpl_parm
);
23702 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
23703 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
23705 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
23707 if (!DECL_PACK_P (tmpl_parm
))
23709 register_local_specialization (spec_parm
, tmpl_parm
);
23710 spec_parm
= DECL_CHAIN (spec_parm
);
23714 /* Register the (value) argument pack as a specialization of
23715 TMPL_PARM, then move on. */
23716 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
23717 register_local_specialization (argpack
, tmpl_parm
);
23720 gcc_assert (!spec_parm
);
23723 /* Produce the definition of D, a _DECL generated from a template. If
23724 DEFER_OK is true, then we don't have to actually do the
23725 instantiation now; we just have to do it sometime. Normally it is
23726 an error if this is an explicit instantiation but D is undefined.
23727 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23728 instantiated class template. */
23731 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
23733 tree tmpl
= DECL_TI_TEMPLATE (d
);
23740 bool pattern_defined
;
23741 location_t saved_loc
= input_location
;
23742 int saved_unevaluated_operand
= cp_unevaluated_operand
;
23743 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23747 /* This function should only be used to instantiate templates for
23748 functions and static member variables. */
23749 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
23751 /* A concept is never instantiated. */
23752 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
23754 /* Variables are never deferred; if instantiation is required, they
23755 are instantiated right away. That allows for better code in the
23756 case that an expression refers to the value of the variable --
23757 if the variable has a constant value the referring expression can
23758 take advantage of that fact. */
23762 /* Don't instantiate cloned functions. Instead, instantiate the
23763 functions they cloned. */
23764 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
23765 d
= DECL_CLONED_FUNCTION (d
);
23767 if (DECL_TEMPLATE_INSTANTIATED (d
)
23768 || (TREE_CODE (d
) == FUNCTION_DECL
23769 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
23770 || DECL_TEMPLATE_SPECIALIZATION (d
))
23771 /* D has already been instantiated or explicitly specialized, so
23772 there's nothing for us to do here.
23774 It might seem reasonable to check whether or not D is an explicit
23775 instantiation, and, if so, stop here. But when an explicit
23776 instantiation is deferred until the end of the compilation,
23777 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23778 the instantiation. */
23781 /* Check to see whether we know that this template will be
23782 instantiated in some other file, as with "extern template"
23784 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
23786 /* In general, we do not instantiate such templates. */
23787 if (external_p
&& !always_instantiate_p (d
))
23790 gen_tmpl
= most_general_template (tmpl
);
23791 gen_args
= DECL_TI_ARGS (d
);
23793 if (tmpl
!= gen_tmpl
)
23794 /* We should already have the extra args. */
23795 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
23796 == TMPL_ARGS_DEPTH (gen_args
));
23797 /* And what's in the hash table should match D. */
23798 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
23799 || spec
== NULL_TREE
);
23801 /* This needs to happen before any tsubsting. */
23802 if (! push_tinst_level (d
))
23805 timevar_push (TV_TEMPLATE_INST
);
23807 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23808 for the instantiation. */
23809 td
= template_for_substitution (d
);
23814 /* Look up an explicit specialization, if any. */
23815 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
23816 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
23817 if (elt
&& elt
!= error_mark_node
)
23819 td
= TREE_VALUE (elt
);
23820 args
= TREE_PURPOSE (elt
);
23824 code_pattern
= DECL_TEMPLATE_RESULT (td
);
23826 /* We should never be trying to instantiate a member of a class
23827 template or partial specialization. */
23828 gcc_assert (d
!= code_pattern
);
23830 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
23831 || DECL_TEMPLATE_SPECIALIZATION (td
))
23832 /* In the case of a friend template whose definition is provided
23833 outside the class, we may have too many arguments. Drop the
23834 ones we don't need. The same is true for specializations. */
23835 args
= get_innermost_template_args
23836 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
23838 if (TREE_CODE (d
) == FUNCTION_DECL
)
23840 deleted_p
= DECL_DELETED_FN (code_pattern
);
23841 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
23842 && DECL_INITIAL (code_pattern
) != error_mark_node
)
23843 || DECL_DEFAULTED_FN (code_pattern
)
23849 if (DECL_CLASS_SCOPE_P (code_pattern
))
23850 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
23851 || DECL_INLINE_VAR_P (code_pattern
));
23853 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
23856 /* We may be in the middle of deferred access check. Disable it now. */
23857 push_deferring_access_checks (dk_no_deferred
);
23859 /* Unless an explicit instantiation directive has already determined
23860 the linkage of D, remember that a definition is available for
23862 if (pattern_defined
23863 && !DECL_INTERFACE_KNOWN (d
)
23864 && !DECL_NOT_REALLY_EXTERN (d
))
23865 mark_definable (d
);
23867 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
23868 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
23869 input_location
= DECL_SOURCE_LOCATION (d
);
23871 /* If D is a member of an explicitly instantiated class template,
23872 and no definition is available, treat it like an implicit
23874 if (!pattern_defined
&& expl_inst_class_mem_p
23875 && DECL_EXPLICIT_INSTANTIATION (d
))
23877 /* Leave linkage flags alone on instantiations with anonymous
23879 if (TREE_PUBLIC (d
))
23881 DECL_NOT_REALLY_EXTERN (d
) = 0;
23882 DECL_INTERFACE_KNOWN (d
) = 0;
23884 SET_DECL_IMPLICIT_INSTANTIATION (d
);
23887 /* Defer all other templates, unless we have been explicitly
23888 forbidden from doing so. */
23889 if (/* If there is no definition, we cannot instantiate the
23892 /* If it's OK to postpone instantiation, do so. */
23894 /* If this is a static data member that will be defined
23895 elsewhere, we don't want to instantiate the entire data
23896 member, but we do want to instantiate the initializer so that
23897 we can substitute that elsewhere. */
23898 || (external_p
&& VAR_P (d
))
23899 /* Handle here a deleted function too, avoid generating
23900 its body (c++/61080). */
23903 /* The definition of the static data member is now required so
23904 we must substitute the initializer. */
23906 && !DECL_INITIAL (d
)
23907 && DECL_INITIAL (code_pattern
))
23911 bool const_init
= false;
23912 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
23914 ns
= decl_namespace_context (d
);
23915 push_nested_namespace (ns
);
23917 push_nested_class (DECL_CONTEXT (d
));
23918 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
23920 tf_warning_or_error
, NULL_TREE
,
23921 /*integral_constant_expression_p=*/false);
23922 /* If instantiating the initializer involved instantiating this
23923 again, don't call cp_finish_decl twice. */
23924 if (!DECL_INITIAL (d
))
23926 /* Make sure the initializer is still constant, in case of
23927 circular dependency (template/instantiate6.C). */
23929 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
23930 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
23931 /*asmspec_tree=*/NULL_TREE
,
23932 LOOKUP_ONLYCONVERTING
);
23935 pop_nested_class ();
23936 pop_nested_namespace (ns
);
23939 /* We restore the source position here because it's used by
23940 add_pending_template. */
23941 input_location
= saved_loc
;
23943 if (at_eof
&& !pattern_defined
23944 && DECL_EXPLICIT_INSTANTIATION (d
)
23945 && DECL_NOT_REALLY_EXTERN (d
))
23948 The definition of a non-exported function template, a
23949 non-exported member function template, or a non-exported
23950 member function or static data member of a class template
23951 shall be present in every translation unit in which it is
23952 explicitly instantiated. */
23953 permerror (input_location
, "explicit instantiation of %qD "
23954 "but no definition available", d
);
23956 /* If we're in unevaluated context, we just wanted to get the
23957 constant value; this isn't an odr use, so don't queue
23958 a full instantiation. */
23959 if (cp_unevaluated_operand
!= 0)
23961 /* ??? Historically, we have instantiated inline functions, even
23962 when marked as "extern template". */
23963 if (!(external_p
&& VAR_P (d
)))
23964 add_pending_template (d
);
23967 /* Tell the repository that D is available in this translation unit
23968 -- and see if it is supposed to be instantiated here. */
23969 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
23971 /* In a PCH file, despite the fact that the repository hasn't
23972 requested instantiation in the PCH it is still possible that
23973 an instantiation will be required in a file that includes the
23976 add_pending_template (d
);
23977 /* Instantiate inline functions so that the inliner can do its
23978 job, even though we'll not be emitting a copy of this
23980 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
23984 bool push_to_top
, nested
;
23986 fn_context
= decl_function_context (d
);
23987 if (LAMBDA_FUNCTION_P (d
))
23988 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23989 fn_context
= NULL_TREE
;
23990 nested
= current_function_decl
!= NULL_TREE
;
23991 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
23993 vec
<tree
> omp_privatization_save
;
23995 save_omp_privatization_clauses (omp_privatization_save
);
23998 push_to_top_level ();
24001 gcc_assert (!processing_template_decl
);
24002 push_function_context ();
24003 cp_unevaluated_operand
= 0;
24004 c_inhibit_evaluation_warnings
= 0;
24007 /* Mark D as instantiated so that recursive calls to
24008 instantiate_decl do not try to instantiate it again. */
24009 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24011 /* Regenerate the declaration in case the template has been modified
24012 by a subsequent redeclaration. */
24013 regenerate_decl_from_template (d
, td
, args
);
24015 /* We already set the file and line above. Reset them now in case
24016 they changed as a result of calling regenerate_decl_from_template. */
24017 input_location
= DECL_SOURCE_LOCATION (d
);
24022 bool const_init
= false;
24024 /* Clear out DECL_RTL; whatever was there before may not be right
24025 since we've reset the type of the declaration. */
24026 SET_DECL_RTL (d
, NULL
);
24027 DECL_IN_AGGR_P (d
) = 0;
24029 /* The initializer is placed in DECL_INITIAL by
24030 regenerate_decl_from_template so we don't need to
24031 push/pop_access_scope again here. Pull it out so that
24032 cp_finish_decl can process it. */
24033 init
= DECL_INITIAL (d
);
24034 DECL_INITIAL (d
) = NULL_TREE
;
24035 DECL_INITIALIZED_P (d
) = 0;
24037 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24038 initializer. That function will defer actual emission until
24039 we have a chance to determine linkage. */
24040 DECL_EXTERNAL (d
) = 0;
24042 /* Enter the scope of D so that access-checking works correctly. */
24043 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24045 push_nested_class (DECL_CONTEXT (d
));
24047 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24048 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
24051 pop_nested_class ();
24053 if (variable_template_p (gen_tmpl
))
24054 note_variable_template_instantiation (d
);
24056 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24057 synthesize_method (d
);
24058 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24060 /* Set up the list of local specializations. */
24061 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24062 tree block
= NULL_TREE
;
24064 /* Set up context. */
24065 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24066 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24067 block
= push_stmt_list ();
24069 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24071 /* Some typedefs referenced from within the template code need to be
24072 access checked at template instantiation time, i.e now. These
24073 types were added to the template at parsing time. Let's get those
24074 and perform the access checks then. */
24075 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24078 /* Create substitution entries for the parameters. */
24079 register_parameter_specializations (code_pattern
, d
);
24081 /* Substitute into the body of the function. */
24082 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24083 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24084 tf_warning_or_error
, tmpl
);
24087 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24088 tf_warning_or_error
, tmpl
,
24089 /*integral_constant_expression_p=*/false);
24091 /* Set the current input_location to the end of the function
24092 so that finish_function knows where we are. */
24094 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24096 /* Remember if we saw an infinite loop in the template. */
24097 current_function_infinite_loop
24098 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24101 /* Finish the function. */
24102 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24103 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24104 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24107 d
= finish_function (/*inline_p=*/false);
24108 expand_or_defer_fn (d
);
24111 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24112 cp_check_omp_declare_reduction (d
);
24115 /* We're not deferring instantiation any more. */
24116 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24119 pop_from_top_level ();
24121 pop_function_context ();
24124 restore_omp_privatization_clauses (omp_privatization_save
);
24127 pop_deferring_access_checks ();
24128 timevar_pop (TV_TEMPLATE_INST
);
24129 pop_tinst_level ();
24130 input_location
= saved_loc
;
24131 cp_unevaluated_operand
= saved_unevaluated_operand
;
24132 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24137 /* Run through the list of templates that we wish we could
24138 instantiate, and instantiate any we can. RETRIES is the
24139 number of times we retry pending template instantiation. */
24142 instantiate_pending_templates (int retries
)
24145 location_t saved_loc
= input_location
;
24147 /* Instantiating templates may trigger vtable generation. This in turn
24148 may require further template instantiations. We place a limit here
24149 to avoid infinite loop. */
24150 if (pending_templates
&& retries
>= max_tinst_depth
)
24152 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24154 fatal_error (input_location
,
24155 "template instantiation depth exceeds maximum of %d"
24156 " instantiating %q+D, possibly from virtual table generation"
24157 " (use -ftemplate-depth= to increase the maximum)",
24158 max_tinst_depth
, decl
);
24159 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24160 /* Pretend that we defined it. */
24161 DECL_INITIAL (decl
) = error_mark_node
;
24167 struct pending_template
**t
= &pending_templates
;
24168 struct pending_template
*last
= NULL
;
24172 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24173 bool complete
= false;
24175 if (TYPE_P (instantiation
))
24177 if (!COMPLETE_TYPE_P (instantiation
))
24179 instantiate_class_template (instantiation
);
24180 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24181 for (tree fld
= TYPE_FIELDS (instantiation
);
24182 fld
; fld
= TREE_CHAIN (fld
))
24184 || (TREE_CODE (fld
) == FUNCTION_DECL
24185 && !DECL_ARTIFICIAL (fld
)))
24186 && DECL_TEMPLATE_INSTANTIATION (fld
))
24187 instantiate_decl (fld
,
24188 /*defer_ok=*/false,
24189 /*expl_inst_class_mem_p=*/false);
24191 if (COMPLETE_TYPE_P (instantiation
))
24195 complete
= COMPLETE_TYPE_P (instantiation
);
24199 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24200 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24203 = instantiate_decl (instantiation
,
24204 /*defer_ok=*/false,
24205 /*expl_inst_class_mem_p=*/false);
24206 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24210 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24211 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24216 /* If INSTANTIATION has been instantiated, then we don't
24217 need to consider it again in the future. */
24218 struct pending_template
*drop
= *t
;
24220 set_refcount_ptr (drop
->tinst
);
24221 pending_template_freelist ().free (drop
);
24229 set_refcount_ptr (current_tinst_level
);
24231 last_pending_template
= last
;
24233 while (reconsider
);
24235 input_location
= saved_loc
;
24238 /* Substitute ARGVEC into T, which is a list of initializers for
24239 either base class or a non-static data member. The TREE_PURPOSEs
24240 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24241 instantiate_decl. */
24244 tsubst_initializer_list (tree t
, tree argvec
)
24246 tree inits
= NULL_TREE
;
24247 tree target_ctor
= error_mark_node
;
24249 for (; t
; t
= TREE_CHAIN (t
))
24253 tree expanded_bases
= NULL_TREE
;
24254 tree expanded_arguments
= NULL_TREE
;
24257 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24262 /* Expand the base class expansion type into separate base
24264 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24265 tf_warning_or_error
,
24267 if (expanded_bases
== error_mark_node
)
24270 /* We'll be building separate TREE_LISTs of arguments for
24272 len
= TREE_VEC_LENGTH (expanded_bases
);
24273 expanded_arguments
= make_tree_vec (len
);
24274 for (i
= 0; i
< len
; i
++)
24275 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24277 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24278 expand each argument in the TREE_VALUE of t. */
24279 expr
= make_node (EXPR_PACK_EXPANSION
);
24280 PACK_EXPANSION_LOCAL_P (expr
) = true;
24281 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24282 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24284 if (TREE_VALUE (t
) == void_type_node
)
24285 /* VOID_TYPE_NODE is used to indicate
24286 value-initialization. */
24288 for (i
= 0; i
< len
; i
++)
24289 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24293 /* Substitute parameter packs into each argument in the
24295 in_base_initializer
= 1;
24296 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
24298 tree expanded_exprs
;
24300 /* Expand the argument. */
24301 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
24303 = tsubst_pack_expansion (expr
, argvec
,
24304 tf_warning_or_error
,
24306 if (expanded_exprs
== error_mark_node
)
24309 /* Prepend each of the expanded expressions to the
24310 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24311 for (i
= 0; i
< len
; i
++)
24313 TREE_VEC_ELT (expanded_arguments
, i
) =
24314 tree_cons (NULL_TREE
,
24315 TREE_VEC_ELT (expanded_exprs
, i
),
24316 TREE_VEC_ELT (expanded_arguments
, i
));
24319 in_base_initializer
= 0;
24321 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24322 since we built them backwards. */
24323 for (i
= 0; i
< len
; i
++)
24325 TREE_VEC_ELT (expanded_arguments
, i
) =
24326 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
24331 for (i
= 0; i
< len
; ++i
)
24333 if (expanded_bases
)
24335 decl
= TREE_VEC_ELT (expanded_bases
, i
);
24336 decl
= expand_member_init (decl
);
24337 init
= TREE_VEC_ELT (expanded_arguments
, i
);
24342 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
24343 tf_warning_or_error
, NULL_TREE
);
24345 decl
= expand_member_init (decl
);
24346 if (decl
&& !DECL_P (decl
))
24347 in_base_initializer
= 1;
24349 init
= TREE_VALUE (t
);
24351 if (init
!= void_type_node
)
24352 init
= tsubst_expr (init
, argvec
,
24353 tf_warning_or_error
, NULL_TREE
,
24354 /*integral_constant_expression_p=*/false);
24355 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
24356 /* If we had an initializer but it instantiated to nothing,
24357 value-initialize the object. This will only occur when
24358 the initializer was a pack expansion where the parameter
24359 packs used in that expansion were of length zero. */
24360 init
= void_type_node
;
24361 in_base_initializer
= 0;
24364 if (target_ctor
!= error_mark_node
24365 && init
!= error_mark_node
)
24367 error ("mem-initializer for %qD follows constructor delegation",
24371 /* Look for a target constructor. */
24372 if (init
!= error_mark_node
24373 && decl
&& CLASS_TYPE_P (decl
)
24374 && same_type_p (decl
, current_class_type
))
24376 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
24379 error ("constructor delegation follows mem-initializer for %qD",
24380 TREE_PURPOSE (inits
));
24383 target_ctor
= init
;
24388 init
= build_tree_list (decl
, init
);
24389 TREE_CHAIN (init
) = inits
;
24397 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24400 set_current_access_from_decl (tree decl
)
24402 if (TREE_PRIVATE (decl
))
24403 current_access_specifier
= access_private_node
;
24404 else if (TREE_PROTECTED (decl
))
24405 current_access_specifier
= access_protected_node
;
24407 current_access_specifier
= access_public_node
;
24410 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24411 is the instantiation (which should have been created with
24412 start_enum) and ARGS are the template arguments to use. */
24415 tsubst_enum (tree tag
, tree newtag
, tree args
)
24419 if (SCOPED_ENUM_P (newtag
))
24420 begin_scope (sk_scoped_enum
, newtag
);
24422 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
24427 decl
= TREE_VALUE (e
);
24428 /* Note that in a template enum, the TREE_VALUE is the
24429 CONST_DECL, not the corresponding INTEGER_CST. */
24430 value
= tsubst_expr (DECL_INITIAL (decl
),
24431 args
, tf_warning_or_error
, NULL_TREE
,
24432 /*integral_constant_expression_p=*/true);
24434 /* Give this enumeration constant the correct access. */
24435 set_current_access_from_decl (decl
);
24437 /* Actually build the enumerator itself. Here we're assuming that
24438 enumerators can't have dependent attributes. */
24439 build_enumerator (DECL_NAME (decl
), value
, newtag
,
24440 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
24443 if (SCOPED_ENUM_P (newtag
))
24446 finish_enum_value_list (newtag
);
24447 finish_enum (newtag
);
24449 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
24450 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
24453 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24454 its type -- but without substituting the innermost set of template
24455 arguments. So, innermost set of template parameters will appear in
24459 get_mostly_instantiated_function_type (tree decl
)
24461 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24462 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
24465 /* Return truthvalue if we're processing a template different from
24466 the last one involved in diagnostics. */
24468 problematic_instantiation_changed (void)
24470 return current_tinst_level
!= last_error_tinst_level
;
24473 /* Remember current template involved in diagnostics. */
24475 record_last_problematic_instantiation (void)
24477 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
24480 struct tinst_level
*
24481 current_instantiation (void)
24483 return current_tinst_level
;
24486 /* Return TRUE if current_function_decl is being instantiated, false
24490 instantiating_current_function_p (void)
24492 return (current_instantiation ()
24493 && (current_instantiation ()->maybe_get_node ()
24494 == current_function_decl
));
24497 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24498 type. Return false for ok, true for disallowed. Issue error and
24499 inform messages under control of COMPLAIN. */
24502 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
24504 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
24506 else if (TYPE_PTR_P (type
))
24508 else if (TYPE_REF_P (type
)
24509 && !TYPE_REF_IS_RVALUE (type
))
24511 else if (TYPE_PTRMEM_P (type
))
24513 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
24515 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
24517 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
24519 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
24521 /* A bound template template parm could later be instantiated to have a valid
24522 nontype parm type via an alias template. */
24523 else if (cxx_dialect
>= cxx11
24524 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24527 if (complain
& tf_error
)
24529 if (type
== error_mark_node
)
24530 inform (input_location
, "invalid template non-type parameter");
24532 error ("%q#T is not a valid type for a template non-type parameter",
24538 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24539 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24542 dependent_type_p_r (tree type
)
24548 A type is dependent if it is:
24550 -- a template parameter. Template template parameters are types
24551 for us (since TYPE_P holds true for them) so we handle
24553 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
24554 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
24556 /* -- a qualified-id with a nested-name-specifier which contains a
24557 class-name that names a dependent type or whose unqualified-id
24558 names a dependent type. */
24559 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24562 /* An alias template specialization can be dependent even if the
24563 resulting type is not. */
24564 if (dependent_alias_template_spec_p (type
))
24567 /* -- a cv-qualified type where the cv-unqualified type is
24569 No code is necessary for this bullet; the code below handles
24570 cv-qualified types, and we don't want to strip aliases with
24571 TYPE_MAIN_VARIANT because of DR 1558. */
24572 /* -- a compound type constructed from any dependent type. */
24573 if (TYPE_PTRMEM_P (type
))
24574 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
24575 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24577 else if (INDIRECT_TYPE_P (type
))
24578 return dependent_type_p (TREE_TYPE (type
));
24579 else if (TREE_CODE (type
) == FUNCTION_TYPE
24580 || TREE_CODE (type
) == METHOD_TYPE
)
24584 if (dependent_type_p (TREE_TYPE (type
)))
24586 for (arg_type
= TYPE_ARG_TYPES (type
);
24588 arg_type
= TREE_CHAIN (arg_type
))
24589 if (dependent_type_p (TREE_VALUE (arg_type
)))
24591 if (cxx_dialect
>= cxx17
)
24592 /* A value-dependent noexcept-specifier makes the type dependent. */
24593 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
24594 if (tree noex
= TREE_PURPOSE (spec
))
24595 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24596 affect overload resolution and treating it as dependent breaks
24598 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
24599 && value_dependent_expression_p (noex
))
24603 /* -- an array type constructed from any dependent type or whose
24604 size is specified by a constant expression that is
24607 We checked for type- and value-dependence of the bounds in
24608 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24609 if (TREE_CODE (type
) == ARRAY_TYPE
)
24611 if (TYPE_DOMAIN (type
)
24612 && dependent_type_p (TYPE_DOMAIN (type
)))
24614 return dependent_type_p (TREE_TYPE (type
));
24617 /* -- a template-id in which either the template name is a template
24619 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
24621 /* ... or any of the template arguments is a dependent type or
24622 an expression that is type-dependent or value-dependent. */
24623 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
24624 && (any_dependent_template_arguments_p
24625 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
24628 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24629 dependent; if the argument of the `typeof' expression is not
24630 type-dependent, then it should already been have resolved. */
24631 if (TREE_CODE (type
) == TYPEOF_TYPE
24632 || TREE_CODE (type
) == DECLTYPE_TYPE
24633 || TREE_CODE (type
) == UNDERLYING_TYPE
)
24636 /* A template argument pack is dependent if any of its packed
24638 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
24640 tree args
= ARGUMENT_PACK_ARGS (type
);
24641 int i
, len
= TREE_VEC_LENGTH (args
);
24642 for (i
= 0; i
< len
; ++i
)
24643 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24647 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24648 be template parameters. */
24649 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
24652 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
24655 /* The standard does not specifically mention types that are local
24656 to template functions or local classes, but they should be
24657 considered dependent too. For example:
24659 template <int I> void f() {
24664 The size of `E' cannot be known until the value of `I' has been
24665 determined. Therefore, `E' must be considered dependent. */
24666 scope
= TYPE_CONTEXT (type
);
24667 if (scope
&& TYPE_P (scope
))
24668 return dependent_type_p (scope
);
24669 /* Don't use type_dependent_expression_p here, as it can lead
24670 to infinite recursion trying to determine whether a lambda
24671 nested in a lambda is dependent (c++/47687). */
24672 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
24673 && DECL_LANG_SPECIFIC (scope
)
24674 && DECL_TEMPLATE_INFO (scope
)
24675 && (any_dependent_template_arguments_p
24676 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
24679 /* Other types are non-dependent. */
24683 /* Returns TRUE if TYPE is dependent, in the sense of
24684 [temp.dep.type]. Note that a NULL type is considered dependent. */
24687 dependent_type_p (tree type
)
24689 /* If there are no template parameters in scope, then there can't be
24690 any dependent types. */
24691 if (!processing_template_decl
)
24693 /* If we are not processing a template, then nobody should be
24694 providing us with a dependent type. */
24696 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
24700 /* If the type is NULL, we have not computed a type for the entity
24701 in question; in that case, the type is dependent. */
24705 /* Erroneous types can be considered non-dependent. */
24706 if (type
== error_mark_node
)
24709 /* Getting here with global_type_node means we improperly called this
24710 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24711 gcc_checking_assert (type
!= global_type_node
);
24713 /* If we have not already computed the appropriate value for TYPE,
24715 if (!TYPE_DEPENDENT_P_VALID (type
))
24717 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
24718 TYPE_DEPENDENT_P_VALID (type
) = 1;
24721 return TYPE_DEPENDENT_P (type
);
24724 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24725 lookup. In other words, a dependent type that is not the current
24729 dependent_scope_p (tree scope
)
24731 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
24732 && !currently_open_class (scope
));
24735 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24736 an unknown base of 'this' (and is therefore instantiation-dependent). */
24739 unknown_base_ref_p (tree t
)
24741 if (!current_class_ptr
)
24744 tree mem
= TREE_OPERAND (t
, 1);
24745 if (shared_member_p (mem
))
24748 tree cur
= current_nonlambda_class_type ();
24749 if (!any_dependent_bases_p (cur
))
24752 tree ctx
= TREE_OPERAND (t
, 0);
24753 if (DERIVED_FROM_P (ctx
, cur
))
24759 /* T is a SCOPE_REF; return whether we need to consider it
24760 instantiation-dependent so that we can check access at instantiation
24761 time even though we know which member it resolves to. */
24764 instantiation_dependent_scope_ref_p (tree t
)
24766 if (DECL_P (TREE_OPERAND (t
, 1))
24767 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
24768 && !unknown_base_ref_p (t
)
24769 && accessible_in_template_p (TREE_OPERAND (t
, 0),
24770 TREE_OPERAND (t
, 1)))
24776 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24777 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24780 /* Note that this predicate is not appropriate for general expressions;
24781 only constant expressions (that satisfy potential_constant_expression)
24782 can be tested for value dependence. */
24785 value_dependent_expression_p (tree expression
)
24787 if (!processing_template_decl
|| expression
== NULL_TREE
)
24790 /* A type-dependent expression is also value-dependent. */
24791 if (type_dependent_expression_p (expression
))
24794 switch (TREE_CODE (expression
))
24797 /* A dependent member function of the current instantiation. */
24798 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
24800 case FUNCTION_DECL
:
24801 /* A dependent member function of the current instantiation. */
24802 if (DECL_CLASS_SCOPE_P (expression
)
24803 && dependent_type_p (DECL_CONTEXT (expression
)))
24807 case IDENTIFIER_NODE
:
24808 /* A name that has not been looked up -- must be dependent. */
24811 case TEMPLATE_PARM_INDEX
:
24812 /* A non-type template parm. */
24816 /* A non-type template parm. */
24817 if (DECL_TEMPLATE_PARM_P (expression
))
24819 return value_dependent_expression_p (DECL_INITIAL (expression
));
24822 /* A constant with literal type and is initialized
24823 with an expression that is value-dependent. */
24824 if (DECL_DEPENDENT_INIT_P (expression
)
24825 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24826 || TYPE_REF_P (TREE_TYPE (expression
)))
24828 if (DECL_HAS_VALUE_EXPR_P (expression
))
24830 tree value_expr
= DECL_VALUE_EXPR (expression
);
24831 if (value_dependent_expression_p (value_expr
))
24836 case DYNAMIC_CAST_EXPR
:
24837 case STATIC_CAST_EXPR
:
24838 case CONST_CAST_EXPR
:
24839 case REINTERPRET_CAST_EXPR
:
24841 case IMPLICIT_CONV_EXPR
:
24842 /* These expressions are value-dependent if the type to which
24843 the cast occurs is dependent or the expression being casted
24844 is value-dependent. */
24846 tree type
= TREE_TYPE (expression
);
24848 if (dependent_type_p (type
))
24851 /* A functional cast has a list of operands. */
24852 expression
= TREE_OPERAND (expression
, 0);
24855 /* If there are no operands, it must be an expression such
24856 as "int()". This should not happen for aggregate types
24857 because it would form non-constant expressions. */
24858 gcc_assert (cxx_dialect
>= cxx11
24859 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
24864 if (TREE_CODE (expression
) == TREE_LIST
)
24865 return any_value_dependent_elements_p (expression
);
24867 return value_dependent_expression_p (expression
);
24871 if (SIZEOF_EXPR_TYPE_P (expression
))
24872 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
24876 /* A `sizeof' expression is value-dependent if the operand is
24877 type-dependent or is a pack expansion. */
24878 expression
= TREE_OPERAND (expression
, 0);
24879 if (PACK_EXPANSION_P (expression
))
24881 else if (TYPE_P (expression
))
24882 return dependent_type_p (expression
);
24883 return instantiation_dependent_uneval_expression_p (expression
);
24885 case AT_ENCODE_EXPR
:
24886 /* An 'encode' expression is value-dependent if the operand is
24888 expression
= TREE_OPERAND (expression
, 0);
24889 return dependent_type_p (expression
);
24891 case NOEXCEPT_EXPR
:
24892 expression
= TREE_OPERAND (expression
, 0);
24893 return instantiation_dependent_uneval_expression_p (expression
);
24896 /* All instantiation-dependent expressions should also be considered
24897 value-dependent. */
24898 return instantiation_dependent_scope_ref_p (expression
);
24900 case COMPONENT_REF
:
24901 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
24902 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
24904 case NONTYPE_ARGUMENT_PACK
:
24905 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24906 is value-dependent. */
24908 tree values
= ARGUMENT_PACK_ARGS (expression
);
24909 int i
, len
= TREE_VEC_LENGTH (values
);
24911 for (i
= 0; i
< len
; ++i
)
24912 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
24920 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
24922 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
24928 if (TREE_CODE (type2
) != TREE_LIST
)
24929 return dependent_type_p (type2
);
24931 for (; type2
; type2
= TREE_CHAIN (type2
))
24932 if (dependent_type_p (TREE_VALUE (type2
)))
24939 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24940 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
24943 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24944 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
24948 tree op
= TREE_OPERAND (expression
, 0);
24949 return (value_dependent_expression_p (op
)
24950 || has_value_dependent_address (op
));
24953 case REQUIRES_EXPR
:
24954 /* Treat all requires-expressions as value-dependent so
24955 we don't try to fold them. */
24959 return dependent_type_p (TREE_OPERAND (expression
, 0));
24963 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
24965 tree fn
= get_callee_fndecl (expression
);
24967 nargs
= call_expr_nargs (expression
);
24968 for (i
= 0; i
< nargs
; ++i
)
24970 tree op
= CALL_EXPR_ARG (expression
, i
);
24971 /* In a call to a constexpr member function, look through the
24972 implicit ADDR_EXPR on the object argument so that it doesn't
24973 cause the call to be considered value-dependent. We also
24974 look through it in potential_constant_expression. */
24975 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
24976 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
24977 && TREE_CODE (op
) == ADDR_EXPR
)
24978 op
= TREE_OPERAND (op
, 0);
24979 if (value_dependent_expression_p (op
))
24985 case TEMPLATE_ID_EXPR
:
24986 return variable_concept_p (TREE_OPERAND (expression
, 0));
24992 if (dependent_type_p (TREE_TYPE (expression
)))
24994 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
24995 if (value_dependent_expression_p (val
))
25001 /* Treat a GNU statement expression as dependent to avoid crashing
25002 under instantiate_non_dependent_expr; it can't be constant. */
25006 /* A constant expression is value-dependent if any subexpression is
25007 value-dependent. */
25008 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25010 case tcc_reference
:
25012 case tcc_comparison
:
25014 case tcc_expression
:
25017 int i
, len
= cp_tree_operand_length (expression
);
25019 for (i
= 0; i
< len
; i
++)
25021 tree t
= TREE_OPERAND (expression
, i
);
25023 /* In some cases, some of the operands may be missing.
25024 (For example, in the case of PREDECREMENT_EXPR, the
25025 amount to increment by may be missing.) That doesn't
25026 make the expression dependent. */
25027 if (t
&& value_dependent_expression_p (t
))
25038 /* The expression is not value-dependent. */
25042 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25043 [temp.dep.expr]. Note that an expression with no type is
25044 considered dependent. Other parts of the compiler arrange for an
25045 expression with type-dependent subexpressions to have no type, so
25046 this function doesn't have to be fully recursive. */
25049 type_dependent_expression_p (tree expression
)
25051 if (!processing_template_decl
)
25054 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25057 STRIP_ANY_LOCATION_WRAPPER (expression
);
25059 /* An unresolved name is always dependent. */
25060 if (identifier_p (expression
)
25061 || TREE_CODE (expression
) == USING_DECL
25062 || TREE_CODE (expression
) == WILDCARD_DECL
)
25065 /* A fold expression is type-dependent. */
25066 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25067 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25068 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25069 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25072 /* Some expression forms are never type-dependent. */
25073 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25074 || TREE_CODE (expression
) == SIZEOF_EXPR
25075 || TREE_CODE (expression
) == ALIGNOF_EXPR
25076 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25077 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25078 || TREE_CODE (expression
) == TRAIT_EXPR
25079 || TREE_CODE (expression
) == TYPEID_EXPR
25080 || TREE_CODE (expression
) == DELETE_EXPR
25081 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25082 || TREE_CODE (expression
) == THROW_EXPR
25083 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25086 /* The types of these expressions depends only on the type to which
25087 the cast occurs. */
25088 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25089 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25090 || TREE_CODE (expression
) == CONST_CAST_EXPR
25091 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25092 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25093 || TREE_CODE (expression
) == CAST_EXPR
)
25094 return dependent_type_p (TREE_TYPE (expression
));
25096 /* The types of these expressions depends only on the type created
25097 by the expression. */
25098 if (TREE_CODE (expression
) == NEW_EXPR
25099 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25101 /* For NEW_EXPR tree nodes created inside a template, either
25102 the object type itself or a TREE_LIST may appear as the
25104 tree type
= TREE_OPERAND (expression
, 1);
25105 if (TREE_CODE (type
) == TREE_LIST
)
25106 /* This is an array type. We need to check array dimensions
25108 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25109 || value_dependent_expression_p
25110 (TREE_OPERAND (TREE_VALUE (type
), 1));
25112 return dependent_type_p (type
);
25115 if (TREE_CODE (expression
) == SCOPE_REF
)
25117 tree scope
= TREE_OPERAND (expression
, 0);
25118 tree name
= TREE_OPERAND (expression
, 1);
25120 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25121 contains an identifier associated by name lookup with one or more
25122 declarations declared with a dependent type, or...a
25123 nested-name-specifier or qualified-id that names a member of an
25124 unknown specialization. */
25125 return (type_dependent_expression_p (name
)
25126 || dependent_scope_p (scope
));
25129 if (TREE_CODE (expression
) == TEMPLATE_DECL
25130 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25131 return uses_outer_template_parms (expression
);
25133 if (TREE_CODE (expression
) == STMT_EXPR
)
25134 expression
= stmt_expr_value_expr (expression
);
25136 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25141 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25143 if (type_dependent_expression_p (elt
))
25149 /* A static data member of the current instantiation with incomplete
25150 array type is type-dependent, as the definition and specializations
25151 can have different bounds. */
25152 if (VAR_P (expression
)
25153 && DECL_CLASS_SCOPE_P (expression
)
25154 && dependent_type_p (DECL_CONTEXT (expression
))
25155 && VAR_HAD_UNKNOWN_BOUND (expression
))
25158 /* An array of unknown bound depending on a variadic parameter, eg:
25160 template<typename... Args>
25161 void foo (Args... args)
25163 int arr[] = { args... };
25166 template<int... vals>
25169 int arr[] = { vals... };
25172 If the array has no length and has an initializer, it must be that
25173 we couldn't determine its length in cp_complete_array_type because
25174 it is dependent. */
25175 if (VAR_P (expression
)
25176 && TREE_TYPE (expression
) != NULL_TREE
25177 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25178 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25179 && DECL_INITIAL (expression
))
25182 /* A function or variable template-id is type-dependent if it has any
25183 dependent template arguments. */
25184 if (VAR_OR_FUNCTION_DECL_P (expression
)
25185 && DECL_LANG_SPECIFIC (expression
)
25186 && DECL_TEMPLATE_INFO (expression
))
25188 /* Consider the innermost template arguments, since those are the ones
25189 that come from the template-id; the template arguments for the
25190 enclosing class do not make it type-dependent unless they are used in
25191 the type of the decl. */
25192 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
25193 && (any_dependent_template_arguments_p
25194 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25198 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25199 type-dependent. Checking this is important for functions with auto return
25200 type, which looks like a dependent type. */
25201 if (TREE_CODE (expression
) == FUNCTION_DECL
25202 && !(DECL_CLASS_SCOPE_P (expression
)
25203 && dependent_type_p (DECL_CONTEXT (expression
)))
25204 && !(DECL_LANG_SPECIFIC (expression
)
25205 && DECL_FRIEND_P (expression
)
25206 && (!DECL_FRIEND_CONTEXT (expression
)
25207 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25208 && !DECL_LOCAL_FUNCTION_P (expression
))
25210 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25211 || undeduced_auto_decl (expression
));
25215 /* Always dependent, on the number of arguments if nothing else. */
25216 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25219 if (TREE_TYPE (expression
) == unknown_type_node
)
25221 if (TREE_CODE (expression
) == ADDR_EXPR
)
25222 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25223 if (TREE_CODE (expression
) == COMPONENT_REF
25224 || TREE_CODE (expression
) == OFFSET_REF
)
25226 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25228 expression
= TREE_OPERAND (expression
, 1);
25229 if (identifier_p (expression
))
25232 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25233 if (TREE_CODE (expression
) == SCOPE_REF
)
25236 if (BASELINK_P (expression
))
25238 if (BASELINK_OPTYPE (expression
)
25239 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25241 expression
= BASELINK_FUNCTIONS (expression
);
25244 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25246 if (any_dependent_template_arguments_p
25247 (TREE_OPERAND (expression
, 1)))
25249 expression
= TREE_OPERAND (expression
, 0);
25250 if (identifier_p (expression
))
25254 gcc_assert (TREE_CODE (expression
) == OVERLOAD
25255 || TREE_CODE (expression
) == FUNCTION_DECL
);
25257 for (lkp_iterator
iter (expression
); iter
; ++iter
)
25258 if (type_dependent_expression_p (*iter
))
25264 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
25266 /* Dependent type attributes might not have made it from the decl to
25268 if (DECL_P (expression
)
25269 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
25272 return (dependent_type_p (TREE_TYPE (expression
)));
25275 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25276 type-dependent if the expression refers to a member of the current
25277 instantiation and the type of the referenced member is dependent, or the
25278 class member access expression refers to a member of an unknown
25281 This function returns true if the OBJECT in such a class member access
25282 expression is of an unknown specialization. */
25285 type_dependent_object_expression_p (tree object
)
25287 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25289 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
25291 tree scope
= TREE_TYPE (object
);
25292 return (!scope
|| dependent_scope_p (scope
));
25295 /* walk_tree callback function for instantiation_dependent_expression_p,
25296 below. Returns non-zero if a dependent subexpression is found. */
25299 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
25304 /* We don't have to worry about decltype currently because decltype
25305 of an instantiation-dependent expr is a dependent type. This
25306 might change depending on the resolution of DR 1172. */
25307 *walk_subtrees
= false;
25310 enum tree_code code
= TREE_CODE (*tp
);
25313 /* Don't treat an argument list as dependent just because it has no
25317 case NONTYPE_ARGUMENT_PACK
:
25320 case TEMPLATE_PARM_INDEX
:
25323 /* Handle expressions with type operands. */
25327 case AT_ENCODE_EXPR
:
25329 tree op
= TREE_OPERAND (*tp
, 0);
25330 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
25331 op
= TREE_TYPE (op
);
25334 if (dependent_type_p (op
))
25338 *walk_subtrees
= false;
25345 case COMPONENT_REF
:
25346 if (identifier_p (TREE_OPERAND (*tp
, 1)))
25347 /* In a template, finish_class_member_access_expr creates a
25348 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25349 type-dependent, so that we can check access control at
25350 instantiation time (PR 42277). See also Core issue 1273. */
25355 if (instantiation_dependent_scope_ref_p (*tp
))
25360 /* Treat statement-expressions as dependent. */
25364 /* Treat requires-expressions as dependent. */
25365 case REQUIRES_EXPR
:
25369 /* Treat calls to function concepts as dependent. */
25370 if (function_concept_check_p (*tp
))
25374 case TEMPLATE_ID_EXPR
:
25375 /* And variable concepts. */
25376 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
25384 if (type_dependent_expression_p (*tp
))
25390 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25391 sense defined by the ABI:
25393 "An expression is instantiation-dependent if it is type-dependent
25394 or value-dependent, or it has a subexpression that is type-dependent
25395 or value-dependent."
25397 Except don't actually check value-dependence for unevaluated expressions,
25398 because in sizeof(i) we don't care about the value of i. Checking
25399 type-dependence will in turn check value-dependence of array bounds/template
25400 arguments as needed. */
25403 instantiation_dependent_uneval_expression_p (tree expression
)
25407 if (!processing_template_decl
)
25410 if (expression
== error_mark_node
)
25413 result
= cp_walk_tree_without_duplicates (&expression
,
25414 instantiation_dependent_r
, NULL
);
25415 return result
!= NULL_TREE
;
25418 /* As above, but also check value-dependence of the expression as a whole. */
25421 instantiation_dependent_expression_p (tree expression
)
25423 return (instantiation_dependent_uneval_expression_p (expression
)
25424 || value_dependent_expression_p (expression
));
25427 /* Like type_dependent_expression_p, but it also works while not processing
25428 a template definition, i.e. during substitution or mangling. */
25431 type_dependent_expression_p_push (tree expr
)
25434 ++processing_template_decl
;
25435 b
= type_dependent_expression_p (expr
);
25436 --processing_template_decl
;
25440 /* Returns TRUE if ARGS contains a type-dependent expression. */
25443 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
25448 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
25450 if (type_dependent_expression_p (arg
))
25456 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25457 expressions) contains any type-dependent expressions. */
25460 any_type_dependent_elements_p (const_tree list
)
25462 for (; list
; list
= TREE_CHAIN (list
))
25463 if (type_dependent_expression_p (TREE_VALUE (list
)))
25469 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25470 expressions) contains any value-dependent expressions. */
25473 any_value_dependent_elements_p (const_tree list
)
25475 for (; list
; list
= TREE_CHAIN (list
))
25476 if (value_dependent_expression_p (TREE_VALUE (list
)))
25482 /* Returns TRUE if the ARG (a template argument) is dependent. */
25485 dependent_template_arg_p (tree arg
)
25487 if (!processing_template_decl
)
25490 /* Assume a template argument that was wrongly written by the user
25491 is dependent. This is consistent with what
25492 any_dependent_template_arguments_p [that calls this function]
25494 if (!arg
|| arg
== error_mark_node
)
25497 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
25498 arg
= argument_pack_select_arg (arg
);
25500 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
25502 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25504 if (DECL_TEMPLATE_PARM_P (arg
))
25506 /* A member template of a dependent class is not necessarily
25507 type-dependent, but it is a dependent template argument because it
25508 will be a member of an unknown specialization to that template. */
25509 tree scope
= CP_DECL_CONTEXT (arg
);
25510 return TYPE_P (scope
) && dependent_type_p (scope
);
25512 else if (ARGUMENT_PACK_P (arg
))
25514 tree args
= ARGUMENT_PACK_ARGS (arg
);
25515 int i
, len
= TREE_VEC_LENGTH (args
);
25516 for (i
= 0; i
< len
; ++i
)
25518 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25524 else if (TYPE_P (arg
))
25525 return dependent_type_p (arg
);
25527 return (type_dependent_expression_p (arg
)
25528 || value_dependent_expression_p (arg
));
25531 /* Returns true if ARGS (a collection of template arguments) contains
25532 any types that require structural equality testing. */
25535 any_template_arguments_need_structural_equality_p (tree args
)
25542 if (args
== error_mark_node
)
25545 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25547 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25548 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25550 tree arg
= TREE_VEC_ELT (level
, j
);
25551 tree packed_args
= NULL_TREE
;
25554 if (ARGUMENT_PACK_P (arg
))
25556 /* Look inside the argument pack. */
25557 packed_args
= ARGUMENT_PACK_ARGS (arg
);
25558 len
= TREE_VEC_LENGTH (packed_args
);
25561 for (k
= 0; k
< len
; ++k
)
25564 arg
= TREE_VEC_ELT (packed_args
, k
);
25566 if (error_operand_p (arg
))
25568 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
25570 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
25572 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
25573 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
25582 /* Returns true if ARGS (a collection of template arguments) contains
25583 any dependent arguments. */
25586 any_dependent_template_arguments_p (const_tree args
)
25593 if (args
== error_mark_node
)
25596 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25598 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25599 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25600 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
25607 /* Returns true if ARGS contains any errors. */
25610 any_erroneous_template_args_p (const_tree args
)
25615 if (args
== error_mark_node
)
25618 if (args
&& TREE_CODE (args
) != TREE_VEC
)
25620 if (tree ti
= get_template_info (args
))
25621 args
= TI_ARGS (ti
);
25629 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
25631 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
25632 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
25633 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
25640 /* Returns TRUE if the template TMPL is type-dependent. */
25643 dependent_template_p (tree tmpl
)
25645 if (TREE_CODE (tmpl
) == OVERLOAD
)
25647 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
25648 if (dependent_template_p (*iter
))
25653 /* Template template parameters are dependent. */
25654 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
25655 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
25657 /* So are names that have not been looked up. */
25658 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
25663 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25666 dependent_template_id_p (tree tmpl
, tree args
)
25668 return (dependent_template_p (tmpl
)
25669 || any_dependent_template_arguments_p (args
));
25672 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25676 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
25680 if (!processing_template_decl
)
25683 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
25685 tree decl
= TREE_VEC_ELT (declv
, i
);
25686 tree init
= TREE_VEC_ELT (initv
, i
);
25687 tree cond
= TREE_VEC_ELT (condv
, i
);
25688 tree incr
= TREE_VEC_ELT (incrv
, i
);
25690 if (type_dependent_expression_p (decl
)
25691 || TREE_CODE (decl
) == SCOPE_REF
)
25694 if (init
&& type_dependent_expression_p (init
))
25697 if (type_dependent_expression_p (cond
))
25700 if (COMPARISON_CLASS_P (cond
)
25701 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
25702 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
25705 if (TREE_CODE (incr
) == MODOP_EXPR
)
25707 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
25708 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
25711 else if (type_dependent_expression_p (incr
))
25713 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
25715 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
25717 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
25719 tree t
= TREE_OPERAND (incr
, 1);
25720 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
25721 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
25730 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25731 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25732 no such TYPE can be found. Note that this function peers inside
25733 uninstantiated templates and therefore should be used only in
25734 extremely limited situations. ONLY_CURRENT_P restricts this
25735 peering to the currently open classes hierarchy (which is required
25736 when comparing types). */
25739 resolve_typename_type (tree type
, bool only_current_p
)
25748 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
25750 scope
= TYPE_CONTEXT (type
);
25751 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25752 gcc_checking_assert (uses_template_parms (scope
));
25754 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25755 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25756 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25757 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25758 identifier of the TYPENAME_TYPE anymore.
25759 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25760 TYPENAME_TYPE instead, we avoid messing up with a possible
25761 typedef variant case. */
25762 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
25764 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25765 it first before we can figure out what NAME refers to. */
25766 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
25768 if (TYPENAME_IS_RESOLVING_P (scope
))
25769 /* Given a class template A with a dependent base with nested type C,
25770 typedef typename A::C::C C will land us here, as trying to resolve
25771 the initial A::C leads to the local C typedef, which leads back to
25772 A::C::C. So we break the recursion now. */
25775 scope
= resolve_typename_type (scope
, only_current_p
);
25777 /* If we don't know what SCOPE refers to, then we cannot resolve the
25779 if (!CLASS_TYPE_P (scope
))
25781 /* If this is a typedef, we don't want to look inside (c++/11987). */
25782 if (typedef_variant_p (type
))
25784 /* If SCOPE isn't the template itself, it will not have a valid
25785 TYPE_FIELDS list. */
25786 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
25787 /* scope is either the template itself or a compatible instantiation
25788 like X<T>, so look up the name in the original template. */
25789 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
25790 /* If scope has no fields, it can't be a current instantiation. Check this
25791 before currently_open_class to avoid infinite recursion (71515). */
25792 if (!TYPE_FIELDS (scope
))
25794 /* If the SCOPE is not the current instantiation, there's no reason
25795 to look inside it. */
25796 if (only_current_p
&& !currently_open_class (scope
))
25798 /* Enter the SCOPE so that name lookup will be resolved as if we
25799 were in the class definition. In particular, SCOPE will no
25800 longer be considered a dependent type. */
25801 pushed_scope
= push_scope (scope
);
25802 /* Look up the declaration. */
25803 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
25804 tf_warning_or_error
);
25806 result
= NULL_TREE
;
25808 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25809 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25810 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
25813 else if (identifier_p (fullname
)
25814 && TREE_CODE (decl
) == TYPE_DECL
)
25816 result
= TREE_TYPE (decl
);
25817 if (result
== error_mark_node
)
25818 result
= NULL_TREE
;
25820 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
25821 && DECL_CLASS_TEMPLATE_P (decl
))
25823 /* Obtain the template and the arguments. */
25824 tree tmpl
= TREE_OPERAND (fullname
, 0);
25825 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
25827 /* We get here with a plain identifier because a previous tentative
25828 parse of the nested-name-specifier as part of a ptr-operator saw
25829 ::template X<A>. The use of ::template is necessary in a
25830 ptr-operator, but wrong in a declarator-id.
25832 [temp.names]: In a qualified-id of a declarator-id, the keyword
25833 template shall not appear at the top level. */
25834 pedwarn (cp_expr_loc_or_loc (fullname
, input_location
), OPT_Wpedantic
,
25835 "keyword %<template%> not allowed in declarator-id");
25838 tree args
= TREE_OPERAND (fullname
, 1);
25839 /* Instantiate the template. */
25840 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
25841 /*entering_scope=*/true,
25842 tf_error
| tf_user
);
25843 if (result
== error_mark_node
)
25844 result
= NULL_TREE
;
25847 /* Leave the SCOPE. */
25849 pop_scope (pushed_scope
);
25851 /* If we failed to resolve it, return the original typename. */
25855 /* If lookup found a typename type, resolve that too. */
25856 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
25858 /* Ill-formed programs can cause infinite recursion here, so we
25859 must catch that. */
25860 TYPENAME_IS_RESOLVING_P (result
) = 1;
25861 result
= resolve_typename_type (result
, only_current_p
);
25862 TYPENAME_IS_RESOLVING_P (result
) = 0;
25865 /* Qualify the resulting type. */
25866 quals
= cp_type_quals (type
);
25868 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
25873 /* EXPR is an expression which is not type-dependent. Return a proxy
25874 for EXPR that can be used to compute the types of larger
25875 expressions containing EXPR. */
25878 build_non_dependent_expr (tree expr
)
25880 tree orig_expr
= expr
;
25883 /* When checking, try to get a constant value for all non-dependent
25884 expressions in order to expose bugs in *_dependent_expression_p
25885 and constexpr. This can affect code generation, see PR70704, so
25886 only do this for -fchecking=2. */
25887 if (flag_checking
> 1
25888 && cxx_dialect
>= cxx11
25889 /* Don't do this during nsdmi parsing as it can lead to
25890 unexpected recursive instantiations. */
25891 && !parsing_nsdmi ()
25892 /* Don't do this during concept expansion either and for
25893 the same reason. */
25894 && !expanding_concept ())
25895 fold_non_dependent_expr (expr
, tf_none
);
25897 STRIP_ANY_LOCATION_WRAPPER (expr
);
25899 /* Preserve OVERLOADs; the functions must be available to resolve
25902 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
25903 inner_expr
= stmt_expr_value_expr (inner_expr
);
25904 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
25905 inner_expr
= TREE_OPERAND (inner_expr
, 0);
25906 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
25907 inner_expr
= TREE_OPERAND (inner_expr
, 1);
25908 if (is_overloaded_fn (inner_expr
)
25909 || TREE_CODE (inner_expr
) == OFFSET_REF
)
25911 /* There is no need to return a proxy for a variable or enumerator. */
25912 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
25914 /* Preserve string constants; conversions from string constants to
25915 "char *" are allowed, even though normally a "const char *"
25916 cannot be used to initialize a "char *". */
25917 if (TREE_CODE (expr
) == STRING_CST
)
25919 /* Preserve void and arithmetic constants, as an optimization -- there is no
25920 reason to create a new node. */
25921 if (TREE_CODE (expr
) == VOID_CST
25922 || TREE_CODE (expr
) == INTEGER_CST
25923 || TREE_CODE (expr
) == REAL_CST
)
25925 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25926 There is at least one place where we want to know that a
25927 particular expression is a throw-expression: when checking a ?:
25928 expression, there are special rules if the second or third
25929 argument is a throw-expression. */
25930 if (TREE_CODE (expr
) == THROW_EXPR
)
25933 /* Don't wrap an initializer list, we need to be able to look inside. */
25934 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
25937 /* Don't wrap a dummy object, we need to be able to test for it. */
25938 if (is_dummy_object (expr
))
25941 if (TREE_CODE (expr
) == COND_EXPR
)
25942 return build3 (COND_EXPR
,
25944 TREE_OPERAND (expr
, 0),
25945 (TREE_OPERAND (expr
, 1)
25946 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
25947 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
25948 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
25949 if (TREE_CODE (expr
) == COMPOUND_EXPR
25950 && !COMPOUND_EXPR_OVERLOADED (expr
))
25951 return build2 (COMPOUND_EXPR
,
25953 TREE_OPERAND (expr
, 0),
25954 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
25956 /* If the type is unknown, it can't really be non-dependent */
25957 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
25959 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25960 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
25961 TREE_TYPE (expr
), expr
);
25964 /* ARGS is a vector of expressions as arguments to a function call.
25965 Replace the arguments with equivalent non-dependent expressions.
25966 This modifies ARGS in place. */
25969 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
25974 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
25976 tree newarg
= build_non_dependent_expr (arg
);
25978 (*args
)[ix
] = newarg
;
25982 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25983 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25984 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25987 make_auto_1 (tree name
, bool set_canonical
)
25989 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
25990 TYPE_NAME (au
) = build_decl (input_location
,
25991 TYPE_DECL
, name
, au
);
25992 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
25993 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
25994 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
25995 TYPE_NAME (au
), NULL_TREE
);
25997 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
25998 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
25999 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26005 make_decltype_auto (void)
26007 return make_auto_1 (decltype_auto_identifier
, true);
26013 return make_auto_1 (auto_identifier
, true);
26016 /* Return a C++17 deduction placeholder for class template TMPL. */
26019 make_template_placeholder (tree tmpl
)
26021 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
26022 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26026 /* True iff T is a C++17 class template deduction placeholder. */
26029 template_placeholder_p (tree t
)
26031 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26034 /* Make a "constrained auto" type-specifier. This is an
26035 auto type with constraints that must be associated after
26036 deduction. The constraint is formed from the given
26037 CONC and its optional sequence of arguments, which are
26038 non-null if written as partial-concept-id. */
26041 make_constrained_auto (tree con
, tree args
)
26043 tree type
= make_auto_1 (auto_identifier
, false);
26045 /* Build the constraint. */
26046 tree tmpl
= DECL_TI_TEMPLATE (con
);
26047 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26048 expr
= build_concept_check (expr
, type
, args
);
26050 tree constr
= normalize_expression (expr
);
26051 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26053 /* Our canonical type depends on the constraint. */
26054 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26056 /* Attach the constraint to the type declaration. */
26057 tree decl
= TYPE_NAME (type
);
26061 /* Given type ARG, return std::initializer_list<ARG>. */
26066 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26068 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26070 gcc_rich_location
richloc (input_location
);
26071 maybe_add_include_fixit (&richloc
, "<initializer_list>");
26072 error_at (&richloc
,
26073 "deducing from brace-enclosed initializer list"
26074 " requires %<#include <initializer_list>%>");
26076 return error_mark_node
;
26078 tree argvec
= make_tree_vec (1);
26079 TREE_VEC_ELT (argvec
, 0) = arg
;
26081 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26082 NULL_TREE
, 0, tf_warning_or_error
);
26085 /* Replace auto in TYPE with std::initializer_list<auto>. */
26088 listify_autos (tree type
, tree auto_node
)
26090 tree init_auto
= listify (auto_node
);
26091 tree argvec
= make_tree_vec (1);
26092 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26093 if (processing_template_decl
)
26094 argvec
= add_to_template_args (current_template_args (), argvec
);
26095 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26098 /* Hash traits for hashing possibly constrained 'auto'
26099 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26101 struct auto_hash
: default_hash_traits
<tree
>
26103 static inline hashval_t
hash (tree
);
26104 static inline bool equal (tree
, tree
);
26107 /* Hash the 'auto' T. */
26110 auto_hash::hash (tree t
)
26112 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
26113 /* Matching constrained-type-specifiers denote the same template
26114 parameter, so hash the constraint. */
26115 return hash_placeholder_constraint (c
);
26117 /* But unconstrained autos are all separate, so just hash the pointer. */
26118 return iterative_hash_object (t
, 0);
26121 /* Compare two 'auto's. */
26124 auto_hash::equal (tree t1
, tree t2
)
26129 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26130 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26132 /* Two unconstrained autos are distinct. */
26136 return equivalent_placeholder_constraints (c1
, c2
);
26139 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26140 constrained) auto, add it to the vector. */
26143 extract_autos_r (tree t
, void *data
)
26145 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26148 /* All the autos were built with index 0; fix that up now. */
26149 tree
*p
= hash
.find_slot (t
, INSERT
);
26152 /* If this is a repeated constrained-type-specifier, use the index we
26154 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26157 /* Otherwise this is new, so use the current count. */
26159 idx
= hash
.elements () - 1;
26161 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26164 /* Always keep walking. */
26168 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26169 says they can appear anywhere in the type. */
26172 extract_autos (tree type
)
26174 hash_set
<tree
> visited
;
26175 hash_table
<auto_hash
> hash (2);
26177 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26179 tree tree_vec
= make_tree_vec (hash
.elements());
26180 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26181 iter
!= hash
.end(); ++iter
)
26184 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26185 TREE_VEC_ELT (tree_vec
, i
)
26186 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26192 /* The stem for deduction guide names. */
26193 const char *const dguide_base
= "__dguide_";
26195 /* Return the name for a deduction guide for class template TMPL. */
26198 dguide_name (tree tmpl
)
26200 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26201 tree tname
= TYPE_IDENTIFIER (type
);
26202 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26203 + IDENTIFIER_LENGTH (tname
));
26204 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26205 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26206 IDENTIFIER_LENGTH (tname
) + 1);
26207 tree dname
= get_identifier (buf
);
26208 TREE_TYPE (dname
) = type
;
26212 /* True if NAME is the name of a deduction guide. */
26215 dguide_name_p (tree name
)
26217 return (TREE_CODE (name
) == IDENTIFIER_NODE
26218 && TREE_TYPE (name
)
26219 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
26220 strlen (dguide_base
)));
26223 /* True if FN is a deduction guide. */
26226 deduction_guide_p (const_tree fn
)
26229 if (tree name
= DECL_NAME (fn
))
26230 return dguide_name_p (name
);
26234 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26237 copy_guide_p (const_tree fn
)
26239 gcc_assert (deduction_guide_p (fn
));
26240 if (!DECL_ARTIFICIAL (fn
))
26242 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
26243 return (TREE_CHAIN (parms
) == void_list_node
26244 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
26247 /* True if FN is a guide generated from a constructor template. */
26250 template_guide_p (const_tree fn
)
26252 gcc_assert (deduction_guide_p (fn
));
26253 if (!DECL_ARTIFICIAL (fn
))
26255 tree tmpl
= DECL_TI_TEMPLATE (fn
);
26256 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
26257 return PRIMARY_TEMPLATE_P (org
);
26261 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26262 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26263 template parameter types. Note that the handling of template template
26264 parameters relies on current_template_parms being set appropriately for the
26268 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
26269 tree tsubst_args
, tsubst_flags_t complain
)
26271 if (olddecl
== error_mark_node
)
26272 return error_mark_node
;
26274 tree oldidx
= get_template_parm_index (olddecl
);
26277 if (TREE_CODE (olddecl
) == TYPE_DECL
26278 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26280 tree oldtype
= TREE_TYPE (olddecl
);
26281 newtype
= cxx_make_type (TREE_CODE (oldtype
));
26282 TYPE_MAIN_VARIANT (newtype
) = newtype
;
26283 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
26284 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
26285 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
26289 newtype
= TREE_TYPE (olddecl
);
26290 if (type_uses_auto (newtype
))
26292 // Substitute once to fix references to other template parameters.
26293 newtype
= tsubst (newtype
, tsubst_args
,
26294 complain
|tf_partial
, NULL_TREE
);
26295 // Now substitute again to reduce the level of the auto.
26296 newtype
= tsubst (newtype
, current_template_args (),
26297 complain
, NULL_TREE
);
26300 newtype
= tsubst (newtype
, tsubst_args
,
26301 complain
, NULL_TREE
);
26305 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
26306 DECL_NAME (olddecl
), newtype
);
26307 SET_DECL_TEMPLATE_PARM_P (newdecl
);
26310 if (TREE_CODE (olddecl
) == TYPE_DECL
26311 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26313 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
26314 = build_template_parm_index (index
, level
, level
,
26316 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26317 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26318 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
26319 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
26321 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26323 DECL_TEMPLATE_RESULT (newdecl
)
26324 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
26325 DECL_NAME (olddecl
), newtype
);
26326 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
26327 // First create a copy (ttargs) of tsubst_args with an
26328 // additional level for the template template parameter's own
26329 // template parameters (ttparms).
26330 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
26331 (DECL_TEMPLATE_PARMS (olddecl
)));
26332 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
26333 tree ttargs
= make_tree_vec (depth
+ 1);
26334 for (int i
= 0; i
< depth
; ++i
)
26335 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
26336 TREE_VEC_ELT (ttargs
, depth
)
26337 = template_parms_level_to_args (ttparms
);
26338 // Substitute ttargs into ttparms to fix references to
26339 // other template parameters.
26340 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26341 complain
|tf_partial
);
26342 // Now substitute again with args based on tparms, to reduce
26343 // the level of the ttparms.
26344 ttargs
= current_template_args ();
26345 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26347 // Finally, tack the adjusted parms onto tparms.
26348 ttparms
= tree_cons (size_int (depth
), ttparms
,
26349 current_template_parms
);
26350 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
26355 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
26357 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
26358 TREE_CODE (oldconst
),
26359 DECL_NAME (oldconst
), newtype
);
26360 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
26361 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
26362 SET_DECL_TEMPLATE_PARM_P (newconst
);
26363 newidx
= build_template_parm_index (index
, level
, level
,
26364 newconst
, newtype
);
26365 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26366 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26367 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
26373 /* Returns a C++17 class deduction guide template based on the constructor
26374 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26375 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26378 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
26380 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
26381 bool memtmpl
= false;
26384 tree fn_tmpl
= NULL_TREE
;
26389 bool copy_p
= TYPE_REF_P (type
);
26392 type
= TREE_TYPE (type
);
26393 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
26396 fparms
= void_list_node
;
26398 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
26399 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
26400 targs
= CLASSTYPE_TI_ARGS (type
);
26403 loc
= DECL_SOURCE_LOCATION (ctmpl
);
26404 explicit_p
= false;
26408 ++processing_template_decl
;
26412 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
26413 : DECL_TI_TEMPLATE (ctor
));
26415 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
26416 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
26418 type
= DECL_CONTEXT (ctor
);
26420 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
26421 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26422 fully specialized args for the enclosing class. Strip those off, as
26423 the deduction guide won't have those template parameters. */
26424 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
26425 TMPL_PARMS_DEPTH (tparms
));
26426 /* Discard the 'this' parameter. */
26427 fparms
= FUNCTION_ARG_CHAIN (ctor
);
26428 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
26429 ci
= get_constraints (ctor
);
26430 loc
= DECL_SOURCE_LOCATION (ctor
);
26431 explicit_p
= DECL_NONCONVERTING_P (ctor
);
26433 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
26437 /* For a member template constructor, we need to flatten the two
26438 template parameter lists into one, and then adjust the function
26439 signature accordingly. This gets...complicated. */
26440 tree save_parms
= current_template_parms
;
26442 /* For a member template we should have two levels of parms/args, one
26443 for the class and one for the constructor. We stripped
26444 specialized args for further enclosing classes above. */
26445 const int depth
= 2;
26446 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
26448 /* Template args for translating references to the two-level template
26449 parameters into references to the one-level template parameters we
26451 tree tsubst_args
= copy_node (targs
);
26452 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
26453 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
26455 /* Template parms for the constructor template. */
26456 tree ftparms
= TREE_VALUE (tparms
);
26457 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
26458 /* Template parms for the class template. */
26459 tparms
= TREE_CHAIN (tparms
);
26460 tree ctparms
= TREE_VALUE (tparms
);
26461 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
26462 /* Template parms for the deduction guide start as a copy of the
26463 template parms for the class. We set current_template_parms for
26464 lookup_template_class_1. */
26465 current_template_parms
= tparms
= copy_node (tparms
);
26466 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
26467 for (unsigned i
= 0; i
< clen
; ++i
)
26468 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
26470 /* Now we need to rewrite the constructor parms to append them to the
26472 for (unsigned i
= 0; i
< flen
; ++i
)
26474 unsigned index
= i
+ clen
;
26475 unsigned level
= 1;
26476 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
26477 tree olddecl
= TREE_VALUE (oldelt
);
26478 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
26479 tsubst_args
, complain
);
26480 if (newdecl
== error_mark_node
)
26482 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
26483 tsubst_args
, complain
, ctor
);
26484 tree list
= build_tree_list (newdef
, newdecl
);
26485 TEMPLATE_PARM_CONSTRAINTS (list
)
26486 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
26487 tsubst_args
, complain
, ctor
);
26488 TREE_VEC_ELT (new_vec
, index
) = list
;
26489 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
26492 /* Now we have a final set of template parms to substitute into the
26493 function signature. */
26494 targs
= template_parms_to_args (tparms
);
26495 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
26497 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
26499 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
26501 current_template_parms
= save_parms
;
26504 --processing_template_decl
;
26506 return error_mark_node
;
26511 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26512 tparms
= copy_node (tparms
);
26513 INNERMOST_TEMPLATE_PARMS (tparms
)
26514 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
26517 tree fntype
= build_function_type (type
, fparms
);
26518 tree ded_fn
= build_lang_decl_loc (loc
,
26520 dguide_name (type
), fntype
);
26521 DECL_ARGUMENTS (ded_fn
) = fargs
;
26522 DECL_ARTIFICIAL (ded_fn
) = true;
26523 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
26524 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
26525 DECL_ARTIFICIAL (ded_tmpl
) = true;
26526 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
26527 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
26528 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
26529 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
26531 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
26533 set_constraints (ded_tmpl
, ci
);
26538 /* Deduce template arguments for the class template placeholder PTYPE for
26539 template TMPL based on the initializer INIT, and return the resulting
26543 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
26544 tsubst_flags_t complain
)
26546 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
26548 /* We should have handled this in the caller. */
26549 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26551 if (complain
& tf_error
)
26552 error ("non-class template %qT used without template arguments", tmpl
);
26553 return error_mark_node
;
26556 tree type
= TREE_TYPE (tmpl
);
26558 bool try_list_ctor
= false;
26560 vec
<tree
,va_gc
> *args
;
26561 if (init
== NULL_TREE
26562 || TREE_CODE (init
) == TREE_LIST
)
26563 args
= make_tree_vector_from_list (init
);
26564 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26566 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
26567 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
26569 /* As an exception, the first phase in 16.3.1.7 (considering the
26570 initializer list as a single argument) is omitted if the
26571 initializer list consists of a single expression of type cv U,
26572 where U is a specialization of C or a class derived from a
26573 specialization of C. */
26574 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
26575 tree etype
= TREE_TYPE (elt
);
26577 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
26578 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26579 int err
= unify (tparms
, targs
, type
, etype
,
26580 UNIFY_ALLOW_DERIVED
, /*explain*/false);
26582 try_list_ctor
= false;
26585 if (try_list_ctor
|| is_std_init_list (type
))
26586 args
= make_tree_vector_single (init
);
26588 args
= make_tree_vector_from_ctor (init
);
26591 args
= make_tree_vector_single (init
);
26593 tree dname
= dguide_name (tmpl
);
26594 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
26595 /*type*/false, /*complain*/false,
26597 bool elided
= false;
26598 if (cands
== error_mark_node
)
26601 /* Prune explicit deduction guides in copy-initialization context. */
26602 if (flags
& LOOKUP_ONLYCONVERTING
)
26604 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
26605 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26610 /* Found a nonconverting guide, prune the candidates. */
26611 tree pruned
= NULL_TREE
;
26612 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26613 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
26614 pruned
= lookup_add (*iter
, pruned
);
26620 tree outer_args
= NULL_TREE
;
26621 if (DECL_CLASS_SCOPE_P (tmpl
)
26622 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
26624 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
26625 type
= TREE_TYPE (most_general_template (tmpl
));
26628 bool saw_ctor
= false;
26629 // FIXME cache artificial deduction guides
26630 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
26632 /* Skip inherited constructors. */
26633 if (iter
.using_p ())
26636 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
26637 if (guide
== error_mark_node
)
26638 return error_mark_node
;
26639 if ((flags
& LOOKUP_ONLYCONVERTING
)
26640 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
26643 cands
= lookup_add (guide
, cands
);
26648 tree call
= error_mark_node
;
26650 /* If this is list-initialization and the class has a list constructor, first
26651 try deducing from the list as a single argument, as [over.match.list]. */
26652 tree list_cands
= NULL_TREE
;
26653 if (try_list_ctor
&& cands
)
26654 for (lkp_iterator
iter (cands
); iter
; ++iter
)
26657 if (is_list_ctor (dg
))
26658 list_cands
= lookup_add (dg
, list_cands
);
26662 ++cp_unevaluated_operand
;
26663 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
26664 --cp_unevaluated_operand
;
26666 if (call
== error_mark_node
)
26668 /* That didn't work, now try treating the list as a sequence of
26670 release_tree_vector (args
);
26671 args
= make_tree_vector_from_ctor (init
);
26675 /* Maybe generate an implicit deduction guide. */
26676 if (call
== error_mark_node
&& args
->length () < 2)
26678 tree gtype
= NULL_TREE
;
26680 if (args
->length () == 1)
26681 /* Generate a copy guide. */
26682 gtype
= build_reference_type (type
);
26683 else if (!saw_ctor
)
26684 /* Generate a default guide. */
26689 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
26690 if (guide
== error_mark_node
)
26691 return error_mark_node
;
26692 cands
= lookup_add (guide
, cands
);
26696 if (elided
&& !cands
)
26698 error ("cannot deduce template arguments for copy-initialization"
26699 " of %qT, as it has no non-explicit deduction guides or "
26700 "user-declared constructors", type
);
26701 return error_mark_node
;
26703 else if (!cands
&& call
== error_mark_node
)
26705 error ("cannot deduce template arguments of %qT, as it has no viable "
26706 "deduction guides", type
);
26707 return error_mark_node
;
26710 if (call
== error_mark_node
)
26712 ++cp_unevaluated_operand
;
26713 call
= build_new_function_call (cands
, &args
, tf_decltype
);
26714 --cp_unevaluated_operand
;
26717 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
26719 error ("class template argument deduction failed:");
26721 ++cp_unevaluated_operand
;
26722 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
26723 --cp_unevaluated_operand
;
26726 inform (input_location
, "explicit deduction guides not considered "
26727 "for copy-initialization");
26730 release_tree_vector (args
);
26732 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
26735 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26736 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26737 The CONTEXT determines the context in which auto deduction is performed
26738 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26739 OUTER_TARGS are used during template argument deduction
26740 (context == adc_unify) to properly substitute the result, and is ignored
26743 For partial-concept-ids, extra args may be appended to the list of deduced
26744 template arguments prior to determining constraint satisfaction. */
26747 do_auto_deduction (tree type
, tree init
, tree auto_node
,
26748 tsubst_flags_t complain
, auto_deduction_context context
,
26749 tree outer_targs
, int flags
)
26753 if (init
== error_mark_node
)
26754 return error_mark_node
;
26756 if (init
&& type_dependent_expression_p (init
)
26757 && context
!= adc_unify
)
26758 /* Defining a subset of type-dependent expressions that we can deduce
26759 from ahead of time isn't worth the trouble. */
26762 /* Similarly, we can't deduce from another undeduced decl. */
26763 if (init
&& undeduced_auto_decl (init
))
26766 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
26767 /* C++17 class template argument deduction. */
26768 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
26770 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
26771 /* Nothing we can do with this, even in deduction context. */
26774 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26775 with either a new invented type template parameter U or, if the
26776 initializer is a braced-init-list (8.5.4), with
26777 std::initializer_list<U>. */
26778 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
26780 if (!DIRECT_LIST_INIT_P (init
))
26781 type
= listify_autos (type
, auto_node
);
26782 else if (CONSTRUCTOR_NELTS (init
) == 1)
26783 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
26786 if (complain
& tf_warning_or_error
)
26788 if (permerror (input_location
, "direct-list-initialization of "
26789 "%<auto%> requires exactly one element"))
26790 inform (input_location
,
26791 "for deduction to %<std::initializer_list%>, use copy-"
26792 "list-initialization (i.e. add %<=%> before the %<{%>)");
26794 type
= listify_autos (type
, auto_node
);
26798 if (type
== error_mark_node
)
26799 return error_mark_node
;
26801 init
= resolve_nondeduced_context (init
, complain
);
26803 if (context
== adc_decomp_type
26804 && auto_node
== type
26805 && init
!= error_mark_node
26806 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
26807 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26808 and initializer has array type, deduce cv-qualified array type. */
26809 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
26811 else if (AUTO_IS_DECLTYPE (auto_node
))
26813 bool id
= (DECL_P (init
)
26814 || ((TREE_CODE (init
) == COMPONENT_REF
26815 || TREE_CODE (init
) == SCOPE_REF
)
26816 && !REF_PARENTHESIZED_P (init
)));
26817 targs
= make_tree_vec (1);
26818 TREE_VEC_ELT (targs
, 0)
26819 = finish_decltype_type (init
, id
, tf_warning_or_error
);
26820 if (type
!= auto_node
)
26822 if (complain
& tf_error
)
26823 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
26824 return error_mark_node
;
26829 tree parms
= build_tree_list (NULL_TREE
, type
);
26833 tparms
= extract_autos (type
);
26836 tparms
= make_tree_vec (1);
26837 TREE_VEC_ELT (tparms
, 0)
26838 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
26841 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
26842 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26844 NULL
, /*explain_p=*/false);
26847 if (processing_template_decl
)
26848 /* Try again at instantiation time. */
26850 if (type
&& type
!= error_mark_node
26851 && (complain
& tf_error
))
26852 /* If type is error_mark_node a diagnostic must have been
26853 emitted by now. Also, having a mention to '<type error>'
26854 in the diagnostic is not really useful to the user. */
26856 if (cfun
&& auto_node
== current_function_auto_return_pattern
26857 && LAMBDA_FUNCTION_P (current_function_decl
))
26858 error ("unable to deduce lambda return type from %qE", init
);
26860 error ("unable to deduce %qT from %qE", type
, init
);
26861 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
26863 NULL
, /*explain_p=*/true);
26865 return error_mark_node
;
26869 /* Check any placeholder constraints against the deduced type. */
26870 if (flag_concepts
&& !processing_template_decl
)
26871 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
26873 /* Use the deduced type to check the associated constraints. If we
26874 have a partial-concept-id, rebuild the argument list so that
26875 we check using the extra arguments. */
26876 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
26877 tree cargs
= CHECK_CONSTR_ARGS (constr
);
26878 if (TREE_VEC_LENGTH (cargs
) > 1)
26880 cargs
= copy_node (cargs
);
26881 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
26885 if (!constraints_satisfied_p (constr
, cargs
))
26887 if (complain
& tf_warning_or_error
)
26891 case adc_unspecified
:
26893 error("placeholder constraints not satisfied");
26895 case adc_variable_type
:
26896 case adc_decomp_type
:
26897 error ("deduced initializer does not satisfy "
26898 "placeholder constraints");
26900 case adc_return_type
:
26901 error ("deduced return type does not satisfy "
26902 "placeholder constraints");
26904 case adc_requirement
:
26905 error ("deduced expression type does not satisfy "
26906 "placeholder constraints");
26909 diagnose_constraints (input_location
, constr
, targs
);
26911 return error_mark_node
;
26915 if (processing_template_decl
&& context
!= adc_unify
)
26916 outer_targs
= current_template_args ();
26917 targs
= add_to_template_args (outer_targs
, targs
);
26918 return tsubst (type
, targs
, complain
, NULL_TREE
);
26921 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26925 splice_late_return_type (tree type
, tree late_return_type
)
26927 if (is_auto (type
))
26929 if (late_return_type
)
26930 return late_return_type
;
26932 tree idx
= get_template_parm_index (type
);
26933 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
26934 /* In an abbreviated function template we didn't know we were dealing
26935 with a function template when we saw the auto return type, so update
26936 it to have the correct level. */
26937 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
26942 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26943 'decltype(auto)' or a deduced class template. */
26946 is_auto (const_tree type
)
26948 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
26949 && (TYPE_IDENTIFIER (type
) == auto_identifier
26950 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
26951 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
26957 /* for_each_template_parm callback for type_uses_auto. */
26960 is_auto_r (tree tp
, void */
*data*/
)
26962 return is_auto (tp
);
26965 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26966 a use of `auto'. Returns NULL_TREE otherwise. */
26969 type_uses_auto (tree type
)
26971 if (type
== NULL_TREE
)
26973 else if (flag_concepts
)
26975 /* The Concepts TS allows multiple autos in one type-specifier; just
26976 return the first one we find, do_auto_deduction will collect all of
26978 if (uses_template_parms (type
))
26979 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
26980 /*visited*/NULL
, /*nondeduced*/false);
26985 return find_type_usage (type
, is_auto
);
26988 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
26989 concepts are enabled, auto is acceptable in template arguments, but
26990 only when TEMPL identifies a template class. Return TRUE if any
26991 such errors were reported. */
26994 check_auto_in_tmpl_args (tree tmpl
, tree args
)
26996 /* If there were previous errors, nevermind. */
26997 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27000 /* If TMPL is an identifier, we're parsing and we can't tell yet
27001 whether TMPL is supposed to be a type, a function or a variable.
27002 We'll only be able to tell during template substitution, so we
27003 expect to be called again then. If concepts are enabled and we
27004 know we have a type, we're ok. */
27006 && (identifier_p (tmpl
)
27008 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27009 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27012 /* Quickly search for any occurrences of auto; usually there won't
27013 be any, and then we'll avoid allocating the vector. */
27014 if (!type_uses_auto (args
))
27017 bool errors
= false;
27019 tree vec
= extract_autos (args
);
27020 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27022 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27023 error_at (DECL_SOURCE_LOCATION (xauto
),
27024 "invalid use of %qT in template argument", xauto
);
27031 /* For a given template T, return the vector of typedefs referenced
27032 in T for which access check is needed at T instantiation time.
27033 T is either a FUNCTION_DECL or a RECORD_TYPE.
27034 Those typedefs were added to T by the function
27035 append_type_to_template_for_access_check. */
27037 vec
<qualified_typedef_usage_t
, va_gc
> *
27038 get_types_needing_access_check (tree t
)
27041 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27043 if (!t
|| t
== error_mark_node
)
27046 if (!(ti
= get_template_info (t
)))
27049 if (CLASS_TYPE_P (t
)
27050 || TREE_CODE (t
) == FUNCTION_DECL
)
27052 if (!TI_TEMPLATE (ti
))
27055 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27061 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27062 tied to T. That list of typedefs will be access checked at
27063 T instantiation time.
27064 T is either a FUNCTION_DECL or a RECORD_TYPE.
27065 TYPE_DECL is a TYPE_DECL node representing a typedef.
27066 SCOPE is the scope through which TYPE_DECL is accessed.
27067 LOCATION is the location of the usage point of TYPE_DECL.
27069 This function is a subroutine of
27070 append_type_to_template_for_access_check. */
27073 append_type_to_template_for_access_check_1 (tree t
,
27076 location_t location
)
27078 qualified_typedef_usage_t typedef_usage
;
27081 if (!t
|| t
== error_mark_node
)
27084 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27085 || CLASS_TYPE_P (t
))
27087 && TREE_CODE (type_decl
) == TYPE_DECL
27090 if (!(ti
= get_template_info (t
)))
27093 gcc_assert (TI_TEMPLATE (ti
));
27095 typedef_usage
.typedef_decl
= type_decl
;
27096 typedef_usage
.context
= scope
;
27097 typedef_usage
.locus
= location
;
27099 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27102 /* Append TYPE_DECL to the template TEMPL.
27103 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27104 At TEMPL instanciation time, TYPE_DECL will be checked to see
27105 if it can be accessed through SCOPE.
27106 LOCATION is the location of the usage point of TYPE_DECL.
27108 e.g. consider the following code snippet:
27115 template<class U> struct S
27117 C::myint mi; // <-- usage point of the typedef C::myint
27122 At S<char> instantiation time, we need to check the access of C::myint
27123 In other words, we need to check the access of the myint typedef through
27124 the C scope. For that purpose, this function will add the myint typedef
27125 and the scope C through which its being accessed to a list of typedefs
27126 tied to the template S. That list will be walked at template instantiation
27127 time and access check performed on each typedefs it contains.
27128 Note that this particular code snippet should yield an error because
27129 myint is private to C. */
27132 append_type_to_template_for_access_check (tree templ
,
27135 location_t location
)
27137 qualified_typedef_usage_t
*iter
;
27140 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27142 /* Make sure we don't append the type to the template twice. */
27143 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27144 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27147 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27151 /* Convert the generic type parameters in PARM that match the types given in the
27152 range [START_IDX, END_IDX) from the current_template_parms into generic type
27156 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27158 tree current
= current_template_parms
;
27159 int depth
= TMPL_PARMS_DEPTH (current
);
27160 current
= INNERMOST_TEMPLATE_PARMS (current
);
27161 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27163 for (int i
= 0; i
< start_idx
; ++i
)
27164 TREE_VEC_ELT (replacement
, i
)
27165 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27167 for (int i
= start_idx
; i
< end_idx
; ++i
)
27169 /* Create a distinct parameter pack type from the current parm and add it
27170 to the replacement args to tsubst below into the generic function
27173 tree o
= TREE_TYPE (TREE_VALUE
27174 (TREE_VEC_ELT (current
, i
)));
27175 tree t
= copy_type (o
);
27176 TEMPLATE_TYPE_PARM_INDEX (t
)
27177 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27179 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27180 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27181 TYPE_MAIN_VARIANT (t
) = t
;
27182 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27183 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27184 TREE_VEC_ELT (replacement
, i
) = t
;
27185 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27188 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27189 TREE_VEC_ELT (replacement
, i
)
27190 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27192 /* If there are more levels then build up the replacement with the outer
27195 replacement
= add_to_template_args (template_parms_to_args
27196 (TREE_CHAIN (current_template_parms
)),
27199 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
27202 /* Entries in the decl_constraint hash table. */
27203 struct GTY((for_user
)) constr_entry
27209 /* Hashing function and equality for constraint entries. */
27210 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
27212 static hashval_t
hash (constr_entry
*e
)
27214 return (hashval_t
)DECL_UID (e
->decl
);
27217 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
27219 return e1
->decl
== e2
->decl
;
27223 /* A mapping from declarations to constraint information. Note that
27224 both templates and their underlying declarations are mapped to the
27225 same constraint information.
27227 FIXME: This is defined in pt.c because garbage collection
27228 code is not being generated for constraint.cc. */
27230 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
27232 /* Returns the template constraints of declaration T. If T is not
27233 constrained, return NULL_TREE. Note that T must be non-null. */
27236 get_constraints (tree t
)
27238 if (!flag_concepts
)
27241 gcc_assert (DECL_P (t
));
27242 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27243 t
= DECL_TEMPLATE_RESULT (t
);
27244 constr_entry elt
= { t
, NULL_TREE
};
27245 constr_entry
* found
= decl_constraints
->find (&elt
);
27252 /* Associate the given constraint information CI with the declaration
27253 T. If T is a template, then the constraints are associated with
27254 its underlying declaration. Don't build associations if CI is
27258 set_constraints (tree t
, tree ci
)
27262 gcc_assert (t
&& flag_concepts
);
27263 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27264 t
= DECL_TEMPLATE_RESULT (t
);
27265 gcc_assert (!get_constraints (t
));
27266 constr_entry elt
= {t
, ci
};
27267 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
27268 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
27273 /* Remove the associated constraints of the declaration T. */
27276 remove_constraints (tree t
)
27278 gcc_assert (DECL_P (t
));
27279 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27280 t
= DECL_TEMPLATE_RESULT (t
);
27282 constr_entry elt
= {t
, NULL_TREE
};
27283 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
27285 decl_constraints
->clear_slot (slot
);
27288 /* Memoized satisfaction results for declarations. This
27289 maps the pair (constraint_info, arguments) to the result computed
27290 by constraints_satisfied_p. */
27292 struct GTY((for_user
)) constraint_sat_entry
27299 /* Hashing function and equality for constraint entries. */
27301 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
27303 static hashval_t
hash (constraint_sat_entry
*e
)
27305 hashval_t val
= iterative_hash_object(e
->ci
, 0);
27306 return iterative_hash_template_arg (e
->args
, val
);
27309 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
27311 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
27315 /* Memoized satisfaction results for concept checks. */
27317 struct GTY((for_user
)) concept_spec_entry
27324 /* Hashing function and equality for constraint entries. */
27326 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
27328 static hashval_t
hash (concept_spec_entry
*e
)
27330 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
27333 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
27335 ++comparing_specializations
;
27336 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
27337 --comparing_specializations
;
27342 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
27343 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
27345 /* Search for a memoized satisfaction result. Returns one of the
27346 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27349 lookup_constraint_satisfaction (tree ci
, tree args
)
27351 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
27352 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
27354 return found
->result
;
27359 /* Memoize the result of a satisfication test. Returns the saved result. */
27362 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
27364 constraint_sat_entry elt
= {ci
, args
, result
};
27365 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
27366 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
27372 /* Search for a memoized satisfaction result for a concept. */
27375 lookup_concept_satisfaction (tree tmpl
, tree args
)
27377 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27378 concept_spec_entry
* found
= concept_memos
->find (&elt
);
27380 return found
->result
;
27385 /* Memoize the result of a concept check. Returns the saved result. */
27388 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
27390 concept_spec_entry elt
= {tmpl
, args
, result
};
27391 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
27392 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27398 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
27400 /* Returns a prior concept specialization. This returns the substituted
27401 and normalized constraints defined by the concept. */
27404 get_concept_expansion (tree tmpl
, tree args
)
27406 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27407 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
27409 return found
->result
;
27414 /* Save a concept expansion for later. */
27417 save_concept_expansion (tree tmpl
, tree args
, tree def
)
27419 concept_spec_entry elt
= {tmpl
, args
, def
};
27420 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
27421 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27428 hash_subsumption_args (tree t1
, tree t2
)
27430 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
27431 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
27433 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
27434 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
27435 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
27436 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
27440 /* Compare the constraints of two subsumption entries. The LEFT1 and
27441 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27442 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27445 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
27447 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
27448 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
27449 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
27450 CHECK_CONSTR_ARGS (right1
)))
27451 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
27452 CHECK_CONSTR_ARGS (right2
));
27456 /* Key/value pair for learning and memoizing subsumption results. This
27457 associates a pair of check constraints (including arguments) with
27458 a boolean value indicating the result. */
27460 struct GTY((for_user
)) subsumption_entry
27467 /* Hashing function and equality for constraint entries. */
27469 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
27471 static hashval_t
hash (subsumption_entry
*e
)
27473 return hash_subsumption_args (e
->t1
, e
->t2
);
27476 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
27478 ++comparing_specializations
;
27479 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
27480 --comparing_specializations
;
27485 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
27487 /* Search for a previously cached subsumption result. */
27490 lookup_subsumption_result (tree t1
, tree t2
)
27492 subsumption_entry elt
= { t1
, t2
, false };
27493 subsumption_entry
* found
= subsumption_table
->find (&elt
);
27495 return &found
->result
;
27500 /* Save a subsumption result. */
27503 save_subsumption_result (tree t1
, tree t2
, bool result
)
27505 subsumption_entry elt
= {t1
, t2
, result
};
27506 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
27507 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
27513 /* Set up the hash table for constraint association. */
27516 init_constraint_processing (void)
27518 if (!flag_concepts
)
27521 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
27522 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
27523 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27524 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
27525 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
27528 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27532 declare_integer_pack (void)
27534 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
27535 build_function_type_list (integer_type_node
,
27538 NULL_TREE
, ECF_CONST
);
27539 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
27540 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
27541 DECL_FUNCTION_CODE (ipfn
)
27542 = (enum built_in_function
) (int) CP_BUILT_IN_INTEGER_PACK
;
27545 /* Set up the hash tables for template instantiations. */
27548 init_template_processing (void)
27550 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27551 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
27553 if (cxx_dialect
>= cxx11
)
27554 declare_integer_pack ();
27557 /* Print stats about the template hash tables for -fstats. */
27560 print_template_statistics (void)
27562 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
27563 "%f collisions\n", (long) decl_specializations
->size (),
27564 (long) decl_specializations
->elements (),
27565 decl_specializations
->collisions ());
27566 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
27567 "%f collisions\n", (long) type_specializations
->size (),
27568 (long) type_specializations
->elements (),
27569 type_specializations
->collisions ());
27574 namespace selftest
{
27576 /* Verify that build_non_dependent_expr () works, for various expressions,
27577 and that location wrappers don't affect the results. */
27580 test_build_non_dependent_expr ()
27582 location_t loc
= BUILTINS_LOCATION
;
27584 /* Verify constants, without and with location wrappers. */
27585 tree int_cst
= build_int_cst (integer_type_node
, 42);
27586 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
27588 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
27589 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
27590 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
27592 tree string_lit
= build_string (4, "foo");
27593 TREE_TYPE (string_lit
) = char_array_type_node
;
27594 string_lit
= fix_string_type (string_lit
);
27595 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
27597 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
27598 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
27599 ASSERT_EQ (wrapped_string_lit
,
27600 build_non_dependent_expr (wrapped_string_lit
));
27603 /* Verify that type_dependent_expression_p () works correctly, even
27604 in the presence of location wrapper nodes. */
27607 test_type_dependent_expression_p ()
27609 location_t loc
= BUILTINS_LOCATION
;
27611 tree name
= get_identifier ("foo");
27613 /* If no templates are involved, nothing is type-dependent. */
27614 gcc_assert (!processing_template_decl
);
27615 ASSERT_FALSE (type_dependent_expression_p (name
));
27617 ++processing_template_decl
;
27619 /* Within a template, an unresolved name is always type-dependent. */
27620 ASSERT_TRUE (type_dependent_expression_p (name
));
27622 /* Ensure it copes with NULL_TREE and errors. */
27623 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
27624 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
27626 /* A USING_DECL in a template should be type-dependent, even if wrapped
27627 with a location wrapper (PR c++/83799). */
27628 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
27629 TREE_TYPE (using_decl
) = integer_type_node
;
27630 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
27631 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
27632 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
27633 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
27635 --processing_template_decl
;
27638 /* Run all of the selftests within this file. */
27643 test_build_non_dependent_expr ();
27644 test_type_dependent_expression_p ();
27647 } // namespace selftest
27649 #endif /* #if CHECKING_P */
27651 #include "gt-cp-pt.h"