1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 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"
44 /* The type of functions taking a tree, and some additional data, and
46 typedef int (*tree_fn_t
) (tree
, void*);
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template
{
52 struct pending_template
*next
;
53 struct tinst_level
*tinst
;
56 static GTY(()) struct pending_template
*pending_templates
;
57 static GTY(()) struct pending_template
*last_pending_template
;
59 int processing_template_parmlist
;
60 static int template_header_count
;
62 static GTY(()) tree saved_trees
;
63 static vec
<int> inline_parm_levels
;
65 static GTY(()) struct tinst_level
*current_tinst_level
;
67 static GTY(()) tree saved_access_scope
;
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr
;
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
77 // Implementation of the RAII helper for creating new local
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations
)
82 local_specializations
= new hash_map
<tree
, tree
>;
85 local_specialization_stack::~local_specialization_stack ()
87 delete local_specializations
;
88 local_specializations
= saved
;
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth
;
94 struct GTY((for_user
)) spec_entry
101 struct spec_hasher
: ggc_ptr_hash
<spec_entry
>
103 static hashval_t
hash (spec_entry
*);
104 static bool equal (spec_entry
*, spec_entry
*);
107 static GTY (()) hash_table
<spec_hasher
> *decl_specializations
;
109 static GTY (()) hash_table
<spec_hasher
> *type_specializations
;
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
126 enum template_base_result
{
128 tbr_ambiguous_baseclass
,
132 static void push_access_scope (tree
);
133 static void pop_access_scope (tree
);
134 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
135 unification_kind_t
, int,
137 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
138 unification_kind_t
, int, bool, bool);
139 static int unify (tree
, tree
, tree
, tree
, int, bool);
140 static void add_pending_template (tree
);
141 static tree
reopen_tinst_level (struct tinst_level
*);
142 static tree
tsubst_initializer_list (tree
, tree
);
143 static tree
get_partial_spec_bindings (tree
, tree
, tree
);
144 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
146 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
148 static void tsubst_enum (tree
, tree
, tree
);
149 static tree
add_to_template_args (tree
, tree
);
150 static tree
add_outermost_template_args (tree
, tree
);
151 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
152 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
154 static int type_unification_real (tree
, tree
, tree
, const tree
*,
155 unsigned int, int, unification_kind_t
, int,
156 vec
<deferred_access_check
, va_gc
> **,
158 static void note_template_header (int);
159 static tree
convert_nontype_argument_function (tree
, tree
, tsubst_flags_t
);
160 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
161 static tree
convert_template_argument (tree
, tree
, tree
,
162 tsubst_flags_t
, int, tree
);
163 static tree
for_each_template_parm (tree
, tree_fn_t
, void*,
164 hash_set
<tree
> *, bool, tree_fn_t
= NULL
);
165 static tree
expand_template_argument_pack (tree
);
166 static tree
build_template_parm_index (int, int, int, tree
, tree
);
167 static bool inline_needs_template_parms (tree
, bool);
168 static void push_inline_template_parms_recursive (tree
, int);
169 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
170 static int mark_template_parm (tree
, void *);
171 static int template_parm_this_level_p (tree
, void *);
172 static tree
tsubst_friend_function (tree
, tree
);
173 static tree
tsubst_friend_class (tree
, tree
);
174 static int can_complete_type_without_circularity (tree
);
175 static tree
get_bindings (tree
, tree
, tree
, bool);
176 static int template_decl_level (tree
);
177 static int check_cv_quals_for_unify (int, tree
, tree
);
178 static void template_parm_level_and_index (tree
, int*, int*);
179 static int unify_pack_expansion (tree
, tree
, tree
,
180 tree
, unification_kind_t
, bool, bool);
181 static tree
copy_template_args (tree
);
182 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
183 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
184 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
185 static tree
most_specialized_partial_spec (tree
, tsubst_flags_t
);
186 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
187 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
188 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
189 static bool check_specialization_scope (void);
190 static tree
process_partial_specialization (tree
);
191 static void set_current_access_from_decl (tree
);
192 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
194 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
195 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
197 static bool template_template_parm_bindings_ok_p (tree
, tree
);
198 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
199 static tree
for_each_template_parm_r (tree
*, int *, void *);
200 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
201 static void copy_default_args_to_explicit_spec (tree
);
202 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
203 static bool dependent_template_arg_p (tree
);
204 static bool any_template_arguments_need_structural_equality_p (tree
);
205 static bool dependent_type_p_r (tree
);
206 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
207 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
208 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
209 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
211 static tree
listify (tree
);
212 static tree
listify_autos (tree
, tree
);
213 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
214 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
215 static bool complex_alias_template_p (const_tree tmpl
);
216 static tree
tsubst_attributes (tree
, tree
, tsubst_flags_t
, tree
);
217 static tree
canonicalize_expr_argument (tree
, tsubst_flags_t
);
218 static tree
make_argument_pack (tree
);
220 /* Make the current scope suitable for access checking when we are
221 processing T. T can be FUNCTION_DECL for instantiated function
222 template, VAR_DECL for static member variable, or TYPE_DECL for
223 alias template (needed by instantiate_decl). */
226 push_access_scope (tree t
)
228 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
229 || TREE_CODE (t
) == TYPE_DECL
);
231 if (DECL_FRIEND_CONTEXT (t
))
232 push_nested_class (DECL_FRIEND_CONTEXT (t
));
233 else if (DECL_CLASS_SCOPE_P (t
))
234 push_nested_class (DECL_CONTEXT (t
));
236 push_to_top_level ();
238 if (TREE_CODE (t
) == FUNCTION_DECL
)
240 saved_access_scope
= tree_cons
241 (NULL_TREE
, current_function_decl
, saved_access_scope
);
242 current_function_decl
= t
;
246 /* Restore the scope set up by push_access_scope. T is the node we
250 pop_access_scope (tree t
)
252 if (TREE_CODE (t
) == FUNCTION_DECL
)
254 current_function_decl
= TREE_VALUE (saved_access_scope
);
255 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
258 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
261 pop_from_top_level ();
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
270 finish_member_template_decl (tree decl
)
272 if (decl
== error_mark_node
)
273 return error_mark_node
;
275 gcc_assert (DECL_P (decl
));
277 if (TREE_CODE (decl
) == TYPE_DECL
)
281 type
= TREE_TYPE (decl
);
282 if (type
== error_mark_node
)
283 return error_mark_node
;
284 if (MAYBE_CLASS_TYPE_P (type
)
285 && CLASSTYPE_TEMPLATE_INFO (type
)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
288 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
289 check_member_template (tmpl
);
294 else if (TREE_CODE (decl
) == FIELD_DECL
)
295 error ("data member %qD cannot be a member template", decl
);
296 else if (DECL_TEMPLATE_INFO (decl
))
298 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
300 check_member_template (DECL_TI_TEMPLATE (decl
));
301 return DECL_TI_TEMPLATE (decl
);
307 error ("invalid member template declaration %qD", decl
);
309 return error_mark_node
;
312 /* Create a template info node. */
315 build_template_info (tree template_decl
, tree template_args
)
317 tree result
= make_node (TEMPLATE_INFO
);
318 TI_TEMPLATE (result
) = template_decl
;
319 TI_ARGS (result
) = template_args
;
323 /* Return the template info node corresponding to T, whatever T is. */
326 get_template_info (const_tree t
)
328 tree tinfo
= NULL_TREE
;
330 if (!t
|| t
== error_mark_node
)
333 if (TREE_CODE (t
) == NAMESPACE_DECL
334 || TREE_CODE (t
) == PARM_DECL
)
337 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
338 tinfo
= DECL_TEMPLATE_INFO (t
);
340 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
343 if (OVERLOAD_TYPE_P (t
))
344 tinfo
= TYPE_TEMPLATE_INFO (t
);
345 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
346 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
351 /* Returns the template nesting level of the indicated class TYPE.
361 A<T>::B<U> has depth two, while A<T> has depth one.
362 Both A<T>::B<int> and A<int>::B<U> have depth one, if
363 they are instantiations, not specializations.
365 This function is guaranteed to return 0 if passed NULL_TREE so
366 that, for example, `template_class_depth (current_class_type)' is
370 template_class_depth (tree type
)
374 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
376 tree tinfo
= get_template_info (type
);
378 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
383 type
= CP_DECL_CONTEXT (type
);
384 else if (LAMBDA_TYPE_P (type
))
385 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
387 type
= CP_TYPE_CONTEXT (type
);
393 /* Subroutine of maybe_begin_member_template_processing.
394 Returns true if processing DECL needs us to push template parms. */
397 inline_needs_template_parms (tree decl
, bool nsdmi
)
399 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
402 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
403 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
406 /* Subroutine of maybe_begin_member_template_processing.
407 Push the template parms in PARMS, starting from LEVELS steps into the
408 chain, and ending at the beginning, since template parms are listed
412 push_inline_template_parms_recursive (tree parmlist
, int levels
)
414 tree parms
= TREE_VALUE (parmlist
);
418 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
420 ++processing_template_decl
;
421 current_template_parms
422 = tree_cons (size_int (processing_template_decl
),
423 parms
, current_template_parms
);
424 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
426 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
428 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
430 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
432 if (error_operand_p (parm
))
435 gcc_assert (DECL_P (parm
));
437 switch (TREE_CODE (parm
))
445 /* Push the CONST_DECL. */
446 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
460 maybe_begin_member_template_processing (tree decl
)
464 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
468 tree ctx
= DECL_CONTEXT (decl
);
469 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
470 /* Disregard full specializations (c++/60999). */
471 && uses_template_parms (ctx
)
472 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
475 if (inline_needs_template_parms (decl
, nsdmi
))
477 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
478 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
480 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
483 parms
= TREE_CHAIN (parms
);
486 push_inline_template_parms_recursive (parms
, levels
);
489 /* Remember how many levels of template parameters we pushed so that
490 we can pop them later. */
491 inline_parm_levels
.safe_push (levels
);
494 /* Undo the effects of maybe_begin_member_template_processing. */
497 maybe_end_member_template_processing (void)
502 if (inline_parm_levels
.length () == 0)
505 last
= inline_parm_levels
.pop ();
506 for (i
= 0; i
< last
; ++i
)
508 --processing_template_decl
;
509 current_template_parms
= TREE_CHAIN (current_template_parms
);
514 /* Return a new template argument vector which contains all of ARGS,
515 but has as its innermost set of arguments the EXTRA_ARGS. */
518 add_to_template_args (tree args
, tree extra_args
)
525 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
528 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
529 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
531 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
532 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
534 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
535 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
540 /* Like add_to_template_args, but only the outermost ARGS are added to
541 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
542 (EXTRA_ARGS) levels are added. This function is used to combine
543 the template arguments from a partial instantiation with the
544 template arguments used to attain the full instantiation from the
545 partial instantiation. */
548 add_outermost_template_args (tree args
, tree extra_args
)
552 /* If there are more levels of EXTRA_ARGS than there are ARGS,
553 something very fishy is going on. */
554 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
556 /* If *all* the new arguments will be the EXTRA_ARGS, just return
558 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
561 /* For the moment, we make ARGS look like it contains fewer levels. */
562 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
564 new_args
= add_to_template_args (args
, extra_args
);
566 /* Now, we restore ARGS to its full dimensions. */
567 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
572 /* Return the N levels of innermost template arguments from the ARGS. */
575 get_innermost_template_args (tree args
, int n
)
583 /* If N is 1, just return the innermost set of template arguments. */
585 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
587 /* If we're not removing anything, just return the arguments we were
589 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
590 gcc_assert (extra_levels
>= 0);
591 if (extra_levels
== 0)
594 /* Make a new set of arguments, not containing the outer arguments. */
595 new_args
= make_tree_vec (n
);
596 for (i
= 1; i
<= n
; ++i
)
597 SET_TMPL_ARGS_LEVEL (new_args
, i
,
598 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
603 /* The inverse of get_innermost_template_args: Return all but the innermost
604 EXTRA_LEVELS levels of template arguments from the ARGS. */
607 strip_innermost_template_args (tree args
, int extra_levels
)
610 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
615 /* If N is 1, just return the outermost set of template arguments. */
617 return TMPL_ARGS_LEVEL (args
, 1);
619 /* If we're not removing anything, just return the arguments we were
621 gcc_assert (extra_levels
>= 0);
622 if (extra_levels
== 0)
625 /* Make a new set of arguments, not containing the inner arguments. */
626 new_args
= make_tree_vec (n
);
627 for (i
= 1; i
<= n
; ++i
)
628 SET_TMPL_ARGS_LEVEL (new_args
, i
,
629 TMPL_ARGS_LEVEL (args
, i
));
634 /* We've got a template header coming up; push to a new level for storing
638 begin_template_parm_list (void)
640 /* We use a non-tag-transparent scope here, which causes pushtag to
641 put tags in this scope, rather than in the enclosing class or
642 namespace scope. This is the right thing, since we want
643 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
644 global template class, push_template_decl handles putting the
645 TEMPLATE_DECL into top-level scope. For a nested template class,
648 template <class T> struct S1 {
649 template <class T> struct S2 {};
652 pushtag contains special code to insert the TEMPLATE_DECL for S2
653 at the right scope. */
654 begin_scope (sk_template_parms
, NULL
);
655 ++processing_template_decl
;
656 ++processing_template_parmlist
;
657 note_template_header (0);
659 /* Add a dummy parameter level while we process the parameter list. */
660 current_template_parms
661 = tree_cons (size_int (processing_template_decl
),
663 current_template_parms
);
666 /* This routine is called when a specialization is declared. If it is
667 invalid to declare a specialization here, an error is reported and
668 false is returned, otherwise this routine will return true. */
671 check_specialization_scope (void)
673 tree scope
= current_scope ();
677 An explicit specialization shall be declared in the namespace of
678 which the template is a member, or, for member templates, in the
679 namespace of which the enclosing class or enclosing class
680 template is a member. An explicit specialization of a member
681 function, member class or static data member of a class template
682 shall be declared in the namespace of which the class template
684 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
686 error ("explicit specialization in non-namespace scope %qD", scope
);
692 In an explicit specialization declaration for a member of a class
693 template or a member template that appears in namespace scope,
694 the member template and some of its enclosing class templates may
695 remain unspecialized, except that the declaration shall not
696 explicitly specialize a class member template if its enclosing
697 class templates are not explicitly specialized as well. */
698 if (current_template_parms
)
700 error ("enclosing class templates are not explicitly specialized");
707 /* We've just seen template <>. */
710 begin_specialization (void)
712 begin_scope (sk_template_spec
, NULL
);
713 note_template_header (1);
714 return check_specialization_scope ();
717 /* Called at then end of processing a declaration preceded by
721 end_specialization (void)
724 reset_specialization ();
727 /* Any template <>'s that we have seen thus far are not referring to a
728 function specialization. */
731 reset_specialization (void)
733 processing_specialization
= 0;
734 template_header_count
= 0;
737 /* We've just seen a template header. If SPECIALIZATION is nonzero,
738 it was of the form template <>. */
741 note_template_header (int specialization
)
743 processing_specialization
= specialization
;
744 template_header_count
++;
747 /* We're beginning an explicit instantiation. */
750 begin_explicit_instantiation (void)
752 gcc_assert (!processing_explicit_instantiation
);
753 processing_explicit_instantiation
= true;
758 end_explicit_instantiation (void)
760 gcc_assert (processing_explicit_instantiation
);
761 processing_explicit_instantiation
= false;
764 /* An explicit specialization or partial specialization of TMPL is being
765 declared. Check that the namespace in which the specialization is
766 occurring is permissible. Returns false iff it is invalid to
767 specialize TMPL in the current namespace. */
770 check_specialization_namespace (tree tmpl
)
772 tree tpl_ns
= decl_namespace_context (tmpl
);
776 An explicit specialization shall be declared in a namespace enclosing the
777 specialized template. An explicit specialization whose declarator-id is
778 not qualified shall be declared in the nearest enclosing namespace of the
779 template, or, if the namespace is inline (7.3.1), any namespace from its
780 enclosing namespace set. */
781 if (current_scope() != DECL_CONTEXT (tmpl
)
782 && !at_namespace_scope_p ())
784 error ("specialization of %qD must appear at namespace scope", tmpl
);
788 if (is_nested_namespace (current_namespace
, tpl_ns
, cxx_dialect
< cxx11
))
789 /* Same or enclosing namespace. */
793 permerror (input_location
,
794 "specialization of %qD in different namespace", tmpl
);
795 inform (DECL_SOURCE_LOCATION (tmpl
),
796 " from definition of %q#D", tmpl
);
801 /* SPEC is an explicit instantiation. Check that it is valid to
802 perform this explicit instantiation in the current namespace. */
805 check_explicit_instantiation_namespace (tree spec
)
809 /* DR 275: An explicit instantiation shall appear in an enclosing
810 namespace of its template. */
811 ns
= decl_namespace_context (spec
);
812 if (!is_nested_namespace (current_namespace
, ns
))
813 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
814 "(which does not enclose namespace %qD)",
815 spec
, current_namespace
, ns
);
818 // Returns the type of a template specialization only if that
819 // specialization needs to be defined. Otherwise (e.g., if the type has
820 // already been defined), the function returns NULL_TREE.
822 maybe_new_partial_specialization (tree type
)
824 // An implicit instantiation of an incomplete type implies
825 // the definition of a new class template.
827 // template<typename T>
830 // template<typename T>
833 // Here, S<T*> is an implicit instantiation of S whose type
835 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
838 // It can also be the case that TYPE is a completed specialization.
839 // Continuing the previous example, suppose we also declare:
841 // template<typename T>
842 // requires Integral<T>
845 // Here, S<T*> refers to the specialization S<T*> defined
846 // above. However, we need to differentiate definitions because
847 // we intend to define a new partial specialization. In this case,
848 // we rely on the fact that the constraints are different for
849 // this declaration than that above.
851 // Note that we also get here for injected class names and
852 // late-parsed template definitions. We must ensure that we
853 // do not create new type declarations for those cases.
854 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
856 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
857 tree args
= CLASSTYPE_TI_ARGS (type
);
859 // If there are no template parameters, this cannot be a new
860 // partial template specializtion?
861 if (!current_template_parms
)
864 // The injected-class-name is not a new partial specialization.
865 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
868 // If the constraints are not the same as those of the primary
869 // then, we can probably create a new specialization.
870 tree type_constr
= current_template_constraints ();
872 if (type
== TREE_TYPE (tmpl
))
874 tree main_constr
= get_constraints (tmpl
);
875 if (equivalent_constraints (type_constr
, main_constr
))
879 // Also, if there's a pre-existing specialization with matching
880 // constraints, then this also isn't new.
881 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
884 tree spec_tmpl
= TREE_VALUE (specs
);
885 tree spec_args
= TREE_PURPOSE (specs
);
886 tree spec_constr
= get_constraints (spec_tmpl
);
887 if (comp_template_args (args
, spec_args
)
888 && equivalent_constraints (type_constr
, spec_constr
))
890 specs
= TREE_CHAIN (specs
);
893 // Create a new type node (and corresponding type decl)
894 // for the newly declared specialization.
895 tree t
= make_class_type (TREE_CODE (type
));
896 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
897 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
899 /* We only need a separate type node for storing the definition of this
900 partial specialization; uses of S<T*> are unconstrained, so all are
901 equivalent. So keep TYPE_CANONICAL the same. */
902 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
904 // Build the corresponding type decl.
905 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
906 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
907 DECL_SOURCE_LOCATION (d
) = input_location
;
915 /* The TYPE is being declared. If it is a template type, that means it
916 is a partial specialization. Do appropriate error-checking. */
919 maybe_process_partial_specialization (tree type
)
923 if (type
== error_mark_node
)
924 return error_mark_node
;
926 /* A lambda that appears in specialization context is not itself a
928 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
931 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
933 error ("name of class shadows template template parameter %qD",
935 return error_mark_node
;
938 context
= TYPE_CONTEXT (type
);
940 if (TYPE_ALIAS_P (type
))
942 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
944 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
945 error ("specialization of alias template %qD",
946 TI_TEMPLATE (tinfo
));
948 error ("explicit specialization of non-template %qT", type
);
949 return error_mark_node
;
951 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
953 /* This is for ordinary explicit specialization and partial
954 specialization of a template class such as:
956 template <> class C<int>;
960 template <class T> class C<T*>;
962 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964 if (tree t
= maybe_new_partial_specialization (type
))
966 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
967 && !at_namespace_scope_p ())
968 return error_mark_node
;
969 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
970 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
971 if (processing_template_decl
)
973 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
974 if (decl
== error_mark_node
)
975 return error_mark_node
;
976 return TREE_TYPE (decl
);
979 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
980 error ("specialization of %qT after instantiation", type
);
981 else if (errorcount
&& !processing_specialization
982 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
983 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
984 /* Trying to define a specialization either without a template<> header
985 or in an inappropriate place. We've already given an error, so just
986 bail now so we don't actually define the specialization. */
987 return error_mark_node
;
989 else if (CLASS_TYPE_P (type
)
990 && !CLASSTYPE_USE_TEMPLATE (type
)
991 && CLASSTYPE_TEMPLATE_INFO (type
)
992 && context
&& CLASS_TYPE_P (context
)
993 && CLASSTYPE_TEMPLATE_INFO (context
))
995 /* This is for an explicit specialization of member class
996 template according to [temp.expl.spec/18]:
998 template <> template <class U> class C<int>::D;
1000 The context `C<int>' must be an implicit instantiation.
1001 Otherwise this is just a member class template declared
1004 template <> class C<int> { template <class U> class D; };
1005 template <> template <class U> class C<int>::D;
1007 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1008 while in the second case, `C<int>::D' is a primary template
1009 and `C<T>::D' may not exist. */
1011 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1012 && !COMPLETE_TYPE_P (type
))
1015 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1017 if (current_namespace
1018 != decl_namespace_context (tmpl
))
1020 permerror (input_location
,
1021 "specializing %q#T in different namespace", type
);
1022 permerror (DECL_SOURCE_LOCATION (tmpl
),
1023 " from definition of %q#D", tmpl
);
1026 /* Check for invalid specialization after instantiation:
1028 template <> template <> class C<int>::D<int>;
1029 template <> template <class U> class C<int>::D; */
1031 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1032 t
; t
= TREE_CHAIN (t
))
1034 tree inst
= TREE_VALUE (t
);
1035 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1036 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1038 /* We already have a full specialization of this partial
1039 instantiation, or a full specialization has been
1040 looked up but not instantiated. Reassign it to the
1041 new member specialization template. */
1045 elt
.tmpl
= most_general_template (tmpl
);
1046 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1049 type_specializations
->remove_elt (&elt
);
1052 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1055 = type_specializations
->find_slot (&elt
, INSERT
);
1056 entry
= ggc_alloc
<spec_entry
> ();
1061 /* But if we've had an implicit instantiation, that's a
1062 problem ([temp.expl.spec]/6). */
1063 error ("specialization %qT after instantiation %qT",
1067 /* Mark TYPE as a specialization. And as a result, we only
1068 have one level of template argument for the innermost
1070 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1071 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1072 CLASSTYPE_TI_ARGS (type
)
1073 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1076 else if (processing_specialization
)
1078 /* Someday C++0x may allow for enum template specialization. */
1079 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1080 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1081 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1082 "of %qD not allowed by ISO C++", type
);
1085 error ("explicit specialization of non-template %qT", type
);
1086 return error_mark_node
;
1093 /* Returns nonzero if we can optimize the retrieval of specializations
1094 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1095 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1098 optimize_specialization_lookup_p (tree tmpl
)
1100 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1101 && DECL_CLASS_SCOPE_P (tmpl
)
1102 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1104 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1105 /* The optimized lookup depends on the fact that the
1106 template arguments for the member function template apply
1107 purely to the containing class, which is not true if the
1108 containing class is an explicit or partial
1110 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1111 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1112 && !DECL_CONV_FN_P (tmpl
)
1113 /* It is possible to have a template that is not a member
1114 template and is not a member of a template class:
1116 template <typename T>
1117 struct S { friend A::f(); };
1119 Here, the friend function is a template, but the context does
1120 not have template information. The optimized lookup relies
1121 on having ARGS be the template arguments for both the class
1122 and the function template. */
1123 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1126 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1127 gone through coerce_template_parms by now. */
1130 verify_unstripped_args (tree args
)
1132 ++processing_template_decl
;
1133 if (!any_dependent_template_arguments_p (args
))
1135 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1136 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1138 tree arg
= TREE_VEC_ELT (inner
, i
);
1139 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1141 else if (TYPE_P (arg
))
1142 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1143 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1144 /* Allow typedefs on the type of a non-type argument, since a
1145 parameter can have them. */;
1147 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1150 --processing_template_decl
;
1153 /* Retrieve the specialization (in the sense of [temp.spec] - a
1154 specialization is either an instantiation or an explicit
1155 specialization) of TMPL for the given template ARGS. If there is
1156 no such specialization, return NULL_TREE. The ARGS are a vector of
1157 arguments, or a vector of vectors of arguments, in the case of
1158 templates with more than one level of parameters.
1160 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1161 then we search for a partial specialization matching ARGS. This
1162 parameter is ignored if TMPL is not a class template.
1164 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1165 result is a NONTYPE_ARGUMENT_PACK. */
1168 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1170 if (tmpl
== NULL_TREE
)
1173 if (args
== error_mark_node
)
1176 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1177 || TREE_CODE (tmpl
) == FIELD_DECL
);
1179 /* There should be as many levels of arguments as there are
1180 levels of parameters. */
1181 gcc_assert (TMPL_ARGS_DEPTH (args
)
1182 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1183 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1184 : template_class_depth (DECL_CONTEXT (tmpl
))));
1187 verify_unstripped_args (args
);
1189 if (optimize_specialization_lookup_p (tmpl
))
1191 /* The template arguments actually apply to the containing
1192 class. Find the class specialization with those
1194 tree class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1195 tree class_specialization
1196 = retrieve_specialization (class_template
, args
, 0);
1197 if (!class_specialization
)
1199 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1200 for the specialization. */
1201 int idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1204 /* Iterate through the methods with the indicated name, looking
1205 for the one that has an instance of TMPL. */
1206 vec
<tree
, va_gc
> *methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1207 for (ovl_iterator
iter ((*methods
)[idx
]); iter
; ++iter
)
1210 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1211 /* using-declarations can add base methods to the method vec,
1212 and we don't want those here. */
1213 && DECL_CONTEXT (fn
) == class_specialization
)
1222 hash_table
<spec_hasher
> *specializations
;
1226 elt
.spec
= NULL_TREE
;
1228 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1229 specializations
= type_specializations
;
1231 specializations
= decl_specializations
;
1234 hash
= spec_hasher::hash (&elt
);
1235 found
= specializations
->find_with_hash (&elt
, hash
);
1243 /* Like retrieve_specialization, but for local declarations. */
1246 retrieve_local_specialization (tree tmpl
)
1248 if (local_specializations
== NULL
)
1251 tree
*slot
= local_specializations
->get (tmpl
);
1252 return slot
? *slot
: NULL_TREE
;
1255 /* Returns nonzero iff DECL is a specialization of TMPL. */
1258 is_specialization_of (tree decl
, tree tmpl
)
1262 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1266 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1272 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1274 for (t
= TREE_TYPE (decl
);
1276 t
= CLASSTYPE_USE_TEMPLATE (t
)
1277 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1278 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1285 /* Returns nonzero iff DECL is a specialization of friend declaration
1286 FRIEND_DECL according to [temp.friend]. */
1289 is_specialization_of_friend (tree decl
, tree friend_decl
)
1291 bool need_template
= true;
1294 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1295 || TREE_CODE (decl
) == TYPE_DECL
);
1297 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1298 of a template class, we want to check if DECL is a specialization
1300 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1301 && DECL_TEMPLATE_INFO (friend_decl
)
1302 && !DECL_USE_TEMPLATE (friend_decl
))
1304 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1305 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1306 need_template
= false;
1308 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1309 && !PRIMARY_TEMPLATE_P (friend_decl
))
1310 need_template
= false;
1312 /* There is nothing to do if this is not a template friend. */
1313 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1316 if (is_specialization_of (decl
, friend_decl
))
1320 A member of a class template may be declared to be a friend of a
1321 non-template class. In this case, the corresponding member of
1322 every specialization of the class template is a friend of the
1323 class granting friendship.
1325 For example, given a template friend declaration
1327 template <class T> friend void A<T>::f();
1329 the member function below is considered a friend
1331 template <> struct A<int> {
1335 For this type of template friend, TEMPLATE_DEPTH below will be
1336 nonzero. To determine if DECL is a friend of FRIEND, we first
1337 check if the enclosing class is a specialization of another. */
1339 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1341 && DECL_CLASS_SCOPE_P (decl
)
1342 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1343 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1345 /* Next, we check the members themselves. In order to handle
1346 a few tricky cases, such as when FRIEND_DECL's are
1348 template <class T> friend void A<T>::g(T t);
1349 template <class T> template <T t> friend void A<T>::h();
1353 void A<int>::g(int);
1354 template <int> void A<int>::h();
1356 we need to figure out ARGS, the template arguments from
1357 the context of DECL. This is required for template substitution
1358 of `T' in the function parameter of `g' and template parameter
1359 of `h' in the above examples. Here ARGS corresponds to `int'. */
1361 tree context
= DECL_CONTEXT (decl
);
1362 tree args
= NULL_TREE
;
1363 int current_depth
= 0;
1365 while (current_depth
< template_depth
)
1367 if (CLASSTYPE_TEMPLATE_INFO (context
))
1369 if (current_depth
== 0)
1370 args
= TYPE_TI_ARGS (context
);
1372 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1375 context
= TYPE_CONTEXT (context
);
1378 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1383 tree friend_args_type
;
1384 tree decl_args_type
;
1386 /* Make sure that both DECL and FRIEND_DECL are templates or
1388 is_template
= DECL_TEMPLATE_INFO (decl
)
1389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1390 if (need_template
^ is_template
)
1392 else if (is_template
)
1394 /* If both are templates, check template parameter list. */
1396 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1398 if (!comp_template_parms
1399 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1403 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1406 decl_type
= TREE_TYPE (decl
);
1408 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1409 tf_none
, NULL_TREE
);
1410 if (friend_type
== error_mark_node
)
1413 /* Check if return types match. */
1414 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1417 /* Check if function parameter types match, ignoring the
1418 `this' parameter. */
1419 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1420 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1422 friend_args_type
= TREE_CHAIN (friend_args_type
);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1424 decl_args_type
= TREE_CHAIN (decl_args_type
);
1426 return compparms (decl_args_type
, friend_args_type
);
1430 /* DECL is a TYPE_DECL */
1432 tree decl_type
= TREE_TYPE (decl
);
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1437 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1440 if (need_template
^ is_template
)
1442 else if (is_template
)
1445 /* If both are templates, check the name of the two
1446 TEMPLATE_DECL's first because is_friend didn't. */
1447 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1448 != DECL_NAME (friend_decl
))
1451 /* Now check template parameter list. */
1453 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1455 return comp_template_parms
1456 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1460 return (DECL_NAME (decl
)
1461 == DECL_NAME (friend_decl
));
1467 /* Register the specialization SPEC as a specialization of TMPL with
1468 the indicated ARGS. IS_FRIEND indicates whether the specialization
1469 is actually just a friend declaration. Returns SPEC, or an
1470 equivalent prior declaration, if available.
1472 We also store instantiations of field packs in the hash table, even
1473 though they are not themselves templates, to make lookup easier. */
1476 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1480 spec_entry
**slot
= NULL
;
1483 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1484 || (TREE_CODE (tmpl
) == FIELD_DECL
1485 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1487 if (TREE_CODE (spec
) == FUNCTION_DECL
1488 && uses_template_parms (DECL_TI_ARGS (spec
)))
1489 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1490 register it; we want the corresponding TEMPLATE_DECL instead.
1491 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1492 the more obvious `uses_template_parms (spec)' to avoid problems
1493 with default function arguments. In particular, given
1494 something like this:
1496 template <class T> void f(T t1, T t = T())
1498 the default argument expression is not substituted for in an
1499 instantiation unless and until it is actually needed. */
1502 if (optimize_specialization_lookup_p (tmpl
))
1503 /* We don't put these specializations in the hash table, but we might
1504 want to give an error about a mismatch. */
1505 fn
= retrieve_specialization (tmpl
, args
, 0);
1513 hash
= spec_hasher::hash (&elt
);
1516 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1518 fn
= ((spec_entry
*) *slot
)->spec
;
1523 /* We can sometimes try to re-register a specialization that we've
1524 already got. In particular, regenerate_decl_from_template calls
1525 duplicate_decls which will update the specialization list. But,
1526 we'll still get called again here anyhow. It's more convenient
1527 to simply allow this than to try to prevent it. */
1530 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1532 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1534 if (DECL_ODR_USED (fn
)
1535 || DECL_EXPLICIT_INSTANTIATION (fn
))
1537 error ("specialization of %qD after instantiation",
1539 return error_mark_node
;
1544 /* This situation should occur only if the first
1545 specialization is an implicit instantiation, the
1546 second is an explicit specialization, and the
1547 implicit instantiation has not yet been used. That
1548 situation can occur if we have implicitly
1549 instantiated a member function and then specialized
1552 We can also wind up here if a friend declaration that
1553 looked like an instantiation turns out to be a
1556 template <class T> void foo(T);
1557 class S { friend void foo<>(int) };
1558 template <> void foo(int);
1560 We transform the existing DECL in place so that any
1561 pointers to it become pointers to the updated
1564 If there was a definition for the template, but not
1565 for the specialization, we want this to look as if
1566 there were no definition, and vice versa. */
1567 DECL_INITIAL (fn
) = NULL_TREE
;
1568 duplicate_decls (spec
, fn
, is_friend
);
1569 /* The call to duplicate_decls will have applied
1572 An explicit specialization of a function template
1573 is inline only if it is explicitly declared to be,
1574 and independently of whether its function template
1577 to the primary function; now copy the inline bits to
1578 the various clones. */
1579 FOR_EACH_CLONE (clone
, fn
)
1581 DECL_DECLARED_INLINE_P (clone
)
1582 = DECL_DECLARED_INLINE_P (fn
);
1583 DECL_SOURCE_LOCATION (clone
)
1584 = DECL_SOURCE_LOCATION (fn
);
1585 DECL_DELETED_FN (clone
)
1586 = DECL_DELETED_FN (fn
);
1588 check_specialization_namespace (tmpl
);
1593 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1595 tree dd
= duplicate_decls (spec
, fn
, is_friend
);
1596 if (dd
== error_mark_node
)
1597 /* We've already complained in duplicate_decls. */
1598 return error_mark_node
;
1600 if (dd
== NULL_TREE
&& DECL_INITIAL (spec
))
1601 /* Dup decl failed, but this is a new definition. Set the
1602 line number so any errors match this new
1604 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1610 return duplicate_decls (spec
, fn
, is_friend
);
1612 /* A specialization must be declared in the same namespace as the
1613 template it is specializing. */
1614 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1615 && !check_specialization_namespace (tmpl
))
1616 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1618 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1620 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1621 gcc_assert (tmpl
&& args
&& spec
);
1624 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1625 && PRIMARY_TEMPLATE_P (tmpl
)
1626 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1627 || variable_template_p (tmpl
))
1628 /* If TMPL is a forward declaration of a template function, keep a list
1629 of all specializations in case we need to reassign them to a friend
1630 template later in tsubst_friend_function.
1632 Also keep a list of all variable template instantiations so that
1633 process_partial_specialization can check whether a later partial
1634 specialization would have used it. */
1635 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1636 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1642 /* Returns true iff two spec_entry nodes are equivalent. */
1644 int comparing_specializations
;
1647 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1651 ++comparing_specializations
;
1652 equal
= (e1
->tmpl
== e2
->tmpl
1653 && comp_template_args (e1
->args
, e2
->args
));
1654 if (equal
&& flag_concepts
1655 /* tmpl could be a FIELD_DECL for a capture pack. */
1656 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1657 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1658 && uses_template_parms (e1
->args
))
1660 /* Partial specializations of a variable template can be distinguished by
1662 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1663 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1664 equal
= equivalent_constraints (c1
, c2
);
1666 --comparing_specializations
;
1671 /* Returns a hash for a template TMPL and template arguments ARGS. */
1674 hash_tmpl_and_args (tree tmpl
, tree args
)
1676 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1677 return iterative_hash_template_arg (args
, val
);
1680 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1684 spec_hasher::hash (spec_entry
*e
)
1686 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1689 /* Recursively calculate a hash value for a template argument ARG, for use
1690 in the hash tables of template specializations. */
1693 iterative_hash_template_arg (tree arg
, hashval_t val
)
1695 unsigned HOST_WIDE_INT i
;
1696 enum tree_code code
;
1699 if (arg
== NULL_TREE
)
1700 return iterative_hash_object (arg
, val
);
1705 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1708 code
= TREE_CODE (arg
);
1709 tclass
= TREE_CODE_CLASS (code
);
1711 val
= iterative_hash_object (code
, val
);
1718 case IDENTIFIER_NODE
:
1719 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1723 int i
, len
= TREE_VEC_LENGTH (arg
);
1724 for (i
= 0; i
< len
; ++i
)
1725 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1729 case TYPE_PACK_EXPANSION
:
1730 case EXPR_PACK_EXPANSION
:
1731 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1732 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1734 case TYPE_ARGUMENT_PACK
:
1735 case NONTYPE_ARGUMENT_PACK
:
1736 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1739 for (; arg
; arg
= TREE_CHAIN (arg
))
1740 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1744 for (lkp_iterator
iter (arg
); iter
; ++iter
)
1745 val
= iterative_hash_template_arg (*iter
, val
);
1751 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1754 val
= iterative_hash_template_arg (field
, val
);
1755 val
= iterative_hash_template_arg (value
, val
);
1761 if (!DECL_ARTIFICIAL (arg
))
1763 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1764 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1766 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1769 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1772 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1773 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1775 case TEMPLATE_PARM_INDEX
:
1776 val
= iterative_hash_template_arg
1777 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1778 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1779 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1782 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1783 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1784 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1787 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1789 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1793 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1794 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1795 val
= iterative_hash_object (code
, val
);
1796 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1799 /* A lambda can't appear in a template arg, but don't crash on
1801 gcc_assert (seen_error ());
1805 case IMPLICIT_CONV_EXPR
:
1806 case STATIC_CAST_EXPR
:
1807 case REINTERPRET_CAST_EXPR
:
1808 case CONST_CAST_EXPR
:
1809 case DYNAMIC_CAST_EXPR
:
1811 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1812 /* Now hash operands as usual. */
1822 if (alias_template_specialization_p (arg
))
1824 // We want an alias specialization that survived strip_typedefs
1825 // to hash differently from its TYPE_CANONICAL, to avoid hash
1826 // collisions that compare as different in template_args_equal.
1827 // These could be dependent specializations that strip_typedefs
1828 // left alone, or untouched specializations because
1829 // coerce_template_parms returns the unconverted template
1830 // arguments if it sees incomplete argument packs.
1831 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1832 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1834 if (TYPE_CANONICAL (arg
))
1835 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1837 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1838 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1839 /* Otherwise just compare the types during lookup. */
1842 case tcc_declaration
:
1844 return iterative_hash_expr (arg
, val
);
1847 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1849 unsigned n
= cp_tree_operand_length (arg
);
1850 for (i
= 0; i
< n
; ++i
)
1851 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1859 /* Unregister the specialization SPEC as a specialization of TMPL.
1860 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1861 if the SPEC was listed as a specialization of TMPL.
1863 Note that SPEC has been ggc_freed, so we can't look inside it. */
1866 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1871 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1872 elt
.args
= TI_ARGS (tinfo
);
1873 elt
.spec
= NULL_TREE
;
1875 entry
= decl_specializations
->find (&elt
);
1878 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1879 gcc_assert (new_spec
!= NULL_TREE
);
1880 entry
->spec
= new_spec
;
1887 /* Like register_specialization, but for local declarations. We are
1888 registering SPEC, an instantiation of TMPL. */
1891 register_local_specialization (tree spec
, tree tmpl
)
1893 local_specializations
->put (tmpl
, spec
);
1896 /* TYPE is a class type. Returns true if TYPE is an explicitly
1897 specialized class. */
1900 explicit_class_specialization_p (tree type
)
1902 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1904 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1907 /* Print the list of functions at FNS, going through all the overloads
1908 for each element of the list. Alternatively, FNS can not be a
1909 TREE_LIST, in which case it will be printed together with all the
1912 MORE and *STR should respectively be FALSE and NULL when the function
1913 is called from the outside. They are used internally on recursive
1914 calls. print_candidates manages the two parameters and leaves NULL
1915 in *STR when it ends. */
1918 print_candidates_1 (tree fns
, char **str
, bool more
= false)
1920 if (TREE_CODE (fns
) == TREE_LIST
)
1921 for (; fns
; fns
= TREE_CHAIN (fns
))
1922 print_candidates_1 (TREE_VALUE (fns
), str
, more
|| TREE_CHAIN (fns
));
1924 for (lkp_iterator
iter (fns
); iter
;)
1929 const char *pfx
= *str
;
1933 pfx
= _("candidates are:");
1935 pfx
= _("candidate is:");
1936 *str
= get_spaces (pfx
);
1938 inform (DECL_SOURCE_LOCATION (cand
), "%s %#qD", pfx
, cand
);
1942 /* Print the list of candidate FNS in an error message. FNS can also
1943 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1946 print_candidates (tree fns
)
1949 print_candidates_1 (fns
, &str
);
1953 /* Get a (possibly) constrained template declaration for the
1954 purpose of ordering candidates. */
1956 get_template_for_ordering (tree list
)
1958 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1959 tree f
= TREE_VALUE (list
);
1960 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1961 return TI_TEMPLATE (ti
);
1965 /* Among candidates having the same signature, return the
1966 most constrained or NULL_TREE if there is no best candidate.
1967 If the signatures of candidates vary (e.g., template
1968 specialization vs. member function), then there can be no
1971 Note that we don't compare constraints on the functions
1972 themselves, but rather those of their templates. */
1974 most_constrained_function (tree candidates
)
1976 // Try to find the best candidate in a first pass.
1977 tree champ
= candidates
;
1978 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1980 int winner
= more_constrained (get_template_for_ordering (champ
),
1981 get_template_for_ordering (c
));
1983 champ
= c
; // The candidate is more constrained
1984 else if (winner
== 0)
1985 return NULL_TREE
; // Neither is more constrained
1988 // Verify that the champ is better than previous candidates.
1989 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
1990 if (!more_constrained (get_template_for_ordering (champ
),
1991 get_template_for_ordering (c
)))
1999 /* Returns the template (one of the functions given by TEMPLATE_ID)
2000 which can be specialized to match the indicated DECL with the
2001 explicit template args given in TEMPLATE_ID. The DECL may be
2002 NULL_TREE if none is available. In that case, the functions in
2003 TEMPLATE_ID are non-members.
2005 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2006 specialization of a member template.
2008 The TEMPLATE_COUNT is the number of references to qualifying
2009 template classes that appeared in the name of the function. See
2010 check_explicit_specialization for a more accurate description.
2012 TSK indicates what kind of template declaration (if any) is being
2013 declared. TSK_TEMPLATE indicates that the declaration given by
2014 DECL, though a FUNCTION_DECL, has template parameters, and is
2015 therefore a template function.
2017 The template args (those explicitly specified and those deduced)
2018 are output in a newly created vector *TARGS_OUT.
2020 If it is impossible to determine the result, an error message is
2021 issued. The error_mark_node is returned to indicate failure. */
2024 determine_specialization (tree template_id
,
2027 int need_member_template
,
2033 tree explicit_targs
;
2034 tree candidates
= NULL_TREE
;
2036 /* A TREE_LIST of templates of which DECL may be a specialization.
2037 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2038 corresponding TREE_PURPOSE is the set of template arguments that,
2039 when used to instantiate the template, would produce a function
2040 with the signature of DECL. */
2041 tree templates
= NULL_TREE
;
2043 cp_binding_level
*b
;
2045 *targs_out
= NULL_TREE
;
2047 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2048 return error_mark_node
;
2050 /* We shouldn't be specializing a member template of an
2051 unspecialized class template; we already gave an error in
2052 check_specialization_scope, now avoid crashing. */
2053 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2054 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2056 gcc_assert (errorcount
);
2057 return error_mark_node
;
2060 fns
= TREE_OPERAND (template_id
, 0);
2061 explicit_targs
= TREE_OPERAND (template_id
, 1);
2063 if (fns
== error_mark_node
)
2064 return error_mark_node
;
2066 /* Check for baselinks. */
2067 if (BASELINK_P (fns
))
2068 fns
= BASELINK_FUNCTIONS (fns
);
2070 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2072 error ("%qD is not a function template", fns
);
2073 return error_mark_node
;
2075 else if (VAR_P (decl
) && !variable_template_p (fns
))
2077 error ("%qD is not a variable template", fns
);
2078 return error_mark_node
;
2081 /* Count the number of template headers specified for this
2084 for (b
= current_binding_level
;
2085 b
->kind
== sk_template_parms
;
2089 tree orig_fns
= fns
;
2091 if (variable_template_p (fns
))
2093 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2094 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2095 tf_warning_or_error
,
2096 /*req_all*/true, /*use_defarg*/true);
2097 if (targs
!= error_mark_node
)
2098 templates
= tree_cons (targs
, fns
, templates
);
2100 else for (lkp_iterator
iter (fns
); iter
; ++iter
)
2104 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2106 tree decl_arg_types
;
2110 /* In case of explicit specialization, we need to check if
2111 the number of template headers appearing in the specialization
2112 is correct. This is usually done in check_explicit_specialization,
2113 but the check done there cannot be exhaustive when specializing
2114 member functions. Consider the following code:
2116 template <> void A<int>::f(int);
2117 template <> template <> void A<int>::f(int);
2119 Assuming that A<int> is not itself an explicit specialization
2120 already, the first line specializes "f" which is a non-template
2121 member function, whilst the second line specializes "f" which
2122 is a template member function. So both lines are syntactically
2123 correct, and check_explicit_specialization does not reject
2126 Here, we can do better, as we are matching the specialization
2127 against the declarations. We count the number of template
2128 headers, and we check if they match TEMPLATE_COUNT + 1
2129 (TEMPLATE_COUNT is the number of qualifying template classes,
2130 plus there must be another header for the member template
2133 Notice that if header_count is zero, this is not a
2134 specialization but rather a template instantiation, so there
2135 is no check we can perform here. */
2136 if (header_count
&& header_count
!= template_count
+ 1)
2139 /* Check that the number of template arguments at the
2140 innermost level for DECL is the same as for FN. */
2141 if (current_binding_level
->kind
== sk_template_parms
2142 && !current_binding_level
->explicit_spec_p
2143 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2144 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2145 (current_template_parms
))))
2148 /* DECL might be a specialization of FN. */
2149 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2150 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2152 /* For a non-static member function, we need to make sure
2153 that the const qualification is the same. Since
2154 get_bindings does not try to merge the "this" parameter,
2155 we must do the comparison explicitly. */
2156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2157 && !same_type_p (TREE_VALUE (fn_arg_types
),
2158 TREE_VALUE (decl_arg_types
)))
2161 /* Skip the "this" parameter and, for constructors of
2162 classes with virtual bases, the VTT parameter. A
2163 full specialization of a constructor will have a VTT
2164 parameter, but a template never will. */
2166 = skip_artificial_parms_for (decl
, decl_arg_types
);
2168 = skip_artificial_parms_for (fn
, fn_arg_types
);
2170 /* Function templates cannot be specializations; there are
2171 no partial specializations of functions. Therefore, if
2172 the type of DECL does not match FN, there is no
2175 Note that it should never be the case that we have both
2176 candidates added here, and for regular member functions
2178 if (tsk
== tsk_template
)
2180 if (compparms (fn_arg_types
, decl_arg_types
))
2181 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2185 /* See whether this function might be a specialization of this
2186 template. Suppress access control because we might be trying
2187 to make this specialization a friend, and we have already done
2188 access control for the declaration of the specialization. */
2189 push_deferring_access_checks (dk_no_check
);
2190 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2191 pop_deferring_access_checks ();
2194 /* We cannot deduce template arguments that when used to
2195 specialize TMPL will produce DECL. */
2198 /* Remove, from the set of candidates, all those functions
2199 whose constraints are not satisfied. */
2200 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2203 // Then, try to form the new function type.
2204 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2205 if (insttype
== error_mark_node
)
2208 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2209 if (!compparms (fn_arg_types
, decl_arg_types
))
2212 /* Save this template, and the arguments deduced. */
2213 templates
= tree_cons (targs
, fn
, templates
);
2215 else if (need_member_template
)
2216 /* FN is an ordinary member function, and we need a
2217 specialization of a member template. */
2219 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2220 /* We can get IDENTIFIER_NODEs here in certain erroneous
2223 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2224 /* This is just an ordinary non-member function. Nothing can
2225 be a specialization of that. */
2227 else if (DECL_ARTIFICIAL (fn
))
2228 /* Cannot specialize functions that are created implicitly. */
2232 tree decl_arg_types
;
2234 /* This is an ordinary member function. However, since
2235 we're here, we can assume its enclosing class is a
2236 template class. For example,
2238 template <typename T> struct S { void f(); };
2239 template <> void S<int>::f() {}
2241 Here, S<int>::f is a non-template, but S<int> is a
2242 template class. If FN has the same type as DECL, we
2243 might be in business. */
2245 if (!DECL_TEMPLATE_INFO (fn
))
2246 /* Its enclosing class is an explicit specialization
2247 of a template class. This is not a candidate. */
2250 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2251 TREE_TYPE (TREE_TYPE (fn
))))
2252 /* The return types differ. */
2255 /* Adjust the type of DECL in case FN is a static member. */
2256 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2257 if (DECL_STATIC_FUNCTION_P (fn
)
2258 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2259 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2261 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2265 // If the deduced arguments do not satisfy the constraints,
2266 // this is not a candidate.
2267 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2270 // Add the candidate.
2271 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2275 if (templates
&& TREE_CHAIN (templates
))
2281 It is possible for a specialization with a given function
2282 signature to be instantiated from more than one function
2283 template. In such cases, explicit specification of the
2284 template arguments must be used to uniquely identify the
2285 function template specialization being specialized.
2287 Note that here, there's no suggestion that we're supposed to
2288 determine which of the candidate templates is most
2289 specialized. However, we, also have:
2293 Partial ordering of overloaded function template
2294 declarations is used in the following contexts to select
2295 the function template to which a function template
2296 specialization refers:
2298 -- when an explicit specialization refers to a function
2301 So, we do use the partial ordering rules, at least for now.
2302 This extension can only serve to make invalid programs valid,
2303 so it's safe. And, there is strong anecdotal evidence that
2304 the committee intended the partial ordering rules to apply;
2305 the EDG front end has that behavior, and John Spicer claims
2306 that the committee simply forgot to delete the wording in
2307 [temp.expl.spec]. */
2308 tree tmpl
= most_specialized_instantiation (templates
);
2309 if (tmpl
!= error_mark_node
)
2312 TREE_CHAIN (templates
) = NULL_TREE
;
2316 // Concepts allows multiple declarations of member functions
2317 // with the same signature. Like above, we need to rely on
2318 // on the partial ordering of those candidates to determine which
2320 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2322 if (tree cand
= most_constrained_function (candidates
))
2325 TREE_CHAIN (cand
) = NULL_TREE
;
2329 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2331 error ("template-id %qD for %q+D does not match any template "
2332 "declaration", template_id
, decl
);
2333 if (header_count
&& header_count
!= template_count
+ 1)
2334 inform (input_location
, "saw %d %<template<>%>, need %d for "
2335 "specializing a member function template",
2336 header_count
, template_count
+ 1);
2338 print_candidates (orig_fns
);
2339 return error_mark_node
;
2341 else if ((templates
&& TREE_CHAIN (templates
))
2342 || (candidates
&& TREE_CHAIN (candidates
))
2343 || (templates
&& candidates
))
2345 error ("ambiguous template specialization %qD for %q+D",
2347 candidates
= chainon (candidates
, templates
);
2348 print_candidates (candidates
);
2349 return error_mark_node
;
2352 /* We have one, and exactly one, match. */
2355 tree fn
= TREE_VALUE (candidates
);
2356 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2358 // Propagate the candidate's constraints to the declaration.
2359 set_constraints (decl
, get_constraints (fn
));
2361 /* DECL is a re-declaration or partial instantiation of a template
2363 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2365 /* It was a specialization of an ordinary member function in a
2367 return DECL_TI_TEMPLATE (fn
);
2370 /* It was a specialization of a template. */
2371 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2372 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2374 *targs_out
= copy_node (targs
);
2375 SET_TMPL_ARGS_LEVEL (*targs_out
,
2376 TMPL_ARGS_DEPTH (*targs_out
),
2377 TREE_PURPOSE (templates
));
2380 *targs_out
= TREE_PURPOSE (templates
);
2381 return TREE_VALUE (templates
);
2384 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2385 but with the default argument values filled in from those in the
2389 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2392 tree new_spec_types
;
2397 if (spec_types
== void_list_node
)
2398 return void_list_node
;
2400 /* Substitute into the rest of the list. */
2402 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2403 TREE_CHAIN (tmpl_types
));
2405 /* Add the default argument for this parameter. */
2406 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2407 TREE_VALUE (spec_types
),
2411 /* DECL is an explicit specialization. Replicate default arguments
2412 from the template it specializes. (That way, code like:
2414 template <class T> void f(T = 3);
2415 template <> void f(double);
2418 works, as required.) An alternative approach would be to look up
2419 the correct default arguments at the call-site, but this approach
2420 is consistent with how implicit instantiations are handled. */
2423 copy_default_args_to_explicit_spec (tree decl
)
2428 tree new_spec_types
;
2432 tree object_type
= NULL_TREE
;
2433 tree in_charge
= NULL_TREE
;
2434 tree vtt
= NULL_TREE
;
2436 /* See if there's anything we need to do. */
2437 tmpl
= DECL_TI_TEMPLATE (decl
);
2438 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2439 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2440 if (TREE_PURPOSE (t
))
2445 old_type
= TREE_TYPE (decl
);
2446 spec_types
= TYPE_ARG_TYPES (old_type
);
2448 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2450 /* Remove the this pointer, but remember the object's type for
2452 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2453 spec_types
= TREE_CHAIN (spec_types
);
2454 tmpl_types
= TREE_CHAIN (tmpl_types
);
2456 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2458 /* DECL may contain more parameters than TMPL due to the extra
2459 in-charge parameter in constructors and destructors. */
2460 in_charge
= spec_types
;
2461 spec_types
= TREE_CHAIN (spec_types
);
2463 if (DECL_HAS_VTT_PARM_P (decl
))
2466 spec_types
= TREE_CHAIN (spec_types
);
2470 /* Compute the merged default arguments. */
2472 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2474 /* Compute the new FUNCTION_TYPE. */
2478 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2483 /* Put the in-charge parameter back. */
2484 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2485 TREE_VALUE (in_charge
),
2488 new_type
= build_method_type_directly (object_type
,
2489 TREE_TYPE (old_type
),
2493 new_type
= build_function_type (TREE_TYPE (old_type
),
2495 new_type
= cp_build_type_attribute_variant (new_type
,
2496 TYPE_ATTRIBUTES (old_type
));
2497 new_type
= build_exception_variant (new_type
,
2498 TYPE_RAISES_EXCEPTIONS (old_type
));
2500 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2501 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2503 TREE_TYPE (decl
) = new_type
;
2506 /* Return the number of template headers we expect to see for a definition
2507 or specialization of CTYPE or one of its non-template members. */
2510 num_template_headers_for_class (tree ctype
)
2512 int num_templates
= 0;
2514 while (ctype
&& CLASS_TYPE_P (ctype
))
2516 /* You're supposed to have one `template <...>' for every
2517 template class, but you don't need one for a full
2518 specialization. For example:
2520 template <class T> struct S{};
2521 template <> struct S<int> { void f(); };
2522 void S<int>::f () {}
2524 is correct; there shouldn't be a `template <>' for the
2525 definition of `S<int>::f'. */
2526 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2527 /* If CTYPE does not have template information of any
2528 kind, then it is not a template, nor is it nested
2529 within a template. */
2531 if (explicit_class_specialization_p (ctype
))
2533 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2536 ctype
= TYPE_CONTEXT (ctype
);
2539 return num_templates
;
2542 /* Do a simple sanity check on the template headers that precede the
2543 variable declaration DECL. */
2546 check_template_variable (tree decl
)
2548 tree ctx
= CP_DECL_CONTEXT (decl
);
2549 int wanted
= num_template_headers_for_class (ctx
);
2550 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2551 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2553 if (cxx_dialect
< cxx14
)
2554 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2555 "variable templates only available with "
2556 "-std=c++14 or -std=gnu++14");
2558 // Namespace-scope variable templates should have a template header.
2561 if (template_header_count
> wanted
)
2563 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2564 "too many template headers for %qD "
2567 if (warned
&& CLASS_TYPE_P (ctx
)
2568 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2569 inform (DECL_SOURCE_LOCATION (decl
),
2570 "members of an explicitly specialized class are defined "
2571 "without a template header");
2575 /* An explicit specialization whose declarator-id or class-head-name is not
2576 qualified shall be declared in the nearest enclosing namespace of the
2577 template, or, if the namespace is inline (7.3.1), any namespace from its
2578 enclosing namespace set.
2580 If the name declared in the explicit instantiation is an unqualified name,
2581 the explicit instantiation shall appear in the namespace where its template
2582 is declared or, if that namespace is inline (7.3.1), any namespace from its
2583 enclosing namespace set. */
2586 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2588 tree tmpl
= most_general_template (t
);
2589 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2590 && !is_nested_namespace (current_namespace
,
2591 CP_DECL_CONTEXT (tmpl
), true))
2593 if (processing_specialization
)
2594 permerror (loc
, "explicit specialization of %qD outside its "
2595 "namespace must use a nested-name-specifier", tmpl
);
2596 else if (processing_explicit_instantiation
2597 && cxx_dialect
>= cxx11
)
2598 /* This was allowed in C++98, so only pedwarn. */
2599 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2600 "outside its namespace must use a nested-name-"
2605 /* Check to see if the function just declared, as indicated in
2606 DECLARATOR, and in DECL, is a specialization of a function
2607 template. We may also discover that the declaration is an explicit
2608 instantiation at this point.
2610 Returns DECL, or an equivalent declaration that should be used
2611 instead if all goes well. Issues an error message if something is
2612 amiss. Returns error_mark_node if the error is not easily
2615 FLAGS is a bitmask consisting of the following flags:
2617 2: The function has a definition.
2618 4: The function is a friend.
2620 The TEMPLATE_COUNT is the number of references to qualifying
2621 template classes that appeared in the name of the function. For
2624 template <class T> struct S { void f(); };
2627 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2628 classes are not counted in the TEMPLATE_COUNT, so that in
2630 template <class T> struct S {};
2631 template <> struct S<int> { void f(); }
2632 template <> void S<int>::f();
2634 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2635 invalid; there should be no template <>.)
2637 If the function is a specialization, it is marked as such via
2638 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2639 is set up correctly, and it is added to the list of specializations
2640 for that template. */
2643 check_explicit_specialization (tree declarator
,
2648 int have_def
= flags
& 2;
2649 int is_friend
= flags
& 4;
2650 bool is_concept
= flags
& 8;
2651 int specialization
= 0;
2652 int explicit_instantiation
= 0;
2653 int member_specialization
= 0;
2654 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2655 tree dname
= DECL_NAME (decl
);
2660 if (!processing_specialization
)
2663 tsk
= tsk_excessive_parms
;
2666 tsk
= current_tmpl_spec_kind (template_count
);
2671 if (processing_specialization
&& !VAR_P (decl
))
2674 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2676 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2679 /* This could be something like:
2681 template <class T> void f(T);
2682 class S { friend void f<>(int); } */
2686 /* This case handles bogus declarations like template <>
2687 template <class T> void f<int>(); */
2689 error ("template-id %qD in declaration of primary template",
2696 case tsk_invalid_member_spec
:
2697 /* The error has already been reported in
2698 check_specialization_scope. */
2699 return error_mark_node
;
2701 case tsk_invalid_expl_inst
:
2702 error ("template parameter list used in explicit instantiation");
2708 error ("definition provided for explicit instantiation");
2710 explicit_instantiation
= 1;
2713 case tsk_excessive_parms
:
2714 case tsk_insufficient_parms
:
2715 if (tsk
== tsk_excessive_parms
)
2716 error ("too many template parameter lists in declaration of %qD",
2718 else if (template_header_count
)
2719 error("too few template parameter lists in declaration of %qD", decl
);
2721 error("explicit specialization of %qD must be introduced by "
2722 "%<template <>%>", decl
);
2727 error ("explicit specialization declared %<concept%>");
2729 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2730 /* In cases like template<> constexpr bool v = true;
2731 We'll give an error in check_template_variable. */
2734 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2736 member_specialization
= 1;
2742 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2744 /* This case handles bogus declarations like template <>
2745 template <class T> void f<int>(); */
2747 if (!uses_template_parms (declarator
))
2748 error ("template-id %qD in declaration of primary template",
2750 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2752 /* Partial specialization of variable template. */
2753 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2757 else if (cxx_dialect
< cxx14
)
2758 error ("non-type partial specialization %qD "
2759 "is not allowed", declarator
);
2761 error ("non-class, non-variable partial specialization %qD "
2762 "is not allowed", declarator
);
2767 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2768 /* This is a specialization of a member template, without
2769 specialization the containing class. Something like:
2771 template <class T> struct S {
2772 template <class U> void f (U);
2774 template <> template <class U> void S<int>::f(U) {}
2776 That's a specialization -- but of the entire template. */
2784 if ((specialization
|| member_specialization
)
2785 /* This doesn't apply to variable templates. */
2786 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2787 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2789 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2790 for (; t
; t
= TREE_CHAIN (t
))
2791 if (TREE_PURPOSE (t
))
2793 permerror (input_location
,
2794 "default argument specified in explicit specialization");
2799 if (specialization
|| member_specialization
|| explicit_instantiation
)
2801 tree tmpl
= NULL_TREE
;
2802 tree targs
= NULL_TREE
;
2803 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2805 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2806 if (!was_template_id
)
2810 gcc_assert (identifier_p (declarator
));
2815 /* If there is no class context, the explicit instantiation
2816 must be at namespace scope. */
2817 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2819 /* Find the namespace binding, using the declaration
2821 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2823 if (fns
== error_mark_node
)
2824 /* If lookup fails, look for a friend declaration so we can
2825 give a better diagnostic. */
2826 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2827 /*type*/false, /*complain*/true,
2830 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2832 error ("%qD is not a template function", dname
);
2833 fns
= error_mark_node
;
2837 declarator
= lookup_template_function (fns
, NULL_TREE
);
2840 if (declarator
== error_mark_node
)
2841 return error_mark_node
;
2843 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2845 if (!explicit_instantiation
)
2846 /* A specialization in class scope. This is invalid,
2847 but the error will already have been flagged by
2848 check_specialization_scope. */
2849 return error_mark_node
;
2852 /* It's not valid to write an explicit instantiation in
2855 class C { template void f(); }
2857 This case is caught by the parser. However, on
2860 template class C { void f(); };
2862 (which is invalid) we can get here. The error will be
2869 else if (ctype
!= NULL_TREE
2870 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2872 // We'll match variable templates in start_decl.
2876 /* Find the list of functions in ctype that have the same
2877 name as the declared function. */
2878 tree name
= TREE_OPERAND (declarator
, 0);
2879 tree fns
= NULL_TREE
;
2882 if (constructor_name_p (name
, ctype
))
2884 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2886 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2887 : !CLASSTYPE_DESTRUCTORS (ctype
))
2889 /* From [temp.expl.spec]:
2891 If such an explicit specialization for the member
2892 of a class template names an implicitly-declared
2893 special member function (clause _special_), the
2894 program is ill-formed.
2896 Similar language is found in [temp.explicit]. */
2897 error ("specialization of implicitly-declared special member function");
2898 return error_mark_node
;
2901 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2904 if (!DECL_CONV_FN_P (decl
))
2906 idx
= lookup_fnfields_1 (ctype
, name
);
2908 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2912 vec
<tree
, va_gc
> *methods
;
2915 /* For a type-conversion operator, we cannot do a
2916 name-based lookup. We might be looking for `operator
2917 int' which will be a specialization of `operator T'.
2918 So, we find *all* the conversion operators, and then
2919 select from them. */
2922 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2924 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2925 methods
->iterate (idx
, &ovl
);
2928 if (!DECL_CONV_FN_P (OVL_FIRST (ovl
)))
2929 /* There are no more conversion functions. */
2932 /* Glue all these conversion functions together
2933 with those we already have. */
2934 fns
= lookup_add (ovl
, fns
);
2938 if (fns
== NULL_TREE
)
2940 error ("no member function %qD declared in %qT", name
, ctype
);
2941 return error_mark_node
;
2944 TREE_OPERAND (declarator
, 0) = fns
;
2947 /* Figure out what exactly is being specialized at this point.
2948 Note that for an explicit instantiation, even one for a
2949 member function, we cannot tell a priori whether the
2950 instantiation is for a member template, or just a member
2951 function of a template class. Even if a member template is
2952 being instantiated, the member template arguments may be
2953 elided if they can be deduced from the rest of the
2955 tmpl
= determine_specialization (declarator
, decl
,
2957 member_specialization
,
2961 if (!tmpl
|| tmpl
== error_mark_node
)
2962 /* We couldn't figure out what this declaration was
2964 return error_mark_node
;
2967 if (TREE_CODE (decl
) == FUNCTION_DECL
2968 && DECL_HIDDEN_FRIEND_P (tmpl
))
2970 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2971 "friend declaration %qD is not visible to "
2972 "explicit specialization", tmpl
))
2973 inform (DECL_SOURCE_LOCATION (tmpl
),
2974 "friend declaration here");
2976 else if (!ctype
&& !is_friend
2977 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2978 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2980 tree gen_tmpl
= most_general_template (tmpl
);
2982 if (explicit_instantiation
)
2984 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2985 is done by do_decl_instantiation later. */
2987 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2988 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2990 if (arg_depth
> parm_depth
)
2992 /* If TMPL is not the most general template (for
2993 example, if TMPL is a friend template that is
2994 injected into namespace scope), then there will
2995 be too many levels of TARGS. Remove some of them
3000 new_targs
= make_tree_vec (parm_depth
);
3001 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3002 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3003 = TREE_VEC_ELT (targs
, i
);
3007 return instantiate_template (tmpl
, targs
, tf_error
);
3010 /* If we thought that the DECL was a member function, but it
3011 turns out to be specializing a static member function,
3012 make DECL a static member function as well. */
3013 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3014 && DECL_STATIC_FUNCTION_P (tmpl
)
3015 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3016 revert_static_member_fn (decl
);
3018 /* If this is a specialization of a member template of a
3019 template class, we want to return the TEMPLATE_DECL, not
3020 the specialization of it. */
3021 if (tsk
== tsk_template
&& !was_template_id
)
3023 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3025 DECL_INITIAL (result
) = NULL_TREE
;
3029 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3030 DECL_SOURCE_LOCATION (result
)
3031 = DECL_SOURCE_LOCATION (decl
);
3032 /* We want to use the argument list specified in the
3033 definition, not in the original declaration. */
3034 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3035 for (parm
= DECL_ARGUMENTS (result
); parm
;
3036 parm
= DECL_CHAIN (parm
))
3037 DECL_CONTEXT (parm
) = result
;
3039 return register_specialization (tmpl
, gen_tmpl
, targs
,
3043 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3044 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3046 if (was_template_id
)
3047 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3049 /* Inherit default function arguments from the template
3050 DECL is specializing. */
3051 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3052 copy_default_args_to_explicit_spec (decl
);
3054 /* This specialization has the same protection as the
3055 template it specializes. */
3056 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3057 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3059 /* 7.1.1-1 [dcl.stc]
3061 A storage-class-specifier shall not be specified in an
3062 explicit specialization...
3064 The parser rejects these, so unless action is taken here,
3065 explicit function specializations will always appear with
3068 The action recommended by the C++ CWG in response to C++
3069 defect report 605 is to make the storage class and linkage
3070 of the explicit specialization match the templated function:
3072 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3074 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3076 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3077 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3079 /* A concept cannot be specialized. */
3080 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3082 error ("explicit specialization of function concept %qD",
3084 return error_mark_node
;
3087 /* This specialization has the same linkage and visibility as
3088 the function template it specializes. */
3089 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3090 if (! TREE_PUBLIC (decl
))
3092 DECL_INTERFACE_KNOWN (decl
) = 1;
3093 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3095 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3096 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3098 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3099 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3103 /* If DECL is a friend declaration, declared using an
3104 unqualified name, the namespace associated with DECL may
3105 have been set incorrectly. For example, in:
3107 template <typename T> void f(T);
3109 struct S { friend void f<int>(int); }
3112 we will have set the DECL_CONTEXT for the friend
3113 declaration to N, rather than to the global namespace. */
3114 if (DECL_NAMESPACE_SCOPE_P (decl
))
3115 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3117 if (is_friend
&& !have_def
)
3118 /* This is not really a declaration of a specialization.
3119 It's just the name of an instantiation. But, it's not
3120 a request for an instantiation, either. */
3121 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3122 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3123 /* A specialization is not necessarily COMDAT. */
3124 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3125 && DECL_DECLARED_INLINE_P (decl
));
3126 else if (VAR_P (decl
))
3127 DECL_COMDAT (decl
) = false;
3129 /* If this is a full specialization, register it so that we can find
3130 it again. Partial specializations will be registered in
3131 process_partial_specialization. */
3132 if (!processing_template_decl
)
3133 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3136 /* A 'structor should already have clones. */
3137 gcc_assert (decl
== error_mark_node
3138 || variable_template_p (tmpl
)
3139 || !(DECL_CONSTRUCTOR_P (decl
)
3140 || DECL_DESTRUCTOR_P (decl
))
3141 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3148 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3149 parameters. These are represented in the same format used for
3150 DECL_TEMPLATE_PARMS. */
3153 comp_template_parms (const_tree parms1
, const_tree parms2
)
3158 if (parms1
== parms2
)
3161 for (p1
= parms1
, p2
= parms2
;
3162 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3163 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3165 tree t1
= TREE_VALUE (p1
);
3166 tree t2
= TREE_VALUE (p2
);
3169 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3170 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3172 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3175 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3177 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3178 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3180 /* If either of the template parameters are invalid, assume
3181 they match for the sake of error recovery. */
3182 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3185 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3188 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3189 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3190 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3192 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3197 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3198 /* One set of parameters has more parameters lists than the
3205 /* Determine whether PARM is a parameter pack. */
3208 template_parameter_pack_p (const_tree parm
)
3210 /* Determine if we have a non-type template parameter pack. */
3211 if (TREE_CODE (parm
) == PARM_DECL
)
3212 return (DECL_TEMPLATE_PARM_P (parm
)
3213 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3214 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3215 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3217 /* If this is a list of template parameters, we could get a
3218 TYPE_DECL or a TEMPLATE_DECL. */
3219 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3220 parm
= TREE_TYPE (parm
);
3222 /* Otherwise it must be a type template parameter. */
3223 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3224 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3225 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3228 /* Determine if T is a function parameter pack. */
3231 function_parameter_pack_p (const_tree t
)
3233 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3234 return DECL_PACK_P (t
);
3238 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3239 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3242 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3244 if (! primary_func_tmpl_inst
3245 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3246 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3249 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3252 /* Return true iff the function parameter PARAM_DECL was expanded
3253 from the function parameter pack PACK. */
3256 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3258 if (DECL_ARTIFICIAL (param_decl
)
3259 || !function_parameter_pack_p (pack
))
3262 /* The parameter pack and its pack arguments have the same
3264 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3267 /* Determine whether ARGS describes a variadic template args list,
3268 i.e., one that is terminated by a template argument pack. */
3271 template_args_variadic_p (tree args
)
3276 if (args
== NULL_TREE
)
3279 args
= INNERMOST_TEMPLATE_ARGS (args
);
3280 nargs
= TREE_VEC_LENGTH (args
);
3285 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3287 return ARGUMENT_PACK_P (last_parm
);
3290 /* Generate a new name for the parameter pack name NAME (an
3291 IDENTIFIER_NODE) that incorporates its */
3294 make_ith_pack_parameter_name (tree name
, int i
)
3296 /* Munge the name to include the parameter index. */
3297 #define NUMBUF_LEN 128
3298 char numbuf
[NUMBUF_LEN
];
3302 if (name
== NULL_TREE
)
3304 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3305 newname_len
= IDENTIFIER_LENGTH (name
)
3306 + strlen (numbuf
) + 2;
3307 newname
= (char*)alloca (newname_len
);
3308 snprintf (newname
, newname_len
,
3309 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3310 return get_identifier (newname
);
3313 /* Return true if T is a primary function, class or alias template
3317 primary_template_instantiation_p (const_tree t
)
3322 if (TREE_CODE (t
) == FUNCTION_DECL
)
3323 return DECL_LANG_SPECIFIC (t
)
3324 && DECL_TEMPLATE_INSTANTIATION (t
)
3325 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3326 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3327 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3328 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3329 else if (alias_template_specialization_p (t
))
3334 /* Return true if PARM is a template template parameter. */
3337 template_template_parameter_p (const_tree parm
)
3339 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3342 /* Return true iff PARM is a DECL representing a type template
3346 template_type_parameter_p (const_tree parm
)
3349 && (TREE_CODE (parm
) == TYPE_DECL
3350 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3351 && DECL_TEMPLATE_PARM_P (parm
));
3354 /* Return the template parameters of T if T is a
3355 primary template instantiation, NULL otherwise. */
3358 get_primary_template_innermost_parameters (const_tree t
)
3360 tree parms
= NULL
, template_info
= NULL
;
3362 if ((template_info
= get_template_info (t
))
3363 && primary_template_instantiation_p (t
))
3364 parms
= INNERMOST_TEMPLATE_PARMS
3365 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3370 /* Return the template parameters of the LEVELth level from the full list
3371 of template parameters PARMS. */
3374 get_template_parms_at_level (tree parms
, int level
)
3378 || TREE_CODE (parms
) != TREE_LIST
3379 || level
> TMPL_PARMS_DEPTH (parms
))
3382 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3383 if (TMPL_PARMS_DEPTH (p
) == level
)
3389 /* Returns the template arguments of T if T is a template instantiation,
3393 get_template_innermost_arguments (const_tree t
)
3395 tree args
= NULL
, template_info
= NULL
;
3397 if ((template_info
= get_template_info (t
))
3398 && TI_ARGS (template_info
))
3399 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3404 /* Return the argument pack elements of T if T is a template argument pack,
3408 get_template_argument_pack_elems (const_tree t
)
3410 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3411 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3414 return ARGUMENT_PACK_ARGS (t
);
3417 /* True iff FN is a function representing a built-in variadic parameter
3421 builtin_pack_fn_p (tree fn
)
3424 || TREE_CODE (fn
) != FUNCTION_DECL
3425 || !DECL_IS_BUILTIN (fn
))
3428 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3434 /* True iff CALL is a call to a function representing a built-in variadic
3438 builtin_pack_call_p (tree call
)
3440 if (TREE_CODE (call
) != CALL_EXPR
)
3442 return builtin_pack_fn_p (CALL_EXPR_FN (call
));
3445 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3448 expand_integer_pack (tree call
, tree args
, tsubst_flags_t complain
,
3451 tree ohi
= CALL_EXPR_ARG (call
, 0);
3452 tree hi
= tsubst_copy_and_build (ohi
, args
, complain
, in_decl
,
3453 false/*fn*/, true/*int_cst*/);
3455 if (value_dependent_expression_p (hi
))
3459 call
= copy_node (call
);
3460 CALL_EXPR_ARG (call
, 0) = hi
;
3462 tree ex
= make_pack_expansion (call
);
3463 tree vec
= make_tree_vec (1);
3464 TREE_VEC_ELT (vec
, 0) = ex
;
3469 hi
= cxx_constant_value (hi
);
3470 int len
= valid_constant_size_p (hi
) ? tree_to_shwi (hi
) : -1;
3472 /* Calculate the largest value of len that won't make the size of the vec
3473 overflow an int. The compiler will exceed resource limits long before
3474 this, but it seems a decent place to diagnose. */
3475 int max
= ((INT_MAX
- sizeof (tree_vec
)) / sizeof (tree
)) + 1;
3477 if (len
< 0 || len
> max
)
3479 if ((complain
& tf_error
)
3480 && hi
!= error_mark_node
)
3481 error ("argument to __integer_pack must be between 0 and %d", max
);
3482 return error_mark_node
;
3485 tree vec
= make_tree_vec (len
);
3487 for (int i
= 0; i
< len
; ++i
)
3488 TREE_VEC_ELT (vec
, i
) = size_int (i
);
3494 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3498 expand_builtin_pack_call (tree call
, tree args
, tsubst_flags_t complain
,
3501 if (!builtin_pack_call_p (call
))
3504 tree fn
= CALL_EXPR_FN (call
);
3506 if (id_equal (DECL_NAME (fn
), "__integer_pack"))
3507 return expand_integer_pack (call
, args
, complain
, in_decl
);
3512 /* Structure used to track the progress of find_parameter_packs_r. */
3513 struct find_parameter_pack_data
3515 /* TREE_LIST that will contain all of the parameter packs found by
3517 tree
* parameter_packs
;
3519 /* Set of AST nodes that have been visited by the traversal. */
3520 hash_set
<tree
> *visited
;
3522 /* True iff we're making a type pack expansion. */
3523 bool type_pack_expansion_p
;
3526 /* Identifies all of the argument packs that occur in a template
3527 argument and appends them to the TREE_LIST inside DATA, which is a
3528 find_parameter_pack_data structure. This is a subroutine of
3529 make_pack_expansion and uses_parameter_packs. */
3531 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3534 struct find_parameter_pack_data
* ppd
=
3535 (struct find_parameter_pack_data
*)data
;
3536 bool parameter_pack_p
= false;
3538 /* Handle type aliases/typedefs. */
3539 if (TYPE_ALIAS_P (t
))
3541 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3542 cp_walk_tree (&TI_ARGS (tinfo
),
3543 &find_parameter_packs_r
,
3549 /* Identify whether this is a parameter pack or not. */
3550 switch (TREE_CODE (t
))
3552 case TEMPLATE_PARM_INDEX
:
3553 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3554 parameter_pack_p
= true;
3557 case TEMPLATE_TYPE_PARM
:
3558 t
= TYPE_MAIN_VARIANT (t
);
3560 case TEMPLATE_TEMPLATE_PARM
:
3561 /* If the placeholder appears in the decl-specifier-seq of a function
3562 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3563 is a pack expansion, the invented template parameter is a template
3565 if (ppd
->type_pack_expansion_p
&& is_auto (t
))
3566 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3567 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3568 parameter_pack_p
= true;
3573 if (DECL_PACK_P (t
))
3575 /* We don't want to walk into the type of a PARM_DECL,
3576 because we don't want to see the type parameter pack. */
3578 parameter_pack_p
= true;
3582 /* Look through a lambda capture proxy to the field pack. */
3584 if (DECL_HAS_VALUE_EXPR_P (t
))
3586 tree v
= DECL_VALUE_EXPR (t
);
3588 &find_parameter_packs_r
,
3592 else if (variable_template_specialization_p (t
))
3594 cp_walk_tree (&DECL_TI_ARGS (t
),
3595 find_parameter_packs_r
,
3602 if (builtin_pack_call_p (t
))
3603 parameter_pack_p
= true;
3607 parameter_pack_p
= true;
3610 /* Not a parameter pack. */
3614 if (parameter_pack_p
)
3616 /* Add this parameter pack to the list. */
3617 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3621 cp_walk_tree (&TYPE_CONTEXT (t
),
3622 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3624 /* This switch statement will return immediately if we don't find a
3626 switch (TREE_CODE (t
))
3628 case TEMPLATE_PARM_INDEX
:
3631 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3632 /* Check the template itself. */
3633 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3634 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3635 /* Check the template arguments. */
3636 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3641 case TEMPLATE_TYPE_PARM
:
3642 case TEMPLATE_TEMPLATE_PARM
:
3649 if (TYPE_PTRMEMFUNC_P (t
))
3655 if (TYPE_TEMPLATE_INFO (t
))
3656 cp_walk_tree (&TYPE_TI_ARGS (t
),
3657 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3663 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3668 cp_walk_tree (&TREE_TYPE (t
),
3669 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3673 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3678 case TYPE_PACK_EXPANSION
:
3679 case EXPR_PACK_EXPANSION
:
3684 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3689 case IDENTIFIER_NODE
:
3690 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3697 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3698 type_pack_expansion_p to false so that any placeholders
3699 within the expression don't get marked as parameter packs. */
3700 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3701 ppd
->type_pack_expansion_p
= false;
3702 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3704 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3716 /* Determines if the expression or type T uses any parameter packs. */
3718 uses_parameter_packs (tree t
)
3720 tree parameter_packs
= NULL_TREE
;
3721 struct find_parameter_pack_data ppd
;
3722 ppd
.parameter_packs
= ¶meter_packs
;
3723 ppd
.visited
= new hash_set
<tree
>;
3724 ppd
.type_pack_expansion_p
= false;
3725 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3727 return parameter_packs
!= NULL_TREE
;
3730 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3731 representation a base-class initializer into a parameter pack
3732 expansion. If all goes well, the resulting node will be an
3733 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3736 make_pack_expansion (tree arg
)
3739 tree parameter_packs
= NULL_TREE
;
3740 bool for_types
= false;
3741 struct find_parameter_pack_data ppd
;
3743 if (!arg
|| arg
== error_mark_node
)
3746 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3748 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3749 class initializer. In this case, the TREE_PURPOSE will be a
3750 _TYPE node (representing the base class expansion we're
3751 initializing) and the TREE_VALUE will be a TREE_LIST
3752 containing the initialization arguments.
3754 The resulting expansion looks somewhat different from most
3755 expansions. Rather than returning just one _EXPANSION, we
3756 return a TREE_LIST whose TREE_PURPOSE is a
3757 TYPE_PACK_EXPANSION containing the bases that will be
3758 initialized. The TREE_VALUE will be identical to the
3759 original TREE_VALUE, which is a list of arguments that will
3760 be passed to each base. We do not introduce any new pack
3761 expansion nodes into the TREE_VALUE (although it is possible
3762 that some already exist), because the TREE_PURPOSE and
3763 TREE_VALUE all need to be expanded together with the same
3764 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3765 resulting TREE_PURPOSE will mention the parameter packs in
3766 both the bases and the arguments to the bases. */
3769 tree parameter_packs
= NULL_TREE
;
3771 /* Determine which parameter packs will be used by the base
3773 ppd
.visited
= new hash_set
<tree
>;
3774 ppd
.parameter_packs
= ¶meter_packs
;
3775 ppd
.type_pack_expansion_p
= true;
3776 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3777 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3780 if (parameter_packs
== NULL_TREE
)
3782 error ("base initializer expansion %qT contains no parameter packs", arg
);
3784 return error_mark_node
;
3787 if (TREE_VALUE (arg
) != void_type_node
)
3789 /* Collect the sets of parameter packs used in each of the
3790 initialization arguments. */
3791 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3793 /* Determine which parameter packs will be expanded in this
3795 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3802 /* Create the pack expansion type for the base type. */
3803 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3804 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3805 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3807 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3808 they will rarely be compared to anything. */
3809 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3811 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3814 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3817 /* Build the PACK_EXPANSION_* node. */
3819 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3820 : make_node (EXPR_PACK_EXPANSION
);
3821 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3822 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3824 /* Propagate type and const-expression information. */
3825 TREE_TYPE (result
) = TREE_TYPE (arg
);
3826 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3827 /* Mark this read now, since the expansion might be length 0. */
3828 mark_exp_read (arg
);
3831 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3832 they will rarely be compared to anything. */
3833 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3835 /* Determine which parameter packs will be expanded. */
3836 ppd
.parameter_packs
= ¶meter_packs
;
3837 ppd
.visited
= new hash_set
<tree
>;
3838 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3839 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3842 /* Make sure we found some parameter packs. */
3843 if (parameter_packs
== NULL_TREE
)
3846 error ("expansion pattern %qT contains no argument packs", arg
);
3848 error ("expansion pattern %qE contains no argument packs", arg
);
3849 return error_mark_node
;
3851 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3853 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3863 foo(f(g(h(args)))...)
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3873 check_for_bare_parameter_packs (tree t
)
3875 tree parameter_packs
= NULL_TREE
;
3876 struct find_parameter_pack_data ppd
;
3878 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3881 if (TREE_CODE (t
) == TYPE_DECL
)
3884 ppd
.parameter_packs
= ¶meter_packs
;
3885 ppd
.visited
= new hash_set
<tree
>;
3886 ppd
.type_pack_expansion_p
= false;
3887 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3890 if (parameter_packs
)
3892 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3893 error_at (loc
, "parameter packs not expanded with %<...%>:");
3894 while (parameter_packs
)
3896 tree pack
= TREE_VALUE (parameter_packs
);
3897 tree name
= NULL_TREE
;
3899 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3900 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3901 name
= TYPE_NAME (pack
);
3902 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3903 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3904 else if (TREE_CODE (pack
) == CALL_EXPR
)
3905 name
= DECL_NAME (CALL_EXPR_FN (pack
));
3907 name
= DECL_NAME (pack
);
3910 inform (loc
, " %qD", name
);
3912 inform (loc
, " <anonymous>");
3914 parameter_packs
= TREE_CHAIN (parameter_packs
);
3923 /* Expand any parameter packs that occur in the template arguments in
3926 expand_template_argument_pack (tree args
)
3928 if (args
== error_mark_node
)
3929 return error_mark_node
;
3931 tree result_args
= NULL_TREE
;
3932 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3933 int num_result_args
= -1;
3934 int non_default_args_count
= -1;
3936 /* First, determine if we need to expand anything, and the number of
3937 slots we'll need. */
3938 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3940 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3941 if (arg
== NULL_TREE
)
3943 if (ARGUMENT_PACK_P (arg
))
3945 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3946 if (num_result_args
< 0)
3947 num_result_args
= in_arg
+ num_packed
;
3949 num_result_args
+= num_packed
;
3953 if (num_result_args
>= 0)
3958 /* If no expansion is necessary, we're done. */
3959 if (num_result_args
< 0)
3962 /* Expand arguments. */
3963 result_args
= make_tree_vec (num_result_args
);
3964 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3965 non_default_args_count
=
3966 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3967 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3969 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3970 if (ARGUMENT_PACK_P (arg
))
3972 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3973 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3974 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3975 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3976 if (non_default_args_count
> 0)
3977 non_default_args_count
+= num_packed
- 1;
3981 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3985 if (non_default_args_count
>= 0)
3986 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3990 /* Checks if DECL shadows a template parameter.
3992 [temp.local]: A template-parameter shall not be redeclared within its
3993 scope (including nested scopes).
3995 Emits an error and returns TRUE if the DECL shadows a parameter,
3996 returns FALSE otherwise. */
3999 check_template_shadow (tree decl
)
4003 /* If we're not in a template, we can't possibly shadow a template
4005 if (!current_template_parms
)
4008 /* Figure out what we're shadowing. */
4009 decl
= OVL_FIRST (decl
);
4010 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
4012 /* If there's no previous binding for this name, we're not shadowing
4013 anything, let alone a template parameter. */
4017 /* If we're not shadowing a template parameter, we're done. Note
4018 that OLDDECL might be an OVERLOAD (or perhaps even an
4019 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4021 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
4024 /* We check for decl != olddecl to avoid bogus errors for using a
4025 name inside a class. We check TPFI to avoid duplicate errors for
4026 inline member templates. */
4028 || (DECL_TEMPLATE_PARM_P (decl
)
4029 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
4032 /* Don't complain about the injected class name, as we've already
4033 complained about the class itself. */
4034 if (DECL_SELF_REFERENCE_P (decl
))
4037 if (DECL_TEMPLATE_PARM_P (decl
))
4038 error ("declaration of template parameter %q+D shadows "
4039 "template parameter", decl
);
4041 error ("declaration of %q+#D shadows template parameter", decl
);
4042 inform (DECL_SOURCE_LOCATION (olddecl
),
4043 "template parameter %qD declared here", olddecl
);
4047 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4048 ORIG_LEVEL, DECL, and TYPE. */
4051 build_template_parm_index (int index
,
4057 tree t
= make_node (TEMPLATE_PARM_INDEX
);
4058 TEMPLATE_PARM_IDX (t
) = index
;
4059 TEMPLATE_PARM_LEVEL (t
) = level
;
4060 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
4061 TEMPLATE_PARM_DECL (t
) = decl
;
4062 TREE_TYPE (t
) = type
;
4063 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
4064 TREE_READONLY (t
) = TREE_READONLY (decl
);
4069 /* Find the canonical type parameter for the given template type
4070 parameter. Returns the canonical type parameter, which may be TYPE
4071 if no such parameter existed. */
4074 canonical_type_parameter (tree type
)
4077 int idx
= TEMPLATE_TYPE_IDX (type
);
4078 if (!canonical_template_parms
)
4079 vec_alloc (canonical_template_parms
, idx
+ 1);
4081 if (canonical_template_parms
->length () <= (unsigned) idx
)
4082 vec_safe_grow_cleared (canonical_template_parms
, idx
+ 1);
4084 list
= (*canonical_template_parms
)[idx
];
4085 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
4086 list
= TREE_CHAIN (list
);
4089 return TREE_VALUE (list
);
4092 (*canonical_template_parms
)[idx
]
4093 = tree_cons (NULL_TREE
, type
, (*canonical_template_parms
)[idx
]);
4098 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4099 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4100 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4101 new one is created. */
4104 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4105 tsubst_flags_t complain
)
4107 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4108 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4109 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4110 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4112 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4115 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4116 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4117 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4118 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4119 DECL_ARTIFICIAL (decl
) = 1;
4120 SET_DECL_TEMPLATE_PARM_P (decl
);
4122 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4123 TEMPLATE_PARM_LEVEL (index
) - levels
,
4124 TEMPLATE_PARM_ORIG_LEVEL (index
),
4126 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4127 TEMPLATE_PARM_PARAMETER_PACK (t
)
4128 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4130 /* Template template parameters need this. */
4131 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4133 DECL_TEMPLATE_RESULT (decl
)
4134 = build_decl (DECL_SOURCE_LOCATION (decl
),
4135 TYPE_DECL
, DECL_NAME (decl
), type
);
4136 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4137 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4138 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4142 return TEMPLATE_PARM_DESCENDANTS (index
);
4145 /* Process information from new template parameter PARM and append it
4146 to the LIST being built. This new parameter is a non-type
4147 parameter iff IS_NON_TYPE is true. This new parameter is a
4148 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4152 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4153 bool is_non_type
, bool is_parameter_pack
)
4158 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4159 tree defval
= TREE_PURPOSE (parm
);
4160 tree constr
= TREE_TYPE (parm
);
4164 tree p
= tree_last (list
);
4166 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4169 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4170 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4172 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4180 parm
= TREE_VALUE (parm
);
4182 SET_DECL_TEMPLATE_PARM_P (parm
);
4184 if (TREE_TYPE (parm
) != error_mark_node
)
4188 The top-level cv-qualifiers on the template-parameter are
4189 ignored when determining its type. */
4190 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4191 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4192 TREE_TYPE (parm
) = error_mark_node
;
4193 else if (uses_parameter_packs (TREE_TYPE (parm
))
4194 && !is_parameter_pack
4195 /* If we're in a nested template parameter list, the template
4196 template parameter could be a parameter pack. */
4197 && processing_template_parmlist
== 1)
4199 /* This template parameter is not a parameter pack, but it
4200 should be. Complain about "bare" parameter packs. */
4201 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4203 /* Recover by calling this a parameter pack. */
4204 is_parameter_pack
= true;
4208 /* A template parameter is not modifiable. */
4209 TREE_CONSTANT (parm
) = 1;
4210 TREE_READONLY (parm
) = 1;
4211 decl
= build_decl (parm_loc
,
4212 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4213 TREE_CONSTANT (decl
) = 1;
4214 TREE_READONLY (decl
) = 1;
4215 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4216 = build_template_parm_index (idx
, processing_template_decl
,
4217 processing_template_decl
,
4218 decl
, TREE_TYPE (parm
));
4220 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4221 = is_parameter_pack
;
4226 parm
= TREE_VALUE (TREE_VALUE (parm
));
4228 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4230 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4231 /* This is for distinguishing between real templates and template
4232 template parameters */
4233 TREE_TYPE (parm
) = t
;
4234 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4239 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4240 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4241 decl
= build_decl (parm_loc
,
4242 TYPE_DECL
, parm
, t
);
4245 TYPE_NAME (t
) = decl
;
4246 TYPE_STUB_DECL (t
) = decl
;
4248 TEMPLATE_TYPE_PARM_INDEX (t
)
4249 = build_template_parm_index (idx
, processing_template_decl
,
4250 processing_template_decl
,
4251 decl
, TREE_TYPE (parm
));
4252 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4253 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4255 DECL_ARTIFICIAL (decl
) = 1;
4256 SET_DECL_TEMPLATE_PARM_P (decl
);
4258 /* Build requirements for the type/template parameter.
4259 This must be done after SET_DECL_TEMPLATE_PARM_P or
4260 process_template_parm could fail. */
4261 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4265 /* Build the parameter node linking the parameter declaration,
4266 its default argument (if any), and its constraints (if any). */
4267 parm
= build_tree_list (defval
, parm
);
4268 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4270 return chainon (list
, parm
);
4273 /* The end of a template parameter list has been reached. Process the
4274 tree list into a parameter vector, converting each parameter into a more
4275 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4279 end_template_parm_list (tree parms
)
4283 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4285 /* Pop the dummy parameter level and add the real one. */
4286 current_template_parms
= TREE_CHAIN (current_template_parms
);
4288 current_template_parms
4289 = tree_cons (size_int (processing_template_decl
),
4290 saved_parmlist
, current_template_parms
);
4292 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4294 next
= TREE_CHAIN (parm
);
4295 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4296 TREE_CHAIN (parm
) = NULL_TREE
;
4299 --processing_template_parmlist
;
4301 return saved_parmlist
;
4304 // Explicitly indicate the end of the template parameter list. We assume
4305 // that the current template parameters have been constructed and/or
4306 // managed explicitly, as when creating new template template parameters
4307 // from a shorthand constraint.
4309 end_template_parm_list ()
4311 --processing_template_parmlist
;
4314 /* end_template_decl is called after a template declaration is seen. */
4317 end_template_decl (void)
4319 reset_specialization ();
4321 if (! processing_template_decl
)
4324 /* This matches the pushlevel in begin_template_parm_list. */
4327 --processing_template_decl
;
4328 current_template_parms
= TREE_CHAIN (current_template_parms
);
4331 /* Takes a TREE_LIST representing a template parameter and convert it
4332 into an argument suitable to be passed to the type substitution
4333 functions. Note that If the TREE_LIST contains an error_mark
4334 node, the returned argument is error_mark_node. */
4337 template_parm_to_arg (tree t
)
4341 || TREE_CODE (t
) != TREE_LIST
)
4344 if (error_operand_p (TREE_VALUE (t
)))
4345 return error_mark_node
;
4349 if (TREE_CODE (t
) == TYPE_DECL
4350 || TREE_CODE (t
) == TEMPLATE_DECL
)
4354 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4356 /* Turn this argument into a TYPE_ARGUMENT_PACK
4357 with a single element, which expands T. */
4358 tree vec
= make_tree_vec (1);
4360 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4362 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4364 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4365 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4370 t
= DECL_INITIAL (t
);
4372 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4374 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4375 with a single element, which expands T. */
4376 tree vec
= make_tree_vec (1);
4378 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4380 t
= convert_from_reference (t
);
4381 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4383 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4384 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4387 t
= convert_from_reference (t
);
4392 /* Given a single level of template parameters (a TREE_VEC), return it
4393 as a set of template arguments. */
4396 template_parms_level_to_args (tree parms
)
4398 tree a
= copy_node (parms
);
4399 TREE_TYPE (a
) = NULL_TREE
;
4400 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4401 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4404 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4409 /* Given a set of template parameters, return them as a set of template
4410 arguments. The template parameters are represented as a TREE_VEC, in
4411 the form documented in cp-tree.h for template arguments. */
4414 template_parms_to_args (tree parms
)
4417 tree args
= NULL_TREE
;
4418 int length
= TMPL_PARMS_DEPTH (parms
);
4421 /* If there is only one level of template parameters, we do not
4422 create a TREE_VEC of TREE_VECs. Instead, we return a single
4423 TREE_VEC containing the arguments. */
4425 args
= make_tree_vec (length
);
4427 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4429 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4432 TREE_VEC_ELT (args
, --l
) = a
;
4440 /* Within the declaration of a template, return the currently active
4441 template parameters as an argument TREE_VEC. */
4444 current_template_args (void)
4446 return template_parms_to_args (current_template_parms
);
4449 /* Update the declared TYPE by doing any lookups which were thought to be
4450 dependent, but are not now that we know the SCOPE of the declarator. */
4453 maybe_update_decl_type (tree orig_type
, tree scope
)
4455 tree type
= orig_type
;
4457 if (type
== NULL_TREE
)
4460 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4461 type
= TREE_TYPE (type
);
4463 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4464 && dependent_type_p (type
)
4465 /* Don't bother building up the args in this case. */
4466 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4468 /* tsubst in the args corresponding to the template parameters,
4469 including auto if present. Most things will be unchanged, but
4470 make_typename_type and tsubst_qualified_id will resolve
4471 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4472 tree args
= current_template_args ();
4473 tree auto_node
= type_uses_auto (type
);
4477 tree auto_vec
= make_tree_vec (1);
4478 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4479 args
= add_to_template_args (args
, auto_vec
);
4481 pushed
= push_scope (scope
);
4482 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4487 if (type
== error_mark_node
)
4490 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4492 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4495 type
= TYPE_NAME (type
);
4500 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4501 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4502 the new template is a member template. */
4505 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4507 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4508 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4509 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4510 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4511 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4516 struct template_parm_data
4518 /* The level of the template parameters we are currently
4522 /* The index of the specialization argument we are currently
4526 /* An array whose size is the number of template parameters. The
4527 elements are nonzero if the parameter has been used in any one
4528 of the arguments processed so far. */
4531 /* An array whose size is the number of template arguments. The
4532 elements are nonzero if the argument makes use of template
4533 parameters of this level. */
4534 int* arg_uses_template_parms
;
4537 /* Subroutine of push_template_decl used to see if each template
4538 parameter in a partial specialization is used in the explicit
4539 argument list. If T is of the LEVEL given in DATA (which is
4540 treated as a template_parm_data*), then DATA->PARMS is marked
4544 mark_template_parm (tree t
, void* data
)
4548 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4550 template_parm_level_and_index (t
, &level
, &idx
);
4552 if (level
== tpd
->level
)
4554 tpd
->parms
[idx
] = 1;
4555 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4558 /* In C++17 the type of a non-type argument is a deduced context. */
4559 if (cxx_dialect
>= cxx1z
4560 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4561 for_each_template_parm (TREE_TYPE (t
),
4562 &mark_template_parm
,
4565 /*include_nondeduced_p=*/false);
4567 /* Return zero so that for_each_template_parm will continue the
4568 traversal of the tree; we want to mark *every* template parm. */
4572 /* Process the partial specialization DECL. */
4575 process_partial_specialization (tree decl
)
4577 tree type
= TREE_TYPE (decl
);
4578 tree tinfo
= get_template_info (decl
);
4579 tree maintmpl
= TI_TEMPLATE (tinfo
);
4580 tree specargs
= TI_ARGS (tinfo
);
4581 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4582 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4585 int nargs
= TREE_VEC_LENGTH (inner_args
);
4588 bool did_error_intro
= false;
4589 struct template_parm_data tpd
;
4590 struct template_parm_data tpd2
;
4592 gcc_assert (current_template_parms
);
4594 /* A concept cannot be specialized. */
4595 if (flag_concepts
&& variable_concept_p (maintmpl
))
4597 error ("specialization of variable concept %q#D", maintmpl
);
4598 return error_mark_node
;
4601 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4602 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4604 /* We check that each of the template parameters given in the
4605 partial specialization is used in the argument list to the
4606 specialization. For example:
4608 template <class T> struct S;
4609 template <class T> struct S<T*>;
4611 The second declaration is OK because `T*' uses the template
4612 parameter T, whereas
4614 template <class T> struct S<int>;
4616 is no good. Even trickier is:
4627 The S2<T> declaration is actually invalid; it is a
4628 full-specialization. Of course,
4631 struct S2<T (*)(U)>;
4633 or some such would have been OK. */
4634 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4635 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4636 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4638 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4639 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4640 for (i
= 0; i
< nargs
; ++i
)
4642 tpd
.current_arg
= i
;
4643 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4644 &mark_template_parm
,
4647 /*include_nondeduced_p=*/false);
4649 for (i
= 0; i
< ntparms
; ++i
)
4650 if (tpd
.parms
[i
] == 0)
4652 /* One of the template parms was not used in a deduced context in the
4654 if (!did_error_intro
)
4656 error ("template parameters not deducible in "
4657 "partial specialization:");
4658 did_error_intro
= true;
4661 inform (input_location
, " %qD",
4662 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4665 if (did_error_intro
)
4666 return error_mark_node
;
4668 /* [temp.class.spec]
4670 The argument list of the specialization shall not be identical to
4671 the implicit argument list of the primary template. */
4673 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4674 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4676 || !strictly_subsumes (current_template_constraints (),
4677 get_constraints (maintmpl
))))
4680 error ("partial specialization %q+D does not specialize "
4681 "any template arguments", decl
);
4683 error ("partial specialization %q+D does not specialize any "
4684 "template arguments and is not more constrained than", decl
);
4685 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4688 /* A partial specialization that replaces multiple parameters of the
4689 primary template with a pack expansion is less specialized for those
4691 if (nargs
< DECL_NTPARMS (maintmpl
))
4693 error ("partial specialization is not more specialized than the "
4694 "primary template because it replaces multiple parameters "
4695 "with a pack expansion");
4696 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4697 /* Avoid crash in process_partial_specialization. */
4701 /* If we aren't in a dependent class, we can actually try deduction. */
4702 else if (tpd
.level
== 1
4703 /* FIXME we should be able to handle a partial specialization of a
4704 partial instantiation, but currently we can't (c++/41727). */
4705 && TMPL_ARGS_DEPTH (specargs
) == 1
4706 && !get_partial_spec_bindings (maintmpl
, maintmpl
, specargs
))
4708 if (permerror (input_location
, "partial specialization %qD is not "
4709 "more specialized than", decl
))
4710 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template %qD",
4714 /* [temp.class.spec]
4716 A partially specialized non-type argument expression shall not
4717 involve template parameters of the partial specialization except
4718 when the argument expression is a simple identifier.
4720 The type of a template parameter corresponding to a specialized
4721 non-type argument shall not be dependent on a parameter of the
4724 Also, we verify that pack expansions only occur at the
4725 end of the argument list. */
4726 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4728 for (i
= 0; i
< nargs
; ++i
)
4730 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4731 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4732 tree packed_args
= NULL_TREE
;
4735 if (ARGUMENT_PACK_P (arg
))
4737 /* Extract the arguments from the argument pack. We'll be
4738 iterating over these in the following loop. */
4739 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4740 len
= TREE_VEC_LENGTH (packed_args
);
4743 for (j
= 0; j
< len
; j
++)
4746 /* Get the Jth argument in the parameter pack. */
4747 arg
= TREE_VEC_ELT (packed_args
, j
);
4749 if (PACK_EXPANSION_P (arg
))
4751 /* Pack expansions must come at the end of the
4753 if ((packed_args
&& j
< len
- 1)
4754 || (!packed_args
&& i
< nargs
- 1))
4756 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4757 error ("parameter pack argument %qE must be at the "
4758 "end of the template argument list", arg
);
4760 error ("parameter pack argument %qT must be at the "
4761 "end of the template argument list", arg
);
4765 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4766 /* We only care about the pattern. */
4767 arg
= PACK_EXPANSION_PATTERN (arg
);
4769 if (/* These first two lines are the `non-type' bit. */
4771 && TREE_CODE (arg
) != TEMPLATE_DECL
4772 /* This next two lines are the `argument expression is not just a
4773 simple identifier' condition and also the `specialized
4774 non-type argument' bit. */
4775 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4776 && !(REFERENCE_REF_P (arg
)
4777 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4779 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4780 || (packed_args
&& uses_template_parms (arg
)))
4781 error ("template argument %qE involves template parameter(s)",
4785 /* Look at the corresponding template parameter,
4786 marking which template parameters its type depends
4788 tree type
= TREE_TYPE (parm
);
4792 /* We haven't yet initialized TPD2. Do so now. */
4793 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4794 /* The number of parameters here is the number in the
4795 main template, which, as checked in the assertion
4797 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4799 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4802 /* Mark the template parameters. But this time, we're
4803 looking for the template parameters of the main
4804 template, not in the specialization. */
4805 tpd2
.current_arg
= i
;
4806 tpd2
.arg_uses_template_parms
[i
] = 0;
4807 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4808 for_each_template_parm (type
,
4809 &mark_template_parm
,
4812 /*include_nondeduced_p=*/false);
4814 if (tpd2
.arg_uses_template_parms
[i
])
4816 /* The type depended on some template parameters.
4817 If they are fully specialized in the
4818 specialization, that's OK. */
4821 for (j
= 0; j
< nargs
; ++j
)
4822 if (tpd2
.parms
[j
] != 0
4823 && tpd
.arg_uses_template_parms
[j
])
4826 error_n (input_location
, count
,
4827 "type %qT of template argument %qE depends "
4828 "on a template parameter",
4829 "type %qT of template argument %qE depends "
4830 "on template parameters",
4839 /* We should only get here once. */
4840 if (TREE_CODE (decl
) == TYPE_DECL
)
4841 gcc_assert (!COMPLETE_TYPE_P (type
));
4843 // Build the template decl.
4844 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4845 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4846 TREE_TYPE (tmpl
) = type
;
4847 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4848 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4849 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4850 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4852 /* Give template template parms a DECL_CONTEXT of the template
4853 for which they are a parameter. */
4854 for (i
= 0; i
< ntparms
; ++i
)
4856 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
4857 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4858 DECL_CONTEXT (parm
) = tmpl
;
4862 /* We didn't register this in check_explicit_specialization so we could
4863 wait until the constraints were set. */
4864 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4866 associate_classtype_constraints (type
);
4868 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4869 = tree_cons (specargs
, tmpl
,
4870 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4871 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4873 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4874 inst
= TREE_CHAIN (inst
))
4876 tree instance
= TREE_VALUE (inst
);
4877 if (TYPE_P (instance
)
4878 ? (COMPLETE_TYPE_P (instance
)
4879 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4880 : DECL_TEMPLATE_INSTANTIATION (instance
))
4882 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4883 tree inst_decl
= (DECL_P (instance
)
4884 ? instance
: TYPE_NAME (instance
));
4887 else if (spec
== error_mark_node
)
4888 permerror (input_location
,
4889 "declaration of %qD ambiguates earlier template "
4890 "instantiation for %qD", decl
, inst_decl
);
4891 else if (TREE_VALUE (spec
) == tmpl
)
4892 permerror (input_location
,
4893 "partial specialization of %qD after instantiation "
4894 "of %qD", decl
, inst_decl
);
4901 /* PARM is a template parameter of some form; return the corresponding
4902 TEMPLATE_PARM_INDEX. */
4905 get_template_parm_index (tree parm
)
4907 if (TREE_CODE (parm
) == PARM_DECL
4908 || TREE_CODE (parm
) == CONST_DECL
)
4909 parm
= DECL_INITIAL (parm
);
4910 else if (TREE_CODE (parm
) == TYPE_DECL
4911 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4912 parm
= TREE_TYPE (parm
);
4913 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4914 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4915 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4916 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4917 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4921 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4922 parameter packs used by the template parameter PARM. */
4925 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4927 /* A type parm can't refer to another parm. */
4928 if (TREE_CODE (parm
) == TYPE_DECL
)
4930 else if (TREE_CODE (parm
) == PARM_DECL
)
4932 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4937 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4939 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4940 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4941 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4944 /* PARM is a template parameter pack. Return any parameter packs used in
4945 its type or the type of any of its template parameters. If there are
4946 any such packs, it will be instantiated into a fixed template parameter
4947 list by partial instantiation rather than be fully deduced. */
4950 fixed_parameter_pack_p (tree parm
)
4952 /* This can only be true in a member template. */
4953 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4955 /* This can only be true for a parameter pack. */
4956 if (!template_parameter_pack_p (parm
))
4958 /* A type parm can't refer to another parm. */
4959 if (TREE_CODE (parm
) == TYPE_DECL
)
4962 tree parameter_packs
= NULL_TREE
;
4963 struct find_parameter_pack_data ppd
;
4964 ppd
.parameter_packs
= ¶meter_packs
;
4965 ppd
.visited
= new hash_set
<tree
>;
4966 ppd
.type_pack_expansion_p
= false;
4968 fixed_parameter_pack_p_1 (parm
, &ppd
);
4971 return parameter_packs
;
4974 /* Check that a template declaration's use of default arguments and
4975 parameter packs is not invalid. Here, PARMS are the template
4976 parameters. IS_PRIMARY is true if DECL is the thing declared by
4977 a primary template. IS_PARTIAL is true if DECL is a partial
4980 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4981 declaration (but not a definition); 1 indicates a declaration, 2
4982 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4983 emitted for extraneous default arguments.
4985 Returns TRUE if there were no errors found, FALSE otherwise. */
4988 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4989 bool is_partial
, int is_friend_decl
)
4992 int last_level_to_check
;
4994 bool no_errors
= true;
4998 A default template-argument shall not be specified in a
4999 function template declaration or a function template definition, nor
5000 in the template-parameter-list of the definition of a member of a
5003 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
5004 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
5005 /* You can't have a function template declaration in a local
5006 scope, nor you can you define a member of a class template in a
5010 if ((TREE_CODE (decl
) == TYPE_DECL
5012 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5013 || (TREE_CODE (decl
) == FUNCTION_DECL
5014 && LAMBDA_FUNCTION_P (decl
)))
5015 /* A lambda doesn't have an explicit declaration; don't complain
5016 about the parms of the enclosing class. */
5019 if (current_class_type
5020 && !TYPE_BEING_DEFINED (current_class_type
)
5021 && DECL_LANG_SPECIFIC (decl
)
5022 && DECL_DECLARES_FUNCTION_P (decl
)
5023 /* If this is either a friend defined in the scope of the class
5024 or a member function. */
5025 && (DECL_FUNCTION_MEMBER_P (decl
)
5026 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
5027 : DECL_FRIEND_CONTEXT (decl
)
5028 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
5030 /* And, if it was a member function, it really was defined in
5031 the scope of the class. */
5032 && (!DECL_FUNCTION_MEMBER_P (decl
)
5033 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
5034 /* We already checked these parameters when the template was
5035 declared, so there's no need to do it again now. This function
5036 was defined in class scope, but we're processing its body now
5037 that the class is complete. */
5040 /* Core issue 226 (C++0x only): the following only applies to class
5043 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
5047 If a template-parameter has a default template-argument, all
5048 subsequent template-parameters shall have a default
5049 template-argument supplied. */
5050 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
5052 tree inner_parms
= TREE_VALUE (parm_level
);
5053 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
5054 int seen_def_arg_p
= 0;
5057 for (i
= 0; i
< ntparms
; ++i
)
5059 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
5061 if (parm
== error_mark_node
)
5064 if (TREE_PURPOSE (parm
))
5066 else if (seen_def_arg_p
5067 && !template_parameter_pack_p (TREE_VALUE (parm
)))
5069 error ("no default argument for %qD", TREE_VALUE (parm
));
5070 /* For better subsequent error-recovery, we indicate that
5071 there should have been a default argument. */
5072 TREE_PURPOSE (parm
) = error_mark_node
;
5075 else if (!is_partial
5077 /* Don't complain about an enclosing partial
5079 && parm_level
== parms
5080 && TREE_CODE (decl
) == TYPE_DECL
5082 && template_parameter_pack_p (TREE_VALUE (parm
))
5083 /* A fixed parameter pack will be partially
5084 instantiated into a fixed length list. */
5085 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
5087 /* A primary class template can only have one
5088 parameter pack, at the end of the template
5091 error ("parameter pack %q+D must be at the end of the"
5092 " template parameter list", TREE_VALUE (parm
));
5094 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
5102 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
5106 /* For an ordinary class template, default template arguments are
5107 allowed at the innermost level, e.g.:
5108 template <class T = int>
5110 but, in a partial specialization, they're not allowed even
5111 there, as we have in [temp.class.spec]:
5113 The template parameter list of a specialization shall not
5114 contain default template argument values.
5116 So, for a partial specialization, or for a function template
5117 (in C++98/C++03), we look at all of them. */
5120 /* But, for a primary class template that is not a partial
5121 specialization we look at all template parameters except the
5123 parms
= TREE_CHAIN (parms
);
5125 /* Figure out what error message to issue. */
5126 if (is_friend_decl
== 2)
5127 msg
= G_("default template arguments may not be used in function template "
5128 "friend re-declaration");
5129 else if (is_friend_decl
)
5130 msg
= G_("default template arguments may not be used in function template "
5131 "friend declarations");
5132 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5133 msg
= G_("default template arguments may not be used in function templates "
5134 "without -std=c++11 or -std=gnu++11");
5135 else if (is_partial
)
5136 msg
= G_("default template arguments may not be used in "
5137 "partial specializations");
5138 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5139 msg
= G_("default argument for template parameter for class enclosing %qD");
5141 /* Per [temp.param]/9, "A default template-argument shall not be
5142 specified in the template-parameter-lists of the definition of
5143 a member of a class template that appears outside of the member's
5144 class.", thus if we aren't handling a member of a class template
5145 there is no need to examine the parameters. */
5148 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5149 /* If we're inside a class definition, there's no need to
5150 examine the parameters to the class itself. On the one
5151 hand, they will be checked when the class is defined, and,
5152 on the other, default arguments are valid in things like:
5153 template <class T = double>
5154 struct S { template <class U> void f(U); };
5155 Here the default argument for `S' has no bearing on the
5156 declaration of `f'. */
5157 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5159 /* Check everything. */
5160 last_level_to_check
= 0;
5162 for (parm_level
= parms
;
5163 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5164 parm_level
= TREE_CHAIN (parm_level
))
5166 tree inner_parms
= TREE_VALUE (parm_level
);
5170 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5171 for (i
= 0; i
< ntparms
; ++i
)
5173 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5176 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5181 if (is_friend_decl
== 2)
5188 /* Clear out the default argument so that we are not
5190 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5194 /* At this point, if we're still interested in issuing messages,
5195 they must apply to classes surrounding the object declared. */
5197 msg
= G_("default argument for template parameter for class "
5204 /* Worker for push_template_decl_real, called via
5205 for_each_template_parm. DATA is really an int, indicating the
5206 level of the parameters we are interested in. If T is a template
5207 parameter of that level, return nonzero. */
5210 template_parm_this_level_p (tree t
, void* data
)
5212 int this_level
= *(int *)data
;
5215 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5216 level
= TEMPLATE_PARM_LEVEL (t
);
5218 level
= TEMPLATE_TYPE_LEVEL (t
);
5219 return level
== this_level
;
5222 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5223 DATA is really an int, indicating the innermost outer level of parameters.
5224 If T is a template parameter of that level or further out, return
5228 template_parm_outer_level (tree t
, void *data
)
5230 int this_level
= *(int *)data
;
5233 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5234 level
= TEMPLATE_PARM_LEVEL (t
);
5236 level
= TEMPLATE_TYPE_LEVEL (t
);
5237 return level
<= this_level
;
5240 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5241 parameters given by current_template_args, or reuses a
5242 previously existing one, if appropriate. Returns the DECL, or an
5243 equivalent one, if it is replaced via a call to duplicate_decls.
5245 If IS_FRIEND is true, DECL is a friend declaration. */
5248 push_template_decl_real (tree decl
, bool is_friend
)
5256 int new_template_p
= 0;
5257 /* True if the template is a member template, in the sense of
5259 bool member_template_p
= false;
5261 if (decl
== error_mark_node
|| !current_template_parms
)
5262 return error_mark_node
;
5264 /* See if this is a partial specialization. */
5265 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5266 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5267 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5269 && DECL_LANG_SPECIFIC (decl
)
5270 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5271 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5273 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5277 /* For a friend, we want the context of the friend function, not
5278 the type of which it is a friend. */
5279 ctx
= CP_DECL_CONTEXT (decl
);
5280 else if (CP_DECL_CONTEXT (decl
)
5281 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5282 /* In the case of a virtual function, we want the class in which
5284 ctx
= CP_DECL_CONTEXT (decl
);
5286 /* Otherwise, if we're currently defining some class, the DECL
5287 is assumed to be a member of the class. */
5288 ctx
= current_scope ();
5290 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5293 if (!DECL_CONTEXT (decl
))
5294 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5296 /* See if this is a primary template. */
5297 if (is_friend
&& ctx
5298 && uses_template_parms_level (ctx
, processing_template_decl
))
5299 /* A friend template that specifies a class context, i.e.
5300 template <typename T> friend void A<T>::f();
5303 else if (TREE_CODE (decl
) == TYPE_DECL
5304 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5307 is_primary
= template_parm_scope_p ();
5311 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5313 if (DECL_CLASS_SCOPE_P (decl
))
5314 member_template_p
= true;
5315 if (TREE_CODE (decl
) == TYPE_DECL
5316 && anon_aggrname_p (DECL_NAME (decl
)))
5318 error ("template class without a name");
5319 return error_mark_node
;
5321 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5323 if (member_template_p
)
5325 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5326 error ("member template %qD may not have virt-specifiers", decl
);
5328 if (DECL_DESTRUCTOR_P (decl
))
5332 A destructor shall not be a member template. */
5333 error ("destructor %qD declared as member template", decl
);
5334 return error_mark_node
;
5336 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
5337 && (!prototype_p (TREE_TYPE (decl
))
5338 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5339 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5340 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
5341 == void_list_node
)))
5343 /* [basic.stc.dynamic.allocation]
5345 An allocation function can be a function
5346 template. ... Template allocation functions shall
5347 have two or more parameters. */
5348 error ("invalid template declaration of %qD", decl
);
5349 return error_mark_node
;
5352 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5353 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5355 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5356 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
5357 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5359 tree t
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5360 if (TREE_CODE (t
) == TYPE_DECL
)
5362 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5363 TEMPLATE_TYPE_PARM_FOR_CLASS (t
) = true;
5366 else if (TREE_CODE (decl
) == TYPE_DECL
5367 && TYPE_DECL_ALIAS_P (decl
))
5368 /* alias-declaration */
5369 gcc_assert (!DECL_ARTIFICIAL (decl
));
5370 else if (VAR_P (decl
))
5371 /* C++14 variable template. */;
5374 error ("template declaration of %q#D", decl
);
5375 return error_mark_node
;
5379 /* Check to see that the rules regarding the use of default
5380 arguments are not being violated. */
5381 check_default_tmpl_args (decl
, current_template_parms
,
5382 is_primary
, is_partial
, /*is_friend_decl=*/0);
5384 /* Ensure that there are no parameter packs in the type of this
5385 declaration that have not been expanded. */
5386 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5388 /* Check each of the arguments individually to see if there are
5389 any bare parameter packs. */
5390 tree type
= TREE_TYPE (decl
);
5391 tree arg
= DECL_ARGUMENTS (decl
);
5392 tree argtype
= TYPE_ARG_TYPES (type
);
5394 while (arg
&& argtype
)
5396 if (!DECL_PACK_P (arg
)
5397 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5399 /* This is a PARM_DECL that contains unexpanded parameter
5400 packs. We have already complained about this in the
5401 check_for_bare_parameter_packs call, so just replace
5402 these types with ERROR_MARK_NODE. */
5403 TREE_TYPE (arg
) = error_mark_node
;
5404 TREE_VALUE (argtype
) = error_mark_node
;
5407 arg
= DECL_CHAIN (arg
);
5408 argtype
= TREE_CHAIN (argtype
);
5411 /* Check for bare parameter packs in the return type and the
5412 exception specifiers. */
5413 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5414 /* Errors were already issued, set return type to int
5415 as the frontend doesn't expect error_mark_node as
5417 TREE_TYPE (type
) = integer_type_node
;
5418 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5419 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5421 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5422 && TYPE_DECL_ALIAS_P (decl
))
5423 ? DECL_ORIGINAL_TYPE (decl
)
5424 : TREE_TYPE (decl
)))
5426 TREE_TYPE (decl
) = error_mark_node
;
5427 return error_mark_node
;
5431 return process_partial_specialization (decl
);
5433 args
= current_template_args ();
5436 || TREE_CODE (ctx
) == FUNCTION_DECL
5437 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5438 || (TREE_CODE (decl
) == TYPE_DECL
5439 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5440 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5442 if (DECL_LANG_SPECIFIC (decl
)
5443 && DECL_TEMPLATE_INFO (decl
)
5444 && DECL_TI_TEMPLATE (decl
))
5445 tmpl
= DECL_TI_TEMPLATE (decl
);
5446 /* If DECL is a TYPE_DECL for a class-template, then there won't
5447 be DECL_LANG_SPECIFIC. The information equivalent to
5448 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5449 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5450 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5451 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5453 /* Since a template declaration already existed for this
5454 class-type, we must be redeclaring it here. Make sure
5455 that the redeclaration is valid. */
5456 redeclare_class_template (TREE_TYPE (decl
),
5457 current_template_parms
,
5458 current_template_constraints ());
5459 /* We don't need to create a new TEMPLATE_DECL; just use the
5460 one we already had. */
5461 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5465 tmpl
= build_template_decl (decl
, current_template_parms
,
5469 if (DECL_LANG_SPECIFIC (decl
)
5470 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5472 /* A specialization of a member template of a template
5474 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5475 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5476 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5482 tree a
, t
, current
, parms
;
5484 tree tinfo
= get_template_info (decl
);
5488 error ("template definition of non-template %q#D", decl
);
5489 return error_mark_node
;
5492 tmpl
= TI_TEMPLATE (tinfo
);
5494 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5495 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5496 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5497 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5501 /* The declaration is a specialization of a member
5502 template, declared outside the class. Therefore, the
5503 innermost template arguments will be NULL, so we
5504 replace them with the arguments determined by the
5505 earlier call to check_explicit_specialization. */
5506 args
= DECL_TI_ARGS (decl
);
5509 = build_template_decl (decl
, current_template_parms
,
5511 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5512 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5513 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5514 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5515 DECL_TEMPLATE_INFO (new_tmpl
)
5516 = build_template_info (tmpl
, args
);
5518 register_specialization (new_tmpl
,
5519 most_general_template (tmpl
),
5525 /* Make sure the template headers we got make sense. */
5527 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5528 i
= TMPL_PARMS_DEPTH (parms
);
5529 if (TMPL_ARGS_DEPTH (args
) != i
)
5531 error ("expected %d levels of template parms for %q#D, got %d",
5532 i
, decl
, TMPL_ARGS_DEPTH (args
));
5533 DECL_INTERFACE_KNOWN (decl
) = 1;
5534 return error_mark_node
;
5537 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5539 a
= TMPL_ARGS_LEVEL (args
, i
);
5540 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5542 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5544 if (current
== decl
)
5545 error ("got %d template parameters for %q#D",
5546 TREE_VEC_LENGTH (a
), decl
);
5548 error ("got %d template parameters for %q#T",
5549 TREE_VEC_LENGTH (a
), current
);
5550 error (" but %d required", TREE_VEC_LENGTH (t
));
5551 /* Avoid crash in import_export_decl. */
5552 DECL_INTERFACE_KNOWN (decl
) = 1;
5553 return error_mark_node
;
5556 if (current
== decl
)
5558 else if (current
== NULL_TREE
)
5559 /* Can happen in erroneous input. */
5562 current
= get_containing_scope (current
);
5565 /* Check that the parms are used in the appropriate qualifying scopes
5566 in the declarator. */
5567 if (!comp_template_args
5569 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5572 template arguments to %qD do not match original template %qD",
5573 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5574 if (!uses_template_parms (TI_ARGS (tinfo
)))
5575 inform (input_location
, "use template<> for an explicit specialization");
5576 /* Avoid crash in import_export_decl. */
5577 DECL_INTERFACE_KNOWN (decl
) = 1;
5578 return error_mark_node
;
5582 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5583 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5585 /* Push template declarations for global functions and types. Note
5586 that we do not try to push a global template friend declared in a
5587 template class; such a thing may well depend on the template
5588 parameters of the class. */
5589 if (new_template_p
&& !ctx
5590 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5592 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5593 if (tmpl
== error_mark_node
)
5594 return error_mark_node
;
5596 /* Hide template friend classes that haven't been declared yet. */
5597 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5599 DECL_ANTICIPATED (tmpl
) = 1;
5600 DECL_FRIEND_P (tmpl
) = 1;
5606 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5609 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5610 if (DECL_CONV_FN_P (tmpl
))
5612 int depth
= TMPL_PARMS_DEPTH (parms
);
5614 /* It is a conversion operator. See if the type converted to
5615 depends on innermost template operands. */
5617 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5619 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5622 /* Give template template parms a DECL_CONTEXT of the template
5623 for which they are a parameter. */
5624 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5625 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5627 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5628 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5629 DECL_CONTEXT (parm
) = tmpl
;
5632 if (TREE_CODE (decl
) == TYPE_DECL
5633 && TYPE_DECL_ALIAS_P (decl
)
5634 && complex_alias_template_p (tmpl
))
5635 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5638 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5639 back to its most general template. If TMPL is a specialization,
5640 ARGS may only have the innermost set of arguments. Add the missing
5641 argument levels if necessary. */
5642 if (DECL_TEMPLATE_INFO (tmpl
))
5643 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5645 info
= build_template_info (tmpl
, args
);
5647 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5648 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5652 retrofit_lang_decl (decl
);
5653 if (DECL_LANG_SPECIFIC (decl
))
5654 DECL_TEMPLATE_INFO (decl
) = info
;
5657 if (flag_implicit_templates
5659 && TREE_PUBLIC (decl
)
5660 && VAR_OR_FUNCTION_DECL_P (decl
))
5661 /* Set DECL_COMDAT on template instantiations; if we force
5662 them to be emitted by explicit instantiation or -frepo,
5663 mark_needed will tell cgraph to do the right thing. */
5664 DECL_COMDAT (decl
) = true;
5666 return DECL_TEMPLATE_RESULT (tmpl
);
5670 push_template_decl (tree decl
)
5672 return push_template_decl_real (decl
, false);
5675 /* FN is an inheriting constructor that inherits from the constructor
5676 template INHERITED; turn FN into a constructor template with a matching
5680 add_inherited_template_parms (tree fn
, tree inherited
)
5683 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5684 inner_parms
= copy_node (inner_parms
);
5686 = tree_cons (size_int (processing_template_decl
+ 1),
5687 inner_parms
, current_template_parms
);
5688 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5689 tree args
= template_parms_to_args (parms
);
5690 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5691 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5692 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5693 DECL_ARTIFICIAL (tmpl
) = true;
5694 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5698 /* Called when a class template TYPE is redeclared with the indicated
5699 template PARMS, e.g.:
5701 template <class T> struct S;
5702 template <class T> struct S {}; */
5705 redeclare_class_template (tree type
, tree parms
, tree cons
)
5711 if (!TYPE_TEMPLATE_INFO (type
))
5713 error ("%qT is not a template type", type
);
5717 tmpl
= TYPE_TI_TEMPLATE (type
);
5718 if (!PRIMARY_TEMPLATE_P (tmpl
))
5719 /* The type is nested in some template class. Nothing to worry
5720 about here; there are no new template parameters for the nested
5726 error ("template specifiers not specified in declaration of %qD",
5731 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5732 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5734 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5736 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5737 "redeclared with %d template parameter",
5738 "redeclared with %d template parameters",
5739 TREE_VEC_LENGTH (parms
));
5740 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5741 "previous declaration %qD used %d template parameter",
5742 "previous declaration %qD used %d template parameters",
5743 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5747 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5754 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5755 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5758 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5759 if (error_operand_p (tmpl_parm
))
5762 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5763 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5764 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5766 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5768 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5769 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5770 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5771 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5772 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5773 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5774 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5775 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5776 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5778 error ("template parameter %q+#D", tmpl_parm
);
5779 error ("redeclared here as %q#D", parm
);
5783 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5785 /* We have in [temp.param]:
5787 A template-parameter may not be given default arguments
5788 by two different declarations in the same scope. */
5789 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5790 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5791 "original definition appeared here");
5795 if (parm_default
!= NULL_TREE
)
5796 /* Update the previous template parameters (which are the ones
5797 that will really count) with the new default value. */
5798 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5799 else if (tmpl_default
!= NULL_TREE
)
5800 /* Update the new parameters, too; they'll be used as the
5801 parameters for any members. */
5802 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5804 /* Give each template template parm in this redeclaration a
5805 DECL_CONTEXT of the template for which they are a parameter. */
5806 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5808 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5809 DECL_CONTEXT (parm
) = tmpl
;
5812 if (TREE_CODE (parm
) == TYPE_DECL
)
5813 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm
)) = true;
5816 // Cannot redeclare a class template with a different set of constraints.
5817 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5819 error_at (input_location
, "redeclaration %q#D with different "
5820 "constraints", tmpl
);
5821 inform (DECL_SOURCE_LOCATION (tmpl
),
5822 "original declaration appeared here");
5828 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5829 to be used when the caller has already checked
5830 (processing_template_decl
5831 && !instantiation_dependent_expression_p (expr)
5832 && potential_constant_expression (expr))
5833 and cleared processing_template_decl. */
5836 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5838 return tsubst_copy_and_build (expr
,
5841 /*in_decl=*/NULL_TREE
,
5842 /*function_p=*/false,
5843 /*integral_constant_expression_p=*/true);
5846 /* Simplify EXPR if it is a non-dependent expression. Returns the
5847 (possibly simplified) expression. */
5850 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5852 if (expr
== NULL_TREE
)
5855 /* If we're in a template, but EXPR isn't value dependent, simplify
5856 it. We're supposed to treat:
5858 template <typename T> void f(T[1 + 1]);
5859 template <typename T> void f(T[2]);
5861 as two declarations of the same function, for example. */
5862 if (processing_template_decl
5863 && potential_nondependent_constant_expression (expr
))
5865 processing_template_decl_sentinel s
;
5866 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5872 instantiate_non_dependent_expr (tree expr
)
5874 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5877 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5878 an uninstantiated expression. */
5881 instantiate_non_dependent_or_null (tree expr
)
5883 if (expr
== NULL_TREE
)
5885 if (processing_template_decl
)
5887 if (!potential_nondependent_constant_expression (expr
))
5891 processing_template_decl_sentinel s
;
5892 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5898 /* True iff T is a specialization of a variable template. */
5901 variable_template_specialization_p (tree t
)
5903 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5905 tree tmpl
= DECL_TI_TEMPLATE (t
);
5906 return variable_template_p (tmpl
);
5909 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5910 template declaration, or a TYPE_DECL for an alias declaration. */
5913 alias_type_or_template_p (tree t
)
5917 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5920 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5921 || DECL_ALIAS_TEMPLATE_P (t
));
5924 /* Return TRUE iff T is a specialization of an alias template. */
5927 alias_template_specialization_p (const_tree t
)
5929 /* It's an alias template specialization if it's an alias and its
5930 TYPE_NAME is a specialization of a primary template. */
5931 if (TYPE_ALIAS_P (t
))
5932 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5933 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5938 /* An alias template is complex from a SFINAE perspective if a template-id
5939 using that alias can be ill-formed when the expansion is not, as with
5940 the void_t template. We determine this by checking whether the
5941 expansion for the alias template uses all its template parameters. */
5943 struct uses_all_template_parms_data
5950 uses_all_template_parms_r (tree t
, void *data_
)
5952 struct uses_all_template_parms_data
&data
5953 = *(struct uses_all_template_parms_data
*)data_
;
5954 tree idx
= get_template_parm_index (t
);
5956 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5957 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5962 complex_alias_template_p (const_tree tmpl
)
5964 struct uses_all_template_parms_data data
;
5965 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5966 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5967 data
.level
= TMPL_PARMS_DEPTH (parms
);
5968 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5969 data
.seen
= XALLOCAVEC (bool, len
);
5970 for (int i
= 0; i
< len
; ++i
)
5971 data
.seen
[i
] = false;
5973 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5974 for (int i
= 0; i
< len
; ++i
)
5980 /* Return TRUE iff T is a specialization of a complex alias template with
5981 dependent template-arguments. */
5984 dependent_alias_template_spec_p (const_tree t
)
5986 if (!alias_template_specialization_p (t
))
5989 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5990 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5993 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5994 if (!any_dependent_template_arguments_p (args
))
6000 /* Return the number of innermost template parameters in TMPL. */
6003 num_innermost_template_parms (tree tmpl
)
6005 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
6006 return TREE_VEC_LENGTH (parms
);
6009 /* Return either TMPL or another template that it is equivalent to under DR
6010 1286: An alias that just changes the name of a template is equivalent to
6011 the other template. */
6014 get_underlying_template (tree tmpl
)
6016 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
6017 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
6019 /* Determine if the alias is equivalent to an underlying template. */
6020 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
6021 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
6025 tree underlying
= TI_TEMPLATE (tinfo
);
6026 if (!PRIMARY_TEMPLATE_P (underlying
)
6027 || (num_innermost_template_parms (tmpl
)
6028 != num_innermost_template_parms (underlying
)))
6031 tree alias_args
= INNERMOST_TEMPLATE_ARGS
6032 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
6033 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
6036 /* Alias is equivalent. Strip it and repeat. */
6043 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6044 must be a reference-to-function or a pointer-to-function type, as specified
6045 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6046 and check that the resulting function has external linkage. */
6049 convert_nontype_argument_function (tree type
, tree expr
,
6050 tsubst_flags_t complain
)
6054 linkage_kind linkage
;
6056 fn
= instantiate_type (type
, fns
, tf_none
);
6057 if (fn
== error_mark_node
)
6058 return error_mark_node
;
6060 if (value_dependent_expression_p (fn
))
6063 fn_no_ptr
= strip_fnptr_conv (fn
);
6064 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
6065 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
6066 if (BASELINK_P (fn_no_ptr
))
6067 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
6069 /* [temp.arg.nontype]/1
6071 A template-argument for a non-type, non-template template-parameter
6074 -- the address of an object or function with external [C++11: or
6075 internal] linkage. */
6077 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
6079 if (complain
& tf_error
)
6081 error ("%qE is not a valid template argument for type %qT",
6083 if (TYPE_PTR_P (type
))
6084 inform (input_location
, "it must be the address of a function "
6085 "with external linkage");
6087 inform (input_location
, "it must be the name of a function with "
6088 "external linkage");
6093 linkage
= decl_linkage (fn_no_ptr
);
6094 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
6096 if (complain
& tf_error
)
6098 if (cxx_dialect
>= cxx11
)
6099 error ("%qE is not a valid template argument for type %qT "
6100 "because %qD has no linkage",
6101 expr
, type
, fn_no_ptr
);
6103 error ("%qE is not a valid template argument for type %qT "
6104 "because %qD does not have external linkage",
6105 expr
, type
, fn_no_ptr
);
6111 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6112 fn
= build_address (fn
);
6113 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
6114 fn
= build_nop (type
, fn
);
6119 /* Subroutine of convert_nontype_argument.
6120 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6121 Emit an error otherwise. */
6124 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
6125 tsubst_flags_t complain
)
6127 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6128 tree orig_expr
= expr
;
6130 if (null_ptr_cst_p (expr
))
6132 if (TREE_CODE (expr
) == PTRMEM_CST
6133 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type
),
6134 PTRMEM_CST_CLASS (expr
)))
6136 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6138 if (processing_template_decl
6139 && TREE_CODE (expr
) == ADDR_EXPR
6140 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6142 if (complain
& tf_error
)
6144 error_at (loc
, "%qE is not a valid template argument for type %qT",
6146 if (TREE_CODE (expr
) != PTRMEM_CST
)
6147 inform (loc
, "it must be a pointer-to-member of the form %<&X::Y%>");
6149 inform (loc
, "because it is a member of %qT", PTRMEM_CST_CLASS (expr
));
6154 /* Returns TRUE iff the address of OP is value-dependent.
6156 14.6.2.4 [temp.dep.temp]:
6157 A non-integral non-type template-argument is dependent if its type is
6158 dependent or it has either of the following forms
6161 and contains a nested-name-specifier which specifies a class-name that
6162 names a dependent type.
6164 We generalize this to just say that the address of a member of a
6165 dependent class is value-dependent; the above doesn't cover the
6166 address of a static data member named with an unqualified-id. */
6169 has_value_dependent_address (tree op
)
6171 /* We could use get_inner_reference here, but there's no need;
6172 this is only relevant for template non-type arguments, which
6173 can only be expressed as &id-expression. */
6176 tree ctx
= CP_DECL_CONTEXT (op
);
6177 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6184 /* The next set of functions are used for providing helpful explanatory
6185 diagnostics for failed overload resolution. Their messages should be
6186 indented by two spaces for consistency with the messages in
6190 unify_success (bool /*explain_p*/)
6195 /* Other failure functions should call this one, to provide a single function
6196 for setting a breakpoint on. */
6199 unify_invalid (bool /*explain_p*/)
6205 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6208 inform (input_location
,
6209 " couldn't deduce template parameter %qD", parm
);
6210 return unify_invalid (explain_p
);
6214 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6217 inform (input_location
,
6218 " types %qT and %qT have incompatible cv-qualifiers",
6220 return unify_invalid (explain_p
);
6224 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6227 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6228 return unify_invalid (explain_p
);
6232 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6235 inform (input_location
,
6236 " template parameter %qD is not a parameter pack, but "
6239 return unify_invalid (explain_p
);
6243 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6246 inform (input_location
,
6247 " template argument %qE does not match "
6248 "pointer-to-member constant %qE",
6250 return unify_invalid (explain_p
);
6254 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6257 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6258 return unify_invalid (explain_p
);
6262 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6265 inform (input_location
,
6266 " inconsistent parameter pack deduction with %qT and %qT",
6268 return unify_invalid (explain_p
);
6272 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6277 inform (input_location
,
6278 " deduced conflicting types for parameter %qT (%qT and %qT)",
6279 parm
, first
, second
);
6281 inform (input_location
,
6282 " deduced conflicting values for non-type parameter "
6283 "%qE (%qE and %qE)", parm
, first
, second
);
6285 return unify_invalid (explain_p
);
6289 unify_vla_arg (bool explain_p
, tree arg
)
6292 inform (input_location
,
6293 " variable-sized array type %qT is not "
6294 "a valid template argument",
6296 return unify_invalid (explain_p
);
6300 unify_method_type_error (bool explain_p
, tree arg
)
6303 inform (input_location
,
6304 " member function type %qT is not a valid template argument",
6306 return unify_invalid (explain_p
);
6310 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6315 inform_n (input_location
, wanted
,
6316 " candidate expects at least %d argument, %d provided",
6317 " candidate expects at least %d arguments, %d provided",
6320 inform_n (input_location
, wanted
,
6321 " candidate expects %d argument, %d provided",
6322 " candidate expects %d arguments, %d provided",
6325 return unify_invalid (explain_p
);
6329 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6331 return unify_arity (explain_p
, have
, wanted
);
6335 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6336 bool least_p
= false)
6338 return unify_arity (explain_p
, have
, wanted
, least_p
);
6342 unify_arg_conversion (bool explain_p
, tree to_type
,
6343 tree from_type
, tree arg
)
6346 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6347 " cannot convert %qE (type %qT) to type %qT",
6348 arg
, from_type
, to_type
);
6349 return unify_invalid (explain_p
);
6353 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6354 tree parm
, tree arg
)
6359 case tbr_ambiguous_baseclass
:
6360 inform (input_location
, " %qT is an ambiguous base class of %qT",
6364 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6367 return unify_invalid (explain_p
);
6371 unify_inconsistent_template_template_parameters (bool explain_p
)
6374 inform (input_location
,
6375 " template parameters of a template template argument are "
6376 "inconsistent with other deduced template arguments");
6377 return unify_invalid (explain_p
);
6381 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6384 inform (input_location
,
6385 " can't deduce a template for %qT from non-template type %qT",
6387 return unify_invalid (explain_p
);
6391 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6394 inform (input_location
,
6395 " template argument %qE does not match %qE", arg
, parm
);
6396 return unify_invalid (explain_p
);
6400 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6403 inform (input_location
,
6404 " could not resolve address from overloaded function %qE",
6406 return unify_invalid (explain_p
);
6409 /* Attempt to convert the non-type template parameter EXPR to the
6410 indicated TYPE. If the conversion is successful, return the
6411 converted value. If the conversion is unsuccessful, return
6412 NULL_TREE if we issued an error message, or error_mark_node if we
6413 did not. We issue error messages for out-and-out bad template
6414 parameters, but not simply because the conversion failed, since we
6415 might be just trying to do argument deduction. Both TYPE and EXPR
6416 must be non-dependent.
6418 The conversion follows the special rules described in
6419 [temp.arg.nontype], and it is much more strict than an implicit
6422 This function is called twice for each template argument (see
6423 lookup_template_class for a more accurate description of this
6424 problem). This means that we need to handle expressions which
6425 are not valid in a C++ source, but can be created from the
6426 first call (for instance, casts to perform conversions). These
6427 hacks can go away after we fix the double coercion problem. */
6430 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6433 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
6434 tree orig_expr
= expr
;
6436 /* Detect immediately string literals as invalid non-type argument.
6437 This special-case is not needed for correctness (we would easily
6438 catch this later), but only to provide better diagnostic for this
6439 common user mistake. As suggested by DR 100, we do not mention
6440 linkage issues in the diagnostic as this is not the point. */
6441 /* FIXME we're making this OK. */
6442 if (TREE_CODE (expr
) == STRING_CST
)
6444 if (complain
& tf_error
)
6445 error ("%qE is not a valid template argument for type %qT "
6446 "because string literals can never be used in this context",
6451 /* Add the ADDR_EXPR now for the benefit of
6452 value_dependent_expression_p. */
6453 if (TYPE_PTROBV_P (type
)
6454 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6456 expr
= decay_conversion (expr
, complain
);
6457 if (expr
== error_mark_node
)
6458 return error_mark_node
;
6461 /* If we are in a template, EXPR may be non-dependent, but still
6462 have a syntactic, rather than semantic, form. For example, EXPR
6463 might be a SCOPE_REF, rather than the VAR_DECL to which the
6464 SCOPE_REF refers. Preserving the qualifying scope is necessary
6465 so that access checking can be performed when the template is
6466 instantiated -- but here we need the resolved form so that we can
6467 convert the argument. */
6468 bool non_dep
= false;
6469 if (TYPE_REF_OBJ_P (type
)
6470 && has_value_dependent_address (expr
))
6471 /* If we want the address and it's value-dependent, don't fold. */;
6472 else if (processing_template_decl
6473 && potential_nondependent_constant_expression (expr
))
6475 if (error_operand_p (expr
))
6476 return error_mark_node
;
6477 expr_type
= TREE_TYPE (expr
);
6478 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6479 expr
= mark_lvalue_use (expr
);
6481 expr
= mark_rvalue_use (expr
);
6483 /* If the argument is non-dependent, perform any conversions in
6484 non-dependent context as well. */
6485 processing_template_decl_sentinel
s (non_dep
);
6487 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6489 if (value_dependent_expression_p (expr
))
6490 expr
= canonicalize_expr_argument (expr
, complain
);
6492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6493 to a non-type argument of "nullptr". */
6494 if (NULLPTR_TYPE_P (expr_type
) && TYPE_PTR_OR_PTRMEM_P (type
))
6495 expr
= fold_simple (convert (type
, expr
));
6497 /* In C++11, integral or enumeration non-type template arguments can be
6498 arbitrary constant expressions. Pointer and pointer to
6499 member arguments can be general constant expressions that evaluate
6500 to a null value, but otherwise still need to be of a specific form. */
6501 if (cxx_dialect
>= cxx11
)
6503 if (TREE_CODE (expr
) == PTRMEM_CST
)
6504 /* A PTRMEM_CST is already constant, and a valid template
6505 argument for a parameter of pointer to member type, we just want
6506 to leave it in that form rather than lower it to a
6508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6509 || cxx_dialect
>= cxx1z
)
6511 /* C++17: A template-argument for a non-type template-parameter shall
6512 be a converted constant expression (8.20) of the type of the
6513 template-parameter. */
6514 expr
= build_converted_constant_expr (type
, expr
, complain
);
6515 if (expr
== error_mark_node
)
6516 return error_mark_node
;
6517 expr
= maybe_constant_value (expr
);
6518 expr
= convert_from_reference (expr
);
6520 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6522 tree folded
= maybe_constant_value (expr
);
6523 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6524 : null_member_pointer_value_p (folded
))
6529 /* HACK: Due to double coercion, we can get a
6530 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6531 which is the tree that we built on the first call (see
6532 below when coercing to reference to object or to reference to
6533 function). We just strip everything and get to the arg.
6534 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6536 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6538 tree probe_type
, probe
= expr
;
6539 if (REFERENCE_REF_P (probe
))
6540 probe
= TREE_OPERAND (probe
, 0);
6541 probe_type
= TREE_TYPE (probe
);
6542 if (TREE_CODE (probe
) == NOP_EXPR
)
6544 /* ??? Maybe we could use convert_from_reference here, but we
6545 would need to relax its constraints because the NOP_EXPR
6546 could actually change the type to something more cv-qualified,
6547 and this is not folded by convert_from_reference. */
6548 tree addr
= TREE_OPERAND (probe
, 0);
6549 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6550 && TREE_CODE (addr
) == ADDR_EXPR
6551 && TYPE_PTR_P (TREE_TYPE (addr
))
6552 && (same_type_ignoring_top_level_qualifiers_p
6553 (TREE_TYPE (probe_type
),
6554 TREE_TYPE (TREE_TYPE (addr
)))))
6556 expr
= TREE_OPERAND (addr
, 0);
6557 expr_type
= TREE_TYPE (probe_type
);
6562 /* [temp.arg.nontype]/5, bullet 1
6564 For a non-type template-parameter of integral or enumeration type,
6565 integral promotions (_conv.prom_) and integral conversions
6566 (_conv.integral_) are applied. */
6567 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6569 if (cxx_dialect
< cxx11
)
6571 tree t
= build_converted_constant_expr (type
, expr
, complain
);
6572 t
= maybe_constant_value (t
);
6573 if (t
!= error_mark_node
)
6577 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6578 return error_mark_node
;
6580 /* Notice that there are constant expressions like '4 % 0' which
6581 do not fold into integer constants. */
6582 if (TREE_CODE (expr
) != INTEGER_CST
6583 && !value_dependent_expression_p (expr
))
6585 if (complain
& tf_error
)
6587 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6588 if (processing_template_decl
6589 && !require_potential_constant_expression (expr
))
6591 expr
= cxx_constant_value (expr
);
6592 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6593 inform (loc
, "in template argument for type %qT ", type
);
6594 if (expr
== error_mark_node
)
6596 /* else cxx_constant_value complained but gave us
6597 a real constant, so go ahead. */
6598 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6604 /* Avoid typedef problems. */
6605 if (TREE_TYPE (expr
) != type
)
6606 expr
= fold_convert (type
, expr
);
6608 /* [temp.arg.nontype]/5, bullet 2
6610 For a non-type template-parameter of type pointer to object,
6611 qualification conversions (_conv.qual_) and the array-to-pointer
6612 conversion (_conv.array_) are applied. */
6613 else if (TYPE_PTROBV_P (type
))
6615 tree decayed
= expr
;
6617 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6618 decay_conversion or an explicit cast. If it's a problematic cast,
6619 we'll complain about it below. */
6620 if (TREE_CODE (expr
) == NOP_EXPR
)
6624 if (TREE_CODE (probe
) == ADDR_EXPR
6625 && TYPE_PTR_P (TREE_TYPE (probe
)))
6628 expr_type
= TREE_TYPE (expr
);
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr
))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr
) != ADDR_EXPR
)
6655 if (complain
& tf_error
)
6656 error ("%qD is not a valid template argument "
6657 "because %qD is a variable, not the address of "
6658 "a variable", orig_expr
, expr
);
6661 if (POINTER_TYPE_P (expr_type
))
6663 if (complain
& tf_error
)
6664 error ("%qE is not a valid template argument for %qT "
6665 "because it is not the address of a variable",
6669 /* Other values, like integer constants, might be valid
6670 non-type arguments of some other type. */
6671 return error_mark_node
;
6675 tree decl
= TREE_OPERAND (expr
, 0);
6679 if (complain
& tf_error
)
6680 error ("%qE is not a valid template argument of type %qT "
6681 "because %qE is not a variable", orig_expr
, type
, decl
);
6684 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6686 if (complain
& tf_error
)
6687 error ("%qE is not a valid template argument of type %qT "
6688 "because %qD does not have external linkage",
6689 orig_expr
, type
, decl
);
6692 else if ((cxx_dialect
>= cxx11
&& cxx_dialect
< cxx1z
)
6693 && decl_linkage (decl
) == lk_none
)
6695 if (complain
& tf_error
)
6696 error ("%qE is not a valid template argument of type %qT "
6697 "because %qD has no linkage", orig_expr
, type
, decl
);
6700 /* C++17: For a non-type template-parameter of reference or pointer
6701 type, the value of the constant expression shall not refer to (or
6702 for a pointer type, shall not be the address of):
6703 * a subobject (4.5),
6704 * a temporary object (15.2),
6705 * a string literal (5.13.5),
6706 * the result of a typeid expression (8.2.8), or
6707 * a predefined __func__ variable (11.4.1). */
6708 else if (DECL_ARTIFICIAL (decl
))
6710 if (complain
& tf_error
)
6711 error ("the address of %qD is not a valid template argument",
6715 else if (!same_type_ignoring_top_level_qualifiers_p
6716 (strip_array_types (TREE_TYPE (type
)),
6717 strip_array_types (TREE_TYPE (decl
))))
6719 if (complain
& tf_error
)
6720 error ("the address of the %qT subobject of %qD is not a "
6721 "valid template argument", TREE_TYPE (type
), decl
);
6724 else if (!TREE_STATIC (decl
) && !DECL_EXTERNAL (decl
))
6726 if (complain
& tf_error
)
6727 error ("the address of %qD is not a valid template argument "
6728 "because it does not have static storage duration",
6736 expr
= perform_qualification_conversions (type
, expr
);
6737 if (expr
== error_mark_node
)
6738 return error_mark_node
;
6740 /* [temp.arg.nontype]/5, bullet 3
6742 For a non-type template-parameter of type reference to object, no
6743 conversions apply. The type referred to by the reference may be more
6744 cv-qualified than the (otherwise identical) type of the
6745 template-argument. The template-parameter is bound directly to the
6746 template-argument, which must be an lvalue. */
6747 else if (TYPE_REF_OBJ_P (type
))
6749 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6751 return error_mark_node
;
6753 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6755 if (complain
& tf_error
)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because of conflicts in cv-qualification", expr
, type
);
6761 if (!lvalue_p (expr
))
6763 if (complain
& tf_error
)
6764 error ("%qE is not a valid template argument for type %qT "
6765 "because it is not an lvalue", expr
, type
);
6769 /* [temp.arg.nontype]/1
6771 A template-argument for a non-type, non-template template-parameter
6772 shall be one of: [...]
6774 -- the address of an object or function with external linkage. */
6775 if (INDIRECT_REF_P (expr
)
6776 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6778 expr
= TREE_OPERAND (expr
, 0);
6781 if (complain
& tf_error
)
6782 error ("%q#D is not a valid template argument for type %qT "
6783 "because a reference variable does not have a constant "
6784 "address", expr
, type
);
6789 if (TYPE_REF_OBJ_P (TREE_TYPE (expr
))
6790 && value_dependent_expression_p (expr
))
6791 /* OK, dependent reference. We don't want to ask whether a DECL is
6792 itself value-dependent, since what we want here is its address. */;
6797 if (complain
& tf_error
)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because it is not an object with linkage",
6804 /* DR 1155 allows internal linkage in C++11 and up. */
6805 linkage_kind linkage
= decl_linkage (expr
);
6806 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6808 if (complain
& tf_error
)
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because object %qD does not have linkage",
6815 expr
= build_address (expr
);
6818 if (!same_type_p (type
, TREE_TYPE (expr
)))
6819 expr
= build_nop (type
, expr
);
6821 /* [temp.arg.nontype]/5, bullet 4
6823 For a non-type template-parameter of type pointer to function, only
6824 the function-to-pointer conversion (_conv.func_) is applied. If the
6825 template-argument represents a set of overloaded functions (or a
6826 pointer to such), the matching function is selected from the set
6828 else if (TYPE_PTRFN_P (type
))
6830 /* If the argument is a template-id, we might not have enough
6831 context information to decay the pointer. */
6832 if (!type_unknown_p (expr_type
))
6834 expr
= decay_conversion (expr
, complain
);
6835 if (expr
== error_mark_node
)
6836 return error_mark_node
;
6839 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6840 /* Null pointer values are OK in C++11. */
6841 return perform_qualification_conversions (type
, expr
);
6843 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6844 if (!expr
|| expr
== error_mark_node
)
6847 /* [temp.arg.nontype]/5, bullet 5
6849 For a non-type template-parameter of type reference to function, no
6850 conversions apply. If the template-argument represents a set of
6851 overloaded functions, the matching function is selected from the set
6853 else if (TYPE_REFFN_P (type
))
6855 if (TREE_CODE (expr
) == ADDR_EXPR
)
6857 if (complain
& tf_error
)
6859 error ("%qE is not a valid template argument for type %qT "
6860 "because it is a pointer", expr
, type
);
6861 inform (input_location
, "try using %qE instead",
6862 TREE_OPERAND (expr
, 0));
6867 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6868 if (!expr
|| expr
== error_mark_node
)
6871 /* [temp.arg.nontype]/5, bullet 6
6873 For a non-type template-parameter of type pointer to member function,
6874 no conversions apply. If the template-argument represents a set of
6875 overloaded member functions, the matching member function is selected
6876 from the set (_over.over_). */
6877 else if (TYPE_PTRMEMFUNC_P (type
))
6879 expr
= instantiate_type (type
, expr
, tf_none
);
6880 if (expr
== error_mark_node
)
6881 return error_mark_node
;
6883 /* [temp.arg.nontype] bullet 1 says the pointer to member
6884 expression must be a pointer-to-member constant. */
6885 if (!value_dependent_expression_p (expr
)
6886 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6889 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6890 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6891 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6892 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6894 /* [temp.arg.nontype]/5, bullet 7
6896 For a non-type template-parameter of type pointer to data member,
6897 qualification conversions (_conv.qual_) are applied. */
6898 else if (TYPE_PTRDATAMEM_P (type
))
6900 /* [temp.arg.nontype] bullet 1 says the pointer to member
6901 expression must be a pointer-to-member constant. */
6902 if (!value_dependent_expression_p (expr
)
6903 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6906 expr
= perform_qualification_conversions (type
, expr
);
6907 if (expr
== error_mark_node
)
6910 else if (NULLPTR_TYPE_P (type
))
6912 if (expr
!= nullptr_node
)
6914 if (complain
& tf_error
)
6915 error ("%qE is not a valid template argument for type %qT "
6916 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6921 /* A template non-type parameter must be one of the above. */
6925 /* Sanity check: did we actually convert the argument to the
6927 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6928 (type
, TREE_TYPE (expr
)));
6929 return convert_from_reference (expr
);
6932 /* Subroutine of coerce_template_template_parms, which returns 1 if
6933 PARM_PARM and ARG_PARM match using the rule for the template
6934 parameters of template template parameters. Both PARM and ARG are
6935 template parameters; the rest of the arguments are the same as for
6936 coerce_template_template_parms.
6939 coerce_template_template_parm (tree parm
,
6941 tsubst_flags_t complain
,
6945 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6946 || parm
== NULL_TREE
|| error_operand_p (parm
))
6949 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6952 switch (TREE_CODE (parm
))
6955 /* We encounter instantiations of templates like
6956 template <template <template <class> class> class TT>
6959 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6960 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6962 if (!coerce_template_template_parms
6963 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6969 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6970 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6971 /* Argument is a parameter pack but parameter is not. */
6976 /* The tsubst call is used to handle cases such as
6978 template <int> class C {};
6979 template <class T, template <T> class TT> class D {};
6982 i.e. the parameter list of TT depends on earlier parameters. */
6983 if (!uses_template_parms (TREE_TYPE (arg
)))
6985 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6986 if (!uses_template_parms (t
)
6987 && !same_type_p (t
, TREE_TYPE (arg
)))
6991 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6992 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6993 /* Argument is a parameter pack but parameter is not. */
7005 /* Coerce template argument list ARGLIST for use with template
7006 template-parameter TEMPL. */
7009 coerce_template_args_for_ttp (tree templ
, tree arglist
,
7010 tsubst_flags_t complain
)
7012 /* Consider an example where a template template parameter declared as
7014 template <class T, class U = std::allocator<T> > class TT
7016 The template parameter level of T and U are one level larger than
7017 of TT. To proper process the default argument of U, say when an
7018 instantiation `TT<int>' is seen, we need to build the full
7019 arguments containing {int} as the innermost level. Outer levels,
7020 available when not appearing as default template argument, can be
7021 obtained from the arguments of the enclosing template.
7023 Suppose that TT is later substituted with std::vector. The above
7024 instantiation is `TT<int, std::allocator<T> >' with TT at
7025 level 1, and T at level 2, while the template arguments at level 1
7026 becomes {std::vector} and the inner level 2 is {int}. */
7028 tree outer
= DECL_CONTEXT (templ
);
7031 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7032 /* We want arguments for the partial specialization, not arguments for
7033 the primary template. */
7034 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7036 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7038 else if (current_template_parms
)
7040 /* This is an argument of the current template, so we haven't set
7041 DECL_CONTEXT yet. */
7042 tree relevant_template_parms
;
7044 /* Parameter levels that are greater than the level of the given
7045 template template parm are irrelevant. */
7046 relevant_template_parms
= current_template_parms
;
7047 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7048 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7049 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7051 outer
= template_parms_to_args (relevant_template_parms
);
7055 arglist
= add_to_template_args (outer
, arglist
);
7057 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7058 return coerce_template_parms (parmlist
, arglist
, templ
,
7060 /*require_all_args=*/true,
7061 /*use_default_args=*/true);
7064 /* A cache of template template parameters with match-all default
7066 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7068 store_defaulted_ttp (tree v
, tree t
)
7070 if (!defaulted_ttp_cache
)
7071 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7072 defaulted_ttp_cache
->put (v
, t
);
7075 lookup_defaulted_ttp (tree v
)
7077 if (defaulted_ttp_cache
)
7078 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7083 /* T is a bound template template-parameter. Copy its arguments into default
7084 arguments of the template template-parameter's template parameters. */
7087 add_defaults_to_ttp (tree otmpl
)
7089 if (tree c
= lookup_defaulted_ttp (otmpl
))
7092 tree ntmpl
= copy_node (otmpl
);
7094 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7095 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7096 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7097 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7098 TYPE_NAME (ntype
) = ntmpl
;
7099 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7101 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7102 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7103 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7104 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7106 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7107 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7108 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7109 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7110 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7112 tree o
= TREE_VEC_ELT (vec
, i
);
7113 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7115 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7116 TREE_PURPOSE (n
) = any_targ_node
;
7120 store_defaulted_ttp (otmpl
, ntmpl
);
7124 /* ARG is a bound potential template template-argument, and PARGS is a list
7125 of arguments for the corresponding template template-parameter. Adjust
7126 PARGS as appropriate for application to ARG's template, and if ARG is a
7127 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7128 arguments to the template template parameter. */
7131 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7133 ++processing_template_decl
;
7134 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7137 /* When comparing two template template-parameters in partial ordering,
7138 rewrite the one currently being used as an argument to have default
7139 arguments for all parameters. */
7140 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7141 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7142 if (pargs
!= error_mark_node
)
7143 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7144 TYPE_TI_ARGS (arg
));
7149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7150 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7151 /*require_all*/true,
7152 /*use_default*/true);
7154 --processing_template_decl
;
7158 /* Subroutine of unify for the case when PARM is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM. */
7162 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7165 tree parmvec
= TYPE_TI_ARGS (parm
);
7166 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7168 /* The template template parm might be variadic and the argument
7169 not, so flatten both argument lists. */
7170 parmvec
= expand_template_argument_pack (parmvec
);
7171 argvec
= expand_template_argument_pack (argvec
);
7173 tree nparmvec
= parmvec
;
7176 /* In keeping with P0522R0, adjust P's template arguments
7177 to apply to A's template; then flatten it again. */
7178 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7179 nparmvec
= expand_template_argument_pack (nparmvec
);
7182 if (unify (tparms
, targs
, nparmvec
, argvec
,
7183 UNIFY_ALLOW_NONE
, explain_p
))
7186 /* If the P0522 adjustment eliminated a pack expansion, deduce
7189 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7190 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7191 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7197 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7198 template template parameters. Both PARM_PARMS and ARG_PARMS are
7199 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7202 Consider the example:
7203 template <class T> class A;
7204 template<template <class U> class TT> class B;
7206 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7207 the parameters to A, and OUTER_ARGS contains A. */
7210 coerce_template_template_parms (tree parm_parms
,
7212 tsubst_flags_t complain
,
7216 int nparms
, nargs
, i
;
7220 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7221 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7223 nparms
= TREE_VEC_LENGTH (parm_parms
);
7224 nargs
= TREE_VEC_LENGTH (arg_parms
);
7228 /* P0522R0: A template template-parameter P is at least as specialized as
7229 a template template-argument A if, given the following rewrite to two
7230 function templates, the function template corresponding to P is at
7231 least as specialized as the function template corresponding to A
7232 according to the partial ordering rules for function templates
7233 ([temp.func.order]). Given an invented class template X with the
7234 template parameter list of A (including default arguments):
7236 * Each of the two function templates has the same template parameters,
7237 respectively, as P or A.
7239 * Each function template has a single function parameter whose type is
7240 a specialization of X with template arguments corresponding to the
7241 template parameters from the respective function template where, for
7242 each template parameter PP in the template parameter list of the
7243 function template, a corresponding template argument AA is formed. If
7244 PP declares a parameter pack, then AA is the pack expansion
7245 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7247 If the rewrite produces an invalid type, then P is not at least as
7248 specialized as A. */
7250 /* So coerce P's args to apply to A's parms, and then deduce between A's
7251 args and the converted args. If that succeeds, A is at least as
7252 specialized as P, so they match.*/
7253 tree pargs
= template_parms_level_to_args (parm_parms
);
7254 ++processing_template_decl
;
7255 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7256 /*require_all*/true, /*use_default*/true);
7257 --processing_template_decl
;
7258 if (pargs
!= error_mark_node
)
7260 tree targs
= make_tree_vec (nargs
);
7261 tree aargs
= template_parms_level_to_args (arg_parms
);
7262 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7268 /* Determine whether we have a parameter pack at the end of the
7269 template template parameter's template parameter list. */
7270 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7272 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7274 if (error_operand_p (parm
))
7277 switch (TREE_CODE (parm
))
7281 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7286 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7296 && !(variadic_p
&& nargs
>= nparms
- 1))
7299 /* Check all of the template parameters except the parameter pack at
7300 the end (if any). */
7301 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7303 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7304 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7307 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7308 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7310 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7318 /* Check each of the template parameters in the template
7319 argument against the template parameter pack at the end of
7320 the template template parameter. */
7321 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7324 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7326 for (; i
< nargs
; ++i
)
7328 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7331 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7333 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7342 /* Verifies that the deduced template arguments (in TARGS) for the
7343 template template parameters (in TPARMS) represent valid bindings,
7344 by comparing the template parameter list of each template argument
7345 to the template parameter list of its corresponding template
7346 template parameter, in accordance with DR150. This
7347 routine can only be called after all template arguments have been
7348 deduced. It will return TRUE if all of the template template
7349 parameter bindings are okay, FALSE otherwise. */
7351 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7353 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7356 /* We're dealing with template parms in this process. */
7357 ++processing_template_decl
;
7359 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7361 for (i
= 0; i
< ntparms
; ++i
)
7363 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7364 tree targ
= TREE_VEC_ELT (targs
, i
);
7366 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7368 tree packed_args
= NULL_TREE
;
7371 if (ARGUMENT_PACK_P (targ
))
7373 /* Look inside the argument pack. */
7374 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7375 len
= TREE_VEC_LENGTH (packed_args
);
7378 for (idx
= 0; idx
< len
; ++idx
)
7380 tree targ_parms
= NULL_TREE
;
7383 /* Extract the next argument from the argument
7385 targ
= TREE_VEC_ELT (packed_args
, idx
);
7387 if (PACK_EXPANSION_P (targ
))
7388 /* Look at the pattern of the pack expansion. */
7389 targ
= PACK_EXPANSION_PATTERN (targ
);
7391 /* Extract the template parameters from the template
7393 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7394 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7395 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7396 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7398 /* Verify that we can coerce the template template
7399 parameters from the template argument to the template
7400 parameter. This requires an exact match. */
7402 && !coerce_template_template_parms
7403 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7418 --processing_template_decl
;
7422 /* Since type attributes aren't mangled, we need to strip them from
7423 template type arguments. */
7426 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7428 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7430 bool removed_attributes
= false;
7431 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7432 if (removed_attributes
7433 && (complain
& tf_warning
))
7434 warning (OPT_Wignored_attributes
,
7435 "ignoring attributes on template argument %qT", arg
);
7439 /* And from inside dependent non-type arguments like sizeof(Type). */
7442 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7444 if (!arg
|| arg
== error_mark_node
)
7446 bool removed_attributes
= false;
7447 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7448 if (removed_attributes
7449 && (complain
& tf_warning
))
7450 warning (OPT_Wignored_attributes
,
7451 "ignoring attributes in template argument %qE", arg
);
7455 // A template declaration can be substituted for a constrained
7456 // template template parameter only when the argument is more
7457 // constrained than the parameter.
7459 is_compatible_template_arg (tree parm
, tree arg
)
7461 tree parm_cons
= get_constraints (parm
);
7463 /* For now, allow constrained template template arguments
7464 and unconstrained template template parameters. */
7465 if (parm_cons
== NULL_TREE
)
7468 tree arg_cons
= get_constraints (arg
);
7470 // If the template parameter is constrained, we need to rewrite its
7471 // constraints in terms of the ARG's template parameters. This ensures
7472 // that all of the template parameter types will have the same depth.
7474 // Note that this is only valid when coerce_template_template_parm is
7475 // true for the innermost template parameters of PARM and ARG. In other
7476 // words, because coercion is successful, this conversion will be valid.
7479 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7480 parm_cons
= tsubst_constraint_info (parm_cons
,
7481 INNERMOST_TEMPLATE_ARGS (args
),
7482 tf_none
, NULL_TREE
);
7483 if (parm_cons
== error_mark_node
)
7487 return subsumes (parm_cons
, arg_cons
);
7490 // Convert a placeholder argument into a binding to the original
7491 // parameter. The original parameter is saved as the TREE_TYPE of
7494 convert_wildcard_argument (tree parm
, tree arg
)
7496 TREE_TYPE (arg
) = parm
;
7500 /* Convert the indicated template ARG as necessary to match the
7501 indicated template PARM. Returns the converted ARG, or
7502 error_mark_node if the conversion was unsuccessful. Error and
7503 warning messages are issued under control of COMPLAIN. This
7504 conversion is for the Ith parameter in the parameter list. ARGS is
7505 the full set of template arguments deduced so far. */
7508 convert_template_argument (tree parm
,
7511 tsubst_flags_t complain
,
7517 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7519 if (parm
== error_mark_node
)
7520 return error_mark_node
;
7522 /* Trivially convert placeholders. */
7523 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7524 return convert_wildcard_argument (parm
, arg
);
7526 if (arg
== any_targ_node
)
7529 if (TREE_CODE (arg
) == TREE_LIST
7530 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7532 /* The template argument was the name of some
7533 member function. That's usually
7534 invalid, but static members are OK. In any
7535 case, grab the underlying fields/functions
7536 and issue an error later if required. */
7537 orig_arg
= TREE_VALUE (arg
);
7538 TREE_TYPE (arg
) = unknown_type_node
;
7543 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7544 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7545 || requires_tmpl_type
);
7547 /* When determining whether an argument pack expansion is a template,
7548 look at the pattern. */
7549 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7550 arg
= PACK_EXPANSION_PATTERN (arg
);
7552 /* Deal with an injected-class-name used as a template template arg. */
7553 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7555 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7556 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7558 if (cxx_dialect
>= cxx11
)
7559 /* OK under DR 1004. */;
7560 else if (complain
& tf_warning_or_error
)
7561 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7562 " used as template template argument", TYPE_NAME (arg
));
7563 else if (flag_pedantic_errors
)
7571 ((TREE_CODE (arg
) == TEMPLATE_DECL
7572 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7573 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7574 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7575 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7578 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7579 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7580 arg
= TYPE_STUB_DECL (arg
);
7582 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7584 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7585 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7587 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7589 if (complain
& tf_error
)
7590 error ("invalid use of destructor %qE as a type", orig_arg
);
7591 return error_mark_node
;
7594 permerror (input_location
,
7595 "to refer to a type member of a template parameter, "
7596 "use %<typename %E%>", orig_arg
);
7598 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7599 TREE_OPERAND (arg
, 1),
7605 if (is_type
!= requires_type
)
7609 if (complain
& tf_error
)
7611 error ("type/value mismatch at argument %d in template "
7612 "parameter list for %qD",
7615 inform (input_location
,
7616 " expected a constant of type %qT, got %qT",
7618 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7619 else if (requires_tmpl_type
)
7620 inform (input_location
,
7621 " expected a class template, got %qE", orig_arg
);
7623 inform (input_location
,
7624 " expected a type, got %qE", orig_arg
);
7627 return error_mark_node
;
7629 if (is_tmpl_type
^ requires_tmpl_type
)
7631 if (in_decl
&& (complain
& tf_error
))
7633 error ("type/value mismatch at argument %d in template "
7634 "parameter list for %qD",
7637 inform (input_location
,
7638 " expected a type, got %qT", DECL_NAME (arg
));
7640 inform (input_location
,
7641 " expected a class template, got %qT", orig_arg
);
7643 return error_mark_node
;
7646 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7647 /* We already did the appropriate conversion when packing args. */
7651 if (requires_tmpl_type
)
7653 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7654 /* The number of argument required is not known yet.
7655 Just accept it for now. */
7656 val
= TREE_TYPE (arg
);
7659 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7662 /* Strip alias templates that are equivalent to another
7664 arg
= get_underlying_template (arg
);
7665 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7667 if (coerce_template_template_parms (parmparm
, argparm
,
7673 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7675 if (val
!= error_mark_node
)
7677 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7678 val
= TREE_TYPE (val
);
7679 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7680 val
= make_pack_expansion (val
);
7685 if (in_decl
&& (complain
& tf_error
))
7687 error ("type/value mismatch at argument %d in "
7688 "template parameter list for %qD",
7690 inform (input_location
,
7691 " expected a template of type %qD, got %qT",
7695 val
= error_mark_node
;
7698 // Check that the constraints are compatible before allowing the
7700 if (val
!= error_mark_node
)
7701 if (!is_compatible_template_arg (parm
, arg
))
7703 if (in_decl
&& (complain
& tf_error
))
7705 error ("constraint mismatch at argument %d in "
7706 "template parameter list for %qD",
7708 inform (input_location
, " expected %qD but got %qD",
7711 val
= error_mark_node
;
7717 /* We only form one instance of each template specialization.
7718 Therefore, if we use a non-canonical variant (i.e., a
7719 typedef), any future messages referring to the type will use
7720 the typedef, which is confusing if those future uses do not
7721 themselves also use the typedef. */
7723 val
= canonicalize_type_argument (val
, complain
);
7727 tree t
= TREE_TYPE (parm
);
7729 if (tree a
= type_uses_auto (t
))
7731 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7732 if (t
== error_mark_node
)
7733 return error_mark_node
;
7736 t
= tsubst (t
, args
, complain
, in_decl
);
7738 if (invalid_nontype_parm_type_p (t
, complain
))
7739 return error_mark_node
;
7741 if (!type_dependent_expression_p (orig_arg
)
7742 && !uses_template_parms (t
))
7743 /* We used to call digest_init here. However, digest_init
7744 will report errors, which we don't want when complain
7745 is zero. More importantly, digest_init will try too
7746 hard to convert things: for example, `0' should not be
7747 converted to pointer type at this point according to
7748 the standard. Accepting this is not merely an
7749 extension, since deciding whether or not these
7750 conversions can occur is part of determining which
7751 function template to call, or whether a given explicit
7752 argument specification is valid. */
7753 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7755 val
= canonicalize_expr_argument (orig_arg
, complain
);
7757 if (val
== NULL_TREE
)
7758 val
= error_mark_node
;
7759 else if (val
== error_mark_node
&& (complain
& tf_error
))
7760 error ("could not convert template argument %qE from %qT to %qT",
7761 orig_arg
, TREE_TYPE (orig_arg
), t
);
7763 if (INDIRECT_REF_P (val
))
7765 /* Reject template arguments that are references to built-in
7766 functions with no library fallbacks. */
7767 const_tree inner
= TREE_OPERAND (val
, 0);
7768 const_tree innertype
= TREE_TYPE (inner
);
7770 && TREE_CODE (innertype
) == REFERENCE_TYPE
7771 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7772 && 0 < TREE_OPERAND_LENGTH (inner
)
7773 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7774 return error_mark_node
;
7777 if (TREE_CODE (val
) == SCOPE_REF
)
7779 /* Strip typedefs from the SCOPE_REF. */
7780 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7781 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7783 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7784 QUALIFIED_NAME_IS_TEMPLATE (val
));
7791 /* Coerces the remaining template arguments in INNER_ARGS (from
7792 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7793 Returns the coerced argument pack. PARM_IDX is the position of this
7794 parameter in the template parameter list. ARGS is the original
7795 template argument list. */
7797 coerce_template_parameter_pack (tree parms
,
7805 tsubst_flags_t complain
)
7807 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7808 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7811 tree packed_parms
= NULL_TREE
;
7813 if (arg_idx
> nargs
)
7816 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7818 /* When the template parameter is a non-type template parameter pack
7819 or template template parameter pack whose type or template
7820 parameters use parameter packs, we know exactly how many arguments
7821 we are looking for. Build a vector of the instantiated decls for
7822 these template parameters in PACKED_PARMS. */
7823 /* We can't use make_pack_expansion here because it would interpret a
7824 _DECL as a use rather than a declaration. */
7825 tree decl
= TREE_VALUE (parm
);
7826 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7827 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7828 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7829 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7831 TREE_VEC_LENGTH (args
)--;
7832 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7833 TREE_VEC_LENGTH (args
)++;
7835 if (packed_parms
== error_mark_node
)
7836 return error_mark_node
;
7838 /* If we're doing a partial instantiation of a member template,
7839 verify that all of the types used for the non-type
7840 template parameter pack are, in fact, valid for non-type
7841 template parameters. */
7843 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7845 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7846 for (j
= 0; j
< len
; ++j
)
7848 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7849 if (invalid_nontype_parm_type_p (t
, complain
))
7850 return error_mark_node
;
7852 /* We don't know how many args we have yet, just
7853 use the unconverted ones for now. */
7857 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7859 /* Check if we have a placeholder pack, which indicates we're
7860 in the context of a introduction list. In that case we want
7861 to match this pack to the single placeholder. */
7862 else if (arg_idx
< nargs
7863 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7864 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7866 nargs
= arg_idx
+ 1;
7867 packed_args
= make_tree_vec (1);
7870 packed_args
= make_tree_vec (nargs
- arg_idx
);
7872 /* Convert the remaining arguments, which will be a part of the
7873 parameter pack "parm". */
7874 int first_pack_arg
= arg_idx
;
7875 for (; arg_idx
< nargs
; ++arg_idx
)
7877 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7878 tree actual_parm
= TREE_VALUE (parm
);
7879 int pack_idx
= arg_idx
- first_pack_arg
;
7883 /* Once we've packed as many args as we have types, stop. */
7884 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7886 else if (PACK_EXPANSION_P (arg
))
7887 /* We don't know how many args we have yet, just
7888 use the unconverted ones for now. */
7891 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7894 if (arg
== error_mark_node
)
7896 if (complain
& tf_error
)
7897 error ("template argument %d is invalid", arg_idx
+ 1);
7900 arg
= convert_template_argument (actual_parm
,
7901 arg
, new_args
, complain
, parm_idx
,
7903 if (arg
== error_mark_node
)
7905 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7908 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7909 && TREE_VEC_LENGTH (packed_args
) > 0)
7911 if (complain
& tf_error
)
7912 error ("wrong number of template arguments (%d, should be %d)",
7913 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7914 return error_mark_node
;
7917 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7918 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7919 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7922 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7923 TREE_CONSTANT (argument_pack
) = 1;
7926 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7928 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7929 TREE_VEC_LENGTH (packed_args
));
7930 return argument_pack
;
7933 /* Returns the number of pack expansions in the template argument vector
7937 pack_expansion_args_count (tree args
)
7942 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7944 tree elt
= TREE_VEC_ELT (args
, i
);
7945 if (elt
&& PACK_EXPANSION_P (elt
))
7951 /* Convert all template arguments to their appropriate types, and
7952 return a vector containing the innermost resulting template
7953 arguments. If any error occurs, return error_mark_node. Error and
7954 warning messages are issued under control of COMPLAIN.
7956 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7957 for arguments not specified in ARGS. Otherwise, if
7958 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7959 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7960 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7964 coerce_template_parms (tree parms
,
7967 tsubst_flags_t complain
,
7968 bool require_all_args
,
7969 bool use_default_args
)
7971 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7972 tree orig_inner_args
;
7975 tree new_inner_args
;
7976 int saved_unevaluated_operand
;
7977 int saved_inhibit_evaluation_warnings
;
7979 /* When used as a boolean value, indicates whether this is a
7980 variadic template parameter list. Since it's an int, we can also
7981 subtract it from nparms to get the number of non-variadic
7984 int variadic_args_p
= 0;
7985 int post_variadic_parms
= 0;
7987 /* Likewise for parameters with default arguments. */
7990 if (args
== error_mark_node
)
7991 return error_mark_node
;
7993 nparms
= TREE_VEC_LENGTH (parms
);
7995 /* Determine if there are any parameter packs or default arguments. */
7996 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7998 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
8000 ++post_variadic_parms
;
8001 if (template_parameter_pack_p (TREE_VALUE (parm
)))
8003 if (TREE_PURPOSE (parm
))
8007 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
8008 /* If there are no parameters that follow a parameter pack, we need to
8009 expand any argument packs so that we can deduce a parameter pack from
8010 some non-packed args followed by an argument pack, as in variadic85.C.
8011 If there are such parameters, we need to leave argument packs intact
8012 so the arguments are assigned properly. This can happen when dealing
8013 with a nested class inside a partial specialization of a class
8014 template, as in variadic92.C, or when deducing a template parameter pack
8015 from a sub-declarator, as in variadic114.C. */
8016 if (!post_variadic_parms
)
8017 inner_args
= expand_template_argument_pack (inner_args
);
8019 /* Count any pack expansion args. */
8020 variadic_args_p
= pack_expansion_args_count (inner_args
);
8022 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8023 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8024 || (nargs
< nparms
- variadic_p
8027 && (!use_default_args
8028 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8029 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8031 if (complain
& tf_error
)
8033 if (variadic_p
|| default_p
)
8035 nparms
-= variadic_p
+ default_p
;
8036 error ("wrong number of template arguments "
8037 "(%d, should be at least %d)", nargs
, nparms
);
8040 error ("wrong number of template arguments "
8041 "(%d, should be %d)", nargs
, nparms
);
8044 inform (DECL_SOURCE_LOCATION (in_decl
),
8045 "provided for %qD", in_decl
);
8048 return error_mark_node
;
8050 /* We can't pass a pack expansion to a non-pack parameter of an alias
8051 template (DR 1430). */
8053 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8054 || concept_template_p (in_decl
))
8056 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8058 if (complain
& tf_error
)
8060 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8062 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8063 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8065 if (PACK_EXPANSION_P (arg
)
8066 && !template_parameter_pack_p (parm
))
8068 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8069 error_at (location_of (arg
),
8070 "pack expansion argument for non-pack parameter "
8071 "%qD of alias template %qD", parm
, in_decl
);
8073 error_at (location_of (arg
),
8074 "pack expansion argument for non-pack parameter "
8075 "%qD of concept %qD", parm
, in_decl
);
8076 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8083 return error_mark_node
;
8086 /* We need to evaluate the template arguments, even though this
8087 template-id may be nested within a "sizeof". */
8088 saved_unevaluated_operand
= cp_unevaluated_operand
;
8089 cp_unevaluated_operand
= 0;
8090 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8091 c_inhibit_evaluation_warnings
= 0;
8092 new_inner_args
= make_tree_vec (nparms
);
8093 new_args
= add_outermost_template_args (args
, new_inner_args
);
8094 int pack_adjust
= 0;
8095 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8100 /* Get the Ith template parameter. */
8101 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8103 if (parm
== error_mark_node
)
8105 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8109 /* Calculate the next argument. */
8110 if (arg_idx
< nargs
)
8111 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8115 if (template_parameter_pack_p (TREE_VALUE (parm
))
8116 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8118 /* Some arguments will be placed in the
8119 template parameter pack PARM. */
8120 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8121 inner_args
, arg_idx
,
8125 if (arg
== NULL_TREE
)
8127 /* We don't know how many args we have yet, just use the
8128 unconverted (and still packed) ones for now. */
8129 new_inner_args
= orig_inner_args
;
8134 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8136 /* Store this argument. */
8137 if (arg
== error_mark_node
)
8140 /* We are done with all of the arguments. */
8145 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8146 arg_idx
+= pack_adjust
;
8153 if (PACK_EXPANSION_P (arg
))
8155 /* "If every valid specialization of a variadic template
8156 requires an empty template parameter pack, the template is
8157 ill-formed, no diagnostic required." So check that the
8158 pattern works with this parameter. */
8159 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8160 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8164 if (conv
== error_mark_node
)
8166 if (complain
& tf_error
)
8167 inform (input_location
, "so any instantiation with a "
8168 "non-empty parameter pack would be ill-formed");
8171 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8172 /* Recover from missing typename. */
8173 TREE_VEC_ELT (inner_args
, arg_idx
)
8174 = make_pack_expansion (conv
);
8176 /* We don't know how many args we have yet, just
8177 use the unconverted ones for now. */
8178 new_inner_args
= inner_args
;
8183 else if (require_all_args
)
8185 /* There must be a default arg in this case. */
8186 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8188 /* The position of the first default template argument,
8189 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8191 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8192 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8193 arg_idx
- pack_adjust
);
8198 if (arg
== error_mark_node
)
8200 if (complain
& tf_error
)
8201 error ("template argument %d is invalid", arg_idx
+ 1);
8204 /* This only occurs if there was an error in the template
8205 parameter list itself (which we would already have
8206 reported) that we are trying to recover from, e.g., a class
8207 template with a parameter list such as
8208 template<typename..., typename>. */
8211 arg
= convert_template_argument (TREE_VALUE (parm
),
8212 arg
, new_args
, complain
,
8215 if (arg
== error_mark_node
)
8217 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8219 cp_unevaluated_operand
= saved_unevaluated_operand
;
8220 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8222 if (variadic_p
&& arg_idx
< nargs
)
8224 if (complain
& tf_error
)
8226 error ("wrong number of template arguments "
8227 "(%d, should be %d)", nargs
, arg_idx
);
8229 error ("provided for %q+D", in_decl
);
8231 return error_mark_node
;
8235 return error_mark_node
;
8237 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8238 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8239 TREE_VEC_LENGTH (new_inner_args
));
8241 return new_inner_args
;
8244 /* Convert all template arguments to their appropriate types, and
8245 return a vector containing the innermost resulting template
8246 arguments. If any error occurs, return error_mark_node. Error and
8247 warning messages are not issued.
8249 Note that no function argument deduction is performed, and default
8250 arguments are used to fill in unspecified arguments. */
8252 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8254 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8257 /* Convert all template arguments to their appropriate type, and
8258 instantiate default arguments as needed. This returns a vector
8259 containing the innermost resulting template arguments, or
8260 error_mark_node if unsuccessful. */
8262 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8263 tsubst_flags_t complain
)
8265 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8268 /* Like coerce_template_parms. If PARMS represents all template
8269 parameters levels, this function returns a vector of vectors
8270 representing all the resulting argument levels. Note that in this
8271 case, only the innermost arguments are coerced because the
8272 outermost ones are supposed to have been coerced already.
8274 Otherwise, if PARMS represents only (the innermost) vector of
8275 parameters, this function returns a vector containing just the
8276 innermost resulting arguments. */
8279 coerce_innermost_template_parms (tree parms
,
8282 tsubst_flags_t complain
,
8283 bool require_all_args
,
8284 bool use_default_args
)
8286 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8287 int args_depth
= TMPL_ARGS_DEPTH (args
);
8290 if (parms_depth
> 1)
8292 coerced_args
= make_tree_vec (parms_depth
);
8296 for (level
= parms
, cur_depth
= parms_depth
;
8297 parms_depth
> 0 && level
!= NULL_TREE
;
8298 level
= TREE_CHAIN (level
), --cur_depth
)
8301 if (cur_depth
== args_depth
)
8302 l
= coerce_template_parms (TREE_VALUE (level
),
8303 args
, in_decl
, complain
,
8307 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8309 if (l
== error_mark_node
)
8310 return error_mark_node
;
8312 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8316 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8317 args
, in_decl
, complain
,
8320 return coerced_args
;
8323 /* Returns 1 if template args OT and NT are equivalent. */
8326 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8330 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8332 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8335 if (TREE_CODE (nt
) == TREE_VEC
)
8336 /* For member templates */
8337 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8338 else if (PACK_EXPANSION_P (ot
))
8339 return (PACK_EXPANSION_P (nt
)
8340 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8341 PACK_EXPANSION_PATTERN (nt
))
8342 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8343 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8344 else if (ARGUMENT_PACK_P (ot
))
8349 if (!ARGUMENT_PACK_P (nt
))
8352 opack
= ARGUMENT_PACK_ARGS (ot
);
8353 npack
= ARGUMENT_PACK_ARGS (nt
);
8354 len
= TREE_VEC_LENGTH (opack
);
8355 if (TREE_VEC_LENGTH (npack
) != len
)
8357 for (i
= 0; i
< len
; ++i
)
8358 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8359 TREE_VEC_ELT (npack
, i
)))
8363 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8365 else if (TYPE_P (nt
))
8369 /* Don't treat an alias template specialization with dependent
8370 arguments as equivalent to its underlying type when used as a
8371 template argument; we need them to be distinct so that we
8372 substitute into the specialization arguments at instantiation
8373 time. And aliases can't be equivalent without being ==, so
8374 we don't need to look any deeper.
8376 During partial ordering, however, we need to treat them normally so
8377 that we can order uses of the same alias with different
8378 cv-qualification (79960). */
8380 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8383 return same_type_p (ot
, nt
);
8385 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8389 /* Try to treat a template non-type argument that has been converted
8390 to the parameter type as equivalent to one that hasn't yet. */
8391 for (enum tree_code code1
= TREE_CODE (ot
);
8392 CONVERT_EXPR_CODE_P (code1
)
8393 || code1
== NON_LVALUE_EXPR
;
8394 code1
= TREE_CODE (ot
))
8395 ot
= TREE_OPERAND (ot
, 0);
8396 for (enum tree_code code2
= TREE_CODE (nt
);
8397 CONVERT_EXPR_CODE_P (code2
)
8398 || code2
== NON_LVALUE_EXPR
;
8399 code2
= TREE_CODE (nt
))
8400 nt
= TREE_OPERAND (nt
, 0);
8402 return cp_tree_equal (ot
, nt
);
8406 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8407 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8408 NEWARG_PTR with the offending arguments if they are non-NULL. */
8411 comp_template_args (tree oldargs
, tree newargs
,
8412 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8417 if (oldargs
== newargs
)
8420 if (!oldargs
|| !newargs
)
8423 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8426 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8428 tree nt
= TREE_VEC_ELT (newargs
, i
);
8429 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8431 if (! template_args_equal (ot
, nt
, partial_order
))
8433 if (oldarg_ptr
!= NULL
)
8435 if (newarg_ptr
!= NULL
)
8444 comp_template_args_porder (tree oargs
, tree nargs
)
8446 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8450 add_pending_template (tree d
)
8452 tree ti
= (TYPE_P (d
)
8453 ? CLASSTYPE_TEMPLATE_INFO (d
)
8454 : DECL_TEMPLATE_INFO (d
));
8455 struct pending_template
*pt
;
8458 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8461 /* We are called both from instantiate_decl, where we've already had a
8462 tinst_level pushed, and instantiate_template, where we haven't.
8464 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8467 push_tinst_level (d
);
8469 pt
= ggc_alloc
<pending_template
> ();
8471 pt
->tinst
= current_tinst_level
;
8472 if (last_pending_template
)
8473 last_pending_template
->next
= pt
;
8475 pending_templates
= pt
;
8477 last_pending_template
= pt
;
8479 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8486 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8487 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8488 documentation for TEMPLATE_ID_EXPR. */
8491 lookup_template_function (tree fns
, tree arglist
)
8495 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8496 return error_mark_node
;
8498 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8500 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8502 error ("%q#D is not a function template", fns
);
8503 return error_mark_node
;
8506 if (BASELINK_P (fns
))
8508 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8510 BASELINK_FUNCTIONS (fns
),
8515 type
= TREE_TYPE (fns
);
8516 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8517 type
= unknown_type_node
;
8519 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8522 /* Within the scope of a template class S<T>, the name S gets bound
8523 (in build_self_reference) to a TYPE_DECL for the class, not a
8524 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8525 or one of its enclosing classes, and that type is a template,
8526 return the associated TEMPLATE_DECL. Otherwise, the original
8529 Also handle the case when DECL is a TREE_LIST of ambiguous
8530 injected-class-names from different bases. */
8533 maybe_get_template_decl_from_type_decl (tree decl
)
8535 if (decl
== NULL_TREE
)
8538 /* DR 176: A lookup that finds an injected-class-name (10.2
8539 [class.member.lookup]) can result in an ambiguity in certain cases
8540 (for example, if it is found in more than one base class). If all of
8541 the injected-class-names that are found refer to specializations of
8542 the same class template, and if the name is followed by a
8543 template-argument-list, the reference refers to the class template
8544 itself and not a specialization thereof, and is not ambiguous. */
8545 if (TREE_CODE (decl
) == TREE_LIST
)
8547 tree t
, tmpl
= NULL_TREE
;
8548 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8550 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8553 else if (tmpl
!= elt
)
8556 if (tmpl
&& t
== NULL_TREE
)
8562 return (decl
!= NULL_TREE
8563 && DECL_SELF_REFERENCE_P (decl
)
8564 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8565 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8568 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8569 parameters, find the desired type.
8571 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8573 IN_DECL, if non-NULL, is the template declaration we are trying to
8576 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8577 the class we are looking up.
8579 Issue error and warning messages under control of COMPLAIN.
8581 If the template class is really a local class in a template
8582 function, then the FUNCTION_CONTEXT is the function in which it is
8585 ??? Note that this function is currently called *twice* for each
8586 template-id: the first time from the parser, while creating the
8587 incomplete type (finish_template_type), and the second type during the
8588 real instantiation (instantiate_template_class). This is surely something
8589 that we want to avoid. It also causes some problems with argument
8590 coercion (see convert_nontype_argument for more information on this). */
8593 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8594 int entering_scope
, tsubst_flags_t complain
)
8596 tree templ
= NULL_TREE
, parmlist
;
8603 if (identifier_p (d1
))
8605 tree value
= innermost_non_namespace_value (d1
);
8606 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8611 push_decl_namespace (context
);
8612 templ
= lookup_name (d1
);
8613 templ
= maybe_get_template_decl_from_type_decl (templ
);
8615 pop_decl_namespace ();
8618 context
= DECL_CONTEXT (templ
);
8620 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8622 tree type
= TREE_TYPE (d1
);
8624 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8625 an implicit typename for the second A. Deal with it. */
8626 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8627 type
= TREE_TYPE (type
);
8629 if (CLASSTYPE_TEMPLATE_INFO (type
))
8631 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8632 d1
= DECL_NAME (templ
);
8635 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8636 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8638 templ
= TYPE_TI_TEMPLATE (d1
);
8639 d1
= DECL_NAME (templ
);
8641 else if (DECL_TYPE_TEMPLATE_P (d1
))
8644 d1
= DECL_NAME (templ
);
8645 context
= DECL_CONTEXT (templ
);
8647 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8650 d1
= DECL_NAME (templ
);
8653 /* Issue an error message if we didn't find a template. */
8656 if (complain
& tf_error
)
8657 error ("%qT is not a template", d1
);
8658 return error_mark_node
;
8661 if (TREE_CODE (templ
) != TEMPLATE_DECL
8662 /* Make sure it's a user visible template, if it was named by
8664 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8665 && !PRIMARY_TEMPLATE_P (templ
)))
8667 if (complain
& tf_error
)
8669 error ("non-template type %qT used as a template", d1
);
8671 error ("for template declaration %q+D", in_decl
);
8673 return error_mark_node
;
8676 complain
&= ~tf_user
;
8678 /* An alias that just changes the name of a template is equivalent to the
8679 other template, so if any of the arguments are pack expansions, strip
8680 the alias to avoid problems with a pack expansion passed to a non-pack
8681 alias template parameter (DR 1430). */
8682 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8683 templ
= get_underlying_template (templ
);
8685 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8688 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8689 if (arglist2
== error_mark_node
8690 || (!uses_template_parms (arglist2
)
8691 && check_instantiated_args (templ
, arglist2
, complain
)))
8692 return error_mark_node
;
8694 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8699 tree template_type
= TREE_TYPE (templ
);
8702 tree found
= NULL_TREE
;
8705 int is_dependent_type
;
8706 int use_partial_inst_tmpl
= false;
8708 if (template_type
== error_mark_node
)
8709 /* An error occurred while building the template TEMPL, and a
8710 diagnostic has most certainly been emitted for that
8711 already. Let's propagate that error. */
8712 return error_mark_node
;
8714 gen_tmpl
= most_general_template (templ
);
8715 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8716 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8717 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8719 if (arg_depth
== 1 && parm_depth
> 1)
8721 /* We've been given an incomplete set of template arguments.
8724 template <class T> struct S1 {
8725 template <class U> struct S2 {};
8726 template <class U> struct S2<U*> {};
8729 we will be called with an ARGLIST of `U*', but the
8730 TEMPLATE will be `template <class T> template
8731 <class U> struct S1<T>::S2'. We must fill in the missing
8733 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8734 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8735 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8738 /* Now we should have enough arguments. */
8739 gcc_assert (parm_depth
== arg_depth
);
8741 /* From here on, we're only interested in the most general
8744 /* Calculate the BOUND_ARGS. These will be the args that are
8745 actually tsubst'd into the definition to create the
8747 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8749 /*require_all_args=*/true,
8750 /*use_default_args=*/true);
8752 if (arglist
== error_mark_node
)
8753 /* We were unable to bind the arguments. */
8754 return error_mark_node
;
8756 /* In the scope of a template class, explicit references to the
8757 template class refer to the type of the template, not any
8758 instantiation of it. For example, in:
8760 template <class T> class C { void f(C<T>); }
8762 the `C<T>' is just the same as `C'. Outside of the
8763 class, however, such a reference is an instantiation. */
8765 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8766 || currently_open_class (template_type
))
8768 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8770 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8771 return template_type
;
8774 /* If we already have this specialization, return it. */
8775 elt
.tmpl
= gen_tmpl
;
8777 elt
.spec
= NULL_TREE
;
8778 hash
= spec_hasher::hash (&elt
);
8779 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8784 /* If the the template's constraints are not satisfied,
8785 then we cannot form a valid type.
8787 Note that the check is deferred until after the hash
8788 lookup. This prevents redundant checks on previously
8789 instantiated specializations. */
8790 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8792 if (complain
& tf_error
)
8794 error ("template constraint failure");
8795 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8797 return error_mark_node
;
8800 is_dependent_type
= uses_template_parms (arglist
);
8802 /* If the deduced arguments are invalid, then the binding
8804 if (!is_dependent_type
8805 && check_instantiated_args (gen_tmpl
,
8806 INNERMOST_TEMPLATE_ARGS (arglist
),
8808 return error_mark_node
;
8810 if (!is_dependent_type
8811 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8812 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8813 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8815 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8816 DECL_NAME (gen_tmpl
),
8817 /*tag_scope=*/ts_global
);
8821 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8823 if (context
== error_mark_node
)
8824 return error_mark_node
;
8827 context
= global_namespace
;
8829 /* Create the type. */
8830 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8832 /* The user referred to a specialization of an alias
8833 template represented by GEN_TMPL.
8835 [temp.alias]/2 says:
8837 When a template-id refers to the specialization of an
8838 alias template, it is equivalent to the associated
8839 type obtained by substitution of its
8840 template-arguments for the template-parameters in the
8841 type-id of the alias template. */
8843 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8844 /* Note that the call above (by indirectly calling
8845 register_specialization in tsubst_decl) registers the
8846 TYPE_DECL representing the specialization of the alias
8847 template. So next time someone substitutes ARGLIST for
8848 the template parms into the alias template (GEN_TMPL),
8849 she'll get that TYPE_DECL back. */
8851 if (t
== error_mark_node
)
8854 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8856 if (!is_dependent_type
)
8858 set_current_access_from_decl (TYPE_NAME (template_type
));
8859 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8860 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8861 arglist
, complain
, in_decl
),
8862 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8863 arglist
, complain
, in_decl
),
8864 SCOPED_ENUM_P (template_type
), NULL
);
8866 if (t
== error_mark_node
)
8871 /* We don't want to call start_enum for this type, since
8872 the values for the enumeration constants may involve
8873 template parameters. And, no one should be interested
8874 in the enumeration constants for such a type. */
8875 t
= cxx_make_type (ENUMERAL_TYPE
);
8876 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8878 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8879 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8880 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8882 else if (CLASS_TYPE_P (template_type
))
8884 t
= make_class_type (TREE_CODE (template_type
));
8885 CLASSTYPE_DECLARED_CLASS (t
)
8886 = CLASSTYPE_DECLARED_CLASS (template_type
);
8887 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8889 /* A local class. Make sure the decl gets registered properly. */
8890 if (context
== current_function_decl
)
8891 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8893 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8894 /* This instantiation is another name for the primary
8895 template type. Set the TYPE_CANONICAL field
8897 TYPE_CANONICAL (t
) = template_type
;
8898 else if (any_template_arguments_need_structural_equality_p (arglist
))
8899 /* Some of the template arguments require structural
8900 equality testing, so this template class requires
8901 structural equality testing. */
8902 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8907 /* If we called start_enum or pushtag above, this information
8908 will already be set up. */
8911 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8913 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8914 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8915 DECL_SOURCE_LOCATION (type_decl
)
8916 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8919 type_decl
= TYPE_NAME (t
);
8921 if (CLASS_TYPE_P (template_type
))
8923 TREE_PRIVATE (type_decl
)
8924 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8925 TREE_PROTECTED (type_decl
)
8926 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8927 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8929 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8930 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8934 if (OVERLOAD_TYPE_P (t
)
8935 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8937 static const char *tags
[] = {"abi_tag", "may_alias"};
8939 for (unsigned ix
= 0; ix
!= 2; ix
++)
8942 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8946 = tree_cons (TREE_PURPOSE (attributes
),
8947 TREE_VALUE (attributes
),
8948 TYPE_ATTRIBUTES (t
));
8952 /* Let's consider the explicit specialization of a member
8953 of a class template specialization that is implicitly instantiated,
8958 template<class U> struct M {}; //#0
8963 struct S<int>::M<char> //#1
8967 [temp.expl.spec]/4 says this is valid.
8969 In this case, when we write:
8972 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8975 When we encounter #1, we want to store the partial instantiation
8976 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8978 For all cases other than this "explicit specialization of member of a
8979 class template", we just want to store the most general template into
8980 the CLASSTYPE_TI_TEMPLATE of M.
8982 This case of "explicit specialization of member of a class template"
8984 1/ the enclosing class is an instantiation of, and therefore not
8985 the same as, the context of the most general template, and
8986 2/ we aren't looking at the partial instantiation itself, i.e.
8987 the innermost arguments are not the same as the innermost parms of
8988 the most general template.
8990 So it's only when 1/ and 2/ happens that we want to use the partial
8991 instantiation of the member template in lieu of its most general
8994 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8995 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8996 /* the enclosing class must be an instantiation... */
8997 && CLASS_TYPE_P (context
)
8998 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
9000 TREE_VEC_LENGTH (arglist
)--;
9001 ++processing_template_decl
;
9002 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
9003 tree partial_inst_args
=
9004 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
9005 arglist
, complain
, NULL_TREE
);
9006 --processing_template_decl
;
9007 TREE_VEC_LENGTH (arglist
)++;
9008 if (partial_inst_args
== error_mark_node
)
9009 return error_mark_node
;
9010 use_partial_inst_tmpl
=
9011 /*...and we must not be looking at the partial instantiation
9013 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
9017 if (!use_partial_inst_tmpl
)
9018 /* This case is easy; there are no member templates involved. */
9022 /* This is a full instantiation of a member template. Find
9023 the partial instantiation of which this is an instance. */
9025 /* Temporarily reduce by one the number of levels in the ARGLIST
9026 so as to avoid comparing the last set of arguments. */
9027 TREE_VEC_LENGTH (arglist
)--;
9028 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9029 TREE_VEC_LENGTH (arglist
)++;
9030 /* FOUND is either a proper class type, or an alias
9031 template specialization. In the later case, it's a
9032 TYPE_DECL, resulting from the substituting of arguments
9033 for parameters in the TYPE_DECL of the alias template
9034 done earlier. So be careful while getting the template
9036 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9038 : (TREE_CODE (found
) == TYPE_DECL
9039 ? DECL_TI_TEMPLATE (found
)
9040 : CLASSTYPE_TI_TEMPLATE (found
)));
9043 // Build template info for the new specialization.
9044 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9047 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9048 entry
= ggc_alloc
<spec_entry
> ();
9052 /* Note this use of the partial instantiation so we can check it
9053 later in maybe_process_partial_specialization. */
9054 DECL_TEMPLATE_INSTANTIATIONS (found
)
9055 = tree_cons (arglist
, t
,
9056 DECL_TEMPLATE_INSTANTIATIONS (found
));
9058 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9059 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9060 /* Now that the type has been registered on the instantiations
9061 list, we set up the enumerators. Because the enumeration
9062 constants may involve the enumeration type itself, we make
9063 sure to register the type first, and then create the
9064 constants. That way, doing tsubst_expr for the enumeration
9065 constants won't result in recursive calls here; we'll find
9066 the instantiation and exit above. */
9067 tsubst_enum (template_type
, t
, arglist
);
9069 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9070 /* If the type makes use of template parameters, the
9071 code that generates debugging information will crash. */
9072 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9074 /* Possibly limit visibility based on template args. */
9075 TREE_PUBLIC (type_decl
) = 1;
9076 determine_visibility (type_decl
);
9078 inherit_targ_abi_tags (t
);
9084 /* Wrapper for lookup_template_class_1. */
9087 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9088 int entering_scope
, tsubst_flags_t complain
)
9091 timevar_push (TV_TEMPLATE_INST
);
9092 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9093 entering_scope
, complain
);
9094 timevar_pop (TV_TEMPLATE_INST
);
9098 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9101 lookup_template_variable (tree templ
, tree arglist
)
9103 /* The type of the expression is NULL_TREE since the template-id could refer
9104 to an explicit or partial specialization. */
9105 tree type
= NULL_TREE
;
9106 if (flag_concepts
&& variable_concept_p (templ
))
9107 /* Except that concepts are always bool. */
9108 type
= boolean_type_node
;
9109 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9112 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9115 finish_template_variable (tree var
, tsubst_flags_t complain
)
9117 tree templ
= TREE_OPERAND (var
, 0);
9118 tree arglist
= TREE_OPERAND (var
, 1);
9120 /* We never want to return a VAR_DECL for a variable concept, since they
9121 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9122 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9123 if (concept_p
&& processing_template_decl
)
9126 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9127 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9129 templ
= most_general_template (templ
);
9130 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9131 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9133 /*use_default*/true);
9135 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9137 if (complain
& tf_error
)
9139 error ("use of invalid variable template %qE", var
);
9140 diagnose_constraints (location_of (var
), templ
, arglist
);
9142 return error_mark_node
;
9145 /* If a template-id refers to a specialization of a variable
9146 concept, then the expression is true if and only if the
9147 concept's constraints are satisfied by the given template
9150 NOTE: This is an extension of Concepts Lite TS that
9151 allows constraints to be used in expressions. */
9154 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9155 return evaluate_variable_concept (decl
, arglist
);
9158 return instantiate_template (templ
, arglist
, complain
);
9161 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9162 TARGS template args, and instantiate it if it's not dependent. */
9165 lookup_and_finish_template_variable (tree templ
, tree targs
,
9166 tsubst_flags_t complain
)
9168 templ
= lookup_template_variable (templ
, targs
);
9169 if (!any_dependent_template_arguments_p (targs
))
9171 templ
= finish_template_variable (templ
, complain
);
9175 return convert_from_reference (templ
);
9184 /* True when we should also visit template parameters that occur in
9185 non-deduced contexts. */
9186 bool include_nondeduced_p
;
9187 hash_set
<tree
> *visited
;
9190 /* Called from for_each_template_parm via walk_tree. */
9193 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9196 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9197 tree_fn_t fn
= pfd
->fn
;
9198 void *data
= pfd
->data
;
9199 tree result
= NULL_TREE
;
9201 #define WALK_SUBTREE(NODE) \
9204 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9205 pfd->include_nondeduced_p, \
9207 if (result) goto out; \
9211 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9215 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9216 WALK_SUBTREE (TYPE_CONTEXT (t
));
9218 switch (TREE_CODE (t
))
9221 if (TYPE_PTRMEMFUNC_P (t
))
9227 if (!TYPE_TEMPLATE_INFO (t
))
9230 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9234 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9235 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9239 /* Since we're not going to walk subtrees, we have to do this
9241 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9245 /* Check the return type. */
9246 WALK_SUBTREE (TREE_TYPE (t
));
9248 /* Check the parameter types. Since default arguments are not
9249 instantiated until they are needed, the TYPE_ARG_TYPES may
9250 contain expressions that involve template parameters. But,
9251 no-one should be looking at them yet. And, once they're
9252 instantiated, they don't contain template parameters, so
9253 there's no point in looking at them then, either. */
9257 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9258 WALK_SUBTREE (TREE_VALUE (parm
));
9260 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9261 want walk_tree walking into them itself. */
9265 if (flag_noexcept_type
)
9267 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9269 WALK_SUBTREE (TREE_PURPOSE (spec
));
9274 case UNDERLYING_TYPE
:
9275 if (pfd
->include_nondeduced_p
9276 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9278 pfd
->include_nondeduced_p
,
9280 return error_mark_node
;
9285 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9286 WALK_SUBTREE (DECL_TI_ARGS (t
));
9291 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9292 WALK_SUBTREE (DECL_INITIAL (t
));
9293 if (DECL_CONTEXT (t
)
9294 && pfd
->include_nondeduced_p
)
9295 WALK_SUBTREE (DECL_CONTEXT (t
));
9298 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9299 /* Record template parameters such as `T' inside `TT<T>'. */
9300 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9303 case TEMPLATE_TEMPLATE_PARM
:
9304 case TEMPLATE_TYPE_PARM
:
9305 case TEMPLATE_PARM_INDEX
:
9306 if (fn
&& (*fn
)(t
, data
))
9313 /* A template template parameter is encountered. */
9314 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9315 WALK_SUBTREE (TREE_TYPE (t
));
9317 /* Already substituted template template parameter */
9322 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9323 partial instantiation. */
9324 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9328 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9329 && pfd
->include_nondeduced_p
)
9330 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9335 /* If there's no type, then this thing must be some expression
9336 involving template parameters. */
9337 if (!fn
&& !TREE_TYPE (t
))
9338 return error_mark_node
;
9343 case IMPLICIT_CONV_EXPR
:
9344 case REINTERPRET_CAST_EXPR
:
9345 case CONST_CAST_EXPR
:
9346 case STATIC_CAST_EXPR
:
9347 case DYNAMIC_CAST_EXPR
:
9351 case PSEUDO_DTOR_EXPR
:
9353 return error_mark_node
;
9362 /* We didn't find any template parameters we liked. */
9367 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9368 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9369 call FN with the parameter and the DATA.
9370 If FN returns nonzero, the iteration is terminated, and
9371 for_each_template_parm returns 1. Otherwise, the iteration
9372 continues. If FN never returns a nonzero value, the value
9373 returned by for_each_template_parm is 0. If FN is NULL, it is
9374 considered to be the function which always returns 1.
9376 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9377 parameters that occur in non-deduced contexts. When false, only
9378 visits those template parameters that can be deduced. */
9381 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9382 hash_set
<tree
> *visited
,
9383 bool include_nondeduced_p
,
9386 struct pair_fn_data pfd
;
9391 pfd
.any_fn
= any_fn
;
9393 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9395 /* Walk the tree. (Conceptually, we would like to walk without
9396 duplicates, but for_each_template_parm_r recursively calls
9397 for_each_template_parm, so we would need to reorganize a fair
9398 bit to use walk_tree_without_duplicates, so we keep our own
9401 pfd
.visited
= visited
;
9403 pfd
.visited
= new hash_set
<tree
>;
9404 result
= cp_walk_tree (&t
,
9405 for_each_template_parm_r
,
9419 /* Returns true if T depends on any template parameter. */
9422 uses_template_parms (tree t
)
9428 int saved_processing_template_decl
;
9430 saved_processing_template_decl
= processing_template_decl
;
9431 if (!saved_processing_template_decl
)
9432 processing_template_decl
= 1;
9434 dependent_p
= dependent_type_p (t
);
9435 else if (TREE_CODE (t
) == TREE_VEC
)
9436 dependent_p
= any_dependent_template_arguments_p (t
);
9437 else if (TREE_CODE (t
) == TREE_LIST
)
9438 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9439 || uses_template_parms (TREE_CHAIN (t
)));
9440 else if (TREE_CODE (t
) == TYPE_DECL
)
9441 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9444 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9445 || TREE_CODE (t
) == OVERLOAD
9448 || TREE_CODE (t
) == TRAIT_EXPR
9449 || TREE_CODE (t
) == CONSTRUCTOR
9450 || CONSTANT_CLASS_P (t
))
9451 dependent_p
= (type_dependent_expression_p (t
)
9452 || value_dependent_expression_p (t
));
9455 gcc_assert (t
== error_mark_node
);
9456 dependent_p
= false;
9459 processing_template_decl
= saved_processing_template_decl
;
9464 /* Returns true iff current_function_decl is an incompletely instantiated
9465 template. Useful instead of processing_template_decl because the latter
9466 is set to 0 during instantiate_non_dependent_expr. */
9469 in_template_function (void)
9471 tree fn
= current_function_decl
;
9473 ++processing_template_decl
;
9474 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9475 && DECL_TEMPLATE_INFO (fn
)
9476 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9477 --processing_template_decl
;
9481 /* Returns true if T depends on any template parameter with level LEVEL. */
9484 uses_template_parms_level (tree t
, int level
)
9486 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9487 /*include_nondeduced_p=*/true);
9490 /* Returns true if the signature of DECL depends on any template parameter from
9491 its enclosing class. */
9494 uses_outer_template_parms (tree decl
)
9496 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9499 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9500 &depth
, NULL
, /*include_nondeduced_p=*/true))
9502 if (PRIMARY_TEMPLATE_P (decl
)
9503 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9504 (DECL_TEMPLATE_PARMS (decl
)),
9505 template_parm_outer_level
,
9506 &depth
, NULL
, /*include_nondeduced_p=*/true))
9508 tree ci
= get_constraints (decl
);
9510 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9511 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9512 &depth
, NULL
, /*nondeduced*/true))
9517 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9518 ill-formed translation unit, i.e. a variable or function that isn't
9519 usable in a constant expression. */
9522 neglectable_inst_p (tree d
)
9525 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9526 : decl_maybe_constant_var_p (d
)));
9529 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9530 neglectable and instantiated from within an erroneous instantiation. */
9533 limit_bad_template_recursion (tree decl
)
9535 struct tinst_level
*lev
= current_tinst_level
;
9536 int errs
= errorcount
+ sorrycount
;
9537 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9540 for (; lev
; lev
= lev
->next
)
9541 if (neglectable_inst_p (lev
->decl
))
9544 return (lev
&& errs
> lev
->errors
);
9547 static int tinst_depth
;
9548 extern int max_tinst_depth
;
9551 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9553 /* We're starting to instantiate D; record the template instantiation context
9554 for diagnostics and to restore it later. */
9557 push_tinst_level (tree d
)
9559 return push_tinst_level_loc (d
, input_location
);
9562 /* We're starting to instantiate D; record the template instantiation context
9563 at LOC for diagnostics and to restore it later. */
9566 push_tinst_level_loc (tree d
, location_t loc
)
9568 struct tinst_level
*new_level
;
9570 if (tinst_depth
>= max_tinst_depth
)
9572 /* Tell error.c not to try to instantiate any templates. */
9574 fatal_error (input_location
,
9575 "template instantiation depth exceeds maximum of %d"
9576 " (use -ftemplate-depth= to increase the maximum)",
9581 /* If the current instantiation caused problems, don't let it instantiate
9582 anything else. Do allow deduction substitution and decls usable in
9583 constant expressions. */
9584 if (limit_bad_template_recursion (d
))
9587 /* When not -quiet, dump template instantiations other than functions, since
9588 announce_function will take care of those. */
9590 && TREE_CODE (d
) != TREE_LIST
9591 && TREE_CODE (d
) != FUNCTION_DECL
)
9592 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9594 new_level
= ggc_alloc
<tinst_level
> ();
9595 new_level
->decl
= d
;
9596 new_level
->locus
= loc
;
9597 new_level
->errors
= errorcount
+sorrycount
;
9598 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9599 new_level
->next
= current_tinst_level
;
9600 current_tinst_level
= new_level
;
9603 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9604 depth_reached
= tinst_depth
;
9609 /* We're done instantiating this template; return to the instantiation
9613 pop_tinst_level (void)
9615 /* Restore the filename and line number stashed away when we started
9616 this instantiation. */
9617 input_location
= current_tinst_level
->locus
;
9618 current_tinst_level
= current_tinst_level
->next
;
9622 /* We're instantiating a deferred template; restore the template
9623 instantiation context in which the instantiation was requested, which
9624 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9627 reopen_tinst_level (struct tinst_level
*level
)
9629 struct tinst_level
*t
;
9632 for (t
= level
; t
; t
= t
->next
)
9635 current_tinst_level
= level
;
9637 if (current_tinst_level
)
9638 current_tinst_level
->errors
= errorcount
+sorrycount
;
9642 /* Returns the TINST_LEVEL which gives the original instantiation
9645 struct tinst_level
*
9646 outermost_tinst_level (void)
9648 struct tinst_level
*level
= current_tinst_level
;
9651 level
= level
->next
;
9655 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9656 vector of template arguments, as for tsubst.
9658 Returns an appropriate tsubst'd friend declaration. */
9661 tsubst_friend_function (tree decl
, tree args
)
9665 if (TREE_CODE (decl
) == FUNCTION_DECL
9666 && DECL_TEMPLATE_INSTANTIATION (decl
)
9667 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9668 /* This was a friend declared with an explicit template
9669 argument list, e.g.:
9673 to indicate that f was a template instantiation, not a new
9674 function declaration. Now, we have to figure out what
9675 instantiation of what template. */
9677 tree template_id
, arglist
, fns
;
9680 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9682 /* Friend functions are looked up in the containing namespace scope.
9683 We must enter that scope, to avoid finding member functions of the
9684 current class with same name. */
9685 push_nested_namespace (ns
);
9686 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9687 tf_warning_or_error
, NULL_TREE
,
9688 /*integral_constant_expression_p=*/false);
9689 pop_nested_namespace (ns
);
9690 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9691 tf_warning_or_error
, NULL_TREE
);
9692 template_id
= lookup_template_function (fns
, arglist
);
9694 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9695 tmpl
= determine_specialization (template_id
, new_friend
,
9697 /*need_member_template=*/0,
9698 TREE_VEC_LENGTH (args
),
9700 return instantiate_template (tmpl
, new_args
, tf_error
);
9703 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9705 /* The NEW_FRIEND will look like an instantiation, to the
9706 compiler, but is not an instantiation from the point of view of
9707 the language. For example, we might have had:
9709 template <class T> struct S {
9710 template <class U> friend void f(T, U);
9713 Then, in S<int>, template <class U> void f(int, U) is not an
9714 instantiation of anything. */
9715 if (new_friend
== error_mark_node
)
9716 return error_mark_node
;
9718 DECL_USE_TEMPLATE (new_friend
) = 0;
9719 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9721 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9722 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9723 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9726 /* The mangled name for the NEW_FRIEND is incorrect. The function
9727 is not a template instantiation and should not be mangled like
9728 one. Therefore, we forget the mangling here; we'll recompute it
9729 later if we need it. */
9730 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9732 SET_DECL_RTL (new_friend
, NULL
);
9733 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9736 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9739 tree new_friend_template_info
;
9740 tree new_friend_result_template_info
;
9742 int new_friend_is_defn
;
9744 /* We must save some information from NEW_FRIEND before calling
9745 duplicate decls since that function will free NEW_FRIEND if
9747 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9748 new_friend_is_defn
=
9749 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9750 (template_for_substitution (new_friend
)))
9752 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9754 /* This declaration is a `primary' template. */
9755 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9757 new_friend_result_template_info
9758 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9761 new_friend_result_template_info
= NULL_TREE
;
9763 /* Inside pushdecl_namespace_level, we will push into the
9764 current namespace. However, the friend function should go
9765 into the namespace of the template. */
9766 ns
= decl_namespace_context (new_friend
);
9767 push_nested_namespace (ns
);
9768 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9769 pop_nested_namespace (ns
);
9771 if (old_decl
== error_mark_node
)
9772 return error_mark_node
;
9774 if (old_decl
!= new_friend
)
9776 /* This new friend declaration matched an existing
9777 declaration. For example, given:
9779 template <class T> void f(T);
9780 template <class U> class C {
9781 template <class T> friend void f(T) {}
9784 the friend declaration actually provides the definition
9785 of `f', once C has been instantiated for some type. So,
9786 old_decl will be the out-of-class template declaration,
9787 while new_friend is the in-class definition.
9789 But, if `f' was called before this point, the
9790 instantiation of `f' will have DECL_TI_ARGS corresponding
9791 to `T' but not to `U', references to which might appear
9792 in the definition of `f'. Previously, the most general
9793 template for an instantiation of `f' was the out-of-class
9794 version; now it is the in-class version. Therefore, we
9795 run through all specialization of `f', adding to their
9796 DECL_TI_ARGS appropriately. In particular, they need a
9797 new set of outer arguments, corresponding to the
9798 arguments for this class instantiation.
9800 The same situation can arise with something like this:
9803 template <class T> class C {
9807 when `C<int>' is instantiated. Now, `f(int)' is defined
9810 if (!new_friend_is_defn
)
9811 /* On the other hand, if the in-class declaration does
9812 *not* provide a definition, then we don't want to alter
9813 existing definitions. We can just leave everything
9818 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9819 tree new_args
= TI_ARGS (new_friend_template_info
);
9821 /* Overwrite whatever template info was there before, if
9822 any, with the new template information pertaining to
9824 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9826 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9828 /* We should have called reregister_specialization in
9830 gcc_assert (retrieve_specialization (new_template
,
9834 /* Instantiate it if the global has already been used. */
9835 if (DECL_ODR_USED (old_decl
))
9836 instantiate_decl (old_decl
, /*defer_ok=*/true,
9837 /*expl_inst_class_mem_p=*/false);
9843 /* Indicate that the old function template is a partial
9845 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9846 = new_friend_result_template_info
;
9848 gcc_assert (new_template
9849 == most_general_template (new_template
));
9850 gcc_assert (new_template
!= old_decl
);
9852 /* Reassign any specializations already in the hash table
9853 to the new more general template, and add the
9854 additional template args. */
9855 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9859 tree spec
= TREE_VALUE (t
);
9862 elt
.tmpl
= old_decl
;
9863 elt
.args
= DECL_TI_ARGS (spec
);
9864 elt
.spec
= NULL_TREE
;
9866 decl_specializations
->remove_elt (&elt
);
9869 = add_outermost_template_args (new_args
,
9870 DECL_TI_ARGS (spec
));
9872 register_specialization
9873 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9876 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9880 /* The information from NEW_FRIEND has been merged into OLD_DECL
9881 by duplicate_decls. */
9882 new_friend
= old_decl
;
9887 tree context
= DECL_CONTEXT (new_friend
);
9891 template <class T> class C {
9892 template <class U> friend void C1<U>::f (); // case 1
9893 friend void C2<T>::f (); // case 2
9895 we only need to make sure CONTEXT is a complete type for
9896 case 2. To distinguish between the two cases, we note that
9897 CONTEXT of case 1 remains dependent type after tsubst while
9898 this isn't true for case 2. */
9899 ++processing_template_decl
;
9900 dependent_p
= dependent_type_p (context
);
9901 --processing_template_decl
;
9904 && !complete_type_or_else (context
, NULL_TREE
))
9905 return error_mark_node
;
9907 if (COMPLETE_TYPE_P (context
))
9909 tree fn
= new_friend
;
9910 /* do_friend adds the TEMPLATE_DECL for any member friend
9911 template even if it isn't a member template, i.e.
9912 template <class T> friend A<T>::f();
9913 Look through it in that case. */
9914 if (TREE_CODE (fn
) == TEMPLATE_DECL
9915 && !PRIMARY_TEMPLATE_P (fn
))
9916 fn
= DECL_TEMPLATE_RESULT (fn
);
9917 /* Check to see that the declaration is really present, and,
9918 possibly obtain an improved declaration. */
9919 fn
= check_classfn (context
, fn
, NULL_TREE
);
9929 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9930 template arguments, as for tsubst.
9932 Returns an appropriate tsubst'd friend type or error_mark_node on
9936 tsubst_friend_class (tree friend_tmpl
, tree args
)
9942 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9944 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9945 return TREE_TYPE (t
);
9948 context
= CP_DECL_CONTEXT (friend_tmpl
);
9950 if (context
!= global_namespace
)
9952 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9953 push_nested_namespace (context
);
9955 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9958 /* Look for a class template declaration. We look for hidden names
9959 because two friend declarations of the same template are the
9960 same. For example, in:
9963 template <typename> friend class F;
9965 template <typename> struct B {
9966 template <typename> friend class F;
9969 both F templates are the same. */
9970 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9971 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9973 /* But, if we don't find one, it might be because we're in a
9974 situation like this:
9982 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9983 for `S<int>', not the TEMPLATE_DECL. */
9984 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9986 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9987 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9990 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9992 /* The friend template has already been declared. Just
9993 check to see that the declarations match, and install any new
9994 default parameters. We must tsubst the default parameters,
9995 of course. We only need the innermost template parameters
9996 because that is all that redeclare_class_template will look
9998 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9999 > TMPL_ARGS_DEPTH (args
))
10002 location_t saved_input_location
;
10003 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
10004 args
, tf_warning_or_error
);
10006 saved_input_location
= input_location
;
10007 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
10008 tree cons
= get_constraints (tmpl
);
10009 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
10010 input_location
= saved_input_location
;
10014 friend_type
= TREE_TYPE (tmpl
);
10018 /* The friend template has not already been declared. In this
10019 case, the instantiation of the template class will cause the
10020 injection of this template into the global scope. */
10021 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10022 if (tmpl
== error_mark_node
)
10023 return error_mark_node
;
10025 /* The new TMPL is not an instantiation of anything, so we
10026 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10027 the new type because that is supposed to be the corresponding
10028 template decl, i.e., TMPL. */
10029 DECL_USE_TEMPLATE (tmpl
) = 0;
10030 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10031 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10032 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10033 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10035 /* Inject this template into the global scope. */
10036 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
, true));
10039 if (context
!= global_namespace
)
10041 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10042 pop_nested_namespace (context
);
10044 pop_nested_class ();
10047 return friend_type
;
10050 /* Returns zero if TYPE cannot be completed later due to circularity.
10051 Otherwise returns one. */
10054 can_complete_type_without_circularity (tree type
)
10056 if (type
== NULL_TREE
|| type
== error_mark_node
)
10058 else if (COMPLETE_TYPE_P (type
))
10060 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10061 return can_complete_type_without_circularity (TREE_TYPE (type
));
10062 else if (CLASS_TYPE_P (type
)
10063 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10069 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10070 tsubst_flags_t
, tree
);
10072 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10073 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10076 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10077 tsubst_flags_t complain
, tree in_decl
)
10079 gcc_assert (ATTR_IS_DEPENDENT (t
));
10081 tree val
= TREE_VALUE (t
);
10082 if (val
== NULL_TREE
)
10083 /* Nothing to do. */;
10084 else if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
10085 && is_attribute_p ("omp declare simd",
10086 get_attribute_name (t
)))
10088 tree clauses
= TREE_VALUE (val
);
10089 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10090 complain
, in_decl
);
10091 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10092 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10093 tree parms
= DECL_ARGUMENTS (*decl_p
);
10095 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10097 val
= build_tree_list (NULL_TREE
, clauses
);
10101 /* If the first attribute argument is an identifier, don't
10102 pass it through tsubst. Attributes like mode, format,
10103 cleanup and several target specific attributes expect it
10105 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10108 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10109 /*integral_constant_expression_p=*/false);
10110 if (chain
!= TREE_CHAIN (val
))
10111 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10113 else if (PACK_EXPANSION_P (val
))
10115 /* An attribute pack expansion. */
10116 tree purp
= TREE_PURPOSE (t
);
10117 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10118 if (pack
== error_mark_node
)
10119 return error_mark_node
;
10120 int len
= TREE_VEC_LENGTH (pack
);
10121 tree list
= NULL_TREE
;
10123 for (int i
= 0; i
< len
; ++i
)
10125 tree elt
= TREE_VEC_ELT (pack
, i
);
10126 *q
= build_tree_list (purp
, elt
);
10127 q
= &TREE_CHAIN (*q
);
10132 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10133 /*integral_constant_expression_p=*/false);
10135 if (val
!= TREE_VALUE (t
))
10136 return build_tree_list (TREE_PURPOSE (t
), val
);
10140 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10141 unchanged or a new TREE_LIST chain. */
10144 tsubst_attributes (tree attributes
, tree args
,
10145 tsubst_flags_t complain
, tree in_decl
)
10147 tree last_dep
= NULL_TREE
;
10149 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10150 if (ATTR_IS_DEPENDENT (t
))
10153 attributes
= copy_list (attributes
);
10158 for (tree
*p
= &attributes
; *p
; )
10161 if (ATTR_IS_DEPENDENT (t
))
10163 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10168 p
= &TREE_CHAIN (*p
);
10170 *p
= TREE_CHAIN (t
);
10174 p
= &TREE_CHAIN (*p
);
10180 /* Apply any attributes which had to be deferred until instantiation
10181 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10182 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10185 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10186 tree args
, tsubst_flags_t complain
, tree in_decl
)
10188 tree last_dep
= NULL_TREE
;
10192 if (attributes
== NULL_TREE
)
10195 if (DECL_P (*decl_p
))
10197 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10199 p
= &DECL_ATTRIBUTES (*decl_p
);
10200 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10201 to our attributes parameter. */
10202 gcc_assert (*p
== attributes
);
10206 p
= &TYPE_ATTRIBUTES (*decl_p
);
10207 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10208 lookup_template_class_1, and should be preserved. */
10209 gcc_assert (*p
!= attributes
);
10211 p
= &TREE_CHAIN (*p
);
10214 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10215 if (ATTR_IS_DEPENDENT (t
))
10218 attributes
= copy_list (attributes
);
10225 tree late_attrs
= NULL_TREE
;
10226 tree
*q
= &late_attrs
;
10231 if (ATTR_IS_DEPENDENT (t
))
10233 *p
= TREE_CHAIN (t
);
10234 TREE_CHAIN (t
) = NULL_TREE
;
10235 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10237 q
= &TREE_CHAIN (*q
);
10241 p
= &TREE_CHAIN (t
);
10244 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10248 /* Perform (or defer) access check for typedefs that were referenced
10249 from within the template TMPL code.
10250 This is a subroutine of instantiate_decl and instantiate_class_template.
10251 TMPL is the template to consider and TARGS is the list of arguments of
10255 perform_typedefs_access_check (tree tmpl
, tree targs
)
10257 location_t saved_location
;
10259 qualified_typedef_usage_t
*iter
;
10262 || (!CLASS_TYPE_P (tmpl
)
10263 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10266 saved_location
= input_location
;
10267 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10269 tree type_decl
= iter
->typedef_decl
;
10270 tree type_scope
= iter
->context
;
10272 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10275 if (uses_template_parms (type_decl
))
10276 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10277 if (uses_template_parms (type_scope
))
10278 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10280 /* Make access check error messages point to the location
10281 of the use of the typedef. */
10282 input_location
= iter
->locus
;
10283 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10284 type_decl
, type_decl
,
10285 tf_warning_or_error
);
10287 input_location
= saved_location
;
10291 instantiate_class_template_1 (tree type
)
10293 tree templ
, args
, pattern
, t
, member
;
10297 unsigned int saved_maximum_field_alignment
;
10300 if (type
== error_mark_node
)
10301 return error_mark_node
;
10303 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10304 || uses_template_parms (type
))
10307 /* Figure out which template is being instantiated. */
10308 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10309 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10311 /* Determine what specialization of the original template to
10313 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10314 if (t
== error_mark_node
)
10316 TYPE_BEING_DEFINED (type
) = 1;
10317 return error_mark_node
;
10321 /* This TYPE is actually an instantiation of a partial
10322 specialization. We replace the innermost set of ARGS with
10323 the arguments appropriate for substitution. For example,
10326 template <class T> struct S {};
10327 template <class T> struct S<T*> {};
10329 and supposing that we are instantiating S<int*>, ARGS will
10330 presently be {int*} -- but we need {int}. */
10331 pattern
= TREE_TYPE (t
);
10332 args
= TREE_PURPOSE (t
);
10336 pattern
= TREE_TYPE (templ
);
10337 args
= CLASSTYPE_TI_ARGS (type
);
10340 /* If the template we're instantiating is incomplete, then clearly
10341 there's nothing we can do. */
10342 if (!COMPLETE_TYPE_P (pattern
))
10345 /* If we've recursively instantiated too many templates, stop. */
10346 if (! push_tinst_level (type
))
10349 /* Now we're really doing the instantiation. Mark the type as in
10350 the process of being defined. */
10351 TYPE_BEING_DEFINED (type
) = 1;
10353 /* We may be in the middle of deferred access check. Disable
10355 push_deferring_access_checks (dk_no_deferred
);
10357 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10358 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10360 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10361 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10362 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10363 fn_context
= error_mark_node
;
10365 push_to_top_level ();
10368 cp_unevaluated_operand
= 0;
10369 c_inhibit_evaluation_warnings
= 0;
10371 /* Use #pragma pack from the template context. */
10372 saved_maximum_field_alignment
= maximum_field_alignment
;
10373 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10375 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10377 /* Set the input location to the most specialized template definition.
10378 This is needed if tsubsting causes an error. */
10379 typedecl
= TYPE_MAIN_DECL (pattern
);
10380 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10381 DECL_SOURCE_LOCATION (typedecl
);
10383 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10384 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10385 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10386 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10387 if (ANON_AGGR_TYPE_P (pattern
))
10388 SET_ANON_AGGR_TYPE_P (type
);
10389 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10391 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10392 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10393 /* Adjust visibility for template arguments. */
10394 determine_visibility (TYPE_MAIN_DECL (type
));
10396 if (CLASS_TYPE_P (type
))
10397 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10399 pbinfo
= TYPE_BINFO (pattern
);
10401 /* We should never instantiate a nested class before its enclosing
10402 class; we need to look up the nested class by name before we can
10403 instantiate it, and that lookup should instantiate the enclosing
10405 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10406 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10408 base_list
= NULL_TREE
;
10409 if (BINFO_N_BASE_BINFOS (pbinfo
))
10415 /* We must enter the scope containing the type, as that is where
10416 the accessibility of types named in dependent bases are
10418 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10420 /* Substitute into each of the bases to determine the actual
10422 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10425 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10426 tree expanded_bases
= NULL_TREE
;
10429 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10432 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10433 args
, tf_error
, NULL_TREE
);
10434 if (expanded_bases
== error_mark_node
)
10437 len
= TREE_VEC_LENGTH (expanded_bases
);
10440 for (idx
= 0; idx
< len
; idx
++)
10442 if (expanded_bases
)
10443 /* Extract the already-expanded base class. */
10444 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10446 /* Substitute to figure out the base class. */
10447 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10450 if (base
== error_mark_node
)
10453 base_list
= tree_cons (access
, base
, base_list
);
10454 if (BINFO_VIRTUAL_P (pbase_binfo
))
10455 TREE_TYPE (base_list
) = integer_type_node
;
10459 /* The list is now in reverse order; correct that. */
10460 base_list
= nreverse (base_list
);
10463 pop_scope (pushed_scope
);
10465 /* Now call xref_basetypes to set up all the base-class
10467 xref_basetypes (type
, base_list
);
10469 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10470 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10471 args
, tf_error
, NULL_TREE
);
10472 fixup_attribute_variants (type
);
10474 /* Now that our base classes are set up, enter the scope of the
10475 class, so that name lookups into base classes, etc. will work
10476 correctly. This is precisely analogous to what we do in
10477 begin_class_definition when defining an ordinary non-template
10478 class, except we also need to push the enclosing classes. */
10479 push_nested_class (type
);
10481 /* Now members are processed in the order of declaration. */
10482 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10483 member
; member
= TREE_CHAIN (member
))
10485 tree t
= TREE_VALUE (member
);
10487 if (TREE_PURPOSE (member
))
10491 /* Build new CLASSTYPE_NESTED_UTDS. */
10494 bool class_template_p
;
10496 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10497 && TYPE_LANG_SPECIFIC (t
)
10498 && CLASSTYPE_IS_TEMPLATE (t
));
10499 /* If the member is a class template, then -- even after
10500 substitution -- there may be dependent types in the
10501 template argument list for the class. We increment
10502 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10503 that function will assume that no types are dependent
10504 when outside of a template. */
10505 if (class_template_p
)
10506 ++processing_template_decl
;
10507 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10508 if (class_template_p
)
10509 --processing_template_decl
;
10510 if (newtag
== error_mark_node
)
10513 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10515 tree name
= TYPE_IDENTIFIER (t
);
10517 if (class_template_p
)
10518 /* Unfortunately, lookup_template_class sets
10519 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10520 instantiation (i.e., for the type of a member
10521 template class nested within a template class.)
10522 This behavior is required for
10523 maybe_process_partial_specialization to work
10524 correctly, but is not accurate in this case;
10525 the TAG is not an instantiation of anything.
10526 (The corresponding TEMPLATE_DECL is an
10527 instantiation, but the TYPE is not.) */
10528 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10530 /* Now, we call pushtag to put this NEWTAG into the scope of
10531 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10532 pushtag calling push_template_decl. We don't have to do
10533 this for enums because it will already have been done in
10536 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10537 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10540 else if (DECL_DECLARES_FUNCTION_P (t
))
10542 /* Build new TYPE_METHODS. */
10545 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10546 ++processing_template_decl
;
10547 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10548 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10549 --processing_template_decl
;
10550 set_current_access_from_decl (r
);
10551 finish_member_declaration (r
);
10552 /* Instantiate members marked with attribute used. */
10553 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10555 if (TREE_CODE (r
) == FUNCTION_DECL
10556 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10557 cp_check_omp_declare_reduction (r
);
10559 else if (DECL_CLASS_TEMPLATE_P (t
)
10560 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10561 /* A closure type for a lambda in a default argument for a
10562 member template. Ignore it; it will be instantiated with
10563 the default argument. */;
10566 /* Build new TYPE_FIELDS. */
10567 if (TREE_CODE (t
) == STATIC_ASSERT
)
10571 ++c_inhibit_evaluation_warnings
;
10573 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10574 tf_warning_or_error
, NULL_TREE
,
10575 /*integral_constant_expression_p=*/true);
10576 --c_inhibit_evaluation_warnings
;
10578 finish_static_assert (condition
,
10579 STATIC_ASSERT_MESSAGE (t
),
10580 STATIC_ASSERT_SOURCE_LOCATION (t
),
10581 /*member_p=*/true);
10583 else if (TREE_CODE (t
) != CONST_DECL
)
10586 tree vec
= NULL_TREE
;
10589 /* The file and line for this declaration, to
10590 assist in error message reporting. Since we
10591 called push_tinst_level above, we don't need to
10593 input_location
= DECL_SOURCE_LOCATION (t
);
10595 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10596 ++processing_template_decl
;
10597 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10598 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10599 --processing_template_decl
;
10601 if (TREE_CODE (r
) == TREE_VEC
)
10603 /* A capture pack became multiple fields. */
10605 len
= TREE_VEC_LENGTH (vec
);
10608 for (int i
= 0; i
< len
; ++i
)
10611 r
= TREE_VEC_ELT (vec
, i
);
10616 [t]he initialization (and any associated
10617 side-effects) of a static data member does
10618 not occur unless the static data member is
10619 itself used in a way that requires the
10620 definition of the static data member to
10623 Therefore, we do not substitute into the
10624 initialized for the static data member here. */
10625 finish_static_data_member_decl
10627 /*init=*/NULL_TREE
,
10628 /*init_const_expr_p=*/false,
10629 /*asmspec_tree=*/NULL_TREE
,
10631 /* Instantiate members marked with attribute used. */
10632 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10635 else if (TREE_CODE (r
) == FIELD_DECL
)
10637 /* Determine whether R has a valid type and can be
10638 completed later. If R is invalid, then its type
10639 is replaced by error_mark_node. */
10640 tree rtype
= TREE_TYPE (r
);
10641 if (can_complete_type_without_circularity (rtype
))
10642 complete_type (rtype
);
10644 if (!complete_or_array_type_p (rtype
))
10646 /* If R's type couldn't be completed and
10647 it isn't a flexible array member (whose
10648 type is incomplete by definition) give
10650 cxx_incomplete_type_error (r
, rtype
);
10651 TREE_TYPE (r
) = error_mark_node
;
10655 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10656 such a thing will already have been added to the field
10657 list by tsubst_enum in finish_member_declaration in the
10658 CLASSTYPE_NESTED_UTDS case above. */
10659 if (!(TREE_CODE (r
) == TYPE_DECL
10660 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10661 && DECL_ARTIFICIAL (r
)))
10663 set_current_access_from_decl (r
);
10664 finish_member_declaration (r
);
10672 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10673 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10675 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10677 tree friend_type
= t
;
10678 bool adjust_processing_template_decl
= false;
10680 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10682 /* template <class T> friend class C; */
10683 friend_type
= tsubst_friend_class (friend_type
, args
);
10684 adjust_processing_template_decl
= true;
10686 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10688 /* template <class T> friend class C::D; */
10689 friend_type
= tsubst (friend_type
, args
,
10690 tf_warning_or_error
, NULL_TREE
);
10691 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10692 friend_type
= TREE_TYPE (friend_type
);
10693 adjust_processing_template_decl
= true;
10695 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10696 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10698 /* This could be either
10702 when dependent_type_p is false or
10704 template <class U> friend class T::C;
10707 /* Bump processing_template_decl in case this is something like
10708 template <class T> friend struct A<T>::B. */
10709 ++processing_template_decl
;
10710 friend_type
= tsubst (friend_type
, args
,
10711 tf_warning_or_error
, NULL_TREE
);
10712 if (dependent_type_p (friend_type
))
10713 adjust_processing_template_decl
= true;
10714 --processing_template_decl
;
10716 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10717 && TYPE_HIDDEN_P (friend_type
))
10721 where C hasn't been declared yet. Let's lookup name
10722 from namespace scope directly, bypassing any name that
10723 come from dependent base class. */
10724 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10726 /* The call to xref_tag_from_type does injection for friend
10728 push_nested_namespace (ns
);
10730 xref_tag_from_type (friend_type
, NULL_TREE
,
10731 /*tag_scope=*/ts_current
);
10732 pop_nested_namespace (ns
);
10734 else if (uses_template_parms (friend_type
))
10735 /* friend class C<T>; */
10736 friend_type
= tsubst (friend_type
, args
,
10737 tf_warning_or_error
, NULL_TREE
);
10742 where C is already declared or
10744 friend class C<int>;
10746 We don't have to do anything in these cases. */
10748 if (adjust_processing_template_decl
)
10749 /* Trick make_friend_class into realizing that the friend
10750 we're adding is a template, not an ordinary class. It's
10751 important that we use make_friend_class since it will
10752 perform some error-checking and output cross-reference
10754 ++processing_template_decl
;
10756 if (friend_type
!= error_mark_node
)
10757 make_friend_class (type
, friend_type
, /*complain=*/false);
10759 if (adjust_processing_template_decl
)
10760 --processing_template_decl
;
10764 /* Build new DECL_FRIENDLIST. */
10767 /* The file and line for this declaration, to
10768 assist in error message reporting. Since we
10769 called push_tinst_level above, we don't need to
10771 input_location
= DECL_SOURCE_LOCATION (t
);
10773 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10775 ++processing_template_decl
;
10776 push_deferring_access_checks (dk_no_check
);
10779 r
= tsubst_friend_function (t
, args
);
10780 add_friend (type
, r
, /*complain=*/false);
10781 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10783 pop_deferring_access_checks ();
10784 --processing_template_decl
;
10792 /* Restore these before substituting into the lambda capture
10794 cp_unevaluated_operand
= saved_unevaluated_operand
;
10795 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10798 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10800 tree decl
= lambda_function (type
);
10803 if (cxx_dialect
>= cxx1z
)
10804 CLASSTYPE_LITERAL_P (type
) = true;
10806 if (!DECL_TEMPLATE_INFO (decl
)
10807 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10809 /* Set function_depth to avoid garbage collection. */
10811 instantiate_decl (decl
, /*defer_ok=*/false, false);
10815 /* We need to instantiate the capture list from the template
10816 after we've instantiated the closure members, but before we
10817 consider adding the conversion op. Also keep any captures
10818 that may have been added during instantiation of the op(). */
10819 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10821 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10822 args
, tf_warning_or_error
, NULL_TREE
,
10825 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10826 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10828 maybe_add_lambda_conv_op (type
);
10831 gcc_assert (errorcount
);
10834 /* Set the file and line number information to whatever is given for
10835 the class itself. This puts error messages involving generated
10836 implicit functions at a predictable point, and the same point
10837 that would be used for non-template classes. */
10838 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10840 unreverse_member_declarations (type
);
10841 finish_struct_1 (type
);
10842 TYPE_BEING_DEFINED (type
) = 0;
10844 /* We don't instantiate default arguments for member functions. 14.7.1:
10846 The implicit instantiation of a class template specialization causes
10847 the implicit instantiation of the declarations, but not of the
10848 definitions or default arguments, of the class member functions,
10849 member classes, static data members and member templates.... */
10851 /* Some typedefs referenced from within the template code need to be access
10852 checked at template instantiation time, i.e now. These types were
10853 added to the template at parsing time. Let's get those and perform
10854 the access checks then. */
10855 perform_typedefs_access_check (pattern
, args
);
10856 perform_deferred_access_checks (tf_warning_or_error
);
10857 pop_nested_class ();
10858 maximum_field_alignment
= saved_maximum_field_alignment
;
10860 pop_from_top_level ();
10861 pop_deferring_access_checks ();
10862 pop_tinst_level ();
10864 /* The vtable for a template class can be emitted in any translation
10865 unit in which the class is instantiated. When there is no key
10866 method, however, finish_struct_1 will already have added TYPE to
10867 the keyed_classes. */
10868 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10869 vec_safe_push (keyed_classes
, type
);
10874 /* Wrapper for instantiate_class_template_1. */
10877 instantiate_class_template (tree type
)
10880 timevar_push (TV_TEMPLATE_INST
);
10881 ret
= instantiate_class_template_1 (type
);
10882 timevar_pop (TV_TEMPLATE_INST
);
10887 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10893 else if (TYPE_P (t
))
10894 r
= tsubst (t
, args
, complain
, in_decl
);
10897 if (!(complain
& tf_warning
))
10898 ++c_inhibit_evaluation_warnings
;
10899 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10900 /*integral_constant_expression_p=*/true);
10901 if (!(complain
& tf_warning
))
10902 --c_inhibit_evaluation_warnings
;
10907 /* Given a function parameter pack TMPL_PARM and some function parameters
10908 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10909 and set *SPEC_P to point at the next point in the list. */
10912 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10914 /* Collect all of the extra "packed" parameters into an
10917 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10918 tree spec_parm
= *spec_p
;
10921 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10923 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10926 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10927 parmvec
= make_tree_vec (len
);
10928 spec_parm
= *spec_p
;
10929 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10930 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10932 /* Build the argument packs. */
10933 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10934 *spec_p
= spec_parm
;
10939 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10940 NONTYPE_ARGUMENT_PACK. */
10943 make_fnparm_pack (tree spec_parm
)
10945 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10948 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10949 pack expansion with no extra args, 2 if it has extra args, or 0
10950 if it is not a pack expansion. */
10953 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10955 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10956 if (i
>= TREE_VEC_LENGTH (vec
))
10958 tree elt
= TREE_VEC_ELT (vec
, i
);
10960 /* A decl pack is itself an expansion. */
10961 elt
= TREE_TYPE (elt
);
10962 if (!PACK_EXPANSION_P (elt
))
10964 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
10970 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10973 make_argument_pack_select (tree arg_pack
, unsigned index
)
10975 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10977 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10978 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10983 /* This is a subroutine of tsubst_pack_expansion.
10985 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10986 mechanism to store the (non complete list of) arguments of the
10987 substitution and return a non substituted pack expansion, in order
10988 to wait for when we have enough arguments to really perform the
10992 use_pack_expansion_extra_args_p (tree parm_packs
,
10994 bool has_empty_arg
)
10996 /* If one pack has an expansion and another pack has a normal
10997 argument or if one pack has an empty argument and an another
10998 one hasn't then tsubst_pack_expansion cannot perform the
10999 substitution and need to fall back on the
11000 PACK_EXPANSION_EXTRA mechanism. */
11001 if (parm_packs
== NULL_TREE
)
11003 else if (has_empty_arg
)
11006 bool has_expansion_arg
= false;
11007 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
11009 bool has_non_expansion_arg
= false;
11010 for (tree parm_pack
= parm_packs
;
11012 parm_pack
= TREE_CHAIN (parm_pack
))
11014 tree arg
= TREE_VALUE (parm_pack
);
11016 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
11018 /* We can't substitute a pack expansion with extra args into
11022 has_expansion_arg
= true;
11024 has_non_expansion_arg
= true;
11027 if (has_expansion_arg
&& has_non_expansion_arg
)
11033 /* [temp.variadic]/6 says that:
11035 The instantiation of a pack expansion [...]
11036 produces a list E1,E2, ..., En, where N is the number of elements
11037 in the pack expansion parameters.
11039 This subroutine of tsubst_pack_expansion produces one of these Ei.
11041 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11042 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11043 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11044 INDEX is the index 'i' of the element Ei to produce. ARGS,
11045 COMPLAIN, and IN_DECL are the same parameters as for the
11046 tsubst_pack_expansion function.
11048 The function returns the resulting Ei upon successful completion,
11049 or error_mark_node.
11051 Note that this function possibly modifies the ARGS parameter, so
11052 it's the responsibility of the caller to restore it. */
11055 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11058 tree args
/* This parm gets
11060 tsubst_flags_t complain
,
11064 bool ith_elem_is_expansion
= false;
11066 /* For each parameter pack, change the substitution of the parameter
11067 pack to the ith argument in its argument pack, then expand the
11069 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11071 tree parm
= TREE_PURPOSE (pack
);
11072 tree arg_pack
= TREE_VALUE (pack
);
11073 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11075 ith_elem_is_expansion
|=
11076 argument_pack_element_is_expansion_p (arg_pack
, index
);
11078 /* Select the Ith argument from the pack. */
11079 if (TREE_CODE (parm
) == PARM_DECL
11080 || TREE_CODE (parm
) == FIELD_DECL
)
11084 aps
= make_argument_pack_select (arg_pack
, index
);
11085 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11086 return error_mark_node
;
11087 register_local_specialization (aps
, parm
);
11090 aps
= retrieve_local_specialization (parm
);
11095 template_parm_level_and_index (parm
, &level
, &idx
);
11099 aps
= make_argument_pack_select (arg_pack
, index
);
11100 /* Update the corresponding argument. */
11101 TMPL_ARG (args
, level
, idx
) = aps
;
11104 /* Re-use the ARGUMENT_PACK_SELECT. */
11105 aps
= TMPL_ARG (args
, level
, idx
);
11107 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11110 /* Substitute into the PATTERN with the (possibly altered)
11112 if (pattern
== in_decl
)
11113 /* Expanding a fixed parameter pack from
11114 coerce_template_parameter_pack. */
11115 t
= tsubst_decl (pattern
, args
, complain
);
11116 else if (pattern
== error_mark_node
)
11117 t
= error_mark_node
;
11118 else if (constraint_p (pattern
))
11120 if (processing_template_decl
)
11121 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11123 t
= (constraints_satisfied_p (pattern
, args
)
11124 ? boolean_true_node
: boolean_false_node
);
11126 else if (!TYPE_P (pattern
))
11127 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11128 /*integral_constant_expression_p=*/false);
11130 t
= tsubst (pattern
, args
, complain
, in_decl
);
11132 /* If the Ith argument pack element is a pack expansion, then
11133 the Ith element resulting from the substituting is going to
11134 be a pack expansion as well. */
11135 if (ith_elem_is_expansion
)
11136 t
= make_pack_expansion (t
);
11141 /* When the unexpanded parameter pack in a fold expression expands to an empty
11142 sequence, the value of the expression is as follows; the program is
11143 ill-formed if the operator is not listed in this table.
11150 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11152 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11153 if (!FOLD_EXPR_MODIFY_P (t
))
11156 case TRUTH_ANDIF_EXPR
:
11157 return boolean_true_node
;
11158 case TRUTH_ORIF_EXPR
:
11159 return boolean_false_node
;
11160 case COMPOUND_EXPR
:
11166 if (complain
& tf_error
)
11167 error_at (location_of (t
),
11168 "fold of empty expansion over %O", code
);
11169 return error_mark_node
;
11172 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11173 form an expression that combines the two terms using the
11177 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11179 tree op
= FOLD_EXPR_OP (t
);
11180 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11182 // Handle compound assignment operators.
11183 if (FOLD_EXPR_MODIFY_P (t
))
11184 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11188 case COMPOUND_EXPR
:
11189 return build_x_compound_expr (input_location
, left
, right
, complain
);
11191 return build_m_component_ref (left
, right
, complain
);
11193 return build_x_binary_op (input_location
, code
,
11194 left
, TREE_CODE (left
),
11195 right
, TREE_CODE (right
),
11201 /* Substitute ARGS into the pack of a fold expression T. */
11204 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11206 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11209 /* Substitute ARGS into the pack of a fold expression T. */
11212 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11214 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11217 /* Expand a PACK of arguments into a grouped as left fold.
11218 Given a pack containing elements A0, A1, ..., An and an
11219 operator @, this builds the expression:
11221 ((A0 @ A1) @ A2) ... @ An
11223 Note that PACK must not be empty.
11225 The operator is defined by the original fold expression T. */
11228 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11230 tree left
= TREE_VEC_ELT (pack
, 0);
11231 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11233 tree right
= TREE_VEC_ELT (pack
, i
);
11234 left
= fold_expression (t
, left
, right
, complain
);
11239 /* Substitute into a unary left fold expression. */
11242 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11245 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11246 if (pack
== error_mark_node
)
11247 return error_mark_node
;
11248 if (PACK_EXPANSION_P (pack
))
11250 tree r
= copy_node (t
);
11251 FOLD_EXPR_PACK (r
) = pack
;
11254 if (TREE_VEC_LENGTH (pack
) == 0)
11255 return expand_empty_fold (t
, complain
);
11257 return expand_left_fold (t
, pack
, complain
);
11260 /* Substitute into a binary left fold expression.
11262 Do ths by building a single (non-empty) vector of argumnts and
11263 building the expression from those elements. */
11266 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11269 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11270 if (pack
== error_mark_node
)
11271 return error_mark_node
;
11272 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11273 if (init
== error_mark_node
)
11274 return error_mark_node
;
11276 if (PACK_EXPANSION_P (pack
))
11278 tree r
= copy_node (t
);
11279 FOLD_EXPR_PACK (r
) = pack
;
11280 FOLD_EXPR_INIT (r
) = init
;
11284 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11285 TREE_VEC_ELT (vec
, 0) = init
;
11286 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11287 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11289 return expand_left_fold (t
, vec
, complain
);
11292 /* Expand a PACK of arguments into a grouped as right fold.
11293 Given a pack containing elementns A0, A1, ..., and an
11294 operator @, this builds the expression:
11296 A0@ ... (An-2 @ (An-1 @ An))
11298 Note that PACK must not be empty.
11300 The operator is defined by the original fold expression T. */
11303 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11305 // Build the expression.
11306 int n
= TREE_VEC_LENGTH (pack
);
11307 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11308 for (--n
; n
!= 0; --n
)
11310 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11311 right
= fold_expression (t
, left
, right
, complain
);
11316 /* Substitute into a unary right fold expression. */
11319 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11322 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11323 if (pack
== error_mark_node
)
11324 return error_mark_node
;
11325 if (PACK_EXPANSION_P (pack
))
11327 tree r
= copy_node (t
);
11328 FOLD_EXPR_PACK (r
) = pack
;
11331 if (TREE_VEC_LENGTH (pack
) == 0)
11332 return expand_empty_fold (t
, complain
);
11334 return expand_right_fold (t
, pack
, complain
);
11337 /* Substitute into a binary right fold expression.
11339 Do ths by building a single (non-empty) vector of arguments and
11340 building the expression from those elements. */
11343 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11346 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11347 if (pack
== error_mark_node
)
11348 return error_mark_node
;
11349 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11350 if (init
== error_mark_node
)
11351 return error_mark_node
;
11353 if (PACK_EXPANSION_P (pack
))
11355 tree r
= copy_node (t
);
11356 FOLD_EXPR_PACK (r
) = pack
;
11357 FOLD_EXPR_INIT (r
) = init
;
11361 int n
= TREE_VEC_LENGTH (pack
);
11362 tree vec
= make_tree_vec (n
+ 1);
11363 for (int i
= 0; i
< n
; ++i
)
11364 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11365 TREE_VEC_ELT (vec
, n
) = init
;
11367 return expand_right_fold (t
, vec
, complain
);
11371 /* Substitute ARGS into T, which is an pack expansion
11372 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11373 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11374 (if only a partial substitution could be performed) or
11375 ERROR_MARK_NODE if there was an error. */
11377 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11381 tree pack
, packs
= NULL_TREE
;
11382 bool unsubstituted_packs
= false;
11385 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11386 bool need_local_specializations
= false;
11389 gcc_assert (PACK_EXPANSION_P (t
));
11390 pattern
= PACK_EXPANSION_PATTERN (t
);
11392 /* Add in any args remembered from an earlier partial instantiation. */
11393 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11395 levels
= TMPL_ARGS_DEPTH (args
);
11397 /* Determine the argument packs that will instantiate the parameter
11398 packs used in the expansion expression. While we're at it,
11399 compute the number of arguments to be expanded and make sure it
11401 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11402 pack
= TREE_CHAIN (pack
))
11404 tree parm_pack
= TREE_VALUE (pack
);
11405 tree arg_pack
= NULL_TREE
;
11406 tree orig_arg
= NULL_TREE
;
11409 if (TREE_CODE (parm_pack
) == BASES
)
11411 gcc_assert (parm_pack
== pattern
);
11412 if (BASES_DIRECT (parm_pack
))
11413 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11414 args
, complain
, in_decl
, false));
11416 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11417 args
, complain
, in_decl
, false));
11419 else if (builtin_pack_call_p (parm_pack
))
11421 /* ??? Support use in other patterns. */
11422 gcc_assert (parm_pack
== pattern
);
11423 return expand_builtin_pack_call (parm_pack
, args
,
11424 complain
, in_decl
);
11426 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11428 /* We know we have correct local_specializations if this
11429 expansion is at function scope, or if we're dealing with a
11430 local parameter in a requires expression; for the latter,
11431 tsubst_requires_expr set it up appropriately. */
11432 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11433 arg_pack
= retrieve_local_specialization (parm_pack
);
11435 /* We can't rely on local_specializations for a parameter
11436 name used later in a function declaration (such as in a
11437 late-specified return type). Even if it exists, it might
11438 have the wrong value for a recursive call. */
11439 need_local_specializations
= true;
11443 /* This parameter pack was used in an unevaluated context. Just
11444 make a dummy decl, since it's only used for its type. */
11445 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11446 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11447 /* Partial instantiation of the parm_pack, we can't build
11448 up an argument pack yet. */
11449 arg_pack
= NULL_TREE
;
11451 arg_pack
= make_fnparm_pack (arg_pack
);
11454 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11455 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11459 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11461 if (level
<= levels
)
11462 arg_pack
= TMPL_ARG (args
, level
, idx
);
11465 orig_arg
= arg_pack
;
11466 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11467 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11469 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11470 /* This can only happen if we forget to expand an argument
11471 pack somewhere else. Just return an error, silently. */
11473 result
= make_tree_vec (1);
11474 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11481 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11483 /* Don't bother trying to do a partial substitution with
11484 incomplete packs; we'll try again after deduction. */
11485 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11490 else if (len
!= my_len
)
11492 if (!(complain
& tf_error
))
11493 /* Fail quietly. */;
11494 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11495 error ("mismatched argument pack lengths while expanding %qT",
11498 error ("mismatched argument pack lengths while expanding %qE",
11500 return error_mark_node
;
11503 /* Keep track of the parameter packs and their corresponding
11505 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11506 TREE_TYPE (packs
) = orig_arg
;
11510 /* We can't substitute for this parameter pack. We use a flag as
11511 well as the missing_level counter because function parameter
11512 packs don't have a level. */
11513 gcc_assert (processing_template_decl
);
11514 unsubstituted_packs
= true;
11518 /* If the expansion is just T..., return the matching argument pack, unless
11519 we need to call convert_from_reference on all the elements. This is an
11520 important optimization; see c++/68422. */
11521 if (!unsubstituted_packs
11522 && TREE_PURPOSE (packs
) == pattern
)
11524 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11525 /* Types need no adjustment, nor does sizeof..., and if we still have
11526 some pack expansion args we won't do anything yet. */
11527 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11528 || PACK_EXPANSION_SIZEOF_P (t
)
11529 || pack_expansion_args_count (args
))
11531 /* Also optimize expression pack expansions if we can tell that the
11532 elements won't have reference type. */
11533 tree type
= TREE_TYPE (pattern
);
11534 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11535 && !PACK_EXPANSION_P (type
)
11536 && !WILDCARD_TYPE_P (type
))
11538 /* Otherwise use the normal path so we get convert_from_reference. */
11541 /* We cannot expand this expansion expression, because we don't have
11542 all of the argument packs we need. */
11543 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11545 /* We got some full packs, but we can't substitute them in until we
11546 have values for all the packs. So remember these until then. */
11548 t
= make_pack_expansion (pattern
);
11549 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11552 else if (unsubstituted_packs
)
11554 /* There were no real arguments, we're just replacing a parameter
11555 pack with another version of itself. Substitute into the
11556 pattern and return a PACK_EXPANSION_*. The caller will need to
11558 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11559 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11560 /*integral_constant_expression_p=*/false);
11562 t
= tsubst (pattern
, args
, complain
, in_decl
);
11563 t
= make_pack_expansion (t
);
11567 gcc_assert (len
>= 0);
11569 if (need_local_specializations
)
11571 /* We're in a late-specified return type, so create our own local
11572 specializations map; the current map is either NULL or (in the
11573 case of recursive unification) might have bindings that we don't
11574 want to use or alter. */
11575 saved_local_specializations
= local_specializations
;
11576 local_specializations
= new hash_map
<tree
, tree
>;
11579 /* For each argument in each argument pack, substitute into the
11581 result
= make_tree_vec (len
);
11582 tree elem_args
= copy_template_args (args
);
11583 for (i
= 0; i
< len
; ++i
)
11585 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11587 elem_args
, complain
,
11589 TREE_VEC_ELT (result
, i
) = t
;
11590 if (t
== error_mark_node
)
11592 result
= error_mark_node
;
11597 /* Update ARGS to restore the substitution from parameter packs to
11598 their argument packs. */
11599 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11601 tree parm
= TREE_PURPOSE (pack
);
11603 if (TREE_CODE (parm
) == PARM_DECL
11604 || TREE_CODE (parm
) == FIELD_DECL
)
11605 register_local_specialization (TREE_TYPE (pack
), parm
);
11610 if (TREE_VALUE (pack
) == NULL_TREE
)
11613 template_parm_level_and_index (parm
, &level
, &idx
);
11615 /* Update the corresponding argument. */
11616 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11617 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11620 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11624 if (need_local_specializations
)
11626 delete local_specializations
;
11627 local_specializations
= saved_local_specializations
;
11630 /* If the dependent pack arguments were such that we end up with only a
11631 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11632 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11633 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11634 return TREE_VEC_ELT (result
, 0);
11639 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11640 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11641 parameter packs; all parms generated from a function parameter pack will
11642 have the same DECL_PARM_INDEX. */
11645 get_pattern_parm (tree parm
, tree tmpl
)
11647 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11650 if (DECL_ARTIFICIAL (parm
))
11652 for (patparm
= DECL_ARGUMENTS (pattern
);
11653 patparm
; patparm
= DECL_CHAIN (patparm
))
11654 if (DECL_ARTIFICIAL (patparm
)
11655 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11660 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11661 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11662 gcc_assert (DECL_PARM_INDEX (patparm
)
11663 == DECL_PARM_INDEX (parm
));
11669 /* Make an argument pack out of the TREE_VEC VEC. */
11672 make_argument_pack (tree vec
)
11675 tree elt
= TREE_VEC_ELT (vec
, 0);
11677 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11680 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11681 TREE_CONSTANT (pack
) = 1;
11683 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11687 /* Return an exact copy of template args T that can be modified
11691 copy_template_args (tree t
)
11693 if (t
== error_mark_node
)
11696 int len
= TREE_VEC_LENGTH (t
);
11697 tree new_vec
= make_tree_vec (len
);
11699 for (int i
= 0; i
< len
; ++i
)
11701 tree elt
= TREE_VEC_ELT (t
, i
);
11702 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11703 elt
= copy_template_args (elt
);
11704 TREE_VEC_ELT (new_vec
, i
) = elt
;
11707 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11708 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11713 /* Substitute ARGS into the vector or list of template arguments T. */
11716 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11719 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11722 if (t
== error_mark_node
)
11723 return error_mark_node
;
11725 len
= TREE_VEC_LENGTH (t
);
11726 elts
= XALLOCAVEC (tree
, len
);
11728 for (i
= 0; i
< len
; i
++)
11730 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11733 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11734 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11735 else if (PACK_EXPANSION_P (orig_arg
))
11737 /* Substitute into an expansion expression. */
11738 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11740 if (TREE_CODE (new_arg
) == TREE_VEC
)
11741 /* Add to the expanded length adjustment the number of
11742 expanded arguments. We subtract one from this
11743 measurement, because the argument pack expression
11744 itself is already counted as 1 in
11745 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11746 the argument pack is empty. */
11747 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11749 else if (ARGUMENT_PACK_P (orig_arg
))
11751 /* Substitute into each of the arguments. */
11752 new_arg
= TYPE_P (orig_arg
)
11753 ? cxx_make_type (TREE_CODE (orig_arg
))
11754 : make_node (TREE_CODE (orig_arg
));
11756 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11757 args
, complain
, in_decl
);
11758 if (pack_args
== error_mark_node
)
11759 new_arg
= error_mark_node
;
11761 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11763 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11764 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11767 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11769 if (new_arg
== error_mark_node
)
11770 return error_mark_node
;
11773 if (new_arg
!= orig_arg
)
11780 /* Make space for the expanded arguments coming from template
11782 t
= make_tree_vec (len
+ expanded_len_adjust
);
11783 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11784 arguments for a member template.
11785 In that case each TREE_VEC in ORIG_T represents a level of template
11786 arguments, and ORIG_T won't carry any non defaulted argument count.
11787 It will rather be the nested TREE_VECs that will carry one.
11788 In other words, ORIG_T carries a non defaulted argument count only
11789 if it doesn't contain any nested TREE_VEC. */
11790 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11792 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11793 count
+= expanded_len_adjust
;
11794 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11796 for (i
= 0, out
= 0; i
< len
; i
++)
11798 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11799 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11800 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11804 /* Now expand the template argument pack "in place". */
11805 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11806 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11810 TREE_VEC_ELT (t
, out
) = elts
[i
];
11818 /* Substitute ARGS into one level PARMS of template parameters. */
11821 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11823 if (parms
== error_mark_node
)
11824 return error_mark_node
;
11826 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11828 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11830 tree tuple
= TREE_VEC_ELT (parms
, i
);
11832 if (tuple
== error_mark_node
)
11835 TREE_VEC_ELT (new_vec
, i
) =
11836 tsubst_template_parm (tuple
, args
, complain
);
11842 /* Return the result of substituting ARGS into the template parameters
11843 given by PARMS. If there are m levels of ARGS and m + n levels of
11844 PARMS, then the result will contain n levels of PARMS. For
11845 example, if PARMS is `template <class T> template <class U>
11846 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11847 result will be `template <int*, double, class V>'. */
11850 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11852 tree r
= NULL_TREE
;
11855 /* When substituting into a template, we must set
11856 PROCESSING_TEMPLATE_DECL as the template parameters may be
11857 dependent if they are based on one-another, and the dependency
11858 predicates are short-circuit outside of templates. */
11859 ++processing_template_decl
;
11861 for (new_parms
= &r
;
11862 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11863 new_parms
= &(TREE_CHAIN (*new_parms
)),
11864 parms
= TREE_CHAIN (parms
))
11866 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11869 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11870 - TMPL_ARGS_DEPTH (args
)),
11871 new_vec
, NULL_TREE
);
11874 --processing_template_decl
;
11879 /* Return the result of substituting ARGS into one template parameter
11880 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11881 parameter and which TREE_PURPOSE is the default argument of the
11882 template parameter. */
11885 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11887 tree default_value
, parm_decl
;
11889 if (args
== NULL_TREE
11891 || t
== error_mark_node
)
11894 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11896 default_value
= TREE_PURPOSE (t
);
11897 parm_decl
= TREE_VALUE (t
);
11899 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11900 if (TREE_CODE (parm_decl
) == PARM_DECL
11901 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11902 parm_decl
= error_mark_node
;
11903 default_value
= tsubst_template_arg (default_value
, args
,
11904 complain
, NULL_TREE
);
11906 return build_tree_list (default_value
, parm_decl
);
11909 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11910 type T. If T is not an aggregate or enumeration type, it is
11911 handled as if by tsubst. IN_DECL is as for tsubst. If
11912 ENTERING_SCOPE is nonzero, T is the context for a template which
11913 we are presently tsubst'ing. Return the substituted value. */
11916 tsubst_aggr_type (tree t
,
11918 tsubst_flags_t complain
,
11920 int entering_scope
)
11922 if (t
== NULL_TREE
)
11925 switch (TREE_CODE (t
))
11928 if (TYPE_PTRMEMFUNC_P (t
))
11929 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11931 /* Fall through. */
11932 case ENUMERAL_TYPE
:
11934 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11939 int saved_unevaluated_operand
;
11940 int saved_inhibit_evaluation_warnings
;
11942 /* In "sizeof(X<I>)" we need to evaluate "I". */
11943 saved_unevaluated_operand
= cp_unevaluated_operand
;
11944 cp_unevaluated_operand
= 0;
11945 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11946 c_inhibit_evaluation_warnings
= 0;
11948 /* First, determine the context for the type we are looking
11950 context
= TYPE_CONTEXT (t
);
11951 if (context
&& TYPE_P (context
))
11953 context
= tsubst_aggr_type (context
, args
, complain
,
11954 in_decl
, /*entering_scope=*/1);
11955 /* If context is a nested class inside a class template,
11956 it may still need to be instantiated (c++/33959). */
11957 context
= complete_type (context
);
11960 /* Then, figure out what arguments are appropriate for the
11961 type we are trying to find. For example, given:
11963 template <class T> struct S;
11964 template <class T, class U> void f(T, U) { S<U> su; }
11966 and supposing that we are instantiating f<int, double>,
11967 then our ARGS will be {int, double}, but, when looking up
11968 S we only want {double}. */
11969 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11970 complain
, in_decl
);
11971 if (argvec
== error_mark_node
)
11972 r
= error_mark_node
;
11975 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11976 entering_scope
, complain
);
11977 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11980 cp_unevaluated_operand
= saved_unevaluated_operand
;
11981 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11986 /* This is not a template type, so there's nothing to do. */
11990 return tsubst (t
, args
, complain
, in_decl
);
11994 /* Substitute into the default argument ARG (a default argument for
11995 FN), which has the indicated TYPE. */
11998 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
12000 tree saved_class_ptr
= NULL_TREE
;
12001 tree saved_class_ref
= NULL_TREE
;
12002 int errs
= errorcount
+ sorrycount
;
12004 /* This can happen in invalid code. */
12005 if (TREE_CODE (arg
) == DEFAULT_ARG
)
12008 /* This default argument came from a template. Instantiate the
12009 default argument here, not in tsubst. In the case of
12018 we must be careful to do name lookup in the scope of S<T>,
12019 rather than in the current class. */
12020 push_access_scope (fn
);
12021 /* The "this" pointer is not valid in a default argument. */
12024 saved_class_ptr
= current_class_ptr
;
12025 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12026 saved_class_ref
= current_class_ref
;
12027 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12030 push_deferring_access_checks(dk_no_deferred
);
12031 /* The default argument expression may cause implicitly defined
12032 member functions to be synthesized, which will result in garbage
12033 collection. We must treat this situation as if we were within
12034 the body of function so as to avoid collecting live data on the
12037 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12038 complain
, NULL_TREE
,
12039 /*integral_constant_expression_p=*/false);
12041 pop_deferring_access_checks();
12043 /* Restore the "this" pointer. */
12046 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12047 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12050 if (errorcount
+sorrycount
> errs
12051 && (complain
& tf_warning_or_error
))
12052 inform (input_location
,
12053 " when instantiating default argument for call to %qD", fn
);
12055 /* Make sure the default argument is reasonable. */
12056 arg
= check_default_argument (type
, arg
, complain
);
12058 pop_access_scope (fn
);
12063 /* Substitute into all the default arguments for FN. */
12066 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12071 tmpl_args
= DECL_TI_ARGS (fn
);
12073 /* If this function is not yet instantiated, we certainly don't need
12074 its default arguments. */
12075 if (uses_template_parms (tmpl_args
))
12077 /* Don't do this again for clones. */
12078 if (DECL_CLONED_FUNCTION_P (fn
))
12081 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12083 arg
= TREE_CHAIN (arg
))
12084 if (TREE_PURPOSE (arg
))
12085 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
12087 TREE_PURPOSE (arg
),
12091 /* Substitute the ARGS into the T, which is a _DECL. Return the
12092 result of the substitution. Issue error and warning messages under
12093 control of COMPLAIN. */
12096 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12098 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12099 location_t saved_loc
;
12100 tree r
= NULL_TREE
;
12102 hashval_t hash
= 0;
12104 /* Set the filename and linenumber to improve error-reporting. */
12105 saved_loc
= input_location
;
12106 input_location
= DECL_SOURCE_LOCATION (t
);
12108 switch (TREE_CODE (t
))
12110 case TEMPLATE_DECL
:
12112 /* We can get here when processing a member function template,
12113 member class template, or template template parameter. */
12114 tree decl
= DECL_TEMPLATE_RESULT (t
);
12119 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12121 /* Template template parameter is treated here. */
12122 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12123 if (new_type
== error_mark_node
)
12124 r
= error_mark_node
;
12125 /* If we get a real template back, return it. This can happen in
12126 the context of most_specialized_partial_spec. */
12127 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12130 /* The new TEMPLATE_DECL was built in
12131 reduce_template_parm_level. */
12132 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12136 /* We might already have an instance of this template.
12137 The ARGS are for the surrounding class type, so the
12138 full args contain the tsubst'd args for the context,
12139 plus the innermost args from the template decl. */
12140 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12141 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12142 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12143 /* Because this is a template, the arguments will still be
12144 dependent, even after substitution. If
12145 PROCESSING_TEMPLATE_DECL is not set, the dependency
12146 predicates will short-circuit. */
12147 ++processing_template_decl
;
12148 full_args
= tsubst_template_args (tmpl_args
, args
,
12149 complain
, in_decl
);
12150 --processing_template_decl
;
12151 if (full_args
== error_mark_node
)
12152 RETURN (error_mark_node
);
12154 /* If this is a default template template argument,
12155 tsubst might not have changed anything. */
12156 if (full_args
== tmpl_args
)
12159 hash
= hash_tmpl_and_args (t
, full_args
);
12160 spec
= retrieve_specialization (t
, full_args
, hash
);
12161 if (spec
!= NULL_TREE
)
12167 /* Make a new template decl. It will be similar to the
12168 original, but will record the current template arguments.
12169 We also create a new function declaration, which is just
12170 like the old one, but points to this new template, rather
12171 than the old one. */
12173 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12174 DECL_CHAIN (r
) = NULL_TREE
;
12176 // Build new template info linking to the original template decl.
12177 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12179 if (TREE_CODE (decl
) == TYPE_DECL
12180 && !TYPE_DECL_ALIAS_P (decl
))
12183 ++processing_template_decl
;
12184 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12185 --processing_template_decl
;
12186 if (new_type
== error_mark_node
)
12187 RETURN (error_mark_node
);
12189 TREE_TYPE (r
) = new_type
;
12190 /* For a partial specialization, we need to keep pointing to
12191 the primary template. */
12192 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12193 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12194 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12195 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12196 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12201 ++processing_template_decl
;
12202 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12203 --processing_template_decl
;
12204 if (new_decl
== error_mark_node
)
12205 RETURN (error_mark_node
);
12207 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12208 DECL_TI_TEMPLATE (new_decl
) = r
;
12209 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12210 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12211 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12214 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12215 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12216 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12218 /* The template parameters for this new template are all the
12219 template parameters for the old template, except the
12220 outermost level of parameters. */
12221 DECL_TEMPLATE_PARMS (r
)
12222 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12225 if (PRIMARY_TEMPLATE_P (t
))
12226 DECL_PRIMARY_TEMPLATE (r
) = r
;
12228 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
12229 /* Record this non-type partial instantiation. */
12230 register_specialization (r
, t
,
12231 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12236 case FUNCTION_DECL
:
12239 tree argvec
= NULL_TREE
;
12247 /* Nobody should be tsubst'ing into non-template functions. */
12248 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12250 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12254 /* If T is not dependent, just return it. */
12255 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12258 /* Calculate the most general template of which R is a
12259 specialization, and the complete set of arguments used to
12261 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12262 argvec
= tsubst_template_args (DECL_TI_ARGS
12263 (DECL_TEMPLATE_RESULT
12264 (DECL_TI_TEMPLATE (t
))),
12265 args
, complain
, in_decl
);
12266 if (argvec
== error_mark_node
)
12267 RETURN (error_mark_node
);
12269 /* Check to see if we already have this specialization. */
12270 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12271 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12279 /* We can see more levels of arguments than parameters if
12280 there was a specialization of a member template, like
12283 template <class T> struct S { template <class U> void f(); }
12284 template <> template <class U> void S<int>::f(U);
12286 Here, we'll be substituting into the specialization,
12287 because that's where we can find the code we actually
12288 want to generate, but we'll have enough arguments for
12289 the most general template.
12291 We also deal with the peculiar case:
12293 template <class T> struct S {
12294 template <class U> friend void f();
12296 template <class U> void f() {}
12298 template void f<double>();
12300 Here, the ARGS for the instantiation of will be {int,
12301 double}. But, we only need as many ARGS as there are
12302 levels of template parameters in CODE_PATTERN. We are
12303 careful not to get fooled into reducing the ARGS in
12306 template <class T> struct S { template <class U> void f(U); }
12307 template <class T> template <> void S<T>::f(int) {}
12309 which we can spot because the pattern will be a
12310 specialization in this case. */
12311 args_depth
= TMPL_ARGS_DEPTH (args
);
12313 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12314 if (args_depth
> parms_depth
12315 && !DECL_TEMPLATE_SPECIALIZATION (t
))
12316 args
= get_innermost_template_args (args
, parms_depth
);
12320 /* This special case arises when we have something like this:
12322 template <class T> struct S {
12323 friend void f<int>(int, double);
12326 Here, the DECL_TI_TEMPLATE for the friend declaration
12327 will be an IDENTIFIER_NODE. We are being called from
12328 tsubst_friend_function, and we want only to create a
12329 new decl (R) with appropriate types so that we can call
12330 determine_specialization. */
12331 gen_tmpl
= NULL_TREE
;
12334 if (DECL_CLASS_SCOPE_P (t
))
12336 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
12340 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
12341 complain
, t
, /*entering_scope=*/1);
12346 ctx
= DECL_CONTEXT (t
);
12348 type
= tsubst (TREE_TYPE (t
), args
, complain
|tf_fndecl_type
, in_decl
);
12349 if (type
== error_mark_node
)
12350 RETURN (error_mark_node
);
12352 /* If we hit excessive deduction depth, the type is bogus even if
12353 it isn't error_mark_node, so don't build a decl. */
12354 if (excessive_deduction_depth
)
12355 RETURN (error_mark_node
);
12357 /* We do NOT check for matching decls pushed separately at this
12358 point, as they may not represent instantiations of this
12359 template, and in any case are considered separate under the
12362 DECL_USE_TEMPLATE (r
) = 0;
12363 TREE_TYPE (r
) = type
;
12364 /* Clear out the mangled name and RTL for the instantiation. */
12365 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12366 SET_DECL_RTL (r
, NULL
);
12367 /* Leave DECL_INITIAL set on deleted instantiations. */
12368 if (!DECL_DELETED_FN (r
))
12369 DECL_INITIAL (r
) = NULL_TREE
;
12370 DECL_CONTEXT (r
) = ctx
;
12372 /* OpenMP UDRs have the only argument a reference to the declared
12373 type. We want to diagnose if the declared type is a reference,
12374 which is invalid, but as references to references are usually
12375 quietly merged, diagnose it here. */
12376 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12379 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12380 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12381 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12382 error_at (DECL_SOURCE_LOCATION (t
),
12383 "reference type %qT in "
12384 "%<#pragma omp declare reduction%>", argtype
);
12385 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12386 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12390 if (member
&& DECL_CONV_FN_P (r
))
12391 /* Type-conversion operator. Reconstruct the name, in
12392 case it's the name of one of the template's parameters. */
12393 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
12395 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
12397 DECL_RESULT (r
) = NULL_TREE
;
12399 TREE_STATIC (r
) = 0;
12400 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12401 DECL_EXTERNAL (r
) = 1;
12402 /* If this is an instantiation of a function with internal
12403 linkage, we already know what object file linkage will be
12404 assigned to the instantiation. */
12405 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12406 DECL_DEFER_OUTPUT (r
) = 0;
12407 DECL_CHAIN (r
) = NULL_TREE
;
12408 DECL_PENDING_INLINE_INFO (r
) = 0;
12409 DECL_PENDING_INLINE_P (r
) = 0;
12410 DECL_SAVED_TREE (r
) = NULL_TREE
;
12411 DECL_STRUCT_FUNCTION (r
) = NULL
;
12413 /* We'll re-clone as appropriate in instantiate_template. */
12414 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12416 /* If we aren't complaining now, return on error before we register
12417 the specialization so that we'll complain eventually. */
12418 if ((complain
& tf_error
) == 0
12419 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12420 && !grok_op_properties (r
, /*complain=*/false))
12421 RETURN (error_mark_node
);
12423 /* When instantiating a constrained member, substitute
12424 into the constraints to create a new constraint. */
12425 if (tree ci
= get_constraints (t
))
12428 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12429 set_constraints (r
, ci
);
12432 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12433 this in the special friend case mentioned above where
12434 GEN_TMPL is NULL. */
12437 DECL_TEMPLATE_INFO (r
)
12438 = build_template_info (gen_tmpl
, argvec
);
12439 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12442 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12444 /* We instantiated this while substituting into
12445 the type earlier (template/friend54.C). */
12448 /* We're not supposed to instantiate default arguments
12449 until they are called, for a template. But, for a
12452 template <class T> void f ()
12453 { extern void g(int i = T()); }
12455 we should do the substitution when the template is
12456 instantiated. We handle the member function case in
12457 instantiate_class_template since the default arguments
12458 might refer to other members of the class. */
12460 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12461 && !uses_template_parms (argvec
))
12462 tsubst_default_arguments (r
, complain
);
12465 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12467 /* Copy the list of befriending classes. */
12468 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
12470 friends
= &TREE_CHAIN (*friends
))
12472 *friends
= copy_node (*friends
);
12473 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
12478 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12480 maybe_retrofit_in_chrg (r
);
12481 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12482 RETURN (error_mark_node
);
12483 /* If this is an instantiation of a member template, clone it.
12484 If it isn't, that'll be handled by
12485 clone_constructors_and_destructors. */
12486 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12487 clone_function_decl (r
, /*update_methods=*/false);
12489 else if ((complain
& tf_error
) != 0
12490 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12491 && !grok_op_properties (r
, /*complain=*/true))
12492 RETURN (error_mark_node
);
12494 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12495 SET_DECL_FRIEND_CONTEXT (r
,
12496 tsubst (DECL_FRIEND_CONTEXT (t
),
12497 args
, complain
, in_decl
));
12499 /* Possibly limit visibility based on template args. */
12500 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12501 if (DECL_VISIBILITY_SPECIFIED (t
))
12503 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12504 DECL_ATTRIBUTES (r
)
12505 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12507 determine_visibility (r
);
12508 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12509 && !processing_template_decl
)
12510 defaulted_late_check (r
);
12512 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12513 args
, complain
, in_decl
);
12519 tree type
= NULL_TREE
;
12521 tree expanded_types
= NULL_TREE
;
12522 tree prev_r
= NULL_TREE
;
12523 tree first_r
= NULL_TREE
;
12525 if (DECL_PACK_P (t
))
12527 /* If there is a local specialization that isn't a
12528 parameter pack, it means that we're doing a "simple"
12529 substitution from inside tsubst_pack_expansion. Just
12530 return the local specialization (which will be a single
12532 tree spec
= retrieve_local_specialization (t
);
12534 && TREE_CODE (spec
) == PARM_DECL
12535 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12538 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12539 the parameters in this function parameter pack. */
12540 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12541 complain
, in_decl
);
12542 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12544 len
= TREE_VEC_LENGTH (expanded_types
);
12546 /* Zero-length parameter packs are boring. Just substitute
12549 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12554 /* All we did was update the type. Make a note of that. */
12555 type
= expanded_types
;
12556 expanded_types
= NULL_TREE
;
12560 /* Loop through all of the parameters we'll build. When T is
12561 a function parameter pack, LEN is the number of expanded
12562 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12564 for (i
= 0; i
< len
; ++i
)
12568 if (DECL_TEMPLATE_PARM_P (t
))
12569 SET_DECL_TEMPLATE_PARM_P (r
);
12571 if (expanded_types
)
12572 /* We're on the Ith parameter of the function parameter
12575 /* Get the Ith type. */
12576 type
= TREE_VEC_ELT (expanded_types
, i
);
12578 /* Rename the parameter to include the index. */
12580 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12583 /* We're dealing with a normal parameter. */
12584 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12586 type
= type_decays_to (type
);
12587 TREE_TYPE (r
) = type
;
12588 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12590 if (DECL_INITIAL (r
))
12592 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12593 DECL_INITIAL (r
) = TREE_TYPE (r
);
12595 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12596 complain
, in_decl
);
12599 DECL_CONTEXT (r
) = NULL_TREE
;
12601 if (!DECL_TEMPLATE_PARM_P (r
))
12602 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12604 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12605 args
, complain
, in_decl
);
12607 /* Keep track of the first new parameter we
12608 generate. That's what will be returned to the
12613 /* Build a proper chain of parameters when substituting
12614 into a function parameter pack. */
12616 DECL_CHAIN (prev_r
) = r
;
12619 /* If cp_unevaluated_operand is set, we're just looking for a
12620 single dummy parameter, so don't keep going. */
12621 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12622 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12623 complain
, DECL_CHAIN (t
));
12625 /* FIRST_R contains the start of the chain we've built. */
12632 tree type
= NULL_TREE
;
12633 tree vec
= NULL_TREE
;
12634 tree expanded_types
= NULL_TREE
;
12637 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12639 /* This field is a lambda capture pack. Return a TREE_VEC of
12640 the expanded fields to instantiate_class_template_1 and
12641 store them in the specializations hash table as a
12642 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12643 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12644 complain
, in_decl
);
12645 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12647 len
= TREE_VEC_LENGTH (expanded_types
);
12648 vec
= make_tree_vec (len
);
12652 /* All we did was update the type. Make a note of that. */
12653 type
= expanded_types
;
12654 expanded_types
= NULL_TREE
;
12658 for (int i
= 0; i
< len
; ++i
)
12661 if (expanded_types
)
12663 type
= TREE_VEC_ELT (expanded_types
, i
);
12665 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12668 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12670 if (type
== error_mark_node
)
12671 RETURN (error_mark_node
);
12672 TREE_TYPE (r
) = type
;
12673 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12675 if (DECL_C_BIT_FIELD (r
))
12676 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12677 non-bit-fields DECL_INITIAL is a non-static data member
12678 initializer, which gets deferred instantiation. */
12680 = tsubst_expr (DECL_INITIAL (t
), args
,
12682 /*integral_constant_expression_p=*/true);
12683 else if (DECL_INITIAL (t
))
12685 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12686 NSDMI in perform_member_init. Still set DECL_INITIAL
12687 so that we know there is one. */
12688 DECL_INITIAL (r
) = void_node
;
12689 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12690 retrofit_lang_decl (r
);
12691 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12693 /* We don't have to set DECL_CONTEXT here; it is set by
12694 finish_member_declaration. */
12695 DECL_CHAIN (r
) = NULL_TREE
;
12697 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12698 args
, complain
, in_decl
);
12701 TREE_VEC_ELT (vec
, i
) = r
;
12707 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12708 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12709 register_specialization (pack
, t
, args
, false, 0);
12715 /* We reach here only for member using decls. We also need to check
12716 uses_template_parms because DECL_DEPENDENT_P is not set for a
12717 using-declaration that designates a member of the current
12718 instantiation (c++/53549). */
12719 if (DECL_DEPENDENT_P (t
)
12720 || uses_template_parms (USING_DECL_SCOPE (t
)))
12722 tree scope
= USING_DECL_SCOPE (t
);
12723 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12724 if (PACK_EXPANSION_P (scope
))
12726 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12727 int len
= TREE_VEC_LENGTH (vec
);
12728 r
= make_tree_vec (len
);
12729 for (int i
= 0; i
< len
; ++i
)
12731 tree escope
= TREE_VEC_ELT (vec
, i
);
12732 tree elt
= do_class_using_decl (escope
, name
);
12735 r
= error_mark_node
;
12740 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12741 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12743 TREE_VEC_ELT (r
, i
) = elt
;
12748 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12749 complain
, in_decl
);
12750 r
= do_class_using_decl (inst_scope
, name
);
12752 r
= error_mark_node
;
12755 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12756 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12763 DECL_CHAIN (r
) = NULL_TREE
;
12770 tree argvec
= NULL_TREE
;
12771 tree gen_tmpl
= NULL_TREE
;
12773 tree tmpl
= NULL_TREE
;
12775 tree type
= NULL_TREE
;
12778 if (TREE_TYPE (t
) == error_mark_node
)
12779 RETURN (error_mark_node
);
12781 if (TREE_CODE (t
) == TYPE_DECL
12782 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12784 /* If this is the canonical decl, we don't have to
12785 mess with instantiations, and often we can't (for
12786 typename, template type parms and such). Note that
12787 TYPE_NAME is not correct for the above test if
12788 we've copied the type for a typedef. */
12789 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12790 if (type
== error_mark_node
)
12791 RETURN (error_mark_node
);
12792 r
= TYPE_NAME (type
);
12796 /* Check to see if we already have the specialization we
12799 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12801 /* T is a static data member or namespace-scope entity.
12802 We have to substitute into namespace-scope variables
12803 (not just variable templates) because of cases like:
12805 template <class T> void f() { extern T t; }
12807 where the entity referenced is not known until
12808 instantiation time. */
12810 ctx
= DECL_CONTEXT (t
);
12811 if (DECL_CLASS_SCOPE_P (t
))
12813 ctx
= tsubst_aggr_type (ctx
, args
,
12815 in_decl
, /*entering_scope=*/1);
12816 /* If CTX is unchanged, then T is in fact the
12817 specialization we want. That situation occurs when
12818 referencing a static data member within in its own
12819 class. We can use pointer equality, rather than
12820 same_type_p, because DECL_CONTEXT is always
12822 if (ctx
== DECL_CONTEXT (t
)
12823 /* ... unless T is a member template; in which
12824 case our caller can be willing to create a
12825 specialization of that template represented
12827 && !(DECL_TI_TEMPLATE (t
)
12828 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12834 tmpl
= DECL_TI_TEMPLATE (t
);
12835 gen_tmpl
= most_general_template (tmpl
);
12836 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12837 if (argvec
!= error_mark_node
)
12838 argvec
= (coerce_innermost_template_parms
12839 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12840 argvec
, t
, complain
,
12841 /*all*/true, /*defarg*/true));
12842 if (argvec
== error_mark_node
)
12843 RETURN (error_mark_node
);
12844 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12845 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12850 /* A local variable. */
12852 /* Subsequent calls to pushdecl will fill this in. */
12854 /* Unless this is a reference to a static variable from an
12855 enclosing function, in which case we need to fill it in now. */
12856 if (TREE_STATIC (t
))
12858 tree fn
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12859 if (fn
!= current_function_decl
)
12862 spec
= retrieve_local_specialization (t
);
12864 /* If we already have the specialization we need, there is
12865 nothing more to do. */
12872 /* Create a new node for the specialization we need. */
12874 if (type
== NULL_TREE
)
12876 if (is_typedef_decl (t
))
12877 type
= DECL_ORIGINAL_TYPE (t
);
12879 type
= TREE_TYPE (t
);
12881 && VAR_HAD_UNKNOWN_BOUND (t
)
12882 && type
!= error_mark_node
)
12883 type
= strip_array_domain (type
);
12884 type
= tsubst (type
, args
, complain
, in_decl
);
12888 /* Even if the original location is out of scope, the
12889 newly substituted one is not. */
12890 DECL_DEAD_FOR_LOCAL (r
) = 0;
12891 DECL_INITIALIZED_P (r
) = 0;
12892 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
12893 if (type
== error_mark_node
)
12894 RETURN (error_mark_node
);
12895 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12897 /* It may seem that this case cannot occur, since:
12902 declares a function, not a variable. However:
12905 template <typename T> void g() { T t; }
12906 template void g<f>();
12908 is an attempt to declare a variable with function
12910 error ("variable %qD has function type",
12911 /* R is not yet sufficiently initialized, so we
12912 just use its name. */
12914 RETURN (error_mark_node
);
12916 type
= complete_type (type
);
12917 /* Wait until cp_finish_decl to set this again, to handle
12918 circular dependency (template/instantiate6.C). */
12919 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
12920 type
= check_var_type (DECL_NAME (r
), type
);
12922 if (DECL_HAS_VALUE_EXPR_P (t
))
12924 tree ve
= DECL_VALUE_EXPR (t
);
12925 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
12926 /*constant_expression_p=*/false);
12927 if (REFERENCE_REF_P (ve
))
12929 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
12930 ve
= TREE_OPERAND (ve
, 0);
12932 SET_DECL_VALUE_EXPR (r
, ve
);
12934 if (CP_DECL_THREAD_LOCAL_P (r
)
12935 && !processing_template_decl
)
12936 set_decl_tls_model (r
, decl_default_tls_model (r
));
12938 else if (DECL_SELF_REFERENCE_P (t
))
12939 SET_DECL_SELF_REFERENCE_P (r
);
12940 TREE_TYPE (r
) = type
;
12941 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12942 DECL_CONTEXT (r
) = ctx
;
12943 /* Clear out the mangled name and RTL for the instantiation. */
12944 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12945 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12946 SET_DECL_RTL (r
, NULL
);
12947 /* The initializer must not be expanded until it is required;
12948 see [temp.inst]. */
12949 DECL_INITIAL (r
) = NULL_TREE
;
12950 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
12953 SET_DECL_MODE (r
, VOIDmode
);
12955 /* Possibly limit visibility based on template args. */
12956 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12957 if (DECL_VISIBILITY_SPECIFIED (t
))
12959 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12960 DECL_ATTRIBUTES (r
)
12961 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12963 determine_visibility (r
);
12968 /* A static data member declaration is always marked
12969 external when it is declared in-class, even if an
12970 initializer is present. We mimic the non-template
12971 processing here. */
12972 DECL_EXTERNAL (r
) = 1;
12973 if (DECL_NAMESPACE_SCOPE_P (t
))
12974 DECL_NOT_REALLY_EXTERN (r
) = 1;
12976 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12977 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12978 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12982 if (DECL_LANG_SPECIFIC (r
))
12983 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12984 if (!cp_unevaluated_operand
)
12985 register_local_specialization (r
, t
);
12988 DECL_CHAIN (r
) = NULL_TREE
;
12990 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12992 args
, complain
, in_decl
);
12994 /* Preserve a typedef that names a type. */
12995 if (is_typedef_decl (r
) && type
!= error_mark_node
)
12997 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12998 set_underlying_type (r
);
12999 if (TYPE_DECL_ALIAS_P (r
))
13000 /* An alias template specialization can be dependent
13001 even if its underlying type is not. */
13002 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
13005 layout_decl (r
, 0);
13010 gcc_unreachable ();
13015 /* Restore the file and line information. */
13016 input_location
= saved_loc
;
13021 /* Substitute into the ARG_TYPES of a function type.
13022 If END is a TREE_CHAIN, leave it and any following types
13026 tsubst_arg_types (tree arg_types
,
13029 tsubst_flags_t complain
,
13032 tree remaining_arg_types
;
13033 tree type
= NULL_TREE
;
13035 tree expanded_args
= NULL_TREE
;
13038 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13041 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13042 args
, end
, complain
, in_decl
);
13043 if (remaining_arg_types
== error_mark_node
)
13044 return error_mark_node
;
13046 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13048 /* For a pack expansion, perform substitution on the
13049 entire expression. Later on, we'll handle the arguments
13051 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13052 args
, complain
, in_decl
);
13054 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13055 /* So that we'll spin through the parameters, one by one. */
13056 i
= TREE_VEC_LENGTH (expanded_args
);
13059 /* We only partially substituted into the parameter
13060 pack. Our type is TYPE_PACK_EXPANSION. */
13061 type
= expanded_args
;
13062 expanded_args
= NULL_TREE
;
13070 type
= TREE_VEC_ELT (expanded_args
, i
);
13072 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13074 if (type
== error_mark_node
)
13075 return error_mark_node
;
13076 if (VOID_TYPE_P (type
))
13078 if (complain
& tf_error
)
13080 error ("invalid parameter type %qT", type
);
13082 error ("in declaration %q+D", in_decl
);
13084 return error_mark_node
;
13087 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13088 return error_mark_node
;
13090 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13091 top-level qualifiers as required. */
13092 type
= cv_unqualified (type_decays_to (type
));
13094 /* We do not substitute into default arguments here. The standard
13095 mandates that they be instantiated only when needed, which is
13096 done in build_over_call. */
13097 default_arg
= TREE_PURPOSE (arg_types
);
13099 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13101 /* We've instantiated a template before its default arguments
13102 have been parsed. This can happen for a nested template
13103 class, and is not an error unless we require the default
13104 argument in a call of this function. */
13105 remaining_arg_types
=
13106 tree_cons (default_arg
, type
, remaining_arg_types
);
13107 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13110 remaining_arg_types
=
13111 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13114 return remaining_arg_types
;
13117 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13118 *not* handle the exception-specification for FNTYPE, because the
13119 initial substitution of explicitly provided template parameters
13120 during argument deduction forbids substitution into the
13121 exception-specification:
13125 All references in the function type of the function template to the
13126 corresponding template parameters are replaced by the specified tem-
13127 plate argument values. If a substitution in a template parameter or
13128 in the function type of the function template results in an invalid
13129 type, type deduction fails. [Note: The equivalent substitution in
13130 exception specifications is done only when the function is instanti-
13131 ated, at which point a program is ill-formed if the substitution
13132 results in an invalid type.] */
13135 tsubst_function_type (tree t
,
13137 tsubst_flags_t complain
,
13141 tree arg_types
= NULL_TREE
;
13144 /* The TYPE_CONTEXT is not used for function/method types. */
13145 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13147 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13149 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13151 if (late_return_type_p
)
13153 /* Substitute the argument types. */
13154 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13155 complain
, in_decl
);
13156 if (arg_types
== error_mark_node
)
13157 return error_mark_node
;
13159 tree save_ccp
= current_class_ptr
;
13160 tree save_ccr
= current_class_ref
;
13161 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13162 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13163 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13166 /* DR 1207: 'this' is in scope in the trailing return type. */
13167 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13170 /* Substitute the return type. */
13171 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13175 current_class_ptr
= save_ccp
;
13176 current_class_ref
= save_ccr
;
13180 /* Substitute the return type. */
13181 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13183 if (return_type
== error_mark_node
)
13184 return error_mark_node
;
13185 /* DR 486 clarifies that creation of a function type with an
13186 invalid return type is a deduction failure. */
13187 if (TREE_CODE (return_type
) == ARRAY_TYPE
13188 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13190 if (complain
& tf_error
)
13192 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13193 error ("function returning an array");
13195 error ("function returning a function");
13197 return error_mark_node
;
13200 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13201 return error_mark_node
;
13203 if (!late_return_type_p
)
13205 /* Substitute the argument types. */
13206 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13207 complain
, in_decl
);
13208 if (arg_types
== error_mark_node
)
13209 return error_mark_node
;
13212 /* Construct a new type node and return it. */
13213 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13215 fntype
= build_function_type (return_type
, arg_types
);
13216 fntype
= apply_memfn_quals (fntype
,
13217 type_memfn_quals (t
),
13218 type_memfn_rqual (t
));
13222 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13223 /* Don't pick up extra function qualifiers from the basetype. */
13224 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13225 if (! MAYBE_CLASS_TYPE_P (r
))
13229 Type deduction may fail for any of the following
13232 -- Attempting to create "pointer to member of T" when T
13233 is not a class type. */
13234 if (complain
& tf_error
)
13235 error ("creating pointer to member function of non-class type %qT",
13237 return error_mark_node
;
13240 fntype
= build_method_type_directly (r
, return_type
,
13241 TREE_CHAIN (arg_types
));
13242 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13244 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13246 if (late_return_type_p
)
13247 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13252 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13253 ARGS into that specification, and return the substituted
13254 specification. If there is no specification, return NULL_TREE. */
13257 tsubst_exception_specification (tree fntype
,
13259 tsubst_flags_t complain
,
13266 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13267 new_specs
= NULL_TREE
;
13268 if (specs
&& TREE_PURPOSE (specs
))
13270 /* A noexcept-specifier. */
13271 tree expr
= TREE_PURPOSE (specs
);
13272 if (TREE_CODE (expr
) == INTEGER_CST
)
13276 /* Defer instantiation of noexcept-specifiers to avoid
13277 excessive instantiations (c++/49107). */
13278 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13279 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13281 /* We already partially instantiated this member template,
13282 so combine the new args with the old. */
13283 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13284 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13285 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13286 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13290 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13291 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13295 new_specs
= tsubst_copy_and_build
13296 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13297 /*integral_constant_expression_p=*/true);
13298 new_specs
= build_noexcept_spec (new_specs
, complain
);
13302 if (! TREE_VALUE (specs
))
13309 tree expanded_specs
= NULL_TREE
;
13311 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13313 /* Expand the pack expansion type. */
13314 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13318 if (expanded_specs
== error_mark_node
)
13319 return error_mark_node
;
13320 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13321 len
= TREE_VEC_LENGTH (expanded_specs
);
13324 /* We're substituting into a member template, so
13325 we got a TYPE_PACK_EXPANSION back. Add that
13326 expansion and move on. */
13327 gcc_assert (TREE_CODE (expanded_specs
)
13328 == TYPE_PACK_EXPANSION
);
13329 new_specs
= add_exception_specifier (new_specs
,
13332 specs
= TREE_CHAIN (specs
);
13337 for (i
= 0; i
< len
; ++i
)
13339 if (expanded_specs
)
13340 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13342 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13343 if (spec
== error_mark_node
)
13345 new_specs
= add_exception_specifier (new_specs
, spec
,
13349 specs
= TREE_CHAIN (specs
);
13355 /* Take the tree structure T and replace template parameters used
13356 therein with the argument vector ARGS. IN_DECL is an associated
13357 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13358 Issue error and warning messages under control of COMPLAIN. Note
13359 that we must be relatively non-tolerant of extensions here, in
13360 order to preserve conformance; if we allow substitutions that
13361 should not be allowed, we may allow argument deductions that should
13362 not succeed, and therefore report ambiguous overload situations
13363 where there are none. In theory, we could allow the substitution,
13364 but indicate that it should have failed, and allow our caller to
13365 make sure that the right thing happens, but we don't try to do this
13368 This function is used for dealing with types, decls and the like;
13369 for expressions, use tsubst_expr or tsubst_copy. */
13372 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13374 enum tree_code code
;
13375 tree type
, r
= NULL_TREE
;
13377 if (t
== NULL_TREE
|| t
== error_mark_node
13378 || t
== integer_type_node
13379 || t
== void_type_node
13380 || t
== char_type_node
13381 || t
== unknown_type_node
13382 || TREE_CODE (t
) == NAMESPACE_DECL
13383 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13387 return tsubst_decl (t
, args
, complain
);
13389 if (args
== NULL_TREE
)
13392 code
= TREE_CODE (t
);
13394 if (code
== IDENTIFIER_NODE
)
13395 type
= IDENTIFIER_TYPE_VALUE (t
);
13397 type
= TREE_TYPE (t
);
13399 gcc_assert (type
!= unknown_type_node
);
13401 /* Reuse typedefs. We need to do this to handle dependent attributes,
13402 such as attribute aligned. */
13404 && typedef_variant_p (t
))
13406 tree decl
= TYPE_NAME (t
);
13408 if (alias_template_specialization_p (t
))
13410 /* DECL represents an alias template and we want to
13412 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13413 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13414 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13416 else if (DECL_CLASS_SCOPE_P (decl
)
13417 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13418 && uses_template_parms (DECL_CONTEXT (decl
)))
13420 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13421 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13422 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13424 else if (DECL_FUNCTION_SCOPE_P (decl
)
13425 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13426 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13427 r
= retrieve_local_specialization (decl
);
13429 /* The typedef is from a non-template context. */
13435 r
= cp_build_qualified_type_real
13436 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13437 complain
| tf_ignore_bad_quals
);
13442 /* We don't have an instantiation yet, so drop the typedef. */
13443 int quals
= cp_type_quals (t
);
13444 t
= DECL_ORIGINAL_TYPE (decl
);
13445 t
= cp_build_qualified_type_real (t
, quals
,
13446 complain
| tf_ignore_bad_quals
);
13450 bool fndecl_type
= (complain
& tf_fndecl_type
);
13451 complain
&= ~tf_fndecl_type
;
13454 && code
!= TYPENAME_TYPE
13455 && code
!= TEMPLATE_TYPE_PARM
13456 && code
!= TEMPLATE_PARM_INDEX
13457 && code
!= IDENTIFIER_NODE
13458 && code
!= FUNCTION_TYPE
13459 && code
!= METHOD_TYPE
)
13460 type
= tsubst (type
, args
, complain
, in_decl
);
13461 if (type
== error_mark_node
)
13462 return error_mark_node
;
13468 case ENUMERAL_TYPE
:
13469 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13470 /*entering_scope=*/0);
13473 case IDENTIFIER_NODE
:
13484 if (t
== integer_type_node
)
13487 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13488 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13492 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13494 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13495 /*integral_constant_expression_p=*/false);
13497 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13499 if (TREE_CODE (max
) == NOP_EXPR
13500 && TREE_SIDE_EFFECTS (omax
)
13501 && !TREE_TYPE (max
))
13502 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13504 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13505 with TREE_SIDE_EFFECTS that indicates this is not an integral
13506 constant expression. */
13507 if (processing_template_decl
13508 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13510 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13511 TREE_SIDE_EFFECTS (max
) = 1;
13514 return compute_array_index_type (NULL_TREE
, max
, complain
);
13517 case TEMPLATE_TYPE_PARM
:
13518 case TEMPLATE_TEMPLATE_PARM
:
13519 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13520 case TEMPLATE_PARM_INDEX
:
13525 tree arg
= NULL_TREE
;
13527 /* Early in template argument deduction substitution, we don't
13528 want to reduce the level of 'auto', or it will be confused
13529 with a normal template parm in subsequent deduction. */
13530 if (is_auto (t
) && (complain
& tf_partial
))
13535 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13536 template_parm_level_and_index (t
, &level
, &idx
);
13538 levels
= TMPL_ARGS_DEPTH (args
);
13539 if (level
<= levels
13540 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13542 arg
= TMPL_ARG (args
, level
, idx
);
13544 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13546 /* See through ARGUMENT_PACK_SELECT arguments. */
13547 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13548 /* If the selected argument is an expansion E, that most
13549 likely means we were called from
13550 gen_elem_of_pack_expansion_instantiation during the
13551 substituting of pack an argument pack (which Ith
13552 element is a pack expansion, where I is
13553 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13554 In this case, the Ith element resulting from this
13555 substituting is going to be a pack expansion, which
13556 pattern is the pattern of E. Let's return the
13558 gen_elem_of_pack_expansion_instantiation will
13559 build the resulting pack expansion from it. */
13560 if (PACK_EXPANSION_P (arg
))
13562 /* Make sure we aren't throwing away arg info. */
13563 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13564 arg
= PACK_EXPANSION_PATTERN (arg
);
13569 if (arg
== error_mark_node
)
13570 return error_mark_node
;
13571 else if (arg
!= NULL_TREE
)
13573 if (ARGUMENT_PACK_P (arg
))
13574 /* If ARG is an argument pack, we don't actually want to
13575 perform a substitution here, because substitutions
13576 for argument packs are only done
13577 element-by-element. We can get to this point when
13578 substituting the type of a non-type template
13579 parameter pack, when that type actually contains
13580 template parameter packs from an outer template, e.g.,
13582 template<typename... Types> struct A {
13583 template<Types... Values> struct B { };
13587 if (code
== TEMPLATE_TYPE_PARM
)
13590 gcc_assert (TYPE_P (arg
));
13592 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13594 return cp_build_qualified_type_real
13595 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13597 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13599 /* We are processing a type constructed from a
13600 template template parameter. */
13601 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13602 args
, complain
, in_decl
);
13603 if (argvec
== error_mark_node
)
13604 return error_mark_node
;
13606 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13607 || TREE_CODE (arg
) == TEMPLATE_DECL
13608 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13610 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13611 /* Consider this code:
13613 template <template <class> class Template>
13615 template <class Arg> using Bind = Template<Arg>;
13618 template <template <class> class Template, class Arg>
13619 using Instantiate = Template<Arg>; //#0
13621 template <template <class> class Template,
13624 Instantiate<Internal<Template>::template Bind,
13627 When #1 is parsed, the
13628 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13629 parameter `Template' in #0 matches the
13630 UNBOUND_CLASS_TEMPLATE representing the argument
13631 `Internal<Template>::template Bind'; We then want
13632 to assemble the type `Bind<Argument>' that can't
13633 be fully created right now, because
13634 `Internal<Template>' not being complete, the Bind
13635 template cannot be looked up in that context. So
13636 we need to "store" `Bind<Argument>' for later
13637 when the context of Bind becomes complete. Let's
13638 store that in a TYPENAME_TYPE. */
13639 return make_typename_type (TYPE_CONTEXT (arg
),
13640 build_nt (TEMPLATE_ID_EXPR
,
13641 TYPE_IDENTIFIER (arg
),
13646 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13647 are resolving nested-types in the signature of a
13648 member function templates. Otherwise ARG is a
13649 TEMPLATE_DECL and is the real template to be
13651 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13652 arg
= TYPE_NAME (arg
);
13654 r
= lookup_template_class (arg
,
13656 DECL_CONTEXT (arg
),
13657 /*entering_scope=*/0,
13659 return cp_build_qualified_type_real
13660 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13662 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13665 /* TEMPLATE_PARM_INDEX. */
13666 return convert_from_reference (unshare_expr (arg
));
13670 /* This can happen during the attempted tsubst'ing in
13671 unify. This means that we don't yet have any information
13672 about the template parameter in question. */
13675 /* If we get here, we must have been looking at a parm for a
13676 more deeply nested template. Make a new version of this
13677 template parameter, but with a lower level. */
13680 case TEMPLATE_TYPE_PARM
:
13681 case TEMPLATE_TEMPLATE_PARM
:
13682 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13683 if (cp_type_quals (t
))
13685 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13686 r
= cp_build_qualified_type_real
13687 (r
, cp_type_quals (t
),
13688 complain
| (code
== TEMPLATE_TYPE_PARM
13689 ? tf_ignore_bad_quals
: 0));
13691 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13692 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13693 && (r
= (TEMPLATE_PARM_DESCENDANTS
13694 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13695 && (r
= TREE_TYPE (r
))
13696 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13697 /* Break infinite recursion when substituting the constraints
13698 of a constrained placeholder. */;
13702 TEMPLATE_TYPE_PARM_INDEX (r
)
13703 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13704 r
, levels
, args
, complain
);
13705 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13706 TYPE_MAIN_VARIANT (r
) = r
;
13707 TYPE_POINTER_TO (r
) = NULL_TREE
;
13708 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13710 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13712 /* Propagate constraints on placeholders. */
13713 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13714 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13715 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13716 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13719 pl
= tsubst (pl
, args
, complain
, in_decl
);
13720 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13724 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13725 /* We have reduced the level of the template
13726 template parameter, but not the levels of its
13727 template parameters, so canonical_type_parameter
13728 will not be able to find the canonical template
13729 template parameter for this level. Thus, we
13730 require structural equality checking to compare
13731 TEMPLATE_TEMPLATE_PARMs. */
13732 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13733 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13734 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13736 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13738 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13740 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13741 /* We might need to substitute into the types of non-type
13742 template parameters. */
13743 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13744 complain
, in_decl
);
13745 if (tmpl
== error_mark_node
)
13746 return error_mark_node
;
13747 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13748 complain
, in_decl
);
13749 if (argvec
== error_mark_node
)
13750 return error_mark_node
;
13752 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13753 = build_template_info (tmpl
, argvec
);
13758 case TEMPLATE_PARM_INDEX
:
13759 /* OK, now substitute the type of the non-type parameter. We
13760 couldn't do it earlier because it might be an auto parameter,
13761 and we wouldn't need to if we had an argument. */
13762 type
= tsubst (type
, args
, complain
, in_decl
);
13763 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13767 gcc_unreachable ();
13775 tree purpose
, value
, chain
;
13777 if (t
== void_list_node
)
13780 purpose
= TREE_PURPOSE (t
);
13783 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13784 if (purpose
== error_mark_node
)
13785 return error_mark_node
;
13787 value
= TREE_VALUE (t
);
13790 value
= tsubst (value
, args
, complain
, in_decl
);
13791 if (value
== error_mark_node
)
13792 return error_mark_node
;
13794 chain
= TREE_CHAIN (t
);
13795 if (chain
&& chain
!= void_type_node
)
13797 chain
= tsubst (chain
, args
, complain
, in_decl
);
13798 if (chain
== error_mark_node
)
13799 return error_mark_node
;
13801 if (purpose
== TREE_PURPOSE (t
)
13802 && value
== TREE_VALUE (t
)
13803 && chain
== TREE_CHAIN (t
))
13805 return hash_tree_cons (purpose
, value
, chain
);
13809 /* We should never be tsubsting a binfo. */
13810 gcc_unreachable ();
13813 /* A vector of template arguments. */
13814 gcc_assert (!type
);
13815 return tsubst_template_args (t
, args
, complain
, in_decl
);
13818 case REFERENCE_TYPE
:
13820 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13825 Type deduction may fail for any of the following
13828 -- Attempting to create a pointer to reference type.
13829 -- Attempting to create a reference to a reference type or
13830 a reference to void.
13832 Core issue 106 says that creating a reference to a reference
13833 during instantiation is no longer a cause for failure. We
13834 only enforce this check in strict C++98 mode. */
13835 if ((TREE_CODE (type
) == REFERENCE_TYPE
13836 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13837 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13839 static location_t last_loc
;
13841 /* We keep track of the last time we issued this error
13842 message to avoid spewing a ton of messages during a
13843 single bad template instantiation. */
13844 if (complain
& tf_error
13845 && last_loc
!= input_location
)
13847 if (VOID_TYPE_P (type
))
13848 error ("forming reference to void");
13849 else if (code
== POINTER_TYPE
)
13850 error ("forming pointer to reference type %qT", type
);
13852 error ("forming reference to reference type %qT", type
);
13853 last_loc
= input_location
;
13856 return error_mark_node
;
13858 else if (TREE_CODE (type
) == FUNCTION_TYPE
13859 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13860 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13862 if (complain
& tf_error
)
13864 if (code
== POINTER_TYPE
)
13865 error ("forming pointer to qualified function type %qT",
13868 error ("forming reference to qualified function type %qT",
13871 return error_mark_node
;
13873 else if (code
== POINTER_TYPE
)
13875 r
= build_pointer_type (type
);
13876 if (TREE_CODE (type
) == METHOD_TYPE
)
13877 r
= build_ptrmemfunc_type (r
);
13879 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13880 /* In C++0x, during template argument substitution, when there is an
13881 attempt to create a reference to a reference type, reference
13882 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13884 "If a template-argument for a template-parameter T names a type
13885 that is a reference to a type A, an attempt to create the type
13886 'lvalue reference to cv T' creates the type 'lvalue reference to
13887 A,' while an attempt to create the type type rvalue reference to
13888 cv T' creates the type T"
13890 r
= cp_build_reference_type
13892 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13894 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13895 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13897 if (r
!= error_mark_node
)
13898 /* Will this ever be needed for TYPE_..._TO values? */
13905 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13906 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13910 Type deduction may fail for any of the following
13913 -- Attempting to create "pointer to member of T" when T
13914 is not a class type. */
13915 if (complain
& tf_error
)
13916 error ("creating pointer to member of non-class type %qT", r
);
13917 return error_mark_node
;
13919 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13921 if (complain
& tf_error
)
13922 error ("creating pointer to member reference type %qT", type
);
13923 return error_mark_node
;
13925 if (VOID_TYPE_P (type
))
13927 if (complain
& tf_error
)
13928 error ("creating pointer to member of type void");
13929 return error_mark_node
;
13931 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13932 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13934 /* The type of the implicit object parameter gets its
13935 cv-qualifiers from the FUNCTION_TYPE. */
13938 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13939 type_memfn_rqual (type
));
13940 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13941 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13945 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13949 case FUNCTION_TYPE
:
13954 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13955 if (fntype
== error_mark_node
)
13956 return error_mark_node
;
13958 /* Substitute the exception specification. */
13959 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
13960 /*defer_ok*/fndecl_type
);
13961 if (specs
== error_mark_node
)
13962 return error_mark_node
;
13964 fntype
= build_exception_variant (fntype
, specs
);
13969 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13970 if (domain
== error_mark_node
)
13971 return error_mark_node
;
13973 /* As an optimization, we avoid regenerating the array type if
13974 it will obviously be the same as T. */
13975 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13978 /* These checks should match the ones in create_array_type_for_decl.
13982 The deduction may fail for any of the following reasons:
13984 -- Attempting to create an array with an element type that
13985 is void, a function type, or a reference type, or [DR337]
13986 an abstract class type. */
13987 if (VOID_TYPE_P (type
)
13988 || TREE_CODE (type
) == FUNCTION_TYPE
13989 || (TREE_CODE (type
) == ARRAY_TYPE
13990 && TYPE_DOMAIN (type
) == NULL_TREE
)
13991 || TREE_CODE (type
) == REFERENCE_TYPE
)
13993 if (complain
& tf_error
)
13994 error ("creating array of %qT", type
);
13995 return error_mark_node
;
13998 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13999 return error_mark_node
;
14001 r
= build_cplus_array_type (type
, domain
);
14003 if (TYPE_USER_ALIGN (t
))
14005 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
14006 TYPE_USER_ALIGN (r
) = 1;
14012 case TYPENAME_TYPE
:
14014 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14015 in_decl
, /*entering_scope=*/1);
14016 if (ctx
== error_mark_node
)
14017 return error_mark_node
;
14019 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14020 complain
, in_decl
);
14021 if (f
== error_mark_node
)
14022 return error_mark_node
;
14024 if (!MAYBE_CLASS_TYPE_P (ctx
))
14026 if (complain
& tf_error
)
14027 error ("%qT is not a class, struct, or union type", ctx
);
14028 return error_mark_node
;
14030 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14032 /* Normally, make_typename_type does not require that the CTX
14033 have complete type in order to allow things like:
14035 template <class T> struct S { typename S<T>::X Y; };
14037 But, such constructs have already been resolved by this
14038 point, so here CTX really should have complete type, unless
14039 it's a partial instantiation. */
14040 ctx
= complete_type (ctx
);
14041 if (!COMPLETE_TYPE_P (ctx
))
14043 if (complain
& tf_error
)
14044 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14045 return error_mark_node
;
14049 f
= make_typename_type (ctx
, f
, typename_type
,
14050 complain
| tf_keep_type_decl
);
14051 if (f
== error_mark_node
)
14053 if (TREE_CODE (f
) == TYPE_DECL
)
14055 complain
|= tf_ignore_bad_quals
;
14059 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14061 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14063 if (complain
& tf_error
)
14064 error ("%qT resolves to %qT, which is not an enumeration type",
14067 return error_mark_node
;
14069 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14071 if (complain
& tf_error
)
14072 error ("%qT resolves to %qT, which is is not a class type",
14075 return error_mark_node
;
14079 return cp_build_qualified_type_real
14080 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14083 case UNBOUND_CLASS_TEMPLATE
:
14085 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14086 in_decl
, /*entering_scope=*/1);
14087 tree name
= TYPE_IDENTIFIER (t
);
14088 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14090 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14091 return error_mark_node
;
14094 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14095 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14102 ++cp_unevaluated_operand
;
14103 ++c_inhibit_evaluation_warnings
;
14105 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14107 /*integral_constant_expression_p=*/false);
14109 --cp_unevaluated_operand
;
14110 --c_inhibit_evaluation_warnings
;
14112 type
= finish_typeof (type
);
14113 return cp_build_qualified_type_real (type
,
14115 | cp_type_quals (type
),
14119 case DECLTYPE_TYPE
:
14123 ++cp_unevaluated_operand
;
14124 ++c_inhibit_evaluation_warnings
;
14126 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14127 complain
|tf_decltype
, in_decl
,
14128 /*function_p*/false,
14129 /*integral_constant_expression*/false);
14131 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14133 if (type
== NULL_TREE
)
14135 if (complain
& tf_error
)
14136 error ("empty initializer in lambda init-capture");
14137 type
= error_mark_node
;
14139 else if (TREE_CODE (type
) == TREE_LIST
)
14140 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14143 --cp_unevaluated_operand
;
14144 --c_inhibit_evaluation_warnings
;
14146 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14147 type
= lambda_capture_field_type (type
,
14148 DECLTYPE_FOR_INIT_CAPTURE (t
),
14149 DECLTYPE_FOR_REF_CAPTURE (t
));
14150 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14151 type
= lambda_proxy_type (type
);
14154 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14155 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14157 /* In a template ~id could be either a complement expression
14158 or an unqualified-id naming a destructor; if instantiating
14159 it produces an expression, it's not an id-expression or
14162 type
= finish_decltype_type (type
, id
, complain
);
14164 return cp_build_qualified_type_real (type
,
14166 | cp_type_quals (type
),
14167 complain
| tf_ignore_bad_quals
);
14170 case UNDERLYING_TYPE
:
14172 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14173 complain
, in_decl
);
14174 return finish_underlying_type (type
);
14177 case TYPE_ARGUMENT_PACK
:
14178 case NONTYPE_ARGUMENT_PACK
:
14182 if (code
== NONTYPE_ARGUMENT_PACK
)
14183 r
= make_node (code
);
14185 r
= cxx_make_type (code
);
14187 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14188 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14189 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14207 /* We should use one of the expression tsubsts for these codes. */
14208 gcc_unreachable ();
14211 sorry ("use of %qs in template", get_tree_code_name (code
));
14212 return error_mark_node
;
14216 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14217 expression on the left-hand side of the "." or "->" operator. A
14218 baselink indicates a function from a base class. Both the
14219 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14220 bases of the template class, rather than the instantiated class.
14221 In addition, lookups that were not ambiguous before may be
14222 ambiguous now. Therefore, we perform the lookup again. */
14225 tsubst_baselink (tree baselink
, tree object_type
,
14226 tree args
, tsubst_flags_t complain
, tree in_decl
)
14228 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
14230 tree qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14231 qualifying_scope
= tsubst (qualifying_scope
, args
, complain
, in_decl
);
14233 tree optype
= BASELINK_OPTYPE (baselink
);
14234 optype
= tsubst (optype
, args
, complain
, in_decl
);
14236 tree template_args
= NULL_TREE
;
14237 bool template_id_p
= false;
14238 tree fns
= BASELINK_FUNCTIONS (baselink
);
14239 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14241 template_id_p
= true;
14242 template_args
= TREE_OPERAND (fns
, 1);
14243 fns
= TREE_OPERAND (fns
, 0);
14245 template_args
= tsubst_template_args (template_args
, args
,
14246 complain
, in_decl
);
14249 tree name
= OVL_NAME (fns
);
14250 if (IDENTIFIER_TYPENAME_P (name
))
14251 name
= mangle_conv_op_name_for_type (optype
);
14253 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14256 if ((complain
& tf_error
) && constructor_name_p (name
, qualifying_scope
))
14257 error ("cannot call constructor %<%T::%D%> directly",
14258 qualifying_scope
, name
);
14259 return error_mark_node
;
14262 /* If lookup found a single function, mark it as used at this point.
14263 (If it lookup found multiple functions the one selected later by
14264 overload resolution will be marked as used at that point.) */
14265 if (BASELINK_P (baselink
))
14266 fns
= BASELINK_FUNCTIONS (baselink
);
14267 if (!template_id_p
&& !really_overloaded_fn (fns
)
14268 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14269 return error_mark_node
;
14271 if (BASELINK_P (baselink
))
14273 /* Add back the template arguments, if present. */
14275 BASELINK_FUNCTIONS (baselink
)
14276 = build2 (TEMPLATE_ID_EXPR
, unknown_type_node
,
14277 BASELINK_FUNCTIONS (baselink
), template_args
);
14279 /* Update the conversion operator type. */
14280 BASELINK_OPTYPE (baselink
) = optype
;
14284 object_type
= current_class_type
;
14286 if (qualified
|| name
== complete_dtor_identifier
)
14288 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14292 /* We need to call adjust_result_of_qualified_name_lookup in case the
14293 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14294 so that we still get virtual function binding. */
14295 BASELINK_QUALIFIED_P (baselink
) = false;
14301 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14302 true if the qualified-id will be a postfix-expression in-and-of
14303 itself; false if more of the postfix-expression follows the
14304 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14308 tsubst_qualified_id (tree qualified_id
, tree args
,
14309 tsubst_flags_t complain
, tree in_decl
,
14310 bool done
, bool address_p
)
14316 tree template_args
;
14317 location_t loc
= UNKNOWN_LOCATION
;
14319 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14321 /* Figure out what name to look up. */
14322 name
= TREE_OPERAND (qualified_id
, 1);
14323 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14325 is_template
= true;
14326 loc
= EXPR_LOCATION (name
);
14327 template_args
= TREE_OPERAND (name
, 1);
14329 template_args
= tsubst_template_args (template_args
, args
,
14330 complain
, in_decl
);
14331 if (template_args
== error_mark_node
)
14332 return error_mark_node
;
14333 name
= TREE_OPERAND (name
, 0);
14337 is_template
= false;
14338 template_args
= NULL_TREE
;
14341 /* Substitute into the qualifying scope. When there are no ARGS, we
14342 are just trying to simplify a non-dependent expression. In that
14343 case the qualifying scope may be dependent, and, in any case,
14344 substituting will not help. */
14345 scope
= TREE_OPERAND (qualified_id
, 0);
14348 scope
= tsubst (scope
, args
, complain
, in_decl
);
14349 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14354 if (dependent_scope_p (scope
))
14357 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14358 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14359 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14360 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14364 if (!BASELINK_P (name
) && !DECL_P (expr
))
14366 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14368 /* A BIT_NOT_EXPR is used to represent a destructor. */
14369 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14371 error ("qualifying type %qT does not match destructor name ~%qT",
14372 scope
, TREE_OPERAND (expr
, 0));
14373 expr
= error_mark_node
;
14376 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14377 /*is_type_p=*/0, false);
14380 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14381 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14382 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14384 if (complain
& tf_error
)
14386 error ("dependent-name %qE is parsed as a non-type, but "
14387 "instantiation yields a type", qualified_id
);
14388 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14390 return error_mark_node
;
14396 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14398 /* Remember that there was a reference to this entity. */
14399 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14400 return error_mark_node
;
14403 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14405 if (complain
& tf_error
)
14406 qualified_name_lookup_error (scope
,
14407 TREE_OPERAND (qualified_id
, 1),
14408 expr
, input_location
);
14409 return error_mark_node
;
14414 if (variable_template_p (expr
))
14415 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14418 expr
= lookup_template_function (expr
, template_args
);
14421 if (expr
== error_mark_node
&& complain
& tf_error
)
14422 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14423 expr
, input_location
);
14424 else if (TYPE_P (scope
))
14426 expr
= (adjust_result_of_qualified_name_lookup
14427 (expr
, scope
, current_nonlambda_class_type ()));
14428 expr
= (finish_qualified_id_expr
14429 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14430 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14431 /*template_arg_p=*/false, complain
));
14434 /* Expressions do not generally have reference type. */
14435 if (TREE_CODE (expr
) != SCOPE_REF
14436 /* However, if we're about to form a pointer-to-member, we just
14437 want the referenced member referenced. */
14438 && TREE_CODE (expr
) != OFFSET_REF
)
14439 expr
= convert_from_reference (expr
);
14441 if (REF_PARENTHESIZED_P (qualified_id
))
14442 expr
= force_paren_expr (expr
);
14447 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14448 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14452 tsubst_init (tree init
, tree decl
, tree args
,
14453 tsubst_flags_t complain
, tree in_decl
)
14458 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14460 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14462 /* If we had an initializer but it
14463 instantiated to nothing,
14464 value-initialize the object. This will
14465 only occur when the initializer was a
14466 pack expansion where the parameter packs
14467 used in that expansion were of length
14469 init
= build_value_init (TREE_TYPE (decl
),
14471 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14472 init
= get_target_expr_sfinae (init
, complain
);
14473 if (TREE_CODE (init
) == TARGET_EXPR
)
14474 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14480 /* Like tsubst, but deals with expressions. This function just replaces
14481 template parms; to finish processing the resultant expression, use
14482 tsubst_copy_and_build or tsubst_expr. */
14485 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14487 enum tree_code code
;
14490 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14493 code
= TREE_CODE (t
);
14498 r
= retrieve_local_specialization (t
);
14500 if (r
== NULL_TREE
)
14502 /* We get here for a use of 'this' in an NSDMI as part of a
14503 constructor call or as part of an aggregate initialization. */
14504 if (DECL_NAME (t
) == this_identifier
14505 && ((current_function_decl
14506 && DECL_CONSTRUCTOR_P (current_function_decl
))
14507 || (current_class_ref
14508 && TREE_CODE (current_class_ref
) == PLACEHOLDER_EXPR
)))
14509 return current_class_ptr
;
14511 /* This can happen for a parameter name used later in a function
14512 declaration (such as in a late-specified return type). Just
14513 make a dummy decl, since it's only used for its type. */
14514 gcc_assert (cp_unevaluated_operand
!= 0);
14515 r
= tsubst_decl (t
, args
, complain
);
14516 /* Give it the template pattern as its context; its true context
14517 hasn't been instantiated yet and this is good enough for
14519 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14522 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14523 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14524 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14525 return error_mark_node
;
14533 if (DECL_TEMPLATE_PARM_P (t
))
14534 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14535 /* There is no need to substitute into namespace-scope
14537 if (DECL_NAMESPACE_SCOPE_P (t
))
14539 /* If ARGS is NULL, then T is known to be non-dependent. */
14540 if (args
== NULL_TREE
)
14541 return scalar_constant_value (t
);
14543 /* Unfortunately, we cannot just call lookup_name here.
14546 template <int I> int f() {
14548 struct S { void g() { E e = a; } };
14551 When we instantiate f<7>::S::g(), say, lookup_name is not
14552 clever enough to find f<7>::a. */
14554 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14555 /*entering_scope=*/0);
14557 for (v
= TYPE_VALUES (enum_type
);
14559 v
= TREE_CHAIN (v
))
14560 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14561 return TREE_VALUE (v
);
14563 /* We didn't find the name. That should never happen; if
14564 name-lookup found it during preliminary parsing, we
14565 should find it again here during instantiation. */
14566 gcc_unreachable ();
14571 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14573 /* Check for a local specialization set up by
14574 tsubst_pack_expansion. */
14575 if (tree r
= retrieve_local_specialization (t
))
14577 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14578 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14582 /* When retrieving a capture pack from a generic lambda, remove the
14583 lambda call op's own template argument list from ARGS. Only the
14584 template arguments active for the closure type should be used to
14585 retrieve the pack specialization. */
14586 if (LAMBDA_FUNCTION_P (current_function_decl
)
14587 && (template_class_depth (DECL_CONTEXT (t
))
14588 != TMPL_ARGS_DEPTH (args
)))
14589 args
= strip_innermost_template_args (args
, 1);
14591 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14592 tsubst_decl put in the hash table. */
14593 return retrieve_specialization (t
, args
, 0);
14596 if (DECL_CONTEXT (t
))
14600 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14601 /*entering_scope=*/1);
14602 if (ctx
!= DECL_CONTEXT (t
))
14604 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14607 if (complain
& tf_error
)
14608 error ("using invalid field %qD", t
);
14609 return error_mark_node
;
14618 case FUNCTION_DECL
:
14619 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14620 r
= tsubst (t
, args
, complain
, in_decl
);
14621 else if (local_variable_p (t
)
14622 && uses_template_parms (DECL_CONTEXT (t
)))
14624 r
= retrieve_local_specialization (t
);
14625 if (r
== NULL_TREE
)
14627 /* First try name lookup to find the instantiation. */
14628 r
= lookup_name (DECL_NAME (t
));
14629 if (r
&& !is_capture_proxy (r
))
14631 /* Make sure that the one we found is the one we want. */
14632 tree ctx
= DECL_CONTEXT (t
);
14633 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
14634 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
14635 if (ctx
!= DECL_CONTEXT (r
))
14643 /* This can happen for a variable used in a
14644 late-specified return type of a local lambda, or for a
14645 local static or constant. Building a new VAR_DECL
14646 should be OK in all those cases. */
14647 r
= tsubst_decl (t
, args
, complain
);
14648 if (local_specializations
)
14649 /* Avoid infinite recursion (79640). */
14650 register_local_specialization (r
, t
);
14651 if (decl_maybe_constant_var_p (r
))
14653 /* We can't call cp_finish_decl, so handle the
14654 initializer by hand. */
14655 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14656 complain
, in_decl
);
14657 if (!processing_template_decl
)
14658 init
= maybe_constant_init (init
);
14659 if (processing_template_decl
14660 ? potential_constant_expression (init
)
14661 : reduced_constant_expression_p (init
))
14662 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14663 = TREE_CONSTANT (r
) = true;
14664 DECL_INITIAL (r
) = init
;
14666 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14667 || decl_constant_var_p (r
)
14668 || errorcount
|| sorrycount
);
14669 if (!processing_template_decl
14670 && !TREE_STATIC (r
))
14671 r
= process_outer_var_ref (r
, complain
);
14673 /* Remember this for subsequent uses. */
14674 if (local_specializations
)
14675 register_local_specialization (r
, t
);
14680 if (!mark_used (r
, complain
))
14681 return error_mark_node
;
14684 case NAMESPACE_DECL
:
14688 /* An OVERLOAD will always be a non-dependent overload set; an
14689 overload set from function scope will just be represented with an
14690 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14691 gcc_assert (!uses_template_parms (t
));
14692 /* We must have marked any lookups as persistent. */
14693 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14697 return tsubst_baselink (t
, current_nonlambda_class_type (),
14698 args
, complain
, in_decl
);
14700 case TEMPLATE_DECL
:
14701 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14702 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14703 args
, complain
, in_decl
);
14704 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14705 return tsubst (t
, args
, complain
, in_decl
);
14706 else if (DECL_CLASS_SCOPE_P (t
)
14707 && uses_template_parms (DECL_CONTEXT (t
)))
14709 /* Template template argument like the following example need
14712 template <template <class> class TT> struct C {};
14713 template <class T> struct D {
14714 template <class U> struct E {};
14719 We are processing the template argument `E' in #1 for
14720 the template instantiation #2. Originally, `E' is a
14721 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14722 have to substitute this with one having context `D<int>'. */
14724 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14725 if (dependent_scope_p (context
))
14727 /* When rewriting a constructor into a deduction guide, a
14728 non-dependent name can become dependent, so memtmpl<args>
14729 becomes context::template memtmpl<args>. */
14730 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14731 return build_qualified_name (type
, context
, DECL_NAME (t
),
14734 return lookup_field (context
, DECL_NAME(t
), 0, false);
14737 /* Ordinary template template argument. */
14741 case REINTERPRET_CAST_EXPR
:
14742 case CONST_CAST_EXPR
:
14743 case STATIC_CAST_EXPR
:
14744 case DYNAMIC_CAST_EXPR
:
14745 case IMPLICIT_CONV_EXPR
:
14749 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14750 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14751 return build1 (code
, type
, op0
);
14755 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14756 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14758 tree expanded
, op
= TREE_OPERAND (t
, 0);
14761 if (SIZEOF_EXPR_TYPE_P (t
))
14762 op
= TREE_TYPE (op
);
14764 ++cp_unevaluated_operand
;
14765 ++c_inhibit_evaluation_warnings
;
14766 /* We only want to compute the number of arguments. */
14767 if (PACK_EXPANSION_P (op
))
14768 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14770 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14771 args
, complain
, in_decl
);
14772 --cp_unevaluated_operand
;
14773 --c_inhibit_evaluation_warnings
;
14775 if (TREE_CODE (expanded
) == TREE_VEC
)
14777 len
= TREE_VEC_LENGTH (expanded
);
14778 /* Set TREE_USED for the benefit of -Wunused. */
14779 for (int i
= 0; i
< len
; i
++)
14780 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14781 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14784 if (expanded
== error_mark_node
)
14785 return error_mark_node
;
14786 else if (PACK_EXPANSION_P (expanded
)
14787 || (TREE_CODE (expanded
) == TREE_VEC
14788 && pack_expansion_args_count (expanded
)))
14791 if (PACK_EXPANSION_P (expanded
))
14793 else if (TREE_VEC_LENGTH (expanded
) == 1)
14794 expanded
= TREE_VEC_ELT (expanded
, 0);
14796 expanded
= make_argument_pack (expanded
);
14798 if (TYPE_P (expanded
))
14799 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14800 complain
& tf_error
);
14802 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14803 complain
& tf_error
);
14806 return build_int_cst (size_type_node
, len
);
14808 if (SIZEOF_EXPR_TYPE_P (t
))
14810 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14811 args
, complain
, in_decl
);
14812 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14813 r
= build1 (SIZEOF_EXPR
,
14814 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14815 SIZEOF_EXPR_TYPE_P (r
) = 1;
14822 case TRUTH_NOT_EXPR
:
14825 case UNARY_PLUS_EXPR
: /* Unary + */
14827 case AT_ENCODE_EXPR
:
14831 case REALPART_EXPR
:
14832 case IMAGPART_EXPR
:
14835 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14836 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14837 return build1 (code
, type
, op0
);
14840 case COMPONENT_REF
:
14845 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14846 name
= TREE_OPERAND (t
, 1);
14847 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14849 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14850 complain
, in_decl
);
14851 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14853 else if (TREE_CODE (name
) == SCOPE_REF
14854 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14856 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14857 complain
, in_decl
);
14858 name
= TREE_OPERAND (name
, 1);
14859 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14860 complain
, in_decl
);
14861 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14862 name
= build_qualified_name (/*type=*/NULL_TREE
,
14864 /*template_p=*/false);
14866 else if (BASELINK_P (name
))
14867 name
= tsubst_baselink (name
,
14868 non_reference (TREE_TYPE (object
)),
14872 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14873 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14879 case TRUNC_DIV_EXPR
:
14880 case CEIL_DIV_EXPR
:
14881 case FLOOR_DIV_EXPR
:
14882 case ROUND_DIV_EXPR
:
14883 case EXACT_DIV_EXPR
:
14887 case TRUNC_MOD_EXPR
:
14888 case FLOOR_MOD_EXPR
:
14889 case TRUTH_ANDIF_EXPR
:
14890 case TRUTH_ORIF_EXPR
:
14891 case TRUTH_AND_EXPR
:
14892 case TRUTH_OR_EXPR
:
14905 case COMPOUND_EXPR
:
14908 case PREDECREMENT_EXPR
:
14909 case PREINCREMENT_EXPR
:
14910 case POSTDECREMENT_EXPR
:
14911 case POSTINCREMENT_EXPR
:
14913 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14914 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14915 return build_nt (code
, op0
, op1
);
14920 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14921 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14922 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14923 QUALIFIED_NAME_IS_TEMPLATE (t
));
14928 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14929 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14930 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14935 int n
= VL_EXP_OPERAND_LENGTH (t
);
14936 tree result
= build_vl_exp (CALL_EXPR
, n
);
14938 for (i
= 0; i
< n
; i
++)
14939 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14940 complain
, in_decl
);
14946 case PSEUDO_DTOR_EXPR
:
14947 case VEC_PERM_EXPR
:
14949 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14950 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14951 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14952 r
= build_nt (code
, op0
, op1
, op2
);
14953 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14959 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14960 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14961 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14962 r
= build_nt (code
, op0
, op1
, op2
);
14963 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14969 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14970 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14971 r
= build_nt (code
, op0
, op1
);
14972 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14973 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14977 case TEMPLATE_ID_EXPR
:
14979 /* Substituted template arguments */
14980 tree fn
= TREE_OPERAND (t
, 0);
14981 tree targs
= TREE_OPERAND (t
, 1);
14983 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14985 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14987 return lookup_template_function (fn
, targs
);
14992 tree purpose
, value
, chain
;
14994 if (t
== void_list_node
)
14997 purpose
= TREE_PURPOSE (t
);
14999 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
15000 value
= TREE_VALUE (t
);
15002 value
= tsubst_copy (value
, args
, complain
, in_decl
);
15003 chain
= TREE_CHAIN (t
);
15004 if (chain
&& chain
!= void_type_node
)
15005 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
15006 if (purpose
== TREE_PURPOSE (t
)
15007 && value
== TREE_VALUE (t
)
15008 && chain
== TREE_CHAIN (t
))
15010 return tree_cons (purpose
, value
, chain
);
15015 case ENUMERAL_TYPE
:
15017 case TEMPLATE_TYPE_PARM
:
15018 case TEMPLATE_TEMPLATE_PARM
:
15019 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15020 case TEMPLATE_PARM_INDEX
:
15022 case REFERENCE_TYPE
:
15024 case FUNCTION_TYPE
:
15027 case TYPENAME_TYPE
:
15028 case UNBOUND_CLASS_TEMPLATE
:
15030 case DECLTYPE_TYPE
:
15032 return tsubst (t
, args
, complain
, in_decl
);
15036 /* Fall through. */
15037 case IDENTIFIER_NODE
:
15038 if (IDENTIFIER_TYPENAME_P (t
))
15040 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15041 return mangle_conv_op_name_for_type (new_type
);
15047 /* This is handled by tsubst_copy_and_build. */
15048 gcc_unreachable ();
15052 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15053 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15054 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15057 case CLEANUP_POINT_EXPR
:
15058 /* We shouldn't have built any of these during initial template
15059 generation. Instead, they should be built during instantiation
15060 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15061 gcc_unreachable ();
15065 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15066 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15067 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15068 r
= build2 (code
, type
, op0
, op1
);
15069 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15070 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15071 && !(complain
& tf_error
))
15072 return error_mark_node
;
15076 case EXPR_PACK_EXPANSION
:
15077 error ("invalid use of pack expansion expression");
15078 return error_mark_node
;
15080 case NONTYPE_ARGUMENT_PACK
:
15081 error ("use %<...%> to expand argument pack");
15082 return error_mark_node
;
15085 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15093 /* Instantiate any typedefs in the type. */
15094 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15095 r
= fold_convert (type
, t
);
15096 gcc_assert (TREE_CODE (r
) == code
);
15101 /* These can sometimes show up in a partial instantiation, but never
15102 involve template parms. */
15103 gcc_assert (!uses_template_parms (t
));
15106 case UNARY_LEFT_FOLD_EXPR
:
15107 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15108 case UNARY_RIGHT_FOLD_EXPR
:
15109 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15110 case BINARY_LEFT_FOLD_EXPR
:
15111 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15112 case BINARY_RIGHT_FOLD_EXPR
:
15113 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15116 /* We shouldn't get here, but keep going if !flag_checking. */
15118 gcc_unreachable ();
15123 /* Helper function for tsubst_omp_clauses, used for instantiation of
15124 OMP_CLAUSE_DECL of clauses. */
15127 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15130 if (decl
== NULL_TREE
)
15133 /* Handle an OpenMP array section represented as a TREE_LIST (or
15134 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15135 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15136 TREE_LIST. We can handle it exactly the same as an array section
15137 (purpose, value, and a chain), even though the nomenclature
15138 (low_bound, length, etc) is different. */
15139 if (TREE_CODE (decl
) == TREE_LIST
)
15142 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15143 /*integral_constant_expression_p=*/false);
15144 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15145 /*integral_constant_expression_p=*/false);
15146 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15148 if (TREE_PURPOSE (decl
) == low_bound
15149 && TREE_VALUE (decl
) == length
15150 && TREE_CHAIN (decl
) == chain
)
15152 tree ret
= tree_cons (low_bound
, length
, chain
);
15153 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15154 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15157 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15158 /*integral_constant_expression_p=*/false);
15159 /* Undo convert_from_reference tsubst_expr could have called. */
15161 && REFERENCE_REF_P (ret
)
15162 && !REFERENCE_REF_P (decl
))
15163 ret
= TREE_OPERAND (ret
, 0);
15167 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15170 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15171 tree args
, tsubst_flags_t complain
, tree in_decl
)
15173 tree new_clauses
= NULL_TREE
, nc
, oc
;
15174 tree linear_no_step
= NULL_TREE
;
15176 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15178 nc
= copy_node (oc
);
15179 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15182 switch (OMP_CLAUSE_CODE (nc
))
15184 case OMP_CLAUSE_LASTPRIVATE
:
15185 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15187 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15188 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15189 in_decl
, /*integral_constant_expression_p=*/false);
15190 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15191 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15194 case OMP_CLAUSE_PRIVATE
:
15195 case OMP_CLAUSE_SHARED
:
15196 case OMP_CLAUSE_FIRSTPRIVATE
:
15197 case OMP_CLAUSE_COPYIN
:
15198 case OMP_CLAUSE_COPYPRIVATE
:
15199 case OMP_CLAUSE_UNIFORM
:
15200 case OMP_CLAUSE_DEPEND
:
15201 case OMP_CLAUSE_FROM
:
15202 case OMP_CLAUSE_TO
:
15203 case OMP_CLAUSE_MAP
:
15204 case OMP_CLAUSE_USE_DEVICE_PTR
:
15205 case OMP_CLAUSE_IS_DEVICE_PTR
:
15206 OMP_CLAUSE_DECL (nc
)
15207 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15210 case OMP_CLAUSE_TILE
:
15211 case OMP_CLAUSE_IF
:
15212 case OMP_CLAUSE_NUM_THREADS
:
15213 case OMP_CLAUSE_SCHEDULE
:
15214 case OMP_CLAUSE_COLLAPSE
:
15215 case OMP_CLAUSE_FINAL
:
15216 case OMP_CLAUSE_DEVICE
:
15217 case OMP_CLAUSE_DIST_SCHEDULE
:
15218 case OMP_CLAUSE_NUM_TEAMS
:
15219 case OMP_CLAUSE_THREAD_LIMIT
:
15220 case OMP_CLAUSE_SAFELEN
:
15221 case OMP_CLAUSE_SIMDLEN
:
15222 case OMP_CLAUSE_NUM_TASKS
:
15223 case OMP_CLAUSE_GRAINSIZE
:
15224 case OMP_CLAUSE_PRIORITY
:
15225 case OMP_CLAUSE_ORDERED
:
15226 case OMP_CLAUSE_HINT
:
15227 case OMP_CLAUSE_NUM_GANGS
:
15228 case OMP_CLAUSE_NUM_WORKERS
:
15229 case OMP_CLAUSE_VECTOR_LENGTH
:
15230 case OMP_CLAUSE_WORKER
:
15231 case OMP_CLAUSE_VECTOR
:
15232 case OMP_CLAUSE_ASYNC
:
15233 case OMP_CLAUSE_WAIT
:
15234 OMP_CLAUSE_OPERAND (nc
, 0)
15235 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15236 in_decl
, /*integral_constant_expression_p=*/false);
15238 case OMP_CLAUSE_REDUCTION
:
15239 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15241 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15242 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15244 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15245 complain
, in_decl
);
15246 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15247 = build_qualified_name (NULL_TREE
, scope
,
15248 TREE_OPERAND (placeholder
, 1),
15252 gcc_assert (identifier_p (placeholder
));
15254 OMP_CLAUSE_DECL (nc
)
15255 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15258 case OMP_CLAUSE_GANG
:
15259 case OMP_CLAUSE_ALIGNED
:
15260 OMP_CLAUSE_DECL (nc
)
15261 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15263 OMP_CLAUSE_OPERAND (nc
, 1)
15264 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15265 in_decl
, /*integral_constant_expression_p=*/false);
15267 case OMP_CLAUSE_LINEAR
:
15268 OMP_CLAUSE_DECL (nc
)
15269 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15271 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15273 gcc_assert (!linear_no_step
);
15274 linear_no_step
= nc
;
15276 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15277 OMP_CLAUSE_LINEAR_STEP (nc
)
15278 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15279 complain
, in_decl
);
15281 OMP_CLAUSE_LINEAR_STEP (nc
)
15282 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15284 /*integral_constant_expression_p=*/false);
15286 case OMP_CLAUSE_NOWAIT
:
15287 case OMP_CLAUSE_DEFAULT
:
15288 case OMP_CLAUSE_UNTIED
:
15289 case OMP_CLAUSE_MERGEABLE
:
15290 case OMP_CLAUSE_INBRANCH
:
15291 case OMP_CLAUSE_NOTINBRANCH
:
15292 case OMP_CLAUSE_PROC_BIND
:
15293 case OMP_CLAUSE_FOR
:
15294 case OMP_CLAUSE_PARALLEL
:
15295 case OMP_CLAUSE_SECTIONS
:
15296 case OMP_CLAUSE_TASKGROUP
:
15297 case OMP_CLAUSE_NOGROUP
:
15298 case OMP_CLAUSE_THREADS
:
15299 case OMP_CLAUSE_SIMD
:
15300 case OMP_CLAUSE_DEFAULTMAP
:
15301 case OMP_CLAUSE_INDEPENDENT
:
15302 case OMP_CLAUSE_AUTO
:
15303 case OMP_CLAUSE_SEQ
:
15306 gcc_unreachable ();
15308 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15309 switch (OMP_CLAUSE_CODE (nc
))
15311 case OMP_CLAUSE_SHARED
:
15312 case OMP_CLAUSE_PRIVATE
:
15313 case OMP_CLAUSE_FIRSTPRIVATE
:
15314 case OMP_CLAUSE_LASTPRIVATE
:
15315 case OMP_CLAUSE_COPYPRIVATE
:
15316 case OMP_CLAUSE_LINEAR
:
15317 case OMP_CLAUSE_REDUCTION
:
15318 case OMP_CLAUSE_USE_DEVICE_PTR
:
15319 case OMP_CLAUSE_IS_DEVICE_PTR
:
15320 /* tsubst_expr on SCOPE_REF results in returning
15321 finish_non_static_data_member result. Undo that here. */
15322 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15323 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15324 == IDENTIFIER_NODE
))
15326 tree t
= OMP_CLAUSE_DECL (nc
);
15329 switch (TREE_CODE (v
))
15331 case COMPONENT_REF
:
15335 case POINTER_PLUS_EXPR
:
15336 v
= TREE_OPERAND (v
, 0);
15339 if (DECL_CONTEXT (v
) == current_function_decl
15340 && DECL_ARTIFICIAL (v
)
15341 && DECL_NAME (v
) == this_identifier
)
15342 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15349 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15350 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15351 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15352 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15353 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15355 tree decl
= OMP_CLAUSE_DECL (nc
);
15358 retrofit_lang_decl (decl
);
15359 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15368 new_clauses
= nreverse (new_clauses
);
15369 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15371 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15372 if (linear_no_step
)
15373 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15374 if (nc
== linear_no_step
)
15376 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15380 return new_clauses
;
15383 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15386 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15389 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15391 tree purpose
, value
, chain
;
15396 if (TREE_CODE (t
) != TREE_LIST
)
15397 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15398 /*function_p=*/false,
15399 /*integral_constant_expression_p=*/false);
15401 if (t
== void_list_node
)
15404 purpose
= TREE_PURPOSE (t
);
15406 purpose
= RECUR (purpose
);
15407 value
= TREE_VALUE (t
);
15410 if (TREE_CODE (value
) != LABEL_DECL
)
15411 value
= RECUR (value
);
15414 value
= lookup_label (DECL_NAME (value
));
15415 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15416 TREE_USED (value
) = 1;
15419 chain
= TREE_CHAIN (t
);
15420 if (chain
&& chain
!= void_type_node
)
15421 chain
= RECUR (chain
);
15422 return tree_cons (purpose
, value
, chain
);
15426 /* Used to temporarily communicate the list of #pragma omp parallel
15427 clauses to #pragma omp for instantiation if they are combined
15430 static tree
*omp_parallel_combined_clauses
;
15432 /* Substitute one OMP_FOR iterator. */
15435 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15436 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15437 tree args
, tsubst_flags_t complain
, tree in_decl
,
15438 bool integral_constant_expression_p
)
15440 #define RECUR(NODE) \
15441 tsubst_expr ((NODE), args, complain, in_decl, \
15442 integral_constant_expression_p)
15443 tree decl
, init
, cond
, incr
;
15445 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15446 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15448 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15450 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15451 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15454 decl
= TREE_OPERAND (init
, 0);
15455 init
= TREE_OPERAND (init
, 1);
15456 tree decl_expr
= NULL_TREE
;
15457 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15459 /* We need to jump through some hoops to handle declarations in the
15460 init-statement, since we might need to handle auto deduction,
15461 but we need to keep control of initialization. */
15463 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15464 decl
= tsubst_decl (decl
, args
, complain
);
15468 if (TREE_CODE (decl
) == SCOPE_REF
)
15470 decl
= RECUR (decl
);
15471 if (TREE_CODE (decl
) == COMPONENT_REF
)
15475 switch (TREE_CODE (v
))
15477 case COMPONENT_REF
:
15481 case POINTER_PLUS_EXPR
:
15482 v
= TREE_OPERAND (v
, 0);
15485 if (DECL_CONTEXT (v
) == current_function_decl
15486 && DECL_ARTIFICIAL (v
)
15487 && DECL_NAME (v
) == this_identifier
)
15489 decl
= TREE_OPERAND (decl
, 1);
15490 decl
= omp_privatize_field (decl
, false);
15500 decl
= RECUR (decl
);
15502 init
= RECUR (init
);
15504 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15505 if (auto_node
&& init
)
15507 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15509 gcc_assert (!type_dependent_expression_p (decl
));
15511 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15515 /* Declare the variable, but don't let that initialize it. */
15516 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15517 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15519 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15522 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15523 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15524 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15526 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15527 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15528 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15529 NOP_EXPR
, rhs
, complain
);
15532 incr
= RECUR (incr
);
15533 TREE_VEC_ELT (declv
, i
) = decl
;
15534 TREE_VEC_ELT (initv
, i
) = init
;
15535 TREE_VEC_ELT (condv
, i
) = cond
;
15536 TREE_VEC_ELT (incrv
, i
) = incr
;
15542 /* Declare and initialize the variable. */
15550 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15552 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15554 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15555 && OMP_CLAUSE_DECL (*pc
) == decl
)
15557 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15558 && OMP_CLAUSE_DECL (*pc
) == decl
)
15562 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15564 *pc
= OMP_CLAUSE_CHAIN (c
);
15565 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15568 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15569 && OMP_CLAUSE_DECL (*pc
) == decl
)
15571 error ("iteration variable %qD should not be firstprivate",
15573 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15575 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15576 && OMP_CLAUSE_DECL (*pc
) == decl
)
15578 error ("iteration variable %qD should not be reduction",
15580 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15583 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15588 if (*pc
== NULL_TREE
)
15590 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15591 OMP_CLAUSE_DECL (c
) = decl
;
15592 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15595 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15600 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15601 if (COMPARISON_CLASS_P (cond
))
15603 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15604 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15605 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15608 cond
= RECUR (cond
);
15609 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15610 switch (TREE_CODE (incr
))
15612 case PREINCREMENT_EXPR
:
15613 case PREDECREMENT_EXPR
:
15614 case POSTINCREMENT_EXPR
:
15615 case POSTDECREMENT_EXPR
:
15616 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15617 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15620 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15621 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15623 tree rhs
= TREE_OPERAND (incr
, 1);
15624 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15625 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15626 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15627 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15628 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15632 incr
= RECUR (incr
);
15635 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15636 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15638 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15639 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15640 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15641 TREE_TYPE (decl
), lhs
,
15642 RECUR (TREE_OPERAND (incr
, 2))));
15644 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15645 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15646 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15648 tree rhs
= TREE_OPERAND (incr
, 2);
15649 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15650 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15651 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15652 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15653 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15657 incr
= RECUR (incr
);
15660 incr
= RECUR (incr
);
15664 TREE_VEC_ELT (declv
, i
) = decl
;
15665 TREE_VEC_ELT (initv
, i
) = init
;
15666 TREE_VEC_ELT (condv
, i
) = cond
;
15667 TREE_VEC_ELT (incrv
, i
) = incr
;
15671 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15672 of OMP_TARGET's body. */
15675 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15677 *walk_subtrees
= 0;
15678 switch (TREE_CODE (*tp
))
15683 case STATEMENT_LIST
:
15684 *walk_subtrees
= 1;
15692 /* Helper function for tsubst_expr. For decomposition declaration
15693 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15694 also the corresponding decls representing the identifiers
15695 of the decomposition declaration. Return DECL if successful
15696 or error_mark_node otherwise, set *FIRST to the first decl
15697 in the list chained through DECL_CHAIN and *CNT to the number
15701 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15702 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15705 tree decl2
, decl3
, prev
= decl
;
15707 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15708 for (decl2
= DECL_CHAIN (pattern_decl
);
15711 && DECL_DECOMPOSITION_P (decl2
)
15712 && DECL_NAME (decl2
);
15713 decl2
= DECL_CHAIN (decl2
))
15715 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15717 gcc_assert (errorcount
);
15718 return error_mark_node
;
15721 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15722 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15723 tree v
= DECL_VALUE_EXPR (decl2
);
15724 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15725 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15726 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15727 SET_DECL_VALUE_EXPR (decl2
, v
);
15728 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15730 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15731 maybe_push_decl (decl3
);
15732 if (error_operand_p (decl3
))
15733 decl
= error_mark_node
;
15734 else if (decl
!= error_mark_node
15735 && DECL_CHAIN (decl3
) != prev
)
15737 gcc_assert (errorcount
);
15738 decl
= error_mark_node
;
15747 /* Like tsubst_copy for expressions, etc. but also does semantic
15751 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15752 bool integral_constant_expression_p
)
15754 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15755 #define RECUR(NODE) \
15756 tsubst_expr ((NODE), args, complain, in_decl, \
15757 integral_constant_expression_p)
15763 if (t
== NULL_TREE
|| t
== error_mark_node
)
15766 loc
= input_location
;
15767 if (EXPR_HAS_LOCATION (t
))
15768 input_location
= EXPR_LOCATION (t
);
15769 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15770 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15772 switch (TREE_CODE (t
))
15774 case STATEMENT_LIST
:
15776 tree_stmt_iterator i
;
15777 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15778 RECUR (tsi_stmt (i
));
15782 case CTOR_INITIALIZER
:
15783 finish_mem_initializers (tsubst_initializer_list
15784 (TREE_OPERAND (t
, 0), args
));
15788 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
15792 tmp
= RECUR (EXPR_STMT_EXPR (t
));
15793 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
15794 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
15796 finish_expr_stmt (tmp
);
15800 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
15801 /*attribs=*/NULL_TREE
);
15806 tree decl
, pattern_decl
;
15809 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15810 if (TREE_CODE (decl
) == LABEL_DECL
)
15811 finish_label_decl (DECL_NAME (decl
));
15812 else if (TREE_CODE (decl
) == USING_DECL
)
15814 tree scope
= USING_DECL_SCOPE (decl
);
15815 tree name
= DECL_NAME (decl
);
15817 scope
= tsubst (scope
, args
, complain
, in_decl
);
15818 decl
= lookup_qualified_name (scope
, name
,
15819 /*is_type_p=*/false,
15820 /*complain=*/false);
15821 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15822 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15824 finish_local_using_decl (decl
, scope
, name
);
15826 else if (DECL_PACK_P (decl
))
15828 /* Don't build up decls for a variadic capture proxy, we'll
15829 instantiate the elements directly as needed. */
15834 init
= DECL_INITIAL (decl
);
15835 decl
= tsubst (decl
, args
, complain
, in_decl
);
15836 if (decl
!= error_mark_node
)
15838 /* By marking the declaration as instantiated, we avoid
15839 trying to instantiate it. Since instantiate_decl can't
15840 handle local variables, and since we've already done
15841 all that needs to be done, that's the right thing to
15844 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15846 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15847 /* Anonymous aggregates are a special case. */
15848 finish_anon_union (decl
);
15849 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15851 DECL_CONTEXT (decl
) = current_function_decl
;
15852 if (DECL_NAME (decl
) == this_identifier
)
15854 tree lam
= DECL_CONTEXT (current_function_decl
);
15855 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15856 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15858 insert_capture_proxy (decl
);
15860 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15861 /* We already did a pushtag. */;
15862 else if (TREE_CODE (decl
) == FUNCTION_DECL
15863 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15864 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15866 DECL_CONTEXT (decl
) = NULL_TREE
;
15868 DECL_CONTEXT (decl
) = current_function_decl
;
15869 cp_check_omp_declare_reduction (decl
);
15873 int const_init
= false;
15874 maybe_push_decl (decl
);
15876 && DECL_PRETTY_FUNCTION_P (decl
))
15878 /* For __PRETTY_FUNCTION__ we have to adjust the
15880 const char *const name
15881 = cxx_printable_name (current_function_decl
, 2);
15882 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15885 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15888 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15890 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15892 && DECL_DECOMPOSITION_P (decl
)
15893 && TREE_TYPE (pattern_decl
) != error_mark_node
)
15897 decl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
15898 complain
, in_decl
, &first
,
15900 if (decl
!= error_mark_node
)
15901 cp_finish_decomp (decl
, first
, cnt
);
15911 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15912 RECUR (FOR_INIT_STMT (t
));
15913 finish_init_stmt (stmt
);
15914 tmp
= RECUR (FOR_COND (t
));
15915 finish_for_cond (tmp
, stmt
, false);
15916 tmp
= RECUR (FOR_EXPR (t
));
15917 finish_for_expr (tmp
, stmt
);
15918 RECUR (FOR_BODY (t
));
15919 finish_for_stmt (stmt
);
15922 case RANGE_FOR_STMT
:
15925 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15926 decl
= RANGE_FOR_DECL (t
);
15927 decl
= tsubst (decl
, args
, complain
, in_decl
);
15928 maybe_push_decl (decl
);
15929 expr
= RECUR (RANGE_FOR_EXPR (t
));
15930 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
15934 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
15935 complain
, in_decl
, &first
, &cnt
);
15936 stmt
= cp_convert_range_for (stmt
, decl
, expr
, first
, cnt
,
15937 RANGE_FOR_IVDEP (t
));
15940 stmt
= cp_convert_range_for (stmt
, decl
, expr
, NULL_TREE
, 0,
15941 RANGE_FOR_IVDEP (t
));
15942 RECUR (RANGE_FOR_BODY (t
));
15943 finish_for_stmt (stmt
);
15948 stmt
= begin_while_stmt ();
15949 tmp
= RECUR (WHILE_COND (t
));
15950 finish_while_stmt_cond (tmp
, stmt
, false);
15951 RECUR (WHILE_BODY (t
));
15952 finish_while_stmt (stmt
);
15956 stmt
= begin_do_stmt ();
15957 RECUR (DO_BODY (t
));
15958 finish_do_body (stmt
);
15959 tmp
= RECUR (DO_COND (t
));
15960 finish_do_stmt (tmp
, stmt
, false);
15964 stmt
= begin_if_stmt ();
15965 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
15966 tmp
= RECUR (IF_COND (t
));
15967 tmp
= finish_if_stmt_cond (tmp
, stmt
);
15968 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
15969 /* Don't instantiate the THEN_CLAUSE. */;
15972 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
15974 ++c_inhibit_evaluation_warnings
;
15975 RECUR (THEN_CLAUSE (t
));
15977 --c_inhibit_evaluation_warnings
;
15979 finish_then_clause (stmt
);
15981 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
15982 /* Don't instantiate the ELSE_CLAUSE. */;
15983 else if (ELSE_CLAUSE (t
))
15985 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
15986 begin_else_clause (stmt
);
15988 ++c_inhibit_evaluation_warnings
;
15989 RECUR (ELSE_CLAUSE (t
));
15991 --c_inhibit_evaluation_warnings
;
15992 finish_else_clause (stmt
);
15995 finish_if_stmt (stmt
);
15999 if (BIND_EXPR_BODY_BLOCK (t
))
16000 stmt
= begin_function_body ();
16002 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
16003 ? BCS_TRY_BLOCK
: 0);
16005 RECUR (BIND_EXPR_BODY (t
));
16007 if (BIND_EXPR_BODY_BLOCK (t
))
16008 finish_function_body (stmt
);
16010 finish_compound_stmt (stmt
);
16014 finish_break_stmt ();
16017 case CONTINUE_STMT
:
16018 finish_continue_stmt ();
16022 stmt
= begin_switch_stmt ();
16023 tmp
= RECUR (SWITCH_STMT_COND (t
));
16024 finish_switch_cond (tmp
, stmt
);
16025 RECUR (SWITCH_STMT_BODY (t
));
16026 finish_switch_stmt (stmt
);
16029 case CASE_LABEL_EXPR
:
16031 tree low
= RECUR (CASE_LOW (t
));
16032 tree high
= RECUR (CASE_HIGH (t
));
16033 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16034 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16035 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16036 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16042 tree decl
= LABEL_EXPR_LABEL (t
);
16045 label
= finish_label_stmt (DECL_NAME (decl
));
16046 if (TREE_CODE (label
) == LABEL_DECL
)
16047 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16048 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16049 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16054 tmp
= GOTO_DESTINATION (t
);
16055 if (TREE_CODE (tmp
) != LABEL_DECL
)
16056 /* Computed goto's must be tsubst'd into. On the other hand,
16057 non-computed gotos must not be; the identifier in question
16058 will have no binding. */
16061 tmp
= DECL_NAME (tmp
);
16062 finish_goto_stmt (tmp
);
16067 tree string
= RECUR (ASM_STRING (t
));
16068 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16069 complain
, in_decl
);
16070 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16071 complain
, in_decl
);
16072 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16073 complain
, in_decl
);
16074 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16075 complain
, in_decl
);
16076 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16078 tree asm_expr
= tmp
;
16079 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16080 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16081 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16088 stmt
= begin_try_block ();
16089 RECUR (TRY_STMTS (t
));
16090 finish_cleanup_try_block (stmt
);
16091 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16095 tree compound_stmt
= NULL_TREE
;
16097 if (FN_TRY_BLOCK_P (t
))
16098 stmt
= begin_function_try_block (&compound_stmt
);
16100 stmt
= begin_try_block ();
16102 RECUR (TRY_STMTS (t
));
16104 if (FN_TRY_BLOCK_P (t
))
16105 finish_function_try_block (stmt
);
16107 finish_try_block (stmt
);
16109 RECUR (TRY_HANDLERS (t
));
16110 if (FN_TRY_BLOCK_P (t
))
16111 finish_function_handler_sequence (stmt
, compound_stmt
);
16113 finish_handler_sequence (stmt
);
16119 tree decl
= HANDLER_PARMS (t
);
16123 decl
= tsubst (decl
, args
, complain
, in_decl
);
16124 /* Prevent instantiate_decl from trying to instantiate
16125 this variable. We've already done all that needs to be
16127 if (decl
!= error_mark_node
)
16128 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16130 stmt
= begin_handler ();
16131 finish_handler_parms (decl
, stmt
);
16132 RECUR (HANDLER_BODY (t
));
16133 finish_handler (stmt
);
16138 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16139 if (CLASS_TYPE_P (tmp
))
16141 /* Local classes are not independent templates; they are
16142 instantiated along with their containing function. And this
16143 way we don't have to deal with pushing out of one local class
16144 to instantiate a member of another local class. */
16146 /* Closures are handled by the LAMBDA_EXPR. */
16147 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16148 complete_type (tmp
);
16149 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
16150 if (!DECL_ARTIFICIAL (fn
))
16151 instantiate_decl (fn
, /*defer_ok=*/false,
16152 /*expl_inst_class=*/false);
16156 case STATIC_ASSERT
:
16160 ++c_inhibit_evaluation_warnings
;
16162 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16165 /*integral_constant_expression_p=*/true);
16166 --c_inhibit_evaluation_warnings
;
16168 finish_static_assert (condition
,
16169 STATIC_ASSERT_MESSAGE (t
),
16170 STATIC_ASSERT_SOURCE_LOCATION (t
),
16171 /*member_p=*/false);
16176 case OACC_PARALLEL
:
16177 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16179 stmt
= begin_omp_parallel ();
16180 RECUR (OMP_BODY (t
));
16181 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16185 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16186 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16187 complain
, in_decl
);
16188 if (OMP_PARALLEL_COMBINED (t
))
16189 omp_parallel_combined_clauses
= &tmp
;
16190 stmt
= begin_omp_parallel ();
16191 RECUR (OMP_PARALLEL_BODY (t
));
16192 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16193 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16194 = OMP_PARALLEL_COMBINED (t
);
16195 pop_omp_privatization_clauses (r
);
16199 r
= push_omp_privatization_clauses (false);
16200 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16201 complain
, in_decl
);
16202 stmt
= begin_omp_task ();
16203 RECUR (OMP_TASK_BODY (t
));
16204 finish_omp_task (tmp
, stmt
);
16205 pop_omp_privatization_clauses (r
);
16212 case OMP_DISTRIBUTE
:
16216 tree clauses
, body
, pre_body
;
16217 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16218 tree orig_declv
= NULL_TREE
;
16219 tree incrv
= NULL_TREE
;
16220 enum c_omp_region_type ort
= C_ORT_OMP
;
16223 if (TREE_CODE (t
) == CILK_SIMD
|| TREE_CODE (t
) == CILK_FOR
)
16225 else if (TREE_CODE (t
) == OACC_LOOP
)
16228 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16229 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16231 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16233 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16234 if (OMP_FOR_ORIG_DECLS (t
))
16235 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16236 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16237 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16238 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16241 stmt
= begin_omp_structured_block ();
16243 pre_body
= push_stmt_list ();
16244 RECUR (OMP_FOR_PRE_BODY (t
));
16245 pre_body
= pop_stmt_list (pre_body
);
16247 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16248 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16249 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16250 incrv
, &clauses
, args
, complain
, in_decl
,
16251 integral_constant_expression_p
);
16252 omp_parallel_combined_clauses
= NULL
;
16254 body
= push_stmt_list ();
16255 RECUR (OMP_FOR_BODY (t
));
16256 body
= pop_stmt_list (body
);
16258 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16259 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16260 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16264 t
= make_node (TREE_CODE (t
));
16265 TREE_TYPE (t
) = void_type_node
;
16266 OMP_FOR_BODY (t
) = body
;
16267 OMP_FOR_PRE_BODY (t
) = pre_body
;
16268 OMP_FOR_CLAUSES (t
) = clauses
;
16269 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16273 add_stmt (finish_omp_structured_block (stmt
));
16274 pop_omp_privatization_clauses (r
);
16279 omp_parallel_combined_clauses
= NULL
;
16284 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16285 && OMP_TEAMS_COMBINED (t
));
16286 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16288 stmt
= push_stmt_list ();
16289 RECUR (OMP_BODY (t
));
16290 stmt
= pop_stmt_list (stmt
);
16293 OMP_BODY (t
) = stmt
;
16294 OMP_CLAUSES (t
) = tmp
;
16296 pop_omp_privatization_clauses (r
);
16300 case OMP_TARGET_DATA
:
16302 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16303 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16305 keep_next_level (true);
16306 stmt
= begin_omp_structured_block ();
16308 RECUR (OMP_BODY (t
));
16309 stmt
= finish_omp_structured_block (stmt
);
16312 OMP_BODY (t
) = stmt
;
16313 OMP_CLAUSES (t
) = tmp
;
16314 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16316 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16319 /* For combined target teams, ensure the num_teams and
16320 thread_limit clause expressions are evaluated on the host,
16321 before entering the target construct. */
16323 for (c
= OMP_TEAMS_CLAUSES (teams
);
16324 c
; c
= OMP_CLAUSE_CHAIN (c
))
16325 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16326 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16327 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16329 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16330 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16331 if (expr
== error_mark_node
)
16333 tmp
= TARGET_EXPR_SLOT (expr
);
16335 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16336 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16337 OMP_CLAUSE_FIRSTPRIVATE
);
16338 OMP_CLAUSE_DECL (tc
) = tmp
;
16339 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16340 OMP_TARGET_CLAUSES (t
) = tc
;
16349 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16350 complain
, in_decl
);
16351 OACC_DECLARE_CLAUSES (t
) = tmp
;
16355 case OMP_TARGET_UPDATE
:
16356 case OMP_TARGET_ENTER_DATA
:
16357 case OMP_TARGET_EXIT_DATA
:
16358 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16359 complain
, in_decl
);
16361 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16365 case OACC_ENTER_DATA
:
16366 case OACC_EXIT_DATA
:
16368 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16369 complain
, in_decl
);
16371 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16376 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16377 complain
, in_decl
);
16378 stmt
= push_stmt_list ();
16379 RECUR (OMP_BODY (t
));
16380 stmt
= pop_stmt_list (stmt
);
16383 OMP_BODY (t
) = stmt
;
16384 OMP_ORDERED_CLAUSES (t
) = tmp
;
16390 case OMP_TASKGROUP
:
16391 stmt
= push_stmt_list ();
16392 RECUR (OMP_BODY (t
));
16393 stmt
= pop_stmt_list (stmt
);
16396 OMP_BODY (t
) = stmt
;
16401 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16402 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16404 tree op1
= TREE_OPERAND (t
, 1);
16405 tree rhs1
= NULL_TREE
;
16407 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16409 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16410 op1
= TREE_OPERAND (op1
, 1);
16412 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16413 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16414 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16415 NULL_TREE
, NULL_TREE
, rhs1
,
16416 OMP_ATOMIC_SEQ_CST (t
));
16420 tree op1
= TREE_OPERAND (t
, 1);
16421 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16422 tree rhs1
= NULL_TREE
;
16423 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16424 enum tree_code opcode
= NOP_EXPR
;
16425 if (code
== OMP_ATOMIC_READ
)
16427 v
= RECUR (TREE_OPERAND (op1
, 0));
16428 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16430 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16431 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16433 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16434 v
= RECUR (TREE_OPERAND (op1
, 0));
16435 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16436 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16438 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16439 op11
= TREE_OPERAND (op11
, 1);
16441 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16442 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16443 opcode
= TREE_CODE (op11
);
16444 if (opcode
== MODIFY_EXPR
)
16450 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16451 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16453 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16454 OMP_ATOMIC_SEQ_CST (t
));
16458 case TRANSACTION_EXPR
:
16461 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16462 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16464 if (TRANSACTION_EXPR_IS_STMT (t
))
16466 tree body
= TRANSACTION_EXPR_BODY (t
);
16467 tree noex
= NULL_TREE
;
16468 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16470 noex
= MUST_NOT_THROW_COND (body
);
16471 if (noex
== NULL_TREE
)
16472 noex
= boolean_true_node
;
16473 body
= TREE_OPERAND (body
, 0);
16475 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16477 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16481 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16482 RECUR (TRANSACTION_EXPR_BODY (t
)),
16489 case MUST_NOT_THROW_EXPR
:
16491 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16492 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16493 RETURN (build_must_not_throw_expr (op0
, cond
));
16496 case EXPR_PACK_EXPANSION
:
16497 error ("invalid use of pack expansion expression");
16498 RETURN (error_mark_node
);
16500 case NONTYPE_ARGUMENT_PACK
:
16501 error ("use %<...%> to expand argument pack");
16502 RETURN (error_mark_node
);
16504 case CILK_SPAWN_STMT
:
16505 cfun
->calls_cilk_spawn
= 1;
16506 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
16508 case CILK_SYNC_STMT
:
16509 RETURN (build_cilk_sync ());
16511 case COMPOUND_EXPR
:
16512 tmp
= RECUR (TREE_OPERAND (t
, 0));
16513 if (tmp
== NULL_TREE
)
16514 /* If the first operand was a statement, we're done with it. */
16515 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16516 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16517 RECUR (TREE_OPERAND (t
, 1)),
16520 case ANNOTATE_EXPR
:
16521 tmp
= RECUR (TREE_OPERAND (t
, 0));
16522 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16523 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
16526 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16528 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16529 /*function_p=*/false,
16530 integral_constant_expression_p
));
16533 RETURN (NULL_TREE
);
16535 input_location
= loc
;
16541 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16542 function. For description of the body see comment above
16543 cp_parser_omp_declare_reduction_exprs. */
16546 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16548 if (t
== NULL_TREE
|| t
== error_mark_node
)
16551 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16553 tree_stmt_iterator tsi
;
16556 memset (stmts
, 0, sizeof stmts
);
16557 for (i
= 0, tsi
= tsi_start (t
);
16558 i
< 7 && !tsi_end_p (tsi
);
16559 i
++, tsi_next (&tsi
))
16560 stmts
[i
] = tsi_stmt (tsi
);
16561 gcc_assert (tsi_end_p (tsi
));
16565 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16566 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16567 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16568 args
, complain
, in_decl
);
16569 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16570 args
, complain
, in_decl
);
16571 DECL_CONTEXT (omp_out
) = current_function_decl
;
16572 DECL_CONTEXT (omp_in
) = current_function_decl
;
16573 keep_next_level (true);
16574 tree block
= begin_omp_structured_block ();
16575 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16576 block
= finish_omp_structured_block (block
);
16577 block
= maybe_cleanup_point_expr_void (block
);
16578 add_decl_expr (omp_out
);
16579 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16580 TREE_NO_WARNING (omp_out
) = 1;
16581 add_decl_expr (omp_in
);
16582 finish_expr_stmt (block
);
16586 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16587 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16588 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16589 args
, complain
, in_decl
);
16590 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16591 args
, complain
, in_decl
);
16592 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16593 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16594 keep_next_level (true);
16595 tree block
= begin_omp_structured_block ();
16596 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16597 block
= finish_omp_structured_block (block
);
16598 block
= maybe_cleanup_point_expr_void (block
);
16599 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16600 add_decl_expr (omp_priv
);
16601 add_decl_expr (omp_orig
);
16602 finish_expr_stmt (block
);
16604 add_decl_expr (omp_orig
);
16608 /* T is a postfix-expression that is not being used in a function
16609 call. Return the substituted version of T. */
16612 tsubst_non_call_postfix_expression (tree t
, tree args
,
16613 tsubst_flags_t complain
,
16616 if (TREE_CODE (t
) == SCOPE_REF
)
16617 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16618 /*done=*/false, /*address_p=*/false);
16620 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16621 /*function_p=*/false,
16622 /*integral_constant_expression_p=*/false);
16627 /* Like tsubst but deals with expressions and performs semantic
16628 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16631 tsubst_copy_and_build (tree t
,
16633 tsubst_flags_t complain
,
16636 bool integral_constant_expression_p
)
16638 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16639 #define RECUR(NODE) \
16640 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16641 /*function_p=*/false, \
16642 integral_constant_expression_p)
16647 if (t
== NULL_TREE
|| t
== error_mark_node
)
16650 loc
= input_location
;
16651 if (EXPR_HAS_LOCATION (t
))
16652 input_location
= EXPR_LOCATION (t
);
16654 /* N3276 decltype magic only applies to calls at the top level or on the
16655 right side of a comma. */
16656 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
16657 complain
&= ~tf_decltype
;
16659 switch (TREE_CODE (t
))
16663 /* Fall through. */
16664 case IDENTIFIER_NODE
:
16668 bool non_integral_constant_expression_p
;
16669 const char *error_msg
;
16671 if (IDENTIFIER_TYPENAME_P (t
))
16673 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16674 t
= mangle_conv_op_name_for_type (new_type
);
16677 /* Look up the name. */
16678 decl
= lookup_name (t
);
16680 /* By convention, expressions use ERROR_MARK_NODE to indicate
16681 failure, not NULL_TREE. */
16682 if (decl
== NULL_TREE
)
16683 decl
= error_mark_node
;
16685 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
16687 integral_constant_expression_p
,
16688 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
16689 &non_integral_constant_expression_p
,
16690 /*template_p=*/false,
16692 /*address_p=*/false,
16693 /*template_arg_p=*/false,
16698 if (!function_p
&& identifier_p (decl
))
16700 if (complain
& tf_error
)
16701 unqualified_name_lookup_error (decl
);
16702 decl
= error_mark_node
;
16707 case TEMPLATE_ID_EXPR
:
16710 tree templ
= RECUR (TREE_OPERAND (t
, 0));
16711 tree targs
= TREE_OPERAND (t
, 1);
16714 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16715 if (targs
== error_mark_node
)
16716 return error_mark_node
;
16718 if (TREE_CODE (templ
) == SCOPE_REF
)
16720 tree name
= TREE_OPERAND (templ
, 1);
16721 tree tid
= lookup_template_function (name
, targs
);
16722 TREE_OPERAND (templ
, 1) = tid
;
16726 if (variable_template_p (templ
))
16727 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
16729 if (TREE_CODE (templ
) == COMPONENT_REF
)
16731 object
= TREE_OPERAND (templ
, 0);
16732 templ
= TREE_OPERAND (templ
, 1);
16735 object
= NULL_TREE
;
16736 templ
= lookup_template_function (templ
, targs
);
16739 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
16740 object
, templ
, NULL_TREE
));
16742 RETURN (baselink_for_fns (templ
));
16747 tree r
= RECUR (TREE_OPERAND (t
, 0));
16749 if (REFERENCE_REF_P (t
))
16751 /* A type conversion to reference type will be enclosed in
16752 such an indirect ref, but the substitution of the cast
16753 will have also added such an indirect ref. */
16754 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
16755 r
= convert_from_reference (r
);
16758 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
16759 complain
|decltype_flag
);
16761 if (TREE_CODE (r
) == INDIRECT_REF
)
16762 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16769 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16770 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16771 RETURN (build_nop (type
, op0
));
16774 case IMPLICIT_CONV_EXPR
:
16776 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16777 tree expr
= RECUR (TREE_OPERAND (t
, 0));
16778 int flags
= LOOKUP_IMPLICIT
;
16779 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
16780 flags
= LOOKUP_NORMAL
;
16781 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
16787 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16788 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16789 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
16793 case REINTERPRET_CAST_EXPR
:
16794 case CONST_CAST_EXPR
:
16795 case DYNAMIC_CAST_EXPR
:
16796 case STATIC_CAST_EXPR
:
16799 tree op
, r
= NULL_TREE
;
16801 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16802 if (integral_constant_expression_p
16803 && !cast_valid_in_integral_constant_expression_p (type
))
16805 if (complain
& tf_error
)
16806 error ("a cast to a type other than an integral or "
16807 "enumeration type cannot appear in a constant-expression");
16808 RETURN (error_mark_node
);
16811 op
= RECUR (TREE_OPERAND (t
, 0));
16813 warning_sentinel
s(warn_useless_cast
);
16814 switch (TREE_CODE (t
))
16817 r
= build_functional_cast (type
, op
, complain
);
16819 case REINTERPRET_CAST_EXPR
:
16820 r
= build_reinterpret_cast (type
, op
, complain
);
16822 case CONST_CAST_EXPR
:
16823 r
= build_const_cast (type
, op
, complain
);
16825 case DYNAMIC_CAST_EXPR
:
16826 r
= build_dynamic_cast (type
, op
, complain
);
16828 case STATIC_CAST_EXPR
:
16829 r
= build_static_cast (type
, op
, complain
);
16832 gcc_unreachable ();
16838 case POSTDECREMENT_EXPR
:
16839 case POSTINCREMENT_EXPR
:
16840 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16841 args
, complain
, in_decl
);
16842 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
16843 complain
|decltype_flag
));
16845 case PREDECREMENT_EXPR
:
16846 case PREINCREMENT_EXPR
:
16850 case TRUTH_NOT_EXPR
:
16851 case UNARY_PLUS_EXPR
: /* Unary + */
16852 case REALPART_EXPR
:
16853 case IMAGPART_EXPR
:
16854 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
16855 RECUR (TREE_OPERAND (t
, 0)),
16856 complain
|decltype_flag
));
16858 case FIX_TRUNC_EXPR
:
16859 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
16863 op1
= TREE_OPERAND (t
, 0);
16864 if (TREE_CODE (op1
) == LABEL_DECL
)
16865 RETURN (finish_label_address_expr (DECL_NAME (op1
),
16866 EXPR_LOCATION (op1
)));
16867 if (TREE_CODE (op1
) == SCOPE_REF
)
16868 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16869 /*done=*/true, /*address_p=*/true);
16871 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16873 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16874 complain
|decltype_flag
));
16879 case TRUNC_DIV_EXPR
:
16880 case CEIL_DIV_EXPR
:
16881 case FLOOR_DIV_EXPR
:
16882 case ROUND_DIV_EXPR
:
16883 case EXACT_DIV_EXPR
:
16887 case TRUNC_MOD_EXPR
:
16888 case FLOOR_MOD_EXPR
:
16889 case TRUTH_ANDIF_EXPR
:
16890 case TRUTH_ORIF_EXPR
:
16891 case TRUTH_AND_EXPR
:
16892 case TRUTH_OR_EXPR
:
16908 warning_sentinel
s1(warn_type_limits
);
16909 warning_sentinel
s2(warn_div_by_zero
);
16910 warning_sentinel
s3(warn_logical_op
);
16911 warning_sentinel
s4(warn_tautological_compare
);
16912 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16913 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16914 tree r
= build_x_binary_op
16915 (input_location
, TREE_CODE (t
),
16917 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16919 : TREE_CODE (TREE_OPERAND (t
, 0))),
16921 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16923 : TREE_CODE (TREE_OPERAND (t
, 1))),
16925 complain
|decltype_flag
);
16926 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16927 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16932 case POINTER_PLUS_EXPR
:
16934 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16935 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16936 return fold_build_pointer_plus (op0
, op1
);
16940 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16941 /*address_p=*/false));
16943 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16944 args
, complain
, in_decl
);
16945 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16946 RECUR (TREE_OPERAND (t
, 1)),
16947 complain
|decltype_flag
));
16949 case ARRAY_NOTATION_REF
:
16951 tree start_index
, length
, stride
;
16952 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16953 args
, complain
, in_decl
);
16954 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16955 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16956 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16957 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16958 length
, stride
, TREE_TYPE (op1
)));
16961 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16962 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16963 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16970 op1
= TREE_OPERAND (t
, 0);
16971 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16972 op1
= TREE_TYPE (op1
);
16975 /* When there are no ARGS, we are trying to evaluate a
16976 non-dependent expression from the parser. Trying to do
16977 the substitutions may not work. */
16979 op1
= TREE_TYPE (op1
);
16983 ++cp_unevaluated_operand
;
16984 ++c_inhibit_evaluation_warnings
;
16986 op1
= tsubst (op1
, args
, complain
, in_decl
);
16988 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16989 /*function_p=*/false,
16990 /*integral_constant_expression_p=*/
16992 --cp_unevaluated_operand
;
16993 --c_inhibit_evaluation_warnings
;
16996 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16997 complain
& tf_error
);
16999 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
17000 complain
& tf_error
);
17001 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
17003 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
17005 if (!processing_template_decl
&& TYPE_P (op1
))
17007 r
= build_min (SIZEOF_EXPR
, size_type_node
,
17008 build1 (NOP_EXPR
, op1
, error_mark_node
));
17009 SIZEOF_EXPR_TYPE_P (r
) = 1;
17012 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
17013 TREE_SIDE_EFFECTS (r
) = 0;
17014 TREE_READONLY (r
) = 1;
17016 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17021 case AT_ENCODE_EXPR
:
17023 op1
= TREE_OPERAND (t
, 0);
17024 ++cp_unevaluated_operand
;
17025 ++c_inhibit_evaluation_warnings
;
17026 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17027 /*function_p=*/false,
17028 /*integral_constant_expression_p=*/false);
17029 --cp_unevaluated_operand
;
17030 --c_inhibit_evaluation_warnings
;
17031 RETURN (objc_build_encode_expr (op1
));
17034 case NOEXCEPT_EXPR
:
17035 op1
= TREE_OPERAND (t
, 0);
17036 ++cp_unevaluated_operand
;
17037 ++c_inhibit_evaluation_warnings
;
17038 ++cp_noexcept_operand
;
17039 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17040 /*function_p=*/false,
17041 /*integral_constant_expression_p=*/false);
17042 --cp_unevaluated_operand
;
17043 --c_inhibit_evaluation_warnings
;
17044 --cp_noexcept_operand
;
17045 RETURN (finish_noexcept_expr (op1
, complain
));
17049 warning_sentinel
s(warn_div_by_zero
);
17050 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17051 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17052 tree r
= build_x_modify_expr
17053 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17054 complain
|decltype_flag
);
17055 /* TREE_NO_WARNING must be set if either the expression was
17056 parenthesized or it uses an operator such as >>= rather
17057 than plain assignment. In the former case, it was already
17058 set and must be copied. In the latter case,
17059 build_x_modify_expr sets it and it must not be reset
17061 if (TREE_NO_WARNING (t
))
17062 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17068 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17069 args
, complain
, in_decl
);
17070 /* Remember that there was a reference to this entity. */
17072 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17073 RETURN (error_mark_node
);
17074 RETURN (build_x_arrow (input_location
, op1
, complain
));
17078 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17079 tree init
= RECUR (TREE_OPERAND (t
, 3));
17080 vec
<tree
, va_gc
> *placement_vec
;
17081 vec
<tree
, va_gc
> *init_vec
;
17084 if (placement
== NULL_TREE
)
17085 placement_vec
= NULL
;
17088 placement_vec
= make_tree_vector ();
17089 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17090 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17093 /* If there was an initializer in the original tree, but it
17094 instantiated to an empty list, then we should pass a
17095 non-NULL empty vector to tell build_new that it was an
17096 empty initializer() rather than no initializer. This can
17097 only happen when the initializer is a pack expansion whose
17098 parameter packs are of length zero. */
17099 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17103 init_vec
= make_tree_vector ();
17104 if (init
== void_node
)
17105 gcc_assert (init_vec
!= NULL
);
17108 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17109 vec_safe_push (init_vec
, TREE_VALUE (init
));
17113 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17114 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17115 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17116 NEW_EXPR_USE_GLOBAL (t
),
17119 if (placement_vec
!= NULL
)
17120 release_tree_vector (placement_vec
);
17121 if (init_vec
!= NULL
)
17122 release_tree_vector (init_vec
);
17129 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17130 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17131 RETURN (delete_sanity (op0
, op1
,
17132 DELETE_EXPR_USE_VEC (t
),
17133 DELETE_EXPR_USE_GLOBAL (t
),
17137 case COMPOUND_EXPR
:
17139 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17140 complain
& ~tf_decltype
, in_decl
,
17141 /*function_p=*/false,
17142 integral_constant_expression_p
);
17143 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17145 RECUR (TREE_OPERAND (t
, 1)),
17146 complain
|decltype_flag
));
17152 vec
<tree
, va_gc
> *call_args
;
17153 unsigned int nargs
, i
;
17158 function
= CALL_EXPR_FN (t
);
17159 /* Internal function with no arguments. */
17160 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17163 /* When we parsed the expression, we determined whether or
17164 not Koenig lookup should be performed. */
17165 koenig_p
= KOENIG_LOOKUP_P (t
);
17166 if (function
== NULL_TREE
)
17169 qualified_p
= false;
17171 else if (TREE_CODE (function
) == SCOPE_REF
)
17173 qualified_p
= true;
17174 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17176 /*address_p=*/false);
17178 else if (koenig_p
&& identifier_p (function
))
17180 /* Do nothing; calling tsubst_copy_and_build on an identifier
17181 would incorrectly perform unqualified lookup again.
17183 Note that we can also have an IDENTIFIER_NODE if the earlier
17184 unqualified lookup found a member function; in that case
17185 koenig_p will be false and we do want to do the lookup
17186 again to find the instantiated member function.
17188 FIXME but doing that causes c++/15272, so we need to stop
17189 using IDENTIFIER_NODE in that situation. */
17190 qualified_p
= false;
17194 if (TREE_CODE (function
) == COMPONENT_REF
)
17196 tree op
= TREE_OPERAND (function
, 1);
17198 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17199 || (BASELINK_P (op
)
17200 && BASELINK_QUALIFIED_P (op
)));
17203 qualified_p
= false;
17205 if (TREE_CODE (function
) == ADDR_EXPR
17206 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17207 /* Avoid error about taking the address of a constructor. */
17208 function
= TREE_OPERAND (function
, 0);
17210 function
= tsubst_copy_and_build (function
, args
, complain
,
17213 integral_constant_expression_p
);
17215 if (BASELINK_P (function
))
17216 qualified_p
= true;
17219 nargs
= call_expr_nargs (t
);
17220 call_args
= make_tree_vector ();
17221 for (i
= 0; i
< nargs
; ++i
)
17223 tree arg
= CALL_EXPR_ARG (t
, i
);
17225 if (!PACK_EXPANSION_P (arg
))
17226 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17229 /* Expand the pack expansion and push each entry onto
17231 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17232 if (TREE_CODE (arg
) == TREE_VEC
)
17234 unsigned int len
, j
;
17236 len
= TREE_VEC_LENGTH (arg
);
17237 for (j
= 0; j
< len
; ++j
)
17239 tree value
= TREE_VEC_ELT (arg
, j
);
17240 if (value
!= NULL_TREE
)
17241 value
= convert_from_reference (value
);
17242 vec_safe_push (call_args
, value
);
17247 /* A partial substitution. Add one entry. */
17248 vec_safe_push (call_args
, arg
);
17253 /* We do not perform argument-dependent lookup if normal
17254 lookup finds a non-function, in accordance with the
17255 expected resolution of DR 218. */
17257 && ((is_overloaded_fn (function
)
17258 /* If lookup found a member function, the Koenig lookup is
17259 not appropriate, even if an unqualified-name was used
17260 to denote the function. */
17261 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17262 || identifier_p (function
))
17263 /* Only do this when substitution turns a dependent call
17264 into a non-dependent call. */
17265 && type_dependent_expression_p_push (t
)
17266 && !any_type_dependent_arguments_p (call_args
))
17267 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17269 if (function
!= NULL_TREE
17270 && identifier_p (function
)
17271 && !any_type_dependent_arguments_p (call_args
))
17273 if (koenig_p
&& (complain
& tf_warning_or_error
))
17275 /* For backwards compatibility and good diagnostics, try
17276 the unqualified lookup again if we aren't in SFINAE
17278 tree unq
= (tsubst_copy_and_build
17279 (function
, args
, complain
, in_decl
, true,
17280 integral_constant_expression_p
));
17281 if (unq
== error_mark_node
)
17283 release_tree_vector (call_args
);
17284 RETURN (error_mark_node
);
17287 if (unq
!= function
)
17289 /* In a lambda fn, we have to be careful to not
17290 introduce new this captures. Legacy code can't
17291 be using lambdas anyway, so it's ok to be
17293 bool in_lambda
= (current_class_type
17294 && LAMBDA_TYPE_P (current_class_type
));
17295 char const *const msg
17296 = G_("%qD was not declared in this scope, "
17297 "and no declarations were found by "
17298 "argument-dependent lookup at the point "
17299 "of instantiation");
17303 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17306 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17312 if (INDIRECT_REF_P (fn
))
17313 fn
= TREE_OPERAND (fn
, 0);
17314 if (is_overloaded_fn (fn
))
17315 fn
= get_first_fn (fn
);
17318 /* Can't say anything more. */;
17319 else if (DECL_CLASS_SCOPE_P (fn
))
17321 location_t loc
= EXPR_LOC_OR_LOC (t
,
17324 "declarations in dependent base %qT are "
17325 "not found by unqualified lookup",
17326 DECL_CLASS_CONTEXT (fn
));
17327 if (current_class_ptr
)
17329 "use %<this->%D%> instead", function
);
17332 "use %<%T::%D%> instead",
17333 current_class_name
, function
);
17336 inform (DECL_SOURCE_LOCATION (fn
),
17337 "%qD declared here, later in the "
17338 "translation unit", fn
);
17341 release_tree_vector (call_args
);
17342 RETURN (error_mark_node
);
17349 if (identifier_p (function
))
17351 if (complain
& tf_error
)
17352 unqualified_name_lookup_error (function
);
17353 release_tree_vector (call_args
);
17354 RETURN (error_mark_node
);
17358 /* Remember that there was a reference to this entity. */
17359 if (function
!= NULL_TREE
17360 && DECL_P (function
)
17361 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17363 release_tree_vector (call_args
);
17364 RETURN (error_mark_node
);
17367 /* Put back tf_decltype for the actual call. */
17368 complain
|= decltype_flag
;
17370 if (function
== NULL_TREE
)
17371 switch (CALL_EXPR_IFN (t
))
17374 gcc_assert (nargs
== 1);
17375 if (vec_safe_length (call_args
) != 1)
17377 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17378 "wrong number of arguments to "
17379 "%<__builtin_launder%>");
17380 ret
= error_mark_node
;
17383 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17385 (*call_args
)[0], complain
);
17389 /* Unsupported internal function with arguments. */
17390 gcc_unreachable ();
17392 else if (TREE_CODE (function
) == OFFSET_REF
)
17393 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17395 else if (TREE_CODE (function
) == COMPONENT_REF
)
17397 tree instance
= TREE_OPERAND (function
, 0);
17398 tree fn
= TREE_OPERAND (function
, 1);
17400 if (processing_template_decl
17401 && (type_dependent_expression_p (instance
)
17402 || (!BASELINK_P (fn
)
17403 && TREE_CODE (fn
) != FIELD_DECL
)
17404 || type_dependent_expression_p (fn
)
17405 || any_type_dependent_arguments_p (call_args
)))
17406 ret
= build_min_nt_call_vec (function
, call_args
);
17407 else if (!BASELINK_P (fn
))
17408 ret
= finish_call_expr (function
, &call_args
,
17409 /*disallow_virtual=*/false,
17410 /*koenig_p=*/false,
17413 ret
= (build_new_method_call
17415 &call_args
, NULL_TREE
,
17416 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17421 ret
= finish_call_expr (function
, &call_args
,
17422 /*disallow_virtual=*/qualified_p
,
17426 release_tree_vector (call_args
);
17428 if (ret
!= error_mark_node
)
17430 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17431 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17432 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17433 bool thk
= CALL_FROM_THUNK_P (t
);
17434 if (op
|| ord
|| rev
|| thk
)
17436 function
= extract_call_expr (ret
);
17437 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17438 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17439 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17442 CALL_FROM_THUNK_P (function
) = true;
17443 /* The thunk location is not interesting. */
17444 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17454 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17455 tree folded_cond
= fold_non_dependent_expr (cond
);
17458 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17460 if (integer_zerop (folded_cond
))
17462 ++c_inhibit_evaluation_warnings
;
17463 exp1
= RECUR (TREE_OPERAND (t
, 1));
17464 --c_inhibit_evaluation_warnings
;
17465 exp2
= RECUR (TREE_OPERAND (t
, 2));
17469 exp1
= RECUR (TREE_OPERAND (t
, 1));
17470 ++c_inhibit_evaluation_warnings
;
17471 exp2
= RECUR (TREE_OPERAND (t
, 2));
17472 --c_inhibit_evaluation_warnings
;
17474 cond
= folded_cond
;
17478 exp1
= RECUR (TREE_OPERAND (t
, 1));
17479 exp2
= RECUR (TREE_OPERAND (t
, 2));
17482 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17483 cond
, exp1
, exp2
, complain
));
17486 case PSEUDO_DTOR_EXPR
:
17488 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17489 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17490 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17491 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17497 tree purpose
, value
, chain
;
17499 if (t
== void_list_node
)
17502 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17503 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17505 /* We have pack expansions, so expand those and
17506 create a new list out of it. */
17507 tree purposevec
= NULL_TREE
;
17508 tree valuevec
= NULL_TREE
;
17512 /* Expand the argument expressions. */
17513 if (TREE_PURPOSE (t
))
17514 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17515 complain
, in_decl
);
17516 if (TREE_VALUE (t
))
17517 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17518 complain
, in_decl
);
17520 /* Build the rest of the list. */
17521 chain
= TREE_CHAIN (t
);
17522 if (chain
&& chain
!= void_type_node
)
17523 chain
= RECUR (chain
);
17525 /* Determine the number of arguments. */
17526 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17528 len
= TREE_VEC_LENGTH (purposevec
);
17529 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17531 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17532 len
= TREE_VEC_LENGTH (valuevec
);
17535 /* Since we only performed a partial substitution into
17536 the argument pack, we only RETURN (a single list
17538 if (purposevec
== TREE_PURPOSE (t
)
17539 && valuevec
== TREE_VALUE (t
)
17540 && chain
== TREE_CHAIN (t
))
17543 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17546 /* Convert the argument vectors into a TREE_LIST */
17550 /* Grab the Ith values. */
17552 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17555 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17558 /* Build the list (backwards). */
17559 chain
= tree_cons (purpose
, value
, chain
);
17565 purpose
= TREE_PURPOSE (t
);
17567 purpose
= RECUR (purpose
);
17568 value
= TREE_VALUE (t
);
17570 value
= RECUR (value
);
17571 chain
= TREE_CHAIN (t
);
17572 if (chain
&& chain
!= void_type_node
)
17573 chain
= RECUR (chain
);
17574 if (purpose
== TREE_PURPOSE (t
)
17575 && value
== TREE_VALUE (t
)
17576 && chain
== TREE_CHAIN (t
))
17578 RETURN (tree_cons (purpose
, value
, chain
));
17581 case COMPONENT_REF
:
17588 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17589 args
, complain
, in_decl
);
17590 /* Remember that there was a reference to this entity. */
17591 if (DECL_P (object
)
17592 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17593 RETURN (error_mark_node
);
17594 object_type
= TREE_TYPE (object
);
17596 member
= TREE_OPERAND (t
, 1);
17597 if (BASELINK_P (member
))
17598 member
= tsubst_baselink (member
,
17599 non_reference (TREE_TYPE (object
)),
17600 args
, complain
, in_decl
);
17602 member
= tsubst_copy (member
, args
, complain
, in_decl
);
17603 if (member
== error_mark_node
)
17604 RETURN (error_mark_node
);
17606 if (TREE_CODE (member
) == FIELD_DECL
)
17608 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
17609 if (TREE_CODE (r
) == COMPONENT_REF
)
17610 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17613 else if (type_dependent_expression_p (object
))
17614 /* We can't do much here. */;
17615 else if (!CLASS_TYPE_P (object_type
))
17617 if (scalarish_type_p (object_type
))
17619 tree s
= NULL_TREE
;
17620 tree dtor
= member
;
17622 if (TREE_CODE (dtor
) == SCOPE_REF
)
17624 s
= TREE_OPERAND (dtor
, 0);
17625 dtor
= TREE_OPERAND (dtor
, 1);
17627 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
17629 dtor
= TREE_OPERAND (dtor
, 0);
17631 RETURN (finish_pseudo_destructor_expr
17632 (object
, s
, dtor
, input_location
));
17636 else if (TREE_CODE (member
) == SCOPE_REF
17637 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
17639 /* Lookup the template functions now that we know what the
17641 tree scope
= TREE_OPERAND (member
, 0);
17642 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
17643 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
17644 member
= lookup_qualified_name (scope
, tmpl
,
17645 /*is_type_p=*/false,
17646 /*complain=*/false);
17647 if (BASELINK_P (member
))
17649 BASELINK_FUNCTIONS (member
)
17650 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
17652 member
= (adjust_result_of_qualified_name_lookup
17653 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
17658 qualified_name_lookup_error (scope
, tmpl
, member
,
17660 RETURN (error_mark_node
);
17663 else if (TREE_CODE (member
) == SCOPE_REF
17664 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
17665 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
17667 if (complain
& tf_error
)
17669 if (TYPE_P (TREE_OPERAND (member
, 0)))
17670 error ("%qT is not a class or namespace",
17671 TREE_OPERAND (member
, 0));
17673 error ("%qD is not a class or namespace",
17674 TREE_OPERAND (member
, 0));
17676 RETURN (error_mark_node
);
17679 r
= finish_class_member_access_expr (object
, member
,
17680 /*template_p=*/false,
17682 if (TREE_CODE (r
) == COMPONENT_REF
)
17683 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17688 RETURN (build_throw
17689 (RECUR (TREE_OPERAND (t
, 0))));
17693 vec
<constructor_elt
, va_gc
> *n
;
17694 constructor_elt
*ce
;
17695 unsigned HOST_WIDE_INT idx
;
17696 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17697 bool process_index_p
;
17699 bool need_copy_p
= false;
17702 if (type
== error_mark_node
)
17703 RETURN (error_mark_node
);
17705 /* digest_init will do the wrong thing if we let it. */
17706 if (type
&& TYPE_PTRMEMFUNC_P (type
))
17709 /* We do not want to process the index of aggregate
17710 initializers as they are identifier nodes which will be
17711 looked up by digest_init. */
17712 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
17714 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
17715 newlen
= vec_safe_length (n
);
17716 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
17718 if (ce
->index
&& process_index_p
17719 /* An identifier index is looked up in the type
17720 being initialized, not the current scope. */
17721 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
17722 ce
->index
= RECUR (ce
->index
);
17724 if (PACK_EXPANSION_P (ce
->value
))
17726 /* Substitute into the pack expansion. */
17727 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
17730 if (ce
->value
== error_mark_node
17731 || PACK_EXPANSION_P (ce
->value
))
17733 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
17734 /* Just move the argument into place. */
17735 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
17738 /* Update the length of the final CONSTRUCTOR
17739 arguments vector, and note that we will need to
17741 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
17742 need_copy_p
= true;
17746 ce
->value
= RECUR (ce
->value
);
17751 vec
<constructor_elt
, va_gc
> *old_n
= n
;
17753 vec_alloc (n
, newlen
);
17754 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
17756 if (TREE_CODE (ce
->value
) == TREE_VEC
)
17758 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
17759 for (i
= 0; i
< len
; ++i
)
17760 CONSTRUCTOR_APPEND_ELT (n
, 0,
17761 TREE_VEC_ELT (ce
->value
, i
));
17764 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
17768 r
= build_constructor (init_list_type_node
, n
);
17769 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
17771 if (TREE_HAS_CONSTRUCTOR (t
))
17773 fcl_t cl
= fcl_functional
;
17774 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
17776 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
17779 TREE_TYPE (r
) = type
;
17785 tree operand_0
= TREE_OPERAND (t
, 0);
17786 if (TYPE_P (operand_0
))
17788 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
17789 RETURN (get_typeid (operand_0
, complain
));
17793 operand_0
= RECUR (operand_0
);
17794 RETURN (build_typeid (operand_0
, complain
));
17801 else if (DECL_PACK_P (t
))
17803 /* We don't build decls for an instantiation of a
17804 variadic capture proxy, we instantiate the elements
17806 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
17807 return RECUR (DECL_VALUE_EXPR (t
));
17813 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
17814 /* ??? We're doing a subset of finish_id_expression here. */
17816 && !processing_template_decl
17817 && !cp_unevaluated_operand
17818 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
17819 && CP_DECL_THREAD_LOCAL_P (r
))
17821 if (tree wrap
= get_tls_wrapper_fn (r
))
17822 /* Replace an evaluated use of the thread_local variable with
17823 a call to its wrapper. */
17824 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
17826 else if (outer_automatic_var_p (r
))
17828 r
= process_outer_var_ref (r
, complain
);
17829 if (is_capture_proxy (r
))
17830 register_local_specialization (r
, t
);
17833 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
17834 /* If the original type was a reference, we'll be wrapped in
17835 the appropriate INDIRECT_REF. */
17836 r
= convert_from_reference (r
);
17842 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17843 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17844 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
17847 case OFFSETOF_EXPR
:
17850 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
17851 in_decl
, /*function_p=*/false,
17852 /*integral_constant_expression_p=*/false);
17853 RETURN (finish_offsetof (object_ptr
,
17854 RECUR (TREE_OPERAND (t
, 0)),
17855 EXPR_LOCATION (t
)));
17858 case ADDRESSOF_EXPR
:
17859 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
17860 RECUR (TREE_OPERAND (t
, 0)), complain
));
17864 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
17865 complain
, in_decl
);
17867 tree type2
= TRAIT_EXPR_TYPE2 (t
);
17868 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
17869 type2
= RECUR (type2
);
17871 type2
= tsubst (type2
, args
, complain
, in_decl
);
17873 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
17878 tree old_stmt_expr
= cur_stmt_expr
;
17879 tree stmt_expr
= begin_stmt_expr ();
17881 cur_stmt_expr
= stmt_expr
;
17882 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
17883 integral_constant_expression_p
);
17884 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
17885 cur_stmt_expr
= old_stmt_expr
;
17887 /* If the resulting list of expression statement is empty,
17888 fold it further into void_node. */
17889 if (empty_expr_stmt_p (stmt_expr
))
17890 stmt_expr
= void_node
;
17892 RETURN (stmt_expr
);
17897 tree r
= build_lambda_expr ();
17899 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
17900 LAMBDA_EXPR_CLOSURE (r
) = type
;
17901 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
17903 LAMBDA_EXPR_LOCATION (r
)
17904 = LAMBDA_EXPR_LOCATION (t
);
17905 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17906 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17907 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17908 LAMBDA_EXPR_DISCRIMINATOR (r
)
17909 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
17910 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
17912 /* No substitution needed. */;
17913 else if (VAR_OR_FUNCTION_DECL_P (scope
))
17914 /* For a function or variable scope, we want to use tsubst so that we
17915 don't complain about referring to an auto before deduction. */
17916 scope
= tsubst (scope
, args
, complain
, in_decl
);
17917 else if (TREE_CODE (scope
) == PARM_DECL
)
17919 /* Look up the parameter we want directly, as tsubst_copy
17920 doesn't do what we need. */
17921 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
17922 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
17923 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
17924 parm
= DECL_CHAIN (parm
);
17926 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17927 if (DECL_CONTEXT (scope
) == NULL_TREE
)
17928 DECL_CONTEXT (scope
) = fn
;
17930 else if (TREE_CODE (scope
) == FIELD_DECL
)
17931 /* For a field, use tsubst_copy so that we look up the existing field
17932 rather than build a new one. */
17933 scope
= RECUR (scope
);
17935 gcc_unreachable ();
17936 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
17938 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17939 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17941 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17942 determine_visibility (TYPE_NAME (type
));
17943 /* Now that we know visibility, instantiate the type so we have a
17944 declaration of the op() for later calls to lambda_function. */
17945 complete_type (type
);
17947 if (tree fn
= lambda_function (type
))
17948 LAMBDA_EXPR_RETURN_TYPE (r
) = TREE_TYPE (TREE_TYPE (fn
));
17950 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17952 insert_pending_capture_proxies ();
17954 RETURN (build_lambda_object (r
));
17958 /* We can get here for a constant initializer of non-dependent type.
17959 FIXME stop folding in cp_parser_initializer_clause. */
17961 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
17966 case TRANSACTION_EXPR
:
17967 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17968 integral_constant_expression_p
));
17971 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17973 case VEC_PERM_EXPR
:
17975 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17976 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17977 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17978 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17982 case REQUIRES_EXPR
:
17983 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17986 /* Handle Objective-C++ constructs, if appropriate. */
17989 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17990 in_decl
, /*function_p=*/false);
17994 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
18000 input_location
= loc
;
18004 /* Verify that the instantiated ARGS are valid. For type arguments,
18005 make sure that the type's linkage is ok. For non-type arguments,
18006 make sure they are constants if they are integral or enumerations.
18007 Emit an error under control of COMPLAIN, and return TRUE on error. */
18010 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
18012 if (dependent_template_arg_p (t
))
18014 if (ARGUMENT_PACK_P (t
))
18016 tree vec
= ARGUMENT_PACK_ARGS (t
);
18017 int len
= TREE_VEC_LENGTH (vec
);
18018 bool result
= false;
18021 for (i
= 0; i
< len
; ++i
)
18022 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18026 else if (TYPE_P (t
))
18028 /* [basic.link]: A name with no linkage (notably, the name
18029 of a class or enumeration declared in a local scope)
18030 shall not be used to declare an entity with linkage.
18031 This implies that names with no linkage cannot be used as
18034 DR 757 relaxes this restriction for C++0x. */
18035 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18036 : no_linkage_check (t
, /*relaxed_p=*/false));
18040 /* DR 488 makes use of a type with no linkage cause
18041 type deduction to fail. */
18042 if (complain
& tf_error
)
18044 if (TYPE_UNNAMED_P (nt
))
18045 error ("%qT is/uses unnamed type", t
);
18047 error ("template argument for %qD uses local type %qT",
18052 /* In order to avoid all sorts of complications, we do not
18053 allow variably-modified types as template arguments. */
18054 else if (variably_modified_type_p (t
, NULL_TREE
))
18056 if (complain
& tf_error
)
18057 error ("%qT is a variably modified type", t
);
18061 /* Class template and alias template arguments should be OK. */
18062 else if (DECL_TYPE_TEMPLATE_P (t
))
18064 /* A non-type argument of integral or enumerated type must be a
18066 else if (TREE_TYPE (t
)
18067 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18068 && !REFERENCE_REF_P (t
)
18069 && !TREE_CONSTANT (t
))
18071 if (complain
& tf_error
)
18072 error ("integral expression %qE is not constant", t
);
18079 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18081 int ix
, len
= DECL_NTPARMS (tmpl
);
18082 bool result
= false;
18084 for (ix
= 0; ix
!= len
; ix
++)
18086 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18089 if (result
&& (complain
& tf_error
))
18090 error (" trying to instantiate %qD", tmpl
);
18094 /* We're out of SFINAE context now, so generate diagnostics for the access
18095 errors we saw earlier when instantiating D from TMPL and ARGS. */
18098 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18100 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18101 tree type
= TREE_TYPE (pattern
);
18102 location_t loc
= input_location
;
18104 push_access_scope (d
);
18105 push_deferring_access_checks (dk_no_deferred
);
18106 input_location
= DECL_SOURCE_LOCATION (pattern
);
18107 tsubst (type
, args
, tf_warning_or_error
, d
);
18108 input_location
= loc
;
18109 pop_deferring_access_checks ();
18110 pop_access_scope (d
);
18113 /* Instantiate the indicated variable, function, or alias template TMPL with
18114 the template arguments in TARG_PTR. */
18117 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18119 tree targ_ptr
= orig_args
;
18123 bool access_ok
= true;
18125 if (tmpl
== error_mark_node
)
18126 return error_mark_node
;
18128 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18130 /* If this function is a clone, handle it specially. */
18131 if (DECL_CLONED_FUNCTION_P (tmpl
))
18136 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18137 DECL_CLONED_FUNCTION. */
18138 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18139 targ_ptr
, complain
);
18140 if (spec
== error_mark_node
)
18141 return error_mark_node
;
18143 /* Look for the clone. */
18144 FOR_EACH_CLONE (clone
, spec
)
18145 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18147 /* We should always have found the clone by now. */
18148 gcc_unreachable ();
18152 if (targ_ptr
== error_mark_node
)
18153 return error_mark_node
;
18155 /* Check to see if we already have this specialization. */
18156 gen_tmpl
= most_general_template (tmpl
);
18157 if (TMPL_ARGS_DEPTH (targ_ptr
)
18158 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18159 /* targ_ptr only has the innermost template args, so add the outer ones
18160 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18161 the case of a non-dependent call within a template definition). */
18162 targ_ptr
= (add_outermost_template_args
18163 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18166 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18167 but it doesn't seem to be on the hot path. */
18168 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18170 gcc_assert (tmpl
== gen_tmpl
18171 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18173 || fndecl
== NULL_TREE
);
18175 if (spec
!= NULL_TREE
)
18177 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18179 if (complain
& tf_error
)
18180 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18181 return error_mark_node
;
18186 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18188 return error_mark_node
;
18190 /* We are building a FUNCTION_DECL, during which the access of its
18191 parameters and return types have to be checked. However this
18192 FUNCTION_DECL which is the desired context for access checking
18193 is not built yet. We solve this chicken-and-egg problem by
18194 deferring all checks until we have the FUNCTION_DECL. */
18195 push_deferring_access_checks (dk_deferred
);
18197 /* Instantiation of the function happens in the context of the function
18198 template, not the context of the overload resolution we're doing. */
18199 push_to_top_level ();
18200 /* If there are dependent arguments, e.g. because we're doing partial
18201 ordering, make sure processing_template_decl stays set. */
18202 if (uses_template_parms (targ_ptr
))
18203 ++processing_template_decl
;
18204 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18206 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18207 complain
, gen_tmpl
, true);
18208 push_nested_class (ctx
);
18211 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18213 fndecl
= NULL_TREE
;
18214 if (VAR_P (pattern
))
18216 /* We need to determine if we're using a partial or explicit
18217 specialization now, because the type of the variable could be
18219 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18220 tree elt
= most_specialized_partial_spec (tid
, complain
);
18221 if (elt
== error_mark_node
)
18222 pattern
= error_mark_node
;
18225 tree partial_tmpl
= TREE_VALUE (elt
);
18226 tree partial_args
= TREE_PURPOSE (elt
);
18227 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18228 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18232 /* Substitute template parameters to obtain the specialization. */
18233 if (fndecl
== NULL_TREE
)
18234 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18235 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18236 pop_nested_class ();
18237 pop_from_top_level ();
18239 if (fndecl
== error_mark_node
)
18241 pop_deferring_access_checks ();
18242 return error_mark_node
;
18245 /* The DECL_TI_TEMPLATE should always be the immediate parent
18246 template, not the most general template. */
18247 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18248 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18250 /* Now we know the specialization, compute access previously
18251 deferred. Do no access control for inheriting constructors,
18252 as we already checked access for the inherited constructor. */
18253 if (!(flag_new_inheriting_ctors
18254 && DECL_INHERITED_CTOR (fndecl
)))
18256 push_access_scope (fndecl
);
18257 if (!perform_deferred_access_checks (complain
))
18259 pop_access_scope (fndecl
);
18261 pop_deferring_access_checks ();
18263 /* If we've just instantiated the main entry point for a function,
18264 instantiate all the alternate entry points as well. We do this
18265 by cloning the instantiation of the main entry point, not by
18266 instantiating the template clones. */
18267 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18268 clone_function_decl (fndecl
, /*update_methods=*/false);
18272 if (!(complain
& tf_error
))
18274 /* Remember to reinstantiate when we're out of SFINAE so the user
18275 can see the errors. */
18276 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18278 return error_mark_node
;
18283 /* Wrapper for instantiate_template_1. */
18286 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18289 timevar_push (TV_TEMPLATE_INST
);
18290 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18291 timevar_pop (TV_TEMPLATE_INST
);
18295 /* Instantiate the alias template TMPL with ARGS. Also push a template
18296 instantiation level, which instantiate_template doesn't do because
18297 functions and variables have sufficient context established by the
18301 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18303 struct pending_template
*old_last_pend
= last_pending_template
;
18304 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18305 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18306 return error_mark_node
;
18307 tree tinst
= build_tree_list (tmpl
, args
);
18308 if (!push_tinst_level (tinst
))
18311 return error_mark_node
;
18315 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18316 args
, tmpl
, complain
,
18317 /*require_all_args=*/true,
18318 /*use_default_args=*/true);
18320 tree r
= instantiate_template (tmpl
, args
, complain
);
18321 pop_tinst_level ();
18322 /* We can't free this if a pending_template entry or last_error_tinst_level
18323 is pointing at it. */
18324 if (last_pending_template
== old_last_pend
18325 && last_error_tinst_level
== old_error_tinst
)
18331 /* PARM is a template parameter pack for FN. Returns true iff
18332 PARM is used in a deducible way in the argument list of FN. */
18335 pack_deducible_p (tree parm
, tree fn
)
18337 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18338 for (; t
; t
= TREE_CHAIN (t
))
18340 tree type
= TREE_VALUE (t
);
18342 if (!PACK_EXPANSION_P (type
))
18344 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18345 packs
; packs
= TREE_CHAIN (packs
))
18346 if (template_args_equal (TREE_VALUE (packs
), parm
))
18348 /* The template parameter pack is used in a function parameter
18349 pack. If this is the end of the parameter list, the
18350 template parameter pack is deducible. */
18351 if (TREE_CHAIN (t
) == void_list_node
)
18354 /* Otherwise, not. Well, it could be deduced from
18355 a non-pack parameter, but doing so would end up with
18356 a deduction mismatch, so don't bother. */
18360 /* The template parameter pack isn't used in any function parameter
18361 packs, but it might be used deeper, e.g. tuple<Args...>. */
18365 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18366 NARGS elements of the arguments that are being used when calling
18367 it. TARGS is a vector into which the deduced template arguments
18370 Returns either a FUNCTION_DECL for the matching specialization of FN or
18371 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18372 true, diagnostics will be printed to explain why it failed.
18374 If FN is a conversion operator, or we are trying to produce a specific
18375 specialization, RETURN_TYPE is the return type desired.
18377 The EXPLICIT_TARGS are explicit template arguments provided via a
18380 The parameter STRICT is one of:
18383 We are deducing arguments for a function call, as in
18384 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18385 deducing arguments for a call to the result of a conversion
18386 function template, as in [over.call.object].
18389 We are deducing arguments for a conversion function, as in
18390 [temp.deduct.conv].
18393 We are deducing arguments when doing an explicit instantiation
18394 as in [temp.explicit], when determining an explicit specialization
18395 as in [temp.expl.spec], or when taking the address of a function
18396 template, as in [temp.deduct.funcaddr]. */
18399 fn_type_unification (tree fn
,
18400 tree explicit_targs
,
18403 unsigned int nargs
,
18405 unification_kind_t strict
,
18412 tree decl
= NULL_TREE
;
18413 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18415 static int deduction_depth
;
18416 struct pending_template
*old_last_pend
= last_pending_template
;
18417 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18420 if (flag_new_inheriting_ctors
)
18421 fn
= strip_inheriting_ctors (fn
);
18423 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18425 tree r
= error_mark_node
;
18427 tree full_targs
= targs
;
18428 if (TMPL_ARGS_DEPTH (targs
)
18429 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18430 full_targs
= (add_outermost_template_args
18431 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18435 complain
|= tf_decltype
;
18437 /* In C++0x, it's possible to have a function template whose type depends
18438 on itself recursively. This is most obvious with decltype, but can also
18439 occur with enumeration scope (c++/48969). So we need to catch infinite
18440 recursion and reject the substitution at deduction time; this function
18441 will return error_mark_node for any repeated substitution.
18443 This also catches excessive recursion such as when f<N> depends on
18444 f<N-1> across all integers, and returns error_mark_node for all the
18445 substitutions back up to the initial one.
18447 This is, of course, not reentrant. */
18448 if (excessive_deduction_depth
)
18449 return error_mark_node
;
18450 tinst
= build_tree_list (fn
, NULL_TREE
);
18453 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18455 fntype
= TREE_TYPE (fn
);
18456 if (explicit_targs
)
18460 The specified template arguments must match the template
18461 parameters in kind (i.e., type, nontype, template), and there
18462 must not be more arguments than there are parameters;
18463 otherwise type deduction fails.
18465 Nontype arguments must match the types of the corresponding
18466 nontype template parameters, or must be convertible to the
18467 types of the corresponding nontype parameters as specified in
18468 _temp.arg.nontype_, otherwise type deduction fails.
18470 All references in the function type of the function template
18471 to the corresponding template parameters are replaced by the
18472 specified template argument values. If a substitution in a
18473 template parameter or in the function type of the function
18474 template results in an invalid type, type deduction fails. */
18475 int i
, len
= TREE_VEC_LENGTH (tparms
);
18476 location_t loc
= input_location
;
18477 bool incomplete
= false;
18479 if (explicit_targs
== error_mark_node
)
18482 if (TMPL_ARGS_DEPTH (explicit_targs
)
18483 < TMPL_ARGS_DEPTH (full_targs
))
18484 explicit_targs
= add_outermost_template_args (full_targs
,
18487 /* Adjust any explicit template arguments before entering the
18488 substitution context. */
18490 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18492 /*require_all_args=*/false,
18493 /*use_default_args=*/false));
18494 if (explicit_targs
== error_mark_node
)
18497 /* Substitute the explicit args into the function type. This is
18498 necessary so that, for instance, explicitly declared function
18499 arguments can match null pointed constants. If we were given
18500 an incomplete set of explicit args, we must not do semantic
18501 processing during substitution as we could create partial
18503 for (i
= 0; i
< len
; i
++)
18505 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18506 bool parameter_pack
= false;
18507 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18509 /* Dig out the actual parm. */
18510 if (TREE_CODE (parm
) == TYPE_DECL
18511 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18513 parm
= TREE_TYPE (parm
);
18514 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18516 else if (TREE_CODE (parm
) == PARM_DECL
)
18518 parm
= DECL_INITIAL (parm
);
18519 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18522 if (!parameter_pack
&& targ
== NULL_TREE
)
18523 /* No explicit argument for this template parameter. */
18526 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18528 /* Mark the argument pack as "incomplete". We could
18529 still deduce more arguments during unification.
18530 We remove this mark in type_unification_real. */
18533 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18534 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18535 = ARGUMENT_PACK_ARGS (targ
);
18538 /* We have some incomplete argument packs. */
18543 TREE_VALUE (tinst
) = explicit_targs
;
18544 if (!push_tinst_level (tinst
))
18546 excessive_deduction_depth
= true;
18549 processing_template_decl
+= incomplete
;
18550 input_location
= DECL_SOURCE_LOCATION (fn
);
18551 /* Ignore any access checks; we'll see them again in
18552 instantiate_template and they might have the wrong
18553 access path at this point. */
18554 push_deferring_access_checks (dk_deferred
);
18555 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18556 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18557 pop_deferring_access_checks ();
18558 input_location
= loc
;
18559 processing_template_decl
-= incomplete
;
18560 pop_tinst_level ();
18562 if (fntype
== error_mark_node
)
18565 /* Place the explicitly specified arguments in TARGS. */
18566 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18567 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18568 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18571 /* Never do unification on the 'this' parameter. */
18572 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18574 if (return_type
&& strict
== DEDUCE_CALL
)
18576 /* We're deducing for a call to the result of a template conversion
18577 function. The parms we really want are in return_type. */
18578 if (POINTER_TYPE_P (return_type
))
18579 return_type
= TREE_TYPE (return_type
);
18580 parms
= TYPE_ARG_TYPES (return_type
);
18582 else if (return_type
)
18586 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18587 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18588 new_args
[0] = return_type
;
18589 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18594 /* We allow incomplete unification without an error message here
18595 because the standard doesn't seem to explicitly prohibit it. Our
18596 callers must be ready to deal with unification failures in any
18599 TREE_VALUE (tinst
) = targs
;
18600 /* If we aren't explaining yet, push tinst context so we can see where
18601 any errors (e.g. from class instantiations triggered by instantiation
18602 of default template arguments) come from. If we are explaining, this
18603 context is redundant. */
18604 if (!explain_p
&& !push_tinst_level (tinst
))
18606 excessive_deduction_depth
= true;
18610 /* type_unification_real will pass back any access checks from default
18611 template argument substitution. */
18612 vec
<deferred_access_check
, va_gc
> *checks
;
18615 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18616 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18617 strict
, flags
, &checks
, explain_p
);
18619 pop_tinst_level ();
18623 /* Now that we have bindings for all of the template arguments,
18624 ensure that the arguments deduced for the template template
18625 parameters have compatible template parameter lists. We cannot
18626 check this property before we have deduced all template
18627 arguments, because the template parameter types of a template
18628 template parameter might depend on prior template parameters
18629 deduced after the template template parameter. The following
18630 ill-formed example illustrates this issue:
18632 template<typename T, template<T> class C> void f(C<5>, T);
18634 template<int N> struct X {};
18637 f(X<5>(), 5l); // error: template argument deduction fails
18640 The template parameter list of 'C' depends on the template type
18641 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18642 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18643 time that we deduce 'C'. */
18644 if (!template_template_parm_bindings_ok_p
18645 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18647 unify_inconsistent_template_template_parameters (explain_p
);
18651 /* All is well so far. Now, check:
18655 When all template arguments have been deduced, all uses of
18656 template parameters in nondeduced contexts are replaced with
18657 the corresponding deduced argument values. If the
18658 substitution results in an invalid type, as described above,
18659 type deduction fails. */
18660 TREE_VALUE (tinst
) = targs
;
18661 if (!push_tinst_level (tinst
))
18663 excessive_deduction_depth
= true;
18667 /* Also collect access checks from the instantiation. */
18668 reopen_deferring_access_checks (checks
);
18670 decl
= instantiate_template (fn
, targs
, complain
);
18672 checks
= get_deferred_access_checks ();
18673 pop_deferring_access_checks ();
18675 pop_tinst_level ();
18677 if (decl
== error_mark_node
)
18680 /* Now perform any access checks encountered during substitution. */
18681 push_access_scope (decl
);
18682 ok
= perform_access_checks (checks
, complain
);
18683 pop_access_scope (decl
);
18687 /* If we're looking for an exact match, check that what we got
18688 is indeed an exact match. It might not be if some template
18689 parameters are used in non-deduced contexts. But don't check
18690 for an exact match if we have dependent template arguments;
18691 in that case we're doing partial ordering, and we already know
18692 that we have two candidates that will provide the actual type. */
18693 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
18695 tree substed
= TREE_TYPE (decl
);
18699 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
18701 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
18702 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
18703 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
18705 unify_type_mismatch (explain_p
, args
[i
],
18706 TREE_VALUE (sarg
));
18711 /* After doing deduction with the inherited constructor, actually return an
18712 instantiation of the inheriting constructor. */
18714 decl
= instantiate_template (orig_fn
, targs
, complain
);
18720 if (excessive_deduction_depth
)
18722 if (deduction_depth
== 0)
18723 /* Reset once we're all the way out. */
18724 excessive_deduction_depth
= false;
18727 /* We can't free this if a pending_template entry or last_error_tinst_level
18728 is pointing at it. */
18729 if (last_pending_template
== old_last_pend
18730 && last_error_tinst_level
== old_error_tinst
)
18736 /* Adjust types before performing type deduction, as described in
18737 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18738 sections are symmetric. PARM is the type of a function parameter
18739 or the return type of the conversion function. ARG is the type of
18740 the argument passed to the call, or the type of the value
18741 initialized with the result of the conversion function.
18742 ARG_EXPR is the original argument expression, which may be null. */
18745 maybe_adjust_types_for_deduction (unification_kind_t strict
,
18758 /* Swap PARM and ARG throughout the remainder of this
18759 function; the handling is precisely symmetric since PARM
18760 will initialize ARG rather than vice versa. */
18761 std::swap (parm
, arg
);
18765 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18766 too, but here handle it by stripping the reference from PARM
18767 rather than by adding it to ARG. */
18768 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18769 && TYPE_REF_IS_RVALUE (*parm
)
18770 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18771 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18772 && TREE_CODE (*arg
) == REFERENCE_TYPE
18773 && !TYPE_REF_IS_RVALUE (*arg
))
18774 *parm
= TREE_TYPE (*parm
);
18775 /* Nothing else to do in this case. */
18779 gcc_unreachable ();
18782 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
18784 /* [temp.deduct.call]
18786 If P is not a reference type:
18788 --If A is an array type, the pointer type produced by the
18789 array-to-pointer standard conversion (_conv.array_) is
18790 used in place of A for type deduction; otherwise,
18792 --If A is a function type, the pointer type produced by
18793 the function-to-pointer standard conversion
18794 (_conv.func_) is used in place of A for type deduction;
18797 --If A is a cv-qualified type, the top level
18798 cv-qualifiers of A's type are ignored for type
18800 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
18801 *arg
= build_pointer_type (TREE_TYPE (*arg
));
18802 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
18803 *arg
= build_pointer_type (*arg
);
18805 *arg
= TYPE_MAIN_VARIANT (*arg
);
18808 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18809 reference to a cv-unqualified template parameter that does not represent a
18810 template parameter of a class template (during class template argument
18811 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18812 an lvalue, the type "lvalue reference to A" is used in place of A for type
18814 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18815 && TYPE_REF_IS_RVALUE (*parm
)
18816 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18817 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
18818 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18819 && (arg_expr
? lvalue_p (arg_expr
)
18820 /* try_one_overload doesn't provide an arg_expr, but
18821 functions are always lvalues. */
18822 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
18823 *arg
= build_reference_type (*arg
);
18825 /* [temp.deduct.call]
18827 If P is a cv-qualified type, the top level cv-qualifiers
18828 of P's type are ignored for type deduction. If P is a
18829 reference type, the type referred to by P is used for
18831 *parm
= TYPE_MAIN_VARIANT (*parm
);
18832 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
18834 *parm
= TREE_TYPE (*parm
);
18835 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18838 /* DR 322. For conversion deduction, remove a reference type on parm
18839 too (which has been swapped into ARG). */
18840 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
18841 *arg
= TREE_TYPE (*arg
);
18846 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18847 template which does contain any deducible template parameters; check if
18848 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18849 unify_one_argument. */
18852 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
18853 int flags
, bool explain_p
)
18858 type
= TREE_TYPE (arg
);
18862 if (same_type_p (parm
, type
))
18863 return unify_success (explain_p
);
18865 if (strict
== DEDUCE_CONV
)
18867 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
18868 explain_p
? tf_warning_or_error
: tf_none
))
18869 return unify_success (explain_p
);
18871 else if (strict
!= DEDUCE_EXACT
)
18873 if (can_convert_arg (parm
, type
,
18874 TYPE_P (arg
) ? NULL_TREE
: arg
,
18875 flags
, explain_p
? tf_warning_or_error
: tf_none
))
18876 return unify_success (explain_p
);
18879 if (strict
== DEDUCE_EXACT
)
18880 return unify_type_mismatch (explain_p
, parm
, arg
);
18882 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
18885 static bool uses_deducible_template_parms (tree type
);
18887 /* Returns true iff the expression EXPR is one from which a template
18888 argument can be deduced. In other words, if it's an undecorated
18889 use of a template non-type parameter. */
18892 deducible_expression (tree expr
)
18894 /* Strip implicit conversions. */
18895 while (CONVERT_EXPR_P (expr
))
18896 expr
= TREE_OPERAND (expr
, 0);
18897 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
18900 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18901 deducible way; that is, if it has a max value of <PARM> - 1. */
18904 deducible_array_bound (tree domain
)
18906 if (domain
== NULL_TREE
)
18909 tree max
= TYPE_MAX_VALUE (domain
);
18910 if (TREE_CODE (max
) != MINUS_EXPR
)
18913 return deducible_expression (TREE_OPERAND (max
, 0));
18916 /* Returns true iff the template arguments ARGS use a template parameter
18917 in a deducible way. */
18920 deducible_template_args (tree args
)
18922 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
18925 tree elt
= TREE_VEC_ELT (args
, i
);
18926 if (ARGUMENT_PACK_P (elt
))
18927 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
18930 if (PACK_EXPANSION_P (elt
))
18931 elt
= PACK_EXPANSION_PATTERN (elt
);
18932 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
18934 else if (TYPE_P (elt
))
18935 deducible
= uses_deducible_template_parms (elt
);
18937 deducible
= deducible_expression (elt
);
18945 /* Returns true iff TYPE contains any deducible references to template
18946 parameters, as per 14.8.2.5. */
18949 uses_deducible_template_parms (tree type
)
18951 if (PACK_EXPANSION_P (type
))
18952 type
= PACK_EXPANSION_PATTERN (type
);
18959 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18960 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18966 if (POINTER_TYPE_P (type
))
18967 return uses_deducible_template_parms (TREE_TYPE (type
));
18969 /* T[integer-constant ]
18971 if (TREE_CODE (type
) == ARRAY_TYPE
)
18972 return (uses_deducible_template_parms (TREE_TYPE (type
))
18973 || deducible_array_bound (TYPE_DOMAIN (type
)));
18985 if (TYPE_PTRMEM_P (type
))
18986 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
18987 || (uses_deducible_template_parms
18988 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
18990 /* template-name <T> (where template-name refers to a class template)
18991 template-name <i> (where template-name refers to a class template) */
18992 if (CLASS_TYPE_P (type
)
18993 && CLASSTYPE_TEMPLATE_INFO (type
)
18994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
18995 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18996 (CLASSTYPE_TI_ARGS (type
)));
19001 if (TREE_CODE (type
) == FUNCTION_TYPE
19002 || TREE_CODE (type
) == METHOD_TYPE
)
19004 if (uses_deducible_template_parms (TREE_TYPE (type
)))
19006 tree parm
= TYPE_ARG_TYPES (type
);
19007 if (TREE_CODE (type
) == METHOD_TYPE
)
19008 parm
= TREE_CHAIN (parm
);
19009 for (; parm
; parm
= TREE_CHAIN (parm
))
19010 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19017 /* Subroutine of type_unification_real and unify_pack_expansion to
19018 handle unification of a single P/A pair. Parameters are as
19019 for those functions. */
19022 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19023 int subr
, unification_kind_t strict
,
19026 tree arg_expr
= NULL_TREE
;
19029 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19030 return unify_invalid (explain_p
);
19031 if (arg
== unknown_type_node
)
19032 /* We can't deduce anything from this, but we might get all the
19033 template args from other function args. */
19034 return unify_success (explain_p
);
19036 /* Implicit conversions (Clause 4) will be performed on a function
19037 argument to convert it to the type of the corresponding function
19038 parameter if the parameter type contains no template-parameters that
19039 participate in template argument deduction. */
19040 if (strict
!= DEDUCE_EXACT
19041 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19042 /* For function parameters with no deducible template parameters,
19043 just return. We'll check non-dependent conversions later. */
19044 return unify_success (explain_p
);
19049 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19050 | UNIFY_ALLOW_MORE_CV_QUAL
19051 | UNIFY_ALLOW_DERIVED
);
19055 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19059 arg_strict
= UNIFY_ALLOW_NONE
;
19063 gcc_unreachable ();
19066 /* We only do these transformations if this is the top-level
19067 parameter_type_list in a call or declaration matching; in other
19068 situations (nested function declarators, template argument lists) we
19069 won't be comparing a type to an expression, and we don't do any type
19075 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19076 if (type_unknown_p (arg
))
19078 /* [temp.deduct.type] A template-argument can be
19079 deduced from a pointer to function or pointer
19080 to member function argument if the set of
19081 overloaded functions does not contain function
19082 templates and at most one of a set of
19083 overloaded functions provides a unique
19086 if (resolve_overloaded_unification
19087 (tparms
, targs
, parm
, arg
, strict
,
19088 arg_strict
, explain_p
))
19089 return unify_success (explain_p
);
19090 return unify_overload_resolution_failure (explain_p
, arg
);
19094 arg
= unlowered_expr_type (arg
);
19095 if (arg
== error_mark_node
)
19096 return unify_invalid (explain_p
);
19100 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19103 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19104 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19105 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19107 /* For deduction from an init-list we need the actual list. */
19108 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19110 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19113 /* for_each_template_parm callback that always returns 0. */
19116 zero_r (tree
, void *)
19121 /* for_each_template_parm any_fn callback to handle deduction of a template
19122 type argument from the type of an array bound. */
19125 array_deduction_r (tree t
, void *data
)
19127 tree_pair_p d
= (tree_pair_p
)data
;
19128 tree
&tparms
= d
->purpose
;
19129 tree
&targs
= d
->value
;
19131 if (TREE_CODE (t
) == ARRAY_TYPE
)
19132 if (tree dom
= TYPE_DOMAIN (t
))
19133 if (tree max
= TYPE_MAX_VALUE (dom
))
19135 if (TREE_CODE (max
) == MINUS_EXPR
)
19136 max
= TREE_OPERAND (max
, 0);
19137 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19138 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19139 UNIFY_ALLOW_NONE
, /*explain*/false);
19142 /* Keep walking. */
19146 /* Try to deduce any not-yet-deduced template type arguments from the type of
19147 an array bound. This is handled separately from unify because 14.8.2.5 says
19148 "The type of a type parameter is only deduced from an array bound if it is
19149 not otherwise deduced." */
19152 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19154 tree_pair_s data
= { tparms
, targs
};
19155 hash_set
<tree
> visited
;
19156 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19157 /*nondeduced*/false, array_deduction_r
);
19160 /* Most parms like fn_type_unification.
19162 If SUBR is 1, we're being called recursively (to unify the
19163 arguments of a function or method parameter of a function
19166 CHECKS is a pointer to a vector of access checks encountered while
19167 substituting default template arguments. */
19170 type_unification_real (tree tparms
,
19174 unsigned int xnargs
,
19176 unification_kind_t strict
,
19178 vec
<deferred_access_check
, va_gc
> **checks
,
19183 int ntparms
= TREE_VEC_LENGTH (tparms
);
19184 int saw_undeduced
= 0;
19187 unsigned int nargs
;
19190 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19191 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19192 gcc_assert (ntparms
> 0);
19194 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19196 /* Reset the number of non-defaulted template arguments contained
19198 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19206 while (parms
&& parms
!= void_list_node
19209 parm
= TREE_VALUE (parms
);
19211 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19212 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19213 /* For a function parameter pack that occurs at the end of the
19214 parameter-declaration-list, the type A of each remaining
19215 argument of the call is compared with the type P of the
19216 declarator-id of the function parameter pack. */
19219 parms
= TREE_CHAIN (parms
);
19221 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19222 /* For a function parameter pack that does not occur at the
19223 end of the parameter-declaration-list, the type of the
19224 parameter pack is a non-deduced context. */
19230 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19236 && parms
!= void_list_node
19237 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19239 /* Unify the remaining arguments with the pack expansion type. */
19241 tree parmvec
= make_tree_vec (1);
19243 /* Allocate a TREE_VEC and copy in all of the arguments */
19244 argvec
= make_tree_vec (nargs
- ia
);
19245 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19246 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19248 /* Copy the parameter into parmvec. */
19249 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19250 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19251 /*subr=*/subr
, explain_p
))
19254 /* Advance to the end of the list of parameters. */
19255 parms
= TREE_CHAIN (parms
);
19258 /* Fail if we've reached the end of the parm list, and more args
19259 are present, and the parm list isn't variadic. */
19260 if (ia
< nargs
&& parms
== void_list_node
)
19261 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19262 /* Fail if parms are left and they don't have default values and
19263 they aren't all deduced as empty packs (c++/57397). This is
19264 consistent with sufficient_parms_p. */
19265 if (parms
&& parms
!= void_list_node
19266 && TREE_PURPOSE (parms
) == NULL_TREE
)
19268 unsigned int count
= nargs
;
19273 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19276 p
= TREE_CHAIN (p
);
19278 while (p
&& p
!= void_list_node
);
19279 if (count
!= nargs
)
19280 return unify_too_few_arguments (explain_p
, ia
, count
,
19286 tsubst_flags_t complain
= (explain_p
19287 ? tf_warning_or_error
19289 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
19291 for (i
= 0; i
< ntparms
; i
++)
19293 tree targ
= TREE_VEC_ELT (targs
, i
);
19294 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19296 /* Clear the "incomplete" flags on all argument packs now so that
19297 substituting them into later default arguments works. */
19298 if (targ
&& ARGUMENT_PACK_P (targ
))
19300 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19301 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19304 if (targ
|| tparm
== error_mark_node
)
19306 tparm
= TREE_VALUE (tparm
);
19308 if (TREE_CODE (tparm
) == TYPE_DECL
19309 && !tried_array_deduction
)
19311 try_array_deduction (tparms
, targs
, xparms
);
19312 tried_array_deduction
= true;
19313 if (TREE_VEC_ELT (targs
, i
))
19317 /* If this is an undeduced nontype parameter that depends on
19318 a type parameter, try another pass; its type may have been
19319 deduced from a later argument than the one from which
19320 this parameter can be deduced. */
19321 if (TREE_CODE (tparm
) == PARM_DECL
19322 && uses_template_parms (TREE_TYPE (tparm
))
19323 && saw_undeduced
< 2)
19329 /* Core issue #226 (C++0x) [temp.deduct]:
19331 If a template argument has not been deduced, its
19332 default template argument, if any, is used.
19334 When we are in C++98 mode, TREE_PURPOSE will either
19335 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19336 to explicitly check cxx_dialect here. */
19337 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19338 /* OK, there is a default argument. Wait until after the
19339 conversion check to do substitution. */
19342 /* If the type parameter is a parameter pack, then it will
19343 be deduced to an empty parameter pack. */
19344 if (template_parameter_pack_p (tparm
))
19348 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19350 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19351 TREE_CONSTANT (arg
) = 1;
19354 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19356 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19358 TREE_VEC_ELT (targs
, i
) = arg
;
19362 return unify_parameter_deduction_failure (explain_p
, tparm
);
19365 /* DR 1391: All parameters have args, now check non-dependent parms for
19367 if (saw_undeduced
< 2)
19368 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19369 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19371 parm
= TREE_VALUE (parms
);
19373 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19374 && (!TREE_CHAIN (parms
)
19375 || TREE_CHAIN (parms
) == void_list_node
))
19376 /* For a function parameter pack that occurs at the end of the
19377 parameter-declaration-list, the type A of each remaining
19378 argument of the call is compared with the type P of the
19379 declarator-id of the function parameter pack. */
19382 parms
= TREE_CHAIN (parms
);
19384 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19385 /* For a function parameter pack that does not occur at the
19386 end of the parameter-declaration-list, the type of the
19387 parameter pack is a non-deduced context. */
19393 if (uses_template_parms (parm
))
19395 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19400 /* Now substitute into the default template arguments. */
19401 for (i
= 0; i
< ntparms
; i
++)
19403 tree targ
= TREE_VEC_ELT (targs
, i
);
19404 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19406 if (targ
|| tparm
== error_mark_node
)
19408 tree parm
= TREE_VALUE (tparm
);
19410 if (TREE_CODE (parm
) == PARM_DECL
19411 && uses_template_parms (TREE_TYPE (parm
))
19412 && saw_undeduced
< 2)
19415 tree arg
= TREE_PURPOSE (tparm
);
19416 reopen_deferring_access_checks (*checks
);
19417 location_t save_loc
= input_location
;
19419 input_location
= DECL_SOURCE_LOCATION (parm
);
19420 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19421 if (!uses_template_parms (arg
))
19422 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19424 else if (saw_undeduced
< 2)
19427 arg
= error_mark_node
;
19428 input_location
= save_loc
;
19429 *checks
= get_deferred_access_checks ();
19430 pop_deferring_access_checks ();
19431 if (arg
== error_mark_node
)
19435 TREE_VEC_ELT (targs
, i
) = arg
;
19436 /* The position of the first default template argument,
19437 is also the number of non-defaulted arguments in TARGS.
19439 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19440 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19444 if (saw_undeduced
++ == 1)
19448 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19449 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19451 return unify_success (explain_p
);
19454 /* Subroutine of type_unification_real. Args are like the variables
19455 at the call site. ARG is an overloaded function (or template-id);
19456 we try deducing template args from each of the overloads, and if
19457 only one succeeds, we go with that. Modifies TARGS and returns
19458 true on success. */
19461 resolve_overloaded_unification (tree tparms
,
19465 unification_kind_t strict
,
19469 tree tempargs
= copy_node (targs
);
19471 tree goodfn
= NULL_TREE
;
19474 if (TREE_CODE (arg
) == ADDR_EXPR
)
19476 arg
= TREE_OPERAND (arg
, 0);
19482 if (TREE_CODE (arg
) == COMPONENT_REF
)
19483 /* Handle `&x' where `x' is some static or non-static member
19485 arg
= TREE_OPERAND (arg
, 1);
19487 if (TREE_CODE (arg
) == OFFSET_REF
)
19488 arg
= TREE_OPERAND (arg
, 1);
19490 /* Strip baselink information. */
19491 if (BASELINK_P (arg
))
19492 arg
= BASELINK_FUNCTIONS (arg
);
19494 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19496 /* If we got some explicit template args, we need to plug them into
19497 the affected templates before we try to unify, in case the
19498 explicit args will completely resolve the templates in question. */
19501 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19502 arg
= TREE_OPERAND (arg
, 0);
19504 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19507 tree subargs
, elem
;
19509 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19512 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19513 expl_subargs
, NULL_TREE
, tf_none
,
19514 /*require_all_args=*/true,
19515 /*use_default_args=*/true);
19516 if (subargs
!= error_mark_node
19517 && !any_dependent_template_arguments_p (subargs
))
19519 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19520 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19521 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19522 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19531 /* If no templates (or more than one) are fully resolved by the
19532 explicit arguments, this template-id is a non-deduced context; it
19533 could still be OK if we deduce all template arguments for the
19534 enclosing call through other arguments. */
19538 else if (TREE_CODE (arg
) != OVERLOAD
19539 && TREE_CODE (arg
) != FUNCTION_DECL
)
19540 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19541 -- but the deduction does not succeed because the expression is
19542 not just the function on its own. */
19545 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19548 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19549 strict
, sub_strict
, addr_p
, explain_p
)
19550 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19557 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19558 to function or pointer to member function argument if the set of
19559 overloaded functions does not contain function templates and at most
19560 one of a set of overloaded functions provides a unique match.
19562 So if we found multiple possibilities, we return success but don't
19563 deduce anything. */
19567 int i
= TREE_VEC_LENGTH (targs
);
19569 if (TREE_VEC_ELT (tempargs
, i
))
19571 tree old
= TREE_VEC_ELT (targs
, i
);
19572 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19573 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19574 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19575 /* Don't forget explicit template arguments in a pack. */
19576 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19577 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19578 TREE_VEC_ELT (targs
, i
) = new_
;
19587 /* Core DR 115: In contexts where deduction is done and fails, or in
19588 contexts where deduction is not done, if a template argument list is
19589 specified and it, along with any default template arguments, identifies
19590 a single function template specialization, then the template-id is an
19591 lvalue for the function template specialization. */
19594 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19596 tree expr
, offset
, baselink
;
19599 if (!type_unknown_p (orig_expr
))
19604 offset
= NULL_TREE
;
19605 baselink
= NULL_TREE
;
19607 if (TREE_CODE (expr
) == ADDR_EXPR
)
19609 expr
= TREE_OPERAND (expr
, 0);
19612 if (TREE_CODE (expr
) == OFFSET_REF
)
19615 expr
= TREE_OPERAND (expr
, 1);
19617 if (BASELINK_P (expr
))
19620 expr
= BASELINK_FUNCTIONS (expr
);
19623 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19626 tree goodfn
= NULL_TREE
;
19628 /* If we got some explicit template args, we need to plug them into
19629 the affected templates before we try to unify, in case the
19630 explicit args will completely resolve the templates in question. */
19632 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19633 tree arg
= TREE_OPERAND (expr
, 0);
19634 tree badfn
= NULL_TREE
;
19635 tree badargs
= NULL_TREE
;
19637 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19640 tree subargs
, elem
;
19642 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19645 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19646 expl_subargs
, NULL_TREE
, tf_none
,
19647 /*require_all_args=*/true,
19648 /*use_default_args=*/true);
19649 if (subargs
!= error_mark_node
19650 && !any_dependent_template_arguments_p (subargs
))
19652 elem
= instantiate_template (fn
, subargs
, tf_none
);
19653 if (elem
== error_mark_node
)
19658 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
19667 mark_used (goodfn
);
19670 expr
= build_baselink (BASELINK_BINFO (baselink
),
19671 BASELINK_ACCESS_BINFO (baselink
),
19672 expr
, BASELINK_OPTYPE (baselink
));
19676 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
19677 expr
= build_offset_ref (base
, expr
, addr
, complain
);
19680 expr
= cp_build_addr_expr (expr
, complain
);
19683 else if (good
== 0 && badargs
&& (complain
& tf_error
))
19684 /* There were no good options and at least one bad one, so let the
19685 user know what the problem is. */
19686 instantiate_template (badfn
, badargs
, complain
);
19691 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19692 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19693 different overloads deduce different arguments for a given parm.
19694 ADDR_P is true if the expression for which deduction is being
19695 performed was of the form "& fn" rather than simply "fn".
19697 Returns 1 on success. */
19700 try_one_overload (tree tparms
,
19705 unification_kind_t strict
,
19714 if (arg
== error_mark_node
)
19717 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19718 to function or pointer to member function argument if the set of
19719 overloaded functions does not contain function templates and at most
19720 one of a set of overloaded functions provides a unique match.
19722 So if this is a template, just return success. */
19724 if (uses_template_parms (arg
))
19727 if (TREE_CODE (arg
) == METHOD_TYPE
)
19728 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
19730 arg
= build_pointer_type (arg
);
19732 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
19734 /* We don't copy orig_targs for this because if we have already deduced
19735 some template args from previous args, unify would complain when we
19736 try to deduce a template parameter for the same argument, even though
19737 there isn't really a conflict. */
19738 nargs
= TREE_VEC_LENGTH (targs
);
19739 tempargs
= make_tree_vec (nargs
);
19741 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
19744 /* First make sure we didn't deduce anything that conflicts with
19745 explicitly specified args. */
19746 for (i
= nargs
; i
--; )
19748 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19749 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
19753 else if (uses_template_parms (elt
))
19754 /* Since we're unifying against ourselves, we will fill in
19755 template args used in the function parm list with our own
19756 template parms. Discard them. */
19757 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
19758 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
19760 /* Check that the argument at each index of the deduced argument pack
19761 is equivalent to the corresponding explicitly specified argument.
19762 We may have deduced more arguments than were explicitly specified,
19765 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19766 that's wrong if we deduce the same argument pack from multiple
19767 function arguments: it's only incomplete the first time. */
19769 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
19770 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
19772 if (TREE_VEC_LENGTH (deduced_pack
)
19773 < TREE_VEC_LENGTH (explicit_pack
))
19776 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
19777 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
19778 TREE_VEC_ELT (deduced_pack
, j
)))
19781 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
19785 for (i
= nargs
; i
--; )
19787 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19790 TREE_VEC_ELT (targs
, i
) = elt
;
19796 /* PARM is a template class (perhaps with unbound template
19797 parameters). ARG is a fully instantiated type. If ARG can be
19798 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19799 TARGS are as for unify. */
19802 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
19805 tree copy_of_targs
;
19807 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19809 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19810 /* Matches anything. */;
19811 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
19812 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
19815 /* We need to make a new template argument vector for the call to
19816 unify. If we used TARGS, we'd clutter it up with the result of
19817 the attempted unification, even if this class didn't work out.
19818 We also don't want to commit ourselves to all the unifications
19819 we've already done, since unification is supposed to be done on
19820 an argument-by-argument basis. In other words, consider the
19821 following pathological case:
19823 template <int I, int J, int K>
19826 template <int I, int J>
19827 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19829 template <int I, int J, int K>
19830 void f(S<I, J, K>, S<I, I, I>);
19839 Now, by the time we consider the unification involving `s2', we
19840 already know that we must have `f<0, 0, 0>'. But, even though
19841 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19842 because there are two ways to unify base classes of S<0, 1, 2>
19843 with S<I, I, I>. If we kept the already deduced knowledge, we
19844 would reject the possibility I=1. */
19845 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19847 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19849 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
19854 /* If unification failed, we're done. */
19855 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
19856 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
19862 /* Given a template type PARM and a class type ARG, find the unique
19863 base type in ARG that is an instance of PARM. We do not examine
19864 ARG itself; only its base-classes. If there is not exactly one
19865 appropriate base class, return NULL_TREE. PARM may be the type of
19866 a partial specialization, as well as a plain template type. Used
19869 static enum template_base_result
19870 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
19871 bool explain_p
, tree
*result
)
19873 tree rval
= NULL_TREE
;
19876 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
19878 binfo
= TYPE_BINFO (complete_type (arg
));
19881 /* The type could not be completed. */
19882 *result
= NULL_TREE
;
19883 return tbr_incomplete_type
;
19886 /* Walk in inheritance graph order. The search order is not
19887 important, and this avoids multiple walks of virtual bases. */
19888 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
19890 tree r
= try_class_unification (tparms
, targs
, parm
,
19891 BINFO_TYPE (binfo
), explain_p
);
19895 /* If there is more than one satisfactory baseclass, then:
19899 If they yield more than one possible deduced A, the type
19903 if (rval
&& !same_type_p (r
, rval
))
19905 *result
= NULL_TREE
;
19906 return tbr_ambiguous_baseclass
;
19914 return tbr_success
;
19917 /* Returns the level of DECL, which declares a template parameter. */
19920 template_decl_level (tree decl
)
19922 switch (TREE_CODE (decl
))
19925 case TEMPLATE_DECL
:
19926 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
19929 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
19932 gcc_unreachable ();
19937 /* Decide whether ARG can be unified with PARM, considering only the
19938 cv-qualifiers of each type, given STRICT as documented for unify.
19939 Returns nonzero iff the unification is OK on that basis. */
19942 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
19944 int arg_quals
= cp_type_quals (arg
);
19945 int parm_quals
= cp_type_quals (parm
);
19947 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19948 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19950 /* Although a CVR qualifier is ignored when being applied to a
19951 substituted template parameter ([8.3.2]/1 for example), that
19952 does not allow us to unify "const T" with "int&" because both
19953 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19954 It is ok when we're allowing additional CV qualifiers
19955 at the outer level [14.8.2.1]/3,1st bullet. */
19956 if ((TREE_CODE (arg
) == REFERENCE_TYPE
19957 || TREE_CODE (arg
) == FUNCTION_TYPE
19958 || TREE_CODE (arg
) == METHOD_TYPE
)
19959 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
19962 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
19963 && (parm_quals
& TYPE_QUAL_RESTRICT
))
19967 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19968 && (arg_quals
& parm_quals
) != parm_quals
)
19971 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
19972 && (parm_quals
& arg_quals
) != arg_quals
)
19978 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19980 template_parm_level_and_index (tree parm
, int* level
, int* index
)
19982 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19983 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19984 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19986 *index
= TEMPLATE_TYPE_IDX (parm
);
19987 *level
= TEMPLATE_TYPE_LEVEL (parm
);
19991 *index
= TEMPLATE_PARM_IDX (parm
);
19992 *level
= TEMPLATE_PARM_LEVEL (parm
);
19996 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19998 if (unify (TP, TA, P, A, S, EP)) \
20002 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20003 expansion at the end of PACKED_PARMS. Returns 0 if the type
20004 deduction succeeds, 1 otherwise. STRICT is the same as in
20005 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20006 function call argument list. We'll need to adjust the arguments to make them
20007 types. SUBR tells us if this is from a recursive call to
20008 type_unification_real, or for comparing two template argument
20012 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
20013 tree packed_args
, unification_kind_t strict
,
20014 bool subr
, bool explain_p
)
20017 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20018 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20019 tree pack
, packs
= NULL_TREE
;
20020 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20022 packed_args
= expand_template_argument_pack (packed_args
);
20024 int len
= TREE_VEC_LENGTH (packed_args
);
20026 /* Determine the parameter packs we will be deducing from the
20027 pattern, and record their current deductions. */
20028 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20029 pack
; pack
= TREE_CHAIN (pack
))
20031 tree parm_pack
= TREE_VALUE (pack
);
20034 /* Determine the index and level of this parameter pack. */
20035 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20037 /* Keep track of the parameter packs and their corresponding
20039 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20040 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20043 /* Loop through all of the arguments that have not yet been
20044 unified and unify each with the pattern. */
20045 for (i
= start
; i
< len
; i
++)
20048 bool any_explicit
= false;
20049 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20051 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20052 or the element of its argument pack at the current index if
20053 this argument was explicitly specified. */
20054 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20058 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20061 if (TREE_VALUE (pack
)
20062 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20063 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20065 any_explicit
= true;
20066 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20068 TMPL_ARG (targs
, level
, idx
) = arg
;
20071 /* If we had explicit template arguments, substitute them into the
20072 pattern before deduction. */
20075 /* Some arguments might still be unspecified or dependent. */
20077 ++processing_template_decl
;
20078 dependent
= any_dependent_template_arguments_p (targs
);
20080 --processing_template_decl
;
20081 parm
= tsubst (pattern
, targs
,
20082 explain_p
? tf_warning_or_error
: tf_none
,
20085 --processing_template_decl
;
20086 if (parm
== error_mark_node
)
20092 /* Unify the pattern with the current argument. */
20093 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20097 /* For each parameter pack, collect the deduced value. */
20098 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20101 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20103 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20104 TMPL_ARG (targs
, level
, idx
);
20108 /* Verify that the results of unification with the parameter packs
20109 produce results consistent with what we've seen before, and make
20110 the deduced argument packs available. */
20111 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20113 tree old_pack
= TREE_VALUE (pack
);
20114 tree new_args
= TREE_TYPE (pack
);
20115 int i
, len
= TREE_VEC_LENGTH (new_args
);
20117 bool nondeduced_p
= false;
20119 /* By default keep the original deduced argument pack.
20120 If necessary, more specific code is going to update the
20121 resulting deduced argument later down in this function. */
20122 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20123 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20125 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20126 actually deduce anything. */
20127 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20128 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20129 nondeduced_p
= true;
20133 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20135 /* If we had fewer function args than explicit template args,
20136 just use the explicits. */
20137 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20138 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20139 if (len
< explicit_len
)
20140 new_args
= explicit_args
;
20146 /* Build the deduced *_ARGUMENT_PACK. */
20147 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20149 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20150 TREE_CONSTANT (result
) = 1;
20153 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20155 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20157 /* Note the deduced argument packs for this parameter
20159 TMPL_ARG (targs
, level
, idx
) = result
;
20161 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20162 && (ARGUMENT_PACK_ARGS (old_pack
)
20163 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20165 /* We only had the explicitly-provided arguments before, but
20166 now we have a complete set of arguments. */
20167 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20169 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20170 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20171 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20175 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20176 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20178 if (!comp_template_args (old_args
, new_args
,
20179 &bad_old_arg
, &bad_new_arg
))
20180 /* Inconsistent unification of this parameter pack. */
20181 return unify_parameter_pack_inconsistent (explain_p
,
20187 return unify_success (explain_p
);
20190 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20191 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20192 parameters and return value are as for unify. */
20195 unify_array_domain (tree tparms
, tree targs
,
20196 tree parm_dom
, tree arg_dom
,
20204 /* Our representation of array types uses "N - 1" as the
20205 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20206 not an integer constant. We cannot unify arbitrarily
20207 complex expressions, so we eliminate the MINUS_EXPRs
20209 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20210 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20213 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20214 parm_max
= TREE_OPERAND (parm_max
, 0);
20216 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20217 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20220 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20221 trying to unify the type of a variable with the type
20222 of a template parameter. For example:
20224 template <unsigned int N>
20225 void f (char (&) [N]);
20232 Here, the type of the ARG will be "int [g(i)]", and
20233 may be a SAVE_EXPR, etc. */
20234 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20235 return unify_vla_arg (explain_p
, arg_dom
);
20236 arg_max
= TREE_OPERAND (arg_max
, 0);
20239 /* If only one of the bounds used a MINUS_EXPR, compensate
20240 by adding one to the other bound. */
20241 if (parm_cst
&& !arg_cst
)
20242 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20246 else if (arg_cst
&& !parm_cst
)
20247 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20252 return unify (tparms
, targs
, parm_max
, arg_max
,
20253 UNIFY_ALLOW_INTEGER
, explain_p
);
20256 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20258 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20263 if (PACK_EXPANSION_P (t
))
20264 t
= PACK_EXPANSION_PATTERN (t
);
20265 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20266 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20267 || DECL_TYPE_TEMPLATE_P (t
))
20269 else if (TYPE_P (t
))
20275 /* Deduce the value of template parameters. TPARMS is the (innermost)
20276 set of template parameters to a template. TARGS is the bindings
20277 for those template parameters, as determined thus far; TARGS may
20278 include template arguments for outer levels of template parameters
20279 as well. PARM is a parameter to a template function, or a
20280 subcomponent of that parameter; ARG is the corresponding argument.
20281 This function attempts to match PARM with ARG in a manner
20282 consistent with the existing assignments in TARGS. If more values
20283 are deduced, then TARGS is updated.
20285 Returns 0 if the type deduction succeeds, 1 otherwise. The
20286 parameter STRICT is a bitwise or of the following flags:
20289 Require an exact match between PARM and ARG.
20290 UNIFY_ALLOW_MORE_CV_QUAL:
20291 Allow the deduced ARG to be more cv-qualified (by qualification
20292 conversion) than ARG.
20293 UNIFY_ALLOW_LESS_CV_QUAL:
20294 Allow the deduced ARG to be less cv-qualified than ARG.
20295 UNIFY_ALLOW_DERIVED:
20296 Allow the deduced ARG to be a template base class of ARG,
20297 or a pointer to a template base class of the type pointed to by
20299 UNIFY_ALLOW_INTEGER:
20300 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20301 case for more information.
20302 UNIFY_ALLOW_OUTER_LEVEL:
20303 This is the outermost level of a deduction. Used to determine validity
20304 of qualification conversions. A valid qualification conversion must
20305 have const qualified pointers leading up to the inner type which
20306 requires additional CV quals, except at the outer level, where const
20307 is not required [conv.qual]. It would be normal to set this flag in
20308 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20309 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20310 This is the outermost level of a deduction, and PARM can be more CV
20311 qualified at this point.
20312 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20313 This is the outermost level of a deduction, and PARM can be less CV
20314 qualified at this point. */
20317 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20323 int strict_in
= strict
;
20324 tsubst_flags_t complain
= (explain_p
20325 ? tf_warning_or_error
20328 /* I don't think this will do the right thing with respect to types.
20329 But the only case I've seen it in so far has been array bounds, where
20330 signedness is the only information lost, and I think that will be
20332 while (CONVERT_EXPR_P (parm
))
20333 parm
= TREE_OPERAND (parm
, 0);
20335 if (arg
== error_mark_node
)
20336 return unify_invalid (explain_p
);
20337 if (arg
== unknown_type_node
20338 || arg
== init_list_type_node
)
20339 /* We can't deduce anything from this, but we might get all the
20340 template args from other function args. */
20341 return unify_success (explain_p
);
20343 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20344 return unify_success (explain_p
);
20346 /* If PARM uses template parameters, then we can't bail out here,
20347 even if ARG == PARM, since we won't record unifications for the
20348 template parameters. We might need them if we're trying to
20349 figure out which of two things is more specialized. */
20350 if (arg
== parm
&& !uses_template_parms (parm
))
20351 return unify_success (explain_p
);
20353 /* Handle init lists early, so the rest of the function can assume
20354 we're dealing with a type. */
20355 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20359 tree orig_parm
= parm
;
20361 /* Replace T with std::initializer_list<T> for deduction. */
20362 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20363 && flag_deduce_init_list
)
20364 parm
= listify (parm
);
20366 if (!is_std_init_list (parm
)
20367 && TREE_CODE (parm
) != ARRAY_TYPE
)
20368 /* We can only deduce from an initializer list argument if the
20369 parameter is std::initializer_list or an array; otherwise this
20370 is a non-deduced context. */
20371 return unify_success (explain_p
);
20373 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20374 elttype
= TREE_TYPE (parm
);
20377 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20378 /* Deduction is defined in terms of a single type, so just punt
20379 on the (bizarre) std::initializer_list<T...>. */
20380 if (PACK_EXPANSION_P (elttype
))
20381 return unify_success (explain_p
);
20384 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20386 int elt_strict
= strict
;
20388 if (elt
== error_mark_node
)
20389 return unify_invalid (explain_p
);
20391 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20393 tree type
= TREE_TYPE (elt
);
20394 if (type
== error_mark_node
)
20395 return unify_invalid (explain_p
);
20396 /* It should only be possible to get here for a call. */
20397 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20398 elt_strict
|= maybe_adjust_types_for_deduction
20399 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20403 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20407 if (TREE_CODE (parm
) == ARRAY_TYPE
20408 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20410 /* Also deduce from the length of the initializer list. */
20411 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20412 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20413 if (idx
== error_mark_node
)
20414 return unify_invalid (explain_p
);
20415 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20419 /* If the std::initializer_list<T> deduction worked, replace the
20420 deduced A with std::initializer_list<A>. */
20421 if (orig_parm
!= parm
)
20423 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20424 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20425 targ
= listify (targ
);
20426 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20428 return unify_success (explain_p
);
20431 /* If parm and arg aren't the same kind of thing (template, type, or
20432 expression), fail early. */
20433 if (pa_kind (parm
) != pa_kind (arg
))
20434 return unify_invalid (explain_p
);
20436 /* Immediately reject some pairs that won't unify because of
20437 cv-qualification mismatches. */
20438 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20440 /* It is the elements of the array which hold the cv quals of an array
20441 type, and the elements might be template type parms. We'll check
20442 when we recurse. */
20443 && TREE_CODE (arg
) != ARRAY_TYPE
20444 /* We check the cv-qualifiers when unifying with template type
20445 parameters below. We want to allow ARG `const T' to unify with
20446 PARM `T' for example, when computing which of two templates
20447 is more specialized, for example. */
20448 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20449 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20450 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20452 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20453 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20454 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20455 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20456 strict
&= ~UNIFY_ALLOW_DERIVED
;
20457 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20458 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20460 switch (TREE_CODE (parm
))
20462 case TYPENAME_TYPE
:
20464 case UNBOUND_CLASS_TEMPLATE
:
20465 /* In a type which contains a nested-name-specifier, template
20466 argument values cannot be deduced for template parameters used
20467 within the nested-name-specifier. */
20468 return unify_success (explain_p
);
20470 case TEMPLATE_TYPE_PARM
:
20471 case TEMPLATE_TEMPLATE_PARM
:
20472 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20473 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20474 if (error_operand_p (tparm
))
20475 return unify_invalid (explain_p
);
20477 if (TEMPLATE_TYPE_LEVEL (parm
)
20478 != template_decl_level (tparm
))
20479 /* The PARM is not one we're trying to unify. Just check
20480 to see if it matches ARG. */
20482 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20483 && (is_auto (parm
) ? is_auto (arg
)
20484 : same_type_p (parm
, arg
)))
20485 return unify_success (explain_p
);
20487 return unify_type_mismatch (explain_p
, parm
, arg
);
20489 idx
= TEMPLATE_TYPE_IDX (parm
);
20490 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20491 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20492 if (error_operand_p (tparm
))
20493 return unify_invalid (explain_p
);
20495 /* Check for mixed types and values. */
20496 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20497 && TREE_CODE (tparm
) != TYPE_DECL
)
20498 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20499 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20500 gcc_unreachable ();
20502 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20504 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20505 && CLASS_TYPE_P (arg
))
20507 /* First try to match ARG directly. */
20508 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20512 /* Otherwise, look for a suitable base of ARG, as below. */
20513 enum template_base_result r
;
20514 r
= get_template_base (tparms
, targs
, parm
, arg
,
20517 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20521 /* ARG must be constructed from a template class or a template
20522 template parameter. */
20523 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20524 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20525 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20527 /* Deduce arguments T, i from TT<T> or TT<i>. */
20528 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20531 arg
= TYPE_TI_TEMPLATE (arg
);
20533 /* Fall through to deduce template name. */
20536 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20537 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20539 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20541 /* Simple cases: Value already set, does match or doesn't. */
20542 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20543 return unify_success (explain_p
);
20545 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20549 /* If PARM is `const T' and ARG is only `int', we don't have
20550 a match unless we are allowing additional qualification.
20551 If ARG is `const int' and PARM is just `T' that's OK;
20552 that binds `const int' to `T'. */
20553 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20555 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20557 /* Consider the case where ARG is `const volatile int' and
20558 PARM is `const T'. Then, T should be `volatile int'. */
20559 arg
= cp_build_qualified_type_real
20560 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20561 if (arg
== error_mark_node
)
20562 return unify_invalid (explain_p
);
20564 /* Simple cases: Value already set, does match or doesn't. */
20565 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20566 return unify_success (explain_p
);
20568 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20570 /* Make sure that ARG is not a variable-sized array. (Note
20571 that were talking about variable-sized arrays (like
20572 `int[n]'), rather than arrays of unknown size (like
20573 `int[]').) We'll get very confused by such a type since
20574 the bound of the array is not constant, and therefore
20575 not mangleable. Besides, such types are not allowed in
20576 ISO C++, so we can do as we please here. We do allow
20577 them for 'auto' deduction, since that isn't ABI-exposed. */
20578 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20579 return unify_vla_arg (explain_p
, arg
);
20581 /* Strip typedefs as in convert_template_argument. */
20582 arg
= canonicalize_type_argument (arg
, tf_none
);
20585 /* If ARG is a parameter pack or an expansion, we cannot unify
20586 against it unless PARM is also a parameter pack. */
20587 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20588 && !template_parameter_pack_p (parm
))
20589 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20591 /* If the argument deduction results is a METHOD_TYPE,
20592 then there is a problem.
20593 METHOD_TYPE doesn't map to any real C++ type the result of
20594 the deduction can not be of that type. */
20595 if (TREE_CODE (arg
) == METHOD_TYPE
)
20596 return unify_method_type_error (explain_p
, arg
);
20598 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20599 return unify_success (explain_p
);
20601 case TEMPLATE_PARM_INDEX
:
20602 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20603 if (error_operand_p (tparm
))
20604 return unify_invalid (explain_p
);
20606 if (TEMPLATE_PARM_LEVEL (parm
)
20607 != template_decl_level (tparm
))
20609 /* The PARM is not one we're trying to unify. Just check
20610 to see if it matches ARG. */
20611 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20612 && cp_tree_equal (parm
, arg
));
20614 unify_expression_unequal (explain_p
, parm
, arg
);
20618 idx
= TEMPLATE_PARM_IDX (parm
);
20619 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20623 int x
= !cp_tree_equal (targ
, arg
);
20625 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20629 /* [temp.deduct.type] If, in the declaration of a function template
20630 with a non-type template-parameter, the non-type
20631 template-parameter is used in an expression in the function
20632 parameter-list and, if the corresponding template-argument is
20633 deduced, the template-argument type shall match the type of the
20634 template-parameter exactly, except that a template-argument
20635 deduced from an array bound may be of any integral type.
20636 The non-type parameter might use already deduced type parameters. */
20637 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20638 if (tree a
= type_uses_auto (tparm
))
20640 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
20641 if (tparm
== error_mark_node
)
20645 if (!TREE_TYPE (arg
))
20646 /* Template-parameter dependent expression. Just accept it for now.
20647 It will later be processed in convert_template_argument. */
20649 else if (same_type_p (non_reference (TREE_TYPE (arg
)),
20650 non_reference (tparm
)))
20652 else if ((strict
& UNIFY_ALLOW_INTEGER
)
20653 && CP_INTEGRAL_TYPE_P (tparm
))
20654 /* Convert the ARG to the type of PARM; the deduced non-type
20655 template argument must exactly match the types of the
20656 corresponding parameter. */
20657 arg
= fold (build_nop (tparm
, arg
));
20658 else if (uses_template_parms (tparm
))
20660 /* We haven't deduced the type of this parameter yet. */
20661 if (cxx_dialect
>= cxx1z
20662 /* We deduce from array bounds in try_array_deduction. */
20663 && !(strict
& UNIFY_ALLOW_INTEGER
))
20665 /* Deduce it from the non-type argument. */
20666 tree atype
= TREE_TYPE (arg
);
20667 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20669 UNIFY_ALLOW_NONE
, explain_p
);
20672 /* Try again later. */
20673 return unify_success (explain_p
);
20676 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
20678 /* If ARG is a parameter pack or an expansion, we cannot unify
20679 against it unless PARM is also a parameter pack. */
20680 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20681 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
20682 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20685 bool removed_attr
= false;
20686 arg
= strip_typedefs_expr (arg
, &removed_attr
);
20688 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20689 return unify_success (explain_p
);
20693 /* A pointer-to-member constant can be unified only with
20694 another constant. */
20695 if (TREE_CODE (arg
) != PTRMEM_CST
)
20696 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
20698 /* Just unify the class member. It would be useless (and possibly
20699 wrong, depending on the strict flags) to unify also
20700 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20701 arg refer to the same variable, even if through different
20702 classes. For instance:
20704 struct A { int x; };
20707 Unification of &A::x and &B::x must succeed. */
20708 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
20709 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
20714 if (!TYPE_PTR_P (arg
))
20715 return unify_type_mismatch (explain_p
, parm
, arg
);
20717 /* [temp.deduct.call]
20719 A can be another pointer or pointer to member type that can
20720 be converted to the deduced A via a qualification
20721 conversion (_conv.qual_).
20723 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20724 This will allow for additional cv-qualification of the
20725 pointed-to types if appropriate. */
20727 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
20728 /* The derived-to-base conversion only persists through one
20729 level of pointers. */
20730 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
20732 return unify (tparms
, targs
, TREE_TYPE (parm
),
20733 TREE_TYPE (arg
), strict
, explain_p
);
20736 case REFERENCE_TYPE
:
20737 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
20738 return unify_type_mismatch (explain_p
, parm
, arg
);
20739 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20740 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20743 if (TREE_CODE (arg
) != ARRAY_TYPE
)
20744 return unify_type_mismatch (explain_p
, parm
, arg
);
20745 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
20746 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
20747 return unify_type_mismatch (explain_p
, parm
, arg
);
20748 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20749 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20750 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
20751 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20752 TYPE_DOMAIN (arg
), explain_p
);
20753 return unify_success (explain_p
);
20760 case ENUMERAL_TYPE
:
20763 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20764 return unify_type_mismatch (explain_p
, parm
, arg
);
20766 /* We have already checked cv-qualification at the top of the
20768 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
20769 return unify_type_mismatch (explain_p
, parm
, arg
);
20771 /* As far as unification is concerned, this wins. Later checks
20772 will invalidate it if necessary. */
20773 return unify_success (explain_p
);
20775 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20776 /* Type INTEGER_CST can come from ordinary constant template args. */
20778 while (CONVERT_EXPR_P (arg
))
20779 arg
= TREE_OPERAND (arg
, 0);
20781 if (TREE_CODE (arg
) != INTEGER_CST
)
20782 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20783 return (tree_int_cst_equal (parm
, arg
)
20784 ? unify_success (explain_p
)
20785 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
20789 int i
, len
, argslen
;
20790 int parm_variadic_p
= 0;
20792 if (TREE_CODE (arg
) != TREE_VEC
)
20793 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20795 len
= TREE_VEC_LENGTH (parm
);
20796 argslen
= TREE_VEC_LENGTH (arg
);
20798 /* Check for pack expansions in the parameters. */
20799 for (i
= 0; i
< len
; ++i
)
20801 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
20804 /* We can unify against something with a trailing
20806 parm_variadic_p
= 1;
20808 /* [temp.deduct.type]/9: If the template argument list of
20809 P contains a pack expansion that is not the last
20810 template argument, the entire template argument list
20811 is a non-deduced context. */
20812 return unify_success (explain_p
);
20816 /* If we don't have enough arguments to satisfy the parameters
20817 (not counting the pack expression at the end), or we have
20818 too many arguments for a parameter list that doesn't end in
20819 a pack expression, we can't unify. */
20820 if (parm_variadic_p
20821 ? argslen
< len
- parm_variadic_p
20823 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
20825 /* Unify all of the parameters that precede the (optional)
20826 pack expression. */
20827 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20829 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20830 TREE_VEC_ELT (parm
, i
),
20831 TREE_VEC_ELT (arg
, i
),
20832 UNIFY_ALLOW_NONE
, explain_p
);
20834 if (parm_variadic_p
)
20835 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
20837 /*subr=*/true, explain_p
);
20838 return unify_success (explain_p
);
20843 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20844 return unify_type_mismatch (explain_p
, parm
, arg
);
20846 if (TYPE_PTRMEMFUNC_P (parm
))
20848 if (!TYPE_PTRMEMFUNC_P (arg
))
20849 return unify_type_mismatch (explain_p
, parm
, arg
);
20851 return unify (tparms
, targs
,
20852 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
20853 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
20854 strict
, explain_p
);
20856 else if (TYPE_PTRMEMFUNC_P (arg
))
20857 return unify_type_mismatch (explain_p
, parm
, arg
);
20859 if (CLASSTYPE_TEMPLATE_INFO (parm
))
20861 tree t
= NULL_TREE
;
20863 if (strict_in
& UNIFY_ALLOW_DERIVED
)
20865 /* First, we try to unify the PARM and ARG directly. */
20866 t
= try_class_unification (tparms
, targs
,
20867 parm
, arg
, explain_p
);
20871 /* Fallback to the special case allowed in
20872 [temp.deduct.call]:
20874 If P is a class, and P has the form
20875 template-id, then A can be a derived class of
20876 the deduced A. Likewise, if P is a pointer to
20877 a class of the form template-id, A can be a
20878 pointer to a derived class pointed to by the
20880 enum template_base_result r
;
20881 r
= get_template_base (tparms
, targs
, parm
, arg
,
20886 /* Don't give the derived diagnostic if we're
20887 already dealing with the same template. */
20889 = (CLASSTYPE_TEMPLATE_INFO (arg
)
20890 && (CLASSTYPE_TI_TEMPLATE (parm
)
20891 == CLASSTYPE_TI_TEMPLATE (arg
)));
20892 return unify_no_common_base (explain_p
&& !same_template
,
20897 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
20898 && (CLASSTYPE_TI_TEMPLATE (parm
)
20899 == CLASSTYPE_TI_TEMPLATE (arg
)))
20900 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20901 Then, we should unify `int' and `U'. */
20904 /* There's no chance of unification succeeding. */
20905 return unify_type_mismatch (explain_p
, parm
, arg
);
20907 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
20908 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
20910 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
20911 return unify_type_mismatch (explain_p
, parm
, arg
);
20912 return unify_success (explain_p
);
20915 case FUNCTION_TYPE
:
20917 unsigned int nargs
;
20922 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20923 return unify_type_mismatch (explain_p
, parm
, arg
);
20925 /* CV qualifications for methods can never be deduced, they must
20926 match exactly. We need to check them explicitly here,
20927 because type_unification_real treats them as any other
20928 cv-qualified parameter. */
20929 if (TREE_CODE (parm
) == METHOD_TYPE
20930 && (!check_cv_quals_for_unify
20932 class_of_this_parm (arg
),
20933 class_of_this_parm (parm
))))
20934 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20935 if (TREE_CODE (arg
) == FUNCTION_TYPE
20936 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
20937 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20938 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
20939 return unify_type_mismatch (explain_p
, parm
, arg
);
20941 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
20942 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
20944 nargs
= list_length (TYPE_ARG_TYPES (arg
));
20945 args
= XALLOCAVEC (tree
, nargs
);
20946 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
20947 a
!= NULL_TREE
&& a
!= void_list_node
;
20948 a
= TREE_CHAIN (a
), ++i
)
20949 args
[i
] = TREE_VALUE (a
);
20952 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
20953 args
, nargs
, 1, DEDUCE_EXACT
,
20954 LOOKUP_NORMAL
, NULL
, explain_p
))
20957 if (flag_noexcept_type
)
20959 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
20960 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
20961 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
20962 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
20963 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
20964 && uses_template_parms (TREE_PURPOSE (pspec
)))
20965 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
20966 TREE_PURPOSE (aspec
),
20967 UNIFY_ALLOW_NONE
, explain_p
);
20968 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
20969 return unify_type_mismatch (explain_p
, parm
, arg
);
20976 /* Unify a pointer to member with a pointer to member function, which
20977 deduces the type of the member as a function type. */
20978 if (TYPE_PTRMEMFUNC_P (arg
))
20980 /* Check top-level cv qualifiers */
20981 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
20982 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20984 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20985 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
20986 UNIFY_ALLOW_NONE
, explain_p
);
20988 /* Determine the type of the function we are unifying against. */
20989 tree fntype
= static_fn_type (arg
);
20991 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
20994 if (TREE_CODE (arg
) != OFFSET_TYPE
)
20995 return unify_type_mismatch (explain_p
, parm
, arg
);
20996 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20997 TYPE_OFFSET_BASETYPE (arg
),
20998 UNIFY_ALLOW_NONE
, explain_p
);
20999 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
21000 strict
, explain_p
);
21003 if (DECL_TEMPLATE_PARM_P (parm
))
21004 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
21005 if (arg
!= scalar_constant_value (parm
))
21006 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21007 return unify_success (explain_p
);
21010 case TEMPLATE_DECL
:
21011 /* Matched cases are handled by the ARG == PARM test above. */
21012 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
21015 /* We might get a variable as a non-type template argument in parm if the
21016 corresponding parameter is type-dependent. Make any necessary
21017 adjustments based on whether arg is a reference. */
21018 if (CONSTANT_CLASS_P (arg
))
21019 parm
= fold_non_dependent_expr (parm
);
21020 else if (REFERENCE_REF_P (arg
))
21022 tree sub
= TREE_OPERAND (arg
, 0);
21024 if (TREE_CODE (sub
) == ADDR_EXPR
)
21025 arg
= TREE_OPERAND (sub
, 0);
21027 /* Now use the normal expression code to check whether they match. */
21030 case TYPE_ARGUMENT_PACK
:
21031 case NONTYPE_ARGUMENT_PACK
:
21032 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21033 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21036 case DECLTYPE_TYPE
:
21037 case UNDERLYING_TYPE
:
21038 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21039 or UNDERLYING_TYPE nodes. */
21040 return unify_success (explain_p
);
21043 /* Unification fails if we hit an error node. */
21044 return unify_invalid (explain_p
);
21047 if (REFERENCE_REF_P (parm
))
21049 bool pexp
= PACK_EXPANSION_P (arg
);
21051 arg
= PACK_EXPANSION_PATTERN (arg
);
21052 if (REFERENCE_REF_P (arg
))
21053 arg
= TREE_OPERAND (arg
, 0);
21055 arg
= make_pack_expansion (arg
);
21056 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21057 strict
, explain_p
);
21062 /* An unresolved overload is a nondeduced context. */
21063 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21064 return unify_success (explain_p
);
21065 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21067 /* We must be looking at an expression. This can happen with
21071 void foo(S<I>, S<I + 2>);
21073 This is a "nondeduced context":
21077 The nondeduced contexts are:
21079 --A type that is a template-id in which one or more of
21080 the template-arguments is an expression that references
21081 a template-parameter.
21083 In these cases, we assume deduction succeeded, but don't
21084 actually infer any unifications. */
21086 if (!uses_template_parms (parm
)
21087 && !template_args_equal (parm
, arg
))
21088 return unify_expression_unequal (explain_p
, parm
, arg
);
21090 return unify_success (explain_p
);
21093 #undef RECUR_AND_CHECK_FAILURE
21095 /* Note that DECL can be defined in this translation unit, if
21099 mark_definable (tree decl
)
21102 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21103 FOR_EACH_CLONE (clone
, decl
)
21104 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21107 /* Called if RESULT is explicitly instantiated, or is a member of an
21108 explicitly instantiated class. */
21111 mark_decl_instantiated (tree result
, int extern_p
)
21113 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21115 /* If this entity has already been written out, it's too late to
21116 make any modifications. */
21117 if (TREE_ASM_WRITTEN (result
))
21120 /* For anonymous namespace we don't need to do anything. */
21121 if (decl_anon_ns_mem_p (result
))
21123 gcc_assert (!TREE_PUBLIC (result
));
21127 if (TREE_CODE (result
) != FUNCTION_DECL
)
21128 /* The TREE_PUBLIC flag for function declarations will have been
21129 set correctly by tsubst. */
21130 TREE_PUBLIC (result
) = 1;
21132 /* This might have been set by an earlier implicit instantiation. */
21133 DECL_COMDAT (result
) = 0;
21136 DECL_NOT_REALLY_EXTERN (result
) = 0;
21139 mark_definable (result
);
21140 mark_needed (result
);
21141 /* Always make artificials weak. */
21142 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21143 comdat_linkage (result
);
21144 /* For WIN32 we also want to put explicit instantiations in
21145 linkonce sections. */
21146 else if (TREE_PUBLIC (result
))
21147 maybe_make_one_only (result
);
21150 /* If EXTERN_P, then this function will not be emitted -- unless
21151 followed by an explicit instantiation, at which point its linkage
21152 will be adjusted. If !EXTERN_P, then this function will be
21153 emitted here. In neither circumstance do we want
21154 import_export_decl to adjust the linkage. */
21155 DECL_INTERFACE_KNOWN (result
) = 1;
21158 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21159 important template arguments. If any are missing, we check whether
21160 they're important by using error_mark_node for substituting into any
21161 args that were used for partial ordering (the ones between ARGS and END)
21162 and seeing if it bubbles up. */
21165 check_undeduced_parms (tree targs
, tree args
, tree end
)
21167 bool found
= false;
21169 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21170 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21173 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21177 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21178 if (substed
== error_mark_node
)
21184 /* Given two function templates PAT1 and PAT2, return:
21186 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21187 -1 if PAT2 is more specialized than PAT1.
21188 0 if neither is more specialized.
21190 LEN indicates the number of parameters we should consider
21191 (defaulted parameters should not be considered).
21193 The 1998 std underspecified function template partial ordering, and
21194 DR214 addresses the issue. We take pairs of arguments, one from
21195 each of the templates, and deduce them against each other. One of
21196 the templates will be more specialized if all the *other*
21197 template's arguments deduce against its arguments and at least one
21198 of its arguments *does* *not* deduce against the other template's
21199 corresponding argument. Deduction is done as for class templates.
21200 The arguments used in deduction have reference and top level cv
21201 qualifiers removed. Iff both arguments were originally reference
21202 types *and* deduction succeeds in both directions, an lvalue reference
21203 wins against an rvalue reference and otherwise the template
21204 with the more cv-qualified argument wins for that pairing (if
21205 neither is more cv-qualified, they both are equal). Unlike regular
21206 deduction, after all the arguments have been deduced in this way,
21207 we do *not* verify the deduced template argument values can be
21208 substituted into non-deduced contexts.
21210 The logic can be a bit confusing here, because we look at deduce1 and
21211 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21212 can find template arguments for pat1 to make arg1 look like arg2, that
21213 means that arg2 is at least as specialized as arg1. */
21216 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21218 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21219 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21220 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21221 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21222 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21223 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21224 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21225 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21226 tree origs1
, origs2
;
21227 bool lose1
= false;
21228 bool lose2
= false;
21230 /* Remove the this parameter from non-static member functions. If
21231 one is a non-static member function and the other is not a static
21232 member function, remove the first parameter from that function
21233 also. This situation occurs for operator functions where we
21234 locate both a member function (with this pointer) and non-member
21235 operator (with explicit first operand). */
21236 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21238 len
--; /* LEN is the number of significant arguments for DECL1 */
21239 args1
= TREE_CHAIN (args1
);
21240 if (!DECL_STATIC_FUNCTION_P (decl2
))
21241 args2
= TREE_CHAIN (args2
);
21243 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21245 args2
= TREE_CHAIN (args2
);
21246 if (!DECL_STATIC_FUNCTION_P (decl1
))
21249 args1
= TREE_CHAIN (args1
);
21253 /* If only one is a conversion operator, they are unordered. */
21254 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21257 /* Consider the return type for a conversion function */
21258 if (DECL_CONV_FN_P (decl1
))
21260 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21261 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21265 processing_template_decl
++;
21271 /* Stop when an ellipsis is seen. */
21272 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21274 tree arg1
= TREE_VALUE (args1
);
21275 tree arg2
= TREE_VALUE (args2
);
21276 int deduce1
, deduce2
;
21282 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21283 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21285 /* When both arguments are pack expansions, we need only
21286 unify the patterns themselves. */
21287 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21288 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21290 /* This is the last comparison we need to do. */
21294 /* DR 1847: If a particular P contains no template-parameters that
21295 participate in template argument deduction, that P is not used to
21296 determine the ordering. */
21297 if (!uses_deducible_template_parms (arg1
)
21298 && !uses_deducible_template_parms (arg2
))
21301 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21303 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21304 arg1
= TREE_TYPE (arg1
);
21305 quals1
= cp_type_quals (arg1
);
21308 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21310 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21311 arg2
= TREE_TYPE (arg2
);
21312 quals2
= cp_type_quals (arg2
);
21315 arg1
= TYPE_MAIN_VARIANT (arg1
);
21316 arg2
= TYPE_MAIN_VARIANT (arg2
);
21318 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21320 int i
, len2
= remaining_arguments (args2
);
21321 tree parmvec
= make_tree_vec (1);
21322 tree argvec
= make_tree_vec (len2
);
21325 /* Setup the parameter vector, which contains only ARG1. */
21326 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21328 /* Setup the argument vector, which contains the remaining
21330 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21331 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21333 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21334 argvec
, DEDUCE_EXACT
,
21335 /*subr=*/true, /*explain_p=*/false)
21338 /* We cannot deduce in the other direction, because ARG1 is
21339 a pack expansion but ARG2 is not. */
21342 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21344 int i
, len1
= remaining_arguments (args1
);
21345 tree parmvec
= make_tree_vec (1);
21346 tree argvec
= make_tree_vec (len1
);
21349 /* Setup the parameter vector, which contains only ARG1. */
21350 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21352 /* Setup the argument vector, which contains the remaining
21354 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21355 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21357 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21358 argvec
, DEDUCE_EXACT
,
21359 /*subr=*/true, /*explain_p=*/false)
21362 /* We cannot deduce in the other direction, because ARG2 is
21363 a pack expansion but ARG1 is not.*/
21369 /* The normal case, where neither argument is a pack
21371 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21372 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21374 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21375 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21379 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21380 arg2, then arg2 is not as specialized as arg1. */
21386 /* "If, for a given type, deduction succeeds in both directions
21387 (i.e., the types are identical after the transformations above)
21388 and both P and A were reference types (before being replaced with
21389 the type referred to above):
21390 - if the type from the argument template was an lvalue reference and
21391 the type from the parameter template was not, the argument type is
21392 considered to be more specialized than the other; otherwise,
21393 - if the type from the argument template is more cv-qualified
21394 than the type from the parameter template (as described above),
21395 the argument type is considered to be more specialized than the other;
21397 - neither type is more specialized than the other." */
21399 if (deduce1
&& deduce2
)
21401 if (ref1
&& ref2
&& ref1
!= ref2
)
21408 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21410 if ((quals1
& quals2
) == quals2
)
21412 if ((quals1
& quals2
) == quals1
)
21417 if (lose1
&& lose2
)
21418 /* We've failed to deduce something in either direction.
21419 These must be unordered. */
21424 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21425 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21426 /* We have already processed all of the arguments in our
21427 handing of the pack expansion type. */
21430 args1
= TREE_CHAIN (args1
);
21431 args2
= TREE_CHAIN (args2
);
21434 /* "In most cases, all template parameters must have values in order for
21435 deduction to succeed, but for partial ordering purposes a template
21436 parameter may remain without a value provided it is not used in the
21437 types being used for partial ordering."
21439 Thus, if we are missing any of the targs1 we need to substitute into
21440 origs1, then pat2 is not as specialized as pat1. This can happen when
21441 there is a nondeduced context. */
21442 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21444 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21447 processing_template_decl
--;
21449 /* If both deductions succeed, the partial ordering selects the more
21450 constrained template. */
21451 if (!lose1
&& !lose2
)
21453 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21454 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21455 lose1
= !subsumes_constraints (c1
, c2
);
21456 lose2
= !subsumes_constraints (c2
, c1
);
21459 /* All things being equal, if the next argument is a pack expansion
21460 for one function but not for the other, prefer the
21461 non-variadic function. FIXME this is bogus; see c++/41958. */
21463 && args1
&& TREE_VALUE (args1
)
21464 && args2
&& TREE_VALUE (args2
))
21466 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21467 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21470 if (lose1
== lose2
)
21478 /* Determine which of two partial specializations of TMPL is more
21481 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21482 to the first partial specialization. The TREE_PURPOSE is the
21483 innermost set of template parameters for the partial
21484 specialization. PAT2 is similar, but for the second template.
21486 Return 1 if the first partial specialization is more specialized;
21487 -1 if the second is more specialized; 0 if neither is more
21490 See [temp.class.order] for information about determining which of
21491 two templates is more specialized. */
21494 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21498 bool any_deductions
= false;
21500 tree tmpl1
= TREE_VALUE (pat1
);
21501 tree tmpl2
= TREE_VALUE (pat2
);
21502 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21503 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21505 /* Just like what happens for functions, if we are ordering between
21506 different template specializations, we may encounter dependent
21507 types in the arguments, and we need our dependency check functions
21508 to behave correctly. */
21509 ++processing_template_decl
;
21510 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21514 any_deductions
= true;
21517 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21521 any_deductions
= true;
21523 --processing_template_decl
;
21525 /* If both deductions succeed, the partial ordering selects the more
21526 constrained template. */
21527 if (!winner
&& any_deductions
)
21528 return more_constrained (tmpl1
, tmpl2
);
21530 /* In the case of a tie where at least one of the templates
21531 has a parameter pack at the end, the template with the most
21532 non-packed parameters wins. */
21535 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21536 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21538 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21539 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21540 int len1
= TREE_VEC_LENGTH (args1
);
21541 int len2
= TREE_VEC_LENGTH (args2
);
21543 /* We don't count the pack expansion at the end. */
21544 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21546 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21551 else if (len1
< len2
)
21558 /* Return the template arguments that will produce the function signature
21559 DECL from the function template FN, with the explicit template
21560 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21561 also match. Return NULL_TREE if no satisfactory arguments could be
21565 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21567 int ntparms
= DECL_NTPARMS (fn
);
21568 tree targs
= make_tree_vec (ntparms
);
21569 tree decl_type
= TREE_TYPE (decl
);
21570 tree decl_arg_types
;
21572 unsigned int nargs
, ix
;
21575 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21577 /* Never do unification on the 'this' parameter. */
21578 decl_arg_types
= skip_artificial_parms_for (decl
,
21579 TYPE_ARG_TYPES (decl_type
));
21581 nargs
= list_length (decl_arg_types
);
21582 args
= XALLOCAVEC (tree
, nargs
);
21583 for (arg
= decl_arg_types
, ix
= 0;
21584 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21585 arg
= TREE_CHAIN (arg
), ++ix
)
21586 args
[ix
] = TREE_VALUE (arg
);
21588 if (fn_type_unification (fn
, explicit_args
, targs
,
21590 (check_rettype
|| DECL_CONV_FN_P (fn
)
21591 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21592 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21594 == error_mark_node
)
21600 /* Return the innermost template arguments that, when applied to a partial
21601 specialization SPEC_TMPL of TMPL, yield the ARGS.
21603 For example, suppose we have:
21605 template <class T, class U> struct S {};
21606 template <class T> struct S<T*, int> {};
21608 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21609 partial specialization and the ARGS will be {double*, int}. The resulting
21610 vector will be {double}, indicating that `T' is bound to `double'. */
21613 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21615 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21617 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21618 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21620 tree innermost_deduced_args
;
21622 innermost_deduced_args
= make_tree_vec (ntparms
);
21623 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21625 deduced_args
= copy_node (args
);
21626 SET_TMPL_ARGS_LEVEL (deduced_args
,
21627 TMPL_ARGS_DEPTH (deduced_args
),
21628 innermost_deduced_args
);
21631 deduced_args
= innermost_deduced_args
;
21633 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
21635 if (unify (tparms
, deduced_args
,
21636 INNERMOST_TEMPLATE_ARGS (spec_args
),
21637 INNERMOST_TEMPLATE_ARGS (args
),
21638 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
21641 for (i
= 0; i
< ntparms
; ++i
)
21642 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
21644 if (!tried_array_deduction
)
21646 try_array_deduction (tparms
, innermost_deduced_args
,
21647 INNERMOST_TEMPLATE_ARGS (spec_args
));
21648 tried_array_deduction
= true;
21649 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
21655 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
21656 if (!push_tinst_level (tinst
))
21658 excessive_deduction_depth
= true;
21662 /* Verify that nondeduced template arguments agree with the type
21663 obtained from argument deduction.
21667 struct A { typedef int X; };
21668 template <class T, class U> struct C {};
21669 template <class T> struct C<T, typename T::X> {};
21671 Then with the instantiation `C<A, int>', we can deduce that
21672 `T' is `A' but unify () does not check whether `typename T::X'
21674 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
21676 if (spec_args
!= error_mark_node
)
21677 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
21678 INNERMOST_TEMPLATE_ARGS (spec_args
),
21679 tmpl
, tf_none
, false, false);
21681 pop_tinst_level ();
21683 if (spec_args
== error_mark_node
21684 /* We only need to check the innermost arguments; the other
21685 arguments will always agree. */
21686 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
21687 INNERMOST_TEMPLATE_ARGS (args
)))
21690 /* Now that we have bindings for all of the template arguments,
21691 ensure that the arguments deduced for the template template
21692 parameters have compatible template parameter lists. See the use
21693 of template_template_parm_bindings_ok_p in fn_type_unification
21694 for more information. */
21695 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
21698 return deduced_args
;
21701 // Compare two function templates T1 and T2 by deducing bindings
21702 // from one against the other. If both deductions succeed, compare
21703 // constraints to see which is more constrained.
21705 more_specialized_inst (tree t1
, tree t2
)
21710 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
21716 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
21722 // If both deductions succeed, then one may be more constrained.
21723 if (count
== 2 && fate
== 0)
21724 fate
= more_constrained (t1
, t2
);
21729 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21730 Return the TREE_LIST node with the most specialized template, if
21731 any. If there is no most specialized template, the error_mark_node
21734 Note that this function does not look at, or modify, the
21735 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21736 returned is one of the elements of INSTANTIATIONS, callers may
21737 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21738 and retrieve it from the value returned. */
21741 most_specialized_instantiation (tree templates
)
21745 ++processing_template_decl
;
21748 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
21750 gcc_assert (TREE_VALUE (champ
) != TREE_VALUE (fn
));
21751 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
21756 /* Equally specialized, move to next function. If there
21757 is no next function, nothing's most specialized. */
21758 fn
= TREE_CHAIN (fn
);
21766 /* Now verify that champ is better than everything earlier in the
21767 instantiation list. */
21768 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
21769 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
21776 processing_template_decl
--;
21779 return error_mark_node
;
21784 /* If DECL is a specialization of some template, return the most
21785 general such template. Otherwise, returns NULL_TREE.
21787 For example, given:
21789 template <class T> struct S { template <class U> void f(U); };
21791 if TMPL is `template <class U> void S<int>::f(U)' this will return
21792 the full template. This function will not trace past partial
21793 specializations, however. For example, given in addition:
21795 template <class T> struct S<T*> { template <class U> void f(U); };
21797 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21798 `template <class T> template <class U> S<T*>::f(U)'. */
21801 most_general_template (tree decl
)
21803 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21805 if (tree tinfo
= get_template_info (decl
))
21806 decl
= TI_TEMPLATE (tinfo
);
21807 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21808 template friend, or a FIELD_DECL for a capture pack. */
21809 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21813 /* Look for more and more general templates. */
21814 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
21816 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21817 (See cp-tree.h for details.) */
21818 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
21821 if (CLASS_TYPE_P (TREE_TYPE (decl
))
21822 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
21823 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
21826 /* Stop if we run into an explicitly specialized class template. */
21827 if (!DECL_NAMESPACE_SCOPE_P (decl
)
21828 && DECL_CONTEXT (decl
)
21829 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
21832 decl
= DECL_TI_TEMPLATE (decl
);
21838 /* Return the most specialized of the template partial specializations
21839 which can produce TARGET, a specialization of some class or variable
21840 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21841 a TEMPLATE_DECL node corresponding to the partial specialization, while
21842 the TREE_PURPOSE is the set of template arguments that must be
21843 substituted into the template pattern in order to generate TARGET.
21845 If the choice of partial specialization is ambiguous, a diagnostic
21846 is issued, and the error_mark_node is returned. If there are no
21847 partial specializations matching TARGET, then NULL_TREE is
21848 returned, indicating that the primary template should be used. */
21851 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
21853 tree list
= NULL_TREE
;
21858 tree outer_args
= NULL_TREE
;
21861 if (TYPE_P (target
))
21863 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
21864 tmpl
= TI_TEMPLATE (tinfo
);
21865 args
= TI_ARGS (tinfo
);
21867 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
21869 tmpl
= TREE_OPERAND (target
, 0);
21870 args
= TREE_OPERAND (target
, 1);
21872 else if (VAR_P (target
))
21874 tree tinfo
= DECL_TEMPLATE_INFO (target
);
21875 tmpl
= TI_TEMPLATE (tinfo
);
21876 args
= TI_ARGS (tinfo
);
21879 gcc_unreachable ();
21881 tree main_tmpl
= most_general_template (tmpl
);
21883 /* For determining which partial specialization to use, only the
21884 innermost args are interesting. */
21885 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21887 outer_args
= strip_innermost_template_args (args
, 1);
21888 args
= INNERMOST_TEMPLATE_ARGS (args
);
21891 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
21894 tree spec_tmpl
= TREE_VALUE (t
);
21898 /* Substitute in the template args from the enclosing class. */
21899 ++processing_template_decl
;
21900 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
21901 --processing_template_decl
;
21904 if (spec_tmpl
== error_mark_node
)
21905 return error_mark_node
;
21907 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
21911 spec_args
= add_to_template_args (outer_args
, spec_args
);
21913 /* Keep the candidate only if the constraints are satisfied,
21914 or if we're not compiling with concepts. */
21916 || constraints_satisfied_p (spec_tmpl
, spec_args
))
21918 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
21919 TREE_TYPE (list
) = TREE_TYPE (t
);
21927 ambiguous_p
= false;
21930 t
= TREE_CHAIN (t
);
21931 for (; t
; t
= TREE_CHAIN (t
))
21933 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21940 t
= TREE_CHAIN (t
);
21943 ambiguous_p
= true;
21952 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
21954 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21957 ambiguous_p
= true;
21965 char *spaces
= NULL
;
21966 if (!(complain
& tf_error
))
21967 return error_mark_node
;
21968 if (TYPE_P (target
))
21969 error ("ambiguous template instantiation for %q#T", target
);
21971 error ("ambiguous template instantiation for %q#D", target
);
21972 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
21973 for (t
= list
; t
; t
= TREE_CHAIN (t
))
21975 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
21976 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
21977 "%s %#qS", spaces
? spaces
: str
, subst
);
21978 spaces
= spaces
? spaces
: get_spaces (str
);
21981 return error_mark_node
;
21987 /* Explicitly instantiate DECL. */
21990 do_decl_instantiation (tree decl
, tree storage
)
21992 tree result
= NULL_TREE
;
21995 if (!decl
|| decl
== error_mark_node
)
21996 /* An error occurred, for which grokdeclarator has already issued
21997 an appropriate message. */
21999 else if (! DECL_LANG_SPECIFIC (decl
))
22001 error ("explicit instantiation of non-template %q#D", decl
);
22005 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
22006 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
22008 if (VAR_P (decl
) && !var_templ
)
22010 /* There is an asymmetry here in the way VAR_DECLs and
22011 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22012 the latter, the DECL we get back will be marked as a
22013 template instantiation, and the appropriate
22014 DECL_TEMPLATE_INFO will be set up. This does not happen for
22015 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22016 should handle VAR_DECLs as it currently handles
22018 if (!DECL_CLASS_SCOPE_P (decl
))
22020 error ("%qD is not a static data member of a class template", decl
);
22023 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22024 if (!result
|| !VAR_P (result
))
22026 error ("no matching template for %qD found", decl
);
22029 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22031 error ("type %qT for explicit instantiation %qD does not match "
22032 "declared type %qT", TREE_TYPE (result
), decl
,
22037 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22039 error ("explicit instantiation of %q#D", decl
);
22045 /* Check for various error cases. Note that if the explicit
22046 instantiation is valid the RESULT will currently be marked as an
22047 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22048 until we get here. */
22050 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22052 /* DR 259 [temp.spec].
22054 Both an explicit instantiation and a declaration of an explicit
22055 specialization shall not appear in a program unless the explicit
22056 instantiation follows a declaration of the explicit specialization.
22058 For a given set of template parameters, if an explicit
22059 instantiation of a template appears after a declaration of an
22060 explicit specialization for that template, the explicit
22061 instantiation has no effect. */
22064 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22068 No program shall explicitly instantiate any template more
22071 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22072 the first instantiation was `extern' and the second is not,
22073 and EXTERN_P for the opposite case. */
22074 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22075 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22076 /* If an "extern" explicit instantiation follows an ordinary
22077 explicit instantiation, the template is instantiated. */
22081 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22083 error ("no matching template for %qD found", result
);
22086 else if (!DECL_TEMPLATE_INFO (result
))
22088 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22092 if (storage
== NULL_TREE
)
22094 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22096 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22097 pedwarn (input_location
, OPT_Wpedantic
,
22098 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22103 error ("storage class %qD applied to template instantiation", storage
);
22105 check_explicit_instantiation_namespace (result
);
22106 mark_decl_instantiated (result
, extern_p
);
22108 instantiate_decl (result
, /*defer_ok=*/true,
22109 /*expl_inst_class_mem_p=*/false);
22113 mark_class_instantiated (tree t
, int extern_p
)
22115 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22116 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22117 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22118 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22121 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22122 rest_of_type_compilation (t
, 1);
22126 /* Called from do_type_instantiation through binding_table_foreach to
22127 do recursive instantiation for the type bound in ENTRY. */
22129 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22131 tree storage
= *(tree
*) data
;
22133 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22134 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22135 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22138 /* Called from do_type_instantiation to instantiate a member
22139 (a member function or a static member variable) of an
22140 explicitly instantiated class template. */
22142 instantiate_class_member (tree decl
, int extern_p
)
22144 mark_decl_instantiated (decl
, extern_p
);
22146 instantiate_decl (decl
, /*defer_ok=*/true,
22147 /*expl_inst_class_mem_p=*/true);
22150 /* Perform an explicit instantiation of template class T. STORAGE, if
22151 non-null, is the RID for extern, inline or static. COMPLAIN is
22152 nonzero if this is called from the parser, zero if called recursively,
22153 since the standard is unclear (as detailed below). */
22156 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22161 int previous_instantiation_extern_p
= 0;
22163 if (TREE_CODE (t
) == TYPE_DECL
)
22166 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22169 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22171 error ("explicit instantiation of non-class template %qD", tmpl
);
22173 error ("explicit instantiation of non-template type %qT", t
);
22179 if (!COMPLETE_TYPE_P (t
))
22181 if (complain
& tf_error
)
22182 error ("explicit instantiation of %q#T before definition of template",
22187 if (storage
!= NULL_TREE
)
22189 if (!in_system_header_at (input_location
))
22191 if (storage
== ridpointers
[(int) RID_EXTERN
])
22193 if (cxx_dialect
== cxx98
)
22194 pedwarn (input_location
, OPT_Wpedantic
,
22195 "ISO C++ 1998 forbids the use of %<extern%> on "
22196 "explicit instantiations");
22199 pedwarn (input_location
, OPT_Wpedantic
,
22200 "ISO C++ forbids the use of %qE"
22201 " on explicit instantiations", storage
);
22204 if (storage
== ridpointers
[(int) RID_INLINE
])
22206 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22208 else if (storage
== ridpointers
[(int) RID_STATIC
])
22212 error ("storage class %qD applied to template instantiation",
22218 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22220 /* DR 259 [temp.spec].
22222 Both an explicit instantiation and a declaration of an explicit
22223 specialization shall not appear in a program unless the explicit
22224 instantiation follows a declaration of the explicit specialization.
22226 For a given set of template parameters, if an explicit
22227 instantiation of a template appears after a declaration of an
22228 explicit specialization for that template, the explicit
22229 instantiation has no effect. */
22232 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22236 No program shall explicitly instantiate any template more
22239 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22240 instantiation was `extern'. If EXTERN_P then the second is.
22241 These cases are OK. */
22242 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22244 if (!previous_instantiation_extern_p
&& !extern_p
22245 && (complain
& tf_error
))
22246 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22248 /* If we've already instantiated the template, just return now. */
22249 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22253 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22254 mark_class_instantiated (t
, extern_p
);
22262 /* In contrast to implicit instantiation, where only the
22263 declarations, and not the definitions, of members are
22264 instantiated, we have here:
22268 The explicit instantiation of a class template specialization
22269 implies the instantiation of all of its members not
22270 previously explicitly specialized in the translation unit
22271 containing the explicit instantiation.
22273 Of course, we can't instantiate member template classes, since
22274 we don't have any arguments for them. Note that the standard
22275 is unclear on whether the instantiation of the members are
22276 *explicit* instantiations or not. However, the most natural
22277 interpretation is that it should be an explicit instantiation. */
22280 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
22281 if (TREE_CODE (tmp
) == FUNCTION_DECL
22282 && DECL_TEMPLATE_INSTANTIATION (tmp
)
22283 && user_provided_p (tmp
))
22284 instantiate_class_member (tmp
, extern_p
);
22286 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
22287 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
22288 instantiate_class_member (tmp
, extern_p
);
22290 if (CLASSTYPE_NESTED_UTDS (t
))
22291 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22292 bt_instantiate_type_proc
, &storage
);
22296 /* Given a function DECL, which is a specialization of TMPL, modify
22297 DECL to be a re-instantiation of TMPL with the same template
22298 arguments. TMPL should be the template into which tsubst'ing
22299 should occur for DECL, not the most general template.
22301 One reason for doing this is a scenario like this:
22304 void f(const T&, int i);
22306 void g() { f(3, 7); }
22309 void f(const T& t, const int i) { }
22311 Note that when the template is first instantiated, with
22312 instantiate_template, the resulting DECL will have no name for the
22313 first parameter, and the wrong type for the second. So, when we go
22314 to instantiate the DECL, we regenerate it. */
22317 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22319 /* The arguments used to instantiate DECL, from the most general
22323 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22325 /* Make sure that we can see identifiers, and compute access
22327 push_access_scope (decl
);
22329 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22337 args_depth
= TMPL_ARGS_DEPTH (args
);
22338 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22339 if (args_depth
> parms_depth
)
22340 args
= get_innermost_template_args (args
, parms_depth
);
22342 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22343 args
, tf_error
, NULL_TREE
,
22344 /*defer_ok*/false);
22345 if (specs
&& specs
!= error_mark_node
)
22346 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22349 /* Merge parameter declarations. */
22350 decl_parm
= skip_artificial_parms_for (decl
,
22351 DECL_ARGUMENTS (decl
));
22353 = skip_artificial_parms_for (code_pattern
,
22354 DECL_ARGUMENTS (code_pattern
));
22355 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22360 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22361 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22362 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22364 parm_type
= type_decays_to (parm_type
);
22365 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22366 TREE_TYPE (decl_parm
) = parm_type
;
22367 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22368 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22370 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22371 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22373 decl_parm
= DECL_CHAIN (decl_parm
);
22374 pattern_parm
= DECL_CHAIN (pattern_parm
);
22376 /* Merge any parameters that match with the function parameter
22378 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22381 tree expanded_types
;
22382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22383 the parameters in this function parameter pack. */
22384 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22385 args
, tf_error
, NULL_TREE
);
22386 len
= TREE_VEC_LENGTH (expanded_types
);
22387 for (i
= 0; i
< len
; i
++)
22392 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22393 /* Rename the parameter to include the index. */
22394 DECL_NAME (decl_parm
) =
22395 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22396 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22397 parm_type
= type_decays_to (parm_type
);
22398 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22399 TREE_TYPE (decl_parm
) = parm_type
;
22400 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22401 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22403 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22404 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22406 decl_parm
= DECL_CHAIN (decl_parm
);
22409 /* Merge additional specifiers from the CODE_PATTERN. */
22410 if (DECL_DECLARED_INLINE_P (code_pattern
)
22411 && !DECL_DECLARED_INLINE_P (decl
))
22412 DECL_DECLARED_INLINE_P (decl
) = 1;
22414 else if (VAR_P (decl
))
22416 DECL_INITIAL (decl
) =
22417 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22418 tf_error
, DECL_TI_TEMPLATE (decl
),
22419 /*integral_constant_expression_p=*/false);
22420 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22421 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22422 tf_error
, DECL_TI_TEMPLATE (decl
));
22425 gcc_unreachable ();
22427 pop_access_scope (decl
);
22430 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22431 substituted to get DECL. */
22434 template_for_substitution (tree decl
)
22436 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22438 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22439 for the instantiation. This is not always the most general
22440 template. Consider, for example:
22443 struct S { template <class U> void f();
22444 template <> void f<int>(); };
22446 and an instantiation of S<double>::f<int>. We want TD to be the
22447 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22448 while (/* An instantiation cannot have a definition, so we need a
22449 more general template. */
22450 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22451 /* We must also deal with friend templates. Given:
22453 template <class T> struct S {
22454 template <class U> friend void f() {};
22457 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22458 so far as the language is concerned, but that's still
22459 where we get the pattern for the instantiation from. On
22460 other hand, if the definition comes outside the class, say:
22462 template <class T> struct S {
22463 template <class U> friend void f();
22465 template <class U> friend void f() {}
22467 we don't need to look any further. That's what the check for
22468 DECL_INITIAL is for. */
22469 || (TREE_CODE (decl
) == FUNCTION_DECL
22470 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22471 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22473 /* The present template, TD, should not be a definition. If it
22474 were a definition, we should be using it! Note that we
22475 cannot restructure the loop to just keep going until we find
22476 a template with a definition, since that might go too far if
22477 a specialization was declared, but not defined. */
22479 /* Fetch the more general template. */
22480 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22486 /* Returns true if we need to instantiate this template instance even if we
22487 know we aren't going to emit it. */
22490 always_instantiate_p (tree decl
)
22492 /* We always instantiate inline functions so that we can inline them. An
22493 explicit instantiation declaration prohibits implicit instantiation of
22494 non-inline functions. With high levels of optimization, we would
22495 normally inline non-inline functions -- but we're not allowed to do
22496 that for "extern template" functions. Therefore, we check
22497 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22498 return ((TREE_CODE (decl
) == FUNCTION_DECL
22499 && (DECL_DECLARED_INLINE_P (decl
)
22500 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22501 /* And we need to instantiate static data members so that
22502 their initializers are available in integral constant
22505 && decl_maybe_constant_var_p (decl
)));
22508 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22509 instantiate it now, modifying TREE_TYPE (fn). */
22512 maybe_instantiate_noexcept (tree fn
)
22514 tree fntype
, spec
, noex
, clone
;
22516 /* Don't instantiate a noexcept-specification from template context. */
22517 if (processing_template_decl
)
22520 if (DECL_CLONED_FUNCTION_P (fn
))
22521 fn
= DECL_CLONED_FUNCTION (fn
);
22522 fntype
= TREE_TYPE (fn
);
22523 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22525 if (!spec
|| !TREE_PURPOSE (spec
))
22528 noex
= TREE_PURPOSE (spec
);
22530 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22532 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22533 spec
= get_defaulted_eh_spec (fn
);
22534 else if (push_tinst_level (fn
))
22536 push_access_scope (fn
);
22537 push_deferring_access_checks (dk_no_deferred
);
22538 input_location
= DECL_SOURCE_LOCATION (fn
);
22539 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22540 DEFERRED_NOEXCEPT_ARGS (noex
),
22541 tf_warning_or_error
, fn
,
22542 /*function_p=*/false,
22543 /*integral_constant_expression_p=*/true);
22544 pop_deferring_access_checks ();
22545 pop_access_scope (fn
);
22546 pop_tinst_level ();
22547 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22548 if (spec
== error_mark_node
)
22549 spec
= noexcept_false_spec
;
22552 spec
= noexcept_false_spec
;
22554 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22557 FOR_EACH_CLONE (clone
, fn
)
22559 if (TREE_TYPE (clone
) == fntype
)
22560 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22562 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22566 /* Produce the definition of D, a _DECL generated from a template. If
22567 DEFER_OK is true, then we don't have to actually do the
22568 instantiation now; we just have to do it sometime. Normally it is
22569 an error if this is an explicit instantiation but D is undefined.
22570 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22571 instantiated class template. */
22574 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22576 tree tmpl
= DECL_TI_TEMPLATE (d
);
22583 bool pattern_defined
;
22584 location_t saved_loc
= input_location
;
22585 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22586 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22590 /* This function should only be used to instantiate templates for
22591 functions and static member variables. */
22592 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22594 /* A concept is never instantiated. */
22595 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
22597 /* Variables are never deferred; if instantiation is required, they
22598 are instantiated right away. That allows for better code in the
22599 case that an expression refers to the value of the variable --
22600 if the variable has a constant value the referring expression can
22601 take advantage of that fact. */
22605 /* Don't instantiate cloned functions. Instead, instantiate the
22606 functions they cloned. */
22607 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
22608 d
= DECL_CLONED_FUNCTION (d
);
22610 if (DECL_TEMPLATE_INSTANTIATED (d
)
22611 || (TREE_CODE (d
) == FUNCTION_DECL
22612 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
22613 || DECL_TEMPLATE_SPECIALIZATION (d
))
22614 /* D has already been instantiated or explicitly specialized, so
22615 there's nothing for us to do here.
22617 It might seem reasonable to check whether or not D is an explicit
22618 instantiation, and, if so, stop here. But when an explicit
22619 instantiation is deferred until the end of the compilation,
22620 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22621 the instantiation. */
22624 /* Check to see whether we know that this template will be
22625 instantiated in some other file, as with "extern template"
22627 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
22629 /* In general, we do not instantiate such templates. */
22630 if (external_p
&& !always_instantiate_p (d
))
22633 gen_tmpl
= most_general_template (tmpl
);
22634 gen_args
= DECL_TI_ARGS (d
);
22636 if (tmpl
!= gen_tmpl
)
22637 /* We should already have the extra args. */
22638 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
22639 == TMPL_ARGS_DEPTH (gen_args
));
22640 /* And what's in the hash table should match D. */
22641 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
22642 || spec
== NULL_TREE
);
22644 /* This needs to happen before any tsubsting. */
22645 if (! push_tinst_level (d
))
22648 timevar_push (TV_TEMPLATE_INST
);
22650 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22651 for the instantiation. */
22652 td
= template_for_substitution (d
);
22657 /* Look up an explicit specialization, if any. */
22658 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
22659 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
22660 if (elt
&& elt
!= error_mark_node
)
22662 td
= TREE_VALUE (elt
);
22663 args
= TREE_PURPOSE (elt
);
22667 code_pattern
= DECL_TEMPLATE_RESULT (td
);
22669 /* We should never be trying to instantiate a member of a class
22670 template or partial specialization. */
22671 gcc_assert (d
!= code_pattern
);
22673 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
22674 || DECL_TEMPLATE_SPECIALIZATION (td
))
22675 /* In the case of a friend template whose definition is provided
22676 outside the class, we may have too many arguments. Drop the
22677 ones we don't need. The same is true for specializations. */
22678 args
= get_innermost_template_args
22679 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
22681 if (TREE_CODE (d
) == FUNCTION_DECL
)
22683 deleted_p
= DECL_DELETED_FN (code_pattern
);
22684 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
22685 && DECL_INITIAL (code_pattern
) != error_mark_node
)
22686 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
22692 if (DECL_CLASS_SCOPE_P (code_pattern
))
22693 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
22694 || DECL_INLINE_VAR_P (code_pattern
));
22696 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
22699 /* We may be in the middle of deferred access check. Disable it now. */
22700 push_deferring_access_checks (dk_no_deferred
);
22702 /* Unless an explicit instantiation directive has already determined
22703 the linkage of D, remember that a definition is available for
22705 if (pattern_defined
22706 && !DECL_INTERFACE_KNOWN (d
)
22707 && !DECL_NOT_REALLY_EXTERN (d
))
22708 mark_definable (d
);
22710 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
22711 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
22712 input_location
= DECL_SOURCE_LOCATION (d
);
22714 /* If D is a member of an explicitly instantiated class template,
22715 and no definition is available, treat it like an implicit
22717 if (!pattern_defined
&& expl_inst_class_mem_p
22718 && DECL_EXPLICIT_INSTANTIATION (d
))
22720 /* Leave linkage flags alone on instantiations with anonymous
22722 if (TREE_PUBLIC (d
))
22724 DECL_NOT_REALLY_EXTERN (d
) = 0;
22725 DECL_INTERFACE_KNOWN (d
) = 0;
22727 SET_DECL_IMPLICIT_INSTANTIATION (d
);
22730 /* Defer all other templates, unless we have been explicitly
22731 forbidden from doing so. */
22732 if (/* If there is no definition, we cannot instantiate the
22735 /* If it's OK to postpone instantiation, do so. */
22737 /* If this is a static data member that will be defined
22738 elsewhere, we don't want to instantiate the entire data
22739 member, but we do want to instantiate the initializer so that
22740 we can substitute that elsewhere. */
22741 || (external_p
&& VAR_P (d
))
22742 /* Handle here a deleted function too, avoid generating
22743 its body (c++/61080). */
22746 /* The definition of the static data member is now required so
22747 we must substitute the initializer. */
22749 && !DECL_INITIAL (d
)
22750 && DECL_INITIAL (code_pattern
))
22754 bool const_init
= false;
22755 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22757 ns
= decl_namespace_context (d
);
22758 push_nested_namespace (ns
);
22760 push_nested_class (DECL_CONTEXT (d
));
22761 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
22763 tf_warning_or_error
, NULL_TREE
,
22764 /*integral_constant_expression_p=*/false);
22765 /* If instantiating the initializer involved instantiating this
22766 again, don't call cp_finish_decl twice. */
22767 if (!DECL_INITIAL (d
))
22769 /* Make sure the initializer is still constant, in case of
22770 circular dependency (template/instantiate6.C). */
22772 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22773 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
22774 /*asmspec_tree=*/NULL_TREE
,
22775 LOOKUP_ONLYCONVERTING
);
22778 pop_nested_class ();
22779 pop_nested_namespace (ns
);
22782 /* We restore the source position here because it's used by
22783 add_pending_template. */
22784 input_location
= saved_loc
;
22786 if (at_eof
&& !pattern_defined
22787 && DECL_EXPLICIT_INSTANTIATION (d
)
22788 && DECL_NOT_REALLY_EXTERN (d
))
22791 The definition of a non-exported function template, a
22792 non-exported member function template, or a non-exported
22793 member function or static data member of a class template
22794 shall be present in every translation unit in which it is
22795 explicitly instantiated. */
22796 permerror (input_location
, "explicit instantiation of %qD "
22797 "but no definition available", d
);
22799 /* If we're in unevaluated context, we just wanted to get the
22800 constant value; this isn't an odr use, so don't queue
22801 a full instantiation. */
22802 if (cp_unevaluated_operand
!= 0)
22804 /* ??? Historically, we have instantiated inline functions, even
22805 when marked as "extern template". */
22806 if (!(external_p
&& VAR_P (d
)))
22807 add_pending_template (d
);
22810 /* Tell the repository that D is available in this translation unit
22811 -- and see if it is supposed to be instantiated here. */
22812 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
22814 /* In a PCH file, despite the fact that the repository hasn't
22815 requested instantiation in the PCH it is still possible that
22816 an instantiation will be required in a file that includes the
22819 add_pending_template (d
);
22820 /* Instantiate inline functions so that the inliner can do its
22821 job, even though we'll not be emitting a copy of this
22823 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
22827 bool push_to_top
, nested
;
22829 fn_context
= decl_function_context (d
);
22830 nested
= current_function_decl
!= NULL_TREE
;
22831 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
22833 vec
<tree
> omp_privatization_save
;
22835 save_omp_privatization_clauses (omp_privatization_save
);
22838 push_to_top_level ();
22841 push_function_context ();
22842 cp_unevaluated_operand
= 0;
22843 c_inhibit_evaluation_warnings
= 0;
22846 /* Mark D as instantiated so that recursive calls to
22847 instantiate_decl do not try to instantiate it again. */
22848 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
22850 /* Regenerate the declaration in case the template has been modified
22851 by a subsequent redeclaration. */
22852 regenerate_decl_from_template (d
, td
, args
);
22854 /* We already set the file and line above. Reset them now in case
22855 they changed as a result of calling regenerate_decl_from_template. */
22856 input_location
= DECL_SOURCE_LOCATION (d
);
22861 bool const_init
= false;
22863 /* Clear out DECL_RTL; whatever was there before may not be right
22864 since we've reset the type of the declaration. */
22865 SET_DECL_RTL (d
, NULL
);
22866 DECL_IN_AGGR_P (d
) = 0;
22868 /* The initializer is placed in DECL_INITIAL by
22869 regenerate_decl_from_template so we don't need to
22870 push/pop_access_scope again here. Pull it out so that
22871 cp_finish_decl can process it. */
22872 init
= DECL_INITIAL (d
);
22873 DECL_INITIAL (d
) = NULL_TREE
;
22874 DECL_INITIALIZED_P (d
) = 0;
22876 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22877 initializer. That function will defer actual emission until
22878 we have a chance to determine linkage. */
22879 DECL_EXTERNAL (d
) = 0;
22881 /* Enter the scope of D so that access-checking works correctly. */
22882 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22884 push_nested_class (DECL_CONTEXT (d
));
22886 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22887 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
22890 pop_nested_class ();
22892 if (variable_template_p (gen_tmpl
))
22893 note_variable_template_instantiation (d
);
22895 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
22896 synthesize_method (d
);
22897 else if (TREE_CODE (d
) == FUNCTION_DECL
)
22899 hash_map
<tree
, tree
> *saved_local_specializations
;
22902 tree block
= NULL_TREE
;
22903 tree lambda_ctx
= NULL_TREE
;
22905 /* Save away the current list, in case we are instantiating one
22906 template from within the body of another. */
22907 saved_local_specializations
= local_specializations
;
22909 /* Set up the list of local specializations. */
22910 local_specializations
= new hash_map
<tree
, tree
>;
22912 /* Set up context. */
22913 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22914 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22915 block
= push_stmt_list ();
22918 if (push_to_top
&& LAMBDA_FUNCTION_P (d
))
22920 /* When instantiating a lambda's templated function
22921 operator, we need to push the non-lambda class scope
22922 of the lambda itself so that the nested function
22923 stack is sufficiently correct to deal with this
22925 lambda_ctx
= DECL_CONTEXT (d
);
22927 lambda_ctx
= decl_type_context (TYPE_NAME (lambda_ctx
));
22928 while (lambda_ctx
&& LAMBDA_TYPE_P (lambda_ctx
));
22930 push_nested_class (lambda_ctx
);
22932 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
22935 /* Some typedefs referenced from within the template code need to be
22936 access checked at template instantiation time, i.e now. These
22937 types were added to the template at parsing time. Let's get those
22938 and perform the access checks then. */
22939 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
22942 /* Create substitution entries for the parameters. */
22943 tmpl_parm
= DECL_ARGUMENTS (code_pattern
);
22944 spec_parm
= DECL_ARGUMENTS (d
);
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
22947 register_local_specialization (spec_parm
, tmpl_parm
);
22948 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
22949 tmpl_parm
= skip_artificial_parms_for (code_pattern
, tmpl_parm
);
22951 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22953 if (!DECL_PACK_P (tmpl_parm
))
22955 register_local_specialization (spec_parm
, tmpl_parm
);
22956 spec_parm
= DECL_CHAIN (spec_parm
);
22960 /* Register the (value) argument pack as a specialization of
22961 TMPL_PARM, then move on. */
22962 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22963 register_local_specialization (argpack
, tmpl_parm
);
22966 gcc_assert (!spec_parm
);
22968 /* Substitute into the body of the function. */
22969 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22970 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
22971 tf_warning_or_error
, tmpl
);
22974 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
22975 tf_warning_or_error
, tmpl
,
22976 /*integral_constant_expression_p=*/false);
22978 /* Set the current input_location to the end of the function
22979 so that finish_function knows where we are. */
22981 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
22983 /* Remember if we saw an infinite loop in the template. */
22984 current_function_infinite_loop
22985 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
22988 /* We don't need the local specializations any more. */
22989 delete local_specializations
;
22990 local_specializations
= saved_local_specializations
;
22992 /* Finish the function. */
22993 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22994 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22995 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
22998 d
= finish_function (0);
22999 expand_or_defer_fn (d
);
23002 pop_nested_class ();
23004 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
23005 cp_check_omp_declare_reduction (d
);
23008 /* We're not deferring instantiation any more. */
23009 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
23012 pop_from_top_level ();
23014 pop_function_context ();
23017 restore_omp_privatization_clauses (omp_privatization_save
);
23020 pop_deferring_access_checks ();
23021 timevar_pop (TV_TEMPLATE_INST
);
23022 pop_tinst_level ();
23023 input_location
= saved_loc
;
23024 cp_unevaluated_operand
= saved_unevaluated_operand
;
23025 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23030 /* Run through the list of templates that we wish we could
23031 instantiate, and instantiate any we can. RETRIES is the
23032 number of times we retry pending template instantiation. */
23035 instantiate_pending_templates (int retries
)
23038 location_t saved_loc
= input_location
;
23040 /* Instantiating templates may trigger vtable generation. This in turn
23041 may require further template instantiations. We place a limit here
23042 to avoid infinite loop. */
23043 if (pending_templates
&& retries
>= max_tinst_depth
)
23045 tree decl
= pending_templates
->tinst
->decl
;
23047 fatal_error (input_location
,
23048 "template instantiation depth exceeds maximum of %d"
23049 " instantiating %q+D, possibly from virtual table generation"
23050 " (use -ftemplate-depth= to increase the maximum)",
23051 max_tinst_depth
, decl
);
23052 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23053 /* Pretend that we defined it. */
23054 DECL_INITIAL (decl
) = error_mark_node
;
23060 struct pending_template
**t
= &pending_templates
;
23061 struct pending_template
*last
= NULL
;
23065 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23066 bool complete
= false;
23068 if (TYPE_P (instantiation
))
23072 if (!COMPLETE_TYPE_P (instantiation
))
23074 instantiate_class_template (instantiation
);
23075 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23076 for (fn
= TYPE_METHODS (instantiation
);
23078 fn
= TREE_CHAIN (fn
))
23079 if (! DECL_ARTIFICIAL (fn
))
23080 instantiate_decl (fn
,
23081 /*defer_ok=*/false,
23082 /*expl_inst_class_mem_p=*/false);
23083 if (COMPLETE_TYPE_P (instantiation
))
23087 complete
= COMPLETE_TYPE_P (instantiation
);
23091 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23092 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23095 = instantiate_decl (instantiation
,
23096 /*defer_ok=*/false,
23097 /*expl_inst_class_mem_p=*/false);
23098 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23102 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23103 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23107 /* If INSTANTIATION has been instantiated, then we don't
23108 need to consider it again in the future. */
23116 current_tinst_level
= NULL
;
23118 last_pending_template
= last
;
23120 while (reconsider
);
23122 input_location
= saved_loc
;
23125 /* Substitute ARGVEC into T, which is a list of initializers for
23126 either base class or a non-static data member. The TREE_PURPOSEs
23127 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23128 instantiate_decl. */
23131 tsubst_initializer_list (tree t
, tree argvec
)
23133 tree inits
= NULL_TREE
;
23135 for (; t
; t
= TREE_CHAIN (t
))
23139 tree expanded_bases
= NULL_TREE
;
23140 tree expanded_arguments
= NULL_TREE
;
23143 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23148 /* Expand the base class expansion type into separate base
23150 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23151 tf_warning_or_error
,
23153 if (expanded_bases
== error_mark_node
)
23156 /* We'll be building separate TREE_LISTs of arguments for
23158 len
= TREE_VEC_LENGTH (expanded_bases
);
23159 expanded_arguments
= make_tree_vec (len
);
23160 for (i
= 0; i
< len
; i
++)
23161 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23163 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23164 expand each argument in the TREE_VALUE of t. */
23165 expr
= make_node (EXPR_PACK_EXPANSION
);
23166 PACK_EXPANSION_LOCAL_P (expr
) = true;
23167 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23168 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23170 if (TREE_VALUE (t
) == void_type_node
)
23171 /* VOID_TYPE_NODE is used to indicate
23172 value-initialization. */
23174 for (i
= 0; i
< len
; i
++)
23175 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23179 /* Substitute parameter packs into each argument in the
23181 in_base_initializer
= 1;
23182 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23184 tree expanded_exprs
;
23186 /* Expand the argument. */
23187 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23189 = tsubst_pack_expansion (expr
, argvec
,
23190 tf_warning_or_error
,
23192 if (expanded_exprs
== error_mark_node
)
23195 /* Prepend each of the expanded expressions to the
23196 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23197 for (i
= 0; i
< len
; i
++)
23199 TREE_VEC_ELT (expanded_arguments
, i
) =
23200 tree_cons (NULL_TREE
,
23201 TREE_VEC_ELT (expanded_exprs
, i
),
23202 TREE_VEC_ELT (expanded_arguments
, i
));
23205 in_base_initializer
= 0;
23207 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23208 since we built them backwards. */
23209 for (i
= 0; i
< len
; i
++)
23211 TREE_VEC_ELT (expanded_arguments
, i
) =
23212 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23217 for (i
= 0; i
< len
; ++i
)
23219 if (expanded_bases
)
23221 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23222 decl
= expand_member_init (decl
);
23223 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23228 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23229 tf_warning_or_error
, NULL_TREE
);
23231 decl
= expand_member_init (decl
);
23232 if (decl
&& !DECL_P (decl
))
23233 in_base_initializer
= 1;
23235 init
= TREE_VALUE (t
);
23237 if (init
!= void_type_node
)
23238 init
= tsubst_expr (init
, argvec
,
23239 tf_warning_or_error
, NULL_TREE
,
23240 /*integral_constant_expression_p=*/false);
23241 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23242 /* If we had an initializer but it instantiated to nothing,
23243 value-initialize the object. This will only occur when
23244 the initializer was a pack expansion where the parameter
23245 packs used in that expansion were of length zero. */
23246 init
= void_type_node
;
23247 in_base_initializer
= 0;
23252 init
= build_tree_list (decl
, init
);
23253 TREE_CHAIN (init
) = inits
;
23261 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23264 set_current_access_from_decl (tree decl
)
23266 if (TREE_PRIVATE (decl
))
23267 current_access_specifier
= access_private_node
;
23268 else if (TREE_PROTECTED (decl
))
23269 current_access_specifier
= access_protected_node
;
23271 current_access_specifier
= access_public_node
;
23274 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23275 is the instantiation (which should have been created with
23276 start_enum) and ARGS are the template arguments to use. */
23279 tsubst_enum (tree tag
, tree newtag
, tree args
)
23283 if (SCOPED_ENUM_P (newtag
))
23284 begin_scope (sk_scoped_enum
, newtag
);
23286 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23291 decl
= TREE_VALUE (e
);
23292 /* Note that in a template enum, the TREE_VALUE is the
23293 CONST_DECL, not the corresponding INTEGER_CST. */
23294 value
= tsubst_expr (DECL_INITIAL (decl
),
23295 args
, tf_warning_or_error
, NULL_TREE
,
23296 /*integral_constant_expression_p=*/true);
23298 /* Give this enumeration constant the correct access. */
23299 set_current_access_from_decl (decl
);
23301 /* Actually build the enumerator itself. Here we're assuming that
23302 enumerators can't have dependent attributes. */
23303 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23304 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23307 if (SCOPED_ENUM_P (newtag
))
23310 finish_enum_value_list (newtag
);
23311 finish_enum (newtag
);
23313 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23314 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23317 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23318 its type -- but without substituting the innermost set of template
23319 arguments. So, innermost set of template parameters will appear in
23323 get_mostly_instantiated_function_type (tree decl
)
23325 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23326 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23329 /* Return truthvalue if we're processing a template different from
23330 the last one involved in diagnostics. */
23332 problematic_instantiation_changed (void)
23334 return current_tinst_level
!= last_error_tinst_level
;
23337 /* Remember current template involved in diagnostics. */
23339 record_last_problematic_instantiation (void)
23341 last_error_tinst_level
= current_tinst_level
;
23344 struct tinst_level
*
23345 current_instantiation (void)
23347 return current_tinst_level
;
23350 /* Return TRUE if current_function_decl is being instantiated, false
23354 instantiating_current_function_p (void)
23356 return (current_instantiation ()
23357 && current_instantiation ()->decl
== current_function_decl
);
23360 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23361 type. Return zero for ok, nonzero for disallowed. Issue error and
23362 warning messages under control of COMPLAIN. */
23365 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23367 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23369 else if (POINTER_TYPE_P (type
))
23371 else if (TYPE_PTRMEM_P (type
))
23373 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23375 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23377 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23379 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23381 /* A bound template template parm could later be instantiated to have a valid
23382 nontype parm type via an alias template. */
23383 else if (cxx_dialect
>= cxx11
23384 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23387 if (complain
& tf_error
)
23389 if (type
== error_mark_node
)
23390 inform (input_location
, "invalid template non-type parameter");
23392 error ("%q#T is not a valid type for a template non-type parameter",
23398 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23399 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23402 dependent_type_p_r (tree type
)
23408 A type is dependent if it is:
23410 -- a template parameter. Template template parameters are types
23411 for us (since TYPE_P holds true for them) so we handle
23413 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23414 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23416 /* -- a qualified-id with a nested-name-specifier which contains a
23417 class-name that names a dependent type or whose unqualified-id
23418 names a dependent type. */
23419 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23422 /* An alias template specialization can be dependent even if the
23423 resulting type is not. */
23424 if (dependent_alias_template_spec_p (type
))
23427 /* -- a cv-qualified type where the cv-unqualified type is
23429 No code is necessary for this bullet; the code below handles
23430 cv-qualified types, and we don't want to strip aliases with
23431 TYPE_MAIN_VARIANT because of DR 1558. */
23432 /* -- a compound type constructed from any dependent type. */
23433 if (TYPE_PTRMEM_P (type
))
23434 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23435 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23437 else if (TYPE_PTR_P (type
)
23438 || TREE_CODE (type
) == REFERENCE_TYPE
)
23439 return dependent_type_p (TREE_TYPE (type
));
23440 else if (TREE_CODE (type
) == FUNCTION_TYPE
23441 || TREE_CODE (type
) == METHOD_TYPE
)
23445 if (dependent_type_p (TREE_TYPE (type
)))
23447 for (arg_type
= TYPE_ARG_TYPES (type
);
23449 arg_type
= TREE_CHAIN (arg_type
))
23450 if (dependent_type_p (TREE_VALUE (arg_type
)))
23452 if (cxx_dialect
>= cxx1z
)
23454 /* A value-dependent noexcept-specifier makes the type dependent. */
23455 tree spec
= TYPE_RAISES_EXCEPTIONS (type
);
23456 if (spec
&& TREE_PURPOSE (spec
)
23457 && value_dependent_expression_p (TREE_PURPOSE (spec
)))
23462 /* -- an array type constructed from any dependent type or whose
23463 size is specified by a constant expression that is
23466 We checked for type- and value-dependence of the bounds in
23467 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23468 if (TREE_CODE (type
) == ARRAY_TYPE
)
23470 if (TYPE_DOMAIN (type
)
23471 && dependent_type_p (TYPE_DOMAIN (type
)))
23473 return dependent_type_p (TREE_TYPE (type
));
23476 /* -- a template-id in which either the template name is a template
23478 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23480 /* ... or any of the template arguments is a dependent type or
23481 an expression that is type-dependent or value-dependent. */
23482 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23483 && (any_dependent_template_arguments_p
23484 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23487 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23488 dependent; if the argument of the `typeof' expression is not
23489 type-dependent, then it should already been have resolved. */
23490 if (TREE_CODE (type
) == TYPEOF_TYPE
23491 || TREE_CODE (type
) == DECLTYPE_TYPE
23492 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23495 /* A template argument pack is dependent if any of its packed
23497 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23499 tree args
= ARGUMENT_PACK_ARGS (type
);
23500 int i
, len
= TREE_VEC_LENGTH (args
);
23501 for (i
= 0; i
< len
; ++i
)
23502 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23506 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23507 be template parameters. */
23508 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23511 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23514 /* The standard does not specifically mention types that are local
23515 to template functions or local classes, but they should be
23516 considered dependent too. For example:
23518 template <int I> void f() {
23523 The size of `E' cannot be known until the value of `I' has been
23524 determined. Therefore, `E' must be considered dependent. */
23525 scope
= TYPE_CONTEXT (type
);
23526 if (scope
&& TYPE_P (scope
))
23527 return dependent_type_p (scope
);
23528 /* Don't use type_dependent_expression_p here, as it can lead
23529 to infinite recursion trying to determine whether a lambda
23530 nested in a lambda is dependent (c++/47687). */
23531 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23532 && DECL_LANG_SPECIFIC (scope
)
23533 && DECL_TEMPLATE_INFO (scope
)
23534 && (any_dependent_template_arguments_p
23535 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23538 /* Other types are non-dependent. */
23542 /* Returns TRUE if TYPE is dependent, in the sense of
23543 [temp.dep.type]. Note that a NULL type is considered dependent. */
23546 dependent_type_p (tree type
)
23548 /* If there are no template parameters in scope, then there can't be
23549 any dependent types. */
23550 if (!processing_template_decl
)
23552 /* If we are not processing a template, then nobody should be
23553 providing us with a dependent type. */
23555 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23559 /* If the type is NULL, we have not computed a type for the entity
23560 in question; in that case, the type is dependent. */
23564 /* Erroneous types can be considered non-dependent. */
23565 if (type
== error_mark_node
)
23568 /* Getting here with global_type_node means we improperly called this
23569 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23570 gcc_checking_assert (type
!= global_type_node
);
23572 /* If we have not already computed the appropriate value for TYPE,
23574 if (!TYPE_DEPENDENT_P_VALID (type
))
23576 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23577 TYPE_DEPENDENT_P_VALID (type
) = 1;
23580 return TYPE_DEPENDENT_P (type
);
23583 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23584 lookup. In other words, a dependent type that is not the current
23588 dependent_scope_p (tree scope
)
23590 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23591 && !currently_open_class (scope
));
23594 /* T is a SCOPE_REF; return whether we need to consider it
23595 instantiation-dependent so that we can check access at instantiation
23596 time even though we know which member it resolves to. */
23599 instantiation_dependent_scope_ref_p (tree t
)
23601 if (DECL_P (TREE_OPERAND (t
, 1))
23602 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23603 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23604 TREE_OPERAND (t
, 1)))
23610 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23611 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23614 /* Note that this predicate is not appropriate for general expressions;
23615 only constant expressions (that satisfy potential_constant_expression)
23616 can be tested for value dependence. */
23619 value_dependent_expression_p (tree expression
)
23621 if (!processing_template_decl
|| expression
== NULL_TREE
)
23624 /* A name declared with a dependent type. */
23625 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
23628 switch (TREE_CODE (expression
))
23631 /* A dependent member function of the current instantiation. */
23632 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23634 case FUNCTION_DECL
:
23635 /* A dependent member function of the current instantiation. */
23636 if (DECL_CLASS_SCOPE_P (expression
)
23637 && dependent_type_p (DECL_CONTEXT (expression
)))
23641 case IDENTIFIER_NODE
:
23642 /* A name that has not been looked up -- must be dependent. */
23645 case TEMPLATE_PARM_INDEX
:
23646 /* A non-type template parm. */
23650 /* A non-type template parm. */
23651 if (DECL_TEMPLATE_PARM_P (expression
))
23653 return value_dependent_expression_p (DECL_INITIAL (expression
));
23656 /* A constant with literal type and is initialized
23657 with an expression that is value-dependent.
23659 Note that a non-dependent parenthesized initializer will have
23660 already been replaced with its constant value, so if we see
23661 a TREE_LIST it must be dependent. */
23662 if (DECL_INITIAL (expression
)
23663 && decl_constant_var_p (expression
)
23664 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
23665 /* cp_finish_decl doesn't fold reference initializers. */
23666 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
23667 || type_dependent_expression_p (DECL_INITIAL (expression
))
23668 || value_dependent_expression_p (DECL_INITIAL (expression
))))
23670 if (DECL_HAS_VALUE_EXPR_P (expression
))
23672 tree value_expr
= DECL_VALUE_EXPR (expression
);
23673 if (type_dependent_expression_p (value_expr
))
23678 case DYNAMIC_CAST_EXPR
:
23679 case STATIC_CAST_EXPR
:
23680 case CONST_CAST_EXPR
:
23681 case REINTERPRET_CAST_EXPR
:
23683 /* These expressions are value-dependent if the type to which
23684 the cast occurs is dependent or the expression being casted
23685 is value-dependent. */
23687 tree type
= TREE_TYPE (expression
);
23689 if (dependent_type_p (type
))
23692 /* A functional cast has a list of operands. */
23693 expression
= TREE_OPERAND (expression
, 0);
23696 /* If there are no operands, it must be an expression such
23697 as "int()". This should not happen for aggregate types
23698 because it would form non-constant expressions. */
23699 gcc_assert (cxx_dialect
>= cxx11
23700 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
23705 if (TREE_CODE (expression
) == TREE_LIST
)
23706 return any_value_dependent_elements_p (expression
);
23708 return value_dependent_expression_p (expression
);
23712 if (SIZEOF_EXPR_TYPE_P (expression
))
23713 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
23717 /* A `sizeof' expression is value-dependent if the operand is
23718 type-dependent or is a pack expansion. */
23719 expression
= TREE_OPERAND (expression
, 0);
23720 if (PACK_EXPANSION_P (expression
))
23722 else if (TYPE_P (expression
))
23723 return dependent_type_p (expression
);
23724 return instantiation_dependent_uneval_expression_p (expression
);
23726 case AT_ENCODE_EXPR
:
23727 /* An 'encode' expression is value-dependent if the operand is
23729 expression
= TREE_OPERAND (expression
, 0);
23730 return dependent_type_p (expression
);
23732 case NOEXCEPT_EXPR
:
23733 expression
= TREE_OPERAND (expression
, 0);
23734 return instantiation_dependent_uneval_expression_p (expression
);
23737 /* All instantiation-dependent expressions should also be considered
23738 value-dependent. */
23739 return instantiation_dependent_scope_ref_p (expression
);
23741 case COMPONENT_REF
:
23742 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
23743 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
23745 case NONTYPE_ARGUMENT_PACK
:
23746 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23747 is value-dependent. */
23749 tree values
= ARGUMENT_PACK_ARGS (expression
);
23750 int i
, len
= TREE_VEC_LENGTH (values
);
23752 for (i
= 0; i
< len
; ++i
)
23753 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
23761 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
23762 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
23763 || (type2
? dependent_type_p (type2
) : false));
23767 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23768 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
23771 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23772 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
23776 tree op
= TREE_OPERAND (expression
, 0);
23777 return (value_dependent_expression_p (op
)
23778 || has_value_dependent_address (op
));
23781 case REQUIRES_EXPR
:
23782 /* Treat all requires-expressions as value-dependent so
23783 we don't try to fold them. */
23787 return dependent_type_p (TREE_OPERAND (expression
, 0));
23791 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
23793 tree fn
= get_callee_fndecl (expression
);
23795 nargs
= call_expr_nargs (expression
);
23796 for (i
= 0; i
< nargs
; ++i
)
23798 tree op
= CALL_EXPR_ARG (expression
, i
);
23799 /* In a call to a constexpr member function, look through the
23800 implicit ADDR_EXPR on the object argument so that it doesn't
23801 cause the call to be considered value-dependent. We also
23802 look through it in potential_constant_expression. */
23803 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
23804 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
23805 && TREE_CODE (op
) == ADDR_EXPR
)
23806 op
= TREE_OPERAND (op
, 0);
23807 if (value_dependent_expression_p (op
))
23813 case TEMPLATE_ID_EXPR
:
23814 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23816 return type_dependent_expression_p (expression
)
23817 || variable_concept_p (TREE_OPERAND (expression
, 0));
23823 if (dependent_type_p (TREE_TYPE (expression
)))
23825 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
23826 if (value_dependent_expression_p (val
))
23832 /* Treat a GNU statement expression as dependent to avoid crashing
23833 under instantiate_non_dependent_expr; it can't be constant. */
23837 /* A constant expression is value-dependent if any subexpression is
23838 value-dependent. */
23839 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
23841 case tcc_reference
:
23843 case tcc_comparison
:
23845 case tcc_expression
:
23848 int i
, len
= cp_tree_operand_length (expression
);
23850 for (i
= 0; i
< len
; i
++)
23852 tree t
= TREE_OPERAND (expression
, i
);
23854 /* In some cases, some of the operands may be missing.
23855 (For example, in the case of PREDECREMENT_EXPR, the
23856 amount to increment by may be missing.) That doesn't
23857 make the expression dependent. */
23858 if (t
&& value_dependent_expression_p (t
))
23869 /* The expression is not value-dependent. */
23873 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23874 [temp.dep.expr]. Note that an expression with no type is
23875 considered dependent. Other parts of the compiler arrange for an
23876 expression with type-dependent subexpressions to have no type, so
23877 this function doesn't have to be fully recursive. */
23880 type_dependent_expression_p (tree expression
)
23882 if (!processing_template_decl
)
23885 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
23888 /* An unresolved name is always dependent. */
23889 if (identifier_p (expression
)
23890 || TREE_CODE (expression
) == USING_DECL
23891 || TREE_CODE (expression
) == WILDCARD_DECL
)
23894 /* A fold expression is type-dependent. */
23895 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
23896 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
23897 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
23898 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
23901 /* Some expression forms are never type-dependent. */
23902 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
23903 || TREE_CODE (expression
) == SIZEOF_EXPR
23904 || TREE_CODE (expression
) == ALIGNOF_EXPR
23905 || TREE_CODE (expression
) == AT_ENCODE_EXPR
23906 || TREE_CODE (expression
) == NOEXCEPT_EXPR
23907 || TREE_CODE (expression
) == TRAIT_EXPR
23908 || TREE_CODE (expression
) == TYPEID_EXPR
23909 || TREE_CODE (expression
) == DELETE_EXPR
23910 || TREE_CODE (expression
) == VEC_DELETE_EXPR
23911 || TREE_CODE (expression
) == THROW_EXPR
23912 || TREE_CODE (expression
) == REQUIRES_EXPR
)
23915 /* The types of these expressions depends only on the type to which
23916 the cast occurs. */
23917 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
23918 || TREE_CODE (expression
) == STATIC_CAST_EXPR
23919 || TREE_CODE (expression
) == CONST_CAST_EXPR
23920 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
23921 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
23922 || TREE_CODE (expression
) == CAST_EXPR
)
23923 return dependent_type_p (TREE_TYPE (expression
));
23925 /* The types of these expressions depends only on the type created
23926 by the expression. */
23927 if (TREE_CODE (expression
) == NEW_EXPR
23928 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
23930 /* For NEW_EXPR tree nodes created inside a template, either
23931 the object type itself or a TREE_LIST may appear as the
23933 tree type
= TREE_OPERAND (expression
, 1);
23934 if (TREE_CODE (type
) == TREE_LIST
)
23935 /* This is an array type. We need to check array dimensions
23937 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
23938 || value_dependent_expression_p
23939 (TREE_OPERAND (TREE_VALUE (type
), 1));
23941 return dependent_type_p (type
);
23944 if (TREE_CODE (expression
) == SCOPE_REF
)
23946 tree scope
= TREE_OPERAND (expression
, 0);
23947 tree name
= TREE_OPERAND (expression
, 1);
23949 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23950 contains an identifier associated by name lookup with one or more
23951 declarations declared with a dependent type, or...a
23952 nested-name-specifier or qualified-id that names a member of an
23953 unknown specialization. */
23954 return (type_dependent_expression_p (name
)
23955 || dependent_scope_p (scope
));
23958 if (TREE_CODE (expression
) == TEMPLATE_DECL
23959 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
23960 return uses_outer_template_parms (expression
);
23962 if (TREE_CODE (expression
) == STMT_EXPR
)
23963 expression
= stmt_expr_value_expr (expression
);
23965 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
23970 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
23972 if (type_dependent_expression_p (elt
))
23978 /* A static data member of the current instantiation with incomplete
23979 array type is type-dependent, as the definition and specializations
23980 can have different bounds. */
23981 if (VAR_P (expression
)
23982 && DECL_CLASS_SCOPE_P (expression
)
23983 && dependent_type_p (DECL_CONTEXT (expression
))
23984 && VAR_HAD_UNKNOWN_BOUND (expression
))
23987 /* An array of unknown bound depending on a variadic parameter, eg:
23989 template<typename... Args>
23990 void foo (Args... args)
23992 int arr[] = { args... };
23995 template<int... vals>
23998 int arr[] = { vals... };
24001 If the array has no length and has an initializer, it must be that
24002 we couldn't determine its length in cp_complete_array_type because
24003 it is dependent. */
24004 if (VAR_P (expression
)
24005 && TREE_TYPE (expression
) != NULL_TREE
24006 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
24007 && !TYPE_DOMAIN (TREE_TYPE (expression
))
24008 && DECL_INITIAL (expression
))
24011 /* A function or variable template-id is type-dependent if it has any
24012 dependent template arguments. */
24013 if (VAR_OR_FUNCTION_DECL_P (expression
)
24014 && DECL_LANG_SPECIFIC (expression
)
24015 && DECL_TEMPLATE_INFO (expression
))
24017 /* Consider the innermost template arguments, since those are the ones
24018 that come from the template-id; the template arguments for the
24019 enclosing class do not make it type-dependent unless they are used in
24020 the type of the decl. */
24021 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
24022 && (any_dependent_template_arguments_p
24023 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
24026 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24027 type-dependent. Checking this is important for functions with auto
24028 return type, which looks like a dependent type. */
24029 if (TREE_CODE (expression
) == FUNCTION_DECL
24030 && (!DECL_CLASS_SCOPE_P (expression
)
24031 || !dependent_type_p (DECL_CONTEXT (expression
)))
24032 && (!DECL_FRIEND_CONTEXT (expression
)
24033 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression
)))
24034 && !DECL_LOCAL_FUNCTION_P (expression
))
24036 gcc_assert (!dependent_type_p (TREE_TYPE (expression
))
24037 || undeduced_auto_decl (expression
));
24042 /* Always dependent, on the number of arguments if nothing else. */
24043 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24046 if (TREE_TYPE (expression
) == unknown_type_node
)
24048 if (TREE_CODE (expression
) == ADDR_EXPR
)
24049 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24050 if (TREE_CODE (expression
) == COMPONENT_REF
24051 || TREE_CODE (expression
) == OFFSET_REF
)
24053 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24055 expression
= TREE_OPERAND (expression
, 1);
24056 if (identifier_p (expression
))
24059 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24060 if (TREE_CODE (expression
) == SCOPE_REF
)
24063 if (BASELINK_P (expression
))
24065 if (BASELINK_OPTYPE (expression
)
24066 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24068 expression
= BASELINK_FUNCTIONS (expression
);
24071 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24073 if (any_dependent_template_arguments_p
24074 (TREE_OPERAND (expression
, 1)))
24076 expression
= TREE_OPERAND (expression
, 0);
24077 if (identifier_p (expression
))
24081 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24082 || TREE_CODE (expression
) == FUNCTION_DECL
);
24084 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24085 if (type_dependent_expression_p (*iter
))
24091 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24093 /* Dependent type attributes might not have made it from the decl to
24095 if (DECL_P (expression
)
24096 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24099 return (dependent_type_p (TREE_TYPE (expression
)));
24102 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24103 type-dependent if the expression refers to a member of the current
24104 instantiation and the type of the referenced member is dependent, or the
24105 class member access expression refers to a member of an unknown
24108 This function returns true if the OBJECT in such a class member access
24109 expression is of an unknown specialization. */
24112 type_dependent_object_expression_p (tree object
)
24114 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24116 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24118 tree scope
= TREE_TYPE (object
);
24119 return (!scope
|| dependent_scope_p (scope
));
24122 /* walk_tree callback function for instantiation_dependent_expression_p,
24123 below. Returns non-zero if a dependent subexpression is found. */
24126 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24131 /* We don't have to worry about decltype currently because decltype
24132 of an instantiation-dependent expr is a dependent type. This
24133 might change depending on the resolution of DR 1172. */
24134 *walk_subtrees
= false;
24137 enum tree_code code
= TREE_CODE (*tp
);
24140 /* Don't treat an argument list as dependent just because it has no
24146 case TEMPLATE_PARM_INDEX
:
24149 /* Handle expressions with type operands. */
24153 case AT_ENCODE_EXPR
:
24155 tree op
= TREE_OPERAND (*tp
, 0);
24156 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24157 op
= TREE_TYPE (op
);
24160 if (dependent_type_p (op
))
24164 *walk_subtrees
= false;
24171 case COMPONENT_REF
:
24172 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24173 /* In a template, finish_class_member_access_expr creates a
24174 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24175 type-dependent, so that we can check access control at
24176 instantiation time (PR 42277). See also Core issue 1273. */
24181 if (instantiation_dependent_scope_ref_p (*tp
))
24186 /* Treat statement-expressions as dependent. */
24190 /* Treat requires-expressions as dependent. */
24191 case REQUIRES_EXPR
:
24195 /* Treat calls to function concepts as dependent. */
24196 if (function_concept_check_p (*tp
))
24200 case TEMPLATE_ID_EXPR
:
24201 /* And variable concepts. */
24202 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24210 if (type_dependent_expression_p (*tp
))
24216 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24217 sense defined by the ABI:
24219 "An expression is instantiation-dependent if it is type-dependent
24220 or value-dependent, or it has a subexpression that is type-dependent
24221 or value-dependent."
24223 Except don't actually check value-dependence for unevaluated expressions,
24224 because in sizeof(i) we don't care about the value of i. Checking
24225 type-dependence will in turn check value-dependence of array bounds/template
24226 arguments as needed. */
24229 instantiation_dependent_uneval_expression_p (tree expression
)
24233 if (!processing_template_decl
)
24236 if (expression
== error_mark_node
)
24239 result
= cp_walk_tree_without_duplicates (&expression
,
24240 instantiation_dependent_r
, NULL
);
24241 return result
!= NULL_TREE
;
24244 /* As above, but also check value-dependence of the expression as a whole. */
24247 instantiation_dependent_expression_p (tree expression
)
24249 return (instantiation_dependent_uneval_expression_p (expression
)
24250 || value_dependent_expression_p (expression
));
24253 /* Like type_dependent_expression_p, but it also works while not processing
24254 a template definition, i.e. during substitution or mangling. */
24257 type_dependent_expression_p_push (tree expr
)
24260 ++processing_template_decl
;
24261 b
= type_dependent_expression_p (expr
);
24262 --processing_template_decl
;
24266 /* Returns TRUE if ARGS contains a type-dependent expression. */
24269 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24274 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24276 if (type_dependent_expression_p (arg
))
24282 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24283 expressions) contains any type-dependent expressions. */
24286 any_type_dependent_elements_p (const_tree list
)
24288 for (; list
; list
= TREE_CHAIN (list
))
24289 if (type_dependent_expression_p (TREE_VALUE (list
)))
24295 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24296 expressions) contains any value-dependent expressions. */
24299 any_value_dependent_elements_p (const_tree list
)
24301 for (; list
; list
= TREE_CHAIN (list
))
24302 if (value_dependent_expression_p (TREE_VALUE (list
)))
24308 /* Returns TRUE if the ARG (a template argument) is dependent. */
24311 dependent_template_arg_p (tree arg
)
24313 if (!processing_template_decl
)
24316 /* Assume a template argument that was wrongly written by the user
24317 is dependent. This is consistent with what
24318 any_dependent_template_arguments_p [that calls this function]
24320 if (!arg
|| arg
== error_mark_node
)
24323 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24324 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24326 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24328 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24330 if (DECL_TEMPLATE_PARM_P (arg
))
24332 /* A member template of a dependent class is not necessarily
24333 type-dependent, but it is a dependent template argument because it
24334 will be a member of an unknown specialization to that template. */
24335 tree scope
= CP_DECL_CONTEXT (arg
);
24336 return TYPE_P (scope
) && dependent_type_p (scope
);
24338 else if (ARGUMENT_PACK_P (arg
))
24340 tree args
= ARGUMENT_PACK_ARGS (arg
);
24341 int i
, len
= TREE_VEC_LENGTH (args
);
24342 for (i
= 0; i
< len
; ++i
)
24344 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24350 else if (TYPE_P (arg
))
24351 return dependent_type_p (arg
);
24353 return (type_dependent_expression_p (arg
)
24354 || value_dependent_expression_p (arg
));
24357 /* Returns true if ARGS (a collection of template arguments) contains
24358 any types that require structural equality testing. */
24361 any_template_arguments_need_structural_equality_p (tree args
)
24368 if (args
== error_mark_node
)
24371 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24373 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24374 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24376 tree arg
= TREE_VEC_ELT (level
, j
);
24377 tree packed_args
= NULL_TREE
;
24380 if (ARGUMENT_PACK_P (arg
))
24382 /* Look inside the argument pack. */
24383 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24384 len
= TREE_VEC_LENGTH (packed_args
);
24387 for (k
= 0; k
< len
; ++k
)
24390 arg
= TREE_VEC_ELT (packed_args
, k
);
24392 if (error_operand_p (arg
))
24394 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24396 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24398 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24399 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24408 /* Returns true if ARGS (a collection of template arguments) contains
24409 any dependent arguments. */
24412 any_dependent_template_arguments_p (const_tree args
)
24419 if (args
== error_mark_node
)
24422 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24424 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24425 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24426 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24433 /* Returns TRUE if the template TMPL is type-dependent. */
24436 dependent_template_p (tree tmpl
)
24438 if (TREE_CODE (tmpl
) == OVERLOAD
)
24440 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24441 if (dependent_template_p (*iter
))
24446 /* Template template parameters are dependent. */
24447 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24448 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24450 /* So are names that have not been looked up. */
24451 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24456 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24459 dependent_template_id_p (tree tmpl
, tree args
)
24461 return (dependent_template_p (tmpl
)
24462 || any_dependent_template_arguments_p (args
));
24465 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24469 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24473 if (!processing_template_decl
)
24476 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24478 tree decl
= TREE_VEC_ELT (declv
, i
);
24479 tree init
= TREE_VEC_ELT (initv
, i
);
24480 tree cond
= TREE_VEC_ELT (condv
, i
);
24481 tree incr
= TREE_VEC_ELT (incrv
, i
);
24483 if (type_dependent_expression_p (decl
)
24484 || TREE_CODE (decl
) == SCOPE_REF
)
24487 if (init
&& type_dependent_expression_p (init
))
24490 if (type_dependent_expression_p (cond
))
24493 if (COMPARISON_CLASS_P (cond
)
24494 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24495 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24498 if (TREE_CODE (incr
) == MODOP_EXPR
)
24500 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24501 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24504 else if (type_dependent_expression_p (incr
))
24506 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24508 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24510 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24512 tree t
= TREE_OPERAND (incr
, 1);
24513 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24514 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24523 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24524 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24525 no such TYPE can be found. Note that this function peers inside
24526 uninstantiated templates and therefore should be used only in
24527 extremely limited situations. ONLY_CURRENT_P restricts this
24528 peering to the currently open classes hierarchy (which is required
24529 when comparing types). */
24532 resolve_typename_type (tree type
, bool only_current_p
)
24541 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24543 scope
= TYPE_CONTEXT (type
);
24544 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24545 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24546 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24547 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24548 identifier of the TYPENAME_TYPE anymore.
24549 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24550 TYPENAME_TYPE instead, we avoid messing up with a possible
24551 typedef variant case. */
24552 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24554 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24555 it first before we can figure out what NAME refers to. */
24556 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24558 if (TYPENAME_IS_RESOLVING_P (scope
))
24559 /* Given a class template A with a dependent base with nested type C,
24560 typedef typename A::C::C C will land us here, as trying to resolve
24561 the initial A::C leads to the local C typedef, which leads back to
24562 A::C::C. So we break the recursion now. */
24565 scope
= resolve_typename_type (scope
, only_current_p
);
24567 /* If we don't know what SCOPE refers to, then we cannot resolve the
24569 if (!CLASS_TYPE_P (scope
))
24571 /* If this is a typedef, we don't want to look inside (c++/11987). */
24572 if (typedef_variant_p (type
))
24574 /* If SCOPE isn't the template itself, it will not have a valid
24575 TYPE_FIELDS list. */
24576 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24577 /* scope is either the template itself or a compatible instantiation
24578 like X<T>, so look up the name in the original template. */
24579 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24580 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24581 gcc_checking_assert (uses_template_parms (scope
));
24582 /* If scope has no fields, it can't be a current instantiation. Check this
24583 before currently_open_class to avoid infinite recursion (71515). */
24584 if (!TYPE_FIELDS (scope
))
24586 /* If the SCOPE is not the current instantiation, there's no reason
24587 to look inside it. */
24588 if (only_current_p
&& !currently_open_class (scope
))
24590 /* Enter the SCOPE so that name lookup will be resolved as if we
24591 were in the class definition. In particular, SCOPE will no
24592 longer be considered a dependent type. */
24593 pushed_scope
= push_scope (scope
);
24594 /* Look up the declaration. */
24595 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24596 tf_warning_or_error
);
24598 result
= NULL_TREE
;
24600 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24601 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24604 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
24605 && TREE_CODE (decl
) == TYPE_DECL
)
24607 result
= TREE_TYPE (decl
);
24608 if (result
== error_mark_node
)
24609 result
= NULL_TREE
;
24611 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
24612 && DECL_CLASS_TEMPLATE_P (decl
))
24616 /* Obtain the template and the arguments. */
24617 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
24618 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
24619 /* Instantiate the template. */
24620 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24621 /*entering_scope=*/0,
24622 tf_error
| tf_user
);
24623 if (result
== error_mark_node
)
24624 result
= NULL_TREE
;
24627 /* Leave the SCOPE. */
24629 pop_scope (pushed_scope
);
24631 /* If we failed to resolve it, return the original typename. */
24635 /* If lookup found a typename type, resolve that too. */
24636 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
24638 /* Ill-formed programs can cause infinite recursion here, so we
24639 must catch that. */
24640 TYPENAME_IS_RESOLVING_P (result
) = 1;
24641 result
= resolve_typename_type (result
, only_current_p
);
24642 TYPENAME_IS_RESOLVING_P (result
) = 0;
24645 /* Qualify the resulting type. */
24646 quals
= cp_type_quals (type
);
24648 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
24653 /* EXPR is an expression which is not type-dependent. Return a proxy
24654 for EXPR that can be used to compute the types of larger
24655 expressions containing EXPR. */
24658 build_non_dependent_expr (tree expr
)
24662 /* When checking, try to get a constant value for all non-dependent
24663 expressions in order to expose bugs in *_dependent_expression_p
24664 and constexpr. This can affect code generation, see PR70704, so
24665 only do this for -fchecking=2. */
24666 if (flag_checking
> 1
24667 && cxx_dialect
>= cxx11
24668 /* Don't do this during nsdmi parsing as it can lead to
24669 unexpected recursive instantiations. */
24670 && !parsing_nsdmi ()
24671 /* Don't do this during concept expansion either and for
24672 the same reason. */
24673 && !expanding_concept ())
24674 fold_non_dependent_expr (expr
);
24676 /* Preserve OVERLOADs; the functions must be available to resolve
24679 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
24680 inner_expr
= stmt_expr_value_expr (inner_expr
);
24681 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
24682 inner_expr
= TREE_OPERAND (inner_expr
, 0);
24683 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
24684 inner_expr
= TREE_OPERAND (inner_expr
, 1);
24685 if (is_overloaded_fn (inner_expr
)
24686 || TREE_CODE (inner_expr
) == OFFSET_REF
)
24688 /* There is no need to return a proxy for a variable. */
24691 /* Preserve string constants; conversions from string constants to
24692 "char *" are allowed, even though normally a "const char *"
24693 cannot be used to initialize a "char *". */
24694 if (TREE_CODE (expr
) == STRING_CST
)
24696 /* Preserve void and arithmetic constants, as an optimization -- there is no
24697 reason to create a new node. */
24698 if (TREE_CODE (expr
) == VOID_CST
24699 || TREE_CODE (expr
) == INTEGER_CST
24700 || TREE_CODE (expr
) == REAL_CST
)
24702 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24703 There is at least one place where we want to know that a
24704 particular expression is a throw-expression: when checking a ?:
24705 expression, there are special rules if the second or third
24706 argument is a throw-expression. */
24707 if (TREE_CODE (expr
) == THROW_EXPR
)
24710 /* Don't wrap an initializer list, we need to be able to look inside. */
24711 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
24714 /* Don't wrap a dummy object, we need to be able to test for it. */
24715 if (is_dummy_object (expr
))
24718 if (TREE_CODE (expr
) == COND_EXPR
)
24719 return build3 (COND_EXPR
,
24721 TREE_OPERAND (expr
, 0),
24722 (TREE_OPERAND (expr
, 1)
24723 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
24724 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
24725 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
24726 if (TREE_CODE (expr
) == COMPOUND_EXPR
24727 && !COMPOUND_EXPR_OVERLOADED (expr
))
24728 return build2 (COMPOUND_EXPR
,
24730 TREE_OPERAND (expr
, 0),
24731 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
24733 /* If the type is unknown, it can't really be non-dependent */
24734 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
24736 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24737 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
24740 /* ARGS is a vector of expressions as arguments to a function call.
24741 Replace the arguments with equivalent non-dependent expressions.
24742 This modifies ARGS in place. */
24745 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
24750 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
24752 tree newarg
= build_non_dependent_expr (arg
);
24754 (*args
)[ix
] = newarg
;
24758 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24759 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24760 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24763 make_auto_1 (tree name
, bool set_canonical
)
24765 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
24766 TYPE_NAME (au
) = build_decl (input_location
,
24767 TYPE_DECL
, name
, au
);
24768 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
24769 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
24770 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
24771 TYPE_NAME (au
), NULL_TREE
);
24773 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
24774 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
24775 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
24781 make_decltype_auto (void)
24783 return make_auto_1 (decltype_auto_identifier
, true);
24789 return make_auto_1 (auto_identifier
, true);
24792 /* Return a C++17 deduction placeholder for class template TMPL. */
24795 make_template_placeholder (tree tmpl
)
24797 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
24798 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
24802 /* Make a "constrained auto" type-specifier. This is an
24803 auto type with constraints that must be associated after
24804 deduction. The constraint is formed from the given
24805 CONC and its optional sequence of arguments, which are
24806 non-null if written as partial-concept-id. */
24809 make_constrained_auto (tree con
, tree args
)
24811 tree type
= make_auto_1 (auto_identifier
, false);
24813 /* Build the constraint. */
24814 tree tmpl
= DECL_TI_TEMPLATE (con
);
24815 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
24816 expr
= build_concept_check (expr
, type
, args
);
24818 tree constr
= normalize_expression (expr
);
24819 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
24821 /* Our canonical type depends on the constraint. */
24822 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
24824 /* Attach the constraint to the type declaration. */
24825 tree decl
= TYPE_NAME (type
);
24829 /* Given type ARG, return std::initializer_list<ARG>. */
24834 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
24836 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
24838 error ("deducing from brace-enclosed initializer list requires "
24839 "#include <initializer_list>");
24840 return error_mark_node
;
24842 tree argvec
= make_tree_vec (1);
24843 TREE_VEC_ELT (argvec
, 0) = arg
;
24845 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
24846 NULL_TREE
, 0, tf_warning_or_error
);
24849 /* Replace auto in TYPE with std::initializer_list<auto>. */
24852 listify_autos (tree type
, tree auto_node
)
24854 tree init_auto
= listify (auto_node
);
24855 tree argvec
= make_tree_vec (1);
24856 TREE_VEC_ELT (argvec
, 0) = init_auto
;
24857 if (processing_template_decl
)
24858 argvec
= add_to_template_args (current_template_args (), argvec
);
24859 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
24862 /* Hash traits for hashing possibly constrained 'auto'
24863 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24865 struct auto_hash
: default_hash_traits
<tree
>
24867 static inline hashval_t
hash (tree
);
24868 static inline bool equal (tree
, tree
);
24871 /* Hash the 'auto' T. */
24874 auto_hash::hash (tree t
)
24876 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
24877 /* Matching constrained-type-specifiers denote the same template
24878 parameter, so hash the constraint. */
24879 return hash_placeholder_constraint (c
);
24881 /* But unconstrained autos are all separate, so just hash the pointer. */
24882 return iterative_hash_object (t
, 0);
24885 /* Compare two 'auto's. */
24888 auto_hash::equal (tree t1
, tree t2
)
24893 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
24894 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
24896 /* Two unconstrained autos are distinct. */
24900 return equivalent_placeholder_constraints (c1
, c2
);
24903 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24904 constrained) auto, add it to the vector. */
24907 extract_autos_r (tree t
, void *data
)
24909 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
24912 /* All the autos were built with index 0; fix that up now. */
24913 tree
*p
= hash
.find_slot (t
, INSERT
);
24916 /* If this is a repeated constrained-type-specifier, use the index we
24918 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
24921 /* Otherwise this is new, so use the current count. */
24923 idx
= hash
.elements () - 1;
24925 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
24928 /* Always keep walking. */
24932 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24933 says they can appear anywhere in the type. */
24936 extract_autos (tree type
)
24938 hash_set
<tree
> visited
;
24939 hash_table
<auto_hash
> hash (2);
24941 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
24943 tree tree_vec
= make_tree_vec (hash
.elements());
24944 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
24945 iter
!= hash
.end(); ++iter
)
24948 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
24949 TREE_VEC_ELT (tree_vec
, i
)
24950 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
24956 /* The stem for deduction guide names. */
24957 const char *const dguide_base
= "__dguide_";
24959 /* Return the name for a deduction guide for class template TMPL. */
24962 dguide_name (tree tmpl
)
24964 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
24965 tree tname
= TYPE_IDENTIFIER (type
);
24966 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
24967 + IDENTIFIER_LENGTH (tname
));
24968 memcpy (buf
, dguide_base
, strlen (dguide_base
));
24969 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
24970 IDENTIFIER_LENGTH (tname
) + 1);
24971 tree dname
= get_identifier (buf
);
24972 TREE_TYPE (dname
) = type
;
24976 /* True if NAME is the name of a deduction guide. */
24979 dguide_name_p (tree name
)
24981 return (TREE_TYPE (name
)
24982 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
24983 strlen (dguide_base
)));
24986 /* True if FN is a deduction guide. */
24989 deduction_guide_p (const_tree fn
)
24992 if (tree name
= DECL_NAME (fn
))
24993 return dguide_name_p (name
);
24997 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25000 copy_guide_p (const_tree fn
)
25002 gcc_assert (deduction_guide_p (fn
));
25003 if (!DECL_ARTIFICIAL (fn
))
25005 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
25006 return (TREE_CHAIN (parms
) == void_list_node
25007 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
25010 /* True if FN is a guide generated from a constructor template. */
25013 template_guide_p (const_tree fn
)
25015 gcc_assert (deduction_guide_p (fn
));
25016 if (!DECL_ARTIFICIAL (fn
))
25018 if (tree ctor
= DECL_ABSTRACT_ORIGIN (fn
))
25020 tree tmpl
= DECL_TI_TEMPLATE (ctor
);
25021 return PRIMARY_TEMPLATE_P (tmpl
);
25026 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25027 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25028 template parameter types. Note that the handling of template template
25029 parameters relies on current_template_parms being set appropriately for the
25033 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
25034 tree tsubst_args
, tsubst_flags_t complain
)
25036 tree oldidx
= get_template_parm_index (olddecl
);
25039 if (TREE_CODE (olddecl
) == TYPE_DECL
25040 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25042 tree oldtype
= TREE_TYPE (olddecl
);
25043 newtype
= cxx_make_type (TREE_CODE (oldtype
));
25044 TYPE_MAIN_VARIANT (newtype
) = newtype
;
25045 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25046 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25047 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25050 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25051 complain
, NULL_TREE
);
25054 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25055 DECL_NAME (olddecl
), newtype
);
25056 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25059 if (TREE_CODE (olddecl
) == TYPE_DECL
25060 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25062 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25063 = build_template_parm_index (index
, level
, level
,
25065 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25066 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25067 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25068 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25070 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25072 DECL_TEMPLATE_RESULT (newdecl
)
25073 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25074 DECL_NAME (olddecl
), newtype
);
25075 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25076 // First create a copy (ttargs) of tsubst_args with an
25077 // additional level for the template template parameter's own
25078 // template parameters (ttparms).
25079 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25080 (DECL_TEMPLATE_PARMS (olddecl
)));
25081 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25082 tree ttargs
= make_tree_vec (depth
+ 1);
25083 for (int i
= 0; i
< depth
; ++i
)
25084 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25085 TREE_VEC_ELT (ttargs
, depth
)
25086 = template_parms_level_to_args (ttparms
);
25087 // Substitute ttargs into ttparms to fix references to
25088 // other template parameters.
25089 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25091 // Now substitute again with args based on tparms, to reduce
25092 // the level of the ttparms.
25093 ttargs
= current_template_args ();
25094 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25096 // Finally, tack the adjusted parms onto tparms.
25097 ttparms
= tree_cons (size_int (depth
), ttparms
,
25098 current_template_parms
);
25099 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25104 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25106 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25107 TREE_CODE (oldconst
),
25108 DECL_NAME (oldconst
), newtype
);
25109 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25110 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25111 SET_DECL_TEMPLATE_PARM_P (newconst
);
25112 newidx
= build_template_parm_index (index
, level
, level
,
25113 newconst
, newtype
);
25114 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25115 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25116 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25122 /* Returns a C++17 class deduction guide template based on the constructor
25123 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25124 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25127 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25129 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25130 bool memtmpl
= false;
25137 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25140 type
= TREE_TYPE (type
);
25141 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25144 fparms
= void_list_node
;
25146 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25147 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25148 targs
= CLASSTYPE_TI_ARGS (type
);
25151 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25152 explicit_p
= false;
25157 ctor
= tsubst (ctor
, outer_args
, complain
, ctor
);
25158 type
= DECL_CONTEXT (ctor
);
25160 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
25163 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25166 fn_tmpl
= DECL_TI_TEMPLATE (ctor
);
25168 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25169 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25170 fully specialized args for the enclosing class. Strip those off, as
25171 the deduction guide won't have those template parameters. */
25172 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25173 TMPL_PARMS_DEPTH (tparms
));
25174 /* Discard the 'this' parameter. */
25175 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25176 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25177 ci
= get_constraints (ctor
);
25178 loc
= DECL_SOURCE_LOCATION (ctor
);
25179 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25181 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25185 /* For a member template constructor, we need to flatten the two
25186 template parameter lists into one, and then adjust the function
25187 signature accordingly. This gets...complicated. */
25188 ++processing_template_decl
;
25189 tree save_parms
= current_template_parms
;
25191 /* For a member template we should have two levels of parms/args, one
25192 for the class and one for the constructor. We stripped
25193 specialized args for further enclosing classes above. */
25194 const int depth
= 2;
25195 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25197 /* Template args for translating references to the two-level template
25198 parameters into references to the one-level template parameters we
25200 tree tsubst_args
= copy_node (targs
);
25201 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25202 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25204 /* Template parms for the constructor template. */
25205 tree ftparms
= TREE_VALUE (tparms
);
25206 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25207 /* Template parms for the class template. */
25208 tparms
= TREE_CHAIN (tparms
);
25209 tree ctparms
= TREE_VALUE (tparms
);
25210 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25211 /* Template parms for the deduction guide start as a copy of the
25212 template parms for the class. We set current_template_parms for
25213 lookup_template_class_1. */
25214 current_template_parms
= tparms
= copy_node (tparms
);
25215 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25216 for (unsigned i
= 0; i
< clen
; ++i
)
25217 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25219 /* Now we need to rewrite the constructor parms to append them to the
25221 for (unsigned i
= 0; i
< flen
; ++i
)
25223 unsigned index
= i
+ clen
;
25224 unsigned level
= 1;
25225 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25226 tree olddecl
= TREE_VALUE (oldelt
);
25227 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25228 tsubst_args
, complain
);
25229 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25230 tsubst_args
, complain
, ctor
);
25231 tree list
= build_tree_list (newdef
, newdecl
);
25232 TEMPLATE_PARM_CONSTRAINTS (list
)
25233 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25234 tsubst_args
, complain
, ctor
);
25235 TREE_VEC_ELT (new_vec
, index
) = list
;
25236 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25239 /* Now we have a final set of template parms to substitute into the
25240 function signature. */
25241 targs
= template_parms_to_args (tparms
);
25242 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25244 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25246 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25248 current_template_parms
= save_parms
;
25249 --processing_template_decl
;
25255 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25256 tparms
= copy_node (tparms
);
25257 INNERMOST_TEMPLATE_PARMS (tparms
)
25258 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25261 tree fntype
= build_function_type (type
, fparms
);
25262 tree ded_fn
= build_lang_decl_loc (loc
,
25264 dguide_name (type
), fntype
);
25265 DECL_ARGUMENTS (ded_fn
) = fargs
;
25266 DECL_ARTIFICIAL (ded_fn
) = true;
25267 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25268 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25269 DECL_ARTIFICIAL (ded_tmpl
) = true;
25270 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25271 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25272 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25273 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25275 DECL_ABSTRACT_ORIGIN (ded_fn
) = ctor
;
25277 set_constraints (ded_tmpl
, ci
);
25282 /* Deduce template arguments for the class template placeholder PTYPE for
25283 template TMPL based on the initializer INIT, and return the resulting
25287 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25288 tsubst_flags_t complain
)
25290 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25292 /* We should have handled this in the caller. */
25293 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25295 if (complain
& tf_error
)
25296 error ("non-class template %qT used without template arguments", tmpl
);
25297 return error_mark_node
;
25300 tree type
= TREE_TYPE (tmpl
);
25302 vec
<tree
,va_gc
> *args
;
25303 if (init
== NULL_TREE
25304 || TREE_CODE (init
) == TREE_LIST
)
25305 args
= make_tree_vector_from_list (init
);
25306 else if (BRACE_ENCLOSED_INITIALIZER_P (init
)
25307 && !TYPE_HAS_LIST_CTOR (type
)
25308 && !is_std_init_list (type
))
25309 args
= make_tree_vector_from_ctor (init
);
25311 args
= make_tree_vector_single (init
);
25313 tree dname
= dguide_name (tmpl
);
25314 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25315 /*type*/false, /*complain*/false,
25317 bool elided
= false;
25318 if (cands
== error_mark_node
)
25321 /* Prune explicit deduction guides in copy-initialization context. */
25322 if (flags
& LOOKUP_ONLYCONVERTING
)
25324 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25325 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25330 /* Found a nonconverting guide, prune the candidates. */
25331 tree pruned
= NULL_TREE
;
25332 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25333 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25334 pruned
= lookup_add (*iter
, pruned
);
25340 tree outer_args
= NULL_TREE
;
25341 if (DECL_CLASS_SCOPE_P (tmpl
)
25342 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25344 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25345 type
= TREE_TYPE (most_general_template (tmpl
));
25348 bool saw_ctor
= false;
25349 if (CLASSTYPE_METHOD_VEC (type
))
25350 // FIXME cache artificial deduction guides
25351 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
));
25354 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25355 if ((flags
& LOOKUP_ONLYCONVERTING
)
25356 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25359 cands
= lookup_add (guide
, cands
);
25364 if (args
->length () < 2)
25366 tree gtype
= NULL_TREE
;
25368 if (args
->length () == 1)
25369 gtype
= build_reference_type (type
);
25370 else if (!saw_ctor
)
25375 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25376 if ((flags
& LOOKUP_ONLYCONVERTING
)
25377 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25380 cands
= lookup_add (guide
, cands
);
25384 if (elided
&& !cands
)
25386 error ("cannot deduce template arguments for copy-initialization"
25387 " of %qT, as it has no non-explicit deduction guides or "
25388 "user-declared constructors", type
);
25389 return error_mark_node
;
25392 ++cp_unevaluated_operand
;
25393 tree t
= build_new_function_call (cands
, &args
, tf_decltype
);
25395 if (t
== error_mark_node
&& (complain
& tf_warning_or_error
))
25397 error ("class template argument deduction failed:");
25398 t
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25400 inform (input_location
, "explicit deduction guides not considered "
25401 "for copy-initialization");
25404 --cp_unevaluated_operand
;
25405 release_tree_vector (args
);
25407 return cp_build_qualified_type (TREE_TYPE (t
), cp_type_quals (ptype
));
25410 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25411 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25414 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25416 return do_auto_deduction (type
, init
, auto_node
,
25417 tf_warning_or_error
,
25421 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25422 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25423 The CONTEXT determines the context in which auto deduction is performed
25424 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25425 OUTER_TARGS are used during template argument deduction
25426 (context == adc_unify) to properly substitute the result, and is ignored
25429 For partial-concept-ids, extra args may be appended to the list of deduced
25430 template arguments prior to determining constraint satisfaction. */
25433 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25434 tsubst_flags_t complain
, auto_deduction_context context
,
25435 tree outer_targs
, int flags
)
25439 if (init
== error_mark_node
)
25440 return error_mark_node
;
25442 if (init
&& type_dependent_expression_p (init
)
25443 && context
!= adc_unify
)
25444 /* Defining a subset of type-dependent expressions that we can deduce
25445 from ahead of time isn't worth the trouble. */
25448 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25449 /* C++17 class template argument deduction. */
25450 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25452 if (TREE_TYPE (init
) == NULL_TREE
)
25453 /* Nothing we can do with this, even in deduction context. */
25456 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25457 with either a new invented type template parameter U or, if the
25458 initializer is a braced-init-list (8.5.4), with
25459 std::initializer_list<U>. */
25460 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25462 if (!DIRECT_LIST_INIT_P (init
))
25463 type
= listify_autos (type
, auto_node
);
25464 else if (CONSTRUCTOR_NELTS (init
) == 1)
25465 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25468 if (complain
& tf_warning_or_error
)
25470 if (permerror (input_location
, "direct-list-initialization of "
25471 "%<auto%> requires exactly one element"))
25472 inform (input_location
,
25473 "for deduction to %<std::initializer_list%>, use copy-"
25474 "list-initialization (i.e. add %<=%> before the %<{%>)");
25476 type
= listify_autos (type
, auto_node
);
25480 if (type
== error_mark_node
)
25481 return error_mark_node
;
25483 init
= resolve_nondeduced_context (init
, complain
);
25485 if (context
== adc_decomp_type
25486 && auto_node
== type
25487 && init
!= error_mark_node
25488 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25489 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25490 and initializer has array type, deduce cv-qualified array type. */
25491 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25493 else if (AUTO_IS_DECLTYPE (auto_node
))
25495 bool id
= (DECL_P (init
)
25496 || ((TREE_CODE (init
) == COMPONENT_REF
25497 || TREE_CODE (init
) == SCOPE_REF
)
25498 && !REF_PARENTHESIZED_P (init
)));
25499 targs
= make_tree_vec (1);
25500 TREE_VEC_ELT (targs
, 0)
25501 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25502 if (type
!= auto_node
)
25504 if (complain
& tf_error
)
25505 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25506 return error_mark_node
;
25511 tree parms
= build_tree_list (NULL_TREE
, type
);
25515 tparms
= extract_autos (type
);
25518 tparms
= make_tree_vec (1);
25519 TREE_VEC_ELT (tparms
, 0)
25520 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25523 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25524 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25525 DEDUCE_CALL
, LOOKUP_NORMAL
,
25526 NULL
, /*explain_p=*/false);
25529 if (processing_template_decl
)
25530 /* Try again at instantiation time. */
25532 if (type
&& type
!= error_mark_node
25533 && (complain
& tf_error
))
25534 /* If type is error_mark_node a diagnostic must have been
25535 emitted by now. Also, having a mention to '<type error>'
25536 in the diagnostic is not really useful to the user. */
25538 if (cfun
&& auto_node
== current_function_auto_return_pattern
25539 && LAMBDA_FUNCTION_P (current_function_decl
))
25540 error ("unable to deduce lambda return type from %qE", init
);
25542 error ("unable to deduce %qT from %qE", type
, init
);
25543 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25544 DEDUCE_CALL
, LOOKUP_NORMAL
,
25545 NULL
, /*explain_p=*/true);
25547 return error_mark_node
;
25551 /* Check any placeholder constraints against the deduced type. */
25552 if (flag_concepts
&& !processing_template_decl
)
25553 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
25555 /* Use the deduced type to check the associated constraints. If we
25556 have a partial-concept-id, rebuild the argument list so that
25557 we check using the extra arguments. */
25558 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
25559 tree cargs
= CHECK_CONSTR_ARGS (constr
);
25560 if (TREE_VEC_LENGTH (cargs
) > 1)
25562 cargs
= copy_node (cargs
);
25563 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
25567 if (!constraints_satisfied_p (constr
, cargs
))
25569 if (complain
& tf_warning_or_error
)
25573 case adc_unspecified
:
25575 error("placeholder constraints not satisfied");
25577 case adc_variable_type
:
25578 case adc_decomp_type
:
25579 error ("deduced initializer does not satisfy "
25580 "placeholder constraints");
25582 case adc_return_type
:
25583 error ("deduced return type does not satisfy "
25584 "placeholder constraints");
25586 case adc_requirement
:
25587 error ("deduced expression type does not satisfy "
25588 "placeholder constraints");
25591 diagnose_constraints (input_location
, constr
, targs
);
25593 return error_mark_node
;
25597 if (processing_template_decl
&& context
!= adc_unify
)
25598 outer_targs
= current_template_args ();
25599 targs
= add_to_template_args (outer_targs
, targs
);
25600 return tsubst (type
, targs
, complain
, NULL_TREE
);
25603 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25607 splice_late_return_type (tree type
, tree late_return_type
)
25609 if (is_auto (type
))
25611 if (late_return_type
)
25612 return late_return_type
;
25614 tree idx
= get_template_parm_index (type
);
25615 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
25616 /* In an abbreviated function template we didn't know we were dealing
25617 with a function template when we saw the auto return type, so update
25618 it to have the correct level. */
25619 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
25624 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25625 'decltype(auto)' or a deduced class template. */
25628 is_auto (const_tree type
)
25630 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25631 && (TYPE_IDENTIFIER (type
) == auto_identifier
25632 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
25633 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
25639 /* for_each_template_parm callback for type_uses_auto. */
25642 is_auto_r (tree tp
, void */
*data*/
)
25644 return is_auto (tp
);
25647 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25648 a use of `auto'. Returns NULL_TREE otherwise. */
25651 type_uses_auto (tree type
)
25653 if (type
== NULL_TREE
)
25655 else if (flag_concepts
)
25657 /* The Concepts TS allows multiple autos in one type-specifier; just
25658 return the first one we find, do_auto_deduction will collect all of
25660 if (uses_template_parms (type
))
25661 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
25662 /*visited*/NULL
, /*nondeduced*/true);
25667 return find_type_usage (type
, is_auto
);
25670 /* For a given template T, return the vector of typedefs referenced
25671 in T for which access check is needed at T instantiation time.
25672 T is either a FUNCTION_DECL or a RECORD_TYPE.
25673 Those typedefs were added to T by the function
25674 append_type_to_template_for_access_check. */
25676 vec
<qualified_typedef_usage_t
, va_gc
> *
25677 get_types_needing_access_check (tree t
)
25680 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
25682 if (!t
|| t
== error_mark_node
)
25685 if (!(ti
= get_template_info (t
)))
25688 if (CLASS_TYPE_P (t
)
25689 || TREE_CODE (t
) == FUNCTION_DECL
)
25691 if (!TI_TEMPLATE (ti
))
25694 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
25700 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25701 tied to T. That list of typedefs will be access checked at
25702 T instantiation time.
25703 T is either a FUNCTION_DECL or a RECORD_TYPE.
25704 TYPE_DECL is a TYPE_DECL node representing a typedef.
25705 SCOPE is the scope through which TYPE_DECL is accessed.
25706 LOCATION is the location of the usage point of TYPE_DECL.
25708 This function is a subroutine of
25709 append_type_to_template_for_access_check. */
25712 append_type_to_template_for_access_check_1 (tree t
,
25715 location_t location
)
25717 qualified_typedef_usage_t typedef_usage
;
25720 if (!t
|| t
== error_mark_node
)
25723 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
25724 || CLASS_TYPE_P (t
))
25726 && TREE_CODE (type_decl
) == TYPE_DECL
25729 if (!(ti
= get_template_info (t
)))
25732 gcc_assert (TI_TEMPLATE (ti
));
25734 typedef_usage
.typedef_decl
= type_decl
;
25735 typedef_usage
.context
= scope
;
25736 typedef_usage
.locus
= location
;
25738 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
25741 /* Append TYPE_DECL to the template TEMPL.
25742 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25743 At TEMPL instanciation time, TYPE_DECL will be checked to see
25744 if it can be accessed through SCOPE.
25745 LOCATION is the location of the usage point of TYPE_DECL.
25747 e.g. consider the following code snippet:
25754 template<class U> struct S
25756 C::myint mi; // <-- usage point of the typedef C::myint
25761 At S<char> instantiation time, we need to check the access of C::myint
25762 In other words, we need to check the access of the myint typedef through
25763 the C scope. For that purpose, this function will add the myint typedef
25764 and the scope C through which its being accessed to a list of typedefs
25765 tied to the template S. That list will be walked at template instantiation
25766 time and access check performed on each typedefs it contains.
25767 Note that this particular code snippet should yield an error because
25768 myint is private to C. */
25771 append_type_to_template_for_access_check (tree templ
,
25774 location_t location
)
25776 qualified_typedef_usage_t
*iter
;
25779 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
25781 /* Make sure we don't append the type to the template twice. */
25782 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
25783 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
25786 append_type_to_template_for_access_check_1 (templ
, type_decl
,
25790 /* Convert the generic type parameters in PARM that match the types given in the
25791 range [START_IDX, END_IDX) from the current_template_parms into generic type
25795 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
25797 tree current
= current_template_parms
;
25798 int depth
= TMPL_PARMS_DEPTH (current
);
25799 current
= INNERMOST_TEMPLATE_PARMS (current
);
25800 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
25802 for (int i
= 0; i
< start_idx
; ++i
)
25803 TREE_VEC_ELT (replacement
, i
)
25804 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25806 for (int i
= start_idx
; i
< end_idx
; ++i
)
25808 /* Create a distinct parameter pack type from the current parm and add it
25809 to the replacement args to tsubst below into the generic function
25812 tree o
= TREE_TYPE (TREE_VALUE
25813 (TREE_VEC_ELT (current
, i
)));
25814 tree t
= copy_type (o
);
25815 TEMPLATE_TYPE_PARM_INDEX (t
)
25816 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
25818 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
25819 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
25820 TYPE_MAIN_VARIANT (t
) = t
;
25821 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
25822 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
25823 TREE_VEC_ELT (replacement
, i
) = t
;
25824 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
25827 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
25828 TREE_VEC_ELT (replacement
, i
)
25829 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25831 /* If there are more levels then build up the replacement with the outer
25834 replacement
= add_to_template_args (template_parms_to_args
25835 (TREE_CHAIN (current_template_parms
)),
25838 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
25841 /* Entries in the decl_constraint hash table. */
25842 struct GTY((for_user
)) constr_entry
25848 /* Hashing function and equality for constraint entries. */
25849 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
25851 static hashval_t
hash (constr_entry
*e
)
25853 return (hashval_t
)DECL_UID (e
->decl
);
25856 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
25858 return e1
->decl
== e2
->decl
;
25862 /* A mapping from declarations to constraint information. Note that
25863 both templates and their underlying declarations are mapped to the
25864 same constraint information.
25866 FIXME: This is defined in pt.c because garbage collection
25867 code is not being generated for constraint.cc. */
25869 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
25871 /* Returns the template constraints of declaration T. If T is not
25872 constrained, return NULL_TREE. Note that T must be non-null. */
25875 get_constraints (tree t
)
25877 if (!flag_concepts
)
25880 gcc_assert (DECL_P (t
));
25881 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25882 t
= DECL_TEMPLATE_RESULT (t
);
25883 constr_entry elt
= { t
, NULL_TREE
};
25884 constr_entry
* found
= decl_constraints
->find (&elt
);
25891 /* Associate the given constraint information CI with the declaration
25892 T. If T is a template, then the constraints are associated with
25893 its underlying declaration. Don't build associations if CI is
25897 set_constraints (tree t
, tree ci
)
25901 gcc_assert (t
&& flag_concepts
);
25902 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25903 t
= DECL_TEMPLATE_RESULT (t
);
25904 gcc_assert (!get_constraints (t
));
25905 constr_entry elt
= {t
, ci
};
25906 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
25907 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
25912 /* Remove the associated constraints of the declaration T. */
25915 remove_constraints (tree t
)
25917 gcc_assert (DECL_P (t
));
25918 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25919 t
= DECL_TEMPLATE_RESULT (t
);
25921 constr_entry elt
= {t
, NULL_TREE
};
25922 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
25924 decl_constraints
->clear_slot (slot
);
25927 /* Memoized satisfaction results for declarations. This
25928 maps the pair (constraint_info, arguments) to the result computed
25929 by constraints_satisfied_p. */
25931 struct GTY((for_user
)) constraint_sat_entry
25938 /* Hashing function and equality for constraint entries. */
25940 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
25942 static hashval_t
hash (constraint_sat_entry
*e
)
25944 hashval_t val
= iterative_hash_object(e
->ci
, 0);
25945 return iterative_hash_template_arg (e
->args
, val
);
25948 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
25950 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
25954 /* Memoized satisfaction results for concept checks. */
25956 struct GTY((for_user
)) concept_spec_entry
25963 /* Hashing function and equality for constraint entries. */
25965 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
25967 static hashval_t
hash (concept_spec_entry
*e
)
25969 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
25972 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
25974 ++comparing_specializations
;
25975 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
25976 --comparing_specializations
;
25981 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
25982 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
25984 /* Search for a memoized satisfaction result. Returns one of the
25985 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25988 lookup_constraint_satisfaction (tree ci
, tree args
)
25990 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
25991 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
25993 return found
->result
;
25998 /* Memoize the result of a satisfication test. Returns the saved result. */
26001 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
26003 constraint_sat_entry elt
= {ci
, args
, result
};
26004 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
26005 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
26011 /* Search for a memoized satisfaction result for a concept. */
26014 lookup_concept_satisfaction (tree tmpl
, tree args
)
26016 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26017 concept_spec_entry
* found
= concept_memos
->find (&elt
);
26019 return found
->result
;
26024 /* Memoize the result of a concept check. Returns the saved result. */
26027 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
26029 concept_spec_entry elt
= {tmpl
, args
, result
};
26030 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
26031 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26037 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
26039 /* Returns a prior concept specialization. This returns the substituted
26040 and normalized constraints defined by the concept. */
26043 get_concept_expansion (tree tmpl
, tree args
)
26045 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26046 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26048 return found
->result
;
26053 /* Save a concept expansion for later. */
26056 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26058 concept_spec_entry elt
= {tmpl
, args
, def
};
26059 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26060 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26067 hash_subsumption_args (tree t1
, tree t2
)
26069 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26070 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26072 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26073 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26074 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26075 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26079 /* Compare the constraints of two subsumption entries. The LEFT1 and
26080 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26081 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26084 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26086 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26087 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26088 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26089 CHECK_CONSTR_ARGS (right1
)))
26090 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26091 CHECK_CONSTR_ARGS (right2
));
26095 /* Key/value pair for learning and memoizing subsumption results. This
26096 associates a pair of check constraints (including arguments) with
26097 a boolean value indicating the result. */
26099 struct GTY((for_user
)) subsumption_entry
26106 /* Hashing function and equality for constraint entries. */
26108 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26110 static hashval_t
hash (subsumption_entry
*e
)
26112 return hash_subsumption_args (e
->t1
, e
->t2
);
26115 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26117 ++comparing_specializations
;
26118 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26119 --comparing_specializations
;
26124 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26126 /* Search for a previously cached subsumption result. */
26129 lookup_subsumption_result (tree t1
, tree t2
)
26131 subsumption_entry elt
= { t1
, t2
, false };
26132 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26134 return &found
->result
;
26139 /* Save a subsumption result. */
26142 save_subsumption_result (tree t1
, tree t2
, bool result
)
26144 subsumption_entry elt
= {t1
, t2
, result
};
26145 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26146 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26152 /* Set up the hash table for constraint association. */
26155 init_constraint_processing (void)
26157 if (!flag_concepts
)
26160 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26161 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26162 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26163 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26164 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26167 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26171 declare_integer_pack (void)
26173 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26174 build_function_type_list (integer_type_node
,
26177 NULL_TREE
, ECF_CONST
);
26178 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26179 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26182 /* Set up the hash tables for template instantiations. */
26185 init_template_processing (void)
26187 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26188 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26190 if (cxx_dialect
>= cxx11
)
26191 declare_integer_pack ();
26194 /* Print stats about the template hash tables for -fstats. */
26197 print_template_statistics (void)
26199 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26200 "%f collisions\n", (long) decl_specializations
->size (),
26201 (long) decl_specializations
->elements (),
26202 decl_specializations
->collisions ());
26203 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26204 "%f collisions\n", (long) type_specializations
->size (),
26205 (long) type_specializations
->elements (),
26206 type_specializations
->collisions ());
26209 #include "gt-cp-pt.h"