1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
29 #include "coretypes.h"
32 #include "stringpool.h"
35 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
43 #include "gcc-rich-location.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
55 struct pending_template
*next
;
56 struct tinst_level
*tinst
;
59 static GTY(()) struct pending_template
*pending_templates
;
60 static GTY(()) struct pending_template
*last_pending_template
;
62 int processing_template_parmlist
;
63 static int template_header_count
;
65 static GTY(()) tree saved_trees
;
66 static vec
<int> inline_parm_levels
;
68 static GTY(()) struct tinst_level
*current_tinst_level
;
70 static GTY(()) tree saved_access_scope
;
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr
;
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
80 // Implementation of the RAII helper for creating new local
82 local_specialization_stack::local_specialization_stack (lss_policy policy
)
83 : saved (local_specializations
)
85 if (policy
== lss_blank
|| !saved
)
86 local_specializations
= new hash_map
<tree
, tree
>;
88 local_specializations
= new hash_map
<tree
, tree
>(*saved
);
91 local_specialization_stack::~local_specialization_stack ()
93 delete local_specializations
;
94 local_specializations
= saved
;
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth
;
100 struct GTY((for_user
)) spec_entry
107 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
109 static hashval_t
hash (spec_entry
*);
110 static bool equal (spec_entry
*, spec_entry
*);
113 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
115 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132 enum template_base_result
{
134 tbr_ambiguous_baseclass
,
138 static void push_access_scope (tree
);
139 static void pop_access_scope (tree
);
140 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
141 unification_kind_t
, int,
143 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
144 unification_kind_t
, int, bool, bool);
145 static int unify (tree
, tree
, tree
, tree
, int, bool);
146 static void add_pending_template (tree
);
147 static tree
reopen_tinst_level (struct tinst_level
*);
148 static tree
tsubst_initializer_list (tree
, tree
);
149 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
150 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
152 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
154 static void tsubst_enum (tree
, tree
, tree
);
155 static tree
add_to_template_args (tree
, tree
);
156 static tree
add_outermost_template_args (tree
, tree
);
157 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
158 static int check_non_deducible_conversion (tree
, tree
, int, int,
159 struct conversion
**, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
162 static int type_unification_real (tree
, tree
, tree
, const tree
*,
163 unsigned int, int, unification_kind_t
,
164 vec
<deferred_access_check
, va_gc
> **,
166 static void note_template_header (int);
167 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
168 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
169 static tree
convert_template_argument (tree
, tree
, tree
,
170 tsubst_flags_t
, int, tree
);
171 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
172 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
173 static tree
expand_template_argument_pack (tree
);
174 static tree
build_template_parm_index (int, int, int, tree
, tree
);
175 static bool inline_needs_template_parms (tree
, bool);
176 static void push_inline_template_parms_recursive (tree
, int);
177 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
178 static int mark_template_parm (tree
, void *);
179 static int template_parm_this_level_p (tree
, void *);
180 static tree
tsubst_friend_function (tree
, tree
);
181 static tree
tsubst_friend_class (tree
, tree
);
182 static int can_complete_type_without_circularity (tree
);
183 static tree
get_bindings (tree
, tree
, tree
, bool);
184 static int template_decl_level (tree
);
185 static int check_cv_quals_for_unify (int, tree
, tree
);
186 static void template_parm_level_and_index (tree
, int*, int*);
187 static int unify_pack_expansion (tree
, tree
, tree
,
188 tree
, unification_kind_t
, bool, bool);
189 static tree
copy_template_args (tree
);
190 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
191 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
192 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
193 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
194 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
195 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
196 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
197 static bool check_specialization_scope (void);
198 static tree
process_partial_specialization (tree
);
199 static void set_current_access_from_decl (tree
);
200 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
202 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
203 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
205 static bool template_template_parm_bindings_ok_p (tree
, tree
);
206 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
207 static tree
for_each_template_parm_r (tree
*, int *, void *);
208 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
209 static void copy_default_args_to_explicit_spec (tree
);
210 static bool invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
211 static bool dependent_template_arg_p (tree
);
212 static bool any_template_arguments_need_structural_equality_p (tree
);
213 static bool dependent_type_p_r (tree
);
214 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
215 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
216 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
217 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
219 static tree
listify (tree
);
220 static tree
listify_autos (tree
, tree
);
221 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
222 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
223 static bool complex_alias_template_p (const_tree tmpl
);
224 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
225 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
226 static tree
make_argument_pack (tree
);
227 static void register_parameter_specializations (tree
, tree
);
228 static tree
enclosing_instantiation_of (tree tctx
);
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
236 push_access_scope (tree t
)
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
239 || TREE_CODE (t
) == TYPE_DECL
);
241 if (DECL_FRIEND_CONTEXT (t
))
242 push_nested_class (DECL_FRIEND_CONTEXT (t
));
243 else if (DECL_CLASS_SCOPE_P (t
))
244 push_nested_class (DECL_CONTEXT (t
));
246 push_to_top_level ();
248 if (TREE_CODE (t
) == FUNCTION_DECL
)
250 saved_access_scope
= tree_cons
251 (NULL_TREE
, current_function_decl
, saved_access_scope
);
252 current_function_decl
= t
;
256 /* Restore the scope set up by push_access_scope. T is the node we
260 pop_access_scope (tree t
)
262 if (TREE_CODE (t
) == FUNCTION_DECL
)
264 current_function_decl
= TREE_VALUE (saved_access_scope
);
265 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
268 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
271 pop_from_top_level ();
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
280 finish_member_template_decl (tree decl
)
282 if (decl
== error_mark_node
)
283 return error_mark_node
;
285 gcc_assert (DECL_P (decl
));
287 if (TREE_CODE (decl
) == TYPE_DECL
)
291 type
= TREE_TYPE (decl
);
292 if (type
== error_mark_node
)
293 return error_mark_node
;
294 if (MAYBE_CLASS_TYPE_P (type
)
295 && CLASSTYPE_TEMPLATE_INFO (type
)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
298 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
299 check_member_template (tmpl
);
304 else if (TREE_CODE (decl
) == FIELD_DECL
)
305 error ("data member %qD cannot be a member template", decl
);
306 else if (DECL_TEMPLATE_INFO (decl
))
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
310 check_member_template (DECL_TI_TEMPLATE (decl
));
311 return DECL_TI_TEMPLATE (decl
);
317 error ("invalid member template declaration %qD", decl
);
319 return error_mark_node
;
322 /* Create a template info node. */
325 build_template_info (tree template_decl
, tree template_args
)
327 tree result
= make_node (TEMPLATE_INFO
);
328 TI_TEMPLATE (result
) = template_decl
;
329 TI_ARGS (result
) = template_args
;
333 /* Return the template info node corresponding to T, whatever T is. */
336 get_template_info (const_tree t
)
338 tree tinfo
= NULL_TREE
;
340 if (!t
|| t
== error_mark_node
)
343 if (TREE_CODE (t
) == NAMESPACE_DECL
344 || TREE_CODE (t
) == PARM_DECL
)
347 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
348 tinfo
= DECL_TEMPLATE_INFO (t
);
350 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
353 if (OVERLOAD_TYPE_P (t
))
354 tinfo
= TYPE_TEMPLATE_INFO (t
);
355 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
356 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
361 /* Returns the template nesting level of the indicated class TYPE.
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
380 template_class_depth (tree type
)
384 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
386 tree tinfo
= get_template_info (type
);
388 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
393 type
= CP_DECL_CONTEXT (type
);
394 else if (LAMBDA_TYPE_P (type
))
395 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
397 type
= CP_TYPE_CONTEXT (type
);
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
407 instantiates_primary_template_p (tree node
)
409 tree tinfo
= get_template_info (node
);
413 tree tmpl
= TI_TEMPLATE (tinfo
);
414 if (PRIMARY_TEMPLATE_P (tmpl
))
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
424 tree ctxt
= DECL_CONTEXT (tmpl
);
425 tree ctinfo
= get_template_info (ctxt
);
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo
))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo
)));
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
437 inline_needs_template_parms (tree decl
, bool nsdmi
)
439 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
443 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
452 push_inline_template_parms_recursive (tree parmlist
, int levels
)
454 tree parms
= TREE_VALUE (parmlist
);
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
460 ++processing_template_decl
;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl
),
463 parms
, current_template_parms
);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
466 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
468 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
470 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
472 if (error_operand_p (parm
))
475 gcc_assert (DECL_P (parm
));
477 switch (TREE_CODE (parm
))
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
500 maybe_begin_member_template_processing (tree decl
)
504 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
508 tree ctx
= DECL_CONTEXT (decl
);
509 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx
)
512 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
515 if (inline_needs_template_parms (decl
, nsdmi
))
517 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
518 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
520 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
523 parms
= TREE_CHAIN (parms
);
526 push_inline_template_parms_recursive (parms
, levels
);
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels
.safe_push (levels
);
534 /* Undo the effects of maybe_begin_member_template_processing. */
537 maybe_end_member_template_processing (void)
542 if (inline_parm_levels
.length () == 0)
545 last
= inline_parm_levels
.pop ();
546 for (i
= 0; i
< last
; ++i
)
548 --processing_template_decl
;
549 current_template_parms
= TREE_CHAIN (current_template_parms
);
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
558 add_to_template_args (tree args
, tree extra_args
)
565 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
568 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
569 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
571 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
572 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
574 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
575 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation. */
588 add_outermost_template_args (tree args
, tree extra_args
)
592 /* If there are more levels of EXTRA_ARGS than there are ARGS,
593 something very fishy is going on. */
594 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
596 /* If *all* the new arguments will be the EXTRA_ARGS, just return
598 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
601 /* For the moment, we make ARGS look like it contains fewer levels. */
602 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
604 new_args
= add_to_template_args (args
, extra_args
);
606 /* Now, we restore ARGS to its full dimensions. */
607 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
612 /* Return the N levels of innermost template arguments from the ARGS. */
615 get_innermost_template_args (tree args
, int n
)
623 /* If N is 1, just return the innermost set of template arguments. */
625 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
627 /* If we're not removing anything, just return the arguments we were
629 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
630 gcc_assert (extra_levels
>= 0);
631 if (extra_levels
== 0)
634 /* Make a new set of arguments, not containing the outer arguments. */
635 new_args
= make_tree_vec (n
);
636 for (i
= 1; i
<= n
; ++i
)
637 SET_TMPL_ARGS_LEVEL (new_args
, i
,
638 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
643 /* The inverse of get_innermost_template_args: Return all but the innermost
644 EXTRA_LEVELS levels of template arguments from the ARGS. */
647 strip_innermost_template_args (tree args
, int extra_levels
)
650 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
655 /* If N is 1, just return the outermost set of template arguments. */
657 return TMPL_ARGS_LEVEL (args
, 1);
659 /* If we're not removing anything, just return the arguments we were
661 gcc_assert (extra_levels
>= 0);
662 if (extra_levels
== 0)
665 /* Make a new set of arguments, not containing the inner arguments. */
666 new_args
= make_tree_vec (n
);
667 for (i
= 1; i
<= n
; ++i
)
668 SET_TMPL_ARGS_LEVEL (new_args
, i
,
669 TMPL_ARGS_LEVEL (args
, i
));
674 /* We've got a template header coming up; push to a new level for storing
678 begin_template_parm_list (void)
680 /* We use a non-tag-transparent scope here, which causes pushtag to
681 put tags in this scope, rather than in the enclosing class or
682 namespace scope. This is the right thing, since we want
683 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
684 global template class, push_template_decl handles putting the
685 TEMPLATE_DECL into top-level scope. For a nested template class,
688 template <class T> struct S1 {
689 template <class T> struct S2 {};
692 pushtag contains special code to insert the TEMPLATE_DECL for S2
693 at the right scope. */
694 begin_scope (sk_template_parms
, NULL
);
695 ++processing_template_decl
;
696 ++processing_template_parmlist
;
697 note_template_header (0);
699 /* Add a dummy parameter level while we process the parameter list. */
700 current_template_parms
701 = tree_cons (size_int (processing_template_decl
),
703 current_template_parms
);
706 /* This routine is called when a specialization is declared. If it is
707 invalid to declare a specialization here, an error is reported and
708 false is returned, otherwise this routine will return true. */
711 check_specialization_scope (void)
713 tree scope
= current_scope ();
717 An explicit specialization shall be declared in the namespace of
718 which the template is a member, or, for member templates, in the
719 namespace of which the enclosing class or enclosing class
720 template is a member. An explicit specialization of a member
721 function, member class or static data member of a class template
722 shall be declared in the namespace of which the class template
724 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
726 error ("explicit specialization in non-namespace scope %qD", scope
);
732 In an explicit specialization declaration for a member of a class
733 template or a member template that appears in namespace scope,
734 the member template and some of its enclosing class templates may
735 remain unspecialized, except that the declaration shall not
736 explicitly specialize a class member template if its enclosing
737 class templates are not explicitly specialized as well. */
738 if (current_template_parms
)
740 error ("enclosing class templates are not explicitly specialized");
747 /* We've just seen template <>. */
750 begin_specialization (void)
752 begin_scope (sk_template_spec
, NULL
);
753 note_template_header (1);
754 return check_specialization_scope ();
757 /* Called at then end of processing a declaration preceded by
761 end_specialization (void)
764 reset_specialization ();
767 /* Any template <>'s that we have seen thus far are not referring to a
768 function specialization. */
771 reset_specialization (void)
773 processing_specialization
= 0;
774 template_header_count
= 0;
777 /* We've just seen a template header. If SPECIALIZATION is nonzero,
778 it was of the form template <>. */
781 note_template_header (int specialization
)
783 processing_specialization
= specialization
;
784 template_header_count
++;
787 /* We're beginning an explicit instantiation. */
790 begin_explicit_instantiation (void)
792 gcc_assert (!processing_explicit_instantiation
);
793 processing_explicit_instantiation
= true;
798 end_explicit_instantiation (void)
800 gcc_assert (processing_explicit_instantiation
);
801 processing_explicit_instantiation
= false;
804 /* An explicit specialization or partial specialization of TMPL is being
805 declared. Check that the namespace in which the specialization is
806 occurring is permissible. Returns false iff it is invalid to
807 specialize TMPL in the current namespace. */
810 check_specialization_namespace (tree tmpl
)
812 tree tpl_ns
= decl_namespace_context (tmpl
);
816 An explicit specialization shall be declared in a namespace enclosing the
817 specialized template. An explicit specialization whose declarator-id is
818 not qualified shall be declared in the nearest enclosing namespace of the
819 template, or, if the namespace is inline (7.3.1), any namespace from its
820 enclosing namespace set. */
821 if (current_scope() != DECL_CONTEXT (tmpl
)
822 && !at_namespace_scope_p ())
824 error ("specialization of %qD must appear at namespace scope", tmpl
);
828 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
829 /* Same or enclosing namespace. */
833 auto_diagnostic_group d
;
834 if (permerror (input_location
,
835 "specialization of %qD in different namespace", tmpl
))
836 inform (DECL_SOURCE_LOCATION (tmpl
),
837 " from definition of %q#D", tmpl
);
842 /* SPEC is an explicit instantiation. Check that it is valid to
843 perform this explicit instantiation in the current namespace. */
846 check_explicit_instantiation_namespace (tree spec
)
850 /* DR 275: An explicit instantiation shall appear in an enclosing
851 namespace of its template. */
852 ns
= decl_namespace_context (spec
);
853 if (!is_nested_namespace (current_namespace
, ns
))
854 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
855 "(which does not enclose namespace %qD)",
856 spec
, current_namespace
, ns
);
859 // Returns the type of a template specialization only if that
860 // specialization needs to be defined. Otherwise (e.g., if the type has
861 // already been defined), the function returns NULL_TREE.
863 maybe_new_partial_specialization (tree type
)
865 // An implicit instantiation of an incomplete type implies
866 // the definition of a new class template.
868 // template<typename T>
871 // template<typename T>
874 // Here, S<T*> is an implicit instantiation of S whose type
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
879 // It can also be the case that TYPE is a completed specialization.
880 // Continuing the previous example, suppose we also declare:
882 // template<typename T>
883 // requires Integral<T>
886 // Here, S<T*> refers to the specialization S<T*> defined
887 // above. However, we need to differentiate definitions because
888 // we intend to define a new partial specialization. In this case,
889 // we rely on the fact that the constraints are different for
890 // this declaration than that above.
892 // Note that we also get here for injected class names and
893 // late-parsed template definitions. We must ensure that we
894 // do not create new type declarations for those cases.
895 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
897 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
898 tree args
= CLASSTYPE_TI_ARGS (type
);
900 // If there are no template parameters, this cannot be a new
901 // partial template specializtion?
902 if (!current_template_parms
)
905 // The injected-class-name is not a new partial specialization.
906 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
909 // If the constraints are not the same as those of the primary
910 // then, we can probably create a new specialization.
911 tree type_constr
= current_template_constraints ();
913 if (type
== TREE_TYPE (tmpl
))
915 tree main_constr
= get_constraints (tmpl
);
916 if (equivalent_constraints (type_constr
, main_constr
))
920 // Also, if there's a pre-existing specialization with matching
921 // constraints, then this also isn't new.
922 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
925 tree spec_tmpl
= TREE_VALUE (specs
);
926 tree spec_args
= TREE_PURPOSE (specs
);
927 tree spec_constr
= get_constraints (spec_tmpl
);
928 if (comp_template_args (args
, spec_args
)
929 && equivalent_constraints (type_constr
, spec_constr
))
931 specs
= TREE_CHAIN (specs
);
934 // Create a new type node (and corresponding type decl)
935 // for the newly declared specialization.
936 tree t
= make_class_type (TREE_CODE (type
));
937 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
938 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
940 /* We only need a separate type node for storing the definition of this
941 partial specialization; uses of S<T*> are unconstrained, so all are
942 equivalent. So keep TYPE_CANONICAL the same. */
943 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
945 // Build the corresponding type decl.
946 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
947 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
948 DECL_SOURCE_LOCATION (d
) = input_location
;
956 /* The TYPE is being declared. If it is a template type, that means it
957 is a partial specialization. Do appropriate error-checking. */
960 maybe_process_partial_specialization (tree type
)
964 if (type
== error_mark_node
)
965 return error_mark_node
;
967 /* A lambda that appears in specialization context is not itself a
969 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
972 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
974 error ("name of class shadows template template parameter %qD",
976 return error_mark_node
;
979 context
= TYPE_CONTEXT (type
);
981 if (TYPE_ALIAS_P (type
))
983 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
985 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
986 error ("specialization of alias template %qD",
987 TI_TEMPLATE (tinfo
));
989 error ("explicit specialization of non-template %qT", type
);
990 return error_mark_node
;
992 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
994 /* This is for ordinary explicit specialization and partial
995 specialization of a template class such as:
997 template <> class C<int>;
1001 template <class T> class C<T*>;
1003 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1005 if (tree t
= maybe_new_partial_specialization (type
))
1007 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
1008 && !at_namespace_scope_p ())
1009 return error_mark_node
;
1010 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
1011 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
1012 if (processing_template_decl
)
1014 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
1015 if (decl
== error_mark_node
)
1016 return error_mark_node
;
1017 return TREE_TYPE (decl
);
1020 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
1021 error ("specialization of %qT after instantiation", type
);
1022 else if (errorcount
&& !processing_specialization
1023 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
1024 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
1025 /* Trying to define a specialization either without a template<> header
1026 or in an inappropriate place. We've already given an error, so just
1027 bail now so we don't actually define the specialization. */
1028 return error_mark_node
;
1030 else if (CLASS_TYPE_P (type
)
1031 && !CLASSTYPE_USE_TEMPLATE (type
)
1032 && CLASSTYPE_TEMPLATE_INFO (type
)
1033 && context
&& CLASS_TYPE_P (context
)
1034 && CLASSTYPE_TEMPLATE_INFO (context
))
1036 /* This is for an explicit specialization of member class
1037 template according to [temp.expl.spec/18]:
1039 template <> template <class U> class C<int>::D;
1041 The context `C<int>' must be an implicit instantiation.
1042 Otherwise this is just a member class template declared
1045 template <> class C<int> { template <class U> class D; };
1046 template <> template <class U> class C<int>::D;
1048 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1049 while in the second case, `C<int>::D' is a primary template
1050 and `C<T>::D' may not exist. */
1052 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1053 && !COMPLETE_TYPE_P (type
))
1056 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1058 if (current_namespace
1059 != decl_namespace_context (tmpl
))
1061 permerror (input_location
,
1062 "specializing %q#T in different namespace", type
);
1063 permerror (DECL_SOURCE_LOCATION (tmpl
),
1064 " from definition of %q#D", tmpl
);
1067 /* Check for invalid specialization after instantiation:
1069 template <> template <> class C<int>::D<int>;
1070 template <> template <class U> class C<int>::D; */
1072 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1073 t
; t
= TREE_CHAIN (t
))
1075 tree inst
= TREE_VALUE (t
);
1076 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1077 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1079 /* We already have a full specialization of this partial
1080 instantiation, or a full specialization has been
1081 looked up but not instantiated. Reassign it to the
1082 new member specialization template. */
1086 elt
.tmpl
= most_general_template (tmpl
);
1087 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1090 type_specializations
->remove_elt (&elt
);
1093 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1096 = type_specializations
->find_slot (&elt
, INSERT
);
1097 entry
= ggc_alloc
<spec_entry
> ();
1102 /* But if we've had an implicit instantiation, that's a
1103 problem ([temp.expl.spec]/6). */
1104 error ("specialization %qT after instantiation %qT",
1108 /* Mark TYPE as a specialization. And as a result, we only
1109 have one level of template argument for the innermost
1111 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1112 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1113 CLASSTYPE_TI_ARGS (type
)
1114 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1117 else if (processing_specialization
)
1119 /* Someday C++0x may allow for enum template specialization. */
1120 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1121 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1122 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1123 "of %qD not allowed by ISO C++", type
);
1126 error ("explicit specialization of non-template %qT", type
);
1127 return error_mark_node
;
1134 /* Returns nonzero if we can optimize the retrieval of specializations
1135 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1136 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1139 optimize_specialization_lookup_p (tree tmpl
)
1141 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1142 && DECL_CLASS_SCOPE_P (tmpl
)
1143 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1145 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1146 /* The optimized lookup depends on the fact that the
1147 template arguments for the member function template apply
1148 purely to the containing class, which is not true if the
1149 containing class is an explicit or partial
1151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1152 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1153 && !DECL_CONV_FN_P (tmpl
)
1154 /* It is possible to have a template that is not a member
1155 template and is not a member of a template class:
1157 template <typename T>
1158 struct S { friend A::f(); };
1160 Here, the friend function is a template, but the context does
1161 not have template information. The optimized lookup relies
1162 on having ARGS be the template arguments for both the class
1163 and the function template. */
1164 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1167 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1168 gone through coerce_template_parms by now. */
1171 verify_unstripped_args_1 (tree inner
)
1173 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1175 tree arg
= TREE_VEC_ELT (inner
, i
);
1176 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1178 else if (TYPE_P (arg
))
1179 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1180 else if (ARGUMENT_PACK_P (arg
))
1181 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1182 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1183 /* Allow typedefs on the type of a non-type argument, since a
1184 parameter can have them. */;
1186 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1191 verify_unstripped_args (tree args
)
1193 ++processing_template_decl
;
1194 if (!any_dependent_template_arguments_p (args
))
1195 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1196 --processing_template_decl
;
1199 /* Retrieve the specialization (in the sense of [temp.spec] - a
1200 specialization is either an instantiation or an explicit
1201 specialization) of TMPL for the given template ARGS. If there is
1202 no such specialization, return NULL_TREE. The ARGS are a vector of
1203 arguments, or a vector of vectors of arguments, in the case of
1204 templates with more than one level of parameters.
1206 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1207 then we search for a partial specialization matching ARGS. This
1208 parameter is ignored if TMPL is not a class template.
1210 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1211 result is a NONTYPE_ARGUMENT_PACK. */
1214 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1216 if (tmpl
== NULL_TREE
)
1219 if (args
== error_mark_node
)
1222 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1223 || TREE_CODE (tmpl
) == FIELD_DECL
);
1225 /* There should be as many levels of arguments as there are
1226 levels of parameters. */
1227 gcc_assert (TMPL_ARGS_DEPTH (args
)
1228 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1229 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1230 : template_class_depth (DECL_CONTEXT (tmpl
))));
1233 verify_unstripped_args (args
);
1235 /* Lambda functions in templates aren't instantiated normally, but through
1236 tsubst_lambda_expr. */
1237 if (lambda_fn_in_template_p (tmpl
))
1240 if (optimize_specialization_lookup_p (tmpl
))
1242 /* The template arguments actually apply to the containing
1243 class. Find the class specialization with those
1245 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1246 tree class_specialization
1247 = retrieve_specialization (class_template
, args
, 0);
1248 if (!class_specialization
)
1251 /* Find the instance of TMPL. */
1252 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1253 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1256 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1257 /* using-declarations can add base methods to the method vec,
1258 and we don't want those here. */
1259 && DECL_CONTEXT (fn
) == class_specialization
)
1268 hash_table
<spec_hasher
> *specializations
;
1272 elt
.spec
= NULL_TREE
;
1274 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1275 specializations
= type_specializations
;
1277 specializations
= decl_specializations
;
1280 hash
= spec_hasher::hash (&elt
);
1281 found
= specializations
->find_with_hash (&elt
, hash
);
1289 /* Like retrieve_specialization, but for local declarations. */
1292 retrieve_local_specialization (tree tmpl
)
1294 if (local_specializations
== NULL
)
1297 tree
*slot
= local_specializations
->get (tmpl
);
1298 return slot
? *slot
: NULL_TREE
;
1301 /* Returns nonzero iff DECL is a specialization of TMPL. */
1304 is_specialization_of (tree decl
, tree tmpl
)
1308 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1312 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1318 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1320 for (t
= TREE_TYPE (decl
);
1322 t
= CLASSTYPE_USE_TEMPLATE (t
)
1323 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1324 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1331 /* Returns nonzero iff DECL is a specialization of friend declaration
1332 FRIEND_DECL according to [temp.friend]. */
1335 is_specialization_of_friend (tree decl
, tree friend_decl
)
1337 bool need_template
= true;
1340 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1341 || TREE_CODE (decl
) == TYPE_DECL
);
1343 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1344 of a template class, we want to check if DECL is a specialization
1346 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1347 && DECL_TEMPLATE_INFO (friend_decl
)
1348 && !DECL_USE_TEMPLATE (friend_decl
))
1350 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1351 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1352 need_template
= false;
1354 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1355 && !PRIMARY_TEMPLATE_P (friend_decl
))
1356 need_template
= false;
1358 /* There is nothing to do if this is not a template friend. */
1359 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1362 if (is_specialization_of (decl
, friend_decl
))
1366 A member of a class template may be declared to be a friend of a
1367 non-template class. In this case, the corresponding member of
1368 every specialization of the class template is a friend of the
1369 class granting friendship.
1371 For example, given a template friend declaration
1373 template <class T> friend void A<T>::f();
1375 the member function below is considered a friend
1377 template <> struct A<int> {
1381 For this type of template friend, TEMPLATE_DEPTH below will be
1382 nonzero. To determine if DECL is a friend of FRIEND, we first
1383 check if the enclosing class is a specialization of another. */
1385 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1387 && DECL_CLASS_SCOPE_P (decl
)
1388 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1389 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1391 /* Next, we check the members themselves. In order to handle
1392 a few tricky cases, such as when FRIEND_DECL's are
1394 template <class T> friend void A<T>::g(T t);
1395 template <class T> template <T t> friend void A<T>::h();
1399 void A<int>::g(int);
1400 template <int> void A<int>::h();
1402 we need to figure out ARGS, the template arguments from
1403 the context of DECL. This is required for template substitution
1404 of `T' in the function parameter of `g' and template parameter
1405 of `h' in the above examples. Here ARGS corresponds to `int'. */
1407 tree context
= DECL_CONTEXT (decl
);
1408 tree args
= NULL_TREE
;
1409 int current_depth
= 0;
1411 while (current_depth
< template_depth
)
1413 if (CLASSTYPE_TEMPLATE_INFO (context
))
1415 if (current_depth
== 0)
1416 args
= TYPE_TI_ARGS (context
);
1418 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1421 context
= TYPE_CONTEXT (context
);
1424 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1429 tree friend_args_type
;
1430 tree decl_args_type
;
1432 /* Make sure that both DECL and FRIEND_DECL are templates or
1434 is_template
= DECL_TEMPLATE_INFO (decl
)
1435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1436 if (need_template
^ is_template
)
1438 else if (is_template
)
1440 /* If both are templates, check template parameter list. */
1442 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1444 if (!comp_template_parms
1445 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1449 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1452 decl_type
= TREE_TYPE (decl
);
1454 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1455 tf_none
, NULL_TREE
);
1456 if (friend_type
== error_mark_node
)
1459 /* Check if return types match. */
1460 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1463 /* Check if function parameter types match, ignoring the
1464 `this' parameter. */
1465 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1466 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1468 friend_args_type
= TREE_CHAIN (friend_args_type
);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1470 decl_args_type
= TREE_CHAIN (decl_args_type
);
1472 return compparms (decl_args_type
, friend_args_type
);
1476 /* DECL is a TYPE_DECL */
1478 tree decl_type
= TREE_TYPE (decl
);
1480 /* Make sure that both DECL and FRIEND_DECL are templates or
1483 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1486 if (need_template
^ is_template
)
1488 else if (is_template
)
1491 /* If both are templates, check the name of the two
1492 TEMPLATE_DECL's first because is_friend didn't. */
1493 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1494 != DECL_NAME (friend_decl
))
1497 /* Now check template parameter list. */
1499 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1501 return comp_template_parms
1502 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1506 return (DECL_NAME (decl
)
1507 == DECL_NAME (friend_decl
));
1513 /* Register the specialization SPEC as a specialization of TMPL with
1514 the indicated ARGS. IS_FRIEND indicates whether the specialization
1515 is actually just a friend declaration. ATTRLIST is the list of
1516 attributes that the specialization is declared with or NULL when
1517 it isn't. Returns SPEC, or an equivalent prior declaration, if
1520 We also store instantiations of field packs in the hash table, even
1521 though they are not themselves templates, to make lookup easier. */
1524 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1528 spec_entry
**slot
= NULL
;
1531 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1532 || (TREE_CODE (tmpl
) == FIELD_DECL
1533 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1535 if (TREE_CODE (spec
) == FUNCTION_DECL
1536 && uses_template_parms (DECL_TI_ARGS (spec
)))
1537 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1538 register it; we want the corresponding TEMPLATE_DECL instead.
1539 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1540 the more obvious `uses_template_parms (spec)' to avoid problems
1541 with default function arguments. In particular, given
1542 something like this:
1544 template <class T> void f(T t1, T t = T())
1546 the default argument expression is not substituted for in an
1547 instantiation unless and until it is actually needed. */
1550 if (optimize_specialization_lookup_p (tmpl
))
1551 /* We don't put these specializations in the hash table, but we might
1552 want to give an error about a mismatch. */
1553 fn
= retrieve_specialization (tmpl
, args
, 0);
1561 hash
= spec_hasher::hash (&elt
);
1564 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1566 fn
= ((spec_entry
*) *slot
)->spec
;
1571 /* We can sometimes try to re-register a specialization that we've
1572 already got. In particular, regenerate_decl_from_template calls
1573 duplicate_decls which will update the specialization list. But,
1574 we'll still get called again here anyhow. It's more convenient
1575 to simply allow this than to try to prevent it. */
1578 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1580 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1582 if (DECL_ODR_USED (fn
)
1583 || DECL_EXPLICIT_INSTANTIATION (fn
))
1585 error ("specialization of %qD after instantiation",
1587 return error_mark_node
;
1592 /* This situation should occur only if the first
1593 specialization is an implicit instantiation, the
1594 second is an explicit specialization, and the
1595 implicit instantiation has not yet been used. That
1596 situation can occur if we have implicitly
1597 instantiated a member function and then specialized
1600 We can also wind up here if a friend declaration that
1601 looked like an instantiation turns out to be a
1604 template <class T> void foo(T);
1605 class S { friend void foo<>(int) };
1606 template <> void foo(int);
1608 We transform the existing DECL in place so that any
1609 pointers to it become pointers to the updated
1612 If there was a definition for the template, but not
1613 for the specialization, we want this to look as if
1614 there were no definition, and vice versa. */
1615 DECL_INITIAL (fn
) = NULL_TREE
;
1616 duplicate_decls (spec
, fn
, is_friend
);
1617 /* The call to duplicate_decls will have applied
1620 An explicit specialization of a function template
1621 is inline only if it is explicitly declared to be,
1622 and independently of whether its function template
1625 to the primary function; now copy the inline bits to
1626 the various clones. */
1627 FOR_EACH_CLONE (clone
, fn
)
1629 DECL_DECLARED_INLINE_P (clone
)
1630 = DECL_DECLARED_INLINE_P (fn
);
1631 DECL_SOURCE_LOCATION (clone
)
1632 = DECL_SOURCE_LOCATION (fn
);
1633 DECL_DELETED_FN (clone
)
1634 = DECL_DELETED_FN (fn
);
1636 check_specialization_namespace (tmpl
);
1641 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1643 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1644 if (dd
== error_mark_node
)
1645 /* We've already complained in duplicate_decls. */
1646 return error_mark_node
;
1648 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1649 /* Dup decl failed, but this is a new definition. Set the
1650 line number so any errors match this new
1652 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1658 return duplicate_decls (spec
, fn
, is_friend
);
1660 /* A specialization must be declared in the same namespace as the
1661 template it is specializing. */
1662 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1663 && !check_specialization_namespace (tmpl
))
1664 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1666 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1668 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1669 gcc_assert (tmpl
&& args
&& spec
);
1672 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1673 && PRIMARY_TEMPLATE_P (tmpl
)
1674 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1675 || variable_template_p (tmpl
))
1676 /* If TMPL is a forward declaration of a template function, keep a list
1677 of all specializations in case we need to reassign them to a friend
1678 template later in tsubst_friend_function.
1680 Also keep a list of all variable template instantiations so that
1681 process_partial_specialization can check whether a later partial
1682 specialization would have used it. */
1683 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1684 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1690 /* Returns true iff two spec_entry nodes are equivalent. */
1692 int comparing_specializations
;
1695 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1699 ++comparing_specializations
;
1700 equal
= (e1
->tmpl
== e2
->tmpl
1701 && comp_template_args (e1
->args
, e2
->args
));
1702 if (equal
&& flag_concepts
1703 /* tmpl could be a FIELD_DECL for a capture pack. */
1704 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1705 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1706 && uses_template_parms (e1
->args
))
1708 /* Partial specializations of a variable template can be distinguished by
1710 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1711 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1712 equal
= equivalent_constraints (c1
, c2
);
1714 --comparing_specializations
;
1719 /* Returns a hash for a template TMPL and template arguments ARGS. */
1722 hash_tmpl_and_args (tree tmpl
, tree args
)
1724 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1725 return iterative_hash_template_arg (args
, val
);
1728 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1732 spec_hasher::hash (spec_entry
*e
)
1734 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1737 /* Recursively calculate a hash value for a template argument ARG, for use
1738 in the hash tables of template specializations. */
1741 iterative_hash_template_arg (tree arg
, hashval_t val
)
1743 unsigned HOST_WIDE_INT i
;
1744 enum tree_code code
;
1747 if (arg
== NULL_TREE
)
1748 return iterative_hash_object (arg
, val
);
1753 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1756 code
= TREE_CODE (arg
);
1757 tclass
= TREE_CODE_CLASS (code
);
1759 val
= iterative_hash_object (code
, val
);
1766 case IDENTIFIER_NODE
:
1767 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1771 int i
, len
= TREE_VEC_LENGTH (arg
);
1772 for (i
= 0; i
< len
; ++i
)
1773 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1777 case TYPE_PACK_EXPANSION
:
1778 case EXPR_PACK_EXPANSION
:
1779 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1780 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1782 case TYPE_ARGUMENT_PACK
:
1783 case NONTYPE_ARGUMENT_PACK
:
1784 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1787 for (; arg
; arg
= TREE_CHAIN (arg
))
1788 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1792 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1793 val
= iterative_hash_template_arg (*iter
, val
);
1799 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1802 val
= iterative_hash_template_arg (field
, val
);
1803 val
= iterative_hash_template_arg (value
, val
);
1809 if (!DECL_ARTIFICIAL (arg
))
1811 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1812 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1814 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1817 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1820 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1821 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1823 case TEMPLATE_PARM_INDEX
:
1824 val
= iterative_hash_template_arg
1825 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1826 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1827 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1830 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1831 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1832 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1835 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1837 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1841 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1842 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1843 val
= iterative_hash_object (code
, val
);
1844 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1847 /* [temp.over.link] Two lambda-expressions are never considered
1850 So just hash the closure type. */
1851 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1854 case IMPLICIT_CONV_EXPR
:
1855 case STATIC_CAST_EXPR
:
1856 case REINTERPRET_CAST_EXPR
:
1857 case CONST_CAST_EXPR
:
1858 case DYNAMIC_CAST_EXPR
:
1860 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1861 /* Now hash operands as usual. */
1871 if (alias_template_specialization_p (arg
))
1873 // We want an alias specialization that survived strip_typedefs
1874 // to hash differently from its TYPE_CANONICAL, to avoid hash
1875 // collisions that compare as different in template_args_equal.
1876 // These could be dependent specializations that strip_typedefs
1877 // left alone, or untouched specializations because
1878 // coerce_template_parms returns the unconverted template
1879 // arguments if it sees incomplete argument packs.
1880 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1881 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1883 if (TYPE_CANONICAL (arg
))
1884 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1886 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1887 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1888 /* Otherwise just compare the types during lookup. */
1891 case tcc_declaration
:
1893 return iterative_hash_expr (arg
, val
);
1896 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1898 unsigned n
= cp_tree_operand_length (arg
);
1899 for (i
= 0; i
< n
; ++i
)
1900 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1908 /* Unregister the specialization SPEC as a specialization of TMPL.
1909 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1910 if the SPEC was listed as a specialization of TMPL.
1912 Note that SPEC has been ggc_freed, so we can't look inside it. */
1915 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1920 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1921 elt
.args
= TI_ARGS (tinfo
);
1922 elt
.spec
= NULL_TREE
;
1924 entry
= decl_specializations
->find (&elt
);
1927 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1928 gcc_assert (new_spec
!= NULL_TREE
);
1929 entry
->spec
= new_spec
;
1936 /* Like register_specialization, but for local declarations. We are
1937 registering SPEC, an instantiation of TMPL. */
1940 register_local_specialization (tree spec
, tree tmpl
)
1942 gcc_assert (tmpl
!= spec
);
1943 local_specializations
->put (tmpl
, spec
);
1946 /* TYPE is a class type. Returns true if TYPE is an explicitly
1947 specialized class. */
1950 explicit_class_specialization_p (tree type
)
1952 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1954 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1957 /* Print the list of functions at FNS, going through all the overloads
1958 for each element of the list. Alternatively, FNS cannot be a
1959 TREE_LIST, in which case it will be printed together with all the
1962 MORE and *STR should respectively be FALSE and NULL when the function
1963 is called from the outside. They are used internally on recursive
1964 calls. print_candidates manages the two parameters and leaves NULL
1965 in *STR when it ends. */
1968 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1970 if (TREE_CODE (fns
) == TREE_LIST
)
1971 for (; fns
; fns
= TREE_CHAIN (fns
))
1972 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1974 for (lkp_iterator
iter (fns
); iter
;)
1979 const char *pfx
= *str
;
1983 pfx
= _("candidates are:");
1985 pfx
= _("candidate is:");
1986 *str
= get_spaces (pfx
);
1988 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1992 /* Print the list of candidate FNS in an error message. FNS can also
1993 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1996 print_candidates (tree fns
)
1999 print_candidates_1 (fns
, &str
);
2003 /* Get a (possibly) constrained template declaration for the
2004 purpose of ordering candidates. */
2006 get_template_for_ordering (tree list
)
2008 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2009 tree f
= TREE_VALUE (list
);
2010 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2011 return TI_TEMPLATE (ti
);
2015 /* Among candidates having the same signature, return the
2016 most constrained or NULL_TREE if there is no best candidate.
2017 If the signatures of candidates vary (e.g., template
2018 specialization vs. member function), then there can be no
2021 Note that we don't compare constraints on the functions
2022 themselves, but rather those of their templates. */
2024 most_constrained_function (tree candidates
)
2026 // Try to find the best candidate in a first pass.
2027 tree champ
= candidates
;
2028 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2030 int winner
= more_constrained (get_template_for_ordering (champ
),
2031 get_template_for_ordering (c
));
2033 champ
= c
; // The candidate is more constrained
2034 else if (winner
== 0)
2035 return NULL_TREE
; // Neither is more constrained
2038 // Verify that the champ is better than previous candidates.
2039 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2040 if (!more_constrained (get_template_for_ordering (champ
),
2041 get_template_for_ordering (c
)))
2049 /* Returns the template (one of the functions given by TEMPLATE_ID)
2050 which can be specialized to match the indicated DECL with the
2051 explicit template args given in TEMPLATE_ID. The DECL may be
2052 NULL_TREE if none is available. In that case, the functions in
2053 TEMPLATE_ID are non-members.
2055 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2056 specialization of a member template.
2058 The TEMPLATE_COUNT is the number of references to qualifying
2059 template classes that appeared in the name of the function. See
2060 check_explicit_specialization for a more accurate description.
2062 TSK indicates what kind of template declaration (if any) is being
2063 declared. TSK_TEMPLATE indicates that the declaration given by
2064 DECL, though a FUNCTION_DECL, has template parameters, and is
2065 therefore a template function.
2067 The template args (those explicitly specified and those deduced)
2068 are output in a newly created vector *TARGS_OUT.
2070 If it is impossible to determine the result, an error message is
2071 issued. The error_mark_node is returned to indicate failure. */
2074 determine_specialization (tree template_id
,
2077 int need_member_template
,
2083 tree explicit_targs
;
2084 tree candidates
= NULL_TREE
;
2086 /* A TREE_LIST of templates of which DECL may be a specialization.
2087 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2088 corresponding TREE_PURPOSE is the set of template arguments that,
2089 when used to instantiate the template, would produce a function
2090 with the signature of DECL. */
2091 tree templates
= NULL_TREE
;
2093 cp_binding_level
*b
;
2095 *targs_out
= NULL_TREE
;
2097 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2098 return error_mark_node
;
2100 /* We shouldn't be specializing a member template of an
2101 unspecialized class template; we already gave an error in
2102 check_specialization_scope, now avoid crashing. */
2104 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2105 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2107 gcc_assert (errorcount
);
2108 return error_mark_node
;
2111 fns
= TREE_OPERAND (template_id
, 0);
2112 explicit_targs
= TREE_OPERAND (template_id
, 1);
2114 if (fns
== error_mark_node
)
2115 return error_mark_node
;
2117 /* Check for baselinks. */
2118 if (BASELINK_P (fns
))
2119 fns
= BASELINK_FUNCTIONS (fns
);
2121 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2123 error ("%qD is not a function template", fns
);
2124 return error_mark_node
;
2126 else if (VAR_P (decl
) && !variable_template_p (fns
))
2128 error ("%qD is not a variable template", fns
);
2129 return error_mark_node
;
2132 /* Count the number of template headers specified for this
2135 for (b
= current_binding_level
;
2136 b
->kind
== sk_template_parms
;
2140 tree orig_fns
= fns
;
2142 if (variable_template_p (fns
))
2144 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2145 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2146 tf_warning_or_error
,
2147 /*req_all*/true, /*use_defarg*/true);
2148 if (targs
!= error_mark_node
)
2149 templates
= tree_cons (targs
, fns
, templates
);
2151 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2155 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2157 tree decl_arg_types
;
2161 /* In case of explicit specialization, we need to check if
2162 the number of template headers appearing in the specialization
2163 is correct. This is usually done in check_explicit_specialization,
2164 but the check done there cannot be exhaustive when specializing
2165 member functions. Consider the following code:
2167 template <> void A<int>::f(int);
2168 template <> template <> void A<int>::f(int);
2170 Assuming that A<int> is not itself an explicit specialization
2171 already, the first line specializes "f" which is a non-template
2172 member function, whilst the second line specializes "f" which
2173 is a template member function. So both lines are syntactically
2174 correct, and check_explicit_specialization does not reject
2177 Here, we can do better, as we are matching the specialization
2178 against the declarations. We count the number of template
2179 headers, and we check if they match TEMPLATE_COUNT + 1
2180 (TEMPLATE_COUNT is the number of qualifying template classes,
2181 plus there must be another header for the member template
2184 Notice that if header_count is zero, this is not a
2185 specialization but rather a template instantiation, so there
2186 is no check we can perform here. */
2187 if (header_count
&& header_count
!= template_count
+ 1)
2190 /* Check that the number of template arguments at the
2191 innermost level for DECL is the same as for FN. */
2192 if (current_binding_level
->kind
== sk_template_parms
2193 && !current_binding_level
->explicit_spec_p
2194 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2195 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2196 (current_template_parms
))))
2199 /* DECL might be a specialization of FN. */
2200 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2201 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2203 /* For a non-static member function, we need to make sure
2204 that the const qualification is the same. Since
2205 get_bindings does not try to merge the "this" parameter,
2206 we must do the comparison explicitly. */
2207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2209 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2210 TREE_VALUE (decl_arg_types
)))
2213 /* And the ref-qualification. */
2214 if (type_memfn_rqual (TREE_TYPE (decl
))
2215 != type_memfn_rqual (TREE_TYPE (fn
)))
2219 /* Skip the "this" parameter and, for constructors of
2220 classes with virtual bases, the VTT parameter. A
2221 full specialization of a constructor will have a VTT
2222 parameter, but a template never will. */
2224 = skip_artificial_parms_for (decl
, decl_arg_types
);
2226 = skip_artificial_parms_for (fn
, fn_arg_types
);
2228 /* Function templates cannot be specializations; there are
2229 no partial specializations of functions. Therefore, if
2230 the type of DECL does not match FN, there is no
2233 Note that it should never be the case that we have both
2234 candidates added here, and for regular member functions
2236 if (tsk
== tsk_template
)
2238 if (compparms (fn_arg_types
, decl_arg_types
))
2239 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2243 /* See whether this function might be a specialization of this
2244 template. Suppress access control because we might be trying
2245 to make this specialization a friend, and we have already done
2246 access control for the declaration of the specialization. */
2247 push_deferring_access_checks (dk_no_check
);
2248 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2249 pop_deferring_access_checks ();
2252 /* We cannot deduce template arguments that when used to
2253 specialize TMPL will produce DECL. */
2256 if (uses_template_parms (targs
))
2257 /* We deduced something involving 'auto', which isn't a valid
2258 template argument. */
2261 /* Remove, from the set of candidates, all those functions
2262 whose constraints are not satisfied. */
2263 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2266 // Then, try to form the new function type.
2267 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2268 if (insttype
== error_mark_node
)
2271 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2272 if (!compparms (fn_arg_types
, decl_arg_types
))
2275 /* Save this template, and the arguments deduced. */
2276 templates
= tree_cons (targs
, fn
, templates
);
2278 else if (need_member_template
)
2279 /* FN is an ordinary member function, and we need a
2280 specialization of a member template. */
2282 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2283 /* We can get IDENTIFIER_NODEs here in certain erroneous
2286 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2287 /* This is just an ordinary non-member function. Nothing can
2288 be a specialization of that. */
2290 else if (DECL_ARTIFICIAL (fn
))
2291 /* Cannot specialize functions that are created implicitly. */
2295 tree decl_arg_types
;
2297 /* This is an ordinary member function. However, since
2298 we're here, we can assume its enclosing class is a
2299 template class. For example,
2301 template <typename T> struct S { void f(); };
2302 template <> void S<int>::f() {}
2304 Here, S<int>::f is a non-template, but S<int> is a
2305 template class. If FN has the same type as DECL, we
2306 might be in business. */
2308 if (!DECL_TEMPLATE_INFO (fn
))
2309 /* Its enclosing class is an explicit specialization
2310 of a template class. This is not a candidate. */
2313 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2314 TREE_TYPE (TREE_TYPE (fn
))))
2315 /* The return types differ. */
2318 /* Adjust the type of DECL in case FN is a static member. */
2319 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2320 if (DECL_STATIC_FUNCTION_P (fn
)
2321 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2322 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2328 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2329 && (type_memfn_rqual (TREE_TYPE (decl
))
2330 != type_memfn_rqual (TREE_TYPE (fn
))))
2333 // If the deduced arguments do not satisfy the constraints,
2334 // this is not a candidate.
2335 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2338 // Add the candidate.
2339 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2343 if (templates
&& TREE_CHAIN (templates
))
2349 It is possible for a specialization with a given function
2350 signature to be instantiated from more than one function
2351 template. In such cases, explicit specification of the
2352 template arguments must be used to uniquely identify the
2353 function template specialization being specialized.
2355 Note that here, there's no suggestion that we're supposed to
2356 determine which of the candidate templates is most
2357 specialized. However, we, also have:
2361 Partial ordering of overloaded function template
2362 declarations is used in the following contexts to select
2363 the function template to which a function template
2364 specialization refers:
2366 -- when an explicit specialization refers to a function
2369 So, we do use the partial ordering rules, at least for now.
2370 This extension can only serve to make invalid programs valid,
2371 so it's safe. And, there is strong anecdotal evidence that
2372 the committee intended the partial ordering rules to apply;
2373 the EDG front end has that behavior, and John Spicer claims
2374 that the committee simply forgot to delete the wording in
2375 [temp.expl.spec]. */
2376 tree tmpl
= most_specialized_instantiation (templates
);
2377 if (tmpl
!= error_mark_node
)
2380 TREE_CHAIN (templates
) = NULL_TREE
;
2384 // Concepts allows multiple declarations of member functions
2385 // with the same signature. Like above, we need to rely on
2386 // on the partial ordering of those candidates to determine which
2388 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2390 if (tree cand
= most_constrained_function (candidates
))
2393 TREE_CHAIN (cand
) = NULL_TREE
;
2397 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2399 error ("template-id %qD for %q+D does not match any template "
2400 "declaration", template_id
, decl
);
2401 if (header_count
&& header_count
!= template_count
+ 1)
2402 inform (input_location
, "saw %d %<template<>%>, need %d for "
2403 "specializing a member function template",
2404 header_count
, template_count
+ 1);
2406 print_candidates (orig_fns
);
2407 return error_mark_node
;
2409 else if ((templates
&& TREE_CHAIN (templates
))
2410 || (candidates
&& TREE_CHAIN (candidates
))
2411 || (templates
&& candidates
))
2413 error ("ambiguous template specialization %qD for %q+D",
2415 candidates
= chainon (candidates
, templates
);
2416 print_candidates (candidates
);
2417 return error_mark_node
;
2420 /* We have one, and exactly one, match. */
2423 tree fn
= TREE_VALUE (candidates
);
2424 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2426 // Propagate the candidate's constraints to the declaration.
2427 set_constraints (decl
, get_constraints (fn
));
2429 /* DECL is a re-declaration or partial instantiation of a template
2431 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2433 /* It was a specialization of an ordinary member function in a
2435 return DECL_TI_TEMPLATE (fn
);
2438 /* It was a specialization of a template. */
2439 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2440 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2442 *targs_out
= copy_node (targs
);
2443 SET_TMPL_ARGS_LEVEL (*targs_out
,
2444 TMPL_ARGS_DEPTH (*targs_out
),
2445 TREE_PURPOSE (templates
));
2448 *targs_out
= TREE_PURPOSE (templates
);
2449 return TREE_VALUE (templates
);
2452 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2453 but with the default argument values filled in from those in the
2457 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2460 tree new_spec_types
;
2465 if (spec_types
== void_list_node
)
2466 return void_list_node
;
2468 /* Substitute into the rest of the list. */
2470 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2471 TREE_CHAIN (tmpl_types
));
2473 /* Add the default argument for this parameter. */
2474 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2475 TREE_VALUE (spec_types
),
2479 /* DECL is an explicit specialization. Replicate default arguments
2480 from the template it specializes. (That way, code like:
2482 template <class T> void f(T = 3);
2483 template <> void f(double);
2486 works, as required.) An alternative approach would be to look up
2487 the correct default arguments at the call-site, but this approach
2488 is consistent with how implicit instantiations are handled. */
2491 copy_default_args_to_explicit_spec (tree decl
)
2496 tree new_spec_types
;
2500 tree object_type
= NULL_TREE
;
2501 tree in_charge
= NULL_TREE
;
2502 tree vtt
= NULL_TREE
;
2504 /* See if there's anything we need to do. */
2505 tmpl
= DECL_TI_TEMPLATE (decl
);
2506 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2507 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2508 if (TREE_PURPOSE (t
))
2513 old_type
= TREE_TYPE (decl
);
2514 spec_types
= TYPE_ARG_TYPES (old_type
);
2516 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2518 /* Remove the this pointer, but remember the object's type for
2520 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2521 spec_types
= TREE_CHAIN (spec_types
);
2522 tmpl_types
= TREE_CHAIN (tmpl_types
);
2524 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2526 /* DECL may contain more parameters than TMPL due to the extra
2527 in-charge parameter in constructors and destructors. */
2528 in_charge
= spec_types
;
2529 spec_types
= TREE_CHAIN (spec_types
);
2531 if (DECL_HAS_VTT_PARM_P (decl
))
2534 spec_types
= TREE_CHAIN (spec_types
);
2538 /* Compute the merged default arguments. */
2540 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2542 /* Compute the new FUNCTION_TYPE. */
2546 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2551 /* Put the in-charge parameter back. */
2552 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2553 TREE_VALUE (in_charge
),
2556 new_type
= build_method_type_directly (object_type
,
2557 TREE_TYPE (old_type
),
2561 new_type
= build_function_type (TREE_TYPE (old_type
),
2563 new_type
= cp_build_type_attribute_variant (new_type
,
2564 TYPE_ATTRIBUTES (old_type
));
2565 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2567 TREE_TYPE (decl
) = new_type
;
2570 /* Return the number of template headers we expect to see for a definition
2571 or specialization of CTYPE or one of its non-template members. */
2574 num_template_headers_for_class (tree ctype
)
2576 int num_templates
= 0;
2578 while (ctype
&& CLASS_TYPE_P (ctype
))
2580 /* You're supposed to have one `template <...>' for every
2581 template class, but you don't need one for a full
2582 specialization. For example:
2584 template <class T> struct S{};
2585 template <> struct S<int> { void f(); };
2586 void S<int>::f () {}
2588 is correct; there shouldn't be a `template <>' for the
2589 definition of `S<int>::f'. */
2590 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2591 /* If CTYPE does not have template information of any
2592 kind, then it is not a template, nor is it nested
2593 within a template. */
2595 if (explicit_class_specialization_p (ctype
))
2597 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2600 ctype
= TYPE_CONTEXT (ctype
);
2603 return num_templates
;
2606 /* Do a simple sanity check on the template headers that precede the
2607 variable declaration DECL. */
2610 check_template_variable (tree decl
)
2612 tree ctx
= CP_DECL_CONTEXT (decl
);
2613 int wanted
= num_template_headers_for_class (ctx
);
2614 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2615 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2617 if (cxx_dialect
< cxx14
)
2618 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2619 "variable templates only available with "
2620 "-std=c++14 or -std=gnu++14");
2622 // Namespace-scope variable templates should have a template header.
2625 if (template_header_count
> wanted
)
2627 auto_diagnostic_group d
;
2628 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2629 "too many template headers for %qD "
2632 if (warned
&& CLASS_TYPE_P (ctx
)
2633 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2634 inform (DECL_SOURCE_LOCATION (decl
),
2635 "members of an explicitly specialized class are defined "
2636 "without a template header");
2640 /* An explicit specialization whose declarator-id or class-head-name is not
2641 qualified shall be declared in the nearest enclosing namespace of the
2642 template, or, if the namespace is inline (7.3.1), any namespace from its
2643 enclosing namespace set.
2645 If the name declared in the explicit instantiation is an unqualified name,
2646 the explicit instantiation shall appear in the namespace where its template
2647 is declared or, if that namespace is inline (7.3.1), any namespace from its
2648 enclosing namespace set. */
2651 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2653 tree tmpl
= most_general_template (t
);
2654 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2655 && !is_nested_namespace (current_namespace
,
2656 CP_DECL_CONTEXT (tmpl
), true))
2658 if (processing_specialization
)
2659 permerror (loc
, "explicit specialization of %qD outside its "
2660 "namespace must use a nested-name-specifier", tmpl
);
2661 else if (processing_explicit_instantiation
2662 && cxx_dialect
>= cxx11
)
2663 /* This was allowed in C++98, so only pedwarn. */
2664 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2665 "outside its namespace must use a nested-name-"
2670 /* Warn for a template specialization SPEC that is missing some of a set
2671 of function or type attributes that the template TEMPL is declared with.
2672 ATTRLIST is a list of additional attributes that SPEC should be taken
2673 to ultimately be declared with. */
2676 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2678 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2679 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2681 /* Avoid warning if the difference between the primary and
2682 the specialization is not in one of the attributes below. */
2683 const char* const blacklist
[] = {
2684 "alloc_align", "alloc_size", "assume_aligned", "format",
2685 "format_arg", "malloc", "nonnull", NULL
2688 /* Put together a list of the black listed attributes that the primary
2689 template is declared with that the specialization is not, in case
2690 it's not apparent from the most recent declaration of the primary. */
2692 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2698 auto_diagnostic_group d
;
2699 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2700 "explicit specialization %q#D may be missing attributes",
2702 inform (DECL_SOURCE_LOCATION (tmpl
),
2704 ? G_("missing primary template attributes %s")
2705 : G_("missing primary template attribute %s"),
2706 pp_formatted_text (&str
));
2709 /* Check to see if the function just declared, as indicated in
2710 DECLARATOR, and in DECL, is a specialization of a function
2711 template. We may also discover that the declaration is an explicit
2712 instantiation at this point.
2714 Returns DECL, or an equivalent declaration that should be used
2715 instead if all goes well. Issues an error message if something is
2716 amiss. Returns error_mark_node if the error is not easily
2719 FLAGS is a bitmask consisting of the following flags:
2721 2: The function has a definition.
2722 4: The function is a friend.
2724 The TEMPLATE_COUNT is the number of references to qualifying
2725 template classes that appeared in the name of the function. For
2728 template <class T> struct S { void f(); };
2731 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2732 classes are not counted in the TEMPLATE_COUNT, so that in
2734 template <class T> struct S {};
2735 template <> struct S<int> { void f(); }
2736 template <> void S<int>::f();
2738 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2739 invalid; there should be no template <>.)
2741 If the function is a specialization, it is marked as such via
2742 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2743 is set up correctly, and it is added to the list of specializations
2744 for that template. */
2747 check_explicit_specialization (tree declarator
,
2753 int have_def
= flags
& 2;
2754 int is_friend
= flags
& 4;
2755 bool is_concept
= flags
& 8;
2756 int specialization
= 0;
2757 int explicit_instantiation
= 0;
2758 int member_specialization
= 0;
2759 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2760 tree dname
= DECL_NAME (decl
);
2765 if (!processing_specialization
)
2768 tsk
= tsk_excessive_parms
;
2771 tsk
= current_tmpl_spec_kind (template_count
);
2776 if (processing_specialization
&& !VAR_P (decl
))
2779 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2781 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2784 /* This could be something like:
2786 template <class T> void f(T);
2787 class S { friend void f<>(int); } */
2791 /* This case handles bogus declarations like template <>
2792 template <class T> void f<int>(); */
2794 error ("template-id %qD in declaration of primary template",
2801 case tsk_invalid_member_spec
:
2802 /* The error has already been reported in
2803 check_specialization_scope. */
2804 return error_mark_node
;
2806 case tsk_invalid_expl_inst
:
2807 error ("template parameter list used in explicit instantiation");
2813 error ("definition provided for explicit instantiation");
2815 explicit_instantiation
= 1;
2818 case tsk_excessive_parms
:
2819 case tsk_insufficient_parms
:
2820 if (tsk
== tsk_excessive_parms
)
2821 error ("too many template parameter lists in declaration of %qD",
2823 else if (template_header_count
)
2824 error("too few template parameter lists in declaration of %qD", decl
);
2826 error("explicit specialization of %qD must be introduced by "
2827 "%<template <>%>", decl
);
2832 error ("explicit specialization declared %<concept%>");
2834 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2835 /* In cases like template<> constexpr bool v = true;
2836 We'll give an error in check_template_variable. */
2839 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2841 member_specialization
= 1;
2847 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2849 /* This case handles bogus declarations like template <>
2850 template <class T> void f<int>(); */
2852 if (!uses_template_parms (declarator
))
2853 error ("template-id %qD in declaration of primary template",
2855 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2857 /* Partial specialization of variable template. */
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2862 else if (cxx_dialect
< cxx14
)
2863 error ("non-type partial specialization %qD "
2864 "is not allowed", declarator
);
2866 error ("non-class, non-variable partial specialization %qD "
2867 "is not allowed", declarator
);
2872 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2873 /* This is a specialization of a member template, without
2874 specialization the containing class. Something like:
2876 template <class T> struct S {
2877 template <class U> void f (U);
2879 template <> template <class U> void S<int>::f(U) {}
2881 That's a specialization -- but of the entire template. */
2889 if ((specialization
|| member_specialization
)
2890 /* This doesn't apply to variable templates. */
2891 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2892 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2894 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2895 for (; t
; t
= TREE_CHAIN (t
))
2896 if (TREE_PURPOSE (t
))
2898 permerror (input_location
,
2899 "default argument specified in explicit specialization");
2904 if (specialization
|| member_specialization
|| explicit_instantiation
)
2906 tree tmpl
= NULL_TREE
;
2907 tree targs
= NULL_TREE
;
2908 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2910 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2911 if (!was_template_id
)
2915 gcc_assert (identifier_p (declarator
));
2920 /* If there is no class context, the explicit instantiation
2921 must be at namespace scope. */
2922 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2924 /* Find the namespace binding, using the declaration
2926 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2928 if (fns
== error_mark_node
)
2929 /* If lookup fails, look for a friend declaration so we can
2930 give a better diagnostic. */
2931 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2932 /*type*/false, /*complain*/true,
2935 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2937 error ("%qD is not a template function", dname
);
2938 fns
= error_mark_node
;
2942 declarator
= lookup_template_function (fns
, NULL_TREE
);
2945 if (declarator
== error_mark_node
)
2946 return error_mark_node
;
2948 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2950 if (!explicit_instantiation
)
2951 /* A specialization in class scope. This is invalid,
2952 but the error will already have been flagged by
2953 check_specialization_scope. */
2954 return error_mark_node
;
2957 /* It's not valid to write an explicit instantiation in
2960 class C { template void f(); }
2962 This case is caught by the parser. However, on
2965 template class C { void f(); };
2967 (which is invalid) we can get here. The error will be
2974 else if (ctype
!= NULL_TREE
2975 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2977 // We'll match variable templates in start_decl.
2981 /* Find the list of functions in ctype that have the same
2982 name as the declared function. */
2983 tree name
= TREE_OPERAND (declarator
, 0);
2985 if (constructor_name_p (name
, ctype
))
2987 if (DECL_CONSTRUCTOR_P (decl
)
2988 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2989 : !CLASSTYPE_DESTRUCTOR (ctype
))
2991 /* From [temp.expl.spec]:
2993 If such an explicit specialization for the member
2994 of a class template names an implicitly-declared
2995 special member function (clause _special_), the
2996 program is ill-formed.
2998 Similar language is found in [temp.explicit]. */
2999 error ("specialization of implicitly-declared special member function");
3000 return error_mark_node
;
3003 name
= DECL_NAME (decl
);
3006 /* For a type-conversion operator, We might be looking for
3007 `operator int' which will be a specialization of
3008 `operator T'. Grab all the conversion operators, and
3009 then select from them. */
3010 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3011 ? conv_op_identifier
: name
);
3013 if (fns
== NULL_TREE
)
3015 error ("no member function %qD declared in %qT", name
, ctype
);
3016 return error_mark_node
;
3019 TREE_OPERAND (declarator
, 0) = fns
;
3022 /* Figure out what exactly is being specialized at this point.
3023 Note that for an explicit instantiation, even one for a
3024 member function, we cannot tell a priori whether the
3025 instantiation is for a member template, or just a member
3026 function of a template class. Even if a member template is
3027 being instantiated, the member template arguments may be
3028 elided if they can be deduced from the rest of the
3030 tmpl
= determine_specialization (declarator
, decl
,
3032 member_specialization
,
3036 if (!tmpl
|| tmpl
== error_mark_node
)
3037 /* We couldn't figure out what this declaration was
3039 return error_mark_node
;
3042 if (TREE_CODE (decl
) == FUNCTION_DECL
3043 && DECL_HIDDEN_FRIEND_P (tmpl
))
3045 auto_diagnostic_group d
;
3046 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3047 "friend declaration %qD is not visible to "
3048 "explicit specialization", tmpl
))
3049 inform (DECL_SOURCE_LOCATION (tmpl
),
3050 "friend declaration here");
3052 else if (!ctype
&& !is_friend
3053 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3054 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3056 tree gen_tmpl
= most_general_template (tmpl
);
3058 if (explicit_instantiation
)
3060 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3061 is done by do_decl_instantiation later. */
3063 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3064 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3066 if (arg_depth
> parm_depth
)
3068 /* If TMPL is not the most general template (for
3069 example, if TMPL is a friend template that is
3070 injected into namespace scope), then there will
3071 be too many levels of TARGS. Remove some of them
3076 new_targs
= make_tree_vec (parm_depth
);
3077 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3078 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3079 = TREE_VEC_ELT (targs
, i
);
3083 return instantiate_template (tmpl
, targs
, tf_error
);
3086 /* If we thought that the DECL was a member function, but it
3087 turns out to be specializing a static member function,
3088 make DECL a static member function as well. */
3089 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3090 && DECL_STATIC_FUNCTION_P (tmpl
)
3091 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3092 revert_static_member_fn (decl
);
3094 /* If this is a specialization of a member template of a
3095 template class, we want to return the TEMPLATE_DECL, not
3096 the specialization of it. */
3097 if (tsk
== tsk_template
&& !was_template_id
)
3099 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3100 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3101 DECL_INITIAL (result
) = NULL_TREE
;
3105 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3106 DECL_SOURCE_LOCATION (result
)
3107 = DECL_SOURCE_LOCATION (decl
);
3108 /* We want to use the argument list specified in the
3109 definition, not in the original declaration. */
3110 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3111 for (parm
= DECL_ARGUMENTS (result
); parm
;
3112 parm
= DECL_CHAIN (parm
))
3113 DECL_CONTEXT (parm
) = result
;
3115 return register_specialization (tmpl
, gen_tmpl
, targs
,
3119 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3120 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3122 if (was_template_id
)
3123 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3125 /* Inherit default function arguments from the template
3126 DECL is specializing. */
3127 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3128 copy_default_args_to_explicit_spec (decl
);
3130 /* This specialization has the same protection as the
3131 template it specializes. */
3132 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3133 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3135 /* 7.1.1-1 [dcl.stc]
3137 A storage-class-specifier shall not be specified in an
3138 explicit specialization...
3140 The parser rejects these, so unless action is taken here,
3141 explicit function specializations will always appear with
3144 The action recommended by the C++ CWG in response to C++
3145 defect report 605 is to make the storage class and linkage
3146 of the explicit specialization match the templated function:
3148 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3150 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3152 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3153 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3155 /* A concept cannot be specialized. */
3156 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3158 error ("explicit specialization of function concept %qD",
3160 return error_mark_node
;
3163 /* This specialization has the same linkage and visibility as
3164 the function template it specializes. */
3165 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3166 if (! TREE_PUBLIC (decl
))
3168 DECL_INTERFACE_KNOWN (decl
) = 1;
3169 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3171 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3172 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3174 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3175 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3179 /* If DECL is a friend declaration, declared using an
3180 unqualified name, the namespace associated with DECL may
3181 have been set incorrectly. For example, in:
3183 template <typename T> void f(T);
3185 struct S { friend void f<int>(int); }
3188 we will have set the DECL_CONTEXT for the friend
3189 declaration to N, rather than to the global namespace. */
3190 if (DECL_NAMESPACE_SCOPE_P (decl
))
3191 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3193 if (is_friend
&& !have_def
)
3194 /* This is not really a declaration of a specialization.
3195 It's just the name of an instantiation. But, it's not
3196 a request for an instantiation, either. */
3197 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3198 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3199 /* A specialization is not necessarily COMDAT. */
3200 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3201 && DECL_DECLARED_INLINE_P (decl
));
3202 else if (VAR_P (decl
))
3203 DECL_COMDAT (decl
) = false;
3205 /* If this is a full specialization, register it so that we can find
3206 it again. Partial specializations will be registered in
3207 process_partial_specialization. */
3208 if (!processing_template_decl
)
3210 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3212 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3217 /* A 'structor should already have clones. */
3218 gcc_assert (decl
== error_mark_node
3219 || variable_template_p (tmpl
)
3220 || !(DECL_CONSTRUCTOR_P (decl
)
3221 || DECL_DESTRUCTOR_P (decl
))
3222 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3229 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3230 parameters. These are represented in the same format used for
3231 DECL_TEMPLATE_PARMS. */
3234 comp_template_parms (const_tree parms1
, const_tree parms2
)
3239 if (parms1
== parms2
)
3242 for (p1
= parms1
, p2
= parms2
;
3243 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3244 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3246 tree t1
= TREE_VALUE (p1
);
3247 tree t2
= TREE_VALUE (p2
);
3250 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3251 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3253 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3256 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3258 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3259 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3261 /* If either of the template parameters are invalid, assume
3262 they match for the sake of error recovery. */
3263 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3266 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3269 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3270 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3271 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3273 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3278 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3279 /* One set of parameters has more parameters lists than the
3286 /* Determine whether PARM is a parameter pack. */
3289 template_parameter_pack_p (const_tree parm
)
3291 /* Determine if we have a non-type template parameter pack. */
3292 if (TREE_CODE (parm
) == PARM_DECL
)
3293 return (DECL_TEMPLATE_PARM_P (parm
)
3294 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3295 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3296 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3298 /* If this is a list of template parameters, we could get a
3299 TYPE_DECL or a TEMPLATE_DECL. */
3300 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3301 parm
= TREE_TYPE (parm
);
3303 /* Otherwise it must be a type template parameter. */
3304 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3305 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3306 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3309 /* Determine if T is a function parameter pack. */
3312 function_parameter_pack_p (const_tree t
)
3314 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3315 return DECL_PACK_P (t
);
3319 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3320 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3323 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3325 if (! primary_func_tmpl_inst
3326 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3327 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3330 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3333 /* Return true iff the function parameter PARAM_DECL was expanded
3334 from the function parameter pack PACK. */
3337 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3339 if (DECL_ARTIFICIAL (param_decl
)
3340 || !function_parameter_pack_p (pack
))
3343 /* The parameter pack and its pack arguments have the same
3345 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3348 /* Determine whether ARGS describes a variadic template args list,
3349 i.e., one that is terminated by a template argument pack. */
3352 template_args_variadic_p (tree args
)
3357 if (args
== NULL_TREE
)
3360 args
= INNERMOST_TEMPLATE_ARGS (args
);
3361 nargs
= TREE_VEC_LENGTH (args
);
3366 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3368 return ARGUMENT_PACK_P (last_parm
);
3371 /* Generate a new name for the parameter pack name NAME (an
3372 IDENTIFIER_NODE) that incorporates its */
3375 make_ith_pack_parameter_name (tree name
, int i
)
3377 /* Munge the name to include the parameter index. */
3378 #define NUMBUF_LEN 128
3379 char numbuf
[NUMBUF_LEN
];
3383 if (name
== NULL_TREE
)
3385 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3386 newname_len
= IDENTIFIER_LENGTH (name
)
3387 + strlen (numbuf
) + 2;
3388 newname
= (char*)alloca (newname_len
);
3389 snprintf (newname
, newname_len
,
3390 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3391 return get_identifier (newname
);
3394 /* Return true if T is a primary function, class or alias template
3395 specialization, not including the template pattern. */
3398 primary_template_specialization_p (const_tree t
)
3403 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3404 return (DECL_LANG_SPECIFIC (t
)
3405 && DECL_USE_TEMPLATE (t
)
3406 && DECL_TEMPLATE_INFO (t
)
3407 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3408 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3409 return (CLASSTYPE_TEMPLATE_INFO (t
)
3410 && CLASSTYPE_USE_TEMPLATE (t
)
3411 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3412 else if (alias_template_specialization_p (t
))
3417 /* Return true if PARM is a template template parameter. */
3420 template_template_parameter_p (const_tree parm
)
3422 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3425 /* Return true iff PARM is a DECL representing a type template
3429 template_type_parameter_p (const_tree parm
)
3432 && (TREE_CODE (parm
) == TYPE_DECL
3433 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3434 && DECL_TEMPLATE_PARM_P (parm
));
3437 /* Return the template parameters of T if T is a
3438 primary template instantiation, NULL otherwise. */
3441 get_primary_template_innermost_parameters (const_tree t
)
3443 tree parms
= NULL
, template_info
= NULL
;
3445 if ((template_info
= get_template_info (t
))
3446 && primary_template_specialization_p (t
))
3447 parms
= INNERMOST_TEMPLATE_PARMS
3448 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3453 /* Return the template parameters of the LEVELth level from the full list
3454 of template parameters PARMS. */
3457 get_template_parms_at_level (tree parms
, int level
)
3461 || TREE_CODE (parms
) != TREE_LIST
3462 || level
> TMPL_PARMS_DEPTH (parms
))
3465 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3466 if (TMPL_PARMS_DEPTH (p
) == level
)
3472 /* Returns the template arguments of T if T is a template instantiation,
3476 get_template_innermost_arguments (const_tree t
)
3478 tree args
= NULL
, template_info
= NULL
;
3480 if ((template_info
= get_template_info (t
))
3481 && TI_ARGS (template_info
))
3482 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3487 /* Return the argument pack elements of T if T is a template argument pack,
3491 get_template_argument_pack_elems (const_tree t
)
3493 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3494 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3497 return ARGUMENT_PACK_ARGS (t
);
3500 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3501 ARGUMENT_PACK_SELECT represents. */
3504 argument_pack_select_arg (tree t
)
3506 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3507 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3509 /* If the selected argument is an expansion E, that most likely means we were
3510 called from gen_elem_of_pack_expansion_instantiation during the
3511 substituting of an argument pack (of which the Ith element is a pack
3512 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3513 In this case, the Ith element resulting from this substituting is going to
3514 be a pack expansion, which pattern is the pattern of E. Let's return the
3515 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3516 resulting pack expansion from it. */
3517 if (PACK_EXPANSION_P (arg
))
3519 /* Make sure we aren't throwing away arg info. */
3520 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3521 arg
= PACK_EXPANSION_PATTERN (arg
);
3528 /* True iff FN is a function representing a built-in variadic parameter
3532 builtin_pack_fn_p (tree fn
)
3535 || TREE_CODE (fn
) != FUNCTION_DECL
3536 || !DECL_IS_BUILTIN (fn
))
3539 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3545 /* True iff CALL is a call to a function representing a built-in variadic
3549 builtin_pack_call_p (tree call
)
3551 if (TREE_CODE (call
) != CALL_EXPR
)
3553 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3556 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3559 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3562 tree ohi
= CALL_EXPR_ARG (call
, 0);
3563 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3564 false/*fn*/, true/*int_cst*/);
3566 if (value_dependent_expression_p (hi
))
3570 call
= copy_node (call
);
3571 CALL_EXPR_ARG (call
, 0) = hi
;
3573 tree ex
= make_pack_expansion (call
, complain
);
3574 tree vec
= make_tree_vec (1);
3575 TREE_VEC_ELT (vec
, 0) = ex
;
3580 hi
= cxx_constant_value (hi
);
3581 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3583 /* Calculate the largest value of len that won't make the size of the vec
3584 overflow an int. The compiler will exceed resource limits long before
3585 this, but it seems a decent place to diagnose. */
3586 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3588 if (len
< 0 || len
> max
)
3590 if ((complain
& tf_error
)
3591 && hi
!= error_mark_node
)
3592 error ("argument to __integer_pack must be between 0 and %d", max
);
3593 return error_mark_node
;
3596 tree vec
= make_tree_vec (len
);
3598 for (int i
= 0; i
< len
; ++i
)
3599 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3605 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3609 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3612 if (!builtin_pack_call_p (call
))
3615 tree fn
= CALL_EXPR_FN (call
);
3617 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3618 return expand_integer_pack (call
, args
, complain
, in_decl
);
3623 /* Structure used to track the progress of find_parameter_packs_r. */
3624 struct find_parameter_pack_data
3626 /* TREE_LIST that will contain all of the parameter packs found by
3628 tree
* parameter_packs
;
3630 /* Set of AST nodes that have been visited by the traversal. */
3631 hash_set
<tree
> *visited
;
3633 /* True iff we're making a type pack expansion. */
3634 bool type_pack_expansion_p
;
3637 /* Identifies all of the argument packs that occur in a template
3638 argument and appends them to the TREE_LIST inside DATA, which is a
3639 find_parameter_pack_data structure. This is a subroutine of
3640 make_pack_expansion and uses_parameter_packs. */
3642 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3645 struct find_parameter_pack_data
* ppd
=
3646 (struct find_parameter_pack_data
*)data
;
3647 bool parameter_pack_p
= false;
3649 /* Handle type aliases/typedefs. */
3650 if (TYPE_ALIAS_P (t
))
3652 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3653 cp_walk_tree (&TI_ARGS (tinfo
),
3654 &find_parameter_packs_r
,
3660 /* Identify whether this is a parameter pack or not. */
3661 switch (TREE_CODE (t
))
3663 case TEMPLATE_PARM_INDEX
:
3664 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3665 parameter_pack_p
= true;
3668 case TEMPLATE_TYPE_PARM
:
3669 t
= TYPE_MAIN_VARIANT (t
);
3671 case TEMPLATE_TEMPLATE_PARM
:
3672 /* If the placeholder appears in the decl-specifier-seq of a function
3673 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3674 is a pack expansion, the invented template parameter is a template
3676 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3677 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3678 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3679 parameter_pack_p
= true;
3684 if (DECL_PACK_P (t
))
3686 /* We don't want to walk into the type of a PARM_DECL,
3687 because we don't want to see the type parameter pack. */
3689 parameter_pack_p
= true;
3694 if (DECL_PACK_P (t
))
3696 /* We don't want to walk into the type of a variadic capture proxy,
3697 because we don't want to see the type parameter pack. */
3699 parameter_pack_p
= true;
3701 else if (variable_template_specialization_p (t
))
3703 cp_walk_tree (&DECL_TI_ARGS (t
),
3704 find_parameter_packs_r
,
3711 if (builtin_pack_call_p (t
))
3712 parameter_pack_p
= true;
3716 parameter_pack_p
= true;
3719 /* Not a parameter pack. */
3723 if (parameter_pack_p
)
3725 /* Add this parameter pack to the list. */
3726 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3730 cp_walk_tree (&TYPE_CONTEXT (t
),
3731 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3733 /* This switch statement will return immediately if we don't find a
3735 switch (TREE_CODE (t
))
3737 case TEMPLATE_PARM_INDEX
:
3740 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3741 /* Check the template itself. */
3742 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3743 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3744 /* Check the template arguments. */
3745 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3750 case TEMPLATE_TYPE_PARM
:
3751 case TEMPLATE_TEMPLATE_PARM
:
3758 /* Ignore the declaration of a capture proxy for a parameter pack. */
3759 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3764 if (TYPE_PTRMEMFUNC_P (t
))
3770 if (TYPE_TEMPLATE_INFO (t
))
3771 cp_walk_tree (&TYPE_TI_ARGS (t
),
3772 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3778 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3783 cp_walk_tree (&TREE_TYPE (t
),
3784 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3788 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3793 case TYPE_PACK_EXPANSION
:
3794 case EXPR_PACK_EXPANSION
:
3799 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3804 case IDENTIFIER_NODE
:
3805 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3812 /* Look at explicit captures. */
3813 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3814 cap
; cap
= TREE_CHAIN (cap
))
3815 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3817 /* Since we defer implicit capture, look in the parms and body. */
3818 tree fn
= lambda_function (t
);
3819 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3821 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3829 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3830 type_pack_expansion_p to false so that any placeholders
3831 within the expression don't get marked as parameter packs. */
3832 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3833 ppd
->type_pack_expansion_p
= false;
3834 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3836 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3842 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3844 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3846 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3848 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3859 /* Determines if the expression or type T uses any parameter packs. */
3861 uses_parameter_packs (tree t
)
3863 tree parameter_packs
= NULL_TREE
;
3864 struct find_parameter_pack_data ppd
;
3865 ppd
.parameter_packs
= ¶meter_packs
;
3866 ppd
.visited
= new hash_set
<tree
>;
3867 ppd
.type_pack_expansion_p
= false;
3868 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3870 return parameter_packs
!= NULL_TREE
;
3873 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3874 representation a base-class initializer into a parameter pack
3875 expansion. If all goes well, the resulting node will be an
3876 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3879 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3882 tree parameter_packs
= NULL_TREE
;
3883 bool for_types
= false;
3884 struct find_parameter_pack_data ppd
;
3886 if (!arg
|| arg
== error_mark_node
)
3889 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3891 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3892 class initializer. In this case, the TREE_PURPOSE will be a
3893 _TYPE node (representing the base class expansion we're
3894 initializing) and the TREE_VALUE will be a TREE_LIST
3895 containing the initialization arguments.
3897 The resulting expansion looks somewhat different from most
3898 expansions. Rather than returning just one _EXPANSION, we
3899 return a TREE_LIST whose TREE_PURPOSE is a
3900 TYPE_PACK_EXPANSION containing the bases that will be
3901 initialized. The TREE_VALUE will be identical to the
3902 original TREE_VALUE, which is a list of arguments that will
3903 be passed to each base. We do not introduce any new pack
3904 expansion nodes into the TREE_VALUE (although it is possible
3905 that some already exist), because the TREE_PURPOSE and
3906 TREE_VALUE all need to be expanded together with the same
3907 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3908 resulting TREE_PURPOSE will mention the parameter packs in
3909 both the bases and the arguments to the bases. */
3912 tree parameter_packs
= NULL_TREE
;
3914 /* Determine which parameter packs will be used by the base
3916 ppd
.visited
= new hash_set
<tree
>;
3917 ppd
.parameter_packs
= ¶meter_packs
;
3918 ppd
.type_pack_expansion_p
= true;
3919 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3920 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3923 if (parameter_packs
== NULL_TREE
)
3925 if (complain
& tf_error
)
3926 error ("base initializer expansion %qT contains no parameter packs",
3929 return error_mark_node
;
3932 if (TREE_VALUE (arg
) != void_type_node
)
3934 /* Collect the sets of parameter packs used in each of the
3935 initialization arguments. */
3936 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3938 /* Determine which parameter packs will be expanded in this
3940 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3947 /* Create the pack expansion type for the base type. */
3948 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3949 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3950 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3951 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3953 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3954 they will rarely be compared to anything. */
3955 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3957 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3960 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3963 /* Build the PACK_EXPANSION_* node. */
3965 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3966 : make_node (EXPR_PACK_EXPANSION
);
3967 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3968 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3970 /* Propagate type and const-expression information. */
3971 TREE_TYPE (result
) = TREE_TYPE (arg
);
3972 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3973 /* Mark this read now, since the expansion might be length 0. */
3974 mark_exp_read (arg
);
3977 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3978 they will rarely be compared to anything. */
3979 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3981 /* Determine which parameter packs will be expanded. */
3982 ppd
.parameter_packs
= ¶meter_packs
;
3983 ppd
.visited
= new hash_set
<tree
>;
3984 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3985 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3988 /* Make sure we found some parameter packs. */
3989 if (parameter_packs
== NULL_TREE
)
3991 if (complain
& tf_error
)
3994 error ("expansion pattern %qT contains no parameter packs", arg
);
3996 error ("expansion pattern %qE contains no parameter packs", arg
);
3998 return error_mark_node
;
4000 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4002 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4007 /* Checks T for any "bare" parameter packs, which have not yet been
4008 expanded, and issues an error if any are found. This operation can
4009 only be done on full expressions or types (e.g., an expression
4010 statement, "if" condition, etc.), because we could have expressions like:
4012 foo(f(g(h(args)))...)
4014 where "args" is a parameter pack. check_for_bare_parameter_packs
4015 should not be called for the subexpressions args, h(args),
4016 g(h(args)), or f(g(h(args))), because we would produce erroneous
4019 Returns TRUE and emits an error if there were bare parameter packs,
4020 returns FALSE otherwise. */
4022 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4024 tree parameter_packs
= NULL_TREE
;
4025 struct find_parameter_pack_data ppd
;
4027 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4030 /* A lambda might use a parameter pack from the containing context. */
4031 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4032 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4035 if (TREE_CODE (t
) == TYPE_DECL
)
4038 ppd
.parameter_packs
= ¶meter_packs
;
4039 ppd
.visited
= new hash_set
<tree
>;
4040 ppd
.type_pack_expansion_p
= false;
4041 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4044 if (parameter_packs
)
4046 if (loc
== UNKNOWN_LOCATION
)
4047 loc
= cp_expr_loc_or_loc (t
, input_location
);
4048 error_at (loc
, "parameter packs not expanded with %<...%>:");
4049 while (parameter_packs
)
4051 tree pack
= TREE_VALUE (parameter_packs
);
4052 tree name
= NULL_TREE
;
4054 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4055 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4056 name
= TYPE_NAME (pack
);
4057 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4058 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4059 else if (TREE_CODE (pack
) == CALL_EXPR
)
4060 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4062 name
= DECL_NAME (pack
);
4065 inform (loc
, " %qD", name
);
4067 inform (loc
, " <anonymous>");
4069 parameter_packs
= TREE_CHAIN (parameter_packs
);
4078 /* Expand any parameter packs that occur in the template arguments in
4081 expand_template_argument_pack (tree args
)
4083 if (args
== error_mark_node
)
4084 return error_mark_node
;
4086 tree result_args
= NULL_TREE
;
4087 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4088 int num_result_args
= -1;
4089 int non_default_args_count
= -1;
4091 /* First, determine if we need to expand anything, and the number of
4092 slots we'll need. */
4093 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4095 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4096 if (arg
== NULL_TREE
)
4098 if (ARGUMENT_PACK_P (arg
))
4100 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4101 if (num_result_args
< 0)
4102 num_result_args
= in_arg
+ num_packed
;
4104 num_result_args
+= num_packed
;
4108 if (num_result_args
>= 0)
4113 /* If no expansion is necessary, we're done. */
4114 if (num_result_args
< 0)
4117 /* Expand arguments. */
4118 result_args
= make_tree_vec (num_result_args
);
4119 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4120 non_default_args_count
=
4121 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4122 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4124 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4125 if (ARGUMENT_PACK_P (arg
))
4127 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4128 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4129 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4130 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4131 if (non_default_args_count
> 0)
4132 non_default_args_count
+= num_packed
- 1;
4136 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4140 if (non_default_args_count
>= 0)
4141 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4145 /* Checks if DECL shadows a template parameter.
4147 [temp.local]: A template-parameter shall not be redeclared within its
4148 scope (including nested scopes).
4150 Emits an error and returns TRUE if the DECL shadows a parameter,
4151 returns FALSE otherwise. */
4154 check_template_shadow (tree decl
)
4158 /* If we're not in a template, we can't possibly shadow a template
4160 if (!current_template_parms
)
4163 /* Figure out what we're shadowing. */
4164 decl
= OVL_FIRST (decl
);
4165 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4167 /* If there's no previous binding for this name, we're not shadowing
4168 anything, let alone a template parameter. */
4172 /* If we're not shadowing a template parameter, we're done. Note
4173 that OLDDECL might be an OVERLOAD (or perhaps even an
4174 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4176 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4179 /* We check for decl != olddecl to avoid bogus errors for using a
4180 name inside a class. We check TPFI to avoid duplicate errors for
4181 inline member templates. */
4183 || (DECL_TEMPLATE_PARM_P (decl
)
4184 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4187 /* Don't complain about the injected class name, as we've already
4188 complained about the class itself. */
4189 if (DECL_SELF_REFERENCE_P (decl
))
4192 if (DECL_TEMPLATE_PARM_P (decl
))
4193 error ("declaration of template parameter %q+D shadows "
4194 "template parameter", decl
);
4196 error ("declaration of %q+#D shadows template parameter", decl
);
4197 inform (DECL_SOURCE_LOCATION (olddecl
),
4198 "template parameter %qD declared here", olddecl
);
4202 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4203 ORIG_LEVEL, DECL, and TYPE. */
4206 build_template_parm_index (int index
,
4212 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4213 TEMPLATE_PARM_IDX (t
) = index
;
4214 TEMPLATE_PARM_LEVEL (t
) = level
;
4215 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4216 TEMPLATE_PARM_DECL (t
) = decl
;
4217 TREE_TYPE (t
) = type
;
4218 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4219 TREE_READONLY (t
) = TREE_READONLY (decl
);
4224 /* Find the canonical type parameter for the given template type
4225 parameter. Returns the canonical type parameter, which may be TYPE
4226 if no such parameter existed. */
4229 canonical_type_parameter (tree type
)
4232 int idx
= TEMPLATE_TYPE_IDX (type
);
4233 if (!canonical_template_parms
)
4234 vec_alloc (canonical_template_parms
, idx
+ 1);
4236 if (canonical_template_parms
->length () <= (unsigned) idx
)
4237 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4239 list
= (*canonical_template_parms
)[idx
];
4240 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4241 list
= TREE_CHAIN (list
);
4244 return TREE_VALUE (list
);
4247 (*canonical_template_parms
)[idx
]
4248 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4253 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4254 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4255 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4256 new one is created. */
4259 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4260 tsubst_flags_t complain
)
4262 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4263 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4264 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4265 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4267 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4270 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4271 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4272 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4273 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4274 DECL_ARTIFICIAL (decl
) = 1;
4275 SET_DECL_TEMPLATE_PARM_P (decl
);
4277 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4278 TEMPLATE_PARM_LEVEL (index
) - levels
,
4279 TEMPLATE_PARM_ORIG_LEVEL (index
),
4281 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4282 TEMPLATE_PARM_PARAMETER_PACK (t
)
4283 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4285 /* Template template parameters need this. */
4286 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4288 DECL_TEMPLATE_RESULT (decl
)
4289 = build_decl (DECL_SOURCE_LOCATION (decl
),
4290 TYPE_DECL
, DECL_NAME (decl
), type
);
4291 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4292 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4293 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4297 return TEMPLATE_PARM_DESCENDANTS (index
);
4300 /* Process information from new template parameter PARM and append it
4301 to the LIST being built. This new parameter is a non-type
4302 parameter iff IS_NON_TYPE is true. This new parameter is a
4303 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4307 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4308 bool is_non_type
, bool is_parameter_pack
)
4313 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4314 tree defval
= TREE_PURPOSE (parm
);
4315 tree constr
= TREE_TYPE (parm
);
4319 tree p
= tree_last (list
);
4321 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4324 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4325 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4327 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4335 parm
= TREE_VALUE (parm
);
4337 SET_DECL_TEMPLATE_PARM_P (parm
);
4339 if (TREE_TYPE (parm
) != error_mark_node
)
4343 The top-level cv-qualifiers on the template-parameter are
4344 ignored when determining its type. */
4345 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4346 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4347 TREE_TYPE (parm
) = error_mark_node
;
4348 else if (uses_parameter_packs (TREE_TYPE (parm
))
4349 && !is_parameter_pack
4350 /* If we're in a nested template parameter list, the template
4351 template parameter could be a parameter pack. */
4352 && processing_template_parmlist
== 1)
4354 /* This template parameter is not a parameter pack, but it
4355 should be. Complain about "bare" parameter packs. */
4356 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4358 /* Recover by calling this a parameter pack. */
4359 is_parameter_pack
= true;
4363 /* A template parameter is not modifiable. */
4364 TREE_CONSTANT (parm
) = 1;
4365 TREE_READONLY (parm
) = 1;
4366 decl
= build_decl (parm_loc
,
4367 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4368 TREE_CONSTANT (decl
) = 1;
4369 TREE_READONLY (decl
) = 1;
4370 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4371 = build_template_parm_index (idx
, processing_template_decl
,
4372 processing_template_decl
,
4373 decl
, TREE_TYPE (parm
));
4375 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4376 = is_parameter_pack
;
4381 parm
= TREE_VALUE (TREE_VALUE (parm
));
4383 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4385 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4386 /* This is for distinguishing between real templates and template
4387 template parameters */
4388 TREE_TYPE (parm
) = t
;
4389 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4394 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4395 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4396 decl
= build_decl (parm_loc
,
4397 TYPE_DECL
, parm
, t
);
4400 TYPE_NAME (t
) = decl
;
4401 TYPE_STUB_DECL (t
) = decl
;
4403 TEMPLATE_TYPE_PARM_INDEX (t
)
4404 = build_template_parm_index (idx
, processing_template_decl
,
4405 processing_template_decl
,
4406 decl
, TREE_TYPE (parm
));
4407 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4408 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4410 DECL_ARTIFICIAL (decl
) = 1;
4411 SET_DECL_TEMPLATE_PARM_P (decl
);
4413 /* Build requirements for the type/template parameter.
4414 This must be done after SET_DECL_TEMPLATE_PARM_P or
4415 process_template_parm could fail. */
4416 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4420 /* Build the parameter node linking the parameter declaration,
4421 its default argument (if any), and its constraints (if any). */
4422 parm
= build_tree_list (defval
, parm
);
4423 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4425 return chainon (list
, parm
);
4428 /* The end of a template parameter list has been reached. Process the
4429 tree list into a parameter vector, converting each parameter into a more
4430 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4434 end_template_parm_list (tree parms
)
4438 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4440 /* Pop the dummy parameter level and add the real one. */
4441 current_template_parms
= TREE_CHAIN (current_template_parms
);
4443 current_template_parms
4444 = tree_cons (size_int (processing_template_decl
),
4445 saved_parmlist
, current_template_parms
);
4447 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4449 next
= TREE_CHAIN (parm
);
4450 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4451 TREE_CHAIN (parm
) = NULL_TREE
;
4454 --processing_template_parmlist
;
4456 return saved_parmlist
;
4459 // Explicitly indicate the end of the template parameter list. We assume
4460 // that the current template parameters have been constructed and/or
4461 // managed explicitly, as when creating new template template parameters
4462 // from a shorthand constraint.
4464 end_template_parm_list ()
4466 --processing_template_parmlist
;
4469 /* end_template_decl is called after a template declaration is seen. */
4472 end_template_decl (void)
4474 reset_specialization ();
4476 if (! processing_template_decl
)
4479 /* This matches the pushlevel in begin_template_parm_list. */
4482 --processing_template_decl
;
4483 current_template_parms
= TREE_CHAIN (current_template_parms
);
4486 /* Takes a TREE_LIST representing a template parameter and convert it
4487 into an argument suitable to be passed to the type substitution
4488 functions. Note that If the TREE_LIST contains an error_mark
4489 node, the returned argument is error_mark_node. */
4492 template_parm_to_arg (tree t
)
4496 || TREE_CODE (t
) != TREE_LIST
)
4499 if (error_operand_p (TREE_VALUE (t
)))
4500 return error_mark_node
;
4504 if (TREE_CODE (t
) == TYPE_DECL
4505 || TREE_CODE (t
) == TEMPLATE_DECL
)
4509 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4511 /* Turn this argument into a TYPE_ARGUMENT_PACK
4512 with a single element, which expands T. */
4513 tree vec
= make_tree_vec (1);
4515 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4517 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4519 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4520 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4525 t
= DECL_INITIAL (t
);
4527 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4529 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4530 with a single element, which expands T. */
4531 tree vec
= make_tree_vec (1);
4533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4535 t
= convert_from_reference (t
);
4536 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4538 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4539 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4542 t
= convert_from_reference (t
);
4547 /* Given a single level of template parameters (a TREE_VEC), return it
4548 as a set of template arguments. */
4551 template_parms_level_to_args (tree parms
)
4553 tree a
= copy_node (parms
);
4554 TREE_TYPE (a
) = NULL_TREE
;
4555 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4556 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4559 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4564 /* Given a set of template parameters, return them as a set of template
4565 arguments. The template parameters are represented as a TREE_VEC, in
4566 the form documented in cp-tree.h for template arguments. */
4569 template_parms_to_args (tree parms
)
4572 tree args
= NULL_TREE
;
4573 int length
= TMPL_PARMS_DEPTH (parms
);
4576 /* If there is only one level of template parameters, we do not
4577 create a TREE_VEC of TREE_VECs. Instead, we return a single
4578 TREE_VEC containing the arguments. */
4580 args
= make_tree_vec (length
);
4582 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4584 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4587 TREE_VEC_ELT (args
, --l
) = a
;
4595 /* Within the declaration of a template, return the currently active
4596 template parameters as an argument TREE_VEC. */
4599 current_template_args (void)
4601 return template_parms_to_args (current_template_parms
);
4604 /* Update the declared TYPE by doing any lookups which were thought to be
4605 dependent, but are not now that we know the SCOPE of the declarator. */
4608 maybe_update_decl_type (tree orig_type
, tree scope
)
4610 tree type
= orig_type
;
4612 if (type
== NULL_TREE
)
4615 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4616 type
= TREE_TYPE (type
);
4618 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4619 && dependent_type_p (type
)
4620 /* Don't bother building up the args in this case. */
4621 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4623 /* tsubst in the args corresponding to the template parameters,
4624 including auto if present. Most things will be unchanged, but
4625 make_typename_type and tsubst_qualified_id will resolve
4626 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4627 tree args
= current_template_args ();
4628 tree auto_node
= type_uses_auto (type
);
4632 tree auto_vec
= make_tree_vec (1);
4633 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4634 args
= add_to_template_args (args
, auto_vec
);
4636 pushed
= push_scope (scope
);
4637 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4642 if (type
== error_mark_node
)
4645 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4647 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4650 type
= TYPE_NAME (type
);
4655 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4656 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4657 the new template is a member template. */
4660 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4662 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4663 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4664 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4665 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4666 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4667 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4672 struct template_parm_data
4674 /* The level of the template parameters we are currently
4678 /* The index of the specialization argument we are currently
4682 /* An array whose size is the number of template parameters. The
4683 elements are nonzero if the parameter has been used in any one
4684 of the arguments processed so far. */
4687 /* An array whose size is the number of template arguments. The
4688 elements are nonzero if the argument makes use of template
4689 parameters of this level. */
4690 int* arg_uses_template_parms
;
4693 /* Subroutine of push_template_decl used to see if each template
4694 parameter in a partial specialization is used in the explicit
4695 argument list. If T is of the LEVEL given in DATA (which is
4696 treated as a template_parm_data*), then DATA->PARMS is marked
4700 mark_template_parm (tree t
, void* data
)
4704 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4706 template_parm_level_and_index (t
, &level
, &idx
);
4708 if (level
== tpd
->level
)
4710 tpd
->parms
[idx
] = 1;
4711 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4714 /* In C++17 the type of a non-type argument is a deduced context. */
4715 if (cxx_dialect
>= cxx17
4716 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4717 for_each_template_parm (TREE_TYPE (t
),
4718 &mark_template_parm
,
4721 /*include_nondeduced_p=*/false);
4723 /* Return zero so that for_each_template_parm will continue the
4724 traversal of the tree; we want to mark *every* template parm. */
4728 /* Process the partial specialization DECL. */
4731 process_partial_specialization (tree decl
)
4733 tree type
= TREE_TYPE (decl
);
4734 tree tinfo
= get_template_info (decl
);
4735 tree maintmpl
= TI_TEMPLATE (tinfo
);
4736 tree specargs
= TI_ARGS (tinfo
);
4737 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4738 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4741 int nargs
= TREE_VEC_LENGTH (inner_args
);
4744 bool did_error_intro
= false;
4745 struct template_parm_data tpd
;
4746 struct template_parm_data tpd2
;
4748 gcc_assert (current_template_parms
);
4750 /* A concept cannot be specialized. */
4751 if (flag_concepts
&& variable_concept_p (maintmpl
))
4753 error ("specialization of variable concept %q#D", maintmpl
);
4754 return error_mark_node
;
4757 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4758 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4760 /* We check that each of the template parameters given in the
4761 partial specialization is used in the argument list to the
4762 specialization. For example:
4764 template <class T> struct S;
4765 template <class T> struct S<T*>;
4767 The second declaration is OK because `T*' uses the template
4768 parameter T, whereas
4770 template <class T> struct S<int>;
4772 is no good. Even trickier is:
4783 The S2<T> declaration is actually invalid; it is a
4784 full-specialization. Of course,
4787 struct S2<T (*)(U)>;
4789 or some such would have been OK. */
4790 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4791 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4792 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4794 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4795 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4796 for (i
= 0; i
< nargs
; ++i
)
4798 tpd
.current_arg
= i
;
4799 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4800 &mark_template_parm
,
4803 /*include_nondeduced_p=*/false);
4805 for (i
= 0; i
< ntparms
; ++i
)
4806 if (tpd
.parms
[i
] == 0)
4808 /* One of the template parms was not used in a deduced context in the
4810 if (!did_error_intro
)
4812 error ("template parameters not deducible in "
4813 "partial specialization:");
4814 did_error_intro
= true;
4817 inform (input_location
, " %qD",
4818 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4821 if (did_error_intro
)
4822 return error_mark_node
;
4824 /* [temp.class.spec]
4826 The argument list of the specialization shall not be identical to
4827 the implicit argument list of the primary template. */
4829 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4830 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4832 || !strictly_subsumes (current_template_constraints (),
4833 get_constraints (maintmpl
))))
4836 error ("partial specialization %q+D does not specialize "
4837 "any template arguments; to define the primary template, "
4838 "remove the template argument list", decl
);
4840 error ("partial specialization %q+D does not specialize any "
4841 "template arguments and is not more constrained than "
4842 "the primary template; to define the primary template, "
4843 "remove the template argument list", decl
);
4844 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4847 /* A partial specialization that replaces multiple parameters of the
4848 primary template with a pack expansion is less specialized for those
4850 if (nargs
< DECL_NTPARMS (maintmpl
))
4852 error ("partial specialization is not more specialized than the "
4853 "primary template because it replaces multiple parameters "
4854 "with a pack expansion");
4855 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4856 /* Avoid crash in process_partial_specialization. */
4860 /* If we aren't in a dependent class, we can actually try deduction. */
4861 else if (tpd
.level
== 1
4862 /* FIXME we should be able to handle a partial specialization of a
4863 partial instantiation, but currently we can't (c++/41727). */
4864 && TMPL_ARGS_DEPTH (specargs
) == 1
4865 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4867 auto_diagnostic_group d
;
4868 if (permerror (input_location
, "partial specialization %qD is not "
4869 "more specialized than", decl
))
4870 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4874 /* [temp.class.spec]
4876 A partially specialized non-type argument expression shall not
4877 involve template parameters of the partial specialization except
4878 when the argument expression is a simple identifier.
4880 The type of a template parameter corresponding to a specialized
4881 non-type argument shall not be dependent on a parameter of the
4884 Also, we verify that pack expansions only occur at the
4885 end of the argument list. */
4886 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4888 for (i
= 0; i
< nargs
; ++i
)
4890 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4891 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4892 tree packed_args
= NULL_TREE
;
4895 if (ARGUMENT_PACK_P (arg
))
4897 /* Extract the arguments from the argument pack. We'll be
4898 iterating over these in the following loop. */
4899 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4900 len
= TREE_VEC_LENGTH (packed_args
);
4903 for (j
= 0; j
< len
; j
++)
4906 /* Get the Jth argument in the parameter pack. */
4907 arg
= TREE_VEC_ELT (packed_args
, j
);
4909 if (PACK_EXPANSION_P (arg
))
4911 /* Pack expansions must come at the end of the
4913 if ((packed_args
&& j
< len
- 1)
4914 || (!packed_args
&& i
< nargs
- 1))
4916 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4917 error ("parameter pack argument %qE must be at the "
4918 "end of the template argument list", arg
);
4920 error ("parameter pack argument %qT must be at the "
4921 "end of the template argument list", arg
);
4925 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4926 /* We only care about the pattern. */
4927 arg
= PACK_EXPANSION_PATTERN (arg
);
4929 if (/* These first two lines are the `non-type' bit. */
4931 && TREE_CODE (arg
) != TEMPLATE_DECL
4932 /* This next two lines are the `argument expression is not just a
4933 simple identifier' condition and also the `specialized
4934 non-type argument' bit. */
4935 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4936 && !(REFERENCE_REF_P (arg
)
4937 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4939 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4940 || (packed_args
&& uses_template_parms (arg
)))
4941 error ("template argument %qE involves template parameter(s)",
4945 /* Look at the corresponding template parameter,
4946 marking which template parameters its type depends
4948 tree type
= TREE_TYPE (parm
);
4952 /* We haven't yet initialized TPD2. Do so now. */
4953 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4954 /* The number of parameters here is the number in the
4955 main template, which, as checked in the assertion
4957 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4959 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4962 /* Mark the template parameters. But this time, we're
4963 looking for the template parameters of the main
4964 template, not in the specialization. */
4965 tpd2
.current_arg
= i
;
4966 tpd2
.arg_uses_template_parms
[i
] = 0;
4967 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4968 for_each_template_parm (type
,
4969 &mark_template_parm
,
4972 /*include_nondeduced_p=*/false);
4974 if (tpd2
.arg_uses_template_parms
[i
])
4976 /* The type depended on some template parameters.
4977 If they are fully specialized in the
4978 specialization, that's OK. */
4981 for (j
= 0; j
< nargs
; ++j
)
4982 if (tpd2
.parms
[j
] != 0
4983 && tpd
.arg_uses_template_parms
[j
])
4986 error_n (input_location
, count
,
4987 "type %qT of template argument %qE depends "
4988 "on a template parameter",
4989 "type %qT of template argument %qE depends "
4990 "on template parameters",
4999 /* We should only get here once. */
5000 if (TREE_CODE (decl
) == TYPE_DECL
)
5001 gcc_assert (!COMPLETE_TYPE_P (type
));
5003 // Build the template decl.
5004 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5005 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5006 TREE_TYPE (tmpl
) = type
;
5007 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5008 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5009 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5010 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5012 /* Give template template parms a DECL_CONTEXT of the template
5013 for which they are a parameter. */
5014 for (i
= 0; i
< ntparms
; ++i
)
5016 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5017 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5018 DECL_CONTEXT (parm
) = tmpl
;
5022 /* We didn't register this in check_explicit_specialization so we could
5023 wait until the constraints were set. */
5024 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5026 associate_classtype_constraints (type
);
5028 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5029 = tree_cons (specargs
, tmpl
,
5030 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5031 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5033 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5034 inst
= TREE_CHAIN (inst
))
5036 tree instance
= TREE_VALUE (inst
);
5037 if (TYPE_P (instance
)
5038 ? (COMPLETE_TYPE_P (instance
)
5039 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5040 : DECL_TEMPLATE_INSTANTIATION (instance
))
5042 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5043 tree inst_decl
= (DECL_P (instance
)
5044 ? instance
: TYPE_NAME (instance
));
5047 else if (spec
== error_mark_node
)
5048 permerror (input_location
,
5049 "declaration of %qD ambiguates earlier template "
5050 "instantiation for %qD", decl
, inst_decl
);
5051 else if (TREE_VALUE (spec
) == tmpl
)
5052 permerror (input_location
,
5053 "partial specialization of %qD after instantiation "
5054 "of %qD", decl
, inst_decl
);
5061 /* PARM is a template parameter of some form; return the corresponding
5062 TEMPLATE_PARM_INDEX. */
5065 get_template_parm_index (tree parm
)
5067 if (TREE_CODE (parm
) == PARM_DECL
5068 || TREE_CODE (parm
) == CONST_DECL
)
5069 parm
= DECL_INITIAL (parm
);
5070 else if (TREE_CODE (parm
) == TYPE_DECL
5071 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5072 parm
= TREE_TYPE (parm
);
5073 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5074 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5075 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5076 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5077 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5081 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5082 parameter packs used by the template parameter PARM. */
5085 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5087 /* A type parm can't refer to another parm. */
5088 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5090 else if (TREE_CODE (parm
) == PARM_DECL
)
5092 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5097 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5099 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5100 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5101 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5104 /* PARM is a template parameter pack. Return any parameter packs used in
5105 its type or the type of any of its template parameters. If there are
5106 any such packs, it will be instantiated into a fixed template parameter
5107 list by partial instantiation rather than be fully deduced. */
5110 fixed_parameter_pack_p (tree parm
)
5112 /* This can only be true in a member template. */
5113 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5115 /* This can only be true for a parameter pack. */
5116 if (!template_parameter_pack_p (parm
))
5118 /* A type parm can't refer to another parm. */
5119 if (TREE_CODE (parm
) == TYPE_DECL
)
5122 tree parameter_packs
= NULL_TREE
;
5123 struct find_parameter_pack_data ppd
;
5124 ppd
.parameter_packs
= ¶meter_packs
;
5125 ppd
.visited
= new hash_set
<tree
>;
5126 ppd
.type_pack_expansion_p
= false;
5128 fixed_parameter_pack_p_1 (parm
, &ppd
);
5131 return parameter_packs
;
5134 /* Check that a template declaration's use of default arguments and
5135 parameter packs is not invalid. Here, PARMS are the template
5136 parameters. IS_PRIMARY is true if DECL is the thing declared by
5137 a primary template. IS_PARTIAL is true if DECL is a partial
5140 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5141 function template declaration or a friend class template
5142 declaration. In the function case, 1 indicates a declaration, 2
5143 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5144 emitted for extraneous default arguments.
5146 Returns TRUE if there were no errors found, FALSE otherwise. */
5149 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5150 bool is_partial
, int is_friend_decl
)
5153 int last_level_to_check
;
5155 bool no_errors
= true;
5159 A default template-argument shall not be specified in a
5160 function template declaration or a function template definition, nor
5161 in the template-parameter-list of the definition of a member of a
5164 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5165 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5166 /* You can't have a function template declaration in a local
5167 scope, nor you can you define a member of a class template in a
5171 if ((TREE_CODE (decl
) == TYPE_DECL
5173 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5174 || (TREE_CODE (decl
) == FUNCTION_DECL
5175 && LAMBDA_FUNCTION_P (decl
)))
5176 /* A lambda doesn't have an explicit declaration; don't complain
5177 about the parms of the enclosing class. */
5180 if (current_class_type
5181 && !TYPE_BEING_DEFINED (current_class_type
)
5182 && DECL_LANG_SPECIFIC (decl
)
5183 && DECL_DECLARES_FUNCTION_P (decl
)
5184 /* If this is either a friend defined in the scope of the class
5185 or a member function. */
5186 && (DECL_FUNCTION_MEMBER_P (decl
)
5187 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5188 : DECL_FRIEND_CONTEXT (decl
)
5189 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5191 /* And, if it was a member function, it really was defined in
5192 the scope of the class. */
5193 && (!DECL_FUNCTION_MEMBER_P (decl
)
5194 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5195 /* We already checked these parameters when the template was
5196 declared, so there's no need to do it again now. This function
5197 was defined in class scope, but we're processing its body now
5198 that the class is complete. */
5201 /* Core issue 226 (C++0x only): the following only applies to class
5204 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5208 If a template-parameter has a default template-argument, all
5209 subsequent template-parameters shall have a default
5210 template-argument supplied. */
5211 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5213 tree inner_parms
= TREE_VALUE (parm_level
);
5214 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5215 int seen_def_arg_p
= 0;
5218 for (i
= 0; i
< ntparms
; ++i
)
5220 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5222 if (parm
== error_mark_node
)
5225 if (TREE_PURPOSE (parm
))
5227 else if (seen_def_arg_p
5228 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5230 error ("no default argument for %qD", TREE_VALUE (parm
));
5231 /* For better subsequent error-recovery, we indicate that
5232 there should have been a default argument. */
5233 TREE_PURPOSE (parm
) = error_mark_node
;
5236 else if (!is_partial
5238 /* Don't complain about an enclosing partial
5240 && parm_level
== parms
5241 && TREE_CODE (decl
) == TYPE_DECL
5243 && template_parameter_pack_p (TREE_VALUE (parm
))
5244 /* A fixed parameter pack will be partially
5245 instantiated into a fixed length list. */
5246 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5248 /* A primary class template can only have one
5249 parameter pack, at the end of the template
5252 error ("parameter pack %q+D must be at the end of the"
5253 " template parameter list", TREE_VALUE (parm
));
5255 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5263 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5267 /* For an ordinary class template, default template arguments are
5268 allowed at the innermost level, e.g.:
5269 template <class T = int>
5271 but, in a partial specialization, they're not allowed even
5272 there, as we have in [temp.class.spec]:
5274 The template parameter list of a specialization shall not
5275 contain default template argument values.
5277 So, for a partial specialization, or for a function template
5278 (in C++98/C++03), we look at all of them. */
5281 /* But, for a primary class template that is not a partial
5282 specialization we look at all template parameters except the
5284 parms
= TREE_CHAIN (parms
);
5286 /* Figure out what error message to issue. */
5287 if (is_friend_decl
== 2)
5288 msg
= G_("default template arguments may not be used in function template "
5289 "friend re-declaration");
5290 else if (is_friend_decl
)
5291 msg
= G_("default template arguments may not be used in template "
5292 "friend declarations");
5293 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5294 msg
= G_("default template arguments may not be used in function templates "
5295 "without -std=c++11 or -std=gnu++11");
5296 else if (is_partial
)
5297 msg
= G_("default template arguments may not be used in "
5298 "partial specializations");
5299 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5300 msg
= G_("default argument for template parameter for class enclosing %qD");
5302 /* Per [temp.param]/9, "A default template-argument shall not be
5303 specified in the template-parameter-lists of the definition of
5304 a member of a class template that appears outside of the member's
5305 class.", thus if we aren't handling a member of a class template
5306 there is no need to examine the parameters. */
5309 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5310 /* If we're inside a class definition, there's no need to
5311 examine the parameters to the class itself. On the one
5312 hand, they will be checked when the class is defined, and,
5313 on the other, default arguments are valid in things like:
5314 template <class T = double>
5315 struct S { template <class U> void f(U); };
5316 Here the default argument for `S' has no bearing on the
5317 declaration of `f'. */
5318 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5320 /* Check everything. */
5321 last_level_to_check
= 0;
5323 for (parm_level
= parms
;
5324 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5325 parm_level
= TREE_CHAIN (parm_level
))
5327 tree inner_parms
= TREE_VALUE (parm_level
);
5331 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5332 for (i
= 0; i
< ntparms
; ++i
)
5334 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5337 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5342 if (is_friend_decl
== 2)
5349 /* Clear out the default argument so that we are not
5351 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5355 /* At this point, if we're still interested in issuing messages,
5356 they must apply to classes surrounding the object declared. */
5358 msg
= G_("default argument for template parameter for class "
5365 /* Worker for push_template_decl_real, called via
5366 for_each_template_parm. DATA is really an int, indicating the
5367 level of the parameters we are interested in. If T is a template
5368 parameter of that level, return nonzero. */
5371 template_parm_this_level_p (tree t
, void* data
)
5373 int this_level
= *(int *)data
;
5376 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5377 level
= TEMPLATE_PARM_LEVEL (t
);
5379 level
= TEMPLATE_TYPE_LEVEL (t
);
5380 return level
== this_level
;
5383 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5384 DATA is really an int, indicating the innermost outer level of parameters.
5385 If T is a template parameter of that level or further out, return
5389 template_parm_outer_level (tree t
, void *data
)
5391 int this_level
= *(int *)data
;
5394 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5395 level
= TEMPLATE_PARM_LEVEL (t
);
5397 level
= TEMPLATE_TYPE_LEVEL (t
);
5398 return level
<= this_level
;
5401 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5402 parameters given by current_template_args, or reuses a
5403 previously existing one, if appropriate. Returns the DECL, or an
5404 equivalent one, if it is replaced via a call to duplicate_decls.
5406 If IS_FRIEND is true, DECL is a friend declaration. */
5409 push_template_decl_real (tree decl
, bool is_friend
)
5417 int new_template_p
= 0;
5418 /* True if the template is a member template, in the sense of
5420 bool member_template_p
= false;
5422 if (decl
== error_mark_node
|| !current_template_parms
)
5423 return error_mark_node
;
5425 /* See if this is a partial specialization. */
5426 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5427 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5428 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5430 && DECL_LANG_SPECIFIC (decl
)
5431 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5432 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5434 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5438 /* For a friend, we want the context of the friend, not
5439 the type of which it is a friend. */
5440 ctx
= CP_DECL_CONTEXT (decl
);
5441 else if (CP_DECL_CONTEXT (decl
)
5442 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5443 /* In the case of a virtual function, we want the class in which
5445 ctx
= CP_DECL_CONTEXT (decl
);
5447 /* Otherwise, if we're currently defining some class, the DECL
5448 is assumed to be a member of the class. */
5449 ctx
= current_scope ();
5451 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5454 if (!DECL_CONTEXT (decl
))
5455 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5457 /* See if this is a primary template. */
5458 if (is_friend
&& ctx
5459 && uses_template_parms_level (ctx
, processing_template_decl
))
5460 /* A friend template that specifies a class context, i.e.
5461 template <typename T> friend void A<T>::f();
5464 else if (TREE_CODE (decl
) == TYPE_DECL
5465 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5468 is_primary
= template_parm_scope_p ();
5472 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5474 if (DECL_CLASS_SCOPE_P (decl
))
5475 member_template_p
= true;
5476 if (TREE_CODE (decl
) == TYPE_DECL
5477 && anon_aggrname_p (DECL_NAME (decl
)))
5479 error ("template class without a name");
5480 return error_mark_node
;
5482 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5484 if (member_template_p
)
5486 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5487 error ("member template %qD may not have virt-specifiers", decl
);
5489 if (DECL_DESTRUCTOR_P (decl
))
5493 A destructor shall not be a member template. */
5494 error ("destructor %qD declared as member template", decl
);
5495 return error_mark_node
;
5497 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5498 && (!prototype_p (TREE_TYPE (decl
))
5499 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5500 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5501 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5502 == void_list_node
)))
5504 /* [basic.stc.dynamic.allocation]
5506 An allocation function can be a function
5507 template. ... Template allocation functions shall
5508 have two or more parameters. */
5509 error ("invalid template declaration of %qD", decl
);
5510 return error_mark_node
;
5513 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5514 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5516 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5517 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5518 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5520 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5521 if (TREE_CODE (t
) == TYPE_DECL
)
5523 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5524 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5527 else if (TREE_CODE (decl
) == TYPE_DECL
5528 && TYPE_DECL_ALIAS_P (decl
))
5529 /* alias-declaration */
5530 gcc_assert (!DECL_ARTIFICIAL (decl
));
5531 else if (VAR_P (decl
))
5532 /* C++14 variable template. */;
5535 error ("template declaration of %q#D", decl
);
5536 return error_mark_node
;
5540 /* Check to see that the rules regarding the use of default
5541 arguments are not being violated. We check args for a friend
5542 functions when we know whether it's a definition, introducing
5543 declaration or re-declaration. */
5544 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5545 check_default_tmpl_args (decl
, current_template_parms
,
5546 is_primary
, is_partial
, is_friend
);
5548 /* Ensure that there are no parameter packs in the type of this
5549 declaration that have not been expanded. */
5550 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5552 /* Check each of the arguments individually to see if there are
5553 any bare parameter packs. */
5554 tree type
= TREE_TYPE (decl
);
5555 tree arg
= DECL_ARGUMENTS (decl
);
5556 tree argtype
= TYPE_ARG_TYPES (type
);
5558 while (arg
&& argtype
)
5560 if (!DECL_PACK_P (arg
)
5561 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5563 /* This is a PARM_DECL that contains unexpanded parameter
5564 packs. We have already complained about this in the
5565 check_for_bare_parameter_packs call, so just replace
5566 these types with ERROR_MARK_NODE. */
5567 TREE_TYPE (arg
) = error_mark_node
;
5568 TREE_VALUE (argtype
) = error_mark_node
;
5571 arg
= DECL_CHAIN (arg
);
5572 argtype
= TREE_CHAIN (argtype
);
5575 /* Check for bare parameter packs in the return type and the
5576 exception specifiers. */
5577 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5578 /* Errors were already issued, set return type to int
5579 as the frontend doesn't expect error_mark_node as
5581 TREE_TYPE (type
) = integer_type_node
;
5582 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5583 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5585 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5586 && TYPE_DECL_ALIAS_P (decl
))
5587 ? DECL_ORIGINAL_TYPE (decl
)
5588 : TREE_TYPE (decl
)))
5590 TREE_TYPE (decl
) = error_mark_node
;
5591 return error_mark_node
;
5595 return process_partial_specialization (decl
);
5597 args
= current_template_args ();
5600 || TREE_CODE (ctx
) == FUNCTION_DECL
5601 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5602 || (TREE_CODE (decl
) == TYPE_DECL
5603 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5604 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5606 if (DECL_LANG_SPECIFIC (decl
)
5607 && DECL_TEMPLATE_INFO (decl
)
5608 && DECL_TI_TEMPLATE (decl
))
5609 tmpl
= DECL_TI_TEMPLATE (decl
);
5610 /* If DECL is a TYPE_DECL for a class-template, then there won't
5611 be DECL_LANG_SPECIFIC. The information equivalent to
5612 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5613 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5614 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5615 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5617 /* Since a template declaration already existed for this
5618 class-type, we must be redeclaring it here. Make sure
5619 that the redeclaration is valid. */
5620 redeclare_class_template (TREE_TYPE (decl
),
5621 current_template_parms
,
5622 current_template_constraints ());
5623 /* We don't need to create a new TEMPLATE_DECL; just use the
5624 one we already had. */
5625 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5629 tmpl
= build_template_decl (decl
, current_template_parms
,
5633 if (DECL_LANG_SPECIFIC (decl
)
5634 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5636 /* A specialization of a member template of a template
5638 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5639 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5640 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5646 tree a
, t
, current
, parms
;
5648 tree tinfo
= get_template_info (decl
);
5652 error ("template definition of non-template %q#D", decl
);
5653 return error_mark_node
;
5656 tmpl
= TI_TEMPLATE (tinfo
);
5658 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5659 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5660 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5661 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5665 /* The declaration is a specialization of a member
5666 template, declared outside the class. Therefore, the
5667 innermost template arguments will be NULL, so we
5668 replace them with the arguments determined by the
5669 earlier call to check_explicit_specialization. */
5670 args
= DECL_TI_ARGS (decl
);
5673 = build_template_decl (decl
, current_template_parms
,
5675 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5676 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5677 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5678 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5679 DECL_TEMPLATE_INFO (new_tmpl
)
5680 = build_template_info (tmpl
, args
);
5682 register_specialization (new_tmpl
,
5683 most_general_template (tmpl
),
5689 /* Make sure the template headers we got make sense. */
5691 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5692 i
= TMPL_PARMS_DEPTH (parms
);
5693 if (TMPL_ARGS_DEPTH (args
) != i
)
5695 error ("expected %d levels of template parms for %q#D, got %d",
5696 i
, decl
, TMPL_ARGS_DEPTH (args
));
5697 DECL_INTERFACE_KNOWN (decl
) = 1;
5698 return error_mark_node
;
5701 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5703 a
= TMPL_ARGS_LEVEL (args
, i
);
5704 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5706 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5708 if (current
== decl
)
5709 error ("got %d template parameters for %q#D",
5710 TREE_VEC_LENGTH (a
), decl
);
5712 error ("got %d template parameters for %q#T",
5713 TREE_VEC_LENGTH (a
), current
);
5714 error (" but %d required", TREE_VEC_LENGTH (t
));
5715 /* Avoid crash in import_export_decl. */
5716 DECL_INTERFACE_KNOWN (decl
) = 1;
5717 return error_mark_node
;
5720 if (current
== decl
)
5722 else if (current
== NULL_TREE
)
5723 /* Can happen in erroneous input. */
5726 current
= get_containing_scope (current
);
5729 /* Check that the parms are used in the appropriate qualifying scopes
5730 in the declarator. */
5731 if (!comp_template_args
5733 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5735 error ("template arguments to %qD do not match original "
5736 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5737 if (!uses_template_parms (TI_ARGS (tinfo
)))
5738 inform (input_location
, "use %<template<>%> for"
5739 " an explicit specialization");
5740 /* Avoid crash in import_export_decl. */
5741 DECL_INTERFACE_KNOWN (decl
) = 1;
5742 return error_mark_node
;
5746 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5747 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5749 /* Push template declarations for global functions and types. Note
5750 that we do not try to push a global template friend declared in a
5751 template class; such a thing may well depend on the template
5752 parameters of the class. */
5753 if (new_template_p
&& !ctx
5754 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5756 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5757 if (tmpl
== error_mark_node
)
5758 return error_mark_node
;
5760 /* Hide template friend classes that haven't been declared yet. */
5761 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5763 DECL_ANTICIPATED (tmpl
) = 1;
5764 DECL_FRIEND_P (tmpl
) = 1;
5770 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5772 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5774 /* Give template template parms a DECL_CONTEXT of the template
5775 for which they are a parameter. */
5776 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5777 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5779 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5780 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5781 DECL_CONTEXT (parm
) = tmpl
;
5784 if (TREE_CODE (decl
) == TYPE_DECL
5785 && TYPE_DECL_ALIAS_P (decl
)
5786 && complex_alias_template_p (tmpl
))
5787 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5790 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5791 back to its most general template. If TMPL is a specialization,
5792 ARGS may only have the innermost set of arguments. Add the missing
5793 argument levels if necessary. */
5794 if (DECL_TEMPLATE_INFO (tmpl
))
5795 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5797 info
= build_template_info (tmpl
, args
);
5799 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5800 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5804 retrofit_lang_decl (decl
);
5805 if (DECL_LANG_SPECIFIC (decl
))
5806 DECL_TEMPLATE_INFO (decl
) = info
;
5809 if (flag_implicit_templates
5811 && TREE_PUBLIC (decl
)
5812 && VAR_OR_FUNCTION_DECL_P (decl
))
5813 /* Set DECL_COMDAT on template instantiations; if we force
5814 them to be emitted by explicit instantiation or -frepo,
5815 mark_needed will tell cgraph to do the right thing. */
5816 DECL_COMDAT (decl
) = true;
5818 return DECL_TEMPLATE_RESULT (tmpl
);
5822 push_template_decl (tree decl
)
5824 return push_template_decl_real (decl
, false);
5827 /* FN is an inheriting constructor that inherits from the constructor
5828 template INHERITED; turn FN into a constructor template with a matching
5832 add_inherited_template_parms (tree fn
, tree inherited
)
5835 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5836 inner_parms
= copy_node (inner_parms
);
5838 = tree_cons (size_int (processing_template_decl
+ 1),
5839 inner_parms
, current_template_parms
);
5840 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5841 tree args
= template_parms_to_args (parms
);
5842 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5843 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5844 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5845 DECL_ARTIFICIAL (tmpl
) = true;
5846 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5850 /* Called when a class template TYPE is redeclared with the indicated
5851 template PARMS, e.g.:
5853 template <class T> struct S;
5854 template <class T> struct S {}; */
5857 redeclare_class_template (tree type
, tree parms
, tree cons
)
5863 if (!TYPE_TEMPLATE_INFO (type
))
5865 error ("%qT is not a template type", type
);
5869 tmpl
= TYPE_TI_TEMPLATE (type
);
5870 if (!PRIMARY_TEMPLATE_P (tmpl
))
5871 /* The type is nested in some template class. Nothing to worry
5872 about here; there are no new template parameters for the nested
5878 error ("template specifiers not specified in declaration of %qD",
5883 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5884 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5886 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5888 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5889 "redeclared with %d template parameter",
5890 "redeclared with %d template parameters",
5891 TREE_VEC_LENGTH (parms
));
5892 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5893 "previous declaration %qD used %d template parameter",
5894 "previous declaration %qD used %d template parameters",
5895 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5899 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5906 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5907 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5910 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5911 if (error_operand_p (tmpl_parm
))
5914 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5915 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5916 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5918 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5920 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5921 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5922 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5923 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5924 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5925 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5926 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5927 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5928 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5930 error ("template parameter %q+#D", tmpl_parm
);
5931 error ("redeclared here as %q#D", parm
);
5935 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5937 /* We have in [temp.param]:
5939 A template-parameter may not be given default arguments
5940 by two different declarations in the same scope. */
5941 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5942 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5943 "original definition appeared here");
5947 if (parm_default
!= NULL_TREE
)
5948 /* Update the previous template parameters (which are the ones
5949 that will really count) with the new default value. */
5950 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5951 else if (tmpl_default
!= NULL_TREE
)
5952 /* Update the new parameters, too; they'll be used as the
5953 parameters for any members. */
5954 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5956 /* Give each template template parm in this redeclaration a
5957 DECL_CONTEXT of the template for which they are a parameter. */
5958 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5960 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5961 DECL_CONTEXT (parm
) = tmpl
;
5964 if (TREE_CODE (parm
) == TYPE_DECL
)
5965 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5968 // Cannot redeclare a class template with a different set of constraints.
5969 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5971 error_at (input_location
, "redeclaration %q#D with different "
5972 "constraints", tmpl
);
5973 inform (DECL_SOURCE_LOCATION (tmpl
),
5974 "original declaration appeared here");
5980 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5981 to be used when the caller has already checked
5982 (processing_template_decl
5983 && !instantiation_dependent_expression_p (expr)
5984 && potential_constant_expression (expr))
5985 and cleared processing_template_decl. */
5988 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5990 return tsubst_copy_and_build (expr
,
5993 /*in_decl=*/NULL_TREE
,
5994 /*function_p=*/false,
5995 /*integral_constant_expression_p=*/true);
5998 /* Simplify EXPR if it is a non-dependent expression. Returns the
5999 (possibly simplified) expression. */
6002 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6004 if (expr
== NULL_TREE
)
6007 /* If we're in a template, but EXPR isn't value dependent, simplify
6008 it. We're supposed to treat:
6010 template <typename T> void f(T[1 + 1]);
6011 template <typename T> void f(T[2]);
6013 as two declarations of the same function, for example. */
6014 if (processing_template_decl
6015 && is_nondependent_constant_expression (expr
))
6017 processing_template_decl_sentinel s
;
6018 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6024 instantiate_non_dependent_expr (tree expr
)
6026 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6029 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6030 an uninstantiated expression. */
6033 instantiate_non_dependent_or_null (tree expr
)
6035 if (expr
== NULL_TREE
)
6037 if (processing_template_decl
)
6039 if (!is_nondependent_constant_expression (expr
))
6043 processing_template_decl_sentinel s
;
6044 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6050 /* True iff T is a specialization of a variable template. */
6053 variable_template_specialization_p (tree t
)
6055 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6057 tree tmpl
= DECL_TI_TEMPLATE (t
);
6058 return variable_template_p (tmpl
);
6061 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6062 template declaration, or a TYPE_DECL for an alias declaration. */
6065 alias_type_or_template_p (tree t
)
6069 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6072 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6073 || DECL_ALIAS_TEMPLATE_P (t
));
6076 /* Return TRUE iff T is a specialization of an alias template. */
6079 alias_template_specialization_p (const_tree t
)
6081 /* It's an alias template specialization if it's an alias and its
6082 TYPE_NAME is a specialization of a primary template. */
6083 if (TYPE_ALIAS_P (t
))
6084 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6085 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6090 /* An alias template is complex from a SFINAE perspective if a template-id
6091 using that alias can be ill-formed when the expansion is not, as with
6092 the void_t template. We determine this by checking whether the
6093 expansion for the alias template uses all its template parameters. */
6095 struct uses_all_template_parms_data
6102 uses_all_template_parms_r (tree t
, void *data_
)
6104 struct uses_all_template_parms_data
&data
6105 = *(struct uses_all_template_parms_data
*)data_
;
6106 tree idx
= get_template_parm_index (t
);
6108 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6109 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6114 complex_alias_template_p (const_tree tmpl
)
6116 struct uses_all_template_parms_data data
;
6117 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6118 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6119 data
.level
= TMPL_PARMS_DEPTH (parms
);
6120 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6121 data
.seen
= XALLOCAVEC (bool, len
);
6122 for (int i
= 0; i
< len
; ++i
)
6123 data
.seen
[i
] = false;
6125 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6126 for (int i
= 0; i
< len
; ++i
)
6132 /* Return TRUE iff T is a specialization of a complex alias template with
6133 dependent template-arguments. */
6136 dependent_alias_template_spec_p (const_tree t
)
6138 if (!alias_template_specialization_p (t
))
6141 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6142 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6145 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6146 if (!any_dependent_template_arguments_p (args
))
6152 /* Return the number of innermost template parameters in TMPL. */
6155 num_innermost_template_parms (tree tmpl
)
6157 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6158 return TREE_VEC_LENGTH (parms
);
6161 /* Return either TMPL or another template that it is equivalent to under DR
6162 1286: An alias that just changes the name of a template is equivalent to
6163 the other template. */
6166 get_underlying_template (tree tmpl
)
6168 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6169 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6171 /* Determine if the alias is equivalent to an underlying template. */
6172 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6173 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6177 tree underlying
= TI_TEMPLATE (tinfo
);
6178 if (!PRIMARY_TEMPLATE_P (underlying
)
6179 || (num_innermost_template_parms (tmpl
)
6180 != num_innermost_template_parms (underlying
)))
6183 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6184 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6185 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6188 /* Alias is equivalent. Strip it and repeat. */
6195 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6196 must be a reference-to-function or a pointer-to-function type, as specified
6197 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6198 and check that the resulting function has external linkage. */
6201 convert_nontype_argument_function (tree type
, tree expr
,
6202 tsubst_flags_t complain
)
6206 linkage_kind linkage
;
6208 fn
= instantiate_type (type
, fns
, tf_none
);
6209 if (fn
== error_mark_node
)
6210 return error_mark_node
;
6212 if (value_dependent_expression_p (fn
))
6215 fn_no_ptr
= strip_fnptr_conv (fn
);
6216 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6217 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6218 if (BASELINK_P (fn_no_ptr
))
6219 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6221 /* [temp.arg.nontype]/1
6223 A template-argument for a non-type, non-template template-parameter
6226 -- the address of an object or function with external [C++11: or
6227 internal] linkage. */
6229 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6230 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6232 if (complain
& tf_error
)
6234 error ("%qE is not a valid template argument for type %qT",
6236 if (TYPE_PTR_P (type
))
6237 inform (input_location
, "it must be the address of a function "
6238 "with external linkage");
6240 inform (input_location
, "it must be the name of a function with "
6241 "external linkage");
6246 linkage
= decl_linkage (fn_no_ptr
);
6247 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6249 if (complain
& tf_error
)
6251 if (cxx_dialect
>= cxx11
)
6252 error ("%qE is not a valid template argument for type %qT "
6253 "because %qD has no linkage",
6254 expr
, type
, fn_no_ptr
);
6256 error ("%qE is not a valid template argument for type %qT "
6257 "because %qD does not have external linkage",
6258 expr
, type
, fn_no_ptr
);
6264 if (TYPE_REF_P (type
))
6266 if (REFERENCE_REF_P (fn
))
6267 fn
= TREE_OPERAND (fn
, 0);
6269 fn
= build_address (fn
);
6271 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6272 fn
= build_nop (type
, fn
);
6277 /* Subroutine of convert_nontype_argument.
6278 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6279 Emit an error otherwise. */
6282 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6283 tsubst_flags_t complain
)
6285 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6286 tree orig_expr
= expr
;
6288 if (null_ptr_cst_p (expr
))
6290 if (TREE_CODE (expr
) == PTRMEM_CST
6291 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6292 PTRMEM_CST_CLASS (expr
)))
6294 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6296 if (processing_template_decl
6297 && TREE_CODE (expr
) == ADDR_EXPR
6298 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6300 if (complain
& tf_error
)
6302 error_at (loc
, "%qE is not a valid template argument for type %qT",
6304 if (TREE_CODE (expr
) != PTRMEM_CST
)
6305 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6307 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6312 /* Returns TRUE iff the address of OP is value-dependent.
6314 14.6.2.4 [temp.dep.temp]:
6315 A non-integral non-type template-argument is dependent if its type is
6316 dependent or it has either of the following forms
6319 and contains a nested-name-specifier which specifies a class-name that
6320 names a dependent type.
6322 We generalize this to just say that the address of a member of a
6323 dependent class is value-dependent; the above doesn't cover the
6324 address of a static data member named with an unqualified-id. */
6327 has_value_dependent_address (tree op
)
6329 /* We could use get_inner_reference here, but there's no need;
6330 this is only relevant for template non-type arguments, which
6331 can only be expressed as &id-expression. */
6334 tree ctx
= CP_DECL_CONTEXT (op
);
6335 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6342 /* The next set of functions are used for providing helpful explanatory
6343 diagnostics for failed overload resolution. Their messages should be
6344 indented by two spaces for consistency with the messages in
6348 unify_success (bool /*explain_p*/)
6353 /* Other failure functions should call this one, to provide a single function
6354 for setting a breakpoint on. */
6357 unify_invalid (bool /*explain_p*/)
6363 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6366 inform (input_location
,
6367 " couldn't deduce template parameter %qD", parm
);
6368 return unify_invalid (explain_p
);
6372 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6375 inform (input_location
,
6376 " types %qT and %qT have incompatible cv-qualifiers",
6378 return unify_invalid (explain_p
);
6382 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6385 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6386 return unify_invalid (explain_p
);
6390 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6393 inform (input_location
,
6394 " template parameter %qD is not a parameter pack, but "
6397 return unify_invalid (explain_p
);
6401 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6404 inform (input_location
,
6405 " template argument %qE does not match "
6406 "pointer-to-member constant %qE",
6408 return unify_invalid (explain_p
);
6412 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6415 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6416 return unify_invalid (explain_p
);
6420 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6423 inform (input_location
,
6424 " inconsistent parameter pack deduction with %qT and %qT",
6426 return unify_invalid (explain_p
);
6430 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6435 inform (input_location
,
6436 " deduced conflicting types for parameter %qT (%qT and %qT)",
6437 parm
, first
, second
);
6439 inform (input_location
,
6440 " deduced conflicting values for non-type parameter "
6441 "%qE (%qE and %qE)", parm
, first
, second
);
6443 return unify_invalid (explain_p
);
6447 unify_vla_arg (bool explain_p
, tree arg
)
6450 inform (input_location
,
6451 " variable-sized array type %qT is not "
6452 "a valid template argument",
6454 return unify_invalid (explain_p
);
6458 unify_method_type_error (bool explain_p
, tree arg
)
6461 inform (input_location
,
6462 " member function type %qT is not a valid template argument",
6464 return unify_invalid (explain_p
);
6468 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6473 inform_n (input_location
, wanted
,
6474 " candidate expects at least %d argument, %d provided",
6475 " candidate expects at least %d arguments, %d provided",
6478 inform_n (input_location
, wanted
,
6479 " candidate expects %d argument, %d provided",
6480 " candidate expects %d arguments, %d provided",
6483 return unify_invalid (explain_p
);
6487 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6489 return unify_arity (explain_p
, have
, wanted
);
6493 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6494 bool least_p
= false)
6496 return unify_arity (explain_p
, have
, wanted
, least_p
);
6500 unify_arg_conversion (bool explain_p
, tree to_type
,
6501 tree from_type
, tree arg
)
6504 inform (cp_expr_loc_or_loc (arg
, input_location
),
6505 " cannot convert %qE (type %qT) to type %qT",
6506 arg
, from_type
, to_type
);
6507 return unify_invalid (explain_p
);
6511 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6512 tree parm
, tree arg
)
6517 case tbr_ambiguous_baseclass
:
6518 inform (input_location
, " %qT is an ambiguous base class of %qT",
6522 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6525 return unify_invalid (explain_p
);
6529 unify_inconsistent_template_template_parameters (bool explain_p
)
6532 inform (input_location
,
6533 " template parameters of a template template argument are "
6534 "inconsistent with other deduced template arguments");
6535 return unify_invalid (explain_p
);
6539 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6542 inform (input_location
,
6543 " can't deduce a template for %qT from non-template type %qT",
6545 return unify_invalid (explain_p
);
6549 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6552 inform (input_location
,
6553 " template argument %qE does not match %qE", arg
, parm
);
6554 return unify_invalid (explain_p
);
6557 /* True if T is a C++20 template parameter object to store the argument for a
6558 template parameter of class type. */
6561 template_parm_object_p (const_tree t
)
6563 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6564 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6567 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6568 argument for TYPE, points to an unsuitable object. */
6571 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6573 switch (TREE_CODE (expr
))
6576 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6580 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6585 unsigned i
; tree elt
;
6586 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6587 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6594 tree decl
= TREE_OPERAND (expr
, 0);
6598 if (complain
& tf_error
)
6599 error ("%qE is not a valid template argument of type %qT "
6600 "because %qE is not a variable", expr
, type
, decl
);
6603 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6605 if (complain
& tf_error
)
6606 error ("%qE is not a valid template argument of type %qT "
6607 "in C++98 because %qD does not have external linkage",
6611 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6612 && decl_linkage (decl
) == lk_none
)
6614 if (complain
& tf_error
)
6615 error ("%qE is not a valid template argument of type %qT "
6616 "because %qD has no linkage", expr
, type
, decl
);
6619 /* C++17: For a non-type template-parameter of reference or pointer
6620 type, the value of the constant expression shall not refer to (or
6621 for a pointer type, shall not be the address of):
6622 * a subobject (4.5),
6623 * a temporary object (15.2),
6624 * a string literal (5.13.5),
6625 * the result of a typeid expression (8.2.8), or
6626 * a predefined __func__ variable (11.4.1). */
6627 else if (DECL_ARTIFICIAL (decl
))
6629 if (complain
& tf_error
)
6630 error ("the address of %qD is not a valid template argument",
6634 else if (!same_type_ignoring_top_level_qualifiers_p
6635 (strip_array_types (TREE_TYPE (type
)),
6636 strip_array_types (TREE_TYPE (decl
))))
6638 if (complain
& tf_error
)
6639 error ("the address of the %qT subobject of %qD is not a "
6640 "valid template argument", TREE_TYPE (type
), decl
);
6643 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6645 if (complain
& tf_error
)
6646 error ("the address of %qD is not a valid template argument "
6647 "because it does not have static storage duration",
6655 if (!INDIRECT_TYPE_P (type
))
6656 /* We're only concerned about pointers and references here. */;
6657 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6658 /* Null pointer values are OK in C++11. */;
6663 if (complain
& tf_error
)
6664 error ("%qD is not a valid template argument "
6665 "because %qD is a variable, not the address of "
6666 "a variable", expr
, expr
);
6671 if (complain
& tf_error
)
6672 error ("%qE is not a valid template argument for %qT "
6673 "because it is not the address of a variable",
6683 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6684 template argument EXPR. */
6687 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6689 if (TREE_CODE (expr
) == TARGET_EXPR
)
6690 expr
= TARGET_EXPR_INITIAL (expr
);
6692 if (!TREE_CONSTANT (expr
))
6694 if ((complain
& tf_error
)
6695 && require_rvalue_constant_expression (expr
))
6696 cxx_constant_value (expr
);
6697 return error_mark_node
;
6699 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6700 return error_mark_node
;
6702 tree name
= mangle_template_parm_object (expr
);
6703 tree decl
= get_global_binding (name
);
6707 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6708 decl
= create_temporary_var (type
);
6709 TREE_STATIC (decl
) = true;
6710 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6711 TREE_READONLY (decl
) = true;
6712 DECL_NAME (decl
) = name
;
6713 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6714 DECL_CONTEXT (decl
) = global_namespace
;
6715 comdat_linkage (decl
);
6716 pushdecl_top_level_and_finish (decl
, expr
);
6720 /* Attempt to convert the non-type template parameter EXPR to the
6721 indicated TYPE. If the conversion is successful, return the
6722 converted value. If the conversion is unsuccessful, return
6723 NULL_TREE if we issued an error message, or error_mark_node if we
6724 did not. We issue error messages for out-and-out bad template
6725 parameters, but not simply because the conversion failed, since we
6726 might be just trying to do argument deduction. Both TYPE and EXPR
6727 must be non-dependent.
6729 The conversion follows the special rules described in
6730 [temp.arg.nontype], and it is much more strict than an implicit
6733 This function is called twice for each template argument (see
6734 lookup_template_class for a more accurate description of this
6735 problem). This means that we need to handle expressions which
6736 are not valid in a C++ source, but can be created from the
6737 first call (for instance, casts to perform conversions). These
6738 hacks can go away after we fix the double coercion problem. */
6741 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6744 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6746 /* Detect immediately string literals as invalid non-type argument.
6747 This special-case is not needed for correctness (we would easily
6748 catch this later), but only to provide better diagnostic for this
6749 common user mistake. As suggested by DR 100, we do not mention
6750 linkage issues in the diagnostic as this is not the point. */
6751 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6753 if (complain
& tf_error
)
6754 error ("%qE is not a valid template argument for type %qT "
6755 "because string literals can never be used in this context",
6760 /* Add the ADDR_EXPR now for the benefit of
6761 value_dependent_expression_p. */
6762 if (TYPE_PTROBV_P (type
)
6763 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6765 expr
= decay_conversion (expr
, complain
);
6766 if (expr
== error_mark_node
)
6767 return error_mark_node
;
6770 /* If we are in a template, EXPR may be non-dependent, but still
6771 have a syntactic, rather than semantic, form. For example, EXPR
6772 might be a SCOPE_REF, rather than the VAR_DECL to which the
6773 SCOPE_REF refers. Preserving the qualifying scope is necessary
6774 so that access checking can be performed when the template is
6775 instantiated -- but here we need the resolved form so that we can
6776 convert the argument. */
6777 bool non_dep
= false;
6778 if (TYPE_REF_OBJ_P (type
)
6779 && has_value_dependent_address (expr
))
6780 /* If we want the address and it's value-dependent, don't fold. */;
6781 else if (processing_template_decl
6782 && is_nondependent_constant_expression (expr
))
6784 if (error_operand_p (expr
))
6785 return error_mark_node
;
6786 expr_type
= TREE_TYPE (expr
);
6788 /* If the argument is non-dependent, perform any conversions in
6789 non-dependent context as well. */
6790 processing_template_decl_sentinel
s (non_dep
);
6792 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6794 if (value_dependent_expression_p (expr
))
6795 expr
= canonicalize_expr_argument (expr
, complain
);
6797 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6798 to a non-type argument of "nullptr". */
6799 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6800 expr
= fold_simple (convert (type
, expr
));
6802 /* In C++11, integral or enumeration non-type template arguments can be
6803 arbitrary constant expressions. Pointer and pointer to
6804 member arguments can be general constant expressions that evaluate
6805 to a null value, but otherwise still need to be of a specific form. */
6806 if (cxx_dialect
>= cxx11
)
6808 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
6809 /* A PTRMEM_CST is already constant, and a valid template
6810 argument for a parameter of pointer to member type, we just want
6811 to leave it in that form rather than lower it to a
6813 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6814 || cxx_dialect
>= cxx17
)
6816 /* C++17: A template-argument for a non-type template-parameter shall
6817 be a converted constant expression (8.20) of the type of the
6818 template-parameter. */
6819 expr
= build_converted_constant_expr (type
, expr
, complain
);
6820 if (expr
== error_mark_node
)
6821 /* Make sure we return NULL_TREE only if we have really issued
6822 an error, as described above. */
6823 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
6824 expr
= maybe_constant_value (expr
);
6825 expr
= convert_from_reference (expr
);
6827 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6829 tree folded
= maybe_constant_value (expr
);
6830 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6831 : null_member_pointer_value_p (folded
))
6836 if (TYPE_REF_P (type
))
6837 expr
= mark_lvalue_use (expr
);
6839 expr
= mark_rvalue_use (expr
);
6841 /* HACK: Due to double coercion, we can get a
6842 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6843 which is the tree that we built on the first call (see
6844 below when coercing to reference to object or to reference to
6845 function). We just strip everything and get to the arg.
6846 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6848 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6850 tree probe_type
, probe
= expr
;
6851 if (REFERENCE_REF_P (probe
))
6852 probe
= TREE_OPERAND (probe
, 0);
6853 probe_type
= TREE_TYPE (probe
);
6854 if (TREE_CODE (probe
) == NOP_EXPR
)
6856 /* ??? Maybe we could use convert_from_reference here, but we
6857 would need to relax its constraints because the NOP_EXPR
6858 could actually change the type to something more cv-qualified,
6859 and this is not folded by convert_from_reference. */
6860 tree addr
= TREE_OPERAND (probe
, 0);
6861 if (TYPE_REF_P (probe_type
)
6862 && TREE_CODE (addr
) == ADDR_EXPR
6863 && TYPE_PTR_P (TREE_TYPE (addr
))
6864 && (same_type_ignoring_top_level_qualifiers_p
6865 (TREE_TYPE (probe_type
),
6866 TREE_TYPE (TREE_TYPE (addr
)))))
6868 expr
= TREE_OPERAND (addr
, 0);
6869 expr_type
= TREE_TYPE (probe_type
);
6874 /* [temp.arg.nontype]/5, bullet 1
6876 For a non-type template-parameter of integral or enumeration type,
6877 integral promotions (_conv.prom_) and integral conversions
6878 (_conv.integral_) are applied. */
6879 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6881 if (cxx_dialect
< cxx11
)
6883 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6884 t
= maybe_constant_value (t
);
6885 if (t
!= error_mark_node
)
6889 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6890 return error_mark_node
;
6892 /* Notice that there are constant expressions like '4 % 0' which
6893 do not fold into integer constants. */
6894 if (TREE_CODE (expr
) != INTEGER_CST
6895 && !value_dependent_expression_p (expr
))
6897 if (complain
& tf_error
)
6899 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6900 if (!require_potential_constant_expression (expr
))
6901 expr
= error_mark_node
;
6903 expr
= cxx_constant_value (expr
);
6904 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6905 inform (loc
, "in template argument for type %qT", type
);
6906 if (expr
== error_mark_node
)
6908 /* else cxx_constant_value complained but gave us
6909 a real constant, so go ahead. */
6910 if (TREE_CODE (expr
) != INTEGER_CST
)
6912 /* Some assemble time constant expressions like
6913 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6914 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6915 as we can emit them into .rodata initializers of
6916 variables, yet they can't fold into an INTEGER_CST at
6917 compile time. Refuse them here. */
6918 gcc_checking_assert (reduced_constant_expression_p (expr
));
6919 error_at (loc
, "template argument %qE for type %qT not "
6920 "a constant integer", expr
, type
);
6928 /* Avoid typedef problems. */
6929 if (TREE_TYPE (expr
) != type
)
6930 expr
= fold_convert (type
, expr
);
6932 /* [temp.arg.nontype]/5, bullet 2
6934 For a non-type template-parameter of type pointer to object,
6935 qualification conversions (_conv.qual_) and the array-to-pointer
6936 conversion (_conv.array_) are applied. */
6937 else if (TYPE_PTROBV_P (type
))
6939 tree decayed
= expr
;
6941 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6942 decay_conversion or an explicit cast. If it's a problematic cast,
6943 we'll complain about it below. */
6944 if (TREE_CODE (expr
) == NOP_EXPR
)
6948 if (TREE_CODE (probe
) == ADDR_EXPR
6949 && TYPE_PTR_P (TREE_TYPE (probe
)))
6952 expr_type
= TREE_TYPE (expr
);
6956 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6958 A template-argument for a non-type, non-template template-parameter
6959 shall be one of: [...]
6961 -- the name of a non-type template-parameter;
6962 -- the address of an object or function with external linkage, [...]
6963 expressed as "& id-expression" where the & is optional if the name
6964 refers to a function or array, or if the corresponding
6965 template-parameter is a reference.
6967 Here, we do not care about functions, as they are invalid anyway
6968 for a parameter of type pointer-to-object. */
6970 if (value_dependent_expression_p (expr
))
6971 /* Non-type template parameters are OK. */
6973 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6974 /* Null pointer values are OK in C++11. */;
6975 else if (TREE_CODE (expr
) != ADDR_EXPR
6976 && !INDIRECT_TYPE_P (expr_type
))
6977 /* Other values, like integer constants, might be valid
6978 non-type arguments of some other type. */
6979 return error_mark_node
;
6980 else if (invalid_tparm_referent_p (type
, expr
, complain
))
6985 expr
= perform_qualification_conversions (type
, expr
);
6986 if (expr
== error_mark_node
)
6987 return error_mark_node
;
6989 /* [temp.arg.nontype]/5, bullet 3
6991 For a non-type template-parameter of type reference to object, no
6992 conversions apply. The type referred to by the reference may be more
6993 cv-qualified than the (otherwise identical) type of the
6994 template-argument. The template-parameter is bound directly to the
6995 template-argument, which must be an lvalue. */
6996 else if (TYPE_REF_OBJ_P (type
))
6998 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7000 return error_mark_node
;
7002 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7004 if (complain
& tf_error
)
7005 error ("%qE is not a valid template argument for type %qT "
7006 "because of conflicts in cv-qualification", expr
, type
);
7010 if (!lvalue_p (expr
))
7012 if (complain
& tf_error
)
7013 error ("%qE is not a valid template argument for type %qT "
7014 "because it is not an lvalue", expr
, type
);
7018 /* [temp.arg.nontype]/1
7020 A template-argument for a non-type, non-template template-parameter
7021 shall be one of: [...]
7023 -- the address of an object or function with external linkage. */
7024 if (INDIRECT_REF_P (expr
)
7025 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7027 expr
= TREE_OPERAND (expr
, 0);
7030 if (complain
& tf_error
)
7031 error ("%q#D is not a valid template argument for type %qT "
7032 "because a reference variable does not have a constant "
7033 "address", expr
, type
);
7038 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7039 && value_dependent_expression_p (expr
))
7040 /* OK, dependent reference. We don't want to ask whether a DECL is
7041 itself value-dependent, since what we want here is its address. */;
7044 expr
= build_address (expr
);
7046 if (invalid_tparm_referent_p (type
, expr
, complain
))
7050 if (!same_type_p (type
, TREE_TYPE (expr
)))
7051 expr
= build_nop (type
, expr
);
7053 /* [temp.arg.nontype]/5, bullet 4
7055 For a non-type template-parameter of type pointer to function, only
7056 the function-to-pointer conversion (_conv.func_) is applied. If the
7057 template-argument represents a set of overloaded functions (or a
7058 pointer to such), the matching function is selected from the set
7060 else if (TYPE_PTRFN_P (type
))
7062 /* If the argument is a template-id, we might not have enough
7063 context information to decay the pointer. */
7064 if (!type_unknown_p (expr_type
))
7066 expr
= decay_conversion (expr
, complain
);
7067 if (expr
== error_mark_node
)
7068 return error_mark_node
;
7071 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7072 /* Null pointer values are OK in C++11. */
7073 return perform_qualification_conversions (type
, expr
);
7075 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7076 if (!expr
|| expr
== error_mark_node
)
7079 /* [temp.arg.nontype]/5, bullet 5
7081 For a non-type template-parameter of type reference to function, no
7082 conversions apply. If the template-argument represents a set of
7083 overloaded functions, the matching function is selected from the set
7085 else if (TYPE_REFFN_P (type
))
7087 if (TREE_CODE (expr
) == ADDR_EXPR
)
7089 if (complain
& tf_error
)
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is a pointer", expr
, type
);
7093 inform (input_location
, "try using %qE instead",
7094 TREE_OPERAND (expr
, 0));
7099 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7100 if (!expr
|| expr
== error_mark_node
)
7103 /* [temp.arg.nontype]/5, bullet 6
7105 For a non-type template-parameter of type pointer to member function,
7106 no conversions apply. If the template-argument represents a set of
7107 overloaded member functions, the matching member function is selected
7108 from the set (_over.over_). */
7109 else if (TYPE_PTRMEMFUNC_P (type
))
7111 expr
= instantiate_type (type
, expr
, tf_none
);
7112 if (expr
== error_mark_node
)
7113 return error_mark_node
;
7115 /* [temp.arg.nontype] bullet 1 says the pointer to member
7116 expression must be a pointer-to-member constant. */
7117 if (!value_dependent_expression_p (expr
)
7118 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7121 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7122 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7123 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7124 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7126 /* [temp.arg.nontype]/5, bullet 7
7128 For a non-type template-parameter of type pointer to data member,
7129 qualification conversions (_conv.qual_) are applied. */
7130 else if (TYPE_PTRDATAMEM_P (type
))
7132 /* [temp.arg.nontype] bullet 1 says the pointer to member
7133 expression must be a pointer-to-member constant. */
7134 if (!value_dependent_expression_p (expr
)
7135 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7138 expr
= perform_qualification_conversions (type
, expr
);
7139 if (expr
== error_mark_node
)
7142 else if (NULLPTR_TYPE_P (type
))
7144 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7146 if (complain
& tf_error
)
7147 error ("%qE is not a valid template argument for type %qT "
7148 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7153 else if (CLASS_TYPE_P (type
))
7155 /* Replace the argument with a reference to the corresponding template
7156 parameter object. */
7157 if (!value_dependent_expression_p (expr
))
7158 expr
= get_template_parm_object (expr
, complain
);
7159 if (expr
== error_mark_node
)
7162 /* A template non-type parameter must be one of the above. */
7166 /* Sanity check: did we actually convert the argument to the
7168 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7169 (type
, TREE_TYPE (expr
)));
7170 return convert_from_reference (expr
);
7173 /* Subroutine of coerce_template_template_parms, which returns 1 if
7174 PARM_PARM and ARG_PARM match using the rule for the template
7175 parameters of template template parameters. Both PARM and ARG are
7176 template parameters; the rest of the arguments are the same as for
7177 coerce_template_template_parms.
7180 coerce_template_template_parm (tree parm
,
7182 tsubst_flags_t complain
,
7186 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7187 || parm
== NULL_TREE
|| error_operand_p (parm
))
7190 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7193 switch (TREE_CODE (parm
))
7196 /* We encounter instantiations of templates like
7197 template <template <template <class> class> class TT>
7200 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7201 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7203 if (!coerce_template_template_parms
7204 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7210 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7211 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7212 /* Argument is a parameter pack but parameter is not. */
7217 /* The tsubst call is used to handle cases such as
7219 template <int> class C {};
7220 template <class T, template <T> class TT> class D {};
7223 i.e. the parameter list of TT depends on earlier parameters. */
7224 if (!uses_template_parms (TREE_TYPE (arg
)))
7226 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7227 if (!uses_template_parms (t
)
7228 && !same_type_p (t
, TREE_TYPE (arg
)))
7232 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7233 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7234 /* Argument is a parameter pack but parameter is not. */
7246 /* Coerce template argument list ARGLIST for use with template
7247 template-parameter TEMPL. */
7250 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7251 tsubst_flags_t complain
)
7253 /* Consider an example where a template template parameter declared as
7255 template <class T, class U = std::allocator<T> > class TT
7257 The template parameter level of T and U are one level larger than
7258 of TT. To proper process the default argument of U, say when an
7259 instantiation `TT<int>' is seen, we need to build the full
7260 arguments containing {int} as the innermost level. Outer levels,
7261 available when not appearing as default template argument, can be
7262 obtained from the arguments of the enclosing template.
7264 Suppose that TT is later substituted with std::vector. The above
7265 instantiation is `TT<int, std::allocator<T> >' with TT at
7266 level 1, and T at level 2, while the template arguments at level 1
7267 becomes {std::vector} and the inner level 2 is {int}. */
7269 tree outer
= DECL_CONTEXT (templ
);
7272 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7273 /* We want arguments for the partial specialization, not arguments for
7274 the primary template. */
7275 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7277 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7279 else if (current_template_parms
)
7281 /* This is an argument of the current template, so we haven't set
7282 DECL_CONTEXT yet. */
7283 tree relevant_template_parms
;
7285 /* Parameter levels that are greater than the level of the given
7286 template template parm are irrelevant. */
7287 relevant_template_parms
= current_template_parms
;
7288 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7289 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7290 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7292 outer
= template_parms_to_args (relevant_template_parms
);
7296 arglist
= add_to_template_args (outer
, arglist
);
7298 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7299 return coerce_template_parms (parmlist
, arglist
, templ
,
7301 /*require_all_args=*/true,
7302 /*use_default_args=*/true);
7305 /* A cache of template template parameters with match-all default
7307 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7309 store_defaulted_ttp (tree v
, tree t
)
7311 if (!defaulted_ttp_cache
)
7312 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7313 defaulted_ttp_cache
->put (v
, t
);
7316 lookup_defaulted_ttp (tree v
)
7318 if (defaulted_ttp_cache
)
7319 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7324 /* T is a bound template template-parameter. Copy its arguments into default
7325 arguments of the template template-parameter's template parameters. */
7328 add_defaults_to_ttp (tree otmpl
)
7330 if (tree c
= lookup_defaulted_ttp (otmpl
))
7333 tree ntmpl
= copy_node (otmpl
);
7335 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7336 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7337 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7338 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7339 TYPE_NAME (ntype
) = ntmpl
;
7340 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7342 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7343 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7344 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7345 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7347 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7348 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7349 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7350 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7351 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7353 tree o
= TREE_VEC_ELT (vec
, i
);
7354 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7356 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7357 TREE_PURPOSE (n
) = any_targ_node
;
7361 store_defaulted_ttp (otmpl
, ntmpl
);
7365 /* ARG is a bound potential template template-argument, and PARGS is a list
7366 of arguments for the corresponding template template-parameter. Adjust
7367 PARGS as appropriate for application to ARG's template, and if ARG is a
7368 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7369 arguments to the template template parameter. */
7372 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7374 ++processing_template_decl
;
7375 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7376 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7378 /* When comparing two template template-parameters in partial ordering,
7379 rewrite the one currently being used as an argument to have default
7380 arguments for all parameters. */
7381 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7382 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7383 if (pargs
!= error_mark_node
)
7384 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7385 TYPE_TI_ARGS (arg
));
7390 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7391 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7392 /*require_all*/true,
7393 /*use_default*/true);
7395 --processing_template_decl
;
7399 /* Subroutine of unify for the case when PARM is a
7400 BOUND_TEMPLATE_TEMPLATE_PARM. */
7403 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7406 tree parmvec
= TYPE_TI_ARGS (parm
);
7407 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7409 /* The template template parm might be variadic and the argument
7410 not, so flatten both argument lists. */
7411 parmvec
= expand_template_argument_pack (parmvec
);
7412 argvec
= expand_template_argument_pack (argvec
);
7416 /* In keeping with P0522R0, adjust P's template arguments
7417 to apply to A's template; then flatten it again. */
7418 tree nparmvec
= parmvec
;
7419 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7420 nparmvec
= expand_template_argument_pack (nparmvec
);
7422 if (unify (tparms
, targs
, nparmvec
, argvec
,
7423 UNIFY_ALLOW_NONE
, explain_p
))
7426 /* If the P0522 adjustment eliminated a pack expansion, deduce
7429 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7430 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7431 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7436 /* Deduce arguments T, i from TT<T> or TT<i>.
7437 We check each element of PARMVEC and ARGVEC individually
7438 rather than the whole TREE_VEC since they can have
7439 different number of elements, which is allowed under N2555. */
7441 int len
= TREE_VEC_LENGTH (parmvec
);
7443 /* Check if the parameters end in a pack, making them
7445 int parm_variadic_p
= 0;
7447 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7448 parm_variadic_p
= 1;
7450 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7451 /* If the template argument list of P contains a pack
7452 expansion that is not the last template argument, the
7453 entire template argument list is a non-deduced
7455 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7456 return unify_success (explain_p
);
7458 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7459 return unify_too_few_arguments (explain_p
,
7460 TREE_VEC_LENGTH (argvec
), len
);
7462 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7463 if (unify (tparms
, targs
,
7464 TREE_VEC_ELT (parmvec
, i
),
7465 TREE_VEC_ELT (argvec
, i
),
7466 UNIFY_ALLOW_NONE
, explain_p
))
7470 && unify_pack_expansion (tparms
, targs
,
7473 /*subr=*/true, explain_p
))
7480 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7481 template template parameters. Both PARM_PARMS and ARG_PARMS are
7482 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7485 Consider the example:
7486 template <class T> class A;
7487 template<template <class U> class TT> class B;
7489 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7490 the parameters to A, and OUTER_ARGS contains A. */
7493 coerce_template_template_parms (tree parm_parms
,
7495 tsubst_flags_t complain
,
7499 int nparms
, nargs
, i
;
7503 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7504 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7506 nparms
= TREE_VEC_LENGTH (parm_parms
);
7507 nargs
= TREE_VEC_LENGTH (arg_parms
);
7511 /* P0522R0: A template template-parameter P is at least as specialized as
7512 a template template-argument A if, given the following rewrite to two
7513 function templates, the function template corresponding to P is at
7514 least as specialized as the function template corresponding to A
7515 according to the partial ordering rules for function templates
7516 ([temp.func.order]). Given an invented class template X with the
7517 template parameter list of A (including default arguments):
7519 * Each of the two function templates has the same template parameters,
7520 respectively, as P or A.
7522 * Each function template has a single function parameter whose type is
7523 a specialization of X with template arguments corresponding to the
7524 template parameters from the respective function template where, for
7525 each template parameter PP in the template parameter list of the
7526 function template, a corresponding template argument AA is formed. If
7527 PP declares a parameter pack, then AA is the pack expansion
7528 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7530 If the rewrite produces an invalid type, then P is not at least as
7531 specialized as A. */
7533 /* So coerce P's args to apply to A's parms, and then deduce between A's
7534 args and the converted args. If that succeeds, A is at least as
7535 specialized as P, so they match.*/
7536 tree pargs
= template_parms_level_to_args (parm_parms
);
7537 ++processing_template_decl
;
7538 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7539 /*require_all*/true, /*use_default*/true);
7540 --processing_template_decl
;
7541 if (pargs
!= error_mark_node
)
7543 tree targs
= make_tree_vec (nargs
);
7544 tree aargs
= template_parms_level_to_args (arg_parms
);
7545 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7551 /* Determine whether we have a parameter pack at the end of the
7552 template template parameter's template parameter list. */
7553 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7555 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7557 if (error_operand_p (parm
))
7560 switch (TREE_CODE (parm
))
7564 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7569 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7579 && !(variadic_p
&& nargs
>= nparms
- 1))
7582 /* Check all of the template parameters except the parameter pack at
7583 the end (if any). */
7584 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7586 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7587 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7590 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7591 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7593 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7601 /* Check each of the template parameters in the template
7602 argument against the template parameter pack at the end of
7603 the template template parameter. */
7604 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7607 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7609 for (; i
< nargs
; ++i
)
7611 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7614 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7616 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7625 /* Verifies that the deduced template arguments (in TARGS) for the
7626 template template parameters (in TPARMS) represent valid bindings,
7627 by comparing the template parameter list of each template argument
7628 to the template parameter list of its corresponding template
7629 template parameter, in accordance with DR150. This
7630 routine can only be called after all template arguments have been
7631 deduced. It will return TRUE if all of the template template
7632 parameter bindings are okay, FALSE otherwise. */
7634 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7636 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7639 /* We're dealing with template parms in this process. */
7640 ++processing_template_decl
;
7642 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7644 for (i
= 0; i
< ntparms
; ++i
)
7646 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7647 tree targ
= TREE_VEC_ELT (targs
, i
);
7649 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7651 tree packed_args
= NULL_TREE
;
7654 if (ARGUMENT_PACK_P (targ
))
7656 /* Look inside the argument pack. */
7657 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7658 len
= TREE_VEC_LENGTH (packed_args
);
7661 for (idx
= 0; idx
< len
; ++idx
)
7663 tree targ_parms
= NULL_TREE
;
7666 /* Extract the next argument from the argument
7668 targ
= TREE_VEC_ELT (packed_args
, idx
);
7670 if (PACK_EXPANSION_P (targ
))
7671 /* Look at the pattern of the pack expansion. */
7672 targ
= PACK_EXPANSION_PATTERN (targ
);
7674 /* Extract the template parameters from the template
7676 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7677 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7678 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7679 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7681 /* Verify that we can coerce the template template
7682 parameters from the template argument to the template
7683 parameter. This requires an exact match. */
7685 && !coerce_template_template_parms
7686 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7701 --processing_template_decl
;
7705 /* Since type attributes aren't mangled, we need to strip them from
7706 template type arguments. */
7709 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7711 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7713 bool removed_attributes
= false;
7714 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7715 if (removed_attributes
7716 && (complain
& tf_warning
))
7717 warning (OPT_Wignored_attributes
,
7718 "ignoring attributes on template argument %qT", arg
);
7722 /* And from inside dependent non-type arguments like sizeof(Type). */
7725 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7727 if (!arg
|| arg
== error_mark_node
)
7729 bool removed_attributes
= false;
7730 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7731 if (removed_attributes
7732 && (complain
& tf_warning
))
7733 warning (OPT_Wignored_attributes
,
7734 "ignoring attributes in template argument %qE", arg
);
7738 // A template declaration can be substituted for a constrained
7739 // template template parameter only when the argument is more
7740 // constrained than the parameter.
7742 is_compatible_template_arg (tree parm
, tree arg
)
7744 tree parm_cons
= get_constraints (parm
);
7746 /* For now, allow constrained template template arguments
7747 and unconstrained template template parameters. */
7748 if (parm_cons
== NULL_TREE
)
7751 tree arg_cons
= get_constraints (arg
);
7753 // If the template parameter is constrained, we need to rewrite its
7754 // constraints in terms of the ARG's template parameters. This ensures
7755 // that all of the template parameter types will have the same depth.
7757 // Note that this is only valid when coerce_template_template_parm is
7758 // true for the innermost template parameters of PARM and ARG. In other
7759 // words, because coercion is successful, this conversion will be valid.
7762 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7763 parm_cons
= tsubst_constraint_info (parm_cons
,
7764 INNERMOST_TEMPLATE_ARGS (args
),
7765 tf_none
, NULL_TREE
);
7766 if (parm_cons
== error_mark_node
)
7770 return subsumes (parm_cons
, arg_cons
);
7773 // Convert a placeholder argument into a binding to the original
7774 // parameter. The original parameter is saved as the TREE_TYPE of
7777 convert_wildcard_argument (tree parm
, tree arg
)
7779 TREE_TYPE (arg
) = parm
;
7783 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7784 because one of them is dependent. But we need to represent the
7785 conversion for the benefit of cp_tree_equal. */
7788 maybe_convert_nontype_argument (tree type
, tree arg
)
7790 /* Auto parms get no conversion. */
7791 if (type_uses_auto (type
))
7793 /* We don't need or want to add this conversion now if we're going to use the
7794 argument for deduction. */
7795 if (value_dependent_expression_p (arg
))
7798 type
= cv_unqualified (type
);
7799 tree argtype
= TREE_TYPE (arg
);
7800 if (same_type_p (type
, argtype
))
7803 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7804 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7808 /* Convert the indicated template ARG as necessary to match the
7809 indicated template PARM. Returns the converted ARG, or
7810 error_mark_node if the conversion was unsuccessful. Error and
7811 warning messages are issued under control of COMPLAIN. This
7812 conversion is for the Ith parameter in the parameter list. ARGS is
7813 the full set of template arguments deduced so far. */
7816 convert_template_argument (tree parm
,
7819 tsubst_flags_t complain
,
7825 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7827 if (parm
== error_mark_node
|| error_operand_p (arg
))
7828 return error_mark_node
;
7830 /* Trivially convert placeholders. */
7831 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7832 return convert_wildcard_argument (parm
, arg
);
7834 if (arg
== any_targ_node
)
7837 if (TREE_CODE (arg
) == TREE_LIST
7838 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7840 /* The template argument was the name of some
7841 member function. That's usually
7842 invalid, but static members are OK. In any
7843 case, grab the underlying fields/functions
7844 and issue an error later if required. */
7845 orig_arg
= TREE_VALUE (arg
);
7846 TREE_TYPE (arg
) = unknown_type_node
;
7851 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7852 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7853 || requires_tmpl_type
);
7855 /* When determining whether an argument pack expansion is a template,
7856 look at the pattern. */
7857 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7858 arg
= PACK_EXPANSION_PATTERN (arg
);
7860 /* Deal with an injected-class-name used as a template template arg. */
7861 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7863 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7864 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7866 if (cxx_dialect
>= cxx11
)
7867 /* OK under DR 1004. */;
7868 else if (complain
& tf_warning_or_error
)
7869 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7870 " used as template template argument", TYPE_NAME (arg
));
7871 else if (flag_pedantic_errors
)
7879 ((TREE_CODE (arg
) == TEMPLATE_DECL
7880 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7881 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7882 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7883 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7886 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7887 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7888 arg
= TYPE_STUB_DECL (arg
);
7890 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7892 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7893 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7895 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7897 if (complain
& tf_error
)
7898 error ("invalid use of destructor %qE as a type", orig_arg
);
7899 return error_mark_node
;
7902 permerror (input_location
,
7903 "to refer to a type member of a template parameter, "
7904 "use %<typename %E%>", orig_arg
);
7906 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7907 TREE_OPERAND (arg
, 1),
7913 if (is_type
!= requires_type
)
7917 if (complain
& tf_error
)
7919 error ("type/value mismatch at argument %d in template "
7920 "parameter list for %qD",
7923 inform (input_location
,
7924 " expected a constant of type %qT, got %qT",
7926 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7927 else if (requires_tmpl_type
)
7928 inform (input_location
,
7929 " expected a class template, got %qE", orig_arg
);
7931 inform (input_location
,
7932 " expected a type, got %qE", orig_arg
);
7935 return error_mark_node
;
7937 if (is_tmpl_type
^ requires_tmpl_type
)
7939 if (in_decl
&& (complain
& tf_error
))
7941 error ("type/value mismatch at argument %d in template "
7942 "parameter list for %qD",
7945 inform (input_location
,
7946 " expected a type, got %qT", DECL_NAME (arg
));
7948 inform (input_location
,
7949 " expected a class template, got %qT", orig_arg
);
7951 return error_mark_node
;
7954 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7955 /* We already did the appropriate conversion when packing args. */
7959 if (requires_tmpl_type
)
7961 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7962 /* The number of argument required is not known yet.
7963 Just accept it for now. */
7967 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7970 /* Strip alias templates that are equivalent to another
7972 arg
= get_underlying_template (arg
);
7973 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7975 if (coerce_template_template_parms (parmparm
, argparm
,
7981 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7983 if (val
!= error_mark_node
)
7985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7986 val
= TREE_TYPE (val
);
7987 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7988 val
= make_pack_expansion (val
, complain
);
7993 if (in_decl
&& (complain
& tf_error
))
7995 error ("type/value mismatch at argument %d in "
7996 "template parameter list for %qD",
7998 inform (input_location
,
7999 " expected a template of type %qD, got %qT",
8003 val
= error_mark_node
;
8006 // Check that the constraints are compatible before allowing the
8008 if (val
!= error_mark_node
)
8009 if (!is_compatible_template_arg (parm
, arg
))
8011 if (in_decl
&& (complain
& tf_error
))
8013 error ("constraint mismatch at argument %d in "
8014 "template parameter list for %qD",
8016 inform (input_location
, " expected %qD but got %qD",
8019 val
= error_mark_node
;
8025 /* We only form one instance of each template specialization.
8026 Therefore, if we use a non-canonical variant (i.e., a
8027 typedef), any future messages referring to the type will use
8028 the typedef, which is confusing if those future uses do not
8029 themselves also use the typedef. */
8031 val
= canonicalize_type_argument (val
, complain
);
8035 tree t
= TREE_TYPE (parm
);
8037 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8038 > TMPL_ARGS_DEPTH (args
))
8039 /* We don't have enough levels of args to do any substitution. This
8040 can happen in the context of -fnew-ttp-matching. */;
8041 else if (tree a
= type_uses_auto (t
))
8043 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8044 if (t
== error_mark_node
)
8045 return error_mark_node
;
8048 t
= tsubst (t
, args
, complain
, in_decl
);
8050 if (invalid_nontype_parm_type_p (t
, complain
))
8051 return error_mark_node
;
8053 if (t
!= TREE_TYPE (parm
))
8054 t
= canonicalize_type_argument (t
, complain
);
8056 if (!type_dependent_expression_p (orig_arg
)
8057 && !uses_template_parms (t
))
8058 /* We used to call digest_init here. However, digest_init
8059 will report errors, which we don't want when complain
8060 is zero. More importantly, digest_init will try too
8061 hard to convert things: for example, `0' should not be
8062 converted to pointer type at this point according to
8063 the standard. Accepting this is not merely an
8064 extension, since deciding whether or not these
8065 conversions can occur is part of determining which
8066 function template to call, or whether a given explicit
8067 argument specification is valid. */
8068 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8071 val
= canonicalize_expr_argument (orig_arg
, complain
);
8072 val
= maybe_convert_nontype_argument (t
, val
);
8076 if (val
== NULL_TREE
)
8077 val
= error_mark_node
;
8078 else if (val
== error_mark_node
&& (complain
& tf_error
))
8079 error ("could not convert template argument %qE from %qT to %qT",
8080 orig_arg
, TREE_TYPE (orig_arg
), t
);
8082 if (INDIRECT_REF_P (val
))
8084 /* Reject template arguments that are references to built-in
8085 functions with no library fallbacks. */
8086 const_tree inner
= TREE_OPERAND (val
, 0);
8087 const_tree innertype
= TREE_TYPE (inner
);
8089 && TYPE_REF_P (innertype
)
8090 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8091 && TREE_OPERAND_LENGTH (inner
) > 0
8092 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8093 return error_mark_node
;
8096 if (TREE_CODE (val
) == SCOPE_REF
)
8098 /* Strip typedefs from the SCOPE_REF. */
8099 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8100 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8102 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8103 QUALIFIED_NAME_IS_TEMPLATE (val
));
8110 /* Coerces the remaining template arguments in INNER_ARGS (from
8111 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8112 Returns the coerced argument pack. PARM_IDX is the position of this
8113 parameter in the template parameter list. ARGS is the original
8114 template argument list. */
8116 coerce_template_parameter_pack (tree parms
,
8124 tsubst_flags_t complain
)
8126 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8127 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8130 tree packed_parms
= NULL_TREE
;
8132 if (arg_idx
> nargs
)
8135 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8137 /* When the template parameter is a non-type template parameter pack
8138 or template template parameter pack whose type or template
8139 parameters use parameter packs, we know exactly how many arguments
8140 we are looking for. Build a vector of the instantiated decls for
8141 these template parameters in PACKED_PARMS. */
8142 /* We can't use make_pack_expansion here because it would interpret a
8143 _DECL as a use rather than a declaration. */
8144 tree decl
= TREE_VALUE (parm
);
8145 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8146 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8147 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8148 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8150 TREE_VEC_LENGTH (args
)--;
8151 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8152 TREE_VEC_LENGTH (args
)++;
8154 if (packed_parms
== error_mark_node
)
8155 return error_mark_node
;
8157 /* If we're doing a partial instantiation of a member template,
8158 verify that all of the types used for the non-type
8159 template parameter pack are, in fact, valid for non-type
8160 template parameters. */
8162 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8164 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8165 for (j
= 0; j
< len
; ++j
)
8167 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8168 if (invalid_nontype_parm_type_p (t
, complain
))
8169 return error_mark_node
;
8171 /* We don't know how many args we have yet, just
8172 use the unconverted ones for now. */
8176 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8178 /* Check if we have a placeholder pack, which indicates we're
8179 in the context of a introduction list. In that case we want
8180 to match this pack to the single placeholder. */
8181 else if (arg_idx
< nargs
8182 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8183 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8185 nargs
= arg_idx
+ 1;
8186 packed_args
= make_tree_vec (1);
8189 packed_args
= make_tree_vec (nargs
- arg_idx
);
8191 /* Convert the remaining arguments, which will be a part of the
8192 parameter pack "parm". */
8193 int first_pack_arg
= arg_idx
;
8194 for (; arg_idx
< nargs
; ++arg_idx
)
8196 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8197 tree actual_parm
= TREE_VALUE (parm
);
8198 int pack_idx
= arg_idx
- first_pack_arg
;
8202 /* Once we've packed as many args as we have types, stop. */
8203 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8205 else if (PACK_EXPANSION_P (arg
))
8206 /* We don't know how many args we have yet, just
8207 use the unconverted ones for now. */
8210 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8213 if (arg
== error_mark_node
)
8215 if (complain
& tf_error
)
8216 error ("template argument %d is invalid", arg_idx
+ 1);
8219 arg
= convert_template_argument (actual_parm
,
8220 arg
, new_args
, complain
, parm_idx
,
8222 if (arg
== error_mark_node
)
8224 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8227 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8228 && TREE_VEC_LENGTH (packed_args
) > 0)
8230 if (complain
& tf_error
)
8231 error ("wrong number of template arguments (%d, should be %d)",
8232 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8233 return error_mark_node
;
8236 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8237 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8238 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8241 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8242 TREE_CONSTANT (argument_pack
) = 1;
8245 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8247 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8248 TREE_VEC_LENGTH (packed_args
));
8249 return argument_pack
;
8252 /* Returns the number of pack expansions in the template argument vector
8256 pack_expansion_args_count (tree args
)
8261 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8263 tree elt
= TREE_VEC_ELT (args
, i
);
8264 if (elt
&& PACK_EXPANSION_P (elt
))
8270 /* Convert all template arguments to their appropriate types, and
8271 return a vector containing the innermost resulting template
8272 arguments. If any error occurs, return error_mark_node. Error and
8273 warning messages are issued under control of COMPLAIN.
8275 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8276 for arguments not specified in ARGS. Otherwise, if
8277 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8278 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8279 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8283 coerce_template_parms (tree parms
,
8286 tsubst_flags_t complain
,
8287 bool require_all_args
,
8288 bool use_default_args
)
8290 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8291 tree orig_inner_args
;
8294 tree new_inner_args
;
8296 /* When used as a boolean value, indicates whether this is a
8297 variadic template parameter list. Since it's an int, we can also
8298 subtract it from nparms to get the number of non-variadic
8301 int variadic_args_p
= 0;
8302 int post_variadic_parms
= 0;
8304 /* Adjustment to nparms for fixed parameter packs. */
8305 int fixed_pack_adjust
= 0;
8306 int fixed_packs
= 0;
8309 /* Likewise for parameters with default arguments. */
8312 if (args
== error_mark_node
)
8313 return error_mark_node
;
8315 nparms
= TREE_VEC_LENGTH (parms
);
8317 /* Determine if there are any parameter packs or default arguments. */
8318 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8320 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8322 ++post_variadic_parms
;
8323 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8325 if (TREE_PURPOSE (parm
))
8329 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8330 /* If there are no parameters that follow a parameter pack, we need to
8331 expand any argument packs so that we can deduce a parameter pack from
8332 some non-packed args followed by an argument pack, as in variadic85.C.
8333 If there are such parameters, we need to leave argument packs intact
8334 so the arguments are assigned properly. This can happen when dealing
8335 with a nested class inside a partial specialization of a class
8336 template, as in variadic92.C, or when deducing a template parameter pack
8337 from a sub-declarator, as in variadic114.C. */
8338 if (!post_variadic_parms
)
8339 inner_args
= expand_template_argument_pack (inner_args
);
8341 /* Count any pack expansion args. */
8342 variadic_args_p
= pack_expansion_args_count (inner_args
);
8344 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8345 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8346 || (nargs
< nparms
- variadic_p
8349 && (!use_default_args
8350 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8351 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8354 if (complain
& tf_error
)
8356 if (variadic_p
|| default_p
)
8358 nparms
-= variadic_p
+ default_p
;
8359 error ("wrong number of template arguments "
8360 "(%d, should be at least %d)", nargs
, nparms
);
8363 error ("wrong number of template arguments "
8364 "(%d, should be %d)", nargs
, nparms
);
8367 inform (DECL_SOURCE_LOCATION (in_decl
),
8368 "provided for %qD", in_decl
);
8371 return error_mark_node
;
8373 /* We can't pass a pack expansion to a non-pack parameter of an alias
8374 template (DR 1430). */
8376 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8377 || concept_template_p (in_decl
))
8379 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8381 if (complain
& tf_error
)
8383 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8385 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8386 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8388 if (PACK_EXPANSION_P (arg
)
8389 && !template_parameter_pack_p (parm
))
8391 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8392 error_at (location_of (arg
),
8393 "pack expansion argument for non-pack parameter "
8394 "%qD of alias template %qD", parm
, in_decl
);
8396 error_at (location_of (arg
),
8397 "pack expansion argument for non-pack parameter "
8398 "%qD of concept %qD", parm
, in_decl
);
8399 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8406 return error_mark_node
;
8409 /* We need to evaluate the template arguments, even though this
8410 template-id may be nested within a "sizeof". */
8413 new_inner_args
= make_tree_vec (nparms
);
8414 new_args
= add_outermost_template_args (args
, new_inner_args
);
8415 int pack_adjust
= 0;
8416 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8421 /* Get the Ith template parameter. */
8422 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8424 if (parm
== error_mark_node
)
8426 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8430 /* Calculate the next argument. */
8431 if (arg_idx
< nargs
)
8432 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8436 if (template_parameter_pack_p (TREE_VALUE (parm
))
8437 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8439 /* Some arguments will be placed in the
8440 template parameter pack PARM. */
8441 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8442 inner_args
, arg_idx
,
8446 if (arg
== NULL_TREE
)
8448 /* We don't know how many args we have yet, just use the
8449 unconverted (and still packed) ones for now. */
8450 new_inner_args
= orig_inner_args
;
8455 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8457 /* Store this argument. */
8458 if (arg
== error_mark_node
)
8461 /* We are done with all of the arguments. */
8467 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8468 arg_idx
+= pack_adjust
;
8469 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8472 fixed_pack_adjust
+= pack_adjust
;
8480 if (PACK_EXPANSION_P (arg
))
8482 /* "If every valid specialization of a variadic template
8483 requires an empty template parameter pack, the template is
8484 ill-formed, no diagnostic required." So check that the
8485 pattern works with this parameter. */
8486 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8487 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8491 if (conv
== error_mark_node
)
8493 if (complain
& tf_error
)
8494 inform (input_location
, "so any instantiation with a "
8495 "non-empty parameter pack would be ill-formed");
8498 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8499 /* Recover from missing typename. */
8500 TREE_VEC_ELT (inner_args
, arg_idx
)
8501 = make_pack_expansion (conv
, complain
);
8503 /* We don't know how many args we have yet, just
8504 use the unconverted ones for now. */
8505 new_inner_args
= inner_args
;
8510 else if (require_all_args
)
8512 /* There must be a default arg in this case. */
8513 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8515 /* The position of the first default template argument,
8516 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8518 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8519 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8520 arg_idx
- pack_adjust
);
8525 if (arg
== error_mark_node
)
8527 if (complain
& tf_error
)
8528 error ("template argument %d is invalid", arg_idx
+ 1);
8532 /* This can occur if there was an error in the template
8533 parameter list itself (which we would already have
8534 reported) that we are trying to recover from, e.g., a class
8535 template with a parameter list such as
8536 template<typename..., typename> (cpp0x/variadic150.C). */
8539 /* This can also happen with a fixed parameter pack (71834). */
8540 if (arg_idx
>= nargs
)
8544 arg
= convert_template_argument (TREE_VALUE (parm
),
8545 arg
, new_args
, complain
,
8548 if (arg
== error_mark_node
)
8550 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8553 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8555 /* If we had fixed parameter packs, we didn't know how many arguments we
8556 actually needed earlier; now we do. */
8557 nparms
+= fixed_pack_adjust
;
8558 variadic_p
-= fixed_packs
;
8562 if (arg_idx
< nargs
)
8564 /* We had some pack expansion arguments that will only work if the packs
8565 are empty, but wait until instantiation time to complain.
8566 See variadic-ttp3.C. */
8567 int len
= nparms
+ (nargs
- arg_idx
);
8568 tree args
= make_tree_vec (len
);
8570 for (; i
< nparms
; ++i
)
8571 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8572 for (; i
< len
; ++i
, ++arg_idx
)
8573 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8574 arg_idx
- pack_adjust
);
8575 new_inner_args
= args
;
8580 gcc_assert (!(complain
& tf_error
) || seen_error ());
8581 return error_mark_node
;
8584 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8585 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8586 TREE_VEC_LENGTH (new_inner_args
));
8588 return new_inner_args
;
8591 /* Convert all template arguments to their appropriate types, and
8592 return a vector containing the innermost resulting template
8593 arguments. If any error occurs, return error_mark_node. Error and
8594 warning messages are not issued.
8596 Note that no function argument deduction is performed, and default
8597 arguments are used to fill in unspecified arguments. */
8599 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8601 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8604 /* Convert all template arguments to their appropriate type, and
8605 instantiate default arguments as needed. This returns a vector
8606 containing the innermost resulting template arguments, or
8607 error_mark_node if unsuccessful. */
8609 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8610 tsubst_flags_t complain
)
8612 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8615 /* Like coerce_template_parms. If PARMS represents all template
8616 parameters levels, this function returns a vector of vectors
8617 representing all the resulting argument levels. Note that in this
8618 case, only the innermost arguments are coerced because the
8619 outermost ones are supposed to have been coerced already.
8621 Otherwise, if PARMS represents only (the innermost) vector of
8622 parameters, this function returns a vector containing just the
8623 innermost resulting arguments. */
8626 coerce_innermost_template_parms (tree parms
,
8629 tsubst_flags_t complain
,
8630 bool require_all_args
,
8631 bool use_default_args
)
8633 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8634 int args_depth
= TMPL_ARGS_DEPTH (args
);
8637 if (parms_depth
> 1)
8639 coerced_args
= make_tree_vec (parms_depth
);
8643 for (level
= parms
, cur_depth
= parms_depth
;
8644 parms_depth
> 0 && level
!= NULL_TREE
;
8645 level
= TREE_CHAIN (level
), --cur_depth
)
8648 if (cur_depth
== args_depth
)
8649 l
= coerce_template_parms (TREE_VALUE (level
),
8650 args
, in_decl
, complain
,
8654 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8656 if (l
== error_mark_node
)
8657 return error_mark_node
;
8659 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8663 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8664 args
, in_decl
, complain
,
8667 return coerced_args
;
8670 /* Returns 1 if template args OT and NT are equivalent. */
8673 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8677 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8679 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8682 if (TREE_CODE (nt
) == TREE_VEC
)
8683 /* For member templates */
8684 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8685 else if (PACK_EXPANSION_P (ot
))
8686 return (PACK_EXPANSION_P (nt
)
8687 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8688 PACK_EXPANSION_PATTERN (nt
))
8689 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8690 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8691 else if (ARGUMENT_PACK_P (ot
))
8696 if (!ARGUMENT_PACK_P (nt
))
8699 opack
= ARGUMENT_PACK_ARGS (ot
);
8700 npack
= ARGUMENT_PACK_ARGS (nt
);
8701 len
= TREE_VEC_LENGTH (opack
);
8702 if (TREE_VEC_LENGTH (npack
) != len
)
8704 for (i
= 0; i
< len
; ++i
)
8705 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8706 TREE_VEC_ELT (npack
, i
)))
8710 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8712 else if (TYPE_P (nt
))
8716 /* Don't treat an alias template specialization with dependent
8717 arguments as equivalent to its underlying type when used as a
8718 template argument; we need them to be distinct so that we
8719 substitute into the specialization arguments at instantiation
8720 time. And aliases can't be equivalent without being ==, so
8721 we don't need to look any deeper.
8723 During partial ordering, however, we need to treat them normally so
8724 that we can order uses of the same alias with different
8725 cv-qualification (79960). */
8727 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8730 return same_type_p (ot
, nt
);
8732 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8736 /* Try to treat a template non-type argument that has been converted
8737 to the parameter type as equivalent to one that hasn't yet. */
8738 for (enum tree_code code1
= TREE_CODE (ot
);
8739 CONVERT_EXPR_CODE_P (code1
)
8740 || code1
== NON_LVALUE_EXPR
;
8741 code1
= TREE_CODE (ot
))
8742 ot
= TREE_OPERAND (ot
, 0);
8743 for (enum tree_code code2
= TREE_CODE (nt
);
8744 CONVERT_EXPR_CODE_P (code2
)
8745 || code2
== NON_LVALUE_EXPR
;
8746 code2
= TREE_CODE (nt
))
8747 nt
= TREE_OPERAND (nt
, 0);
8749 return cp_tree_equal (ot
, nt
);
8753 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8754 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8755 NEWARG_PTR with the offending arguments if they are non-NULL. */
8758 comp_template_args (tree oldargs
, tree newargs
,
8759 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8764 if (oldargs
== newargs
)
8767 if (!oldargs
|| !newargs
)
8770 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8773 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8775 tree nt
= TREE_VEC_ELT (newargs
, i
);
8776 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8778 if (! template_args_equal (ot
, nt
, partial_order
))
8780 if (oldarg_ptr
!= NULL
)
8782 if (newarg_ptr
!= NULL
)
8791 comp_template_args_porder (tree oargs
, tree nargs
)
8793 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8796 /* Implement a freelist interface for objects of type T.
8798 Head is a separate object, rather than a regular member, so that we
8799 can define it as a GTY deletable pointer, which is highly
8800 desirable. A data member could be declared that way, but then the
8801 containing object would implicitly get GTY((user)), which would
8802 prevent us from instantiating freelists as global objects.
8803 Although this way we can create freelist global objects, they're
8804 such thin wrappers that instantiating temporaries at every use
8805 loses nothing and saves permanent storage for the freelist object.
8807 Member functions next, anew, poison and reinit have default
8808 implementations that work for most of the types we're interested
8809 in, but if they don't work for some type, they should be explicitly
8810 specialized. See the comments before them for requirements, and
8811 the example specializations for the tree_list_freelist. */
8812 template <typename T
>
8815 /* Return the next object in a chain. We could just do type
8816 punning, but if we access the object with its underlying type, we
8817 avoid strict-aliasing trouble. This needs only work between
8818 poison and reinit. */
8819 static T
*&next (T
*obj
) { return obj
->next
; }
8821 /* Return a newly allocated, uninitialized or minimally-initialized
8822 object of type T. Any initialization performed by anew should
8823 either remain across the life of the object and the execution of
8824 poison, or be redone by reinit. */
8825 static T
*anew () { return ggc_alloc
<T
> (); }
8827 /* Optionally scribble all over the bits holding the object, so that
8828 they become (mostly?) uninitialized memory. This is called while
8829 preparing to make the object part of the free list. */
8830 static void poison (T
*obj
) {
8831 T
*p ATTRIBUTE_UNUSED
= obj
;
8832 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8834 #ifdef ENABLE_GC_CHECKING
8835 /* Poison the data, to indicate the data is garbage. */
8836 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8837 memset (p
, 0xa5, sizeof (*p
));
8839 /* Let valgrind know the object is free. */
8840 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8842 /* Let valgrind know the next portion of the object is available,
8843 but uninitialized. */
8844 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8847 /* Bring an object that underwent at least one lifecycle after anew
8848 and before the most recent free and poison, back to a usable
8849 state, reinitializing whatever is needed for it to be
8850 functionally equivalent to an object just allocated and returned
8851 by anew. This may poison or clear the next field, used by
8852 freelist housekeeping after poison was called. */
8853 static void reinit (T
*obj
) {
8854 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8856 #ifdef ENABLE_GC_CHECKING
8857 memset (q
, 0xa5, sizeof (*q
));
8859 /* Let valgrind know the entire object is available, but
8861 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8864 /* Reference a GTY-deletable pointer that points to the first object
8865 in the free list proper. */
8868 /* Construct a freelist object chaining objects off of HEAD. */
8869 freelist (T
*&head
) : head(head
) {}
8871 /* Add OBJ to the free object list. The former head becomes OBJ's
8880 /* Take an object from the free list, if one is available, or
8881 allocate a new one. Objects taken from the free list should be
8882 regarded as filled with garbage, except for bits that are
8883 configured to be preserved across free and alloc. */
8898 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8899 want to allocate a TREE_LIST using the usual interface, and ensure
8900 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8901 build_tree_list logic in reinit, so this could go out of sync. */
8904 freelist
<tree_node
>::next (tree obj
)
8906 return TREE_CHAIN (obj
);
8910 freelist
<tree_node
>::anew ()
8912 return build_tree_list (NULL
, NULL
);
8916 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8918 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8919 tree p ATTRIBUTE_UNUSED
= obj
;
8920 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8921 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8923 #ifdef ENABLE_GC_CHECKING
8924 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8926 /* Poison the data, to indicate the data is garbage. */
8927 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8928 memset (p
, 0xa5, size
);
8930 /* Let valgrind know the object is free. */
8931 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8932 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8933 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8934 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8936 #ifdef ENABLE_GC_CHECKING
8937 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
8938 /* Keep TREE_CHAIN functional. */
8939 TREE_SET_CODE (obj
, TREE_LIST
);
8941 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8946 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
8948 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8950 #ifdef ENABLE_GC_CHECKING
8951 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8952 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8953 memset (obj
, 0, sizeof (tree_list
));
8956 /* Let valgrind know the entire object is available, but
8958 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8960 #ifdef ENABLE_GC_CHECKING
8961 TREE_SET_CODE (obj
, TREE_LIST
);
8963 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8967 /* Point to the first object in the TREE_LIST freelist. */
8968 static GTY((deletable
)) tree tree_list_freelist_head
;
8969 /* Return the/an actual TREE_LIST freelist. */
8970 static inline freelist
<tree_node
>
8971 tree_list_freelist ()
8973 return tree_list_freelist_head
;
8976 /* Point to the first object in the tinst_level freelist. */
8977 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
8978 /* Return the/an actual tinst_level freelist. */
8979 static inline freelist
<tinst_level
>
8980 tinst_level_freelist ()
8982 return tinst_level_freelist_head
;
8985 /* Point to the first object in the pending_template freelist. */
8986 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
8987 /* Return the/an actual pending_template freelist. */
8988 static inline freelist
<pending_template
>
8989 pending_template_freelist ()
8991 return pending_template_freelist_head
;
8994 /* Build the TREE_LIST object out of a split list, store it
8995 permanently, and return it. */
8997 tinst_level::to_list ()
8999 gcc_assert (split_list_p ());
9000 tree ret
= tree_list_freelist ().alloc ();
9001 TREE_PURPOSE (ret
) = tldcl
;
9002 TREE_VALUE (ret
) = targs
;
9005 gcc_assert (tree_list_p ());
9009 const unsigned short tinst_level::refcount_infinity
;
9011 /* Increment OBJ's refcount unless it is already infinite. */
9012 static tinst_level
*
9013 inc_refcount_use (tinst_level
*obj
)
9015 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9020 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9022 tinst_level::free (tinst_level
*obj
)
9024 if (obj
->tree_list_p ())
9025 tree_list_freelist ().free (obj
->get_node ());
9026 tinst_level_freelist ().free (obj
);
9029 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9030 OBJ's DECL and OBJ, and start over with the tinst_level object that
9031 used to be referenced by OBJ's NEXT. */
9033 dec_refcount_use (tinst_level
*obj
)
9036 && obj
->refcount
!= tinst_level::refcount_infinity
9037 && !--obj
->refcount
)
9039 tinst_level
*next
= obj
->next
;
9040 tinst_level::free (obj
);
9045 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9046 and of the former PTR. Omitting the second argument is equivalent
9047 to passing (T*)NULL; this is allowed because passing the
9048 zero-valued integral constant NULL confuses type deduction and/or
9049 overload resolution. */
9050 template <typename T
>
9052 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9055 ptr
= inc_refcount_use (obj
);
9056 dec_refcount_use (save
);
9060 add_pending_template (tree d
)
9062 tree ti
= (TYPE_P (d
)
9063 ? CLASSTYPE_TEMPLATE_INFO (d
)
9064 : DECL_TEMPLATE_INFO (d
));
9065 struct pending_template
*pt
;
9068 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9071 /* We are called both from instantiate_decl, where we've already had a
9072 tinst_level pushed, and instantiate_template, where we haven't.
9074 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9075 level
= !current_tinst_level
9076 || current_tinst_level
->maybe_get_node () != d
;
9079 push_tinst_level (d
);
9081 pt
= pending_template_freelist ().alloc ();
9084 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9085 if (last_pending_template
)
9086 last_pending_template
->next
= pt
;
9088 pending_templates
= pt
;
9090 last_pending_template
= pt
;
9092 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9099 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9100 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9101 documentation for TEMPLATE_ID_EXPR. */
9104 lookup_template_function (tree fns
, tree arglist
)
9106 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9107 return error_mark_node
;
9109 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9111 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9113 error ("%q#D is not a function template", fns
);
9114 return error_mark_node
;
9117 if (BASELINK_P (fns
))
9119 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9121 BASELINK_FUNCTIONS (fns
),
9126 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9129 /* Within the scope of a template class S<T>, the name S gets bound
9130 (in build_self_reference) to a TYPE_DECL for the class, not a
9131 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9132 or one of its enclosing classes, and that type is a template,
9133 return the associated TEMPLATE_DECL. Otherwise, the original
9136 Also handle the case when DECL is a TREE_LIST of ambiguous
9137 injected-class-names from different bases. */
9140 maybe_get_template_decl_from_type_decl (tree decl
)
9142 if (decl
== NULL_TREE
)
9145 /* DR 176: A lookup that finds an injected-class-name (10.2
9146 [class.member.lookup]) can result in an ambiguity in certain cases
9147 (for example, if it is found in more than one base class). If all of
9148 the injected-class-names that are found refer to specializations of
9149 the same class template, and if the name is followed by a
9150 template-argument-list, the reference refers to the class template
9151 itself and not a specialization thereof, and is not ambiguous. */
9152 if (TREE_CODE (decl
) == TREE_LIST
)
9154 tree t
, tmpl
= NULL_TREE
;
9155 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9157 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9160 else if (tmpl
!= elt
)
9163 if (tmpl
&& t
== NULL_TREE
)
9169 return (decl
!= NULL_TREE
9170 && DECL_SELF_REFERENCE_P (decl
)
9171 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9172 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9175 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9176 parameters, find the desired type.
9178 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9180 IN_DECL, if non-NULL, is the template declaration we are trying to
9183 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9184 the class we are looking up.
9186 Issue error and warning messages under control of COMPLAIN.
9188 If the template class is really a local class in a template
9189 function, then the FUNCTION_CONTEXT is the function in which it is
9192 ??? Note that this function is currently called *twice* for each
9193 template-id: the first time from the parser, while creating the
9194 incomplete type (finish_template_type), and the second type during the
9195 real instantiation (instantiate_template_class). This is surely something
9196 that we want to avoid. It also causes some problems with argument
9197 coercion (see convert_nontype_argument for more information on this). */
9200 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9201 int entering_scope
, tsubst_flags_t complain
)
9203 tree templ
= NULL_TREE
, parmlist
;
9210 if (identifier_p (d1
))
9212 tree value
= innermost_non_namespace_value (d1
);
9213 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9218 push_decl_namespace (context
);
9219 templ
= lookup_name (d1
);
9220 templ
= maybe_get_template_decl_from_type_decl (templ
);
9222 pop_decl_namespace ();
9225 context
= DECL_CONTEXT (templ
);
9227 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9229 tree type
= TREE_TYPE (d1
);
9231 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9232 an implicit typename for the second A. Deal with it. */
9233 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9234 type
= TREE_TYPE (type
);
9236 if (CLASSTYPE_TEMPLATE_INFO (type
))
9238 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9239 d1
= DECL_NAME (templ
);
9242 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9243 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9245 templ
= TYPE_TI_TEMPLATE (d1
);
9246 d1
= DECL_NAME (templ
);
9248 else if (DECL_TYPE_TEMPLATE_P (d1
))
9251 d1
= DECL_NAME (templ
);
9252 context
= DECL_CONTEXT (templ
);
9254 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9257 d1
= DECL_NAME (templ
);
9260 /* Issue an error message if we didn't find a template. */
9263 if (complain
& tf_error
)
9264 error ("%qT is not a template", d1
);
9265 return error_mark_node
;
9268 if (TREE_CODE (templ
) != TEMPLATE_DECL
9269 /* Make sure it's a user visible template, if it was named by
9271 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9272 && !PRIMARY_TEMPLATE_P (templ
)))
9274 if (complain
& tf_error
)
9276 error ("non-template type %qT used as a template", d1
);
9278 error ("for template declaration %q+D", in_decl
);
9280 return error_mark_node
;
9283 complain
&= ~tf_user
;
9285 /* An alias that just changes the name of a template is equivalent to the
9286 other template, so if any of the arguments are pack expansions, strip
9287 the alias to avoid problems with a pack expansion passed to a non-pack
9288 alias template parameter (DR 1430). */
9289 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9290 templ
= get_underlying_template (templ
);
9292 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9295 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9296 if (arglist2
== error_mark_node
9297 || (!uses_template_parms (arglist2
)
9298 && check_instantiated_args (templ
, arglist2
, complain
)))
9299 return error_mark_node
;
9301 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9306 tree template_type
= TREE_TYPE (templ
);
9309 tree found
= NULL_TREE
;
9312 int is_dependent_type
;
9313 int use_partial_inst_tmpl
= false;
9315 if (template_type
== error_mark_node
)
9316 /* An error occurred while building the template TEMPL, and a
9317 diagnostic has most certainly been emitted for that
9318 already. Let's propagate that error. */
9319 return error_mark_node
;
9321 gen_tmpl
= most_general_template (templ
);
9322 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9323 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9324 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9326 if (arg_depth
== 1 && parm_depth
> 1)
9328 /* We've been given an incomplete set of template arguments.
9331 template <class T> struct S1 {
9332 template <class U> struct S2 {};
9333 template <class U> struct S2<U*> {};
9336 we will be called with an ARGLIST of `U*', but the
9337 TEMPLATE will be `template <class T> template
9338 <class U> struct S1<T>::S2'. We must fill in the missing
9340 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9341 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9342 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9345 /* Now we should have enough arguments. */
9346 gcc_assert (parm_depth
== arg_depth
);
9348 /* From here on, we're only interested in the most general
9351 /* Calculate the BOUND_ARGS. These will be the args that are
9352 actually tsubst'd into the definition to create the
9354 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9356 /*require_all_args=*/true,
9357 /*use_default_args=*/true);
9359 if (arglist
== error_mark_node
)
9360 /* We were unable to bind the arguments. */
9361 return error_mark_node
;
9363 /* In the scope of a template class, explicit references to the
9364 template class refer to the type of the template, not any
9365 instantiation of it. For example, in:
9367 template <class T> class C { void f(C<T>); }
9369 the `C<T>' is just the same as `C'. Outside of the
9370 class, however, such a reference is an instantiation. */
9372 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9373 || currently_open_class (template_type
))
9375 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9377 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9378 return template_type
;
9381 /* If we already have this specialization, return it. */
9382 elt
.tmpl
= gen_tmpl
;
9384 elt
.spec
= NULL_TREE
;
9385 hash
= spec_hasher::hash (&elt
);
9386 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9391 /* If the the template's constraints are not satisfied,
9392 then we cannot form a valid type.
9394 Note that the check is deferred until after the hash
9395 lookup. This prevents redundant checks on previously
9396 instantiated specializations. */
9397 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9399 if (complain
& tf_error
)
9401 auto_diagnostic_group d
;
9402 error ("template constraint failure");
9403 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9405 return error_mark_node
;
9408 is_dependent_type
= uses_template_parms (arglist
);
9410 /* If the deduced arguments are invalid, then the binding
9412 if (!is_dependent_type
9413 && check_instantiated_args (gen_tmpl
,
9414 INNERMOST_TEMPLATE_ARGS (arglist
),
9416 return error_mark_node
;
9418 if (!is_dependent_type
9419 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9420 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9421 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9423 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9424 DECL_NAME (gen_tmpl
),
9425 /*tag_scope=*/ts_global
);
9429 context
= DECL_CONTEXT (gen_tmpl
);
9430 if (context
&& TYPE_P (context
))
9432 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9433 context
= complete_type (context
);
9436 context
= tsubst (context
, arglist
, complain
, in_decl
);
9438 if (context
== error_mark_node
)
9439 return error_mark_node
;
9442 context
= global_namespace
;
9444 /* Create the type. */
9445 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9447 /* The user referred to a specialization of an alias
9448 template represented by GEN_TMPL.
9450 [temp.alias]/2 says:
9452 When a template-id refers to the specialization of an
9453 alias template, it is equivalent to the associated
9454 type obtained by substitution of its
9455 template-arguments for the template-parameters in the
9456 type-id of the alias template. */
9458 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9459 /* Note that the call above (by indirectly calling
9460 register_specialization in tsubst_decl) registers the
9461 TYPE_DECL representing the specialization of the alias
9462 template. So next time someone substitutes ARGLIST for
9463 the template parms into the alias template (GEN_TMPL),
9464 she'll get that TYPE_DECL back. */
9466 if (t
== error_mark_node
)
9469 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9471 if (!is_dependent_type
)
9473 set_current_access_from_decl (TYPE_NAME (template_type
));
9474 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9475 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9476 arglist
, complain
, in_decl
),
9477 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9478 arglist
, complain
, in_decl
),
9479 SCOPED_ENUM_P (template_type
), NULL
);
9481 if (t
== error_mark_node
)
9486 /* We don't want to call start_enum for this type, since
9487 the values for the enumeration constants may involve
9488 template parameters. And, no one should be interested
9489 in the enumeration constants for such a type. */
9490 t
= cxx_make_type (ENUMERAL_TYPE
);
9491 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9493 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9494 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9495 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9497 else if (CLASS_TYPE_P (template_type
))
9499 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9500 instantiated here. */
9501 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9503 t
= make_class_type (TREE_CODE (template_type
));
9504 CLASSTYPE_DECLARED_CLASS (t
)
9505 = CLASSTYPE_DECLARED_CLASS (template_type
);
9506 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9508 /* A local class. Make sure the decl gets registered properly. */
9509 if (context
== current_function_decl
)
9510 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9512 return error_mark_node
;
9514 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9515 /* This instantiation is another name for the primary
9516 template type. Set the TYPE_CANONICAL field
9518 TYPE_CANONICAL (t
) = template_type
;
9519 else if (any_template_arguments_need_structural_equality_p (arglist
))
9520 /* Some of the template arguments require structural
9521 equality testing, so this template class requires
9522 structural equality testing. */
9523 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9528 /* If we called start_enum or pushtag above, this information
9529 will already be set up. */
9532 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9534 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9535 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9536 DECL_SOURCE_LOCATION (type_decl
)
9537 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9540 type_decl
= TYPE_NAME (t
);
9542 if (CLASS_TYPE_P (template_type
))
9544 TREE_PRIVATE (type_decl
)
9545 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9546 TREE_PROTECTED (type_decl
)
9547 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9548 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9550 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9551 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9555 if (OVERLOAD_TYPE_P (t
)
9556 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9558 static const char *tags
[] = {"abi_tag", "may_alias"};
9560 for (unsigned ix
= 0; ix
!= 2; ix
++)
9563 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9567 = tree_cons (TREE_PURPOSE (attributes
),
9568 TREE_VALUE (attributes
),
9569 TYPE_ATTRIBUTES (t
));
9573 /* Let's consider the explicit specialization of a member
9574 of a class template specialization that is implicitly instantiated,
9579 template<class U> struct M {}; //#0
9584 struct S<int>::M<char> //#1
9588 [temp.expl.spec]/4 says this is valid.
9590 In this case, when we write:
9593 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9596 When we encounter #1, we want to store the partial instantiation
9597 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9599 For all cases other than this "explicit specialization of member of a
9600 class template", we just want to store the most general template into
9601 the CLASSTYPE_TI_TEMPLATE of M.
9603 This case of "explicit specialization of member of a class template"
9605 1/ the enclosing class is an instantiation of, and therefore not
9606 the same as, the context of the most general template, and
9607 2/ we aren't looking at the partial instantiation itself, i.e.
9608 the innermost arguments are not the same as the innermost parms of
9609 the most general template.
9611 So it's only when 1/ and 2/ happens that we want to use the partial
9612 instantiation of the member template in lieu of its most general
9615 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9616 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9617 /* the enclosing class must be an instantiation... */
9618 && CLASS_TYPE_P (context
)
9619 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9621 TREE_VEC_LENGTH (arglist
)--;
9622 ++processing_template_decl
;
9623 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9624 tree partial_inst_args
=
9625 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9626 arglist
, complain
, NULL_TREE
);
9627 --processing_template_decl
;
9628 TREE_VEC_LENGTH (arglist
)++;
9629 if (partial_inst_args
== error_mark_node
)
9630 return error_mark_node
;
9631 use_partial_inst_tmpl
=
9632 /*...and we must not be looking at the partial instantiation
9634 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9638 if (!use_partial_inst_tmpl
)
9639 /* This case is easy; there are no member templates involved. */
9643 /* This is a full instantiation of a member template. Find
9644 the partial instantiation of which this is an instance. */
9646 /* Temporarily reduce by one the number of levels in the ARGLIST
9647 so as to avoid comparing the last set of arguments. */
9648 TREE_VEC_LENGTH (arglist
)--;
9649 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9650 TREE_VEC_LENGTH (arglist
)++;
9651 /* FOUND is either a proper class type, or an alias
9652 template specialization. In the later case, it's a
9653 TYPE_DECL, resulting from the substituting of arguments
9654 for parameters in the TYPE_DECL of the alias template
9655 done earlier. So be careful while getting the template
9657 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9659 : (TREE_CODE (found
) == TYPE_DECL
9660 ? DECL_TI_TEMPLATE (found
)
9661 : CLASSTYPE_TI_TEMPLATE (found
)));
9664 // Build template info for the new specialization.
9665 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9668 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9669 entry
= ggc_alloc
<spec_entry
> ();
9673 /* Note this use of the partial instantiation so we can check it
9674 later in maybe_process_partial_specialization. */
9675 DECL_TEMPLATE_INSTANTIATIONS (found
)
9676 = tree_cons (arglist
, t
,
9677 DECL_TEMPLATE_INSTANTIATIONS (found
));
9679 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9680 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9681 /* Now that the type has been registered on the instantiations
9682 list, we set up the enumerators. Because the enumeration
9683 constants may involve the enumeration type itself, we make
9684 sure to register the type first, and then create the
9685 constants. That way, doing tsubst_expr for the enumeration
9686 constants won't result in recursive calls here; we'll find
9687 the instantiation and exit above. */
9688 tsubst_enum (template_type
, t
, arglist
);
9690 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9691 /* If the type makes use of template parameters, the
9692 code that generates debugging information will crash. */
9693 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9695 /* Possibly limit visibility based on template args. */
9696 TREE_PUBLIC (type_decl
) = 1;
9697 determine_visibility (type_decl
);
9699 inherit_targ_abi_tags (t
);
9705 /* Wrapper for lookup_template_class_1. */
9708 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9709 int entering_scope
, tsubst_flags_t complain
)
9712 timevar_push (TV_TEMPLATE_INST
);
9713 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9714 entering_scope
, complain
);
9715 timevar_pop (TV_TEMPLATE_INST
);
9719 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9722 lookup_template_variable (tree templ
, tree arglist
)
9724 /* The type of the expression is NULL_TREE since the template-id could refer
9725 to an explicit or partial specialization. */
9726 tree type
= NULL_TREE
;
9727 if (flag_concepts
&& variable_concept_p (templ
))
9728 /* Except that concepts are always bool. */
9729 type
= boolean_type_node
;
9730 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9733 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9736 finish_template_variable (tree var
, tsubst_flags_t complain
)
9738 tree templ
= TREE_OPERAND (var
, 0);
9739 tree arglist
= TREE_OPERAND (var
, 1);
9741 /* We never want to return a VAR_DECL for a variable concept, since they
9742 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9743 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9744 if (concept_p
&& processing_template_decl
)
9747 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9748 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9750 templ
= most_general_template (templ
);
9751 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9752 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9754 /*use_default*/true);
9756 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9758 if (complain
& tf_error
)
9760 auto_diagnostic_group d
;
9761 error ("use of invalid variable template %qE", var
);
9762 diagnose_constraints (location_of (var
), templ
, arglist
);
9764 return error_mark_node
;
9767 /* If a template-id refers to a specialization of a variable
9768 concept, then the expression is true if and only if the
9769 concept's constraints are satisfied by the given template
9772 NOTE: This is an extension of Concepts Lite TS that
9773 allows constraints to be used in expressions. */
9776 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9777 return evaluate_variable_concept (decl
, arglist
);
9780 return instantiate_template (templ
, arglist
, complain
);
9783 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9784 TARGS template args, and instantiate it if it's not dependent. */
9787 lookup_and_finish_template_variable (tree templ
, tree targs
,
9788 tsubst_flags_t complain
)
9790 templ
= lookup_template_variable (templ
, targs
);
9791 if (!any_dependent_template_arguments_p (targs
))
9793 templ
= finish_template_variable (templ
, complain
);
9797 return convert_from_reference (templ
);
9806 /* True when we should also visit template parameters that occur in
9807 non-deduced contexts. */
9808 bool include_nondeduced_p
;
9809 hash_set
<tree
> *visited
;
9812 /* Called from for_each_template_parm via walk_tree. */
9815 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9818 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9819 tree_fn_t fn
= pfd
->fn
;
9820 void *data
= pfd
->data
;
9821 tree result
= NULL_TREE
;
9823 #define WALK_SUBTREE(NODE) \
9826 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9827 pfd->include_nondeduced_p, \
9829 if (result) goto out; \
9833 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9837 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9838 WALK_SUBTREE (TYPE_CONTEXT (t
));
9840 switch (TREE_CODE (t
))
9843 if (TYPE_PTRMEMFUNC_P (t
))
9849 if (!TYPE_TEMPLATE_INFO (t
))
9852 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9856 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9857 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9861 /* Since we're not going to walk subtrees, we have to do this
9863 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9867 /* Check the return type. */
9868 WALK_SUBTREE (TREE_TYPE (t
));
9870 /* Check the parameter types. Since default arguments are not
9871 instantiated until they are needed, the TYPE_ARG_TYPES may
9872 contain expressions that involve template parameters. But,
9873 no-one should be looking at them yet. And, once they're
9874 instantiated, they don't contain template parameters, so
9875 there's no point in looking at them then, either. */
9879 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9880 WALK_SUBTREE (TREE_VALUE (parm
));
9882 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9883 want walk_tree walking into them itself. */
9887 if (flag_noexcept_type
)
9889 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9891 WALK_SUBTREE (TREE_PURPOSE (spec
));
9897 case UNDERLYING_TYPE
:
9898 if (pfd
->include_nondeduced_p
9899 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9901 pfd
->include_nondeduced_p
,
9903 return error_mark_node
;
9904 *walk_subtrees
= false;
9909 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9910 WALK_SUBTREE (DECL_TI_ARGS (t
));
9915 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9916 WALK_SUBTREE (DECL_INITIAL (t
));
9917 if (DECL_CONTEXT (t
)
9918 && pfd
->include_nondeduced_p
)
9919 WALK_SUBTREE (DECL_CONTEXT (t
));
9922 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9923 /* Record template parameters such as `T' inside `TT<T>'. */
9924 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9927 case TEMPLATE_TEMPLATE_PARM
:
9928 case TEMPLATE_TYPE_PARM
:
9929 case TEMPLATE_PARM_INDEX
:
9930 if (fn
&& (*fn
)(t
, data
))
9937 /* A template template parameter is encountered. */
9938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9939 WALK_SUBTREE (TREE_TYPE (t
));
9941 /* Already substituted template template parameter */
9946 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9947 partial instantiation. */
9948 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9952 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9953 && pfd
->include_nondeduced_p
)
9954 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9959 /* If there's no type, then this thing must be some expression
9960 involving template parameters. */
9961 if (!fn
&& !TREE_TYPE (t
))
9962 return error_mark_node
;
9967 case IMPLICIT_CONV_EXPR
:
9968 case REINTERPRET_CAST_EXPR
:
9969 case CONST_CAST_EXPR
:
9970 case STATIC_CAST_EXPR
:
9971 case DYNAMIC_CAST_EXPR
:
9975 case PSEUDO_DTOR_EXPR
:
9977 return error_mark_node
;
9986 /* We didn't find any template parameters we liked. */
9991 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9992 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9993 call FN with the parameter and the DATA.
9994 If FN returns nonzero, the iteration is terminated, and
9995 for_each_template_parm returns 1. Otherwise, the iteration
9996 continues. If FN never returns a nonzero value, the value
9997 returned by for_each_template_parm is 0. If FN is NULL, it is
9998 considered to be the function which always returns 1.
10000 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10001 parameters that occur in non-deduced contexts. When false, only
10002 visits those template parameters that can be deduced. */
10005 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10006 hash_set
<tree
> *visited
,
10007 bool include_nondeduced_p
,
10010 struct pair_fn_data pfd
;
10015 pfd
.any_fn
= any_fn
;
10017 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10019 /* Walk the tree. (Conceptually, we would like to walk without
10020 duplicates, but for_each_template_parm_r recursively calls
10021 for_each_template_parm, so we would need to reorganize a fair
10022 bit to use walk_tree_without_duplicates, so we keep our own
10025 pfd
.visited
= visited
;
10027 pfd
.visited
= new hash_set
<tree
>;
10028 result
= cp_walk_tree (&t
,
10029 for_each_template_parm_r
,
10036 delete pfd
.visited
;
10043 /* Returns true if T depends on any template parameter. */
10046 uses_template_parms (tree t
)
10048 if (t
== NULL_TREE
)
10052 int saved_processing_template_decl
;
10054 saved_processing_template_decl
= processing_template_decl
;
10055 if (!saved_processing_template_decl
)
10056 processing_template_decl
= 1;
10058 dependent_p
= dependent_type_p (t
);
10059 else if (TREE_CODE (t
) == TREE_VEC
)
10060 dependent_p
= any_dependent_template_arguments_p (t
);
10061 else if (TREE_CODE (t
) == TREE_LIST
)
10062 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10063 || uses_template_parms (TREE_CHAIN (t
)));
10064 else if (TREE_CODE (t
) == TYPE_DECL
)
10065 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10066 else if (DECL_P (t
)
10068 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10069 || TREE_CODE (t
) == OVERLOAD
10071 || identifier_p (t
)
10072 || TREE_CODE (t
) == TRAIT_EXPR
10073 || TREE_CODE (t
) == CONSTRUCTOR
10074 || CONSTANT_CLASS_P (t
))
10075 dependent_p
= (type_dependent_expression_p (t
)
10076 || value_dependent_expression_p (t
));
10079 gcc_assert (t
== error_mark_node
);
10080 dependent_p
= false;
10083 processing_template_decl
= saved_processing_template_decl
;
10085 return dependent_p
;
10088 /* Returns true iff current_function_decl is an incompletely instantiated
10089 template. Useful instead of processing_template_decl because the latter
10090 is set to 0 during instantiate_non_dependent_expr. */
10093 in_template_function (void)
10095 tree fn
= current_function_decl
;
10097 ++processing_template_decl
;
10098 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10099 && DECL_TEMPLATE_INFO (fn
)
10100 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10101 --processing_template_decl
;
10105 /* Returns true if T depends on any template parameter with level LEVEL. */
10108 uses_template_parms_level (tree t
, int level
)
10110 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10111 /*include_nondeduced_p=*/true);
10114 /* Returns true if the signature of DECL depends on any template parameter from
10115 its enclosing class. */
10118 uses_outer_template_parms (tree decl
)
10120 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10123 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10124 &depth
, NULL
, /*include_nondeduced_p=*/true))
10126 if (PRIMARY_TEMPLATE_P (decl
)
10127 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10128 (DECL_TEMPLATE_PARMS (decl
)),
10129 template_parm_outer_level
,
10130 &depth
, NULL
, /*include_nondeduced_p=*/true))
10132 tree ci
= get_constraints (decl
);
10134 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10135 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10136 &depth
, NULL
, /*nondeduced*/true))
10141 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10142 ill-formed translation unit, i.e. a variable or function that isn't
10143 usable in a constant expression. */
10146 neglectable_inst_p (tree d
)
10148 return (d
&& DECL_P (d
)
10149 && !undeduced_auto_decl (d
)
10150 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10151 : decl_maybe_constant_var_p (d
)));
10154 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10155 neglectable and instantiated from within an erroneous instantiation. */
10158 limit_bad_template_recursion (tree decl
)
10160 struct tinst_level
*lev
= current_tinst_level
;
10161 int errs
= errorcount
+ sorrycount
;
10162 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10165 for (; lev
; lev
= lev
->next
)
10166 if (neglectable_inst_p (lev
->maybe_get_node ()))
10169 return (lev
&& errs
> lev
->errors
);
10172 static int tinst_depth
;
10173 extern int max_tinst_depth
;
10176 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10178 /* We're starting to instantiate D; record the template instantiation context
10179 at LOC for diagnostics and to restore it later. */
10182 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10184 struct tinst_level
*new_level
;
10186 if (tinst_depth
>= max_tinst_depth
)
10188 /* Tell error.c not to try to instantiate any templates. */
10190 fatal_error (input_location
,
10191 "template instantiation depth exceeds maximum of %d"
10192 " (use -ftemplate-depth= to increase the maximum)",
10197 /* If the current instantiation caused problems, don't let it instantiate
10198 anything else. Do allow deduction substitution and decls usable in
10199 constant expressions. */
10200 if (!targs
&& limit_bad_template_recursion (tldcl
))
10203 /* When not -quiet, dump template instantiations other than functions, since
10204 announce_function will take care of those. */
10205 if (!quiet_flag
&& !targs
10206 && TREE_CODE (tldcl
) != TREE_LIST
10207 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10208 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10210 new_level
= tinst_level_freelist ().alloc ();
10211 new_level
->tldcl
= tldcl
;
10212 new_level
->targs
= targs
;
10213 new_level
->locus
= loc
;
10214 new_level
->errors
= errorcount
+ sorrycount
;
10215 new_level
->next
= NULL
;
10216 new_level
->refcount
= 0;
10217 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10218 set_refcount_ptr (current_tinst_level
, new_level
);
10221 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10222 depth_reached
= tinst_depth
;
10227 /* We're starting substitution of TMPL<ARGS>; record the template
10228 substitution context for diagnostics and to restore it later. */
10231 push_tinst_level (tree tmpl
, tree args
)
10233 return push_tinst_level_loc (tmpl
, args
, input_location
);
10236 /* We're starting to instantiate D; record INPUT_LOCATION and the
10237 template instantiation context for diagnostics and to restore it
10241 push_tinst_level (tree d
)
10243 return push_tinst_level_loc (d
, input_location
);
10246 /* Likewise, but record LOC as the program location. */
10249 push_tinst_level_loc (tree d
, location_t loc
)
10251 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10252 return push_tinst_level_loc (d
, NULL
, loc
);
10255 /* We're done instantiating this template; return to the instantiation
10259 pop_tinst_level (void)
10261 /* Restore the filename and line number stashed away when we started
10262 this instantiation. */
10263 input_location
= current_tinst_level
->locus
;
10264 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10268 /* We're instantiating a deferred template; restore the template
10269 instantiation context in which the instantiation was requested, which
10270 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10273 reopen_tinst_level (struct tinst_level
*level
)
10275 struct tinst_level
*t
;
10278 for (t
= level
; t
; t
= t
->next
)
10281 set_refcount_ptr (current_tinst_level
, level
);
10282 pop_tinst_level ();
10283 if (current_tinst_level
)
10284 current_tinst_level
->errors
= errorcount
+sorrycount
;
10285 return level
->maybe_get_node ();
10288 /* Returns the TINST_LEVEL which gives the original instantiation
10291 struct tinst_level
*
10292 outermost_tinst_level (void)
10294 struct tinst_level
*level
= current_tinst_level
;
10296 while (level
->next
)
10297 level
= level
->next
;
10301 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10302 vector of template arguments, as for tsubst.
10304 Returns an appropriate tsubst'd friend declaration. */
10307 tsubst_friend_function (tree decl
, tree args
)
10311 if (TREE_CODE (decl
) == FUNCTION_DECL
10312 && DECL_TEMPLATE_INSTANTIATION (decl
)
10313 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10314 /* This was a friend declared with an explicit template
10315 argument list, e.g.:
10317 friend void f<>(T);
10319 to indicate that f was a template instantiation, not a new
10320 function declaration. Now, we have to figure out what
10321 instantiation of what template. */
10323 tree template_id
, arglist
, fns
;
10326 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10328 /* Friend functions are looked up in the containing namespace scope.
10329 We must enter that scope, to avoid finding member functions of the
10330 current class with same name. */
10331 push_nested_namespace (ns
);
10332 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10333 tf_warning_or_error
, NULL_TREE
,
10334 /*integral_constant_expression_p=*/false);
10335 pop_nested_namespace (ns
);
10336 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10337 tf_warning_or_error
, NULL_TREE
);
10338 template_id
= lookup_template_function (fns
, arglist
);
10340 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10341 tmpl
= determine_specialization (template_id
, new_friend
,
10343 /*need_member_template=*/0,
10344 TREE_VEC_LENGTH (args
),
10346 return instantiate_template (tmpl
, new_args
, tf_error
);
10349 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10351 /* The NEW_FRIEND will look like an instantiation, to the
10352 compiler, but is not an instantiation from the point of view of
10353 the language. For example, we might have had:
10355 template <class T> struct S {
10356 template <class U> friend void f(T, U);
10359 Then, in S<int>, template <class U> void f(int, U) is not an
10360 instantiation of anything. */
10361 if (new_friend
== error_mark_node
)
10362 return error_mark_node
;
10364 DECL_USE_TEMPLATE (new_friend
) = 0;
10365 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10367 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10368 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10369 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10372 /* The mangled name for the NEW_FRIEND is incorrect. The function
10373 is not a template instantiation and should not be mangled like
10374 one. Therefore, we forget the mangling here; we'll recompute it
10375 later if we need it. */
10376 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10378 SET_DECL_RTL (new_friend
, NULL
);
10379 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10382 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10385 tree new_friend_template_info
;
10386 tree new_friend_result_template_info
;
10388 int new_friend_is_defn
;
10390 /* We must save some information from NEW_FRIEND before calling
10391 duplicate decls since that function will free NEW_FRIEND if
10393 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10394 new_friend_is_defn
=
10395 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10396 (template_for_substitution (new_friend
)))
10398 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10400 /* This declaration is a `primary' template. */
10401 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10403 new_friend_result_template_info
10404 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10407 new_friend_result_template_info
= NULL_TREE
;
10409 /* Inside pushdecl_namespace_level, we will push into the
10410 current namespace. However, the friend function should go
10411 into the namespace of the template. */
10412 ns
= decl_namespace_context (new_friend
);
10413 push_nested_namespace (ns
);
10414 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10415 pop_nested_namespace (ns
);
10417 if (old_decl
== error_mark_node
)
10418 return error_mark_node
;
10420 if (old_decl
!= new_friend
)
10422 /* This new friend declaration matched an existing
10423 declaration. For example, given:
10425 template <class T> void f(T);
10426 template <class U> class C {
10427 template <class T> friend void f(T) {}
10430 the friend declaration actually provides the definition
10431 of `f', once C has been instantiated for some type. So,
10432 old_decl will be the out-of-class template declaration,
10433 while new_friend is the in-class definition.
10435 But, if `f' was called before this point, the
10436 instantiation of `f' will have DECL_TI_ARGS corresponding
10437 to `T' but not to `U', references to which might appear
10438 in the definition of `f'. Previously, the most general
10439 template for an instantiation of `f' was the out-of-class
10440 version; now it is the in-class version. Therefore, we
10441 run through all specialization of `f', adding to their
10442 DECL_TI_ARGS appropriately. In particular, they need a
10443 new set of outer arguments, corresponding to the
10444 arguments for this class instantiation.
10446 The same situation can arise with something like this:
10448 friend void f(int);
10449 template <class T> class C {
10450 friend void f(T) {}
10453 when `C<int>' is instantiated. Now, `f(int)' is defined
10456 if (!new_friend_is_defn
)
10457 /* On the other hand, if the in-class declaration does
10458 *not* provide a definition, then we don't want to alter
10459 existing definitions. We can just leave everything
10464 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10465 tree new_args
= TI_ARGS (new_friend_template_info
);
10467 /* Overwrite whatever template info was there before, if
10468 any, with the new template information pertaining to
10469 the declaration. */
10470 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10472 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10474 /* We should have called reregister_specialization in
10475 duplicate_decls. */
10476 gcc_assert (retrieve_specialization (new_template
,
10480 /* Instantiate it if the global has already been used. */
10481 if (DECL_ODR_USED (old_decl
))
10482 instantiate_decl (old_decl
, /*defer_ok=*/true,
10483 /*expl_inst_class_mem_p=*/false);
10489 /* Indicate that the old function template is a partial
10491 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10492 = new_friend_result_template_info
;
10494 gcc_assert (new_template
10495 == most_general_template (new_template
));
10496 gcc_assert (new_template
!= old_decl
);
10498 /* Reassign any specializations already in the hash table
10499 to the new more general template, and add the
10500 additional template args. */
10501 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10503 t
= TREE_CHAIN (t
))
10505 tree spec
= TREE_VALUE (t
);
10508 elt
.tmpl
= old_decl
;
10509 elt
.args
= DECL_TI_ARGS (spec
);
10510 elt
.spec
= NULL_TREE
;
10512 decl_specializations
->remove_elt (&elt
);
10514 DECL_TI_ARGS (spec
)
10515 = add_outermost_template_args (new_args
,
10516 DECL_TI_ARGS (spec
));
10518 register_specialization
10519 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10522 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10526 /* The information from NEW_FRIEND has been merged into OLD_DECL
10527 by duplicate_decls. */
10528 new_friend
= old_decl
;
10533 tree context
= DECL_CONTEXT (new_friend
);
10537 template <class T> class C {
10538 template <class U> friend void C1<U>::f (); // case 1
10539 friend void C2<T>::f (); // case 2
10541 we only need to make sure CONTEXT is a complete type for
10542 case 2. To distinguish between the two cases, we note that
10543 CONTEXT of case 1 remains dependent type after tsubst while
10544 this isn't true for case 2. */
10545 ++processing_template_decl
;
10546 dependent_p
= dependent_type_p (context
);
10547 --processing_template_decl
;
10550 && !complete_type_or_else (context
, NULL_TREE
))
10551 return error_mark_node
;
10553 if (COMPLETE_TYPE_P (context
))
10555 tree fn
= new_friend
;
10556 /* do_friend adds the TEMPLATE_DECL for any member friend
10557 template even if it isn't a member template, i.e.
10558 template <class T> friend A<T>::f();
10559 Look through it in that case. */
10560 if (TREE_CODE (fn
) == TEMPLATE_DECL
10561 && !PRIMARY_TEMPLATE_P (fn
))
10562 fn
= DECL_TEMPLATE_RESULT (fn
);
10563 /* Check to see that the declaration is really present, and,
10564 possibly obtain an improved declaration. */
10565 fn
= check_classfn (context
, fn
, NULL_TREE
);
10575 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10576 template arguments, as for tsubst.
10578 Returns an appropriate tsubst'd friend type or error_mark_node on
10582 tsubst_friend_class (tree friend_tmpl
, tree args
)
10586 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10588 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10589 return TREE_TYPE (tmpl
);
10592 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10593 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10594 push_nested_namespace (context
);
10597 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
10598 push_nested_class (context
);
10601 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10602 /*non_class=*/false, /*block_p=*/false,
10603 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10605 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10607 /* The friend template has already been declared. Just
10608 check to see that the declarations match, and install any new
10609 default parameters. We must tsubst the default parameters,
10610 of course. We only need the innermost template parameters
10611 because that is all that redeclare_class_template will look
10613 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10614 > TMPL_ARGS_DEPTH (args
))
10616 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10617 args
, tf_warning_or_error
);
10618 location_t saved_input_location
= input_location
;
10619 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10620 tree cons
= get_constraints (tmpl
);
10621 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10622 input_location
= saved_input_location
;
10627 /* The friend template has not already been declared. In this
10628 case, the instantiation of the template class will cause the
10629 injection of this template into the namespace scope. */
10630 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10632 if (tmpl
!= error_mark_node
)
10634 /* The new TMPL is not an instantiation of anything, so we
10635 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10636 for the new type because that is supposed to be the
10637 corresponding template decl, i.e., TMPL. */
10638 DECL_USE_TEMPLATE (tmpl
) = 0;
10639 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10640 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10641 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10642 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10644 /* It is hidden. */
10645 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10646 DECL_ANTICIPATED (tmpl
)
10647 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10649 /* Inject this template into the enclosing namspace scope. */
10650 tmpl
= pushdecl_namespace_level (tmpl
, true);
10654 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10655 pop_nested_namespace (context
);
10657 pop_nested_class ();
10659 return TREE_TYPE (tmpl
);
10662 /* Returns zero if TYPE cannot be completed later due to circularity.
10663 Otherwise returns one. */
10666 can_complete_type_without_circularity (tree type
)
10668 if (type
== NULL_TREE
|| type
== error_mark_node
)
10670 else if (COMPLETE_TYPE_P (type
))
10672 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10673 return can_complete_type_without_circularity (TREE_TYPE (type
));
10674 else if (CLASS_TYPE_P (type
)
10675 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10681 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10682 tsubst_flags_t
, tree
);
10684 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10685 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10688 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10689 tsubst_flags_t complain
, tree in_decl
)
10691 gcc_assert (ATTR_IS_DEPENDENT (t
));
10693 tree val
= TREE_VALUE (t
);
10694 if (val
== NULL_TREE
)
10695 /* Nothing to do. */;
10696 else if ((flag_openmp
|| flag_openmp_simd
)
10697 && is_attribute_p ("omp declare simd",
10698 get_attribute_name (t
)))
10700 tree clauses
= TREE_VALUE (val
);
10701 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10702 complain
, in_decl
);
10703 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10704 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10705 tree parms
= DECL_ARGUMENTS (*decl_p
);
10707 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10709 val
= build_tree_list (NULL_TREE
, clauses
);
10713 /* If the first attribute argument is an identifier, don't
10714 pass it through tsubst. Attributes like mode, format,
10715 cleanup and several target specific attributes expect it
10717 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10720 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10721 /*integral_constant_expression_p=*/false);
10722 if (chain
!= TREE_CHAIN (val
))
10723 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10725 else if (PACK_EXPANSION_P (val
))
10727 /* An attribute pack expansion. */
10728 tree purp
= TREE_PURPOSE (t
);
10729 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10730 if (pack
== error_mark_node
)
10731 return error_mark_node
;
10732 int len
= TREE_VEC_LENGTH (pack
);
10733 tree list
= NULL_TREE
;
10735 for (int i
= 0; i
< len
; ++i
)
10737 tree elt
= TREE_VEC_ELT (pack
, i
);
10738 *q
= build_tree_list (purp
, elt
);
10739 q
= &TREE_CHAIN (*q
);
10744 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10745 /*integral_constant_expression_p=*/false);
10747 if (val
!= TREE_VALUE (t
))
10748 return build_tree_list (TREE_PURPOSE (t
), val
);
10752 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10753 unchanged or a new TREE_LIST chain. */
10756 tsubst_attributes (tree attributes
, tree args
,
10757 tsubst_flags_t complain
, tree in_decl
)
10759 tree last_dep
= NULL_TREE
;
10761 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10762 if (ATTR_IS_DEPENDENT (t
))
10765 attributes
= copy_list (attributes
);
10770 for (tree
*p
= &attributes
; *p
; )
10773 if (ATTR_IS_DEPENDENT (t
))
10775 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10780 p
= &TREE_CHAIN (*p
);
10781 *p
= TREE_CHAIN (t
);
10785 p
= &TREE_CHAIN (*p
);
10791 /* Apply any attributes which had to be deferred until instantiation
10792 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10793 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10796 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10797 tree args
, tsubst_flags_t complain
, tree in_decl
)
10799 tree last_dep
= NULL_TREE
;
10803 if (attributes
== NULL_TREE
)
10806 if (DECL_P (*decl_p
))
10808 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10810 p
= &DECL_ATTRIBUTES (*decl_p
);
10811 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10812 to our attributes parameter. */
10813 gcc_assert (*p
== attributes
);
10817 p
= &TYPE_ATTRIBUTES (*decl_p
);
10818 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10819 lookup_template_class_1, and should be preserved. */
10820 gcc_assert (*p
!= attributes
);
10822 p
= &TREE_CHAIN (*p
);
10825 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10826 if (ATTR_IS_DEPENDENT (t
))
10829 attributes
= copy_list (attributes
);
10836 tree late_attrs
= NULL_TREE
;
10837 tree
*q
= &late_attrs
;
10842 if (ATTR_IS_DEPENDENT (t
))
10844 *p
= TREE_CHAIN (t
);
10845 TREE_CHAIN (t
) = NULL_TREE
;
10846 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10848 q
= &TREE_CHAIN (*q
);
10851 p
= &TREE_CHAIN (t
);
10854 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10858 /* Perform (or defer) access check for typedefs that were referenced
10859 from within the template TMPL code.
10860 This is a subroutine of instantiate_decl and instantiate_class_template.
10861 TMPL is the template to consider and TARGS is the list of arguments of
10865 perform_typedefs_access_check (tree tmpl
, tree targs
)
10867 location_t saved_location
;
10869 qualified_typedef_usage_t
*iter
;
10872 || (!CLASS_TYPE_P (tmpl
)
10873 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10876 saved_location
= input_location
;
10877 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10879 tree type_decl
= iter
->typedef_decl
;
10880 tree type_scope
= iter
->context
;
10882 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10885 if (uses_template_parms (type_decl
))
10886 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10887 if (uses_template_parms (type_scope
))
10888 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10890 /* Make access check error messages point to the location
10891 of the use of the typedef. */
10892 input_location
= iter
->locus
;
10893 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10894 type_decl
, type_decl
,
10895 tf_warning_or_error
);
10897 input_location
= saved_location
;
10901 instantiate_class_template_1 (tree type
)
10903 tree templ
, args
, pattern
, t
, member
;
10907 unsigned int saved_maximum_field_alignment
;
10910 if (type
== error_mark_node
)
10911 return error_mark_node
;
10913 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10914 || uses_template_parms (type
))
10917 /* Figure out which template is being instantiated. */
10918 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10919 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10921 /* Mark the type as in the process of being defined. */
10922 TYPE_BEING_DEFINED (type
) = 1;
10924 /* We may be in the middle of deferred access check. Disable
10926 deferring_access_check_sentinel
acs (dk_no_deferred
);
10928 /* Determine what specialization of the original template to
10930 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10931 if (t
== error_mark_node
)
10932 return error_mark_node
;
10935 /* This TYPE is actually an instantiation of a partial
10936 specialization. We replace the innermost set of ARGS with
10937 the arguments appropriate for substitution. For example,
10940 template <class T> struct S {};
10941 template <class T> struct S<T*> {};
10943 and supposing that we are instantiating S<int*>, ARGS will
10944 presently be {int*} -- but we need {int}. */
10945 pattern
= TREE_TYPE (t
);
10946 args
= TREE_PURPOSE (t
);
10950 pattern
= TREE_TYPE (templ
);
10951 args
= CLASSTYPE_TI_ARGS (type
);
10954 /* If the template we're instantiating is incomplete, then clearly
10955 there's nothing we can do. */
10956 if (!COMPLETE_TYPE_P (pattern
))
10958 /* We can try again later. */
10959 TYPE_BEING_DEFINED (type
) = 0;
10963 /* If we've recursively instantiated too many templates, stop. */
10964 if (! push_tinst_level (type
))
10967 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10968 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10970 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10971 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10972 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10973 fn_context
= error_mark_node
;
10975 push_to_top_level ();
10978 cp_unevaluated_operand
= 0;
10979 c_inhibit_evaluation_warnings
= 0;
10981 /* Use #pragma pack from the template context. */
10982 saved_maximum_field_alignment
= maximum_field_alignment
;
10983 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10985 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10987 /* Set the input location to the most specialized template definition.
10988 This is needed if tsubsting causes an error. */
10989 typedecl
= TYPE_MAIN_DECL (pattern
);
10990 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10991 DECL_SOURCE_LOCATION (typedecl
);
10993 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10994 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10995 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10996 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10997 if (ANON_AGGR_TYPE_P (pattern
))
10998 SET_ANON_AGGR_TYPE_P (type
);
10999 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11001 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11002 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11003 /* Adjust visibility for template arguments. */
11004 determine_visibility (TYPE_MAIN_DECL (type
));
11006 if (CLASS_TYPE_P (type
))
11007 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11009 pbinfo
= TYPE_BINFO (pattern
);
11011 /* We should never instantiate a nested class before its enclosing
11012 class; we need to look up the nested class by name before we can
11013 instantiate it, and that lookup should instantiate the enclosing
11015 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11016 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11018 base_list
= NULL_TREE
;
11019 if (BINFO_N_BASE_BINFOS (pbinfo
))
11025 /* We must enter the scope containing the type, as that is where
11026 the accessibility of types named in dependent bases are
11028 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11030 /* Substitute into each of the bases to determine the actual
11032 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11035 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11036 tree expanded_bases
= NULL_TREE
;
11039 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11042 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11043 args
, tf_error
, NULL_TREE
);
11044 if (expanded_bases
== error_mark_node
)
11047 len
= TREE_VEC_LENGTH (expanded_bases
);
11050 for (idx
= 0; idx
< len
; idx
++)
11052 if (expanded_bases
)
11053 /* Extract the already-expanded base class. */
11054 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11056 /* Substitute to figure out the base class. */
11057 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11060 if (base
== error_mark_node
)
11063 base_list
= tree_cons (access
, base
, base_list
);
11064 if (BINFO_VIRTUAL_P (pbase_binfo
))
11065 TREE_TYPE (base_list
) = integer_type_node
;
11069 /* The list is now in reverse order; correct that. */
11070 base_list
= nreverse (base_list
);
11073 pop_scope (pushed_scope
);
11075 /* Now call xref_basetypes to set up all the base-class
11077 xref_basetypes (type
, base_list
);
11079 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11080 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11081 args
, tf_error
, NULL_TREE
);
11082 fixup_attribute_variants (type
);
11084 /* Now that our base classes are set up, enter the scope of the
11085 class, so that name lookups into base classes, etc. will work
11086 correctly. This is precisely analogous to what we do in
11087 begin_class_definition when defining an ordinary non-template
11088 class, except we also need to push the enclosing classes. */
11089 push_nested_class (type
);
11091 /* Now members are processed in the order of declaration. */
11092 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11093 member
; member
= TREE_CHAIN (member
))
11095 tree t
= TREE_VALUE (member
);
11097 if (TREE_PURPOSE (member
))
11101 if (LAMBDA_TYPE_P (t
))
11102 /* A closure type for a lambda in an NSDMI or default argument.
11103 Ignore it; it will be regenerated when needed. */
11106 /* Build new CLASSTYPE_NESTED_UTDS. */
11109 bool class_template_p
;
11111 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11112 && TYPE_LANG_SPECIFIC (t
)
11113 && CLASSTYPE_IS_TEMPLATE (t
));
11114 /* If the member is a class template, then -- even after
11115 substitution -- there may be dependent types in the
11116 template argument list for the class. We increment
11117 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11118 that function will assume that no types are dependent
11119 when outside of a template. */
11120 if (class_template_p
)
11121 ++processing_template_decl
;
11122 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11123 if (class_template_p
)
11124 --processing_template_decl
;
11125 if (newtag
== error_mark_node
)
11128 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11130 tree name
= TYPE_IDENTIFIER (t
);
11132 if (class_template_p
)
11133 /* Unfortunately, lookup_template_class sets
11134 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11135 instantiation (i.e., for the type of a member
11136 template class nested within a template class.)
11137 This behavior is required for
11138 maybe_process_partial_specialization to work
11139 correctly, but is not accurate in this case;
11140 the TAG is not an instantiation of anything.
11141 (The corresponding TEMPLATE_DECL is an
11142 instantiation, but the TYPE is not.) */
11143 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11145 /* Now, we call pushtag to put this NEWTAG into the scope of
11146 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11147 pushtag calling push_template_decl. We don't have to do
11148 this for enums because it will already have been done in
11151 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11152 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11155 else if (DECL_DECLARES_FUNCTION_P (t
))
11159 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11160 ++processing_template_decl
;
11161 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11162 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11163 --processing_template_decl
;
11164 set_current_access_from_decl (r
);
11165 finish_member_declaration (r
);
11166 /* Instantiate members marked with attribute used. */
11167 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11169 if (TREE_CODE (r
) == FUNCTION_DECL
11170 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11171 cp_check_omp_declare_reduction (r
);
11173 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11174 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11175 /* A closure type for a lambda in an NSDMI or default argument.
11176 Ignore it; it will be regenerated when needed. */;
11179 /* Build new TYPE_FIELDS. */
11180 if (TREE_CODE (t
) == STATIC_ASSERT
)
11184 ++c_inhibit_evaluation_warnings
;
11186 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11187 tf_warning_or_error
, NULL_TREE
,
11188 /*integral_constant_expression_p=*/true);
11189 --c_inhibit_evaluation_warnings
;
11191 finish_static_assert (condition
,
11192 STATIC_ASSERT_MESSAGE (t
),
11193 STATIC_ASSERT_SOURCE_LOCATION (t
),
11194 /*member_p=*/true);
11196 else if (TREE_CODE (t
) != CONST_DECL
)
11199 tree vec
= NULL_TREE
;
11202 /* The file and line for this declaration, to
11203 assist in error message reporting. Since we
11204 called push_tinst_level above, we don't need to
11206 input_location
= DECL_SOURCE_LOCATION (t
);
11208 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11209 ++processing_template_decl
;
11210 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11211 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11212 --processing_template_decl
;
11214 if (TREE_CODE (r
) == TREE_VEC
)
11216 /* A capture pack became multiple fields. */
11218 len
= TREE_VEC_LENGTH (vec
);
11221 for (int i
= 0; i
< len
; ++i
)
11224 r
= TREE_VEC_ELT (vec
, i
);
11229 [t]he initialization (and any associated
11230 side-effects) of a static data member does
11231 not occur unless the static data member is
11232 itself used in a way that requires the
11233 definition of the static data member to
11236 Therefore, we do not substitute into the
11237 initialized for the static data member here. */
11238 finish_static_data_member_decl
11240 /*init=*/NULL_TREE
,
11241 /*init_const_expr_p=*/false,
11242 /*asmspec_tree=*/NULL_TREE
,
11244 /* Instantiate members marked with attribute used. */
11245 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11248 else if (TREE_CODE (r
) == FIELD_DECL
)
11250 /* Determine whether R has a valid type and can be
11251 completed later. If R is invalid, then its type
11252 is replaced by error_mark_node. */
11253 tree rtype
= TREE_TYPE (r
);
11254 if (can_complete_type_without_circularity (rtype
))
11255 complete_type (rtype
);
11257 if (!complete_or_array_type_p (rtype
))
11259 /* If R's type couldn't be completed and
11260 it isn't a flexible array member (whose
11261 type is incomplete by definition) give
11263 cxx_incomplete_type_error (r
, rtype
);
11264 TREE_TYPE (r
) = error_mark_node
;
11266 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11267 && TYPE_DOMAIN (rtype
) == NULL_TREE
11268 && (TREE_CODE (type
) == UNION_TYPE
11269 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11271 error ("flexible array member %qD in union", r
);
11272 TREE_TYPE (r
) = error_mark_node
;
11276 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11277 such a thing will already have been added to the field
11278 list by tsubst_enum in finish_member_declaration in the
11279 CLASSTYPE_NESTED_UTDS case above. */
11280 if (!(TREE_CODE (r
) == TYPE_DECL
11281 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11282 && DECL_ARTIFICIAL (r
)))
11284 set_current_access_from_decl (r
);
11285 finish_member_declaration (r
);
11293 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11294 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11296 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11298 tree friend_type
= t
;
11299 bool adjust_processing_template_decl
= false;
11301 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11303 /* template <class T> friend class C; */
11304 friend_type
= tsubst_friend_class (friend_type
, args
);
11305 adjust_processing_template_decl
= true;
11307 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11309 /* template <class T> friend class C::D; */
11310 friend_type
= tsubst (friend_type
, args
,
11311 tf_warning_or_error
, NULL_TREE
);
11312 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11313 friend_type
= TREE_TYPE (friend_type
);
11314 adjust_processing_template_decl
= true;
11316 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11317 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11319 /* This could be either
11323 when dependent_type_p is false or
11325 template <class U> friend class T::C;
11328 /* Bump processing_template_decl in case this is something like
11329 template <class T> friend struct A<T>::B. */
11330 ++processing_template_decl
;
11331 friend_type
= tsubst (friend_type
, args
,
11332 tf_warning_or_error
, NULL_TREE
);
11333 if (dependent_type_p (friend_type
))
11334 adjust_processing_template_decl
= true;
11335 --processing_template_decl
;
11337 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11338 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11339 && TYPE_HIDDEN_P (friend_type
))
11343 where C hasn't been declared yet. Let's lookup name
11344 from namespace scope directly, bypassing any name that
11345 come from dependent base class. */
11346 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11348 /* The call to xref_tag_from_type does injection for friend
11350 push_nested_namespace (ns
);
11352 xref_tag_from_type (friend_type
, NULL_TREE
,
11353 /*tag_scope=*/ts_current
);
11354 pop_nested_namespace (ns
);
11356 else if (uses_template_parms (friend_type
))
11357 /* friend class C<T>; */
11358 friend_type
= tsubst (friend_type
, args
,
11359 tf_warning_or_error
, NULL_TREE
);
11364 where C is already declared or
11366 friend class C<int>;
11368 We don't have to do anything in these cases. */
11370 if (adjust_processing_template_decl
)
11371 /* Trick make_friend_class into realizing that the friend
11372 we're adding is a template, not an ordinary class. It's
11373 important that we use make_friend_class since it will
11374 perform some error-checking and output cross-reference
11376 ++processing_template_decl
;
11378 if (friend_type
!= error_mark_node
)
11379 make_friend_class (type
, friend_type
, /*complain=*/false);
11381 if (adjust_processing_template_decl
)
11382 --processing_template_decl
;
11386 /* Build new DECL_FRIENDLIST. */
11389 /* The file and line for this declaration, to
11390 assist in error message reporting. Since we
11391 called push_tinst_level above, we don't need to
11393 input_location
= DECL_SOURCE_LOCATION (t
);
11395 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11397 ++processing_template_decl
;
11398 push_deferring_access_checks (dk_no_check
);
11401 r
= tsubst_friend_function (t
, args
);
11402 add_friend (type
, r
, /*complain=*/false);
11403 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11405 pop_deferring_access_checks ();
11406 --processing_template_decl
;
11414 /* Restore these before substituting into the lambda capture
11416 cp_unevaluated_operand
= saved_unevaluated_operand
;
11417 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11420 /* Set the file and line number information to whatever is given for
11421 the class itself. This puts error messages involving generated
11422 implicit functions at a predictable point, and the same point
11423 that would be used for non-template classes. */
11424 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11426 unreverse_member_declarations (type
);
11427 finish_struct_1 (type
);
11428 TYPE_BEING_DEFINED (type
) = 0;
11430 /* We don't instantiate default arguments for member functions. 14.7.1:
11432 The implicit instantiation of a class template specialization causes
11433 the implicit instantiation of the declarations, but not of the
11434 definitions or default arguments, of the class member functions,
11435 member classes, static data members and member templates.... */
11437 /* Some typedefs referenced from within the template code need to be access
11438 checked at template instantiation time, i.e now. These types were
11439 added to the template at parsing time. Let's get those and perform
11440 the access checks then. */
11441 perform_typedefs_access_check (pattern
, args
);
11442 perform_deferred_access_checks (tf_warning_or_error
);
11443 pop_nested_class ();
11444 maximum_field_alignment
= saved_maximum_field_alignment
;
11446 pop_from_top_level ();
11447 pop_tinst_level ();
11449 /* The vtable for a template class can be emitted in any translation
11450 unit in which the class is instantiated. When there is no key
11451 method, however, finish_struct_1 will already have added TYPE to
11452 the keyed_classes. */
11453 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11454 vec_safe_push (keyed_classes
, type
);
11459 /* Wrapper for instantiate_class_template_1. */
11462 instantiate_class_template (tree type
)
11465 timevar_push (TV_TEMPLATE_INST
);
11466 ret
= instantiate_class_template_1 (type
);
11467 timevar_pop (TV_TEMPLATE_INST
);
11472 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11478 else if (TYPE_P (t
))
11479 r
= tsubst (t
, args
, complain
, in_decl
);
11482 if (!(complain
& tf_warning
))
11483 ++c_inhibit_evaluation_warnings
;
11484 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11485 /*integral_constant_expression_p=*/true);
11486 if (!(complain
& tf_warning
))
11487 --c_inhibit_evaluation_warnings
;
11492 /* Given a function parameter pack TMPL_PARM and some function parameters
11493 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11494 and set *SPEC_P to point at the next point in the list. */
11497 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11499 /* Collect all of the extra "packed" parameters into an
11502 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11503 tree spec_parm
= *spec_p
;
11506 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11508 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11511 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11512 parmvec
= make_tree_vec (len
);
11513 spec_parm
= *spec_p
;
11514 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11516 tree elt
= spec_parm
;
11517 if (DECL_PACK_P (elt
))
11518 elt
= make_pack_expansion (elt
);
11519 TREE_VEC_ELT (parmvec
, i
) = elt
;
11522 /* Build the argument packs. */
11523 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11524 *spec_p
= spec_parm
;
11529 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11530 NONTYPE_ARGUMENT_PACK. */
11533 make_fnparm_pack (tree spec_parm
)
11535 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11538 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11539 pack expansion with no extra args, 2 if it has extra args, or 0
11540 if it is not a pack expansion. */
11543 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11545 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11546 if (i
>= TREE_VEC_LENGTH (vec
))
11548 tree elt
= TREE_VEC_ELT (vec
, i
);
11550 /* A decl pack is itself an expansion. */
11551 elt
= TREE_TYPE (elt
);
11552 if (!PACK_EXPANSION_P (elt
))
11554 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11560 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11563 make_argument_pack_select (tree arg_pack
, unsigned index
)
11565 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11567 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11568 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11573 /* This is a subroutine of tsubst_pack_expansion.
11575 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11576 mechanism to store the (non complete list of) arguments of the
11577 substitution and return a non substituted pack expansion, in order
11578 to wait for when we have enough arguments to really perform the
11582 use_pack_expansion_extra_args_p (tree parm_packs
,
11584 bool has_empty_arg
)
11586 /* If one pack has an expansion and another pack has a normal
11587 argument or if one pack has an empty argument and an another
11588 one hasn't then tsubst_pack_expansion cannot perform the
11589 substitution and need to fall back on the
11590 PACK_EXPANSION_EXTRA mechanism. */
11591 if (parm_packs
== NULL_TREE
)
11593 else if (has_empty_arg
)
11596 bool has_expansion_arg
= false;
11597 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11599 bool has_non_expansion_arg
= false;
11600 for (tree parm_pack
= parm_packs
;
11602 parm_pack
= TREE_CHAIN (parm_pack
))
11604 tree arg
= TREE_VALUE (parm_pack
);
11606 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11608 /* We can't substitute a pack expansion with extra args into
11612 has_expansion_arg
= true;
11614 has_non_expansion_arg
= true;
11617 if (has_expansion_arg
&& has_non_expansion_arg
)
11623 /* [temp.variadic]/6 says that:
11625 The instantiation of a pack expansion [...]
11626 produces a list E1,E2, ..., En, where N is the number of elements
11627 in the pack expansion parameters.
11629 This subroutine of tsubst_pack_expansion produces one of these Ei.
11631 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11632 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11633 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11634 INDEX is the index 'i' of the element Ei to produce. ARGS,
11635 COMPLAIN, and IN_DECL are the same parameters as for the
11636 tsubst_pack_expansion function.
11638 The function returns the resulting Ei upon successful completion,
11639 or error_mark_node.
11641 Note that this function possibly modifies the ARGS parameter, so
11642 it's the responsibility of the caller to restore it. */
11645 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11648 tree args
/* This parm gets
11650 tsubst_flags_t complain
,
11654 bool ith_elem_is_expansion
= false;
11656 /* For each parameter pack, change the substitution of the parameter
11657 pack to the ith argument in its argument pack, then expand the
11659 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11661 tree parm
= TREE_PURPOSE (pack
);
11662 tree arg_pack
= TREE_VALUE (pack
);
11663 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11665 ith_elem_is_expansion
|=
11666 argument_pack_element_is_expansion_p (arg_pack
, index
);
11668 /* Select the Ith argument from the pack. */
11669 if (TREE_CODE (parm
) == PARM_DECL
11671 || TREE_CODE (parm
) == FIELD_DECL
)
11675 aps
= make_argument_pack_select (arg_pack
, index
);
11676 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11677 return error_mark_node
;
11678 register_local_specialization (aps
, parm
);
11681 aps
= retrieve_local_specialization (parm
);
11686 template_parm_level_and_index (parm
, &level
, &idx
);
11690 aps
= make_argument_pack_select (arg_pack
, index
);
11691 /* Update the corresponding argument. */
11692 TMPL_ARG (args
, level
, idx
) = aps
;
11695 /* Re-use the ARGUMENT_PACK_SELECT. */
11696 aps
= TMPL_ARG (args
, level
, idx
);
11698 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11701 /* Substitute into the PATTERN with the (possibly altered)
11703 if (pattern
== in_decl
)
11704 /* Expanding a fixed parameter pack from
11705 coerce_template_parameter_pack. */
11706 t
= tsubst_decl (pattern
, args
, complain
);
11707 else if (pattern
== error_mark_node
)
11708 t
= error_mark_node
;
11709 else if (constraint_p (pattern
))
11711 if (processing_template_decl
)
11712 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11714 t
= (constraints_satisfied_p (pattern
, args
)
11715 ? boolean_true_node
: boolean_false_node
);
11717 else if (!TYPE_P (pattern
))
11718 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11719 /*integral_constant_expression_p=*/false);
11721 t
= tsubst (pattern
, args
, complain
, in_decl
);
11723 /* If the Ith argument pack element is a pack expansion, then
11724 the Ith element resulting from the substituting is going to
11725 be a pack expansion as well. */
11726 if (ith_elem_is_expansion
)
11727 t
= make_pack_expansion (t
, complain
);
11732 /* When the unexpanded parameter pack in a fold expression expands to an empty
11733 sequence, the value of the expression is as follows; the program is
11734 ill-formed if the operator is not listed in this table.
11741 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11743 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11744 if (!FOLD_EXPR_MODIFY_P (t
))
11747 case TRUTH_ANDIF_EXPR
:
11748 return boolean_true_node
;
11749 case TRUTH_ORIF_EXPR
:
11750 return boolean_false_node
;
11751 case COMPOUND_EXPR
:
11757 if (complain
& tf_error
)
11758 error_at (location_of (t
),
11759 "fold of empty expansion over %O", code
);
11760 return error_mark_node
;
11763 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11764 form an expression that combines the two terms using the
11768 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11770 tree op
= FOLD_EXPR_OP (t
);
11771 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11773 // Handle compound assignment operators.
11774 if (FOLD_EXPR_MODIFY_P (t
))
11775 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11779 case COMPOUND_EXPR
:
11780 return build_x_compound_expr (input_location
, left
, right
, complain
);
11782 return build_m_component_ref (left
, right
, complain
);
11784 return build_x_binary_op (input_location
, code
,
11785 left
, TREE_CODE (left
),
11786 right
, TREE_CODE (right
),
11792 /* Substitute ARGS into the pack of a fold expression T. */
11795 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11797 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11800 /* Substitute ARGS into the pack of a fold expression T. */
11803 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11805 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11808 /* Expand a PACK of arguments into a grouped as left fold.
11809 Given a pack containing elements A0, A1, ..., An and an
11810 operator @, this builds the expression:
11812 ((A0 @ A1) @ A2) ... @ An
11814 Note that PACK must not be empty.
11816 The operator is defined by the original fold expression T. */
11819 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11821 tree left
= TREE_VEC_ELT (pack
, 0);
11822 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11824 tree right
= TREE_VEC_ELT (pack
, i
);
11825 left
= fold_expression (t
, left
, right
, complain
);
11830 /* Substitute into a unary left fold expression. */
11833 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11836 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11837 if (pack
== error_mark_node
)
11838 return error_mark_node
;
11839 if (PACK_EXPANSION_P (pack
))
11841 tree r
= copy_node (t
);
11842 FOLD_EXPR_PACK (r
) = pack
;
11845 if (TREE_VEC_LENGTH (pack
) == 0)
11846 return expand_empty_fold (t
, complain
);
11848 return expand_left_fold (t
, pack
, complain
);
11851 /* Substitute into a binary left fold expression.
11853 Do ths by building a single (non-empty) vector of argumnts and
11854 building the expression from those elements. */
11857 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11860 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11861 if (pack
== error_mark_node
)
11862 return error_mark_node
;
11863 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11864 if (init
== error_mark_node
)
11865 return error_mark_node
;
11867 if (PACK_EXPANSION_P (pack
))
11869 tree r
= copy_node (t
);
11870 FOLD_EXPR_PACK (r
) = pack
;
11871 FOLD_EXPR_INIT (r
) = init
;
11875 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11876 TREE_VEC_ELT (vec
, 0) = init
;
11877 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11878 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11880 return expand_left_fold (t
, vec
, complain
);
11883 /* Expand a PACK of arguments into a grouped as right fold.
11884 Given a pack containing elementns A0, A1, ..., and an
11885 operator @, this builds the expression:
11887 A0@ ... (An-2 @ (An-1 @ An))
11889 Note that PACK must not be empty.
11891 The operator is defined by the original fold expression T. */
11894 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11896 // Build the expression.
11897 int n
= TREE_VEC_LENGTH (pack
);
11898 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11899 for (--n
; n
!= 0; --n
)
11901 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11902 right
= fold_expression (t
, left
, right
, complain
);
11907 /* Substitute into a unary right fold expression. */
11910 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11913 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11914 if (pack
== error_mark_node
)
11915 return error_mark_node
;
11916 if (PACK_EXPANSION_P (pack
))
11918 tree r
= copy_node (t
);
11919 FOLD_EXPR_PACK (r
) = pack
;
11922 if (TREE_VEC_LENGTH (pack
) == 0)
11923 return expand_empty_fold (t
, complain
);
11925 return expand_right_fold (t
, pack
, complain
);
11928 /* Substitute into a binary right fold expression.
11930 Do ths by building a single (non-empty) vector of arguments and
11931 building the expression from those elements. */
11934 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11937 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11938 if (pack
== error_mark_node
)
11939 return error_mark_node
;
11940 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11941 if (init
== error_mark_node
)
11942 return error_mark_node
;
11944 if (PACK_EXPANSION_P (pack
))
11946 tree r
= copy_node (t
);
11947 FOLD_EXPR_PACK (r
) = pack
;
11948 FOLD_EXPR_INIT (r
) = init
;
11952 int n
= TREE_VEC_LENGTH (pack
);
11953 tree vec
= make_tree_vec (n
+ 1);
11954 for (int i
= 0; i
< n
; ++i
)
11955 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11956 TREE_VEC_ELT (vec
, n
) = init
;
11958 return expand_right_fold (t
, vec
, complain
);
11961 /* Walk through the pattern of a pack expansion, adding everything in
11962 local_specializations to a list. */
11966 hash_set
<tree
> internal
;
11968 tsubst_flags_t complain
;
11970 el_data (tsubst_flags_t c
)
11971 : extra (NULL_TREE
), complain (c
) {}
11974 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
11976 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
11977 tree
*extra
= &data
.extra
;
11978 tsubst_flags_t complain
= data
.complain
;
11980 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
11981 /* Remember local typedefs (85214). */
11982 tp
= &TYPE_NAME (*tp
);
11984 if (TREE_CODE (*tp
) == DECL_EXPR
)
11985 data
.internal
.add (DECL_EXPR_DECL (*tp
));
11986 else if (tree spec
= retrieve_local_specialization (*tp
))
11988 if (data
.internal
.contains (*tp
))
11989 /* Don't mess with variables declared within the pattern. */
11991 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
11993 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11994 tree args
= ARGUMENT_PACK_ARGS (spec
);
11995 if (TREE_VEC_LENGTH (args
) == 1)
11997 tree elt
= TREE_VEC_ELT (args
, 0);
11998 if (PACK_EXPANSION_P (elt
))
11999 elt
= PACK_EXPANSION_PATTERN (elt
);
12000 if (DECL_PACK_P (elt
))
12003 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12005 /* Handle lambda capture here, since we aren't doing any
12006 substitution now, and so tsubst_copy won't call
12007 process_outer_var_ref. */
12008 tree args
= ARGUMENT_PACK_ARGS (spec
);
12009 int len
= TREE_VEC_LENGTH (args
);
12010 for (int i
= 0; i
< len
; ++i
)
12012 tree arg
= TREE_VEC_ELT (args
, i
);
12014 if (outer_automatic_var_p (arg
))
12015 carg
= process_outer_var_ref (arg
, complain
);
12018 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12022 spec
= copy_node (spec
);
12023 args
= copy_node (args
);
12024 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12025 register_local_specialization (spec
, *tp
);
12027 TREE_VEC_ELT (args
, i
) = carg
;
12032 if (outer_automatic_var_p (spec
))
12033 spec
= process_outer_var_ref (spec
, complain
);
12034 *extra
= tree_cons (*tp
, spec
, *extra
);
12039 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12041 el_data
data (complain
);
12042 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12046 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12047 for use in PACK_EXPANSION_EXTRA_ARGS. */
12050 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12053 if (local_specializations
)
12054 if (tree locals
= extract_local_specs (pattern
, complain
))
12055 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12059 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12060 normal template args to ARGS. */
12063 add_extra_args (tree extra
, tree args
)
12065 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12067 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12069 /* The partial instantiation involved local declarations collected in
12070 extract_local_specs; map from the general template to our local
12072 tree gen
= TREE_PURPOSE (elt
);
12073 tree inst
= TREE_VALUE (elt
);
12075 if (tree local
= retrieve_local_specialization (inst
))
12077 /* else inst is already a full instantiation of the pack. */
12078 register_local_specialization (inst
, gen
);
12080 gcc_assert (!TREE_PURPOSE (extra
));
12081 extra
= TREE_VALUE (extra
);
12083 return add_to_template_args (extra
, args
);
12086 /* Substitute ARGS into T, which is an pack expansion
12087 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12088 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12089 (if only a partial substitution could be performed) or
12090 ERROR_MARK_NODE if there was an error. */
12092 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12096 tree pack
, packs
= NULL_TREE
;
12097 bool unsubstituted_packs
= false;
12098 bool unsubstituted_fn_pack
= false;
12101 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12102 bool need_local_specializations
= false;
12105 gcc_assert (PACK_EXPANSION_P (t
));
12106 pattern
= PACK_EXPANSION_PATTERN (t
);
12108 /* Add in any args remembered from an earlier partial instantiation. */
12109 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12111 levels
= TMPL_ARGS_DEPTH (args
);
12113 /* Determine the argument packs that will instantiate the parameter
12114 packs used in the expansion expression. While we're at it,
12115 compute the number of arguments to be expanded and make sure it
12117 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12118 pack
= TREE_CHAIN (pack
))
12120 tree parm_pack
= TREE_VALUE (pack
);
12121 tree arg_pack
= NULL_TREE
;
12122 tree orig_arg
= NULL_TREE
;
12125 if (TREE_CODE (parm_pack
) == BASES
)
12127 gcc_assert (parm_pack
== pattern
);
12128 if (BASES_DIRECT (parm_pack
))
12129 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12134 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12135 args
, complain
, in_decl
,
12138 else if (builtin_pack_call_p (parm_pack
))
12140 if (parm_pack
!= pattern
)
12142 if (complain
& tf_error
)
12143 sorry ("%qE is not the entire pattern of the pack expansion",
12145 return error_mark_node
;
12147 return expand_builtin_pack_call (parm_pack
, args
,
12148 complain
, in_decl
);
12150 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12152 /* We know we have correct local_specializations if this
12153 expansion is at function scope, or if we're dealing with a
12154 local parameter in a requires expression; for the latter,
12155 tsubst_requires_expr set it up appropriately. */
12156 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12157 arg_pack
= retrieve_local_specialization (parm_pack
);
12159 /* We can't rely on local_specializations for a parameter
12160 name used later in a function declaration (such as in a
12161 late-specified return type). Even if it exists, it might
12162 have the wrong value for a recursive call. */
12163 need_local_specializations
= true;
12167 /* This parameter pack was used in an unevaluated context. Just
12168 make a dummy decl, since it's only used for its type. */
12169 ++cp_unevaluated_operand
;
12170 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12171 --cp_unevaluated_operand
;
12172 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12173 /* Partial instantiation of the parm_pack, we can't build
12174 up an argument pack yet. */
12175 arg_pack
= NULL_TREE
;
12177 arg_pack
= make_fnparm_pack (arg_pack
);
12179 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12180 /* This argument pack isn't fully instantiated yet. We set this
12181 flag rather than clear arg_pack because we do want to do the
12182 optimization below, and we don't want to substitute directly
12183 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12184 where it isn't expected). */
12185 unsubstituted_fn_pack
= true;
12187 else if (is_capture_proxy (parm_pack
))
12189 arg_pack
= retrieve_local_specialization (parm_pack
);
12190 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12191 unsubstituted_fn_pack
= true;
12196 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12198 if (level
<= levels
)
12199 arg_pack
= TMPL_ARG (args
, level
, idx
);
12202 orig_arg
= arg_pack
;
12203 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12204 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12206 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12207 /* This can only happen if we forget to expand an argument
12208 pack somewhere else. Just return an error, silently. */
12210 result
= make_tree_vec (1);
12211 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12218 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12220 /* Don't bother trying to do a partial substitution with
12221 incomplete packs; we'll try again after deduction. */
12222 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12227 else if (len
!= my_len
12228 && !unsubstituted_fn_pack
)
12230 if (!(complain
& tf_error
))
12231 /* Fail quietly. */;
12232 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12233 error ("mismatched argument pack lengths while expanding %qT",
12236 error ("mismatched argument pack lengths while expanding %qE",
12238 return error_mark_node
;
12241 /* Keep track of the parameter packs and their corresponding
12243 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12244 TREE_TYPE (packs
) = orig_arg
;
12248 /* We can't substitute for this parameter pack. We use a flag as
12249 well as the missing_level counter because function parameter
12250 packs don't have a level. */
12251 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12252 unsubstituted_packs
= true;
12256 /* If the expansion is just T..., return the matching argument pack, unless
12257 we need to call convert_from_reference on all the elements. This is an
12258 important optimization; see c++/68422. */
12259 if (!unsubstituted_packs
12260 && TREE_PURPOSE (packs
) == pattern
)
12262 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12264 /* If the argument pack is a single pack expansion, pull it out. */
12265 if (TREE_VEC_LENGTH (args
) == 1
12266 && pack_expansion_args_count (args
))
12267 return TREE_VEC_ELT (args
, 0);
12269 /* Types need no adjustment, nor does sizeof..., and if we still have
12270 some pack expansion args we won't do anything yet. */
12271 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12272 || PACK_EXPANSION_SIZEOF_P (t
)
12273 || pack_expansion_args_count (args
))
12275 /* Also optimize expression pack expansions if we can tell that the
12276 elements won't have reference type. */
12277 tree type
= TREE_TYPE (pattern
);
12278 if (type
&& !TYPE_REF_P (type
)
12279 && !PACK_EXPANSION_P (type
)
12280 && !WILDCARD_TYPE_P (type
))
12282 /* Otherwise use the normal path so we get convert_from_reference. */
12285 /* We cannot expand this expansion expression, because we don't have
12286 all of the argument packs we need. */
12287 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12289 /* We got some full packs, but we can't substitute them in until we
12290 have values for all the packs. So remember these until then. */
12292 t
= make_pack_expansion (pattern
, complain
);
12293 PACK_EXPANSION_EXTRA_ARGS (t
)
12294 = build_extra_args (pattern
, args
, complain
);
12297 else if (unsubstituted_packs
)
12299 /* There were no real arguments, we're just replacing a parameter
12300 pack with another version of itself. Substitute into the
12301 pattern and return a PACK_EXPANSION_*. The caller will need to
12303 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12304 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12305 /*integral_constant_expression_p=*/false);
12307 t
= tsubst (pattern
, args
, complain
, in_decl
);
12308 t
= make_pack_expansion (t
, complain
);
12312 gcc_assert (len
>= 0);
12314 if (need_local_specializations
)
12316 /* We're in a late-specified return type, so create our own local
12317 specializations map; the current map is either NULL or (in the
12318 case of recursive unification) might have bindings that we don't
12319 want to use or alter. */
12320 saved_local_specializations
= local_specializations
;
12321 local_specializations
= new hash_map
<tree
, tree
>;
12324 /* For each argument in each argument pack, substitute into the
12326 result
= make_tree_vec (len
);
12327 tree elem_args
= copy_template_args (args
);
12328 for (i
= 0; i
< len
; ++i
)
12330 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12332 elem_args
, complain
,
12334 TREE_VEC_ELT (result
, i
) = t
;
12335 if (t
== error_mark_node
)
12337 result
= error_mark_node
;
12342 /* Update ARGS to restore the substitution from parameter packs to
12343 their argument packs. */
12344 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12346 tree parm
= TREE_PURPOSE (pack
);
12348 if (TREE_CODE (parm
) == PARM_DECL
12350 || TREE_CODE (parm
) == FIELD_DECL
)
12351 register_local_specialization (TREE_TYPE (pack
), parm
);
12356 if (TREE_VALUE (pack
) == NULL_TREE
)
12359 template_parm_level_and_index (parm
, &level
, &idx
);
12361 /* Update the corresponding argument. */
12362 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12363 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12366 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12370 if (need_local_specializations
)
12372 delete local_specializations
;
12373 local_specializations
= saved_local_specializations
;
12376 /* If the dependent pack arguments were such that we end up with only a
12377 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12378 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12379 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12380 return TREE_VEC_ELT (result
, 0);
12385 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12386 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12387 parameter packs; all parms generated from a function parameter pack will
12388 have the same DECL_PARM_INDEX. */
12391 get_pattern_parm (tree parm
, tree tmpl
)
12393 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12396 if (DECL_ARTIFICIAL (parm
))
12398 for (patparm
= DECL_ARGUMENTS (pattern
);
12399 patparm
; patparm
= DECL_CHAIN (patparm
))
12400 if (DECL_ARTIFICIAL (patparm
)
12401 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12406 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12407 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12408 gcc_assert (DECL_PARM_INDEX (patparm
)
12409 == DECL_PARM_INDEX (parm
));
12415 /* Make an argument pack out of the TREE_VEC VEC. */
12418 make_argument_pack (tree vec
)
12421 tree elt
= TREE_VEC_ELT (vec
, 0);
12423 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12426 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12427 TREE_CONSTANT (pack
) = 1;
12429 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12433 /* Return an exact copy of template args T that can be modified
12437 copy_template_args (tree t
)
12439 if (t
== error_mark_node
)
12442 int len
= TREE_VEC_LENGTH (t
);
12443 tree new_vec
= make_tree_vec (len
);
12445 for (int i
= 0; i
< len
; ++i
)
12447 tree elt
= TREE_VEC_ELT (t
, i
);
12448 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12449 elt
= copy_template_args (elt
);
12450 TREE_VEC_ELT (new_vec
, i
) = elt
;
12453 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12454 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12459 /* Substitute ARGS into the vector or list of template arguments T. */
12462 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12465 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12468 if (t
== error_mark_node
)
12469 return error_mark_node
;
12471 len
= TREE_VEC_LENGTH (t
);
12472 elts
= XALLOCAVEC (tree
, len
);
12474 for (i
= 0; i
< len
; i
++)
12476 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12479 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12480 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12481 else if (PACK_EXPANSION_P (orig_arg
))
12483 /* Substitute into an expansion expression. */
12484 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12486 if (TREE_CODE (new_arg
) == TREE_VEC
)
12487 /* Add to the expanded length adjustment the number of
12488 expanded arguments. We subtract one from this
12489 measurement, because the argument pack expression
12490 itself is already counted as 1 in
12491 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12492 the argument pack is empty. */
12493 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12495 else if (ARGUMENT_PACK_P (orig_arg
))
12497 /* Substitute into each of the arguments. */
12498 new_arg
= TYPE_P (orig_arg
)
12499 ? cxx_make_type (TREE_CODE (orig_arg
))
12500 : make_node (TREE_CODE (orig_arg
));
12502 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12503 args
, complain
, in_decl
);
12504 if (pack_args
== error_mark_node
)
12505 new_arg
= error_mark_node
;
12507 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12509 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12510 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12513 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12515 if (new_arg
== error_mark_node
)
12516 return error_mark_node
;
12519 if (new_arg
!= orig_arg
)
12526 /* Make space for the expanded arguments coming from template
12528 t
= make_tree_vec (len
+ expanded_len_adjust
);
12529 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12530 arguments for a member template.
12531 In that case each TREE_VEC in ORIG_T represents a level of template
12532 arguments, and ORIG_T won't carry any non defaulted argument count.
12533 It will rather be the nested TREE_VECs that will carry one.
12534 In other words, ORIG_T carries a non defaulted argument count only
12535 if it doesn't contain any nested TREE_VEC. */
12536 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12538 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12539 count
+= expanded_len_adjust
;
12540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12542 for (i
= 0, out
= 0; i
< len
; i
++)
12544 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12545 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12546 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12550 /* Now expand the template argument pack "in place". */
12551 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12552 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12556 TREE_VEC_ELT (t
, out
) = elts
[i
];
12564 /* Substitute ARGS into one level PARMS of template parameters. */
12567 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12569 if (parms
== error_mark_node
)
12570 return error_mark_node
;
12572 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12574 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12576 tree tuple
= TREE_VEC_ELT (parms
, i
);
12578 if (tuple
== error_mark_node
)
12581 TREE_VEC_ELT (new_vec
, i
) =
12582 tsubst_template_parm (tuple
, args
, complain
);
12588 /* Return the result of substituting ARGS into the template parameters
12589 given by PARMS. If there are m levels of ARGS and m + n levels of
12590 PARMS, then the result will contain n levels of PARMS. For
12591 example, if PARMS is `template <class T> template <class U>
12592 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12593 result will be `template <int*, double, class V>'. */
12596 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12598 tree r
= NULL_TREE
;
12601 /* When substituting into a template, we must set
12602 PROCESSING_TEMPLATE_DECL as the template parameters may be
12603 dependent if they are based on one-another, and the dependency
12604 predicates are short-circuit outside of templates. */
12605 ++processing_template_decl
;
12607 for (new_parms
= &r
;
12608 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12609 new_parms
= &(TREE_CHAIN (*new_parms
)),
12610 parms
= TREE_CHAIN (parms
))
12612 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12615 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12616 - TMPL_ARGS_DEPTH (args
)),
12617 new_vec
, NULL_TREE
);
12620 --processing_template_decl
;
12625 /* Return the result of substituting ARGS into one template parameter
12626 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12627 parameter and which TREE_PURPOSE is the default argument of the
12628 template parameter. */
12631 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12633 tree default_value
, parm_decl
;
12635 if (args
== NULL_TREE
12637 || t
== error_mark_node
)
12640 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12642 default_value
= TREE_PURPOSE (t
);
12643 parm_decl
= TREE_VALUE (t
);
12645 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12646 if (TREE_CODE (parm_decl
) == PARM_DECL
12647 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12648 parm_decl
= error_mark_node
;
12649 default_value
= tsubst_template_arg (default_value
, args
,
12650 complain
, NULL_TREE
);
12652 return build_tree_list (default_value
, parm_decl
);
12655 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12656 type T. If T is not an aggregate or enumeration type, it is
12657 handled as if by tsubst. IN_DECL is as for tsubst. If
12658 ENTERING_SCOPE is nonzero, T is the context for a template which
12659 we are presently tsubst'ing. Return the substituted value. */
12662 tsubst_aggr_type (tree t
,
12664 tsubst_flags_t complain
,
12666 int entering_scope
)
12668 if (t
== NULL_TREE
)
12671 switch (TREE_CODE (t
))
12674 if (TYPE_PTRMEMFUNC_P (t
))
12675 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12677 /* Fall through. */
12678 case ENUMERAL_TYPE
:
12680 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12686 /* In "sizeof(X<I>)" we need to evaluate "I". */
12689 /* First, determine the context for the type we are looking
12691 context
= TYPE_CONTEXT (t
);
12692 if (context
&& TYPE_P (context
))
12694 context
= tsubst_aggr_type (context
, args
, complain
,
12695 in_decl
, /*entering_scope=*/1);
12696 /* If context is a nested class inside a class template,
12697 it may still need to be instantiated (c++/33959). */
12698 context
= complete_type (context
);
12701 /* Then, figure out what arguments are appropriate for the
12702 type we are trying to find. For example, given:
12704 template <class T> struct S;
12705 template <class T, class U> void f(T, U) { S<U> su; }
12707 and supposing that we are instantiating f<int, double>,
12708 then our ARGS will be {int, double}, but, when looking up
12709 S we only want {double}. */
12710 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12711 complain
, in_decl
);
12712 if (argvec
== error_mark_node
)
12713 r
= error_mark_node
;
12716 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12717 entering_scope
, complain
);
12718 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12724 /* This is not a template type, so there's nothing to do. */
12728 return tsubst (t
, args
, complain
, in_decl
);
12732 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12734 /* Substitute into the default argument ARG (a default argument for
12735 FN), which has the indicated TYPE. */
12738 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12739 tsubst_flags_t complain
)
12741 int errs
= errorcount
+ sorrycount
;
12743 /* This can happen in invalid code. */
12744 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12747 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12748 parm
= chain_index (parmnum
, parm
);
12749 tree parmtype
= TREE_TYPE (parm
);
12750 if (DECL_BY_REFERENCE (parm
))
12751 parmtype
= TREE_TYPE (parmtype
);
12752 if (parmtype
== error_mark_node
)
12753 return error_mark_node
;
12755 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12758 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12761 /* This default argument came from a template. Instantiate the
12762 default argument here, not in tsubst. In the case of
12771 we must be careful to do name lookup in the scope of S<T>,
12772 rather than in the current class. */
12773 push_to_top_level ();
12774 push_access_scope (fn
);
12775 start_lambda_scope (parm
);
12777 /* The default argument expression may cause implicitly defined
12778 member functions to be synthesized, which will result in garbage
12779 collection. We must treat this situation as if we were within
12780 the body of function so as to avoid collecting live data on the
12783 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12784 complain
, NULL_TREE
,
12785 /*integral_constant_expression_p=*/false);
12788 finish_lambda_scope ();
12790 /* Make sure the default argument is reasonable. */
12791 arg
= check_default_argument (type
, arg
, complain
);
12793 if (errorcount
+sorrycount
> errs
12794 && (complain
& tf_warning_or_error
))
12795 inform (input_location
,
12796 " when instantiating default argument for call to %qD", fn
);
12798 pop_access_scope (fn
);
12799 pop_from_top_level ();
12801 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12804 defarg_inst
= tree_cache_map::create_ggc (37);
12805 defarg_inst
->put (parm
, arg
);
12811 /* Substitute into all the default arguments for FN. */
12814 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12819 tmpl_args
= DECL_TI_ARGS (fn
);
12821 /* If this function is not yet instantiated, we certainly don't need
12822 its default arguments. */
12823 if (uses_template_parms (tmpl_args
))
12825 /* Don't do this again for clones. */
12826 if (DECL_CLONED_FUNCTION_P (fn
))
12830 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12832 arg
= TREE_CHAIN (arg
), ++i
)
12833 if (TREE_PURPOSE (arg
))
12834 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12836 TREE_PURPOSE (arg
),
12840 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12841 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
12843 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12846 store_explicit_specifier (tree v
, tree t
)
12848 if (!explicit_specifier_map
)
12849 explicit_specifier_map
= tree_cache_map::create_ggc (37);
12850 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
12851 explicit_specifier_map
->put (v
, t
);
12854 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12857 lookup_explicit_specifier (tree v
)
12859 return *explicit_specifier_map
->get (v
);
12862 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12865 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12866 tree lambda_fntype
)
12868 tree gen_tmpl
, argvec
;
12869 hashval_t hash
= 0;
12872 /* Nobody should be tsubst'ing into non-template functions. */
12873 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12875 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12877 /* If T is not dependent, just return it. */
12878 if (!uses_template_parms (DECL_TI_ARGS (t
))
12879 && !LAMBDA_FUNCTION_P (t
))
12882 /* Calculate the most general template of which R is a
12884 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12886 /* We're substituting a lambda function under tsubst_lambda_expr but not
12887 directly from it; find the matching function we're already inside.
12888 But don't do this if T is a generic lambda with a single level of
12889 template parms, as in that case we're doing a normal instantiation. */
12890 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12891 && (!generic_lambda_fn_p (t
)
12892 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12893 return enclosing_instantiation_of (t
);
12895 /* Calculate the complete set of arguments used to
12897 argvec
= tsubst_template_args (DECL_TI_ARGS
12898 (DECL_TEMPLATE_RESULT
12899 (DECL_TI_TEMPLATE (t
))),
12900 args
, complain
, in_decl
);
12901 if (argvec
== error_mark_node
)
12902 return error_mark_node
;
12904 /* Check to see if we already have this specialization. */
12905 if (!lambda_fntype
)
12907 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12908 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12912 /* We can see more levels of arguments than parameters if
12913 there was a specialization of a member template, like
12916 template <class T> struct S { template <class U> void f(); }
12917 template <> template <class U> void S<int>::f(U);
12919 Here, we'll be substituting into the specialization,
12920 because that's where we can find the code we actually
12921 want to generate, but we'll have enough arguments for
12922 the most general template.
12924 We also deal with the peculiar case:
12926 template <class T> struct S {
12927 template <class U> friend void f();
12929 template <class U> void f() {}
12931 template void f<double>();
12933 Here, the ARGS for the instantiation of will be {int,
12934 double}. But, we only need as many ARGS as there are
12935 levels of template parameters in CODE_PATTERN. We are
12936 careful not to get fooled into reducing the ARGS in
12939 template <class T> struct S { template <class U> void f(U); }
12940 template <class T> template <> void S<T>::f(int) {}
12942 which we can spot because the pattern will be a
12943 specialization in this case. */
12944 int args_depth
= TMPL_ARGS_DEPTH (args
);
12946 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12948 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12949 args
= get_innermost_template_args (args
, parms_depth
);
12953 /* This special case arises when we have something like this:
12955 template <class T> struct S {
12956 friend void f<int>(int, double);
12959 Here, the DECL_TI_TEMPLATE for the friend declaration
12960 will be an IDENTIFIER_NODE. We are being called from
12961 tsubst_friend_function, and we want only to create a
12962 new decl (R) with appropriate types so that we can call
12963 determine_specialization. */
12964 gen_tmpl
= NULL_TREE
;
12965 argvec
= NULL_TREE
;
12968 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
12970 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
12971 bool member
= ctx
&& TYPE_P (ctx
);
12973 if (member
&& !closure
)
12974 ctx
= tsubst_aggr_type (ctx
, args
,
12975 complain
, t
, /*entering_scope=*/1);
12977 tree type
= (lambda_fntype
? lambda_fntype
12978 : tsubst (TREE_TYPE (t
), args
,
12979 complain
| tf_fndecl_type
, in_decl
));
12980 if (type
== error_mark_node
)
12981 return error_mark_node
;
12983 /* If we hit excessive deduction depth, the type is bogus even if
12984 it isn't error_mark_node, so don't build a decl. */
12985 if (excessive_deduction_depth
)
12986 return error_mark_node
;
12988 /* We do NOT check for matching decls pushed separately at this
12989 point, as they may not represent instantiations of this
12990 template, and in any case are considered separate under the
12992 tree r
= copy_decl (t
);
12993 DECL_USE_TEMPLATE (r
) = 0;
12994 TREE_TYPE (r
) = type
;
12995 /* Clear out the mangled name and RTL for the instantiation. */
12996 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12997 SET_DECL_RTL (r
, NULL
);
12998 /* Leave DECL_INITIAL set on deleted instantiations. */
12999 if (!DECL_DELETED_FN (r
))
13000 DECL_INITIAL (r
) = NULL_TREE
;
13001 DECL_CONTEXT (r
) = ctx
;
13003 /* Handle explicit(dependent-expr). */
13004 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13006 tree spec
= lookup_explicit_specifier (t
);
13007 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13008 /*function_p=*/false,
13010 spec
= build_explicit_specifier (spec
, complain
);
13011 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13014 /* OpenMP UDRs have the only argument a reference to the declared
13015 type. We want to diagnose if the declared type is a reference,
13016 which is invalid, but as references to references are usually
13017 quietly merged, diagnose it here. */
13018 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13021 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13022 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13023 if (TYPE_REF_P (argtype
))
13024 error_at (DECL_SOURCE_LOCATION (t
),
13025 "reference type %qT in "
13026 "%<#pragma omp declare reduction%>", argtype
);
13027 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13028 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13032 if (member
&& DECL_CONV_FN_P (r
))
13033 /* Type-conversion operator. Reconstruct the name, in
13034 case it's the name of one of the template's parameters. */
13035 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13037 tree parms
= DECL_ARGUMENTS (t
);
13039 parms
= DECL_CHAIN (parms
);
13040 parms
= tsubst (parms
, args
, complain
, t
);
13041 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13042 DECL_CONTEXT (parm
) = r
;
13045 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13046 DECL_CHAIN (tparm
) = parms
;
13049 DECL_ARGUMENTS (r
) = parms
;
13050 DECL_RESULT (r
) = NULL_TREE
;
13052 TREE_STATIC (r
) = 0;
13053 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13054 DECL_EXTERNAL (r
) = 1;
13055 /* If this is an instantiation of a function with internal
13056 linkage, we already know what object file linkage will be
13057 assigned to the instantiation. */
13058 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13059 DECL_DEFER_OUTPUT (r
) = 0;
13060 DECL_CHAIN (r
) = NULL_TREE
;
13061 DECL_PENDING_INLINE_INFO (r
) = 0;
13062 DECL_PENDING_INLINE_P (r
) = 0;
13063 DECL_SAVED_TREE (r
) = NULL_TREE
;
13064 DECL_STRUCT_FUNCTION (r
) = NULL
;
13066 /* We'll re-clone as appropriate in instantiate_template. */
13067 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13069 /* If we aren't complaining now, return on error before we register
13070 the specialization so that we'll complain eventually. */
13071 if ((complain
& tf_error
) == 0
13072 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13073 && !grok_op_properties (r
, /*complain=*/false))
13074 return error_mark_node
;
13076 /* When instantiating a constrained member, substitute
13077 into the constraints to create a new constraint. */
13078 if (tree ci
= get_constraints (t
))
13081 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13082 set_constraints (r
, ci
);
13085 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13086 this in the special friend case mentioned above where
13087 GEN_TMPL is NULL. */
13088 if (gen_tmpl
&& !closure
)
13090 DECL_TEMPLATE_INFO (r
)
13091 = build_template_info (gen_tmpl
, argvec
);
13092 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13095 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13097 /* We instantiated this while substituting into
13098 the type earlier (template/friend54.C). */
13101 /* We're not supposed to instantiate default arguments
13102 until they are called, for a template. But, for a
13105 template <class T> void f ()
13106 { extern void g(int i = T()); }
13108 we should do the substitution when the template is
13109 instantiated. We handle the member function case in
13110 instantiate_class_template since the default arguments
13111 might refer to other members of the class. */
13113 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13114 && !uses_template_parms (argvec
))
13115 tsubst_default_arguments (r
, complain
);
13118 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13120 /* Copy the list of befriending classes. */
13121 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13123 friends
= &TREE_CHAIN (*friends
))
13125 *friends
= copy_node (*friends
);
13126 TREE_VALUE (*friends
)
13127 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13130 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13132 maybe_retrofit_in_chrg (r
);
13133 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13134 return error_mark_node
;
13135 /* If this is an instantiation of a member template, clone it.
13136 If it isn't, that'll be handled by
13137 clone_constructors_and_destructors. */
13138 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13139 clone_function_decl (r
, /*update_methods=*/false);
13141 else if ((complain
& tf_error
) != 0
13142 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13143 && !grok_op_properties (r
, /*complain=*/true))
13144 return error_mark_node
;
13146 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13147 SET_DECL_FRIEND_CONTEXT (r
,
13148 tsubst (DECL_FRIEND_CONTEXT (t
),
13149 args
, complain
, in_decl
));
13151 /* Possibly limit visibility based on template args. */
13152 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13153 if (DECL_VISIBILITY_SPECIFIED (t
))
13155 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13156 DECL_ATTRIBUTES (r
)
13157 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13159 determine_visibility (r
);
13160 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13161 && !processing_template_decl
)
13162 defaulted_late_check (r
);
13164 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13165 args
, complain
, in_decl
);
13169 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13172 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13173 tree lambda_fntype
)
13175 /* We can get here when processing a member function template,
13176 member class template, or template template parameter. */
13177 tree decl
= DECL_TEMPLATE_RESULT (t
);
13183 hashval_t hash
= 0;
13185 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13187 /* Template template parameter is treated here. */
13188 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13189 if (new_type
== error_mark_node
)
13190 r
= error_mark_node
;
13191 /* If we get a real template back, return it. This can happen in
13192 the context of most_specialized_partial_spec. */
13193 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13196 /* The new TEMPLATE_DECL was built in
13197 reduce_template_parm_level. */
13198 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13202 if (!lambda_fntype
)
13204 /* We might already have an instance of this template.
13205 The ARGS are for the surrounding class type, so the
13206 full args contain the tsubst'd args for the context,
13207 plus the innermost args from the template decl. */
13208 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13209 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13210 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13211 /* Because this is a template, the arguments will still be
13212 dependent, even after substitution. If
13213 PROCESSING_TEMPLATE_DECL is not set, the dependency
13214 predicates will short-circuit. */
13215 ++processing_template_decl
;
13216 full_args
= tsubst_template_args (tmpl_args
, args
,
13217 complain
, in_decl
);
13218 --processing_template_decl
;
13219 if (full_args
== error_mark_node
)
13220 return error_mark_node
;
13222 /* If this is a default template template argument,
13223 tsubst might not have changed anything. */
13224 if (full_args
== tmpl_args
)
13227 hash
= hash_tmpl_and_args (t
, full_args
);
13228 spec
= retrieve_specialization (t
, full_args
, hash
);
13229 if (spec
!= NULL_TREE
)
13233 /* Make a new template decl. It will be similar to the
13234 original, but will record the current template arguments.
13235 We also create a new function declaration, which is just
13236 like the old one, but points to this new template, rather
13237 than the old one. */
13239 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13240 DECL_CHAIN (r
) = NULL_TREE
;
13242 // Build new template info linking to the original template decl.
13243 if (!lambda_fntype
)
13245 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13246 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13249 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13251 /* The template parameters for this new template are all the
13252 template parameters for the old template, except the
13253 outermost level of parameters. */
13254 DECL_TEMPLATE_PARMS (r
)
13255 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13258 if (TREE_CODE (decl
) == TYPE_DECL
13259 && !TYPE_DECL_ALIAS_P (decl
))
13262 ++processing_template_decl
;
13263 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13264 --processing_template_decl
;
13265 if (new_type
== error_mark_node
)
13266 return error_mark_node
;
13268 TREE_TYPE (r
) = new_type
;
13269 /* For a partial specialization, we need to keep pointing to
13270 the primary template. */
13271 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13272 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13273 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13274 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13275 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13280 ++processing_template_decl
;
13281 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13282 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13284 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13285 --processing_template_decl
;
13286 if (new_decl
== error_mark_node
)
13287 return error_mark_node
;
13289 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13290 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13291 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13294 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13295 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13299 DECL_TI_TEMPLATE (new_decl
) = r
;
13300 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13304 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13305 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13307 if (PRIMARY_TEMPLATE_P (t
))
13308 DECL_PRIMARY_TEMPLATE (r
) = r
;
13310 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13312 /* Record this non-type partial instantiation. */
13313 register_specialization (r
, t
,
13314 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13320 /* True if FN is the op() for a lambda in an uninstantiated template. */
13323 lambda_fn_in_template_p (tree fn
)
13325 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13327 tree closure
= DECL_CONTEXT (fn
);
13328 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13331 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13332 which the above is true. */
13335 instantiated_lambda_fn_p (tree fn
)
13337 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13339 tree closure
= DECL_CONTEXT (fn
);
13340 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13341 return LAMBDA_EXPR_INSTANTIATED (lam
);
13344 /* We're instantiating a variable from template function TCTX. Return the
13345 corresponding current enclosing scope. This gets complicated because lambda
13346 functions in templates are regenerated rather than instantiated, but generic
13347 lambda functions are subsequently instantiated. */
13350 enclosing_instantiation_of (tree otctx
)
13353 tree fn
= current_function_decl
;
13354 int lambda_count
= 0;
13356 for (; tctx
&& lambda_fn_in_template_p (tctx
);
13357 tctx
= decl_function_context (tctx
))
13359 for (; fn
; fn
= decl_function_context (fn
))
13362 int flambda_count
= 0;
13363 for (; fn
&& instantiated_lambda_fn_p (fn
);
13364 fn
= decl_function_context (fn
))
13366 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13367 ? most_general_template (fn
) != most_general_template (tctx
)
13370 if (flambda_count
!= lambda_count
)
13372 gcc_assert (flambda_count
> lambda_count
);
13373 for (; flambda_count
> lambda_count
; --flambda_count
)
13374 ofn
= decl_function_context (ofn
);
13376 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13377 || DECL_CONV_FN_P (ofn
));
13380 gcc_unreachable ();
13383 /* Substitute the ARGS into the T, which is a _DECL. Return the
13384 result of the substitution. Issue error and warning messages under
13385 control of COMPLAIN. */
13388 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13390 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13391 location_t saved_loc
;
13392 tree r
= NULL_TREE
;
13394 hashval_t hash
= 0;
13396 /* Set the filename and linenumber to improve error-reporting. */
13397 saved_loc
= input_location
;
13398 input_location
= DECL_SOURCE_LOCATION (t
);
13400 switch (TREE_CODE (t
))
13402 case TEMPLATE_DECL
:
13403 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13406 case FUNCTION_DECL
:
13407 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13412 tree type
= NULL_TREE
;
13414 tree expanded_types
= NULL_TREE
;
13415 tree prev_r
= NULL_TREE
;
13416 tree first_r
= NULL_TREE
;
13418 if (DECL_PACK_P (t
))
13420 /* If there is a local specialization that isn't a
13421 parameter pack, it means that we're doing a "simple"
13422 substitution from inside tsubst_pack_expansion. Just
13423 return the local specialization (which will be a single
13425 tree spec
= retrieve_local_specialization (t
);
13427 && TREE_CODE (spec
) == PARM_DECL
13428 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13431 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13432 the parameters in this function parameter pack. */
13433 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13434 complain
, in_decl
);
13435 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13437 len
= TREE_VEC_LENGTH (expanded_types
);
13439 /* Zero-length parameter packs are boring. Just substitute
13442 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13447 /* All we did was update the type. Make a note of that. */
13448 type
= expanded_types
;
13449 expanded_types
= NULL_TREE
;
13453 /* Loop through all of the parameters we'll build. When T is
13454 a function parameter pack, LEN is the number of expanded
13455 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13457 for (i
= 0; i
< len
; ++i
)
13461 if (DECL_TEMPLATE_PARM_P (t
))
13462 SET_DECL_TEMPLATE_PARM_P (r
);
13464 if (expanded_types
)
13465 /* We're on the Ith parameter of the function parameter
13468 /* Get the Ith type. */
13469 type
= TREE_VEC_ELT (expanded_types
, i
);
13471 /* Rename the parameter to include the index. */
13473 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13476 /* We're dealing with a normal parameter. */
13477 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13479 type
= type_decays_to (type
);
13480 TREE_TYPE (r
) = type
;
13481 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13483 if (DECL_INITIAL (r
))
13485 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13486 DECL_INITIAL (r
) = TREE_TYPE (r
);
13488 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13489 complain
, in_decl
);
13492 DECL_CONTEXT (r
) = NULL_TREE
;
13494 if (!DECL_TEMPLATE_PARM_P (r
))
13495 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13497 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13498 args
, complain
, in_decl
);
13500 /* Keep track of the first new parameter we
13501 generate. That's what will be returned to the
13506 /* Build a proper chain of parameters when substituting
13507 into a function parameter pack. */
13509 DECL_CHAIN (prev_r
) = r
;
13512 /* If cp_unevaluated_operand is set, we're just looking for a
13513 single dummy parameter, so don't keep going. */
13514 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13515 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13516 complain
, DECL_CHAIN (t
));
13518 /* FIRST_R contains the start of the chain we've built. */
13525 tree type
= NULL_TREE
;
13526 tree vec
= NULL_TREE
;
13527 tree expanded_types
= NULL_TREE
;
13530 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13532 /* This field is a lambda capture pack. Return a TREE_VEC of
13533 the expanded fields to instantiate_class_template_1. */
13534 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13535 complain
, in_decl
);
13536 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13538 len
= TREE_VEC_LENGTH (expanded_types
);
13539 vec
= make_tree_vec (len
);
13543 /* All we did was update the type. Make a note of that. */
13544 type
= expanded_types
;
13545 expanded_types
= NULL_TREE
;
13549 for (int i
= 0; i
< len
; ++i
)
13552 if (expanded_types
)
13554 type
= TREE_VEC_ELT (expanded_types
, i
);
13556 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13559 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13561 if (type
== error_mark_node
)
13562 RETURN (error_mark_node
);
13563 TREE_TYPE (r
) = type
;
13564 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13566 if (DECL_C_BIT_FIELD (r
))
13567 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13569 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13570 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13572 /*integral_constant_expression_p=*/true);
13573 if (DECL_INITIAL (t
))
13575 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13576 NSDMI in perform_member_init. Still set DECL_INITIAL
13577 so that we know there is one. */
13578 DECL_INITIAL (r
) = void_node
;
13579 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13580 retrofit_lang_decl (r
);
13581 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13583 /* We don't have to set DECL_CONTEXT here; it is set by
13584 finish_member_declaration. */
13585 DECL_CHAIN (r
) = NULL_TREE
;
13587 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13588 args
, complain
, in_decl
);
13591 TREE_VEC_ELT (vec
, i
) = r
;
13600 /* We reach here only for member using decls. We also need to check
13601 uses_template_parms because DECL_DEPENDENT_P is not set for a
13602 using-declaration that designates a member of the current
13603 instantiation (c++/53549). */
13604 if (DECL_DEPENDENT_P (t
)
13605 || uses_template_parms (USING_DECL_SCOPE (t
)))
13607 tree scope
= USING_DECL_SCOPE (t
);
13608 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13609 if (PACK_EXPANSION_P (scope
))
13611 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13612 int len
= TREE_VEC_LENGTH (vec
);
13613 r
= make_tree_vec (len
);
13614 for (int i
= 0; i
< len
; ++i
)
13616 tree escope
= TREE_VEC_ELT (vec
, i
);
13617 tree elt
= do_class_using_decl (escope
, name
);
13620 r
= error_mark_node
;
13625 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13626 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13628 TREE_VEC_ELT (r
, i
) = elt
;
13633 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13634 complain
, in_decl
);
13635 r
= do_class_using_decl (inst_scope
, name
);
13637 r
= error_mark_node
;
13640 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13641 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13648 DECL_CHAIN (r
) = NULL_TREE
;
13655 tree argvec
= NULL_TREE
;
13656 tree gen_tmpl
= NULL_TREE
;
13658 tree tmpl
= NULL_TREE
;
13660 tree type
= NULL_TREE
;
13663 if (TREE_TYPE (t
) == error_mark_node
)
13664 RETURN (error_mark_node
);
13666 if (TREE_CODE (t
) == TYPE_DECL
13667 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13669 /* If this is the canonical decl, we don't have to
13670 mess with instantiations, and often we can't (for
13671 typename, template type parms and such). Note that
13672 TYPE_NAME is not correct for the above test if
13673 we've copied the type for a typedef. */
13674 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13675 if (type
== error_mark_node
)
13676 RETURN (error_mark_node
);
13677 r
= TYPE_NAME (type
);
13681 /* Check to see if we already have the specialization we
13684 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13686 /* T is a static data member or namespace-scope entity.
13687 We have to substitute into namespace-scope variables
13688 (not just variable templates) because of cases like:
13690 template <class T> void f() { extern T t; }
13692 where the entity referenced is not known until
13693 instantiation time. */
13695 ctx
= DECL_CONTEXT (t
);
13696 if (DECL_CLASS_SCOPE_P (t
))
13698 ctx
= tsubst_aggr_type (ctx
, args
,
13700 in_decl
, /*entering_scope=*/1);
13701 /* If CTX is unchanged, then T is in fact the
13702 specialization we want. That situation occurs when
13703 referencing a static data member within in its own
13704 class. We can use pointer equality, rather than
13705 same_type_p, because DECL_CONTEXT is always
13707 if (ctx
== DECL_CONTEXT (t
)
13708 /* ... unless T is a member template; in which
13709 case our caller can be willing to create a
13710 specialization of that template represented
13712 && !(DECL_TI_TEMPLATE (t
)
13713 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13719 tmpl
= DECL_TI_TEMPLATE (t
);
13720 gen_tmpl
= most_general_template (tmpl
);
13721 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13722 if (argvec
!= error_mark_node
)
13723 argvec
= (coerce_innermost_template_parms
13724 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13725 argvec
, t
, complain
,
13726 /*all*/true, /*defarg*/true));
13727 if (argvec
== error_mark_node
)
13728 RETURN (error_mark_node
);
13729 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13730 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13735 /* A local variable. */
13737 /* Subsequent calls to pushdecl will fill this in. */
13739 /* Unless this is a reference to a static variable from an
13740 enclosing function, in which case we need to fill it in now. */
13741 if (TREE_STATIC (t
))
13743 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13744 if (fn
!= current_function_decl
)
13747 spec
= retrieve_local_specialization (t
);
13749 /* If we already have the specialization we need, there is
13750 nothing more to do. */
13757 /* Create a new node for the specialization we need. */
13758 if (type
== NULL_TREE
)
13760 if (is_typedef_decl (t
))
13761 type
= DECL_ORIGINAL_TYPE (t
);
13763 type
= TREE_TYPE (t
);
13765 && VAR_HAD_UNKNOWN_BOUND (t
)
13766 && type
!= error_mark_node
)
13767 type
= strip_array_domain (type
);
13768 tree sub_args
= args
;
13769 if (tree auto_node
= type_uses_auto (type
))
13771 /* Mask off any template args past the variable's context so we
13772 don't replace the auto with an unrelated argument. */
13773 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13774 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13776 /* This should never happen with the new lambda instantiation
13777 model, but keep the handling just in case. */
13778 gcc_assert (!CHECKING_P
),
13779 sub_args
= strip_innermost_template_args (args
, extra
);
13781 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13782 /* Substituting the type might have recursively instantiated this
13783 same alias (c++/86171). */
13784 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13785 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13794 DECL_INITIALIZED_P (r
) = 0;
13795 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13796 if (type
== error_mark_node
)
13797 RETURN (error_mark_node
);
13798 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13800 /* It may seem that this case cannot occur, since:
13805 declares a function, not a variable. However:
13808 template <typename T> void g() { T t; }
13809 template void g<f>();
13811 is an attempt to declare a variable with function
13813 error ("variable %qD has function type",
13814 /* R is not yet sufficiently initialized, so we
13815 just use its name. */
13817 RETURN (error_mark_node
);
13819 type
= complete_type (type
);
13820 /* Wait until cp_finish_decl to set this again, to handle
13821 circular dependency (template/instantiate6.C). */
13822 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13823 type
= check_var_type (DECL_NAME (r
), type
);
13825 if (DECL_HAS_VALUE_EXPR_P (t
))
13827 tree ve
= DECL_VALUE_EXPR (t
);
13828 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13829 /*constant_expression_p=*/false);
13830 if (REFERENCE_REF_P (ve
))
13832 gcc_assert (TYPE_REF_P (type
));
13833 ve
= TREE_OPERAND (ve
, 0);
13835 SET_DECL_VALUE_EXPR (r
, ve
);
13837 if (CP_DECL_THREAD_LOCAL_P (r
)
13838 && !processing_template_decl
)
13839 set_decl_tls_model (r
, decl_default_tls_model (r
));
13841 else if (DECL_SELF_REFERENCE_P (t
))
13842 SET_DECL_SELF_REFERENCE_P (r
);
13843 TREE_TYPE (r
) = type
;
13844 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13845 DECL_CONTEXT (r
) = ctx
;
13846 /* Clear out the mangled name and RTL for the instantiation. */
13847 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13848 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13849 SET_DECL_RTL (r
, NULL
);
13850 /* The initializer must not be expanded until it is required;
13851 see [temp.inst]. */
13852 DECL_INITIAL (r
) = NULL_TREE
;
13853 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13856 if (DECL_LANG_SPECIFIC (r
))
13857 SET_DECL_DEPENDENT_INIT_P (r
, false);
13859 SET_DECL_MODE (r
, VOIDmode
);
13861 /* Possibly limit visibility based on template args. */
13862 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13863 if (DECL_VISIBILITY_SPECIFIED (t
))
13865 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13866 DECL_ATTRIBUTES (r
)
13867 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13869 determine_visibility (r
);
13874 /* A static data member declaration is always marked
13875 external when it is declared in-class, even if an
13876 initializer is present. We mimic the non-template
13877 processing here. */
13878 DECL_EXTERNAL (r
) = 1;
13879 if (DECL_NAMESPACE_SCOPE_P (t
))
13880 DECL_NOT_REALLY_EXTERN (r
) = 1;
13882 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13883 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13884 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13888 if (DECL_LANG_SPECIFIC (r
))
13889 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13890 if (!cp_unevaluated_operand
)
13891 register_local_specialization (r
, t
);
13894 DECL_CHAIN (r
) = NULL_TREE
;
13896 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13898 args
, complain
, in_decl
);
13900 /* Preserve a typedef that names a type. */
13901 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13903 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13904 set_underlying_type (r
);
13905 if (TYPE_DECL_ALIAS_P (r
))
13906 /* An alias template specialization can be dependent
13907 even if its underlying type is not. */
13908 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13911 layout_decl (r
, 0);
13916 gcc_unreachable ();
13921 /* Restore the file and line information. */
13922 input_location
= saved_loc
;
13927 /* Substitute into the ARG_TYPES of a function type.
13928 If END is a TREE_CHAIN, leave it and any following types
13932 tsubst_arg_types (tree arg_types
,
13935 tsubst_flags_t complain
,
13938 tree remaining_arg_types
;
13939 tree type
= NULL_TREE
;
13941 tree expanded_args
= NULL_TREE
;
13944 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13947 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13948 args
, end
, complain
, in_decl
);
13949 if (remaining_arg_types
== error_mark_node
)
13950 return error_mark_node
;
13952 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13954 /* For a pack expansion, perform substitution on the
13955 entire expression. Later on, we'll handle the arguments
13957 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13958 args
, complain
, in_decl
);
13960 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13961 /* So that we'll spin through the parameters, one by one. */
13962 i
= TREE_VEC_LENGTH (expanded_args
);
13965 /* We only partially substituted into the parameter
13966 pack. Our type is TYPE_PACK_EXPANSION. */
13967 type
= expanded_args
;
13968 expanded_args
= NULL_TREE
;
13976 type
= TREE_VEC_ELT (expanded_args
, i
);
13978 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13980 if (type
== error_mark_node
)
13981 return error_mark_node
;
13982 if (VOID_TYPE_P (type
))
13984 if (complain
& tf_error
)
13986 error ("invalid parameter type %qT", type
);
13988 error ("in declaration %q+D", in_decl
);
13990 return error_mark_node
;
13993 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13994 return error_mark_node
;
13996 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13997 top-level qualifiers as required. */
13998 type
= cv_unqualified (type_decays_to (type
));
14000 /* We do not substitute into default arguments here. The standard
14001 mandates that they be instantiated only when needed, which is
14002 done in build_over_call. */
14003 default_arg
= TREE_PURPOSE (arg_types
);
14005 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14006 since the new op() won't have any associated template arguments for us
14007 to refer to later. */
14008 if (lambda_fn_in_template_p (in_decl
))
14009 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14010 false/*fn*/, false/*constexpr*/);
14012 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
14014 /* We've instantiated a template before its default arguments
14015 have been parsed. This can happen for a nested template
14016 class, and is not an error unless we require the default
14017 argument in a call of this function. */
14018 remaining_arg_types
=
14019 tree_cons (default_arg
, type
, remaining_arg_types
);
14020 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
14023 remaining_arg_types
=
14024 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14027 return remaining_arg_types
;
14030 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14031 *not* handle the exception-specification for FNTYPE, because the
14032 initial substitution of explicitly provided template parameters
14033 during argument deduction forbids substitution into the
14034 exception-specification:
14038 All references in the function type of the function template to the
14039 corresponding template parameters are replaced by the specified tem-
14040 plate argument values. If a substitution in a template parameter or
14041 in the function type of the function template results in an invalid
14042 type, type deduction fails. [Note: The equivalent substitution in
14043 exception specifications is done only when the function is instanti-
14044 ated, at which point a program is ill-formed if the substitution
14045 results in an invalid type.] */
14048 tsubst_function_type (tree t
,
14050 tsubst_flags_t complain
,
14054 tree arg_types
= NULL_TREE
;
14057 /* The TYPE_CONTEXT is not used for function/method types. */
14058 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14060 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14062 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14064 if (late_return_type_p
)
14066 /* Substitute the argument types. */
14067 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14068 complain
, in_decl
);
14069 if (arg_types
== error_mark_node
)
14070 return error_mark_node
;
14072 tree save_ccp
= current_class_ptr
;
14073 tree save_ccr
= current_class_ref
;
14074 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14075 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14076 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14079 /* DR 1207: 'this' is in scope in the trailing return type. */
14080 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14083 /* Substitute the return type. */
14084 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14088 current_class_ptr
= save_ccp
;
14089 current_class_ref
= save_ccr
;
14093 /* Substitute the return type. */
14094 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14096 if (return_type
== error_mark_node
)
14097 return error_mark_node
;
14098 /* DR 486 clarifies that creation of a function type with an
14099 invalid return type is a deduction failure. */
14100 if (TREE_CODE (return_type
) == ARRAY_TYPE
14101 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14103 if (complain
& tf_error
)
14105 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14106 error ("function returning an array");
14108 error ("function returning a function");
14110 return error_mark_node
;
14113 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14114 return error_mark_node
;
14116 if (!late_return_type_p
)
14118 /* Substitute the argument types. */
14119 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14120 complain
, in_decl
);
14121 if (arg_types
== error_mark_node
)
14122 return error_mark_node
;
14125 /* Construct a new type node and return it. */
14126 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14128 fntype
= build_function_type (return_type
, arg_types
);
14129 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14133 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14134 /* Don't pick up extra function qualifiers from the basetype. */
14135 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14136 if (! MAYBE_CLASS_TYPE_P (r
))
14140 Type deduction may fail for any of the following
14143 -- Attempting to create "pointer to member of T" when T
14144 is not a class type. */
14145 if (complain
& tf_error
)
14146 error ("creating pointer to member function of non-class type %qT",
14148 return error_mark_node
;
14151 fntype
= build_method_type_directly (r
, return_type
,
14152 TREE_CHAIN (arg_types
));
14154 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14156 /* See comment above. */
14157 tree raises
= NULL_TREE
;
14158 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14159 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14164 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14165 ARGS into that specification, and return the substituted
14166 specification. If there is no specification, return NULL_TREE. */
14169 tsubst_exception_specification (tree fntype
,
14171 tsubst_flags_t complain
,
14178 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14179 new_specs
= NULL_TREE
;
14180 if (specs
&& TREE_PURPOSE (specs
))
14182 /* A noexcept-specifier. */
14183 tree expr
= TREE_PURPOSE (specs
);
14184 if (TREE_CODE (expr
) == INTEGER_CST
)
14188 /* Defer instantiation of noexcept-specifiers to avoid
14189 excessive instantiations (c++/49107). */
14190 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14191 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14193 /* We already partially instantiated this member template,
14194 so combine the new args with the old. */
14195 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14196 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14197 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14198 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14202 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14203 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14208 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14210 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14212 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14214 new_specs
= tsubst_copy_and_build
14215 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14216 /*integral_constant_expression_p=*/true);
14218 new_specs
= build_noexcept_spec (new_specs
, complain
);
14222 if (! TREE_VALUE (specs
))
14229 tree expanded_specs
= NULL_TREE
;
14231 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14233 /* Expand the pack expansion type. */
14234 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14238 if (expanded_specs
== error_mark_node
)
14239 return error_mark_node
;
14240 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14241 len
= TREE_VEC_LENGTH (expanded_specs
);
14244 /* We're substituting into a member template, so
14245 we got a TYPE_PACK_EXPANSION back. Add that
14246 expansion and move on. */
14247 gcc_assert (TREE_CODE (expanded_specs
)
14248 == TYPE_PACK_EXPANSION
);
14249 new_specs
= add_exception_specifier (new_specs
,
14252 specs
= TREE_CHAIN (specs
);
14257 for (i
= 0; i
< len
; ++i
)
14259 if (expanded_specs
)
14260 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14262 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14263 if (spec
== error_mark_node
)
14265 new_specs
= add_exception_specifier (new_specs
, spec
,
14269 specs
= TREE_CHAIN (specs
);
14275 /* Take the tree structure T and replace template parameters used
14276 therein with the argument vector ARGS. IN_DECL is an associated
14277 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14278 Issue error and warning messages under control of COMPLAIN. Note
14279 that we must be relatively non-tolerant of extensions here, in
14280 order to preserve conformance; if we allow substitutions that
14281 should not be allowed, we may allow argument deductions that should
14282 not succeed, and therefore report ambiguous overload situations
14283 where there are none. In theory, we could allow the substitution,
14284 but indicate that it should have failed, and allow our caller to
14285 make sure that the right thing happens, but we don't try to do this
14288 This function is used for dealing with types, decls and the like;
14289 for expressions, use tsubst_expr or tsubst_copy. */
14292 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14294 enum tree_code code
;
14295 tree type
, r
= NULL_TREE
;
14297 if (t
== NULL_TREE
|| t
== error_mark_node
14298 || t
== integer_type_node
14299 || t
== void_type_node
14300 || t
== char_type_node
14301 || t
== unknown_type_node
14302 || TREE_CODE (t
) == NAMESPACE_DECL
14303 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14307 return tsubst_decl (t
, args
, complain
);
14309 if (args
== NULL_TREE
)
14312 code
= TREE_CODE (t
);
14314 if (code
== IDENTIFIER_NODE
)
14315 type
= IDENTIFIER_TYPE_VALUE (t
);
14317 type
= TREE_TYPE (t
);
14319 gcc_assert (type
!= unknown_type_node
);
14321 /* Reuse typedefs. We need to do this to handle dependent attributes,
14322 such as attribute aligned. */
14324 && typedef_variant_p (t
))
14326 tree decl
= TYPE_NAME (t
);
14328 if (alias_template_specialization_p (t
))
14330 /* DECL represents an alias template and we want to
14332 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14333 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14334 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14336 else if (DECL_CLASS_SCOPE_P (decl
)
14337 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14338 && uses_template_parms (DECL_CONTEXT (decl
)))
14340 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14341 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14342 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14344 else if (DECL_FUNCTION_SCOPE_P (decl
)
14345 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14346 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14347 r
= retrieve_local_specialization (decl
);
14349 /* The typedef is from a non-template context. */
14355 r
= cp_build_qualified_type_real
14356 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14357 complain
| tf_ignore_bad_quals
);
14362 /* We don't have an instantiation yet, so drop the typedef. */
14363 int quals
= cp_type_quals (t
);
14364 t
= DECL_ORIGINAL_TYPE (decl
);
14365 t
= cp_build_qualified_type_real (t
, quals
,
14366 complain
| tf_ignore_bad_quals
);
14370 bool fndecl_type
= (complain
& tf_fndecl_type
);
14371 complain
&= ~tf_fndecl_type
;
14374 && code
!= TYPENAME_TYPE
14375 && code
!= TEMPLATE_TYPE_PARM
14376 && code
!= TEMPLATE_PARM_INDEX
14377 && code
!= IDENTIFIER_NODE
14378 && code
!= FUNCTION_TYPE
14379 && code
!= METHOD_TYPE
)
14380 type
= tsubst (type
, args
, complain
, in_decl
);
14381 if (type
== error_mark_node
)
14382 return error_mark_node
;
14388 case ENUMERAL_TYPE
:
14389 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14390 /*entering_scope=*/0);
14393 case IDENTIFIER_NODE
:
14404 if (t
== integer_type_node
)
14407 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14408 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14412 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14414 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14415 /*integral_constant_expression_p=*/false);
14417 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14419 if (TREE_CODE (max
) == NOP_EXPR
14420 && TREE_SIDE_EFFECTS (omax
)
14421 && !TREE_TYPE (max
))
14422 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14424 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14425 with TREE_SIDE_EFFECTS that indicates this is not an integral
14426 constant expression. */
14427 if (processing_template_decl
14428 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14430 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14431 TREE_SIDE_EFFECTS (max
) = 1;
14434 return compute_array_index_type (NULL_TREE
, max
, complain
);
14437 case TEMPLATE_TYPE_PARM
:
14438 case TEMPLATE_TEMPLATE_PARM
:
14439 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14440 case TEMPLATE_PARM_INDEX
:
14445 tree arg
= NULL_TREE
;
14447 /* Early in template argument deduction substitution, we don't
14448 want to reduce the level of 'auto', or it will be confused
14449 with a normal template parm in subsequent deduction. */
14450 if (is_auto (t
) && (complain
& tf_partial
))
14455 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14456 template_parm_level_and_index (t
, &level
, &idx
);
14458 levels
= TMPL_ARGS_DEPTH (args
);
14459 if (level
<= levels
14460 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14462 arg
= TMPL_ARG (args
, level
, idx
);
14464 /* See through ARGUMENT_PACK_SELECT arguments. */
14465 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14466 arg
= argument_pack_select_arg (arg
);
14469 if (arg
== error_mark_node
)
14470 return error_mark_node
;
14471 else if (arg
!= NULL_TREE
)
14473 if (ARGUMENT_PACK_P (arg
))
14474 /* If ARG is an argument pack, we don't actually want to
14475 perform a substitution here, because substitutions
14476 for argument packs are only done
14477 element-by-element. We can get to this point when
14478 substituting the type of a non-type template
14479 parameter pack, when that type actually contains
14480 template parameter packs from an outer template, e.g.,
14482 template<typename... Types> struct A {
14483 template<Types... Values> struct B { };
14487 if (code
== TEMPLATE_TYPE_PARM
)
14490 gcc_assert (TYPE_P (arg
));
14492 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14494 return cp_build_qualified_type_real
14495 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14497 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14499 /* We are processing a type constructed from a
14500 template template parameter. */
14501 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14502 args
, complain
, in_decl
);
14503 if (argvec
== error_mark_node
)
14504 return error_mark_node
;
14506 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14507 || TREE_CODE (arg
) == TEMPLATE_DECL
14508 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14510 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14511 /* Consider this code:
14513 template <template <class> class Template>
14515 template <class Arg> using Bind = Template<Arg>;
14518 template <template <class> class Template, class Arg>
14519 using Instantiate = Template<Arg>; //#0
14521 template <template <class> class Template,
14524 Instantiate<Internal<Template>::template Bind,
14527 When #1 is parsed, the
14528 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14529 parameter `Template' in #0 matches the
14530 UNBOUND_CLASS_TEMPLATE representing the argument
14531 `Internal<Template>::template Bind'; We then want
14532 to assemble the type `Bind<Argument>' that can't
14533 be fully created right now, because
14534 `Internal<Template>' not being complete, the Bind
14535 template cannot be looked up in that context. So
14536 we need to "store" `Bind<Argument>' for later
14537 when the context of Bind becomes complete. Let's
14538 store that in a TYPENAME_TYPE. */
14539 return make_typename_type (TYPE_CONTEXT (arg
),
14540 build_nt (TEMPLATE_ID_EXPR
,
14541 TYPE_IDENTIFIER (arg
),
14546 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14547 are resolving nested-types in the signature of a
14548 member function templates. Otherwise ARG is a
14549 TEMPLATE_DECL and is the real template to be
14551 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14552 arg
= TYPE_NAME (arg
);
14554 r
= lookup_template_class (arg
,
14556 DECL_CONTEXT (arg
),
14557 /*entering_scope=*/0,
14559 return cp_build_qualified_type_real
14560 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14562 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14565 /* TEMPLATE_PARM_INDEX. */
14566 return convert_from_reference (unshare_expr (arg
));
14570 /* This can happen during the attempted tsubst'ing in
14571 unify. This means that we don't yet have any information
14572 about the template parameter in question. */
14575 /* If we get here, we must have been looking at a parm for a
14576 more deeply nested template. Make a new version of this
14577 template parameter, but with a lower level. */
14580 case TEMPLATE_TYPE_PARM
:
14581 case TEMPLATE_TEMPLATE_PARM
:
14582 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14583 if (cp_type_quals (t
))
14585 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14586 r
= cp_build_qualified_type_real
14587 (r
, cp_type_quals (t
),
14588 complain
| (code
== TEMPLATE_TYPE_PARM
14589 ? tf_ignore_bad_quals
: 0));
14591 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14592 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14593 && (r
= (TEMPLATE_PARM_DESCENDANTS
14594 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14595 && (r
= TREE_TYPE (r
))
14596 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14597 /* Break infinite recursion when substituting the constraints
14598 of a constrained placeholder. */;
14599 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14600 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14601 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14602 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14603 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14604 && (TEMPLATE_PARM_LEVEL (r
)
14605 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14606 /* Cache the simple case of lowering a type parameter. */
14611 TEMPLATE_TYPE_PARM_INDEX (r
)
14612 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14613 r
, levels
, args
, complain
);
14614 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14615 TYPE_MAIN_VARIANT (r
) = r
;
14616 TYPE_POINTER_TO (r
) = NULL_TREE
;
14617 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14619 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14621 /* Propagate constraints on placeholders. */
14622 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14623 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14624 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14625 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14627 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14628 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14632 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14633 /* We have reduced the level of the template
14634 template parameter, but not the levels of its
14635 template parameters, so canonical_type_parameter
14636 will not be able to find the canonical template
14637 template parameter for this level. Thus, we
14638 require structural equality checking to compare
14639 TEMPLATE_TEMPLATE_PARMs. */
14640 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14641 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14642 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14644 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14646 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14648 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14649 /* We might need to substitute into the types of non-type
14650 template parameters. */
14651 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14652 complain
, in_decl
);
14653 if (tmpl
== error_mark_node
)
14654 return error_mark_node
;
14655 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14656 complain
, in_decl
);
14657 if (argvec
== error_mark_node
)
14658 return error_mark_node
;
14660 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14661 = build_template_info (tmpl
, argvec
);
14666 case TEMPLATE_PARM_INDEX
:
14667 /* OK, now substitute the type of the non-type parameter. We
14668 couldn't do it earlier because it might be an auto parameter,
14669 and we wouldn't need to if we had an argument. */
14670 type
= tsubst (type
, args
, complain
, in_decl
);
14671 if (type
== error_mark_node
)
14672 return error_mark_node
;
14673 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14677 gcc_unreachable ();
14685 tree purpose
, value
, chain
;
14687 if (t
== void_list_node
)
14690 purpose
= TREE_PURPOSE (t
);
14693 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14694 if (purpose
== error_mark_node
)
14695 return error_mark_node
;
14697 value
= TREE_VALUE (t
);
14700 value
= tsubst (value
, args
, complain
, in_decl
);
14701 if (value
== error_mark_node
)
14702 return error_mark_node
;
14704 chain
= TREE_CHAIN (t
);
14705 if (chain
&& chain
!= void_type_node
)
14707 chain
= tsubst (chain
, args
, complain
, in_decl
);
14708 if (chain
== error_mark_node
)
14709 return error_mark_node
;
14711 if (purpose
== TREE_PURPOSE (t
)
14712 && value
== TREE_VALUE (t
)
14713 && chain
== TREE_CHAIN (t
))
14715 return hash_tree_cons (purpose
, value
, chain
);
14719 /* We should never be tsubsting a binfo. */
14720 gcc_unreachable ();
14723 /* A vector of template arguments. */
14724 gcc_assert (!type
);
14725 return tsubst_template_args (t
, args
, complain
, in_decl
);
14728 case REFERENCE_TYPE
:
14730 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14735 Type deduction may fail for any of the following
14738 -- Attempting to create a pointer to reference type.
14739 -- Attempting to create a reference to a reference type or
14740 a reference to void.
14742 Core issue 106 says that creating a reference to a reference
14743 during instantiation is no longer a cause for failure. We
14744 only enforce this check in strict C++98 mode. */
14745 if ((TYPE_REF_P (type
)
14746 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14747 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14749 static location_t last_loc
;
14751 /* We keep track of the last time we issued this error
14752 message to avoid spewing a ton of messages during a
14753 single bad template instantiation. */
14754 if (complain
& tf_error
14755 && last_loc
!= input_location
)
14757 if (VOID_TYPE_P (type
))
14758 error ("forming reference to void");
14759 else if (code
== POINTER_TYPE
)
14760 error ("forming pointer to reference type %qT", type
);
14762 error ("forming reference to reference type %qT", type
);
14763 last_loc
= input_location
;
14766 return error_mark_node
;
14768 else if (TREE_CODE (type
) == FUNCTION_TYPE
14769 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14770 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14772 if (complain
& tf_error
)
14774 if (code
== POINTER_TYPE
)
14775 error ("forming pointer to qualified function type %qT",
14778 error ("forming reference to qualified function type %qT",
14781 return error_mark_node
;
14783 else if (code
== POINTER_TYPE
)
14785 r
= build_pointer_type (type
);
14786 if (TREE_CODE (type
) == METHOD_TYPE
)
14787 r
= build_ptrmemfunc_type (r
);
14789 else if (TYPE_REF_P (type
))
14790 /* In C++0x, during template argument substitution, when there is an
14791 attempt to create a reference to a reference type, reference
14792 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14794 "If a template-argument for a template-parameter T names a type
14795 that is a reference to a type A, an attempt to create the type
14796 'lvalue reference to cv T' creates the type 'lvalue reference to
14797 A,' while an attempt to create the type type rvalue reference to
14798 cv T' creates the type T"
14800 r
= cp_build_reference_type
14802 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14804 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14805 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14807 if (r
!= error_mark_node
)
14808 /* Will this ever be needed for TYPE_..._TO values? */
14815 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14816 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14820 Type deduction may fail for any of the following
14823 -- Attempting to create "pointer to member of T" when T
14824 is not a class type. */
14825 if (complain
& tf_error
)
14826 error ("creating pointer to member of non-class type %qT", r
);
14827 return error_mark_node
;
14829 if (TYPE_REF_P (type
))
14831 if (complain
& tf_error
)
14832 error ("creating pointer to member reference type %qT", type
);
14833 return error_mark_node
;
14835 if (VOID_TYPE_P (type
))
14837 if (complain
& tf_error
)
14838 error ("creating pointer to member of type void");
14839 return error_mark_node
;
14841 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14842 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14844 /* The type of the implicit object parameter gets its
14845 cv-qualifiers from the FUNCTION_TYPE. */
14848 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14849 type_memfn_rqual (type
));
14850 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14851 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14855 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14859 case FUNCTION_TYPE
:
14864 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14865 if (fntype
== error_mark_node
)
14866 return error_mark_node
;
14868 /* Substitute the exception specification. */
14869 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14870 /*defer_ok*/fndecl_type
);
14871 if (specs
== error_mark_node
)
14872 return error_mark_node
;
14874 fntype
= build_exception_variant (fntype
, specs
);
14879 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14880 if (domain
== error_mark_node
)
14881 return error_mark_node
;
14883 /* As an optimization, we avoid regenerating the array type if
14884 it will obviously be the same as T. */
14885 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14888 /* These checks should match the ones in create_array_type_for_decl.
14892 The deduction may fail for any of the following reasons:
14894 -- Attempting to create an array with an element type that
14895 is void, a function type, or a reference type, or [DR337]
14896 an abstract class type. */
14897 if (VOID_TYPE_P (type
)
14898 || TREE_CODE (type
) == FUNCTION_TYPE
14899 || (TREE_CODE (type
) == ARRAY_TYPE
14900 && TYPE_DOMAIN (type
) == NULL_TREE
)
14901 || TYPE_REF_P (type
))
14903 if (complain
& tf_error
)
14904 error ("creating array of %qT", type
);
14905 return error_mark_node
;
14908 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14909 return error_mark_node
;
14911 r
= build_cplus_array_type (type
, domain
);
14913 if (!valid_array_size_p (input_location
, r
, in_decl
,
14914 (complain
& tf_error
)))
14915 return error_mark_node
;
14917 if (TYPE_USER_ALIGN (t
))
14919 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14920 TYPE_USER_ALIGN (r
) = 1;
14926 case TYPENAME_TYPE
:
14928 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14929 in_decl
, /*entering_scope=*/1);
14930 if (ctx
== error_mark_node
)
14931 return error_mark_node
;
14933 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14934 complain
, in_decl
);
14935 if (f
== error_mark_node
)
14936 return error_mark_node
;
14938 if (!MAYBE_CLASS_TYPE_P (ctx
))
14940 if (complain
& tf_error
)
14941 error ("%qT is not a class, struct, or union type", ctx
);
14942 return error_mark_node
;
14944 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14946 /* Normally, make_typename_type does not require that the CTX
14947 have complete type in order to allow things like:
14949 template <class T> struct S { typename S<T>::X Y; };
14951 But, such constructs have already been resolved by this
14952 point, so here CTX really should have complete type, unless
14953 it's a partial instantiation. */
14954 ctx
= complete_type (ctx
);
14955 if (!COMPLETE_TYPE_P (ctx
))
14957 if (complain
& tf_error
)
14958 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14959 return error_mark_node
;
14963 f
= make_typename_type (ctx
, f
, typename_type
,
14964 complain
| tf_keep_type_decl
);
14965 if (f
== error_mark_node
)
14967 if (TREE_CODE (f
) == TYPE_DECL
)
14969 complain
|= tf_ignore_bad_quals
;
14973 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14975 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14977 if (complain
& tf_error
)
14978 error ("%qT resolves to %qT, which is not an enumeration type",
14981 return error_mark_node
;
14983 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14985 if (complain
& tf_error
)
14986 error ("%qT resolves to %qT, which is is not a class type",
14989 return error_mark_node
;
14993 return cp_build_qualified_type_real
14994 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14997 case UNBOUND_CLASS_TEMPLATE
:
14999 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15000 in_decl
, /*entering_scope=*/1);
15001 tree name
= TYPE_IDENTIFIER (t
);
15002 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15004 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15005 return error_mark_node
;
15008 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15009 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15016 ++cp_unevaluated_operand
;
15017 ++c_inhibit_evaluation_warnings
;
15019 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15021 /*integral_constant_expression_p=*/false);
15023 --cp_unevaluated_operand
;
15024 --c_inhibit_evaluation_warnings
;
15026 type
= finish_typeof (type
);
15027 return cp_build_qualified_type_real (type
,
15029 | cp_type_quals (type
),
15033 case DECLTYPE_TYPE
:
15037 ++cp_unevaluated_operand
;
15038 ++c_inhibit_evaluation_warnings
;
15040 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15041 complain
|tf_decltype
, in_decl
,
15042 /*function_p*/false,
15043 /*integral_constant_expression*/false);
15045 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
15047 if (type
== NULL_TREE
)
15049 if (complain
& tf_error
)
15050 error ("empty initializer in lambda init-capture");
15051 type
= error_mark_node
;
15053 else if (TREE_CODE (type
) == TREE_LIST
)
15054 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
15057 --cp_unevaluated_operand
;
15058 --c_inhibit_evaluation_warnings
;
15060 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15061 type
= lambda_capture_field_type (type
,
15062 DECLTYPE_FOR_INIT_CAPTURE (t
),
15063 DECLTYPE_FOR_REF_CAPTURE (t
));
15064 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15065 type
= lambda_proxy_type (type
);
15068 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15069 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15071 /* In a template ~id could be either a complement expression
15072 or an unqualified-id naming a destructor; if instantiating
15073 it produces an expression, it's not an id-expression or
15076 type
= finish_decltype_type (type
, id
, complain
);
15078 return cp_build_qualified_type_real (type
,
15080 | cp_type_quals (type
),
15081 complain
| tf_ignore_bad_quals
);
15084 case UNDERLYING_TYPE
:
15086 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15087 complain
, in_decl
);
15088 return finish_underlying_type (type
);
15091 case TYPE_ARGUMENT_PACK
:
15092 case NONTYPE_ARGUMENT_PACK
:
15096 if (code
== NONTYPE_ARGUMENT_PACK
)
15097 r
= make_node (code
);
15099 r
= cxx_make_type (code
);
15101 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15102 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15103 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15121 /* We should use one of the expression tsubsts for these codes. */
15122 gcc_unreachable ();
15125 sorry ("use of %qs in template", get_tree_code_name (code
));
15126 return error_mark_node
;
15130 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15131 expression on the left-hand side of the "." or "->" operator. We
15132 only do the lookup if we had a dependent BASELINK. Otherwise we
15133 adjust it onto the instantiated heirarchy. */
15136 tsubst_baselink (tree baselink
, tree object_type
,
15137 tree args
, tsubst_flags_t complain
, tree in_decl
)
15139 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15140 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15141 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15143 tree optype
= BASELINK_OPTYPE (baselink
);
15144 optype
= tsubst (optype
, args
, complain
, in_decl
);
15146 tree template_args
= NULL_TREE
;
15147 bool template_id_p
= false;
15148 tree fns
= BASELINK_FUNCTIONS (baselink
);
15149 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15151 template_id_p
= true;
15152 template_args
= TREE_OPERAND (fns
, 1);
15153 fns
= TREE_OPERAND (fns
, 0);
15155 template_args
= tsubst_template_args (template_args
, args
,
15156 complain
, in_decl
);
15159 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15160 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15161 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15165 tree name
= OVL_NAME (fns
);
15166 if (IDENTIFIER_CONV_OP_P (name
))
15167 name
= make_conv_op_name (optype
);
15169 if (name
== complete_dtor_identifier
)
15170 /* Treat as-if non-dependent below. */
15171 dependent_p
= false;
15173 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15176 if ((complain
& tf_error
)
15177 && constructor_name_p (name
, qualifying_scope
))
15178 error ("cannot call constructor %<%T::%D%> directly",
15179 qualifying_scope
, name
);
15180 return error_mark_node
;
15183 if (BASELINK_P (baselink
))
15184 fns
= BASELINK_FUNCTIONS (baselink
);
15187 /* We're going to overwrite pieces below, make a duplicate. */
15188 baselink
= copy_node (baselink
);
15190 /* If lookup found a single function, mark it as used at this point.
15191 (If lookup found multiple functions the one selected later by
15192 overload resolution will be marked as used at that point.) */
15193 if (!template_id_p
&& !really_overloaded_fn (fns
))
15195 tree fn
= OVL_FIRST (fns
);
15196 bool ok
= mark_used (fn
, complain
);
15197 if (!ok
&& !(complain
& tf_error
))
15198 return error_mark_node
;
15199 if (ok
&& BASELINK_P (baselink
))
15200 /* We might have instantiated an auto function. */
15201 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15204 if (BASELINK_P (baselink
))
15206 /* Add back the template arguments, if present. */
15208 BASELINK_FUNCTIONS (baselink
)
15209 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15211 /* Update the conversion operator type. */
15212 BASELINK_OPTYPE (baselink
) = optype
;
15216 object_type
= current_class_type
;
15218 if (qualified_p
|| !dependent_p
)
15220 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15224 /* We need to call adjust_result_of_qualified_name_lookup in case the
15225 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15226 so that we still get virtual function binding. */
15227 BASELINK_QUALIFIED_P (baselink
) = false;
15233 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15234 true if the qualified-id will be a postfix-expression in-and-of
15235 itself; false if more of the postfix-expression follows the
15236 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15240 tsubst_qualified_id (tree qualified_id
, tree args
,
15241 tsubst_flags_t complain
, tree in_decl
,
15242 bool done
, bool address_p
)
15248 tree template_args
;
15249 location_t loc
= UNKNOWN_LOCATION
;
15251 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15253 /* Figure out what name to look up. */
15254 name
= TREE_OPERAND (qualified_id
, 1);
15255 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15257 is_template
= true;
15258 loc
= EXPR_LOCATION (name
);
15259 template_args
= TREE_OPERAND (name
, 1);
15261 template_args
= tsubst_template_args (template_args
, args
,
15262 complain
, in_decl
);
15263 if (template_args
== error_mark_node
)
15264 return error_mark_node
;
15265 name
= TREE_OPERAND (name
, 0);
15269 is_template
= false;
15270 template_args
= NULL_TREE
;
15273 /* Substitute into the qualifying scope. When there are no ARGS, we
15274 are just trying to simplify a non-dependent expression. In that
15275 case the qualifying scope may be dependent, and, in any case,
15276 substituting will not help. */
15277 scope
= TREE_OPERAND (qualified_id
, 0);
15280 scope
= tsubst (scope
, args
, complain
, in_decl
);
15281 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15286 if (dependent_scope_p (scope
))
15289 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15290 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15291 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15292 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15296 if (!BASELINK_P (name
) && !DECL_P (expr
))
15298 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15300 /* A BIT_NOT_EXPR is used to represent a destructor. */
15301 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15303 error ("qualifying type %qT does not match destructor name ~%qT",
15304 scope
, TREE_OPERAND (expr
, 0));
15305 expr
= error_mark_node
;
15308 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15309 /*is_type_p=*/0, false);
15312 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15313 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15314 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15316 if (complain
& tf_error
)
15318 error ("dependent-name %qE is parsed as a non-type, but "
15319 "instantiation yields a type", qualified_id
);
15320 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15322 return error_mark_node
;
15328 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15330 /* Remember that there was a reference to this entity. */
15331 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15332 return error_mark_node
;
15335 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15337 if (complain
& tf_error
)
15338 qualified_name_lookup_error (scope
,
15339 TREE_OPERAND (qualified_id
, 1),
15340 expr
, input_location
);
15341 return error_mark_node
;
15346 /* We may be repeating a check already done during parsing, but
15347 if it was well-formed and passed then, it will pass again
15348 now, and if it didn't, we wouldn't have got here. The case
15349 we want to catch is when we couldn't tell then, and can now,
15350 namely when templ prior to substitution was an
15352 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15353 return error_mark_node
;
15355 if (variable_template_p (expr
))
15356 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15359 expr
= lookup_template_function (expr
, template_args
);
15362 if (expr
== error_mark_node
&& complain
& tf_error
)
15363 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15364 expr
, input_location
);
15365 else if (TYPE_P (scope
))
15367 expr
= (adjust_result_of_qualified_name_lookup
15368 (expr
, scope
, current_nonlambda_class_type ()));
15369 expr
= (finish_qualified_id_expr
15370 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15371 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15372 /*template_arg_p=*/false, complain
));
15375 /* Expressions do not generally have reference type. */
15376 if (TREE_CODE (expr
) != SCOPE_REF
15377 /* However, if we're about to form a pointer-to-member, we just
15378 want the referenced member referenced. */
15379 && TREE_CODE (expr
) != OFFSET_REF
)
15380 expr
= convert_from_reference (expr
);
15382 if (REF_PARENTHESIZED_P (qualified_id
))
15383 expr
= force_paren_expr (expr
);
15388 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15389 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15393 tsubst_init (tree init
, tree decl
, tree args
,
15394 tsubst_flags_t complain
, tree in_decl
)
15399 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15401 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
15403 /* If we had an initializer but it
15404 instantiated to nothing,
15405 value-initialize the object. This will
15406 only occur when the initializer was a
15407 pack expansion where the parameter packs
15408 used in that expansion were of length
15410 init
= build_value_init (TREE_TYPE (decl
),
15412 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15413 init
= get_target_expr_sfinae (init
, complain
);
15414 if (TREE_CODE (init
) == TARGET_EXPR
)
15415 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15421 /* Like tsubst, but deals with expressions. This function just replaces
15422 template parms; to finish processing the resultant expression, use
15423 tsubst_copy_and_build or tsubst_expr. */
15426 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15428 enum tree_code code
;
15431 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15434 code
= TREE_CODE (t
);
15439 r
= retrieve_local_specialization (t
);
15441 if (r
== NULL_TREE
)
15443 /* We get here for a use of 'this' in an NSDMI. */
15444 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15445 return current_class_ptr
;
15447 /* This can happen for a parameter name used later in a function
15448 declaration (such as in a late-specified return type). Just
15449 make a dummy decl, since it's only used for its type. */
15450 gcc_assert (cp_unevaluated_operand
!= 0);
15451 r
= tsubst_decl (t
, args
, complain
);
15452 /* Give it the template pattern as its context; its true context
15453 hasn't been instantiated yet and this is good enough for
15455 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15458 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15459 r
= argument_pack_select_arg (r
);
15460 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15461 return error_mark_node
;
15469 if (DECL_TEMPLATE_PARM_P (t
))
15470 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15471 /* There is no need to substitute into namespace-scope
15473 if (DECL_NAMESPACE_SCOPE_P (t
))
15475 /* If ARGS is NULL, then T is known to be non-dependent. */
15476 if (args
== NULL_TREE
)
15477 return scalar_constant_value (t
);
15479 /* Unfortunately, we cannot just call lookup_name here.
15482 template <int I> int f() {
15484 struct S { void g() { E e = a; } };
15487 When we instantiate f<7>::S::g(), say, lookup_name is not
15488 clever enough to find f<7>::a. */
15490 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15491 /*entering_scope=*/0);
15493 for (v
= TYPE_VALUES (enum_type
);
15495 v
= TREE_CHAIN (v
))
15496 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15497 return TREE_VALUE (v
);
15499 /* We didn't find the name. That should never happen; if
15500 name-lookup found it during preliminary parsing, we
15501 should find it again here during instantiation. */
15502 gcc_unreachable ();
15507 if (DECL_CONTEXT (t
))
15511 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15512 /*entering_scope=*/1);
15513 if (ctx
!= DECL_CONTEXT (t
))
15515 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15518 if (complain
& tf_error
)
15519 error ("using invalid field %qD", t
);
15520 return error_mark_node
;
15529 case FUNCTION_DECL
:
15530 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15531 r
= tsubst (t
, args
, complain
, in_decl
);
15532 else if (local_variable_p (t
)
15533 && uses_template_parms (DECL_CONTEXT (t
)))
15535 r
= retrieve_local_specialization (t
);
15536 if (r
== NULL_TREE
)
15538 /* First try name lookup to find the instantiation. */
15539 r
= lookup_name (DECL_NAME (t
));
15540 if (r
&& !is_capture_proxy (r
))
15542 /* Make sure that the one we found is the one we want. */
15543 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15544 if (ctx
!= DECL_CONTEXT (r
))
15552 /* This can happen for a variable used in a
15553 late-specified return type of a local lambda, or for a
15554 local static or constant. Building a new VAR_DECL
15555 should be OK in all those cases. */
15556 r
= tsubst_decl (t
, args
, complain
);
15557 if (local_specializations
)
15558 /* Avoid infinite recursion (79640). */
15559 register_local_specialization (r
, t
);
15560 if (decl_maybe_constant_var_p (r
))
15562 /* We can't call cp_finish_decl, so handle the
15563 initializer by hand. */
15564 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15565 complain
, in_decl
);
15566 if (!processing_template_decl
)
15567 init
= maybe_constant_init (init
);
15568 if (processing_template_decl
15569 ? potential_constant_expression (init
)
15570 : reduced_constant_expression_p (init
))
15571 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15572 = TREE_CONSTANT (r
) = true;
15573 DECL_INITIAL (r
) = init
;
15574 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15576 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15577 complain
, adc_variable_type
);
15579 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15580 || decl_constant_var_p (r
)
15581 || errorcount
|| sorrycount
);
15582 if (!processing_template_decl
15583 && !TREE_STATIC (r
))
15584 r
= process_outer_var_ref (r
, complain
);
15586 /* Remember this for subsequent uses. */
15587 if (local_specializations
)
15588 register_local_specialization (r
, t
);
15590 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15591 r
= argument_pack_select_arg (r
);
15595 if (!mark_used (r
, complain
))
15596 return error_mark_node
;
15599 case NAMESPACE_DECL
:
15606 return tsubst_baselink (t
, current_nonlambda_class_type (),
15607 args
, complain
, in_decl
);
15609 case TEMPLATE_DECL
:
15610 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15611 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15612 args
, complain
, in_decl
);
15613 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15614 return tsubst (t
, args
, complain
, in_decl
);
15615 else if (DECL_CLASS_SCOPE_P (t
)
15616 && uses_template_parms (DECL_CONTEXT (t
)))
15618 /* Template template argument like the following example need
15621 template <template <class> class TT> struct C {};
15622 template <class T> struct D {
15623 template <class U> struct E {};
15628 We are processing the template argument `E' in #1 for
15629 the template instantiation #2. Originally, `E' is a
15630 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15631 have to substitute this with one having context `D<int>'. */
15633 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15634 if (dependent_scope_p (context
))
15636 /* When rewriting a constructor into a deduction guide, a
15637 non-dependent name can become dependent, so memtmpl<args>
15638 becomes context::template memtmpl<args>. */
15639 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15640 return build_qualified_name (type
, context
, DECL_NAME (t
),
15643 return lookup_field (context
, DECL_NAME(t
), 0, false);
15646 /* Ordinary template template argument. */
15649 case NON_LVALUE_EXPR
:
15650 case VIEW_CONVERT_EXPR
:
15652 /* Handle location wrappers by substituting the wrapped node
15653 first, *then* reusing the resulting type. Doing the type
15654 first ensures that we handle template parameters and
15655 parameter pack expansions. */
15656 if (location_wrapper_p (t
))
15658 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
15659 complain
, in_decl
);
15660 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15662 tree op
= TREE_OPERAND (t
, 0);
15663 if (code
== VIEW_CONVERT_EXPR
15664 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15666 /* Wrapper to make a C++20 template parameter object const. */
15667 op
= tsubst_copy (op
, args
, complain
, in_decl
);
15668 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15670 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15671 return build1 (code
, type
, op
);
15675 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
15679 /* We shouldn't see any other uses of these in templates. */
15680 gcc_unreachable ();
15684 case REINTERPRET_CAST_EXPR
:
15685 case CONST_CAST_EXPR
:
15686 case STATIC_CAST_EXPR
:
15687 case DYNAMIC_CAST_EXPR
:
15688 case IMPLICIT_CONV_EXPR
:
15692 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15693 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15694 return build1 (code
, type
, op0
);
15698 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15699 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15701 tree expanded
, op
= TREE_OPERAND (t
, 0);
15704 if (SIZEOF_EXPR_TYPE_P (t
))
15705 op
= TREE_TYPE (op
);
15707 ++cp_unevaluated_operand
;
15708 ++c_inhibit_evaluation_warnings
;
15709 /* We only want to compute the number of arguments. */
15710 if (PACK_EXPANSION_P (op
))
15711 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15713 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15714 args
, complain
, in_decl
);
15715 --cp_unevaluated_operand
;
15716 --c_inhibit_evaluation_warnings
;
15718 if (TREE_CODE (expanded
) == TREE_VEC
)
15720 len
= TREE_VEC_LENGTH (expanded
);
15721 /* Set TREE_USED for the benefit of -Wunused. */
15722 for (int i
= 0; i
< len
; i
++)
15723 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15724 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15727 if (expanded
== error_mark_node
)
15728 return error_mark_node
;
15729 else if (PACK_EXPANSION_P (expanded
)
15730 || (TREE_CODE (expanded
) == TREE_VEC
15731 && pack_expansion_args_count (expanded
)))
15734 if (PACK_EXPANSION_P (expanded
))
15736 else if (TREE_VEC_LENGTH (expanded
) == 1)
15737 expanded
= TREE_VEC_ELT (expanded
, 0);
15739 expanded
= make_argument_pack (expanded
);
15741 if (TYPE_P (expanded
))
15742 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15744 complain
& tf_error
);
15746 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15747 complain
& tf_error
);
15750 return build_int_cst (size_type_node
, len
);
15752 if (SIZEOF_EXPR_TYPE_P (t
))
15754 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15755 args
, complain
, in_decl
);
15756 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15757 r
= build1 (SIZEOF_EXPR
,
15758 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15759 SIZEOF_EXPR_TYPE_P (r
) = 1;
15766 case TRUTH_NOT_EXPR
:
15769 case UNARY_PLUS_EXPR
: /* Unary + */
15771 case AT_ENCODE_EXPR
:
15775 case REALPART_EXPR
:
15776 case IMAGPART_EXPR
:
15779 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15780 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15781 r
= build1 (code
, type
, op0
);
15782 if (code
== ALIGNOF_EXPR
)
15783 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15787 case COMPONENT_REF
:
15792 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15793 name
= TREE_OPERAND (t
, 1);
15794 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15796 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15797 complain
, in_decl
);
15798 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15800 else if (TREE_CODE (name
) == SCOPE_REF
15801 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15803 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15804 complain
, in_decl
);
15805 name
= TREE_OPERAND (name
, 1);
15806 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15807 complain
, in_decl
);
15808 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15809 name
= build_qualified_name (/*type=*/NULL_TREE
,
15811 /*template_p=*/false);
15813 else if (BASELINK_P (name
))
15814 name
= tsubst_baselink (name
,
15815 non_reference (TREE_TYPE (object
)),
15819 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15820 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15826 case TRUNC_DIV_EXPR
:
15827 case CEIL_DIV_EXPR
:
15828 case FLOOR_DIV_EXPR
:
15829 case ROUND_DIV_EXPR
:
15830 case EXACT_DIV_EXPR
:
15834 case TRUNC_MOD_EXPR
:
15835 case FLOOR_MOD_EXPR
:
15836 case TRUTH_ANDIF_EXPR
:
15837 case TRUTH_ORIF_EXPR
:
15838 case TRUTH_AND_EXPR
:
15839 case TRUTH_OR_EXPR
:
15852 case COMPOUND_EXPR
:
15855 case PREDECREMENT_EXPR
:
15856 case PREINCREMENT_EXPR
:
15857 case POSTDECREMENT_EXPR
:
15858 case POSTINCREMENT_EXPR
:
15860 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15861 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15862 return build_nt (code
, op0
, op1
);
15867 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15868 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15869 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15870 QUALIFIED_NAME_IS_TEMPLATE (t
));
15875 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15876 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15877 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15882 int n
= VL_EXP_OPERAND_LENGTH (t
);
15883 tree result
= build_vl_exp (CALL_EXPR
, n
);
15885 for (i
= 0; i
< n
; i
++)
15886 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15887 complain
, in_decl
);
15893 case PSEUDO_DTOR_EXPR
:
15894 case VEC_PERM_EXPR
:
15896 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15897 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15898 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15899 r
= build_nt (code
, op0
, op1
, op2
);
15900 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15906 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15907 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15908 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15909 r
= build_nt (code
, op0
, op1
, op2
);
15910 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
15916 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15917 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15918 r
= build_nt (code
, op0
, op1
);
15919 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
15920 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
15924 case TEMPLATE_ID_EXPR
:
15926 /* Substituted template arguments */
15927 tree fn
= TREE_OPERAND (t
, 0);
15928 tree targs
= TREE_OPERAND (t
, 1);
15930 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
15932 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
15934 return lookup_template_function (fn
, targs
);
15939 tree purpose
, value
, chain
;
15941 if (t
== void_list_node
)
15944 purpose
= TREE_PURPOSE (t
);
15946 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15947 value
= TREE_VALUE (t
);
15949 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15950 chain
= TREE_CHAIN (t
);
15951 if (chain
&& chain
!= void_type_node
)
15952 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15953 if (purpose
== TREE_PURPOSE (t
)
15954 && value
== TREE_VALUE (t
)
15955 && chain
== TREE_CHAIN (t
))
15957 return tree_cons (purpose
, value
, chain
);
15962 case ENUMERAL_TYPE
:
15964 case TEMPLATE_TYPE_PARM
:
15965 case TEMPLATE_TEMPLATE_PARM
:
15966 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15967 case TEMPLATE_PARM_INDEX
:
15969 case REFERENCE_TYPE
:
15971 case FUNCTION_TYPE
:
15974 case TYPENAME_TYPE
:
15975 case UNBOUND_CLASS_TEMPLATE
:
15977 case DECLTYPE_TYPE
:
15979 return tsubst (t
, args
, complain
, in_decl
);
15983 /* Fall through. */
15984 case IDENTIFIER_NODE
:
15985 if (IDENTIFIER_CONV_OP_P (t
))
15987 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15988 return make_conv_op_name (new_type
);
15994 /* This is handled by tsubst_copy_and_build. */
15995 gcc_unreachable ();
15999 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16000 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16001 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16004 case CLEANUP_POINT_EXPR
:
16005 /* We shouldn't have built any of these during initial template
16006 generation. Instead, they should be built during instantiation
16007 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16008 gcc_unreachable ();
16012 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16013 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16014 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16015 r
= build2 (code
, type
, op0
, op1
);
16016 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16017 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16018 && !(complain
& tf_error
))
16019 return error_mark_node
;
16023 case EXPR_PACK_EXPANSION
:
16024 error ("invalid use of pack expansion expression");
16025 return error_mark_node
;
16027 case NONTYPE_ARGUMENT_PACK
:
16028 error ("use %<...%> to expand argument pack");
16029 return error_mark_node
;
16032 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16040 /* Instantiate any typedefs in the type. */
16041 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16042 r
= fold_convert (type
, t
);
16043 gcc_assert (TREE_CODE (r
) == code
);
16048 /* These can sometimes show up in a partial instantiation, but never
16049 involve template parms. */
16050 gcc_assert (!uses_template_parms (t
));
16053 case UNARY_LEFT_FOLD_EXPR
:
16054 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16055 case UNARY_RIGHT_FOLD_EXPR
:
16056 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16057 case BINARY_LEFT_FOLD_EXPR
:
16058 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16059 case BINARY_RIGHT_FOLD_EXPR
:
16060 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16064 case DEBUG_BEGIN_STMT
:
16065 /* ??? There's no point in copying it for now, but maybe some
16066 day it will contain more information, such as a pointer back
16067 to the containing function, inlined copy or so. */
16071 /* We shouldn't get here, but keep going if !flag_checking. */
16073 gcc_unreachable ();
16078 /* Helper function for tsubst_omp_clauses, used for instantiation of
16079 OMP_CLAUSE_DECL of clauses. */
16082 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16083 tree in_decl
, tree
*iterator_cache
)
16085 if (decl
== NULL_TREE
)
16088 /* Handle OpenMP iterators. */
16089 if (TREE_CODE (decl
) == TREE_LIST
16090 && TREE_PURPOSE (decl
)
16091 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16094 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16095 ret
= iterator_cache
[1];
16099 begin_scope (sk_omp
, NULL
);
16100 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16102 *tp
= copy_node (it
);
16103 TREE_VEC_ELT (*tp
, 0)
16104 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16105 TREE_VEC_ELT (*tp
, 1)
16106 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16107 /*integral_constant_expression_p=*/false);
16108 TREE_VEC_ELT (*tp
, 2)
16109 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16110 /*integral_constant_expression_p=*/false);
16111 TREE_VEC_ELT (*tp
, 3)
16112 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16113 /*integral_constant_expression_p=*/false);
16114 TREE_CHAIN (*tp
) = NULL_TREE
;
16115 tp
= &TREE_CHAIN (*tp
);
16117 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16118 iterator_cache
[0] = TREE_PURPOSE (decl
);
16119 iterator_cache
[1] = ret
;
16121 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16126 /* Handle an OpenMP array section represented as a TREE_LIST (or
16127 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16128 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16129 TREE_LIST. We can handle it exactly the same as an array section
16130 (purpose, value, and a chain), even though the nomenclature
16131 (low_bound, length, etc) is different. */
16132 if (TREE_CODE (decl
) == TREE_LIST
)
16135 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16136 /*integral_constant_expression_p=*/false);
16137 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16138 /*integral_constant_expression_p=*/false);
16139 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16141 if (TREE_PURPOSE (decl
) == low_bound
16142 && TREE_VALUE (decl
) == length
16143 && TREE_CHAIN (decl
) == chain
)
16145 tree ret
= tree_cons (low_bound
, length
, chain
);
16146 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16147 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16150 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16151 /*integral_constant_expression_p=*/false);
16152 /* Undo convert_from_reference tsubst_expr could have called. */
16154 && REFERENCE_REF_P (ret
)
16155 && !REFERENCE_REF_P (decl
))
16156 ret
= TREE_OPERAND (ret
, 0);
16160 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16163 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16164 tree args
, tsubst_flags_t complain
, tree in_decl
)
16166 tree new_clauses
= NULL_TREE
, nc
, oc
;
16167 tree linear_no_step
= NULL_TREE
;
16168 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16170 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16172 nc
= copy_node (oc
);
16173 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16176 switch (OMP_CLAUSE_CODE (nc
))
16178 case OMP_CLAUSE_LASTPRIVATE
:
16179 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16181 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16182 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16183 in_decl
, /*integral_constant_expression_p=*/false);
16184 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16185 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16188 case OMP_CLAUSE_PRIVATE
:
16189 case OMP_CLAUSE_SHARED
:
16190 case OMP_CLAUSE_FIRSTPRIVATE
:
16191 case OMP_CLAUSE_COPYIN
:
16192 case OMP_CLAUSE_COPYPRIVATE
:
16193 case OMP_CLAUSE_UNIFORM
:
16194 case OMP_CLAUSE_DEPEND
:
16195 case OMP_CLAUSE_FROM
:
16196 case OMP_CLAUSE_TO
:
16197 case OMP_CLAUSE_MAP
:
16198 case OMP_CLAUSE_NONTEMPORAL
:
16199 case OMP_CLAUSE_USE_DEVICE_PTR
:
16200 case OMP_CLAUSE_IS_DEVICE_PTR
:
16201 OMP_CLAUSE_DECL (nc
)
16202 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16203 in_decl
, iterator_cache
);
16205 case OMP_CLAUSE_TILE
:
16206 case OMP_CLAUSE_IF
:
16207 case OMP_CLAUSE_NUM_THREADS
:
16208 case OMP_CLAUSE_SCHEDULE
:
16209 case OMP_CLAUSE_COLLAPSE
:
16210 case OMP_CLAUSE_FINAL
:
16211 case OMP_CLAUSE_DEVICE
:
16212 case OMP_CLAUSE_DIST_SCHEDULE
:
16213 case OMP_CLAUSE_NUM_TEAMS
:
16214 case OMP_CLAUSE_THREAD_LIMIT
:
16215 case OMP_CLAUSE_SAFELEN
:
16216 case OMP_CLAUSE_SIMDLEN
:
16217 case OMP_CLAUSE_NUM_TASKS
:
16218 case OMP_CLAUSE_GRAINSIZE
:
16219 case OMP_CLAUSE_PRIORITY
:
16220 case OMP_CLAUSE_ORDERED
:
16221 case OMP_CLAUSE_HINT
:
16222 case OMP_CLAUSE_NUM_GANGS
:
16223 case OMP_CLAUSE_NUM_WORKERS
:
16224 case OMP_CLAUSE_VECTOR_LENGTH
:
16225 case OMP_CLAUSE_WORKER
:
16226 case OMP_CLAUSE_VECTOR
:
16227 case OMP_CLAUSE_ASYNC
:
16228 case OMP_CLAUSE_WAIT
:
16229 OMP_CLAUSE_OPERAND (nc
, 0)
16230 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16231 in_decl
, /*integral_constant_expression_p=*/false);
16233 case OMP_CLAUSE_REDUCTION
:
16234 case OMP_CLAUSE_IN_REDUCTION
:
16235 case OMP_CLAUSE_TASK_REDUCTION
:
16236 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16238 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16239 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16241 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16242 complain
, in_decl
);
16243 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16244 = build_qualified_name (NULL_TREE
, scope
,
16245 TREE_OPERAND (placeholder
, 1),
16249 gcc_assert (identifier_p (placeholder
));
16251 OMP_CLAUSE_DECL (nc
)
16252 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16255 case OMP_CLAUSE_GANG
:
16256 case OMP_CLAUSE_ALIGNED
:
16257 OMP_CLAUSE_DECL (nc
)
16258 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16260 OMP_CLAUSE_OPERAND (nc
, 1)
16261 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16262 in_decl
, /*integral_constant_expression_p=*/false);
16264 case OMP_CLAUSE_LINEAR
:
16265 OMP_CLAUSE_DECL (nc
)
16266 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16268 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16270 gcc_assert (!linear_no_step
);
16271 linear_no_step
= nc
;
16273 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16274 OMP_CLAUSE_LINEAR_STEP (nc
)
16275 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16276 complain
, in_decl
, NULL
);
16278 OMP_CLAUSE_LINEAR_STEP (nc
)
16279 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16281 /*integral_constant_expression_p=*/false);
16283 case OMP_CLAUSE_NOWAIT
:
16284 case OMP_CLAUSE_DEFAULT
:
16285 case OMP_CLAUSE_UNTIED
:
16286 case OMP_CLAUSE_MERGEABLE
:
16287 case OMP_CLAUSE_INBRANCH
:
16288 case OMP_CLAUSE_NOTINBRANCH
:
16289 case OMP_CLAUSE_PROC_BIND
:
16290 case OMP_CLAUSE_FOR
:
16291 case OMP_CLAUSE_PARALLEL
:
16292 case OMP_CLAUSE_SECTIONS
:
16293 case OMP_CLAUSE_TASKGROUP
:
16294 case OMP_CLAUSE_NOGROUP
:
16295 case OMP_CLAUSE_THREADS
:
16296 case OMP_CLAUSE_SIMD
:
16297 case OMP_CLAUSE_DEFAULTMAP
:
16298 case OMP_CLAUSE_INDEPENDENT
:
16299 case OMP_CLAUSE_AUTO
:
16300 case OMP_CLAUSE_SEQ
:
16301 case OMP_CLAUSE_IF_PRESENT
:
16302 case OMP_CLAUSE_FINALIZE
:
16305 gcc_unreachable ();
16307 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16308 switch (OMP_CLAUSE_CODE (nc
))
16310 case OMP_CLAUSE_SHARED
:
16311 case OMP_CLAUSE_PRIVATE
:
16312 case OMP_CLAUSE_FIRSTPRIVATE
:
16313 case OMP_CLAUSE_LASTPRIVATE
:
16314 case OMP_CLAUSE_COPYPRIVATE
:
16315 case OMP_CLAUSE_LINEAR
:
16316 case OMP_CLAUSE_REDUCTION
:
16317 case OMP_CLAUSE_IN_REDUCTION
:
16318 case OMP_CLAUSE_TASK_REDUCTION
:
16319 case OMP_CLAUSE_USE_DEVICE_PTR
:
16320 case OMP_CLAUSE_IS_DEVICE_PTR
:
16321 /* tsubst_expr on SCOPE_REF results in returning
16322 finish_non_static_data_member result. Undo that here. */
16323 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16324 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16325 == IDENTIFIER_NODE
))
16327 tree t
= OMP_CLAUSE_DECL (nc
);
16330 switch (TREE_CODE (v
))
16332 case COMPONENT_REF
:
16336 case POINTER_PLUS_EXPR
:
16337 v
= TREE_OPERAND (v
, 0);
16340 if (DECL_CONTEXT (v
) == current_function_decl
16341 && DECL_ARTIFICIAL (v
)
16342 && DECL_NAME (v
) == this_identifier
)
16343 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16350 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16351 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16352 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16353 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16354 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16356 tree decl
= OMP_CLAUSE_DECL (nc
);
16359 retrofit_lang_decl (decl
);
16360 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16369 new_clauses
= nreverse (new_clauses
);
16370 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16372 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16373 if (linear_no_step
)
16374 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16375 if (nc
== linear_no_step
)
16377 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16381 return new_clauses
;
16384 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16387 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16390 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16392 tree purpose
, value
, chain
;
16397 if (TREE_CODE (t
) != TREE_LIST
)
16398 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16399 /*function_p=*/false,
16400 /*integral_constant_expression_p=*/false);
16402 if (t
== void_list_node
)
16405 purpose
= TREE_PURPOSE (t
);
16407 purpose
= RECUR (purpose
);
16408 value
= TREE_VALUE (t
);
16411 if (TREE_CODE (value
) != LABEL_DECL
)
16412 value
= RECUR (value
);
16415 value
= lookup_label (DECL_NAME (value
));
16416 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16417 TREE_USED (value
) = 1;
16420 chain
= TREE_CHAIN (t
);
16421 if (chain
&& chain
!= void_type_node
)
16422 chain
= RECUR (chain
);
16423 return tree_cons (purpose
, value
, chain
);
16427 /* Used to temporarily communicate the list of #pragma omp parallel
16428 clauses to #pragma omp for instantiation if they are combined
16431 static tree
*omp_parallel_combined_clauses
;
16433 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
16434 tree
*, unsigned int *);
16436 /* Substitute one OMP_FOR iterator. */
16439 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
16440 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16441 tree args
, tsubst_flags_t complain
, tree in_decl
,
16442 bool integral_constant_expression_p
)
16444 #define RECUR(NODE) \
16445 tsubst_expr ((NODE), args, complain, in_decl, \
16446 integral_constant_expression_p)
16447 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
16450 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16451 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16453 decl
= TREE_OPERAND (init
, 0);
16454 init
= TREE_OPERAND (init
, 1);
16455 tree decl_expr
= NULL_TREE
;
16456 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
16459 bool decomp
= false;
16460 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
16462 tree v
= DECL_VALUE_EXPR (decl
);
16463 if (TREE_CODE (v
) == ARRAY_REF
16464 && VAR_P (TREE_OPERAND (v
, 0))
16465 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
16467 tree decomp_first
= NULL_TREE
;
16468 unsigned decomp_cnt
= 0;
16469 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
16470 maybe_push_decl (d
);
16471 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
16472 in_decl
, &decomp_first
, &decomp_cnt
);
16474 if (d
== error_mark_node
)
16475 decl
= error_mark_node
;
16477 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
16479 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
16481 tree v
= build_nt (ARRAY_REF
, d
,
16482 size_int (decomp_cnt
- i
- 1),
16483 NULL_TREE
, NULL_TREE
);
16484 SET_DECL_VALUE_EXPR (decomp_first
, v
);
16485 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
16487 fit_decomposition_lang_decl (decomp_first
, d
);
16488 decomp_first
= DECL_CHAIN (decomp_first
);
16492 decl
= tsubst_decl (decl
, args
, complain
);
16494 maybe_push_decl (decl
);
16496 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16498 /* We need to jump through some hoops to handle declarations in the
16499 init-statement, since we might need to handle auto deduction,
16500 but we need to keep control of initialization. */
16502 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16503 decl
= tsubst_decl (decl
, args
, complain
);
16507 if (TREE_CODE (decl
) == SCOPE_REF
)
16509 decl
= RECUR (decl
);
16510 if (TREE_CODE (decl
) == COMPONENT_REF
)
16514 switch (TREE_CODE (v
))
16516 case COMPONENT_REF
:
16520 case POINTER_PLUS_EXPR
:
16521 v
= TREE_OPERAND (v
, 0);
16524 if (DECL_CONTEXT (v
) == current_function_decl
16525 && DECL_ARTIFICIAL (v
)
16526 && DECL_NAME (v
) == this_identifier
)
16528 decl
= TREE_OPERAND (decl
, 1);
16529 decl
= omp_privatize_field (decl
, false);
16539 decl
= RECUR (decl
);
16541 init
= RECUR (init
);
16543 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16545 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16546 if (TREE_CODE (o
) == TREE_LIST
)
16547 TREE_VEC_ELT (orig_declv
, i
)
16548 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16549 RECUR (TREE_VALUE (o
)),
16552 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16557 tree this_pre_body
= NULL_TREE
;
16558 tree orig_init
= NULL_TREE
;
16559 tree orig_decl
= NULL_TREE
;
16560 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
16561 orig_init
, cond
, incr
);
16564 if (orig_declv
== NULL_TREE
)
16565 orig_declv
= copy_node (declv
);
16566 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
16569 else if (orig_declv
)
16570 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16573 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16574 if (!range_for
&& auto_node
&& init
)
16576 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16578 gcc_assert (!type_dependent_expression_p (decl
));
16580 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
16584 /* Declare the variable, but don't let that initialize it. */
16585 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16586 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16588 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16593 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16594 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16595 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16597 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16598 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16599 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16600 NOP_EXPR
, rhs
, complain
);
16603 incr
= RECUR (incr
);
16604 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16605 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16607 TREE_VEC_ELT (declv
, i
) = decl
;
16608 TREE_VEC_ELT (initv
, i
) = init
;
16609 TREE_VEC_ELT (condv
, i
) = cond
;
16610 TREE_VEC_ELT (incrv
, i
) = incr
;
16616 /* Declare and initialize the variable. */
16624 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16626 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16628 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16629 && OMP_CLAUSE_DECL (*pc
) == decl
)
16631 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16632 && OMP_CLAUSE_DECL (*pc
) == decl
)
16636 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16638 *pc
= OMP_CLAUSE_CHAIN (c
);
16639 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16642 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16643 && OMP_CLAUSE_DECL (*pc
) == decl
)
16645 error ("iteration variable %qD should not be firstprivate",
16647 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16649 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16650 && OMP_CLAUSE_DECL (*pc
) == decl
)
16652 error ("iteration variable %qD should not be reduction",
16654 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16657 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16662 if (*pc
== NULL_TREE
)
16664 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16665 OMP_CLAUSE_DECL (c
) = decl
;
16666 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16669 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16674 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16675 if (COMPARISON_CLASS_P (cond
))
16677 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16678 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16679 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16682 cond
= RECUR (cond
);
16683 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16684 switch (TREE_CODE (incr
))
16686 case PREINCREMENT_EXPR
:
16687 case PREDECREMENT_EXPR
:
16688 case POSTINCREMENT_EXPR
:
16689 case POSTDECREMENT_EXPR
:
16690 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16691 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16694 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16695 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16697 tree rhs
= TREE_OPERAND (incr
, 1);
16698 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16699 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16700 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16701 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16702 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16706 incr
= RECUR (incr
);
16709 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16710 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16712 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16713 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16714 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16715 TREE_TYPE (decl
), lhs
,
16716 RECUR (TREE_OPERAND (incr
, 2))));
16718 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16719 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16720 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16722 tree rhs
= TREE_OPERAND (incr
, 2);
16723 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16724 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16725 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16726 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16727 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16731 incr
= RECUR (incr
);
16734 incr
= RECUR (incr
);
16738 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16739 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16740 TREE_VEC_ELT (declv
, i
) = decl
;
16741 TREE_VEC_ELT (initv
, i
) = init
;
16742 TREE_VEC_ELT (condv
, i
) = cond
;
16743 TREE_VEC_ELT (incrv
, i
) = incr
;
16748 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16749 of OMP_TARGET's body. */
16752 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16754 *walk_subtrees
= 0;
16755 switch (TREE_CODE (*tp
))
16760 case STATEMENT_LIST
:
16761 *walk_subtrees
= 1;
16769 /* Helper function for tsubst_expr. For decomposition declaration
16770 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16771 also the corresponding decls representing the identifiers
16772 of the decomposition declaration. Return DECL if successful
16773 or error_mark_node otherwise, set *FIRST to the first decl
16774 in the list chained through DECL_CHAIN and *CNT to the number
16778 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16779 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16782 tree decl2
, decl3
, prev
= decl
;
16784 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16785 for (decl2
= DECL_CHAIN (pattern_decl
);
16788 && DECL_DECOMPOSITION_P (decl2
)
16789 && DECL_NAME (decl2
);
16790 decl2
= DECL_CHAIN (decl2
))
16792 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16794 gcc_assert (errorcount
);
16795 return error_mark_node
;
16798 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16799 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16800 tree v
= DECL_VALUE_EXPR (decl2
);
16801 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16802 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16803 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16804 SET_DECL_VALUE_EXPR (decl2
, v
);
16805 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16807 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16810 gcc_assert (errorcount
);
16811 decl
= error_mark_node
;
16814 maybe_push_decl (decl3
);
16815 if (error_operand_p (decl3
))
16816 decl
= error_mark_node
;
16817 else if (decl
!= error_mark_node
16818 && DECL_CHAIN (decl3
) != prev
16821 gcc_assert (errorcount
);
16822 decl
= error_mark_node
;
16831 /* Return the proper local_specialization for init-capture pack DECL. */
16834 lookup_init_capture_pack (tree decl
)
16836 /* We handle normal pack captures by forwarding to the specialization of the
16837 captured parameter. We can't do that for pack init-captures; we need them
16838 to have their own local_specialization. We created the individual
16839 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16840 when we process the DECL_EXPR for the pack init-capture in the template.
16841 So, how do we find them? We don't know the capture proxy pack when
16842 building the individual resulting proxies, and we don't know the
16843 individual proxies when instantiating the pack. What we have in common is
16846 So...when we instantiate the FIELD_DECL, we stick the result in
16847 local_specializations. Then at the DECL_EXPR we look up that result, see
16848 how many elements it has, synthesize the names, and look them up. */
16850 tree cname
= DECL_NAME (decl
);
16851 tree val
= DECL_VALUE_EXPR (decl
);
16852 tree field
= TREE_OPERAND (val
, 1);
16853 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
16854 tree fpack
= retrieve_local_specialization (field
);
16855 if (fpack
== error_mark_node
)
16856 return error_mark_node
;
16859 tree vec
= NULL_TREE
;
16860 tree r
= NULL_TREE
;
16861 if (TREE_CODE (fpack
) == TREE_VEC
)
16863 len
= TREE_VEC_LENGTH (fpack
);
16864 vec
= make_tree_vec (len
);
16865 r
= make_node (NONTYPE_ARGUMENT_PACK
);
16866 SET_ARGUMENT_PACK_ARGS (r
, vec
);
16868 for (int i
= 0; i
< len
; ++i
)
16870 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
16871 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
16873 TREE_VEC_ELT (vec
, i
) = elt
;
16880 /* Like tsubst_copy for expressions, etc. but also does semantic
16884 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16885 bool integral_constant_expression_p
)
16887 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16888 #define RECUR(NODE) \
16889 tsubst_expr ((NODE), args, complain, in_decl, \
16890 integral_constant_expression_p)
16896 if (t
== NULL_TREE
|| t
== error_mark_node
)
16899 loc
= input_location
;
16900 if (location_t eloc
= cp_expr_location (t
))
16901 input_location
= eloc
;
16902 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16903 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16905 switch (TREE_CODE (t
))
16907 case STATEMENT_LIST
:
16909 tree_stmt_iterator i
;
16910 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16911 RECUR (tsi_stmt (i
));
16915 case CTOR_INITIALIZER
:
16916 finish_mem_initializers (tsubst_initializer_list
16917 (TREE_OPERAND (t
, 0), args
));
16921 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
16925 tmp
= RECUR (EXPR_STMT_EXPR (t
));
16926 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
16927 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
16929 finish_expr_stmt (tmp
);
16933 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
16934 /*attribs=*/NULL_TREE
);
16939 tree decl
, pattern_decl
;
16942 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
16943 if (TREE_CODE (decl
) == LABEL_DECL
)
16944 finish_label_decl (DECL_NAME (decl
));
16945 else if (TREE_CODE (decl
) == USING_DECL
)
16947 tree scope
= USING_DECL_SCOPE (decl
);
16948 tree name
= DECL_NAME (decl
);
16950 scope
= tsubst (scope
, args
, complain
, in_decl
);
16951 decl
= lookup_qualified_name (scope
, name
,
16952 /*is_type_p=*/false,
16953 /*complain=*/false);
16954 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
16955 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
16957 finish_local_using_decl (decl
, scope
, name
);
16959 else if (is_capture_proxy (decl
)
16960 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
16962 /* We're in tsubst_lambda_expr, we've already inserted a new
16963 capture proxy, so look it up and register it. */
16965 if (!DECL_PACK_P (decl
))
16967 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
16968 /*nonclass*/1, /*block_p=*/true,
16969 /*ns_only*/0, LOOKUP_HIDDEN
);
16970 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
16972 else if (is_normal_capture_proxy (decl
))
16974 inst
= (retrieve_local_specialization
16975 (DECL_CAPTURED_VARIABLE (decl
)));
16976 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
16979 inst
= lookup_init_capture_pack (decl
);
16981 register_local_specialization (inst
, decl
);
16984 else if (DECL_PRETTY_FUNCTION_P (decl
))
16985 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
16987 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16988 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
16989 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
16990 /* Don't copy the old closure; we'll create a new one in
16991 tsubst_lambda_expr. */
16995 init
= DECL_INITIAL (decl
);
16996 decl
= tsubst (decl
, args
, complain
, in_decl
);
16997 if (decl
!= error_mark_node
)
16999 /* By marking the declaration as instantiated, we avoid
17000 trying to instantiate it. Since instantiate_decl can't
17001 handle local variables, and since we've already done
17002 all that needs to be done, that's the right thing to
17005 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17006 if (VAR_P (decl
) && !DECL_NAME (decl
)
17007 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17008 /* Anonymous aggregates are a special case. */
17009 finish_anon_union (decl
);
17010 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17012 DECL_CONTEXT (decl
) = current_function_decl
;
17013 if (DECL_NAME (decl
) == this_identifier
)
17015 tree lam
= DECL_CONTEXT (current_function_decl
);
17016 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17017 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17019 insert_capture_proxy (decl
);
17021 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17022 /* We already did a pushtag. */;
17023 else if (TREE_CODE (decl
) == FUNCTION_DECL
17024 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17025 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17027 DECL_CONTEXT (decl
) = NULL_TREE
;
17029 DECL_CONTEXT (decl
) = current_function_decl
;
17030 cp_check_omp_declare_reduction (decl
);
17034 int const_init
= false;
17035 unsigned int cnt
= 0;
17036 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17037 maybe_push_decl (decl
);
17040 && DECL_DECOMPOSITION_P (decl
)
17041 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17042 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17043 complain
, in_decl
, &first
,
17046 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17049 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17052 if (ndecl
!= error_mark_node
)
17053 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17055 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
17057 if (ndecl
!= error_mark_node
)
17058 cp_finish_decomp (ndecl
, first
, cnt
);
17067 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17068 RECUR (FOR_INIT_STMT (t
));
17069 finish_init_stmt (stmt
);
17070 tmp
= RECUR (FOR_COND (t
));
17071 finish_for_cond (tmp
, stmt
, false, 0);
17072 tmp
= RECUR (FOR_EXPR (t
));
17073 finish_for_expr (tmp
, stmt
);
17075 bool prev
= note_iteration_stmt_body_start ();
17076 RECUR (FOR_BODY (t
));
17077 note_iteration_stmt_body_end (prev
);
17079 finish_for_stmt (stmt
);
17082 case RANGE_FOR_STMT
:
17084 /* Construct another range_for, if this is not a final
17085 substitution (for inside inside a generic lambda of a
17086 template). Otherwise convert to a regular for. */
17088 stmt
= (processing_template_decl
17089 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17090 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17091 RECUR (RANGE_FOR_INIT_STMT (t
));
17092 decl
= RANGE_FOR_DECL (t
);
17093 decl
= tsubst (decl
, args
, complain
, in_decl
);
17094 maybe_push_decl (decl
);
17095 expr
= RECUR (RANGE_FOR_EXPR (t
));
17097 tree decomp_first
= NULL_TREE
;
17098 unsigned decomp_cnt
= 0;
17099 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17100 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17102 &decomp_first
, &decomp_cnt
);
17104 if (processing_template_decl
)
17106 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17107 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17108 finish_range_for_decl (stmt
, decl
, expr
);
17109 if (decomp_first
&& decl
!= error_mark_node
)
17110 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17114 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17115 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17116 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17117 decomp_first
, decomp_cnt
,
17118 RANGE_FOR_IVDEP (t
), unroll
);
17121 bool prev
= note_iteration_stmt_body_start ();
17122 RECUR (RANGE_FOR_BODY (t
));
17123 note_iteration_stmt_body_end (prev
);
17124 finish_for_stmt (stmt
);
17129 stmt
= begin_while_stmt ();
17130 tmp
= RECUR (WHILE_COND (t
));
17131 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17133 bool prev
= note_iteration_stmt_body_start ();
17134 RECUR (WHILE_BODY (t
));
17135 note_iteration_stmt_body_end (prev
);
17137 finish_while_stmt (stmt
);
17141 stmt
= begin_do_stmt ();
17143 bool prev
= note_iteration_stmt_body_start ();
17144 RECUR (DO_BODY (t
));
17145 note_iteration_stmt_body_end (prev
);
17147 finish_do_body (stmt
);
17148 tmp
= RECUR (DO_COND (t
));
17149 finish_do_stmt (tmp
, stmt
, false, 0);
17153 stmt
= begin_if_stmt ();
17154 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17155 if (IF_STMT_CONSTEXPR_P (t
))
17156 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17157 tmp
= RECUR (IF_COND (t
));
17158 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17159 if (IF_STMT_CONSTEXPR_P (t
)
17160 && instantiation_dependent_expression_p (tmp
))
17162 /* We're partially instantiating a generic lambda, but the condition
17163 of the constexpr if is still dependent. Don't substitute into the
17164 branches now, just remember the template arguments. */
17165 do_poplevel (IF_SCOPE (stmt
));
17166 IF_COND (stmt
) = IF_COND (t
);
17167 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17168 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17169 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17173 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17174 /* Don't instantiate the THEN_CLAUSE. */;
17177 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17178 bool inhibit
= integer_zerop (folded
);
17180 ++c_inhibit_evaluation_warnings
;
17181 RECUR (THEN_CLAUSE (t
));
17183 --c_inhibit_evaluation_warnings
;
17185 finish_then_clause (stmt
);
17187 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17188 /* Don't instantiate the ELSE_CLAUSE. */;
17189 else if (ELSE_CLAUSE (t
))
17191 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17192 bool inhibit
= integer_nonzerop (folded
);
17193 begin_else_clause (stmt
);
17195 ++c_inhibit_evaluation_warnings
;
17196 RECUR (ELSE_CLAUSE (t
));
17198 --c_inhibit_evaluation_warnings
;
17199 finish_else_clause (stmt
);
17202 finish_if_stmt (stmt
);
17206 if (BIND_EXPR_BODY_BLOCK (t
))
17207 stmt
= begin_function_body ();
17209 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17210 ? BCS_TRY_BLOCK
: 0);
17212 RECUR (BIND_EXPR_BODY (t
));
17214 if (BIND_EXPR_BODY_BLOCK (t
))
17215 finish_function_body (stmt
);
17217 finish_compound_stmt (stmt
);
17221 finish_break_stmt ();
17224 case CONTINUE_STMT
:
17225 finish_continue_stmt ();
17229 stmt
= begin_switch_stmt ();
17230 tmp
= RECUR (SWITCH_STMT_COND (t
));
17231 finish_switch_cond (tmp
, stmt
);
17232 RECUR (SWITCH_STMT_BODY (t
));
17233 finish_switch_stmt (stmt
);
17236 case CASE_LABEL_EXPR
:
17238 tree decl
= CASE_LABEL (t
);
17239 tree low
= RECUR (CASE_LOW (t
));
17240 tree high
= RECUR (CASE_HIGH (t
));
17241 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17242 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17244 tree label
= CASE_LABEL (l
);
17245 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17246 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17247 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17254 tree decl
= LABEL_EXPR_LABEL (t
);
17257 label
= finish_label_stmt (DECL_NAME (decl
));
17258 if (TREE_CODE (label
) == LABEL_DECL
)
17259 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17260 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17261 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17266 tmp
= GOTO_DESTINATION (t
);
17267 if (TREE_CODE (tmp
) != LABEL_DECL
)
17268 /* Computed goto's must be tsubst'd into. On the other hand,
17269 non-computed gotos must not be; the identifier in question
17270 will have no binding. */
17273 tmp
= DECL_NAME (tmp
);
17274 finish_goto_stmt (tmp
);
17279 tree string
= RECUR (ASM_STRING (t
));
17280 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17281 complain
, in_decl
);
17282 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17283 complain
, in_decl
);
17284 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17285 complain
, in_decl
);
17286 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17287 complain
, in_decl
);
17288 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
17289 clobbers
, labels
, ASM_INLINE_P (t
));
17290 tree asm_expr
= tmp
;
17291 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17292 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17293 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17300 stmt
= begin_try_block ();
17301 RECUR (TRY_STMTS (t
));
17302 finish_cleanup_try_block (stmt
);
17303 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17307 tree compound_stmt
= NULL_TREE
;
17309 if (FN_TRY_BLOCK_P (t
))
17310 stmt
= begin_function_try_block (&compound_stmt
);
17312 stmt
= begin_try_block ();
17314 RECUR (TRY_STMTS (t
));
17316 if (FN_TRY_BLOCK_P (t
))
17317 finish_function_try_block (stmt
);
17319 finish_try_block (stmt
);
17321 RECUR (TRY_HANDLERS (t
));
17322 if (FN_TRY_BLOCK_P (t
))
17323 finish_function_handler_sequence (stmt
, compound_stmt
);
17325 finish_handler_sequence (stmt
);
17331 tree decl
= HANDLER_PARMS (t
);
17335 decl
= tsubst (decl
, args
, complain
, in_decl
);
17336 /* Prevent instantiate_decl from trying to instantiate
17337 this variable. We've already done all that needs to be
17339 if (decl
!= error_mark_node
)
17340 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17342 stmt
= begin_handler ();
17343 finish_handler_parms (decl
, stmt
);
17344 RECUR (HANDLER_BODY (t
));
17345 finish_handler (stmt
);
17350 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17351 if (CLASS_TYPE_P (tmp
))
17353 /* Local classes are not independent templates; they are
17354 instantiated along with their containing function. And this
17355 way we don't have to deal with pushing out of one local class
17356 to instantiate a member of another local class. */
17357 /* Closures are handled by the LAMBDA_EXPR. */
17358 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17359 complete_type (tmp
);
17360 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17362 || (TREE_CODE (fld
) == FUNCTION_DECL
17363 && !DECL_ARTIFICIAL (fld
)))
17364 && DECL_TEMPLATE_INSTANTIATION (fld
))
17365 instantiate_decl (fld
, /*defer_ok=*/false,
17366 /*expl_inst_class=*/false);
17370 case STATIC_ASSERT
:
17374 ++c_inhibit_evaluation_warnings
;
17376 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17379 /*integral_constant_expression_p=*/true);
17380 --c_inhibit_evaluation_warnings
;
17382 finish_static_assert (condition
,
17383 STATIC_ASSERT_MESSAGE (t
),
17384 STATIC_ASSERT_SOURCE_LOCATION (t
),
17385 /*member_p=*/false);
17390 case OACC_PARALLEL
:
17391 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17393 stmt
= begin_omp_parallel ();
17394 RECUR (OMP_BODY (t
));
17395 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17399 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17400 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17401 complain
, in_decl
);
17402 if (OMP_PARALLEL_COMBINED (t
))
17403 omp_parallel_combined_clauses
= &tmp
;
17404 stmt
= begin_omp_parallel ();
17405 RECUR (OMP_PARALLEL_BODY (t
));
17406 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17407 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17408 = OMP_PARALLEL_COMBINED (t
);
17409 pop_omp_privatization_clauses (r
);
17413 if (OMP_TASK_BODY (t
) == NULL_TREE
)
17415 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17416 complain
, in_decl
);
17418 OMP_TASK_CLAUSES (t
) = tmp
;
17422 r
= push_omp_privatization_clauses (false);
17423 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17424 complain
, in_decl
);
17425 stmt
= begin_omp_task ();
17426 RECUR (OMP_TASK_BODY (t
));
17427 finish_omp_task (tmp
, stmt
);
17428 pop_omp_privatization_clauses (r
);
17433 case OMP_DISTRIBUTE
:
17437 tree clauses
, body
, pre_body
;
17438 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17439 tree orig_declv
= NULL_TREE
;
17440 tree incrv
= NULL_TREE
;
17441 enum c_omp_region_type ort
= C_ORT_OMP
;
17442 bool any_range_for
= false;
17445 if (TREE_CODE (t
) == OACC_LOOP
)
17448 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17449 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17451 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17453 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17454 if (OMP_FOR_ORIG_DECLS (t
))
17455 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17456 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17457 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17458 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17461 keep_next_level (true);
17462 stmt
= begin_omp_structured_block ();
17464 pre_body
= push_stmt_list ();
17465 RECUR (OMP_FOR_PRE_BODY (t
));
17466 pre_body
= pop_stmt_list (pre_body
);
17468 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17469 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17471 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
17472 condv
, incrv
, &clauses
, args
,
17474 integral_constant_expression_p
);
17475 omp_parallel_combined_clauses
= NULL
;
17479 gcc_assert (orig_declv
);
17480 body
= begin_omp_structured_block ();
17481 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17482 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
17483 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
17484 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
17485 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
17486 TREE_VEC_ELT (declv
, i
));
17489 body
= push_stmt_list ();
17490 RECUR (OMP_FOR_BODY (t
));
17492 body
= finish_omp_structured_block (body
);
17494 body
= pop_stmt_list (body
);
17496 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17497 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17498 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17502 t
= make_node (TREE_CODE (t
));
17503 TREE_TYPE (t
) = void_type_node
;
17504 OMP_FOR_BODY (t
) = body
;
17505 OMP_FOR_PRE_BODY (t
) = pre_body
;
17506 OMP_FOR_CLAUSES (t
) = clauses
;
17507 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17511 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
17513 pop_omp_privatization_clauses (r
);
17518 omp_parallel_combined_clauses
= NULL
;
17523 case OMP_TASKGROUP
:
17524 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17525 && OMP_TEAMS_COMBINED (t
));
17526 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17528 if (TREE_CODE (t
) == OMP_TEAMS
)
17530 keep_next_level (true);
17531 stmt
= begin_omp_structured_block ();
17532 RECUR (OMP_BODY (t
));
17533 stmt
= finish_omp_structured_block (stmt
);
17537 stmt
= push_stmt_list ();
17538 RECUR (OMP_BODY (t
));
17539 stmt
= pop_stmt_list (stmt
);
17543 OMP_BODY (t
) = stmt
;
17544 OMP_CLAUSES (t
) = tmp
;
17546 pop_omp_privatization_clauses (r
);
17550 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
17551 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
17553 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
17554 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
17556 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
17557 args
, complain
, in_decl
);
17558 if (tmp
== NULL_TREE
)
17559 tmp
= error_mark_node
;
17563 kind
= (enum omp_clause_depend_kind
)
17564 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
17567 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
17570 finish_omp_depobj (EXPR_LOCATION (t
), r
,
17571 OMP_CLAUSE_DEPEND_SOURCE
,
17572 OMP_DEPOBJ_CLAUSES (t
));
17576 case OMP_TARGET_DATA
:
17578 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17579 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17581 keep_next_level (true);
17582 stmt
= begin_omp_structured_block ();
17584 RECUR (OMP_BODY (t
));
17585 stmt
= finish_omp_structured_block (stmt
);
17588 OMP_BODY (t
) = stmt
;
17589 OMP_CLAUSES (t
) = tmp
;
17590 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17592 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17595 /* For combined target teams, ensure the num_teams and
17596 thread_limit clause expressions are evaluated on the host,
17597 before entering the target construct. */
17599 for (c
= OMP_TEAMS_CLAUSES (teams
);
17600 c
; c
= OMP_CLAUSE_CHAIN (c
))
17601 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17602 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17603 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17605 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17606 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17607 if (expr
== error_mark_node
)
17609 tmp
= TARGET_EXPR_SLOT (expr
);
17611 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17612 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17613 OMP_CLAUSE_FIRSTPRIVATE
);
17614 OMP_CLAUSE_DECL (tc
) = tmp
;
17615 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17616 OMP_TARGET_CLAUSES (t
) = tc
;
17625 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17626 complain
, in_decl
);
17627 OACC_DECLARE_CLAUSES (t
) = tmp
;
17631 case OMP_TARGET_UPDATE
:
17632 case OMP_TARGET_ENTER_DATA
:
17633 case OMP_TARGET_EXIT_DATA
:
17634 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17635 complain
, in_decl
);
17637 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17641 case OACC_ENTER_DATA
:
17642 case OACC_EXIT_DATA
:
17644 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17645 complain
, in_decl
);
17647 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17652 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17653 complain
, in_decl
);
17654 stmt
= push_stmt_list ();
17655 RECUR (OMP_BODY (t
));
17656 stmt
= pop_stmt_list (stmt
);
17659 OMP_BODY (t
) = stmt
;
17660 OMP_ORDERED_CLAUSES (t
) = tmp
;
17666 stmt
= push_stmt_list ();
17667 RECUR (OMP_BODY (t
));
17668 stmt
= pop_stmt_list (stmt
);
17671 OMP_BODY (t
) = stmt
;
17676 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17678 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
17679 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
17680 complain
, in_decl
);
17681 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17683 tree op1
= TREE_OPERAND (t
, 1);
17684 tree rhs1
= NULL_TREE
;
17686 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17688 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17689 op1
= TREE_OPERAND (op1
, 1);
17691 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17692 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17693 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
17694 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
17695 OMP_ATOMIC_MEMORY_ORDER (t
));
17699 tree op1
= TREE_OPERAND (t
, 1);
17700 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17701 tree rhs1
= NULL_TREE
;
17702 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17703 enum tree_code opcode
= NOP_EXPR
;
17704 if (code
== OMP_ATOMIC_READ
)
17706 v
= RECUR (TREE_OPERAND (op1
, 0));
17707 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17709 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17710 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17712 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17713 v
= RECUR (TREE_OPERAND (op1
, 0));
17714 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17715 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17717 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17718 op11
= TREE_OPERAND (op11
, 1);
17720 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17721 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17722 opcode
= TREE_CODE (op11
);
17723 if (opcode
== MODIFY_EXPR
)
17729 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17730 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17732 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
17733 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
17737 case TRANSACTION_EXPR
:
17740 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17741 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17743 if (TRANSACTION_EXPR_IS_STMT (t
))
17745 tree body
= TRANSACTION_EXPR_BODY (t
);
17746 tree noex
= NULL_TREE
;
17747 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17749 noex
= MUST_NOT_THROW_COND (body
);
17750 if (noex
== NULL_TREE
)
17751 noex
= boolean_true_node
;
17752 body
= TREE_OPERAND (body
, 0);
17754 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17756 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17760 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17761 RECUR (TRANSACTION_EXPR_BODY (t
)),
17768 case MUST_NOT_THROW_EXPR
:
17770 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17771 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17772 RETURN (build_must_not_throw_expr (op0
, cond
));
17775 case EXPR_PACK_EXPANSION
:
17776 error ("invalid use of pack expansion expression");
17777 RETURN (error_mark_node
);
17779 case NONTYPE_ARGUMENT_PACK
:
17780 error ("use %<...%> to expand argument pack");
17781 RETURN (error_mark_node
);
17783 case COMPOUND_EXPR
:
17784 tmp
= RECUR (TREE_OPERAND (t
, 0));
17785 if (tmp
== NULL_TREE
)
17786 /* If the first operand was a statement, we're done with it. */
17787 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17788 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17789 RECUR (TREE_OPERAND (t
, 1)),
17792 case ANNOTATE_EXPR
:
17793 tmp
= RECUR (TREE_OPERAND (t
, 0));
17794 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17795 TREE_TYPE (tmp
), tmp
,
17796 RECUR (TREE_OPERAND (t
, 1)),
17797 RECUR (TREE_OPERAND (t
, 2))));
17800 RETURN (add_stmt (copy_node (t
)));
17803 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17805 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17806 /*function_p=*/false,
17807 integral_constant_expression_p
));
17810 RETURN (NULL_TREE
);
17812 input_location
= loc
;
17818 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17819 function. For description of the body see comment above
17820 cp_parser_omp_declare_reduction_exprs. */
17823 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17825 if (t
== NULL_TREE
|| t
== error_mark_node
)
17828 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17830 tree_stmt_iterator tsi
;
17833 memset (stmts
, 0, sizeof stmts
);
17834 for (i
= 0, tsi
= tsi_start (t
);
17835 i
< 7 && !tsi_end_p (tsi
);
17836 i
++, tsi_next (&tsi
))
17837 stmts
[i
] = tsi_stmt (tsi
);
17838 gcc_assert (tsi_end_p (tsi
));
17842 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17843 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17844 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17845 args
, complain
, in_decl
);
17846 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17847 args
, complain
, in_decl
);
17848 DECL_CONTEXT (omp_out
) = current_function_decl
;
17849 DECL_CONTEXT (omp_in
) = current_function_decl
;
17850 keep_next_level (true);
17851 tree block
= begin_omp_structured_block ();
17852 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17853 block
= finish_omp_structured_block (block
);
17854 block
= maybe_cleanup_point_expr_void (block
);
17855 add_decl_expr (omp_out
);
17856 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17857 TREE_NO_WARNING (omp_out
) = 1;
17858 add_decl_expr (omp_in
);
17859 finish_expr_stmt (block
);
17863 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17864 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17865 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17866 args
, complain
, in_decl
);
17867 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17868 args
, complain
, in_decl
);
17869 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17870 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17871 keep_next_level (true);
17872 tree block
= begin_omp_structured_block ();
17873 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17874 block
= finish_omp_structured_block (block
);
17875 block
= maybe_cleanup_point_expr_void (block
);
17876 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17877 add_decl_expr (omp_priv
);
17878 add_decl_expr (omp_orig
);
17879 finish_expr_stmt (block
);
17881 add_decl_expr (omp_orig
);
17885 /* T is a postfix-expression that is not being used in a function
17886 call. Return the substituted version of T. */
17889 tsubst_non_call_postfix_expression (tree t
, tree args
,
17890 tsubst_flags_t complain
,
17893 if (TREE_CODE (t
) == SCOPE_REF
)
17894 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17895 /*done=*/false, /*address_p=*/false);
17897 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17898 /*function_p=*/false,
17899 /*integral_constant_expression_p=*/false);
17904 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17905 instantiation context. Instantiating a pack expansion containing a lambda
17906 might result in multiple lambdas all based on the same lambda in the
17910 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17912 tree oldfn
= lambda_function (t
);
17915 tree r
= build_lambda_expr ();
17917 LAMBDA_EXPR_LOCATION (r
)
17918 = LAMBDA_EXPR_LOCATION (t
);
17919 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17920 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17921 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17922 LAMBDA_EXPR_INSTANTIATED (r
) = true;
17924 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
17925 /* A lambda in a default argument outside a class gets no
17926 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17927 tsubst_default_argument calls start_lambda_scope, so we need to
17928 specifically ignore it here, and use the global scope. */
17929 record_null_lambda_scope (r
);
17931 record_lambda_scope (r
);
17933 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17934 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17936 vec
<tree
,va_gc
>* field_packs
= NULL
;
17938 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
17939 cap
= TREE_CHAIN (cap
))
17941 tree ofield
= TREE_PURPOSE (cap
);
17942 if (PACK_EXPANSION_P (ofield
))
17943 ofield
= PACK_EXPANSION_PATTERN (ofield
);
17944 tree field
= tsubst_decl (ofield
, args
, complain
);
17946 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
17948 /* Remember these for when we've pushed local_specializations. */
17949 vec_safe_push (field_packs
, ofield
);
17950 vec_safe_push (field_packs
, field
);
17953 if (field
== error_mark_node
)
17954 return error_mark_node
;
17956 tree init
= TREE_VALUE (cap
);
17957 if (PACK_EXPANSION_P (init
))
17958 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
17960 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
17961 /*fn*/false, /*constexpr*/false);
17963 if (TREE_CODE (field
) == TREE_VEC
)
17965 int len
= TREE_VEC_LENGTH (field
);
17966 gcc_assert (TREE_CODE (init
) == TREE_VEC
17967 && TREE_VEC_LENGTH (init
) == len
);
17968 for (int i
= 0; i
< len
; ++i
)
17969 LAMBDA_EXPR_CAPTURE_LIST (r
)
17970 = tree_cons (TREE_VEC_ELT (field
, i
),
17971 TREE_VEC_ELT (init
, i
),
17972 LAMBDA_EXPR_CAPTURE_LIST (r
));
17976 LAMBDA_EXPR_CAPTURE_LIST (r
)
17977 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
17979 if (id_equal (DECL_NAME (field
), "__this"))
17980 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
17984 tree type
= begin_lambda_type (r
);
17985 if (type
== error_mark_node
)
17986 return error_mark_node
;
17988 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17989 determine_visibility (TYPE_NAME (type
));
17991 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
17993 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
17994 ? DECL_TI_TEMPLATE (oldfn
)
17997 tree fntype
= static_fn_type (oldfn
);
17999 ++processing_template_decl
;
18000 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18002 --processing_template_decl
;
18004 if (fntype
== error_mark_node
)
18005 r
= error_mark_node
;
18008 /* Fix the type of 'this'. */
18009 fntype
= build_memfn_type (fntype
, type
,
18010 type_memfn_quals (fntype
),
18011 type_memfn_rqual (fntype
));
18015 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18016 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18017 finish_member_declaration (tmpl
);
18022 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18023 finish_member_declaration (fn
);
18026 /* Let finish_function set this. */
18027 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18029 /* The body of a lambda-expression is not a subexpression of the
18030 enclosing expression. */
18033 bool nested
= cfun
;
18035 push_function_context ();
18037 /* Still increment function_depth so that we don't GC in the
18038 middle of an expression. */
18041 local_specialization_stack
s (lss_copy
);
18043 tree body
= start_lambda_function (fn
, r
);
18045 /* Now record them for lookup_init_capture_pack. */
18046 int fplen
= vec_safe_length (field_packs
);
18047 for (int i
= 0; i
< fplen
; )
18049 tree pack
= (*field_packs
)[i
++];
18050 tree inst
= (*field_packs
)[i
++];
18051 register_local_specialization (inst
, pack
);
18053 release_tree_vector (field_packs
);
18055 register_parameter_specializations (oldfn
, fn
);
18059 /* We might not partially instantiate some parts of the function, so
18060 copy these flags from the original template. */
18061 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18062 current_function_returns_value
= ol
->returns_value
;
18063 current_function_returns_null
= ol
->returns_null
;
18064 current_function_returns_abnormally
= ol
->returns_abnormally
;
18065 current_function_infinite_loop
= ol
->infinite_loop
;
18068 /* [temp.deduct] A lambda-expression appearing in a function type or a
18069 template parameter is not considered part of the immediate context for
18070 the purposes of template argument deduction. */
18071 complain
= tf_warning_or_error
;
18073 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18074 /*constexpr*/false);
18076 finish_lambda_function (body
);
18079 pop_function_context ();
18083 /* The capture list was built up in reverse order; fix that now. */
18084 LAMBDA_EXPR_CAPTURE_LIST (r
)
18085 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18087 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18089 maybe_add_lambda_conv_op (type
);
18092 finish_struct (type
, /*attr*/NULL_TREE
);
18094 insert_pending_capture_proxies ();
18099 /* Like tsubst but deals with expressions and performs semantic
18100 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18103 tsubst_copy_and_build (tree t
,
18105 tsubst_flags_t complain
,
18108 bool integral_constant_expression_p
)
18110 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18111 #define RECUR(NODE) \
18112 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18113 /*function_p=*/false, \
18114 integral_constant_expression_p)
18119 if (t
== NULL_TREE
|| t
== error_mark_node
)
18122 loc
= input_location
;
18123 if (location_t eloc
= cp_expr_location (t
))
18124 input_location
= eloc
;
18126 /* N3276 decltype magic only applies to calls at the top level or on the
18127 right side of a comma. */
18128 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18129 complain
&= ~tf_decltype
;
18131 switch (TREE_CODE (t
))
18135 /* Fall through. */
18136 case IDENTIFIER_NODE
:
18140 bool non_integral_constant_expression_p
;
18141 const char *error_msg
;
18143 if (IDENTIFIER_CONV_OP_P (t
))
18145 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18146 t
= make_conv_op_name (new_type
);
18149 /* Look up the name. */
18150 decl
= lookup_name (t
);
18152 /* By convention, expressions use ERROR_MARK_NODE to indicate
18153 failure, not NULL_TREE. */
18154 if (decl
== NULL_TREE
)
18155 decl
= error_mark_node
;
18157 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18159 integral_constant_expression_p
,
18160 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18161 &non_integral_constant_expression_p
,
18162 /*template_p=*/false,
18164 /*address_p=*/false,
18165 /*template_arg_p=*/false,
18170 if (!function_p
&& identifier_p (decl
))
18172 if (complain
& tf_error
)
18173 unqualified_name_lookup_error (decl
);
18174 decl
= error_mark_node
;
18179 case TEMPLATE_ID_EXPR
:
18182 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18183 tree targs
= TREE_OPERAND (t
, 1);
18186 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18187 if (targs
== error_mark_node
)
18188 RETURN (error_mark_node
);
18190 if (TREE_CODE (templ
) == SCOPE_REF
)
18192 tree name
= TREE_OPERAND (templ
, 1);
18193 tree tid
= lookup_template_function (name
, targs
);
18194 TREE_OPERAND (templ
, 1) = tid
;
18198 if (variable_template_p (templ
))
18199 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18201 if (TREE_CODE (templ
) == COMPONENT_REF
)
18203 object
= TREE_OPERAND (templ
, 0);
18204 templ
= TREE_OPERAND (templ
, 1);
18207 object
= NULL_TREE
;
18208 templ
= lookup_template_function (templ
, targs
);
18211 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18212 object
, templ
, NULL_TREE
));
18214 RETURN (baselink_for_fns (templ
));
18219 tree r
= RECUR (TREE_OPERAND (t
, 0));
18221 if (REFERENCE_REF_P (t
))
18223 /* A type conversion to reference type will be enclosed in
18224 such an indirect ref, but the substitution of the cast
18225 will have also added such an indirect ref. */
18226 r
= convert_from_reference (r
);
18229 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18230 complain
|decltype_flag
);
18232 if (REF_PARENTHESIZED_P (t
))
18233 r
= force_paren_expr (r
);
18240 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18241 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18242 RETURN (build_nop (type
, op0
));
18245 case IMPLICIT_CONV_EXPR
:
18247 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18248 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18249 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18251 retval
= copy_node (t
);
18252 TREE_TYPE (retval
) = type
;
18253 TREE_OPERAND (retval
, 0) = expr
;
18256 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18257 /* We'll pass this to convert_nontype_argument again, we don't need
18258 to actually perform any conversion here. */
18260 int flags
= LOOKUP_IMPLICIT
;
18261 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18262 flags
= LOOKUP_NORMAL
;
18263 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18264 flags
|= LOOKUP_NO_NARROWING
;
18265 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18271 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18272 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18273 if (op0
== error_mark_node
)
18274 RETURN (error_mark_node
);
18275 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18279 case REINTERPRET_CAST_EXPR
:
18280 case CONST_CAST_EXPR
:
18281 case DYNAMIC_CAST_EXPR
:
18282 case STATIC_CAST_EXPR
:
18285 tree op
, r
= NULL_TREE
;
18287 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18288 if (integral_constant_expression_p
18289 && !cast_valid_in_integral_constant_expression_p (type
))
18291 if (complain
& tf_error
)
18292 error ("a cast to a type other than an integral or "
18293 "enumeration type cannot appear in a constant-expression");
18294 RETURN (error_mark_node
);
18297 op
= RECUR (TREE_OPERAND (t
, 0));
18299 warning_sentinel
s(warn_useless_cast
);
18300 warning_sentinel
s2(warn_ignored_qualifiers
);
18301 switch (TREE_CODE (t
))
18304 r
= build_functional_cast (type
, op
, complain
);
18306 case REINTERPRET_CAST_EXPR
:
18307 r
= build_reinterpret_cast (type
, op
, complain
);
18309 case CONST_CAST_EXPR
:
18310 r
= build_const_cast (type
, op
, complain
);
18312 case DYNAMIC_CAST_EXPR
:
18313 r
= build_dynamic_cast (type
, op
, complain
);
18315 case STATIC_CAST_EXPR
:
18316 r
= build_static_cast (type
, op
, complain
);
18319 gcc_unreachable ();
18325 case POSTDECREMENT_EXPR
:
18326 case POSTINCREMENT_EXPR
:
18327 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18328 args
, complain
, in_decl
);
18329 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
18330 complain
|decltype_flag
));
18332 case PREDECREMENT_EXPR
:
18333 case PREINCREMENT_EXPR
:
18337 case TRUTH_NOT_EXPR
:
18338 case UNARY_PLUS_EXPR
: /* Unary + */
18339 case REALPART_EXPR
:
18340 case IMAGPART_EXPR
:
18341 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
18342 RECUR (TREE_OPERAND (t
, 0)),
18343 complain
|decltype_flag
));
18345 case FIX_TRUNC_EXPR
:
18346 gcc_unreachable ();
18349 op1
= TREE_OPERAND (t
, 0);
18350 if (TREE_CODE (op1
) == LABEL_DECL
)
18351 RETURN (finish_label_address_expr (DECL_NAME (op1
),
18352 EXPR_LOCATION (op1
)));
18353 if (TREE_CODE (op1
) == SCOPE_REF
)
18354 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
18355 /*done=*/true, /*address_p=*/true);
18357 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
18359 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
18360 complain
|decltype_flag
));
18365 case TRUNC_DIV_EXPR
:
18366 case CEIL_DIV_EXPR
:
18367 case FLOOR_DIV_EXPR
:
18368 case ROUND_DIV_EXPR
:
18369 case EXACT_DIV_EXPR
:
18373 case TRUNC_MOD_EXPR
:
18374 case FLOOR_MOD_EXPR
:
18375 case TRUTH_ANDIF_EXPR
:
18376 case TRUTH_ORIF_EXPR
:
18377 case TRUTH_AND_EXPR
:
18378 case TRUTH_OR_EXPR
:
18394 warning_sentinel
s1(warn_type_limits
);
18395 warning_sentinel
s2(warn_div_by_zero
);
18396 warning_sentinel
s3(warn_logical_op
);
18397 warning_sentinel
s4(warn_tautological_compare
);
18398 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18399 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18400 tree r
= build_x_binary_op
18401 (input_location
, TREE_CODE (t
),
18403 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18405 : TREE_CODE (TREE_OPERAND (t
, 0))),
18407 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18409 : TREE_CODE (TREE_OPERAND (t
, 1))),
18411 complain
|decltype_flag
);
18412 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18413 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18418 case POINTER_PLUS_EXPR
:
18420 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18421 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18422 RETURN (fold_build_pointer_plus (op0
, op1
));
18426 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18427 /*address_p=*/false));
18429 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18430 args
, complain
, in_decl
);
18431 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18432 RECUR (TREE_OPERAND (t
, 1)),
18433 complain
|decltype_flag
));
18436 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18437 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18438 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18445 op1
= TREE_OPERAND (t
, 0);
18446 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18447 op1
= TREE_TYPE (op1
);
18448 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18449 && ALIGNOF_EXPR_STD_P (t
));
18452 /* When there are no ARGS, we are trying to evaluate a
18453 non-dependent expression from the parser. Trying to do
18454 the substitutions may not work. */
18456 op1
= TREE_TYPE (op1
);
18460 ++cp_unevaluated_operand
;
18461 ++c_inhibit_evaluation_warnings
;
18463 op1
= tsubst (op1
, args
, complain
, in_decl
);
18465 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18466 /*function_p=*/false,
18467 /*integral_constant_expression_p=*/
18469 --cp_unevaluated_operand
;
18470 --c_inhibit_evaluation_warnings
;
18473 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18474 complain
& tf_error
);
18476 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18477 complain
& tf_error
);
18478 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18480 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18482 if (!processing_template_decl
&& TYPE_P (op1
))
18484 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18485 build1 (NOP_EXPR
, op1
, error_mark_node
));
18486 SIZEOF_EXPR_TYPE_P (r
) = 1;
18489 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18490 TREE_SIDE_EFFECTS (r
) = 0;
18491 TREE_READONLY (r
) = 1;
18493 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18498 case AT_ENCODE_EXPR
:
18500 op1
= TREE_OPERAND (t
, 0);
18501 ++cp_unevaluated_operand
;
18502 ++c_inhibit_evaluation_warnings
;
18503 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18504 /*function_p=*/false,
18505 /*integral_constant_expression_p=*/false);
18506 --cp_unevaluated_operand
;
18507 --c_inhibit_evaluation_warnings
;
18508 RETURN (objc_build_encode_expr (op1
));
18511 case NOEXCEPT_EXPR
:
18512 op1
= TREE_OPERAND (t
, 0);
18513 ++cp_unevaluated_operand
;
18514 ++c_inhibit_evaluation_warnings
;
18515 ++cp_noexcept_operand
;
18516 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18517 /*function_p=*/false,
18518 /*integral_constant_expression_p=*/false);
18519 --cp_unevaluated_operand
;
18520 --c_inhibit_evaluation_warnings
;
18521 --cp_noexcept_operand
;
18522 RETURN (finish_noexcept_expr (op1
, complain
));
18526 warning_sentinel
s(warn_div_by_zero
);
18527 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18528 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18529 tree r
= build_x_modify_expr
18530 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18531 complain
|decltype_flag
);
18532 /* TREE_NO_WARNING must be set if either the expression was
18533 parenthesized or it uses an operator such as >>= rather
18534 than plain assignment. In the former case, it was already
18535 set and must be copied. In the latter case,
18536 build_x_modify_expr sets it and it must not be reset
18538 if (TREE_NO_WARNING (t
))
18539 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18545 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18546 args
, complain
, in_decl
);
18547 /* Remember that there was a reference to this entity. */
18549 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18550 RETURN (error_mark_node
);
18551 RETURN (build_x_arrow (input_location
, op1
, complain
));
18555 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18556 tree init
= RECUR (TREE_OPERAND (t
, 3));
18557 vec
<tree
, va_gc
> *placement_vec
;
18558 vec
<tree
, va_gc
> *init_vec
;
18561 if (placement
== NULL_TREE
)
18562 placement_vec
= NULL
;
18565 placement_vec
= make_tree_vector ();
18566 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18567 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18570 /* If there was an initializer in the original tree, but it
18571 instantiated to an empty list, then we should pass a
18572 non-NULL empty vector to tell build_new that it was an
18573 empty initializer() rather than no initializer. This can
18574 only happen when the initializer is a pack expansion whose
18575 parameter packs are of length zero. */
18576 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18580 init_vec
= make_tree_vector ();
18581 if (init
== void_node
)
18582 gcc_assert (init_vec
!= NULL
);
18585 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18586 vec_safe_push (init_vec
, TREE_VALUE (init
));
18590 /* Avoid passing an enclosing decl to valid_array_size_p. */
18591 in_decl
= NULL_TREE
;
18593 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18594 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18595 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18596 NEW_EXPR_USE_GLOBAL (t
),
18599 if (placement_vec
!= NULL
)
18600 release_tree_vector (placement_vec
);
18601 if (init_vec
!= NULL
)
18602 release_tree_vector (init_vec
);
18609 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18610 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18611 RETURN (delete_sanity (op0
, op1
,
18612 DELETE_EXPR_USE_VEC (t
),
18613 DELETE_EXPR_USE_GLOBAL (t
),
18617 case COMPOUND_EXPR
:
18619 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18620 complain
& ~tf_decltype
, in_decl
,
18621 /*function_p=*/false,
18622 integral_constant_expression_p
);
18623 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18625 RECUR (TREE_OPERAND (t
, 1)),
18626 complain
|decltype_flag
));
18632 vec
<tree
, va_gc
> *call_args
;
18633 unsigned int nargs
, i
;
18638 function
= CALL_EXPR_FN (t
);
18639 /* Internal function with no arguments. */
18640 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18643 /* When we parsed the expression, we determined whether or
18644 not Koenig lookup should be performed. */
18645 koenig_p
= KOENIG_LOOKUP_P (t
);
18646 if (function
== NULL_TREE
)
18649 qualified_p
= false;
18651 else if (TREE_CODE (function
) == SCOPE_REF
)
18653 qualified_p
= true;
18654 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18656 /*address_p=*/false);
18658 else if (koenig_p
&& identifier_p (function
))
18660 /* Do nothing; calling tsubst_copy_and_build on an identifier
18661 would incorrectly perform unqualified lookup again.
18663 Note that we can also have an IDENTIFIER_NODE if the earlier
18664 unqualified lookup found a member function; in that case
18665 koenig_p will be false and we do want to do the lookup
18666 again to find the instantiated member function.
18668 FIXME but doing that causes c++/15272, so we need to stop
18669 using IDENTIFIER_NODE in that situation. */
18670 qualified_p
= false;
18674 if (TREE_CODE (function
) == COMPONENT_REF
)
18676 tree op
= TREE_OPERAND (function
, 1);
18678 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18679 || (BASELINK_P (op
)
18680 && BASELINK_QUALIFIED_P (op
)));
18683 qualified_p
= false;
18685 if (TREE_CODE (function
) == ADDR_EXPR
18686 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18687 /* Avoid error about taking the address of a constructor. */
18688 function
= TREE_OPERAND (function
, 0);
18690 function
= tsubst_copy_and_build (function
, args
, complain
,
18693 integral_constant_expression_p
);
18695 if (BASELINK_P (function
))
18696 qualified_p
= true;
18699 nargs
= call_expr_nargs (t
);
18700 call_args
= make_tree_vector ();
18701 for (i
= 0; i
< nargs
; ++i
)
18703 tree arg
= CALL_EXPR_ARG (t
, i
);
18705 if (!PACK_EXPANSION_P (arg
))
18706 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18709 /* Expand the pack expansion and push each entry onto
18711 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18712 if (TREE_CODE (arg
) == TREE_VEC
)
18714 unsigned int len
, j
;
18716 len
= TREE_VEC_LENGTH (arg
);
18717 for (j
= 0; j
< len
; ++j
)
18719 tree value
= TREE_VEC_ELT (arg
, j
);
18720 if (value
!= NULL_TREE
)
18721 value
= convert_from_reference (value
);
18722 vec_safe_push (call_args
, value
);
18727 /* A partial substitution. Add one entry. */
18728 vec_safe_push (call_args
, arg
);
18733 /* Stripped-down processing for a call in a thunk. Specifically, in
18734 the thunk template for a generic lambda. */
18735 if (CALL_FROM_THUNK_P (t
))
18737 tree thisarg
= NULL_TREE
;
18738 if (TREE_CODE (function
) == COMPONENT_REF
)
18740 thisarg
= TREE_OPERAND (function
, 0);
18741 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
18742 thisarg
= TREE_OPERAND (thisarg
, 0);
18743 function
= TREE_OPERAND (function
, 1);
18744 if (TREE_CODE (function
) == BASELINK
)
18745 function
= BASELINK_FUNCTIONS (function
);
18747 /* We aren't going to do normal overload resolution, so force the
18748 template-id to resolve. */
18749 function
= resolve_nondeduced_context (function
, complain
);
18750 for (unsigned i
= 0; i
< nargs
; ++i
)
18752 /* In a thunk, pass through args directly, without any
18754 tree arg
= (*call_args
)[i
];
18755 while (TREE_CODE (arg
) != PARM_DECL
)
18756 arg
= TREE_OPERAND (arg
, 0);
18757 (*call_args
)[i
] = arg
;
18761 /* Shift the other args over to make room. */
18762 vec_safe_push (call_args
, (*call_args
)[nargs
-1]);
18763 for (int i
= nargs
-1; i
> 0; --i
)
18764 (*call_args
)[i
] = (*call_args
)[i
-1];
18765 (*call_args
)[0] = thisarg
;
18767 ret
= build_call_a (function
, call_args
->length (),
18768 call_args
->address ());
18769 /* The thunk location is not interesting. */
18770 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
18771 CALL_FROM_THUNK_P (ret
) = true;
18772 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
18773 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
18775 release_tree_vector (call_args
);
18779 /* We do not perform argument-dependent lookup if normal
18780 lookup finds a non-function, in accordance with the
18781 expected resolution of DR 218. */
18783 && ((is_overloaded_fn (function
)
18784 /* If lookup found a member function, the Koenig lookup is
18785 not appropriate, even if an unqualified-name was used
18786 to denote the function. */
18787 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18788 || identifier_p (function
))
18789 /* Only do this when substitution turns a dependent call
18790 into a non-dependent call. */
18791 && type_dependent_expression_p_push (t
)
18792 && !any_type_dependent_arguments_p (call_args
))
18793 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18795 if (function
!= NULL_TREE
18796 && identifier_p (function
)
18797 && !any_type_dependent_arguments_p (call_args
))
18799 if (koenig_p
&& (complain
& tf_warning_or_error
))
18801 /* For backwards compatibility and good diagnostics, try
18802 the unqualified lookup again if we aren't in SFINAE
18804 tree unq
= (tsubst_copy_and_build
18805 (function
, args
, complain
, in_decl
, true,
18806 integral_constant_expression_p
));
18807 if (unq
== error_mark_node
)
18809 release_tree_vector (call_args
);
18810 RETURN (error_mark_node
);
18813 if (unq
!= function
)
18815 /* In a lambda fn, we have to be careful to not
18816 introduce new this captures. Legacy code can't
18817 be using lambdas anyway, so it's ok to be
18819 bool in_lambda
= (current_class_type
18820 && LAMBDA_TYPE_P (current_class_type
));
18821 char const *const msg
18822 = G_("%qD was not declared in this scope, "
18823 "and no declarations were found by "
18824 "argument-dependent lookup at the point "
18825 "of instantiation");
18829 error_at (cp_expr_loc_or_loc (t
, input_location
),
18832 diag
= permerror (cp_expr_loc_or_loc (t
, input_location
),
18838 if (INDIRECT_REF_P (fn
))
18839 fn
= TREE_OPERAND (fn
, 0);
18840 if (is_overloaded_fn (fn
))
18841 fn
= get_first_fn (fn
);
18844 /* Can't say anything more. */;
18845 else if (DECL_CLASS_SCOPE_P (fn
))
18847 location_t loc
= cp_expr_loc_or_loc (t
,
18850 "declarations in dependent base %qT are "
18851 "not found by unqualified lookup",
18852 DECL_CLASS_CONTEXT (fn
));
18853 if (current_class_ptr
)
18855 "use %<this->%D%> instead", function
);
18858 "use %<%T::%D%> instead",
18859 current_class_name
, function
);
18862 inform (DECL_SOURCE_LOCATION (fn
),
18863 "%qD declared here, later in the "
18864 "translation unit", fn
);
18867 release_tree_vector (call_args
);
18868 RETURN (error_mark_node
);
18875 if (identifier_p (function
))
18877 if (complain
& tf_error
)
18878 unqualified_name_lookup_error (function
);
18879 release_tree_vector (call_args
);
18880 RETURN (error_mark_node
);
18884 /* Remember that there was a reference to this entity. */
18885 if (function
!= NULL_TREE
18886 && DECL_P (function
)
18887 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18889 release_tree_vector (call_args
);
18890 RETURN (error_mark_node
);
18893 /* Put back tf_decltype for the actual call. */
18894 complain
|= decltype_flag
;
18896 if (function
== NULL_TREE
)
18897 switch (CALL_EXPR_IFN (t
))
18900 gcc_assert (nargs
== 1);
18901 if (vec_safe_length (call_args
) != 1)
18903 error_at (cp_expr_loc_or_loc (t
, input_location
),
18904 "wrong number of arguments to "
18905 "%<__builtin_launder%>");
18906 ret
= error_mark_node
;
18909 ret
= finish_builtin_launder (cp_expr_loc_or_loc (t
,
18911 (*call_args
)[0], complain
);
18914 case IFN_VEC_CONVERT
:
18915 gcc_assert (nargs
== 1);
18916 if (vec_safe_length (call_args
) != 1)
18918 error_at (cp_expr_loc_or_loc (t
, input_location
),
18919 "wrong number of arguments to "
18920 "%<__builtin_convertvector%>");
18921 ret
= error_mark_node
;
18924 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
18925 tsubst (TREE_TYPE (t
), args
,
18926 complain
, in_decl
),
18928 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
18930 release_tree_vector (call_args
);
18936 /* Unsupported internal function with arguments. */
18937 gcc_unreachable ();
18939 else if (TREE_CODE (function
) == OFFSET_REF
18940 || TREE_CODE (function
) == DOTSTAR_EXPR
18941 || TREE_CODE (function
) == MEMBER_REF
)
18942 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
18944 else if (TREE_CODE (function
) == COMPONENT_REF
)
18946 tree instance
= TREE_OPERAND (function
, 0);
18947 tree fn
= TREE_OPERAND (function
, 1);
18949 if (processing_template_decl
18950 && (type_dependent_expression_p (instance
)
18951 || (!BASELINK_P (fn
)
18952 && TREE_CODE (fn
) != FIELD_DECL
)
18953 || type_dependent_expression_p (fn
)
18954 || any_type_dependent_arguments_p (call_args
)))
18955 ret
= build_min_nt_call_vec (function
, call_args
);
18956 else if (!BASELINK_P (fn
))
18957 ret
= finish_call_expr (function
, &call_args
,
18958 /*disallow_virtual=*/false,
18959 /*koenig_p=*/false,
18962 ret
= (build_new_method_call
18964 &call_args
, NULL_TREE
,
18965 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
18970 ret
= finish_call_expr (function
, &call_args
,
18971 /*disallow_virtual=*/qualified_p
,
18975 release_tree_vector (call_args
);
18977 if (ret
!= error_mark_node
)
18979 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
18980 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
18981 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
18982 if (op
|| ord
|| rev
)
18984 function
= extract_call_expr (ret
);
18985 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
18986 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
18987 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
18996 tree cond
= RECUR (TREE_OPERAND (t
, 0));
18997 cond
= mark_rvalue_use (cond
);
18998 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19001 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19003 if (integer_zerop (folded_cond
))
19005 ++c_inhibit_evaluation_warnings
;
19006 exp1
= RECUR (TREE_OPERAND (t
, 1));
19007 --c_inhibit_evaluation_warnings
;
19008 exp2
= RECUR (TREE_OPERAND (t
, 2));
19012 exp1
= RECUR (TREE_OPERAND (t
, 1));
19013 ++c_inhibit_evaluation_warnings
;
19014 exp2
= RECUR (TREE_OPERAND (t
, 2));
19015 --c_inhibit_evaluation_warnings
;
19017 cond
= folded_cond
;
19021 exp1
= RECUR (TREE_OPERAND (t
, 1));
19022 exp2
= RECUR (TREE_OPERAND (t
, 2));
19025 warning_sentinel
s(warn_duplicated_branches
);
19026 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19027 cond
, exp1
, exp2
, complain
));
19030 case PSEUDO_DTOR_EXPR
:
19032 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19033 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19034 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19035 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19041 tree purpose
, value
, chain
;
19043 if (t
== void_list_node
)
19046 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19047 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19049 /* We have pack expansions, so expand those and
19050 create a new list out of it. */
19051 tree purposevec
= NULL_TREE
;
19052 tree valuevec
= NULL_TREE
;
19056 /* Expand the argument expressions. */
19057 if (TREE_PURPOSE (t
))
19058 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19059 complain
, in_decl
);
19060 if (TREE_VALUE (t
))
19061 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19062 complain
, in_decl
);
19064 /* Build the rest of the list. */
19065 chain
= TREE_CHAIN (t
);
19066 if (chain
&& chain
!= void_type_node
)
19067 chain
= RECUR (chain
);
19069 /* Determine the number of arguments. */
19070 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19072 len
= TREE_VEC_LENGTH (purposevec
);
19073 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19075 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19076 len
= TREE_VEC_LENGTH (valuevec
);
19079 /* Since we only performed a partial substitution into
19080 the argument pack, we only RETURN (a single list
19082 if (purposevec
== TREE_PURPOSE (t
)
19083 && valuevec
== TREE_VALUE (t
)
19084 && chain
== TREE_CHAIN (t
))
19087 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19090 /* Convert the argument vectors into a TREE_LIST */
19094 /* Grab the Ith values. */
19096 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19099 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19102 /* Build the list (backwards). */
19103 chain
= tree_cons (purpose
, value
, chain
);
19109 purpose
= TREE_PURPOSE (t
);
19111 purpose
= RECUR (purpose
);
19112 value
= TREE_VALUE (t
);
19114 value
= RECUR (value
);
19115 chain
= TREE_CHAIN (t
);
19116 if (chain
&& chain
!= void_type_node
)
19117 chain
= RECUR (chain
);
19118 if (purpose
== TREE_PURPOSE (t
)
19119 && value
== TREE_VALUE (t
)
19120 && chain
== TREE_CHAIN (t
))
19122 RETURN (tree_cons (purpose
, value
, chain
));
19125 case COMPONENT_REF
:
19132 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19133 args
, complain
, in_decl
);
19134 /* Remember that there was a reference to this entity. */
19135 if (DECL_P (object
)
19136 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19137 RETURN (error_mark_node
);
19138 object_type
= TREE_TYPE (object
);
19140 member
= TREE_OPERAND (t
, 1);
19141 if (BASELINK_P (member
))
19142 member
= tsubst_baselink (member
,
19143 non_reference (TREE_TYPE (object
)),
19144 args
, complain
, in_decl
);
19146 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19147 if (member
== error_mark_node
)
19148 RETURN (error_mark_node
);
19150 if (TREE_CODE (member
) == FIELD_DECL
)
19152 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19153 if (TREE_CODE (r
) == COMPONENT_REF
)
19154 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19157 else if (type_dependent_expression_p (object
))
19158 /* We can't do much here. */;
19159 else if (!CLASS_TYPE_P (object_type
))
19161 if (scalarish_type_p (object_type
))
19163 tree s
= NULL_TREE
;
19164 tree dtor
= member
;
19166 if (TREE_CODE (dtor
) == SCOPE_REF
)
19168 s
= TREE_OPERAND (dtor
, 0);
19169 dtor
= TREE_OPERAND (dtor
, 1);
19171 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19173 dtor
= TREE_OPERAND (dtor
, 0);
19175 RETURN (finish_pseudo_destructor_expr
19176 (object
, s
, dtor
, input_location
));
19180 else if (TREE_CODE (member
) == SCOPE_REF
19181 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19183 /* Lookup the template functions now that we know what the
19185 tree scope
= TREE_OPERAND (member
, 0);
19186 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19187 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19188 member
= lookup_qualified_name (scope
, tmpl
,
19189 /*is_type_p=*/false,
19190 /*complain=*/false);
19191 if (BASELINK_P (member
))
19193 BASELINK_FUNCTIONS (member
)
19194 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19196 member
= (adjust_result_of_qualified_name_lookup
19197 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19202 qualified_name_lookup_error (scope
, tmpl
, member
,
19204 RETURN (error_mark_node
);
19207 else if (TREE_CODE (member
) == SCOPE_REF
19208 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19209 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19211 if (complain
& tf_error
)
19213 if (TYPE_P (TREE_OPERAND (member
, 0)))
19214 error ("%qT is not a class or namespace",
19215 TREE_OPERAND (member
, 0));
19217 error ("%qD is not a class or namespace",
19218 TREE_OPERAND (member
, 0));
19220 RETURN (error_mark_node
);
19223 r
= finish_class_member_access_expr (object
, member
,
19224 /*template_p=*/false,
19226 if (TREE_CODE (r
) == COMPONENT_REF
)
19227 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19232 RETURN (build_throw
19233 (RECUR (TREE_OPERAND (t
, 0))));
19237 vec
<constructor_elt
, va_gc
> *n
;
19238 constructor_elt
*ce
;
19239 unsigned HOST_WIDE_INT idx
;
19240 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19241 bool process_index_p
;
19243 bool need_copy_p
= false;
19246 if (type
== error_mark_node
)
19247 RETURN (error_mark_node
);
19249 /* We do not want to process the index of aggregate
19250 initializers as they are identifier nodes which will be
19251 looked up by digest_init. */
19252 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19254 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19255 newlen
= vec_safe_length (n
);
19256 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19258 if (ce
->index
&& process_index_p
19259 /* An identifier index is looked up in the type
19260 being initialized, not the current scope. */
19261 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19262 ce
->index
= RECUR (ce
->index
);
19264 if (PACK_EXPANSION_P (ce
->value
))
19266 /* Substitute into the pack expansion. */
19267 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19270 if (ce
->value
== error_mark_node
19271 || PACK_EXPANSION_P (ce
->value
))
19273 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19274 /* Just move the argument into place. */
19275 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19278 /* Update the length of the final CONSTRUCTOR
19279 arguments vector, and note that we will need to
19281 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19282 need_copy_p
= true;
19286 ce
->value
= RECUR (ce
->value
);
19291 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19293 vec_alloc (n
, newlen
);
19294 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19296 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19298 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19299 for (i
= 0; i
< len
; ++i
)
19300 CONSTRUCTOR_APPEND_ELT (n
, 0,
19301 TREE_VEC_ELT (ce
->value
, i
));
19304 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19308 r
= build_constructor (init_list_type_node
, n
);
19309 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19311 if (TREE_HAS_CONSTRUCTOR (t
))
19313 fcl_t cl
= fcl_functional
;
19314 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
19316 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
19319 TREE_TYPE (r
) = type
;
19325 tree operand_0
= TREE_OPERAND (t
, 0);
19326 if (TYPE_P (operand_0
))
19328 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
19329 RETURN (get_typeid (operand_0
, complain
));
19333 operand_0
= RECUR (operand_0
);
19334 RETURN (build_typeid (operand_0
, complain
));
19345 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
19346 /* ??? We're doing a subset of finish_id_expression here. */
19348 && !processing_template_decl
19349 && !cp_unevaluated_operand
19350 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
19351 && CP_DECL_THREAD_LOCAL_P (r
))
19353 if (tree wrap
= get_tls_wrapper_fn (r
))
19354 /* Replace an evaluated use of the thread_local variable with
19355 a call to its wrapper. */
19356 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
19358 else if (outer_automatic_var_p (r
))
19359 r
= process_outer_var_ref (r
, complain
);
19361 if (!TYPE_REF_P (TREE_TYPE (t
)))
19362 /* If the original type was a reference, we'll be wrapped in
19363 the appropriate INDIRECT_REF. */
19364 r
= convert_from_reference (r
);
19370 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19371 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19372 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
19375 case OFFSETOF_EXPR
:
19378 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
19379 in_decl
, /*function_p=*/false,
19380 /*integral_constant_expression_p=*/false);
19381 RETURN (finish_offsetof (object_ptr
,
19382 RECUR (TREE_OPERAND (t
, 0)),
19383 EXPR_LOCATION (t
)));
19386 case ADDRESSOF_EXPR
:
19387 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
19388 RECUR (TREE_OPERAND (t
, 0)), complain
));
19392 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
19393 complain
, in_decl
);
19395 tree type2
= TRAIT_EXPR_TYPE2 (t
);
19396 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
19397 type2
= RECUR (type2
);
19399 type2
= tsubst (type2
, args
, complain
, in_decl
);
19401 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
19406 tree old_stmt_expr
= cur_stmt_expr
;
19407 tree stmt_expr
= begin_stmt_expr ();
19409 cur_stmt_expr
= stmt_expr
;
19410 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
19411 integral_constant_expression_p
);
19412 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
19413 cur_stmt_expr
= old_stmt_expr
;
19415 /* If the resulting list of expression statement is empty,
19416 fold it further into void_node. */
19417 if (empty_expr_stmt_p (stmt_expr
))
19418 stmt_expr
= void_node
;
19420 RETURN (stmt_expr
);
19425 if (complain
& tf_partial
)
19427 /* We don't have a full set of template arguments yet; don't touch
19428 the lambda at all. */
19429 gcc_assert (processing_template_decl
);
19432 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
19434 RETURN (build_lambda_object (r
));
19438 /* We can get here for a constant initializer of non-dependent type.
19439 FIXME stop folding in cp_parser_initializer_clause. */
19441 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
19446 case TRANSACTION_EXPR
:
19447 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
19448 integral_constant_expression_p
));
19451 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
19453 case VEC_PERM_EXPR
:
19455 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19456 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19457 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19458 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19462 case REQUIRES_EXPR
:
19463 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19466 /* No need to substitute further, a RANGE_EXPR will always be built
19467 with constant operands. */
19470 case NON_LVALUE_EXPR
:
19471 case VIEW_CONVERT_EXPR
:
19472 if (location_wrapper_p (t
))
19473 /* We need to do this here as well as in tsubst_copy so we get the
19474 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19475 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19476 EXPR_LOCATION (t
)));
19480 /* Handle Objective-C++ constructs, if appropriate. */
19483 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19484 in_decl
, /*function_p=*/false);
19488 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19494 input_location
= loc
;
19498 /* Verify that the instantiated ARGS are valid. For type arguments,
19499 make sure that the type's linkage is ok. For non-type arguments,
19500 make sure they are constants if they are integral or enumerations.
19501 Emit an error under control of COMPLAIN, and return TRUE on error. */
19504 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19506 if (dependent_template_arg_p (t
))
19508 if (ARGUMENT_PACK_P (t
))
19510 tree vec
= ARGUMENT_PACK_ARGS (t
);
19511 int len
= TREE_VEC_LENGTH (vec
);
19512 bool result
= false;
19515 for (i
= 0; i
< len
; ++i
)
19516 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19520 else if (TYPE_P (t
))
19522 /* [basic.link]: A name with no linkage (notably, the name
19523 of a class or enumeration declared in a local scope)
19524 shall not be used to declare an entity with linkage.
19525 This implies that names with no linkage cannot be used as
19528 DR 757 relaxes this restriction for C++0x. */
19529 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19530 : no_linkage_check (t
, /*relaxed_p=*/false));
19534 /* DR 488 makes use of a type with no linkage cause
19535 type deduction to fail. */
19536 if (complain
& tf_error
)
19538 if (TYPE_UNNAMED_P (nt
))
19539 error ("%qT is/uses unnamed type", t
);
19541 error ("template argument for %qD uses local type %qT",
19546 /* In order to avoid all sorts of complications, we do not
19547 allow variably-modified types as template arguments. */
19548 else if (variably_modified_type_p (t
, NULL_TREE
))
19550 if (complain
& tf_error
)
19551 error ("%qT is a variably modified type", t
);
19555 /* Class template and alias template arguments should be OK. */
19556 else if (DECL_TYPE_TEMPLATE_P (t
))
19558 /* A non-type argument of integral or enumerated type must be a
19560 else if (TREE_TYPE (t
)
19561 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19562 && !REFERENCE_REF_P (t
)
19563 && !TREE_CONSTANT (t
))
19565 if (complain
& tf_error
)
19566 error ("integral expression %qE is not constant", t
);
19573 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19575 int ix
, len
= DECL_NTPARMS (tmpl
);
19576 bool result
= false;
19578 for (ix
= 0; ix
!= len
; ix
++)
19580 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19583 if (result
&& (complain
& tf_error
))
19584 error (" trying to instantiate %qD", tmpl
);
19588 /* We're out of SFINAE context now, so generate diagnostics for the access
19589 errors we saw earlier when instantiating D from TMPL and ARGS. */
19592 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19594 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19595 tree type
= TREE_TYPE (pattern
);
19596 location_t loc
= input_location
;
19598 push_access_scope (d
);
19599 push_deferring_access_checks (dk_no_deferred
);
19600 input_location
= DECL_SOURCE_LOCATION (pattern
);
19601 tsubst (type
, args
, tf_warning_or_error
, d
);
19602 input_location
= loc
;
19603 pop_deferring_access_checks ();
19604 pop_access_scope (d
);
19607 /* Instantiate the indicated variable, function, or alias template TMPL with
19608 the template arguments in TARG_PTR. */
19611 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19613 tree targ_ptr
= orig_args
;
19617 bool access_ok
= true;
19619 if (tmpl
== error_mark_node
)
19620 return error_mark_node
;
19622 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19624 /* If this function is a clone, handle it specially. */
19625 if (DECL_CLONED_FUNCTION_P (tmpl
))
19630 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19631 DECL_CLONED_FUNCTION. */
19632 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19633 targ_ptr
, complain
);
19634 if (spec
== error_mark_node
)
19635 return error_mark_node
;
19637 /* Look for the clone. */
19638 FOR_EACH_CLONE (clone
, spec
)
19639 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19641 /* We should always have found the clone by now. */
19642 gcc_unreachable ();
19646 if (targ_ptr
== error_mark_node
)
19647 return error_mark_node
;
19649 /* Check to see if we already have this specialization. */
19650 gen_tmpl
= most_general_template (tmpl
);
19651 if (TMPL_ARGS_DEPTH (targ_ptr
)
19652 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19653 /* targ_ptr only has the innermost template args, so add the outer ones
19654 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19655 the case of a non-dependent call within a template definition). */
19656 targ_ptr
= (add_outermost_template_args
19657 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19660 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19661 but it doesn't seem to be on the hot path. */
19662 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19664 gcc_assert (tmpl
== gen_tmpl
19665 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19667 || fndecl
== NULL_TREE
);
19669 if (spec
!= NULL_TREE
)
19671 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19673 if (complain
& tf_error
)
19674 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19675 return error_mark_node
;
19680 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19682 return error_mark_node
;
19684 /* We are building a FUNCTION_DECL, during which the access of its
19685 parameters and return types have to be checked. However this
19686 FUNCTION_DECL which is the desired context for access checking
19687 is not built yet. We solve this chicken-and-egg problem by
19688 deferring all checks until we have the FUNCTION_DECL. */
19689 push_deferring_access_checks (dk_deferred
);
19691 /* Instantiation of the function happens in the context of the function
19692 template, not the context of the overload resolution we're doing. */
19693 push_to_top_level ();
19694 /* If there are dependent arguments, e.g. because we're doing partial
19695 ordering, make sure processing_template_decl stays set. */
19696 if (uses_template_parms (targ_ptr
))
19697 ++processing_template_decl
;
19698 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19700 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19701 complain
, gen_tmpl
, true);
19702 push_nested_class (ctx
);
19705 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19707 fndecl
= NULL_TREE
;
19708 if (VAR_P (pattern
))
19710 /* We need to determine if we're using a partial or explicit
19711 specialization now, because the type of the variable could be
19713 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19714 tree elt
= most_specialized_partial_spec (tid
, complain
);
19715 if (elt
== error_mark_node
)
19716 pattern
= error_mark_node
;
19719 tree partial_tmpl
= TREE_VALUE (elt
);
19720 tree partial_args
= TREE_PURPOSE (elt
);
19721 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19722 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19726 /* Substitute template parameters to obtain the specialization. */
19727 if (fndecl
== NULL_TREE
)
19728 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19729 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19730 pop_nested_class ();
19731 pop_from_top_level ();
19733 if (fndecl
== error_mark_node
)
19735 pop_deferring_access_checks ();
19736 return error_mark_node
;
19739 /* The DECL_TI_TEMPLATE should always be the immediate parent
19740 template, not the most general template. */
19741 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19742 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19744 /* Now we know the specialization, compute access previously
19745 deferred. Do no access control for inheriting constructors,
19746 as we already checked access for the inherited constructor. */
19747 if (!(flag_new_inheriting_ctors
19748 && DECL_INHERITED_CTOR (fndecl
)))
19750 push_access_scope (fndecl
);
19751 if (!perform_deferred_access_checks (complain
))
19753 pop_access_scope (fndecl
);
19755 pop_deferring_access_checks ();
19757 /* If we've just instantiated the main entry point for a function,
19758 instantiate all the alternate entry points as well. We do this
19759 by cloning the instantiation of the main entry point, not by
19760 instantiating the template clones. */
19761 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
19762 clone_function_decl (fndecl
, /*update_methods=*/false);
19766 if (!(complain
& tf_error
))
19768 /* Remember to reinstantiate when we're out of SFINAE so the user
19769 can see the errors. */
19770 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19772 return error_mark_node
;
19777 /* Wrapper for instantiate_template_1. */
19780 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19783 timevar_push (TV_TEMPLATE_INST
);
19784 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19785 timevar_pop (TV_TEMPLATE_INST
);
19789 /* Instantiate the alias template TMPL with ARGS. Also push a template
19790 instantiation level, which instantiate_template doesn't do because
19791 functions and variables have sufficient context established by the
19795 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19797 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19798 return error_mark_node
;
19799 if (!push_tinst_level (tmpl
, args
))
19800 return error_mark_node
;
19803 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19804 args
, tmpl
, complain
,
19805 /*require_all_args=*/true,
19806 /*use_default_args=*/true);
19808 tree r
= instantiate_template (tmpl
, args
, complain
);
19809 pop_tinst_level ();
19814 /* PARM is a template parameter pack for FN. Returns true iff
19815 PARM is used in a deducible way in the argument list of FN. */
19818 pack_deducible_p (tree parm
, tree fn
)
19820 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19821 for (; t
; t
= TREE_CHAIN (t
))
19823 tree type
= TREE_VALUE (t
);
19825 if (!PACK_EXPANSION_P (type
))
19827 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19828 packs
; packs
= TREE_CHAIN (packs
))
19829 if (template_args_equal (TREE_VALUE (packs
), parm
))
19831 /* The template parameter pack is used in a function parameter
19832 pack. If this is the end of the parameter list, the
19833 template parameter pack is deducible. */
19834 if (TREE_CHAIN (t
) == void_list_node
)
19837 /* Otherwise, not. Well, it could be deduced from
19838 a non-pack parameter, but doing so would end up with
19839 a deduction mismatch, so don't bother. */
19843 /* The template parameter pack isn't used in any function parameter
19844 packs, but it might be used deeper, e.g. tuple<Args...>. */
19848 /* Subroutine of fn_type_unification: check non-dependent parms for
19852 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
19853 tree fn
, unification_kind_t strict
, int flags
,
19854 struct conversion
**convs
, bool explain_p
)
19856 /* Non-constructor methods need to leave a conversion for 'this', which
19857 isn't included in nargs here. */
19858 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19859 && !DECL_CONSTRUCTOR_P (fn
));
19861 for (unsigned ia
= 0;
19862 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19864 tree parm
= TREE_VALUE (parms
);
19866 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19867 && (!TREE_CHAIN (parms
)
19868 || TREE_CHAIN (parms
) == void_list_node
))
19869 /* For a function parameter pack that occurs at the end of the
19870 parameter-declaration-list, the type A of each remaining
19871 argument of the call is compared with the type P of the
19872 declarator-id of the function parameter pack. */
19875 parms
= TREE_CHAIN (parms
);
19877 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19878 /* For a function parameter pack that does not occur at the
19879 end of the parameter-declaration-list, the type of the
19880 parameter pack is a non-deduced context. */
19883 if (!uses_template_parms (parm
))
19885 tree arg
= args
[ia
];
19886 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
19887 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
19889 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
19890 conv_p
, explain_p
))
19900 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19901 NARGS elements of the arguments that are being used when calling
19902 it. TARGS is a vector into which the deduced template arguments
19905 Returns either a FUNCTION_DECL for the matching specialization of FN or
19906 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19907 true, diagnostics will be printed to explain why it failed.
19909 If FN is a conversion operator, or we are trying to produce a specific
19910 specialization, RETURN_TYPE is the return type desired.
19912 The EXPLICIT_TARGS are explicit template arguments provided via a
19915 The parameter STRICT is one of:
19918 We are deducing arguments for a function call, as in
19919 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19920 deducing arguments for a call to the result of a conversion
19921 function template, as in [over.call.object].
19924 We are deducing arguments for a conversion function, as in
19925 [temp.deduct.conv].
19928 We are deducing arguments when doing an explicit instantiation
19929 as in [temp.explicit], when determining an explicit specialization
19930 as in [temp.expl.spec], or when taking the address of a function
19931 template, as in [temp.deduct.funcaddr]. */
19934 fn_type_unification (tree fn
,
19935 tree explicit_targs
,
19938 unsigned int nargs
,
19940 unification_kind_t strict
,
19942 struct conversion
**convs
,
19948 tree decl
= NULL_TREE
;
19949 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
19951 static int deduction_depth
;
19952 /* type_unification_real will pass back any access checks from default
19953 template argument substitution. */
19954 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
19955 /* We don't have all the template args yet. */
19956 bool incomplete
= true;
19959 if (flag_new_inheriting_ctors
)
19960 fn
= strip_inheriting_ctors (fn
);
19962 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
19963 tree r
= error_mark_node
;
19965 tree full_targs
= targs
;
19966 if (TMPL_ARGS_DEPTH (targs
)
19967 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
19968 full_targs
= (add_outermost_template_args
19969 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
19973 complain
|= tf_decltype
;
19975 /* In C++0x, it's possible to have a function template whose type depends
19976 on itself recursively. This is most obvious with decltype, but can also
19977 occur with enumeration scope (c++/48969). So we need to catch infinite
19978 recursion and reject the substitution at deduction time; this function
19979 will return error_mark_node for any repeated substitution.
19981 This also catches excessive recursion such as when f<N> depends on
19982 f<N-1> across all integers, and returns error_mark_node for all the
19983 substitutions back up to the initial one.
19985 This is, of course, not reentrant. */
19986 if (excessive_deduction_depth
)
19987 return error_mark_node
;
19990 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
19992 fntype
= TREE_TYPE (fn
);
19993 if (explicit_targs
)
19997 The specified template arguments must match the template
19998 parameters in kind (i.e., type, nontype, template), and there
19999 must not be more arguments than there are parameters;
20000 otherwise type deduction fails.
20002 Nontype arguments must match the types of the corresponding
20003 nontype template parameters, or must be convertible to the
20004 types of the corresponding nontype parameters as specified in
20005 _temp.arg.nontype_, otherwise type deduction fails.
20007 All references in the function type of the function template
20008 to the corresponding template parameters are replaced by the
20009 specified template argument values. If a substitution in a
20010 template parameter or in the function type of the function
20011 template results in an invalid type, type deduction fails. */
20012 int i
, len
= TREE_VEC_LENGTH (tparms
);
20013 location_t loc
= input_location
;
20014 incomplete
= false;
20016 if (explicit_targs
== error_mark_node
)
20019 if (TMPL_ARGS_DEPTH (explicit_targs
)
20020 < TMPL_ARGS_DEPTH (full_targs
))
20021 explicit_targs
= add_outermost_template_args (full_targs
,
20024 /* Adjust any explicit template arguments before entering the
20025 substitution context. */
20027 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20029 /*require_all_args=*/false,
20030 /*use_default_args=*/false));
20031 if (explicit_targs
== error_mark_node
)
20034 /* Substitute the explicit args into the function type. This is
20035 necessary so that, for instance, explicitly declared function
20036 arguments can match null pointed constants. If we were given
20037 an incomplete set of explicit args, we must not do semantic
20038 processing during substitution as we could create partial
20040 for (i
= 0; i
< len
; i
++)
20042 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20043 bool parameter_pack
= false;
20044 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20046 /* Dig out the actual parm. */
20047 if (TREE_CODE (parm
) == TYPE_DECL
20048 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20050 parm
= TREE_TYPE (parm
);
20051 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20053 else if (TREE_CODE (parm
) == PARM_DECL
)
20055 parm
= DECL_INITIAL (parm
);
20056 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20059 if (!parameter_pack
&& targ
== NULL_TREE
)
20060 /* No explicit argument for this template parameter. */
20063 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20065 /* Mark the argument pack as "incomplete". We could
20066 still deduce more arguments during unification.
20067 We remove this mark in type_unification_real. */
20070 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20071 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20072 = ARGUMENT_PACK_ARGS (targ
);
20075 /* We have some incomplete argument packs. */
20082 if (!push_tinst_level (fn
, explicit_targs
))
20084 excessive_deduction_depth
= true;
20087 ++processing_template_decl
;
20088 input_location
= DECL_SOURCE_LOCATION (fn
);
20089 /* Ignore any access checks; we'll see them again in
20090 instantiate_template and they might have the wrong
20091 access path at this point. */
20092 push_deferring_access_checks (dk_deferred
);
20093 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20094 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20095 pop_deferring_access_checks ();
20096 input_location
= loc
;
20097 --processing_template_decl
;
20098 pop_tinst_level ();
20100 if (fntype
== error_mark_node
)
20104 /* Place the explicitly specified arguments in TARGS. */
20105 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20106 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20107 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20108 if (!incomplete
&& CHECKING_P
20109 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20110 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20111 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20114 if (return_type
&& strict
!= DEDUCE_CALL
)
20116 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20117 new_args
[0] = return_type
;
20118 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20126 /* Never do unification on the 'this' parameter. */
20127 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20129 if (return_type
&& strict
== DEDUCE_CALL
)
20131 /* We're deducing for a call to the result of a template conversion
20132 function. The parms we really want are in return_type. */
20133 if (INDIRECT_TYPE_P (return_type
))
20134 return_type
= TREE_TYPE (return_type
);
20135 parms
= TYPE_ARG_TYPES (return_type
);
20137 else if (return_type
)
20139 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20142 /* We allow incomplete unification without an error message here
20143 because the standard doesn't seem to explicitly prohibit it. Our
20144 callers must be ready to deal with unification failures in any
20147 /* If we aren't explaining yet, push tinst context so we can see where
20148 any errors (e.g. from class instantiations triggered by instantiation
20149 of default template arguments) come from. If we are explaining, this
20150 context is redundant. */
20151 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20153 excessive_deduction_depth
= true;
20157 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20158 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20159 strict
, &checks
, explain_p
);
20161 pop_tinst_level ();
20165 /* Now that we have bindings for all of the template arguments,
20166 ensure that the arguments deduced for the template template
20167 parameters have compatible template parameter lists. We cannot
20168 check this property before we have deduced all template
20169 arguments, because the template parameter types of a template
20170 template parameter might depend on prior template parameters
20171 deduced after the template template parameter. The following
20172 ill-formed example illustrates this issue:
20174 template<typename T, template<T> class C> void f(C<5>, T);
20176 template<int N> struct X {};
20179 f(X<5>(), 5l); // error: template argument deduction fails
20182 The template parameter list of 'C' depends on the template type
20183 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20184 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20185 time that we deduce 'C'. */
20186 if (!template_template_parm_bindings_ok_p
20187 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20189 unify_inconsistent_template_template_parameters (explain_p
);
20193 /* DR 1391: All parameters have args, now check non-dependent parms for
20195 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20200 /* All is well so far. Now, check:
20204 When all template arguments have been deduced, all uses of
20205 template parameters in nondeduced contexts are replaced with
20206 the corresponding deduced argument values. If the
20207 substitution results in an invalid type, as described above,
20208 type deduction fails. */
20209 if (!push_tinst_level (fn
, targs
))
20211 excessive_deduction_depth
= true;
20215 /* Also collect access checks from the instantiation. */
20216 reopen_deferring_access_checks (checks
);
20218 decl
= instantiate_template (fn
, targs
, complain
);
20220 checks
= get_deferred_access_checks ();
20221 pop_deferring_access_checks ();
20223 pop_tinst_level ();
20225 if (decl
== error_mark_node
)
20228 /* Now perform any access checks encountered during substitution. */
20229 push_access_scope (decl
);
20230 ok
= perform_access_checks (checks
, complain
);
20231 pop_access_scope (decl
);
20235 /* If we're looking for an exact match, check that what we got
20236 is indeed an exact match. It might not be if some template
20237 parameters are used in non-deduced contexts. But don't check
20238 for an exact match if we have dependent template arguments;
20239 in that case we're doing partial ordering, and we already know
20240 that we have two candidates that will provide the actual type. */
20241 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20243 tree substed
= TREE_TYPE (decl
);
20247 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20249 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20250 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20251 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20253 unify_type_mismatch (explain_p
, args
[i
],
20254 TREE_VALUE (sarg
));
20259 /* After doing deduction with the inherited constructor, actually return an
20260 instantiation of the inheriting constructor. */
20262 decl
= instantiate_template (orig_fn
, targs
, complain
);
20268 if (excessive_deduction_depth
)
20270 if (deduction_depth
== 0)
20271 /* Reset once we're all the way out. */
20272 excessive_deduction_depth
= false;
20278 /* Adjust types before performing type deduction, as described in
20279 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20280 sections are symmetric. PARM is the type of a function parameter
20281 or the return type of the conversion function. ARG is the type of
20282 the argument passed to the call, or the type of the value
20283 initialized with the result of the conversion function.
20284 ARG_EXPR is the original argument expression, which may be null. */
20287 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20300 /* Swap PARM and ARG throughout the remainder of this
20301 function; the handling is precisely symmetric since PARM
20302 will initialize ARG rather than vice versa. */
20303 std::swap (parm
, arg
);
20307 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20308 too, but here handle it by stripping the reference from PARM
20309 rather than by adding it to ARG. */
20310 if (TYPE_REF_P (*parm
)
20311 && TYPE_REF_IS_RVALUE (*parm
)
20312 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20313 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20314 && TYPE_REF_P (*arg
)
20315 && !TYPE_REF_IS_RVALUE (*arg
))
20316 *parm
= TREE_TYPE (*parm
);
20317 /* Nothing else to do in this case. */
20321 gcc_unreachable ();
20324 if (!TYPE_REF_P (*parm
))
20326 /* [temp.deduct.call]
20328 If P is not a reference type:
20330 --If A is an array type, the pointer type produced by the
20331 array-to-pointer standard conversion (_conv.array_) is
20332 used in place of A for type deduction; otherwise,
20334 --If A is a function type, the pointer type produced by
20335 the function-to-pointer standard conversion
20336 (_conv.func_) is used in place of A for type deduction;
20339 --If A is a cv-qualified type, the top level
20340 cv-qualifiers of A's type are ignored for type
20342 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
20343 *arg
= build_pointer_type (TREE_TYPE (*arg
));
20344 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
20345 *arg
= build_pointer_type (*arg
);
20347 *arg
= TYPE_MAIN_VARIANT (*arg
);
20350 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20351 reference to a cv-unqualified template parameter that does not represent a
20352 template parameter of a class template (during class template argument
20353 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20354 an lvalue, the type "lvalue reference to A" is used in place of A for type
20356 if (TYPE_REF_P (*parm
)
20357 && TYPE_REF_IS_RVALUE (*parm
)
20358 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20359 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
20360 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20361 && (arg_expr
? lvalue_p (arg_expr
)
20362 /* try_one_overload doesn't provide an arg_expr, but
20363 functions are always lvalues. */
20364 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
20365 *arg
= build_reference_type (*arg
);
20367 /* [temp.deduct.call]
20369 If P is a cv-qualified type, the top level cv-qualifiers
20370 of P's type are ignored for type deduction. If P is a
20371 reference type, the type referred to by P is used for
20373 *parm
= TYPE_MAIN_VARIANT (*parm
);
20374 if (TYPE_REF_P (*parm
))
20376 *parm
= TREE_TYPE (*parm
);
20377 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20380 /* DR 322. For conversion deduction, remove a reference type on parm
20381 too (which has been swapped into ARG). */
20382 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
20383 *arg
= TREE_TYPE (*arg
);
20388 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20389 template which doesn't contain any deducible template parameters; check if
20390 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20391 unify_one_argument. */
20394 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
20395 int flags
, struct conversion
**conv_p
,
20401 type
= TREE_TYPE (arg
);
20405 if (same_type_p (parm
, type
))
20406 return unify_success (explain_p
);
20408 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20409 if (strict
== DEDUCE_CONV
)
20411 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
20412 return unify_success (explain_p
);
20414 else if (strict
!= DEDUCE_EXACT
)
20417 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
20419 /* Avoid recalculating this in add_function_candidate. */
20421 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
20423 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
20425 return unify_success (explain_p
);
20428 if (strict
== DEDUCE_EXACT
)
20429 return unify_type_mismatch (explain_p
, parm
, arg
);
20431 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
20434 static bool uses_deducible_template_parms (tree type
);
20436 /* Returns true iff the expression EXPR is one from which a template
20437 argument can be deduced. In other words, if it's an undecorated
20438 use of a template non-type parameter. */
20441 deducible_expression (tree expr
)
20443 /* Strip implicit conversions. */
20444 while (CONVERT_EXPR_P (expr
))
20445 expr
= TREE_OPERAND (expr
, 0);
20446 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
20449 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20450 deducible way; that is, if it has a max value of <PARM> - 1. */
20453 deducible_array_bound (tree domain
)
20455 if (domain
== NULL_TREE
)
20458 tree max
= TYPE_MAX_VALUE (domain
);
20459 if (TREE_CODE (max
) != MINUS_EXPR
)
20462 return deducible_expression (TREE_OPERAND (max
, 0));
20465 /* Returns true iff the template arguments ARGS use a template parameter
20466 in a deducible way. */
20469 deducible_template_args (tree args
)
20471 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20474 tree elt
= TREE_VEC_ELT (args
, i
);
20475 if (ARGUMENT_PACK_P (elt
))
20476 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20479 if (PACK_EXPANSION_P (elt
))
20480 elt
= PACK_EXPANSION_PATTERN (elt
);
20481 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20483 else if (TYPE_P (elt
))
20484 deducible
= uses_deducible_template_parms (elt
);
20486 deducible
= deducible_expression (elt
);
20494 /* Returns true iff TYPE contains any deducible references to template
20495 parameters, as per 14.8.2.5. */
20498 uses_deducible_template_parms (tree type
)
20500 if (PACK_EXPANSION_P (type
))
20501 type
= PACK_EXPANSION_PATTERN (type
);
20508 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20509 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20515 if (INDIRECT_TYPE_P (type
))
20516 return uses_deducible_template_parms (TREE_TYPE (type
));
20518 /* T[integer-constant ]
20520 if (TREE_CODE (type
) == ARRAY_TYPE
)
20521 return (uses_deducible_template_parms (TREE_TYPE (type
))
20522 || deducible_array_bound (TYPE_DOMAIN (type
)));
20534 if (TYPE_PTRMEM_P (type
))
20535 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20536 || (uses_deducible_template_parms
20537 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20539 /* template-name <T> (where template-name refers to a class template)
20540 template-name <i> (where template-name refers to a class template) */
20541 if (CLASS_TYPE_P (type
)
20542 && CLASSTYPE_TEMPLATE_INFO (type
)
20543 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20544 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20545 (CLASSTYPE_TI_ARGS (type
)));
20550 if (TREE_CODE (type
) == FUNCTION_TYPE
20551 || TREE_CODE (type
) == METHOD_TYPE
)
20553 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20555 tree parm
= TYPE_ARG_TYPES (type
);
20556 if (TREE_CODE (type
) == METHOD_TYPE
)
20557 parm
= TREE_CHAIN (parm
);
20558 for (; parm
; parm
= TREE_CHAIN (parm
))
20559 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20566 /* Subroutine of type_unification_real and unify_pack_expansion to
20567 handle unification of a single P/A pair. Parameters are as
20568 for those functions. */
20571 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20572 int subr
, unification_kind_t strict
,
20575 tree arg_expr
= NULL_TREE
;
20578 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20579 return unify_invalid (explain_p
);
20580 if (arg
== unknown_type_node
)
20581 /* We can't deduce anything from this, but we might get all the
20582 template args from other function args. */
20583 return unify_success (explain_p
);
20585 /* Implicit conversions (Clause 4) will be performed on a function
20586 argument to convert it to the type of the corresponding function
20587 parameter if the parameter type contains no template-parameters that
20588 participate in template argument deduction. */
20589 if (strict
!= DEDUCE_EXACT
20590 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20591 /* For function parameters with no deducible template parameters,
20592 just return. We'll check non-dependent conversions later. */
20593 return unify_success (explain_p
);
20598 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20599 | UNIFY_ALLOW_MORE_CV_QUAL
20600 | UNIFY_ALLOW_DERIVED
);
20604 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20608 arg_strict
= UNIFY_ALLOW_NONE
;
20612 gcc_unreachable ();
20615 /* We only do these transformations if this is the top-level
20616 parameter_type_list in a call or declaration matching; in other
20617 situations (nested function declarators, template argument lists) we
20618 won't be comparing a type to an expression, and we don't do any type
20624 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20625 if (type_unknown_p (arg
))
20627 /* [temp.deduct.type] A template-argument can be
20628 deduced from a pointer to function or pointer
20629 to member function argument if the set of
20630 overloaded functions does not contain function
20631 templates and at most one of a set of
20632 overloaded functions provides a unique
20634 resolve_overloaded_unification (tparms
, targs
, parm
,
20636 arg_strict
, explain_p
);
20637 /* If a unique match was not found, this is a
20638 non-deduced context, so we still succeed. */
20639 return unify_success (explain_p
);
20643 arg
= unlowered_expr_type (arg
);
20644 if (arg
== error_mark_node
)
20645 return unify_invalid (explain_p
);
20649 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20652 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20653 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20654 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20656 /* For deduction from an init-list we need the actual list. */
20657 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20659 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20662 /* for_each_template_parm callback that always returns 0. */
20665 zero_r (tree
, void *)
20670 /* for_each_template_parm any_fn callback to handle deduction of a template
20671 type argument from the type of an array bound. */
20674 array_deduction_r (tree t
, void *data
)
20676 tree_pair_p d
= (tree_pair_p
)data
;
20677 tree
&tparms
= d
->purpose
;
20678 tree
&targs
= d
->value
;
20680 if (TREE_CODE (t
) == ARRAY_TYPE
)
20681 if (tree dom
= TYPE_DOMAIN (t
))
20682 if (tree max
= TYPE_MAX_VALUE (dom
))
20684 if (TREE_CODE (max
) == MINUS_EXPR
)
20685 max
= TREE_OPERAND (max
, 0);
20686 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20687 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20688 UNIFY_ALLOW_NONE
, /*explain*/false);
20691 /* Keep walking. */
20695 /* Try to deduce any not-yet-deduced template type arguments from the type of
20696 an array bound. This is handled separately from unify because 14.8.2.5 says
20697 "The type of a type parameter is only deduced from an array bound if it is
20698 not otherwise deduced." */
20701 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20703 tree_pair_s data
= { tparms
, targs
};
20704 hash_set
<tree
> visited
;
20705 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20706 /*nondeduced*/false, array_deduction_r
);
20709 /* Most parms like fn_type_unification.
20711 If SUBR is 1, we're being called recursively (to unify the
20712 arguments of a function or method parameter of a function
20715 CHECKS is a pointer to a vector of access checks encountered while
20716 substituting default template arguments. */
20719 type_unification_real (tree tparms
,
20723 unsigned int xnargs
,
20725 unification_kind_t strict
,
20726 vec
<deferred_access_check
, va_gc
> **checks
,
20731 int ntparms
= TREE_VEC_LENGTH (tparms
);
20732 int saw_undeduced
= 0;
20735 unsigned int nargs
;
20738 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20739 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20740 gcc_assert (ntparms
> 0);
20742 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20744 /* Reset the number of non-defaulted template arguments contained
20746 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20754 while (parms
&& parms
!= void_list_node
20757 parm
= TREE_VALUE (parms
);
20759 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20760 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20761 /* For a function parameter pack that occurs at the end of the
20762 parameter-declaration-list, the type A of each remaining
20763 argument of the call is compared with the type P of the
20764 declarator-id of the function parameter pack. */
20767 parms
= TREE_CHAIN (parms
);
20769 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20770 /* For a function parameter pack that does not occur at the
20771 end of the parameter-declaration-list, the type of the
20772 parameter pack is a non-deduced context. */
20778 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20784 && parms
!= void_list_node
20785 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20787 /* Unify the remaining arguments with the pack expansion type. */
20789 tree parmvec
= make_tree_vec (1);
20791 /* Allocate a TREE_VEC and copy in all of the arguments */
20792 argvec
= make_tree_vec (nargs
- ia
);
20793 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20794 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20796 /* Copy the parameter into parmvec. */
20797 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20798 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20799 /*subr=*/subr
, explain_p
))
20802 /* Advance to the end of the list of parameters. */
20803 parms
= TREE_CHAIN (parms
);
20806 /* Fail if we've reached the end of the parm list, and more args
20807 are present, and the parm list isn't variadic. */
20808 if (ia
< nargs
&& parms
== void_list_node
)
20809 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20810 /* Fail if parms are left and they don't have default values and
20811 they aren't all deduced as empty packs (c++/57397). This is
20812 consistent with sufficient_parms_p. */
20813 if (parms
&& parms
!= void_list_node
20814 && TREE_PURPOSE (parms
) == NULL_TREE
)
20816 unsigned int count
= nargs
;
20821 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20824 p
= TREE_CHAIN (p
);
20826 while (p
&& p
!= void_list_node
);
20827 if (count
!= nargs
)
20828 return unify_too_few_arguments (explain_p
, ia
, count
,
20834 tsubst_flags_t complain
= (explain_p
20835 ? tf_warning_or_error
20837 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20839 for (i
= 0; i
< ntparms
; i
++)
20841 tree targ
= TREE_VEC_ELT (targs
, i
);
20842 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20844 /* Clear the "incomplete" flags on all argument packs now so that
20845 substituting them into later default arguments works. */
20846 if (targ
&& ARGUMENT_PACK_P (targ
))
20848 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
20849 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
20852 if (targ
|| tparm
== error_mark_node
)
20854 tparm
= TREE_VALUE (tparm
);
20856 if (TREE_CODE (tparm
) == TYPE_DECL
20857 && !tried_array_deduction
)
20859 try_array_deduction (tparms
, targs
, xparms
);
20860 tried_array_deduction
= true;
20861 if (TREE_VEC_ELT (targs
, i
))
20865 /* If this is an undeduced nontype parameter that depends on
20866 a type parameter, try another pass; its type may have been
20867 deduced from a later argument than the one from which
20868 this parameter can be deduced. */
20869 if (TREE_CODE (tparm
) == PARM_DECL
20870 && uses_template_parms (TREE_TYPE (tparm
))
20871 && saw_undeduced
< 2)
20877 /* Core issue #226 (C++0x) [temp.deduct]:
20879 If a template argument has not been deduced, its
20880 default template argument, if any, is used.
20882 When we are in C++98 mode, TREE_PURPOSE will either
20883 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20884 to explicitly check cxx_dialect here. */
20885 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20886 /* OK, there is a default argument. Wait until after the
20887 conversion check to do substitution. */
20890 /* If the type parameter is a parameter pack, then it will
20891 be deduced to an empty parameter pack. */
20892 if (template_parameter_pack_p (tparm
))
20896 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20898 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20899 TREE_CONSTANT (arg
) = 1;
20902 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20904 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
20906 TREE_VEC_ELT (targs
, i
) = arg
;
20910 return unify_parameter_deduction_failure (explain_p
, tparm
);
20913 /* Now substitute into the default template arguments. */
20914 for (i
= 0; i
< ntparms
; i
++)
20916 tree targ
= TREE_VEC_ELT (targs
, i
);
20917 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20919 if (targ
|| tparm
== error_mark_node
)
20921 tree parm
= TREE_VALUE (tparm
);
20922 tree arg
= TREE_PURPOSE (tparm
);
20923 reopen_deferring_access_checks (*checks
);
20924 location_t save_loc
= input_location
;
20926 input_location
= DECL_SOURCE_LOCATION (parm
);
20928 if (saw_undeduced
== 1
20929 && TREE_CODE (parm
) == PARM_DECL
20930 && uses_template_parms (TREE_TYPE (parm
)))
20932 /* The type of this non-type parameter depends on undeduced
20933 parameters. Don't try to use its default argument yet,
20934 since we might deduce an argument for it on the next pass,
20935 but do check whether the arguments we already have cause
20936 substitution failure, so that that happens before we try
20937 later default arguments (78489). */
20938 ++processing_template_decl
;
20939 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
20941 --processing_template_decl
;
20942 if (type
== error_mark_node
)
20943 arg
= error_mark_node
;
20949 tree substed
= NULL_TREE
;
20950 if (saw_undeduced
== 1 && processing_template_decl
== 0)
20952 /* First instatiate in template context, in case we still
20953 depend on undeduced template parameters. */
20954 ++processing_template_decl
;
20955 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20957 --processing_template_decl
;
20958 if (substed
!= error_mark_node
20959 && !uses_template_parms (substed
))
20960 /* We replaced all the tparms, substitute again out of
20961 template context. */
20962 substed
= NULL_TREE
;
20965 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
20968 if (!uses_template_parms (substed
))
20969 arg
= convert_template_argument (parm
, substed
, full_targs
,
20970 complain
, i
, NULL_TREE
);
20971 else if (saw_undeduced
== 1)
20974 arg
= error_mark_node
;
20977 input_location
= save_loc
;
20978 *checks
= get_deferred_access_checks ();
20979 pop_deferring_access_checks ();
20981 if (arg
== error_mark_node
)
20985 TREE_VEC_ELT (targs
, i
) = arg
;
20986 /* The position of the first default template argument,
20987 is also the number of non-defaulted arguments in TARGS.
20989 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
20994 if (saw_undeduced
++ == 1)
20998 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20999 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21001 return unify_success (explain_p
);
21004 /* Subroutine of type_unification_real. Args are like the variables
21005 at the call site. ARG is an overloaded function (or template-id);
21006 we try deducing template args from each of the overloads, and if
21007 only one succeeds, we go with that. Modifies TARGS and returns
21008 true on success. */
21011 resolve_overloaded_unification (tree tparms
,
21015 unification_kind_t strict
,
21019 tree tempargs
= copy_node (targs
);
21021 tree goodfn
= NULL_TREE
;
21024 if (TREE_CODE (arg
) == ADDR_EXPR
)
21026 arg
= TREE_OPERAND (arg
, 0);
21032 if (TREE_CODE (arg
) == COMPONENT_REF
)
21033 /* Handle `&x' where `x' is some static or non-static member
21035 arg
= TREE_OPERAND (arg
, 1);
21037 if (TREE_CODE (arg
) == OFFSET_REF
)
21038 arg
= TREE_OPERAND (arg
, 1);
21040 /* Strip baselink information. */
21041 if (BASELINK_P (arg
))
21042 arg
= BASELINK_FUNCTIONS (arg
);
21044 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21046 /* If we got some explicit template args, we need to plug them into
21047 the affected templates before we try to unify, in case the
21048 explicit args will completely resolve the templates in question. */
21051 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21052 arg
= TREE_OPERAND (arg
, 0);
21054 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21057 tree subargs
, elem
;
21059 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21062 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21063 expl_subargs
, NULL_TREE
, tf_none
,
21064 /*require_all_args=*/true,
21065 /*use_default_args=*/true);
21066 if (subargs
!= error_mark_node
21067 && !any_dependent_template_arguments_p (subargs
))
21069 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21070 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21071 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21072 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21081 /* If no templates (or more than one) are fully resolved by the
21082 explicit arguments, this template-id is a non-deduced context; it
21083 could still be OK if we deduce all template arguments for the
21084 enclosing call through other arguments. */
21088 else if (TREE_CODE (arg
) != OVERLOAD
21089 && TREE_CODE (arg
) != FUNCTION_DECL
)
21090 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21091 -- but the deduction does not succeed because the expression is
21092 not just the function on its own. */
21095 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21098 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21099 strict
, sub_strict
, addr_p
, explain_p
)
21100 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21107 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21108 to function or pointer to member function argument if the set of
21109 overloaded functions does not contain function templates and at most
21110 one of a set of overloaded functions provides a unique match.
21112 So if we found multiple possibilities, we return success but don't
21113 deduce anything. */
21117 int i
= TREE_VEC_LENGTH (targs
);
21119 if (TREE_VEC_ELT (tempargs
, i
))
21121 tree old
= TREE_VEC_ELT (targs
, i
);
21122 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21123 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21124 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21125 /* Don't forget explicit template arguments in a pack. */
21126 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21127 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21128 TREE_VEC_ELT (targs
, i
) = new_
;
21137 /* Core DR 115: In contexts where deduction is done and fails, or in
21138 contexts where deduction is not done, if a template argument list is
21139 specified and it, along with any default template arguments, identifies
21140 a single function template specialization, then the template-id is an
21141 lvalue for the function template specialization. */
21144 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21146 tree expr
, offset
, baselink
;
21149 if (!type_unknown_p (orig_expr
))
21154 offset
= NULL_TREE
;
21155 baselink
= NULL_TREE
;
21157 if (TREE_CODE (expr
) == ADDR_EXPR
)
21159 expr
= TREE_OPERAND (expr
, 0);
21162 if (TREE_CODE (expr
) == OFFSET_REF
)
21165 expr
= TREE_OPERAND (expr
, 1);
21167 if (BASELINK_P (expr
))
21170 expr
= BASELINK_FUNCTIONS (expr
);
21173 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21176 tree goodfn
= NULL_TREE
;
21178 /* If we got some explicit template args, we need to plug them into
21179 the affected templates before we try to unify, in case the
21180 explicit args will completely resolve the templates in question. */
21182 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21183 tree arg
= TREE_OPERAND (expr
, 0);
21184 tree badfn
= NULL_TREE
;
21185 tree badargs
= NULL_TREE
;
21187 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21190 tree subargs
, elem
;
21192 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21195 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21196 expl_subargs
, NULL_TREE
, tf_none
,
21197 /*require_all_args=*/true,
21198 /*use_default_args=*/true);
21199 if (subargs
!= error_mark_node
21200 && !any_dependent_template_arguments_p (subargs
))
21202 elem
= instantiate_template (fn
, subargs
, tf_none
);
21203 if (elem
== error_mark_node
)
21208 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21217 mark_used (goodfn
);
21220 expr
= build_baselink (BASELINK_BINFO (baselink
),
21221 BASELINK_ACCESS_BINFO (baselink
),
21222 expr
, BASELINK_OPTYPE (baselink
));
21226 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21227 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21230 expr
= cp_build_addr_expr (expr
, complain
);
21233 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21234 /* There were no good options and at least one bad one, so let the
21235 user know what the problem is. */
21236 instantiate_template (badfn
, badargs
, complain
);
21241 /* As above, but error out if the expression remains overloaded. */
21244 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
21246 exp
= resolve_nondeduced_context (exp
, complain
);
21247 if (type_unknown_p (exp
))
21249 if (complain
& tf_error
)
21250 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
21251 return error_mark_node
;
21256 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21257 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21258 different overloads deduce different arguments for a given parm.
21259 ADDR_P is true if the expression for which deduction is being
21260 performed was of the form "& fn" rather than simply "fn".
21262 Returns 1 on success. */
21265 try_one_overload (tree tparms
,
21270 unification_kind_t strict
,
21279 if (arg
== error_mark_node
)
21282 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21283 to function or pointer to member function argument if the set of
21284 overloaded functions does not contain function templates and at most
21285 one of a set of overloaded functions provides a unique match.
21287 So if this is a template, just return success. */
21289 if (uses_template_parms (arg
))
21292 if (TREE_CODE (arg
) == METHOD_TYPE
)
21293 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21295 arg
= build_pointer_type (arg
);
21297 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21299 /* We don't copy orig_targs for this because if we have already deduced
21300 some template args from previous args, unify would complain when we
21301 try to deduce a template parameter for the same argument, even though
21302 there isn't really a conflict. */
21303 nargs
= TREE_VEC_LENGTH (targs
);
21304 tempargs
= make_tree_vec (nargs
);
21306 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
21309 /* First make sure we didn't deduce anything that conflicts with
21310 explicitly specified args. */
21311 for (i
= nargs
; i
--; )
21313 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21314 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
21318 else if (uses_template_parms (elt
))
21319 /* Since we're unifying against ourselves, we will fill in
21320 template args used in the function parm list with our own
21321 template parms. Discard them. */
21322 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
21323 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
21325 /* Check that the argument at each index of the deduced argument pack
21326 is equivalent to the corresponding explicitly specified argument.
21327 We may have deduced more arguments than were explicitly specified,
21330 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21331 that's wrong if we deduce the same argument pack from multiple
21332 function arguments: it's only incomplete the first time. */
21334 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
21335 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
21337 if (TREE_VEC_LENGTH (deduced_pack
)
21338 < TREE_VEC_LENGTH (explicit_pack
))
21341 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
21342 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
21343 TREE_VEC_ELT (deduced_pack
, j
)))
21346 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
21350 for (i
= nargs
; i
--; )
21352 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21355 TREE_VEC_ELT (targs
, i
) = elt
;
21361 /* PARM is a template class (perhaps with unbound template
21362 parameters). ARG is a fully instantiated type. If ARG can be
21363 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21364 TARGS are as for unify. */
21367 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
21370 tree copy_of_targs
;
21372 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21374 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21375 /* Matches anything. */;
21376 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
21377 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
21380 /* We need to make a new template argument vector for the call to
21381 unify. If we used TARGS, we'd clutter it up with the result of
21382 the attempted unification, even if this class didn't work out.
21383 We also don't want to commit ourselves to all the unifications
21384 we've already done, since unification is supposed to be done on
21385 an argument-by-argument basis. In other words, consider the
21386 following pathological case:
21388 template <int I, int J, int K>
21391 template <int I, int J>
21392 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21394 template <int I, int J, int K>
21395 void f(S<I, J, K>, S<I, I, I>);
21404 Now, by the time we consider the unification involving `s2', we
21405 already know that we must have `f<0, 0, 0>'. But, even though
21406 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21407 because there are two ways to unify base classes of S<0, 1, 2>
21408 with S<I, I, I>. If we kept the already deduced knowledge, we
21409 would reject the possibility I=1. */
21410 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
21412 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21414 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
21419 /* If unification failed, we're done. */
21420 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
21421 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
21427 /* Given a template type PARM and a class type ARG, find the unique
21428 base type in ARG that is an instance of PARM. We do not examine
21429 ARG itself; only its base-classes. If there is not exactly one
21430 appropriate base class, return NULL_TREE. PARM may be the type of
21431 a partial specialization, as well as a plain template type. Used
21434 static enum template_base_result
21435 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
21436 bool explain_p
, tree
*result
)
21438 tree rval
= NULL_TREE
;
21441 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
21443 binfo
= TYPE_BINFO (complete_type (arg
));
21446 /* The type could not be completed. */
21447 *result
= NULL_TREE
;
21448 return tbr_incomplete_type
;
21451 /* Walk in inheritance graph order. The search order is not
21452 important, and this avoids multiple walks of virtual bases. */
21453 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
21455 tree r
= try_class_unification (tparms
, targs
, parm
,
21456 BINFO_TYPE (binfo
), explain_p
);
21460 /* If there is more than one satisfactory baseclass, then:
21464 If they yield more than one possible deduced A, the type
21468 if (rval
&& !same_type_p (r
, rval
))
21470 *result
= NULL_TREE
;
21471 return tbr_ambiguous_baseclass
;
21479 return tbr_success
;
21482 /* Returns the level of DECL, which declares a template parameter. */
21485 template_decl_level (tree decl
)
21487 switch (TREE_CODE (decl
))
21490 case TEMPLATE_DECL
:
21491 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21494 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21497 gcc_unreachable ();
21502 /* Decide whether ARG can be unified with PARM, considering only the
21503 cv-qualifiers of each type, given STRICT as documented for unify.
21504 Returns nonzero iff the unification is OK on that basis. */
21507 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21509 int arg_quals
= cp_type_quals (arg
);
21510 int parm_quals
= cp_type_quals (parm
);
21512 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21513 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21515 /* Although a CVR qualifier is ignored when being applied to a
21516 substituted template parameter ([8.3.2]/1 for example), that
21517 does not allow us to unify "const T" with "int&" because both
21518 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21519 It is ok when we're allowing additional CV qualifiers
21520 at the outer level [14.8.2.1]/3,1st bullet. */
21521 if ((TYPE_REF_P (arg
)
21522 || TREE_CODE (arg
) == FUNCTION_TYPE
21523 || TREE_CODE (arg
) == METHOD_TYPE
)
21524 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21527 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21528 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21532 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21533 && (arg_quals
& parm_quals
) != parm_quals
)
21536 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21537 && (parm_quals
& arg_quals
) != arg_quals
)
21543 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21545 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21547 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21548 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21549 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21551 *index
= TEMPLATE_TYPE_IDX (parm
);
21552 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21556 *index
= TEMPLATE_PARM_IDX (parm
);
21557 *level
= TEMPLATE_PARM_LEVEL (parm
);
21561 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21563 if (unify (TP, TA, P, A, S, EP)) \
21567 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21568 expansion at the end of PACKED_PARMS. Returns 0 if the type
21569 deduction succeeds, 1 otherwise. STRICT is the same as in
21570 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21571 function call argument list. We'll need to adjust the arguments to make them
21572 types. SUBR tells us if this is from a recursive call to
21573 type_unification_real, or for comparing two template argument
21577 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21578 tree packed_args
, unification_kind_t strict
,
21579 bool subr
, bool explain_p
)
21582 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21583 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21584 tree pack
, packs
= NULL_TREE
;
21585 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21587 /* Add in any args remembered from an earlier partial instantiation. */
21588 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21589 int levels
= TMPL_ARGS_DEPTH (targs
);
21591 packed_args
= expand_template_argument_pack (packed_args
);
21593 int len
= TREE_VEC_LENGTH (packed_args
);
21595 /* Determine the parameter packs we will be deducing from the
21596 pattern, and record their current deductions. */
21597 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21598 pack
; pack
= TREE_CHAIN (pack
))
21600 tree parm_pack
= TREE_VALUE (pack
);
21603 /* Only template parameter packs can be deduced, not e.g. function
21604 parameter packs or __bases or __integer_pack. */
21605 if (!TEMPLATE_PARM_P (parm_pack
))
21608 /* Determine the index and level of this parameter pack. */
21609 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21610 if (level
< levels
)
21613 /* Keep track of the parameter packs and their corresponding
21615 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21616 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21619 /* Loop through all of the arguments that have not yet been
21620 unified and unify each with the pattern. */
21621 for (i
= start
; i
< len
; i
++)
21624 bool any_explicit
= false;
21625 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21627 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21628 or the element of its argument pack at the current index if
21629 this argument was explicitly specified. */
21630 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21634 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21637 if (TREE_VALUE (pack
)
21638 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21639 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21641 any_explicit
= true;
21642 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21644 TMPL_ARG (targs
, level
, idx
) = arg
;
21647 /* If we had explicit template arguments, substitute them into the
21648 pattern before deduction. */
21651 /* Some arguments might still be unspecified or dependent. */
21653 ++processing_template_decl
;
21654 dependent
= any_dependent_template_arguments_p (targs
);
21656 --processing_template_decl
;
21657 parm
= tsubst (pattern
, targs
,
21658 explain_p
? tf_warning_or_error
: tf_none
,
21661 --processing_template_decl
;
21662 if (parm
== error_mark_node
)
21668 /* Unify the pattern with the current argument. */
21669 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21673 /* For each parameter pack, collect the deduced value. */
21674 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21677 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21679 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21680 TMPL_ARG (targs
, level
, idx
);
21684 /* Verify that the results of unification with the parameter packs
21685 produce results consistent with what we've seen before, and make
21686 the deduced argument packs available. */
21687 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21689 tree old_pack
= TREE_VALUE (pack
);
21690 tree new_args
= TREE_TYPE (pack
);
21691 int i
, len
= TREE_VEC_LENGTH (new_args
);
21693 bool nondeduced_p
= false;
21695 /* By default keep the original deduced argument pack.
21696 If necessary, more specific code is going to update the
21697 resulting deduced argument later down in this function. */
21698 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21699 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21701 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21702 actually deduce anything. */
21703 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21704 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21705 nondeduced_p
= true;
21709 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21711 /* If we had fewer function args than explicit template args,
21712 just use the explicits. */
21713 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21714 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21715 if (len
< explicit_len
)
21716 new_args
= explicit_args
;
21722 /* Build the deduced *_ARGUMENT_PACK. */
21723 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21725 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21726 TREE_CONSTANT (result
) = 1;
21729 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21731 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21733 /* Note the deduced argument packs for this parameter
21735 TMPL_ARG (targs
, level
, idx
) = result
;
21737 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21738 && (ARGUMENT_PACK_ARGS (old_pack
)
21739 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21741 /* We only had the explicitly-provided arguments before, but
21742 now we have a complete set of arguments. */
21743 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21745 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21746 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21747 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21751 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21752 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21754 if (!comp_template_args (old_args
, new_args
,
21755 &bad_old_arg
, &bad_new_arg
))
21756 /* Inconsistent unification of this parameter pack. */
21757 return unify_parameter_pack_inconsistent (explain_p
,
21763 return unify_success (explain_p
);
21766 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21767 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21768 parameters and return value are as for unify. */
21771 unify_array_domain (tree tparms
, tree targs
,
21772 tree parm_dom
, tree arg_dom
,
21780 /* Our representation of array types uses "N - 1" as the
21781 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21782 not an integer constant. We cannot unify arbitrarily
21783 complex expressions, so we eliminate the MINUS_EXPRs
21785 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21786 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21789 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21790 parm_max
= TREE_OPERAND (parm_max
, 0);
21792 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21793 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21796 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21797 trying to unify the type of a variable with the type
21798 of a template parameter. For example:
21800 template <unsigned int N>
21801 void f (char (&) [N]);
21808 Here, the type of the ARG will be "int [g(i)]", and
21809 may be a SAVE_EXPR, etc. */
21810 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21811 return unify_vla_arg (explain_p
, arg_dom
);
21812 arg_max
= TREE_OPERAND (arg_max
, 0);
21815 /* If only one of the bounds used a MINUS_EXPR, compensate
21816 by adding one to the other bound. */
21817 if (parm_cst
&& !arg_cst
)
21818 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21822 else if (arg_cst
&& !parm_cst
)
21823 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21828 return unify (tparms
, targs
, parm_max
, arg_max
,
21829 UNIFY_ALLOW_INTEGER
, explain_p
);
21832 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21834 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21839 if (PACK_EXPANSION_P (t
))
21840 t
= PACK_EXPANSION_PATTERN (t
);
21841 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
21842 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
21843 || DECL_TYPE_TEMPLATE_P (t
))
21845 else if (TYPE_P (t
))
21851 /* Deduce the value of template parameters. TPARMS is the (innermost)
21852 set of template parameters to a template. TARGS is the bindings
21853 for those template parameters, as determined thus far; TARGS may
21854 include template arguments for outer levels of template parameters
21855 as well. PARM is a parameter to a template function, or a
21856 subcomponent of that parameter; ARG is the corresponding argument.
21857 This function attempts to match PARM with ARG in a manner
21858 consistent with the existing assignments in TARGS. If more values
21859 are deduced, then TARGS is updated.
21861 Returns 0 if the type deduction succeeds, 1 otherwise. The
21862 parameter STRICT is a bitwise or of the following flags:
21865 Require an exact match between PARM and ARG.
21866 UNIFY_ALLOW_MORE_CV_QUAL:
21867 Allow the deduced ARG to be more cv-qualified (by qualification
21868 conversion) than ARG.
21869 UNIFY_ALLOW_LESS_CV_QUAL:
21870 Allow the deduced ARG to be less cv-qualified than ARG.
21871 UNIFY_ALLOW_DERIVED:
21872 Allow the deduced ARG to be a template base class of ARG,
21873 or a pointer to a template base class of the type pointed to by
21875 UNIFY_ALLOW_INTEGER:
21876 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21877 case for more information.
21878 UNIFY_ALLOW_OUTER_LEVEL:
21879 This is the outermost level of a deduction. Used to determine validity
21880 of qualification conversions. A valid qualification conversion must
21881 have const qualified pointers leading up to the inner type which
21882 requires additional CV quals, except at the outer level, where const
21883 is not required [conv.qual]. It would be normal to set this flag in
21884 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21885 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21886 This is the outermost level of a deduction, and PARM can be more CV
21887 qualified at this point.
21888 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21889 This is the outermost level of a deduction, and PARM can be less CV
21890 qualified at this point. */
21893 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
21899 int strict_in
= strict
;
21900 tsubst_flags_t complain
= (explain_p
21901 ? tf_warning_or_error
21904 /* I don't think this will do the right thing with respect to types.
21905 But the only case I've seen it in so far has been array bounds, where
21906 signedness is the only information lost, and I think that will be
21908 while (CONVERT_EXPR_P (parm
))
21909 parm
= TREE_OPERAND (parm
, 0);
21911 if (arg
== error_mark_node
)
21912 return unify_invalid (explain_p
);
21913 if (arg
== unknown_type_node
21914 || arg
== init_list_type_node
)
21915 /* We can't deduce anything from this, but we might get all the
21916 template args from other function args. */
21917 return unify_success (explain_p
);
21919 if (parm
== any_targ_node
|| arg
== any_targ_node
)
21920 return unify_success (explain_p
);
21922 /* If PARM uses template parameters, then we can't bail out here,
21923 even if ARG == PARM, since we won't record unifications for the
21924 template parameters. We might need them if we're trying to
21925 figure out which of two things is more specialized. */
21926 if (arg
== parm
&& !uses_template_parms (parm
))
21927 return unify_success (explain_p
);
21929 /* Handle init lists early, so the rest of the function can assume
21930 we're dealing with a type. */
21931 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
21935 tree orig_parm
= parm
;
21937 /* Replace T with std::initializer_list<T> for deduction. */
21938 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21939 && flag_deduce_init_list
)
21940 parm
= listify (parm
);
21942 if (!is_std_init_list (parm
)
21943 && TREE_CODE (parm
) != ARRAY_TYPE
)
21944 /* We can only deduce from an initializer list argument if the
21945 parameter is std::initializer_list or an array; otherwise this
21946 is a non-deduced context. */
21947 return unify_success (explain_p
);
21949 if (TREE_CODE (parm
) == ARRAY_TYPE
)
21950 elttype
= TREE_TYPE (parm
);
21953 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
21954 /* Deduction is defined in terms of a single type, so just punt
21955 on the (bizarre) std::initializer_list<T...>. */
21956 if (PACK_EXPANSION_P (elttype
))
21957 return unify_success (explain_p
);
21960 if (strict
!= DEDUCE_EXACT
21961 && TYPE_P (elttype
)
21962 && !uses_deducible_template_parms (elttype
))
21963 /* If ELTTYPE has no deducible template parms, skip deduction from
21964 the list elements. */;
21966 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
21968 int elt_strict
= strict
;
21970 if (elt
== error_mark_node
)
21971 return unify_invalid (explain_p
);
21973 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
21975 tree type
= TREE_TYPE (elt
);
21976 if (type
== error_mark_node
)
21977 return unify_invalid (explain_p
);
21978 /* It should only be possible to get here for a call. */
21979 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
21980 elt_strict
|= maybe_adjust_types_for_deduction
21981 (DEDUCE_CALL
, &elttype
, &type
, elt
);
21985 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
21989 if (TREE_CODE (parm
) == ARRAY_TYPE
21990 && deducible_array_bound (TYPE_DOMAIN (parm
)))
21992 /* Also deduce from the length of the initializer list. */
21993 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
21994 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
21995 if (idx
== error_mark_node
)
21996 return unify_invalid (explain_p
);
21997 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22001 /* If the std::initializer_list<T> deduction worked, replace the
22002 deduced A with std::initializer_list<A>. */
22003 if (orig_parm
!= parm
)
22005 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22006 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22007 targ
= listify (targ
);
22008 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22010 return unify_success (explain_p
);
22013 /* If parm and arg aren't the same kind of thing (template, type, or
22014 expression), fail early. */
22015 if (pa_kind (parm
) != pa_kind (arg
))
22016 return unify_invalid (explain_p
);
22018 /* Immediately reject some pairs that won't unify because of
22019 cv-qualification mismatches. */
22020 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22022 /* It is the elements of the array which hold the cv quals of an array
22023 type, and the elements might be template type parms. We'll check
22024 when we recurse. */
22025 && TREE_CODE (arg
) != ARRAY_TYPE
22026 /* We check the cv-qualifiers when unifying with template type
22027 parameters below. We want to allow ARG `const T' to unify with
22028 PARM `T' for example, when computing which of two templates
22029 is more specialized, for example. */
22030 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22031 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22032 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22034 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22035 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22036 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22037 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22038 strict
&= ~UNIFY_ALLOW_DERIVED
;
22039 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22040 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22042 switch (TREE_CODE (parm
))
22044 case TYPENAME_TYPE
:
22046 case UNBOUND_CLASS_TEMPLATE
:
22047 /* In a type which contains a nested-name-specifier, template
22048 argument values cannot be deduced for template parameters used
22049 within the nested-name-specifier. */
22050 return unify_success (explain_p
);
22052 case TEMPLATE_TYPE_PARM
:
22053 case TEMPLATE_TEMPLATE_PARM
:
22054 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22055 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22056 if (error_operand_p (tparm
))
22057 return unify_invalid (explain_p
);
22059 if (TEMPLATE_TYPE_LEVEL (parm
)
22060 != template_decl_level (tparm
))
22061 /* The PARM is not one we're trying to unify. Just check
22062 to see if it matches ARG. */
22064 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22065 && (is_auto (parm
) ? is_auto (arg
)
22066 : same_type_p (parm
, arg
)))
22067 return unify_success (explain_p
);
22069 return unify_type_mismatch (explain_p
, parm
, arg
);
22071 idx
= TEMPLATE_TYPE_IDX (parm
);
22072 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22073 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22074 if (error_operand_p (tparm
))
22075 return unify_invalid (explain_p
);
22077 /* Check for mixed types and values. */
22078 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22079 && TREE_CODE (tparm
) != TYPE_DECL
)
22080 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22081 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22082 gcc_unreachable ();
22084 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22086 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22087 && CLASS_TYPE_P (arg
))
22089 /* First try to match ARG directly. */
22090 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22094 /* Otherwise, look for a suitable base of ARG, as below. */
22095 enum template_base_result r
;
22096 r
= get_template_base (tparms
, targs
, parm
, arg
,
22099 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22103 /* ARG must be constructed from a template class or a template
22104 template parameter. */
22105 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22106 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22107 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22109 /* Deduce arguments T, i from TT<T> or TT<i>. */
22110 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22113 arg
= TYPE_TI_TEMPLATE (arg
);
22115 /* Fall through to deduce template name. */
22118 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22119 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22121 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22123 /* Simple cases: Value already set, does match or doesn't. */
22124 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22125 return unify_success (explain_p
);
22127 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22131 /* If PARM is `const T' and ARG is only `int', we don't have
22132 a match unless we are allowing additional qualification.
22133 If ARG is `const int' and PARM is just `T' that's OK;
22134 that binds `const int' to `T'. */
22135 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22137 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22139 /* Consider the case where ARG is `const volatile int' and
22140 PARM is `const T'. Then, T should be `volatile int'. */
22141 arg
= cp_build_qualified_type_real
22142 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22143 if (arg
== error_mark_node
)
22144 return unify_invalid (explain_p
);
22146 /* Simple cases: Value already set, does match or doesn't. */
22147 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22148 return unify_success (explain_p
);
22150 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22152 /* Make sure that ARG is not a variable-sized array. (Note
22153 that were talking about variable-sized arrays (like
22154 `int[n]'), rather than arrays of unknown size (like
22155 `int[]').) We'll get very confused by such a type since
22156 the bound of the array is not constant, and therefore
22157 not mangleable. Besides, such types are not allowed in
22158 ISO C++, so we can do as we please here. We do allow
22159 them for 'auto' deduction, since that isn't ABI-exposed. */
22160 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22161 return unify_vla_arg (explain_p
, arg
);
22163 /* Strip typedefs as in convert_template_argument. */
22164 arg
= canonicalize_type_argument (arg
, tf_none
);
22167 /* If ARG is a parameter pack or an expansion, we cannot unify
22168 against it unless PARM is also a parameter pack. */
22169 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22170 && !template_parameter_pack_p (parm
))
22171 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22173 /* If the argument deduction results is a METHOD_TYPE,
22174 then there is a problem.
22175 METHOD_TYPE doesn't map to any real C++ type the result of
22176 the deduction cannot be of that type. */
22177 if (TREE_CODE (arg
) == METHOD_TYPE
)
22178 return unify_method_type_error (explain_p
, arg
);
22180 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22181 return unify_success (explain_p
);
22183 case TEMPLATE_PARM_INDEX
:
22184 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22185 if (error_operand_p (tparm
))
22186 return unify_invalid (explain_p
);
22188 if (TEMPLATE_PARM_LEVEL (parm
)
22189 != template_decl_level (tparm
))
22191 /* The PARM is not one we're trying to unify. Just check
22192 to see if it matches ARG. */
22193 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22194 && cp_tree_equal (parm
, arg
));
22196 unify_expression_unequal (explain_p
, parm
, arg
);
22200 idx
= TEMPLATE_PARM_IDX (parm
);
22201 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22205 if ((strict
& UNIFY_ALLOW_INTEGER
)
22206 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22207 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22208 /* We're deducing from an array bound, the type doesn't matter. */
22209 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22210 int x
= !cp_tree_equal (targ
, arg
);
22212 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22216 /* [temp.deduct.type] If, in the declaration of a function template
22217 with a non-type template-parameter, the non-type
22218 template-parameter is used in an expression in the function
22219 parameter-list and, if the corresponding template-argument is
22220 deduced, the template-argument type shall match the type of the
22221 template-parameter exactly, except that a template-argument
22222 deduced from an array bound may be of any integral type.
22223 The non-type parameter might use already deduced type parameters. */
22224 tparm
= TREE_TYPE (parm
);
22225 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22226 /* We don't have enough levels of args to do any substitution. This
22227 can happen in the context of -fnew-ttp-matching. */;
22230 ++processing_template_decl
;
22231 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22232 --processing_template_decl
;
22234 if (tree a
= type_uses_auto (tparm
))
22236 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22237 if (tparm
== error_mark_node
)
22242 if (!TREE_TYPE (arg
))
22243 /* Template-parameter dependent expression. Just accept it for now.
22244 It will later be processed in convert_template_argument. */
22246 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
22247 non_reference (tparm
)))
22249 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22250 && CP_INTEGRAL_TYPE_P (tparm
))
22251 /* Convert the ARG to the type of PARM; the deduced non-type
22252 template argument must exactly match the types of the
22253 corresponding parameter. */
22254 arg
= fold (build_nop (tparm
, arg
));
22255 else if (uses_template_parms (tparm
))
22257 /* We haven't deduced the type of this parameter yet. */
22258 if (cxx_dialect
>= cxx17
22259 /* We deduce from array bounds in try_array_deduction. */
22260 && !(strict
& UNIFY_ALLOW_INTEGER
))
22262 /* Deduce it from the non-type argument. */
22263 tree atype
= TREE_TYPE (arg
);
22264 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22266 UNIFY_ALLOW_NONE
, explain_p
);
22269 /* Try again later. */
22270 return unify_success (explain_p
);
22273 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22275 /* If ARG is a parameter pack or an expansion, we cannot unify
22276 against it unless PARM is also a parameter pack. */
22277 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22278 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22279 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22282 bool removed_attr
= false;
22283 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22285 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22286 return unify_success (explain_p
);
22290 /* A pointer-to-member constant can be unified only with
22291 another constant. */
22292 if (TREE_CODE (arg
) != PTRMEM_CST
)
22293 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22295 /* Just unify the class member. It would be useless (and possibly
22296 wrong, depending on the strict flags) to unify also
22297 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22298 arg refer to the same variable, even if through different
22299 classes. For instance:
22301 struct A { int x; };
22304 Unification of &A::x and &B::x must succeed. */
22305 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22306 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
22311 if (!TYPE_PTR_P (arg
))
22312 return unify_type_mismatch (explain_p
, parm
, arg
);
22314 /* [temp.deduct.call]
22316 A can be another pointer or pointer to member type that can
22317 be converted to the deduced A via a qualification
22318 conversion (_conv.qual_).
22320 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22321 This will allow for additional cv-qualification of the
22322 pointed-to types if appropriate. */
22324 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
22325 /* The derived-to-base conversion only persists through one
22326 level of pointers. */
22327 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
22329 return unify (tparms
, targs
, TREE_TYPE (parm
),
22330 TREE_TYPE (arg
), strict
, explain_p
);
22333 case REFERENCE_TYPE
:
22334 if (!TYPE_REF_P (arg
))
22335 return unify_type_mismatch (explain_p
, parm
, arg
);
22336 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22337 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22340 if (TREE_CODE (arg
) != ARRAY_TYPE
)
22341 return unify_type_mismatch (explain_p
, parm
, arg
);
22342 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
22343 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
22344 return unify_type_mismatch (explain_p
, parm
, arg
);
22345 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22346 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22347 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
22348 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22349 TYPE_DOMAIN (arg
), explain_p
);
22350 return unify_success (explain_p
);
22357 case ENUMERAL_TYPE
:
22360 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22361 return unify_type_mismatch (explain_p
, parm
, arg
);
22363 /* We have already checked cv-qualification at the top of the
22365 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
22366 return unify_type_mismatch (explain_p
, parm
, arg
);
22368 /* As far as unification is concerned, this wins. Later checks
22369 will invalidate it if necessary. */
22370 return unify_success (explain_p
);
22372 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22373 /* Type INTEGER_CST can come from ordinary constant template args. */
22375 while (CONVERT_EXPR_P (arg
))
22376 arg
= TREE_OPERAND (arg
, 0);
22378 if (TREE_CODE (arg
) != INTEGER_CST
)
22379 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22380 return (tree_int_cst_equal (parm
, arg
)
22381 ? unify_success (explain_p
)
22382 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
22386 int i
, len
, argslen
;
22387 int parm_variadic_p
= 0;
22389 if (TREE_CODE (arg
) != TREE_VEC
)
22390 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22392 len
= TREE_VEC_LENGTH (parm
);
22393 argslen
= TREE_VEC_LENGTH (arg
);
22395 /* Check for pack expansions in the parameters. */
22396 for (i
= 0; i
< len
; ++i
)
22398 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
22401 /* We can unify against something with a trailing
22403 parm_variadic_p
= 1;
22405 /* [temp.deduct.type]/9: If the template argument list of
22406 P contains a pack expansion that is not the last
22407 template argument, the entire template argument list
22408 is a non-deduced context. */
22409 return unify_success (explain_p
);
22413 /* If we don't have enough arguments to satisfy the parameters
22414 (not counting the pack expression at the end), or we have
22415 too many arguments for a parameter list that doesn't end in
22416 a pack expression, we can't unify. */
22417 if (parm_variadic_p
22418 ? argslen
< len
- parm_variadic_p
22420 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
22422 /* Unify all of the parameters that precede the (optional)
22423 pack expression. */
22424 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
22426 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22427 TREE_VEC_ELT (parm
, i
),
22428 TREE_VEC_ELT (arg
, i
),
22429 UNIFY_ALLOW_NONE
, explain_p
);
22431 if (parm_variadic_p
)
22432 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
22434 /*subr=*/true, explain_p
);
22435 return unify_success (explain_p
);
22440 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22441 return unify_type_mismatch (explain_p
, parm
, arg
);
22443 if (TYPE_PTRMEMFUNC_P (parm
))
22445 if (!TYPE_PTRMEMFUNC_P (arg
))
22446 return unify_type_mismatch (explain_p
, parm
, arg
);
22448 return unify (tparms
, targs
,
22449 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
22450 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
22451 strict
, explain_p
);
22453 else if (TYPE_PTRMEMFUNC_P (arg
))
22454 return unify_type_mismatch (explain_p
, parm
, arg
);
22456 if (CLASSTYPE_TEMPLATE_INFO (parm
))
22458 tree t
= NULL_TREE
;
22460 if (strict_in
& UNIFY_ALLOW_DERIVED
)
22462 /* First, we try to unify the PARM and ARG directly. */
22463 t
= try_class_unification (tparms
, targs
,
22464 parm
, arg
, explain_p
);
22468 /* Fallback to the special case allowed in
22469 [temp.deduct.call]:
22471 If P is a class, and P has the form
22472 template-id, then A can be a derived class of
22473 the deduced A. Likewise, if P is a pointer to
22474 a class of the form template-id, A can be a
22475 pointer to a derived class pointed to by the
22477 enum template_base_result r
;
22478 r
= get_template_base (tparms
, targs
, parm
, arg
,
22483 /* Don't give the derived diagnostic if we're
22484 already dealing with the same template. */
22486 = (CLASSTYPE_TEMPLATE_INFO (arg
)
22487 && (CLASSTYPE_TI_TEMPLATE (parm
)
22488 == CLASSTYPE_TI_TEMPLATE (arg
)));
22489 return unify_no_common_base (explain_p
&& !same_template
,
22494 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22495 && (CLASSTYPE_TI_TEMPLATE (parm
)
22496 == CLASSTYPE_TI_TEMPLATE (arg
)))
22497 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22498 Then, we should unify `int' and `U'. */
22501 /* There's no chance of unification succeeding. */
22502 return unify_type_mismatch (explain_p
, parm
, arg
);
22504 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22505 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22507 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22508 return unify_type_mismatch (explain_p
, parm
, arg
);
22509 return unify_success (explain_p
);
22512 case FUNCTION_TYPE
:
22514 unsigned int nargs
;
22519 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22520 return unify_type_mismatch (explain_p
, parm
, arg
);
22522 /* CV qualifications for methods can never be deduced, they must
22523 match exactly. We need to check them explicitly here,
22524 because type_unification_real treats them as any other
22525 cv-qualified parameter. */
22526 if (TREE_CODE (parm
) == METHOD_TYPE
22527 && (!check_cv_quals_for_unify
22529 class_of_this_parm (arg
),
22530 class_of_this_parm (parm
))))
22531 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22532 if (TREE_CODE (arg
) == FUNCTION_TYPE
22533 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22534 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22535 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22536 return unify_type_mismatch (explain_p
, parm
, arg
);
22538 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22539 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22541 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22542 args
= XALLOCAVEC (tree
, nargs
);
22543 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22544 a
!= NULL_TREE
&& a
!= void_list_node
;
22545 a
= TREE_CHAIN (a
), ++i
)
22546 args
[i
] = TREE_VALUE (a
);
22549 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22550 args
, nargs
, 1, DEDUCE_EXACT
,
22554 if (flag_noexcept_type
)
22556 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22557 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22558 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22559 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22560 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22561 && uses_template_parms (TREE_PURPOSE (pspec
)))
22562 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22563 TREE_PURPOSE (aspec
),
22564 UNIFY_ALLOW_NONE
, explain_p
);
22565 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22566 return unify_type_mismatch (explain_p
, parm
, arg
);
22573 /* Unify a pointer to member with a pointer to member function, which
22574 deduces the type of the member as a function type. */
22575 if (TYPE_PTRMEMFUNC_P (arg
))
22577 /* Check top-level cv qualifiers */
22578 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22579 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22581 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22582 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22583 UNIFY_ALLOW_NONE
, explain_p
);
22585 /* Determine the type of the function we are unifying against. */
22586 tree fntype
= static_fn_type (arg
);
22588 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22591 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22592 return unify_type_mismatch (explain_p
, parm
, arg
);
22593 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22594 TYPE_OFFSET_BASETYPE (arg
),
22595 UNIFY_ALLOW_NONE
, explain_p
);
22596 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22597 strict
, explain_p
);
22600 if (DECL_TEMPLATE_PARM_P (parm
))
22601 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22602 if (arg
!= scalar_constant_value (parm
))
22603 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22604 return unify_success (explain_p
);
22607 case TEMPLATE_DECL
:
22608 /* Matched cases are handled by the ARG == PARM test above. */
22609 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22612 /* We might get a variable as a non-type template argument in parm if the
22613 corresponding parameter is type-dependent. Make any necessary
22614 adjustments based on whether arg is a reference. */
22615 if (CONSTANT_CLASS_P (arg
))
22616 parm
= fold_non_dependent_expr (parm
, complain
);
22617 else if (REFERENCE_REF_P (arg
))
22619 tree sub
= TREE_OPERAND (arg
, 0);
22621 if (TREE_CODE (sub
) == ADDR_EXPR
)
22622 arg
= TREE_OPERAND (sub
, 0);
22624 /* Now use the normal expression code to check whether they match. */
22627 case TYPE_ARGUMENT_PACK
:
22628 case NONTYPE_ARGUMENT_PACK
:
22629 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22630 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22633 case DECLTYPE_TYPE
:
22634 case UNDERLYING_TYPE
:
22635 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22636 or UNDERLYING_TYPE nodes. */
22637 return unify_success (explain_p
);
22640 /* Unification fails if we hit an error node. */
22641 return unify_invalid (explain_p
);
22644 if (REFERENCE_REF_P (parm
))
22646 bool pexp
= PACK_EXPANSION_P (arg
);
22648 arg
= PACK_EXPANSION_PATTERN (arg
);
22649 if (REFERENCE_REF_P (arg
))
22650 arg
= TREE_OPERAND (arg
, 0);
22652 arg
= make_pack_expansion (arg
, complain
);
22653 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22654 strict
, explain_p
);
22659 /* An unresolved overload is a nondeduced context. */
22660 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22661 return unify_success (explain_p
);
22662 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
22664 /* We must be looking at an expression. This can happen with
22668 void foo(S<I>, S<I + 2>);
22670 This is a "nondeduced context":
22674 The nondeduced contexts are:
22676 --A type that is a template-id in which one or more of
22677 the template-arguments is an expression that references
22678 a template-parameter.
22680 In these cases, we assume deduction succeeded, but don't
22681 actually infer any unifications. */
22683 if (!uses_template_parms (parm
)
22684 && !template_args_equal (parm
, arg
))
22685 return unify_expression_unequal (explain_p
, parm
, arg
);
22687 return unify_success (explain_p
);
22690 #undef RECUR_AND_CHECK_FAILURE
22692 /* Note that DECL can be defined in this translation unit, if
22696 mark_definable (tree decl
)
22699 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22700 FOR_EACH_CLONE (clone
, decl
)
22701 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22704 /* Called if RESULT is explicitly instantiated, or is a member of an
22705 explicitly instantiated class. */
22708 mark_decl_instantiated (tree result
, int extern_p
)
22710 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22712 /* If this entity has already been written out, it's too late to
22713 make any modifications. */
22714 if (TREE_ASM_WRITTEN (result
))
22717 /* For anonymous namespace we don't need to do anything. */
22718 if (decl_anon_ns_mem_p (result
))
22720 gcc_assert (!TREE_PUBLIC (result
));
22724 if (TREE_CODE (result
) != FUNCTION_DECL
)
22725 /* The TREE_PUBLIC flag for function declarations will have been
22726 set correctly by tsubst. */
22727 TREE_PUBLIC (result
) = 1;
22729 /* This might have been set by an earlier implicit instantiation. */
22730 DECL_COMDAT (result
) = 0;
22733 DECL_NOT_REALLY_EXTERN (result
) = 0;
22736 mark_definable (result
);
22737 mark_needed (result
);
22738 /* Always make artificials weak. */
22739 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22740 comdat_linkage (result
);
22741 /* For WIN32 we also want to put explicit instantiations in
22742 linkonce sections. */
22743 else if (TREE_PUBLIC (result
))
22744 maybe_make_one_only (result
);
22745 if (TREE_CODE (result
) == FUNCTION_DECL
22746 && DECL_TEMPLATE_INSTANTIATED (result
))
22747 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22748 since start_preparsed_function wouldn't have if we had an earlier
22749 extern explicit instantiation. */
22750 DECL_EXTERNAL (result
) = 0;
22753 /* If EXTERN_P, then this function will not be emitted -- unless
22754 followed by an explicit instantiation, at which point its linkage
22755 will be adjusted. If !EXTERN_P, then this function will be
22756 emitted here. In neither circumstance do we want
22757 import_export_decl to adjust the linkage. */
22758 DECL_INTERFACE_KNOWN (result
) = 1;
22761 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22762 important template arguments. If any are missing, we check whether
22763 they're important by using error_mark_node for substituting into any
22764 args that were used for partial ordering (the ones between ARGS and END)
22765 and seeing if it bubbles up. */
22768 check_undeduced_parms (tree targs
, tree args
, tree end
)
22770 bool found
= false;
22772 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22773 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22776 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22780 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22781 if (substed
== error_mark_node
)
22787 /* Given two function templates PAT1 and PAT2, return:
22789 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22790 -1 if PAT2 is more specialized than PAT1.
22791 0 if neither is more specialized.
22793 LEN indicates the number of parameters we should consider
22794 (defaulted parameters should not be considered).
22796 The 1998 std underspecified function template partial ordering, and
22797 DR214 addresses the issue. We take pairs of arguments, one from
22798 each of the templates, and deduce them against each other. One of
22799 the templates will be more specialized if all the *other*
22800 template's arguments deduce against its arguments and at least one
22801 of its arguments *does* *not* deduce against the other template's
22802 corresponding argument. Deduction is done as for class templates.
22803 The arguments used in deduction have reference and top level cv
22804 qualifiers removed. Iff both arguments were originally reference
22805 types *and* deduction succeeds in both directions, an lvalue reference
22806 wins against an rvalue reference and otherwise the template
22807 with the more cv-qualified argument wins for that pairing (if
22808 neither is more cv-qualified, they both are equal). Unlike regular
22809 deduction, after all the arguments have been deduced in this way,
22810 we do *not* verify the deduced template argument values can be
22811 substituted into non-deduced contexts.
22813 The logic can be a bit confusing here, because we look at deduce1 and
22814 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22815 can find template arguments for pat1 to make arg1 look like arg2, that
22816 means that arg2 is at least as specialized as arg1. */
22819 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22821 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22822 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22823 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22824 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22825 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22826 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22827 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22828 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22829 tree origs1
, origs2
;
22830 bool lose1
= false;
22831 bool lose2
= false;
22833 /* Remove the this parameter from non-static member functions. If
22834 one is a non-static member function and the other is not a static
22835 member function, remove the first parameter from that function
22836 also. This situation occurs for operator functions where we
22837 locate both a member function (with this pointer) and non-member
22838 operator (with explicit first operand). */
22839 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
22841 len
--; /* LEN is the number of significant arguments for DECL1 */
22842 args1
= TREE_CHAIN (args1
);
22843 if (!DECL_STATIC_FUNCTION_P (decl2
))
22844 args2
= TREE_CHAIN (args2
);
22846 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
22848 args2
= TREE_CHAIN (args2
);
22849 if (!DECL_STATIC_FUNCTION_P (decl1
))
22852 args1
= TREE_CHAIN (args1
);
22856 /* If only one is a conversion operator, they are unordered. */
22857 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
22860 /* Consider the return type for a conversion function */
22861 if (DECL_CONV_FN_P (decl1
))
22863 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
22864 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
22868 processing_template_decl
++;
22874 /* Stop when an ellipsis is seen. */
22875 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
22877 tree arg1
= TREE_VALUE (args1
);
22878 tree arg2
= TREE_VALUE (args2
);
22879 int deduce1
, deduce2
;
22885 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22886 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22888 /* When both arguments are pack expansions, we need only
22889 unify the patterns themselves. */
22890 arg1
= PACK_EXPANSION_PATTERN (arg1
);
22891 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22893 /* This is the last comparison we need to do. */
22897 /* DR 1847: If a particular P contains no template-parameters that
22898 participate in template argument deduction, that P is not used to
22899 determine the ordering. */
22900 if (!uses_deducible_template_parms (arg1
)
22901 && !uses_deducible_template_parms (arg2
))
22904 if (TYPE_REF_P (arg1
))
22906 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
22907 arg1
= TREE_TYPE (arg1
);
22908 quals1
= cp_type_quals (arg1
);
22911 if (TYPE_REF_P (arg2
))
22913 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
22914 arg2
= TREE_TYPE (arg2
);
22915 quals2
= cp_type_quals (arg2
);
22918 arg1
= TYPE_MAIN_VARIANT (arg1
);
22919 arg2
= TYPE_MAIN_VARIANT (arg2
);
22921 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
22923 int i
, len2
= remaining_arguments (args2
);
22924 tree parmvec
= make_tree_vec (1);
22925 tree argvec
= make_tree_vec (len2
);
22928 /* Setup the parameter vector, which contains only ARG1. */
22929 TREE_VEC_ELT (parmvec
, 0) = arg1
;
22931 /* Setup the argument vector, which contains the remaining
22933 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
22934 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22936 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
22937 argvec
, DEDUCE_EXACT
,
22938 /*subr=*/true, /*explain_p=*/false)
22941 /* We cannot deduce in the other direction, because ARG1 is
22942 a pack expansion but ARG2 is not. */
22945 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22947 int i
, len1
= remaining_arguments (args1
);
22948 tree parmvec
= make_tree_vec (1);
22949 tree argvec
= make_tree_vec (len1
);
22952 /* Setup the parameter vector, which contains only ARG1. */
22953 TREE_VEC_ELT (parmvec
, 0) = arg2
;
22955 /* Setup the argument vector, which contains the remaining
22957 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
22958 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
22960 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
22961 argvec
, DEDUCE_EXACT
,
22962 /*subr=*/true, /*explain_p=*/false)
22965 /* We cannot deduce in the other direction, because ARG2 is
22966 a pack expansion but ARG1 is not.*/
22972 /* The normal case, where neither argument is a pack
22974 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
22975 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22977 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
22978 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
22982 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22983 arg2, then arg2 is not as specialized as arg1. */
22989 /* "If, for a given type, deduction succeeds in both directions
22990 (i.e., the types are identical after the transformations above)
22991 and both P and A were reference types (before being replaced with
22992 the type referred to above):
22993 - if the type from the argument template was an lvalue reference and
22994 the type from the parameter template was not, the argument type is
22995 considered to be more specialized than the other; otherwise,
22996 - if the type from the argument template is more cv-qualified
22997 than the type from the parameter template (as described above),
22998 the argument type is considered to be more specialized than the other;
23000 - neither type is more specialized than the other." */
23002 if (deduce1
&& deduce2
)
23004 if (ref1
&& ref2
&& ref1
!= ref2
)
23011 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23013 if ((quals1
& quals2
) == quals2
)
23015 if ((quals1
& quals2
) == quals1
)
23020 if (lose1
&& lose2
)
23021 /* We've failed to deduce something in either direction.
23022 These must be unordered. */
23027 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23028 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23029 /* We have already processed all of the arguments in our
23030 handing of the pack expansion type. */
23033 args1
= TREE_CHAIN (args1
);
23034 args2
= TREE_CHAIN (args2
);
23037 /* "In most cases, all template parameters must have values in order for
23038 deduction to succeed, but for partial ordering purposes a template
23039 parameter may remain without a value provided it is not used in the
23040 types being used for partial ordering."
23042 Thus, if we are missing any of the targs1 we need to substitute into
23043 origs1, then pat2 is not as specialized as pat1. This can happen when
23044 there is a nondeduced context. */
23045 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23047 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23050 processing_template_decl
--;
23052 /* If both deductions succeed, the partial ordering selects the more
23053 constrained template. */
23054 if (!lose1
&& !lose2
)
23056 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
23057 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
23058 lose1
= !subsumes_constraints (c1
, c2
);
23059 lose2
= !subsumes_constraints (c2
, c1
);
23062 /* All things being equal, if the next argument is a pack expansion
23063 for one function but not for the other, prefer the
23064 non-variadic function. FIXME this is bogus; see c++/41958. */
23066 && args1
&& TREE_VALUE (args1
)
23067 && args2
&& TREE_VALUE (args2
))
23069 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23070 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23073 if (lose1
== lose2
)
23081 /* Determine which of two partial specializations of TMPL is more
23084 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23085 to the first partial specialization. The TREE_PURPOSE is the
23086 innermost set of template parameters for the partial
23087 specialization. PAT2 is similar, but for the second template.
23089 Return 1 if the first partial specialization is more specialized;
23090 -1 if the second is more specialized; 0 if neither is more
23093 See [temp.class.order] for information about determining which of
23094 two templates is more specialized. */
23097 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23101 bool any_deductions
= false;
23103 tree tmpl1
= TREE_VALUE (pat1
);
23104 tree tmpl2
= TREE_VALUE (pat2
);
23105 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23106 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23108 /* Just like what happens for functions, if we are ordering between
23109 different template specializations, we may encounter dependent
23110 types in the arguments, and we need our dependency check functions
23111 to behave correctly. */
23112 ++processing_template_decl
;
23113 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23117 any_deductions
= true;
23120 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23124 any_deductions
= true;
23126 --processing_template_decl
;
23128 /* If both deductions succeed, the partial ordering selects the more
23129 constrained template. */
23130 if (!winner
&& any_deductions
)
23131 return more_constrained (tmpl1
, tmpl2
);
23133 /* In the case of a tie where at least one of the templates
23134 has a parameter pack at the end, the template with the most
23135 non-packed parameters wins. */
23138 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23139 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23141 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23142 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23143 int len1
= TREE_VEC_LENGTH (args1
);
23144 int len2
= TREE_VEC_LENGTH (args2
);
23146 /* We don't count the pack expansion at the end. */
23147 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23149 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23154 else if (len1
< len2
)
23161 /* Return the template arguments that will produce the function signature
23162 DECL from the function template FN, with the explicit template
23163 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23164 also match. Return NULL_TREE if no satisfactory arguments could be
23168 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23170 int ntparms
= DECL_NTPARMS (fn
);
23171 tree targs
= make_tree_vec (ntparms
);
23172 tree decl_type
= TREE_TYPE (decl
);
23173 tree decl_arg_types
;
23175 unsigned int nargs
, ix
;
23178 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23180 /* Never do unification on the 'this' parameter. */
23181 decl_arg_types
= skip_artificial_parms_for (decl
,
23182 TYPE_ARG_TYPES (decl_type
));
23184 nargs
= list_length (decl_arg_types
);
23185 args
= XALLOCAVEC (tree
, nargs
);
23186 for (arg
= decl_arg_types
, ix
= 0;
23187 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23188 arg
= TREE_CHAIN (arg
), ++ix
)
23189 args
[ix
] = TREE_VALUE (arg
);
23191 if (fn_type_unification (fn
, explicit_args
, targs
,
23193 (check_rettype
|| DECL_CONV_FN_P (fn
)
23194 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23195 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23196 /*explain_p=*/false,
23198 == error_mark_node
)
23204 /* Return the innermost template arguments that, when applied to a partial
23205 specialization SPEC_TMPL of TMPL, yield the ARGS.
23207 For example, suppose we have:
23209 template <class T, class U> struct S {};
23210 template <class T> struct S<T*, int> {};
23212 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23213 partial specialization and the ARGS will be {double*, int}. The resulting
23214 vector will be {double}, indicating that `T' is bound to `double'. */
23217 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23219 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23221 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23222 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23224 tree innermost_deduced_args
;
23226 innermost_deduced_args
= make_tree_vec (ntparms
);
23227 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23229 deduced_args
= copy_node (args
);
23230 SET_TMPL_ARGS_LEVEL (deduced_args
,
23231 TMPL_ARGS_DEPTH (deduced_args
),
23232 innermost_deduced_args
);
23235 deduced_args
= innermost_deduced_args
;
23237 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23239 if (unify (tparms
, deduced_args
,
23240 INNERMOST_TEMPLATE_ARGS (spec_args
),
23241 INNERMOST_TEMPLATE_ARGS (args
),
23242 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23245 for (i
= 0; i
< ntparms
; ++i
)
23246 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23248 if (!tried_array_deduction
)
23250 try_array_deduction (tparms
, innermost_deduced_args
,
23251 INNERMOST_TEMPLATE_ARGS (spec_args
));
23252 tried_array_deduction
= true;
23253 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23259 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23261 excessive_deduction_depth
= true;
23265 /* Verify that nondeduced template arguments agree with the type
23266 obtained from argument deduction.
23270 struct A { typedef int X; };
23271 template <class T, class U> struct C {};
23272 template <class T> struct C<T, typename T::X> {};
23274 Then with the instantiation `C<A, int>', we can deduce that
23275 `T' is `A' but unify () does not check whether `typename T::X'
23277 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23279 if (spec_args
!= error_mark_node
)
23280 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23281 INNERMOST_TEMPLATE_ARGS (spec_args
),
23282 tmpl
, tf_none
, false, false);
23284 pop_tinst_level ();
23286 if (spec_args
== error_mark_node
23287 /* We only need to check the innermost arguments; the other
23288 arguments will always agree. */
23289 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23290 INNERMOST_TEMPLATE_ARGS (args
)))
23293 /* Now that we have bindings for all of the template arguments,
23294 ensure that the arguments deduced for the template template
23295 parameters have compatible template parameter lists. See the use
23296 of template_template_parm_bindings_ok_p in fn_type_unification
23297 for more information. */
23298 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23301 return deduced_args
;
23304 // Compare two function templates T1 and T2 by deducing bindings
23305 // from one against the other. If both deductions succeed, compare
23306 // constraints to see which is more constrained.
23308 more_specialized_inst (tree t1
, tree t2
)
23313 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
23319 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
23325 // If both deductions succeed, then one may be more constrained.
23326 if (count
== 2 && fate
== 0)
23327 fate
= more_constrained (t1
, t2
);
23332 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23333 Return the TREE_LIST node with the most specialized template, if
23334 any. If there is no most specialized template, the error_mark_node
23337 Note that this function does not look at, or modify, the
23338 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23339 returned is one of the elements of INSTANTIATIONS, callers may
23340 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23341 and retrieve it from the value returned. */
23344 most_specialized_instantiation (tree templates
)
23348 ++processing_template_decl
;
23351 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
23353 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
23354 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
23359 /* Equally specialized, move to next function. If there
23360 is no next function, nothing's most specialized. */
23361 fn
= TREE_CHAIN (fn
);
23369 /* Now verify that champ is better than everything earlier in the
23370 instantiation list. */
23371 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
23372 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
23379 processing_template_decl
--;
23382 return error_mark_node
;
23387 /* If DECL is a specialization of some template, return the most
23388 general such template. Otherwise, returns NULL_TREE.
23390 For example, given:
23392 template <class T> struct S { template <class U> void f(U); };
23394 if TMPL is `template <class U> void S<int>::f(U)' this will return
23395 the full template. This function will not trace past partial
23396 specializations, however. For example, given in addition:
23398 template <class T> struct S<T*> { template <class U> void f(U); };
23400 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23401 `template <class T> template <class U> S<T*>::f(U)'. */
23404 most_general_template (tree decl
)
23406 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23408 if (tree tinfo
= get_template_info (decl
))
23409 decl
= TI_TEMPLATE (tinfo
);
23410 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23411 template friend, or a FIELD_DECL for a capture pack. */
23412 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23416 /* Look for more and more general templates. */
23417 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
23419 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23420 (See cp-tree.h for details.) */
23421 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
23424 if (CLASS_TYPE_P (TREE_TYPE (decl
))
23425 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
23426 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
23429 /* Stop if we run into an explicitly specialized class template. */
23430 if (!DECL_NAMESPACE_SCOPE_P (decl
)
23431 && DECL_CONTEXT (decl
)
23432 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
23435 decl
= DECL_TI_TEMPLATE (decl
);
23441 /* Return the most specialized of the template partial specializations
23442 which can produce TARGET, a specialization of some class or variable
23443 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23444 a TEMPLATE_DECL node corresponding to the partial specialization, while
23445 the TREE_PURPOSE is the set of template arguments that must be
23446 substituted into the template pattern in order to generate TARGET.
23448 If the choice of partial specialization is ambiguous, a diagnostic
23449 is issued, and the error_mark_node is returned. If there are no
23450 partial specializations matching TARGET, then NULL_TREE is
23451 returned, indicating that the primary template should be used. */
23454 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
23456 tree list
= NULL_TREE
;
23461 tree outer_args
= NULL_TREE
;
23464 if (TYPE_P (target
))
23466 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
23467 tmpl
= TI_TEMPLATE (tinfo
);
23468 args
= TI_ARGS (tinfo
);
23470 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
23472 tmpl
= TREE_OPERAND (target
, 0);
23473 args
= TREE_OPERAND (target
, 1);
23475 else if (VAR_P (target
))
23477 tree tinfo
= DECL_TEMPLATE_INFO (target
);
23478 tmpl
= TI_TEMPLATE (tinfo
);
23479 args
= TI_ARGS (tinfo
);
23482 gcc_unreachable ();
23484 tree main_tmpl
= most_general_template (tmpl
);
23486 /* For determining which partial specialization to use, only the
23487 innermost args are interesting. */
23488 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23490 outer_args
= strip_innermost_template_args (args
, 1);
23491 args
= INNERMOST_TEMPLATE_ARGS (args
);
23494 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23497 tree spec_tmpl
= TREE_VALUE (t
);
23501 /* Substitute in the template args from the enclosing class. */
23502 ++processing_template_decl
;
23503 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23504 --processing_template_decl
;
23507 if (spec_tmpl
== error_mark_node
)
23508 return error_mark_node
;
23510 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23514 spec_args
= add_to_template_args (outer_args
, spec_args
);
23516 /* Keep the candidate only if the constraints are satisfied,
23517 or if we're not compiling with concepts. */
23519 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23521 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23522 TREE_TYPE (list
) = TREE_TYPE (t
);
23530 ambiguous_p
= false;
23533 t
= TREE_CHAIN (t
);
23534 for (; t
; t
= TREE_CHAIN (t
))
23536 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23543 t
= TREE_CHAIN (t
);
23546 ambiguous_p
= true;
23555 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23557 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23560 ambiguous_p
= true;
23568 char *spaces
= NULL
;
23569 if (!(complain
& tf_error
))
23570 return error_mark_node
;
23571 if (TYPE_P (target
))
23572 error ("ambiguous template instantiation for %q#T", target
);
23574 error ("ambiguous template instantiation for %q#D", target
);
23575 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23576 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23578 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23579 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23580 "%s %#qS", spaces
? spaces
: str
, subst
);
23581 spaces
= spaces
? spaces
: get_spaces (str
);
23584 return error_mark_node
;
23590 /* Explicitly instantiate DECL. */
23593 do_decl_instantiation (tree decl
, tree storage
)
23595 tree result
= NULL_TREE
;
23598 if (!decl
|| decl
== error_mark_node
)
23599 /* An error occurred, for which grokdeclarator has already issued
23600 an appropriate message. */
23602 else if (! DECL_LANG_SPECIFIC (decl
))
23604 error ("explicit instantiation of non-template %q#D", decl
);
23607 else if (DECL_DECLARED_CONCEPT_P (decl
))
23610 error ("explicit instantiation of variable concept %q#D", decl
);
23612 error ("explicit instantiation of function concept %q#D", decl
);
23616 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23617 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23619 if (VAR_P (decl
) && !var_templ
)
23621 /* There is an asymmetry here in the way VAR_DECLs and
23622 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23623 the latter, the DECL we get back will be marked as a
23624 template instantiation, and the appropriate
23625 DECL_TEMPLATE_INFO will be set up. This does not happen for
23626 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23627 should handle VAR_DECLs as it currently handles
23629 if (!DECL_CLASS_SCOPE_P (decl
))
23631 error ("%qD is not a static data member of a class template", decl
);
23634 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23635 if (!result
|| !VAR_P (result
))
23637 error ("no matching template for %qD found", decl
);
23640 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23642 error ("type %qT for explicit instantiation %qD does not match "
23643 "declared type %qT", TREE_TYPE (result
), decl
,
23648 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23650 error ("explicit instantiation of %q#D", decl
);
23656 /* Check for various error cases. Note that if the explicit
23657 instantiation is valid the RESULT will currently be marked as an
23658 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23659 until we get here. */
23661 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23663 /* DR 259 [temp.spec].
23665 Both an explicit instantiation and a declaration of an explicit
23666 specialization shall not appear in a program unless the explicit
23667 instantiation follows a declaration of the explicit specialization.
23669 For a given set of template parameters, if an explicit
23670 instantiation of a template appears after a declaration of an
23671 explicit specialization for that template, the explicit
23672 instantiation has no effect. */
23675 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23679 No program shall explicitly instantiate any template more
23682 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23683 the first instantiation was `extern' and the second is not,
23684 and EXTERN_P for the opposite case. */
23685 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23686 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23687 /* If an "extern" explicit instantiation follows an ordinary
23688 explicit instantiation, the template is instantiated. */
23692 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23694 error ("no matching template for %qD found", result
);
23697 else if (!DECL_TEMPLATE_INFO (result
))
23699 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23703 if (storage
== NULL_TREE
)
23705 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23707 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23708 pedwarn (input_location
, OPT_Wpedantic
,
23709 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23714 error ("storage class %qD applied to template instantiation", storage
);
23716 check_explicit_instantiation_namespace (result
);
23717 mark_decl_instantiated (result
, extern_p
);
23719 instantiate_decl (result
, /*defer_ok=*/true,
23720 /*expl_inst_class_mem_p=*/false);
23724 mark_class_instantiated (tree t
, int extern_p
)
23726 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23727 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23728 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23729 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23732 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23733 rest_of_type_compilation (t
, 1);
23737 /* Called from do_type_instantiation through binding_table_foreach to
23738 do recursive instantiation for the type bound in ENTRY. */
23740 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23742 tree storage
= *(tree
*) data
;
23744 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23745 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23746 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23747 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23750 /* Perform an explicit instantiation of template class T. STORAGE, if
23751 non-null, is the RID for extern, inline or static. COMPLAIN is
23752 nonzero if this is called from the parser, zero if called recursively,
23753 since the standard is unclear (as detailed below). */
23756 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23761 int previous_instantiation_extern_p
= 0;
23763 if (TREE_CODE (t
) == TYPE_DECL
)
23766 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23769 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23771 error ("explicit instantiation of non-class template %qD", tmpl
);
23773 error ("explicit instantiation of non-template type %qT", t
);
23779 if (!COMPLETE_TYPE_P (t
))
23781 if (complain
& tf_error
)
23782 error ("explicit instantiation of %q#T before definition of template",
23787 if (storage
!= NULL_TREE
)
23789 if (!in_system_header_at (input_location
))
23791 if (storage
== ridpointers
[(int) RID_EXTERN
])
23793 if (cxx_dialect
== cxx98
)
23794 pedwarn (input_location
, OPT_Wpedantic
,
23795 "ISO C++ 1998 forbids the use of %<extern%> on "
23796 "explicit instantiations");
23799 pedwarn (input_location
, OPT_Wpedantic
,
23800 "ISO C++ forbids the use of %qE"
23801 " on explicit instantiations", storage
);
23804 if (storage
== ridpointers
[(int) RID_INLINE
])
23806 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23808 else if (storage
== ridpointers
[(int) RID_STATIC
])
23812 error ("storage class %qD applied to template instantiation",
23818 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23820 /* DR 259 [temp.spec].
23822 Both an explicit instantiation and a declaration of an explicit
23823 specialization shall not appear in a program unless the explicit
23824 instantiation follows a declaration of the explicit specialization.
23826 For a given set of template parameters, if an explicit
23827 instantiation of a template appears after a declaration of an
23828 explicit specialization for that template, the explicit
23829 instantiation has no effect. */
23832 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
23836 No program shall explicitly instantiate any template more
23839 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23840 instantiation was `extern'. If EXTERN_P then the second is.
23841 These cases are OK. */
23842 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
23844 if (!previous_instantiation_extern_p
&& !extern_p
23845 && (complain
& tf_error
))
23846 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
23848 /* If we've already instantiated the template, just return now. */
23849 if (!CLASSTYPE_INTERFACE_ONLY (t
))
23853 check_explicit_instantiation_namespace (TYPE_NAME (t
));
23854 mark_class_instantiated (t
, extern_p
);
23859 /* In contrast to implicit instantiation, where only the
23860 declarations, and not the definitions, of members are
23861 instantiated, we have here:
23865 The explicit instantiation of a class template specialization
23866 implies the instantiation of all of its members not
23867 previously explicitly specialized in the translation unit
23868 containing the explicit instantiation.
23870 Of course, we can't instantiate member template classes, since we
23871 don't have any arguments for them. Note that the standard is
23872 unclear on whether the instantiation of the members are
23873 *explicit* instantiations or not. However, the most natural
23874 interpretation is that it should be an explicit
23876 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
23878 || (TREE_CODE (fld
) == FUNCTION_DECL
23880 && user_provided_p (fld
)))
23881 && DECL_TEMPLATE_INSTANTIATION (fld
))
23883 mark_decl_instantiated (fld
, extern_p
);
23885 instantiate_decl (fld
, /*defer_ok=*/true,
23886 /*expl_inst_class_mem_p=*/true);
23889 if (CLASSTYPE_NESTED_UTDS (t
))
23890 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
23891 bt_instantiate_type_proc
, &storage
);
23894 /* Given a function DECL, which is a specialization of TMPL, modify
23895 DECL to be a re-instantiation of TMPL with the same template
23896 arguments. TMPL should be the template into which tsubst'ing
23897 should occur for DECL, not the most general template.
23899 One reason for doing this is a scenario like this:
23902 void f(const T&, int i);
23904 void g() { f(3, 7); }
23907 void f(const T& t, const int i) { }
23909 Note that when the template is first instantiated, with
23910 instantiate_template, the resulting DECL will have no name for the
23911 first parameter, and the wrong type for the second. So, when we go
23912 to instantiate the DECL, we regenerate it. */
23915 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
23917 /* The arguments used to instantiate DECL, from the most general
23921 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
23923 /* Make sure that we can see identifiers, and compute access
23925 push_access_scope (decl
);
23927 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23935 args_depth
= TMPL_ARGS_DEPTH (args
);
23936 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
23937 if (args_depth
> parms_depth
)
23938 args
= get_innermost_template_args (args
, parms_depth
);
23940 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
23941 args
, tf_error
, NULL_TREE
,
23942 /*defer_ok*/false);
23943 if (specs
&& specs
!= error_mark_node
)
23944 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
23947 /* Merge parameter declarations. */
23948 decl_parm
= skip_artificial_parms_for (decl
,
23949 DECL_ARGUMENTS (decl
));
23951 = skip_artificial_parms_for (code_pattern
,
23952 DECL_ARGUMENTS (code_pattern
));
23953 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
23958 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23959 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
23960 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
23962 parm_type
= type_decays_to (parm_type
);
23963 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23964 TREE_TYPE (decl_parm
) = parm_type
;
23965 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23966 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
23968 DECL_ATTRIBUTES (decl_parm
) = attributes
;
23969 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
23971 decl_parm
= DECL_CHAIN (decl_parm
);
23972 pattern_parm
= DECL_CHAIN (pattern_parm
);
23974 /* Merge any parameters that match with the function parameter
23976 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
23979 tree expanded_types
;
23980 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23981 the parameters in this function parameter pack. */
23982 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
23983 args
, tf_error
, NULL_TREE
);
23984 len
= TREE_VEC_LENGTH (expanded_types
);
23985 for (i
= 0; i
< len
; i
++)
23990 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
23991 /* Rename the parameter to include the index. */
23992 DECL_NAME (decl_parm
) =
23993 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
23994 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
23995 parm_type
= type_decays_to (parm_type
);
23996 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
23997 TREE_TYPE (decl_parm
) = parm_type
;
23998 attributes
= DECL_ATTRIBUTES (pattern_parm
);
23999 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24001 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24002 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24004 decl_parm
= DECL_CHAIN (decl_parm
);
24007 /* Merge additional specifiers from the CODE_PATTERN. */
24008 if (DECL_DECLARED_INLINE_P (code_pattern
)
24009 && !DECL_DECLARED_INLINE_P (decl
))
24010 DECL_DECLARED_INLINE_P (decl
) = 1;
24012 else if (VAR_P (decl
))
24014 start_lambda_scope (decl
);
24015 DECL_INITIAL (decl
) =
24016 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
24017 tf_error
, DECL_TI_TEMPLATE (decl
),
24018 /*integral_constant_expression_p=*/false);
24019 finish_lambda_scope ();
24020 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24021 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24022 tf_error
, DECL_TI_TEMPLATE (decl
));
24025 gcc_unreachable ();
24027 pop_access_scope (decl
);
24030 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24031 substituted to get DECL. */
24034 template_for_substitution (tree decl
)
24036 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24038 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24039 for the instantiation. This is not always the most general
24040 template. Consider, for example:
24043 struct S { template <class U> void f();
24044 template <> void f<int>(); };
24046 and an instantiation of S<double>::f<int>. We want TD to be the
24047 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24048 while (/* An instantiation cannot have a definition, so we need a
24049 more general template. */
24050 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24051 /* We must also deal with friend templates. Given:
24053 template <class T> struct S {
24054 template <class U> friend void f() {};
24057 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24058 so far as the language is concerned, but that's still
24059 where we get the pattern for the instantiation from. On
24060 other hand, if the definition comes outside the class, say:
24062 template <class T> struct S {
24063 template <class U> friend void f();
24065 template <class U> friend void f() {}
24067 we don't need to look any further. That's what the check for
24068 DECL_INITIAL is for. */
24069 || (TREE_CODE (decl
) == FUNCTION_DECL
24070 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24071 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24073 /* The present template, TD, should not be a definition. If it
24074 were a definition, we should be using it! Note that we
24075 cannot restructure the loop to just keep going until we find
24076 a template with a definition, since that might go too far if
24077 a specialization was declared, but not defined. */
24079 /* Fetch the more general template. */
24080 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24086 /* Returns true if we need to instantiate this template instance even if we
24087 know we aren't going to emit it. */
24090 always_instantiate_p (tree decl
)
24092 /* We always instantiate inline functions so that we can inline them. An
24093 explicit instantiation declaration prohibits implicit instantiation of
24094 non-inline functions. With high levels of optimization, we would
24095 normally inline non-inline functions -- but we're not allowed to do
24096 that for "extern template" functions. Therefore, we check
24097 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24098 return ((TREE_CODE (decl
) == FUNCTION_DECL
24099 && (DECL_DECLARED_INLINE_P (decl
)
24100 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24101 /* And we need to instantiate static data members so that
24102 their initializers are available in integral constant
24105 && decl_maybe_constant_var_p (decl
)));
24108 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24109 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24110 error, true otherwise. */
24113 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24115 tree fntype
, spec
, noex
, clone
;
24117 /* Don't instantiate a noexcept-specification from template context. */
24118 if (processing_template_decl
24119 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24122 if (DECL_CLONED_FUNCTION_P (fn
))
24123 fn
= DECL_CLONED_FUNCTION (fn
);
24124 fntype
= TREE_TYPE (fn
);
24125 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24127 if (!spec
|| !TREE_PURPOSE (spec
))
24130 noex
= TREE_PURPOSE (spec
);
24132 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24134 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24135 bool added
= false;
24136 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24137 spec
= get_defaulted_eh_spec (fn
, complain
);
24138 else if (!(added
= !fns
->add (fn
)))
24140 /* If hash_set::add returns true, the element was already there. */
24141 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24142 DECL_SOURCE_LOCATION (fn
));
24144 "exception specification of %qD depends on itself",
24146 spec
= noexcept_false_spec
;
24148 else if (push_tinst_level (fn
))
24150 push_access_scope (fn
);
24151 push_deferring_access_checks (dk_no_deferred
);
24152 input_location
= DECL_SOURCE_LOCATION (fn
);
24153 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24154 DEFERRED_NOEXCEPT_ARGS (noex
),
24155 tf_warning_or_error
, fn
,
24156 /*function_p=*/false,
24157 /*integral_constant_expression_p=*/true);
24158 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24159 pop_deferring_access_checks ();
24160 pop_access_scope (fn
);
24161 pop_tinst_level ();
24162 if (spec
== error_mark_node
)
24163 spec
= noexcept_false_spec
;
24166 spec
= noexcept_false_spec
;
24171 if (spec
== error_mark_node
)
24174 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24177 FOR_EACH_CLONE (clone
, fn
)
24179 if (TREE_TYPE (clone
) == fntype
)
24180 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24182 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24188 /* We're starting to process the function INST, an instantiation of PATTERN;
24189 add their parameters to local_specializations. */
24192 register_parameter_specializations (tree pattern
, tree inst
)
24194 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24195 tree spec_parm
= DECL_ARGUMENTS (inst
);
24196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24198 register_local_specialization (spec_parm
, tmpl_parm
);
24199 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24200 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24202 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24204 if (!DECL_PACK_P (tmpl_parm
))
24206 register_local_specialization (spec_parm
, tmpl_parm
);
24207 spec_parm
= DECL_CHAIN (spec_parm
);
24211 /* Register the (value) argument pack as a specialization of
24212 TMPL_PARM, then move on. */
24213 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24214 register_local_specialization (argpack
, tmpl_parm
);
24217 gcc_assert (!spec_parm
);
24220 /* Produce the definition of D, a _DECL generated from a template. If
24221 DEFER_OK is true, then we don't have to actually do the
24222 instantiation now; we just have to do it sometime. Normally it is
24223 an error if this is an explicit instantiation but D is undefined.
24224 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24225 instantiated class template. */
24228 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24230 tree tmpl
= DECL_TI_TEMPLATE (d
);
24237 bool pattern_defined
;
24238 location_t saved_loc
= input_location
;
24239 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24240 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24244 /* This function should only be used to instantiate templates for
24245 functions and static member variables. */
24246 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
24248 /* A concept is never instantiated. */
24249 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
24251 /* Variables are never deferred; if instantiation is required, they
24252 are instantiated right away. That allows for better code in the
24253 case that an expression refers to the value of the variable --
24254 if the variable has a constant value the referring expression can
24255 take advantage of that fact. */
24259 /* Don't instantiate cloned functions. Instead, instantiate the
24260 functions they cloned. */
24261 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
24262 d
= DECL_CLONED_FUNCTION (d
);
24264 if (DECL_TEMPLATE_INSTANTIATED (d
)
24265 || (TREE_CODE (d
) == FUNCTION_DECL
24266 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
24267 || DECL_TEMPLATE_SPECIALIZATION (d
))
24268 /* D has already been instantiated or explicitly specialized, so
24269 there's nothing for us to do here.
24271 It might seem reasonable to check whether or not D is an explicit
24272 instantiation, and, if so, stop here. But when an explicit
24273 instantiation is deferred until the end of the compilation,
24274 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24275 the instantiation. */
24278 /* Check to see whether we know that this template will be
24279 instantiated in some other file, as with "extern template"
24281 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
24283 /* In general, we do not instantiate such templates. */
24284 if (external_p
&& !always_instantiate_p (d
))
24287 gen_tmpl
= most_general_template (tmpl
);
24288 gen_args
= DECL_TI_ARGS (d
);
24290 if (tmpl
!= gen_tmpl
)
24291 /* We should already have the extra args. */
24292 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
24293 == TMPL_ARGS_DEPTH (gen_args
));
24294 /* And what's in the hash table should match D. */
24295 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
24296 || spec
== NULL_TREE
);
24298 /* This needs to happen before any tsubsting. */
24299 if (! push_tinst_level (d
))
24302 timevar_push (TV_TEMPLATE_INST
);
24304 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24305 for the instantiation. */
24306 td
= template_for_substitution (d
);
24311 /* Look up an explicit specialization, if any. */
24312 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
24313 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
24314 if (elt
&& elt
!= error_mark_node
)
24316 td
= TREE_VALUE (elt
);
24317 args
= TREE_PURPOSE (elt
);
24321 code_pattern
= DECL_TEMPLATE_RESULT (td
);
24323 /* We should never be trying to instantiate a member of a class
24324 template or partial specialization. */
24325 gcc_assert (d
!= code_pattern
);
24327 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
24328 || DECL_TEMPLATE_SPECIALIZATION (td
))
24329 /* In the case of a friend template whose definition is provided
24330 outside the class, we may have too many arguments. Drop the
24331 ones we don't need. The same is true for specializations. */
24332 args
= get_innermost_template_args
24333 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
24335 if (TREE_CODE (d
) == FUNCTION_DECL
)
24337 deleted_p
= DECL_DELETED_FN (code_pattern
);
24338 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
24339 && DECL_INITIAL (code_pattern
) != error_mark_node
)
24340 || DECL_DEFAULTED_FN (code_pattern
)
24346 if (DECL_CLASS_SCOPE_P (code_pattern
))
24347 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
24348 || DECL_INLINE_VAR_P (code_pattern
));
24350 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
24353 /* We may be in the middle of deferred access check. Disable it now. */
24354 push_deferring_access_checks (dk_no_deferred
);
24356 /* Unless an explicit instantiation directive has already determined
24357 the linkage of D, remember that a definition is available for
24359 if (pattern_defined
24360 && !DECL_INTERFACE_KNOWN (d
)
24361 && !DECL_NOT_REALLY_EXTERN (d
))
24362 mark_definable (d
);
24364 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
24365 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
24366 input_location
= DECL_SOURCE_LOCATION (d
);
24368 /* If D is a member of an explicitly instantiated class template,
24369 and no definition is available, treat it like an implicit
24371 if (!pattern_defined
&& expl_inst_class_mem_p
24372 && DECL_EXPLICIT_INSTANTIATION (d
))
24374 /* Leave linkage flags alone on instantiations with anonymous
24376 if (TREE_PUBLIC (d
))
24378 DECL_NOT_REALLY_EXTERN (d
) = 0;
24379 DECL_INTERFACE_KNOWN (d
) = 0;
24381 SET_DECL_IMPLICIT_INSTANTIATION (d
);
24384 /* Defer all other templates, unless we have been explicitly
24385 forbidden from doing so. */
24386 if (/* If there is no definition, we cannot instantiate the
24389 /* If it's OK to postpone instantiation, do so. */
24391 /* If this is a static data member that will be defined
24392 elsewhere, we don't want to instantiate the entire data
24393 member, but we do want to instantiate the initializer so that
24394 we can substitute that elsewhere. */
24395 || (external_p
&& VAR_P (d
))
24396 /* Handle here a deleted function too, avoid generating
24397 its body (c++/61080). */
24400 /* The definition of the static data member is now required so
24401 we must substitute the initializer. */
24403 && !DECL_INITIAL (d
)
24404 && DECL_INITIAL (code_pattern
))
24408 bool const_init
= false;
24409 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24411 ns
= decl_namespace_context (d
);
24412 push_nested_namespace (ns
);
24414 push_nested_class (DECL_CONTEXT (d
));
24415 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
24417 tf_warning_or_error
, NULL_TREE
,
24418 /*integral_constant_expression_p=*/false);
24419 /* If instantiating the initializer involved instantiating this
24420 again, don't call cp_finish_decl twice. */
24421 if (!DECL_INITIAL (d
))
24423 /* Make sure the initializer is still constant, in case of
24424 circular dependency (template/instantiate6.C). */
24426 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24427 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
24428 /*asmspec_tree=*/NULL_TREE
,
24429 LOOKUP_ONLYCONVERTING
);
24432 pop_nested_class ();
24433 pop_nested_namespace (ns
);
24436 /* We restore the source position here because it's used by
24437 add_pending_template. */
24438 input_location
= saved_loc
;
24440 if (at_eof
&& !pattern_defined
24441 && DECL_EXPLICIT_INSTANTIATION (d
)
24442 && DECL_NOT_REALLY_EXTERN (d
))
24445 The definition of a non-exported function template, a
24446 non-exported member function template, or a non-exported
24447 member function or static data member of a class template
24448 shall be present in every translation unit in which it is
24449 explicitly instantiated. */
24450 permerror (input_location
, "explicit instantiation of %qD "
24451 "but no definition available", d
);
24453 /* If we're in unevaluated context, we just wanted to get the
24454 constant value; this isn't an odr use, so don't queue
24455 a full instantiation. */
24456 if (cp_unevaluated_operand
!= 0)
24458 /* ??? Historically, we have instantiated inline functions, even
24459 when marked as "extern template". */
24460 if (!(external_p
&& VAR_P (d
)))
24461 add_pending_template (d
);
24464 /* Tell the repository that D is available in this translation unit
24465 -- and see if it is supposed to be instantiated here. */
24466 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
24468 /* In a PCH file, despite the fact that the repository hasn't
24469 requested instantiation in the PCH it is still possible that
24470 an instantiation will be required in a file that includes the
24473 add_pending_template (d
);
24474 /* Instantiate inline functions so that the inliner can do its
24475 job, even though we'll not be emitting a copy of this
24477 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
24481 bool push_to_top
, nested
;
24483 fn_context
= decl_function_context (d
);
24484 if (LAMBDA_FUNCTION_P (d
))
24485 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24486 fn_context
= NULL_TREE
;
24487 nested
= current_function_decl
!= NULL_TREE
;
24488 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
24490 vec
<tree
> omp_privatization_save
;
24492 save_omp_privatization_clauses (omp_privatization_save
);
24495 push_to_top_level ();
24498 gcc_assert (!processing_template_decl
);
24499 push_function_context ();
24500 cp_unevaluated_operand
= 0;
24501 c_inhibit_evaluation_warnings
= 0;
24504 /* Mark D as instantiated so that recursive calls to
24505 instantiate_decl do not try to instantiate it again. */
24506 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24508 /* Regenerate the declaration in case the template has been modified
24509 by a subsequent redeclaration. */
24510 regenerate_decl_from_template (d
, td
, args
);
24512 /* We already set the file and line above. Reset them now in case
24513 they changed as a result of calling regenerate_decl_from_template. */
24514 input_location
= DECL_SOURCE_LOCATION (d
);
24519 bool const_init
= false;
24521 /* Clear out DECL_RTL; whatever was there before may not be right
24522 since we've reset the type of the declaration. */
24523 SET_DECL_RTL (d
, NULL
);
24524 DECL_IN_AGGR_P (d
) = 0;
24526 /* The initializer is placed in DECL_INITIAL by
24527 regenerate_decl_from_template so we don't need to
24528 push/pop_access_scope again here. Pull it out so that
24529 cp_finish_decl can process it. */
24530 init
= DECL_INITIAL (d
);
24531 DECL_INITIAL (d
) = NULL_TREE
;
24532 DECL_INITIALIZED_P (d
) = 0;
24534 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24535 initializer. That function will defer actual emission until
24536 we have a chance to determine linkage. */
24537 DECL_EXTERNAL (d
) = 0;
24539 /* Enter the scope of D so that access-checking works correctly. */
24540 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24542 push_nested_class (DECL_CONTEXT (d
));
24544 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24545 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
24548 pop_nested_class ();
24550 if (variable_template_p (gen_tmpl
))
24551 note_variable_template_instantiation (d
);
24553 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24554 synthesize_method (d
);
24555 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24557 /* Set up the list of local specializations. */
24558 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24559 tree block
= NULL_TREE
;
24561 /* Set up context. */
24562 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24563 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24564 block
= push_stmt_list ();
24566 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24568 /* Some typedefs referenced from within the template code need to be
24569 access checked at template instantiation time, i.e now. These
24570 types were added to the template at parsing time. Let's get those
24571 and perform the access checks then. */
24572 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24575 /* Create substitution entries for the parameters. */
24576 register_parameter_specializations (code_pattern
, d
);
24578 /* Substitute into the body of the function. */
24579 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24580 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24581 tf_warning_or_error
, tmpl
);
24584 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24585 tf_warning_or_error
, tmpl
,
24586 /*integral_constant_expression_p=*/false);
24588 /* Set the current input_location to the end of the function
24589 so that finish_function knows where we are. */
24591 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24593 /* Remember if we saw an infinite loop in the template. */
24594 current_function_infinite_loop
24595 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24598 /* Finish the function. */
24599 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24600 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24601 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24604 d
= finish_function (/*inline_p=*/false);
24605 expand_or_defer_fn (d
);
24608 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24609 cp_check_omp_declare_reduction (d
);
24612 /* We're not deferring instantiation any more. */
24613 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24616 pop_from_top_level ();
24618 pop_function_context ();
24621 restore_omp_privatization_clauses (omp_privatization_save
);
24624 pop_deferring_access_checks ();
24625 timevar_pop (TV_TEMPLATE_INST
);
24626 pop_tinst_level ();
24627 input_location
= saved_loc
;
24628 cp_unevaluated_operand
= saved_unevaluated_operand
;
24629 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24634 /* Run through the list of templates that we wish we could
24635 instantiate, and instantiate any we can. RETRIES is the
24636 number of times we retry pending template instantiation. */
24639 instantiate_pending_templates (int retries
)
24642 location_t saved_loc
= input_location
;
24644 /* Instantiating templates may trigger vtable generation. This in turn
24645 may require further template instantiations. We place a limit here
24646 to avoid infinite loop. */
24647 if (pending_templates
&& retries
>= max_tinst_depth
)
24649 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24651 fatal_error (input_location
,
24652 "template instantiation depth exceeds maximum of %d"
24653 " instantiating %q+D, possibly from virtual table generation"
24654 " (use -ftemplate-depth= to increase the maximum)",
24655 max_tinst_depth
, decl
);
24656 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24657 /* Pretend that we defined it. */
24658 DECL_INITIAL (decl
) = error_mark_node
;
24664 struct pending_template
**t
= &pending_templates
;
24665 struct pending_template
*last
= NULL
;
24669 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24670 bool complete
= false;
24672 if (TYPE_P (instantiation
))
24674 if (!COMPLETE_TYPE_P (instantiation
))
24676 instantiate_class_template (instantiation
);
24677 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24678 for (tree fld
= TYPE_FIELDS (instantiation
);
24679 fld
; fld
= TREE_CHAIN (fld
))
24681 || (TREE_CODE (fld
) == FUNCTION_DECL
24682 && !DECL_ARTIFICIAL (fld
)))
24683 && DECL_TEMPLATE_INSTANTIATION (fld
))
24684 instantiate_decl (fld
,
24685 /*defer_ok=*/false,
24686 /*expl_inst_class_mem_p=*/false);
24688 if (COMPLETE_TYPE_P (instantiation
))
24692 complete
= COMPLETE_TYPE_P (instantiation
);
24696 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24697 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24700 = instantiate_decl (instantiation
,
24701 /*defer_ok=*/false,
24702 /*expl_inst_class_mem_p=*/false);
24703 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24707 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24708 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24713 /* If INSTANTIATION has been instantiated, then we don't
24714 need to consider it again in the future. */
24715 struct pending_template
*drop
= *t
;
24717 set_refcount_ptr (drop
->tinst
);
24718 pending_template_freelist ().free (drop
);
24726 set_refcount_ptr (current_tinst_level
);
24728 last_pending_template
= last
;
24730 while (reconsider
);
24732 input_location
= saved_loc
;
24735 /* Substitute ARGVEC into T, which is a list of initializers for
24736 either base class or a non-static data member. The TREE_PURPOSEs
24737 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24738 instantiate_decl. */
24741 tsubst_initializer_list (tree t
, tree argvec
)
24743 tree inits
= NULL_TREE
;
24744 tree target_ctor
= error_mark_node
;
24746 for (; t
; t
= TREE_CHAIN (t
))
24750 tree expanded_bases
= NULL_TREE
;
24751 tree expanded_arguments
= NULL_TREE
;
24754 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24759 /* Expand the base class expansion type into separate base
24761 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24762 tf_warning_or_error
,
24764 if (expanded_bases
== error_mark_node
)
24767 /* We'll be building separate TREE_LISTs of arguments for
24769 len
= TREE_VEC_LENGTH (expanded_bases
);
24770 expanded_arguments
= make_tree_vec (len
);
24771 for (i
= 0; i
< len
; i
++)
24772 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24774 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24775 expand each argument in the TREE_VALUE of t. */
24776 expr
= make_node (EXPR_PACK_EXPANSION
);
24777 PACK_EXPANSION_LOCAL_P (expr
) = true;
24778 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24779 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24781 if (TREE_VALUE (t
) == void_type_node
)
24782 /* VOID_TYPE_NODE is used to indicate
24783 value-initialization. */
24785 for (i
= 0; i
< len
; i
++)
24786 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24790 /* Substitute parameter packs into each argument in the
24792 in_base_initializer
= 1;
24793 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
24795 tree expanded_exprs
;
24797 /* Expand the argument. */
24798 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
24800 = tsubst_pack_expansion (expr
, argvec
,
24801 tf_warning_or_error
,
24803 if (expanded_exprs
== error_mark_node
)
24806 /* Prepend each of the expanded expressions to the
24807 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24808 for (i
= 0; i
< len
; i
++)
24810 TREE_VEC_ELT (expanded_arguments
, i
) =
24811 tree_cons (NULL_TREE
,
24812 TREE_VEC_ELT (expanded_exprs
, i
),
24813 TREE_VEC_ELT (expanded_arguments
, i
));
24816 in_base_initializer
= 0;
24818 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24819 since we built them backwards. */
24820 for (i
= 0; i
< len
; i
++)
24822 TREE_VEC_ELT (expanded_arguments
, i
) =
24823 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
24828 for (i
= 0; i
< len
; ++i
)
24830 if (expanded_bases
)
24832 decl
= TREE_VEC_ELT (expanded_bases
, i
);
24833 decl
= expand_member_init (decl
);
24834 init
= TREE_VEC_ELT (expanded_arguments
, i
);
24839 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
24840 tf_warning_or_error
, NULL_TREE
);
24842 decl
= expand_member_init (decl
);
24843 if (decl
&& !DECL_P (decl
))
24844 in_base_initializer
= 1;
24846 init
= TREE_VALUE (t
);
24848 if (init
!= void_type_node
)
24849 init
= tsubst_expr (init
, argvec
,
24850 tf_warning_or_error
, NULL_TREE
,
24851 /*integral_constant_expression_p=*/false);
24852 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
24853 /* If we had an initializer but it instantiated to nothing,
24854 value-initialize the object. This will only occur when
24855 the initializer was a pack expansion where the parameter
24856 packs used in that expansion were of length zero. */
24857 init
= void_type_node
;
24858 in_base_initializer
= 0;
24861 if (target_ctor
!= error_mark_node
24862 && init
!= error_mark_node
)
24864 error ("mem-initializer for %qD follows constructor delegation",
24868 /* Look for a target constructor. */
24869 if (init
!= error_mark_node
24870 && decl
&& CLASS_TYPE_P (decl
)
24871 && same_type_p (decl
, current_class_type
))
24873 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
24876 error ("constructor delegation follows mem-initializer for %qD",
24877 TREE_PURPOSE (inits
));
24880 target_ctor
= init
;
24885 init
= build_tree_list (decl
, init
);
24886 TREE_CHAIN (init
) = inits
;
24894 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24897 set_current_access_from_decl (tree decl
)
24899 if (TREE_PRIVATE (decl
))
24900 current_access_specifier
= access_private_node
;
24901 else if (TREE_PROTECTED (decl
))
24902 current_access_specifier
= access_protected_node
;
24904 current_access_specifier
= access_public_node
;
24907 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24908 is the instantiation (which should have been created with
24909 start_enum) and ARGS are the template arguments to use. */
24912 tsubst_enum (tree tag
, tree newtag
, tree args
)
24916 if (SCOPED_ENUM_P (newtag
))
24917 begin_scope (sk_scoped_enum
, newtag
);
24919 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
24924 decl
= TREE_VALUE (e
);
24925 /* Note that in a template enum, the TREE_VALUE is the
24926 CONST_DECL, not the corresponding INTEGER_CST. */
24927 value
= tsubst_expr (DECL_INITIAL (decl
),
24928 args
, tf_warning_or_error
, NULL_TREE
,
24929 /*integral_constant_expression_p=*/true);
24931 /* Give this enumeration constant the correct access. */
24932 set_current_access_from_decl (decl
);
24934 /* Actually build the enumerator itself. Here we're assuming that
24935 enumerators can't have dependent attributes. */
24936 build_enumerator (DECL_NAME (decl
), value
, newtag
,
24937 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
24940 if (SCOPED_ENUM_P (newtag
))
24943 finish_enum_value_list (newtag
);
24944 finish_enum (newtag
);
24946 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
24947 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
24950 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24951 its type -- but without substituting the innermost set of template
24952 arguments. So, innermost set of template parameters will appear in
24956 get_mostly_instantiated_function_type (tree decl
)
24958 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24959 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
24962 /* Return truthvalue if we're processing a template different from
24963 the last one involved in diagnostics. */
24965 problematic_instantiation_changed (void)
24967 return current_tinst_level
!= last_error_tinst_level
;
24970 /* Remember current template involved in diagnostics. */
24972 record_last_problematic_instantiation (void)
24974 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
24977 struct tinst_level
*
24978 current_instantiation (void)
24980 return current_tinst_level
;
24983 /* Return TRUE if current_function_decl is being instantiated, false
24987 instantiating_current_function_p (void)
24989 return (current_instantiation ()
24990 && (current_instantiation ()->maybe_get_node ()
24991 == current_function_decl
));
24994 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24995 type. Return false for ok, true for disallowed. Issue error and
24996 inform messages under control of COMPLAIN. */
24999 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25001 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25003 else if (TYPE_PTR_P (type
))
25005 else if (TYPE_REF_P (type
)
25006 && !TYPE_REF_IS_RVALUE (type
))
25008 else if (TYPE_PTRMEM_P (type
))
25010 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25012 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25014 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25016 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25018 /* A bound template template parm could later be instantiated to have a valid
25019 nontype parm type via an alias template. */
25020 else if (cxx_dialect
>= cxx11
25021 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25023 else if (CLASS_TYPE_P (type
))
25025 if (cxx_dialect
< cxx2a
)
25027 error ("non-type template parameters of class type only available "
25028 "with -std=c++2a or -std=gnu++2a");
25031 if (!complete_type_or_else (type
, NULL_TREE
))
25033 if (!literal_type_p (type
))
25035 error ("%qT is not a valid type for a template non-type parameter "
25036 "because it is not literal", type
);
25037 explain_non_literal_class (type
);
25040 if (cp_has_mutable_p (type
))
25042 error ("%qT is not a valid type for a template non-type parameter "
25043 "because it has a mutable member", type
);
25046 /* FIXME check op<=> and strong structural equality once spaceship is
25051 if (complain
& tf_error
)
25053 if (type
== error_mark_node
)
25054 inform (input_location
, "invalid template non-type parameter");
25056 error ("%q#T is not a valid type for a template non-type parameter",
25062 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25063 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25066 dependent_type_p_r (tree type
)
25072 A type is dependent if it is:
25074 -- a template parameter. Template template parameters are types
25075 for us (since TYPE_P holds true for them) so we handle
25077 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25078 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25080 /* -- a qualified-id with a nested-name-specifier which contains a
25081 class-name that names a dependent type or whose unqualified-id
25082 names a dependent type. */
25083 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25086 /* An alias template specialization can be dependent even if the
25087 resulting type is not. */
25088 if (dependent_alias_template_spec_p (type
))
25091 /* -- a cv-qualified type where the cv-unqualified type is
25093 No code is necessary for this bullet; the code below handles
25094 cv-qualified types, and we don't want to strip aliases with
25095 TYPE_MAIN_VARIANT because of DR 1558. */
25096 /* -- a compound type constructed from any dependent type. */
25097 if (TYPE_PTRMEM_P (type
))
25098 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25099 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25101 else if (INDIRECT_TYPE_P (type
))
25102 return dependent_type_p (TREE_TYPE (type
));
25103 else if (TREE_CODE (type
) == FUNCTION_TYPE
25104 || TREE_CODE (type
) == METHOD_TYPE
)
25108 if (dependent_type_p (TREE_TYPE (type
)))
25110 for (arg_type
= TYPE_ARG_TYPES (type
);
25112 arg_type
= TREE_CHAIN (arg_type
))
25113 if (dependent_type_p (TREE_VALUE (arg_type
)))
25115 if (cxx_dialect
>= cxx17
)
25116 /* A value-dependent noexcept-specifier makes the type dependent. */
25117 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25118 if (tree noex
= TREE_PURPOSE (spec
))
25119 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25120 affect overload resolution and treating it as dependent breaks
25122 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25123 && value_dependent_expression_p (noex
))
25127 /* -- an array type constructed from any dependent type or whose
25128 size is specified by a constant expression that is
25131 We checked for type- and value-dependence of the bounds in
25132 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25133 if (TREE_CODE (type
) == ARRAY_TYPE
)
25135 if (TYPE_DOMAIN (type
)
25136 && dependent_type_p (TYPE_DOMAIN (type
)))
25138 return dependent_type_p (TREE_TYPE (type
));
25141 /* -- a template-id in which either the template name is a template
25143 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25145 /* ... or any of the template arguments is a dependent type or
25146 an expression that is type-dependent or value-dependent. */
25147 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25148 && (any_dependent_template_arguments_p
25149 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25152 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25153 dependent; if the argument of the `typeof' expression is not
25154 type-dependent, then it should already been have resolved. */
25155 if (TREE_CODE (type
) == TYPEOF_TYPE
25156 || TREE_CODE (type
) == DECLTYPE_TYPE
25157 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25160 /* A template argument pack is dependent if any of its packed
25162 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25164 tree args
= ARGUMENT_PACK_ARGS (type
);
25165 int i
, len
= TREE_VEC_LENGTH (args
);
25166 for (i
= 0; i
< len
; ++i
)
25167 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25171 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25172 be template parameters. */
25173 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25176 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25179 /* The standard does not specifically mention types that are local
25180 to template functions or local classes, but they should be
25181 considered dependent too. For example:
25183 template <int I> void f() {
25188 The size of `E' cannot be known until the value of `I' has been
25189 determined. Therefore, `E' must be considered dependent. */
25190 scope
= TYPE_CONTEXT (type
);
25191 if (scope
&& TYPE_P (scope
))
25192 return dependent_type_p (scope
);
25193 /* Don't use type_dependent_expression_p here, as it can lead
25194 to infinite recursion trying to determine whether a lambda
25195 nested in a lambda is dependent (c++/47687). */
25196 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25197 && DECL_LANG_SPECIFIC (scope
)
25198 && DECL_TEMPLATE_INFO (scope
)
25199 && (any_dependent_template_arguments_p
25200 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25203 /* Other types are non-dependent. */
25207 /* Returns TRUE if TYPE is dependent, in the sense of
25208 [temp.dep.type]. Note that a NULL type is considered dependent. */
25211 dependent_type_p (tree type
)
25213 /* If there are no template parameters in scope, then there can't be
25214 any dependent types. */
25215 if (!processing_template_decl
)
25217 /* If we are not processing a template, then nobody should be
25218 providing us with a dependent type. */
25220 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25224 /* If the type is NULL, we have not computed a type for the entity
25225 in question; in that case, the type is dependent. */
25229 /* Erroneous types can be considered non-dependent. */
25230 if (type
== error_mark_node
)
25233 /* Getting here with global_type_node means we improperly called this
25234 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25235 gcc_checking_assert (type
!= global_type_node
);
25237 /* If we have not already computed the appropriate value for TYPE,
25239 if (!TYPE_DEPENDENT_P_VALID (type
))
25241 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25242 TYPE_DEPENDENT_P_VALID (type
) = 1;
25245 return TYPE_DEPENDENT_P (type
);
25248 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25249 lookup. In other words, a dependent type that is not the current
25253 dependent_scope_p (tree scope
)
25255 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25256 && !currently_open_class (scope
));
25259 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25260 an unknown base of 'this' (and is therefore instantiation-dependent). */
25263 unknown_base_ref_p (tree t
)
25265 if (!current_class_ptr
)
25268 tree mem
= TREE_OPERAND (t
, 1);
25269 if (shared_member_p (mem
))
25272 tree cur
= current_nonlambda_class_type ();
25273 if (!any_dependent_bases_p (cur
))
25276 tree ctx
= TREE_OPERAND (t
, 0);
25277 if (DERIVED_FROM_P (ctx
, cur
))
25283 /* T is a SCOPE_REF; return whether we need to consider it
25284 instantiation-dependent so that we can check access at instantiation
25285 time even though we know which member it resolves to. */
25288 instantiation_dependent_scope_ref_p (tree t
)
25290 if (DECL_P (TREE_OPERAND (t
, 1))
25291 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
25292 && !unknown_base_ref_p (t
)
25293 && accessible_in_template_p (TREE_OPERAND (t
, 0),
25294 TREE_OPERAND (t
, 1)))
25300 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25301 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25304 /* Note that this predicate is not appropriate for general expressions;
25305 only constant expressions (that satisfy potential_constant_expression)
25306 can be tested for value dependence. */
25309 value_dependent_expression_p (tree expression
)
25311 if (!processing_template_decl
|| expression
== NULL_TREE
)
25314 /* A type-dependent expression is also value-dependent. */
25315 if (type_dependent_expression_p (expression
))
25318 switch (TREE_CODE (expression
))
25321 /* A dependent member function of the current instantiation. */
25322 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
25324 case FUNCTION_DECL
:
25325 /* A dependent member function of the current instantiation. */
25326 if (DECL_CLASS_SCOPE_P (expression
)
25327 && dependent_type_p (DECL_CONTEXT (expression
)))
25331 case IDENTIFIER_NODE
:
25332 /* A name that has not been looked up -- must be dependent. */
25335 case TEMPLATE_PARM_INDEX
:
25336 /* A non-type template parm. */
25340 /* A non-type template parm. */
25341 if (DECL_TEMPLATE_PARM_P (expression
))
25343 return value_dependent_expression_p (DECL_INITIAL (expression
));
25346 /* A constant with literal type and is initialized
25347 with an expression that is value-dependent. */
25348 if (DECL_DEPENDENT_INIT_P (expression
)
25349 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25350 || TYPE_REF_P (TREE_TYPE (expression
)))
25352 if (DECL_HAS_VALUE_EXPR_P (expression
))
25354 tree value_expr
= DECL_VALUE_EXPR (expression
);
25355 if (value_dependent_expression_p (value_expr
))
25360 case DYNAMIC_CAST_EXPR
:
25361 case STATIC_CAST_EXPR
:
25362 case CONST_CAST_EXPR
:
25363 case REINTERPRET_CAST_EXPR
:
25365 case IMPLICIT_CONV_EXPR
:
25366 /* These expressions are value-dependent if the type to which
25367 the cast occurs is dependent or the expression being casted
25368 is value-dependent. */
25370 tree type
= TREE_TYPE (expression
);
25372 if (dependent_type_p (type
))
25375 /* A functional cast has a list of operands. */
25376 expression
= TREE_OPERAND (expression
, 0);
25379 /* If there are no operands, it must be an expression such
25380 as "int()". This should not happen for aggregate types
25381 because it would form non-constant expressions. */
25382 gcc_assert (cxx_dialect
>= cxx11
25383 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
25388 if (TREE_CODE (expression
) == TREE_LIST
)
25389 return any_value_dependent_elements_p (expression
);
25391 return value_dependent_expression_p (expression
);
25395 if (SIZEOF_EXPR_TYPE_P (expression
))
25396 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
25400 /* A `sizeof' expression is value-dependent if the operand is
25401 type-dependent or is a pack expansion. */
25402 expression
= TREE_OPERAND (expression
, 0);
25403 if (PACK_EXPANSION_P (expression
))
25405 else if (TYPE_P (expression
))
25406 return dependent_type_p (expression
);
25407 return instantiation_dependent_uneval_expression_p (expression
);
25409 case AT_ENCODE_EXPR
:
25410 /* An 'encode' expression is value-dependent if the operand is
25412 expression
= TREE_OPERAND (expression
, 0);
25413 return dependent_type_p (expression
);
25415 case NOEXCEPT_EXPR
:
25416 expression
= TREE_OPERAND (expression
, 0);
25417 return instantiation_dependent_uneval_expression_p (expression
);
25420 /* All instantiation-dependent expressions should also be considered
25421 value-dependent. */
25422 return instantiation_dependent_scope_ref_p (expression
);
25424 case COMPONENT_REF
:
25425 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
25426 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
25428 case NONTYPE_ARGUMENT_PACK
:
25429 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25430 is value-dependent. */
25432 tree values
= ARGUMENT_PACK_ARGS (expression
);
25433 int i
, len
= TREE_VEC_LENGTH (values
);
25435 for (i
= 0; i
< len
; ++i
)
25436 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
25444 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
25446 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
25452 if (TREE_CODE (type2
) != TREE_LIST
)
25453 return dependent_type_p (type2
);
25455 for (; type2
; type2
= TREE_CHAIN (type2
))
25456 if (dependent_type_p (TREE_VALUE (type2
)))
25463 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25464 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
25467 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25468 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
25472 tree op
= TREE_OPERAND (expression
, 0);
25473 return (value_dependent_expression_p (op
)
25474 || has_value_dependent_address (op
));
25477 case REQUIRES_EXPR
:
25478 /* Treat all requires-expressions as value-dependent so
25479 we don't try to fold them. */
25483 return dependent_type_p (TREE_OPERAND (expression
, 0));
25487 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
25489 tree fn
= get_callee_fndecl (expression
);
25491 nargs
= call_expr_nargs (expression
);
25492 for (i
= 0; i
< nargs
; ++i
)
25494 tree op
= CALL_EXPR_ARG (expression
, i
);
25495 /* In a call to a constexpr member function, look through the
25496 implicit ADDR_EXPR on the object argument so that it doesn't
25497 cause the call to be considered value-dependent. We also
25498 look through it in potential_constant_expression. */
25499 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
25500 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
25501 && TREE_CODE (op
) == ADDR_EXPR
)
25502 op
= TREE_OPERAND (op
, 0);
25503 if (value_dependent_expression_p (op
))
25509 case TEMPLATE_ID_EXPR
:
25510 return variable_concept_p (TREE_OPERAND (expression
, 0));
25516 if (dependent_type_p (TREE_TYPE (expression
)))
25518 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
25519 if (value_dependent_expression_p (val
))
25525 /* Treat a GNU statement expression as dependent to avoid crashing
25526 under instantiate_non_dependent_expr; it can't be constant. */
25530 /* A constant expression is value-dependent if any subexpression is
25531 value-dependent. */
25532 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25534 case tcc_reference
:
25536 case tcc_comparison
:
25538 case tcc_expression
:
25541 int i
, len
= cp_tree_operand_length (expression
);
25543 for (i
= 0; i
< len
; i
++)
25545 tree t
= TREE_OPERAND (expression
, i
);
25547 /* In some cases, some of the operands may be missing.
25548 (For example, in the case of PREDECREMENT_EXPR, the
25549 amount to increment by may be missing.) That doesn't
25550 make the expression dependent. */
25551 if (t
&& value_dependent_expression_p (t
))
25562 /* The expression is not value-dependent. */
25566 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25567 [temp.dep.expr]. Note that an expression with no type is
25568 considered dependent. Other parts of the compiler arrange for an
25569 expression with type-dependent subexpressions to have no type, so
25570 this function doesn't have to be fully recursive. */
25573 type_dependent_expression_p (tree expression
)
25575 if (!processing_template_decl
)
25578 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25581 STRIP_ANY_LOCATION_WRAPPER (expression
);
25583 /* An unresolved name is always dependent. */
25584 if (identifier_p (expression
)
25585 || TREE_CODE (expression
) == USING_DECL
25586 || TREE_CODE (expression
) == WILDCARD_DECL
)
25589 /* A fold expression is type-dependent. */
25590 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25591 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25592 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25593 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25596 /* Some expression forms are never type-dependent. */
25597 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25598 || TREE_CODE (expression
) == SIZEOF_EXPR
25599 || TREE_CODE (expression
) == ALIGNOF_EXPR
25600 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25601 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25602 || TREE_CODE (expression
) == TRAIT_EXPR
25603 || TREE_CODE (expression
) == TYPEID_EXPR
25604 || TREE_CODE (expression
) == DELETE_EXPR
25605 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25606 || TREE_CODE (expression
) == THROW_EXPR
25607 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25610 /* The types of these expressions depends only on the type to which
25611 the cast occurs. */
25612 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25613 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25614 || TREE_CODE (expression
) == CONST_CAST_EXPR
25615 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25616 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25617 || TREE_CODE (expression
) == CAST_EXPR
)
25618 return dependent_type_p (TREE_TYPE (expression
));
25620 /* The types of these expressions depends only on the type created
25621 by the expression. */
25622 if (TREE_CODE (expression
) == NEW_EXPR
25623 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25625 /* For NEW_EXPR tree nodes created inside a template, either
25626 the object type itself or a TREE_LIST may appear as the
25628 tree type
= TREE_OPERAND (expression
, 1);
25629 if (TREE_CODE (type
) == TREE_LIST
)
25630 /* This is an array type. We need to check array dimensions
25632 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25633 || value_dependent_expression_p
25634 (TREE_OPERAND (TREE_VALUE (type
), 1));
25636 return dependent_type_p (type
);
25639 if (TREE_CODE (expression
) == SCOPE_REF
)
25641 tree scope
= TREE_OPERAND (expression
, 0);
25642 tree name
= TREE_OPERAND (expression
, 1);
25644 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25645 contains an identifier associated by name lookup with one or more
25646 declarations declared with a dependent type, or...a
25647 nested-name-specifier or qualified-id that names a member of an
25648 unknown specialization. */
25649 return (type_dependent_expression_p (name
)
25650 || dependent_scope_p (scope
));
25653 if (TREE_CODE (expression
) == TEMPLATE_DECL
25654 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25655 return uses_outer_template_parms (expression
);
25657 if (TREE_CODE (expression
) == STMT_EXPR
)
25658 expression
= stmt_expr_value_expr (expression
);
25660 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25665 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25667 if (type_dependent_expression_p (elt
))
25673 /* A static data member of the current instantiation with incomplete
25674 array type is type-dependent, as the definition and specializations
25675 can have different bounds. */
25676 if (VAR_P (expression
)
25677 && DECL_CLASS_SCOPE_P (expression
)
25678 && dependent_type_p (DECL_CONTEXT (expression
))
25679 && VAR_HAD_UNKNOWN_BOUND (expression
))
25682 /* An array of unknown bound depending on a variadic parameter, eg:
25684 template<typename... Args>
25685 void foo (Args... args)
25687 int arr[] = { args... };
25690 template<int... vals>
25693 int arr[] = { vals... };
25696 If the array has no length and has an initializer, it must be that
25697 we couldn't determine its length in cp_complete_array_type because
25698 it is dependent. */
25699 if (VAR_P (expression
)
25700 && TREE_TYPE (expression
) != NULL_TREE
25701 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25702 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25703 && DECL_INITIAL (expression
))
25706 /* A function or variable template-id is type-dependent if it has any
25707 dependent template arguments. */
25708 if (VAR_OR_FUNCTION_DECL_P (expression
)
25709 && DECL_LANG_SPECIFIC (expression
)
25710 && DECL_TEMPLATE_INFO (expression
))
25712 /* Consider the innermost template arguments, since those are the ones
25713 that come from the template-id; the template arguments for the
25714 enclosing class do not make it type-dependent unless they are used in
25715 the type of the decl. */
25716 if (instantiates_primary_template_p (expression
)
25717 && (any_dependent_template_arguments_p
25718 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25722 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25723 type-dependent. Checking this is important for functions with auto return
25724 type, which looks like a dependent type. */
25725 if (TREE_CODE (expression
) == FUNCTION_DECL
25726 && !(DECL_CLASS_SCOPE_P (expression
)
25727 && dependent_type_p (DECL_CONTEXT (expression
)))
25728 && !(DECL_LANG_SPECIFIC (expression
)
25729 && DECL_FRIEND_P (expression
)
25730 && (!DECL_FRIEND_CONTEXT (expression
)
25731 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25732 && !DECL_LOCAL_FUNCTION_P (expression
))
25734 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25735 || undeduced_auto_decl (expression
));
25739 /* Always dependent, on the number of arguments if nothing else. */
25740 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25743 if (TREE_TYPE (expression
) == unknown_type_node
)
25745 if (TREE_CODE (expression
) == ADDR_EXPR
)
25746 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25747 if (TREE_CODE (expression
) == COMPONENT_REF
25748 || TREE_CODE (expression
) == OFFSET_REF
)
25750 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25752 expression
= TREE_OPERAND (expression
, 1);
25753 if (identifier_p (expression
))
25756 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25757 if (TREE_CODE (expression
) == SCOPE_REF
)
25760 if (BASELINK_P (expression
))
25762 if (BASELINK_OPTYPE (expression
)
25763 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25765 expression
= BASELINK_FUNCTIONS (expression
);
25768 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25770 if (any_dependent_template_arguments_p
25771 (TREE_OPERAND (expression
, 1)))
25773 expression
= TREE_OPERAND (expression
, 0);
25774 if (identifier_p (expression
))
25778 gcc_assert (TREE_CODE (expression
) == OVERLOAD
25779 || TREE_CODE (expression
) == FUNCTION_DECL
);
25781 for (lkp_iterator
iter (expression
); iter
; ++iter
)
25782 if (type_dependent_expression_p (*iter
))
25788 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
25790 /* Dependent type attributes might not have made it from the decl to
25792 if (DECL_P (expression
)
25793 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
25796 return (dependent_type_p (TREE_TYPE (expression
)));
25799 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25800 type-dependent if the expression refers to a member of the current
25801 instantiation and the type of the referenced member is dependent, or the
25802 class member access expression refers to a member of an unknown
25805 This function returns true if the OBJECT in such a class member access
25806 expression is of an unknown specialization. */
25809 type_dependent_object_expression_p (tree object
)
25811 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25813 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
25815 tree scope
= TREE_TYPE (object
);
25816 return (!scope
|| dependent_scope_p (scope
));
25819 /* walk_tree callback function for instantiation_dependent_expression_p,
25820 below. Returns non-zero if a dependent subexpression is found. */
25823 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
25828 /* We don't have to worry about decltype currently because decltype
25829 of an instantiation-dependent expr is a dependent type. This
25830 might change depending on the resolution of DR 1172. */
25831 *walk_subtrees
= false;
25834 enum tree_code code
= TREE_CODE (*tp
);
25837 /* Don't treat an argument list as dependent just because it has no
25841 case NONTYPE_ARGUMENT_PACK
:
25844 case TEMPLATE_PARM_INDEX
:
25845 if (dependent_type_p (TREE_TYPE (*tp
)))
25847 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
25849 /* We'll check value-dependence separately. */
25852 /* Handle expressions with type operands. */
25856 case AT_ENCODE_EXPR
:
25858 tree op
= TREE_OPERAND (*tp
, 0);
25859 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
25860 op
= TREE_TYPE (op
);
25863 if (dependent_type_p (op
))
25867 *walk_subtrees
= false;
25874 case COMPONENT_REF
:
25875 if (identifier_p (TREE_OPERAND (*tp
, 1)))
25876 /* In a template, finish_class_member_access_expr creates a
25877 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25878 type-dependent, so that we can check access control at
25879 instantiation time (PR 42277). See also Core issue 1273. */
25884 if (instantiation_dependent_scope_ref_p (*tp
))
25889 /* Treat statement-expressions as dependent. */
25893 /* Treat requires-expressions as dependent. */
25894 case REQUIRES_EXPR
:
25898 /* Treat calls to function concepts as dependent. */
25899 if (function_concept_check_p (*tp
))
25903 case TEMPLATE_ID_EXPR
:
25904 /* And variable concepts. */
25905 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
25910 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
25918 if (type_dependent_expression_p (*tp
))
25924 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25925 sense defined by the ABI:
25927 "An expression is instantiation-dependent if it is type-dependent
25928 or value-dependent, or it has a subexpression that is type-dependent
25929 or value-dependent."
25931 Except don't actually check value-dependence for unevaluated expressions,
25932 because in sizeof(i) we don't care about the value of i. Checking
25933 type-dependence will in turn check value-dependence of array bounds/template
25934 arguments as needed. */
25937 instantiation_dependent_uneval_expression_p (tree expression
)
25941 if (!processing_template_decl
)
25944 if (expression
== error_mark_node
)
25947 result
= cp_walk_tree_without_duplicates (&expression
,
25948 instantiation_dependent_r
, NULL
);
25949 return result
!= NULL_TREE
;
25952 /* As above, but also check value-dependence of the expression as a whole. */
25955 instantiation_dependent_expression_p (tree expression
)
25957 return (instantiation_dependent_uneval_expression_p (expression
)
25958 || value_dependent_expression_p (expression
));
25961 /* Like type_dependent_expression_p, but it also works while not processing
25962 a template definition, i.e. during substitution or mangling. */
25965 type_dependent_expression_p_push (tree expr
)
25968 ++processing_template_decl
;
25969 b
= type_dependent_expression_p (expr
);
25970 --processing_template_decl
;
25974 /* Returns TRUE if ARGS contains a type-dependent expression. */
25977 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
25982 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
25984 if (type_dependent_expression_p (arg
))
25990 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25991 expressions) contains any type-dependent expressions. */
25994 any_type_dependent_elements_p (const_tree list
)
25996 for (; list
; list
= TREE_CHAIN (list
))
25997 if (type_dependent_expression_p (TREE_VALUE (list
)))
26003 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26004 expressions) contains any value-dependent expressions. */
26007 any_value_dependent_elements_p (const_tree list
)
26009 for (; list
; list
= TREE_CHAIN (list
))
26010 if (value_dependent_expression_p (TREE_VALUE (list
)))
26016 /* Returns TRUE if the ARG (a template argument) is dependent. */
26019 dependent_template_arg_p (tree arg
)
26021 if (!processing_template_decl
)
26024 /* Assume a template argument that was wrongly written by the user
26025 is dependent. This is consistent with what
26026 any_dependent_template_arguments_p [that calls this function]
26028 if (!arg
|| arg
== error_mark_node
)
26031 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26032 arg
= argument_pack_select_arg (arg
);
26034 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26036 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26038 if (DECL_TEMPLATE_PARM_P (arg
))
26040 /* A member template of a dependent class is not necessarily
26041 type-dependent, but it is a dependent template argument because it
26042 will be a member of an unknown specialization to that template. */
26043 tree scope
= CP_DECL_CONTEXT (arg
);
26044 return TYPE_P (scope
) && dependent_type_p (scope
);
26046 else if (ARGUMENT_PACK_P (arg
))
26048 tree args
= ARGUMENT_PACK_ARGS (arg
);
26049 int i
, len
= TREE_VEC_LENGTH (args
);
26050 for (i
= 0; i
< len
; ++i
)
26052 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26058 else if (TYPE_P (arg
))
26059 return dependent_type_p (arg
);
26061 return (type_dependent_expression_p (arg
)
26062 || value_dependent_expression_p (arg
));
26065 /* Returns true if ARGS (a collection of template arguments) contains
26066 any types that require structural equality testing. */
26069 any_template_arguments_need_structural_equality_p (tree args
)
26076 if (args
== error_mark_node
)
26079 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26081 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26082 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26084 tree arg
= TREE_VEC_ELT (level
, j
);
26085 tree packed_args
= NULL_TREE
;
26088 if (ARGUMENT_PACK_P (arg
))
26090 /* Look inside the argument pack. */
26091 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26092 len
= TREE_VEC_LENGTH (packed_args
);
26095 for (k
= 0; k
< len
; ++k
)
26098 arg
= TREE_VEC_ELT (packed_args
, k
);
26100 if (error_operand_p (arg
))
26102 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26104 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26106 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26107 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26116 /* Returns true if ARGS (a collection of template arguments) contains
26117 any dependent arguments. */
26120 any_dependent_template_arguments_p (const_tree args
)
26127 if (args
== error_mark_node
)
26130 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26132 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26133 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26134 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26141 /* Returns true if ARGS contains any errors. */
26144 any_erroneous_template_args_p (const_tree args
)
26149 if (args
== error_mark_node
)
26152 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26154 if (tree ti
= get_template_info (args
))
26155 args
= TI_ARGS (ti
);
26163 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26165 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26166 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26167 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26174 /* Returns TRUE if the template TMPL is type-dependent. */
26177 dependent_template_p (tree tmpl
)
26179 if (TREE_CODE (tmpl
) == OVERLOAD
)
26181 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26182 if (dependent_template_p (*iter
))
26187 /* Template template parameters are dependent. */
26188 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26189 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26191 /* So are names that have not been looked up. */
26192 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26197 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26200 dependent_template_id_p (tree tmpl
, tree args
)
26202 return (dependent_template_p (tmpl
)
26203 || any_dependent_template_arguments_p (args
));
26206 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26210 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26214 if (!processing_template_decl
)
26217 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26219 tree decl
= TREE_VEC_ELT (declv
, i
);
26220 tree init
= TREE_VEC_ELT (initv
, i
);
26221 tree cond
= TREE_VEC_ELT (condv
, i
);
26222 tree incr
= TREE_VEC_ELT (incrv
, i
);
26224 if (type_dependent_expression_p (decl
)
26225 || TREE_CODE (decl
) == SCOPE_REF
)
26228 if (init
&& type_dependent_expression_p (init
))
26231 if (cond
== global_namespace
)
26234 if (type_dependent_expression_p (cond
))
26237 if (COMPARISON_CLASS_P (cond
)
26238 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26239 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26242 if (TREE_CODE (incr
) == MODOP_EXPR
)
26244 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
26245 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
26248 else if (type_dependent_expression_p (incr
))
26250 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
26252 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
26254 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
26256 tree t
= TREE_OPERAND (incr
, 1);
26257 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
26258 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
26261 /* If this loop has a class iterator with != comparison
26262 with increment other than i++/++i/i--/--i, make sure the
26263 increment is constant. */
26264 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26265 && TREE_CODE (cond
) == NE_EXPR
)
26267 if (TREE_OPERAND (t
, 0) == decl
)
26268 t
= TREE_OPERAND (t
, 1);
26270 t
= TREE_OPERAND (t
, 0);
26271 if (TREE_CODE (t
) != INTEGER_CST
)
26281 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26282 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26283 no such TYPE can be found. Note that this function peers inside
26284 uninstantiated templates and therefore should be used only in
26285 extremely limited situations. ONLY_CURRENT_P restricts this
26286 peering to the currently open classes hierarchy (which is required
26287 when comparing types). */
26290 resolve_typename_type (tree type
, bool only_current_p
)
26299 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
26301 scope
= TYPE_CONTEXT (type
);
26302 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26303 gcc_checking_assert (uses_template_parms (scope
));
26305 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26306 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26307 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26308 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26309 identifier of the TYPENAME_TYPE anymore.
26310 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26311 TYPENAME_TYPE instead, we avoid messing up with a possible
26312 typedef variant case. */
26313 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
26315 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26316 it first before we can figure out what NAME refers to. */
26317 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
26319 if (TYPENAME_IS_RESOLVING_P (scope
))
26320 /* Given a class template A with a dependent base with nested type C,
26321 typedef typename A::C::C C will land us here, as trying to resolve
26322 the initial A::C leads to the local C typedef, which leads back to
26323 A::C::C. So we break the recursion now. */
26326 scope
= resolve_typename_type (scope
, only_current_p
);
26328 /* If we don't know what SCOPE refers to, then we cannot resolve the
26330 if (!CLASS_TYPE_P (scope
))
26332 /* If this is a typedef, we don't want to look inside (c++/11987). */
26333 if (typedef_variant_p (type
))
26335 /* If SCOPE isn't the template itself, it will not have a valid
26336 TYPE_FIELDS list. */
26337 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
26338 /* scope is either the template itself or a compatible instantiation
26339 like X<T>, so look up the name in the original template. */
26340 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
26341 /* If scope has no fields, it can't be a current instantiation. Check this
26342 before currently_open_class to avoid infinite recursion (71515). */
26343 if (!TYPE_FIELDS (scope
))
26345 /* If the SCOPE is not the current instantiation, there's no reason
26346 to look inside it. */
26347 if (only_current_p
&& !currently_open_class (scope
))
26349 /* Enter the SCOPE so that name lookup will be resolved as if we
26350 were in the class definition. In particular, SCOPE will no
26351 longer be considered a dependent type. */
26352 pushed_scope
= push_scope (scope
);
26353 /* Look up the declaration. */
26354 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
26355 tf_warning_or_error
);
26357 result
= NULL_TREE
;
26359 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26360 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26361 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
26364 else if (identifier_p (fullname
)
26365 && TREE_CODE (decl
) == TYPE_DECL
)
26367 result
= TREE_TYPE (decl
);
26368 if (result
== error_mark_node
)
26369 result
= NULL_TREE
;
26371 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
26372 && DECL_CLASS_TEMPLATE_P (decl
))
26374 /* Obtain the template and the arguments. */
26375 tree tmpl
= TREE_OPERAND (fullname
, 0);
26376 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
26378 /* We get here with a plain identifier because a previous tentative
26379 parse of the nested-name-specifier as part of a ptr-operator saw
26380 ::template X<A>. The use of ::template is necessary in a
26381 ptr-operator, but wrong in a declarator-id.
26383 [temp.names]: In a qualified-id of a declarator-id, the keyword
26384 template shall not appear at the top level. */
26385 pedwarn (cp_expr_loc_or_loc (fullname
, input_location
), OPT_Wpedantic
,
26386 "keyword %<template%> not allowed in declarator-id");
26389 tree args
= TREE_OPERAND (fullname
, 1);
26390 /* Instantiate the template. */
26391 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
26392 /*entering_scope=*/true,
26393 tf_error
| tf_user
);
26394 if (result
== error_mark_node
)
26395 result
= NULL_TREE
;
26398 /* Leave the SCOPE. */
26400 pop_scope (pushed_scope
);
26402 /* If we failed to resolve it, return the original typename. */
26406 /* If lookup found a typename type, resolve that too. */
26407 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
26409 /* Ill-formed programs can cause infinite recursion here, so we
26410 must catch that. */
26411 TYPENAME_IS_RESOLVING_P (result
) = 1;
26412 result
= resolve_typename_type (result
, only_current_p
);
26413 TYPENAME_IS_RESOLVING_P (result
) = 0;
26416 /* Qualify the resulting type. */
26417 quals
= cp_type_quals (type
);
26419 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
26424 /* EXPR is an expression which is not type-dependent. Return a proxy
26425 for EXPR that can be used to compute the types of larger
26426 expressions containing EXPR. */
26429 build_non_dependent_expr (tree expr
)
26431 tree orig_expr
= expr
;
26434 /* When checking, try to get a constant value for all non-dependent
26435 expressions in order to expose bugs in *_dependent_expression_p
26436 and constexpr. This can affect code generation, see PR70704, so
26437 only do this for -fchecking=2. */
26438 if (flag_checking
> 1
26439 && cxx_dialect
>= cxx11
26440 /* Don't do this during nsdmi parsing as it can lead to
26441 unexpected recursive instantiations. */
26442 && !parsing_nsdmi ()
26443 /* Don't do this during concept expansion either and for
26444 the same reason. */
26445 && !expanding_concept ())
26446 fold_non_dependent_expr (expr
, tf_none
);
26448 STRIP_ANY_LOCATION_WRAPPER (expr
);
26450 /* Preserve OVERLOADs; the functions must be available to resolve
26453 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
26454 inner_expr
= stmt_expr_value_expr (inner_expr
);
26455 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
26456 inner_expr
= TREE_OPERAND (inner_expr
, 0);
26457 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
26458 inner_expr
= TREE_OPERAND (inner_expr
, 1);
26459 if (is_overloaded_fn (inner_expr
)
26460 || TREE_CODE (inner_expr
) == OFFSET_REF
)
26462 /* There is no need to return a proxy for a variable or enumerator. */
26463 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
26465 /* Preserve string constants; conversions from string constants to
26466 "char *" are allowed, even though normally a "const char *"
26467 cannot be used to initialize a "char *". */
26468 if (TREE_CODE (expr
) == STRING_CST
)
26470 /* Preserve void and arithmetic constants, as an optimization -- there is no
26471 reason to create a new node. */
26472 if (TREE_CODE (expr
) == VOID_CST
26473 || TREE_CODE (expr
) == INTEGER_CST
26474 || TREE_CODE (expr
) == REAL_CST
)
26476 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26477 There is at least one place where we want to know that a
26478 particular expression is a throw-expression: when checking a ?:
26479 expression, there are special rules if the second or third
26480 argument is a throw-expression. */
26481 if (TREE_CODE (expr
) == THROW_EXPR
)
26484 /* Don't wrap an initializer list, we need to be able to look inside. */
26485 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
26488 /* Don't wrap a dummy object, we need to be able to test for it. */
26489 if (is_dummy_object (expr
))
26492 if (TREE_CODE (expr
) == COND_EXPR
)
26493 return build3 (COND_EXPR
,
26495 TREE_OPERAND (expr
, 0),
26496 (TREE_OPERAND (expr
, 1)
26497 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
26498 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
26499 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
26500 if (TREE_CODE (expr
) == COMPOUND_EXPR
26501 && !COMPOUND_EXPR_OVERLOADED (expr
))
26502 return build2 (COMPOUND_EXPR
,
26504 TREE_OPERAND (expr
, 0),
26505 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
26507 /* If the type is unknown, it can't really be non-dependent */
26508 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
26510 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26511 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
26512 TREE_TYPE (expr
), expr
);
26515 /* ARGS is a vector of expressions as arguments to a function call.
26516 Replace the arguments with equivalent non-dependent expressions.
26517 This modifies ARGS in place. */
26520 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
26525 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
26527 tree newarg
= build_non_dependent_expr (arg
);
26529 (*args
)[ix
] = newarg
;
26533 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26534 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26535 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26538 make_auto_1 (tree name
, bool set_canonical
)
26540 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
26541 TYPE_NAME (au
) = build_decl (input_location
,
26542 TYPE_DECL
, name
, au
);
26543 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
26544 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
26545 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
26546 TYPE_NAME (au
), NULL_TREE
);
26548 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
26549 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
26550 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26556 make_decltype_auto (void)
26558 return make_auto_1 (decltype_auto_identifier
, true);
26564 return make_auto_1 (auto_identifier
, true);
26567 /* Return a C++17 deduction placeholder for class template TMPL. */
26570 make_template_placeholder (tree tmpl
)
26572 tree t
= make_auto_1 (auto_identifier
, true);
26573 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26577 /* True iff T is a C++17 class template deduction placeholder. */
26580 template_placeholder_p (tree t
)
26582 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26585 /* Make a "constrained auto" type-specifier. This is an
26586 auto type with constraints that must be associated after
26587 deduction. The constraint is formed from the given
26588 CONC and its optional sequence of arguments, which are
26589 non-null if written as partial-concept-id. */
26592 make_constrained_auto (tree con
, tree args
)
26594 tree type
= make_auto_1 (auto_identifier
, false);
26596 /* Build the constraint. */
26597 tree tmpl
= DECL_TI_TEMPLATE (con
);
26598 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26599 expr
= build_concept_check (expr
, type
, args
);
26601 tree constr
= normalize_expression (expr
);
26602 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26604 /* Our canonical type depends on the constraint. */
26605 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26607 /* Attach the constraint to the type declaration. */
26608 tree decl
= TYPE_NAME (type
);
26612 /* Given type ARG, return std::initializer_list<ARG>. */
26617 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26619 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26621 gcc_rich_location
richloc (input_location
);
26622 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
26623 error_at (&richloc
,
26624 "deducing from brace-enclosed initializer list"
26625 " requires %<#include <initializer_list>%>");
26627 return error_mark_node
;
26629 tree argvec
= make_tree_vec (1);
26630 TREE_VEC_ELT (argvec
, 0) = arg
;
26632 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26633 NULL_TREE
, 0, tf_warning_or_error
);
26636 /* Replace auto in TYPE with std::initializer_list<auto>. */
26639 listify_autos (tree type
, tree auto_node
)
26641 tree init_auto
= listify (auto_node
);
26642 tree argvec
= make_tree_vec (1);
26643 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26644 if (processing_template_decl
)
26645 argvec
= add_to_template_args (current_template_args (), argvec
);
26646 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26649 /* Hash traits for hashing possibly constrained 'auto'
26650 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26652 struct auto_hash
: default_hash_traits
<tree
>
26654 static inline hashval_t
hash (tree
);
26655 static inline bool equal (tree
, tree
);
26658 /* Hash the 'auto' T. */
26661 auto_hash::hash (tree t
)
26663 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
26664 /* Matching constrained-type-specifiers denote the same template
26665 parameter, so hash the constraint. */
26666 return hash_placeholder_constraint (c
);
26668 /* But unconstrained autos are all separate, so just hash the pointer. */
26669 return iterative_hash_object (t
, 0);
26672 /* Compare two 'auto's. */
26675 auto_hash::equal (tree t1
, tree t2
)
26680 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26681 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26683 /* Two unconstrained autos are distinct. */
26687 return equivalent_placeholder_constraints (c1
, c2
);
26690 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26691 constrained) auto, add it to the vector. */
26694 extract_autos_r (tree t
, void *data
)
26696 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26699 /* All the autos were built with index 0; fix that up now. */
26700 tree
*p
= hash
.find_slot (t
, INSERT
);
26703 /* If this is a repeated constrained-type-specifier, use the index we
26705 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26708 /* Otherwise this is new, so use the current count. */
26710 idx
= hash
.elements () - 1;
26712 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26715 /* Always keep walking. */
26719 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26720 says they can appear anywhere in the type. */
26723 extract_autos (tree type
)
26725 hash_set
<tree
> visited
;
26726 hash_table
<auto_hash
> hash (2);
26728 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26730 tree tree_vec
= make_tree_vec (hash
.elements());
26731 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26732 iter
!= hash
.end(); ++iter
)
26735 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26736 TREE_VEC_ELT (tree_vec
, i
)
26737 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26743 /* The stem for deduction guide names. */
26744 const char *const dguide_base
= "__dguide_";
26746 /* Return the name for a deduction guide for class template TMPL. */
26749 dguide_name (tree tmpl
)
26751 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26752 tree tname
= TYPE_IDENTIFIER (type
);
26753 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26754 + IDENTIFIER_LENGTH (tname
));
26755 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26756 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26757 IDENTIFIER_LENGTH (tname
) + 1);
26758 tree dname
= get_identifier (buf
);
26759 TREE_TYPE (dname
) = type
;
26763 /* True if NAME is the name of a deduction guide. */
26766 dguide_name_p (tree name
)
26768 return (TREE_CODE (name
) == IDENTIFIER_NODE
26769 && TREE_TYPE (name
)
26770 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
26771 strlen (dguide_base
)));
26774 /* True if FN is a deduction guide. */
26777 deduction_guide_p (const_tree fn
)
26780 if (tree name
= DECL_NAME (fn
))
26781 return dguide_name_p (name
);
26785 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26788 copy_guide_p (const_tree fn
)
26790 gcc_assert (deduction_guide_p (fn
));
26791 if (!DECL_ARTIFICIAL (fn
))
26793 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
26794 return (TREE_CHAIN (parms
) == void_list_node
26795 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
26798 /* True if FN is a guide generated from a constructor template. */
26801 template_guide_p (const_tree fn
)
26803 gcc_assert (deduction_guide_p (fn
));
26804 if (!DECL_ARTIFICIAL (fn
))
26806 tree tmpl
= DECL_TI_TEMPLATE (fn
);
26807 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
26808 return PRIMARY_TEMPLATE_P (org
);
26812 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26813 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26814 template parameter types. Note that the handling of template template
26815 parameters relies on current_template_parms being set appropriately for the
26819 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
26820 tree tsubst_args
, tsubst_flags_t complain
)
26822 if (olddecl
== error_mark_node
)
26823 return error_mark_node
;
26825 tree oldidx
= get_template_parm_index (olddecl
);
26828 if (TREE_CODE (olddecl
) == TYPE_DECL
26829 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26831 tree oldtype
= TREE_TYPE (olddecl
);
26832 newtype
= cxx_make_type (TREE_CODE (oldtype
));
26833 TYPE_MAIN_VARIANT (newtype
) = newtype
;
26834 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
26835 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
26836 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
26840 newtype
= TREE_TYPE (olddecl
);
26841 if (type_uses_auto (newtype
))
26843 // Substitute once to fix references to other template parameters.
26844 newtype
= tsubst (newtype
, tsubst_args
,
26845 complain
|tf_partial
, NULL_TREE
);
26846 // Now substitute again to reduce the level of the auto.
26847 newtype
= tsubst (newtype
, current_template_args (),
26848 complain
, NULL_TREE
);
26851 newtype
= tsubst (newtype
, tsubst_args
,
26852 complain
, NULL_TREE
);
26856 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
26857 DECL_NAME (olddecl
), newtype
);
26858 SET_DECL_TEMPLATE_PARM_P (newdecl
);
26861 if (TREE_CODE (olddecl
) == TYPE_DECL
26862 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26864 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
26865 = build_template_parm_index (index
, level
, level
,
26867 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26868 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26869 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
26870 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
26872 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
26874 DECL_TEMPLATE_RESULT (newdecl
)
26875 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
26876 DECL_NAME (olddecl
), newtype
);
26877 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
26878 // First create a copy (ttargs) of tsubst_args with an
26879 // additional level for the template template parameter's own
26880 // template parameters (ttparms).
26881 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
26882 (DECL_TEMPLATE_PARMS (olddecl
)));
26883 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
26884 tree ttargs
= make_tree_vec (depth
+ 1);
26885 for (int i
= 0; i
< depth
; ++i
)
26886 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
26887 TREE_VEC_ELT (ttargs
, depth
)
26888 = template_parms_level_to_args (ttparms
);
26889 // Substitute ttargs into ttparms to fix references to
26890 // other template parameters.
26891 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26892 complain
|tf_partial
);
26893 // Now substitute again with args based on tparms, to reduce
26894 // the level of the ttparms.
26895 ttargs
= current_template_args ();
26896 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
26898 // Finally, tack the adjusted parms onto tparms.
26899 ttparms
= tree_cons (size_int (depth
), ttparms
,
26900 current_template_parms
);
26901 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
26906 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
26908 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
26909 TREE_CODE (oldconst
),
26910 DECL_NAME (oldconst
), newtype
);
26911 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
26912 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
26913 SET_DECL_TEMPLATE_PARM_P (newconst
);
26914 newidx
= build_template_parm_index (index
, level
, level
,
26915 newconst
, newtype
);
26916 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
26917 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
26918 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
26924 /* Returns a C++17 class deduction guide template based on the constructor
26925 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26926 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26929 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
26931 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
26932 bool memtmpl
= false;
26935 tree fn_tmpl
= NULL_TREE
;
26940 bool copy_p
= TYPE_REF_P (type
);
26943 type
= TREE_TYPE (type
);
26944 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
26947 fparms
= void_list_node
;
26949 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
26950 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
26951 targs
= CLASSTYPE_TI_ARGS (type
);
26954 loc
= DECL_SOURCE_LOCATION (ctmpl
);
26955 explicit_p
= false;
26959 ++processing_template_decl
;
26963 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
26964 : DECL_TI_TEMPLATE (ctor
));
26966 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
26967 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
26969 type
= DECL_CONTEXT (ctor
);
26971 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
26972 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26973 fully specialized args for the enclosing class. Strip those off, as
26974 the deduction guide won't have those template parameters. */
26975 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
26976 TMPL_PARMS_DEPTH (tparms
));
26977 /* Discard the 'this' parameter. */
26978 fparms
= FUNCTION_ARG_CHAIN (ctor
);
26979 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
26980 ci
= get_constraints (ctor
);
26981 loc
= DECL_SOURCE_LOCATION (ctor
);
26982 explicit_p
= DECL_NONCONVERTING_P (ctor
);
26984 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
26988 /* For a member template constructor, we need to flatten the two
26989 template parameter lists into one, and then adjust the function
26990 signature accordingly. This gets...complicated. */
26991 tree save_parms
= current_template_parms
;
26993 /* For a member template we should have two levels of parms/args, one
26994 for the class and one for the constructor. We stripped
26995 specialized args for further enclosing classes above. */
26996 const int depth
= 2;
26997 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
26999 /* Template args for translating references to the two-level template
27000 parameters into references to the one-level template parameters we
27002 tree tsubst_args
= copy_node (targs
);
27003 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27004 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27006 /* Template parms for the constructor template. */
27007 tree ftparms
= TREE_VALUE (tparms
);
27008 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27009 /* Template parms for the class template. */
27010 tparms
= TREE_CHAIN (tparms
);
27011 tree ctparms
= TREE_VALUE (tparms
);
27012 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27013 /* Template parms for the deduction guide start as a copy of the
27014 template parms for the class. We set current_template_parms for
27015 lookup_template_class_1. */
27016 current_template_parms
= tparms
= copy_node (tparms
);
27017 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27018 for (unsigned i
= 0; i
< clen
; ++i
)
27019 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27021 /* Now we need to rewrite the constructor parms to append them to the
27023 for (unsigned i
= 0; i
< flen
; ++i
)
27025 unsigned index
= i
+ clen
;
27026 unsigned level
= 1;
27027 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27028 tree olddecl
= TREE_VALUE (oldelt
);
27029 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27030 tsubst_args
, complain
);
27031 if (newdecl
== error_mark_node
)
27033 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27034 tsubst_args
, complain
, ctor
);
27035 tree list
= build_tree_list (newdef
, newdecl
);
27036 TEMPLATE_PARM_CONSTRAINTS (list
)
27037 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27038 tsubst_args
, complain
, ctor
);
27039 TREE_VEC_ELT (new_vec
, index
) = list
;
27040 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
27043 /* Now we have a final set of template parms to substitute into the
27044 function signature. */
27045 targs
= template_parms_to_args (tparms
);
27046 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27048 if (fparms
== error_mark_node
)
27050 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27052 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27054 current_template_parms
= save_parms
;
27057 --processing_template_decl
;
27059 return error_mark_node
;
27064 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27065 tparms
= copy_node (tparms
);
27066 INNERMOST_TEMPLATE_PARMS (tparms
)
27067 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27070 tree fntype
= build_function_type (type
, fparms
);
27071 tree ded_fn
= build_lang_decl_loc (loc
,
27073 dguide_name (type
), fntype
);
27074 DECL_ARGUMENTS (ded_fn
) = fargs
;
27075 DECL_ARTIFICIAL (ded_fn
) = true;
27076 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27077 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27078 DECL_ARTIFICIAL (ded_tmpl
) = true;
27079 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27080 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27081 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27082 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27084 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27086 set_constraints (ded_tmpl
, ci
);
27091 /* Deduce template arguments for the class template placeholder PTYPE for
27092 template TMPL based on the initializer INIT, and return the resulting
27096 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
27097 tsubst_flags_t complain
)
27099 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
27101 /* We should have handled this in the caller. */
27102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27104 if (complain
& tf_error
)
27105 error ("non-class template %qT used without template arguments", tmpl
);
27106 return error_mark_node
;
27109 tree type
= TREE_TYPE (tmpl
);
27111 bool try_list_ctor
= false;
27113 vec
<tree
,va_gc
> *args
;
27114 if (init
== NULL_TREE
27115 || TREE_CODE (init
) == TREE_LIST
)
27116 args
= make_tree_vector_from_list (init
);
27117 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27119 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
27120 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
27122 /* As an exception, the first phase in 16.3.1.7 (considering the
27123 initializer list as a single argument) is omitted if the
27124 initializer list consists of a single expression of type cv U,
27125 where U is a specialization of C or a class derived from a
27126 specialization of C. */
27127 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
27128 tree etype
= TREE_TYPE (elt
);
27130 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
27131 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27132 int err
= unify (tparms
, targs
, type
, etype
,
27133 UNIFY_ALLOW_DERIVED
, /*explain*/false);
27135 try_list_ctor
= false;
27138 if (try_list_ctor
|| is_std_init_list (type
))
27139 args
= make_tree_vector_single (init
);
27141 args
= make_tree_vector_from_ctor (init
);
27144 args
= make_tree_vector_single (init
);
27146 tree dname
= dguide_name (tmpl
);
27147 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
27148 /*type*/false, /*complain*/false,
27150 bool elided
= false;
27151 if (cands
== error_mark_node
)
27154 /* Prune explicit deduction guides in copy-initialization context. */
27155 if (flags
& LOOKUP_ONLYCONVERTING
)
27157 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
27158 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27163 /* Found a nonconverting guide, prune the candidates. */
27164 tree pruned
= NULL_TREE
;
27165 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27166 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27167 pruned
= lookup_add (*iter
, pruned
);
27173 tree outer_args
= NULL_TREE
;
27174 if (DECL_CLASS_SCOPE_P (tmpl
)
27175 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
27177 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
27178 type
= TREE_TYPE (most_general_template (tmpl
));
27181 bool saw_ctor
= false;
27182 // FIXME cache artificial deduction guides
27183 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
27185 /* Skip inherited constructors. */
27186 if (iter
.using_p ())
27189 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
27190 if (guide
== error_mark_node
)
27191 return error_mark_node
;
27192 if ((flags
& LOOKUP_ONLYCONVERTING
)
27193 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
27196 cands
= lookup_add (guide
, cands
);
27201 tree call
= error_mark_node
;
27203 /* If this is list-initialization and the class has a list constructor, first
27204 try deducing from the list as a single argument, as [over.match.list]. */
27205 tree list_cands
= NULL_TREE
;
27206 if (try_list_ctor
&& cands
)
27207 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27210 if (is_list_ctor (dg
))
27211 list_cands
= lookup_add (dg
, list_cands
);
27215 ++cp_unevaluated_operand
;
27216 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
27217 --cp_unevaluated_operand
;
27219 if (call
== error_mark_node
)
27221 /* That didn't work, now try treating the list as a sequence of
27223 release_tree_vector (args
);
27224 args
= make_tree_vector_from_ctor (init
);
27228 /* Maybe generate an implicit deduction guide. */
27229 if (call
== error_mark_node
&& args
->length () < 2)
27231 tree gtype
= NULL_TREE
;
27233 if (args
->length () == 1)
27234 /* Generate a copy guide. */
27235 gtype
= build_reference_type (type
);
27236 else if (!saw_ctor
)
27237 /* Generate a default guide. */
27242 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
27243 if (guide
== error_mark_node
)
27244 return error_mark_node
;
27245 cands
= lookup_add (guide
, cands
);
27249 if (elided
&& !cands
)
27251 error ("cannot deduce template arguments for copy-initialization"
27252 " of %qT, as it has no non-explicit deduction guides or "
27253 "user-declared constructors", type
);
27254 return error_mark_node
;
27256 else if (!cands
&& call
== error_mark_node
)
27258 error ("cannot deduce template arguments of %qT, as it has no viable "
27259 "deduction guides", type
);
27260 return error_mark_node
;
27263 if (call
== error_mark_node
)
27265 ++cp_unevaluated_operand
;
27266 call
= build_new_function_call (cands
, &args
, tf_decltype
);
27267 --cp_unevaluated_operand
;
27270 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
27272 error ("class template argument deduction failed:");
27274 ++cp_unevaluated_operand
;
27275 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
27276 --cp_unevaluated_operand
;
27279 inform (input_location
, "explicit deduction guides not considered "
27280 "for copy-initialization");
27283 release_tree_vector (args
);
27285 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
27288 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27289 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27290 The CONTEXT determines the context in which auto deduction is performed
27291 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27292 OUTER_TARGS are used during template argument deduction
27293 (context == adc_unify) to properly substitute the result, and is ignored
27296 For partial-concept-ids, extra args may be appended to the list of deduced
27297 template arguments prior to determining constraint satisfaction. */
27300 do_auto_deduction (tree type
, tree init
, tree auto_node
,
27301 tsubst_flags_t complain
, auto_deduction_context context
,
27302 tree outer_targs
, int flags
)
27306 if (init
== error_mark_node
)
27307 return error_mark_node
;
27309 if (init
&& type_dependent_expression_p (init
)
27310 && context
!= adc_unify
)
27311 /* Defining a subset of type-dependent expressions that we can deduce
27312 from ahead of time isn't worth the trouble. */
27315 /* Similarly, we can't deduce from another undeduced decl. */
27316 if (init
&& undeduced_auto_decl (init
))
27319 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
27320 /* C++17 class template argument deduction. */
27321 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
27323 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
27324 /* Nothing we can do with this, even in deduction context. */
27327 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27328 with either a new invented type template parameter U or, if the
27329 initializer is a braced-init-list (8.5.4), with
27330 std::initializer_list<U>. */
27331 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27333 if (!DIRECT_LIST_INIT_P (init
))
27334 type
= listify_autos (type
, auto_node
);
27335 else if (CONSTRUCTOR_NELTS (init
) == 1)
27336 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
27339 if (complain
& tf_warning_or_error
)
27341 if (permerror (input_location
, "direct-list-initialization of "
27342 "%<auto%> requires exactly one element"))
27343 inform (input_location
,
27344 "for deduction to %<std::initializer_list%>, use copy-"
27345 "list-initialization (i.e. add %<=%> before the %<{%>)");
27347 type
= listify_autos (type
, auto_node
);
27351 if (type
== error_mark_node
)
27352 return error_mark_node
;
27354 init
= resolve_nondeduced_context (init
, complain
);
27356 if (context
== adc_decomp_type
27357 && auto_node
== type
27358 && init
!= error_mark_node
27359 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
27360 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27361 and initializer has array type, deduce cv-qualified array type. */
27362 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
27364 else if (AUTO_IS_DECLTYPE (auto_node
))
27366 tree stripped_init
= tree_strip_any_location_wrapper (init
);
27367 bool id
= (DECL_P (stripped_init
)
27368 || ((TREE_CODE (init
) == COMPONENT_REF
27369 || TREE_CODE (init
) == SCOPE_REF
)
27370 && !REF_PARENTHESIZED_P (init
)));
27371 targs
= make_tree_vec (1);
27372 TREE_VEC_ELT (targs
, 0)
27373 = finish_decltype_type (init
, id
, tf_warning_or_error
);
27374 if (type
!= auto_node
)
27376 if (complain
& tf_error
)
27377 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
27378 return error_mark_node
;
27383 tree parms
= build_tree_list (NULL_TREE
, type
);
27387 tparms
= extract_autos (type
);
27390 tparms
= make_tree_vec (1);
27391 TREE_VEC_ELT (tparms
, 0)
27392 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
27395 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27396 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27398 NULL
, /*explain_p=*/false);
27401 if (processing_template_decl
)
27402 /* Try again at instantiation time. */
27404 if (type
&& type
!= error_mark_node
27405 && (complain
& tf_error
))
27406 /* If type is error_mark_node a diagnostic must have been
27407 emitted by now. Also, having a mention to '<type error>'
27408 in the diagnostic is not really useful to the user. */
27410 if (cfun
&& auto_node
== current_function_auto_return_pattern
27411 && LAMBDA_FUNCTION_P (current_function_decl
))
27412 error ("unable to deduce lambda return type from %qE", init
);
27414 error ("unable to deduce %qT from %qE", type
, init
);
27415 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27417 NULL
, /*explain_p=*/true);
27419 return error_mark_node
;
27423 /* Check any placeholder constraints against the deduced type. */
27424 if (flag_concepts
&& !processing_template_decl
)
27425 if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
27427 /* Use the deduced type to check the associated constraints. If we
27428 have a partial-concept-id, rebuild the argument list so that
27429 we check using the extra arguments. */
27430 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
27431 tree cargs
= CHECK_CONSTR_ARGS (constr
);
27432 if (TREE_VEC_LENGTH (cargs
) > 1)
27434 cargs
= copy_node (cargs
);
27435 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
27439 if (!constraints_satisfied_p (constr
, cargs
))
27441 if (complain
& tf_warning_or_error
)
27443 auto_diagnostic_group d
;
27446 case adc_unspecified
:
27448 error("placeholder constraints not satisfied");
27450 case adc_variable_type
:
27451 case adc_decomp_type
:
27452 error ("deduced initializer does not satisfy "
27453 "placeholder constraints");
27455 case adc_return_type
:
27456 error ("deduced return type does not satisfy "
27457 "placeholder constraints");
27459 case adc_requirement
:
27460 error ("deduced expression type does not satisfy "
27461 "placeholder constraints");
27464 diagnose_constraints (input_location
, constr
, targs
);
27466 return error_mark_node
;
27470 if (processing_template_decl
&& context
!= adc_unify
)
27471 outer_targs
= current_template_args ();
27472 targs
= add_to_template_args (outer_targs
, targs
);
27473 return tsubst (type
, targs
, complain
, NULL_TREE
);
27476 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27480 splice_late_return_type (tree type
, tree late_return_type
)
27482 if (is_auto (type
))
27484 if (late_return_type
)
27485 return late_return_type
;
27487 tree idx
= get_template_parm_index (type
);
27488 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
27489 /* In an abbreviated function template we didn't know we were dealing
27490 with a function template when we saw the auto return type, so update
27491 it to have the correct level. */
27492 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
27497 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27498 'decltype(auto)' or a deduced class template. */
27501 is_auto (const_tree type
)
27503 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27504 && (TYPE_IDENTIFIER (type
) == auto_identifier
27505 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
27511 /* for_each_template_parm callback for type_uses_auto. */
27514 is_auto_r (tree tp
, void */
*data*/
)
27516 return is_auto (tp
);
27519 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27520 a use of `auto'. Returns NULL_TREE otherwise. */
27523 type_uses_auto (tree type
)
27525 if (type
== NULL_TREE
)
27527 else if (flag_concepts
)
27529 /* The Concepts TS allows multiple autos in one type-specifier; just
27530 return the first one we find, do_auto_deduction will collect all of
27532 if (uses_template_parms (type
))
27533 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
27534 /*visited*/NULL
, /*nondeduced*/false);
27539 return find_type_usage (type
, is_auto
);
27542 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27543 concepts are enabled, auto is acceptable in template arguments, but
27544 only when TEMPL identifies a template class. Return TRUE if any
27545 such errors were reported. */
27548 check_auto_in_tmpl_args (tree tmpl
, tree args
)
27550 /* If there were previous errors, nevermind. */
27551 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27554 /* If TMPL is an identifier, we're parsing and we can't tell yet
27555 whether TMPL is supposed to be a type, a function or a variable.
27556 We'll only be able to tell during template substitution, so we
27557 expect to be called again then. If concepts are enabled and we
27558 know we have a type, we're ok. */
27560 && (identifier_p (tmpl
)
27562 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27563 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27566 /* Quickly search for any occurrences of auto; usually there won't
27567 be any, and then we'll avoid allocating the vector. */
27568 if (!type_uses_auto (args
))
27571 bool errors
= false;
27573 tree vec
= extract_autos (args
);
27574 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27576 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27577 error_at (DECL_SOURCE_LOCATION (xauto
),
27578 "invalid use of %qT in template argument", xauto
);
27585 /* For a given template T, return the vector of typedefs referenced
27586 in T for which access check is needed at T instantiation time.
27587 T is either a FUNCTION_DECL or a RECORD_TYPE.
27588 Those typedefs were added to T by the function
27589 append_type_to_template_for_access_check. */
27591 vec
<qualified_typedef_usage_t
, va_gc
> *
27592 get_types_needing_access_check (tree t
)
27595 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27597 if (!t
|| t
== error_mark_node
)
27600 if (!(ti
= get_template_info (t
)))
27603 if (CLASS_TYPE_P (t
)
27604 || TREE_CODE (t
) == FUNCTION_DECL
)
27606 if (!TI_TEMPLATE (ti
))
27609 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27615 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27616 tied to T. That list of typedefs will be access checked at
27617 T instantiation time.
27618 T is either a FUNCTION_DECL or a RECORD_TYPE.
27619 TYPE_DECL is a TYPE_DECL node representing a typedef.
27620 SCOPE is the scope through which TYPE_DECL is accessed.
27621 LOCATION is the location of the usage point of TYPE_DECL.
27623 This function is a subroutine of
27624 append_type_to_template_for_access_check. */
27627 append_type_to_template_for_access_check_1 (tree t
,
27630 location_t location
)
27632 qualified_typedef_usage_t typedef_usage
;
27635 if (!t
|| t
== error_mark_node
)
27638 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27639 || CLASS_TYPE_P (t
))
27641 && TREE_CODE (type_decl
) == TYPE_DECL
27644 if (!(ti
= get_template_info (t
)))
27647 gcc_assert (TI_TEMPLATE (ti
));
27649 typedef_usage
.typedef_decl
= type_decl
;
27650 typedef_usage
.context
= scope
;
27651 typedef_usage
.locus
= location
;
27653 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27656 /* Append TYPE_DECL to the template TEMPL.
27657 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27658 At TEMPL instanciation time, TYPE_DECL will be checked to see
27659 if it can be accessed through SCOPE.
27660 LOCATION is the location of the usage point of TYPE_DECL.
27662 e.g. consider the following code snippet:
27669 template<class U> struct S
27671 C::myint mi; // <-- usage point of the typedef C::myint
27676 At S<char> instantiation time, we need to check the access of C::myint
27677 In other words, we need to check the access of the myint typedef through
27678 the C scope. For that purpose, this function will add the myint typedef
27679 and the scope C through which its being accessed to a list of typedefs
27680 tied to the template S. That list will be walked at template instantiation
27681 time and access check performed on each typedefs it contains.
27682 Note that this particular code snippet should yield an error because
27683 myint is private to C. */
27686 append_type_to_template_for_access_check (tree templ
,
27689 location_t location
)
27691 qualified_typedef_usage_t
*iter
;
27694 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27696 /* Make sure we don't append the type to the template twice. */
27697 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27698 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27701 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27705 /* Convert the generic type parameters in PARM that match the types given in the
27706 range [START_IDX, END_IDX) from the current_template_parms into generic type
27710 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27712 tree current
= current_template_parms
;
27713 int depth
= TMPL_PARMS_DEPTH (current
);
27714 current
= INNERMOST_TEMPLATE_PARMS (current
);
27715 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27717 for (int i
= 0; i
< start_idx
; ++i
)
27718 TREE_VEC_ELT (replacement
, i
)
27719 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27721 for (int i
= start_idx
; i
< end_idx
; ++i
)
27723 /* Create a distinct parameter pack type from the current parm and add it
27724 to the replacement args to tsubst below into the generic function
27727 tree o
= TREE_TYPE (TREE_VALUE
27728 (TREE_VEC_ELT (current
, i
)));
27729 tree t
= copy_type (o
);
27730 TEMPLATE_TYPE_PARM_INDEX (t
)
27731 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27733 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27734 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27735 TYPE_MAIN_VARIANT (t
) = t
;
27736 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27737 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27738 TREE_VEC_ELT (replacement
, i
) = t
;
27739 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27742 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27743 TREE_VEC_ELT (replacement
, i
)
27744 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27746 /* If there are more levels then build up the replacement with the outer
27749 replacement
= add_to_template_args (template_parms_to_args
27750 (TREE_CHAIN (current_template_parms
)),
27753 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
27756 /* Entries in the decl_constraint hash table. */
27757 struct GTY((for_user
)) constr_entry
27763 /* Hashing function and equality for constraint entries. */
27764 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
27766 static hashval_t
hash (constr_entry
*e
)
27768 return (hashval_t
)DECL_UID (e
->decl
);
27771 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
27773 return e1
->decl
== e2
->decl
;
27777 /* A mapping from declarations to constraint information. Note that
27778 both templates and their underlying declarations are mapped to the
27779 same constraint information.
27781 FIXME: This is defined in pt.c because garbage collection
27782 code is not being generated for constraint.cc. */
27784 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
27786 /* Returns the template constraints of declaration T. If T is not
27787 constrained, return NULL_TREE. Note that T must be non-null. */
27790 get_constraints (tree t
)
27792 if (!flag_concepts
)
27795 gcc_assert (DECL_P (t
));
27796 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27797 t
= DECL_TEMPLATE_RESULT (t
);
27798 constr_entry elt
= { t
, NULL_TREE
};
27799 constr_entry
* found
= decl_constraints
->find (&elt
);
27806 /* Associate the given constraint information CI with the declaration
27807 T. If T is a template, then the constraints are associated with
27808 its underlying declaration. Don't build associations if CI is
27812 set_constraints (tree t
, tree ci
)
27816 gcc_assert (t
&& flag_concepts
);
27817 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27818 t
= DECL_TEMPLATE_RESULT (t
);
27819 gcc_assert (!get_constraints (t
));
27820 constr_entry elt
= {t
, ci
};
27821 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
27822 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
27827 /* Remove the associated constraints of the declaration T. */
27830 remove_constraints (tree t
)
27832 gcc_assert (DECL_P (t
));
27833 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27834 t
= DECL_TEMPLATE_RESULT (t
);
27836 constr_entry elt
= {t
, NULL_TREE
};
27837 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
27839 decl_constraints
->clear_slot (slot
);
27842 /* Memoized satisfaction results for declarations. This
27843 maps the pair (constraint_info, arguments) to the result computed
27844 by constraints_satisfied_p. */
27846 struct GTY((for_user
)) constraint_sat_entry
27853 /* Hashing function and equality for constraint entries. */
27855 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
27857 static hashval_t
hash (constraint_sat_entry
*e
)
27859 hashval_t val
= iterative_hash_object(e
->ci
, 0);
27860 return iterative_hash_template_arg (e
->args
, val
);
27863 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
27865 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
27869 /* Memoized satisfaction results for concept checks. */
27871 struct GTY((for_user
)) concept_spec_entry
27878 /* Hashing function and equality for constraint entries. */
27880 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
27882 static hashval_t
hash (concept_spec_entry
*e
)
27884 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
27887 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
27889 ++comparing_specializations
;
27890 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
27891 --comparing_specializations
;
27896 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
27897 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
27899 /* Search for a memoized satisfaction result. Returns one of the
27900 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27903 lookup_constraint_satisfaction (tree ci
, tree args
)
27905 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
27906 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
27908 return found
->result
;
27913 /* Memoize the result of a satisfication test. Returns the saved result. */
27916 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
27918 constraint_sat_entry elt
= {ci
, args
, result
};
27919 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
27920 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
27926 /* Search for a memoized satisfaction result for a concept. */
27929 lookup_concept_satisfaction (tree tmpl
, tree args
)
27931 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27932 concept_spec_entry
* found
= concept_memos
->find (&elt
);
27934 return found
->result
;
27939 /* Memoize the result of a concept check. Returns the saved result. */
27942 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
27944 concept_spec_entry elt
= {tmpl
, args
, result
};
27945 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
27946 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27952 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
27954 /* Returns a prior concept specialization. This returns the substituted
27955 and normalized constraints defined by the concept. */
27958 get_concept_expansion (tree tmpl
, tree args
)
27960 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
27961 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
27963 return found
->result
;
27968 /* Save a concept expansion for later. */
27971 save_concept_expansion (tree tmpl
, tree args
, tree def
)
27973 concept_spec_entry elt
= {tmpl
, args
, def
};
27974 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
27975 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
27982 hash_subsumption_args (tree t1
, tree t2
)
27984 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
27985 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
27987 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
27988 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
27989 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
27990 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
27994 /* Compare the constraints of two subsumption entries. The LEFT1 and
27995 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27996 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27999 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
28001 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
28002 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
28003 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
28004 CHECK_CONSTR_ARGS (right1
)))
28005 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
28006 CHECK_CONSTR_ARGS (right2
));
28010 /* Key/value pair for learning and memoizing subsumption results. This
28011 associates a pair of check constraints (including arguments) with
28012 a boolean value indicating the result. */
28014 struct GTY((for_user
)) subsumption_entry
28021 /* Hashing function and equality for constraint entries. */
28023 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
28025 static hashval_t
hash (subsumption_entry
*e
)
28027 return hash_subsumption_args (e
->t1
, e
->t2
);
28030 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
28032 ++comparing_specializations
;
28033 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
28034 --comparing_specializations
;
28039 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
28041 /* Search for a previously cached subsumption result. */
28044 lookup_subsumption_result (tree t1
, tree t2
)
28046 subsumption_entry elt
= { t1
, t2
, false };
28047 subsumption_entry
* found
= subsumption_table
->find (&elt
);
28049 return &found
->result
;
28054 /* Save a subsumption result. */
28057 save_subsumption_result (tree t1
, tree t2
, bool result
)
28059 subsumption_entry elt
= {t1
, t2
, result
};
28060 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
28061 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
28067 /* Set up the hash table for constraint association. */
28070 init_constraint_processing (void)
28072 if (!flag_concepts
)
28075 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
28076 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
28077 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28078 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28079 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
28082 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28086 declare_integer_pack (void)
28088 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
28089 build_function_type_list (integer_type_node
,
28092 NULL_TREE
, ECF_CONST
);
28093 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
28094 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
28095 DECL_FUNCTION_CODE (ipfn
)
28096 = (enum built_in_function
) (int) CP_BUILT_IN_INTEGER_PACK
;
28099 /* Set up the hash tables for template instantiations. */
28102 init_template_processing (void)
28104 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
28105 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
28107 if (cxx_dialect
>= cxx11
)
28108 declare_integer_pack ();
28111 /* Print stats about the template hash tables for -fstats. */
28114 print_template_statistics (void)
28116 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28117 "%f collisions\n", (long) decl_specializations
->size (),
28118 (long) decl_specializations
->elements (),
28119 decl_specializations
->collisions ());
28120 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28121 "%f collisions\n", (long) type_specializations
->size (),
28122 (long) type_specializations
->elements (),
28123 type_specializations
->collisions ());
28128 namespace selftest
{
28130 /* Verify that build_non_dependent_expr () works, for various expressions,
28131 and that location wrappers don't affect the results. */
28134 test_build_non_dependent_expr ()
28136 location_t loc
= BUILTINS_LOCATION
;
28138 /* Verify constants, without and with location wrappers. */
28139 tree int_cst
= build_int_cst (integer_type_node
, 42);
28140 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28142 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28143 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28144 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28146 tree string_lit
= build_string (4, "foo");
28147 TREE_TYPE (string_lit
) = char_array_type_node
;
28148 string_lit
= fix_string_type (string_lit
);
28149 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28151 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28152 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28153 ASSERT_EQ (wrapped_string_lit
,
28154 build_non_dependent_expr (wrapped_string_lit
));
28157 /* Verify that type_dependent_expression_p () works correctly, even
28158 in the presence of location wrapper nodes. */
28161 test_type_dependent_expression_p ()
28163 location_t loc
= BUILTINS_LOCATION
;
28165 tree name
= get_identifier ("foo");
28167 /* If no templates are involved, nothing is type-dependent. */
28168 gcc_assert (!processing_template_decl
);
28169 ASSERT_FALSE (type_dependent_expression_p (name
));
28171 ++processing_template_decl
;
28173 /* Within a template, an unresolved name is always type-dependent. */
28174 ASSERT_TRUE (type_dependent_expression_p (name
));
28176 /* Ensure it copes with NULL_TREE and errors. */
28177 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28178 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28180 /* A USING_DECL in a template should be type-dependent, even if wrapped
28181 with a location wrapper (PR c++/83799). */
28182 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28183 TREE_TYPE (using_decl
) = integer_type_node
;
28184 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28185 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28186 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28187 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28189 --processing_template_decl
;
28192 /* Run all of the selftests within this file. */
28197 test_build_non_dependent_expr ();
28198 test_type_dependent_expression_p ();
28201 } // namespace selftest
28203 #endif /* #if CHECKING_P */
28205 #include "gt-cp-pt.h"