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 CLASSTYPE_TI_ARGS (inst
)
1094 = elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1097 = type_specializations
->find_slot (&elt
, INSERT
);
1098 entry
= ggc_alloc
<spec_entry
> ();
1103 /* But if we've had an implicit instantiation, that's a
1104 problem ([temp.expl.spec]/6). */
1105 error ("specialization %qT after instantiation %qT",
1109 /* Mark TYPE as a specialization. And as a result, we only
1110 have one level of template argument for the innermost
1112 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1113 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1114 CLASSTYPE_TI_ARGS (type
)
1115 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1118 else if (processing_specialization
)
1120 /* Someday C++0x may allow for enum template specialization. */
1121 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1122 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1123 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1124 "of %qD not allowed by ISO C++", type
);
1127 error ("explicit specialization of non-template %qT", type
);
1128 return error_mark_node
;
1135 /* Returns nonzero if we can optimize the retrieval of specializations
1136 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1137 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1140 optimize_specialization_lookup_p (tree tmpl
)
1142 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1143 && DECL_CLASS_SCOPE_P (tmpl
)
1144 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1146 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1147 /* The optimized lookup depends on the fact that the
1148 template arguments for the member function template apply
1149 purely to the containing class, which is not true if the
1150 containing class is an explicit or partial
1152 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1153 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1154 && !DECL_CONV_FN_P (tmpl
)
1155 /* It is possible to have a template that is not a member
1156 template and is not a member of a template class:
1158 template <typename T>
1159 struct S { friend A::f(); };
1161 Here, the friend function is a template, but the context does
1162 not have template information. The optimized lookup relies
1163 on having ARGS be the template arguments for both the class
1164 and the function template. */
1165 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1168 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1169 gone through coerce_template_parms by now. */
1172 verify_unstripped_args_1 (tree inner
)
1174 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1176 tree arg
= TREE_VEC_ELT (inner
, i
);
1177 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1179 else if (TYPE_P (arg
))
1180 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1181 else if (ARGUMENT_PACK_P (arg
))
1182 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg
));
1183 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1184 /* Allow typedefs on the type of a non-type argument, since a
1185 parameter can have them. */;
1187 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1192 verify_unstripped_args (tree args
)
1194 ++processing_template_decl
;
1195 if (!any_dependent_template_arguments_p (args
))
1196 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args
));
1197 --processing_template_decl
;
1200 /* Retrieve the specialization (in the sense of [temp.spec] - a
1201 specialization is either an instantiation or an explicit
1202 specialization) of TMPL for the given template ARGS. If there is
1203 no such specialization, return NULL_TREE. The ARGS are a vector of
1204 arguments, or a vector of vectors of arguments, in the case of
1205 templates with more than one level of parameters.
1207 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1208 then we search for a partial specialization matching ARGS. This
1209 parameter is ignored if TMPL is not a class template.
1211 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1212 result is a NONTYPE_ARGUMENT_PACK. */
1215 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1217 if (tmpl
== NULL_TREE
)
1220 if (args
== error_mark_node
)
1223 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1224 || TREE_CODE (tmpl
) == FIELD_DECL
);
1226 /* There should be as many levels of arguments as there are
1227 levels of parameters. */
1228 gcc_assert (TMPL_ARGS_DEPTH (args
)
1229 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1230 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1231 : template_class_depth (DECL_CONTEXT (tmpl
))));
1234 verify_unstripped_args (args
);
1236 /* Lambda functions in templates aren't instantiated normally, but through
1237 tsubst_lambda_expr. */
1238 if (lambda_fn_in_template_p (tmpl
))
1241 if (optimize_specialization_lookup_p (tmpl
))
1243 /* The template arguments actually apply to the containing
1244 class. Find the class specialization with those
1246 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1247 tree class_specialization
1248 = retrieve_specialization (class_template
, args
, 0);
1249 if (!class_specialization
)
1252 /* Find the instance of TMPL. */
1253 tree fns
= get_class_binding (class_specialization
, DECL_NAME (tmpl
));
1254 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1257 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1258 /* using-declarations can add base methods to the method vec,
1259 and we don't want those here. */
1260 && DECL_CONTEXT (fn
) == class_specialization
)
1269 hash_table
<spec_hasher
> *specializations
;
1273 elt
.spec
= NULL_TREE
;
1275 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1276 specializations
= type_specializations
;
1278 specializations
= decl_specializations
;
1281 hash
= spec_hasher::hash (&elt
);
1282 found
= specializations
->find_with_hash (&elt
, hash
);
1290 /* Like retrieve_specialization, but for local declarations. */
1293 retrieve_local_specialization (tree tmpl
)
1295 if (local_specializations
== NULL
)
1298 tree
*slot
= local_specializations
->get (tmpl
);
1299 return slot
? *slot
: NULL_TREE
;
1302 /* Returns nonzero iff DECL is a specialization of TMPL. */
1305 is_specialization_of (tree decl
, tree tmpl
)
1309 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1313 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1319 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1321 for (t
= TREE_TYPE (decl
);
1323 t
= CLASSTYPE_USE_TEMPLATE (t
)
1324 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1325 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1332 /* Returns nonzero iff DECL is a specialization of friend declaration
1333 FRIEND_DECL according to [temp.friend]. */
1336 is_specialization_of_friend (tree decl
, tree friend_decl
)
1338 bool need_template
= true;
1341 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1342 || TREE_CODE (decl
) == TYPE_DECL
);
1344 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1345 of a template class, we want to check if DECL is a specialization
1347 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1348 && DECL_TEMPLATE_INFO (friend_decl
)
1349 && !DECL_USE_TEMPLATE (friend_decl
))
1351 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1352 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1353 need_template
= false;
1355 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1356 && !PRIMARY_TEMPLATE_P (friend_decl
))
1357 need_template
= false;
1359 /* There is nothing to do if this is not a template friend. */
1360 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1363 if (is_specialization_of (decl
, friend_decl
))
1367 A member of a class template may be declared to be a friend of a
1368 non-template class. In this case, the corresponding member of
1369 every specialization of the class template is a friend of the
1370 class granting friendship.
1372 For example, given a template friend declaration
1374 template <class T> friend void A<T>::f();
1376 the member function below is considered a friend
1378 template <> struct A<int> {
1382 For this type of template friend, TEMPLATE_DEPTH below will be
1383 nonzero. To determine if DECL is a friend of FRIEND, we first
1384 check if the enclosing class is a specialization of another. */
1386 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1388 && DECL_CLASS_SCOPE_P (decl
)
1389 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1390 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1392 /* Next, we check the members themselves. In order to handle
1393 a few tricky cases, such as when FRIEND_DECL's are
1395 template <class T> friend void A<T>::g(T t);
1396 template <class T> template <T t> friend void A<T>::h();
1400 void A<int>::g(int);
1401 template <int> void A<int>::h();
1403 we need to figure out ARGS, the template arguments from
1404 the context of DECL. This is required for template substitution
1405 of `T' in the function parameter of `g' and template parameter
1406 of `h' in the above examples. Here ARGS corresponds to `int'. */
1408 tree context
= DECL_CONTEXT (decl
);
1409 tree args
= NULL_TREE
;
1410 int current_depth
= 0;
1412 while (current_depth
< template_depth
)
1414 if (CLASSTYPE_TEMPLATE_INFO (context
))
1416 if (current_depth
== 0)
1417 args
= TYPE_TI_ARGS (context
);
1419 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1422 context
= TYPE_CONTEXT (context
);
1425 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1430 tree friend_args_type
;
1431 tree decl_args_type
;
1433 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 is_template
= DECL_TEMPLATE_INFO (decl
)
1436 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1437 if (need_template
^ is_template
)
1439 else if (is_template
)
1441 /* If both are templates, check template parameter list. */
1443 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1445 if (!comp_template_parms
1446 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1450 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1453 decl_type
= TREE_TYPE (decl
);
1455 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1456 tf_none
, NULL_TREE
);
1457 if (friend_type
== error_mark_node
)
1460 /* Check if return types match. */
1461 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1464 /* Check if function parameter types match, ignoring the
1465 `this' parameter. */
1466 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1467 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1469 friend_args_type
= TREE_CHAIN (friend_args_type
);
1470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1471 decl_args_type
= TREE_CHAIN (decl_args_type
);
1473 return compparms (decl_args_type
, friend_args_type
);
1477 /* DECL is a TYPE_DECL */
1479 tree decl_type
= TREE_TYPE (decl
);
1481 /* Make sure that both DECL and FRIEND_DECL are templates or
1484 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1485 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1487 if (need_template
^ is_template
)
1489 else if (is_template
)
1492 /* If both are templates, check the name of the two
1493 TEMPLATE_DECL's first because is_friend didn't. */
1494 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1495 != DECL_NAME (friend_decl
))
1498 /* Now check template parameter list. */
1500 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1502 return comp_template_parms
1503 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1507 return (DECL_NAME (decl
)
1508 == DECL_NAME (friend_decl
));
1514 /* Register the specialization SPEC as a specialization of TMPL with
1515 the indicated ARGS. IS_FRIEND indicates whether the specialization
1516 is actually just a friend declaration. ATTRLIST is the list of
1517 attributes that the specialization is declared with or NULL when
1518 it isn't. Returns SPEC, or an equivalent prior declaration, if
1521 We also store instantiations of field packs in the hash table, even
1522 though they are not themselves templates, to make lookup easier. */
1525 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1529 spec_entry
**slot
= NULL
;
1532 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1533 || (TREE_CODE (tmpl
) == FIELD_DECL
1534 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1536 if (TREE_CODE (spec
) == FUNCTION_DECL
1537 && uses_template_parms (DECL_TI_ARGS (spec
)))
1538 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1539 register it; we want the corresponding TEMPLATE_DECL instead.
1540 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1541 the more obvious `uses_template_parms (spec)' to avoid problems
1542 with default function arguments. In particular, given
1543 something like this:
1545 template <class T> void f(T t1, T t = T())
1547 the default argument expression is not substituted for in an
1548 instantiation unless and until it is actually needed. */
1551 if (optimize_specialization_lookup_p (tmpl
))
1552 /* We don't put these specializations in the hash table, but we might
1553 want to give an error about a mismatch. */
1554 fn
= retrieve_specialization (tmpl
, args
, 0);
1562 hash
= spec_hasher::hash (&elt
);
1565 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1567 fn
= ((spec_entry
*) *slot
)->spec
;
1572 /* We can sometimes try to re-register a specialization that we've
1573 already got. In particular, regenerate_decl_from_template calls
1574 duplicate_decls which will update the specialization list. But,
1575 we'll still get called again here anyhow. It's more convenient
1576 to simply allow this than to try to prevent it. */
1579 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1581 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1583 if (DECL_ODR_USED (fn
)
1584 || DECL_EXPLICIT_INSTANTIATION (fn
))
1586 error ("specialization of %qD after instantiation",
1588 return error_mark_node
;
1593 /* This situation should occur only if the first
1594 specialization is an implicit instantiation, the
1595 second is an explicit specialization, and the
1596 implicit instantiation has not yet been used. That
1597 situation can occur if we have implicitly
1598 instantiated a member function and then specialized
1601 We can also wind up here if a friend declaration that
1602 looked like an instantiation turns out to be a
1605 template <class T> void foo(T);
1606 class S { friend void foo<>(int) };
1607 template <> void foo(int);
1609 We transform the existing DECL in place so that any
1610 pointers to it become pointers to the updated
1613 If there was a definition for the template, but not
1614 for the specialization, we want this to look as if
1615 there were no definition, and vice versa. */
1616 DECL_INITIAL (fn
) = NULL_TREE
;
1617 duplicate_decls (spec
, fn
, is_friend
);
1618 /* The call to duplicate_decls will have applied
1621 An explicit specialization of a function template
1622 is inline only if it is explicitly declared to be,
1623 and independently of whether its function template
1626 to the primary function; now copy the inline bits to
1627 the various clones. */
1628 FOR_EACH_CLONE (clone
, fn
)
1630 DECL_DECLARED_INLINE_P (clone
)
1631 = DECL_DECLARED_INLINE_P (fn
);
1632 DECL_SOURCE_LOCATION (clone
)
1633 = DECL_SOURCE_LOCATION (fn
);
1634 DECL_DELETED_FN (clone
)
1635 = DECL_DELETED_FN (fn
);
1637 check_specialization_namespace (tmpl
);
1642 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1644 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1645 if (dd
== error_mark_node
)
1646 /* We've already complained in duplicate_decls. */
1647 return error_mark_node
;
1649 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1650 /* Dup decl failed, but this is a new definition. Set the
1651 line number so any errors match this new
1653 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1659 return duplicate_decls (spec
, fn
, is_friend
);
1661 /* A specialization must be declared in the same namespace as the
1662 template it is specializing. */
1663 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1664 && !check_specialization_namespace (tmpl
))
1665 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1667 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1669 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1670 gcc_assert (tmpl
&& args
&& spec
);
1673 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1674 && PRIMARY_TEMPLATE_P (tmpl
)
1675 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1676 || variable_template_p (tmpl
))
1677 /* If TMPL is a forward declaration of a template function, keep a list
1678 of all specializations in case we need to reassign them to a friend
1679 template later in tsubst_friend_function.
1681 Also keep a list of all variable template instantiations so that
1682 process_partial_specialization can check whether a later partial
1683 specialization would have used it. */
1684 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1685 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1691 /* Returns true iff two spec_entry nodes are equivalent. */
1693 int comparing_specializations
;
1696 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1700 ++comparing_specializations
;
1701 equal
= (e1
->tmpl
== e2
->tmpl
1702 && comp_template_args (e1
->args
, e2
->args
));
1703 if (equal
&& flag_concepts
1704 /* tmpl could be a FIELD_DECL for a capture pack. */
1705 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1706 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1707 && uses_template_parms (e1
->args
))
1709 /* Partial specializations of a variable template can be distinguished by
1711 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1712 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1713 equal
= equivalent_constraints (c1
, c2
);
1715 --comparing_specializations
;
1720 /* Returns a hash for a template TMPL and template arguments ARGS. */
1723 hash_tmpl_and_args (tree tmpl
, tree args
)
1725 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1726 return iterative_hash_template_arg (args
, val
);
1729 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1733 spec_hasher::hash (spec_entry
*e
)
1735 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1738 /* Recursively calculate a hash value for a template argument ARG, for use
1739 in the hash tables of template specializations. */
1742 iterative_hash_template_arg (tree arg
, hashval_t val
)
1744 unsigned HOST_WIDE_INT i
;
1745 enum tree_code code
;
1748 if (arg
== NULL_TREE
)
1749 return iterative_hash_object (arg
, val
);
1754 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1757 code
= TREE_CODE (arg
);
1758 tclass
= TREE_CODE_CLASS (code
);
1760 val
= iterative_hash_object (code
, val
);
1767 case IDENTIFIER_NODE
:
1768 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1772 int i
, len
= TREE_VEC_LENGTH (arg
);
1773 for (i
= 0; i
< len
; ++i
)
1774 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1778 case TYPE_PACK_EXPANSION
:
1779 case EXPR_PACK_EXPANSION
:
1780 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1781 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1783 case TYPE_ARGUMENT_PACK
:
1784 case NONTYPE_ARGUMENT_PACK
:
1785 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1788 for (; arg
; arg
= TREE_CHAIN (arg
))
1789 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1793 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1794 val
= iterative_hash_template_arg (*iter
, val
);
1800 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1801 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1803 val
= iterative_hash_template_arg (field
, val
);
1804 val
= iterative_hash_template_arg (value
, val
);
1810 if (!DECL_ARTIFICIAL (arg
))
1812 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1813 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1815 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1818 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1821 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1822 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1824 case TEMPLATE_PARM_INDEX
:
1825 val
= iterative_hash_template_arg
1826 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1827 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1828 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1831 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1832 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1833 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1836 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1838 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1842 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1843 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1844 val
= iterative_hash_object (code
, val
);
1845 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1848 /* [temp.over.link] Two lambda-expressions are never considered
1851 So just hash the closure type. */
1852 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1855 case IMPLICIT_CONV_EXPR
:
1856 case STATIC_CAST_EXPR
:
1857 case REINTERPRET_CAST_EXPR
:
1858 case CONST_CAST_EXPR
:
1859 case DYNAMIC_CAST_EXPR
:
1861 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1862 /* Now hash operands as usual. */
1867 tree fn
= CALL_EXPR_FN (arg
);
1868 if (tree name
= dependent_name (fn
))
1870 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
1871 val
= iterative_hash_template_arg (TREE_OPERAND (fn
, 1), val
);
1874 val
= iterative_hash_template_arg (fn
, val
);
1875 call_expr_arg_iterator ai
;
1876 for (tree x
= first_call_expr_arg (arg
, &ai
); x
;
1877 x
= next_call_expr_arg (&ai
))
1878 val
= iterative_hash_template_arg (x
, val
);
1889 if (alias_template_specialization_p (arg
))
1891 // We want an alias specialization that survived strip_typedefs
1892 // to hash differently from its TYPE_CANONICAL, to avoid hash
1893 // collisions that compare as different in template_args_equal.
1894 // These could be dependent specializations that strip_typedefs
1895 // left alone, or untouched specializations because
1896 // coerce_template_parms returns the unconverted template
1897 // arguments if it sees incomplete argument packs.
1898 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1899 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1901 if (TYPE_CANONICAL (arg
))
1902 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1904 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1905 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1906 /* Otherwise just compare the types during lookup. */
1909 case tcc_declaration
:
1911 return iterative_hash_expr (arg
, val
);
1914 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1916 unsigned n
= cp_tree_operand_length (arg
);
1917 for (i
= 0; i
< n
; ++i
)
1918 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1926 /* Unregister the specialization SPEC as a specialization of TMPL.
1927 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1928 if the SPEC was listed as a specialization of TMPL.
1930 Note that SPEC has been ggc_freed, so we can't look inside it. */
1933 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1938 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1939 elt
.args
= TI_ARGS (tinfo
);
1940 elt
.spec
= NULL_TREE
;
1942 entry
= decl_specializations
->find (&elt
);
1945 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1946 gcc_assert (new_spec
!= NULL_TREE
);
1947 entry
->spec
= new_spec
;
1954 /* Like register_specialization, but for local declarations. We are
1955 registering SPEC, an instantiation of TMPL. */
1958 register_local_specialization (tree spec
, tree tmpl
)
1960 gcc_assert (tmpl
!= spec
);
1961 local_specializations
->put (tmpl
, spec
);
1964 /* TYPE is a class type. Returns true if TYPE is an explicitly
1965 specialized class. */
1968 explicit_class_specialization_p (tree type
)
1970 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1972 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1975 /* Print the list of functions at FNS, going through all the overloads
1976 for each element of the list. Alternatively, FNS cannot be a
1977 TREE_LIST, in which case it will be printed together with all the
1980 MORE and *STR should respectively be FALSE and NULL when the function
1981 is called from the outside. They are used internally on recursive
1982 calls. print_candidates manages the two parameters and leaves NULL
1983 in *STR when it ends. */
1986 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1988 if (TREE_CODE (fns
) == TREE_LIST
)
1989 for (; fns
; fns
= TREE_CHAIN (fns
))
1990 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1992 for (lkp_iterator
iter (fns
); iter
;)
1997 const char *pfx
= *str
;
2001 pfx
= _("candidates are:");
2003 pfx
= _("candidate is:");
2004 *str
= get_spaces (pfx
);
2006 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
2010 /* Print the list of candidate FNS in an error message. FNS can also
2011 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2014 print_candidates (tree fns
)
2017 print_candidates_1 (fns
, &str
);
2021 /* Get a (possibly) constrained template declaration for the
2022 purpose of ordering candidates. */
2024 get_template_for_ordering (tree list
)
2026 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
2027 tree f
= TREE_VALUE (list
);
2028 if (tree ti
= DECL_TEMPLATE_INFO (f
))
2029 return TI_TEMPLATE (ti
);
2033 /* Among candidates having the same signature, return the
2034 most constrained or NULL_TREE if there is no best candidate.
2035 If the signatures of candidates vary (e.g., template
2036 specialization vs. member function), then there can be no
2039 Note that we don't compare constraints on the functions
2040 themselves, but rather those of their templates. */
2042 most_constrained_function (tree candidates
)
2044 // Try to find the best candidate in a first pass.
2045 tree champ
= candidates
;
2046 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
2048 int winner
= more_constrained (get_template_for_ordering (champ
),
2049 get_template_for_ordering (c
));
2051 champ
= c
; // The candidate is more constrained
2052 else if (winner
== 0)
2053 return NULL_TREE
; // Neither is more constrained
2056 // Verify that the champ is better than previous candidates.
2057 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2058 if (!more_constrained (get_template_for_ordering (champ
),
2059 get_template_for_ordering (c
)))
2067 /* Returns the template (one of the functions given by TEMPLATE_ID)
2068 which can be specialized to match the indicated DECL with the
2069 explicit template args given in TEMPLATE_ID. The DECL may be
2070 NULL_TREE if none is available. In that case, the functions in
2071 TEMPLATE_ID are non-members.
2073 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2074 specialization of a member template.
2076 The TEMPLATE_COUNT is the number of references to qualifying
2077 template classes that appeared in the name of the function. See
2078 check_explicit_specialization for a more accurate description.
2080 TSK indicates what kind of template declaration (if any) is being
2081 declared. TSK_TEMPLATE indicates that the declaration given by
2082 DECL, though a FUNCTION_DECL, has template parameters, and is
2083 therefore a template function.
2085 The template args (those explicitly specified and those deduced)
2086 are output in a newly created vector *TARGS_OUT.
2088 If it is impossible to determine the result, an error message is
2089 issued. The error_mark_node is returned to indicate failure. */
2092 determine_specialization (tree template_id
,
2095 int need_member_template
,
2101 tree explicit_targs
;
2102 tree candidates
= NULL_TREE
;
2104 /* A TREE_LIST of templates of which DECL may be a specialization.
2105 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2106 corresponding TREE_PURPOSE is the set of template arguments that,
2107 when used to instantiate the template, would produce a function
2108 with the signature of DECL. */
2109 tree templates
= NULL_TREE
;
2111 cp_binding_level
*b
;
2113 *targs_out
= NULL_TREE
;
2115 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2116 return error_mark_node
;
2118 /* We shouldn't be specializing a member template of an
2119 unspecialized class template; we already gave an error in
2120 check_specialization_scope, now avoid crashing. */
2122 && template_count
&& DECL_CLASS_SCOPE_P (decl
)
2123 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2125 gcc_assert (errorcount
);
2126 return error_mark_node
;
2129 fns
= TREE_OPERAND (template_id
, 0);
2130 explicit_targs
= TREE_OPERAND (template_id
, 1);
2132 if (fns
== error_mark_node
)
2133 return error_mark_node
;
2135 /* Check for baselinks. */
2136 if (BASELINK_P (fns
))
2137 fns
= BASELINK_FUNCTIONS (fns
);
2139 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2141 error ("%qD is not a function template", fns
);
2142 return error_mark_node
;
2144 else if (VAR_P (decl
) && !variable_template_p (fns
))
2146 error ("%qD is not a variable template", fns
);
2147 return error_mark_node
;
2150 /* Count the number of template headers specified for this
2153 for (b
= current_binding_level
;
2154 b
->kind
== sk_template_parms
;
2158 tree orig_fns
= fns
;
2160 if (variable_template_p (fns
))
2162 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2163 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2164 tf_warning_or_error
,
2165 /*req_all*/true, /*use_defarg*/true);
2166 if (targs
!= error_mark_node
)
2167 templates
= tree_cons (targs
, fns
, templates
);
2169 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2173 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2175 tree decl_arg_types
;
2179 /* In case of explicit specialization, we need to check if
2180 the number of template headers appearing in the specialization
2181 is correct. This is usually done in check_explicit_specialization,
2182 but the check done there cannot be exhaustive when specializing
2183 member functions. Consider the following code:
2185 template <> void A<int>::f(int);
2186 template <> template <> void A<int>::f(int);
2188 Assuming that A<int> is not itself an explicit specialization
2189 already, the first line specializes "f" which is a non-template
2190 member function, whilst the second line specializes "f" which
2191 is a template member function. So both lines are syntactically
2192 correct, and check_explicit_specialization does not reject
2195 Here, we can do better, as we are matching the specialization
2196 against the declarations. We count the number of template
2197 headers, and we check if they match TEMPLATE_COUNT + 1
2198 (TEMPLATE_COUNT is the number of qualifying template classes,
2199 plus there must be another header for the member template
2202 Notice that if header_count is zero, this is not a
2203 specialization but rather a template instantiation, so there
2204 is no check we can perform here. */
2205 if (header_count
&& header_count
!= template_count
+ 1)
2208 /* Check that the number of template arguments at the
2209 innermost level for DECL is the same as for FN. */
2210 if (current_binding_level
->kind
== sk_template_parms
2211 && !current_binding_level
->explicit_spec_p
2212 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2213 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2214 (current_template_parms
))))
2217 /* DECL might be a specialization of FN. */
2218 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2219 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2221 /* For a non-static member function, we need to make sure
2222 that the const qualification is the same. Since
2223 get_bindings does not try to merge the "this" parameter,
2224 we must do the comparison explicitly. */
2225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2227 if (!same_type_p (TREE_VALUE (fn_arg_types
),
2228 TREE_VALUE (decl_arg_types
)))
2231 /* And the ref-qualification. */
2232 if (type_memfn_rqual (TREE_TYPE (decl
))
2233 != type_memfn_rqual (TREE_TYPE (fn
)))
2237 /* Skip the "this" parameter and, for constructors of
2238 classes with virtual bases, the VTT parameter. A
2239 full specialization of a constructor will have a VTT
2240 parameter, but a template never will. */
2242 = skip_artificial_parms_for (decl
, decl_arg_types
);
2244 = skip_artificial_parms_for (fn
, fn_arg_types
);
2246 /* Function templates cannot be specializations; there are
2247 no partial specializations of functions. Therefore, if
2248 the type of DECL does not match FN, there is no
2251 Note that it should never be the case that we have both
2252 candidates added here, and for regular member functions
2254 if (tsk
== tsk_template
)
2256 if (compparms (fn_arg_types
, decl_arg_types
))
2257 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2261 /* See whether this function might be a specialization of this
2262 template. Suppress access control because we might be trying
2263 to make this specialization a friend, and we have already done
2264 access control for the declaration of the specialization. */
2265 push_deferring_access_checks (dk_no_check
);
2266 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2267 pop_deferring_access_checks ();
2270 /* We cannot deduce template arguments that when used to
2271 specialize TMPL will produce DECL. */
2274 if (uses_template_parms (targs
))
2275 /* We deduced something involving 'auto', which isn't a valid
2276 template argument. */
2279 /* Remove, from the set of candidates, all those functions
2280 whose constraints are not satisfied. */
2281 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2284 // Then, try to form the new function type.
2285 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2286 if (insttype
== error_mark_node
)
2289 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2290 if (!compparms (fn_arg_types
, decl_arg_types
))
2293 /* Save this template, and the arguments deduced. */
2294 templates
= tree_cons (targs
, fn
, templates
);
2296 else if (need_member_template
)
2297 /* FN is an ordinary member function, and we need a
2298 specialization of a member template. */
2300 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2301 /* We can get IDENTIFIER_NODEs here in certain erroneous
2304 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2305 /* This is just an ordinary non-member function. Nothing can
2306 be a specialization of that. */
2308 else if (DECL_ARTIFICIAL (fn
))
2309 /* Cannot specialize functions that are created implicitly. */
2313 tree decl_arg_types
;
2315 /* This is an ordinary member function. However, since
2316 we're here, we can assume its enclosing class is a
2317 template class. For example,
2319 template <typename T> struct S { void f(); };
2320 template <> void S<int>::f() {}
2322 Here, S<int>::f is a non-template, but S<int> is a
2323 template class. If FN has the same type as DECL, we
2324 might be in business. */
2326 if (!DECL_TEMPLATE_INFO (fn
))
2327 /* Its enclosing class is an explicit specialization
2328 of a template class. This is not a candidate. */
2331 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2332 TREE_TYPE (TREE_TYPE (fn
))))
2333 /* The return types differ. */
2336 /* Adjust the type of DECL in case FN is a static member. */
2337 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2338 if (DECL_STATIC_FUNCTION_P (fn
)
2339 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2340 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2342 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2346 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2347 && (type_memfn_rqual (TREE_TYPE (decl
))
2348 != type_memfn_rqual (TREE_TYPE (fn
))))
2351 // If the deduced arguments do not satisfy the constraints,
2352 // this is not a candidate.
2353 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2356 // Add the candidate.
2357 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2361 if (templates
&& TREE_CHAIN (templates
))
2367 It is possible for a specialization with a given function
2368 signature to be instantiated from more than one function
2369 template. In such cases, explicit specification of the
2370 template arguments must be used to uniquely identify the
2371 function template specialization being specialized.
2373 Note that here, there's no suggestion that we're supposed to
2374 determine which of the candidate templates is most
2375 specialized. However, we, also have:
2379 Partial ordering of overloaded function template
2380 declarations is used in the following contexts to select
2381 the function template to which a function template
2382 specialization refers:
2384 -- when an explicit specialization refers to a function
2387 So, we do use the partial ordering rules, at least for now.
2388 This extension can only serve to make invalid programs valid,
2389 so it's safe. And, there is strong anecdotal evidence that
2390 the committee intended the partial ordering rules to apply;
2391 the EDG front end has that behavior, and John Spicer claims
2392 that the committee simply forgot to delete the wording in
2393 [temp.expl.spec]. */
2394 tree tmpl
= most_specialized_instantiation (templates
);
2395 if (tmpl
!= error_mark_node
)
2398 TREE_CHAIN (templates
) = NULL_TREE
;
2402 // Concepts allows multiple declarations of member functions
2403 // with the same signature. Like above, we need to rely on
2404 // on the partial ordering of those candidates to determine which
2406 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2408 if (tree cand
= most_constrained_function (candidates
))
2411 TREE_CHAIN (cand
) = NULL_TREE
;
2415 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2417 error ("template-id %qD for %q+D does not match any template "
2418 "declaration", template_id
, decl
);
2419 if (header_count
&& header_count
!= template_count
+ 1)
2420 inform (input_location
, "saw %d %<template<>%>, need %d for "
2421 "specializing a member function template",
2422 header_count
, template_count
+ 1);
2424 print_candidates (orig_fns
);
2425 return error_mark_node
;
2427 else if ((templates
&& TREE_CHAIN (templates
))
2428 || (candidates
&& TREE_CHAIN (candidates
))
2429 || (templates
&& candidates
))
2431 error ("ambiguous template specialization %qD for %q+D",
2433 candidates
= chainon (candidates
, templates
);
2434 print_candidates (candidates
);
2435 return error_mark_node
;
2438 /* We have one, and exactly one, match. */
2441 tree fn
= TREE_VALUE (candidates
);
2442 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2444 // Propagate the candidate's constraints to the declaration.
2445 set_constraints (decl
, get_constraints (fn
));
2447 /* DECL is a re-declaration or partial instantiation of a template
2449 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2451 /* It was a specialization of an ordinary member function in a
2453 return DECL_TI_TEMPLATE (fn
);
2456 /* It was a specialization of a template. */
2457 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2458 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2460 *targs_out
= copy_node (targs
);
2461 SET_TMPL_ARGS_LEVEL (*targs_out
,
2462 TMPL_ARGS_DEPTH (*targs_out
),
2463 TREE_PURPOSE (templates
));
2466 *targs_out
= TREE_PURPOSE (templates
);
2467 return TREE_VALUE (templates
);
2470 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2471 but with the default argument values filled in from those in the
2475 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2478 tree new_spec_types
;
2483 if (spec_types
== void_list_node
)
2484 return void_list_node
;
2486 /* Substitute into the rest of the list. */
2488 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2489 TREE_CHAIN (tmpl_types
));
2491 /* Add the default argument for this parameter. */
2492 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2493 TREE_VALUE (spec_types
),
2497 /* DECL is an explicit specialization. Replicate default arguments
2498 from the template it specializes. (That way, code like:
2500 template <class T> void f(T = 3);
2501 template <> void f(double);
2504 works, as required.) An alternative approach would be to look up
2505 the correct default arguments at the call-site, but this approach
2506 is consistent with how implicit instantiations are handled. */
2509 copy_default_args_to_explicit_spec (tree decl
)
2514 tree new_spec_types
;
2518 tree object_type
= NULL_TREE
;
2519 tree in_charge
= NULL_TREE
;
2520 tree vtt
= NULL_TREE
;
2522 /* See if there's anything we need to do. */
2523 tmpl
= DECL_TI_TEMPLATE (decl
);
2524 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2525 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2526 if (TREE_PURPOSE (t
))
2531 old_type
= TREE_TYPE (decl
);
2532 spec_types
= TYPE_ARG_TYPES (old_type
);
2534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2536 /* Remove the this pointer, but remember the object's type for
2538 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2539 spec_types
= TREE_CHAIN (spec_types
);
2540 tmpl_types
= TREE_CHAIN (tmpl_types
);
2542 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2544 /* DECL may contain more parameters than TMPL due to the extra
2545 in-charge parameter in constructors and destructors. */
2546 in_charge
= spec_types
;
2547 spec_types
= TREE_CHAIN (spec_types
);
2549 if (DECL_HAS_VTT_PARM_P (decl
))
2552 spec_types
= TREE_CHAIN (spec_types
);
2556 /* Compute the merged default arguments. */
2558 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2560 /* Compute the new FUNCTION_TYPE. */
2564 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2569 /* Put the in-charge parameter back. */
2570 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2571 TREE_VALUE (in_charge
),
2574 new_type
= build_method_type_directly (object_type
,
2575 TREE_TYPE (old_type
),
2579 new_type
= build_function_type (TREE_TYPE (old_type
),
2581 new_type
= cp_build_type_attribute_variant (new_type
,
2582 TYPE_ATTRIBUTES (old_type
));
2583 new_type
= cxx_copy_lang_qualifiers (new_type
, old_type
);
2585 TREE_TYPE (decl
) = new_type
;
2588 /* Return the number of template headers we expect to see for a definition
2589 or specialization of CTYPE or one of its non-template members. */
2592 num_template_headers_for_class (tree ctype
)
2594 int num_templates
= 0;
2596 while (ctype
&& CLASS_TYPE_P (ctype
))
2598 /* You're supposed to have one `template <...>' for every
2599 template class, but you don't need one for a full
2600 specialization. For example:
2602 template <class T> struct S{};
2603 template <> struct S<int> { void f(); };
2604 void S<int>::f () {}
2606 is correct; there shouldn't be a `template <>' for the
2607 definition of `S<int>::f'. */
2608 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2609 /* If CTYPE does not have template information of any
2610 kind, then it is not a template, nor is it nested
2611 within a template. */
2613 if (explicit_class_specialization_p (ctype
))
2615 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2618 ctype
= TYPE_CONTEXT (ctype
);
2621 return num_templates
;
2624 /* Do a simple sanity check on the template headers that precede the
2625 variable declaration DECL. */
2628 check_template_variable (tree decl
)
2630 tree ctx
= CP_DECL_CONTEXT (decl
);
2631 int wanted
= num_template_headers_for_class (ctx
);
2632 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2635 if (cxx_dialect
< cxx14
)
2636 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2637 "variable templates only available with "
2638 "%<-std=c++14%> or %<-std=gnu++14%>");
2640 // Namespace-scope variable templates should have a template header.
2643 if (template_header_count
> wanted
)
2645 auto_diagnostic_group d
;
2646 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2647 "too many template headers for %qD "
2650 if (warned
&& CLASS_TYPE_P (ctx
)
2651 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2652 inform (DECL_SOURCE_LOCATION (decl
),
2653 "members of an explicitly specialized class are defined "
2654 "without a template header");
2658 /* An explicit specialization whose declarator-id or class-head-name is not
2659 qualified shall be declared in the nearest enclosing namespace of the
2660 template, or, if the namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set.
2663 If the name declared in the explicit instantiation is an unqualified name,
2664 the explicit instantiation shall appear in the namespace where its template
2665 is declared or, if that namespace is inline (7.3.1), any namespace from its
2666 enclosing namespace set. */
2669 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2671 tree tmpl
= most_general_template (t
);
2672 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2673 && !is_nested_namespace (current_namespace
,
2674 CP_DECL_CONTEXT (tmpl
), true))
2676 if (processing_specialization
)
2677 permerror (loc
, "explicit specialization of %qD outside its "
2678 "namespace must use a nested-name-specifier", tmpl
);
2679 else if (processing_explicit_instantiation
2680 && cxx_dialect
>= cxx11
)
2681 /* This was allowed in C++98, so only pedwarn. */
2682 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2683 "outside its namespace must use a nested-name-"
2688 /* Warn for a template specialization SPEC that is missing some of a set
2689 of function or type attributes that the template TEMPL is declared with.
2690 ATTRLIST is a list of additional attributes that SPEC should be taken
2691 to ultimately be declared with. */
2694 warn_spec_missing_attributes (tree tmpl
, tree spec
, tree attrlist
)
2696 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
2697 tmpl
= DECL_TEMPLATE_RESULT (tmpl
);
2699 /* Avoid warning if the difference between the primary and
2700 the specialization is not in one of the attributes below. */
2701 const char* const blacklist
[] = {
2702 "alloc_align", "alloc_size", "assume_aligned", "format",
2703 "format_arg", "malloc", "nonnull", NULL
2706 /* Put together a list of the black listed attributes that the primary
2707 template is declared with that the specialization is not, in case
2708 it's not apparent from the most recent declaration of the primary. */
2710 unsigned nattrs
= decls_mismatched_attributes (tmpl
, spec
, attrlist
,
2716 auto_diagnostic_group d
;
2717 if (warning_at (DECL_SOURCE_LOCATION (spec
), OPT_Wmissing_attributes
,
2718 "explicit specialization %q#D may be missing attributes",
2720 inform (DECL_SOURCE_LOCATION (tmpl
),
2722 ? G_("missing primary template attributes %s")
2723 : G_("missing primary template attribute %s"),
2724 pp_formatted_text (&str
));
2727 /* Check to see if the function just declared, as indicated in
2728 DECLARATOR, and in DECL, is a specialization of a function
2729 template. We may also discover that the declaration is an explicit
2730 instantiation at this point.
2732 Returns DECL, or an equivalent declaration that should be used
2733 instead if all goes well. Issues an error message if something is
2734 amiss. Returns error_mark_node if the error is not easily
2737 FLAGS is a bitmask consisting of the following flags:
2739 2: The function has a definition.
2740 4: The function is a friend.
2742 The TEMPLATE_COUNT is the number of references to qualifying
2743 template classes that appeared in the name of the function. For
2746 template <class T> struct S { void f(); };
2749 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2750 classes are not counted in the TEMPLATE_COUNT, so that in
2752 template <class T> struct S {};
2753 template <> struct S<int> { void f(); }
2754 template <> void S<int>::f();
2756 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2757 invalid; there should be no template <>.)
2759 If the function is a specialization, it is marked as such via
2760 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2761 is set up correctly, and it is added to the list of specializations
2762 for that template. */
2765 check_explicit_specialization (tree declarator
,
2771 int have_def
= flags
& 2;
2772 int is_friend
= flags
& 4;
2773 bool is_concept
= flags
& 8;
2774 int specialization
= 0;
2775 int explicit_instantiation
= 0;
2776 int member_specialization
= 0;
2777 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2778 tree dname
= DECL_NAME (decl
);
2783 if (!processing_specialization
)
2786 tsk
= tsk_excessive_parms
;
2789 tsk
= current_tmpl_spec_kind (template_count
);
2794 if (processing_specialization
&& !VAR_P (decl
))
2797 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2799 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2802 /* This could be something like:
2804 template <class T> void f(T);
2805 class S { friend void f<>(int); } */
2809 /* This case handles bogus declarations like template <>
2810 template <class T> void f<int>(); */
2812 error ("template-id %qD in declaration of primary template",
2819 case tsk_invalid_member_spec
:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node
;
2824 case tsk_invalid_expl_inst
:
2825 error ("template parameter list used in explicit instantiation");
2831 error ("definition provided for explicit instantiation");
2833 explicit_instantiation
= 1;
2836 case tsk_excessive_parms
:
2837 case tsk_insufficient_parms
:
2838 if (tsk
== tsk_excessive_parms
)
2839 error ("too many template parameter lists in declaration of %qD",
2841 else if (template_header_count
)
2842 error("too few template parameter lists in declaration of %qD", decl
);
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl
);
2850 error ("explicit specialization declared %<concept%>");
2852 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2859 member_specialization
= 1;
2865 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2870 if (!uses_template_parms (TREE_OPERAND (declarator
, 1)))
2871 error ("template-id %qD in declaration of primary template",
2873 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2875 /* Partial specialization of variable template. */
2876 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2880 else if (cxx_dialect
< cxx14
)
2881 error ("non-type partial specialization %qD "
2882 "is not allowed", declarator
);
2884 error ("non-class, non-variable partial specialization %qD "
2885 "is not allowed", declarator
);
2890 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2891 /* This is a specialization of a member template, without
2892 specialization the containing class. Something like:
2894 template <class T> struct S {
2895 template <class U> void f (U);
2897 template <> template <class U> void S<int>::f(U) {}
2899 That's a specialization -- but of the entire template. */
2907 if ((specialization
|| member_specialization
)
2908 /* This doesn't apply to variable templates. */
2909 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2910 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2912 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2913 for (; t
; t
= TREE_CHAIN (t
))
2914 if (TREE_PURPOSE (t
))
2916 permerror (input_location
,
2917 "default argument specified in explicit specialization");
2922 if (specialization
|| member_specialization
|| explicit_instantiation
)
2924 tree tmpl
= NULL_TREE
;
2925 tree targs
= NULL_TREE
;
2926 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2928 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2929 if (!was_template_id
)
2933 gcc_assert (identifier_p (declarator
));
2938 /* If there is no class context, the explicit instantiation
2939 must be at namespace scope. */
2940 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2942 /* Find the namespace binding, using the declaration
2944 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2946 if (fns
== error_mark_node
)
2947 /* If lookup fails, look for a friend declaration so we can
2948 give a better diagnostic. */
2949 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2950 /*type*/false, /*complain*/true,
2953 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2955 error ("%qD is not a template function", dname
);
2956 fns
= error_mark_node
;
2960 declarator
= lookup_template_function (fns
, NULL_TREE
);
2963 if (declarator
== error_mark_node
)
2964 return error_mark_node
;
2966 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2968 if (!explicit_instantiation
)
2969 /* A specialization in class scope. This is invalid,
2970 but the error will already have been flagged by
2971 check_specialization_scope. */
2972 return error_mark_node
;
2975 /* It's not valid to write an explicit instantiation in
2978 class C { template void f(); }
2980 This case is caught by the parser. However, on
2983 template class C { void f(); };
2985 (which is invalid) we can get here. The error will be
2992 else if (ctype
!= NULL_TREE
2993 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2995 // We'll match variable templates in start_decl.
2999 /* Find the list of functions in ctype that have the same
3000 name as the declared function. */
3001 tree name
= TREE_OPERAND (declarator
, 0);
3003 if (constructor_name_p (name
, ctype
))
3005 if (DECL_CONSTRUCTOR_P (decl
)
3006 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
3007 : !CLASSTYPE_DESTRUCTOR (ctype
))
3009 /* From [temp.expl.spec]:
3011 If such an explicit specialization for the member
3012 of a class template names an implicitly-declared
3013 special member function (clause _special_), the
3014 program is ill-formed.
3016 Similar language is found in [temp.explicit]. */
3017 error ("specialization of implicitly-declared special member function");
3018 return error_mark_node
;
3021 name
= DECL_NAME (decl
);
3024 /* For a type-conversion operator, We might be looking for
3025 `operator int' which will be a specialization of
3026 `operator T'. Grab all the conversion operators, and
3027 then select from them. */
3028 tree fns
= get_class_binding (ctype
, IDENTIFIER_CONV_OP_P (name
)
3029 ? conv_op_identifier
: name
);
3031 if (fns
== NULL_TREE
)
3033 error ("no member function %qD declared in %qT", name
, ctype
);
3034 return error_mark_node
;
3037 TREE_OPERAND (declarator
, 0) = fns
;
3040 /* Figure out what exactly is being specialized at this point.
3041 Note that for an explicit instantiation, even one for a
3042 member function, we cannot tell a priori whether the
3043 instantiation is for a member template, or just a member
3044 function of a template class. Even if a member template is
3045 being instantiated, the member template arguments may be
3046 elided if they can be deduced from the rest of the
3048 tmpl
= determine_specialization (declarator
, decl
,
3050 member_specialization
,
3054 if (!tmpl
|| tmpl
== error_mark_node
)
3055 /* We couldn't figure out what this declaration was
3057 return error_mark_node
;
3060 if (TREE_CODE (decl
) == FUNCTION_DECL
3061 && DECL_HIDDEN_FRIEND_P (tmpl
))
3063 auto_diagnostic_group d
;
3064 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3065 "friend declaration %qD is not visible to "
3066 "explicit specialization", tmpl
))
3067 inform (DECL_SOURCE_LOCATION (tmpl
),
3068 "friend declaration here");
3070 else if (!ctype
&& !is_friend
3071 && CP_DECL_CONTEXT (decl
) == current_namespace
)
3072 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
3074 tree gen_tmpl
= most_general_template (tmpl
);
3076 if (explicit_instantiation
)
3078 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3079 is done by do_decl_instantiation later. */
3081 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3082 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3084 if (arg_depth
> parm_depth
)
3086 /* If TMPL is not the most general template (for
3087 example, if TMPL is a friend template that is
3088 injected into namespace scope), then there will
3089 be too many levels of TARGS. Remove some of them
3094 new_targs
= make_tree_vec (parm_depth
);
3095 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3096 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3097 = TREE_VEC_ELT (targs
, i
);
3101 return instantiate_template (tmpl
, targs
, tf_error
);
3104 /* If we thought that the DECL was a member function, but it
3105 turns out to be specializing a static member function,
3106 make DECL a static member function as well. */
3107 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3108 && DECL_STATIC_FUNCTION_P (tmpl
)
3109 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3110 revert_static_member_fn (decl
);
3112 /* If this is a specialization of a member template of a
3113 template class, we want to return the TEMPLATE_DECL, not
3114 the specialization of it. */
3115 if (tsk
== tsk_template
&& !was_template_id
)
3117 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3118 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3119 DECL_INITIAL (result
) = NULL_TREE
;
3123 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3124 DECL_SOURCE_LOCATION (result
)
3125 = DECL_SOURCE_LOCATION (decl
);
3126 /* We want to use the argument list specified in the
3127 definition, not in the original declaration. */
3128 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3129 for (parm
= DECL_ARGUMENTS (result
); parm
;
3130 parm
= DECL_CHAIN (parm
))
3131 DECL_CONTEXT (parm
) = result
;
3133 return register_specialization (tmpl
, gen_tmpl
, targs
,
3137 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3138 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3140 if (was_template_id
)
3141 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3143 /* Inherit default function arguments from the template
3144 DECL is specializing. */
3145 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3146 copy_default_args_to_explicit_spec (decl
);
3148 /* This specialization has the same protection as the
3149 template it specializes. */
3150 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3151 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3153 /* 7.1.1-1 [dcl.stc]
3155 A storage-class-specifier shall not be specified in an
3156 explicit specialization...
3158 The parser rejects these, so unless action is taken here,
3159 explicit function specializations will always appear with
3162 The action recommended by the C++ CWG in response to C++
3163 defect report 605 is to make the storage class and linkage
3164 of the explicit specialization match the templated function:
3166 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3168 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3170 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3171 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3173 /* A concept cannot be specialized. */
3174 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3176 error ("explicit specialization of function concept %qD",
3178 return error_mark_node
;
3181 /* This specialization has the same linkage and visibility as
3182 the function template it specializes. */
3183 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3184 if (! TREE_PUBLIC (decl
))
3186 DECL_INTERFACE_KNOWN (decl
) = 1;
3187 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3189 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3190 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3192 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3193 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3197 /* If DECL is a friend declaration, declared using an
3198 unqualified name, the namespace associated with DECL may
3199 have been set incorrectly. For example, in:
3201 template <typename T> void f(T);
3203 struct S { friend void f<int>(int); }
3206 we will have set the DECL_CONTEXT for the friend
3207 declaration to N, rather than to the global namespace. */
3208 if (DECL_NAMESPACE_SCOPE_P (decl
))
3209 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3211 if (is_friend
&& !have_def
)
3212 /* This is not really a declaration of a specialization.
3213 It's just the name of an instantiation. But, it's not
3214 a request for an instantiation, either. */
3215 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3216 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3217 /* A specialization is not necessarily COMDAT. */
3218 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3219 && DECL_DECLARED_INLINE_P (decl
));
3220 else if (VAR_P (decl
))
3221 DECL_COMDAT (decl
) = false;
3223 /* If this is a full specialization, register it so that we can find
3224 it again. Partial specializations will be registered in
3225 process_partial_specialization. */
3226 if (!processing_template_decl
)
3228 warn_spec_missing_attributes (gen_tmpl
, decl
, attrlist
);
3230 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3235 /* A 'structor should already have clones. */
3236 gcc_assert (decl
== error_mark_node
3237 || variable_template_p (tmpl
)
3238 || !(DECL_CONSTRUCTOR_P (decl
)
3239 || DECL_DESTRUCTOR_P (decl
))
3240 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3247 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3248 parameters. These are represented in the same format used for
3249 DECL_TEMPLATE_PARMS. */
3252 comp_template_parms (const_tree parms1
, const_tree parms2
)
3257 if (parms1
== parms2
)
3260 for (p1
= parms1
, p2
= parms2
;
3261 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3262 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3264 tree t1
= TREE_VALUE (p1
);
3265 tree t2
= TREE_VALUE (p2
);
3268 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3269 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3271 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3274 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3276 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3277 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3279 /* If either of the template parameters are invalid, assume
3280 they match for the sake of error recovery. */
3281 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3284 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3287 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3288 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3289 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3291 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3296 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3297 /* One set of parameters has more parameters lists than the
3304 /* Determine whether PARM is a parameter pack. */
3307 template_parameter_pack_p (const_tree parm
)
3309 /* Determine if we have a non-type template parameter pack. */
3310 if (TREE_CODE (parm
) == PARM_DECL
)
3311 return (DECL_TEMPLATE_PARM_P (parm
)
3312 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3313 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3314 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3316 /* If this is a list of template parameters, we could get a
3317 TYPE_DECL or a TEMPLATE_DECL. */
3318 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3319 parm
= TREE_TYPE (parm
);
3321 /* Otherwise it must be a type template parameter. */
3322 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3323 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3324 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3327 /* Determine if T is a function parameter pack. */
3330 function_parameter_pack_p (const_tree t
)
3332 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3333 return DECL_PACK_P (t
);
3337 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3338 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3341 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3343 if (! primary_func_tmpl_inst
3344 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3345 || ! primary_template_specialization_p (primary_func_tmpl_inst
))
3348 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3351 /* Return true iff the function parameter PARAM_DECL was expanded
3352 from the function parameter pack PACK. */
3355 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3357 if (DECL_ARTIFICIAL (param_decl
)
3358 || !function_parameter_pack_p (pack
))
3361 /* The parameter pack and its pack arguments have the same
3363 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3366 /* Determine whether ARGS describes a variadic template args list,
3367 i.e., one that is terminated by a template argument pack. */
3370 template_args_variadic_p (tree args
)
3375 if (args
== NULL_TREE
)
3378 args
= INNERMOST_TEMPLATE_ARGS (args
);
3379 nargs
= TREE_VEC_LENGTH (args
);
3384 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3386 return ARGUMENT_PACK_P (last_parm
);
3389 /* Generate a new name for the parameter pack name NAME (an
3390 IDENTIFIER_NODE) that incorporates its */
3393 make_ith_pack_parameter_name (tree name
, int i
)
3395 /* Munge the name to include the parameter index. */
3396 #define NUMBUF_LEN 128
3397 char numbuf
[NUMBUF_LEN
];
3401 if (name
== NULL_TREE
)
3403 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3404 newname_len
= IDENTIFIER_LENGTH (name
)
3405 + strlen (numbuf
) + 2;
3406 newname
= (char*)alloca (newname_len
);
3407 snprintf (newname
, newname_len
,
3408 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3409 return get_identifier (newname
);
3412 /* Return true if T is a primary function, class or alias template
3413 specialization, not including the template pattern. */
3416 primary_template_specialization_p (const_tree t
)
3421 if (TREE_CODE (t
) == FUNCTION_DECL
|| VAR_P (t
))
3422 return (DECL_LANG_SPECIFIC (t
)
3423 && DECL_USE_TEMPLATE (t
)
3424 && DECL_TEMPLATE_INFO (t
)
3425 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
)));
3426 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3427 return (CLASSTYPE_TEMPLATE_INFO (t
)
3428 && CLASSTYPE_USE_TEMPLATE (t
)
3429 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)));
3430 else if (alias_template_specialization_p (t
))
3435 /* Return true if PARM is a template template parameter. */
3438 template_template_parameter_p (const_tree parm
)
3440 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3443 /* Return true iff PARM is a DECL representing a type template
3447 template_type_parameter_p (const_tree parm
)
3450 && (TREE_CODE (parm
) == TYPE_DECL
3451 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3452 && DECL_TEMPLATE_PARM_P (parm
));
3455 /* Return the template parameters of T if T is a
3456 primary template instantiation, NULL otherwise. */
3459 get_primary_template_innermost_parameters (const_tree t
)
3461 tree parms
= NULL
, template_info
= NULL
;
3463 if ((template_info
= get_template_info (t
))
3464 && primary_template_specialization_p (t
))
3465 parms
= INNERMOST_TEMPLATE_PARMS
3466 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3471 /* Return the template parameters of the LEVELth level from the full list
3472 of template parameters PARMS. */
3475 get_template_parms_at_level (tree parms
, int level
)
3479 || TREE_CODE (parms
) != TREE_LIST
3480 || level
> TMPL_PARMS_DEPTH (parms
))
3483 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3484 if (TMPL_PARMS_DEPTH (p
) == level
)
3490 /* Returns the template arguments of T if T is a template instantiation,
3494 get_template_innermost_arguments (const_tree t
)
3496 tree args
= NULL
, template_info
= NULL
;
3498 if ((template_info
= get_template_info (t
))
3499 && TI_ARGS (template_info
))
3500 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3505 /* Return the argument pack elements of T if T is a template argument pack,
3509 get_template_argument_pack_elems (const_tree t
)
3511 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3512 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3515 return ARGUMENT_PACK_ARGS (t
);
3518 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3519 ARGUMENT_PACK_SELECT represents. */
3522 argument_pack_select_arg (tree t
)
3524 tree args
= ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t
));
3525 tree arg
= TREE_VEC_ELT (args
, ARGUMENT_PACK_SELECT_INDEX (t
));
3527 /* If the selected argument is an expansion E, that most likely means we were
3528 called from gen_elem_of_pack_expansion_instantiation during the
3529 substituting of an argument pack (of which the Ith element is a pack
3530 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3531 In this case, the Ith element resulting from this substituting is going to
3532 be a pack expansion, which pattern is the pattern of E. Let's return the
3533 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3534 resulting pack expansion from it. */
3535 if (PACK_EXPANSION_P (arg
))
3537 /* Make sure we aren't throwing away arg info. */
3538 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
3539 arg
= PACK_EXPANSION_PATTERN (arg
);
3546 /* True iff FN is a function representing a built-in variadic parameter
3550 builtin_pack_fn_p (tree fn
)
3553 || TREE_CODE (fn
) != FUNCTION_DECL
3554 || !DECL_IS_BUILTIN (fn
))
3557 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3563 /* True iff CALL is a call to a function representing a built-in variadic
3567 builtin_pack_call_p (tree call
)
3569 if (TREE_CODE (call
) != CALL_EXPR
)
3571 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3574 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3577 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3580 tree ohi
= CALL_EXPR_ARG (call
, 0);
3581 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3582 false/*fn*/, true/*int_cst*/);
3584 if (value_dependent_expression_p (hi
))
3588 call
= copy_node (call
);
3589 CALL_EXPR_ARG (call
, 0) = hi
;
3591 tree ex
= make_pack_expansion (call
, complain
);
3592 tree vec
= make_tree_vec (1);
3593 TREE_VEC_ELT (vec
, 0) = ex
;
3598 hi
= cxx_constant_value (hi
);
3599 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3601 /* Calculate the largest value of len that won't make the size of the vec
3602 overflow an int. The compiler will exceed resource limits long before
3603 this, but it seems a decent place to diagnose. */
3604 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3606 if (len
< 0 || len
> max
)
3608 if ((complain
& tf_error
)
3609 && hi
!= error_mark_node
)
3610 error ("argument to __integer_pack must be between 0 and %d", max
);
3611 return error_mark_node
;
3614 tree vec
= make_tree_vec (len
);
3616 for (int i
= 0; i
< len
; ++i
)
3617 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3623 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3627 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3630 if (!builtin_pack_call_p (call
))
3633 tree fn
= CALL_EXPR_FN (call
);
3635 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3636 return expand_integer_pack (call
, args
, complain
, in_decl
);
3641 /* Structure used to track the progress of find_parameter_packs_r. */
3642 struct find_parameter_pack_data
3644 /* TREE_LIST that will contain all of the parameter packs found by
3646 tree
* parameter_packs
;
3648 /* Set of AST nodes that have been visited by the traversal. */
3649 hash_set
<tree
> *visited
;
3651 /* True iff we're making a type pack expansion. */
3652 bool type_pack_expansion_p
;
3655 /* Identifies all of the argument packs that occur in a template
3656 argument and appends them to the TREE_LIST inside DATA, which is a
3657 find_parameter_pack_data structure. This is a subroutine of
3658 make_pack_expansion and uses_parameter_packs. */
3660 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3663 struct find_parameter_pack_data
* ppd
=
3664 (struct find_parameter_pack_data
*)data
;
3665 bool parameter_pack_p
= false;
3667 /* Handle type aliases/typedefs. */
3668 if (TYPE_ALIAS_P (t
))
3670 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3671 cp_walk_tree (&TI_ARGS (tinfo
),
3672 &find_parameter_packs_r
,
3678 /* Identify whether this is a parameter pack or not. */
3679 switch (TREE_CODE (t
))
3681 case TEMPLATE_PARM_INDEX
:
3682 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3683 parameter_pack_p
= true;
3686 case TEMPLATE_TYPE_PARM
:
3687 t
= TYPE_MAIN_VARIANT (t
);
3689 case TEMPLATE_TEMPLATE_PARM
:
3690 /* If the placeholder appears in the decl-specifier-seq of a function
3691 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3692 is a pack expansion, the invented template parameter is a template
3694 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3695 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3696 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3697 parameter_pack_p
= true;
3702 if (DECL_PACK_P (t
))
3704 /* We don't want to walk into the type of a PARM_DECL,
3705 because we don't want to see the type parameter pack. */
3707 parameter_pack_p
= true;
3712 if (DECL_PACK_P (t
))
3714 /* We don't want to walk into the type of a variadic capture proxy,
3715 because we don't want to see the type parameter pack. */
3717 parameter_pack_p
= true;
3719 else if (variable_template_specialization_p (t
))
3721 cp_walk_tree (&DECL_TI_ARGS (t
),
3722 find_parameter_packs_r
,
3729 if (builtin_pack_call_p (t
))
3730 parameter_pack_p
= true;
3734 parameter_pack_p
= true;
3737 /* Not a parameter pack. */
3741 if (parameter_pack_p
)
3743 /* Add this parameter pack to the list. */
3744 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3748 cp_walk_tree (&TYPE_CONTEXT (t
),
3749 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3751 /* This switch statement will return immediately if we don't find a
3753 switch (TREE_CODE (t
))
3755 case TEMPLATE_PARM_INDEX
:
3758 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3759 /* Check the template itself. */
3760 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3761 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3762 /* Check the template arguments. */
3763 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3768 case TEMPLATE_TYPE_PARM
:
3769 case TEMPLATE_TEMPLATE_PARM
:
3776 /* Ignore the declaration of a capture proxy for a parameter pack. */
3777 if (is_capture_proxy (DECL_EXPR_DECL (t
)))
3782 if (TYPE_PTRMEMFUNC_P (t
))
3788 if (TYPE_TEMPLATE_INFO (t
))
3789 cp_walk_tree (&TYPE_TI_ARGS (t
),
3790 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3796 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3801 cp_walk_tree (&TREE_TYPE (t
),
3802 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3806 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3811 case TYPE_PACK_EXPANSION
:
3812 case EXPR_PACK_EXPANSION
:
3817 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3822 case IDENTIFIER_NODE
:
3823 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3830 /* Look at explicit captures. */
3831 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
);
3832 cap
; cap
= TREE_CHAIN (cap
))
3833 cp_walk_tree (&TREE_VALUE (cap
), &find_parameter_packs_r
, ppd
,
3835 /* Since we defer implicit capture, look in the parms and body. */
3836 tree fn
= lambda_function (t
);
3837 cp_walk_tree (&TREE_TYPE (fn
), &find_parameter_packs_r
, ppd
,
3839 cp_walk_tree (&DECL_SAVED_TREE (fn
), &find_parameter_packs_r
, ppd
,
3847 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3848 type_pack_expansion_p to false so that any placeholders
3849 within the expression don't get marked as parameter packs. */
3850 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3851 ppd
->type_pack_expansion_p
= false;
3852 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3854 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3860 cp_walk_tree (&IF_COND (t
), &find_parameter_packs_r
,
3862 cp_walk_tree (&THEN_CLAUSE (t
), &find_parameter_packs_r
,
3864 cp_walk_tree (&ELSE_CLAUSE (t
), &find_parameter_packs_r
,
3866 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3877 /* Determines if the expression or type T uses any parameter packs. */
3879 uses_parameter_packs (tree t
)
3881 tree parameter_packs
= NULL_TREE
;
3882 struct find_parameter_pack_data ppd
;
3883 ppd
.parameter_packs
= ¶meter_packs
;
3884 ppd
.visited
= new hash_set
<tree
>;
3885 ppd
.type_pack_expansion_p
= false;
3886 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3888 return parameter_packs
!= NULL_TREE
;
3891 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3892 representation a base-class initializer into a parameter pack
3893 expansion. If all goes well, the resulting node will be an
3894 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3897 make_pack_expansion (tree arg
, tsubst_flags_t complain
)
3900 tree parameter_packs
= NULL_TREE
;
3901 bool for_types
= false;
3902 struct find_parameter_pack_data ppd
;
3904 if (!arg
|| arg
== error_mark_node
)
3907 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3909 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3910 class initializer. In this case, the TREE_PURPOSE will be a
3911 _TYPE node (representing the base class expansion we're
3912 initializing) and the TREE_VALUE will be a TREE_LIST
3913 containing the initialization arguments.
3915 The resulting expansion looks somewhat different from most
3916 expansions. Rather than returning just one _EXPANSION, we
3917 return a TREE_LIST whose TREE_PURPOSE is a
3918 TYPE_PACK_EXPANSION containing the bases that will be
3919 initialized. The TREE_VALUE will be identical to the
3920 original TREE_VALUE, which is a list of arguments that will
3921 be passed to each base. We do not introduce any new pack
3922 expansion nodes into the TREE_VALUE (although it is possible
3923 that some already exist), because the TREE_PURPOSE and
3924 TREE_VALUE all need to be expanded together with the same
3925 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3926 resulting TREE_PURPOSE will mention the parameter packs in
3927 both the bases and the arguments to the bases. */
3930 tree parameter_packs
= NULL_TREE
;
3932 /* Determine which parameter packs will be used by the base
3934 ppd
.visited
= new hash_set
<tree
>;
3935 ppd
.parameter_packs
= ¶meter_packs
;
3936 ppd
.type_pack_expansion_p
= false;
3937 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3938 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3941 if (parameter_packs
== NULL_TREE
)
3943 if (complain
& tf_error
)
3944 error ("base initializer expansion %qT contains no parameter packs",
3947 return error_mark_node
;
3950 if (TREE_VALUE (arg
) != void_type_node
)
3952 /* Collect the sets of parameter packs used in each of the
3953 initialization arguments. */
3954 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3956 /* Determine which parameter packs will be expanded in this
3958 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3965 /* Create the pack expansion type for the base type. */
3966 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3967 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3968 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3969 PACK_EXPANSION_LOCAL_P (purpose
) = at_function_scope_p ();
3971 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3972 they will rarely be compared to anything. */
3973 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3975 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3978 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3981 /* Build the PACK_EXPANSION_* node. */
3983 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3984 : make_node (EXPR_PACK_EXPANSION
);
3985 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3986 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3988 /* Propagate type and const-expression information. */
3989 TREE_TYPE (result
) = TREE_TYPE (arg
);
3990 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3991 /* Mark this read now, since the expansion might be length 0. */
3992 mark_exp_read (arg
);
3995 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3996 they will rarely be compared to anything. */
3997 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3999 /* Determine which parameter packs will be expanded. */
4000 ppd
.parameter_packs
= ¶meter_packs
;
4001 ppd
.visited
= new hash_set
<tree
>;
4002 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
4003 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4006 /* Make sure we found some parameter packs. */
4007 if (parameter_packs
== NULL_TREE
)
4009 if (complain
& tf_error
)
4012 error ("expansion pattern %qT contains no parameter packs", arg
);
4014 error ("expansion pattern %qE contains no parameter packs", arg
);
4016 return error_mark_node
;
4018 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
4020 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
4025 /* Checks T for any "bare" parameter packs, which have not yet been
4026 expanded, and issues an error if any are found. This operation can
4027 only be done on full expressions or types (e.g., an expression
4028 statement, "if" condition, etc.), because we could have expressions like:
4030 foo(f(g(h(args)))...)
4032 where "args" is a parameter pack. check_for_bare_parameter_packs
4033 should not be called for the subexpressions args, h(args),
4034 g(h(args)), or f(g(h(args))), because we would produce erroneous
4037 Returns TRUE and emits an error if there were bare parameter packs,
4038 returns FALSE otherwise. */
4040 check_for_bare_parameter_packs (tree t
, location_t loc
/* = UNKNOWN_LOCATION */)
4042 tree parameter_packs
= NULL_TREE
;
4043 struct find_parameter_pack_data ppd
;
4045 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
4048 /* A lambda might use a parameter pack from the containing context. */
4049 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)
4050 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
4053 if (TREE_CODE (t
) == TYPE_DECL
)
4056 ppd
.parameter_packs
= ¶meter_packs
;
4057 ppd
.visited
= new hash_set
<tree
>;
4058 ppd
.type_pack_expansion_p
= false;
4059 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
4062 if (parameter_packs
)
4064 if (loc
== UNKNOWN_LOCATION
)
4065 loc
= cp_expr_loc_or_loc (t
, input_location
);
4066 error_at (loc
, "parameter packs not expanded with %<...%>:");
4067 while (parameter_packs
)
4069 tree pack
= TREE_VALUE (parameter_packs
);
4070 tree name
= NULL_TREE
;
4072 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
4073 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
4074 name
= TYPE_NAME (pack
);
4075 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
4076 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
4077 else if (TREE_CODE (pack
) == CALL_EXPR
)
4078 name
= DECL_NAME (CALL_EXPR_FN (pack
));
4080 name
= DECL_NAME (pack
);
4083 inform (loc
, " %qD", name
);
4085 inform (loc
, " <anonymous>");
4087 parameter_packs
= TREE_CHAIN (parameter_packs
);
4096 /* Expand any parameter packs that occur in the template arguments in
4099 expand_template_argument_pack (tree args
)
4101 if (args
== error_mark_node
)
4102 return error_mark_node
;
4104 tree result_args
= NULL_TREE
;
4105 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
4106 int num_result_args
= -1;
4107 int non_default_args_count
= -1;
4109 /* First, determine if we need to expand anything, and the number of
4110 slots we'll need. */
4111 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4113 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4114 if (arg
== NULL_TREE
)
4116 if (ARGUMENT_PACK_P (arg
))
4118 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
4119 if (num_result_args
< 0)
4120 num_result_args
= in_arg
+ num_packed
;
4122 num_result_args
+= num_packed
;
4126 if (num_result_args
>= 0)
4131 /* If no expansion is necessary, we're done. */
4132 if (num_result_args
< 0)
4135 /* Expand arguments. */
4136 result_args
= make_tree_vec (num_result_args
);
4137 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
4138 non_default_args_count
=
4139 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
4140 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
4142 tree arg
= TREE_VEC_ELT (args
, in_arg
);
4143 if (ARGUMENT_PACK_P (arg
))
4145 tree packed
= ARGUMENT_PACK_ARGS (arg
);
4146 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
4147 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
4148 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
4149 if (non_default_args_count
> 0)
4150 non_default_args_count
+= num_packed
- 1;
4154 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
4158 if (non_default_args_count
>= 0)
4159 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
4163 /* Checks if DECL shadows a template parameter.
4165 [temp.local]: A template-parameter shall not be redeclared within its
4166 scope (including nested scopes).
4168 Emits an error and returns TRUE if the DECL shadows a parameter,
4169 returns FALSE otherwise. */
4172 check_template_shadow (tree decl
)
4176 /* If we're not in a template, we can't possibly shadow a template
4178 if (!current_template_parms
)
4181 /* Figure out what we're shadowing. */
4182 decl
= OVL_FIRST (decl
);
4183 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4185 /* If there's no previous binding for this name, we're not shadowing
4186 anything, let alone a template parameter. */
4190 /* If we're not shadowing a template parameter, we're done. Note
4191 that OLDDECL might be an OVERLOAD (or perhaps even an
4192 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4194 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4197 /* We check for decl != olddecl to avoid bogus errors for using a
4198 name inside a class. We check TPFI to avoid duplicate errors for
4199 inline member templates. */
4201 || (DECL_TEMPLATE_PARM_P (decl
)
4202 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4205 /* Don't complain about the injected class name, as we've already
4206 complained about the class itself. */
4207 if (DECL_SELF_REFERENCE_P (decl
))
4210 if (DECL_TEMPLATE_PARM_P (decl
))
4211 error ("declaration of template parameter %q+D shadows "
4212 "template parameter", decl
);
4214 error ("declaration of %q+#D shadows template parameter", decl
);
4215 inform (DECL_SOURCE_LOCATION (olddecl
),
4216 "template parameter %qD declared here", olddecl
);
4220 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4221 ORIG_LEVEL, DECL, and TYPE. */
4224 build_template_parm_index (int index
,
4230 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4231 TEMPLATE_PARM_IDX (t
) = index
;
4232 TEMPLATE_PARM_LEVEL (t
) = level
;
4233 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4234 TEMPLATE_PARM_DECL (t
) = decl
;
4235 TREE_TYPE (t
) = type
;
4236 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4237 TREE_READONLY (t
) = TREE_READONLY (decl
);
4242 /* Find the canonical type parameter for the given template type
4243 parameter. Returns the canonical type parameter, which may be TYPE
4244 if no such parameter existed. */
4247 canonical_type_parameter (tree type
)
4250 int idx
= TEMPLATE_TYPE_IDX (type
);
4251 if (!canonical_template_parms
)
4252 vec_alloc (canonical_template_parms
, idx
+ 1);
4254 if (canonical_template_parms
->length () <= (unsigned) idx
)
4255 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4257 list
= (*canonical_template_parms
)[idx
];
4258 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4259 list
= TREE_CHAIN (list
);
4262 return TREE_VALUE (list
);
4265 (*canonical_template_parms
)[idx
]
4266 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4271 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4272 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4273 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4274 new one is created. */
4277 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4278 tsubst_flags_t complain
)
4280 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4281 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4282 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4283 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4285 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4288 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4289 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4290 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4291 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4292 DECL_ARTIFICIAL (decl
) = 1;
4293 SET_DECL_TEMPLATE_PARM_P (decl
);
4295 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4296 TEMPLATE_PARM_LEVEL (index
) - levels
,
4297 TEMPLATE_PARM_ORIG_LEVEL (index
),
4299 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4300 TEMPLATE_PARM_PARAMETER_PACK (t
)
4301 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4303 /* Template template parameters need this. */
4304 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4306 DECL_TEMPLATE_RESULT (decl
)
4307 = build_decl (DECL_SOURCE_LOCATION (decl
),
4308 TYPE_DECL
, DECL_NAME (decl
), type
);
4309 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4310 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4311 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4315 return TEMPLATE_PARM_DESCENDANTS (index
);
4318 /* Process information from new template parameter PARM and append it
4319 to the LIST being built. This new parameter is a non-type
4320 parameter iff IS_NON_TYPE is true. This new parameter is a
4321 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4325 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4326 bool is_non_type
, bool is_parameter_pack
)
4331 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4332 tree defval
= TREE_PURPOSE (parm
);
4333 tree constr
= TREE_TYPE (parm
);
4337 tree p
= tree_last (list
);
4339 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4342 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4343 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4345 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4353 parm
= TREE_VALUE (parm
);
4355 SET_DECL_TEMPLATE_PARM_P (parm
);
4357 if (TREE_TYPE (parm
) != error_mark_node
)
4361 The top-level cv-qualifiers on the template-parameter are
4362 ignored when determining its type. */
4363 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4364 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4365 TREE_TYPE (parm
) = error_mark_node
;
4366 else if (uses_parameter_packs (TREE_TYPE (parm
))
4367 && !is_parameter_pack
4368 /* If we're in a nested template parameter list, the template
4369 template parameter could be a parameter pack. */
4370 && processing_template_parmlist
== 1)
4372 /* This template parameter is not a parameter pack, but it
4373 should be. Complain about "bare" parameter packs. */
4374 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4376 /* Recover by calling this a parameter pack. */
4377 is_parameter_pack
= true;
4381 /* A template parameter is not modifiable. */
4382 TREE_CONSTANT (parm
) = 1;
4383 TREE_READONLY (parm
) = 1;
4384 decl
= build_decl (parm_loc
,
4385 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4386 TREE_CONSTANT (decl
) = 1;
4387 TREE_READONLY (decl
) = 1;
4388 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4389 = build_template_parm_index (idx
, processing_template_decl
,
4390 processing_template_decl
,
4391 decl
, TREE_TYPE (parm
));
4393 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4394 = is_parameter_pack
;
4399 parm
= TREE_VALUE (TREE_VALUE (parm
));
4401 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4403 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4404 /* This is for distinguishing between real templates and template
4405 template parameters */
4406 TREE_TYPE (parm
) = t
;
4407 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4412 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4413 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4414 decl
= build_decl (parm_loc
,
4415 TYPE_DECL
, parm
, t
);
4418 TYPE_NAME (t
) = decl
;
4419 TYPE_STUB_DECL (t
) = decl
;
4421 TEMPLATE_TYPE_PARM_INDEX (t
)
4422 = build_template_parm_index (idx
, processing_template_decl
,
4423 processing_template_decl
,
4424 decl
, TREE_TYPE (parm
));
4425 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4426 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4428 DECL_ARTIFICIAL (decl
) = 1;
4429 SET_DECL_TEMPLATE_PARM_P (decl
);
4431 /* Build requirements for the type/template parameter.
4432 This must be done after SET_DECL_TEMPLATE_PARM_P or
4433 process_template_parm could fail. */
4434 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4438 /* Build the parameter node linking the parameter declaration,
4439 its default argument (if any), and its constraints (if any). */
4440 parm
= build_tree_list (defval
, parm
);
4441 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4443 return chainon (list
, parm
);
4446 /* The end of a template parameter list has been reached. Process the
4447 tree list into a parameter vector, converting each parameter into a more
4448 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4452 end_template_parm_list (tree parms
)
4456 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4458 /* Pop the dummy parameter level and add the real one. */
4459 current_template_parms
= TREE_CHAIN (current_template_parms
);
4461 current_template_parms
4462 = tree_cons (size_int (processing_template_decl
),
4463 saved_parmlist
, current_template_parms
);
4465 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4467 next
= TREE_CHAIN (parm
);
4468 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4469 TREE_CHAIN (parm
) = NULL_TREE
;
4472 --processing_template_parmlist
;
4474 return saved_parmlist
;
4477 // Explicitly indicate the end of the template parameter list. We assume
4478 // that the current template parameters have been constructed and/or
4479 // managed explicitly, as when creating new template template parameters
4480 // from a shorthand constraint.
4482 end_template_parm_list ()
4484 --processing_template_parmlist
;
4487 /* end_template_decl is called after a template declaration is seen. */
4490 end_template_decl (void)
4492 reset_specialization ();
4494 if (! processing_template_decl
)
4497 /* This matches the pushlevel in begin_template_parm_list. */
4500 --processing_template_decl
;
4501 current_template_parms
= TREE_CHAIN (current_template_parms
);
4504 /* Takes a TREE_LIST representing a template parameter and convert it
4505 into an argument suitable to be passed to the type substitution
4506 functions. Note that If the TREE_LIST contains an error_mark
4507 node, the returned argument is error_mark_node. */
4510 template_parm_to_arg (tree t
)
4514 || TREE_CODE (t
) != TREE_LIST
)
4517 if (error_operand_p (TREE_VALUE (t
)))
4518 return error_mark_node
;
4522 if (TREE_CODE (t
) == TYPE_DECL
4523 || TREE_CODE (t
) == TEMPLATE_DECL
)
4527 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4529 /* Turn this argument into a TYPE_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 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4537 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4538 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4543 t
= DECL_INITIAL (t
);
4545 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4547 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4548 with a single element, which expands T. */
4549 tree vec
= make_tree_vec (1);
4551 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4553 t
= convert_from_reference (t
);
4554 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4556 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4557 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4560 t
= convert_from_reference (t
);
4565 /* Given a single level of template parameters (a TREE_VEC), return it
4566 as a set of template arguments. */
4569 template_parms_level_to_args (tree parms
)
4571 tree a
= copy_node (parms
);
4572 TREE_TYPE (a
) = NULL_TREE
;
4573 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4574 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4577 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4582 /* Given a set of template parameters, return them as a set of template
4583 arguments. The template parameters are represented as a TREE_VEC, in
4584 the form documented in cp-tree.h for template arguments. */
4587 template_parms_to_args (tree parms
)
4590 tree args
= NULL_TREE
;
4591 int length
= TMPL_PARMS_DEPTH (parms
);
4594 /* If there is only one level of template parameters, we do not
4595 create a TREE_VEC of TREE_VECs. Instead, we return a single
4596 TREE_VEC containing the arguments. */
4598 args
= make_tree_vec (length
);
4600 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4602 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4605 TREE_VEC_ELT (args
, --l
) = a
;
4613 /* Within the declaration of a template, return the currently active
4614 template parameters as an argument TREE_VEC. */
4617 current_template_args (void)
4619 return template_parms_to_args (current_template_parms
);
4622 /* Update the declared TYPE by doing any lookups which were thought to be
4623 dependent, but are not now that we know the SCOPE of the declarator. */
4626 maybe_update_decl_type (tree orig_type
, tree scope
)
4628 tree type
= orig_type
;
4630 if (type
== NULL_TREE
)
4633 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4634 type
= TREE_TYPE (type
);
4636 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4637 && dependent_type_p (type
)
4638 /* Don't bother building up the args in this case. */
4639 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4641 /* tsubst in the args corresponding to the template parameters,
4642 including auto if present. Most things will be unchanged, but
4643 make_typename_type and tsubst_qualified_id will resolve
4644 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4645 tree args
= current_template_args ();
4646 tree auto_node
= type_uses_auto (type
);
4650 tree auto_vec
= make_tree_vec (1);
4651 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4652 args
= add_to_template_args (args
, auto_vec
);
4654 pushed
= push_scope (scope
);
4655 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4660 if (type
== error_mark_node
)
4663 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4665 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4668 type
= TYPE_NAME (type
);
4673 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4674 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4675 the new template is a member template. */
4678 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4680 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4681 SET_DECL_LANGUAGE (tmpl
, DECL_LANGUAGE (decl
));
4682 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4683 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4684 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4685 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4690 struct template_parm_data
4692 /* The level of the template parameters we are currently
4696 /* The index of the specialization argument we are currently
4700 /* An array whose size is the number of template parameters. The
4701 elements are nonzero if the parameter has been used in any one
4702 of the arguments processed so far. */
4705 /* An array whose size is the number of template arguments. The
4706 elements are nonzero if the argument makes use of template
4707 parameters of this level. */
4708 int* arg_uses_template_parms
;
4711 /* Subroutine of push_template_decl used to see if each template
4712 parameter in a partial specialization is used in the explicit
4713 argument list. If T is of the LEVEL given in DATA (which is
4714 treated as a template_parm_data*), then DATA->PARMS is marked
4718 mark_template_parm (tree t
, void* data
)
4722 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4724 template_parm_level_and_index (t
, &level
, &idx
);
4726 if (level
== tpd
->level
)
4728 tpd
->parms
[idx
] = 1;
4729 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4732 /* In C++17 the type of a non-type argument is a deduced context. */
4733 if (cxx_dialect
>= cxx17
4734 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4735 for_each_template_parm (TREE_TYPE (t
),
4736 &mark_template_parm
,
4739 /*include_nondeduced_p=*/false);
4741 /* Return zero so that for_each_template_parm will continue the
4742 traversal of the tree; we want to mark *every* template parm. */
4746 /* Process the partial specialization DECL. */
4749 process_partial_specialization (tree decl
)
4751 tree type
= TREE_TYPE (decl
);
4752 tree tinfo
= get_template_info (decl
);
4753 tree maintmpl
= TI_TEMPLATE (tinfo
);
4754 tree specargs
= TI_ARGS (tinfo
);
4755 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4756 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4759 int nargs
= TREE_VEC_LENGTH (inner_args
);
4762 bool did_error_intro
= false;
4763 struct template_parm_data tpd
;
4764 struct template_parm_data tpd2
;
4766 gcc_assert (current_template_parms
);
4768 /* A concept cannot be specialized. */
4769 if (flag_concepts
&& variable_concept_p (maintmpl
))
4771 error ("specialization of variable concept %q#D", maintmpl
);
4772 return error_mark_node
;
4775 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4776 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4778 /* We check that each of the template parameters given in the
4779 partial specialization is used in the argument list to the
4780 specialization. For example:
4782 template <class T> struct S;
4783 template <class T> struct S<T*>;
4785 The second declaration is OK because `T*' uses the template
4786 parameter T, whereas
4788 template <class T> struct S<int>;
4790 is no good. Even trickier is:
4801 The S2<T> declaration is actually invalid; it is a
4802 full-specialization. Of course,
4805 struct S2<T (*)(U)>;
4807 or some such would have been OK. */
4808 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4809 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4810 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4812 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4813 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4814 for (i
= 0; i
< nargs
; ++i
)
4816 tpd
.current_arg
= i
;
4817 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4818 &mark_template_parm
,
4821 /*include_nondeduced_p=*/false);
4823 for (i
= 0; i
< ntparms
; ++i
)
4824 if (tpd
.parms
[i
] == 0)
4826 /* One of the template parms was not used in a deduced context in the
4828 if (!did_error_intro
)
4830 error ("template parameters not deducible in "
4831 "partial specialization:");
4832 did_error_intro
= true;
4835 inform (input_location
, " %qD",
4836 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4839 if (did_error_intro
)
4840 return error_mark_node
;
4842 /* [temp.class.spec]
4844 The argument list of the specialization shall not be identical to
4845 the implicit argument list of the primary template. */
4847 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4848 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4850 || !strictly_subsumes (current_template_constraints (),
4851 get_constraints (maintmpl
))))
4854 error ("partial specialization %q+D does not specialize "
4855 "any template arguments; to define the primary template, "
4856 "remove the template argument list", decl
);
4858 error ("partial specialization %q+D does not specialize any "
4859 "template arguments and is not more constrained than "
4860 "the primary template; to define the primary template, "
4861 "remove the template argument list", decl
);
4862 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4865 /* A partial specialization that replaces multiple parameters of the
4866 primary template with a pack expansion is less specialized for those
4868 if (nargs
< DECL_NTPARMS (maintmpl
))
4870 error ("partial specialization is not more specialized than the "
4871 "primary template because it replaces multiple parameters "
4872 "with a pack expansion");
4873 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4874 /* Avoid crash in process_partial_specialization. */
4878 /* If we aren't in a dependent class, we can actually try deduction. */
4879 else if (tpd
.level
== 1
4880 /* FIXME we should be able to handle a partial specialization of a
4881 partial instantiation, but currently we can't (c++/41727). */
4882 && TMPL_ARGS_DEPTH (specargs
) == 1
4883 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4885 auto_diagnostic_group d
;
4886 if (permerror (input_location
, "partial specialization %qD is not "
4887 "more specialized than", decl
))
4888 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4892 /* [temp.class.spec]
4894 A partially specialized non-type argument expression shall not
4895 involve template parameters of the partial specialization except
4896 when the argument expression is a simple identifier.
4898 The type of a template parameter corresponding to a specialized
4899 non-type argument shall not be dependent on a parameter of the
4902 Also, we verify that pack expansions only occur at the
4903 end of the argument list. */
4904 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4906 for (i
= 0; i
< nargs
; ++i
)
4908 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4909 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4910 tree packed_args
= NULL_TREE
;
4913 if (ARGUMENT_PACK_P (arg
))
4915 /* Extract the arguments from the argument pack. We'll be
4916 iterating over these in the following loop. */
4917 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4918 len
= TREE_VEC_LENGTH (packed_args
);
4921 for (j
= 0; j
< len
; j
++)
4924 /* Get the Jth argument in the parameter pack. */
4925 arg
= TREE_VEC_ELT (packed_args
, j
);
4927 if (PACK_EXPANSION_P (arg
))
4929 /* Pack expansions must come at the end of the
4931 if ((packed_args
&& j
< len
- 1)
4932 || (!packed_args
&& i
< nargs
- 1))
4934 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4935 error ("parameter pack argument %qE must be at the "
4936 "end of the template argument list", arg
);
4938 error ("parameter pack argument %qT must be at the "
4939 "end of the template argument list", arg
);
4943 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4944 /* We only care about the pattern. */
4945 arg
= PACK_EXPANSION_PATTERN (arg
);
4947 if (/* These first two lines are the `non-type' bit. */
4949 && TREE_CODE (arg
) != TEMPLATE_DECL
4950 /* This next two lines are the `argument expression is not just a
4951 simple identifier' condition and also the `specialized
4952 non-type argument' bit. */
4953 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4954 && !(REFERENCE_REF_P (arg
)
4955 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4957 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4958 || (packed_args
&& uses_template_parms (arg
)))
4959 error ("template argument %qE involves template parameter(s)",
4963 /* Look at the corresponding template parameter,
4964 marking which template parameters its type depends
4966 tree type
= TREE_TYPE (parm
);
4970 /* We haven't yet initialized TPD2. Do so now. */
4971 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4972 /* The number of parameters here is the number in the
4973 main template, which, as checked in the assertion
4975 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4977 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4980 /* Mark the template parameters. But this time, we're
4981 looking for the template parameters of the main
4982 template, not in the specialization. */
4983 tpd2
.current_arg
= i
;
4984 tpd2
.arg_uses_template_parms
[i
] = 0;
4985 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4986 for_each_template_parm (type
,
4987 &mark_template_parm
,
4990 /*include_nondeduced_p=*/false);
4992 if (tpd2
.arg_uses_template_parms
[i
])
4994 /* The type depended on some template parameters.
4995 If they are fully specialized in the
4996 specialization, that's OK. */
4999 for (j
= 0; j
< nargs
; ++j
)
5000 if (tpd2
.parms
[j
] != 0
5001 && tpd
.arg_uses_template_parms
[j
])
5004 error_n (input_location
, count
,
5005 "type %qT of template argument %qE depends "
5006 "on a template parameter",
5007 "type %qT of template argument %qE depends "
5008 "on template parameters",
5017 /* We should only get here once. */
5018 if (TREE_CODE (decl
) == TYPE_DECL
)
5019 gcc_assert (!COMPLETE_TYPE_P (type
));
5021 // Build the template decl.
5022 tree tmpl
= build_template_decl (decl
, current_template_parms
,
5023 DECL_MEMBER_TEMPLATE_P (maintmpl
));
5024 TREE_TYPE (tmpl
) = type
;
5025 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5026 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5027 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
5028 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
5030 /* Give template template parms a DECL_CONTEXT of the template
5031 for which they are a parameter. */
5032 for (i
= 0; i
< ntparms
; ++i
)
5034 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
5035 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5036 DECL_CONTEXT (parm
) = tmpl
;
5040 /* We didn't register this in check_explicit_specialization so we could
5041 wait until the constraints were set. */
5042 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
5044 associate_classtype_constraints (type
);
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
5047 = tree_cons (specargs
, tmpl
,
5048 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
5049 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
5051 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
5052 inst
= TREE_CHAIN (inst
))
5054 tree instance
= TREE_VALUE (inst
);
5055 if (TYPE_P (instance
)
5056 ? (COMPLETE_TYPE_P (instance
)
5057 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
5058 : DECL_TEMPLATE_INSTANTIATION (instance
))
5060 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
5061 tree inst_decl
= (DECL_P (instance
)
5062 ? instance
: TYPE_NAME (instance
));
5065 else if (spec
== error_mark_node
)
5066 permerror (input_location
,
5067 "declaration of %qD ambiguates earlier template "
5068 "instantiation for %qD", decl
, inst_decl
);
5069 else if (TREE_VALUE (spec
) == tmpl
)
5070 permerror (input_location
,
5071 "partial specialization of %qD after instantiation "
5072 "of %qD", decl
, inst_decl
);
5079 /* PARM is a template parameter of some form; return the corresponding
5080 TEMPLATE_PARM_INDEX. */
5083 get_template_parm_index (tree parm
)
5085 if (TREE_CODE (parm
) == PARM_DECL
5086 || TREE_CODE (parm
) == CONST_DECL
)
5087 parm
= DECL_INITIAL (parm
);
5088 else if (TREE_CODE (parm
) == TYPE_DECL
5089 || TREE_CODE (parm
) == TEMPLATE_DECL
)
5090 parm
= TREE_TYPE (parm
);
5091 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5092 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
5093 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5094 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
5095 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
5099 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5100 parameter packs used by the template parameter PARM. */
5103 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
5105 /* A type parm can't refer to another parm. */
5106 if (TREE_CODE (parm
) == TYPE_DECL
|| parm
== error_mark_node
)
5108 else if (TREE_CODE (parm
) == PARM_DECL
)
5110 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
5115 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
5117 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
5118 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
5119 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
5122 /* PARM is a template parameter pack. Return any parameter packs used in
5123 its type or the type of any of its template parameters. If there are
5124 any such packs, it will be instantiated into a fixed template parameter
5125 list by partial instantiation rather than be fully deduced. */
5128 fixed_parameter_pack_p (tree parm
)
5130 /* This can only be true in a member template. */
5131 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
5133 /* This can only be true for a parameter pack. */
5134 if (!template_parameter_pack_p (parm
))
5136 /* A type parm can't refer to another parm. */
5137 if (TREE_CODE (parm
) == TYPE_DECL
)
5140 tree parameter_packs
= NULL_TREE
;
5141 struct find_parameter_pack_data ppd
;
5142 ppd
.parameter_packs
= ¶meter_packs
;
5143 ppd
.visited
= new hash_set
<tree
>;
5144 ppd
.type_pack_expansion_p
= false;
5146 fixed_parameter_pack_p_1 (parm
, &ppd
);
5149 return parameter_packs
;
5152 /* Check that a template declaration's use of default arguments and
5153 parameter packs is not invalid. Here, PARMS are the template
5154 parameters. IS_PRIMARY is true if DECL is the thing declared by
5155 a primary template. IS_PARTIAL is true if DECL is a partial
5158 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5159 function template declaration or a friend class template
5160 declaration. In the function case, 1 indicates a declaration, 2
5161 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5162 emitted for extraneous default arguments.
5164 Returns TRUE if there were no errors found, FALSE otherwise. */
5167 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
5168 bool is_partial
, int is_friend_decl
)
5171 int last_level_to_check
;
5173 bool no_errors
= true;
5177 A default template-argument shall not be specified in a
5178 function template declaration or a function template definition, nor
5179 in the template-parameter-list of the definition of a member of a
5182 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5183 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5184 /* You can't have a function template declaration in a local
5185 scope, nor you can you define a member of a class template in a
5189 if ((TREE_CODE (decl
) == TYPE_DECL
5191 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5192 || (TREE_CODE (decl
) == FUNCTION_DECL
5193 && LAMBDA_FUNCTION_P (decl
)))
5194 /* A lambda doesn't have an explicit declaration; don't complain
5195 about the parms of the enclosing class. */
5198 if (current_class_type
5199 && !TYPE_BEING_DEFINED (current_class_type
)
5200 && DECL_LANG_SPECIFIC (decl
)
5201 && DECL_DECLARES_FUNCTION_P (decl
)
5202 /* If this is either a friend defined in the scope of the class
5203 or a member function. */
5204 && (DECL_FUNCTION_MEMBER_P (decl
)
5205 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5206 : DECL_FRIEND_CONTEXT (decl
)
5207 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5209 /* And, if it was a member function, it really was defined in
5210 the scope of the class. */
5211 && (!DECL_FUNCTION_MEMBER_P (decl
)
5212 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5213 /* We already checked these parameters when the template was
5214 declared, so there's no need to do it again now. This function
5215 was defined in class scope, but we're processing its body now
5216 that the class is complete. */
5219 /* Core issue 226 (C++0x only): the following only applies to class
5222 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5226 If a template-parameter has a default template-argument, all
5227 subsequent template-parameters shall have a default
5228 template-argument supplied. */
5229 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5231 tree inner_parms
= TREE_VALUE (parm_level
);
5232 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5233 int seen_def_arg_p
= 0;
5236 for (i
= 0; i
< ntparms
; ++i
)
5238 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5240 if (parm
== error_mark_node
)
5243 if (TREE_PURPOSE (parm
))
5245 else if (seen_def_arg_p
5246 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5248 error ("no default argument for %qD", TREE_VALUE (parm
));
5249 /* For better subsequent error-recovery, we indicate that
5250 there should have been a default argument. */
5251 TREE_PURPOSE (parm
) = error_mark_node
;
5254 else if (!is_partial
5256 /* Don't complain about an enclosing partial
5258 && parm_level
== parms
5259 && TREE_CODE (decl
) == TYPE_DECL
5261 && template_parameter_pack_p (TREE_VALUE (parm
))
5262 /* A fixed parameter pack will be partially
5263 instantiated into a fixed length list. */
5264 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5266 /* A primary class template can only have one
5267 parameter pack, at the end of the template
5270 error ("parameter pack %q+D must be at the end of the"
5271 " template parameter list", TREE_VALUE (parm
));
5273 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5281 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5285 /* For an ordinary class template, default template arguments are
5286 allowed at the innermost level, e.g.:
5287 template <class T = int>
5289 but, in a partial specialization, they're not allowed even
5290 there, as we have in [temp.class.spec]:
5292 The template parameter list of a specialization shall not
5293 contain default template argument values.
5295 So, for a partial specialization, or for a function template
5296 (in C++98/C++03), we look at all of them. */
5299 /* But, for a primary class template that is not a partial
5300 specialization we look at all template parameters except the
5302 parms
= TREE_CHAIN (parms
);
5304 /* Figure out what error message to issue. */
5305 if (is_friend_decl
== 2)
5306 msg
= G_("default template arguments may not be used in function template "
5307 "friend re-declaration");
5308 else if (is_friend_decl
)
5309 msg
= G_("default template arguments may not be used in template "
5310 "friend declarations");
5311 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5312 msg
= G_("default template arguments may not be used in function templates "
5313 "without %<-std=c++11%> or %<-std=gnu++11%>");
5314 else if (is_partial
)
5315 msg
= G_("default template arguments may not be used in "
5316 "partial specializations");
5317 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5318 msg
= G_("default argument for template parameter for class enclosing %qD");
5320 /* Per [temp.param]/9, "A default template-argument shall not be
5321 specified in the template-parameter-lists of the definition of
5322 a member of a class template that appears outside of the member's
5323 class.", thus if we aren't handling a member of a class template
5324 there is no need to examine the parameters. */
5327 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5328 /* If we're inside a class definition, there's no need to
5329 examine the parameters to the class itself. On the one
5330 hand, they will be checked when the class is defined, and,
5331 on the other, default arguments are valid in things like:
5332 template <class T = double>
5333 struct S { template <class U> void f(U); };
5334 Here the default argument for `S' has no bearing on the
5335 declaration of `f'. */
5336 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5338 /* Check everything. */
5339 last_level_to_check
= 0;
5341 for (parm_level
= parms
;
5342 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5343 parm_level
= TREE_CHAIN (parm_level
))
5345 tree inner_parms
= TREE_VALUE (parm_level
);
5349 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5350 for (i
= 0; i
< ntparms
; ++i
)
5352 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5355 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5360 if (is_friend_decl
== 2)
5367 /* Clear out the default argument so that we are not
5369 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5373 /* At this point, if we're still interested in issuing messages,
5374 they must apply to classes surrounding the object declared. */
5376 msg
= G_("default argument for template parameter for class "
5383 /* Worker for push_template_decl_real, called via
5384 for_each_template_parm. DATA is really an int, indicating the
5385 level of the parameters we are interested in. If T is a template
5386 parameter of that level, return nonzero. */
5389 template_parm_this_level_p (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 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5402 DATA is really an int, indicating the innermost outer level of parameters.
5403 If T is a template parameter of that level or further out, return
5407 template_parm_outer_level (tree t
, void *data
)
5409 int this_level
= *(int *)data
;
5412 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5413 level
= TEMPLATE_PARM_LEVEL (t
);
5415 level
= TEMPLATE_TYPE_LEVEL (t
);
5416 return level
<= this_level
;
5419 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5420 parameters given by current_template_args, or reuses a
5421 previously existing one, if appropriate. Returns the DECL, or an
5422 equivalent one, if it is replaced via a call to duplicate_decls.
5424 If IS_FRIEND is true, DECL is a friend declaration. */
5427 push_template_decl_real (tree decl
, bool is_friend
)
5435 int new_template_p
= 0;
5436 /* True if the template is a member template, in the sense of
5438 bool member_template_p
= false;
5440 if (decl
== error_mark_node
|| !current_template_parms
)
5441 return error_mark_node
;
5443 /* See if this is a partial specialization. */
5444 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5445 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5446 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5448 && DECL_LANG_SPECIFIC (decl
)
5449 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5450 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5452 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5456 /* For a friend, we want the context of the friend, not
5457 the type of which it is a friend. */
5458 ctx
= CP_DECL_CONTEXT (decl
);
5459 else if (CP_DECL_CONTEXT (decl
)
5460 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5461 /* In the case of a virtual function, we want the class in which
5463 ctx
= CP_DECL_CONTEXT (decl
);
5465 /* Otherwise, if we're currently defining some class, the DECL
5466 is assumed to be a member of the class. */
5467 ctx
= current_scope ();
5469 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5472 if (!DECL_CONTEXT (decl
))
5473 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5475 /* See if this is a primary template. */
5476 if (is_friend
&& ctx
5477 && uses_template_parms_level (ctx
, processing_template_decl
))
5478 /* A friend template that specifies a class context, i.e.
5479 template <typename T> friend void A<T>::f();
5482 else if (TREE_CODE (decl
) == TYPE_DECL
5483 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5486 is_primary
= template_parm_scope_p ();
5490 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5492 if (DECL_CLASS_SCOPE_P (decl
))
5493 member_template_p
= true;
5494 if (TREE_CODE (decl
) == TYPE_DECL
5495 && anon_aggrname_p (DECL_NAME (decl
)))
5497 error ("template class without a name");
5498 return error_mark_node
;
5500 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5502 if (member_template_p
)
5504 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5505 error ("member template %qD may not have virt-specifiers", decl
);
5507 if (DECL_DESTRUCTOR_P (decl
))
5511 A destructor shall not be a member template. */
5512 error ("destructor %qD declared as member template", decl
);
5513 return error_mark_node
;
5515 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl
))
5516 && (!prototype_p (TREE_TYPE (decl
))
5517 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5518 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5519 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5520 == void_list_node
)))
5522 /* [basic.stc.dynamic.allocation]
5524 An allocation function can be a function
5525 template. ... Template allocation functions shall
5526 have two or more parameters. */
5527 error ("invalid template declaration of %qD", decl
);
5528 return error_mark_node
;
5531 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5532 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5534 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5535 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5536 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5538 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5539 if (TREE_CODE (t
) == TYPE_DECL
)
5541 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5542 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5545 else if (TREE_CODE (decl
) == TYPE_DECL
5546 && TYPE_DECL_ALIAS_P (decl
))
5547 /* alias-declaration */
5548 gcc_assert (!DECL_ARTIFICIAL (decl
));
5549 else if (VAR_P (decl
))
5550 /* C++14 variable template. */;
5553 error ("template declaration of %q#D", decl
);
5554 return error_mark_node
;
5558 /* Check to see that the rules regarding the use of default
5559 arguments are not being violated. We check args for a friend
5560 functions when we know whether it's a definition, introducing
5561 declaration or re-declaration. */
5562 if (!is_friend
|| TREE_CODE (decl
) != FUNCTION_DECL
)
5563 check_default_tmpl_args (decl
, current_template_parms
,
5564 is_primary
, is_partial
, is_friend
);
5566 /* Ensure that there are no parameter packs in the type of this
5567 declaration that have not been expanded. */
5568 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5570 /* Check each of the arguments individually to see if there are
5571 any bare parameter packs. */
5572 tree type
= TREE_TYPE (decl
);
5573 tree arg
= DECL_ARGUMENTS (decl
);
5574 tree argtype
= TYPE_ARG_TYPES (type
);
5576 while (arg
&& argtype
)
5578 if (!DECL_PACK_P (arg
)
5579 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5581 /* This is a PARM_DECL that contains unexpanded parameter
5582 packs. We have already complained about this in the
5583 check_for_bare_parameter_packs call, so just replace
5584 these types with ERROR_MARK_NODE. */
5585 TREE_TYPE (arg
) = error_mark_node
;
5586 TREE_VALUE (argtype
) = error_mark_node
;
5589 arg
= DECL_CHAIN (arg
);
5590 argtype
= TREE_CHAIN (argtype
);
5593 /* Check for bare parameter packs in the return type and the
5594 exception specifiers. */
5595 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5596 /* Errors were already issued, set return type to int
5597 as the frontend doesn't expect error_mark_node as
5599 TREE_TYPE (type
) = integer_type_node
;
5600 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5601 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5603 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5604 && TYPE_DECL_ALIAS_P (decl
))
5605 ? DECL_ORIGINAL_TYPE (decl
)
5606 : TREE_TYPE (decl
)))
5608 TREE_TYPE (decl
) = error_mark_node
;
5609 return error_mark_node
;
5613 return process_partial_specialization (decl
);
5615 args
= current_template_args ();
5618 || TREE_CODE (ctx
) == FUNCTION_DECL
5619 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5620 || (TREE_CODE (decl
) == TYPE_DECL
5621 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5622 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5624 if (DECL_LANG_SPECIFIC (decl
)
5625 && DECL_TEMPLATE_INFO (decl
)
5626 && DECL_TI_TEMPLATE (decl
))
5627 tmpl
= DECL_TI_TEMPLATE (decl
);
5628 /* If DECL is a TYPE_DECL for a class-template, then there won't
5629 be DECL_LANG_SPECIFIC. The information equivalent to
5630 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5631 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5632 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5633 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5635 /* Since a template declaration already existed for this
5636 class-type, we must be redeclaring it here. Make sure
5637 that the redeclaration is valid. */
5638 redeclare_class_template (TREE_TYPE (decl
),
5639 current_template_parms
,
5640 current_template_constraints ());
5641 /* We don't need to create a new TEMPLATE_DECL; just use the
5642 one we already had. */
5643 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5647 tmpl
= build_template_decl (decl
, current_template_parms
,
5651 if (DECL_LANG_SPECIFIC (decl
)
5652 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5654 /* A specialization of a member template of a template
5656 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5657 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5658 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5664 tree a
, t
, current
, parms
;
5666 tree tinfo
= get_template_info (decl
);
5670 error ("template definition of non-template %q#D", decl
);
5671 return error_mark_node
;
5674 tmpl
= TI_TEMPLATE (tinfo
);
5676 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5677 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5678 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5679 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5683 /* The declaration is a specialization of a member
5684 template, declared outside the class. Therefore, the
5685 innermost template arguments will be NULL, so we
5686 replace them with the arguments determined by the
5687 earlier call to check_explicit_specialization. */
5688 args
= DECL_TI_ARGS (decl
);
5691 = build_template_decl (decl
, current_template_parms
,
5693 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5694 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5695 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5696 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5697 DECL_TEMPLATE_INFO (new_tmpl
)
5698 = build_template_info (tmpl
, args
);
5700 register_specialization (new_tmpl
,
5701 most_general_template (tmpl
),
5707 /* Make sure the template headers we got make sense. */
5709 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5710 i
= TMPL_PARMS_DEPTH (parms
);
5711 if (TMPL_ARGS_DEPTH (args
) != i
)
5713 error ("expected %d levels of template parms for %q#D, got %d",
5714 i
, decl
, TMPL_ARGS_DEPTH (args
));
5715 DECL_INTERFACE_KNOWN (decl
) = 1;
5716 return error_mark_node
;
5719 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5721 a
= TMPL_ARGS_LEVEL (args
, i
);
5722 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5724 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5726 if (current
== decl
)
5727 error ("got %d template parameters for %q#D",
5728 TREE_VEC_LENGTH (a
), decl
);
5730 error ("got %d template parameters for %q#T",
5731 TREE_VEC_LENGTH (a
), current
);
5732 error (" but %d required", TREE_VEC_LENGTH (t
));
5733 /* Avoid crash in import_export_decl. */
5734 DECL_INTERFACE_KNOWN (decl
) = 1;
5735 return error_mark_node
;
5738 if (current
== decl
)
5740 else if (current
== NULL_TREE
)
5741 /* Can happen in erroneous input. */
5744 current
= get_containing_scope (current
);
5747 /* Check that the parms are used in the appropriate qualifying scopes
5748 in the declarator. */
5749 if (!comp_template_args
5751 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5753 error ("template arguments to %qD do not match original "
5754 "template %qD", decl
, DECL_TEMPLATE_RESULT (tmpl
));
5755 if (!uses_template_parms (TI_ARGS (tinfo
)))
5756 inform (input_location
, "use %<template<>%> for"
5757 " an explicit specialization");
5758 /* Avoid crash in import_export_decl. */
5759 DECL_INTERFACE_KNOWN (decl
) = 1;
5760 return error_mark_node
;
5764 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5765 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5767 /* Push template declarations for global functions and types. Note
5768 that we do not try to push a global template friend declared in a
5769 template class; such a thing may well depend on the template
5770 parameters of the class. */
5771 if (new_template_p
&& !ctx
5772 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5774 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5775 if (tmpl
== error_mark_node
)
5776 return error_mark_node
;
5778 /* Hide template friend classes that haven't been declared yet. */
5779 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5781 DECL_ANTICIPATED (tmpl
) = 1;
5782 DECL_FRIEND_P (tmpl
) = 1;
5788 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5790 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5792 /* Give template template parms a DECL_CONTEXT of the template
5793 for which they are a parameter. */
5794 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5795 for (int i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5797 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5798 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5799 DECL_CONTEXT (parm
) = tmpl
;
5802 if (TREE_CODE (decl
) == TYPE_DECL
5803 && TYPE_DECL_ALIAS_P (decl
)
5804 && complex_alias_template_p (tmpl
))
5805 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5808 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5809 back to its most general template. If TMPL is a specialization,
5810 ARGS may only have the innermost set of arguments. Add the missing
5811 argument levels if necessary. */
5812 if (DECL_TEMPLATE_INFO (tmpl
))
5813 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5815 info
= build_template_info (tmpl
, args
);
5817 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5818 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5822 retrofit_lang_decl (decl
);
5823 if (DECL_LANG_SPECIFIC (decl
))
5824 DECL_TEMPLATE_INFO (decl
) = info
;
5827 if (flag_implicit_templates
5829 && TREE_PUBLIC (decl
)
5830 && VAR_OR_FUNCTION_DECL_P (decl
))
5831 /* Set DECL_COMDAT on template instantiations; if we force
5832 them to be emitted by explicit instantiation or -frepo,
5833 mark_needed will tell cgraph to do the right thing. */
5834 DECL_COMDAT (decl
) = true;
5836 return DECL_TEMPLATE_RESULT (tmpl
);
5840 push_template_decl (tree decl
)
5842 return push_template_decl_real (decl
, false);
5845 /* FN is an inheriting constructor that inherits from the constructor
5846 template INHERITED; turn FN into a constructor template with a matching
5850 add_inherited_template_parms (tree fn
, tree inherited
)
5853 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5854 inner_parms
= copy_node (inner_parms
);
5856 = tree_cons (size_int (processing_template_decl
+ 1),
5857 inner_parms
, current_template_parms
);
5858 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5859 tree args
= template_parms_to_args (parms
);
5860 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5861 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5862 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5863 DECL_ARTIFICIAL (tmpl
) = true;
5864 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5868 /* Called when a class template TYPE is redeclared with the indicated
5869 template PARMS, e.g.:
5871 template <class T> struct S;
5872 template <class T> struct S {}; */
5875 redeclare_class_template (tree type
, tree parms
, tree cons
)
5881 if (!TYPE_TEMPLATE_INFO (type
))
5883 error ("%qT is not a template type", type
);
5887 tmpl
= TYPE_TI_TEMPLATE (type
);
5888 if (!PRIMARY_TEMPLATE_P (tmpl
))
5889 /* The type is nested in some template class. Nothing to worry
5890 about here; there are no new template parameters for the nested
5896 error ("template specifiers not specified in declaration of %qD",
5901 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5902 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5904 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5906 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5907 "redeclared with %d template parameter",
5908 "redeclared with %d template parameters",
5909 TREE_VEC_LENGTH (parms
));
5910 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5911 "previous declaration %qD used %d template parameter",
5912 "previous declaration %qD used %d template parameters",
5913 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5917 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5924 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5925 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5928 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5929 if (error_operand_p (tmpl_parm
))
5932 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5933 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5934 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5936 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5938 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5939 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5940 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5941 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5942 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5943 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5944 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5945 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5946 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5948 error ("template parameter %q+#D", tmpl_parm
);
5949 error ("redeclared here as %q#D", parm
);
5953 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5955 /* We have in [temp.param]:
5957 A template-parameter may not be given default arguments
5958 by two different declarations in the same scope. */
5959 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5960 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5961 "original definition appeared here");
5965 if (parm_default
!= NULL_TREE
)
5966 /* Update the previous template parameters (which are the ones
5967 that will really count) with the new default value. */
5968 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5969 else if (tmpl_default
!= NULL_TREE
)
5970 /* Update the new parameters, too; they'll be used as the
5971 parameters for any members. */
5972 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5974 /* Give each template template parm in this redeclaration a
5975 DECL_CONTEXT of the template for which they are a parameter. */
5976 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5978 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5979 DECL_CONTEXT (parm
) = tmpl
;
5982 if (TREE_CODE (parm
) == TYPE_DECL
)
5983 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5986 // Cannot redeclare a class template with a different set of constraints.
5987 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5989 error_at (input_location
, "redeclaration %q#D with different "
5990 "constraints", tmpl
);
5991 inform (DECL_SOURCE_LOCATION (tmpl
),
5992 "original declaration appeared here");
5998 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5999 to be used when the caller has already checked
6000 (processing_template_decl
6001 && !instantiation_dependent_expression_p (expr)
6002 && potential_constant_expression (expr))
6003 and cleared processing_template_decl. */
6006 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
6008 return tsubst_copy_and_build (expr
,
6011 /*in_decl=*/NULL_TREE
,
6012 /*function_p=*/false,
6013 /*integral_constant_expression_p=*/true);
6016 /* Simplify EXPR if it is a non-dependent expression. Returns the
6017 (possibly simplified) expression. */
6020 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
6022 if (expr
== NULL_TREE
)
6025 /* If we're in a template, but EXPR isn't value dependent, simplify
6026 it. We're supposed to treat:
6028 template <typename T> void f(T[1 + 1]);
6029 template <typename T> void f(T[2]);
6031 as two declarations of the same function, for example. */
6032 if (processing_template_decl
6033 && is_nondependent_constant_expression (expr
))
6035 processing_template_decl_sentinel s
;
6036 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6042 instantiate_non_dependent_expr (tree expr
)
6044 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
6047 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6048 an uninstantiated expression. */
6051 instantiate_non_dependent_or_null (tree expr
)
6053 if (expr
== NULL_TREE
)
6055 if (processing_template_decl
)
6057 if (!is_nondependent_constant_expression (expr
))
6061 processing_template_decl_sentinel s
;
6062 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
6068 /* True iff T is a specialization of a variable template. */
6071 variable_template_specialization_p (tree t
)
6073 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
6075 tree tmpl
= DECL_TI_TEMPLATE (t
);
6076 return variable_template_p (tmpl
);
6079 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6080 template declaration, or a TYPE_DECL for an alias declaration. */
6083 alias_type_or_template_p (tree t
)
6087 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
6090 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
6091 || DECL_ALIAS_TEMPLATE_P (t
));
6094 /* Return TRUE iff T is a specialization of an alias template. */
6097 alias_template_specialization_p (const_tree t
)
6099 /* It's an alias template specialization if it's an alias and its
6100 TYPE_NAME is a specialization of a primary template. */
6101 if (TYPE_ALIAS_P (t
))
6102 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
6103 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
6108 /* An alias template is complex from a SFINAE perspective if a template-id
6109 using that alias can be ill-formed when the expansion is not, as with
6110 the void_t template. We determine this by checking whether the
6111 expansion for the alias template uses all its template parameters. */
6113 struct uses_all_template_parms_data
6120 uses_all_template_parms_r (tree t
, void *data_
)
6122 struct uses_all_template_parms_data
&data
6123 = *(struct uses_all_template_parms_data
*)data_
;
6124 tree idx
= get_template_parm_index (t
);
6126 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
6127 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
6132 complex_alias_template_p (const_tree tmpl
)
6134 struct uses_all_template_parms_data data
;
6135 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6136 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
6137 data
.level
= TMPL_PARMS_DEPTH (parms
);
6138 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
6139 data
.seen
= XALLOCAVEC (bool, len
);
6140 for (int i
= 0; i
< len
; ++i
)
6141 data
.seen
[i
] = false;
6143 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
6144 for (int i
= 0; i
< len
; ++i
)
6150 /* Return TRUE iff T is a specialization of a complex alias template with
6151 dependent template-arguments. */
6154 dependent_alias_template_spec_p (const_tree t
)
6156 if (!alias_template_specialization_p (t
))
6159 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
6160 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
6163 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
6164 if (!any_dependent_template_arguments_p (args
))
6170 /* Return the number of innermost template parameters in TMPL. */
6173 num_innermost_template_parms (tree tmpl
)
6175 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6176 return TREE_VEC_LENGTH (parms
);
6179 /* Return either TMPL or another template that it is equivalent to under DR
6180 1286: An alias that just changes the name of a template is equivalent to
6181 the other template. */
6184 get_underlying_template (tree tmpl
)
6186 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6187 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6189 /* Determine if the alias is equivalent to an underlying template. */
6190 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6191 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6195 tree underlying
= TI_TEMPLATE (tinfo
);
6196 if (!PRIMARY_TEMPLATE_P (underlying
)
6197 || (num_innermost_template_parms (tmpl
)
6198 != num_innermost_template_parms (underlying
)))
6201 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6202 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6203 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6206 /* Alias is equivalent. Strip it and repeat. */
6213 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6214 must be a reference-to-function or a pointer-to-function type, as specified
6215 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6216 and check that the resulting function has external linkage. */
6219 convert_nontype_argument_function (tree type
, tree expr
,
6220 tsubst_flags_t complain
)
6224 linkage_kind linkage
;
6226 fn
= instantiate_type (type
, fns
, tf_none
);
6227 if (fn
== error_mark_node
)
6228 return error_mark_node
;
6230 if (value_dependent_expression_p (fn
))
6233 fn_no_ptr
= strip_fnptr_conv (fn
);
6234 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6235 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6236 if (BASELINK_P (fn_no_ptr
))
6237 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6239 /* [temp.arg.nontype]/1
6241 A template-argument for a non-type, non-template template-parameter
6244 -- the address of an object or function with external [C++11: or
6245 internal] linkage. */
6247 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr
);
6248 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6250 if (complain
& tf_error
)
6252 error ("%qE is not a valid template argument for type %qT",
6254 if (TYPE_PTR_P (type
))
6255 inform (input_location
, "it must be the address of a function "
6256 "with external linkage");
6258 inform (input_location
, "it must be the name of a function with "
6259 "external linkage");
6264 linkage
= decl_linkage (fn_no_ptr
);
6265 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6267 if (complain
& tf_error
)
6269 if (cxx_dialect
>= cxx11
)
6270 error ("%qE is not a valid template argument for type %qT "
6271 "because %qD has no linkage",
6272 expr
, type
, fn_no_ptr
);
6274 error ("%qE is not a valid template argument for type %qT "
6275 "because %qD does not have external linkage",
6276 expr
, type
, fn_no_ptr
);
6282 if (TYPE_REF_P (type
))
6284 if (REFERENCE_REF_P (fn
))
6285 fn
= TREE_OPERAND (fn
, 0);
6287 fn
= build_address (fn
);
6289 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6290 fn
= build_nop (type
, fn
);
6295 /* Subroutine of convert_nontype_argument.
6296 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6297 Emit an error otherwise. */
6300 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6301 tsubst_flags_t complain
)
6303 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6304 tree orig_expr
= expr
;
6306 if (null_ptr_cst_p (expr
))
6308 if (TREE_CODE (expr
) == PTRMEM_CST
6309 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6310 PTRMEM_CST_CLASS (expr
)))
6312 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6314 if (processing_template_decl
6315 && TREE_CODE (expr
) == ADDR_EXPR
6316 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6318 if (complain
& tf_error
)
6320 error_at (loc
, "%qE is not a valid template argument for type %qT",
6322 if (TREE_CODE (expr
) != PTRMEM_CST
)
6323 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6325 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6330 /* Returns TRUE iff the address of OP is value-dependent.
6332 14.6.2.4 [temp.dep.temp]:
6333 A non-integral non-type template-argument is dependent if its type is
6334 dependent or it has either of the following forms
6337 and contains a nested-name-specifier which specifies a class-name that
6338 names a dependent type.
6340 We generalize this to just say that the address of a member of a
6341 dependent class is value-dependent; the above doesn't cover the
6342 address of a static data member named with an unqualified-id. */
6345 has_value_dependent_address (tree op
)
6347 /* We could use get_inner_reference here, but there's no need;
6348 this is only relevant for template non-type arguments, which
6349 can only be expressed as &id-expression. */
6352 tree ctx
= CP_DECL_CONTEXT (op
);
6353 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6360 /* The next set of functions are used for providing helpful explanatory
6361 diagnostics for failed overload resolution. Their messages should be
6362 indented by two spaces for consistency with the messages in
6366 unify_success (bool /*explain_p*/)
6371 /* Other failure functions should call this one, to provide a single function
6372 for setting a breakpoint on. */
6375 unify_invalid (bool /*explain_p*/)
6381 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6384 inform (input_location
,
6385 " couldn%'t deduce template parameter %qD", parm
);
6386 return unify_invalid (explain_p
);
6390 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6393 inform (input_location
,
6394 " types %qT and %qT have incompatible cv-qualifiers",
6396 return unify_invalid (explain_p
);
6400 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6403 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6404 return unify_invalid (explain_p
);
6408 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6411 inform (input_location
,
6412 " template parameter %qD is not a parameter pack, but "
6415 return unify_invalid (explain_p
);
6419 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6422 inform (input_location
,
6423 " template argument %qE does not match "
6424 "pointer-to-member constant %qE",
6426 return unify_invalid (explain_p
);
6430 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6433 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6434 return unify_invalid (explain_p
);
6438 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6441 inform (input_location
,
6442 " inconsistent parameter pack deduction with %qT and %qT",
6444 return unify_invalid (explain_p
);
6448 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6453 inform (input_location
,
6454 " deduced conflicting types for parameter %qT (%qT and %qT)",
6455 parm
, first
, second
);
6457 inform (input_location
,
6458 " deduced conflicting values for non-type parameter "
6459 "%qE (%qE and %qE)", parm
, first
, second
);
6461 return unify_invalid (explain_p
);
6465 unify_vla_arg (bool explain_p
, tree arg
)
6468 inform (input_location
,
6469 " variable-sized array type %qT is not "
6470 "a valid template argument",
6472 return unify_invalid (explain_p
);
6476 unify_method_type_error (bool explain_p
, tree arg
)
6479 inform (input_location
,
6480 " member function type %qT is not a valid template argument",
6482 return unify_invalid (explain_p
);
6486 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6491 inform_n (input_location
, wanted
,
6492 " candidate expects at least %d argument, %d provided",
6493 " candidate expects at least %d arguments, %d provided",
6496 inform_n (input_location
, wanted
,
6497 " candidate expects %d argument, %d provided",
6498 " candidate expects %d arguments, %d provided",
6501 return unify_invalid (explain_p
);
6505 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6507 return unify_arity (explain_p
, have
, wanted
);
6511 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6512 bool least_p
= false)
6514 return unify_arity (explain_p
, have
, wanted
, least_p
);
6518 unify_arg_conversion (bool explain_p
, tree to_type
,
6519 tree from_type
, tree arg
)
6522 inform (cp_expr_loc_or_loc (arg
, input_location
),
6523 " cannot convert %qE (type %qT) to type %qT",
6524 arg
, from_type
, to_type
);
6525 return unify_invalid (explain_p
);
6529 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6530 tree parm
, tree arg
)
6535 case tbr_ambiguous_baseclass
:
6536 inform (input_location
, " %qT is an ambiguous base class of %qT",
6540 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6543 return unify_invalid (explain_p
);
6547 unify_inconsistent_template_template_parameters (bool explain_p
)
6550 inform (input_location
,
6551 " template parameters of a template template argument are "
6552 "inconsistent with other deduced template arguments");
6553 return unify_invalid (explain_p
);
6557 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6560 inform (input_location
,
6561 " can%'t deduce a template for %qT from non-template type %qT",
6563 return unify_invalid (explain_p
);
6567 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6570 inform (input_location
,
6571 " template argument %qE does not match %qE", arg
, parm
);
6572 return unify_invalid (explain_p
);
6575 /* True if T is a C++20 template parameter object to store the argument for a
6576 template parameter of class type. */
6579 template_parm_object_p (const_tree t
)
6581 return (TREE_CODE (t
) == VAR_DECL
&& DECL_ARTIFICIAL (t
) && DECL_NAME (t
)
6582 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t
)), "_ZTA", 4));
6585 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6586 argument for TYPE, points to an unsuitable object. */
6589 invalid_tparm_referent_p (tree type
, tree expr
, tsubst_flags_t complain
)
6591 switch (TREE_CODE (expr
))
6594 return invalid_tparm_referent_p (type
, TREE_OPERAND (expr
, 0),
6598 return invalid_tparm_referent_p (type
, TARGET_EXPR_INITIAL (expr
),
6603 unsigned i
; tree elt
;
6604 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr
), i
, elt
)
6605 if (invalid_tparm_referent_p (TREE_TYPE (elt
), elt
, complain
))
6612 tree decl
= TREE_OPERAND (expr
, 0);
6616 if (complain
& tf_error
)
6617 error ("%qE is not a valid template argument of type %qT "
6618 "because %qE is not a variable", expr
, type
, decl
);
6621 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6623 if (complain
& tf_error
)
6624 error ("%qE is not a valid template argument of type %qT "
6625 "in C++98 because %qD does not have external linkage",
6629 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx17
)
6630 && decl_linkage (decl
) == lk_none
)
6632 if (complain
& tf_error
)
6633 error ("%qE is not a valid template argument of type %qT "
6634 "because %qD has no linkage", expr
, type
, decl
);
6637 /* C++17: For a non-type template-parameter of reference or pointer
6638 type, the value of the constant expression shall not refer to (or
6639 for a pointer type, shall not be the address of):
6640 * a subobject (4.5),
6641 * a temporary object (15.2),
6642 * a string literal (5.13.5),
6643 * the result of a typeid expression (8.2.8), or
6644 * a predefined __func__ variable (11.4.1). */
6645 else if (DECL_ARTIFICIAL (decl
))
6647 if (complain
& tf_error
)
6648 error ("the address of %qD is not a valid template argument",
6652 else if (!same_type_ignoring_top_level_qualifiers_p
6653 (strip_array_types (TREE_TYPE (type
)),
6654 strip_array_types (TREE_TYPE (decl
))))
6656 if (complain
& tf_error
)
6657 error ("the address of the %qT subobject of %qD is not a "
6658 "valid template argument", TREE_TYPE (type
), decl
);
6661 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6663 if (complain
& tf_error
)
6664 error ("the address of %qD is not a valid template argument "
6665 "because it does not have static storage duration",
6673 if (!INDIRECT_TYPE_P (type
))
6674 /* We're only concerned about pointers and references here. */;
6675 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6676 /* Null pointer values are OK in C++11. */;
6681 if (complain
& tf_error
)
6682 error ("%qD is not a valid template argument "
6683 "because %qD is a variable, not the address of "
6684 "a variable", expr
, expr
);
6689 if (complain
& tf_error
)
6690 error ("%qE is not a valid template argument for %qT "
6691 "because it is not the address of a variable",
6701 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6702 template argument EXPR. */
6705 get_template_parm_object (tree expr
, tsubst_flags_t complain
)
6707 if (TREE_CODE (expr
) == TARGET_EXPR
)
6708 expr
= TARGET_EXPR_INITIAL (expr
);
6710 if (!TREE_CONSTANT (expr
))
6712 if ((complain
& tf_error
)
6713 && require_rvalue_constant_expression (expr
))
6714 cxx_constant_value (expr
);
6715 return error_mark_node
;
6717 if (invalid_tparm_referent_p (TREE_TYPE (expr
), expr
, complain
))
6718 return error_mark_node
;
6720 tree name
= mangle_template_parm_object (expr
);
6721 tree decl
= get_global_binding (name
);
6725 tree type
= cp_build_qualified_type (TREE_TYPE (expr
), TYPE_QUAL_CONST
);
6726 decl
= create_temporary_var (type
);
6727 TREE_STATIC (decl
) = true;
6728 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
6729 TREE_READONLY (decl
) = true;
6730 DECL_NAME (decl
) = name
;
6731 SET_DECL_ASSEMBLER_NAME (decl
, name
);
6732 DECL_CONTEXT (decl
) = global_namespace
;
6733 comdat_linkage (decl
);
6734 pushdecl_top_level_and_finish (decl
, expr
);
6738 /* Attempt to convert the non-type template parameter EXPR to the
6739 indicated TYPE. If the conversion is successful, return the
6740 converted value. If the conversion is unsuccessful, return
6741 NULL_TREE if we issued an error message, or error_mark_node if we
6742 did not. We issue error messages for out-and-out bad template
6743 parameters, but not simply because the conversion failed, since we
6744 might be just trying to do argument deduction. Both TYPE and EXPR
6745 must be non-dependent.
6747 The conversion follows the special rules described in
6748 [temp.arg.nontype], and it is much more strict than an implicit
6751 This function is called twice for each template argument (see
6752 lookup_template_class for a more accurate description of this
6753 problem). This means that we need to handle expressions which
6754 are not valid in a C++ source, but can be created from the
6755 first call (for instance, casts to perform conversions). These
6756 hacks can go away after we fix the double coercion problem. */
6759 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6762 location_t loc
= cp_expr_loc_or_loc (expr
, input_location
);
6764 /* Detect immediately string literals as invalid non-type argument.
6765 This special-case is not needed for correctness (we would easily
6766 catch this later), but only to provide better diagnostic for this
6767 common user mistake. As suggested by DR 100, we do not mention
6768 linkage issues in the diagnostic as this is not the point. */
6769 if (TREE_CODE (expr
) == STRING_CST
&& !CLASS_TYPE_P (type
))
6771 if (complain
& tf_error
)
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because string literals can never be used in this context",
6778 /* Add the ADDR_EXPR now for the benefit of
6779 value_dependent_expression_p. */
6780 if (TYPE_PTROBV_P (type
)
6781 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6783 expr
= decay_conversion (expr
, complain
);
6784 if (expr
== error_mark_node
)
6785 return error_mark_node
;
6788 /* If we are in a template, EXPR may be non-dependent, but still
6789 have a syntactic, rather than semantic, form. For example, EXPR
6790 might be a SCOPE_REF, rather than the VAR_DECL to which the
6791 SCOPE_REF refers. Preserving the qualifying scope is necessary
6792 so that access checking can be performed when the template is
6793 instantiated -- but here we need the resolved form so that we can
6794 convert the argument. */
6795 bool non_dep
= false;
6796 if (TYPE_REF_OBJ_P (type
)
6797 && has_value_dependent_address (expr
))
6798 /* If we want the address and it's value-dependent, don't fold. */;
6799 else if (processing_template_decl
6800 && is_nondependent_constant_expression (expr
))
6802 if (error_operand_p (expr
))
6803 return error_mark_node
;
6804 expr_type
= TREE_TYPE (expr
);
6806 /* If the argument is non-dependent, perform any conversions in
6807 non-dependent context as well. */
6808 processing_template_decl_sentinel
s (non_dep
);
6810 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6812 if (value_dependent_expression_p (expr
))
6813 expr
= canonicalize_expr_argument (expr
, complain
);
6815 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6816 to a non-type argument of "nullptr". */
6817 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6818 expr
= fold_simple (convert (type
, expr
));
6820 /* In C++11, integral or enumeration non-type template arguments can be
6821 arbitrary constant expressions. Pointer and pointer to
6822 member arguments can be general constant expressions that evaluate
6823 to a null value, but otherwise still need to be of a specific form. */
6824 if (cxx_dialect
>= cxx11
)
6826 if (TREE_CODE (expr
) == PTRMEM_CST
&& TYPE_PTRMEM_P (type
))
6827 /* A PTRMEM_CST is already constant, and a valid template
6828 argument for a parameter of pointer to member type, we just want
6829 to leave it in that form rather than lower it to a
6831 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6832 || cxx_dialect
>= cxx17
)
6834 /* C++17: A template-argument for a non-type template-parameter shall
6835 be a converted constant expression (8.20) of the type of the
6836 template-parameter. */
6837 expr
= build_converted_constant_expr (type
, expr
, complain
);
6838 if (expr
== error_mark_node
)
6839 /* Make sure we return NULL_TREE only if we have really issued
6840 an error, as described above. */
6841 return (complain
& tf_error
) ? NULL_TREE
: error_mark_node
;
6842 expr
= maybe_constant_value (expr
, NULL_TREE
,
6843 /*manifestly_const_eval=*/true);
6844 expr
= convert_from_reference (expr
);
6846 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6848 tree folded
= maybe_constant_value (expr
, NULL_TREE
,
6849 /*manifestly_const_eval=*/true);
6850 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6851 : null_member_pointer_value_p (folded
))
6856 if (TYPE_REF_P (type
))
6857 expr
= mark_lvalue_use (expr
);
6859 expr
= mark_rvalue_use (expr
);
6861 /* HACK: Due to double coercion, we can get a
6862 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6863 which is the tree that we built on the first call (see
6864 below when coercing to reference to object or to reference to
6865 function). We just strip everything and get to the arg.
6866 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6868 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6870 tree probe_type
, probe
= expr
;
6871 if (REFERENCE_REF_P (probe
))
6872 probe
= TREE_OPERAND (probe
, 0);
6873 probe_type
= TREE_TYPE (probe
);
6874 if (TREE_CODE (probe
) == NOP_EXPR
)
6876 /* ??? Maybe we could use convert_from_reference here, but we
6877 would need to relax its constraints because the NOP_EXPR
6878 could actually change the type to something more cv-qualified,
6879 and this is not folded by convert_from_reference. */
6880 tree addr
= TREE_OPERAND (probe
, 0);
6881 if (TYPE_REF_P (probe_type
)
6882 && TREE_CODE (addr
) == ADDR_EXPR
6883 && TYPE_PTR_P (TREE_TYPE (addr
))
6884 && (same_type_ignoring_top_level_qualifiers_p
6885 (TREE_TYPE (probe_type
),
6886 TREE_TYPE (TREE_TYPE (addr
)))))
6888 expr
= TREE_OPERAND (addr
, 0);
6889 expr_type
= TREE_TYPE (probe_type
);
6894 /* [temp.arg.nontype]/5, bullet 1
6896 For a non-type template-parameter of integral or enumeration type,
6897 integral promotions (_conv.prom_) and integral conversions
6898 (_conv.integral_) are applied. */
6899 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6901 if (cxx_dialect
< cxx11
)
6903 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6904 t
= maybe_constant_value (t
);
6905 if (t
!= error_mark_node
)
6909 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6910 return error_mark_node
;
6912 /* Notice that there are constant expressions like '4 % 0' which
6913 do not fold into integer constants. */
6914 if (TREE_CODE (expr
) != INTEGER_CST
6915 && !value_dependent_expression_p (expr
))
6917 if (complain
& tf_error
)
6919 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6920 if (!require_potential_constant_expression (expr
))
6921 expr
= error_mark_node
;
6923 expr
= cxx_constant_value (expr
);
6924 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6925 inform (loc
, "in template argument for type %qT", type
);
6926 if (expr
== error_mark_node
)
6928 /* else cxx_constant_value complained but gave us
6929 a real constant, so go ahead. */
6930 if (TREE_CODE (expr
) != INTEGER_CST
)
6932 /* Some assemble time constant expressions like
6933 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6934 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6935 as we can emit them into .rodata initializers of
6936 variables, yet they can't fold into an INTEGER_CST at
6937 compile time. Refuse them here. */
6938 gcc_checking_assert (reduced_constant_expression_p (expr
));
6939 error_at (loc
, "template argument %qE for type %qT not "
6940 "a constant integer", expr
, type
);
6948 /* Avoid typedef problems. */
6949 if (TREE_TYPE (expr
) != type
)
6950 expr
= fold_convert (type
, expr
);
6952 /* [temp.arg.nontype]/5, bullet 2
6954 For a non-type template-parameter of type pointer to object,
6955 qualification conversions (_conv.qual_) and the array-to-pointer
6956 conversion (_conv.array_) are applied. */
6957 else if (TYPE_PTROBV_P (type
))
6959 tree decayed
= expr
;
6961 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6962 decay_conversion or an explicit cast. If it's a problematic cast,
6963 we'll complain about it below. */
6964 if (TREE_CODE (expr
) == NOP_EXPR
)
6968 if (TREE_CODE (probe
) == ADDR_EXPR
6969 && TYPE_PTR_P (TREE_TYPE (probe
)))
6972 expr_type
= TREE_TYPE (expr
);
6976 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6978 A template-argument for a non-type, non-template template-parameter
6979 shall be one of: [...]
6981 -- the name of a non-type template-parameter;
6982 -- the address of an object or function with external linkage, [...]
6983 expressed as "& id-expression" where the & is optional if the name
6984 refers to a function or array, or if the corresponding
6985 template-parameter is a reference.
6987 Here, we do not care about functions, as they are invalid anyway
6988 for a parameter of type pointer-to-object. */
6990 if (value_dependent_expression_p (expr
))
6991 /* Non-type template parameters are OK. */
6993 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6994 /* Null pointer values are OK in C++11. */;
6995 else if (TREE_CODE (expr
) != ADDR_EXPR
6996 && !INDIRECT_TYPE_P (expr_type
))
6997 /* Other values, like integer constants, might be valid
6998 non-type arguments of some other type. */
6999 return error_mark_node
;
7000 else if (invalid_tparm_referent_p (type
, expr
, complain
))
7005 expr
= perform_qualification_conversions (type
, expr
);
7006 if (expr
== error_mark_node
)
7007 return error_mark_node
;
7009 /* [temp.arg.nontype]/5, bullet 3
7011 For a non-type template-parameter of type reference to object, no
7012 conversions apply. The type referred to by the reference may be more
7013 cv-qualified than the (otherwise identical) type of the
7014 template-argument. The template-parameter is bound directly to the
7015 template-argument, which must be an lvalue. */
7016 else if (TYPE_REF_OBJ_P (type
))
7018 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
7020 return error_mark_node
;
7022 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
7024 if (complain
& tf_error
)
7025 error ("%qE is not a valid template argument for type %qT "
7026 "because of conflicts in cv-qualification", expr
, type
);
7030 if (!lvalue_p (expr
))
7032 if (complain
& tf_error
)
7033 error ("%qE is not a valid template argument for type %qT "
7034 "because it is not an lvalue", expr
, type
);
7038 /* [temp.arg.nontype]/1
7040 A template-argument for a non-type, non-template template-parameter
7041 shall be one of: [...]
7043 -- the address of an object or function with external linkage. */
7044 if (INDIRECT_REF_P (expr
)
7045 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
7047 expr
= TREE_OPERAND (expr
, 0);
7050 if (complain
& tf_error
)
7051 error ("%q#D is not a valid template argument for type %qT "
7052 "because a reference variable does not have a constant "
7053 "address", expr
, type
);
7058 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
7059 && value_dependent_expression_p (expr
))
7060 /* OK, dependent reference. We don't want to ask whether a DECL is
7061 itself value-dependent, since what we want here is its address. */;
7064 expr
= build_address (expr
);
7066 if (invalid_tparm_referent_p (type
, expr
, complain
))
7070 if (!same_type_p (type
, TREE_TYPE (expr
)))
7071 expr
= build_nop (type
, expr
);
7073 /* [temp.arg.nontype]/5, bullet 4
7075 For a non-type template-parameter of type pointer to function, only
7076 the function-to-pointer conversion (_conv.func_) is applied. If the
7077 template-argument represents a set of overloaded functions (or a
7078 pointer to such), the matching function is selected from the set
7080 else if (TYPE_PTRFN_P (type
))
7082 /* If the argument is a template-id, we might not have enough
7083 context information to decay the pointer. */
7084 if (!type_unknown_p (expr_type
))
7086 expr
= decay_conversion (expr
, complain
);
7087 if (expr
== error_mark_node
)
7088 return error_mark_node
;
7091 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
7092 /* Null pointer values are OK in C++11. */
7093 return perform_qualification_conversions (type
, expr
);
7095 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7096 if (!expr
|| expr
== error_mark_node
)
7099 /* [temp.arg.nontype]/5, bullet 5
7101 For a non-type template-parameter of type reference to function, no
7102 conversions apply. If the template-argument represents a set of
7103 overloaded functions, the matching function is selected from the set
7105 else if (TYPE_REFFN_P (type
))
7107 if (TREE_CODE (expr
) == ADDR_EXPR
)
7109 if (complain
& tf_error
)
7111 error ("%qE is not a valid template argument for type %qT "
7112 "because it is a pointer", expr
, type
);
7113 inform (input_location
, "try using %qE instead",
7114 TREE_OPERAND (expr
, 0));
7119 expr
= convert_nontype_argument_function (type
, expr
, complain
);
7120 if (!expr
|| expr
== error_mark_node
)
7123 /* [temp.arg.nontype]/5, bullet 6
7125 For a non-type template-parameter of type pointer to member function,
7126 no conversions apply. If the template-argument represents a set of
7127 overloaded member functions, the matching member function is selected
7128 from the set (_over.over_). */
7129 else if (TYPE_PTRMEMFUNC_P (type
))
7131 expr
= instantiate_type (type
, expr
, tf_none
);
7132 if (expr
== error_mark_node
)
7133 return error_mark_node
;
7135 /* [temp.arg.nontype] bullet 1 says the pointer to member
7136 expression must be a pointer-to-member constant. */
7137 if (!value_dependent_expression_p (expr
)
7138 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7141 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7142 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7143 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
7144 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
7146 /* [temp.arg.nontype]/5, bullet 7
7148 For a non-type template-parameter of type pointer to data member,
7149 qualification conversions (_conv.qual_) are applied. */
7150 else if (TYPE_PTRDATAMEM_P (type
))
7152 /* [temp.arg.nontype] bullet 1 says the pointer to member
7153 expression must be a pointer-to-member constant. */
7154 if (!value_dependent_expression_p (expr
)
7155 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
7158 expr
= perform_qualification_conversions (type
, expr
);
7159 if (expr
== error_mark_node
)
7162 else if (NULLPTR_TYPE_P (type
))
7164 if (!NULLPTR_TYPE_P (TREE_TYPE (expr
)))
7166 if (complain
& tf_error
)
7167 error ("%qE is not a valid template argument for type %qT "
7168 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
7173 else if (CLASS_TYPE_P (type
))
7175 /* Replace the argument with a reference to the corresponding template
7176 parameter object. */
7177 if (!value_dependent_expression_p (expr
))
7178 expr
= get_template_parm_object (expr
, complain
);
7179 if (expr
== error_mark_node
)
7182 /* A template non-type parameter must be one of the above. */
7186 /* Sanity check: did we actually convert the argument to the
7188 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7189 (type
, TREE_TYPE (expr
)));
7190 return convert_from_reference (expr
);
7193 /* Subroutine of coerce_template_template_parms, which returns 1 if
7194 PARM_PARM and ARG_PARM match using the rule for the template
7195 parameters of template template parameters. Both PARM and ARG are
7196 template parameters; the rest of the arguments are the same as for
7197 coerce_template_template_parms.
7200 coerce_template_template_parm (tree parm
,
7202 tsubst_flags_t complain
,
7206 if (arg
== NULL_TREE
|| error_operand_p (arg
)
7207 || parm
== NULL_TREE
|| error_operand_p (parm
))
7210 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7213 switch (TREE_CODE (parm
))
7216 /* We encounter instantiations of templates like
7217 template <template <template <class> class> class TT>
7220 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7221 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7223 if (!coerce_template_template_parms
7224 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
7230 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
7231 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7232 /* Argument is a parameter pack but parameter is not. */
7237 /* The tsubst call is used to handle cases such as
7239 template <int> class C {};
7240 template <class T, template <T> class TT> class D {};
7243 i.e. the parameter list of TT depends on earlier parameters. */
7244 if (!uses_template_parms (TREE_TYPE (arg
)))
7246 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
7247 if (!uses_template_parms (t
)
7248 && !same_type_p (t
, TREE_TYPE (arg
)))
7252 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
7253 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7254 /* Argument is a parameter pack but parameter is not. */
7266 /* Coerce template argument list ARGLIST for use with template
7267 template-parameter TEMPL. */
7270 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7271 tsubst_flags_t complain
)
7273 /* Consider an example where a template template parameter declared as
7275 template <class T, class U = std::allocator<T> > class TT
7277 The template parameter level of T and U are one level larger than
7278 of TT. To proper process the default argument of U, say when an
7279 instantiation `TT<int>' is seen, we need to build the full
7280 arguments containing {int} as the innermost level. Outer levels,
7281 available when not appearing as default template argument, can be
7282 obtained from the arguments of the enclosing template.
7284 Suppose that TT is later substituted with std::vector. The above
7285 instantiation is `TT<int, std::allocator<T> >' with TT at
7286 level 1, and T at level 2, while the template arguments at level 1
7287 becomes {std::vector} and the inner level 2 is {int}. */
7289 tree outer
= DECL_CONTEXT (templ
);
7292 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7293 /* We want arguments for the partial specialization, not arguments for
7294 the primary template. */
7295 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7297 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7299 else if (current_template_parms
)
7301 /* This is an argument of the current template, so we haven't set
7302 DECL_CONTEXT yet. */
7303 tree relevant_template_parms
;
7305 /* Parameter levels that are greater than the level of the given
7306 template template parm are irrelevant. */
7307 relevant_template_parms
= current_template_parms
;
7308 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7309 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7310 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7312 outer
= template_parms_to_args (relevant_template_parms
);
7316 arglist
= add_to_template_args (outer
, arglist
);
7318 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7319 return coerce_template_parms (parmlist
, arglist
, templ
,
7321 /*require_all_args=*/true,
7322 /*use_default_args=*/true);
7325 /* A cache of template template parameters with match-all default
7327 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7329 store_defaulted_ttp (tree v
, tree t
)
7331 if (!defaulted_ttp_cache
)
7332 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7333 defaulted_ttp_cache
->put (v
, t
);
7336 lookup_defaulted_ttp (tree v
)
7338 if (defaulted_ttp_cache
)
7339 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7344 /* T is a bound template template-parameter. Copy its arguments into default
7345 arguments of the template template-parameter's template parameters. */
7348 add_defaults_to_ttp (tree otmpl
)
7350 if (tree c
= lookup_defaulted_ttp (otmpl
))
7353 tree ntmpl
= copy_node (otmpl
);
7355 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7356 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7357 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7358 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7359 TYPE_NAME (ntype
) = ntmpl
;
7360 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7362 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7363 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7364 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7365 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7367 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7368 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7369 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7370 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7371 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7373 tree o
= TREE_VEC_ELT (vec
, i
);
7374 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7376 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7377 TREE_PURPOSE (n
) = any_targ_node
;
7381 store_defaulted_ttp (otmpl
, ntmpl
);
7385 /* ARG is a bound potential template template-argument, and PARGS is a list
7386 of arguments for the corresponding template template-parameter. Adjust
7387 PARGS as appropriate for application to ARG's template, and if ARG is a
7388 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7389 arguments to the template template parameter. */
7392 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7394 ++processing_template_decl
;
7395 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7396 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7398 /* When comparing two template template-parameters in partial ordering,
7399 rewrite the one currently being used as an argument to have default
7400 arguments for all parameters. */
7401 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7402 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7403 if (pargs
!= error_mark_node
)
7404 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7405 TYPE_TI_ARGS (arg
));
7410 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7411 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7412 /*require_all*/true,
7413 /*use_default*/true);
7415 --processing_template_decl
;
7419 /* Subroutine of unify for the case when PARM is a
7420 BOUND_TEMPLATE_TEMPLATE_PARM. */
7423 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7426 tree parmvec
= TYPE_TI_ARGS (parm
);
7427 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7429 /* The template template parm might be variadic and the argument
7430 not, so flatten both argument lists. */
7431 parmvec
= expand_template_argument_pack (parmvec
);
7432 argvec
= expand_template_argument_pack (argvec
);
7436 /* In keeping with P0522R0, adjust P's template arguments
7437 to apply to A's template; then flatten it again. */
7438 tree nparmvec
= parmvec
;
7439 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7440 nparmvec
= expand_template_argument_pack (nparmvec
);
7442 if (unify (tparms
, targs
, nparmvec
, argvec
,
7443 UNIFY_ALLOW_NONE
, explain_p
))
7446 /* If the P0522 adjustment eliminated a pack expansion, deduce
7449 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7450 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7451 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7456 /* Deduce arguments T, i from TT<T> or TT<i>.
7457 We check each element of PARMVEC and ARGVEC individually
7458 rather than the whole TREE_VEC since they can have
7459 different number of elements, which is allowed under N2555. */
7461 int len
= TREE_VEC_LENGTH (parmvec
);
7463 /* Check if the parameters end in a pack, making them
7465 int parm_variadic_p
= 0;
7467 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
7468 parm_variadic_p
= 1;
7470 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7471 /* If the template argument list of P contains a pack
7472 expansion that is not the last template argument, the
7473 entire template argument list is a non-deduced
7475 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
7476 return unify_success (explain_p
);
7478 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
7479 return unify_too_few_arguments (explain_p
,
7480 TREE_VEC_LENGTH (argvec
), len
);
7482 for (int i
= 0; i
< len
- parm_variadic_p
; ++i
)
7483 if (unify (tparms
, targs
,
7484 TREE_VEC_ELT (parmvec
, i
),
7485 TREE_VEC_ELT (argvec
, i
),
7486 UNIFY_ALLOW_NONE
, explain_p
))
7490 && unify_pack_expansion (tparms
, targs
,
7493 /*subr=*/true, explain_p
))
7500 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7501 template template parameters. Both PARM_PARMS and ARG_PARMS are
7502 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7505 Consider the example:
7506 template <class T> class A;
7507 template<template <class U> class TT> class B;
7509 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7510 the parameters to A, and OUTER_ARGS contains A. */
7513 coerce_template_template_parms (tree parm_parms
,
7515 tsubst_flags_t complain
,
7519 int nparms
, nargs
, i
;
7523 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7524 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7526 nparms
= TREE_VEC_LENGTH (parm_parms
);
7527 nargs
= TREE_VEC_LENGTH (arg_parms
);
7531 /* P0522R0: A template template-parameter P is at least as specialized as
7532 a template template-argument A if, given the following rewrite to two
7533 function templates, the function template corresponding to P is at
7534 least as specialized as the function template corresponding to A
7535 according to the partial ordering rules for function templates
7536 ([temp.func.order]). Given an invented class template X with the
7537 template parameter list of A (including default arguments):
7539 * Each of the two function templates has the same template parameters,
7540 respectively, as P or A.
7542 * Each function template has a single function parameter whose type is
7543 a specialization of X with template arguments corresponding to the
7544 template parameters from the respective function template where, for
7545 each template parameter PP in the template parameter list of the
7546 function template, a corresponding template argument AA is formed. If
7547 PP declares a parameter pack, then AA is the pack expansion
7548 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7550 If the rewrite produces an invalid type, then P is not at least as
7551 specialized as A. */
7553 /* So coerce P's args to apply to A's parms, and then deduce between A's
7554 args and the converted args. If that succeeds, A is at least as
7555 specialized as P, so they match.*/
7556 tree pargs
= template_parms_level_to_args (parm_parms
);
7557 ++processing_template_decl
;
7558 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7559 /*require_all*/true, /*use_default*/true);
7560 --processing_template_decl
;
7561 if (pargs
!= error_mark_node
)
7563 tree targs
= make_tree_vec (nargs
);
7564 tree aargs
= template_parms_level_to_args (arg_parms
);
7565 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7571 /* Determine whether we have a parameter pack at the end of the
7572 template template parameter's template parameter list. */
7573 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7575 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7577 if (error_operand_p (parm
))
7580 switch (TREE_CODE (parm
))
7584 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7589 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7599 && !(variadic_p
&& nargs
>= nparms
- 1))
7602 /* Check all of the template parameters except the parameter pack at
7603 the end (if any). */
7604 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7606 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7607 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7610 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7611 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7613 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7621 /* Check each of the template parameters in the template
7622 argument against the template parameter pack at the end of
7623 the template template parameter. */
7624 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7627 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7629 for (; i
< nargs
; ++i
)
7631 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7634 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7636 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7645 /* Verifies that the deduced template arguments (in TARGS) for the
7646 template template parameters (in TPARMS) represent valid bindings,
7647 by comparing the template parameter list of each template argument
7648 to the template parameter list of its corresponding template
7649 template parameter, in accordance with DR150. This
7650 routine can only be called after all template arguments have been
7651 deduced. It will return TRUE if all of the template template
7652 parameter bindings are okay, FALSE otherwise. */
7654 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7656 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7659 /* We're dealing with template parms in this process. */
7660 ++processing_template_decl
;
7662 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7664 for (i
= 0; i
< ntparms
; ++i
)
7666 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7667 tree targ
= TREE_VEC_ELT (targs
, i
);
7669 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7671 tree packed_args
= NULL_TREE
;
7674 if (ARGUMENT_PACK_P (targ
))
7676 /* Look inside the argument pack. */
7677 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7678 len
= TREE_VEC_LENGTH (packed_args
);
7681 for (idx
= 0; idx
< len
; ++idx
)
7683 tree targ_parms
= NULL_TREE
;
7686 /* Extract the next argument from the argument
7688 targ
= TREE_VEC_ELT (packed_args
, idx
);
7690 if (PACK_EXPANSION_P (targ
))
7691 /* Look at the pattern of the pack expansion. */
7692 targ
= PACK_EXPANSION_PATTERN (targ
);
7694 /* Extract the template parameters from the template
7696 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7697 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7698 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7699 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7701 /* Verify that we can coerce the template template
7702 parameters from the template argument to the template
7703 parameter. This requires an exact match. */
7705 && !coerce_template_template_parms
7706 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7721 --processing_template_decl
;
7725 /* Since type attributes aren't mangled, we need to strip them from
7726 template type arguments. */
7729 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7731 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7733 bool removed_attributes
= false;
7734 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7735 if (removed_attributes
7736 && (complain
& tf_warning
))
7737 warning (OPT_Wignored_attributes
,
7738 "ignoring attributes on template argument %qT", arg
);
7742 /* And from inside dependent non-type arguments like sizeof(Type). */
7745 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7747 if (!arg
|| arg
== error_mark_node
)
7749 bool removed_attributes
= false;
7750 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7751 if (removed_attributes
7752 && (complain
& tf_warning
))
7753 warning (OPT_Wignored_attributes
,
7754 "ignoring attributes in template argument %qE", arg
);
7758 // A template declaration can be substituted for a constrained
7759 // template template parameter only when the argument is more
7760 // constrained than the parameter.
7762 is_compatible_template_arg (tree parm
, tree arg
)
7764 tree parm_cons
= get_constraints (parm
);
7766 /* For now, allow constrained template template arguments
7767 and unconstrained template template parameters. */
7768 if (parm_cons
== NULL_TREE
)
7771 tree arg_cons
= get_constraints (arg
);
7773 // If the template parameter is constrained, we need to rewrite its
7774 // constraints in terms of the ARG's template parameters. This ensures
7775 // that all of the template parameter types will have the same depth.
7777 // Note that this is only valid when coerce_template_template_parm is
7778 // true for the innermost template parameters of PARM and ARG. In other
7779 // words, because coercion is successful, this conversion will be valid.
7782 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7783 parm_cons
= tsubst_constraint_info (parm_cons
,
7784 INNERMOST_TEMPLATE_ARGS (args
),
7785 tf_none
, NULL_TREE
);
7786 if (parm_cons
== error_mark_node
)
7790 return subsumes (parm_cons
, arg_cons
);
7793 // Convert a placeholder argument into a binding to the original
7794 // parameter. The original parameter is saved as the TREE_TYPE of
7797 convert_wildcard_argument (tree parm
, tree arg
)
7799 TREE_TYPE (arg
) = parm
;
7803 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7804 because one of them is dependent. But we need to represent the
7805 conversion for the benefit of cp_tree_equal. */
7808 maybe_convert_nontype_argument (tree type
, tree arg
)
7810 /* Auto parms get no conversion. */
7811 if (type_uses_auto (type
))
7813 /* We don't need or want to add this conversion now if we're going to use the
7814 argument for deduction. */
7815 if (value_dependent_expression_p (arg
))
7818 type
= cv_unqualified (type
);
7819 tree argtype
= TREE_TYPE (arg
);
7820 if (same_type_p (type
, argtype
))
7823 arg
= build1 (IMPLICIT_CONV_EXPR
, type
, arg
);
7824 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg
) = true;
7828 /* Convert the indicated template ARG as necessary to match the
7829 indicated template PARM. Returns the converted ARG, or
7830 error_mark_node if the conversion was unsuccessful. Error and
7831 warning messages are issued under control of COMPLAIN. This
7832 conversion is for the Ith parameter in the parameter list. ARGS is
7833 the full set of template arguments deduced so far. */
7836 convert_template_argument (tree parm
,
7839 tsubst_flags_t complain
,
7845 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7847 if (parm
== error_mark_node
|| error_operand_p (arg
))
7848 return error_mark_node
;
7850 /* Trivially convert placeholders. */
7851 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7852 return convert_wildcard_argument (parm
, arg
);
7854 if (arg
== any_targ_node
)
7857 if (TREE_CODE (arg
) == TREE_LIST
7858 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7860 /* The template argument was the name of some
7861 member function. That's usually
7862 invalid, but static members are OK. In any
7863 case, grab the underlying fields/functions
7864 and issue an error later if required. */
7865 orig_arg
= TREE_VALUE (arg
);
7866 TREE_TYPE (arg
) = unknown_type_node
;
7871 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7872 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7873 || requires_tmpl_type
);
7875 /* When determining whether an argument pack expansion is a template,
7876 look at the pattern. */
7877 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7878 arg
= PACK_EXPANSION_PATTERN (arg
);
7880 /* Deal with an injected-class-name used as a template template arg. */
7881 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7883 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7884 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7886 if (cxx_dialect
>= cxx11
)
7887 /* OK under DR 1004. */;
7888 else if (complain
& tf_warning_or_error
)
7889 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7890 " used as template template argument", TYPE_NAME (arg
));
7891 else if (flag_pedantic_errors
)
7899 ((TREE_CODE (arg
) == TEMPLATE_DECL
7900 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7901 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7902 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7903 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7906 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7907 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7908 arg
= TYPE_STUB_DECL (arg
);
7910 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7912 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7913 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7915 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7917 if (complain
& tf_error
)
7918 error ("invalid use of destructor %qE as a type", orig_arg
);
7919 return error_mark_node
;
7922 permerror (input_location
,
7923 "to refer to a type member of a template parameter, "
7924 "use %<typename %E%>", orig_arg
);
7926 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7927 TREE_OPERAND (arg
, 1),
7933 if (is_type
!= requires_type
)
7937 if (complain
& tf_error
)
7939 error ("type/value mismatch at argument %d in template "
7940 "parameter list for %qD",
7943 inform (input_location
,
7944 " expected a constant of type %qT, got %qT",
7946 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7947 else if (requires_tmpl_type
)
7948 inform (input_location
,
7949 " expected a class template, got %qE", orig_arg
);
7951 inform (input_location
,
7952 " expected a type, got %qE", orig_arg
);
7955 return error_mark_node
;
7957 if (is_tmpl_type
^ requires_tmpl_type
)
7959 if (in_decl
&& (complain
& tf_error
))
7961 error ("type/value mismatch at argument %d in template "
7962 "parameter list for %qD",
7965 inform (input_location
,
7966 " expected a type, got %qT", DECL_NAME (arg
));
7968 inform (input_location
,
7969 " expected a class template, got %qT", orig_arg
);
7971 return error_mark_node
;
7974 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7975 /* We already did the appropriate conversion when packing args. */
7979 if (requires_tmpl_type
)
7981 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7982 /* The number of argument required is not known yet.
7983 Just accept it for now. */
7987 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7990 /* Strip alias templates that are equivalent to another
7992 arg
= get_underlying_template (arg
);
7993 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7995 if (coerce_template_template_parms (parmparm
, argparm
,
8001 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8003 if (val
!= error_mark_node
)
8005 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
8006 val
= TREE_TYPE (val
);
8007 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
8008 val
= make_pack_expansion (val
, complain
);
8013 if (in_decl
&& (complain
& tf_error
))
8015 error ("type/value mismatch at argument %d in "
8016 "template parameter list for %qD",
8018 inform (input_location
,
8019 " expected a template of type %qD, got %qT",
8023 val
= error_mark_node
;
8026 // Check that the constraints are compatible before allowing the
8028 if (val
!= error_mark_node
)
8029 if (!is_compatible_template_arg (parm
, arg
))
8031 if (in_decl
&& (complain
& tf_error
))
8033 error ("constraint mismatch at argument %d in "
8034 "template parameter list for %qD",
8036 inform (input_location
, " expected %qD but got %qD",
8039 val
= error_mark_node
;
8045 /* We only form one instance of each template specialization.
8046 Therefore, if we use a non-canonical variant (i.e., a
8047 typedef), any future messages referring to the type will use
8048 the typedef, which is confusing if those future uses do not
8049 themselves also use the typedef. */
8051 val
= canonicalize_type_argument (val
, complain
);
8055 tree t
= TREE_TYPE (parm
);
8057 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm
))
8058 > TMPL_ARGS_DEPTH (args
))
8059 /* We don't have enough levels of args to do any substitution. This
8060 can happen in the context of -fnew-ttp-matching. */;
8061 else if (tree a
= type_uses_auto (t
))
8063 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
8064 if (t
== error_mark_node
)
8065 return error_mark_node
;
8068 t
= tsubst (t
, args
, complain
, in_decl
);
8070 if (invalid_nontype_parm_type_p (t
, complain
))
8071 return error_mark_node
;
8073 if (t
!= TREE_TYPE (parm
))
8074 t
= canonicalize_type_argument (t
, complain
);
8076 if (!type_dependent_expression_p (orig_arg
)
8077 && !uses_template_parms (t
))
8078 /* We used to call digest_init here. However, digest_init
8079 will report errors, which we don't want when complain
8080 is zero. More importantly, digest_init will try too
8081 hard to convert things: for example, `0' should not be
8082 converted to pointer type at this point according to
8083 the standard. Accepting this is not merely an
8084 extension, since deciding whether or not these
8085 conversions can occur is part of determining which
8086 function template to call, or whether a given explicit
8087 argument specification is valid. */
8088 val
= convert_nontype_argument (t
, orig_arg
, complain
);
8091 val
= canonicalize_expr_argument (orig_arg
, complain
);
8092 val
= maybe_convert_nontype_argument (t
, val
);
8096 if (val
== NULL_TREE
)
8097 val
= error_mark_node
;
8098 else if (val
== error_mark_node
&& (complain
& tf_error
))
8099 error ("could not convert template argument %qE from %qT to %qT",
8100 orig_arg
, TREE_TYPE (orig_arg
), t
);
8102 if (INDIRECT_REF_P (val
))
8104 /* Reject template arguments that are references to built-in
8105 functions with no library fallbacks. */
8106 const_tree inner
= TREE_OPERAND (val
, 0);
8107 const_tree innertype
= TREE_TYPE (inner
);
8109 && TYPE_REF_P (innertype
)
8110 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
8111 && TREE_OPERAND_LENGTH (inner
) > 0
8112 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
8113 return error_mark_node
;
8116 if (TREE_CODE (val
) == SCOPE_REF
)
8118 /* Strip typedefs from the SCOPE_REF. */
8119 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
8120 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
8122 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
8123 QUALIFIED_NAME_IS_TEMPLATE (val
));
8130 /* Coerces the remaining template arguments in INNER_ARGS (from
8131 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8132 Returns the coerced argument pack. PARM_IDX is the position of this
8133 parameter in the template parameter list. ARGS is the original
8134 template argument list. */
8136 coerce_template_parameter_pack (tree parms
,
8144 tsubst_flags_t complain
)
8146 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8147 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8150 tree packed_parms
= NULL_TREE
;
8152 if (arg_idx
> nargs
)
8155 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
8157 /* When the template parameter is a non-type template parameter pack
8158 or template template parameter pack whose type or template
8159 parameters use parameter packs, we know exactly how many arguments
8160 we are looking for. Build a vector of the instantiated decls for
8161 these template parameters in PACKED_PARMS. */
8162 /* We can't use make_pack_expansion here because it would interpret a
8163 _DECL as a use rather than a declaration. */
8164 tree decl
= TREE_VALUE (parm
);
8165 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
8166 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
8167 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
8168 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
8170 TREE_VEC_LENGTH (args
)--;
8171 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
8172 TREE_VEC_LENGTH (args
)++;
8174 if (packed_parms
== error_mark_node
)
8175 return error_mark_node
;
8177 /* If we're doing a partial instantiation of a member template,
8178 verify that all of the types used for the non-type
8179 template parameter pack are, in fact, valid for non-type
8180 template parameters. */
8182 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8184 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
8185 for (j
= 0; j
< len
; ++j
)
8187 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
8188 if (invalid_nontype_parm_type_p (t
, complain
))
8189 return error_mark_node
;
8191 /* We don't know how many args we have yet, just
8192 use the unconverted ones for now. */
8196 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
8198 /* Check if we have a placeholder pack, which indicates we're
8199 in the context of a introduction list. In that case we want
8200 to match this pack to the single placeholder. */
8201 else if (arg_idx
< nargs
8202 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
8203 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
8205 nargs
= arg_idx
+ 1;
8206 packed_args
= make_tree_vec (1);
8209 packed_args
= make_tree_vec (nargs
- arg_idx
);
8211 /* Convert the remaining arguments, which will be a part of the
8212 parameter pack "parm". */
8213 int first_pack_arg
= arg_idx
;
8214 for (; arg_idx
< nargs
; ++arg_idx
)
8216 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8217 tree actual_parm
= TREE_VALUE (parm
);
8218 int pack_idx
= arg_idx
- first_pack_arg
;
8222 /* Once we've packed as many args as we have types, stop. */
8223 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
8225 else if (PACK_EXPANSION_P (arg
))
8226 /* We don't know how many args we have yet, just
8227 use the unconverted ones for now. */
8230 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
8233 if (arg
== error_mark_node
)
8235 if (complain
& tf_error
)
8236 error ("template argument %d is invalid", arg_idx
+ 1);
8239 arg
= convert_template_argument (actual_parm
,
8240 arg
, new_args
, complain
, parm_idx
,
8242 if (arg
== error_mark_node
)
8244 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
8247 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
8248 && TREE_VEC_LENGTH (packed_args
) > 0)
8250 if (complain
& tf_error
)
8251 error ("wrong number of template arguments (%d, should be %d)",
8252 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
8253 return error_mark_node
;
8256 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
8257 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
8258 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8261 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
8262 TREE_CONSTANT (argument_pack
) = 1;
8265 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
8267 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
8268 TREE_VEC_LENGTH (packed_args
));
8269 return argument_pack
;
8272 /* Returns the number of pack expansions in the template argument vector
8276 pack_expansion_args_count (tree args
)
8281 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
8283 tree elt
= TREE_VEC_ELT (args
, i
);
8284 if (elt
&& PACK_EXPANSION_P (elt
))
8290 /* Convert all template arguments to their appropriate types, and
8291 return a vector containing the innermost resulting template
8292 arguments. If any error occurs, return error_mark_node. Error and
8293 warning messages are issued under control of COMPLAIN.
8295 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8296 for arguments not specified in ARGS. Otherwise, if
8297 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8298 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8299 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8303 coerce_template_parms (tree parms
,
8306 tsubst_flags_t complain
,
8307 bool require_all_args
,
8308 bool use_default_args
)
8310 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
8311 tree orig_inner_args
;
8314 tree new_inner_args
;
8316 /* When used as a boolean value, indicates whether this is a
8317 variadic template parameter list. Since it's an int, we can also
8318 subtract it from nparms to get the number of non-variadic
8321 int variadic_args_p
= 0;
8322 int post_variadic_parms
= 0;
8324 /* Adjustment to nparms for fixed parameter packs. */
8325 int fixed_pack_adjust
= 0;
8326 int fixed_packs
= 0;
8329 /* Likewise for parameters with default arguments. */
8332 if (args
== error_mark_node
)
8333 return error_mark_node
;
8335 nparms
= TREE_VEC_LENGTH (parms
);
8337 /* Determine if there are any parameter packs or default arguments. */
8338 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
8340 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8342 ++post_variadic_parms
;
8343 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8345 if (TREE_PURPOSE (parm
))
8349 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8350 /* If there are no parameters that follow a parameter pack, we need to
8351 expand any argument packs so that we can deduce a parameter pack from
8352 some non-packed args followed by an argument pack, as in variadic85.C.
8353 If there are such parameters, we need to leave argument packs intact
8354 so the arguments are assigned properly. This can happen when dealing
8355 with a nested class inside a partial specialization of a class
8356 template, as in variadic92.C, or when deducing a template parameter pack
8357 from a sub-declarator, as in variadic114.C. */
8358 if (!post_variadic_parms
)
8359 inner_args
= expand_template_argument_pack (inner_args
);
8361 /* Count any pack expansion args. */
8362 variadic_args_p
= pack_expansion_args_count (inner_args
);
8364 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8365 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8366 || (nargs
< nparms
- variadic_p
8369 && (!use_default_args
8370 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8371 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8374 if (complain
& tf_error
)
8376 if (variadic_p
|| default_p
)
8378 nparms
-= variadic_p
+ default_p
;
8379 error ("wrong number of template arguments "
8380 "(%d, should be at least %d)", nargs
, nparms
);
8383 error ("wrong number of template arguments "
8384 "(%d, should be %d)", nargs
, nparms
);
8387 inform (DECL_SOURCE_LOCATION (in_decl
),
8388 "provided for %qD", in_decl
);
8391 return error_mark_node
;
8393 /* We can't pass a pack expansion to a non-pack parameter of an alias
8394 template (DR 1430). */
8396 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8397 || concept_template_p (in_decl
))
8399 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8401 if (complain
& tf_error
)
8403 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8405 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8406 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8408 if (PACK_EXPANSION_P (arg
)
8409 && !template_parameter_pack_p (parm
))
8411 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8412 error_at (location_of (arg
),
8413 "pack expansion argument for non-pack parameter "
8414 "%qD of alias template %qD", parm
, in_decl
);
8416 error_at (location_of (arg
),
8417 "pack expansion argument for non-pack parameter "
8418 "%qD of concept %qD", parm
, in_decl
);
8419 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8426 return error_mark_node
;
8429 /* We need to evaluate the template arguments, even though this
8430 template-id may be nested within a "sizeof". */
8433 new_inner_args
= make_tree_vec (nparms
);
8434 new_args
= add_outermost_template_args (args
, new_inner_args
);
8435 int pack_adjust
= 0;
8436 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8441 /* Get the Ith template parameter. */
8442 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8444 if (parm
== error_mark_node
)
8446 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8450 /* Calculate the next argument. */
8451 if (arg_idx
< nargs
)
8452 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8456 if (template_parameter_pack_p (TREE_VALUE (parm
))
8457 && (arg
|| !(complain
& tf_partial
))
8458 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8460 /* Some arguments will be placed in the
8461 template parameter pack PARM. */
8462 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8463 inner_args
, arg_idx
,
8467 if (arg
== NULL_TREE
)
8469 /* We don't know how many args we have yet, just use the
8470 unconverted (and still packed) ones for now. */
8471 new_inner_args
= orig_inner_args
;
8476 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8478 /* Store this argument. */
8479 if (arg
== error_mark_node
)
8482 /* We are done with all of the arguments. */
8488 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8489 arg_idx
+= pack_adjust
;
8490 if (fixed_parameter_pack_p (TREE_VALUE (parm
)))
8493 fixed_pack_adjust
+= pack_adjust
;
8501 if (PACK_EXPANSION_P (arg
))
8503 /* "If every valid specialization of a variadic template
8504 requires an empty template parameter pack, the template is
8505 ill-formed, no diagnostic required." So check that the
8506 pattern works with this parameter. */
8507 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8508 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8512 if (conv
== error_mark_node
)
8514 if (complain
& tf_error
)
8515 inform (input_location
, "so any instantiation with a "
8516 "non-empty parameter pack would be ill-formed");
8519 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8520 /* Recover from missing typename. */
8521 TREE_VEC_ELT (inner_args
, arg_idx
)
8522 = make_pack_expansion (conv
, complain
);
8524 /* We don't know how many args we have yet, just
8525 use the unconverted ones for now. */
8526 new_inner_args
= inner_args
;
8531 else if (require_all_args
)
8533 /* There must be a default arg in this case. */
8534 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8536 /* The position of the first default template argument,
8537 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8539 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8541 arg_idx
- pack_adjust
);
8546 if (arg
== error_mark_node
)
8548 if (complain
& tf_error
)
8549 error ("template argument %d is invalid", arg_idx
+ 1);
8553 /* This can occur if there was an error in the template
8554 parameter list itself (which we would already have
8555 reported) that we are trying to recover from, e.g., a class
8556 template with a parameter list such as
8557 template<typename..., typename> (cpp0x/variadic150.C). */
8560 /* This can also happen with a fixed parameter pack (71834). */
8561 if (arg_idx
>= nargs
)
8565 arg
= convert_template_argument (TREE_VALUE (parm
),
8566 arg
, new_args
, complain
,
8569 if (arg
== error_mark_node
)
8571 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8574 if (missing
|| arg_idx
< nargs
- variadic_args_p
)
8576 /* If we had fixed parameter packs, we didn't know how many arguments we
8577 actually needed earlier; now we do. */
8578 nparms
+= fixed_pack_adjust
;
8579 variadic_p
-= fixed_packs
;
8583 if (arg_idx
< nargs
)
8585 /* We had some pack expansion arguments that will only work if the packs
8586 are empty, but wait until instantiation time to complain.
8587 See variadic-ttp3.C. */
8588 int len
= nparms
+ (nargs
- arg_idx
);
8589 tree args
= make_tree_vec (len
);
8591 for (; i
< nparms
; ++i
)
8592 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (new_inner_args
, i
);
8593 for (; i
< len
; ++i
, ++arg_idx
)
8594 TREE_VEC_ELT (args
, i
) = TREE_VEC_ELT (inner_args
,
8595 arg_idx
- pack_adjust
);
8596 new_inner_args
= args
;
8601 gcc_assert (!(complain
& tf_error
) || seen_error ());
8602 return error_mark_node
;
8605 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8606 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8607 TREE_VEC_LENGTH (new_inner_args
));
8609 return new_inner_args
;
8612 /* Convert all template arguments to their appropriate types, and
8613 return a vector containing the innermost resulting template
8614 arguments. If any error occurs, return error_mark_node. Error and
8615 warning messages are not issued.
8617 Note that no function argument deduction is performed, and default
8618 arguments are used to fill in unspecified arguments. */
8620 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8622 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8625 /* Convert all template arguments to their appropriate type, and
8626 instantiate default arguments as needed. This returns a vector
8627 containing the innermost resulting template arguments, or
8628 error_mark_node if unsuccessful. */
8630 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8631 tsubst_flags_t complain
)
8633 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8636 /* Like coerce_template_parms. If PARMS represents all template
8637 parameters levels, this function returns a vector of vectors
8638 representing all the resulting argument levels. Note that in this
8639 case, only the innermost arguments are coerced because the
8640 outermost ones are supposed to have been coerced already.
8642 Otherwise, if PARMS represents only (the innermost) vector of
8643 parameters, this function returns a vector containing just the
8644 innermost resulting arguments. */
8647 coerce_innermost_template_parms (tree parms
,
8650 tsubst_flags_t complain
,
8651 bool require_all_args
,
8652 bool use_default_args
)
8654 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8655 int args_depth
= TMPL_ARGS_DEPTH (args
);
8658 if (parms_depth
> 1)
8660 coerced_args
= make_tree_vec (parms_depth
);
8664 for (level
= parms
, cur_depth
= parms_depth
;
8665 parms_depth
> 0 && level
!= NULL_TREE
;
8666 level
= TREE_CHAIN (level
), --cur_depth
)
8669 if (cur_depth
== args_depth
)
8670 l
= coerce_template_parms (TREE_VALUE (level
),
8671 args
, in_decl
, complain
,
8675 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8677 if (l
== error_mark_node
)
8678 return error_mark_node
;
8680 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8684 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8685 args
, in_decl
, complain
,
8688 return coerced_args
;
8691 /* Returns 1 if template args OT and NT are equivalent. */
8694 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8698 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8700 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8703 if (TREE_CODE (nt
) == TREE_VEC
)
8704 /* For member templates */
8705 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8706 else if (PACK_EXPANSION_P (ot
))
8707 return (PACK_EXPANSION_P (nt
)
8708 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8709 PACK_EXPANSION_PATTERN (nt
))
8710 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8711 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8712 else if (ARGUMENT_PACK_P (ot
))
8717 if (!ARGUMENT_PACK_P (nt
))
8720 opack
= ARGUMENT_PACK_ARGS (ot
);
8721 npack
= ARGUMENT_PACK_ARGS (nt
);
8722 len
= TREE_VEC_LENGTH (opack
);
8723 if (TREE_VEC_LENGTH (npack
) != len
)
8725 for (i
= 0; i
< len
; ++i
)
8726 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8727 TREE_VEC_ELT (npack
, i
)))
8731 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8733 else if (TYPE_P (nt
))
8737 /* Don't treat an alias template specialization with dependent
8738 arguments as equivalent to its underlying type when used as a
8739 template argument; we need them to be distinct so that we
8740 substitute into the specialization arguments at instantiation
8741 time. And aliases can't be equivalent without being ==, so
8742 we don't need to look any deeper.
8744 During partial ordering, however, we need to treat them normally so
8745 that we can order uses of the same alias with different
8746 cv-qualification (79960). */
8748 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8751 return same_type_p (ot
, nt
);
8753 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8757 /* Try to treat a template non-type argument that has been converted
8758 to the parameter type as equivalent to one that hasn't yet. */
8759 for (enum tree_code code1
= TREE_CODE (ot
);
8760 CONVERT_EXPR_CODE_P (code1
)
8761 || code1
== NON_LVALUE_EXPR
;
8762 code1
= TREE_CODE (ot
))
8763 ot
= TREE_OPERAND (ot
, 0);
8764 for (enum tree_code code2
= TREE_CODE (nt
);
8765 CONVERT_EXPR_CODE_P (code2
)
8766 || code2
== NON_LVALUE_EXPR
;
8767 code2
= TREE_CODE (nt
))
8768 nt
= TREE_OPERAND (nt
, 0);
8770 return cp_tree_equal (ot
, nt
);
8774 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8775 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8776 NEWARG_PTR with the offending arguments if they are non-NULL. */
8779 comp_template_args (tree oldargs
, tree newargs
,
8780 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8785 if (oldargs
== newargs
)
8788 if (!oldargs
|| !newargs
)
8791 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8794 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8796 tree nt
= TREE_VEC_ELT (newargs
, i
);
8797 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8799 if (! template_args_equal (ot
, nt
, partial_order
))
8801 if (oldarg_ptr
!= NULL
)
8803 if (newarg_ptr
!= NULL
)
8812 comp_template_args_porder (tree oargs
, tree nargs
)
8814 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8817 /* Implement a freelist interface for objects of type T.
8819 Head is a separate object, rather than a regular member, so that we
8820 can define it as a GTY deletable pointer, which is highly
8821 desirable. A data member could be declared that way, but then the
8822 containing object would implicitly get GTY((user)), which would
8823 prevent us from instantiating freelists as global objects.
8824 Although this way we can create freelist global objects, they're
8825 such thin wrappers that instantiating temporaries at every use
8826 loses nothing and saves permanent storage for the freelist object.
8828 Member functions next, anew, poison and reinit have default
8829 implementations that work for most of the types we're interested
8830 in, but if they don't work for some type, they should be explicitly
8831 specialized. See the comments before them for requirements, and
8832 the example specializations for the tree_list_freelist. */
8833 template <typename T
>
8836 /* Return the next object in a chain. We could just do type
8837 punning, but if we access the object with its underlying type, we
8838 avoid strict-aliasing trouble. This needs only work between
8839 poison and reinit. */
8840 static T
*&next (T
*obj
) { return obj
->next
; }
8842 /* Return a newly allocated, uninitialized or minimally-initialized
8843 object of type T. Any initialization performed by anew should
8844 either remain across the life of the object and the execution of
8845 poison, or be redone by reinit. */
8846 static T
*anew () { return ggc_alloc
<T
> (); }
8848 /* Optionally scribble all over the bits holding the object, so that
8849 they become (mostly?) uninitialized memory. This is called while
8850 preparing to make the object part of the free list. */
8851 static void poison (T
*obj
) {
8852 T
*p ATTRIBUTE_UNUSED
= obj
;
8853 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8855 #ifdef ENABLE_GC_CHECKING
8856 /* Poison the data, to indicate the data is garbage. */
8857 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, sizeof (*p
)));
8858 memset (p
, 0xa5, sizeof (*p
));
8860 /* Let valgrind know the object is free. */
8861 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, sizeof (*p
)));
8863 /* Let valgrind know the next portion of the object is available,
8864 but uninitialized. */
8865 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8868 /* Bring an object that underwent at least one lifecycle after anew
8869 and before the most recent free and poison, back to a usable
8870 state, reinitializing whatever is needed for it to be
8871 functionally equivalent to an object just allocated and returned
8872 by anew. This may poison or clear the next field, used by
8873 freelist housekeeping after poison was called. */
8874 static void reinit (T
*obj
) {
8875 T
**q ATTRIBUTE_UNUSED
= &next (obj
);
8877 #ifdef ENABLE_GC_CHECKING
8878 memset (q
, 0xa5, sizeof (*q
));
8880 /* Let valgrind know the entire object is available, but
8882 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (*obj
)));
8885 /* Reference a GTY-deletable pointer that points to the first object
8886 in the free list proper. */
8889 /* Construct a freelist object chaining objects off of HEAD. */
8890 freelist (T
*&head
) : head(head
) {}
8892 /* Add OBJ to the free object list. The former head becomes OBJ's
8901 /* Take an object from the free list, if one is available, or
8902 allocate a new one. Objects taken from the free list should be
8903 regarded as filled with garbage, except for bits that are
8904 configured to be preserved across free and alloc. */
8919 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8920 want to allocate a TREE_LIST using the usual interface, and ensure
8921 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8922 build_tree_list logic in reinit, so this could go out of sync. */
8925 freelist
<tree_node
>::next (tree obj
)
8927 return TREE_CHAIN (obj
);
8931 freelist
<tree_node
>::anew ()
8933 return build_tree_list (NULL
, NULL
);
8937 freelist
<tree_node
>::poison (tree obj ATTRIBUTE_UNUSED
)
8939 int size ATTRIBUTE_UNUSED
= sizeof (tree_list
);
8940 tree p ATTRIBUTE_UNUSED
= obj
;
8941 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8942 tree
*q ATTRIBUTE_UNUSED
= &next (obj
);
8944 #ifdef ENABLE_GC_CHECKING
8945 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8947 /* Poison the data, to indicate the data is garbage. */
8948 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
8949 memset (p
, 0xa5, size
);
8951 /* Let valgrind know the object is free. */
8952 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
8953 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8954 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8955 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q
, sizeof (*q
)));
8957 #ifdef ENABLE_GC_CHECKING
8958 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b
, sizeof (*b
)));
8959 /* Keep TREE_CHAIN functional. */
8960 TREE_SET_CODE (obj
, TREE_LIST
);
8962 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8967 freelist
<tree_node
>::reinit (tree obj ATTRIBUTE_UNUSED
)
8969 tree_base
*b ATTRIBUTE_UNUSED
= &obj
->base
;
8971 #ifdef ENABLE_GC_CHECKING
8972 gcc_checking_assert (TREE_CODE (obj
) == TREE_LIST
);
8973 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8974 memset (obj
, 0, sizeof (tree_list
));
8977 /* Let valgrind know the entire object is available, but
8979 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj
, sizeof (tree_list
)));
8981 #ifdef ENABLE_GC_CHECKING
8982 TREE_SET_CODE (obj
, TREE_LIST
);
8984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b
, sizeof (*b
)));
8988 /* Point to the first object in the TREE_LIST freelist. */
8989 static GTY((deletable
)) tree tree_list_freelist_head
;
8990 /* Return the/an actual TREE_LIST freelist. */
8991 static inline freelist
<tree_node
>
8992 tree_list_freelist ()
8994 return tree_list_freelist_head
;
8997 /* Point to the first object in the tinst_level freelist. */
8998 static GTY((deletable
)) tinst_level
*tinst_level_freelist_head
;
8999 /* Return the/an actual tinst_level freelist. */
9000 static inline freelist
<tinst_level
>
9001 tinst_level_freelist ()
9003 return tinst_level_freelist_head
;
9006 /* Point to the first object in the pending_template freelist. */
9007 static GTY((deletable
)) pending_template
*pending_template_freelist_head
;
9008 /* Return the/an actual pending_template freelist. */
9009 static inline freelist
<pending_template
>
9010 pending_template_freelist ()
9012 return pending_template_freelist_head
;
9015 /* Build the TREE_LIST object out of a split list, store it
9016 permanently, and return it. */
9018 tinst_level::to_list ()
9020 gcc_assert (split_list_p ());
9021 tree ret
= tree_list_freelist ().alloc ();
9022 TREE_PURPOSE (ret
) = tldcl
;
9023 TREE_VALUE (ret
) = targs
;
9026 gcc_assert (tree_list_p ());
9030 const unsigned short tinst_level::refcount_infinity
;
9032 /* Increment OBJ's refcount unless it is already infinite. */
9033 static tinst_level
*
9034 inc_refcount_use (tinst_level
*obj
)
9036 if (obj
&& obj
->refcount
!= tinst_level::refcount_infinity
)
9041 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9043 tinst_level::free (tinst_level
*obj
)
9045 if (obj
->tree_list_p ())
9046 tree_list_freelist ().free (obj
->get_node ());
9047 tinst_level_freelist ().free (obj
);
9050 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9051 OBJ's DECL and OBJ, and start over with the tinst_level object that
9052 used to be referenced by OBJ's NEXT. */
9054 dec_refcount_use (tinst_level
*obj
)
9057 && obj
->refcount
!= tinst_level::refcount_infinity
9058 && !--obj
->refcount
)
9060 tinst_level
*next
= obj
->next
;
9061 tinst_level::free (obj
);
9066 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9067 and of the former PTR. Omitting the second argument is equivalent
9068 to passing (T*)NULL; this is allowed because passing the
9069 zero-valued integral constant NULL confuses type deduction and/or
9070 overload resolution. */
9071 template <typename T
>
9073 set_refcount_ptr (T
*& ptr
, T
*obj
= NULL
)
9076 ptr
= inc_refcount_use (obj
);
9077 dec_refcount_use (save
);
9081 add_pending_template (tree d
)
9083 tree ti
= (TYPE_P (d
)
9084 ? CLASSTYPE_TEMPLATE_INFO (d
)
9085 : DECL_TEMPLATE_INFO (d
));
9086 struct pending_template
*pt
;
9089 if (TI_PENDING_TEMPLATE_FLAG (ti
))
9092 /* We are called both from instantiate_decl, where we've already had a
9093 tinst_level pushed, and instantiate_template, where we haven't.
9095 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
9096 level
= !current_tinst_level
9097 || current_tinst_level
->maybe_get_node () != d
;
9100 push_tinst_level (d
);
9102 pt
= pending_template_freelist ().alloc ();
9105 set_refcount_ptr (pt
->tinst
, current_tinst_level
);
9106 if (last_pending_template
)
9107 last_pending_template
->next
= pt
;
9109 pending_templates
= pt
;
9111 last_pending_template
= pt
;
9113 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
9120 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9121 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9122 documentation for TEMPLATE_ID_EXPR. */
9125 lookup_template_function (tree fns
, tree arglist
)
9127 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
9128 return error_mark_node
;
9130 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
9132 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
9134 error ("%q#D is not a function template", fns
);
9135 return error_mark_node
;
9138 if (BASELINK_P (fns
))
9140 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
9142 BASELINK_FUNCTIONS (fns
),
9147 return build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, arglist
);
9150 /* Within the scope of a template class S<T>, the name S gets bound
9151 (in build_self_reference) to a TYPE_DECL for the class, not a
9152 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9153 or one of its enclosing classes, and that type is a template,
9154 return the associated TEMPLATE_DECL. Otherwise, the original
9157 Also handle the case when DECL is a TREE_LIST of ambiguous
9158 injected-class-names from different bases. */
9161 maybe_get_template_decl_from_type_decl (tree decl
)
9163 if (decl
== NULL_TREE
)
9166 /* DR 176: A lookup that finds an injected-class-name (10.2
9167 [class.member.lookup]) can result in an ambiguity in certain cases
9168 (for example, if it is found in more than one base class). If all of
9169 the injected-class-names that are found refer to specializations of
9170 the same class template, and if the name is followed by a
9171 template-argument-list, the reference refers to the class template
9172 itself and not a specialization thereof, and is not ambiguous. */
9173 if (TREE_CODE (decl
) == TREE_LIST
)
9175 tree t
, tmpl
= NULL_TREE
;
9176 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
9178 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
9181 else if (tmpl
!= elt
)
9184 if (tmpl
&& t
== NULL_TREE
)
9190 return (decl
!= NULL_TREE
9191 && DECL_SELF_REFERENCE_P (decl
)
9192 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
9193 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
9196 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9197 parameters, find the desired type.
9199 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9201 IN_DECL, if non-NULL, is the template declaration we are trying to
9204 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9205 the class we are looking up.
9207 Issue error and warning messages under control of COMPLAIN.
9209 If the template class is really a local class in a template
9210 function, then the FUNCTION_CONTEXT is the function in which it is
9213 ??? Note that this function is currently called *twice* for each
9214 template-id: the first time from the parser, while creating the
9215 incomplete type (finish_template_type), and the second type during the
9216 real instantiation (instantiate_template_class). This is surely something
9217 that we want to avoid. It also causes some problems with argument
9218 coercion (see convert_nontype_argument for more information on this). */
9221 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
9222 int entering_scope
, tsubst_flags_t complain
)
9224 tree templ
= NULL_TREE
, parmlist
;
9231 if (identifier_p (d1
))
9233 tree value
= innermost_non_namespace_value (d1
);
9234 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
9239 push_decl_namespace (context
);
9240 templ
= lookup_name (d1
);
9241 templ
= maybe_get_template_decl_from_type_decl (templ
);
9243 pop_decl_namespace ();
9246 context
= DECL_CONTEXT (templ
);
9248 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
9250 tree type
= TREE_TYPE (d1
);
9252 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9253 an implicit typename for the second A. Deal with it. */
9254 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
9255 type
= TREE_TYPE (type
);
9257 if (CLASSTYPE_TEMPLATE_INFO (type
))
9259 templ
= CLASSTYPE_TI_TEMPLATE (type
);
9260 d1
= DECL_NAME (templ
);
9263 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
9264 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
9266 templ
= TYPE_TI_TEMPLATE (d1
);
9267 d1
= DECL_NAME (templ
);
9269 else if (DECL_TYPE_TEMPLATE_P (d1
))
9272 d1
= DECL_NAME (templ
);
9273 context
= DECL_CONTEXT (templ
);
9275 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
9278 d1
= DECL_NAME (templ
);
9281 /* Issue an error message if we didn't find a template. */
9284 if (complain
& tf_error
)
9285 error ("%qT is not a template", d1
);
9286 return error_mark_node
;
9289 if (TREE_CODE (templ
) != TEMPLATE_DECL
9290 /* Make sure it's a user visible template, if it was named by
9292 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
9293 && !PRIMARY_TEMPLATE_P (templ
)))
9295 if (complain
& tf_error
)
9297 error ("non-template type %qT used as a template", d1
);
9299 error ("for template declaration %q+D", in_decl
);
9301 return error_mark_node
;
9304 complain
&= ~tf_user
;
9306 /* An alias that just changes the name of a template is equivalent to the
9307 other template, so if any of the arguments are pack expansions, strip
9308 the alias to avoid problems with a pack expansion passed to a non-pack
9309 alias template parameter (DR 1430). */
9310 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
9311 templ
= get_underlying_template (templ
);
9313 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
9316 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
9317 if (arglist2
== error_mark_node
9318 || (!uses_template_parms (arglist2
)
9319 && check_instantiated_args (templ
, arglist2
, complain
)))
9320 return error_mark_node
;
9322 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
9327 tree template_type
= TREE_TYPE (templ
);
9330 tree found
= NULL_TREE
;
9333 int is_dependent_type
;
9334 int use_partial_inst_tmpl
= false;
9336 if (template_type
== error_mark_node
)
9337 /* An error occurred while building the template TEMPL, and a
9338 diagnostic has most certainly been emitted for that
9339 already. Let's propagate that error. */
9340 return error_mark_node
;
9342 gen_tmpl
= most_general_template (templ
);
9343 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
9344 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
9345 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9347 if (arg_depth
== 1 && parm_depth
> 1)
9349 /* We've been given an incomplete set of template arguments.
9352 template <class T> struct S1 {
9353 template <class U> struct S2 {};
9354 template <class U> struct S2<U*> {};
9357 we will be called with an ARGLIST of `U*', but the
9358 TEMPLATE will be `template <class T> template
9359 <class U> struct S1<T>::S2'. We must fill in the missing
9361 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
9362 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
9363 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
9366 /* Now we should have enough arguments. */
9367 gcc_assert (parm_depth
== arg_depth
);
9369 /* From here on, we're only interested in the most general
9372 /* Calculate the BOUND_ARGS. These will be the args that are
9373 actually tsubst'd into the definition to create the
9375 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
9377 /*require_all_args=*/true,
9378 /*use_default_args=*/true);
9380 if (arglist
== error_mark_node
)
9381 /* We were unable to bind the arguments. */
9382 return error_mark_node
;
9384 /* In the scope of a template class, explicit references to the
9385 template class refer to the type of the template, not any
9386 instantiation of it. For example, in:
9388 template <class T> class C { void f(C<T>); }
9390 the `C<T>' is just the same as `C'. Outside of the
9391 class, however, such a reference is an instantiation. */
9393 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
9394 || currently_open_class (template_type
))
9396 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
9398 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
9399 return template_type
;
9402 /* If we already have this specialization, return it. */
9403 elt
.tmpl
= gen_tmpl
;
9405 elt
.spec
= NULL_TREE
;
9406 hash
= spec_hasher::hash (&elt
);
9407 entry
= type_specializations
->find_with_hash (&elt
, hash
);
9412 /* If the the template's constraints are not satisfied,
9413 then we cannot form a valid type.
9415 Note that the check is deferred until after the hash
9416 lookup. This prevents redundant checks on previously
9417 instantiated specializations. */
9418 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
9420 if (complain
& tf_error
)
9422 auto_diagnostic_group d
;
9423 error ("template constraint failure");
9424 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
9426 return error_mark_node
;
9429 is_dependent_type
= uses_template_parms (arglist
);
9431 /* If the deduced arguments are invalid, then the binding
9433 if (!is_dependent_type
9434 && check_instantiated_args (gen_tmpl
,
9435 INNERMOST_TEMPLATE_ARGS (arglist
),
9437 return error_mark_node
;
9439 if (!is_dependent_type
9440 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9441 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
9442 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
9444 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
9445 DECL_NAME (gen_tmpl
),
9446 /*tag_scope=*/ts_global
);
9450 context
= DECL_CONTEXT (gen_tmpl
);
9451 if (context
&& TYPE_P (context
))
9453 context
= tsubst_aggr_type (context
, arglist
, complain
, in_decl
, true);
9454 context
= complete_type (context
);
9457 context
= tsubst (context
, arglist
, complain
, in_decl
);
9459 if (context
== error_mark_node
)
9460 return error_mark_node
;
9463 context
= global_namespace
;
9465 /* Create the type. */
9466 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9468 /* The user referred to a specialization of an alias
9469 template represented by GEN_TMPL.
9471 [temp.alias]/2 says:
9473 When a template-id refers to the specialization of an
9474 alias template, it is equivalent to the associated
9475 type obtained by substitution of its
9476 template-arguments for the template-parameters in the
9477 type-id of the alias template. */
9479 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
9480 /* Note that the call above (by indirectly calling
9481 register_specialization in tsubst_decl) registers the
9482 TYPE_DECL representing the specialization of the alias
9483 template. So next time someone substitutes ARGLIST for
9484 the template parms into the alias template (GEN_TMPL),
9485 she'll get that TYPE_DECL back. */
9487 if (t
== error_mark_node
)
9490 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
9492 if (!is_dependent_type
)
9494 set_current_access_from_decl (TYPE_NAME (template_type
));
9495 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
9496 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
9497 arglist
, complain
, in_decl
),
9498 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
9499 arglist
, complain
, in_decl
),
9500 SCOPED_ENUM_P (template_type
), NULL
);
9502 if (t
== error_mark_node
)
9507 /* We don't want to call start_enum for this type, since
9508 the values for the enumeration constants may involve
9509 template parameters. And, no one should be interested
9510 in the enumeration constants for such a type. */
9511 t
= cxx_make_type (ENUMERAL_TYPE
);
9512 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
9514 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
9515 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
9516 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
9518 else if (CLASS_TYPE_P (template_type
))
9520 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9521 instantiated here. */
9522 gcc_assert (!LAMBDA_TYPE_P (template_type
));
9524 t
= make_class_type (TREE_CODE (template_type
));
9525 CLASSTYPE_DECLARED_CLASS (t
)
9526 = CLASSTYPE_DECLARED_CLASS (template_type
);
9527 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
9529 /* A local class. Make sure the decl gets registered properly. */
9530 if (context
== current_function_decl
)
9531 if (pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
)
9533 return error_mark_node
;
9535 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
9536 /* This instantiation is another name for the primary
9537 template type. Set the TYPE_CANONICAL field
9539 TYPE_CANONICAL (t
) = template_type
;
9540 else if (any_template_arguments_need_structural_equality_p (arglist
))
9541 /* Some of the template arguments require structural
9542 equality testing, so this template class requires
9543 structural equality testing. */
9544 SET_TYPE_STRUCTURAL_EQUALITY (t
);
9549 /* If we called start_enum or pushtag above, this information
9550 will already be set up. */
9553 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
9555 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
9556 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
9557 DECL_SOURCE_LOCATION (type_decl
)
9558 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
9561 type_decl
= TYPE_NAME (t
);
9563 if (CLASS_TYPE_P (template_type
))
9565 TREE_PRIVATE (type_decl
)
9566 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
9567 TREE_PROTECTED (type_decl
)
9568 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
9569 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
9571 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
9572 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
9576 if (OVERLOAD_TYPE_P (t
)
9577 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9579 static const char *tags
[] = {"abi_tag", "may_alias"};
9581 for (unsigned ix
= 0; ix
!= 2; ix
++)
9584 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
9588 = tree_cons (TREE_PURPOSE (attributes
),
9589 TREE_VALUE (attributes
),
9590 TYPE_ATTRIBUTES (t
));
9594 /* Let's consider the explicit specialization of a member
9595 of a class template specialization that is implicitly instantiated,
9600 template<class U> struct M {}; //#0
9605 struct S<int>::M<char> //#1
9609 [temp.expl.spec]/4 says this is valid.
9611 In this case, when we write:
9614 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9617 When we encounter #1, we want to store the partial instantiation
9618 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9620 For all cases other than this "explicit specialization of member of a
9621 class template", we just want to store the most general template into
9622 the CLASSTYPE_TI_TEMPLATE of M.
9624 This case of "explicit specialization of member of a class template"
9626 1/ the enclosing class is an instantiation of, and therefore not
9627 the same as, the context of the most general template, and
9628 2/ we aren't looking at the partial instantiation itself, i.e.
9629 the innermost arguments are not the same as the innermost parms of
9630 the most general template.
9632 So it's only when 1/ and 2/ happens that we want to use the partial
9633 instantiation of the member template in lieu of its most general
9636 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
9637 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
9638 /* the enclosing class must be an instantiation... */
9639 && CLASS_TYPE_P (context
)
9640 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9642 TREE_VEC_LENGTH (arglist
)--;
9643 ++processing_template_decl
;
9644 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9645 tree partial_inst_args
=
9646 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9647 arglist
, complain
, NULL_TREE
);
9648 --processing_template_decl
;
9649 TREE_VEC_LENGTH (arglist
)++;
9650 if (partial_inst_args
== error_mark_node
)
9651 return error_mark_node
;
9652 use_partial_inst_tmpl
=
9653 /*...and we must not be looking at the partial instantiation
9655 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9659 if (!use_partial_inst_tmpl
)
9660 /* This case is easy; there are no member templates involved. */
9664 /* This is a full instantiation of a member template. Find
9665 the partial instantiation of which this is an instance. */
9667 /* Temporarily reduce by one the number of levels in the ARGLIST
9668 so as to avoid comparing the last set of arguments. */
9669 TREE_VEC_LENGTH (arglist
)--;
9670 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9671 TREE_VEC_LENGTH (arglist
)++;
9672 /* FOUND is either a proper class type, or an alias
9673 template specialization. In the later case, it's a
9674 TYPE_DECL, resulting from the substituting of arguments
9675 for parameters in the TYPE_DECL of the alias template
9676 done earlier. So be careful while getting the template
9678 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9680 : (TREE_CODE (found
) == TYPE_DECL
9681 ? DECL_TI_TEMPLATE (found
)
9682 : CLASSTYPE_TI_TEMPLATE (found
)));
9684 if (DECL_CLASS_TEMPLATE_P (found
)
9685 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found
)))
9687 /* If this partial instantiation is specialized, we want to
9688 use it for hash table lookup. */
9690 elt
.args
= arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
9691 hash
= spec_hasher::hash (&elt
);
9695 // Build template info for the new specialization.
9696 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9699 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9700 gcc_checking_assert (*slot
== NULL
);
9701 entry
= ggc_alloc
<spec_entry
> ();
9705 /* Note this use of the partial instantiation so we can check it
9706 later in maybe_process_partial_specialization. */
9707 DECL_TEMPLATE_INSTANTIATIONS (found
)
9708 = tree_cons (arglist
, t
,
9709 DECL_TEMPLATE_INSTANTIATIONS (found
));
9711 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9712 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9713 /* Now that the type has been registered on the instantiations
9714 list, we set up the enumerators. Because the enumeration
9715 constants may involve the enumeration type itself, we make
9716 sure to register the type first, and then create the
9717 constants. That way, doing tsubst_expr for the enumeration
9718 constants won't result in recursive calls here; we'll find
9719 the instantiation and exit above. */
9720 tsubst_enum (template_type
, t
, arglist
);
9722 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9723 /* If the type makes use of template parameters, the
9724 code that generates debugging information will crash. */
9725 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9727 /* Possibly limit visibility based on template args. */
9728 TREE_PUBLIC (type_decl
) = 1;
9729 determine_visibility (type_decl
);
9731 inherit_targ_abi_tags (t
);
9737 /* Wrapper for lookup_template_class_1. */
9740 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9741 int entering_scope
, tsubst_flags_t complain
)
9744 timevar_push (TV_TEMPLATE_INST
);
9745 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9746 entering_scope
, complain
);
9747 timevar_pop (TV_TEMPLATE_INST
);
9751 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9754 lookup_template_variable (tree templ
, tree arglist
)
9756 /* The type of the expression is NULL_TREE since the template-id could refer
9757 to an explicit or partial specialization. */
9758 tree type
= NULL_TREE
;
9759 if (flag_concepts
&& variable_concept_p (templ
))
9760 /* Except that concepts are always bool. */
9761 type
= boolean_type_node
;
9762 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9765 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9768 finish_template_variable (tree var
, tsubst_flags_t complain
)
9770 tree templ
= TREE_OPERAND (var
, 0);
9771 tree arglist
= TREE_OPERAND (var
, 1);
9773 /* We never want to return a VAR_DECL for a variable concept, since they
9774 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9775 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9776 if (concept_p
&& processing_template_decl
)
9779 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9780 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9782 templ
= most_general_template (templ
);
9783 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9784 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9786 /*use_default*/true);
9788 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9790 if (complain
& tf_error
)
9792 auto_diagnostic_group d
;
9793 error ("use of invalid variable template %qE", var
);
9794 diagnose_constraints (location_of (var
), templ
, arglist
);
9796 return error_mark_node
;
9799 /* If a template-id refers to a specialization of a variable
9800 concept, then the expression is true if and only if the
9801 concept's constraints are satisfied by the given template
9804 NOTE: This is an extension of Concepts Lite TS that
9805 allows constraints to be used in expressions. */
9808 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9809 return evaluate_variable_concept (decl
, arglist
);
9812 return instantiate_template (templ
, arglist
, complain
);
9815 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9816 TARGS template args, and instantiate it if it's not dependent. */
9819 lookup_and_finish_template_variable (tree templ
, tree targs
,
9820 tsubst_flags_t complain
)
9822 templ
= lookup_template_variable (templ
, targs
);
9823 if (!any_dependent_template_arguments_p (targs
))
9825 templ
= finish_template_variable (templ
, complain
);
9829 return convert_from_reference (templ
);
9838 /* True when we should also visit template parameters that occur in
9839 non-deduced contexts. */
9840 bool include_nondeduced_p
;
9841 hash_set
<tree
> *visited
;
9844 /* Called from for_each_template_parm via walk_tree. */
9847 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9850 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9851 tree_fn_t fn
= pfd
->fn
;
9852 void *data
= pfd
->data
;
9853 tree result
= NULL_TREE
;
9855 #define WALK_SUBTREE(NODE) \
9858 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9859 pfd->include_nondeduced_p, \
9861 if (result) goto out; \
9865 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9869 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9870 WALK_SUBTREE (TYPE_CONTEXT (t
));
9872 switch (TREE_CODE (t
))
9875 if (TYPE_PTRMEMFUNC_P (t
))
9881 if (!TYPE_TEMPLATE_INFO (t
))
9884 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9888 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9889 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9893 /* Since we're not going to walk subtrees, we have to do this
9895 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9899 /* Check the return type. */
9900 WALK_SUBTREE (TREE_TYPE (t
));
9902 /* Check the parameter types. Since default arguments are not
9903 instantiated until they are needed, the TYPE_ARG_TYPES may
9904 contain expressions that involve template parameters. But,
9905 no-one should be looking at them yet. And, once they're
9906 instantiated, they don't contain template parameters, so
9907 there's no point in looking at them then, either. */
9911 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9912 WALK_SUBTREE (TREE_VALUE (parm
));
9914 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9915 want walk_tree walking into them itself. */
9919 if (flag_noexcept_type
)
9921 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9923 WALK_SUBTREE (TREE_PURPOSE (spec
));
9929 case UNDERLYING_TYPE
:
9930 if (pfd
->include_nondeduced_p
9931 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9933 pfd
->include_nondeduced_p
,
9935 return error_mark_node
;
9936 *walk_subtrees
= false;
9941 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9942 WALK_SUBTREE (DECL_TI_ARGS (t
));
9947 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9948 WALK_SUBTREE (DECL_INITIAL (t
));
9949 if (DECL_CONTEXT (t
)
9950 && pfd
->include_nondeduced_p
)
9951 WALK_SUBTREE (DECL_CONTEXT (t
));
9954 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9955 /* Record template parameters such as `T' inside `TT<T>'. */
9956 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9959 case TEMPLATE_TEMPLATE_PARM
:
9960 case TEMPLATE_TYPE_PARM
:
9961 case TEMPLATE_PARM_INDEX
:
9962 if (fn
&& (*fn
)(t
, data
))
9969 /* A template template parameter is encountered. */
9970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9971 WALK_SUBTREE (TREE_TYPE (t
));
9973 /* Already substituted template template parameter */
9978 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9979 partial instantiation. */
9980 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9984 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9985 && pfd
->include_nondeduced_p
)
9986 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9991 /* If there's no type, then this thing must be some expression
9992 involving template parameters. */
9993 if (!fn
&& !TREE_TYPE (t
))
9994 return error_mark_node
;
9999 case IMPLICIT_CONV_EXPR
:
10000 case REINTERPRET_CAST_EXPR
:
10001 case CONST_CAST_EXPR
:
10002 case STATIC_CAST_EXPR
:
10003 case DYNAMIC_CAST_EXPR
:
10007 case PSEUDO_DTOR_EXPR
:
10009 return error_mark_node
;
10016 #undef WALK_SUBTREE
10018 /* We didn't find any template parameters we liked. */
10023 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10024 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10025 call FN with the parameter and the DATA.
10026 If FN returns nonzero, the iteration is terminated, and
10027 for_each_template_parm returns 1. Otherwise, the iteration
10028 continues. If FN never returns a nonzero value, the value
10029 returned by for_each_template_parm is 0. If FN is NULL, it is
10030 considered to be the function which always returns 1.
10032 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10033 parameters that occur in non-deduced contexts. When false, only
10034 visits those template parameters that can be deduced. */
10037 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
10038 hash_set
<tree
> *visited
,
10039 bool include_nondeduced_p
,
10042 struct pair_fn_data pfd
;
10047 pfd
.any_fn
= any_fn
;
10049 pfd
.include_nondeduced_p
= include_nondeduced_p
;
10051 /* Walk the tree. (Conceptually, we would like to walk without
10052 duplicates, but for_each_template_parm_r recursively calls
10053 for_each_template_parm, so we would need to reorganize a fair
10054 bit to use walk_tree_without_duplicates, so we keep our own
10057 pfd
.visited
= visited
;
10059 pfd
.visited
= new hash_set
<tree
>;
10060 result
= cp_walk_tree (&t
,
10061 for_each_template_parm_r
,
10068 delete pfd
.visited
;
10075 /* Returns true if T depends on any template parameter. */
10078 uses_template_parms (tree t
)
10080 if (t
== NULL_TREE
)
10084 int saved_processing_template_decl
;
10086 saved_processing_template_decl
= processing_template_decl
;
10087 if (!saved_processing_template_decl
)
10088 processing_template_decl
= 1;
10090 dependent_p
= dependent_type_p (t
);
10091 else if (TREE_CODE (t
) == TREE_VEC
)
10092 dependent_p
= any_dependent_template_arguments_p (t
);
10093 else if (TREE_CODE (t
) == TREE_LIST
)
10094 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
10095 || uses_template_parms (TREE_CHAIN (t
)));
10096 else if (TREE_CODE (t
) == TYPE_DECL
)
10097 dependent_p
= dependent_type_p (TREE_TYPE (t
));
10098 else if (DECL_P (t
)
10100 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
10101 || TREE_CODE (t
) == OVERLOAD
10103 || identifier_p (t
)
10104 || TREE_CODE (t
) == TRAIT_EXPR
10105 || TREE_CODE (t
) == CONSTRUCTOR
10106 || CONSTANT_CLASS_P (t
))
10107 dependent_p
= (type_dependent_expression_p (t
)
10108 || value_dependent_expression_p (t
));
10111 gcc_assert (t
== error_mark_node
);
10112 dependent_p
= false;
10115 processing_template_decl
= saved_processing_template_decl
;
10117 return dependent_p
;
10120 /* Returns true iff current_function_decl is an incompletely instantiated
10121 template. Useful instead of processing_template_decl because the latter
10122 is set to 0 during instantiate_non_dependent_expr. */
10125 in_template_function (void)
10127 tree fn
= current_function_decl
;
10129 ++processing_template_decl
;
10130 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
10131 && DECL_TEMPLATE_INFO (fn
)
10132 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
10133 --processing_template_decl
;
10137 /* Returns true if T depends on any template parameter with level LEVEL. */
10140 uses_template_parms_level (tree t
, int level
)
10142 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
10143 /*include_nondeduced_p=*/true);
10146 /* Returns true if the signature of DECL depends on any template parameter from
10147 its enclosing class. */
10150 uses_outer_template_parms (tree decl
)
10152 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
10155 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
10156 &depth
, NULL
, /*include_nondeduced_p=*/true))
10158 if (PRIMARY_TEMPLATE_P (decl
)
10159 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10160 (DECL_TEMPLATE_PARMS (decl
)),
10161 template_parm_outer_level
,
10162 &depth
, NULL
, /*include_nondeduced_p=*/true))
10164 tree ci
= get_constraints (decl
);
10166 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
10167 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
10168 &depth
, NULL
, /*nondeduced*/true))
10173 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10174 ill-formed translation unit, i.e. a variable or function that isn't
10175 usable in a constant expression. */
10178 neglectable_inst_p (tree d
)
10180 return (d
&& DECL_P (d
)
10181 && !undeduced_auto_decl (d
)
10182 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
10183 : decl_maybe_constant_var_p (d
)));
10186 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10187 neglectable and instantiated from within an erroneous instantiation. */
10190 limit_bad_template_recursion (tree decl
)
10192 struct tinst_level
*lev
= current_tinst_level
;
10193 int errs
= errorcount
+ sorrycount
;
10194 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
10197 for (; lev
; lev
= lev
->next
)
10198 if (neglectable_inst_p (lev
->maybe_get_node ()))
10201 return (lev
&& errs
> lev
->errors
);
10204 static int tinst_depth
;
10205 extern int max_tinst_depth
;
10208 static GTY(()) struct tinst_level
*last_error_tinst_level
;
10210 /* We're starting to instantiate D; record the template instantiation context
10211 at LOC for diagnostics and to restore it later. */
10214 push_tinst_level_loc (tree tldcl
, tree targs
, location_t loc
)
10216 struct tinst_level
*new_level
;
10218 if (tinst_depth
>= max_tinst_depth
)
10220 /* Tell error.c not to try to instantiate any templates. */
10222 fatal_error (input_location
,
10223 "template instantiation depth exceeds maximum of %d"
10224 " (use %<-ftemplate-depth=%> to increase the maximum)",
10229 /* If the current instantiation caused problems, don't let it instantiate
10230 anything else. Do allow deduction substitution and decls usable in
10231 constant expressions. */
10232 if (!targs
&& limit_bad_template_recursion (tldcl
))
10235 /* When not -quiet, dump template instantiations other than functions, since
10236 announce_function will take care of those. */
10237 if (!quiet_flag
&& !targs
10238 && TREE_CODE (tldcl
) != TREE_LIST
10239 && TREE_CODE (tldcl
) != FUNCTION_DECL
)
10240 fprintf (stderr
, " %s", decl_as_string (tldcl
, TFF_DECL_SPECIFIERS
));
10242 new_level
= tinst_level_freelist ().alloc ();
10243 new_level
->tldcl
= tldcl
;
10244 new_level
->targs
= targs
;
10245 new_level
->locus
= loc
;
10246 new_level
->errors
= errorcount
+ sorrycount
;
10247 new_level
->next
= NULL
;
10248 new_level
->refcount
= 0;
10249 set_refcount_ptr (new_level
->next
, current_tinst_level
);
10250 set_refcount_ptr (current_tinst_level
, new_level
);
10253 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
10254 depth_reached
= tinst_depth
;
10259 /* We're starting substitution of TMPL<ARGS>; record the template
10260 substitution context for diagnostics and to restore it later. */
10263 push_tinst_level (tree tmpl
, tree args
)
10265 return push_tinst_level_loc (tmpl
, args
, input_location
);
10268 /* We're starting to instantiate D; record INPUT_LOCATION and the
10269 template instantiation context for diagnostics and to restore it
10273 push_tinst_level (tree d
)
10275 return push_tinst_level_loc (d
, input_location
);
10278 /* Likewise, but record LOC as the program location. */
10281 push_tinst_level_loc (tree d
, location_t loc
)
10283 gcc_assert (TREE_CODE (d
) != TREE_LIST
);
10284 return push_tinst_level_loc (d
, NULL
, loc
);
10287 /* We're done instantiating this template; return to the instantiation
10291 pop_tinst_level (void)
10293 /* Restore the filename and line number stashed away when we started
10294 this instantiation. */
10295 input_location
= current_tinst_level
->locus
;
10296 set_refcount_ptr (current_tinst_level
, current_tinst_level
->next
);
10300 /* We're instantiating a deferred template; restore the template
10301 instantiation context in which the instantiation was requested, which
10302 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10305 reopen_tinst_level (struct tinst_level
*level
)
10307 struct tinst_level
*t
;
10310 for (t
= level
; t
; t
= t
->next
)
10313 set_refcount_ptr (current_tinst_level
, level
);
10314 pop_tinst_level ();
10315 if (current_tinst_level
)
10316 current_tinst_level
->errors
= errorcount
+sorrycount
;
10317 return level
->maybe_get_node ();
10320 /* Returns the TINST_LEVEL which gives the original instantiation
10323 struct tinst_level
*
10324 outermost_tinst_level (void)
10326 struct tinst_level
*level
= current_tinst_level
;
10328 while (level
->next
)
10329 level
= level
->next
;
10333 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10334 vector of template arguments, as for tsubst.
10336 Returns an appropriate tsubst'd friend declaration. */
10339 tsubst_friend_function (tree decl
, tree args
)
10343 if (TREE_CODE (decl
) == FUNCTION_DECL
10344 && DECL_TEMPLATE_INSTANTIATION (decl
)
10345 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
10346 /* This was a friend declared with an explicit template
10347 argument list, e.g.:
10349 friend void f<>(T);
10351 to indicate that f was a template instantiation, not a new
10352 function declaration. Now, we have to figure out what
10353 instantiation of what template. */
10355 tree template_id
, arglist
, fns
;
10358 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
10360 /* Friend functions are looked up in the containing namespace scope.
10361 We must enter that scope, to avoid finding member functions of the
10362 current class with same name. */
10363 push_nested_namespace (ns
);
10364 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
10365 tf_warning_or_error
, NULL_TREE
,
10366 /*integral_constant_expression_p=*/false);
10367 pop_nested_namespace (ns
);
10368 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
10369 tf_warning_or_error
, NULL_TREE
);
10370 template_id
= lookup_template_function (fns
, arglist
);
10372 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10373 tmpl
= determine_specialization (template_id
, new_friend
,
10375 /*need_member_template=*/0,
10376 TREE_VEC_LENGTH (args
),
10378 return instantiate_template (tmpl
, new_args
, tf_error
);
10381 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
10383 /* The NEW_FRIEND will look like an instantiation, to the
10384 compiler, but is not an instantiation from the point of view of
10385 the language. For example, we might have had:
10387 template <class T> struct S {
10388 template <class U> friend void f(T, U);
10391 Then, in S<int>, template <class U> void f(int, U) is not an
10392 instantiation of anything. */
10393 if (new_friend
== error_mark_node
)
10394 return error_mark_node
;
10396 DECL_USE_TEMPLATE (new_friend
) = 0;
10397 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
10399 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
10400 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
10401 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
10404 /* The mangled name for the NEW_FRIEND is incorrect. The function
10405 is not a template instantiation and should not be mangled like
10406 one. Therefore, we forget the mangling here; we'll recompute it
10407 later if we need it. */
10408 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
10410 SET_DECL_RTL (new_friend
, NULL
);
10411 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
10414 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
10417 tree new_friend_template_info
;
10418 tree new_friend_result_template_info
;
10420 int new_friend_is_defn
;
10422 /* We must save some information from NEW_FRIEND before calling
10423 duplicate decls since that function will free NEW_FRIEND if
10425 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
10426 new_friend_is_defn
=
10427 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10428 (template_for_substitution (new_friend
)))
10430 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
10432 /* This declaration is a `primary' template. */
10433 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
10435 new_friend_result_template_info
10436 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
10439 new_friend_result_template_info
= NULL_TREE
;
10441 /* Inside pushdecl_namespace_level, we will push into the
10442 current namespace. However, the friend function should go
10443 into the namespace of the template. */
10444 ns
= decl_namespace_context (new_friend
);
10445 push_nested_namespace (ns
);
10446 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
10447 pop_nested_namespace (ns
);
10449 if (old_decl
== error_mark_node
)
10450 return error_mark_node
;
10452 if (old_decl
!= new_friend
)
10454 /* This new friend declaration matched an existing
10455 declaration. For example, given:
10457 template <class T> void f(T);
10458 template <class U> class C {
10459 template <class T> friend void f(T) {}
10462 the friend declaration actually provides the definition
10463 of `f', once C has been instantiated for some type. So,
10464 old_decl will be the out-of-class template declaration,
10465 while new_friend is the in-class definition.
10467 But, if `f' was called before this point, the
10468 instantiation of `f' will have DECL_TI_ARGS corresponding
10469 to `T' but not to `U', references to which might appear
10470 in the definition of `f'. Previously, the most general
10471 template for an instantiation of `f' was the out-of-class
10472 version; now it is the in-class version. Therefore, we
10473 run through all specialization of `f', adding to their
10474 DECL_TI_ARGS appropriately. In particular, they need a
10475 new set of outer arguments, corresponding to the
10476 arguments for this class instantiation.
10478 The same situation can arise with something like this:
10480 friend void f(int);
10481 template <class T> class C {
10482 friend void f(T) {}
10485 when `C<int>' is instantiated. Now, `f(int)' is defined
10488 if (!new_friend_is_defn
)
10489 /* On the other hand, if the in-class declaration does
10490 *not* provide a definition, then we don't want to alter
10491 existing definitions. We can just leave everything
10496 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
10497 tree new_args
= TI_ARGS (new_friend_template_info
);
10499 /* Overwrite whatever template info was there before, if
10500 any, with the new template information pertaining to
10501 the declaration. */
10502 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
10504 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
10506 /* We should have called reregister_specialization in
10507 duplicate_decls. */
10508 gcc_assert (retrieve_specialization (new_template
,
10512 /* Instantiate it if the global has already been used. */
10513 if (DECL_ODR_USED (old_decl
))
10514 instantiate_decl (old_decl
, /*defer_ok=*/true,
10515 /*expl_inst_class_mem_p=*/false);
10521 /* Indicate that the old function template is a partial
10523 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
10524 = new_friend_result_template_info
;
10526 gcc_assert (new_template
10527 == most_general_template (new_template
));
10528 gcc_assert (new_template
!= old_decl
);
10530 /* Reassign any specializations already in the hash table
10531 to the new more general template, and add the
10532 additional template args. */
10533 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
10535 t
= TREE_CHAIN (t
))
10537 tree spec
= TREE_VALUE (t
);
10540 elt
.tmpl
= old_decl
;
10541 elt
.args
= DECL_TI_ARGS (spec
);
10542 elt
.spec
= NULL_TREE
;
10544 decl_specializations
->remove_elt (&elt
);
10546 DECL_TI_ARGS (spec
)
10547 = add_outermost_template_args (new_args
,
10548 DECL_TI_ARGS (spec
));
10550 register_specialization
10551 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
10554 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
10558 /* The information from NEW_FRIEND has been merged into OLD_DECL
10559 by duplicate_decls. */
10560 new_friend
= old_decl
;
10565 tree context
= DECL_CONTEXT (new_friend
);
10569 template <class T> class C {
10570 template <class U> friend void C1<U>::f (); // case 1
10571 friend void C2<T>::f (); // case 2
10573 we only need to make sure CONTEXT is a complete type for
10574 case 2. To distinguish between the two cases, we note that
10575 CONTEXT of case 1 remains dependent type after tsubst while
10576 this isn't true for case 2. */
10577 ++processing_template_decl
;
10578 dependent_p
= dependent_type_p (context
);
10579 --processing_template_decl
;
10582 && !complete_type_or_else (context
, NULL_TREE
))
10583 return error_mark_node
;
10585 if (COMPLETE_TYPE_P (context
))
10587 tree fn
= new_friend
;
10588 /* do_friend adds the TEMPLATE_DECL for any member friend
10589 template even if it isn't a member template, i.e.
10590 template <class T> friend A<T>::f();
10591 Look through it in that case. */
10592 if (TREE_CODE (fn
) == TEMPLATE_DECL
10593 && !PRIMARY_TEMPLATE_P (fn
))
10594 fn
= DECL_TEMPLATE_RESULT (fn
);
10595 /* Check to see that the declaration is really present, and,
10596 possibly obtain an improved declaration. */
10597 fn
= check_classfn (context
, fn
, NULL_TREE
);
10607 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10608 template arguments, as for tsubst.
10610 Returns an appropriate tsubst'd friend type or error_mark_node on
10614 tsubst_friend_class (tree friend_tmpl
, tree args
)
10618 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
10620 tmpl
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
10621 return TREE_TYPE (tmpl
);
10624 tree context
= CP_DECL_CONTEXT (friend_tmpl
);
10625 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10626 push_nested_namespace (context
);
10629 context
= tsubst (context
, args
, tf_error
, NULL_TREE
);
10630 push_nested_class (context
);
10633 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), /*prefer_type=*/false,
10634 /*non_class=*/false, /*block_p=*/false,
10635 /*namespaces_only=*/false, LOOKUP_HIDDEN
);
10637 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
10639 /* The friend template has already been declared. Just
10640 check to see that the declarations match, and install any new
10641 default parameters. We must tsubst the default parameters,
10642 of course. We only need the innermost template parameters
10643 because that is all that redeclare_class_template will look
10645 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
10646 > TMPL_ARGS_DEPTH (args
))
10648 tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10649 args
, tf_warning_or_error
);
10650 location_t saved_input_location
= input_location
;
10651 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10652 tree cons
= get_constraints (tmpl
);
10653 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10654 input_location
= saved_input_location
;
10659 /* The friend template has not already been declared. In this
10660 case, the instantiation of the template class will cause the
10661 injection of this template into the namespace scope. */
10662 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10664 if (tmpl
!= error_mark_node
)
10666 /* The new TMPL is not an instantiation of anything, so we
10667 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10668 for the new type because that is supposed to be the
10669 corresponding template decl, i.e., TMPL. */
10670 DECL_USE_TEMPLATE (tmpl
) = 0;
10671 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10672 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10673 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10674 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10676 /* It is hidden. */
10677 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl
));
10678 DECL_ANTICIPATED (tmpl
)
10679 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl
)) = true;
10681 /* Inject this template into the enclosing namspace scope. */
10682 tmpl
= pushdecl_namespace_level (tmpl
, true);
10686 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10687 pop_nested_namespace (context
);
10689 pop_nested_class ();
10691 return TREE_TYPE (tmpl
);
10694 /* Returns zero if TYPE cannot be completed later due to circularity.
10695 Otherwise returns one. */
10698 can_complete_type_without_circularity (tree type
)
10700 if (type
== NULL_TREE
|| type
== error_mark_node
)
10702 else if (COMPLETE_TYPE_P (type
))
10704 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10705 return can_complete_type_without_circularity (TREE_TYPE (type
));
10706 else if (CLASS_TYPE_P (type
)
10707 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10713 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10714 tsubst_flags_t
, tree
);
10716 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10717 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10720 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10721 tsubst_flags_t complain
, tree in_decl
)
10723 gcc_assert (ATTR_IS_DEPENDENT (t
));
10725 tree val
= TREE_VALUE (t
);
10726 if (val
== NULL_TREE
)
10727 /* Nothing to do. */;
10728 else if ((flag_openmp
|| flag_openmp_simd
)
10729 && is_attribute_p ("omp declare simd",
10730 get_attribute_name (t
)))
10732 tree clauses
= TREE_VALUE (val
);
10733 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10734 complain
, in_decl
);
10735 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10736 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10737 tree parms
= DECL_ARGUMENTS (*decl_p
);
10739 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10741 val
= build_tree_list (NULL_TREE
, clauses
);
10745 /* If the first attribute argument is an identifier, don't
10746 pass it through tsubst. Attributes like mode, format,
10747 cleanup and several target specific attributes expect it
10749 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10752 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10753 /*integral_constant_expression_p=*/false);
10754 if (chain
!= TREE_CHAIN (val
))
10755 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10757 else if (PACK_EXPANSION_P (val
))
10759 /* An attribute pack expansion. */
10760 tree purp
= TREE_PURPOSE (t
);
10761 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10762 if (pack
== error_mark_node
)
10763 return error_mark_node
;
10764 int len
= TREE_VEC_LENGTH (pack
);
10765 tree list
= NULL_TREE
;
10767 for (int i
= 0; i
< len
; ++i
)
10769 tree elt
= TREE_VEC_ELT (pack
, i
);
10770 *q
= build_tree_list (purp
, elt
);
10771 q
= &TREE_CHAIN (*q
);
10776 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10777 /*integral_constant_expression_p=*/false);
10779 if (val
!= TREE_VALUE (t
))
10780 return build_tree_list (TREE_PURPOSE (t
), val
);
10784 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10785 unchanged or a new TREE_LIST chain. */
10788 tsubst_attributes (tree attributes
, tree args
,
10789 tsubst_flags_t complain
, tree in_decl
)
10791 tree last_dep
= NULL_TREE
;
10793 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10794 if (ATTR_IS_DEPENDENT (t
))
10797 attributes
= copy_list (attributes
);
10802 for (tree
*p
= &attributes
; *p
; )
10805 if (ATTR_IS_DEPENDENT (t
))
10807 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10812 p
= &TREE_CHAIN (*p
);
10813 *p
= TREE_CHAIN (t
);
10817 p
= &TREE_CHAIN (*p
);
10823 /* Apply any attributes which had to be deferred until instantiation
10824 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10825 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10828 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10829 tree args
, tsubst_flags_t complain
, tree in_decl
)
10831 tree last_dep
= NULL_TREE
;
10835 if (attributes
== NULL_TREE
)
10838 if (DECL_P (*decl_p
))
10840 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10842 p
= &DECL_ATTRIBUTES (*decl_p
);
10843 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10844 to our attributes parameter. */
10845 gcc_assert (*p
== attributes
);
10849 p
= &TYPE_ATTRIBUTES (*decl_p
);
10850 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10851 lookup_template_class_1, and should be preserved. */
10852 gcc_assert (*p
!= attributes
);
10854 p
= &TREE_CHAIN (*p
);
10857 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10858 if (ATTR_IS_DEPENDENT (t
))
10861 attributes
= copy_list (attributes
);
10868 tree late_attrs
= NULL_TREE
;
10869 tree
*q
= &late_attrs
;
10874 if (ATTR_IS_DEPENDENT (t
))
10876 *p
= TREE_CHAIN (t
);
10877 TREE_CHAIN (t
) = NULL_TREE
;
10878 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10880 q
= &TREE_CHAIN (*q
);
10883 p
= &TREE_CHAIN (t
);
10886 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10890 /* Perform (or defer) access check for typedefs that were referenced
10891 from within the template TMPL code.
10892 This is a subroutine of instantiate_decl and instantiate_class_template.
10893 TMPL is the template to consider and TARGS is the list of arguments of
10897 perform_typedefs_access_check (tree tmpl
, tree targs
)
10899 location_t saved_location
;
10901 qualified_typedef_usage_t
*iter
;
10904 || (!CLASS_TYPE_P (tmpl
)
10905 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10908 saved_location
= input_location
;
10909 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10911 tree type_decl
= iter
->typedef_decl
;
10912 tree type_scope
= iter
->context
;
10914 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10917 if (uses_template_parms (type_decl
))
10918 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10919 if (uses_template_parms (type_scope
))
10920 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10922 /* Make access check error messages point to the location
10923 of the use of the typedef. */
10924 input_location
= iter
->locus
;
10925 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10926 type_decl
, type_decl
,
10927 tf_warning_or_error
);
10929 input_location
= saved_location
;
10933 instantiate_class_template_1 (tree type
)
10935 tree templ
, args
, pattern
, t
, member
;
10939 unsigned int saved_maximum_field_alignment
;
10942 if (type
== error_mark_node
)
10943 return error_mark_node
;
10945 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10946 || uses_template_parms (type
))
10949 /* Figure out which template is being instantiated. */
10950 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10951 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10953 /* Mark the type as in the process of being defined. */
10954 TYPE_BEING_DEFINED (type
) = 1;
10956 /* We may be in the middle of deferred access check. Disable
10958 deferring_access_check_sentinel
acs (dk_no_deferred
);
10960 /* Determine what specialization of the original template to
10962 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10963 if (t
== error_mark_node
)
10964 return error_mark_node
;
10967 /* This TYPE is actually an instantiation of a partial
10968 specialization. We replace the innermost set of ARGS with
10969 the arguments appropriate for substitution. For example,
10972 template <class T> struct S {};
10973 template <class T> struct S<T*> {};
10975 and supposing that we are instantiating S<int*>, ARGS will
10976 presently be {int*} -- but we need {int}. */
10977 pattern
= TREE_TYPE (t
);
10978 args
= TREE_PURPOSE (t
);
10982 pattern
= TREE_TYPE (templ
);
10983 args
= CLASSTYPE_TI_ARGS (type
);
10986 /* If the template we're instantiating is incomplete, then clearly
10987 there's nothing we can do. */
10988 if (!COMPLETE_TYPE_P (pattern
))
10990 /* We can try again later. */
10991 TYPE_BEING_DEFINED (type
) = 0;
10995 /* If we've recursively instantiated too many templates, stop. */
10996 if (! push_tinst_level (type
))
10999 int saved_unevaluated_operand
= cp_unevaluated_operand
;
11000 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11002 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
11003 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11004 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
11005 fn_context
= error_mark_node
;
11007 push_to_top_level ();
11010 cp_unevaluated_operand
= 0;
11011 c_inhibit_evaluation_warnings
= 0;
11013 /* Use #pragma pack from the template context. */
11014 saved_maximum_field_alignment
= maximum_field_alignment
;
11015 maximum_field_alignment
= TYPE_PRECISION (pattern
);
11017 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
11019 /* Set the input location to the most specialized template definition.
11020 This is needed if tsubsting causes an error. */
11021 typedecl
= TYPE_MAIN_DECL (pattern
);
11022 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
11023 DECL_SOURCE_LOCATION (typedecl
);
11025 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
11026 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
11027 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
11028 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
11029 if (ANON_AGGR_TYPE_P (pattern
))
11030 SET_ANON_AGGR_TYPE_P (type
);
11031 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
11033 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
11034 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
11035 /* Adjust visibility for template arguments. */
11036 determine_visibility (TYPE_MAIN_DECL (type
));
11038 if (CLASS_TYPE_P (type
))
11039 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
11041 pbinfo
= TYPE_BINFO (pattern
);
11043 /* We should never instantiate a nested class before its enclosing
11044 class; we need to look up the nested class by name before we can
11045 instantiate it, and that lookup should instantiate the enclosing
11047 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
11048 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
11050 base_list
= NULL_TREE
;
11051 if (BINFO_N_BASE_BINFOS (pbinfo
))
11057 /* We must enter the scope containing the type, as that is where
11058 the accessibility of types named in dependent bases are
11060 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
11062 /* Substitute into each of the bases to determine the actual
11064 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
11067 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
11068 tree expanded_bases
= NULL_TREE
;
11071 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
11074 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
11075 args
, tf_error
, NULL_TREE
);
11076 if (expanded_bases
== error_mark_node
)
11079 len
= TREE_VEC_LENGTH (expanded_bases
);
11082 for (idx
= 0; idx
< len
; idx
++)
11084 if (expanded_bases
)
11085 /* Extract the already-expanded base class. */
11086 base
= TREE_VEC_ELT (expanded_bases
, idx
);
11088 /* Substitute to figure out the base class. */
11089 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
11092 if (base
== error_mark_node
)
11095 base_list
= tree_cons (access
, base
, base_list
);
11096 if (BINFO_VIRTUAL_P (pbase_binfo
))
11097 TREE_TYPE (base_list
) = integer_type_node
;
11101 /* The list is now in reverse order; correct that. */
11102 base_list
= nreverse (base_list
);
11105 pop_scope (pushed_scope
);
11107 /* Now call xref_basetypes to set up all the base-class
11109 xref_basetypes (type
, base_list
);
11111 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
11112 (int) ATTR_FLAG_TYPE_IN_PLACE
,
11113 args
, tf_error
, NULL_TREE
);
11114 fixup_attribute_variants (type
);
11116 /* Now that our base classes are set up, enter the scope of the
11117 class, so that name lookups into base classes, etc. will work
11118 correctly. This is precisely analogous to what we do in
11119 begin_class_definition when defining an ordinary non-template
11120 class, except we also need to push the enclosing classes. */
11121 push_nested_class (type
);
11123 /* Now members are processed in the order of declaration. */
11124 for (member
= CLASSTYPE_DECL_LIST (pattern
);
11125 member
; member
= TREE_CHAIN (member
))
11127 tree t
= TREE_VALUE (member
);
11129 if (TREE_PURPOSE (member
))
11133 if (LAMBDA_TYPE_P (t
))
11134 /* A closure type for a lambda in an NSDMI or default argument.
11135 Ignore it; it will be regenerated when needed. */
11138 /* Build new CLASSTYPE_NESTED_UTDS. */
11141 bool class_template_p
;
11143 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
11144 && TYPE_LANG_SPECIFIC (t
)
11145 && CLASSTYPE_IS_TEMPLATE (t
));
11146 /* If the member is a class template, then -- even after
11147 substitution -- there may be dependent types in the
11148 template argument list for the class. We increment
11149 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11150 that function will assume that no types are dependent
11151 when outside of a template. */
11152 if (class_template_p
)
11153 ++processing_template_decl
;
11154 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11155 if (class_template_p
)
11156 --processing_template_decl
;
11157 if (newtag
== error_mark_node
)
11160 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
11162 tree name
= TYPE_IDENTIFIER (t
);
11164 if (class_template_p
)
11165 /* Unfortunately, lookup_template_class sets
11166 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11167 instantiation (i.e., for the type of a member
11168 template class nested within a template class.)
11169 This behavior is required for
11170 maybe_process_partial_specialization to work
11171 correctly, but is not accurate in this case;
11172 the TAG is not an instantiation of anything.
11173 (The corresponding TEMPLATE_DECL is an
11174 instantiation, but the TYPE is not.) */
11175 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
11177 /* Now, we call pushtag to put this NEWTAG into the scope of
11178 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11179 pushtag calling push_template_decl. We don't have to do
11180 this for enums because it will already have been done in
11183 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
11184 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
11187 else if (DECL_DECLARES_FUNCTION_P (t
))
11191 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11192 ++processing_template_decl
;
11193 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
11194 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11195 --processing_template_decl
;
11196 set_current_access_from_decl (r
);
11197 finish_member_declaration (r
);
11198 /* Instantiate members marked with attribute used. */
11199 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11201 if (TREE_CODE (r
) == FUNCTION_DECL
11202 && DECL_OMP_DECLARE_REDUCTION_P (r
))
11203 cp_check_omp_declare_reduction (r
);
11205 else if ((DECL_CLASS_TEMPLATE_P (t
) || DECL_IMPLICIT_TYPEDEF_P (t
))
11206 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
11207 /* A closure type for a lambda in an NSDMI or default argument.
11208 Ignore it; it will be regenerated when needed. */;
11211 /* Build new TYPE_FIELDS. */
11212 if (TREE_CODE (t
) == STATIC_ASSERT
)
11216 ++c_inhibit_evaluation_warnings
;
11218 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
11219 tf_warning_or_error
, NULL_TREE
,
11220 /*integral_constant_expression_p=*/true);
11221 --c_inhibit_evaluation_warnings
;
11223 finish_static_assert (condition
,
11224 STATIC_ASSERT_MESSAGE (t
),
11225 STATIC_ASSERT_SOURCE_LOCATION (t
),
11226 /*member_p=*/true);
11228 else if (TREE_CODE (t
) != CONST_DECL
)
11231 tree vec
= NULL_TREE
;
11234 /* The file and line for this declaration, to
11235 assist in error message reporting. Since we
11236 called push_tinst_level above, we don't need to
11238 input_location
= DECL_SOURCE_LOCATION (t
);
11240 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11241 ++processing_template_decl
;
11242 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
11243 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11244 --processing_template_decl
;
11246 if (TREE_CODE (r
) == TREE_VEC
)
11248 /* A capture pack became multiple fields. */
11250 len
= TREE_VEC_LENGTH (vec
);
11253 for (int i
= 0; i
< len
; ++i
)
11256 r
= TREE_VEC_ELT (vec
, i
);
11261 [t]he initialization (and any associated
11262 side-effects) of a static data member does
11263 not occur unless the static data member is
11264 itself used in a way that requires the
11265 definition of the static data member to
11268 Therefore, we do not substitute into the
11269 initialized for the static data member here. */
11270 finish_static_data_member_decl
11272 /*init=*/NULL_TREE
,
11273 /*init_const_expr_p=*/false,
11274 /*asmspec_tree=*/NULL_TREE
,
11276 /* Instantiate members marked with attribute used. */
11277 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
11280 else if (TREE_CODE (r
) == FIELD_DECL
)
11282 /* Determine whether R has a valid type and can be
11283 completed later. If R is invalid, then its type
11284 is replaced by error_mark_node. */
11285 tree rtype
= TREE_TYPE (r
);
11286 if (can_complete_type_without_circularity (rtype
))
11287 complete_type (rtype
);
11289 if (!complete_or_array_type_p (rtype
))
11291 /* If R's type couldn't be completed and
11292 it isn't a flexible array member (whose
11293 type is incomplete by definition) give
11295 cxx_incomplete_type_error (r
, rtype
);
11296 TREE_TYPE (r
) = error_mark_node
;
11298 else if (TREE_CODE (rtype
) == ARRAY_TYPE
11299 && TYPE_DOMAIN (rtype
) == NULL_TREE
11300 && (TREE_CODE (type
) == UNION_TYPE
11301 || TREE_CODE (type
) == QUAL_UNION_TYPE
))
11303 error ("flexible array member %qD in union", r
);
11304 TREE_TYPE (r
) = error_mark_node
;
11308 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11309 such a thing will already have been added to the field
11310 list by tsubst_enum in finish_member_declaration in the
11311 CLASSTYPE_NESTED_UTDS case above. */
11312 if (!(TREE_CODE (r
) == TYPE_DECL
11313 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
11314 && DECL_ARTIFICIAL (r
)))
11316 set_current_access_from_decl (r
);
11317 finish_member_declaration (r
);
11325 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
11326 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11328 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11330 tree friend_type
= t
;
11331 bool adjust_processing_template_decl
= false;
11333 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11335 /* template <class T> friend class C; */
11336 friend_type
= tsubst_friend_class (friend_type
, args
);
11337 adjust_processing_template_decl
= true;
11339 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
11341 /* template <class T> friend class C::D; */
11342 friend_type
= tsubst (friend_type
, args
,
11343 tf_warning_or_error
, NULL_TREE
);
11344 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
11345 friend_type
= TREE_TYPE (friend_type
);
11346 adjust_processing_template_decl
= true;
11348 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
11349 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
11351 /* This could be either
11355 when dependent_type_p is false or
11357 template <class U> friend class T::C;
11360 /* Bump processing_template_decl in case this is something like
11361 template <class T> friend struct A<T>::B. */
11362 ++processing_template_decl
;
11363 friend_type
= tsubst (friend_type
, args
,
11364 tf_warning_or_error
, NULL_TREE
);
11365 if (dependent_type_p (friend_type
))
11366 adjust_processing_template_decl
= true;
11367 --processing_template_decl
;
11369 else if (TREE_CODE (friend_type
) != BOUND_TEMPLATE_TEMPLATE_PARM
11370 && !CLASSTYPE_USE_TEMPLATE (friend_type
)
11371 && TYPE_HIDDEN_P (friend_type
))
11375 where C hasn't been declared yet. Let's lookup name
11376 from namespace scope directly, bypassing any name that
11377 come from dependent base class. */
11378 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
11380 /* The call to xref_tag_from_type does injection for friend
11382 push_nested_namespace (ns
);
11384 xref_tag_from_type (friend_type
, NULL_TREE
,
11385 /*tag_scope=*/ts_current
);
11386 pop_nested_namespace (ns
);
11388 else if (uses_template_parms (friend_type
))
11389 /* friend class C<T>; */
11390 friend_type
= tsubst (friend_type
, args
,
11391 tf_warning_or_error
, NULL_TREE
);
11396 where C is already declared or
11398 friend class C<int>;
11400 We don't have to do anything in these cases. */
11402 if (adjust_processing_template_decl
)
11403 /* Trick make_friend_class into realizing that the friend
11404 we're adding is a template, not an ordinary class. It's
11405 important that we use make_friend_class since it will
11406 perform some error-checking and output cross-reference
11408 ++processing_template_decl
;
11410 if (friend_type
!= error_mark_node
)
11411 make_friend_class (type
, friend_type
, /*complain=*/false);
11413 if (adjust_processing_template_decl
)
11414 --processing_template_decl
;
11418 /* Build new DECL_FRIENDLIST. */
11421 /* The file and line for this declaration, to
11422 assist in error message reporting. Since we
11423 called push_tinst_level above, we don't need to
11425 input_location
= DECL_SOURCE_LOCATION (t
);
11427 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11429 ++processing_template_decl
;
11430 push_deferring_access_checks (dk_no_check
);
11433 r
= tsubst_friend_function (t
, args
);
11434 add_friend (type
, r
, /*complain=*/false);
11435 if (TREE_CODE (t
) == TEMPLATE_DECL
)
11437 pop_deferring_access_checks ();
11438 --processing_template_decl
;
11446 /* Restore these before substituting into the lambda capture
11448 cp_unevaluated_operand
= saved_unevaluated_operand
;
11449 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11452 /* Set the file and line number information to whatever is given for
11453 the class itself. This puts error messages involving generated
11454 implicit functions at a predictable point, and the same point
11455 that would be used for non-template classes. */
11456 input_location
= DECL_SOURCE_LOCATION (typedecl
);
11458 unreverse_member_declarations (type
);
11459 finish_struct_1 (type
);
11460 TYPE_BEING_DEFINED (type
) = 0;
11462 /* We don't instantiate default arguments for member functions. 14.7.1:
11464 The implicit instantiation of a class template specialization causes
11465 the implicit instantiation of the declarations, but not of the
11466 definitions or default arguments, of the class member functions,
11467 member classes, static data members and member templates.... */
11469 /* Some typedefs referenced from within the template code need to be access
11470 checked at template instantiation time, i.e now. These types were
11471 added to the template at parsing time. Let's get those and perform
11472 the access checks then. */
11473 perform_typedefs_access_check (pattern
, args
);
11474 perform_deferred_access_checks (tf_warning_or_error
);
11475 pop_nested_class ();
11476 maximum_field_alignment
= saved_maximum_field_alignment
;
11478 pop_from_top_level ();
11479 pop_tinst_level ();
11481 /* The vtable for a template class can be emitted in any translation
11482 unit in which the class is instantiated. When there is no key
11483 method, however, finish_struct_1 will already have added TYPE to
11484 the keyed_classes. */
11485 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
11486 vec_safe_push (keyed_classes
, type
);
11491 /* Wrapper for instantiate_class_template_1. */
11494 instantiate_class_template (tree type
)
11497 timevar_push (TV_TEMPLATE_INST
);
11498 ret
= instantiate_class_template_1 (type
);
11499 timevar_pop (TV_TEMPLATE_INST
);
11504 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11510 else if (TYPE_P (t
))
11511 r
= tsubst (t
, args
, complain
, in_decl
);
11514 if (!(complain
& tf_warning
))
11515 ++c_inhibit_evaluation_warnings
;
11516 r
= tsubst_expr (t
, args
, complain
, in_decl
,
11517 /*integral_constant_expression_p=*/true);
11518 if (!(complain
& tf_warning
))
11519 --c_inhibit_evaluation_warnings
;
11524 /* Given a function parameter pack TMPL_PARM and some function parameters
11525 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11526 and set *SPEC_P to point at the next point in the list. */
11529 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
11531 /* Collect all of the extra "packed" parameters into an
11534 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
11535 tree spec_parm
= *spec_p
;
11538 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
11540 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
11543 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11544 parmvec
= make_tree_vec (len
);
11545 spec_parm
= *spec_p
;
11546 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
11548 tree elt
= spec_parm
;
11549 if (DECL_PACK_P (elt
))
11550 elt
= make_pack_expansion (elt
);
11551 TREE_VEC_ELT (parmvec
, i
) = elt
;
11554 /* Build the argument packs. */
11555 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
11556 *spec_p
= spec_parm
;
11561 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11562 NONTYPE_ARGUMENT_PACK. */
11565 make_fnparm_pack (tree spec_parm
)
11567 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
11570 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11571 pack expansion with no extra args, 2 if it has extra args, or 0
11572 if it is not a pack expansion. */
11575 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
11577 if (TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11578 /* We're being called before this happens in tsubst_pack_expansion. */
11579 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11580 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
11581 if (i
>= TREE_VEC_LENGTH (vec
))
11583 tree elt
= TREE_VEC_ELT (vec
, i
);
11585 /* A decl pack is itself an expansion. */
11586 elt
= TREE_TYPE (elt
);
11587 if (!PACK_EXPANSION_P (elt
))
11589 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
11595 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11598 make_argument_pack_select (tree arg_pack
, unsigned index
)
11600 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
11602 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
11603 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11608 /* This is a subroutine of tsubst_pack_expansion.
11610 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11611 mechanism to store the (non complete list of) arguments of the
11612 substitution and return a non substituted pack expansion, in order
11613 to wait for when we have enough arguments to really perform the
11617 use_pack_expansion_extra_args_p (tree parm_packs
,
11619 bool has_empty_arg
)
11621 /* If one pack has an expansion and another pack has a normal
11622 argument or if one pack has an empty argument and an another
11623 one hasn't then tsubst_pack_expansion cannot perform the
11624 substitution and need to fall back on the
11625 PACK_EXPANSION_EXTRA mechanism. */
11626 if (parm_packs
== NULL_TREE
)
11628 else if (has_empty_arg
)
11631 bool has_expansion_arg
= false;
11632 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11634 bool has_non_expansion_arg
= false;
11635 for (tree parm_pack
= parm_packs
;
11637 parm_pack
= TREE_CHAIN (parm_pack
))
11639 tree arg
= TREE_VALUE (parm_pack
);
11641 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11643 /* We can't substitute a pack expansion with extra args into
11647 has_expansion_arg
= true;
11649 has_non_expansion_arg
= true;
11652 if (has_expansion_arg
&& has_non_expansion_arg
)
11658 /* [temp.variadic]/6 says that:
11660 The instantiation of a pack expansion [...]
11661 produces a list E1,E2, ..., En, where N is the number of elements
11662 in the pack expansion parameters.
11664 This subroutine of tsubst_pack_expansion produces one of these Ei.
11666 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11667 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11668 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11669 INDEX is the index 'i' of the element Ei to produce. ARGS,
11670 COMPLAIN, and IN_DECL are the same parameters as for the
11671 tsubst_pack_expansion function.
11673 The function returns the resulting Ei upon successful completion,
11674 or error_mark_node.
11676 Note that this function possibly modifies the ARGS parameter, so
11677 it's the responsibility of the caller to restore it. */
11680 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11683 tree args
/* This parm gets
11685 tsubst_flags_t complain
,
11689 bool ith_elem_is_expansion
= false;
11691 /* For each parameter pack, change the substitution of the parameter
11692 pack to the ith argument in its argument pack, then expand the
11694 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11696 tree parm
= TREE_PURPOSE (pack
);
11697 tree arg_pack
= TREE_VALUE (pack
);
11698 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11700 ith_elem_is_expansion
|=
11701 argument_pack_element_is_expansion_p (arg_pack
, index
);
11703 /* Select the Ith argument from the pack. */
11704 if (TREE_CODE (parm
) == PARM_DECL
11706 || TREE_CODE (parm
) == FIELD_DECL
)
11710 aps
= make_argument_pack_select (arg_pack
, index
);
11711 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11712 return error_mark_node
;
11713 register_local_specialization (aps
, parm
);
11716 aps
= retrieve_local_specialization (parm
);
11721 template_parm_level_and_index (parm
, &level
, &idx
);
11725 aps
= make_argument_pack_select (arg_pack
, index
);
11726 /* Update the corresponding argument. */
11727 TMPL_ARG (args
, level
, idx
) = aps
;
11730 /* Re-use the ARGUMENT_PACK_SELECT. */
11731 aps
= TMPL_ARG (args
, level
, idx
);
11733 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11736 // Any local specialization bindings arising from this substitution
11737 // cannot be reused for a different INDEX.
11738 local_specialization_stack
lss (lss_copy
);
11740 /* Substitute into the PATTERN with the (possibly altered)
11742 if (pattern
== in_decl
)
11743 /* Expanding a fixed parameter pack from
11744 coerce_template_parameter_pack. */
11745 t
= tsubst_decl (pattern
, args
, complain
);
11746 else if (pattern
== error_mark_node
)
11747 t
= error_mark_node
;
11748 else if (constraint_p (pattern
))
11750 if (processing_template_decl
)
11751 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11753 t
= (constraints_satisfied_p (pattern
, args
)
11754 ? boolean_true_node
: boolean_false_node
);
11756 else if (!TYPE_P (pattern
))
11757 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11758 /*integral_constant_expression_p=*/false);
11760 t
= tsubst (pattern
, args
, complain
, in_decl
);
11762 /* If the Ith argument pack element is a pack expansion, then
11763 the Ith element resulting from the substituting is going to
11764 be a pack expansion as well. */
11765 if (ith_elem_is_expansion
)
11766 t
= make_pack_expansion (t
, complain
);
11771 /* When the unexpanded parameter pack in a fold expression expands to an empty
11772 sequence, the value of the expression is as follows; the program is
11773 ill-formed if the operator is not listed in this table.
11780 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11782 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11783 if (!FOLD_EXPR_MODIFY_P (t
))
11786 case TRUTH_ANDIF_EXPR
:
11787 return boolean_true_node
;
11788 case TRUTH_ORIF_EXPR
:
11789 return boolean_false_node
;
11790 case COMPOUND_EXPR
:
11796 if (complain
& tf_error
)
11797 error_at (location_of (t
),
11798 "fold of empty expansion over %O", code
);
11799 return error_mark_node
;
11802 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11803 form an expression that combines the two terms using the
11807 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11809 tree op
= FOLD_EXPR_OP (t
);
11810 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11812 // Handle compound assignment operators.
11813 if (FOLD_EXPR_MODIFY_P (t
))
11814 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11818 case COMPOUND_EXPR
:
11819 return build_x_compound_expr (input_location
, left
, right
, complain
);
11821 return build_x_binary_op (input_location
, code
,
11822 left
, TREE_CODE (left
),
11823 right
, TREE_CODE (right
),
11829 /* Substitute ARGS into the pack of a fold expression T. */
11832 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11834 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11837 /* Substitute ARGS into the pack of a fold expression T. */
11840 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11842 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11845 /* Expand a PACK of arguments into a grouped as left fold.
11846 Given a pack containing elements A0, A1, ..., An and an
11847 operator @, this builds the expression:
11849 ((A0 @ A1) @ A2) ... @ An
11851 Note that PACK must not be empty.
11853 The operator is defined by the original fold expression T. */
11856 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11858 tree left
= TREE_VEC_ELT (pack
, 0);
11859 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11861 tree right
= TREE_VEC_ELT (pack
, i
);
11862 left
= fold_expression (t
, left
, right
, complain
);
11867 /* Substitute into a unary left fold expression. */
11870 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11873 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11874 if (pack
== error_mark_node
)
11875 return error_mark_node
;
11876 if (PACK_EXPANSION_P (pack
))
11878 tree r
= copy_node (t
);
11879 FOLD_EXPR_PACK (r
) = pack
;
11882 if (TREE_VEC_LENGTH (pack
) == 0)
11883 return expand_empty_fold (t
, complain
);
11885 return expand_left_fold (t
, pack
, complain
);
11888 /* Substitute into a binary left fold expression.
11890 Do ths by building a single (non-empty) vector of argumnts and
11891 building the expression from those elements. */
11894 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11897 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11898 if (pack
== error_mark_node
)
11899 return error_mark_node
;
11900 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11901 if (init
== error_mark_node
)
11902 return error_mark_node
;
11904 if (PACK_EXPANSION_P (pack
))
11906 tree r
= copy_node (t
);
11907 FOLD_EXPR_PACK (r
) = pack
;
11908 FOLD_EXPR_INIT (r
) = init
;
11912 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11913 TREE_VEC_ELT (vec
, 0) = init
;
11914 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11915 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11917 return expand_left_fold (t
, vec
, complain
);
11920 /* Expand a PACK of arguments into a grouped as right fold.
11921 Given a pack containing elementns A0, A1, ..., and an
11922 operator @, this builds the expression:
11924 A0@ ... (An-2 @ (An-1 @ An))
11926 Note that PACK must not be empty.
11928 The operator is defined by the original fold expression T. */
11931 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11933 // Build the expression.
11934 int n
= TREE_VEC_LENGTH (pack
);
11935 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11936 for (--n
; n
!= 0; --n
)
11938 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11939 right
= fold_expression (t
, left
, right
, complain
);
11944 /* Substitute into a unary right fold expression. */
11947 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11950 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11951 if (pack
== error_mark_node
)
11952 return error_mark_node
;
11953 if (PACK_EXPANSION_P (pack
))
11955 tree r
= copy_node (t
);
11956 FOLD_EXPR_PACK (r
) = pack
;
11959 if (TREE_VEC_LENGTH (pack
) == 0)
11960 return expand_empty_fold (t
, complain
);
11962 return expand_right_fold (t
, pack
, complain
);
11965 /* Substitute into a binary right fold expression.
11967 Do ths by building a single (non-empty) vector of arguments and
11968 building the expression from those elements. */
11971 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11974 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11975 if (pack
== error_mark_node
)
11976 return error_mark_node
;
11977 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11978 if (init
== error_mark_node
)
11979 return error_mark_node
;
11981 if (PACK_EXPANSION_P (pack
))
11983 tree r
= copy_node (t
);
11984 FOLD_EXPR_PACK (r
) = pack
;
11985 FOLD_EXPR_INIT (r
) = init
;
11989 int n
= TREE_VEC_LENGTH (pack
);
11990 tree vec
= make_tree_vec (n
+ 1);
11991 for (int i
= 0; i
< n
; ++i
)
11992 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11993 TREE_VEC_ELT (vec
, n
) = init
;
11995 return expand_right_fold (t
, vec
, complain
);
11998 /* Walk through the pattern of a pack expansion, adding everything in
11999 local_specializations to a list. */
12003 hash_set
<tree
> internal
;
12005 tsubst_flags_t complain
;
12007 el_data (tsubst_flags_t c
)
12008 : extra (NULL_TREE
), complain (c
) {}
12011 extract_locals_r (tree
*tp
, int */
*walk_subtrees*/
, void *data_
)
12013 el_data
&data
= *reinterpret_cast<el_data
*>(data_
);
12014 tree
*extra
= &data
.extra
;
12015 tsubst_flags_t complain
= data
.complain
;
12017 if (TYPE_P (*tp
) && typedef_variant_p (*tp
))
12018 /* Remember local typedefs (85214). */
12019 tp
= &TYPE_NAME (*tp
);
12021 if (TREE_CODE (*tp
) == DECL_EXPR
)
12022 data
.internal
.add (DECL_EXPR_DECL (*tp
));
12023 else if (tree spec
= retrieve_local_specialization (*tp
))
12025 if (data
.internal
.contains (*tp
))
12026 /* Don't mess with variables declared within the pattern. */
12028 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12030 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12031 tree args
= ARGUMENT_PACK_ARGS (spec
);
12032 if (TREE_VEC_LENGTH (args
) == 1)
12034 tree elt
= TREE_VEC_ELT (args
, 0);
12035 if (PACK_EXPANSION_P (elt
))
12036 elt
= PACK_EXPANSION_PATTERN (elt
);
12037 if (DECL_PACK_P (elt
))
12040 if (TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
)
12042 /* Handle lambda capture here, since we aren't doing any
12043 substitution now, and so tsubst_copy won't call
12044 process_outer_var_ref. */
12045 tree args
= ARGUMENT_PACK_ARGS (spec
);
12046 int len
= TREE_VEC_LENGTH (args
);
12047 for (int i
= 0; i
< len
; ++i
)
12049 tree arg
= TREE_VEC_ELT (args
, i
);
12051 if (outer_automatic_var_p (arg
))
12052 carg
= process_outer_var_ref (arg
, complain
);
12055 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12059 spec
= copy_node (spec
);
12060 args
= copy_node (args
);
12061 SET_ARGUMENT_PACK_ARGS (spec
, args
);
12062 register_local_specialization (spec
, *tp
);
12064 TREE_VEC_ELT (args
, i
) = carg
;
12069 if (outer_automatic_var_p (spec
))
12070 spec
= process_outer_var_ref (spec
, complain
);
12071 *extra
= tree_cons (*tp
, spec
, *extra
);
12076 extract_local_specs (tree pattern
, tsubst_flags_t complain
)
12078 el_data
data (complain
);
12079 cp_walk_tree_without_duplicates (&pattern
, extract_locals_r
, &data
);
12083 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12084 for use in PACK_EXPANSION_EXTRA_ARGS. */
12087 build_extra_args (tree pattern
, tree args
, tsubst_flags_t complain
)
12090 if (local_specializations
)
12091 if (tree locals
= extract_local_specs (pattern
, complain
))
12092 extra
= tree_cons (NULL_TREE
, extra
, locals
);
12096 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12097 normal template args to ARGS. */
12100 add_extra_args (tree extra
, tree args
)
12102 if (extra
&& TREE_CODE (extra
) == TREE_LIST
)
12104 for (tree elt
= TREE_CHAIN (extra
); elt
; elt
= TREE_CHAIN (elt
))
12106 /* The partial instantiation involved local declarations collected in
12107 extract_local_specs; map from the general template to our local
12109 tree gen
= TREE_PURPOSE (elt
);
12110 tree inst
= TREE_VALUE (elt
);
12112 if (tree local
= retrieve_local_specialization (inst
))
12114 /* else inst is already a full instantiation of the pack. */
12115 register_local_specialization (inst
, gen
);
12117 gcc_assert (!TREE_PURPOSE (extra
));
12118 extra
= TREE_VALUE (extra
);
12120 return add_to_template_args (extra
, args
);
12123 /* Substitute ARGS into T, which is an pack expansion
12124 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12125 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12126 (if only a partial substitution could be performed) or
12127 ERROR_MARK_NODE if there was an error. */
12129 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
12133 tree pack
, packs
= NULL_TREE
;
12134 bool unsubstituted_packs
= false;
12135 bool unsubstituted_fn_pack
= false;
12138 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
12139 bool need_local_specializations
= false;
12142 gcc_assert (PACK_EXPANSION_P (t
));
12143 pattern
= PACK_EXPANSION_PATTERN (t
);
12145 /* Add in any args remembered from an earlier partial instantiation. */
12146 args
= add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
12148 levels
= TMPL_ARGS_DEPTH (args
);
12150 /* Determine the argument packs that will instantiate the parameter
12151 packs used in the expansion expression. While we're at it,
12152 compute the number of arguments to be expanded and make sure it
12154 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
12155 pack
= TREE_CHAIN (pack
))
12157 tree parm_pack
= TREE_VALUE (pack
);
12158 tree arg_pack
= NULL_TREE
;
12159 tree orig_arg
= NULL_TREE
;
12162 if (TREE_CODE (parm_pack
) == BASES
)
12164 gcc_assert (parm_pack
== pattern
);
12165 if (BASES_DIRECT (parm_pack
))
12166 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12171 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
12172 args
, complain
, in_decl
,
12175 else if (builtin_pack_call_p (parm_pack
))
12177 if (parm_pack
!= pattern
)
12179 if (complain
& tf_error
)
12180 sorry ("%qE is not the entire pattern of the pack expansion",
12182 return error_mark_node
;
12184 return expand_builtin_pack_call (parm_pack
, args
,
12185 complain
, in_decl
);
12187 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
12189 /* We know we have correct local_specializations if this
12190 expansion is at function scope, or if we're dealing with a
12191 local parameter in a requires expression; for the latter,
12192 tsubst_requires_expr set it up appropriately. */
12193 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
12194 arg_pack
= retrieve_local_specialization (parm_pack
);
12196 /* We can't rely on local_specializations for a parameter
12197 name used later in a function declaration (such as in a
12198 late-specified return type). Even if it exists, it might
12199 have the wrong value for a recursive call. */
12200 need_local_specializations
= true;
12204 /* This parameter pack was used in an unevaluated context. Just
12205 make a dummy decl, since it's only used for its type. */
12206 ++cp_unevaluated_operand
;
12207 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
12208 --cp_unevaluated_operand
;
12209 if (arg_pack
&& DECL_PACK_P (arg_pack
))
12210 /* Partial instantiation of the parm_pack, we can't build
12211 up an argument pack yet. */
12212 arg_pack
= NULL_TREE
;
12214 arg_pack
= make_fnparm_pack (arg_pack
);
12216 else if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12217 /* This argument pack isn't fully instantiated yet. We set this
12218 flag rather than clear arg_pack because we do want to do the
12219 optimization below, and we don't want to substitute directly
12220 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12221 where it isn't expected). */
12222 unsubstituted_fn_pack
= true;
12224 else if (is_capture_proxy (parm_pack
))
12226 arg_pack
= retrieve_local_specialization (parm_pack
);
12227 if (argument_pack_element_is_expansion_p (arg_pack
, 0))
12228 unsubstituted_fn_pack
= true;
12233 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12235 if (level
<= levels
)
12236 arg_pack
= TMPL_ARG (args
, level
, idx
);
12239 orig_arg
= arg_pack
;
12240 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
12241 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
12243 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
12244 /* This can only happen if we forget to expand an argument
12245 pack somewhere else. Just return an error, silently. */
12247 result
= make_tree_vec (1);
12248 TREE_VEC_ELT (result
, 0) = error_mark_node
;
12255 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
12257 /* Don't bother trying to do a partial substitution with
12258 incomplete packs; we'll try again after deduction. */
12259 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
12264 else if (len
!= my_len
12265 && !unsubstituted_fn_pack
)
12267 if (!(complain
& tf_error
))
12268 /* Fail quietly. */;
12269 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
12270 error ("mismatched argument pack lengths while expanding %qT",
12273 error ("mismatched argument pack lengths while expanding %qE",
12275 return error_mark_node
;
12278 /* Keep track of the parameter packs and their corresponding
12280 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
12281 TREE_TYPE (packs
) = orig_arg
;
12285 /* We can't substitute for this parameter pack. We use a flag as
12286 well as the missing_level counter because function parameter
12287 packs don't have a level. */
12288 gcc_assert (processing_template_decl
|| is_auto (parm_pack
));
12289 unsubstituted_packs
= true;
12293 /* If the expansion is just T..., return the matching argument pack, unless
12294 we need to call convert_from_reference on all the elements. This is an
12295 important optimization; see c++/68422. */
12296 if (!unsubstituted_packs
12297 && TREE_PURPOSE (packs
) == pattern
)
12299 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
12301 /* If the argument pack is a single pack expansion, pull it out. */
12302 if (TREE_VEC_LENGTH (args
) == 1
12303 && pack_expansion_args_count (args
))
12304 return TREE_VEC_ELT (args
, 0);
12306 /* Types need no adjustment, nor does sizeof..., and if we still have
12307 some pack expansion args we won't do anything yet. */
12308 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
12309 || PACK_EXPANSION_SIZEOF_P (t
)
12310 || pack_expansion_args_count (args
))
12312 /* Also optimize expression pack expansions if we can tell that the
12313 elements won't have reference type. */
12314 tree type
= TREE_TYPE (pattern
);
12315 if (type
&& !TYPE_REF_P (type
)
12316 && !PACK_EXPANSION_P (type
)
12317 && !WILDCARD_TYPE_P (type
))
12319 /* Otherwise use the normal path so we get convert_from_reference. */
12322 /* We cannot expand this expansion expression, because we don't have
12323 all of the argument packs we need. */
12324 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
12326 /* We got some full packs, but we can't substitute them in until we
12327 have values for all the packs. So remember these until then. */
12329 t
= make_pack_expansion (pattern
, complain
);
12330 PACK_EXPANSION_EXTRA_ARGS (t
)
12331 = build_extra_args (pattern
, args
, complain
);
12334 else if (unsubstituted_packs
)
12336 /* There were no real arguments, we're just replacing a parameter
12337 pack with another version of itself. Substitute into the
12338 pattern and return a PACK_EXPANSION_*. The caller will need to
12340 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
12341 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
12342 /*integral_constant_expression_p=*/false);
12344 t
= tsubst (pattern
, args
, complain
, in_decl
);
12345 t
= make_pack_expansion (t
, complain
);
12349 gcc_assert (len
>= 0);
12351 if (need_local_specializations
)
12353 /* We're in a late-specified return type, so create our own local
12354 specializations map; the current map is either NULL or (in the
12355 case of recursive unification) might have bindings that we don't
12356 want to use or alter. */
12357 saved_local_specializations
= local_specializations
;
12358 local_specializations
= new hash_map
<tree
, tree
>;
12361 /* For each argument in each argument pack, substitute into the
12363 result
= make_tree_vec (len
);
12364 tree elem_args
= copy_template_args (args
);
12365 for (i
= 0; i
< len
; ++i
)
12367 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
12369 elem_args
, complain
,
12371 TREE_VEC_ELT (result
, i
) = t
;
12372 if (t
== error_mark_node
)
12374 result
= error_mark_node
;
12379 /* Update ARGS to restore the substitution from parameter packs to
12380 their argument packs. */
12381 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12383 tree parm
= TREE_PURPOSE (pack
);
12385 if (TREE_CODE (parm
) == PARM_DECL
12387 || TREE_CODE (parm
) == FIELD_DECL
)
12388 register_local_specialization (TREE_TYPE (pack
), parm
);
12393 if (TREE_VALUE (pack
) == NULL_TREE
)
12396 template_parm_level_and_index (parm
, &level
, &idx
);
12398 /* Update the corresponding argument. */
12399 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
12400 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
12403 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
12407 if (need_local_specializations
)
12409 delete local_specializations
;
12410 local_specializations
= saved_local_specializations
;
12413 /* If the dependent pack arguments were such that we end up with only a
12414 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12415 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
12416 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
12417 return TREE_VEC_ELT (result
, 0);
12422 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12423 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12424 parameter packs; all parms generated from a function parameter pack will
12425 have the same DECL_PARM_INDEX. */
12428 get_pattern_parm (tree parm
, tree tmpl
)
12430 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
12433 if (DECL_ARTIFICIAL (parm
))
12435 for (patparm
= DECL_ARGUMENTS (pattern
);
12436 patparm
; patparm
= DECL_CHAIN (patparm
))
12437 if (DECL_ARTIFICIAL (patparm
)
12438 && DECL_NAME (parm
) == DECL_NAME (patparm
))
12443 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
12444 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
12445 gcc_assert (DECL_PARM_INDEX (patparm
)
12446 == DECL_PARM_INDEX (parm
));
12452 /* Make an argument pack out of the TREE_VEC VEC. */
12455 make_argument_pack (tree vec
)
12458 tree elt
= TREE_VEC_ELT (vec
, 0);
12460 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12463 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12464 TREE_CONSTANT (pack
) = 1;
12466 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12470 /* Return an exact copy of template args T that can be modified
12474 copy_template_args (tree t
)
12476 if (t
== error_mark_node
)
12479 int len
= TREE_VEC_LENGTH (t
);
12480 tree new_vec
= make_tree_vec (len
);
12482 for (int i
= 0; i
< len
; ++i
)
12484 tree elt
= TREE_VEC_ELT (t
, i
);
12485 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
12486 elt
= copy_template_args (elt
);
12487 TREE_VEC_ELT (new_vec
, i
) = elt
;
12490 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
12491 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
12496 /* Substitute ARGS into the vector or list of template arguments T. */
12499 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12502 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
12505 if (t
== error_mark_node
)
12506 return error_mark_node
;
12508 len
= TREE_VEC_LENGTH (t
);
12509 elts
= XALLOCAVEC (tree
, len
);
12511 for (i
= 0; i
< len
; i
++)
12513 tree orig_arg
= TREE_VEC_ELT (t
, i
);
12516 if (TREE_CODE (orig_arg
) == TREE_VEC
)
12517 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
12518 else if (PACK_EXPANSION_P (orig_arg
))
12520 /* Substitute into an expansion expression. */
12521 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
12523 if (TREE_CODE (new_arg
) == TREE_VEC
)
12524 /* Add to the expanded length adjustment the number of
12525 expanded arguments. We subtract one from this
12526 measurement, because the argument pack expression
12527 itself is already counted as 1 in
12528 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12529 the argument pack is empty. */
12530 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
12532 else if (ARGUMENT_PACK_P (orig_arg
))
12534 /* Substitute into each of the arguments. */
12535 new_arg
= TYPE_P (orig_arg
)
12536 ? cxx_make_type (TREE_CODE (orig_arg
))
12537 : make_node (TREE_CODE (orig_arg
));
12539 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
12540 args
, complain
, in_decl
);
12541 if (pack_args
== error_mark_node
)
12542 new_arg
= error_mark_node
;
12544 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
12546 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
12547 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
12550 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
12552 if (new_arg
== error_mark_node
)
12553 return error_mark_node
;
12556 if (new_arg
!= orig_arg
)
12563 /* Make space for the expanded arguments coming from template
12565 t
= make_tree_vec (len
+ expanded_len_adjust
);
12566 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12567 arguments for a member template.
12568 In that case each TREE_VEC in ORIG_T represents a level of template
12569 arguments, and ORIG_T won't carry any non defaulted argument count.
12570 It will rather be the nested TREE_VECs that will carry one.
12571 In other words, ORIG_T carries a non defaulted argument count only
12572 if it doesn't contain any nested TREE_VEC. */
12573 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
12575 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
12576 count
+= expanded_len_adjust
;
12577 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
12579 for (i
= 0, out
= 0; i
< len
; i
++)
12581 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
12582 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
12583 && TREE_CODE (elts
[i
]) == TREE_VEC
)
12587 /* Now expand the template argument pack "in place". */
12588 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
12589 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
12593 TREE_VEC_ELT (t
, out
) = elts
[i
];
12601 /* Substitute ARGS into one level PARMS of template parameters. */
12604 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
12606 if (parms
== error_mark_node
)
12607 return error_mark_node
;
12609 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
12611 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
12613 tree tuple
= TREE_VEC_ELT (parms
, i
);
12615 if (tuple
== error_mark_node
)
12618 TREE_VEC_ELT (new_vec
, i
) =
12619 tsubst_template_parm (tuple
, args
, complain
);
12625 /* Return the result of substituting ARGS into the template parameters
12626 given by PARMS. If there are m levels of ARGS and m + n levels of
12627 PARMS, then the result will contain n levels of PARMS. For
12628 example, if PARMS is `template <class T> template <class U>
12629 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12630 result will be `template <int*, double, class V>'. */
12633 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
12635 tree r
= NULL_TREE
;
12638 /* When substituting into a template, we must set
12639 PROCESSING_TEMPLATE_DECL as the template parameters may be
12640 dependent if they are based on one-another, and the dependency
12641 predicates are short-circuit outside of templates. */
12642 ++processing_template_decl
;
12644 for (new_parms
= &r
;
12645 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
12646 new_parms
= &(TREE_CHAIN (*new_parms
)),
12647 parms
= TREE_CHAIN (parms
))
12649 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
12652 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
12653 - TMPL_ARGS_DEPTH (args
)),
12654 new_vec
, NULL_TREE
);
12657 --processing_template_decl
;
12662 /* Return the result of substituting ARGS into one template parameter
12663 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12664 parameter and which TREE_PURPOSE is the default argument of the
12665 template parameter. */
12668 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
12670 tree default_value
, parm_decl
;
12672 if (args
== NULL_TREE
12674 || t
== error_mark_node
)
12677 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
12679 default_value
= TREE_PURPOSE (t
);
12680 parm_decl
= TREE_VALUE (t
);
12682 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
12683 if (TREE_CODE (parm_decl
) == PARM_DECL
12684 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
12685 parm_decl
= error_mark_node
;
12686 default_value
= tsubst_template_arg (default_value
, args
,
12687 complain
, NULL_TREE
);
12689 return build_tree_list (default_value
, parm_decl
);
12692 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12693 type T. If T is not an aggregate or enumeration type, it is
12694 handled as if by tsubst. IN_DECL is as for tsubst. If
12695 ENTERING_SCOPE is nonzero, T is the context for a template which
12696 we are presently tsubst'ing. Return the substituted value. */
12699 tsubst_aggr_type (tree t
,
12701 tsubst_flags_t complain
,
12703 int entering_scope
)
12705 if (t
== NULL_TREE
)
12708 switch (TREE_CODE (t
))
12711 if (TYPE_PTRMEMFUNC_P (t
))
12712 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
12714 /* Fall through. */
12715 case ENUMERAL_TYPE
:
12717 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
12723 /* In "sizeof(X<I>)" we need to evaluate "I". */
12726 /* First, determine the context for the type we are looking
12728 context
= TYPE_CONTEXT (t
);
12729 if (context
&& TYPE_P (context
))
12731 context
= tsubst_aggr_type (context
, args
, complain
,
12732 in_decl
, /*entering_scope=*/1);
12733 /* If context is a nested class inside a class template,
12734 it may still need to be instantiated (c++/33959). */
12735 context
= complete_type (context
);
12738 /* Then, figure out what arguments are appropriate for the
12739 type we are trying to find. For example, given:
12741 template <class T> struct S;
12742 template <class T, class U> void f(T, U) { S<U> su; }
12744 and supposing that we are instantiating f<int, double>,
12745 then our ARGS will be {int, double}, but, when looking up
12746 S we only want {double}. */
12747 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
12748 complain
, in_decl
);
12749 if (argvec
== error_mark_node
)
12750 r
= error_mark_node
;
12753 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
12754 entering_scope
, complain
);
12755 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
12761 /* This is not a template type, so there's nothing to do. */
12765 return tsubst (t
, args
, complain
, in_decl
);
12769 static GTY((cache
)) tree_cache_map
*defarg_inst
;
12771 /* Substitute into the default argument ARG (a default argument for
12772 FN), which has the indicated TYPE. */
12775 tsubst_default_argument (tree fn
, int parmnum
, tree type
, tree arg
,
12776 tsubst_flags_t complain
)
12778 int errs
= errorcount
+ sorrycount
;
12780 /* This can happen in invalid code. */
12781 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12784 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
12785 parm
= chain_index (parmnum
, parm
);
12786 tree parmtype
= TREE_TYPE (parm
);
12787 if (DECL_BY_REFERENCE (parm
))
12788 parmtype
= TREE_TYPE (parmtype
);
12789 if (parmtype
== error_mark_node
)
12790 return error_mark_node
;
12792 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, parmtype
));
12795 if (defarg_inst
&& (slot
= defarg_inst
->get (parm
)))
12798 /* This default argument came from a template. Instantiate the
12799 default argument here, not in tsubst. In the case of
12808 we must be careful to do name lookup in the scope of S<T>,
12809 rather than in the current class. */
12810 push_to_top_level ();
12811 push_access_scope (fn
);
12812 push_deferring_access_checks (dk_no_deferred
);
12813 start_lambda_scope (parm
);
12815 /* The default argument expression may cause implicitly defined
12816 member functions to be synthesized, which will result in garbage
12817 collection. We must treat this situation as if we were within
12818 the body of function so as to avoid collecting live data on the
12821 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12822 complain
, NULL_TREE
,
12823 /*integral_constant_expression_p=*/false);
12826 finish_lambda_scope ();
12828 /* Make sure the default argument is reasonable. */
12829 arg
= check_default_argument (type
, arg
, complain
);
12831 if (errorcount
+sorrycount
> errs
12832 && (complain
& tf_warning_or_error
))
12833 inform (input_location
,
12834 " when instantiating default argument for call to %qD", fn
);
12836 pop_deferring_access_checks ();
12837 pop_access_scope (fn
);
12838 pop_from_top_level ();
12840 if (arg
!= error_mark_node
&& !cp_unevaluated_operand
)
12843 defarg_inst
= tree_cache_map::create_ggc (37);
12844 defarg_inst
->put (parm
, arg
);
12850 /* Substitute into all the default arguments for FN. */
12853 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12858 tmpl_args
= DECL_TI_ARGS (fn
);
12860 /* If this function is not yet instantiated, we certainly don't need
12861 its default arguments. */
12862 if (uses_template_parms (tmpl_args
))
12864 /* Don't do this again for clones. */
12865 if (DECL_CLONED_FUNCTION_P (fn
))
12869 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12871 arg
= TREE_CHAIN (arg
), ++i
)
12872 if (TREE_PURPOSE (arg
))
12873 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
, i
,
12875 TREE_PURPOSE (arg
),
12879 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12880 static GTY((cache
)) tree_cache_map
*explicit_specifier_map
;
12882 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12885 store_explicit_specifier (tree v
, tree t
)
12887 if (!explicit_specifier_map
)
12888 explicit_specifier_map
= tree_cache_map::create_ggc (37);
12889 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v
) = true;
12890 explicit_specifier_map
->put (v
, t
);
12893 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12896 lookup_explicit_specifier (tree v
)
12898 return *explicit_specifier_map
->get (v
);
12901 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12904 tsubst_function_decl (tree t
, tree args
, tsubst_flags_t complain
,
12905 tree lambda_fntype
)
12907 tree gen_tmpl
, argvec
;
12908 hashval_t hash
= 0;
12911 /* Nobody should be tsubst'ing into non-template functions. */
12912 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12914 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12916 /* If T is not dependent, just return it. */
12917 if (!uses_template_parms (DECL_TI_ARGS (t
))
12918 && !LAMBDA_FUNCTION_P (t
))
12921 /* Calculate the most general template of which R is a
12923 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12925 /* We're substituting a lambda function under tsubst_lambda_expr but not
12926 directly from it; find the matching function we're already inside.
12927 But don't do this if T is a generic lambda with a single level of
12928 template parms, as in that case we're doing a normal instantiation. */
12929 if (LAMBDA_FUNCTION_P (t
) && !lambda_fntype
12930 && (!generic_lambda_fn_p (t
)
12931 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)) > 1))
12932 return enclosing_instantiation_of (t
);
12934 /* Calculate the complete set of arguments used to
12936 argvec
= tsubst_template_args (DECL_TI_ARGS
12937 (DECL_TEMPLATE_RESULT
12938 (DECL_TI_TEMPLATE (t
))),
12939 args
, complain
, in_decl
);
12940 if (argvec
== error_mark_node
)
12941 return error_mark_node
;
12943 /* Check to see if we already have this specialization. */
12944 if (!lambda_fntype
)
12946 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12947 if (tree spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
))
12951 /* We can see more levels of arguments than parameters if
12952 there was a specialization of a member template, like
12955 template <class T> struct S { template <class U> void f(); }
12956 template <> template <class U> void S<int>::f(U);
12958 Here, we'll be substituting into the specialization,
12959 because that's where we can find the code we actually
12960 want to generate, but we'll have enough arguments for
12961 the most general template.
12963 We also deal with the peculiar case:
12965 template <class T> struct S {
12966 template <class U> friend void f();
12968 template <class U> void f() {}
12970 template void f<double>();
12972 Here, the ARGS for the instantiation of will be {int,
12973 double}. But, we only need as many ARGS as there are
12974 levels of template parameters in CODE_PATTERN. We are
12975 careful not to get fooled into reducing the ARGS in
12978 template <class T> struct S { template <class U> void f(U); }
12979 template <class T> template <> void S<T>::f(int) {}
12981 which we can spot because the pattern will be a
12982 specialization in this case. */
12983 int args_depth
= TMPL_ARGS_DEPTH (args
);
12985 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12987 if (args_depth
> parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t
))
12988 args
= get_innermost_template_args (args
, parms_depth
);
12992 /* This special case arises when we have something like this:
12994 template <class T> struct S {
12995 friend void f<int>(int, double);
12998 Here, the DECL_TI_TEMPLATE for the friend declaration
12999 will be an IDENTIFIER_NODE. We are being called from
13000 tsubst_friend_function, and we want only to create a
13001 new decl (R) with appropriate types so that we can call
13002 determine_specialization. */
13003 gen_tmpl
= NULL_TREE
;
13004 argvec
= NULL_TREE
;
13007 tree closure
= (lambda_fntype
? TYPE_METHOD_BASETYPE (lambda_fntype
)
13009 tree ctx
= closure
? closure
: DECL_CONTEXT (t
);
13010 bool member
= ctx
&& TYPE_P (ctx
);
13012 if (member
&& !closure
)
13013 ctx
= tsubst_aggr_type (ctx
, args
,
13014 complain
, t
, /*entering_scope=*/1);
13016 tree type
= (lambda_fntype
? lambda_fntype
13017 : tsubst (TREE_TYPE (t
), args
,
13018 complain
| tf_fndecl_type
, in_decl
));
13019 if (type
== error_mark_node
)
13020 return error_mark_node
;
13022 /* If we hit excessive deduction depth, the type is bogus even if
13023 it isn't error_mark_node, so don't build a decl. */
13024 if (excessive_deduction_depth
)
13025 return error_mark_node
;
13027 /* We do NOT check for matching decls pushed separately at this
13028 point, as they may not represent instantiations of this
13029 template, and in any case are considered separate under the
13031 tree r
= copy_decl (t
);
13032 DECL_USE_TEMPLATE (r
) = 0;
13033 TREE_TYPE (r
) = type
;
13034 /* Clear out the mangled name and RTL for the instantiation. */
13035 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13036 SET_DECL_RTL (r
, NULL
);
13037 /* Leave DECL_INITIAL set on deleted instantiations. */
13038 if (!DECL_DELETED_FN (r
))
13039 DECL_INITIAL (r
) = NULL_TREE
;
13040 DECL_CONTEXT (r
) = ctx
;
13042 /* Handle explicit(dependent-expr). */
13043 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t
))
13045 tree spec
= lookup_explicit_specifier (t
);
13046 spec
= tsubst_copy_and_build (spec
, args
, complain
, in_decl
,
13047 /*function_p=*/false,
13049 spec
= build_explicit_specifier (spec
, complain
);
13050 DECL_NONCONVERTING_P (r
) = (spec
== boolean_true_node
);
13053 /* OpenMP UDRs have the only argument a reference to the declared
13054 type. We want to diagnose if the declared type is a reference,
13055 which is invalid, but as references to references are usually
13056 quietly merged, diagnose it here. */
13057 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
13060 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
13061 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
13062 if (TYPE_REF_P (argtype
))
13063 error_at (DECL_SOURCE_LOCATION (t
),
13064 "reference type %qT in "
13065 "%<#pragma omp declare reduction%>", argtype
);
13066 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
13067 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
13071 if (member
&& DECL_CONV_FN_P (r
))
13072 /* Type-conversion operator. Reconstruct the name, in
13073 case it's the name of one of the template's parameters. */
13074 DECL_NAME (r
) = make_conv_op_name (TREE_TYPE (type
));
13076 tree parms
= DECL_ARGUMENTS (t
);
13078 parms
= DECL_CHAIN (parms
);
13079 parms
= tsubst (parms
, args
, complain
, t
);
13080 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
13081 DECL_CONTEXT (parm
) = r
;
13084 tree tparm
= build_this_parm (r
, closure
, type_memfn_quals (type
));
13085 DECL_CHAIN (tparm
) = parms
;
13088 DECL_ARGUMENTS (r
) = parms
;
13089 DECL_RESULT (r
) = NULL_TREE
;
13091 TREE_STATIC (r
) = 0;
13092 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
13093 DECL_EXTERNAL (r
) = 1;
13094 /* If this is an instantiation of a function with internal
13095 linkage, we already know what object file linkage will be
13096 assigned to the instantiation. */
13097 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
13098 DECL_DEFER_OUTPUT (r
) = 0;
13099 DECL_CHAIN (r
) = NULL_TREE
;
13100 DECL_PENDING_INLINE_INFO (r
) = 0;
13101 DECL_PENDING_INLINE_P (r
) = 0;
13102 DECL_SAVED_TREE (r
) = NULL_TREE
;
13103 DECL_STRUCT_FUNCTION (r
) = NULL
;
13105 /* We'll re-clone as appropriate in instantiate_template. */
13106 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
13108 /* If we aren't complaining now, return on error before we register
13109 the specialization so that we'll complain eventually. */
13110 if ((complain
& tf_error
) == 0
13111 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13112 && !grok_op_properties (r
, /*complain=*/false))
13113 return error_mark_node
;
13115 /* When instantiating a constrained member, substitute
13116 into the constraints to create a new constraint. */
13117 if (tree ci
= get_constraints (t
))
13120 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
13121 set_constraints (r
, ci
);
13124 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
13125 SET_DECL_FRIEND_CONTEXT (r
,
13126 tsubst (DECL_FRIEND_CONTEXT (t
),
13127 args
, complain
, in_decl
));
13129 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13130 this in the special friend case mentioned above where
13131 GEN_TMPL is NULL. */
13132 if (gen_tmpl
&& !closure
)
13134 DECL_TEMPLATE_INFO (r
)
13135 = build_template_info (gen_tmpl
, argvec
);
13136 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13139 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13141 /* We instantiated this while substituting into
13142 the type earlier (template/friend54.C). */
13145 /* We're not supposed to instantiate default arguments
13146 until they are called, for a template. But, for a
13149 template <class T> void f ()
13150 { extern void g(int i = T()); }
13152 we should do the substitution when the template is
13153 instantiated. We handle the member function case in
13154 instantiate_class_template since the default arguments
13155 might refer to other members of the class. */
13157 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
13158 && !uses_template_parms (argvec
))
13159 tsubst_default_arguments (r
, complain
);
13162 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13164 /* Copy the list of befriending classes. */
13165 for (tree
*friends
= &DECL_BEFRIENDING_CLASSES (r
);
13167 friends
= &TREE_CHAIN (*friends
))
13169 *friends
= copy_node (*friends
);
13170 TREE_VALUE (*friends
)
13171 = tsubst (TREE_VALUE (*friends
), args
, complain
, in_decl
);
13174 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
13176 maybe_retrofit_in_chrg (r
);
13177 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
13178 return error_mark_node
;
13179 /* If this is an instantiation of a member template, clone it.
13180 If it isn't, that'll be handled by
13181 clone_constructors_and_destructors. */
13182 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
13183 clone_function_decl (r
, /*update_methods=*/false);
13185 else if ((complain
& tf_error
) != 0
13186 && IDENTIFIER_ANY_OP_P (DECL_NAME (r
))
13187 && !grok_op_properties (r
, /*complain=*/true))
13188 return error_mark_node
;
13190 /* Possibly limit visibility based on template args. */
13191 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13192 if (DECL_VISIBILITY_SPECIFIED (t
))
13194 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13195 DECL_ATTRIBUTES (r
)
13196 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13198 determine_visibility (r
);
13199 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
13200 && !processing_template_decl
)
13201 defaulted_late_check (r
);
13203 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13204 args
, complain
, in_decl
);
13208 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13211 tsubst_template_decl (tree t
, tree args
, tsubst_flags_t complain
,
13212 tree lambda_fntype
)
13214 /* We can get here when processing a member function template,
13215 member class template, or template template parameter. */
13216 tree decl
= DECL_TEMPLATE_RESULT (t
);
13222 hashval_t hash
= 0;
13224 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
13226 /* Template template parameter is treated here. */
13227 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13228 if (new_type
== error_mark_node
)
13229 r
= error_mark_node
;
13230 /* If we get a real template back, return it. This can happen in
13231 the context of most_specialized_partial_spec. */
13232 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
13235 /* The new TEMPLATE_DECL was built in
13236 reduce_template_parm_level. */
13237 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
13241 if (!lambda_fntype
)
13243 /* We might already have an instance of this template.
13244 The ARGS are for the surrounding class type, so the
13245 full args contain the tsubst'd args for the context,
13246 plus the innermost args from the template decl. */
13247 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
13248 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
13249 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
13250 /* Because this is a template, the arguments will still be
13251 dependent, even after substitution. If
13252 PROCESSING_TEMPLATE_DECL is not set, the dependency
13253 predicates will short-circuit. */
13254 ++processing_template_decl
;
13255 full_args
= tsubst_template_args (tmpl_args
, args
,
13256 complain
, in_decl
);
13257 --processing_template_decl
;
13258 if (full_args
== error_mark_node
)
13259 return error_mark_node
;
13261 /* If this is a default template template argument,
13262 tsubst might not have changed anything. */
13263 if (full_args
== tmpl_args
)
13266 hash
= hash_tmpl_and_args (t
, full_args
);
13267 spec
= retrieve_specialization (t
, full_args
, hash
);
13268 if (spec
!= NULL_TREE
)
13271 /* Type partial instantiations are stored as the type by
13272 lookup_template_class_1, not here as the template. */
13273 spec
= CLASSTYPE_TI_TEMPLATE (spec
);
13278 /* Make a new template decl. It will be similar to the
13279 original, but will record the current template arguments.
13280 We also create a new function declaration, which is just
13281 like the old one, but points to this new template, rather
13282 than the old one. */
13284 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
13285 DECL_CHAIN (r
) = NULL_TREE
;
13287 // Build new template info linking to the original template decl.
13288 if (!lambda_fntype
)
13290 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13291 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13294 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13296 /* The template parameters for this new template are all the
13297 template parameters for the old template, except the
13298 outermost level of parameters. */
13299 DECL_TEMPLATE_PARMS (r
)
13300 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
13303 if (TREE_CODE (decl
) == TYPE_DECL
13304 && !TYPE_DECL_ALIAS_P (decl
))
13307 ++processing_template_decl
;
13308 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13309 --processing_template_decl
;
13310 if (new_type
== error_mark_node
)
13311 return error_mark_node
;
13313 TREE_TYPE (r
) = new_type
;
13314 /* For a partial specialization, we need to keep pointing to
13315 the primary template. */
13316 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
13317 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
13318 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
13319 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
13320 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
13325 ++processing_template_decl
;
13326 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13327 new_decl
= tsubst_function_decl (decl
, args
, complain
, lambda_fntype
);
13329 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
13330 --processing_template_decl
;
13331 if (new_decl
== error_mark_node
)
13332 return error_mark_node
;
13334 DECL_TEMPLATE_RESULT (r
) = new_decl
;
13335 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
13336 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
13339 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (r
));
13340 DECL_TEMPLATE_INFO (new_decl
) = build_template_info (r
, args
);
13344 DECL_TI_TEMPLATE (new_decl
) = r
;
13345 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
13349 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
13350 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
13352 if (PRIMARY_TEMPLATE_P (t
))
13353 DECL_PRIMARY_TEMPLATE (r
) = r
;
13355 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
)
13357 /* Record this non-type partial instantiation. */
13358 register_specialization (r
, t
,
13359 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
13365 /* True if FN is the op() for a lambda in an uninstantiated template. */
13368 lambda_fn_in_template_p (tree fn
)
13370 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13372 tree closure
= DECL_CONTEXT (fn
);
13373 return CLASSTYPE_TEMPLATE_INFO (closure
) != NULL_TREE
;
13376 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13377 which the above is true. */
13380 instantiated_lambda_fn_p (tree fn
)
13382 if (!fn
|| !LAMBDA_FUNCTION_P (fn
))
13384 tree closure
= DECL_CONTEXT (fn
);
13385 tree lam
= CLASSTYPE_LAMBDA_EXPR (closure
);
13386 return LAMBDA_EXPR_INSTANTIATED (lam
);
13389 /* We're instantiating a variable from template function TCTX. Return the
13390 corresponding current enclosing scope. This gets complicated because lambda
13391 functions in templates are regenerated rather than instantiated, but generic
13392 lambda functions are subsequently instantiated. */
13395 enclosing_instantiation_of (tree otctx
)
13398 tree fn
= current_function_decl
;
13399 int lambda_count
= 0;
13401 for (; tctx
&& (lambda_fn_in_template_p (tctx
)
13402 || instantiated_lambda_fn_p (tctx
));
13403 tctx
= decl_function_context (tctx
))
13405 for (; fn
; fn
= decl_function_context (fn
))
13408 int flambda_count
= 0;
13409 for (; fn
&& instantiated_lambda_fn_p (fn
);
13410 fn
= decl_function_context (fn
))
13412 if ((fn
&& DECL_TEMPLATE_INFO (fn
))
13413 ? most_general_template (fn
) != most_general_template (tctx
)
13416 if (flambda_count
!= lambda_count
)
13418 gcc_assert (flambda_count
> lambda_count
);
13419 for (; flambda_count
> lambda_count
; --flambda_count
)
13420 ofn
= decl_function_context (ofn
);
13422 gcc_assert (DECL_NAME (ofn
) == DECL_NAME (otctx
)
13423 || DECL_CONV_FN_P (ofn
));
13426 gcc_unreachable ();
13429 /* Substitute the ARGS into the T, which is a _DECL. Return the
13430 result of the substitution. Issue error and warning messages under
13431 control of COMPLAIN. */
13434 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
13436 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13437 location_t saved_loc
;
13438 tree r
= NULL_TREE
;
13440 hashval_t hash
= 0;
13442 /* Set the filename and linenumber to improve error-reporting. */
13443 saved_loc
= input_location
;
13444 input_location
= DECL_SOURCE_LOCATION (t
);
13446 switch (TREE_CODE (t
))
13448 case TEMPLATE_DECL
:
13449 r
= tsubst_template_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13452 case FUNCTION_DECL
:
13453 r
= tsubst_function_decl (t
, args
, complain
, /*lambda*/NULL_TREE
);
13458 tree type
= NULL_TREE
;
13460 tree expanded_types
= NULL_TREE
;
13461 tree prev_r
= NULL_TREE
;
13462 tree first_r
= NULL_TREE
;
13464 if (DECL_PACK_P (t
))
13466 /* If there is a local specialization that isn't a
13467 parameter pack, it means that we're doing a "simple"
13468 substitution from inside tsubst_pack_expansion. Just
13469 return the local specialization (which will be a single
13471 tree spec
= retrieve_local_specialization (t
);
13473 && TREE_CODE (spec
) == PARM_DECL
13474 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
13477 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13478 the parameters in this function parameter pack. */
13479 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13480 complain
, in_decl
);
13481 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13483 len
= TREE_VEC_LENGTH (expanded_types
);
13485 /* Zero-length parameter packs are boring. Just substitute
13488 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
13493 /* All we did was update the type. Make a note of that. */
13494 type
= expanded_types
;
13495 expanded_types
= NULL_TREE
;
13499 /* Loop through all of the parameters we'll build. When T is
13500 a function parameter pack, LEN is the number of expanded
13501 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13503 for (i
= 0; i
< len
; ++i
)
13507 if (DECL_TEMPLATE_PARM_P (t
))
13508 SET_DECL_TEMPLATE_PARM_P (r
);
13510 if (expanded_types
)
13511 /* We're on the Ith parameter of the function parameter
13514 /* Get the Ith type. */
13515 type
= TREE_VEC_ELT (expanded_types
, i
);
13517 /* Rename the parameter to include the index. */
13519 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13522 /* We're dealing with a normal parameter. */
13523 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13525 type
= type_decays_to (type
);
13526 TREE_TYPE (r
) = type
;
13527 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13529 if (DECL_INITIAL (r
))
13531 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
13532 DECL_INITIAL (r
) = TREE_TYPE (r
);
13534 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
13535 complain
, in_decl
);
13538 DECL_CONTEXT (r
) = NULL_TREE
;
13540 if (!DECL_TEMPLATE_PARM_P (r
))
13541 DECL_ARG_TYPE (r
) = type_passed_as (type
);
13543 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13544 args
, complain
, in_decl
);
13546 /* Keep track of the first new parameter we
13547 generate. That's what will be returned to the
13552 /* Build a proper chain of parameters when substituting
13553 into a function parameter pack. */
13555 DECL_CHAIN (prev_r
) = r
;
13558 /* If cp_unevaluated_operand is set, we're just looking for a
13559 single dummy parameter, so don't keep going. */
13560 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
13561 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
13562 complain
, DECL_CHAIN (t
));
13564 /* FIRST_R contains the start of the chain we've built. */
13571 tree type
= NULL_TREE
;
13572 tree vec
= NULL_TREE
;
13573 tree expanded_types
= NULL_TREE
;
13576 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
13578 /* This field is a lambda capture pack. Return a TREE_VEC of
13579 the expanded fields to instantiate_class_template_1. */
13580 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
13581 complain
, in_decl
);
13582 if (TREE_CODE (expanded_types
) == TREE_VEC
)
13584 len
= TREE_VEC_LENGTH (expanded_types
);
13585 vec
= make_tree_vec (len
);
13589 /* All we did was update the type. Make a note of that. */
13590 type
= expanded_types
;
13591 expanded_types
= NULL_TREE
;
13595 for (int i
= 0; i
< len
; ++i
)
13598 if (expanded_types
)
13600 type
= TREE_VEC_ELT (expanded_types
, i
);
13602 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
13605 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13607 if (type
== error_mark_node
)
13608 RETURN (error_mark_node
);
13609 TREE_TYPE (r
) = type
;
13610 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13612 if (DECL_C_BIT_FIELD (r
))
13613 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13615 DECL_BIT_FIELD_REPRESENTATIVE (r
)
13616 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t
), args
,
13618 /*integral_constant_expression_p=*/true);
13619 if (DECL_INITIAL (t
))
13621 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13622 NSDMI in perform_member_init. Still set DECL_INITIAL
13623 so that we know there is one. */
13624 DECL_INITIAL (r
) = void_node
;
13625 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
13626 retrofit_lang_decl (r
);
13627 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
13629 /* We don't have to set DECL_CONTEXT here; it is set by
13630 finish_member_declaration. */
13631 DECL_CHAIN (r
) = NULL_TREE
;
13633 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
13634 args
, complain
, in_decl
);
13637 TREE_VEC_ELT (vec
, i
) = r
;
13646 /* We reach here only for member using decls. We also need to check
13647 uses_template_parms because DECL_DEPENDENT_P is not set for a
13648 using-declaration that designates a member of the current
13649 instantiation (c++/53549). */
13650 if (DECL_DEPENDENT_P (t
)
13651 || uses_template_parms (USING_DECL_SCOPE (t
)))
13653 tree scope
= USING_DECL_SCOPE (t
);
13654 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
13655 if (PACK_EXPANSION_P (scope
))
13657 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
13658 int len
= TREE_VEC_LENGTH (vec
);
13659 r
= make_tree_vec (len
);
13660 for (int i
= 0; i
< len
; ++i
)
13662 tree escope
= TREE_VEC_ELT (vec
, i
);
13663 tree elt
= do_class_using_decl (escope
, name
);
13666 r
= error_mark_node
;
13671 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
13672 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
13674 TREE_VEC_ELT (r
, i
) = elt
;
13679 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
13680 complain
, in_decl
);
13681 r
= do_class_using_decl (inst_scope
, name
);
13683 r
= error_mark_node
;
13686 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
13687 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
13694 DECL_CHAIN (r
) = NULL_TREE
;
13701 tree argvec
= NULL_TREE
;
13702 tree gen_tmpl
= NULL_TREE
;
13704 tree tmpl
= NULL_TREE
;
13706 tree type
= NULL_TREE
;
13709 if (TREE_TYPE (t
) == error_mark_node
)
13710 RETURN (error_mark_node
);
13712 if (TREE_CODE (t
) == TYPE_DECL
13713 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
13715 /* If this is the canonical decl, we don't have to
13716 mess with instantiations, and often we can't (for
13717 typename, template type parms and such). Note that
13718 TYPE_NAME is not correct for the above test if
13719 we've copied the type for a typedef. */
13720 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13721 if (type
== error_mark_node
)
13722 RETURN (error_mark_node
);
13723 r
= TYPE_NAME (type
);
13727 /* Check to see if we already have the specialization we
13730 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
13732 /* T is a static data member or namespace-scope entity.
13733 We have to substitute into namespace-scope variables
13734 (not just variable templates) because of cases like:
13736 template <class T> void f() { extern T t; }
13738 where the entity referenced is not known until
13739 instantiation time. */
13741 ctx
= DECL_CONTEXT (t
);
13742 if (DECL_CLASS_SCOPE_P (t
))
13744 ctx
= tsubst_aggr_type (ctx
, args
,
13746 in_decl
, /*entering_scope=*/1);
13747 /* If CTX is unchanged, then T is in fact the
13748 specialization we want. That situation occurs when
13749 referencing a static data member within in its own
13750 class. We can use pointer equality, rather than
13751 same_type_p, because DECL_CONTEXT is always
13753 if (ctx
== DECL_CONTEXT (t
)
13754 /* ... unless T is a member template; in which
13755 case our caller can be willing to create a
13756 specialization of that template represented
13758 && !(DECL_TI_TEMPLATE (t
)
13759 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
13765 tmpl
= DECL_TI_TEMPLATE (t
);
13766 gen_tmpl
= most_general_template (tmpl
);
13767 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
13768 if (argvec
!= error_mark_node
)
13769 argvec
= (coerce_innermost_template_parms
13770 (DECL_TEMPLATE_PARMS (gen_tmpl
),
13771 argvec
, t
, complain
,
13772 /*all*/true, /*defarg*/true));
13773 if (argvec
== error_mark_node
)
13774 RETURN (error_mark_node
);
13775 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
13776 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
13781 /* A local variable. */
13783 /* Subsequent calls to pushdecl will fill this in. */
13785 /* Unless this is a reference to a static variable from an
13786 enclosing function, in which case we need to fill it in now. */
13787 if (TREE_STATIC (t
))
13789 tree fn
= enclosing_instantiation_of (DECL_CONTEXT (t
));
13790 if (fn
!= current_function_decl
)
13793 spec
= retrieve_local_specialization (t
);
13795 /* If we already have the specialization we need, there is
13796 nothing more to do. */
13803 /* Create a new node for the specialization we need. */
13804 if (type
== NULL_TREE
)
13806 if (is_typedef_decl (t
))
13807 type
= DECL_ORIGINAL_TYPE (t
);
13809 type
= TREE_TYPE (t
);
13811 && VAR_HAD_UNKNOWN_BOUND (t
)
13812 && type
!= error_mark_node
)
13813 type
= strip_array_domain (type
);
13814 tree sub_args
= args
;
13815 if (tree auto_node
= type_uses_auto (type
))
13817 /* Mask off any template args past the variable's context so we
13818 don't replace the auto with an unrelated argument. */
13819 int nouter
= TEMPLATE_TYPE_LEVEL (auto_node
) - 1;
13820 int extra
= TMPL_ARGS_DEPTH (args
) - nouter
;
13822 /* This should never happen with the new lambda instantiation
13823 model, but keep the handling just in case. */
13824 gcc_assert (!CHECKING_P
),
13825 sub_args
= strip_innermost_template_args (args
, extra
);
13827 type
= tsubst (type
, sub_args
, complain
, in_decl
);
13828 /* Substituting the type might have recursively instantiated this
13829 same alias (c++/86171). */
13830 if (gen_tmpl
&& DECL_ALIAS_TEMPLATE_P (gen_tmpl
)
13831 && (spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
)))
13840 DECL_INITIALIZED_P (r
) = 0;
13841 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
13842 if (type
== error_mark_node
)
13843 RETURN (error_mark_node
);
13844 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13846 /* It may seem that this case cannot occur, since:
13851 declares a function, not a variable. However:
13854 template <typename T> void g() { T t; }
13855 template void g<f>();
13857 is an attempt to declare a variable with function
13859 error ("variable %qD has function type",
13860 /* R is not yet sufficiently initialized, so we
13861 just use its name. */
13863 RETURN (error_mark_node
);
13865 type
= complete_type (type
);
13866 /* Wait until cp_finish_decl to set this again, to handle
13867 circular dependency (template/instantiate6.C). */
13868 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
13869 type
= check_var_type (DECL_NAME (r
), type
);
13871 if (DECL_HAS_VALUE_EXPR_P (t
))
13873 tree ve
= DECL_VALUE_EXPR (t
);
13874 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
13875 /*constant_expression_p=*/false);
13876 if (REFERENCE_REF_P (ve
))
13878 gcc_assert (TYPE_REF_P (type
));
13879 ve
= TREE_OPERAND (ve
, 0);
13881 SET_DECL_VALUE_EXPR (r
, ve
);
13883 if (CP_DECL_THREAD_LOCAL_P (r
)
13884 && !processing_template_decl
)
13885 set_decl_tls_model (r
, decl_default_tls_model (r
));
13887 else if (DECL_SELF_REFERENCE_P (t
))
13888 SET_DECL_SELF_REFERENCE_P (r
);
13889 TREE_TYPE (r
) = type
;
13890 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
13891 DECL_CONTEXT (r
) = ctx
;
13892 /* Clear out the mangled name and RTL for the instantiation. */
13893 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
13894 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
13895 SET_DECL_RTL (r
, NULL
);
13896 /* The initializer must not be expanded until it is required;
13897 see [temp.inst]. */
13898 DECL_INITIAL (r
) = NULL_TREE
;
13899 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
13902 if (DECL_LANG_SPECIFIC (r
))
13903 SET_DECL_DEPENDENT_INIT_P (r
, false);
13905 SET_DECL_MODE (r
, VOIDmode
);
13907 /* Possibly limit visibility based on template args. */
13908 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
13909 if (DECL_VISIBILITY_SPECIFIED (t
))
13911 DECL_VISIBILITY_SPECIFIED (r
) = 0;
13912 DECL_ATTRIBUTES (r
)
13913 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
13915 determine_visibility (r
);
13920 /* A static data member declaration is always marked
13921 external when it is declared in-class, even if an
13922 initializer is present. We mimic the non-template
13923 processing here. */
13924 DECL_EXTERNAL (r
) = 1;
13925 if (DECL_NAMESPACE_SCOPE_P (t
))
13926 DECL_NOT_REALLY_EXTERN (r
) = 1;
13928 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
13929 SET_DECL_IMPLICIT_INSTANTIATION (r
);
13930 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
13934 if (DECL_LANG_SPECIFIC (r
))
13935 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
13936 if (!cp_unevaluated_operand
)
13937 register_local_specialization (r
, t
);
13940 DECL_CHAIN (r
) = NULL_TREE
;
13942 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
13944 args
, complain
, in_decl
);
13946 /* Preserve a typedef that names a type. */
13947 if (is_typedef_decl (r
) && type
!= error_mark_node
)
13949 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
13950 set_underlying_type (r
);
13951 if (TYPE_DECL_ALIAS_P (r
))
13952 /* An alias template specialization can be dependent
13953 even if its underlying type is not. */
13954 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13957 layout_decl (r
, 0);
13962 gcc_unreachable ();
13967 /* Restore the file and line information. */
13968 input_location
= saved_loc
;
13973 /* Substitute into the ARG_TYPES of a function type.
13974 If END is a TREE_CHAIN, leave it and any following types
13978 tsubst_arg_types (tree arg_types
,
13981 tsubst_flags_t complain
,
13984 tree remaining_arg_types
;
13985 tree type
= NULL_TREE
;
13987 tree expanded_args
= NULL_TREE
;
13990 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13993 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13994 args
, end
, complain
, in_decl
);
13995 if (remaining_arg_types
== error_mark_node
)
13996 return error_mark_node
;
13998 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
14000 /* For a pack expansion, perform substitution on the
14001 entire expression. Later on, we'll handle the arguments
14003 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
14004 args
, complain
, in_decl
);
14006 if (TREE_CODE (expanded_args
) == TREE_VEC
)
14007 /* So that we'll spin through the parameters, one by one. */
14008 i
= TREE_VEC_LENGTH (expanded_args
);
14011 /* We only partially substituted into the parameter
14012 pack. Our type is TYPE_PACK_EXPANSION. */
14013 type
= expanded_args
;
14014 expanded_args
= NULL_TREE
;
14022 type
= TREE_VEC_ELT (expanded_args
, i
);
14024 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
14026 if (type
== error_mark_node
)
14027 return error_mark_node
;
14028 if (VOID_TYPE_P (type
))
14030 if (complain
& tf_error
)
14032 error ("invalid parameter type %qT", type
);
14034 error ("in declaration %q+D", in_decl
);
14036 return error_mark_node
;
14039 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
14040 return error_mark_node
;
14042 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14043 top-level qualifiers as required. */
14044 type
= cv_unqualified (type_decays_to (type
));
14046 /* We do not substitute into default arguments here. The standard
14047 mandates that they be instantiated only when needed, which is
14048 done in build_over_call. */
14049 default_arg
= TREE_PURPOSE (arg_types
);
14051 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14052 since the new op() won't have any associated template arguments for us
14053 to refer to later. */
14054 if (lambda_fn_in_template_p (in_decl
))
14055 default_arg
= tsubst_copy_and_build (default_arg
, args
, complain
, in_decl
,
14056 false/*fn*/, false/*constexpr*/);
14058 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
14060 /* We've instantiated a template before its default arguments
14061 have been parsed. This can happen for a nested template
14062 class, and is not an error unless we require the default
14063 argument in a call of this function. */
14064 remaining_arg_types
=
14065 tree_cons (default_arg
, type
, remaining_arg_types
);
14066 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
14069 remaining_arg_types
=
14070 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
14073 return remaining_arg_types
;
14076 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14077 *not* handle the exception-specification for FNTYPE, because the
14078 initial substitution of explicitly provided template parameters
14079 during argument deduction forbids substitution into the
14080 exception-specification:
14084 All references in the function type of the function template to the
14085 corresponding template parameters are replaced by the specified tem-
14086 plate argument values. If a substitution in a template parameter or
14087 in the function type of the function template results in an invalid
14088 type, type deduction fails. [Note: The equivalent substitution in
14089 exception specifications is done only when the function is instanti-
14090 ated, at which point a program is ill-formed if the substitution
14091 results in an invalid type.] */
14094 tsubst_function_type (tree t
,
14096 tsubst_flags_t complain
,
14100 tree arg_types
= NULL_TREE
;
14103 /* The TYPE_CONTEXT is not used for function/method types. */
14104 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
14106 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14108 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
14110 if (late_return_type_p
)
14112 /* Substitute the argument types. */
14113 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14114 complain
, in_decl
);
14115 if (arg_types
== error_mark_node
)
14116 return error_mark_node
;
14118 tree save_ccp
= current_class_ptr
;
14119 tree save_ccr
= current_class_ref
;
14120 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
14121 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
14122 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
14125 /* DR 1207: 'this' is in scope in the trailing return type. */
14126 inject_this_parameter (this_type
, cp_type_quals (this_type
));
14129 /* Substitute the return type. */
14130 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14134 current_class_ptr
= save_ccp
;
14135 current_class_ref
= save_ccr
;
14139 /* Substitute the return type. */
14140 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14142 if (return_type
== error_mark_node
)
14143 return error_mark_node
;
14144 /* DR 486 clarifies that creation of a function type with an
14145 invalid return type is a deduction failure. */
14146 if (TREE_CODE (return_type
) == ARRAY_TYPE
14147 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
14149 if (complain
& tf_error
)
14151 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
14152 error ("function returning an array");
14154 error ("function returning a function");
14156 return error_mark_node
;
14159 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
14160 return error_mark_node
;
14162 if (!late_return_type_p
)
14164 /* Substitute the argument types. */
14165 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
14166 complain
, in_decl
);
14167 if (arg_types
== error_mark_node
)
14168 return error_mark_node
;
14171 /* Construct a new type node and return it. */
14172 if (TREE_CODE (t
) == FUNCTION_TYPE
)
14174 fntype
= build_function_type (return_type
, arg_types
);
14175 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
14179 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
14180 /* Don't pick up extra function qualifiers from the basetype. */
14181 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
14182 if (! MAYBE_CLASS_TYPE_P (r
))
14186 Type deduction may fail for any of the following
14189 -- Attempting to create "pointer to member of T" when T
14190 is not a class type. */
14191 if (complain
& tf_error
)
14192 error ("creating pointer to member function of non-class type %qT",
14194 return error_mark_node
;
14197 fntype
= build_method_type_directly (r
, return_type
,
14198 TREE_CHAIN (arg_types
));
14200 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
14202 /* See comment above. */
14203 tree raises
= NULL_TREE
;
14204 cp_ref_qualifier rqual
= type_memfn_rqual (t
);
14205 fntype
= build_cp_fntype_variant (fntype
, rqual
, raises
, late_return_type_p
);
14210 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14211 ARGS into that specification, and return the substituted
14212 specification. If there is no specification, return NULL_TREE. */
14215 tsubst_exception_specification (tree fntype
,
14217 tsubst_flags_t complain
,
14224 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
14225 new_specs
= NULL_TREE
;
14226 if (specs
&& TREE_PURPOSE (specs
))
14228 /* A noexcept-specifier. */
14229 tree expr
= TREE_PURPOSE (specs
);
14230 if (TREE_CODE (expr
) == INTEGER_CST
)
14234 /* Defer instantiation of noexcept-specifiers to avoid
14235 excessive instantiations (c++/49107). */
14236 new_specs
= make_node (DEFERRED_NOEXCEPT
);
14237 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14239 /* We already partially instantiated this member template,
14240 so combine the new args with the old. */
14241 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
14242 = DEFERRED_NOEXCEPT_PATTERN (expr
);
14243 DEFERRED_NOEXCEPT_ARGS (new_specs
)
14244 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
14248 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
14249 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
14254 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
14256 args
= add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
),
14258 expr
= DEFERRED_NOEXCEPT_PATTERN (expr
);
14260 new_specs
= tsubst_copy_and_build
14261 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
14262 /*integral_constant_expression_p=*/true);
14264 new_specs
= build_noexcept_spec (new_specs
, complain
);
14268 if (! TREE_VALUE (specs
))
14275 tree expanded_specs
= NULL_TREE
;
14277 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
14279 /* Expand the pack expansion type. */
14280 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
14284 if (expanded_specs
== error_mark_node
)
14285 return error_mark_node
;
14286 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
14287 len
= TREE_VEC_LENGTH (expanded_specs
);
14290 /* We're substituting into a member template, so
14291 we got a TYPE_PACK_EXPANSION back. Add that
14292 expansion and move on. */
14293 gcc_assert (TREE_CODE (expanded_specs
)
14294 == TYPE_PACK_EXPANSION
);
14295 new_specs
= add_exception_specifier (new_specs
,
14298 specs
= TREE_CHAIN (specs
);
14303 for (i
= 0; i
< len
; ++i
)
14305 if (expanded_specs
)
14306 spec
= TREE_VEC_ELT (expanded_specs
, i
);
14308 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
14309 if (spec
== error_mark_node
)
14311 new_specs
= add_exception_specifier (new_specs
, spec
,
14315 specs
= TREE_CHAIN (specs
);
14321 /* Take the tree structure T and replace template parameters used
14322 therein with the argument vector ARGS. IN_DECL is an associated
14323 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14324 Issue error and warning messages under control of COMPLAIN. Note
14325 that we must be relatively non-tolerant of extensions here, in
14326 order to preserve conformance; if we allow substitutions that
14327 should not be allowed, we may allow argument deductions that should
14328 not succeed, and therefore report ambiguous overload situations
14329 where there are none. In theory, we could allow the substitution,
14330 but indicate that it should have failed, and allow our caller to
14331 make sure that the right thing happens, but we don't try to do this
14334 This function is used for dealing with types, decls and the like;
14335 for expressions, use tsubst_expr or tsubst_copy. */
14338 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14340 enum tree_code code
;
14341 tree type
, r
= NULL_TREE
;
14343 if (t
== NULL_TREE
|| t
== error_mark_node
14344 || t
== integer_type_node
14345 || t
== void_type_node
14346 || t
== char_type_node
14347 || t
== unknown_type_node
14348 || TREE_CODE (t
) == NAMESPACE_DECL
14349 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
14353 return tsubst_decl (t
, args
, complain
);
14355 if (args
== NULL_TREE
)
14358 code
= TREE_CODE (t
);
14360 if (code
== IDENTIFIER_NODE
)
14361 type
= IDENTIFIER_TYPE_VALUE (t
);
14363 type
= TREE_TYPE (t
);
14365 gcc_assert (type
!= unknown_type_node
);
14367 /* Reuse typedefs. We need to do this to handle dependent attributes,
14368 such as attribute aligned. */
14370 && typedef_variant_p (t
))
14372 tree decl
= TYPE_NAME (t
);
14374 if (alias_template_specialization_p (t
))
14376 /* DECL represents an alias template and we want to
14378 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14379 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14380 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
14382 else if (DECL_CLASS_SCOPE_P (decl
)
14383 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14384 && uses_template_parms (DECL_CONTEXT (decl
)))
14386 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14387 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
14388 r
= retrieve_specialization (tmpl
, gen_args
, 0);
14390 else if (DECL_FUNCTION_SCOPE_P (decl
)
14391 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
14392 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
14393 r
= retrieve_local_specialization (decl
);
14395 /* The typedef is from a non-template context. */
14401 r
= cp_build_qualified_type_real
14402 (r
, cp_type_quals (t
) | cp_type_quals (r
),
14403 complain
| tf_ignore_bad_quals
);
14408 /* We don't have an instantiation yet, so drop the typedef. */
14409 int quals
= cp_type_quals (t
);
14410 t
= DECL_ORIGINAL_TYPE (decl
);
14411 t
= cp_build_qualified_type_real (t
, quals
,
14412 complain
| tf_ignore_bad_quals
);
14416 bool fndecl_type
= (complain
& tf_fndecl_type
);
14417 complain
&= ~tf_fndecl_type
;
14420 && code
!= TYPENAME_TYPE
14421 && code
!= TEMPLATE_TYPE_PARM
14422 && code
!= TEMPLATE_PARM_INDEX
14423 && code
!= IDENTIFIER_NODE
14424 && code
!= FUNCTION_TYPE
14425 && code
!= METHOD_TYPE
)
14426 type
= tsubst (type
, args
, complain
, in_decl
);
14427 if (type
== error_mark_node
)
14428 return error_mark_node
;
14434 case ENUMERAL_TYPE
:
14435 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
14436 /*entering_scope=*/0);
14439 case IDENTIFIER_NODE
:
14450 if (t
== integer_type_node
)
14453 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
14454 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
14458 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
14460 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
14461 /*integral_constant_expression_p=*/false);
14463 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14465 if (TREE_CODE (max
) == NOP_EXPR
14466 && TREE_SIDE_EFFECTS (omax
)
14467 && !TREE_TYPE (max
))
14468 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
14470 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14471 with TREE_SIDE_EFFECTS that indicates this is not an integral
14472 constant expression. */
14473 if (processing_template_decl
14474 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
14476 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
14477 TREE_SIDE_EFFECTS (max
) = 1;
14480 return compute_array_index_type (NULL_TREE
, max
, complain
);
14483 case TEMPLATE_TYPE_PARM
:
14484 case TEMPLATE_TEMPLATE_PARM
:
14485 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14486 case TEMPLATE_PARM_INDEX
:
14491 tree arg
= NULL_TREE
;
14493 /* Early in template argument deduction substitution, we don't
14494 want to reduce the level of 'auto', or it will be confused
14495 with a normal template parm in subsequent deduction. */
14496 if (is_auto (t
) && (complain
& tf_partial
))
14501 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
14502 template_parm_level_and_index (t
, &level
, &idx
);
14504 levels
= TMPL_ARGS_DEPTH (args
);
14505 if (level
<= levels
14506 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
14508 arg
= TMPL_ARG (args
, level
, idx
);
14510 /* See through ARGUMENT_PACK_SELECT arguments. */
14511 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
14512 arg
= argument_pack_select_arg (arg
);
14515 if (arg
== error_mark_node
)
14516 return error_mark_node
;
14517 else if (arg
!= NULL_TREE
)
14519 if (ARGUMENT_PACK_P (arg
))
14520 /* If ARG is an argument pack, we don't actually want to
14521 perform a substitution here, because substitutions
14522 for argument packs are only done
14523 element-by-element. We can get to this point when
14524 substituting the type of a non-type template
14525 parameter pack, when that type actually contains
14526 template parameter packs from an outer template, e.g.,
14528 template<typename... Types> struct A {
14529 template<Types... Values> struct B { };
14533 if (code
== TEMPLATE_TYPE_PARM
)
14536 gcc_assert (TYPE_P (arg
));
14538 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
14540 return cp_build_qualified_type_real
14541 (arg
, quals
, complain
| tf_ignore_bad_quals
);
14543 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14545 /* We are processing a type constructed from a
14546 template template parameter. */
14547 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
14548 args
, complain
, in_decl
);
14549 if (argvec
== error_mark_node
)
14550 return error_mark_node
;
14552 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
14553 || TREE_CODE (arg
) == TEMPLATE_DECL
14554 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
14556 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
14557 /* Consider this code:
14559 template <template <class> class Template>
14561 template <class Arg> using Bind = Template<Arg>;
14564 template <template <class> class Template, class Arg>
14565 using Instantiate = Template<Arg>; //#0
14567 template <template <class> class Template,
14570 Instantiate<Internal<Template>::template Bind,
14573 When #1 is parsed, the
14574 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14575 parameter `Template' in #0 matches the
14576 UNBOUND_CLASS_TEMPLATE representing the argument
14577 `Internal<Template>::template Bind'; We then want
14578 to assemble the type `Bind<Argument>' that can't
14579 be fully created right now, because
14580 `Internal<Template>' not being complete, the Bind
14581 template cannot be looked up in that context. So
14582 we need to "store" `Bind<Argument>' for later
14583 when the context of Bind becomes complete. Let's
14584 store that in a TYPENAME_TYPE. */
14585 return make_typename_type (TYPE_CONTEXT (arg
),
14586 build_nt (TEMPLATE_ID_EXPR
,
14587 TYPE_IDENTIFIER (arg
),
14592 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14593 are resolving nested-types in the signature of a
14594 member function templates. Otherwise ARG is a
14595 TEMPLATE_DECL and is the real template to be
14597 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
14598 arg
= TYPE_NAME (arg
);
14600 r
= lookup_template_class (arg
,
14602 DECL_CONTEXT (arg
),
14603 /*entering_scope=*/0,
14605 return cp_build_qualified_type_real
14606 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
14608 else if (code
== TEMPLATE_TEMPLATE_PARM
)
14611 /* TEMPLATE_PARM_INDEX. */
14612 return convert_from_reference (unshare_expr (arg
));
14616 /* This can happen during the attempted tsubst'ing in
14617 unify. This means that we don't yet have any information
14618 about the template parameter in question. */
14621 /* If we get here, we must have been looking at a parm for a
14622 more deeply nested template. Make a new version of this
14623 template parameter, but with a lower level. */
14626 case TEMPLATE_TYPE_PARM
:
14627 case TEMPLATE_TEMPLATE_PARM
:
14628 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14629 if (cp_type_quals (t
))
14631 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
14632 r
= cp_build_qualified_type_real
14633 (r
, cp_type_quals (t
),
14634 complain
| (code
== TEMPLATE_TYPE_PARM
14635 ? tf_ignore_bad_quals
: 0));
14637 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14638 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14639 && (r
= (TEMPLATE_PARM_DESCENDANTS
14640 (TEMPLATE_TYPE_PARM_INDEX (t
))))
14641 && (r
= TREE_TYPE (r
))
14642 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
14643 /* Break infinite recursion when substituting the constraints
14644 of a constrained placeholder. */;
14645 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
14646 && !PLACEHOLDER_TYPE_CONSTRAINTS (t
)
14647 && !CLASS_PLACEHOLDER_TEMPLATE (t
)
14648 && (arg
= TEMPLATE_TYPE_PARM_INDEX (t
),
14649 r
= TEMPLATE_PARM_DESCENDANTS (arg
))
14650 && (TEMPLATE_PARM_LEVEL (r
)
14651 == TEMPLATE_PARM_LEVEL (arg
) - levels
))
14652 /* Cache the simple case of lowering a type parameter. */
14657 TEMPLATE_TYPE_PARM_INDEX (r
)
14658 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
14659 r
, levels
, args
, complain
);
14660 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
14661 TYPE_MAIN_VARIANT (r
) = r
;
14662 TYPE_POINTER_TO (r
) = NULL_TREE
;
14663 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
14665 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
14667 /* Propagate constraints on placeholders. */
14668 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
14669 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
14670 = tsubst_constraint (constr
, args
, complain
, in_decl
);
14671 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
14673 pl
= tsubst_copy (pl
, args
, complain
, in_decl
);
14674 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
14678 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
14679 /* We have reduced the level of the template
14680 template parameter, but not the levels of its
14681 template parameters, so canonical_type_parameter
14682 will not be able to find the canonical template
14683 template parameter for this level. Thus, we
14684 require structural equality checking to compare
14685 TEMPLATE_TEMPLATE_PARMs. */
14686 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14687 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
14688 SET_TYPE_STRUCTURAL_EQUALITY (r
);
14690 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
14692 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
14694 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
14695 /* We might need to substitute into the types of non-type
14696 template parameters. */
14697 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
14698 complain
, in_decl
);
14699 if (tmpl
== error_mark_node
)
14700 return error_mark_node
;
14701 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
14702 complain
, in_decl
);
14703 if (argvec
== error_mark_node
)
14704 return error_mark_node
;
14706 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
14707 = build_template_info (tmpl
, argvec
);
14712 case TEMPLATE_PARM_INDEX
:
14713 /* OK, now substitute the type of the non-type parameter. We
14714 couldn't do it earlier because it might be an auto parameter,
14715 and we wouldn't need to if we had an argument. */
14716 type
= tsubst (type
, args
, complain
, in_decl
);
14717 if (type
== error_mark_node
)
14718 return error_mark_node
;
14719 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
14723 gcc_unreachable ();
14731 tree purpose
, value
, chain
;
14733 if (t
== void_list_node
)
14736 purpose
= TREE_PURPOSE (t
);
14739 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
14740 if (purpose
== error_mark_node
)
14741 return error_mark_node
;
14743 value
= TREE_VALUE (t
);
14746 value
= tsubst (value
, args
, complain
, in_decl
);
14747 if (value
== error_mark_node
)
14748 return error_mark_node
;
14750 chain
= TREE_CHAIN (t
);
14751 if (chain
&& chain
!= void_type_node
)
14753 chain
= tsubst (chain
, args
, complain
, in_decl
);
14754 if (chain
== error_mark_node
)
14755 return error_mark_node
;
14757 if (purpose
== TREE_PURPOSE (t
)
14758 && value
== TREE_VALUE (t
)
14759 && chain
== TREE_CHAIN (t
))
14761 return hash_tree_cons (purpose
, value
, chain
);
14765 /* We should never be tsubsting a binfo. */
14766 gcc_unreachable ();
14769 /* A vector of template arguments. */
14770 gcc_assert (!type
);
14771 return tsubst_template_args (t
, args
, complain
, in_decl
);
14774 case REFERENCE_TYPE
:
14776 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
14781 Type deduction may fail for any of the following
14784 -- Attempting to create a pointer to reference type.
14785 -- Attempting to create a reference to a reference type or
14786 a reference to void.
14788 Core issue 106 says that creating a reference to a reference
14789 during instantiation is no longer a cause for failure. We
14790 only enforce this check in strict C++98 mode. */
14791 if ((TYPE_REF_P (type
)
14792 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
14793 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
14795 static location_t last_loc
;
14797 /* We keep track of the last time we issued this error
14798 message to avoid spewing a ton of messages during a
14799 single bad template instantiation. */
14800 if (complain
& tf_error
14801 && last_loc
!= input_location
)
14803 if (VOID_TYPE_P (type
))
14804 error ("forming reference to void");
14805 else if (code
== POINTER_TYPE
)
14806 error ("forming pointer to reference type %qT", type
);
14808 error ("forming reference to reference type %qT", type
);
14809 last_loc
= input_location
;
14812 return error_mark_node
;
14814 else if (TREE_CODE (type
) == FUNCTION_TYPE
14815 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
14816 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
14818 if (complain
& tf_error
)
14820 if (code
== POINTER_TYPE
)
14821 error ("forming pointer to qualified function type %qT",
14824 error ("forming reference to qualified function type %qT",
14827 return error_mark_node
;
14829 else if (code
== POINTER_TYPE
)
14831 r
= build_pointer_type (type
);
14832 if (TREE_CODE (type
) == METHOD_TYPE
)
14833 r
= build_ptrmemfunc_type (r
);
14835 else if (TYPE_REF_P (type
))
14836 /* In C++0x, during template argument substitution, when there is an
14837 attempt to create a reference to a reference type, reference
14838 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14840 "If a template-argument for a template-parameter T names a type
14841 that is a reference to a type A, an attempt to create the type
14842 'lvalue reference to cv T' creates the type 'lvalue reference to
14843 A,' while an attempt to create the type type rvalue reference to
14844 cv T' creates the type T"
14846 r
= cp_build_reference_type
14848 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
14850 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
14851 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
14853 if (r
!= error_mark_node
)
14854 /* Will this ever be needed for TYPE_..._TO values? */
14861 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
14862 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
14866 Type deduction may fail for any of the following
14869 -- Attempting to create "pointer to member of T" when T
14870 is not a class type. */
14871 if (complain
& tf_error
)
14872 error ("creating pointer to member of non-class type %qT", r
);
14873 return error_mark_node
;
14875 if (TYPE_REF_P (type
))
14877 if (complain
& tf_error
)
14878 error ("creating pointer to member reference type %qT", type
);
14879 return error_mark_node
;
14881 if (VOID_TYPE_P (type
))
14883 if (complain
& tf_error
)
14884 error ("creating pointer to member of type void");
14885 return error_mark_node
;
14887 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
14888 if (TREE_CODE (type
) == FUNCTION_TYPE
)
14890 /* The type of the implicit object parameter gets its
14891 cv-qualifiers from the FUNCTION_TYPE. */
14894 = build_memfn_type (type
, r
, type_memfn_quals (type
),
14895 type_memfn_rqual (type
));
14896 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
14897 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
14901 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
14905 case FUNCTION_TYPE
:
14910 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
14911 if (fntype
== error_mark_node
)
14912 return error_mark_node
;
14914 /* Substitute the exception specification. */
14915 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
14916 /*defer_ok*/fndecl_type
);
14917 if (specs
== error_mark_node
)
14918 return error_mark_node
;
14920 fntype
= build_exception_variant (fntype
, specs
);
14925 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
14926 if (domain
== error_mark_node
)
14927 return error_mark_node
;
14929 /* As an optimization, we avoid regenerating the array type if
14930 it will obviously be the same as T. */
14931 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
14934 /* These checks should match the ones in create_array_type_for_decl.
14938 The deduction may fail for any of the following reasons:
14940 -- Attempting to create an array with an element type that
14941 is void, a function type, or a reference type, or [DR337]
14942 an abstract class type. */
14943 if (VOID_TYPE_P (type
)
14944 || TREE_CODE (type
) == FUNCTION_TYPE
14945 || (TREE_CODE (type
) == ARRAY_TYPE
14946 && TYPE_DOMAIN (type
) == NULL_TREE
)
14947 || TYPE_REF_P (type
))
14949 if (complain
& tf_error
)
14950 error ("creating array of %qT", type
);
14951 return error_mark_node
;
14954 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
14955 return error_mark_node
;
14957 r
= build_cplus_array_type (type
, domain
);
14959 if (!valid_array_size_p (input_location
, r
, in_decl
,
14960 (complain
& tf_error
)))
14961 return error_mark_node
;
14963 if (TYPE_USER_ALIGN (t
))
14965 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14966 TYPE_USER_ALIGN (r
) = 1;
14972 case TYPENAME_TYPE
:
14974 tree ctx
= TYPE_CONTEXT (t
);
14975 if (TREE_CODE (ctx
) == TYPE_PACK_EXPANSION
)
14977 ctx
= tsubst_pack_expansion (ctx
, args
, complain
, in_decl
);
14978 if (ctx
== error_mark_node
14979 || TREE_VEC_LENGTH (ctx
) > 1)
14980 return error_mark_node
;
14981 if (TREE_VEC_LENGTH (ctx
) == 0)
14983 if (complain
& tf_error
)
14984 error ("%qD is instantiated for an empty pack",
14985 TYPENAME_TYPE_FULLNAME (t
));
14986 return error_mark_node
;
14988 ctx
= TREE_VEC_ELT (ctx
, 0);
14991 ctx
= tsubst_aggr_type (ctx
, args
, complain
, in_decl
,
14992 /*entering_scope=*/1);
14993 if (ctx
== error_mark_node
)
14994 return error_mark_node
;
14996 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14997 complain
, in_decl
);
14998 if (f
== error_mark_node
)
14999 return error_mark_node
;
15001 if (!MAYBE_CLASS_TYPE_P (ctx
))
15003 if (complain
& tf_error
)
15004 error ("%qT is not a class, struct, or union type", ctx
);
15005 return error_mark_node
;
15007 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
15009 /* Normally, make_typename_type does not require that the CTX
15010 have complete type in order to allow things like:
15012 template <class T> struct S { typename S<T>::X Y; };
15014 But, such constructs have already been resolved by this
15015 point, so here CTX really should have complete type, unless
15016 it's a partial instantiation. */
15017 ctx
= complete_type (ctx
);
15018 if (!COMPLETE_TYPE_P (ctx
))
15020 if (complain
& tf_error
)
15021 cxx_incomplete_type_error (NULL_TREE
, ctx
);
15022 return error_mark_node
;
15026 f
= make_typename_type (ctx
, f
, typename_type
,
15027 complain
| tf_keep_type_decl
);
15028 if (f
== error_mark_node
)
15030 if (TREE_CODE (f
) == TYPE_DECL
)
15032 complain
|= tf_ignore_bad_quals
;
15036 if (TREE_CODE (f
) != TYPENAME_TYPE
)
15038 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
15040 if (complain
& tf_error
)
15041 error ("%qT resolves to %qT, which is not an enumeration type",
15044 return error_mark_node
;
15046 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
15048 if (complain
& tf_error
)
15049 error ("%qT resolves to %qT, which is is not a class type",
15052 return error_mark_node
;
15056 return cp_build_qualified_type_real
15057 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
15060 case UNBOUND_CLASS_TEMPLATE
:
15062 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
15063 in_decl
, /*entering_scope=*/1);
15064 tree name
= TYPE_IDENTIFIER (t
);
15065 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
15067 if (ctx
== error_mark_node
|| name
== error_mark_node
)
15068 return error_mark_node
;
15071 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
15072 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
15079 ++cp_unevaluated_operand
;
15080 ++c_inhibit_evaluation_warnings
;
15082 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
15084 /*integral_constant_expression_p=*/false);
15086 --cp_unevaluated_operand
;
15087 --c_inhibit_evaluation_warnings
;
15089 type
= finish_typeof (type
);
15090 return cp_build_qualified_type_real (type
,
15092 | cp_type_quals (type
),
15096 case DECLTYPE_TYPE
:
15100 ++cp_unevaluated_operand
;
15101 ++c_inhibit_evaluation_warnings
;
15103 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
15104 complain
|tf_decltype
, in_decl
,
15105 /*function_p*/false,
15106 /*integral_constant_expression*/false);
15108 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
15110 if (type
== NULL_TREE
)
15112 if (complain
& tf_error
)
15113 error ("empty initializer in lambda init-capture");
15114 type
= error_mark_node
;
15116 else if (TREE_CODE (type
) == TREE_LIST
)
15117 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
15120 --cp_unevaluated_operand
;
15121 --c_inhibit_evaluation_warnings
;
15123 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
15124 type
= lambda_capture_field_type (type
,
15125 DECLTYPE_FOR_INIT_CAPTURE (t
),
15126 DECLTYPE_FOR_REF_CAPTURE (t
));
15127 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
15128 type
= lambda_proxy_type (type
);
15131 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
15132 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
15134 /* In a template ~id could be either a complement expression
15135 or an unqualified-id naming a destructor; if instantiating
15136 it produces an expression, it's not an id-expression or
15139 type
= finish_decltype_type (type
, id
, complain
);
15141 return cp_build_qualified_type_real (type
,
15143 | cp_type_quals (type
),
15144 complain
| tf_ignore_bad_quals
);
15147 case UNDERLYING_TYPE
:
15149 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
15150 complain
, in_decl
);
15151 return finish_underlying_type (type
);
15154 case TYPE_ARGUMENT_PACK
:
15155 case NONTYPE_ARGUMENT_PACK
:
15159 if (code
== NONTYPE_ARGUMENT_PACK
)
15160 r
= make_node (code
);
15162 r
= cxx_make_type (code
);
15164 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
15165 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
15166 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
15184 /* We should use one of the expression tsubsts for these codes. */
15185 gcc_unreachable ();
15188 sorry ("use of %qs in template", get_tree_code_name (code
));
15189 return error_mark_node
;
15193 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15194 expression on the left-hand side of the "." or "->" operator. We
15195 only do the lookup if we had a dependent BASELINK. Otherwise we
15196 adjust it onto the instantiated heirarchy. */
15199 tsubst_baselink (tree baselink
, tree object_type
,
15200 tree args
, tsubst_flags_t complain
, tree in_decl
)
15202 bool qualified_p
= BASELINK_QUALIFIED_P (baselink
);
15203 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
15204 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
15206 tree optype
= BASELINK_OPTYPE (baselink
);
15207 optype
= tsubst (optype
, args
, complain
, in_decl
);
15209 tree template_args
= NULL_TREE
;
15210 bool template_id_p
= false;
15211 tree fns
= BASELINK_FUNCTIONS (baselink
);
15212 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
15214 template_id_p
= true;
15215 template_args
= TREE_OPERAND (fns
, 1);
15216 fns
= TREE_OPERAND (fns
, 0);
15218 template_args
= tsubst_template_args (template_args
, args
,
15219 complain
, in_decl
);
15222 tree binfo_type
= BINFO_TYPE (BASELINK_BINFO (baselink
));
15223 binfo_type
= tsubst (binfo_type
, args
, complain
, in_decl
);
15224 bool dependent_p
= binfo_type
!= BINFO_TYPE (BASELINK_BINFO (baselink
));
15228 tree name
= OVL_NAME (fns
);
15229 if (IDENTIFIER_CONV_OP_P (name
))
15230 name
= make_conv_op_name (optype
);
15232 if (name
== complete_dtor_identifier
)
15233 /* Treat as-if non-dependent below. */
15234 dependent_p
= false;
15236 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
15239 if ((complain
& tf_error
)
15240 && constructor_name_p (name
, qualifying_scope
))
15241 error ("cannot call constructor %<%T::%D%> directly",
15242 qualifying_scope
, name
);
15243 return error_mark_node
;
15246 if (BASELINK_P (baselink
))
15247 fns
= BASELINK_FUNCTIONS (baselink
);
15250 /* We're going to overwrite pieces below, make a duplicate. */
15251 baselink
= copy_node (baselink
);
15253 /* If lookup found a single function, mark it as used at this point.
15254 (If lookup found multiple functions the one selected later by
15255 overload resolution will be marked as used at that point.) */
15256 if (!template_id_p
&& !really_overloaded_fn (fns
))
15258 tree fn
= OVL_FIRST (fns
);
15259 bool ok
= mark_used (fn
, complain
);
15260 if (!ok
&& !(complain
& tf_error
))
15261 return error_mark_node
;
15262 if (ok
&& BASELINK_P (baselink
))
15263 /* We might have instantiated an auto function. */
15264 TREE_TYPE (baselink
) = TREE_TYPE (fn
);
15267 if (BASELINK_P (baselink
))
15269 /* Add back the template arguments, if present. */
15271 BASELINK_FUNCTIONS (baselink
)
15272 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
, fns
, template_args
);
15274 /* Update the conversion operator type. */
15275 BASELINK_OPTYPE (baselink
) = optype
;
15279 object_type
= current_class_type
;
15281 if (qualified_p
|| !dependent_p
)
15283 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
15287 /* We need to call adjust_result_of_qualified_name_lookup in case the
15288 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15289 so that we still get virtual function binding. */
15290 BASELINK_QUALIFIED_P (baselink
) = false;
15296 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15297 true if the qualified-id will be a postfix-expression in-and-of
15298 itself; false if more of the postfix-expression follows the
15299 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15303 tsubst_qualified_id (tree qualified_id
, tree args
,
15304 tsubst_flags_t complain
, tree in_decl
,
15305 bool done
, bool address_p
)
15311 tree template_args
;
15312 location_t loc
= UNKNOWN_LOCATION
;
15314 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
15316 /* Figure out what name to look up. */
15317 name
= TREE_OPERAND (qualified_id
, 1);
15318 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
15320 is_template
= true;
15321 loc
= EXPR_LOCATION (name
);
15322 template_args
= TREE_OPERAND (name
, 1);
15324 template_args
= tsubst_template_args (template_args
, args
,
15325 complain
, in_decl
);
15326 if (template_args
== error_mark_node
)
15327 return error_mark_node
;
15328 name
= TREE_OPERAND (name
, 0);
15332 is_template
= false;
15333 template_args
= NULL_TREE
;
15336 /* Substitute into the qualifying scope. When there are no ARGS, we
15337 are just trying to simplify a non-dependent expression. In that
15338 case the qualifying scope may be dependent, and, in any case,
15339 substituting will not help. */
15340 scope
= TREE_OPERAND (qualified_id
, 0);
15343 scope
= tsubst (scope
, args
, complain
, in_decl
);
15344 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
15349 if (dependent_scope_p (scope
))
15352 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
15353 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
15354 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
15355 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
15359 if (!BASELINK_P (name
) && !DECL_P (expr
))
15361 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
15363 /* A BIT_NOT_EXPR is used to represent a destructor. */
15364 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
15366 error ("qualifying type %qT does not match destructor name ~%qT",
15367 scope
, TREE_OPERAND (expr
, 0));
15368 expr
= error_mark_node
;
15371 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
15372 /*is_type_p=*/0, false);
15375 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
15376 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
15377 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
15379 if (complain
& tf_error
)
15381 error ("dependent-name %qE is parsed as a non-type, but "
15382 "instantiation yields a type", qualified_id
);
15383 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
15385 return error_mark_node
;
15391 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
15393 /* Remember that there was a reference to this entity. */
15394 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
15395 return error_mark_node
;
15398 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
15400 if (complain
& tf_error
)
15401 qualified_name_lookup_error (scope
,
15402 TREE_OPERAND (qualified_id
, 1),
15403 expr
, input_location
);
15404 return error_mark_node
;
15409 /* We may be repeating a check already done during parsing, but
15410 if it was well-formed and passed then, it will pass again
15411 now, and if it didn't, we wouldn't have got here. The case
15412 we want to catch is when we couldn't tell then, and can now,
15413 namely when templ prior to substitution was an
15415 if (flag_concepts
&& check_auto_in_tmpl_args (expr
, template_args
))
15416 return error_mark_node
;
15418 if (variable_template_p (expr
))
15419 expr
= lookup_and_finish_template_variable (expr
, template_args
,
15422 expr
= lookup_template_function (expr
, template_args
);
15425 if (expr
== error_mark_node
&& complain
& tf_error
)
15426 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
15427 expr
, input_location
);
15428 else if (TYPE_P (scope
))
15430 expr
= (adjust_result_of_qualified_name_lookup
15431 (expr
, scope
, current_nonlambda_class_type ()));
15432 expr
= (finish_qualified_id_expr
15433 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
15434 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
15435 /*template_arg_p=*/false, complain
));
15438 /* Expressions do not generally have reference type. */
15439 if (TREE_CODE (expr
) != SCOPE_REF
15440 /* However, if we're about to form a pointer-to-member, we just
15441 want the referenced member referenced. */
15442 && TREE_CODE (expr
) != OFFSET_REF
)
15443 expr
= convert_from_reference (expr
);
15445 if (REF_PARENTHESIZED_P (qualified_id
))
15446 expr
= force_paren_expr (expr
);
15451 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15452 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15456 tsubst_init (tree init
, tree decl
, tree args
,
15457 tsubst_flags_t complain
, tree in_decl
)
15462 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
15464 tree type
= TREE_TYPE (decl
);
15466 if (!init
&& type
!= error_mark_node
)
15468 if (tree auto_node
= type_uses_auto (type
))
15470 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
15472 if (complain
& tf_error
)
15473 error ("initializer for %q#D expands to an empty list "
15474 "of expressions", decl
);
15475 return error_mark_node
;
15478 else if (!dependent_type_p (type
))
15480 /* If we had an initializer but it
15481 instantiated to nothing,
15482 value-initialize the object. This will
15483 only occur when the initializer was a
15484 pack expansion where the parameter packs
15485 used in that expansion were of length
15487 init
= build_value_init (type
, complain
);
15488 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
15489 init
= get_target_expr_sfinae (init
, complain
);
15490 if (TREE_CODE (init
) == TARGET_EXPR
)
15491 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
15498 /* Like tsubst, but deals with expressions. This function just replaces
15499 template parms; to finish processing the resultant expression, use
15500 tsubst_copy_and_build or tsubst_expr. */
15503 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
15505 enum tree_code code
;
15508 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
15511 code
= TREE_CODE (t
);
15516 r
= retrieve_local_specialization (t
);
15518 if (r
== NULL_TREE
)
15520 /* We get here for a use of 'this' in an NSDMI. */
15521 if (DECL_NAME (t
) == this_identifier
&& current_class_ptr
)
15522 return current_class_ptr
;
15524 /* This can happen for a parameter name used later in a function
15525 declaration (such as in a late-specified return type). Just
15526 make a dummy decl, since it's only used for its type. */
15527 gcc_assert (cp_unevaluated_operand
!= 0);
15528 r
= tsubst_decl (t
, args
, complain
);
15529 /* Give it the template pattern as its context; its true context
15530 hasn't been instantiated yet and this is good enough for
15532 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
15535 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15536 r
= argument_pack_select_arg (r
);
15537 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
15538 return error_mark_node
;
15546 if (DECL_TEMPLATE_PARM_P (t
))
15547 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
15548 /* There is no need to substitute into namespace-scope
15550 if (DECL_NAMESPACE_SCOPE_P (t
))
15552 /* If ARGS is NULL, then T is known to be non-dependent. */
15553 if (args
== NULL_TREE
)
15554 return scalar_constant_value (t
);
15556 /* Unfortunately, we cannot just call lookup_name here.
15559 template <int I> int f() {
15561 struct S { void g() { E e = a; } };
15564 When we instantiate f<7>::S::g(), say, lookup_name is not
15565 clever enough to find f<7>::a. */
15567 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15568 /*entering_scope=*/0);
15570 for (v
= TYPE_VALUES (enum_type
);
15572 v
= TREE_CHAIN (v
))
15573 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
15574 return TREE_VALUE (v
);
15576 /* We didn't find the name. That should never happen; if
15577 name-lookup found it during preliminary parsing, we
15578 should find it again here during instantiation. */
15579 gcc_unreachable ();
15584 if (DECL_CONTEXT (t
))
15588 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
15589 /*entering_scope=*/1);
15590 if (ctx
!= DECL_CONTEXT (t
))
15592 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
15595 if (complain
& tf_error
)
15596 error ("using invalid field %qD", t
);
15597 return error_mark_node
;
15606 case FUNCTION_DECL
:
15607 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
15608 r
= tsubst (t
, args
, complain
, in_decl
);
15609 else if (local_variable_p (t
)
15610 && uses_template_parms (DECL_CONTEXT (t
)))
15612 r
= retrieve_local_specialization (t
);
15613 if (r
== NULL_TREE
)
15615 /* First try name lookup to find the instantiation. */
15616 r
= lookup_name (DECL_NAME (t
));
15621 /* During error-recovery we may find a non-variable,
15622 even an OVERLOAD: just bail out and avoid ICEs and
15623 duplicate diagnostics (c++/62207). */
15624 gcc_assert (seen_error ());
15625 return error_mark_node
;
15627 if (!is_capture_proxy (r
))
15629 /* Make sure the one we found is the one we want. */
15630 tree ctx
= enclosing_instantiation_of (DECL_CONTEXT (t
));
15631 if (ctx
!= DECL_CONTEXT (r
))
15640 /* This can happen for a variable used in a
15641 late-specified return type of a local lambda, or for a
15642 local static or constant. Building a new VAR_DECL
15643 should be OK in all those cases. */
15644 r
= tsubst_decl (t
, args
, complain
);
15645 if (local_specializations
)
15646 /* Avoid infinite recursion (79640). */
15647 register_local_specialization (r
, t
);
15648 if (decl_maybe_constant_var_p (r
))
15650 /* We can't call cp_finish_decl, so handle the
15651 initializer by hand. */
15652 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
15653 complain
, in_decl
);
15654 if (!processing_template_decl
)
15655 init
= maybe_constant_init (init
);
15656 if (processing_template_decl
15657 ? potential_constant_expression (init
)
15658 : reduced_constant_expression_p (init
))
15659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
15660 = TREE_CONSTANT (r
) = true;
15661 DECL_INITIAL (r
) = init
;
15662 if (tree auto_node
= type_uses_auto (TREE_TYPE (r
)))
15664 = do_auto_deduction (TREE_TYPE (r
), init
, auto_node
,
15665 complain
, adc_variable_type
);
15667 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
15668 || decl_constant_var_p (r
)
15670 if (!processing_template_decl
15671 && !TREE_STATIC (r
))
15672 r
= process_outer_var_ref (r
, complain
);
15674 /* Remember this for subsequent uses. */
15675 if (local_specializations
)
15676 register_local_specialization (r
, t
);
15678 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
15679 r
= argument_pack_select_arg (r
);
15683 if (!mark_used (r
, complain
))
15684 return error_mark_node
;
15687 case NAMESPACE_DECL
:
15694 return tsubst_baselink (t
, current_nonlambda_class_type (),
15695 args
, complain
, in_decl
);
15697 case TEMPLATE_DECL
:
15698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
15699 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
15700 args
, complain
, in_decl
);
15701 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
15702 return tsubst (t
, args
, complain
, in_decl
);
15703 else if (DECL_CLASS_SCOPE_P (t
)
15704 && uses_template_parms (DECL_CONTEXT (t
)))
15706 /* Template template argument like the following example need
15709 template <template <class> class TT> struct C {};
15710 template <class T> struct D {
15711 template <class U> struct E {};
15716 We are processing the template argument `E' in #1 for
15717 the template instantiation #2. Originally, `E' is a
15718 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15719 have to substitute this with one having context `D<int>'. */
15721 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
15722 if (dependent_scope_p (context
))
15724 /* When rewriting a constructor into a deduction guide, a
15725 non-dependent name can become dependent, so memtmpl<args>
15726 becomes context::template memtmpl<args>. */
15727 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15728 return build_qualified_name (type
, context
, DECL_NAME (t
),
15731 return lookup_field (context
, DECL_NAME(t
), 0, false);
15734 /* Ordinary template template argument. */
15737 case NON_LVALUE_EXPR
:
15738 case VIEW_CONVERT_EXPR
:
15740 /* Handle location wrappers by substituting the wrapped node
15741 first, *then* reusing the resulting type. Doing the type
15742 first ensures that we handle template parameters and
15743 parameter pack expansions. */
15744 if (location_wrapper_p (t
))
15746 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
,
15747 complain
, in_decl
);
15748 return maybe_wrap_with_location (op0
, EXPR_LOCATION (t
));
15750 tree op
= TREE_OPERAND (t
, 0);
15751 if (code
== VIEW_CONVERT_EXPR
15752 && TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15754 /* Wrapper to make a C++20 template parameter object const. */
15755 op
= tsubst_copy (op
, args
, complain
, in_decl
);
15756 if (TREE_CODE (op
) == TEMPLATE_PARM_INDEX
)
15758 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15759 return build1 (code
, type
, op
);
15763 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op
)));
15767 /* We shouldn't see any other uses of these in templates. */
15768 gcc_unreachable ();
15772 case REINTERPRET_CAST_EXPR
:
15773 case CONST_CAST_EXPR
:
15774 case STATIC_CAST_EXPR
:
15775 case DYNAMIC_CAST_EXPR
:
15776 case IMPLICIT_CONV_EXPR
:
15780 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15781 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15782 return build1 (code
, type
, op0
);
15786 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
15787 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
15789 tree expanded
, op
= TREE_OPERAND (t
, 0);
15792 if (SIZEOF_EXPR_TYPE_P (t
))
15793 op
= TREE_TYPE (op
);
15795 ++cp_unevaluated_operand
;
15796 ++c_inhibit_evaluation_warnings
;
15797 /* We only want to compute the number of arguments. */
15798 if (PACK_EXPANSION_P (op
))
15799 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
15801 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
15802 args
, complain
, in_decl
);
15803 --cp_unevaluated_operand
;
15804 --c_inhibit_evaluation_warnings
;
15806 if (TREE_CODE (expanded
) == TREE_VEC
)
15808 len
= TREE_VEC_LENGTH (expanded
);
15809 /* Set TREE_USED for the benefit of -Wunused. */
15810 for (int i
= 0; i
< len
; i
++)
15811 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
15812 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
15815 if (expanded
== error_mark_node
)
15816 return error_mark_node
;
15817 else if (PACK_EXPANSION_P (expanded
)
15818 || (TREE_CODE (expanded
) == TREE_VEC
15819 && pack_expansion_args_count (expanded
)))
15822 if (PACK_EXPANSION_P (expanded
))
15824 else if (TREE_VEC_LENGTH (expanded
) == 1)
15825 expanded
= TREE_VEC_ELT (expanded
, 0);
15827 expanded
= make_argument_pack (expanded
);
15829 if (TYPE_P (expanded
))
15830 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
15832 complain
& tf_error
);
15834 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
15835 complain
& tf_error
);
15838 return build_int_cst (size_type_node
, len
);
15840 if (SIZEOF_EXPR_TYPE_P (t
))
15842 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
15843 args
, complain
, in_decl
);
15844 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
15845 r
= build1 (SIZEOF_EXPR
,
15846 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
15847 SIZEOF_EXPR_TYPE_P (r
) = 1;
15854 case TRUTH_NOT_EXPR
:
15857 case UNARY_PLUS_EXPR
: /* Unary + */
15859 case AT_ENCODE_EXPR
:
15863 case REALPART_EXPR
:
15864 case IMAGPART_EXPR
:
15867 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15868 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15869 r
= build1 (code
, type
, op0
);
15870 if (code
== ALIGNOF_EXPR
)
15871 ALIGNOF_EXPR_STD_P (r
) = ALIGNOF_EXPR_STD_P (t
);
15875 case COMPONENT_REF
:
15880 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15881 name
= TREE_OPERAND (t
, 1);
15882 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
15884 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15885 complain
, in_decl
);
15886 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15888 else if (TREE_CODE (name
) == SCOPE_REF
15889 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
15891 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15892 complain
, in_decl
);
15893 name
= TREE_OPERAND (name
, 1);
15894 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
15895 complain
, in_decl
);
15896 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
15897 name
= build_qualified_name (/*type=*/NULL_TREE
,
15899 /*template_p=*/false);
15901 else if (BASELINK_P (name
))
15902 name
= tsubst_baselink (name
,
15903 non_reference (TREE_TYPE (object
)),
15907 name
= tsubst_copy (name
, args
, complain
, in_decl
);
15908 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
15914 case TRUNC_DIV_EXPR
:
15915 case CEIL_DIV_EXPR
:
15916 case FLOOR_DIV_EXPR
:
15917 case ROUND_DIV_EXPR
:
15918 case EXACT_DIV_EXPR
:
15922 case TRUNC_MOD_EXPR
:
15923 case FLOOR_MOD_EXPR
:
15924 case TRUTH_ANDIF_EXPR
:
15925 case TRUTH_ORIF_EXPR
:
15926 case TRUTH_AND_EXPR
:
15927 case TRUTH_OR_EXPR
:
15940 case COMPOUND_EXPR
:
15943 case PREDECREMENT_EXPR
:
15944 case PREINCREMENT_EXPR
:
15945 case POSTDECREMENT_EXPR
:
15946 case POSTINCREMENT_EXPR
:
15948 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15949 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15950 return build_nt (code
, op0
, op1
);
15955 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15956 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15957 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
15958 QUALIFIED_NAME_IS_TEMPLATE (t
));
15963 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15964 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15965 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
15970 int n
= VL_EXP_OPERAND_LENGTH (t
);
15971 tree result
= build_vl_exp (CALL_EXPR
, n
);
15973 for (i
= 0; i
< n
; i
++)
15974 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
15975 complain
, in_decl
);
15981 case PSEUDO_DTOR_EXPR
:
15982 case VEC_PERM_EXPR
:
15984 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15985 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15986 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15987 r
= build_nt (code
, op0
, op1
, op2
);
15988 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
15994 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15995 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15996 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
15997 r
= build_nt (code
, op0
, op1
, op2
);
15998 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
16004 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16005 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16006 r
= build_nt (code
, op0
, op1
);
16007 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
16008 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
16012 case TEMPLATE_ID_EXPR
:
16014 /* Substituted template arguments */
16015 tree fn
= TREE_OPERAND (t
, 0);
16016 tree targs
= TREE_OPERAND (t
, 1);
16018 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
16020 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16022 return lookup_template_function (fn
, targs
);
16027 tree purpose
, value
, chain
;
16029 if (t
== void_list_node
)
16032 purpose
= TREE_PURPOSE (t
);
16034 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
16035 value
= TREE_VALUE (t
);
16037 value
= tsubst_copy (value
, args
, complain
, in_decl
);
16038 chain
= TREE_CHAIN (t
);
16039 if (chain
&& chain
!= void_type_node
)
16040 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
16041 if (purpose
== TREE_PURPOSE (t
)
16042 && value
== TREE_VALUE (t
)
16043 && chain
== TREE_CHAIN (t
))
16045 return tree_cons (purpose
, value
, chain
);
16050 case ENUMERAL_TYPE
:
16052 case TEMPLATE_TYPE_PARM
:
16053 case TEMPLATE_TEMPLATE_PARM
:
16054 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16055 case TEMPLATE_PARM_INDEX
:
16057 case REFERENCE_TYPE
:
16059 case FUNCTION_TYPE
:
16062 case TYPENAME_TYPE
:
16063 case UNBOUND_CLASS_TEMPLATE
:
16065 case DECLTYPE_TYPE
:
16067 return tsubst (t
, args
, complain
, in_decl
);
16071 /* Fall through. */
16072 case IDENTIFIER_NODE
:
16073 if (IDENTIFIER_CONV_OP_P (t
))
16075 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16076 return make_conv_op_name (new_type
);
16082 /* This is handled by tsubst_copy_and_build. */
16083 gcc_unreachable ();
16087 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16088 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16089 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
16092 case CLEANUP_POINT_EXPR
:
16093 /* We shouldn't have built any of these during initial template
16094 generation. Instead, they should be built during instantiation
16095 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16096 gcc_unreachable ();
16100 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16101 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
16102 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16103 r
= build2 (code
, type
, op0
, op1
);
16104 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
16105 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
16106 && !(complain
& tf_error
))
16107 return error_mark_node
;
16111 case EXPR_PACK_EXPANSION
:
16112 error ("invalid use of pack expansion expression");
16113 return error_mark_node
;
16115 case NONTYPE_ARGUMENT_PACK
:
16116 error ("use %<...%> to expand argument pack");
16117 return error_mark_node
;
16120 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
16128 /* Instantiate any typedefs in the type. */
16129 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16130 r
= fold_convert (type
, t
);
16131 gcc_assert (TREE_CODE (r
) == code
);
16136 /* These can sometimes show up in a partial instantiation, but never
16137 involve template parms. */
16138 gcc_assert (!uses_template_parms (t
));
16141 case UNARY_LEFT_FOLD_EXPR
:
16142 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
16143 case UNARY_RIGHT_FOLD_EXPR
:
16144 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
16145 case BINARY_LEFT_FOLD_EXPR
:
16146 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
16147 case BINARY_RIGHT_FOLD_EXPR
:
16148 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
16152 case DEBUG_BEGIN_STMT
:
16153 /* ??? There's no point in copying it for now, but maybe some
16154 day it will contain more information, such as a pointer back
16155 to the containing function, inlined copy or so. */
16159 /* We shouldn't get here, but keep going if !flag_checking. */
16161 gcc_unreachable ();
16166 /* Helper function for tsubst_omp_clauses, used for instantiation of
16167 OMP_CLAUSE_DECL of clauses. */
16170 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
16171 tree in_decl
, tree
*iterator_cache
)
16173 if (decl
== NULL_TREE
)
16176 /* Handle OpenMP iterators. */
16177 if (TREE_CODE (decl
) == TREE_LIST
16178 && TREE_PURPOSE (decl
)
16179 && TREE_CODE (TREE_PURPOSE (decl
)) == TREE_VEC
)
16182 if (iterator_cache
[0] == TREE_PURPOSE (decl
))
16183 ret
= iterator_cache
[1];
16187 begin_scope (sk_omp
, NULL
);
16188 for (tree it
= TREE_PURPOSE (decl
); it
; it
= TREE_CHAIN (it
))
16190 *tp
= copy_node (it
);
16191 TREE_VEC_ELT (*tp
, 0)
16192 = tsubst_decl (TREE_VEC_ELT (it
, 0), args
, complain
);
16193 TREE_VEC_ELT (*tp
, 1)
16194 = tsubst_expr (TREE_VEC_ELT (it
, 1), args
, complain
, in_decl
,
16195 /*integral_constant_expression_p=*/false);
16196 TREE_VEC_ELT (*tp
, 2)
16197 = tsubst_expr (TREE_VEC_ELT (it
, 2), args
, complain
, in_decl
,
16198 /*integral_constant_expression_p=*/false);
16199 TREE_VEC_ELT (*tp
, 3)
16200 = tsubst_expr (TREE_VEC_ELT (it
, 3), args
, complain
, in_decl
,
16201 /*integral_constant_expression_p=*/false);
16202 TREE_CHAIN (*tp
) = NULL_TREE
;
16203 tp
= &TREE_CHAIN (*tp
);
16205 TREE_VEC_ELT (ret
, 5) = poplevel (1, 1, 0);
16206 iterator_cache
[0] = TREE_PURPOSE (decl
);
16207 iterator_cache
[1] = ret
;
16209 return build_tree_list (ret
, tsubst_omp_clause_decl (TREE_VALUE (decl
),
16214 /* Handle an OpenMP array section represented as a TREE_LIST (or
16215 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16216 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16217 TREE_LIST. We can handle it exactly the same as an array section
16218 (purpose, value, and a chain), even though the nomenclature
16219 (low_bound, length, etc) is different. */
16220 if (TREE_CODE (decl
) == TREE_LIST
)
16223 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
16224 /*integral_constant_expression_p=*/false);
16225 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
16226 /*integral_constant_expression_p=*/false);
16227 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
16229 if (TREE_PURPOSE (decl
) == low_bound
16230 && TREE_VALUE (decl
) == length
16231 && TREE_CHAIN (decl
) == chain
)
16233 tree ret
= tree_cons (low_bound
, length
, chain
);
16234 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
16235 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
16238 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
16239 /*integral_constant_expression_p=*/false);
16240 /* Undo convert_from_reference tsubst_expr could have called. */
16242 && REFERENCE_REF_P (ret
)
16243 && !REFERENCE_REF_P (decl
))
16244 ret
= TREE_OPERAND (ret
, 0);
16248 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16251 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
16252 tree args
, tsubst_flags_t complain
, tree in_decl
)
16254 tree new_clauses
= NULL_TREE
, nc
, oc
;
16255 tree linear_no_step
= NULL_TREE
;
16256 tree iterator_cache
[2] = { NULL_TREE
, NULL_TREE
};
16258 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
16260 nc
= copy_node (oc
);
16261 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
16264 switch (OMP_CLAUSE_CODE (nc
))
16266 case OMP_CLAUSE_LASTPRIVATE
:
16267 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
16269 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
16270 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
16271 in_decl
, /*integral_constant_expression_p=*/false);
16272 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
16273 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
16276 case OMP_CLAUSE_PRIVATE
:
16277 case OMP_CLAUSE_SHARED
:
16278 case OMP_CLAUSE_FIRSTPRIVATE
:
16279 case OMP_CLAUSE_COPYIN
:
16280 case OMP_CLAUSE_COPYPRIVATE
:
16281 case OMP_CLAUSE_UNIFORM
:
16282 case OMP_CLAUSE_DEPEND
:
16283 case OMP_CLAUSE_FROM
:
16284 case OMP_CLAUSE_TO
:
16285 case OMP_CLAUSE_MAP
:
16286 case OMP_CLAUSE_NONTEMPORAL
:
16287 case OMP_CLAUSE_USE_DEVICE_PTR
:
16288 case OMP_CLAUSE_IS_DEVICE_PTR
:
16289 OMP_CLAUSE_DECL (nc
)
16290 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16291 in_decl
, iterator_cache
);
16293 case OMP_CLAUSE_TILE
:
16294 case OMP_CLAUSE_IF
:
16295 case OMP_CLAUSE_NUM_THREADS
:
16296 case OMP_CLAUSE_SCHEDULE
:
16297 case OMP_CLAUSE_COLLAPSE
:
16298 case OMP_CLAUSE_FINAL
:
16299 case OMP_CLAUSE_DEVICE
:
16300 case OMP_CLAUSE_DIST_SCHEDULE
:
16301 case OMP_CLAUSE_NUM_TEAMS
:
16302 case OMP_CLAUSE_THREAD_LIMIT
:
16303 case OMP_CLAUSE_SAFELEN
:
16304 case OMP_CLAUSE_SIMDLEN
:
16305 case OMP_CLAUSE_NUM_TASKS
:
16306 case OMP_CLAUSE_GRAINSIZE
:
16307 case OMP_CLAUSE_PRIORITY
:
16308 case OMP_CLAUSE_ORDERED
:
16309 case OMP_CLAUSE_HINT
:
16310 case OMP_CLAUSE_NUM_GANGS
:
16311 case OMP_CLAUSE_NUM_WORKERS
:
16312 case OMP_CLAUSE_VECTOR_LENGTH
:
16313 case OMP_CLAUSE_WORKER
:
16314 case OMP_CLAUSE_VECTOR
:
16315 case OMP_CLAUSE_ASYNC
:
16316 case OMP_CLAUSE_WAIT
:
16317 OMP_CLAUSE_OPERAND (nc
, 0)
16318 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
16319 in_decl
, /*integral_constant_expression_p=*/false);
16321 case OMP_CLAUSE_REDUCTION
:
16322 case OMP_CLAUSE_IN_REDUCTION
:
16323 case OMP_CLAUSE_TASK_REDUCTION
:
16324 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
16326 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
16327 if (TREE_CODE (placeholder
) == SCOPE_REF
)
16329 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
16330 complain
, in_decl
);
16331 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
16332 = build_qualified_name (NULL_TREE
, scope
,
16333 TREE_OPERAND (placeholder
, 1),
16337 gcc_assert (identifier_p (placeholder
));
16339 OMP_CLAUSE_DECL (nc
)
16340 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16343 case OMP_CLAUSE_GANG
:
16344 case OMP_CLAUSE_ALIGNED
:
16345 OMP_CLAUSE_DECL (nc
)
16346 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16348 OMP_CLAUSE_OPERAND (nc
, 1)
16349 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
16350 in_decl
, /*integral_constant_expression_p=*/false);
16352 case OMP_CLAUSE_LINEAR
:
16353 OMP_CLAUSE_DECL (nc
)
16354 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
16356 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
16358 gcc_assert (!linear_no_step
);
16359 linear_no_step
= nc
;
16361 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
16362 OMP_CLAUSE_LINEAR_STEP (nc
)
16363 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
16364 complain
, in_decl
, NULL
);
16366 OMP_CLAUSE_LINEAR_STEP (nc
)
16367 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
16369 /*integral_constant_expression_p=*/false);
16371 case OMP_CLAUSE_NOWAIT
:
16372 case OMP_CLAUSE_DEFAULT
:
16373 case OMP_CLAUSE_UNTIED
:
16374 case OMP_CLAUSE_MERGEABLE
:
16375 case OMP_CLAUSE_INBRANCH
:
16376 case OMP_CLAUSE_NOTINBRANCH
:
16377 case OMP_CLAUSE_PROC_BIND
:
16378 case OMP_CLAUSE_FOR
:
16379 case OMP_CLAUSE_PARALLEL
:
16380 case OMP_CLAUSE_SECTIONS
:
16381 case OMP_CLAUSE_TASKGROUP
:
16382 case OMP_CLAUSE_NOGROUP
:
16383 case OMP_CLAUSE_THREADS
:
16384 case OMP_CLAUSE_SIMD
:
16385 case OMP_CLAUSE_DEFAULTMAP
:
16386 case OMP_CLAUSE_INDEPENDENT
:
16387 case OMP_CLAUSE_AUTO
:
16388 case OMP_CLAUSE_SEQ
:
16389 case OMP_CLAUSE_IF_PRESENT
:
16390 case OMP_CLAUSE_FINALIZE
:
16393 gcc_unreachable ();
16395 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
16396 switch (OMP_CLAUSE_CODE (nc
))
16398 case OMP_CLAUSE_SHARED
:
16399 case OMP_CLAUSE_PRIVATE
:
16400 case OMP_CLAUSE_FIRSTPRIVATE
:
16401 case OMP_CLAUSE_LASTPRIVATE
:
16402 case OMP_CLAUSE_COPYPRIVATE
:
16403 case OMP_CLAUSE_LINEAR
:
16404 case OMP_CLAUSE_REDUCTION
:
16405 case OMP_CLAUSE_IN_REDUCTION
:
16406 case OMP_CLAUSE_TASK_REDUCTION
:
16407 case OMP_CLAUSE_USE_DEVICE_PTR
:
16408 case OMP_CLAUSE_IS_DEVICE_PTR
:
16409 /* tsubst_expr on SCOPE_REF results in returning
16410 finish_non_static_data_member result. Undo that here. */
16411 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
16412 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
16413 == IDENTIFIER_NODE
))
16415 tree t
= OMP_CLAUSE_DECL (nc
);
16418 switch (TREE_CODE (v
))
16420 case COMPONENT_REF
:
16424 case POINTER_PLUS_EXPR
:
16425 v
= TREE_OPERAND (v
, 0);
16428 if (DECL_CONTEXT (v
) == current_function_decl
16429 && DECL_ARTIFICIAL (v
)
16430 && DECL_NAME (v
) == this_identifier
)
16431 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
16438 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
16439 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
16440 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
16441 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
16442 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
16444 tree decl
= OMP_CLAUSE_DECL (nc
);
16447 retrofit_lang_decl (decl
);
16448 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
16457 new_clauses
= nreverse (new_clauses
);
16458 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
16460 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
16461 if (linear_no_step
)
16462 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
16463 if (nc
== linear_no_step
)
16465 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
16469 return new_clauses
;
16472 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16475 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
16478 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16480 tree purpose
, value
, chain
;
16485 if (TREE_CODE (t
) != TREE_LIST
)
16486 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16487 /*function_p=*/false,
16488 /*integral_constant_expression_p=*/false);
16490 if (t
== void_list_node
)
16493 purpose
= TREE_PURPOSE (t
);
16495 purpose
= RECUR (purpose
);
16496 value
= TREE_VALUE (t
);
16499 if (TREE_CODE (value
) != LABEL_DECL
)
16500 value
= RECUR (value
);
16503 value
= lookup_label (DECL_NAME (value
));
16504 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
16505 TREE_USED (value
) = 1;
16508 chain
= TREE_CHAIN (t
);
16509 if (chain
&& chain
!= void_type_node
)
16510 chain
= RECUR (chain
);
16511 return tree_cons (purpose
, value
, chain
);
16515 /* Used to temporarily communicate the list of #pragma omp parallel
16516 clauses to #pragma omp for instantiation if they are combined
16519 static tree
*omp_parallel_combined_clauses
;
16521 static tree
tsubst_decomp_names (tree
, tree
, tree
, tsubst_flags_t
, tree
,
16522 tree
*, unsigned int *);
16524 /* Substitute one OMP_FOR iterator. */
16527 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree
&orig_declv
,
16528 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
16529 tree args
, tsubst_flags_t complain
, tree in_decl
,
16530 bool integral_constant_expression_p
)
16532 #define RECUR(NODE) \
16533 tsubst_expr ((NODE), args, complain, in_decl, \
16534 integral_constant_expression_p)
16535 tree decl
, init
, cond
= NULL_TREE
, incr
= NULL_TREE
;
16538 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
16539 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
16541 decl
= TREE_OPERAND (init
, 0);
16542 init
= TREE_OPERAND (init
, 1);
16543 tree decl_expr
= NULL_TREE
;
16544 bool range_for
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
) == global_namespace
;
16547 bool decomp
= false;
16548 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
16550 tree v
= DECL_VALUE_EXPR (decl
);
16551 if (TREE_CODE (v
) == ARRAY_REF
16552 && VAR_P (TREE_OPERAND (v
, 0))
16553 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
16555 tree decomp_first
= NULL_TREE
;
16556 unsigned decomp_cnt
= 0;
16557 tree d
= tsubst_decl (TREE_OPERAND (v
, 0), args
, complain
);
16558 maybe_push_decl (d
);
16559 d
= tsubst_decomp_names (d
, TREE_OPERAND (v
, 0), args
, complain
,
16560 in_decl
, &decomp_first
, &decomp_cnt
);
16562 if (d
== error_mark_node
)
16563 decl
= error_mark_node
;
16565 for (unsigned int i
= 0; i
< decomp_cnt
; i
++)
16567 if (!DECL_HAS_VALUE_EXPR_P (decomp_first
))
16569 tree v
= build_nt (ARRAY_REF
, d
,
16570 size_int (decomp_cnt
- i
- 1),
16571 NULL_TREE
, NULL_TREE
);
16572 SET_DECL_VALUE_EXPR (decomp_first
, v
);
16573 DECL_HAS_VALUE_EXPR_P (decomp_first
) = 1;
16575 fit_decomposition_lang_decl (decomp_first
, d
);
16576 decomp_first
= DECL_CHAIN (decomp_first
);
16580 decl
= tsubst_decl (decl
, args
, complain
);
16582 maybe_push_decl (decl
);
16584 else if (init
&& TREE_CODE (init
) == DECL_EXPR
)
16586 /* We need to jump through some hoops to handle declarations in the
16587 init-statement, since we might need to handle auto deduction,
16588 but we need to keep control of initialization. */
16590 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
16591 decl
= tsubst_decl (decl
, args
, complain
);
16595 if (TREE_CODE (decl
) == SCOPE_REF
)
16597 decl
= RECUR (decl
);
16598 if (TREE_CODE (decl
) == COMPONENT_REF
)
16602 switch (TREE_CODE (v
))
16604 case COMPONENT_REF
:
16608 case POINTER_PLUS_EXPR
:
16609 v
= TREE_OPERAND (v
, 0);
16612 if (DECL_CONTEXT (v
) == current_function_decl
16613 && DECL_ARTIFICIAL (v
)
16614 && DECL_NAME (v
) == this_identifier
)
16616 decl
= TREE_OPERAND (decl
, 1);
16617 decl
= omp_privatize_field (decl
, false);
16627 decl
= RECUR (decl
);
16629 init
= RECUR (init
);
16631 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
16633 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
16634 if (TREE_CODE (o
) == TREE_LIST
)
16635 TREE_VEC_ELT (orig_declv
, i
)
16636 = tree_cons (RECUR (TREE_PURPOSE (o
)),
16637 RECUR (TREE_VALUE (o
)),
16640 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
16645 tree this_pre_body
= NULL_TREE
;
16646 tree orig_init
= NULL_TREE
;
16647 tree orig_decl
= NULL_TREE
;
16648 cp_convert_omp_range_for (this_pre_body
, NULL
, decl
, orig_decl
, init
,
16649 orig_init
, cond
, incr
);
16652 if (orig_declv
== NULL_TREE
)
16653 orig_declv
= copy_node (declv
);
16654 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
16657 else if (orig_declv
)
16658 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16661 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
16662 if (!range_for
&& auto_node
&& init
)
16664 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
, complain
);
16666 gcc_assert (!type_dependent_expression_p (decl
));
16668 if (!CLASS_TYPE_P (TREE_TYPE (decl
)) || range_for
)
16672 /* Declare the variable, but don't let that initialize it. */
16673 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
16674 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
16676 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
16681 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
16682 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16683 if (TREE_CODE (incr
) == MODIFY_EXPR
)
16685 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16686 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
16687 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
16688 NOP_EXPR
, rhs
, complain
);
16691 incr
= RECUR (incr
);
16692 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16693 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16695 TREE_VEC_ELT (declv
, i
) = decl
;
16696 TREE_VEC_ELT (initv
, i
) = init
;
16697 TREE_VEC_ELT (condv
, i
) = cond
;
16698 TREE_VEC_ELT (incrv
, i
) = incr
;
16704 /* Declare and initialize the variable. */
16712 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
16714 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
16716 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
16717 && OMP_CLAUSE_DECL (*pc
) == decl
)
16719 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
16720 && OMP_CLAUSE_DECL (*pc
) == decl
)
16724 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16726 *pc
= OMP_CLAUSE_CHAIN (c
);
16727 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16730 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
16731 && OMP_CLAUSE_DECL (*pc
) == decl
)
16733 error ("iteration variable %qD should not be firstprivate",
16735 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16737 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
16738 && OMP_CLAUSE_DECL (*pc
) == decl
)
16740 error ("iteration variable %qD should not be reduction",
16742 *pc
= OMP_CLAUSE_CHAIN (*pc
);
16745 pc
= &OMP_CLAUSE_CHAIN (*pc
);
16750 if (*pc
== NULL_TREE
)
16752 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
16753 OMP_CLAUSE_DECL (c
) = decl
;
16754 c
= finish_omp_clauses (c
, C_ORT_OMP
);
16757 OMP_CLAUSE_CHAIN (c
) = *clauses
;
16762 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
16763 if (COMPARISON_CLASS_P (cond
))
16765 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
16766 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
16767 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
16770 cond
= RECUR (cond
);
16771 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
16772 switch (TREE_CODE (incr
))
16774 case PREINCREMENT_EXPR
:
16775 case PREDECREMENT_EXPR
:
16776 case POSTINCREMENT_EXPR
:
16777 case POSTDECREMENT_EXPR
:
16778 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
16779 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
16782 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16783 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16785 tree rhs
= TREE_OPERAND (incr
, 1);
16786 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16787 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16788 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16789 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16790 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16794 incr
= RECUR (incr
);
16797 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
16798 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
16800 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16801 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16802 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
16803 TREE_TYPE (decl
), lhs
,
16804 RECUR (TREE_OPERAND (incr
, 2))));
16806 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
16807 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
16808 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
16810 tree rhs
= TREE_OPERAND (incr
, 2);
16811 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
16812 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
16813 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
16814 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
16815 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
16819 incr
= RECUR (incr
);
16822 incr
= RECUR (incr
);
16826 if (orig_declv
&& !OMP_FOR_ORIG_DECLS (t
))
16827 TREE_VEC_ELT (orig_declv
, i
) = decl
;
16828 TREE_VEC_ELT (declv
, i
) = decl
;
16829 TREE_VEC_ELT (initv
, i
) = init
;
16830 TREE_VEC_ELT (condv
, i
) = cond
;
16831 TREE_VEC_ELT (incrv
, i
) = incr
;
16836 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16837 of OMP_TARGET's body. */
16840 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
16842 *walk_subtrees
= 0;
16843 switch (TREE_CODE (*tp
))
16848 case STATEMENT_LIST
:
16849 *walk_subtrees
= 1;
16857 /* Helper function for tsubst_expr. For decomposition declaration
16858 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16859 also the corresponding decls representing the identifiers
16860 of the decomposition declaration. Return DECL if successful
16861 or error_mark_node otherwise, set *FIRST to the first decl
16862 in the list chained through DECL_CHAIN and *CNT to the number
16866 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
16867 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
16870 tree decl2
, decl3
, prev
= decl
;
16872 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
16873 for (decl2
= DECL_CHAIN (pattern_decl
);
16876 && DECL_DECOMPOSITION_P (decl2
)
16877 && DECL_NAME (decl2
);
16878 decl2
= DECL_CHAIN (decl2
))
16880 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
16882 gcc_assert (errorcount
);
16883 return error_mark_node
;
16886 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
16887 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
16888 tree v
= DECL_VALUE_EXPR (decl2
);
16889 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
16890 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
16891 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
16892 SET_DECL_VALUE_EXPR (decl2
, v
);
16893 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
16895 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
16898 gcc_assert (errorcount
);
16899 decl
= error_mark_node
;
16902 maybe_push_decl (decl3
);
16903 if (error_operand_p (decl3
))
16904 decl
= error_mark_node
;
16905 else if (decl
!= error_mark_node
16906 && DECL_CHAIN (decl3
) != prev
16909 gcc_assert (errorcount
);
16910 decl
= error_mark_node
;
16919 /* Return the proper local_specialization for init-capture pack DECL. */
16922 lookup_init_capture_pack (tree decl
)
16924 /* We handle normal pack captures by forwarding to the specialization of the
16925 captured parameter. We can't do that for pack init-captures; we need them
16926 to have their own local_specialization. We created the individual
16927 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16928 when we process the DECL_EXPR for the pack init-capture in the template.
16929 So, how do we find them? We don't know the capture proxy pack when
16930 building the individual resulting proxies, and we don't know the
16931 individual proxies when instantiating the pack. What we have in common is
16934 So...when we instantiate the FIELD_DECL, we stick the result in
16935 local_specializations. Then at the DECL_EXPR we look up that result, see
16936 how many elements it has, synthesize the names, and look them up. */
16938 tree cname
= DECL_NAME (decl
);
16939 tree val
= DECL_VALUE_EXPR (decl
);
16940 tree field
= TREE_OPERAND (val
, 1);
16941 gcc_assert (TREE_CODE (field
) == FIELD_DECL
);
16942 tree fpack
= retrieve_local_specialization (field
);
16943 if (fpack
== error_mark_node
)
16944 return error_mark_node
;
16947 tree vec
= NULL_TREE
;
16948 tree r
= NULL_TREE
;
16949 if (TREE_CODE (fpack
) == TREE_VEC
)
16951 len
= TREE_VEC_LENGTH (fpack
);
16952 vec
= make_tree_vec (len
);
16953 r
= make_node (NONTYPE_ARGUMENT_PACK
);
16954 SET_ARGUMENT_PACK_ARGS (r
, vec
);
16956 for (int i
= 0; i
< len
; ++i
)
16958 tree ename
= vec
? make_ith_pack_parameter_name (cname
, i
) : cname
;
16959 tree elt
= lookup_name_real (ename
, 0, 0, true, 0, LOOKUP_NORMAL
);
16961 TREE_VEC_ELT (vec
, i
) = elt
;
16968 /* Like tsubst_copy for expressions, etc. but also does semantic
16972 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
16973 bool integral_constant_expression_p
)
16975 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16976 #define RECUR(NODE) \
16977 tsubst_expr ((NODE), args, complain, in_decl, \
16978 integral_constant_expression_p)
16984 if (t
== NULL_TREE
|| t
== error_mark_node
)
16987 loc
= input_location
;
16988 if (location_t eloc
= cp_expr_location (t
))
16989 input_location
= eloc
;
16990 if (STATEMENT_CODE_P (TREE_CODE (t
)))
16991 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
16993 switch (TREE_CODE (t
))
16995 case STATEMENT_LIST
:
16997 tree_stmt_iterator i
;
16998 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
16999 RECUR (tsi_stmt (i
));
17003 case CTOR_INITIALIZER
:
17004 finish_mem_initializers (tsubst_initializer_list
17005 (TREE_OPERAND (t
, 0), args
));
17009 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
17013 tmp
= RECUR (EXPR_STMT_EXPR (t
));
17014 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
17015 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
17017 finish_expr_stmt (tmp
);
17021 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
17022 /*attribs=*/NULL_TREE
);
17027 tree decl
, pattern_decl
;
17030 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
17031 if (TREE_CODE (decl
) == LABEL_DECL
)
17032 finish_label_decl (DECL_NAME (decl
));
17033 else if (TREE_CODE (decl
) == USING_DECL
)
17035 tree scope
= USING_DECL_SCOPE (decl
);
17036 tree name
= DECL_NAME (decl
);
17038 scope
= tsubst (scope
, args
, complain
, in_decl
);
17039 decl
= lookup_qualified_name (scope
, name
,
17040 /*is_type_p=*/false,
17041 /*complain=*/false);
17042 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
17043 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
17045 finish_local_using_decl (decl
, scope
, name
);
17047 else if (is_capture_proxy (decl
)
17048 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
17050 /* We're in tsubst_lambda_expr, we've already inserted a new
17051 capture proxy, so look it up and register it. */
17053 if (!DECL_PACK_P (decl
))
17055 inst
= lookup_name_real (DECL_NAME (decl
), /*prefer_type*/0,
17056 /*nonclass*/1, /*block_p=*/true,
17057 /*ns_only*/0, LOOKUP_HIDDEN
);
17058 gcc_assert (inst
!= decl
&& is_capture_proxy (inst
));
17060 else if (is_normal_capture_proxy (decl
))
17062 inst
= (retrieve_local_specialization
17063 (DECL_CAPTURED_VARIABLE (decl
)));
17064 gcc_assert (TREE_CODE (inst
) == NONTYPE_ARGUMENT_PACK
);
17067 inst
= lookup_init_capture_pack (decl
);
17069 register_local_specialization (inst
, decl
);
17072 else if (DECL_PRETTY_FUNCTION_P (decl
))
17073 decl
= make_fname_decl (DECL_SOURCE_LOCATION (decl
),
17075 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17076 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
17077 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
17078 /* Don't copy the old closure; we'll create a new one in
17079 tsubst_lambda_expr. */
17083 init
= DECL_INITIAL (decl
);
17084 decl
= tsubst (decl
, args
, complain
, in_decl
);
17085 if (decl
!= error_mark_node
)
17087 /* By marking the declaration as instantiated, we avoid
17088 trying to instantiate it. Since instantiate_decl can't
17089 handle local variables, and since we've already done
17090 all that needs to be done, that's the right thing to
17093 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17094 if (VAR_P (decl
) && !DECL_NAME (decl
)
17095 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
17096 /* Anonymous aggregates are a special case. */
17097 finish_anon_union (decl
);
17098 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
17100 DECL_CONTEXT (decl
) = current_function_decl
;
17101 if (DECL_NAME (decl
) == this_identifier
)
17103 tree lam
= DECL_CONTEXT (current_function_decl
);
17104 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
17105 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
17107 insert_capture_proxy (decl
);
17109 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
17110 /* We already did a pushtag. */;
17111 else if (TREE_CODE (decl
) == FUNCTION_DECL
17112 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
17113 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
17115 DECL_CONTEXT (decl
) = NULL_TREE
;
17117 DECL_CONTEXT (decl
) = current_function_decl
;
17118 cp_check_omp_declare_reduction (decl
);
17122 int const_init
= false;
17123 unsigned int cnt
= 0;
17124 tree first
= NULL_TREE
, ndecl
= error_mark_node
;
17125 maybe_push_decl (decl
);
17128 && DECL_DECOMPOSITION_P (decl
)
17129 && TREE_TYPE (pattern_decl
) != error_mark_node
)
17130 ndecl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
17131 complain
, in_decl
, &first
,
17134 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
17137 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17140 if (ndecl
!= error_mark_node
)
17141 cp_maybe_mangle_decomp (ndecl
, first
, cnt
);
17143 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
17145 if (ndecl
!= error_mark_node
)
17146 cp_finish_decomp (ndecl
, first
, cnt
);
17155 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
17156 RECUR (FOR_INIT_STMT (t
));
17157 finish_init_stmt (stmt
);
17158 tmp
= RECUR (FOR_COND (t
));
17159 finish_for_cond (tmp
, stmt
, false, 0);
17160 tmp
= RECUR (FOR_EXPR (t
));
17161 finish_for_expr (tmp
, stmt
);
17163 bool prev
= note_iteration_stmt_body_start ();
17164 RECUR (FOR_BODY (t
));
17165 note_iteration_stmt_body_end (prev
);
17167 finish_for_stmt (stmt
);
17170 case RANGE_FOR_STMT
:
17172 /* Construct another range_for, if this is not a final
17173 substitution (for inside inside a generic lambda of a
17174 template). Otherwise convert to a regular for. */
17176 stmt
= (processing_template_decl
17177 ? begin_range_for_stmt (NULL_TREE
, NULL_TREE
)
17178 : begin_for_stmt (NULL_TREE
, NULL_TREE
));
17179 RECUR (RANGE_FOR_INIT_STMT (t
));
17180 decl
= RANGE_FOR_DECL (t
);
17181 decl
= tsubst (decl
, args
, complain
, in_decl
);
17182 maybe_push_decl (decl
);
17183 expr
= RECUR (RANGE_FOR_EXPR (t
));
17185 tree decomp_first
= NULL_TREE
;
17186 unsigned decomp_cnt
= 0;
17187 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
17188 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
17190 &decomp_first
, &decomp_cnt
);
17192 if (processing_template_decl
)
17194 RANGE_FOR_IVDEP (stmt
) = RANGE_FOR_IVDEP (t
);
17195 RANGE_FOR_UNROLL (stmt
) = RANGE_FOR_UNROLL (t
);
17196 finish_range_for_decl (stmt
, decl
, expr
);
17197 if (decomp_first
&& decl
!= error_mark_node
)
17198 cp_finish_decomp (decl
, decomp_first
, decomp_cnt
);
17202 unsigned short unroll
= (RANGE_FOR_UNROLL (t
)
17203 ? tree_to_uhwi (RANGE_FOR_UNROLL (t
)) : 0);
17204 stmt
= cp_convert_range_for (stmt
, decl
, expr
,
17205 decomp_first
, decomp_cnt
,
17206 RANGE_FOR_IVDEP (t
), unroll
);
17209 bool prev
= note_iteration_stmt_body_start ();
17210 RECUR (RANGE_FOR_BODY (t
));
17211 note_iteration_stmt_body_end (prev
);
17212 finish_for_stmt (stmt
);
17217 stmt
= begin_while_stmt ();
17218 tmp
= RECUR (WHILE_COND (t
));
17219 finish_while_stmt_cond (tmp
, stmt
, false, 0);
17221 bool prev
= note_iteration_stmt_body_start ();
17222 RECUR (WHILE_BODY (t
));
17223 note_iteration_stmt_body_end (prev
);
17225 finish_while_stmt (stmt
);
17229 stmt
= begin_do_stmt ();
17231 bool prev
= note_iteration_stmt_body_start ();
17232 RECUR (DO_BODY (t
));
17233 note_iteration_stmt_body_end (prev
);
17235 finish_do_body (stmt
);
17236 tmp
= RECUR (DO_COND (t
));
17237 finish_do_stmt (tmp
, stmt
, false, 0);
17241 stmt
= begin_if_stmt ();
17242 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
17243 if (IF_STMT_CONSTEXPR_P (t
))
17244 args
= add_extra_args (IF_STMT_EXTRA_ARGS (t
), args
);
17245 tmp
= RECUR (IF_COND (t
));
17246 tmp
= finish_if_stmt_cond (tmp
, stmt
);
17247 if (IF_STMT_CONSTEXPR_P (t
)
17248 && instantiation_dependent_expression_p (tmp
))
17250 /* We're partially instantiating a generic lambda, but the condition
17251 of the constexpr if is still dependent. Don't substitute into the
17252 branches now, just remember the template arguments. */
17253 do_poplevel (IF_SCOPE (stmt
));
17254 IF_COND (stmt
) = IF_COND (t
);
17255 THEN_CLAUSE (stmt
) = THEN_CLAUSE (t
);
17256 ELSE_CLAUSE (stmt
) = ELSE_CLAUSE (t
);
17257 IF_STMT_EXTRA_ARGS (stmt
) = build_extra_args (t
, args
, complain
);
17261 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
17262 /* Don't instantiate the THEN_CLAUSE. */;
17265 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17266 bool inhibit
= integer_zerop (folded
);
17268 ++c_inhibit_evaluation_warnings
;
17269 RECUR (THEN_CLAUSE (t
));
17271 --c_inhibit_evaluation_warnings
;
17273 finish_then_clause (stmt
);
17275 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
17276 /* Don't instantiate the ELSE_CLAUSE. */;
17277 else if (ELSE_CLAUSE (t
))
17279 tree folded
= fold_non_dependent_expr (tmp
, complain
);
17280 bool inhibit
= integer_nonzerop (folded
);
17281 begin_else_clause (stmt
);
17283 ++c_inhibit_evaluation_warnings
;
17284 RECUR (ELSE_CLAUSE (t
));
17286 --c_inhibit_evaluation_warnings
;
17287 finish_else_clause (stmt
);
17290 finish_if_stmt (stmt
);
17294 if (BIND_EXPR_BODY_BLOCK (t
))
17295 stmt
= begin_function_body ();
17297 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
17298 ? BCS_TRY_BLOCK
: 0);
17300 RECUR (BIND_EXPR_BODY (t
));
17302 if (BIND_EXPR_BODY_BLOCK (t
))
17303 finish_function_body (stmt
);
17305 finish_compound_stmt (stmt
);
17309 finish_break_stmt ();
17312 case CONTINUE_STMT
:
17313 finish_continue_stmt ();
17317 stmt
= begin_switch_stmt ();
17318 tmp
= RECUR (SWITCH_STMT_COND (t
));
17319 finish_switch_cond (tmp
, stmt
);
17320 RECUR (SWITCH_STMT_BODY (t
));
17321 finish_switch_stmt (stmt
);
17324 case CASE_LABEL_EXPR
:
17326 tree decl
= CASE_LABEL (t
);
17327 tree low
= RECUR (CASE_LOW (t
));
17328 tree high
= RECUR (CASE_HIGH (t
));
17329 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
17330 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
17332 tree label
= CASE_LABEL (l
);
17333 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17334 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17335 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17342 tree decl
= LABEL_EXPR_LABEL (t
);
17345 label
= finish_label_stmt (DECL_NAME (decl
));
17346 if (TREE_CODE (label
) == LABEL_DECL
)
17347 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
17348 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
17349 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
17354 tmp
= GOTO_DESTINATION (t
);
17355 if (TREE_CODE (tmp
) != LABEL_DECL
)
17356 /* Computed goto's must be tsubst'd into. On the other hand,
17357 non-computed gotos must not be; the identifier in question
17358 will have no binding. */
17361 tmp
= DECL_NAME (tmp
);
17362 finish_goto_stmt (tmp
);
17367 tree string
= RECUR (ASM_STRING (t
));
17368 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
17369 complain
, in_decl
);
17370 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
17371 complain
, in_decl
);
17372 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
17373 complain
, in_decl
);
17374 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
17375 complain
, in_decl
);
17376 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
17377 clobbers
, labels
, ASM_INLINE_P (t
));
17378 tree asm_expr
= tmp
;
17379 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
17380 asm_expr
= TREE_OPERAND (asm_expr
, 0);
17381 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
17388 stmt
= begin_try_block ();
17389 RECUR (TRY_STMTS (t
));
17390 finish_cleanup_try_block (stmt
);
17391 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
17395 tree compound_stmt
= NULL_TREE
;
17397 if (FN_TRY_BLOCK_P (t
))
17398 stmt
= begin_function_try_block (&compound_stmt
);
17400 stmt
= begin_try_block ();
17402 RECUR (TRY_STMTS (t
));
17404 if (FN_TRY_BLOCK_P (t
))
17405 finish_function_try_block (stmt
);
17407 finish_try_block (stmt
);
17409 RECUR (TRY_HANDLERS (t
));
17410 if (FN_TRY_BLOCK_P (t
))
17411 finish_function_handler_sequence (stmt
, compound_stmt
);
17413 finish_handler_sequence (stmt
);
17419 tree decl
= HANDLER_PARMS (t
);
17423 decl
= tsubst (decl
, args
, complain
, in_decl
);
17424 /* Prevent instantiate_decl from trying to instantiate
17425 this variable. We've already done all that needs to be
17427 if (decl
!= error_mark_node
)
17428 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
17430 stmt
= begin_handler ();
17431 finish_handler_parms (decl
, stmt
);
17432 RECUR (HANDLER_BODY (t
));
17433 finish_handler (stmt
);
17438 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
17439 if (CLASS_TYPE_P (tmp
))
17441 /* Local classes are not independent templates; they are
17442 instantiated along with their containing function. And this
17443 way we don't have to deal with pushing out of one local class
17444 to instantiate a member of another local class. */
17445 /* Closures are handled by the LAMBDA_EXPR. */
17446 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
17447 complete_type (tmp
);
17448 for (tree fld
= TYPE_FIELDS (tmp
); fld
; fld
= DECL_CHAIN (fld
))
17450 || (TREE_CODE (fld
) == FUNCTION_DECL
17451 && !DECL_ARTIFICIAL (fld
)))
17452 && DECL_TEMPLATE_INSTANTIATION (fld
))
17453 instantiate_decl (fld
, /*defer_ok=*/false,
17454 /*expl_inst_class=*/false);
17458 case STATIC_ASSERT
:
17462 ++c_inhibit_evaluation_warnings
;
17464 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
17467 /*integral_constant_expression_p=*/true);
17468 --c_inhibit_evaluation_warnings
;
17470 finish_static_assert (condition
,
17471 STATIC_ASSERT_MESSAGE (t
),
17472 STATIC_ASSERT_SOURCE_LOCATION (t
),
17473 /*member_p=*/false);
17478 case OACC_PARALLEL
:
17479 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
17481 stmt
= begin_omp_parallel ();
17482 RECUR (OMP_BODY (t
));
17483 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
17487 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
17488 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
17489 complain
, in_decl
);
17490 if (OMP_PARALLEL_COMBINED (t
))
17491 omp_parallel_combined_clauses
= &tmp
;
17492 stmt
= begin_omp_parallel ();
17493 RECUR (OMP_PARALLEL_BODY (t
));
17494 gcc_assert (omp_parallel_combined_clauses
== NULL
);
17495 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
17496 = OMP_PARALLEL_COMBINED (t
);
17497 pop_omp_privatization_clauses (r
);
17501 if (OMP_TASK_BODY (t
) == NULL_TREE
)
17503 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17504 complain
, in_decl
);
17506 OMP_TASK_CLAUSES (t
) = tmp
;
17510 r
= push_omp_privatization_clauses (false);
17511 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
17512 complain
, in_decl
);
17513 stmt
= begin_omp_task ();
17514 RECUR (OMP_TASK_BODY (t
));
17515 finish_omp_task (tmp
, stmt
);
17516 pop_omp_privatization_clauses (r
);
17521 case OMP_DISTRIBUTE
:
17525 tree clauses
, body
, pre_body
;
17526 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
17527 tree orig_declv
= NULL_TREE
;
17528 tree incrv
= NULL_TREE
;
17529 enum c_omp_region_type ort
= C_ORT_OMP
;
17530 bool any_range_for
= false;
17533 if (TREE_CODE (t
) == OACC_LOOP
)
17536 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
17537 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
17539 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17541 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17542 if (OMP_FOR_ORIG_DECLS (t
))
17543 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17544 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17545 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17546 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
17549 keep_next_level (true);
17550 stmt
= begin_omp_structured_block ();
17552 pre_body
= push_stmt_list ();
17553 RECUR (OMP_FOR_PRE_BODY (t
));
17554 pre_body
= pop_stmt_list (pre_body
);
17556 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17557 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17559 |= tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
,
17560 condv
, incrv
, &clauses
, args
,
17562 integral_constant_expression_p
);
17563 omp_parallel_combined_clauses
= NULL
;
17567 gcc_assert (orig_declv
);
17568 body
= begin_omp_structured_block ();
17569 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
17570 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
)
17571 && TREE_CODE (TREE_VEC_ELT (orig_declv
, i
)) == TREE_LIST
17572 && TREE_CHAIN (TREE_VEC_ELT (orig_declv
, i
)))
17573 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
17574 TREE_VEC_ELT (declv
, i
));
17577 body
= push_stmt_list ();
17578 RECUR (OMP_FOR_BODY (t
));
17580 body
= finish_omp_structured_block (body
);
17582 body
= pop_stmt_list (body
);
17584 if (OMP_FOR_INIT (t
) != NULL_TREE
)
17585 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
17586 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
17590 t
= make_node (TREE_CODE (t
));
17591 TREE_TYPE (t
) = void_type_node
;
17592 OMP_FOR_BODY (t
) = body
;
17593 OMP_FOR_PRE_BODY (t
) = pre_body
;
17594 OMP_FOR_CLAUSES (t
) = clauses
;
17595 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
17599 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt
),
17601 pop_omp_privatization_clauses (r
);
17606 omp_parallel_combined_clauses
= NULL
;
17611 case OMP_TASKGROUP
:
17612 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
17613 && OMP_TEAMS_COMBINED (t
));
17614 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
17616 if (TREE_CODE (t
) == OMP_TEAMS
)
17618 keep_next_level (true);
17619 stmt
= begin_omp_structured_block ();
17620 RECUR (OMP_BODY (t
));
17621 stmt
= finish_omp_structured_block (stmt
);
17625 stmt
= push_stmt_list ();
17626 RECUR (OMP_BODY (t
));
17627 stmt
= pop_stmt_list (stmt
);
17631 OMP_BODY (t
) = stmt
;
17632 OMP_CLAUSES (t
) = tmp
;
17634 pop_omp_privatization_clauses (r
);
17638 r
= RECUR (OMP_DEPOBJ_DEPOBJ (t
));
17639 if (OMP_DEPOBJ_CLAUSES (t
) && OMP_DEPOBJ_CLAUSES (t
) != error_mark_node
)
17641 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
17642 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t
)) == OMP_CLAUSE
)
17644 tmp
= tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t
), C_ORT_OMP
,
17645 args
, complain
, in_decl
);
17646 if (tmp
== NULL_TREE
)
17647 tmp
= error_mark_node
;
17651 kind
= (enum omp_clause_depend_kind
)
17652 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t
));
17655 finish_omp_depobj (EXPR_LOCATION (t
), r
, kind
, tmp
);
17658 finish_omp_depobj (EXPR_LOCATION (t
), r
,
17659 OMP_CLAUSE_DEPEND_SOURCE
,
17660 OMP_DEPOBJ_CLAUSES (t
));
17664 case OMP_TARGET_DATA
:
17666 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
17667 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
17669 keep_next_level (true);
17670 stmt
= begin_omp_structured_block ();
17672 RECUR (OMP_BODY (t
));
17673 stmt
= finish_omp_structured_block (stmt
);
17676 OMP_BODY (t
) = stmt
;
17677 OMP_CLAUSES (t
) = tmp
;
17678 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
17680 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
17683 /* For combined target teams, ensure the num_teams and
17684 thread_limit clause expressions are evaluated on the host,
17685 before entering the target construct. */
17687 for (c
= OMP_TEAMS_CLAUSES (teams
);
17688 c
; c
= OMP_CLAUSE_CHAIN (c
))
17689 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
17690 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
17691 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
17693 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
17694 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
17695 if (expr
== error_mark_node
)
17697 tmp
= TARGET_EXPR_SLOT (expr
);
17699 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
17700 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
17701 OMP_CLAUSE_FIRSTPRIVATE
);
17702 OMP_CLAUSE_DECL (tc
) = tmp
;
17703 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
17704 OMP_TARGET_CLAUSES (t
) = tc
;
17713 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
17714 complain
, in_decl
);
17715 OACC_DECLARE_CLAUSES (t
) = tmp
;
17719 case OMP_TARGET_UPDATE
:
17720 case OMP_TARGET_ENTER_DATA
:
17721 case OMP_TARGET_EXIT_DATA
:
17722 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
17723 complain
, in_decl
);
17725 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17729 case OACC_ENTER_DATA
:
17730 case OACC_EXIT_DATA
:
17732 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
17733 complain
, in_decl
);
17735 OMP_STANDALONE_CLAUSES (t
) = tmp
;
17740 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
17741 complain
, in_decl
);
17742 stmt
= push_stmt_list ();
17743 RECUR (OMP_BODY (t
));
17744 stmt
= pop_stmt_list (stmt
);
17747 OMP_BODY (t
) = stmt
;
17748 OMP_ORDERED_CLAUSES (t
) = tmp
;
17754 stmt
= push_stmt_list ();
17755 RECUR (OMP_BODY (t
));
17756 stmt
= pop_stmt_list (stmt
);
17759 OMP_BODY (t
) = stmt
;
17764 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
17766 if (TREE_CODE (TREE_OPERAND (t
, 0)) == OMP_CLAUSE
)
17767 tmp
= tsubst_omp_clauses (TREE_OPERAND (t
, 0), C_ORT_OMP
, args
,
17768 complain
, in_decl
);
17769 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
17771 tree op1
= TREE_OPERAND (t
, 1);
17772 tree rhs1
= NULL_TREE
;
17774 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
17776 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
17777 op1
= TREE_OPERAND (op1
, 1);
17779 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17780 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17781 finish_omp_atomic (EXPR_LOCATION (t
), OMP_ATOMIC
, TREE_CODE (op1
),
17782 lhs
, rhs
, NULL_TREE
, NULL_TREE
, rhs1
, tmp
,
17783 OMP_ATOMIC_MEMORY_ORDER (t
));
17787 tree op1
= TREE_OPERAND (t
, 1);
17788 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
17789 tree rhs1
= NULL_TREE
;
17790 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
17791 enum tree_code opcode
= NOP_EXPR
;
17792 if (code
== OMP_ATOMIC_READ
)
17794 v
= RECUR (TREE_OPERAND (op1
, 0));
17795 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17797 else if (code
== OMP_ATOMIC_CAPTURE_OLD
17798 || code
== OMP_ATOMIC_CAPTURE_NEW
)
17800 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
17801 v
= RECUR (TREE_OPERAND (op1
, 0));
17802 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
17803 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
17805 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
17806 op11
= TREE_OPERAND (op11
, 1);
17808 lhs
= RECUR (TREE_OPERAND (op11
, 0));
17809 rhs
= RECUR (TREE_OPERAND (op11
, 1));
17810 opcode
= TREE_CODE (op11
);
17811 if (opcode
== MODIFY_EXPR
)
17817 lhs
= RECUR (TREE_OPERAND (op1
, 0));
17818 rhs
= RECUR (TREE_OPERAND (op1
, 1));
17820 finish_omp_atomic (EXPR_LOCATION (t
), code
, opcode
, lhs
, rhs
, v
,
17821 lhs1
, rhs1
, tmp
, OMP_ATOMIC_MEMORY_ORDER (t
));
17825 case TRANSACTION_EXPR
:
17828 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
17829 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
17831 if (TRANSACTION_EXPR_IS_STMT (t
))
17833 tree body
= TRANSACTION_EXPR_BODY (t
);
17834 tree noex
= NULL_TREE
;
17835 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
17837 noex
= MUST_NOT_THROW_COND (body
);
17838 if (noex
== NULL_TREE
)
17839 noex
= boolean_true_node
;
17840 body
= TREE_OPERAND (body
, 0);
17842 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
17844 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
17848 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
17849 RECUR (TRANSACTION_EXPR_BODY (t
)),
17856 case MUST_NOT_THROW_EXPR
:
17858 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17859 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
17860 RETURN (build_must_not_throw_expr (op0
, cond
));
17863 case EXPR_PACK_EXPANSION
:
17864 error ("invalid use of pack expansion expression");
17865 RETURN (error_mark_node
);
17867 case NONTYPE_ARGUMENT_PACK
:
17868 error ("use %<...%> to expand argument pack");
17869 RETURN (error_mark_node
);
17871 case COMPOUND_EXPR
:
17872 tmp
= RECUR (TREE_OPERAND (t
, 0));
17873 if (tmp
== NULL_TREE
)
17874 /* If the first operand was a statement, we're done with it. */
17875 RETURN (RECUR (TREE_OPERAND (t
, 1)));
17876 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
17877 RECUR (TREE_OPERAND (t
, 1)),
17880 case ANNOTATE_EXPR
:
17881 tmp
= RECUR (TREE_OPERAND (t
, 0));
17882 RETURN (build3_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
17883 TREE_TYPE (tmp
), tmp
,
17884 RECUR (TREE_OPERAND (t
, 1)),
17885 RECUR (TREE_OPERAND (t
, 2))));
17888 RETURN (add_stmt (copy_node (t
)));
17891 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
17893 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17894 /*function_p=*/false,
17895 integral_constant_expression_p
));
17898 RETURN (NULL_TREE
);
17900 input_location
= loc
;
17906 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17907 function. For description of the body see comment above
17908 cp_parser_omp_declare_reduction_exprs. */
17911 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
17913 if (t
== NULL_TREE
|| t
== error_mark_node
)
17916 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
17918 tree_stmt_iterator tsi
;
17921 memset (stmts
, 0, sizeof stmts
);
17922 for (i
= 0, tsi
= tsi_start (t
);
17923 i
< 7 && !tsi_end_p (tsi
);
17924 i
++, tsi_next (&tsi
))
17925 stmts
[i
] = tsi_stmt (tsi
);
17926 gcc_assert (tsi_end_p (tsi
));
17930 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
17931 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
17932 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
17933 args
, complain
, in_decl
);
17934 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
17935 args
, complain
, in_decl
);
17936 DECL_CONTEXT (omp_out
) = current_function_decl
;
17937 DECL_CONTEXT (omp_in
) = current_function_decl
;
17938 keep_next_level (true);
17939 tree block
= begin_omp_structured_block ();
17940 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
17941 block
= finish_omp_structured_block (block
);
17942 block
= maybe_cleanup_point_expr_void (block
);
17943 add_decl_expr (omp_out
);
17944 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
17945 TREE_NO_WARNING (omp_out
) = 1;
17946 add_decl_expr (omp_in
);
17947 finish_expr_stmt (block
);
17951 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
17952 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
17953 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
17954 args
, complain
, in_decl
);
17955 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
17956 args
, complain
, in_decl
);
17957 DECL_CONTEXT (omp_priv
) = current_function_decl
;
17958 DECL_CONTEXT (omp_orig
) = current_function_decl
;
17959 keep_next_level (true);
17960 tree block
= begin_omp_structured_block ();
17961 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
17962 block
= finish_omp_structured_block (block
);
17963 block
= maybe_cleanup_point_expr_void (block
);
17964 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
17965 add_decl_expr (omp_priv
);
17966 add_decl_expr (omp_orig
);
17967 finish_expr_stmt (block
);
17969 add_decl_expr (omp_orig
);
17973 /* T is a postfix-expression that is not being used in a function
17974 call. Return the substituted version of T. */
17977 tsubst_non_call_postfix_expression (tree t
, tree args
,
17978 tsubst_flags_t complain
,
17981 if (TREE_CODE (t
) == SCOPE_REF
)
17982 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
17983 /*done=*/false, /*address_p=*/false);
17985 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
17986 /*function_p=*/false,
17987 /*integral_constant_expression_p=*/false);
17992 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17993 instantiation context. Instantiating a pack expansion containing a lambda
17994 might result in multiple lambdas all based on the same lambda in the
17998 tsubst_lambda_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
18000 tree oldfn
= lambda_function (t
);
18003 /* If we have already specialized this lambda expr, reuse it. See
18005 if (local_specializations
)
18006 if (tree r
= retrieve_local_specialization (t
))
18009 tree r
= build_lambda_expr ();
18011 if (local_specializations
)
18012 register_local_specialization (r
, t
);
18014 LAMBDA_EXPR_LOCATION (r
)
18015 = LAMBDA_EXPR_LOCATION (t
);
18016 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
18017 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
18018 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
18019 LAMBDA_EXPR_INSTANTIATED (r
) = true;
18021 if (LAMBDA_EXPR_EXTRA_SCOPE (t
) == NULL_TREE
)
18022 /* A lambda in a default argument outside a class gets no
18023 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18024 tsubst_default_argument calls start_lambda_scope, so we need to
18025 specifically ignore it here, and use the global scope. */
18026 record_null_lambda_scope (r
);
18028 record_lambda_scope (r
);
18030 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
18031 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
18033 vec
<tree
,va_gc
>* field_packs
= NULL
;
18035 for (tree cap
= LAMBDA_EXPR_CAPTURE_LIST (t
); cap
;
18036 cap
= TREE_CHAIN (cap
))
18038 tree ofield
= TREE_PURPOSE (cap
);
18039 if (PACK_EXPANSION_P (ofield
))
18040 ofield
= PACK_EXPANSION_PATTERN (ofield
);
18041 tree field
= tsubst_decl (ofield
, args
, complain
);
18043 if (DECL_PACK_P (ofield
) && !DECL_NORMAL_CAPTURE_P (ofield
))
18045 /* Remember these for when we've pushed local_specializations. */
18046 vec_safe_push (field_packs
, ofield
);
18047 vec_safe_push (field_packs
, field
);
18050 if (field
== error_mark_node
)
18051 return error_mark_node
;
18053 tree init
= TREE_VALUE (cap
);
18054 if (PACK_EXPANSION_P (init
))
18055 init
= tsubst_pack_expansion (init
, args
, complain
, in_decl
);
18057 init
= tsubst_copy_and_build (init
, args
, complain
, in_decl
,
18058 /*fn*/false, /*constexpr*/false);
18060 if (TREE_CODE (field
) == TREE_VEC
)
18062 int len
= TREE_VEC_LENGTH (field
);
18063 gcc_assert (TREE_CODE (init
) == TREE_VEC
18064 && TREE_VEC_LENGTH (init
) == len
);
18065 for (int i
= 0; i
< len
; ++i
)
18066 LAMBDA_EXPR_CAPTURE_LIST (r
)
18067 = tree_cons (TREE_VEC_ELT (field
, i
),
18068 TREE_VEC_ELT (init
, i
),
18069 LAMBDA_EXPR_CAPTURE_LIST (r
));
18073 LAMBDA_EXPR_CAPTURE_LIST (r
)
18074 = tree_cons (field
, init
, LAMBDA_EXPR_CAPTURE_LIST (r
));
18076 if (id_equal (DECL_NAME (field
), "__this"))
18077 LAMBDA_EXPR_THIS_CAPTURE (r
) = field
;
18081 tree type
= begin_lambda_type (r
);
18082 if (type
== error_mark_node
)
18083 return error_mark_node
;
18085 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18086 determine_visibility (TYPE_NAME (type
));
18088 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r
));
18090 tree oldtmpl
= (generic_lambda_fn_p (oldfn
)
18091 ? DECL_TI_TEMPLATE (oldfn
)
18094 tree fntype
= static_fn_type (oldfn
);
18096 ++processing_template_decl
;
18097 fntype
= tsubst (fntype
, args
, complain
, in_decl
);
18099 --processing_template_decl
;
18101 if (fntype
== error_mark_node
)
18102 r
= error_mark_node
;
18105 /* The body of a lambda-expression is not a subexpression of the
18106 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18107 which would be skipped if cp_unevaluated_operand. */
18110 /* Fix the type of 'this'. */
18111 fntype
= build_memfn_type (fntype
, type
,
18112 type_memfn_quals (fntype
),
18113 type_memfn_rqual (fntype
));
18117 tmpl
= tsubst_template_decl (oldtmpl
, args
, complain
, fntype
);
18118 fn
= DECL_TEMPLATE_RESULT (tmpl
);
18119 finish_member_declaration (tmpl
);
18124 fn
= tsubst_function_decl (oldfn
, args
, complain
, fntype
);
18125 finish_member_declaration (fn
);
18128 /* Let finish_function set this. */
18129 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
18131 bool nested
= cfun
;
18133 push_function_context ();
18135 /* Still increment function_depth so that we don't GC in the
18136 middle of an expression. */
18139 local_specialization_stack
s (lss_copy
);
18141 tree body
= start_lambda_function (fn
, r
);
18143 /* Now record them for lookup_init_capture_pack. */
18144 int fplen
= vec_safe_length (field_packs
);
18145 for (int i
= 0; i
< fplen
; )
18147 tree pack
= (*field_packs
)[i
++];
18148 tree inst
= (*field_packs
)[i
++];
18149 register_local_specialization (inst
, pack
);
18151 release_tree_vector (field_packs
);
18153 register_parameter_specializations (oldfn
, fn
);
18157 /* We might not partially instantiate some parts of the function, so
18158 copy these flags from the original template. */
18159 language_function
*ol
= DECL_STRUCT_FUNCTION (oldfn
)->language
;
18160 current_function_returns_value
= ol
->returns_value
;
18161 current_function_returns_null
= ol
->returns_null
;
18162 current_function_returns_abnormally
= ol
->returns_abnormally
;
18163 current_function_infinite_loop
= ol
->infinite_loop
;
18166 /* [temp.deduct] A lambda-expression appearing in a function type or a
18167 template parameter is not considered part of the immediate context for
18168 the purposes of template argument deduction. */
18169 complain
= tf_warning_or_error
;
18171 tsubst_expr (DECL_SAVED_TREE (oldfn
), args
, complain
, r
,
18172 /*constexpr*/false);
18174 finish_lambda_function (body
);
18177 pop_function_context ();
18181 /* The capture list was built up in reverse order; fix that now. */
18182 LAMBDA_EXPR_CAPTURE_LIST (r
)
18183 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r
));
18185 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
18187 maybe_add_lambda_conv_op (type
);
18190 finish_struct (type
, /*attr*/NULL_TREE
);
18192 insert_pending_capture_proxies ();
18197 /* Like tsubst but deals with expressions and performs semantic
18198 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18201 tsubst_copy_and_build (tree t
,
18203 tsubst_flags_t complain
,
18206 bool integral_constant_expression_p
)
18208 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18209 #define RECUR(NODE) \
18210 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18211 /*function_p=*/false, \
18212 integral_constant_expression_p)
18217 if (t
== NULL_TREE
|| t
== error_mark_node
)
18220 loc
= input_location
;
18221 if (location_t eloc
= cp_expr_location (t
))
18222 input_location
= eloc
;
18224 /* N3276 decltype magic only applies to calls at the top level or on the
18225 right side of a comma. */
18226 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
18227 complain
&= ~tf_decltype
;
18229 switch (TREE_CODE (t
))
18233 /* Fall through. */
18234 case IDENTIFIER_NODE
:
18238 bool non_integral_constant_expression_p
;
18239 const char *error_msg
;
18241 if (IDENTIFIER_CONV_OP_P (t
))
18243 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18244 t
= make_conv_op_name (new_type
);
18247 /* Look up the name. */
18248 decl
= lookup_name (t
);
18250 /* By convention, expressions use ERROR_MARK_NODE to indicate
18251 failure, not NULL_TREE. */
18252 if (decl
== NULL_TREE
)
18253 decl
= error_mark_node
;
18255 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
18257 integral_constant_expression_p
,
18258 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
18259 &non_integral_constant_expression_p
,
18260 /*template_p=*/false,
18262 /*address_p=*/false,
18263 /*template_arg_p=*/false,
18268 if (!function_p
&& identifier_p (decl
))
18270 if (complain
& tf_error
)
18271 unqualified_name_lookup_error (decl
);
18272 decl
= error_mark_node
;
18277 case TEMPLATE_ID_EXPR
:
18280 tree templ
= RECUR (TREE_OPERAND (t
, 0));
18281 tree targs
= TREE_OPERAND (t
, 1);
18284 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
18285 if (targs
== error_mark_node
)
18286 RETURN (error_mark_node
);
18288 if (TREE_CODE (templ
) == SCOPE_REF
)
18290 tree name
= TREE_OPERAND (templ
, 1);
18291 tree tid
= lookup_template_function (name
, targs
);
18292 TREE_OPERAND (templ
, 1) = tid
;
18296 if (variable_template_p (templ
))
18297 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
18299 if (TREE_CODE (templ
) == COMPONENT_REF
)
18301 object
= TREE_OPERAND (templ
, 0);
18302 templ
= TREE_OPERAND (templ
, 1);
18305 object
= NULL_TREE
;
18306 templ
= lookup_template_function (templ
, targs
);
18309 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
18310 object
, templ
, NULL_TREE
));
18312 RETURN (baselink_for_fns (templ
));
18317 tree r
= RECUR (TREE_OPERAND (t
, 0));
18319 if (REFERENCE_REF_P (t
))
18321 /* A type conversion to reference type will be enclosed in
18322 such an indirect ref, but the substitution of the cast
18323 will have also added such an indirect ref. */
18324 r
= convert_from_reference (r
);
18327 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
18328 complain
|decltype_flag
);
18330 if (REF_PARENTHESIZED_P (t
))
18331 r
= force_paren_expr (r
);
18338 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18339 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18340 RETURN (build_nop (type
, op0
));
18343 case IMPLICIT_CONV_EXPR
:
18345 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18346 tree expr
= RECUR (TREE_OPERAND (t
, 0));
18347 if (dependent_type_p (type
) || type_dependent_expression_p (expr
))
18349 retval
= copy_node (t
);
18350 TREE_TYPE (retval
) = type
;
18351 TREE_OPERAND (retval
, 0) = expr
;
18354 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t
))
18355 /* We'll pass this to convert_nontype_argument again, we don't need
18356 to actually perform any conversion here. */
18358 int flags
= LOOKUP_IMPLICIT
;
18359 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
18360 flags
= LOOKUP_NORMAL
;
18361 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t
))
18362 flags
|= LOOKUP_NO_NARROWING
;
18363 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
18369 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18370 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18371 if (op0
== error_mark_node
)
18372 RETURN (error_mark_node
);
18373 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
18377 case REINTERPRET_CAST_EXPR
:
18378 case CONST_CAST_EXPR
:
18379 case DYNAMIC_CAST_EXPR
:
18380 case STATIC_CAST_EXPR
:
18383 tree op
, r
= NULL_TREE
;
18385 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
18386 if (integral_constant_expression_p
18387 && !cast_valid_in_integral_constant_expression_p (type
))
18389 if (complain
& tf_error
)
18390 error ("a cast to a type other than an integral or "
18391 "enumeration type cannot appear in a constant-expression");
18392 RETURN (error_mark_node
);
18395 op
= RECUR (TREE_OPERAND (t
, 0));
18397 warning_sentinel
s(warn_useless_cast
);
18398 warning_sentinel
s2(warn_ignored_qualifiers
);
18399 switch (TREE_CODE (t
))
18402 r
= build_functional_cast (type
, op
, complain
);
18404 case REINTERPRET_CAST_EXPR
:
18405 r
= build_reinterpret_cast (type
, op
, complain
);
18407 case CONST_CAST_EXPR
:
18408 r
= build_const_cast (type
, op
, complain
);
18410 case DYNAMIC_CAST_EXPR
:
18411 r
= build_dynamic_cast (type
, op
, complain
);
18413 case STATIC_CAST_EXPR
:
18414 r
= build_static_cast (type
, op
, complain
);
18417 gcc_unreachable ();
18423 case POSTDECREMENT_EXPR
:
18424 case POSTINCREMENT_EXPR
:
18425 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18426 args
, complain
, in_decl
);
18427 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
18428 complain
|decltype_flag
));
18430 case PREDECREMENT_EXPR
:
18431 case PREINCREMENT_EXPR
:
18435 case TRUTH_NOT_EXPR
:
18436 case UNARY_PLUS_EXPR
: /* Unary + */
18437 case REALPART_EXPR
:
18438 case IMAGPART_EXPR
:
18439 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
18440 RECUR (TREE_OPERAND (t
, 0)),
18441 complain
|decltype_flag
));
18443 case FIX_TRUNC_EXPR
:
18444 gcc_unreachable ();
18447 op1
= TREE_OPERAND (t
, 0);
18448 if (TREE_CODE (op1
) == LABEL_DECL
)
18449 RETURN (finish_label_address_expr (DECL_NAME (op1
),
18450 EXPR_LOCATION (op1
)));
18451 if (TREE_CODE (op1
) == SCOPE_REF
)
18452 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
18453 /*done=*/true, /*address_p=*/true);
18455 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
18457 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
18458 complain
|decltype_flag
));
18463 case TRUNC_DIV_EXPR
:
18464 case CEIL_DIV_EXPR
:
18465 case FLOOR_DIV_EXPR
:
18466 case ROUND_DIV_EXPR
:
18467 case EXACT_DIV_EXPR
:
18471 case TRUNC_MOD_EXPR
:
18472 case FLOOR_MOD_EXPR
:
18473 case TRUTH_ANDIF_EXPR
:
18474 case TRUTH_ORIF_EXPR
:
18475 case TRUTH_AND_EXPR
:
18476 case TRUTH_OR_EXPR
:
18492 warning_sentinel
s1(warn_type_limits
);
18493 warning_sentinel
s2(warn_div_by_zero
);
18494 warning_sentinel
s3(warn_logical_op
);
18495 warning_sentinel
s4(warn_tautological_compare
);
18496 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18497 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18498 tree r
= build_x_binary_op
18499 (input_location
, TREE_CODE (t
),
18501 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
18503 : TREE_CODE (TREE_OPERAND (t
, 0))),
18505 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
18507 : TREE_CODE (TREE_OPERAND (t
, 1))),
18509 complain
|decltype_flag
);
18510 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
18511 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18516 case POINTER_PLUS_EXPR
:
18518 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18519 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18520 RETURN (fold_build_pointer_plus (op0
, op1
));
18524 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
18525 /*address_p=*/false));
18527 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18528 args
, complain
, in_decl
);
18529 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
18530 RECUR (TREE_OPERAND (t
, 1)),
18531 complain
|decltype_flag
));
18534 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
18535 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
18536 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18543 op1
= TREE_OPERAND (t
, 0);
18544 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
18545 op1
= TREE_TYPE (op1
);
18546 bool std_alignof
= (TREE_CODE (t
) == ALIGNOF_EXPR
18547 && ALIGNOF_EXPR_STD_P (t
));
18550 /* When there are no ARGS, we are trying to evaluate a
18551 non-dependent expression from the parser. Trying to do
18552 the substitutions may not work. */
18554 op1
= TREE_TYPE (op1
);
18558 ++cp_unevaluated_operand
;
18559 ++c_inhibit_evaluation_warnings
;
18561 op1
= tsubst (op1
, args
, complain
, in_decl
);
18563 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18564 /*function_p=*/false,
18565 /*integral_constant_expression_p=*/
18567 --cp_unevaluated_operand
;
18568 --c_inhibit_evaluation_warnings
;
18571 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), std_alignof
,
18572 complain
& tf_error
);
18574 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
18575 complain
& tf_error
);
18576 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
18578 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
18580 if (!processing_template_decl
&& TYPE_P (op1
))
18582 r
= build_min (SIZEOF_EXPR
, size_type_node
,
18583 build1 (NOP_EXPR
, op1
, error_mark_node
));
18584 SIZEOF_EXPR_TYPE_P (r
) = 1;
18587 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
18588 TREE_SIDE_EFFECTS (r
) = 0;
18589 TREE_READONLY (r
) = 1;
18591 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
18596 case AT_ENCODE_EXPR
:
18598 op1
= TREE_OPERAND (t
, 0);
18599 ++cp_unevaluated_operand
;
18600 ++c_inhibit_evaluation_warnings
;
18601 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18602 /*function_p=*/false,
18603 /*integral_constant_expression_p=*/false);
18604 --cp_unevaluated_operand
;
18605 --c_inhibit_evaluation_warnings
;
18606 RETURN (objc_build_encode_expr (op1
));
18609 case NOEXCEPT_EXPR
:
18610 op1
= TREE_OPERAND (t
, 0);
18611 ++cp_unevaluated_operand
;
18612 ++c_inhibit_evaluation_warnings
;
18613 ++cp_noexcept_operand
;
18614 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
18615 /*function_p=*/false,
18616 /*integral_constant_expression_p=*/false);
18617 --cp_unevaluated_operand
;
18618 --c_inhibit_evaluation_warnings
;
18619 --cp_noexcept_operand
;
18620 RETURN (finish_noexcept_expr (op1
, complain
));
18624 warning_sentinel
s(warn_div_by_zero
);
18625 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
18626 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
18627 tree r
= build_x_modify_expr
18628 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
18629 complain
|decltype_flag
);
18630 /* TREE_NO_WARNING must be set if either the expression was
18631 parenthesized or it uses an operator such as >>= rather
18632 than plain assignment. In the former case, it was already
18633 set and must be copied. In the latter case,
18634 build_x_modify_expr sets it and it must not be reset
18636 if (TREE_NO_WARNING (t
))
18637 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
18643 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
18644 args
, complain
, in_decl
);
18645 /* Remember that there was a reference to this entity. */
18647 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
18648 RETURN (error_mark_node
);
18649 RETURN (build_x_arrow (input_location
, op1
, complain
));
18653 tree placement
= RECUR (TREE_OPERAND (t
, 0));
18654 tree init
= RECUR (TREE_OPERAND (t
, 3));
18655 vec
<tree
, va_gc
> *placement_vec
;
18656 vec
<tree
, va_gc
> *init_vec
;
18659 if (placement
== NULL_TREE
)
18660 placement_vec
= NULL
;
18663 placement_vec
= make_tree_vector ();
18664 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
18665 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
18668 /* If there was an initializer in the original tree, but it
18669 instantiated to an empty list, then we should pass a
18670 non-NULL empty vector to tell build_new that it was an
18671 empty initializer() rather than no initializer. This can
18672 only happen when the initializer is a pack expansion whose
18673 parameter packs are of length zero. */
18674 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
18678 init_vec
= make_tree_vector ();
18679 if (init
== void_node
)
18680 gcc_assert (init_vec
!= NULL
);
18683 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
18684 vec_safe_push (init_vec
, TREE_VALUE (init
));
18688 /* Avoid passing an enclosing decl to valid_array_size_p. */
18689 in_decl
= NULL_TREE
;
18691 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
18692 tree op2
= RECUR (TREE_OPERAND (t
, 2));
18693 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
18694 NEW_EXPR_USE_GLOBAL (t
),
18697 if (placement_vec
!= NULL
)
18698 release_tree_vector (placement_vec
);
18699 if (init_vec
!= NULL
)
18700 release_tree_vector (init_vec
);
18707 tree op0
= RECUR (TREE_OPERAND (t
, 0));
18708 tree op1
= RECUR (TREE_OPERAND (t
, 1));
18709 RETURN (delete_sanity (op0
, op1
,
18710 DELETE_EXPR_USE_VEC (t
),
18711 DELETE_EXPR_USE_GLOBAL (t
),
18715 case COMPOUND_EXPR
:
18717 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
18718 complain
& ~tf_decltype
, in_decl
,
18719 /*function_p=*/false,
18720 integral_constant_expression_p
);
18721 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
18723 RECUR (TREE_OPERAND (t
, 1)),
18724 complain
|decltype_flag
));
18730 vec
<tree
, va_gc
> *call_args
;
18731 unsigned int nargs
, i
;
18736 function
= CALL_EXPR_FN (t
);
18737 /* Internal function with no arguments. */
18738 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
18741 /* When we parsed the expression, we determined whether or
18742 not Koenig lookup should be performed. */
18743 koenig_p
= KOENIG_LOOKUP_P (t
);
18744 if (function
== NULL_TREE
)
18747 qualified_p
= false;
18749 else if (TREE_CODE (function
) == SCOPE_REF
)
18751 qualified_p
= true;
18752 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
18754 /*address_p=*/false);
18756 else if (koenig_p
&& identifier_p (function
))
18758 /* Do nothing; calling tsubst_copy_and_build on an identifier
18759 would incorrectly perform unqualified lookup again.
18761 Note that we can also have an IDENTIFIER_NODE if the earlier
18762 unqualified lookup found a member function; in that case
18763 koenig_p will be false and we do want to do the lookup
18764 again to find the instantiated member function.
18766 FIXME but doing that causes c++/15272, so we need to stop
18767 using IDENTIFIER_NODE in that situation. */
18768 qualified_p
= false;
18772 if (TREE_CODE (function
) == COMPONENT_REF
)
18774 tree op
= TREE_OPERAND (function
, 1);
18776 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
18777 || (BASELINK_P (op
)
18778 && BASELINK_QUALIFIED_P (op
)));
18781 qualified_p
= false;
18783 if (TREE_CODE (function
) == ADDR_EXPR
18784 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
18785 /* Avoid error about taking the address of a constructor. */
18786 function
= TREE_OPERAND (function
, 0);
18788 function
= tsubst_copy_and_build (function
, args
, complain
,
18791 integral_constant_expression_p
);
18793 if (BASELINK_P (function
))
18794 qualified_p
= true;
18797 nargs
= call_expr_nargs (t
);
18798 call_args
= make_tree_vector ();
18799 for (i
= 0; i
< nargs
; ++i
)
18801 tree arg
= CALL_EXPR_ARG (t
, i
);
18803 if (!PACK_EXPANSION_P (arg
))
18804 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
18807 /* Expand the pack expansion and push each entry onto
18809 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
18810 if (TREE_CODE (arg
) == TREE_VEC
)
18812 unsigned int len
, j
;
18814 len
= TREE_VEC_LENGTH (arg
);
18815 for (j
= 0; j
< len
; ++j
)
18817 tree value
= TREE_VEC_ELT (arg
, j
);
18818 if (value
!= NULL_TREE
)
18819 value
= convert_from_reference (value
);
18820 vec_safe_push (call_args
, value
);
18825 /* A partial substitution. Add one entry. */
18826 vec_safe_push (call_args
, arg
);
18831 /* Stripped-down processing for a call in a thunk. Specifically, in
18832 the thunk template for a generic lambda. */
18833 if (CALL_FROM_THUNK_P (t
))
18835 tree thisarg
= NULL_TREE
;
18836 if (TREE_CODE (function
) == COMPONENT_REF
)
18838 thisarg
= TREE_OPERAND (function
, 0);
18839 if (TREE_CODE (thisarg
) == INDIRECT_REF
)
18840 thisarg
= TREE_OPERAND (thisarg
, 0);
18841 function
= TREE_OPERAND (function
, 1);
18842 if (TREE_CODE (function
) == BASELINK
)
18843 function
= BASELINK_FUNCTIONS (function
);
18845 /* We aren't going to do normal overload resolution, so force the
18846 template-id to resolve. */
18847 function
= resolve_nondeduced_context (function
, complain
);
18848 for (unsigned i
= 0; i
< nargs
; ++i
)
18850 /* In a thunk, pass through args directly, without any
18852 tree arg
= (*call_args
)[i
];
18853 while (TREE_CODE (arg
) != PARM_DECL
)
18854 arg
= TREE_OPERAND (arg
, 0);
18855 (*call_args
)[i
] = arg
;
18859 /* Shift the other args over to make room. */
18860 vec_safe_push (call_args
, (*call_args
)[nargs
-1]);
18861 for (int i
= nargs
-1; i
> 0; --i
)
18862 (*call_args
)[i
] = (*call_args
)[i
-1];
18863 (*call_args
)[0] = thisarg
;
18865 ret
= build_call_a (function
, call_args
->length (),
18866 call_args
->address ());
18867 /* The thunk location is not interesting. */
18868 SET_EXPR_LOCATION (ret
, UNKNOWN_LOCATION
);
18869 CALL_FROM_THUNK_P (ret
) = true;
18870 if (CLASS_TYPE_P (TREE_TYPE (ret
)))
18871 CALL_EXPR_RETURN_SLOT_OPT (ret
) = true;
18873 release_tree_vector (call_args
);
18877 /* We do not perform argument-dependent lookup if normal
18878 lookup finds a non-function, in accordance with the
18879 expected resolution of DR 218. */
18881 && ((is_overloaded_fn (function
)
18882 /* If lookup found a member function, the Koenig lookup is
18883 not appropriate, even if an unqualified-name was used
18884 to denote the function. */
18885 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
18886 || identifier_p (function
))
18887 /* Only do this when substitution turns a dependent call
18888 into a non-dependent call. */
18889 && type_dependent_expression_p_push (t
)
18890 && !any_type_dependent_arguments_p (call_args
))
18891 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
18893 if (function
!= NULL_TREE
18894 && identifier_p (function
)
18895 && !any_type_dependent_arguments_p (call_args
))
18897 if (koenig_p
&& (complain
& tf_warning_or_error
))
18899 /* For backwards compatibility and good diagnostics, try
18900 the unqualified lookup again if we aren't in SFINAE
18902 tree unq
= (tsubst_copy_and_build
18903 (function
, args
, complain
, in_decl
, true,
18904 integral_constant_expression_p
));
18905 if (unq
== error_mark_node
)
18907 release_tree_vector (call_args
);
18908 RETURN (error_mark_node
);
18911 if (unq
!= function
)
18913 /* In a lambda fn, we have to be careful to not
18914 introduce new this captures. Legacy code can't
18915 be using lambdas anyway, so it's ok to be
18917 bool in_lambda
= (current_class_type
18918 && LAMBDA_TYPE_P (current_class_type
));
18919 char const *const msg
18920 = G_("%qD was not declared in this scope, "
18921 "and no declarations were found by "
18922 "argument-dependent lookup at the point "
18923 "of instantiation");
18927 error_at (cp_expr_loc_or_loc (t
, input_location
),
18930 diag
= permerror (cp_expr_loc_or_loc (t
, input_location
),
18936 if (INDIRECT_REF_P (fn
))
18937 fn
= TREE_OPERAND (fn
, 0);
18938 if (is_overloaded_fn (fn
))
18939 fn
= get_first_fn (fn
);
18942 /* Can't say anything more. */;
18943 else if (DECL_CLASS_SCOPE_P (fn
))
18945 location_t loc
= cp_expr_loc_or_loc (t
,
18948 "declarations in dependent base %qT are "
18949 "not found by unqualified lookup",
18950 DECL_CLASS_CONTEXT (fn
));
18951 if (current_class_ptr
)
18953 "use %<this->%D%> instead", function
);
18956 "use %<%T::%D%> instead",
18957 current_class_name
, function
);
18960 inform (DECL_SOURCE_LOCATION (fn
),
18961 "%qD declared here, later in the "
18962 "translation unit", fn
);
18965 release_tree_vector (call_args
);
18966 RETURN (error_mark_node
);
18973 if (identifier_p (function
))
18975 if (complain
& tf_error
)
18976 unqualified_name_lookup_error (function
);
18977 release_tree_vector (call_args
);
18978 RETURN (error_mark_node
);
18982 /* Remember that there was a reference to this entity. */
18983 if (function
!= NULL_TREE
18984 && DECL_P (function
)
18985 && !mark_used (function
, complain
) && !(complain
& tf_error
))
18987 release_tree_vector (call_args
);
18988 RETURN (error_mark_node
);
18991 /* Put back tf_decltype for the actual call. */
18992 complain
|= decltype_flag
;
18994 if (function
== NULL_TREE
)
18995 switch (CALL_EXPR_IFN (t
))
18998 gcc_assert (nargs
== 1);
18999 if (vec_safe_length (call_args
) != 1)
19001 error_at (cp_expr_loc_or_loc (t
, input_location
),
19002 "wrong number of arguments to "
19003 "%<__builtin_launder%>");
19004 ret
= error_mark_node
;
19007 ret
= finish_builtin_launder (cp_expr_loc_or_loc (t
,
19009 (*call_args
)[0], complain
);
19012 case IFN_VEC_CONVERT
:
19013 gcc_assert (nargs
== 1);
19014 if (vec_safe_length (call_args
) != 1)
19016 error_at (cp_expr_loc_or_loc (t
, input_location
),
19017 "wrong number of arguments to "
19018 "%<__builtin_convertvector%>");
19019 ret
= error_mark_node
;
19022 ret
= cp_build_vec_convert ((*call_args
)[0], input_location
,
19023 tsubst (TREE_TYPE (t
), args
,
19024 complain
, in_decl
),
19026 if (TREE_CODE (ret
) == VIEW_CONVERT_EXPR
)
19028 release_tree_vector (call_args
);
19034 /* Unsupported internal function with arguments. */
19035 gcc_unreachable ();
19037 else if (TREE_CODE (function
) == OFFSET_REF
19038 || TREE_CODE (function
) == DOTSTAR_EXPR
19039 || TREE_CODE (function
) == MEMBER_REF
)
19040 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
19042 else if (TREE_CODE (function
) == COMPONENT_REF
)
19044 tree instance
= TREE_OPERAND (function
, 0);
19045 tree fn
= TREE_OPERAND (function
, 1);
19047 if (processing_template_decl
19048 && (type_dependent_expression_p (instance
)
19049 || (!BASELINK_P (fn
)
19050 && TREE_CODE (fn
) != FIELD_DECL
)
19051 || type_dependent_expression_p (fn
)
19052 || any_type_dependent_arguments_p (call_args
)))
19053 ret
= build_min_nt_call_vec (function
, call_args
);
19054 else if (!BASELINK_P (fn
))
19055 ret
= finish_call_expr (function
, &call_args
,
19056 /*disallow_virtual=*/false,
19057 /*koenig_p=*/false,
19060 ret
= (build_new_method_call
19062 &call_args
, NULL_TREE
,
19063 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
19068 ret
= finish_call_expr (function
, &call_args
,
19069 /*disallow_virtual=*/qualified_p
,
19073 release_tree_vector (call_args
);
19075 if (ret
!= error_mark_node
)
19077 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
19078 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
19079 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
19080 if (op
|| ord
|| rev
)
19082 function
= extract_call_expr (ret
);
19083 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
19084 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
19085 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
19094 tree cond
= RECUR (TREE_OPERAND (t
, 0));
19095 cond
= mark_rvalue_use (cond
);
19096 tree folded_cond
= fold_non_dependent_expr (cond
, complain
);
19099 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
19101 if (integer_zerop (folded_cond
))
19103 ++c_inhibit_evaluation_warnings
;
19104 exp1
= RECUR (TREE_OPERAND (t
, 1));
19105 --c_inhibit_evaluation_warnings
;
19106 exp2
= RECUR (TREE_OPERAND (t
, 2));
19110 exp1
= RECUR (TREE_OPERAND (t
, 1));
19111 ++c_inhibit_evaluation_warnings
;
19112 exp2
= RECUR (TREE_OPERAND (t
, 2));
19113 --c_inhibit_evaluation_warnings
;
19115 cond
= folded_cond
;
19119 exp1
= RECUR (TREE_OPERAND (t
, 1));
19120 exp2
= RECUR (TREE_OPERAND (t
, 2));
19123 warning_sentinel
s(warn_duplicated_branches
);
19124 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
19125 cond
, exp1
, exp2
, complain
));
19128 case PSEUDO_DTOR_EXPR
:
19130 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19131 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19132 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
19133 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
19139 tree purpose
, value
, chain
;
19141 if (t
== void_list_node
)
19144 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
19145 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
19147 /* We have pack expansions, so expand those and
19148 create a new list out of it. */
19149 tree purposevec
= NULL_TREE
;
19150 tree valuevec
= NULL_TREE
;
19154 /* Expand the argument expressions. */
19155 if (TREE_PURPOSE (t
))
19156 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
19157 complain
, in_decl
);
19158 if (TREE_VALUE (t
))
19159 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
19160 complain
, in_decl
);
19162 /* Build the rest of the list. */
19163 chain
= TREE_CHAIN (t
);
19164 if (chain
&& chain
!= void_type_node
)
19165 chain
= RECUR (chain
);
19167 /* Determine the number of arguments. */
19168 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
19170 len
= TREE_VEC_LENGTH (purposevec
);
19171 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
19173 else if (TREE_CODE (valuevec
) == TREE_VEC
)
19174 len
= TREE_VEC_LENGTH (valuevec
);
19177 /* Since we only performed a partial substitution into
19178 the argument pack, we only RETURN (a single list
19180 if (purposevec
== TREE_PURPOSE (t
)
19181 && valuevec
== TREE_VALUE (t
)
19182 && chain
== TREE_CHAIN (t
))
19185 RETURN (tree_cons (purposevec
, valuevec
, chain
));
19188 /* Convert the argument vectors into a TREE_LIST */
19192 /* Grab the Ith values. */
19194 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
19197 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
19200 /* Build the list (backwards). */
19201 chain
= tree_cons (purpose
, value
, chain
);
19207 purpose
= TREE_PURPOSE (t
);
19209 purpose
= RECUR (purpose
);
19210 value
= TREE_VALUE (t
);
19212 value
= RECUR (value
);
19213 chain
= TREE_CHAIN (t
);
19214 if (chain
&& chain
!= void_type_node
)
19215 chain
= RECUR (chain
);
19216 if (purpose
== TREE_PURPOSE (t
)
19217 && value
== TREE_VALUE (t
)
19218 && chain
== TREE_CHAIN (t
))
19220 RETURN (tree_cons (purpose
, value
, chain
));
19223 case COMPONENT_REF
:
19230 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
19231 args
, complain
, in_decl
);
19232 /* Remember that there was a reference to this entity. */
19233 if (DECL_P (object
)
19234 && !mark_used (object
, complain
) && !(complain
& tf_error
))
19235 RETURN (error_mark_node
);
19236 object_type
= TREE_TYPE (object
);
19238 member
= TREE_OPERAND (t
, 1);
19239 if (BASELINK_P (member
))
19240 member
= tsubst_baselink (member
,
19241 non_reference (TREE_TYPE (object
)),
19242 args
, complain
, in_decl
);
19244 member
= tsubst_copy (member
, args
, complain
, in_decl
);
19245 if (member
== error_mark_node
)
19246 RETURN (error_mark_node
);
19248 if (TREE_CODE (member
) == FIELD_DECL
)
19250 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
19251 if (TREE_CODE (r
) == COMPONENT_REF
)
19252 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19255 else if (type_dependent_expression_p (object
))
19256 /* We can't do much here. */;
19257 else if (!CLASS_TYPE_P (object_type
))
19259 if (scalarish_type_p (object_type
))
19261 tree s
= NULL_TREE
;
19262 tree dtor
= member
;
19264 if (TREE_CODE (dtor
) == SCOPE_REF
)
19266 s
= TREE_OPERAND (dtor
, 0);
19267 dtor
= TREE_OPERAND (dtor
, 1);
19269 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
19271 dtor
= TREE_OPERAND (dtor
, 0);
19273 RETURN (finish_pseudo_destructor_expr
19274 (object
, s
, dtor
, input_location
));
19278 else if (TREE_CODE (member
) == SCOPE_REF
19279 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
19281 /* Lookup the template functions now that we know what the
19283 tree scope
= TREE_OPERAND (member
, 0);
19284 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
19285 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
19286 member
= lookup_qualified_name (scope
, tmpl
,
19287 /*is_type_p=*/false,
19288 /*complain=*/false);
19289 if (BASELINK_P (member
))
19291 BASELINK_FUNCTIONS (member
)
19292 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
19294 member
= (adjust_result_of_qualified_name_lookup
19295 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
19300 qualified_name_lookup_error (scope
, tmpl
, member
,
19302 RETURN (error_mark_node
);
19305 else if (TREE_CODE (member
) == SCOPE_REF
19306 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
19307 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
19309 if (complain
& tf_error
)
19311 if (TYPE_P (TREE_OPERAND (member
, 0)))
19312 error ("%qT is not a class or namespace",
19313 TREE_OPERAND (member
, 0));
19315 error ("%qD is not a class or namespace",
19316 TREE_OPERAND (member
, 0));
19318 RETURN (error_mark_node
);
19321 r
= finish_class_member_access_expr (object
, member
,
19322 /*template_p=*/false,
19324 if (TREE_CODE (r
) == COMPONENT_REF
)
19325 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
19330 RETURN (build_throw
19331 (RECUR (TREE_OPERAND (t
, 0))));
19335 vec
<constructor_elt
, va_gc
> *n
;
19336 constructor_elt
*ce
;
19337 unsigned HOST_WIDE_INT idx
;
19338 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19339 bool process_index_p
;
19341 bool need_copy_p
= false;
19344 if (type
== error_mark_node
)
19345 RETURN (error_mark_node
);
19347 /* We do not want to process the index of aggregate
19348 initializers as they are identifier nodes which will be
19349 looked up by digest_init. */
19350 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
19352 if (null_member_pointer_value_p (t
))
19354 gcc_assert (same_type_p (type
, TREE_TYPE (t
)));
19358 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
19359 newlen
= vec_safe_length (n
);
19360 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
19362 if (ce
->index
&& process_index_p
19363 /* An identifier index is looked up in the type
19364 being initialized, not the current scope. */
19365 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
19366 ce
->index
= RECUR (ce
->index
);
19368 if (PACK_EXPANSION_P (ce
->value
))
19370 /* Substitute into the pack expansion. */
19371 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
19374 if (ce
->value
== error_mark_node
19375 || PACK_EXPANSION_P (ce
->value
))
19377 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
19378 /* Just move the argument into place. */
19379 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
19382 /* Update the length of the final CONSTRUCTOR
19383 arguments vector, and note that we will need to
19385 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
19386 need_copy_p
= true;
19390 ce
->value
= RECUR (ce
->value
);
19395 vec
<constructor_elt
, va_gc
> *old_n
= n
;
19397 vec_alloc (n
, newlen
);
19398 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
19400 if (TREE_CODE (ce
->value
) == TREE_VEC
)
19402 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
19403 for (i
= 0; i
< len
; ++i
)
19404 CONSTRUCTOR_APPEND_ELT (n
, 0,
19405 TREE_VEC_ELT (ce
->value
, i
));
19408 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
19412 r
= build_constructor (init_list_type_node
, n
);
19413 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
19414 CONSTRUCTOR_IS_DESIGNATED_INIT (r
)
19415 = CONSTRUCTOR_IS_DESIGNATED_INIT (t
);
19417 if (TREE_HAS_CONSTRUCTOR (t
))
19419 fcl_t cl
= fcl_functional
;
19420 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
19422 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
19425 TREE_TYPE (r
) = type
;
19431 tree operand_0
= TREE_OPERAND (t
, 0);
19432 if (TYPE_P (operand_0
))
19434 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
19435 RETURN (get_typeid (operand_0
, complain
));
19439 operand_0
= RECUR (operand_0
);
19440 RETURN (build_typeid (operand_0
, complain
));
19451 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
19452 /* ??? We're doing a subset of finish_id_expression here. */
19453 if (tree wrap
= maybe_get_tls_wrapper_call (r
))
19454 /* Replace an evaluated use of the thread_local variable with
19455 a call to its wrapper. */
19457 else if (outer_automatic_var_p (r
))
19458 r
= process_outer_var_ref (r
, complain
);
19460 if (!TYPE_REF_P (TREE_TYPE (t
)))
19461 /* If the original type was a reference, we'll be wrapped in
19462 the appropriate INDIRECT_REF. */
19463 r
= convert_from_reference (r
);
19469 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19470 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
19471 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
19474 case OFFSETOF_EXPR
:
19477 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
19478 in_decl
, /*function_p=*/false,
19479 /*integral_constant_expression_p=*/false);
19480 RETURN (finish_offsetof (object_ptr
,
19481 RECUR (TREE_OPERAND (t
, 0)),
19482 EXPR_LOCATION (t
)));
19485 case ADDRESSOF_EXPR
:
19486 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
19487 RECUR (TREE_OPERAND (t
, 0)), complain
));
19491 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
19492 complain
, in_decl
);
19494 tree type2
= TRAIT_EXPR_TYPE2 (t
);
19495 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
19496 type2
= RECUR (type2
);
19498 type2
= tsubst (type2
, args
, complain
, in_decl
);
19500 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
19505 tree old_stmt_expr
= cur_stmt_expr
;
19506 tree stmt_expr
= begin_stmt_expr ();
19508 cur_stmt_expr
= stmt_expr
;
19509 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
19510 integral_constant_expression_p
);
19511 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
19512 cur_stmt_expr
= old_stmt_expr
;
19514 /* If the resulting list of expression statement is empty,
19515 fold it further into void_node. */
19516 if (empty_expr_stmt_p (stmt_expr
))
19517 stmt_expr
= void_node
;
19519 RETURN (stmt_expr
);
19524 if (complain
& tf_partial
)
19526 /* We don't have a full set of template arguments yet; don't touch
19527 the lambda at all. */
19528 gcc_assert (processing_template_decl
);
19531 tree r
= tsubst_lambda_expr (t
, args
, complain
, in_decl
);
19533 RETURN (build_lambda_object (r
));
19537 /* We can get here for a constant initializer of non-dependent type.
19538 FIXME stop folding in cp_parser_initializer_clause. */
19540 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
19545 case TRANSACTION_EXPR
:
19546 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
19547 integral_constant_expression_p
));
19550 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
19552 case VEC_PERM_EXPR
:
19554 tree op0
= RECUR (TREE_OPERAND (t
, 0));
19555 tree op1
= RECUR (TREE_OPERAND (t
, 1));
19556 tree op2
= RECUR (TREE_OPERAND (t
, 2));
19557 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
19561 case REQUIRES_EXPR
:
19562 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
19565 /* No need to substitute further, a RANGE_EXPR will always be built
19566 with constant operands. */
19569 case NON_LVALUE_EXPR
:
19570 case VIEW_CONVERT_EXPR
:
19571 if (location_wrapper_p (t
))
19572 /* We need to do this here as well as in tsubst_copy so we get the
19573 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19574 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t
, 0)),
19575 EXPR_LOCATION (t
)));
19579 /* Handle Objective-C++ constructs, if appropriate. */
19582 = objcp_tsubst_copy_and_build (t
, args
, complain
,
19583 in_decl
, /*function_p=*/false);
19587 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
19593 input_location
= loc
;
19597 /* Verify that the instantiated ARGS are valid. For type arguments,
19598 make sure that the type's linkage is ok. For non-type arguments,
19599 make sure they are constants if they are integral or enumerations.
19600 Emit an error under control of COMPLAIN, and return TRUE on error. */
19603 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
19605 if (dependent_template_arg_p (t
))
19607 if (ARGUMENT_PACK_P (t
))
19609 tree vec
= ARGUMENT_PACK_ARGS (t
);
19610 int len
= TREE_VEC_LENGTH (vec
);
19611 bool result
= false;
19614 for (i
= 0; i
< len
; ++i
)
19615 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
19619 else if (TYPE_P (t
))
19621 /* [basic.link]: A name with no linkage (notably, the name
19622 of a class or enumeration declared in a local scope)
19623 shall not be used to declare an entity with linkage.
19624 This implies that names with no linkage cannot be used as
19627 DR 757 relaxes this restriction for C++0x. */
19628 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
19629 : no_linkage_check (t
, /*relaxed_p=*/false));
19633 /* DR 488 makes use of a type with no linkage cause
19634 type deduction to fail. */
19635 if (complain
& tf_error
)
19637 if (TYPE_UNNAMED_P (nt
))
19638 error ("%qT is/uses unnamed type", t
);
19640 error ("template argument for %qD uses local type %qT",
19645 /* In order to avoid all sorts of complications, we do not
19646 allow variably-modified types as template arguments. */
19647 else if (variably_modified_type_p (t
, NULL_TREE
))
19649 if (complain
& tf_error
)
19650 error ("%qT is a variably modified type", t
);
19654 /* Class template and alias template arguments should be OK. */
19655 else if (DECL_TYPE_TEMPLATE_P (t
))
19657 /* A non-type argument of integral or enumerated type must be a
19659 else if (TREE_TYPE (t
)
19660 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
19661 && !REFERENCE_REF_P (t
)
19662 && !TREE_CONSTANT (t
))
19664 if (complain
& tf_error
)
19665 error ("integral expression %qE is not constant", t
);
19672 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
19674 int ix
, len
= DECL_NTPARMS (tmpl
);
19675 bool result
= false;
19677 for (ix
= 0; ix
!= len
; ix
++)
19679 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
19682 if (result
&& (complain
& tf_error
))
19683 error (" trying to instantiate %qD", tmpl
);
19687 /* We're out of SFINAE context now, so generate diagnostics for the access
19688 errors we saw earlier when instantiating D from TMPL and ARGS. */
19691 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
19693 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
19694 tree type
= TREE_TYPE (pattern
);
19695 location_t loc
= input_location
;
19697 push_access_scope (d
);
19698 push_deferring_access_checks (dk_no_deferred
);
19699 input_location
= DECL_SOURCE_LOCATION (pattern
);
19700 tsubst (type
, args
, tf_warning_or_error
, d
);
19701 input_location
= loc
;
19702 pop_deferring_access_checks ();
19703 pop_access_scope (d
);
19706 /* Instantiate the indicated variable, function, or alias template TMPL with
19707 the template arguments in TARG_PTR. */
19710 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19712 tree targ_ptr
= orig_args
;
19716 bool access_ok
= true;
19718 if (tmpl
== error_mark_node
)
19719 return error_mark_node
;
19721 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
19723 /* If this function is a clone, handle it specially. */
19724 if (DECL_CLONED_FUNCTION_P (tmpl
))
19729 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19730 DECL_CLONED_FUNCTION. */
19731 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
19732 targ_ptr
, complain
);
19733 if (spec
== error_mark_node
)
19734 return error_mark_node
;
19736 /* Look for the clone. */
19737 FOR_EACH_CLONE (clone
, spec
)
19738 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
19740 /* We should always have found the clone by now. */
19741 gcc_unreachable ();
19745 if (targ_ptr
== error_mark_node
)
19746 return error_mark_node
;
19748 /* Check to see if we already have this specialization. */
19749 gen_tmpl
= most_general_template (tmpl
);
19750 if (TMPL_ARGS_DEPTH (targ_ptr
)
19751 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
19752 /* targ_ptr only has the innermost template args, so add the outer ones
19753 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19754 the case of a non-dependent call within a template definition). */
19755 targ_ptr
= (add_outermost_template_args
19756 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
19759 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19760 but it doesn't seem to be on the hot path. */
19761 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
19763 gcc_assert (tmpl
== gen_tmpl
19764 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
19766 || fndecl
== NULL_TREE
);
19768 if (spec
!= NULL_TREE
)
19770 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
19772 if (complain
& tf_error
)
19773 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
19774 return error_mark_node
;
19779 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
19781 return error_mark_node
;
19783 /* We are building a FUNCTION_DECL, during which the access of its
19784 parameters and return types have to be checked. However this
19785 FUNCTION_DECL which is the desired context for access checking
19786 is not built yet. We solve this chicken-and-egg problem by
19787 deferring all checks until we have the FUNCTION_DECL. */
19788 push_deferring_access_checks (dk_deferred
);
19790 /* Instantiation of the function happens in the context of the function
19791 template, not the context of the overload resolution we're doing. */
19792 push_to_top_level ();
19793 /* If there are dependent arguments, e.g. because we're doing partial
19794 ordering, make sure processing_template_decl stays set. */
19795 if (uses_template_parms (targ_ptr
))
19796 ++processing_template_decl
;
19797 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19799 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
19800 complain
, gen_tmpl
, true);
19801 push_nested_class (ctx
);
19804 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
19806 fndecl
= NULL_TREE
;
19807 if (VAR_P (pattern
))
19809 /* We need to determine if we're using a partial or explicit
19810 specialization now, because the type of the variable could be
19812 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
19813 tree elt
= most_specialized_partial_spec (tid
, complain
);
19814 if (elt
== error_mark_node
)
19815 pattern
= error_mark_node
;
19818 tree partial_tmpl
= TREE_VALUE (elt
);
19819 tree partial_args
= TREE_PURPOSE (elt
);
19820 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
19821 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
19825 /* Substitute template parameters to obtain the specialization. */
19826 if (fndecl
== NULL_TREE
)
19827 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
19828 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
19829 pop_nested_class ();
19830 pop_from_top_level ();
19832 if (fndecl
== error_mark_node
)
19834 pop_deferring_access_checks ();
19835 return error_mark_node
;
19838 /* The DECL_TI_TEMPLATE should always be the immediate parent
19839 template, not the most general template. */
19840 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
19841 DECL_TI_ARGS (fndecl
) = targ_ptr
;
19843 /* Now we know the specialization, compute access previously
19844 deferred. Do no access control for inheriting constructors,
19845 as we already checked access for the inherited constructor. */
19846 if (!(flag_new_inheriting_ctors
19847 && DECL_INHERITED_CTOR (fndecl
)))
19849 push_access_scope (fndecl
);
19850 if (!perform_deferred_access_checks (complain
))
19852 pop_access_scope (fndecl
);
19854 pop_deferring_access_checks ();
19856 /* If we've just instantiated the main entry point for a function,
19857 instantiate all the alternate entry points as well. We do this
19858 by cloning the instantiation of the main entry point, not by
19859 instantiating the template clones. */
19860 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
19861 clone_function_decl (fndecl
, /*update_methods=*/false);
19865 if (!(complain
& tf_error
))
19867 /* Remember to reinstantiate when we're out of SFINAE so the user
19868 can see the errors. */
19869 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
19871 return error_mark_node
;
19876 /* Wrapper for instantiate_template_1. */
19879 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
19882 timevar_push (TV_TEMPLATE_INST
);
19883 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
19884 timevar_pop (TV_TEMPLATE_INST
);
19888 /* Instantiate the alias template TMPL with ARGS. Also push a template
19889 instantiation level, which instantiate_template doesn't do because
19890 functions and variables have sufficient context established by the
19894 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
19896 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
19897 return error_mark_node
;
19898 if (!push_tinst_level (tmpl
, args
))
19899 return error_mark_node
;
19902 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
19903 args
, tmpl
, complain
,
19904 /*require_all_args=*/true,
19905 /*use_default_args=*/true);
19907 tree r
= instantiate_template (tmpl
, args
, complain
);
19908 pop_tinst_level ();
19913 /* PARM is a template parameter pack for FN. Returns true iff
19914 PARM is used in a deducible way in the argument list of FN. */
19917 pack_deducible_p (tree parm
, tree fn
)
19919 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
19920 for (; t
; t
= TREE_CHAIN (t
))
19922 tree type
= TREE_VALUE (t
);
19924 if (!PACK_EXPANSION_P (type
))
19926 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
19927 packs
; packs
= TREE_CHAIN (packs
))
19928 if (template_args_equal (TREE_VALUE (packs
), parm
))
19930 /* The template parameter pack is used in a function parameter
19931 pack. If this is the end of the parameter list, the
19932 template parameter pack is deducible. */
19933 if (TREE_CHAIN (t
) == void_list_node
)
19936 /* Otherwise, not. Well, it could be deduced from
19937 a non-pack parameter, but doing so would end up with
19938 a deduction mismatch, so don't bother. */
19942 /* The template parameter pack isn't used in any function parameter
19943 packs, but it might be used deeper, e.g. tuple<Args...>. */
19947 /* Subroutine of fn_type_unification: check non-dependent parms for
19951 check_non_deducible_conversions (tree parms
, const tree
*args
, unsigned nargs
,
19952 tree fn
, unification_kind_t strict
, int flags
,
19953 struct conversion
**convs
, bool explain_p
)
19955 /* Non-constructor methods need to leave a conversion for 'this', which
19956 isn't included in nargs here. */
19957 unsigned offset
= (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19958 && !DECL_CONSTRUCTOR_P (fn
));
19960 for (unsigned ia
= 0;
19961 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19963 tree parm
= TREE_VALUE (parms
);
19965 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19966 && (!TREE_CHAIN (parms
)
19967 || TREE_CHAIN (parms
) == void_list_node
))
19968 /* For a function parameter pack that occurs at the end of the
19969 parameter-declaration-list, the type A of each remaining
19970 argument of the call is compared with the type P of the
19971 declarator-id of the function parameter pack. */
19974 parms
= TREE_CHAIN (parms
);
19976 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19977 /* For a function parameter pack that does not occur at the
19978 end of the parameter-declaration-list, the type of the
19979 parameter pack is a non-deduced context. */
19982 if (!uses_template_parms (parm
))
19984 tree arg
= args
[ia
];
19985 conversion
**conv_p
= convs
? &convs
[ia
+offset
] : NULL
;
19986 int lflags
= conv_flags (ia
, nargs
, fn
, arg
, flags
);
19988 if (check_non_deducible_conversion (parm
, arg
, strict
, lflags
,
19989 conv_p
, explain_p
))
19999 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20000 NARGS elements of the arguments that are being used when calling
20001 it. TARGS is a vector into which the deduced template arguments
20004 Returns either a FUNCTION_DECL for the matching specialization of FN or
20005 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20006 true, diagnostics will be printed to explain why it failed.
20008 If FN is a conversion operator, or we are trying to produce a specific
20009 specialization, RETURN_TYPE is the return type desired.
20011 The EXPLICIT_TARGS are explicit template arguments provided via a
20014 The parameter STRICT is one of:
20017 We are deducing arguments for a function call, as in
20018 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20019 deducing arguments for a call to the result of a conversion
20020 function template, as in [over.call.object].
20023 We are deducing arguments for a conversion function, as in
20024 [temp.deduct.conv].
20027 We are deducing arguments when doing an explicit instantiation
20028 as in [temp.explicit], when determining an explicit specialization
20029 as in [temp.expl.spec], or when taking the address of a function
20030 template, as in [temp.deduct.funcaddr]. */
20033 fn_type_unification (tree fn
,
20034 tree explicit_targs
,
20037 unsigned int nargs
,
20039 unification_kind_t strict
,
20041 struct conversion
**convs
,
20047 tree decl
= NULL_TREE
;
20048 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20050 static int deduction_depth
;
20051 /* type_unification_real will pass back any access checks from default
20052 template argument substitution. */
20053 vec
<deferred_access_check
, va_gc
> *checks
= NULL
;
20054 /* We don't have all the template args yet. */
20055 bool incomplete
= true;
20058 if (flag_new_inheriting_ctors
)
20059 fn
= strip_inheriting_ctors (fn
);
20061 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
20062 tree r
= error_mark_node
;
20064 tree full_targs
= targs
;
20065 if (TMPL_ARGS_DEPTH (targs
)
20066 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
20067 full_targs
= (add_outermost_template_args
20068 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
20072 complain
|= tf_decltype
;
20074 /* In C++0x, it's possible to have a function template whose type depends
20075 on itself recursively. This is most obvious with decltype, but can also
20076 occur with enumeration scope (c++/48969). So we need to catch infinite
20077 recursion and reject the substitution at deduction time; this function
20078 will return error_mark_node for any repeated substitution.
20080 This also catches excessive recursion such as when f<N> depends on
20081 f<N-1> across all integers, and returns error_mark_node for all the
20082 substitutions back up to the initial one.
20084 This is, of course, not reentrant. */
20085 if (excessive_deduction_depth
)
20086 return error_mark_node
;
20089 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
20091 fntype
= TREE_TYPE (fn
);
20092 if (explicit_targs
)
20096 The specified template arguments must match the template
20097 parameters in kind (i.e., type, nontype, template), and there
20098 must not be more arguments than there are parameters;
20099 otherwise type deduction fails.
20101 Nontype arguments must match the types of the corresponding
20102 nontype template parameters, or must be convertible to the
20103 types of the corresponding nontype parameters as specified in
20104 _temp.arg.nontype_, otherwise type deduction fails.
20106 All references in the function type of the function template
20107 to the corresponding template parameters are replaced by the
20108 specified template argument values. If a substitution in a
20109 template parameter or in the function type of the function
20110 template results in an invalid type, type deduction fails. */
20111 int i
, len
= TREE_VEC_LENGTH (tparms
);
20112 location_t loc
= input_location
;
20113 incomplete
= false;
20115 if (explicit_targs
== error_mark_node
)
20118 if (TMPL_ARGS_DEPTH (explicit_targs
)
20119 < TMPL_ARGS_DEPTH (full_targs
))
20120 explicit_targs
= add_outermost_template_args (full_targs
,
20123 /* Adjust any explicit template arguments before entering the
20124 substitution context. */
20126 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
20127 complain
|tf_partial
,
20128 /*require_all_args=*/false,
20129 /*use_default_args=*/false));
20130 if (explicit_targs
== error_mark_node
)
20133 /* Substitute the explicit args into the function type. This is
20134 necessary so that, for instance, explicitly declared function
20135 arguments can match null pointed constants. If we were given
20136 an incomplete set of explicit args, we must not do semantic
20137 processing during substitution as we could create partial
20139 for (i
= 0; i
< len
; i
++)
20141 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
20142 bool parameter_pack
= false;
20143 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
20145 /* Dig out the actual parm. */
20146 if (TREE_CODE (parm
) == TYPE_DECL
20147 || TREE_CODE (parm
) == TEMPLATE_DECL
)
20149 parm
= TREE_TYPE (parm
);
20150 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
20152 else if (TREE_CODE (parm
) == PARM_DECL
)
20154 parm
= DECL_INITIAL (parm
);
20155 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
20158 if (!parameter_pack
&& targ
== NULL_TREE
)
20159 /* No explicit argument for this template parameter. */
20162 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
20164 /* Mark the argument pack as "incomplete". We could
20165 still deduce more arguments during unification.
20166 We remove this mark in type_unification_real. */
20169 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
20170 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
20171 = ARGUMENT_PACK_ARGS (targ
);
20174 /* We have some incomplete argument packs. */
20181 if (!push_tinst_level (fn
, explicit_targs
))
20183 excessive_deduction_depth
= true;
20186 ++processing_template_decl
;
20187 input_location
= DECL_SOURCE_LOCATION (fn
);
20188 /* Ignore any access checks; we'll see them again in
20189 instantiate_template and they might have the wrong
20190 access path at this point. */
20191 push_deferring_access_checks (dk_deferred
);
20192 tsubst_flags_t ecomplain
= complain
| tf_partial
| tf_fndecl_type
;
20193 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
, ecomplain
, NULL_TREE
);
20194 pop_deferring_access_checks ();
20195 input_location
= loc
;
20196 --processing_template_decl
;
20197 pop_tinst_level ();
20199 if (fntype
== error_mark_node
)
20203 /* Place the explicitly specified arguments in TARGS. */
20204 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
20205 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
20206 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
20207 if (!incomplete
&& CHECKING_P
20208 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
20209 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20210 (targs
, NUM_TMPL_ARGS (explicit_targs
));
20213 if (return_type
&& strict
!= DEDUCE_CALL
)
20215 tree
*new_args
= XALLOCAVEC (tree
, nargs
+ 1);
20216 new_args
[0] = return_type
;
20217 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
20225 /* Never do unification on the 'this' parameter. */
20226 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
20228 if (return_type
&& strict
== DEDUCE_CALL
)
20230 /* We're deducing for a call to the result of a template conversion
20231 function. The parms we really want are in return_type. */
20232 if (INDIRECT_TYPE_P (return_type
))
20233 return_type
= TREE_TYPE (return_type
);
20234 parms
= TYPE_ARG_TYPES (return_type
);
20236 else if (return_type
)
20238 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
20241 /* We allow incomplete unification without an error message here
20242 because the standard doesn't seem to explicitly prohibit it. Our
20243 callers must be ready to deal with unification failures in any
20246 /* If we aren't explaining yet, push tinst context so we can see where
20247 any errors (e.g. from class instantiations triggered by instantiation
20248 of default template arguments) come from. If we are explaining, this
20249 context is redundant. */
20250 if (!explain_p
&& !push_tinst_level (fn
, targs
))
20252 excessive_deduction_depth
= true;
20256 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
20257 full_targs
, parms
, args
, nargs
, /*subr=*/0,
20258 strict
, &checks
, explain_p
);
20260 pop_tinst_level ();
20264 /* Now that we have bindings for all of the template arguments,
20265 ensure that the arguments deduced for the template template
20266 parameters have compatible template parameter lists. We cannot
20267 check this property before we have deduced all template
20268 arguments, because the template parameter types of a template
20269 template parameter might depend on prior template parameters
20270 deduced after the template template parameter. The following
20271 ill-formed example illustrates this issue:
20273 template<typename T, template<T> class C> void f(C<5>, T);
20275 template<int N> struct X {};
20278 f(X<5>(), 5l); // error: template argument deduction fails
20281 The template parameter list of 'C' depends on the template type
20282 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20283 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20284 time that we deduce 'C'. */
20285 if (!template_template_parm_bindings_ok_p
20286 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
20288 unify_inconsistent_template_template_parameters (explain_p
);
20292 /* DR 1391: All parameters have args, now check non-dependent parms for
20294 if (check_non_deducible_conversions (parms
, args
, nargs
, fn
, strict
, flags
,
20299 /* All is well so far. Now, check:
20303 When all template arguments have been deduced, all uses of
20304 template parameters in nondeduced contexts are replaced with
20305 the corresponding deduced argument values. If the
20306 substitution results in an invalid type, as described above,
20307 type deduction fails. */
20308 if (!push_tinst_level (fn
, targs
))
20310 excessive_deduction_depth
= true;
20314 /* Also collect access checks from the instantiation. */
20315 reopen_deferring_access_checks (checks
);
20317 decl
= instantiate_template (fn
, targs
, complain
);
20319 checks
= get_deferred_access_checks ();
20320 pop_deferring_access_checks ();
20322 pop_tinst_level ();
20324 if (decl
== error_mark_node
)
20327 /* Now perform any access checks encountered during substitution. */
20328 push_access_scope (decl
);
20329 ok
= perform_access_checks (checks
, complain
);
20330 pop_access_scope (decl
);
20334 /* If we're looking for an exact match, check that what we got
20335 is indeed an exact match. It might not be if some template
20336 parameters are used in non-deduced contexts. But don't check
20337 for an exact match if we have dependent template arguments;
20338 in that case we're doing partial ordering, and we already know
20339 that we have two candidates that will provide the actual type. */
20340 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
20342 tree substed
= TREE_TYPE (decl
);
20346 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
20348 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
20349 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
20350 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
20352 unify_type_mismatch (explain_p
, args
[i
],
20353 TREE_VALUE (sarg
));
20358 /* After doing deduction with the inherited constructor, actually return an
20359 instantiation of the inheriting constructor. */
20361 decl
= instantiate_template (orig_fn
, targs
, complain
);
20367 if (excessive_deduction_depth
)
20369 if (deduction_depth
== 0)
20370 /* Reset once we're all the way out. */
20371 excessive_deduction_depth
= false;
20377 /* Adjust types before performing type deduction, as described in
20378 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20379 sections are symmetric. PARM is the type of a function parameter
20380 or the return type of the conversion function. ARG is the type of
20381 the argument passed to the call, or the type of the value
20382 initialized with the result of the conversion function.
20383 ARG_EXPR is the original argument expression, which may be null. */
20386 maybe_adjust_types_for_deduction (unification_kind_t strict
,
20399 /* Swap PARM and ARG throughout the remainder of this
20400 function; the handling is precisely symmetric since PARM
20401 will initialize ARG rather than vice versa. */
20402 std::swap (parm
, arg
);
20406 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20407 too, but here handle it by stripping the reference from PARM
20408 rather than by adding it to ARG. */
20409 if (TYPE_REF_P (*parm
)
20410 && TYPE_REF_IS_RVALUE (*parm
)
20411 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20412 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20413 && TYPE_REF_P (*arg
)
20414 && !TYPE_REF_IS_RVALUE (*arg
))
20415 *parm
= TREE_TYPE (*parm
);
20416 /* Nothing else to do in this case. */
20420 gcc_unreachable ();
20423 if (!TYPE_REF_P (*parm
))
20425 /* [temp.deduct.call]
20427 If P is not a reference type:
20429 --If A is an array type, the pointer type produced by the
20430 array-to-pointer standard conversion (_conv.array_) is
20431 used in place of A for type deduction; otherwise,
20433 --If A is a function type, the pointer type produced by
20434 the function-to-pointer standard conversion
20435 (_conv.func_) is used in place of A for type deduction;
20438 --If A is a cv-qualified type, the top level
20439 cv-qualifiers of A's type are ignored for type
20441 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
20442 *arg
= build_pointer_type (TREE_TYPE (*arg
));
20443 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
20444 *arg
= build_pointer_type (*arg
);
20446 *arg
= TYPE_MAIN_VARIANT (*arg
);
20449 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20450 reference to a cv-unqualified template parameter that does not represent a
20451 template parameter of a class template (during class template argument
20452 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20453 an lvalue, the type "lvalue reference to A" is used in place of A for type
20455 if (TYPE_REF_P (*parm
)
20456 && TYPE_REF_IS_RVALUE (*parm
)
20457 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
20458 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
20459 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
20460 && (arg_expr
? lvalue_p (arg_expr
)
20461 /* try_one_overload doesn't provide an arg_expr, but
20462 functions are always lvalues. */
20463 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
20464 *arg
= build_reference_type (*arg
);
20466 /* [temp.deduct.call]
20468 If P is a cv-qualified type, the top level cv-qualifiers
20469 of P's type are ignored for type deduction. If P is a
20470 reference type, the type referred to by P is used for
20472 *parm
= TYPE_MAIN_VARIANT (*parm
);
20473 if (TYPE_REF_P (*parm
))
20475 *parm
= TREE_TYPE (*parm
);
20476 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20479 /* DR 322. For conversion deduction, remove a reference type on parm
20480 too (which has been swapped into ARG). */
20481 if (strict
== DEDUCE_CONV
&& TYPE_REF_P (*arg
))
20482 *arg
= TREE_TYPE (*arg
);
20487 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20488 template which doesn't contain any deducible template parameters; check if
20489 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20490 unify_one_argument. */
20493 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
20494 int flags
, struct conversion
**conv_p
,
20500 type
= TREE_TYPE (arg
);
20504 if (same_type_p (parm
, type
))
20505 return unify_success (explain_p
);
20507 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
20508 if (strict
== DEDUCE_CONV
)
20510 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
, complain
))
20511 return unify_success (explain_p
);
20513 else if (strict
!= DEDUCE_EXACT
)
20516 tree conv_arg
= TYPE_P (arg
) ? NULL_TREE
: arg
;
20518 /* Avoid recalculating this in add_function_candidate. */
20520 = good_conversion (parm
, type
, conv_arg
, flags
, complain
));
20522 ok
= can_convert_arg (parm
, type
, conv_arg
, flags
, complain
);
20524 return unify_success (explain_p
);
20527 if (strict
== DEDUCE_EXACT
)
20528 return unify_type_mismatch (explain_p
, parm
, arg
);
20530 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
20533 static bool uses_deducible_template_parms (tree type
);
20535 /* Returns true iff the expression EXPR is one from which a template
20536 argument can be deduced. In other words, if it's an undecorated
20537 use of a template non-type parameter. */
20540 deducible_expression (tree expr
)
20542 /* Strip implicit conversions. */
20543 while (CONVERT_EXPR_P (expr
))
20544 expr
= TREE_OPERAND (expr
, 0);
20545 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
20548 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20549 deducible way; that is, if it has a max value of <PARM> - 1. */
20552 deducible_array_bound (tree domain
)
20554 if (domain
== NULL_TREE
)
20557 tree max
= TYPE_MAX_VALUE (domain
);
20558 if (TREE_CODE (max
) != MINUS_EXPR
)
20561 return deducible_expression (TREE_OPERAND (max
, 0));
20564 /* Returns true iff the template arguments ARGS use a template parameter
20565 in a deducible way. */
20568 deducible_template_args (tree args
)
20570 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
20573 tree elt
= TREE_VEC_ELT (args
, i
);
20574 if (ARGUMENT_PACK_P (elt
))
20575 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
20578 if (PACK_EXPANSION_P (elt
))
20579 elt
= PACK_EXPANSION_PATTERN (elt
);
20580 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
20582 else if (TYPE_P (elt
))
20583 deducible
= uses_deducible_template_parms (elt
);
20585 deducible
= deducible_expression (elt
);
20593 /* Returns true iff TYPE contains any deducible references to template
20594 parameters, as per 14.8.2.5. */
20597 uses_deducible_template_parms (tree type
)
20599 if (PACK_EXPANSION_P (type
))
20600 type
= PACK_EXPANSION_PATTERN (type
);
20607 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20608 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20614 if (INDIRECT_TYPE_P (type
))
20615 return uses_deducible_template_parms (TREE_TYPE (type
));
20617 /* T[integer-constant ]
20619 if (TREE_CODE (type
) == ARRAY_TYPE
)
20620 return (uses_deducible_template_parms (TREE_TYPE (type
))
20621 || deducible_array_bound (TYPE_DOMAIN (type
)));
20633 if (TYPE_PTRMEM_P (type
))
20634 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
20635 || (uses_deducible_template_parms
20636 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
20638 /* template-name <T> (where template-name refers to a class template)
20639 template-name <i> (where template-name refers to a class template) */
20640 if (CLASS_TYPE_P (type
)
20641 && CLASSTYPE_TEMPLATE_INFO (type
)
20642 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
20643 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20644 (CLASSTYPE_TI_ARGS (type
)));
20649 if (TREE_CODE (type
) == FUNCTION_TYPE
20650 || TREE_CODE (type
) == METHOD_TYPE
)
20652 if (uses_deducible_template_parms (TREE_TYPE (type
)))
20654 tree parm
= TYPE_ARG_TYPES (type
);
20655 if (TREE_CODE (type
) == METHOD_TYPE
)
20656 parm
= TREE_CHAIN (parm
);
20657 for (; parm
; parm
= TREE_CHAIN (parm
))
20658 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
20665 /* Subroutine of type_unification_real and unify_pack_expansion to
20666 handle unification of a single P/A pair. Parameters are as
20667 for those functions. */
20670 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
20671 int subr
, unification_kind_t strict
,
20674 tree arg_expr
= NULL_TREE
;
20677 if (arg
== error_mark_node
|| parm
== error_mark_node
)
20678 return unify_invalid (explain_p
);
20679 if (arg
== unknown_type_node
)
20680 /* We can't deduce anything from this, but we might get all the
20681 template args from other function args. */
20682 return unify_success (explain_p
);
20684 /* Implicit conversions (Clause 4) will be performed on a function
20685 argument to convert it to the type of the corresponding function
20686 parameter if the parameter type contains no template-parameters that
20687 participate in template argument deduction. */
20688 if (strict
!= DEDUCE_EXACT
20689 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
20690 /* For function parameters with no deducible template parameters,
20691 just return. We'll check non-dependent conversions later. */
20692 return unify_success (explain_p
);
20697 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
20698 | UNIFY_ALLOW_MORE_CV_QUAL
20699 | UNIFY_ALLOW_DERIVED
);
20703 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
20707 arg_strict
= UNIFY_ALLOW_NONE
;
20711 gcc_unreachable ();
20714 /* We only do these transformations if this is the top-level
20715 parameter_type_list in a call or declaration matching; in other
20716 situations (nested function declarators, template argument lists) we
20717 won't be comparing a type to an expression, and we don't do any type
20723 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
20724 if (type_unknown_p (arg
))
20726 /* [temp.deduct.type] A template-argument can be
20727 deduced from a pointer to function or pointer
20728 to member function argument if the set of
20729 overloaded functions does not contain function
20730 templates and at most one of a set of
20731 overloaded functions provides a unique
20733 resolve_overloaded_unification (tparms
, targs
, parm
,
20735 arg_strict
, explain_p
);
20736 /* If a unique match was not found, this is a
20737 non-deduced context, so we still succeed. */
20738 return unify_success (explain_p
);
20742 arg
= unlowered_expr_type (arg
);
20743 if (arg
== error_mark_node
)
20744 return unify_invalid (explain_p
);
20748 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
20751 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
20752 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
20753 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20755 /* For deduction from an init-list we need the actual list. */
20756 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
20758 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
20761 /* for_each_template_parm callback that always returns 0. */
20764 zero_r (tree
, void *)
20769 /* for_each_template_parm any_fn callback to handle deduction of a template
20770 type argument from the type of an array bound. */
20773 array_deduction_r (tree t
, void *data
)
20775 tree_pair_p d
= (tree_pair_p
)data
;
20776 tree
&tparms
= d
->purpose
;
20777 tree
&targs
= d
->value
;
20779 if (TREE_CODE (t
) == ARRAY_TYPE
)
20780 if (tree dom
= TYPE_DOMAIN (t
))
20781 if (tree max
= TYPE_MAX_VALUE (dom
))
20783 if (TREE_CODE (max
) == MINUS_EXPR
)
20784 max
= TREE_OPERAND (max
, 0);
20785 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
20786 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
20787 UNIFY_ALLOW_NONE
, /*explain*/false);
20790 /* Keep walking. */
20794 /* Try to deduce any not-yet-deduced template type arguments from the type of
20795 an array bound. This is handled separately from unify because 14.8.2.5 says
20796 "The type of a type parameter is only deduced from an array bound if it is
20797 not otherwise deduced." */
20800 try_array_deduction (tree tparms
, tree targs
, tree parm
)
20802 tree_pair_s data
= { tparms
, targs
};
20803 hash_set
<tree
> visited
;
20804 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
20805 /*nondeduced*/false, array_deduction_r
);
20808 /* Most parms like fn_type_unification.
20810 If SUBR is 1, we're being called recursively (to unify the
20811 arguments of a function or method parameter of a function
20814 CHECKS is a pointer to a vector of access checks encountered while
20815 substituting default template arguments. */
20818 type_unification_real (tree tparms
,
20822 unsigned int xnargs
,
20824 unification_kind_t strict
,
20825 vec
<deferred_access_check
, va_gc
> **checks
,
20830 int ntparms
= TREE_VEC_LENGTH (tparms
);
20831 int saw_undeduced
= 0;
20834 unsigned int nargs
;
20837 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
20838 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
20839 gcc_assert (ntparms
> 0);
20841 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
20843 /* Reset the number of non-defaulted template arguments contained
20845 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
20853 while (parms
&& parms
!= void_list_node
20856 parm
= TREE_VALUE (parms
);
20858 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
20859 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
20860 /* For a function parameter pack that occurs at the end of the
20861 parameter-declaration-list, the type A of each remaining
20862 argument of the call is compared with the type P of the
20863 declarator-id of the function parameter pack. */
20866 parms
= TREE_CHAIN (parms
);
20868 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
20869 /* For a function parameter pack that does not occur at the
20870 end of the parameter-declaration-list, the type of the
20871 parameter pack is a non-deduced context. */
20877 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
20883 && parms
!= void_list_node
20884 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
20886 /* Unify the remaining arguments with the pack expansion type. */
20888 tree parmvec
= make_tree_vec (1);
20890 /* Allocate a TREE_VEC and copy in all of the arguments */
20891 argvec
= make_tree_vec (nargs
- ia
);
20892 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
20893 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
20895 /* Copy the parameter into parmvec. */
20896 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
20897 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
20898 /*subr=*/subr
, explain_p
))
20901 /* Advance to the end of the list of parameters. */
20902 parms
= TREE_CHAIN (parms
);
20905 /* Fail if we've reached the end of the parm list, and more args
20906 are present, and the parm list isn't variadic. */
20907 if (ia
< nargs
&& parms
== void_list_node
)
20908 return unify_too_many_arguments (explain_p
, nargs
, ia
);
20909 /* Fail if parms are left and they don't have default values and
20910 they aren't all deduced as empty packs (c++/57397). This is
20911 consistent with sufficient_parms_p. */
20912 if (parms
&& parms
!= void_list_node
20913 && TREE_PURPOSE (parms
) == NULL_TREE
)
20915 unsigned int count
= nargs
;
20920 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
20923 p
= TREE_CHAIN (p
);
20925 while (p
&& p
!= void_list_node
);
20926 if (count
!= nargs
)
20927 return unify_too_few_arguments (explain_p
, ia
, count
,
20933 tsubst_flags_t complain
= (explain_p
20934 ? tf_warning_or_error
20936 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
20938 for (i
= 0; i
< ntparms
; i
++)
20940 tree targ
= TREE_VEC_ELT (targs
, i
);
20941 tree tparm
= TREE_VEC_ELT (tparms
, i
);
20943 /* Clear the "incomplete" flags on all argument packs now so that
20944 substituting them into later default arguments works. */
20945 if (targ
&& ARGUMENT_PACK_P (targ
))
20947 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
20948 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
20951 if (targ
|| tparm
== error_mark_node
)
20953 tparm
= TREE_VALUE (tparm
);
20955 if (TREE_CODE (tparm
) == TYPE_DECL
20956 && !tried_array_deduction
)
20958 try_array_deduction (tparms
, targs
, xparms
);
20959 tried_array_deduction
= true;
20960 if (TREE_VEC_ELT (targs
, i
))
20964 /* If this is an undeduced nontype parameter that depends on
20965 a type parameter, try another pass; its type may have been
20966 deduced from a later argument than the one from which
20967 this parameter can be deduced. */
20968 if (TREE_CODE (tparm
) == PARM_DECL
20969 && uses_template_parms (TREE_TYPE (tparm
))
20970 && saw_undeduced
< 2)
20976 /* Core issue #226 (C++0x) [temp.deduct]:
20978 If a template argument has not been deduced, its
20979 default template argument, if any, is used.
20981 When we are in C++98 mode, TREE_PURPOSE will either
20982 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20983 to explicitly check cxx_dialect here. */
20984 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
20985 /* OK, there is a default argument. Wait until after the
20986 conversion check to do substitution. */
20989 /* If the type parameter is a parameter pack, then it will
20990 be deduced to an empty parameter pack. */
20991 if (template_parameter_pack_p (tparm
))
20995 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
20997 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
20998 TREE_CONSTANT (arg
) = 1;
21001 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21003 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
21005 TREE_VEC_ELT (targs
, i
) = arg
;
21009 return unify_parameter_deduction_failure (explain_p
, tparm
);
21012 /* Now substitute into the default template arguments. */
21013 for (i
= 0; i
< ntparms
; i
++)
21015 tree targ
= TREE_VEC_ELT (targs
, i
);
21016 tree tparm
= TREE_VEC_ELT (tparms
, i
);
21018 if (targ
|| tparm
== error_mark_node
)
21020 tree parm
= TREE_VALUE (tparm
);
21021 tree arg
= TREE_PURPOSE (tparm
);
21022 reopen_deferring_access_checks (*checks
);
21023 location_t save_loc
= input_location
;
21025 input_location
= DECL_SOURCE_LOCATION (parm
);
21027 if (saw_undeduced
== 1
21028 && TREE_CODE (parm
) == PARM_DECL
21029 && uses_template_parms (TREE_TYPE (parm
)))
21031 /* The type of this non-type parameter depends on undeduced
21032 parameters. Don't try to use its default argument yet,
21033 since we might deduce an argument for it on the next pass,
21034 but do check whether the arguments we already have cause
21035 substitution failure, so that that happens before we try
21036 later default arguments (78489). */
21037 ++processing_template_decl
;
21038 tree type
= tsubst (TREE_TYPE (parm
), full_targs
, complain
,
21040 --processing_template_decl
;
21041 if (type
== error_mark_node
)
21042 arg
= error_mark_node
;
21048 /* Even if the call is happening in template context, getting
21049 here means it's non-dependent, and a default argument is
21050 considered a separate definition under [temp.decls], so we can
21051 do this substitution without processing_template_decl. This
21052 is important if the default argument contains something that
21053 might be instantiation-dependent like access (87480). */
21054 processing_template_decl_sentinel s
;
21055 tree substed
= NULL_TREE
;
21056 if (saw_undeduced
== 1)
21058 /* First instatiate in template context, in case we still
21059 depend on undeduced template parameters. */
21060 ++processing_template_decl
;
21061 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21063 --processing_template_decl
;
21064 if (substed
!= error_mark_node
21065 && !uses_template_parms (substed
))
21066 /* We replaced all the tparms, substitute again out of
21067 template context. */
21068 substed
= NULL_TREE
;
21071 substed
= tsubst_template_arg (arg
, full_targs
, complain
,
21074 if (!uses_template_parms (substed
))
21075 arg
= convert_template_argument (parm
, substed
, full_targs
,
21076 complain
, i
, NULL_TREE
);
21077 else if (saw_undeduced
== 1)
21080 arg
= error_mark_node
;
21083 input_location
= save_loc
;
21084 *checks
= get_deferred_access_checks ();
21085 pop_deferring_access_checks ();
21087 if (arg
== error_mark_node
)
21091 TREE_VEC_ELT (targs
, i
) = arg
;
21092 /* The position of the first default template argument,
21093 is also the number of non-defaulted arguments in TARGS.
21095 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21096 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
21100 if (saw_undeduced
++ == 1)
21104 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
21105 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
21107 return unify_success (explain_p
);
21110 /* Subroutine of type_unification_real. Args are like the variables
21111 at the call site. ARG is an overloaded function (or template-id);
21112 we try deducing template args from each of the overloads, and if
21113 only one succeeds, we go with that. Modifies TARGS and returns
21114 true on success. */
21117 resolve_overloaded_unification (tree tparms
,
21121 unification_kind_t strict
,
21125 tree tempargs
= copy_node (targs
);
21127 tree goodfn
= NULL_TREE
;
21130 if (TREE_CODE (arg
) == ADDR_EXPR
)
21132 arg
= TREE_OPERAND (arg
, 0);
21138 if (TREE_CODE (arg
) == COMPONENT_REF
)
21139 /* Handle `&x' where `x' is some static or non-static member
21141 arg
= TREE_OPERAND (arg
, 1);
21143 if (TREE_CODE (arg
) == OFFSET_REF
)
21144 arg
= TREE_OPERAND (arg
, 1);
21146 /* Strip baselink information. */
21147 if (BASELINK_P (arg
))
21148 arg
= BASELINK_FUNCTIONS (arg
);
21150 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
21152 /* If we got some explicit template args, we need to plug them into
21153 the affected templates before we try to unify, in case the
21154 explicit args will completely resolve the templates in question. */
21157 tree expl_subargs
= TREE_OPERAND (arg
, 1);
21158 arg
= TREE_OPERAND (arg
, 0);
21160 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21163 tree subargs
, elem
;
21165 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21168 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21169 expl_subargs
, NULL_TREE
, tf_none
,
21170 /*require_all_args=*/true,
21171 /*use_default_args=*/true);
21172 if (subargs
!= error_mark_node
21173 && !any_dependent_template_arguments_p (subargs
))
21175 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
21176 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
21177 elem
, strict
, sub_strict
, addr_p
, explain_p
)
21178 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
21187 /* If no templates (or more than one) are fully resolved by the
21188 explicit arguments, this template-id is a non-deduced context; it
21189 could still be OK if we deduce all template arguments for the
21190 enclosing call through other arguments. */
21194 else if (TREE_CODE (arg
) != OVERLOAD
21195 && TREE_CODE (arg
) != FUNCTION_DECL
)
21196 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21197 -- but the deduction does not succeed because the expression is
21198 not just the function on its own. */
21201 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21204 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
21205 strict
, sub_strict
, addr_p
, explain_p
)
21206 && (!goodfn
|| !decls_match (goodfn
, fn
)))
21213 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21214 to function or pointer to member function argument if the set of
21215 overloaded functions does not contain function templates and at most
21216 one of a set of overloaded functions provides a unique match.
21218 So if we found multiple possibilities, we return success but don't
21219 deduce anything. */
21223 int i
= TREE_VEC_LENGTH (targs
);
21225 if (TREE_VEC_ELT (tempargs
, i
))
21227 tree old
= TREE_VEC_ELT (targs
, i
);
21228 tree new_
= TREE_VEC_ELT (tempargs
, i
);
21229 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
21230 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
21231 /* Don't forget explicit template arguments in a pack. */
21232 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
21233 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
21234 TREE_VEC_ELT (targs
, i
) = new_
;
21243 /* Core DR 115: In contexts where deduction is done and fails, or in
21244 contexts where deduction is not done, if a template argument list is
21245 specified and it, along with any default template arguments, identifies
21246 a single function template specialization, then the template-id is an
21247 lvalue for the function template specialization. */
21250 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
21252 tree expr
, offset
, baselink
;
21255 if (!type_unknown_p (orig_expr
))
21260 offset
= NULL_TREE
;
21261 baselink
= NULL_TREE
;
21263 if (TREE_CODE (expr
) == ADDR_EXPR
)
21265 expr
= TREE_OPERAND (expr
, 0);
21268 if (TREE_CODE (expr
) == OFFSET_REF
)
21271 expr
= TREE_OPERAND (expr
, 1);
21273 if (BASELINK_P (expr
))
21276 expr
= BASELINK_FUNCTIONS (expr
);
21279 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
21282 tree goodfn
= NULL_TREE
;
21284 /* If we got some explicit template args, we need to plug them into
21285 the affected templates before we try to unify, in case the
21286 explicit args will completely resolve the templates in question. */
21288 tree expl_subargs
= TREE_OPERAND (expr
, 1);
21289 tree arg
= TREE_OPERAND (expr
, 0);
21290 tree badfn
= NULL_TREE
;
21291 tree badargs
= NULL_TREE
;
21293 for (lkp_iterator
iter (arg
); iter
; ++iter
)
21296 tree subargs
, elem
;
21298 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
21301 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
21302 expl_subargs
, NULL_TREE
, tf_none
,
21303 /*require_all_args=*/true,
21304 /*use_default_args=*/true);
21305 if (subargs
!= error_mark_node
21306 && !any_dependent_template_arguments_p (subargs
))
21308 elem
= instantiate_template (fn
, subargs
, tf_none
);
21309 if (elem
== error_mark_node
)
21314 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
21323 mark_used (goodfn
);
21326 expr
= build_baselink (BASELINK_BINFO (baselink
),
21327 BASELINK_ACCESS_BINFO (baselink
),
21328 expr
, BASELINK_OPTYPE (baselink
));
21332 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
21333 expr
= build_offset_ref (base
, expr
, addr
, complain
);
21336 expr
= cp_build_addr_expr (expr
, complain
);
21339 else if (good
== 0 && badargs
&& (complain
& tf_error
))
21340 /* There were no good options and at least one bad one, so let the
21341 user know what the problem is. */
21342 instantiate_template (badfn
, badargs
, complain
);
21347 /* As above, but error out if the expression remains overloaded. */
21350 resolve_nondeduced_context_or_error (tree exp
, tsubst_flags_t complain
)
21352 exp
= resolve_nondeduced_context (exp
, complain
);
21353 if (type_unknown_p (exp
))
21355 if (complain
& tf_error
)
21356 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
21357 return error_mark_node
;
21362 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21363 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21364 different overloads deduce different arguments for a given parm.
21365 ADDR_P is true if the expression for which deduction is being
21366 performed was of the form "& fn" rather than simply "fn".
21368 Returns 1 on success. */
21371 try_one_overload (tree tparms
,
21376 unification_kind_t strict
,
21385 if (arg
== error_mark_node
)
21388 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21389 to function or pointer to member function argument if the set of
21390 overloaded functions does not contain function templates and at most
21391 one of a set of overloaded functions provides a unique match.
21393 So if this is a template, just return success. */
21395 if (uses_template_parms (arg
))
21398 if (TREE_CODE (arg
) == METHOD_TYPE
)
21399 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
21401 arg
= build_pointer_type (arg
);
21403 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
21405 /* We don't copy orig_targs for this because if we have already deduced
21406 some template args from previous args, unify would complain when we
21407 try to deduce a template parameter for the same argument, even though
21408 there isn't really a conflict. */
21409 nargs
= TREE_VEC_LENGTH (targs
);
21410 tempargs
= make_tree_vec (nargs
);
21412 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
21415 /* First make sure we didn't deduce anything that conflicts with
21416 explicitly specified args. */
21417 for (i
= nargs
; i
--; )
21419 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21420 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
21424 else if (uses_template_parms (elt
))
21425 /* Since we're unifying against ourselves, we will fill in
21426 template args used in the function parm list with our own
21427 template parms. Discard them. */
21428 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
21429 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
21431 /* Check that the argument at each index of the deduced argument pack
21432 is equivalent to the corresponding explicitly specified argument.
21433 We may have deduced more arguments than were explicitly specified,
21436 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21437 that's wrong if we deduce the same argument pack from multiple
21438 function arguments: it's only incomplete the first time. */
21440 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
21441 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
21443 if (TREE_VEC_LENGTH (deduced_pack
)
21444 < TREE_VEC_LENGTH (explicit_pack
))
21447 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
21448 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
21449 TREE_VEC_ELT (deduced_pack
, j
)))
21452 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
21456 for (i
= nargs
; i
--; )
21458 tree elt
= TREE_VEC_ELT (tempargs
, i
);
21461 TREE_VEC_ELT (targs
, i
) = elt
;
21467 /* PARM is a template class (perhaps with unbound template
21468 parameters). ARG is a fully instantiated type. If ARG can be
21469 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21470 TARGS are as for unify. */
21473 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
21476 tree copy_of_targs
;
21478 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
21480 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21481 /* Matches anything. */;
21482 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
21483 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
21486 /* We need to make a new template argument vector for the call to
21487 unify. If we used TARGS, we'd clutter it up with the result of
21488 the attempted unification, even if this class didn't work out.
21489 We also don't want to commit ourselves to all the unifications
21490 we've already done, since unification is supposed to be done on
21491 an argument-by-argument basis. In other words, consider the
21492 following pathological case:
21494 template <int I, int J, int K>
21497 template <int I, int J>
21498 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21500 template <int I, int J, int K>
21501 void f(S<I, J, K>, S<I, I, I>);
21510 Now, by the time we consider the unification involving `s2', we
21511 already know that we must have `f<0, 0, 0>'. But, even though
21512 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21513 because there are two ways to unify base classes of S<0, 1, 2>
21514 with S<I, I, I>. If we kept the already deduced knowledge, we
21515 would reject the possibility I=1. */
21516 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
21518 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21520 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
21525 /* If unification failed, we're done. */
21526 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
21527 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
21533 /* Given a template type PARM and a class type ARG, find the unique
21534 base type in ARG that is an instance of PARM. We do not examine
21535 ARG itself; only its base-classes. If there is not exactly one
21536 appropriate base class, return NULL_TREE. PARM may be the type of
21537 a partial specialization, as well as a plain template type. Used
21540 static enum template_base_result
21541 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
21542 bool explain_p
, tree
*result
)
21544 tree rval
= NULL_TREE
;
21547 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
21549 binfo
= TYPE_BINFO (complete_type (arg
));
21552 /* The type could not be completed. */
21553 *result
= NULL_TREE
;
21554 return tbr_incomplete_type
;
21557 /* Walk in inheritance graph order. The search order is not
21558 important, and this avoids multiple walks of virtual bases. */
21559 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
21561 tree r
= try_class_unification (tparms
, targs
, parm
,
21562 BINFO_TYPE (binfo
), explain_p
);
21566 /* If there is more than one satisfactory baseclass, then:
21570 If they yield more than one possible deduced A, the type
21574 if (rval
&& !same_type_p (r
, rval
))
21576 *result
= NULL_TREE
;
21577 return tbr_ambiguous_baseclass
;
21585 return tbr_success
;
21588 /* Returns the level of DECL, which declares a template parameter. */
21591 template_decl_level (tree decl
)
21593 switch (TREE_CODE (decl
))
21596 case TEMPLATE_DECL
:
21597 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
21600 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
21603 gcc_unreachable ();
21608 /* Decide whether ARG can be unified with PARM, considering only the
21609 cv-qualifiers of each type, given STRICT as documented for unify.
21610 Returns nonzero iff the unification is OK on that basis. */
21613 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
21615 int arg_quals
= cp_type_quals (arg
);
21616 int parm_quals
= cp_type_quals (parm
);
21618 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21619 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21621 /* Although a CVR qualifier is ignored when being applied to a
21622 substituted template parameter ([8.3.2]/1 for example), that
21623 does not allow us to unify "const T" with "int&" because both
21624 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21625 It is ok when we're allowing additional CV qualifiers
21626 at the outer level [14.8.2.1]/3,1st bullet. */
21627 if ((TYPE_REF_P (arg
)
21628 || TREE_CODE (arg
) == FUNCTION_TYPE
21629 || TREE_CODE (arg
) == METHOD_TYPE
)
21630 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
21633 if ((!INDIRECT_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
21634 && (parm_quals
& TYPE_QUAL_RESTRICT
))
21638 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
21639 && (arg_quals
& parm_quals
) != parm_quals
)
21642 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
21643 && (parm_quals
& arg_quals
) != arg_quals
)
21649 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21651 template_parm_level_and_index (tree parm
, int* level
, int* index
)
21653 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
21654 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
21655 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
21657 *index
= TEMPLATE_TYPE_IDX (parm
);
21658 *level
= TEMPLATE_TYPE_LEVEL (parm
);
21662 *index
= TEMPLATE_PARM_IDX (parm
);
21663 *level
= TEMPLATE_PARM_LEVEL (parm
);
21667 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21669 if (unify (TP, TA, P, A, S, EP)) \
21673 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21674 expansion at the end of PACKED_PARMS. Returns 0 if the type
21675 deduction succeeds, 1 otherwise. STRICT is the same as in
21676 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21677 function call argument list. We'll need to adjust the arguments to make them
21678 types. SUBR tells us if this is from a recursive call to
21679 type_unification_real, or for comparing two template argument
21683 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
21684 tree packed_args
, unification_kind_t strict
,
21685 bool subr
, bool explain_p
)
21688 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
21689 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
21690 tree pack
, packs
= NULL_TREE
;
21691 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
21693 /* Add in any args remembered from an earlier partial instantiation. */
21694 targs
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm
), targs
);
21695 int levels
= TMPL_ARGS_DEPTH (targs
);
21697 packed_args
= expand_template_argument_pack (packed_args
);
21699 int len
= TREE_VEC_LENGTH (packed_args
);
21701 /* Determine the parameter packs we will be deducing from the
21702 pattern, and record their current deductions. */
21703 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
21704 pack
; pack
= TREE_CHAIN (pack
))
21706 tree parm_pack
= TREE_VALUE (pack
);
21709 /* Only template parameter packs can be deduced, not e.g. function
21710 parameter packs or __bases or __integer_pack. */
21711 if (!TEMPLATE_PARM_P (parm_pack
))
21714 /* Determine the index and level of this parameter pack. */
21715 template_parm_level_and_index (parm_pack
, &level
, &idx
);
21716 if (level
< levels
)
21719 /* Keep track of the parameter packs and their corresponding
21721 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
21722 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
21725 /* Loop through all of the arguments that have not yet been
21726 unified and unify each with the pattern. */
21727 for (i
= start
; i
< len
; i
++)
21730 bool any_explicit
= false;
21731 tree arg
= TREE_VEC_ELT (packed_args
, i
);
21733 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21734 or the element of its argument pack at the current index if
21735 this argument was explicitly specified. */
21736 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21740 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21743 if (TREE_VALUE (pack
)
21744 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
21745 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
21747 any_explicit
= true;
21748 arg
= TREE_VEC_ELT (pargs
, i
- start
);
21750 TMPL_ARG (targs
, level
, idx
) = arg
;
21753 /* If we had explicit template arguments, substitute them into the
21754 pattern before deduction. */
21757 /* Some arguments might still be unspecified or dependent. */
21759 ++processing_template_decl
;
21760 dependent
= any_dependent_template_arguments_p (targs
);
21762 --processing_template_decl
;
21763 parm
= tsubst (pattern
, targs
,
21764 explain_p
? tf_warning_or_error
: tf_none
,
21767 --processing_template_decl
;
21768 if (parm
== error_mark_node
)
21774 /* Unify the pattern with the current argument. */
21775 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
21779 /* For each parameter pack, collect the deduced value. */
21780 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21783 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21785 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
21786 TMPL_ARG (targs
, level
, idx
);
21790 /* Verify that the results of unification with the parameter packs
21791 produce results consistent with what we've seen before, and make
21792 the deduced argument packs available. */
21793 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
21795 tree old_pack
= TREE_VALUE (pack
);
21796 tree new_args
= TREE_TYPE (pack
);
21797 int i
, len
= TREE_VEC_LENGTH (new_args
);
21799 bool nondeduced_p
= false;
21801 /* By default keep the original deduced argument pack.
21802 If necessary, more specific code is going to update the
21803 resulting deduced argument later down in this function. */
21804 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
21805 TMPL_ARG (targs
, level
, idx
) = old_pack
;
21807 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21808 actually deduce anything. */
21809 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
21810 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
21811 nondeduced_p
= true;
21815 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
21817 /* If we had fewer function args than explicit template args,
21818 just use the explicits. */
21819 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21820 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
21821 if (len
< explicit_len
)
21822 new_args
= explicit_args
;
21828 /* Build the deduced *_ARGUMENT_PACK. */
21829 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
21831 result
= make_node (NONTYPE_ARGUMENT_PACK
);
21832 TREE_CONSTANT (result
) = 1;
21835 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
21837 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
21839 /* Note the deduced argument packs for this parameter
21841 TMPL_ARG (targs
, level
, idx
) = result
;
21843 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
21844 && (ARGUMENT_PACK_ARGS (old_pack
)
21845 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
21847 /* We only had the explicitly-provided arguments before, but
21848 now we have a complete set of arguments. */
21849 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
21851 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
21852 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
21853 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
21857 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
21858 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
21860 if (!comp_template_args (old_args
, new_args
,
21861 &bad_old_arg
, &bad_new_arg
))
21862 /* Inconsistent unification of this parameter pack. */
21863 return unify_parameter_pack_inconsistent (explain_p
,
21869 return unify_success (explain_p
);
21872 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21873 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21874 parameters and return value are as for unify. */
21877 unify_array_domain (tree tparms
, tree targs
,
21878 tree parm_dom
, tree arg_dom
,
21886 /* Our representation of array types uses "N - 1" as the
21887 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21888 not an integer constant. We cannot unify arbitrarily
21889 complex expressions, so we eliminate the MINUS_EXPRs
21891 parm_max
= TYPE_MAX_VALUE (parm_dom
);
21892 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
21895 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
21896 parm_max
= TREE_OPERAND (parm_max
, 0);
21898 arg_max
= TYPE_MAX_VALUE (arg_dom
);
21899 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
21902 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21903 trying to unify the type of a variable with the type
21904 of a template parameter. For example:
21906 template <unsigned int N>
21907 void f (char (&) [N]);
21914 Here, the type of the ARG will be "int [g(i)]", and
21915 may be a SAVE_EXPR, etc. */
21916 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
21917 return unify_vla_arg (explain_p
, arg_dom
);
21918 arg_max
= TREE_OPERAND (arg_max
, 0);
21921 /* If only one of the bounds used a MINUS_EXPR, compensate
21922 by adding one to the other bound. */
21923 if (parm_cst
&& !arg_cst
)
21924 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21928 else if (arg_cst
&& !parm_cst
)
21929 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
21934 return unify (tparms
, targs
, parm_max
, arg_max
,
21935 UNIFY_ALLOW_INTEGER
, explain_p
);
21938 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21940 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
21945 if (PACK_EXPANSION_P (t
))
21946 t
= PACK_EXPANSION_PATTERN (t
);
21947 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
21948 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
21949 || DECL_TYPE_TEMPLATE_P (t
))
21951 else if (TYPE_P (t
))
21957 /* Deduce the value of template parameters. TPARMS is the (innermost)
21958 set of template parameters to a template. TARGS is the bindings
21959 for those template parameters, as determined thus far; TARGS may
21960 include template arguments for outer levels of template parameters
21961 as well. PARM is a parameter to a template function, or a
21962 subcomponent of that parameter; ARG is the corresponding argument.
21963 This function attempts to match PARM with ARG in a manner
21964 consistent with the existing assignments in TARGS. If more values
21965 are deduced, then TARGS is updated.
21967 Returns 0 if the type deduction succeeds, 1 otherwise. The
21968 parameter STRICT is a bitwise or of the following flags:
21971 Require an exact match between PARM and ARG.
21972 UNIFY_ALLOW_MORE_CV_QUAL:
21973 Allow the deduced ARG to be more cv-qualified (by qualification
21974 conversion) than ARG.
21975 UNIFY_ALLOW_LESS_CV_QUAL:
21976 Allow the deduced ARG to be less cv-qualified than ARG.
21977 UNIFY_ALLOW_DERIVED:
21978 Allow the deduced ARG to be a template base class of ARG,
21979 or a pointer to a template base class of the type pointed to by
21981 UNIFY_ALLOW_INTEGER:
21982 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21983 case for more information.
21984 UNIFY_ALLOW_OUTER_LEVEL:
21985 This is the outermost level of a deduction. Used to determine validity
21986 of qualification conversions. A valid qualification conversion must
21987 have const qualified pointers leading up to the inner type which
21988 requires additional CV quals, except at the outer level, where const
21989 is not required [conv.qual]. It would be normal to set this flag in
21990 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21991 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21992 This is the outermost level of a deduction, and PARM can be more CV
21993 qualified at this point.
21994 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21995 This is the outermost level of a deduction, and PARM can be less CV
21996 qualified at this point. */
21999 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
22005 int strict_in
= strict
;
22006 tsubst_flags_t complain
= (explain_p
22007 ? tf_warning_or_error
22010 /* I don't think this will do the right thing with respect to types.
22011 But the only case I've seen it in so far has been array bounds, where
22012 signedness is the only information lost, and I think that will be
22014 while (CONVERT_EXPR_P (parm
))
22015 parm
= TREE_OPERAND (parm
, 0);
22017 if (arg
== error_mark_node
)
22018 return unify_invalid (explain_p
);
22019 if (arg
== unknown_type_node
22020 || arg
== init_list_type_node
)
22021 /* We can't deduce anything from this, but we might get all the
22022 template args from other function args. */
22023 return unify_success (explain_p
);
22025 if (parm
== any_targ_node
|| arg
== any_targ_node
)
22026 return unify_success (explain_p
);
22028 /* If PARM uses template parameters, then we can't bail out here,
22029 even if ARG == PARM, since we won't record unifications for the
22030 template parameters. We might need them if we're trying to
22031 figure out which of two things is more specialized. */
22032 if (arg
== parm
&& !uses_template_parms (parm
))
22033 return unify_success (explain_p
);
22035 /* Handle init lists early, so the rest of the function can assume
22036 we're dealing with a type. */
22037 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
22041 tree orig_parm
= parm
;
22043 /* Replace T with std::initializer_list<T> for deduction. */
22044 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22045 && flag_deduce_init_list
)
22046 parm
= listify (parm
);
22048 if (!is_std_init_list (parm
)
22049 && TREE_CODE (parm
) != ARRAY_TYPE
)
22050 /* We can only deduce from an initializer list argument if the
22051 parameter is std::initializer_list or an array; otherwise this
22052 is a non-deduced context. */
22053 return unify_success (explain_p
);
22055 if (TREE_CODE (parm
) == ARRAY_TYPE
)
22056 elttype
= TREE_TYPE (parm
);
22059 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
22060 /* Deduction is defined in terms of a single type, so just punt
22061 on the (bizarre) std::initializer_list<T...>. */
22062 if (PACK_EXPANSION_P (elttype
))
22063 return unify_success (explain_p
);
22066 if (strict
!= DEDUCE_EXACT
22067 && TYPE_P (elttype
)
22068 && !uses_deducible_template_parms (elttype
))
22069 /* If ELTTYPE has no deducible template parms, skip deduction from
22070 the list elements. */;
22072 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
22074 int elt_strict
= strict
;
22076 if (elt
== error_mark_node
)
22077 return unify_invalid (explain_p
);
22079 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
22081 tree type
= TREE_TYPE (elt
);
22082 if (type
== error_mark_node
)
22083 return unify_invalid (explain_p
);
22084 /* It should only be possible to get here for a call. */
22085 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
22086 elt_strict
|= maybe_adjust_types_for_deduction
22087 (DEDUCE_CALL
, &elttype
, &type
, elt
);
22091 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
22095 if (TREE_CODE (parm
) == ARRAY_TYPE
22096 && deducible_array_bound (TYPE_DOMAIN (parm
)))
22098 /* Also deduce from the length of the initializer list. */
22099 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
22100 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
22101 if (idx
== error_mark_node
)
22102 return unify_invalid (explain_p
);
22103 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22107 /* If the std::initializer_list<T> deduction worked, replace the
22108 deduced A with std::initializer_list<A>. */
22109 if (orig_parm
!= parm
)
22111 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
22112 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22113 targ
= listify (targ
);
22114 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
22116 return unify_success (explain_p
);
22119 /* If parm and arg aren't the same kind of thing (template, type, or
22120 expression), fail early. */
22121 if (pa_kind (parm
) != pa_kind (arg
))
22122 return unify_invalid (explain_p
);
22124 /* Immediately reject some pairs that won't unify because of
22125 cv-qualification mismatches. */
22126 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22128 /* It is the elements of the array which hold the cv quals of an array
22129 type, and the elements might be template type parms. We'll check
22130 when we recurse. */
22131 && TREE_CODE (arg
) != ARRAY_TYPE
22132 /* We check the cv-qualifiers when unifying with template type
22133 parameters below. We want to allow ARG `const T' to unify with
22134 PARM `T' for example, when computing which of two templates
22135 is more specialized, for example. */
22136 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
22137 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
22138 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22140 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
22141 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
22142 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
22143 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
22144 strict
&= ~UNIFY_ALLOW_DERIVED
;
22145 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
22146 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
22148 switch (TREE_CODE (parm
))
22150 case TYPENAME_TYPE
:
22152 case UNBOUND_CLASS_TEMPLATE
:
22153 /* In a type which contains a nested-name-specifier, template
22154 argument values cannot be deduced for template parameters used
22155 within the nested-name-specifier. */
22156 return unify_success (explain_p
);
22158 case TEMPLATE_TYPE_PARM
:
22159 case TEMPLATE_TEMPLATE_PARM
:
22160 case BOUND_TEMPLATE_TEMPLATE_PARM
:
22161 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22162 if (error_operand_p (tparm
))
22163 return unify_invalid (explain_p
);
22165 if (TEMPLATE_TYPE_LEVEL (parm
)
22166 != template_decl_level (tparm
))
22167 /* The PARM is not one we're trying to unify. Just check
22168 to see if it matches ARG. */
22170 if (TREE_CODE (arg
) == TREE_CODE (parm
)
22171 && (is_auto (parm
) ? is_auto (arg
)
22172 : same_type_p (parm
, arg
)))
22173 return unify_success (explain_p
);
22175 return unify_type_mismatch (explain_p
, parm
, arg
);
22177 idx
= TEMPLATE_TYPE_IDX (parm
);
22178 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22179 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
22180 if (error_operand_p (tparm
))
22181 return unify_invalid (explain_p
);
22183 /* Check for mixed types and values. */
22184 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
22185 && TREE_CODE (tparm
) != TYPE_DECL
)
22186 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22187 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
22188 gcc_unreachable ();
22190 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22192 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
22193 && CLASS_TYPE_P (arg
))
22195 /* First try to match ARG directly. */
22196 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
22200 /* Otherwise, look for a suitable base of ARG, as below. */
22201 enum template_base_result r
;
22202 r
= get_template_base (tparms
, targs
, parm
, arg
,
22205 return unify_no_common_base (explain_p
, r
, parm
, arg
);
22209 /* ARG must be constructed from a template class or a template
22210 template parameter. */
22211 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
22212 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
22213 return unify_template_deduction_failure (explain_p
, parm
, arg
);
22215 /* Deduce arguments T, i from TT<T> or TT<i>. */
22216 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
22219 arg
= TYPE_TI_TEMPLATE (arg
);
22221 /* Fall through to deduce template name. */
22224 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
22225 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22227 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22229 /* Simple cases: Value already set, does match or doesn't. */
22230 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
22231 return unify_success (explain_p
);
22233 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22237 /* If PARM is `const T' and ARG is only `int', we don't have
22238 a match unless we are allowing additional qualification.
22239 If ARG is `const int' and PARM is just `T' that's OK;
22240 that binds `const int' to `T'. */
22241 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
22243 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22245 /* Consider the case where ARG is `const volatile int' and
22246 PARM is `const T'. Then, T should be `volatile int'. */
22247 arg
= cp_build_qualified_type_real
22248 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
22249 if (arg
== error_mark_node
)
22250 return unify_invalid (explain_p
);
22252 /* Simple cases: Value already set, does match or doesn't. */
22253 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
22254 return unify_success (explain_p
);
22256 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
22258 /* Make sure that ARG is not a variable-sized array. (Note
22259 that were talking about variable-sized arrays (like
22260 `int[n]'), rather than arrays of unknown size (like
22261 `int[]').) We'll get very confused by such a type since
22262 the bound of the array is not constant, and therefore
22263 not mangleable. Besides, such types are not allowed in
22264 ISO C++, so we can do as we please here. We do allow
22265 them for 'auto' deduction, since that isn't ABI-exposed. */
22266 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
22267 return unify_vla_arg (explain_p
, arg
);
22269 /* Strip typedefs as in convert_template_argument. */
22270 arg
= canonicalize_type_argument (arg
, tf_none
);
22273 /* If ARG is a parameter pack or an expansion, we cannot unify
22274 against it unless PARM is also a parameter pack. */
22275 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22276 && !template_parameter_pack_p (parm
))
22277 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22279 /* If the argument deduction results is a METHOD_TYPE,
22280 then there is a problem.
22281 METHOD_TYPE doesn't map to any real C++ type the result of
22282 the deduction cannot be of that type. */
22283 if (TREE_CODE (arg
) == METHOD_TYPE
)
22284 return unify_method_type_error (explain_p
, arg
);
22286 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22287 return unify_success (explain_p
);
22289 case TEMPLATE_PARM_INDEX
:
22290 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
22291 if (error_operand_p (tparm
))
22292 return unify_invalid (explain_p
);
22294 if (TEMPLATE_PARM_LEVEL (parm
)
22295 != template_decl_level (tparm
))
22297 /* The PARM is not one we're trying to unify. Just check
22298 to see if it matches ARG. */
22299 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
22300 && cp_tree_equal (parm
, arg
));
22302 unify_expression_unequal (explain_p
, parm
, arg
);
22306 idx
= TEMPLATE_PARM_IDX (parm
);
22307 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
22311 if ((strict
& UNIFY_ALLOW_INTEGER
)
22312 && TREE_TYPE (targ
) && TREE_TYPE (arg
)
22313 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
22314 /* We're deducing from an array bound, the type doesn't matter. */
22315 arg
= fold_convert (TREE_TYPE (targ
), arg
);
22316 int x
= !cp_tree_equal (targ
, arg
);
22318 unify_inconsistency (explain_p
, parm
, targ
, arg
);
22322 /* [temp.deduct.type] If, in the declaration of a function template
22323 with a non-type template-parameter, the non-type
22324 template-parameter is used in an expression in the function
22325 parameter-list and, if the corresponding template-argument is
22326 deduced, the template-argument type shall match the type of the
22327 template-parameter exactly, except that a template-argument
22328 deduced from an array bound may be of any integral type.
22329 The non-type parameter might use already deduced type parameters. */
22330 tparm
= TREE_TYPE (parm
);
22331 if (TEMPLATE_PARM_LEVEL (parm
) > TMPL_ARGS_DEPTH (targs
))
22332 /* We don't have enough levels of args to do any substitution. This
22333 can happen in the context of -fnew-ttp-matching. */;
22336 ++processing_template_decl
;
22337 tparm
= tsubst (tparm
, targs
, tf_none
, NULL_TREE
);
22338 --processing_template_decl
;
22340 if (tree a
= type_uses_auto (tparm
))
22342 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
22343 if (tparm
== error_mark_node
)
22348 if (!TREE_TYPE (arg
))
22349 /* Template-parameter dependent expression. Just accept it for now.
22350 It will later be processed in convert_template_argument. */
22352 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
22353 non_reference (tparm
)))
22355 else if ((strict
& UNIFY_ALLOW_INTEGER
)
22356 && CP_INTEGRAL_TYPE_P (tparm
))
22357 /* Convert the ARG to the type of PARM; the deduced non-type
22358 template argument must exactly match the types of the
22359 corresponding parameter. */
22360 arg
= fold (build_nop (tparm
, arg
));
22361 else if (uses_template_parms (tparm
))
22363 /* We haven't deduced the type of this parameter yet. */
22364 if (cxx_dialect
>= cxx17
22365 /* We deduce from array bounds in try_array_deduction. */
22366 && !(strict
& UNIFY_ALLOW_INTEGER
))
22368 /* Deduce it from the non-type argument. */
22369 tree atype
= TREE_TYPE (arg
);
22370 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22372 UNIFY_ALLOW_NONE
, explain_p
);
22375 /* Try again later. */
22376 return unify_success (explain_p
);
22379 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
22381 /* If ARG is a parameter pack or an expansion, we cannot unify
22382 against it unless PARM is also a parameter pack. */
22383 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
22384 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
22385 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
22388 bool removed_attr
= false;
22389 arg
= strip_typedefs_expr (arg
, &removed_attr
);
22391 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
22392 return unify_success (explain_p
);
22396 /* A pointer-to-member constant can be unified only with
22397 another constant. */
22398 if (TREE_CODE (arg
) != PTRMEM_CST
)
22399 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
22401 /* Just unify the class member. It would be useless (and possibly
22402 wrong, depending on the strict flags) to unify also
22403 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22404 arg refer to the same variable, even if through different
22405 classes. For instance:
22407 struct A { int x; };
22410 Unification of &A::x and &B::x must succeed. */
22411 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
22412 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
22417 if (!TYPE_PTR_P (arg
))
22418 return unify_type_mismatch (explain_p
, parm
, arg
);
22420 /* [temp.deduct.call]
22422 A can be another pointer or pointer to member type that can
22423 be converted to the deduced A via a qualification
22424 conversion (_conv.qual_).
22426 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22427 This will allow for additional cv-qualification of the
22428 pointed-to types if appropriate. */
22430 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
22431 /* The derived-to-base conversion only persists through one
22432 level of pointers. */
22433 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
22435 return unify (tparms
, targs
, TREE_TYPE (parm
),
22436 TREE_TYPE (arg
), strict
, explain_p
);
22439 case REFERENCE_TYPE
:
22440 if (!TYPE_REF_P (arg
))
22441 return unify_type_mismatch (explain_p
, parm
, arg
);
22442 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22443 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22446 if (TREE_CODE (arg
) != ARRAY_TYPE
)
22447 return unify_type_mismatch (explain_p
, parm
, arg
);
22448 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
22449 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
22450 return unify_type_mismatch (explain_p
, parm
, arg
);
22451 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22452 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
22453 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
22454 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
22455 TYPE_DOMAIN (arg
), explain_p
);
22456 return unify_success (explain_p
);
22463 case ENUMERAL_TYPE
:
22466 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22467 return unify_type_mismatch (explain_p
, parm
, arg
);
22469 /* We have already checked cv-qualification at the top of the
22471 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
22472 return unify_type_mismatch (explain_p
, parm
, arg
);
22474 /* As far as unification is concerned, this wins. Later checks
22475 will invalidate it if necessary. */
22476 return unify_success (explain_p
);
22478 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22479 /* Type INTEGER_CST can come from ordinary constant template args. */
22481 while (CONVERT_EXPR_P (arg
))
22482 arg
= TREE_OPERAND (arg
, 0);
22484 if (TREE_CODE (arg
) != INTEGER_CST
)
22485 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22486 return (tree_int_cst_equal (parm
, arg
)
22487 ? unify_success (explain_p
)
22488 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
22492 int i
, len
, argslen
;
22493 int parm_variadic_p
= 0;
22495 if (TREE_CODE (arg
) != TREE_VEC
)
22496 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22498 len
= TREE_VEC_LENGTH (parm
);
22499 argslen
= TREE_VEC_LENGTH (arg
);
22501 /* Check for pack expansions in the parameters. */
22502 for (i
= 0; i
< len
; ++i
)
22504 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
22507 /* We can unify against something with a trailing
22509 parm_variadic_p
= 1;
22511 /* [temp.deduct.type]/9: If the template argument list of
22512 P contains a pack expansion that is not the last
22513 template argument, the entire template argument list
22514 is a non-deduced context. */
22515 return unify_success (explain_p
);
22519 /* If we don't have enough arguments to satisfy the parameters
22520 (not counting the pack expression at the end), or we have
22521 too many arguments for a parameter list that doesn't end in
22522 a pack expression, we can't unify. */
22523 if (parm_variadic_p
22524 ? argslen
< len
- parm_variadic_p
22526 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
22528 /* Unify all of the parameters that precede the (optional)
22529 pack expression. */
22530 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
22532 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
22533 TREE_VEC_ELT (parm
, i
),
22534 TREE_VEC_ELT (arg
, i
),
22535 UNIFY_ALLOW_NONE
, explain_p
);
22537 if (parm_variadic_p
)
22538 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
22540 /*subr=*/true, explain_p
);
22541 return unify_success (explain_p
);
22546 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22547 return unify_type_mismatch (explain_p
, parm
, arg
);
22549 if (TYPE_PTRMEMFUNC_P (parm
))
22551 if (!TYPE_PTRMEMFUNC_P (arg
))
22552 return unify_type_mismatch (explain_p
, parm
, arg
);
22554 return unify (tparms
, targs
,
22555 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
22556 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
22557 strict
, explain_p
);
22559 else if (TYPE_PTRMEMFUNC_P (arg
))
22560 return unify_type_mismatch (explain_p
, parm
, arg
);
22562 if (CLASSTYPE_TEMPLATE_INFO (parm
))
22564 tree t
= NULL_TREE
;
22566 if (strict_in
& UNIFY_ALLOW_DERIVED
)
22568 /* First, we try to unify the PARM and ARG directly. */
22569 t
= try_class_unification (tparms
, targs
,
22570 parm
, arg
, explain_p
);
22574 /* Fallback to the special case allowed in
22575 [temp.deduct.call]:
22577 If P is a class, and P has the form
22578 template-id, then A can be a derived class of
22579 the deduced A. Likewise, if P is a pointer to
22580 a class of the form template-id, A can be a
22581 pointer to a derived class pointed to by the
22583 enum template_base_result r
;
22584 r
= get_template_base (tparms
, targs
, parm
, arg
,
22589 /* Don't give the derived diagnostic if we're
22590 already dealing with the same template. */
22592 = (CLASSTYPE_TEMPLATE_INFO (arg
)
22593 && (CLASSTYPE_TI_TEMPLATE (parm
)
22594 == CLASSTYPE_TI_TEMPLATE (arg
)));
22595 return unify_no_common_base (explain_p
&& !same_template
,
22600 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
22601 && (CLASSTYPE_TI_TEMPLATE (parm
)
22602 == CLASSTYPE_TI_TEMPLATE (arg
)))
22603 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22604 Then, we should unify `int' and `U'. */
22607 /* There's no chance of unification succeeding. */
22608 return unify_type_mismatch (explain_p
, parm
, arg
);
22610 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
22611 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
22613 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
22614 return unify_type_mismatch (explain_p
, parm
, arg
);
22615 return unify_success (explain_p
);
22618 case FUNCTION_TYPE
:
22620 unsigned int nargs
;
22625 if (TREE_CODE (arg
) != TREE_CODE (parm
))
22626 return unify_type_mismatch (explain_p
, parm
, arg
);
22628 /* CV qualifications for methods can never be deduced, they must
22629 match exactly. We need to check them explicitly here,
22630 because type_unification_real treats them as any other
22631 cv-qualified parameter. */
22632 if (TREE_CODE (parm
) == METHOD_TYPE
22633 && (!check_cv_quals_for_unify
22635 class_of_this_parm (arg
),
22636 class_of_this_parm (parm
))))
22637 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22638 if (TREE_CODE (arg
) == FUNCTION_TYPE
22639 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
22640 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22641 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
22642 return unify_type_mismatch (explain_p
, parm
, arg
);
22644 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
22645 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
22647 nargs
= list_length (TYPE_ARG_TYPES (arg
));
22648 args
= XALLOCAVEC (tree
, nargs
);
22649 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
22650 a
!= NULL_TREE
&& a
!= void_list_node
;
22651 a
= TREE_CHAIN (a
), ++i
)
22652 args
[i
] = TREE_VALUE (a
);
22655 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
22656 args
, nargs
, 1, DEDUCE_EXACT
,
22660 if (flag_noexcept_type
)
22662 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
22663 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
22664 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
22665 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
22666 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
22667 && uses_template_parms (TREE_PURPOSE (pspec
)))
22668 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
22669 TREE_PURPOSE (aspec
),
22670 UNIFY_ALLOW_NONE
, explain_p
);
22671 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
22672 return unify_type_mismatch (explain_p
, parm
, arg
);
22679 /* Unify a pointer to member with a pointer to member function, which
22680 deduces the type of the member as a function type. */
22681 if (TYPE_PTRMEMFUNC_P (arg
))
22683 /* Check top-level cv qualifiers */
22684 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
22685 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
22687 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22688 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
22689 UNIFY_ALLOW_NONE
, explain_p
);
22691 /* Determine the type of the function we are unifying against. */
22692 tree fntype
= static_fn_type (arg
);
22694 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
22697 if (TREE_CODE (arg
) != OFFSET_TYPE
)
22698 return unify_type_mismatch (explain_p
, parm
, arg
);
22699 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
22700 TYPE_OFFSET_BASETYPE (arg
),
22701 UNIFY_ALLOW_NONE
, explain_p
);
22702 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
22703 strict
, explain_p
);
22706 if (DECL_TEMPLATE_PARM_P (parm
))
22707 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
22708 if (arg
!= scalar_constant_value (parm
))
22709 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22710 return unify_success (explain_p
);
22713 case TEMPLATE_DECL
:
22714 /* Matched cases are handled by the ARG == PARM test above. */
22715 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
22718 /* We might get a variable as a non-type template argument in parm if the
22719 corresponding parameter is type-dependent. Make any necessary
22720 adjustments based on whether arg is a reference. */
22721 if (CONSTANT_CLASS_P (arg
))
22722 parm
= fold_non_dependent_expr (parm
, complain
);
22723 else if (REFERENCE_REF_P (arg
))
22725 tree sub
= TREE_OPERAND (arg
, 0);
22727 if (TREE_CODE (sub
) == ADDR_EXPR
)
22728 arg
= TREE_OPERAND (sub
, 0);
22730 /* Now use the normal expression code to check whether they match. */
22733 case TYPE_ARGUMENT_PACK
:
22734 case NONTYPE_ARGUMENT_PACK
:
22735 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
22736 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
22739 case DECLTYPE_TYPE
:
22740 case UNDERLYING_TYPE
:
22741 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22742 or UNDERLYING_TYPE nodes. */
22743 return unify_success (explain_p
);
22746 /* Unification fails if we hit an error node. */
22747 return unify_invalid (explain_p
);
22750 if (REFERENCE_REF_P (parm
))
22752 bool pexp
= PACK_EXPANSION_P (arg
);
22754 arg
= PACK_EXPANSION_PATTERN (arg
);
22755 if (REFERENCE_REF_P (arg
))
22756 arg
= TREE_OPERAND (arg
, 0);
22758 arg
= make_pack_expansion (arg
, complain
);
22759 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
22760 strict
, explain_p
);
22765 /* An unresolved overload is a nondeduced context. */
22766 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
22767 return unify_success (explain_p
);
22768 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
22770 /* We must be looking at an expression. This can happen with
22774 void foo(S<I>, S<I + 2>);
22776 This is a "nondeduced context":
22780 The nondeduced contexts are:
22782 --A type that is a template-id in which one or more of
22783 the template-arguments is an expression that references
22784 a template-parameter.
22786 In these cases, we assume deduction succeeded, but don't
22787 actually infer any unifications. */
22789 if (!uses_template_parms (parm
)
22790 && !template_args_equal (parm
, arg
))
22791 return unify_expression_unequal (explain_p
, parm
, arg
);
22793 return unify_success (explain_p
);
22796 #undef RECUR_AND_CHECK_FAILURE
22798 /* Note that DECL can be defined in this translation unit, if
22802 mark_definable (tree decl
)
22805 DECL_NOT_REALLY_EXTERN (decl
) = 1;
22806 FOR_EACH_CLONE (clone
, decl
)
22807 DECL_NOT_REALLY_EXTERN (clone
) = 1;
22810 /* Called if RESULT is explicitly instantiated, or is a member of an
22811 explicitly instantiated class. */
22814 mark_decl_instantiated (tree result
, int extern_p
)
22816 SET_DECL_EXPLICIT_INSTANTIATION (result
);
22818 /* If this entity has already been written out, it's too late to
22819 make any modifications. */
22820 if (TREE_ASM_WRITTEN (result
))
22823 /* For anonymous namespace we don't need to do anything. */
22824 if (decl_anon_ns_mem_p (result
))
22826 gcc_assert (!TREE_PUBLIC (result
));
22830 if (TREE_CODE (result
) != FUNCTION_DECL
)
22831 /* The TREE_PUBLIC flag for function declarations will have been
22832 set correctly by tsubst. */
22833 TREE_PUBLIC (result
) = 1;
22835 /* This might have been set by an earlier implicit instantiation. */
22836 DECL_COMDAT (result
) = 0;
22839 DECL_NOT_REALLY_EXTERN (result
) = 0;
22842 mark_definable (result
);
22843 mark_needed (result
);
22844 /* Always make artificials weak. */
22845 if (DECL_ARTIFICIAL (result
) && flag_weak
)
22846 comdat_linkage (result
);
22847 /* For WIN32 we also want to put explicit instantiations in
22848 linkonce sections. */
22849 else if (TREE_PUBLIC (result
))
22850 maybe_make_one_only (result
);
22851 if (TREE_CODE (result
) == FUNCTION_DECL
22852 && DECL_TEMPLATE_INSTANTIATED (result
))
22853 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22854 since start_preparsed_function wouldn't have if we had an earlier
22855 extern explicit instantiation. */
22856 DECL_EXTERNAL (result
) = 0;
22859 /* If EXTERN_P, then this function will not be emitted -- unless
22860 followed by an explicit instantiation, at which point its linkage
22861 will be adjusted. If !EXTERN_P, then this function will be
22862 emitted here. In neither circumstance do we want
22863 import_export_decl to adjust the linkage. */
22864 DECL_INTERFACE_KNOWN (result
) = 1;
22867 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22868 important template arguments. If any are missing, we check whether
22869 they're important by using error_mark_node for substituting into any
22870 args that were used for partial ordering (the ones between ARGS and END)
22871 and seeing if it bubbles up. */
22874 check_undeduced_parms (tree targs
, tree args
, tree end
)
22876 bool found
= false;
22878 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
22879 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
22882 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
22886 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
22887 if (substed
== error_mark_node
)
22893 /* Given two function templates PAT1 and PAT2, return:
22895 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22896 -1 if PAT2 is more specialized than PAT1.
22897 0 if neither is more specialized.
22899 LEN indicates the number of parameters we should consider
22900 (defaulted parameters should not be considered).
22902 The 1998 std underspecified function template partial ordering, and
22903 DR214 addresses the issue. We take pairs of arguments, one from
22904 each of the templates, and deduce them against each other. One of
22905 the templates will be more specialized if all the *other*
22906 template's arguments deduce against its arguments and at least one
22907 of its arguments *does* *not* deduce against the other template's
22908 corresponding argument. Deduction is done as for class templates.
22909 The arguments used in deduction have reference and top level cv
22910 qualifiers removed. Iff both arguments were originally reference
22911 types *and* deduction succeeds in both directions, an lvalue reference
22912 wins against an rvalue reference and otherwise the template
22913 with the more cv-qualified argument wins for that pairing (if
22914 neither is more cv-qualified, they both are equal). Unlike regular
22915 deduction, after all the arguments have been deduced in this way,
22916 we do *not* verify the deduced template argument values can be
22917 substituted into non-deduced contexts.
22919 The logic can be a bit confusing here, because we look at deduce1 and
22920 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22921 can find template arguments for pat1 to make arg1 look like arg2, that
22922 means that arg2 is at least as specialized as arg1. */
22925 more_specialized_fn (tree pat1
, tree pat2
, int len
)
22927 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
22928 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
22929 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
22930 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
22931 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
22932 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
22933 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
22934 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
22935 tree origs1
, origs2
;
22936 bool lose1
= false;
22937 bool lose2
= false;
22939 /* Remove the this parameter from non-static member functions. If
22940 one is a non-static member function and the other is not a static
22941 member function, remove the first parameter from that function
22942 also. This situation occurs for operator functions where we
22943 locate both a member function (with this pointer) and non-member
22944 operator (with explicit first operand). */
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
22947 len
--; /* LEN is the number of significant arguments for DECL1 */
22948 args1
= TREE_CHAIN (args1
);
22949 if (!DECL_STATIC_FUNCTION_P (decl2
))
22950 args2
= TREE_CHAIN (args2
);
22952 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
22954 args2
= TREE_CHAIN (args2
);
22955 if (!DECL_STATIC_FUNCTION_P (decl1
))
22958 args1
= TREE_CHAIN (args1
);
22962 /* If only one is a conversion operator, they are unordered. */
22963 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
22966 /* Consider the return type for a conversion function */
22967 if (DECL_CONV_FN_P (decl1
))
22969 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
22970 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
22974 processing_template_decl
++;
22980 /* Stop when an ellipsis is seen. */
22981 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
22983 tree arg1
= TREE_VALUE (args1
);
22984 tree arg2
= TREE_VALUE (args2
);
22985 int deduce1
, deduce2
;
22991 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
22992 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
22994 /* When both arguments are pack expansions, we need only
22995 unify the patterns themselves. */
22996 arg1
= PACK_EXPANSION_PATTERN (arg1
);
22997 arg2
= PACK_EXPANSION_PATTERN (arg2
);
22999 /* This is the last comparison we need to do. */
23003 /* DR 1847: If a particular P contains no template-parameters that
23004 participate in template argument deduction, that P is not used to
23005 determine the ordering. */
23006 if (!uses_deducible_template_parms (arg1
)
23007 && !uses_deducible_template_parms (arg2
))
23010 if (TYPE_REF_P (arg1
))
23012 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
23013 arg1
= TREE_TYPE (arg1
);
23014 quals1
= cp_type_quals (arg1
);
23017 if (TYPE_REF_P (arg2
))
23019 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
23020 arg2
= TREE_TYPE (arg2
);
23021 quals2
= cp_type_quals (arg2
);
23024 arg1
= TYPE_MAIN_VARIANT (arg1
);
23025 arg2
= TYPE_MAIN_VARIANT (arg2
);
23027 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
23029 int i
, len2
= remaining_arguments (args2
);
23030 tree parmvec
= make_tree_vec (1);
23031 tree argvec
= make_tree_vec (len2
);
23034 /* Setup the parameter vector, which contains only ARG1. */
23035 TREE_VEC_ELT (parmvec
, 0) = arg1
;
23037 /* Setup the argument vector, which contains the remaining
23039 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
23040 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23042 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
23043 argvec
, DEDUCE_EXACT
,
23044 /*subr=*/true, /*explain_p=*/false)
23047 /* We cannot deduce in the other direction, because ARG1 is
23048 a pack expansion but ARG2 is not. */
23051 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23053 int i
, len1
= remaining_arguments (args1
);
23054 tree parmvec
= make_tree_vec (1);
23055 tree argvec
= make_tree_vec (len1
);
23058 /* Setup the parameter vector, which contains only ARG1. */
23059 TREE_VEC_ELT (parmvec
, 0) = arg2
;
23061 /* Setup the argument vector, which contains the remaining
23063 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
23064 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
23066 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
23067 argvec
, DEDUCE_EXACT
,
23068 /*subr=*/true, /*explain_p=*/false)
23071 /* We cannot deduce in the other direction, because ARG2 is
23072 a pack expansion but ARG1 is not.*/
23078 /* The normal case, where neither argument is a pack
23080 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
23081 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23083 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
23084 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
23088 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23089 arg2, then arg2 is not as specialized as arg1. */
23095 /* "If, for a given type, deduction succeeds in both directions
23096 (i.e., the types are identical after the transformations above)
23097 and both P and A were reference types (before being replaced with
23098 the type referred to above):
23099 - if the type from the argument template was an lvalue reference and
23100 the type from the parameter template was not, the argument type is
23101 considered to be more specialized than the other; otherwise,
23102 - if the type from the argument template is more cv-qualified
23103 than the type from the parameter template (as described above),
23104 the argument type is considered to be more specialized than the other;
23106 - neither type is more specialized than the other." */
23108 if (deduce1
&& deduce2
)
23110 if (ref1
&& ref2
&& ref1
!= ref2
)
23117 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
23119 if ((quals1
& quals2
) == quals2
)
23121 if ((quals1
& quals2
) == quals1
)
23126 if (lose1
&& lose2
)
23127 /* We've failed to deduce something in either direction.
23128 These must be unordered. */
23133 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
23134 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
23135 /* We have already processed all of the arguments in our
23136 handing of the pack expansion type. */
23139 args1
= TREE_CHAIN (args1
);
23140 args2
= TREE_CHAIN (args2
);
23143 /* "In most cases, all template parameters must have values in order for
23144 deduction to succeed, but for partial ordering purposes a template
23145 parameter may remain without a value provided it is not used in the
23146 types being used for partial ordering."
23148 Thus, if we are missing any of the targs1 we need to substitute into
23149 origs1, then pat2 is not as specialized as pat1. This can happen when
23150 there is a nondeduced context. */
23151 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
23153 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
23156 processing_template_decl
--;
23158 /* If both deductions succeed, the partial ordering selects the more
23159 constrained template. */
23160 if (!lose1
&& !lose2
)
23162 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
23163 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
23164 lose1
= !subsumes_constraints (c1
, c2
);
23165 lose2
= !subsumes_constraints (c2
, c1
);
23168 /* All things being equal, if the next argument is a pack expansion
23169 for one function but not for the other, prefer the
23170 non-variadic function. FIXME this is bogus; see c++/41958. */
23172 && args1
&& TREE_VALUE (args1
)
23173 && args2
&& TREE_VALUE (args2
))
23175 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
23176 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
23179 if (lose1
== lose2
)
23187 /* Determine which of two partial specializations of TMPL is more
23190 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23191 to the first partial specialization. The TREE_PURPOSE is the
23192 innermost set of template parameters for the partial
23193 specialization. PAT2 is similar, but for the second template.
23195 Return 1 if the first partial specialization is more specialized;
23196 -1 if the second is more specialized; 0 if neither is more
23199 See [temp.class.order] for information about determining which of
23200 two templates is more specialized. */
23203 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
23207 bool any_deductions
= false;
23209 tree tmpl1
= TREE_VALUE (pat1
);
23210 tree tmpl2
= TREE_VALUE (pat2
);
23211 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
23212 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
23214 /* Just like what happens for functions, if we are ordering between
23215 different template specializations, we may encounter dependent
23216 types in the arguments, and we need our dependency check functions
23217 to behave correctly. */
23218 ++processing_template_decl
;
23219 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
23223 any_deductions
= true;
23226 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
23230 any_deductions
= true;
23232 --processing_template_decl
;
23234 /* If both deductions succeed, the partial ordering selects the more
23235 constrained template. */
23236 if (!winner
&& any_deductions
)
23237 return more_constrained (tmpl1
, tmpl2
);
23239 /* In the case of a tie where at least one of the templates
23240 has a parameter pack at the end, the template with the most
23241 non-packed parameters wins. */
23244 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
23245 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
23247 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
23248 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
23249 int len1
= TREE_VEC_LENGTH (args1
);
23250 int len2
= TREE_VEC_LENGTH (args2
);
23252 /* We don't count the pack expansion at the end. */
23253 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
23255 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
23260 else if (len1
< len2
)
23267 /* Return the template arguments that will produce the function signature
23268 DECL from the function template FN, with the explicit template
23269 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23270 also match. Return NULL_TREE if no satisfactory arguments could be
23274 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
23276 int ntparms
= DECL_NTPARMS (fn
);
23277 tree targs
= make_tree_vec (ntparms
);
23278 tree decl_type
= TREE_TYPE (decl
);
23279 tree decl_arg_types
;
23281 unsigned int nargs
, ix
;
23284 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
23286 /* Never do unification on the 'this' parameter. */
23287 decl_arg_types
= skip_artificial_parms_for (decl
,
23288 TYPE_ARG_TYPES (decl_type
));
23290 nargs
= list_length (decl_arg_types
);
23291 args
= XALLOCAVEC (tree
, nargs
);
23292 for (arg
= decl_arg_types
, ix
= 0;
23293 arg
!= NULL_TREE
&& arg
!= void_list_node
;
23294 arg
= TREE_CHAIN (arg
), ++ix
)
23295 args
[ix
] = TREE_VALUE (arg
);
23297 if (fn_type_unification (fn
, explicit_args
, targs
,
23299 (check_rettype
|| DECL_CONV_FN_P (fn
)
23300 ? TREE_TYPE (decl_type
) : NULL_TREE
),
23301 DEDUCE_EXACT
, LOOKUP_NORMAL
, NULL
,
23302 /*explain_p=*/false,
23304 == error_mark_node
)
23310 /* Return the innermost template arguments that, when applied to a partial
23311 specialization SPEC_TMPL of TMPL, yield the ARGS.
23313 For example, suppose we have:
23315 template <class T, class U> struct S {};
23316 template <class T> struct S<T*, int> {};
23318 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23319 partial specialization and the ARGS will be {double*, int}. The resulting
23320 vector will be {double}, indicating that `T' is bound to `double'. */
23323 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
23325 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
23327 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
23328 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
23330 tree innermost_deduced_args
;
23332 innermost_deduced_args
= make_tree_vec (ntparms
);
23333 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23335 deduced_args
= copy_node (args
);
23336 SET_TMPL_ARGS_LEVEL (deduced_args
,
23337 TMPL_ARGS_DEPTH (deduced_args
),
23338 innermost_deduced_args
);
23341 deduced_args
= innermost_deduced_args
;
23343 bool tried_array_deduction
= (cxx_dialect
< cxx17
);
23345 if (unify (tparms
, deduced_args
,
23346 INNERMOST_TEMPLATE_ARGS (spec_args
),
23347 INNERMOST_TEMPLATE_ARGS (args
),
23348 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
23351 for (i
= 0; i
< ntparms
; ++i
)
23352 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
23354 if (!tried_array_deduction
)
23356 try_array_deduction (tparms
, innermost_deduced_args
,
23357 INNERMOST_TEMPLATE_ARGS (spec_args
));
23358 tried_array_deduction
= true;
23359 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
23365 if (!push_tinst_level (spec_tmpl
, deduced_args
))
23367 excessive_deduction_depth
= true;
23371 /* Verify that nondeduced template arguments agree with the type
23372 obtained from argument deduction.
23376 struct A { typedef int X; };
23377 template <class T, class U> struct C {};
23378 template <class T> struct C<T, typename T::X> {};
23380 Then with the instantiation `C<A, int>', we can deduce that
23381 `T' is `A' but unify () does not check whether `typename T::X'
23383 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
23385 if (spec_args
!= error_mark_node
)
23386 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
23387 INNERMOST_TEMPLATE_ARGS (spec_args
),
23388 tmpl
, tf_none
, false, false);
23390 pop_tinst_level ();
23392 if (spec_args
== error_mark_node
23393 /* We only need to check the innermost arguments; the other
23394 arguments will always agree. */
23395 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
23396 INNERMOST_TEMPLATE_ARGS (args
)))
23399 /* Now that we have bindings for all of the template arguments,
23400 ensure that the arguments deduced for the template template
23401 parameters have compatible template parameter lists. See the use
23402 of template_template_parm_bindings_ok_p in fn_type_unification
23403 for more information. */
23404 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
23407 return deduced_args
;
23410 // Compare two function templates T1 and T2 by deducing bindings
23411 // from one against the other. If both deductions succeed, compare
23412 // constraints to see which is more constrained.
23414 more_specialized_inst (tree t1
, tree t2
)
23419 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
23425 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
23431 // If both deductions succeed, then one may be more constrained.
23432 if (count
== 2 && fate
== 0)
23433 fate
= more_constrained (t1
, t2
);
23438 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23439 Return the TREE_LIST node with the most specialized template, if
23440 any. If there is no most specialized template, the error_mark_node
23443 Note that this function does not look at, or modify, the
23444 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23445 returned is one of the elements of INSTANTIATIONS, callers may
23446 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23447 and retrieve it from the value returned. */
23450 most_specialized_instantiation (tree templates
)
23454 ++processing_template_decl
;
23457 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
23459 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
23460 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
23465 /* Equally specialized, move to next function. If there
23466 is no next function, nothing's most specialized. */
23467 fn
= TREE_CHAIN (fn
);
23475 /* Now verify that champ is better than everything earlier in the
23476 instantiation list. */
23477 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
23478 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
23485 processing_template_decl
--;
23488 return error_mark_node
;
23493 /* If DECL is a specialization of some template, return the most
23494 general such template. Otherwise, returns NULL_TREE.
23496 For example, given:
23498 template <class T> struct S { template <class U> void f(U); };
23500 if TMPL is `template <class U> void S<int>::f(U)' this will return
23501 the full template. This function will not trace past partial
23502 specializations, however. For example, given in addition:
23504 template <class T> struct S<T*> { template <class U> void f(U); };
23506 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23507 `template <class T> template <class U> S<T*>::f(U)'. */
23510 most_general_template (tree decl
)
23512 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23514 if (tree tinfo
= get_template_info (decl
))
23515 decl
= TI_TEMPLATE (tinfo
);
23516 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23517 template friend, or a FIELD_DECL for a capture pack. */
23518 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
23522 /* Look for more and more general templates. */
23523 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
23525 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23526 (See cp-tree.h for details.) */
23527 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
23530 if (CLASS_TYPE_P (TREE_TYPE (decl
))
23531 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
23532 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
23535 /* Stop if we run into an explicitly specialized class template. */
23536 if (!DECL_NAMESPACE_SCOPE_P (decl
)
23537 && DECL_CONTEXT (decl
)
23538 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
23541 decl
= DECL_TI_TEMPLATE (decl
);
23547 /* Return the most specialized of the template partial specializations
23548 which can produce TARGET, a specialization of some class or variable
23549 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23550 a TEMPLATE_DECL node corresponding to the partial specialization, while
23551 the TREE_PURPOSE is the set of template arguments that must be
23552 substituted into the template pattern in order to generate TARGET.
23554 If the choice of partial specialization is ambiguous, a diagnostic
23555 is issued, and the error_mark_node is returned. If there are no
23556 partial specializations matching TARGET, then NULL_TREE is
23557 returned, indicating that the primary template should be used. */
23560 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
23562 tree list
= NULL_TREE
;
23567 tree outer_args
= NULL_TREE
;
23570 if (TYPE_P (target
))
23572 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
23573 tmpl
= TI_TEMPLATE (tinfo
);
23574 args
= TI_ARGS (tinfo
);
23576 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
23578 tmpl
= TREE_OPERAND (target
, 0);
23579 args
= TREE_OPERAND (target
, 1);
23581 else if (VAR_P (target
))
23583 tree tinfo
= DECL_TEMPLATE_INFO (target
);
23584 tmpl
= TI_TEMPLATE (tinfo
);
23585 args
= TI_ARGS (tinfo
);
23588 gcc_unreachable ();
23590 tree main_tmpl
= most_general_template (tmpl
);
23592 /* For determining which partial specialization to use, only the
23593 innermost args are interesting. */
23594 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
23596 outer_args
= strip_innermost_template_args (args
, 1);
23597 args
= INNERMOST_TEMPLATE_ARGS (args
);
23600 /* The caller hasn't called push_to_top_level yet, but we need
23601 get_partial_spec_bindings to be done in non-template context so that we'll
23602 fully resolve everything. */
23603 processing_template_decl_sentinel ptds
;
23605 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
23608 tree spec_tmpl
= TREE_VALUE (t
);
23612 /* Substitute in the template args from the enclosing class. */
23613 ++processing_template_decl
;
23614 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
23615 --processing_template_decl
;
23618 if (spec_tmpl
== error_mark_node
)
23619 return error_mark_node
;
23621 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
23625 spec_args
= add_to_template_args (outer_args
, spec_args
);
23627 /* Keep the candidate only if the constraints are satisfied,
23628 or if we're not compiling with concepts. */
23630 || constraints_satisfied_p (spec_tmpl
, spec_args
))
23632 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
23633 TREE_TYPE (list
) = TREE_TYPE (t
);
23641 ambiguous_p
= false;
23644 t
= TREE_CHAIN (t
);
23645 for (; t
; t
= TREE_CHAIN (t
))
23647 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23654 t
= TREE_CHAIN (t
);
23657 ambiguous_p
= true;
23666 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
23668 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
23671 ambiguous_p
= true;
23679 char *spaces
= NULL
;
23680 if (!(complain
& tf_error
))
23681 return error_mark_node
;
23682 if (TYPE_P (target
))
23683 error ("ambiguous template instantiation for %q#T", target
);
23685 error ("ambiguous template instantiation for %q#D", target
);
23686 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
23687 for (t
= list
; t
; t
= TREE_CHAIN (t
))
23689 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
23690 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
23691 "%s %#qS", spaces
? spaces
: str
, subst
);
23692 spaces
= spaces
? spaces
: get_spaces (str
);
23695 return error_mark_node
;
23701 /* Explicitly instantiate DECL. */
23704 do_decl_instantiation (tree decl
, tree storage
)
23706 tree result
= NULL_TREE
;
23709 if (!decl
|| decl
== error_mark_node
)
23710 /* An error occurred, for which grokdeclarator has already issued
23711 an appropriate message. */
23713 else if (! DECL_LANG_SPECIFIC (decl
))
23715 error ("explicit instantiation of non-template %q#D", decl
);
23718 else if (DECL_DECLARED_CONCEPT_P (decl
))
23721 error ("explicit instantiation of variable concept %q#D", decl
);
23723 error ("explicit instantiation of function concept %q#D", decl
);
23727 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
23728 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
23730 if (VAR_P (decl
) && !var_templ
)
23732 /* There is an asymmetry here in the way VAR_DECLs and
23733 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23734 the latter, the DECL we get back will be marked as a
23735 template instantiation, and the appropriate
23736 DECL_TEMPLATE_INFO will be set up. This does not happen for
23737 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23738 should handle VAR_DECLs as it currently handles
23740 if (!DECL_CLASS_SCOPE_P (decl
))
23742 error ("%qD is not a static data member of a class template", decl
);
23745 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
23746 if (!result
|| !VAR_P (result
))
23748 error ("no matching template for %qD found", decl
);
23751 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
23753 error ("type %qT for explicit instantiation %qD does not match "
23754 "declared type %qT", TREE_TYPE (result
), decl
,
23759 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
23761 error ("explicit instantiation of %q#D", decl
);
23767 /* Check for various error cases. Note that if the explicit
23768 instantiation is valid the RESULT will currently be marked as an
23769 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23770 until we get here. */
23772 if (DECL_TEMPLATE_SPECIALIZATION (result
))
23774 /* DR 259 [temp.spec].
23776 Both an explicit instantiation and a declaration of an explicit
23777 specialization shall not appear in a program unless the explicit
23778 instantiation follows a declaration of the explicit specialization.
23780 For a given set of template parameters, if an explicit
23781 instantiation of a template appears after a declaration of an
23782 explicit specialization for that template, the explicit
23783 instantiation has no effect. */
23786 else if (DECL_EXPLICIT_INSTANTIATION (result
))
23790 No program shall explicitly instantiate any template more
23793 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23794 the first instantiation was `extern' and the second is not,
23795 and EXTERN_P for the opposite case. */
23796 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
23797 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
23798 /* If an "extern" explicit instantiation follows an ordinary
23799 explicit instantiation, the template is instantiated. */
23803 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
23805 error ("no matching template for %qD found", result
);
23808 else if (!DECL_TEMPLATE_INFO (result
))
23810 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
23814 if (storage
== NULL_TREE
)
23816 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23818 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
23819 pedwarn (input_location
, OPT_Wpedantic
,
23820 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23825 error ("storage class %qD applied to template instantiation", storage
);
23827 check_explicit_instantiation_namespace (result
);
23828 mark_decl_instantiated (result
, extern_p
);
23830 instantiate_decl (result
, /*defer_ok=*/true,
23831 /*expl_inst_class_mem_p=*/false);
23835 mark_class_instantiated (tree t
, int extern_p
)
23837 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
23838 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
23839 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
23840 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
23843 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
23844 rest_of_type_compilation (t
, 1);
23848 /* Called from do_type_instantiation through binding_table_foreach to
23849 do recursive instantiation for the type bound in ENTRY. */
23851 bt_instantiate_type_proc (binding_entry entry
, void *data
)
23853 tree storage
= *(tree
*) data
;
23855 if (MAYBE_CLASS_TYPE_P (entry
->type
)
23856 && CLASSTYPE_TEMPLATE_INFO (entry
->type
)
23857 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
23858 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
23861 /* Perform an explicit instantiation of template class T. STORAGE, if
23862 non-null, is the RID for extern, inline or static. COMPLAIN is
23863 nonzero if this is called from the parser, zero if called recursively,
23864 since the standard is unclear (as detailed below). */
23867 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
23872 int previous_instantiation_extern_p
= 0;
23874 if (TREE_CODE (t
) == TYPE_DECL
)
23877 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
23880 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
23882 error ("explicit instantiation of non-class template %qD", tmpl
);
23884 error ("explicit instantiation of non-template type %qT", t
);
23890 if (!COMPLETE_TYPE_P (t
))
23892 if (complain
& tf_error
)
23893 error ("explicit instantiation of %q#T before definition of template",
23898 if (storage
!= NULL_TREE
)
23900 if (!in_system_header_at (input_location
))
23902 if (storage
== ridpointers
[(int) RID_EXTERN
])
23904 if (cxx_dialect
== cxx98
)
23905 pedwarn (input_location
, OPT_Wpedantic
,
23906 "ISO C++ 1998 forbids the use of %<extern%> on "
23907 "explicit instantiations");
23910 pedwarn (input_location
, OPT_Wpedantic
,
23911 "ISO C++ forbids the use of %qE"
23912 " on explicit instantiations", storage
);
23915 if (storage
== ridpointers
[(int) RID_INLINE
])
23917 else if (storage
== ridpointers
[(int) RID_EXTERN
])
23919 else if (storage
== ridpointers
[(int) RID_STATIC
])
23923 error ("storage class %qD applied to template instantiation",
23929 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
23931 /* DR 259 [temp.spec].
23933 Both an explicit instantiation and a declaration of an explicit
23934 specialization shall not appear in a program unless the explicit
23935 instantiation follows a declaration of the explicit specialization.
23937 For a given set of template parameters, if an explicit
23938 instantiation of a template appears after a declaration of an
23939 explicit specialization for that template, the explicit
23940 instantiation has no effect. */
23943 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
23947 No program shall explicitly instantiate any template more
23950 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23951 instantiation was `extern'. If EXTERN_P then the second is.
23952 These cases are OK. */
23953 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
23955 if (!previous_instantiation_extern_p
&& !extern_p
23956 && (complain
& tf_error
))
23957 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
23959 /* If we've already instantiated the template, just return now. */
23960 if (!CLASSTYPE_INTERFACE_ONLY (t
))
23964 check_explicit_instantiation_namespace (TYPE_NAME (t
));
23965 mark_class_instantiated (t
, extern_p
);
23970 /* In contrast to implicit instantiation, where only the
23971 declarations, and not the definitions, of members are
23972 instantiated, we have here:
23976 The explicit instantiation of a class template specialization
23977 implies the instantiation of all of its members not
23978 previously explicitly specialized in the translation unit
23979 containing the explicit instantiation.
23981 Of course, we can't instantiate member template classes, since we
23982 don't have any arguments for them. Note that the standard is
23983 unclear on whether the instantiation of the members are
23984 *explicit* instantiations or not. However, the most natural
23985 interpretation is that it should be an explicit
23987 for (tree fld
= TYPE_FIELDS (t
); fld
; fld
= DECL_CHAIN (fld
))
23989 || (TREE_CODE (fld
) == FUNCTION_DECL
23991 && user_provided_p (fld
)))
23992 && DECL_TEMPLATE_INSTANTIATION (fld
))
23994 mark_decl_instantiated (fld
, extern_p
);
23996 instantiate_decl (fld
, /*defer_ok=*/true,
23997 /*expl_inst_class_mem_p=*/true);
24000 if (CLASSTYPE_NESTED_UTDS (t
))
24001 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
24002 bt_instantiate_type_proc
, &storage
);
24005 /* Given a function DECL, which is a specialization of TMPL, modify
24006 DECL to be a re-instantiation of TMPL with the same template
24007 arguments. TMPL should be the template into which tsubst'ing
24008 should occur for DECL, not the most general template.
24010 One reason for doing this is a scenario like this:
24013 void f(const T&, int i);
24015 void g() { f(3, 7); }
24018 void f(const T& t, const int i) { }
24020 Note that when the template is first instantiated, with
24021 instantiate_template, the resulting DECL will have no name for the
24022 first parameter, and the wrong type for the second. So, when we go
24023 to instantiate the DECL, we regenerate it. */
24026 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
24028 /* The arguments used to instantiate DECL, from the most general
24032 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
24034 /* Make sure that we can see identifiers, and compute access
24036 push_access_scope (decl
);
24038 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24046 args_depth
= TMPL_ARGS_DEPTH (args
);
24047 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
24048 if (args_depth
> parms_depth
)
24049 args
= get_innermost_template_args (args
, parms_depth
);
24051 /* Instantiate a dynamic exception-specification. noexcept will be
24053 if (tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern
)))
24054 if (TREE_VALUE (raises
))
24056 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
24057 args
, tf_error
, NULL_TREE
,
24058 /*defer_ok*/false);
24059 if (specs
&& specs
!= error_mark_node
)
24060 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
24064 /* Merge parameter declarations. */
24065 decl_parm
= skip_artificial_parms_for (decl
,
24066 DECL_ARGUMENTS (decl
));
24068 = skip_artificial_parms_for (code_pattern
,
24069 DECL_ARGUMENTS (code_pattern
));
24070 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
24075 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24076 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
24077 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
24079 parm_type
= type_decays_to (parm_type
);
24080 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24081 TREE_TYPE (decl_parm
) = parm_type
;
24082 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24083 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24085 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24086 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24088 decl_parm
= DECL_CHAIN (decl_parm
);
24089 pattern_parm
= DECL_CHAIN (pattern_parm
);
24091 /* Merge any parameters that match with the function parameter
24093 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
24096 tree expanded_types
;
24097 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24098 the parameters in this function parameter pack. */
24099 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
24100 args
, tf_error
, NULL_TREE
);
24101 len
= TREE_VEC_LENGTH (expanded_types
);
24102 for (i
= 0; i
< len
; i
++)
24107 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
24108 /* Rename the parameter to include the index. */
24109 DECL_NAME (decl_parm
) =
24110 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
24111 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
24112 parm_type
= type_decays_to (parm_type
);
24113 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
24114 TREE_TYPE (decl_parm
) = parm_type
;
24115 attributes
= DECL_ATTRIBUTES (pattern_parm
);
24116 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
24118 DECL_ATTRIBUTES (decl_parm
) = attributes
;
24119 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
24121 decl_parm
= DECL_CHAIN (decl_parm
);
24124 /* Merge additional specifiers from the CODE_PATTERN. */
24125 if (DECL_DECLARED_INLINE_P (code_pattern
)
24126 && !DECL_DECLARED_INLINE_P (decl
))
24127 DECL_DECLARED_INLINE_P (decl
) = 1;
24129 maybe_instantiate_noexcept (decl
, tf_error
);
24131 else if (VAR_P (decl
))
24133 start_lambda_scope (decl
);
24134 DECL_INITIAL (decl
) =
24135 tsubst_init (DECL_INITIAL (code_pattern
), decl
, args
,
24136 tf_error
, DECL_TI_TEMPLATE (decl
));
24137 finish_lambda_scope ();
24138 if (VAR_HAD_UNKNOWN_BOUND (decl
))
24139 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
24140 tf_error
, DECL_TI_TEMPLATE (decl
));
24143 gcc_unreachable ();
24145 pop_access_scope (decl
);
24148 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24149 substituted to get DECL. */
24152 template_for_substitution (tree decl
)
24154 tree tmpl
= DECL_TI_TEMPLATE (decl
);
24156 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24157 for the instantiation. This is not always the most general
24158 template. Consider, for example:
24161 struct S { template <class U> void f();
24162 template <> void f<int>(); };
24164 and an instantiation of S<double>::f<int>. We want TD to be the
24165 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24166 while (/* An instantiation cannot have a definition, so we need a
24167 more general template. */
24168 DECL_TEMPLATE_INSTANTIATION (tmpl
)
24169 /* We must also deal with friend templates. Given:
24171 template <class T> struct S {
24172 template <class U> friend void f() {};
24175 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24176 so far as the language is concerned, but that's still
24177 where we get the pattern for the instantiation from. On
24178 other hand, if the definition comes outside the class, say:
24180 template <class T> struct S {
24181 template <class U> friend void f();
24183 template <class U> friend void f() {}
24185 we don't need to look any further. That's what the check for
24186 DECL_INITIAL is for. */
24187 || (TREE_CODE (decl
) == FUNCTION_DECL
24188 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
24189 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
24191 /* The present template, TD, should not be a definition. If it
24192 were a definition, we should be using it! Note that we
24193 cannot restructure the loop to just keep going until we find
24194 a template with a definition, since that might go too far if
24195 a specialization was declared, but not defined. */
24197 /* Fetch the more general template. */
24198 tmpl
= DECL_TI_TEMPLATE (tmpl
);
24204 /* Returns true if we need to instantiate this template instance even if we
24205 know we aren't going to emit it. */
24208 always_instantiate_p (tree decl
)
24210 /* We always instantiate inline functions so that we can inline them. An
24211 explicit instantiation declaration prohibits implicit instantiation of
24212 non-inline functions. With high levels of optimization, we would
24213 normally inline non-inline functions -- but we're not allowed to do
24214 that for "extern template" functions. Therefore, we check
24215 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24216 return ((TREE_CODE (decl
) == FUNCTION_DECL
24217 && (DECL_DECLARED_INLINE_P (decl
)
24218 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
24219 /* And we need to instantiate static data members so that
24220 their initializers are available in integral constant
24223 && decl_maybe_constant_var_p (decl
)));
24226 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24227 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24228 error, true otherwise. */
24231 maybe_instantiate_noexcept (tree fn
, tsubst_flags_t complain
)
24233 tree fntype
, spec
, noex
, clone
;
24235 /* Don't instantiate a noexcept-specification from template context. */
24236 if (processing_template_decl
24237 && (!flag_noexcept_type
|| type_dependent_expression_p (fn
)))
24240 if (DECL_CLONED_FUNCTION_P (fn
))
24241 fn
= DECL_CLONED_FUNCTION (fn
);
24243 tree orig_fn
= NULL_TREE
;
24244 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24245 its FUNCTION_DECL for the rest of this function -- push_access_scope
24246 doesn't accept TEMPLATE_DECLs. */
24247 if (DECL_FUNCTION_TEMPLATE_P (fn
))
24250 fn
= DECL_TEMPLATE_RESULT (fn
);
24253 fntype
= TREE_TYPE (fn
);
24254 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
24256 if (!spec
|| !TREE_PURPOSE (spec
))
24259 noex
= TREE_PURPOSE (spec
);
24261 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
24263 static hash_set
<tree
>* fns
= new hash_set
<tree
>;
24264 bool added
= false;
24265 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
24267 spec
= get_defaulted_eh_spec (fn
, complain
);
24268 if (spec
== error_mark_node
)
24269 /* This might have failed because of an unparsed DMI, so
24270 let's try again later. */
24273 else if (!(added
= !fns
->add (fn
)))
24275 /* If hash_set::add returns true, the element was already there. */
24276 location_t loc
= cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex
),
24277 DECL_SOURCE_LOCATION (fn
));
24279 "exception specification of %qD depends on itself",
24281 spec
= noexcept_false_spec
;
24283 else if (push_tinst_level (fn
))
24285 push_access_scope (fn
);
24286 push_deferring_access_checks (dk_no_deferred
);
24287 input_location
= DECL_SOURCE_LOCATION (fn
);
24289 tree save_ccp
= current_class_ptr
;
24290 tree save_ccr
= current_class_ref
;
24291 /* If needed, set current_class_ptr for the benefit of
24292 tsubst_copy/PARM_DECL. */
24293 tree tdecl
= DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn
));
24294 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl
))
24296 tree this_parm
= DECL_ARGUMENTS (tdecl
);
24297 current_class_ptr
= NULL_TREE
;
24298 current_class_ref
= cp_build_fold_indirect_ref (this_parm
);
24299 current_class_ptr
= this_parm
;
24302 /* If this function is represented by a TEMPLATE_DECL, then
24303 the deferred noexcept-specification might still contain
24304 dependent types, even after substitution. And we need the
24305 dependency check functions to work in build_noexcept_spec. */
24307 ++processing_template_decl
;
24309 /* Do deferred instantiation of the noexcept-specifier. */
24310 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
24311 DEFERRED_NOEXCEPT_ARGS (noex
),
24312 tf_warning_or_error
, fn
,
24313 /*function_p=*/false,
24316 current_class_ptr
= save_ccp
;
24317 current_class_ref
= save_ccr
;
24319 /* Build up the noexcept-specification. */
24320 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
24323 --processing_template_decl
;
24325 pop_deferring_access_checks ();
24326 pop_access_scope (fn
);
24327 pop_tinst_level ();
24330 spec
= noexcept_false_spec
;
24335 if (spec
== error_mark_node
)
24337 /* This failed with a hard error, so let's go with false. */
24338 gcc_assert (seen_error ());
24339 spec
= noexcept_false_spec
;
24342 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
24344 TREE_TYPE (orig_fn
) = TREE_TYPE (fn
);
24347 FOR_EACH_CLONE (clone
, fn
)
24349 if (TREE_TYPE (clone
) == fntype
)
24350 TREE_TYPE (clone
) = TREE_TYPE (fn
);
24352 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
24358 /* We're starting to process the function INST, an instantiation of PATTERN;
24359 add their parameters to local_specializations. */
24362 register_parameter_specializations (tree pattern
, tree inst
)
24364 tree tmpl_parm
= DECL_ARGUMENTS (pattern
);
24365 tree spec_parm
= DECL_ARGUMENTS (inst
);
24366 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst
))
24368 register_local_specialization (spec_parm
, tmpl_parm
);
24369 spec_parm
= skip_artificial_parms_for (inst
, spec_parm
);
24370 tmpl_parm
= skip_artificial_parms_for (pattern
, tmpl_parm
);
24372 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
24374 if (!DECL_PACK_P (tmpl_parm
))
24376 register_local_specialization (spec_parm
, tmpl_parm
);
24377 spec_parm
= DECL_CHAIN (spec_parm
);
24381 /* Register the (value) argument pack as a specialization of
24382 TMPL_PARM, then move on. */
24383 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
24384 register_local_specialization (argpack
, tmpl_parm
);
24387 gcc_assert (!spec_parm
);
24390 /* Produce the definition of D, a _DECL generated from a template. If
24391 DEFER_OK is true, then we don't have to actually do the
24392 instantiation now; we just have to do it sometime. Normally it is
24393 an error if this is an explicit instantiation but D is undefined.
24394 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24395 instantiated class template. */
24398 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
24400 tree tmpl
= DECL_TI_TEMPLATE (d
);
24407 bool pattern_defined
;
24408 location_t saved_loc
= input_location
;
24409 int saved_unevaluated_operand
= cp_unevaluated_operand
;
24410 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24414 /* This function should only be used to instantiate templates for
24415 functions and static member variables. */
24416 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
24418 /* A concept is never instantiated. */
24419 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
24421 /* Variables are never deferred; if instantiation is required, they
24422 are instantiated right away. That allows for better code in the
24423 case that an expression refers to the value of the variable --
24424 if the variable has a constant value the referring expression can
24425 take advantage of that fact. */
24429 /* Don't instantiate cloned functions. Instead, instantiate the
24430 functions they cloned. */
24431 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
24432 d
= DECL_CLONED_FUNCTION (d
);
24434 if (DECL_TEMPLATE_INSTANTIATED (d
)
24435 || (TREE_CODE (d
) == FUNCTION_DECL
24436 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
24437 || DECL_TEMPLATE_SPECIALIZATION (d
))
24438 /* D has already been instantiated or explicitly specialized, so
24439 there's nothing for us to do here.
24441 It might seem reasonable to check whether or not D is an explicit
24442 instantiation, and, if so, stop here. But when an explicit
24443 instantiation is deferred until the end of the compilation,
24444 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24445 the instantiation. */
24448 /* Check to see whether we know that this template will be
24449 instantiated in some other file, as with "extern template"
24451 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
24453 /* In general, we do not instantiate such templates. */
24454 if (external_p
&& !always_instantiate_p (d
))
24457 gen_tmpl
= most_general_template (tmpl
);
24458 gen_args
= DECL_TI_ARGS (d
);
24460 if (tmpl
!= gen_tmpl
)
24461 /* We should already have the extra args. */
24462 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
24463 == TMPL_ARGS_DEPTH (gen_args
));
24464 /* And what's in the hash table should match D. */
24465 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
24466 || spec
== NULL_TREE
);
24468 /* This needs to happen before any tsubsting. */
24469 if (! push_tinst_level (d
))
24472 timevar_push (TV_TEMPLATE_INST
);
24474 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24475 for the instantiation. */
24476 td
= template_for_substitution (d
);
24481 /* Look up an explicit specialization, if any. */
24482 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
24483 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
24484 if (elt
&& elt
!= error_mark_node
)
24486 td
= TREE_VALUE (elt
);
24487 args
= TREE_PURPOSE (elt
);
24491 code_pattern
= DECL_TEMPLATE_RESULT (td
);
24493 /* We should never be trying to instantiate a member of a class
24494 template or partial specialization. */
24495 gcc_assert (d
!= code_pattern
);
24497 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
24498 || DECL_TEMPLATE_SPECIALIZATION (td
))
24499 /* In the case of a friend template whose definition is provided
24500 outside the class, we may have too many arguments. Drop the
24501 ones we don't need. The same is true for specializations. */
24502 args
= get_innermost_template_args
24503 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
24505 if (TREE_CODE (d
) == FUNCTION_DECL
)
24507 deleted_p
= DECL_DELETED_FN (code_pattern
);
24508 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
24509 && DECL_INITIAL (code_pattern
) != error_mark_node
)
24510 || DECL_DEFAULTED_FN (code_pattern
)
24516 if (DECL_CLASS_SCOPE_P (code_pattern
))
24517 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
24519 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
24522 /* We may be in the middle of deferred access check. Disable it now. */
24523 push_deferring_access_checks (dk_no_deferred
);
24525 /* Unless an explicit instantiation directive has already determined
24526 the linkage of D, remember that a definition is available for
24528 if (pattern_defined
24529 && !DECL_INTERFACE_KNOWN (d
)
24530 && !DECL_NOT_REALLY_EXTERN (d
))
24531 mark_definable (d
);
24533 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
24534 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
24535 input_location
= DECL_SOURCE_LOCATION (d
);
24537 /* If D is a member of an explicitly instantiated class template,
24538 and no definition is available, treat it like an implicit
24540 if (!pattern_defined
&& expl_inst_class_mem_p
24541 && DECL_EXPLICIT_INSTANTIATION (d
))
24543 /* Leave linkage flags alone on instantiations with anonymous
24545 if (TREE_PUBLIC (d
))
24547 DECL_NOT_REALLY_EXTERN (d
) = 0;
24548 DECL_INTERFACE_KNOWN (d
) = 0;
24550 SET_DECL_IMPLICIT_INSTANTIATION (d
);
24553 /* Defer all other templates, unless we have been explicitly
24554 forbidden from doing so. */
24555 if (/* If there is no definition, we cannot instantiate the
24558 /* If it's OK to postpone instantiation, do so. */
24560 /* If this is a static data member that will be defined
24561 elsewhere, we don't want to instantiate the entire data
24562 member, but we do want to instantiate the initializer so that
24563 we can substitute that elsewhere. */
24564 || (external_p
&& VAR_P (d
))
24565 /* Handle here a deleted function too, avoid generating
24566 its body (c++/61080). */
24569 /* The definition of the static data member is now required so
24570 we must substitute the initializer. */
24572 && !DECL_INITIAL (d
)
24573 && DECL_INITIAL (code_pattern
))
24577 bool const_init
= false;
24578 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24580 ns
= decl_namespace_context (d
);
24581 push_nested_namespace (ns
);
24583 push_nested_class (DECL_CONTEXT (d
));
24584 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
24586 tf_warning_or_error
, NULL_TREE
,
24587 /*integral_constant_expression_p=*/false);
24588 /* If instantiating the initializer involved instantiating this
24589 again, don't call cp_finish_decl twice. */
24590 if (!DECL_INITIAL (d
))
24592 /* Make sure the initializer is still constant, in case of
24593 circular dependency (template/instantiate6.C). */
24595 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24596 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
24597 /*asmspec_tree=*/NULL_TREE
,
24598 LOOKUP_ONLYCONVERTING
);
24601 pop_nested_class ();
24602 pop_nested_namespace (ns
);
24605 /* We restore the source position here because it's used by
24606 add_pending_template. */
24607 input_location
= saved_loc
;
24609 if (at_eof
&& !pattern_defined
24610 && DECL_EXPLICIT_INSTANTIATION (d
)
24611 && DECL_NOT_REALLY_EXTERN (d
))
24614 The definition of a non-exported function template, a
24615 non-exported member function template, or a non-exported
24616 member function or static data member of a class template
24617 shall be present in every translation unit in which it is
24618 explicitly instantiated. */
24619 permerror (input_location
, "explicit instantiation of %qD "
24620 "but no definition available", d
);
24622 /* If we're in unevaluated context, we just wanted to get the
24623 constant value; this isn't an odr use, so don't queue
24624 a full instantiation. */
24625 if (cp_unevaluated_operand
!= 0)
24627 /* ??? Historically, we have instantiated inline functions, even
24628 when marked as "extern template". */
24629 if (!(external_p
&& VAR_P (d
)))
24630 add_pending_template (d
);
24633 /* Tell the repository that D is available in this translation unit
24634 -- and see if it is supposed to be instantiated here. */
24635 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
24637 /* In a PCH file, despite the fact that the repository hasn't
24638 requested instantiation in the PCH it is still possible that
24639 an instantiation will be required in a file that includes the
24642 add_pending_template (d
);
24643 /* Instantiate inline functions so that the inliner can do its
24644 job, even though we'll not be emitting a copy of this
24646 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
24650 bool push_to_top
, nested
;
24652 fn_context
= decl_function_context (d
);
24653 if (LAMBDA_FUNCTION_P (d
))
24654 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24655 fn_context
= NULL_TREE
;
24656 nested
= current_function_decl
!= NULL_TREE
;
24657 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
24659 vec
<tree
> omp_privatization_save
;
24661 save_omp_privatization_clauses (omp_privatization_save
);
24664 push_to_top_level ();
24667 gcc_assert (!processing_template_decl
);
24668 push_function_context ();
24669 cp_unevaluated_operand
= 0;
24670 c_inhibit_evaluation_warnings
= 0;
24673 /* Mark D as instantiated so that recursive calls to
24674 instantiate_decl do not try to instantiate it again. */
24675 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
24677 /* Regenerate the declaration in case the template has been modified
24678 by a subsequent redeclaration. */
24679 regenerate_decl_from_template (d
, td
, args
);
24681 /* We already set the file and line above. Reset them now in case
24682 they changed as a result of calling regenerate_decl_from_template. */
24683 input_location
= DECL_SOURCE_LOCATION (d
);
24688 bool const_init
= false;
24690 /* Clear out DECL_RTL; whatever was there before may not be right
24691 since we've reset the type of the declaration. */
24692 SET_DECL_RTL (d
, NULL
);
24693 DECL_IN_AGGR_P (d
) = 0;
24695 /* The initializer is placed in DECL_INITIAL by
24696 regenerate_decl_from_template so we don't need to
24697 push/pop_access_scope again here. Pull it out so that
24698 cp_finish_decl can process it. */
24699 init
= DECL_INITIAL (d
);
24700 DECL_INITIAL (d
) = NULL_TREE
;
24701 DECL_INITIALIZED_P (d
) = 0;
24703 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24704 initializer. That function will defer actual emission until
24705 we have a chance to determine linkage. */
24706 DECL_EXTERNAL (d
) = 0;
24708 /* Enter the scope of D so that access-checking works correctly. */
24709 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
24711 push_nested_class (DECL_CONTEXT (d
));
24713 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
24714 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
24717 pop_nested_class ();
24719 if (variable_template_p (gen_tmpl
))
24720 note_variable_template_instantiation (d
);
24722 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
24723 synthesize_method (d
);
24724 else if (TREE_CODE (d
) == FUNCTION_DECL
)
24726 /* Set up the list of local specializations. */
24727 local_specialization_stack
lss (push_to_top
? lss_blank
: lss_copy
);
24728 tree block
= NULL_TREE
;
24730 /* Set up context. */
24731 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24732 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24733 block
= push_stmt_list ();
24735 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
24737 /* Some typedefs referenced from within the template code need to be
24738 access checked at template instantiation time, i.e now. These
24739 types were added to the template at parsing time. Let's get those
24740 and perform the access checks then. */
24741 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
24744 /* Create substitution entries for the parameters. */
24745 register_parameter_specializations (code_pattern
, d
);
24747 /* Substitute into the body of the function. */
24748 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24749 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
24750 tf_warning_or_error
, tmpl
);
24753 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
24754 tf_warning_or_error
, tmpl
,
24755 /*integral_constant_expression_p=*/false);
24757 /* Set the current input_location to the end of the function
24758 so that finish_function knows where we are. */
24760 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
24762 /* Remember if we saw an infinite loop in the template. */
24763 current_function_infinite_loop
24764 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
24767 /* Finish the function. */
24768 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
24769 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
24770 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
24773 d
= finish_function (/*inline_p=*/false);
24774 expand_or_defer_fn (d
);
24777 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
24778 cp_check_omp_declare_reduction (d
);
24781 /* We're not deferring instantiation any more. */
24782 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
24785 pop_from_top_level ();
24787 pop_function_context ();
24790 restore_omp_privatization_clauses (omp_privatization_save
);
24793 pop_deferring_access_checks ();
24794 timevar_pop (TV_TEMPLATE_INST
);
24795 pop_tinst_level ();
24796 input_location
= saved_loc
;
24797 cp_unevaluated_operand
= saved_unevaluated_operand
;
24798 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24803 /* Run through the list of templates that we wish we could
24804 instantiate, and instantiate any we can. RETRIES is the
24805 number of times we retry pending template instantiation. */
24808 instantiate_pending_templates (int retries
)
24811 location_t saved_loc
= input_location
;
24813 /* Instantiating templates may trigger vtable generation. This in turn
24814 may require further template instantiations. We place a limit here
24815 to avoid infinite loop. */
24816 if (pending_templates
&& retries
>= max_tinst_depth
)
24818 tree decl
= pending_templates
->tinst
->maybe_get_node ();
24820 fatal_error (input_location
,
24821 "template instantiation depth exceeds maximum of %d"
24822 " instantiating %q+D, possibly from virtual table generation"
24823 " (use %<-ftemplate-depth=%> to increase the maximum)",
24824 max_tinst_depth
, decl
);
24825 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24826 /* Pretend that we defined it. */
24827 DECL_INITIAL (decl
) = error_mark_node
;
24833 struct pending_template
**t
= &pending_templates
;
24834 struct pending_template
*last
= NULL
;
24838 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
24839 bool complete
= false;
24841 if (TYPE_P (instantiation
))
24843 if (!COMPLETE_TYPE_P (instantiation
))
24845 instantiate_class_template (instantiation
);
24846 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
24847 for (tree fld
= TYPE_FIELDS (instantiation
);
24848 fld
; fld
= TREE_CHAIN (fld
))
24850 || (TREE_CODE (fld
) == FUNCTION_DECL
24851 && !DECL_ARTIFICIAL (fld
)))
24852 && DECL_TEMPLATE_INSTANTIATION (fld
))
24853 instantiate_decl (fld
,
24854 /*defer_ok=*/false,
24855 /*expl_inst_class_mem_p=*/false);
24857 if (COMPLETE_TYPE_P (instantiation
))
24861 complete
= COMPLETE_TYPE_P (instantiation
);
24865 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24866 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
24869 = instantiate_decl (instantiation
,
24870 /*defer_ok=*/false,
24871 /*expl_inst_class_mem_p=*/false);
24872 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
24876 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
24877 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
24882 /* If INSTANTIATION has been instantiated, then we don't
24883 need to consider it again in the future. */
24884 struct pending_template
*drop
= *t
;
24886 set_refcount_ptr (drop
->tinst
);
24887 pending_template_freelist ().free (drop
);
24895 set_refcount_ptr (current_tinst_level
);
24897 last_pending_template
= last
;
24899 while (reconsider
);
24901 input_location
= saved_loc
;
24904 /* Substitute ARGVEC into T, which is a list of initializers for
24905 either base class or a non-static data member. The TREE_PURPOSEs
24906 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24907 instantiate_decl. */
24910 tsubst_initializer_list (tree t
, tree argvec
)
24912 tree inits
= NULL_TREE
;
24913 tree target_ctor
= error_mark_node
;
24915 for (; t
; t
= TREE_CHAIN (t
))
24919 tree expanded_bases
= NULL_TREE
;
24920 tree expanded_arguments
= NULL_TREE
;
24923 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
24928 /* Expand the base class expansion type into separate base
24930 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
24931 tf_warning_or_error
,
24933 if (expanded_bases
== error_mark_node
)
24936 /* We'll be building separate TREE_LISTs of arguments for
24938 len
= TREE_VEC_LENGTH (expanded_bases
);
24939 expanded_arguments
= make_tree_vec (len
);
24940 for (i
= 0; i
< len
; i
++)
24941 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
24943 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24944 expand each argument in the TREE_VALUE of t. */
24945 expr
= make_node (EXPR_PACK_EXPANSION
);
24946 PACK_EXPANSION_LOCAL_P (expr
) = true;
24947 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
24948 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
24950 if (TREE_VALUE (t
) == void_type_node
)
24951 /* VOID_TYPE_NODE is used to indicate
24952 value-initialization. */
24954 for (i
= 0; i
< len
; i
++)
24955 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
24959 /* Substitute parameter packs into each argument in the
24961 in_base_initializer
= 1;
24962 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
24964 tree expanded_exprs
;
24966 /* Expand the argument. */
24967 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
24969 = tsubst_pack_expansion (expr
, argvec
,
24970 tf_warning_or_error
,
24972 if (expanded_exprs
== error_mark_node
)
24975 /* Prepend each of the expanded expressions to the
24976 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24977 for (i
= 0; i
< len
; i
++)
24979 TREE_VEC_ELT (expanded_arguments
, i
) =
24980 tree_cons (NULL_TREE
,
24981 TREE_VEC_ELT (expanded_exprs
, i
),
24982 TREE_VEC_ELT (expanded_arguments
, i
));
24985 in_base_initializer
= 0;
24987 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24988 since we built them backwards. */
24989 for (i
= 0; i
< len
; i
++)
24991 TREE_VEC_ELT (expanded_arguments
, i
) =
24992 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
24997 for (i
= 0; i
< len
; ++i
)
24999 if (expanded_bases
)
25001 decl
= TREE_VEC_ELT (expanded_bases
, i
);
25002 decl
= expand_member_init (decl
);
25003 init
= TREE_VEC_ELT (expanded_arguments
, i
);
25008 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
25009 tf_warning_or_error
, NULL_TREE
);
25011 decl
= expand_member_init (decl
);
25012 if (decl
&& !DECL_P (decl
))
25013 in_base_initializer
= 1;
25015 init
= TREE_VALUE (t
);
25017 if (init
!= void_type_node
)
25018 init
= tsubst_expr (init
, argvec
,
25019 tf_warning_or_error
, NULL_TREE
,
25020 /*integral_constant_expression_p=*/false);
25021 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
25022 /* If we had an initializer but it instantiated to nothing,
25023 value-initialize the object. This will only occur when
25024 the initializer was a pack expansion where the parameter
25025 packs used in that expansion were of length zero. */
25026 init
= void_type_node
;
25027 in_base_initializer
= 0;
25030 if (target_ctor
!= error_mark_node
25031 && init
!= error_mark_node
)
25033 error ("mem-initializer for %qD follows constructor delegation",
25037 /* Look for a target constructor. */
25038 if (init
!= error_mark_node
25039 && decl
&& CLASS_TYPE_P (decl
)
25040 && same_type_p (decl
, current_class_type
))
25042 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
25045 error ("constructor delegation follows mem-initializer for %qD",
25046 TREE_PURPOSE (inits
));
25049 target_ctor
= init
;
25054 init
= build_tree_list (decl
, init
);
25055 TREE_CHAIN (init
) = inits
;
25063 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25066 set_current_access_from_decl (tree decl
)
25068 if (TREE_PRIVATE (decl
))
25069 current_access_specifier
= access_private_node
;
25070 else if (TREE_PROTECTED (decl
))
25071 current_access_specifier
= access_protected_node
;
25073 current_access_specifier
= access_public_node
;
25076 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25077 is the instantiation (which should have been created with
25078 start_enum) and ARGS are the template arguments to use. */
25081 tsubst_enum (tree tag
, tree newtag
, tree args
)
25085 if (SCOPED_ENUM_P (newtag
))
25086 begin_scope (sk_scoped_enum
, newtag
);
25088 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
25093 decl
= TREE_VALUE (e
);
25094 /* Note that in a template enum, the TREE_VALUE is the
25095 CONST_DECL, not the corresponding INTEGER_CST. */
25096 value
= tsubst_expr (DECL_INITIAL (decl
),
25097 args
, tf_warning_or_error
, NULL_TREE
,
25098 /*integral_constant_expression_p=*/true);
25100 /* Give this enumeration constant the correct access. */
25101 set_current_access_from_decl (decl
);
25103 /* Actually build the enumerator itself. Here we're assuming that
25104 enumerators can't have dependent attributes. */
25105 build_enumerator (DECL_NAME (decl
), value
, newtag
,
25106 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
25109 if (SCOPED_ENUM_P (newtag
))
25112 finish_enum_value_list (newtag
);
25113 finish_enum (newtag
);
25115 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
25116 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
25119 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25120 its type -- but without substituting the innermost set of template
25121 arguments. So, innermost set of template parameters will appear in
25125 get_mostly_instantiated_function_type (tree decl
)
25127 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25128 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
25131 /* Return truthvalue if we're processing a template different from
25132 the last one involved in diagnostics. */
25134 problematic_instantiation_changed (void)
25136 return current_tinst_level
!= last_error_tinst_level
;
25139 /* Remember current template involved in diagnostics. */
25141 record_last_problematic_instantiation (void)
25143 set_refcount_ptr (last_error_tinst_level
, current_tinst_level
);
25146 struct tinst_level
*
25147 current_instantiation (void)
25149 return current_tinst_level
;
25152 /* Return TRUE if current_function_decl is being instantiated, false
25156 instantiating_current_function_p (void)
25158 return (current_instantiation ()
25159 && (current_instantiation ()->maybe_get_node ()
25160 == current_function_decl
));
25163 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25164 type. Return false for ok, true for disallowed. Issue error and
25165 inform messages under control of COMPLAIN. */
25168 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
25170 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
25172 else if (TYPE_PTR_P (type
))
25174 else if (TYPE_REF_P (type
)
25175 && !TYPE_REF_IS_RVALUE (type
))
25177 else if (TYPE_PTRMEM_P (type
))
25179 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
25181 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
25183 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
25185 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
25187 /* A bound template template parm could later be instantiated to have a valid
25188 nontype parm type via an alias template. */
25189 else if (cxx_dialect
>= cxx11
25190 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25192 else if (CLASS_TYPE_P (type
))
25194 if (cxx_dialect
< cxx2a
)
25196 error ("non-type template parameters of class type only available "
25197 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25200 if (!complete_type_or_else (type
, NULL_TREE
))
25202 if (!literal_type_p (type
))
25204 error ("%qT is not a valid type for a template non-type parameter "
25205 "because it is not literal", type
);
25206 explain_non_literal_class (type
);
25209 if (cp_has_mutable_p (type
))
25211 error ("%qT is not a valid type for a template non-type parameter "
25212 "because it has a mutable member", type
);
25215 /* FIXME check op<=> and strong structural equality once spaceship is
25220 if (complain
& tf_error
)
25222 if (type
== error_mark_node
)
25223 inform (input_location
, "invalid template non-type parameter");
25225 error ("%q#T is not a valid type for a template non-type parameter",
25231 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25232 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25235 dependent_type_p_r (tree type
)
25241 A type is dependent if it is:
25243 -- a template parameter. Template template parameters are types
25244 for us (since TYPE_P holds true for them) so we handle
25246 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25247 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
25249 /* -- a qualified-id with a nested-name-specifier which contains a
25250 class-name that names a dependent type or whose unqualified-id
25251 names a dependent type. */
25252 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25255 /* An alias template specialization can be dependent even if the
25256 resulting type is not. */
25257 if (dependent_alias_template_spec_p (type
))
25260 /* -- a cv-qualified type where the cv-unqualified type is
25262 No code is necessary for this bullet; the code below handles
25263 cv-qualified types, and we don't want to strip aliases with
25264 TYPE_MAIN_VARIANT because of DR 1558. */
25265 /* -- a compound type constructed from any dependent type. */
25266 if (TYPE_PTRMEM_P (type
))
25267 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
25268 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25270 else if (INDIRECT_TYPE_P (type
))
25271 return dependent_type_p (TREE_TYPE (type
));
25272 else if (TREE_CODE (type
) == FUNCTION_TYPE
25273 || TREE_CODE (type
) == METHOD_TYPE
)
25277 if (dependent_type_p (TREE_TYPE (type
)))
25279 for (arg_type
= TYPE_ARG_TYPES (type
);
25281 arg_type
= TREE_CHAIN (arg_type
))
25282 if (dependent_type_p (TREE_VALUE (arg_type
)))
25284 if (cxx_dialect
>= cxx17
)
25285 /* A value-dependent noexcept-specifier makes the type dependent. */
25286 if (tree spec
= TYPE_RAISES_EXCEPTIONS (type
))
25287 if (tree noex
= TREE_PURPOSE (spec
))
25288 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25289 affect overload resolution and treating it as dependent breaks
25291 if (TREE_CODE (noex
) != DEFERRED_NOEXCEPT
25292 && value_dependent_expression_p (noex
))
25296 /* -- an array type constructed from any dependent type or whose
25297 size is specified by a constant expression that is
25300 We checked for type- and value-dependence of the bounds in
25301 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25302 if (TREE_CODE (type
) == ARRAY_TYPE
)
25304 if (TYPE_DOMAIN (type
)
25305 && dependent_type_p (TYPE_DOMAIN (type
)))
25307 return dependent_type_p (TREE_TYPE (type
));
25310 /* -- a template-id in which either the template name is a template
25312 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
25314 /* ... or any of the template arguments is a dependent type or
25315 an expression that is type-dependent or value-dependent. */
25316 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
25317 && (any_dependent_template_arguments_p
25318 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
25321 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25322 dependent; if the argument of the `typeof' expression is not
25323 type-dependent, then it should already been have resolved. */
25324 if (TREE_CODE (type
) == TYPEOF_TYPE
25325 || TREE_CODE (type
) == DECLTYPE_TYPE
25326 || TREE_CODE (type
) == UNDERLYING_TYPE
)
25329 /* A template argument pack is dependent if any of its packed
25331 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
25333 tree args
= ARGUMENT_PACK_ARGS (type
);
25334 int i
, len
= TREE_VEC_LENGTH (args
);
25335 for (i
= 0; i
< len
; ++i
)
25336 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
25340 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25341 be template parameters. */
25342 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
25345 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
25348 /* The standard does not specifically mention types that are local
25349 to template functions or local classes, but they should be
25350 considered dependent too. For example:
25352 template <int I> void f() {
25357 The size of `E' cannot be known until the value of `I' has been
25358 determined. Therefore, `E' must be considered dependent. */
25359 scope
= TYPE_CONTEXT (type
);
25360 if (scope
&& TYPE_P (scope
))
25361 return dependent_type_p (scope
);
25362 /* Don't use type_dependent_expression_p here, as it can lead
25363 to infinite recursion trying to determine whether a lambda
25364 nested in a lambda is dependent (c++/47687). */
25365 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
25366 && DECL_LANG_SPECIFIC (scope
)
25367 && DECL_TEMPLATE_INFO (scope
)
25368 && (any_dependent_template_arguments_p
25369 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
25372 /* Other types are non-dependent. */
25376 /* Returns TRUE if TYPE is dependent, in the sense of
25377 [temp.dep.type]. Note that a NULL type is considered dependent. */
25380 dependent_type_p (tree type
)
25382 /* If there are no template parameters in scope, then there can't be
25383 any dependent types. */
25384 if (!processing_template_decl
)
25386 /* If we are not processing a template, then nobody should be
25387 providing us with a dependent type. */
25389 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
25393 /* If the type is NULL, we have not computed a type for the entity
25394 in question; in that case, the type is dependent. */
25398 /* Erroneous types can be considered non-dependent. */
25399 if (type
== error_mark_node
)
25402 /* Getting here with global_type_node means we improperly called this
25403 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25404 gcc_checking_assert (type
!= global_type_node
);
25406 /* If we have not already computed the appropriate value for TYPE,
25408 if (!TYPE_DEPENDENT_P_VALID (type
))
25410 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
25411 TYPE_DEPENDENT_P_VALID (type
) = 1;
25414 return TYPE_DEPENDENT_P (type
);
25417 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25418 lookup. In other words, a dependent type that is not the current
25422 dependent_scope_p (tree scope
)
25424 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
25425 && !currently_open_class (scope
));
25428 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25429 an unknown base of 'this' (and is therefore instantiation-dependent). */
25432 unknown_base_ref_p (tree t
)
25434 if (!current_class_ptr
)
25437 tree mem
= TREE_OPERAND (t
, 1);
25438 if (shared_member_p (mem
))
25441 tree cur
= current_nonlambda_class_type ();
25442 if (!any_dependent_bases_p (cur
))
25445 tree ctx
= TREE_OPERAND (t
, 0);
25446 if (DERIVED_FROM_P (ctx
, cur
))
25452 /* T is a SCOPE_REF; return whether we need to consider it
25453 instantiation-dependent so that we can check access at instantiation
25454 time even though we know which member it resolves to. */
25457 instantiation_dependent_scope_ref_p (tree t
)
25459 if (DECL_P (TREE_OPERAND (t
, 1))
25460 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
25461 && !unknown_base_ref_p (t
)
25462 && accessible_in_template_p (TREE_OPERAND (t
, 0),
25463 TREE_OPERAND (t
, 1)))
25469 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25470 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25473 /* Note that this predicate is not appropriate for general expressions;
25474 only constant expressions (that satisfy potential_constant_expression)
25475 can be tested for value dependence. */
25478 value_dependent_expression_p (tree expression
)
25480 if (!processing_template_decl
|| expression
== NULL_TREE
)
25483 /* A type-dependent expression is also value-dependent. */
25484 if (type_dependent_expression_p (expression
))
25487 switch (TREE_CODE (expression
))
25490 /* A dependent member function of the current instantiation. */
25491 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
25493 case FUNCTION_DECL
:
25494 /* A dependent member function of the current instantiation. */
25495 if (DECL_CLASS_SCOPE_P (expression
)
25496 && dependent_type_p (DECL_CONTEXT (expression
)))
25500 case IDENTIFIER_NODE
:
25501 /* A name that has not been looked up -- must be dependent. */
25504 case TEMPLATE_PARM_INDEX
:
25505 /* A non-type template parm. */
25509 /* A non-type template parm. */
25510 if (DECL_TEMPLATE_PARM_P (expression
))
25512 return value_dependent_expression_p (DECL_INITIAL (expression
));
25515 /* A constant with literal type and is initialized
25516 with an expression that is value-dependent. */
25517 if (DECL_DEPENDENT_INIT_P (expression
)
25518 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25519 || TYPE_REF_P (TREE_TYPE (expression
)))
25521 if (DECL_HAS_VALUE_EXPR_P (expression
))
25523 tree value_expr
= DECL_VALUE_EXPR (expression
);
25524 if (value_dependent_expression_p (value_expr
))
25529 case DYNAMIC_CAST_EXPR
:
25530 case STATIC_CAST_EXPR
:
25531 case CONST_CAST_EXPR
:
25532 case REINTERPRET_CAST_EXPR
:
25534 case IMPLICIT_CONV_EXPR
:
25535 /* These expressions are value-dependent if the type to which
25536 the cast occurs is dependent or the expression being casted
25537 is value-dependent. */
25539 tree type
= TREE_TYPE (expression
);
25541 if (dependent_type_p (type
))
25544 /* A functional cast has a list of operands. */
25545 expression
= TREE_OPERAND (expression
, 0);
25548 /* If there are no operands, it must be an expression such
25549 as "int()". This should not happen for aggregate types
25550 because it would form non-constant expressions. */
25551 gcc_assert (cxx_dialect
>= cxx11
25552 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
25557 if (TREE_CODE (expression
) == TREE_LIST
)
25558 return any_value_dependent_elements_p (expression
);
25560 return value_dependent_expression_p (expression
);
25564 if (SIZEOF_EXPR_TYPE_P (expression
))
25565 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
25569 /* A `sizeof' expression is value-dependent if the operand is
25570 type-dependent or is a pack expansion. */
25571 expression
= TREE_OPERAND (expression
, 0);
25572 if (PACK_EXPANSION_P (expression
))
25574 else if (TYPE_P (expression
))
25575 return dependent_type_p (expression
);
25576 return instantiation_dependent_uneval_expression_p (expression
);
25578 case AT_ENCODE_EXPR
:
25579 /* An 'encode' expression is value-dependent if the operand is
25581 expression
= TREE_OPERAND (expression
, 0);
25582 return dependent_type_p (expression
);
25584 case NOEXCEPT_EXPR
:
25585 expression
= TREE_OPERAND (expression
, 0);
25586 return instantiation_dependent_uneval_expression_p (expression
);
25589 /* All instantiation-dependent expressions should also be considered
25590 value-dependent. */
25591 return instantiation_dependent_scope_ref_p (expression
);
25593 case COMPONENT_REF
:
25594 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
25595 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
25597 case NONTYPE_ARGUMENT_PACK
:
25598 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25599 is value-dependent. */
25601 tree values
= ARGUMENT_PACK_ARGS (expression
);
25602 int i
, len
= TREE_VEC_LENGTH (values
);
25604 for (i
= 0; i
< len
; ++i
)
25605 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
25613 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
25615 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
)))
25621 if (TREE_CODE (type2
) != TREE_LIST
)
25622 return dependent_type_p (type2
);
25624 for (; type2
; type2
= TREE_CHAIN (type2
))
25625 if (dependent_type_p (TREE_VALUE (type2
)))
25632 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25633 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
25636 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25637 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
25641 tree op
= TREE_OPERAND (expression
, 0);
25642 return (value_dependent_expression_p (op
)
25643 || has_value_dependent_address (op
));
25646 case REQUIRES_EXPR
:
25647 /* Treat all requires-expressions as value-dependent so
25648 we don't try to fold them. */
25652 return dependent_type_p (TREE_OPERAND (expression
, 0));
25656 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
25658 tree fn
= get_callee_fndecl (expression
);
25660 nargs
= call_expr_nargs (expression
);
25661 for (i
= 0; i
< nargs
; ++i
)
25663 tree op
= CALL_EXPR_ARG (expression
, i
);
25664 /* In a call to a constexpr member function, look through the
25665 implicit ADDR_EXPR on the object argument so that it doesn't
25666 cause the call to be considered value-dependent. We also
25667 look through it in potential_constant_expression. */
25668 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
25669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
25670 && TREE_CODE (op
) == ADDR_EXPR
)
25671 op
= TREE_OPERAND (op
, 0);
25672 if (value_dependent_expression_p (op
))
25678 case TEMPLATE_ID_EXPR
:
25679 return variable_concept_p (TREE_OPERAND (expression
, 0));
25685 if (dependent_type_p (TREE_TYPE (expression
)))
25687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
25688 if (value_dependent_expression_p (val
))
25694 /* Treat a GNU statement expression as dependent to avoid crashing
25695 under instantiate_non_dependent_expr; it can't be constant. */
25699 /* A constant expression is value-dependent if any subexpression is
25700 value-dependent. */
25701 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
25703 case tcc_reference
:
25705 case tcc_comparison
:
25707 case tcc_expression
:
25710 int i
, len
= cp_tree_operand_length (expression
);
25712 for (i
= 0; i
< len
; i
++)
25714 tree t
= TREE_OPERAND (expression
, i
);
25716 /* In some cases, some of the operands may be missing.
25717 (For example, in the case of PREDECREMENT_EXPR, the
25718 amount to increment by may be missing.) That doesn't
25719 make the expression dependent. */
25720 if (t
&& value_dependent_expression_p (t
))
25731 /* The expression is not value-dependent. */
25735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25736 [temp.dep.expr]. Note that an expression with no type is
25737 considered dependent. Other parts of the compiler arrange for an
25738 expression with type-dependent subexpressions to have no type, so
25739 this function doesn't have to be fully recursive. */
25742 type_dependent_expression_p (tree expression
)
25744 if (!processing_template_decl
)
25747 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
25750 STRIP_ANY_LOCATION_WRAPPER (expression
);
25752 /* An unresolved name is always dependent. */
25753 if (identifier_p (expression
)
25754 || TREE_CODE (expression
) == USING_DECL
25755 || TREE_CODE (expression
) == WILDCARD_DECL
)
25758 /* A lambda-expression in template context is dependent. dependent_type_p is
25759 true for a lambda in the scope of a class or function template, but that
25760 doesn't cover all template contexts, like a default template argument. */
25761 if (TREE_CODE (expression
) == LAMBDA_EXPR
)
25764 /* A fold expression is type-dependent. */
25765 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
25766 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
25767 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
25768 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
25771 /* Some expression forms are never type-dependent. */
25772 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
25773 || TREE_CODE (expression
) == SIZEOF_EXPR
25774 || TREE_CODE (expression
) == ALIGNOF_EXPR
25775 || TREE_CODE (expression
) == AT_ENCODE_EXPR
25776 || TREE_CODE (expression
) == NOEXCEPT_EXPR
25777 || TREE_CODE (expression
) == TRAIT_EXPR
25778 || TREE_CODE (expression
) == TYPEID_EXPR
25779 || TREE_CODE (expression
) == DELETE_EXPR
25780 || TREE_CODE (expression
) == VEC_DELETE_EXPR
25781 || TREE_CODE (expression
) == THROW_EXPR
25782 || TREE_CODE (expression
) == REQUIRES_EXPR
)
25785 /* The types of these expressions depends only on the type to which
25786 the cast occurs. */
25787 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
25788 || TREE_CODE (expression
) == STATIC_CAST_EXPR
25789 || TREE_CODE (expression
) == CONST_CAST_EXPR
25790 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
25791 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
25792 || TREE_CODE (expression
) == CAST_EXPR
)
25793 return dependent_type_p (TREE_TYPE (expression
));
25795 /* The types of these expressions depends only on the type created
25796 by the expression. */
25797 if (TREE_CODE (expression
) == NEW_EXPR
25798 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
25800 /* For NEW_EXPR tree nodes created inside a template, either
25801 the object type itself or a TREE_LIST may appear as the
25803 tree type
= TREE_OPERAND (expression
, 1);
25804 if (TREE_CODE (type
) == TREE_LIST
)
25805 /* This is an array type. We need to check array dimensions
25807 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
25808 || value_dependent_expression_p
25809 (TREE_OPERAND (TREE_VALUE (type
), 1));
25811 return dependent_type_p (type
);
25814 if (TREE_CODE (expression
) == SCOPE_REF
)
25816 tree scope
= TREE_OPERAND (expression
, 0);
25817 tree name
= TREE_OPERAND (expression
, 1);
25819 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25820 contains an identifier associated by name lookup with one or more
25821 declarations declared with a dependent type, or...a
25822 nested-name-specifier or qualified-id that names a member of an
25823 unknown specialization. */
25824 return (type_dependent_expression_p (name
)
25825 || dependent_scope_p (scope
));
25828 if (TREE_CODE (expression
) == TEMPLATE_DECL
25829 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
25830 return uses_outer_template_parms (expression
);
25832 if (TREE_CODE (expression
) == STMT_EXPR
)
25833 expression
= stmt_expr_value_expr (expression
);
25835 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
25840 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
25842 if (type_dependent_expression_p (elt
))
25848 /* A static data member of the current instantiation with incomplete
25849 array type is type-dependent, as the definition and specializations
25850 can have different bounds. */
25851 if (VAR_P (expression
)
25852 && DECL_CLASS_SCOPE_P (expression
)
25853 && dependent_type_p (DECL_CONTEXT (expression
))
25854 && VAR_HAD_UNKNOWN_BOUND (expression
))
25857 /* An array of unknown bound depending on a variadic parameter, eg:
25859 template<typename... Args>
25860 void foo (Args... args)
25862 int arr[] = { args... };
25865 template<int... vals>
25868 int arr[] = { vals... };
25871 If the array has no length and has an initializer, it must be that
25872 we couldn't determine its length in cp_complete_array_type because
25873 it is dependent. */
25874 if (VAR_P (expression
)
25875 && TREE_TYPE (expression
) != NULL_TREE
25876 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
25877 && !TYPE_DOMAIN (TREE_TYPE (expression
))
25878 && DECL_INITIAL (expression
))
25881 /* A function or variable template-id is type-dependent if it has any
25882 dependent template arguments. */
25883 if (VAR_OR_FUNCTION_DECL_P (expression
)
25884 && DECL_LANG_SPECIFIC (expression
)
25885 && DECL_TEMPLATE_INFO (expression
))
25887 /* Consider the innermost template arguments, since those are the ones
25888 that come from the template-id; the template arguments for the
25889 enclosing class do not make it type-dependent unless they are used in
25890 the type of the decl. */
25891 if (instantiates_primary_template_p (expression
)
25892 && (any_dependent_template_arguments_p
25893 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
25897 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25898 type-dependent. Checking this is important for functions with auto return
25899 type, which looks like a dependent type. */
25900 if (TREE_CODE (expression
) == FUNCTION_DECL
25901 && !(DECL_CLASS_SCOPE_P (expression
)
25902 && dependent_type_p (DECL_CONTEXT (expression
)))
25903 && !(DECL_LANG_SPECIFIC (expression
)
25904 && DECL_FRIEND_P (expression
)
25905 && (!DECL_FRIEND_CONTEXT (expression
)
25906 || dependent_type_p (DECL_FRIEND_CONTEXT (expression
))))
25907 && !DECL_LOCAL_FUNCTION_P (expression
))
25909 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
25910 || undeduced_auto_decl (expression
));
25914 /* Always dependent, on the number of arguments if nothing else. */
25915 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
25918 if (TREE_TYPE (expression
) == unknown_type_node
)
25920 if (TREE_CODE (expression
) == ADDR_EXPR
)
25921 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
25922 if (TREE_CODE (expression
) == COMPONENT_REF
25923 || TREE_CODE (expression
) == OFFSET_REF
)
25925 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
25927 expression
= TREE_OPERAND (expression
, 1);
25928 if (identifier_p (expression
))
25931 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25932 if (TREE_CODE (expression
) == SCOPE_REF
)
25935 if (BASELINK_P (expression
))
25937 if (BASELINK_OPTYPE (expression
)
25938 && dependent_type_p (BASELINK_OPTYPE (expression
)))
25940 expression
= BASELINK_FUNCTIONS (expression
);
25943 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
25945 if (any_dependent_template_arguments_p
25946 (TREE_OPERAND (expression
, 1)))
25948 expression
= TREE_OPERAND (expression
, 0);
25949 if (identifier_p (expression
))
25953 gcc_assert (TREE_CODE (expression
) == OVERLOAD
25954 || TREE_CODE (expression
) == FUNCTION_DECL
);
25956 for (lkp_iterator
iter (expression
); iter
; ++iter
)
25957 if (type_dependent_expression_p (*iter
))
25963 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
25965 /* Dependent type attributes might not have made it from the decl to
25967 if (DECL_P (expression
)
25968 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
25971 return (dependent_type_p (TREE_TYPE (expression
)));
25974 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25975 type-dependent if the expression refers to a member of the current
25976 instantiation and the type of the referenced member is dependent, or the
25977 class member access expression refers to a member of an unknown
25980 This function returns true if the OBJECT in such a class member access
25981 expression is of an unknown specialization. */
25984 type_dependent_object_expression_p (tree object
)
25986 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25988 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
25990 tree scope
= TREE_TYPE (object
);
25991 return (!scope
|| dependent_scope_p (scope
));
25994 /* walk_tree callback function for instantiation_dependent_expression_p,
25995 below. Returns non-zero if a dependent subexpression is found. */
25998 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
26003 /* We don't have to worry about decltype currently because decltype
26004 of an instantiation-dependent expr is a dependent type. This
26005 might change depending on the resolution of DR 1172. */
26006 *walk_subtrees
= false;
26009 enum tree_code code
= TREE_CODE (*tp
);
26012 /* Don't treat an argument list as dependent just because it has no
26016 case NONTYPE_ARGUMENT_PACK
:
26019 case TEMPLATE_PARM_INDEX
:
26020 if (dependent_type_p (TREE_TYPE (*tp
)))
26022 if (TEMPLATE_PARM_PARAMETER_PACK (*tp
))
26024 /* We'll check value-dependence separately. */
26027 /* Handle expressions with type operands. */
26031 case AT_ENCODE_EXPR
:
26033 tree op
= TREE_OPERAND (*tp
, 0);
26034 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
26035 op
= TREE_TYPE (op
);
26038 if (dependent_type_p (op
))
26042 *walk_subtrees
= false;
26049 case COMPONENT_REF
:
26050 if (identifier_p (TREE_OPERAND (*tp
, 1)))
26051 /* In a template, finish_class_member_access_expr creates a
26052 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26053 type-dependent, so that we can check access control at
26054 instantiation time (PR 42277). See also Core issue 1273. */
26059 if (instantiation_dependent_scope_ref_p (*tp
))
26064 /* Treat statement-expressions as dependent. */
26068 /* Treat requires-expressions as dependent. */
26069 case REQUIRES_EXPR
:
26073 /* Treat calls to function concepts as dependent. */
26074 if (function_concept_check_p (*tp
))
26078 case TEMPLATE_ID_EXPR
:
26079 /* And variable concepts. */
26080 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
26085 if (CONSTRUCTOR_IS_DEPENDENT (*tp
))
26093 if (type_dependent_expression_p (*tp
))
26099 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26100 sense defined by the ABI:
26102 "An expression is instantiation-dependent if it is type-dependent
26103 or value-dependent, or it has a subexpression that is type-dependent
26104 or value-dependent."
26106 Except don't actually check value-dependence for unevaluated expressions,
26107 because in sizeof(i) we don't care about the value of i. Checking
26108 type-dependence will in turn check value-dependence of array bounds/template
26109 arguments as needed. */
26112 instantiation_dependent_uneval_expression_p (tree expression
)
26116 if (!processing_template_decl
)
26119 if (expression
== error_mark_node
)
26122 result
= cp_walk_tree_without_duplicates (&expression
,
26123 instantiation_dependent_r
, NULL
);
26124 return result
!= NULL_TREE
;
26127 /* As above, but also check value-dependence of the expression as a whole. */
26130 instantiation_dependent_expression_p (tree expression
)
26132 return (instantiation_dependent_uneval_expression_p (expression
)
26133 || value_dependent_expression_p (expression
));
26136 /* Like type_dependent_expression_p, but it also works while not processing
26137 a template definition, i.e. during substitution or mangling. */
26140 type_dependent_expression_p_push (tree expr
)
26143 ++processing_template_decl
;
26144 b
= type_dependent_expression_p (expr
);
26145 --processing_template_decl
;
26149 /* Returns TRUE if ARGS contains a type-dependent expression. */
26152 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
26157 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
26159 if (type_dependent_expression_p (arg
))
26165 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26166 expressions) contains any type-dependent expressions. */
26169 any_type_dependent_elements_p (const_tree list
)
26171 for (; list
; list
= TREE_CHAIN (list
))
26172 if (type_dependent_expression_p (TREE_VALUE (list
)))
26178 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26179 expressions) contains any value-dependent expressions. */
26182 any_value_dependent_elements_p (const_tree list
)
26184 for (; list
; list
= TREE_CHAIN (list
))
26185 if (value_dependent_expression_p (TREE_VALUE (list
)))
26191 /* Returns TRUE if the ARG (a template argument) is dependent. */
26194 dependent_template_arg_p (tree arg
)
26196 if (!processing_template_decl
)
26199 /* Assume a template argument that was wrongly written by the user
26200 is dependent. This is consistent with what
26201 any_dependent_template_arguments_p [that calls this function]
26203 if (!arg
|| arg
== error_mark_node
)
26206 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
26207 arg
= argument_pack_select_arg (arg
);
26209 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
26211 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26213 if (DECL_TEMPLATE_PARM_P (arg
))
26215 /* A member template of a dependent class is not necessarily
26216 type-dependent, but it is a dependent template argument because it
26217 will be a member of an unknown specialization to that template. */
26218 tree scope
= CP_DECL_CONTEXT (arg
);
26219 return TYPE_P (scope
) && dependent_type_p (scope
);
26221 else if (ARGUMENT_PACK_P (arg
))
26223 tree args
= ARGUMENT_PACK_ARGS (arg
);
26224 int i
, len
= TREE_VEC_LENGTH (args
);
26225 for (i
= 0; i
< len
; ++i
)
26227 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
26233 else if (TYPE_P (arg
))
26234 return dependent_type_p (arg
);
26236 return (type_dependent_expression_p (arg
)
26237 || value_dependent_expression_p (arg
));
26240 /* Returns true if ARGS (a collection of template arguments) contains
26241 any types that require structural equality testing. */
26244 any_template_arguments_need_structural_equality_p (tree args
)
26251 if (args
== error_mark_node
)
26254 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26256 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26257 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26259 tree arg
= TREE_VEC_ELT (level
, j
);
26260 tree packed_args
= NULL_TREE
;
26263 if (ARGUMENT_PACK_P (arg
))
26265 /* Look inside the argument pack. */
26266 packed_args
= ARGUMENT_PACK_ARGS (arg
);
26267 len
= TREE_VEC_LENGTH (packed_args
);
26270 for (k
= 0; k
< len
; ++k
)
26273 arg
= TREE_VEC_ELT (packed_args
, k
);
26275 if (error_operand_p (arg
))
26277 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
26279 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
26281 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
26282 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
26291 /* Returns true if ARGS (a collection of template arguments) contains
26292 any dependent arguments. */
26295 any_dependent_template_arguments_p (const_tree args
)
26302 if (args
== error_mark_node
)
26305 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26307 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26308 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26309 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
26316 /* Returns true if ARGS contains any errors. */
26319 any_erroneous_template_args_p (const_tree args
)
26324 if (args
== error_mark_node
)
26327 if (args
&& TREE_CODE (args
) != TREE_VEC
)
26329 if (tree ti
= get_template_info (args
))
26330 args
= TI_ARGS (ti
);
26338 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
26340 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
26341 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
26342 if (error_operand_p (TREE_VEC_ELT (level
, j
)))
26349 /* Returns TRUE if the template TMPL is type-dependent. */
26352 dependent_template_p (tree tmpl
)
26354 if (TREE_CODE (tmpl
) == OVERLOAD
)
26356 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
26357 if (dependent_template_p (*iter
))
26362 /* Template template parameters are dependent. */
26363 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
26364 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
26366 /* So are names that have not been looked up. */
26367 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
26372 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26375 dependent_template_id_p (tree tmpl
, tree args
)
26377 return (dependent_template_p (tmpl
)
26378 || any_dependent_template_arguments_p (args
));
26381 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26385 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
26389 if (!processing_template_decl
)
26392 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
26394 tree decl
= TREE_VEC_ELT (declv
, i
);
26395 tree init
= TREE_VEC_ELT (initv
, i
);
26396 tree cond
= TREE_VEC_ELT (condv
, i
);
26397 tree incr
= TREE_VEC_ELT (incrv
, i
);
26399 if (type_dependent_expression_p (decl
)
26400 || TREE_CODE (decl
) == SCOPE_REF
)
26403 if (init
&& type_dependent_expression_p (init
))
26406 if (cond
== global_namespace
)
26409 if (type_dependent_expression_p (cond
))
26412 if (COMPARISON_CLASS_P (cond
)
26413 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
26414 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
26417 if (TREE_CODE (incr
) == MODOP_EXPR
)
26419 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
26420 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
26423 else if (type_dependent_expression_p (incr
))
26425 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
26427 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
26429 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
26431 tree t
= TREE_OPERAND (incr
, 1);
26432 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
26433 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
26436 /* If this loop has a class iterator with != comparison
26437 with increment other than i++/++i/i--/--i, make sure the
26438 increment is constant. */
26439 if (CLASS_TYPE_P (TREE_TYPE (decl
))
26440 && TREE_CODE (cond
) == NE_EXPR
)
26442 if (TREE_OPERAND (t
, 0) == decl
)
26443 t
= TREE_OPERAND (t
, 1);
26445 t
= TREE_OPERAND (t
, 0);
26446 if (TREE_CODE (t
) != INTEGER_CST
)
26456 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26457 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26458 no such TYPE can be found. Note that this function peers inside
26459 uninstantiated templates and therefore should be used only in
26460 extremely limited situations. ONLY_CURRENT_P restricts this
26461 peering to the currently open classes hierarchy (which is required
26462 when comparing types). */
26465 resolve_typename_type (tree type
, bool only_current_p
)
26474 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
26476 scope
= TYPE_CONTEXT (type
);
26477 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26478 gcc_checking_assert (uses_template_parms (scope
));
26480 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26481 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26482 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26483 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26484 identifier of the TYPENAME_TYPE anymore.
26485 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26486 TYPENAME_TYPE instead, we avoid messing up with a possible
26487 typedef variant case. */
26488 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
26490 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26491 it first before we can figure out what NAME refers to. */
26492 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
26494 if (TYPENAME_IS_RESOLVING_P (scope
))
26495 /* Given a class template A with a dependent base with nested type C,
26496 typedef typename A::C::C C will land us here, as trying to resolve
26497 the initial A::C leads to the local C typedef, which leads back to
26498 A::C::C. So we break the recursion now. */
26501 scope
= resolve_typename_type (scope
, only_current_p
);
26503 /* If we don't know what SCOPE refers to, then we cannot resolve the
26505 if (!CLASS_TYPE_P (scope
))
26507 /* If this is a typedef, we don't want to look inside (c++/11987). */
26508 if (typedef_variant_p (type
))
26510 /* If SCOPE isn't the template itself, it will not have a valid
26511 TYPE_FIELDS list. */
26512 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
26513 /* scope is either the template itself or a compatible instantiation
26514 like X<T>, so look up the name in the original template. */
26515 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
26516 /* If scope has no fields, it can't be a current instantiation. Check this
26517 before currently_open_class to avoid infinite recursion (71515). */
26518 if (!TYPE_FIELDS (scope
))
26520 /* If the SCOPE is not the current instantiation, there's no reason
26521 to look inside it. */
26522 if (only_current_p
&& !currently_open_class (scope
))
26524 /* Enter the SCOPE so that name lookup will be resolved as if we
26525 were in the class definition. In particular, SCOPE will no
26526 longer be considered a dependent type. */
26527 pushed_scope
= push_scope (scope
);
26528 /* Look up the declaration. */
26529 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
26530 tf_warning_or_error
);
26532 result
= NULL_TREE
;
26534 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26535 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26536 tree fullname
= TYPENAME_TYPE_FULLNAME (type
);
26539 else if (identifier_p (fullname
)
26540 && TREE_CODE (decl
) == TYPE_DECL
)
26542 result
= TREE_TYPE (decl
);
26543 if (result
== error_mark_node
)
26544 result
= NULL_TREE
;
26546 else if (TREE_CODE (fullname
) == TEMPLATE_ID_EXPR
26547 && DECL_CLASS_TEMPLATE_P (decl
))
26549 /* Obtain the template and the arguments. */
26550 tree tmpl
= TREE_OPERAND (fullname
, 0);
26551 if (TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
26553 /* We get here with a plain identifier because a previous tentative
26554 parse of the nested-name-specifier as part of a ptr-operator saw
26555 ::template X<A>. The use of ::template is necessary in a
26556 ptr-operator, but wrong in a declarator-id.
26558 [temp.names]: In a qualified-id of a declarator-id, the keyword
26559 template shall not appear at the top level. */
26560 pedwarn (cp_expr_loc_or_loc (fullname
, input_location
), OPT_Wpedantic
,
26561 "keyword %<template%> not allowed in declarator-id");
26564 tree args
= TREE_OPERAND (fullname
, 1);
26565 /* Instantiate the template. */
26566 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
26567 /*entering_scope=*/true,
26568 tf_error
| tf_user
);
26569 if (result
== error_mark_node
)
26570 result
= NULL_TREE
;
26573 /* Leave the SCOPE. */
26575 pop_scope (pushed_scope
);
26577 /* If we failed to resolve it, return the original typename. */
26581 /* If lookup found a typename type, resolve that too. */
26582 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
26584 /* Ill-formed programs can cause infinite recursion here, so we
26585 must catch that. */
26586 TYPENAME_IS_RESOLVING_P (result
) = 1;
26587 result
= resolve_typename_type (result
, only_current_p
);
26588 TYPENAME_IS_RESOLVING_P (result
) = 0;
26591 /* Qualify the resulting type. */
26592 quals
= cp_type_quals (type
);
26594 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
26599 /* EXPR is an expression which is not type-dependent. Return a proxy
26600 for EXPR that can be used to compute the types of larger
26601 expressions containing EXPR. */
26604 build_non_dependent_expr (tree expr
)
26606 tree orig_expr
= expr
;
26609 /* When checking, try to get a constant value for all non-dependent
26610 expressions in order to expose bugs in *_dependent_expression_p
26611 and constexpr. This can affect code generation, see PR70704, so
26612 only do this for -fchecking=2. */
26613 if (flag_checking
> 1
26614 && cxx_dialect
>= cxx11
26615 /* Don't do this during nsdmi parsing as it can lead to
26616 unexpected recursive instantiations. */
26617 && !parsing_nsdmi ()
26618 /* Don't do this during concept expansion either and for
26619 the same reason. */
26620 && !expanding_concept ())
26621 fold_non_dependent_expr (expr
, tf_none
);
26623 STRIP_ANY_LOCATION_WRAPPER (expr
);
26625 /* Preserve OVERLOADs; the functions must be available to resolve
26628 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
26629 inner_expr
= stmt_expr_value_expr (inner_expr
);
26630 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
26631 inner_expr
= TREE_OPERAND (inner_expr
, 0);
26632 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
26633 inner_expr
= TREE_OPERAND (inner_expr
, 1);
26634 if (is_overloaded_fn (inner_expr
)
26635 || TREE_CODE (inner_expr
) == OFFSET_REF
)
26637 /* There is no need to return a proxy for a variable or enumerator. */
26638 if (VAR_P (expr
) || TREE_CODE (expr
) == CONST_DECL
)
26640 /* Preserve string constants; conversions from string constants to
26641 "char *" are allowed, even though normally a "const char *"
26642 cannot be used to initialize a "char *". */
26643 if (TREE_CODE (expr
) == STRING_CST
)
26645 /* Preserve void and arithmetic constants, as an optimization -- there is no
26646 reason to create a new node. */
26647 if (TREE_CODE (expr
) == VOID_CST
26648 || TREE_CODE (expr
) == INTEGER_CST
26649 || TREE_CODE (expr
) == REAL_CST
)
26651 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26652 There is at least one place where we want to know that a
26653 particular expression is a throw-expression: when checking a ?:
26654 expression, there are special rules if the second or third
26655 argument is a throw-expression. */
26656 if (TREE_CODE (expr
) == THROW_EXPR
)
26659 /* Don't wrap an initializer list, we need to be able to look inside. */
26660 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
26663 /* Don't wrap a dummy object, we need to be able to test for it. */
26664 if (is_dummy_object (expr
))
26667 if (TREE_CODE (expr
) == COND_EXPR
)
26668 return build3 (COND_EXPR
,
26670 TREE_OPERAND (expr
, 0),
26671 (TREE_OPERAND (expr
, 1)
26672 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
26673 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
26674 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
26675 if (TREE_CODE (expr
) == COMPOUND_EXPR
26676 && !COMPOUND_EXPR_OVERLOADED (expr
))
26677 return build2 (COMPOUND_EXPR
,
26679 TREE_OPERAND (expr
, 0),
26680 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
26682 /* If the type is unknown, it can't really be non-dependent */
26683 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
26685 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26686 return build1_loc (EXPR_LOCATION (orig_expr
), NON_DEPENDENT_EXPR
,
26687 TREE_TYPE (expr
), expr
);
26690 /* ARGS is a vector of expressions as arguments to a function call.
26691 Replace the arguments with equivalent non-dependent expressions.
26692 This modifies ARGS in place. */
26695 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
26700 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
26702 tree newarg
= build_non_dependent_expr (arg
);
26704 (*args
)[ix
] = newarg
;
26708 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26709 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26710 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26713 make_auto_1 (tree name
, bool set_canonical
)
26715 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
26716 TYPE_NAME (au
) = build_decl (input_location
,
26717 TYPE_DECL
, name
, au
);
26718 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
26719 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
26720 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
26721 TYPE_NAME (au
), NULL_TREE
);
26723 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
26724 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
26725 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
26731 make_decltype_auto (void)
26733 return make_auto_1 (decltype_auto_identifier
, true);
26739 return make_auto_1 (auto_identifier
, true);
26742 /* Return a C++17 deduction placeholder for class template TMPL. */
26745 make_template_placeholder (tree tmpl
)
26747 tree t
= make_auto_1 (auto_identifier
, false);
26748 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
26749 /* Our canonical type depends on the placeholder. */
26750 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
26754 /* True iff T is a C++17 class template deduction placeholder. */
26757 template_placeholder_p (tree t
)
26759 return is_auto (t
) && CLASS_PLACEHOLDER_TEMPLATE (t
);
26762 /* Make a "constrained auto" type-specifier. This is an
26763 auto type with constraints that must be associated after
26764 deduction. The constraint is formed from the given
26765 CONC and its optional sequence of arguments, which are
26766 non-null if written as partial-concept-id. */
26769 make_constrained_auto (tree con
, tree args
)
26771 tree type
= make_auto_1 (auto_identifier
, false);
26773 /* Build the constraint. */
26774 tree tmpl
= DECL_TI_TEMPLATE (con
);
26775 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
26776 expr
= build_concept_check (expr
, type
, args
);
26778 tree constr
= normalize_expression (expr
);
26779 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
26781 /* Our canonical type depends on the constraint. */
26782 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
26784 /* Attach the constraint to the type declaration. */
26785 tree decl
= TYPE_NAME (type
);
26789 /* Given type ARG, return std::initializer_list<ARG>. */
26794 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
26796 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
26798 gcc_rich_location
richloc (input_location
);
26799 maybe_add_include_fixit (&richloc
, "<initializer_list>", false);
26800 error_at (&richloc
,
26801 "deducing from brace-enclosed initializer list"
26802 " requires %<#include <initializer_list>%>");
26804 return error_mark_node
;
26806 tree argvec
= make_tree_vec (1);
26807 TREE_VEC_ELT (argvec
, 0) = arg
;
26809 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
26810 NULL_TREE
, 0, tf_warning_or_error
);
26813 /* Replace auto in TYPE with std::initializer_list<auto>. */
26816 listify_autos (tree type
, tree auto_node
)
26818 tree init_auto
= listify (auto_node
);
26819 tree argvec
= make_tree_vec (1);
26820 TREE_VEC_ELT (argvec
, 0) = init_auto
;
26821 if (processing_template_decl
)
26822 argvec
= add_to_template_args (current_template_args (), argvec
);
26823 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
26826 /* Hash traits for hashing possibly constrained 'auto'
26827 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26829 struct auto_hash
: default_hash_traits
<tree
>
26831 static inline hashval_t
hash (tree
);
26832 static inline bool equal (tree
, tree
);
26835 /* Hash the 'auto' T. */
26838 auto_hash::hash (tree t
)
26840 if (tree c
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t
)))
26841 /* Matching constrained-type-specifiers denote the same template
26842 parameter, so hash the constraint. */
26843 return hash_placeholder_constraint (c
);
26845 /* But unconstrained autos are all separate, so just hash the pointer. */
26846 return iterative_hash_object (t
, 0);
26849 /* Compare two 'auto's. */
26852 auto_hash::equal (tree t1
, tree t2
)
26857 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
26858 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
26860 /* Two unconstrained autos are distinct. */
26864 return equivalent_placeholder_constraints (c1
, c2
);
26867 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26868 constrained) auto, add it to the vector. */
26871 extract_autos_r (tree t
, void *data
)
26873 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
26876 /* All the autos were built with index 0; fix that up now. */
26877 tree
*p
= hash
.find_slot (t
, INSERT
);
26880 /* If this is a repeated constrained-type-specifier, use the index we
26882 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
26885 /* Otherwise this is new, so use the current count. */
26887 idx
= hash
.elements () - 1;
26889 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
26892 /* Always keep walking. */
26896 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26897 says they can appear anywhere in the type. */
26900 extract_autos (tree type
)
26902 hash_set
<tree
> visited
;
26903 hash_table
<auto_hash
> hash (2);
26905 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
26907 tree tree_vec
= make_tree_vec (hash
.elements());
26908 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
26909 iter
!= hash
.end(); ++iter
)
26912 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
26913 TREE_VEC_ELT (tree_vec
, i
)
26914 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
26920 /* The stem for deduction guide names. */
26921 const char *const dguide_base
= "__dguide_";
26923 /* Return the name for a deduction guide for class template TMPL. */
26926 dguide_name (tree tmpl
)
26928 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
26929 tree tname
= TYPE_IDENTIFIER (type
);
26930 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
26931 + IDENTIFIER_LENGTH (tname
));
26932 memcpy (buf
, dguide_base
, strlen (dguide_base
));
26933 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
26934 IDENTIFIER_LENGTH (tname
) + 1);
26935 tree dname
= get_identifier (buf
);
26936 TREE_TYPE (dname
) = type
;
26940 /* True if NAME is the name of a deduction guide. */
26943 dguide_name_p (tree name
)
26945 return (TREE_CODE (name
) == IDENTIFIER_NODE
26946 && TREE_TYPE (name
)
26947 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
26948 strlen (dguide_base
)));
26951 /* True if FN is a deduction guide. */
26954 deduction_guide_p (const_tree fn
)
26957 if (tree name
= DECL_NAME (fn
))
26958 return dguide_name_p (name
);
26962 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26965 copy_guide_p (const_tree fn
)
26967 gcc_assert (deduction_guide_p (fn
));
26968 if (!DECL_ARTIFICIAL (fn
))
26970 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
26971 return (TREE_CHAIN (parms
) == void_list_node
26972 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
26975 /* True if FN is a guide generated from a constructor template. */
26978 template_guide_p (const_tree fn
)
26980 gcc_assert (deduction_guide_p (fn
));
26981 if (!DECL_ARTIFICIAL (fn
))
26983 tree tmpl
= DECL_TI_TEMPLATE (fn
);
26984 if (tree org
= DECL_ABSTRACT_ORIGIN (tmpl
))
26985 return PRIMARY_TEMPLATE_P (org
);
26989 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26990 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26991 template parameter types. Note that the handling of template template
26992 parameters relies on current_template_parms being set appropriately for the
26996 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
26997 tree tsubst_args
, tsubst_flags_t complain
)
26999 if (olddecl
== error_mark_node
)
27000 return error_mark_node
;
27002 tree oldidx
= get_template_parm_index (olddecl
);
27005 if (TREE_CODE (olddecl
) == TYPE_DECL
27006 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27008 tree oldtype
= TREE_TYPE (olddecl
);
27009 newtype
= cxx_make_type (TREE_CODE (oldtype
));
27010 TYPE_MAIN_VARIANT (newtype
) = newtype
;
27011 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
27012 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
27013 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
27017 newtype
= TREE_TYPE (olddecl
);
27018 if (type_uses_auto (newtype
))
27020 // Substitute once to fix references to other template parameters.
27021 newtype
= tsubst (newtype
, tsubst_args
,
27022 complain
|tf_partial
, NULL_TREE
);
27023 // Now substitute again to reduce the level of the auto.
27024 newtype
= tsubst (newtype
, current_template_args (),
27025 complain
, NULL_TREE
);
27028 newtype
= tsubst (newtype
, tsubst_args
,
27029 complain
, NULL_TREE
);
27033 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
27034 DECL_NAME (olddecl
), newtype
);
27035 SET_DECL_TEMPLATE_PARM_P (newdecl
);
27038 if (TREE_CODE (olddecl
) == TYPE_DECL
27039 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27041 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
27042 = build_template_parm_index (index
, level
, level
,
27044 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27045 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27046 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
27047 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
27049 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
27051 DECL_TEMPLATE_RESULT (newdecl
)
27052 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
27053 DECL_NAME (olddecl
), newtype
);
27054 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
27055 // First create a copy (ttargs) of tsubst_args with an
27056 // additional level for the template template parameter's own
27057 // template parameters (ttparms).
27058 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
27059 (DECL_TEMPLATE_PARMS (olddecl
)));
27060 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
27061 tree ttargs
= make_tree_vec (depth
+ 1);
27062 for (int i
= 0; i
< depth
; ++i
)
27063 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
27064 TREE_VEC_ELT (ttargs
, depth
)
27065 = template_parms_level_to_args (ttparms
);
27066 // Substitute ttargs into ttparms to fix references to
27067 // other template parameters.
27068 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27069 complain
|tf_partial
);
27070 // Now substitute again with args based on tparms, to reduce
27071 // the level of the ttparms.
27072 ttargs
= current_template_args ();
27073 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
27075 // Finally, tack the adjusted parms onto tparms.
27076 ttparms
= tree_cons (size_int (depth
), ttparms
,
27077 current_template_parms
);
27078 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
27083 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
27085 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
27086 TREE_CODE (oldconst
),
27087 DECL_NAME (oldconst
), newtype
);
27088 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
27089 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
27090 SET_DECL_TEMPLATE_PARM_P (newconst
);
27091 newidx
= build_template_parm_index (index
, level
, level
,
27092 newconst
, newtype
);
27093 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
27094 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
27095 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
27101 /* Returns a C++17 class deduction guide template based on the constructor
27102 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27103 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27106 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
27108 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
27109 bool memtmpl
= false;
27112 tree fn_tmpl
= NULL_TREE
;
27117 bool copy_p
= TYPE_REF_P (type
);
27120 type
= TREE_TYPE (type
);
27121 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
27124 fparms
= void_list_node
;
27126 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
27127 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
27128 targs
= CLASSTYPE_TI_ARGS (type
);
27131 loc
= DECL_SOURCE_LOCATION (ctmpl
);
27132 explicit_p
= false;
27136 ++processing_template_decl
;
27140 = (TREE_CODE (ctor
) == TEMPLATE_DECL
? ctor
27141 : DECL_TI_TEMPLATE (ctor
));
27143 fn_tmpl
= tsubst (fn_tmpl
, outer_args
, complain
, ctor
);
27144 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
27146 type
= DECL_CONTEXT (ctor
);
27148 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
27149 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27150 fully specialized args for the enclosing class. Strip those off, as
27151 the deduction guide won't have those template parameters. */
27152 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
27153 TMPL_PARMS_DEPTH (tparms
));
27154 /* Discard the 'this' parameter. */
27155 fparms
= FUNCTION_ARG_CHAIN (ctor
);
27156 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
27157 ci
= get_constraints (ctor
);
27158 loc
= DECL_SOURCE_LOCATION (ctor
);
27159 explicit_p
= DECL_NONCONVERTING_P (ctor
);
27161 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
27165 /* For a member template constructor, we need to flatten the two
27166 template parameter lists into one, and then adjust the function
27167 signature accordingly. This gets...complicated. */
27168 tree save_parms
= current_template_parms
;
27170 /* For a member template we should have two levels of parms/args, one
27171 for the class and one for the constructor. We stripped
27172 specialized args for further enclosing classes above. */
27173 const int depth
= 2;
27174 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
27176 /* Template args for translating references to the two-level template
27177 parameters into references to the one-level template parameters we
27179 tree tsubst_args
= copy_node (targs
);
27180 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
27181 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
27183 /* Template parms for the constructor template. */
27184 tree ftparms
= TREE_VALUE (tparms
);
27185 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
27186 /* Template parms for the class template. */
27187 tparms
= TREE_CHAIN (tparms
);
27188 tree ctparms
= TREE_VALUE (tparms
);
27189 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
27190 /* Template parms for the deduction guide start as a copy of the
27191 template parms for the class. We set current_template_parms for
27192 lookup_template_class_1. */
27193 current_template_parms
= tparms
= copy_node (tparms
);
27194 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
27195 for (unsigned i
= 0; i
< clen
; ++i
)
27196 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
27198 /* Now we need to rewrite the constructor parms to append them to the
27200 for (unsigned i
= 0; i
< flen
; ++i
)
27202 unsigned index
= i
+ clen
;
27203 unsigned level
= 1;
27204 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
27205 tree olddecl
= TREE_VALUE (oldelt
);
27206 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
27207 tsubst_args
, complain
);
27208 if (newdecl
== error_mark_node
)
27210 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
27211 tsubst_args
, complain
, ctor
);
27212 tree list
= build_tree_list (newdef
, newdecl
);
27213 TEMPLATE_PARM_CONSTRAINTS (list
)
27214 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
27215 tsubst_args
, complain
, ctor
);
27216 TREE_VEC_ELT (new_vec
, index
) = list
;
27217 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
27220 /* Now we have a final set of template parms to substitute into the
27221 function signature. */
27222 targs
= template_parms_to_args (tparms
);
27223 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
27225 if (fparms
== error_mark_node
)
27227 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
27229 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
27231 current_template_parms
= save_parms
;
27234 --processing_template_decl
;
27236 return error_mark_node
;
27241 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27242 tparms
= copy_node (tparms
);
27243 INNERMOST_TEMPLATE_PARMS (tparms
)
27244 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
27247 tree fntype
= build_function_type (type
, fparms
);
27248 tree ded_fn
= build_lang_decl_loc (loc
,
27250 dguide_name (type
), fntype
);
27251 DECL_ARGUMENTS (ded_fn
) = fargs
;
27252 DECL_ARTIFICIAL (ded_fn
) = true;
27253 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
27254 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
27255 DECL_ARTIFICIAL (ded_tmpl
) = true;
27256 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
27257 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
27258 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
27259 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
27261 DECL_ABSTRACT_ORIGIN (ded_tmpl
) = fn_tmpl
;
27263 set_constraints (ded_tmpl
, ci
);
27268 /* Deduce template arguments for the class template placeholder PTYPE for
27269 template TMPL based on the initializer INIT, and return the resulting
27273 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
27274 tsubst_flags_t complain
)
27276 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
27278 /* We should have handled this in the caller. */
27279 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27281 if (complain
& tf_error
)
27282 error ("non-class template %qT used without template arguments", tmpl
);
27283 return error_mark_node
;
27285 if (init
&& TREE_TYPE (init
) == ptype
)
27286 /* Using the template parm as its own argument. */
27289 tree type
= TREE_TYPE (tmpl
);
27291 bool try_list_ctor
= false;
27293 vec
<tree
,va_gc
> *args
;
27294 if (init
== NULL_TREE
27295 || TREE_CODE (init
) == TREE_LIST
)
27296 args
= make_tree_vector_from_list (init
);
27297 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27299 try_list_ctor
= TYPE_HAS_LIST_CTOR (type
);
27300 if (try_list_ctor
&& CONSTRUCTOR_NELTS (init
) == 1)
27302 /* As an exception, the first phase in 16.3.1.7 (considering the
27303 initializer list as a single argument) is omitted if the
27304 initializer list consists of a single expression of type cv U,
27305 where U is a specialization of C or a class derived from a
27306 specialization of C. */
27307 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
27308 tree etype
= TREE_TYPE (elt
);
27310 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
27311 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27312 int err
= unify (tparms
, targs
, type
, etype
,
27313 UNIFY_ALLOW_DERIVED
, /*explain*/false);
27315 try_list_ctor
= false;
27318 if (try_list_ctor
|| is_std_init_list (type
))
27319 args
= make_tree_vector_single (init
);
27321 args
= make_tree_vector_from_ctor (init
);
27324 args
= make_tree_vector_single (init
);
27326 tree dname
= dguide_name (tmpl
);
27327 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
27328 /*type*/false, /*complain*/false,
27330 bool elided
= false;
27331 if (cands
== error_mark_node
)
27334 /* Prune explicit deduction guides in copy-initialization context. */
27335 if (flags
& LOOKUP_ONLYCONVERTING
)
27337 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
27338 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27343 /* Found a nonconverting guide, prune the candidates. */
27344 tree pruned
= NULL_TREE
;
27345 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27346 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
27347 pruned
= lookup_add (*iter
, pruned
);
27353 tree outer_args
= NULL_TREE
;
27354 if (DECL_CLASS_SCOPE_P (tmpl
)
27355 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl
)))
27357 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
27358 type
= TREE_TYPE (most_general_template (tmpl
));
27361 bool saw_ctor
= false;
27362 // FIXME cache artificial deduction guides
27363 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
)); iter
; ++iter
)
27365 /* Skip inherited constructors. */
27366 if (iter
.using_p ())
27369 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
27370 if (guide
== error_mark_node
)
27371 return error_mark_node
;
27372 if ((flags
& LOOKUP_ONLYCONVERTING
)
27373 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
27376 cands
= lookup_add (guide
, cands
);
27381 tree call
= error_mark_node
;
27383 /* If this is list-initialization and the class has a list constructor, first
27384 try deducing from the list as a single argument, as [over.match.list]. */
27385 tree list_cands
= NULL_TREE
;
27386 if (try_list_ctor
&& cands
)
27387 for (lkp_iterator
iter (cands
); iter
; ++iter
)
27390 if (is_list_ctor (dg
))
27391 list_cands
= lookup_add (dg
, list_cands
);
27395 ++cp_unevaluated_operand
;
27396 call
= build_new_function_call (list_cands
, &args
, tf_decltype
);
27397 --cp_unevaluated_operand
;
27399 if (call
== error_mark_node
)
27401 /* That didn't work, now try treating the list as a sequence of
27403 release_tree_vector (args
);
27404 args
= make_tree_vector_from_ctor (init
);
27408 /* Maybe generate an implicit deduction guide. */
27409 if (call
== error_mark_node
&& args
->length () < 2)
27411 tree gtype
= NULL_TREE
;
27413 if (args
->length () == 1)
27414 /* Generate a copy guide. */
27415 gtype
= build_reference_type (type
);
27416 else if (!saw_ctor
)
27417 /* Generate a default guide. */
27422 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
27423 if (guide
== error_mark_node
)
27424 return error_mark_node
;
27425 cands
= lookup_add (guide
, cands
);
27429 if (elided
&& !cands
)
27431 error ("cannot deduce template arguments for copy-initialization"
27432 " of %qT, as it has no non-explicit deduction guides or "
27433 "user-declared constructors", type
);
27434 return error_mark_node
;
27436 else if (!cands
&& call
== error_mark_node
)
27438 error ("cannot deduce template arguments of %qT, as it has no viable "
27439 "deduction guides", type
);
27440 return error_mark_node
;
27443 if (call
== error_mark_node
)
27445 ++cp_unevaluated_operand
;
27446 call
= build_new_function_call (cands
, &args
, tf_decltype
);
27447 --cp_unevaluated_operand
;
27450 if (call
== error_mark_node
&& (complain
& tf_warning_or_error
))
27452 error ("class template argument deduction failed:");
27454 ++cp_unevaluated_operand
;
27455 call
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
27456 --cp_unevaluated_operand
;
27459 inform (input_location
, "explicit deduction guides not considered "
27460 "for copy-initialization");
27463 release_tree_vector (args
);
27465 return cp_build_qualified_type (TREE_TYPE (call
), cp_type_quals (ptype
));
27468 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27469 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27470 The CONTEXT determines the context in which auto deduction is performed
27471 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27472 OUTER_TARGS are used during template argument deduction
27473 (context == adc_unify) to properly substitute the result, and is ignored
27476 For partial-concept-ids, extra args may be appended to the list of deduced
27477 template arguments prior to determining constraint satisfaction. */
27480 do_auto_deduction (tree type
, tree init
, tree auto_node
,
27481 tsubst_flags_t complain
, auto_deduction_context context
,
27482 tree outer_targs
, int flags
)
27486 if (init
== error_mark_node
)
27487 return error_mark_node
;
27489 if (init
&& type_dependent_expression_p (init
)
27490 && context
!= adc_unify
)
27491 /* Defining a subset of type-dependent expressions that we can deduce
27492 from ahead of time isn't worth the trouble. */
27495 /* Similarly, we can't deduce from another undeduced decl. */
27496 if (init
&& undeduced_auto_decl (init
))
27499 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
27500 /* C++17 class template argument deduction. */
27501 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
27503 if (init
== NULL_TREE
|| TREE_TYPE (init
) == NULL_TREE
)
27504 /* Nothing we can do with this, even in deduction context. */
27507 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27508 with either a new invented type template parameter U or, if the
27509 initializer is a braced-init-list (8.5.4), with
27510 std::initializer_list<U>. */
27511 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
27513 if (!DIRECT_LIST_INIT_P (init
))
27514 type
= listify_autos (type
, auto_node
);
27515 else if (CONSTRUCTOR_NELTS (init
) == 1)
27516 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
27519 if (complain
& tf_warning_or_error
)
27521 if (permerror (input_location
, "direct-list-initialization of "
27522 "%<auto%> requires exactly one element"))
27523 inform (input_location
,
27524 "for deduction to %<std::initializer_list%>, use copy-"
27525 "list-initialization (i.e. add %<=%> before the %<{%>)");
27527 type
= listify_autos (type
, auto_node
);
27531 if (type
== error_mark_node
)
27532 return error_mark_node
;
27534 init
= resolve_nondeduced_context (init
, complain
);
27536 if (context
== adc_decomp_type
27537 && auto_node
== type
27538 && init
!= error_mark_node
27539 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
27540 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27541 and initializer has array type, deduce cv-qualified array type. */
27542 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
27544 else if (AUTO_IS_DECLTYPE (auto_node
))
27546 tree stripped_init
= tree_strip_any_location_wrapper (init
);
27547 bool id
= (DECL_P (stripped_init
)
27548 || ((TREE_CODE (init
) == COMPONENT_REF
27549 || TREE_CODE (init
) == SCOPE_REF
)
27550 && !REF_PARENTHESIZED_P (init
)));
27551 targs
= make_tree_vec (1);
27552 TREE_VEC_ELT (targs
, 0)
27553 = finish_decltype_type (init
, id
, tf_warning_or_error
);
27554 if (type
!= auto_node
)
27556 if (complain
& tf_error
)
27557 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
27558 return error_mark_node
;
27563 tree parms
= build_tree_list (NULL_TREE
, type
);
27567 tparms
= extract_autos (type
);
27570 tparms
= make_tree_vec (1);
27571 TREE_VEC_ELT (tparms
, 0)
27572 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
27575 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
27576 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27578 NULL
, /*explain_p=*/false);
27581 if (processing_template_decl
)
27582 /* Try again at instantiation time. */
27584 if (type
&& type
!= error_mark_node
27585 && (complain
& tf_error
))
27586 /* If type is error_mark_node a diagnostic must have been
27587 emitted by now. Also, having a mention to '<type error>'
27588 in the diagnostic is not really useful to the user. */
27590 if (cfun
&& auto_node
== current_function_auto_return_pattern
27591 && LAMBDA_FUNCTION_P (current_function_decl
))
27592 error ("unable to deduce lambda return type from %qE", init
);
27594 error ("unable to deduce %qT from %qE", type
, init
);
27595 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
27597 NULL
, /*explain_p=*/true);
27599 return error_mark_node
;
27603 /* Check any placeholder constraints against the deduced type. */
27604 if (flag_concepts
&& !processing_template_decl
)
27605 if (tree constr
= NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
)))
27607 /* Use the deduced type to check the associated constraints. If we
27608 have a partial-concept-id, rebuild the argument list so that
27609 we check using the extra arguments. */
27610 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
27611 tree cargs
= CHECK_CONSTR_ARGS (constr
);
27612 if (TREE_VEC_LENGTH (cargs
) > 1)
27614 cargs
= copy_node (cargs
);
27615 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
27619 if (!constraints_satisfied_p (constr
, cargs
))
27621 if (complain
& tf_warning_or_error
)
27623 auto_diagnostic_group d
;
27626 case adc_unspecified
:
27628 error("placeholder constraints not satisfied");
27630 case adc_variable_type
:
27631 case adc_decomp_type
:
27632 error ("deduced initializer does not satisfy "
27633 "placeholder constraints");
27635 case adc_return_type
:
27636 error ("deduced return type does not satisfy "
27637 "placeholder constraints");
27639 case adc_requirement
:
27640 error ("deduced expression type does not satisfy "
27641 "placeholder constraints");
27644 diagnose_constraints (input_location
, constr
, targs
);
27646 return error_mark_node
;
27650 if (processing_template_decl
&& context
!= adc_unify
)
27651 outer_targs
= current_template_args ();
27652 targs
= add_to_template_args (outer_targs
, targs
);
27653 return tsubst (type
, targs
, complain
, NULL_TREE
);
27656 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27660 splice_late_return_type (tree type
, tree late_return_type
)
27662 if (is_auto (type
))
27664 if (late_return_type
)
27665 return late_return_type
;
27667 tree idx
= get_template_parm_index (type
);
27668 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
27669 /* In an abbreviated function template we didn't know we were dealing
27670 with a function template when we saw the auto return type, so update
27671 it to have the correct level. */
27672 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
27677 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27678 'decltype(auto)' or a deduced class template. */
27681 is_auto (const_tree type
)
27683 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
27684 && (TYPE_IDENTIFIER (type
) == auto_identifier
27685 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
))
27691 /* for_each_template_parm callback for type_uses_auto. */
27694 is_auto_r (tree tp
, void */
*data*/
)
27696 return is_auto (tp
);
27699 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27700 a use of `auto'. Returns NULL_TREE otherwise. */
27703 type_uses_auto (tree type
)
27705 if (type
== NULL_TREE
)
27707 else if (flag_concepts
)
27709 /* The Concepts TS allows multiple autos in one type-specifier; just
27710 return the first one we find, do_auto_deduction will collect all of
27712 if (uses_template_parms (type
))
27713 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
27714 /*visited*/NULL
, /*nondeduced*/false);
27719 return find_type_usage (type
, is_auto
);
27722 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27723 concepts are enabled, auto is acceptable in template arguments, but
27724 only when TEMPL identifies a template class. Return TRUE if any
27725 such errors were reported. */
27728 check_auto_in_tmpl_args (tree tmpl
, tree args
)
27730 /* If there were previous errors, nevermind. */
27731 if (!args
|| TREE_CODE (args
) != TREE_VEC
)
27734 /* If TMPL is an identifier, we're parsing and we can't tell yet
27735 whether TMPL is supposed to be a type, a function or a variable.
27736 We'll only be able to tell during template substitution, so we
27737 expect to be called again then. If concepts are enabled and we
27738 know we have a type, we're ok. */
27740 && (identifier_p (tmpl
)
27742 && (DECL_TYPE_TEMPLATE_P (tmpl
)
27743 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))))
27746 /* Quickly search for any occurrences of auto; usually there won't
27747 be any, and then we'll avoid allocating the vector. */
27748 if (!type_uses_auto (args
))
27751 bool errors
= false;
27753 tree vec
= extract_autos (args
);
27754 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); i
++)
27756 tree xauto
= TREE_VALUE (TREE_VEC_ELT (vec
, i
));
27757 error_at (DECL_SOURCE_LOCATION (xauto
),
27758 "invalid use of %qT in template argument", xauto
);
27765 /* For a given template T, return the vector of typedefs referenced
27766 in T for which access check is needed at T instantiation time.
27767 T is either a FUNCTION_DECL or a RECORD_TYPE.
27768 Those typedefs were added to T by the function
27769 append_type_to_template_for_access_check. */
27771 vec
<qualified_typedef_usage_t
, va_gc
> *
27772 get_types_needing_access_check (tree t
)
27775 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
27777 if (!t
|| t
== error_mark_node
)
27780 if (!(ti
= get_template_info (t
)))
27783 if (CLASS_TYPE_P (t
)
27784 || TREE_CODE (t
) == FUNCTION_DECL
)
27786 if (!TI_TEMPLATE (ti
))
27789 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
27795 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27796 tied to T. That list of typedefs will be access checked at
27797 T instantiation time.
27798 T is either a FUNCTION_DECL or a RECORD_TYPE.
27799 TYPE_DECL is a TYPE_DECL node representing a typedef.
27800 SCOPE is the scope through which TYPE_DECL is accessed.
27801 LOCATION is the location of the usage point of TYPE_DECL.
27803 This function is a subroutine of
27804 append_type_to_template_for_access_check. */
27807 append_type_to_template_for_access_check_1 (tree t
,
27810 location_t location
)
27812 qualified_typedef_usage_t typedef_usage
;
27815 if (!t
|| t
== error_mark_node
)
27818 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
27819 || CLASS_TYPE_P (t
))
27821 && TREE_CODE (type_decl
) == TYPE_DECL
27824 if (!(ti
= get_template_info (t
)))
27827 gcc_assert (TI_TEMPLATE (ti
));
27829 typedef_usage
.typedef_decl
= type_decl
;
27830 typedef_usage
.context
= scope
;
27831 typedef_usage
.locus
= location
;
27833 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
27836 /* Append TYPE_DECL to the template TEMPL.
27837 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27838 At TEMPL instanciation time, TYPE_DECL will be checked to see
27839 if it can be accessed through SCOPE.
27840 LOCATION is the location of the usage point of TYPE_DECL.
27842 e.g. consider the following code snippet:
27849 template<class U> struct S
27851 C::myint mi; // <-- usage point of the typedef C::myint
27856 At S<char> instantiation time, we need to check the access of C::myint
27857 In other words, we need to check the access of the myint typedef through
27858 the C scope. For that purpose, this function will add the myint typedef
27859 and the scope C through which its being accessed to a list of typedefs
27860 tied to the template S. That list will be walked at template instantiation
27861 time and access check performed on each typedefs it contains.
27862 Note that this particular code snippet should yield an error because
27863 myint is private to C. */
27866 append_type_to_template_for_access_check (tree templ
,
27869 location_t location
)
27871 qualified_typedef_usage_t
*iter
;
27874 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
27876 /* Make sure we don't append the type to the template twice. */
27877 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
27878 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
27881 append_type_to_template_for_access_check_1 (templ
, type_decl
,
27885 /* Convert the generic type parameters in PARM that match the types given in the
27886 range [START_IDX, END_IDX) from the current_template_parms into generic type
27890 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
27892 tree current
= current_template_parms
;
27893 int depth
= TMPL_PARMS_DEPTH (current
);
27894 current
= INNERMOST_TEMPLATE_PARMS (current
);
27895 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
27897 for (int i
= 0; i
< start_idx
; ++i
)
27898 TREE_VEC_ELT (replacement
, i
)
27899 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27901 for (int i
= start_idx
; i
< end_idx
; ++i
)
27903 /* Create a distinct parameter pack type from the current parm and add it
27904 to the replacement args to tsubst below into the generic function
27907 tree o
= TREE_TYPE (TREE_VALUE
27908 (TREE_VEC_ELT (current
, i
)));
27909 tree t
= copy_type (o
);
27910 TEMPLATE_TYPE_PARM_INDEX (t
)
27911 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
27913 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
27914 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
27915 TYPE_MAIN_VARIANT (t
) = t
;
27916 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
27917 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
27918 TREE_VEC_ELT (replacement
, i
) = t
;
27919 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
27922 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
27923 TREE_VEC_ELT (replacement
, i
)
27924 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
27926 /* If there are more levels then build up the replacement with the outer
27929 replacement
= add_to_template_args (template_parms_to_args
27930 (TREE_CHAIN (current_template_parms
)),
27933 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
27936 /* Entries in the decl_constraint hash table. */
27937 struct GTY((for_user
)) constr_entry
27943 /* Hashing function and equality for constraint entries. */
27944 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
27946 static hashval_t
hash (constr_entry
*e
)
27948 return (hashval_t
)DECL_UID (e
->decl
);
27951 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
27953 return e1
->decl
== e2
->decl
;
27957 /* A mapping from declarations to constraint information. Note that
27958 both templates and their underlying declarations are mapped to the
27959 same constraint information.
27961 FIXME: This is defined in pt.c because garbage collection
27962 code is not being generated for constraint.cc. */
27964 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
27966 /* Returns the template constraints of declaration T. If T is not
27967 constrained, return NULL_TREE. Note that T must be non-null. */
27970 get_constraints (tree t
)
27972 if (!flag_concepts
)
27975 gcc_assert (DECL_P (t
));
27976 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27977 t
= DECL_TEMPLATE_RESULT (t
);
27978 constr_entry elt
= { t
, NULL_TREE
};
27979 constr_entry
* found
= decl_constraints
->find (&elt
);
27986 /* Associate the given constraint information CI with the declaration
27987 T. If T is a template, then the constraints are associated with
27988 its underlying declaration. Don't build associations if CI is
27992 set_constraints (tree t
, tree ci
)
27996 gcc_assert (t
&& flag_concepts
);
27997 if (TREE_CODE (t
) == TEMPLATE_DECL
)
27998 t
= DECL_TEMPLATE_RESULT (t
);
27999 gcc_assert (!get_constraints (t
));
28000 constr_entry elt
= {t
, ci
};
28001 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
28002 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
28007 /* Remove the associated constraints of the declaration T. */
28010 remove_constraints (tree t
)
28012 gcc_assert (DECL_P (t
));
28013 if (TREE_CODE (t
) == TEMPLATE_DECL
)
28014 t
= DECL_TEMPLATE_RESULT (t
);
28016 constr_entry elt
= {t
, NULL_TREE
};
28017 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
28019 decl_constraints
->clear_slot (slot
);
28022 /* Memoized satisfaction results for declarations. This
28023 maps the pair (constraint_info, arguments) to the result computed
28024 by constraints_satisfied_p. */
28026 struct GTY((for_user
)) constraint_sat_entry
28033 /* Hashing function and equality for constraint entries. */
28035 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
28037 static hashval_t
hash (constraint_sat_entry
*e
)
28039 hashval_t val
= iterative_hash_object(e
->ci
, 0);
28040 return iterative_hash_template_arg (e
->args
, val
);
28043 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
28045 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
28049 /* Memoized satisfaction results for concept checks. */
28051 struct GTY((for_user
)) concept_spec_entry
28058 /* Hashing function and equality for constraint entries. */
28060 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
28062 static hashval_t
hash (concept_spec_entry
*e
)
28064 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
28067 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
28069 ++comparing_specializations
;
28070 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
28071 --comparing_specializations
;
28076 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
28077 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
28079 /* Search for a memoized satisfaction result. Returns one of the
28080 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28083 lookup_constraint_satisfaction (tree ci
, tree args
)
28085 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
28086 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
28088 return found
->result
;
28093 /* Memoize the result of a satisfication test. Returns the saved result. */
28096 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
28098 constraint_sat_entry elt
= {ci
, args
, result
};
28099 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
28100 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
28106 /* Search for a memoized satisfaction result for a concept. */
28109 lookup_concept_satisfaction (tree tmpl
, tree args
)
28111 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
28112 concept_spec_entry
* found
= concept_memos
->find (&elt
);
28114 return found
->result
;
28119 /* Memoize the result of a concept check. Returns the saved result. */
28122 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
28124 concept_spec_entry elt
= {tmpl
, args
, result
};
28125 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
28126 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
28132 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
28134 /* Returns a prior concept specialization. This returns the substituted
28135 and normalized constraints defined by the concept. */
28138 get_concept_expansion (tree tmpl
, tree args
)
28140 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
28141 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
28143 return found
->result
;
28148 /* Save a concept expansion for later. */
28151 save_concept_expansion (tree tmpl
, tree args
, tree def
)
28153 concept_spec_entry elt
= {tmpl
, args
, def
};
28154 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
28155 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
28162 hash_subsumption_args (tree t1
, tree t2
)
28164 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
28165 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
28167 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
28168 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
28169 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
28170 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
28174 /* Compare the constraints of two subsumption entries. The LEFT1 and
28175 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28176 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28179 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
28181 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
28182 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
28183 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
28184 CHECK_CONSTR_ARGS (right1
)))
28185 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
28186 CHECK_CONSTR_ARGS (right2
));
28190 /* Key/value pair for learning and memoizing subsumption results. This
28191 associates a pair of check constraints (including arguments) with
28192 a boolean value indicating the result. */
28194 struct GTY((for_user
)) subsumption_entry
28201 /* Hashing function and equality for constraint entries. */
28203 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
28205 static hashval_t
hash (subsumption_entry
*e
)
28207 return hash_subsumption_args (e
->t1
, e
->t2
);
28210 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
28212 ++comparing_specializations
;
28213 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
28214 --comparing_specializations
;
28219 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
28221 /* Search for a previously cached subsumption result. */
28224 lookup_subsumption_result (tree t1
, tree t2
)
28226 subsumption_entry elt
= { t1
, t2
, false };
28227 subsumption_entry
* found
= subsumption_table
->find (&elt
);
28229 return &found
->result
;
28234 /* Save a subsumption result. */
28237 save_subsumption_result (tree t1
, tree t2
, bool result
)
28239 subsumption_entry elt
= {t1
, t2
, result
};
28240 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
28241 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
28247 /* Set up the hash table for constraint association. */
28250 init_constraint_processing (void)
28252 if (!flag_concepts
)
28255 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
28256 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
28257 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28258 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
28259 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
28262 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28266 declare_integer_pack (void)
28268 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
28269 build_function_type_list (integer_type_node
,
28272 NULL_TREE
, ECF_CONST
);
28273 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
28274 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
28275 DECL_FUNCTION_CODE (ipfn
)
28276 = (enum built_in_function
) (int) CP_BUILT_IN_INTEGER_PACK
;
28279 /* Set up the hash tables for template instantiations. */
28282 init_template_processing (void)
28284 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
28285 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
28287 if (cxx_dialect
>= cxx11
)
28288 declare_integer_pack ();
28291 /* Print stats about the template hash tables for -fstats. */
28294 print_template_statistics (void)
28296 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
28297 "%f collisions\n", (long) decl_specializations
->size (),
28298 (long) decl_specializations
->elements (),
28299 decl_specializations
->collisions ());
28300 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
28301 "%f collisions\n", (long) type_specializations
->size (),
28302 (long) type_specializations
->elements (),
28303 type_specializations
->collisions ());
28308 namespace selftest
{
28310 /* Verify that build_non_dependent_expr () works, for various expressions,
28311 and that location wrappers don't affect the results. */
28314 test_build_non_dependent_expr ()
28316 location_t loc
= BUILTINS_LOCATION
;
28318 /* Verify constants, without and with location wrappers. */
28319 tree int_cst
= build_int_cst (integer_type_node
, 42);
28320 ASSERT_EQ (int_cst
, build_non_dependent_expr (int_cst
));
28322 tree wrapped_int_cst
= maybe_wrap_with_location (int_cst
, loc
);
28323 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst
));
28324 ASSERT_EQ (wrapped_int_cst
, build_non_dependent_expr (wrapped_int_cst
));
28326 tree string_lit
= build_string (4, "foo");
28327 TREE_TYPE (string_lit
) = char_array_type_node
;
28328 string_lit
= fix_string_type (string_lit
);
28329 ASSERT_EQ (string_lit
, build_non_dependent_expr (string_lit
));
28331 tree wrapped_string_lit
= maybe_wrap_with_location (string_lit
, loc
);
28332 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit
));
28333 ASSERT_EQ (wrapped_string_lit
,
28334 build_non_dependent_expr (wrapped_string_lit
));
28337 /* Verify that type_dependent_expression_p () works correctly, even
28338 in the presence of location wrapper nodes. */
28341 test_type_dependent_expression_p ()
28343 location_t loc
= BUILTINS_LOCATION
;
28345 tree name
= get_identifier ("foo");
28347 /* If no templates are involved, nothing is type-dependent. */
28348 gcc_assert (!processing_template_decl
);
28349 ASSERT_FALSE (type_dependent_expression_p (name
));
28351 ++processing_template_decl
;
28353 /* Within a template, an unresolved name is always type-dependent. */
28354 ASSERT_TRUE (type_dependent_expression_p (name
));
28356 /* Ensure it copes with NULL_TREE and errors. */
28357 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE
));
28358 ASSERT_FALSE (type_dependent_expression_p (error_mark_node
));
28360 /* A USING_DECL in a template should be type-dependent, even if wrapped
28361 with a location wrapper (PR c++/83799). */
28362 tree using_decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
28363 TREE_TYPE (using_decl
) = integer_type_node
;
28364 ASSERT_TRUE (type_dependent_expression_p (using_decl
));
28365 tree wrapped_using_decl
= maybe_wrap_with_location (using_decl
, loc
);
28366 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl
));
28367 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl
));
28369 --processing_template_decl
;
28372 /* Run all of the selftests within this file. */
28377 test_build_non_dependent_expr ();
28378 test_type_dependent_expression_p ();
28381 } // namespace selftest
28383 #endif /* #if CHECKING_P */
28385 #include "gt-cp-pt.h"