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 (strcmp (IDENTIFIER_POINTER (DECL_NAME (fn
)), "__integer_pack") == 0)
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 (strcmp (IDENTIFIER_POINTER (DECL_NAME (fn
)), "__integer_pack") == 0)
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
)
6128 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
6130 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
6132 if (processing_template_decl
6133 && TREE_CODE (expr
) == ADDR_EXPR
6134 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
6136 if (complain
& tf_error
)
6138 error ("%qE is not a valid template argument for type %qT",
6140 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6145 /* Returns TRUE iff the address of OP is value-dependent.
6147 14.6.2.4 [temp.dep.temp]:
6148 A non-integral non-type template-argument is dependent if its type is
6149 dependent or it has either of the following forms
6152 and contains a nested-name-specifier which specifies a class-name that
6153 names a dependent type.
6155 We generalize this to just say that the address of a member of a
6156 dependent class is value-dependent; the above doesn't cover the
6157 address of a static data member named with an unqualified-id. */
6160 has_value_dependent_address (tree op
)
6162 /* We could use get_inner_reference here, but there's no need;
6163 this is only relevant for template non-type arguments, which
6164 can only be expressed as &id-expression. */
6167 tree ctx
= CP_DECL_CONTEXT (op
);
6168 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6175 /* The next set of functions are used for providing helpful explanatory
6176 diagnostics for failed overload resolution. Their messages should be
6177 indented by two spaces for consistency with the messages in
6181 unify_success (bool /*explain_p*/)
6186 /* Other failure functions should call this one, to provide a single function
6187 for setting a breakpoint on. */
6190 unify_invalid (bool /*explain_p*/)
6196 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6199 inform (input_location
,
6200 " couldn't deduce template parameter %qD", parm
);
6201 return unify_invalid (explain_p
);
6205 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6208 inform (input_location
,
6209 " types %qT and %qT have incompatible cv-qualifiers",
6211 return unify_invalid (explain_p
);
6215 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6218 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6219 return unify_invalid (explain_p
);
6223 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6226 inform (input_location
,
6227 " template parameter %qD is not a parameter pack, but "
6230 return unify_invalid (explain_p
);
6234 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6237 inform (input_location
,
6238 " template argument %qE does not match "
6239 "pointer-to-member constant %qE",
6241 return unify_invalid (explain_p
);
6245 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6248 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6249 return unify_invalid (explain_p
);
6253 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6256 inform (input_location
,
6257 " inconsistent parameter pack deduction with %qT and %qT",
6259 return unify_invalid (explain_p
);
6263 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6268 inform (input_location
,
6269 " deduced conflicting types for parameter %qT (%qT and %qT)",
6270 parm
, first
, second
);
6272 inform (input_location
,
6273 " deduced conflicting values for non-type parameter "
6274 "%qE (%qE and %qE)", parm
, first
, second
);
6276 return unify_invalid (explain_p
);
6280 unify_vla_arg (bool explain_p
, tree arg
)
6283 inform (input_location
,
6284 " variable-sized array type %qT is not "
6285 "a valid template argument",
6287 return unify_invalid (explain_p
);
6291 unify_method_type_error (bool explain_p
, tree arg
)
6294 inform (input_location
,
6295 " member function type %qT is not a valid template argument",
6297 return unify_invalid (explain_p
);
6301 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6306 inform_n (input_location
, wanted
,
6307 " candidate expects at least %d argument, %d provided",
6308 " candidate expects at least %d arguments, %d provided",
6311 inform_n (input_location
, wanted
,
6312 " candidate expects %d argument, %d provided",
6313 " candidate expects %d arguments, %d provided",
6316 return unify_invalid (explain_p
);
6320 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6322 return unify_arity (explain_p
, have
, wanted
);
6326 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6327 bool least_p
= false)
6329 return unify_arity (explain_p
, have
, wanted
, least_p
);
6333 unify_arg_conversion (bool explain_p
, tree to_type
,
6334 tree from_type
, tree arg
)
6337 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6338 " cannot convert %qE (type %qT) to type %qT",
6339 arg
, from_type
, to_type
);
6340 return unify_invalid (explain_p
);
6344 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6345 tree parm
, tree arg
)
6350 case tbr_ambiguous_baseclass
:
6351 inform (input_location
, " %qT is an ambiguous base class of %qT",
6355 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6358 return unify_invalid (explain_p
);
6362 unify_inconsistent_template_template_parameters (bool explain_p
)
6365 inform (input_location
,
6366 " template parameters of a template template argument are "
6367 "inconsistent with other deduced template arguments");
6368 return unify_invalid (explain_p
);
6372 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6375 inform (input_location
,
6376 " can't deduce a template for %qT from non-template type %qT",
6378 return unify_invalid (explain_p
);
6382 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6385 inform (input_location
,
6386 " template argument %qE does not match %qE", arg
, parm
);
6387 return unify_invalid (explain_p
);
6391 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6394 inform (input_location
,
6395 " could not resolve address from overloaded function %qE",
6397 return unify_invalid (explain_p
);
6400 /* Attempt to convert the non-type template parameter EXPR to the
6401 indicated TYPE. If the conversion is successful, return the
6402 converted value. If the conversion is unsuccessful, return
6403 NULL_TREE if we issued an error message, or error_mark_node if we
6404 did not. We issue error messages for out-and-out bad template
6405 parameters, but not simply because the conversion failed, since we
6406 might be just trying to do argument deduction. Both TYPE and EXPR
6407 must be non-dependent.
6409 The conversion follows the special rules described in
6410 [temp.arg.nontype], and it is much more strict than an implicit
6413 This function is called twice for each template argument (see
6414 lookup_template_class for a more accurate description of this
6415 problem). This means that we need to handle expressions which
6416 are not valid in a C++ source, but can be created from the
6417 first call (for instance, casts to perform conversions). These
6418 hacks can go away after we fix the double coercion problem. */
6421 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6425 /* Detect immediately string literals as invalid non-type argument.
6426 This special-case is not needed for correctness (we would easily
6427 catch this later), but only to provide better diagnostic for this
6428 common user mistake. As suggested by DR 100, we do not mention
6429 linkage issues in the diagnostic as this is not the point. */
6430 /* FIXME we're making this OK. */
6431 if (TREE_CODE (expr
) == STRING_CST
)
6433 if (complain
& tf_error
)
6434 error ("%qE is not a valid template argument for type %qT "
6435 "because string literals can never be used in this context",
6440 /* Add the ADDR_EXPR now for the benefit of
6441 value_dependent_expression_p. */
6442 if (TYPE_PTROBV_P (type
)
6443 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6445 expr
= decay_conversion (expr
, complain
);
6446 if (expr
== error_mark_node
)
6447 return error_mark_node
;
6450 /* If we are in a template, EXPR may be non-dependent, but still
6451 have a syntactic, rather than semantic, form. For example, EXPR
6452 might be a SCOPE_REF, rather than the VAR_DECL to which the
6453 SCOPE_REF refers. Preserving the qualifying scope is necessary
6454 so that access checking can be performed when the template is
6455 instantiated -- but here we need the resolved form so that we can
6456 convert the argument. */
6457 bool non_dep
= false;
6458 if (TYPE_REF_OBJ_P (type
)
6459 && has_value_dependent_address (expr
))
6460 /* If we want the address and it's value-dependent, don't fold. */;
6461 else if (processing_template_decl
6462 && potential_nondependent_constant_expression (expr
))
6464 if (error_operand_p (expr
))
6465 return error_mark_node
;
6466 expr_type
= TREE_TYPE (expr
);
6467 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6468 expr
= mark_lvalue_use (expr
);
6470 expr
= mark_rvalue_use (expr
);
6472 /* If the argument is non-dependent, perform any conversions in
6473 non-dependent context as well. */
6474 processing_template_decl_sentinel
s (non_dep
);
6476 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6478 if (value_dependent_expression_p (expr
))
6479 expr
= canonicalize_expr_argument (expr
, complain
);
6481 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6482 to a non-type argument of "nullptr". */
6483 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
6484 expr
= fold_simple (convert (type
, expr
));
6486 /* In C++11, integral or enumeration non-type template arguments can be
6487 arbitrary constant expressions. Pointer and pointer to
6488 member arguments can be general constant expressions that evaluate
6489 to a null value, but otherwise still need to be of a specific form. */
6490 if (cxx_dialect
>= cxx11
)
6492 if (TREE_CODE (expr
) == PTRMEM_CST
)
6493 /* A PTRMEM_CST is already constant, and a valid template
6494 argument for a parameter of pointer to member type, we just want
6495 to leave it in that form rather than lower it to a
6497 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6498 /* Constant value checking is done later with type conversion. */;
6499 else if (cxx_dialect
>= cxx1z
)
6501 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6502 expr
= maybe_constant_value (expr
);
6503 else if (REFERENCE_REF_P (expr
))
6505 expr
= TREE_OPERAND (expr
, 0);
6506 expr
= maybe_constant_value (expr
);
6507 expr
= convert_from_reference (expr
);
6510 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6512 tree folded
= maybe_constant_value (expr
);
6513 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6514 : null_member_pointer_value_p (folded
))
6519 /* HACK: Due to double coercion, we can get a
6520 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6521 which is the tree that we built on the first call (see
6522 below when coercing to reference to object or to reference to
6523 function). We just strip everything and get to the arg.
6524 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6526 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6528 tree probe_type
, probe
= expr
;
6529 if (REFERENCE_REF_P (probe
))
6530 probe
= TREE_OPERAND (probe
, 0);
6531 probe_type
= TREE_TYPE (probe
);
6532 if (TREE_CODE (probe
) == NOP_EXPR
)
6534 /* ??? Maybe we could use convert_from_reference here, but we
6535 would need to relax its constraints because the NOP_EXPR
6536 could actually change the type to something more cv-qualified,
6537 and this is not folded by convert_from_reference. */
6538 tree addr
= TREE_OPERAND (probe
, 0);
6539 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6540 && TREE_CODE (addr
) == ADDR_EXPR
6541 && TYPE_PTR_P (TREE_TYPE (addr
))
6542 && (same_type_ignoring_top_level_qualifiers_p
6543 (TREE_TYPE (probe_type
),
6544 TREE_TYPE (TREE_TYPE (addr
)))))
6546 expr
= TREE_OPERAND (addr
, 0);
6547 expr_type
= TREE_TYPE (probe_type
);
6552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6553 parameter is a pointer to object, through decay and
6554 qualification conversion. Let's strip everything. */
6555 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6559 if (TREE_CODE (probe
) == ADDR_EXPR
6560 && TYPE_PTR_P (TREE_TYPE (probe
)))
6562 /* Skip the ADDR_EXPR only if it is part of the decay for
6563 an array. Otherwise, it is part of the original argument
6564 in the source code. */
6565 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6566 probe
= TREE_OPERAND (probe
, 0);
6568 expr_type
= TREE_TYPE (expr
);
6572 /* [temp.arg.nontype]/5, bullet 1
6574 For a non-type template-parameter of integral or enumeration type,
6575 integral promotions (_conv.prom_) and integral conversions
6576 (_conv.integral_) are applied. */
6577 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6579 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6580 t
= maybe_constant_value (t
);
6581 if (t
!= error_mark_node
)
6584 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6585 return error_mark_node
;
6587 /* Notice that there are constant expressions like '4 % 0' which
6588 do not fold into integer constants. */
6589 if (TREE_CODE (expr
) != INTEGER_CST
6590 && !value_dependent_expression_p (expr
))
6592 if (complain
& tf_error
)
6594 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6595 if (processing_template_decl
6596 && !require_potential_constant_expression (expr
))
6598 expr
= cxx_constant_value (expr
);
6599 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6600 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6601 "in template argument for type %qT ", type
);
6602 if (expr
== error_mark_node
)
6604 /* else cxx_constant_value complained but gave us
6605 a real constant, so go ahead. */
6606 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6612 /* Avoid typedef problems. */
6613 if (TREE_TYPE (expr
) != type
)
6614 expr
= fold_convert (type
, expr
);
6616 /* [temp.arg.nontype]/5, bullet 2
6618 For a non-type template-parameter of type pointer to object,
6619 qualification conversions (_conv.qual_) and the array-to-pointer
6620 conversion (_conv.array_) are applied. */
6621 else if (TYPE_PTROBV_P (type
))
6623 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6625 A template-argument for a non-type, non-template template-parameter
6626 shall be one of: [...]
6628 -- the name of a non-type template-parameter;
6629 -- the address of an object or function with external linkage, [...]
6630 expressed as "& id-expression" where the & is optional if the name
6631 refers to a function or array, or if the corresponding
6632 template-parameter is a reference.
6634 Here, we do not care about functions, as they are invalid anyway
6635 for a parameter of type pointer-to-object. */
6637 if (value_dependent_expression_p (expr
))
6638 /* Non-type template parameters are OK. */
6640 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6641 /* Null pointer values are OK in C++11. */;
6642 else if (TREE_CODE (expr
) != ADDR_EXPR
6643 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6647 if (complain
& tf_error
)
6648 error ("%qD is not a valid template argument "
6649 "because %qD is a variable, not the address of "
6650 "a variable", expr
, expr
);
6653 if (POINTER_TYPE_P (expr_type
))
6655 if (complain
& tf_error
)
6656 error ("%qE is not a valid template argument for %qT "
6657 "because it is not the address of a variable",
6661 /* Other values, like integer constants, might be valid
6662 non-type arguments of some other type. */
6663 return error_mark_node
;
6669 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6670 ? TREE_OPERAND (expr
, 0) : expr
);
6673 if (complain
& tf_error
)
6674 error ("%qE is not a valid template argument of type %qT "
6675 "because %qE is not a variable", expr
, type
, decl
);
6678 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6680 if (complain
& tf_error
)
6681 error ("%qE is not a valid template argument of type %qT "
6682 "because %qD does not have external linkage",
6686 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6688 if (complain
& tf_error
)
6689 error ("%qE is not a valid template argument of type %qT "
6690 "because %qD has no linkage", expr
, type
, decl
);
6695 expr
= decay_conversion (expr
, complain
);
6696 if (expr
== error_mark_node
)
6697 return error_mark_node
;
6699 expr
= perform_qualification_conversions (type
, expr
);
6700 if (expr
== error_mark_node
)
6701 return error_mark_node
;
6703 /* [temp.arg.nontype]/5, bullet 3
6705 For a non-type template-parameter of type reference to object, no
6706 conversions apply. The type referred to by the reference may be more
6707 cv-qualified than the (otherwise identical) type of the
6708 template-argument. The template-parameter is bound directly to the
6709 template-argument, which must be an lvalue. */
6710 else if (TYPE_REF_OBJ_P (type
))
6712 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6714 return error_mark_node
;
6716 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6718 if (complain
& tf_error
)
6719 error ("%qE is not a valid template argument for type %qT "
6720 "because of conflicts in cv-qualification", expr
, type
);
6724 if (!lvalue_p (expr
))
6726 if (complain
& tf_error
)
6727 error ("%qE is not a valid template argument for type %qT "
6728 "because it is not an lvalue", expr
, type
);
6732 /* [temp.arg.nontype]/1
6734 A template-argument for a non-type, non-template template-parameter
6735 shall be one of: [...]
6737 -- the address of an object or function with external linkage. */
6738 if (INDIRECT_REF_P (expr
)
6739 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6741 expr
= TREE_OPERAND (expr
, 0);
6744 if (complain
& tf_error
)
6745 error ("%q#D is not a valid template argument for type %qT "
6746 "because a reference variable does not have a constant "
6747 "address", expr
, type
);
6752 if (!value_dependent_expression_p (expr
))
6756 if (complain
& tf_error
)
6757 error ("%qE is not a valid template argument for type %qT "
6758 "because it is not an object with linkage",
6763 /* DR 1155 allows internal linkage in C++11 and up. */
6764 linkage_kind linkage
= decl_linkage (expr
);
6765 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6767 if (complain
& tf_error
)
6768 error ("%qE is not a valid template argument for type %qT "
6769 "because object %qD does not have linkage",
6774 expr
= build_nop (type
, build_address (expr
));
6777 /* [temp.arg.nontype]/5, bullet 4
6779 For a non-type template-parameter of type pointer to function, only
6780 the function-to-pointer conversion (_conv.func_) is applied. If the
6781 template-argument represents a set of overloaded functions (or a
6782 pointer to such), the matching function is selected from the set
6784 else if (TYPE_PTRFN_P (type
))
6786 /* If the argument is a template-id, we might not have enough
6787 context information to decay the pointer. */
6788 if (!type_unknown_p (expr_type
))
6790 expr
= decay_conversion (expr
, complain
);
6791 if (expr
== error_mark_node
)
6792 return error_mark_node
;
6795 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6796 /* Null pointer values are OK in C++11. */
6797 return perform_qualification_conversions (type
, expr
);
6799 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6800 if (!expr
|| expr
== error_mark_node
)
6803 /* [temp.arg.nontype]/5, bullet 5
6805 For a non-type template-parameter of type reference to function, no
6806 conversions apply. If the template-argument represents a set of
6807 overloaded functions, the matching function is selected from the set
6809 else if (TYPE_REFFN_P (type
))
6811 if (TREE_CODE (expr
) == ADDR_EXPR
)
6813 if (complain
& tf_error
)
6815 error ("%qE is not a valid template argument for type %qT "
6816 "because it is a pointer", expr
, type
);
6817 inform (input_location
, "try using %qE instead",
6818 TREE_OPERAND (expr
, 0));
6823 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6824 if (!expr
|| expr
== error_mark_node
)
6827 /* [temp.arg.nontype]/5, bullet 6
6829 For a non-type template-parameter of type pointer to member function,
6830 no conversions apply. If the template-argument represents a set of
6831 overloaded member functions, the matching member function is selected
6832 from the set (_over.over_). */
6833 else if (TYPE_PTRMEMFUNC_P (type
))
6835 expr
= instantiate_type (type
, expr
, tf_none
);
6836 if (expr
== error_mark_node
)
6837 return error_mark_node
;
6839 /* [temp.arg.nontype] bullet 1 says the pointer to member
6840 expression must be a pointer-to-member constant. */
6841 if (!value_dependent_expression_p (expr
)
6842 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6843 return error_mark_node
;
6845 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6846 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6847 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6848 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6850 /* There is no way to disable standard conversions in
6851 resolve_address_of_overloaded_function (called by
6852 instantiate_type). It is possible that the call succeeded by
6853 converting &B::I to &D::I (where B is a base of D), so we need
6854 to reject this conversion here.
6856 Actually, even if there was a way to disable standard conversions,
6857 it would still be better to reject them here so that we can
6858 provide a superior diagnostic. */
6859 if (!same_type_p (TREE_TYPE (expr
), type
))
6861 if (complain
& tf_error
)
6863 error ("%qE is not a valid template argument for type %qT "
6864 "because it is of type %qT", expr
, type
,
6866 /* If we are just one standard conversion off, explain. */
6867 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6868 inform (input_location
,
6869 "standard conversions are not allowed in this context");
6874 /* [temp.arg.nontype]/5, bullet 7
6876 For a non-type template-parameter of type pointer to data member,
6877 qualification conversions (_conv.qual_) are applied. */
6878 else if (TYPE_PTRDATAMEM_P (type
))
6880 /* [temp.arg.nontype] bullet 1 says the pointer to member
6881 expression must be a pointer-to-member constant. */
6882 if (!value_dependent_expression_p (expr
)
6883 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6884 return error_mark_node
;
6886 expr
= perform_qualification_conversions (type
, expr
);
6887 if (expr
== error_mark_node
)
6890 else if (NULLPTR_TYPE_P (type
))
6892 if (expr
!= nullptr_node
)
6894 if (complain
& tf_error
)
6895 error ("%qE is not a valid template argument for type %qT "
6896 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6901 /* A template non-type parameter must be one of the above. */
6905 /* Sanity check: did we actually convert the argument to the
6907 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6908 (type
, TREE_TYPE (expr
)));
6909 return convert_from_reference (expr
);
6912 /* Subroutine of coerce_template_template_parms, which returns 1 if
6913 PARM_PARM and ARG_PARM match using the rule for the template
6914 parameters of template template parameters. Both PARM and ARG are
6915 template parameters; the rest of the arguments are the same as for
6916 coerce_template_template_parms.
6919 coerce_template_template_parm (tree parm
,
6921 tsubst_flags_t complain
,
6925 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6926 || parm
== NULL_TREE
|| error_operand_p (parm
))
6929 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6932 switch (TREE_CODE (parm
))
6935 /* We encounter instantiations of templates like
6936 template <template <template <class> class> class TT>
6939 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6940 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6942 if (!coerce_template_template_parms
6943 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6949 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6950 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6951 /* Argument is a parameter pack but parameter is not. */
6956 /* The tsubst call is used to handle cases such as
6958 template <int> class C {};
6959 template <class T, template <T> class TT> class D {};
6962 i.e. the parameter list of TT depends on earlier parameters. */
6963 if (!uses_template_parms (TREE_TYPE (arg
)))
6965 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6966 if (!uses_template_parms (t
)
6967 && !same_type_p (t
, TREE_TYPE (arg
)))
6971 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6972 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6973 /* Argument is a parameter pack but parameter is not. */
6985 /* Coerce template argument list ARGLIST for use with template
6986 template-parameter TEMPL. */
6989 coerce_template_args_for_ttp (tree templ
, tree arglist
,
6990 tsubst_flags_t complain
)
6992 /* Consider an example where a template template parameter declared as
6994 template <class T, class U = std::allocator<T> > class TT
6996 The template parameter level of T and U are one level larger than
6997 of TT. To proper process the default argument of U, say when an
6998 instantiation `TT<int>' is seen, we need to build the full
6999 arguments containing {int} as the innermost level. Outer levels,
7000 available when not appearing as default template argument, can be
7001 obtained from the arguments of the enclosing template.
7003 Suppose that TT is later substituted with std::vector. The above
7004 instantiation is `TT<int, std::allocator<T> >' with TT at
7005 level 1, and T at level 2, while the template arguments at level 1
7006 becomes {std::vector} and the inner level 2 is {int}. */
7008 tree outer
= DECL_CONTEXT (templ
);
7011 if (DECL_TEMPLATE_SPECIALIZATION (outer
))
7012 /* We want arguments for the partial specialization, not arguments for
7013 the primary template. */
7014 outer
= template_parms_to_args (DECL_TEMPLATE_PARMS (outer
));
7016 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7018 else if (current_template_parms
)
7020 /* This is an argument of the current template, so we haven't set
7021 DECL_CONTEXT yet. */
7022 tree relevant_template_parms
;
7024 /* Parameter levels that are greater than the level of the given
7025 template template parm are irrelevant. */
7026 relevant_template_parms
= current_template_parms
;
7027 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
7028 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
7029 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
7031 outer
= template_parms_to_args (relevant_template_parms
);
7035 arglist
= add_to_template_args (outer
, arglist
);
7037 tree parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7038 return coerce_template_parms (parmlist
, arglist
, templ
,
7040 /*require_all_args=*/true,
7041 /*use_default_args=*/true);
7044 /* A cache of template template parameters with match-all default
7046 static GTY((deletable
)) hash_map
<tree
,tree
> *defaulted_ttp_cache
;
7048 store_defaulted_ttp (tree v
, tree t
)
7050 if (!defaulted_ttp_cache
)
7051 defaulted_ttp_cache
= hash_map
<tree
,tree
>::create_ggc (13);
7052 defaulted_ttp_cache
->put (v
, t
);
7055 lookup_defaulted_ttp (tree v
)
7057 if (defaulted_ttp_cache
)
7058 if (tree
*p
= defaulted_ttp_cache
->get (v
))
7063 /* T is a bound template template-parameter. Copy its arguments into default
7064 arguments of the template template-parameter's template parameters. */
7067 add_defaults_to_ttp (tree otmpl
)
7069 if (tree c
= lookup_defaulted_ttp (otmpl
))
7072 tree ntmpl
= copy_node (otmpl
);
7074 tree ntype
= copy_node (TREE_TYPE (otmpl
));
7075 TYPE_STUB_DECL (ntype
) = TYPE_NAME (ntype
) = ntmpl
;
7076 TYPE_MAIN_VARIANT (ntype
) = ntype
;
7077 TYPE_POINTER_TO (ntype
) = TYPE_REFERENCE_TO (ntype
) = NULL_TREE
;
7078 TYPE_NAME (ntype
) = ntmpl
;
7079 SET_TYPE_STRUCTURAL_EQUALITY (ntype
);
7081 tree idx
= TEMPLATE_TYPE_PARM_INDEX (ntype
)
7082 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype
));
7083 TEMPLATE_PARM_DECL (idx
) = ntmpl
;
7084 TREE_TYPE (ntmpl
) = TREE_TYPE (idx
) = ntype
;
7086 tree oparms
= DECL_TEMPLATE_PARMS (otmpl
);
7087 tree parms
= DECL_TEMPLATE_PARMS (ntmpl
) = copy_node (oparms
);
7088 TREE_CHAIN (parms
) = TREE_CHAIN (oparms
);
7089 tree vec
= TREE_VALUE (parms
) = copy_node (TREE_VALUE (parms
));
7090 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
7092 tree o
= TREE_VEC_ELT (vec
, i
);
7093 if (!template_parameter_pack_p (TREE_VALUE (o
)))
7095 tree n
= TREE_VEC_ELT (vec
, i
) = copy_node (o
);
7096 TREE_PURPOSE (n
) = any_targ_node
;
7100 store_defaulted_ttp (otmpl
, ntmpl
);
7104 /* ARG is a bound potential template template-argument, and PARGS is a list
7105 of arguments for the corresponding template template-parameter. Adjust
7106 PARGS as appropriate for application to ARG's template, and if ARG is a
7107 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7108 arguments to the template template parameter. */
7111 coerce_ttp_args_for_tta (tree
& arg
, tree pargs
, tsubst_flags_t complain
)
7113 ++processing_template_decl
;
7114 tree arg_tmpl
= TYPE_TI_TEMPLATE (arg
);
7115 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl
))
7117 /* When comparing two template template-parameters in partial ordering,
7118 rewrite the one currently being used as an argument to have default
7119 arguments for all parameters. */
7120 arg_tmpl
= add_defaults_to_ttp (arg_tmpl
);
7121 pargs
= coerce_template_args_for_ttp (arg_tmpl
, pargs
, complain
);
7122 if (pargs
!= error_mark_node
)
7123 arg
= bind_template_template_parm (TREE_TYPE (arg_tmpl
),
7124 TYPE_TI_ARGS (arg
));
7129 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl
));
7130 pargs
= coerce_template_parms (aparms
, pargs
, arg_tmpl
, complain
,
7131 /*require_all*/true,
7132 /*use_default*/true);
7134 --processing_template_decl
;
7138 /* Subroutine of unify for the case when PARM is a
7139 BOUND_TEMPLATE_TEMPLATE_PARM. */
7142 unify_bound_ttp_args (tree tparms
, tree targs
, tree parm
, tree
& arg
,
7145 tree parmvec
= TYPE_TI_ARGS (parm
);
7146 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
7148 /* The template template parm might be variadic and the argument
7149 not, so flatten both argument lists. */
7150 parmvec
= expand_template_argument_pack (parmvec
);
7151 argvec
= expand_template_argument_pack (argvec
);
7153 tree nparmvec
= parmvec
;
7156 /* In keeping with P0522R0, adjust P's template arguments
7157 to apply to A's template; then flatten it again. */
7158 nparmvec
= coerce_ttp_args_for_tta (arg
, parmvec
, tf_none
);
7159 nparmvec
= expand_template_argument_pack (nparmvec
);
7162 if (unify (tparms
, targs
, nparmvec
, argvec
,
7163 UNIFY_ALLOW_NONE
, explain_p
))
7166 /* If the P0522 adjustment eliminated a pack expansion, deduce
7169 && TREE_VEC_LENGTH (nparmvec
) < TREE_VEC_LENGTH (parmvec
)
7170 && unify_pack_expansion (tparms
, targs
, parmvec
, argvec
,
7171 DEDUCE_EXACT
, /*sub*/true, explain_p
))
7177 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7178 template template parameters. Both PARM_PARMS and ARG_PARMS are
7179 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7182 Consider the example:
7183 template <class T> class A;
7184 template<template <class U> class TT> class B;
7186 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7187 the parameters to A, and OUTER_ARGS contains A. */
7190 coerce_template_template_parms (tree parm_parms
,
7192 tsubst_flags_t complain
,
7196 int nparms
, nargs
, i
;
7200 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
7201 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
7203 nparms
= TREE_VEC_LENGTH (parm_parms
);
7204 nargs
= TREE_VEC_LENGTH (arg_parms
);
7208 /* P0522R0: A template template-parameter P is at least as specialized as
7209 a template template-argument A if, given the following rewrite to two
7210 function templates, the function template corresponding to P is at
7211 least as specialized as the function template corresponding to A
7212 according to the partial ordering rules for function templates
7213 ([temp.func.order]). Given an invented class template X with the
7214 template parameter list of A (including default arguments):
7216 * Each of the two function templates has the same template parameters,
7217 respectively, as P or A.
7219 * Each function template has a single function parameter whose type is
7220 a specialization of X with template arguments corresponding to the
7221 template parameters from the respective function template where, for
7222 each template parameter PP in the template parameter list of the
7223 function template, a corresponding template argument AA is formed. If
7224 PP declares a parameter pack, then AA is the pack expansion
7225 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7227 If the rewrite produces an invalid type, then P is not at least as
7228 specialized as A. */
7230 /* So coerce P's args to apply to A's parms, and then deduce between A's
7231 args and the converted args. If that succeeds, A is at least as
7232 specialized as P, so they match.*/
7233 tree pargs
= template_parms_level_to_args (parm_parms
);
7234 ++processing_template_decl
;
7235 pargs
= coerce_template_parms (arg_parms
, pargs
, NULL_TREE
, tf_none
,
7236 /*require_all*/true, /*use_default*/true);
7237 --processing_template_decl
;
7238 if (pargs
!= error_mark_node
)
7240 tree targs
= make_tree_vec (nargs
);
7241 tree aargs
= template_parms_level_to_args (arg_parms
);
7242 if (!unify (arg_parms
, targs
, aargs
, pargs
, UNIFY_ALLOW_NONE
,
7248 /* Determine whether we have a parameter pack at the end of the
7249 template template parameter's template parameter list. */
7250 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
7252 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
7254 if (error_operand_p (parm
))
7257 switch (TREE_CODE (parm
))
7261 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
7266 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
7276 && !(variadic_p
&& nargs
>= nparms
- 1))
7279 /* Check all of the template parameters except the parameter pack at
7280 the end (if any). */
7281 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
7283 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
7284 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7287 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7288 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7290 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7298 /* Check each of the template parameters in the template
7299 argument against the template parameter pack at the end of
7300 the template template parameter. */
7301 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
7304 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
7306 for (; i
< nargs
; ++i
)
7308 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
7311 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
7313 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
7322 /* Verifies that the deduced template arguments (in TARGS) for the
7323 template template parameters (in TPARMS) represent valid bindings,
7324 by comparing the template parameter list of each template argument
7325 to the template parameter list of its corresponding template
7326 template parameter, in accordance with DR150. This
7327 routine can only be called after all template arguments have been
7328 deduced. It will return TRUE if all of the template template
7329 parameter bindings are okay, FALSE otherwise. */
7331 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
7333 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7336 /* We're dealing with template parms in this process. */
7337 ++processing_template_decl
;
7339 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
7341 for (i
= 0; i
< ntparms
; ++i
)
7343 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
7344 tree targ
= TREE_VEC_ELT (targs
, i
);
7346 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
7348 tree packed_args
= NULL_TREE
;
7351 if (ARGUMENT_PACK_P (targ
))
7353 /* Look inside the argument pack. */
7354 packed_args
= ARGUMENT_PACK_ARGS (targ
);
7355 len
= TREE_VEC_LENGTH (packed_args
);
7358 for (idx
= 0; idx
< len
; ++idx
)
7360 tree targ_parms
= NULL_TREE
;
7363 /* Extract the next argument from the argument
7365 targ
= TREE_VEC_ELT (packed_args
, idx
);
7367 if (PACK_EXPANSION_P (targ
))
7368 /* Look at the pattern of the pack expansion. */
7369 targ
= PACK_EXPANSION_PATTERN (targ
);
7371 /* Extract the template parameters from the template
7373 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7374 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7375 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7376 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7378 /* Verify that we can coerce the template template
7379 parameters from the template argument to the template
7380 parameter. This requires an exact match. */
7382 && !coerce_template_template_parms
7383 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7398 --processing_template_decl
;
7402 /* Since type attributes aren't mangled, we need to strip them from
7403 template type arguments. */
7406 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7408 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7410 bool removed_attributes
= false;
7411 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7412 if (removed_attributes
7413 && (complain
& tf_warning
))
7414 warning (OPT_Wignored_attributes
,
7415 "ignoring attributes on template argument %qT", arg
);
7419 /* And from inside dependent non-type arguments like sizeof(Type). */
7422 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7424 if (!arg
|| arg
== error_mark_node
)
7426 bool removed_attributes
= false;
7427 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7428 if (removed_attributes
7429 && (complain
& tf_warning
))
7430 warning (OPT_Wignored_attributes
,
7431 "ignoring attributes in template argument %qE", arg
);
7435 // A template declaration can be substituted for a constrained
7436 // template template parameter only when the argument is more
7437 // constrained than the parameter.
7439 is_compatible_template_arg (tree parm
, tree arg
)
7441 tree parm_cons
= get_constraints (parm
);
7443 /* For now, allow constrained template template arguments
7444 and unconstrained template template parameters. */
7445 if (parm_cons
== NULL_TREE
)
7448 tree arg_cons
= get_constraints (arg
);
7450 // If the template parameter is constrained, we need to rewrite its
7451 // constraints in terms of the ARG's template parameters. This ensures
7452 // that all of the template parameter types will have the same depth.
7454 // Note that this is only valid when coerce_template_template_parm is
7455 // true for the innermost template parameters of PARM and ARG. In other
7456 // words, because coercion is successful, this conversion will be valid.
7459 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7460 parm_cons
= tsubst_constraint_info (parm_cons
,
7461 INNERMOST_TEMPLATE_ARGS (args
),
7462 tf_none
, NULL_TREE
);
7463 if (parm_cons
== error_mark_node
)
7467 return subsumes (parm_cons
, arg_cons
);
7470 // Convert a placeholder argument into a binding to the original
7471 // parameter. The original parameter is saved as the TREE_TYPE of
7474 convert_wildcard_argument (tree parm
, tree arg
)
7476 TREE_TYPE (arg
) = parm
;
7480 /* Convert the indicated template ARG as necessary to match the
7481 indicated template PARM. Returns the converted ARG, or
7482 error_mark_node if the conversion was unsuccessful. Error and
7483 warning messages are issued under control of COMPLAIN. This
7484 conversion is for the Ith parameter in the parameter list. ARGS is
7485 the full set of template arguments deduced so far. */
7488 convert_template_argument (tree parm
,
7491 tsubst_flags_t complain
,
7497 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7499 if (parm
== error_mark_node
)
7500 return error_mark_node
;
7502 /* Trivially convert placeholders. */
7503 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7504 return convert_wildcard_argument (parm
, arg
);
7506 if (arg
== any_targ_node
)
7509 if (TREE_CODE (arg
) == TREE_LIST
7510 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7512 /* The template argument was the name of some
7513 member function. That's usually
7514 invalid, but static members are OK. In any
7515 case, grab the underlying fields/functions
7516 and issue an error later if required. */
7517 orig_arg
= TREE_VALUE (arg
);
7518 TREE_TYPE (arg
) = unknown_type_node
;
7523 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7524 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7525 || requires_tmpl_type
);
7527 /* When determining whether an argument pack expansion is a template,
7528 look at the pattern. */
7529 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7530 arg
= PACK_EXPANSION_PATTERN (arg
);
7532 /* Deal with an injected-class-name used as a template template arg. */
7533 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7535 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7536 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7538 if (cxx_dialect
>= cxx11
)
7539 /* OK under DR 1004. */;
7540 else if (complain
& tf_warning_or_error
)
7541 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7542 " used as template template argument", TYPE_NAME (arg
));
7543 else if (flag_pedantic_errors
)
7551 ((TREE_CODE (arg
) == TEMPLATE_DECL
7552 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7553 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7554 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7555 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7558 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7559 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7560 arg
= TYPE_STUB_DECL (arg
);
7562 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7564 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7565 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7567 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7569 if (complain
& tf_error
)
7570 error ("invalid use of destructor %qE as a type", orig_arg
);
7571 return error_mark_node
;
7574 permerror (input_location
,
7575 "to refer to a type member of a template parameter, "
7576 "use %<typename %E%>", orig_arg
);
7578 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7579 TREE_OPERAND (arg
, 1),
7585 if (is_type
!= requires_type
)
7589 if (complain
& tf_error
)
7591 error ("type/value mismatch at argument %d in template "
7592 "parameter list for %qD",
7595 inform (input_location
,
7596 " expected a constant of type %qT, got %qT",
7598 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7599 else if (requires_tmpl_type
)
7600 inform (input_location
,
7601 " expected a class template, got %qE", orig_arg
);
7603 inform (input_location
,
7604 " expected a type, got %qE", orig_arg
);
7607 return error_mark_node
;
7609 if (is_tmpl_type
^ requires_tmpl_type
)
7611 if (in_decl
&& (complain
& tf_error
))
7613 error ("type/value mismatch at argument %d in template "
7614 "parameter list for %qD",
7617 inform (input_location
,
7618 " expected a type, got %qT", DECL_NAME (arg
));
7620 inform (input_location
,
7621 " expected a class template, got %qT", orig_arg
);
7623 return error_mark_node
;
7626 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7627 /* We already did the appropriate conversion when packing args. */
7631 if (requires_tmpl_type
)
7633 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7634 /* The number of argument required is not known yet.
7635 Just accept it for now. */
7636 val
= TREE_TYPE (arg
);
7639 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7642 /* Strip alias templates that are equivalent to another
7644 arg
= get_underlying_template (arg
);
7645 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7647 if (coerce_template_template_parms (parmparm
, argparm
,
7653 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7655 if (val
!= error_mark_node
)
7657 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7658 val
= TREE_TYPE (val
);
7659 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7660 val
= make_pack_expansion (val
);
7665 if (in_decl
&& (complain
& tf_error
))
7667 error ("type/value mismatch at argument %d in "
7668 "template parameter list for %qD",
7670 inform (input_location
,
7671 " expected a template of type %qD, got %qT",
7675 val
= error_mark_node
;
7678 // Check that the constraints are compatible before allowing the
7680 if (val
!= error_mark_node
)
7681 if (!is_compatible_template_arg (parm
, arg
))
7683 if (in_decl
&& (complain
& tf_error
))
7685 error ("constraint mismatch at argument %d in "
7686 "template parameter list for %qD",
7688 inform (input_location
, " expected %qD but got %qD",
7691 val
= error_mark_node
;
7697 /* We only form one instance of each template specialization.
7698 Therefore, if we use a non-canonical variant (i.e., a
7699 typedef), any future messages referring to the type will use
7700 the typedef, which is confusing if those future uses do not
7701 themselves also use the typedef. */
7703 val
= canonicalize_type_argument (val
, complain
);
7707 tree t
= TREE_TYPE (parm
);
7709 if (tree a
= type_uses_auto (t
))
7711 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unify
, args
);
7712 if (t
== error_mark_node
)
7713 return error_mark_node
;
7716 t
= tsubst (t
, args
, complain
, in_decl
);
7718 if (invalid_nontype_parm_type_p (t
, complain
))
7719 return error_mark_node
;
7721 if (!type_dependent_expression_p (orig_arg
)
7722 && !uses_template_parms (t
))
7723 /* We used to call digest_init here. However, digest_init
7724 will report errors, which we don't want when complain
7725 is zero. More importantly, digest_init will try too
7726 hard to convert things: for example, `0' should not be
7727 converted to pointer type at this point according to
7728 the standard. Accepting this is not merely an
7729 extension, since deciding whether or not these
7730 conversions can occur is part of determining which
7731 function template to call, or whether a given explicit
7732 argument specification is valid. */
7733 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7735 val
= canonicalize_expr_argument (orig_arg
, complain
);
7737 if (val
== NULL_TREE
)
7738 val
= error_mark_node
;
7739 else if (val
== error_mark_node
&& (complain
& tf_error
))
7740 error ("could not convert template argument %qE from %qT to %qT",
7741 orig_arg
, TREE_TYPE (orig_arg
), t
);
7743 if (INDIRECT_REF_P (val
))
7745 /* Reject template arguments that are references to built-in
7746 functions with no library fallbacks. */
7747 const_tree inner
= TREE_OPERAND (val
, 0);
7748 const_tree innertype
= TREE_TYPE (inner
);
7750 && TREE_CODE (innertype
) == REFERENCE_TYPE
7751 && TREE_CODE (TREE_TYPE (innertype
)) == FUNCTION_TYPE
7752 && 0 < TREE_OPERAND_LENGTH (inner
)
7753 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7754 return error_mark_node
;
7757 if (TREE_CODE (val
) == SCOPE_REF
)
7759 /* Strip typedefs from the SCOPE_REF. */
7760 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7761 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7763 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7764 QUALIFIED_NAME_IS_TEMPLATE (val
));
7771 /* Coerces the remaining template arguments in INNER_ARGS (from
7772 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7773 Returns the coerced argument pack. PARM_IDX is the position of this
7774 parameter in the template parameter list. ARGS is the original
7775 template argument list. */
7777 coerce_template_parameter_pack (tree parms
,
7785 tsubst_flags_t complain
)
7787 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7788 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7791 tree packed_parms
= NULL_TREE
;
7793 if (arg_idx
> nargs
)
7796 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7798 /* When the template parameter is a non-type template parameter pack
7799 or template template parameter pack whose type or template
7800 parameters use parameter packs, we know exactly how many arguments
7801 we are looking for. Build a vector of the instantiated decls for
7802 these template parameters in PACKED_PARMS. */
7803 /* We can't use make_pack_expansion here because it would interpret a
7804 _DECL as a use rather than a declaration. */
7805 tree decl
= TREE_VALUE (parm
);
7806 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7807 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7808 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7809 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7811 TREE_VEC_LENGTH (args
)--;
7812 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7813 TREE_VEC_LENGTH (args
)++;
7815 if (packed_parms
== error_mark_node
)
7816 return error_mark_node
;
7818 /* If we're doing a partial instantiation of a member template,
7819 verify that all of the types used for the non-type
7820 template parameter pack are, in fact, valid for non-type
7821 template parameters. */
7823 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7825 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7826 for (j
= 0; j
< len
; ++j
)
7828 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7829 if (invalid_nontype_parm_type_p (t
, complain
))
7830 return error_mark_node
;
7832 /* We don't know how many args we have yet, just
7833 use the unconverted ones for now. */
7837 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7839 /* Check if we have a placeholder pack, which indicates we're
7840 in the context of a introduction list. In that case we want
7841 to match this pack to the single placeholder. */
7842 else if (arg_idx
< nargs
7843 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7844 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7846 nargs
= arg_idx
+ 1;
7847 packed_args
= make_tree_vec (1);
7850 packed_args
= make_tree_vec (nargs
- arg_idx
);
7852 /* Convert the remaining arguments, which will be a part of the
7853 parameter pack "parm". */
7854 int first_pack_arg
= arg_idx
;
7855 for (; arg_idx
< nargs
; ++arg_idx
)
7857 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7858 tree actual_parm
= TREE_VALUE (parm
);
7859 int pack_idx
= arg_idx
- first_pack_arg
;
7863 /* Once we've packed as many args as we have types, stop. */
7864 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7866 else if (PACK_EXPANSION_P (arg
))
7867 /* We don't know how many args we have yet, just
7868 use the unconverted ones for now. */
7871 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7874 if (arg
== error_mark_node
)
7876 if (complain
& tf_error
)
7877 error ("template argument %d is invalid", arg_idx
+ 1);
7880 arg
= convert_template_argument (actual_parm
,
7881 arg
, new_args
, complain
, parm_idx
,
7883 if (arg
== error_mark_node
)
7885 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7888 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7889 && TREE_VEC_LENGTH (packed_args
) > 0)
7891 if (complain
& tf_error
)
7892 error ("wrong number of template arguments (%d, should be %d)",
7893 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7894 return error_mark_node
;
7897 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7898 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7899 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7902 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7903 TREE_CONSTANT (argument_pack
) = 1;
7906 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7908 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7909 TREE_VEC_LENGTH (packed_args
));
7910 return argument_pack
;
7913 /* Returns the number of pack expansions in the template argument vector
7917 pack_expansion_args_count (tree args
)
7922 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7924 tree elt
= TREE_VEC_ELT (args
, i
);
7925 if (elt
&& PACK_EXPANSION_P (elt
))
7931 /* Convert all template arguments to their appropriate types, and
7932 return a vector containing the innermost resulting template
7933 arguments. If any error occurs, return error_mark_node. Error and
7934 warning messages are issued under control of COMPLAIN.
7936 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7937 for arguments not specified in ARGS. Otherwise, if
7938 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7939 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7940 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7944 coerce_template_parms (tree parms
,
7947 tsubst_flags_t complain
,
7948 bool require_all_args
,
7949 bool use_default_args
)
7951 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7952 tree orig_inner_args
;
7955 tree new_inner_args
;
7956 int saved_unevaluated_operand
;
7957 int saved_inhibit_evaluation_warnings
;
7959 /* When used as a boolean value, indicates whether this is a
7960 variadic template parameter list. Since it's an int, we can also
7961 subtract it from nparms to get the number of non-variadic
7964 int variadic_args_p
= 0;
7965 int post_variadic_parms
= 0;
7967 /* Likewise for parameters with default arguments. */
7970 if (args
== error_mark_node
)
7971 return error_mark_node
;
7973 nparms
= TREE_VEC_LENGTH (parms
);
7975 /* Determine if there are any parameter packs or default arguments. */
7976 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7978 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7980 ++post_variadic_parms
;
7981 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7983 if (TREE_PURPOSE (parm
))
7987 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7988 /* If there are no parameters that follow a parameter pack, we need to
7989 expand any argument packs so that we can deduce a parameter pack from
7990 some non-packed args followed by an argument pack, as in variadic85.C.
7991 If there are such parameters, we need to leave argument packs intact
7992 so the arguments are assigned properly. This can happen when dealing
7993 with a nested class inside a partial specialization of a class
7994 template, as in variadic92.C, or when deducing a template parameter pack
7995 from a sub-declarator, as in variadic114.C. */
7996 if (!post_variadic_parms
)
7997 inner_args
= expand_template_argument_pack (inner_args
);
7999 /* Count any pack expansion args. */
8000 variadic_args_p
= pack_expansion_args_count (inner_args
);
8002 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
8003 if ((nargs
- variadic_args_p
> nparms
&& !variadic_p
)
8004 || (nargs
< nparms
- variadic_p
8007 && (!use_default_args
8008 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
8009 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
8011 if (complain
& tf_error
)
8013 if (variadic_p
|| default_p
)
8015 nparms
-= variadic_p
+ default_p
;
8016 error ("wrong number of template arguments "
8017 "(%d, should be at least %d)", nargs
, nparms
);
8020 error ("wrong number of template arguments "
8021 "(%d, should be %d)", nargs
, nparms
);
8024 inform (DECL_SOURCE_LOCATION (in_decl
),
8025 "provided for %qD", in_decl
);
8028 return error_mark_node
;
8030 /* We can't pass a pack expansion to a non-pack parameter of an alias
8031 template (DR 1430). */
8033 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
8034 || concept_template_p (in_decl
))
8036 && nargs
- variadic_args_p
< nparms
- variadic_p
)
8038 if (complain
& tf_error
)
8040 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
8042 tree arg
= TREE_VEC_ELT (inner_args
, i
);
8043 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8045 if (PACK_EXPANSION_P (arg
)
8046 && !template_parameter_pack_p (parm
))
8048 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
8049 error_at (location_of (arg
),
8050 "pack expansion argument for non-pack parameter "
8051 "%qD of alias template %qD", parm
, in_decl
);
8053 error_at (location_of (arg
),
8054 "pack expansion argument for non-pack parameter "
8055 "%qD of concept %qD", parm
, in_decl
);
8056 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
8063 return error_mark_node
;
8066 /* We need to evaluate the template arguments, even though this
8067 template-id may be nested within a "sizeof". */
8068 saved_unevaluated_operand
= cp_unevaluated_operand
;
8069 cp_unevaluated_operand
= 0;
8070 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8071 c_inhibit_evaluation_warnings
= 0;
8072 new_inner_args
= make_tree_vec (nparms
);
8073 new_args
= add_outermost_template_args (args
, new_inner_args
);
8074 int pack_adjust
= 0;
8075 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
8080 /* Get the Ith template parameter. */
8081 parm
= TREE_VEC_ELT (parms
, parm_idx
);
8083 if (parm
== error_mark_node
)
8085 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
8089 /* Calculate the next argument. */
8090 if (arg_idx
< nargs
)
8091 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
8095 if (template_parameter_pack_p (TREE_VALUE (parm
))
8096 && !(arg
&& ARGUMENT_PACK_P (arg
)))
8098 /* Some arguments will be placed in the
8099 template parameter pack PARM. */
8100 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
8101 inner_args
, arg_idx
,
8105 if (arg
== NULL_TREE
)
8107 /* We don't know how many args we have yet, just use the
8108 unconverted (and still packed) ones for now. */
8109 new_inner_args
= orig_inner_args
;
8114 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
8116 /* Store this argument. */
8117 if (arg
== error_mark_node
)
8120 /* We are done with all of the arguments. */
8125 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
8126 arg_idx
+= pack_adjust
;
8133 if (PACK_EXPANSION_P (arg
))
8135 /* "If every valid specialization of a variadic template
8136 requires an empty template parameter pack, the template is
8137 ill-formed, no diagnostic required." So check that the
8138 pattern works with this parameter. */
8139 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
8140 tree conv
= convert_template_argument (TREE_VALUE (parm
),
8144 if (conv
== error_mark_node
)
8146 if (complain
& tf_error
)
8147 inform (input_location
, "so any instantiation with a "
8148 "non-empty parameter pack would be ill-formed");
8151 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
8152 /* Recover from missing typename. */
8153 TREE_VEC_ELT (inner_args
, arg_idx
)
8154 = make_pack_expansion (conv
);
8156 /* We don't know how many args we have yet, just
8157 use the unconverted ones for now. */
8158 new_inner_args
= inner_args
;
8163 else if (require_all_args
)
8165 /* There must be a default arg in this case. */
8166 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
8168 /* The position of the first default template argument,
8169 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8171 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8172 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8173 arg_idx
- pack_adjust
);
8178 if (arg
== error_mark_node
)
8180 if (complain
& tf_error
)
8181 error ("template argument %d is invalid", arg_idx
+ 1);
8184 /* This only occurs if there was an error in the template
8185 parameter list itself (which we would already have
8186 reported) that we are trying to recover from, e.g., a class
8187 template with a parameter list such as
8188 template<typename..., typename>. */
8191 arg
= convert_template_argument (TREE_VALUE (parm
),
8192 arg
, new_args
, complain
,
8195 if (arg
== error_mark_node
)
8197 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
8199 cp_unevaluated_operand
= saved_unevaluated_operand
;
8200 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8202 if (variadic_p
&& arg_idx
< nargs
)
8204 if (complain
& tf_error
)
8206 error ("wrong number of template arguments "
8207 "(%d, should be %d)", nargs
, arg_idx
);
8209 error ("provided for %q+D", in_decl
);
8211 return error_mark_node
;
8215 return error_mark_node
;
8217 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
8218 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
8219 TREE_VEC_LENGTH (new_inner_args
));
8221 return new_inner_args
;
8224 /* Convert all template arguments to their appropriate types, and
8225 return a vector containing the innermost resulting template
8226 arguments. If any error occurs, return error_mark_node. Error and
8227 warning messages are not issued.
8229 Note that no function argument deduction is performed, and default
8230 arguments are used to fill in unspecified arguments. */
8232 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
8234 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
8237 /* Convert all template arguments to their appropriate type, and
8238 instantiate default arguments as needed. This returns a vector
8239 containing the innermost resulting template arguments, or
8240 error_mark_node if unsuccessful. */
8242 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
8243 tsubst_flags_t complain
)
8245 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
8248 /* Like coerce_template_parms. If PARMS represents all template
8249 parameters levels, this function returns a vector of vectors
8250 representing all the resulting argument levels. Note that in this
8251 case, only the innermost arguments are coerced because the
8252 outermost ones are supposed to have been coerced already.
8254 Otherwise, if PARMS represents only (the innermost) vector of
8255 parameters, this function returns a vector containing just the
8256 innermost resulting arguments. */
8259 coerce_innermost_template_parms (tree parms
,
8262 tsubst_flags_t complain
,
8263 bool require_all_args
,
8264 bool use_default_args
)
8266 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
8267 int args_depth
= TMPL_ARGS_DEPTH (args
);
8270 if (parms_depth
> 1)
8272 coerced_args
= make_tree_vec (parms_depth
);
8276 for (level
= parms
, cur_depth
= parms_depth
;
8277 parms_depth
> 0 && level
!= NULL_TREE
;
8278 level
= TREE_CHAIN (level
), --cur_depth
)
8281 if (cur_depth
== args_depth
)
8282 l
= coerce_template_parms (TREE_VALUE (level
),
8283 args
, in_decl
, complain
,
8287 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
8289 if (l
== error_mark_node
)
8290 return error_mark_node
;
8292 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
8296 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
8297 args
, in_decl
, complain
,
8300 return coerced_args
;
8303 /* Returns 1 if template args OT and NT are equivalent. */
8306 template_args_equal (tree ot
, tree nt
, bool partial_order
/* = false */)
8310 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
8312 if (nt
== any_targ_node
|| ot
== any_targ_node
)
8315 if (TREE_CODE (nt
) == TREE_VEC
)
8316 /* For member templates */
8317 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
8318 else if (PACK_EXPANSION_P (ot
))
8319 return (PACK_EXPANSION_P (nt
)
8320 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
8321 PACK_EXPANSION_PATTERN (nt
))
8322 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
8323 PACK_EXPANSION_EXTRA_ARGS (nt
)));
8324 else if (ARGUMENT_PACK_P (ot
))
8329 if (!ARGUMENT_PACK_P (nt
))
8332 opack
= ARGUMENT_PACK_ARGS (ot
);
8333 npack
= ARGUMENT_PACK_ARGS (nt
);
8334 len
= TREE_VEC_LENGTH (opack
);
8335 if (TREE_VEC_LENGTH (npack
) != len
)
8337 for (i
= 0; i
< len
; ++i
)
8338 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
8339 TREE_VEC_ELT (npack
, i
)))
8343 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
8345 else if (TYPE_P (nt
))
8349 /* Don't treat an alias template specialization with dependent
8350 arguments as equivalent to its underlying type when used as a
8351 template argument; we need them to be distinct so that we
8352 substitute into the specialization arguments at instantiation
8353 time. And aliases can't be equivalent without being ==, so
8354 we don't need to look any deeper.
8356 During partial ordering, however, we need to treat them normally so
8357 that we can order uses of the same alias with different
8358 cv-qualification (79960). */
8360 && (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
)))
8363 return same_type_p (ot
, nt
);
8365 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
8369 /* Try to treat a template non-type argument that has been converted
8370 to the parameter type as equivalent to one that hasn't yet. */
8371 for (enum tree_code code1
= TREE_CODE (ot
);
8372 CONVERT_EXPR_CODE_P (code1
)
8373 || code1
== NON_LVALUE_EXPR
;
8374 code1
= TREE_CODE (ot
))
8375 ot
= TREE_OPERAND (ot
, 0);
8376 for (enum tree_code code2
= TREE_CODE (nt
);
8377 CONVERT_EXPR_CODE_P (code2
)
8378 || code2
== NON_LVALUE_EXPR
;
8379 code2
= TREE_CODE (nt
))
8380 nt
= TREE_OPERAND (nt
, 0);
8382 return cp_tree_equal (ot
, nt
);
8386 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8387 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8388 NEWARG_PTR with the offending arguments if they are non-NULL. */
8391 comp_template_args (tree oldargs
, tree newargs
,
8392 tree
*oldarg_ptr
, tree
*newarg_ptr
,
8397 if (oldargs
== newargs
)
8400 if (!oldargs
|| !newargs
)
8403 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8406 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8408 tree nt
= TREE_VEC_ELT (newargs
, i
);
8409 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8411 if (! template_args_equal (ot
, nt
, partial_order
))
8413 if (oldarg_ptr
!= NULL
)
8415 if (newarg_ptr
!= NULL
)
8424 comp_template_args_porder (tree oargs
, tree nargs
)
8426 return comp_template_args (oargs
, nargs
, NULL
, NULL
, true);
8430 add_pending_template (tree d
)
8432 tree ti
= (TYPE_P (d
)
8433 ? CLASSTYPE_TEMPLATE_INFO (d
)
8434 : DECL_TEMPLATE_INFO (d
));
8435 struct pending_template
*pt
;
8438 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8441 /* We are called both from instantiate_decl, where we've already had a
8442 tinst_level pushed, and instantiate_template, where we haven't.
8444 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8447 push_tinst_level (d
);
8449 pt
= ggc_alloc
<pending_template
> ();
8451 pt
->tinst
= current_tinst_level
;
8452 if (last_pending_template
)
8453 last_pending_template
->next
= pt
;
8455 pending_templates
= pt
;
8457 last_pending_template
= pt
;
8459 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8466 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8467 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8468 documentation for TEMPLATE_ID_EXPR. */
8471 lookup_template_function (tree fns
, tree arglist
)
8475 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8476 return error_mark_node
;
8478 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8480 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8482 error ("%q#D is not a function template", fns
);
8483 return error_mark_node
;
8486 if (BASELINK_P (fns
))
8488 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8490 BASELINK_FUNCTIONS (fns
),
8495 type
= TREE_TYPE (fns
);
8496 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8497 type
= unknown_type_node
;
8499 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8502 /* Within the scope of a template class S<T>, the name S gets bound
8503 (in build_self_reference) to a TYPE_DECL for the class, not a
8504 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8505 or one of its enclosing classes, and that type is a template,
8506 return the associated TEMPLATE_DECL. Otherwise, the original
8509 Also handle the case when DECL is a TREE_LIST of ambiguous
8510 injected-class-names from different bases. */
8513 maybe_get_template_decl_from_type_decl (tree decl
)
8515 if (decl
== NULL_TREE
)
8518 /* DR 176: A lookup that finds an injected-class-name (10.2
8519 [class.member.lookup]) can result in an ambiguity in certain cases
8520 (for example, if it is found in more than one base class). If all of
8521 the injected-class-names that are found refer to specializations of
8522 the same class template, and if the name is followed by a
8523 template-argument-list, the reference refers to the class template
8524 itself and not a specialization thereof, and is not ambiguous. */
8525 if (TREE_CODE (decl
) == TREE_LIST
)
8527 tree t
, tmpl
= NULL_TREE
;
8528 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8530 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8533 else if (tmpl
!= elt
)
8536 if (tmpl
&& t
== NULL_TREE
)
8542 return (decl
!= NULL_TREE
8543 && DECL_SELF_REFERENCE_P (decl
)
8544 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8545 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8548 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8549 parameters, find the desired type.
8551 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8553 IN_DECL, if non-NULL, is the template declaration we are trying to
8556 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8557 the class we are looking up.
8559 Issue error and warning messages under control of COMPLAIN.
8561 If the template class is really a local class in a template
8562 function, then the FUNCTION_CONTEXT is the function in which it is
8565 ??? Note that this function is currently called *twice* for each
8566 template-id: the first time from the parser, while creating the
8567 incomplete type (finish_template_type), and the second type during the
8568 real instantiation (instantiate_template_class). This is surely something
8569 that we want to avoid. It also causes some problems with argument
8570 coercion (see convert_nontype_argument for more information on this). */
8573 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8574 int entering_scope
, tsubst_flags_t complain
)
8576 tree templ
= NULL_TREE
, parmlist
;
8583 if (identifier_p (d1
))
8585 tree value
= innermost_non_namespace_value (d1
);
8586 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8591 push_decl_namespace (context
);
8592 templ
= lookup_name (d1
);
8593 templ
= maybe_get_template_decl_from_type_decl (templ
);
8595 pop_decl_namespace ();
8598 context
= DECL_CONTEXT (templ
);
8600 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8602 tree type
= TREE_TYPE (d1
);
8604 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8605 an implicit typename for the second A. Deal with it. */
8606 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8607 type
= TREE_TYPE (type
);
8609 if (CLASSTYPE_TEMPLATE_INFO (type
))
8611 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8612 d1
= DECL_NAME (templ
);
8615 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8616 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8618 templ
= TYPE_TI_TEMPLATE (d1
);
8619 d1
= DECL_NAME (templ
);
8621 else if (DECL_TYPE_TEMPLATE_P (d1
))
8624 d1
= DECL_NAME (templ
);
8625 context
= DECL_CONTEXT (templ
);
8627 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8630 d1
= DECL_NAME (templ
);
8633 /* Issue an error message if we didn't find a template. */
8636 if (complain
& tf_error
)
8637 error ("%qT is not a template", d1
);
8638 return error_mark_node
;
8641 if (TREE_CODE (templ
) != TEMPLATE_DECL
8642 /* Make sure it's a user visible template, if it was named by
8644 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8645 && !PRIMARY_TEMPLATE_P (templ
)))
8647 if (complain
& tf_error
)
8649 error ("non-template type %qT used as a template", d1
);
8651 error ("for template declaration %q+D", in_decl
);
8653 return error_mark_node
;
8656 complain
&= ~tf_user
;
8658 /* An alias that just changes the name of a template is equivalent to the
8659 other template, so if any of the arguments are pack expansions, strip
8660 the alias to avoid problems with a pack expansion passed to a non-pack
8661 alias template parameter (DR 1430). */
8662 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8663 templ
= get_underlying_template (templ
);
8665 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8668 tree arglist2
= coerce_template_args_for_ttp (templ
, arglist
, complain
);
8669 if (arglist2
== error_mark_node
8670 || (!uses_template_parms (arglist2
)
8671 && check_instantiated_args (templ
, arglist2
, complain
)))
8672 return error_mark_node
;
8674 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8679 tree template_type
= TREE_TYPE (templ
);
8682 tree found
= NULL_TREE
;
8685 int is_dependent_type
;
8686 int use_partial_inst_tmpl
= false;
8688 if (template_type
== error_mark_node
)
8689 /* An error occurred while building the template TEMPL, and a
8690 diagnostic has most certainly been emitted for that
8691 already. Let's propagate that error. */
8692 return error_mark_node
;
8694 gen_tmpl
= most_general_template (templ
);
8695 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8696 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8697 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8699 if (arg_depth
== 1 && parm_depth
> 1)
8701 /* We've been given an incomplete set of template arguments.
8704 template <class T> struct S1 {
8705 template <class U> struct S2 {};
8706 template <class U> struct S2<U*> {};
8709 we will be called with an ARGLIST of `U*', but the
8710 TEMPLATE will be `template <class T> template
8711 <class U> struct S1<T>::S2'. We must fill in the missing
8713 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8714 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8715 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8718 /* Now we should have enough arguments. */
8719 gcc_assert (parm_depth
== arg_depth
);
8721 /* From here on, we're only interested in the most general
8724 /* Calculate the BOUND_ARGS. These will be the args that are
8725 actually tsubst'd into the definition to create the
8727 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8729 /*require_all_args=*/true,
8730 /*use_default_args=*/true);
8732 if (arglist
== error_mark_node
)
8733 /* We were unable to bind the arguments. */
8734 return error_mark_node
;
8736 /* In the scope of a template class, explicit references to the
8737 template class refer to the type of the template, not any
8738 instantiation of it. For example, in:
8740 template <class T> class C { void f(C<T>); }
8742 the `C<T>' is just the same as `C'. Outside of the
8743 class, however, such a reference is an instantiation. */
8745 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8746 || currently_open_class (template_type
))
8748 tree tinfo
= TYPE_TEMPLATE_INFO (template_type
);
8750 if (tinfo
&& comp_template_args (TI_ARGS (tinfo
), arglist
))
8751 return template_type
;
8754 /* If we already have this specialization, return it. */
8755 elt
.tmpl
= gen_tmpl
;
8757 elt
.spec
= NULL_TREE
;
8758 hash
= spec_hasher::hash (&elt
);
8759 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8764 /* If the the template's constraints are not satisfied,
8765 then we cannot form a valid type.
8767 Note that the check is deferred until after the hash
8768 lookup. This prevents redundant checks on previously
8769 instantiated specializations. */
8770 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8772 if (complain
& tf_error
)
8774 error ("template constraint failure");
8775 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8777 return error_mark_node
;
8780 is_dependent_type
= uses_template_parms (arglist
);
8782 /* If the deduced arguments are invalid, then the binding
8784 if (!is_dependent_type
8785 && check_instantiated_args (gen_tmpl
,
8786 INNERMOST_TEMPLATE_ARGS (arglist
),
8788 return error_mark_node
;
8790 if (!is_dependent_type
8791 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8792 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8793 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8795 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8796 DECL_NAME (gen_tmpl
),
8797 /*tag_scope=*/ts_global
);
8801 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8803 if (context
== error_mark_node
)
8804 return error_mark_node
;
8807 context
= global_namespace
;
8809 /* Create the type. */
8810 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8812 /* The user referred to a specialization of an alias
8813 template represented by GEN_TMPL.
8815 [temp.alias]/2 says:
8817 When a template-id refers to the specialization of an
8818 alias template, it is equivalent to the associated
8819 type obtained by substitution of its
8820 template-arguments for the template-parameters in the
8821 type-id of the alias template. */
8823 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8824 /* Note that the call above (by indirectly calling
8825 register_specialization in tsubst_decl) registers the
8826 TYPE_DECL representing the specialization of the alias
8827 template. So next time someone substitutes ARGLIST for
8828 the template parms into the alias template (GEN_TMPL),
8829 she'll get that TYPE_DECL back. */
8831 if (t
== error_mark_node
)
8834 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8836 if (!is_dependent_type
)
8838 set_current_access_from_decl (TYPE_NAME (template_type
));
8839 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8840 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8841 arglist
, complain
, in_decl
),
8842 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8843 arglist
, complain
, in_decl
),
8844 SCOPED_ENUM_P (template_type
), NULL
);
8846 if (t
== error_mark_node
)
8851 /* We don't want to call start_enum for this type, since
8852 the values for the enumeration constants may involve
8853 template parameters. And, no one should be interested
8854 in the enumeration constants for such a type. */
8855 t
= cxx_make_type (ENUMERAL_TYPE
);
8856 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8858 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8859 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8860 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8862 else if (CLASS_TYPE_P (template_type
))
8864 t
= make_class_type (TREE_CODE (template_type
));
8865 CLASSTYPE_DECLARED_CLASS (t
)
8866 = CLASSTYPE_DECLARED_CLASS (template_type
);
8867 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8869 /* A local class. Make sure the decl gets registered properly. */
8870 if (context
== current_function_decl
)
8871 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8873 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8874 /* This instantiation is another name for the primary
8875 template type. Set the TYPE_CANONICAL field
8877 TYPE_CANONICAL (t
) = template_type
;
8878 else if (any_template_arguments_need_structural_equality_p (arglist
))
8879 /* Some of the template arguments require structural
8880 equality testing, so this template class requires
8881 structural equality testing. */
8882 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8887 /* If we called start_enum or pushtag above, this information
8888 will already be set up. */
8891 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8893 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8894 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8895 DECL_SOURCE_LOCATION (type_decl
)
8896 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8899 type_decl
= TYPE_NAME (t
);
8901 if (CLASS_TYPE_P (template_type
))
8903 TREE_PRIVATE (type_decl
)
8904 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8905 TREE_PROTECTED (type_decl
)
8906 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8907 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8909 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8910 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8914 if (OVERLOAD_TYPE_P (t
)
8915 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8917 static const char *tags
[] = {"abi_tag", "may_alias"};
8919 for (unsigned ix
= 0; ix
!= 2; ix
++)
8922 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8926 = tree_cons (TREE_PURPOSE (attributes
),
8927 TREE_VALUE (attributes
),
8928 TYPE_ATTRIBUTES (t
));
8932 /* Let's consider the explicit specialization of a member
8933 of a class template specialization that is implicitly instantiated,
8938 template<class U> struct M {}; //#0
8943 struct S<int>::M<char> //#1
8947 [temp.expl.spec]/4 says this is valid.
8949 In this case, when we write:
8952 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8955 When we encounter #1, we want to store the partial instantiation
8956 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8958 For all cases other than this "explicit specialization of member of a
8959 class template", we just want to store the most general template into
8960 the CLASSTYPE_TI_TEMPLATE of M.
8962 This case of "explicit specialization of member of a class template"
8964 1/ the enclosing class is an instantiation of, and therefore not
8965 the same as, the context of the most general template, and
8966 2/ we aren't looking at the partial instantiation itself, i.e.
8967 the innermost arguments are not the same as the innermost parms of
8968 the most general template.
8970 So it's only when 1/ and 2/ happens that we want to use the partial
8971 instantiation of the member template in lieu of its most general
8974 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8975 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8976 /* the enclosing class must be an instantiation... */
8977 && CLASS_TYPE_P (context
)
8978 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8980 TREE_VEC_LENGTH (arglist
)--;
8981 ++processing_template_decl
;
8982 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
8983 tree partial_inst_args
=
8984 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
8985 arglist
, complain
, NULL_TREE
);
8986 --processing_template_decl
;
8987 TREE_VEC_LENGTH (arglist
)++;
8988 if (partial_inst_args
== error_mark_node
)
8989 return error_mark_node
;
8990 use_partial_inst_tmpl
=
8991 /*...and we must not be looking at the partial instantiation
8993 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8997 if (!use_partial_inst_tmpl
)
8998 /* This case is easy; there are no member templates involved. */
9002 /* This is a full instantiation of a member template. Find
9003 the partial instantiation of which this is an instance. */
9005 /* Temporarily reduce by one the number of levels in the ARGLIST
9006 so as to avoid comparing the last set of arguments. */
9007 TREE_VEC_LENGTH (arglist
)--;
9008 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
9009 TREE_VEC_LENGTH (arglist
)++;
9010 /* FOUND is either a proper class type, or an alias
9011 template specialization. In the later case, it's a
9012 TYPE_DECL, resulting from the substituting of arguments
9013 for parameters in the TYPE_DECL of the alias template
9014 done earlier. So be careful while getting the template
9016 found
= (TREE_CODE (found
) == TEMPLATE_DECL
9018 : (TREE_CODE (found
) == TYPE_DECL
9019 ? DECL_TI_TEMPLATE (found
)
9020 : CLASSTYPE_TI_TEMPLATE (found
)));
9023 // Build template info for the new specialization.
9024 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
9027 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
9028 entry
= ggc_alloc
<spec_entry
> ();
9032 /* Note this use of the partial instantiation so we can check it
9033 later in maybe_process_partial_specialization. */
9034 DECL_TEMPLATE_INSTANTIATIONS (found
)
9035 = tree_cons (arglist
, t
,
9036 DECL_TEMPLATE_INSTANTIATIONS (found
));
9038 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
9039 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
9040 /* Now that the type has been registered on the instantiations
9041 list, we set up the enumerators. Because the enumeration
9042 constants may involve the enumeration type itself, we make
9043 sure to register the type first, and then create the
9044 constants. That way, doing tsubst_expr for the enumeration
9045 constants won't result in recursive calls here; we'll find
9046 the instantiation and exit above. */
9047 tsubst_enum (template_type
, t
, arglist
);
9049 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
9050 /* If the type makes use of template parameters, the
9051 code that generates debugging information will crash. */
9052 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
9054 /* Possibly limit visibility based on template args. */
9055 TREE_PUBLIC (type_decl
) = 1;
9056 determine_visibility (type_decl
);
9058 inherit_targ_abi_tags (t
);
9064 /* Wrapper for lookup_template_class_1. */
9067 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
9068 int entering_scope
, tsubst_flags_t complain
)
9071 timevar_push (TV_TEMPLATE_INST
);
9072 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
9073 entering_scope
, complain
);
9074 timevar_pop (TV_TEMPLATE_INST
);
9078 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9081 lookup_template_variable (tree templ
, tree arglist
)
9083 /* The type of the expression is NULL_TREE since the template-id could refer
9084 to an explicit or partial specialization. */
9085 tree type
= NULL_TREE
;
9086 if (flag_concepts
&& variable_concept_p (templ
))
9087 /* Except that concepts are always bool. */
9088 type
= boolean_type_node
;
9089 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
9092 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9095 finish_template_variable (tree var
, tsubst_flags_t complain
)
9097 tree templ
= TREE_OPERAND (var
, 0);
9098 tree arglist
= TREE_OPERAND (var
, 1);
9100 /* We never want to return a VAR_DECL for a variable concept, since they
9101 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9102 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
9103 if (concept_p
&& processing_template_decl
)
9106 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
9107 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
9109 templ
= most_general_template (templ
);
9110 tree parms
= DECL_TEMPLATE_PARMS (templ
);
9111 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
9113 /*use_default*/true);
9115 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
9117 if (complain
& tf_error
)
9119 error ("use of invalid variable template %qE", var
);
9120 diagnose_constraints (location_of (var
), templ
, arglist
);
9122 return error_mark_node
;
9125 /* If a template-id refers to a specialization of a variable
9126 concept, then the expression is true if and only if the
9127 concept's constraints are satisfied by the given template
9130 NOTE: This is an extension of Concepts Lite TS that
9131 allows constraints to be used in expressions. */
9134 tree decl
= DECL_TEMPLATE_RESULT (templ
);
9135 return evaluate_variable_concept (decl
, arglist
);
9138 return instantiate_template (templ
, arglist
, complain
);
9141 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9142 TARGS template args, and instantiate it if it's not dependent. */
9145 lookup_and_finish_template_variable (tree templ
, tree targs
,
9146 tsubst_flags_t complain
)
9148 templ
= lookup_template_variable (templ
, targs
);
9149 if (!any_dependent_template_arguments_p (targs
))
9151 templ
= finish_template_variable (templ
, complain
);
9155 return convert_from_reference (templ
);
9164 /* True when we should also visit template parameters that occur in
9165 non-deduced contexts. */
9166 bool include_nondeduced_p
;
9167 hash_set
<tree
> *visited
;
9170 /* Called from for_each_template_parm via walk_tree. */
9173 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
9176 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
9177 tree_fn_t fn
= pfd
->fn
;
9178 void *data
= pfd
->data
;
9179 tree result
= NULL_TREE
;
9181 #define WALK_SUBTREE(NODE) \
9184 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9185 pfd->include_nondeduced_p, \
9187 if (result) goto out; \
9191 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
9195 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
9196 WALK_SUBTREE (TYPE_CONTEXT (t
));
9198 switch (TREE_CODE (t
))
9201 if (TYPE_PTRMEMFUNC_P (t
))
9207 if (!TYPE_TEMPLATE_INFO (t
))
9210 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9214 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
9215 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
9219 /* Since we're not going to walk subtrees, we have to do this
9221 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
9225 /* Check the return type. */
9226 WALK_SUBTREE (TREE_TYPE (t
));
9228 /* Check the parameter types. Since default arguments are not
9229 instantiated until they are needed, the TYPE_ARG_TYPES may
9230 contain expressions that involve template parameters. But,
9231 no-one should be looking at them yet. And, once they're
9232 instantiated, they don't contain template parameters, so
9233 there's no point in looking at them then, either. */
9237 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
9238 WALK_SUBTREE (TREE_VALUE (parm
));
9240 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9241 want walk_tree walking into them itself. */
9245 if (flag_noexcept_type
)
9247 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
9249 WALK_SUBTREE (TREE_PURPOSE (spec
));
9254 case UNDERLYING_TYPE
:
9255 if (pfd
->include_nondeduced_p
9256 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
9258 pfd
->include_nondeduced_p
,
9260 return error_mark_node
;
9265 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9266 WALK_SUBTREE (DECL_TI_ARGS (t
));
9271 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
9272 WALK_SUBTREE (DECL_INITIAL (t
));
9273 if (DECL_CONTEXT (t
)
9274 && pfd
->include_nondeduced_p
)
9275 WALK_SUBTREE (DECL_CONTEXT (t
));
9278 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9279 /* Record template parameters such as `T' inside `TT<T>'. */
9280 WALK_SUBTREE (TYPE_TI_ARGS (t
));
9283 case TEMPLATE_TEMPLATE_PARM
:
9284 case TEMPLATE_TYPE_PARM
:
9285 case TEMPLATE_PARM_INDEX
:
9286 if (fn
&& (*fn
)(t
, data
))
9293 /* A template template parameter is encountered. */
9294 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9295 WALK_SUBTREE (TREE_TYPE (t
));
9297 /* Already substituted template template parameter */
9302 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9303 partial instantiation. */
9304 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
9308 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
9309 && pfd
->include_nondeduced_p
)
9310 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
9315 /* If there's no type, then this thing must be some expression
9316 involving template parameters. */
9317 if (!fn
&& !TREE_TYPE (t
))
9318 return error_mark_node
;
9323 case IMPLICIT_CONV_EXPR
:
9324 case REINTERPRET_CAST_EXPR
:
9325 case CONST_CAST_EXPR
:
9326 case STATIC_CAST_EXPR
:
9327 case DYNAMIC_CAST_EXPR
:
9331 case PSEUDO_DTOR_EXPR
:
9333 return error_mark_node
;
9342 /* We didn't find any template parameters we liked. */
9347 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9348 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9349 call FN with the parameter and the DATA.
9350 If FN returns nonzero, the iteration is terminated, and
9351 for_each_template_parm returns 1. Otherwise, the iteration
9352 continues. If FN never returns a nonzero value, the value
9353 returned by for_each_template_parm is 0. If FN is NULL, it is
9354 considered to be the function which always returns 1.
9356 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9357 parameters that occur in non-deduced contexts. When false, only
9358 visits those template parameters that can be deduced. */
9361 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9362 hash_set
<tree
> *visited
,
9363 bool include_nondeduced_p
,
9366 struct pair_fn_data pfd
;
9371 pfd
.any_fn
= any_fn
;
9373 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9375 /* Walk the tree. (Conceptually, we would like to walk without
9376 duplicates, but for_each_template_parm_r recursively calls
9377 for_each_template_parm, so we would need to reorganize a fair
9378 bit to use walk_tree_without_duplicates, so we keep our own
9381 pfd
.visited
= visited
;
9383 pfd
.visited
= new hash_set
<tree
>;
9384 result
= cp_walk_tree (&t
,
9385 for_each_template_parm_r
,
9399 /* Returns true if T depends on any template parameter. */
9402 uses_template_parms (tree t
)
9408 int saved_processing_template_decl
;
9410 saved_processing_template_decl
= processing_template_decl
;
9411 if (!saved_processing_template_decl
)
9412 processing_template_decl
= 1;
9414 dependent_p
= dependent_type_p (t
);
9415 else if (TREE_CODE (t
) == TREE_VEC
)
9416 dependent_p
= any_dependent_template_arguments_p (t
);
9417 else if (TREE_CODE (t
) == TREE_LIST
)
9418 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9419 || uses_template_parms (TREE_CHAIN (t
)));
9420 else if (TREE_CODE (t
) == TYPE_DECL
)
9421 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9424 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9425 || TREE_CODE (t
) == OVERLOAD
9428 || TREE_CODE (t
) == TRAIT_EXPR
9429 || TREE_CODE (t
) == CONSTRUCTOR
9430 || CONSTANT_CLASS_P (t
))
9431 dependent_p
= (type_dependent_expression_p (t
)
9432 || value_dependent_expression_p (t
));
9435 gcc_assert (t
== error_mark_node
);
9436 dependent_p
= false;
9439 processing_template_decl
= saved_processing_template_decl
;
9444 /* Returns true iff current_function_decl is an incompletely instantiated
9445 template. Useful instead of processing_template_decl because the latter
9446 is set to 0 during instantiate_non_dependent_expr. */
9449 in_template_function (void)
9451 tree fn
= current_function_decl
;
9453 ++processing_template_decl
;
9454 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9455 && DECL_TEMPLATE_INFO (fn
)
9456 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9457 --processing_template_decl
;
9461 /* Returns true if T depends on any template parameter with level LEVEL. */
9464 uses_template_parms_level (tree t
, int level
)
9466 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9467 /*include_nondeduced_p=*/true);
9470 /* Returns true if the signature of DECL depends on any template parameter from
9471 its enclosing class. */
9474 uses_outer_template_parms (tree decl
)
9476 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9479 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9480 &depth
, NULL
, /*include_nondeduced_p=*/true))
9482 if (PRIMARY_TEMPLATE_P (decl
)
9483 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9484 (DECL_TEMPLATE_PARMS (decl
)),
9485 template_parm_outer_level
,
9486 &depth
, NULL
, /*include_nondeduced_p=*/true))
9488 tree ci
= get_constraints (decl
);
9490 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9491 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9492 &depth
, NULL
, /*nondeduced*/true))
9497 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9498 ill-formed translation unit, i.e. a variable or function that isn't
9499 usable in a constant expression. */
9502 neglectable_inst_p (tree d
)
9505 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9506 : decl_maybe_constant_var_p (d
)));
9509 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9510 neglectable and instantiated from within an erroneous instantiation. */
9513 limit_bad_template_recursion (tree decl
)
9515 struct tinst_level
*lev
= current_tinst_level
;
9516 int errs
= errorcount
+ sorrycount
;
9517 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9520 for (; lev
; lev
= lev
->next
)
9521 if (neglectable_inst_p (lev
->decl
))
9524 return (lev
&& errs
> lev
->errors
);
9527 static int tinst_depth
;
9528 extern int max_tinst_depth
;
9531 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9533 /* We're starting to instantiate D; record the template instantiation context
9534 for diagnostics and to restore it later. */
9537 push_tinst_level (tree d
)
9539 return push_tinst_level_loc (d
, input_location
);
9542 /* We're starting to instantiate D; record the template instantiation context
9543 at LOC for diagnostics and to restore it later. */
9546 push_tinst_level_loc (tree d
, location_t loc
)
9548 struct tinst_level
*new_level
;
9550 if (tinst_depth
>= max_tinst_depth
)
9552 /* Tell error.c not to try to instantiate any templates. */
9554 fatal_error (input_location
,
9555 "template instantiation depth exceeds maximum of %d"
9556 " (use -ftemplate-depth= to increase the maximum)",
9561 /* If the current instantiation caused problems, don't let it instantiate
9562 anything else. Do allow deduction substitution and decls usable in
9563 constant expressions. */
9564 if (limit_bad_template_recursion (d
))
9567 /* When not -quiet, dump template instantiations other than functions, since
9568 announce_function will take care of those. */
9570 && TREE_CODE (d
) != TREE_LIST
9571 && TREE_CODE (d
) != FUNCTION_DECL
)
9572 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9574 new_level
= ggc_alloc
<tinst_level
> ();
9575 new_level
->decl
= d
;
9576 new_level
->locus
= loc
;
9577 new_level
->errors
= errorcount
+sorrycount
;
9578 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9579 new_level
->next
= current_tinst_level
;
9580 current_tinst_level
= new_level
;
9583 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9584 depth_reached
= tinst_depth
;
9589 /* We're done instantiating this template; return to the instantiation
9593 pop_tinst_level (void)
9595 /* Restore the filename and line number stashed away when we started
9596 this instantiation. */
9597 input_location
= current_tinst_level
->locus
;
9598 current_tinst_level
= current_tinst_level
->next
;
9602 /* We're instantiating a deferred template; restore the template
9603 instantiation context in which the instantiation was requested, which
9604 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9607 reopen_tinst_level (struct tinst_level
*level
)
9609 struct tinst_level
*t
;
9612 for (t
= level
; t
; t
= t
->next
)
9615 current_tinst_level
= level
;
9617 if (current_tinst_level
)
9618 current_tinst_level
->errors
= errorcount
+sorrycount
;
9622 /* Returns the TINST_LEVEL which gives the original instantiation
9625 struct tinst_level
*
9626 outermost_tinst_level (void)
9628 struct tinst_level
*level
= current_tinst_level
;
9631 level
= level
->next
;
9635 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9636 vector of template arguments, as for tsubst.
9638 Returns an appropriate tsubst'd friend declaration. */
9641 tsubst_friend_function (tree decl
, tree args
)
9645 if (TREE_CODE (decl
) == FUNCTION_DECL
9646 && DECL_TEMPLATE_INSTANTIATION (decl
)
9647 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9648 /* This was a friend declared with an explicit template
9649 argument list, e.g.:
9653 to indicate that f was a template instantiation, not a new
9654 function declaration. Now, we have to figure out what
9655 instantiation of what template. */
9657 tree template_id
, arglist
, fns
;
9660 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9662 /* Friend functions are looked up in the containing namespace scope.
9663 We must enter that scope, to avoid finding member functions of the
9664 current class with same name. */
9665 push_nested_namespace (ns
);
9666 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9667 tf_warning_or_error
, NULL_TREE
,
9668 /*integral_constant_expression_p=*/false);
9669 pop_nested_namespace (ns
);
9670 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9671 tf_warning_or_error
, NULL_TREE
);
9672 template_id
= lookup_template_function (fns
, arglist
);
9674 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9675 tmpl
= determine_specialization (template_id
, new_friend
,
9677 /*need_member_template=*/0,
9678 TREE_VEC_LENGTH (args
),
9680 return instantiate_template (tmpl
, new_args
, tf_error
);
9683 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9685 /* The NEW_FRIEND will look like an instantiation, to the
9686 compiler, but is not an instantiation from the point of view of
9687 the language. For example, we might have had:
9689 template <class T> struct S {
9690 template <class U> friend void f(T, U);
9693 Then, in S<int>, template <class U> void f(int, U) is not an
9694 instantiation of anything. */
9695 if (new_friend
== error_mark_node
)
9696 return error_mark_node
;
9698 DECL_USE_TEMPLATE (new_friend
) = 0;
9699 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9701 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9702 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9703 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9706 /* The mangled name for the NEW_FRIEND is incorrect. The function
9707 is not a template instantiation and should not be mangled like
9708 one. Therefore, we forget the mangling here; we'll recompute it
9709 later if we need it. */
9710 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9712 SET_DECL_RTL (new_friend
, NULL
);
9713 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9716 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9719 tree new_friend_template_info
;
9720 tree new_friend_result_template_info
;
9722 int new_friend_is_defn
;
9724 /* We must save some information from NEW_FRIEND before calling
9725 duplicate decls since that function will free NEW_FRIEND if
9727 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9728 new_friend_is_defn
=
9729 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9730 (template_for_substitution (new_friend
)))
9732 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9734 /* This declaration is a `primary' template. */
9735 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9737 new_friend_result_template_info
9738 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9741 new_friend_result_template_info
= NULL_TREE
;
9743 /* Inside pushdecl_namespace_level, we will push into the
9744 current namespace. However, the friend function should go
9745 into the namespace of the template. */
9746 ns
= decl_namespace_context (new_friend
);
9747 push_nested_namespace (ns
);
9748 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9749 pop_nested_namespace (ns
);
9751 if (old_decl
== error_mark_node
)
9752 return error_mark_node
;
9754 if (old_decl
!= new_friend
)
9756 /* This new friend declaration matched an existing
9757 declaration. For example, given:
9759 template <class T> void f(T);
9760 template <class U> class C {
9761 template <class T> friend void f(T) {}
9764 the friend declaration actually provides the definition
9765 of `f', once C has been instantiated for some type. So,
9766 old_decl will be the out-of-class template declaration,
9767 while new_friend is the in-class definition.
9769 But, if `f' was called before this point, the
9770 instantiation of `f' will have DECL_TI_ARGS corresponding
9771 to `T' but not to `U', references to which might appear
9772 in the definition of `f'. Previously, the most general
9773 template for an instantiation of `f' was the out-of-class
9774 version; now it is the in-class version. Therefore, we
9775 run through all specialization of `f', adding to their
9776 DECL_TI_ARGS appropriately. In particular, they need a
9777 new set of outer arguments, corresponding to the
9778 arguments for this class instantiation.
9780 The same situation can arise with something like this:
9783 template <class T> class C {
9787 when `C<int>' is instantiated. Now, `f(int)' is defined
9790 if (!new_friend_is_defn
)
9791 /* On the other hand, if the in-class declaration does
9792 *not* provide a definition, then we don't want to alter
9793 existing definitions. We can just leave everything
9798 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9799 tree new_args
= TI_ARGS (new_friend_template_info
);
9801 /* Overwrite whatever template info was there before, if
9802 any, with the new template information pertaining to
9804 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9806 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9808 /* We should have called reregister_specialization in
9810 gcc_assert (retrieve_specialization (new_template
,
9814 /* Instantiate it if the global has already been used. */
9815 if (DECL_ODR_USED (old_decl
))
9816 instantiate_decl (old_decl
, /*defer_ok=*/true,
9817 /*expl_inst_class_mem_p=*/false);
9823 /* Indicate that the old function template is a partial
9825 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9826 = new_friend_result_template_info
;
9828 gcc_assert (new_template
9829 == most_general_template (new_template
));
9830 gcc_assert (new_template
!= old_decl
);
9832 /* Reassign any specializations already in the hash table
9833 to the new more general template, and add the
9834 additional template args. */
9835 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9839 tree spec
= TREE_VALUE (t
);
9842 elt
.tmpl
= old_decl
;
9843 elt
.args
= DECL_TI_ARGS (spec
);
9844 elt
.spec
= NULL_TREE
;
9846 decl_specializations
->remove_elt (&elt
);
9849 = add_outermost_template_args (new_args
,
9850 DECL_TI_ARGS (spec
));
9852 register_specialization
9853 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9856 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9860 /* The information from NEW_FRIEND has been merged into OLD_DECL
9861 by duplicate_decls. */
9862 new_friend
= old_decl
;
9867 tree context
= DECL_CONTEXT (new_friend
);
9871 template <class T> class C {
9872 template <class U> friend void C1<U>::f (); // case 1
9873 friend void C2<T>::f (); // case 2
9875 we only need to make sure CONTEXT is a complete type for
9876 case 2. To distinguish between the two cases, we note that
9877 CONTEXT of case 1 remains dependent type after tsubst while
9878 this isn't true for case 2. */
9879 ++processing_template_decl
;
9880 dependent_p
= dependent_type_p (context
);
9881 --processing_template_decl
;
9884 && !complete_type_or_else (context
, NULL_TREE
))
9885 return error_mark_node
;
9887 if (COMPLETE_TYPE_P (context
))
9889 tree fn
= new_friend
;
9890 /* do_friend adds the TEMPLATE_DECL for any member friend
9891 template even if it isn't a member template, i.e.
9892 template <class T> friend A<T>::f();
9893 Look through it in that case. */
9894 if (TREE_CODE (fn
) == TEMPLATE_DECL
9895 && !PRIMARY_TEMPLATE_P (fn
))
9896 fn
= DECL_TEMPLATE_RESULT (fn
);
9897 /* Check to see that the declaration is really present, and,
9898 possibly obtain an improved declaration. */
9899 fn
= check_classfn (context
, fn
, NULL_TREE
);
9909 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9910 template arguments, as for tsubst.
9912 Returns an appropriate tsubst'd friend type or error_mark_node on
9916 tsubst_friend_class (tree friend_tmpl
, tree args
)
9922 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9924 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9925 return TREE_TYPE (t
);
9928 context
= CP_DECL_CONTEXT (friend_tmpl
);
9930 if (context
!= global_namespace
)
9932 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9933 push_nested_namespace (context
);
9935 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9938 /* Look for a class template declaration. We look for hidden names
9939 because two friend declarations of the same template are the
9940 same. For example, in:
9943 template <typename> friend class F;
9945 template <typename> struct B {
9946 template <typename> friend class F;
9949 both F templates are the same. */
9950 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9951 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9953 /* But, if we don't find one, it might be because we're in a
9954 situation like this:
9962 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9963 for `S<int>', not the TEMPLATE_DECL. */
9964 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9966 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9967 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9970 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9972 /* The friend template has already been declared. Just
9973 check to see that the declarations match, and install any new
9974 default parameters. We must tsubst the default parameters,
9975 of course. We only need the innermost template parameters
9976 because that is all that redeclare_class_template will look
9978 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9979 > TMPL_ARGS_DEPTH (args
))
9982 location_t saved_input_location
;
9983 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9984 args
, tf_warning_or_error
);
9986 saved_input_location
= input_location
;
9987 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9988 tree cons
= get_constraints (tmpl
);
9989 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
9990 input_location
= saved_input_location
;
9994 friend_type
= TREE_TYPE (tmpl
);
9998 /* The friend template has not already been declared. In this
9999 case, the instantiation of the template class will cause the
10000 injection of this template into the global scope. */
10001 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
10002 if (tmpl
== error_mark_node
)
10003 return error_mark_node
;
10005 /* The new TMPL is not an instantiation of anything, so we
10006 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10007 the new type because that is supposed to be the corresponding
10008 template decl, i.e., TMPL. */
10009 DECL_USE_TEMPLATE (tmpl
) = 0;
10010 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
10011 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
10012 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
10013 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
10015 /* Inject this template into the global scope. */
10016 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
, true));
10019 if (context
!= global_namespace
)
10021 if (TREE_CODE (context
) == NAMESPACE_DECL
)
10022 pop_nested_namespace (context
);
10024 pop_nested_class ();
10027 return friend_type
;
10030 /* Returns zero if TYPE cannot be completed later due to circularity.
10031 Otherwise returns one. */
10034 can_complete_type_without_circularity (tree type
)
10036 if (type
== NULL_TREE
|| type
== error_mark_node
)
10038 else if (COMPLETE_TYPE_P (type
))
10040 else if (TREE_CODE (type
) == ARRAY_TYPE
)
10041 return can_complete_type_without_circularity (TREE_TYPE (type
));
10042 else if (CLASS_TYPE_P (type
)
10043 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
10049 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
10050 tsubst_flags_t
, tree
);
10052 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10053 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10056 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
10057 tsubst_flags_t complain
, tree in_decl
)
10059 gcc_assert (ATTR_IS_DEPENDENT (t
));
10061 tree val
= TREE_VALUE (t
);
10062 if (val
== NULL_TREE
)
10063 /* Nothing to do. */;
10064 else if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
10065 && is_attribute_p ("omp declare simd",
10066 get_attribute_name (t
)))
10068 tree clauses
= TREE_VALUE (val
);
10069 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
10070 complain
, in_decl
);
10071 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
10072 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
10073 tree parms
= DECL_ARGUMENTS (*decl_p
);
10075 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
10077 val
= build_tree_list (NULL_TREE
, clauses
);
10081 /* If the first attribute argument is an identifier, don't
10082 pass it through tsubst. Attributes like mode, format,
10083 cleanup and several target specific attributes expect it
10085 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
10088 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
10089 /*integral_constant_expression_p=*/false);
10090 if (chain
!= TREE_CHAIN (val
))
10091 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
10093 else if (PACK_EXPANSION_P (val
))
10095 /* An attribute pack expansion. */
10096 tree purp
= TREE_PURPOSE (t
);
10097 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
10098 if (pack
== error_mark_node
)
10099 return error_mark_node
;
10100 int len
= TREE_VEC_LENGTH (pack
);
10101 tree list
= NULL_TREE
;
10103 for (int i
= 0; i
< len
; ++i
)
10105 tree elt
= TREE_VEC_ELT (pack
, i
);
10106 *q
= build_tree_list (purp
, elt
);
10107 q
= &TREE_CHAIN (*q
);
10112 val
= tsubst_expr (val
, args
, complain
, in_decl
,
10113 /*integral_constant_expression_p=*/false);
10115 if (val
!= TREE_VALUE (t
))
10116 return build_tree_list (TREE_PURPOSE (t
), val
);
10120 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10121 unchanged or a new TREE_LIST chain. */
10124 tsubst_attributes (tree attributes
, tree args
,
10125 tsubst_flags_t complain
, tree in_decl
)
10127 tree last_dep
= NULL_TREE
;
10129 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
10130 if (ATTR_IS_DEPENDENT (t
))
10133 attributes
= copy_list (attributes
);
10138 for (tree
*p
= &attributes
; *p
; )
10141 if (ATTR_IS_DEPENDENT (t
))
10143 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
10148 p
= &TREE_CHAIN (*p
);
10150 *p
= TREE_CHAIN (t
);
10154 p
= &TREE_CHAIN (*p
);
10160 /* Apply any attributes which had to be deferred until instantiation
10161 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10162 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10165 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
10166 tree args
, tsubst_flags_t complain
, tree in_decl
)
10168 tree last_dep
= NULL_TREE
;
10172 if (attributes
== NULL_TREE
)
10175 if (DECL_P (*decl_p
))
10177 if (TREE_TYPE (*decl_p
) == error_mark_node
)
10179 p
= &DECL_ATTRIBUTES (*decl_p
);
10180 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10181 to our attributes parameter. */
10182 gcc_assert (*p
== attributes
);
10186 p
= &TYPE_ATTRIBUTES (*decl_p
);
10187 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10188 lookup_template_class_1, and should be preserved. */
10189 gcc_assert (*p
!= attributes
);
10191 p
= &TREE_CHAIN (*p
);
10194 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
10195 if (ATTR_IS_DEPENDENT (t
))
10198 attributes
= copy_list (attributes
);
10205 tree late_attrs
= NULL_TREE
;
10206 tree
*q
= &late_attrs
;
10211 if (ATTR_IS_DEPENDENT (t
))
10213 *p
= TREE_CHAIN (t
);
10214 TREE_CHAIN (t
) = NULL_TREE
;
10215 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
10217 q
= &TREE_CHAIN (*q
);
10221 p
= &TREE_CHAIN (t
);
10224 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
10228 /* Perform (or defer) access check for typedefs that were referenced
10229 from within the template TMPL code.
10230 This is a subroutine of instantiate_decl and instantiate_class_template.
10231 TMPL is the template to consider and TARGS is the list of arguments of
10235 perform_typedefs_access_check (tree tmpl
, tree targs
)
10237 location_t saved_location
;
10239 qualified_typedef_usage_t
*iter
;
10242 || (!CLASS_TYPE_P (tmpl
)
10243 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
10246 saved_location
= input_location
;
10247 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
10249 tree type_decl
= iter
->typedef_decl
;
10250 tree type_scope
= iter
->context
;
10252 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
10255 if (uses_template_parms (type_decl
))
10256 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
10257 if (uses_template_parms (type_scope
))
10258 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
10260 /* Make access check error messages point to the location
10261 of the use of the typedef. */
10262 input_location
= iter
->locus
;
10263 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
10264 type_decl
, type_decl
,
10265 tf_warning_or_error
);
10267 input_location
= saved_location
;
10271 instantiate_class_template_1 (tree type
)
10273 tree templ
, args
, pattern
, t
, member
;
10277 unsigned int saved_maximum_field_alignment
;
10280 if (type
== error_mark_node
)
10281 return error_mark_node
;
10283 if (COMPLETE_OR_OPEN_TYPE_P (type
)
10284 || uses_template_parms (type
))
10287 /* Figure out which template is being instantiated. */
10288 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
10289 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
10291 /* Determine what specialization of the original template to
10293 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
10294 if (t
== error_mark_node
)
10296 TYPE_BEING_DEFINED (type
) = 1;
10297 return error_mark_node
;
10301 /* This TYPE is actually an instantiation of a partial
10302 specialization. We replace the innermost set of ARGS with
10303 the arguments appropriate for substitution. For example,
10306 template <class T> struct S {};
10307 template <class T> struct S<T*> {};
10309 and supposing that we are instantiating S<int*>, ARGS will
10310 presently be {int*} -- but we need {int}. */
10311 pattern
= TREE_TYPE (t
);
10312 args
= TREE_PURPOSE (t
);
10316 pattern
= TREE_TYPE (templ
);
10317 args
= CLASSTYPE_TI_ARGS (type
);
10320 /* If the template we're instantiating is incomplete, then clearly
10321 there's nothing we can do. */
10322 if (!COMPLETE_TYPE_P (pattern
))
10325 /* If we've recursively instantiated too many templates, stop. */
10326 if (! push_tinst_level (type
))
10329 /* Now we're really doing the instantiation. Mark the type as in
10330 the process of being defined. */
10331 TYPE_BEING_DEFINED (type
) = 1;
10333 /* We may be in the middle of deferred access check. Disable
10335 push_deferring_access_checks (dk_no_deferred
);
10337 int saved_unevaluated_operand
= cp_unevaluated_operand
;
10338 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
10340 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
10341 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10342 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10343 fn_context
= error_mark_node
;
10345 push_to_top_level ();
10348 cp_unevaluated_operand
= 0;
10349 c_inhibit_evaluation_warnings
= 0;
10351 /* Use #pragma pack from the template context. */
10352 saved_maximum_field_alignment
= maximum_field_alignment
;
10353 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10355 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10357 /* Set the input location to the most specialized template definition.
10358 This is needed if tsubsting causes an error. */
10359 typedecl
= TYPE_MAIN_DECL (pattern
);
10360 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10361 DECL_SOURCE_LOCATION (typedecl
);
10363 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10364 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10365 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10366 CLASSTYPE_NON_AGGREGATE (type
) = CLASSTYPE_NON_AGGREGATE (pattern
);
10367 if (ANON_AGGR_TYPE_P (pattern
))
10368 SET_ANON_AGGR_TYPE_P (type
);
10369 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10371 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10372 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10373 /* Adjust visibility for template arguments. */
10374 determine_visibility (TYPE_MAIN_DECL (type
));
10376 if (CLASS_TYPE_P (type
))
10377 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10379 pbinfo
= TYPE_BINFO (pattern
);
10381 /* We should never instantiate a nested class before its enclosing
10382 class; we need to look up the nested class by name before we can
10383 instantiate it, and that lookup should instantiate the enclosing
10385 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10386 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10388 base_list
= NULL_TREE
;
10389 if (BINFO_N_BASE_BINFOS (pbinfo
))
10395 /* We must enter the scope containing the type, as that is where
10396 the accessibility of types named in dependent bases are
10398 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10400 /* Substitute into each of the bases to determine the actual
10402 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10405 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10406 tree expanded_bases
= NULL_TREE
;
10409 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10412 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10413 args
, tf_error
, NULL_TREE
);
10414 if (expanded_bases
== error_mark_node
)
10417 len
= TREE_VEC_LENGTH (expanded_bases
);
10420 for (idx
= 0; idx
< len
; idx
++)
10422 if (expanded_bases
)
10423 /* Extract the already-expanded base class. */
10424 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10426 /* Substitute to figure out the base class. */
10427 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10430 if (base
== error_mark_node
)
10433 base_list
= tree_cons (access
, base
, base_list
);
10434 if (BINFO_VIRTUAL_P (pbase_binfo
))
10435 TREE_TYPE (base_list
) = integer_type_node
;
10439 /* The list is now in reverse order; correct that. */
10440 base_list
= nreverse (base_list
);
10443 pop_scope (pushed_scope
);
10445 /* Now call xref_basetypes to set up all the base-class
10447 xref_basetypes (type
, base_list
);
10449 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10450 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10451 args
, tf_error
, NULL_TREE
);
10452 fixup_attribute_variants (type
);
10454 /* Now that our base classes are set up, enter the scope of the
10455 class, so that name lookups into base classes, etc. will work
10456 correctly. This is precisely analogous to what we do in
10457 begin_class_definition when defining an ordinary non-template
10458 class, except we also need to push the enclosing classes. */
10459 push_nested_class (type
);
10461 /* Now members are processed in the order of declaration. */
10462 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10463 member
; member
= TREE_CHAIN (member
))
10465 tree t
= TREE_VALUE (member
);
10467 if (TREE_PURPOSE (member
))
10471 /* Build new CLASSTYPE_NESTED_UTDS. */
10474 bool class_template_p
;
10476 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10477 && TYPE_LANG_SPECIFIC (t
)
10478 && CLASSTYPE_IS_TEMPLATE (t
));
10479 /* If the member is a class template, then -- even after
10480 substitution -- there may be dependent types in the
10481 template argument list for the class. We increment
10482 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10483 that function will assume that no types are dependent
10484 when outside of a template. */
10485 if (class_template_p
)
10486 ++processing_template_decl
;
10487 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10488 if (class_template_p
)
10489 --processing_template_decl
;
10490 if (newtag
== error_mark_node
)
10493 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10495 tree name
= TYPE_IDENTIFIER (t
);
10497 if (class_template_p
)
10498 /* Unfortunately, lookup_template_class sets
10499 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10500 instantiation (i.e., for the type of a member
10501 template class nested within a template class.)
10502 This behavior is required for
10503 maybe_process_partial_specialization to work
10504 correctly, but is not accurate in this case;
10505 the TAG is not an instantiation of anything.
10506 (The corresponding TEMPLATE_DECL is an
10507 instantiation, but the TYPE is not.) */
10508 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10510 /* Now, we call pushtag to put this NEWTAG into the scope of
10511 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10512 pushtag calling push_template_decl. We don't have to do
10513 this for enums because it will already have been done in
10516 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10517 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10520 else if (DECL_DECLARES_FUNCTION_P (t
))
10522 /* Build new TYPE_METHODS. */
10525 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10526 ++processing_template_decl
;
10527 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10528 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10529 --processing_template_decl
;
10530 set_current_access_from_decl (r
);
10531 finish_member_declaration (r
);
10532 /* Instantiate members marked with attribute used. */
10533 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10535 if (TREE_CODE (r
) == FUNCTION_DECL
10536 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10537 cp_check_omp_declare_reduction (r
);
10539 else if (DECL_CLASS_TEMPLATE_P (t
)
10540 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10541 /* A closure type for a lambda in a default argument for a
10542 member template. Ignore it; it will be instantiated with
10543 the default argument. */;
10546 /* Build new TYPE_FIELDS. */
10547 if (TREE_CODE (t
) == STATIC_ASSERT
)
10551 ++c_inhibit_evaluation_warnings
;
10553 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10554 tf_warning_or_error
, NULL_TREE
,
10555 /*integral_constant_expression_p=*/true);
10556 --c_inhibit_evaluation_warnings
;
10558 finish_static_assert (condition
,
10559 STATIC_ASSERT_MESSAGE (t
),
10560 STATIC_ASSERT_SOURCE_LOCATION (t
),
10561 /*member_p=*/true);
10563 else if (TREE_CODE (t
) != CONST_DECL
)
10566 tree vec
= NULL_TREE
;
10569 /* The file and line for this declaration, to
10570 assist in error message reporting. Since we
10571 called push_tinst_level above, we don't need to
10573 input_location
= DECL_SOURCE_LOCATION (t
);
10575 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10576 ++processing_template_decl
;
10577 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10578 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10579 --processing_template_decl
;
10581 if (TREE_CODE (r
) == TREE_VEC
)
10583 /* A capture pack became multiple fields. */
10585 len
= TREE_VEC_LENGTH (vec
);
10588 for (int i
= 0; i
< len
; ++i
)
10591 r
= TREE_VEC_ELT (vec
, i
);
10596 [t]he initialization (and any associated
10597 side-effects) of a static data member does
10598 not occur unless the static data member is
10599 itself used in a way that requires the
10600 definition of the static data member to
10603 Therefore, we do not substitute into the
10604 initialized for the static data member here. */
10605 finish_static_data_member_decl
10607 /*init=*/NULL_TREE
,
10608 /*init_const_expr_p=*/false,
10609 /*asmspec_tree=*/NULL_TREE
,
10611 /* Instantiate members marked with attribute used. */
10612 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10615 else if (TREE_CODE (r
) == FIELD_DECL
)
10617 /* Determine whether R has a valid type and can be
10618 completed later. If R is invalid, then its type
10619 is replaced by error_mark_node. */
10620 tree rtype
= TREE_TYPE (r
);
10621 if (can_complete_type_without_circularity (rtype
))
10622 complete_type (rtype
);
10624 if (!complete_or_array_type_p (rtype
))
10626 /* If R's type couldn't be completed and
10627 it isn't a flexible array member (whose
10628 type is incomplete by definition) give
10630 cxx_incomplete_type_error (r
, rtype
);
10631 TREE_TYPE (r
) = error_mark_node
;
10635 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10636 such a thing will already have been added to the field
10637 list by tsubst_enum in finish_member_declaration in the
10638 CLASSTYPE_NESTED_UTDS case above. */
10639 if (!(TREE_CODE (r
) == TYPE_DECL
10640 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10641 && DECL_ARTIFICIAL (r
)))
10643 set_current_access_from_decl (r
);
10644 finish_member_declaration (r
);
10652 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10653 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10655 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10657 tree friend_type
= t
;
10658 bool adjust_processing_template_decl
= false;
10660 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10662 /* template <class T> friend class C; */
10663 friend_type
= tsubst_friend_class (friend_type
, args
);
10664 adjust_processing_template_decl
= true;
10666 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10668 /* template <class T> friend class C::D; */
10669 friend_type
= tsubst (friend_type
, args
,
10670 tf_warning_or_error
, NULL_TREE
);
10671 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10672 friend_type
= TREE_TYPE (friend_type
);
10673 adjust_processing_template_decl
= true;
10675 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10676 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10678 /* This could be either
10682 when dependent_type_p is false or
10684 template <class U> friend class T::C;
10687 /* Bump processing_template_decl in case this is something like
10688 template <class T> friend struct A<T>::B. */
10689 ++processing_template_decl
;
10690 friend_type
= tsubst (friend_type
, args
,
10691 tf_warning_or_error
, NULL_TREE
);
10692 if (dependent_type_p (friend_type
))
10693 adjust_processing_template_decl
= true;
10694 --processing_template_decl
;
10696 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10697 && TYPE_HIDDEN_P (friend_type
))
10701 where C hasn't been declared yet. Let's lookup name
10702 from namespace scope directly, bypassing any name that
10703 come from dependent base class. */
10704 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10706 /* The call to xref_tag_from_type does injection for friend
10708 push_nested_namespace (ns
);
10710 xref_tag_from_type (friend_type
, NULL_TREE
,
10711 /*tag_scope=*/ts_current
);
10712 pop_nested_namespace (ns
);
10714 else if (uses_template_parms (friend_type
))
10715 /* friend class C<T>; */
10716 friend_type
= tsubst (friend_type
, args
,
10717 tf_warning_or_error
, NULL_TREE
);
10722 where C is already declared or
10724 friend class C<int>;
10726 We don't have to do anything in these cases. */
10728 if (adjust_processing_template_decl
)
10729 /* Trick make_friend_class into realizing that the friend
10730 we're adding is a template, not an ordinary class. It's
10731 important that we use make_friend_class since it will
10732 perform some error-checking and output cross-reference
10734 ++processing_template_decl
;
10736 if (friend_type
!= error_mark_node
)
10737 make_friend_class (type
, friend_type
, /*complain=*/false);
10739 if (adjust_processing_template_decl
)
10740 --processing_template_decl
;
10744 /* Build new DECL_FRIENDLIST. */
10747 /* The file and line for this declaration, to
10748 assist in error message reporting. Since we
10749 called push_tinst_level above, we don't need to
10751 input_location
= DECL_SOURCE_LOCATION (t
);
10753 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10755 ++processing_template_decl
;
10756 push_deferring_access_checks (dk_no_check
);
10759 r
= tsubst_friend_function (t
, args
);
10760 add_friend (type
, r
, /*complain=*/false);
10761 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10763 pop_deferring_access_checks ();
10764 --processing_template_decl
;
10772 /* Restore these before substituting into the lambda capture
10774 cp_unevaluated_operand
= saved_unevaluated_operand
;
10775 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10778 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10780 tree decl
= lambda_function (type
);
10783 if (cxx_dialect
>= cxx1z
)
10784 CLASSTYPE_LITERAL_P (type
) = true;
10786 if (!DECL_TEMPLATE_INFO (decl
)
10787 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10789 /* Set function_depth to avoid garbage collection. */
10791 instantiate_decl (decl
, /*defer_ok=*/false, false);
10795 /* We need to instantiate the capture list from the template
10796 after we've instantiated the closure members, but before we
10797 consider adding the conversion op. Also keep any captures
10798 that may have been added during instantiation of the op(). */
10799 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10801 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10802 args
, tf_warning_or_error
, NULL_TREE
,
10805 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10806 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10808 maybe_add_lambda_conv_op (type
);
10811 gcc_assert (errorcount
);
10814 /* Set the file and line number information to whatever is given for
10815 the class itself. This puts error messages involving generated
10816 implicit functions at a predictable point, and the same point
10817 that would be used for non-template classes. */
10818 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10820 unreverse_member_declarations (type
);
10821 finish_struct_1 (type
);
10822 TYPE_BEING_DEFINED (type
) = 0;
10824 /* We don't instantiate default arguments for member functions. 14.7.1:
10826 The implicit instantiation of a class template specialization causes
10827 the implicit instantiation of the declarations, but not of the
10828 definitions or default arguments, of the class member functions,
10829 member classes, static data members and member templates.... */
10831 /* Some typedefs referenced from within the template code need to be access
10832 checked at template instantiation time, i.e now. These types were
10833 added to the template at parsing time. Let's get those and perform
10834 the access checks then. */
10835 perform_typedefs_access_check (pattern
, args
);
10836 perform_deferred_access_checks (tf_warning_or_error
);
10837 pop_nested_class ();
10838 maximum_field_alignment
= saved_maximum_field_alignment
;
10840 pop_from_top_level ();
10841 pop_deferring_access_checks ();
10842 pop_tinst_level ();
10844 /* The vtable for a template class can be emitted in any translation
10845 unit in which the class is instantiated. When there is no key
10846 method, however, finish_struct_1 will already have added TYPE to
10847 the keyed_classes list. */
10848 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10849 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
10854 /* Wrapper for instantiate_class_template_1. */
10857 instantiate_class_template (tree type
)
10860 timevar_push (TV_TEMPLATE_INST
);
10861 ret
= instantiate_class_template_1 (type
);
10862 timevar_pop (TV_TEMPLATE_INST
);
10867 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10873 else if (TYPE_P (t
))
10874 r
= tsubst (t
, args
, complain
, in_decl
);
10877 if (!(complain
& tf_warning
))
10878 ++c_inhibit_evaluation_warnings
;
10879 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10880 /*integral_constant_expression_p=*/true);
10881 if (!(complain
& tf_warning
))
10882 --c_inhibit_evaluation_warnings
;
10887 /* Given a function parameter pack TMPL_PARM and some function parameters
10888 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10889 and set *SPEC_P to point at the next point in the list. */
10892 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10894 /* Collect all of the extra "packed" parameters into an
10897 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10898 tree spec_parm
= *spec_p
;
10901 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10903 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10906 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10907 parmvec
= make_tree_vec (len
);
10908 spec_parm
= *spec_p
;
10909 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10910 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10912 /* Build the argument packs. */
10913 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10914 *spec_p
= spec_parm
;
10919 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10920 NONTYPE_ARGUMENT_PACK. */
10923 make_fnparm_pack (tree spec_parm
)
10925 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10928 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10929 pack expansion with no extra args, 2 if it has extra args, or 0
10930 if it is not a pack expansion. */
10933 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10935 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10936 if (i
>= TREE_VEC_LENGTH (vec
))
10938 tree elt
= TREE_VEC_ELT (vec
, i
);
10940 /* A decl pack is itself an expansion. */
10941 elt
= TREE_TYPE (elt
);
10942 if (!PACK_EXPANSION_P (elt
))
10944 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
10950 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10953 make_argument_pack_select (tree arg_pack
, unsigned index
)
10955 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10957 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10958 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10963 /* This is a subroutine of tsubst_pack_expansion.
10965 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10966 mechanism to store the (non complete list of) arguments of the
10967 substitution and return a non substituted pack expansion, in order
10968 to wait for when we have enough arguments to really perform the
10972 use_pack_expansion_extra_args_p (tree parm_packs
,
10974 bool has_empty_arg
)
10976 /* If one pack has an expansion and another pack has a normal
10977 argument or if one pack has an empty argument and an another
10978 one hasn't then tsubst_pack_expansion cannot perform the
10979 substitution and need to fall back on the
10980 PACK_EXPANSION_EXTRA mechanism. */
10981 if (parm_packs
== NULL_TREE
)
10983 else if (has_empty_arg
)
10986 bool has_expansion_arg
= false;
10987 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
10989 bool has_non_expansion_arg
= false;
10990 for (tree parm_pack
= parm_packs
;
10992 parm_pack
= TREE_CHAIN (parm_pack
))
10994 tree arg
= TREE_VALUE (parm_pack
);
10996 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
10998 /* We can't substitute a pack expansion with extra args into
11002 has_expansion_arg
= true;
11004 has_non_expansion_arg
= true;
11007 if (has_expansion_arg
&& has_non_expansion_arg
)
11013 /* [temp.variadic]/6 says that:
11015 The instantiation of a pack expansion [...]
11016 produces a list E1,E2, ..., En, where N is the number of elements
11017 in the pack expansion parameters.
11019 This subroutine of tsubst_pack_expansion produces one of these Ei.
11021 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11022 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11023 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11024 INDEX is the index 'i' of the element Ei to produce. ARGS,
11025 COMPLAIN, and IN_DECL are the same parameters as for the
11026 tsubst_pack_expansion function.
11028 The function returns the resulting Ei upon successful completion,
11029 or error_mark_node.
11031 Note that this function possibly modifies the ARGS parameter, so
11032 it's the responsibility of the caller to restore it. */
11035 gen_elem_of_pack_expansion_instantiation (tree pattern
,
11038 tree args
/* This parm gets
11040 tsubst_flags_t complain
,
11044 bool ith_elem_is_expansion
= false;
11046 /* For each parameter pack, change the substitution of the parameter
11047 pack to the ith argument in its argument pack, then expand the
11049 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
11051 tree parm
= TREE_PURPOSE (pack
);
11052 tree arg_pack
= TREE_VALUE (pack
);
11053 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
11055 ith_elem_is_expansion
|=
11056 argument_pack_element_is_expansion_p (arg_pack
, index
);
11058 /* Select the Ith argument from the pack. */
11059 if (TREE_CODE (parm
) == PARM_DECL
11060 || TREE_CODE (parm
) == FIELD_DECL
)
11064 aps
= make_argument_pack_select (arg_pack
, index
);
11065 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
11066 return error_mark_node
;
11067 register_local_specialization (aps
, parm
);
11070 aps
= retrieve_local_specialization (parm
);
11075 template_parm_level_and_index (parm
, &level
, &idx
);
11079 aps
= make_argument_pack_select (arg_pack
, index
);
11080 /* Update the corresponding argument. */
11081 TMPL_ARG (args
, level
, idx
) = aps
;
11084 /* Re-use the ARGUMENT_PACK_SELECT. */
11085 aps
= TMPL_ARG (args
, level
, idx
);
11087 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
11090 /* Substitute into the PATTERN with the (possibly altered)
11092 if (pattern
== in_decl
)
11093 /* Expanding a fixed parameter pack from
11094 coerce_template_parameter_pack. */
11095 t
= tsubst_decl (pattern
, args
, complain
);
11096 else if (pattern
== error_mark_node
)
11097 t
= error_mark_node
;
11098 else if (constraint_p (pattern
))
11100 if (processing_template_decl
)
11101 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
11103 t
= (constraints_satisfied_p (pattern
, args
)
11104 ? boolean_true_node
: boolean_false_node
);
11106 else if (!TYPE_P (pattern
))
11107 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11108 /*integral_constant_expression_p=*/false);
11110 t
= tsubst (pattern
, args
, complain
, in_decl
);
11112 /* If the Ith argument pack element is a pack expansion, then
11113 the Ith element resulting from the substituting is going to
11114 be a pack expansion as well. */
11115 if (ith_elem_is_expansion
)
11116 t
= make_pack_expansion (t
);
11121 /* When the unexpanded parameter pack in a fold expression expands to an empty
11122 sequence, the value of the expression is as follows; the program is
11123 ill-formed if the operator is not listed in this table.
11130 expand_empty_fold (tree t
, tsubst_flags_t complain
)
11132 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
11133 if (!FOLD_EXPR_MODIFY_P (t
))
11136 case TRUTH_ANDIF_EXPR
:
11137 return boolean_true_node
;
11138 case TRUTH_ORIF_EXPR
:
11139 return boolean_false_node
;
11140 case COMPOUND_EXPR
:
11146 if (complain
& tf_error
)
11147 error_at (location_of (t
),
11148 "fold of empty expansion over %O", code
);
11149 return error_mark_node
;
11152 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11153 form an expression that combines the two terms using the
11157 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
11159 tree op
= FOLD_EXPR_OP (t
);
11160 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
11162 // Handle compound assignment operators.
11163 if (FOLD_EXPR_MODIFY_P (t
))
11164 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
11168 case COMPOUND_EXPR
:
11169 return build_x_compound_expr (input_location
, left
, right
, complain
);
11171 return build_m_component_ref (left
, right
, complain
);
11173 return build_x_binary_op (input_location
, code
,
11174 left
, TREE_CODE (left
),
11175 right
, TREE_CODE (right
),
11181 /* Substitute ARGS into the pack of a fold expression T. */
11184 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11186 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
11189 /* Substitute ARGS into the pack of a fold expression T. */
11192 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11194 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
11197 /* Expand a PACK of arguments into a grouped as left fold.
11198 Given a pack containing elements A0, A1, ..., An and an
11199 operator @, this builds the expression:
11201 ((A0 @ A1) @ A2) ... @ An
11203 Note that PACK must not be empty.
11205 The operator is defined by the original fold expression T. */
11208 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11210 tree left
= TREE_VEC_ELT (pack
, 0);
11211 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
11213 tree right
= TREE_VEC_ELT (pack
, i
);
11214 left
= fold_expression (t
, left
, right
, complain
);
11219 /* Substitute into a unary left fold expression. */
11222 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11225 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11226 if (pack
== error_mark_node
)
11227 return error_mark_node
;
11228 if (PACK_EXPANSION_P (pack
))
11230 tree r
= copy_node (t
);
11231 FOLD_EXPR_PACK (r
) = pack
;
11234 if (TREE_VEC_LENGTH (pack
) == 0)
11235 return expand_empty_fold (t
, complain
);
11237 return expand_left_fold (t
, pack
, complain
);
11240 /* Substitute into a binary left fold expression.
11242 Do ths by building a single (non-empty) vector of argumnts and
11243 building the expression from those elements. */
11246 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
11249 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11250 if (pack
== error_mark_node
)
11251 return error_mark_node
;
11252 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11253 if (init
== error_mark_node
)
11254 return error_mark_node
;
11256 if (PACK_EXPANSION_P (pack
))
11258 tree r
= copy_node (t
);
11259 FOLD_EXPR_PACK (r
) = pack
;
11260 FOLD_EXPR_INIT (r
) = init
;
11264 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
11265 TREE_VEC_ELT (vec
, 0) = init
;
11266 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
11267 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
11269 return expand_left_fold (t
, vec
, complain
);
11272 /* Expand a PACK of arguments into a grouped as right fold.
11273 Given a pack containing elementns A0, A1, ..., and an
11274 operator @, this builds the expression:
11276 A0@ ... (An-2 @ (An-1 @ An))
11278 Note that PACK must not be empty.
11280 The operator is defined by the original fold expression T. */
11283 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
11285 // Build the expression.
11286 int n
= TREE_VEC_LENGTH (pack
);
11287 tree right
= TREE_VEC_ELT (pack
, n
- 1);
11288 for (--n
; n
!= 0; --n
)
11290 tree left
= TREE_VEC_ELT (pack
, n
- 1);
11291 right
= fold_expression (t
, left
, right
, complain
);
11296 /* Substitute into a unary right fold expression. */
11299 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11302 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11303 if (pack
== error_mark_node
)
11304 return error_mark_node
;
11305 if (PACK_EXPANSION_P (pack
))
11307 tree r
= copy_node (t
);
11308 FOLD_EXPR_PACK (r
) = pack
;
11311 if (TREE_VEC_LENGTH (pack
) == 0)
11312 return expand_empty_fold (t
, complain
);
11314 return expand_right_fold (t
, pack
, complain
);
11317 /* Substitute into a binary right fold expression.
11319 Do ths by building a single (non-empty) vector of arguments and
11320 building the expression from those elements. */
11323 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
11326 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
11327 if (pack
== error_mark_node
)
11328 return error_mark_node
;
11329 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
11330 if (init
== error_mark_node
)
11331 return error_mark_node
;
11333 if (PACK_EXPANSION_P (pack
))
11335 tree r
= copy_node (t
);
11336 FOLD_EXPR_PACK (r
) = pack
;
11337 FOLD_EXPR_INIT (r
) = init
;
11341 int n
= TREE_VEC_LENGTH (pack
);
11342 tree vec
= make_tree_vec (n
+ 1);
11343 for (int i
= 0; i
< n
; ++i
)
11344 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11345 TREE_VEC_ELT (vec
, n
) = init
;
11347 return expand_right_fold (t
, vec
, complain
);
11351 /* Substitute ARGS into T, which is an pack expansion
11352 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11353 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11354 (if only a partial substitution could be performed) or
11355 ERROR_MARK_NODE if there was an error. */
11357 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11361 tree pack
, packs
= NULL_TREE
;
11362 bool unsubstituted_packs
= false;
11365 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11366 bool need_local_specializations
= false;
11369 gcc_assert (PACK_EXPANSION_P (t
));
11370 pattern
= PACK_EXPANSION_PATTERN (t
);
11372 /* Add in any args remembered from an earlier partial instantiation. */
11373 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11375 levels
= TMPL_ARGS_DEPTH (args
);
11377 /* Determine the argument packs that will instantiate the parameter
11378 packs used in the expansion expression. While we're at it,
11379 compute the number of arguments to be expanded and make sure it
11381 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11382 pack
= TREE_CHAIN (pack
))
11384 tree parm_pack
= TREE_VALUE (pack
);
11385 tree arg_pack
= NULL_TREE
;
11386 tree orig_arg
= NULL_TREE
;
11389 if (TREE_CODE (parm_pack
) == BASES
)
11391 gcc_assert (parm_pack
== pattern
);
11392 if (BASES_DIRECT (parm_pack
))
11393 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11394 args
, complain
, in_decl
, false));
11396 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11397 args
, complain
, in_decl
, false));
11399 else if (builtin_pack_call_p (parm_pack
))
11401 /* ??? Support use in other patterns. */
11402 gcc_assert (parm_pack
== pattern
);
11403 return expand_builtin_pack_call (parm_pack
, args
,
11404 complain
, in_decl
);
11406 else if (TREE_CODE (parm_pack
) == PARM_DECL
)
11408 /* We know we have correct local_specializations if this
11409 expansion is at function scope, or if we're dealing with a
11410 local parameter in a requires expression; for the latter,
11411 tsubst_requires_expr set it up appropriately. */
11412 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11413 arg_pack
= retrieve_local_specialization (parm_pack
);
11415 /* We can't rely on local_specializations for a parameter
11416 name used later in a function declaration (such as in a
11417 late-specified return type). Even if it exists, it might
11418 have the wrong value for a recursive call. */
11419 need_local_specializations
= true;
11423 /* This parameter pack was used in an unevaluated context. Just
11424 make a dummy decl, since it's only used for its type. */
11425 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11426 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11427 /* Partial instantiation of the parm_pack, we can't build
11428 up an argument pack yet. */
11429 arg_pack
= NULL_TREE
;
11431 arg_pack
= make_fnparm_pack (arg_pack
);
11434 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11435 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11439 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11441 if (level
<= levels
)
11442 arg_pack
= TMPL_ARG (args
, level
, idx
);
11445 orig_arg
= arg_pack
;
11446 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11447 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11449 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11450 /* This can only happen if we forget to expand an argument
11451 pack somewhere else. Just return an error, silently. */
11453 result
= make_tree_vec (1);
11454 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11461 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11463 /* Don't bother trying to do a partial substitution with
11464 incomplete packs; we'll try again after deduction. */
11465 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11470 else if (len
!= my_len
)
11472 if (!(complain
& tf_error
))
11473 /* Fail quietly. */;
11474 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11475 error ("mismatched argument pack lengths while expanding %qT",
11478 error ("mismatched argument pack lengths while expanding %qE",
11480 return error_mark_node
;
11483 /* Keep track of the parameter packs and their corresponding
11485 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11486 TREE_TYPE (packs
) = orig_arg
;
11490 /* We can't substitute for this parameter pack. We use a flag as
11491 well as the missing_level counter because function parameter
11492 packs don't have a level. */
11493 gcc_assert (processing_template_decl
);
11494 unsubstituted_packs
= true;
11498 /* If the expansion is just T..., return the matching argument pack, unless
11499 we need to call convert_from_reference on all the elements. This is an
11500 important optimization; see c++/68422. */
11501 if (!unsubstituted_packs
11502 && TREE_PURPOSE (packs
) == pattern
)
11504 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11505 /* Types need no adjustment, nor does sizeof..., and if we still have
11506 some pack expansion args we won't do anything yet. */
11507 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11508 || PACK_EXPANSION_SIZEOF_P (t
)
11509 || pack_expansion_args_count (args
))
11511 /* Also optimize expression pack expansions if we can tell that the
11512 elements won't have reference type. */
11513 tree type
= TREE_TYPE (pattern
);
11514 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11515 && !PACK_EXPANSION_P (type
)
11516 && !WILDCARD_TYPE_P (type
))
11518 /* Otherwise use the normal path so we get convert_from_reference. */
11521 /* We cannot expand this expansion expression, because we don't have
11522 all of the argument packs we need. */
11523 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11525 /* We got some full packs, but we can't substitute them in until we
11526 have values for all the packs. So remember these until then. */
11528 t
= make_pack_expansion (pattern
);
11529 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11532 else if (unsubstituted_packs
)
11534 /* There were no real arguments, we're just replacing a parameter
11535 pack with another version of itself. Substitute into the
11536 pattern and return a PACK_EXPANSION_*. The caller will need to
11538 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11539 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11540 /*integral_constant_expression_p=*/false);
11542 t
= tsubst (pattern
, args
, complain
, in_decl
);
11543 t
= make_pack_expansion (t
);
11547 gcc_assert (len
>= 0);
11549 if (need_local_specializations
)
11551 /* We're in a late-specified return type, so create our own local
11552 specializations map; the current map is either NULL or (in the
11553 case of recursive unification) might have bindings that we don't
11554 want to use or alter. */
11555 saved_local_specializations
= local_specializations
;
11556 local_specializations
= new hash_map
<tree
, tree
>;
11559 /* For each argument in each argument pack, substitute into the
11561 result
= make_tree_vec (len
);
11562 tree elem_args
= copy_template_args (args
);
11563 for (i
= 0; i
< len
; ++i
)
11565 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11567 elem_args
, complain
,
11569 TREE_VEC_ELT (result
, i
) = t
;
11570 if (t
== error_mark_node
)
11572 result
= error_mark_node
;
11577 /* Update ARGS to restore the substitution from parameter packs to
11578 their argument packs. */
11579 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11581 tree parm
= TREE_PURPOSE (pack
);
11583 if (TREE_CODE (parm
) == PARM_DECL
11584 || TREE_CODE (parm
) == FIELD_DECL
)
11585 register_local_specialization (TREE_TYPE (pack
), parm
);
11590 if (TREE_VALUE (pack
) == NULL_TREE
)
11593 template_parm_level_and_index (parm
, &level
, &idx
);
11595 /* Update the corresponding argument. */
11596 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11597 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11600 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11604 if (need_local_specializations
)
11606 delete local_specializations
;
11607 local_specializations
= saved_local_specializations
;
11610 /* If the dependent pack arguments were such that we end up with only a
11611 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11612 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11613 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11614 return TREE_VEC_ELT (result
, 0);
11619 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11620 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11621 parameter packs; all parms generated from a function parameter pack will
11622 have the same DECL_PARM_INDEX. */
11625 get_pattern_parm (tree parm
, tree tmpl
)
11627 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11630 if (DECL_ARTIFICIAL (parm
))
11632 for (patparm
= DECL_ARGUMENTS (pattern
);
11633 patparm
; patparm
= DECL_CHAIN (patparm
))
11634 if (DECL_ARTIFICIAL (patparm
)
11635 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11640 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11641 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11642 gcc_assert (DECL_PARM_INDEX (patparm
)
11643 == DECL_PARM_INDEX (parm
));
11649 /* Make an argument pack out of the TREE_VEC VEC. */
11652 make_argument_pack (tree vec
)
11655 tree elt
= TREE_VEC_ELT (vec
, 0);
11657 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11660 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11661 TREE_CONSTANT (pack
) = 1;
11663 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11667 /* Return an exact copy of template args T that can be modified
11671 copy_template_args (tree t
)
11673 if (t
== error_mark_node
)
11676 int len
= TREE_VEC_LENGTH (t
);
11677 tree new_vec
= make_tree_vec (len
);
11679 for (int i
= 0; i
< len
; ++i
)
11681 tree elt
= TREE_VEC_ELT (t
, i
);
11682 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11683 elt
= copy_template_args (elt
);
11684 TREE_VEC_ELT (new_vec
, i
) = elt
;
11687 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11688 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11693 /* Substitute ARGS into the vector or list of template arguments T. */
11696 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11699 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11702 if (t
== error_mark_node
)
11703 return error_mark_node
;
11705 len
= TREE_VEC_LENGTH (t
);
11706 elts
= XALLOCAVEC (tree
, len
);
11708 for (i
= 0; i
< len
; i
++)
11710 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11713 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11714 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11715 else if (PACK_EXPANSION_P (orig_arg
))
11717 /* Substitute into an expansion expression. */
11718 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11720 if (TREE_CODE (new_arg
) == TREE_VEC
)
11721 /* Add to the expanded length adjustment the number of
11722 expanded arguments. We subtract one from this
11723 measurement, because the argument pack expression
11724 itself is already counted as 1 in
11725 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11726 the argument pack is empty. */
11727 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11729 else if (ARGUMENT_PACK_P (orig_arg
))
11731 /* Substitute into each of the arguments. */
11732 new_arg
= TYPE_P (orig_arg
)
11733 ? cxx_make_type (TREE_CODE (orig_arg
))
11734 : make_node (TREE_CODE (orig_arg
));
11736 tree pack_args
= tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11737 args
, complain
, in_decl
);
11738 if (pack_args
== error_mark_node
)
11739 new_arg
= error_mark_node
;
11741 SET_ARGUMENT_PACK_ARGS (new_arg
, pack_args
);
11743 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
)
11744 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11747 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11749 if (new_arg
== error_mark_node
)
11750 return error_mark_node
;
11753 if (new_arg
!= orig_arg
)
11760 /* Make space for the expanded arguments coming from template
11762 t
= make_tree_vec (len
+ expanded_len_adjust
);
11763 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11764 arguments for a member template.
11765 In that case each TREE_VEC in ORIG_T represents a level of template
11766 arguments, and ORIG_T won't carry any non defaulted argument count.
11767 It will rather be the nested TREE_VECs that will carry one.
11768 In other words, ORIG_T carries a non defaulted argument count only
11769 if it doesn't contain any nested TREE_VEC. */
11770 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11772 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11773 count
+= expanded_len_adjust
;
11774 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11776 for (i
= 0, out
= 0; i
< len
; i
++)
11778 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11779 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11780 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11784 /* Now expand the template argument pack "in place". */
11785 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11786 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11790 TREE_VEC_ELT (t
, out
) = elts
[i
];
11798 /* Substitute ARGS into one level PARMS of template parameters. */
11801 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11803 if (parms
== error_mark_node
)
11804 return error_mark_node
;
11806 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11808 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11810 tree tuple
= TREE_VEC_ELT (parms
, i
);
11812 if (tuple
== error_mark_node
)
11815 TREE_VEC_ELT (new_vec
, i
) =
11816 tsubst_template_parm (tuple
, args
, complain
);
11822 /* Return the result of substituting ARGS into the template parameters
11823 given by PARMS. If there are m levels of ARGS and m + n levels of
11824 PARMS, then the result will contain n levels of PARMS. For
11825 example, if PARMS is `template <class T> template <class U>
11826 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11827 result will be `template <int*, double, class V>'. */
11830 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11832 tree r
= NULL_TREE
;
11835 /* When substituting into a template, we must set
11836 PROCESSING_TEMPLATE_DECL as the template parameters may be
11837 dependent if they are based on one-another, and the dependency
11838 predicates are short-circuit outside of templates. */
11839 ++processing_template_decl
;
11841 for (new_parms
= &r
;
11842 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11843 new_parms
= &(TREE_CHAIN (*new_parms
)),
11844 parms
= TREE_CHAIN (parms
))
11846 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11849 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11850 - TMPL_ARGS_DEPTH (args
)),
11851 new_vec
, NULL_TREE
);
11854 --processing_template_decl
;
11859 /* Return the result of substituting ARGS into one template parameter
11860 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11861 parameter and which TREE_PURPOSE is the default argument of the
11862 template parameter. */
11865 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11867 tree default_value
, parm_decl
;
11869 if (args
== NULL_TREE
11871 || t
== error_mark_node
)
11874 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11876 default_value
= TREE_PURPOSE (t
);
11877 parm_decl
= TREE_VALUE (t
);
11879 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11880 if (TREE_CODE (parm_decl
) == PARM_DECL
11881 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11882 parm_decl
= error_mark_node
;
11883 default_value
= tsubst_template_arg (default_value
, args
,
11884 complain
, NULL_TREE
);
11886 return build_tree_list (default_value
, parm_decl
);
11889 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11890 type T. If T is not an aggregate or enumeration type, it is
11891 handled as if by tsubst. IN_DECL is as for tsubst. If
11892 ENTERING_SCOPE is nonzero, T is the context for a template which
11893 we are presently tsubst'ing. Return the substituted value. */
11896 tsubst_aggr_type (tree t
,
11898 tsubst_flags_t complain
,
11900 int entering_scope
)
11902 if (t
== NULL_TREE
)
11905 switch (TREE_CODE (t
))
11908 if (TYPE_PTRMEMFUNC_P (t
))
11909 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11911 /* Fall through. */
11912 case ENUMERAL_TYPE
:
11914 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11919 int saved_unevaluated_operand
;
11920 int saved_inhibit_evaluation_warnings
;
11922 /* In "sizeof(X<I>)" we need to evaluate "I". */
11923 saved_unevaluated_operand
= cp_unevaluated_operand
;
11924 cp_unevaluated_operand
= 0;
11925 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11926 c_inhibit_evaluation_warnings
= 0;
11928 /* First, determine the context for the type we are looking
11930 context
= TYPE_CONTEXT (t
);
11931 if (context
&& TYPE_P (context
))
11933 context
= tsubst_aggr_type (context
, args
, complain
,
11934 in_decl
, /*entering_scope=*/1);
11935 /* If context is a nested class inside a class template,
11936 it may still need to be instantiated (c++/33959). */
11937 context
= complete_type (context
);
11940 /* Then, figure out what arguments are appropriate for the
11941 type we are trying to find. For example, given:
11943 template <class T> struct S;
11944 template <class T, class U> void f(T, U) { S<U> su; }
11946 and supposing that we are instantiating f<int, double>,
11947 then our ARGS will be {int, double}, but, when looking up
11948 S we only want {double}. */
11949 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11950 complain
, in_decl
);
11951 if (argvec
== error_mark_node
)
11952 r
= error_mark_node
;
11955 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11956 entering_scope
, complain
);
11957 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11960 cp_unevaluated_operand
= saved_unevaluated_operand
;
11961 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11966 /* This is not a template type, so there's nothing to do. */
11970 return tsubst (t
, args
, complain
, in_decl
);
11974 /* Substitute into the default argument ARG (a default argument for
11975 FN), which has the indicated TYPE. */
11978 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
11980 tree saved_class_ptr
= NULL_TREE
;
11981 tree saved_class_ref
= NULL_TREE
;
11982 int errs
= errorcount
+ sorrycount
;
11984 /* This can happen in invalid code. */
11985 if (TREE_CODE (arg
) == DEFAULT_ARG
)
11988 /* This default argument came from a template. Instantiate the
11989 default argument here, not in tsubst. In the case of
11998 we must be careful to do name lookup in the scope of S<T>,
11999 rather than in the current class. */
12000 push_access_scope (fn
);
12001 /* The "this" pointer is not valid in a default argument. */
12004 saved_class_ptr
= current_class_ptr
;
12005 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
12006 saved_class_ref
= current_class_ref
;
12007 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
12010 push_deferring_access_checks(dk_no_deferred
);
12011 /* The default argument expression may cause implicitly defined
12012 member functions to be synthesized, which will result in garbage
12013 collection. We must treat this situation as if we were within
12014 the body of function so as to avoid collecting live data on the
12017 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
12018 complain
, NULL_TREE
,
12019 /*integral_constant_expression_p=*/false);
12021 pop_deferring_access_checks();
12023 /* Restore the "this" pointer. */
12026 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
12027 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
12030 if (errorcount
+sorrycount
> errs
12031 && (complain
& tf_warning_or_error
))
12032 inform (input_location
,
12033 " when instantiating default argument for call to %qD", fn
);
12035 /* Make sure the default argument is reasonable. */
12036 arg
= check_default_argument (type
, arg
, complain
);
12038 pop_access_scope (fn
);
12043 /* Substitute into all the default arguments for FN. */
12046 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
12051 tmpl_args
= DECL_TI_ARGS (fn
);
12053 /* If this function is not yet instantiated, we certainly don't need
12054 its default arguments. */
12055 if (uses_template_parms (tmpl_args
))
12057 /* Don't do this again for clones. */
12058 if (DECL_CLONED_FUNCTION_P (fn
))
12061 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
12063 arg
= TREE_CHAIN (arg
))
12064 if (TREE_PURPOSE (arg
))
12065 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
12067 TREE_PURPOSE (arg
),
12071 /* Substitute the ARGS into the T, which is a _DECL. Return the
12072 result of the substitution. Issue error and warning messages under
12073 control of COMPLAIN. */
12076 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
12078 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12079 location_t saved_loc
;
12080 tree r
= NULL_TREE
;
12082 hashval_t hash
= 0;
12084 /* Set the filename and linenumber to improve error-reporting. */
12085 saved_loc
= input_location
;
12086 input_location
= DECL_SOURCE_LOCATION (t
);
12088 switch (TREE_CODE (t
))
12090 case TEMPLATE_DECL
:
12092 /* We can get here when processing a member function template,
12093 member class template, or template template parameter. */
12094 tree decl
= DECL_TEMPLATE_RESULT (t
);
12099 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12101 /* Template template parameter is treated here. */
12102 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12103 if (new_type
== error_mark_node
)
12104 r
= error_mark_node
;
12105 /* If we get a real template back, return it. This can happen in
12106 the context of most_specialized_partial_spec. */
12107 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
12110 /* The new TEMPLATE_DECL was built in
12111 reduce_template_parm_level. */
12112 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
12116 /* We might already have an instance of this template.
12117 The ARGS are for the surrounding class type, so the
12118 full args contain the tsubst'd args for the context,
12119 plus the innermost args from the template decl. */
12120 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
12121 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
12122 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
12123 /* Because this is a template, the arguments will still be
12124 dependent, even after substitution. If
12125 PROCESSING_TEMPLATE_DECL is not set, the dependency
12126 predicates will short-circuit. */
12127 ++processing_template_decl
;
12128 full_args
= tsubst_template_args (tmpl_args
, args
,
12129 complain
, in_decl
);
12130 --processing_template_decl
;
12131 if (full_args
== error_mark_node
)
12132 RETURN (error_mark_node
);
12134 /* If this is a default template template argument,
12135 tsubst might not have changed anything. */
12136 if (full_args
== tmpl_args
)
12139 hash
= hash_tmpl_and_args (t
, full_args
);
12140 spec
= retrieve_specialization (t
, full_args
, hash
);
12141 if (spec
!= NULL_TREE
)
12147 /* Make a new template decl. It will be similar to the
12148 original, but will record the current template arguments.
12149 We also create a new function declaration, which is just
12150 like the old one, but points to this new template, rather
12151 than the old one. */
12153 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
12154 DECL_CHAIN (r
) = NULL_TREE
;
12156 // Build new template info linking to the original template decl.
12157 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12159 if (TREE_CODE (decl
) == TYPE_DECL
12160 && !TYPE_DECL_ALIAS_P (decl
))
12163 ++processing_template_decl
;
12164 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12165 --processing_template_decl
;
12166 if (new_type
== error_mark_node
)
12167 RETURN (error_mark_node
);
12169 TREE_TYPE (r
) = new_type
;
12170 /* For a partial specialization, we need to keep pointing to
12171 the primary template. */
12172 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
12173 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
12174 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
12175 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
12176 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
12181 ++processing_template_decl
;
12182 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
12183 --processing_template_decl
;
12184 if (new_decl
== error_mark_node
)
12185 RETURN (error_mark_node
);
12187 DECL_TEMPLATE_RESULT (r
) = new_decl
;
12188 DECL_TI_TEMPLATE (new_decl
) = r
;
12189 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
12190 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
12191 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
12194 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12195 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
12196 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
12198 /* The template parameters for this new template are all the
12199 template parameters for the old template, except the
12200 outermost level of parameters. */
12201 DECL_TEMPLATE_PARMS (r
)
12202 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
12205 if (PRIMARY_TEMPLATE_P (t
))
12206 DECL_PRIMARY_TEMPLATE (r
) = r
;
12208 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
12209 /* Record this non-type partial instantiation. */
12210 register_specialization (r
, t
,
12211 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
12216 case FUNCTION_DECL
:
12219 tree argvec
= NULL_TREE
;
12227 /* Nobody should be tsubst'ing into non-template functions. */
12228 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
12230 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
12234 /* If T is not dependent, just return it. */
12235 if (!uses_template_parms (DECL_TI_ARGS (t
)))
12238 /* Calculate the most general template of which R is a
12239 specialization, and the complete set of arguments used to
12241 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
12242 argvec
= tsubst_template_args (DECL_TI_ARGS
12243 (DECL_TEMPLATE_RESULT
12244 (DECL_TI_TEMPLATE (t
))),
12245 args
, complain
, in_decl
);
12246 if (argvec
== error_mark_node
)
12247 RETURN (error_mark_node
);
12249 /* Check to see if we already have this specialization. */
12250 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12251 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12259 /* We can see more levels of arguments than parameters if
12260 there was a specialization of a member template, like
12263 template <class T> struct S { template <class U> void f(); }
12264 template <> template <class U> void S<int>::f(U);
12266 Here, we'll be substituting into the specialization,
12267 because that's where we can find the code we actually
12268 want to generate, but we'll have enough arguments for
12269 the most general template.
12271 We also deal with the peculiar case:
12273 template <class T> struct S {
12274 template <class U> friend void f();
12276 template <class U> void f() {}
12278 template void f<double>();
12280 Here, the ARGS for the instantiation of will be {int,
12281 double}. But, we only need as many ARGS as there are
12282 levels of template parameters in CODE_PATTERN. We are
12283 careful not to get fooled into reducing the ARGS in
12286 template <class T> struct S { template <class U> void f(U); }
12287 template <class T> template <> void S<T>::f(int) {}
12289 which we can spot because the pattern will be a
12290 specialization in this case. */
12291 args_depth
= TMPL_ARGS_DEPTH (args
);
12293 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
12294 if (args_depth
> parms_depth
12295 && !DECL_TEMPLATE_SPECIALIZATION (t
))
12296 args
= get_innermost_template_args (args
, parms_depth
);
12300 /* This special case arises when we have something like this:
12302 template <class T> struct S {
12303 friend void f<int>(int, double);
12306 Here, the DECL_TI_TEMPLATE for the friend declaration
12307 will be an IDENTIFIER_NODE. We are being called from
12308 tsubst_friend_function, and we want only to create a
12309 new decl (R) with appropriate types so that we can call
12310 determine_specialization. */
12311 gen_tmpl
= NULL_TREE
;
12314 if (DECL_CLASS_SCOPE_P (t
))
12316 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
12320 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
12321 complain
, t
, /*entering_scope=*/1);
12326 ctx
= DECL_CONTEXT (t
);
12328 type
= tsubst (TREE_TYPE (t
), args
, complain
|tf_fndecl_type
, in_decl
);
12329 if (type
== error_mark_node
)
12330 RETURN (error_mark_node
);
12332 /* If we hit excessive deduction depth, the type is bogus even if
12333 it isn't error_mark_node, so don't build a decl. */
12334 if (excessive_deduction_depth
)
12335 RETURN (error_mark_node
);
12337 /* We do NOT check for matching decls pushed separately at this
12338 point, as they may not represent instantiations of this
12339 template, and in any case are considered separate under the
12342 DECL_USE_TEMPLATE (r
) = 0;
12343 TREE_TYPE (r
) = type
;
12344 /* Clear out the mangled name and RTL for the instantiation. */
12345 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12346 SET_DECL_RTL (r
, NULL
);
12347 /* Leave DECL_INITIAL set on deleted instantiations. */
12348 if (!DECL_DELETED_FN (r
))
12349 DECL_INITIAL (r
) = NULL_TREE
;
12350 DECL_CONTEXT (r
) = ctx
;
12352 /* OpenMP UDRs have the only argument a reference to the declared
12353 type. We want to diagnose if the declared type is a reference,
12354 which is invalid, but as references to references are usually
12355 quietly merged, diagnose it here. */
12356 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12359 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12360 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12361 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12362 error_at (DECL_SOURCE_LOCATION (t
),
12363 "reference type %qT in "
12364 "%<#pragma omp declare reduction%>", argtype
);
12365 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12366 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12370 if (member
&& DECL_CONV_FN_P (r
))
12371 /* Type-conversion operator. Reconstruct the name, in
12372 case it's the name of one of the template's parameters. */
12373 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
12375 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
12377 DECL_RESULT (r
) = NULL_TREE
;
12379 TREE_STATIC (r
) = 0;
12380 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12381 DECL_EXTERNAL (r
) = 1;
12382 /* If this is an instantiation of a function with internal
12383 linkage, we already know what object file linkage will be
12384 assigned to the instantiation. */
12385 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12386 DECL_DEFER_OUTPUT (r
) = 0;
12387 DECL_CHAIN (r
) = NULL_TREE
;
12388 DECL_PENDING_INLINE_INFO (r
) = 0;
12389 DECL_PENDING_INLINE_P (r
) = 0;
12390 DECL_SAVED_TREE (r
) = NULL_TREE
;
12391 DECL_STRUCT_FUNCTION (r
) = NULL
;
12393 /* We'll re-clone as appropriate in instantiate_template. */
12394 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12396 /* If we aren't complaining now, return on error before we register
12397 the specialization so that we'll complain eventually. */
12398 if ((complain
& tf_error
) == 0
12399 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12400 && !grok_op_properties (r
, /*complain=*/false))
12401 RETURN (error_mark_node
);
12403 /* When instantiating a constrained member, substitute
12404 into the constraints to create a new constraint. */
12405 if (tree ci
= get_constraints (t
))
12408 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12409 set_constraints (r
, ci
);
12412 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12413 this in the special friend case mentioned above where
12414 GEN_TMPL is NULL. */
12417 DECL_TEMPLATE_INFO (r
)
12418 = build_template_info (gen_tmpl
, argvec
);
12419 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12422 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12424 /* We instantiated this while substituting into
12425 the type earlier (template/friend54.C). */
12428 /* We're not supposed to instantiate default arguments
12429 until they are called, for a template. But, for a
12432 template <class T> void f ()
12433 { extern void g(int i = T()); }
12435 we should do the substitution when the template is
12436 instantiated. We handle the member function case in
12437 instantiate_class_template since the default arguments
12438 might refer to other members of the class. */
12440 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12441 && !uses_template_parms (argvec
))
12442 tsubst_default_arguments (r
, complain
);
12445 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12447 /* Copy the list of befriending classes. */
12448 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
12450 friends
= &TREE_CHAIN (*friends
))
12452 *friends
= copy_node (*friends
);
12453 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
12458 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12460 maybe_retrofit_in_chrg (r
);
12461 if (DECL_CONSTRUCTOR_P (r
) && !grok_ctor_properties (ctx
, r
))
12462 RETURN (error_mark_node
);
12463 /* If this is an instantiation of a member template, clone it.
12464 If it isn't, that'll be handled by
12465 clone_constructors_and_destructors. */
12466 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12467 clone_function_decl (r
, /*update_methods=*/false);
12469 else if ((complain
& tf_error
) != 0
12470 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12471 && !grok_op_properties (r
, /*complain=*/true))
12472 RETURN (error_mark_node
);
12474 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12475 SET_DECL_FRIEND_CONTEXT (r
,
12476 tsubst (DECL_FRIEND_CONTEXT (t
),
12477 args
, complain
, in_decl
));
12479 /* Possibly limit visibility based on template args. */
12480 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12481 if (DECL_VISIBILITY_SPECIFIED (t
))
12483 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12484 DECL_ATTRIBUTES (r
)
12485 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12487 determine_visibility (r
);
12488 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12489 && !processing_template_decl
)
12490 defaulted_late_check (r
);
12492 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12493 args
, complain
, in_decl
);
12499 tree type
= NULL_TREE
;
12501 tree expanded_types
= NULL_TREE
;
12502 tree prev_r
= NULL_TREE
;
12503 tree first_r
= NULL_TREE
;
12505 if (DECL_PACK_P (t
))
12507 /* If there is a local specialization that isn't a
12508 parameter pack, it means that we're doing a "simple"
12509 substitution from inside tsubst_pack_expansion. Just
12510 return the local specialization (which will be a single
12512 tree spec
= retrieve_local_specialization (t
);
12514 && TREE_CODE (spec
) == PARM_DECL
12515 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12518 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12519 the parameters in this function parameter pack. */
12520 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12521 complain
, in_decl
);
12522 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12524 len
= TREE_VEC_LENGTH (expanded_types
);
12526 /* Zero-length parameter packs are boring. Just substitute
12529 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12534 /* All we did was update the type. Make a note of that. */
12535 type
= expanded_types
;
12536 expanded_types
= NULL_TREE
;
12540 /* Loop through all of the parameters we'll build. When T is
12541 a function parameter pack, LEN is the number of expanded
12542 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12544 for (i
= 0; i
< len
; ++i
)
12548 if (DECL_TEMPLATE_PARM_P (t
))
12549 SET_DECL_TEMPLATE_PARM_P (r
);
12551 if (expanded_types
)
12552 /* We're on the Ith parameter of the function parameter
12555 /* Get the Ith type. */
12556 type
= TREE_VEC_ELT (expanded_types
, i
);
12558 /* Rename the parameter to include the index. */
12560 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12563 /* We're dealing with a normal parameter. */
12564 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12566 type
= type_decays_to (type
);
12567 TREE_TYPE (r
) = type
;
12568 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12570 if (DECL_INITIAL (r
))
12572 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12573 DECL_INITIAL (r
) = TREE_TYPE (r
);
12575 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12576 complain
, in_decl
);
12579 DECL_CONTEXT (r
) = NULL_TREE
;
12581 if (!DECL_TEMPLATE_PARM_P (r
))
12582 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12584 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12585 args
, complain
, in_decl
);
12587 /* Keep track of the first new parameter we
12588 generate. That's what will be returned to the
12593 /* Build a proper chain of parameters when substituting
12594 into a function parameter pack. */
12596 DECL_CHAIN (prev_r
) = r
;
12599 /* If cp_unevaluated_operand is set, we're just looking for a
12600 single dummy parameter, so don't keep going. */
12601 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12602 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12603 complain
, DECL_CHAIN (t
));
12605 /* FIRST_R contains the start of the chain we've built. */
12612 tree type
= NULL_TREE
;
12613 tree vec
= NULL_TREE
;
12614 tree expanded_types
= NULL_TREE
;
12617 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12619 /* This field is a lambda capture pack. Return a TREE_VEC of
12620 the expanded fields to instantiate_class_template_1 and
12621 store them in the specializations hash table as a
12622 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12623 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12624 complain
, in_decl
);
12625 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12627 len
= TREE_VEC_LENGTH (expanded_types
);
12628 vec
= make_tree_vec (len
);
12632 /* All we did was update the type. Make a note of that. */
12633 type
= expanded_types
;
12634 expanded_types
= NULL_TREE
;
12638 for (int i
= 0; i
< len
; ++i
)
12641 if (expanded_types
)
12643 type
= TREE_VEC_ELT (expanded_types
, i
);
12645 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12648 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12650 if (type
== error_mark_node
)
12651 RETURN (error_mark_node
);
12652 TREE_TYPE (r
) = type
;
12653 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12655 if (DECL_C_BIT_FIELD (r
))
12656 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12657 non-bit-fields DECL_INITIAL is a non-static data member
12658 initializer, which gets deferred instantiation. */
12660 = tsubst_expr (DECL_INITIAL (t
), args
,
12662 /*integral_constant_expression_p=*/true);
12663 else if (DECL_INITIAL (t
))
12665 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12666 NSDMI in perform_member_init. Still set DECL_INITIAL
12667 so that we know there is one. */
12668 DECL_INITIAL (r
) = void_node
;
12669 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12670 retrofit_lang_decl (r
);
12671 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12673 /* We don't have to set DECL_CONTEXT here; it is set by
12674 finish_member_declaration. */
12675 DECL_CHAIN (r
) = NULL_TREE
;
12677 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12678 args
, complain
, in_decl
);
12681 TREE_VEC_ELT (vec
, i
) = r
;
12687 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12688 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12689 register_specialization (pack
, t
, args
, false, 0);
12695 /* We reach here only for member using decls. We also need to check
12696 uses_template_parms because DECL_DEPENDENT_P is not set for a
12697 using-declaration that designates a member of the current
12698 instantiation (c++/53549). */
12699 if (DECL_DEPENDENT_P (t
)
12700 || uses_template_parms (USING_DECL_SCOPE (t
)))
12702 tree scope
= USING_DECL_SCOPE (t
);
12703 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12704 if (PACK_EXPANSION_P (scope
))
12706 tree vec
= tsubst_pack_expansion (scope
, args
, complain
, in_decl
);
12707 int len
= TREE_VEC_LENGTH (vec
);
12708 r
= make_tree_vec (len
);
12709 for (int i
= 0; i
< len
; ++i
)
12711 tree escope
= TREE_VEC_ELT (vec
, i
);
12712 tree elt
= do_class_using_decl (escope
, name
);
12715 r
= error_mark_node
;
12720 TREE_PROTECTED (elt
) = TREE_PROTECTED (t
);
12721 TREE_PRIVATE (elt
) = TREE_PRIVATE (t
);
12723 TREE_VEC_ELT (r
, i
) = elt
;
12728 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12729 complain
, in_decl
);
12730 r
= do_class_using_decl (inst_scope
, name
);
12732 r
= error_mark_node
;
12735 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12736 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12743 DECL_CHAIN (r
) = NULL_TREE
;
12750 tree argvec
= NULL_TREE
;
12751 tree gen_tmpl
= NULL_TREE
;
12753 tree tmpl
= NULL_TREE
;
12755 tree type
= NULL_TREE
;
12758 if (TREE_TYPE (t
) == error_mark_node
)
12759 RETURN (error_mark_node
);
12761 if (TREE_CODE (t
) == TYPE_DECL
12762 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12764 /* If this is the canonical decl, we don't have to
12765 mess with instantiations, and often we can't (for
12766 typename, template type parms and such). Note that
12767 TYPE_NAME is not correct for the above test if
12768 we've copied the type for a typedef. */
12769 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12770 if (type
== error_mark_node
)
12771 RETURN (error_mark_node
);
12772 r
= TYPE_NAME (type
);
12776 /* Check to see if we already have the specialization we
12779 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12781 /* T is a static data member or namespace-scope entity.
12782 We have to substitute into namespace-scope variables
12783 (not just variable templates) because of cases like:
12785 template <class T> void f() { extern T t; }
12787 where the entity referenced is not known until
12788 instantiation time. */
12790 ctx
= DECL_CONTEXT (t
);
12791 if (DECL_CLASS_SCOPE_P (t
))
12793 ctx
= tsubst_aggr_type (ctx
, args
,
12795 in_decl
, /*entering_scope=*/1);
12796 /* If CTX is unchanged, then T is in fact the
12797 specialization we want. That situation occurs when
12798 referencing a static data member within in its own
12799 class. We can use pointer equality, rather than
12800 same_type_p, because DECL_CONTEXT is always
12802 if (ctx
== DECL_CONTEXT (t
)
12803 /* ... unless T is a member template; in which
12804 case our caller can be willing to create a
12805 specialization of that template represented
12807 && !(DECL_TI_TEMPLATE (t
)
12808 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12814 tmpl
= DECL_TI_TEMPLATE (t
);
12815 gen_tmpl
= most_general_template (tmpl
);
12816 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12817 if (argvec
!= error_mark_node
)
12818 argvec
= (coerce_innermost_template_parms
12819 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12820 argvec
, t
, complain
,
12821 /*all*/true, /*defarg*/true));
12822 if (argvec
== error_mark_node
)
12823 RETURN (error_mark_node
);
12824 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12825 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12830 /* A local variable. */
12832 /* Subsequent calls to pushdecl will fill this in. */
12834 /* Unless this is a reference to a static variable from an
12835 enclosing function, in which case we need to fill it in now. */
12836 if (TREE_STATIC (t
))
12838 tree fn
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12839 if (fn
!= current_function_decl
)
12842 spec
= retrieve_local_specialization (t
);
12844 /* If we already have the specialization we need, there is
12845 nothing more to do. */
12852 /* Create a new node for the specialization we need. */
12854 if (type
== NULL_TREE
)
12856 if (is_typedef_decl (t
))
12857 type
= DECL_ORIGINAL_TYPE (t
);
12859 type
= TREE_TYPE (t
);
12861 && VAR_HAD_UNKNOWN_BOUND (t
)
12862 && type
!= error_mark_node
)
12863 type
= strip_array_domain (type
);
12864 type
= tsubst (type
, args
, complain
, in_decl
);
12868 /* Even if the original location is out of scope, the
12869 newly substituted one is not. */
12870 DECL_DEAD_FOR_LOCAL (r
) = 0;
12871 DECL_INITIALIZED_P (r
) = 0;
12872 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
12873 if (type
== error_mark_node
)
12874 RETURN (error_mark_node
);
12875 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12877 /* It may seem that this case cannot occur, since:
12882 declares a function, not a variable. However:
12885 template <typename T> void g() { T t; }
12886 template void g<f>();
12888 is an attempt to declare a variable with function
12890 error ("variable %qD has function type",
12891 /* R is not yet sufficiently initialized, so we
12892 just use its name. */
12894 RETURN (error_mark_node
);
12896 type
= complete_type (type
);
12897 /* Wait until cp_finish_decl to set this again, to handle
12898 circular dependency (template/instantiate6.C). */
12899 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
12900 type
= check_var_type (DECL_NAME (r
), type
);
12902 if (DECL_HAS_VALUE_EXPR_P (t
))
12904 tree ve
= DECL_VALUE_EXPR (t
);
12905 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
12906 /*constant_expression_p=*/false);
12907 if (REFERENCE_REF_P (ve
))
12909 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
12910 ve
= TREE_OPERAND (ve
, 0);
12912 SET_DECL_VALUE_EXPR (r
, ve
);
12914 if (CP_DECL_THREAD_LOCAL_P (r
)
12915 && !processing_template_decl
)
12916 set_decl_tls_model (r
, decl_default_tls_model (r
));
12918 else if (DECL_SELF_REFERENCE_P (t
))
12919 SET_DECL_SELF_REFERENCE_P (r
);
12920 TREE_TYPE (r
) = type
;
12921 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12922 DECL_CONTEXT (r
) = ctx
;
12923 /* Clear out the mangled name and RTL for the instantiation. */
12924 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12925 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12926 SET_DECL_RTL (r
, NULL
);
12927 /* The initializer must not be expanded until it is required;
12928 see [temp.inst]. */
12929 DECL_INITIAL (r
) = NULL_TREE
;
12931 SET_DECL_MODE (r
, VOIDmode
);
12932 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12933 SET_DECL_RTL (r
, NULL
);
12934 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
12937 /* Possibly limit visibility based on template args. */
12938 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12939 if (DECL_VISIBILITY_SPECIFIED (t
))
12941 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12942 DECL_ATTRIBUTES (r
)
12943 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12945 determine_visibility (r
);
12950 /* A static data member declaration is always marked
12951 external when it is declared in-class, even if an
12952 initializer is present. We mimic the non-template
12953 processing here. */
12954 DECL_EXTERNAL (r
) = 1;
12955 if (DECL_NAMESPACE_SCOPE_P (t
))
12956 DECL_NOT_REALLY_EXTERN (r
) = 1;
12958 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12959 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12960 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12964 if (DECL_LANG_SPECIFIC (r
))
12965 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12966 if (!cp_unevaluated_operand
)
12967 register_local_specialization (r
, t
);
12970 DECL_CHAIN (r
) = NULL_TREE
;
12972 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12974 args
, complain
, in_decl
);
12976 /* Preserve a typedef that names a type. */
12977 if (is_typedef_decl (r
) && type
!= error_mark_node
)
12979 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12980 set_underlying_type (r
);
12981 if (TYPE_DECL_ALIAS_P (r
))
12982 /* An alias template specialization can be dependent
12983 even if its underlying type is not. */
12984 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
12987 layout_decl (r
, 0);
12992 gcc_unreachable ();
12997 /* Restore the file and line information. */
12998 input_location
= saved_loc
;
13003 /* Substitute into the ARG_TYPES of a function type.
13004 If END is a TREE_CHAIN, leave it and any following types
13008 tsubst_arg_types (tree arg_types
,
13011 tsubst_flags_t complain
,
13014 tree remaining_arg_types
;
13015 tree type
= NULL_TREE
;
13017 tree expanded_args
= NULL_TREE
;
13020 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
13023 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
13024 args
, end
, complain
, in_decl
);
13025 if (remaining_arg_types
== error_mark_node
)
13026 return error_mark_node
;
13028 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
13030 /* For a pack expansion, perform substitution on the
13031 entire expression. Later on, we'll handle the arguments
13033 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
13034 args
, complain
, in_decl
);
13036 if (TREE_CODE (expanded_args
) == TREE_VEC
)
13037 /* So that we'll spin through the parameters, one by one. */
13038 i
= TREE_VEC_LENGTH (expanded_args
);
13041 /* We only partially substituted into the parameter
13042 pack. Our type is TYPE_PACK_EXPANSION. */
13043 type
= expanded_args
;
13044 expanded_args
= NULL_TREE
;
13052 type
= TREE_VEC_ELT (expanded_args
, i
);
13054 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
13056 if (type
== error_mark_node
)
13057 return error_mark_node
;
13058 if (VOID_TYPE_P (type
))
13060 if (complain
& tf_error
)
13062 error ("invalid parameter type %qT", type
);
13064 error ("in declaration %q+D", in_decl
);
13066 return error_mark_node
;
13069 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
13070 return error_mark_node
;
13072 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13073 top-level qualifiers as required. */
13074 type
= cv_unqualified (type_decays_to (type
));
13076 /* We do not substitute into default arguments here. The standard
13077 mandates that they be instantiated only when needed, which is
13078 done in build_over_call. */
13079 default_arg
= TREE_PURPOSE (arg_types
);
13081 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
13083 /* We've instantiated a template before its default arguments
13084 have been parsed. This can happen for a nested template
13085 class, and is not an error unless we require the default
13086 argument in a call of this function. */
13087 remaining_arg_types
=
13088 tree_cons (default_arg
, type
, remaining_arg_types
);
13089 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
13092 remaining_arg_types
=
13093 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
13096 return remaining_arg_types
;
13099 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13100 *not* handle the exception-specification for FNTYPE, because the
13101 initial substitution of explicitly provided template parameters
13102 during argument deduction forbids substitution into the
13103 exception-specification:
13107 All references in the function type of the function template to the
13108 corresponding template parameters are replaced by the specified tem-
13109 plate argument values. If a substitution in a template parameter or
13110 in the function type of the function template results in an invalid
13111 type, type deduction fails. [Note: The equivalent substitution in
13112 exception specifications is done only when the function is instanti-
13113 ated, at which point a program is ill-formed if the substitution
13114 results in an invalid type.] */
13117 tsubst_function_type (tree t
,
13119 tsubst_flags_t complain
,
13123 tree arg_types
= NULL_TREE
;
13126 /* The TYPE_CONTEXT is not used for function/method types. */
13127 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
13129 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13131 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
13133 if (late_return_type_p
)
13135 /* Substitute the argument types. */
13136 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13137 complain
, in_decl
);
13138 if (arg_types
== error_mark_node
)
13139 return error_mark_node
;
13141 tree save_ccp
= current_class_ptr
;
13142 tree save_ccr
= current_class_ref
;
13143 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
13144 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
13145 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
13148 /* DR 1207: 'this' is in scope in the trailing return type. */
13149 inject_this_parameter (this_type
, cp_type_quals (this_type
));
13152 /* Substitute the return type. */
13153 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13157 current_class_ptr
= save_ccp
;
13158 current_class_ref
= save_ccr
;
13162 /* Substitute the return type. */
13163 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13165 if (return_type
== error_mark_node
)
13166 return error_mark_node
;
13167 /* DR 486 clarifies that creation of a function type with an
13168 invalid return type is a deduction failure. */
13169 if (TREE_CODE (return_type
) == ARRAY_TYPE
13170 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
13172 if (complain
& tf_error
)
13174 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
13175 error ("function returning an array");
13177 error ("function returning a function");
13179 return error_mark_node
;
13182 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
13183 return error_mark_node
;
13185 if (!late_return_type_p
)
13187 /* Substitute the argument types. */
13188 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
13189 complain
, in_decl
);
13190 if (arg_types
== error_mark_node
)
13191 return error_mark_node
;
13194 /* Construct a new type node and return it. */
13195 if (TREE_CODE (t
) == FUNCTION_TYPE
)
13197 fntype
= build_function_type (return_type
, arg_types
);
13198 fntype
= apply_memfn_quals (fntype
,
13199 type_memfn_quals (t
),
13200 type_memfn_rqual (t
));
13204 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
13205 /* Don't pick up extra function qualifiers from the basetype. */
13206 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
13207 if (! MAYBE_CLASS_TYPE_P (r
))
13211 Type deduction may fail for any of the following
13214 -- Attempting to create "pointer to member of T" when T
13215 is not a class type. */
13216 if (complain
& tf_error
)
13217 error ("creating pointer to member function of non-class type %qT",
13219 return error_mark_node
;
13222 fntype
= build_method_type_directly (r
, return_type
,
13223 TREE_CHAIN (arg_types
));
13224 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
13226 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
13228 if (late_return_type_p
)
13229 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
13234 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13235 ARGS into that specification, and return the substituted
13236 specification. If there is no specification, return NULL_TREE. */
13239 tsubst_exception_specification (tree fntype
,
13241 tsubst_flags_t complain
,
13248 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
13249 new_specs
= NULL_TREE
;
13250 if (specs
&& TREE_PURPOSE (specs
))
13252 /* A noexcept-specifier. */
13253 tree expr
= TREE_PURPOSE (specs
);
13254 if (TREE_CODE (expr
) == INTEGER_CST
)
13258 /* Defer instantiation of noexcept-specifiers to avoid
13259 excessive instantiations (c++/49107). */
13260 new_specs
= make_node (DEFERRED_NOEXCEPT
);
13261 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
13263 /* We already partially instantiated this member template,
13264 so combine the new args with the old. */
13265 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
13266 = DEFERRED_NOEXCEPT_PATTERN (expr
);
13267 DEFERRED_NOEXCEPT_ARGS (new_specs
)
13268 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
13272 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
13273 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
13277 new_specs
= tsubst_copy_and_build
13278 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
13279 /*integral_constant_expression_p=*/true);
13280 new_specs
= build_noexcept_spec (new_specs
, complain
);
13284 if (! TREE_VALUE (specs
))
13291 tree expanded_specs
= NULL_TREE
;
13293 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
13295 /* Expand the pack expansion type. */
13296 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
13300 if (expanded_specs
== error_mark_node
)
13301 return error_mark_node
;
13302 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
13303 len
= TREE_VEC_LENGTH (expanded_specs
);
13306 /* We're substituting into a member template, so
13307 we got a TYPE_PACK_EXPANSION back. Add that
13308 expansion and move on. */
13309 gcc_assert (TREE_CODE (expanded_specs
)
13310 == TYPE_PACK_EXPANSION
);
13311 new_specs
= add_exception_specifier (new_specs
,
13314 specs
= TREE_CHAIN (specs
);
13319 for (i
= 0; i
< len
; ++i
)
13321 if (expanded_specs
)
13322 spec
= TREE_VEC_ELT (expanded_specs
, i
);
13324 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
13325 if (spec
== error_mark_node
)
13327 new_specs
= add_exception_specifier (new_specs
, spec
,
13331 specs
= TREE_CHAIN (specs
);
13337 /* Take the tree structure T and replace template parameters used
13338 therein with the argument vector ARGS. IN_DECL is an associated
13339 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13340 Issue error and warning messages under control of COMPLAIN. Note
13341 that we must be relatively non-tolerant of extensions here, in
13342 order to preserve conformance; if we allow substitutions that
13343 should not be allowed, we may allow argument deductions that should
13344 not succeed, and therefore report ambiguous overload situations
13345 where there are none. In theory, we could allow the substitution,
13346 but indicate that it should have failed, and allow our caller to
13347 make sure that the right thing happens, but we don't try to do this
13350 This function is used for dealing with types, decls and the like;
13351 for expressions, use tsubst_expr or tsubst_copy. */
13354 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13356 enum tree_code code
;
13357 tree type
, r
= NULL_TREE
;
13359 if (t
== NULL_TREE
|| t
== error_mark_node
13360 || t
== integer_type_node
13361 || t
== void_type_node
13362 || t
== char_type_node
13363 || t
== unknown_type_node
13364 || TREE_CODE (t
) == NAMESPACE_DECL
13365 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13369 return tsubst_decl (t
, args
, complain
);
13371 if (args
== NULL_TREE
)
13374 code
= TREE_CODE (t
);
13376 if (code
== IDENTIFIER_NODE
)
13377 type
= IDENTIFIER_TYPE_VALUE (t
);
13379 type
= TREE_TYPE (t
);
13381 gcc_assert (type
!= unknown_type_node
);
13383 /* Reuse typedefs. We need to do this to handle dependent attributes,
13384 such as attribute aligned. */
13386 && typedef_variant_p (t
))
13388 tree decl
= TYPE_NAME (t
);
13390 if (alias_template_specialization_p (t
))
13392 /* DECL represents an alias template and we want to
13394 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13395 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13396 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13398 else if (DECL_CLASS_SCOPE_P (decl
)
13399 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13400 && uses_template_parms (DECL_CONTEXT (decl
)))
13402 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13403 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13404 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13406 else if (DECL_FUNCTION_SCOPE_P (decl
)
13407 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13408 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13409 r
= retrieve_local_specialization (decl
);
13411 /* The typedef is from a non-template context. */
13417 r
= cp_build_qualified_type_real
13418 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13419 complain
| tf_ignore_bad_quals
);
13424 /* We don't have an instantiation yet, so drop the typedef. */
13425 int quals
= cp_type_quals (t
);
13426 t
= DECL_ORIGINAL_TYPE (decl
);
13427 t
= cp_build_qualified_type_real (t
, quals
,
13428 complain
| tf_ignore_bad_quals
);
13432 bool fndecl_type
= (complain
& tf_fndecl_type
);
13433 complain
&= ~tf_fndecl_type
;
13436 && code
!= TYPENAME_TYPE
13437 && code
!= TEMPLATE_TYPE_PARM
13438 && code
!= TEMPLATE_PARM_INDEX
13439 && code
!= IDENTIFIER_NODE
13440 && code
!= FUNCTION_TYPE
13441 && code
!= METHOD_TYPE
)
13442 type
= tsubst (type
, args
, complain
, in_decl
);
13443 if (type
== error_mark_node
)
13444 return error_mark_node
;
13450 case ENUMERAL_TYPE
:
13451 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13452 /*entering_scope=*/0);
13455 case IDENTIFIER_NODE
:
13466 if (t
== integer_type_node
)
13469 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13470 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13474 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13476 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13477 /*integral_constant_expression_p=*/false);
13479 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13481 if (TREE_CODE (max
) == NOP_EXPR
13482 && TREE_SIDE_EFFECTS (omax
)
13483 && !TREE_TYPE (max
))
13484 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13486 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13487 with TREE_SIDE_EFFECTS that indicates this is not an integral
13488 constant expression. */
13489 if (processing_template_decl
13490 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13492 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13493 TREE_SIDE_EFFECTS (max
) = 1;
13496 return compute_array_index_type (NULL_TREE
, max
, complain
);
13499 case TEMPLATE_TYPE_PARM
:
13500 case TEMPLATE_TEMPLATE_PARM
:
13501 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13502 case TEMPLATE_PARM_INDEX
:
13507 tree arg
= NULL_TREE
;
13509 /* Early in template argument deduction substitution, we don't
13510 want to reduce the level of 'auto', or it will be confused
13511 with a normal template parm in subsequent deduction. */
13512 if (is_auto (t
) && (complain
& tf_partial
))
13517 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13518 template_parm_level_and_index (t
, &level
, &idx
);
13520 levels
= TMPL_ARGS_DEPTH (args
);
13521 if (level
<= levels
13522 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13524 arg
= TMPL_ARG (args
, level
, idx
);
13526 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13528 /* See through ARGUMENT_PACK_SELECT arguments. */
13529 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13530 /* If the selected argument is an expansion E, that most
13531 likely means we were called from
13532 gen_elem_of_pack_expansion_instantiation during the
13533 substituting of pack an argument pack (which Ith
13534 element is a pack expansion, where I is
13535 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13536 In this case, the Ith element resulting from this
13537 substituting is going to be a pack expansion, which
13538 pattern is the pattern of E. Let's return the
13540 gen_elem_of_pack_expansion_instantiation will
13541 build the resulting pack expansion from it. */
13542 if (PACK_EXPANSION_P (arg
))
13544 /* Make sure we aren't throwing away arg info. */
13545 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13546 arg
= PACK_EXPANSION_PATTERN (arg
);
13551 if (arg
== error_mark_node
)
13552 return error_mark_node
;
13553 else if (arg
!= NULL_TREE
)
13555 if (ARGUMENT_PACK_P (arg
))
13556 /* If ARG is an argument pack, we don't actually want to
13557 perform a substitution here, because substitutions
13558 for argument packs are only done
13559 element-by-element. We can get to this point when
13560 substituting the type of a non-type template
13561 parameter pack, when that type actually contains
13562 template parameter packs from an outer template, e.g.,
13564 template<typename... Types> struct A {
13565 template<Types... Values> struct B { };
13569 if (code
== TEMPLATE_TYPE_PARM
)
13572 gcc_assert (TYPE_P (arg
));
13574 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13576 return cp_build_qualified_type_real
13577 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13579 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13581 /* We are processing a type constructed from a
13582 template template parameter. */
13583 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13584 args
, complain
, in_decl
);
13585 if (argvec
== error_mark_node
)
13586 return error_mark_node
;
13588 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13589 || TREE_CODE (arg
) == TEMPLATE_DECL
13590 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13592 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13593 /* Consider this code:
13595 template <template <class> class Template>
13597 template <class Arg> using Bind = Template<Arg>;
13600 template <template <class> class Template, class Arg>
13601 using Instantiate = Template<Arg>; //#0
13603 template <template <class> class Template,
13606 Instantiate<Internal<Template>::template Bind,
13609 When #1 is parsed, the
13610 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13611 parameter `Template' in #0 matches the
13612 UNBOUND_CLASS_TEMPLATE representing the argument
13613 `Internal<Template>::template Bind'; We then want
13614 to assemble the type `Bind<Argument>' that can't
13615 be fully created right now, because
13616 `Internal<Template>' not being complete, the Bind
13617 template cannot be looked up in that context. So
13618 we need to "store" `Bind<Argument>' for later
13619 when the context of Bind becomes complete. Let's
13620 store that in a TYPENAME_TYPE. */
13621 return make_typename_type (TYPE_CONTEXT (arg
),
13622 build_nt (TEMPLATE_ID_EXPR
,
13623 TYPE_IDENTIFIER (arg
),
13628 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13629 are resolving nested-types in the signature of a
13630 member function templates. Otherwise ARG is a
13631 TEMPLATE_DECL and is the real template to be
13633 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13634 arg
= TYPE_NAME (arg
);
13636 r
= lookup_template_class (arg
,
13638 DECL_CONTEXT (arg
),
13639 /*entering_scope=*/0,
13641 return cp_build_qualified_type_real
13642 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13644 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13647 /* TEMPLATE_PARM_INDEX. */
13648 return convert_from_reference (unshare_expr (arg
));
13652 /* This can happen during the attempted tsubst'ing in
13653 unify. This means that we don't yet have any information
13654 about the template parameter in question. */
13657 /* If we get here, we must have been looking at a parm for a
13658 more deeply nested template. Make a new version of this
13659 template parameter, but with a lower level. */
13662 case TEMPLATE_TYPE_PARM
:
13663 case TEMPLATE_TEMPLATE_PARM
:
13664 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13665 if (cp_type_quals (t
))
13667 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13668 r
= cp_build_qualified_type_real
13669 (r
, cp_type_quals (t
),
13670 complain
| (code
== TEMPLATE_TYPE_PARM
13671 ? tf_ignore_bad_quals
: 0));
13673 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13674 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13675 && (r
= (TEMPLATE_PARM_DESCENDANTS
13676 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13677 && (r
= TREE_TYPE (r
))
13678 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13679 /* Break infinite recursion when substituting the constraints
13680 of a constrained placeholder. */;
13684 TEMPLATE_TYPE_PARM_INDEX (r
)
13685 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13686 r
, levels
, args
, complain
);
13687 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13688 TYPE_MAIN_VARIANT (r
) = r
;
13689 TYPE_POINTER_TO (r
) = NULL_TREE
;
13690 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13692 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13694 /* Propagate constraints on placeholders. */
13695 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13696 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13697 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13698 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13701 pl
= tsubst (pl
, args
, complain
, in_decl
);
13702 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13706 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13707 /* We have reduced the level of the template
13708 template parameter, but not the levels of its
13709 template parameters, so canonical_type_parameter
13710 will not be able to find the canonical template
13711 template parameter for this level. Thus, we
13712 require structural equality checking to compare
13713 TEMPLATE_TEMPLATE_PARMs. */
13714 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13715 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13716 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13718 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13720 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13722 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13723 /* We might need to substitute into the types of non-type
13724 template parameters. */
13725 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13726 complain
, in_decl
);
13727 if (tmpl
== error_mark_node
)
13728 return error_mark_node
;
13729 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13730 complain
, in_decl
);
13731 if (argvec
== error_mark_node
)
13732 return error_mark_node
;
13734 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13735 = build_template_info (tmpl
, argvec
);
13740 case TEMPLATE_PARM_INDEX
:
13741 /* OK, now substitute the type of the non-type parameter. We
13742 couldn't do it earlier because it might be an auto parameter,
13743 and we wouldn't need to if we had an argument. */
13744 type
= tsubst (type
, args
, complain
, in_decl
);
13745 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13749 gcc_unreachable ();
13757 tree purpose
, value
, chain
;
13759 if (t
== void_list_node
)
13762 purpose
= TREE_PURPOSE (t
);
13765 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13766 if (purpose
== error_mark_node
)
13767 return error_mark_node
;
13769 value
= TREE_VALUE (t
);
13772 value
= tsubst (value
, args
, complain
, in_decl
);
13773 if (value
== error_mark_node
)
13774 return error_mark_node
;
13776 chain
= TREE_CHAIN (t
);
13777 if (chain
&& chain
!= void_type_node
)
13779 chain
= tsubst (chain
, args
, complain
, in_decl
);
13780 if (chain
== error_mark_node
)
13781 return error_mark_node
;
13783 if (purpose
== TREE_PURPOSE (t
)
13784 && value
== TREE_VALUE (t
)
13785 && chain
== TREE_CHAIN (t
))
13787 return hash_tree_cons (purpose
, value
, chain
);
13791 /* We should never be tsubsting a binfo. */
13792 gcc_unreachable ();
13795 /* A vector of template arguments. */
13796 gcc_assert (!type
);
13797 return tsubst_template_args (t
, args
, complain
, in_decl
);
13800 case REFERENCE_TYPE
:
13802 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13807 Type deduction may fail for any of the following
13810 -- Attempting to create a pointer to reference type.
13811 -- Attempting to create a reference to a reference type or
13812 a reference to void.
13814 Core issue 106 says that creating a reference to a reference
13815 during instantiation is no longer a cause for failure. We
13816 only enforce this check in strict C++98 mode. */
13817 if ((TREE_CODE (type
) == REFERENCE_TYPE
13818 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13819 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13821 static location_t last_loc
;
13823 /* We keep track of the last time we issued this error
13824 message to avoid spewing a ton of messages during a
13825 single bad template instantiation. */
13826 if (complain
& tf_error
13827 && last_loc
!= input_location
)
13829 if (VOID_TYPE_P (type
))
13830 error ("forming reference to void");
13831 else if (code
== POINTER_TYPE
)
13832 error ("forming pointer to reference type %qT", type
);
13834 error ("forming reference to reference type %qT", type
);
13835 last_loc
= input_location
;
13838 return error_mark_node
;
13840 else if (TREE_CODE (type
) == FUNCTION_TYPE
13841 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13842 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13844 if (complain
& tf_error
)
13846 if (code
== POINTER_TYPE
)
13847 error ("forming pointer to qualified function type %qT",
13850 error ("forming reference to qualified function type %qT",
13853 return error_mark_node
;
13855 else if (code
== POINTER_TYPE
)
13857 r
= build_pointer_type (type
);
13858 if (TREE_CODE (type
) == METHOD_TYPE
)
13859 r
= build_ptrmemfunc_type (r
);
13861 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13862 /* In C++0x, during template argument substitution, when there is an
13863 attempt to create a reference to a reference type, reference
13864 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13866 "If a template-argument for a template-parameter T names a type
13867 that is a reference to a type A, an attempt to create the type
13868 'lvalue reference to cv T' creates the type 'lvalue reference to
13869 A,' while an attempt to create the type type rvalue reference to
13870 cv T' creates the type T"
13872 r
= cp_build_reference_type
13874 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13876 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13877 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13879 if (r
!= error_mark_node
)
13880 /* Will this ever be needed for TYPE_..._TO values? */
13887 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13888 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13892 Type deduction may fail for any of the following
13895 -- Attempting to create "pointer to member of T" when T
13896 is not a class type. */
13897 if (complain
& tf_error
)
13898 error ("creating pointer to member of non-class type %qT", r
);
13899 return error_mark_node
;
13901 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13903 if (complain
& tf_error
)
13904 error ("creating pointer to member reference type %qT", type
);
13905 return error_mark_node
;
13907 if (VOID_TYPE_P (type
))
13909 if (complain
& tf_error
)
13910 error ("creating pointer to member of type void");
13911 return error_mark_node
;
13913 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13914 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13916 /* The type of the implicit object parameter gets its
13917 cv-qualifiers from the FUNCTION_TYPE. */
13920 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13921 type_memfn_rqual (type
));
13922 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13923 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13927 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13931 case FUNCTION_TYPE
:
13936 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13937 if (fntype
== error_mark_node
)
13938 return error_mark_node
;
13940 /* Substitute the exception specification. */
13941 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
13942 /*defer_ok*/fndecl_type
);
13943 if (specs
== error_mark_node
)
13944 return error_mark_node
;
13946 fntype
= build_exception_variant (fntype
, specs
);
13951 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13952 if (domain
== error_mark_node
)
13953 return error_mark_node
;
13955 /* As an optimization, we avoid regenerating the array type if
13956 it will obviously be the same as T. */
13957 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13960 /* These checks should match the ones in create_array_type_for_decl.
13964 The deduction may fail for any of the following reasons:
13966 -- Attempting to create an array with an element type that
13967 is void, a function type, or a reference type, or [DR337]
13968 an abstract class type. */
13969 if (VOID_TYPE_P (type
)
13970 || TREE_CODE (type
) == FUNCTION_TYPE
13971 || (TREE_CODE (type
) == ARRAY_TYPE
13972 && TYPE_DOMAIN (type
) == NULL_TREE
)
13973 || TREE_CODE (type
) == REFERENCE_TYPE
)
13975 if (complain
& tf_error
)
13976 error ("creating array of %qT", type
);
13977 return error_mark_node
;
13980 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13981 return error_mark_node
;
13983 r
= build_cplus_array_type (type
, domain
);
13985 if (TYPE_USER_ALIGN (t
))
13987 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
13988 TYPE_USER_ALIGN (r
) = 1;
13994 case TYPENAME_TYPE
:
13996 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13997 in_decl
, /*entering_scope=*/1);
13998 if (ctx
== error_mark_node
)
13999 return error_mark_node
;
14001 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
14002 complain
, in_decl
);
14003 if (f
== error_mark_node
)
14004 return error_mark_node
;
14006 if (!MAYBE_CLASS_TYPE_P (ctx
))
14008 if (complain
& tf_error
)
14009 error ("%qT is not a class, struct, or union type", ctx
);
14010 return error_mark_node
;
14012 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
14014 /* Normally, make_typename_type does not require that the CTX
14015 have complete type in order to allow things like:
14017 template <class T> struct S { typename S<T>::X Y; };
14019 But, such constructs have already been resolved by this
14020 point, so here CTX really should have complete type, unless
14021 it's a partial instantiation. */
14022 ctx
= complete_type (ctx
);
14023 if (!COMPLETE_TYPE_P (ctx
))
14025 if (complain
& tf_error
)
14026 cxx_incomplete_type_error (NULL_TREE
, ctx
);
14027 return error_mark_node
;
14031 f
= make_typename_type (ctx
, f
, typename_type
,
14032 complain
| tf_keep_type_decl
);
14033 if (f
== error_mark_node
)
14035 if (TREE_CODE (f
) == TYPE_DECL
)
14037 complain
|= tf_ignore_bad_quals
;
14041 if (TREE_CODE (f
) != TYPENAME_TYPE
)
14043 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
14045 if (complain
& tf_error
)
14046 error ("%qT resolves to %qT, which is not an enumeration type",
14049 return error_mark_node
;
14051 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
14053 if (complain
& tf_error
)
14054 error ("%qT resolves to %qT, which is is not a class type",
14057 return error_mark_node
;
14061 return cp_build_qualified_type_real
14062 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
14065 case UNBOUND_CLASS_TEMPLATE
:
14067 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
14068 in_decl
, /*entering_scope=*/1);
14069 tree name
= TYPE_IDENTIFIER (t
);
14070 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
14072 if (ctx
== error_mark_node
|| name
== error_mark_node
)
14073 return error_mark_node
;
14076 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
14077 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
14084 ++cp_unevaluated_operand
;
14085 ++c_inhibit_evaluation_warnings
;
14087 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
14089 /*integral_constant_expression_p=*/false);
14091 --cp_unevaluated_operand
;
14092 --c_inhibit_evaluation_warnings
;
14094 type
= finish_typeof (type
);
14095 return cp_build_qualified_type_real (type
,
14097 | cp_type_quals (type
),
14101 case DECLTYPE_TYPE
:
14105 ++cp_unevaluated_operand
;
14106 ++c_inhibit_evaluation_warnings
;
14108 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
14109 complain
|tf_decltype
, in_decl
,
14110 /*function_p*/false,
14111 /*integral_constant_expression*/false);
14113 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
14115 if (type
== NULL_TREE
)
14117 if (complain
& tf_error
)
14118 error ("empty initializer in lambda init-capture");
14119 type
= error_mark_node
;
14121 else if (TREE_CODE (type
) == TREE_LIST
)
14122 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
14125 --cp_unevaluated_operand
;
14126 --c_inhibit_evaluation_warnings
;
14128 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
14129 type
= lambda_capture_field_type (type
,
14130 DECLTYPE_FOR_INIT_CAPTURE (t
),
14131 DECLTYPE_FOR_REF_CAPTURE (t
));
14132 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
14133 type
= lambda_proxy_type (type
);
14136 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
14137 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
14139 /* In a template ~id could be either a complement expression
14140 or an unqualified-id naming a destructor; if instantiating
14141 it produces an expression, it's not an id-expression or
14144 type
= finish_decltype_type (type
, id
, complain
);
14146 return cp_build_qualified_type_real (type
,
14148 | cp_type_quals (type
),
14149 complain
| tf_ignore_bad_quals
);
14152 case UNDERLYING_TYPE
:
14154 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
14155 complain
, in_decl
);
14156 return finish_underlying_type (type
);
14159 case TYPE_ARGUMENT_PACK
:
14160 case NONTYPE_ARGUMENT_PACK
:
14164 if (code
== NONTYPE_ARGUMENT_PACK
)
14165 r
= make_node (code
);
14167 r
= cxx_make_type (code
);
14169 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
14170 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
14171 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
14189 /* We should use one of the expression tsubsts for these codes. */
14190 gcc_unreachable ();
14193 sorry ("use of %qs in template", get_tree_code_name (code
));
14194 return error_mark_node
;
14198 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14199 type of the expression on the left-hand side of the "." or "->"
14203 tsubst_baselink (tree baselink
, tree object_type
,
14204 tree args
, tsubst_flags_t complain
, tree in_decl
)
14207 tree qualifying_scope
;
14210 tree template_args
= 0;
14211 bool template_id_p
= false;
14212 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
14214 /* A baselink indicates a function from a base class. Both the
14215 BASELINK_ACCESS_BINFO and the base class referenced may
14216 indicate bases of the template class, rather than the
14217 instantiated class. In addition, lookups that were not
14218 ambiguous before may be ambiguous now. Therefore, we perform
14219 the lookup again. */
14220 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
14221 qualifying_scope
= tsubst (qualifying_scope
, args
,
14222 complain
, in_decl
);
14223 fns
= BASELINK_FUNCTIONS (baselink
);
14224 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
14225 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
14227 template_id_p
= true;
14228 template_args
= TREE_OPERAND (fns
, 1);
14229 fns
= TREE_OPERAND (fns
, 0);
14231 template_args
= tsubst_template_args (template_args
, args
,
14232 complain
, in_decl
);
14234 name
= OVL_NAME (fns
);
14235 if (IDENTIFIER_TYPENAME_P (name
))
14236 name
= mangle_conv_op_name_for_type (optype
);
14237 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
14240 if (constructor_name_p (name
, qualifying_scope
))
14242 if (complain
& tf_error
)
14243 error ("cannot call constructor %<%T::%D%> directly",
14244 qualifying_scope
, name
);
14246 return error_mark_node
;
14249 /* If lookup found a single function, mark it as used at this
14250 point. (If it lookup found multiple functions the one selected
14251 later by overload resolution will be marked as used at that
14253 if (BASELINK_P (baselink
))
14254 fns
= BASELINK_FUNCTIONS (baselink
);
14255 if (!template_id_p
&& !really_overloaded_fn (fns
)
14256 && !mark_used (OVL_FIRST (fns
), complain
) && !(complain
& tf_error
))
14257 return error_mark_node
;
14259 /* Add back the template arguments, if present. */
14260 if (BASELINK_P (baselink
) && template_id_p
)
14261 BASELINK_FUNCTIONS (baselink
)
14262 = build2 (TEMPLATE_ID_EXPR
,
14264 BASELINK_FUNCTIONS (baselink
),
14266 /* Update the conversion operator type. */
14267 if (BASELINK_P (baselink
))
14268 BASELINK_OPTYPE (baselink
) = optype
;
14271 object_type
= current_class_type
;
14273 if (qualified
|| name
== complete_dtor_identifier
)
14275 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
14279 /* We need to call adjust_result_of_qualified_name_lookup in case the
14280 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14281 so that we still get virtual function binding. */
14282 BASELINK_QUALIFIED_P (baselink
) = false;
14287 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14288 true if the qualified-id will be a postfix-expression in-and-of
14289 itself; false if more of the postfix-expression follows the
14290 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14294 tsubst_qualified_id (tree qualified_id
, tree args
,
14295 tsubst_flags_t complain
, tree in_decl
,
14296 bool done
, bool address_p
)
14302 tree template_args
;
14303 location_t loc
= UNKNOWN_LOCATION
;
14305 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
14307 /* Figure out what name to look up. */
14308 name
= TREE_OPERAND (qualified_id
, 1);
14309 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
14311 is_template
= true;
14312 loc
= EXPR_LOCATION (name
);
14313 template_args
= TREE_OPERAND (name
, 1);
14315 template_args
= tsubst_template_args (template_args
, args
,
14316 complain
, in_decl
);
14317 if (template_args
== error_mark_node
)
14318 return error_mark_node
;
14319 name
= TREE_OPERAND (name
, 0);
14323 is_template
= false;
14324 template_args
= NULL_TREE
;
14327 /* Substitute into the qualifying scope. When there are no ARGS, we
14328 are just trying to simplify a non-dependent expression. In that
14329 case the qualifying scope may be dependent, and, in any case,
14330 substituting will not help. */
14331 scope
= TREE_OPERAND (qualified_id
, 0);
14334 scope
= tsubst (scope
, args
, complain
, in_decl
);
14335 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
14340 if (dependent_scope_p (scope
))
14343 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
14344 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
14345 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
14346 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
14350 if (!BASELINK_P (name
) && !DECL_P (expr
))
14352 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
14354 /* A BIT_NOT_EXPR is used to represent a destructor. */
14355 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14357 error ("qualifying type %qT does not match destructor name ~%qT",
14358 scope
, TREE_OPERAND (expr
, 0));
14359 expr
= error_mark_node
;
14362 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14363 /*is_type_p=*/0, false);
14366 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14367 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14368 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14370 if (complain
& tf_error
)
14372 error ("dependent-name %qE is parsed as a non-type, but "
14373 "instantiation yields a type", qualified_id
);
14374 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14376 return error_mark_node
;
14382 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14384 /* Remember that there was a reference to this entity. */
14385 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14386 return error_mark_node
;
14389 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14391 if (complain
& tf_error
)
14392 qualified_name_lookup_error (scope
,
14393 TREE_OPERAND (qualified_id
, 1),
14394 expr
, input_location
);
14395 return error_mark_node
;
14400 if (variable_template_p (expr
))
14401 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14404 expr
= lookup_template_function (expr
, template_args
);
14407 if (expr
== error_mark_node
&& complain
& tf_error
)
14408 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14409 expr
, input_location
);
14410 else if (TYPE_P (scope
))
14412 expr
= (adjust_result_of_qualified_name_lookup
14413 (expr
, scope
, current_nonlambda_class_type ()));
14414 expr
= (finish_qualified_id_expr
14415 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14416 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14417 /*template_arg_p=*/false, complain
));
14420 /* Expressions do not generally have reference type. */
14421 if (TREE_CODE (expr
) != SCOPE_REF
14422 /* However, if we're about to form a pointer-to-member, we just
14423 want the referenced member referenced. */
14424 && TREE_CODE (expr
) != OFFSET_REF
)
14425 expr
= convert_from_reference (expr
);
14427 if (REF_PARENTHESIZED_P (qualified_id
))
14428 expr
= force_paren_expr (expr
);
14433 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14434 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14438 tsubst_init (tree init
, tree decl
, tree args
,
14439 tsubst_flags_t complain
, tree in_decl
)
14444 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14446 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14448 /* If we had an initializer but it
14449 instantiated to nothing,
14450 value-initialize the object. This will
14451 only occur when the initializer was a
14452 pack expansion where the parameter packs
14453 used in that expansion were of length
14455 init
= build_value_init (TREE_TYPE (decl
),
14457 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14458 init
= get_target_expr_sfinae (init
, complain
);
14459 if (TREE_CODE (init
) == TARGET_EXPR
)
14460 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
14466 /* Like tsubst, but deals with expressions. This function just replaces
14467 template parms; to finish processing the resultant expression, use
14468 tsubst_copy_and_build or tsubst_expr. */
14471 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14473 enum tree_code code
;
14476 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14479 code
= TREE_CODE (t
);
14484 r
= retrieve_local_specialization (t
);
14486 if (r
== NULL_TREE
)
14488 /* We get here for a use of 'this' in an NSDMI as part of a
14489 constructor call or as part of an aggregate initialization. */
14490 if (DECL_NAME (t
) == this_identifier
14491 && ((current_function_decl
14492 && DECL_CONSTRUCTOR_P (current_function_decl
))
14493 || (current_class_ref
14494 && TREE_CODE (current_class_ref
) == PLACEHOLDER_EXPR
)))
14495 return current_class_ptr
;
14497 /* This can happen for a parameter name used later in a function
14498 declaration (such as in a late-specified return type). Just
14499 make a dummy decl, since it's only used for its type. */
14500 gcc_assert (cp_unevaluated_operand
!= 0);
14501 r
= tsubst_decl (t
, args
, complain
);
14502 /* Give it the template pattern as its context; its true context
14503 hasn't been instantiated yet and this is good enough for
14505 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14508 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14509 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14510 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14511 return error_mark_node
;
14519 if (DECL_TEMPLATE_PARM_P (t
))
14520 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14521 /* There is no need to substitute into namespace-scope
14523 if (DECL_NAMESPACE_SCOPE_P (t
))
14525 /* If ARGS is NULL, then T is known to be non-dependent. */
14526 if (args
== NULL_TREE
)
14527 return scalar_constant_value (t
);
14529 /* Unfortunately, we cannot just call lookup_name here.
14532 template <int I> int f() {
14534 struct S { void g() { E e = a; } };
14537 When we instantiate f<7>::S::g(), say, lookup_name is not
14538 clever enough to find f<7>::a. */
14540 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14541 /*entering_scope=*/0);
14543 for (v
= TYPE_VALUES (enum_type
);
14545 v
= TREE_CHAIN (v
))
14546 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14547 return TREE_VALUE (v
);
14549 /* We didn't find the name. That should never happen; if
14550 name-lookup found it during preliminary parsing, we
14551 should find it again here during instantiation. */
14552 gcc_unreachable ();
14557 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14559 /* Check for a local specialization set up by
14560 tsubst_pack_expansion. */
14561 if (tree r
= retrieve_local_specialization (t
))
14563 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14564 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14568 /* When retrieving a capture pack from a generic lambda, remove the
14569 lambda call op's own template argument list from ARGS. Only the
14570 template arguments active for the closure type should be used to
14571 retrieve the pack specialization. */
14572 if (LAMBDA_FUNCTION_P (current_function_decl
)
14573 && (template_class_depth (DECL_CONTEXT (t
))
14574 != TMPL_ARGS_DEPTH (args
)))
14575 args
= strip_innermost_template_args (args
, 1);
14577 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14578 tsubst_decl put in the hash table. */
14579 return retrieve_specialization (t
, args
, 0);
14582 if (DECL_CONTEXT (t
))
14586 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14587 /*entering_scope=*/1);
14588 if (ctx
!= DECL_CONTEXT (t
))
14590 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14593 if (complain
& tf_error
)
14594 error ("using invalid field %qD", t
);
14595 return error_mark_node
;
14604 case FUNCTION_DECL
:
14605 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14606 r
= tsubst (t
, args
, complain
, in_decl
);
14607 else if (local_variable_p (t
)
14608 && uses_template_parms (DECL_CONTEXT (t
)))
14610 r
= retrieve_local_specialization (t
);
14611 if (r
== NULL_TREE
)
14613 /* First try name lookup to find the instantiation. */
14614 r
= lookup_name (DECL_NAME (t
));
14615 if (r
&& !is_capture_proxy (r
))
14617 /* Make sure that the one we found is the one we want. */
14618 tree ctx
= DECL_CONTEXT (t
);
14619 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
14620 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
14621 if (ctx
!= DECL_CONTEXT (r
))
14629 /* This can happen for a variable used in a
14630 late-specified return type of a local lambda, or for a
14631 local static or constant. Building a new VAR_DECL
14632 should be OK in all those cases. */
14633 r
= tsubst_decl (t
, args
, complain
);
14634 if (local_specializations
)
14635 /* Avoid infinite recursion (79640). */
14636 register_local_specialization (r
, t
);
14637 if (decl_maybe_constant_var_p (r
))
14639 /* We can't call cp_finish_decl, so handle the
14640 initializer by hand. */
14641 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14642 complain
, in_decl
);
14643 if (!processing_template_decl
)
14644 init
= maybe_constant_init (init
);
14645 if (processing_template_decl
14646 ? potential_constant_expression (init
)
14647 : reduced_constant_expression_p (init
))
14648 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14649 = TREE_CONSTANT (r
) = true;
14650 DECL_INITIAL (r
) = init
;
14652 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14653 || decl_constant_var_p (r
)
14654 || errorcount
|| sorrycount
);
14655 if (!processing_template_decl
14656 && !TREE_STATIC (r
))
14657 r
= process_outer_var_ref (r
, complain
);
14659 /* Remember this for subsequent uses. */
14660 if (local_specializations
)
14661 register_local_specialization (r
, t
);
14666 if (!mark_used (r
, complain
))
14667 return error_mark_node
;
14670 case NAMESPACE_DECL
:
14674 /* An OVERLOAD will always be a non-dependent overload set; an
14675 overload set from function scope will just be represented with an
14676 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14677 gcc_assert (!uses_template_parms (t
));
14678 /* We must have marked any lookups as persistent. */
14679 gcc_assert (!OVL_LOOKUP_P (t
) || OVL_USED_P (t
));
14683 return tsubst_baselink (t
, current_nonlambda_class_type (),
14684 args
, complain
, in_decl
);
14686 case TEMPLATE_DECL
:
14687 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14688 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14689 args
, complain
, in_decl
);
14690 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14691 return tsubst (t
, args
, complain
, in_decl
);
14692 else if (DECL_CLASS_SCOPE_P (t
)
14693 && uses_template_parms (DECL_CONTEXT (t
)))
14695 /* Template template argument like the following example need
14698 template <template <class> class TT> struct C {};
14699 template <class T> struct D {
14700 template <class U> struct E {};
14705 We are processing the template argument `E' in #1 for
14706 the template instantiation #2. Originally, `E' is a
14707 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14708 have to substitute this with one having context `D<int>'. */
14710 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14711 if (dependent_scope_p (context
))
14713 /* When rewriting a constructor into a deduction guide, a
14714 non-dependent name can become dependent, so memtmpl<args>
14715 becomes context::template memtmpl<args>. */
14716 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14717 return build_qualified_name (type
, context
, DECL_NAME (t
),
14720 return lookup_field (context
, DECL_NAME(t
), 0, false);
14723 /* Ordinary template template argument. */
14727 case REINTERPRET_CAST_EXPR
:
14728 case CONST_CAST_EXPR
:
14729 case STATIC_CAST_EXPR
:
14730 case DYNAMIC_CAST_EXPR
:
14731 case IMPLICIT_CONV_EXPR
:
14735 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14736 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14737 return build1 (code
, type
, op0
);
14741 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14742 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14744 tree expanded
, op
= TREE_OPERAND (t
, 0);
14747 if (SIZEOF_EXPR_TYPE_P (t
))
14748 op
= TREE_TYPE (op
);
14750 ++cp_unevaluated_operand
;
14751 ++c_inhibit_evaluation_warnings
;
14752 /* We only want to compute the number of arguments. */
14753 if (PACK_EXPANSION_P (op
))
14754 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14756 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14757 args
, complain
, in_decl
);
14758 --cp_unevaluated_operand
;
14759 --c_inhibit_evaluation_warnings
;
14761 if (TREE_CODE (expanded
) == TREE_VEC
)
14763 len
= TREE_VEC_LENGTH (expanded
);
14764 /* Set TREE_USED for the benefit of -Wunused. */
14765 for (int i
= 0; i
< len
; i
++)
14766 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14767 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14770 if (expanded
== error_mark_node
)
14771 return error_mark_node
;
14772 else if (PACK_EXPANSION_P (expanded
)
14773 || (TREE_CODE (expanded
) == TREE_VEC
14774 && pack_expansion_args_count (expanded
)))
14777 if (PACK_EXPANSION_P (expanded
))
14779 else if (TREE_VEC_LENGTH (expanded
) == 1)
14780 expanded
= TREE_VEC_ELT (expanded
, 0);
14782 expanded
= make_argument_pack (expanded
);
14784 if (TYPE_P (expanded
))
14785 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14786 complain
& tf_error
);
14788 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14789 complain
& tf_error
);
14792 return build_int_cst (size_type_node
, len
);
14794 if (SIZEOF_EXPR_TYPE_P (t
))
14796 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14797 args
, complain
, in_decl
);
14798 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14799 r
= build1 (SIZEOF_EXPR
,
14800 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14801 SIZEOF_EXPR_TYPE_P (r
) = 1;
14808 case TRUTH_NOT_EXPR
:
14811 case UNARY_PLUS_EXPR
: /* Unary + */
14813 case AT_ENCODE_EXPR
:
14817 case REALPART_EXPR
:
14818 case IMAGPART_EXPR
:
14821 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14822 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14823 return build1 (code
, type
, op0
);
14826 case COMPONENT_REF
:
14831 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14832 name
= TREE_OPERAND (t
, 1);
14833 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14835 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14836 complain
, in_decl
);
14837 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14839 else if (TREE_CODE (name
) == SCOPE_REF
14840 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14842 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14843 complain
, in_decl
);
14844 name
= TREE_OPERAND (name
, 1);
14845 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14846 complain
, in_decl
);
14847 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14848 name
= build_qualified_name (/*type=*/NULL_TREE
,
14850 /*template_p=*/false);
14852 else if (BASELINK_P (name
))
14853 name
= tsubst_baselink (name
,
14854 non_reference (TREE_TYPE (object
)),
14858 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14859 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14865 case TRUNC_DIV_EXPR
:
14866 case CEIL_DIV_EXPR
:
14867 case FLOOR_DIV_EXPR
:
14868 case ROUND_DIV_EXPR
:
14869 case EXACT_DIV_EXPR
:
14873 case TRUNC_MOD_EXPR
:
14874 case FLOOR_MOD_EXPR
:
14875 case TRUTH_ANDIF_EXPR
:
14876 case TRUTH_ORIF_EXPR
:
14877 case TRUTH_AND_EXPR
:
14878 case TRUTH_OR_EXPR
:
14891 case COMPOUND_EXPR
:
14894 case PREDECREMENT_EXPR
:
14895 case PREINCREMENT_EXPR
:
14896 case POSTDECREMENT_EXPR
:
14897 case POSTINCREMENT_EXPR
:
14899 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14900 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14901 return build_nt (code
, op0
, op1
);
14906 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14907 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14908 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14909 QUALIFIED_NAME_IS_TEMPLATE (t
));
14914 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14915 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14916 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14921 int n
= VL_EXP_OPERAND_LENGTH (t
);
14922 tree result
= build_vl_exp (CALL_EXPR
, n
);
14924 for (i
= 0; i
< n
; i
++)
14925 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14926 complain
, in_decl
);
14932 case PSEUDO_DTOR_EXPR
:
14933 case VEC_PERM_EXPR
:
14935 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14936 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14937 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14938 r
= build_nt (code
, op0
, op1
, op2
);
14939 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14945 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14946 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14947 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14948 r
= build_nt (code
, op0
, op1
, op2
);
14949 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14955 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14956 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14957 r
= build_nt (code
, op0
, op1
);
14958 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14959 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14963 case TEMPLATE_ID_EXPR
:
14965 /* Substituted template arguments */
14966 tree fn
= TREE_OPERAND (t
, 0);
14967 tree targs
= TREE_OPERAND (t
, 1);
14969 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14971 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14973 return lookup_template_function (fn
, targs
);
14978 tree purpose
, value
, chain
;
14980 if (t
== void_list_node
)
14983 purpose
= TREE_PURPOSE (t
);
14985 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14986 value
= TREE_VALUE (t
);
14988 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14989 chain
= TREE_CHAIN (t
);
14990 if (chain
&& chain
!= void_type_node
)
14991 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14992 if (purpose
== TREE_PURPOSE (t
)
14993 && value
== TREE_VALUE (t
)
14994 && chain
== TREE_CHAIN (t
))
14996 return tree_cons (purpose
, value
, chain
);
15001 case ENUMERAL_TYPE
:
15003 case TEMPLATE_TYPE_PARM
:
15004 case TEMPLATE_TEMPLATE_PARM
:
15005 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15006 case TEMPLATE_PARM_INDEX
:
15008 case REFERENCE_TYPE
:
15010 case FUNCTION_TYPE
:
15013 case TYPENAME_TYPE
:
15014 case UNBOUND_CLASS_TEMPLATE
:
15016 case DECLTYPE_TYPE
:
15018 return tsubst (t
, args
, complain
, in_decl
);
15022 /* Fall through. */
15023 case IDENTIFIER_NODE
:
15024 if (IDENTIFIER_TYPENAME_P (t
))
15026 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15027 return mangle_conv_op_name_for_type (new_type
);
15033 /* This is handled by tsubst_copy_and_build. */
15034 gcc_unreachable ();
15038 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15039 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15040 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
15043 case CLEANUP_POINT_EXPR
:
15044 /* We shouldn't have built any of these during initial template
15045 generation. Instead, they should be built during instantiation
15046 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15047 gcc_unreachable ();
15051 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15052 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
15053 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
15054 r
= build2 (code
, type
, op0
, op1
);
15055 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
15056 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
15057 && !(complain
& tf_error
))
15058 return error_mark_node
;
15062 case EXPR_PACK_EXPANSION
:
15063 error ("invalid use of pack expansion expression");
15064 return error_mark_node
;
15066 case NONTYPE_ARGUMENT_PACK
:
15067 error ("use %<...%> to expand argument pack");
15068 return error_mark_node
;
15071 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
15079 /* Instantiate any typedefs in the type. */
15080 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
15081 r
= fold_convert (type
, t
);
15082 gcc_assert (TREE_CODE (r
) == code
);
15087 /* These can sometimes show up in a partial instantiation, but never
15088 involve template parms. */
15089 gcc_assert (!uses_template_parms (t
));
15092 case UNARY_LEFT_FOLD_EXPR
:
15093 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
15094 case UNARY_RIGHT_FOLD_EXPR
:
15095 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
15096 case BINARY_LEFT_FOLD_EXPR
:
15097 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
15098 case BINARY_RIGHT_FOLD_EXPR
:
15099 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
15102 /* We shouldn't get here, but keep going if !flag_checking. */
15104 gcc_unreachable ();
15109 /* Helper function for tsubst_omp_clauses, used for instantiation of
15110 OMP_CLAUSE_DECL of clauses. */
15113 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
15116 if (decl
== NULL_TREE
)
15119 /* Handle an OpenMP array section represented as a TREE_LIST (or
15120 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15121 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15122 TREE_LIST. We can handle it exactly the same as an array section
15123 (purpose, value, and a chain), even though the nomenclature
15124 (low_bound, length, etc) is different. */
15125 if (TREE_CODE (decl
) == TREE_LIST
)
15128 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
15129 /*integral_constant_expression_p=*/false);
15130 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
15131 /*integral_constant_expression_p=*/false);
15132 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
15134 if (TREE_PURPOSE (decl
) == low_bound
15135 && TREE_VALUE (decl
) == length
15136 && TREE_CHAIN (decl
) == chain
)
15138 tree ret
= tree_cons (low_bound
, length
, chain
);
15139 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
15140 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
15143 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
15144 /*integral_constant_expression_p=*/false);
15145 /* Undo convert_from_reference tsubst_expr could have called. */
15147 && REFERENCE_REF_P (ret
)
15148 && !REFERENCE_REF_P (decl
))
15149 ret
= TREE_OPERAND (ret
, 0);
15153 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15156 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
15157 tree args
, tsubst_flags_t complain
, tree in_decl
)
15159 tree new_clauses
= NULL_TREE
, nc
, oc
;
15160 tree linear_no_step
= NULL_TREE
;
15162 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
15164 nc
= copy_node (oc
);
15165 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
15168 switch (OMP_CLAUSE_CODE (nc
))
15170 case OMP_CLAUSE_LASTPRIVATE
:
15171 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
15173 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
15174 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
15175 in_decl
, /*integral_constant_expression_p=*/false);
15176 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
15177 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
15180 case OMP_CLAUSE_PRIVATE
:
15181 case OMP_CLAUSE_SHARED
:
15182 case OMP_CLAUSE_FIRSTPRIVATE
:
15183 case OMP_CLAUSE_COPYIN
:
15184 case OMP_CLAUSE_COPYPRIVATE
:
15185 case OMP_CLAUSE_UNIFORM
:
15186 case OMP_CLAUSE_DEPEND
:
15187 case OMP_CLAUSE_FROM
:
15188 case OMP_CLAUSE_TO
:
15189 case OMP_CLAUSE_MAP
:
15190 case OMP_CLAUSE_USE_DEVICE_PTR
:
15191 case OMP_CLAUSE_IS_DEVICE_PTR
:
15192 OMP_CLAUSE_DECL (nc
)
15193 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15196 case OMP_CLAUSE_TILE
:
15197 case OMP_CLAUSE_IF
:
15198 case OMP_CLAUSE_NUM_THREADS
:
15199 case OMP_CLAUSE_SCHEDULE
:
15200 case OMP_CLAUSE_COLLAPSE
:
15201 case OMP_CLAUSE_FINAL
:
15202 case OMP_CLAUSE_DEVICE
:
15203 case OMP_CLAUSE_DIST_SCHEDULE
:
15204 case OMP_CLAUSE_NUM_TEAMS
:
15205 case OMP_CLAUSE_THREAD_LIMIT
:
15206 case OMP_CLAUSE_SAFELEN
:
15207 case OMP_CLAUSE_SIMDLEN
:
15208 case OMP_CLAUSE_NUM_TASKS
:
15209 case OMP_CLAUSE_GRAINSIZE
:
15210 case OMP_CLAUSE_PRIORITY
:
15211 case OMP_CLAUSE_ORDERED
:
15212 case OMP_CLAUSE_HINT
:
15213 case OMP_CLAUSE_NUM_GANGS
:
15214 case OMP_CLAUSE_NUM_WORKERS
:
15215 case OMP_CLAUSE_VECTOR_LENGTH
:
15216 case OMP_CLAUSE_WORKER
:
15217 case OMP_CLAUSE_VECTOR
:
15218 case OMP_CLAUSE_ASYNC
:
15219 case OMP_CLAUSE_WAIT
:
15220 OMP_CLAUSE_OPERAND (nc
, 0)
15221 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
15222 in_decl
, /*integral_constant_expression_p=*/false);
15224 case OMP_CLAUSE_REDUCTION
:
15225 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
15227 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
15228 if (TREE_CODE (placeholder
) == SCOPE_REF
)
15230 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
15231 complain
, in_decl
);
15232 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
15233 = build_qualified_name (NULL_TREE
, scope
,
15234 TREE_OPERAND (placeholder
, 1),
15238 gcc_assert (identifier_p (placeholder
));
15240 OMP_CLAUSE_DECL (nc
)
15241 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15244 case OMP_CLAUSE_GANG
:
15245 case OMP_CLAUSE_ALIGNED
:
15246 OMP_CLAUSE_DECL (nc
)
15247 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15249 OMP_CLAUSE_OPERAND (nc
, 1)
15250 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
15251 in_decl
, /*integral_constant_expression_p=*/false);
15253 case OMP_CLAUSE_LINEAR
:
15254 OMP_CLAUSE_DECL (nc
)
15255 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
15257 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
15259 gcc_assert (!linear_no_step
);
15260 linear_no_step
= nc
;
15262 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
15263 OMP_CLAUSE_LINEAR_STEP (nc
)
15264 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
15265 complain
, in_decl
);
15267 OMP_CLAUSE_LINEAR_STEP (nc
)
15268 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
15270 /*integral_constant_expression_p=*/false);
15272 case OMP_CLAUSE_NOWAIT
:
15273 case OMP_CLAUSE_DEFAULT
:
15274 case OMP_CLAUSE_UNTIED
:
15275 case OMP_CLAUSE_MERGEABLE
:
15276 case OMP_CLAUSE_INBRANCH
:
15277 case OMP_CLAUSE_NOTINBRANCH
:
15278 case OMP_CLAUSE_PROC_BIND
:
15279 case OMP_CLAUSE_FOR
:
15280 case OMP_CLAUSE_PARALLEL
:
15281 case OMP_CLAUSE_SECTIONS
:
15282 case OMP_CLAUSE_TASKGROUP
:
15283 case OMP_CLAUSE_NOGROUP
:
15284 case OMP_CLAUSE_THREADS
:
15285 case OMP_CLAUSE_SIMD
:
15286 case OMP_CLAUSE_DEFAULTMAP
:
15287 case OMP_CLAUSE_INDEPENDENT
:
15288 case OMP_CLAUSE_AUTO
:
15289 case OMP_CLAUSE_SEQ
:
15292 gcc_unreachable ();
15294 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
15295 switch (OMP_CLAUSE_CODE (nc
))
15297 case OMP_CLAUSE_SHARED
:
15298 case OMP_CLAUSE_PRIVATE
:
15299 case OMP_CLAUSE_FIRSTPRIVATE
:
15300 case OMP_CLAUSE_LASTPRIVATE
:
15301 case OMP_CLAUSE_COPYPRIVATE
:
15302 case OMP_CLAUSE_LINEAR
:
15303 case OMP_CLAUSE_REDUCTION
:
15304 case OMP_CLAUSE_USE_DEVICE_PTR
:
15305 case OMP_CLAUSE_IS_DEVICE_PTR
:
15306 /* tsubst_expr on SCOPE_REF results in returning
15307 finish_non_static_data_member result. Undo that here. */
15308 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
15309 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
15310 == IDENTIFIER_NODE
))
15312 tree t
= OMP_CLAUSE_DECL (nc
);
15315 switch (TREE_CODE (v
))
15317 case COMPONENT_REF
:
15321 case POINTER_PLUS_EXPR
:
15322 v
= TREE_OPERAND (v
, 0);
15325 if (DECL_CONTEXT (v
) == current_function_decl
15326 && DECL_ARTIFICIAL (v
)
15327 && DECL_NAME (v
) == this_identifier
)
15328 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
15335 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
15336 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
15337 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
15338 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
15339 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
15341 tree decl
= OMP_CLAUSE_DECL (nc
);
15344 retrofit_lang_decl (decl
);
15345 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
15354 new_clauses
= nreverse (new_clauses
);
15355 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
15357 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
15358 if (linear_no_step
)
15359 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15360 if (nc
== linear_no_step
)
15362 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15366 return new_clauses
;
15369 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15372 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15375 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15377 tree purpose
, value
, chain
;
15382 if (TREE_CODE (t
) != TREE_LIST
)
15383 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15384 /*function_p=*/false,
15385 /*integral_constant_expression_p=*/false);
15387 if (t
== void_list_node
)
15390 purpose
= TREE_PURPOSE (t
);
15392 purpose
= RECUR (purpose
);
15393 value
= TREE_VALUE (t
);
15396 if (TREE_CODE (value
) != LABEL_DECL
)
15397 value
= RECUR (value
);
15400 value
= lookup_label (DECL_NAME (value
));
15401 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15402 TREE_USED (value
) = 1;
15405 chain
= TREE_CHAIN (t
);
15406 if (chain
&& chain
!= void_type_node
)
15407 chain
= RECUR (chain
);
15408 return tree_cons (purpose
, value
, chain
);
15412 /* Used to temporarily communicate the list of #pragma omp parallel
15413 clauses to #pragma omp for instantiation if they are combined
15416 static tree
*omp_parallel_combined_clauses
;
15418 /* Substitute one OMP_FOR iterator. */
15421 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15422 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15423 tree args
, tsubst_flags_t complain
, tree in_decl
,
15424 bool integral_constant_expression_p
)
15426 #define RECUR(NODE) \
15427 tsubst_expr ((NODE), args, complain, in_decl, \
15428 integral_constant_expression_p)
15429 tree decl
, init
, cond
, incr
;
15431 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15432 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15434 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15436 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15437 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15440 decl
= TREE_OPERAND (init
, 0);
15441 init
= TREE_OPERAND (init
, 1);
15442 tree decl_expr
= NULL_TREE
;
15443 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15445 /* We need to jump through some hoops to handle declarations in the
15446 init-statement, since we might need to handle auto deduction,
15447 but we need to keep control of initialization. */
15449 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15450 decl
= tsubst_decl (decl
, args
, complain
);
15454 if (TREE_CODE (decl
) == SCOPE_REF
)
15456 decl
= RECUR (decl
);
15457 if (TREE_CODE (decl
) == COMPONENT_REF
)
15461 switch (TREE_CODE (v
))
15463 case COMPONENT_REF
:
15467 case POINTER_PLUS_EXPR
:
15468 v
= TREE_OPERAND (v
, 0);
15471 if (DECL_CONTEXT (v
) == current_function_decl
15472 && DECL_ARTIFICIAL (v
)
15473 && DECL_NAME (v
) == this_identifier
)
15475 decl
= TREE_OPERAND (decl
, 1);
15476 decl
= omp_privatize_field (decl
, false);
15486 decl
= RECUR (decl
);
15488 init
= RECUR (init
);
15490 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15491 if (auto_node
&& init
)
15493 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15495 gcc_assert (!type_dependent_expression_p (decl
));
15497 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15501 /* Declare the variable, but don't let that initialize it. */
15502 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15503 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15505 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15508 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15509 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15510 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15512 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15513 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15514 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15515 NOP_EXPR
, rhs
, complain
);
15518 incr
= RECUR (incr
);
15519 TREE_VEC_ELT (declv
, i
) = decl
;
15520 TREE_VEC_ELT (initv
, i
) = init
;
15521 TREE_VEC_ELT (condv
, i
) = cond
;
15522 TREE_VEC_ELT (incrv
, i
) = incr
;
15528 /* Declare and initialize the variable. */
15536 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15538 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15540 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15541 && OMP_CLAUSE_DECL (*pc
) == decl
)
15543 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15544 && OMP_CLAUSE_DECL (*pc
) == decl
)
15548 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15550 *pc
= OMP_CLAUSE_CHAIN (c
);
15551 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15554 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15555 && OMP_CLAUSE_DECL (*pc
) == decl
)
15557 error ("iteration variable %qD should not be firstprivate",
15559 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15561 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15562 && OMP_CLAUSE_DECL (*pc
) == decl
)
15564 error ("iteration variable %qD should not be reduction",
15566 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15569 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15574 if (*pc
== NULL_TREE
)
15576 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15577 OMP_CLAUSE_DECL (c
) = decl
;
15578 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15581 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15586 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15587 if (COMPARISON_CLASS_P (cond
))
15589 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15590 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15591 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15594 cond
= RECUR (cond
);
15595 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15596 switch (TREE_CODE (incr
))
15598 case PREINCREMENT_EXPR
:
15599 case PREDECREMENT_EXPR
:
15600 case POSTINCREMENT_EXPR
:
15601 case POSTDECREMENT_EXPR
:
15602 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15603 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15606 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15607 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15609 tree rhs
= TREE_OPERAND (incr
, 1);
15610 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15611 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15612 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15613 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15614 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15618 incr
= RECUR (incr
);
15621 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15622 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15624 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15625 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15626 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15627 TREE_TYPE (decl
), lhs
,
15628 RECUR (TREE_OPERAND (incr
, 2))));
15630 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15631 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15632 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15634 tree rhs
= TREE_OPERAND (incr
, 2);
15635 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15636 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15637 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15638 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15639 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15643 incr
= RECUR (incr
);
15646 incr
= RECUR (incr
);
15650 TREE_VEC_ELT (declv
, i
) = decl
;
15651 TREE_VEC_ELT (initv
, i
) = init
;
15652 TREE_VEC_ELT (condv
, i
) = cond
;
15653 TREE_VEC_ELT (incrv
, i
) = incr
;
15657 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15658 of OMP_TARGET's body. */
15661 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15663 *walk_subtrees
= 0;
15664 switch (TREE_CODE (*tp
))
15669 case STATEMENT_LIST
:
15670 *walk_subtrees
= 1;
15678 /* Helper function for tsubst_expr. For decomposition declaration
15679 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15680 also the corresponding decls representing the identifiers
15681 of the decomposition declaration. Return DECL if successful
15682 or error_mark_node otherwise, set *FIRST to the first decl
15683 in the list chained through DECL_CHAIN and *CNT to the number
15687 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15688 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15691 tree decl2
, decl3
, prev
= decl
;
15693 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15694 for (decl2
= DECL_CHAIN (pattern_decl
);
15697 && DECL_DECOMPOSITION_P (decl2
)
15698 && DECL_NAME (decl2
);
15699 decl2
= DECL_CHAIN (decl2
))
15701 if (TREE_TYPE (decl2
) == error_mark_node
&& *cnt
== 0)
15703 gcc_assert (errorcount
);
15704 return error_mark_node
;
15707 gcc_assert (DECL_DECOMP_BASE (decl2
) == pattern_decl
);
15708 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15709 tree v
= DECL_VALUE_EXPR (decl2
);
15710 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15711 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15712 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15713 SET_DECL_VALUE_EXPR (decl2
, v
);
15714 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15716 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15717 maybe_push_decl (decl3
);
15718 if (error_operand_p (decl3
))
15719 decl
= error_mark_node
;
15720 else if (decl
!= error_mark_node
15721 && DECL_CHAIN (decl3
) != prev
)
15723 gcc_assert (errorcount
);
15724 decl
= error_mark_node
;
15733 /* Like tsubst_copy for expressions, etc. but also does semantic
15737 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15738 bool integral_constant_expression_p
)
15740 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15741 #define RECUR(NODE) \
15742 tsubst_expr ((NODE), args, complain, in_decl, \
15743 integral_constant_expression_p)
15749 if (t
== NULL_TREE
|| t
== error_mark_node
)
15752 loc
= input_location
;
15753 if (EXPR_HAS_LOCATION (t
))
15754 input_location
= EXPR_LOCATION (t
);
15755 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15756 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15758 switch (TREE_CODE (t
))
15760 case STATEMENT_LIST
:
15762 tree_stmt_iterator i
;
15763 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15764 RECUR (tsi_stmt (i
));
15768 case CTOR_INITIALIZER
:
15769 finish_mem_initializers (tsubst_initializer_list
15770 (TREE_OPERAND (t
, 0), args
));
15774 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
15778 tmp
= RECUR (EXPR_STMT_EXPR (t
));
15779 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
15780 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
15782 finish_expr_stmt (tmp
);
15786 finish_local_using_directive (USING_STMT_NAMESPACE (t
),
15787 /*attribs=*/NULL_TREE
);
15792 tree decl
, pattern_decl
;
15795 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15796 if (TREE_CODE (decl
) == LABEL_DECL
)
15797 finish_label_decl (DECL_NAME (decl
));
15798 else if (TREE_CODE (decl
) == USING_DECL
)
15800 tree scope
= USING_DECL_SCOPE (decl
);
15801 tree name
= DECL_NAME (decl
);
15803 scope
= tsubst (scope
, args
, complain
, in_decl
);
15804 decl
= lookup_qualified_name (scope
, name
,
15805 /*is_type_p=*/false,
15806 /*complain=*/false);
15807 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15808 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15810 finish_local_using_decl (decl
, scope
, name
);
15812 else if (DECL_PACK_P (decl
))
15814 /* Don't build up decls for a variadic capture proxy, we'll
15815 instantiate the elements directly as needed. */
15820 init
= DECL_INITIAL (decl
);
15821 decl
= tsubst (decl
, args
, complain
, in_decl
);
15822 if (decl
!= error_mark_node
)
15824 /* By marking the declaration as instantiated, we avoid
15825 trying to instantiate it. Since instantiate_decl can't
15826 handle local variables, and since we've already done
15827 all that needs to be done, that's the right thing to
15830 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15832 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15833 /* Anonymous aggregates are a special case. */
15834 finish_anon_union (decl
);
15835 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15837 DECL_CONTEXT (decl
) = current_function_decl
;
15838 if (DECL_NAME (decl
) == this_identifier
)
15840 tree lam
= DECL_CONTEXT (current_function_decl
);
15841 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15842 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15844 insert_capture_proxy (decl
);
15846 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15847 /* We already did a pushtag. */;
15848 else if (TREE_CODE (decl
) == FUNCTION_DECL
15849 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15850 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15852 DECL_CONTEXT (decl
) = NULL_TREE
;
15854 DECL_CONTEXT (decl
) = current_function_decl
;
15855 cp_check_omp_declare_reduction (decl
);
15859 int const_init
= false;
15860 maybe_push_decl (decl
);
15862 && DECL_PRETTY_FUNCTION_P (decl
))
15864 /* For __PRETTY_FUNCTION__ we have to adjust the
15866 const char *const name
15867 = cxx_printable_name (current_function_decl
, 2);
15868 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15871 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15874 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15876 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15878 && DECL_DECOMPOSITION_P (decl
)
15879 && TREE_TYPE (pattern_decl
) != error_mark_node
)
15883 decl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
15884 complain
, in_decl
, &first
,
15886 if (decl
!= error_mark_node
)
15887 cp_finish_decomp (decl
, first
, cnt
);
15897 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15898 RECUR (FOR_INIT_STMT (t
));
15899 finish_init_stmt (stmt
);
15900 tmp
= RECUR (FOR_COND (t
));
15901 finish_for_cond (tmp
, stmt
, false);
15902 tmp
= RECUR (FOR_EXPR (t
));
15903 finish_for_expr (tmp
, stmt
);
15904 RECUR (FOR_BODY (t
));
15905 finish_for_stmt (stmt
);
15908 case RANGE_FOR_STMT
:
15911 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15912 decl
= RANGE_FOR_DECL (t
);
15913 decl
= tsubst (decl
, args
, complain
, in_decl
);
15914 maybe_push_decl (decl
);
15915 expr
= RECUR (RANGE_FOR_EXPR (t
));
15916 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
15920 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
15921 complain
, in_decl
, &first
, &cnt
);
15922 stmt
= cp_convert_range_for (stmt
, decl
, expr
, first
, cnt
,
15923 RANGE_FOR_IVDEP (t
));
15926 stmt
= cp_convert_range_for (stmt
, decl
, expr
, NULL_TREE
, 0,
15927 RANGE_FOR_IVDEP (t
));
15928 RECUR (RANGE_FOR_BODY (t
));
15929 finish_for_stmt (stmt
);
15934 stmt
= begin_while_stmt ();
15935 tmp
= RECUR (WHILE_COND (t
));
15936 finish_while_stmt_cond (tmp
, stmt
, false);
15937 RECUR (WHILE_BODY (t
));
15938 finish_while_stmt (stmt
);
15942 stmt
= begin_do_stmt ();
15943 RECUR (DO_BODY (t
));
15944 finish_do_body (stmt
);
15945 tmp
= RECUR (DO_COND (t
));
15946 finish_do_stmt (tmp
, stmt
, false);
15950 stmt
= begin_if_stmt ();
15951 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
15952 tmp
= RECUR (IF_COND (t
));
15953 tmp
= finish_if_stmt_cond (tmp
, stmt
);
15954 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
15955 /* Don't instantiate the THEN_CLAUSE. */;
15958 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
15960 ++c_inhibit_evaluation_warnings
;
15961 RECUR (THEN_CLAUSE (t
));
15963 --c_inhibit_evaluation_warnings
;
15965 finish_then_clause (stmt
);
15967 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
15968 /* Don't instantiate the ELSE_CLAUSE. */;
15969 else if (ELSE_CLAUSE (t
))
15971 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
15972 begin_else_clause (stmt
);
15974 ++c_inhibit_evaluation_warnings
;
15975 RECUR (ELSE_CLAUSE (t
));
15977 --c_inhibit_evaluation_warnings
;
15978 finish_else_clause (stmt
);
15981 finish_if_stmt (stmt
);
15985 if (BIND_EXPR_BODY_BLOCK (t
))
15986 stmt
= begin_function_body ();
15988 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15989 ? BCS_TRY_BLOCK
: 0);
15991 RECUR (BIND_EXPR_BODY (t
));
15993 if (BIND_EXPR_BODY_BLOCK (t
))
15994 finish_function_body (stmt
);
15996 finish_compound_stmt (stmt
);
16000 finish_break_stmt ();
16003 case CONTINUE_STMT
:
16004 finish_continue_stmt ();
16008 stmt
= begin_switch_stmt ();
16009 tmp
= RECUR (SWITCH_STMT_COND (t
));
16010 finish_switch_cond (tmp
, stmt
);
16011 RECUR (SWITCH_STMT_BODY (t
));
16012 finish_switch_stmt (stmt
);
16015 case CASE_LABEL_EXPR
:
16017 tree low
= RECUR (CASE_LOW (t
));
16018 tree high
= RECUR (CASE_HIGH (t
));
16019 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
16020 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
16021 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
16022 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
16028 tree decl
= LABEL_EXPR_LABEL (t
);
16031 label
= finish_label_stmt (DECL_NAME (decl
));
16032 if (TREE_CODE (label
) == LABEL_DECL
)
16033 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
16034 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
16035 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
16040 tmp
= GOTO_DESTINATION (t
);
16041 if (TREE_CODE (tmp
) != LABEL_DECL
)
16042 /* Computed goto's must be tsubst'd into. On the other hand,
16043 non-computed gotos must not be; the identifier in question
16044 will have no binding. */
16047 tmp
= DECL_NAME (tmp
);
16048 finish_goto_stmt (tmp
);
16053 tree string
= RECUR (ASM_STRING (t
));
16054 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
16055 complain
, in_decl
);
16056 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
16057 complain
, in_decl
);
16058 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
16059 complain
, in_decl
);
16060 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
16061 complain
, in_decl
);
16062 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
16064 tree asm_expr
= tmp
;
16065 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
16066 asm_expr
= TREE_OPERAND (asm_expr
, 0);
16067 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
16074 stmt
= begin_try_block ();
16075 RECUR (TRY_STMTS (t
));
16076 finish_cleanup_try_block (stmt
);
16077 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
16081 tree compound_stmt
= NULL_TREE
;
16083 if (FN_TRY_BLOCK_P (t
))
16084 stmt
= begin_function_try_block (&compound_stmt
);
16086 stmt
= begin_try_block ();
16088 RECUR (TRY_STMTS (t
));
16090 if (FN_TRY_BLOCK_P (t
))
16091 finish_function_try_block (stmt
);
16093 finish_try_block (stmt
);
16095 RECUR (TRY_HANDLERS (t
));
16096 if (FN_TRY_BLOCK_P (t
))
16097 finish_function_handler_sequence (stmt
, compound_stmt
);
16099 finish_handler_sequence (stmt
);
16105 tree decl
= HANDLER_PARMS (t
);
16109 decl
= tsubst (decl
, args
, complain
, in_decl
);
16110 /* Prevent instantiate_decl from trying to instantiate
16111 this variable. We've already done all that needs to be
16113 if (decl
!= error_mark_node
)
16114 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
16116 stmt
= begin_handler ();
16117 finish_handler_parms (decl
, stmt
);
16118 RECUR (HANDLER_BODY (t
));
16119 finish_handler (stmt
);
16124 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
16125 if (CLASS_TYPE_P (tmp
))
16127 /* Local classes are not independent templates; they are
16128 instantiated along with their containing function. And this
16129 way we don't have to deal with pushing out of one local class
16130 to instantiate a member of another local class. */
16132 /* Closures are handled by the LAMBDA_EXPR. */
16133 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
16134 complete_type (tmp
);
16135 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
16136 if (!DECL_ARTIFICIAL (fn
))
16137 instantiate_decl (fn
, /*defer_ok=*/false,
16138 /*expl_inst_class=*/false);
16142 case STATIC_ASSERT
:
16146 ++c_inhibit_evaluation_warnings
;
16148 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
16151 /*integral_constant_expression_p=*/true);
16152 --c_inhibit_evaluation_warnings
;
16154 finish_static_assert (condition
,
16155 STATIC_ASSERT_MESSAGE (t
),
16156 STATIC_ASSERT_SOURCE_LOCATION (t
),
16157 /*member_p=*/false);
16162 case OACC_PARALLEL
:
16163 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
16165 stmt
= begin_omp_parallel ();
16166 RECUR (OMP_BODY (t
));
16167 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
16171 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
16172 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
16173 complain
, in_decl
);
16174 if (OMP_PARALLEL_COMBINED (t
))
16175 omp_parallel_combined_clauses
= &tmp
;
16176 stmt
= begin_omp_parallel ();
16177 RECUR (OMP_PARALLEL_BODY (t
));
16178 gcc_assert (omp_parallel_combined_clauses
== NULL
);
16179 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
16180 = OMP_PARALLEL_COMBINED (t
);
16181 pop_omp_privatization_clauses (r
);
16185 r
= push_omp_privatization_clauses (false);
16186 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
16187 complain
, in_decl
);
16188 stmt
= begin_omp_task ();
16189 RECUR (OMP_TASK_BODY (t
));
16190 finish_omp_task (tmp
, stmt
);
16191 pop_omp_privatization_clauses (r
);
16198 case OMP_DISTRIBUTE
:
16202 tree clauses
, body
, pre_body
;
16203 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
16204 tree orig_declv
= NULL_TREE
;
16205 tree incrv
= NULL_TREE
;
16206 enum c_omp_region_type ort
= C_ORT_OMP
;
16209 if (TREE_CODE (t
) == CILK_SIMD
|| TREE_CODE (t
) == CILK_FOR
)
16211 else if (TREE_CODE (t
) == OACC_LOOP
)
16214 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
16215 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
16217 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16219 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16220 if (OMP_FOR_ORIG_DECLS (t
))
16221 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16222 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16223 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16224 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
16227 stmt
= begin_omp_structured_block ();
16229 pre_body
= push_stmt_list ();
16230 RECUR (OMP_FOR_PRE_BODY (t
));
16231 pre_body
= pop_stmt_list (pre_body
);
16233 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16234 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
16235 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
16236 incrv
, &clauses
, args
, complain
, in_decl
,
16237 integral_constant_expression_p
);
16238 omp_parallel_combined_clauses
= NULL
;
16240 body
= push_stmt_list ();
16241 RECUR (OMP_FOR_BODY (t
));
16242 body
= pop_stmt_list (body
);
16244 if (OMP_FOR_INIT (t
) != NULL_TREE
)
16245 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
16246 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
16250 t
= make_node (TREE_CODE (t
));
16251 TREE_TYPE (t
) = void_type_node
;
16252 OMP_FOR_BODY (t
) = body
;
16253 OMP_FOR_PRE_BODY (t
) = pre_body
;
16254 OMP_FOR_CLAUSES (t
) = clauses
;
16255 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
16259 add_stmt (finish_omp_structured_block (stmt
));
16260 pop_omp_privatization_clauses (r
);
16265 omp_parallel_combined_clauses
= NULL
;
16270 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
16271 && OMP_TEAMS_COMBINED (t
));
16272 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
16274 stmt
= push_stmt_list ();
16275 RECUR (OMP_BODY (t
));
16276 stmt
= pop_stmt_list (stmt
);
16279 OMP_BODY (t
) = stmt
;
16280 OMP_CLAUSES (t
) = tmp
;
16282 pop_omp_privatization_clauses (r
);
16286 case OMP_TARGET_DATA
:
16288 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
16289 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
16291 keep_next_level (true);
16292 stmt
= begin_omp_structured_block ();
16294 RECUR (OMP_BODY (t
));
16295 stmt
= finish_omp_structured_block (stmt
);
16298 OMP_BODY (t
) = stmt
;
16299 OMP_CLAUSES (t
) = tmp
;
16300 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
16302 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
16305 /* For combined target teams, ensure the num_teams and
16306 thread_limit clause expressions are evaluated on the host,
16307 before entering the target construct. */
16309 for (c
= OMP_TEAMS_CLAUSES (teams
);
16310 c
; c
= OMP_CLAUSE_CHAIN (c
))
16311 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
16312 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
16313 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
16315 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
16316 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
16317 if (expr
== error_mark_node
)
16319 tmp
= TARGET_EXPR_SLOT (expr
);
16321 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
16322 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
16323 OMP_CLAUSE_FIRSTPRIVATE
);
16324 OMP_CLAUSE_DECL (tc
) = tmp
;
16325 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
16326 OMP_TARGET_CLAUSES (t
) = tc
;
16335 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
16336 complain
, in_decl
);
16337 OACC_DECLARE_CLAUSES (t
) = tmp
;
16341 case OMP_TARGET_UPDATE
:
16342 case OMP_TARGET_ENTER_DATA
:
16343 case OMP_TARGET_EXIT_DATA
:
16344 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
16345 complain
, in_decl
);
16347 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16351 case OACC_ENTER_DATA
:
16352 case OACC_EXIT_DATA
:
16354 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
16355 complain
, in_decl
);
16357 OMP_STANDALONE_CLAUSES (t
) = tmp
;
16362 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
16363 complain
, in_decl
);
16364 stmt
= push_stmt_list ();
16365 RECUR (OMP_BODY (t
));
16366 stmt
= pop_stmt_list (stmt
);
16369 OMP_BODY (t
) = stmt
;
16370 OMP_ORDERED_CLAUSES (t
) = tmp
;
16376 case OMP_TASKGROUP
:
16377 stmt
= push_stmt_list ();
16378 RECUR (OMP_BODY (t
));
16379 stmt
= pop_stmt_list (stmt
);
16382 OMP_BODY (t
) = stmt
;
16387 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16388 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16390 tree op1
= TREE_OPERAND (t
, 1);
16391 tree rhs1
= NULL_TREE
;
16393 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16395 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16396 op1
= TREE_OPERAND (op1
, 1);
16398 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16399 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16400 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16401 NULL_TREE
, NULL_TREE
, rhs1
,
16402 OMP_ATOMIC_SEQ_CST (t
));
16406 tree op1
= TREE_OPERAND (t
, 1);
16407 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16408 tree rhs1
= NULL_TREE
;
16409 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16410 enum tree_code opcode
= NOP_EXPR
;
16411 if (code
== OMP_ATOMIC_READ
)
16413 v
= RECUR (TREE_OPERAND (op1
, 0));
16414 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16416 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16417 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16419 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16420 v
= RECUR (TREE_OPERAND (op1
, 0));
16421 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16422 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16424 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16425 op11
= TREE_OPERAND (op11
, 1);
16427 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16428 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16429 opcode
= TREE_CODE (op11
);
16430 if (opcode
== MODIFY_EXPR
)
16436 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16437 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16439 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16440 OMP_ATOMIC_SEQ_CST (t
));
16444 case TRANSACTION_EXPR
:
16447 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16448 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16450 if (TRANSACTION_EXPR_IS_STMT (t
))
16452 tree body
= TRANSACTION_EXPR_BODY (t
);
16453 tree noex
= NULL_TREE
;
16454 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16456 noex
= MUST_NOT_THROW_COND (body
);
16457 if (noex
== NULL_TREE
)
16458 noex
= boolean_true_node
;
16459 body
= TREE_OPERAND (body
, 0);
16461 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16463 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16467 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16468 RECUR (TRANSACTION_EXPR_BODY (t
)),
16475 case MUST_NOT_THROW_EXPR
:
16477 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16478 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16479 RETURN (build_must_not_throw_expr (op0
, cond
));
16482 case EXPR_PACK_EXPANSION
:
16483 error ("invalid use of pack expansion expression");
16484 RETURN (error_mark_node
);
16486 case NONTYPE_ARGUMENT_PACK
:
16487 error ("use %<...%> to expand argument pack");
16488 RETURN (error_mark_node
);
16490 case CILK_SPAWN_STMT
:
16491 cfun
->calls_cilk_spawn
= 1;
16492 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
16494 case CILK_SYNC_STMT
:
16495 RETURN (build_cilk_sync ());
16497 case COMPOUND_EXPR
:
16498 tmp
= RECUR (TREE_OPERAND (t
, 0));
16499 if (tmp
== NULL_TREE
)
16500 /* If the first operand was a statement, we're done with it. */
16501 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16502 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16503 RECUR (TREE_OPERAND (t
, 1)),
16506 case ANNOTATE_EXPR
:
16507 tmp
= RECUR (TREE_OPERAND (t
, 0));
16508 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16509 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
16512 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16514 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16515 /*function_p=*/false,
16516 integral_constant_expression_p
));
16519 RETURN (NULL_TREE
);
16521 input_location
= loc
;
16527 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16528 function. For description of the body see comment above
16529 cp_parser_omp_declare_reduction_exprs. */
16532 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16534 if (t
== NULL_TREE
|| t
== error_mark_node
)
16537 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16539 tree_stmt_iterator tsi
;
16542 memset (stmts
, 0, sizeof stmts
);
16543 for (i
= 0, tsi
= tsi_start (t
);
16544 i
< 7 && !tsi_end_p (tsi
);
16545 i
++, tsi_next (&tsi
))
16546 stmts
[i
] = tsi_stmt (tsi
);
16547 gcc_assert (tsi_end_p (tsi
));
16551 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16552 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16553 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16554 args
, complain
, in_decl
);
16555 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16556 args
, complain
, in_decl
);
16557 DECL_CONTEXT (omp_out
) = current_function_decl
;
16558 DECL_CONTEXT (omp_in
) = current_function_decl
;
16559 keep_next_level (true);
16560 tree block
= begin_omp_structured_block ();
16561 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16562 block
= finish_omp_structured_block (block
);
16563 block
= maybe_cleanup_point_expr_void (block
);
16564 add_decl_expr (omp_out
);
16565 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16566 TREE_NO_WARNING (omp_out
) = 1;
16567 add_decl_expr (omp_in
);
16568 finish_expr_stmt (block
);
16572 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16573 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16574 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16575 args
, complain
, in_decl
);
16576 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16577 args
, complain
, in_decl
);
16578 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16579 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16580 keep_next_level (true);
16581 tree block
= begin_omp_structured_block ();
16582 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16583 block
= finish_omp_structured_block (block
);
16584 block
= maybe_cleanup_point_expr_void (block
);
16585 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16586 add_decl_expr (omp_priv
);
16587 add_decl_expr (omp_orig
);
16588 finish_expr_stmt (block
);
16590 add_decl_expr (omp_orig
);
16594 /* T is a postfix-expression that is not being used in a function
16595 call. Return the substituted version of T. */
16598 tsubst_non_call_postfix_expression (tree t
, tree args
,
16599 tsubst_flags_t complain
,
16602 if (TREE_CODE (t
) == SCOPE_REF
)
16603 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16604 /*done=*/false, /*address_p=*/false);
16606 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16607 /*function_p=*/false,
16608 /*integral_constant_expression_p=*/false);
16613 /* Like tsubst but deals with expressions and performs semantic
16614 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16617 tsubst_copy_and_build (tree t
,
16619 tsubst_flags_t complain
,
16622 bool integral_constant_expression_p
)
16624 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16625 #define RECUR(NODE) \
16626 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16627 /*function_p=*/false, \
16628 integral_constant_expression_p)
16633 if (t
== NULL_TREE
|| t
== error_mark_node
)
16636 loc
= input_location
;
16637 if (EXPR_HAS_LOCATION (t
))
16638 input_location
= EXPR_LOCATION (t
);
16640 /* N3276 decltype magic only applies to calls at the top level or on the
16641 right side of a comma. */
16642 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
16643 complain
&= ~tf_decltype
;
16645 switch (TREE_CODE (t
))
16649 /* Fall through. */
16650 case IDENTIFIER_NODE
:
16654 bool non_integral_constant_expression_p
;
16655 const char *error_msg
;
16657 if (IDENTIFIER_TYPENAME_P (t
))
16659 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16660 t
= mangle_conv_op_name_for_type (new_type
);
16663 /* Look up the name. */
16664 decl
= lookup_name (t
);
16666 /* By convention, expressions use ERROR_MARK_NODE to indicate
16667 failure, not NULL_TREE. */
16668 if (decl
== NULL_TREE
)
16669 decl
= error_mark_node
;
16671 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
16673 integral_constant_expression_p
,
16674 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
16675 &non_integral_constant_expression_p
,
16676 /*template_p=*/false,
16678 /*address_p=*/false,
16679 /*template_arg_p=*/false,
16684 if (!function_p
&& identifier_p (decl
))
16686 if (complain
& tf_error
)
16687 unqualified_name_lookup_error (decl
);
16688 decl
= error_mark_node
;
16693 case TEMPLATE_ID_EXPR
:
16696 tree templ
= RECUR (TREE_OPERAND (t
, 0));
16697 tree targs
= TREE_OPERAND (t
, 1);
16700 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16701 if (targs
== error_mark_node
)
16702 return error_mark_node
;
16704 if (TREE_CODE (templ
) == SCOPE_REF
)
16706 tree name
= TREE_OPERAND (templ
, 1);
16707 tree tid
= lookup_template_function (name
, targs
);
16708 TREE_OPERAND (templ
, 1) = tid
;
16712 if (variable_template_p (templ
))
16713 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
16715 if (TREE_CODE (templ
) == COMPONENT_REF
)
16717 object
= TREE_OPERAND (templ
, 0);
16718 templ
= TREE_OPERAND (templ
, 1);
16721 object
= NULL_TREE
;
16722 templ
= lookup_template_function (templ
, targs
);
16725 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
16726 object
, templ
, NULL_TREE
));
16728 RETURN (baselink_for_fns (templ
));
16733 tree r
= RECUR (TREE_OPERAND (t
, 0));
16735 if (REFERENCE_REF_P (t
))
16737 /* A type conversion to reference type will be enclosed in
16738 such an indirect ref, but the substitution of the cast
16739 will have also added such an indirect ref. */
16740 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
16741 r
= convert_from_reference (r
);
16744 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
16745 complain
|decltype_flag
);
16747 if (TREE_CODE (r
) == INDIRECT_REF
)
16748 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16755 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16756 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16757 RETURN (build_nop (type
, op0
));
16760 case IMPLICIT_CONV_EXPR
:
16762 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16763 tree expr
= RECUR (TREE_OPERAND (t
, 0));
16764 int flags
= LOOKUP_IMPLICIT
;
16765 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
16766 flags
= LOOKUP_NORMAL
;
16767 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
16773 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16774 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16775 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
16779 case REINTERPRET_CAST_EXPR
:
16780 case CONST_CAST_EXPR
:
16781 case DYNAMIC_CAST_EXPR
:
16782 case STATIC_CAST_EXPR
:
16785 tree op
, r
= NULL_TREE
;
16787 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16788 if (integral_constant_expression_p
16789 && !cast_valid_in_integral_constant_expression_p (type
))
16791 if (complain
& tf_error
)
16792 error ("a cast to a type other than an integral or "
16793 "enumeration type cannot appear in a constant-expression");
16794 RETURN (error_mark_node
);
16797 op
= RECUR (TREE_OPERAND (t
, 0));
16799 warning_sentinel
s(warn_useless_cast
);
16800 switch (TREE_CODE (t
))
16803 r
= build_functional_cast (type
, op
, complain
);
16805 case REINTERPRET_CAST_EXPR
:
16806 r
= build_reinterpret_cast (type
, op
, complain
);
16808 case CONST_CAST_EXPR
:
16809 r
= build_const_cast (type
, op
, complain
);
16811 case DYNAMIC_CAST_EXPR
:
16812 r
= build_dynamic_cast (type
, op
, complain
);
16814 case STATIC_CAST_EXPR
:
16815 r
= build_static_cast (type
, op
, complain
);
16818 gcc_unreachable ();
16824 case POSTDECREMENT_EXPR
:
16825 case POSTINCREMENT_EXPR
:
16826 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16827 args
, complain
, in_decl
);
16828 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
16829 complain
|decltype_flag
));
16831 case PREDECREMENT_EXPR
:
16832 case PREINCREMENT_EXPR
:
16836 case TRUTH_NOT_EXPR
:
16837 case UNARY_PLUS_EXPR
: /* Unary + */
16838 case REALPART_EXPR
:
16839 case IMAGPART_EXPR
:
16840 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
16841 RECUR (TREE_OPERAND (t
, 0)),
16842 complain
|decltype_flag
));
16844 case FIX_TRUNC_EXPR
:
16845 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
16849 op1
= TREE_OPERAND (t
, 0);
16850 if (TREE_CODE (op1
) == LABEL_DECL
)
16851 RETURN (finish_label_address_expr (DECL_NAME (op1
),
16852 EXPR_LOCATION (op1
)));
16853 if (TREE_CODE (op1
) == SCOPE_REF
)
16854 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16855 /*done=*/true, /*address_p=*/true);
16857 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16859 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16860 complain
|decltype_flag
));
16865 case TRUNC_DIV_EXPR
:
16866 case CEIL_DIV_EXPR
:
16867 case FLOOR_DIV_EXPR
:
16868 case ROUND_DIV_EXPR
:
16869 case EXACT_DIV_EXPR
:
16873 case TRUNC_MOD_EXPR
:
16874 case FLOOR_MOD_EXPR
:
16875 case TRUTH_ANDIF_EXPR
:
16876 case TRUTH_ORIF_EXPR
:
16877 case TRUTH_AND_EXPR
:
16878 case TRUTH_OR_EXPR
:
16894 warning_sentinel
s1(warn_type_limits
);
16895 warning_sentinel
s2(warn_div_by_zero
);
16896 warning_sentinel
s3(warn_logical_op
);
16897 warning_sentinel
s4(warn_tautological_compare
);
16898 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16899 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16900 tree r
= build_x_binary_op
16901 (input_location
, TREE_CODE (t
),
16903 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16905 : TREE_CODE (TREE_OPERAND (t
, 0))),
16907 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16909 : TREE_CODE (TREE_OPERAND (t
, 1))),
16911 complain
|decltype_flag
);
16912 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16913 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16918 case POINTER_PLUS_EXPR
:
16920 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16921 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16922 return fold_build_pointer_plus (op0
, op1
);
16926 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16927 /*address_p=*/false));
16929 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16930 args
, complain
, in_decl
);
16931 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16932 RECUR (TREE_OPERAND (t
, 1)),
16933 complain
|decltype_flag
));
16935 case ARRAY_NOTATION_REF
:
16937 tree start_index
, length
, stride
;
16938 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16939 args
, complain
, in_decl
);
16940 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16941 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16942 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16943 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16944 length
, stride
, TREE_TYPE (op1
)));
16947 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16948 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16949 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16956 op1
= TREE_OPERAND (t
, 0);
16957 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16958 op1
= TREE_TYPE (op1
);
16961 /* When there are no ARGS, we are trying to evaluate a
16962 non-dependent expression from the parser. Trying to do
16963 the substitutions may not work. */
16965 op1
= TREE_TYPE (op1
);
16969 ++cp_unevaluated_operand
;
16970 ++c_inhibit_evaluation_warnings
;
16972 op1
= tsubst (op1
, args
, complain
, in_decl
);
16974 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16975 /*function_p=*/false,
16976 /*integral_constant_expression_p=*/
16978 --cp_unevaluated_operand
;
16979 --c_inhibit_evaluation_warnings
;
16982 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16983 complain
& tf_error
);
16985 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16986 complain
& tf_error
);
16987 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16989 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16991 if (!processing_template_decl
&& TYPE_P (op1
))
16993 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16994 build1 (NOP_EXPR
, op1
, error_mark_node
));
16995 SIZEOF_EXPR_TYPE_P (r
) = 1;
16998 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16999 TREE_SIDE_EFFECTS (r
) = 0;
17000 TREE_READONLY (r
) = 1;
17002 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
17007 case AT_ENCODE_EXPR
:
17009 op1
= TREE_OPERAND (t
, 0);
17010 ++cp_unevaluated_operand
;
17011 ++c_inhibit_evaluation_warnings
;
17012 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17013 /*function_p=*/false,
17014 /*integral_constant_expression_p=*/false);
17015 --cp_unevaluated_operand
;
17016 --c_inhibit_evaluation_warnings
;
17017 RETURN (objc_build_encode_expr (op1
));
17020 case NOEXCEPT_EXPR
:
17021 op1
= TREE_OPERAND (t
, 0);
17022 ++cp_unevaluated_operand
;
17023 ++c_inhibit_evaluation_warnings
;
17024 ++cp_noexcept_operand
;
17025 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
17026 /*function_p=*/false,
17027 /*integral_constant_expression_p=*/false);
17028 --cp_unevaluated_operand
;
17029 --c_inhibit_evaluation_warnings
;
17030 --cp_noexcept_operand
;
17031 RETURN (finish_noexcept_expr (op1
, complain
));
17035 warning_sentinel
s(warn_div_by_zero
);
17036 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
17037 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
17038 tree r
= build_x_modify_expr
17039 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
17040 complain
|decltype_flag
);
17041 /* TREE_NO_WARNING must be set if either the expression was
17042 parenthesized or it uses an operator such as >>= rather
17043 than plain assignment. In the former case, it was already
17044 set and must be copied. In the latter case,
17045 build_x_modify_expr sets it and it must not be reset
17047 if (TREE_NO_WARNING (t
))
17048 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
17054 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17055 args
, complain
, in_decl
);
17056 /* Remember that there was a reference to this entity. */
17058 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
17059 RETURN (error_mark_node
);
17060 RETURN (build_x_arrow (input_location
, op1
, complain
));
17064 tree placement
= RECUR (TREE_OPERAND (t
, 0));
17065 tree init
= RECUR (TREE_OPERAND (t
, 3));
17066 vec
<tree
, va_gc
> *placement_vec
;
17067 vec
<tree
, va_gc
> *init_vec
;
17070 if (placement
== NULL_TREE
)
17071 placement_vec
= NULL
;
17074 placement_vec
= make_tree_vector ();
17075 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
17076 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
17079 /* If there was an initializer in the original tree, but it
17080 instantiated to an empty list, then we should pass a
17081 non-NULL empty vector to tell build_new that it was an
17082 empty initializer() rather than no initializer. This can
17083 only happen when the initializer is a pack expansion whose
17084 parameter packs are of length zero. */
17085 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
17089 init_vec
= make_tree_vector ();
17090 if (init
== void_node
)
17091 gcc_assert (init_vec
!= NULL
);
17094 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
17095 vec_safe_push (init_vec
, TREE_VALUE (init
));
17099 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
17100 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17101 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
17102 NEW_EXPR_USE_GLOBAL (t
),
17105 if (placement_vec
!= NULL
)
17106 release_tree_vector (placement_vec
);
17107 if (init_vec
!= NULL
)
17108 release_tree_vector (init_vec
);
17115 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17116 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17117 RETURN (delete_sanity (op0
, op1
,
17118 DELETE_EXPR_USE_VEC (t
),
17119 DELETE_EXPR_USE_GLOBAL (t
),
17123 case COMPOUND_EXPR
:
17125 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
17126 complain
& ~tf_decltype
, in_decl
,
17127 /*function_p=*/false,
17128 integral_constant_expression_p
);
17129 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
17131 RECUR (TREE_OPERAND (t
, 1)),
17132 complain
|decltype_flag
));
17138 vec
<tree
, va_gc
> *call_args
;
17139 unsigned int nargs
, i
;
17144 function
= CALL_EXPR_FN (t
);
17145 /* Internal function with no arguments. */
17146 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
17149 /* When we parsed the expression, we determined whether or
17150 not Koenig lookup should be performed. */
17151 koenig_p
= KOENIG_LOOKUP_P (t
);
17152 if (function
== NULL_TREE
)
17155 qualified_p
= false;
17157 else if (TREE_CODE (function
) == SCOPE_REF
)
17159 qualified_p
= true;
17160 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
17162 /*address_p=*/false);
17164 else if (koenig_p
&& identifier_p (function
))
17166 /* Do nothing; calling tsubst_copy_and_build on an identifier
17167 would incorrectly perform unqualified lookup again.
17169 Note that we can also have an IDENTIFIER_NODE if the earlier
17170 unqualified lookup found a member function; in that case
17171 koenig_p will be false and we do want to do the lookup
17172 again to find the instantiated member function.
17174 FIXME but doing that causes c++/15272, so we need to stop
17175 using IDENTIFIER_NODE in that situation. */
17176 qualified_p
= false;
17180 if (TREE_CODE (function
) == COMPONENT_REF
)
17182 tree op
= TREE_OPERAND (function
, 1);
17184 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
17185 || (BASELINK_P (op
)
17186 && BASELINK_QUALIFIED_P (op
)));
17189 qualified_p
= false;
17191 if (TREE_CODE (function
) == ADDR_EXPR
17192 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
17193 /* Avoid error about taking the address of a constructor. */
17194 function
= TREE_OPERAND (function
, 0);
17196 function
= tsubst_copy_and_build (function
, args
, complain
,
17199 integral_constant_expression_p
);
17201 if (BASELINK_P (function
))
17202 qualified_p
= true;
17205 nargs
= call_expr_nargs (t
);
17206 call_args
= make_tree_vector ();
17207 for (i
= 0; i
< nargs
; ++i
)
17209 tree arg
= CALL_EXPR_ARG (t
, i
);
17211 if (!PACK_EXPANSION_P (arg
))
17212 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
17215 /* Expand the pack expansion and push each entry onto
17217 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
17218 if (TREE_CODE (arg
) == TREE_VEC
)
17220 unsigned int len
, j
;
17222 len
= TREE_VEC_LENGTH (arg
);
17223 for (j
= 0; j
< len
; ++j
)
17225 tree value
= TREE_VEC_ELT (arg
, j
);
17226 if (value
!= NULL_TREE
)
17227 value
= convert_from_reference (value
);
17228 vec_safe_push (call_args
, value
);
17233 /* A partial substitution. Add one entry. */
17234 vec_safe_push (call_args
, arg
);
17239 /* We do not perform argument-dependent lookup if normal
17240 lookup finds a non-function, in accordance with the
17241 expected resolution of DR 218. */
17243 && ((is_overloaded_fn (function
)
17244 /* If lookup found a member function, the Koenig lookup is
17245 not appropriate, even if an unqualified-name was used
17246 to denote the function. */
17247 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
17248 || identifier_p (function
))
17249 /* Only do this when substitution turns a dependent call
17250 into a non-dependent call. */
17251 && type_dependent_expression_p_push (t
)
17252 && !any_type_dependent_arguments_p (call_args
))
17253 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
17255 if (function
!= NULL_TREE
17256 && identifier_p (function
)
17257 && !any_type_dependent_arguments_p (call_args
))
17259 if (koenig_p
&& (complain
& tf_warning_or_error
))
17261 /* For backwards compatibility and good diagnostics, try
17262 the unqualified lookup again if we aren't in SFINAE
17264 tree unq
= (tsubst_copy_and_build
17265 (function
, args
, complain
, in_decl
, true,
17266 integral_constant_expression_p
));
17267 if (unq
== error_mark_node
)
17269 release_tree_vector (call_args
);
17270 RETURN (error_mark_node
);
17273 if (unq
!= function
)
17275 /* In a lambda fn, we have to be careful to not
17276 introduce new this captures. Legacy code can't
17277 be using lambdas anyway, so it's ok to be
17279 bool in_lambda
= (current_class_type
17280 && LAMBDA_TYPE_P (current_class_type
));
17281 char const *const msg
17282 = G_("%qD was not declared in this scope, "
17283 "and no declarations were found by "
17284 "argument-dependent lookup at the point "
17285 "of instantiation");
17289 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17292 diag
= permerror (EXPR_LOC_OR_LOC (t
, input_location
),
17298 if (INDIRECT_REF_P (fn
))
17299 fn
= TREE_OPERAND (fn
, 0);
17300 if (is_overloaded_fn (fn
))
17301 fn
= get_first_fn (fn
);
17304 /* Can't say anything more. */;
17305 else if (DECL_CLASS_SCOPE_P (fn
))
17307 location_t loc
= EXPR_LOC_OR_LOC (t
,
17310 "declarations in dependent base %qT are "
17311 "not found by unqualified lookup",
17312 DECL_CLASS_CONTEXT (fn
));
17313 if (current_class_ptr
)
17315 "use %<this->%D%> instead", function
);
17318 "use %<%T::%D%> instead",
17319 current_class_name
, function
);
17322 inform (DECL_SOURCE_LOCATION (fn
),
17323 "%qD declared here, later in the "
17324 "translation unit", fn
);
17327 release_tree_vector (call_args
);
17328 RETURN (error_mark_node
);
17335 if (identifier_p (function
))
17337 if (complain
& tf_error
)
17338 unqualified_name_lookup_error (function
);
17339 release_tree_vector (call_args
);
17340 RETURN (error_mark_node
);
17344 /* Remember that there was a reference to this entity. */
17345 if (function
!= NULL_TREE
17346 && DECL_P (function
)
17347 && !mark_used (function
, complain
) && !(complain
& tf_error
))
17349 release_tree_vector (call_args
);
17350 RETURN (error_mark_node
);
17353 /* Put back tf_decltype for the actual call. */
17354 complain
|= decltype_flag
;
17356 if (function
== NULL_TREE
)
17357 switch (CALL_EXPR_IFN (t
))
17360 gcc_assert (nargs
== 1);
17361 if (vec_safe_length (call_args
) != 1)
17363 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
17364 "wrong number of arguments to "
17365 "%<__builtin_launder%>");
17366 ret
= error_mark_node
;
17369 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
17371 (*call_args
)[0], complain
);
17375 /* Unsupported internal function with arguments. */
17376 gcc_unreachable ();
17378 else if (TREE_CODE (function
) == OFFSET_REF
)
17379 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
17381 else if (TREE_CODE (function
) == COMPONENT_REF
)
17383 tree instance
= TREE_OPERAND (function
, 0);
17384 tree fn
= TREE_OPERAND (function
, 1);
17386 if (processing_template_decl
17387 && (type_dependent_expression_p (instance
)
17388 || (!BASELINK_P (fn
)
17389 && TREE_CODE (fn
) != FIELD_DECL
)
17390 || type_dependent_expression_p (fn
)
17391 || any_type_dependent_arguments_p (call_args
)))
17392 ret
= build_min_nt_call_vec (function
, call_args
);
17393 else if (!BASELINK_P (fn
))
17394 ret
= finish_call_expr (function
, &call_args
,
17395 /*disallow_virtual=*/false,
17396 /*koenig_p=*/false,
17399 ret
= (build_new_method_call
17401 &call_args
, NULL_TREE
,
17402 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17407 ret
= finish_call_expr (function
, &call_args
,
17408 /*disallow_virtual=*/qualified_p
,
17412 release_tree_vector (call_args
);
17414 if (ret
!= error_mark_node
)
17416 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17417 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17418 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17419 bool thk
= CALL_FROM_THUNK_P (t
);
17420 if (op
|| ord
|| rev
|| thk
)
17422 function
= extract_call_expr (ret
);
17423 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17424 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17425 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17428 CALL_FROM_THUNK_P (function
) = true;
17429 /* The thunk location is not interesting. */
17430 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17440 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17441 tree folded_cond
= fold_non_dependent_expr (cond
);
17444 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17446 if (integer_zerop (folded_cond
))
17448 ++c_inhibit_evaluation_warnings
;
17449 exp1
= RECUR (TREE_OPERAND (t
, 1));
17450 --c_inhibit_evaluation_warnings
;
17451 exp2
= RECUR (TREE_OPERAND (t
, 2));
17455 exp1
= RECUR (TREE_OPERAND (t
, 1));
17456 ++c_inhibit_evaluation_warnings
;
17457 exp2
= RECUR (TREE_OPERAND (t
, 2));
17458 --c_inhibit_evaluation_warnings
;
17460 cond
= folded_cond
;
17464 exp1
= RECUR (TREE_OPERAND (t
, 1));
17465 exp2
= RECUR (TREE_OPERAND (t
, 2));
17468 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17469 cond
, exp1
, exp2
, complain
));
17472 case PSEUDO_DTOR_EXPR
:
17474 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17475 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17476 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17477 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17483 tree purpose
, value
, chain
;
17485 if (t
== void_list_node
)
17488 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17489 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17491 /* We have pack expansions, so expand those and
17492 create a new list out of it. */
17493 tree purposevec
= NULL_TREE
;
17494 tree valuevec
= NULL_TREE
;
17498 /* Expand the argument expressions. */
17499 if (TREE_PURPOSE (t
))
17500 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17501 complain
, in_decl
);
17502 if (TREE_VALUE (t
))
17503 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17504 complain
, in_decl
);
17506 /* Build the rest of the list. */
17507 chain
= TREE_CHAIN (t
);
17508 if (chain
&& chain
!= void_type_node
)
17509 chain
= RECUR (chain
);
17511 /* Determine the number of arguments. */
17512 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17514 len
= TREE_VEC_LENGTH (purposevec
);
17515 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17517 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17518 len
= TREE_VEC_LENGTH (valuevec
);
17521 /* Since we only performed a partial substitution into
17522 the argument pack, we only RETURN (a single list
17524 if (purposevec
== TREE_PURPOSE (t
)
17525 && valuevec
== TREE_VALUE (t
)
17526 && chain
== TREE_CHAIN (t
))
17529 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17532 /* Convert the argument vectors into a TREE_LIST */
17536 /* Grab the Ith values. */
17538 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17541 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17544 /* Build the list (backwards). */
17545 chain
= tree_cons (purpose
, value
, chain
);
17551 purpose
= TREE_PURPOSE (t
);
17553 purpose
= RECUR (purpose
);
17554 value
= TREE_VALUE (t
);
17556 value
= RECUR (value
);
17557 chain
= TREE_CHAIN (t
);
17558 if (chain
&& chain
!= void_type_node
)
17559 chain
= RECUR (chain
);
17560 if (purpose
== TREE_PURPOSE (t
)
17561 && value
== TREE_VALUE (t
)
17562 && chain
== TREE_CHAIN (t
))
17564 RETURN (tree_cons (purpose
, value
, chain
));
17567 case COMPONENT_REF
:
17574 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17575 args
, complain
, in_decl
);
17576 /* Remember that there was a reference to this entity. */
17577 if (DECL_P (object
)
17578 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17579 RETURN (error_mark_node
);
17580 object_type
= TREE_TYPE (object
);
17582 member
= TREE_OPERAND (t
, 1);
17583 if (BASELINK_P (member
))
17584 member
= tsubst_baselink (member
,
17585 non_reference (TREE_TYPE (object
)),
17586 args
, complain
, in_decl
);
17588 member
= tsubst_copy (member
, args
, complain
, in_decl
);
17589 if (member
== error_mark_node
)
17590 RETURN (error_mark_node
);
17592 if (TREE_CODE (member
) == FIELD_DECL
)
17594 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
17595 if (TREE_CODE (r
) == COMPONENT_REF
)
17596 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17599 else if (type_dependent_expression_p (object
))
17600 /* We can't do much here. */;
17601 else if (!CLASS_TYPE_P (object_type
))
17603 if (scalarish_type_p (object_type
))
17605 tree s
= NULL_TREE
;
17606 tree dtor
= member
;
17608 if (TREE_CODE (dtor
) == SCOPE_REF
)
17610 s
= TREE_OPERAND (dtor
, 0);
17611 dtor
= TREE_OPERAND (dtor
, 1);
17613 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
17615 dtor
= TREE_OPERAND (dtor
, 0);
17617 RETURN (finish_pseudo_destructor_expr
17618 (object
, s
, dtor
, input_location
));
17622 else if (TREE_CODE (member
) == SCOPE_REF
17623 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
17625 /* Lookup the template functions now that we know what the
17627 tree scope
= TREE_OPERAND (member
, 0);
17628 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
17629 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
17630 member
= lookup_qualified_name (scope
, tmpl
,
17631 /*is_type_p=*/false,
17632 /*complain=*/false);
17633 if (BASELINK_P (member
))
17635 BASELINK_FUNCTIONS (member
)
17636 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
17638 member
= (adjust_result_of_qualified_name_lookup
17639 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
17644 qualified_name_lookup_error (scope
, tmpl
, member
,
17646 RETURN (error_mark_node
);
17649 else if (TREE_CODE (member
) == SCOPE_REF
17650 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
17651 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
17653 if (complain
& tf_error
)
17655 if (TYPE_P (TREE_OPERAND (member
, 0)))
17656 error ("%qT is not a class or namespace",
17657 TREE_OPERAND (member
, 0));
17659 error ("%qD is not a class or namespace",
17660 TREE_OPERAND (member
, 0));
17662 RETURN (error_mark_node
);
17665 r
= finish_class_member_access_expr (object
, member
,
17666 /*template_p=*/false,
17668 if (TREE_CODE (r
) == COMPONENT_REF
)
17669 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17674 RETURN (build_throw
17675 (RECUR (TREE_OPERAND (t
, 0))));
17679 vec
<constructor_elt
, va_gc
> *n
;
17680 constructor_elt
*ce
;
17681 unsigned HOST_WIDE_INT idx
;
17682 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17683 bool process_index_p
;
17685 bool need_copy_p
= false;
17688 if (type
== error_mark_node
)
17689 RETURN (error_mark_node
);
17691 /* digest_init will do the wrong thing if we let it. */
17692 if (type
&& TYPE_PTRMEMFUNC_P (type
))
17695 /* We do not want to process the index of aggregate
17696 initializers as they are identifier nodes which will be
17697 looked up by digest_init. */
17698 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
17700 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
17701 newlen
= vec_safe_length (n
);
17702 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
17704 if (ce
->index
&& process_index_p
17705 /* An identifier index is looked up in the type
17706 being initialized, not the current scope. */
17707 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
17708 ce
->index
= RECUR (ce
->index
);
17710 if (PACK_EXPANSION_P (ce
->value
))
17712 /* Substitute into the pack expansion. */
17713 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
17716 if (ce
->value
== error_mark_node
17717 || PACK_EXPANSION_P (ce
->value
))
17719 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
17720 /* Just move the argument into place. */
17721 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
17724 /* Update the length of the final CONSTRUCTOR
17725 arguments vector, and note that we will need to
17727 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
17728 need_copy_p
= true;
17732 ce
->value
= RECUR (ce
->value
);
17737 vec
<constructor_elt
, va_gc
> *old_n
= n
;
17739 vec_alloc (n
, newlen
);
17740 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
17742 if (TREE_CODE (ce
->value
) == TREE_VEC
)
17744 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
17745 for (i
= 0; i
< len
; ++i
)
17746 CONSTRUCTOR_APPEND_ELT (n
, 0,
17747 TREE_VEC_ELT (ce
->value
, i
));
17750 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
17754 r
= build_constructor (init_list_type_node
, n
);
17755 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
17757 if (TREE_HAS_CONSTRUCTOR (t
))
17759 fcl_t cl
= fcl_functional
;
17760 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t
))
17762 RETURN (finish_compound_literal (type
, r
, complain
, cl
));
17765 TREE_TYPE (r
) = type
;
17771 tree operand_0
= TREE_OPERAND (t
, 0);
17772 if (TYPE_P (operand_0
))
17774 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
17775 RETURN (get_typeid (operand_0
, complain
));
17779 operand_0
= RECUR (operand_0
);
17780 RETURN (build_typeid (operand_0
, complain
));
17787 else if (DECL_PACK_P (t
))
17789 /* We don't build decls for an instantiation of a
17790 variadic capture proxy, we instantiate the elements
17792 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
17793 return RECUR (DECL_VALUE_EXPR (t
));
17799 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
17800 /* ??? We're doing a subset of finish_id_expression here. */
17802 && !processing_template_decl
17803 && !cp_unevaluated_operand
17804 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
17805 && CP_DECL_THREAD_LOCAL_P (r
))
17807 if (tree wrap
= get_tls_wrapper_fn (r
))
17808 /* Replace an evaluated use of the thread_local variable with
17809 a call to its wrapper. */
17810 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
17812 else if (outer_automatic_var_p (r
))
17814 r
= process_outer_var_ref (r
, complain
);
17815 if (is_capture_proxy (r
))
17816 register_local_specialization (r
, t
);
17819 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
17820 /* If the original type was a reference, we'll be wrapped in
17821 the appropriate INDIRECT_REF. */
17822 r
= convert_from_reference (r
);
17828 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17829 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17830 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
17833 case OFFSETOF_EXPR
:
17836 = tsubst_copy_and_build (TREE_OPERAND (t
, 1), args
, complain
,
17837 in_decl
, /*function_p=*/false,
17838 /*integral_constant_expression_p=*/false);
17839 RETURN (finish_offsetof (object_ptr
,
17840 RECUR (TREE_OPERAND (t
, 0)),
17841 EXPR_LOCATION (t
)));
17844 case ADDRESSOF_EXPR
:
17845 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
17846 RECUR (TREE_OPERAND (t
, 0)), complain
));
17850 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
17851 complain
, in_decl
);
17853 tree type2
= TRAIT_EXPR_TYPE2 (t
);
17854 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
17855 type2
= RECUR (type2
);
17857 type2
= tsubst (type2
, args
, complain
, in_decl
);
17859 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
17864 tree old_stmt_expr
= cur_stmt_expr
;
17865 tree stmt_expr
= begin_stmt_expr ();
17867 cur_stmt_expr
= stmt_expr
;
17868 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
17869 integral_constant_expression_p
);
17870 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
17871 cur_stmt_expr
= old_stmt_expr
;
17873 /* If the resulting list of expression statement is empty,
17874 fold it further into void_node. */
17875 if (empty_expr_stmt_p (stmt_expr
))
17876 stmt_expr
= void_node
;
17878 RETURN (stmt_expr
);
17883 tree r
= build_lambda_expr ();
17885 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
17886 LAMBDA_EXPR_CLOSURE (r
) = type
;
17887 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
17889 LAMBDA_EXPR_LOCATION (r
)
17890 = LAMBDA_EXPR_LOCATION (t
);
17891 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17892 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17893 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17894 LAMBDA_EXPR_DISCRIMINATOR (r
)
17895 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
17896 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
17898 /* No substitution needed. */;
17899 else if (VAR_OR_FUNCTION_DECL_P (scope
))
17900 /* For a function or variable scope, we want to use tsubst so that we
17901 don't complain about referring to an auto before deduction. */
17902 scope
= tsubst (scope
, args
, complain
, in_decl
);
17903 else if (TREE_CODE (scope
) == PARM_DECL
)
17905 /* Look up the parameter we want directly, as tsubst_copy
17906 doesn't do what we need. */
17907 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
17908 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
17909 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
17910 parm
= DECL_CHAIN (parm
);
17912 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17913 if (DECL_CONTEXT (scope
) == NULL_TREE
)
17914 DECL_CONTEXT (scope
) = fn
;
17916 else if (TREE_CODE (scope
) == FIELD_DECL
)
17917 /* For a field, use tsubst_copy so that we look up the existing field
17918 rather than build a new one. */
17919 scope
= RECUR (scope
);
17921 gcc_unreachable ();
17922 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
17924 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17925 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17927 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17928 determine_visibility (TYPE_NAME (type
));
17929 /* Now that we know visibility, instantiate the type so we have a
17930 declaration of the op() for later calls to lambda_function. */
17931 complete_type (type
);
17933 if (tree fn
= lambda_function (type
))
17934 LAMBDA_EXPR_RETURN_TYPE (r
) = TREE_TYPE (TREE_TYPE (fn
));
17936 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17938 insert_pending_capture_proxies ();
17940 RETURN (build_lambda_object (r
));
17944 /* We can get here for a constant initializer of non-dependent type.
17945 FIXME stop folding in cp_parser_initializer_clause. */
17947 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
17952 case TRANSACTION_EXPR
:
17953 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17954 integral_constant_expression_p
));
17957 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17959 case VEC_PERM_EXPR
:
17961 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17962 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17963 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17964 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17968 case REQUIRES_EXPR
:
17969 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17972 /* Handle Objective-C++ constructs, if appropriate. */
17975 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17976 in_decl
, /*function_p=*/false);
17980 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17986 input_location
= loc
;
17990 /* Verify that the instantiated ARGS are valid. For type arguments,
17991 make sure that the type's linkage is ok. For non-type arguments,
17992 make sure they are constants if they are integral or enumerations.
17993 Emit an error under control of COMPLAIN, and return TRUE on error. */
17996 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17998 if (dependent_template_arg_p (t
))
18000 if (ARGUMENT_PACK_P (t
))
18002 tree vec
= ARGUMENT_PACK_ARGS (t
);
18003 int len
= TREE_VEC_LENGTH (vec
);
18004 bool result
= false;
18007 for (i
= 0; i
< len
; ++i
)
18008 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
18012 else if (TYPE_P (t
))
18014 /* [basic.link]: A name with no linkage (notably, the name
18015 of a class or enumeration declared in a local scope)
18016 shall not be used to declare an entity with linkage.
18017 This implies that names with no linkage cannot be used as
18020 DR 757 relaxes this restriction for C++0x. */
18021 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
18022 : no_linkage_check (t
, /*relaxed_p=*/false));
18026 /* DR 488 makes use of a type with no linkage cause
18027 type deduction to fail. */
18028 if (complain
& tf_error
)
18030 if (TYPE_UNNAMED_P (nt
))
18031 error ("%qT is/uses unnamed type", t
);
18033 error ("template argument for %qD uses local type %qT",
18038 /* In order to avoid all sorts of complications, we do not
18039 allow variably-modified types as template arguments. */
18040 else if (variably_modified_type_p (t
, NULL_TREE
))
18042 if (complain
& tf_error
)
18043 error ("%qT is a variably modified type", t
);
18047 /* Class template and alias template arguments should be OK. */
18048 else if (DECL_TYPE_TEMPLATE_P (t
))
18050 /* A non-type argument of integral or enumerated type must be a
18052 else if (TREE_TYPE (t
)
18053 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
18054 && !REFERENCE_REF_P (t
)
18055 && !TREE_CONSTANT (t
))
18057 if (complain
& tf_error
)
18058 error ("integral expression %qE is not constant", t
);
18065 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
18067 int ix
, len
= DECL_NTPARMS (tmpl
);
18068 bool result
= false;
18070 for (ix
= 0; ix
!= len
; ix
++)
18072 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
18075 if (result
&& (complain
& tf_error
))
18076 error (" trying to instantiate %qD", tmpl
);
18080 /* We're out of SFINAE context now, so generate diagnostics for the access
18081 errors we saw earlier when instantiating D from TMPL and ARGS. */
18084 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
18086 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18087 tree type
= TREE_TYPE (pattern
);
18088 location_t loc
= input_location
;
18090 push_access_scope (d
);
18091 push_deferring_access_checks (dk_no_deferred
);
18092 input_location
= DECL_SOURCE_LOCATION (pattern
);
18093 tsubst (type
, args
, tf_warning_or_error
, d
);
18094 input_location
= loc
;
18095 pop_deferring_access_checks ();
18096 pop_access_scope (d
);
18099 /* Instantiate the indicated variable, function, or alias template TMPL with
18100 the template arguments in TARG_PTR. */
18103 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18105 tree targ_ptr
= orig_args
;
18109 bool access_ok
= true;
18111 if (tmpl
== error_mark_node
)
18112 return error_mark_node
;
18114 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
18116 /* If this function is a clone, handle it specially. */
18117 if (DECL_CLONED_FUNCTION_P (tmpl
))
18122 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18123 DECL_CLONED_FUNCTION. */
18124 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
18125 targ_ptr
, complain
);
18126 if (spec
== error_mark_node
)
18127 return error_mark_node
;
18129 /* Look for the clone. */
18130 FOR_EACH_CLONE (clone
, spec
)
18131 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
18133 /* We should always have found the clone by now. */
18134 gcc_unreachable ();
18138 if (targ_ptr
== error_mark_node
)
18139 return error_mark_node
;
18141 /* Check to see if we already have this specialization. */
18142 gen_tmpl
= most_general_template (tmpl
);
18143 if (TMPL_ARGS_DEPTH (targ_ptr
)
18144 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
18145 /* targ_ptr only has the innermost template args, so add the outer ones
18146 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18147 the case of a non-dependent call within a template definition). */
18148 targ_ptr
= (add_outermost_template_args
18149 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
18152 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18153 but it doesn't seem to be on the hot path. */
18154 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
18156 gcc_assert (tmpl
== gen_tmpl
18157 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
18159 || fndecl
== NULL_TREE
);
18161 if (spec
!= NULL_TREE
)
18163 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
18165 if (complain
& tf_error
)
18166 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
18167 return error_mark_node
;
18172 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
18174 return error_mark_node
;
18176 /* We are building a FUNCTION_DECL, during which the access of its
18177 parameters and return types have to be checked. However this
18178 FUNCTION_DECL which is the desired context for access checking
18179 is not built yet. We solve this chicken-and-egg problem by
18180 deferring all checks until we have the FUNCTION_DECL. */
18181 push_deferring_access_checks (dk_deferred
);
18183 /* Instantiation of the function happens in the context of the function
18184 template, not the context of the overload resolution we're doing. */
18185 push_to_top_level ();
18186 /* If there are dependent arguments, e.g. because we're doing partial
18187 ordering, make sure processing_template_decl stays set. */
18188 if (uses_template_parms (targ_ptr
))
18189 ++processing_template_decl
;
18190 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18192 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
18193 complain
, gen_tmpl
, true);
18194 push_nested_class (ctx
);
18197 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18199 fndecl
= NULL_TREE
;
18200 if (VAR_P (pattern
))
18202 /* We need to determine if we're using a partial or explicit
18203 specialization now, because the type of the variable could be
18205 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
18206 tree elt
= most_specialized_partial_spec (tid
, complain
);
18207 if (elt
== error_mark_node
)
18208 pattern
= error_mark_node
;
18211 tree partial_tmpl
= TREE_VALUE (elt
);
18212 tree partial_args
= TREE_PURPOSE (elt
);
18213 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
18214 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
18218 /* Substitute template parameters to obtain the specialization. */
18219 if (fndecl
== NULL_TREE
)
18220 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
18221 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
18222 pop_nested_class ();
18223 pop_from_top_level ();
18225 if (fndecl
== error_mark_node
)
18227 pop_deferring_access_checks ();
18228 return error_mark_node
;
18231 /* The DECL_TI_TEMPLATE should always be the immediate parent
18232 template, not the most general template. */
18233 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
18234 DECL_TI_ARGS (fndecl
) = targ_ptr
;
18236 /* Now we know the specialization, compute access previously
18237 deferred. Do no access control for inheriting constructors,
18238 as we already checked access for the inherited constructor. */
18239 if (!(flag_new_inheriting_ctors
18240 && DECL_INHERITED_CTOR (fndecl
)))
18242 push_access_scope (fndecl
);
18243 if (!perform_deferred_access_checks (complain
))
18245 pop_access_scope (fndecl
);
18247 pop_deferring_access_checks ();
18249 /* If we've just instantiated the main entry point for a function,
18250 instantiate all the alternate entry points as well. We do this
18251 by cloning the instantiation of the main entry point, not by
18252 instantiating the template clones. */
18253 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
18254 clone_function_decl (fndecl
, /*update_methods=*/false);
18258 if (!(complain
& tf_error
))
18260 /* Remember to reinstantiate when we're out of SFINAE so the user
18261 can see the errors. */
18262 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
18264 return error_mark_node
;
18269 /* Wrapper for instantiate_template_1. */
18272 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
18275 timevar_push (TV_TEMPLATE_INST
);
18276 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
18277 timevar_pop (TV_TEMPLATE_INST
);
18281 /* Instantiate the alias template TMPL with ARGS. Also push a template
18282 instantiation level, which instantiate_template doesn't do because
18283 functions and variables have sufficient context established by the
18287 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
18289 struct pending_template
*old_last_pend
= last_pending_template
;
18290 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18291 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
18292 return error_mark_node
;
18293 tree tinst
= build_tree_list (tmpl
, args
);
18294 if (!push_tinst_level (tinst
))
18297 return error_mark_node
;
18301 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
18302 args
, tmpl
, complain
,
18303 /*require_all_args=*/true,
18304 /*use_default_args=*/true);
18306 tree r
= instantiate_template (tmpl
, args
, complain
);
18307 pop_tinst_level ();
18308 /* We can't free this if a pending_template entry or last_error_tinst_level
18309 is pointing at it. */
18310 if (last_pending_template
== old_last_pend
18311 && last_error_tinst_level
== old_error_tinst
)
18317 /* PARM is a template parameter pack for FN. Returns true iff
18318 PARM is used in a deducible way in the argument list of FN. */
18321 pack_deducible_p (tree parm
, tree fn
)
18323 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
18324 for (; t
; t
= TREE_CHAIN (t
))
18326 tree type
= TREE_VALUE (t
);
18328 if (!PACK_EXPANSION_P (type
))
18330 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
18331 packs
; packs
= TREE_CHAIN (packs
))
18332 if (template_args_equal (TREE_VALUE (packs
), parm
))
18334 /* The template parameter pack is used in a function parameter
18335 pack. If this is the end of the parameter list, the
18336 template parameter pack is deducible. */
18337 if (TREE_CHAIN (t
) == void_list_node
)
18340 /* Otherwise, not. Well, it could be deduced from
18341 a non-pack parameter, but doing so would end up with
18342 a deduction mismatch, so don't bother. */
18346 /* The template parameter pack isn't used in any function parameter
18347 packs, but it might be used deeper, e.g. tuple<Args...>. */
18351 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18352 NARGS elements of the arguments that are being used when calling
18353 it. TARGS is a vector into which the deduced template arguments
18356 Returns either a FUNCTION_DECL for the matching specialization of FN or
18357 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18358 true, diagnostics will be printed to explain why it failed.
18360 If FN is a conversion operator, or we are trying to produce a specific
18361 specialization, RETURN_TYPE is the return type desired.
18363 The EXPLICIT_TARGS are explicit template arguments provided via a
18366 The parameter STRICT is one of:
18369 We are deducing arguments for a function call, as in
18370 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18371 deducing arguments for a call to the result of a conversion
18372 function template, as in [over.call.object].
18375 We are deducing arguments for a conversion function, as in
18376 [temp.deduct.conv].
18379 We are deducing arguments when doing an explicit instantiation
18380 as in [temp.explicit], when determining an explicit specialization
18381 as in [temp.expl.spec], or when taking the address of a function
18382 template, as in [temp.deduct.funcaddr]. */
18385 fn_type_unification (tree fn
,
18386 tree explicit_targs
,
18389 unsigned int nargs
,
18391 unification_kind_t strict
,
18398 tree decl
= NULL_TREE
;
18399 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
18401 static int deduction_depth
;
18402 struct pending_template
*old_last_pend
= last_pending_template
;
18403 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
18406 if (flag_new_inheriting_ctors
)
18407 fn
= strip_inheriting_ctors (fn
);
18409 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18411 tree r
= error_mark_node
;
18413 tree full_targs
= targs
;
18414 if (TMPL_ARGS_DEPTH (targs
)
18415 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18416 full_targs
= (add_outermost_template_args
18417 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18421 complain
|= tf_decltype
;
18423 /* In C++0x, it's possible to have a function template whose type depends
18424 on itself recursively. This is most obvious with decltype, but can also
18425 occur with enumeration scope (c++/48969). So we need to catch infinite
18426 recursion and reject the substitution at deduction time; this function
18427 will return error_mark_node for any repeated substitution.
18429 This also catches excessive recursion such as when f<N> depends on
18430 f<N-1> across all integers, and returns error_mark_node for all the
18431 substitutions back up to the initial one.
18433 This is, of course, not reentrant. */
18434 if (excessive_deduction_depth
)
18435 return error_mark_node
;
18436 tinst
= build_tree_list (fn
, NULL_TREE
);
18439 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18441 fntype
= TREE_TYPE (fn
);
18442 if (explicit_targs
)
18446 The specified template arguments must match the template
18447 parameters in kind (i.e., type, nontype, template), and there
18448 must not be more arguments than there are parameters;
18449 otherwise type deduction fails.
18451 Nontype arguments must match the types of the corresponding
18452 nontype template parameters, or must be convertible to the
18453 types of the corresponding nontype parameters as specified in
18454 _temp.arg.nontype_, otherwise type deduction fails.
18456 All references in the function type of the function template
18457 to the corresponding template parameters are replaced by the
18458 specified template argument values. If a substitution in a
18459 template parameter or in the function type of the function
18460 template results in an invalid type, type deduction fails. */
18461 int i
, len
= TREE_VEC_LENGTH (tparms
);
18462 location_t loc
= input_location
;
18463 bool incomplete
= false;
18465 if (explicit_targs
== error_mark_node
)
18468 if (TMPL_ARGS_DEPTH (explicit_targs
)
18469 < TMPL_ARGS_DEPTH (full_targs
))
18470 explicit_targs
= add_outermost_template_args (full_targs
,
18473 /* Adjust any explicit template arguments before entering the
18474 substitution context. */
18476 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18478 /*require_all_args=*/false,
18479 /*use_default_args=*/false));
18480 if (explicit_targs
== error_mark_node
)
18483 /* Substitute the explicit args into the function type. This is
18484 necessary so that, for instance, explicitly declared function
18485 arguments can match null pointed constants. If we were given
18486 an incomplete set of explicit args, we must not do semantic
18487 processing during substitution as we could create partial
18489 for (i
= 0; i
< len
; i
++)
18491 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18492 bool parameter_pack
= false;
18493 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18495 /* Dig out the actual parm. */
18496 if (TREE_CODE (parm
) == TYPE_DECL
18497 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18499 parm
= TREE_TYPE (parm
);
18500 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18502 else if (TREE_CODE (parm
) == PARM_DECL
)
18504 parm
= DECL_INITIAL (parm
);
18505 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18508 if (!parameter_pack
&& targ
== NULL_TREE
)
18509 /* No explicit argument for this template parameter. */
18512 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18514 /* Mark the argument pack as "incomplete". We could
18515 still deduce more arguments during unification.
18516 We remove this mark in type_unification_real. */
18519 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18520 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18521 = ARGUMENT_PACK_ARGS (targ
);
18524 /* We have some incomplete argument packs. */
18529 TREE_VALUE (tinst
) = explicit_targs
;
18530 if (!push_tinst_level (tinst
))
18532 excessive_deduction_depth
= true;
18535 processing_template_decl
+= incomplete
;
18536 input_location
= DECL_SOURCE_LOCATION (fn
);
18537 /* Ignore any access checks; we'll see them again in
18538 instantiate_template and they might have the wrong
18539 access path at this point. */
18540 push_deferring_access_checks (dk_deferred
);
18541 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18542 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18543 pop_deferring_access_checks ();
18544 input_location
= loc
;
18545 processing_template_decl
-= incomplete
;
18546 pop_tinst_level ();
18548 if (fntype
== error_mark_node
)
18551 /* Place the explicitly specified arguments in TARGS. */
18552 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18553 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18554 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18557 /* Never do unification on the 'this' parameter. */
18558 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18560 if (return_type
&& strict
== DEDUCE_CALL
)
18562 /* We're deducing for a call to the result of a template conversion
18563 function. The parms we really want are in return_type. */
18564 if (POINTER_TYPE_P (return_type
))
18565 return_type
= TREE_TYPE (return_type
);
18566 parms
= TYPE_ARG_TYPES (return_type
);
18568 else if (return_type
)
18572 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18573 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18574 new_args
[0] = return_type
;
18575 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18580 /* We allow incomplete unification without an error message here
18581 because the standard doesn't seem to explicitly prohibit it. Our
18582 callers must be ready to deal with unification failures in any
18585 TREE_VALUE (tinst
) = targs
;
18586 /* If we aren't explaining yet, push tinst context so we can see where
18587 any errors (e.g. from class instantiations triggered by instantiation
18588 of default template arguments) come from. If we are explaining, this
18589 context is redundant. */
18590 if (!explain_p
&& !push_tinst_level (tinst
))
18592 excessive_deduction_depth
= true;
18596 /* type_unification_real will pass back any access checks from default
18597 template argument substitution. */
18598 vec
<deferred_access_check
, va_gc
> *checks
;
18601 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18602 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18603 strict
, flags
, &checks
, explain_p
);
18605 pop_tinst_level ();
18609 /* Now that we have bindings for all of the template arguments,
18610 ensure that the arguments deduced for the template template
18611 parameters have compatible template parameter lists. We cannot
18612 check this property before we have deduced all template
18613 arguments, because the template parameter types of a template
18614 template parameter might depend on prior template parameters
18615 deduced after the template template parameter. The following
18616 ill-formed example illustrates this issue:
18618 template<typename T, template<T> class C> void f(C<5>, T);
18620 template<int N> struct X {};
18623 f(X<5>(), 5l); // error: template argument deduction fails
18626 The template parameter list of 'C' depends on the template type
18627 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18628 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18629 time that we deduce 'C'. */
18630 if (!template_template_parm_bindings_ok_p
18631 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18633 unify_inconsistent_template_template_parameters (explain_p
);
18637 /* All is well so far. Now, check:
18641 When all template arguments have been deduced, all uses of
18642 template parameters in nondeduced contexts are replaced with
18643 the corresponding deduced argument values. If the
18644 substitution results in an invalid type, as described above,
18645 type deduction fails. */
18646 TREE_VALUE (tinst
) = targs
;
18647 if (!push_tinst_level (tinst
))
18649 excessive_deduction_depth
= true;
18653 /* Also collect access checks from the instantiation. */
18654 reopen_deferring_access_checks (checks
);
18656 decl
= instantiate_template (fn
, targs
, complain
);
18658 checks
= get_deferred_access_checks ();
18659 pop_deferring_access_checks ();
18661 pop_tinst_level ();
18663 if (decl
== error_mark_node
)
18666 /* Now perform any access checks encountered during substitution. */
18667 push_access_scope (decl
);
18668 ok
= perform_access_checks (checks
, complain
);
18669 pop_access_scope (decl
);
18673 /* If we're looking for an exact match, check that what we got
18674 is indeed an exact match. It might not be if some template
18675 parameters are used in non-deduced contexts. But don't check
18676 for an exact match if we have dependent template arguments;
18677 in that case we're doing partial ordering, and we already know
18678 that we have two candidates that will provide the actual type. */
18679 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
18681 tree substed
= TREE_TYPE (decl
);
18685 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
18687 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
18688 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
18689 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
18691 unify_type_mismatch (explain_p
, args
[i
],
18692 TREE_VALUE (sarg
));
18697 /* After doing deduction with the inherited constructor, actually return an
18698 instantiation of the inheriting constructor. */
18700 decl
= instantiate_template (orig_fn
, targs
, complain
);
18706 if (excessive_deduction_depth
)
18708 if (deduction_depth
== 0)
18709 /* Reset once we're all the way out. */
18710 excessive_deduction_depth
= false;
18713 /* We can't free this if a pending_template entry or last_error_tinst_level
18714 is pointing at it. */
18715 if (last_pending_template
== old_last_pend
18716 && last_error_tinst_level
== old_error_tinst
)
18722 /* Adjust types before performing type deduction, as described in
18723 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18724 sections are symmetric. PARM is the type of a function parameter
18725 or the return type of the conversion function. ARG is the type of
18726 the argument passed to the call, or the type of the value
18727 initialized with the result of the conversion function.
18728 ARG_EXPR is the original argument expression, which may be null. */
18731 maybe_adjust_types_for_deduction (unification_kind_t strict
,
18744 /* Swap PARM and ARG throughout the remainder of this
18745 function; the handling is precisely symmetric since PARM
18746 will initialize ARG rather than vice versa. */
18747 std::swap (parm
, arg
);
18751 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18752 too, but here handle it by stripping the reference from PARM
18753 rather than by adding it to ARG. */
18754 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18755 && TYPE_REF_IS_RVALUE (*parm
)
18756 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18757 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18758 && TREE_CODE (*arg
) == REFERENCE_TYPE
18759 && !TYPE_REF_IS_RVALUE (*arg
))
18760 *parm
= TREE_TYPE (*parm
);
18761 /* Nothing else to do in this case. */
18765 gcc_unreachable ();
18768 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
18770 /* [temp.deduct.call]
18772 If P is not a reference type:
18774 --If A is an array type, the pointer type produced by the
18775 array-to-pointer standard conversion (_conv.array_) is
18776 used in place of A for type deduction; otherwise,
18778 --If A is a function type, the pointer type produced by
18779 the function-to-pointer standard conversion
18780 (_conv.func_) is used in place of A for type deduction;
18783 --If A is a cv-qualified type, the top level
18784 cv-qualifiers of A's type are ignored for type
18786 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
18787 *arg
= build_pointer_type (TREE_TYPE (*arg
));
18788 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
18789 *arg
= build_pointer_type (*arg
);
18791 *arg
= TYPE_MAIN_VARIANT (*arg
);
18794 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18795 reference to a cv-unqualified template parameter that does not represent a
18796 template parameter of a class template (during class template argument
18797 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18798 an lvalue, the type "lvalue reference to A" is used in place of A for type
18800 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18801 && TYPE_REF_IS_RVALUE (*parm
)
18802 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18803 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm
))
18804 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18805 && (arg_expr
? lvalue_p (arg_expr
)
18806 /* try_one_overload doesn't provide an arg_expr, but
18807 functions are always lvalues. */
18808 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
18809 *arg
= build_reference_type (*arg
);
18811 /* [temp.deduct.call]
18813 If P is a cv-qualified type, the top level cv-qualifiers
18814 of P's type are ignored for type deduction. If P is a
18815 reference type, the type referred to by P is used for
18817 *parm
= TYPE_MAIN_VARIANT (*parm
);
18818 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
18820 *parm
= TREE_TYPE (*parm
);
18821 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18824 /* DR 322. For conversion deduction, remove a reference type on parm
18825 too (which has been swapped into ARG). */
18826 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
18827 *arg
= TREE_TYPE (*arg
);
18832 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18833 template which does contain any deducible template parameters; check if
18834 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18835 unify_one_argument. */
18838 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
18839 int flags
, bool explain_p
)
18844 type
= TREE_TYPE (arg
);
18848 if (same_type_p (parm
, type
))
18849 return unify_success (explain_p
);
18851 if (strict
== DEDUCE_CONV
)
18853 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
18854 explain_p
? tf_warning_or_error
: tf_none
))
18855 return unify_success (explain_p
);
18857 else if (strict
!= DEDUCE_EXACT
)
18859 if (can_convert_arg (parm
, type
,
18860 TYPE_P (arg
) ? NULL_TREE
: arg
,
18861 flags
, explain_p
? tf_warning_or_error
: tf_none
))
18862 return unify_success (explain_p
);
18865 if (strict
== DEDUCE_EXACT
)
18866 return unify_type_mismatch (explain_p
, parm
, arg
);
18868 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
18871 static bool uses_deducible_template_parms (tree type
);
18873 /* Returns true iff the expression EXPR is one from which a template
18874 argument can be deduced. In other words, if it's an undecorated
18875 use of a template non-type parameter. */
18878 deducible_expression (tree expr
)
18880 /* Strip implicit conversions. */
18881 while (CONVERT_EXPR_P (expr
))
18882 expr
= TREE_OPERAND (expr
, 0);
18883 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
18886 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18887 deducible way; that is, if it has a max value of <PARM> - 1. */
18890 deducible_array_bound (tree domain
)
18892 if (domain
== NULL_TREE
)
18895 tree max
= TYPE_MAX_VALUE (domain
);
18896 if (TREE_CODE (max
) != MINUS_EXPR
)
18899 return deducible_expression (TREE_OPERAND (max
, 0));
18902 /* Returns true iff the template arguments ARGS use a template parameter
18903 in a deducible way. */
18906 deducible_template_args (tree args
)
18908 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
18911 tree elt
= TREE_VEC_ELT (args
, i
);
18912 if (ARGUMENT_PACK_P (elt
))
18913 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
18916 if (PACK_EXPANSION_P (elt
))
18917 elt
= PACK_EXPANSION_PATTERN (elt
);
18918 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
18920 else if (TYPE_P (elt
))
18921 deducible
= uses_deducible_template_parms (elt
);
18923 deducible
= deducible_expression (elt
);
18931 /* Returns true iff TYPE contains any deducible references to template
18932 parameters, as per 14.8.2.5. */
18935 uses_deducible_template_parms (tree type
)
18937 if (PACK_EXPANSION_P (type
))
18938 type
= PACK_EXPANSION_PATTERN (type
);
18945 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18946 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18952 if (POINTER_TYPE_P (type
))
18953 return uses_deducible_template_parms (TREE_TYPE (type
));
18955 /* T[integer-constant ]
18957 if (TREE_CODE (type
) == ARRAY_TYPE
)
18958 return (uses_deducible_template_parms (TREE_TYPE (type
))
18959 || deducible_array_bound (TYPE_DOMAIN (type
)));
18971 if (TYPE_PTRMEM_P (type
))
18972 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
18973 || (uses_deducible_template_parms
18974 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
18976 /* template-name <T> (where template-name refers to a class template)
18977 template-name <i> (where template-name refers to a class template) */
18978 if (CLASS_TYPE_P (type
)
18979 && CLASSTYPE_TEMPLATE_INFO (type
)
18980 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
18981 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18982 (CLASSTYPE_TI_ARGS (type
)));
18987 if (TREE_CODE (type
) == FUNCTION_TYPE
18988 || TREE_CODE (type
) == METHOD_TYPE
)
18990 if (uses_deducible_template_parms (TREE_TYPE (type
)))
18992 tree parm
= TYPE_ARG_TYPES (type
);
18993 if (TREE_CODE (type
) == METHOD_TYPE
)
18994 parm
= TREE_CHAIN (parm
);
18995 for (; parm
; parm
= TREE_CHAIN (parm
))
18996 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
19003 /* Subroutine of type_unification_real and unify_pack_expansion to
19004 handle unification of a single P/A pair. Parameters are as
19005 for those functions. */
19008 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
19009 int subr
, unification_kind_t strict
,
19012 tree arg_expr
= NULL_TREE
;
19015 if (arg
== error_mark_node
|| parm
== error_mark_node
)
19016 return unify_invalid (explain_p
);
19017 if (arg
== unknown_type_node
)
19018 /* We can't deduce anything from this, but we might get all the
19019 template args from other function args. */
19020 return unify_success (explain_p
);
19022 /* Implicit conversions (Clause 4) will be performed on a function
19023 argument to convert it to the type of the corresponding function
19024 parameter if the parameter type contains no template-parameters that
19025 participate in template argument deduction. */
19026 if (strict
!= DEDUCE_EXACT
19027 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
19028 /* For function parameters with no deducible template parameters,
19029 just return. We'll check non-dependent conversions later. */
19030 return unify_success (explain_p
);
19035 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
19036 | UNIFY_ALLOW_MORE_CV_QUAL
19037 | UNIFY_ALLOW_DERIVED
);
19041 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
19045 arg_strict
= UNIFY_ALLOW_NONE
;
19049 gcc_unreachable ();
19052 /* We only do these transformations if this is the top-level
19053 parameter_type_list in a call or declaration matching; in other
19054 situations (nested function declarators, template argument lists) we
19055 won't be comparing a type to an expression, and we don't do any type
19061 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
19062 if (type_unknown_p (arg
))
19064 /* [temp.deduct.type] A template-argument can be
19065 deduced from a pointer to function or pointer
19066 to member function argument if the set of
19067 overloaded functions does not contain function
19068 templates and at most one of a set of
19069 overloaded functions provides a unique
19072 if (resolve_overloaded_unification
19073 (tparms
, targs
, parm
, arg
, strict
,
19074 arg_strict
, explain_p
))
19075 return unify_success (explain_p
);
19076 return unify_overload_resolution_failure (explain_p
, arg
);
19080 arg
= unlowered_expr_type (arg
);
19081 if (arg
== error_mark_node
)
19082 return unify_invalid (explain_p
);
19086 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
19089 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
19090 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
19091 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
19093 /* For deduction from an init-list we need the actual list. */
19094 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
19096 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
19099 /* for_each_template_parm callback that always returns 0. */
19102 zero_r (tree
, void *)
19107 /* for_each_template_parm any_fn callback to handle deduction of a template
19108 type argument from the type of an array bound. */
19111 array_deduction_r (tree t
, void *data
)
19113 tree_pair_p d
= (tree_pair_p
)data
;
19114 tree
&tparms
= d
->purpose
;
19115 tree
&targs
= d
->value
;
19117 if (TREE_CODE (t
) == ARRAY_TYPE
)
19118 if (tree dom
= TYPE_DOMAIN (t
))
19119 if (tree max
= TYPE_MAX_VALUE (dom
))
19121 if (TREE_CODE (max
) == MINUS_EXPR
)
19122 max
= TREE_OPERAND (max
, 0);
19123 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
19124 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
19125 UNIFY_ALLOW_NONE
, /*explain*/false);
19128 /* Keep walking. */
19132 /* Try to deduce any not-yet-deduced template type arguments from the type of
19133 an array bound. This is handled separately from unify because 14.8.2.5 says
19134 "The type of a type parameter is only deduced from an array bound if it is
19135 not otherwise deduced." */
19138 try_array_deduction (tree tparms
, tree targs
, tree parm
)
19140 tree_pair_s data
= { tparms
, targs
};
19141 hash_set
<tree
> visited
;
19142 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
19143 /*nondeduced*/false, array_deduction_r
);
19146 /* Most parms like fn_type_unification.
19148 If SUBR is 1, we're being called recursively (to unify the
19149 arguments of a function or method parameter of a function
19152 CHECKS is a pointer to a vector of access checks encountered while
19153 substituting default template arguments. */
19156 type_unification_real (tree tparms
,
19160 unsigned int xnargs
,
19162 unification_kind_t strict
,
19164 vec
<deferred_access_check
, va_gc
> **checks
,
19169 int ntparms
= TREE_VEC_LENGTH (tparms
);
19170 int saw_undeduced
= 0;
19173 unsigned int nargs
;
19176 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
19177 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
19178 gcc_assert (ntparms
> 0);
19180 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
19182 /* Reset the number of non-defaulted template arguments contained
19184 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
19192 while (parms
&& parms
!= void_list_node
19195 parm
= TREE_VALUE (parms
);
19197 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19198 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
19199 /* For a function parameter pack that occurs at the end of the
19200 parameter-declaration-list, the type A of each remaining
19201 argument of the call is compared with the type P of the
19202 declarator-id of the function parameter pack. */
19205 parms
= TREE_CHAIN (parms
);
19207 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19208 /* For a function parameter pack that does not occur at the
19209 end of the parameter-declaration-list, the type of the
19210 parameter pack is a non-deduced context. */
19216 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
19222 && parms
!= void_list_node
19223 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
19225 /* Unify the remaining arguments with the pack expansion type. */
19227 tree parmvec
= make_tree_vec (1);
19229 /* Allocate a TREE_VEC and copy in all of the arguments */
19230 argvec
= make_tree_vec (nargs
- ia
);
19231 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
19232 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
19234 /* Copy the parameter into parmvec. */
19235 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
19236 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
19237 /*subr=*/subr
, explain_p
))
19240 /* Advance to the end of the list of parameters. */
19241 parms
= TREE_CHAIN (parms
);
19244 /* Fail if we've reached the end of the parm list, and more args
19245 are present, and the parm list isn't variadic. */
19246 if (ia
< nargs
&& parms
== void_list_node
)
19247 return unify_too_many_arguments (explain_p
, nargs
, ia
);
19248 /* Fail if parms are left and they don't have default values and
19249 they aren't all deduced as empty packs (c++/57397). This is
19250 consistent with sufficient_parms_p. */
19251 if (parms
&& parms
!= void_list_node
19252 && TREE_PURPOSE (parms
) == NULL_TREE
)
19254 unsigned int count
= nargs
;
19259 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
19262 p
= TREE_CHAIN (p
);
19264 while (p
&& p
!= void_list_node
);
19265 if (count
!= nargs
)
19266 return unify_too_few_arguments (explain_p
, ia
, count
,
19272 tsubst_flags_t complain
= (explain_p
19273 ? tf_warning_or_error
19275 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
19277 for (i
= 0; i
< ntparms
; i
++)
19279 tree targ
= TREE_VEC_ELT (targs
, i
);
19280 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19282 /* Clear the "incomplete" flags on all argument packs now so that
19283 substituting them into later default arguments works. */
19284 if (targ
&& ARGUMENT_PACK_P (targ
))
19286 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
19287 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
19290 if (targ
|| tparm
== error_mark_node
)
19292 tparm
= TREE_VALUE (tparm
);
19294 if (TREE_CODE (tparm
) == TYPE_DECL
19295 && !tried_array_deduction
)
19297 try_array_deduction (tparms
, targs
, xparms
);
19298 tried_array_deduction
= true;
19299 if (TREE_VEC_ELT (targs
, i
))
19303 /* If this is an undeduced nontype parameter that depends on
19304 a type parameter, try another pass; its type may have been
19305 deduced from a later argument than the one from which
19306 this parameter can be deduced. */
19307 if (TREE_CODE (tparm
) == PARM_DECL
19308 && uses_template_parms (TREE_TYPE (tparm
))
19309 && saw_undeduced
< 2)
19315 /* Core issue #226 (C++0x) [temp.deduct]:
19317 If a template argument has not been deduced, its
19318 default template argument, if any, is used.
19320 When we are in C++98 mode, TREE_PURPOSE will either
19321 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19322 to explicitly check cxx_dialect here. */
19323 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
19324 /* OK, there is a default argument. Wait until after the
19325 conversion check to do substitution. */
19328 /* If the type parameter is a parameter pack, then it will
19329 be deduced to an empty parameter pack. */
19330 if (template_parameter_pack_p (tparm
))
19334 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
19336 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
19337 TREE_CONSTANT (arg
) = 1;
19340 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19342 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
19344 TREE_VEC_ELT (targs
, i
) = arg
;
19348 return unify_parameter_deduction_failure (explain_p
, tparm
);
19351 /* DR 1391: All parameters have args, now check non-dependent parms for
19353 if (saw_undeduced
< 2)
19354 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
19355 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
19357 parm
= TREE_VALUE (parms
);
19359 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
19360 && (!TREE_CHAIN (parms
)
19361 || TREE_CHAIN (parms
) == void_list_node
))
19362 /* For a function parameter pack that occurs at the end of the
19363 parameter-declaration-list, the type A of each remaining
19364 argument of the call is compared with the type P of the
19365 declarator-id of the function parameter pack. */
19368 parms
= TREE_CHAIN (parms
);
19370 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
19371 /* For a function parameter pack that does not occur at the
19372 end of the parameter-declaration-list, the type of the
19373 parameter pack is a non-deduced context. */
19379 if (uses_template_parms (parm
))
19381 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
19386 /* Now substitute into the default template arguments. */
19387 for (i
= 0; i
< ntparms
; i
++)
19389 tree targ
= TREE_VEC_ELT (targs
, i
);
19390 tree tparm
= TREE_VEC_ELT (tparms
, i
);
19392 if (targ
|| tparm
== error_mark_node
)
19394 tree parm
= TREE_VALUE (tparm
);
19396 if (TREE_CODE (parm
) == PARM_DECL
19397 && uses_template_parms (TREE_TYPE (parm
))
19398 && saw_undeduced
< 2)
19401 tree arg
= TREE_PURPOSE (tparm
);
19402 reopen_deferring_access_checks (*checks
);
19403 location_t save_loc
= input_location
;
19405 input_location
= DECL_SOURCE_LOCATION (parm
);
19406 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
19407 if (!uses_template_parms (arg
))
19408 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
19410 else if (saw_undeduced
< 2)
19413 arg
= error_mark_node
;
19414 input_location
= save_loc
;
19415 *checks
= get_deferred_access_checks ();
19416 pop_deferring_access_checks ();
19417 if (arg
== error_mark_node
)
19421 TREE_VEC_ELT (targs
, i
) = arg
;
19422 /* The position of the first default template argument,
19423 is also the number of non-defaulted arguments in TARGS.
19425 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19426 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19430 if (saw_undeduced
++ == 1)
19434 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19435 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19437 return unify_success (explain_p
);
19440 /* Subroutine of type_unification_real. Args are like the variables
19441 at the call site. ARG is an overloaded function (or template-id);
19442 we try deducing template args from each of the overloads, and if
19443 only one succeeds, we go with that. Modifies TARGS and returns
19444 true on success. */
19447 resolve_overloaded_unification (tree tparms
,
19451 unification_kind_t strict
,
19455 tree tempargs
= copy_node (targs
);
19457 tree goodfn
= NULL_TREE
;
19460 if (TREE_CODE (arg
) == ADDR_EXPR
)
19462 arg
= TREE_OPERAND (arg
, 0);
19468 if (TREE_CODE (arg
) == COMPONENT_REF
)
19469 /* Handle `&x' where `x' is some static or non-static member
19471 arg
= TREE_OPERAND (arg
, 1);
19473 if (TREE_CODE (arg
) == OFFSET_REF
)
19474 arg
= TREE_OPERAND (arg
, 1);
19476 /* Strip baselink information. */
19477 if (BASELINK_P (arg
))
19478 arg
= BASELINK_FUNCTIONS (arg
);
19480 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19482 /* If we got some explicit template args, we need to plug them into
19483 the affected templates before we try to unify, in case the
19484 explicit args will completely resolve the templates in question. */
19487 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19488 arg
= TREE_OPERAND (arg
, 0);
19490 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19493 tree subargs
, elem
;
19495 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19498 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19499 expl_subargs
, NULL_TREE
, tf_none
,
19500 /*require_all_args=*/true,
19501 /*use_default_args=*/true);
19502 if (subargs
!= error_mark_node
19503 && !any_dependent_template_arguments_p (subargs
))
19505 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19506 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19507 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19508 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19517 /* If no templates (or more than one) are fully resolved by the
19518 explicit arguments, this template-id is a non-deduced context; it
19519 could still be OK if we deduce all template arguments for the
19520 enclosing call through other arguments. */
19524 else if (TREE_CODE (arg
) != OVERLOAD
19525 && TREE_CODE (arg
) != FUNCTION_DECL
)
19526 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19527 -- but the deduction does not succeed because the expression is
19528 not just the function on its own. */
19531 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19534 if (try_one_overload (tparms
, targs
, tempargs
, parm
, TREE_TYPE (fn
),
19535 strict
, sub_strict
, addr_p
, explain_p
)
19536 && (!goodfn
|| !decls_match (goodfn
, fn
)))
19543 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19544 to function or pointer to member function argument if the set of
19545 overloaded functions does not contain function templates and at most
19546 one of a set of overloaded functions provides a unique match.
19548 So if we found multiple possibilities, we return success but don't
19549 deduce anything. */
19553 int i
= TREE_VEC_LENGTH (targs
);
19555 if (TREE_VEC_ELT (tempargs
, i
))
19557 tree old
= TREE_VEC_ELT (targs
, i
);
19558 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19559 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19560 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19561 /* Don't forget explicit template arguments in a pack. */
19562 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19563 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19564 TREE_VEC_ELT (targs
, i
) = new_
;
19573 /* Core DR 115: In contexts where deduction is done and fails, or in
19574 contexts where deduction is not done, if a template argument list is
19575 specified and it, along with any default template arguments, identifies
19576 a single function template specialization, then the template-id is an
19577 lvalue for the function template specialization. */
19580 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19582 tree expr
, offset
, baselink
;
19585 if (!type_unknown_p (orig_expr
))
19590 offset
= NULL_TREE
;
19591 baselink
= NULL_TREE
;
19593 if (TREE_CODE (expr
) == ADDR_EXPR
)
19595 expr
= TREE_OPERAND (expr
, 0);
19598 if (TREE_CODE (expr
) == OFFSET_REF
)
19601 expr
= TREE_OPERAND (expr
, 1);
19603 if (BASELINK_P (expr
))
19606 expr
= BASELINK_FUNCTIONS (expr
);
19609 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19612 tree goodfn
= NULL_TREE
;
19614 /* If we got some explicit template args, we need to plug them into
19615 the affected templates before we try to unify, in case the
19616 explicit args will completely resolve the templates in question. */
19618 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19619 tree arg
= TREE_OPERAND (expr
, 0);
19620 tree badfn
= NULL_TREE
;
19621 tree badargs
= NULL_TREE
;
19623 for (lkp_iterator
iter (arg
); iter
; ++iter
)
19626 tree subargs
, elem
;
19628 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19631 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19632 expl_subargs
, NULL_TREE
, tf_none
,
19633 /*require_all_args=*/true,
19634 /*use_default_args=*/true);
19635 if (subargs
!= error_mark_node
19636 && !any_dependent_template_arguments_p (subargs
))
19638 elem
= instantiate_template (fn
, subargs
, tf_none
);
19639 if (elem
== error_mark_node
)
19644 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
19653 mark_used (goodfn
);
19656 expr
= build_baselink (BASELINK_BINFO (baselink
),
19657 BASELINK_ACCESS_BINFO (baselink
),
19658 expr
, BASELINK_OPTYPE (baselink
));
19662 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
19663 expr
= build_offset_ref (base
, expr
, addr
, complain
);
19666 expr
= cp_build_addr_expr (expr
, complain
);
19669 else if (good
== 0 && badargs
&& (complain
& tf_error
))
19670 /* There were no good options and at least one bad one, so let the
19671 user know what the problem is. */
19672 instantiate_template (badfn
, badargs
, complain
);
19677 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19678 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19679 different overloads deduce different arguments for a given parm.
19680 ADDR_P is true if the expression for which deduction is being
19681 performed was of the form "& fn" rather than simply "fn".
19683 Returns 1 on success. */
19686 try_one_overload (tree tparms
,
19691 unification_kind_t strict
,
19700 if (arg
== error_mark_node
)
19703 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19704 to function or pointer to member function argument if the set of
19705 overloaded functions does not contain function templates and at most
19706 one of a set of overloaded functions provides a unique match.
19708 So if this is a template, just return success. */
19710 if (uses_template_parms (arg
))
19713 if (TREE_CODE (arg
) == METHOD_TYPE
)
19714 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
19716 arg
= build_pointer_type (arg
);
19718 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
19720 /* We don't copy orig_targs for this because if we have already deduced
19721 some template args from previous args, unify would complain when we
19722 try to deduce a template parameter for the same argument, even though
19723 there isn't really a conflict. */
19724 nargs
= TREE_VEC_LENGTH (targs
);
19725 tempargs
= make_tree_vec (nargs
);
19727 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
19730 /* First make sure we didn't deduce anything that conflicts with
19731 explicitly specified args. */
19732 for (i
= nargs
; i
--; )
19734 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19735 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
19739 else if (uses_template_parms (elt
))
19740 /* Since we're unifying against ourselves, we will fill in
19741 template args used in the function parm list with our own
19742 template parms. Discard them. */
19743 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
19744 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
19746 /* Check that the argument at each index of the deduced argument pack
19747 is equivalent to the corresponding explicitly specified argument.
19748 We may have deduced more arguments than were explicitly specified,
19751 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19752 that's wrong if we deduce the same argument pack from multiple
19753 function arguments: it's only incomplete the first time. */
19755 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
19756 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
19758 if (TREE_VEC_LENGTH (deduced_pack
)
19759 < TREE_VEC_LENGTH (explicit_pack
))
19762 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
19763 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
19764 TREE_VEC_ELT (deduced_pack
, j
)))
19767 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
19771 for (i
= nargs
; i
--; )
19773 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19776 TREE_VEC_ELT (targs
, i
) = elt
;
19782 /* PARM is a template class (perhaps with unbound template
19783 parameters). ARG is a fully instantiated type. If ARG can be
19784 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19785 TARGS are as for unify. */
19788 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
19791 tree copy_of_targs
;
19793 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
19795 else if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19796 /* Matches anything. */;
19797 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
19798 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
)))
19801 /* We need to make a new template argument vector for the call to
19802 unify. If we used TARGS, we'd clutter it up with the result of
19803 the attempted unification, even if this class didn't work out.
19804 We also don't want to commit ourselves to all the unifications
19805 we've already done, since unification is supposed to be done on
19806 an argument-by-argument basis. In other words, consider the
19807 following pathological case:
19809 template <int I, int J, int K>
19812 template <int I, int J>
19813 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19815 template <int I, int J, int K>
19816 void f(S<I, J, K>, S<I, I, I>);
19825 Now, by the time we consider the unification involving `s2', we
19826 already know that we must have `f<0, 0, 0>'. But, even though
19827 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19828 because there are two ways to unify base classes of S<0, 1, 2>
19829 with S<I, I, I>. If we kept the already deduced knowledge, we
19830 would reject the possibility I=1. */
19831 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19833 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19835 if (unify_bound_ttp_args (tparms
, copy_of_targs
, parm
, arg
, explain_p
))
19840 /* If unification failed, we're done. */
19841 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
19842 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
19848 /* Given a template type PARM and a class type ARG, find the unique
19849 base type in ARG that is an instance of PARM. We do not examine
19850 ARG itself; only its base-classes. If there is not exactly one
19851 appropriate base class, return NULL_TREE. PARM may be the type of
19852 a partial specialization, as well as a plain template type. Used
19855 static enum template_base_result
19856 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
19857 bool explain_p
, tree
*result
)
19859 tree rval
= NULL_TREE
;
19862 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
19864 binfo
= TYPE_BINFO (complete_type (arg
));
19867 /* The type could not be completed. */
19868 *result
= NULL_TREE
;
19869 return tbr_incomplete_type
;
19872 /* Walk in inheritance graph order. The search order is not
19873 important, and this avoids multiple walks of virtual bases. */
19874 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
19876 tree r
= try_class_unification (tparms
, targs
, parm
,
19877 BINFO_TYPE (binfo
), explain_p
);
19881 /* If there is more than one satisfactory baseclass, then:
19885 If they yield more than one possible deduced A, the type
19889 if (rval
&& !same_type_p (r
, rval
))
19891 *result
= NULL_TREE
;
19892 return tbr_ambiguous_baseclass
;
19900 return tbr_success
;
19903 /* Returns the level of DECL, which declares a template parameter. */
19906 template_decl_level (tree decl
)
19908 switch (TREE_CODE (decl
))
19911 case TEMPLATE_DECL
:
19912 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
19915 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
19918 gcc_unreachable ();
19923 /* Decide whether ARG can be unified with PARM, considering only the
19924 cv-qualifiers of each type, given STRICT as documented for unify.
19925 Returns nonzero iff the unification is OK on that basis. */
19928 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
19930 int arg_quals
= cp_type_quals (arg
);
19931 int parm_quals
= cp_type_quals (parm
);
19933 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19934 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19936 /* Although a CVR qualifier is ignored when being applied to a
19937 substituted template parameter ([8.3.2]/1 for example), that
19938 does not allow us to unify "const T" with "int&" because both
19939 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19940 It is ok when we're allowing additional CV qualifiers
19941 at the outer level [14.8.2.1]/3,1st bullet. */
19942 if ((TREE_CODE (arg
) == REFERENCE_TYPE
19943 || TREE_CODE (arg
) == FUNCTION_TYPE
19944 || TREE_CODE (arg
) == METHOD_TYPE
)
19945 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
19948 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
19949 && (parm_quals
& TYPE_QUAL_RESTRICT
))
19953 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19954 && (arg_quals
& parm_quals
) != parm_quals
)
19957 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
19958 && (parm_quals
& arg_quals
) != arg_quals
)
19964 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19966 template_parm_level_and_index (tree parm
, int* level
, int* index
)
19968 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19969 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19970 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19972 *index
= TEMPLATE_TYPE_IDX (parm
);
19973 *level
= TEMPLATE_TYPE_LEVEL (parm
);
19977 *index
= TEMPLATE_PARM_IDX (parm
);
19978 *level
= TEMPLATE_PARM_LEVEL (parm
);
19982 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19984 if (unify (TP, TA, P, A, S, EP)) \
19988 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19989 expansion at the end of PACKED_PARMS. Returns 0 if the type
19990 deduction succeeds, 1 otherwise. STRICT is the same as in
19991 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19992 function call argument list. We'll need to adjust the arguments to make them
19993 types. SUBR tells us if this is from a recursive call to
19994 type_unification_real, or for comparing two template argument
19998 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
19999 tree packed_args
, unification_kind_t strict
,
20000 bool subr
, bool explain_p
)
20003 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
20004 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
20005 tree pack
, packs
= NULL_TREE
;
20006 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
20008 packed_args
= expand_template_argument_pack (packed_args
);
20010 int len
= TREE_VEC_LENGTH (packed_args
);
20012 /* Determine the parameter packs we will be deducing from the
20013 pattern, and record their current deductions. */
20014 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
20015 pack
; pack
= TREE_CHAIN (pack
))
20017 tree parm_pack
= TREE_VALUE (pack
);
20020 /* Determine the index and level of this parameter pack. */
20021 template_parm_level_and_index (parm_pack
, &level
, &idx
);
20023 /* Keep track of the parameter packs and their corresponding
20025 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
20026 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
20029 /* Loop through all of the arguments that have not yet been
20030 unified and unify each with the pattern. */
20031 for (i
= start
; i
< len
; i
++)
20034 bool any_explicit
= false;
20035 tree arg
= TREE_VEC_ELT (packed_args
, i
);
20037 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20038 or the element of its argument pack at the current index if
20039 this argument was explicitly specified. */
20040 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20044 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20047 if (TREE_VALUE (pack
)
20048 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
20049 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
20051 any_explicit
= true;
20052 arg
= TREE_VEC_ELT (pargs
, i
- start
);
20054 TMPL_ARG (targs
, level
, idx
) = arg
;
20057 /* If we had explicit template arguments, substitute them into the
20058 pattern before deduction. */
20061 /* Some arguments might still be unspecified or dependent. */
20063 ++processing_template_decl
;
20064 dependent
= any_dependent_template_arguments_p (targs
);
20066 --processing_template_decl
;
20067 parm
= tsubst (pattern
, targs
,
20068 explain_p
? tf_warning_or_error
: tf_none
,
20071 --processing_template_decl
;
20072 if (parm
== error_mark_node
)
20078 /* Unify the pattern with the current argument. */
20079 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
20083 /* For each parameter pack, collect the deduced value. */
20084 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20087 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20089 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
20090 TMPL_ARG (targs
, level
, idx
);
20094 /* Verify that the results of unification with the parameter packs
20095 produce results consistent with what we've seen before, and make
20096 the deduced argument packs available. */
20097 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
20099 tree old_pack
= TREE_VALUE (pack
);
20100 tree new_args
= TREE_TYPE (pack
);
20101 int i
, len
= TREE_VEC_LENGTH (new_args
);
20103 bool nondeduced_p
= false;
20105 /* By default keep the original deduced argument pack.
20106 If necessary, more specific code is going to update the
20107 resulting deduced argument later down in this function. */
20108 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
20109 TMPL_ARG (targs
, level
, idx
) = old_pack
;
20111 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20112 actually deduce anything. */
20113 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
20114 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
20115 nondeduced_p
= true;
20119 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
20121 /* If we had fewer function args than explicit template args,
20122 just use the explicits. */
20123 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20124 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
20125 if (len
< explicit_len
)
20126 new_args
= explicit_args
;
20132 /* Build the deduced *_ARGUMENT_PACK. */
20133 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
20135 result
= make_node (NONTYPE_ARGUMENT_PACK
);
20136 TREE_CONSTANT (result
) = 1;
20139 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
20141 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
20143 /* Note the deduced argument packs for this parameter
20145 TMPL_ARG (targs
, level
, idx
) = result
;
20147 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
20148 && (ARGUMENT_PACK_ARGS (old_pack
)
20149 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
20151 /* We only had the explicitly-provided arguments before, but
20152 now we have a complete set of arguments. */
20153 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
20155 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
20156 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
20157 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
20161 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
20162 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
20164 if (!comp_template_args (old_args
, new_args
,
20165 &bad_old_arg
, &bad_new_arg
))
20166 /* Inconsistent unification of this parameter pack. */
20167 return unify_parameter_pack_inconsistent (explain_p
,
20173 return unify_success (explain_p
);
20176 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20177 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20178 parameters and return value are as for unify. */
20181 unify_array_domain (tree tparms
, tree targs
,
20182 tree parm_dom
, tree arg_dom
,
20190 /* Our representation of array types uses "N - 1" as the
20191 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20192 not an integer constant. We cannot unify arbitrarily
20193 complex expressions, so we eliminate the MINUS_EXPRs
20195 parm_max
= TYPE_MAX_VALUE (parm_dom
);
20196 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
20199 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
20200 parm_max
= TREE_OPERAND (parm_max
, 0);
20202 arg_max
= TYPE_MAX_VALUE (arg_dom
);
20203 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
20206 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20207 trying to unify the type of a variable with the type
20208 of a template parameter. For example:
20210 template <unsigned int N>
20211 void f (char (&) [N]);
20218 Here, the type of the ARG will be "int [g(i)]", and
20219 may be a SAVE_EXPR, etc. */
20220 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
20221 return unify_vla_arg (explain_p
, arg_dom
);
20222 arg_max
= TREE_OPERAND (arg_max
, 0);
20225 /* If only one of the bounds used a MINUS_EXPR, compensate
20226 by adding one to the other bound. */
20227 if (parm_cst
&& !arg_cst
)
20228 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20232 else if (arg_cst
&& !parm_cst
)
20233 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
20238 return unify (tparms
, targs
, parm_max
, arg_max
,
20239 UNIFY_ALLOW_INTEGER
, explain_p
);
20242 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20244 enum pa_kind_t
{ pa_type
, pa_tmpl
, pa_expr
};
20249 if (PACK_EXPANSION_P (t
))
20250 t
= PACK_EXPANSION_PATTERN (t
);
20251 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
20252 || TREE_CODE (t
) == UNBOUND_CLASS_TEMPLATE
20253 || DECL_TYPE_TEMPLATE_P (t
))
20255 else if (TYPE_P (t
))
20261 /* Deduce the value of template parameters. TPARMS is the (innermost)
20262 set of template parameters to a template. TARGS is the bindings
20263 for those template parameters, as determined thus far; TARGS may
20264 include template arguments for outer levels of template parameters
20265 as well. PARM is a parameter to a template function, or a
20266 subcomponent of that parameter; ARG is the corresponding argument.
20267 This function attempts to match PARM with ARG in a manner
20268 consistent with the existing assignments in TARGS. If more values
20269 are deduced, then TARGS is updated.
20271 Returns 0 if the type deduction succeeds, 1 otherwise. The
20272 parameter STRICT is a bitwise or of the following flags:
20275 Require an exact match between PARM and ARG.
20276 UNIFY_ALLOW_MORE_CV_QUAL:
20277 Allow the deduced ARG to be more cv-qualified (by qualification
20278 conversion) than ARG.
20279 UNIFY_ALLOW_LESS_CV_QUAL:
20280 Allow the deduced ARG to be less cv-qualified than ARG.
20281 UNIFY_ALLOW_DERIVED:
20282 Allow the deduced ARG to be a template base class of ARG,
20283 or a pointer to a template base class of the type pointed to by
20285 UNIFY_ALLOW_INTEGER:
20286 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20287 case for more information.
20288 UNIFY_ALLOW_OUTER_LEVEL:
20289 This is the outermost level of a deduction. Used to determine validity
20290 of qualification conversions. A valid qualification conversion must
20291 have const qualified pointers leading up to the inner type which
20292 requires additional CV quals, except at the outer level, where const
20293 is not required [conv.qual]. It would be normal to set this flag in
20294 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20295 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20296 This is the outermost level of a deduction, and PARM can be more CV
20297 qualified at this point.
20298 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20299 This is the outermost level of a deduction, and PARM can be less CV
20300 qualified at this point. */
20303 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
20309 int strict_in
= strict
;
20310 tsubst_flags_t complain
= (explain_p
20311 ? tf_warning_or_error
20314 /* I don't think this will do the right thing with respect to types.
20315 But the only case I've seen it in so far has been array bounds, where
20316 signedness is the only information lost, and I think that will be
20318 while (CONVERT_EXPR_P (parm
))
20319 parm
= TREE_OPERAND (parm
, 0);
20321 if (arg
== error_mark_node
)
20322 return unify_invalid (explain_p
);
20323 if (arg
== unknown_type_node
20324 || arg
== init_list_type_node
)
20325 /* We can't deduce anything from this, but we might get all the
20326 template args from other function args. */
20327 return unify_success (explain_p
);
20329 if (parm
== any_targ_node
|| arg
== any_targ_node
)
20330 return unify_success (explain_p
);
20332 /* If PARM uses template parameters, then we can't bail out here,
20333 even if ARG == PARM, since we won't record unifications for the
20334 template parameters. We might need them if we're trying to
20335 figure out which of two things is more specialized. */
20336 if (arg
== parm
&& !uses_template_parms (parm
))
20337 return unify_success (explain_p
);
20339 /* Handle init lists early, so the rest of the function can assume
20340 we're dealing with a type. */
20341 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
20345 tree orig_parm
= parm
;
20347 /* Replace T with std::initializer_list<T> for deduction. */
20348 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20349 && flag_deduce_init_list
)
20350 parm
= listify (parm
);
20352 if (!is_std_init_list (parm
)
20353 && TREE_CODE (parm
) != ARRAY_TYPE
)
20354 /* We can only deduce from an initializer list argument if the
20355 parameter is std::initializer_list or an array; otherwise this
20356 is a non-deduced context. */
20357 return unify_success (explain_p
);
20359 if (TREE_CODE (parm
) == ARRAY_TYPE
)
20360 elttype
= TREE_TYPE (parm
);
20363 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
20364 /* Deduction is defined in terms of a single type, so just punt
20365 on the (bizarre) std::initializer_list<T...>. */
20366 if (PACK_EXPANSION_P (elttype
))
20367 return unify_success (explain_p
);
20370 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
20372 int elt_strict
= strict
;
20374 if (elt
== error_mark_node
)
20375 return unify_invalid (explain_p
);
20377 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
20379 tree type
= TREE_TYPE (elt
);
20380 if (type
== error_mark_node
)
20381 return unify_invalid (explain_p
);
20382 /* It should only be possible to get here for a call. */
20383 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
20384 elt_strict
|= maybe_adjust_types_for_deduction
20385 (DEDUCE_CALL
, &elttype
, &type
, elt
);
20389 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
20393 if (TREE_CODE (parm
) == ARRAY_TYPE
20394 && deducible_array_bound (TYPE_DOMAIN (parm
)))
20396 /* Also deduce from the length of the initializer list. */
20397 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
20398 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
20399 if (idx
== error_mark_node
)
20400 return unify_invalid (explain_p
);
20401 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20405 /* If the std::initializer_list<T> deduction worked, replace the
20406 deduced A with std::initializer_list<A>. */
20407 if (orig_parm
!= parm
)
20409 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
20410 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20411 targ
= listify (targ
);
20412 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
20414 return unify_success (explain_p
);
20417 /* If parm and arg aren't the same kind of thing (template, type, or
20418 expression), fail early. */
20419 if (pa_kind (parm
) != pa_kind (arg
))
20420 return unify_invalid (explain_p
);
20422 /* Immediately reject some pairs that won't unify because of
20423 cv-qualification mismatches. */
20424 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20426 /* It is the elements of the array which hold the cv quals of an array
20427 type, and the elements might be template type parms. We'll check
20428 when we recurse. */
20429 && TREE_CODE (arg
) != ARRAY_TYPE
20430 /* We check the cv-qualifiers when unifying with template type
20431 parameters below. We want to allow ARG `const T' to unify with
20432 PARM `T' for example, when computing which of two templates
20433 is more specialized, for example. */
20434 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
20435 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
20436 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20438 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
20439 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
20440 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
20441 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
20442 strict
&= ~UNIFY_ALLOW_DERIVED
;
20443 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
20444 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
20446 switch (TREE_CODE (parm
))
20448 case TYPENAME_TYPE
:
20450 case UNBOUND_CLASS_TEMPLATE
:
20451 /* In a type which contains a nested-name-specifier, template
20452 argument values cannot be deduced for template parameters used
20453 within the nested-name-specifier. */
20454 return unify_success (explain_p
);
20456 case TEMPLATE_TYPE_PARM
:
20457 case TEMPLATE_TEMPLATE_PARM
:
20458 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20459 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20460 if (error_operand_p (tparm
))
20461 return unify_invalid (explain_p
);
20463 if (TEMPLATE_TYPE_LEVEL (parm
)
20464 != template_decl_level (tparm
))
20465 /* The PARM is not one we're trying to unify. Just check
20466 to see if it matches ARG. */
20468 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20469 && (is_auto (parm
) ? is_auto (arg
)
20470 : same_type_p (parm
, arg
)))
20471 return unify_success (explain_p
);
20473 return unify_type_mismatch (explain_p
, parm
, arg
);
20475 idx
= TEMPLATE_TYPE_IDX (parm
);
20476 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20477 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20478 if (error_operand_p (tparm
))
20479 return unify_invalid (explain_p
);
20481 /* Check for mixed types and values. */
20482 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20483 && TREE_CODE (tparm
) != TYPE_DECL
)
20484 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20485 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20486 gcc_unreachable ();
20488 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20490 if ((strict_in
& UNIFY_ALLOW_DERIVED
)
20491 && CLASS_TYPE_P (arg
))
20493 /* First try to match ARG directly. */
20494 tree t
= try_class_unification (tparms
, targs
, parm
, arg
,
20498 /* Otherwise, look for a suitable base of ARG, as below. */
20499 enum template_base_result r
;
20500 r
= get_template_base (tparms
, targs
, parm
, arg
,
20503 return unify_no_common_base (explain_p
, r
, parm
, arg
);
20507 /* ARG must be constructed from a template class or a template
20508 template parameter. */
20509 else if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20510 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20511 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20513 /* Deduce arguments T, i from TT<T> or TT<i>. */
20514 if (unify_bound_ttp_args (tparms
, targs
, parm
, arg
, explain_p
))
20517 arg
= TYPE_TI_TEMPLATE (arg
);
20519 /* Fall through to deduce template name. */
20522 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20523 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20525 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20527 /* Simple cases: Value already set, does match or doesn't. */
20528 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20529 return unify_success (explain_p
);
20531 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20535 /* If PARM is `const T' and ARG is only `int', we don't have
20536 a match unless we are allowing additional qualification.
20537 If ARG is `const int' and PARM is just `T' that's OK;
20538 that binds `const int' to `T'. */
20539 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20541 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20543 /* Consider the case where ARG is `const volatile int' and
20544 PARM is `const T'. Then, T should be `volatile int'. */
20545 arg
= cp_build_qualified_type_real
20546 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20547 if (arg
== error_mark_node
)
20548 return unify_invalid (explain_p
);
20550 /* Simple cases: Value already set, does match or doesn't. */
20551 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20552 return unify_success (explain_p
);
20554 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20556 /* Make sure that ARG is not a variable-sized array. (Note
20557 that were talking about variable-sized arrays (like
20558 `int[n]'), rather than arrays of unknown size (like
20559 `int[]').) We'll get very confused by such a type since
20560 the bound of the array is not constant, and therefore
20561 not mangleable. Besides, such types are not allowed in
20562 ISO C++, so we can do as we please here. We do allow
20563 them for 'auto' deduction, since that isn't ABI-exposed. */
20564 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20565 return unify_vla_arg (explain_p
, arg
);
20567 /* Strip typedefs as in convert_template_argument. */
20568 arg
= canonicalize_type_argument (arg
, tf_none
);
20571 /* If ARG is a parameter pack or an expansion, we cannot unify
20572 against it unless PARM is also a parameter pack. */
20573 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20574 && !template_parameter_pack_p (parm
))
20575 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20577 /* If the argument deduction results is a METHOD_TYPE,
20578 then there is a problem.
20579 METHOD_TYPE doesn't map to any real C++ type the result of
20580 the deduction can not be of that type. */
20581 if (TREE_CODE (arg
) == METHOD_TYPE
)
20582 return unify_method_type_error (explain_p
, arg
);
20584 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20585 return unify_success (explain_p
);
20587 case TEMPLATE_PARM_INDEX
:
20588 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20589 if (error_operand_p (tparm
))
20590 return unify_invalid (explain_p
);
20592 if (TEMPLATE_PARM_LEVEL (parm
)
20593 != template_decl_level (tparm
))
20595 /* The PARM is not one we're trying to unify. Just check
20596 to see if it matches ARG. */
20597 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20598 && cp_tree_equal (parm
, arg
));
20600 unify_expression_unequal (explain_p
, parm
, arg
);
20604 idx
= TEMPLATE_PARM_IDX (parm
);
20605 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20609 int x
= !cp_tree_equal (targ
, arg
);
20611 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20615 if (cxx_dialect
>= cxx1z
20616 /* We deduce from array bounds in try_array_deduction. */
20617 && !(strict
& UNIFY_ALLOW_INTEGER
)
20618 && uses_template_parms (TREE_TYPE (parm
))
20619 && !type_uses_auto (TREE_TYPE (parm
)))
20621 tree atype
= TREE_TYPE (arg
);
20622 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20623 TREE_TYPE (parm
), atype
,
20624 UNIFY_ALLOW_NONE
, explain_p
);
20627 /* [temp.deduct.type] If, in the declaration of a function template
20628 with a non-type template-parameter, the non-type
20629 template-parameter is used in an expression in the function
20630 parameter-list and, if the corresponding template-argument is
20631 deduced, the template-argument type shall match the type of the
20632 template-parameter exactly, except that a template-argument
20633 deduced from an array bound may be of any integral type.
20634 The non-type parameter might use already deduced type parameters. */
20635 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20636 if (tree a
= type_uses_auto (tparm
))
20638 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
20639 if (tparm
== error_mark_node
)
20643 if (!TREE_TYPE (arg
))
20644 /* Template-parameter dependent expression. Just accept it for now.
20645 It will later be processed in convert_template_argument. */
20647 else if (same_type_p (TREE_TYPE (arg
), tparm
))
20649 else if ((strict
& UNIFY_ALLOW_INTEGER
)
20650 && CP_INTEGRAL_TYPE_P (tparm
))
20651 /* Convert the ARG to the type of PARM; the deduced non-type
20652 template argument must exactly match the types of the
20653 corresponding parameter. */
20654 arg
= fold (build_nop (tparm
, arg
));
20655 else if (uses_template_parms (tparm
))
20656 /* We haven't deduced the type of this parameter yet. Try again
20658 return unify_success (explain_p
);
20660 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
20662 /* If ARG is a parameter pack or an expansion, we cannot unify
20663 against it unless PARM is also a parameter pack. */
20664 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20665 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
20666 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20669 bool removed_attr
= false;
20670 arg
= strip_typedefs_expr (arg
, &removed_attr
);
20672 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20673 return unify_success (explain_p
);
20677 /* A pointer-to-member constant can be unified only with
20678 another constant. */
20679 if (TREE_CODE (arg
) != PTRMEM_CST
)
20680 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
20682 /* Just unify the class member. It would be useless (and possibly
20683 wrong, depending on the strict flags) to unify also
20684 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20685 arg refer to the same variable, even if through different
20686 classes. For instance:
20688 struct A { int x; };
20691 Unification of &A::x and &B::x must succeed. */
20692 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
20693 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
20698 if (!TYPE_PTR_P (arg
))
20699 return unify_type_mismatch (explain_p
, parm
, arg
);
20701 /* [temp.deduct.call]
20703 A can be another pointer or pointer to member type that can
20704 be converted to the deduced A via a qualification
20705 conversion (_conv.qual_).
20707 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20708 This will allow for additional cv-qualification of the
20709 pointed-to types if appropriate. */
20711 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
20712 /* The derived-to-base conversion only persists through one
20713 level of pointers. */
20714 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
20716 return unify (tparms
, targs
, TREE_TYPE (parm
),
20717 TREE_TYPE (arg
), strict
, explain_p
);
20720 case REFERENCE_TYPE
:
20721 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
20722 return unify_type_mismatch (explain_p
, parm
, arg
);
20723 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20724 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20727 if (TREE_CODE (arg
) != ARRAY_TYPE
)
20728 return unify_type_mismatch (explain_p
, parm
, arg
);
20729 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
20730 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
20731 return unify_type_mismatch (explain_p
, parm
, arg
);
20732 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20733 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20734 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
20735 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20736 TYPE_DOMAIN (arg
), explain_p
);
20737 return unify_success (explain_p
);
20744 case ENUMERAL_TYPE
:
20747 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20748 return unify_type_mismatch (explain_p
, parm
, arg
);
20750 /* We have already checked cv-qualification at the top of the
20752 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
20753 return unify_type_mismatch (explain_p
, parm
, arg
);
20755 /* As far as unification is concerned, this wins. Later checks
20756 will invalidate it if necessary. */
20757 return unify_success (explain_p
);
20759 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20760 /* Type INTEGER_CST can come from ordinary constant template args. */
20762 while (CONVERT_EXPR_P (arg
))
20763 arg
= TREE_OPERAND (arg
, 0);
20765 if (TREE_CODE (arg
) != INTEGER_CST
)
20766 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20767 return (tree_int_cst_equal (parm
, arg
)
20768 ? unify_success (explain_p
)
20769 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
20773 int i
, len
, argslen
;
20774 int parm_variadic_p
= 0;
20776 if (TREE_CODE (arg
) != TREE_VEC
)
20777 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20779 len
= TREE_VEC_LENGTH (parm
);
20780 argslen
= TREE_VEC_LENGTH (arg
);
20782 /* Check for pack expansions in the parameters. */
20783 for (i
= 0; i
< len
; ++i
)
20785 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
20788 /* We can unify against something with a trailing
20790 parm_variadic_p
= 1;
20792 /* [temp.deduct.type]/9: If the template argument list of
20793 P contains a pack expansion that is not the last
20794 template argument, the entire template argument list
20795 is a non-deduced context. */
20796 return unify_success (explain_p
);
20800 /* If we don't have enough arguments to satisfy the parameters
20801 (not counting the pack expression at the end), or we have
20802 too many arguments for a parameter list that doesn't end in
20803 a pack expression, we can't unify. */
20804 if (parm_variadic_p
20805 ? argslen
< len
- parm_variadic_p
20807 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
20809 /* Unify all of the parameters that precede the (optional)
20810 pack expression. */
20811 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20813 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20814 TREE_VEC_ELT (parm
, i
),
20815 TREE_VEC_ELT (arg
, i
),
20816 UNIFY_ALLOW_NONE
, explain_p
);
20818 if (parm_variadic_p
)
20819 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
20821 /*subr=*/true, explain_p
);
20822 return unify_success (explain_p
);
20827 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20828 return unify_type_mismatch (explain_p
, parm
, arg
);
20830 if (TYPE_PTRMEMFUNC_P (parm
))
20832 if (!TYPE_PTRMEMFUNC_P (arg
))
20833 return unify_type_mismatch (explain_p
, parm
, arg
);
20835 return unify (tparms
, targs
,
20836 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
20837 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
20838 strict
, explain_p
);
20840 else if (TYPE_PTRMEMFUNC_P (arg
))
20841 return unify_type_mismatch (explain_p
, parm
, arg
);
20843 if (CLASSTYPE_TEMPLATE_INFO (parm
))
20845 tree t
= NULL_TREE
;
20847 if (strict_in
& UNIFY_ALLOW_DERIVED
)
20849 /* First, we try to unify the PARM and ARG directly. */
20850 t
= try_class_unification (tparms
, targs
,
20851 parm
, arg
, explain_p
);
20855 /* Fallback to the special case allowed in
20856 [temp.deduct.call]:
20858 If P is a class, and P has the form
20859 template-id, then A can be a derived class of
20860 the deduced A. Likewise, if P is a pointer to
20861 a class of the form template-id, A can be a
20862 pointer to a derived class pointed to by the
20864 enum template_base_result r
;
20865 r
= get_template_base (tparms
, targs
, parm
, arg
,
20870 /* Don't give the derived diagnostic if we're
20871 already dealing with the same template. */
20873 = (CLASSTYPE_TEMPLATE_INFO (arg
)
20874 && (CLASSTYPE_TI_TEMPLATE (parm
)
20875 == CLASSTYPE_TI_TEMPLATE (arg
)));
20876 return unify_no_common_base (explain_p
&& !same_template
,
20881 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
20882 && (CLASSTYPE_TI_TEMPLATE (parm
)
20883 == CLASSTYPE_TI_TEMPLATE (arg
)))
20884 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20885 Then, we should unify `int' and `U'. */
20888 /* There's no chance of unification succeeding. */
20889 return unify_type_mismatch (explain_p
, parm
, arg
);
20891 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
20892 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
20894 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
20895 return unify_type_mismatch (explain_p
, parm
, arg
);
20896 return unify_success (explain_p
);
20899 case FUNCTION_TYPE
:
20901 unsigned int nargs
;
20906 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20907 return unify_type_mismatch (explain_p
, parm
, arg
);
20909 /* CV qualifications for methods can never be deduced, they must
20910 match exactly. We need to check them explicitly here,
20911 because type_unification_real treats them as any other
20912 cv-qualified parameter. */
20913 if (TREE_CODE (parm
) == METHOD_TYPE
20914 && (!check_cv_quals_for_unify
20916 class_of_this_parm (arg
),
20917 class_of_this_parm (parm
))))
20918 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20919 if (TREE_CODE (arg
) == FUNCTION_TYPE
20920 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
20921 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20922 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
20923 return unify_type_mismatch (explain_p
, parm
, arg
);
20925 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
20926 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
20928 nargs
= list_length (TYPE_ARG_TYPES (arg
));
20929 args
= XALLOCAVEC (tree
, nargs
);
20930 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
20931 a
!= NULL_TREE
&& a
!= void_list_node
;
20932 a
= TREE_CHAIN (a
), ++i
)
20933 args
[i
] = TREE_VALUE (a
);
20936 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
20937 args
, nargs
, 1, DEDUCE_EXACT
,
20938 LOOKUP_NORMAL
, NULL
, explain_p
))
20941 if (flag_noexcept_type
)
20943 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
20944 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
20945 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
20946 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
20947 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
20948 && uses_template_parms (TREE_PURPOSE (pspec
)))
20949 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
20950 TREE_PURPOSE (aspec
),
20951 UNIFY_ALLOW_NONE
, explain_p
);
20952 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
20953 return unify_type_mismatch (explain_p
, parm
, arg
);
20960 /* Unify a pointer to member with a pointer to member function, which
20961 deduces the type of the member as a function type. */
20962 if (TYPE_PTRMEMFUNC_P (arg
))
20964 /* Check top-level cv qualifiers */
20965 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
20966 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20968 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20969 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
20970 UNIFY_ALLOW_NONE
, explain_p
);
20972 /* Determine the type of the function we are unifying against. */
20973 tree fntype
= static_fn_type (arg
);
20975 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
20978 if (TREE_CODE (arg
) != OFFSET_TYPE
)
20979 return unify_type_mismatch (explain_p
, parm
, arg
);
20980 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20981 TYPE_OFFSET_BASETYPE (arg
),
20982 UNIFY_ALLOW_NONE
, explain_p
);
20983 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20984 strict
, explain_p
);
20987 if (DECL_TEMPLATE_PARM_P (parm
))
20988 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
20989 if (arg
!= scalar_constant_value (parm
))
20990 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20991 return unify_success (explain_p
);
20994 case TEMPLATE_DECL
:
20995 /* Matched cases are handled by the ARG == PARM test above. */
20996 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20999 /* We might get a variable as a non-type template argument in parm if the
21000 corresponding parameter is type-dependent. Make any necessary
21001 adjustments based on whether arg is a reference. */
21002 if (CONSTANT_CLASS_P (arg
))
21003 parm
= fold_non_dependent_expr (parm
);
21004 else if (REFERENCE_REF_P (arg
))
21006 tree sub
= TREE_OPERAND (arg
, 0);
21008 if (TREE_CODE (sub
) == ADDR_EXPR
)
21009 arg
= TREE_OPERAND (sub
, 0);
21011 /* Now use the normal expression code to check whether they match. */
21014 case TYPE_ARGUMENT_PACK
:
21015 case NONTYPE_ARGUMENT_PACK
:
21016 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
21017 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
21020 case DECLTYPE_TYPE
:
21021 case UNDERLYING_TYPE
:
21022 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21023 or UNDERLYING_TYPE nodes. */
21024 return unify_success (explain_p
);
21027 /* Unification fails if we hit an error node. */
21028 return unify_invalid (explain_p
);
21031 if (REFERENCE_REF_P (parm
))
21033 bool pexp
= PACK_EXPANSION_P (arg
);
21035 arg
= PACK_EXPANSION_PATTERN (arg
);
21036 if (REFERENCE_REF_P (arg
))
21037 arg
= TREE_OPERAND (arg
, 0);
21039 arg
= make_pack_expansion (arg
);
21040 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
21041 strict
, explain_p
);
21046 /* An unresolved overload is a nondeduced context. */
21047 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
21048 return unify_success (explain_p
);
21049 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
21051 /* We must be looking at an expression. This can happen with
21055 void foo(S<I>, S<I + 2>);
21057 This is a "nondeduced context":
21061 The nondeduced contexts are:
21063 --A type that is a template-id in which one or more of
21064 the template-arguments is an expression that references
21065 a template-parameter.
21067 In these cases, we assume deduction succeeded, but don't
21068 actually infer any unifications. */
21070 if (!uses_template_parms (parm
)
21071 && !template_args_equal (parm
, arg
))
21072 return unify_expression_unequal (explain_p
, parm
, arg
);
21074 return unify_success (explain_p
);
21077 #undef RECUR_AND_CHECK_FAILURE
21079 /* Note that DECL can be defined in this translation unit, if
21083 mark_definable (tree decl
)
21086 DECL_NOT_REALLY_EXTERN (decl
) = 1;
21087 FOR_EACH_CLONE (clone
, decl
)
21088 DECL_NOT_REALLY_EXTERN (clone
) = 1;
21091 /* Called if RESULT is explicitly instantiated, or is a member of an
21092 explicitly instantiated class. */
21095 mark_decl_instantiated (tree result
, int extern_p
)
21097 SET_DECL_EXPLICIT_INSTANTIATION (result
);
21099 /* If this entity has already been written out, it's too late to
21100 make any modifications. */
21101 if (TREE_ASM_WRITTEN (result
))
21104 /* For anonymous namespace we don't need to do anything. */
21105 if (decl_anon_ns_mem_p (result
))
21107 gcc_assert (!TREE_PUBLIC (result
));
21111 if (TREE_CODE (result
) != FUNCTION_DECL
)
21112 /* The TREE_PUBLIC flag for function declarations will have been
21113 set correctly by tsubst. */
21114 TREE_PUBLIC (result
) = 1;
21116 /* This might have been set by an earlier implicit instantiation. */
21117 DECL_COMDAT (result
) = 0;
21120 DECL_NOT_REALLY_EXTERN (result
) = 0;
21123 mark_definable (result
);
21124 mark_needed (result
);
21125 /* Always make artificials weak. */
21126 if (DECL_ARTIFICIAL (result
) && flag_weak
)
21127 comdat_linkage (result
);
21128 /* For WIN32 we also want to put explicit instantiations in
21129 linkonce sections. */
21130 else if (TREE_PUBLIC (result
))
21131 maybe_make_one_only (result
);
21134 /* If EXTERN_P, then this function will not be emitted -- unless
21135 followed by an explicit instantiation, at which point its linkage
21136 will be adjusted. If !EXTERN_P, then this function will be
21137 emitted here. In neither circumstance do we want
21138 import_export_decl to adjust the linkage. */
21139 DECL_INTERFACE_KNOWN (result
) = 1;
21142 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21143 important template arguments. If any are missing, we check whether
21144 they're important by using error_mark_node for substituting into any
21145 args that were used for partial ordering (the ones between ARGS and END)
21146 and seeing if it bubbles up. */
21149 check_undeduced_parms (tree targs
, tree args
, tree end
)
21151 bool found
= false;
21153 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
21154 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
21157 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
21161 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
21162 if (substed
== error_mark_node
)
21168 /* Given two function templates PAT1 and PAT2, return:
21170 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21171 -1 if PAT2 is more specialized than PAT1.
21172 0 if neither is more specialized.
21174 LEN indicates the number of parameters we should consider
21175 (defaulted parameters should not be considered).
21177 The 1998 std underspecified function template partial ordering, and
21178 DR214 addresses the issue. We take pairs of arguments, one from
21179 each of the templates, and deduce them against each other. One of
21180 the templates will be more specialized if all the *other*
21181 template's arguments deduce against its arguments and at least one
21182 of its arguments *does* *not* deduce against the other template's
21183 corresponding argument. Deduction is done as for class templates.
21184 The arguments used in deduction have reference and top level cv
21185 qualifiers removed. Iff both arguments were originally reference
21186 types *and* deduction succeeds in both directions, an lvalue reference
21187 wins against an rvalue reference and otherwise the template
21188 with the more cv-qualified argument wins for that pairing (if
21189 neither is more cv-qualified, they both are equal). Unlike regular
21190 deduction, after all the arguments have been deduced in this way,
21191 we do *not* verify the deduced template argument values can be
21192 substituted into non-deduced contexts.
21194 The logic can be a bit confusing here, because we look at deduce1 and
21195 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21196 can find template arguments for pat1 to make arg1 look like arg2, that
21197 means that arg2 is at least as specialized as arg1. */
21200 more_specialized_fn (tree pat1
, tree pat2
, int len
)
21202 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
21203 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
21204 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
21205 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
21206 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
21207 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
21208 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
21209 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
21210 tree origs1
, origs2
;
21211 bool lose1
= false;
21212 bool lose2
= false;
21214 /* Remove the this parameter from non-static member functions. If
21215 one is a non-static member function and the other is not a static
21216 member function, remove the first parameter from that function
21217 also. This situation occurs for operator functions where we
21218 locate both a member function (with this pointer) and non-member
21219 operator (with explicit first operand). */
21220 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
21222 len
--; /* LEN is the number of significant arguments for DECL1 */
21223 args1
= TREE_CHAIN (args1
);
21224 if (!DECL_STATIC_FUNCTION_P (decl2
))
21225 args2
= TREE_CHAIN (args2
);
21227 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
21229 args2
= TREE_CHAIN (args2
);
21230 if (!DECL_STATIC_FUNCTION_P (decl1
))
21233 args1
= TREE_CHAIN (args1
);
21237 /* If only one is a conversion operator, they are unordered. */
21238 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
21241 /* Consider the return type for a conversion function */
21242 if (DECL_CONV_FN_P (decl1
))
21244 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
21245 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
21249 processing_template_decl
++;
21255 /* Stop when an ellipsis is seen. */
21256 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
21258 tree arg1
= TREE_VALUE (args1
);
21259 tree arg2
= TREE_VALUE (args2
);
21260 int deduce1
, deduce2
;
21266 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21267 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21269 /* When both arguments are pack expansions, we need only
21270 unify the patterns themselves. */
21271 arg1
= PACK_EXPANSION_PATTERN (arg1
);
21272 arg2
= PACK_EXPANSION_PATTERN (arg2
);
21274 /* This is the last comparison we need to do. */
21278 /* DR 1847: If a particular P contains no template-parameters that
21279 participate in template argument deduction, that P is not used to
21280 determine the ordering. */
21281 if (!uses_deducible_template_parms (arg1
)
21282 && !uses_deducible_template_parms (arg2
))
21285 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
21287 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
21288 arg1
= TREE_TYPE (arg1
);
21289 quals1
= cp_type_quals (arg1
);
21292 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
21294 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
21295 arg2
= TREE_TYPE (arg2
);
21296 quals2
= cp_type_quals (arg2
);
21299 arg1
= TYPE_MAIN_VARIANT (arg1
);
21300 arg2
= TYPE_MAIN_VARIANT (arg2
);
21302 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
21304 int i
, len2
= remaining_arguments (args2
);
21305 tree parmvec
= make_tree_vec (1);
21306 tree argvec
= make_tree_vec (len2
);
21309 /* Setup the parameter vector, which contains only ARG1. */
21310 TREE_VEC_ELT (parmvec
, 0) = arg1
;
21312 /* Setup the argument vector, which contains the remaining
21314 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
21315 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21317 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
21318 argvec
, DEDUCE_EXACT
,
21319 /*subr=*/true, /*explain_p=*/false)
21322 /* We cannot deduce in the other direction, because ARG1 is
21323 a pack expansion but ARG2 is not. */
21326 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21328 int i
, len1
= remaining_arguments (args1
);
21329 tree parmvec
= make_tree_vec (1);
21330 tree argvec
= make_tree_vec (len1
);
21333 /* Setup the parameter vector, which contains only ARG1. */
21334 TREE_VEC_ELT (parmvec
, 0) = arg2
;
21336 /* Setup the argument vector, which contains the remaining
21338 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
21339 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
21341 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
21342 argvec
, DEDUCE_EXACT
,
21343 /*subr=*/true, /*explain_p=*/false)
21346 /* We cannot deduce in the other direction, because ARG2 is
21347 a pack expansion but ARG1 is not.*/
21353 /* The normal case, where neither argument is a pack
21355 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
21356 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21358 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
21359 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
21363 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21364 arg2, then arg2 is not as specialized as arg1. */
21370 /* "If, for a given type, deduction succeeds in both directions
21371 (i.e., the types are identical after the transformations above)
21372 and both P and A were reference types (before being replaced with
21373 the type referred to above):
21374 - if the type from the argument template was an lvalue reference and
21375 the type from the parameter template was not, the argument type is
21376 considered to be more specialized than the other; otherwise,
21377 - if the type from the argument template is more cv-qualified
21378 than the type from the parameter template (as described above),
21379 the argument type is considered to be more specialized than the other;
21381 - neither type is more specialized than the other." */
21383 if (deduce1
&& deduce2
)
21385 if (ref1
&& ref2
&& ref1
!= ref2
)
21392 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
21394 if ((quals1
& quals2
) == quals2
)
21396 if ((quals1
& quals2
) == quals1
)
21401 if (lose1
&& lose2
)
21402 /* We've failed to deduce something in either direction.
21403 These must be unordered. */
21408 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21409 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21410 /* We have already processed all of the arguments in our
21411 handing of the pack expansion type. */
21414 args1
= TREE_CHAIN (args1
);
21415 args2
= TREE_CHAIN (args2
);
21418 /* "In most cases, all template parameters must have values in order for
21419 deduction to succeed, but for partial ordering purposes a template
21420 parameter may remain without a value provided it is not used in the
21421 types being used for partial ordering."
21423 Thus, if we are missing any of the targs1 we need to substitute into
21424 origs1, then pat2 is not as specialized as pat1. This can happen when
21425 there is a nondeduced context. */
21426 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21428 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21431 processing_template_decl
--;
21433 /* If both deductions succeed, the partial ordering selects the more
21434 constrained template. */
21435 if (!lose1
&& !lose2
)
21437 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21438 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21439 lose1
= !subsumes_constraints (c1
, c2
);
21440 lose2
= !subsumes_constraints (c2
, c1
);
21443 /* All things being equal, if the next argument is a pack expansion
21444 for one function but not for the other, prefer the
21445 non-variadic function. FIXME this is bogus; see c++/41958. */
21447 && args1
&& TREE_VALUE (args1
)
21448 && args2
&& TREE_VALUE (args2
))
21450 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21451 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21454 if (lose1
== lose2
)
21462 /* Determine which of two partial specializations of TMPL is more
21465 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21466 to the first partial specialization. The TREE_PURPOSE is the
21467 innermost set of template parameters for the partial
21468 specialization. PAT2 is similar, but for the second template.
21470 Return 1 if the first partial specialization is more specialized;
21471 -1 if the second is more specialized; 0 if neither is more
21474 See [temp.class.order] for information about determining which of
21475 two templates is more specialized. */
21478 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21482 bool any_deductions
= false;
21484 tree tmpl1
= TREE_VALUE (pat1
);
21485 tree tmpl2
= TREE_VALUE (pat2
);
21486 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21487 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21489 /* Just like what happens for functions, if we are ordering between
21490 different template specializations, we may encounter dependent
21491 types in the arguments, and we need our dependency check functions
21492 to behave correctly. */
21493 ++processing_template_decl
;
21494 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21498 any_deductions
= true;
21501 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21505 any_deductions
= true;
21507 --processing_template_decl
;
21509 /* If both deductions succeed, the partial ordering selects the more
21510 constrained template. */
21511 if (!winner
&& any_deductions
)
21512 return more_constrained (tmpl1
, tmpl2
);
21514 /* In the case of a tie where at least one of the templates
21515 has a parameter pack at the end, the template with the most
21516 non-packed parameters wins. */
21519 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21520 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21522 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21523 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21524 int len1
= TREE_VEC_LENGTH (args1
);
21525 int len2
= TREE_VEC_LENGTH (args2
);
21527 /* We don't count the pack expansion at the end. */
21528 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21530 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21535 else if (len1
< len2
)
21542 /* Return the template arguments that will produce the function signature
21543 DECL from the function template FN, with the explicit template
21544 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21545 also match. Return NULL_TREE if no satisfactory arguments could be
21549 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21551 int ntparms
= DECL_NTPARMS (fn
);
21552 tree targs
= make_tree_vec (ntparms
);
21553 tree decl_type
= TREE_TYPE (decl
);
21554 tree decl_arg_types
;
21556 unsigned int nargs
, ix
;
21559 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21561 /* Never do unification on the 'this' parameter. */
21562 decl_arg_types
= skip_artificial_parms_for (decl
,
21563 TYPE_ARG_TYPES (decl_type
));
21565 nargs
= list_length (decl_arg_types
);
21566 args
= XALLOCAVEC (tree
, nargs
);
21567 for (arg
= decl_arg_types
, ix
= 0;
21568 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21569 arg
= TREE_CHAIN (arg
), ++ix
)
21570 args
[ix
] = TREE_VALUE (arg
);
21572 if (fn_type_unification (fn
, explicit_args
, targs
,
21574 (check_rettype
|| DECL_CONV_FN_P (fn
)
21575 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21576 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21578 == error_mark_node
)
21584 /* Return the innermost template arguments that, when applied to a partial
21585 specialization SPEC_TMPL of TMPL, yield the ARGS.
21587 For example, suppose we have:
21589 template <class T, class U> struct S {};
21590 template <class T> struct S<T*, int> {};
21592 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21593 partial specialization and the ARGS will be {double*, int}. The resulting
21594 vector will be {double}, indicating that `T' is bound to `double'. */
21597 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21599 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21601 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21602 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21604 tree innermost_deduced_args
;
21606 innermost_deduced_args
= make_tree_vec (ntparms
);
21607 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21609 deduced_args
= copy_node (args
);
21610 SET_TMPL_ARGS_LEVEL (deduced_args
,
21611 TMPL_ARGS_DEPTH (deduced_args
),
21612 innermost_deduced_args
);
21615 deduced_args
= innermost_deduced_args
;
21617 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
21619 if (unify (tparms
, deduced_args
,
21620 INNERMOST_TEMPLATE_ARGS (spec_args
),
21621 INNERMOST_TEMPLATE_ARGS (args
),
21622 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
21625 for (i
= 0; i
< ntparms
; ++i
)
21626 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
21628 if (!tried_array_deduction
)
21630 try_array_deduction (tparms
, innermost_deduced_args
,
21631 INNERMOST_TEMPLATE_ARGS (spec_args
));
21632 tried_array_deduction
= true;
21633 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
21639 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
21640 if (!push_tinst_level (tinst
))
21642 excessive_deduction_depth
= true;
21646 /* Verify that nondeduced template arguments agree with the type
21647 obtained from argument deduction.
21651 struct A { typedef int X; };
21652 template <class T, class U> struct C {};
21653 template <class T> struct C<T, typename T::X> {};
21655 Then with the instantiation `C<A, int>', we can deduce that
21656 `T' is `A' but unify () does not check whether `typename T::X'
21658 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
21659 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
21661 tf_none
, false, false);
21663 pop_tinst_level ();
21665 if (spec_args
== error_mark_node
21666 /* We only need to check the innermost arguments; the other
21667 arguments will always agree. */
21668 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args
),
21669 INNERMOST_TEMPLATE_ARGS (args
)))
21672 /* Now that we have bindings for all of the template arguments,
21673 ensure that the arguments deduced for the template template
21674 parameters have compatible template parameter lists. See the use
21675 of template_template_parm_bindings_ok_p in fn_type_unification
21676 for more information. */
21677 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
21680 return deduced_args
;
21683 // Compare two function templates T1 and T2 by deducing bindings
21684 // from one against the other. If both deductions succeed, compare
21685 // constraints to see which is more constrained.
21687 more_specialized_inst (tree t1
, tree t2
)
21692 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
21698 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
21704 // If both deductions succeed, then one may be more constrained.
21705 if (count
== 2 && fate
== 0)
21706 fate
= more_constrained (t1
, t2
);
21711 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21712 Return the TREE_LIST node with the most specialized template, if
21713 any. If there is no most specialized template, the error_mark_node
21716 Note that this function does not look at, or modify, the
21717 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21718 returned is one of the elements of INSTANTIATIONS, callers may
21719 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21720 and retrieve it from the value returned. */
21723 most_specialized_instantiation (tree templates
)
21727 ++processing_template_decl
;
21730 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
21732 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
21737 /* Equally specialized, move to next function. If there
21738 is no next function, nothing's most specialized. */
21739 fn
= TREE_CHAIN (fn
);
21747 /* Now verify that champ is better than everything earlier in the
21748 instantiation list. */
21749 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
21750 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
21757 processing_template_decl
--;
21760 return error_mark_node
;
21765 /* If DECL is a specialization of some template, return the most
21766 general such template. Otherwise, returns NULL_TREE.
21768 For example, given:
21770 template <class T> struct S { template <class U> void f(U); };
21772 if TMPL is `template <class U> void S<int>::f(U)' this will return
21773 the full template. This function will not trace past partial
21774 specializations, however. For example, given in addition:
21776 template <class T> struct S<T*> { template <class U> void f(U); };
21778 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21779 `template <class T> template <class U> S<T*>::f(U)'. */
21782 most_general_template (tree decl
)
21784 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21786 if (tree tinfo
= get_template_info (decl
))
21787 decl
= TI_TEMPLATE (tinfo
);
21788 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21789 template friend, or a FIELD_DECL for a capture pack. */
21790 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21794 /* Look for more and more general templates. */
21795 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
21797 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21798 (See cp-tree.h for details.) */
21799 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
21802 if (CLASS_TYPE_P (TREE_TYPE (decl
))
21803 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
21804 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
21807 /* Stop if we run into an explicitly specialized class template. */
21808 if (!DECL_NAMESPACE_SCOPE_P (decl
)
21809 && DECL_CONTEXT (decl
)
21810 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
21813 decl
= DECL_TI_TEMPLATE (decl
);
21819 /* Return the most specialized of the template partial specializations
21820 which can produce TARGET, a specialization of some class or variable
21821 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21822 a TEMPLATE_DECL node corresponding to the partial specialization, while
21823 the TREE_PURPOSE is the set of template arguments that must be
21824 substituted into the template pattern in order to generate TARGET.
21826 If the choice of partial specialization is ambiguous, a diagnostic
21827 is issued, and the error_mark_node is returned. If there are no
21828 partial specializations matching TARGET, then NULL_TREE is
21829 returned, indicating that the primary template should be used. */
21832 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
21834 tree list
= NULL_TREE
;
21839 tree outer_args
= NULL_TREE
;
21842 if (TYPE_P (target
))
21844 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
21845 tmpl
= TI_TEMPLATE (tinfo
);
21846 args
= TI_ARGS (tinfo
);
21848 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
21850 tmpl
= TREE_OPERAND (target
, 0);
21851 args
= TREE_OPERAND (target
, 1);
21853 else if (VAR_P (target
))
21855 tree tinfo
= DECL_TEMPLATE_INFO (target
);
21856 tmpl
= TI_TEMPLATE (tinfo
);
21857 args
= TI_ARGS (tinfo
);
21860 gcc_unreachable ();
21862 tree main_tmpl
= most_general_template (tmpl
);
21864 /* For determining which partial specialization to use, only the
21865 innermost args are interesting. */
21866 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21868 outer_args
= strip_innermost_template_args (args
, 1);
21869 args
= INNERMOST_TEMPLATE_ARGS (args
);
21872 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
21875 tree spec_tmpl
= TREE_VALUE (t
);
21879 /* Substitute in the template args from the enclosing class. */
21880 ++processing_template_decl
;
21881 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
21882 --processing_template_decl
;
21885 if (spec_tmpl
== error_mark_node
)
21886 return error_mark_node
;
21888 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
21892 spec_args
= add_to_template_args (outer_args
, spec_args
);
21894 /* Keep the candidate only if the constraints are satisfied,
21895 or if we're not compiling with concepts. */
21897 || constraints_satisfied_p (spec_tmpl
, spec_args
))
21899 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
21900 TREE_TYPE (list
) = TREE_TYPE (t
);
21908 ambiguous_p
= false;
21911 t
= TREE_CHAIN (t
);
21912 for (; t
; t
= TREE_CHAIN (t
))
21914 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21921 t
= TREE_CHAIN (t
);
21924 ambiguous_p
= true;
21933 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
21935 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21938 ambiguous_p
= true;
21946 char *spaces
= NULL
;
21947 if (!(complain
& tf_error
))
21948 return error_mark_node
;
21949 if (TYPE_P (target
))
21950 error ("ambiguous template instantiation for %q#T", target
);
21952 error ("ambiguous template instantiation for %q#D", target
);
21953 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
21954 for (t
= list
; t
; t
= TREE_CHAIN (t
))
21956 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
21957 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
21958 "%s %#qS", spaces
? spaces
: str
, subst
);
21959 spaces
= spaces
? spaces
: get_spaces (str
);
21962 return error_mark_node
;
21968 /* Explicitly instantiate DECL. */
21971 do_decl_instantiation (tree decl
, tree storage
)
21973 tree result
= NULL_TREE
;
21976 if (!decl
|| decl
== error_mark_node
)
21977 /* An error occurred, for which grokdeclarator has already issued
21978 an appropriate message. */
21980 else if (! DECL_LANG_SPECIFIC (decl
))
21982 error ("explicit instantiation of non-template %q#D", decl
);
21986 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
21987 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
21989 if (VAR_P (decl
) && !var_templ
)
21991 /* There is an asymmetry here in the way VAR_DECLs and
21992 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21993 the latter, the DECL we get back will be marked as a
21994 template instantiation, and the appropriate
21995 DECL_TEMPLATE_INFO will be set up. This does not happen for
21996 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21997 should handle VAR_DECLs as it currently handles
21999 if (!DECL_CLASS_SCOPE_P (decl
))
22001 error ("%qD is not a static data member of a class template", decl
);
22004 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
22005 if (!result
|| !VAR_P (result
))
22007 error ("no matching template for %qD found", decl
);
22010 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
22012 error ("type %qT for explicit instantiation %qD does not match "
22013 "declared type %qT", TREE_TYPE (result
), decl
,
22018 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
22020 error ("explicit instantiation of %q#D", decl
);
22026 /* Check for various error cases. Note that if the explicit
22027 instantiation is valid the RESULT will currently be marked as an
22028 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22029 until we get here. */
22031 if (DECL_TEMPLATE_SPECIALIZATION (result
))
22033 /* DR 259 [temp.spec].
22035 Both an explicit instantiation and a declaration of an explicit
22036 specialization shall not appear in a program unless the explicit
22037 instantiation follows a declaration of the explicit specialization.
22039 For a given set of template parameters, if an explicit
22040 instantiation of a template appears after a declaration of an
22041 explicit specialization for that template, the explicit
22042 instantiation has no effect. */
22045 else if (DECL_EXPLICIT_INSTANTIATION (result
))
22049 No program shall explicitly instantiate any template more
22052 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22053 the first instantiation was `extern' and the second is not,
22054 and EXTERN_P for the opposite case. */
22055 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
22056 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
22057 /* If an "extern" explicit instantiation follows an ordinary
22058 explicit instantiation, the template is instantiated. */
22062 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
22064 error ("no matching template for %qD found", result
);
22067 else if (!DECL_TEMPLATE_INFO (result
))
22069 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
22073 if (storage
== NULL_TREE
)
22075 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22077 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
22078 pedwarn (input_location
, OPT_Wpedantic
,
22079 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22084 error ("storage class %qD applied to template instantiation", storage
);
22086 check_explicit_instantiation_namespace (result
);
22087 mark_decl_instantiated (result
, extern_p
);
22089 instantiate_decl (result
, /*defer_ok=*/true,
22090 /*expl_inst_class_mem_p=*/false);
22094 mark_class_instantiated (tree t
, int extern_p
)
22096 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
22097 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
22098 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
22099 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
22102 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
22103 rest_of_type_compilation (t
, 1);
22107 /* Called from do_type_instantiation through binding_table_foreach to
22108 do recursive instantiation for the type bound in ENTRY. */
22110 bt_instantiate_type_proc (binding_entry entry
, void *data
)
22112 tree storage
= *(tree
*) data
;
22114 if (MAYBE_CLASS_TYPE_P (entry
->type
)
22115 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
22116 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
22119 /* Called from do_type_instantiation to instantiate a member
22120 (a member function or a static member variable) of an
22121 explicitly instantiated class template. */
22123 instantiate_class_member (tree decl
, int extern_p
)
22125 mark_decl_instantiated (decl
, extern_p
);
22127 instantiate_decl (decl
, /*defer_ok=*/true,
22128 /*expl_inst_class_mem_p=*/true);
22131 /* Perform an explicit instantiation of template class T. STORAGE, if
22132 non-null, is the RID for extern, inline or static. COMPLAIN is
22133 nonzero if this is called from the parser, zero if called recursively,
22134 since the standard is unclear (as detailed below). */
22137 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
22142 int previous_instantiation_extern_p
= 0;
22144 if (TREE_CODE (t
) == TYPE_DECL
)
22147 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
22150 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
22152 error ("explicit instantiation of non-class template %qD", tmpl
);
22154 error ("explicit instantiation of non-template type %qT", t
);
22160 if (!COMPLETE_TYPE_P (t
))
22162 if (complain
& tf_error
)
22163 error ("explicit instantiation of %q#T before definition of template",
22168 if (storage
!= NULL_TREE
)
22170 if (!in_system_header_at (input_location
))
22172 if (storage
== ridpointers
[(int) RID_EXTERN
])
22174 if (cxx_dialect
== cxx98
)
22175 pedwarn (input_location
, OPT_Wpedantic
,
22176 "ISO C++ 1998 forbids the use of %<extern%> on "
22177 "explicit instantiations");
22180 pedwarn (input_location
, OPT_Wpedantic
,
22181 "ISO C++ forbids the use of %qE"
22182 " on explicit instantiations", storage
);
22185 if (storage
== ridpointers
[(int) RID_INLINE
])
22187 else if (storage
== ridpointers
[(int) RID_EXTERN
])
22189 else if (storage
== ridpointers
[(int) RID_STATIC
])
22193 error ("storage class %qD applied to template instantiation",
22199 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
22201 /* DR 259 [temp.spec].
22203 Both an explicit instantiation and a declaration of an explicit
22204 specialization shall not appear in a program unless the explicit
22205 instantiation follows a declaration of the explicit specialization.
22207 For a given set of template parameters, if an explicit
22208 instantiation of a template appears after a declaration of an
22209 explicit specialization for that template, the explicit
22210 instantiation has no effect. */
22213 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
22217 No program shall explicitly instantiate any template more
22220 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22221 instantiation was `extern'. If EXTERN_P then the second is.
22222 These cases are OK. */
22223 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
22225 if (!previous_instantiation_extern_p
&& !extern_p
22226 && (complain
& tf_error
))
22227 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
22229 /* If we've already instantiated the template, just return now. */
22230 if (!CLASSTYPE_INTERFACE_ONLY (t
))
22234 check_explicit_instantiation_namespace (TYPE_NAME (t
));
22235 mark_class_instantiated (t
, extern_p
);
22243 /* In contrast to implicit instantiation, where only the
22244 declarations, and not the definitions, of members are
22245 instantiated, we have here:
22249 The explicit instantiation of a class template specialization
22250 implies the instantiation of all of its members not
22251 previously explicitly specialized in the translation unit
22252 containing the explicit instantiation.
22254 Of course, we can't instantiate member template classes, since
22255 we don't have any arguments for them. Note that the standard
22256 is unclear on whether the instantiation of the members are
22257 *explicit* instantiations or not. However, the most natural
22258 interpretation is that it should be an explicit instantiation. */
22261 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
22262 if (TREE_CODE (tmp
) == FUNCTION_DECL
22263 && DECL_TEMPLATE_INSTANTIATION (tmp
)
22264 && user_provided_p (tmp
))
22265 instantiate_class_member (tmp
, extern_p
);
22267 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
22268 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
22269 instantiate_class_member (tmp
, extern_p
);
22271 if (CLASSTYPE_NESTED_UTDS (t
))
22272 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
22273 bt_instantiate_type_proc
, &storage
);
22277 /* Given a function DECL, which is a specialization of TMPL, modify
22278 DECL to be a re-instantiation of TMPL with the same template
22279 arguments. TMPL should be the template into which tsubst'ing
22280 should occur for DECL, not the most general template.
22282 One reason for doing this is a scenario like this:
22285 void f(const T&, int i);
22287 void g() { f(3, 7); }
22290 void f(const T& t, const int i) { }
22292 Note that when the template is first instantiated, with
22293 instantiate_template, the resulting DECL will have no name for the
22294 first parameter, and the wrong type for the second. So, when we go
22295 to instantiate the DECL, we regenerate it. */
22298 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
22300 /* The arguments used to instantiate DECL, from the most general
22304 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
22306 /* Make sure that we can see identifiers, and compute access
22308 push_access_scope (decl
);
22310 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22318 args_depth
= TMPL_ARGS_DEPTH (args
);
22319 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
22320 if (args_depth
> parms_depth
)
22321 args
= get_innermost_template_args (args
, parms_depth
);
22323 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
22324 args
, tf_error
, NULL_TREE
,
22325 /*defer_ok*/false);
22326 if (specs
&& specs
!= error_mark_node
)
22327 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
22330 /* Merge parameter declarations. */
22331 decl_parm
= skip_artificial_parms_for (decl
,
22332 DECL_ARGUMENTS (decl
));
22334 = skip_artificial_parms_for (code_pattern
,
22335 DECL_ARGUMENTS (code_pattern
));
22336 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
22341 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22342 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
22343 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
22345 parm_type
= type_decays_to (parm_type
);
22346 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22347 TREE_TYPE (decl_parm
) = parm_type
;
22348 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22349 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22351 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22352 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22354 decl_parm
= DECL_CHAIN (decl_parm
);
22355 pattern_parm
= DECL_CHAIN (pattern_parm
);
22357 /* Merge any parameters that match with the function parameter
22359 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
22362 tree expanded_types
;
22363 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22364 the parameters in this function parameter pack. */
22365 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
22366 args
, tf_error
, NULL_TREE
);
22367 len
= TREE_VEC_LENGTH (expanded_types
);
22368 for (i
= 0; i
< len
; i
++)
22373 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
22374 /* Rename the parameter to include the index. */
22375 DECL_NAME (decl_parm
) =
22376 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
22377 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
22378 parm_type
= type_decays_to (parm_type
);
22379 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
22380 TREE_TYPE (decl_parm
) = parm_type
;
22381 attributes
= DECL_ATTRIBUTES (pattern_parm
);
22382 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
22384 DECL_ATTRIBUTES (decl_parm
) = attributes
;
22385 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
22387 decl_parm
= DECL_CHAIN (decl_parm
);
22390 /* Merge additional specifiers from the CODE_PATTERN. */
22391 if (DECL_DECLARED_INLINE_P (code_pattern
)
22392 && !DECL_DECLARED_INLINE_P (decl
))
22393 DECL_DECLARED_INLINE_P (decl
) = 1;
22395 else if (VAR_P (decl
))
22397 DECL_INITIAL (decl
) =
22398 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22399 tf_error
, DECL_TI_TEMPLATE (decl
),
22400 /*integral_constant_expression_p=*/false);
22401 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22402 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22403 tf_error
, DECL_TI_TEMPLATE (decl
));
22406 gcc_unreachable ();
22408 pop_access_scope (decl
);
22411 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22412 substituted to get DECL. */
22415 template_for_substitution (tree decl
)
22417 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22419 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22420 for the instantiation. This is not always the most general
22421 template. Consider, for example:
22424 struct S { template <class U> void f();
22425 template <> void f<int>(); };
22427 and an instantiation of S<double>::f<int>. We want TD to be the
22428 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22429 while (/* An instantiation cannot have a definition, so we need a
22430 more general template. */
22431 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22432 /* We must also deal with friend templates. Given:
22434 template <class T> struct S {
22435 template <class U> friend void f() {};
22438 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22439 so far as the language is concerned, but that's still
22440 where we get the pattern for the instantiation from. On
22441 other hand, if the definition comes outside the class, say:
22443 template <class T> struct S {
22444 template <class U> friend void f();
22446 template <class U> friend void f() {}
22448 we don't need to look any further. That's what the check for
22449 DECL_INITIAL is for. */
22450 || (TREE_CODE (decl
) == FUNCTION_DECL
22451 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22452 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22454 /* The present template, TD, should not be a definition. If it
22455 were a definition, we should be using it! Note that we
22456 cannot restructure the loop to just keep going until we find
22457 a template with a definition, since that might go too far if
22458 a specialization was declared, but not defined. */
22460 /* Fetch the more general template. */
22461 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22467 /* Returns true if we need to instantiate this template instance even if we
22468 know we aren't going to emit it. */
22471 always_instantiate_p (tree decl
)
22473 /* We always instantiate inline functions so that we can inline them. An
22474 explicit instantiation declaration prohibits implicit instantiation of
22475 non-inline functions. With high levels of optimization, we would
22476 normally inline non-inline functions -- but we're not allowed to do
22477 that for "extern template" functions. Therefore, we check
22478 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22479 return ((TREE_CODE (decl
) == FUNCTION_DECL
22480 && (DECL_DECLARED_INLINE_P (decl
)
22481 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22482 /* And we need to instantiate static data members so that
22483 their initializers are available in integral constant
22486 && decl_maybe_constant_var_p (decl
)));
22489 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22490 instantiate it now, modifying TREE_TYPE (fn). */
22493 maybe_instantiate_noexcept (tree fn
)
22495 tree fntype
, spec
, noex
, clone
;
22497 /* Don't instantiate a noexcept-specification from template context. */
22498 if (processing_template_decl
)
22501 if (DECL_CLONED_FUNCTION_P (fn
))
22502 fn
= DECL_CLONED_FUNCTION (fn
);
22503 fntype
= TREE_TYPE (fn
);
22504 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22506 if (!spec
|| !TREE_PURPOSE (spec
))
22509 noex
= TREE_PURPOSE (spec
);
22511 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22513 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22514 spec
= get_defaulted_eh_spec (fn
);
22515 else if (push_tinst_level (fn
))
22517 push_access_scope (fn
);
22518 push_deferring_access_checks (dk_no_deferred
);
22519 input_location
= DECL_SOURCE_LOCATION (fn
);
22520 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22521 DEFERRED_NOEXCEPT_ARGS (noex
),
22522 tf_warning_or_error
, fn
,
22523 /*function_p=*/false,
22524 /*integral_constant_expression_p=*/true);
22525 pop_deferring_access_checks ();
22526 pop_access_scope (fn
);
22527 pop_tinst_level ();
22528 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22529 if (spec
== error_mark_node
)
22530 spec
= noexcept_false_spec
;
22533 spec
= noexcept_false_spec
;
22535 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22538 FOR_EACH_CLONE (clone
, fn
)
22540 if (TREE_TYPE (clone
) == fntype
)
22541 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22543 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22547 /* Produce the definition of D, a _DECL generated from a template. If
22548 DEFER_OK is true, then we don't have to actually do the
22549 instantiation now; we just have to do it sometime. Normally it is
22550 an error if this is an explicit instantiation but D is undefined.
22551 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22552 instantiated class template. */
22555 instantiate_decl (tree d
, bool defer_ok
, bool expl_inst_class_mem_p
)
22557 tree tmpl
= DECL_TI_TEMPLATE (d
);
22564 bool pattern_defined
;
22565 location_t saved_loc
= input_location
;
22566 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22567 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22571 /* This function should only be used to instantiate templates for
22572 functions and static member variables. */
22573 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22575 /* A concept is never instantiated. */
22576 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
22578 /* Variables are never deferred; if instantiation is required, they
22579 are instantiated right away. That allows for better code in the
22580 case that an expression refers to the value of the variable --
22581 if the variable has a constant value the referring expression can
22582 take advantage of that fact. */
22586 /* Don't instantiate cloned functions. Instead, instantiate the
22587 functions they cloned. */
22588 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
22589 d
= DECL_CLONED_FUNCTION (d
);
22591 if (DECL_TEMPLATE_INSTANTIATED (d
)
22592 || (TREE_CODE (d
) == FUNCTION_DECL
22593 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
22594 || DECL_TEMPLATE_SPECIALIZATION (d
))
22595 /* D has already been instantiated or explicitly specialized, so
22596 there's nothing for us to do here.
22598 It might seem reasonable to check whether or not D is an explicit
22599 instantiation, and, if so, stop here. But when an explicit
22600 instantiation is deferred until the end of the compilation,
22601 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22602 the instantiation. */
22605 /* Check to see whether we know that this template will be
22606 instantiated in some other file, as with "extern template"
22608 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
22610 /* In general, we do not instantiate such templates. */
22611 if (external_p
&& !always_instantiate_p (d
))
22614 gen_tmpl
= most_general_template (tmpl
);
22615 gen_args
= DECL_TI_ARGS (d
);
22617 if (tmpl
!= gen_tmpl
)
22618 /* We should already have the extra args. */
22619 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
22620 == TMPL_ARGS_DEPTH (gen_args
));
22621 /* And what's in the hash table should match D. */
22622 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
22623 || spec
== NULL_TREE
);
22625 /* This needs to happen before any tsubsting. */
22626 if (! push_tinst_level (d
))
22629 timevar_push (TV_TEMPLATE_INST
);
22631 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22632 for the instantiation. */
22633 td
= template_for_substitution (d
);
22638 /* Look up an explicit specialization, if any. */
22639 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
22640 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
22641 if (elt
&& elt
!= error_mark_node
)
22643 td
= TREE_VALUE (elt
);
22644 args
= TREE_PURPOSE (elt
);
22648 code_pattern
= DECL_TEMPLATE_RESULT (td
);
22650 /* We should never be trying to instantiate a member of a class
22651 template or partial specialization. */
22652 gcc_assert (d
!= code_pattern
);
22654 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
22655 || DECL_TEMPLATE_SPECIALIZATION (td
))
22656 /* In the case of a friend template whose definition is provided
22657 outside the class, we may have too many arguments. Drop the
22658 ones we don't need. The same is true for specializations. */
22659 args
= get_innermost_template_args
22660 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
22662 if (TREE_CODE (d
) == FUNCTION_DECL
)
22664 deleted_p
= DECL_DELETED_FN (code_pattern
);
22665 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
22666 && DECL_INITIAL (code_pattern
) != error_mark_node
)
22667 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
22673 if (DECL_CLASS_SCOPE_P (code_pattern
))
22674 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
22675 || DECL_INLINE_VAR_P (code_pattern
));
22677 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
22680 /* We may be in the middle of deferred access check. Disable it now. */
22681 push_deferring_access_checks (dk_no_deferred
);
22683 /* Unless an explicit instantiation directive has already determined
22684 the linkage of D, remember that a definition is available for
22686 if (pattern_defined
22687 && !DECL_INTERFACE_KNOWN (d
)
22688 && !DECL_NOT_REALLY_EXTERN (d
))
22689 mark_definable (d
);
22691 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
22692 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
22693 input_location
= DECL_SOURCE_LOCATION (d
);
22695 /* If D is a member of an explicitly instantiated class template,
22696 and no definition is available, treat it like an implicit
22698 if (!pattern_defined
&& expl_inst_class_mem_p
22699 && DECL_EXPLICIT_INSTANTIATION (d
))
22701 /* Leave linkage flags alone on instantiations with anonymous
22703 if (TREE_PUBLIC (d
))
22705 DECL_NOT_REALLY_EXTERN (d
) = 0;
22706 DECL_INTERFACE_KNOWN (d
) = 0;
22708 SET_DECL_IMPLICIT_INSTANTIATION (d
);
22711 /* Defer all other templates, unless we have been explicitly
22712 forbidden from doing so. */
22713 if (/* If there is no definition, we cannot instantiate the
22716 /* If it's OK to postpone instantiation, do so. */
22718 /* If this is a static data member that will be defined
22719 elsewhere, we don't want to instantiate the entire data
22720 member, but we do want to instantiate the initializer so that
22721 we can substitute that elsewhere. */
22722 || (external_p
&& VAR_P (d
))
22723 /* Handle here a deleted function too, avoid generating
22724 its body (c++/61080). */
22727 /* The definition of the static data member is now required so
22728 we must substitute the initializer. */
22730 && !DECL_INITIAL (d
)
22731 && DECL_INITIAL (code_pattern
))
22735 bool const_init
= false;
22736 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22738 ns
= decl_namespace_context (d
);
22739 push_nested_namespace (ns
);
22741 push_nested_class (DECL_CONTEXT (d
));
22742 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
22744 tf_warning_or_error
, NULL_TREE
,
22745 /*integral_constant_expression_p=*/false);
22746 /* If instantiating the initializer involved instantiating this
22747 again, don't call cp_finish_decl twice. */
22748 if (!DECL_INITIAL (d
))
22750 /* Make sure the initializer is still constant, in case of
22751 circular dependency (template/instantiate6.C). */
22753 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22754 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
22755 /*asmspec_tree=*/NULL_TREE
,
22756 LOOKUP_ONLYCONVERTING
);
22759 pop_nested_class ();
22760 pop_nested_namespace (ns
);
22763 /* We restore the source position here because it's used by
22764 add_pending_template. */
22765 input_location
= saved_loc
;
22767 if (at_eof
&& !pattern_defined
22768 && DECL_EXPLICIT_INSTANTIATION (d
)
22769 && DECL_NOT_REALLY_EXTERN (d
))
22772 The definition of a non-exported function template, a
22773 non-exported member function template, or a non-exported
22774 member function or static data member of a class template
22775 shall be present in every translation unit in which it is
22776 explicitly instantiated. */
22777 permerror (input_location
, "explicit instantiation of %qD "
22778 "but no definition available", d
);
22780 /* If we're in unevaluated context, we just wanted to get the
22781 constant value; this isn't an odr use, so don't queue
22782 a full instantiation. */
22783 if (cp_unevaluated_operand
!= 0)
22785 /* ??? Historically, we have instantiated inline functions, even
22786 when marked as "extern template". */
22787 if (!(external_p
&& VAR_P (d
)))
22788 add_pending_template (d
);
22791 /* Tell the repository that D is available in this translation unit
22792 -- and see if it is supposed to be instantiated here. */
22793 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
22795 /* In a PCH file, despite the fact that the repository hasn't
22796 requested instantiation in the PCH it is still possible that
22797 an instantiation will be required in a file that includes the
22800 add_pending_template (d
);
22801 /* Instantiate inline functions so that the inliner can do its
22802 job, even though we'll not be emitting a copy of this
22804 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
22808 bool push_to_top
, nested
;
22810 fn_context
= decl_function_context (d
);
22811 nested
= current_function_decl
!= NULL_TREE
;
22812 push_to_top
= !(nested
&& fn_context
== current_function_decl
);
22814 vec
<tree
> omp_privatization_save
;
22816 save_omp_privatization_clauses (omp_privatization_save
);
22819 push_to_top_level ();
22822 push_function_context ();
22823 cp_unevaluated_operand
= 0;
22824 c_inhibit_evaluation_warnings
= 0;
22827 /* Mark D as instantiated so that recursive calls to
22828 instantiate_decl do not try to instantiate it again. */
22829 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
22831 /* Regenerate the declaration in case the template has been modified
22832 by a subsequent redeclaration. */
22833 regenerate_decl_from_template (d
, td
, args
);
22835 /* We already set the file and line above. Reset them now in case
22836 they changed as a result of calling regenerate_decl_from_template. */
22837 input_location
= DECL_SOURCE_LOCATION (d
);
22842 bool const_init
= false;
22844 /* Clear out DECL_RTL; whatever was there before may not be right
22845 since we've reset the type of the declaration. */
22846 SET_DECL_RTL (d
, NULL
);
22847 DECL_IN_AGGR_P (d
) = 0;
22849 /* The initializer is placed in DECL_INITIAL by
22850 regenerate_decl_from_template so we don't need to
22851 push/pop_access_scope again here. Pull it out so that
22852 cp_finish_decl can process it. */
22853 init
= DECL_INITIAL (d
);
22854 DECL_INITIAL (d
) = NULL_TREE
;
22855 DECL_INITIALIZED_P (d
) = 0;
22857 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22858 initializer. That function will defer actual emission until
22859 we have a chance to determine linkage. */
22860 DECL_EXTERNAL (d
) = 0;
22862 /* Enter the scope of D so that access-checking works correctly. */
22863 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22865 push_nested_class (DECL_CONTEXT (d
));
22867 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22868 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
22871 pop_nested_class ();
22873 if (variable_template_p (gen_tmpl
))
22874 note_variable_template_instantiation (d
);
22876 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
22877 synthesize_method (d
);
22878 else if (TREE_CODE (d
) == FUNCTION_DECL
)
22880 hash_map
<tree
, tree
> *saved_local_specializations
;
22883 tree block
= NULL_TREE
;
22884 tree lambda_ctx
= NULL_TREE
;
22886 /* Save away the current list, in case we are instantiating one
22887 template from within the body of another. */
22888 saved_local_specializations
= local_specializations
;
22890 /* Set up the list of local specializations. */
22891 local_specializations
= new hash_map
<tree
, tree
>;
22893 /* Set up context. */
22894 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22895 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22896 block
= push_stmt_list ();
22899 if (push_to_top
&& LAMBDA_FUNCTION_P (d
))
22901 /* When instantiating a lambda's templated function
22902 operator, we need to push the non-lambda class scope
22903 of the lambda itself so that the nested function
22904 stack is sufficiently correct to deal with this
22906 lambda_ctx
= DECL_CONTEXT (d
);
22908 lambda_ctx
= decl_type_context (TYPE_NAME (lambda_ctx
));
22909 while (lambda_ctx
&& LAMBDA_TYPE_P (lambda_ctx
));
22911 push_nested_class (lambda_ctx
);
22913 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
22916 /* Some typedefs referenced from within the template code need to be
22917 access checked at template instantiation time, i.e now. These
22918 types were added to the template at parsing time. Let's get those
22919 and perform the access checks then. */
22920 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
22923 /* Create substitution entries for the parameters. */
22924 tmpl_parm
= DECL_ARGUMENTS (code_pattern
);
22925 spec_parm
= DECL_ARGUMENTS (d
);
22926 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
22928 register_local_specialization (spec_parm
, tmpl_parm
);
22929 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
22930 tmpl_parm
= skip_artificial_parms_for (code_pattern
, tmpl_parm
);
22932 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22934 if (!DECL_PACK_P (tmpl_parm
))
22936 register_local_specialization (spec_parm
, tmpl_parm
);
22937 spec_parm
= DECL_CHAIN (spec_parm
);
22941 /* Register the (value) argument pack as a specialization of
22942 TMPL_PARM, then move on. */
22943 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22944 register_local_specialization (argpack
, tmpl_parm
);
22947 gcc_assert (!spec_parm
);
22949 /* Substitute into the body of the function. */
22950 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22951 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
22952 tf_warning_or_error
, tmpl
);
22955 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
22956 tf_warning_or_error
, tmpl
,
22957 /*integral_constant_expression_p=*/false);
22959 /* Set the current input_location to the end of the function
22960 so that finish_function knows where we are. */
22962 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
22964 /* Remember if we saw an infinite loop in the template. */
22965 current_function_infinite_loop
22966 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
22969 /* We don't need the local specializations any more. */
22970 delete local_specializations
;
22971 local_specializations
= saved_local_specializations
;
22973 /* Finish the function. */
22974 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22975 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22976 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
22979 d
= finish_function (0);
22980 expand_or_defer_fn (d
);
22983 pop_nested_class ();
22985 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22986 cp_check_omp_declare_reduction (d
);
22989 /* We're not deferring instantiation any more. */
22990 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
22993 pop_from_top_level ();
22995 pop_function_context ();
22998 restore_omp_privatization_clauses (omp_privatization_save
);
23001 pop_deferring_access_checks ();
23002 timevar_pop (TV_TEMPLATE_INST
);
23003 pop_tinst_level ();
23004 input_location
= saved_loc
;
23005 cp_unevaluated_operand
= saved_unevaluated_operand
;
23006 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23011 /* Run through the list of templates that we wish we could
23012 instantiate, and instantiate any we can. RETRIES is the
23013 number of times we retry pending template instantiation. */
23016 instantiate_pending_templates (int retries
)
23019 location_t saved_loc
= input_location
;
23021 /* Instantiating templates may trigger vtable generation. This in turn
23022 may require further template instantiations. We place a limit here
23023 to avoid infinite loop. */
23024 if (pending_templates
&& retries
>= max_tinst_depth
)
23026 tree decl
= pending_templates
->tinst
->decl
;
23028 fatal_error (input_location
,
23029 "template instantiation depth exceeds maximum of %d"
23030 " instantiating %q+D, possibly from virtual table generation"
23031 " (use -ftemplate-depth= to increase the maximum)",
23032 max_tinst_depth
, decl
);
23033 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23034 /* Pretend that we defined it. */
23035 DECL_INITIAL (decl
) = error_mark_node
;
23041 struct pending_template
**t
= &pending_templates
;
23042 struct pending_template
*last
= NULL
;
23046 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
23047 bool complete
= false;
23049 if (TYPE_P (instantiation
))
23053 if (!COMPLETE_TYPE_P (instantiation
))
23055 instantiate_class_template (instantiation
);
23056 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
23057 for (fn
= TYPE_METHODS (instantiation
);
23059 fn
= TREE_CHAIN (fn
))
23060 if (! DECL_ARTIFICIAL (fn
))
23061 instantiate_decl (fn
,
23062 /*defer_ok=*/false,
23063 /*expl_inst_class_mem_p=*/false);
23064 if (COMPLETE_TYPE_P (instantiation
))
23068 complete
= COMPLETE_TYPE_P (instantiation
);
23072 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23073 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
23076 = instantiate_decl (instantiation
,
23077 /*defer_ok=*/false,
23078 /*expl_inst_class_mem_p=*/false);
23079 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
23083 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
23084 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
23088 /* If INSTANTIATION has been instantiated, then we don't
23089 need to consider it again in the future. */
23097 current_tinst_level
= NULL
;
23099 last_pending_template
= last
;
23101 while (reconsider
);
23103 input_location
= saved_loc
;
23106 /* Substitute ARGVEC into T, which is a list of initializers for
23107 either base class or a non-static data member. The TREE_PURPOSEs
23108 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23109 instantiate_decl. */
23112 tsubst_initializer_list (tree t
, tree argvec
)
23114 tree inits
= NULL_TREE
;
23116 for (; t
; t
= TREE_CHAIN (t
))
23120 tree expanded_bases
= NULL_TREE
;
23121 tree expanded_arguments
= NULL_TREE
;
23124 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
23129 /* Expand the base class expansion type into separate base
23131 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
23132 tf_warning_or_error
,
23134 if (expanded_bases
== error_mark_node
)
23137 /* We'll be building separate TREE_LISTs of arguments for
23139 len
= TREE_VEC_LENGTH (expanded_bases
);
23140 expanded_arguments
= make_tree_vec (len
);
23141 for (i
= 0; i
< len
; i
++)
23142 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
23144 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23145 expand each argument in the TREE_VALUE of t. */
23146 expr
= make_node (EXPR_PACK_EXPANSION
);
23147 PACK_EXPANSION_LOCAL_P (expr
) = true;
23148 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
23149 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
23151 if (TREE_VALUE (t
) == void_type_node
)
23152 /* VOID_TYPE_NODE is used to indicate
23153 value-initialization. */
23155 for (i
= 0; i
< len
; i
++)
23156 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
23160 /* Substitute parameter packs into each argument in the
23162 in_base_initializer
= 1;
23163 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
23165 tree expanded_exprs
;
23167 /* Expand the argument. */
23168 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
23170 = tsubst_pack_expansion (expr
, argvec
,
23171 tf_warning_or_error
,
23173 if (expanded_exprs
== error_mark_node
)
23176 /* Prepend each of the expanded expressions to the
23177 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23178 for (i
= 0; i
< len
; i
++)
23180 TREE_VEC_ELT (expanded_arguments
, i
) =
23181 tree_cons (NULL_TREE
,
23182 TREE_VEC_ELT (expanded_exprs
, i
),
23183 TREE_VEC_ELT (expanded_arguments
, i
));
23186 in_base_initializer
= 0;
23188 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23189 since we built them backwards. */
23190 for (i
= 0; i
< len
; i
++)
23192 TREE_VEC_ELT (expanded_arguments
, i
) =
23193 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
23198 for (i
= 0; i
< len
; ++i
)
23200 if (expanded_bases
)
23202 decl
= TREE_VEC_ELT (expanded_bases
, i
);
23203 decl
= expand_member_init (decl
);
23204 init
= TREE_VEC_ELT (expanded_arguments
, i
);
23209 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
23210 tf_warning_or_error
, NULL_TREE
);
23212 decl
= expand_member_init (decl
);
23213 if (decl
&& !DECL_P (decl
))
23214 in_base_initializer
= 1;
23216 init
= TREE_VALUE (t
);
23218 if (init
!= void_type_node
)
23219 init
= tsubst_expr (init
, argvec
,
23220 tf_warning_or_error
, NULL_TREE
,
23221 /*integral_constant_expression_p=*/false);
23222 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
23223 /* If we had an initializer but it instantiated to nothing,
23224 value-initialize the object. This will only occur when
23225 the initializer was a pack expansion where the parameter
23226 packs used in that expansion were of length zero. */
23227 init
= void_type_node
;
23228 in_base_initializer
= 0;
23233 init
= build_tree_list (decl
, init
);
23234 TREE_CHAIN (init
) = inits
;
23242 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23245 set_current_access_from_decl (tree decl
)
23247 if (TREE_PRIVATE (decl
))
23248 current_access_specifier
= access_private_node
;
23249 else if (TREE_PROTECTED (decl
))
23250 current_access_specifier
= access_protected_node
;
23252 current_access_specifier
= access_public_node
;
23255 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23256 is the instantiation (which should have been created with
23257 start_enum) and ARGS are the template arguments to use. */
23260 tsubst_enum (tree tag
, tree newtag
, tree args
)
23264 if (SCOPED_ENUM_P (newtag
))
23265 begin_scope (sk_scoped_enum
, newtag
);
23267 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
23272 decl
= TREE_VALUE (e
);
23273 /* Note that in a template enum, the TREE_VALUE is the
23274 CONST_DECL, not the corresponding INTEGER_CST. */
23275 value
= tsubst_expr (DECL_INITIAL (decl
),
23276 args
, tf_warning_or_error
, NULL_TREE
,
23277 /*integral_constant_expression_p=*/true);
23279 /* Give this enumeration constant the correct access. */
23280 set_current_access_from_decl (decl
);
23282 /* Actually build the enumerator itself. Here we're assuming that
23283 enumerators can't have dependent attributes. */
23284 build_enumerator (DECL_NAME (decl
), value
, newtag
,
23285 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
23288 if (SCOPED_ENUM_P (newtag
))
23291 finish_enum_value_list (newtag
);
23292 finish_enum (newtag
);
23294 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
23295 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
23298 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23299 its type -- but without substituting the innermost set of template
23300 arguments. So, innermost set of template parameters will appear in
23304 get_mostly_instantiated_function_type (tree decl
)
23306 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23307 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
23310 /* Return truthvalue if we're processing a template different from
23311 the last one involved in diagnostics. */
23313 problematic_instantiation_changed (void)
23315 return current_tinst_level
!= last_error_tinst_level
;
23318 /* Remember current template involved in diagnostics. */
23320 record_last_problematic_instantiation (void)
23322 last_error_tinst_level
= current_tinst_level
;
23325 struct tinst_level
*
23326 current_instantiation (void)
23328 return current_tinst_level
;
23331 /* Return TRUE if current_function_decl is being instantiated, false
23335 instantiating_current_function_p (void)
23337 return (current_instantiation ()
23338 && current_instantiation ()->decl
== current_function_decl
);
23341 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23342 type. Return zero for ok, nonzero for disallowed. Issue error and
23343 warning messages under control of COMPLAIN. */
23346 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
23348 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
23350 else if (POINTER_TYPE_P (type
))
23352 else if (TYPE_PTRMEM_P (type
))
23354 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
23356 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
23358 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
23360 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
23362 /* A bound template template parm could later be instantiated to have a valid
23363 nontype parm type via an alias template. */
23364 else if (cxx_dialect
>= cxx11
23365 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23368 if (complain
& tf_error
)
23370 if (type
== error_mark_node
)
23371 inform (input_location
, "invalid template non-type parameter");
23373 error ("%q#T is not a valid type for a template non-type parameter",
23379 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23380 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23383 dependent_type_p_r (tree type
)
23389 A type is dependent if it is:
23391 -- a template parameter. Template template parameters are types
23392 for us (since TYPE_P holds true for them) so we handle
23394 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
23395 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
23397 /* -- a qualified-id with a nested-name-specifier which contains a
23398 class-name that names a dependent type or whose unqualified-id
23399 names a dependent type. */
23400 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23403 /* An alias template specialization can be dependent even if the
23404 resulting type is not. */
23405 if (dependent_alias_template_spec_p (type
))
23408 /* -- a cv-qualified type where the cv-unqualified type is
23410 No code is necessary for this bullet; the code below handles
23411 cv-qualified types, and we don't want to strip aliases with
23412 TYPE_MAIN_VARIANT because of DR 1558. */
23413 /* -- a compound type constructed from any dependent type. */
23414 if (TYPE_PTRMEM_P (type
))
23415 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
23416 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23418 else if (TYPE_PTR_P (type
)
23419 || TREE_CODE (type
) == REFERENCE_TYPE
)
23420 return dependent_type_p (TREE_TYPE (type
));
23421 else if (TREE_CODE (type
) == FUNCTION_TYPE
23422 || TREE_CODE (type
) == METHOD_TYPE
)
23426 if (dependent_type_p (TREE_TYPE (type
)))
23428 for (arg_type
= TYPE_ARG_TYPES (type
);
23430 arg_type
= TREE_CHAIN (arg_type
))
23431 if (dependent_type_p (TREE_VALUE (arg_type
)))
23435 /* -- an array type constructed from any dependent type or whose
23436 size is specified by a constant expression that is
23439 We checked for type- and value-dependence of the bounds in
23440 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23441 if (TREE_CODE (type
) == ARRAY_TYPE
)
23443 if (TYPE_DOMAIN (type
)
23444 && dependent_type_p (TYPE_DOMAIN (type
)))
23446 return dependent_type_p (TREE_TYPE (type
));
23449 /* -- a template-id in which either the template name is a template
23451 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23453 /* ... or any of the template arguments is a dependent type or
23454 an expression that is type-dependent or value-dependent. */
23455 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23456 && (any_dependent_template_arguments_p
23457 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23460 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23461 dependent; if the argument of the `typeof' expression is not
23462 type-dependent, then it should already been have resolved. */
23463 if (TREE_CODE (type
) == TYPEOF_TYPE
23464 || TREE_CODE (type
) == DECLTYPE_TYPE
23465 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23468 /* A template argument pack is dependent if any of its packed
23470 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23472 tree args
= ARGUMENT_PACK_ARGS (type
);
23473 int i
, len
= TREE_VEC_LENGTH (args
);
23474 for (i
= 0; i
< len
; ++i
)
23475 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23479 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23480 be template parameters. */
23481 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23484 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23487 /* The standard does not specifically mention types that are local
23488 to template functions or local classes, but they should be
23489 considered dependent too. For example:
23491 template <int I> void f() {
23496 The size of `E' cannot be known until the value of `I' has been
23497 determined. Therefore, `E' must be considered dependent. */
23498 scope
= TYPE_CONTEXT (type
);
23499 if (scope
&& TYPE_P (scope
))
23500 return dependent_type_p (scope
);
23501 /* Don't use type_dependent_expression_p here, as it can lead
23502 to infinite recursion trying to determine whether a lambda
23503 nested in a lambda is dependent (c++/47687). */
23504 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23505 && DECL_LANG_SPECIFIC (scope
)
23506 && DECL_TEMPLATE_INFO (scope
)
23507 && (any_dependent_template_arguments_p
23508 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23511 /* Other types are non-dependent. */
23515 /* Returns TRUE if TYPE is dependent, in the sense of
23516 [temp.dep.type]. Note that a NULL type is considered dependent. */
23519 dependent_type_p (tree type
)
23521 /* If there are no template parameters in scope, then there can't be
23522 any dependent types. */
23523 if (!processing_template_decl
)
23525 /* If we are not processing a template, then nobody should be
23526 providing us with a dependent type. */
23528 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23532 /* If the type is NULL, we have not computed a type for the entity
23533 in question; in that case, the type is dependent. */
23537 /* Erroneous types can be considered non-dependent. */
23538 if (type
== error_mark_node
)
23541 /* Getting here with global_type_node means we improperly called this
23542 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23543 gcc_checking_assert (type
!= global_type_node
);
23545 /* If we have not already computed the appropriate value for TYPE,
23547 if (!TYPE_DEPENDENT_P_VALID (type
))
23549 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23550 TYPE_DEPENDENT_P_VALID (type
) = 1;
23553 return TYPE_DEPENDENT_P (type
);
23556 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23557 lookup. In other words, a dependent type that is not the current
23561 dependent_scope_p (tree scope
)
23563 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23564 && !currently_open_class (scope
));
23567 /* T is a SCOPE_REF; return whether we need to consider it
23568 instantiation-dependent so that we can check access at instantiation
23569 time even though we know which member it resolves to. */
23572 instantiation_dependent_scope_ref_p (tree t
)
23574 if (DECL_P (TREE_OPERAND (t
, 1))
23575 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23576 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23577 TREE_OPERAND (t
, 1)))
23583 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23584 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23587 /* Note that this predicate is not appropriate for general expressions;
23588 only constant expressions (that satisfy potential_constant_expression)
23589 can be tested for value dependence. */
23592 value_dependent_expression_p (tree expression
)
23594 if (!processing_template_decl
|| expression
== NULL_TREE
)
23597 /* A name declared with a dependent type. */
23598 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
23601 switch (TREE_CODE (expression
))
23604 /* A dependent member function of the current instantiation. */
23605 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23607 case FUNCTION_DECL
:
23608 /* A dependent member function of the current instantiation. */
23609 if (DECL_CLASS_SCOPE_P (expression
)
23610 && dependent_type_p (DECL_CONTEXT (expression
)))
23614 case IDENTIFIER_NODE
:
23615 /* A name that has not been looked up -- must be dependent. */
23618 case TEMPLATE_PARM_INDEX
:
23619 /* A non-type template parm. */
23623 /* A non-type template parm. */
23624 if (DECL_TEMPLATE_PARM_P (expression
))
23626 return value_dependent_expression_p (DECL_INITIAL (expression
));
23629 /* A constant with literal type and is initialized
23630 with an expression that is value-dependent.
23632 Note that a non-dependent parenthesized initializer will have
23633 already been replaced with its constant value, so if we see
23634 a TREE_LIST it must be dependent. */
23635 if (DECL_INITIAL (expression
)
23636 && decl_constant_var_p (expression
)
23637 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
23638 /* cp_finish_decl doesn't fold reference initializers. */
23639 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
23640 || type_dependent_expression_p (DECL_INITIAL (expression
))
23641 || value_dependent_expression_p (DECL_INITIAL (expression
))))
23643 if (DECL_HAS_VALUE_EXPR_P (expression
))
23645 tree value_expr
= DECL_VALUE_EXPR (expression
);
23646 if (type_dependent_expression_p (value_expr
))
23651 case DYNAMIC_CAST_EXPR
:
23652 case STATIC_CAST_EXPR
:
23653 case CONST_CAST_EXPR
:
23654 case REINTERPRET_CAST_EXPR
:
23656 /* These expressions are value-dependent if the type to which
23657 the cast occurs is dependent or the expression being casted
23658 is value-dependent. */
23660 tree type
= TREE_TYPE (expression
);
23662 if (dependent_type_p (type
))
23665 /* A functional cast has a list of operands. */
23666 expression
= TREE_OPERAND (expression
, 0);
23669 /* If there are no operands, it must be an expression such
23670 as "int()". This should not happen for aggregate types
23671 because it would form non-constant expressions. */
23672 gcc_assert (cxx_dialect
>= cxx11
23673 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
23678 if (TREE_CODE (expression
) == TREE_LIST
)
23679 return any_value_dependent_elements_p (expression
);
23681 return value_dependent_expression_p (expression
);
23685 if (SIZEOF_EXPR_TYPE_P (expression
))
23686 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
23690 /* A `sizeof' expression is value-dependent if the operand is
23691 type-dependent or is a pack expansion. */
23692 expression
= TREE_OPERAND (expression
, 0);
23693 if (PACK_EXPANSION_P (expression
))
23695 else if (TYPE_P (expression
))
23696 return dependent_type_p (expression
);
23697 return instantiation_dependent_uneval_expression_p (expression
);
23699 case AT_ENCODE_EXPR
:
23700 /* An 'encode' expression is value-dependent if the operand is
23702 expression
= TREE_OPERAND (expression
, 0);
23703 return dependent_type_p (expression
);
23705 case NOEXCEPT_EXPR
:
23706 expression
= TREE_OPERAND (expression
, 0);
23707 return instantiation_dependent_uneval_expression_p (expression
);
23710 /* All instantiation-dependent expressions should also be considered
23711 value-dependent. */
23712 return instantiation_dependent_scope_ref_p (expression
);
23714 case COMPONENT_REF
:
23715 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
23716 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
23718 case NONTYPE_ARGUMENT_PACK
:
23719 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23720 is value-dependent. */
23722 tree values
= ARGUMENT_PACK_ARGS (expression
);
23723 int i
, len
= TREE_VEC_LENGTH (values
);
23725 for (i
= 0; i
< len
; ++i
)
23726 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
23734 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
23735 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
23736 || (type2
? dependent_type_p (type2
) : false));
23740 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23741 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
23744 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23745 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
23749 tree op
= TREE_OPERAND (expression
, 0);
23750 return (value_dependent_expression_p (op
)
23751 || has_value_dependent_address (op
));
23754 case REQUIRES_EXPR
:
23755 /* Treat all requires-expressions as value-dependent so
23756 we don't try to fold them. */
23760 return dependent_type_p (TREE_OPERAND (expression
, 0));
23764 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
23766 tree fn
= get_callee_fndecl (expression
);
23768 nargs
= call_expr_nargs (expression
);
23769 for (i
= 0; i
< nargs
; ++i
)
23771 tree op
= CALL_EXPR_ARG (expression
, i
);
23772 /* In a call to a constexpr member function, look through the
23773 implicit ADDR_EXPR on the object argument so that it doesn't
23774 cause the call to be considered value-dependent. We also
23775 look through it in potential_constant_expression. */
23776 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
23777 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
23778 && TREE_CODE (op
) == ADDR_EXPR
)
23779 op
= TREE_OPERAND (op
, 0);
23780 if (value_dependent_expression_p (op
))
23786 case TEMPLATE_ID_EXPR
:
23787 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23789 return type_dependent_expression_p (expression
)
23790 || variable_concept_p (TREE_OPERAND (expression
, 0));
23796 if (dependent_type_p (TREE_TYPE (expression
)))
23798 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
23799 if (value_dependent_expression_p (val
))
23805 /* Treat a GNU statement expression as dependent to avoid crashing
23806 under instantiate_non_dependent_expr; it can't be constant. */
23810 /* A constant expression is value-dependent if any subexpression is
23811 value-dependent. */
23812 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
23814 case tcc_reference
:
23816 case tcc_comparison
:
23818 case tcc_expression
:
23821 int i
, len
= cp_tree_operand_length (expression
);
23823 for (i
= 0; i
< len
; i
++)
23825 tree t
= TREE_OPERAND (expression
, i
);
23827 /* In some cases, some of the operands may be missing.l
23828 (For example, in the case of PREDECREMENT_EXPR, the
23829 amount to increment by may be missing.) That doesn't
23830 make the expression dependent. */
23831 if (t
&& value_dependent_expression_p (t
))
23842 /* The expression is not value-dependent. */
23846 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23847 [temp.dep.expr]. Note that an expression with no type is
23848 considered dependent. Other parts of the compiler arrange for an
23849 expression with type-dependent subexpressions to have no type, so
23850 this function doesn't have to be fully recursive. */
23853 type_dependent_expression_p (tree expression
)
23855 if (!processing_template_decl
)
23858 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
23861 /* An unresolved name is always dependent. */
23862 if (identifier_p (expression
)
23863 || TREE_CODE (expression
) == USING_DECL
23864 || TREE_CODE (expression
) == WILDCARD_DECL
)
23867 /* A fold expression is type-dependent. */
23868 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
23869 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
23870 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
23871 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
23874 /* Some expression forms are never type-dependent. */
23875 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
23876 || TREE_CODE (expression
) == SIZEOF_EXPR
23877 || TREE_CODE (expression
) == ALIGNOF_EXPR
23878 || TREE_CODE (expression
) == AT_ENCODE_EXPR
23879 || TREE_CODE (expression
) == NOEXCEPT_EXPR
23880 || TREE_CODE (expression
) == TRAIT_EXPR
23881 || TREE_CODE (expression
) == TYPEID_EXPR
23882 || TREE_CODE (expression
) == DELETE_EXPR
23883 || TREE_CODE (expression
) == VEC_DELETE_EXPR
23884 || TREE_CODE (expression
) == THROW_EXPR
23885 || TREE_CODE (expression
) == REQUIRES_EXPR
)
23888 /* The types of these expressions depends only on the type to which
23889 the cast occurs. */
23890 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
23891 || TREE_CODE (expression
) == STATIC_CAST_EXPR
23892 || TREE_CODE (expression
) == CONST_CAST_EXPR
23893 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
23894 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
23895 || TREE_CODE (expression
) == CAST_EXPR
)
23896 return dependent_type_p (TREE_TYPE (expression
));
23898 /* The types of these expressions depends only on the type created
23899 by the expression. */
23900 if (TREE_CODE (expression
) == NEW_EXPR
23901 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
23903 /* For NEW_EXPR tree nodes created inside a template, either
23904 the object type itself or a TREE_LIST may appear as the
23906 tree type
= TREE_OPERAND (expression
, 1);
23907 if (TREE_CODE (type
) == TREE_LIST
)
23908 /* This is an array type. We need to check array dimensions
23910 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
23911 || value_dependent_expression_p
23912 (TREE_OPERAND (TREE_VALUE (type
), 1));
23914 return dependent_type_p (type
);
23917 if (TREE_CODE (expression
) == SCOPE_REF
)
23919 tree scope
= TREE_OPERAND (expression
, 0);
23920 tree name
= TREE_OPERAND (expression
, 1);
23922 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23923 contains an identifier associated by name lookup with one or more
23924 declarations declared with a dependent type, or...a
23925 nested-name-specifier or qualified-id that names a member of an
23926 unknown specialization. */
23927 return (type_dependent_expression_p (name
)
23928 || dependent_scope_p (scope
));
23931 if (TREE_CODE (expression
) == TEMPLATE_DECL
23932 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
23933 return uses_outer_template_parms (expression
);
23935 if (TREE_CODE (expression
) == STMT_EXPR
)
23936 expression
= stmt_expr_value_expr (expression
);
23938 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
23943 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
23945 if (type_dependent_expression_p (elt
))
23951 /* A static data member of the current instantiation with incomplete
23952 array type is type-dependent, as the definition and specializations
23953 can have different bounds. */
23954 if (VAR_P (expression
)
23955 && DECL_CLASS_SCOPE_P (expression
)
23956 && dependent_type_p (DECL_CONTEXT (expression
))
23957 && VAR_HAD_UNKNOWN_BOUND (expression
))
23960 /* An array of unknown bound depending on a variadic parameter, eg:
23962 template<typename... Args>
23963 void foo (Args... args)
23965 int arr[] = { args... };
23968 template<int... vals>
23971 int arr[] = { vals... };
23974 If the array has no length and has an initializer, it must be that
23975 we couldn't determine its length in cp_complete_array_type because
23976 it is dependent. */
23977 if (VAR_P (expression
)
23978 && TREE_TYPE (expression
) != NULL_TREE
23979 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
23980 && !TYPE_DOMAIN (TREE_TYPE (expression
))
23981 && DECL_INITIAL (expression
))
23984 /* A function or variable template-id is type-dependent if it has any
23985 dependent template arguments. Note that we only consider the innermost
23986 template arguments here, since those are the ones that come from the
23987 template-id; the template arguments for the enclosing class do not make it
23988 type-dependent, they only make a member function value-dependent. */
23989 if (VAR_OR_FUNCTION_DECL_P (expression
)
23990 && DECL_LANG_SPECIFIC (expression
)
23991 && DECL_TEMPLATE_INFO (expression
)
23992 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
23993 && (any_dependent_template_arguments_p
23994 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
23997 /* Always dependent, on the number of arguments if nothing else. */
23998 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
24001 if (TREE_TYPE (expression
) == unknown_type_node
)
24003 if (TREE_CODE (expression
) == ADDR_EXPR
)
24004 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
24005 if (TREE_CODE (expression
) == COMPONENT_REF
24006 || TREE_CODE (expression
) == OFFSET_REF
)
24008 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
24010 expression
= TREE_OPERAND (expression
, 1);
24011 if (identifier_p (expression
))
24014 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24015 if (TREE_CODE (expression
) == SCOPE_REF
)
24018 if (BASELINK_P (expression
))
24020 if (BASELINK_OPTYPE (expression
)
24021 && dependent_type_p (BASELINK_OPTYPE (expression
)))
24023 expression
= BASELINK_FUNCTIONS (expression
);
24026 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
24028 if (any_dependent_template_arguments_p
24029 (TREE_OPERAND (expression
, 1)))
24031 expression
= TREE_OPERAND (expression
, 0);
24032 if (identifier_p (expression
))
24036 gcc_assert (TREE_CODE (expression
) == OVERLOAD
24037 || TREE_CODE (expression
) == FUNCTION_DECL
);
24039 for (lkp_iterator
iter (expression
); iter
; ++iter
)
24040 if (type_dependent_expression_p (*iter
))
24046 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
24048 /* Dependent type attributes might not have made it from the decl to
24050 if (DECL_P (expression
)
24051 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
24054 return (dependent_type_p (TREE_TYPE (expression
)));
24057 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24058 type-dependent if the expression refers to a member of the current
24059 instantiation and the type of the referenced member is dependent, or the
24060 class member access expression refers to a member of an unknown
24063 This function returns true if the OBJECT in such a class member access
24064 expression is of an unknown specialization. */
24067 type_dependent_object_expression_p (tree object
)
24069 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24071 if (TREE_CODE (object
) == IDENTIFIER_NODE
)
24073 tree scope
= TREE_TYPE (object
);
24074 return (!scope
|| dependent_scope_p (scope
));
24077 /* walk_tree callback function for instantiation_dependent_expression_p,
24078 below. Returns non-zero if a dependent subexpression is found. */
24081 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
24086 /* We don't have to worry about decltype currently because decltype
24087 of an instantiation-dependent expr is a dependent type. This
24088 might change depending on the resolution of DR 1172. */
24089 *walk_subtrees
= false;
24092 enum tree_code code
= TREE_CODE (*tp
);
24095 /* Don't treat an argument list as dependent just because it has no
24101 case TEMPLATE_PARM_INDEX
:
24104 /* Handle expressions with type operands. */
24108 case AT_ENCODE_EXPR
:
24110 tree op
= TREE_OPERAND (*tp
, 0);
24111 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
24112 op
= TREE_TYPE (op
);
24115 if (dependent_type_p (op
))
24119 *walk_subtrees
= false;
24126 case COMPONENT_REF
:
24127 if (identifier_p (TREE_OPERAND (*tp
, 1)))
24128 /* In a template, finish_class_member_access_expr creates a
24129 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24130 type-dependent, so that we can check access control at
24131 instantiation time (PR 42277). See also Core issue 1273. */
24136 if (instantiation_dependent_scope_ref_p (*tp
))
24141 /* Treat statement-expressions as dependent. */
24145 /* Treat requires-expressions as dependent. */
24146 case REQUIRES_EXPR
:
24150 /* Treat calls to function concepts as dependent. */
24151 if (function_concept_check_p (*tp
))
24155 case TEMPLATE_ID_EXPR
:
24156 /* And variable concepts. */
24157 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
24165 if (type_dependent_expression_p (*tp
))
24171 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24172 sense defined by the ABI:
24174 "An expression is instantiation-dependent if it is type-dependent
24175 or value-dependent, or it has a subexpression that is type-dependent
24176 or value-dependent."
24178 Except don't actually check value-dependence for unevaluated expressions,
24179 because in sizeof(i) we don't care about the value of i. Checking
24180 type-dependence will in turn check value-dependence of array bounds/template
24181 arguments as needed. */
24184 instantiation_dependent_uneval_expression_p (tree expression
)
24188 if (!processing_template_decl
)
24191 if (expression
== error_mark_node
)
24194 result
= cp_walk_tree_without_duplicates (&expression
,
24195 instantiation_dependent_r
, NULL
);
24196 return result
!= NULL_TREE
;
24199 /* As above, but also check value-dependence of the expression as a whole. */
24202 instantiation_dependent_expression_p (tree expression
)
24204 return (instantiation_dependent_uneval_expression_p (expression
)
24205 || value_dependent_expression_p (expression
));
24208 /* Like type_dependent_expression_p, but it also works while not processing
24209 a template definition, i.e. during substitution or mangling. */
24212 type_dependent_expression_p_push (tree expr
)
24215 ++processing_template_decl
;
24216 b
= type_dependent_expression_p (expr
);
24217 --processing_template_decl
;
24221 /* Returns TRUE if ARGS contains a type-dependent expression. */
24224 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
24229 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
24231 if (type_dependent_expression_p (arg
))
24237 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24238 expressions) contains any type-dependent expressions. */
24241 any_type_dependent_elements_p (const_tree list
)
24243 for (; list
; list
= TREE_CHAIN (list
))
24244 if (type_dependent_expression_p (TREE_VALUE (list
)))
24250 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24251 expressions) contains any value-dependent expressions. */
24254 any_value_dependent_elements_p (const_tree list
)
24256 for (; list
; list
= TREE_CHAIN (list
))
24257 if (value_dependent_expression_p (TREE_VALUE (list
)))
24263 /* Returns TRUE if the ARG (a template argument) is dependent. */
24266 dependent_template_arg_p (tree arg
)
24268 if (!processing_template_decl
)
24271 /* Assume a template argument that was wrongly written by the user
24272 is dependent. This is consistent with what
24273 any_dependent_template_arguments_p [that calls this function]
24275 if (!arg
|| arg
== error_mark_node
)
24278 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
24279 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
24281 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
24283 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24285 if (DECL_TEMPLATE_PARM_P (arg
))
24287 /* A member template of a dependent class is not necessarily
24288 type-dependent, but it is a dependent template argument because it
24289 will be a member of an unknown specialization to that template. */
24290 tree scope
= CP_DECL_CONTEXT (arg
);
24291 return TYPE_P (scope
) && dependent_type_p (scope
);
24293 else if (ARGUMENT_PACK_P (arg
))
24295 tree args
= ARGUMENT_PACK_ARGS (arg
);
24296 int i
, len
= TREE_VEC_LENGTH (args
);
24297 for (i
= 0; i
< len
; ++i
)
24299 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
24305 else if (TYPE_P (arg
))
24306 return dependent_type_p (arg
);
24308 return (type_dependent_expression_p (arg
)
24309 || value_dependent_expression_p (arg
));
24312 /* Returns true if ARGS (a collection of template arguments) contains
24313 any types that require structural equality testing. */
24316 any_template_arguments_need_structural_equality_p (tree args
)
24323 if (args
== error_mark_node
)
24326 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24328 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24329 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24331 tree arg
= TREE_VEC_ELT (level
, j
);
24332 tree packed_args
= NULL_TREE
;
24335 if (ARGUMENT_PACK_P (arg
))
24337 /* Look inside the argument pack. */
24338 packed_args
= ARGUMENT_PACK_ARGS (arg
);
24339 len
= TREE_VEC_LENGTH (packed_args
);
24342 for (k
= 0; k
< len
; ++k
)
24345 arg
= TREE_VEC_ELT (packed_args
, k
);
24347 if (error_operand_p (arg
))
24349 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
24351 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
24353 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
24354 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
24363 /* Returns true if ARGS (a collection of template arguments) contains
24364 any dependent arguments. */
24367 any_dependent_template_arguments_p (const_tree args
)
24374 if (args
== error_mark_node
)
24377 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
24379 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
24380 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
24381 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
24388 /* Returns TRUE if the template TMPL is type-dependent. */
24391 dependent_template_p (tree tmpl
)
24393 if (TREE_CODE (tmpl
) == OVERLOAD
)
24395 for (lkp_iterator
iter (tmpl
); iter
; ++iter
)
24396 if (dependent_template_p (*iter
))
24401 /* Template template parameters are dependent. */
24402 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
24403 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
24405 /* So are names that have not been looked up. */
24406 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
24411 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24414 dependent_template_id_p (tree tmpl
, tree args
)
24416 return (dependent_template_p (tmpl
)
24417 || any_dependent_template_arguments_p (args
));
24420 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24424 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24428 if (!processing_template_decl
)
24431 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24433 tree decl
= TREE_VEC_ELT (declv
, i
);
24434 tree init
= TREE_VEC_ELT (initv
, i
);
24435 tree cond
= TREE_VEC_ELT (condv
, i
);
24436 tree incr
= TREE_VEC_ELT (incrv
, i
);
24438 if (type_dependent_expression_p (decl
)
24439 || TREE_CODE (decl
) == SCOPE_REF
)
24442 if (init
&& type_dependent_expression_p (init
))
24445 if (type_dependent_expression_p (cond
))
24448 if (COMPARISON_CLASS_P (cond
)
24449 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24450 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24453 if (TREE_CODE (incr
) == MODOP_EXPR
)
24455 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24456 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24459 else if (type_dependent_expression_p (incr
))
24461 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24463 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24465 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24467 tree t
= TREE_OPERAND (incr
, 1);
24468 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24469 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24478 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24479 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24480 no such TYPE can be found. Note that this function peers inside
24481 uninstantiated templates and therefore should be used only in
24482 extremely limited situations. ONLY_CURRENT_P restricts this
24483 peering to the currently open classes hierarchy (which is required
24484 when comparing types). */
24487 resolve_typename_type (tree type
, bool only_current_p
)
24496 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24498 scope
= TYPE_CONTEXT (type
);
24499 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24500 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24501 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24502 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24503 identifier of the TYPENAME_TYPE anymore.
24504 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24505 TYPENAME_TYPE instead, we avoid messing up with a possible
24506 typedef variant case. */
24507 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24509 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24510 it first before we can figure out what NAME refers to. */
24511 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24513 if (TYPENAME_IS_RESOLVING_P (scope
))
24514 /* Given a class template A with a dependent base with nested type C,
24515 typedef typename A::C::C C will land us here, as trying to resolve
24516 the initial A::C leads to the local C typedef, which leads back to
24517 A::C::C. So we break the recursion now. */
24520 scope
= resolve_typename_type (scope
, only_current_p
);
24522 /* If we don't know what SCOPE refers to, then we cannot resolve the
24524 if (!CLASS_TYPE_P (scope
))
24526 /* If this is a typedef, we don't want to look inside (c++/11987). */
24527 if (typedef_variant_p (type
))
24529 /* If SCOPE isn't the template itself, it will not have a valid
24530 TYPE_FIELDS list. */
24531 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24532 /* scope is either the template itself or a compatible instantiation
24533 like X<T>, so look up the name in the original template. */
24534 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24535 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24536 gcc_checking_assert (uses_template_parms (scope
));
24537 /* If scope has no fields, it can't be a current instantiation. Check this
24538 before currently_open_class to avoid infinite recursion (71515). */
24539 if (!TYPE_FIELDS (scope
))
24541 /* If the SCOPE is not the current instantiation, there's no reason
24542 to look inside it. */
24543 if (only_current_p
&& !currently_open_class (scope
))
24545 /* Enter the SCOPE so that name lookup will be resolved as if we
24546 were in the class definition. In particular, SCOPE will no
24547 longer be considered a dependent type. */
24548 pushed_scope
= push_scope (scope
);
24549 /* Look up the declaration. */
24550 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24551 tf_warning_or_error
);
24553 result
= NULL_TREE
;
24555 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24556 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24559 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
24560 && TREE_CODE (decl
) == TYPE_DECL
)
24562 result
= TREE_TYPE (decl
);
24563 if (result
== error_mark_node
)
24564 result
= NULL_TREE
;
24566 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
24567 && DECL_CLASS_TEMPLATE_P (decl
))
24571 /* Obtain the template and the arguments. */
24572 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
24573 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
24574 /* Instantiate the template. */
24575 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24576 /*entering_scope=*/0,
24577 tf_error
| tf_user
);
24578 if (result
== error_mark_node
)
24579 result
= NULL_TREE
;
24582 /* Leave the SCOPE. */
24584 pop_scope (pushed_scope
);
24586 /* If we failed to resolve it, return the original typename. */
24590 /* If lookup found a typename type, resolve that too. */
24591 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
24593 /* Ill-formed programs can cause infinite recursion here, so we
24594 must catch that. */
24595 TYPENAME_IS_RESOLVING_P (result
) = 1;
24596 result
= resolve_typename_type (result
, only_current_p
);
24597 TYPENAME_IS_RESOLVING_P (result
) = 0;
24600 /* Qualify the resulting type. */
24601 quals
= cp_type_quals (type
);
24603 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
24608 /* EXPR is an expression which is not type-dependent. Return a proxy
24609 for EXPR that can be used to compute the types of larger
24610 expressions containing EXPR. */
24613 build_non_dependent_expr (tree expr
)
24617 /* When checking, try to get a constant value for all non-dependent
24618 expressions in order to expose bugs in *_dependent_expression_p
24619 and constexpr. This can affect code generation, see PR70704, so
24620 only do this for -fchecking=2. */
24621 if (flag_checking
> 1
24622 && cxx_dialect
>= cxx11
24623 /* Don't do this during nsdmi parsing as it can lead to
24624 unexpected recursive instantiations. */
24625 && !parsing_nsdmi ()
24626 /* Don't do this during concept expansion either and for
24627 the same reason. */
24628 && !expanding_concept ())
24629 fold_non_dependent_expr (expr
);
24631 /* Preserve OVERLOADs; the functions must be available to resolve
24634 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
24635 inner_expr
= stmt_expr_value_expr (inner_expr
);
24636 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
24637 inner_expr
= TREE_OPERAND (inner_expr
, 0);
24638 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
24639 inner_expr
= TREE_OPERAND (inner_expr
, 1);
24640 if (is_overloaded_fn (inner_expr
)
24641 || TREE_CODE (inner_expr
) == OFFSET_REF
)
24643 /* There is no need to return a proxy for a variable. */
24646 /* Preserve string constants; conversions from string constants to
24647 "char *" are allowed, even though normally a "const char *"
24648 cannot be used to initialize a "char *". */
24649 if (TREE_CODE (expr
) == STRING_CST
)
24651 /* Preserve void and arithmetic constants, as an optimization -- there is no
24652 reason to create a new node. */
24653 if (TREE_CODE (expr
) == VOID_CST
24654 || TREE_CODE (expr
) == INTEGER_CST
24655 || TREE_CODE (expr
) == REAL_CST
)
24657 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24658 There is at least one place where we want to know that a
24659 particular expression is a throw-expression: when checking a ?:
24660 expression, there are special rules if the second or third
24661 argument is a throw-expression. */
24662 if (TREE_CODE (expr
) == THROW_EXPR
)
24665 /* Don't wrap an initializer list, we need to be able to look inside. */
24666 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
24669 /* Don't wrap a dummy object, we need to be able to test for it. */
24670 if (is_dummy_object (expr
))
24673 if (TREE_CODE (expr
) == COND_EXPR
)
24674 return build3 (COND_EXPR
,
24676 TREE_OPERAND (expr
, 0),
24677 (TREE_OPERAND (expr
, 1)
24678 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
24679 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
24680 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
24681 if (TREE_CODE (expr
) == COMPOUND_EXPR
24682 && !COMPOUND_EXPR_OVERLOADED (expr
))
24683 return build2 (COMPOUND_EXPR
,
24685 TREE_OPERAND (expr
, 0),
24686 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
24688 /* If the type is unknown, it can't really be non-dependent */
24689 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
24691 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24692 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
24695 /* ARGS is a vector of expressions as arguments to a function call.
24696 Replace the arguments with equivalent non-dependent expressions.
24697 This modifies ARGS in place. */
24700 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
24705 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
24707 tree newarg
= build_non_dependent_expr (arg
);
24709 (*args
)[ix
] = newarg
;
24713 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24714 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24715 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24718 make_auto_1 (tree name
, bool set_canonical
)
24720 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
24721 TYPE_NAME (au
) = build_decl (input_location
,
24722 TYPE_DECL
, name
, au
);
24723 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
24724 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
24725 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
24726 TYPE_NAME (au
), NULL_TREE
);
24728 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
24729 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
24730 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
24736 make_decltype_auto (void)
24738 return make_auto_1 (decltype_auto_identifier
, true);
24744 return make_auto_1 (auto_identifier
, true);
24747 /* Return a C++17 deduction placeholder for class template TMPL. */
24750 make_template_placeholder (tree tmpl
)
24752 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
24753 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
24757 /* Make a "constrained auto" type-specifier. This is an
24758 auto type with constraints that must be associated after
24759 deduction. The constraint is formed from the given
24760 CONC and its optional sequence of arguments, which are
24761 non-null if written as partial-concept-id. */
24764 make_constrained_auto (tree con
, tree args
)
24766 tree type
= make_auto_1 (auto_identifier
, false);
24768 /* Build the constraint. */
24769 tree tmpl
= DECL_TI_TEMPLATE (con
);
24770 tree expr
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
24771 expr
= build_concept_check (expr
, type
, args
);
24773 tree constr
= normalize_expression (expr
);
24774 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
24776 /* Our canonical type depends on the constraint. */
24777 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
24779 /* Attach the constraint to the type declaration. */
24780 tree decl
= TYPE_NAME (type
);
24784 /* Given type ARG, return std::initializer_list<ARG>. */
24789 tree std_init_list
= get_namespace_binding (std_node
, init_list_identifier
);
24791 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
24793 error ("deducing from brace-enclosed initializer list requires "
24794 "#include <initializer_list>");
24795 return error_mark_node
;
24797 argvec
= make_tree_vec (1);
24798 TREE_VEC_ELT (argvec
, 0) = arg
;
24799 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
24800 NULL_TREE
, 0, tf_warning_or_error
);
24803 /* Replace auto in TYPE with std::initializer_list<auto>. */
24806 listify_autos (tree type
, tree auto_node
)
24808 tree init_auto
= listify (auto_node
);
24809 tree argvec
= make_tree_vec (1);
24810 TREE_VEC_ELT (argvec
, 0) = init_auto
;
24811 if (processing_template_decl
)
24812 argvec
= add_to_template_args (current_template_args (), argvec
);
24813 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
24816 /* Hash traits for hashing possibly constrained 'auto'
24817 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24819 struct auto_hash
: default_hash_traits
<tree
>
24821 static inline hashval_t
hash (tree
);
24822 static inline bool equal (tree
, tree
);
24825 /* Hash the 'auto' T. */
24828 auto_hash::hash (tree t
)
24830 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
24831 /* Matching constrained-type-specifiers denote the same template
24832 parameter, so hash the constraint. */
24833 return hash_placeholder_constraint (c
);
24835 /* But unconstrained autos are all separate, so just hash the pointer. */
24836 return iterative_hash_object (t
, 0);
24839 /* Compare two 'auto's. */
24842 auto_hash::equal (tree t1
, tree t2
)
24847 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
24848 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
24850 /* Two unconstrained autos are distinct. */
24854 return equivalent_placeholder_constraints (c1
, c2
);
24857 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24858 constrained) auto, add it to the vector. */
24861 extract_autos_r (tree t
, void *data
)
24863 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
24866 /* All the autos were built with index 0; fix that up now. */
24867 tree
*p
= hash
.find_slot (t
, INSERT
);
24870 /* If this is a repeated constrained-type-specifier, use the index we
24872 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
24875 /* Otherwise this is new, so use the current count. */
24877 idx
= hash
.elements () - 1;
24879 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
24882 /* Always keep walking. */
24886 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24887 says they can appear anywhere in the type. */
24890 extract_autos (tree type
)
24892 hash_set
<tree
> visited
;
24893 hash_table
<auto_hash
> hash (2);
24895 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
24897 tree tree_vec
= make_tree_vec (hash
.elements());
24898 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
24899 iter
!= hash
.end(); ++iter
)
24902 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
24903 TREE_VEC_ELT (tree_vec
, i
)
24904 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
24910 /* The stem for deduction guide names. */
24911 const char *const dguide_base
= "__dguide_";
24913 /* Return the name for a deduction guide for class template TMPL. */
24916 dguide_name (tree tmpl
)
24918 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
24919 tree tname
= TYPE_IDENTIFIER (type
);
24920 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
24921 + IDENTIFIER_LENGTH (tname
));
24922 memcpy (buf
, dguide_base
, strlen (dguide_base
));
24923 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
24924 IDENTIFIER_LENGTH (tname
) + 1);
24925 tree dname
= get_identifier (buf
);
24926 TREE_TYPE (dname
) = type
;
24930 /* True if NAME is the name of a deduction guide. */
24933 dguide_name_p (tree name
)
24935 return (TREE_TYPE (name
)
24936 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
24937 strlen (dguide_base
)));
24940 /* True if FN is a deduction guide. */
24943 deduction_guide_p (const_tree fn
)
24946 if (tree name
= DECL_NAME (fn
))
24947 return dguide_name_p (name
);
24951 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24954 copy_guide_p (const_tree fn
)
24956 gcc_assert (deduction_guide_p (fn
));
24957 if (!DECL_ARTIFICIAL (fn
))
24959 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn
));
24960 return (TREE_CHAIN (parms
) == void_list_node
24961 && same_type_p (TREE_VALUE (parms
), TREE_TYPE (DECL_NAME (fn
))));
24964 /* True if FN is a guide generated from a constructor template. */
24967 template_guide_p (const_tree fn
)
24969 gcc_assert (deduction_guide_p (fn
));
24970 if (!DECL_ARTIFICIAL (fn
))
24972 if (tree ctor
= DECL_ABSTRACT_ORIGIN (fn
))
24974 tree tmpl
= DECL_TI_TEMPLATE (ctor
);
24975 return PRIMARY_TEMPLATE_P (tmpl
);
24980 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24981 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24982 template parameter types. Note that the handling of template template
24983 parameters relies on current_template_parms being set appropriately for the
24987 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
24988 tree tsubst_args
, tsubst_flags_t complain
)
24990 tree oldidx
= get_template_parm_index (olddecl
);
24993 if (TREE_CODE (olddecl
) == TYPE_DECL
24994 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
24996 tree oldtype
= TREE_TYPE (olddecl
);
24997 newtype
= cxx_make_type (TREE_CODE (oldtype
));
24998 TYPE_MAIN_VARIANT (newtype
) = newtype
;
24999 if (TREE_CODE (oldtype
) == TEMPLATE_TYPE_PARM
)
25000 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype
)
25001 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype
);
25004 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
25005 complain
, NULL_TREE
);
25008 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
25009 DECL_NAME (olddecl
), newtype
);
25010 SET_DECL_TEMPLATE_PARM_P (newdecl
);
25013 if (TREE_CODE (olddecl
) == TYPE_DECL
25014 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25016 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
25017 = build_template_parm_index (index
, level
, level
,
25019 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25020 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25021 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
25022 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
25024 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
25026 DECL_TEMPLATE_RESULT (newdecl
)
25027 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
25028 DECL_NAME (olddecl
), newtype
);
25029 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
25030 // First create a copy (ttargs) of tsubst_args with an
25031 // additional level for the template template parameter's own
25032 // template parameters (ttparms).
25033 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
25034 (DECL_TEMPLATE_PARMS (olddecl
)));
25035 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
25036 tree ttargs
= make_tree_vec (depth
+ 1);
25037 for (int i
= 0; i
< depth
; ++i
)
25038 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
25039 TREE_VEC_ELT (ttargs
, depth
)
25040 = template_parms_level_to_args (ttparms
);
25041 // Substitute ttargs into ttparms to fix references to
25042 // other template parameters.
25043 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25045 // Now substitute again with args based on tparms, to reduce
25046 // the level of the ttparms.
25047 ttargs
= current_template_args ();
25048 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
25050 // Finally, tack the adjusted parms onto tparms.
25051 ttparms
= tree_cons (size_int (depth
), ttparms
,
25052 current_template_parms
);
25053 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
25058 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
25060 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
25061 TREE_CODE (oldconst
),
25062 DECL_NAME (oldconst
), newtype
);
25063 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
25064 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
25065 SET_DECL_TEMPLATE_PARM_P (newconst
);
25066 newidx
= build_template_parm_index (index
, level
, level
,
25067 newconst
, newtype
);
25068 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
25069 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
25070 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
25076 /* Returns a C++17 class deduction guide template based on the constructor
25077 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25078 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25081 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
25083 tree type
, tparms
, targs
, fparms
, fargs
, ci
;
25084 bool memtmpl
= false;
25091 bool copy_p
= TREE_CODE (type
) == REFERENCE_TYPE
;
25094 type
= TREE_TYPE (type
);
25095 fparms
= tree_cons (NULL_TREE
, type
, void_list_node
);
25098 fparms
= void_list_node
;
25100 tree ctmpl
= CLASSTYPE_TI_TEMPLATE (type
);
25101 tparms
= DECL_TEMPLATE_PARMS (ctmpl
);
25102 targs
= CLASSTYPE_TI_ARGS (type
);
25105 loc
= DECL_SOURCE_LOCATION (ctmpl
);
25106 explicit_p
= false;
25111 ctor
= tsubst (ctor
, outer_args
, complain
, ctor
);
25112 type
= DECL_CONTEXT (ctor
);
25114 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
25117 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
25120 fn_tmpl
= DECL_TI_TEMPLATE (ctor
);
25122 tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
25123 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25124 fully specialized args for the enclosing class. Strip those off, as
25125 the deduction guide won't have those template parameters. */
25126 targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
25127 TMPL_PARMS_DEPTH (tparms
));
25128 /* Discard the 'this' parameter. */
25129 fparms
= FUNCTION_ARG_CHAIN (ctor
);
25130 fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
25131 ci
= get_constraints (ctor
);
25132 loc
= DECL_SOURCE_LOCATION (ctor
);
25133 explicit_p
= DECL_NONCONVERTING_P (ctor
);
25135 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
25139 /* For a member template constructor, we need to flatten the two
25140 template parameter lists into one, and then adjust the function
25141 signature accordingly. This gets...complicated. */
25142 ++processing_template_decl
;
25143 tree save_parms
= current_template_parms
;
25145 /* For a member template we should have two levels of parms/args, one
25146 for the class and one for the constructor. We stripped
25147 specialized args for further enclosing classes above. */
25148 const int depth
= 2;
25149 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
25151 /* Template args for translating references to the two-level template
25152 parameters into references to the one-level template parameters we
25154 tree tsubst_args
= copy_node (targs
);
25155 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
25156 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
25158 /* Template parms for the constructor template. */
25159 tree ftparms
= TREE_VALUE (tparms
);
25160 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
25161 /* Template parms for the class template. */
25162 tparms
= TREE_CHAIN (tparms
);
25163 tree ctparms
= TREE_VALUE (tparms
);
25164 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
25165 /* Template parms for the deduction guide start as a copy of the
25166 template parms for the class. We set current_template_parms for
25167 lookup_template_class_1. */
25168 current_template_parms
= tparms
= copy_node (tparms
);
25169 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
25170 for (unsigned i
= 0; i
< clen
; ++i
)
25171 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
25173 /* Now we need to rewrite the constructor parms to append them to the
25175 for (unsigned i
= 0; i
< flen
; ++i
)
25177 unsigned index
= i
+ clen
;
25178 unsigned level
= 1;
25179 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
25180 tree olddecl
= TREE_VALUE (oldelt
);
25181 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
25182 tsubst_args
, complain
);
25183 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
25184 tsubst_args
, complain
, ctor
);
25185 tree list
= build_tree_list (newdef
, newdecl
);
25186 TEMPLATE_PARM_CONSTRAINTS (list
)
25187 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
25188 tsubst_args
, complain
, ctor
);
25189 TREE_VEC_ELT (new_vec
, index
) = list
;
25190 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
25193 /* Now we have a final set of template parms to substitute into the
25194 function signature. */
25195 targs
= template_parms_to_args (tparms
);
25196 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
25198 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
25200 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
25202 current_template_parms
= save_parms
;
25203 --processing_template_decl
;
25209 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25210 tparms
= copy_node (tparms
);
25211 INNERMOST_TEMPLATE_PARMS (tparms
)
25212 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms
));
25215 tree fntype
= build_function_type (type
, fparms
);
25216 tree ded_fn
= build_lang_decl_loc (loc
,
25218 dguide_name (type
), fntype
);
25219 DECL_ARGUMENTS (ded_fn
) = fargs
;
25220 DECL_ARTIFICIAL (ded_fn
) = true;
25221 DECL_NONCONVERTING_P (ded_fn
) = explicit_p
;
25222 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
25223 DECL_ARTIFICIAL (ded_tmpl
) = true;
25224 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
25225 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
25226 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
25227 DECL_PRIMARY_TEMPLATE (ded_tmpl
) = ded_tmpl
;
25229 DECL_ABSTRACT_ORIGIN (ded_fn
) = ctor
;
25231 set_constraints (ded_tmpl
, ci
);
25236 /* Deduce template arguments for the class template placeholder PTYPE for
25237 template TMPL based on the initializer INIT, and return the resulting
25241 do_class_deduction (tree ptype
, tree tmpl
, tree init
, int flags
,
25242 tsubst_flags_t complain
)
25244 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
25246 /* We should have handled this in the caller. */
25247 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
25249 if (complain
& tf_error
)
25250 error ("non-class template %qT used without template arguments", tmpl
);
25251 return error_mark_node
;
25254 tree type
= TREE_TYPE (tmpl
);
25256 vec
<tree
,va_gc
> *args
;
25257 if (init
== NULL_TREE
25258 || TREE_CODE (init
) == TREE_LIST
)
25259 args
= make_tree_vector_from_list (init
);
25260 else if (BRACE_ENCLOSED_INITIALIZER_P (init
)
25261 && !TYPE_HAS_LIST_CTOR (type
)
25262 && !is_std_init_list (type
))
25263 args
= make_tree_vector_from_ctor (init
);
25265 args
= make_tree_vector_single (init
);
25267 tree dname
= dguide_name (tmpl
);
25268 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
25269 /*type*/false, /*complain*/false,
25271 bool elided
= false;
25272 if (cands
== error_mark_node
)
25275 /* Prune explicit deduction guides in copy-initialization context. */
25276 if (flags
& LOOKUP_ONLYCONVERTING
)
25278 for (lkp_iterator
iter (cands
); !elided
&& iter
; ++iter
)
25279 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25284 /* Found a nonconverting guide, prune the candidates. */
25285 tree pruned
= NULL_TREE
;
25286 for (lkp_iterator
iter (cands
); iter
; ++iter
)
25287 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter
)))
25288 pruned
= lookup_add (*iter
, pruned
);
25294 tree outer_args
= NULL_TREE
;
25295 if (DECL_CLASS_SCOPE_P (tmpl
)
25296 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
25298 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
25299 type
= TREE_TYPE (most_general_template (tmpl
));
25302 bool saw_ctor
= false;
25303 if (CLASSTYPE_METHOD_VEC (type
))
25304 // FIXME cache artificial deduction guides
25305 for (ovl_iterator
iter (CLASSTYPE_CONSTRUCTORS (type
));
25308 tree guide
= build_deduction_guide (*iter
, outer_args
, complain
);
25309 if ((flags
& LOOKUP_ONLYCONVERTING
)
25310 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25313 cands
= lookup_add (guide
, cands
);
25318 if (args
->length () < 2)
25320 tree gtype
= NULL_TREE
;
25322 if (args
->length () == 1)
25323 gtype
= build_reference_type (type
);
25324 else if (!saw_ctor
)
25329 tree guide
= build_deduction_guide (gtype
, outer_args
, complain
);
25330 if ((flags
& LOOKUP_ONLYCONVERTING
)
25331 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide
)))
25334 cands
= lookup_add (guide
, cands
);
25338 if (elided
&& !cands
)
25340 error ("cannot deduce template arguments for copy-initialization"
25341 " of %qT, as it has no non-explicit deduction guides or "
25342 "user-declared constructors", type
);
25343 return error_mark_node
;
25346 ++cp_unevaluated_operand
;
25347 tree t
= build_new_function_call (cands
, &args
, tf_decltype
);
25349 if (t
== error_mark_node
&& (complain
& tf_warning_or_error
))
25351 error ("class template argument deduction failed:");
25352 t
= build_new_function_call (cands
, &args
, complain
| tf_decltype
);
25354 inform (input_location
, "explicit deduction guides not considered "
25355 "for copy-initialization");
25358 --cp_unevaluated_operand
;
25359 release_tree_vector (args
);
25361 return TREE_TYPE (t
);
25364 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25365 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25368 do_auto_deduction (tree type
, tree init
, tree auto_node
)
25370 return do_auto_deduction (type
, init
, auto_node
,
25371 tf_warning_or_error
,
25375 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25376 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25377 The CONTEXT determines the context in which auto deduction is performed
25378 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25379 OUTER_TARGS are used during template argument deduction
25380 (context == adc_unify) to properly substitute the result, and is ignored
25383 For partial-concept-ids, extra args may be appended to the list of deduced
25384 template arguments prior to determining constraint satisfaction. */
25387 do_auto_deduction (tree type
, tree init
, tree auto_node
,
25388 tsubst_flags_t complain
, auto_deduction_context context
,
25389 tree outer_targs
, int flags
)
25393 if (init
== error_mark_node
)
25394 return error_mark_node
;
25396 if (init
&& type_dependent_expression_p (init
)
25397 && context
!= adc_unify
)
25398 /* Defining a subset of type-dependent expressions that we can deduce
25399 from ahead of time isn't worth the trouble. */
25402 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
25403 /* C++17 class template argument deduction. */
25404 return do_class_deduction (type
, tmpl
, init
, flags
, complain
);
25406 if (TREE_TYPE (init
) == NULL_TREE
)
25407 /* Nothing we can do with this, even in deduction context. */
25410 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25411 with either a new invented type template parameter U or, if the
25412 initializer is a braced-init-list (8.5.4), with
25413 std::initializer_list<U>. */
25414 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
25416 if (!DIRECT_LIST_INIT_P (init
))
25417 type
= listify_autos (type
, auto_node
);
25418 else if (CONSTRUCTOR_NELTS (init
) == 1)
25419 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
25422 if (complain
& tf_warning_or_error
)
25424 if (permerror (input_location
, "direct-list-initialization of "
25425 "%<auto%> requires exactly one element"))
25426 inform (input_location
,
25427 "for deduction to %<std::initializer_list%>, use copy-"
25428 "list-initialization (i.e. add %<=%> before the %<{%>)");
25430 type
= listify_autos (type
, auto_node
);
25434 if (type
== error_mark_node
)
25435 return error_mark_node
;
25437 init
= resolve_nondeduced_context (init
, complain
);
25439 if (context
== adc_decomp_type
25440 && auto_node
== type
25441 && init
!= error_mark_node
25442 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
25443 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25444 and initializer has array type, deduce cv-qualified array type. */
25445 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
25447 else if (AUTO_IS_DECLTYPE (auto_node
))
25449 bool id
= (DECL_P (init
)
25450 || ((TREE_CODE (init
) == COMPONENT_REF
25451 || TREE_CODE (init
) == SCOPE_REF
)
25452 && !REF_PARENTHESIZED_P (init
)));
25453 targs
= make_tree_vec (1);
25454 TREE_VEC_ELT (targs
, 0)
25455 = finish_decltype_type (init
, id
, tf_warning_or_error
);
25456 if (type
!= auto_node
)
25458 if (complain
& tf_error
)
25459 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
25460 return error_mark_node
;
25465 tree parms
= build_tree_list (NULL_TREE
, type
);
25469 tparms
= extract_autos (type
);
25472 tparms
= make_tree_vec (1);
25473 TREE_VEC_ELT (tparms
, 0)
25474 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
25477 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
25478 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25479 DEDUCE_CALL
, LOOKUP_NORMAL
,
25480 NULL
, /*explain_p=*/false);
25483 if (processing_template_decl
)
25484 /* Try again at instantiation time. */
25486 if (type
&& type
!= error_mark_node
25487 && (complain
& tf_error
))
25488 /* If type is error_mark_node a diagnostic must have been
25489 emitted by now. Also, having a mention to '<type error>'
25490 in the diagnostic is not really useful to the user. */
25492 if (cfun
&& auto_node
== current_function_auto_return_pattern
25493 && LAMBDA_FUNCTION_P (current_function_decl
))
25494 error ("unable to deduce lambda return type from %qE", init
);
25496 error ("unable to deduce %qT from %qE", type
, init
);
25497 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
25498 DEDUCE_CALL
, LOOKUP_NORMAL
,
25499 NULL
, /*explain_p=*/true);
25501 return error_mark_node
;
25505 /* Check any placeholder constraints against the deduced type. */
25506 if (flag_concepts
&& !processing_template_decl
)
25507 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
25509 /* Use the deduced type to check the associated constraints. If we
25510 have a partial-concept-id, rebuild the argument list so that
25511 we check using the extra arguments. */
25512 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
25513 tree cargs
= CHECK_CONSTR_ARGS (constr
);
25514 if (TREE_VEC_LENGTH (cargs
) > 1)
25516 cargs
= copy_node (cargs
);
25517 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
25521 if (!constraints_satisfied_p (constr
, cargs
))
25523 if (complain
& tf_warning_or_error
)
25527 case adc_unspecified
:
25529 error("placeholder constraints not satisfied");
25531 case adc_variable_type
:
25532 case adc_decomp_type
:
25533 error ("deduced initializer does not satisfy "
25534 "placeholder constraints");
25536 case adc_return_type
:
25537 error ("deduced return type does not satisfy "
25538 "placeholder constraints");
25540 case adc_requirement
:
25541 error ("deduced expression type does not satisfy "
25542 "placeholder constraints");
25545 diagnose_constraints (input_location
, constr
, targs
);
25547 return error_mark_node
;
25551 if (processing_template_decl
&& context
!= adc_unify
)
25552 outer_targs
= current_template_args ();
25553 targs
= add_to_template_args (outer_targs
, targs
);
25554 return tsubst (type
, targs
, complain
, NULL_TREE
);
25557 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25561 splice_late_return_type (tree type
, tree late_return_type
)
25563 if (is_auto (type
))
25565 if (late_return_type
)
25566 return late_return_type
;
25568 tree idx
= get_template_parm_index (type
);
25569 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
25570 /* In an abbreviated function template we didn't know we were dealing
25571 with a function template when we saw the auto return type, so update
25572 it to have the correct level. */
25573 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
25578 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25579 'decltype(auto)' or a deduced class template. */
25582 is_auto (const_tree type
)
25584 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25585 && (TYPE_IDENTIFIER (type
) == auto_identifier
25586 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
25587 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
25593 /* for_each_template_parm callback for type_uses_auto. */
25596 is_auto_r (tree tp
, void */
*data*/
)
25598 return is_auto (tp
);
25601 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25602 a use of `auto'. Returns NULL_TREE otherwise. */
25605 type_uses_auto (tree type
)
25607 if (type
== NULL_TREE
)
25609 else if (flag_concepts
)
25611 /* The Concepts TS allows multiple autos in one type-specifier; just
25612 return the first one we find, do_auto_deduction will collect all of
25614 if (uses_template_parms (type
))
25615 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
25616 /*visited*/NULL
, /*nondeduced*/true);
25621 return find_type_usage (type
, is_auto
);
25624 /* For a given template T, return the vector of typedefs referenced
25625 in T for which access check is needed at T instantiation time.
25626 T is either a FUNCTION_DECL or a RECORD_TYPE.
25627 Those typedefs were added to T by the function
25628 append_type_to_template_for_access_check. */
25630 vec
<qualified_typedef_usage_t
, va_gc
> *
25631 get_types_needing_access_check (tree t
)
25634 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
25636 if (!t
|| t
== error_mark_node
)
25639 if (!(ti
= get_template_info (t
)))
25642 if (CLASS_TYPE_P (t
)
25643 || TREE_CODE (t
) == FUNCTION_DECL
)
25645 if (!TI_TEMPLATE (ti
))
25648 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
25654 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25655 tied to T. That list of typedefs will be access checked at
25656 T instantiation time.
25657 T is either a FUNCTION_DECL or a RECORD_TYPE.
25658 TYPE_DECL is a TYPE_DECL node representing a typedef.
25659 SCOPE is the scope through which TYPE_DECL is accessed.
25660 LOCATION is the location of the usage point of TYPE_DECL.
25662 This function is a subroutine of
25663 append_type_to_template_for_access_check. */
25666 append_type_to_template_for_access_check_1 (tree t
,
25669 location_t location
)
25671 qualified_typedef_usage_t typedef_usage
;
25674 if (!t
|| t
== error_mark_node
)
25677 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
25678 || CLASS_TYPE_P (t
))
25680 && TREE_CODE (type_decl
) == TYPE_DECL
25683 if (!(ti
= get_template_info (t
)))
25686 gcc_assert (TI_TEMPLATE (ti
));
25688 typedef_usage
.typedef_decl
= type_decl
;
25689 typedef_usage
.context
= scope
;
25690 typedef_usage
.locus
= location
;
25692 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
25695 /* Append TYPE_DECL to the template TEMPL.
25696 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25697 At TEMPL instanciation time, TYPE_DECL will be checked to see
25698 if it can be accessed through SCOPE.
25699 LOCATION is the location of the usage point of TYPE_DECL.
25701 e.g. consider the following code snippet:
25708 template<class U> struct S
25710 C::myint mi; // <-- usage point of the typedef C::myint
25715 At S<char> instantiation time, we need to check the access of C::myint
25716 In other words, we need to check the access of the myint typedef through
25717 the C scope. For that purpose, this function will add the myint typedef
25718 and the scope C through which its being accessed to a list of typedefs
25719 tied to the template S. That list will be walked at template instantiation
25720 time and access check performed on each typedefs it contains.
25721 Note that this particular code snippet should yield an error because
25722 myint is private to C. */
25725 append_type_to_template_for_access_check (tree templ
,
25728 location_t location
)
25730 qualified_typedef_usage_t
*iter
;
25733 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
25735 /* Make sure we don't append the type to the template twice. */
25736 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
25737 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
25740 append_type_to_template_for_access_check_1 (templ
, type_decl
,
25744 /* Convert the generic type parameters in PARM that match the types given in the
25745 range [START_IDX, END_IDX) from the current_template_parms into generic type
25749 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
25751 tree current
= current_template_parms
;
25752 int depth
= TMPL_PARMS_DEPTH (current
);
25753 current
= INNERMOST_TEMPLATE_PARMS (current
);
25754 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
25756 for (int i
= 0; i
< start_idx
; ++i
)
25757 TREE_VEC_ELT (replacement
, i
)
25758 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25760 for (int i
= start_idx
; i
< end_idx
; ++i
)
25762 /* Create a distinct parameter pack type from the current parm and add it
25763 to the replacement args to tsubst below into the generic function
25766 tree o
= TREE_TYPE (TREE_VALUE
25767 (TREE_VEC_ELT (current
, i
)));
25768 tree t
= copy_type (o
);
25769 TEMPLATE_TYPE_PARM_INDEX (t
)
25770 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
25772 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
25773 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
25774 TYPE_MAIN_VARIANT (t
) = t
;
25775 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
25776 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
25777 TREE_VEC_ELT (replacement
, i
) = t
;
25778 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
25781 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
25782 TREE_VEC_ELT (replacement
, i
)
25783 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25785 /* If there are more levels then build up the replacement with the outer
25788 replacement
= add_to_template_args (template_parms_to_args
25789 (TREE_CHAIN (current_template_parms
)),
25792 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
25795 /* Entries in the decl_constraint hash table. */
25796 struct GTY((for_user
)) constr_entry
25802 /* Hashing function and equality for constraint entries. */
25803 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
25805 static hashval_t
hash (constr_entry
*e
)
25807 return (hashval_t
)DECL_UID (e
->decl
);
25810 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
25812 return e1
->decl
== e2
->decl
;
25816 /* A mapping from declarations to constraint information. Note that
25817 both templates and their underlying declarations are mapped to the
25818 same constraint information.
25820 FIXME: This is defined in pt.c because garbage collection
25821 code is not being generated for constraint.cc. */
25823 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
25825 /* Returns the template constraints of declaration T. If T is not
25826 constrained, return NULL_TREE. Note that T must be non-null. */
25829 get_constraints (tree t
)
25831 if (!flag_concepts
)
25834 gcc_assert (DECL_P (t
));
25835 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25836 t
= DECL_TEMPLATE_RESULT (t
);
25837 constr_entry elt
= { t
, NULL_TREE
};
25838 constr_entry
* found
= decl_constraints
->find (&elt
);
25845 /* Associate the given constraint information CI with the declaration
25846 T. If T is a template, then the constraints are associated with
25847 its underlying declaration. Don't build associations if CI is
25851 set_constraints (tree t
, tree ci
)
25855 gcc_assert (t
&& flag_concepts
);
25856 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25857 t
= DECL_TEMPLATE_RESULT (t
);
25858 gcc_assert (!get_constraints (t
));
25859 constr_entry elt
= {t
, ci
};
25860 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
25861 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
25866 /* Remove the associated constraints of the declaration T. */
25869 remove_constraints (tree t
)
25871 gcc_assert (DECL_P (t
));
25872 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25873 t
= DECL_TEMPLATE_RESULT (t
);
25875 constr_entry elt
= {t
, NULL_TREE
};
25876 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
25878 decl_constraints
->clear_slot (slot
);
25881 /* Memoized satisfaction results for declarations. This
25882 maps the pair (constraint_info, arguments) to the result computed
25883 by constraints_satisfied_p. */
25885 struct GTY((for_user
)) constraint_sat_entry
25892 /* Hashing function and equality for constraint entries. */
25894 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
25896 static hashval_t
hash (constraint_sat_entry
*e
)
25898 hashval_t val
= iterative_hash_object(e
->ci
, 0);
25899 return iterative_hash_template_arg (e
->args
, val
);
25902 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
25904 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
25908 /* Memoized satisfaction results for concept checks. */
25910 struct GTY((for_user
)) concept_spec_entry
25917 /* Hashing function and equality for constraint entries. */
25919 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
25921 static hashval_t
hash (concept_spec_entry
*e
)
25923 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
25926 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
25928 ++comparing_specializations
;
25929 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
25930 --comparing_specializations
;
25935 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
25936 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
25938 /* Search for a memoized satisfaction result. Returns one of the
25939 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25942 lookup_constraint_satisfaction (tree ci
, tree args
)
25944 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
25945 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
25947 return found
->result
;
25952 /* Memoize the result of a satisfication test. Returns the saved result. */
25955 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
25957 constraint_sat_entry elt
= {ci
, args
, result
};
25958 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
25959 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
25965 /* Search for a memoized satisfaction result for a concept. */
25968 lookup_concept_satisfaction (tree tmpl
, tree args
)
25970 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
25971 concept_spec_entry
* found
= concept_memos
->find (&elt
);
25973 return found
->result
;
25978 /* Memoize the result of a concept check. Returns the saved result. */
25981 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
25983 concept_spec_entry elt
= {tmpl
, args
, result
};
25984 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
25985 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
25991 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
25993 /* Returns a prior concept specialization. This returns the substituted
25994 and normalized constraints defined by the concept. */
25997 get_concept_expansion (tree tmpl
, tree args
)
25999 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
26000 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
26002 return found
->result
;
26007 /* Save a concept expansion for later. */
26010 save_concept_expansion (tree tmpl
, tree args
, tree def
)
26012 concept_spec_entry elt
= {tmpl
, args
, def
};
26013 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
26014 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
26021 hash_subsumption_args (tree t1
, tree t2
)
26023 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
26024 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
26026 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
26027 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
26028 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
26029 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
26033 /* Compare the constraints of two subsumption entries. The LEFT1 and
26034 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26035 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26038 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
26040 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
26041 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
26042 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
26043 CHECK_CONSTR_ARGS (right1
)))
26044 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
26045 CHECK_CONSTR_ARGS (right2
));
26049 /* Key/value pair for learning and memoizing subsumption results. This
26050 associates a pair of check constraints (including arguments) with
26051 a boolean value indicating the result. */
26053 struct GTY((for_user
)) subsumption_entry
26060 /* Hashing function and equality for constraint entries. */
26062 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
26064 static hashval_t
hash (subsumption_entry
*e
)
26066 return hash_subsumption_args (e
->t1
, e
->t2
);
26069 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
26071 ++comparing_specializations
;
26072 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
26073 --comparing_specializations
;
26078 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
26080 /* Search for a previously cached subsumption result. */
26083 lookup_subsumption_result (tree t1
, tree t2
)
26085 subsumption_entry elt
= { t1
, t2
, false };
26086 subsumption_entry
* found
= subsumption_table
->find (&elt
);
26088 return &found
->result
;
26093 /* Save a subsumption result. */
26096 save_subsumption_result (tree t1
, tree t2
, bool result
)
26098 subsumption_entry elt
= {t1
, t2
, result
};
26099 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
26100 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
26106 /* Set up the hash table for constraint association. */
26109 init_constraint_processing (void)
26111 if (!flag_concepts
)
26114 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
26115 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
26116 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26117 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
26118 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
26121 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26125 declare_integer_pack (void)
26127 tree ipfn
= push_library_fn (get_identifier ("__integer_pack"),
26128 build_function_type_list (integer_type_node
,
26131 NULL_TREE
, ECF_CONST
);
26132 DECL_DECLARED_CONSTEXPR_P (ipfn
) = true;
26133 DECL_BUILT_IN_CLASS (ipfn
) = BUILT_IN_FRONTEND
;
26136 /* Set up the hash tables for template instantiations. */
26139 init_template_processing (void)
26141 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26142 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
26144 if (cxx_dialect
>= cxx11
)
26145 declare_integer_pack ();
26148 /* Print stats about the template hash tables for -fstats. */
26151 print_template_statistics (void)
26153 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
26154 "%f collisions\n", (long) decl_specializations
->size (),
26155 (long) decl_specializations
->elements (),
26156 decl_specializations
->collisions ());
26157 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
26158 "%f collisions\n", (long) type_specializations
->size (),
26159 (long) type_specializations
->elements (),
26160 type_specializations
->collisions ());
26163 #include "gt-cp-pt.h"