1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 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
);
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
225 push_access_scope (tree t
)
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
228 || TREE_CODE (t
) == TYPE_DECL
);
230 if (DECL_FRIEND_CONTEXT (t
))
231 push_nested_class (DECL_FRIEND_CONTEXT (t
));
232 else if (DECL_CLASS_SCOPE_P (t
))
233 push_nested_class (DECL_CONTEXT (t
));
235 push_to_top_level ();
237 if (TREE_CODE (t
) == FUNCTION_DECL
)
239 saved_access_scope
= tree_cons
240 (NULL_TREE
, current_function_decl
, saved_access_scope
);
241 current_function_decl
= t
;
245 /* Restore the scope set up by push_access_scope. T is the node we
249 pop_access_scope (tree t
)
251 if (TREE_CODE (t
) == FUNCTION_DECL
)
253 current_function_decl
= TREE_VALUE (saved_access_scope
);
254 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
257 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
260 pop_from_top_level ();
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
269 finish_member_template_decl (tree decl
)
271 if (decl
== error_mark_node
)
272 return error_mark_node
;
274 gcc_assert (DECL_P (decl
));
276 if (TREE_CODE (decl
) == TYPE_DECL
)
280 type
= TREE_TYPE (decl
);
281 if (type
== error_mark_node
)
282 return error_mark_node
;
283 if (MAYBE_CLASS_TYPE_P (type
)
284 && CLASSTYPE_TEMPLATE_INFO (type
)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
287 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
288 check_member_template (tmpl
);
293 else if (TREE_CODE (decl
) == FIELD_DECL
)
294 error ("data member %qD cannot be a member template", decl
);
295 else if (DECL_TEMPLATE_INFO (decl
))
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
299 check_member_template (DECL_TI_TEMPLATE (decl
));
300 return DECL_TI_TEMPLATE (decl
);
306 error ("invalid member template declaration %qD", decl
);
308 return error_mark_node
;
311 /* Create a template info node. */
314 build_template_info (tree template_decl
, tree template_args
)
316 tree result
= make_node (TEMPLATE_INFO
);
317 TI_TEMPLATE (result
) = template_decl
;
318 TI_ARGS (result
) = template_args
;
322 /* Return the template info node corresponding to T, whatever T is. */
325 get_template_info (const_tree t
)
327 tree tinfo
= NULL_TREE
;
329 if (!t
|| t
== error_mark_node
)
332 if (TREE_CODE (t
) == NAMESPACE_DECL
333 || TREE_CODE (t
) == PARM_DECL
)
336 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
337 tinfo
= DECL_TEMPLATE_INFO (t
);
339 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
342 if (OVERLOAD_TYPE_P (t
))
343 tinfo
= TYPE_TEMPLATE_INFO (t
);
344 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
345 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
350 /* Returns the template nesting level of the indicated class TYPE.
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
369 template_class_depth (tree type
)
373 for (depth
= 0; type
&& TREE_CODE (type
) != NAMESPACE_DECL
; )
375 tree tinfo
= get_template_info (type
);
377 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
382 type
= CP_DECL_CONTEXT (type
);
383 else if (LAMBDA_TYPE_P (type
))
384 type
= LAMBDA_TYPE_EXTRA_SCOPE (type
);
386 type
= CP_TYPE_CONTEXT (type
);
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
396 inline_needs_template_parms (tree decl
, bool nsdmi
)
398 if (!decl
|| (!nsdmi
&& ! DECL_TEMPLATE_INFO (decl
)))
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
402 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
411 push_inline_template_parms_recursive (tree parmlist
, int levels
)
413 tree parms
= TREE_VALUE (parmlist
);
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
419 ++processing_template_decl
;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl
),
422 parms
, current_template_parms
);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
425 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
427 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
429 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
431 if (error_operand_p (parm
))
434 gcc_assert (DECL_P (parm
));
436 switch (TREE_CODE (parm
))
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm
)));
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
459 maybe_begin_member_template_processing (tree decl
)
463 bool nsdmi
= TREE_CODE (decl
) == FIELD_DECL
;
467 tree ctx
= DECL_CONTEXT (decl
);
468 decl
= (CLASSTYPE_TEMPLATE_INFO (ctx
)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx
)
471 ? CLASSTYPE_TI_TEMPLATE (ctx
) : NULL_TREE
);
474 if (inline_needs_template_parms (decl
, nsdmi
))
476 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
477 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
479 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
482 parms
= TREE_CHAIN (parms
);
485 push_inline_template_parms_recursive (parms
, levels
);
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels
.safe_push (levels
);
493 /* Undo the effects of maybe_begin_member_template_processing. */
496 maybe_end_member_template_processing (void)
501 if (inline_parm_levels
.length () == 0)
504 last
= inline_parm_levels
.pop ();
505 for (i
= 0; i
< last
; ++i
)
507 --processing_template_decl
;
508 current_template_parms
= TREE_CHAIN (current_template_parms
);
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
517 add_to_template_args (tree args
, tree extra_args
)
524 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
527 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
528 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
530 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
531 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
533 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
534 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
547 add_outermost_template_args (tree args
, tree extra_args
)
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
557 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
563 new_args
= add_to_template_args (args
, extra_args
);
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
571 /* Return the N levels of innermost template arguments from the ARGS. */
574 get_innermost_template_args (tree args
, int n
)
582 /* If N is 1, just return the innermost set of template arguments. */
584 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
586 /* If we're not removing anything, just return the arguments we were
588 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
589 gcc_assert (extra_levels
>= 0);
590 if (extra_levels
== 0)
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args
= make_tree_vec (n
);
595 for (i
= 1; i
<= n
; ++i
)
596 SET_TMPL_ARGS_LEVEL (new_args
, i
,
597 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
606 strip_innermost_template_args (tree args
, int extra_levels
)
609 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
614 /* If N is 1, just return the outermost set of template arguments. */
616 return TMPL_ARGS_LEVEL (args
, 1);
618 /* If we're not removing anything, just return the arguments we were
620 gcc_assert (extra_levels
>= 0);
621 if (extra_levels
== 0)
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args
= make_tree_vec (n
);
626 for (i
= 1; i
<= n
; ++i
)
627 SET_TMPL_ARGS_LEVEL (new_args
, i
,
628 TMPL_ARGS_LEVEL (args
, i
));
633 /* We've got a template header coming up; push to a new level for storing
637 begin_template_parm_list (void)
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms
, NULL
);
654 ++processing_template_decl
;
655 ++processing_template_parmlist
;
656 note_template_header (0);
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl
),
662 current_template_parms
);
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
670 check_specialization_scope (void)
672 tree scope
= current_scope ();
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
683 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
685 error ("explicit specialization in non-namespace scope %qD", scope
);
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms
)
699 error ("enclosing class templates are not explicitly specialized");
706 /* We've just seen template <>. */
709 begin_specialization (void)
711 begin_scope (sk_template_spec
, NULL
);
712 note_template_header (1);
713 return check_specialization_scope ();
716 /* Called at then end of processing a declaration preceded by
720 end_specialization (void)
723 reset_specialization ();
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
730 reset_specialization (void)
732 processing_specialization
= 0;
733 template_header_count
= 0;
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
740 note_template_header (int specialization
)
742 processing_specialization
= specialization
;
743 template_header_count
++;
746 /* We're beginning an explicit instantiation. */
749 begin_explicit_instantiation (void)
751 gcc_assert (!processing_explicit_instantiation
);
752 processing_explicit_instantiation
= true;
757 end_explicit_instantiation (void)
759 gcc_assert (processing_explicit_instantiation
);
760 processing_explicit_instantiation
= false;
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
769 check_specialization_namespace (tree tmpl
)
771 tree tpl_ns
= decl_namespace_context (tmpl
);
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
780 if (current_scope() != DECL_CONTEXT (tmpl
)
781 && !at_namespace_scope_p ())
783 error ("specialization of %qD must appear at namespace scope", tmpl
);
787 if (cxx_dialect
< cxx11
788 ? is_associated_namespace (current_namespace
, tpl_ns
)
789 : is_ancestor (current_namespace
, tpl_ns
))
790 /* Same or enclosing namespace. */
794 permerror (input_location
,
795 "specialization of %qD in different namespace", tmpl
);
796 inform (DECL_SOURCE_LOCATION (tmpl
),
797 " from definition of %q#D", tmpl
);
802 /* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
806 check_explicit_instantiation_namespace (tree spec
)
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns
= decl_namespace_context (spec
);
813 if (!is_ancestor (current_namespace
, ns
))
814 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec
, current_namespace
, ns
);
819 // Returns the type of a template specialization only if that
820 // specialization needs to be defined. Otherwise (e.g., if the type has
821 // already been defined), the function returns NULL_TREE.
823 maybe_new_partial_specialization (tree type
)
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
828 // template<typename T>
831 // template<typename T>
834 // Here, S<T*> is an implicit instantiation of S whose type
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
) && !COMPLETE_TYPE_P (type
))
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
842 // template<typename T>
843 // requires Integral<T>
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
857 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
858 tree args
= CLASSTYPE_TI_ARGS (type
);
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms
)
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type
)))
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr
= current_template_constraints ();
873 if (type
== TREE_TYPE (tmpl
))
875 tree main_constr
= get_constraints (tmpl
);
876 if (equivalent_constraints (type_constr
, main_constr
))
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
885 tree spec_tmpl
= TREE_VALUE (specs
);
886 tree spec_args
= TREE_PURPOSE (specs
);
887 tree spec_constr
= get_constraints (spec_tmpl
);
888 if (comp_template_args (args
, spec_args
)
889 && equivalent_constraints (type_constr
, spec_constr
))
891 specs
= TREE_CHAIN (specs
);
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t
= make_class_type (TREE_CODE (type
));
897 CLASSTYPE_DECLARED_CLASS (t
) = CLASSTYPE_DECLARED_CLASS (type
);
898 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (tmpl
, args
));
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t
) = TYPE_CANONICAL (type
);
905 // Build the corresponding type decl.
906 tree d
= create_implicit_typedef (DECL_NAME (tmpl
), t
);
907 DECL_CONTEXT (d
) = TYPE_CONTEXT (t
);
908 DECL_SOURCE_LOCATION (d
) = input_location
;
916 /* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
920 maybe_process_partial_specialization (tree type
)
924 if (type
== error_mark_node
)
925 return error_mark_node
;
927 /* A lambda that appears in specialization context is not itself a
929 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
932 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
934 error ("name of class shadows template template parameter %qD",
936 return error_mark_node
;
939 context
= TYPE_CONTEXT (type
);
941 if (TYPE_ALIAS_P (type
))
943 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (type
);
945 if (tinfo
&& DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo
)))
946 error ("specialization of alias template %qD",
947 TI_TEMPLATE (tinfo
));
949 error ("explicit specialization of non-template %qT", type
);
950 return error_mark_node
;
952 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
954 /* This is for ordinary explicit specialization and partial
955 specialization of a template class such as:
957 template <> class C<int>;
961 template <class T> class C<T*>;
963 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
965 if (tree t
= maybe_new_partial_specialization (type
))
967 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t
))
968 && !at_namespace_scope_p ())
969 return error_mark_node
;
970 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t
);
971 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t
)) = input_location
;
972 if (processing_template_decl
)
974 tree decl
= push_template_decl (TYPE_MAIN_DECL (t
));
975 if (decl
== error_mark_node
)
976 return error_mark_node
;
977 return TREE_TYPE (decl
);
980 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
981 error ("specialization of %qT after instantiation", type
);
982 else if (errorcount
&& !processing_specialization
983 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
984 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
985 /* Trying to define a specialization either without a template<> header
986 or in an inappropriate place. We've already given an error, so just
987 bail now so we don't actually define the specialization. */
988 return error_mark_node
;
990 else if (CLASS_TYPE_P (type
)
991 && !CLASSTYPE_USE_TEMPLATE (type
)
992 && CLASSTYPE_TEMPLATE_INFO (type
)
993 && context
&& CLASS_TYPE_P (context
)
994 && CLASSTYPE_TEMPLATE_INFO (context
))
996 /* This is for an explicit specialization of member class
997 template according to [temp.expl.spec/18]:
999 template <> template <class U> class C<int>::D;
1001 The context `C<int>' must be an implicit instantiation.
1002 Otherwise this is just a member class template declared
1005 template <> class C<int> { template <class U> class D; };
1006 template <> template <class U> class C<int>::D;
1008 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1009 while in the second case, `C<int>::D' is a primary template
1010 and `C<T>::D' may not exist. */
1012 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
1013 && !COMPLETE_TYPE_P (type
))
1016 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1018 if (current_namespace
1019 != decl_namespace_context (tmpl
))
1021 permerror (input_location
,
1022 "specializing %q#T in different namespace", type
);
1023 permerror (DECL_SOURCE_LOCATION (tmpl
),
1024 " from definition of %q#D", tmpl
);
1027 /* Check for invalid specialization after instantiation:
1029 template <> template <> class C<int>::D<int>;
1030 template <> template <class U> class C<int>::D; */
1032 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1033 t
; t
= TREE_CHAIN (t
))
1035 tree inst
= TREE_VALUE (t
);
1036 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
)
1037 || !COMPLETE_OR_OPEN_TYPE_P (inst
))
1039 /* We already have a full specialization of this partial
1040 instantiation, or a full specialization has been
1041 looked up but not instantiated. Reassign it to the
1042 new member specialization template. */
1046 elt
.tmpl
= most_general_template (tmpl
);
1047 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
1050 type_specializations
->remove_elt (&elt
);
1053 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
1056 = type_specializations
->find_slot (&elt
, INSERT
);
1057 entry
= ggc_alloc
<spec_entry
> ();
1062 /* But if we've had an implicit instantiation, that's a
1063 problem ([temp.expl.spec]/6). */
1064 error ("specialization %qT after instantiation %qT",
1068 /* Mark TYPE as a specialization. And as a result, we only
1069 have one level of template argument for the innermost
1071 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
1072 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
1073 CLASSTYPE_TI_ARGS (type
)
1074 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1077 else if (processing_specialization
)
1079 /* Someday C++0x may allow for enum template specialization. */
1080 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
1081 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
1082 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
1083 "of %qD not allowed by ISO C++", type
);
1086 error ("explicit specialization of non-template %qT", type
);
1087 return error_mark_node
;
1094 /* Returns nonzero if we can optimize the retrieval of specializations
1095 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1096 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1099 optimize_specialization_lookup_p (tree tmpl
)
1101 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
1102 && DECL_CLASS_SCOPE_P (tmpl
)
1103 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1105 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
1106 /* The optimized lookup depends on the fact that the
1107 template arguments for the member function template apply
1108 purely to the containing class, which is not true if the
1109 containing class is an explicit or partial
1111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
1112 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
1113 && !DECL_CONV_FN_P (tmpl
)
1114 /* It is possible to have a template that is not a member
1115 template and is not a member of a template class:
1117 template <typename T>
1118 struct S { friend A::f(); };
1120 Here, the friend function is a template, but the context does
1121 not have template information. The optimized lookup relies
1122 on having ARGS be the template arguments for both the class
1123 and the function template. */
1124 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
1127 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1128 gone through coerce_template_parms by now. */
1131 verify_unstripped_args (tree args
)
1133 ++processing_template_decl
;
1134 if (!any_dependent_template_arguments_p (args
))
1136 tree inner
= INNERMOST_TEMPLATE_ARGS (args
);
1137 for (int i
= 0; i
< TREE_VEC_LENGTH (inner
); ++i
)
1139 tree arg
= TREE_VEC_ELT (inner
, i
);
1140 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1142 else if (TYPE_P (arg
))
1143 gcc_assert (strip_typedefs (arg
, NULL
) == arg
);
1144 else if (strip_typedefs (TREE_TYPE (arg
), NULL
) != TREE_TYPE (arg
))
1145 /* Allow typedefs on the type of a non-type argument, since a
1146 parameter can have them. */;
1148 gcc_assert (strip_typedefs_expr (arg
, NULL
) == arg
);
1151 --processing_template_decl
;
1154 /* Retrieve the specialization (in the sense of [temp.spec] - a
1155 specialization is either an instantiation or an explicit
1156 specialization) of TMPL for the given template ARGS. If there is
1157 no such specialization, return NULL_TREE. The ARGS are a vector of
1158 arguments, or a vector of vectors of arguments, in the case of
1159 templates with more than one level of parameters.
1161 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1162 then we search for a partial specialization matching ARGS. This
1163 parameter is ignored if TMPL is not a class template.
1165 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1166 result is a NONTYPE_ARGUMENT_PACK. */
1169 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1171 if (tmpl
== NULL_TREE
)
1174 if (args
== error_mark_node
)
1177 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1178 || TREE_CODE (tmpl
) == FIELD_DECL
);
1180 /* There should be as many levels of arguments as there are
1181 levels of parameters. */
1182 gcc_assert (TMPL_ARGS_DEPTH (args
)
1183 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1184 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1185 : template_class_depth (DECL_CONTEXT (tmpl
))));
1188 verify_unstripped_args (args
);
1190 if (optimize_specialization_lookup_p (tmpl
))
1192 tree class_template
;
1193 tree class_specialization
;
1194 vec
<tree
, va_gc
> *methods
;
1198 /* The template arguments actually apply to the containing
1199 class. Find the class specialization with those
1201 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1202 class_specialization
1203 = retrieve_specialization (class_template
, args
, 0);
1204 if (!class_specialization
)
1206 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1207 for the specialization. */
1208 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1211 /* Iterate through the methods with the indicated name, looking
1212 for the one that has an instance of TMPL. */
1213 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1214 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1216 tree fn
= OVL_CURRENT (fns
);
1217 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn
) == class_specialization
)
1229 hash_table
<spec_hasher
> *specializations
;
1233 elt
.spec
= NULL_TREE
;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1236 specializations
= type_specializations
;
1238 specializations
= decl_specializations
;
1241 hash
= spec_hasher::hash (&elt
);
1242 found
= specializations
->find_with_hash (&elt
, hash
);
1250 /* Like retrieve_specialization, but for local declarations. */
1253 retrieve_local_specialization (tree tmpl
)
1255 if (local_specializations
== NULL
)
1258 tree
*slot
= local_specializations
->get (tmpl
);
1259 return slot
? *slot
: NULL_TREE
;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl
, tree tmpl
)
1269 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1273 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1279 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1281 for (t
= TREE_TYPE (decl
);
1283 t
= CLASSTYPE_USE_TEMPLATE (t
)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1285 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1296 is_specialization_of_friend (tree decl
, tree friend_decl
)
1298 bool need_template
= true;
1301 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1302 || TREE_CODE (decl
) == TYPE_DECL
);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1307 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl
)
1309 && !DECL_USE_TEMPLATE (friend_decl
))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1313 need_template
= false;
1315 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl
))
1317 need_template
= false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1323 if (is_specialization_of (decl
, friend_decl
))
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1348 && DECL_CLASS_SCOPE_P (decl
)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context
= DECL_CONTEXT (decl
);
1369 tree args
= NULL_TREE
;
1370 int current_depth
= 0;
1372 while (current_depth
< template_depth
)
1374 if (CLASSTYPE_TEMPLATE_INFO (context
))
1376 if (current_depth
== 0)
1377 args
= TYPE_TI_ARGS (context
);
1379 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1382 context
= TYPE_CONTEXT (context
);
1385 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1390 tree friend_args_type
;
1391 tree decl_args_type
;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1395 is_template
= DECL_TEMPLATE_INFO (decl
)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1397 if (need_template
^ is_template
)
1399 else if (is_template
)
1401 /* If both are templates, check template parameter list. */
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1410 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1413 decl_type
= TREE_TYPE (decl
);
1415 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1416 tf_none
, NULL_TREE
);
1417 if (friend_type
== error_mark_node
)
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1427 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1429 friend_args_type
= TREE_CHAIN (friend_args_type
);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1431 decl_args_type
= TREE_CHAIN (decl_args_type
);
1433 return compparms (decl_args_type
, friend_args_type
);
1437 /* DECL is a TYPE_DECL */
1439 tree decl_type
= TREE_TYPE (decl
);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1447 if (need_template
^ is_template
)
1449 else if (is_template
)
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1455 != DECL_NAME (friend_decl
))
1458 /* Now check template parameter list. */
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1467 return (DECL_NAME (decl
)
1468 == DECL_NAME (friend_decl
));
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1483 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1487 spec_entry
**slot
= NULL
;
1490 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1491 || (TREE_CODE (tmpl
) == FIELD_DECL
1492 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1494 if (TREE_CODE (spec
) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec
)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1509 if (optimize_specialization_lookup_p (tmpl
))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn
= retrieve_specialization (tmpl
, args
, 0);
1520 hash
= spec_hasher::hash (&elt
);
1523 decl_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
1525 fn
= ((spec_entry
*) *slot
)->spec
;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1537 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1541 if (DECL_ODR_USED (fn
)
1542 || DECL_EXPLICIT_INSTANTIATION (fn
))
1544 error ("specialization of %qD after instantiation",
1546 return error_mark_node
;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn
) = NULL_TREE
;
1575 duplicate_decls (spec
, fn
, is_friend
);
1576 /* The call to duplicate_decls will have applied
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone
, fn
)
1588 DECL_DECLARED_INLINE_P (clone
)
1589 = DECL_DECLARED_INLINE_P (fn
);
1590 DECL_SOURCE_LOCATION (clone
)
1591 = DECL_SOURCE_LOCATION (fn
);
1592 DECL_DELETED_FN (clone
)
1593 = DECL_DELETED_FN (fn
);
1595 check_specialization_namespace (tmpl
);
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1602 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1603 /* Dup decl failed, but this is a new definition. Set the
1604 line number so any errors match this new
1606 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1612 return duplicate_decls (spec
, fn
, is_friend
);
1614 /* A specialization must be declared in the same namespace as the
1615 template it is specializing. */
1616 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1617 && !check_specialization_namespace (tmpl
))
1618 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1620 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1622 spec_entry
*entry
= ggc_alloc
<spec_entry
> ();
1623 gcc_assert (tmpl
&& args
&& spec
);
1626 if ((TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1627 && PRIMARY_TEMPLATE_P (tmpl
)
1628 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1629 || variable_template_p (tmpl
))
1630 /* If TMPL is a forward declaration of a template function, keep a list
1631 of all specializations in case we need to reassign them to a friend
1632 template later in tsubst_friend_function.
1634 Also keep a list of all variable template instantiations so that
1635 process_partial_specialization can check whether a later partial
1636 specialization would have used it. */
1637 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1638 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1644 /* Returns true iff two spec_entry nodes are equivalent. */
1646 int comparing_specializations
;
1649 spec_hasher::equal (spec_entry
*e1
, spec_entry
*e2
)
1653 ++comparing_specializations
;
1654 equal
= (e1
->tmpl
== e2
->tmpl
1655 && comp_template_args (e1
->args
, e2
->args
));
1656 if (equal
&& flag_concepts
1657 /* tmpl could be a FIELD_DECL for a capture pack. */
1658 && TREE_CODE (e1
->tmpl
) == TEMPLATE_DECL
1659 && VAR_P (DECL_TEMPLATE_RESULT (e1
->tmpl
))
1660 && uses_template_parms (e1
->args
))
1662 /* Partial specializations of a variable template can be distinguished by
1664 tree c1
= e1
->spec
? get_constraints (e1
->spec
) : NULL_TREE
;
1665 tree c2
= e2
->spec
? get_constraints (e2
->spec
) : NULL_TREE
;
1666 equal
= equivalent_constraints (c1
, c2
);
1668 --comparing_specializations
;
1673 /* Returns a hash for a template TMPL and template arguments ARGS. */
1676 hash_tmpl_and_args (tree tmpl
, tree args
)
1678 hashval_t val
= iterative_hash_object (DECL_UID (tmpl
), 0);
1679 return iterative_hash_template_arg (args
, val
);
1682 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1686 spec_hasher::hash (spec_entry
*e
)
1688 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1691 /* Recursively calculate a hash value for a template argument ARG, for use
1692 in the hash tables of template specializations. */
1695 iterative_hash_template_arg (tree arg
, hashval_t val
)
1697 unsigned HOST_WIDE_INT i
;
1698 enum tree_code code
;
1701 if (arg
== NULL_TREE
)
1702 return iterative_hash_object (arg
, val
);
1707 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1710 code
= TREE_CODE (arg
);
1711 tclass
= TREE_CODE_CLASS (code
);
1713 val
= iterative_hash_object (code
, val
);
1720 case IDENTIFIER_NODE
:
1721 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1725 int i
, len
= TREE_VEC_LENGTH (arg
);
1726 for (i
= 0; i
< len
; ++i
)
1727 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1731 case TYPE_PACK_EXPANSION
:
1732 case EXPR_PACK_EXPANSION
:
1733 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1734 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1736 case TYPE_ARGUMENT_PACK
:
1737 case NONTYPE_ARGUMENT_PACK
:
1738 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1741 for (; arg
; arg
= TREE_CHAIN (arg
))
1742 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1746 for (; arg
; arg
= OVL_NEXT (arg
))
1747 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1753 iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1754 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1756 val
= iterative_hash_template_arg (field
, val
);
1757 val
= iterative_hash_template_arg (value
, val
);
1763 if (!DECL_ARTIFICIAL (arg
))
1765 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1766 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1768 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1771 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1774 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1775 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1777 case TEMPLATE_PARM_INDEX
:
1778 val
= iterative_hash_template_arg
1779 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1780 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1781 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1784 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1785 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1786 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1789 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1791 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1795 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1796 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1797 val
= iterative_hash_object (code
, val
);
1798 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1801 /* A lambda can't appear in a template arg, but don't crash on
1803 gcc_assert (seen_error ());
1807 case IMPLICIT_CONV_EXPR
:
1808 case STATIC_CAST_EXPR
:
1809 case REINTERPRET_CAST_EXPR
:
1810 case CONST_CAST_EXPR
:
1811 case DYNAMIC_CAST_EXPR
:
1813 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1814 /* Now hash operands as usual. */
1824 if (alias_template_specialization_p (arg
))
1826 // We want an alias specialization that survived strip_typedefs
1827 // to hash differently from its TYPE_CANONICAL, to avoid hash
1828 // collisions that compare as different in template_args_equal.
1829 // These could be dependent specializations that strip_typedefs
1830 // left alone, or untouched specializations because
1831 // coerce_template_parms returns the unconverted template
1832 // arguments if it sees incomplete argument packs.
1833 tree ti
= TYPE_ALIAS_TEMPLATE_INFO (arg
);
1834 return hash_tmpl_and_args (TI_TEMPLATE (ti
), TI_ARGS (ti
));
1836 if (TYPE_CANONICAL (arg
))
1837 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1839 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1840 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1841 /* Otherwise just compare the types during lookup. */
1844 case tcc_declaration
:
1846 return iterative_hash_expr (arg
, val
);
1849 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1851 unsigned n
= cp_tree_operand_length (arg
);
1852 for (i
= 0; i
< n
; ++i
)
1853 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1861 /* Unregister the specialization SPEC as a specialization of TMPL.
1862 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1863 if the SPEC was listed as a specialization of TMPL.
1865 Note that SPEC has been ggc_freed, so we can't look inside it. */
1868 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1873 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1874 elt
.args
= TI_ARGS (tinfo
);
1875 elt
.spec
= NULL_TREE
;
1877 entry
= decl_specializations
->find (&elt
);
1880 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1881 gcc_assert (new_spec
!= NULL_TREE
);
1882 entry
->spec
= new_spec
;
1889 /* Like register_specialization, but for local declarations. We are
1890 registering SPEC, an instantiation of TMPL. */
1893 register_local_specialization (tree spec
, tree tmpl
)
1895 local_specializations
->put (tmpl
, spec
);
1898 /* TYPE is a class type. Returns true if TYPE is an explicitly
1899 specialized class. */
1902 explicit_class_specialization_p (tree type
)
1904 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1906 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1909 /* Print the list of functions at FNS, going through all the overloads
1910 for each element of the list. Alternatively, FNS can not be a
1911 TREE_LIST, in which case it will be printed together with all the
1914 MORE and *STR should respectively be FALSE and NULL when the function
1915 is called from the outside. They are used internally on recursive
1916 calls. print_candidates manages the two parameters and leaves NULL
1917 in *STR when it ends. */
1920 print_candidates_1 (tree fns
, bool more
, const char **str
)
1923 char *spaces
= NULL
;
1925 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1926 if (TREE_CODE (fn
) == TREE_LIST
)
1928 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1929 print_candidates_1 (TREE_VALUE (fn2
),
1930 TREE_CHAIN (fn2
) || more
, str
);
1934 tree cand
= OVL_CURRENT (fn
);
1937 /* Pick the prefix string. */
1938 if (!more
&& !OVL_NEXT (fns
))
1940 inform (DECL_SOURCE_LOCATION (cand
),
1941 "candidate is: %#D", cand
);
1945 *str
= _("candidates are:");
1946 spaces
= get_spaces (*str
);
1948 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1949 *str
= spaces
? spaces
: *str
;
1959 /* Print the list of candidate FNS in an error message. FNS can also
1960 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1963 print_candidates (tree fns
)
1965 const char *str
= NULL
;
1966 print_candidates_1 (fns
, false, &str
);
1967 gcc_assert (str
== NULL
);
1970 /* Get a (possibly) constrained template declaration for the
1971 purpose of ordering candidates. */
1973 get_template_for_ordering (tree list
)
1975 gcc_assert (TREE_CODE (list
) == TREE_LIST
);
1976 tree f
= TREE_VALUE (list
);
1977 if (tree ti
= DECL_TEMPLATE_INFO (f
))
1978 return TI_TEMPLATE (ti
);
1982 /* Among candidates having the same signature, return the
1983 most constrained or NULL_TREE if there is no best candidate.
1984 If the signatures of candidates vary (e.g., template
1985 specialization vs. member function), then there can be no
1988 Note that we don't compare constraints on the functions
1989 themselves, but rather those of their templates. */
1991 most_constrained_function (tree candidates
)
1993 // Try to find the best candidate in a first pass.
1994 tree champ
= candidates
;
1995 for (tree c
= TREE_CHAIN (champ
); c
; c
= TREE_CHAIN (c
))
1997 int winner
= more_constrained (get_template_for_ordering (champ
),
1998 get_template_for_ordering (c
));
2000 champ
= c
; // The candidate is more constrained
2001 else if (winner
== 0)
2002 return NULL_TREE
; // Neither is more constrained
2005 // Verify that the champ is better than previous candidates.
2006 for (tree c
= candidates
; c
!= champ
; c
= TREE_CHAIN (c
)) {
2007 if (!more_constrained (get_template_for_ordering (champ
),
2008 get_template_for_ordering (c
)))
2016 /* Returns the template (one of the functions given by TEMPLATE_ID)
2017 which can be specialized to match the indicated DECL with the
2018 explicit template args given in TEMPLATE_ID. The DECL may be
2019 NULL_TREE if none is available. In that case, the functions in
2020 TEMPLATE_ID are non-members.
2022 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2023 specialization of a member template.
2025 The TEMPLATE_COUNT is the number of references to qualifying
2026 template classes that appeared in the name of the function. See
2027 check_explicit_specialization for a more accurate description.
2029 TSK indicates what kind of template declaration (if any) is being
2030 declared. TSK_TEMPLATE indicates that the declaration given by
2031 DECL, though a FUNCTION_DECL, has template parameters, and is
2032 therefore a template function.
2034 The template args (those explicitly specified and those deduced)
2035 are output in a newly created vector *TARGS_OUT.
2037 If it is impossible to determine the result, an error message is
2038 issued. The error_mark_node is returned to indicate failure. */
2041 determine_specialization (tree template_id
,
2044 int need_member_template
,
2050 tree explicit_targs
;
2051 tree candidates
= NULL_TREE
;
2053 /* A TREE_LIST of templates of which DECL may be a specialization.
2054 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2055 corresponding TREE_PURPOSE is the set of template arguments that,
2056 when used to instantiate the template, would produce a function
2057 with the signature of DECL. */
2058 tree templates
= NULL_TREE
;
2060 cp_binding_level
*b
;
2062 *targs_out
= NULL_TREE
;
2064 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
2065 return error_mark_node
;
2067 /* We shouldn't be specializing a member template of an
2068 unspecialized class template; we already gave an error in
2069 check_specialization_scope, now avoid crashing. */
2070 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
2071 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
2073 gcc_assert (errorcount
);
2074 return error_mark_node
;
2077 fns
= TREE_OPERAND (template_id
, 0);
2078 explicit_targs
= TREE_OPERAND (template_id
, 1);
2080 if (fns
== error_mark_node
)
2081 return error_mark_node
;
2083 /* Check for baselinks. */
2084 if (BASELINK_P (fns
))
2085 fns
= BASELINK_FUNCTIONS (fns
);
2087 if (TREE_CODE (decl
) == FUNCTION_DECL
&& !is_overloaded_fn (fns
))
2089 error ("%qD is not a function template", fns
);
2090 return error_mark_node
;
2092 else if (VAR_P (decl
) && !variable_template_p (fns
))
2094 error ("%qD is not a variable template", fns
);
2095 return error_mark_node
;
2098 /* Count the number of template headers specified for this
2101 for (b
= current_binding_level
;
2102 b
->kind
== sk_template_parms
;
2106 tree orig_fns
= fns
;
2108 if (variable_template_p (fns
))
2110 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns
));
2111 targs
= coerce_template_parms (parms
, explicit_targs
, fns
,
2112 tf_warning_or_error
,
2113 /*req_all*/true, /*use_defarg*/true);
2114 if (targs
!= error_mark_node
)
2115 templates
= tree_cons (targs
, fns
, templates
);
2117 else for (; fns
; fns
= OVL_NEXT (fns
))
2119 tree fn
= OVL_CURRENT (fns
);
2121 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2123 tree decl_arg_types
;
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2153 if (header_count
&& header_count
!= template_count
+ 1)
2156 /* Check that the number of template arguments at the
2157 innermost level for DECL is the same as for FN. */
2158 if (current_binding_level
->kind
== sk_template_parms
2159 && !current_binding_level
->explicit_spec_p
2160 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
2161 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2162 (current_template_parms
))))
2165 /* DECL might be a specialization of FN. */
2166 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2167 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2169 /* For a non-static member function, we need to make sure
2170 that the const qualification is the same. Since
2171 get_bindings does not try to merge the "this" parameter,
2172 we must do the comparison explicitly. */
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
2174 && !same_type_p (TREE_VALUE (fn_arg_types
),
2175 TREE_VALUE (decl_arg_types
)))
2178 /* Skip the "this" parameter and, for constructors of
2179 classes with virtual bases, the VTT parameter. A
2180 full specialization of a constructor will have a VTT
2181 parameter, but a template never will. */
2183 = skip_artificial_parms_for (decl
, decl_arg_types
);
2185 = skip_artificial_parms_for (fn
, fn_arg_types
);
2187 /* Function templates cannot be specializations; there are
2188 no partial specializations of functions. Therefore, if
2189 the type of DECL does not match FN, there is no
2192 Note that it should never be the case that we have both
2193 candidates added here, and for regular member functions
2195 if (tsk
== tsk_template
)
2197 if (compparms (fn_arg_types
, decl_arg_types
))
2198 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2202 /* See whether this function might be a specialization of this
2203 template. Suppress access control because we might be trying
2204 to make this specialization a friend, and we have already done
2205 access control for the declaration of the specialization. */
2206 push_deferring_access_checks (dk_no_check
);
2207 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
2208 pop_deferring_access_checks ();
2211 /* We cannot deduce template arguments that when used to
2212 specialize TMPL will produce DECL. */
2215 /* Remove, from the set of candidates, all those functions
2216 whose constraints are not satisfied. */
2217 if (flag_concepts
&& !constraints_satisfied_p (fn
, targs
))
2220 // Then, try to form the new function type.
2221 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_fndecl_type
, NULL_TREE
);
2222 if (insttype
== error_mark_node
)
2225 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
2226 if (!compparms (fn_arg_types
, decl_arg_types
))
2229 /* Save this template, and the arguments deduced. */
2230 templates
= tree_cons (targs
, fn
, templates
);
2232 else if (need_member_template
)
2233 /* FN is an ordinary member function, and we need a
2234 specialization of a member template. */
2236 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
2237 /* We can get IDENTIFIER_NODEs here in certain erroneous
2240 else if (!DECL_FUNCTION_MEMBER_P (fn
))
2241 /* This is just an ordinary non-member function. Nothing can
2242 be a specialization of that. */
2244 else if (DECL_ARTIFICIAL (fn
))
2245 /* Cannot specialize functions that are created implicitly. */
2249 tree decl_arg_types
;
2251 /* This is an ordinary member function. However, since
2252 we're here, we can assume its enclosing class is a
2253 template class. For example,
2255 template <typename T> struct S { void f(); };
2256 template <> void S<int>::f() {}
2258 Here, S<int>::f is a non-template, but S<int> is a
2259 template class. If FN has the same type as DECL, we
2260 might be in business. */
2262 if (!DECL_TEMPLATE_INFO (fn
))
2263 /* Its enclosing class is an explicit specialization
2264 of a template class. This is not a candidate. */
2267 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2268 TREE_TYPE (TREE_TYPE (fn
))))
2269 /* The return types differ. */
2272 /* Adjust the type of DECL in case FN is a static member. */
2273 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2274 if (DECL_STATIC_FUNCTION_P (fn
)
2275 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2276 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2278 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2282 // If the deduced arguments do not satisfy the constraints,
2283 // this is not a candidate.
2284 if (flag_concepts
&& !constraints_satisfied_p (fn
))
2287 // Add the candidate.
2288 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2292 if (templates
&& TREE_CHAIN (templates
))
2298 It is possible for a specialization with a given function
2299 signature to be instantiated from more than one function
2300 template. In such cases, explicit specification of the
2301 template arguments must be used to uniquely identify the
2302 function template specialization being specialized.
2304 Note that here, there's no suggestion that we're supposed to
2305 determine which of the candidate templates is most
2306 specialized. However, we, also have:
2310 Partial ordering of overloaded function template
2311 declarations is used in the following contexts to select
2312 the function template to which a function template
2313 specialization refers:
2315 -- when an explicit specialization refers to a function
2318 So, we do use the partial ordering rules, at least for now.
2319 This extension can only serve to make invalid programs valid,
2320 so it's safe. And, there is strong anecdotal evidence that
2321 the committee intended the partial ordering rules to apply;
2322 the EDG front end has that behavior, and John Spicer claims
2323 that the committee simply forgot to delete the wording in
2324 [temp.expl.spec]. */
2325 tree tmpl
= most_specialized_instantiation (templates
);
2326 if (tmpl
!= error_mark_node
)
2329 TREE_CHAIN (templates
) = NULL_TREE
;
2333 // Concepts allows multiple declarations of member functions
2334 // with the same signature. Like above, we need to rely on
2335 // on the partial ordering of those candidates to determine which
2337 if (flag_concepts
&& candidates
&& TREE_CHAIN (candidates
))
2339 if (tree cand
= most_constrained_function (candidates
))
2342 TREE_CHAIN (cand
) = NULL_TREE
;
2346 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2348 error ("template-id %qD for %q+D does not match any template "
2349 "declaration", template_id
, decl
);
2350 if (header_count
&& header_count
!= template_count
+ 1)
2351 inform (input_location
, "saw %d %<template<>%>, need %d for "
2352 "specializing a member function template",
2353 header_count
, template_count
+ 1);
2355 print_candidates (orig_fns
);
2356 return error_mark_node
;
2358 else if ((templates
&& TREE_CHAIN (templates
))
2359 || (candidates
&& TREE_CHAIN (candidates
))
2360 || (templates
&& candidates
))
2362 error ("ambiguous template specialization %qD for %q+D",
2364 candidates
= chainon (candidates
, templates
);
2365 print_candidates (candidates
);
2366 return error_mark_node
;
2369 /* We have one, and exactly one, match. */
2372 tree fn
= TREE_VALUE (candidates
);
2373 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2375 // Propagate the candidate's constraints to the declaration.
2376 set_constraints (decl
, get_constraints (fn
));
2378 /* DECL is a re-declaration or partial instantiation of a template
2380 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2382 /* It was a specialization of an ordinary member function in a
2384 return DECL_TI_TEMPLATE (fn
);
2387 /* It was a specialization of a template. */
2388 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2391 *targs_out
= copy_node (targs
);
2392 SET_TMPL_ARGS_LEVEL (*targs_out
,
2393 TMPL_ARGS_DEPTH (*targs_out
),
2394 TREE_PURPOSE (templates
));
2397 *targs_out
= TREE_PURPOSE (templates
);
2398 return TREE_VALUE (templates
);
2401 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2402 but with the default argument values filled in from those in the
2406 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2409 tree new_spec_types
;
2414 if (spec_types
== void_list_node
)
2415 return void_list_node
;
2417 /* Substitute into the rest of the list. */
2419 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2420 TREE_CHAIN (tmpl_types
));
2422 /* Add the default argument for this parameter. */
2423 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2424 TREE_VALUE (spec_types
),
2428 /* DECL is an explicit specialization. Replicate default arguments
2429 from the template it specializes. (That way, code like:
2431 template <class T> void f(T = 3);
2432 template <> void f(double);
2435 works, as required.) An alternative approach would be to look up
2436 the correct default arguments at the call-site, but this approach
2437 is consistent with how implicit instantiations are handled. */
2440 copy_default_args_to_explicit_spec (tree decl
)
2445 tree new_spec_types
;
2449 tree object_type
= NULL_TREE
;
2450 tree in_charge
= NULL_TREE
;
2451 tree vtt
= NULL_TREE
;
2453 /* See if there's anything we need to do. */
2454 tmpl
= DECL_TI_TEMPLATE (decl
);
2455 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2456 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2457 if (TREE_PURPOSE (t
))
2462 old_type
= TREE_TYPE (decl
);
2463 spec_types
= TYPE_ARG_TYPES (old_type
);
2465 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2467 /* Remove the this pointer, but remember the object's type for
2469 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2470 spec_types
= TREE_CHAIN (spec_types
);
2471 tmpl_types
= TREE_CHAIN (tmpl_types
);
2473 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2475 /* DECL may contain more parameters than TMPL due to the extra
2476 in-charge parameter in constructors and destructors. */
2477 in_charge
= spec_types
;
2478 spec_types
= TREE_CHAIN (spec_types
);
2480 if (DECL_HAS_VTT_PARM_P (decl
))
2483 spec_types
= TREE_CHAIN (spec_types
);
2487 /* Compute the merged default arguments. */
2489 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2491 /* Compute the new FUNCTION_TYPE. */
2495 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2500 /* Put the in-charge parameter back. */
2501 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2502 TREE_VALUE (in_charge
),
2505 new_type
= build_method_type_directly (object_type
,
2506 TREE_TYPE (old_type
),
2510 new_type
= build_function_type (TREE_TYPE (old_type
),
2512 new_type
= cp_build_type_attribute_variant (new_type
,
2513 TYPE_ATTRIBUTES (old_type
));
2514 new_type
= build_exception_variant (new_type
,
2515 TYPE_RAISES_EXCEPTIONS (old_type
));
2517 if (TYPE_HAS_LATE_RETURN_TYPE (old_type
))
2518 TYPE_HAS_LATE_RETURN_TYPE (new_type
) = 1;
2520 TREE_TYPE (decl
) = new_type
;
2523 /* Return the number of template headers we expect to see for a definition
2524 or specialization of CTYPE or one of its non-template members. */
2527 num_template_headers_for_class (tree ctype
)
2529 int num_templates
= 0;
2531 while (ctype
&& CLASS_TYPE_P (ctype
))
2533 /* You're supposed to have one `template <...>' for every
2534 template class, but you don't need one for a full
2535 specialization. For example:
2537 template <class T> struct S{};
2538 template <> struct S<int> { void f(); };
2539 void S<int>::f () {}
2541 is correct; there shouldn't be a `template <>' for the
2542 definition of `S<int>::f'. */
2543 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2544 /* If CTYPE does not have template information of any
2545 kind, then it is not a template, nor is it nested
2546 within a template. */
2548 if (explicit_class_specialization_p (ctype
))
2550 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2553 ctype
= TYPE_CONTEXT (ctype
);
2556 return num_templates
;
2559 /* Do a simple sanity check on the template headers that precede the
2560 variable declaration DECL. */
2563 check_template_variable (tree decl
)
2565 tree ctx
= CP_DECL_CONTEXT (decl
);
2566 int wanted
= num_template_headers_for_class (ctx
);
2567 if (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
2568 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2570 if (cxx_dialect
< cxx14
)
2571 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2572 "variable templates only available with "
2573 "-std=c++14 or -std=gnu++14");
2575 // Namespace-scope variable templates should have a template header.
2578 if (template_header_count
> wanted
)
2580 bool warned
= pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2581 "too many template headers for %D (should be %d)",
2583 if (warned
&& CLASS_TYPE_P (ctx
)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2585 inform (DECL_SOURCE_LOCATION (decl
),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2602 check_unqualified_spec_or_inst (tree t
, location_t loc
)
2604 tree tmpl
= most_general_template (t
);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl
)
2606 && !is_associated_namespace (current_namespace
,
2607 CP_DECL_CONTEXT (tmpl
)))
2609 if (processing_specialization
)
2610 permerror (loc
, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl
);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect
>= cxx11
)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc
, OPT_Wpedantic
, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2621 /* Check to see if the function just declared, as indicated in
2622 DECLARATOR, and in DECL, is a specialization of a function
2623 template. We may also discover that the declaration is an explicit
2624 instantiation at this point.
2626 Returns DECL, or an equivalent declaration that should be used
2627 instead if all goes well. Issues an error message if something is
2628 amiss. Returns error_mark_node if the error is not easily
2631 FLAGS is a bitmask consisting of the following flags:
2633 2: The function has a definition.
2634 4: The function is a friend.
2636 The TEMPLATE_COUNT is the number of references to qualifying
2637 template classes that appeared in the name of the function. For
2640 template <class T> struct S { void f(); };
2643 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2644 classes are not counted in the TEMPLATE_COUNT, so that in
2646 template <class T> struct S {};
2647 template <> struct S<int> { void f(); }
2648 template <> void S<int>::f();
2650 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2651 invalid; there should be no template <>.)
2653 If the function is a specialization, it is marked as such via
2654 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2655 is set up correctly, and it is added to the list of specializations
2656 for that template. */
2659 check_explicit_specialization (tree declarator
,
2664 int have_def
= flags
& 2;
2665 int is_friend
= flags
& 4;
2666 bool is_concept
= flags
& 8;
2667 int specialization
= 0;
2668 int explicit_instantiation
= 0;
2669 int member_specialization
= 0;
2670 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2671 tree dname
= DECL_NAME (decl
);
2676 if (!processing_specialization
)
2679 tsk
= tsk_excessive_parms
;
2682 tsk
= current_tmpl_spec_kind (template_count
);
2687 if (processing_specialization
&& !VAR_P (decl
))
2690 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2692 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2695 /* This could be something like:
2697 template <class T> void f(T);
2698 class S { friend void f<>(int); } */
2702 /* This case handles bogus declarations like template <>
2703 template <class T> void f<int>(); */
2705 error ("template-id %qD in declaration of primary template",
2712 case tsk_invalid_member_spec
:
2713 /* The error has already been reported in
2714 check_specialization_scope. */
2715 return error_mark_node
;
2717 case tsk_invalid_expl_inst
:
2718 error ("template parameter list used in explicit instantiation");
2724 error ("definition provided for explicit instantiation");
2726 explicit_instantiation
= 1;
2729 case tsk_excessive_parms
:
2730 case tsk_insufficient_parms
:
2731 if (tsk
== tsk_excessive_parms
)
2732 error ("too many template parameter lists in declaration of %qD",
2734 else if (template_header_count
)
2735 error("too few template parameter lists in declaration of %qD", decl
);
2737 error("explicit specialization of %qD must be introduced by "
2738 "%<template <>%>", decl
);
2743 error ("explicit specialization declared %<concept%>");
2745 if (VAR_P (decl
) && TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2746 /* In cases like template<> constexpr bool v = true;
2747 We'll give an error in check_template_variable. */
2750 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2752 member_specialization
= 1;
2758 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2760 /* This case handles bogus declarations like template <>
2761 template <class T> void f<int>(); */
2763 if (!uses_template_parms (declarator
))
2764 error ("template-id %qD in declaration of primary template",
2766 else if (variable_template_p (TREE_OPERAND (declarator
, 0)))
2768 /* Partial specialization of variable template. */
2769 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2773 else if (cxx_dialect
< cxx14
)
2774 error ("non-type partial specialization %qD "
2775 "is not allowed", declarator
);
2777 error ("non-class, non-variable partial specialization %qD "
2778 "is not allowed", declarator
);
2783 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2784 /* This is a specialization of a member template, without
2785 specialization the containing class. Something like:
2787 template <class T> struct S {
2788 template <class U> void f (U);
2790 template <> template <class U> void S<int>::f(U) {}
2792 That's a specialization -- but of the entire template. */
2800 if ((specialization
|| member_specialization
)
2801 /* This doesn't apply to variable templates. */
2802 && (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
2803 || TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
))
2805 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2806 for (; t
; t
= TREE_CHAIN (t
))
2807 if (TREE_PURPOSE (t
))
2809 permerror (input_location
,
2810 "default argument specified in explicit specialization");
2815 if (specialization
|| member_specialization
|| explicit_instantiation
)
2817 tree tmpl
= NULL_TREE
;
2818 tree targs
= NULL_TREE
;
2819 bool was_template_id
= (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
);
2821 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2822 if (!was_template_id
)
2826 gcc_assert (identifier_p (declarator
));
2831 /* If there is no class context, the explicit instantiation
2832 must be at namespace scope. */
2833 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2835 /* Find the namespace binding, using the declaration
2837 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2839 if (fns
== error_mark_node
)
2840 /* If lookup fails, look for a friend declaration so we can
2841 give a better diagnostic. */
2842 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2843 /*type*/false, /*complain*/true,
2846 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2848 error ("%qD is not a template function", dname
);
2849 fns
= error_mark_node
;
2853 declarator
= lookup_template_function (fns
, NULL_TREE
);
2856 if (declarator
== error_mark_node
)
2857 return error_mark_node
;
2859 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2861 if (!explicit_instantiation
)
2862 /* A specialization in class scope. This is invalid,
2863 but the error will already have been flagged by
2864 check_specialization_scope. */
2865 return error_mark_node
;
2868 /* It's not valid to write an explicit instantiation in
2871 class C { template void f(); }
2873 This case is caught by the parser. However, on
2876 template class C { void f(); };
2878 (which is invalid) we can get here. The error will be
2885 else if (ctype
!= NULL_TREE
2886 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2888 // We'll match variable templates in start_decl.
2892 /* Find the list of functions in ctype that have the same
2893 name as the declared function. */
2894 tree name
= TREE_OPERAND (declarator
, 0);
2895 tree fns
= NULL_TREE
;
2898 if (constructor_name_p (name
, ctype
))
2900 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2902 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2903 : !CLASSTYPE_DESTRUCTORS (ctype
))
2905 /* From [temp.expl.spec]:
2907 If such an explicit specialization for the member
2908 of a class template names an implicitly-declared
2909 special member function (clause _special_), the
2910 program is ill-formed.
2912 Similar language is found in [temp.explicit]. */
2913 error ("specialization of implicitly-declared special member function");
2914 return error_mark_node
;
2917 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2920 if (!DECL_CONV_FN_P (decl
))
2922 idx
= lookup_fnfields_1 (ctype
, name
);
2924 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2928 vec
<tree
, va_gc
> *methods
;
2931 /* For a type-conversion operator, we cannot do a
2932 name-based lookup. We might be looking for `operator
2933 int' which will be a specialization of `operator T'.
2934 So, we find *all* the conversion operators, and then
2935 select from them. */
2938 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2940 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2941 methods
->iterate (idx
, &ovl
);
2944 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2945 /* There are no more conversion functions. */
2948 /* Glue all these conversion functions together
2949 with those we already have. */
2950 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2951 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2955 if (fns
== NULL_TREE
)
2957 error ("no member function %qD declared in %qT", name
, ctype
);
2958 return error_mark_node
;
2961 TREE_OPERAND (declarator
, 0) = fns
;
2964 /* Figure out what exactly is being specialized at this point.
2965 Note that for an explicit instantiation, even one for a
2966 member function, we cannot tell apriori whether the
2967 instantiation is for a member template, or just a member
2968 function of a template class. Even if a member template is
2969 being instantiated, the member template arguments may be
2970 elided if they can be deduced from the rest of the
2972 tmpl
= determine_specialization (declarator
, decl
,
2974 member_specialization
,
2978 if (!tmpl
|| tmpl
== error_mark_node
)
2979 /* We couldn't figure out what this declaration was
2981 return error_mark_node
;
2984 if (TREE_CODE (decl
) == FUNCTION_DECL
2985 && DECL_HIDDEN_FRIEND_P (tmpl
))
2987 if (pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2988 "friend declaration %qD is not visible to "
2989 "explicit specialization", tmpl
))
2990 inform (DECL_SOURCE_LOCATION (tmpl
),
2991 "friend declaration here");
2993 else if (!ctype
&& !is_friend
2994 && CP_DECL_CONTEXT (decl
) == current_namespace
)
2995 check_unqualified_spec_or_inst (tmpl
, DECL_SOURCE_LOCATION (decl
));
2997 tree gen_tmpl
= most_general_template (tmpl
);
2999 if (explicit_instantiation
)
3001 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3002 is done by do_decl_instantiation later. */
3004 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
3005 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3007 if (arg_depth
> parm_depth
)
3009 /* If TMPL is not the most general template (for
3010 example, if TMPL is a friend template that is
3011 injected into namespace scope), then there will
3012 be too many levels of TARGS. Remove some of them
3017 new_targs
= make_tree_vec (parm_depth
);
3018 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
3019 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
3020 = TREE_VEC_ELT (targs
, i
);
3024 return instantiate_template (tmpl
, targs
, tf_error
);
3027 /* If we thought that the DECL was a member function, but it
3028 turns out to be specializing a static member function,
3029 make DECL a static member function as well. */
3030 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3031 && DECL_STATIC_FUNCTION_P (tmpl
)
3032 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
3033 revert_static_member_fn (decl
);
3035 /* If this is a specialization of a member template of a
3036 template class, we want to return the TEMPLATE_DECL, not
3037 the specialization of it. */
3038 if (tsk
== tsk_template
&& !was_template_id
)
3040 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
3041 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3042 DECL_INITIAL (result
) = NULL_TREE
;
3046 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3047 DECL_SOURCE_LOCATION (result
)
3048 = DECL_SOURCE_LOCATION (decl
);
3049 /* We want to use the argument list specified in the
3050 definition, not in the original declaration. */
3051 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
3052 for (parm
= DECL_ARGUMENTS (result
); parm
;
3053 parm
= DECL_CHAIN (parm
))
3054 DECL_CONTEXT (parm
) = result
;
3056 return register_specialization (tmpl
, gen_tmpl
, targs
,
3060 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3061 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
3063 if (was_template_id
)
3064 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
)) = true;
3066 /* Inherit default function arguments from the template
3067 DECL is specializing. */
3068 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3069 copy_default_args_to_explicit_spec (decl
);
3071 /* This specialization has the same protection as the
3072 template it specializes. */
3073 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
3074 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
3076 /* 7.1.1-1 [dcl.stc]
3078 A storage-class-specifier shall not be specified in an
3079 explicit specialization...
3081 The parser rejects these, so unless action is taken here,
3082 explicit function specializations will always appear with
3085 The action recommended by the C++ CWG in response to C++
3086 defect report 605 is to make the storage class and linkage
3087 of the explicit specialization match the templated function:
3089 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3091 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
3093 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
3094 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
3096 /* A concept cannot be specialized. */
3097 if (DECL_DECLARED_CONCEPT_P (tmpl_func
))
3099 error ("explicit specialization of function concept %qD",
3101 return error_mark_node
;
3104 /* This specialization has the same linkage and visibility as
3105 the function template it specializes. */
3106 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
3107 if (! TREE_PUBLIC (decl
))
3109 DECL_INTERFACE_KNOWN (decl
) = 1;
3110 DECL_NOT_REALLY_EXTERN (decl
) = 1;
3112 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
3113 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
3115 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
3116 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
3120 /* If DECL is a friend declaration, declared using an
3121 unqualified name, the namespace associated with DECL may
3122 have been set incorrectly. For example, in:
3124 template <typename T> void f(T);
3126 struct S { friend void f<int>(int); }
3129 we will have set the DECL_CONTEXT for the friend
3130 declaration to N, rather than to the global namespace. */
3131 if (DECL_NAMESPACE_SCOPE_P (decl
))
3132 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
3134 if (is_friend
&& !have_def
)
3135 /* This is not really a declaration of a specialization.
3136 It's just the name of an instantiation. But, it's not
3137 a request for an instantiation, either. */
3138 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
3139 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3140 /* A specialization is not necessarily COMDAT. */
3141 DECL_COMDAT (decl
) = (TREE_PUBLIC (decl
)
3142 && DECL_DECLARED_INLINE_P (decl
));
3143 else if (VAR_P (decl
))
3144 DECL_COMDAT (decl
) = false;
3146 /* If this is a full specialization, register it so that we can find
3147 it again. Partial specializations will be registered in
3148 process_partial_specialization. */
3149 if (!processing_template_decl
)
3150 decl
= register_specialization (decl
, gen_tmpl
, targs
,
3153 /* A 'structor should already have clones. */
3154 gcc_assert (decl
== error_mark_node
3155 || variable_template_p (tmpl
)
3156 || !(DECL_CONSTRUCTOR_P (decl
)
3157 || DECL_DESTRUCTOR_P (decl
))
3158 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl
)));
3165 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3166 parameters. These are represented in the same format used for
3167 DECL_TEMPLATE_PARMS. */
3170 comp_template_parms (const_tree parms1
, const_tree parms2
)
3175 if (parms1
== parms2
)
3178 for (p1
= parms1
, p2
= parms2
;
3179 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
3180 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
3182 tree t1
= TREE_VALUE (p1
);
3183 tree t2
= TREE_VALUE (p2
);
3186 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
3187 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
3189 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
3192 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
3194 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
3195 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
3197 /* If either of the template parameters are invalid, assume
3198 they match for the sake of error recovery. */
3199 if (error_operand_p (parm1
) || error_operand_p (parm2
))
3202 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
3205 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
3206 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
3207 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
3209 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
3214 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
3215 /* One set of parameters has more parameters lists than the
3222 /* Determine whether PARM is a parameter pack. */
3225 template_parameter_pack_p (const_tree parm
)
3227 /* Determine if we have a non-type template parameter pack. */
3228 if (TREE_CODE (parm
) == PARM_DECL
)
3229 return (DECL_TEMPLATE_PARM_P (parm
)
3230 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
3231 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
3232 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
3234 /* If this is a list of template parameters, we could get a
3235 TYPE_DECL or a TEMPLATE_DECL. */
3236 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
3237 parm
= TREE_TYPE (parm
);
3239 /* Otherwise it must be a type template parameter. */
3240 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
3241 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
3242 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
3245 /* Determine if T is a function parameter pack. */
3248 function_parameter_pack_p (const_tree t
)
3250 if (t
&& TREE_CODE (t
) == PARM_DECL
)
3251 return DECL_PACK_P (t
);
3255 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3256 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3259 get_function_template_decl (const_tree primary_func_tmpl_inst
)
3261 if (! primary_func_tmpl_inst
3262 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
3263 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
3266 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
3269 /* Return true iff the function parameter PARAM_DECL was expanded
3270 from the function parameter pack PACK. */
3273 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
3275 if (DECL_ARTIFICIAL (param_decl
)
3276 || !function_parameter_pack_p (pack
))
3279 /* The parameter pack and its pack arguments have the same
3281 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
3284 /* Determine whether ARGS describes a variadic template args list,
3285 i.e., one that is terminated by a template argument pack. */
3288 template_args_variadic_p (tree args
)
3293 if (args
== NULL_TREE
)
3296 args
= INNERMOST_TEMPLATE_ARGS (args
);
3297 nargs
= TREE_VEC_LENGTH (args
);
3302 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
3304 return ARGUMENT_PACK_P (last_parm
);
3307 /* Generate a new name for the parameter pack name NAME (an
3308 IDENTIFIER_NODE) that incorporates its */
3311 make_ith_pack_parameter_name (tree name
, int i
)
3313 /* Munge the name to include the parameter index. */
3314 #define NUMBUF_LEN 128
3315 char numbuf
[NUMBUF_LEN
];
3319 if (name
== NULL_TREE
)
3321 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
3322 newname_len
= IDENTIFIER_LENGTH (name
)
3323 + strlen (numbuf
) + 2;
3324 newname
= (char*)alloca (newname_len
);
3325 snprintf (newname
, newname_len
,
3326 "%s#%i", IDENTIFIER_POINTER (name
), i
);
3327 return get_identifier (newname
);
3330 /* Return true if T is a primary function, class or alias template
3334 primary_template_instantiation_p (const_tree t
)
3339 if (TREE_CODE (t
) == FUNCTION_DECL
)
3340 return DECL_LANG_SPECIFIC (t
)
3341 && DECL_TEMPLATE_INSTANTIATION (t
)
3342 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
3343 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3344 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
3345 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
3346 else if (alias_template_specialization_p (t
))
3351 /* Return true if PARM is a template template parameter. */
3354 template_template_parameter_p (const_tree parm
)
3356 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
3359 /* Return true iff PARM is a DECL representing a type template
3363 template_type_parameter_p (const_tree parm
)
3366 && (TREE_CODE (parm
) == TYPE_DECL
3367 || TREE_CODE (parm
) == TEMPLATE_DECL
)
3368 && DECL_TEMPLATE_PARM_P (parm
));
3371 /* Return the template parameters of T if T is a
3372 primary template instantiation, NULL otherwise. */
3375 get_primary_template_innermost_parameters (const_tree t
)
3377 tree parms
= NULL
, template_info
= NULL
;
3379 if ((template_info
= get_template_info (t
))
3380 && primary_template_instantiation_p (t
))
3381 parms
= INNERMOST_TEMPLATE_PARMS
3382 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3387 /* Return the template parameters of the LEVELth level from the full list
3388 of template parameters PARMS. */
3391 get_template_parms_at_level (tree parms
, int level
)
3395 || TREE_CODE (parms
) != TREE_LIST
3396 || level
> TMPL_PARMS_DEPTH (parms
))
3399 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3400 if (TMPL_PARMS_DEPTH (p
) == level
)
3406 /* Returns the template arguments of T if T is a template instantiation,
3410 get_template_innermost_arguments (const_tree t
)
3412 tree args
= NULL
, template_info
= NULL
;
3414 if ((template_info
= get_template_info (t
))
3415 && TI_ARGS (template_info
))
3416 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3421 /* Return the argument pack elements of T if T is a template argument pack,
3425 get_template_argument_pack_elems (const_tree t
)
3427 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3428 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3431 return ARGUMENT_PACK_ARGS (t
);
3434 /* Structure used to track the progress of find_parameter_packs_r. */
3435 struct find_parameter_pack_data
3437 /* TREE_LIST that will contain all of the parameter packs found by
3439 tree
* parameter_packs
;
3441 /* Set of AST nodes that have been visited by the traversal. */
3442 hash_set
<tree
> *visited
;
3444 /* True iff we're making a type pack expansion. */
3445 bool type_pack_expansion_p
;
3448 /* Identifies all of the argument packs that occur in a template
3449 argument and appends them to the TREE_LIST inside DATA, which is a
3450 find_parameter_pack_data structure. This is a subroutine of
3451 make_pack_expansion and uses_parameter_packs. */
3453 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3456 struct find_parameter_pack_data
* ppd
=
3457 (struct find_parameter_pack_data
*)data
;
3458 bool parameter_pack_p
= false;
3460 /* Handle type aliases/typedefs. */
3461 if (TYPE_ALIAS_P (t
))
3463 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
3464 cp_walk_tree (&TI_ARGS (tinfo
),
3465 &find_parameter_packs_r
,
3471 /* Identify whether this is a parameter pack or not. */
3472 switch (TREE_CODE (t
))
3474 case TEMPLATE_PARM_INDEX
:
3475 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3476 parameter_pack_p
= true;
3479 case TEMPLATE_TYPE_PARM
:
3480 t
= TYPE_MAIN_VARIANT (t
);
3482 case TEMPLATE_TEMPLATE_PARM
:
3483 /* If the placeholder appears in the decl-specifier-seq of a function
3484 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3485 is a pack expansion, the invented template parameter is a template
3487 if (ppd
->type_pack_expansion_p
&& is_auto_or_concept (t
))
3488 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
3489 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3490 parameter_pack_p
= true;
3495 if (DECL_PACK_P (t
))
3497 /* We don't want to walk into the type of a PARM_DECL,
3498 because we don't want to see the type parameter pack. */
3500 parameter_pack_p
= true;
3504 /* Look through a lambda capture proxy to the field pack. */
3506 if (DECL_HAS_VALUE_EXPR_P (t
))
3508 tree v
= DECL_VALUE_EXPR (t
);
3510 &find_parameter_packs_r
,
3514 else if (variable_template_specialization_p (t
))
3516 cp_walk_tree (&DECL_TI_ARGS (t
),
3517 find_parameter_packs_r
,
3524 parameter_pack_p
= true;
3527 /* Not a parameter pack. */
3531 if (parameter_pack_p
)
3533 /* Add this parameter pack to the list. */
3534 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3538 cp_walk_tree (&TYPE_CONTEXT (t
),
3539 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3541 /* This switch statement will return immediately if we don't find a
3543 switch (TREE_CODE (t
))
3545 case TEMPLATE_PARM_INDEX
:
3548 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3549 /* Check the template itself. */
3550 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3551 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3552 /* Check the template arguments. */
3553 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3558 case TEMPLATE_TYPE_PARM
:
3559 case TEMPLATE_TEMPLATE_PARM
:
3566 if (TYPE_PTRMEMFUNC_P (t
))
3572 if (TYPE_TEMPLATE_INFO (t
))
3573 cp_walk_tree (&TYPE_TI_ARGS (t
),
3574 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3581 cp_walk_tree (&TREE_TYPE (t
),
3582 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3586 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3591 case TYPE_PACK_EXPANSION
:
3592 case EXPR_PACK_EXPANSION
:
3597 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3602 case IDENTIFIER_NODE
:
3603 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3610 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3611 type_pack_expansion_p to false so that any placeholders
3612 within the expression don't get marked as parameter packs. */
3613 bool type_pack_expansion_p
= ppd
->type_pack_expansion_p
;
3614 ppd
->type_pack_expansion_p
= false;
3615 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t
), &find_parameter_packs_r
,
3617 ppd
->type_pack_expansion_p
= type_pack_expansion_p
;
3629 /* Determines if the expression or type T uses any parameter packs. */
3631 uses_parameter_packs (tree t
)
3633 tree parameter_packs
= NULL_TREE
;
3634 struct find_parameter_pack_data ppd
;
3635 ppd
.parameter_packs
= ¶meter_packs
;
3636 ppd
.visited
= new hash_set
<tree
>;
3637 ppd
.type_pack_expansion_p
= false;
3638 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3640 return parameter_packs
!= NULL_TREE
;
3643 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3644 representation a base-class initializer into a parameter pack
3645 expansion. If all goes well, the resulting node will be an
3646 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3649 make_pack_expansion (tree arg
)
3652 tree parameter_packs
= NULL_TREE
;
3653 bool for_types
= false;
3654 struct find_parameter_pack_data ppd
;
3656 if (!arg
|| arg
== error_mark_node
)
3659 if (TREE_CODE (arg
) == TREE_LIST
&& TREE_PURPOSE (arg
))
3661 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3662 class initializer. In this case, the TREE_PURPOSE will be a
3663 _TYPE node (representing the base class expansion we're
3664 initializing) and the TREE_VALUE will be a TREE_LIST
3665 containing the initialization arguments.
3667 The resulting expansion looks somewhat different from most
3668 expansions. Rather than returning just one _EXPANSION, we
3669 return a TREE_LIST whose TREE_PURPOSE is a
3670 TYPE_PACK_EXPANSION containing the bases that will be
3671 initialized. The TREE_VALUE will be identical to the
3672 original TREE_VALUE, which is a list of arguments that will
3673 be passed to each base. We do not introduce any new pack
3674 expansion nodes into the TREE_VALUE (although it is possible
3675 that some already exist), because the TREE_PURPOSE and
3676 TREE_VALUE all need to be expanded together with the same
3677 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3678 resulting TREE_PURPOSE will mention the parameter packs in
3679 both the bases and the arguments to the bases. */
3682 tree parameter_packs
= NULL_TREE
;
3684 /* Determine which parameter packs will be used by the base
3686 ppd
.visited
= new hash_set
<tree
>;
3687 ppd
.parameter_packs
= ¶meter_packs
;
3688 ppd
.type_pack_expansion_p
= true;
3689 gcc_assert (TYPE_P (TREE_PURPOSE (arg
)));
3690 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3693 if (parameter_packs
== NULL_TREE
)
3695 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3697 return error_mark_node
;
3700 if (TREE_VALUE (arg
) != void_type_node
)
3702 /* Collect the sets of parameter packs used in each of the
3703 initialization arguments. */
3704 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3706 /* Determine which parameter packs will be expanded in this
3708 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3715 /* Create the pack expansion type for the base type. */
3716 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3717 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3718 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3720 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3721 they will rarely be compared to anything. */
3722 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3724 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3727 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3730 /* Build the PACK_EXPANSION_* node. */
3732 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3733 : make_node (EXPR_PACK_EXPANSION
);
3734 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3735 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3737 /* Propagate type and const-expression information. */
3738 TREE_TYPE (result
) = TREE_TYPE (arg
);
3739 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3740 /* Mark this read now, since the expansion might be length 0. */
3741 mark_exp_read (arg
);
3744 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3745 they will rarely be compared to anything. */
3746 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3748 /* Determine which parameter packs will be expanded. */
3749 ppd
.parameter_packs
= ¶meter_packs
;
3750 ppd
.visited
= new hash_set
<tree
>;
3751 ppd
.type_pack_expansion_p
= TYPE_P (arg
);
3752 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3755 /* Make sure we found some parameter packs. */
3756 if (parameter_packs
== NULL_TREE
)
3759 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3761 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3762 return error_mark_node
;
3764 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3766 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3771 /* Checks T for any "bare" parameter packs, which have not yet been
3772 expanded, and issues an error if any are found. This operation can
3773 only be done on full expressions or types (e.g., an expression
3774 statement, "if" condition, etc.), because we could have expressions like:
3776 foo(f(g(h(args)))...)
3778 where "args" is a parameter pack. check_for_bare_parameter_packs
3779 should not be called for the subexpressions args, h(args),
3780 g(h(args)), or f(g(h(args))), because we would produce erroneous
3783 Returns TRUE and emits an error if there were bare parameter packs,
3784 returns FALSE otherwise. */
3786 check_for_bare_parameter_packs (tree t
)
3788 tree parameter_packs
= NULL_TREE
;
3789 struct find_parameter_pack_data ppd
;
3791 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3794 if (TREE_CODE (t
) == TYPE_DECL
)
3797 ppd
.parameter_packs
= ¶meter_packs
;
3798 ppd
.visited
= new hash_set
<tree
>;
3799 ppd
.type_pack_expansion_p
= false;
3800 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3803 if (parameter_packs
)
3805 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
3806 error_at (loc
, "parameter packs not expanded with %<...%>:");
3807 while (parameter_packs
)
3809 tree pack
= TREE_VALUE (parameter_packs
);
3810 tree name
= NULL_TREE
;
3812 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3813 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3814 name
= TYPE_NAME (pack
);
3815 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3816 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3818 name
= DECL_NAME (pack
);
3821 inform (loc
, " %qD", name
);
3823 inform (loc
, " <anonymous>");
3825 parameter_packs
= TREE_CHAIN (parameter_packs
);
3834 /* Expand any parameter packs that occur in the template arguments in
3837 expand_template_argument_pack (tree args
)
3839 tree result_args
= NULL_TREE
;
3840 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3841 int num_result_args
= -1;
3842 int non_default_args_count
= -1;
3844 /* First, determine if we need to expand anything, and the number of
3845 slots we'll need. */
3846 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3848 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3849 if (arg
== NULL_TREE
)
3851 if (ARGUMENT_PACK_P (arg
))
3853 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3854 if (num_result_args
< 0)
3855 num_result_args
= in_arg
+ num_packed
;
3857 num_result_args
+= num_packed
;
3861 if (num_result_args
>= 0)
3866 /* If no expansion is necessary, we're done. */
3867 if (num_result_args
< 0)
3870 /* Expand arguments. */
3871 result_args
= make_tree_vec (num_result_args
);
3872 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3873 non_default_args_count
=
3874 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3875 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3877 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3878 if (ARGUMENT_PACK_P (arg
))
3880 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3881 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3882 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3883 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3884 if (non_default_args_count
> 0)
3885 non_default_args_count
+= num_packed
- 1;
3889 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3893 if (non_default_args_count
>= 0)
3894 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3898 /* Checks if DECL shadows a template parameter.
3900 [temp.local]: A template-parameter shall not be redeclared within its
3901 scope (including nested scopes).
3903 Emits an error and returns TRUE if the DECL shadows a parameter,
3904 returns FALSE otherwise. */
3907 check_template_shadow (tree decl
)
3911 /* If we're not in a template, we can't possibly shadow a template
3913 if (!current_template_parms
)
3916 /* Figure out what we're shadowing. */
3917 if (TREE_CODE (decl
) == OVERLOAD
)
3918 decl
= OVL_CURRENT (decl
);
3919 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3921 /* If there's no previous binding for this name, we're not shadowing
3922 anything, let alone a template parameter. */
3926 /* If we're not shadowing a template parameter, we're done. Note
3927 that OLDDECL might be an OVERLOAD (or perhaps even an
3928 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3930 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3933 /* We check for decl != olddecl to avoid bogus errors for using a
3934 name inside a class. We check TPFI to avoid duplicate errors for
3935 inline member templates. */
3937 || (DECL_TEMPLATE_PARM_P (decl
)
3938 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
)))
3941 /* Don't complain about the injected class name, as we've already
3942 complained about the class itself. */
3943 if (DECL_SELF_REFERENCE_P (decl
))
3946 if (DECL_TEMPLATE_PARM_P (decl
))
3947 error ("declaration of template parameter %q+D shadows "
3948 "template parameter", decl
);
3950 error ("declaration of %q+#D shadows template parameter", decl
);
3951 inform (DECL_SOURCE_LOCATION (olddecl
),
3952 "template parameter %qD declared here", olddecl
);
3956 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3957 ORIG_LEVEL, DECL, and TYPE. */
3960 build_template_parm_index (int index
,
3966 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3967 TEMPLATE_PARM_IDX (t
) = index
;
3968 TEMPLATE_PARM_LEVEL (t
) = level
;
3969 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3970 TEMPLATE_PARM_DECL (t
) = decl
;
3971 TREE_TYPE (t
) = type
;
3972 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3973 TREE_READONLY (t
) = TREE_READONLY (decl
);
3978 /* Find the canonical type parameter for the given template type
3979 parameter. Returns the canonical type parameter, which may be TYPE
3980 if no such parameter existed. */
3983 canonical_type_parameter (tree type
)
3986 int idx
= TEMPLATE_TYPE_IDX (type
);
3987 if (!canonical_template_parms
)
3988 vec_alloc (canonical_template_parms
, idx
+1);
3990 while (canonical_template_parms
->length () <= (unsigned)idx
)
3991 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3993 list
= (*canonical_template_parms
)[idx
];
3994 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3995 list
= TREE_CHAIN (list
);
3998 return TREE_VALUE (list
);
4001 (*canonical_template_parms
)[idx
]
4002 = tree_cons (NULL_TREE
, type
,
4003 (*canonical_template_parms
)[idx
]);
4008 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4009 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4010 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4011 new one is created. */
4014 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
4015 tsubst_flags_t complain
)
4017 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
4018 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
4019 != TEMPLATE_PARM_LEVEL (index
) - levels
)
4020 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
4022 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
4025 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
4026 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
4027 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
4028 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
4029 DECL_ARTIFICIAL (decl
) = 1;
4030 SET_DECL_TEMPLATE_PARM_P (decl
);
4032 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
4033 TEMPLATE_PARM_LEVEL (index
) - levels
,
4034 TEMPLATE_PARM_ORIG_LEVEL (index
),
4036 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
4037 TEMPLATE_PARM_PARAMETER_PACK (t
)
4038 = TEMPLATE_PARM_PARAMETER_PACK (index
);
4040 /* Template template parameters need this. */
4041 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4043 DECL_TEMPLATE_RESULT (decl
)
4044 = build_decl (DECL_SOURCE_LOCATION (decl
),
4045 TYPE_DECL
, DECL_NAME (decl
), type
);
4046 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl
)) = true;
4047 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
4048 (DECL_TEMPLATE_PARMS (orig_decl
), args
, complain
);
4052 return TEMPLATE_PARM_DESCENDANTS (index
);
4055 /* Process information from new template parameter PARM and append it
4056 to the LIST being built. This new parameter is a non-type
4057 parameter iff IS_NON_TYPE is true. This new parameter is a
4058 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4062 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
4063 bool is_non_type
, bool is_parameter_pack
)
4068 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
4069 tree defval
= TREE_PURPOSE (parm
);
4070 tree constr
= TREE_TYPE (parm
);
4074 tree p
= tree_last (list
);
4076 if (p
&& TREE_VALUE (p
) != error_mark_node
)
4079 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
4080 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
4082 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
4090 parm
= TREE_VALUE (parm
);
4092 SET_DECL_TEMPLATE_PARM_P (parm
);
4094 if (TREE_TYPE (parm
) != error_mark_node
)
4098 The top-level cv-qualifiers on the template-parameter are
4099 ignored when determining its type. */
4100 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
4101 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
4102 TREE_TYPE (parm
) = error_mark_node
;
4103 else if (uses_parameter_packs (TREE_TYPE (parm
))
4104 && !is_parameter_pack
4105 /* If we're in a nested template parameter list, the template
4106 template parameter could be a parameter pack. */
4107 && processing_template_parmlist
== 1)
4109 /* This template parameter is not a parameter pack, but it
4110 should be. Complain about "bare" parameter packs. */
4111 check_for_bare_parameter_packs (TREE_TYPE (parm
));
4113 /* Recover by calling this a parameter pack. */
4114 is_parameter_pack
= true;
4118 /* A template parameter is not modifiable. */
4119 TREE_CONSTANT (parm
) = 1;
4120 TREE_READONLY (parm
) = 1;
4121 decl
= build_decl (parm_loc
,
4122 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
4123 TREE_CONSTANT (decl
) = 1;
4124 TREE_READONLY (decl
) = 1;
4125 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
4126 = build_template_parm_index (idx
, processing_template_decl
,
4127 processing_template_decl
,
4128 decl
, TREE_TYPE (parm
));
4130 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4131 = is_parameter_pack
;
4136 parm
= TREE_VALUE (TREE_VALUE (parm
));
4138 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
4140 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
4141 /* This is for distinguishing between real templates and template
4142 template parameters */
4143 TREE_TYPE (parm
) = t
;
4144 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
4149 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
4150 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4151 decl
= build_decl (parm_loc
,
4152 TYPE_DECL
, parm
, t
);
4155 TYPE_NAME (t
) = decl
;
4156 TYPE_STUB_DECL (t
) = decl
;
4158 TEMPLATE_TYPE_PARM_INDEX (t
)
4159 = build_template_parm_index (idx
, processing_template_decl
,
4160 processing_template_decl
,
4161 decl
, TREE_TYPE (parm
));
4162 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
4163 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
4165 DECL_ARTIFICIAL (decl
) = 1;
4166 SET_DECL_TEMPLATE_PARM_P (decl
);
4168 /* Build requirements for the type/template parameter.
4169 This must be done after SET_DECL_TEMPLATE_PARM_P or
4170 process_template_parm could fail. */
4171 tree reqs
= finish_shorthand_constraint (parm
, constr
);
4175 /* Build the parameter node linking the parameter declaration,
4176 its default argument (if any), and its constraints (if any). */
4177 parm
= build_tree_list (defval
, parm
);
4178 TEMPLATE_PARM_CONSTRAINTS (parm
) = reqs
;
4180 return chainon (list
, parm
);
4183 /* The end of a template parameter list has been reached. Process the
4184 tree list into a parameter vector, converting each parameter into a more
4185 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4189 end_template_parm_list (tree parms
)
4193 tree saved_parmlist
= make_tree_vec (list_length (parms
));
4195 /* Pop the dummy parameter level and add the real one. */
4196 current_template_parms
= TREE_CHAIN (current_template_parms
);
4198 current_template_parms
4199 = tree_cons (size_int (processing_template_decl
),
4200 saved_parmlist
, current_template_parms
);
4202 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
4204 next
= TREE_CHAIN (parm
);
4205 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
4206 TREE_CHAIN (parm
) = NULL_TREE
;
4209 --processing_template_parmlist
;
4211 return saved_parmlist
;
4214 // Explicitly indicate the end of the template parameter list. We assume
4215 // that the current template parameters have been constructed and/or
4216 // managed explicitly, as when creating new template template parameters
4217 // from a shorthand constraint.
4219 end_template_parm_list ()
4221 --processing_template_parmlist
;
4224 /* end_template_decl is called after a template declaration is seen. */
4227 end_template_decl (void)
4229 reset_specialization ();
4231 if (! processing_template_decl
)
4234 /* This matches the pushlevel in begin_template_parm_list. */
4237 --processing_template_decl
;
4238 current_template_parms
= TREE_CHAIN (current_template_parms
);
4241 /* Takes a TREE_LIST representing a template parameter and convert it
4242 into an argument suitable to be passed to the type substitution
4243 functions. Note that If the TREE_LIST contains an error_mark
4244 node, the returned argument is error_mark_node. */
4247 template_parm_to_arg (tree t
)
4251 || TREE_CODE (t
) != TREE_LIST
)
4254 if (error_operand_p (TREE_VALUE (t
)))
4255 return error_mark_node
;
4259 if (TREE_CODE (t
) == TYPE_DECL
4260 || TREE_CODE (t
) == TEMPLATE_DECL
)
4264 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4266 /* Turn this argument into a TYPE_ARGUMENT_PACK
4267 with a single element, which expands T. */
4268 tree vec
= make_tree_vec (1);
4270 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4272 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4274 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4275 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4280 t
= DECL_INITIAL (t
);
4282 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4284 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4285 with a single element, which expands T. */
4286 tree vec
= make_tree_vec (1);
4287 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4289 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
4291 t
= convert_from_reference (t
);
4292 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4294 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4295 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4296 TREE_TYPE (t
) = type
;
4299 t
= convert_from_reference (t
);
4304 /* Given a single level of template parameters (a TREE_VEC), return it
4305 as a set of template arguments. */
4308 template_parms_level_to_args (tree parms
)
4310 tree a
= copy_node (parms
);
4311 TREE_TYPE (a
) = NULL_TREE
;
4312 for (int i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4313 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4316 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4321 /* Given a set of template parameters, return them as a set of template
4322 arguments. The template parameters are represented as a TREE_VEC, in
4323 the form documented in cp-tree.h for template arguments. */
4326 template_parms_to_args (tree parms
)
4329 tree args
= NULL_TREE
;
4330 int length
= TMPL_PARMS_DEPTH (parms
);
4333 /* If there is only one level of template parameters, we do not
4334 create a TREE_VEC of TREE_VECs. Instead, we return a single
4335 TREE_VEC containing the arguments. */
4337 args
= make_tree_vec (length
);
4339 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
4341 tree a
= template_parms_level_to_args (TREE_VALUE (header
));
4344 TREE_VEC_ELT (args
, --l
) = a
;
4352 /* Within the declaration of a template, return the currently active
4353 template parameters as an argument TREE_VEC. */
4356 current_template_args (void)
4358 return template_parms_to_args (current_template_parms
);
4361 /* Update the declared TYPE by doing any lookups which were thought to be
4362 dependent, but are not now that we know the SCOPE of the declarator. */
4365 maybe_update_decl_type (tree orig_type
, tree scope
)
4367 tree type
= orig_type
;
4369 if (type
== NULL_TREE
)
4372 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4373 type
= TREE_TYPE (type
);
4375 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4376 && dependent_type_p (type
)
4377 /* Don't bother building up the args in this case. */
4378 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4380 /* tsubst in the args corresponding to the template parameters,
4381 including auto if present. Most things will be unchanged, but
4382 make_typename_type and tsubst_qualified_id will resolve
4383 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4384 tree args
= current_template_args ();
4385 tree auto_node
= type_uses_auto (type
);
4389 tree auto_vec
= make_tree_vec (1);
4390 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4391 args
= add_to_template_args (args
, auto_vec
);
4393 pushed
= push_scope (scope
);
4394 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4399 if (type
== error_mark_node
)
4402 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4404 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4407 type
= TYPE_NAME (type
);
4412 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4413 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4414 the new template is a member template. */
4417 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4419 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4420 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4421 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4422 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4423 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4428 struct template_parm_data
4430 /* The level of the template parameters we are currently
4434 /* The index of the specialization argument we are currently
4438 /* An array whose size is the number of template parameters. The
4439 elements are nonzero if the parameter has been used in any one
4440 of the arguments processed so far. */
4443 /* An array whose size is the number of template arguments. The
4444 elements are nonzero if the argument makes use of template
4445 parameters of this level. */
4446 int* arg_uses_template_parms
;
4449 /* Subroutine of push_template_decl used to see if each template
4450 parameter in a partial specialization is used in the explicit
4451 argument list. If T is of the LEVEL given in DATA (which is
4452 treated as a template_parm_data*), then DATA->PARMS is marked
4456 mark_template_parm (tree t
, void* data
)
4460 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4462 template_parm_level_and_index (t
, &level
, &idx
);
4464 if (level
== tpd
->level
)
4466 tpd
->parms
[idx
] = 1;
4467 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4470 /* Return zero so that for_each_template_parm will continue the
4471 traversal of the tree; we want to mark *every* template parm. */
4475 /* Process the partial specialization DECL. */
4478 process_partial_specialization (tree decl
)
4480 tree type
= TREE_TYPE (decl
);
4481 tree tinfo
= get_template_info (decl
);
4482 tree maintmpl
= TI_TEMPLATE (tinfo
);
4483 tree specargs
= TI_ARGS (tinfo
);
4484 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4485 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4488 int nargs
= TREE_VEC_LENGTH (inner_args
);
4491 bool did_error_intro
= false;
4492 struct template_parm_data tpd
;
4493 struct template_parm_data tpd2
;
4495 gcc_assert (current_template_parms
);
4497 /* A concept cannot be specialized. */
4498 if (flag_concepts
&& variable_concept_p (maintmpl
))
4500 error ("specialization of variable concept %q#D", maintmpl
);
4501 return error_mark_node
;
4504 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4505 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4507 /* We check that each of the template parameters given in the
4508 partial specialization is used in the argument list to the
4509 specialization. For example:
4511 template <class T> struct S;
4512 template <class T> struct S<T*>;
4514 The second declaration is OK because `T*' uses the template
4515 parameter T, whereas
4517 template <class T> struct S<int>;
4519 is no good. Even trickier is:
4530 The S2<T> declaration is actually invalid; it is a
4531 full-specialization. Of course,
4534 struct S2<T (*)(U)>;
4536 or some such would have been OK. */
4537 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4538 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4539 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4541 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4542 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4543 for (i
= 0; i
< nargs
; ++i
)
4545 tpd
.current_arg
= i
;
4546 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4547 &mark_template_parm
,
4550 /*include_nondeduced_p=*/false);
4552 for (i
= 0; i
< ntparms
; ++i
)
4553 if (tpd
.parms
[i
] == 0)
4555 /* One of the template parms was not used in a deduced context in the
4557 if (!did_error_intro
)
4559 error ("template parameters not deducible in "
4560 "partial specialization:");
4561 did_error_intro
= true;
4564 inform (input_location
, " %qD",
4565 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4568 if (did_error_intro
)
4569 return error_mark_node
;
4571 /* [temp.class.spec]
4573 The argument list of the specialization shall not be identical to
4574 the implicit argument list of the primary template. */
4576 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl
)));
4577 if (comp_template_args (inner_args
, INNERMOST_TEMPLATE_ARGS (main_args
))
4579 || !strictly_subsumes (current_template_constraints (),
4580 get_constraints (maintmpl
))))
4583 error ("partial specialization %q+D does not specialize "
4584 "any template arguments", decl
);
4586 error ("partial specialization %q+D does not specialize any "
4587 "template arguments and is not more constrained than", decl
);
4588 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4591 /* A partial specialization that replaces multiple parameters of the
4592 primary template with a pack expansion is less specialized for those
4594 if (nargs
< DECL_NTPARMS (maintmpl
))
4596 error ("partial specialization is not more specialized than the "
4597 "primary template because it replaces multiple parameters "
4598 "with a pack expansion");
4599 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4603 /* [temp.class.spec]
4605 A partially specialized non-type argument expression shall not
4606 involve template parameters of the partial specialization except
4607 when the argument expression is a simple identifier.
4609 The type of a template parameter corresponding to a specialized
4610 non-type argument shall not be dependent on a parameter of the
4613 Also, we verify that pack expansions only occur at the
4614 end of the argument list. */
4615 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4617 for (i
= 0; i
< nargs
; ++i
)
4619 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4620 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4621 tree packed_args
= NULL_TREE
;
4624 if (ARGUMENT_PACK_P (arg
))
4626 /* Extract the arguments from the argument pack. We'll be
4627 iterating over these in the following loop. */
4628 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4629 len
= TREE_VEC_LENGTH (packed_args
);
4632 for (j
= 0; j
< len
; j
++)
4635 /* Get the Jth argument in the parameter pack. */
4636 arg
= TREE_VEC_ELT (packed_args
, j
);
4638 if (PACK_EXPANSION_P (arg
))
4640 /* Pack expansions must come at the end of the
4642 if ((packed_args
&& j
< len
- 1)
4643 || (!packed_args
&& i
< nargs
- 1))
4645 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4646 error ("parameter pack argument %qE must be at the "
4647 "end of the template argument list", arg
);
4649 error ("parameter pack argument %qT must be at the "
4650 "end of the template argument list", arg
);
4654 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4655 /* We only care about the pattern. */
4656 arg
= PACK_EXPANSION_PATTERN (arg
);
4658 if (/* These first two lines are the `non-type' bit. */
4660 && TREE_CODE (arg
) != TEMPLATE_DECL
4661 /* This next two lines are the `argument expression is not just a
4662 simple identifier' condition and also the `specialized
4663 non-type argument' bit. */
4664 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
4665 && !(REFERENCE_REF_P (arg
)
4666 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_PARM_INDEX
))
4668 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4669 || (packed_args
&& uses_template_parms (arg
)))
4670 error ("template argument %qE involves template parameter(s)",
4674 /* Look at the corresponding template parameter,
4675 marking which template parameters its type depends
4677 tree type
= TREE_TYPE (parm
);
4681 /* We haven't yet initialized TPD2. Do so now. */
4682 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4683 /* The number of parameters here is the number in the
4684 main template, which, as checked in the assertion
4686 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4688 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4691 /* Mark the template parameters. But this time, we're
4692 looking for the template parameters of the main
4693 template, not in the specialization. */
4694 tpd2
.current_arg
= i
;
4695 tpd2
.arg_uses_template_parms
[i
] = 0;
4696 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4697 for_each_template_parm (type
,
4698 &mark_template_parm
,
4701 /*include_nondeduced_p=*/false);
4703 if (tpd2
.arg_uses_template_parms
[i
])
4705 /* The type depended on some template parameters.
4706 If they are fully specialized in the
4707 specialization, that's OK. */
4710 for (j
= 0; j
< nargs
; ++j
)
4711 if (tpd2
.parms
[j
] != 0
4712 && tpd
.arg_uses_template_parms
[j
])
4715 error_n (input_location
, count
,
4716 "type %qT of template argument %qE depends "
4717 "on a template parameter",
4718 "type %qT of template argument %qE depends "
4719 "on template parameters",
4728 /* We should only get here once. */
4729 if (TREE_CODE (decl
) == TYPE_DECL
)
4730 gcc_assert (!COMPLETE_TYPE_P (type
));
4732 // Build the template decl.
4733 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4734 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4735 TREE_TYPE (tmpl
) = type
;
4736 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4737 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4738 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4739 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4742 /* We didn't register this in check_explicit_specialization so we could
4743 wait until the constraints were set. */
4744 decl
= register_specialization (decl
, maintmpl
, specargs
, false, 0);
4746 associate_classtype_constraints (type
);
4748 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4749 = tree_cons (specargs
, tmpl
,
4750 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4751 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4753 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4754 inst
= TREE_CHAIN (inst
))
4756 tree instance
= TREE_VALUE (inst
);
4757 if (TYPE_P (instance
)
4758 ? (COMPLETE_TYPE_P (instance
)
4759 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance
))
4760 : DECL_TEMPLATE_INSTANTIATION (instance
))
4762 tree spec
= most_specialized_partial_spec (instance
, tf_none
);
4763 tree inst_decl
= (DECL_P (instance
)
4764 ? instance
: TYPE_NAME (instance
));
4767 else if (spec
== error_mark_node
)
4768 permerror (input_location
,
4769 "declaration of %qD ambiguates earlier template "
4770 "instantiation for %qD", decl
, inst_decl
);
4771 else if (TREE_VALUE (spec
) == tmpl
)
4772 permerror (input_location
,
4773 "partial specialization of %qD after instantiation "
4774 "of %qD", decl
, inst_decl
);
4781 /* PARM is a template parameter of some form; return the corresponding
4782 TEMPLATE_PARM_INDEX. */
4785 get_template_parm_index (tree parm
)
4787 if (TREE_CODE (parm
) == PARM_DECL
4788 || TREE_CODE (parm
) == CONST_DECL
)
4789 parm
= DECL_INITIAL (parm
);
4790 else if (TREE_CODE (parm
) == TYPE_DECL
4791 || TREE_CODE (parm
) == TEMPLATE_DECL
)
4792 parm
= TREE_TYPE (parm
);
4793 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
4794 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
4795 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
4796 parm
= TEMPLATE_TYPE_PARM_INDEX (parm
);
4797 gcc_assert (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
);
4801 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4802 parameter packs used by the template parameter PARM. */
4805 fixed_parameter_pack_p_1 (tree parm
, struct find_parameter_pack_data
*ppd
)
4807 /* A type parm can't refer to another parm. */
4808 if (TREE_CODE (parm
) == TYPE_DECL
)
4810 else if (TREE_CODE (parm
) == PARM_DECL
)
4812 cp_walk_tree (&TREE_TYPE (parm
), &find_parameter_packs_r
,
4817 gcc_assert (TREE_CODE (parm
) == TEMPLATE_DECL
);
4819 tree vec
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
4820 for (int i
= 0; i
< TREE_VEC_LENGTH (vec
); ++i
)
4821 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec
, i
)), ppd
);
4824 /* PARM is a template parameter pack. Return any parameter packs used in
4825 its type or the type of any of its template parameters. If there are
4826 any such packs, it will be instantiated into a fixed template parameter
4827 list by partial instantiation rather than be fully deduced. */
4830 fixed_parameter_pack_p (tree parm
)
4832 /* This can only be true in a member template. */
4833 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm
)) < 2)
4835 /* This can only be true for a parameter pack. */
4836 if (!template_parameter_pack_p (parm
))
4838 /* A type parm can't refer to another parm. */
4839 if (TREE_CODE (parm
) == TYPE_DECL
)
4842 tree parameter_packs
= NULL_TREE
;
4843 struct find_parameter_pack_data ppd
;
4844 ppd
.parameter_packs
= ¶meter_packs
;
4845 ppd
.visited
= new hash_set
<tree
>;
4846 ppd
.type_pack_expansion_p
= false;
4848 fixed_parameter_pack_p_1 (parm
, &ppd
);
4851 return parameter_packs
;
4854 /* Check that a template declaration's use of default arguments and
4855 parameter packs is not invalid. Here, PARMS are the template
4856 parameters. IS_PRIMARY is true if DECL is the thing declared by
4857 a primary template. IS_PARTIAL is true if DECL is a partial
4860 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4861 declaration (but not a definition); 1 indicates a declaration, 2
4862 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4863 emitted for extraneous default arguments.
4865 Returns TRUE if there were no errors found, FALSE otherwise. */
4868 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4869 bool is_partial
, int is_friend_decl
)
4872 int last_level_to_check
;
4874 bool no_errors
= true;
4878 A default template-argument shall not be specified in a
4879 function template declaration or a function template definition, nor
4880 in the template-parameter-list of the definition of a member of a
4883 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
4884 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (decl
)))
4885 /* You can't have a function template declaration in a local
4886 scope, nor you can you define a member of a class template in a
4890 if ((TREE_CODE (decl
) == TYPE_DECL
4892 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4893 || (TREE_CODE (decl
) == FUNCTION_DECL
4894 && LAMBDA_FUNCTION_P (decl
)))
4895 /* A lambda doesn't have an explicit declaration; don't complain
4896 about the parms of the enclosing class. */
4899 if (current_class_type
4900 && !TYPE_BEING_DEFINED (current_class_type
)
4901 && DECL_LANG_SPECIFIC (decl
)
4902 && DECL_DECLARES_FUNCTION_P (decl
)
4903 /* If this is either a friend defined in the scope of the class
4904 or a member function. */
4905 && (DECL_FUNCTION_MEMBER_P (decl
)
4906 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4907 : DECL_FRIEND_CONTEXT (decl
)
4908 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4910 /* And, if it was a member function, it really was defined in
4911 the scope of the class. */
4912 && (!DECL_FUNCTION_MEMBER_P (decl
)
4913 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4914 /* We already checked these parameters when the template was
4915 declared, so there's no need to do it again now. This function
4916 was defined in class scope, but we're processing its body now
4917 that the class is complete. */
4920 /* Core issue 226 (C++0x only): the following only applies to class
4923 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4927 If a template-parameter has a default template-argument, all
4928 subsequent template-parameters shall have a default
4929 template-argument supplied. */
4930 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4932 tree inner_parms
= TREE_VALUE (parm_level
);
4933 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4934 int seen_def_arg_p
= 0;
4937 for (i
= 0; i
< ntparms
; ++i
)
4939 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4941 if (parm
== error_mark_node
)
4944 if (TREE_PURPOSE (parm
))
4946 else if (seen_def_arg_p
4947 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4949 error ("no default argument for %qD", TREE_VALUE (parm
));
4950 /* For better subsequent error-recovery, we indicate that
4951 there should have been a default argument. */
4952 TREE_PURPOSE (parm
) = error_mark_node
;
4955 else if (!is_partial
4957 /* Don't complain about an enclosing partial
4959 && parm_level
== parms
4960 && TREE_CODE (decl
) == TYPE_DECL
4962 && template_parameter_pack_p (TREE_VALUE (parm
))
4963 /* A fixed parameter pack will be partially
4964 instantiated into a fixed length list. */
4965 && !fixed_parameter_pack_p (TREE_VALUE (parm
)))
4967 /* A primary class template can only have one
4968 parameter pack, at the end of the template
4971 error ("parameter pack %q+D must be at the end of the"
4972 " template parameter list", TREE_VALUE (parm
));
4974 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4982 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4986 /* For an ordinary class template, default template arguments are
4987 allowed at the innermost level, e.g.:
4988 template <class T = int>
4990 but, in a partial specialization, they're not allowed even
4991 there, as we have in [temp.class.spec]:
4993 The template parameter list of a specialization shall not
4994 contain default template argument values.
4996 So, for a partial specialization, or for a function template
4997 (in C++98/C++03), we look at all of them. */
5000 /* But, for a primary class template that is not a partial
5001 specialization we look at all template parameters except the
5003 parms
= TREE_CHAIN (parms
);
5005 /* Figure out what error message to issue. */
5006 if (is_friend_decl
== 2)
5007 msg
= G_("default template arguments may not be used in function template "
5008 "friend re-declaration");
5009 else if (is_friend_decl
)
5010 msg
= G_("default template arguments may not be used in function template "
5011 "friend declarations");
5012 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
5013 msg
= G_("default template arguments may not be used in function templates "
5014 "without -std=c++11 or -std=gnu++11");
5015 else if (is_partial
)
5016 msg
= G_("default template arguments may not be used in "
5017 "partial specializations");
5018 else if (current_class_type
&& CLASSTYPE_IS_TEMPLATE (current_class_type
))
5019 msg
= G_("default argument for template parameter for class enclosing %qD");
5021 /* Per [temp.param]/9, "A default template-argument shall not be
5022 specified in the template-parameter-lists of the definition of
5023 a member of a class template that appears outside of the member's
5024 class.", thus if we aren't handling a member of a class template
5025 there is no need to examine the parameters. */
5028 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
5029 /* If we're inside a class definition, there's no need to
5030 examine the parameters to the class itself. On the one
5031 hand, they will be checked when the class is defined, and,
5032 on the other, default arguments are valid in things like:
5033 template <class T = double>
5034 struct S { template <class U> void f(U); };
5035 Here the default argument for `S' has no bearing on the
5036 declaration of `f'. */
5037 last_level_to_check
= template_class_depth (current_class_type
) + 1;
5039 /* Check everything. */
5040 last_level_to_check
= 0;
5042 for (parm_level
= parms
;
5043 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
5044 parm_level
= TREE_CHAIN (parm_level
))
5046 tree inner_parms
= TREE_VALUE (parm_level
);
5050 ntparms
= TREE_VEC_LENGTH (inner_parms
);
5051 for (i
= 0; i
< ntparms
; ++i
)
5053 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
5056 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
5061 if (is_friend_decl
== 2)
5068 /* Clear out the default argument so that we are not
5070 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
5074 /* At this point, if we're still interested in issuing messages,
5075 they must apply to classes surrounding the object declared. */
5077 msg
= G_("default argument for template parameter for class "
5084 /* Worker for push_template_decl_real, called via
5085 for_each_template_parm. DATA is really an int, indicating the
5086 level of the parameters we are interested in. If T is a template
5087 parameter of that level, return nonzero. */
5090 template_parm_this_level_p (tree t
, void* data
)
5092 int this_level
= *(int *)data
;
5095 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5096 level
= TEMPLATE_PARM_LEVEL (t
);
5098 level
= TEMPLATE_TYPE_LEVEL (t
);
5099 return level
== this_level
;
5102 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5103 DATA is really an int, indicating the innermost outer level of parameters.
5104 If T is a template parameter of that level or further out, return
5108 template_parm_outer_level (tree t
, void *data
)
5110 int this_level
= *(int *)data
;
5113 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
5114 level
= TEMPLATE_PARM_LEVEL (t
);
5116 level
= TEMPLATE_TYPE_LEVEL (t
);
5117 return level
<= this_level
;
5120 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5121 parameters given by current_template_args, or reuses a
5122 previously existing one, if appropriate. Returns the DECL, or an
5123 equivalent one, if it is replaced via a call to duplicate_decls.
5125 If IS_FRIEND is true, DECL is a friend declaration. */
5128 push_template_decl_real (tree decl
, bool is_friend
)
5136 int new_template_p
= 0;
5137 /* True if the template is a member template, in the sense of
5139 bool member_template_p
= false;
5141 if (decl
== error_mark_node
|| !current_template_parms
)
5142 return error_mark_node
;
5144 /* See if this is a partial specialization. */
5145 is_partial
= ((DECL_IMPLICIT_TYPEDEF_P (decl
)
5146 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
5147 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
5149 && DECL_LANG_SPECIFIC (decl
)
5150 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5151 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl
))));
5153 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
5157 /* For a friend, we want the context of the friend function, not
5158 the type of which it is a friend. */
5159 ctx
= CP_DECL_CONTEXT (decl
);
5160 else if (CP_DECL_CONTEXT (decl
)
5161 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
5162 /* In the case of a virtual function, we want the class in which
5164 ctx
= CP_DECL_CONTEXT (decl
);
5166 /* Otherwise, if we're currently defining some class, the DECL
5167 is assumed to be a member of the class. */
5168 ctx
= current_scope ();
5170 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
5173 if (!DECL_CONTEXT (decl
))
5174 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
5176 /* See if this is a primary template. */
5177 if (is_friend
&& ctx
5178 && uses_template_parms_level (ctx
, processing_template_decl
))
5179 /* A friend template that specifies a class context, i.e.
5180 template <typename T> friend void A<T>::f();
5183 else if (TREE_CODE (decl
) == TYPE_DECL
5184 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5187 is_primary
= template_parm_scope_p ();
5191 warning (OPT_Wtemplates
, "template %qD declared", decl
);
5193 if (DECL_CLASS_SCOPE_P (decl
))
5194 member_template_p
= true;
5195 if (TREE_CODE (decl
) == TYPE_DECL
5196 && anon_aggrname_p (DECL_NAME (decl
)))
5198 error ("template class without a name");
5199 return error_mark_node
;
5201 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
5203 if (member_template_p
)
5205 if (DECL_OVERRIDE_P (decl
) || DECL_FINAL_P (decl
))
5206 error ("member template %qD may not have virt-specifiers", decl
);
5208 if (DECL_DESTRUCTOR_P (decl
))
5212 A destructor shall not be a member template. */
5213 error ("destructor %qD declared as member template", decl
);
5214 return error_mark_node
;
5216 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
5217 && (!prototype_p (TREE_TYPE (decl
))
5218 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
5219 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
5220 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
5221 == void_list_node
)))
5223 /* [basic.stc.dynamic.allocation]
5225 An allocation function can be a function
5226 template. ... Template allocation functions shall
5227 have two or more parameters. */
5228 error ("invalid template declaration of %qD", decl
);
5229 return error_mark_node
;
5232 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5233 && CLASS_TYPE_P (TREE_TYPE (decl
)))
5235 else if (TREE_CODE (decl
) == TYPE_DECL
5236 && TYPE_DECL_ALIAS_P (decl
))
5237 /* alias-declaration */
5238 gcc_assert (!DECL_ARTIFICIAL (decl
));
5239 else if (VAR_P (decl
))
5240 /* C++14 variable template. */;
5243 error ("template declaration of %q#D", decl
);
5244 return error_mark_node
;
5248 /* Check to see that the rules regarding the use of default
5249 arguments are not being violated. */
5250 check_default_tmpl_args (decl
, current_template_parms
,
5251 is_primary
, is_partial
, /*is_friend_decl=*/0);
5253 /* Ensure that there are no parameter packs in the type of this
5254 declaration that have not been expanded. */
5255 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5257 /* Check each of the arguments individually to see if there are
5258 any bare parameter packs. */
5259 tree type
= TREE_TYPE (decl
);
5260 tree arg
= DECL_ARGUMENTS (decl
);
5261 tree argtype
= TYPE_ARG_TYPES (type
);
5263 while (arg
&& argtype
)
5265 if (!DECL_PACK_P (arg
)
5266 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
5268 /* This is a PARM_DECL that contains unexpanded parameter
5269 packs. We have already complained about this in the
5270 check_for_bare_parameter_packs call, so just replace
5271 these types with ERROR_MARK_NODE. */
5272 TREE_TYPE (arg
) = error_mark_node
;
5273 TREE_VALUE (argtype
) = error_mark_node
;
5276 arg
= DECL_CHAIN (arg
);
5277 argtype
= TREE_CHAIN (argtype
);
5280 /* Check for bare parameter packs in the return type and the
5281 exception specifiers. */
5282 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
5283 /* Errors were already issued, set return type to int
5284 as the frontend doesn't expect error_mark_node as
5286 TREE_TYPE (type
) = integer_type_node
;
5287 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
5288 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
5290 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
5291 && TYPE_DECL_ALIAS_P (decl
))
5292 ? DECL_ORIGINAL_TYPE (decl
)
5293 : TREE_TYPE (decl
)))
5295 TREE_TYPE (decl
) = error_mark_node
;
5296 return error_mark_node
;
5300 return process_partial_specialization (decl
);
5302 args
= current_template_args ();
5305 || TREE_CODE (ctx
) == FUNCTION_DECL
5306 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
5307 || (TREE_CODE (decl
) == TYPE_DECL
5308 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
5309 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
5311 if (DECL_LANG_SPECIFIC (decl
)
5312 && DECL_TEMPLATE_INFO (decl
)
5313 && DECL_TI_TEMPLATE (decl
))
5314 tmpl
= DECL_TI_TEMPLATE (decl
);
5315 /* If DECL is a TYPE_DECL for a class-template, then there won't
5316 be DECL_LANG_SPECIFIC. The information equivalent to
5317 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5318 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
5319 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
5320 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
5322 /* Since a template declaration already existed for this
5323 class-type, we must be redeclaring it here. Make sure
5324 that the redeclaration is valid. */
5325 redeclare_class_template (TREE_TYPE (decl
),
5326 current_template_parms
,
5327 current_template_constraints ());
5328 /* We don't need to create a new TEMPLATE_DECL; just use the
5329 one we already had. */
5330 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
5334 tmpl
= build_template_decl (decl
, current_template_parms
,
5338 if (DECL_LANG_SPECIFIC (decl
)
5339 && DECL_TEMPLATE_SPECIALIZATION (decl
))
5341 /* A specialization of a member template of a template
5343 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
5344 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
5345 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
5351 tree a
, t
, current
, parms
;
5353 tree tinfo
= get_template_info (decl
);
5357 error ("template definition of non-template %q#D", decl
);
5358 return error_mark_node
;
5361 tmpl
= TI_TEMPLATE (tinfo
);
5363 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
5364 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5365 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5366 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5370 /* The declaration is a specialization of a member
5371 template, declared outside the class. Therefore, the
5372 innermost template arguments will be NULL, so we
5373 replace them with the arguments determined by the
5374 earlier call to check_explicit_specialization. */
5375 args
= DECL_TI_ARGS (decl
);
5378 = build_template_decl (decl
, current_template_parms
,
5380 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5381 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5382 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5383 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5384 DECL_TEMPLATE_INFO (new_tmpl
)
5385 = build_template_info (tmpl
, args
);
5387 register_specialization (new_tmpl
,
5388 most_general_template (tmpl
),
5394 /* Make sure the template headers we got make sense. */
5396 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5397 i
= TMPL_PARMS_DEPTH (parms
);
5398 if (TMPL_ARGS_DEPTH (args
) != i
)
5400 error ("expected %d levels of template parms for %q#D, got %d",
5401 i
, decl
, TMPL_ARGS_DEPTH (args
));
5402 DECL_INTERFACE_KNOWN (decl
) = 1;
5403 return error_mark_node
;
5406 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5408 a
= TMPL_ARGS_LEVEL (args
, i
);
5409 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5411 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5413 if (current
== decl
)
5414 error ("got %d template parameters for %q#D",
5415 TREE_VEC_LENGTH (a
), decl
);
5417 error ("got %d template parameters for %q#T",
5418 TREE_VEC_LENGTH (a
), current
);
5419 error (" but %d required", TREE_VEC_LENGTH (t
));
5420 /* Avoid crash in import_export_decl. */
5421 DECL_INTERFACE_KNOWN (decl
) = 1;
5422 return error_mark_node
;
5425 if (current
== decl
)
5427 else if (current
== NULL_TREE
)
5428 /* Can happen in erroneous input. */
5431 current
= get_containing_scope (current
);
5434 /* Check that the parms are used in the appropriate qualifying scopes
5435 in the declarator. */
5436 if (!comp_template_args
5438 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5441 template arguments to %qD do not match original template %qD",
5442 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5443 if (!uses_template_parms (TI_ARGS (tinfo
)))
5444 inform (input_location
, "use template<> for an explicit specialization");
5445 /* Avoid crash in import_export_decl. */
5446 DECL_INTERFACE_KNOWN (decl
) = 1;
5447 return error_mark_node
;
5451 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5452 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5454 /* Push template declarations for global functions and types. Note
5455 that we do not try to push a global template friend declared in a
5456 template class; such a thing may well depend on the template
5457 parameters of the class. */
5458 if (new_template_p
&& !ctx
5459 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5461 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5462 if (tmpl
== error_mark_node
)
5463 return error_mark_node
;
5465 /* Hide template friend classes that haven't been declared yet. */
5466 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5468 DECL_ANTICIPATED (tmpl
) = 1;
5469 DECL_FRIEND_P (tmpl
) = 1;
5475 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5478 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5479 if (DECL_CONV_FN_P (tmpl
))
5481 int depth
= TMPL_PARMS_DEPTH (parms
);
5483 /* It is a conversion operator. See if the type converted to
5484 depends on innermost template operands. */
5486 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5488 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5491 /* Give template template parms a DECL_CONTEXT of the template
5492 for which they are a parameter. */
5493 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5494 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5496 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5497 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5498 DECL_CONTEXT (parm
) = tmpl
;
5501 if (TREE_CODE (decl
) == TYPE_DECL
5502 && TYPE_DECL_ALIAS_P (decl
)
5503 && complex_alias_template_p (tmpl
))
5504 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl
) = true;
5507 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5508 back to its most general template. If TMPL is a specialization,
5509 ARGS may only have the innermost set of arguments. Add the missing
5510 argument levels if necessary. */
5511 if (DECL_TEMPLATE_INFO (tmpl
))
5512 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5514 info
= build_template_info (tmpl
, args
);
5516 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5517 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5520 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
5521 retrofit_lang_decl (decl
);
5522 if (DECL_LANG_SPECIFIC (decl
))
5523 DECL_TEMPLATE_INFO (decl
) = info
;
5526 if (flag_implicit_templates
5528 && TREE_PUBLIC (decl
)
5529 && VAR_OR_FUNCTION_DECL_P (decl
))
5530 /* Set DECL_COMDAT on template instantiations; if we force
5531 them to be emitted by explicit instantiation or -frepo,
5532 mark_needed will tell cgraph to do the right thing. */
5533 DECL_COMDAT (decl
) = true;
5535 return DECL_TEMPLATE_RESULT (tmpl
);
5539 push_template_decl (tree decl
)
5541 return push_template_decl_real (decl
, false);
5544 /* FN is an inheriting constructor that inherits from the constructor
5545 template INHERITED; turn FN into a constructor template with a matching
5549 add_inherited_template_parms (tree fn
, tree inherited
)
5552 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
5553 inner_parms
= copy_node (inner_parms
);
5555 = tree_cons (size_int (processing_template_decl
+ 1),
5556 inner_parms
, current_template_parms
);
5557 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
5558 tree args
= template_parms_to_args (parms
);
5559 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
5560 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
5561 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
5562 DECL_ARTIFICIAL (tmpl
) = true;
5563 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5567 /* Called when a class template TYPE is redeclared with the indicated
5568 template PARMS, e.g.:
5570 template <class T> struct S;
5571 template <class T> struct S {}; */
5574 redeclare_class_template (tree type
, tree parms
, tree cons
)
5580 if (!TYPE_TEMPLATE_INFO (type
))
5582 error ("%qT is not a template type", type
);
5586 tmpl
= TYPE_TI_TEMPLATE (type
);
5587 if (!PRIMARY_TEMPLATE_P (tmpl
))
5588 /* The type is nested in some template class. Nothing to worry
5589 about here; there are no new template parameters for the nested
5595 error ("template specifiers not specified in declaration of %qD",
5600 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5601 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5603 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5605 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5606 "redeclared with %d template parameter",
5607 "redeclared with %d template parameters",
5608 TREE_VEC_LENGTH (parms
));
5609 inform_n (DECL_SOURCE_LOCATION (tmpl
), TREE_VEC_LENGTH (tmpl_parms
),
5610 "previous declaration %qD used %d template parameter",
5611 "previous declaration %qD used %d template parameters",
5612 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5616 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5623 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5624 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5627 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5628 if (error_operand_p (tmpl_parm
))
5631 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5632 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5633 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5635 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5637 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5638 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5639 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5640 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5641 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5642 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5643 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5644 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5645 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5647 error ("template parameter %q+#D", tmpl_parm
);
5648 error ("redeclared here as %q#D", parm
);
5652 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5654 /* We have in [temp.param]:
5656 A template-parameter may not be given default arguments
5657 by two different declarations in the same scope. */
5658 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5659 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5660 "original definition appeared here");
5664 if (parm_default
!= NULL_TREE
)
5665 /* Update the previous template parameters (which are the ones
5666 that will really count) with the new default value. */
5667 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5668 else if (tmpl_default
!= NULL_TREE
)
5669 /* Update the new parameters, too; they'll be used as the
5670 parameters for any members. */
5671 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5673 /* Give each template template parm in this redeclaration a
5674 DECL_CONTEXT of the template for which they are a parameter. */
5675 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5677 gcc_assert (DECL_CONTEXT (parm
) == NULL_TREE
);
5678 DECL_CONTEXT (parm
) = tmpl
;
5682 // Cannot redeclare a class template with a different set of constraints.
5683 if (!equivalent_constraints (get_constraints (tmpl
), cons
))
5685 error_at (input_location
, "redeclaration %q#D with different "
5686 "constraints", tmpl
);
5687 inform (DECL_SOURCE_LOCATION (tmpl
),
5688 "original declaration appeared here");
5694 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5695 to be used when the caller has already checked
5696 (processing_template_decl
5697 && !instantiation_dependent_expression_p (expr)
5698 && potential_constant_expression (expr))
5699 and cleared processing_template_decl. */
5702 instantiate_non_dependent_expr_internal (tree expr
, tsubst_flags_t complain
)
5704 return tsubst_copy_and_build (expr
,
5707 /*in_decl=*/NULL_TREE
,
5708 /*function_p=*/false,
5709 /*integral_constant_expression_p=*/true);
5712 /* Simplify EXPR if it is a non-dependent expression. Returns the
5713 (possibly simplified) expression. */
5716 instantiate_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5718 if (expr
== NULL_TREE
)
5721 /* If we're in a template, but EXPR isn't value dependent, simplify
5722 it. We're supposed to treat:
5724 template <typename T> void f(T[1 + 1]);
5725 template <typename T> void f(T[2]);
5727 as two declarations of the same function, for example. */
5728 if (processing_template_decl
5729 && potential_nondependent_constant_expression (expr
))
5731 processing_template_decl_sentinel s
;
5732 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
5738 instantiate_non_dependent_expr (tree expr
)
5740 return instantiate_non_dependent_expr_sfinae (expr
, tf_error
);
5743 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5744 an uninstantiated expression. */
5747 instantiate_non_dependent_or_null (tree expr
)
5749 if (expr
== NULL_TREE
)
5751 if (processing_template_decl
)
5753 if (!potential_nondependent_constant_expression (expr
))
5757 processing_template_decl_sentinel s
;
5758 expr
= instantiate_non_dependent_expr_internal (expr
, tf_error
);
5764 /* True iff T is a specialization of a variable template. */
5767 variable_template_specialization_p (tree t
)
5769 if (!VAR_P (t
) || !DECL_LANG_SPECIFIC (t
) || !DECL_TEMPLATE_INFO (t
))
5771 tree tmpl
= DECL_TI_TEMPLATE (t
);
5772 return variable_template_p (tmpl
);
5775 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5776 template declaration, or a TYPE_DECL for an alias declaration. */
5779 alias_type_or_template_p (tree t
)
5783 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5786 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5787 || DECL_ALIAS_TEMPLATE_P (t
));
5790 /* Return TRUE iff T is a specialization of an alias template. */
5793 alias_template_specialization_p (const_tree t
)
5795 /* It's an alias template specialization if it's an alias and its
5796 TYPE_NAME is a specialization of a primary template. */
5797 if (TYPE_ALIAS_P (t
))
5798 if (tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
))
5799 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
));
5804 /* An alias template is complex from a SFINAE perspective if a template-id
5805 using that alias can be ill-formed when the expansion is not, as with
5806 the void_t template. We determine this by checking whether the
5807 expansion for the alias template uses all its template parameters. */
5809 struct uses_all_template_parms_data
5816 uses_all_template_parms_r (tree t
, void *data_
)
5818 struct uses_all_template_parms_data
&data
5819 = *(struct uses_all_template_parms_data
*)data_
;
5820 tree idx
= get_template_parm_index (t
);
5822 if (TEMPLATE_PARM_LEVEL (idx
) == data
.level
)
5823 data
.seen
[TEMPLATE_PARM_IDX (idx
)] = true;
5828 complex_alias_template_p (const_tree tmpl
)
5830 struct uses_all_template_parms_data data
;
5831 tree pat
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5832 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5833 data
.level
= TMPL_PARMS_DEPTH (parms
);
5834 int len
= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms
));
5835 data
.seen
= XALLOCAVEC (bool, len
);
5836 for (int i
= 0; i
< len
; ++i
)
5837 data
.seen
[i
] = false;
5839 for_each_template_parm (pat
, uses_all_template_parms_r
, &data
, NULL
, true);
5840 for (int i
= 0; i
< len
; ++i
)
5846 /* Return TRUE iff T is a specialization of a complex alias template with
5847 dependent template-arguments. */
5850 dependent_alias_template_spec_p (const_tree t
)
5852 if (!alias_template_specialization_p (t
))
5855 tree tinfo
= TYPE_ALIAS_TEMPLATE_INFO (t
);
5856 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo
)))
5859 tree args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
));
5860 if (!any_dependent_template_arguments_p (args
))
5866 /* Return the number of innermost template parameters in TMPL. */
5869 num_innermost_template_parms (tree tmpl
)
5871 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
5872 return TREE_VEC_LENGTH (parms
);
5875 /* Return either TMPL or another template that it is equivalent to under DR
5876 1286: An alias that just changes the name of a template is equivalent to
5877 the other template. */
5880 get_underlying_template (tree tmpl
)
5882 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5883 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5885 /* Determine if the alias is equivalent to an underlying template. */
5886 tree orig_type
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5887 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type
);
5891 tree underlying
= TI_TEMPLATE (tinfo
);
5892 if (!PRIMARY_TEMPLATE_P (underlying
)
5893 || (num_innermost_template_parms (tmpl
)
5894 != num_innermost_template_parms (underlying
)))
5897 tree alias_args
= INNERMOST_TEMPLATE_ARGS
5898 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl
)));
5899 if (!comp_template_args (TI_ARGS (tinfo
), alias_args
))
5902 /* Alias is equivalent. Strip it and repeat. */
5909 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5910 must be a reference-to-function or a pointer-to-function type, as specified
5911 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5912 and check that the resulting function has external linkage. */
5915 convert_nontype_argument_function (tree type
, tree expr
,
5916 tsubst_flags_t complain
)
5920 linkage_kind linkage
;
5922 fn
= instantiate_type (type
, fns
, tf_none
);
5923 if (fn
== error_mark_node
)
5924 return error_mark_node
;
5926 fn_no_ptr
= strip_fnptr_conv (fn
);
5927 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5928 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5929 if (BASELINK_P (fn_no_ptr
))
5930 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5932 /* [temp.arg.nontype]/1
5934 A template-argument for a non-type, non-template template-parameter
5937 -- the address of an object or function with external [C++11: or
5938 internal] linkage. */
5940 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5942 if (complain
& tf_error
)
5944 error ("%qE is not a valid template argument for type %qT",
5946 if (TYPE_PTR_P (type
))
5947 inform (input_location
, "it must be the address of a function "
5948 "with external linkage");
5950 inform (input_location
, "it must be the name of a function with "
5951 "external linkage");
5956 linkage
= decl_linkage (fn_no_ptr
);
5957 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5959 if (complain
& tf_error
)
5961 if (cxx_dialect
>= cxx11
)
5962 error ("%qE is not a valid template argument for type %qT "
5963 "because %qD has no linkage",
5964 expr
, type
, fn_no_ptr
);
5966 error ("%qE is not a valid template argument for type %qT "
5967 "because %qD does not have external linkage",
5968 expr
, type
, fn_no_ptr
);
5973 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5974 fn
= build_address (fn
);
5975 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (fn
)))
5976 fn
= build_nop (type
, fn
);
5981 /* Subroutine of convert_nontype_argument.
5982 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5983 Emit an error otherwise. */
5986 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5987 tsubst_flags_t complain
)
5990 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5992 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5994 if (processing_template_decl
5995 && TREE_CODE (expr
) == ADDR_EXPR
5996 && TREE_CODE (TREE_OPERAND (expr
, 0)) == OFFSET_REF
)
5998 if (complain
& tf_error
)
6000 error ("%qE is not a valid template argument for type %qT",
6002 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6007 /* Returns TRUE iff the address of OP is value-dependent.
6009 14.6.2.4 [temp.dep.temp]:
6010 A non-integral non-type template-argument is dependent if its type is
6011 dependent or it has either of the following forms
6014 and contains a nested-name-specifier which specifies a class-name that
6015 names a dependent type.
6017 We generalize this to just say that the address of a member of a
6018 dependent class is value-dependent; the above doesn't cover the
6019 address of a static data member named with an unqualified-id. */
6022 has_value_dependent_address (tree op
)
6024 /* We could use get_inner_reference here, but there's no need;
6025 this is only relevant for template non-type arguments, which
6026 can only be expressed as &id-expression. */
6029 tree ctx
= CP_DECL_CONTEXT (op
);
6030 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
6037 /* The next set of functions are used for providing helpful explanatory
6038 diagnostics for failed overload resolution. Their messages should be
6039 indented by two spaces for consistency with the messages in
6043 unify_success (bool /*explain_p*/)
6049 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
6052 inform (input_location
,
6053 " couldn't deduce template parameter %qD", parm
);
6058 unify_invalid (bool /*explain_p*/)
6064 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
6067 inform (input_location
,
6068 " types %qT and %qT have incompatible cv-qualifiers",
6074 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
6077 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
6082 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
6085 inform (input_location
,
6086 " template parameter %qD is not a parameter pack, but "
6093 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
6096 inform (input_location
,
6097 " template argument %qE does not match "
6098 "pointer-to-member constant %qE",
6104 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
6107 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
6112 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
6115 inform (input_location
,
6116 " inconsistent parameter pack deduction with %qT and %qT",
6122 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
6127 inform (input_location
,
6128 " deduced conflicting types for parameter %qT (%qT and %qT)",
6129 parm
, first
, second
);
6131 inform (input_location
,
6132 " deduced conflicting values for non-type parameter "
6133 "%qE (%qE and %qE)", parm
, first
, second
);
6139 unify_vla_arg (bool explain_p
, tree arg
)
6142 inform (input_location
,
6143 " variable-sized array type %qT is not "
6144 "a valid template argument",
6150 unify_method_type_error (bool explain_p
, tree arg
)
6153 inform (input_location
,
6154 " member function type %qT is not a valid template argument",
6160 unify_arity (bool explain_p
, int have
, int wanted
, bool least_p
= false)
6165 inform_n (input_location
, wanted
,
6166 " candidate expects at least %d argument, %d provided",
6167 " candidate expects at least %d arguments, %d provided",
6170 inform_n (input_location
, wanted
,
6171 " candidate expects %d argument, %d provided",
6172 " candidate expects %d arguments, %d provided",
6179 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
6181 return unify_arity (explain_p
, have
, wanted
);
6185 unify_too_few_arguments (bool explain_p
, int have
, int wanted
,
6186 bool least_p
= false)
6188 return unify_arity (explain_p
, have
, wanted
, least_p
);
6192 unify_arg_conversion (bool explain_p
, tree to_type
,
6193 tree from_type
, tree arg
)
6196 inform (EXPR_LOC_OR_LOC (arg
, input_location
),
6197 " cannot convert %qE (type %qT) to type %qT",
6198 arg
, from_type
, to_type
);
6203 unify_no_common_base (bool explain_p
, enum template_base_result r
,
6204 tree parm
, tree arg
)
6209 case tbr_ambiguous_baseclass
:
6210 inform (input_location
, " %qT is an ambiguous base class of %qT",
6214 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
6221 unify_inconsistent_template_template_parameters (bool explain_p
)
6224 inform (input_location
,
6225 " template parameters of a template template argument are "
6226 "inconsistent with other deduced template arguments");
6231 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
6234 inform (input_location
,
6235 " can't deduce a template for %qT from non-template type %qT",
6241 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
6244 inform (input_location
,
6245 " template argument %qE does not match %qE", arg
, parm
);
6250 unify_overload_resolution_failure (bool explain_p
, tree arg
)
6253 inform (input_location
,
6254 " could not resolve address from overloaded function %qE",
6259 /* Attempt to convert the non-type template parameter EXPR to the
6260 indicated TYPE. If the conversion is successful, return the
6261 converted value. If the conversion is unsuccessful, return
6262 NULL_TREE if we issued an error message, or error_mark_node if we
6263 did not. We issue error messages for out-and-out bad template
6264 parameters, but not simply because the conversion failed, since we
6265 might be just trying to do argument deduction. Both TYPE and EXPR
6266 must be non-dependent.
6268 The conversion follows the special rules described in
6269 [temp.arg.nontype], and it is much more strict than an implicit
6272 This function is called twice for each template argument (see
6273 lookup_template_class for a more accurate description of this
6274 problem). This means that we need to handle expressions which
6275 are not valid in a C++ source, but can be created from the
6276 first call (for instance, casts to perform conversions). These
6277 hacks can go away after we fix the double coercion problem. */
6280 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
6284 /* Detect immediately string literals as invalid non-type argument.
6285 This special-case is not needed for correctness (we would easily
6286 catch this later), but only to provide better diagnostic for this
6287 common user mistake. As suggested by DR 100, we do not mention
6288 linkage issues in the diagnostic as this is not the point. */
6289 /* FIXME we're making this OK. */
6290 if (TREE_CODE (expr
) == STRING_CST
)
6292 if (complain
& tf_error
)
6293 error ("%qE is not a valid template argument for type %qT "
6294 "because string literals can never be used in this context",
6299 /* Add the ADDR_EXPR now for the benefit of
6300 value_dependent_expression_p. */
6301 if (TYPE_PTROBV_P (type
)
6302 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
6304 expr
= decay_conversion (expr
, complain
);
6305 if (expr
== error_mark_node
)
6306 return error_mark_node
;
6309 /* If we are in a template, EXPR may be non-dependent, but still
6310 have a syntactic, rather than semantic, form. For example, EXPR
6311 might be a SCOPE_REF, rather than the VAR_DECL to which the
6312 SCOPE_REF refers. Preserving the qualifying scope is necessary
6313 so that access checking can be performed when the template is
6314 instantiated -- but here we need the resolved form so that we can
6315 convert the argument. */
6316 bool non_dep
= false;
6317 if (TYPE_REF_OBJ_P (type
)
6318 && has_value_dependent_address (expr
))
6319 /* If we want the address and it's value-dependent, don't fold. */;
6320 else if (processing_template_decl
6321 && potential_nondependent_constant_expression (expr
))
6323 if (error_operand_p (expr
))
6324 return error_mark_node
;
6325 expr_type
= TREE_TYPE (expr
);
6326 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6327 expr
= mark_lvalue_use (expr
);
6329 expr
= mark_rvalue_use (expr
);
6331 /* If the argument is non-dependent, perform any conversions in
6332 non-dependent context as well. */
6333 processing_template_decl_sentinel
s (non_dep
);
6335 expr
= instantiate_non_dependent_expr_internal (expr
, complain
);
6337 if (value_dependent_expression_p (expr
))
6338 expr
= canonicalize_expr_argument (expr
, complain
);
6340 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6341 to a non-type argument of "nullptr". */
6342 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
6343 expr
= fold_simple (convert (type
, expr
));
6345 /* In C++11, integral or enumeration non-type template arguments can be
6346 arbitrary constant expressions. Pointer and pointer to
6347 member arguments can be general constant expressions that evaluate
6348 to a null value, but otherwise still need to be of a specific form. */
6349 if (cxx_dialect
>= cxx11
)
6351 if (TREE_CODE (expr
) == PTRMEM_CST
)
6352 /* A PTRMEM_CST is already constant, and a valid template
6353 argument for a parameter of pointer to member type, we just want
6354 to leave it in that form rather than lower it to a
6356 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6357 expr
= maybe_constant_value (expr
);
6358 else if (cxx_dialect
>= cxx1z
)
6360 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6361 expr
= maybe_constant_value (expr
);
6362 else if (REFERENCE_REF_P (expr
))
6364 expr
= TREE_OPERAND (expr
, 0);
6365 expr
= maybe_constant_value (expr
);
6366 expr
= convert_from_reference (expr
);
6369 else if (TYPE_PTR_OR_PTRMEM_P (type
))
6371 tree folded
= maybe_constant_value (expr
);
6372 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
6373 : null_member_pointer_value_p (folded
))
6378 /* HACK: Due to double coercion, we can get a
6379 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6380 which is the tree that we built on the first call (see
6381 below when coercing to reference to object or to reference to
6382 function). We just strip everything and get to the arg.
6383 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6385 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
6387 tree probe_type
, probe
= expr
;
6388 if (REFERENCE_REF_P (probe
))
6389 probe
= TREE_OPERAND (probe
, 0);
6390 probe_type
= TREE_TYPE (probe
);
6391 if (TREE_CODE (probe
) == NOP_EXPR
)
6393 /* ??? Maybe we could use convert_from_reference here, but we
6394 would need to relax its constraints because the NOP_EXPR
6395 could actually change the type to something more cv-qualified,
6396 and this is not folded by convert_from_reference. */
6397 tree addr
= TREE_OPERAND (probe
, 0);
6398 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
6399 && TREE_CODE (addr
) == ADDR_EXPR
6400 && TYPE_PTR_P (TREE_TYPE (addr
))
6401 && (same_type_ignoring_top_level_qualifiers_p
6402 (TREE_TYPE (probe_type
),
6403 TREE_TYPE (TREE_TYPE (addr
)))))
6405 expr
= TREE_OPERAND (addr
, 0);
6406 expr_type
= TREE_TYPE (probe_type
);
6411 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6412 parameter is a pointer to object, through decay and
6413 qualification conversion. Let's strip everything. */
6414 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
6418 if (TREE_CODE (probe
) == ADDR_EXPR
6419 && TYPE_PTR_P (TREE_TYPE (probe
)))
6421 /* Skip the ADDR_EXPR only if it is part of the decay for
6422 an array. Otherwise, it is part of the original argument
6423 in the source code. */
6424 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
6425 probe
= TREE_OPERAND (probe
, 0);
6427 expr_type
= TREE_TYPE (expr
);
6431 /* [temp.arg.nontype]/5, bullet 1
6433 For a non-type template-parameter of integral or enumeration type,
6434 integral promotions (_conv.prom_) and integral conversions
6435 (_conv.integral_) are applied. */
6436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6438 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
6439 t
= maybe_constant_value (t
);
6440 if (t
!= error_mark_node
)
6443 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
6444 return error_mark_node
;
6446 /* Notice that there are constant expressions like '4 % 0' which
6447 do not fold into integer constants. */
6448 if (TREE_CODE (expr
) != INTEGER_CST
6449 && !value_dependent_expression_p (expr
))
6451 if (complain
& tf_error
)
6453 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
6454 if (processing_template_decl
6455 && !require_potential_constant_expression (expr
))
6457 expr
= cxx_constant_value (expr
);
6458 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
6459 inform (EXPR_LOC_OR_LOC (expr
, input_location
),
6460 "in template argument for type %qT ", type
);
6461 if (expr
== error_mark_node
)
6463 /* else cxx_constant_value complained but gave us
6464 a real constant, so go ahead. */
6465 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
6471 /* Avoid typedef problems. */
6472 if (TREE_TYPE (expr
) != type
)
6473 expr
= fold_convert (type
, expr
);
6475 /* [temp.arg.nontype]/5, bullet 2
6477 For a non-type template-parameter of type pointer to object,
6478 qualification conversions (_conv.qual_) and the array-to-pointer
6479 conversion (_conv.array_) are applied. */
6480 else if (TYPE_PTROBV_P (type
))
6482 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6484 A template-argument for a non-type, non-template template-parameter
6485 shall be one of: [...]
6487 -- the name of a non-type template-parameter;
6488 -- the address of an object or function with external linkage, [...]
6489 expressed as "& id-expression" where the & is optional if the name
6490 refers to a function or array, or if the corresponding
6491 template-parameter is a reference.
6493 Here, we do not care about functions, as they are invalid anyway
6494 for a parameter of type pointer-to-object. */
6496 if (value_dependent_expression_p (expr
))
6497 /* Non-type template parameters are OK. */
6499 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6500 /* Null pointer values are OK in C++11. */;
6501 else if (TREE_CODE (expr
) != ADDR_EXPR
6502 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
6506 if (complain
& tf_error
)
6507 error ("%qD is not a valid template argument "
6508 "because %qD is a variable, not the address of "
6509 "a variable", expr
, expr
);
6512 if (POINTER_TYPE_P (expr_type
))
6514 if (complain
& tf_error
)
6515 error ("%qE is not a valid template argument for %qT "
6516 "because it is not the address of a variable",
6520 /* Other values, like integer constants, might be valid
6521 non-type arguments of some other type. */
6522 return error_mark_node
;
6528 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
6529 ? TREE_OPERAND (expr
, 0) : expr
);
6532 if (complain
& tf_error
)
6533 error ("%qE is not a valid template argument of type %qT "
6534 "because %qE is not a variable", expr
, type
, decl
);
6537 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
6539 if (complain
& tf_error
)
6540 error ("%qE is not a valid template argument of type %qT "
6541 "because %qD does not have external linkage",
6545 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
6547 if (complain
& tf_error
)
6548 error ("%qE is not a valid template argument of type %qT "
6549 "because %qD has no linkage", expr
, type
, decl
);
6554 expr
= decay_conversion (expr
, complain
);
6555 if (expr
== error_mark_node
)
6556 return error_mark_node
;
6558 expr
= perform_qualification_conversions (type
, expr
);
6559 if (expr
== error_mark_node
)
6560 return error_mark_node
;
6562 /* [temp.arg.nontype]/5, bullet 3
6564 For a non-type template-parameter of type reference to object, no
6565 conversions apply. The type referred to by the reference may be more
6566 cv-qualified than the (otherwise identical) type of the
6567 template-argument. The template-parameter is bound directly to the
6568 template-argument, which must be an lvalue. */
6569 else if (TYPE_REF_OBJ_P (type
))
6571 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
6573 return error_mark_node
;
6575 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
6577 if (complain
& tf_error
)
6578 error ("%qE is not a valid template argument for type %qT "
6579 "because of conflicts in cv-qualification", expr
, type
);
6583 if (!lvalue_p (expr
))
6585 if (complain
& tf_error
)
6586 error ("%qE is not a valid template argument for type %qT "
6587 "because it is not an lvalue", expr
, type
);
6591 /* [temp.arg.nontype]/1
6593 A template-argument for a non-type, non-template template-parameter
6594 shall be one of: [...]
6596 -- the address of an object or function with external linkage. */
6597 if (INDIRECT_REF_P (expr
)
6598 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
6600 expr
= TREE_OPERAND (expr
, 0);
6603 if (complain
& tf_error
)
6604 error ("%q#D is not a valid template argument for type %qT "
6605 "because a reference variable does not have a constant "
6606 "address", expr
, type
);
6611 if (!value_dependent_expression_p (expr
))
6615 if (complain
& tf_error
)
6616 error ("%qE is not a valid template argument for type %qT "
6617 "because it is not an object with linkage",
6622 /* DR 1155 allows internal linkage in C++11 and up. */
6623 linkage_kind linkage
= decl_linkage (expr
);
6624 if (linkage
< (cxx_dialect
>= cxx11
? lk_internal
: lk_external
))
6626 if (complain
& tf_error
)
6627 error ("%qE is not a valid template argument for type %qT "
6628 "because object %qD does not have linkage",
6633 expr
= build_nop (type
, build_address (expr
));
6636 /* [temp.arg.nontype]/5, bullet 4
6638 For a non-type template-parameter of type pointer to function, only
6639 the function-to-pointer conversion (_conv.func_) is applied. If the
6640 template-argument represents a set of overloaded functions (or a
6641 pointer to such), the matching function is selected from the set
6643 else if (TYPE_PTRFN_P (type
))
6645 /* If the argument is a template-id, we might not have enough
6646 context information to decay the pointer. */
6647 if (!type_unknown_p (expr_type
))
6649 expr
= decay_conversion (expr
, complain
);
6650 if (expr
== error_mark_node
)
6651 return error_mark_node
;
6654 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
6655 /* Null pointer values are OK in C++11. */
6656 return perform_qualification_conversions (type
, expr
);
6658 if (!value_dependent_expression_p (expr
))
6659 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6660 if (!expr
|| expr
== error_mark_node
)
6663 /* [temp.arg.nontype]/5, bullet 5
6665 For a non-type template-parameter of type reference to function, no
6666 conversions apply. If the template-argument represents a set of
6667 overloaded functions, the matching function is selected from the set
6669 else if (TYPE_REFFN_P (type
))
6671 if (TREE_CODE (expr
) == ADDR_EXPR
)
6673 if (complain
& tf_error
)
6675 error ("%qE is not a valid template argument for type %qT "
6676 "because it is a pointer", expr
, type
);
6677 inform (input_location
, "try using %qE instead",
6678 TREE_OPERAND (expr
, 0));
6683 if (!value_dependent_expression_p (expr
))
6684 expr
= convert_nontype_argument_function (type
, expr
, complain
);
6685 if (!expr
|| expr
== error_mark_node
)
6688 /* [temp.arg.nontype]/5, bullet 6
6690 For a non-type template-parameter of type pointer to member function,
6691 no conversions apply. If the template-argument represents a set of
6692 overloaded member functions, the matching member function is selected
6693 from the set (_over.over_). */
6694 else if (TYPE_PTRMEMFUNC_P (type
))
6696 expr
= instantiate_type (type
, expr
, tf_none
);
6697 if (expr
== error_mark_node
)
6698 return error_mark_node
;
6700 /* [temp.arg.nontype] bullet 1 says the pointer to member
6701 expression must be a pointer-to-member constant. */
6702 if (!value_dependent_expression_p (expr
)
6703 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6704 return error_mark_node
;
6706 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6707 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6708 if (fnptr_conv_p (type
, TREE_TYPE (expr
)))
6709 expr
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
6711 /* There is no way to disable standard conversions in
6712 resolve_address_of_overloaded_function (called by
6713 instantiate_type). It is possible that the call succeeded by
6714 converting &B::I to &D::I (where B is a base of D), so we need
6715 to reject this conversion here.
6717 Actually, even if there was a way to disable standard conversions,
6718 it would still be better to reject them here so that we can
6719 provide a superior diagnostic. */
6720 if (!same_type_p (TREE_TYPE (expr
), type
))
6722 if (complain
& tf_error
)
6724 error ("%qE is not a valid template argument for type %qT "
6725 "because it is of type %qT", expr
, type
,
6727 /* If we are just one standard conversion off, explain. */
6728 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
6729 inform (input_location
,
6730 "standard conversions are not allowed in this context");
6735 /* [temp.arg.nontype]/5, bullet 7
6737 For a non-type template-parameter of type pointer to data member,
6738 qualification conversions (_conv.qual_) are applied. */
6739 else if (TYPE_PTRDATAMEM_P (type
))
6741 /* [temp.arg.nontype] bullet 1 says the pointer to member
6742 expression must be a pointer-to-member constant. */
6743 if (!value_dependent_expression_p (expr
)
6744 && !check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6745 return error_mark_node
;
6747 expr
= perform_qualification_conversions (type
, expr
);
6748 if (expr
== error_mark_node
)
6751 else if (NULLPTR_TYPE_P (type
))
6753 if (expr
!= nullptr_node
)
6755 if (complain
& tf_error
)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6762 /* A template non-type parameter must be one of the above. */
6766 /* Sanity check: did we actually convert the argument to the
6768 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6769 (type
, TREE_TYPE (expr
)));
6770 return convert_from_reference (expr
);
6773 /* Subroutine of coerce_template_template_parms, which returns 1 if
6774 PARM_PARM and ARG_PARM match using the rule for the template
6775 parameters of template template parameters. Both PARM and ARG are
6776 template parameters; the rest of the arguments are the same as for
6777 coerce_template_template_parms.
6780 coerce_template_template_parm (tree parm
,
6782 tsubst_flags_t complain
,
6786 if (arg
== NULL_TREE
|| error_operand_p (arg
)
6787 || parm
== NULL_TREE
|| error_operand_p (parm
))
6790 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6793 switch (TREE_CODE (parm
))
6796 /* We encounter instantiations of templates like
6797 template <template <template <class> class> class TT>
6800 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6801 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6803 if (!coerce_template_template_parms
6804 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6810 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6811 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6812 /* Argument is a parameter pack but parameter is not. */
6817 /* The tsubst call is used to handle cases such as
6819 template <int> class C {};
6820 template <class T, template <T> class TT> class D {};
6823 i.e. the parameter list of TT depends on earlier parameters. */
6824 if (!uses_template_parms (TREE_TYPE (arg
)))
6826 tree t
= tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
);
6827 if (!uses_template_parms (t
)
6828 && !same_type_p (t
, TREE_TYPE (arg
)))
6832 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6833 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6834 /* Argument is a parameter pack but parameter is not. */
6847 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6848 template template parameters. Both PARM_PARMS and ARG_PARMS are
6849 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6852 Consider the example:
6853 template <class T> class A;
6854 template<template <class U> class TT> class B;
6856 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6857 the parameters to A, and OUTER_ARGS contains A. */
6860 coerce_template_template_parms (tree parm_parms
,
6862 tsubst_flags_t complain
,
6866 int nparms
, nargs
, i
;
6870 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6871 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6873 nparms
= TREE_VEC_LENGTH (parm_parms
);
6874 nargs
= TREE_VEC_LENGTH (arg_parms
);
6876 /* Determine whether we have a parameter pack at the end of the
6877 template template parameter's template parameter list. */
6878 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6880 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6882 if (error_operand_p (parm
))
6885 switch (TREE_CODE (parm
))
6889 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6894 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6904 && !(variadic_p
&& nargs
>= nparms
- 1))
6907 /* Check all of the template parameters except the parameter pack at
6908 the end (if any). */
6909 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6911 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6912 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6915 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6916 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6918 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6926 /* Check each of the template parameters in the template
6927 argument against the template parameter pack at the end of
6928 the template template parameter. */
6929 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6932 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6934 for (; i
< nargs
; ++i
)
6936 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6939 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6941 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6950 /* Verifies that the deduced template arguments (in TARGS) for the
6951 template template parameters (in TPARMS) represent valid bindings,
6952 by comparing the template parameter list of each template argument
6953 to the template parameter list of its corresponding template
6954 template parameter, in accordance with DR150. This
6955 routine can only be called after all template arguments have been
6956 deduced. It will return TRUE if all of the template template
6957 parameter bindings are okay, FALSE otherwise. */
6959 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6961 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6964 /* We're dealing with template parms in this process. */
6965 ++processing_template_decl
;
6967 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6969 for (i
= 0; i
< ntparms
; ++i
)
6971 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6972 tree targ
= TREE_VEC_ELT (targs
, i
);
6974 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6976 tree packed_args
= NULL_TREE
;
6979 if (ARGUMENT_PACK_P (targ
))
6981 /* Look inside the argument pack. */
6982 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6983 len
= TREE_VEC_LENGTH (packed_args
);
6986 for (idx
= 0; idx
< len
; ++idx
)
6988 tree targ_parms
= NULL_TREE
;
6991 /* Extract the next argument from the argument
6993 targ
= TREE_VEC_ELT (packed_args
, idx
);
6995 if (PACK_EXPANSION_P (targ
))
6996 /* Look at the pattern of the pack expansion. */
6997 targ
= PACK_EXPANSION_PATTERN (targ
);
6999 /* Extract the template parameters from the template
7001 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
7002 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
7003 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
7004 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
7006 /* Verify that we can coerce the template template
7007 parameters from the template argument to the template
7008 parameter. This requires an exact match. */
7010 && !coerce_template_template_parms
7011 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
7026 --processing_template_decl
;
7030 /* Since type attributes aren't mangled, we need to strip them from
7031 template type arguments. */
7034 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
7036 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
7038 bool removed_attributes
= false;
7039 tree canon
= strip_typedefs (arg
, &removed_attributes
);
7040 if (removed_attributes
7041 && (complain
& tf_warning
))
7042 warning (OPT_Wignored_attributes
,
7043 "ignoring attributes on template argument %qT", arg
);
7047 /* And from inside dependent non-type arguments like sizeof(Type). */
7050 canonicalize_expr_argument (tree arg
, tsubst_flags_t complain
)
7052 if (!arg
|| arg
== error_mark_node
)
7054 bool removed_attributes
= false;
7055 tree canon
= strip_typedefs_expr (arg
, &removed_attributes
);
7056 if (removed_attributes
7057 && (complain
& tf_warning
))
7058 warning (OPT_Wignored_attributes
,
7059 "ignoring attributes in template argument %qE", arg
);
7063 // A template declaration can be substituted for a constrained
7064 // template template parameter only when the argument is more
7065 // constrained than the parameter.
7067 is_compatible_template_arg (tree parm
, tree arg
)
7069 tree parm_cons
= get_constraints (parm
);
7071 /* For now, allow constrained template template arguments
7072 and unconstrained template template parameters. */
7073 if (parm_cons
== NULL_TREE
)
7076 tree arg_cons
= get_constraints (arg
);
7078 // If the template parameter is constrained, we need to rewrite its
7079 // constraints in terms of the ARG's template parameters. This ensures
7080 // that all of the template parameter types will have the same depth.
7082 // Note that this is only valid when coerce_template_template_parm is
7083 // true for the innermost template parameters of PARM and ARG. In other
7084 // words, because coercion is successful, this conversion will be valid.
7087 tree args
= template_parms_to_args (DECL_TEMPLATE_PARMS (arg
));
7088 parm_cons
= tsubst_constraint_info (parm_cons
,
7089 INNERMOST_TEMPLATE_ARGS (args
),
7090 tf_none
, NULL_TREE
);
7091 if (parm_cons
== error_mark_node
)
7095 return subsumes (parm_cons
, arg_cons
);
7098 // Convert a placeholder argument into a binding to the original
7099 // parameter. The original parameter is saved as the TREE_TYPE of
7102 convert_wildcard_argument (tree parm
, tree arg
)
7104 TREE_TYPE (arg
) = parm
;
7108 /* Convert the indicated template ARG as necessary to match the
7109 indicated template PARM. Returns the converted ARG, or
7110 error_mark_node if the conversion was unsuccessful. Error and
7111 warning messages are issued under control of COMPLAIN. This
7112 conversion is for the Ith parameter in the parameter list. ARGS is
7113 the full set of template arguments deduced so far. */
7116 convert_template_argument (tree parm
,
7119 tsubst_flags_t complain
,
7125 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
7127 if (parm
== error_mark_node
)
7128 return error_mark_node
;
7130 /* Trivially convert placeholders. */
7131 if (TREE_CODE (arg
) == WILDCARD_DECL
)
7132 return convert_wildcard_argument (parm
, arg
);
7134 if (TREE_CODE (arg
) == TREE_LIST
7135 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
7137 /* The template argument was the name of some
7138 member function. That's usually
7139 invalid, but static members are OK. In any
7140 case, grab the underlying fields/functions
7141 and issue an error later if required. */
7142 orig_arg
= TREE_VALUE (arg
);
7143 TREE_TYPE (arg
) = unknown_type_node
;
7148 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
7149 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
7150 || requires_tmpl_type
);
7152 /* When determining whether an argument pack expansion is a template,
7153 look at the pattern. */
7154 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
7155 arg
= PACK_EXPANSION_PATTERN (arg
);
7157 /* Deal with an injected-class-name used as a template template arg. */
7158 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
7160 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
7161 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7163 if (cxx_dialect
>= cxx11
)
7164 /* OK under DR 1004. */;
7165 else if (complain
& tf_warning_or_error
)
7166 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
7167 " used as template template argument", TYPE_NAME (arg
));
7168 else if (flag_pedantic_errors
)
7176 ((TREE_CODE (arg
) == TEMPLATE_DECL
7177 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
7178 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
7179 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7180 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
7183 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
7184 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
7185 arg
= TYPE_STUB_DECL (arg
);
7187 is_type
= TYPE_P (arg
) || is_tmpl_type
;
7189 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
7190 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
7192 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
7194 if (complain
& tf_error
)
7195 error ("invalid use of destructor %qE as a type", orig_arg
);
7196 return error_mark_node
;
7199 permerror (input_location
,
7200 "to refer to a type member of a template parameter, "
7201 "use %<typename %E%>", orig_arg
);
7203 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
7204 TREE_OPERAND (arg
, 1),
7210 if (is_type
!= requires_type
)
7214 if (complain
& tf_error
)
7216 error ("type/value mismatch at argument %d in template "
7217 "parameter list for %qD",
7220 inform (input_location
,
7221 " expected a constant of type %qT, got %qT",
7223 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
7224 else if (requires_tmpl_type
)
7225 inform (input_location
,
7226 " expected a class template, got %qE", orig_arg
);
7228 inform (input_location
,
7229 " expected a type, got %qE", orig_arg
);
7232 return error_mark_node
;
7234 if (is_tmpl_type
^ requires_tmpl_type
)
7236 if (in_decl
&& (complain
& tf_error
))
7238 error ("type/value mismatch at argument %d in template "
7239 "parameter list for %qD",
7242 inform (input_location
,
7243 " expected a type, got %qT", DECL_NAME (arg
));
7245 inform (input_location
,
7246 " expected a class template, got %qT", orig_arg
);
7248 return error_mark_node
;
7253 if (requires_tmpl_type
)
7255 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7257 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
7258 /* The number of argument required is not known yet.
7259 Just accept it for now. */
7260 val
= TREE_TYPE (arg
);
7263 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
7266 /* Strip alias templates that are equivalent to another
7268 arg
= get_underlying_template (arg
);
7269 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
7271 if (coerce_template_template_parms (parmparm
, argparm
,
7277 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7279 if (val
!= error_mark_node
)
7281 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
7282 val
= TREE_TYPE (val
);
7283 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
7284 val
= make_pack_expansion (val
);
7289 if (in_decl
&& (complain
& tf_error
))
7291 error ("type/value mismatch at argument %d in "
7292 "template parameter list for %qD",
7294 inform (input_location
,
7295 " expected a template of type %qD, got %qT",
7299 val
= error_mark_node
;
7302 // Check that the constraints are compatible before allowing the
7304 if (val
!= error_mark_node
)
7305 if (!is_compatible_template_arg (parm
, arg
))
7307 if (in_decl
&& (complain
& tf_error
))
7309 error ("constraint mismatch at argument %d in "
7310 "template parameter list for %qD",
7312 inform (input_location
, " expected %qD but got %qD",
7315 val
= error_mark_node
;
7321 /* We only form one instance of each template specialization.
7322 Therefore, if we use a non-canonical variant (i.e., a
7323 typedef), any future messages referring to the type will use
7324 the typedef, which is confusing if those future uses do not
7325 themselves also use the typedef. */
7327 val
= canonicalize_type_argument (val
, complain
);
7331 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
7333 if (tree a
= type_uses_auto (t
))
7335 t
= do_auto_deduction (t
, arg
, a
, complain
, adc_unspecified
);
7336 if (t
== error_mark_node
)
7337 return error_mark_node
;
7340 if (invalid_nontype_parm_type_p (t
, complain
))
7341 return error_mark_node
;
7343 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
7345 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
7349 /* Not sure if this is reachable, but it doesn't hurt
7351 error ("type mismatch in nontype parameter pack");
7352 val
= error_mark_node
;
7355 else if (!type_dependent_expression_p (orig_arg
)
7356 && !uses_template_parms (t
))
7357 /* We used to call digest_init here. However, digest_init
7358 will report errors, which we don't want when complain
7359 is zero. More importantly, digest_init will try too
7360 hard to convert things: for example, `0' should not be
7361 converted to pointer type at this point according to
7362 the standard. Accepting this is not merely an
7363 extension, since deciding whether or not these
7364 conversions can occur is part of determining which
7365 function template to call, or whether a given explicit
7366 argument specification is valid. */
7367 val
= convert_nontype_argument (t
, orig_arg
, complain
);
7369 val
= canonicalize_expr_argument (orig_arg
, complain
);
7371 if (val
== NULL_TREE
)
7372 val
= error_mark_node
;
7373 else if (val
== error_mark_node
&& (complain
& tf_error
))
7374 error ("could not convert template argument %qE from %qT to %qT",
7375 orig_arg
, TREE_TYPE (orig_arg
), t
);
7377 if (INDIRECT_REF_P (val
))
7379 /* Reject template arguments that are references to built-in
7380 functions with no library fallbacks. */
7381 const_tree inner
= TREE_OPERAND (val
, 0);
7382 if (TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7383 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner
))) == FUNCTION_TYPE
7384 && TREE_CODE (TREE_TYPE (inner
)) == REFERENCE_TYPE
7385 && 0 < TREE_OPERAND_LENGTH (inner
)
7386 && reject_gcc_builtin (TREE_OPERAND (inner
, 0)))
7387 return error_mark_node
;
7390 if (TREE_CODE (val
) == SCOPE_REF
)
7392 /* Strip typedefs from the SCOPE_REF. */
7393 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
7394 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
7396 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
7397 QUALIFIED_NAME_IS_TEMPLATE (val
));
7404 /* Coerces the remaining template arguments in INNER_ARGS (from
7405 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7406 Returns the coerced argument pack. PARM_IDX is the position of this
7407 parameter in the template parameter list. ARGS is the original
7408 template argument list. */
7410 coerce_template_parameter_pack (tree parms
,
7418 tsubst_flags_t complain
)
7420 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7421 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7424 tree packed_parms
= NULL_TREE
;
7426 if (arg_idx
> nargs
)
7429 if (tree packs
= fixed_parameter_pack_p (TREE_VALUE (parm
)))
7431 /* When the template parameter is a non-type template parameter pack
7432 or template template parameter pack whose type or template
7433 parameters use parameter packs, we know exactly how many arguments
7434 we are looking for. Build a vector of the instantiated decls for
7435 these template parameters in PACKED_PARMS. */
7436 /* We can't use make_pack_expansion here because it would interpret a
7437 _DECL as a use rather than a declaration. */
7438 tree decl
= TREE_VALUE (parm
);
7439 tree exp
= cxx_make_type (TYPE_PACK_EXPANSION
);
7440 SET_PACK_EXPANSION_PATTERN (exp
, decl
);
7441 PACK_EXPANSION_PARAMETER_PACKS (exp
) = packs
;
7442 SET_TYPE_STRUCTURAL_EQUALITY (exp
);
7444 TREE_VEC_LENGTH (args
)--;
7445 packed_parms
= tsubst_pack_expansion (exp
, args
, complain
, decl
);
7446 TREE_VEC_LENGTH (args
)++;
7448 if (packed_parms
== error_mark_node
)
7449 return error_mark_node
;
7451 /* If we're doing a partial instantiation of a member template,
7452 verify that all of the types used for the non-type
7453 template parameter pack are, in fact, valid for non-type
7454 template parameters. */
7456 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7458 int j
, len
= TREE_VEC_LENGTH (packed_parms
);
7459 for (j
= 0; j
< len
; ++j
)
7461 tree t
= TREE_TYPE (TREE_VEC_ELT (packed_parms
, j
));
7462 if (invalid_nontype_parm_type_p (t
, complain
))
7463 return error_mark_node
;
7465 /* We don't know how many args we have yet, just
7466 use the unconverted ones for now. */
7470 packed_args
= make_tree_vec (TREE_VEC_LENGTH (packed_parms
));
7472 /* Check if we have a placeholder pack, which indicates we're
7473 in the context of a introduction list. In that case we want
7474 to match this pack to the single placeholder. */
7475 else if (arg_idx
< nargs
7476 && TREE_CODE (TREE_VEC_ELT (inner_args
, arg_idx
)) == WILDCARD_DECL
7477 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
7479 nargs
= arg_idx
+ 1;
7480 packed_args
= make_tree_vec (1);
7483 packed_args
= make_tree_vec (nargs
- arg_idx
);
7485 /* Convert the remaining arguments, which will be a part of the
7486 parameter pack "parm". */
7487 int first_pack_arg
= arg_idx
;
7488 for (; arg_idx
< nargs
; ++arg_idx
)
7490 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7491 tree actual_parm
= TREE_VALUE (parm
);
7492 int pack_idx
= arg_idx
- first_pack_arg
;
7496 /* Once we've packed as many args as we have types, stop. */
7497 if (pack_idx
>= TREE_VEC_LENGTH (packed_parms
))
7499 else if (PACK_EXPANSION_P (arg
))
7500 /* We don't know how many args we have yet, just
7501 use the unconverted ones for now. */
7504 actual_parm
= TREE_VEC_ELT (packed_parms
, pack_idx
);
7507 if (arg
== error_mark_node
)
7509 if (complain
& tf_error
)
7510 error ("template argument %d is invalid", arg_idx
+ 1);
7513 arg
= convert_template_argument (actual_parm
,
7514 arg
, new_args
, complain
, parm_idx
,
7516 if (arg
== error_mark_node
)
7518 TREE_VEC_ELT (packed_args
, pack_idx
) = arg
;
7521 if (arg_idx
- first_pack_arg
< TREE_VEC_LENGTH (packed_args
)
7522 && TREE_VEC_LENGTH (packed_args
) > 0)
7524 if (complain
& tf_error
)
7525 error ("wrong number of template arguments (%d, should be %d)",
7526 arg_idx
- first_pack_arg
, TREE_VEC_LENGTH (packed_args
));
7527 return error_mark_node
;
7530 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
7531 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
7532 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
7535 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
7536 TREE_TYPE (argument_pack
)
7537 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
7538 TREE_CONSTANT (argument_pack
) = 1;
7541 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
7543 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
7544 TREE_VEC_LENGTH (packed_args
));
7545 return argument_pack
;
7548 /* Returns the number of pack expansions in the template argument vector
7552 pack_expansion_args_count (tree args
)
7557 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
7559 tree elt
= TREE_VEC_ELT (args
, i
);
7560 if (elt
&& PACK_EXPANSION_P (elt
))
7566 /* Convert all template arguments to their appropriate types, and
7567 return a vector containing the innermost resulting template
7568 arguments. If any error occurs, return error_mark_node. Error and
7569 warning messages are issued under control of COMPLAIN.
7571 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7572 for arguments not specified in ARGS. Otherwise, if
7573 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7574 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7575 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7579 coerce_template_parms (tree parms
,
7582 tsubst_flags_t complain
,
7583 bool require_all_args
,
7584 bool use_default_args
)
7586 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
7587 tree orig_inner_args
;
7590 tree new_inner_args
;
7591 int saved_unevaluated_operand
;
7592 int saved_inhibit_evaluation_warnings
;
7594 /* When used as a boolean value, indicates whether this is a
7595 variadic template parameter list. Since it's an int, we can also
7596 subtract it from nparms to get the number of non-variadic
7599 int variadic_args_p
= 0;
7600 int post_variadic_parms
= 0;
7602 /* Likewise for parameters with default arguments. */
7605 if (args
== error_mark_node
)
7606 return error_mark_node
;
7608 nparms
= TREE_VEC_LENGTH (parms
);
7610 /* Determine if there are any parameter packs or default arguments. */
7611 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
7613 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
7615 ++post_variadic_parms
;
7616 if (template_parameter_pack_p (TREE_VALUE (parm
)))
7618 if (TREE_PURPOSE (parm
))
7622 inner_args
= orig_inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
7623 /* If there are no parameters that follow a parameter pack, we need to
7624 expand any argument packs so that we can deduce a parameter pack from
7625 some non-packed args followed by an argument pack, as in variadic85.C.
7626 If there are such parameters, we need to leave argument packs intact
7627 so the arguments are assigned properly. This can happen when dealing
7628 with a nested class inside a partial specialization of a class
7629 template, as in variadic92.C, or when deducing a template parameter pack
7630 from a sub-declarator, as in variadic114.C. */
7631 if (!post_variadic_parms
)
7632 inner_args
= expand_template_argument_pack (inner_args
);
7634 /* Count any pack expansion args. */
7635 variadic_args_p
= pack_expansion_args_count (inner_args
);
7637 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
7638 if ((nargs
> nparms
&& !variadic_p
)
7639 || (nargs
< nparms
- variadic_p
7642 && (!use_default_args
7643 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
7644 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
7646 if (complain
& tf_error
)
7648 if (variadic_p
|| default_p
)
7650 nparms
-= variadic_p
+ default_p
;
7651 error ("wrong number of template arguments "
7652 "(%d, should be at least %d)", nargs
, nparms
);
7655 error ("wrong number of template arguments "
7656 "(%d, should be %d)", nargs
, nparms
);
7659 inform (DECL_SOURCE_LOCATION (in_decl
),
7660 "provided for %qD", in_decl
);
7663 return error_mark_node
;
7665 /* We can't pass a pack expansion to a non-pack parameter of an alias
7666 template (DR 1430). */
7668 && (DECL_ALIAS_TEMPLATE_P (in_decl
)
7669 || concept_template_p (in_decl
))
7671 && nargs
- variadic_args_p
< nparms
- variadic_p
)
7673 if (complain
& tf_error
)
7675 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
7677 tree arg
= TREE_VEC_ELT (inner_args
, i
);
7678 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7680 if (PACK_EXPANSION_P (arg
)
7681 && !template_parameter_pack_p (parm
))
7683 if (DECL_ALIAS_TEMPLATE_P (in_decl
))
7684 error_at (location_of (arg
),
7685 "pack expansion argument for non-pack parameter "
7686 "%qD of alias template %qD", parm
, in_decl
);
7688 error_at (location_of (arg
),
7689 "pack expansion argument for non-pack parameter "
7690 "%qD of concept %qD", parm
, in_decl
);
7691 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
7698 return error_mark_node
;
7701 /* We need to evaluate the template arguments, even though this
7702 template-id may be nested within a "sizeof". */
7703 saved_unevaluated_operand
= cp_unevaluated_operand
;
7704 cp_unevaluated_operand
= 0;
7705 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7706 c_inhibit_evaluation_warnings
= 0;
7707 new_inner_args
= make_tree_vec (nparms
);
7708 new_args
= add_outermost_template_args (args
, new_inner_args
);
7709 int pack_adjust
= 0;
7710 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
7715 /* Get the Ith template parameter. */
7716 parm
= TREE_VEC_ELT (parms
, parm_idx
);
7718 if (parm
== error_mark_node
)
7720 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
7724 /* Calculate the next argument. */
7725 if (arg_idx
< nargs
)
7726 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
7730 if (template_parameter_pack_p (TREE_VALUE (parm
))
7731 && !(arg
&& ARGUMENT_PACK_P (arg
)))
7733 /* Some arguments will be placed in the
7734 template parameter pack PARM. */
7735 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
7736 inner_args
, arg_idx
,
7740 if (arg
== NULL_TREE
)
7742 /* We don't know how many args we have yet, just use the
7743 unconverted (and still packed) ones for now. */
7744 new_inner_args
= orig_inner_args
;
7749 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
7751 /* Store this argument. */
7752 if (arg
== error_mark_node
)
7755 /* We are done with all of the arguments. */
7760 pack_adjust
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
)) - 1;
7761 arg_idx
+= pack_adjust
;
7768 if (PACK_EXPANSION_P (arg
))
7770 /* "If every valid specialization of a variadic template
7771 requires an empty template parameter pack, the template is
7772 ill-formed, no diagnostic required." So check that the
7773 pattern works with this parameter. */
7774 tree pattern
= PACK_EXPANSION_PATTERN (arg
);
7775 tree conv
= convert_template_argument (TREE_VALUE (parm
),
7779 if (conv
== error_mark_node
)
7781 inform (input_location
, "so any instantiation with a "
7782 "non-empty parameter pack would be ill-formed");
7785 else if (TYPE_P (conv
) && !TYPE_P (pattern
))
7786 /* Recover from missing typename. */
7787 TREE_VEC_ELT (inner_args
, arg_idx
)
7788 = make_pack_expansion (conv
);
7790 /* We don't know how many args we have yet, just
7791 use the unconverted ones for now. */
7792 new_inner_args
= inner_args
;
7797 else if (require_all_args
)
7799 /* There must be a default arg in this case. */
7800 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
7802 /* The position of the first default template argument,
7803 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7805 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7806 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7807 arg_idx
- pack_adjust
);
7812 if (arg
== error_mark_node
)
7814 if (complain
& tf_error
)
7815 error ("template argument %d is invalid", arg_idx
+ 1);
7818 /* This only occurs if there was an error in the template
7819 parameter list itself (which we would already have
7820 reported) that we are trying to recover from, e.g., a class
7821 template with a parameter list such as
7822 template<typename..., typename>. */
7825 arg
= convert_template_argument (TREE_VALUE (parm
),
7826 arg
, new_args
, complain
,
7829 if (arg
== error_mark_node
)
7831 TREE_VEC_ELT (new_inner_args
, arg_idx
- pack_adjust
) = arg
;
7833 cp_unevaluated_operand
= saved_unevaluated_operand
;
7834 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
7836 if (variadic_p
&& arg_idx
< nargs
)
7838 if (complain
& tf_error
)
7840 error ("wrong number of template arguments "
7841 "(%d, should be %d)", nargs
, arg_idx
);
7843 error ("provided for %q+D", in_decl
);
7845 return error_mark_node
;
7849 return error_mark_node
;
7851 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
7852 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
7853 TREE_VEC_LENGTH (new_inner_args
));
7855 return new_inner_args
;
7858 /* Convert all template arguments to their appropriate types, and
7859 return a vector containing the innermost resulting template
7860 arguments. If any error occurs, return error_mark_node. Error and
7861 warning messages are not issued.
7863 Note that no function argument deduction is performed, and default
7864 arguments are used to fill in unspecified arguments. */
7866 coerce_template_parms (tree parms
, tree args
, tree in_decl
)
7868 return coerce_template_parms (parms
, args
, in_decl
, tf_none
, true, true);
7871 /* Convert all template arguments to their appropriate type, and
7872 instantiate default arguments as needed. This returns a vector
7873 containing the innermost resulting template arguments, or
7874 error_mark_node if unsuccessful. */
7876 coerce_template_parms (tree parms
, tree args
, tree in_decl
,
7877 tsubst_flags_t complain
)
7879 return coerce_template_parms (parms
, args
, in_decl
, complain
, true, true);
7882 /* Like coerce_template_parms. If PARMS represents all template
7883 parameters levels, this function returns a vector of vectors
7884 representing all the resulting argument levels. Note that in this
7885 case, only the innermost arguments are coerced because the
7886 outermost ones are supposed to have been coerced already.
7888 Otherwise, if PARMS represents only (the innermost) vector of
7889 parameters, this function returns a vector containing just the
7890 innermost resulting arguments. */
7893 coerce_innermost_template_parms (tree parms
,
7896 tsubst_flags_t complain
,
7897 bool require_all_args
,
7898 bool use_default_args
)
7900 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
7901 int args_depth
= TMPL_ARGS_DEPTH (args
);
7904 if (parms_depth
> 1)
7906 coerced_args
= make_tree_vec (parms_depth
);
7910 for (level
= parms
, cur_depth
= parms_depth
;
7911 parms_depth
> 0 && level
!= NULL_TREE
;
7912 level
= TREE_CHAIN (level
), --cur_depth
)
7915 if (cur_depth
== args_depth
)
7916 l
= coerce_template_parms (TREE_VALUE (level
),
7917 args
, in_decl
, complain
,
7921 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
7923 if (l
== error_mark_node
)
7924 return error_mark_node
;
7926 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
7930 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
7931 args
, in_decl
, complain
,
7934 return coerced_args
;
7937 /* Returns 1 if template args OT and NT are equivalent. */
7940 template_args_equal (tree ot
, tree nt
)
7944 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
7947 if (TREE_CODE (nt
) == TREE_VEC
)
7948 /* For member templates */
7949 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
7950 else if (PACK_EXPANSION_P (ot
))
7951 return (PACK_EXPANSION_P (nt
)
7952 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
7953 PACK_EXPANSION_PATTERN (nt
))
7954 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
7955 PACK_EXPANSION_EXTRA_ARGS (nt
)));
7956 else if (ARGUMENT_PACK_P (ot
))
7961 if (!ARGUMENT_PACK_P (nt
))
7964 opack
= ARGUMENT_PACK_ARGS (ot
);
7965 npack
= ARGUMENT_PACK_ARGS (nt
);
7966 len
= TREE_VEC_LENGTH (opack
);
7967 if (TREE_VEC_LENGTH (npack
) != len
)
7969 for (i
= 0; i
< len
; ++i
)
7970 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
7971 TREE_VEC_ELT (npack
, i
)))
7975 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7977 else if (TYPE_P (nt
))
7981 /* Don't treat an alias template specialization with dependent
7982 arguments as equivalent to its underlying type when used as a
7983 template argument; we need them to be distinct so that we
7984 substitute into the specialization arguments at instantiation
7985 time. And aliases can't be equivalent without being ==, so
7986 we don't need to look any deeper. */
7987 if (TYPE_ALIAS_P (nt
) || TYPE_ALIAS_P (ot
))
7990 return same_type_p (ot
, nt
);
7992 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7996 /* Try to treat a template non-type argument that has been converted
7997 to the parameter type as equivalent to one that hasn't yet. */
7998 for (enum tree_code code1
= TREE_CODE (ot
);
7999 CONVERT_EXPR_CODE_P (code1
)
8000 || code1
== NON_LVALUE_EXPR
;
8001 code1
= TREE_CODE (ot
))
8002 ot
= TREE_OPERAND (ot
, 0);
8003 for (enum tree_code code2
= TREE_CODE (nt
);
8004 CONVERT_EXPR_CODE_P (code2
)
8005 || code2
== NON_LVALUE_EXPR
;
8006 code2
= TREE_CODE (nt
))
8007 nt
= TREE_OPERAND (nt
, 0);
8009 return cp_tree_equal (ot
, nt
);
8013 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8014 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8015 NEWARG_PTR with the offending arguments if they are non-NULL. */
8018 comp_template_args (tree oldargs
, tree newargs
,
8019 tree
*oldarg_ptr
, tree
*newarg_ptr
)
8023 if (oldargs
== newargs
)
8026 if (!oldargs
|| !newargs
)
8029 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
8032 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
8034 tree nt
= TREE_VEC_ELT (newargs
, i
);
8035 tree ot
= TREE_VEC_ELT (oldargs
, i
);
8037 if (! template_args_equal (ot
, nt
))
8039 if (oldarg_ptr
!= NULL
)
8041 if (newarg_ptr
!= NULL
)
8050 add_pending_template (tree d
)
8052 tree ti
= (TYPE_P (d
)
8053 ? CLASSTYPE_TEMPLATE_INFO (d
)
8054 : DECL_TEMPLATE_INFO (d
));
8055 struct pending_template
*pt
;
8058 if (TI_PENDING_TEMPLATE_FLAG (ti
))
8061 /* We are called both from instantiate_decl, where we've already had a
8062 tinst_level pushed, and instantiate_template, where we haven't.
8064 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
8067 push_tinst_level (d
);
8069 pt
= ggc_alloc
<pending_template
> ();
8071 pt
->tinst
= current_tinst_level
;
8072 if (last_pending_template
)
8073 last_pending_template
->next
= pt
;
8075 pending_templates
= pt
;
8077 last_pending_template
= pt
;
8079 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
8086 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8087 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8088 documentation for TEMPLATE_ID_EXPR. */
8091 lookup_template_function (tree fns
, tree arglist
)
8095 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
8096 return error_mark_node
;
8098 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
8100 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
8102 error ("%q#D is not a function template", fns
);
8103 return error_mark_node
;
8106 if (BASELINK_P (fns
))
8108 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
8110 BASELINK_FUNCTIONS (fns
),
8115 type
= TREE_TYPE (fns
);
8116 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
8117 type
= unknown_type_node
;
8119 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
8122 /* Within the scope of a template class S<T>, the name S gets bound
8123 (in build_self_reference) to a TYPE_DECL for the class, not a
8124 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8125 or one of its enclosing classes, and that type is a template,
8126 return the associated TEMPLATE_DECL. Otherwise, the original
8129 Also handle the case when DECL is a TREE_LIST of ambiguous
8130 injected-class-names from different bases. */
8133 maybe_get_template_decl_from_type_decl (tree decl
)
8135 if (decl
== NULL_TREE
)
8138 /* DR 176: A lookup that finds an injected-class-name (10.2
8139 [class.member.lookup]) can result in an ambiguity in certain cases
8140 (for example, if it is found in more than one base class). If all of
8141 the injected-class-names that are found refer to specializations of
8142 the same class template, and if the name is followed by a
8143 template-argument-list, the reference refers to the class template
8144 itself and not a specialization thereof, and is not ambiguous. */
8145 if (TREE_CODE (decl
) == TREE_LIST
)
8147 tree t
, tmpl
= NULL_TREE
;
8148 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
8150 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
8153 else if (tmpl
!= elt
)
8156 if (tmpl
&& t
== NULL_TREE
)
8162 return (decl
!= NULL_TREE
8163 && DECL_SELF_REFERENCE_P (decl
)
8164 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
8165 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
8168 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8169 parameters, find the desired type.
8171 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8173 IN_DECL, if non-NULL, is the template declaration we are trying to
8176 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8177 the class we are looking up.
8179 Issue error and warning messages under control of COMPLAIN.
8181 If the template class is really a local class in a template
8182 function, then the FUNCTION_CONTEXT is the function in which it is
8185 ??? Note that this function is currently called *twice* for each
8186 template-id: the first time from the parser, while creating the
8187 incomplete type (finish_template_type), and the second type during the
8188 real instantiation (instantiate_template_class). This is surely something
8189 that we want to avoid. It also causes some problems with argument
8190 coercion (see convert_nontype_argument for more information on this). */
8193 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
8194 int entering_scope
, tsubst_flags_t complain
)
8196 tree templ
= NULL_TREE
, parmlist
;
8203 if (identifier_p (d1
))
8205 tree value
= innermost_non_namespace_value (d1
);
8206 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
8211 push_decl_namespace (context
);
8212 templ
= lookup_name (d1
);
8213 templ
= maybe_get_template_decl_from_type_decl (templ
);
8215 pop_decl_namespace ();
8218 context
= DECL_CONTEXT (templ
);
8220 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
8222 tree type
= TREE_TYPE (d1
);
8224 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8225 an implicit typename for the second A. Deal with it. */
8226 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
8227 type
= TREE_TYPE (type
);
8229 if (CLASSTYPE_TEMPLATE_INFO (type
))
8231 templ
= CLASSTYPE_TI_TEMPLATE (type
);
8232 d1
= DECL_NAME (templ
);
8235 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
8236 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
8238 templ
= TYPE_TI_TEMPLATE (d1
);
8239 d1
= DECL_NAME (templ
);
8241 else if (DECL_TYPE_TEMPLATE_P (d1
))
8244 d1
= DECL_NAME (templ
);
8245 context
= DECL_CONTEXT (templ
);
8247 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
8250 d1
= DECL_NAME (templ
);
8253 /* Issue an error message if we didn't find a template. */
8256 if (complain
& tf_error
)
8257 error ("%qT is not a template", d1
);
8258 return error_mark_node
;
8261 if (TREE_CODE (templ
) != TEMPLATE_DECL
8262 /* Make sure it's a user visible template, if it was named by
8264 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
8265 && !PRIMARY_TEMPLATE_P (templ
)))
8267 if (complain
& tf_error
)
8269 error ("non-template type %qT used as a template", d1
);
8271 error ("for template declaration %q+D", in_decl
);
8273 return error_mark_node
;
8276 complain
&= ~tf_user
;
8278 /* An alias that just changes the name of a template is equivalent to the
8279 other template, so if any of the arguments are pack expansions, strip
8280 the alias to avoid problems with a pack expansion passed to a non-pack
8281 alias template parameter (DR 1430). */
8282 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
8283 templ
= get_underlying_template (templ
);
8285 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
8287 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8288 template arguments */
8294 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
8296 /* Consider an example where a template template parameter declared as
8298 template <class T, class U = std::allocator<T> > class TT
8300 The template parameter level of T and U are one level larger than
8301 of TT. To proper process the default argument of U, say when an
8302 instantiation `TT<int>' is seen, we need to build the full
8303 arguments containing {int} as the innermost level. Outer levels,
8304 available when not appearing as default template argument, can be
8305 obtained from the arguments of the enclosing template.
8307 Suppose that TT is later substituted with std::vector. The above
8308 instantiation is `TT<int, std::allocator<T> >' with TT at
8309 level 1, and T at level 2, while the template arguments at level 1
8310 becomes {std::vector} and the inner level 2 is {int}. */
8312 outer
= DECL_CONTEXT (templ
);
8314 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
8315 else if (current_template_parms
)
8317 /* This is an argument of the current template, so we haven't set
8318 DECL_CONTEXT yet. */
8319 tree relevant_template_parms
;
8321 /* Parameter levels that are greater than the level of the given
8322 template template parm are irrelevant. */
8323 relevant_template_parms
= current_template_parms
;
8324 while (TMPL_PARMS_DEPTH (relevant_template_parms
)
8325 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ
)))
8326 relevant_template_parms
= TREE_CHAIN (relevant_template_parms
);
8328 outer
= template_parms_to_args (relevant_template_parms
);
8332 arglist
= add_to_template_args (outer
, arglist
);
8334 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
8336 /*require_all_args=*/true,
8337 /*use_default_args=*/true);
8338 if (arglist2
== error_mark_node
8339 || (!uses_template_parms (arglist2
)
8340 && check_instantiated_args (templ
, arglist2
, complain
)))
8341 return error_mark_node
;
8343 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
8348 tree template_type
= TREE_TYPE (templ
);
8351 tree found
= NULL_TREE
;
8354 int is_dependent_type
;
8355 int use_partial_inst_tmpl
= false;
8357 if (template_type
== error_mark_node
)
8358 /* An error occurred while building the template TEMPL, and a
8359 diagnostic has most certainly been emitted for that
8360 already. Let's propagate that error. */
8361 return error_mark_node
;
8363 gen_tmpl
= most_general_template (templ
);
8364 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
8365 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
8366 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8368 if (arg_depth
== 1 && parm_depth
> 1)
8370 /* We've been given an incomplete set of template arguments.
8373 template <class T> struct S1 {
8374 template <class U> struct S2 {};
8375 template <class U> struct S2<U*> {};
8378 we will be called with an ARGLIST of `U*', but the
8379 TEMPLATE will be `template <class T> template
8380 <class U> struct S1<T>::S2'. We must fill in the missing
8382 tree ti
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ
));
8383 arglist
= add_outermost_template_args (TI_ARGS (ti
), arglist
);
8384 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
8387 /* Now we should have enough arguments. */
8388 gcc_assert (parm_depth
== arg_depth
);
8390 /* From here on, we're only interested in the most general
8393 /* Calculate the BOUND_ARGS. These will be the args that are
8394 actually tsubst'd into the definition to create the
8396 arglist
= coerce_innermost_template_parms (parmlist
, arglist
, gen_tmpl
,
8398 /*require_all_args=*/true,
8399 /*use_default_args=*/true);
8401 if (arglist
== error_mark_node
)
8402 /* We were unable to bind the arguments. */
8403 return error_mark_node
;
8405 /* In the scope of a template class, explicit references to the
8406 template class refer to the type of the template, not any
8407 instantiation of it. For example, in:
8409 template <class T> class C { void f(C<T>); }
8411 the `C<T>' is just the same as `C'. Outside of the
8412 class, however, such a reference is an instantiation. */
8414 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
8415 || currently_open_class (template_type
))
8417 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (template_type
);
8419 if (comp_template_args (TI_ARGS (tinfo
), arglist
))
8420 return template_type
;
8423 /* If we already have this specialization, return it. */
8424 elt
.tmpl
= gen_tmpl
;
8426 elt
.spec
= NULL_TREE
;
8427 hash
= spec_hasher::hash (&elt
);
8428 entry
= type_specializations
->find_with_hash (&elt
, hash
);
8433 /* If the the template's constraints are not satisfied,
8434 then we cannot form a valid type.
8436 Note that the check is deferred until after the hash
8437 lookup. This prevents redundant checks on previously
8438 instantiated specializations. */
8439 if (flag_concepts
&& !constraints_satisfied_p (gen_tmpl
, arglist
))
8441 if (complain
& tf_error
)
8443 error ("template constraint failure");
8444 diagnose_constraints (input_location
, gen_tmpl
, arglist
);
8446 return error_mark_node
;
8449 is_dependent_type
= uses_template_parms (arglist
);
8451 /* If the deduced arguments are invalid, then the binding
8453 if (!is_dependent_type
8454 && check_instantiated_args (gen_tmpl
,
8455 INNERMOST_TEMPLATE_ARGS (arglist
),
8457 return error_mark_node
;
8459 if (!is_dependent_type
8460 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8461 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
8462 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
8464 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
8465 DECL_NAME (gen_tmpl
),
8466 /*tag_scope=*/ts_global
);
8470 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
8472 if (context
== error_mark_node
)
8473 return error_mark_node
;
8476 context
= global_namespace
;
8478 /* Create the type. */
8479 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8481 /* The user referred to a specialization of an alias
8482 template represented by GEN_TMPL.
8484 [temp.alias]/2 says:
8486 When a template-id refers to the specialization of an
8487 alias template, it is equivalent to the associated
8488 type obtained by substitution of its
8489 template-arguments for the template-parameters in the
8490 type-id of the alias template. */
8492 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
8493 /* Note that the call above (by indirectly calling
8494 register_specialization in tsubst_decl) registers the
8495 TYPE_DECL representing the specialization of the alias
8496 template. So next time someone substitutes ARGLIST for
8497 the template parms into the alias template (GEN_TMPL),
8498 she'll get that TYPE_DECL back. */
8500 if (t
== error_mark_node
)
8503 else if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
8505 if (!is_dependent_type
)
8507 set_current_access_from_decl (TYPE_NAME (template_type
));
8508 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
8509 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
8510 arglist
, complain
, in_decl
),
8511 tsubst_attributes (TYPE_ATTRIBUTES (template_type
),
8512 arglist
, complain
, in_decl
),
8513 SCOPED_ENUM_P (template_type
), NULL
);
8515 if (t
== error_mark_node
)
8520 /* We don't want to call start_enum for this type, since
8521 the values for the enumeration constants may involve
8522 template parameters. And, no one should be interested
8523 in the enumeration constants for such a type. */
8524 t
= cxx_make_type (ENUMERAL_TYPE
);
8525 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
8527 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
8528 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
8529 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
8531 else if (CLASS_TYPE_P (template_type
))
8533 t
= make_class_type (TREE_CODE (template_type
));
8534 CLASSTYPE_DECLARED_CLASS (t
)
8535 = CLASSTYPE_DECLARED_CLASS (template_type
);
8536 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
8538 /* A local class. Make sure the decl gets registered properly. */
8539 if (context
== current_function_decl
)
8540 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
8542 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
8543 /* This instantiation is another name for the primary
8544 template type. Set the TYPE_CANONICAL field
8546 TYPE_CANONICAL (t
) = template_type
;
8547 else if (any_template_arguments_need_structural_equality_p (arglist
))
8548 /* Some of the template arguments require structural
8549 equality testing, so this template class requires
8550 structural equality testing. */
8551 SET_TYPE_STRUCTURAL_EQUALITY (t
);
8556 /* If we called start_enum or pushtag above, this information
8557 will already be set up. */
8560 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
8562 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
8563 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
8564 DECL_SOURCE_LOCATION (type_decl
)
8565 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
8568 type_decl
= TYPE_NAME (t
);
8570 if (CLASS_TYPE_P (template_type
))
8572 TREE_PRIVATE (type_decl
)
8573 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
8574 TREE_PROTECTED (type_decl
)
8575 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
8576 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
8578 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
8579 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
8583 if (OVERLOAD_TYPE_P (t
)
8584 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8586 static const char *tags
[] = {"abi_tag", "may_alias"};
8588 for (unsigned ix
= 0; ix
!= 2; ix
++)
8591 = lookup_attribute (tags
[ix
], TYPE_ATTRIBUTES (template_type
));
8595 = tree_cons (TREE_PURPOSE (attributes
),
8596 TREE_VALUE (attributes
),
8597 TYPE_ATTRIBUTES (t
));
8601 /* Let's consider the explicit specialization of a member
8602 of a class template specialization that is implicitly instantiated,
8607 template<class U> struct M {}; //#0
8612 struct S<int>::M<char> //#1
8616 [temp.expl.spec]/4 says this is valid.
8618 In this case, when we write:
8621 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8624 When we encounter #1, we want to store the partial instantiation
8625 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8627 For all cases other than this "explicit specialization of member of a
8628 class template", we just want to store the most general template into
8629 the CLASSTYPE_TI_TEMPLATE of M.
8631 This case of "explicit specialization of member of a class template"
8633 1/ the enclosing class is an instantiation of, and therefore not
8634 the same as, the context of the most general template, and
8635 2/ we aren't looking at the partial instantiation itself, i.e.
8636 the innermost arguments are not the same as the innermost parms of
8637 the most general template.
8639 So it's only when 1/ and 2/ happens that we want to use the partial
8640 instantiation of the member template in lieu of its most general
8643 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
8644 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
8645 /* the enclosing class must be an instantiation... */
8646 && CLASS_TYPE_P (context
)
8647 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
8649 TREE_VEC_LENGTH (arglist
)--;
8650 ++processing_template_decl
;
8651 tree tinfo
= TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl
));
8652 tree partial_inst_args
=
8653 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
)),
8654 arglist
, complain
, NULL_TREE
);
8655 --processing_template_decl
;
8656 TREE_VEC_LENGTH (arglist
)++;
8657 if (partial_inst_args
== error_mark_node
)
8658 return error_mark_node
;
8659 use_partial_inst_tmpl
=
8660 /*...and we must not be looking at the partial instantiation
8662 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
8666 if (!use_partial_inst_tmpl
)
8667 /* This case is easy; there are no member templates involved. */
8671 /* This is a full instantiation of a member template. Find
8672 the partial instantiation of which this is an instance. */
8674 /* Temporarily reduce by one the number of levels in the ARGLIST
8675 so as to avoid comparing the last set of arguments. */
8676 TREE_VEC_LENGTH (arglist
)--;
8677 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
8678 TREE_VEC_LENGTH (arglist
)++;
8679 /* FOUND is either a proper class type, or an alias
8680 template specialization. In the later case, it's a
8681 TYPE_DECL, resulting from the substituting of arguments
8682 for parameters in the TYPE_DECL of the alias template
8683 done earlier. So be careful while getting the template
8685 found
= (TREE_CODE (found
) == TEMPLATE_DECL
8687 : (TREE_CODE (found
) == TYPE_DECL
8688 ? DECL_TI_TEMPLATE (found
)
8689 : CLASSTYPE_TI_TEMPLATE (found
)));
8692 // Build template info for the new specialization.
8693 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
8696 slot
= type_specializations
->find_slot_with_hash (&elt
, hash
, INSERT
);
8697 entry
= ggc_alloc
<spec_entry
> ();
8701 /* Note this use of the partial instantiation so we can check it
8702 later in maybe_process_partial_specialization. */
8703 DECL_TEMPLATE_INSTANTIATIONS (found
)
8704 = tree_cons (arglist
, t
,
8705 DECL_TEMPLATE_INSTANTIATIONS (found
));
8707 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
8708 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
8709 /* Now that the type has been registered on the instantiations
8710 list, we set up the enumerators. Because the enumeration
8711 constants may involve the enumeration type itself, we make
8712 sure to register the type first, and then create the
8713 constants. That way, doing tsubst_expr for the enumeration
8714 constants won't result in recursive calls here; we'll find
8715 the instantiation and exit above. */
8716 tsubst_enum (template_type
, t
, arglist
);
8718 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
8719 /* If the type makes use of template parameters, the
8720 code that generates debugging information will crash. */
8721 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
8723 /* Possibly limit visibility based on template args. */
8724 TREE_PUBLIC (type_decl
) = 1;
8725 determine_visibility (type_decl
);
8727 inherit_targ_abi_tags (t
);
8733 /* Wrapper for lookup_template_class_1. */
8736 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
8737 int entering_scope
, tsubst_flags_t complain
)
8740 timevar_push (TV_TEMPLATE_INST
);
8741 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
8742 entering_scope
, complain
);
8743 timevar_pop (TV_TEMPLATE_INST
);
8747 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8750 lookup_template_variable (tree templ
, tree arglist
)
8752 /* The type of the expression is NULL_TREE since the template-id could refer
8753 to an explicit or partial specialization. */
8754 tree type
= NULL_TREE
;
8755 if (flag_concepts
&& variable_concept_p (templ
))
8756 /* Except that concepts are always bool. */
8757 type
= boolean_type_node
;
8758 return build2 (TEMPLATE_ID_EXPR
, type
, templ
, arglist
);
8761 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8764 finish_template_variable (tree var
, tsubst_flags_t complain
)
8766 tree templ
= TREE_OPERAND (var
, 0);
8767 tree arglist
= TREE_OPERAND (var
, 1);
8769 /* We never want to return a VAR_DECL for a variable concept, since they
8770 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8771 bool concept_p
= flag_concepts
&& variable_concept_p (templ
);
8772 if (concept_p
&& processing_template_decl
)
8775 tree tmpl_args
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ
));
8776 arglist
= add_outermost_template_args (tmpl_args
, arglist
);
8778 tree parms
= DECL_TEMPLATE_PARMS (templ
);
8779 arglist
= coerce_innermost_template_parms (parms
, arglist
, templ
, complain
,
8781 /*use_default*/true);
8783 if (flag_concepts
&& !constraints_satisfied_p (templ
, arglist
))
8785 if (complain
& tf_error
)
8787 error ("use of invalid variable template %qE", var
);
8788 diagnose_constraints (location_of (var
), templ
, arglist
);
8790 return error_mark_node
;
8793 /* If a template-id refers to a specialization of a variable
8794 concept, then the expression is true if and only if the
8795 concept's constraints are satisfied by the given template
8798 NOTE: This is an extension of Concepts Lite TS that
8799 allows constraints to be used in expressions. */
8802 tree decl
= DECL_TEMPLATE_RESULT (templ
);
8803 return evaluate_variable_concept (decl
, arglist
);
8806 return instantiate_template (templ
, arglist
, complain
);
8809 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8810 TARGS template args, and instantiate it if it's not dependent. */
8813 lookup_and_finish_template_variable (tree templ
, tree targs
,
8814 tsubst_flags_t complain
)
8816 templ
= lookup_template_variable (templ
, targs
);
8817 if (!any_dependent_template_arguments_p (targs
))
8819 templ
= finish_template_variable (templ
, complain
);
8823 return convert_from_reference (templ
);
8832 /* True when we should also visit template parameters that occur in
8833 non-deduced contexts. */
8834 bool include_nondeduced_p
;
8835 hash_set
<tree
> *visited
;
8838 /* Called from for_each_template_parm via walk_tree. */
8841 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
8844 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
8845 tree_fn_t fn
= pfd
->fn
;
8846 void *data
= pfd
->data
;
8847 tree result
= NULL_TREE
;
8849 #define WALK_SUBTREE(NODE) \
8852 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8853 pfd->include_nondeduced_p, \
8855 if (result) goto out; \
8859 if (pfd
->any_fn
&& (*pfd
->any_fn
)(t
, data
))
8863 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
))
8864 WALK_SUBTREE (TYPE_CONTEXT (t
));
8866 switch (TREE_CODE (t
))
8869 if (TYPE_PTRMEMFUNC_P (t
))
8875 if (!TYPE_TEMPLATE_INFO (t
))
8878 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8882 WALK_SUBTREE (TYPE_MIN_VALUE (t
));
8883 WALK_SUBTREE (TYPE_MAX_VALUE (t
));
8887 /* Since we're not going to walk subtrees, we have to do this
8889 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t
));
8893 /* Check the return type. */
8894 WALK_SUBTREE (TREE_TYPE (t
));
8896 /* Check the parameter types. Since default arguments are not
8897 instantiated until they are needed, the TYPE_ARG_TYPES may
8898 contain expressions that involve template parameters. But,
8899 no-one should be looking at them yet. And, once they're
8900 instantiated, they don't contain template parameters, so
8901 there's no point in looking at them then, either. */
8905 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
8906 WALK_SUBTREE (TREE_VALUE (parm
));
8908 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8909 want walk_tree walking into them itself. */
8913 if (flag_noexcept_type
)
8915 tree spec
= TYPE_RAISES_EXCEPTIONS (t
);
8917 WALK_SUBTREE (TREE_PURPOSE (spec
));
8922 case UNDERLYING_TYPE
:
8923 if (pfd
->include_nondeduced_p
8924 && for_each_template_parm (TYPE_VALUES_RAW (t
), fn
, data
,
8926 pfd
->include_nondeduced_p
,
8928 return error_mark_node
;
8933 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
8934 WALK_SUBTREE (DECL_TI_ARGS (t
));
8939 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
))
8940 WALK_SUBTREE (DECL_INITIAL (t
));
8941 if (DECL_CONTEXT (t
)
8942 && pfd
->include_nondeduced_p
)
8943 WALK_SUBTREE (DECL_CONTEXT (t
));
8946 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8947 /* Record template parameters such as `T' inside `TT<T>'. */
8948 WALK_SUBTREE (TYPE_TI_ARGS (t
));
8951 case TEMPLATE_TEMPLATE_PARM
:
8952 case TEMPLATE_TYPE_PARM
:
8953 case TEMPLATE_PARM_INDEX
:
8954 if (fn
&& (*fn
)(t
, data
))
8959 /* In C++17 we can deduce a type argument from the type of a non-type
8961 if (cxx_dialect
>= cxx1z
8962 && TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
8963 WALK_SUBTREE (TREE_TYPE (t
));
8967 /* A template template parameter is encountered. */
8968 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8969 WALK_SUBTREE (TREE_TYPE (t
));
8971 /* Already substituted template template parameter */
8976 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8977 partial instantiation. */
8978 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t
));
8982 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
8983 && pfd
->include_nondeduced_p
)
8984 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
8989 /* If there's no type, then this thing must be some expression
8990 involving template parameters. */
8991 if (!fn
&& !TREE_TYPE (t
))
8992 return error_mark_node
;
8997 case IMPLICIT_CONV_EXPR
:
8998 case REINTERPRET_CAST_EXPR
:
8999 case CONST_CAST_EXPR
:
9000 case STATIC_CAST_EXPR
:
9001 case DYNAMIC_CAST_EXPR
:
9005 case PSEUDO_DTOR_EXPR
:
9007 return error_mark_node
;
9016 /* We didn't find any template parameters we liked. */
9021 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9022 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9023 call FN with the parameter and the DATA.
9024 If FN returns nonzero, the iteration is terminated, and
9025 for_each_template_parm returns 1. Otherwise, the iteration
9026 continues. If FN never returns a nonzero value, the value
9027 returned by for_each_template_parm is 0. If FN is NULL, it is
9028 considered to be the function which always returns 1.
9030 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9031 parameters that occur in non-deduced contexts. When false, only
9032 visits those template parameters that can be deduced. */
9035 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
9036 hash_set
<tree
> *visited
,
9037 bool include_nondeduced_p
,
9040 struct pair_fn_data pfd
;
9045 pfd
.any_fn
= any_fn
;
9047 pfd
.include_nondeduced_p
= include_nondeduced_p
;
9049 /* Walk the tree. (Conceptually, we would like to walk without
9050 duplicates, but for_each_template_parm_r recursively calls
9051 for_each_template_parm, so we would need to reorganize a fair
9052 bit to use walk_tree_without_duplicates, so we keep our own
9055 pfd
.visited
= visited
;
9057 pfd
.visited
= new hash_set
<tree
>;
9058 result
= cp_walk_tree (&t
,
9059 for_each_template_parm_r
,
9073 /* Returns true if T depends on any template parameter. */
9076 uses_template_parms (tree t
)
9082 int saved_processing_template_decl
;
9084 saved_processing_template_decl
= processing_template_decl
;
9085 if (!saved_processing_template_decl
)
9086 processing_template_decl
= 1;
9088 dependent_p
= dependent_type_p (t
);
9089 else if (TREE_CODE (t
) == TREE_VEC
)
9090 dependent_p
= any_dependent_template_arguments_p (t
);
9091 else if (TREE_CODE (t
) == TREE_LIST
)
9092 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
9093 || uses_template_parms (TREE_CHAIN (t
)));
9094 else if (TREE_CODE (t
) == TYPE_DECL
)
9095 dependent_p
= dependent_type_p (TREE_TYPE (t
));
9098 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
9099 || TREE_CODE (t
) == OVERLOAD
9102 || TREE_CODE (t
) == TRAIT_EXPR
9103 || TREE_CODE (t
) == CONSTRUCTOR
9104 || CONSTANT_CLASS_P (t
))
9105 dependent_p
= (type_dependent_expression_p (t
)
9106 || value_dependent_expression_p (t
));
9109 gcc_assert (t
== error_mark_node
);
9110 dependent_p
= false;
9113 processing_template_decl
= saved_processing_template_decl
;
9118 /* Returns true iff current_function_decl is an incompletely instantiated
9119 template. Useful instead of processing_template_decl because the latter
9120 is set to 0 during instantiate_non_dependent_expr. */
9123 in_template_function (void)
9125 tree fn
= current_function_decl
;
9127 ++processing_template_decl
;
9128 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
9129 && DECL_TEMPLATE_INFO (fn
)
9130 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
9131 --processing_template_decl
;
9135 /* Returns true if T depends on any template parameter with level LEVEL. */
9138 uses_template_parms_level (tree t
, int level
)
9140 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
9141 /*include_nondeduced_p=*/true);
9144 /* Returns true if the signature of DECL depends on any template parameter from
9145 its enclosing class. */
9148 uses_outer_template_parms (tree decl
)
9150 int depth
= template_class_depth (CP_DECL_CONTEXT (decl
));
9153 if (for_each_template_parm (TREE_TYPE (decl
), template_parm_outer_level
,
9154 &depth
, NULL
, /*include_nondeduced_p=*/true))
9156 if (PRIMARY_TEMPLATE_P (decl
)
9157 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9158 (DECL_TEMPLATE_PARMS (decl
)),
9159 template_parm_outer_level
,
9160 &depth
, NULL
, /*include_nondeduced_p=*/true))
9162 tree ci
= get_constraints (decl
);
9164 ci
= CI_ASSOCIATED_CONSTRAINTS (ci
);
9165 if (ci
&& for_each_template_parm (ci
, template_parm_outer_level
,
9166 &depth
, NULL
, /*nondeduced*/true))
9171 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9172 ill-formed translation unit, i.e. a variable or function that isn't
9173 usable in a constant expression. */
9176 neglectable_inst_p (tree d
)
9179 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
9180 : decl_maybe_constant_var_p (d
)));
9183 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9184 neglectable and instantiated from within an erroneous instantiation. */
9187 limit_bad_template_recursion (tree decl
)
9189 struct tinst_level
*lev
= current_tinst_level
;
9190 int errs
= errorcount
+ sorrycount
;
9191 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
9194 for (; lev
; lev
= lev
->next
)
9195 if (neglectable_inst_p (lev
->decl
))
9198 return (lev
&& errs
> lev
->errors
);
9201 static int tinst_depth
;
9202 extern int max_tinst_depth
;
9205 static GTY(()) struct tinst_level
*last_error_tinst_level
;
9207 /* We're starting to instantiate D; record the template instantiation context
9208 for diagnostics and to restore it later. */
9211 push_tinst_level (tree d
)
9213 return push_tinst_level_loc (d
, input_location
);
9216 /* We're starting to instantiate D; record the template instantiation context
9217 at LOC for diagnostics and to restore it later. */
9220 push_tinst_level_loc (tree d
, location_t loc
)
9222 struct tinst_level
*new_level
;
9224 if (tinst_depth
>= max_tinst_depth
)
9226 /* Tell error.c not to try to instantiate any templates. */
9228 fatal_error (input_location
,
9229 "template instantiation depth exceeds maximum of %d"
9230 " (use -ftemplate-depth= to increase the maximum)",
9235 /* If the current instantiation caused problems, don't let it instantiate
9236 anything else. Do allow deduction substitution and decls usable in
9237 constant expressions. */
9238 if (limit_bad_template_recursion (d
))
9241 /* When not -quiet, dump template instantiations other than functions, since
9242 announce_function will take care of those. */
9244 && TREE_CODE (d
) != TREE_LIST
9245 && TREE_CODE (d
) != FUNCTION_DECL
)
9246 fprintf (stderr
, " %s", decl_as_string (d
, TFF_DECL_SPECIFIERS
));
9248 new_level
= ggc_alloc
<tinst_level
> ();
9249 new_level
->decl
= d
;
9250 new_level
->locus
= loc
;
9251 new_level
->errors
= errorcount
+sorrycount
;
9252 new_level
->in_system_header_p
= in_system_header_at (input_location
);
9253 new_level
->next
= current_tinst_level
;
9254 current_tinst_level
= new_level
;
9257 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
9258 depth_reached
= tinst_depth
;
9263 /* We're done instantiating this template; return to the instantiation
9267 pop_tinst_level (void)
9269 /* Restore the filename and line number stashed away when we started
9270 this instantiation. */
9271 input_location
= current_tinst_level
->locus
;
9272 current_tinst_level
= current_tinst_level
->next
;
9276 /* We're instantiating a deferred template; restore the template
9277 instantiation context in which the instantiation was requested, which
9278 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9281 reopen_tinst_level (struct tinst_level
*level
)
9283 struct tinst_level
*t
;
9286 for (t
= level
; t
; t
= t
->next
)
9289 current_tinst_level
= level
;
9291 if (current_tinst_level
)
9292 current_tinst_level
->errors
= errorcount
+sorrycount
;
9296 /* Returns the TINST_LEVEL which gives the original instantiation
9299 struct tinst_level
*
9300 outermost_tinst_level (void)
9302 struct tinst_level
*level
= current_tinst_level
;
9305 level
= level
->next
;
9309 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9310 vector of template arguments, as for tsubst.
9312 Returns an appropriate tsubst'd friend declaration. */
9315 tsubst_friend_function (tree decl
, tree args
)
9319 if (TREE_CODE (decl
) == FUNCTION_DECL
9320 && DECL_TEMPLATE_INSTANTIATION (decl
)
9321 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
9322 /* This was a friend declared with an explicit template
9323 argument list, e.g.:
9327 to indicate that f was a template instantiation, not a new
9328 function declaration. Now, we have to figure out what
9329 instantiation of what template. */
9331 tree template_id
, arglist
, fns
;
9334 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
9336 /* Friend functions are looked up in the containing namespace scope.
9337 We must enter that scope, to avoid finding member functions of the
9338 current class with same name. */
9339 push_nested_namespace (ns
);
9340 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
9341 tf_warning_or_error
, NULL_TREE
,
9342 /*integral_constant_expression_p=*/false);
9343 pop_nested_namespace (ns
);
9344 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
9345 tf_warning_or_error
, NULL_TREE
);
9346 template_id
= lookup_template_function (fns
, arglist
);
9348 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9349 tmpl
= determine_specialization (template_id
, new_friend
,
9351 /*need_member_template=*/0,
9352 TREE_VEC_LENGTH (args
),
9354 return instantiate_template (tmpl
, new_args
, tf_error
);
9357 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
9359 /* The NEW_FRIEND will look like an instantiation, to the
9360 compiler, but is not an instantiation from the point of view of
9361 the language. For example, we might have had:
9363 template <class T> struct S {
9364 template <class U> friend void f(T, U);
9367 Then, in S<int>, template <class U> void f(int, U) is not an
9368 instantiation of anything. */
9369 if (new_friend
== error_mark_node
)
9370 return error_mark_node
;
9372 DECL_USE_TEMPLATE (new_friend
) = 0;
9373 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
9375 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
9376 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
9377 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
9380 /* The mangled name for the NEW_FRIEND is incorrect. The function
9381 is not a template instantiation and should not be mangled like
9382 one. Therefore, we forget the mangling here; we'll recompute it
9383 later if we need it. */
9384 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
9386 SET_DECL_RTL (new_friend
, NULL
);
9387 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
9390 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
9393 tree new_friend_template_info
;
9394 tree new_friend_result_template_info
;
9396 int new_friend_is_defn
;
9398 /* We must save some information from NEW_FRIEND before calling
9399 duplicate decls since that function will free NEW_FRIEND if
9401 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
9402 new_friend_is_defn
=
9403 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9404 (template_for_substitution (new_friend
)))
9406 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
9408 /* This declaration is a `primary' template. */
9409 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
9411 new_friend_result_template_info
9412 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
9415 new_friend_result_template_info
= NULL_TREE
;
9417 /* Inside pushdecl_namespace_level, we will push into the
9418 current namespace. However, the friend function should go
9419 into the namespace of the template. */
9420 ns
= decl_namespace_context (new_friend
);
9421 push_nested_namespace (ns
);
9422 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
9423 pop_nested_namespace (ns
);
9425 if (old_decl
== error_mark_node
)
9426 return error_mark_node
;
9428 if (old_decl
!= new_friend
)
9430 /* This new friend declaration matched an existing
9431 declaration. For example, given:
9433 template <class T> void f(T);
9434 template <class U> class C {
9435 template <class T> friend void f(T) {}
9438 the friend declaration actually provides the definition
9439 of `f', once C has been instantiated for some type. So,
9440 old_decl will be the out-of-class template declaration,
9441 while new_friend is the in-class definition.
9443 But, if `f' was called before this point, the
9444 instantiation of `f' will have DECL_TI_ARGS corresponding
9445 to `T' but not to `U', references to which might appear
9446 in the definition of `f'. Previously, the most general
9447 template for an instantiation of `f' was the out-of-class
9448 version; now it is the in-class version. Therefore, we
9449 run through all specialization of `f', adding to their
9450 DECL_TI_ARGS appropriately. In particular, they need a
9451 new set of outer arguments, corresponding to the
9452 arguments for this class instantiation.
9454 The same situation can arise with something like this:
9457 template <class T> class C {
9461 when `C<int>' is instantiated. Now, `f(int)' is defined
9464 if (!new_friend_is_defn
)
9465 /* On the other hand, if the in-class declaration does
9466 *not* provide a definition, then we don't want to alter
9467 existing definitions. We can just leave everything
9472 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
9473 tree new_args
= TI_ARGS (new_friend_template_info
);
9475 /* Overwrite whatever template info was there before, if
9476 any, with the new template information pertaining to
9478 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
9480 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
9482 /* We should have called reregister_specialization in
9484 gcc_assert (retrieve_specialization (new_template
,
9488 /* Instantiate it if the global has already been used. */
9489 if (DECL_ODR_USED (old_decl
))
9490 instantiate_decl (old_decl
, /*defer_ok=*/true,
9491 /*expl_inst_class_mem_p=*/false);
9497 /* Indicate that the old function template is a partial
9499 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
9500 = new_friend_result_template_info
;
9502 gcc_assert (new_template
9503 == most_general_template (new_template
));
9504 gcc_assert (new_template
!= old_decl
);
9506 /* Reassign any specializations already in the hash table
9507 to the new more general template, and add the
9508 additional template args. */
9509 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
9513 tree spec
= TREE_VALUE (t
);
9516 elt
.tmpl
= old_decl
;
9517 elt
.args
= DECL_TI_ARGS (spec
);
9518 elt
.spec
= NULL_TREE
;
9520 decl_specializations
->remove_elt (&elt
);
9523 = add_outermost_template_args (new_args
,
9524 DECL_TI_ARGS (spec
));
9526 register_specialization
9527 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
9530 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
9534 /* The information from NEW_FRIEND has been merged into OLD_DECL
9535 by duplicate_decls. */
9536 new_friend
= old_decl
;
9541 tree context
= DECL_CONTEXT (new_friend
);
9545 template <class T> class C {
9546 template <class U> friend void C1<U>::f (); // case 1
9547 friend void C2<T>::f (); // case 2
9549 we only need to make sure CONTEXT is a complete type for
9550 case 2. To distinguish between the two cases, we note that
9551 CONTEXT of case 1 remains dependent type after tsubst while
9552 this isn't true for case 2. */
9553 ++processing_template_decl
;
9554 dependent_p
= dependent_type_p (context
);
9555 --processing_template_decl
;
9558 && !complete_type_or_else (context
, NULL_TREE
))
9559 return error_mark_node
;
9561 if (COMPLETE_TYPE_P (context
))
9563 tree fn
= new_friend
;
9564 /* do_friend adds the TEMPLATE_DECL for any member friend
9565 template even if it isn't a member template, i.e.
9566 template <class T> friend A<T>::f();
9567 Look through it in that case. */
9568 if (TREE_CODE (fn
) == TEMPLATE_DECL
9569 && !PRIMARY_TEMPLATE_P (fn
))
9570 fn
= DECL_TEMPLATE_RESULT (fn
);
9571 /* Check to see that the declaration is really present, and,
9572 possibly obtain an improved declaration. */
9573 fn
= check_classfn (context
, fn
, NULL_TREE
);
9583 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9584 template arguments, as for tsubst.
9586 Returns an appropriate tsubst'd friend type or error_mark_node on
9590 tsubst_friend_class (tree friend_tmpl
, tree args
)
9596 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
9598 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
9599 return TREE_TYPE (t
);
9602 context
= CP_DECL_CONTEXT (friend_tmpl
);
9604 if (context
!= global_namespace
)
9606 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9607 push_nested_namespace (context
);
9609 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
9612 /* Look for a class template declaration. We look for hidden names
9613 because two friend declarations of the same template are the
9614 same. For example, in:
9617 template <typename> friend class F;
9619 template <typename> struct B {
9620 template <typename> friend class F;
9623 both F templates are the same. */
9624 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
9625 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
9627 /* But, if we don't find one, it might be because we're in a
9628 situation like this:
9636 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9637 for `S<int>', not the TEMPLATE_DECL. */
9638 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
9640 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
9641 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
9644 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
9646 /* The friend template has already been declared. Just
9647 check to see that the declarations match, and install any new
9648 default parameters. We must tsubst the default parameters,
9649 of course. We only need the innermost template parameters
9650 because that is all that redeclare_class_template will look
9652 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
9653 > TMPL_ARGS_DEPTH (args
))
9656 location_t saved_input_location
;
9657 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
9658 args
, tf_warning_or_error
);
9660 saved_input_location
= input_location
;
9661 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
9662 tree cons
= get_constraints (tmpl
);
9663 redeclare_class_template (TREE_TYPE (tmpl
), parms
, cons
);
9664 input_location
= saved_input_location
;
9668 friend_type
= TREE_TYPE (tmpl
);
9672 /* The friend template has not already been declared. In this
9673 case, the instantiation of the template class will cause the
9674 injection of this template into the global scope. */
9675 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
9676 if (tmpl
== error_mark_node
)
9677 return error_mark_node
;
9679 /* The new TMPL is not an instantiation of anything, so we
9680 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9681 the new type because that is supposed to be the corresponding
9682 template decl, i.e., TMPL. */
9683 DECL_USE_TEMPLATE (tmpl
) = 0;
9684 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
9685 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
9686 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
9687 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
9689 /* Inject this template into the global scope. */
9690 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
9693 if (context
!= global_namespace
)
9695 if (TREE_CODE (context
) == NAMESPACE_DECL
)
9696 pop_nested_namespace (context
);
9698 pop_nested_class ();
9704 /* Returns zero if TYPE cannot be completed later due to circularity.
9705 Otherwise returns one. */
9708 can_complete_type_without_circularity (tree type
)
9710 if (type
== NULL_TREE
|| type
== error_mark_node
)
9712 else if (COMPLETE_TYPE_P (type
))
9714 else if (TREE_CODE (type
) == ARRAY_TYPE
)
9715 return can_complete_type_without_circularity (TREE_TYPE (type
));
9716 else if (CLASS_TYPE_P (type
)
9717 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
9723 static tree
tsubst_omp_clauses (tree
, enum c_omp_region_type
, tree
,
9724 tsubst_flags_t
, tree
);
9726 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9727 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9730 tsubst_attribute (tree t
, tree
*decl_p
, tree args
,
9731 tsubst_flags_t complain
, tree in_decl
)
9733 gcc_assert (ATTR_IS_DEPENDENT (t
));
9735 tree val
= TREE_VALUE (t
);
9736 if (val
== NULL_TREE
)
9737 /* Nothing to do. */;
9738 else if ((flag_openmp
|| flag_openmp_simd
|| flag_cilkplus
)
9739 && is_attribute_p ("omp declare simd",
9740 get_attribute_name (t
)))
9742 tree clauses
= TREE_VALUE (val
);
9743 clauses
= tsubst_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
, args
,
9745 c_omp_declare_simd_clauses_to_decls (*decl_p
, clauses
);
9746 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
9747 tree parms
= DECL_ARGUMENTS (*decl_p
);
9749 = c_omp_declare_simd_clauses_to_numbers (parms
, clauses
);
9751 val
= build_tree_list (NULL_TREE
, clauses
);
9755 /* If the first attribute argument is an identifier, don't
9756 pass it through tsubst. Attributes like mode, format,
9757 cleanup and several target specific attributes expect it
9759 else if (attribute_takes_identifier_p (get_attribute_name (t
)))
9762 = tsubst_expr (TREE_CHAIN (val
), args
, complain
, in_decl
,
9763 /*integral_constant_expression_p=*/false);
9764 if (chain
!= TREE_CHAIN (val
))
9765 val
= tree_cons (NULL_TREE
, TREE_VALUE (val
), chain
);
9767 else if (PACK_EXPANSION_P (val
))
9769 /* An attribute pack expansion. */
9770 tree purp
= TREE_PURPOSE (t
);
9771 tree pack
= tsubst_pack_expansion (val
, args
, complain
, in_decl
);
9772 int len
= TREE_VEC_LENGTH (pack
);
9773 tree list
= NULL_TREE
;
9775 for (int i
= 0; i
< len
; ++i
)
9777 tree elt
= TREE_VEC_ELT (pack
, i
);
9778 *q
= build_tree_list (purp
, elt
);
9779 q
= &TREE_CHAIN (*q
);
9784 val
= tsubst_expr (val
, args
, complain
, in_decl
,
9785 /*integral_constant_expression_p=*/false);
9787 if (val
!= TREE_VALUE (t
))
9788 return build_tree_list (TREE_PURPOSE (t
), val
);
9792 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9793 unchanged or a new TREE_LIST chain. */
9796 tsubst_attributes (tree attributes
, tree args
,
9797 tsubst_flags_t complain
, tree in_decl
)
9799 tree last_dep
= NULL_TREE
;
9801 for (tree t
= attributes
; t
; t
= TREE_CHAIN (t
))
9802 if (ATTR_IS_DEPENDENT (t
))
9805 attributes
= copy_list (attributes
);
9810 for (tree
*p
= &attributes
; *p
; )
9813 if (ATTR_IS_DEPENDENT (t
))
9815 tree subst
= tsubst_attribute (t
, NULL
, args
, complain
, in_decl
);
9820 p
= &TREE_CHAIN (*p
);
9822 *p
= TREE_CHAIN (t
);
9826 p
= &TREE_CHAIN (*p
);
9832 /* Apply any attributes which had to be deferred until instantiation
9833 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9834 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9837 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
9838 tree args
, tsubst_flags_t complain
, tree in_decl
)
9840 tree last_dep
= NULL_TREE
;
9844 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
9845 if (ATTR_IS_DEPENDENT (t
))
9848 attributes
= copy_list (attributes
);
9852 if (DECL_P (*decl_p
))
9854 if (TREE_TYPE (*decl_p
) == error_mark_node
)
9856 p
= &DECL_ATTRIBUTES (*decl_p
);
9859 p
= &TYPE_ATTRIBUTES (*decl_p
);
9863 tree late_attrs
= NULL_TREE
;
9864 tree
*q
= &late_attrs
;
9866 for (*p
= attributes
; *p
; )
9869 if (ATTR_IS_DEPENDENT (t
))
9871 *p
= TREE_CHAIN (t
);
9872 TREE_CHAIN (t
) = NULL_TREE
;
9873 *q
= tsubst_attribute (t
, decl_p
, args
, complain
, in_decl
);
9875 q
= &TREE_CHAIN (*q
);
9879 p
= &TREE_CHAIN (t
);
9882 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
9886 /* Perform (or defer) access check for typedefs that were referenced
9887 from within the template TMPL code.
9888 This is a subroutine of instantiate_decl and instantiate_class_template.
9889 TMPL is the template to consider and TARGS is the list of arguments of
9893 perform_typedefs_access_check (tree tmpl
, tree targs
)
9895 location_t saved_location
;
9897 qualified_typedef_usage_t
*iter
;
9900 || (!CLASS_TYPE_P (tmpl
)
9901 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
9904 saved_location
= input_location
;
9905 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
9907 tree type_decl
= iter
->typedef_decl
;
9908 tree type_scope
= iter
->context
;
9910 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
9913 if (uses_template_parms (type_decl
))
9914 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
9915 if (uses_template_parms (type_scope
))
9916 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
9918 /* Make access check error messages point to the location
9919 of the use of the typedef. */
9920 input_location
= iter
->locus
;
9921 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
9922 type_decl
, type_decl
,
9923 tf_warning_or_error
);
9925 input_location
= saved_location
;
9929 instantiate_class_template_1 (tree type
)
9931 tree templ
, args
, pattern
, t
, member
;
9935 unsigned int saved_maximum_field_alignment
;
9938 if (type
== error_mark_node
)
9939 return error_mark_node
;
9941 if (COMPLETE_OR_OPEN_TYPE_P (type
)
9942 || uses_template_parms (type
))
9945 /* Figure out which template is being instantiated. */
9946 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
9947 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
9949 /* Determine what specialization of the original template to
9951 t
= most_specialized_partial_spec (type
, tf_warning_or_error
);
9952 if (t
== error_mark_node
)
9954 TYPE_BEING_DEFINED (type
) = 1;
9955 return error_mark_node
;
9959 /* This TYPE is actually an instantiation of a partial
9960 specialization. We replace the innermost set of ARGS with
9961 the arguments appropriate for substitution. For example,
9964 template <class T> struct S {};
9965 template <class T> struct S<T*> {};
9967 and supposing that we are instantiating S<int*>, ARGS will
9968 presently be {int*} -- but we need {int}. */
9969 pattern
= TREE_TYPE (t
);
9970 args
= TREE_PURPOSE (t
);
9974 pattern
= TREE_TYPE (templ
);
9975 args
= CLASSTYPE_TI_ARGS (type
);
9978 /* If the template we're instantiating is incomplete, then clearly
9979 there's nothing we can do. */
9980 if (!COMPLETE_TYPE_P (pattern
))
9983 /* If we've recursively instantiated too many templates, stop. */
9984 if (! push_tinst_level (type
))
9987 /* Now we're really doing the instantiation. Mark the type as in
9988 the process of being defined. */
9989 TYPE_BEING_DEFINED (type
) = 1;
9991 /* We may be in the middle of deferred access check. Disable
9993 push_deferring_access_checks (dk_no_deferred
);
9995 int saved_unevaluated_operand
= cp_unevaluated_operand
;
9996 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9998 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
9999 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10000 if (!fn_context
&& LAMBDA_TYPE_P (type
) && TYPE_CLASS_SCOPE_P (type
))
10001 fn_context
= error_mark_node
;
10003 push_to_top_level ();
10006 cp_unevaluated_operand
= 0;
10007 c_inhibit_evaluation_warnings
= 0;
10009 /* Use #pragma pack from the template context. */
10010 saved_maximum_field_alignment
= maximum_field_alignment
;
10011 maximum_field_alignment
= TYPE_PRECISION (pattern
);
10013 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
10015 /* Set the input location to the most specialized template definition.
10016 This is needed if tsubsting causes an error. */
10017 typedecl
= TYPE_MAIN_DECL (pattern
);
10018 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
10019 DECL_SOURCE_LOCATION (typedecl
);
10021 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
10022 SET_TYPE_ALIGN (type
, TYPE_ALIGN (pattern
));
10023 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
10024 if (ANON_AGGR_TYPE_P (pattern
))
10025 SET_ANON_AGGR_TYPE_P (type
);
10026 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
10028 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
10029 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
10030 /* Adjust visibility for template arguments. */
10031 determine_visibility (TYPE_MAIN_DECL (type
));
10033 if (CLASS_TYPE_P (type
))
10034 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
10036 pbinfo
= TYPE_BINFO (pattern
);
10038 /* We should never instantiate a nested class before its enclosing
10039 class; we need to look up the nested class by name before we can
10040 instantiate it, and that lookup should instantiate the enclosing
10042 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
10043 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
10045 base_list
= NULL_TREE
;
10046 if (BINFO_N_BASE_BINFOS (pbinfo
))
10052 /* We must enter the scope containing the type, as that is where
10053 the accessibility of types named in dependent bases are
10055 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
10057 /* Substitute into each of the bases to determine the actual
10059 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
10062 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
10063 tree expanded_bases
= NULL_TREE
;
10066 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
10069 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
10070 args
, tf_error
, NULL_TREE
);
10071 if (expanded_bases
== error_mark_node
)
10074 len
= TREE_VEC_LENGTH (expanded_bases
);
10077 for (idx
= 0; idx
< len
; idx
++)
10079 if (expanded_bases
)
10080 /* Extract the already-expanded base class. */
10081 base
= TREE_VEC_ELT (expanded_bases
, idx
);
10083 /* Substitute to figure out the base class. */
10084 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
10087 if (base
== error_mark_node
)
10090 base_list
= tree_cons (access
, base
, base_list
);
10091 if (BINFO_VIRTUAL_P (pbase_binfo
))
10092 TREE_TYPE (base_list
) = integer_type_node
;
10096 /* The list is now in reverse order; correct that. */
10097 base_list
= nreverse (base_list
);
10100 pop_scope (pushed_scope
);
10102 /* Now call xref_basetypes to set up all the base-class
10104 xref_basetypes (type
, base_list
);
10106 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
10107 (int) ATTR_FLAG_TYPE_IN_PLACE
,
10108 args
, tf_error
, NULL_TREE
);
10109 fixup_attribute_variants (type
);
10111 /* Now that our base classes are set up, enter the scope of the
10112 class, so that name lookups into base classes, etc. will work
10113 correctly. This is precisely analogous to what we do in
10114 begin_class_definition when defining an ordinary non-template
10115 class, except we also need to push the enclosing classes. */
10116 push_nested_class (type
);
10118 /* Now members are processed in the order of declaration. */
10119 for (member
= CLASSTYPE_DECL_LIST (pattern
);
10120 member
; member
= TREE_CHAIN (member
))
10122 tree t
= TREE_VALUE (member
);
10124 if (TREE_PURPOSE (member
))
10128 /* Build new CLASSTYPE_NESTED_UTDS. */
10131 bool class_template_p
;
10133 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
10134 && TYPE_LANG_SPECIFIC (t
)
10135 && CLASSTYPE_IS_TEMPLATE (t
));
10136 /* If the member is a class template, then -- even after
10137 substitution -- there may be dependent types in the
10138 template argument list for the class. We increment
10139 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10140 that function will assume that no types are dependent
10141 when outside of a template. */
10142 if (class_template_p
)
10143 ++processing_template_decl
;
10144 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10145 if (class_template_p
)
10146 --processing_template_decl
;
10147 if (newtag
== error_mark_node
)
10150 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
10152 tree name
= TYPE_IDENTIFIER (t
);
10154 if (class_template_p
)
10155 /* Unfortunately, lookup_template_class sets
10156 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10157 instantiation (i.e., for the type of a member
10158 template class nested within a template class.)
10159 This behavior is required for
10160 maybe_process_partial_specialization to work
10161 correctly, but is not accurate in this case;
10162 the TAG is not an instantiation of anything.
10163 (The corresponding TEMPLATE_DECL is an
10164 instantiation, but the TYPE is not.) */
10165 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
10167 /* Now, we call pushtag to put this NEWTAG into the scope of
10168 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10169 pushtag calling push_template_decl. We don't have to do
10170 this for enums because it will already have been done in
10173 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
10174 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
10177 else if (DECL_DECLARES_FUNCTION_P (t
))
10179 /* Build new TYPE_METHODS. */
10182 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10183 ++processing_template_decl
;
10184 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
10185 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10186 --processing_template_decl
;
10187 set_current_access_from_decl (r
);
10188 finish_member_declaration (r
);
10189 /* Instantiate members marked with attribute used. */
10190 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10192 if (TREE_CODE (r
) == FUNCTION_DECL
10193 && DECL_OMP_DECLARE_REDUCTION_P (r
))
10194 cp_check_omp_declare_reduction (r
);
10196 else if (DECL_CLASS_TEMPLATE_P (t
)
10197 && LAMBDA_TYPE_P (TREE_TYPE (t
)))
10198 /* A closure type for a lambda in a default argument for a
10199 member template. Ignore it; it will be instantiated with
10200 the default argument. */;
10203 /* Build new TYPE_FIELDS. */
10204 if (TREE_CODE (t
) == STATIC_ASSERT
)
10208 ++c_inhibit_evaluation_warnings
;
10210 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
10211 tf_warning_or_error
, NULL_TREE
,
10212 /*integral_constant_expression_p=*/true);
10213 --c_inhibit_evaluation_warnings
;
10215 finish_static_assert (condition
,
10216 STATIC_ASSERT_MESSAGE (t
),
10217 STATIC_ASSERT_SOURCE_LOCATION (t
),
10218 /*member_p=*/true);
10220 else if (TREE_CODE (t
) != CONST_DECL
)
10223 tree vec
= NULL_TREE
;
10226 /* The file and line for this declaration, to
10227 assist in error message reporting. Since we
10228 called push_tinst_level above, we don't need to
10230 input_location
= DECL_SOURCE_LOCATION (t
);
10232 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10233 ++processing_template_decl
;
10234 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
10235 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10236 --processing_template_decl
;
10238 if (TREE_CODE (r
) == TREE_VEC
)
10240 /* A capture pack became multiple fields. */
10242 len
= TREE_VEC_LENGTH (vec
);
10245 for (int i
= 0; i
< len
; ++i
)
10248 r
= TREE_VEC_ELT (vec
, i
);
10253 [t]he initialization (and any associated
10254 side-effects) of a static data member does
10255 not occur unless the static data member is
10256 itself used in a way that requires the
10257 definition of the static data member to
10260 Therefore, we do not substitute into the
10261 initialized for the static data member here. */
10262 finish_static_data_member_decl
10264 /*init=*/NULL_TREE
,
10265 /*init_const_expr_p=*/false,
10266 /*asmspec_tree=*/NULL_TREE
,
10268 /* Instantiate members marked with attribute used. */
10269 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
10272 else if (TREE_CODE (r
) == FIELD_DECL
)
10274 /* Determine whether R has a valid type and can be
10275 completed later. If R is invalid, then its type
10276 is replaced by error_mark_node. */
10277 tree rtype
= TREE_TYPE (r
);
10278 if (can_complete_type_without_circularity (rtype
))
10279 complete_type (rtype
);
10281 if (!complete_or_array_type_p (rtype
))
10283 /* If R's type couldn't be completed and
10284 it isn't a flexible array member (whose
10285 type is incomplete by definition) give
10287 cxx_incomplete_type_error (r
, rtype
);
10288 TREE_TYPE (r
) = error_mark_node
;
10292 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10293 such a thing will already have been added to the field
10294 list by tsubst_enum in finish_member_declaration in the
10295 CLASSTYPE_NESTED_UTDS case above. */
10296 if (!(TREE_CODE (r
) == TYPE_DECL
10297 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
10298 && DECL_ARTIFICIAL (r
)))
10300 set_current_access_from_decl (r
);
10301 finish_member_declaration (r
);
10309 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
10310 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10312 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10314 tree friend_type
= t
;
10315 bool adjust_processing_template_decl
= false;
10317 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10319 /* template <class T> friend class C; */
10320 friend_type
= tsubst_friend_class (friend_type
, args
);
10321 adjust_processing_template_decl
= true;
10323 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
10325 /* template <class T> friend class C::D; */
10326 friend_type
= tsubst (friend_type
, args
,
10327 tf_warning_or_error
, NULL_TREE
);
10328 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
10329 friend_type
= TREE_TYPE (friend_type
);
10330 adjust_processing_template_decl
= true;
10332 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
10333 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
10335 /* This could be either
10339 when dependent_type_p is false or
10341 template <class U> friend class T::C;
10344 /* Bump processing_template_decl in case this is something like
10345 template <class T> friend struct A<T>::B. */
10346 ++processing_template_decl
;
10347 friend_type
= tsubst (friend_type
, args
,
10348 tf_warning_or_error
, NULL_TREE
);
10349 if (dependent_type_p (friend_type
))
10350 adjust_processing_template_decl
= true;
10351 --processing_template_decl
;
10353 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
10354 && hidden_name_p (TYPE_NAME (friend_type
)))
10358 where C hasn't been declared yet. Let's lookup name
10359 from namespace scope directly, bypassing any name that
10360 come from dependent base class. */
10361 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
10363 /* The call to xref_tag_from_type does injection for friend
10365 push_nested_namespace (ns
);
10367 xref_tag_from_type (friend_type
, NULL_TREE
,
10368 /*tag_scope=*/ts_current
);
10369 pop_nested_namespace (ns
);
10371 else if (uses_template_parms (friend_type
))
10372 /* friend class C<T>; */
10373 friend_type
= tsubst (friend_type
, args
,
10374 tf_warning_or_error
, NULL_TREE
);
10379 where C is already declared or
10381 friend class C<int>;
10383 We don't have to do anything in these cases. */
10385 if (adjust_processing_template_decl
)
10386 /* Trick make_friend_class into realizing that the friend
10387 we're adding is a template, not an ordinary class. It's
10388 important that we use make_friend_class since it will
10389 perform some error-checking and output cross-reference
10391 ++processing_template_decl
;
10393 if (friend_type
!= error_mark_node
)
10394 make_friend_class (type
, friend_type
, /*complain=*/false);
10396 if (adjust_processing_template_decl
)
10397 --processing_template_decl
;
10401 /* Build new DECL_FRIENDLIST. */
10404 /* The file and line for this declaration, to
10405 assist in error message reporting. Since we
10406 called push_tinst_level above, we don't need to
10408 input_location
= DECL_SOURCE_LOCATION (t
);
10410 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10412 ++processing_template_decl
;
10413 push_deferring_access_checks (dk_no_check
);
10416 r
= tsubst_friend_function (t
, args
);
10417 add_friend (type
, r
, /*complain=*/false);
10418 if (TREE_CODE (t
) == TEMPLATE_DECL
)
10420 pop_deferring_access_checks ();
10421 --processing_template_decl
;
10429 /* Restore these before substituting into the lambda capture
10431 cp_unevaluated_operand
= saved_unevaluated_operand
;
10432 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
10435 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
10437 tree decl
= lambda_function (type
);
10440 if (cxx_dialect
>= cxx1z
)
10441 CLASSTYPE_LITERAL_P (type
) = true;
10443 if (!DECL_TEMPLATE_INFO (decl
)
10444 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
10446 /* Set function_depth to avoid garbage collection. */
10448 instantiate_decl (decl
, false, false);
10452 /* We need to instantiate the capture list from the template
10453 after we've instantiated the closure members, but before we
10454 consider adding the conversion op. Also keep any captures
10455 that may have been added during instantiation of the op(). */
10456 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
10458 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
10459 args
, tf_warning_or_error
, NULL_TREE
,
10462 LAMBDA_EXPR_CAPTURE_LIST (expr
)
10463 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
10465 maybe_add_lambda_conv_op (type
);
10468 gcc_assert (errorcount
);
10471 /* Set the file and line number information to whatever is given for
10472 the class itself. This puts error messages involving generated
10473 implicit functions at a predictable point, and the same point
10474 that would be used for non-template classes. */
10475 input_location
= DECL_SOURCE_LOCATION (typedecl
);
10477 unreverse_member_declarations (type
);
10478 finish_struct_1 (type
);
10479 TYPE_BEING_DEFINED (type
) = 0;
10481 /* We don't instantiate default arguments for member functions. 14.7.1:
10483 The implicit instantiation of a class template specialization causes
10484 the implicit instantiation of the declarations, but not of the
10485 definitions or default arguments, of the class member functions,
10486 member classes, static data members and member templates.... */
10488 /* Some typedefs referenced from within the template code need to be access
10489 checked at template instantiation time, i.e now. These types were
10490 added to the template at parsing time. Let's get those and perform
10491 the access checks then. */
10492 perform_typedefs_access_check (pattern
, args
);
10493 perform_deferred_access_checks (tf_warning_or_error
);
10494 pop_nested_class ();
10495 maximum_field_alignment
= saved_maximum_field_alignment
;
10497 pop_from_top_level ();
10498 pop_deferring_access_checks ();
10499 pop_tinst_level ();
10501 /* The vtable for a template class can be emitted in any translation
10502 unit in which the class is instantiated. When there is no key
10503 method, however, finish_struct_1 will already have added TYPE to
10504 the keyed_classes list. */
10505 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
10506 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
10511 /* Wrapper for instantiate_class_template_1. */
10514 instantiate_class_template (tree type
)
10517 timevar_push (TV_TEMPLATE_INST
);
10518 ret
= instantiate_class_template_1 (type
);
10519 timevar_pop (TV_TEMPLATE_INST
);
10524 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10530 else if (TYPE_P (t
))
10531 r
= tsubst (t
, args
, complain
, in_decl
);
10534 if (!(complain
& tf_warning
))
10535 ++c_inhibit_evaluation_warnings
;
10536 r
= tsubst_expr (t
, args
, complain
, in_decl
,
10537 /*integral_constant_expression_p=*/true);
10538 if (!(complain
& tf_warning
))
10539 --c_inhibit_evaluation_warnings
;
10544 /* Given a function parameter pack TMPL_PARM and some function parameters
10545 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10546 and set *SPEC_P to point at the next point in the list. */
10549 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
10551 /* Collect all of the extra "packed" parameters into an
10555 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
10556 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10557 tree spec_parm
= *spec_p
;
10560 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
10562 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
10565 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10566 parmvec
= make_tree_vec (len
);
10567 parmtypevec
= make_tree_vec (len
);
10568 spec_parm
= *spec_p
;
10569 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
10571 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
10572 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
10575 /* Build the argument packs. */
10576 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
10577 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
10578 TREE_TYPE (argpack
) = argtypepack
;
10579 *spec_p
= spec_parm
;
10584 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10585 NONTYPE_ARGUMENT_PACK. */
10588 make_fnparm_pack (tree spec_parm
)
10590 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
10593 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10594 pack expansion with no extra args, 2 if it has extra args, or 0
10595 if it is not a pack expansion. */
10598 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
10600 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
10601 if (i
>= TREE_VEC_LENGTH (vec
))
10603 tree elt
= TREE_VEC_ELT (vec
, i
);
10605 /* A decl pack is itself an expansion. */
10606 elt
= TREE_TYPE (elt
);
10607 if (!PACK_EXPANSION_P (elt
))
10609 if (PACK_EXPANSION_EXTRA_ARGS (elt
))
10615 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10618 make_argument_pack_select (tree arg_pack
, unsigned index
)
10620 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
10622 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
10623 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10628 /* This is a subroutine of tsubst_pack_expansion.
10630 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10631 mechanism to store the (non complete list of) arguments of the
10632 substitution and return a non substituted pack expansion, in order
10633 to wait for when we have enough arguments to really perform the
10637 use_pack_expansion_extra_args_p (tree parm_packs
,
10639 bool has_empty_arg
)
10641 /* If one pack has an expansion and another pack has a normal
10642 argument or if one pack has an empty argument and an another
10643 one hasn't then tsubst_pack_expansion cannot perform the
10644 substitution and need to fall back on the
10645 PACK_EXPANSION_EXTRA mechanism. */
10646 if (parm_packs
== NULL_TREE
)
10648 else if (has_empty_arg
)
10651 bool has_expansion_arg
= false;
10652 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
10654 bool has_non_expansion_arg
= false;
10655 for (tree parm_pack
= parm_packs
;
10657 parm_pack
= TREE_CHAIN (parm_pack
))
10659 tree arg
= TREE_VALUE (parm_pack
);
10661 int exp
= argument_pack_element_is_expansion_p (arg
, i
);
10663 /* We can't substitute a pack expansion with extra args into
10667 has_expansion_arg
= true;
10669 has_non_expansion_arg
= true;
10672 if (has_expansion_arg
&& has_non_expansion_arg
)
10678 /* [temp.variadic]/6 says that:
10680 The instantiation of a pack expansion [...]
10681 produces a list E1,E2, ..., En, where N is the number of elements
10682 in the pack expansion parameters.
10684 This subroutine of tsubst_pack_expansion produces one of these Ei.
10686 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10687 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10688 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10689 INDEX is the index 'i' of the element Ei to produce. ARGS,
10690 COMPLAIN, and IN_DECL are the same parameters as for the
10691 tsubst_pack_expansion function.
10693 The function returns the resulting Ei upon successful completion,
10694 or error_mark_node.
10696 Note that this function possibly modifies the ARGS parameter, so
10697 it's the responsibility of the caller to restore it. */
10700 gen_elem_of_pack_expansion_instantiation (tree pattern
,
10703 tree args
/* This parm gets
10705 tsubst_flags_t complain
,
10709 bool ith_elem_is_expansion
= false;
10711 /* For each parameter pack, change the substitution of the parameter
10712 pack to the ith argument in its argument pack, then expand the
10714 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
10716 tree parm
= TREE_PURPOSE (pack
);
10717 tree arg_pack
= TREE_VALUE (pack
);
10718 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
10720 ith_elem_is_expansion
|=
10721 argument_pack_element_is_expansion_p (arg_pack
, index
);
10723 /* Select the Ith argument from the pack. */
10724 if (TREE_CODE (parm
) == PARM_DECL
10725 || TREE_CODE (parm
) == FIELD_DECL
)
10729 aps
= make_argument_pack_select (arg_pack
, index
);
10730 if (!mark_used (parm
, complain
) && !(complain
& tf_error
))
10731 return error_mark_node
;
10732 register_local_specialization (aps
, parm
);
10735 aps
= retrieve_local_specialization (parm
);
10740 template_parm_level_and_index (parm
, &level
, &idx
);
10744 aps
= make_argument_pack_select (arg_pack
, index
);
10745 /* Update the corresponding argument. */
10746 TMPL_ARG (args
, level
, idx
) = aps
;
10749 /* Re-use the ARGUMENT_PACK_SELECT. */
10750 aps
= TMPL_ARG (args
, level
, idx
);
10752 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
10755 /* Substitute into the PATTERN with the (possibly altered)
10757 if (pattern
== in_decl
)
10758 /* Expanding a fixed parameter pack from
10759 coerce_template_parameter_pack. */
10760 t
= tsubst_decl (pattern
, args
, complain
);
10761 else if (pattern
== error_mark_node
)
10762 t
= error_mark_node
;
10763 else if (constraint_p (pattern
))
10765 if (processing_template_decl
)
10766 t
= tsubst_constraint (pattern
, args
, complain
, in_decl
);
10768 t
= (constraints_satisfied_p (pattern
, args
)
10769 ? boolean_true_node
: boolean_false_node
);
10771 else if (!TYPE_P (pattern
))
10772 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
10773 /*integral_constant_expression_p=*/false);
10775 t
= tsubst (pattern
, args
, complain
, in_decl
);
10777 /* If the Ith argument pack element is a pack expansion, then
10778 the Ith element resulting from the substituting is going to
10779 be a pack expansion as well. */
10780 if (ith_elem_is_expansion
)
10781 t
= make_pack_expansion (t
);
10786 /* When the unexpanded parameter pack in a fold expression expands to an empty
10787 sequence, the value of the expression is as follows; the program is
10788 ill-formed if the operator is not listed in this table.
10795 expand_empty_fold (tree t
, tsubst_flags_t complain
)
10797 tree_code code
= (tree_code
)TREE_INT_CST_LOW (TREE_OPERAND (t
, 0));
10798 if (!FOLD_EXPR_MODIFY_P (t
))
10801 case TRUTH_ANDIF_EXPR
:
10802 return boolean_true_node
;
10803 case TRUTH_ORIF_EXPR
:
10804 return boolean_false_node
;
10805 case COMPOUND_EXPR
:
10811 if (complain
& tf_error
)
10812 error_at (location_of (t
),
10813 "fold of empty expansion over %O", code
);
10814 return error_mark_node
;
10817 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10818 form an expression that combines the two terms using the
10822 fold_expression (tree t
, tree left
, tree right
, tsubst_flags_t complain
)
10824 tree op
= FOLD_EXPR_OP (t
);
10825 tree_code code
= (tree_code
)TREE_INT_CST_LOW (op
);
10827 // Handle compound assignment operators.
10828 if (FOLD_EXPR_MODIFY_P (t
))
10829 return build_x_modify_expr (input_location
, left
, code
, right
, complain
);
10833 case COMPOUND_EXPR
:
10834 return build_x_compound_expr (input_location
, left
, right
, complain
);
10836 return build_m_component_ref (left
, right
, complain
);
10838 return build_x_binary_op (input_location
, code
,
10839 left
, TREE_CODE (left
),
10840 right
, TREE_CODE (right
),
10846 /* Substitute ARGS into the pack of a fold expression T. */
10849 tsubst_fold_expr_pack (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10851 return tsubst_pack_expansion (FOLD_EXPR_PACK (t
), args
, complain
, in_decl
);
10854 /* Substitute ARGS into the pack of a fold expression T. */
10857 tsubst_fold_expr_init (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10859 return tsubst_expr (FOLD_EXPR_INIT (t
), args
, complain
, in_decl
, false);
10862 /* Expand a PACK of arguments into a grouped as left fold.
10863 Given a pack containing elements A0, A1, ..., An and an
10864 operator @, this builds the expression:
10866 ((A0 @ A1) @ A2) ... @ An
10868 Note that PACK must not be empty.
10870 The operator is defined by the original fold expression T. */
10873 expand_left_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10875 tree left
= TREE_VEC_ELT (pack
, 0);
10876 for (int i
= 1; i
< TREE_VEC_LENGTH (pack
); ++i
)
10878 tree right
= TREE_VEC_ELT (pack
, i
);
10879 left
= fold_expression (t
, left
, right
, complain
);
10884 /* Substitute into a unary left fold expression. */
10887 tsubst_unary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10890 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10891 if (pack
== error_mark_node
)
10892 return error_mark_node
;
10893 if (PACK_EXPANSION_P (pack
))
10895 tree r
= copy_node (t
);
10896 FOLD_EXPR_PACK (r
) = pack
;
10899 if (TREE_VEC_LENGTH (pack
) == 0)
10900 return expand_empty_fold (t
, complain
);
10902 return expand_left_fold (t
, pack
, complain
);
10905 /* Substitute into a binary left fold expression.
10907 Do ths by building a single (non-empty) vector of argumnts and
10908 building the expression from those elements. */
10911 tsubst_binary_left_fold (tree t
, tree args
, tsubst_flags_t complain
,
10914 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10915 if (pack
== error_mark_node
)
10916 return error_mark_node
;
10917 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10918 if (init
== error_mark_node
)
10919 return error_mark_node
;
10921 if (PACK_EXPANSION_P (pack
))
10923 tree r
= copy_node (t
);
10924 FOLD_EXPR_PACK (r
) = pack
;
10925 FOLD_EXPR_INIT (r
) = init
;
10929 tree vec
= make_tree_vec (TREE_VEC_LENGTH (pack
) + 1);
10930 TREE_VEC_ELT (vec
, 0) = init
;
10931 for (int i
= 0; i
< TREE_VEC_LENGTH (pack
); ++i
)
10932 TREE_VEC_ELT (vec
, i
+ 1) = TREE_VEC_ELT (pack
, i
);
10934 return expand_left_fold (t
, vec
, complain
);
10937 /* Expand a PACK of arguments into a grouped as right fold.
10938 Given a pack containing elementns A0, A1, ..., and an
10939 operator @, this builds the expression:
10941 A0@ ... (An-2 @ (An-1 @ An))
10943 Note that PACK must not be empty.
10945 The operator is defined by the original fold expression T. */
10948 expand_right_fold (tree t
, tree pack
, tsubst_flags_t complain
)
10950 // Build the expression.
10951 int n
= TREE_VEC_LENGTH (pack
);
10952 tree right
= TREE_VEC_ELT (pack
, n
- 1);
10953 for (--n
; n
!= 0; --n
)
10955 tree left
= TREE_VEC_ELT (pack
, n
- 1);
10956 right
= fold_expression (t
, left
, right
, complain
);
10961 /* Substitute into a unary right fold expression. */
10964 tsubst_unary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10967 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10968 if (pack
== error_mark_node
)
10969 return error_mark_node
;
10970 if (PACK_EXPANSION_P (pack
))
10972 tree r
= copy_node (t
);
10973 FOLD_EXPR_PACK (r
) = pack
;
10976 if (TREE_VEC_LENGTH (pack
) == 0)
10977 return expand_empty_fold (t
, complain
);
10979 return expand_right_fold (t
, pack
, complain
);
10982 /* Substitute into a binary right fold expression.
10984 Do ths by building a single (non-empty) vector of arguments and
10985 building the expression from those elements. */
10988 tsubst_binary_right_fold (tree t
, tree args
, tsubst_flags_t complain
,
10991 tree pack
= tsubst_fold_expr_pack (t
, args
, complain
, in_decl
);
10992 if (pack
== error_mark_node
)
10993 return error_mark_node
;
10994 tree init
= tsubst_fold_expr_init (t
, args
, complain
, in_decl
);
10995 if (init
== error_mark_node
)
10996 return error_mark_node
;
10998 if (PACK_EXPANSION_P (pack
))
11000 tree r
= copy_node (t
);
11001 FOLD_EXPR_PACK (r
) = pack
;
11002 FOLD_EXPR_INIT (r
) = init
;
11006 int n
= TREE_VEC_LENGTH (pack
);
11007 tree vec
= make_tree_vec (n
+ 1);
11008 for (int i
= 0; i
< n
; ++i
)
11009 TREE_VEC_ELT (vec
, i
) = TREE_VEC_ELT (pack
, i
);
11010 TREE_VEC_ELT (vec
, n
) = init
;
11012 return expand_right_fold (t
, vec
, complain
);
11016 /* Substitute ARGS into T, which is an pack expansion
11017 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11018 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11019 (if only a partial substitution could be performed) or
11020 ERROR_MARK_NODE if there was an error. */
11022 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
11026 tree pack
, packs
= NULL_TREE
;
11027 bool unsubstituted_packs
= false;
11030 hash_map
<tree
, tree
> *saved_local_specializations
= NULL
;
11031 bool need_local_specializations
= false;
11034 gcc_assert (PACK_EXPANSION_P (t
));
11035 pattern
= PACK_EXPANSION_PATTERN (t
);
11037 /* Add in any args remembered from an earlier partial instantiation. */
11038 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
11040 levels
= TMPL_ARGS_DEPTH (args
);
11042 /* Determine the argument packs that will instantiate the parameter
11043 packs used in the expansion expression. While we're at it,
11044 compute the number of arguments to be expanded and make sure it
11046 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
11047 pack
= TREE_CHAIN (pack
))
11049 tree parm_pack
= TREE_VALUE (pack
);
11050 tree arg_pack
= NULL_TREE
;
11051 tree orig_arg
= NULL_TREE
;
11054 if (TREE_CODE (parm_pack
) == BASES
)
11056 if (BASES_DIRECT (parm_pack
))
11057 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11058 args
, complain
, in_decl
, false));
11060 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
11061 args
, complain
, in_decl
, false));
11063 if (TREE_CODE (parm_pack
) == PARM_DECL
)
11065 /* We know we have correct local_specializations if this
11066 expansion is at function scope, or if we're dealing with a
11067 local parameter in a requires expression; for the latter,
11068 tsubst_requires_expr set it up appropriately. */
11069 if (PACK_EXPANSION_LOCAL_P (t
) || CONSTRAINT_VAR_P (parm_pack
))
11070 arg_pack
= retrieve_local_specialization (parm_pack
);
11072 /* We can't rely on local_specializations for a parameter
11073 name used later in a function declaration (such as in a
11074 late-specified return type). Even if it exists, it might
11075 have the wrong value for a recursive call. */
11076 need_local_specializations
= true;
11080 /* This parameter pack was used in an unevaluated context. Just
11081 make a dummy decl, since it's only used for its type. */
11082 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
11083 if (arg_pack
&& DECL_PACK_P (arg_pack
))
11084 /* Partial instantiation of the parm_pack, we can't build
11085 up an argument pack yet. */
11086 arg_pack
= NULL_TREE
;
11088 arg_pack
= make_fnparm_pack (arg_pack
);
11091 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
11092 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
11096 template_parm_level_and_index (parm_pack
, &level
, &idx
);
11098 if (level
<= levels
)
11099 arg_pack
= TMPL_ARG (args
, level
, idx
);
11102 orig_arg
= arg_pack
;
11103 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
11104 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
11106 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
11107 /* This can only happen if we forget to expand an argument
11108 pack somewhere else. Just return an error, silently. */
11110 result
= make_tree_vec (1);
11111 TREE_VEC_ELT (result
, 0) = error_mark_node
;
11118 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
11120 /* Don't bother trying to do a partial substitution with
11121 incomplete packs; we'll try again after deduction. */
11122 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
11127 else if (len
!= my_len
)
11129 if (!(complain
& tf_error
))
11130 /* Fail quietly. */;
11131 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
11132 error ("mismatched argument pack lengths while expanding "
11136 error ("mismatched argument pack lengths while expanding "
11139 return error_mark_node
;
11142 /* Keep track of the parameter packs and their corresponding
11144 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
11145 TREE_TYPE (packs
) = orig_arg
;
11149 /* We can't substitute for this parameter pack. We use a flag as
11150 well as the missing_level counter because function parameter
11151 packs don't have a level. */
11152 gcc_assert (processing_template_decl
);
11153 unsubstituted_packs
= true;
11157 /* If the expansion is just T..., return the matching argument pack, unless
11158 we need to call convert_from_reference on all the elements. This is an
11159 important optimization; see c++/68422. */
11160 if (!unsubstituted_packs
11161 && TREE_PURPOSE (packs
) == pattern
)
11163 tree args
= ARGUMENT_PACK_ARGS (TREE_VALUE (packs
));
11164 /* Types need no adjustment, nor does sizeof..., and if we still have
11165 some pack expansion args we won't do anything yet. */
11166 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
11167 || PACK_EXPANSION_SIZEOF_P (t
)
11168 || pack_expansion_args_count (args
))
11170 /* Also optimize expression pack expansions if we can tell that the
11171 elements won't have reference type. */
11172 tree type
= TREE_TYPE (pattern
);
11173 if (type
&& TREE_CODE (type
) != REFERENCE_TYPE
11174 && !PACK_EXPANSION_P (type
)
11175 && !WILDCARD_TYPE_P (type
))
11177 /* Otherwise use the normal path so we get convert_from_reference. */
11180 /* We cannot expand this expansion expression, because we don't have
11181 all of the argument packs we need. */
11182 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
11184 /* We got some full packs, but we can't substitute them in until we
11185 have values for all the packs. So remember these until then. */
11187 t
= make_pack_expansion (pattern
);
11188 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
11191 else if (unsubstituted_packs
)
11193 /* There were no real arguments, we're just replacing a parameter
11194 pack with another version of itself. Substitute into the
11195 pattern and return a PACK_EXPANSION_*. The caller will need to
11197 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
11198 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
11199 /*integral_constant_expression_p=*/false);
11201 t
= tsubst (pattern
, args
, complain
, in_decl
);
11202 t
= make_pack_expansion (t
);
11206 gcc_assert (len
>= 0);
11208 if (need_local_specializations
)
11210 /* We're in a late-specified return type, so create our own local
11211 specializations map; the current map is either NULL or (in the
11212 case of recursive unification) might have bindings that we don't
11213 want to use or alter. */
11214 saved_local_specializations
= local_specializations
;
11215 local_specializations
= new hash_map
<tree
, tree
>;
11218 /* For each argument in each argument pack, substitute into the
11220 result
= make_tree_vec (len
);
11221 tree elem_args
= copy_template_args (args
);
11222 for (i
= 0; i
< len
; ++i
)
11224 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
11226 elem_args
, complain
,
11228 TREE_VEC_ELT (result
, i
) = t
;
11229 if (t
== error_mark_node
)
11231 result
= error_mark_node
;
11236 /* Update ARGS to restore the substitution from parameter packs to
11237 their argument packs. */
11238 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
11240 tree parm
= TREE_PURPOSE (pack
);
11242 if (TREE_CODE (parm
) == PARM_DECL
11243 || TREE_CODE (parm
) == FIELD_DECL
)
11244 register_local_specialization (TREE_TYPE (pack
), parm
);
11249 if (TREE_VALUE (pack
) == NULL_TREE
)
11252 template_parm_level_and_index (parm
, &level
, &idx
);
11254 /* Update the corresponding argument. */
11255 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11256 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
11259 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
11263 if (need_local_specializations
)
11265 delete local_specializations
;
11266 local_specializations
= saved_local_specializations
;
11269 /* If the dependent pack arguments were such that we end up with only a
11270 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11271 if (len
== 1 && TREE_CODE (result
) == TREE_VEC
11272 && PACK_EXPANSION_P (TREE_VEC_ELT (result
, 0)))
11273 return TREE_VEC_ELT (result
, 0);
11278 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11279 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11280 parameter packs; all parms generated from a function parameter pack will
11281 have the same DECL_PARM_INDEX. */
11284 get_pattern_parm (tree parm
, tree tmpl
)
11286 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11289 if (DECL_ARTIFICIAL (parm
))
11291 for (patparm
= DECL_ARGUMENTS (pattern
);
11292 patparm
; patparm
= DECL_CHAIN (patparm
))
11293 if (DECL_ARTIFICIAL (patparm
)
11294 && DECL_NAME (parm
) == DECL_NAME (patparm
))
11299 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
11300 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
11301 gcc_assert (DECL_PARM_INDEX (patparm
)
11302 == DECL_PARM_INDEX (parm
));
11308 /* Make an argument pack out of the TREE_VEC VEC. */
11311 make_argument_pack (tree vec
)
11314 tree elt
= TREE_VEC_ELT (vec
, 0);
11316 pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
11319 pack
= make_node (NONTYPE_ARGUMENT_PACK
);
11320 TREE_TYPE (pack
) = TREE_TYPE (elt
);
11321 TREE_CONSTANT (pack
) = 1;
11323 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
11327 /* Return an exact copy of template args T that can be modified
11331 copy_template_args (tree t
)
11333 if (t
== error_mark_node
)
11336 int len
= TREE_VEC_LENGTH (t
);
11337 tree new_vec
= make_tree_vec (len
);
11339 for (int i
= 0; i
< len
; ++i
)
11341 tree elt
= TREE_VEC_ELT (t
, i
);
11342 if (elt
&& TREE_CODE (elt
) == TREE_VEC
)
11343 elt
= copy_template_args (elt
);
11344 TREE_VEC_ELT (new_vec
, i
) = elt
;
11347 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec
)
11348 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
);
11353 /* Substitute ARGS into the vector or list of template arguments T. */
11356 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11359 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
11362 if (t
== error_mark_node
)
11363 return error_mark_node
;
11365 len
= TREE_VEC_LENGTH (t
);
11366 elts
= XALLOCAVEC (tree
, len
);
11368 for (i
= 0; i
< len
; i
++)
11370 tree orig_arg
= TREE_VEC_ELT (t
, i
);
11373 if (TREE_CODE (orig_arg
) == TREE_VEC
)
11374 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
11375 else if (PACK_EXPANSION_P (orig_arg
))
11377 /* Substitute into an expansion expression. */
11378 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
11380 if (TREE_CODE (new_arg
) == TREE_VEC
)
11381 /* Add to the expanded length adjustment the number of
11382 expanded arguments. We subtract one from this
11383 measurement, because the argument pack expression
11384 itself is already counted as 1 in
11385 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11386 the argument pack is empty. */
11387 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
11389 else if (ARGUMENT_PACK_P (orig_arg
))
11391 /* Substitute into each of the arguments. */
11392 new_arg
= TYPE_P (orig_arg
)
11393 ? cxx_make_type (TREE_CODE (orig_arg
))
11394 : make_node (TREE_CODE (orig_arg
));
11396 SET_ARGUMENT_PACK_ARGS (
11398 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
11399 args
, complain
, in_decl
));
11401 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
11402 new_arg
= error_mark_node
;
11404 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
11405 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
11406 complain
, in_decl
);
11407 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
11409 if (TREE_TYPE (new_arg
) == error_mark_node
)
11410 new_arg
= error_mark_node
;
11414 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
11416 if (new_arg
== error_mark_node
)
11417 return error_mark_node
;
11420 if (new_arg
!= orig_arg
)
11427 /* Make space for the expanded arguments coming from template
11429 t
= make_tree_vec (len
+ expanded_len_adjust
);
11430 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11431 arguments for a member template.
11432 In that case each TREE_VEC in ORIG_T represents a level of template
11433 arguments, and ORIG_T won't carry any non defaulted argument count.
11434 It will rather be the nested TREE_VECs that will carry one.
11435 In other words, ORIG_T carries a non defaulted argument count only
11436 if it doesn't contain any nested TREE_VEC. */
11437 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
11439 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
11440 count
+= expanded_len_adjust
;
11441 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
11443 for (i
= 0, out
= 0; i
< len
; i
++)
11445 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
11446 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
11447 && TREE_CODE (elts
[i
]) == TREE_VEC
)
11451 /* Now expand the template argument pack "in place". */
11452 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
11453 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
11457 TREE_VEC_ELT (t
, out
) = elts
[i
];
11465 /* Substitute ARGS into one level PARMS of template parameters. */
11468 tsubst_template_parms_level (tree parms
, tree args
, tsubst_flags_t complain
)
11470 if (parms
== error_mark_node
)
11471 return error_mark_node
;
11473 tree new_vec
= make_tree_vec (TREE_VEC_LENGTH (parms
));
11475 for (int i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
11477 tree tuple
= TREE_VEC_ELT (parms
, i
);
11479 if (tuple
== error_mark_node
)
11482 TREE_VEC_ELT (new_vec
, i
) =
11483 tsubst_template_parm (tuple
, args
, complain
);
11489 /* Return the result of substituting ARGS into the template parameters
11490 given by PARMS. If there are m levels of ARGS and m + n levels of
11491 PARMS, then the result will contain n levels of PARMS. For
11492 example, if PARMS is `template <class T> template <class U>
11493 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11494 result will be `template <int*, double, class V>'. */
11497 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
11499 tree r
= NULL_TREE
;
11502 /* When substituting into a template, we must set
11503 PROCESSING_TEMPLATE_DECL as the template parameters may be
11504 dependent if they are based on one-another, and the dependency
11505 predicates are short-circuit outside of templates. */
11506 ++processing_template_decl
;
11508 for (new_parms
= &r
;
11509 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
11510 new_parms
= &(TREE_CHAIN (*new_parms
)),
11511 parms
= TREE_CHAIN (parms
))
11513 tree new_vec
= tsubst_template_parms_level (TREE_VALUE (parms
),
11516 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
11517 - TMPL_ARGS_DEPTH (args
)),
11518 new_vec
, NULL_TREE
);
11521 --processing_template_decl
;
11526 /* Return the result of substituting ARGS into one template parameter
11527 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11528 parameter and which TREE_PURPOSE is the default argument of the
11529 template parameter. */
11532 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
11534 tree default_value
, parm_decl
;
11536 if (args
== NULL_TREE
11538 || t
== error_mark_node
)
11541 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
11543 default_value
= TREE_PURPOSE (t
);
11544 parm_decl
= TREE_VALUE (t
);
11546 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
11547 if (TREE_CODE (parm_decl
) == PARM_DECL
11548 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
11549 parm_decl
= error_mark_node
;
11550 default_value
= tsubst_template_arg (default_value
, args
,
11551 complain
, NULL_TREE
);
11553 return build_tree_list (default_value
, parm_decl
);
11556 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11557 type T. If T is not an aggregate or enumeration type, it is
11558 handled as if by tsubst. IN_DECL is as for tsubst. If
11559 ENTERING_SCOPE is nonzero, T is the context for a template which
11560 we are presently tsubst'ing. Return the substituted value. */
11563 tsubst_aggr_type (tree t
,
11565 tsubst_flags_t complain
,
11567 int entering_scope
)
11569 if (t
== NULL_TREE
)
11572 switch (TREE_CODE (t
))
11575 if (TYPE_PTRMEMFUNC_P (t
))
11576 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
11578 /* Fall through. */
11579 case ENUMERAL_TYPE
:
11581 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
11586 int saved_unevaluated_operand
;
11587 int saved_inhibit_evaluation_warnings
;
11589 /* In "sizeof(X<I>)" we need to evaluate "I". */
11590 saved_unevaluated_operand
= cp_unevaluated_operand
;
11591 cp_unevaluated_operand
= 0;
11592 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
11593 c_inhibit_evaluation_warnings
= 0;
11595 /* First, determine the context for the type we are looking
11597 context
= TYPE_CONTEXT (t
);
11598 if (context
&& TYPE_P (context
))
11600 context
= tsubst_aggr_type (context
, args
, complain
,
11601 in_decl
, /*entering_scope=*/1);
11602 /* If context is a nested class inside a class template,
11603 it may still need to be instantiated (c++/33959). */
11604 context
= complete_type (context
);
11607 /* Then, figure out what arguments are appropriate for the
11608 type we are trying to find. For example, given:
11610 template <class T> struct S;
11611 template <class T, class U> void f(T, U) { S<U> su; }
11613 and supposing that we are instantiating f<int, double>,
11614 then our ARGS will be {int, double}, but, when looking up
11615 S we only want {double}. */
11616 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
11617 complain
, in_decl
);
11618 if (argvec
== error_mark_node
)
11619 r
= error_mark_node
;
11622 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
11623 entering_scope
, complain
);
11624 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11627 cp_unevaluated_operand
= saved_unevaluated_operand
;
11628 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
11633 /* This is not a template type, so there's nothing to do. */
11637 return tsubst (t
, args
, complain
, in_decl
);
11641 /* Substitute into the default argument ARG (a default argument for
11642 FN), which has the indicated TYPE. */
11645 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
11647 tree saved_class_ptr
= NULL_TREE
;
11648 tree saved_class_ref
= NULL_TREE
;
11649 int errs
= errorcount
+ sorrycount
;
11651 /* This can happen in invalid code. */
11652 if (TREE_CODE (arg
) == DEFAULT_ARG
)
11655 /* This default argument came from a template. Instantiate the
11656 default argument here, not in tsubst. In the case of
11665 we must be careful to do name lookup in the scope of S<T>,
11666 rather than in the current class. */
11667 push_access_scope (fn
);
11668 /* The "this" pointer is not valid in a default argument. */
11671 saved_class_ptr
= current_class_ptr
;
11672 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
11673 saved_class_ref
= current_class_ref
;
11674 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
11677 push_deferring_access_checks(dk_no_deferred
);
11678 /* The default argument expression may cause implicitly defined
11679 member functions to be synthesized, which will result in garbage
11680 collection. We must treat this situation as if we were within
11681 the body of function so as to avoid collecting live data on the
11684 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
11685 complain
, NULL_TREE
,
11686 /*integral_constant_expression_p=*/false);
11688 pop_deferring_access_checks();
11690 /* Restore the "this" pointer. */
11693 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
11694 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
11697 if (errorcount
+sorrycount
> errs
11698 && (complain
& tf_warning_or_error
))
11699 inform (input_location
,
11700 " when instantiating default argument for call to %D", fn
);
11702 /* Make sure the default argument is reasonable. */
11703 arg
= check_default_argument (type
, arg
, complain
);
11705 pop_access_scope (fn
);
11710 /* Substitute into all the default arguments for FN. */
11713 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
11718 tmpl_args
= DECL_TI_ARGS (fn
);
11720 /* If this function is not yet instantiated, we certainly don't need
11721 its default arguments. */
11722 if (uses_template_parms (tmpl_args
))
11724 /* Don't do this again for clones. */
11725 if (DECL_CLONED_FUNCTION_P (fn
))
11728 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
11730 arg
= TREE_CHAIN (arg
))
11731 if (TREE_PURPOSE (arg
))
11732 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
11734 TREE_PURPOSE (arg
),
11738 /* Substitute the ARGS into the T, which is a _DECL. Return the
11739 result of the substitution. Issue error and warning messages under
11740 control of COMPLAIN. */
11743 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
11745 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11746 location_t saved_loc
;
11747 tree r
= NULL_TREE
;
11749 hashval_t hash
= 0;
11751 /* Set the filename and linenumber to improve error-reporting. */
11752 saved_loc
= input_location
;
11753 input_location
= DECL_SOURCE_LOCATION (t
);
11755 switch (TREE_CODE (t
))
11757 case TEMPLATE_DECL
:
11759 /* We can get here when processing a member function template,
11760 member class template, or template template parameter. */
11761 tree decl
= DECL_TEMPLATE_RESULT (t
);
11766 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11768 /* Template template parameter is treated here. */
11769 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11770 if (new_type
== error_mark_node
)
11771 r
= error_mark_node
;
11772 /* If we get a real template back, return it. This can happen in
11773 the context of most_specialized_partial_spec. */
11774 else if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
11777 /* The new TEMPLATE_DECL was built in
11778 reduce_template_parm_level. */
11779 r
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type
);
11783 /* We might already have an instance of this template.
11784 The ARGS are for the surrounding class type, so the
11785 full args contain the tsubst'd args for the context,
11786 plus the innermost args from the template decl. */
11787 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
11788 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
11789 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
11790 /* Because this is a template, the arguments will still be
11791 dependent, even after substitution. If
11792 PROCESSING_TEMPLATE_DECL is not set, the dependency
11793 predicates will short-circuit. */
11794 ++processing_template_decl
;
11795 full_args
= tsubst_template_args (tmpl_args
, args
,
11796 complain
, in_decl
);
11797 --processing_template_decl
;
11798 if (full_args
== error_mark_node
)
11799 RETURN (error_mark_node
);
11801 /* If this is a default template template argument,
11802 tsubst might not have changed anything. */
11803 if (full_args
== tmpl_args
)
11806 hash
= hash_tmpl_and_args (t
, full_args
);
11807 spec
= retrieve_specialization (t
, full_args
, hash
);
11808 if (spec
!= NULL_TREE
)
11814 /* Make a new template decl. It will be similar to the
11815 original, but will record the current template arguments.
11816 We also create a new function declaration, which is just
11817 like the old one, but points to this new template, rather
11818 than the old one. */
11820 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
11821 DECL_CHAIN (r
) = NULL_TREE
;
11823 // Build new template info linking to the original template decl.
11824 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
11826 if (TREE_CODE (decl
) == TYPE_DECL
11827 && !TYPE_DECL_ALIAS_P (decl
))
11830 ++processing_template_decl
;
11831 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11832 --processing_template_decl
;
11833 if (new_type
== error_mark_node
)
11834 RETURN (error_mark_node
);
11836 TREE_TYPE (r
) = new_type
;
11837 /* For a partial specialization, we need to keep pointing to
11838 the primary template. */
11839 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
11840 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
11841 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
11842 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
11843 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
11848 ++processing_template_decl
;
11849 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
11850 --processing_template_decl
;
11851 if (new_decl
== error_mark_node
)
11852 RETURN (error_mark_node
);
11854 DECL_TEMPLATE_RESULT (r
) = new_decl
;
11855 DECL_TI_TEMPLATE (new_decl
) = r
;
11856 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
11857 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
11858 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
11861 SET_DECL_IMPLICIT_INSTANTIATION (r
);
11862 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
11863 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
11865 /* The template parameters for this new template are all the
11866 template parameters for the old template, except the
11867 outermost level of parameters. */
11868 DECL_TEMPLATE_PARMS (r
)
11869 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
11872 if (PRIMARY_TEMPLATE_P (t
))
11873 DECL_PRIMARY_TEMPLATE (r
) = r
;
11875 if (TREE_CODE (decl
) != TYPE_DECL
&& !VAR_P (decl
))
11876 /* Record this non-type partial instantiation. */
11877 register_specialization (r
, t
,
11878 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
11883 case FUNCTION_DECL
:
11886 tree argvec
= NULL_TREE
;
11894 /* Nobody should be tsubst'ing into non-template functions. */
11895 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
11897 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
11901 /* If T is not dependent, just return it. */
11902 if (!uses_template_parms (DECL_TI_ARGS (t
)))
11905 /* Calculate the most general template of which R is a
11906 specialization, and the complete set of arguments used to
11908 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
11909 argvec
= tsubst_template_args (DECL_TI_ARGS
11910 (DECL_TEMPLATE_RESULT
11911 (DECL_TI_TEMPLATE (t
))),
11912 args
, complain
, in_decl
);
11913 if (argvec
== error_mark_node
)
11914 RETURN (error_mark_node
);
11916 /* Check to see if we already have this specialization. */
11917 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
11918 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
11926 /* We can see more levels of arguments than parameters if
11927 there was a specialization of a member template, like
11930 template <class T> struct S { template <class U> void f(); }
11931 template <> template <class U> void S<int>::f(U);
11933 Here, we'll be substituting into the specialization,
11934 because that's where we can find the code we actually
11935 want to generate, but we'll have enough arguments for
11936 the most general template.
11938 We also deal with the peculiar case:
11940 template <class T> struct S {
11941 template <class U> friend void f();
11943 template <class U> void f() {}
11945 template void f<double>();
11947 Here, the ARGS for the instantiation of will be {int,
11948 double}. But, we only need as many ARGS as there are
11949 levels of template parameters in CODE_PATTERN. We are
11950 careful not to get fooled into reducing the ARGS in
11953 template <class T> struct S { template <class U> void f(U); }
11954 template <class T> template <> void S<T>::f(int) {}
11956 which we can spot because the pattern will be a
11957 specialization in this case. */
11958 args_depth
= TMPL_ARGS_DEPTH (args
);
11960 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
11961 if (args_depth
> parms_depth
11962 && !DECL_TEMPLATE_SPECIALIZATION (t
))
11963 args
= get_innermost_template_args (args
, parms_depth
);
11967 /* This special case arises when we have something like this:
11969 template <class T> struct S {
11970 friend void f<int>(int, double);
11973 Here, the DECL_TI_TEMPLATE for the friend declaration
11974 will be an IDENTIFIER_NODE. We are being called from
11975 tsubst_friend_function, and we want only to create a
11976 new decl (R) with appropriate types so that we can call
11977 determine_specialization. */
11978 gen_tmpl
= NULL_TREE
;
11981 if (DECL_CLASS_SCOPE_P (t
))
11983 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
11987 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
11988 complain
, t
, /*entering_scope=*/1);
11993 ctx
= DECL_CONTEXT (t
);
11995 type
= tsubst (TREE_TYPE (t
), args
, complain
|tf_fndecl_type
, in_decl
);
11996 if (type
== error_mark_node
)
11997 RETURN (error_mark_node
);
11999 /* If we hit excessive deduction depth, the type is bogus even if
12000 it isn't error_mark_node, so don't build a decl. */
12001 if (excessive_deduction_depth
)
12002 RETURN (error_mark_node
);
12004 /* We do NOT check for matching decls pushed separately at this
12005 point, as they may not represent instantiations of this
12006 template, and in any case are considered separate under the
12009 DECL_USE_TEMPLATE (r
) = 0;
12010 TREE_TYPE (r
) = type
;
12011 /* Clear out the mangled name and RTL for the instantiation. */
12012 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12013 SET_DECL_RTL (r
, NULL
);
12014 /* Leave DECL_INITIAL set on deleted instantiations. */
12015 if (!DECL_DELETED_FN (r
))
12016 DECL_INITIAL (r
) = NULL_TREE
;
12017 DECL_CONTEXT (r
) = ctx
;
12019 /* OpenMP UDRs have the only argument a reference to the declared
12020 type. We want to diagnose if the declared type is a reference,
12021 which is invalid, but as references to references are usually
12022 quietly merged, diagnose it here. */
12023 if (DECL_OMP_DECLARE_REDUCTION_P (t
))
12026 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t
))));
12027 argtype
= tsubst (argtype
, args
, complain
, in_decl
);
12028 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
12029 error_at (DECL_SOURCE_LOCATION (t
),
12030 "reference type %qT in "
12031 "%<#pragma omp declare reduction%>", argtype
);
12032 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t
)), '~') == NULL
)
12033 DECL_NAME (r
) = omp_reduction_id (ERROR_MARK
, DECL_NAME (t
),
12037 if (member
&& DECL_CONV_FN_P (r
))
12038 /* Type-conversion operator. Reconstruct the name, in
12039 case it's the name of one of the template's parameters. */
12040 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
12042 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
12044 DECL_RESULT (r
) = NULL_TREE
;
12046 TREE_STATIC (r
) = 0;
12047 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
12048 DECL_EXTERNAL (r
) = 1;
12049 /* If this is an instantiation of a function with internal
12050 linkage, we already know what object file linkage will be
12051 assigned to the instantiation. */
12052 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
12053 DECL_DEFER_OUTPUT (r
) = 0;
12054 DECL_CHAIN (r
) = NULL_TREE
;
12055 DECL_PENDING_INLINE_INFO (r
) = 0;
12056 DECL_PENDING_INLINE_P (r
) = 0;
12057 DECL_SAVED_TREE (r
) = NULL_TREE
;
12058 DECL_STRUCT_FUNCTION (r
) = NULL
;
12060 /* We'll re-clone as appropriate in instantiate_template. */
12061 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
12063 /* If we aren't complaining now, return on error before we register
12064 the specialization so that we'll complain eventually. */
12065 if ((complain
& tf_error
) == 0
12066 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12067 && !grok_op_properties (r
, /*complain=*/false))
12068 RETURN (error_mark_node
);
12070 /* When instantiating a constrained member, substitute
12071 into the constraints to create a new constraint. */
12072 if (tree ci
= get_constraints (t
))
12075 ci
= tsubst_constraint_info (ci
, argvec
, complain
, NULL_TREE
);
12076 set_constraints (r
, ci
);
12079 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12080 this in the special friend case mentioned above where
12081 GEN_TMPL is NULL. */
12084 DECL_TEMPLATE_INFO (r
)
12085 = build_template_info (gen_tmpl
, argvec
);
12086 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12089 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12091 /* We instantiated this while substituting into
12092 the type earlier (template/friend54.C). */
12095 /* We're not supposed to instantiate default arguments
12096 until they are called, for a template. But, for a
12099 template <class T> void f ()
12100 { extern void g(int i = T()); }
12102 we should do the substitution when the template is
12103 instantiated. We handle the member function case in
12104 instantiate_class_template since the default arguments
12105 might refer to other members of the class. */
12107 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
12108 && !uses_template_parms (argvec
))
12109 tsubst_default_arguments (r
, complain
);
12112 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12114 /* Copy the list of befriending classes. */
12115 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
12117 friends
= &TREE_CHAIN (*friends
))
12119 *friends
= copy_node (*friends
);
12120 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
12125 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
12127 maybe_retrofit_in_chrg (r
);
12128 if (DECL_CONSTRUCTOR_P (r
))
12129 grok_ctor_properties (ctx
, r
);
12130 if (DECL_INHERITED_CTOR (r
))
12131 deduce_inheriting_ctor (r
);
12132 /* If this is an instantiation of a member template, clone it.
12133 If it isn't, that'll be handled by
12134 clone_constructors_and_destructors. */
12135 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
12136 clone_function_decl (r
, /*update_method_vec_p=*/0);
12138 else if ((complain
& tf_error
) != 0
12139 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
12140 && !grok_op_properties (r
, /*complain=*/true))
12141 RETURN (error_mark_node
);
12143 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
12144 SET_DECL_FRIEND_CONTEXT (r
,
12145 tsubst (DECL_FRIEND_CONTEXT (t
),
12146 args
, complain
, in_decl
));
12148 /* Possibly limit visibility based on template args. */
12149 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12150 if (DECL_VISIBILITY_SPECIFIED (t
))
12152 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12153 DECL_ATTRIBUTES (r
)
12154 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12156 determine_visibility (r
);
12157 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
12158 && !processing_template_decl
)
12159 defaulted_late_check (r
);
12161 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12162 args
, complain
, in_decl
);
12168 tree type
= NULL_TREE
;
12170 tree expanded_types
= NULL_TREE
;
12171 tree prev_r
= NULL_TREE
;
12172 tree first_r
= NULL_TREE
;
12174 if (DECL_PACK_P (t
))
12176 /* If there is a local specialization that isn't a
12177 parameter pack, it means that we're doing a "simple"
12178 substitution from inside tsubst_pack_expansion. Just
12179 return the local specialization (which will be a single
12181 tree spec
= retrieve_local_specialization (t
);
12183 && TREE_CODE (spec
) == PARM_DECL
12184 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
12187 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12188 the parameters in this function parameter pack. */
12189 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12190 complain
, in_decl
);
12191 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12193 len
= TREE_VEC_LENGTH (expanded_types
);
12195 /* Zero-length parameter packs are boring. Just substitute
12198 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
12203 /* All we did was update the type. Make a note of that. */
12204 type
= expanded_types
;
12205 expanded_types
= NULL_TREE
;
12209 /* Loop through all of the parameters we'll build. When T is
12210 a function parameter pack, LEN is the number of expanded
12211 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12213 for (i
= 0; i
< len
; ++i
)
12217 if (DECL_TEMPLATE_PARM_P (t
))
12218 SET_DECL_TEMPLATE_PARM_P (r
);
12220 if (expanded_types
)
12221 /* We're on the Ith parameter of the function parameter
12224 /* Get the Ith type. */
12225 type
= TREE_VEC_ELT (expanded_types
, i
);
12227 /* Rename the parameter to include the index. */
12229 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12232 /* We're dealing with a normal parameter. */
12233 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12235 type
= type_decays_to (type
);
12236 TREE_TYPE (r
) = type
;
12237 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12239 if (DECL_INITIAL (r
))
12241 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
12242 DECL_INITIAL (r
) = TREE_TYPE (r
);
12244 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
12245 complain
, in_decl
);
12248 DECL_CONTEXT (r
) = NULL_TREE
;
12250 if (!DECL_TEMPLATE_PARM_P (r
))
12251 DECL_ARG_TYPE (r
) = type_passed_as (type
);
12253 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12254 args
, complain
, in_decl
);
12256 /* Keep track of the first new parameter we
12257 generate. That's what will be returned to the
12262 /* Build a proper chain of parameters when substituting
12263 into a function parameter pack. */
12265 DECL_CHAIN (prev_r
) = r
;
12268 /* If cp_unevaluated_operand is set, we're just looking for a
12269 single dummy parameter, so don't keep going. */
12270 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
12271 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
12272 complain
, DECL_CHAIN (t
));
12274 /* FIRST_R contains the start of the chain we've built. */
12281 tree type
= NULL_TREE
;
12282 tree vec
= NULL_TREE
;
12283 tree expanded_types
= NULL_TREE
;
12286 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12288 /* This field is a lambda capture pack. Return a TREE_VEC of
12289 the expanded fields to instantiate_class_template_1 and
12290 store them in the specializations hash table as a
12291 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12292 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
12293 complain
, in_decl
);
12294 if (TREE_CODE (expanded_types
) == TREE_VEC
)
12296 len
= TREE_VEC_LENGTH (expanded_types
);
12297 vec
= make_tree_vec (len
);
12301 /* All we did was update the type. Make a note of that. */
12302 type
= expanded_types
;
12303 expanded_types
= NULL_TREE
;
12307 for (int i
= 0; i
< len
; ++i
)
12310 if (expanded_types
)
12312 type
= TREE_VEC_ELT (expanded_types
, i
);
12314 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
12317 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12319 if (type
== error_mark_node
)
12320 RETURN (error_mark_node
);
12321 TREE_TYPE (r
) = type
;
12322 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12324 if (DECL_C_BIT_FIELD (r
))
12325 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12326 non-bit-fields DECL_INITIAL is a non-static data member
12327 initializer, which gets deferred instantiation. */
12329 = tsubst_expr (DECL_INITIAL (t
), args
,
12331 /*integral_constant_expression_p=*/true);
12332 else if (DECL_INITIAL (t
))
12334 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12335 NSDMI in perform_member_init. Still set DECL_INITIAL
12336 so that we know there is one. */
12337 DECL_INITIAL (r
) = void_node
;
12338 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
12339 retrofit_lang_decl (r
);
12340 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
12342 /* We don't have to set DECL_CONTEXT here; it is set by
12343 finish_member_declaration. */
12344 DECL_CHAIN (r
) = NULL_TREE
;
12346 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
12347 args
, complain
, in_decl
);
12350 TREE_VEC_ELT (vec
, i
) = r
;
12356 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
12357 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
12358 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
12359 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
12360 TREE_TYPE (pack
) = tpack
;
12361 register_specialization (pack
, t
, args
, false, 0);
12367 /* We reach here only for member using decls. We also need to check
12368 uses_template_parms because DECL_DEPENDENT_P is not set for a
12369 using-declaration that designates a member of the current
12370 instantiation (c++/53549). */
12371 if (DECL_DEPENDENT_P (t
)
12372 || uses_template_parms (USING_DECL_SCOPE (t
)))
12374 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
12375 complain
, in_decl
);
12376 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
12377 r
= do_class_using_decl (inst_scope
, name
);
12379 r
= error_mark_node
;
12382 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
12383 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
12389 DECL_CHAIN (r
) = NULL_TREE
;
12396 tree argvec
= NULL_TREE
;
12397 tree gen_tmpl
= NULL_TREE
;
12399 tree tmpl
= NULL_TREE
;
12401 tree type
= NULL_TREE
;
12404 if (TREE_TYPE (t
) == error_mark_node
)
12405 RETURN (error_mark_node
);
12407 if (TREE_CODE (t
) == TYPE_DECL
12408 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
12410 /* If this is the canonical decl, we don't have to
12411 mess with instantiations, and often we can't (for
12412 typename, template type parms and such). Note that
12413 TYPE_NAME is not correct for the above test if
12414 we've copied the type for a typedef. */
12415 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12416 if (type
== error_mark_node
)
12417 RETURN (error_mark_node
);
12418 r
= TYPE_NAME (type
);
12422 /* Check to see if we already have the specialization we
12425 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
12427 /* T is a static data member or namespace-scope entity.
12428 We have to substitute into namespace-scope variables
12429 (not just variable templates) because of cases like:
12431 template <class T> void f() { extern T t; }
12433 where the entity referenced is not known until
12434 instantiation time. */
12436 ctx
= DECL_CONTEXT (t
);
12437 if (DECL_CLASS_SCOPE_P (t
))
12439 ctx
= tsubst_aggr_type (ctx
, args
,
12441 in_decl
, /*entering_scope=*/1);
12442 /* If CTX is unchanged, then T is in fact the
12443 specialization we want. That situation occurs when
12444 referencing a static data member within in its own
12445 class. We can use pointer equality, rather than
12446 same_type_p, because DECL_CONTEXT is always
12448 if (ctx
== DECL_CONTEXT (t
)
12449 /* ... unless T is a member template; in which
12450 case our caller can be willing to create a
12451 specialization of that template represented
12453 && !(DECL_TI_TEMPLATE (t
)
12454 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
))))
12460 tmpl
= DECL_TI_TEMPLATE (t
);
12461 gen_tmpl
= most_general_template (tmpl
);
12462 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
12463 if (argvec
!= error_mark_node
)
12464 argvec
= (coerce_innermost_template_parms
12465 (DECL_TEMPLATE_PARMS (gen_tmpl
),
12466 argvec
, t
, complain
,
12467 /*all*/true, /*defarg*/true));
12468 if (argvec
== error_mark_node
)
12469 RETURN (error_mark_node
);
12470 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
12471 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
12476 /* A local variable. */
12478 /* Subsequent calls to pushdecl will fill this in. */
12480 /* Unless this is a reference to a static variable from an
12481 enclosing function, in which case we need to fill it in now. */
12482 if (TREE_STATIC (t
))
12484 tree fn
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12485 if (fn
!= current_function_decl
)
12488 spec
= retrieve_local_specialization (t
);
12490 /* If we already have the specialization we need, there is
12491 nothing more to do. */
12498 /* Create a new node for the specialization we need. */
12500 if (type
== NULL_TREE
)
12502 if (is_typedef_decl (t
))
12503 type
= DECL_ORIGINAL_TYPE (t
);
12505 type
= TREE_TYPE (t
);
12507 && VAR_HAD_UNKNOWN_BOUND (t
)
12508 && type
!= error_mark_node
)
12509 type
= strip_array_domain (type
);
12510 type
= tsubst (type
, args
, complain
, in_decl
);
12514 /* Even if the original location is out of scope, the
12515 newly substituted one is not. */
12516 DECL_DEAD_FOR_LOCAL (r
) = 0;
12517 DECL_INITIALIZED_P (r
) = 0;
12518 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
12519 if (type
== error_mark_node
)
12520 RETURN (error_mark_node
);
12521 if (TREE_CODE (type
) == FUNCTION_TYPE
)
12523 /* It may seem that this case cannot occur, since:
12528 declares a function, not a variable. However:
12531 template <typename T> void g() { T t; }
12532 template void g<f>();
12534 is an attempt to declare a variable with function
12536 error ("variable %qD has function type",
12537 /* R is not yet sufficiently initialized, so we
12538 just use its name. */
12540 RETURN (error_mark_node
);
12542 type
= complete_type (type
);
12543 /* Wait until cp_finish_decl to set this again, to handle
12544 circular dependency (template/instantiate6.C). */
12545 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
12546 type
= check_var_type (DECL_NAME (r
), type
);
12548 if (DECL_HAS_VALUE_EXPR_P (t
))
12550 tree ve
= DECL_VALUE_EXPR (t
);
12551 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
12552 /*constant_expression_p=*/false);
12553 if (REFERENCE_REF_P (ve
))
12555 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
12556 ve
= TREE_OPERAND (ve
, 0);
12558 SET_DECL_VALUE_EXPR (r
, ve
);
12560 if (CP_DECL_THREAD_LOCAL_P (r
)
12561 && !processing_template_decl
)
12562 set_decl_tls_model (r
, decl_default_tls_model (r
));
12564 else if (DECL_SELF_REFERENCE_P (t
))
12565 SET_DECL_SELF_REFERENCE_P (r
);
12566 TREE_TYPE (r
) = type
;
12567 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
12568 DECL_CONTEXT (r
) = ctx
;
12569 /* Clear out the mangled name and RTL for the instantiation. */
12570 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
12571 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12572 SET_DECL_RTL (r
, NULL
);
12573 /* The initializer must not be expanded until it is required;
12574 see [temp.inst]. */
12575 DECL_INITIAL (r
) = NULL_TREE
;
12577 SET_DECL_MODE (r
, VOIDmode
);
12578 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
12579 SET_DECL_RTL (r
, NULL
);
12580 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
12583 /* Possibly limit visibility based on template args. */
12584 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
12585 if (DECL_VISIBILITY_SPECIFIED (t
))
12587 DECL_VISIBILITY_SPECIFIED (r
) = 0;
12588 DECL_ATTRIBUTES (r
)
12589 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
12591 determine_visibility (r
);
12596 /* A static data member declaration is always marked
12597 external when it is declared in-class, even if an
12598 initializer is present. We mimic the non-template
12599 processing here. */
12600 DECL_EXTERNAL (r
) = 1;
12601 if (DECL_NAMESPACE_SCOPE_P (t
))
12602 DECL_NOT_REALLY_EXTERN (r
) = 1;
12604 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
12605 SET_DECL_IMPLICIT_INSTANTIATION (r
);
12606 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
12610 if (DECL_LANG_SPECIFIC (r
))
12611 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
12612 if (!cp_unevaluated_operand
)
12613 register_local_specialization (r
, t
);
12616 DECL_CHAIN (r
) = NULL_TREE
;
12618 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
12620 args
, complain
, in_decl
);
12622 /* Preserve a typedef that names a type. */
12623 if (is_typedef_decl (r
))
12625 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
12626 set_underlying_type (r
);
12627 if (TYPE_DECL_ALIAS_P (r
) && type
!= error_mark_node
)
12628 /* An alias template specialization can be dependent
12629 even if its underlying type is not. */
12630 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r
)) = false;
12633 layout_decl (r
, 0);
12638 gcc_unreachable ();
12643 /* Restore the file and line information. */
12644 input_location
= saved_loc
;
12649 /* Substitute into the ARG_TYPES of a function type.
12650 If END is a TREE_CHAIN, leave it and any following types
12654 tsubst_arg_types (tree arg_types
,
12657 tsubst_flags_t complain
,
12660 tree remaining_arg_types
;
12661 tree type
= NULL_TREE
;
12663 tree expanded_args
= NULL_TREE
;
12666 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
12669 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
12670 args
, end
, complain
, in_decl
);
12671 if (remaining_arg_types
== error_mark_node
)
12672 return error_mark_node
;
12674 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
12676 /* For a pack expansion, perform substitution on the
12677 entire expression. Later on, we'll handle the arguments
12679 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
12680 args
, complain
, in_decl
);
12682 if (TREE_CODE (expanded_args
) == TREE_VEC
)
12683 /* So that we'll spin through the parameters, one by one. */
12684 i
= TREE_VEC_LENGTH (expanded_args
);
12687 /* We only partially substituted into the parameter
12688 pack. Our type is TYPE_PACK_EXPANSION. */
12689 type
= expanded_args
;
12690 expanded_args
= NULL_TREE
;
12698 type
= TREE_VEC_ELT (expanded_args
, i
);
12700 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
12702 if (type
== error_mark_node
)
12703 return error_mark_node
;
12704 if (VOID_TYPE_P (type
))
12706 if (complain
& tf_error
)
12708 error ("invalid parameter type %qT", type
);
12710 error ("in declaration %q+D", in_decl
);
12712 return error_mark_node
;
12715 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
12716 return error_mark_node
;
12718 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12719 top-level qualifiers as required. */
12720 type
= cv_unqualified (type_decays_to (type
));
12722 /* We do not substitute into default arguments here. The standard
12723 mandates that they be instantiated only when needed, which is
12724 done in build_over_call. */
12725 default_arg
= TREE_PURPOSE (arg_types
);
12727 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
12729 /* We've instantiated a template before its default arguments
12730 have been parsed. This can happen for a nested template
12731 class, and is not an error unless we require the default
12732 argument in a call of this function. */
12733 remaining_arg_types
=
12734 tree_cons (default_arg
, type
, remaining_arg_types
);
12735 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
12738 remaining_arg_types
=
12739 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
12742 return remaining_arg_types
;
12745 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12746 *not* handle the exception-specification for FNTYPE, because the
12747 initial substitution of explicitly provided template parameters
12748 during argument deduction forbids substitution into the
12749 exception-specification:
12753 All references in the function type of the function template to the
12754 corresponding template parameters are replaced by the specified tem-
12755 plate argument values. If a substitution in a template parameter or
12756 in the function type of the function template results in an invalid
12757 type, type deduction fails. [Note: The equivalent substitution in
12758 exception specifications is done only when the function is instanti-
12759 ated, at which point a program is ill-formed if the substitution
12760 results in an invalid type.] */
12763 tsubst_function_type (tree t
,
12765 tsubst_flags_t complain
,
12769 tree arg_types
= NULL_TREE
;
12772 /* The TYPE_CONTEXT is not used for function/method types. */
12773 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
12775 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12777 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t
);
12779 if (late_return_type_p
)
12781 /* Substitute the argument types. */
12782 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12783 complain
, in_decl
);
12784 if (arg_types
== error_mark_node
)
12785 return error_mark_node
;
12787 tree save_ccp
= current_class_ptr
;
12788 tree save_ccr
= current_class_ref
;
12789 tree this_type
= (TREE_CODE (t
) == METHOD_TYPE
12790 ? TREE_TYPE (TREE_VALUE (arg_types
)) : NULL_TREE
);
12791 bool do_inject
= this_type
&& CLASS_TYPE_P (this_type
);
12794 /* DR 1207: 'this' is in scope in the trailing return type. */
12795 inject_this_parameter (this_type
, cp_type_quals (this_type
));
12798 /* Substitute the return type. */
12799 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12803 current_class_ptr
= save_ccp
;
12804 current_class_ref
= save_ccr
;
12808 /* Substitute the return type. */
12809 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12811 if (return_type
== error_mark_node
)
12812 return error_mark_node
;
12813 /* DR 486 clarifies that creation of a function type with an
12814 invalid return type is a deduction failure. */
12815 if (TREE_CODE (return_type
) == ARRAY_TYPE
12816 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
12818 if (complain
& tf_error
)
12820 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
12821 error ("function returning an array");
12823 error ("function returning a function");
12825 return error_mark_node
;
12828 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
12829 return error_mark_node
;
12831 if (!late_return_type_p
)
12833 /* Substitute the argument types. */
12834 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
12835 complain
, in_decl
);
12836 if (arg_types
== error_mark_node
)
12837 return error_mark_node
;
12840 /* Construct a new type node and return it. */
12841 if (TREE_CODE (t
) == FUNCTION_TYPE
)
12843 fntype
= build_function_type (return_type
, arg_types
);
12844 fntype
= apply_memfn_quals (fntype
,
12845 type_memfn_quals (t
),
12846 type_memfn_rqual (t
));
12850 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
12851 /* Don't pick up extra function qualifiers from the basetype. */
12852 r
= cp_build_qualified_type_real (r
, type_memfn_quals (t
), complain
);
12853 if (! MAYBE_CLASS_TYPE_P (r
))
12857 Type deduction may fail for any of the following
12860 -- Attempting to create "pointer to member of T" when T
12861 is not a class type. */
12862 if (complain
& tf_error
)
12863 error ("creating pointer to member function of non-class type %qT",
12865 return error_mark_node
;
12868 fntype
= build_method_type_directly (r
, return_type
,
12869 TREE_CHAIN (arg_types
));
12870 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
12872 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
12874 if (late_return_type_p
)
12875 TYPE_HAS_LATE_RETURN_TYPE (fntype
) = 1;
12880 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12881 ARGS into that specification, and return the substituted
12882 specification. If there is no specification, return NULL_TREE. */
12885 tsubst_exception_specification (tree fntype
,
12887 tsubst_flags_t complain
,
12894 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
12895 new_specs
= NULL_TREE
;
12896 if (specs
&& TREE_PURPOSE (specs
))
12898 /* A noexcept-specifier. */
12899 tree expr
= TREE_PURPOSE (specs
);
12900 if (TREE_CODE (expr
) == INTEGER_CST
)
12904 /* Defer instantiation of noexcept-specifiers to avoid
12905 excessive instantiations (c++/49107). */
12906 new_specs
= make_node (DEFERRED_NOEXCEPT
);
12907 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
12909 /* We already partially instantiated this member template,
12910 so combine the new args with the old. */
12911 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
12912 = DEFERRED_NOEXCEPT_PATTERN (expr
);
12913 DEFERRED_NOEXCEPT_ARGS (new_specs
)
12914 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
12918 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
12919 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
12923 new_specs
= tsubst_copy_and_build
12924 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
12925 /*integral_constant_expression_p=*/true);
12926 new_specs
= build_noexcept_spec (new_specs
, complain
);
12930 if (! TREE_VALUE (specs
))
12937 tree expanded_specs
= NULL_TREE
;
12939 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
12941 /* Expand the pack expansion type. */
12942 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
12946 if (expanded_specs
== error_mark_node
)
12947 return error_mark_node
;
12948 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
12949 len
= TREE_VEC_LENGTH (expanded_specs
);
12952 /* We're substituting into a member template, so
12953 we got a TYPE_PACK_EXPANSION back. Add that
12954 expansion and move on. */
12955 gcc_assert (TREE_CODE (expanded_specs
)
12956 == TYPE_PACK_EXPANSION
);
12957 new_specs
= add_exception_specifier (new_specs
,
12960 specs
= TREE_CHAIN (specs
);
12965 for (i
= 0; i
< len
; ++i
)
12967 if (expanded_specs
)
12968 spec
= TREE_VEC_ELT (expanded_specs
, i
);
12970 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
12971 if (spec
== error_mark_node
)
12973 new_specs
= add_exception_specifier (new_specs
, spec
,
12977 specs
= TREE_CHAIN (specs
);
12983 /* Take the tree structure T and replace template parameters used
12984 therein with the argument vector ARGS. IN_DECL is an associated
12985 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12986 Issue error and warning messages under control of COMPLAIN. Note
12987 that we must be relatively non-tolerant of extensions here, in
12988 order to preserve conformance; if we allow substitutions that
12989 should not be allowed, we may allow argument deductions that should
12990 not succeed, and therefore report ambiguous overload situations
12991 where there are none. In theory, we could allow the substitution,
12992 but indicate that it should have failed, and allow our caller to
12993 make sure that the right thing happens, but we don't try to do this
12996 This function is used for dealing with types, decls and the like;
12997 for expressions, use tsubst_expr or tsubst_copy. */
13000 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
13002 enum tree_code code
;
13003 tree type
, r
= NULL_TREE
;
13005 if (t
== NULL_TREE
|| t
== error_mark_node
13006 || t
== integer_type_node
13007 || t
== void_type_node
13008 || t
== char_type_node
13009 || t
== unknown_type_node
13010 || TREE_CODE (t
) == NAMESPACE_DECL
13011 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
13015 return tsubst_decl (t
, args
, complain
);
13017 if (args
== NULL_TREE
)
13020 code
= TREE_CODE (t
);
13022 if (code
== IDENTIFIER_NODE
)
13023 type
= IDENTIFIER_TYPE_VALUE (t
);
13025 type
= TREE_TYPE (t
);
13027 gcc_assert (type
!= unknown_type_node
);
13029 /* Reuse typedefs. We need to do this to handle dependent attributes,
13030 such as attribute aligned. */
13032 && typedef_variant_p (t
))
13034 tree decl
= TYPE_NAME (t
);
13036 if (alias_template_specialization_p (t
))
13038 /* DECL represents an alias template and we want to
13040 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13041 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13042 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
13044 else if (DECL_CLASS_SCOPE_P (decl
)
13045 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13046 && uses_template_parms (DECL_CONTEXT (decl
)))
13048 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
13049 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
13050 r
= retrieve_specialization (tmpl
, gen_args
, 0);
13052 else if (DECL_FUNCTION_SCOPE_P (decl
)
13053 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
13054 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
13055 r
= retrieve_local_specialization (decl
);
13057 /* The typedef is from a non-template context. */
13063 r
= cp_build_qualified_type_real
13064 (r
, cp_type_quals (t
) | cp_type_quals (r
),
13065 complain
| tf_ignore_bad_quals
);
13070 /* We don't have an instantiation yet, so drop the typedef. */
13071 int quals
= cp_type_quals (t
);
13072 t
= DECL_ORIGINAL_TYPE (decl
);
13073 t
= cp_build_qualified_type_real (t
, quals
,
13074 complain
| tf_ignore_bad_quals
);
13078 bool fndecl_type
= (complain
& tf_fndecl_type
);
13079 complain
&= ~tf_fndecl_type
;
13082 && code
!= TYPENAME_TYPE
13083 && code
!= TEMPLATE_TYPE_PARM
13084 && code
!= IDENTIFIER_NODE
13085 && code
!= FUNCTION_TYPE
13086 && code
!= METHOD_TYPE
)
13087 type
= tsubst (type
, args
, complain
, in_decl
);
13088 if (type
== error_mark_node
)
13089 return error_mark_node
;
13095 case ENUMERAL_TYPE
:
13096 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
13097 /*entering_scope=*/0);
13100 case IDENTIFIER_NODE
:
13111 if (t
== integer_type_node
)
13114 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
13115 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
13119 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
13121 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
13122 /*integral_constant_expression_p=*/false);
13124 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13126 if (TREE_CODE (max
) == NOP_EXPR
13127 && TREE_SIDE_EFFECTS (omax
)
13128 && !TREE_TYPE (max
))
13129 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
13131 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13132 with TREE_SIDE_EFFECTS that indicates this is not an integral
13133 constant expression. */
13134 if (processing_template_decl
13135 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
13137 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
13138 TREE_SIDE_EFFECTS (max
) = 1;
13141 return compute_array_index_type (NULL_TREE
, max
, complain
);
13144 case TEMPLATE_TYPE_PARM
:
13145 case TEMPLATE_TEMPLATE_PARM
:
13146 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13147 case TEMPLATE_PARM_INDEX
:
13152 tree arg
= NULL_TREE
;
13154 /* Early in template argument deduction substitution, we don't
13155 want to reduce the level of 'auto', or it will be confused
13156 with a normal template parm in subsequent deduction. */
13157 if (is_auto (t
) && (complain
& tf_partial
))
13162 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
13163 template_parm_level_and_index (t
, &level
, &idx
);
13165 levels
= TMPL_ARGS_DEPTH (args
);
13166 if (level
<= levels
13167 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args
, level
)) > 0)
13169 arg
= TMPL_ARG (args
, level
, idx
);
13171 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
13173 /* See through ARGUMENT_PACK_SELECT arguments. */
13174 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
13175 /* If the selected argument is an expansion E, that most
13176 likely means we were called from
13177 gen_elem_of_pack_expansion_instantiation during the
13178 substituting of pack an argument pack (which Ith
13179 element is a pack expansion, where I is
13180 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13181 In this case, the Ith element resulting from this
13182 substituting is going to be a pack expansion, which
13183 pattern is the pattern of E. Let's return the
13185 gen_elem_of_pack_expansion_instantiation will
13186 build the resulting pack expansion from it. */
13187 if (PACK_EXPANSION_P (arg
))
13189 /* Make sure we aren't throwing away arg info. */
13190 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg
));
13191 arg
= PACK_EXPANSION_PATTERN (arg
);
13196 if (arg
== error_mark_node
)
13197 return error_mark_node
;
13198 else if (arg
!= NULL_TREE
)
13200 if (ARGUMENT_PACK_P (arg
))
13201 /* If ARG is an argument pack, we don't actually want to
13202 perform a substitution here, because substitutions
13203 for argument packs are only done
13204 element-by-element. We can get to this point when
13205 substituting the type of a non-type template
13206 parameter pack, when that type actually contains
13207 template parameter packs from an outer template, e.g.,
13209 template<typename... Types> struct A {
13210 template<Types... Values> struct B { };
13214 if (code
== TEMPLATE_TYPE_PARM
)
13217 gcc_assert (TYPE_P (arg
));
13219 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
13221 return cp_build_qualified_type_real
13222 (arg
, quals
, complain
| tf_ignore_bad_quals
);
13224 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13226 /* We are processing a type constructed from a
13227 template template parameter. */
13228 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
13229 args
, complain
, in_decl
);
13230 if (argvec
== error_mark_node
)
13231 return error_mark_node
;
13233 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
13234 || TREE_CODE (arg
) == TEMPLATE_DECL
13235 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
13237 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
13238 /* Consider this code:
13240 template <template <class> class Template>
13242 template <class Arg> using Bind = Template<Arg>;
13245 template <template <class> class Template, class Arg>
13246 using Instantiate = Template<Arg>; //#0
13248 template <template <class> class Template,
13251 Instantiate<Internal<Template>::template Bind,
13254 When #1 is parsed, the
13255 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13256 parameter `Template' in #0 matches the
13257 UNBOUND_CLASS_TEMPLATE representing the argument
13258 `Internal<Template>::template Bind'; We then want
13259 to assemble the type `Bind<Argument>' that can't
13260 be fully created right now, because
13261 `Internal<Template>' not being complete, the Bind
13262 template cannot be looked up in that context. So
13263 we need to "store" `Bind<Argument>' for later
13264 when the context of Bind becomes complete. Let's
13265 store that in a TYPENAME_TYPE. */
13266 return make_typename_type (TYPE_CONTEXT (arg
),
13267 build_nt (TEMPLATE_ID_EXPR
,
13268 TYPE_IDENTIFIER (arg
),
13273 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13274 are resolving nested-types in the signature of a
13275 member function templates. Otherwise ARG is a
13276 TEMPLATE_DECL and is the real template to be
13278 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13279 arg
= TYPE_NAME (arg
);
13281 r
= lookup_template_class (arg
,
13283 DECL_CONTEXT (arg
),
13284 /*entering_scope=*/0,
13286 return cp_build_qualified_type_real
13287 (r
, cp_type_quals (t
) | cp_type_quals (r
), complain
);
13289 else if (code
== TEMPLATE_TEMPLATE_PARM
)
13292 /* TEMPLATE_PARM_INDEX. */
13293 return convert_from_reference (unshare_expr (arg
));
13297 /* This can happen during the attempted tsubst'ing in
13298 unify. This means that we don't yet have any information
13299 about the template parameter in question. */
13302 /* If we get here, we must have been looking at a parm for a
13303 more deeply nested template. Make a new version of this
13304 template parameter, but with a lower level. */
13307 case TEMPLATE_TYPE_PARM
:
13308 case TEMPLATE_TEMPLATE_PARM
:
13309 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13310 if (cp_type_quals (t
))
13312 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
13313 r
= cp_build_qualified_type_real
13314 (r
, cp_type_quals (t
),
13315 complain
| (code
== TEMPLATE_TYPE_PARM
13316 ? tf_ignore_bad_quals
: 0));
13318 else if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
13319 && PLACEHOLDER_TYPE_CONSTRAINTS (t
)
13320 && (r
= (TEMPLATE_PARM_DESCENDANTS
13321 (TEMPLATE_TYPE_PARM_INDEX (t
))))
13322 && (r
= TREE_TYPE (r
))
13323 && !PLACEHOLDER_TYPE_CONSTRAINTS (r
))
13324 /* Break infinite recursion when substituting the constraints
13325 of a constrained placeholder. */;
13329 TEMPLATE_TYPE_PARM_INDEX (r
)
13330 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
13331 r
, levels
, args
, complain
);
13332 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
13333 TYPE_MAIN_VARIANT (r
) = r
;
13334 TYPE_POINTER_TO (r
) = NULL_TREE
;
13335 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
13337 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
13339 /* Propagate constraints on placeholders. */
13340 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
13341 PLACEHOLDER_TYPE_CONSTRAINTS (r
)
13342 = tsubst_constraint (constr
, args
, complain
, in_decl
);
13343 else if (tree pl
= CLASS_PLACEHOLDER_TEMPLATE (t
))
13345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl
))
13346 pl
= tsubst (pl
, args
, complain
, in_decl
);
13347 CLASS_PLACEHOLDER_TEMPLATE (r
) = pl
;
13351 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
13352 /* We have reduced the level of the template
13353 template parameter, but not the levels of its
13354 template parameters, so canonical_type_parameter
13355 will not be able to find the canonical template
13356 template parameter for this level. Thus, we
13357 require structural equality checking to compare
13358 TEMPLATE_TEMPLATE_PARMs. */
13359 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13360 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
13361 SET_TYPE_STRUCTURAL_EQUALITY (r
);
13363 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
13365 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
13367 tree tinfo
= TYPE_TEMPLATE_INFO (t
);
13368 /* We might need to substitute into the types of non-type
13369 template parameters. */
13370 tree tmpl
= tsubst (TI_TEMPLATE (tinfo
), args
,
13371 complain
, in_decl
);
13372 if (tmpl
== error_mark_node
)
13373 return error_mark_node
;
13374 tree argvec
= tsubst (TI_ARGS (tinfo
), args
,
13375 complain
, in_decl
);
13376 if (argvec
== error_mark_node
)
13377 return error_mark_node
;
13379 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
13380 = build_template_info (tmpl
, argvec
);
13385 case TEMPLATE_PARM_INDEX
:
13386 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
13390 gcc_unreachable ();
13398 tree purpose
, value
, chain
;
13400 if (t
== void_list_node
)
13403 purpose
= TREE_PURPOSE (t
);
13406 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
13407 if (purpose
== error_mark_node
)
13408 return error_mark_node
;
13410 value
= TREE_VALUE (t
);
13413 value
= tsubst (value
, args
, complain
, in_decl
);
13414 if (value
== error_mark_node
)
13415 return error_mark_node
;
13417 chain
= TREE_CHAIN (t
);
13418 if (chain
&& chain
!= void_type_node
)
13420 chain
= tsubst (chain
, args
, complain
, in_decl
);
13421 if (chain
== error_mark_node
)
13422 return error_mark_node
;
13424 if (purpose
== TREE_PURPOSE (t
)
13425 && value
== TREE_VALUE (t
)
13426 && chain
== TREE_CHAIN (t
))
13428 return hash_tree_cons (purpose
, value
, chain
);
13432 /* We should never be tsubsting a binfo. */
13433 gcc_unreachable ();
13436 /* A vector of template arguments. */
13437 gcc_assert (!type
);
13438 return tsubst_template_args (t
, args
, complain
, in_decl
);
13441 case REFERENCE_TYPE
:
13443 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
13448 Type deduction may fail for any of the following
13451 -- Attempting to create a pointer to reference type.
13452 -- Attempting to create a reference to a reference type or
13453 a reference to void.
13455 Core issue 106 says that creating a reference to a reference
13456 during instantiation is no longer a cause for failure. We
13457 only enforce this check in strict C++98 mode. */
13458 if ((TREE_CODE (type
) == REFERENCE_TYPE
13459 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
13460 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
13462 static location_t last_loc
;
13464 /* We keep track of the last time we issued this error
13465 message to avoid spewing a ton of messages during a
13466 single bad template instantiation. */
13467 if (complain
& tf_error
13468 && last_loc
!= input_location
)
13470 if (VOID_TYPE_P (type
))
13471 error ("forming reference to void");
13472 else if (code
== POINTER_TYPE
)
13473 error ("forming pointer to reference type %qT", type
);
13475 error ("forming reference to reference type %qT", type
);
13476 last_loc
= input_location
;
13479 return error_mark_node
;
13481 else if (TREE_CODE (type
) == FUNCTION_TYPE
13482 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
13483 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
13485 if (complain
& tf_error
)
13487 if (code
== POINTER_TYPE
)
13488 error ("forming pointer to qualified function type %qT",
13491 error ("forming reference to qualified function type %qT",
13494 return error_mark_node
;
13496 else if (code
== POINTER_TYPE
)
13498 r
= build_pointer_type (type
);
13499 if (TREE_CODE (type
) == METHOD_TYPE
)
13500 r
= build_ptrmemfunc_type (r
);
13502 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
13503 /* In C++0x, during template argument substitution, when there is an
13504 attempt to create a reference to a reference type, reference
13505 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13507 "If a template-argument for a template-parameter T names a type
13508 that is a reference to a type A, an attempt to create the type
13509 'lvalue reference to cv T' creates the type 'lvalue reference to
13510 A,' while an attempt to create the type type rvalue reference to
13511 cv T' creates the type T"
13513 r
= cp_build_reference_type
13515 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
13517 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
13518 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
13520 if (r
!= error_mark_node
)
13521 /* Will this ever be needed for TYPE_..._TO values? */
13528 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
13529 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
13533 Type deduction may fail for any of the following
13536 -- Attempting to create "pointer to member of T" when T
13537 is not a class type. */
13538 if (complain
& tf_error
)
13539 error ("creating pointer to member of non-class type %qT", r
);
13540 return error_mark_node
;
13542 if (TREE_CODE (type
) == REFERENCE_TYPE
)
13544 if (complain
& tf_error
)
13545 error ("creating pointer to member reference type %qT", type
);
13546 return error_mark_node
;
13548 if (VOID_TYPE_P (type
))
13550 if (complain
& tf_error
)
13551 error ("creating pointer to member of type void");
13552 return error_mark_node
;
13554 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
13555 if (TREE_CODE (type
) == FUNCTION_TYPE
)
13557 /* The type of the implicit object parameter gets its
13558 cv-qualifiers from the FUNCTION_TYPE. */
13561 = build_memfn_type (type
, r
, type_memfn_quals (type
),
13562 type_memfn_rqual (type
));
13563 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
13564 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
13568 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
13572 case FUNCTION_TYPE
:
13577 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
13578 if (fntype
== error_mark_node
)
13579 return error_mark_node
;
13581 /* Substitute the exception specification. */
13582 specs
= tsubst_exception_specification (t
, args
, complain
, in_decl
,
13583 /*defer_ok*/fndecl_type
);
13584 if (specs
== error_mark_node
)
13585 return error_mark_node
;
13587 fntype
= build_exception_variant (fntype
, specs
);
13592 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
13593 if (domain
== error_mark_node
)
13594 return error_mark_node
;
13596 /* As an optimization, we avoid regenerating the array type if
13597 it will obviously be the same as T. */
13598 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
13601 /* These checks should match the ones in create_array_type_for_decl.
13605 The deduction may fail for any of the following reasons:
13607 -- Attempting to create an array with an element type that
13608 is void, a function type, or a reference type, or [DR337]
13609 an abstract class type. */
13610 if (VOID_TYPE_P (type
)
13611 || TREE_CODE (type
) == FUNCTION_TYPE
13612 || (TREE_CODE (type
) == ARRAY_TYPE
13613 && TYPE_DOMAIN (type
) == NULL_TREE
)
13614 || TREE_CODE (type
) == REFERENCE_TYPE
)
13616 if (complain
& tf_error
)
13617 error ("creating array of %qT", type
);
13618 return error_mark_node
;
13621 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
13622 return error_mark_node
;
13624 r
= build_cplus_array_type (type
, domain
);
13626 if (TYPE_USER_ALIGN (t
))
13628 SET_TYPE_ALIGN (r
, TYPE_ALIGN (t
));
13629 TYPE_USER_ALIGN (r
) = 1;
13635 case TYPENAME_TYPE
:
13637 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13638 in_decl
, /*entering_scope=*/1);
13639 if (ctx
== error_mark_node
)
13640 return error_mark_node
;
13642 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
13643 complain
, in_decl
);
13644 if (f
== error_mark_node
)
13645 return error_mark_node
;
13647 if (!MAYBE_CLASS_TYPE_P (ctx
))
13649 if (complain
& tf_error
)
13650 error ("%qT is not a class, struct, or union type", ctx
);
13651 return error_mark_node
;
13653 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
13655 /* Normally, make_typename_type does not require that the CTX
13656 have complete type in order to allow things like:
13658 template <class T> struct S { typename S<T>::X Y; };
13660 But, such constructs have already been resolved by this
13661 point, so here CTX really should have complete type, unless
13662 it's a partial instantiation. */
13663 ctx
= complete_type (ctx
);
13664 if (!COMPLETE_TYPE_P (ctx
))
13666 if (complain
& tf_error
)
13667 cxx_incomplete_type_error (NULL_TREE
, ctx
);
13668 return error_mark_node
;
13672 f
= make_typename_type (ctx
, f
, typename_type
,
13673 complain
| tf_keep_type_decl
);
13674 if (f
== error_mark_node
)
13676 if (TREE_CODE (f
) == TYPE_DECL
)
13678 complain
|= tf_ignore_bad_quals
;
13682 if (TREE_CODE (f
) != TYPENAME_TYPE
)
13684 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
13686 if (complain
& tf_error
)
13687 error ("%qT resolves to %qT, which is not an enumeration type",
13690 return error_mark_node
;
13692 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
13694 if (complain
& tf_error
)
13695 error ("%qT resolves to %qT, which is is not a class type",
13698 return error_mark_node
;
13702 return cp_build_qualified_type_real
13703 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
13706 case UNBOUND_CLASS_TEMPLATE
:
13708 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
13709 in_decl
, /*entering_scope=*/1);
13710 tree name
= TYPE_IDENTIFIER (t
);
13711 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
13713 if (ctx
== error_mark_node
|| name
== error_mark_node
)
13714 return error_mark_node
;
13717 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
13718 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
13725 ++cp_unevaluated_operand
;
13726 ++c_inhibit_evaluation_warnings
;
13728 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
13730 /*integral_constant_expression_p=*/false);
13732 --cp_unevaluated_operand
;
13733 --c_inhibit_evaluation_warnings
;
13735 type
= finish_typeof (type
);
13736 return cp_build_qualified_type_real (type
,
13738 | cp_type_quals (type
),
13742 case DECLTYPE_TYPE
:
13746 ++cp_unevaluated_operand
;
13747 ++c_inhibit_evaluation_warnings
;
13749 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
13750 complain
|tf_decltype
, in_decl
,
13751 /*function_p*/false,
13752 /*integral_constant_expression*/false);
13754 if (DECLTYPE_FOR_INIT_CAPTURE (t
))
13756 if (type
== NULL_TREE
)
13758 if (complain
& tf_error
)
13759 error ("empty initializer in lambda init-capture");
13760 type
= error_mark_node
;
13762 else if (TREE_CODE (type
) == TREE_LIST
)
13763 type
= build_x_compound_expr_from_list (type
, ELK_INIT
, complain
);
13766 --cp_unevaluated_operand
;
13767 --c_inhibit_evaluation_warnings
;
13769 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
13770 type
= lambda_capture_field_type (type
,
13771 DECLTYPE_FOR_INIT_CAPTURE (t
));
13772 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
13773 type
= lambda_proxy_type (type
);
13776 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
13777 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
13779 /* In a template ~id could be either a complement expression
13780 or an unqualified-id naming a destructor; if instantiating
13781 it produces an expression, it's not an id-expression or
13784 type
= finish_decltype_type (type
, id
, complain
);
13786 return cp_build_qualified_type_real (type
,
13788 | cp_type_quals (type
),
13789 complain
| tf_ignore_bad_quals
);
13792 case UNDERLYING_TYPE
:
13794 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
13795 complain
, in_decl
);
13796 return finish_underlying_type (type
);
13799 case TYPE_ARGUMENT_PACK
:
13800 case NONTYPE_ARGUMENT_PACK
:
13804 if (code
== NONTYPE_ARGUMENT_PACK
)
13806 r
= make_node (code
);
13807 /* Set the already-substituted type. */
13808 TREE_TYPE (r
) = type
;
13811 r
= cxx_make_type (code
);
13813 tree pack_args
= ARGUMENT_PACK_ARGS (t
);
13814 pack_args
= tsubst_template_args (pack_args
, args
, complain
, in_decl
);
13815 SET_ARGUMENT_PACK_ARGS (r
, pack_args
);
13833 /* We should use one of the expression tsubsts for these codes. */
13834 gcc_unreachable ();
13837 sorry ("use of %qs in template", get_tree_code_name (code
));
13838 return error_mark_node
;
13842 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13843 type of the expression on the left-hand side of the "." or "->"
13847 tsubst_baselink (tree baselink
, tree object_type
,
13848 tree args
, tsubst_flags_t complain
, tree in_decl
)
13851 tree qualifying_scope
;
13854 tree template_args
= 0;
13855 bool template_id_p
= false;
13856 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
13858 /* A baselink indicates a function from a base class. Both the
13859 BASELINK_ACCESS_BINFO and the base class referenced may
13860 indicate bases of the template class, rather than the
13861 instantiated class. In addition, lookups that were not
13862 ambiguous before may be ambiguous now. Therefore, we perform
13863 the lookup again. */
13864 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
13865 qualifying_scope
= tsubst (qualifying_scope
, args
,
13866 complain
, in_decl
);
13867 fns
= BASELINK_FUNCTIONS (baselink
);
13868 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
13869 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
13871 template_id_p
= true;
13872 template_args
= TREE_OPERAND (fns
, 1);
13873 fns
= TREE_OPERAND (fns
, 0);
13875 template_args
= tsubst_template_args (template_args
, args
,
13876 complain
, in_decl
);
13878 name
= DECL_NAME (get_first_fn (fns
));
13879 if (IDENTIFIER_TYPENAME_P (name
))
13880 name
= mangle_conv_op_name_for_type (optype
);
13881 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
13884 if (constructor_name_p (name
, qualifying_scope
))
13886 if (complain
& tf_error
)
13887 error ("cannot call constructor %<%T::%D%> directly",
13888 qualifying_scope
, name
);
13890 return error_mark_node
;
13893 /* If lookup found a single function, mark it as used at this
13894 point. (If it lookup found multiple functions the one selected
13895 later by overload resolution will be marked as used at that
13897 if (BASELINK_P (baselink
))
13898 fns
= BASELINK_FUNCTIONS (baselink
);
13899 if (!template_id_p
&& !really_overloaded_fn (fns
)
13900 && !mark_used (OVL_CURRENT (fns
), complain
) && !(complain
& tf_error
))
13901 return error_mark_node
;
13903 /* Add back the template arguments, if present. */
13904 if (BASELINK_P (baselink
) && template_id_p
)
13905 BASELINK_FUNCTIONS (baselink
)
13906 = build2 (TEMPLATE_ID_EXPR
,
13908 BASELINK_FUNCTIONS (baselink
),
13910 /* Update the conversion operator type. */
13911 if (BASELINK_P (baselink
))
13912 BASELINK_OPTYPE (baselink
) = optype
;
13915 object_type
= current_class_type
;
13917 if (qualified
|| name
== complete_dtor_identifier
)
13919 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
13923 /* We need to call adjust_result_of_qualified_name_lookup in case the
13924 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13925 so that we still get virtual function binding. */
13926 BASELINK_QUALIFIED_P (baselink
) = false;
13931 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13932 true if the qualified-id will be a postfix-expression in-and-of
13933 itself; false if more of the postfix-expression follows the
13934 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13938 tsubst_qualified_id (tree qualified_id
, tree args
,
13939 tsubst_flags_t complain
, tree in_decl
,
13940 bool done
, bool address_p
)
13946 tree template_args
;
13947 location_t loc
= UNKNOWN_LOCATION
;
13949 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
13951 /* Figure out what name to look up. */
13952 name
= TREE_OPERAND (qualified_id
, 1);
13953 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
13955 is_template
= true;
13956 loc
= EXPR_LOCATION (name
);
13957 template_args
= TREE_OPERAND (name
, 1);
13959 template_args
= tsubst_template_args (template_args
, args
,
13960 complain
, in_decl
);
13961 if (template_args
== error_mark_node
)
13962 return error_mark_node
;
13963 name
= TREE_OPERAND (name
, 0);
13967 is_template
= false;
13968 template_args
= NULL_TREE
;
13971 /* Substitute into the qualifying scope. When there are no ARGS, we
13972 are just trying to simplify a non-dependent expression. In that
13973 case the qualifying scope may be dependent, and, in any case,
13974 substituting will not help. */
13975 scope
= TREE_OPERAND (qualified_id
, 0);
13978 scope
= tsubst (scope
, args
, complain
, in_decl
);
13979 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
13984 if (dependent_scope_p (scope
))
13987 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
13988 tree r
= build_qualified_name (NULL_TREE
, scope
, expr
,
13989 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
13990 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (qualified_id
);
13994 if (!BASELINK_P (name
) && !DECL_P (expr
))
13996 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
13998 /* A BIT_NOT_EXPR is used to represent a destructor. */
13999 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
14001 error ("qualifying type %qT does not match destructor name ~%qT",
14002 scope
, TREE_OPERAND (expr
, 0));
14003 expr
= error_mark_node
;
14006 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
14007 /*is_type_p=*/0, false);
14010 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
14011 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
14012 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
14014 if (complain
& tf_error
)
14016 error ("dependent-name %qE is parsed as a non-type, but "
14017 "instantiation yields a type", qualified_id
);
14018 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
14020 return error_mark_node
;
14026 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
14028 /* Remember that there was a reference to this entity. */
14029 if (!mark_used (expr
, complain
) && !(complain
& tf_error
))
14030 return error_mark_node
;
14033 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
14035 if (complain
& tf_error
)
14036 qualified_name_lookup_error (scope
,
14037 TREE_OPERAND (qualified_id
, 1),
14038 expr
, input_location
);
14039 return error_mark_node
;
14044 if (variable_template_p (expr
))
14045 expr
= lookup_and_finish_template_variable (expr
, template_args
,
14048 expr
= lookup_template_function (expr
, template_args
);
14051 if (expr
== error_mark_node
&& complain
& tf_error
)
14052 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
14053 expr
, input_location
);
14054 else if (TYPE_P (scope
))
14056 expr
= (adjust_result_of_qualified_name_lookup
14057 (expr
, scope
, current_nonlambda_class_type ()));
14058 expr
= (finish_qualified_id_expr
14059 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
14060 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
14061 /*template_arg_p=*/false, complain
));
14064 /* Expressions do not generally have reference type. */
14065 if (TREE_CODE (expr
) != SCOPE_REF
14066 /* However, if we're about to form a pointer-to-member, we just
14067 want the referenced member referenced. */
14068 && TREE_CODE (expr
) != OFFSET_REF
)
14069 expr
= convert_from_reference (expr
);
14071 if (REF_PARENTHESIZED_P (qualified_id
))
14072 expr
= force_paren_expr (expr
);
14077 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14078 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14082 tsubst_init (tree init
, tree decl
, tree args
,
14083 tsubst_flags_t complain
, tree in_decl
)
14088 init
= tsubst_expr (init
, args
, complain
, in_decl
, false);
14090 if (!init
&& TREE_TYPE (decl
) != error_mark_node
)
14092 /* If we had an initializer but it
14093 instantiated to nothing,
14094 value-initialize the object. This will
14095 only occur when the initializer was a
14096 pack expansion where the parameter packs
14097 used in that expansion were of length
14099 init
= build_value_init (TREE_TYPE (decl
),
14101 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
14102 init
= get_target_expr_sfinae (init
, complain
);
14108 /* Like tsubst, but deals with expressions. This function just replaces
14109 template parms; to finish processing the resultant expression, use
14110 tsubst_copy_and_build or tsubst_expr. */
14113 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
14115 enum tree_code code
;
14118 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
14121 code
= TREE_CODE (t
);
14126 r
= retrieve_local_specialization (t
);
14128 if (r
== NULL_TREE
)
14130 /* We get here for a use of 'this' in an NSDMI as part of a
14131 constructor call or as part of an aggregate initialization. */
14132 if (DECL_NAME (t
) == this_identifier
14133 && ((current_function_decl
14134 && DECL_CONSTRUCTOR_P (current_function_decl
))
14135 || (current_class_ref
14136 && TREE_CODE (current_class_ref
) == PLACEHOLDER_EXPR
)))
14137 return current_class_ptr
;
14139 /* This can happen for a parameter name used later in a function
14140 declaration (such as in a late-specified return type). Just
14141 make a dummy decl, since it's only used for its type. */
14142 gcc_assert (cp_unevaluated_operand
!= 0);
14143 r
= tsubst_decl (t
, args
, complain
);
14144 /* Give it the template pattern as its context; its true context
14145 hasn't been instantiated yet and this is good enough for
14147 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
14150 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14151 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14152 if (!mark_used (r
, complain
) && !(complain
& tf_error
))
14153 return error_mark_node
;
14161 if (DECL_TEMPLATE_PARM_P (t
))
14162 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
14163 /* There is no need to substitute into namespace-scope
14165 if (DECL_NAMESPACE_SCOPE_P (t
))
14167 /* If ARGS is NULL, then T is known to be non-dependent. */
14168 if (args
== NULL_TREE
)
14169 return scalar_constant_value (t
);
14171 /* Unfortunately, we cannot just call lookup_name here.
14174 template <int I> int f() {
14176 struct S { void g() { E e = a; } };
14179 When we instantiate f<7>::S::g(), say, lookup_name is not
14180 clever enough to find f<7>::a. */
14182 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14183 /*entering_scope=*/0);
14185 for (v
= TYPE_VALUES (enum_type
);
14187 v
= TREE_CHAIN (v
))
14188 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
14189 return TREE_VALUE (v
);
14191 /* We didn't find the name. That should never happen; if
14192 name-lookup found it during preliminary parsing, we
14193 should find it again here during instantiation. */
14194 gcc_unreachable ();
14199 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
14201 /* Check for a local specialization set up by
14202 tsubst_pack_expansion. */
14203 if (tree r
= retrieve_local_specialization (t
))
14205 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
14206 r
= ARGUMENT_PACK_SELECT_ARG (r
);
14210 /* When retrieving a capture pack from a generic lambda, remove the
14211 lambda call op's own template argument list from ARGS. Only the
14212 template arguments active for the closure type should be used to
14213 retrieve the pack specialization. */
14214 if (LAMBDA_FUNCTION_P (current_function_decl
)
14215 && (template_class_depth (DECL_CONTEXT (t
))
14216 != TMPL_ARGS_DEPTH (args
)))
14217 args
= strip_innermost_template_args (args
, 1);
14219 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14220 tsubst_decl put in the hash table. */
14221 return retrieve_specialization (t
, args
, 0);
14224 if (DECL_CONTEXT (t
))
14228 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
14229 /*entering_scope=*/1);
14230 if (ctx
!= DECL_CONTEXT (t
))
14232 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
14235 if (complain
& tf_error
)
14236 error ("using invalid field %qD", t
);
14237 return error_mark_node
;
14246 case FUNCTION_DECL
:
14247 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
14248 r
= tsubst (t
, args
, complain
, in_decl
);
14249 else if (local_variable_p (t
)
14250 && uses_template_parms (DECL_CONTEXT (t
)))
14252 r
= retrieve_local_specialization (t
);
14253 if (r
== NULL_TREE
)
14255 /* First try name lookup to find the instantiation. */
14256 r
= lookup_name (DECL_NAME (t
));
14259 /* Make sure that the one we found is the one we want. */
14260 tree ctx
= DECL_CONTEXT (t
);
14261 if (DECL_LANG_SPECIFIC (ctx
) && DECL_TEMPLATE_INFO (ctx
))
14262 ctx
= tsubst (ctx
, args
, complain
, in_decl
);
14263 if (ctx
!= DECL_CONTEXT (r
))
14271 /* This can happen for a variable used in a
14272 late-specified return type of a local lambda, or for a
14273 local static or constant. Building a new VAR_DECL
14274 should be OK in all those cases. */
14275 r
= tsubst_decl (t
, args
, complain
);
14276 if (decl_maybe_constant_var_p (r
))
14278 /* We can't call cp_finish_decl, so handle the
14279 initializer by hand. */
14280 tree init
= tsubst_init (DECL_INITIAL (t
), r
, args
,
14281 complain
, in_decl
);
14282 if (!processing_template_decl
)
14283 init
= maybe_constant_init (init
);
14284 if (processing_template_decl
14285 ? potential_constant_expression (init
)
14286 : reduced_constant_expression_p (init
))
14287 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
14288 = TREE_CONSTANT (r
) = true;
14289 DECL_INITIAL (r
) = init
;
14291 gcc_assert (cp_unevaluated_operand
|| TREE_STATIC (r
)
14292 || decl_constant_var_p (r
)
14293 || errorcount
|| sorrycount
);
14294 if (!processing_template_decl
14295 && !TREE_STATIC (r
))
14296 r
= process_outer_var_ref (r
, complain
);
14298 /* Remember this for subsequent uses. */
14299 if (local_specializations
)
14300 register_local_specialization (r
, t
);
14305 if (!mark_used (r
, complain
))
14306 return error_mark_node
;
14309 case NAMESPACE_DECL
:
14313 /* An OVERLOAD will always be a non-dependent overload set; an
14314 overload set from function scope will just be represented with an
14315 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14316 gcc_assert (!uses_template_parms (t
));
14320 return tsubst_baselink (t
, current_nonlambda_class_type (),
14321 args
, complain
, in_decl
);
14323 case TEMPLATE_DECL
:
14324 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
14325 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
14326 args
, complain
, in_decl
);
14327 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
14328 return tsubst (t
, args
, complain
, in_decl
);
14329 else if (DECL_CLASS_SCOPE_P (t
)
14330 && uses_template_parms (DECL_CONTEXT (t
)))
14332 /* Template template argument like the following example need
14335 template <template <class> class TT> struct C {};
14336 template <class T> struct D {
14337 template <class U> struct E {};
14342 We are processing the template argument `E' in #1 for
14343 the template instantiation #2. Originally, `E' is a
14344 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14345 have to substitute this with one having context `D<int>'. */
14347 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
14348 return lookup_field (context
, DECL_NAME(t
), 0, false);
14351 /* Ordinary template template argument. */
14355 case REINTERPRET_CAST_EXPR
:
14356 case CONST_CAST_EXPR
:
14357 case STATIC_CAST_EXPR
:
14358 case DYNAMIC_CAST_EXPR
:
14359 case IMPLICIT_CONV_EXPR
:
14363 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14364 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14365 return build1 (code
, type
, op0
);
14369 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
14370 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
14372 tree expanded
, op
= TREE_OPERAND (t
, 0);
14375 if (SIZEOF_EXPR_TYPE_P (t
))
14376 op
= TREE_TYPE (op
);
14378 ++cp_unevaluated_operand
;
14379 ++c_inhibit_evaluation_warnings
;
14380 /* We only want to compute the number of arguments. */
14381 if (PACK_EXPANSION_P (op
))
14382 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
14384 expanded
= tsubst_template_args (ARGUMENT_PACK_ARGS (op
),
14385 args
, complain
, in_decl
);
14386 --cp_unevaluated_operand
;
14387 --c_inhibit_evaluation_warnings
;
14389 if (TREE_CODE (expanded
) == TREE_VEC
)
14391 len
= TREE_VEC_LENGTH (expanded
);
14392 /* Set TREE_USED for the benefit of -Wunused. */
14393 for (int i
= 0; i
< len
; i
++)
14394 if (DECL_P (TREE_VEC_ELT (expanded
, i
)))
14395 TREE_USED (TREE_VEC_ELT (expanded
, i
)) = true;
14398 if (expanded
== error_mark_node
)
14399 return error_mark_node
;
14400 else if (PACK_EXPANSION_P (expanded
)
14401 || (TREE_CODE (expanded
) == TREE_VEC
14402 && pack_expansion_args_count (expanded
)))
14405 if (PACK_EXPANSION_P (expanded
))
14407 else if (TREE_VEC_LENGTH (expanded
) == 1)
14408 expanded
= TREE_VEC_ELT (expanded
, 0);
14410 expanded
= make_argument_pack (expanded
);
14412 if (TYPE_P (expanded
))
14413 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
14414 complain
& tf_error
);
14416 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
14417 complain
& tf_error
);
14420 return build_int_cst (size_type_node
, len
);
14422 if (SIZEOF_EXPR_TYPE_P (t
))
14424 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
14425 args
, complain
, in_decl
);
14426 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
14427 r
= build1 (SIZEOF_EXPR
,
14428 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
14429 SIZEOF_EXPR_TYPE_P (r
) = 1;
14436 case TRUTH_NOT_EXPR
:
14439 case UNARY_PLUS_EXPR
: /* Unary + */
14441 case AT_ENCODE_EXPR
:
14445 case REALPART_EXPR
:
14446 case IMAGPART_EXPR
:
14449 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14450 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14451 return build1 (code
, type
, op0
);
14454 case COMPONENT_REF
:
14459 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14460 name
= TREE_OPERAND (t
, 1);
14461 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
14463 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14464 complain
, in_decl
);
14465 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14467 else if (TREE_CODE (name
) == SCOPE_REF
14468 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
14470 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14471 complain
, in_decl
);
14472 name
= TREE_OPERAND (name
, 1);
14473 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
14474 complain
, in_decl
);
14475 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
14476 name
= build_qualified_name (/*type=*/NULL_TREE
,
14478 /*template_p=*/false);
14480 else if (BASELINK_P (name
))
14481 name
= tsubst_baselink (name
,
14482 non_reference (TREE_TYPE (object
)),
14486 name
= tsubst_copy (name
, args
, complain
, in_decl
);
14487 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
14493 case TRUNC_DIV_EXPR
:
14494 case CEIL_DIV_EXPR
:
14495 case FLOOR_DIV_EXPR
:
14496 case ROUND_DIV_EXPR
:
14497 case EXACT_DIV_EXPR
:
14501 case TRUNC_MOD_EXPR
:
14502 case FLOOR_MOD_EXPR
:
14503 case TRUTH_ANDIF_EXPR
:
14504 case TRUTH_ORIF_EXPR
:
14505 case TRUTH_AND_EXPR
:
14506 case TRUTH_OR_EXPR
:
14519 case COMPOUND_EXPR
:
14522 case PREDECREMENT_EXPR
:
14523 case PREINCREMENT_EXPR
:
14524 case POSTDECREMENT_EXPR
:
14525 case POSTINCREMENT_EXPR
:
14527 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14528 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14529 return build_nt (code
, op0
, op1
);
14534 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14535 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14536 return build_qualified_name (/*type=*/NULL_TREE
, op0
, op1
,
14537 QUALIFIED_NAME_IS_TEMPLATE (t
));
14542 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14543 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14544 return build_nt (ARRAY_REF
, op0
, op1
, NULL_TREE
, NULL_TREE
);
14549 int n
= VL_EXP_OPERAND_LENGTH (t
);
14550 tree result
= build_vl_exp (CALL_EXPR
, n
);
14552 for (i
= 0; i
< n
; i
++)
14553 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
14554 complain
, in_decl
);
14560 case PSEUDO_DTOR_EXPR
:
14561 case VEC_PERM_EXPR
:
14563 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14564 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14565 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14566 r
= build_nt (code
, op0
, op1
, op2
);
14567 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14573 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14574 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14575 tree op2
= tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
14576 r
= build_nt (code
, op0
, op1
, op2
);
14577 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
14583 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14584 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14585 r
= build_nt (code
, op0
, op1
);
14586 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
14587 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
14591 case TEMPLATE_ID_EXPR
:
14593 /* Substituted template arguments */
14594 tree fn
= TREE_OPERAND (t
, 0);
14595 tree targs
= TREE_OPERAND (t
, 1);
14597 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
14599 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
14601 return lookup_template_function (fn
, targs
);
14606 tree purpose
, value
, chain
;
14608 if (t
== void_list_node
)
14611 purpose
= TREE_PURPOSE (t
);
14613 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
14614 value
= TREE_VALUE (t
);
14616 value
= tsubst_copy (value
, args
, complain
, in_decl
);
14617 chain
= TREE_CHAIN (t
);
14618 if (chain
&& chain
!= void_type_node
)
14619 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
14620 if (purpose
== TREE_PURPOSE (t
)
14621 && value
== TREE_VALUE (t
)
14622 && chain
== TREE_CHAIN (t
))
14624 return tree_cons (purpose
, value
, chain
);
14629 case ENUMERAL_TYPE
:
14631 case TEMPLATE_TYPE_PARM
:
14632 case TEMPLATE_TEMPLATE_PARM
:
14633 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14634 case TEMPLATE_PARM_INDEX
:
14636 case REFERENCE_TYPE
:
14638 case FUNCTION_TYPE
:
14641 case TYPENAME_TYPE
:
14642 case UNBOUND_CLASS_TEMPLATE
:
14644 case DECLTYPE_TYPE
:
14646 return tsubst (t
, args
, complain
, in_decl
);
14650 /* Fall through. */
14651 case IDENTIFIER_NODE
:
14652 if (IDENTIFIER_TYPENAME_P (t
))
14654 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14655 return mangle_conv_op_name_for_type (new_type
);
14661 /* This is handled by tsubst_copy_and_build. */
14662 gcc_unreachable ();
14666 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14667 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14668 return build_x_va_arg (EXPR_LOCATION (t
), op0
, type
);
14671 case CLEANUP_POINT_EXPR
:
14672 /* We shouldn't have built any of these during initial template
14673 generation. Instead, they should be built during instantiation
14674 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14675 gcc_unreachable ();
14679 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14680 tree op0
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
14681 tree op1
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
14682 r
= build2 (code
, type
, op0
, op1
);
14683 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
14684 if (!mark_used (TREE_OPERAND (r
, 1), complain
)
14685 && !(complain
& tf_error
))
14686 return error_mark_node
;
14690 case EXPR_PACK_EXPANSION
:
14691 error ("invalid use of pack expansion expression");
14692 return error_mark_node
;
14694 case NONTYPE_ARGUMENT_PACK
:
14695 error ("use %<...%> to expand argument pack");
14696 return error_mark_node
;
14699 gcc_checking_assert (t
== void_node
&& VOID_TYPE_P (TREE_TYPE (t
)));
14707 /* Instantiate any typedefs in the type. */
14708 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14709 r
= fold_convert (type
, t
);
14710 gcc_assert (TREE_CODE (r
) == code
);
14715 /* These can sometimes show up in a partial instantiation, but never
14716 involve template parms. */
14717 gcc_assert (!uses_template_parms (t
));
14720 case UNARY_LEFT_FOLD_EXPR
:
14721 return tsubst_unary_left_fold (t
, args
, complain
, in_decl
);
14722 case UNARY_RIGHT_FOLD_EXPR
:
14723 return tsubst_unary_right_fold (t
, args
, complain
, in_decl
);
14724 case BINARY_LEFT_FOLD_EXPR
:
14725 return tsubst_binary_left_fold (t
, args
, complain
, in_decl
);
14726 case BINARY_RIGHT_FOLD_EXPR
:
14727 return tsubst_binary_right_fold (t
, args
, complain
, in_decl
);
14730 /* We shouldn't get here, but keep going if !flag_checking. */
14732 gcc_unreachable ();
14737 /* Helper function for tsubst_omp_clauses, used for instantiation of
14738 OMP_CLAUSE_DECL of clauses. */
14741 tsubst_omp_clause_decl (tree decl
, tree args
, tsubst_flags_t complain
,
14744 if (decl
== NULL_TREE
)
14747 /* Handle an OpenMP array section represented as a TREE_LIST (or
14748 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14749 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14750 TREE_LIST. We can handle it exactly the same as an array section
14751 (purpose, value, and a chain), even though the nomenclature
14752 (low_bound, length, etc) is different. */
14753 if (TREE_CODE (decl
) == TREE_LIST
)
14756 = tsubst_expr (TREE_PURPOSE (decl
), args
, complain
, in_decl
,
14757 /*integral_constant_expression_p=*/false);
14758 tree length
= tsubst_expr (TREE_VALUE (decl
), args
, complain
, in_decl
,
14759 /*integral_constant_expression_p=*/false);
14760 tree chain
= tsubst_omp_clause_decl (TREE_CHAIN (decl
), args
, complain
,
14762 if (TREE_PURPOSE (decl
) == low_bound
14763 && TREE_VALUE (decl
) == length
14764 && TREE_CHAIN (decl
) == chain
)
14766 tree ret
= tree_cons (low_bound
, length
, chain
);
14767 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret
)
14768 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl
);
14771 tree ret
= tsubst_expr (decl
, args
, complain
, in_decl
,
14772 /*integral_constant_expression_p=*/false);
14773 /* Undo convert_from_reference tsubst_expr could have called. */
14775 && REFERENCE_REF_P (ret
)
14776 && !REFERENCE_REF_P (decl
))
14777 ret
= TREE_OPERAND (ret
, 0);
14781 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14784 tsubst_omp_clauses (tree clauses
, enum c_omp_region_type ort
,
14785 tree args
, tsubst_flags_t complain
, tree in_decl
)
14787 tree new_clauses
= NULL_TREE
, nc
, oc
;
14788 tree linear_no_step
= NULL_TREE
;
14790 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
14792 nc
= copy_node (oc
);
14793 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
14796 switch (OMP_CLAUSE_CODE (nc
))
14798 case OMP_CLAUSE_LASTPRIVATE
:
14799 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
14801 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
14802 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
14803 in_decl
, /*integral_constant_expression_p=*/false);
14804 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
14805 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
14808 case OMP_CLAUSE_PRIVATE
:
14809 case OMP_CLAUSE_SHARED
:
14810 case OMP_CLAUSE_FIRSTPRIVATE
:
14811 case OMP_CLAUSE_COPYIN
:
14812 case OMP_CLAUSE_COPYPRIVATE
:
14813 case OMP_CLAUSE_UNIFORM
:
14814 case OMP_CLAUSE_DEPEND
:
14815 case OMP_CLAUSE_FROM
:
14816 case OMP_CLAUSE_TO
:
14817 case OMP_CLAUSE_MAP
:
14818 case OMP_CLAUSE_USE_DEVICE_PTR
:
14819 case OMP_CLAUSE_IS_DEVICE_PTR
:
14820 OMP_CLAUSE_DECL (nc
)
14821 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14824 case OMP_CLAUSE_IF
:
14825 case OMP_CLAUSE_NUM_THREADS
:
14826 case OMP_CLAUSE_SCHEDULE
:
14827 case OMP_CLAUSE_COLLAPSE
:
14828 case OMP_CLAUSE_FINAL
:
14829 case OMP_CLAUSE_DEVICE
:
14830 case OMP_CLAUSE_DIST_SCHEDULE
:
14831 case OMP_CLAUSE_NUM_TEAMS
:
14832 case OMP_CLAUSE_THREAD_LIMIT
:
14833 case OMP_CLAUSE_SAFELEN
:
14834 case OMP_CLAUSE_SIMDLEN
:
14835 case OMP_CLAUSE_NUM_TASKS
:
14836 case OMP_CLAUSE_GRAINSIZE
:
14837 case OMP_CLAUSE_PRIORITY
:
14838 case OMP_CLAUSE_ORDERED
:
14839 case OMP_CLAUSE_HINT
:
14840 case OMP_CLAUSE_NUM_GANGS
:
14841 case OMP_CLAUSE_NUM_WORKERS
:
14842 case OMP_CLAUSE_VECTOR_LENGTH
:
14843 case OMP_CLAUSE_WORKER
:
14844 case OMP_CLAUSE_VECTOR
:
14845 case OMP_CLAUSE_ASYNC
:
14846 case OMP_CLAUSE_WAIT
:
14847 OMP_CLAUSE_OPERAND (nc
, 0)
14848 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
14849 in_decl
, /*integral_constant_expression_p=*/false);
14851 case OMP_CLAUSE_REDUCTION
:
14852 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
))
14854 tree placeholder
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc
);
14855 if (TREE_CODE (placeholder
) == SCOPE_REF
)
14857 tree scope
= tsubst (TREE_OPERAND (placeholder
, 0), args
,
14858 complain
, in_decl
);
14859 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc
)
14860 = build_qualified_name (NULL_TREE
, scope
,
14861 TREE_OPERAND (placeholder
, 1),
14865 gcc_assert (identifier_p (placeholder
));
14867 OMP_CLAUSE_DECL (nc
)
14868 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14871 case OMP_CLAUSE_GANG
:
14872 case OMP_CLAUSE_ALIGNED
:
14873 OMP_CLAUSE_DECL (nc
)
14874 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14876 OMP_CLAUSE_OPERAND (nc
, 1)
14877 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 1), args
, complain
,
14878 in_decl
, /*integral_constant_expression_p=*/false);
14880 case OMP_CLAUSE_LINEAR
:
14881 OMP_CLAUSE_DECL (nc
)
14882 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc
), args
, complain
,
14884 if (OMP_CLAUSE_LINEAR_STEP (oc
) == NULL_TREE
)
14886 gcc_assert (!linear_no_step
);
14887 linear_no_step
= nc
;
14889 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc
))
14890 OMP_CLAUSE_LINEAR_STEP (nc
)
14891 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc
), args
,
14892 complain
, in_decl
);
14894 OMP_CLAUSE_LINEAR_STEP (nc
)
14895 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc
), args
, complain
,
14897 /*integral_constant_expression_p=*/false);
14899 case OMP_CLAUSE_NOWAIT
:
14900 case OMP_CLAUSE_DEFAULT
:
14901 case OMP_CLAUSE_UNTIED
:
14902 case OMP_CLAUSE_MERGEABLE
:
14903 case OMP_CLAUSE_INBRANCH
:
14904 case OMP_CLAUSE_NOTINBRANCH
:
14905 case OMP_CLAUSE_PROC_BIND
:
14906 case OMP_CLAUSE_FOR
:
14907 case OMP_CLAUSE_PARALLEL
:
14908 case OMP_CLAUSE_SECTIONS
:
14909 case OMP_CLAUSE_TASKGROUP
:
14910 case OMP_CLAUSE_NOGROUP
:
14911 case OMP_CLAUSE_THREADS
:
14912 case OMP_CLAUSE_SIMD
:
14913 case OMP_CLAUSE_DEFAULTMAP
:
14914 case OMP_CLAUSE_INDEPENDENT
:
14915 case OMP_CLAUSE_AUTO
:
14916 case OMP_CLAUSE_SEQ
:
14918 case OMP_CLAUSE_TILE
:
14921 for (lnc
= OMP_CLAUSE_TILE_LIST (nc
),
14922 loc
= OMP_CLAUSE_TILE_LIST (oc
);
14924 loc
= TREE_CHAIN (loc
), lnc
= TREE_CHAIN (lnc
))
14926 TREE_VALUE (lnc
) = tsubst_expr (TREE_VALUE (loc
), args
,
14927 complain
, in_decl
, false);
14932 gcc_unreachable ();
14934 if ((ort
& C_ORT_OMP_DECLARE_SIMD
) == C_ORT_OMP
)
14935 switch (OMP_CLAUSE_CODE (nc
))
14937 case OMP_CLAUSE_SHARED
:
14938 case OMP_CLAUSE_PRIVATE
:
14939 case OMP_CLAUSE_FIRSTPRIVATE
:
14940 case OMP_CLAUSE_LASTPRIVATE
:
14941 case OMP_CLAUSE_COPYPRIVATE
:
14942 case OMP_CLAUSE_LINEAR
:
14943 case OMP_CLAUSE_REDUCTION
:
14944 case OMP_CLAUSE_USE_DEVICE_PTR
:
14945 case OMP_CLAUSE_IS_DEVICE_PTR
:
14946 /* tsubst_expr on SCOPE_REF results in returning
14947 finish_non_static_data_member result. Undo that here. */
14948 if (TREE_CODE (OMP_CLAUSE_DECL (oc
)) == SCOPE_REF
14949 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc
), 1))
14950 == IDENTIFIER_NODE
))
14952 tree t
= OMP_CLAUSE_DECL (nc
);
14955 switch (TREE_CODE (v
))
14957 case COMPONENT_REF
:
14961 case POINTER_PLUS_EXPR
:
14962 v
= TREE_OPERAND (v
, 0);
14965 if (DECL_CONTEXT (v
) == current_function_decl
14966 && DECL_ARTIFICIAL (v
)
14967 && DECL_NAME (v
) == this_identifier
)
14968 OMP_CLAUSE_DECL (nc
) = TREE_OPERAND (t
, 1);
14975 else if (VAR_P (OMP_CLAUSE_DECL (oc
))
14976 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc
))
14977 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc
))
14978 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc
))
14979 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc
)))
14981 tree decl
= OMP_CLAUSE_DECL (nc
);
14984 if (!DECL_LANG_SPECIFIC (decl
))
14985 retrofit_lang_decl (decl
);
14986 DECL_OMP_PRIVATIZED_MEMBER (decl
) = 1;
14995 new_clauses
= nreverse (new_clauses
);
14996 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
14998 new_clauses
= finish_omp_clauses (new_clauses
, ort
);
14999 if (linear_no_step
)
15000 for (nc
= new_clauses
; nc
; nc
= OMP_CLAUSE_CHAIN (nc
))
15001 if (nc
== linear_no_step
)
15003 OMP_CLAUSE_LINEAR_STEP (nc
) = NULL_TREE
;
15007 return new_clauses
;
15010 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15013 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
15016 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15018 tree purpose
, value
, chain
;
15023 if (TREE_CODE (t
) != TREE_LIST
)
15024 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
15025 /*function_p=*/false,
15026 /*integral_constant_expression_p=*/false);
15028 if (t
== void_list_node
)
15031 purpose
= TREE_PURPOSE (t
);
15033 purpose
= RECUR (purpose
);
15034 value
= TREE_VALUE (t
);
15037 if (TREE_CODE (value
) != LABEL_DECL
)
15038 value
= RECUR (value
);
15041 value
= lookup_label (DECL_NAME (value
));
15042 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
15043 TREE_USED (value
) = 1;
15046 chain
= TREE_CHAIN (t
);
15047 if (chain
&& chain
!= void_type_node
)
15048 chain
= RECUR (chain
);
15049 return tree_cons (purpose
, value
, chain
);
15053 /* Used to temporarily communicate the list of #pragma omp parallel
15054 clauses to #pragma omp for instantiation if they are combined
15057 static tree
*omp_parallel_combined_clauses
;
15059 /* Substitute one OMP_FOR iterator. */
15062 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree orig_declv
,
15063 tree initv
, tree condv
, tree incrv
, tree
*clauses
,
15064 tree args
, tsubst_flags_t complain
, tree in_decl
,
15065 bool integral_constant_expression_p
)
15067 #define RECUR(NODE) \
15068 tsubst_expr ((NODE), args, complain, in_decl, \
15069 integral_constant_expression_p)
15070 tree decl
, init
, cond
, incr
;
15072 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
15073 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
15075 if (orig_declv
&& OMP_FOR_ORIG_DECLS (t
))
15077 tree o
= TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t
), i
);
15078 TREE_VEC_ELT (orig_declv
, i
) = RECUR (o
);
15081 decl
= TREE_OPERAND (init
, 0);
15082 init
= TREE_OPERAND (init
, 1);
15083 tree decl_expr
= NULL_TREE
;
15084 if (init
&& TREE_CODE (init
) == DECL_EXPR
)
15086 /* We need to jump through some hoops to handle declarations in the
15087 init-statement, since we might need to handle auto deduction,
15088 but we need to keep control of initialization. */
15090 init
= DECL_INITIAL (DECL_EXPR_DECL (init
));
15091 decl
= tsubst_decl (decl
, args
, complain
);
15095 if (TREE_CODE (decl
) == SCOPE_REF
)
15097 decl
= RECUR (decl
);
15098 if (TREE_CODE (decl
) == COMPONENT_REF
)
15102 switch (TREE_CODE (v
))
15104 case COMPONENT_REF
:
15108 case POINTER_PLUS_EXPR
:
15109 v
= TREE_OPERAND (v
, 0);
15112 if (DECL_CONTEXT (v
) == current_function_decl
15113 && DECL_ARTIFICIAL (v
)
15114 && DECL_NAME (v
) == this_identifier
)
15116 decl
= TREE_OPERAND (decl
, 1);
15117 decl
= omp_privatize_field (decl
, false);
15127 decl
= RECUR (decl
);
15129 init
= RECUR (init
);
15131 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
15132 if (auto_node
&& init
)
15134 = do_auto_deduction (TREE_TYPE (decl
), init
, auto_node
);
15136 gcc_assert (!type_dependent_expression_p (decl
));
15138 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
15142 /* Declare the variable, but don't let that initialize it. */
15143 tree init_sav
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
15144 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL_TREE
;
15146 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init_sav
;
15149 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
15150 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15151 if (TREE_CODE (incr
) == MODIFY_EXPR
)
15153 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15154 tree rhs
= RECUR (TREE_OPERAND (incr
, 1));
15155 incr
= build_x_modify_expr (EXPR_LOCATION (incr
), lhs
,
15156 NOP_EXPR
, rhs
, complain
);
15159 incr
= RECUR (incr
);
15160 TREE_VEC_ELT (declv
, i
) = decl
;
15161 TREE_VEC_ELT (initv
, i
) = init
;
15162 TREE_VEC_ELT (condv
, i
) = cond
;
15163 TREE_VEC_ELT (incrv
, i
) = incr
;
15169 /* Declare and initialize the variable. */
15177 for (j
= (omp_parallel_combined_clauses
== NULL
? 1 : 0); j
< 2; j
++)
15179 for (pc
= j
? clauses
: omp_parallel_combined_clauses
; *pc
; )
15181 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_PRIVATE
15182 && OMP_CLAUSE_DECL (*pc
) == decl
)
15184 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LASTPRIVATE
15185 && OMP_CLAUSE_DECL (*pc
) == decl
)
15189 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15191 *pc
= OMP_CLAUSE_CHAIN (c
);
15192 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15195 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_FIRSTPRIVATE
15196 && OMP_CLAUSE_DECL (*pc
) == decl
)
15198 error ("iteration variable %qD should not be firstprivate",
15200 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15202 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_REDUCTION
15203 && OMP_CLAUSE_DECL (*pc
) == decl
)
15205 error ("iteration variable %qD should not be reduction",
15207 *pc
= OMP_CLAUSE_CHAIN (*pc
);
15210 pc
= &OMP_CLAUSE_CHAIN (*pc
);
15215 if (*pc
== NULL_TREE
)
15217 tree c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
15218 OMP_CLAUSE_DECL (c
) = decl
;
15219 c
= finish_omp_clauses (c
, C_ORT_OMP
);
15222 OMP_CLAUSE_CHAIN (c
) = *clauses
;
15227 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
15228 if (COMPARISON_CLASS_P (cond
))
15230 tree op0
= RECUR (TREE_OPERAND (cond
, 0));
15231 tree op1
= RECUR (TREE_OPERAND (cond
, 1));
15232 cond
= build2 (TREE_CODE (cond
), boolean_type_node
, op0
, op1
);
15235 cond
= RECUR (cond
);
15236 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
15237 switch (TREE_CODE (incr
))
15239 case PREINCREMENT_EXPR
:
15240 case PREDECREMENT_EXPR
:
15241 case POSTINCREMENT_EXPR
:
15242 case POSTDECREMENT_EXPR
:
15243 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
15244 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
15247 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15248 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15250 tree rhs
= TREE_OPERAND (incr
, 1);
15251 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15252 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15253 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15254 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15255 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15259 incr
= RECUR (incr
);
15262 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
15263 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
15265 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15266 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15267 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
15268 TREE_TYPE (decl
), lhs
,
15269 RECUR (TREE_OPERAND (incr
, 2))));
15271 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
15272 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
15273 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
15275 tree rhs
= TREE_OPERAND (incr
, 2);
15276 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
15277 tree rhs0
= RECUR (TREE_OPERAND (rhs
, 0));
15278 tree rhs1
= RECUR (TREE_OPERAND (rhs
, 1));
15279 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
15280 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
15284 incr
= RECUR (incr
);
15287 incr
= RECUR (incr
);
15291 TREE_VEC_ELT (declv
, i
) = decl
;
15292 TREE_VEC_ELT (initv
, i
) = init
;
15293 TREE_VEC_ELT (condv
, i
) = cond
;
15294 TREE_VEC_ELT (incrv
, i
) = incr
;
15298 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15299 of OMP_TARGET's body. */
15302 tsubst_find_omp_teams (tree
*tp
, int *walk_subtrees
, void *)
15304 *walk_subtrees
= 0;
15305 switch (TREE_CODE (*tp
))
15310 case STATEMENT_LIST
:
15311 *walk_subtrees
= 1;
15319 /* Helper function for tsubst_expr. For decomposition declaration
15320 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15321 also the corresponding decls representing the identifiers
15322 of the decomposition declaration. Return DECL if successful
15323 or error_mark_node otherwise, set *FIRST to the first decl
15324 in the list chained through DECL_CHAIN and *CNT to the number
15328 tsubst_decomp_names (tree decl
, tree pattern_decl
, tree args
,
15329 tsubst_flags_t complain
, tree in_decl
, tree
*first
,
15332 tree decl2
, decl3
, prev
= decl
;
15334 gcc_assert (DECL_NAME (decl
) == NULL_TREE
);
15335 for (decl2
= DECL_CHAIN (pattern_decl
);
15338 && DECL_DECOMPOSITION_P (decl2
)
15339 && DECL_NAME (decl2
);
15340 decl2
= DECL_CHAIN (decl2
))
15343 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2
));
15344 tree v
= DECL_VALUE_EXPR (decl2
);
15345 DECL_HAS_VALUE_EXPR_P (decl2
) = 0;
15346 SET_DECL_VALUE_EXPR (decl2
, NULL_TREE
);
15347 decl3
= tsubst (decl2
, args
, complain
, in_decl
);
15348 SET_DECL_VALUE_EXPR (decl2
, v
);
15349 DECL_HAS_VALUE_EXPR_P (decl2
) = 1;
15351 DECL_TEMPLATE_INSTANTIATED (decl3
) = 1;
15352 maybe_push_decl (decl3
);
15353 if (error_operand_p (decl3
))
15354 decl
= error_mark_node
;
15355 else if (decl
!= error_mark_node
15356 && DECL_CHAIN (decl3
) != prev
)
15358 gcc_assert (errorcount
);
15359 decl
= error_mark_node
;
15368 /* Like tsubst_copy for expressions, etc. but also does semantic
15372 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
15373 bool integral_constant_expression_p
)
15375 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15376 #define RECUR(NODE) \
15377 tsubst_expr ((NODE), args, complain, in_decl, \
15378 integral_constant_expression_p)
15384 if (t
== NULL_TREE
|| t
== error_mark_node
)
15387 loc
= input_location
;
15388 if (EXPR_HAS_LOCATION (t
))
15389 input_location
= EXPR_LOCATION (t
);
15390 if (STATEMENT_CODE_P (TREE_CODE (t
)))
15391 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
15393 switch (TREE_CODE (t
))
15395 case STATEMENT_LIST
:
15397 tree_stmt_iterator i
;
15398 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
15399 RECUR (tsi_stmt (i
));
15403 case CTOR_INITIALIZER
:
15404 finish_mem_initializers (tsubst_initializer_list
15405 (TREE_OPERAND (t
, 0), args
));
15409 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
15413 tmp
= RECUR (EXPR_STMT_EXPR (t
));
15414 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
15415 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
15417 finish_expr_stmt (tmp
);
15421 do_using_directive (USING_STMT_NAMESPACE (t
));
15426 tree decl
, pattern_decl
;
15429 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
15430 if (TREE_CODE (decl
) == LABEL_DECL
)
15431 finish_label_decl (DECL_NAME (decl
));
15432 else if (TREE_CODE (decl
) == USING_DECL
)
15434 tree scope
= USING_DECL_SCOPE (decl
);
15435 tree name
= DECL_NAME (decl
);
15437 scope
= tsubst (scope
, args
, complain
, in_decl
);
15438 decl
= lookup_qualified_name (scope
, name
,
15439 /*is_type_p=*/false,
15440 /*complain=*/false);
15441 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
15442 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
15444 do_local_using_decl (decl
, scope
, name
);
15446 else if (DECL_PACK_P (decl
))
15448 /* Don't build up decls for a variadic capture proxy, we'll
15449 instantiate the elements directly as needed. */
15454 init
= DECL_INITIAL (decl
);
15455 decl
= tsubst (decl
, args
, complain
, in_decl
);
15456 if (decl
!= error_mark_node
)
15458 /* By marking the declaration as instantiated, we avoid
15459 trying to instantiate it. Since instantiate_decl can't
15460 handle local variables, and since we've already done
15461 all that needs to be done, that's the right thing to
15464 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15466 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
15467 /* Anonymous aggregates are a special case. */
15468 finish_anon_union (decl
);
15469 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
15471 DECL_CONTEXT (decl
) = current_function_decl
;
15472 if (DECL_NAME (decl
) == this_identifier
)
15474 tree lam
= DECL_CONTEXT (current_function_decl
);
15475 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
15476 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
15478 insert_capture_proxy (decl
);
15480 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
15481 /* We already did a pushtag. */;
15482 else if (TREE_CODE (decl
) == FUNCTION_DECL
15483 && DECL_OMP_DECLARE_REDUCTION_P (decl
)
15484 && DECL_FUNCTION_SCOPE_P (pattern_decl
))
15486 DECL_CONTEXT (decl
) = NULL_TREE
;
15488 DECL_CONTEXT (decl
) = current_function_decl
;
15489 cp_check_omp_declare_reduction (decl
);
15493 int const_init
= false;
15494 maybe_push_decl (decl
);
15496 && DECL_PRETTY_FUNCTION_P (decl
))
15498 /* For __PRETTY_FUNCTION__ we have to adjust the
15500 const char *const name
15501 = cxx_printable_name (current_function_decl
, 2);
15502 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
15505 init
= tsubst_init (init
, decl
, args
, complain
, in_decl
);
15508 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15510 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
15511 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
15515 decl
= tsubst_decomp_names (decl
, pattern_decl
, args
,
15516 complain
, in_decl
, &first
,
15518 if (decl
!= error_mark_node
)
15519 cp_finish_decomp (decl
, first
, cnt
);
15529 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15530 RECUR (FOR_INIT_STMT (t
));
15531 finish_init_stmt (stmt
);
15532 tmp
= RECUR (FOR_COND (t
));
15533 finish_for_cond (tmp
, stmt
, false);
15534 tmp
= RECUR (FOR_EXPR (t
));
15535 finish_for_expr (tmp
, stmt
);
15536 RECUR (FOR_BODY (t
));
15537 finish_for_stmt (stmt
);
15540 case RANGE_FOR_STMT
:
15543 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
15544 decl
= RANGE_FOR_DECL (t
);
15545 decl
= tsubst (decl
, args
, complain
, in_decl
);
15546 maybe_push_decl (decl
);
15547 expr
= RECUR (RANGE_FOR_EXPR (t
));
15548 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
15552 decl
= tsubst_decomp_names (decl
, RANGE_FOR_DECL (t
), args
,
15553 complain
, in_decl
, &first
, &cnt
);
15554 stmt
= cp_convert_range_for (stmt
, decl
, expr
, first
, cnt
,
15555 RANGE_FOR_IVDEP (t
));
15558 stmt
= cp_convert_range_for (stmt
, decl
, expr
, NULL_TREE
, 0,
15559 RANGE_FOR_IVDEP (t
));
15560 RECUR (RANGE_FOR_BODY (t
));
15561 finish_for_stmt (stmt
);
15566 stmt
= begin_while_stmt ();
15567 tmp
= RECUR (WHILE_COND (t
));
15568 finish_while_stmt_cond (tmp
, stmt
, false);
15569 RECUR (WHILE_BODY (t
));
15570 finish_while_stmt (stmt
);
15574 stmt
= begin_do_stmt ();
15575 RECUR (DO_BODY (t
));
15576 finish_do_body (stmt
);
15577 tmp
= RECUR (DO_COND (t
));
15578 finish_do_stmt (tmp
, stmt
, false);
15582 stmt
= begin_if_stmt ();
15583 IF_STMT_CONSTEXPR_P (stmt
) = IF_STMT_CONSTEXPR_P (t
);
15584 tmp
= RECUR (IF_COND (t
));
15585 tmp
= finish_if_stmt_cond (tmp
, stmt
);
15586 if (IF_STMT_CONSTEXPR_P (t
) && integer_zerop (tmp
))
15587 /* Don't instantiate the THEN_CLAUSE. */;
15590 bool inhibit
= integer_zerop (fold_non_dependent_expr (tmp
));
15592 ++c_inhibit_evaluation_warnings
;
15593 RECUR (THEN_CLAUSE (t
));
15595 --c_inhibit_evaluation_warnings
;
15597 finish_then_clause (stmt
);
15599 if (IF_STMT_CONSTEXPR_P (t
) && integer_nonzerop (tmp
))
15600 /* Don't instantiate the ELSE_CLAUSE. */;
15601 else if (ELSE_CLAUSE (t
))
15603 bool inhibit
= integer_nonzerop (fold_non_dependent_expr (tmp
));
15604 begin_else_clause (stmt
);
15606 ++c_inhibit_evaluation_warnings
;
15607 RECUR (ELSE_CLAUSE (t
));
15609 --c_inhibit_evaluation_warnings
;
15610 finish_else_clause (stmt
);
15613 finish_if_stmt (stmt
);
15617 if (BIND_EXPR_BODY_BLOCK (t
))
15618 stmt
= begin_function_body ();
15620 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
15621 ? BCS_TRY_BLOCK
: 0);
15623 RECUR (BIND_EXPR_BODY (t
));
15625 if (BIND_EXPR_BODY_BLOCK (t
))
15626 finish_function_body (stmt
);
15628 finish_compound_stmt (stmt
);
15632 finish_break_stmt ();
15635 case CONTINUE_STMT
:
15636 finish_continue_stmt ();
15640 stmt
= begin_switch_stmt ();
15641 tmp
= RECUR (SWITCH_STMT_COND (t
));
15642 finish_switch_cond (tmp
, stmt
);
15643 RECUR (SWITCH_STMT_BODY (t
));
15644 finish_switch_stmt (stmt
);
15647 case CASE_LABEL_EXPR
:
15649 tree low
= RECUR (CASE_LOW (t
));
15650 tree high
= RECUR (CASE_HIGH (t
));
15651 tree l
= finish_case_label (EXPR_LOCATION (t
), low
, high
);
15652 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
15653 FALLTHROUGH_LABEL_P (CASE_LABEL (l
))
15654 = FALLTHROUGH_LABEL_P (CASE_LABEL (t
));
15660 tree decl
= LABEL_EXPR_LABEL (t
);
15663 label
= finish_label_stmt (DECL_NAME (decl
));
15664 if (TREE_CODE (label
) == LABEL_DECL
)
15665 FALLTHROUGH_LABEL_P (label
) = FALLTHROUGH_LABEL_P (decl
);
15666 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
15667 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
15672 tmp
= GOTO_DESTINATION (t
);
15673 if (TREE_CODE (tmp
) != LABEL_DECL
)
15674 /* Computed goto's must be tsubst'd into. On the other hand,
15675 non-computed gotos must not be; the identifier in question
15676 will have no binding. */
15679 tmp
= DECL_NAME (tmp
);
15680 finish_goto_stmt (tmp
);
15685 tree string
= RECUR (ASM_STRING (t
));
15686 tree outputs
= tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
,
15687 complain
, in_decl
);
15688 tree inputs
= tsubst_copy_asm_operands (ASM_INPUTS (t
), args
,
15689 complain
, in_decl
);
15690 tree clobbers
= tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
,
15691 complain
, in_decl
);
15692 tree labels
= tsubst_copy_asm_operands (ASM_LABELS (t
), args
,
15693 complain
, in_decl
);
15694 tmp
= finish_asm_stmt (ASM_VOLATILE_P (t
), string
, outputs
, inputs
,
15696 tree asm_expr
= tmp
;
15697 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
15698 asm_expr
= TREE_OPERAND (asm_expr
, 0);
15699 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
15706 stmt
= begin_try_block ();
15707 RECUR (TRY_STMTS (t
));
15708 finish_cleanup_try_block (stmt
);
15709 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
15713 tree compound_stmt
= NULL_TREE
;
15715 if (FN_TRY_BLOCK_P (t
))
15716 stmt
= begin_function_try_block (&compound_stmt
);
15718 stmt
= begin_try_block ();
15720 RECUR (TRY_STMTS (t
));
15722 if (FN_TRY_BLOCK_P (t
))
15723 finish_function_try_block (stmt
);
15725 finish_try_block (stmt
);
15727 RECUR (TRY_HANDLERS (t
));
15728 if (FN_TRY_BLOCK_P (t
))
15729 finish_function_handler_sequence (stmt
, compound_stmt
);
15731 finish_handler_sequence (stmt
);
15737 tree decl
= HANDLER_PARMS (t
);
15741 decl
= tsubst (decl
, args
, complain
, in_decl
);
15742 /* Prevent instantiate_decl from trying to instantiate
15743 this variable. We've already done all that needs to be
15745 if (decl
!= error_mark_node
)
15746 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
15748 stmt
= begin_handler ();
15749 finish_handler_parms (decl
, stmt
);
15750 RECUR (HANDLER_BODY (t
));
15751 finish_handler (stmt
);
15756 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
15757 if (CLASS_TYPE_P (tmp
))
15759 /* Local classes are not independent templates; they are
15760 instantiated along with their containing function. And this
15761 way we don't have to deal with pushing out of one local class
15762 to instantiate a member of another local class. */
15764 /* Closures are handled by the LAMBDA_EXPR. */
15765 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
15766 complete_type (tmp
);
15767 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
15768 if (!DECL_ARTIFICIAL (fn
))
15769 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
15773 case STATIC_ASSERT
:
15777 ++c_inhibit_evaluation_warnings
;
15779 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
15782 /*integral_constant_expression_p=*/true);
15783 --c_inhibit_evaluation_warnings
;
15785 finish_static_assert (condition
,
15786 STATIC_ASSERT_MESSAGE (t
),
15787 STATIC_ASSERT_SOURCE_LOCATION (t
),
15788 /*member_p=*/false);
15793 case OACC_PARALLEL
:
15794 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_ACC
, args
, complain
,
15796 stmt
= begin_omp_parallel ();
15797 RECUR (OMP_BODY (t
));
15798 finish_omp_construct (TREE_CODE (t
), stmt
, tmp
);
15802 r
= push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t
));
15803 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
), C_ORT_OMP
, args
,
15804 complain
, in_decl
);
15805 if (OMP_PARALLEL_COMBINED (t
))
15806 omp_parallel_combined_clauses
= &tmp
;
15807 stmt
= begin_omp_parallel ();
15808 RECUR (OMP_PARALLEL_BODY (t
));
15809 gcc_assert (omp_parallel_combined_clauses
== NULL
);
15810 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
15811 = OMP_PARALLEL_COMBINED (t
);
15812 pop_omp_privatization_clauses (r
);
15816 r
= push_omp_privatization_clauses (false);
15817 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
), C_ORT_OMP
, args
,
15818 complain
, in_decl
);
15819 stmt
= begin_omp_task ();
15820 RECUR (OMP_TASK_BODY (t
));
15821 finish_omp_task (tmp
, stmt
);
15822 pop_omp_privatization_clauses (r
);
15829 case OMP_DISTRIBUTE
:
15833 tree clauses
, body
, pre_body
;
15834 tree declv
= NULL_TREE
, initv
= NULL_TREE
, condv
= NULL_TREE
;
15835 tree orig_declv
= NULL_TREE
;
15836 tree incrv
= NULL_TREE
;
15837 enum c_omp_region_type ort
= C_ORT_OMP
;
15840 if (TREE_CODE (t
) == CILK_SIMD
|| TREE_CODE (t
) == CILK_FOR
)
15842 else if (TREE_CODE (t
) == OACC_LOOP
)
15845 r
= push_omp_privatization_clauses (OMP_FOR_INIT (t
) == NULL_TREE
);
15846 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
), ort
, args
, complain
,
15848 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15850 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15851 if (OMP_FOR_ORIG_DECLS (t
))
15852 orig_declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15853 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15854 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15855 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
15858 stmt
= begin_omp_structured_block ();
15860 pre_body
= push_stmt_list ();
15861 RECUR (OMP_FOR_PRE_BODY (t
));
15862 pre_body
= pop_stmt_list (pre_body
);
15864 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15865 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
15866 tsubst_omp_for_iterator (t
, i
, declv
, orig_declv
, initv
, condv
,
15867 incrv
, &clauses
, args
, complain
, in_decl
,
15868 integral_constant_expression_p
);
15869 omp_parallel_combined_clauses
= NULL
;
15871 body
= push_stmt_list ();
15872 RECUR (OMP_FOR_BODY (t
));
15873 body
= pop_stmt_list (body
);
15875 if (OMP_FOR_INIT (t
) != NULL_TREE
)
15876 t
= finish_omp_for (EXPR_LOCATION (t
), TREE_CODE (t
), declv
,
15877 orig_declv
, initv
, condv
, incrv
, body
, pre_body
,
15881 t
= make_node (TREE_CODE (t
));
15882 TREE_TYPE (t
) = void_type_node
;
15883 OMP_FOR_BODY (t
) = body
;
15884 OMP_FOR_PRE_BODY (t
) = pre_body
;
15885 OMP_FOR_CLAUSES (t
) = clauses
;
15886 SET_EXPR_LOCATION (t
, EXPR_LOCATION (t
));
15890 add_stmt (finish_omp_structured_block (stmt
));
15891 pop_omp_privatization_clauses (r
);
15896 omp_parallel_combined_clauses
= NULL
;
15901 r
= push_omp_privatization_clauses (TREE_CODE (t
) == OMP_TEAMS
15902 && OMP_TEAMS_COMBINED (t
));
15903 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), C_ORT_OMP
, args
, complain
,
15905 stmt
= push_stmt_list ();
15906 RECUR (OMP_BODY (t
));
15907 stmt
= pop_stmt_list (stmt
);
15910 OMP_BODY (t
) = stmt
;
15911 OMP_CLAUSES (t
) = tmp
;
15913 pop_omp_privatization_clauses (r
);
15917 case OMP_TARGET_DATA
:
15919 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), (TREE_CODE (t
) == OACC_DATA
)
15920 ? C_ORT_ACC
: C_ORT_OMP
, args
, complain
,
15922 keep_next_level (true);
15923 stmt
= begin_omp_structured_block ();
15925 RECUR (OMP_BODY (t
));
15926 stmt
= finish_omp_structured_block (stmt
);
15929 OMP_BODY (t
) = stmt
;
15930 OMP_CLAUSES (t
) = tmp
;
15931 if (TREE_CODE (t
) == OMP_TARGET
&& OMP_TARGET_COMBINED (t
))
15933 tree teams
= cp_walk_tree (&stmt
, tsubst_find_omp_teams
, NULL
, NULL
);
15936 /* For combined target teams, ensure the num_teams and
15937 thread_limit clause expressions are evaluated on the host,
15938 before entering the target construct. */
15940 for (c
= OMP_TEAMS_CLAUSES (teams
);
15941 c
; c
= OMP_CLAUSE_CHAIN (c
))
15942 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
15943 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
15944 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
15946 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
15947 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
15948 if (expr
== error_mark_node
)
15950 tmp
= TARGET_EXPR_SLOT (expr
);
15952 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
15953 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
15954 OMP_CLAUSE_FIRSTPRIVATE
);
15955 OMP_CLAUSE_DECL (tc
) = tmp
;
15956 OMP_CLAUSE_CHAIN (tc
) = OMP_TARGET_CLAUSES (t
);
15957 OMP_TARGET_CLAUSES (t
) = tc
;
15966 tmp
= tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t
), C_ORT_ACC
, args
,
15967 complain
, in_decl
);
15968 OACC_DECLARE_CLAUSES (t
) = tmp
;
15972 case OMP_TARGET_UPDATE
:
15973 case OMP_TARGET_ENTER_DATA
:
15974 case OMP_TARGET_EXIT_DATA
:
15975 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_OMP
, args
,
15976 complain
, in_decl
);
15978 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15982 case OACC_ENTER_DATA
:
15983 case OACC_EXIT_DATA
:
15985 tmp
= tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t
), C_ORT_ACC
, args
,
15986 complain
, in_decl
);
15988 OMP_STANDALONE_CLAUSES (t
) = tmp
;
15993 tmp
= tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t
), C_ORT_OMP
, args
,
15994 complain
, in_decl
);
15995 stmt
= push_stmt_list ();
15996 RECUR (OMP_BODY (t
));
15997 stmt
= pop_stmt_list (stmt
);
16000 OMP_BODY (t
) = stmt
;
16001 OMP_ORDERED_CLAUSES (t
) = tmp
;
16007 case OMP_TASKGROUP
:
16008 stmt
= push_stmt_list ();
16009 RECUR (OMP_BODY (t
));
16010 stmt
= pop_stmt_list (stmt
);
16013 OMP_BODY (t
) = stmt
;
16018 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
16019 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
16021 tree op1
= TREE_OPERAND (t
, 1);
16022 tree rhs1
= NULL_TREE
;
16024 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
16026 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
16027 op1
= TREE_OPERAND (op1
, 1);
16029 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16030 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16031 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
16032 NULL_TREE
, NULL_TREE
, rhs1
,
16033 OMP_ATOMIC_SEQ_CST (t
));
16037 tree op1
= TREE_OPERAND (t
, 1);
16038 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
16039 tree rhs1
= NULL_TREE
;
16040 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
16041 enum tree_code opcode
= NOP_EXPR
;
16042 if (code
== OMP_ATOMIC_READ
)
16044 v
= RECUR (TREE_OPERAND (op1
, 0));
16045 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16047 else if (code
== OMP_ATOMIC_CAPTURE_OLD
16048 || code
== OMP_ATOMIC_CAPTURE_NEW
)
16050 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
16051 v
= RECUR (TREE_OPERAND (op1
, 0));
16052 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
16053 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
16055 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
16056 op11
= TREE_OPERAND (op11
, 1);
16058 lhs
= RECUR (TREE_OPERAND (op11
, 0));
16059 rhs
= RECUR (TREE_OPERAND (op11
, 1));
16060 opcode
= TREE_CODE (op11
);
16061 if (opcode
== MODIFY_EXPR
)
16067 lhs
= RECUR (TREE_OPERAND (op1
, 0));
16068 rhs
= RECUR (TREE_OPERAND (op1
, 1));
16070 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
,
16071 OMP_ATOMIC_SEQ_CST (t
));
16075 case TRANSACTION_EXPR
:
16078 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
16079 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
16081 if (TRANSACTION_EXPR_IS_STMT (t
))
16083 tree body
= TRANSACTION_EXPR_BODY (t
);
16084 tree noex
= NULL_TREE
;
16085 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
16087 noex
= MUST_NOT_THROW_COND (body
);
16088 if (noex
== NULL_TREE
)
16089 noex
= boolean_true_node
;
16090 body
= TREE_OPERAND (body
, 0);
16092 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
16094 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
16098 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
16099 RECUR (TRANSACTION_EXPR_BODY (t
)),
16106 case MUST_NOT_THROW_EXPR
:
16108 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16109 tree cond
= RECUR (MUST_NOT_THROW_COND (t
));
16110 RETURN (build_must_not_throw_expr (op0
, cond
));
16113 case EXPR_PACK_EXPANSION
:
16114 error ("invalid use of pack expansion expression");
16115 RETURN (error_mark_node
);
16117 case NONTYPE_ARGUMENT_PACK
:
16118 error ("use %<...%> to expand argument pack");
16119 RETURN (error_mark_node
);
16121 case CILK_SPAWN_STMT
:
16122 cfun
->calls_cilk_spawn
= 1;
16123 RETURN (build_cilk_spawn (EXPR_LOCATION (t
), RECUR (CILK_SPAWN_FN (t
))));
16125 case CILK_SYNC_STMT
:
16126 RETURN (build_cilk_sync ());
16128 case COMPOUND_EXPR
:
16129 tmp
= RECUR (TREE_OPERAND (t
, 0));
16130 if (tmp
== NULL_TREE
)
16131 /* If the first operand was a statement, we're done with it. */
16132 RETURN (RECUR (TREE_OPERAND (t
, 1)));
16133 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
16134 RECUR (TREE_OPERAND (t
, 1)),
16137 case ANNOTATE_EXPR
:
16138 tmp
= RECUR (TREE_OPERAND (t
, 0));
16139 RETURN (build2_loc (EXPR_LOCATION (t
), ANNOTATE_EXPR
,
16140 TREE_TYPE (tmp
), tmp
, RECUR (TREE_OPERAND (t
, 1))));
16143 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
16145 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16146 /*function_p=*/false,
16147 integral_constant_expression_p
));
16150 RETURN (NULL_TREE
);
16152 input_location
= loc
;
16158 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16159 function. For description of the body see comment above
16160 cp_parser_omp_declare_reduction_exprs. */
16163 tsubst_omp_udr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
16165 if (t
== NULL_TREE
|| t
== error_mark_node
)
16168 gcc_assert (TREE_CODE (t
) == STATEMENT_LIST
);
16170 tree_stmt_iterator tsi
;
16173 memset (stmts
, 0, sizeof stmts
);
16174 for (i
= 0, tsi
= tsi_start (t
);
16175 i
< 7 && !tsi_end_p (tsi
);
16176 i
++, tsi_next (&tsi
))
16177 stmts
[i
] = tsi_stmt (tsi
);
16178 gcc_assert (tsi_end_p (tsi
));
16182 gcc_assert (TREE_CODE (stmts
[0]) == DECL_EXPR
16183 && TREE_CODE (stmts
[1]) == DECL_EXPR
);
16184 tree omp_out
= tsubst (DECL_EXPR_DECL (stmts
[0]),
16185 args
, complain
, in_decl
);
16186 tree omp_in
= tsubst (DECL_EXPR_DECL (stmts
[1]),
16187 args
, complain
, in_decl
);
16188 DECL_CONTEXT (omp_out
) = current_function_decl
;
16189 DECL_CONTEXT (omp_in
) = current_function_decl
;
16190 keep_next_level (true);
16191 tree block
= begin_omp_structured_block ();
16192 tsubst_expr (stmts
[2], args
, complain
, in_decl
, false);
16193 block
= finish_omp_structured_block (block
);
16194 block
= maybe_cleanup_point_expr_void (block
);
16195 add_decl_expr (omp_out
);
16196 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts
[0])))
16197 TREE_NO_WARNING (omp_out
) = 1;
16198 add_decl_expr (omp_in
);
16199 finish_expr_stmt (block
);
16203 gcc_assert (TREE_CODE (stmts
[3]) == DECL_EXPR
16204 && TREE_CODE (stmts
[4]) == DECL_EXPR
);
16205 tree omp_priv
= tsubst (DECL_EXPR_DECL (stmts
[3]),
16206 args
, complain
, in_decl
);
16207 tree omp_orig
= tsubst (DECL_EXPR_DECL (stmts
[4]),
16208 args
, complain
, in_decl
);
16209 DECL_CONTEXT (omp_priv
) = current_function_decl
;
16210 DECL_CONTEXT (omp_orig
) = current_function_decl
;
16211 keep_next_level (true);
16212 tree block
= begin_omp_structured_block ();
16213 tsubst_expr (stmts
[5], args
, complain
, in_decl
, false);
16214 block
= finish_omp_structured_block (block
);
16215 block
= maybe_cleanup_point_expr_void (block
);
16216 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
16217 add_decl_expr (omp_priv
);
16218 add_decl_expr (omp_orig
);
16219 finish_expr_stmt (block
);
16221 add_decl_expr (omp_orig
);
16225 /* T is a postfix-expression that is not being used in a function
16226 call. Return the substituted version of T. */
16229 tsubst_non_call_postfix_expression (tree t
, tree args
,
16230 tsubst_flags_t complain
,
16233 if (TREE_CODE (t
) == SCOPE_REF
)
16234 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
16235 /*done=*/false, /*address_p=*/false);
16237 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
16238 /*function_p=*/false,
16239 /*integral_constant_expression_p=*/false);
16244 /* Like tsubst but deals with expressions and performs semantic
16245 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16248 tsubst_copy_and_build (tree t
,
16250 tsubst_flags_t complain
,
16253 bool integral_constant_expression_p
)
16255 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16256 #define RECUR(NODE) \
16257 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16258 /*function_p=*/false, \
16259 integral_constant_expression_p)
16264 if (t
== NULL_TREE
|| t
== error_mark_node
)
16267 loc
= input_location
;
16268 if (EXPR_HAS_LOCATION (t
))
16269 input_location
= EXPR_LOCATION (t
);
16271 /* N3276 decltype magic only applies to calls at the top level or on the
16272 right side of a comma. */
16273 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
16274 complain
&= ~tf_decltype
;
16276 switch (TREE_CODE (t
))
16280 /* Fall through. */
16281 case IDENTIFIER_NODE
:
16285 bool non_integral_constant_expression_p
;
16286 const char *error_msg
;
16288 if (IDENTIFIER_TYPENAME_P (t
))
16290 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16291 t
= mangle_conv_op_name_for_type (new_type
);
16294 /* Look up the name. */
16295 decl
= lookup_name (t
);
16297 /* By convention, expressions use ERROR_MARK_NODE to indicate
16298 failure, not NULL_TREE. */
16299 if (decl
== NULL_TREE
)
16300 decl
= error_mark_node
;
16302 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
16304 integral_constant_expression_p
,
16305 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
16306 &non_integral_constant_expression_p
,
16307 /*template_p=*/false,
16309 /*address_p=*/false,
16310 /*template_arg_p=*/false,
16315 if (!function_p
&& identifier_p (decl
))
16317 if (complain
& tf_error
)
16318 unqualified_name_lookup_error (decl
);
16319 decl
= error_mark_node
;
16324 case TEMPLATE_ID_EXPR
:
16327 tree templ
= RECUR (TREE_OPERAND (t
, 0));
16328 tree targs
= TREE_OPERAND (t
, 1);
16331 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
16332 if (targs
== error_mark_node
)
16333 return error_mark_node
;
16335 if (variable_template_p (templ
))
16336 RETURN (lookup_and_finish_template_variable (templ
, targs
, complain
));
16338 if (TREE_CODE (templ
) == COMPONENT_REF
)
16340 object
= TREE_OPERAND (templ
, 0);
16341 templ
= TREE_OPERAND (templ
, 1);
16344 object
= NULL_TREE
;
16345 templ
= lookup_template_function (templ
, targs
);
16348 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
16349 object
, templ
, NULL_TREE
));
16351 RETURN (baselink_for_fns (templ
));
16356 tree r
= RECUR (TREE_OPERAND (t
, 0));
16358 if (REFERENCE_REF_P (t
))
16360 /* A type conversion to reference type will be enclosed in
16361 such an indirect ref, but the substitution of the cast
16362 will have also added such an indirect ref. */
16363 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
16364 r
= convert_from_reference (r
);
16367 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
16368 complain
|decltype_flag
);
16370 if (TREE_CODE (r
) == INDIRECT_REF
)
16371 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
16378 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16379 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16380 RETURN (build_nop (type
, op0
));
16383 case IMPLICIT_CONV_EXPR
:
16385 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16386 tree expr
= RECUR (TREE_OPERAND (t
, 0));
16387 int flags
= LOOKUP_IMPLICIT
;
16388 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
16389 flags
= LOOKUP_NORMAL
;
16390 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
16396 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16397 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16398 RETURN (build1 (CONVERT_EXPR
, type
, op0
));
16402 case REINTERPRET_CAST_EXPR
:
16403 case CONST_CAST_EXPR
:
16404 case DYNAMIC_CAST_EXPR
:
16405 case STATIC_CAST_EXPR
:
16408 tree op
, r
= NULL_TREE
;
16410 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
16411 if (integral_constant_expression_p
16412 && !cast_valid_in_integral_constant_expression_p (type
))
16414 if (complain
& tf_error
)
16415 error ("a cast to a type other than an integral or "
16416 "enumeration type cannot appear in a constant-expression");
16417 RETURN (error_mark_node
);
16420 op
= RECUR (TREE_OPERAND (t
, 0));
16422 warning_sentinel
s(warn_useless_cast
);
16423 switch (TREE_CODE (t
))
16426 r
= build_functional_cast (type
, op
, complain
);
16428 case REINTERPRET_CAST_EXPR
:
16429 r
= build_reinterpret_cast (type
, op
, complain
);
16431 case CONST_CAST_EXPR
:
16432 r
= build_const_cast (type
, op
, complain
);
16434 case DYNAMIC_CAST_EXPR
:
16435 r
= build_dynamic_cast (type
, op
, complain
);
16437 case STATIC_CAST_EXPR
:
16438 r
= build_static_cast (type
, op
, complain
);
16441 gcc_unreachable ();
16447 case POSTDECREMENT_EXPR
:
16448 case POSTINCREMENT_EXPR
:
16449 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16450 args
, complain
, in_decl
);
16451 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
16452 complain
|decltype_flag
));
16454 case PREDECREMENT_EXPR
:
16455 case PREINCREMENT_EXPR
:
16459 case TRUTH_NOT_EXPR
:
16460 case UNARY_PLUS_EXPR
: /* Unary + */
16461 case REALPART_EXPR
:
16462 case IMAGPART_EXPR
:
16463 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
16464 RECUR (TREE_OPERAND (t
, 0)),
16465 complain
|decltype_flag
));
16467 case FIX_TRUNC_EXPR
:
16468 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
16472 op1
= TREE_OPERAND (t
, 0);
16473 if (TREE_CODE (op1
) == LABEL_DECL
)
16474 RETURN (finish_label_address_expr (DECL_NAME (op1
),
16475 EXPR_LOCATION (op1
)));
16476 if (TREE_CODE (op1
) == SCOPE_REF
)
16477 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
16478 /*done=*/true, /*address_p=*/true);
16480 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
16482 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
16483 complain
|decltype_flag
));
16488 case TRUNC_DIV_EXPR
:
16489 case CEIL_DIV_EXPR
:
16490 case FLOOR_DIV_EXPR
:
16491 case ROUND_DIV_EXPR
:
16492 case EXACT_DIV_EXPR
:
16496 case TRUNC_MOD_EXPR
:
16497 case FLOOR_MOD_EXPR
:
16498 case TRUTH_ANDIF_EXPR
:
16499 case TRUTH_ORIF_EXPR
:
16500 case TRUTH_AND_EXPR
:
16501 case TRUTH_OR_EXPR
:
16517 warning_sentinel
s1(warn_type_limits
);
16518 warning_sentinel
s2(warn_div_by_zero
);
16519 warning_sentinel
s3(warn_logical_op
);
16520 warning_sentinel
s4(warn_tautological_compare
);
16521 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16522 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16523 tree r
= build_x_binary_op
16524 (input_location
, TREE_CODE (t
),
16526 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
16528 : TREE_CODE (TREE_OPERAND (t
, 0))),
16530 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
16532 : TREE_CODE (TREE_OPERAND (t
, 1))),
16534 complain
|decltype_flag
);
16535 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
16536 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16541 case POINTER_PLUS_EXPR
:
16543 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16544 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16545 return fold_build_pointer_plus (op0
, op1
);
16549 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
16550 /*address_p=*/false));
16552 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16553 args
, complain
, in_decl
);
16554 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
16555 RECUR (TREE_OPERAND (t
, 1)),
16556 complain
|decltype_flag
));
16558 case ARRAY_NOTATION_REF
:
16560 tree start_index
, length
, stride
;
16561 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
16562 args
, complain
, in_decl
);
16563 start_index
= RECUR (ARRAY_NOTATION_START (t
));
16564 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
16565 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
16566 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
16567 length
, stride
, TREE_TYPE (op1
)));
16570 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0))
16571 || ARGUMENT_PACK_P (TREE_OPERAND (t
, 0)))
16572 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
16579 op1
= TREE_OPERAND (t
, 0);
16580 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
16581 op1
= TREE_TYPE (op1
);
16584 /* When there are no ARGS, we are trying to evaluate a
16585 non-dependent expression from the parser. Trying to do
16586 the substitutions may not work. */
16588 op1
= TREE_TYPE (op1
);
16592 ++cp_unevaluated_operand
;
16593 ++c_inhibit_evaluation_warnings
;
16595 op1
= tsubst (op1
, args
, complain
, in_decl
);
16597 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16598 /*function_p=*/false,
16599 /*integral_constant_expression_p=*/
16601 --cp_unevaluated_operand
;
16602 --c_inhibit_evaluation_warnings
;
16605 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
16606 complain
& tf_error
);
16608 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
16609 complain
& tf_error
);
16610 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
16612 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
16614 if (!processing_template_decl
&& TYPE_P (op1
))
16616 r
= build_min (SIZEOF_EXPR
, size_type_node
,
16617 build1 (NOP_EXPR
, op1
, error_mark_node
));
16618 SIZEOF_EXPR_TYPE_P (r
) = 1;
16621 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
16622 TREE_SIDE_EFFECTS (r
) = 0;
16623 TREE_READONLY (r
) = 1;
16625 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
16630 case AT_ENCODE_EXPR
:
16632 op1
= TREE_OPERAND (t
, 0);
16633 ++cp_unevaluated_operand
;
16634 ++c_inhibit_evaluation_warnings
;
16635 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16636 /*function_p=*/false,
16637 /*integral_constant_expression_p=*/false);
16638 --cp_unevaluated_operand
;
16639 --c_inhibit_evaluation_warnings
;
16640 RETURN (objc_build_encode_expr (op1
));
16643 case NOEXCEPT_EXPR
:
16644 op1
= TREE_OPERAND (t
, 0);
16645 ++cp_unevaluated_operand
;
16646 ++c_inhibit_evaluation_warnings
;
16647 ++cp_noexcept_operand
;
16648 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
16649 /*function_p=*/false,
16650 /*integral_constant_expression_p=*/false);
16651 --cp_unevaluated_operand
;
16652 --c_inhibit_evaluation_warnings
;
16653 --cp_noexcept_operand
;
16654 RETURN (finish_noexcept_expr (op1
, complain
));
16658 warning_sentinel
s(warn_div_by_zero
);
16659 tree lhs
= RECUR (TREE_OPERAND (t
, 0));
16660 tree rhs
= RECUR (TREE_OPERAND (t
, 2));
16661 tree r
= build_x_modify_expr
16662 (EXPR_LOCATION (t
), lhs
, TREE_CODE (TREE_OPERAND (t
, 1)), rhs
,
16663 complain
|decltype_flag
);
16664 /* TREE_NO_WARNING must be set if either the expression was
16665 parenthesized or it uses an operator such as >>= rather
16666 than plain assignment. In the former case, it was already
16667 set and must be copied. In the latter case,
16668 build_x_modify_expr sets it and it must not be reset
16670 if (TREE_NO_WARNING (t
))
16671 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
16677 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
16678 args
, complain
, in_decl
);
16679 /* Remember that there was a reference to this entity. */
16681 && !mark_used (op1
, complain
) && !(complain
& tf_error
))
16682 RETURN (error_mark_node
);
16683 RETURN (build_x_arrow (input_location
, op1
, complain
));
16687 tree placement
= RECUR (TREE_OPERAND (t
, 0));
16688 tree init
= RECUR (TREE_OPERAND (t
, 3));
16689 vec
<tree
, va_gc
> *placement_vec
;
16690 vec
<tree
, va_gc
> *init_vec
;
16693 if (placement
== NULL_TREE
)
16694 placement_vec
= NULL
;
16697 placement_vec
= make_tree_vector ();
16698 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
16699 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
16702 /* If there was an initializer in the original tree, but it
16703 instantiated to an empty list, then we should pass a
16704 non-NULL empty vector to tell build_new that it was an
16705 empty initializer() rather than no initializer. This can
16706 only happen when the initializer is a pack expansion whose
16707 parameter packs are of length zero. */
16708 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
16712 init_vec
= make_tree_vector ();
16713 if (init
== void_node
)
16714 gcc_assert (init_vec
!= NULL
);
16717 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
16718 vec_safe_push (init_vec
, TREE_VALUE (init
));
16722 tree op1
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
16723 tree op2
= RECUR (TREE_OPERAND (t
, 2));
16724 ret
= build_new (&placement_vec
, op1
, op2
, &init_vec
,
16725 NEW_EXPR_USE_GLOBAL (t
),
16728 if (placement_vec
!= NULL
)
16729 release_tree_vector (placement_vec
);
16730 if (init_vec
!= NULL
)
16731 release_tree_vector (init_vec
);
16738 tree op0
= RECUR (TREE_OPERAND (t
, 0));
16739 tree op1
= RECUR (TREE_OPERAND (t
, 1));
16740 RETURN (delete_sanity (op0
, op1
,
16741 DELETE_EXPR_USE_VEC (t
),
16742 DELETE_EXPR_USE_GLOBAL (t
),
16746 case COMPOUND_EXPR
:
16748 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
16749 complain
& ~tf_decltype
, in_decl
,
16750 /*function_p=*/false,
16751 integral_constant_expression_p
);
16752 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
16754 RECUR (TREE_OPERAND (t
, 1)),
16755 complain
|decltype_flag
));
16761 vec
<tree
, va_gc
> *call_args
;
16762 unsigned int nargs
, i
;
16767 function
= CALL_EXPR_FN (t
);
16768 /* Internal function with no arguments. */
16769 if (function
== NULL_TREE
&& call_expr_nargs (t
) == 0)
16772 /* When we parsed the expression, we determined whether or
16773 not Koenig lookup should be performed. */
16774 koenig_p
= KOENIG_LOOKUP_P (t
);
16775 if (function
== NULL_TREE
)
16778 qualified_p
= false;
16780 else if (TREE_CODE (function
) == SCOPE_REF
)
16782 qualified_p
= true;
16783 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
16785 /*address_p=*/false);
16787 else if (koenig_p
&& identifier_p (function
))
16789 /* Do nothing; calling tsubst_copy_and_build on an identifier
16790 would incorrectly perform unqualified lookup again.
16792 Note that we can also have an IDENTIFIER_NODE if the earlier
16793 unqualified lookup found a member function; in that case
16794 koenig_p will be false and we do want to do the lookup
16795 again to find the instantiated member function.
16797 FIXME but doing that causes c++/15272, so we need to stop
16798 using IDENTIFIER_NODE in that situation. */
16799 qualified_p
= false;
16803 if (TREE_CODE (function
) == COMPONENT_REF
)
16805 tree op
= TREE_OPERAND (function
, 1);
16807 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
16808 || (BASELINK_P (op
)
16809 && BASELINK_QUALIFIED_P (op
)));
16812 qualified_p
= false;
16814 if (TREE_CODE (function
) == ADDR_EXPR
16815 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
16816 /* Avoid error about taking the address of a constructor. */
16817 function
= TREE_OPERAND (function
, 0);
16819 function
= tsubst_copy_and_build (function
, args
, complain
,
16822 integral_constant_expression_p
);
16824 if (BASELINK_P (function
))
16825 qualified_p
= true;
16828 nargs
= call_expr_nargs (t
);
16829 call_args
= make_tree_vector ();
16830 for (i
= 0; i
< nargs
; ++i
)
16832 tree arg
= CALL_EXPR_ARG (t
, i
);
16834 if (!PACK_EXPANSION_P (arg
))
16835 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
16838 /* Expand the pack expansion and push each entry onto
16840 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
16841 if (TREE_CODE (arg
) == TREE_VEC
)
16843 unsigned int len
, j
;
16845 len
= TREE_VEC_LENGTH (arg
);
16846 for (j
= 0; j
< len
; ++j
)
16848 tree value
= TREE_VEC_ELT (arg
, j
);
16849 if (value
!= NULL_TREE
)
16850 value
= convert_from_reference (value
);
16851 vec_safe_push (call_args
, value
);
16856 /* A partial substitution. Add one entry. */
16857 vec_safe_push (call_args
, arg
);
16862 /* We do not perform argument-dependent lookup if normal
16863 lookup finds a non-function, in accordance with the
16864 expected resolution of DR 218. */
16866 && ((is_overloaded_fn (function
)
16867 /* If lookup found a member function, the Koenig lookup is
16868 not appropriate, even if an unqualified-name was used
16869 to denote the function. */
16870 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
16871 || identifier_p (function
))
16872 /* Only do this when substitution turns a dependent call
16873 into a non-dependent call. */
16874 && type_dependent_expression_p_push (t
)
16875 && !any_type_dependent_arguments_p (call_args
))
16876 function
= perform_koenig_lookup (function
, call_args
, tf_none
);
16878 if (function
!= NULL_TREE
16879 && identifier_p (function
)
16880 && !any_type_dependent_arguments_p (call_args
))
16882 if (koenig_p
&& (complain
& tf_warning_or_error
))
16884 /* For backwards compatibility and good diagnostics, try
16885 the unqualified lookup again if we aren't in SFINAE
16887 tree unq
= (tsubst_copy_and_build
16888 (function
, args
, complain
, in_decl
, true,
16889 integral_constant_expression_p
));
16890 if (unq
== error_mark_node
)
16892 release_tree_vector (call_args
);
16893 RETURN (error_mark_node
);
16896 if (unq
!= function
)
16899 if (INDIRECT_REF_P (fn
))
16900 fn
= TREE_OPERAND (fn
, 0);
16901 if (TREE_CODE (fn
) == COMPONENT_REF
)
16902 fn
= TREE_OPERAND (fn
, 1);
16903 if (is_overloaded_fn (fn
))
16904 fn
= get_first_fn (fn
);
16905 if (permerror (EXPR_LOC_OR_LOC (t
, input_location
),
16906 "%qD was not declared in this scope, "
16907 "and no declarations were found by "
16908 "argument-dependent lookup at the point "
16909 "of instantiation", function
))
16912 /* Can't say anything more. */;
16913 else if (DECL_CLASS_SCOPE_P (fn
))
16915 location_t loc
= EXPR_LOC_OR_LOC (t
,
16918 "declarations in dependent base %qT are "
16919 "not found by unqualified lookup",
16920 DECL_CLASS_CONTEXT (fn
));
16921 if (current_class_ptr
)
16923 "use %<this->%D%> instead", function
);
16926 "use %<%T::%D%> instead",
16927 current_class_name
, function
);
16930 inform (DECL_SOURCE_LOCATION (fn
),
16931 "%qD declared here, later in the "
16932 "translation unit", fn
);
16937 if (identifier_p (function
))
16939 if (complain
& tf_error
)
16940 unqualified_name_lookup_error (function
);
16941 release_tree_vector (call_args
);
16942 RETURN (error_mark_node
);
16946 /* Remember that there was a reference to this entity. */
16947 if (function
!= NULL_TREE
16948 && DECL_P (function
)
16949 && !mark_used (function
, complain
) && !(complain
& tf_error
))
16951 release_tree_vector (call_args
);
16952 RETURN (error_mark_node
);
16955 /* Put back tf_decltype for the actual call. */
16956 complain
|= decltype_flag
;
16958 if (function
== NULL_TREE
)
16959 switch (CALL_EXPR_IFN (t
))
16962 gcc_assert (nargs
== 1);
16963 if (vec_safe_length (call_args
) != 1)
16965 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
16966 "wrong number of arguments to "
16967 "%<__builtin_launder%>");
16968 ret
= error_mark_node
;
16971 ret
= finish_builtin_launder (EXPR_LOC_OR_LOC (t
,
16973 (*call_args
)[0], complain
);
16977 /* Unsupported internal function with arguments. */
16978 gcc_unreachable ();
16980 else if (TREE_CODE (function
) == OFFSET_REF
)
16981 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
16983 else if (TREE_CODE (function
) == COMPONENT_REF
)
16985 tree instance
= TREE_OPERAND (function
, 0);
16986 tree fn
= TREE_OPERAND (function
, 1);
16988 if (processing_template_decl
16989 && (type_dependent_expression_p (instance
)
16990 || (!BASELINK_P (fn
)
16991 && TREE_CODE (fn
) != FIELD_DECL
)
16992 || type_dependent_expression_p (fn
)
16993 || any_type_dependent_arguments_p (call_args
)))
16994 ret
= build_nt_call_vec (function
, call_args
);
16995 else if (!BASELINK_P (fn
))
16996 ret
= finish_call_expr (function
, &call_args
,
16997 /*disallow_virtual=*/false,
16998 /*koenig_p=*/false,
17001 ret
= (build_new_method_call
17003 &call_args
, NULL_TREE
,
17004 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
17009 ret
= finish_call_expr (function
, &call_args
,
17010 /*disallow_virtual=*/qualified_p
,
17014 release_tree_vector (call_args
);
17016 if (ret
!= error_mark_node
)
17018 bool op
= CALL_EXPR_OPERATOR_SYNTAX (t
);
17019 bool ord
= CALL_EXPR_ORDERED_ARGS (t
);
17020 bool rev
= CALL_EXPR_REVERSE_ARGS (t
);
17021 bool thk
= CALL_FROM_THUNK_P (t
);
17022 if (op
|| ord
|| rev
|| thk
)
17024 function
= extract_call_expr (ret
);
17025 CALL_EXPR_OPERATOR_SYNTAX (function
) = op
;
17026 CALL_EXPR_ORDERED_ARGS (function
) = ord
;
17027 CALL_EXPR_REVERSE_ARGS (function
) = rev
;
17030 CALL_FROM_THUNK_P (function
) = true;
17031 /* The thunk location is not interesting. */
17032 SET_EXPR_LOCATION (function
, UNKNOWN_LOCATION
);
17042 tree cond
= RECUR (TREE_OPERAND (t
, 0));
17043 tree folded_cond
= fold_non_dependent_expr (cond
);
17046 if (TREE_CODE (folded_cond
) == INTEGER_CST
)
17048 if (integer_zerop (folded_cond
))
17050 ++c_inhibit_evaluation_warnings
;
17051 exp1
= RECUR (TREE_OPERAND (t
, 1));
17052 --c_inhibit_evaluation_warnings
;
17053 exp2
= RECUR (TREE_OPERAND (t
, 2));
17057 exp1
= RECUR (TREE_OPERAND (t
, 1));
17058 ++c_inhibit_evaluation_warnings
;
17059 exp2
= RECUR (TREE_OPERAND (t
, 2));
17060 --c_inhibit_evaluation_warnings
;
17062 cond
= folded_cond
;
17066 exp1
= RECUR (TREE_OPERAND (t
, 1));
17067 exp2
= RECUR (TREE_OPERAND (t
, 2));
17070 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
17071 cond
, exp1
, exp2
, complain
));
17074 case PSEUDO_DTOR_EXPR
:
17076 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17077 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17078 tree op2
= tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
17079 RETURN (finish_pseudo_destructor_expr (op0
, op1
, op2
,
17085 tree purpose
, value
, chain
;
17087 if (t
== void_list_node
)
17090 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
17091 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
17093 /* We have pack expansions, so expand those and
17094 create a new list out of it. */
17095 tree purposevec
= NULL_TREE
;
17096 tree valuevec
= NULL_TREE
;
17100 /* Expand the argument expressions. */
17101 if (TREE_PURPOSE (t
))
17102 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
17103 complain
, in_decl
);
17104 if (TREE_VALUE (t
))
17105 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
17106 complain
, in_decl
);
17108 /* Build the rest of the list. */
17109 chain
= TREE_CHAIN (t
);
17110 if (chain
&& chain
!= void_type_node
)
17111 chain
= RECUR (chain
);
17113 /* Determine the number of arguments. */
17114 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
17116 len
= TREE_VEC_LENGTH (purposevec
);
17117 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
17119 else if (TREE_CODE (valuevec
) == TREE_VEC
)
17120 len
= TREE_VEC_LENGTH (valuevec
);
17123 /* Since we only performed a partial substitution into
17124 the argument pack, we only RETURN (a single list
17126 if (purposevec
== TREE_PURPOSE (t
)
17127 && valuevec
== TREE_VALUE (t
)
17128 && chain
== TREE_CHAIN (t
))
17131 RETURN (tree_cons (purposevec
, valuevec
, chain
));
17134 /* Convert the argument vectors into a TREE_LIST */
17138 /* Grab the Ith values. */
17140 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
17143 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
17146 /* Build the list (backwards). */
17147 chain
= tree_cons (purpose
, value
, chain
);
17153 purpose
= TREE_PURPOSE (t
);
17155 purpose
= RECUR (purpose
);
17156 value
= TREE_VALUE (t
);
17158 value
= RECUR (value
);
17159 chain
= TREE_CHAIN (t
);
17160 if (chain
&& chain
!= void_type_node
)
17161 chain
= RECUR (chain
);
17162 if (purpose
== TREE_PURPOSE (t
)
17163 && value
== TREE_VALUE (t
)
17164 && chain
== TREE_CHAIN (t
))
17166 RETURN (tree_cons (purpose
, value
, chain
));
17169 case COMPONENT_REF
:
17176 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
17177 args
, complain
, in_decl
);
17178 /* Remember that there was a reference to this entity. */
17179 if (DECL_P (object
)
17180 && !mark_used (object
, complain
) && !(complain
& tf_error
))
17181 RETURN (error_mark_node
);
17182 object_type
= TREE_TYPE (object
);
17184 member
= TREE_OPERAND (t
, 1);
17185 if (BASELINK_P (member
))
17186 member
= tsubst_baselink (member
,
17187 non_reference (TREE_TYPE (object
)),
17188 args
, complain
, in_decl
);
17190 member
= tsubst_copy (member
, args
, complain
, in_decl
);
17191 if (member
== error_mark_node
)
17192 RETURN (error_mark_node
);
17194 if (type_dependent_expression_p (object
))
17195 /* We can't do much here. */;
17196 else if (!CLASS_TYPE_P (object_type
))
17198 if (scalarish_type_p (object_type
))
17200 tree s
= NULL_TREE
;
17201 tree dtor
= member
;
17203 if (TREE_CODE (dtor
) == SCOPE_REF
)
17205 s
= TREE_OPERAND (dtor
, 0);
17206 dtor
= TREE_OPERAND (dtor
, 1);
17208 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
17210 dtor
= TREE_OPERAND (dtor
, 0);
17212 RETURN (finish_pseudo_destructor_expr
17213 (object
, s
, dtor
, input_location
));
17217 else if (TREE_CODE (member
) == SCOPE_REF
17218 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
17220 /* Lookup the template functions now that we know what the
17222 tree scope
= TREE_OPERAND (member
, 0);
17223 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
17224 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
17225 member
= lookup_qualified_name (scope
, tmpl
,
17226 /*is_type_p=*/false,
17227 /*complain=*/false);
17228 if (BASELINK_P (member
))
17230 BASELINK_FUNCTIONS (member
)
17231 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
17233 member
= (adjust_result_of_qualified_name_lookup
17234 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
17239 qualified_name_lookup_error (scope
, tmpl
, member
,
17241 RETURN (error_mark_node
);
17244 else if (TREE_CODE (member
) == SCOPE_REF
17245 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
17246 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
17248 if (complain
& tf_error
)
17250 if (TYPE_P (TREE_OPERAND (member
, 0)))
17251 error ("%qT is not a class or namespace",
17252 TREE_OPERAND (member
, 0));
17254 error ("%qD is not a class or namespace",
17255 TREE_OPERAND (member
, 0));
17257 RETURN (error_mark_node
);
17259 else if (TREE_CODE (member
) == FIELD_DECL
)
17261 r
= finish_non_static_data_member (member
, object
, NULL_TREE
);
17262 if (TREE_CODE (r
) == COMPONENT_REF
)
17263 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17267 r
= finish_class_member_access_expr (object
, member
,
17268 /*template_p=*/false,
17270 if (TREE_CODE (r
) == COMPONENT_REF
)
17271 REF_PARENTHESIZED_P (r
) = REF_PARENTHESIZED_P (t
);
17276 RETURN (build_throw
17277 (RECUR (TREE_OPERAND (t
, 0))));
17281 vec
<constructor_elt
, va_gc
> *n
;
17282 constructor_elt
*ce
;
17283 unsigned HOST_WIDE_INT idx
;
17284 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17285 bool process_index_p
;
17287 bool need_copy_p
= false;
17290 if (type
== error_mark_node
)
17291 RETURN (error_mark_node
);
17293 /* digest_init will do the wrong thing if we let it. */
17294 if (type
&& TYPE_PTRMEMFUNC_P (type
))
17297 /* We do not want to process the index of aggregate
17298 initializers as they are identifier nodes which will be
17299 looked up by digest_init. */
17300 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
17302 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
17303 newlen
= vec_safe_length (n
);
17304 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
17306 if (ce
->index
&& process_index_p
17307 /* An identifier index is looked up in the type
17308 being initialized, not the current scope. */
17309 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
17310 ce
->index
= RECUR (ce
->index
);
17312 if (PACK_EXPANSION_P (ce
->value
))
17314 /* Substitute into the pack expansion. */
17315 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
17318 if (ce
->value
== error_mark_node
17319 || PACK_EXPANSION_P (ce
->value
))
17321 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
17322 /* Just move the argument into place. */
17323 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
17326 /* Update the length of the final CONSTRUCTOR
17327 arguments vector, and note that we will need to
17329 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
17330 need_copy_p
= true;
17334 ce
->value
= RECUR (ce
->value
);
17339 vec
<constructor_elt
, va_gc
> *old_n
= n
;
17341 vec_alloc (n
, newlen
);
17342 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
17344 if (TREE_CODE (ce
->value
) == TREE_VEC
)
17346 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
17347 for (i
= 0; i
< len
; ++i
)
17348 CONSTRUCTOR_APPEND_ELT (n
, 0,
17349 TREE_VEC_ELT (ce
->value
, i
));
17352 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
17356 r
= build_constructor (init_list_type_node
, n
);
17357 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
17359 if (TREE_HAS_CONSTRUCTOR (t
))
17360 RETURN (finish_compound_literal (type
, r
, complain
));
17362 TREE_TYPE (r
) = type
;
17368 tree operand_0
= TREE_OPERAND (t
, 0);
17369 if (TYPE_P (operand_0
))
17371 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
17372 RETURN (get_typeid (operand_0
, complain
));
17376 operand_0
= RECUR (operand_0
);
17377 RETURN (build_typeid (operand_0
, complain
));
17384 else if (DECL_PACK_P (t
))
17386 /* We don't build decls for an instantiation of a
17387 variadic capture proxy, we instantiate the elements
17389 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
17390 return RECUR (DECL_VALUE_EXPR (t
));
17396 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
17397 /* ??? We're doing a subset of finish_id_expression here. */
17399 && !processing_template_decl
17400 && !cp_unevaluated_operand
17401 && (TREE_STATIC (r
) || DECL_EXTERNAL (r
))
17402 && CP_DECL_THREAD_LOCAL_P (r
))
17404 if (tree wrap
= get_tls_wrapper_fn (r
))
17405 /* Replace an evaluated use of the thread_local variable with
17406 a call to its wrapper. */
17407 r
= build_cxx_call (wrap
, 0, NULL
, tf_warning_or_error
);
17409 else if (outer_automatic_var_p (r
))
17411 r
= process_outer_var_ref (r
, complain
);
17412 if (is_capture_proxy (r
))
17413 register_local_specialization (r
, t
);
17416 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
17417 /* If the original type was a reference, we'll be wrapped in
17418 the appropriate INDIRECT_REF. */
17419 r
= convert_from_reference (r
);
17425 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17426 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
17427 RETURN (build_x_va_arg (EXPR_LOCATION (t
), op0
, type
));
17430 case OFFSETOF_EXPR
:
17431 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0)),
17432 EXPR_LOCATION (t
)));
17434 case ADDRESSOF_EXPR
:
17435 RETURN (cp_build_addressof (EXPR_LOCATION (t
),
17436 RECUR (TREE_OPERAND (t
, 0)), complain
));
17440 tree type1
= tsubst (TRAIT_EXPR_TYPE1 (t
), args
,
17441 complain
, in_decl
);
17443 tree type2
= TRAIT_EXPR_TYPE2 (t
);
17444 if (type2
&& TREE_CODE (type2
) == TREE_LIST
)
17445 type2
= RECUR (type2
);
17447 type2
= tsubst (type2
, args
, complain
, in_decl
);
17449 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
17454 tree old_stmt_expr
= cur_stmt_expr
;
17455 tree stmt_expr
= begin_stmt_expr ();
17457 cur_stmt_expr
= stmt_expr
;
17458 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
17459 integral_constant_expression_p
);
17460 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
17461 cur_stmt_expr
= old_stmt_expr
;
17463 /* If the resulting list of expression statement is empty,
17464 fold it further into void_node. */
17465 if (empty_expr_stmt_p (stmt_expr
))
17466 stmt_expr
= void_node
;
17468 RETURN (stmt_expr
);
17473 tree r
= build_lambda_expr ();
17475 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
17476 LAMBDA_EXPR_CLOSURE (r
) = type
;
17477 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
17479 LAMBDA_EXPR_LOCATION (r
)
17480 = LAMBDA_EXPR_LOCATION (t
);
17481 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
17482 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
17483 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
17484 LAMBDA_EXPR_DISCRIMINATOR (r
)
17485 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
17486 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
17488 /* No substitution needed. */;
17489 else if (VAR_OR_FUNCTION_DECL_P (scope
))
17490 /* For a function or variable scope, we want to use tsubst so that we
17491 don't complain about referring to an auto before deduction. */
17492 scope
= tsubst (scope
, args
, complain
, in_decl
);
17493 else if (TREE_CODE (scope
) == PARM_DECL
)
17495 /* Look up the parameter we want directly, as tsubst_copy
17496 doesn't do what we need. */
17497 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
17498 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
17499 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
17500 parm
= DECL_CHAIN (parm
);
17502 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17503 if (DECL_CONTEXT (scope
) == NULL_TREE
)
17504 DECL_CONTEXT (scope
) = fn
;
17506 else if (TREE_CODE (scope
) == FIELD_DECL
)
17507 /* For a field, use tsubst_copy so that we look up the existing field
17508 rather than build a new one. */
17509 scope
= RECUR (scope
);
17511 gcc_unreachable ();
17512 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
17514 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
17515 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
17517 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17518 determine_visibility (TYPE_NAME (type
));
17519 /* Now that we know visibility, instantiate the type so we have a
17520 declaration of the op() for later calls to lambda_function. */
17521 complete_type (type
);
17523 if (tree fn
= lambda_function (type
))
17524 LAMBDA_EXPR_RETURN_TYPE (r
) = TREE_TYPE (TREE_TYPE (fn
));
17526 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
17528 insert_pending_capture_proxies ();
17530 RETURN (build_lambda_object (r
));
17534 /* We can get here for a constant initializer of non-dependent type.
17535 FIXME stop folding in cp_parser_initializer_clause. */
17537 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
17542 case TRANSACTION_EXPR
:
17543 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
17544 integral_constant_expression_p
));
17547 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
17549 case VEC_PERM_EXPR
:
17551 tree op0
= RECUR (TREE_OPERAND (t
, 0));
17552 tree op1
= RECUR (TREE_OPERAND (t
, 1));
17553 tree op2
= RECUR (TREE_OPERAND (t
, 2));
17554 RETURN (build_x_vec_perm_expr (input_location
, op0
, op1
, op2
,
17558 case REQUIRES_EXPR
:
17559 RETURN (tsubst_requires_expr (t
, args
, complain
, in_decl
));
17562 /* Handle Objective-C++ constructs, if appropriate. */
17565 = objcp_tsubst_copy_and_build (t
, args
, complain
,
17566 in_decl
, /*function_p=*/false);
17570 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
17576 input_location
= loc
;
17580 /* Verify that the instantiated ARGS are valid. For type arguments,
17581 make sure that the type's linkage is ok. For non-type arguments,
17582 make sure they are constants if they are integral or enumerations.
17583 Emit an error under control of COMPLAIN, and return TRUE on error. */
17586 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
17588 if (dependent_template_arg_p (t
))
17590 if (ARGUMENT_PACK_P (t
))
17592 tree vec
= ARGUMENT_PACK_ARGS (t
);
17593 int len
= TREE_VEC_LENGTH (vec
);
17594 bool result
= false;
17597 for (i
= 0; i
< len
; ++i
)
17598 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
17602 else if (TYPE_P (t
))
17604 /* [basic.link]: A name with no linkage (notably, the name
17605 of a class or enumeration declared in a local scope)
17606 shall not be used to declare an entity with linkage.
17607 This implies that names with no linkage cannot be used as
17610 DR 757 relaxes this restriction for C++0x. */
17611 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
17612 : no_linkage_check (t
, /*relaxed_p=*/false));
17616 /* DR 488 makes use of a type with no linkage cause
17617 type deduction to fail. */
17618 if (complain
& tf_error
)
17620 if (TYPE_UNNAMED_P (nt
))
17621 error ("%qT is/uses unnamed type", t
);
17623 error ("template argument for %qD uses local type %qT",
17628 /* In order to avoid all sorts of complications, we do not
17629 allow variably-modified types as template arguments. */
17630 else if (variably_modified_type_p (t
, NULL_TREE
))
17632 if (complain
& tf_error
)
17633 error ("%qT is a variably modified type", t
);
17637 /* Class template and alias template arguments should be OK. */
17638 else if (DECL_TYPE_TEMPLATE_P (t
))
17640 /* A non-type argument of integral or enumerated type must be a
17642 else if (TREE_TYPE (t
)
17643 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
17644 && !REFERENCE_REF_P (t
)
17645 && !TREE_CONSTANT (t
))
17647 if (complain
& tf_error
)
17648 error ("integral expression %qE is not constant", t
);
17655 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
17657 int ix
, len
= DECL_NTPARMS (tmpl
);
17658 bool result
= false;
17660 for (ix
= 0; ix
!= len
; ix
++)
17662 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
17665 if (result
&& (complain
& tf_error
))
17666 error (" trying to instantiate %qD", tmpl
);
17670 /* We're out of SFINAE context now, so generate diagnostics for the access
17671 errors we saw earlier when instantiating D from TMPL and ARGS. */
17674 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
17676 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17677 tree type
= TREE_TYPE (pattern
);
17678 location_t loc
= input_location
;
17680 push_access_scope (d
);
17681 push_deferring_access_checks (dk_no_deferred
);
17682 input_location
= DECL_SOURCE_LOCATION (pattern
);
17683 tsubst (type
, args
, tf_warning_or_error
, d
);
17684 input_location
= loc
;
17685 pop_deferring_access_checks ();
17686 pop_access_scope (d
);
17689 /* Instantiate the indicated variable, function, or alias template TMPL with
17690 the template arguments in TARG_PTR. */
17693 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17695 tree targ_ptr
= orig_args
;
17699 bool access_ok
= true;
17701 if (tmpl
== error_mark_node
)
17702 return error_mark_node
;
17704 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
17706 /* If this function is a clone, handle it specially. */
17707 if (DECL_CLONED_FUNCTION_P (tmpl
))
17712 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17713 DECL_CLONED_FUNCTION. */
17714 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
17715 targ_ptr
, complain
);
17716 if (spec
== error_mark_node
)
17717 return error_mark_node
;
17719 /* Look for the clone. */
17720 FOR_EACH_CLONE (clone
, spec
)
17721 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
17723 /* We should always have found the clone by now. */
17724 gcc_unreachable ();
17728 if (targ_ptr
== error_mark_node
)
17729 return error_mark_node
;
17731 /* Check to see if we already have this specialization. */
17732 gen_tmpl
= most_general_template (tmpl
);
17733 if (TMPL_ARGS_DEPTH (targ_ptr
)
17734 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
)))
17735 /* targ_ptr only has the innermost template args, so add the outer ones
17736 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17737 the case of a non-dependent call within a template definition). */
17738 targ_ptr
= (add_outermost_template_args
17739 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl
)),
17742 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17743 but it doesn't seem to be on the hot path. */
17744 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
17746 gcc_assert (tmpl
== gen_tmpl
17747 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
17749 || fndecl
== NULL_TREE
);
17751 if (spec
!= NULL_TREE
)
17753 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
17755 if (complain
& tf_error
)
17756 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
17757 return error_mark_node
;
17762 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
17764 return error_mark_node
;
17766 /* We are building a FUNCTION_DECL, during which the access of its
17767 parameters and return types have to be checked. However this
17768 FUNCTION_DECL which is the desired context for access checking
17769 is not built yet. We solve this chicken-and-egg problem by
17770 deferring all checks until we have the FUNCTION_DECL. */
17771 push_deferring_access_checks (dk_deferred
);
17773 /* Instantiation of the function happens in the context of the function
17774 template, not the context of the overload resolution we're doing. */
17775 push_to_top_level ();
17776 /* If there are dependent arguments, e.g. because we're doing partial
17777 ordering, make sure processing_template_decl stays set. */
17778 if (uses_template_parms (targ_ptr
))
17779 ++processing_template_decl
;
17780 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17782 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
17783 complain
, gen_tmpl
, true);
17784 push_nested_class (ctx
);
17787 tree pattern
= DECL_TEMPLATE_RESULT (gen_tmpl
);
17789 fndecl
= NULL_TREE
;
17790 if (VAR_P (pattern
))
17792 /* We need to determine if we're using a partial or explicit
17793 specialization now, because the type of the variable could be
17795 tree tid
= lookup_template_variable (gen_tmpl
, targ_ptr
);
17796 tree elt
= most_specialized_partial_spec (tid
, complain
);
17797 if (elt
== error_mark_node
)
17798 pattern
= error_mark_node
;
17801 tree partial_tmpl
= TREE_VALUE (elt
);
17802 tree partial_args
= TREE_PURPOSE (elt
);
17803 tree partial_pat
= DECL_TEMPLATE_RESULT (partial_tmpl
);
17804 fndecl
= tsubst (partial_pat
, partial_args
, complain
, gen_tmpl
);
17808 /* Substitute template parameters to obtain the specialization. */
17809 if (fndecl
== NULL_TREE
)
17810 fndecl
= tsubst (pattern
, targ_ptr
, complain
, gen_tmpl
);
17811 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
17812 pop_nested_class ();
17813 pop_from_top_level ();
17815 if (fndecl
== error_mark_node
)
17817 pop_deferring_access_checks ();
17818 return error_mark_node
;
17821 /* The DECL_TI_TEMPLATE should always be the immediate parent
17822 template, not the most general template. */
17823 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
17824 DECL_TI_ARGS (fndecl
) = targ_ptr
;
17826 /* Now we know the specialization, compute access previously
17827 deferred. Do no access control for inheriting constructors,
17828 as we already checked access for the inherited constructor. */
17829 if (!(flag_new_inheriting_ctors
17830 && DECL_INHERITED_CTOR (fndecl
)))
17832 push_access_scope (fndecl
);
17833 if (!perform_deferred_access_checks (complain
))
17835 pop_access_scope (fndecl
);
17837 pop_deferring_access_checks ();
17839 /* If we've just instantiated the main entry point for a function,
17840 instantiate all the alternate entry points as well. We do this
17841 by cloning the instantiation of the main entry point, not by
17842 instantiating the template clones. */
17843 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
17844 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
17848 if (!(complain
& tf_error
))
17850 /* Remember to reinstantiate when we're out of SFINAE so the user
17851 can see the errors. */
17852 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
17854 return error_mark_node
;
17859 /* Wrapper for instantiate_template_1. */
17862 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
17865 timevar_push (TV_TEMPLATE_INST
);
17866 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
17867 timevar_pop (TV_TEMPLATE_INST
);
17871 /* Instantiate the alias template TMPL with ARGS. Also push a template
17872 instantiation level, which instantiate_template doesn't do because
17873 functions and variables have sufficient context established by the
17877 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
17879 struct pending_template
*old_last_pend
= last_pending_template
;
17880 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17881 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
17882 return error_mark_node
;
17883 tree tinst
= build_tree_list (tmpl
, args
);
17884 if (!push_tinst_level (tinst
))
17887 return error_mark_node
;
17891 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
17892 args
, tmpl
, complain
,
17893 /*require_all_args=*/true,
17894 /*use_default_args=*/true);
17896 tree r
= instantiate_template (tmpl
, args
, complain
);
17897 pop_tinst_level ();
17898 /* We can't free this if a pending_template entry or last_error_tinst_level
17899 is pointing at it. */
17900 if (last_pending_template
== old_last_pend
17901 && last_error_tinst_level
== old_error_tinst
)
17907 /* PARM is a template parameter pack for FN. Returns true iff
17908 PARM is used in a deducible way in the argument list of FN. */
17911 pack_deducible_p (tree parm
, tree fn
)
17913 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
17914 for (; t
; t
= TREE_CHAIN (t
))
17916 tree type
= TREE_VALUE (t
);
17918 if (!PACK_EXPANSION_P (type
))
17920 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
17921 packs
; packs
= TREE_CHAIN (packs
))
17922 if (template_args_equal (TREE_VALUE (packs
), parm
))
17924 /* The template parameter pack is used in a function parameter
17925 pack. If this is the end of the parameter list, the
17926 template parameter pack is deducible. */
17927 if (TREE_CHAIN (t
) == void_list_node
)
17930 /* Otherwise, not. Well, it could be deduced from
17931 a non-pack parameter, but doing so would end up with
17932 a deduction mismatch, so don't bother. */
17936 /* The template parameter pack isn't used in any function parameter
17937 packs, but it might be used deeper, e.g. tuple<Args...>. */
17941 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17942 NARGS elements of the arguments that are being used when calling
17943 it. TARGS is a vector into which the deduced template arguments
17946 Returns either a FUNCTION_DECL for the matching specialization of FN or
17947 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17948 true, diagnostics will be printed to explain why it failed.
17950 If FN is a conversion operator, or we are trying to produce a specific
17951 specialization, RETURN_TYPE is the return type desired.
17953 The EXPLICIT_TARGS are explicit template arguments provided via a
17956 The parameter STRICT is one of:
17959 We are deducing arguments for a function call, as in
17960 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17961 deducing arguments for a call to the result of a conversion
17962 function template, as in [over.call.object].
17965 We are deducing arguments for a conversion function, as in
17966 [temp.deduct.conv].
17969 We are deducing arguments when doing an explicit instantiation
17970 as in [temp.explicit], when determining an explicit specialization
17971 as in [temp.expl.spec], or when taking the address of a function
17972 template, as in [temp.deduct.funcaddr]. */
17975 fn_type_unification (tree fn
,
17976 tree explicit_targs
,
17979 unsigned int nargs
,
17981 unification_kind_t strict
,
17988 tree decl
= NULL_TREE
;
17989 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
17991 static int deduction_depth
;
17992 struct pending_template
*old_last_pend
= last_pending_template
;
17993 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
17996 if (flag_new_inheriting_ctors
)
17997 fn
= strip_inheriting_ctors (fn
);
17999 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
18001 tree r
= error_mark_node
;
18003 tree full_targs
= targs
;
18004 if (TMPL_ARGS_DEPTH (targs
)
18005 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn
)))
18006 full_targs
= (add_outermost_template_args
18007 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn
)),
18011 complain
|= tf_decltype
;
18013 /* In C++0x, it's possible to have a function template whose type depends
18014 on itself recursively. This is most obvious with decltype, but can also
18015 occur with enumeration scope (c++/48969). So we need to catch infinite
18016 recursion and reject the substitution at deduction time; this function
18017 will return error_mark_node for any repeated substitution.
18019 This also catches excessive recursion such as when f<N> depends on
18020 f<N-1> across all integers, and returns error_mark_node for all the
18021 substitutions back up to the initial one.
18023 This is, of course, not reentrant. */
18024 if (excessive_deduction_depth
)
18025 return error_mark_node
;
18026 tinst
= build_tree_list (fn
, NULL_TREE
);
18029 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
18031 fntype
= TREE_TYPE (fn
);
18032 if (explicit_targs
)
18036 The specified template arguments must match the template
18037 parameters in kind (i.e., type, nontype, template), and there
18038 must not be more arguments than there are parameters;
18039 otherwise type deduction fails.
18041 Nontype arguments must match the types of the corresponding
18042 nontype template parameters, or must be convertible to the
18043 types of the corresponding nontype parameters as specified in
18044 _temp.arg.nontype_, otherwise type deduction fails.
18046 All references in the function type of the function template
18047 to the corresponding template parameters are replaced by the
18048 specified template argument values. If a substitution in a
18049 template parameter or in the function type of the function
18050 template results in an invalid type, type deduction fails. */
18051 int i
, len
= TREE_VEC_LENGTH (tparms
);
18052 location_t loc
= input_location
;
18053 bool incomplete
= false;
18055 if (explicit_targs
== error_mark_node
)
18058 if (TMPL_ARGS_DEPTH (explicit_targs
)
18059 < TMPL_ARGS_DEPTH (full_targs
))
18060 explicit_targs
= add_outermost_template_args (full_targs
,
18063 /* Adjust any explicit template arguments before entering the
18064 substitution context. */
18066 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
18068 /*require_all_args=*/false,
18069 /*use_default_args=*/false));
18070 if (explicit_targs
== error_mark_node
)
18073 /* Substitute the explicit args into the function type. This is
18074 necessary so that, for instance, explicitly declared function
18075 arguments can match null pointed constants. If we were given
18076 an incomplete set of explicit args, we must not do semantic
18077 processing during substitution as we could create partial
18079 for (i
= 0; i
< len
; i
++)
18081 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
18082 bool parameter_pack
= false;
18083 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
18085 /* Dig out the actual parm. */
18086 if (TREE_CODE (parm
) == TYPE_DECL
18087 || TREE_CODE (parm
) == TEMPLATE_DECL
)
18089 parm
= TREE_TYPE (parm
);
18090 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
18092 else if (TREE_CODE (parm
) == PARM_DECL
)
18094 parm
= DECL_INITIAL (parm
);
18095 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
18098 if (!parameter_pack
&& targ
== NULL_TREE
)
18099 /* No explicit argument for this template parameter. */
18102 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
18104 /* Mark the argument pack as "incomplete". We could
18105 still deduce more arguments during unification.
18106 We remove this mark in type_unification_real. */
18109 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
18110 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
18111 = ARGUMENT_PACK_ARGS (targ
);
18114 /* We have some incomplete argument packs. */
18119 TREE_VALUE (tinst
) = explicit_targs
;
18120 if (!push_tinst_level (tinst
))
18122 excessive_deduction_depth
= true;
18125 processing_template_decl
+= incomplete
;
18126 input_location
= DECL_SOURCE_LOCATION (fn
);
18127 /* Ignore any access checks; we'll see them again in
18128 instantiate_template and they might have the wrong
18129 access path at this point. */
18130 push_deferring_access_checks (dk_deferred
);
18131 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
18132 complain
| tf_partial
| tf_fndecl_type
, NULL_TREE
);
18133 pop_deferring_access_checks ();
18134 input_location
= loc
;
18135 processing_template_decl
-= incomplete
;
18136 pop_tinst_level ();
18138 if (fntype
== error_mark_node
)
18141 /* Place the explicitly specified arguments in TARGS. */
18142 explicit_targs
= INNERMOST_TEMPLATE_ARGS (explicit_targs
);
18143 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
18144 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
18147 /* Never do unification on the 'this' parameter. */
18148 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
18150 if (return_type
&& strict
== DEDUCE_CALL
)
18152 /* We're deducing for a call to the result of a template conversion
18153 function. The parms we really want are in return_type. */
18154 if (POINTER_TYPE_P (return_type
))
18155 return_type
= TREE_TYPE (return_type
);
18156 parms
= TYPE_ARG_TYPES (return_type
);
18158 else if (return_type
)
18162 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
18163 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
18164 new_args
[0] = return_type
;
18165 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
18170 /* We allow incomplete unification without an error message here
18171 because the standard doesn't seem to explicitly prohibit it. Our
18172 callers must be ready to deal with unification failures in any
18175 TREE_VALUE (tinst
) = targs
;
18176 /* If we aren't explaining yet, push tinst context so we can see where
18177 any errors (e.g. from class instantiations triggered by instantiation
18178 of default template arguments) come from. If we are explaining, this
18179 context is redundant. */
18180 if (!explain_p
&& !push_tinst_level (tinst
))
18182 excessive_deduction_depth
= true;
18186 /* type_unification_real will pass back any access checks from default
18187 template argument substitution. */
18188 vec
<deferred_access_check
, va_gc
> *checks
;
18191 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
18192 full_targs
, parms
, args
, nargs
, /*subr=*/0,
18193 strict
, flags
, &checks
, explain_p
);
18195 pop_tinst_level ();
18199 /* Now that we have bindings for all of the template arguments,
18200 ensure that the arguments deduced for the template template
18201 parameters have compatible template parameter lists. We cannot
18202 check this property before we have deduced all template
18203 arguments, because the template parameter types of a template
18204 template parameter might depend on prior template parameters
18205 deduced after the template template parameter. The following
18206 ill-formed example illustrates this issue:
18208 template<typename T, template<T> class C> void f(C<5>, T);
18210 template<int N> struct X {};
18213 f(X<5>(), 5l); // error: template argument deduction fails
18216 The template parameter list of 'C' depends on the template type
18217 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18218 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18219 time that we deduce 'C'. */
18220 if (!template_template_parm_bindings_ok_p
18221 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
18223 unify_inconsistent_template_template_parameters (explain_p
);
18227 /* All is well so far. Now, check:
18231 When all template arguments have been deduced, all uses of
18232 template parameters in nondeduced contexts are replaced with
18233 the corresponding deduced argument values. If the
18234 substitution results in an invalid type, as described above,
18235 type deduction fails. */
18236 TREE_VALUE (tinst
) = targs
;
18237 if (!push_tinst_level (tinst
))
18239 excessive_deduction_depth
= true;
18243 /* Also collect access checks from the instantiation. */
18244 reopen_deferring_access_checks (checks
);
18246 decl
= instantiate_template (fn
, targs
, complain
);
18248 checks
= get_deferred_access_checks ();
18249 pop_deferring_access_checks ();
18251 pop_tinst_level ();
18253 if (decl
== error_mark_node
)
18256 /* Now perform any access checks encountered during substitution. */
18257 push_access_scope (decl
);
18258 ok
= perform_access_checks (checks
, complain
);
18259 pop_access_scope (decl
);
18263 /* If we're looking for an exact match, check that what we got
18264 is indeed an exact match. It might not be if some template
18265 parameters are used in non-deduced contexts. But don't check
18266 for an exact match if we have dependent template arguments;
18267 in that case we're doing partial ordering, and we already know
18268 that we have two candidates that will provide the actual type. */
18269 if (strict
== DEDUCE_EXACT
&& !any_dependent_template_arguments_p (targs
))
18271 tree substed
= TREE_TYPE (decl
);
18275 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
18277 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
18278 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
18279 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
18281 unify_type_mismatch (explain_p
, args
[i
],
18282 TREE_VALUE (sarg
));
18287 /* After doing deduction with the inherited constructor, actually return an
18288 instantiation of the inheriting constructor. */
18290 decl
= instantiate_template (orig_fn
, targs
, complain
);
18296 if (excessive_deduction_depth
)
18298 if (deduction_depth
== 0)
18299 /* Reset once we're all the way out. */
18300 excessive_deduction_depth
= false;
18303 /* We can't free this if a pending_template entry or last_error_tinst_level
18304 is pointing at it. */
18305 if (last_pending_template
== old_last_pend
18306 && last_error_tinst_level
== old_error_tinst
)
18312 /* Adjust types before performing type deduction, as described in
18313 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18314 sections are symmetric. PARM is the type of a function parameter
18315 or the return type of the conversion function. ARG is the type of
18316 the argument passed to the call, or the type of the value
18317 initialized with the result of the conversion function.
18318 ARG_EXPR is the original argument expression, which may be null. */
18321 maybe_adjust_types_for_deduction (unification_kind_t strict
,
18334 /* Swap PARM and ARG throughout the remainder of this
18335 function; the handling is precisely symmetric since PARM
18336 will initialize ARG rather than vice versa. */
18337 std::swap (parm
, arg
);
18341 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18342 too, but here handle it by stripping the reference from PARM
18343 rather than by adding it to ARG. */
18344 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18345 && TYPE_REF_IS_RVALUE (*parm
)
18346 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18347 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18348 && TREE_CODE (*arg
) == REFERENCE_TYPE
18349 && !TYPE_REF_IS_RVALUE (*arg
))
18350 *parm
= TREE_TYPE (*parm
);
18351 /* Nothing else to do in this case. */
18355 gcc_unreachable ();
18358 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
18360 /* [temp.deduct.call]
18362 If P is not a reference type:
18364 --If A is an array type, the pointer type produced by the
18365 array-to-pointer standard conversion (_conv.array_) is
18366 used in place of A for type deduction; otherwise,
18368 --If A is a function type, the pointer type produced by
18369 the function-to-pointer standard conversion
18370 (_conv.func_) is used in place of A for type deduction;
18373 --If A is a cv-qualified type, the top level
18374 cv-qualifiers of A's type are ignored for type
18376 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
18377 *arg
= build_pointer_type (TREE_TYPE (*arg
));
18378 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
18379 *arg
= build_pointer_type (*arg
);
18381 *arg
= TYPE_MAIN_VARIANT (*arg
);
18384 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18385 of the form T&&, where T is a template parameter, and the argument
18386 is an lvalue, T is deduced as A& */
18387 if (TREE_CODE (*parm
) == REFERENCE_TYPE
18388 && TYPE_REF_IS_RVALUE (*parm
)
18389 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
18390 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
18391 && (arg_expr
? lvalue_p (arg_expr
)
18392 /* try_one_overload doesn't provide an arg_expr, but
18393 functions are always lvalues. */
18394 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
18395 *arg
= build_reference_type (*arg
);
18397 /* [temp.deduct.call]
18399 If P is a cv-qualified type, the top level cv-qualifiers
18400 of P's type are ignored for type deduction. If P is a
18401 reference type, the type referred to by P is used for
18403 *parm
= TYPE_MAIN_VARIANT (*parm
);
18404 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
18406 *parm
= TREE_TYPE (*parm
);
18407 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
18410 /* DR 322. For conversion deduction, remove a reference type on parm
18411 too (which has been swapped into ARG). */
18412 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
18413 *arg
= TREE_TYPE (*arg
);
18418 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18419 template which does contain any deducible template parameters; check if
18420 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18421 unify_one_argument. */
18424 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
18425 int flags
, bool explain_p
)
18430 type
= TREE_TYPE (arg
);
18434 if (same_type_p (parm
, type
))
18435 return unify_success (explain_p
);
18437 if (strict
== DEDUCE_CONV
)
18439 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
18440 explain_p
? tf_warning_or_error
: tf_none
))
18441 return unify_success (explain_p
);
18443 else if (strict
!= DEDUCE_EXACT
)
18445 if (can_convert_arg (parm
, type
,
18446 TYPE_P (arg
) ? NULL_TREE
: arg
,
18447 flags
, explain_p
? tf_warning_or_error
: tf_none
))
18448 return unify_success (explain_p
);
18451 if (strict
== DEDUCE_EXACT
)
18452 return unify_type_mismatch (explain_p
, parm
, arg
);
18454 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
18457 static bool uses_deducible_template_parms (tree type
);
18459 /* Returns true iff the expression EXPR is one from which a template
18460 argument can be deduced. In other words, if it's an undecorated
18461 use of a template non-type parameter. */
18464 deducible_expression (tree expr
)
18466 /* Strip implicit conversions. */
18467 while (CONVERT_EXPR_P (expr
))
18468 expr
= TREE_OPERAND (expr
, 0);
18469 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
18472 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18473 deducible way; that is, if it has a max value of <PARM> - 1. */
18476 deducible_array_bound (tree domain
)
18478 if (domain
== NULL_TREE
)
18481 tree max
= TYPE_MAX_VALUE (domain
);
18482 if (TREE_CODE (max
) != MINUS_EXPR
)
18485 return deducible_expression (TREE_OPERAND (max
, 0));
18488 /* Returns true iff the template arguments ARGS use a template parameter
18489 in a deducible way. */
18492 deducible_template_args (tree args
)
18494 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
18497 tree elt
= TREE_VEC_ELT (args
, i
);
18498 if (ARGUMENT_PACK_P (elt
))
18499 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
18502 if (PACK_EXPANSION_P (elt
))
18503 elt
= PACK_EXPANSION_PATTERN (elt
);
18504 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
18506 else if (TYPE_P (elt
))
18507 deducible
= uses_deducible_template_parms (elt
);
18509 deducible
= deducible_expression (elt
);
18517 /* Returns true iff TYPE contains any deducible references to template
18518 parameters, as per 14.8.2.5. */
18521 uses_deducible_template_parms (tree type
)
18523 if (PACK_EXPANSION_P (type
))
18524 type
= PACK_EXPANSION_PATTERN (type
);
18531 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18532 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18538 if (POINTER_TYPE_P (type
))
18539 return uses_deducible_template_parms (TREE_TYPE (type
));
18541 /* T[integer-constant ]
18543 if (TREE_CODE (type
) == ARRAY_TYPE
)
18544 return (uses_deducible_template_parms (TREE_TYPE (type
))
18545 || deducible_array_bound (TYPE_DOMAIN (type
)));
18557 if (TYPE_PTRMEM_P (type
))
18558 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
18559 || (uses_deducible_template_parms
18560 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
18562 /* template-name <T> (where template-name refers to a class template)
18563 template-name <i> (where template-name refers to a class template) */
18564 if (CLASS_TYPE_P (type
)
18565 && CLASSTYPE_TEMPLATE_INFO (type
)
18566 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
18567 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18568 (CLASSTYPE_TI_ARGS (type
)));
18573 if (TREE_CODE (type
) == FUNCTION_TYPE
18574 || TREE_CODE (type
) == METHOD_TYPE
)
18576 if (uses_deducible_template_parms (TREE_TYPE (type
)))
18578 tree parm
= TYPE_ARG_TYPES (type
);
18579 if (TREE_CODE (type
) == METHOD_TYPE
)
18580 parm
= TREE_CHAIN (parm
);
18581 for (; parm
; parm
= TREE_CHAIN (parm
))
18582 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
18589 /* Subroutine of type_unification_real and unify_pack_expansion to
18590 handle unification of a single P/A pair. Parameters are as
18591 for those functions. */
18594 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
18595 int subr
, unification_kind_t strict
,
18598 tree arg_expr
= NULL_TREE
;
18601 if (arg
== error_mark_node
|| parm
== error_mark_node
)
18602 return unify_invalid (explain_p
);
18603 if (arg
== unknown_type_node
)
18604 /* We can't deduce anything from this, but we might get all the
18605 template args from other function args. */
18606 return unify_success (explain_p
);
18608 /* Implicit conversions (Clause 4) will be performed on a function
18609 argument to convert it to the type of the corresponding function
18610 parameter if the parameter type contains no template-parameters that
18611 participate in template argument deduction. */
18612 if (strict
!= DEDUCE_EXACT
18613 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
18614 /* For function parameters with no deducible template parameters,
18615 just return. We'll check non-dependent conversions later. */
18616 return unify_success (explain_p
);
18621 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
18622 | UNIFY_ALLOW_MORE_CV_QUAL
18623 | UNIFY_ALLOW_DERIVED
);
18627 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
18631 arg_strict
= UNIFY_ALLOW_NONE
;
18635 gcc_unreachable ();
18638 /* We only do these transformations if this is the top-level
18639 parameter_type_list in a call or declaration matching; in other
18640 situations (nested function declarators, template argument lists) we
18641 won't be comparing a type to an expression, and we don't do any type
18647 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
18648 if (type_unknown_p (arg
))
18650 /* [temp.deduct.type] A template-argument can be
18651 deduced from a pointer to function or pointer
18652 to member function argument if the set of
18653 overloaded functions does not contain function
18654 templates and at most one of a set of
18655 overloaded functions provides a unique
18658 if (resolve_overloaded_unification
18659 (tparms
, targs
, parm
, arg
, strict
,
18660 arg_strict
, explain_p
))
18661 return unify_success (explain_p
);
18662 return unify_overload_resolution_failure (explain_p
, arg
);
18666 arg
= unlowered_expr_type (arg
);
18667 if (arg
== error_mark_node
)
18668 return unify_invalid (explain_p
);
18672 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
18675 if ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
18676 != (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
))
18677 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
18679 /* For deduction from an init-list we need the actual list. */
18680 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
18682 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
18685 /* for_each_template_parm callback that always returns 0. */
18688 zero_r (tree
, void *)
18693 /* for_each_template_parm any_fn callback to handle deduction of a template
18694 type argument from the type of an array bound. */
18697 array_deduction_r (tree t
, void *data
)
18699 tree_pair_p d
= (tree_pair_p
)data
;
18700 tree
&tparms
= d
->purpose
;
18701 tree
&targs
= d
->value
;
18703 if (TREE_CODE (t
) == ARRAY_TYPE
)
18704 if (tree dom
= TYPE_DOMAIN (t
))
18705 if (tree max
= TYPE_MAX_VALUE (dom
))
18707 if (TREE_CODE (max
) == MINUS_EXPR
)
18708 max
= TREE_OPERAND (max
, 0);
18709 if (TREE_CODE (max
) == TEMPLATE_PARM_INDEX
)
18710 unify (tparms
, targs
, TREE_TYPE (max
), size_type_node
,
18711 UNIFY_ALLOW_NONE
, /*explain*/false);
18714 /* Keep walking. */
18718 /* Try to deduce any not-yet-deduced template type arguments from the type of
18719 an array bound. This is handled separately from unify because 14.8.2.5 says
18720 "The type of a type parameter is only deduced from an array bound if it is
18721 not otherwise deduced." */
18724 try_array_deduction (tree tparms
, tree targs
, tree parm
)
18726 tree_pair_s data
= { tparms
, targs
};
18727 hash_set
<tree
> visited
;
18728 for_each_template_parm (parm
, zero_r
, &data
, &visited
,
18729 /*nondeduced*/false, array_deduction_r
);
18732 /* Most parms like fn_type_unification.
18734 If SUBR is 1, we're being called recursively (to unify the
18735 arguments of a function or method parameter of a function
18738 CHECKS is a pointer to a vector of access checks encountered while
18739 substituting default template arguments. */
18742 type_unification_real (tree tparms
,
18746 unsigned int xnargs
,
18748 unification_kind_t strict
,
18750 vec
<deferred_access_check
, va_gc
> **checks
,
18755 int ntparms
= TREE_VEC_LENGTH (tparms
);
18756 int saw_undeduced
= 0;
18759 unsigned int nargs
;
18762 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
18763 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
18764 gcc_assert (ntparms
> 0);
18766 tree targs
= INNERMOST_TEMPLATE_ARGS (full_targs
);
18768 /* Reset the number of non-defaulted template arguments contained
18770 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
18778 while (parms
&& parms
!= void_list_node
18781 parm
= TREE_VALUE (parms
);
18783 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18784 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
18785 /* For a function parameter pack that occurs at the end of the
18786 parameter-declaration-list, the type A of each remaining
18787 argument of the call is compared with the type P of the
18788 declarator-id of the function parameter pack. */
18791 parms
= TREE_CHAIN (parms
);
18793 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18794 /* For a function parameter pack that does not occur at the
18795 end of the parameter-declaration-list, the type of the
18796 parameter pack is a non-deduced context. */
18802 if (unify_one_argument (tparms
, full_targs
, parm
, arg
, subr
, strict
,
18808 && parms
!= void_list_node
18809 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
18811 /* Unify the remaining arguments with the pack expansion type. */
18813 tree parmvec
= make_tree_vec (1);
18815 /* Allocate a TREE_VEC and copy in all of the arguments */
18816 argvec
= make_tree_vec (nargs
- ia
);
18817 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
18818 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
18820 /* Copy the parameter into parmvec. */
18821 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
18822 if (unify_pack_expansion (tparms
, full_targs
, parmvec
, argvec
, strict
,
18823 /*subr=*/subr
, explain_p
))
18826 /* Advance to the end of the list of parameters. */
18827 parms
= TREE_CHAIN (parms
);
18830 /* Fail if we've reached the end of the parm list, and more args
18831 are present, and the parm list isn't variadic. */
18832 if (ia
< nargs
&& parms
== void_list_node
)
18833 return unify_too_many_arguments (explain_p
, nargs
, ia
);
18834 /* Fail if parms are left and they don't have default values and
18835 they aren't all deduced as empty packs (c++/57397). This is
18836 consistent with sufficient_parms_p. */
18837 if (parms
&& parms
!= void_list_node
18838 && TREE_PURPOSE (parms
) == NULL_TREE
)
18840 unsigned int count
= nargs
;
18845 type_pack_p
= TREE_CODE (TREE_VALUE (p
)) == TYPE_PACK_EXPANSION
;
18848 p
= TREE_CHAIN (p
);
18850 while (p
&& p
!= void_list_node
);
18851 if (count
!= nargs
)
18852 return unify_too_few_arguments (explain_p
, ia
, count
,
18858 tsubst_flags_t complain
= (explain_p
18859 ? tf_warning_or_error
18861 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
18863 for (i
= 0; i
< ntparms
; i
++)
18865 tree targ
= TREE_VEC_ELT (targs
, i
);
18866 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18868 /* Clear the "incomplete" flags on all argument packs now so that
18869 substituting them into later default arguments works. */
18870 if (targ
&& ARGUMENT_PACK_P (targ
))
18872 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
18873 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
18876 if (targ
|| tparm
== error_mark_node
)
18878 tparm
= TREE_VALUE (tparm
);
18880 if (TREE_CODE (tparm
) == TYPE_DECL
18881 && !tried_array_deduction
)
18883 try_array_deduction (tparms
, targs
, xparms
);
18884 tried_array_deduction
= true;
18885 if (TREE_VEC_ELT (targs
, i
))
18889 /* If this is an undeduced nontype parameter that depends on
18890 a type parameter, try another pass; its type may have been
18891 deduced from a later argument than the one from which
18892 this parameter can be deduced. */
18893 if (TREE_CODE (tparm
) == PARM_DECL
18894 && uses_template_parms (TREE_TYPE (tparm
))
18895 && saw_undeduced
< 2)
18901 /* Core issue #226 (C++0x) [temp.deduct]:
18903 If a template argument has not been deduced, its
18904 default template argument, if any, is used.
18906 When we are in C++98 mode, TREE_PURPOSE will either
18907 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18908 to explicitly check cxx_dialect here. */
18909 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
18910 /* OK, there is a default argument. Wait until after the
18911 conversion check to do substitution. */
18914 /* If the type parameter is a parameter pack, then it will
18915 be deduced to an empty parameter pack. */
18916 if (template_parameter_pack_p (tparm
))
18920 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
18922 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
18923 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
18924 TREE_CONSTANT (arg
) = 1;
18927 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
18929 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
18931 TREE_VEC_ELT (targs
, i
) = arg
;
18935 return unify_parameter_deduction_failure (explain_p
, tparm
);
18938 /* DR 1391: All parameters have args, now check non-dependent parms for
18940 if (saw_undeduced
< 2)
18941 for (ia
= 0, parms
= xparms
, args
= xargs
, nargs
= xnargs
;
18942 parms
&& parms
!= void_list_node
&& ia
< nargs
; )
18944 parm
= TREE_VALUE (parms
);
18946 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
18947 && (!TREE_CHAIN (parms
)
18948 || TREE_CHAIN (parms
) == void_list_node
))
18949 /* For a function parameter pack that occurs at the end of the
18950 parameter-declaration-list, the type A of each remaining
18951 argument of the call is compared with the type P of the
18952 declarator-id of the function parameter pack. */
18955 parms
= TREE_CHAIN (parms
);
18957 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
18958 /* For a function parameter pack that does not occur at the
18959 end of the parameter-declaration-list, the type of the
18960 parameter pack is a non-deduced context. */
18966 if (uses_template_parms (parm
))
18968 if (check_non_deducible_conversion (parm
, arg
, strict
, flags
,
18973 /* Now substitute into the default template arguments. */
18974 for (i
= 0; i
< ntparms
; i
++)
18976 tree targ
= TREE_VEC_ELT (targs
, i
);
18977 tree tparm
= TREE_VEC_ELT (tparms
, i
);
18979 if (targ
|| tparm
== error_mark_node
)
18981 tree parm
= TREE_VALUE (tparm
);
18983 if (TREE_CODE (parm
) == PARM_DECL
18984 && uses_template_parms (TREE_TYPE (parm
))
18985 && saw_undeduced
< 2)
18988 tree arg
= TREE_PURPOSE (tparm
);
18989 reopen_deferring_access_checks (*checks
);
18990 location_t save_loc
= input_location
;
18992 input_location
= DECL_SOURCE_LOCATION (parm
);
18993 arg
= tsubst_template_arg (arg
, full_targs
, complain
, NULL_TREE
);
18994 if (!uses_template_parms (arg
))
18995 arg
= convert_template_argument (parm
, arg
, full_targs
, complain
,
18997 else if (saw_undeduced
< 2)
19000 arg
= error_mark_node
;
19001 input_location
= save_loc
;
19002 *checks
= get_deferred_access_checks ();
19003 pop_deferring_access_checks ();
19004 if (arg
== error_mark_node
)
19008 TREE_VEC_ELT (targs
, i
) = arg
;
19009 /* The position of the first default template argument,
19010 is also the number of non-defaulted arguments in TARGS.
19012 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
19017 if (saw_undeduced
++ == 1)
19021 if (CHECKING_P
&& !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
19022 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
19024 return unify_success (explain_p
);
19027 /* Subroutine of type_unification_real. Args are like the variables
19028 at the call site. ARG is an overloaded function (or template-id);
19029 we try deducing template args from each of the overloads, and if
19030 only one succeeds, we go with that. Modifies TARGS and returns
19031 true on success. */
19034 resolve_overloaded_unification (tree tparms
,
19038 unification_kind_t strict
,
19042 tree tempargs
= copy_node (targs
);
19044 tree goodfn
= NULL_TREE
;
19047 if (TREE_CODE (arg
) == ADDR_EXPR
)
19049 arg
= TREE_OPERAND (arg
, 0);
19055 if (TREE_CODE (arg
) == COMPONENT_REF
)
19056 /* Handle `&x' where `x' is some static or non-static member
19058 arg
= TREE_OPERAND (arg
, 1);
19060 if (TREE_CODE (arg
) == OFFSET_REF
)
19061 arg
= TREE_OPERAND (arg
, 1);
19063 /* Strip baselink information. */
19064 if (BASELINK_P (arg
))
19065 arg
= BASELINK_FUNCTIONS (arg
);
19067 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
19069 /* If we got some explicit template args, we need to plug them into
19070 the affected templates before we try to unify, in case the
19071 explicit args will completely resolve the templates in question. */
19074 tree expl_subargs
= TREE_OPERAND (arg
, 1);
19075 arg
= TREE_OPERAND (arg
, 0);
19077 for (; arg
; arg
= OVL_NEXT (arg
))
19079 tree fn
= OVL_CURRENT (arg
);
19080 tree subargs
, elem
;
19082 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19085 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19086 expl_subargs
, NULL_TREE
, tf_none
,
19087 /*require_all_args=*/true,
19088 /*use_default_args=*/true);
19089 if (subargs
!= error_mark_node
19090 && !any_dependent_template_arguments_p (subargs
))
19092 elem
= TREE_TYPE (instantiate_template (fn
, subargs
, tf_none
));
19093 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19094 elem
, strict
, sub_strict
, addr_p
, explain_p
)
19095 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
19104 /* If no templates (or more than one) are fully resolved by the
19105 explicit arguments, this template-id is a non-deduced context; it
19106 could still be OK if we deduce all template arguments for the
19107 enclosing call through other arguments. */
19111 else if (TREE_CODE (arg
) != OVERLOAD
19112 && TREE_CODE (arg
) != FUNCTION_DECL
)
19113 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19114 -- but the deduction does not succeed because the expression is
19115 not just the function on its own. */
19118 for (; arg
; arg
= OVL_NEXT (arg
))
19119 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
19120 TREE_TYPE (OVL_CURRENT (arg
)),
19121 strict
, sub_strict
, addr_p
, explain_p
)
19122 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
19124 goodfn
= OVL_CURRENT (arg
);
19128 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19129 to function or pointer to member function argument if the set of
19130 overloaded functions does not contain function templates and at most
19131 one of a set of overloaded functions provides a unique match.
19133 So if we found multiple possibilities, we return success but don't
19134 deduce anything. */
19138 int i
= TREE_VEC_LENGTH (targs
);
19140 if (TREE_VEC_ELT (tempargs
, i
))
19142 tree old
= TREE_VEC_ELT (targs
, i
);
19143 tree new_
= TREE_VEC_ELT (tempargs
, i
);
19144 if (new_
&& old
&& ARGUMENT_PACK_P (old
)
19145 && ARGUMENT_PACK_EXPLICIT_ARGS (old
))
19146 /* Don't forget explicit template arguments in a pack. */
19147 ARGUMENT_PACK_EXPLICIT_ARGS (new_
)
19148 = ARGUMENT_PACK_EXPLICIT_ARGS (old
);
19149 TREE_VEC_ELT (targs
, i
) = new_
;
19158 /* Core DR 115: In contexts where deduction is done and fails, or in
19159 contexts where deduction is not done, if a template argument list is
19160 specified and it, along with any default template arguments, identifies
19161 a single function template specialization, then the template-id is an
19162 lvalue for the function template specialization. */
19165 resolve_nondeduced_context (tree orig_expr
, tsubst_flags_t complain
)
19167 tree expr
, offset
, baselink
;
19170 if (!type_unknown_p (orig_expr
))
19175 offset
= NULL_TREE
;
19176 baselink
= NULL_TREE
;
19178 if (TREE_CODE (expr
) == ADDR_EXPR
)
19180 expr
= TREE_OPERAND (expr
, 0);
19183 if (TREE_CODE (expr
) == OFFSET_REF
)
19186 expr
= TREE_OPERAND (expr
, 1);
19188 if (BASELINK_P (expr
))
19191 expr
= BASELINK_FUNCTIONS (expr
);
19194 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
19197 tree goodfn
= NULL_TREE
;
19199 /* If we got some explicit template args, we need to plug them into
19200 the affected templates before we try to unify, in case the
19201 explicit args will completely resolve the templates in question. */
19203 tree expl_subargs
= TREE_OPERAND (expr
, 1);
19204 tree arg
= TREE_OPERAND (expr
, 0);
19205 tree badfn
= NULL_TREE
;
19206 tree badargs
= NULL_TREE
;
19208 for (; arg
; arg
= OVL_NEXT (arg
))
19210 tree fn
= OVL_CURRENT (arg
);
19211 tree subargs
, elem
;
19213 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
19216 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
19217 expl_subargs
, NULL_TREE
, tf_none
,
19218 /*require_all_args=*/true,
19219 /*use_default_args=*/true);
19220 if (subargs
!= error_mark_node
19221 && !any_dependent_template_arguments_p (subargs
))
19223 elem
= instantiate_template (fn
, subargs
, tf_none
);
19224 if (elem
== error_mark_node
)
19229 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
19238 mark_used (goodfn
);
19241 expr
= build_baselink (BASELINK_BINFO (baselink
),
19242 BASELINK_ACCESS_BINFO (baselink
),
19243 expr
, BASELINK_OPTYPE (baselink
));
19247 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
19248 expr
= build_offset_ref (base
, expr
, addr
, complain
);
19251 expr
= cp_build_addr_expr (expr
, complain
);
19254 else if (good
== 0 && badargs
&& (complain
& tf_error
))
19255 /* There were no good options and at least one bad one, so let the
19256 user know what the problem is. */
19257 instantiate_template (badfn
, badargs
, complain
);
19262 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19263 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19264 different overloads deduce different arguments for a given parm.
19265 ADDR_P is true if the expression for which deduction is being
19266 performed was of the form "& fn" rather than simply "fn".
19268 Returns 1 on success. */
19271 try_one_overload (tree tparms
,
19276 unification_kind_t strict
,
19285 if (arg
== error_mark_node
)
19288 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19289 to function or pointer to member function argument if the set of
19290 overloaded functions does not contain function templates and at most
19291 one of a set of overloaded functions provides a unique match.
19293 So if this is a template, just return success. */
19295 if (uses_template_parms (arg
))
19298 if (TREE_CODE (arg
) == METHOD_TYPE
)
19299 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
19301 arg
= build_pointer_type (arg
);
19303 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
19305 /* We don't copy orig_targs for this because if we have already deduced
19306 some template args from previous args, unify would complain when we
19307 try to deduce a template parameter for the same argument, even though
19308 there isn't really a conflict. */
19309 nargs
= TREE_VEC_LENGTH (targs
);
19310 tempargs
= make_tree_vec (nargs
);
19312 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
19315 /* First make sure we didn't deduce anything that conflicts with
19316 explicitly specified args. */
19317 for (i
= nargs
; i
--; )
19319 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19320 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
19324 else if (uses_template_parms (elt
))
19325 /* Since we're unifying against ourselves, we will fill in
19326 template args used in the function parm list with our own
19327 template parms. Discard them. */
19328 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
19329 else if (oldelt
&& ARGUMENT_PACK_P (oldelt
))
19331 /* Check that the argument at each index of the deduced argument pack
19332 is equivalent to the corresponding explicitly specified argument.
19333 We may have deduced more arguments than were explicitly specified,
19335 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt
));
19336 gcc_assert (ARGUMENT_PACK_ARGS (oldelt
)
19337 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt
));
19339 tree explicit_pack
= ARGUMENT_PACK_ARGS (oldelt
);
19340 tree deduced_pack
= ARGUMENT_PACK_ARGS (elt
);
19342 if (TREE_VEC_LENGTH (deduced_pack
)
19343 < TREE_VEC_LENGTH (explicit_pack
))
19346 for (int j
= 0; j
< TREE_VEC_LENGTH (explicit_pack
); j
++)
19347 if (!template_args_equal (TREE_VEC_ELT (explicit_pack
, j
),
19348 TREE_VEC_ELT (deduced_pack
, j
)))
19351 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
19355 for (i
= nargs
; i
--; )
19357 tree elt
= TREE_VEC_ELT (tempargs
, i
);
19360 TREE_VEC_ELT (targs
, i
) = elt
;
19366 /* PARM is a template class (perhaps with unbound template
19367 parameters). ARG is a fully instantiated type. If ARG can be
19368 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19369 TARGS are as for unify. */
19372 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
19375 tree copy_of_targs
;
19377 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
19378 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
19379 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
19382 /* We need to make a new template argument vector for the call to
19383 unify. If we used TARGS, we'd clutter it up with the result of
19384 the attempted unification, even if this class didn't work out.
19385 We also don't want to commit ourselves to all the unifications
19386 we've already done, since unification is supposed to be done on
19387 an argument-by-argument basis. In other words, consider the
19388 following pathological case:
19390 template <int I, int J, int K>
19393 template <int I, int J>
19394 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19396 template <int I, int J, int K>
19397 void f(S<I, J, K>, S<I, I, I>);
19406 Now, by the time we consider the unification involving `s2', we
19407 already know that we must have `f<0, 0, 0>'. But, even though
19408 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19409 because there are two ways to unify base classes of S<0, 1, 2>
19410 with S<I, I, I>. If we kept the already deduced knowledge, we
19411 would reject the possibility I=1. */
19412 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19414 /* If unification failed, we're done. */
19415 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
19416 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
19422 /* Given a template type PARM and a class type ARG, find the unique
19423 base type in ARG that is an instance of PARM. We do not examine
19424 ARG itself; only its base-classes. If there is not exactly one
19425 appropriate base class, return NULL_TREE. PARM may be the type of
19426 a partial specialization, as well as a plain template type. Used
19429 static enum template_base_result
19430 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
19431 bool explain_p
, tree
*result
)
19433 tree rval
= NULL_TREE
;
19436 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
19438 binfo
= TYPE_BINFO (complete_type (arg
));
19441 /* The type could not be completed. */
19442 *result
= NULL_TREE
;
19443 return tbr_incomplete_type
;
19446 /* Walk in inheritance graph order. The search order is not
19447 important, and this avoids multiple walks of virtual bases. */
19448 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
19450 tree r
= try_class_unification (tparms
, targs
, parm
,
19451 BINFO_TYPE (binfo
), explain_p
);
19455 /* If there is more than one satisfactory baseclass, then:
19459 If they yield more than one possible deduced A, the type
19463 if (rval
&& !same_type_p (r
, rval
))
19465 *result
= NULL_TREE
;
19466 return tbr_ambiguous_baseclass
;
19474 return tbr_success
;
19477 /* Returns the level of DECL, which declares a template parameter. */
19480 template_decl_level (tree decl
)
19482 switch (TREE_CODE (decl
))
19485 case TEMPLATE_DECL
:
19486 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
19489 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
19492 gcc_unreachable ();
19497 /* Decide whether ARG can be unified with PARM, considering only the
19498 cv-qualifiers of each type, given STRICT as documented for unify.
19499 Returns nonzero iff the unification is OK on that basis. */
19502 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
19504 int arg_quals
= cp_type_quals (arg
);
19505 int parm_quals
= cp_type_quals (parm
);
19507 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19508 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19510 /* Although a CVR qualifier is ignored when being applied to a
19511 substituted template parameter ([8.3.2]/1 for example), that
19512 does not allow us to unify "const T" with "int&" because both
19513 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19514 It is ok when we're allowing additional CV qualifiers
19515 at the outer level [14.8.2.1]/3,1st bullet. */
19516 if ((TREE_CODE (arg
) == REFERENCE_TYPE
19517 || TREE_CODE (arg
) == FUNCTION_TYPE
19518 || TREE_CODE (arg
) == METHOD_TYPE
)
19519 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
19522 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
19523 && (parm_quals
& TYPE_QUAL_RESTRICT
))
19527 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
19528 && (arg_quals
& parm_quals
) != parm_quals
)
19531 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
19532 && (parm_quals
& arg_quals
) != arg_quals
)
19538 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19540 template_parm_level_and_index (tree parm
, int* level
, int* index
)
19542 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19543 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
19544 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19546 *index
= TEMPLATE_TYPE_IDX (parm
);
19547 *level
= TEMPLATE_TYPE_LEVEL (parm
);
19551 *index
= TEMPLATE_PARM_IDX (parm
);
19552 *level
= TEMPLATE_PARM_LEVEL (parm
);
19556 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19558 if (unify (TP, TA, P, A, S, EP)) \
19562 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19563 expansion at the end of PACKED_PARMS. Returns 0 if the type
19564 deduction succeeds, 1 otherwise. STRICT is the same as in
19565 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19566 function call argument list. We'll need to adjust the arguments to make them
19567 types. SUBR tells us if this is from a recursive call to
19568 type_unification_real, or for comparing two template argument
19572 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
19573 tree packed_args
, unification_kind_t strict
,
19574 bool subr
, bool explain_p
)
19577 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
19578 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
19579 tree pack
, packs
= NULL_TREE
;
19580 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
19582 packed_args
= expand_template_argument_pack (packed_args
);
19584 int len
= TREE_VEC_LENGTH (packed_args
);
19586 /* Determine the parameter packs we will be deducing from the
19587 pattern, and record their current deductions. */
19588 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
19589 pack
; pack
= TREE_CHAIN (pack
))
19591 tree parm_pack
= TREE_VALUE (pack
);
19594 /* Determine the index and level of this parameter pack. */
19595 template_parm_level_and_index (parm_pack
, &level
, &idx
);
19597 /* Keep track of the parameter packs and their corresponding
19599 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
19600 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
19603 /* Loop through all of the arguments that have not yet been
19604 unified and unify each with the pattern. */
19605 for (i
= start
; i
< len
; i
++)
19608 bool any_explicit
= false;
19609 tree arg
= TREE_VEC_ELT (packed_args
, i
);
19611 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19612 or the element of its argument pack at the current index if
19613 this argument was explicitly specified. */
19614 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19618 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19621 if (TREE_VALUE (pack
)
19622 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
19623 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
19625 any_explicit
= true;
19626 arg
= TREE_VEC_ELT (pargs
, i
- start
);
19628 TMPL_ARG (targs
, level
, idx
) = arg
;
19631 /* If we had explicit template arguments, substitute them into the
19632 pattern before deduction. */
19635 /* Some arguments might still be unspecified or dependent. */
19637 ++processing_template_decl
;
19638 dependent
= any_dependent_template_arguments_p (targs
);
19640 --processing_template_decl
;
19641 parm
= tsubst (pattern
, targs
,
19642 explain_p
? tf_warning_or_error
: tf_none
,
19645 --processing_template_decl
;
19646 if (parm
== error_mark_node
)
19652 /* Unify the pattern with the current argument. */
19653 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
19657 /* For each parameter pack, collect the deduced value. */
19658 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19661 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19663 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
19664 TMPL_ARG (targs
, level
, idx
);
19668 /* Verify that the results of unification with the parameter packs
19669 produce results consistent with what we've seen before, and make
19670 the deduced argument packs available. */
19671 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
19673 tree old_pack
= TREE_VALUE (pack
);
19674 tree new_args
= TREE_TYPE (pack
);
19675 int i
, len
= TREE_VEC_LENGTH (new_args
);
19677 bool nondeduced_p
= false;
19679 /* By default keep the original deduced argument pack.
19680 If necessary, more specific code is going to update the
19681 resulting deduced argument later down in this function. */
19682 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
19683 TMPL_ARG (targs
, level
, idx
) = old_pack
;
19685 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19686 actually deduce anything. */
19687 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
19688 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
19689 nondeduced_p
= true;
19693 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
19695 /* If we had fewer function args than explicit template args,
19696 just use the explicits. */
19697 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19698 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
19699 if (len
< explicit_len
)
19700 new_args
= explicit_args
;
19706 /* Build the deduced *_ARGUMENT_PACK. */
19707 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
19709 result
= make_node (NONTYPE_ARGUMENT_PACK
);
19710 TREE_TYPE (result
) =
19711 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
19712 TREE_CONSTANT (result
) = 1;
19715 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
19717 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
19719 /* Note the deduced argument packs for this parameter
19721 TMPL_ARG (targs
, level
, idx
) = result
;
19723 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
19724 && (ARGUMENT_PACK_ARGS (old_pack
)
19725 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
19727 /* We only had the explicitly-provided arguments before, but
19728 now we have a complete set of arguments. */
19729 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
19731 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
19732 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
19733 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
19737 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
19738 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
19740 if (!comp_template_args (old_args
, new_args
,
19741 &bad_old_arg
, &bad_new_arg
))
19742 /* Inconsistent unification of this parameter pack. */
19743 return unify_parameter_pack_inconsistent (explain_p
,
19749 return unify_success (explain_p
);
19752 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19753 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19754 parameters and return value are as for unify. */
19757 unify_array_domain (tree tparms
, tree targs
,
19758 tree parm_dom
, tree arg_dom
,
19766 /* Our representation of array types uses "N - 1" as the
19767 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19768 not an integer constant. We cannot unify arbitrarily
19769 complex expressions, so we eliminate the MINUS_EXPRs
19771 parm_max
= TYPE_MAX_VALUE (parm_dom
);
19772 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
19775 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
19776 parm_max
= TREE_OPERAND (parm_max
, 0);
19778 arg_max
= TYPE_MAX_VALUE (arg_dom
);
19779 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
19782 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19783 trying to unify the type of a variable with the type
19784 of a template parameter. For example:
19786 template <unsigned int N>
19787 void f (char (&) [N]);
19794 Here, the type of the ARG will be "int [g(i)]", and
19795 may be a SAVE_EXPR, etc. */
19796 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
19797 return unify_vla_arg (explain_p
, arg_dom
);
19798 arg_max
= TREE_OPERAND (arg_max
, 0);
19801 /* If only one of the bounds used a MINUS_EXPR, compensate
19802 by adding one to the other bound. */
19803 if (parm_cst
&& !arg_cst
)
19804 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19808 else if (arg_cst
&& !parm_cst
)
19809 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
19814 return unify (tparms
, targs
, parm_max
, arg_max
,
19815 UNIFY_ALLOW_INTEGER
, explain_p
);
19818 /* Deduce the value of template parameters. TPARMS is the (innermost)
19819 set of template parameters to a template. TARGS is the bindings
19820 for those template parameters, as determined thus far; TARGS may
19821 include template arguments for outer levels of template parameters
19822 as well. PARM is a parameter to a template function, or a
19823 subcomponent of that parameter; ARG is the corresponding argument.
19824 This function attempts to match PARM with ARG in a manner
19825 consistent with the existing assignments in TARGS. If more values
19826 are deduced, then TARGS is updated.
19828 Returns 0 if the type deduction succeeds, 1 otherwise. The
19829 parameter STRICT is a bitwise or of the following flags:
19832 Require an exact match between PARM and ARG.
19833 UNIFY_ALLOW_MORE_CV_QUAL:
19834 Allow the deduced ARG to be more cv-qualified (by qualification
19835 conversion) than ARG.
19836 UNIFY_ALLOW_LESS_CV_QUAL:
19837 Allow the deduced ARG to be less cv-qualified than ARG.
19838 UNIFY_ALLOW_DERIVED:
19839 Allow the deduced ARG to be a template base class of ARG,
19840 or a pointer to a template base class of the type pointed to by
19842 UNIFY_ALLOW_INTEGER:
19843 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19844 case for more information.
19845 UNIFY_ALLOW_OUTER_LEVEL:
19846 This is the outermost level of a deduction. Used to determine validity
19847 of qualification conversions. A valid qualification conversion must
19848 have const qualified pointers leading up to the inner type which
19849 requires additional CV quals, except at the outer level, where const
19850 is not required [conv.qual]. It would be normal to set this flag in
19851 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19852 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19853 This is the outermost level of a deduction, and PARM can be more CV
19854 qualified at this point.
19855 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19856 This is the outermost level of a deduction, and PARM can be less CV
19857 qualified at this point. */
19860 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
19866 int strict_in
= strict
;
19867 tsubst_flags_t complain
= (explain_p
19868 ? tf_warning_or_error
19871 /* I don't think this will do the right thing with respect to types.
19872 But the only case I've seen it in so far has been array bounds, where
19873 signedness is the only information lost, and I think that will be
19875 while (CONVERT_EXPR_P (parm
))
19876 parm
= TREE_OPERAND (parm
, 0);
19878 if (arg
== error_mark_node
)
19879 return unify_invalid (explain_p
);
19880 if (arg
== unknown_type_node
19881 || arg
== init_list_type_node
)
19882 /* We can't deduce anything from this, but we might get all the
19883 template args from other function args. */
19884 return unify_success (explain_p
);
19886 /* If PARM uses template parameters, then we can't bail out here,
19887 even if ARG == PARM, since we won't record unifications for the
19888 template parameters. We might need them if we're trying to
19889 figure out which of two things is more specialized. */
19890 if (arg
== parm
&& !uses_template_parms (parm
))
19891 return unify_success (explain_p
);
19893 /* Handle init lists early, so the rest of the function can assume
19894 we're dealing with a type. */
19895 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
19899 tree orig_parm
= parm
;
19901 /* Replace T with std::initializer_list<T> for deduction. */
19902 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
19903 && flag_deduce_init_list
)
19904 parm
= listify (parm
);
19906 if (!is_std_init_list (parm
)
19907 && TREE_CODE (parm
) != ARRAY_TYPE
)
19908 /* We can only deduce from an initializer list argument if the
19909 parameter is std::initializer_list or an array; otherwise this
19910 is a non-deduced context. */
19911 return unify_success (explain_p
);
19913 if (TREE_CODE (parm
) == ARRAY_TYPE
)
19914 elttype
= TREE_TYPE (parm
);
19917 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
19918 /* Deduction is defined in terms of a single type, so just punt
19919 on the (bizarre) std::initializer_list<T...>. */
19920 if (PACK_EXPANSION_P (elttype
))
19921 return unify_success (explain_p
);
19924 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
19926 int elt_strict
= strict
;
19928 if (elt
== error_mark_node
)
19929 return unify_invalid (explain_p
);
19931 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
19933 tree type
= TREE_TYPE (elt
);
19934 if (type
== error_mark_node
)
19935 return unify_invalid (explain_p
);
19936 /* It should only be possible to get here for a call. */
19937 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
19938 elt_strict
|= maybe_adjust_types_for_deduction
19939 (DEDUCE_CALL
, &elttype
, &type
, elt
);
19943 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
19947 if (TREE_CODE (parm
) == ARRAY_TYPE
19948 && deducible_array_bound (TYPE_DOMAIN (parm
)))
19950 /* Also deduce from the length of the initializer list. */
19951 tree max
= size_int (CONSTRUCTOR_NELTS (arg
));
19952 tree idx
= compute_array_index_type (NULL_TREE
, max
, tf_none
);
19953 if (idx
== error_mark_node
)
19954 return unify_invalid (explain_p
);
19955 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
19959 /* If the std::initializer_list<T> deduction worked, replace the
19960 deduced A with std::initializer_list<A>. */
19961 if (orig_parm
!= parm
)
19963 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
19964 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
19965 targ
= listify (targ
);
19966 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
19968 return unify_success (explain_p
);
19971 /* Immediately reject some pairs that won't unify because of
19972 cv-qualification mismatches. */
19973 if (TREE_CODE (arg
) == TREE_CODE (parm
)
19975 /* It is the elements of the array which hold the cv quals of an array
19976 type, and the elements might be template type parms. We'll check
19977 when we recurse. */
19978 && TREE_CODE (arg
) != ARRAY_TYPE
19979 /* We check the cv-qualifiers when unifying with template type
19980 parameters below. We want to allow ARG `const T' to unify with
19981 PARM `T' for example, when computing which of two templates
19982 is more specialized, for example. */
19983 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
19984 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
19985 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
19987 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
19988 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
19989 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
19990 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
19991 strict
&= ~UNIFY_ALLOW_DERIVED
;
19992 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
19993 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
19995 switch (TREE_CODE (parm
))
19997 case TYPENAME_TYPE
:
19999 case UNBOUND_CLASS_TEMPLATE
:
20000 /* In a type which contains a nested-name-specifier, template
20001 argument values cannot be deduced for template parameters used
20002 within the nested-name-specifier. */
20003 return unify_success (explain_p
);
20005 case TEMPLATE_TYPE_PARM
:
20006 case TEMPLATE_TEMPLATE_PARM
:
20007 case BOUND_TEMPLATE_TEMPLATE_PARM
:
20008 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20009 if (error_operand_p (tparm
))
20010 return unify_invalid (explain_p
);
20012 if (TEMPLATE_TYPE_LEVEL (parm
)
20013 != template_decl_level (tparm
))
20014 /* The PARM is not one we're trying to unify. Just check
20015 to see if it matches ARG. */
20017 if (TREE_CODE (arg
) == TREE_CODE (parm
)
20018 && (is_auto (parm
) ? is_auto (arg
)
20019 : same_type_p (parm
, arg
)))
20020 return unify_success (explain_p
);
20022 return unify_type_mismatch (explain_p
, parm
, arg
);
20024 idx
= TEMPLATE_TYPE_IDX (parm
);
20025 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20026 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
20027 if (error_operand_p (tparm
))
20028 return unify_invalid (explain_p
);
20030 /* Check for mixed types and values. */
20031 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
20032 && TREE_CODE (tparm
) != TYPE_DECL
)
20033 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20034 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
20035 gcc_unreachable ();
20037 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20039 /* ARG must be constructed from a template class or a template
20040 template parameter. */
20041 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
20042 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
20043 return unify_template_deduction_failure (explain_p
, parm
, arg
);
20045 tree parmvec
= TYPE_TI_ARGS (parm
);
20046 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
20047 tree full_argvec
= add_to_template_args (targs
, argvec
);
20049 = DECL_INNERMOST_TEMPLATE_PARMS
20050 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
20052 int parm_variadic_p
= 0;
20054 /* The resolution to DR150 makes clear that default
20055 arguments for an N-argument may not be used to bind T
20056 to a template template parameter with fewer than N
20057 parameters. It is not safe to permit the binding of
20058 default arguments as an extension, as that may change
20059 the meaning of a conforming program. Consider:
20061 struct Dense { static const unsigned int dim = 1; };
20063 template <template <typename> class View,
20065 void operator+(float, View<Block> const&);
20067 template <typename Block,
20068 unsigned int Dim = Block::dim>
20069 struct Lvalue_proxy { operator float() const; };
20073 Lvalue_proxy<Dense> p;
20078 Here, if Lvalue_proxy is permitted to bind to View, then
20079 the global operator+ will be used; if they are not, the
20080 Lvalue_proxy will be converted to float. */
20081 if (coerce_template_parms (parm_parms
,
20083 TYPE_TI_TEMPLATE (parm
),
20085 /*require_all_args=*/true,
20086 /*use_default_args=*/false)
20087 == error_mark_node
)
20090 /* Deduce arguments T, i from TT<T> or TT<i>.
20091 We check each element of PARMVEC and ARGVEC individually
20092 rather than the whole TREE_VEC since they can have
20093 different number of elements. */
20095 parmvec
= expand_template_argument_pack (parmvec
);
20096 argvec
= expand_template_argument_pack (argvec
);
20098 len
= TREE_VEC_LENGTH (parmvec
);
20100 /* Check if the parameters end in a pack, making them
20103 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
20104 parm_variadic_p
= 1;
20106 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20107 /* If the template argument list of P contains a pack
20108 expansion that is not the last template argument, the
20109 entire template argument list is a non-deduced
20111 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
20112 return unify_success (explain_p
);
20114 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
20115 return unify_too_few_arguments (explain_p
,
20116 TREE_VEC_LENGTH (argvec
), len
);
20118 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20120 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20121 TREE_VEC_ELT (parmvec
, i
),
20122 TREE_VEC_ELT (argvec
, i
),
20123 UNIFY_ALLOW_NONE
, explain_p
);
20126 if (parm_variadic_p
20127 && unify_pack_expansion (tparms
, targs
,
20130 /*subr=*/true, explain_p
))
20133 arg
= TYPE_TI_TEMPLATE (arg
);
20135 /* Fall through to deduce template name. */
20138 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
20139 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
20141 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20143 /* Simple cases: Value already set, does match or doesn't. */
20144 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
20145 return unify_success (explain_p
);
20147 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20151 /* If PARM is `const T' and ARG is only `int', we don't have
20152 a match unless we are allowing additional qualification.
20153 If ARG is `const int' and PARM is just `T' that's OK;
20154 that binds `const int' to `T'. */
20155 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
20157 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20159 /* Consider the case where ARG is `const volatile int' and
20160 PARM is `const T'. Then, T should be `volatile int'. */
20161 arg
= cp_build_qualified_type_real
20162 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
20163 if (arg
== error_mark_node
)
20164 return unify_invalid (explain_p
);
20166 /* Simple cases: Value already set, does match or doesn't. */
20167 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
20168 return unify_success (explain_p
);
20170 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
20172 /* Make sure that ARG is not a variable-sized array. (Note
20173 that were talking about variable-sized arrays (like
20174 `int[n]'), rather than arrays of unknown size (like
20175 `int[]').) We'll get very confused by such a type since
20176 the bound of the array is not constant, and therefore
20177 not mangleable. Besides, such types are not allowed in
20178 ISO C++, so we can do as we please here. We do allow
20179 them for 'auto' deduction, since that isn't ABI-exposed. */
20180 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
20181 return unify_vla_arg (explain_p
, arg
);
20183 /* Strip typedefs as in convert_template_argument. */
20184 arg
= canonicalize_type_argument (arg
, tf_none
);
20187 /* If ARG is a parameter pack or an expansion, we cannot unify
20188 against it unless PARM is also a parameter pack. */
20189 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20190 && !template_parameter_pack_p (parm
))
20191 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20193 /* If the argument deduction results is a METHOD_TYPE,
20194 then there is a problem.
20195 METHOD_TYPE doesn't map to any real C++ type the result of
20196 the deduction can not be of that type. */
20197 if (TREE_CODE (arg
) == METHOD_TYPE
)
20198 return unify_method_type_error (explain_p
, arg
);
20200 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20201 return unify_success (explain_p
);
20203 case TEMPLATE_PARM_INDEX
:
20204 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
20205 if (error_operand_p (tparm
))
20206 return unify_invalid (explain_p
);
20208 if (TEMPLATE_PARM_LEVEL (parm
)
20209 != template_decl_level (tparm
))
20211 /* The PARM is not one we're trying to unify. Just check
20212 to see if it matches ARG. */
20213 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
20214 && cp_tree_equal (parm
, arg
));
20216 unify_expression_unequal (explain_p
, parm
, arg
);
20220 idx
= TEMPLATE_PARM_IDX (parm
);
20221 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
20225 int x
= !cp_tree_equal (targ
, arg
);
20227 unify_inconsistency (explain_p
, parm
, targ
, arg
);
20231 if (cxx_dialect
>= cxx1z
20232 /* We deduce from array bounds in try_array_deduction. */
20233 && !(strict
& UNIFY_ALLOW_INTEGER
)
20234 && uses_template_parms (TREE_TYPE (parm
))
20235 && !type_uses_auto (TREE_TYPE (parm
)))
20237 tree atype
= TREE_TYPE (arg
);
20238 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20239 TREE_TYPE (parm
), atype
,
20240 UNIFY_ALLOW_NONE
, explain_p
);
20243 /* [temp.deduct.type] If, in the declaration of a function template
20244 with a non-type template-parameter, the non-type
20245 template-parameter is used in an expression in the function
20246 parameter-list and, if the corresponding template-argument is
20247 deduced, the template-argument type shall match the type of the
20248 template-parameter exactly, except that a template-argument
20249 deduced from an array bound may be of any integral type.
20250 The non-type parameter might use already deduced type parameters. */
20251 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
20252 if (tree a
= type_uses_auto (tparm
))
20254 tparm
= do_auto_deduction (tparm
, arg
, a
, complain
, adc_unify
);
20255 if (tparm
== error_mark_node
)
20259 if (!TREE_TYPE (arg
))
20260 /* Template-parameter dependent expression. Just accept it for now.
20261 It will later be processed in convert_template_argument. */
20263 else if (same_type_p (TREE_TYPE (arg
), tparm
))
20265 else if ((strict
& UNIFY_ALLOW_INTEGER
)
20266 && CP_INTEGRAL_TYPE_P (tparm
))
20267 /* Convert the ARG to the type of PARM; the deduced non-type
20268 template argument must exactly match the types of the
20269 corresponding parameter. */
20270 arg
= fold (build_nop (tparm
, arg
));
20271 else if (uses_template_parms (tparm
))
20272 /* We haven't deduced the type of this parameter yet. Try again
20274 return unify_success (explain_p
);
20276 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
20278 /* If ARG is a parameter pack or an expansion, we cannot unify
20279 against it unless PARM is also a parameter pack. */
20280 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
20281 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
20282 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
20285 bool removed_attr
= false;
20286 arg
= strip_typedefs_expr (arg
, &removed_attr
);
20288 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
20289 return unify_success (explain_p
);
20293 /* A pointer-to-member constant can be unified only with
20294 another constant. */
20295 if (TREE_CODE (arg
) != PTRMEM_CST
)
20296 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
20298 /* Just unify the class member. It would be useless (and possibly
20299 wrong, depending on the strict flags) to unify also
20300 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20301 arg refer to the same variable, even if through different
20302 classes. For instance:
20304 struct A { int x; };
20307 Unification of &A::x and &B::x must succeed. */
20308 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
20309 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
20314 if (!TYPE_PTR_P (arg
))
20315 return unify_type_mismatch (explain_p
, parm
, arg
);
20317 /* [temp.deduct.call]
20319 A can be another pointer or pointer to member type that can
20320 be converted to the deduced A via a qualification
20321 conversion (_conv.qual_).
20323 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20324 This will allow for additional cv-qualification of the
20325 pointed-to types if appropriate. */
20327 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
20328 /* The derived-to-base conversion only persists through one
20329 level of pointers. */
20330 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
20332 return unify (tparms
, targs
, TREE_TYPE (parm
),
20333 TREE_TYPE (arg
), strict
, explain_p
);
20336 case REFERENCE_TYPE
:
20337 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
20338 return unify_type_mismatch (explain_p
, parm
, arg
);
20339 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20340 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20343 if (TREE_CODE (arg
) != ARRAY_TYPE
)
20344 return unify_type_mismatch (explain_p
, parm
, arg
);
20345 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
20346 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
20347 return unify_type_mismatch (explain_p
, parm
, arg
);
20348 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20349 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
20350 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
20351 return unify_array_domain (tparms
, targs
, TYPE_DOMAIN (parm
),
20352 TYPE_DOMAIN (arg
), explain_p
);
20353 return unify_success (explain_p
);
20360 case ENUMERAL_TYPE
:
20363 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20364 return unify_type_mismatch (explain_p
, parm
, arg
);
20366 /* We have already checked cv-qualification at the top of the
20368 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
20369 return unify_type_mismatch (explain_p
, parm
, arg
);
20371 /* As far as unification is concerned, this wins. Later checks
20372 will invalidate it if necessary. */
20373 return unify_success (explain_p
);
20375 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20376 /* Type INTEGER_CST can come from ordinary constant template args. */
20378 while (CONVERT_EXPR_P (arg
))
20379 arg
= TREE_OPERAND (arg
, 0);
20381 if (TREE_CODE (arg
) != INTEGER_CST
)
20382 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20383 return (tree_int_cst_equal (parm
, arg
)
20384 ? unify_success (explain_p
)
20385 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
20389 int i
, len
, argslen
;
20390 int parm_variadic_p
= 0;
20392 if (TREE_CODE (arg
) != TREE_VEC
)
20393 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20395 len
= TREE_VEC_LENGTH (parm
);
20396 argslen
= TREE_VEC_LENGTH (arg
);
20398 /* Check for pack expansions in the parameters. */
20399 for (i
= 0; i
< len
; ++i
)
20401 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
20404 /* We can unify against something with a trailing
20406 parm_variadic_p
= 1;
20408 /* [temp.deduct.type]/9: If the template argument list of
20409 P contains a pack expansion that is not the last
20410 template argument, the entire template argument list
20411 is a non-deduced context. */
20412 return unify_success (explain_p
);
20416 /* If we don't have enough arguments to satisfy the parameters
20417 (not counting the pack expression at the end), or we have
20418 too many arguments for a parameter list that doesn't end in
20419 a pack expression, we can't unify. */
20420 if (parm_variadic_p
20421 ? argslen
< len
- parm_variadic_p
20423 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
20425 /* Unify all of the parameters that precede the (optional)
20426 pack expression. */
20427 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
20429 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
20430 TREE_VEC_ELT (parm
, i
),
20431 TREE_VEC_ELT (arg
, i
),
20432 UNIFY_ALLOW_NONE
, explain_p
);
20434 if (parm_variadic_p
)
20435 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
20437 /*subr=*/true, explain_p
);
20438 return unify_success (explain_p
);
20443 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20444 return unify_type_mismatch (explain_p
, parm
, arg
);
20446 if (TYPE_PTRMEMFUNC_P (parm
))
20448 if (!TYPE_PTRMEMFUNC_P (arg
))
20449 return unify_type_mismatch (explain_p
, parm
, arg
);
20451 return unify (tparms
, targs
,
20452 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
20453 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
20454 strict
, explain_p
);
20456 else if (TYPE_PTRMEMFUNC_P (arg
))
20457 return unify_type_mismatch (explain_p
, parm
, arg
);
20459 if (CLASSTYPE_TEMPLATE_INFO (parm
))
20461 tree t
= NULL_TREE
;
20463 if (strict_in
& UNIFY_ALLOW_DERIVED
)
20465 /* First, we try to unify the PARM and ARG directly. */
20466 t
= try_class_unification (tparms
, targs
,
20467 parm
, arg
, explain_p
);
20471 /* Fallback to the special case allowed in
20472 [temp.deduct.call]:
20474 If P is a class, and P has the form
20475 template-id, then A can be a derived class of
20476 the deduced A. Likewise, if P is a pointer to
20477 a class of the form template-id, A can be a
20478 pointer to a derived class pointed to by the
20480 enum template_base_result r
;
20481 r
= get_template_base (tparms
, targs
, parm
, arg
,
20486 /* Don't give the derived diagnostic if we're
20487 already dealing with the same template. */
20489 = (CLASSTYPE_TEMPLATE_INFO (arg
)
20490 && (CLASSTYPE_TI_TEMPLATE (parm
)
20491 == CLASSTYPE_TI_TEMPLATE (arg
)));
20492 return unify_no_common_base (explain_p
&& !same_template
,
20497 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
20498 && (CLASSTYPE_TI_TEMPLATE (parm
)
20499 == CLASSTYPE_TI_TEMPLATE (arg
)))
20500 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20501 Then, we should unify `int' and `U'. */
20504 /* There's no chance of unification succeeding. */
20505 return unify_type_mismatch (explain_p
, parm
, arg
);
20507 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
20508 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
20510 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
20511 return unify_type_mismatch (explain_p
, parm
, arg
);
20512 return unify_success (explain_p
);
20515 case FUNCTION_TYPE
:
20517 unsigned int nargs
;
20522 if (TREE_CODE (arg
) != TREE_CODE (parm
))
20523 return unify_type_mismatch (explain_p
, parm
, arg
);
20525 /* CV qualifications for methods can never be deduced, they must
20526 match exactly. We need to check them explicitly here,
20527 because type_unification_real treats them as any other
20528 cv-qualified parameter. */
20529 if (TREE_CODE (parm
) == METHOD_TYPE
20530 && (!check_cv_quals_for_unify
20532 class_of_this_parm (arg
),
20533 class_of_this_parm (parm
))))
20534 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20535 if (TREE_CODE (arg
) == FUNCTION_TYPE
20536 && type_memfn_quals (parm
) != type_memfn_quals (arg
))
20537 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20538 if (type_memfn_rqual (parm
) != type_memfn_rqual (arg
))
20539 return unify_type_mismatch (explain_p
, parm
, arg
);
20541 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
20542 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
20544 nargs
= list_length (TYPE_ARG_TYPES (arg
));
20545 args
= XALLOCAVEC (tree
, nargs
);
20546 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
20547 a
!= NULL_TREE
&& a
!= void_list_node
;
20548 a
= TREE_CHAIN (a
), ++i
)
20549 args
[i
] = TREE_VALUE (a
);
20552 if (type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
20553 args
, nargs
, 1, DEDUCE_EXACT
,
20554 LOOKUP_NORMAL
, NULL
, explain_p
))
20557 if (flag_noexcept_type
)
20559 tree pspec
= TYPE_RAISES_EXCEPTIONS (parm
);
20560 tree aspec
= canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg
));
20561 if (pspec
== NULL_TREE
) pspec
= noexcept_false_spec
;
20562 if (aspec
== NULL_TREE
) aspec
= noexcept_false_spec
;
20563 if (TREE_PURPOSE (pspec
) && TREE_PURPOSE (aspec
)
20564 && uses_template_parms (TREE_PURPOSE (pspec
)))
20565 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_PURPOSE (pspec
),
20566 TREE_PURPOSE (aspec
),
20567 UNIFY_ALLOW_NONE
, explain_p
);
20568 else if (nothrow_spec_p (pspec
) && !nothrow_spec_p (aspec
))
20569 return unify_type_mismatch (explain_p
, parm
, arg
);
20576 /* Unify a pointer to member with a pointer to member function, which
20577 deduces the type of the member as a function type. */
20578 if (TYPE_PTRMEMFUNC_P (arg
))
20580 /* Check top-level cv qualifiers */
20581 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
20582 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
20584 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20585 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
20586 UNIFY_ALLOW_NONE
, explain_p
);
20588 /* Determine the type of the function we are unifying against. */
20589 tree fntype
= static_fn_type (arg
);
20591 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
20594 if (TREE_CODE (arg
) != OFFSET_TYPE
)
20595 return unify_type_mismatch (explain_p
, parm
, arg
);
20596 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
20597 TYPE_OFFSET_BASETYPE (arg
),
20598 UNIFY_ALLOW_NONE
, explain_p
);
20599 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
20600 strict
, explain_p
);
20603 if (DECL_TEMPLATE_PARM_P (parm
))
20604 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
20605 if (arg
!= scalar_constant_value (parm
))
20606 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20607 return unify_success (explain_p
);
20610 case TEMPLATE_DECL
:
20611 /* Matched cases are handled by the ARG == PARM test above. */
20612 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
20615 /* We might get a variable as a non-type template argument in parm if the
20616 corresponding parameter is type-dependent. Make any necessary
20617 adjustments based on whether arg is a reference. */
20618 if (CONSTANT_CLASS_P (arg
))
20619 parm
= fold_non_dependent_expr (parm
);
20620 else if (REFERENCE_REF_P (arg
))
20622 tree sub
= TREE_OPERAND (arg
, 0);
20624 if (TREE_CODE (sub
) == ADDR_EXPR
)
20625 arg
= TREE_OPERAND (sub
, 0);
20627 /* Now use the normal expression code to check whether they match. */
20630 case TYPE_ARGUMENT_PACK
:
20631 case NONTYPE_ARGUMENT_PACK
:
20632 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
20633 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
20636 case DECLTYPE_TYPE
:
20637 case UNDERLYING_TYPE
:
20638 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20639 or UNDERLYING_TYPE nodes. */
20640 return unify_success (explain_p
);
20643 /* Unification fails if we hit an error node. */
20644 return unify_invalid (explain_p
);
20647 if (REFERENCE_REF_P (parm
))
20649 if (REFERENCE_REF_P (arg
))
20650 arg
= TREE_OPERAND (arg
, 0);
20651 return unify (tparms
, targs
, TREE_OPERAND (parm
, 0), arg
,
20652 strict
, explain_p
);
20657 /* An unresolved overload is a nondeduced context. */
20658 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
20659 return unify_success (explain_p
);
20660 gcc_assert (EXPR_P (parm
) || TREE_CODE (parm
) == TRAIT_EXPR
);
20662 /* We must be looking at an expression. This can happen with
20666 void foo(S<I>, S<I + 2>);
20668 This is a "nondeduced context":
20672 The nondeduced contexts are:
20674 --A type that is a template-id in which one or more of
20675 the template-arguments is an expression that references
20676 a template-parameter.
20678 In these cases, we assume deduction succeeded, but don't
20679 actually infer any unifications. */
20681 if (!uses_template_parms (parm
)
20682 && !template_args_equal (parm
, arg
))
20683 return unify_expression_unequal (explain_p
, parm
, arg
);
20685 return unify_success (explain_p
);
20688 #undef RECUR_AND_CHECK_FAILURE
20690 /* Note that DECL can be defined in this translation unit, if
20694 mark_definable (tree decl
)
20697 DECL_NOT_REALLY_EXTERN (decl
) = 1;
20698 FOR_EACH_CLONE (clone
, decl
)
20699 DECL_NOT_REALLY_EXTERN (clone
) = 1;
20702 /* Called if RESULT is explicitly instantiated, or is a member of an
20703 explicitly instantiated class. */
20706 mark_decl_instantiated (tree result
, int extern_p
)
20708 SET_DECL_EXPLICIT_INSTANTIATION (result
);
20710 /* If this entity has already been written out, it's too late to
20711 make any modifications. */
20712 if (TREE_ASM_WRITTEN (result
))
20715 /* For anonymous namespace we don't need to do anything. */
20716 if (decl_anon_ns_mem_p (result
))
20718 gcc_assert (!TREE_PUBLIC (result
));
20722 if (TREE_CODE (result
) != FUNCTION_DECL
)
20723 /* The TREE_PUBLIC flag for function declarations will have been
20724 set correctly by tsubst. */
20725 TREE_PUBLIC (result
) = 1;
20727 /* This might have been set by an earlier implicit instantiation. */
20728 DECL_COMDAT (result
) = 0;
20731 DECL_NOT_REALLY_EXTERN (result
) = 0;
20734 mark_definable (result
);
20735 mark_needed (result
);
20736 /* Always make artificials weak. */
20737 if (DECL_ARTIFICIAL (result
) && flag_weak
)
20738 comdat_linkage (result
);
20739 /* For WIN32 we also want to put explicit instantiations in
20740 linkonce sections. */
20741 else if (TREE_PUBLIC (result
))
20742 maybe_make_one_only (result
);
20745 /* If EXTERN_P, then this function will not be emitted -- unless
20746 followed by an explicit instantiation, at which point its linkage
20747 will be adjusted. If !EXTERN_P, then this function will be
20748 emitted here. In neither circumstance do we want
20749 import_export_decl to adjust the linkage. */
20750 DECL_INTERFACE_KNOWN (result
) = 1;
20753 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20754 important template arguments. If any are missing, we check whether
20755 they're important by using error_mark_node for substituting into any
20756 args that were used for partial ordering (the ones between ARGS and END)
20757 and seeing if it bubbles up. */
20760 check_undeduced_parms (tree targs
, tree args
, tree end
)
20762 bool found
= false;
20764 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
20765 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
20768 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
20772 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
20773 if (substed
== error_mark_node
)
20779 /* Given two function templates PAT1 and PAT2, return:
20781 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20782 -1 if PAT2 is more specialized than PAT1.
20783 0 if neither is more specialized.
20785 LEN indicates the number of parameters we should consider
20786 (defaulted parameters should not be considered).
20788 The 1998 std underspecified function template partial ordering, and
20789 DR214 addresses the issue. We take pairs of arguments, one from
20790 each of the templates, and deduce them against each other. One of
20791 the templates will be more specialized if all the *other*
20792 template's arguments deduce against its arguments and at least one
20793 of its arguments *does* *not* deduce against the other template's
20794 corresponding argument. Deduction is done as for class templates.
20795 The arguments used in deduction have reference and top level cv
20796 qualifiers removed. Iff both arguments were originally reference
20797 types *and* deduction succeeds in both directions, an lvalue reference
20798 wins against an rvalue reference and otherwise the template
20799 with the more cv-qualified argument wins for that pairing (if
20800 neither is more cv-qualified, they both are equal). Unlike regular
20801 deduction, after all the arguments have been deduced in this way,
20802 we do *not* verify the deduced template argument values can be
20803 substituted into non-deduced contexts.
20805 The logic can be a bit confusing here, because we look at deduce1 and
20806 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20807 can find template arguments for pat1 to make arg1 look like arg2, that
20808 means that arg2 is at least as specialized as arg1. */
20811 more_specialized_fn (tree pat1
, tree pat2
, int len
)
20813 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
20814 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
20815 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
20816 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
20817 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
20818 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
20819 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
20820 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
20821 tree origs1
, origs2
;
20822 bool lose1
= false;
20823 bool lose2
= false;
20825 /* Remove the this parameter from non-static member functions. If
20826 one is a non-static member function and the other is not a static
20827 member function, remove the first parameter from that function
20828 also. This situation occurs for operator functions where we
20829 locate both a member function (with this pointer) and non-member
20830 operator (with explicit first operand). */
20831 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
20833 len
--; /* LEN is the number of significant arguments for DECL1 */
20834 args1
= TREE_CHAIN (args1
);
20835 if (!DECL_STATIC_FUNCTION_P (decl2
))
20836 args2
= TREE_CHAIN (args2
);
20838 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
20840 args2
= TREE_CHAIN (args2
);
20841 if (!DECL_STATIC_FUNCTION_P (decl1
))
20844 args1
= TREE_CHAIN (args1
);
20848 /* If only one is a conversion operator, they are unordered. */
20849 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
20852 /* Consider the return type for a conversion function */
20853 if (DECL_CONV_FN_P (decl1
))
20855 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
20856 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
20860 processing_template_decl
++;
20866 /* Stop when an ellipsis is seen. */
20867 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
20869 tree arg1
= TREE_VALUE (args1
);
20870 tree arg2
= TREE_VALUE (args2
);
20871 int deduce1
, deduce2
;
20877 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
20878 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20880 /* When both arguments are pack expansions, we need only
20881 unify the patterns themselves. */
20882 arg1
= PACK_EXPANSION_PATTERN (arg1
);
20883 arg2
= PACK_EXPANSION_PATTERN (arg2
);
20885 /* This is the last comparison we need to do. */
20889 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
20891 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
20892 arg1
= TREE_TYPE (arg1
);
20893 quals1
= cp_type_quals (arg1
);
20896 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
20898 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
20899 arg2
= TREE_TYPE (arg2
);
20900 quals2
= cp_type_quals (arg2
);
20903 arg1
= TYPE_MAIN_VARIANT (arg1
);
20904 arg2
= TYPE_MAIN_VARIANT (arg2
);
20906 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
20908 int i
, len2
= remaining_arguments (args2
);
20909 tree parmvec
= make_tree_vec (1);
20910 tree argvec
= make_tree_vec (len2
);
20913 /* Setup the parameter vector, which contains only ARG1. */
20914 TREE_VEC_ELT (parmvec
, 0) = arg1
;
20916 /* Setup the argument vector, which contains the remaining
20918 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
20919 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20921 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
20922 argvec
, DEDUCE_EXACT
,
20923 /*subr=*/true, /*explain_p=*/false)
20926 /* We cannot deduce in the other direction, because ARG1 is
20927 a pack expansion but ARG2 is not. */
20930 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
20932 int i
, len1
= remaining_arguments (args1
);
20933 tree parmvec
= make_tree_vec (1);
20934 tree argvec
= make_tree_vec (len1
);
20937 /* Setup the parameter vector, which contains only ARG1. */
20938 TREE_VEC_ELT (parmvec
, 0) = arg2
;
20940 /* Setup the argument vector, which contains the remaining
20942 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
20943 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
20945 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
20946 argvec
, DEDUCE_EXACT
,
20947 /*subr=*/true, /*explain_p=*/false)
20950 /* We cannot deduce in the other direction, because ARG2 is
20951 a pack expansion but ARG1 is not.*/
20957 /* The normal case, where neither argument is a pack
20959 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
20960 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20962 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
20963 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
20967 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20968 arg2, then arg2 is not as specialized as arg1. */
20974 /* "If, for a given type, deduction succeeds in both directions
20975 (i.e., the types are identical after the transformations above)
20976 and both P and A were reference types (before being replaced with
20977 the type referred to above):
20978 - if the type from the argument template was an lvalue reference and
20979 the type from the parameter template was not, the argument type is
20980 considered to be more specialized than the other; otherwise,
20981 - if the type from the argument template is more cv-qualified
20982 than the type from the parameter template (as described above),
20983 the argument type is considered to be more specialized than the other;
20985 - neither type is more specialized than the other." */
20987 if (deduce1
&& deduce2
)
20989 if (ref1
&& ref2
&& ref1
!= ref2
)
20996 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
20998 if ((quals1
& quals2
) == quals2
)
21000 if ((quals1
& quals2
) == quals1
)
21005 if (lose1
&& lose2
)
21006 /* We've failed to deduce something in either direction.
21007 These must be unordered. */
21010 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
21011 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
21012 /* We have already processed all of the arguments in our
21013 handing of the pack expansion type. */
21016 args1
= TREE_CHAIN (args1
);
21017 args2
= TREE_CHAIN (args2
);
21020 /* "In most cases, all template parameters must have values in order for
21021 deduction to succeed, but for partial ordering purposes a template
21022 parameter may remain without a value provided it is not used in the
21023 types being used for partial ordering."
21025 Thus, if we are missing any of the targs1 we need to substitute into
21026 origs1, then pat2 is not as specialized as pat1. This can happen when
21027 there is a nondeduced context. */
21028 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
21030 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
21033 processing_template_decl
--;
21035 /* If both deductions succeed, the partial ordering selects the more
21036 constrained template. */
21037 if (!lose1
&& !lose2
)
21039 tree c1
= get_constraints (DECL_TEMPLATE_RESULT (pat1
));
21040 tree c2
= get_constraints (DECL_TEMPLATE_RESULT (pat2
));
21041 lose1
= !subsumes_constraints (c1
, c2
);
21042 lose2
= !subsumes_constraints (c2
, c1
);
21045 /* All things being equal, if the next argument is a pack expansion
21046 for one function but not for the other, prefer the
21047 non-variadic function. FIXME this is bogus; see c++/41958. */
21049 && args1
&& TREE_VALUE (args1
)
21050 && args2
&& TREE_VALUE (args2
))
21052 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
21053 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
21056 if (lose1
== lose2
)
21064 /* Determine which of two partial specializations of TMPL is more
21067 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21068 to the first partial specialization. The TREE_PURPOSE is the
21069 innermost set of template parameters for the partial
21070 specialization. PAT2 is similar, but for the second template.
21072 Return 1 if the first partial specialization is more specialized;
21073 -1 if the second is more specialized; 0 if neither is more
21076 See [temp.class.order] for information about determining which of
21077 two templates is more specialized. */
21080 more_specialized_partial_spec (tree tmpl
, tree pat1
, tree pat2
)
21084 bool any_deductions
= false;
21086 tree tmpl1
= TREE_VALUE (pat1
);
21087 tree tmpl2
= TREE_VALUE (pat2
);
21088 tree specargs1
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1
)));
21089 tree specargs2
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2
)));
21091 /* Just like what happens for functions, if we are ordering between
21092 different template specializations, we may encounter dependent
21093 types in the arguments, and we need our dependency check functions
21094 to behave correctly. */
21095 ++processing_template_decl
;
21096 targs
= get_partial_spec_bindings (tmpl
, tmpl1
, specargs2
);
21100 any_deductions
= true;
21103 targs
= get_partial_spec_bindings (tmpl
, tmpl2
, specargs1
);
21107 any_deductions
= true;
21109 --processing_template_decl
;
21111 /* If both deductions succeed, the partial ordering selects the more
21112 constrained template. */
21113 if (!winner
&& any_deductions
)
21114 return more_constrained (tmpl1
, tmpl2
);
21116 /* In the case of a tie where at least one of the templates
21117 has a parameter pack at the end, the template with the most
21118 non-packed parameters wins. */
21121 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
21122 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
21124 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
21125 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
21126 int len1
= TREE_VEC_LENGTH (args1
);
21127 int len2
= TREE_VEC_LENGTH (args2
);
21129 /* We don't count the pack expansion at the end. */
21130 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
21132 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
21137 else if (len1
< len2
)
21144 /* Return the template arguments that will produce the function signature
21145 DECL from the function template FN, with the explicit template
21146 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21147 also match. Return NULL_TREE if no satisfactory arguments could be
21151 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
21153 int ntparms
= DECL_NTPARMS (fn
);
21154 tree targs
= make_tree_vec (ntparms
);
21155 tree decl_type
= TREE_TYPE (decl
);
21156 tree decl_arg_types
;
21158 unsigned int nargs
, ix
;
21161 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
21163 /* Never do unification on the 'this' parameter. */
21164 decl_arg_types
= skip_artificial_parms_for (decl
,
21165 TYPE_ARG_TYPES (decl_type
));
21167 nargs
= list_length (decl_arg_types
);
21168 args
= XALLOCAVEC (tree
, nargs
);
21169 for (arg
= decl_arg_types
, ix
= 0;
21170 arg
!= NULL_TREE
&& arg
!= void_list_node
;
21171 arg
= TREE_CHAIN (arg
), ++ix
)
21172 args
[ix
] = TREE_VALUE (arg
);
21174 if (fn_type_unification (fn
, explicit_args
, targs
,
21176 (check_rettype
|| DECL_CONV_FN_P (fn
)
21177 ? TREE_TYPE (decl_type
) : NULL_TREE
),
21178 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
21180 == error_mark_node
)
21186 /* Return the innermost template arguments that, when applied to a partial
21187 specialization SPEC_TMPL of TMPL, yield the ARGS.
21189 For example, suppose we have:
21191 template <class T, class U> struct S {};
21192 template <class T> struct S<T*, int> {};
21194 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21195 partial specialization and the ARGS will be {double*, int}. The resulting
21196 vector will be {double}, indicating that `T' is bound to `double'. */
21199 get_partial_spec_bindings (tree tmpl
, tree spec_tmpl
, tree args
)
21201 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
21203 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl
)));
21204 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
21206 tree innermost_deduced_args
;
21208 innermost_deduced_args
= make_tree_vec (ntparms
);
21209 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21211 deduced_args
= copy_node (args
);
21212 SET_TMPL_ARGS_LEVEL (deduced_args
,
21213 TMPL_ARGS_DEPTH (deduced_args
),
21214 innermost_deduced_args
);
21217 deduced_args
= innermost_deduced_args
;
21219 bool tried_array_deduction
= (cxx_dialect
< cxx1z
);
21221 if (unify (tparms
, deduced_args
,
21222 INNERMOST_TEMPLATE_ARGS (spec_args
),
21223 INNERMOST_TEMPLATE_ARGS (args
),
21224 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
21227 for (i
= 0; i
< ntparms
; ++i
)
21228 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
21230 if (!tried_array_deduction
)
21232 try_array_deduction (tparms
, innermost_deduced_args
,
21233 INNERMOST_TEMPLATE_ARGS (spec_args
));
21234 tried_array_deduction
= true;
21235 if (TREE_VEC_ELT (innermost_deduced_args
, i
))
21241 tree tinst
= build_tree_list (spec_tmpl
, deduced_args
);
21242 if (!push_tinst_level (tinst
))
21244 excessive_deduction_depth
= true;
21248 /* Verify that nondeduced template arguments agree with the type
21249 obtained from argument deduction.
21253 struct A { typedef int X; };
21254 template <class T, class U> struct C {};
21255 template <class T> struct C<T, typename T::X> {};
21257 Then with the instantiation `C<A, int>', we can deduce that
21258 `T' is `A' but unify () does not check whether `typename T::X'
21260 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
21261 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
21263 tf_none
, false, false);
21265 pop_tinst_level ();
21267 if (spec_args
== error_mark_node
21268 /* We only need to check the innermost arguments; the other
21269 arguments will always agree. */
21270 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
21271 INNERMOST_TEMPLATE_ARGS (args
)))
21274 /* Now that we have bindings for all of the template arguments,
21275 ensure that the arguments deduced for the template template
21276 parameters have compatible template parameter lists. See the use
21277 of template_template_parm_bindings_ok_p in fn_type_unification
21278 for more information. */
21279 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
21282 return deduced_args
;
21285 // Compare two function templates T1 and T2 by deducing bindings
21286 // from one against the other. If both deductions succeed, compare
21287 // constraints to see which is more constrained.
21289 more_specialized_inst (tree t1
, tree t2
)
21294 if (get_bindings (t1
, DECL_TEMPLATE_RESULT (t2
), NULL_TREE
, true))
21300 if (get_bindings (t2
, DECL_TEMPLATE_RESULT (t1
), NULL_TREE
, true))
21306 // If both deductions succeed, then one may be more constrained.
21307 if (count
== 2 && fate
== 0)
21308 fate
= more_constrained (t1
, t2
);
21313 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21314 Return the TREE_LIST node with the most specialized template, if
21315 any. If there is no most specialized template, the error_mark_node
21318 Note that this function does not look at, or modify, the
21319 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21320 returned is one of the elements of INSTANTIATIONS, callers may
21321 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21322 and retrieve it from the value returned. */
21325 most_specialized_instantiation (tree templates
)
21329 ++processing_template_decl
;
21332 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
21334 int fate
= more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
));
21339 /* Equally specialized, move to next function. If there
21340 is no next function, nothing's most specialized. */
21341 fn
= TREE_CHAIN (fn
);
21349 /* Now verify that champ is better than everything earlier in the
21350 instantiation list. */
21351 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
)) {
21352 if (more_specialized_inst (TREE_VALUE (champ
), TREE_VALUE (fn
)) != 1)
21359 processing_template_decl
--;
21362 return error_mark_node
;
21367 /* If DECL is a specialization of some template, return the most
21368 general such template. Otherwise, returns NULL_TREE.
21370 For example, given:
21372 template <class T> struct S { template <class U> void f(U); };
21374 if TMPL is `template <class U> void S<int>::f(U)' this will return
21375 the full template. This function will not trace past partial
21376 specializations, however. For example, given in addition:
21378 template <class T> struct S<T*> { template <class U> void f(U); };
21380 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21381 `template <class T> template <class U> S<T*>::f(U)'. */
21384 most_general_template (tree decl
)
21386 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21388 if (tree tinfo
= get_template_info (decl
))
21389 decl
= TI_TEMPLATE (tinfo
);
21390 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21391 template friend, or a FIELD_DECL for a capture pack. */
21392 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
21396 /* Look for more and more general templates. */
21397 while (DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
))
21399 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21400 (See cp-tree.h for details.) */
21401 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
21404 if (CLASS_TYPE_P (TREE_TYPE (decl
))
21405 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl
)))
21406 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
21409 /* Stop if we run into an explicitly specialized class template. */
21410 if (!DECL_NAMESPACE_SCOPE_P (decl
)
21411 && DECL_CONTEXT (decl
)
21412 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
21415 decl
= DECL_TI_TEMPLATE (decl
);
21421 /* Return the most specialized of the template partial specializations
21422 which can produce TARGET, a specialization of some class or variable
21423 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21424 a TEMPLATE_DECL node corresponding to the partial specialization, while
21425 the TREE_PURPOSE is the set of template arguments that must be
21426 substituted into the template pattern in order to generate TARGET.
21428 If the choice of partial specialization is ambiguous, a diagnostic
21429 is issued, and the error_mark_node is returned. If there are no
21430 partial specializations matching TARGET, then NULL_TREE is
21431 returned, indicating that the primary template should be used. */
21434 most_specialized_partial_spec (tree target
, tsubst_flags_t complain
)
21436 tree list
= NULL_TREE
;
21441 tree outer_args
= NULL_TREE
;
21444 if (TYPE_P (target
))
21446 tree tinfo
= CLASSTYPE_TEMPLATE_INFO (target
);
21447 tmpl
= TI_TEMPLATE (tinfo
);
21448 args
= TI_ARGS (tinfo
);
21450 else if (TREE_CODE (target
) == TEMPLATE_ID_EXPR
)
21452 tmpl
= TREE_OPERAND (target
, 0);
21453 args
= TREE_OPERAND (target
, 1);
21455 else if (VAR_P (target
))
21457 tree tinfo
= DECL_TEMPLATE_INFO (target
);
21458 tmpl
= TI_TEMPLATE (tinfo
);
21459 args
= TI_ARGS (tinfo
);
21462 gcc_unreachable ();
21464 tree main_tmpl
= most_general_template (tmpl
);
21466 /* For determining which partial specialization to use, only the
21467 innermost args are interesting. */
21468 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
21470 outer_args
= strip_innermost_template_args (args
, 1);
21471 args
= INNERMOST_TEMPLATE_ARGS (args
);
21474 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl
); t
; t
= TREE_CHAIN (t
))
21477 tree spec_tmpl
= TREE_VALUE (t
);
21481 /* Substitute in the template args from the enclosing class. */
21482 ++processing_template_decl
;
21483 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
21484 --processing_template_decl
;
21487 if (spec_tmpl
== error_mark_node
)
21488 return error_mark_node
;
21490 spec_args
= get_partial_spec_bindings (tmpl
, spec_tmpl
, args
);
21494 spec_args
= add_to_template_args (outer_args
, spec_args
);
21496 /* Keep the candidate only if the constraints are satisfied,
21497 or if we're not compiling with concepts. */
21499 || constraints_satisfied_p (spec_tmpl
, spec_args
))
21501 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
21502 TREE_TYPE (list
) = TREE_TYPE (t
);
21510 ambiguous_p
= false;
21513 t
= TREE_CHAIN (t
);
21514 for (; t
; t
= TREE_CHAIN (t
))
21516 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21523 t
= TREE_CHAIN (t
);
21526 ambiguous_p
= true;
21535 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
21537 fate
= more_specialized_partial_spec (tmpl
, champ
, t
);
21540 ambiguous_p
= true;
21548 char *spaces
= NULL
;
21549 if (!(complain
& tf_error
))
21550 return error_mark_node
;
21551 if (TYPE_P (target
))
21552 error ("ambiguous template instantiation for %q#T", target
);
21554 error ("ambiguous template instantiation for %q#D", target
);
21555 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
21556 for (t
= list
; t
; t
= TREE_CHAIN (t
))
21558 tree subst
= build_tree_list (TREE_VALUE (t
), TREE_PURPOSE (t
));
21559 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t
)),
21560 "%s %#S", spaces
? spaces
: str
, subst
);
21561 spaces
= spaces
? spaces
: get_spaces (str
);
21564 return error_mark_node
;
21570 /* Explicitly instantiate DECL. */
21573 do_decl_instantiation (tree decl
, tree storage
)
21575 tree result
= NULL_TREE
;
21578 if (!decl
|| decl
== error_mark_node
)
21579 /* An error occurred, for which grokdeclarator has already issued
21580 an appropriate message. */
21582 else if (! DECL_LANG_SPECIFIC (decl
))
21584 error ("explicit instantiation of non-template %q#D", decl
);
21588 bool var_templ
= (DECL_TEMPLATE_INFO (decl
)
21589 && variable_template_p (DECL_TI_TEMPLATE (decl
)));
21591 if (VAR_P (decl
) && !var_templ
)
21593 /* There is an asymmetry here in the way VAR_DECLs and
21594 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21595 the latter, the DECL we get back will be marked as a
21596 template instantiation, and the appropriate
21597 DECL_TEMPLATE_INFO will be set up. This does not happen for
21598 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21599 should handle VAR_DECLs as it currently handles
21601 if (!DECL_CLASS_SCOPE_P (decl
))
21603 error ("%qD is not a static data member of a class template", decl
);
21606 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
21607 if (!result
|| !VAR_P (result
))
21609 error ("no matching template for %qD found", decl
);
21612 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
21614 error ("type %qT for explicit instantiation %qD does not match "
21615 "declared type %qT", TREE_TYPE (result
), decl
,
21620 else if (TREE_CODE (decl
) != FUNCTION_DECL
&& !var_templ
)
21622 error ("explicit instantiation of %q#D", decl
);
21628 /* Check for various error cases. Note that if the explicit
21629 instantiation is valid the RESULT will currently be marked as an
21630 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21631 until we get here. */
21633 if (DECL_TEMPLATE_SPECIALIZATION (result
))
21635 /* DR 259 [temp.spec].
21637 Both an explicit instantiation and a declaration of an explicit
21638 specialization shall not appear in a program unless the explicit
21639 instantiation follows a declaration of the explicit specialization.
21641 For a given set of template parameters, if an explicit
21642 instantiation of a template appears after a declaration of an
21643 explicit specialization for that template, the explicit
21644 instantiation has no effect. */
21647 else if (DECL_EXPLICIT_INSTANTIATION (result
))
21651 No program shall explicitly instantiate any template more
21654 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21655 the first instantiation was `extern' and the second is not,
21656 and EXTERN_P for the opposite case. */
21657 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
21658 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
21659 /* If an "extern" explicit instantiation follows an ordinary
21660 explicit instantiation, the template is instantiated. */
21664 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
21666 error ("no matching template for %qD found", result
);
21669 else if (!DECL_TEMPLATE_INFO (result
))
21671 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
21675 if (storage
== NULL_TREE
)
21677 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21679 if (!in_system_header_at (input_location
) && (cxx_dialect
== cxx98
))
21680 pedwarn (input_location
, OPT_Wpedantic
,
21681 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21686 error ("storage class %qD applied to template instantiation", storage
);
21688 check_explicit_instantiation_namespace (result
);
21689 mark_decl_instantiated (result
, extern_p
);
21691 instantiate_decl (result
, /*defer_ok=*/1,
21692 /*expl_inst_class_mem_p=*/false);
21696 mark_class_instantiated (tree t
, int extern_p
)
21698 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
21699 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
21700 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
21701 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
21704 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
21705 rest_of_type_compilation (t
, 1);
21709 /* Called from do_type_instantiation through binding_table_foreach to
21710 do recursive instantiation for the type bound in ENTRY. */
21712 bt_instantiate_type_proc (binding_entry entry
, void *data
)
21714 tree storage
= *(tree
*) data
;
21716 if (MAYBE_CLASS_TYPE_P (entry
->type
)
21717 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
21718 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
21721 /* Called from do_type_instantiation to instantiate a member
21722 (a member function or a static member variable) of an
21723 explicitly instantiated class template. */
21725 instantiate_class_member (tree decl
, int extern_p
)
21727 mark_decl_instantiated (decl
, extern_p
);
21729 instantiate_decl (decl
, /*defer_ok=*/1,
21730 /*expl_inst_class_mem_p=*/true);
21733 /* Perform an explicit instantiation of template class T. STORAGE, if
21734 non-null, is the RID for extern, inline or static. COMPLAIN is
21735 nonzero if this is called from the parser, zero if called recursively,
21736 since the standard is unclear (as detailed below). */
21739 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
21744 int previous_instantiation_extern_p
= 0;
21746 if (TREE_CODE (t
) == TYPE_DECL
)
21749 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
21752 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
21754 error ("explicit instantiation of non-class template %qD", tmpl
);
21756 error ("explicit instantiation of non-template type %qT", t
);
21762 if (!COMPLETE_TYPE_P (t
))
21764 if (complain
& tf_error
)
21765 error ("explicit instantiation of %q#T before definition of template",
21770 if (storage
!= NULL_TREE
)
21772 if (!in_system_header_at (input_location
))
21774 if (storage
== ridpointers
[(int) RID_EXTERN
])
21776 if (cxx_dialect
== cxx98
)
21777 pedwarn (input_location
, OPT_Wpedantic
,
21778 "ISO C++ 1998 forbids the use of %<extern%> on "
21779 "explicit instantiations");
21782 pedwarn (input_location
, OPT_Wpedantic
,
21783 "ISO C++ forbids the use of %qE"
21784 " on explicit instantiations", storage
);
21787 if (storage
== ridpointers
[(int) RID_INLINE
])
21789 else if (storage
== ridpointers
[(int) RID_EXTERN
])
21791 else if (storage
== ridpointers
[(int) RID_STATIC
])
21795 error ("storage class %qD applied to template instantiation",
21801 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
21803 /* DR 259 [temp.spec].
21805 Both an explicit instantiation and a declaration of an explicit
21806 specialization shall not appear in a program unless the explicit
21807 instantiation follows a declaration of the explicit specialization.
21809 For a given set of template parameters, if an explicit
21810 instantiation of a template appears after a declaration of an
21811 explicit specialization for that template, the explicit
21812 instantiation has no effect. */
21815 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
21819 No program shall explicitly instantiate any template more
21822 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21823 instantiation was `extern'. If EXTERN_P then the second is.
21824 These cases are OK. */
21825 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
21827 if (!previous_instantiation_extern_p
&& !extern_p
21828 && (complain
& tf_error
))
21829 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
21831 /* If we've already instantiated the template, just return now. */
21832 if (!CLASSTYPE_INTERFACE_ONLY (t
))
21836 check_explicit_instantiation_namespace (TYPE_NAME (t
));
21837 mark_class_instantiated (t
, extern_p
);
21845 /* In contrast to implicit instantiation, where only the
21846 declarations, and not the definitions, of members are
21847 instantiated, we have here:
21851 The explicit instantiation of a class template specialization
21852 implies the instantiation of all of its members not
21853 previously explicitly specialized in the translation unit
21854 containing the explicit instantiation.
21856 Of course, we can't instantiate member template classes, since
21857 we don't have any arguments for them. Note that the standard
21858 is unclear on whether the instantiation of the members are
21859 *explicit* instantiations or not. However, the most natural
21860 interpretation is that it should be an explicit instantiation. */
21863 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21864 if (TREE_CODE (tmp
) == FUNCTION_DECL
21865 && DECL_TEMPLATE_INSTANTIATION (tmp
)
21866 && user_provided_p (tmp
))
21867 instantiate_class_member (tmp
, extern_p
);
21869 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
21870 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
21871 instantiate_class_member (tmp
, extern_p
);
21873 if (CLASSTYPE_NESTED_UTDS (t
))
21874 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
21875 bt_instantiate_type_proc
, &storage
);
21879 /* Given a function DECL, which is a specialization of TMPL, modify
21880 DECL to be a re-instantiation of TMPL with the same template
21881 arguments. TMPL should be the template into which tsubst'ing
21882 should occur for DECL, not the most general template.
21884 One reason for doing this is a scenario like this:
21887 void f(const T&, int i);
21889 void g() { f(3, 7); }
21892 void f(const T& t, const int i) { }
21894 Note that when the template is first instantiated, with
21895 instantiate_template, the resulting DECL will have no name for the
21896 first parameter, and the wrong type for the second. So, when we go
21897 to instantiate the DECL, we regenerate it. */
21900 regenerate_decl_from_template (tree decl
, tree tmpl
, tree args
)
21902 /* The arguments used to instantiate DECL, from the most general
21906 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
21908 /* Make sure that we can see identifiers, and compute access
21910 push_access_scope (decl
);
21912 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21920 args_depth
= TMPL_ARGS_DEPTH (args
);
21921 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
21922 if (args_depth
> parms_depth
)
21923 args
= get_innermost_template_args (args
, parms_depth
);
21925 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
21926 args
, tf_error
, NULL_TREE
,
21927 /*defer_ok*/false);
21928 if (specs
&& specs
!= error_mark_node
)
21929 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
21932 /* Merge parameter declarations. */
21933 decl_parm
= skip_artificial_parms_for (decl
,
21934 DECL_ARGUMENTS (decl
));
21936 = skip_artificial_parms_for (code_pattern
,
21937 DECL_ARGUMENTS (code_pattern
));
21938 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
21943 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21944 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
21945 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
21947 parm_type
= type_decays_to (parm_type
);
21948 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21949 TREE_TYPE (decl_parm
) = parm_type
;
21950 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21951 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21953 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21954 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21956 decl_parm
= DECL_CHAIN (decl_parm
);
21957 pattern_parm
= DECL_CHAIN (pattern_parm
);
21959 /* Merge any parameters that match with the function parameter
21961 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
21964 tree expanded_types
;
21965 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21966 the parameters in this function parameter pack. */
21967 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
21968 args
, tf_error
, NULL_TREE
);
21969 len
= TREE_VEC_LENGTH (expanded_types
);
21970 for (i
= 0; i
< len
; i
++)
21975 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
21976 /* Rename the parameter to include the index. */
21977 DECL_NAME (decl_parm
) =
21978 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
21979 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
21980 parm_type
= type_decays_to (parm_type
);
21981 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
21982 TREE_TYPE (decl_parm
) = parm_type
;
21983 attributes
= DECL_ATTRIBUTES (pattern_parm
);
21984 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
21986 DECL_ATTRIBUTES (decl_parm
) = attributes
;
21987 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
21989 decl_parm
= DECL_CHAIN (decl_parm
);
21992 /* Merge additional specifiers from the CODE_PATTERN. */
21993 if (DECL_DECLARED_INLINE_P (code_pattern
)
21994 && !DECL_DECLARED_INLINE_P (decl
))
21995 DECL_DECLARED_INLINE_P (decl
) = 1;
21997 else if (VAR_P (decl
))
21999 DECL_INITIAL (decl
) =
22000 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
22001 tf_error
, DECL_TI_TEMPLATE (decl
),
22002 /*integral_constant_expression_p=*/false);
22003 if (VAR_HAD_UNKNOWN_BOUND (decl
))
22004 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
22005 tf_error
, DECL_TI_TEMPLATE (decl
));
22008 gcc_unreachable ();
22010 pop_access_scope (decl
);
22013 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22014 substituted to get DECL. */
22017 template_for_substitution (tree decl
)
22019 tree tmpl
= DECL_TI_TEMPLATE (decl
);
22021 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22022 for the instantiation. This is not always the most general
22023 template. Consider, for example:
22026 struct S { template <class U> void f();
22027 template <> void f<int>(); };
22029 and an instantiation of S<double>::f<int>. We want TD to be the
22030 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22031 while (/* An instantiation cannot have a definition, so we need a
22032 more general template. */
22033 DECL_TEMPLATE_INSTANTIATION (tmpl
)
22034 /* We must also deal with friend templates. Given:
22036 template <class T> struct S {
22037 template <class U> friend void f() {};
22040 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22041 so far as the language is concerned, but that's still
22042 where we get the pattern for the instantiation from. On
22043 other hand, if the definition comes outside the class, say:
22045 template <class T> struct S {
22046 template <class U> friend void f();
22048 template <class U> friend void f() {}
22050 we don't need to look any further. That's what the check for
22051 DECL_INITIAL is for. */
22052 || (TREE_CODE (decl
) == FUNCTION_DECL
22053 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
22054 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
22056 /* The present template, TD, should not be a definition. If it
22057 were a definition, we should be using it! Note that we
22058 cannot restructure the loop to just keep going until we find
22059 a template with a definition, since that might go too far if
22060 a specialization was declared, but not defined. */
22062 /* Fetch the more general template. */
22063 tmpl
= DECL_TI_TEMPLATE (tmpl
);
22069 /* Returns true if we need to instantiate this template instance even if we
22070 know we aren't going to emit it. */
22073 always_instantiate_p (tree decl
)
22075 /* We always instantiate inline functions so that we can inline them. An
22076 explicit instantiation declaration prohibits implicit instantiation of
22077 non-inline functions. With high levels of optimization, we would
22078 normally inline non-inline functions -- but we're not allowed to do
22079 that for "extern template" functions. Therefore, we check
22080 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22081 return ((TREE_CODE (decl
) == FUNCTION_DECL
22082 && (DECL_DECLARED_INLINE_P (decl
)
22083 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
22084 /* And we need to instantiate static data members so that
22085 their initializers are available in integral constant
22088 && decl_maybe_constant_var_p (decl
)));
22091 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22092 instantiate it now, modifying TREE_TYPE (fn). */
22095 maybe_instantiate_noexcept (tree fn
)
22097 tree fntype
, spec
, noex
, clone
;
22099 /* Don't instantiate a noexcept-specification from template context. */
22100 if (processing_template_decl
)
22103 if (DECL_CLONED_FUNCTION_P (fn
))
22104 fn
= DECL_CLONED_FUNCTION (fn
);
22105 fntype
= TREE_TYPE (fn
);
22106 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
22108 if (!spec
|| !TREE_PURPOSE (spec
))
22111 noex
= TREE_PURPOSE (spec
);
22113 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
22115 if (DEFERRED_NOEXCEPT_PATTERN (noex
) == NULL_TREE
)
22116 spec
= get_defaulted_eh_spec (fn
);
22117 else if (push_tinst_level (fn
))
22119 push_access_scope (fn
);
22120 push_deferring_access_checks (dk_no_deferred
);
22121 input_location
= DECL_SOURCE_LOCATION (fn
);
22122 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
22123 DEFERRED_NOEXCEPT_ARGS (noex
),
22124 tf_warning_or_error
, fn
,
22125 /*function_p=*/false,
22126 /*integral_constant_expression_p=*/true);
22127 pop_deferring_access_checks ();
22128 pop_access_scope (fn
);
22129 pop_tinst_level ();
22130 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
22131 if (spec
== error_mark_node
)
22132 spec
= noexcept_false_spec
;
22135 spec
= noexcept_false_spec
;
22137 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
22140 FOR_EACH_CLONE (clone
, fn
)
22142 if (TREE_TYPE (clone
) == fntype
)
22143 TREE_TYPE (clone
) = TREE_TYPE (fn
);
22145 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
22149 /* Produce the definition of D, a _DECL generated from a template. If
22150 DEFER_OK is nonzero, then we don't have to actually do the
22151 instantiation now; we just have to do it sometime. Normally it is
22152 an error if this is an explicit instantiation but D is undefined.
22153 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
22154 explicitly instantiated class template. */
22157 instantiate_decl (tree d
, int defer_ok
,
22158 bool expl_inst_class_mem_p
)
22160 tree tmpl
= DECL_TI_TEMPLATE (d
);
22167 bool pattern_defined
;
22168 location_t saved_loc
= input_location
;
22169 int saved_unevaluated_operand
= cp_unevaluated_operand
;
22170 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22174 bool nested
= false;
22176 /* This function should only be used to instantiate templates for
22177 functions and static member variables. */
22178 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
22180 /* A concept is never instantiated. */
22181 gcc_assert (!DECL_DECLARED_CONCEPT_P (d
));
22183 /* Variables are never deferred; if instantiation is required, they
22184 are instantiated right away. That allows for better code in the
22185 case that an expression refers to the value of the variable --
22186 if the variable has a constant value the referring expression can
22187 take advantage of that fact. */
22191 /* Don't instantiate cloned functions. Instead, instantiate the
22192 functions they cloned. */
22193 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
22194 d
= DECL_CLONED_FUNCTION (d
);
22196 if (DECL_TEMPLATE_INSTANTIATED (d
)
22197 || (TREE_CODE (d
) == FUNCTION_DECL
22198 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
22199 || DECL_TEMPLATE_SPECIALIZATION (d
))
22200 /* D has already been instantiated or explicitly specialized, so
22201 there's nothing for us to do here.
22203 It might seem reasonable to check whether or not D is an explicit
22204 instantiation, and, if so, stop here. But when an explicit
22205 instantiation is deferred until the end of the compilation,
22206 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22207 the instantiation. */
22210 /* Check to see whether we know that this template will be
22211 instantiated in some other file, as with "extern template"
22213 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
22215 /* In general, we do not instantiate such templates. */
22216 if (external_p
&& !always_instantiate_p (d
))
22219 gen_tmpl
= most_general_template (tmpl
);
22220 gen_args
= DECL_TI_ARGS (d
);
22222 if (tmpl
!= gen_tmpl
)
22223 /* We should already have the extra args. */
22224 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
22225 == TMPL_ARGS_DEPTH (gen_args
));
22226 /* And what's in the hash table should match D. */
22227 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
22228 || spec
== NULL_TREE
);
22230 /* This needs to happen before any tsubsting. */
22231 if (! push_tinst_level (d
))
22234 timevar_push (TV_TEMPLATE_INST
);
22236 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22237 for the instantiation. */
22238 td
= template_for_substitution (d
);
22243 /* Look up an explicit specialization, if any. */
22244 tree tid
= lookup_template_variable (gen_tmpl
, gen_args
);
22245 tree elt
= most_specialized_partial_spec (tid
, tf_warning_or_error
);
22246 if (elt
&& elt
!= error_mark_node
)
22248 td
= TREE_VALUE (elt
);
22249 args
= TREE_PURPOSE (elt
);
22253 code_pattern
= DECL_TEMPLATE_RESULT (td
);
22255 /* We should never be trying to instantiate a member of a class
22256 template or partial specialization. */
22257 gcc_assert (d
!= code_pattern
);
22259 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
22260 || DECL_TEMPLATE_SPECIALIZATION (td
))
22261 /* In the case of a friend template whose definition is provided
22262 outside the class, we may have too many arguments. Drop the
22263 ones we don't need. The same is true for specializations. */
22264 args
= get_innermost_template_args
22265 (args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
22267 if (TREE_CODE (d
) == FUNCTION_DECL
)
22269 deleted_p
= DECL_DELETED_FN (code_pattern
);
22270 pattern_defined
= ((DECL_SAVED_TREE (code_pattern
) != NULL_TREE
22271 && DECL_INITIAL (code_pattern
) != error_mark_node
)
22272 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
)
22278 if (DECL_CLASS_SCOPE_P (code_pattern
))
22279 pattern_defined
= (! DECL_IN_AGGR_P (code_pattern
)
22280 || DECL_INLINE_VAR_P (code_pattern
));
22282 pattern_defined
= ! DECL_EXTERNAL (code_pattern
);
22285 /* We may be in the middle of deferred access check. Disable it now. */
22286 push_deferring_access_checks (dk_no_deferred
);
22288 /* Unless an explicit instantiation directive has already determined
22289 the linkage of D, remember that a definition is available for
22291 if (pattern_defined
22292 && !DECL_INTERFACE_KNOWN (d
)
22293 && !DECL_NOT_REALLY_EXTERN (d
))
22294 mark_definable (d
);
22296 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
22297 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
22298 input_location
= DECL_SOURCE_LOCATION (d
);
22300 /* If D is a member of an explicitly instantiated class template,
22301 and no definition is available, treat it like an implicit
22303 if (!pattern_defined
&& expl_inst_class_mem_p
22304 && DECL_EXPLICIT_INSTANTIATION (d
))
22306 /* Leave linkage flags alone on instantiations with anonymous
22308 if (TREE_PUBLIC (d
))
22310 DECL_NOT_REALLY_EXTERN (d
) = 0;
22311 DECL_INTERFACE_KNOWN (d
) = 0;
22313 SET_DECL_IMPLICIT_INSTANTIATION (d
);
22316 /* Defer all other templates, unless we have been explicitly
22317 forbidden from doing so. */
22318 if (/* If there is no definition, we cannot instantiate the
22321 /* If it's OK to postpone instantiation, do so. */
22323 /* If this is a static data member that will be defined
22324 elsewhere, we don't want to instantiate the entire data
22325 member, but we do want to instantiate the initializer so that
22326 we can substitute that elsewhere. */
22327 || (external_p
&& VAR_P (d
))
22328 /* Handle here a deleted function too, avoid generating
22329 its body (c++/61080). */
22332 /* The definition of the static data member is now required so
22333 we must substitute the initializer. */
22335 && !DECL_INITIAL (d
)
22336 && DECL_INITIAL (code_pattern
))
22340 bool const_init
= false;
22341 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22343 ns
= decl_namespace_context (d
);
22344 push_nested_namespace (ns
);
22346 push_nested_class (DECL_CONTEXT (d
));
22347 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
22349 tf_warning_or_error
, NULL_TREE
,
22350 /*integral_constant_expression_p=*/false);
22351 /* If instantiating the initializer involved instantiating this
22352 again, don't call cp_finish_decl twice. */
22353 if (!DECL_INITIAL (d
))
22355 /* Make sure the initializer is still constant, in case of
22356 circular dependency (template/instantiate6.C). */
22358 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22359 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
22360 /*asmspec_tree=*/NULL_TREE
,
22361 LOOKUP_ONLYCONVERTING
);
22364 pop_nested_class ();
22365 pop_nested_namespace (ns
);
22368 /* We restore the source position here because it's used by
22369 add_pending_template. */
22370 input_location
= saved_loc
;
22372 if (at_eof
&& !pattern_defined
22373 && DECL_EXPLICIT_INSTANTIATION (d
)
22374 && DECL_NOT_REALLY_EXTERN (d
))
22377 The definition of a non-exported function template, a
22378 non-exported member function template, or a non-exported
22379 member function or static data member of a class template
22380 shall be present in every translation unit in which it is
22381 explicitly instantiated. */
22382 permerror (input_location
, "explicit instantiation of %qD "
22383 "but no definition available", d
);
22385 /* If we're in unevaluated context, we just wanted to get the
22386 constant value; this isn't an odr use, so don't queue
22387 a full instantiation. */
22388 if (cp_unevaluated_operand
!= 0)
22390 /* ??? Historically, we have instantiated inline functions, even
22391 when marked as "extern template". */
22392 if (!(external_p
&& VAR_P (d
)))
22393 add_pending_template (d
);
22396 /* Tell the repository that D is available in this translation unit
22397 -- and see if it is supposed to be instantiated here. */
22398 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
22400 /* In a PCH file, despite the fact that the repository hasn't
22401 requested instantiation in the PCH it is still possible that
22402 an instantiation will be required in a file that includes the
22405 add_pending_template (d
);
22406 /* Instantiate inline functions so that the inliner can do its
22407 job, even though we'll not be emitting a copy of this
22409 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
22413 fn_context
= decl_function_context (d
);
22414 nested
= (current_function_decl
!= NULL_TREE
);
22415 vec
<tree
> omp_privatization_save
;
22417 save_omp_privatization_clauses (omp_privatization_save
);
22420 push_to_top_level ();
22424 push_function_context ();
22425 cp_unevaluated_operand
= 0;
22426 c_inhibit_evaluation_warnings
= 0;
22429 /* Mark D as instantiated so that recursive calls to
22430 instantiate_decl do not try to instantiate it again. */
22431 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
22433 /* Regenerate the declaration in case the template has been modified
22434 by a subsequent redeclaration. */
22435 regenerate_decl_from_template (d
, td
, args
);
22437 /* We already set the file and line above. Reset them now in case
22438 they changed as a result of calling regenerate_decl_from_template. */
22439 input_location
= DECL_SOURCE_LOCATION (d
);
22444 bool const_init
= false;
22446 /* Clear out DECL_RTL; whatever was there before may not be right
22447 since we've reset the type of the declaration. */
22448 SET_DECL_RTL (d
, NULL
);
22449 DECL_IN_AGGR_P (d
) = 0;
22451 /* The initializer is placed in DECL_INITIAL by
22452 regenerate_decl_from_template so we don't need to
22453 push/pop_access_scope again here. Pull it out so that
22454 cp_finish_decl can process it. */
22455 init
= DECL_INITIAL (d
);
22456 DECL_INITIAL (d
) = NULL_TREE
;
22457 DECL_INITIALIZED_P (d
) = 0;
22459 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22460 initializer. That function will defer actual emission until
22461 we have a chance to determine linkage. */
22462 DECL_EXTERNAL (d
) = 0;
22464 /* Enter the scope of D so that access-checking works correctly. */
22465 bool enter_context
= DECL_CLASS_SCOPE_P (d
);
22467 push_nested_class (DECL_CONTEXT (d
));
22469 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
22470 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
22473 pop_nested_class ();
22475 if (variable_template_p (gen_tmpl
))
22476 note_variable_template_instantiation (d
);
22478 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
22479 synthesize_method (d
);
22480 else if (TREE_CODE (d
) == FUNCTION_DECL
)
22482 hash_map
<tree
, tree
> *saved_local_specializations
;
22485 tree block
= NULL_TREE
;
22487 /* Save away the current list, in case we are instantiating one
22488 template from within the body of another. */
22489 saved_local_specializations
= local_specializations
;
22491 /* Set up the list of local specializations. */
22492 local_specializations
= new hash_map
<tree
, tree
>;
22494 /* Set up context. */
22495 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22496 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22497 block
= push_stmt_list ();
22499 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
22501 /* Some typedefs referenced from within the template code need to be
22502 access checked at template instantiation time, i.e now. These
22503 types were added to the template at parsing time. Let's get those
22504 and perform the access checks then. */
22505 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td
),
22508 /* Create substitution entries for the parameters. */
22509 tmpl_parm
= DECL_ARGUMENTS (code_pattern
);
22510 spec_parm
= DECL_ARGUMENTS (d
);
22511 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
22513 register_local_specialization (spec_parm
, tmpl_parm
);
22514 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
22515 tmpl_parm
= skip_artificial_parms_for (code_pattern
, tmpl_parm
);
22517 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
22519 if (!DECL_PACK_P (tmpl_parm
))
22521 register_local_specialization (spec_parm
, tmpl_parm
);
22522 spec_parm
= DECL_CHAIN (spec_parm
);
22526 /* Register the (value) argument pack as a specialization of
22527 TMPL_PARM, then move on. */
22528 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
22529 register_local_specialization (argpack
, tmpl_parm
);
22532 gcc_assert (!spec_parm
);
22534 /* Substitute into the body of the function. */
22535 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22536 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern
), args
,
22537 tf_warning_or_error
, tmpl
);
22540 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
22541 tf_warning_or_error
, tmpl
,
22542 /*integral_constant_expression_p=*/false);
22544 /* Set the current input_location to the end of the function
22545 so that finish_function knows where we are. */
22547 = DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
22549 /* Remember if we saw an infinite loop in the template. */
22550 current_function_infinite_loop
22551 = DECL_STRUCT_FUNCTION (code_pattern
)->language
->infinite_loop
;
22554 /* We don't need the local specializations any more. */
22555 delete local_specializations
;
22556 local_specializations
= saved_local_specializations
;
22558 /* Finish the function. */
22559 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
)
22560 && TREE_CODE (DECL_CONTEXT (code_pattern
)) == FUNCTION_DECL
)
22561 DECL_SAVED_TREE (d
) = pop_stmt_list (block
);
22564 d
= finish_function (0);
22565 expand_or_defer_fn (d
);
22568 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern
))
22569 cp_check_omp_declare_reduction (d
);
22572 /* We're not deferring instantiation any more. */
22573 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
22576 pop_from_top_level ();
22578 pop_function_context ();
22581 input_location
= saved_loc
;
22582 cp_unevaluated_operand
= saved_unevaluated_operand
;
22583 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
22584 pop_deferring_access_checks ();
22585 pop_tinst_level ();
22587 restore_omp_privatization_clauses (omp_privatization_save
);
22589 timevar_pop (TV_TEMPLATE_INST
);
22594 /* Run through the list of templates that we wish we could
22595 instantiate, and instantiate any we can. RETRIES is the
22596 number of times we retry pending template instantiation. */
22599 instantiate_pending_templates (int retries
)
22602 location_t saved_loc
= input_location
;
22604 /* Instantiating templates may trigger vtable generation. This in turn
22605 may require further template instantiations. We place a limit here
22606 to avoid infinite loop. */
22607 if (pending_templates
&& retries
>= max_tinst_depth
)
22609 tree decl
= pending_templates
->tinst
->decl
;
22611 fatal_error (input_location
,
22612 "template instantiation depth exceeds maximum of %d"
22613 " instantiating %q+D, possibly from virtual table generation"
22614 " (use -ftemplate-depth= to increase the maximum)",
22615 max_tinst_depth
, decl
);
22616 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22617 /* Pretend that we defined it. */
22618 DECL_INITIAL (decl
) = error_mark_node
;
22624 struct pending_template
**t
= &pending_templates
;
22625 struct pending_template
*last
= NULL
;
22629 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
22630 bool complete
= false;
22632 if (TYPE_P (instantiation
))
22636 if (!COMPLETE_TYPE_P (instantiation
))
22638 instantiate_class_template (instantiation
);
22639 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
22640 for (fn
= TYPE_METHODS (instantiation
);
22642 fn
= TREE_CHAIN (fn
))
22643 if (! DECL_ARTIFICIAL (fn
))
22644 instantiate_decl (fn
,
22646 /*expl_inst_class_mem_p=*/false);
22647 if (COMPLETE_TYPE_P (instantiation
))
22651 complete
= COMPLETE_TYPE_P (instantiation
);
22655 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
22656 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
22659 = instantiate_decl (instantiation
,
22661 /*expl_inst_class_mem_p=*/false);
22662 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
22666 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
22667 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
22671 /* If INSTANTIATION has been instantiated, then we don't
22672 need to consider it again in the future. */
22680 current_tinst_level
= NULL
;
22682 last_pending_template
= last
;
22684 while (reconsider
);
22686 input_location
= saved_loc
;
22689 /* Substitute ARGVEC into T, which is a list of initializers for
22690 either base class or a non-static data member. The TREE_PURPOSEs
22691 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22692 instantiate_decl. */
22695 tsubst_initializer_list (tree t
, tree argvec
)
22697 tree inits
= NULL_TREE
;
22699 for (; t
; t
= TREE_CHAIN (t
))
22703 tree expanded_bases
= NULL_TREE
;
22704 tree expanded_arguments
= NULL_TREE
;
22707 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
22712 /* Expand the base class expansion type into separate base
22714 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
22715 tf_warning_or_error
,
22717 if (expanded_bases
== error_mark_node
)
22720 /* We'll be building separate TREE_LISTs of arguments for
22722 len
= TREE_VEC_LENGTH (expanded_bases
);
22723 expanded_arguments
= make_tree_vec (len
);
22724 for (i
= 0; i
< len
; i
++)
22725 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
22727 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22728 expand each argument in the TREE_VALUE of t. */
22729 expr
= make_node (EXPR_PACK_EXPANSION
);
22730 PACK_EXPANSION_LOCAL_P (expr
) = true;
22731 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
22732 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
22734 if (TREE_VALUE (t
) == void_type_node
)
22735 /* VOID_TYPE_NODE is used to indicate
22736 value-initialization. */
22738 for (i
= 0; i
< len
; i
++)
22739 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
22743 /* Substitute parameter packs into each argument in the
22745 in_base_initializer
= 1;
22746 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
22748 tree expanded_exprs
;
22750 /* Expand the argument. */
22751 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
22753 = tsubst_pack_expansion (expr
, argvec
,
22754 tf_warning_or_error
,
22756 if (expanded_exprs
== error_mark_node
)
22759 /* Prepend each of the expanded expressions to the
22760 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22761 for (i
= 0; i
< len
; i
++)
22763 TREE_VEC_ELT (expanded_arguments
, i
) =
22764 tree_cons (NULL_TREE
,
22765 TREE_VEC_ELT (expanded_exprs
, i
),
22766 TREE_VEC_ELT (expanded_arguments
, i
));
22769 in_base_initializer
= 0;
22771 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22772 since we built them backwards. */
22773 for (i
= 0; i
< len
; i
++)
22775 TREE_VEC_ELT (expanded_arguments
, i
) =
22776 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
22781 for (i
= 0; i
< len
; ++i
)
22783 if (expanded_bases
)
22785 decl
= TREE_VEC_ELT (expanded_bases
, i
);
22786 decl
= expand_member_init (decl
);
22787 init
= TREE_VEC_ELT (expanded_arguments
, i
);
22792 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
22793 tf_warning_or_error
, NULL_TREE
);
22795 decl
= expand_member_init (decl
);
22796 if (decl
&& !DECL_P (decl
))
22797 in_base_initializer
= 1;
22799 init
= TREE_VALUE (t
);
22801 if (init
!= void_type_node
)
22802 init
= tsubst_expr (init
, argvec
,
22803 tf_warning_or_error
, NULL_TREE
,
22804 /*integral_constant_expression_p=*/false);
22805 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
22806 /* If we had an initializer but it instantiated to nothing,
22807 value-initialize the object. This will only occur when
22808 the initializer was a pack expansion where the parameter
22809 packs used in that expansion were of length zero. */
22810 init
= void_type_node
;
22811 in_base_initializer
= 0;
22816 init
= build_tree_list (decl
, init
);
22817 TREE_CHAIN (init
) = inits
;
22825 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22828 set_current_access_from_decl (tree decl
)
22830 if (TREE_PRIVATE (decl
))
22831 current_access_specifier
= access_private_node
;
22832 else if (TREE_PROTECTED (decl
))
22833 current_access_specifier
= access_protected_node
;
22835 current_access_specifier
= access_public_node
;
22838 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22839 is the instantiation (which should have been created with
22840 start_enum) and ARGS are the template arguments to use. */
22843 tsubst_enum (tree tag
, tree newtag
, tree args
)
22847 if (SCOPED_ENUM_P (newtag
))
22848 begin_scope (sk_scoped_enum
, newtag
);
22850 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
22855 decl
= TREE_VALUE (e
);
22856 /* Note that in a template enum, the TREE_VALUE is the
22857 CONST_DECL, not the corresponding INTEGER_CST. */
22858 value
= tsubst_expr (DECL_INITIAL (decl
),
22859 args
, tf_warning_or_error
, NULL_TREE
,
22860 /*integral_constant_expression_p=*/true);
22862 /* Give this enumeration constant the correct access. */
22863 set_current_access_from_decl (decl
);
22865 /* Actually build the enumerator itself. Here we're assuming that
22866 enumerators can't have dependent attributes. */
22867 build_enumerator (DECL_NAME (decl
), value
, newtag
,
22868 DECL_ATTRIBUTES (decl
), DECL_SOURCE_LOCATION (decl
));
22871 if (SCOPED_ENUM_P (newtag
))
22874 finish_enum_value_list (newtag
);
22875 finish_enum (newtag
);
22877 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
22878 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
22881 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22882 its type -- but without substituting the innermost set of template
22883 arguments. So, innermost set of template parameters will appear in
22887 get_mostly_instantiated_function_type (tree decl
)
22889 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22890 return TREE_TYPE (DECL_TI_TEMPLATE (decl
));
22893 /* Return truthvalue if we're processing a template different from
22894 the last one involved in diagnostics. */
22896 problematic_instantiation_changed (void)
22898 return current_tinst_level
!= last_error_tinst_level
;
22901 /* Remember current template involved in diagnostics. */
22903 record_last_problematic_instantiation (void)
22905 last_error_tinst_level
= current_tinst_level
;
22908 struct tinst_level
*
22909 current_instantiation (void)
22911 return current_tinst_level
;
22914 /* Return TRUE if current_function_decl is being instantiated, false
22918 instantiating_current_function_p (void)
22920 return (current_instantiation ()
22921 && current_instantiation ()->decl
== current_function_decl
);
22924 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22925 type. Return zero for ok, nonzero for disallowed. Issue error and
22926 warning messages under control of COMPLAIN. */
22929 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
22931 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
22933 else if (POINTER_TYPE_P (type
))
22935 else if (TYPE_PTRMEM_P (type
))
22937 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
22939 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
22941 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
22943 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
22945 /* A bound template template parm could later be instantiated to have a valid
22946 nontype parm type via an alias template. */
22947 else if (cxx_dialect
>= cxx11
22948 && TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
22951 if (complain
& tf_error
)
22953 if (type
== error_mark_node
)
22954 inform (input_location
, "invalid template non-type parameter");
22956 error ("%q#T is not a valid type for a template non-type parameter",
22962 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22963 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22966 dependent_type_p_r (tree type
)
22972 A type is dependent if it is:
22974 -- a template parameter. Template template parameters are types
22975 for us (since TYPE_P holds true for them) so we handle
22977 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
22978 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
22980 /* -- a qualified-id with a nested-name-specifier which contains a
22981 class-name that names a dependent type or whose unqualified-id
22982 names a dependent type. */
22983 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22986 /* An alias template specialization can be dependent even if the
22987 resulting type is not. */
22988 if (dependent_alias_template_spec_p (type
))
22991 /* -- a cv-qualified type where the cv-unqualified type is
22993 No code is necessary for this bullet; the code below handles
22994 cv-qualified types, and we don't want to strip aliases with
22995 TYPE_MAIN_VARIANT because of DR 1558. */
22996 /* -- a compound type constructed from any dependent type. */
22997 if (TYPE_PTRMEM_P (type
))
22998 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
22999 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23001 else if (TYPE_PTR_P (type
)
23002 || TREE_CODE (type
) == REFERENCE_TYPE
)
23003 return dependent_type_p (TREE_TYPE (type
));
23004 else if (TREE_CODE (type
) == FUNCTION_TYPE
23005 || TREE_CODE (type
) == METHOD_TYPE
)
23009 if (dependent_type_p (TREE_TYPE (type
)))
23011 for (arg_type
= TYPE_ARG_TYPES (type
);
23013 arg_type
= TREE_CHAIN (arg_type
))
23014 if (dependent_type_p (TREE_VALUE (arg_type
)))
23018 /* -- an array type constructed from any dependent type or whose
23019 size is specified by a constant expression that is
23022 We checked for type- and value-dependence of the bounds in
23023 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23024 if (TREE_CODE (type
) == ARRAY_TYPE
)
23026 if (TYPE_DOMAIN (type
)
23027 && dependent_type_p (TYPE_DOMAIN (type
)))
23029 return dependent_type_p (TREE_TYPE (type
));
23032 /* -- a template-id in which either the template name is a template
23034 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
23036 /* ... or any of the template arguments is a dependent type or
23037 an expression that is type-dependent or value-dependent. */
23038 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
23039 && (any_dependent_template_arguments_p
23040 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
23043 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23044 dependent; if the argument of the `typeof' expression is not
23045 type-dependent, then it should already been have resolved. */
23046 if (TREE_CODE (type
) == TYPEOF_TYPE
23047 || TREE_CODE (type
) == DECLTYPE_TYPE
23048 || TREE_CODE (type
) == UNDERLYING_TYPE
)
23051 /* A template argument pack is dependent if any of its packed
23053 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
23055 tree args
= ARGUMENT_PACK_ARGS (type
);
23056 int i
, len
= TREE_VEC_LENGTH (args
);
23057 for (i
= 0; i
< len
; ++i
)
23058 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23062 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23063 be template parameters. */
23064 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
23067 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type
)))
23070 /* The standard does not specifically mention types that are local
23071 to template functions or local classes, but they should be
23072 considered dependent too. For example:
23074 template <int I> void f() {
23079 The size of `E' cannot be known until the value of `I' has been
23080 determined. Therefore, `E' must be considered dependent. */
23081 scope
= TYPE_CONTEXT (type
);
23082 if (scope
&& TYPE_P (scope
))
23083 return dependent_type_p (scope
);
23084 /* Don't use type_dependent_expression_p here, as it can lead
23085 to infinite recursion trying to determine whether a lambda
23086 nested in a lambda is dependent (c++/47687). */
23087 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
23088 && DECL_LANG_SPECIFIC (scope
)
23089 && DECL_TEMPLATE_INFO (scope
)
23090 && (any_dependent_template_arguments_p
23091 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
23094 /* Other types are non-dependent. */
23098 /* Returns TRUE if TYPE is dependent, in the sense of
23099 [temp.dep.type]. Note that a NULL type is considered dependent. */
23102 dependent_type_p (tree type
)
23104 /* If there are no template parameters in scope, then there can't be
23105 any dependent types. */
23106 if (!processing_template_decl
)
23108 /* If we are not processing a template, then nobody should be
23109 providing us with a dependent type. */
23111 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
23115 /* If the type is NULL, we have not computed a type for the entity
23116 in question; in that case, the type is dependent. */
23120 /* Erroneous types can be considered non-dependent. */
23121 if (type
== error_mark_node
)
23124 /* If we have not already computed the appropriate value for TYPE,
23126 if (!TYPE_DEPENDENT_P_VALID (type
))
23128 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
23129 TYPE_DEPENDENT_P_VALID (type
) = 1;
23132 return TYPE_DEPENDENT_P (type
);
23135 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23136 lookup. In other words, a dependent type that is not the current
23140 dependent_scope_p (tree scope
)
23142 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
23143 && !currently_open_class (scope
));
23146 /* T is a SCOPE_REF; return whether we need to consider it
23147 instantiation-dependent so that we can check access at instantiation
23148 time even though we know which member it resolves to. */
23151 instantiation_dependent_scope_ref_p (tree t
)
23153 if (DECL_P (TREE_OPERAND (t
, 1))
23154 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
23155 && accessible_in_template_p (TREE_OPERAND (t
, 0),
23156 TREE_OPERAND (t
, 1)))
23162 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23163 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23166 /* Note that this predicate is not appropriate for general expressions;
23167 only constant expressions (that satisfy potential_constant_expression)
23168 can be tested for value dependence. */
23171 value_dependent_expression_p (tree expression
)
23173 if (!processing_template_decl
|| expression
== NULL_TREE
)
23176 /* A name declared with a dependent type. */
23177 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
23180 switch (TREE_CODE (expression
))
23183 /* A dependent member function of the current instantiation. */
23184 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression
)));
23186 case FUNCTION_DECL
:
23187 /* A dependent member function of the current instantiation. */
23188 if (DECL_CLASS_SCOPE_P (expression
)
23189 && dependent_type_p (DECL_CONTEXT (expression
)))
23193 case IDENTIFIER_NODE
:
23194 /* A name that has not been looked up -- must be dependent. */
23197 case TEMPLATE_PARM_INDEX
:
23198 /* A non-type template parm. */
23202 /* A non-type template parm. */
23203 if (DECL_TEMPLATE_PARM_P (expression
))
23205 return value_dependent_expression_p (DECL_INITIAL (expression
));
23208 /* A constant with literal type and is initialized
23209 with an expression that is value-dependent.
23211 Note that a non-dependent parenthesized initializer will have
23212 already been replaced with its constant value, so if we see
23213 a TREE_LIST it must be dependent. */
23214 if (DECL_INITIAL (expression
)
23215 && decl_constant_var_p (expression
)
23216 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
23217 /* cp_finish_decl doesn't fold reference initializers. */
23218 || TREE_CODE (TREE_TYPE (expression
)) == REFERENCE_TYPE
23219 || type_dependent_expression_p (DECL_INITIAL (expression
))
23220 || value_dependent_expression_p (DECL_INITIAL (expression
))))
23224 case DYNAMIC_CAST_EXPR
:
23225 case STATIC_CAST_EXPR
:
23226 case CONST_CAST_EXPR
:
23227 case REINTERPRET_CAST_EXPR
:
23229 /* These expressions are value-dependent if the type to which
23230 the cast occurs is dependent or the expression being casted
23231 is value-dependent. */
23233 tree type
= TREE_TYPE (expression
);
23235 if (dependent_type_p (type
))
23238 /* A functional cast has a list of operands. */
23239 expression
= TREE_OPERAND (expression
, 0);
23242 /* If there are no operands, it must be an expression such
23243 as "int()". This should not happen for aggregate types
23244 because it would form non-constant expressions. */
23245 gcc_assert (cxx_dialect
>= cxx11
23246 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
23251 if (TREE_CODE (expression
) == TREE_LIST
)
23252 return any_value_dependent_elements_p (expression
);
23254 return value_dependent_expression_p (expression
);
23258 if (SIZEOF_EXPR_TYPE_P (expression
))
23259 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
23263 /* A `sizeof' expression is value-dependent if the operand is
23264 type-dependent or is a pack expansion. */
23265 expression
= TREE_OPERAND (expression
, 0);
23266 if (PACK_EXPANSION_P (expression
))
23268 else if (TYPE_P (expression
))
23269 return dependent_type_p (expression
);
23270 return instantiation_dependent_uneval_expression_p (expression
);
23272 case AT_ENCODE_EXPR
:
23273 /* An 'encode' expression is value-dependent if the operand is
23275 expression
= TREE_OPERAND (expression
, 0);
23276 return dependent_type_p (expression
);
23278 case NOEXCEPT_EXPR
:
23279 expression
= TREE_OPERAND (expression
, 0);
23280 return instantiation_dependent_uneval_expression_p (expression
);
23283 /* All instantiation-dependent expressions should also be considered
23284 value-dependent. */
23285 return instantiation_dependent_scope_ref_p (expression
);
23287 case COMPONENT_REF
:
23288 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
23289 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
23291 case NONTYPE_ARGUMENT_PACK
:
23292 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23293 is value-dependent. */
23295 tree values
= ARGUMENT_PACK_ARGS (expression
);
23296 int i
, len
= TREE_VEC_LENGTH (values
);
23298 for (i
= 0; i
< len
; ++i
)
23299 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
23307 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
23308 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
23309 || (type2
? dependent_type_p (type2
) : false));
23313 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23314 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
23317 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23318 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
23322 tree op
= TREE_OPERAND (expression
, 0);
23323 return (value_dependent_expression_p (op
)
23324 || has_value_dependent_address (op
));
23327 case REQUIRES_EXPR
:
23328 /* Treat all requires-expressions as value-dependent so
23329 we don't try to fold them. */
23333 return dependent_type_p (TREE_OPERAND (expression
, 0));
23337 if (value_dependent_expression_p (CALL_EXPR_FN (expression
)))
23339 tree fn
= get_callee_fndecl (expression
);
23341 nargs
= call_expr_nargs (expression
);
23342 for (i
= 0; i
< nargs
; ++i
)
23344 tree op
= CALL_EXPR_ARG (expression
, i
);
23345 /* In a call to a constexpr member function, look through the
23346 implicit ADDR_EXPR on the object argument so that it doesn't
23347 cause the call to be considered value-dependent. We also
23348 look through it in potential_constant_expression. */
23349 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
23350 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
23351 && TREE_CODE (op
) == ADDR_EXPR
)
23352 op
= TREE_OPERAND (op
, 0);
23353 if (value_dependent_expression_p (op
))
23359 case TEMPLATE_ID_EXPR
:
23360 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23362 return type_dependent_expression_p (expression
)
23363 || variable_concept_p (TREE_OPERAND (expression
, 0));
23369 if (dependent_type_p (TREE_TYPE (expression
)))
23371 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
23372 if (value_dependent_expression_p (val
))
23378 /* Treat a GNU statement expression as dependent to avoid crashing
23379 under instantiate_non_dependent_expr; it can't be constant. */
23383 /* A constant expression is value-dependent if any subexpression is
23384 value-dependent. */
23385 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
23387 case tcc_reference
:
23389 case tcc_comparison
:
23391 case tcc_expression
:
23394 int i
, len
= cp_tree_operand_length (expression
);
23396 for (i
= 0; i
< len
; i
++)
23398 tree t
= TREE_OPERAND (expression
, i
);
23400 /* In some cases, some of the operands may be missing.l
23401 (For example, in the case of PREDECREMENT_EXPR, the
23402 amount to increment by may be missing.) That doesn't
23403 make the expression dependent. */
23404 if (t
&& value_dependent_expression_p (t
))
23415 /* The expression is not value-dependent. */
23419 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23420 [temp.dep.expr]. Note that an expression with no type is
23421 considered dependent. Other parts of the compiler arrange for an
23422 expression with type-dependent subexpressions to have no type, so
23423 this function doesn't have to be fully recursive. */
23426 type_dependent_expression_p (tree expression
)
23428 if (!processing_template_decl
)
23431 if (expression
== NULL_TREE
|| expression
== error_mark_node
)
23434 /* An unresolved name is always dependent. */
23435 if (identifier_p (expression
)
23436 || TREE_CODE (expression
) == USING_DECL
23437 || TREE_CODE (expression
) == WILDCARD_DECL
)
23440 /* A fold expression is type-dependent. */
23441 if (TREE_CODE (expression
) == UNARY_LEFT_FOLD_EXPR
23442 || TREE_CODE (expression
) == UNARY_RIGHT_FOLD_EXPR
23443 || TREE_CODE (expression
) == BINARY_LEFT_FOLD_EXPR
23444 || TREE_CODE (expression
) == BINARY_RIGHT_FOLD_EXPR
)
23447 /* Some expression forms are never type-dependent. */
23448 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
23449 || TREE_CODE (expression
) == SIZEOF_EXPR
23450 || TREE_CODE (expression
) == ALIGNOF_EXPR
23451 || TREE_CODE (expression
) == AT_ENCODE_EXPR
23452 || TREE_CODE (expression
) == NOEXCEPT_EXPR
23453 || TREE_CODE (expression
) == TRAIT_EXPR
23454 || TREE_CODE (expression
) == TYPEID_EXPR
23455 || TREE_CODE (expression
) == DELETE_EXPR
23456 || TREE_CODE (expression
) == VEC_DELETE_EXPR
23457 || TREE_CODE (expression
) == THROW_EXPR
23458 || TREE_CODE (expression
) == REQUIRES_EXPR
)
23461 /* The types of these expressions depends only on the type to which
23462 the cast occurs. */
23463 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
23464 || TREE_CODE (expression
) == STATIC_CAST_EXPR
23465 || TREE_CODE (expression
) == CONST_CAST_EXPR
23466 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
23467 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
23468 || TREE_CODE (expression
) == CAST_EXPR
)
23469 return dependent_type_p (TREE_TYPE (expression
));
23471 /* The types of these expressions depends only on the type created
23472 by the expression. */
23473 if (TREE_CODE (expression
) == NEW_EXPR
23474 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
23476 /* For NEW_EXPR tree nodes created inside a template, either
23477 the object type itself or a TREE_LIST may appear as the
23479 tree type
= TREE_OPERAND (expression
, 1);
23480 if (TREE_CODE (type
) == TREE_LIST
)
23481 /* This is an array type. We need to check array dimensions
23483 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
23484 || value_dependent_expression_p
23485 (TREE_OPERAND (TREE_VALUE (type
), 1));
23487 return dependent_type_p (type
);
23490 if (TREE_CODE (expression
) == SCOPE_REF
)
23492 tree scope
= TREE_OPERAND (expression
, 0);
23493 tree name
= TREE_OPERAND (expression
, 1);
23495 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23496 contains an identifier associated by name lookup with one or more
23497 declarations declared with a dependent type, or...a
23498 nested-name-specifier or qualified-id that names a member of an
23499 unknown specialization. */
23500 return (type_dependent_expression_p (name
)
23501 || dependent_scope_p (scope
));
23504 if (TREE_CODE (expression
) == TEMPLATE_DECL
23505 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
23506 return uses_outer_template_parms (expression
);
23508 if (TREE_CODE (expression
) == STMT_EXPR
)
23509 expression
= stmt_expr_value_expr (expression
);
23511 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
23516 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
23518 if (type_dependent_expression_p (elt
))
23524 /* A static data member of the current instantiation with incomplete
23525 array type is type-dependent, as the definition and specializations
23526 can have different bounds. */
23527 if (VAR_P (expression
)
23528 && DECL_CLASS_SCOPE_P (expression
)
23529 && dependent_type_p (DECL_CONTEXT (expression
))
23530 && VAR_HAD_UNKNOWN_BOUND (expression
))
23533 /* An array of unknown bound depending on a variadic parameter, eg:
23535 template<typename... Args>
23536 void foo (Args... args)
23538 int arr[] = { args... };
23541 template<int... vals>
23544 int arr[] = { vals... };
23547 If the array has no length and has an initializer, it must be that
23548 we couldn't determine its length in cp_complete_array_type because
23549 it is dependent. */
23550 if (VAR_P (expression
)
23551 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
23552 && !TYPE_DOMAIN (TREE_TYPE (expression
))
23553 && DECL_INITIAL (expression
))
23556 /* A function or variable template-id is type-dependent if it has any
23557 dependent template arguments. Note that we only consider the innermost
23558 template arguments here, since those are the ones that come from the
23559 template-id; the template arguments for the enclosing class do not make it
23560 type-dependent, they only make a member function value-dependent. */
23561 if (VAR_OR_FUNCTION_DECL_P (expression
)
23562 && DECL_LANG_SPECIFIC (expression
)
23563 && DECL_TEMPLATE_INFO (expression
)
23564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression
))
23565 && (any_dependent_template_arguments_p
23566 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
23569 /* Always dependent, on the number of arguments if nothing else. */
23570 if (TREE_CODE (expression
) == EXPR_PACK_EXPANSION
)
23573 if (TREE_TYPE (expression
) == unknown_type_node
)
23575 if (TREE_CODE (expression
) == ADDR_EXPR
)
23576 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
23577 if (TREE_CODE (expression
) == COMPONENT_REF
23578 || TREE_CODE (expression
) == OFFSET_REF
)
23580 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
23582 expression
= TREE_OPERAND (expression
, 1);
23583 if (identifier_p (expression
))
23586 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23587 if (TREE_CODE (expression
) == SCOPE_REF
)
23590 if (BASELINK_P (expression
))
23592 if (BASELINK_OPTYPE (expression
)
23593 && dependent_type_p (BASELINK_OPTYPE (expression
)))
23595 expression
= BASELINK_FUNCTIONS (expression
);
23598 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
23600 if (any_dependent_template_arguments_p
23601 (TREE_OPERAND (expression
, 1)))
23603 expression
= TREE_OPERAND (expression
, 0);
23604 if (identifier_p (expression
))
23608 gcc_assert (TREE_CODE (expression
) == OVERLOAD
23609 || TREE_CODE (expression
) == FUNCTION_DECL
);
23613 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
23615 expression
= OVL_NEXT (expression
);
23620 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
23622 /* Dependent type attributes might not have made it from the decl to
23624 if (DECL_P (expression
)
23625 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression
)))
23628 return (dependent_type_p (TREE_TYPE (expression
)));
23631 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23632 type-dependent if the expression refers to a member of the current
23633 instantiation and the type of the referenced member is dependent, or the
23634 class member access expression refers to a member of an unknown
23637 This function returns true if the OBJECT in such a class member access
23638 expression is of an unknown specialization. */
23641 type_dependent_object_expression_p (tree object
)
23643 tree scope
= TREE_TYPE (object
);
23644 return (!scope
|| dependent_scope_p (scope
));
23647 /* walk_tree callback function for instantiation_dependent_expression_p,
23648 below. Returns non-zero if a dependent subexpression is found. */
23651 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
23656 /* We don't have to worry about decltype currently because decltype
23657 of an instantiation-dependent expr is a dependent type. This
23658 might change depending on the resolution of DR 1172. */
23659 *walk_subtrees
= false;
23662 enum tree_code code
= TREE_CODE (*tp
);
23665 /* Don't treat an argument list as dependent just because it has no
23671 case TEMPLATE_PARM_INDEX
:
23674 /* Handle expressions with type operands. */
23678 case AT_ENCODE_EXPR
:
23680 tree op
= TREE_OPERAND (*tp
, 0);
23681 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
23682 op
= TREE_TYPE (op
);
23685 if (dependent_type_p (op
))
23689 *walk_subtrees
= false;
23696 case COMPONENT_REF
:
23697 if (identifier_p (TREE_OPERAND (*tp
, 1)))
23698 /* In a template, finish_class_member_access_expr creates a
23699 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23700 type-dependent, so that we can check access control at
23701 instantiation time (PR 42277). See also Core issue 1273. */
23706 if (instantiation_dependent_scope_ref_p (*tp
))
23711 /* Treat statement-expressions as dependent. */
23715 /* Treat requires-expressions as dependent. */
23716 case REQUIRES_EXPR
:
23720 /* Treat calls to function concepts as dependent. */
23721 if (function_concept_check_p (*tp
))
23725 case TEMPLATE_ID_EXPR
:
23726 /* And variable concepts. */
23727 if (variable_concept_p (TREE_OPERAND (*tp
, 0)))
23735 if (type_dependent_expression_p (*tp
))
23741 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23742 sense defined by the ABI:
23744 "An expression is instantiation-dependent if it is type-dependent
23745 or value-dependent, or it has a subexpression that is type-dependent
23746 or value-dependent."
23748 Except don't actually check value-dependence for unevaluated expressions,
23749 because in sizeof(i) we don't care about the value of i. Checking
23750 type-dependence will in turn check value-dependence of array bounds/template
23751 arguments as needed. */
23754 instantiation_dependent_uneval_expression_p (tree expression
)
23758 if (!processing_template_decl
)
23761 if (expression
== error_mark_node
)
23764 result
= cp_walk_tree_without_duplicates (&expression
,
23765 instantiation_dependent_r
, NULL
);
23766 return result
!= NULL_TREE
;
23769 /* As above, but also check value-dependence of the expression as a whole. */
23772 instantiation_dependent_expression_p (tree expression
)
23774 return (instantiation_dependent_uneval_expression_p (expression
)
23775 || value_dependent_expression_p (expression
));
23778 /* Like type_dependent_expression_p, but it also works while not processing
23779 a template definition, i.e. during substitution or mangling. */
23782 type_dependent_expression_p_push (tree expr
)
23785 ++processing_template_decl
;
23786 b
= type_dependent_expression_p (expr
);
23787 --processing_template_decl
;
23791 /* Returns TRUE if ARGS contains a type-dependent expression. */
23794 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
23799 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
23801 if (type_dependent_expression_p (arg
))
23807 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23808 expressions) contains any type-dependent expressions. */
23811 any_type_dependent_elements_p (const_tree list
)
23813 for (; list
; list
= TREE_CHAIN (list
))
23814 if (type_dependent_expression_p (TREE_VALUE (list
)))
23820 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23821 expressions) contains any value-dependent expressions. */
23824 any_value_dependent_elements_p (const_tree list
)
23826 for (; list
; list
= TREE_CHAIN (list
))
23827 if (value_dependent_expression_p (TREE_VALUE (list
)))
23833 /* Returns TRUE if the ARG (a template argument) is dependent. */
23836 dependent_template_arg_p (tree arg
)
23838 if (!processing_template_decl
)
23841 /* Assume a template argument that was wrongly written by the user
23842 is dependent. This is consistent with what
23843 any_dependent_template_arguments_p [that calls this function]
23845 if (!arg
|| arg
== error_mark_node
)
23848 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
23849 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
23851 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
23853 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23855 if (DECL_TEMPLATE_PARM_P (arg
))
23857 /* A member template of a dependent class is not necessarily
23858 type-dependent, but it is a dependent template argument because it
23859 will be a member of an unknown specialization to that template. */
23860 tree scope
= CP_DECL_CONTEXT (arg
);
23861 return TYPE_P (scope
) && dependent_type_p (scope
);
23863 else if (ARGUMENT_PACK_P (arg
))
23865 tree args
= ARGUMENT_PACK_ARGS (arg
);
23866 int i
, len
= TREE_VEC_LENGTH (args
);
23867 for (i
= 0; i
< len
; ++i
)
23869 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
23875 else if (TYPE_P (arg
))
23876 return dependent_type_p (arg
);
23878 return (type_dependent_expression_p (arg
)
23879 || value_dependent_expression_p (arg
));
23882 /* Returns true if ARGS (a collection of template arguments) contains
23883 any types that require structural equality testing. */
23886 any_template_arguments_need_structural_equality_p (tree args
)
23893 if (args
== error_mark_node
)
23896 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23898 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23899 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23901 tree arg
= TREE_VEC_ELT (level
, j
);
23902 tree packed_args
= NULL_TREE
;
23905 if (ARGUMENT_PACK_P (arg
))
23907 /* Look inside the argument pack. */
23908 packed_args
= ARGUMENT_PACK_ARGS (arg
);
23909 len
= TREE_VEC_LENGTH (packed_args
);
23912 for (k
= 0; k
< len
; ++k
)
23915 arg
= TREE_VEC_ELT (packed_args
, k
);
23917 if (error_operand_p (arg
))
23919 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
23921 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
23923 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
23924 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
23933 /* Returns true if ARGS (a collection of template arguments) contains
23934 any dependent arguments. */
23937 any_dependent_template_arguments_p (const_tree args
)
23944 if (args
== error_mark_node
)
23947 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
23949 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
23950 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
23951 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
23958 /* Returns TRUE if the template TMPL is type-dependent. */
23961 dependent_template_p (tree tmpl
)
23963 if (TREE_CODE (tmpl
) == OVERLOAD
)
23967 if (dependent_template_p (OVL_CURRENT (tmpl
)))
23969 tmpl
= OVL_NEXT (tmpl
);
23974 /* Template template parameters are dependent. */
23975 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
23976 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
23978 /* So are names that have not been looked up. */
23979 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
23984 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23987 dependent_template_id_p (tree tmpl
, tree args
)
23989 return (dependent_template_p (tmpl
)
23990 || any_dependent_template_arguments_p (args
));
23993 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23997 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
24001 if (!processing_template_decl
)
24004 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
24006 tree decl
= TREE_VEC_ELT (declv
, i
);
24007 tree init
= TREE_VEC_ELT (initv
, i
);
24008 tree cond
= TREE_VEC_ELT (condv
, i
);
24009 tree incr
= TREE_VEC_ELT (incrv
, i
);
24011 if (type_dependent_expression_p (decl
)
24012 || TREE_CODE (decl
) == SCOPE_REF
)
24015 if (init
&& type_dependent_expression_p (init
))
24018 if (type_dependent_expression_p (cond
))
24021 if (COMPARISON_CLASS_P (cond
)
24022 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
24023 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
24026 if (TREE_CODE (incr
) == MODOP_EXPR
)
24028 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
24029 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
24032 else if (type_dependent_expression_p (incr
))
24034 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
24036 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
24038 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
24040 tree t
= TREE_OPERAND (incr
, 1);
24041 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
24042 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
24051 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24052 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24053 no such TYPE can be found. Note that this function peers inside
24054 uninstantiated templates and therefore should be used only in
24055 extremely limited situations. ONLY_CURRENT_P restricts this
24056 peering to the currently open classes hierarchy (which is required
24057 when comparing types). */
24060 resolve_typename_type (tree type
, bool only_current_p
)
24069 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
24071 scope
= TYPE_CONTEXT (type
);
24072 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24073 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24074 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24075 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24076 identifier of the TYPENAME_TYPE anymore.
24077 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24078 TYPENAME_TYPE instead, we avoid messing up with a possible
24079 typedef variant case. */
24080 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
24082 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24083 it first before we can figure out what NAME refers to. */
24084 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
24086 if (TYPENAME_IS_RESOLVING_P (scope
))
24087 /* Given a class template A with a dependent base with nested type C,
24088 typedef typename A::C::C C will land us here, as trying to resolve
24089 the initial A::C leads to the local C typedef, which leads back to
24090 A::C::C. So we break the recursion now. */
24093 scope
= resolve_typename_type (scope
, only_current_p
);
24095 /* If we don't know what SCOPE refers to, then we cannot resolve the
24097 if (!CLASS_TYPE_P (scope
))
24099 /* If this is a typedef, we don't want to look inside (c++/11987). */
24100 if (typedef_variant_p (type
))
24102 /* If SCOPE isn't the template itself, it will not have a valid
24103 TYPE_FIELDS list. */
24104 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
24105 /* scope is either the template itself or a compatible instantiation
24106 like X<T>, so look up the name in the original template. */
24107 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
24108 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24109 gcc_checking_assert (uses_template_parms (scope
));
24110 /* If scope has no fields, it can't be a current instantiation. Check this
24111 before currently_open_class to avoid infinite recursion (71515). */
24112 if (!TYPE_FIELDS (scope
))
24114 /* If the SCOPE is not the current instantiation, there's no reason
24115 to look inside it. */
24116 if (only_current_p
&& !currently_open_class (scope
))
24118 /* Enter the SCOPE so that name lookup will be resolved as if we
24119 were in the class definition. In particular, SCOPE will no
24120 longer be considered a dependent type. */
24121 pushed_scope
= push_scope (scope
);
24122 /* Look up the declaration. */
24123 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
24124 tf_warning_or_error
);
24126 result
= NULL_TREE
;
24128 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24129 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24132 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
24133 && TREE_CODE (decl
) == TYPE_DECL
)
24135 result
= TREE_TYPE (decl
);
24136 if (result
== error_mark_node
)
24137 result
= NULL_TREE
;
24139 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
24140 && DECL_CLASS_TEMPLATE_P (decl
))
24144 /* Obtain the template and the arguments. */
24145 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
24146 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
24147 /* Instantiate the template. */
24148 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
24149 /*entering_scope=*/0,
24150 tf_error
| tf_user
);
24151 if (result
== error_mark_node
)
24152 result
= NULL_TREE
;
24155 /* Leave the SCOPE. */
24157 pop_scope (pushed_scope
);
24159 /* If we failed to resolve it, return the original typename. */
24163 /* If lookup found a typename type, resolve that too. */
24164 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
24166 /* Ill-formed programs can cause infinite recursion here, so we
24167 must catch that. */
24168 TYPENAME_IS_RESOLVING_P (result
) = 1;
24169 result
= resolve_typename_type (result
, only_current_p
);
24170 TYPENAME_IS_RESOLVING_P (result
) = 0;
24173 /* Qualify the resulting type. */
24174 quals
= cp_type_quals (type
);
24176 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
24181 /* EXPR is an expression which is not type-dependent. Return a proxy
24182 for EXPR that can be used to compute the types of larger
24183 expressions containing EXPR. */
24186 build_non_dependent_expr (tree expr
)
24190 /* When checking, try to get a constant value for all non-dependent
24191 expressions in order to expose bugs in *_dependent_expression_p
24192 and constexpr. This can affect code generation, see PR70704, so
24193 only do this for -fchecking=2. */
24194 if (flag_checking
> 1
24195 && cxx_dialect
>= cxx11
24196 /* Don't do this during nsdmi parsing as it can lead to
24197 unexpected recursive instantiations. */
24198 && !parsing_nsdmi ()
24199 /* Don't do this during concept expansion either and for
24200 the same reason. */
24201 && !expanding_concept ())
24202 fold_non_dependent_expr (expr
);
24204 /* Preserve OVERLOADs; the functions must be available to resolve
24207 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
24208 inner_expr
= stmt_expr_value_expr (inner_expr
);
24209 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
24210 inner_expr
= TREE_OPERAND (inner_expr
, 0);
24211 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
24212 inner_expr
= TREE_OPERAND (inner_expr
, 1);
24213 if (is_overloaded_fn (inner_expr
)
24214 || TREE_CODE (inner_expr
) == OFFSET_REF
)
24216 /* There is no need to return a proxy for a variable. */
24219 /* Preserve string constants; conversions from string constants to
24220 "char *" are allowed, even though normally a "const char *"
24221 cannot be used to initialize a "char *". */
24222 if (TREE_CODE (expr
) == STRING_CST
)
24224 /* Preserve void and arithmetic constants, as an optimization -- there is no
24225 reason to create a new node. */
24226 if (TREE_CODE (expr
) == VOID_CST
24227 || TREE_CODE (expr
) == INTEGER_CST
24228 || TREE_CODE (expr
) == REAL_CST
)
24230 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24231 There is at least one place where we want to know that a
24232 particular expression is a throw-expression: when checking a ?:
24233 expression, there are special rules if the second or third
24234 argument is a throw-expression. */
24235 if (TREE_CODE (expr
) == THROW_EXPR
)
24238 /* Don't wrap an initializer list, we need to be able to look inside. */
24239 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
24242 /* Don't wrap a dummy object, we need to be able to test for it. */
24243 if (is_dummy_object (expr
))
24246 if (TREE_CODE (expr
) == COND_EXPR
)
24247 return build3 (COND_EXPR
,
24249 TREE_OPERAND (expr
, 0),
24250 (TREE_OPERAND (expr
, 1)
24251 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
24252 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
24253 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
24254 if (TREE_CODE (expr
) == COMPOUND_EXPR
24255 && !COMPOUND_EXPR_OVERLOADED (expr
))
24256 return build2 (COMPOUND_EXPR
,
24258 TREE_OPERAND (expr
, 0),
24259 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
24261 /* If the type is unknown, it can't really be non-dependent */
24262 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
24264 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24265 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
24268 /* ARGS is a vector of expressions as arguments to a function call.
24269 Replace the arguments with equivalent non-dependent expressions.
24270 This modifies ARGS in place. */
24273 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
24278 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
24280 tree newarg
= build_non_dependent_expr (arg
);
24282 (*args
)[ix
] = newarg
;
24286 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24287 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24288 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24291 make_auto_1 (tree name
, bool set_canonical
)
24293 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
24294 TYPE_NAME (au
) = build_decl (input_location
,
24295 TYPE_DECL
, name
, au
);
24296 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
24297 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
24298 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
24299 TYPE_NAME (au
), NULL_TREE
);
24301 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
24302 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
24303 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
24309 make_decltype_auto (void)
24311 return make_auto_1 (decltype_auto_identifier
, true);
24317 return make_auto_1 (auto_identifier
, true);
24320 /* Return a C++17 deduction placeholder for class template TMPL. */
24323 make_template_placeholder (tree tmpl
)
24325 tree t
= make_auto_1 (DECL_NAME (tmpl
), true);
24326 CLASS_PLACEHOLDER_TEMPLATE (t
) = tmpl
;
24330 /* Make a "constrained auto" type-specifier. This is an
24331 auto type with constraints that must be associated after
24332 deduction. The constraint is formed from the given
24333 CONC and its optional sequence of arguments, which are
24334 non-null if written as partial-concept-id. */
24337 make_constrained_auto (tree con
, tree args
)
24339 tree type
= make_auto_1 (auto_identifier
, false);
24341 /* Build the constraint. */
24342 tree tmpl
= DECL_TI_TEMPLATE (con
);
24345 expr
= build_concept_check (tmpl
, type
, args
);
24347 expr
= build_concept_check (build_overload (tmpl
, NULL_TREE
), type
, args
);
24349 tree constr
= normalize_expression (expr
);
24350 PLACEHOLDER_TYPE_CONSTRAINTS (type
) = constr
;
24352 /* Our canonical type depends on the constraint. */
24353 TYPE_CANONICAL (type
) = canonical_type_parameter (type
);
24355 /* Attach the constraint to the type declaration. */
24356 tree decl
= TYPE_NAME (type
);
24360 /* Given type ARG, return std::initializer_list<ARG>. */
24365 tree std_init_list
= namespace_binding
24366 (get_identifier ("initializer_list"), std_node
);
24368 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
24370 error ("deducing from brace-enclosed initializer list requires "
24371 "#include <initializer_list>");
24372 return error_mark_node
;
24374 argvec
= make_tree_vec (1);
24375 TREE_VEC_ELT (argvec
, 0) = arg
;
24376 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
24377 NULL_TREE
, 0, tf_warning_or_error
);
24380 /* Replace auto in TYPE with std::initializer_list<auto>. */
24383 listify_autos (tree type
, tree auto_node
)
24385 tree init_auto
= listify (auto_node
);
24386 tree argvec
= make_tree_vec (1);
24387 TREE_VEC_ELT (argvec
, 0) = init_auto
;
24388 if (processing_template_decl
)
24389 argvec
= add_to_template_args (current_template_args (), argvec
);
24390 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
24393 /* Hash traits for hashing possibly constrained 'auto'
24394 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24396 struct auto_hash
: default_hash_traits
<tree
>
24398 static inline hashval_t
hash (tree
);
24399 static inline bool equal (tree
, tree
);
24402 /* Hash the 'auto' T. */
24405 auto_hash::hash (tree t
)
24407 if (tree c
= PLACEHOLDER_TYPE_CONSTRAINTS (t
))
24408 /* Matching constrained-type-specifiers denote the same template
24409 parameter, so hash the constraint. */
24410 return hash_placeholder_constraint (c
);
24412 /* But unconstrained autos are all separate, so just hash the pointer. */
24413 return iterative_hash_object (t
, 0);
24416 /* Compare two 'auto's. */
24419 auto_hash::equal (tree t1
, tree t2
)
24424 tree c1
= PLACEHOLDER_TYPE_CONSTRAINTS (t1
);
24425 tree c2
= PLACEHOLDER_TYPE_CONSTRAINTS (t2
);
24427 /* Two unconstrained autos are distinct. */
24431 return equivalent_placeholder_constraints (c1
, c2
);
24434 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24435 constrained) auto, add it to the vector. */
24438 extract_autos_r (tree t
, void *data
)
24440 hash_table
<auto_hash
> &hash
= *(hash_table
<auto_hash
>*)data
;
24441 if (is_auto_or_concept (t
))
24443 /* All the autos were built with index 0; fix that up now. */
24444 tree
*p
= hash
.find_slot (t
, INSERT
);
24447 /* If this is a repeated constrained-type-specifier, use the index we
24449 idx
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p
));
24452 /* Otherwise this is new, so use the current count. */
24454 idx
= hash
.elements () - 1;
24456 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t
)) = idx
;
24459 /* Always keep walking. */
24463 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24464 says they can appear anywhere in the type. */
24467 extract_autos (tree type
)
24469 hash_set
<tree
> visited
;
24470 hash_table
<auto_hash
> hash (2);
24472 for_each_template_parm (type
, extract_autos_r
, &hash
, &visited
, true);
24474 tree tree_vec
= make_tree_vec (hash
.elements());
24475 for (hash_table
<auto_hash
>::iterator iter
= hash
.begin();
24476 iter
!= hash
.end(); ++iter
)
24479 unsigned i
= TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt
));
24480 TREE_VEC_ELT (tree_vec
, i
)
24481 = build_tree_list (NULL_TREE
, TYPE_NAME (elt
));
24487 /* The stem for deduction guide names. */
24488 const char *const dguide_base
= "__dguide_";
24490 /* Return the name for a deduction guide for class template TMPL. */
24493 dguide_name (tree tmpl
)
24495 tree type
= (TYPE_P (tmpl
) ? tmpl
: TREE_TYPE (tmpl
));
24496 tree tname
= TYPE_IDENTIFIER (type
);
24497 char *buf
= (char *) alloca (1 + strlen (dguide_base
)
24498 + IDENTIFIER_LENGTH (tname
));
24499 memcpy (buf
, dguide_base
, strlen (dguide_base
));
24500 memcpy (buf
+ strlen (dguide_base
), IDENTIFIER_POINTER (tname
),
24501 IDENTIFIER_LENGTH (tname
) + 1);
24502 tree dname
= get_identifier (buf
);
24503 TREE_TYPE (dname
) = type
;
24507 /* True if NAME is the name of a deduction guide. */
24510 dguide_name_p (tree name
)
24512 return (TREE_TYPE (name
)
24513 && !strncmp (IDENTIFIER_POINTER (name
), dguide_base
,
24514 strlen (dguide_base
)));
24517 /* True if FN is a deduction guide. */
24520 deduction_guide_p (tree fn
)
24522 if (tree name
= DECL_NAME (fn
))
24523 return dguide_name_p (name
);
24527 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24528 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24529 template parameter types. Note that the handling of template template
24530 parameters relies on current_template_parms being set appropriately for the
24534 rewrite_template_parm (tree olddecl
, unsigned index
, unsigned level
,
24535 tree tsubst_args
, tsubst_flags_t complain
)
24537 tree oldidx
= get_template_parm_index (olddecl
);
24540 if (TREE_CODE (olddecl
) == TYPE_DECL
24541 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
24543 newtype
= cxx_make_type (TREE_CODE (TREE_TYPE (olddecl
)));
24544 TYPE_MAIN_VARIANT (newtype
) = newtype
;
24547 newtype
= tsubst (TREE_TYPE (olddecl
), tsubst_args
,
24548 complain
, NULL_TREE
);
24551 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TREE_CODE (olddecl
),
24552 DECL_NAME (olddecl
), newtype
);
24553 SET_DECL_TEMPLATE_PARM_P (newdecl
);
24556 if (TREE_CODE (olddecl
) == TYPE_DECL
24557 || TREE_CODE (olddecl
) == TEMPLATE_DECL
)
24559 newidx
= TEMPLATE_TYPE_PARM_INDEX (newtype
)
24560 = build_template_parm_index (index
, level
, level
,
24562 TYPE_STUB_DECL (newtype
) = TYPE_NAME (newtype
) = newdecl
;
24563 TYPE_CANONICAL (newtype
) = canonical_type_parameter (newtype
);
24565 if (TREE_CODE (olddecl
) == TEMPLATE_DECL
)
24567 DECL_TEMPLATE_RESULT (newdecl
)
24568 = build_decl (DECL_SOURCE_LOCATION (olddecl
), TYPE_DECL
,
24569 DECL_NAME (olddecl
), newtype
);
24570 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl
)) = true;
24571 // First create a copy (ttargs) of tsubst_args with an
24572 // additional level for the template template parameter's own
24573 // template parameters (ttparms).
24574 tree ttparms
= (INNERMOST_TEMPLATE_PARMS
24575 (DECL_TEMPLATE_PARMS (olddecl
)));
24576 const int depth
= TMPL_ARGS_DEPTH (tsubst_args
);
24577 tree ttargs
= make_tree_vec (depth
+ 1);
24578 for (int i
= 0; i
< depth
; ++i
)
24579 TREE_VEC_ELT (ttargs
, i
) = TREE_VEC_ELT (tsubst_args
, i
);
24580 TREE_VEC_ELT (ttargs
, depth
)
24581 = template_parms_level_to_args (ttparms
);
24582 // Substitute ttargs into ttparms to fix references to
24583 // other template parameters.
24584 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
24586 // Now substitute again with args based on tparms, to reduce
24587 // the level of the ttparms.
24588 ttargs
= current_template_args ();
24589 ttparms
= tsubst_template_parms_level (ttparms
, ttargs
,
24591 // Finally, tack the adjusted parms onto tparms.
24592 ttparms
= tree_cons (size_int (depth
), ttparms
,
24593 current_template_parms
);
24594 DECL_TEMPLATE_PARMS (newdecl
) = ttparms
;
24599 tree oldconst
= TEMPLATE_PARM_DECL (oldidx
);
24601 = build_decl (DECL_SOURCE_LOCATION (oldconst
),
24602 TREE_CODE (oldconst
),
24603 DECL_NAME (oldconst
), newtype
);
24604 TREE_CONSTANT (newconst
) = TREE_CONSTANT (newdecl
)
24605 = TREE_READONLY (newconst
) = TREE_READONLY (newdecl
) = true;
24606 SET_DECL_TEMPLATE_PARM_P (newconst
);
24607 newidx
= build_template_parm_index (index
, level
, level
,
24608 newconst
, newtype
);
24609 DECL_INITIAL (newdecl
) = DECL_INITIAL (newconst
) = newidx
;
24612 TEMPLATE_PARM_PARAMETER_PACK (newidx
)
24613 = TEMPLATE_PARM_PARAMETER_PACK (oldidx
);
24617 /* Returns a C++17 class deduction guide template based on the constructor
24621 build_deduction_guide (tree ctor
, tree outer_args
, tsubst_flags_t complain
)
24624 ctor
= tsubst (ctor
, outer_args
, complain
, ctor
);
24625 tree type
= DECL_CONTEXT (ctor
);
24627 if (TREE_CODE (ctor
) == TEMPLATE_DECL
)
24630 ctor
= DECL_TEMPLATE_RESULT (fn_tmpl
);
24633 fn_tmpl
= DECL_TI_TEMPLATE (ctor
);
24635 tree tparms
= DECL_TEMPLATE_PARMS (fn_tmpl
);
24636 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24637 specialized args for the enclosing class. Strip those off, as the
24638 deduction guide won't have those template parameters. */
24639 tree targs
= get_innermost_template_args (DECL_TI_ARGS (ctor
),
24640 TMPL_PARMS_DEPTH (tparms
));
24641 /* Discard the 'this' parameter. */
24642 tree fparms
= FUNCTION_ARG_CHAIN (ctor
);
24643 tree fargs
= TREE_CHAIN (DECL_ARGUMENTS (ctor
));
24644 tree ci
= get_constraints (ctor
);
24646 if (PRIMARY_TEMPLATE_P (fn_tmpl
))
24648 /* For a member template constructor, we need to flatten the two template
24649 parameter lists into one, and then adjust the function signature
24650 accordingly. This gets...complicated. */
24651 ++processing_template_decl
;
24652 tree save_parms
= current_template_parms
;
24654 /* For a member template we should have two levels of parms/args, one for
24655 the class and one for the constructor. We stripped specialized args
24656 for further enclosing classes above. */
24657 const int depth
= 2;
24658 gcc_assert (TMPL_ARGS_DEPTH (targs
) == depth
);
24660 /* Template args for translating references to the two-level template
24661 parameters into references to the one-level template parameters we are
24663 tree tsubst_args
= copy_node (targs
);
24664 TMPL_ARGS_LEVEL (tsubst_args
, depth
)
24665 = copy_node (TMPL_ARGS_LEVEL (tsubst_args
, depth
));
24667 /* Template parms for the constructor template. */
24668 tree ftparms
= TREE_VALUE (tparms
);
24669 unsigned flen
= TREE_VEC_LENGTH (ftparms
);
24670 /* Template parms for the class template. */
24671 tparms
= TREE_CHAIN (tparms
);
24672 tree ctparms
= TREE_VALUE (tparms
);
24673 unsigned clen
= TREE_VEC_LENGTH (ctparms
);
24674 /* Template parms for the deduction guide start as a copy of the template
24675 parms for the class. We set current_template_parms for
24676 lookup_template_class_1. */
24677 current_template_parms
= tparms
= copy_node (tparms
);
24678 tree new_vec
= TREE_VALUE (tparms
) = make_tree_vec (flen
+ clen
);
24679 for (unsigned i
= 0; i
< clen
; ++i
)
24680 TREE_VEC_ELT (new_vec
, i
) = TREE_VEC_ELT (ctparms
, i
);
24682 /* Now we need to rewrite the constructor parms to append them to the
24684 for (unsigned i
= 0; i
< flen
; ++i
)
24686 unsigned index
= i
+ clen
;
24687 unsigned level
= 1;
24688 tree oldelt
= TREE_VEC_ELT (ftparms
, i
);
24689 tree olddecl
= TREE_VALUE (oldelt
);
24690 tree newdecl
= rewrite_template_parm (olddecl
, index
, level
,
24691 tsubst_args
, complain
);
24692 tree newdef
= tsubst_template_arg (TREE_PURPOSE (oldelt
),
24693 tsubst_args
, complain
, ctor
);
24694 tree list
= build_tree_list (newdef
, newdecl
);
24695 TEMPLATE_PARM_CONSTRAINTS (list
)
24696 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt
),
24697 tsubst_args
, complain
, ctor
);
24698 TREE_VEC_ELT (new_vec
, index
) = list
;
24699 TMPL_ARG (tsubst_args
, depth
, i
) = template_parm_to_arg (list
);
24702 /* Now we have a final set of template parms to substitute into the
24703 function signature. */
24704 targs
= template_parms_to_args (tparms
);
24705 fparms
= tsubst_arg_types (fparms
, tsubst_args
, NULL_TREE
,
24707 fargs
= tsubst (fargs
, tsubst_args
, complain
, ctor
);
24709 ci
= tsubst_constraint_info (ci
, tsubst_args
, complain
, ctor
);
24711 current_template_parms
= save_parms
;
24712 --processing_template_decl
;
24715 tree fntype
= build_function_type (type
, fparms
);
24716 tree ded_fn
= build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor
),
24718 dguide_name (type
), fntype
);
24719 DECL_ARGUMENTS (ded_fn
) = fargs
;
24720 tree ded_tmpl
= build_template_decl (ded_fn
, tparms
, /*member*/false);
24721 DECL_TEMPLATE_RESULT (ded_tmpl
) = ded_fn
;
24722 TREE_TYPE (ded_tmpl
) = TREE_TYPE (ded_fn
);
24723 DECL_TEMPLATE_INFO (ded_fn
) = build_template_info (ded_tmpl
, targs
);
24725 set_constraints (ded_tmpl
, ci
);
24730 /* Deduce template arguments for the class template placeholder PTYPE for
24731 template TMPL based on the initializer INIT, and return the resulting
24735 do_class_deduction (tree ptype
, tree tmpl
, tree init
, tsubst_flags_t complain
)
24737 if (!DECL_CLASS_TEMPLATE_P (tmpl
))
24739 /* We should have handled this in the caller. */
24740 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
24742 if (complain
& tf_error
)
24743 error ("non-class template %qT used without template arguments", tmpl
);
24744 return error_mark_node
;
24747 tree type
= TREE_TYPE (tmpl
);
24749 vec
<tree
,va_gc
> *args
;
24750 if (TREE_CODE (init
) == TREE_LIST
)
24751 args
= make_tree_vector_from_list (init
);
24752 else if (BRACE_ENCLOSED_INITIALIZER_P (init
))
24753 args
= make_tree_vector_from_ctor (init
);
24755 args
= make_tree_vector_single (init
);
24757 if (args
->length() == 1)
24759 /* First try to deduce directly, since we don't have implicitly-declared
24760 constructors yet. */
24761 tree parms
= build_tree_list (NULL_TREE
, type
);
24762 tree tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
24763 tree targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
24764 int err
= type_unification_real (tparms
, targs
, parms
, &(*args
)[0],
24765 1, /*subr*/false, DEDUCE_CALL
,
24766 LOOKUP_NORMAL
, NULL
, /*explain*/false);
24768 return tsubst (type
, targs
, complain
, tmpl
);
24771 tree dname
= dguide_name (tmpl
);
24772 tree cands
= lookup_qualified_name (CP_DECL_CONTEXT (tmpl
), dname
,
24773 /*type*/false, /*complain*/false,
24775 if (cands
== error_mark_node
)
24778 tree outer_args
= NULL_TREE
;
24779 if (DECL_CLASS_SCOPE_P (tmpl
)
24780 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl
)))
24782 outer_args
= CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl
));
24783 type
= TREE_TYPE (most_general_template (tmpl
));
24786 if (CLASSTYPE_METHOD_VEC (type
))
24787 // FIXME cache artificial deduction guides
24788 for (tree fns
= CLASSTYPE_CONSTRUCTORS (type
); fns
; fns
= OVL_NEXT (fns
))
24790 tree fn
= OVL_CURRENT (fns
);
24791 tree guide
= build_deduction_guide (fn
, outer_args
, complain
);
24792 cands
= ovl_cons (guide
, cands
);
24795 if (cands
== NULL_TREE
)
24797 error ("cannot deduce template arguments for %qT, as it has "
24798 "no deduction guides or user-declared constructors", type
);
24799 return error_mark_node
;
24802 ++cp_unevaluated_operand
;
24803 tree t
= build_new_function_call (cands
, &args
, /*koenig*/false,
24804 complain
|tf_decltype
);
24805 --cp_unevaluated_operand
;
24806 release_tree_vector (args
);
24808 return TREE_TYPE (t
);
24811 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24812 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24815 do_auto_deduction (tree type
, tree init
, tree auto_node
)
24817 return do_auto_deduction (type
, init
, auto_node
,
24818 tf_warning_or_error
,
24822 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24823 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24824 The CONTEXT determines the context in which auto deduction is performed
24825 and is used to control error diagnostics.
24827 For partial-concept-ids, extra args may be appended to the list of deduced
24828 template arguments prior to determining constraint satisfaction. */
24831 do_auto_deduction (tree type
, tree init
, tree auto_node
,
24832 tsubst_flags_t complain
, auto_deduction_context context
,
24837 if (init
== error_mark_node
)
24838 return error_mark_node
;
24840 if (type_dependent_expression_p (init
)
24841 && context
!= adc_unify
)
24842 /* Defining a subset of type-dependent expressions that we can deduce
24843 from ahead of time isn't worth the trouble. */
24846 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
24847 /* C++17 class template argument deduction. */
24848 return do_class_deduction (type
, tmpl
, init
, complain
);
24850 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24851 with either a new invented type template parameter U or, if the
24852 initializer is a braced-init-list (8.5.4), with
24853 std::initializer_list<U>. */
24854 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
24856 if (!DIRECT_LIST_INIT_P (init
))
24857 type
= listify_autos (type
, auto_node
);
24858 else if (CONSTRUCTOR_NELTS (init
) == 1)
24859 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
24862 if (complain
& tf_warning_or_error
)
24864 if (permerror (input_location
, "direct-list-initialization of "
24865 "%<auto%> requires exactly one element"))
24866 inform (input_location
,
24867 "for deduction to %<std::initializer_list%>, use copy-"
24868 "list-initialization (i.e. add %<=%> before the %<{%>)");
24870 type
= listify_autos (type
, auto_node
);
24874 if (type
== error_mark_node
)
24875 return error_mark_node
;
24877 init
= resolve_nondeduced_context (init
, complain
);
24879 if (context
== adc_decomp_type
24880 && auto_node
== type
24881 && init
!= error_mark_node
24882 && TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
)
24883 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
24884 and initializer has array type, deduce cv-qualified array type. */
24885 return cp_build_qualified_type_real (TREE_TYPE (init
), TYPE_QUALS (type
),
24887 else if (AUTO_IS_DECLTYPE (auto_node
))
24889 bool id
= (DECL_P (init
)
24890 || ((TREE_CODE (init
) == COMPONENT_REF
24891 || TREE_CODE (init
) == SCOPE_REF
)
24892 && !REF_PARENTHESIZED_P (init
)));
24893 targs
= make_tree_vec (1);
24894 TREE_VEC_ELT (targs
, 0)
24895 = finish_decltype_type (init
, id
, tf_warning_or_error
);
24896 if (type
!= auto_node
)
24898 if (complain
& tf_error
)
24899 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
24900 return error_mark_node
;
24905 tree parms
= build_tree_list (NULL_TREE
, type
);
24909 tparms
= extract_autos (type
);
24912 tparms
= make_tree_vec (1);
24913 TREE_VEC_ELT (tparms
, 0)
24914 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
24917 targs
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
24918 int val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
24919 DEDUCE_CALL
, LOOKUP_NORMAL
,
24920 NULL
, /*explain_p=*/false);
24923 if (processing_template_decl
)
24924 /* Try again at instantiation time. */
24926 if (type
&& type
!= error_mark_node
24927 && (complain
& tf_error
))
24928 /* If type is error_mark_node a diagnostic must have been
24929 emitted by now. Also, having a mention to '<type error>'
24930 in the diagnostic is not really useful to the user. */
24932 if (cfun
&& auto_node
== current_function_auto_return_pattern
24933 && LAMBDA_FUNCTION_P (current_function_decl
))
24934 error ("unable to deduce lambda return type from %qE", init
);
24936 error ("unable to deduce %qT from %qE", type
, init
);
24937 type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
24938 DEDUCE_CALL
, LOOKUP_NORMAL
,
24939 NULL
, /*explain_p=*/true);
24941 return error_mark_node
;
24945 /* Check any placeholder constraints against the deduced type. */
24946 if (flag_concepts
&& !processing_template_decl
)
24947 if (tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (auto_node
))
24949 /* Use the deduced type to check the associated constraints. If we
24950 have a partial-concept-id, rebuild the argument list so that
24951 we check using the extra arguments. */
24952 gcc_assert (TREE_CODE (constr
) == CHECK_CONSTR
);
24953 tree cargs
= CHECK_CONSTR_ARGS (constr
);
24954 if (TREE_VEC_LENGTH (cargs
) > 1)
24956 cargs
= copy_node (cargs
);
24957 TREE_VEC_ELT (cargs
, 0) = TREE_VEC_ELT (targs
, 0);
24961 if (!constraints_satisfied_p (constr
, cargs
))
24963 if (complain
& tf_warning_or_error
)
24967 case adc_unspecified
:
24969 error("placeholder constraints not satisfied");
24971 case adc_variable_type
:
24972 case adc_decomp_type
:
24973 error ("deduced initializer does not satisfy "
24974 "placeholder constraints");
24976 case adc_return_type
:
24977 error ("deduced return type does not satisfy "
24978 "placeholder constraints");
24980 case adc_requirement
:
24981 error ("deduced expression type does not satisfy "
24982 "placeholder constraints");
24985 diagnose_constraints (input_location
, constr
, targs
);
24987 return error_mark_node
;
24991 if (processing_template_decl
&& context
!= adc_unify
)
24992 outer_targs
= current_template_args ();
24993 targs
= add_to_template_args (outer_targs
, targs
);
24994 return tsubst (type
, targs
, complain
, NULL_TREE
);
24997 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25001 splice_late_return_type (tree type
, tree late_return_type
)
25003 if (is_auto (type
))
25005 if (late_return_type
)
25006 return late_return_type
;
25008 tree idx
= get_template_parm_index (type
);
25009 if (TEMPLATE_PARM_LEVEL (idx
) <= processing_template_decl
)
25010 /* In an abbreviated function template we didn't know we were dealing
25011 with a function template when we saw the auto return type, so update
25012 it to have the correct level. */
25013 return make_auto_1 (TYPE_IDENTIFIER (type
), true);
25018 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25019 'decltype(auto)' or a deduced class template. */
25022 is_auto (const_tree type
)
25024 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
25025 && (TYPE_IDENTIFIER (type
) == auto_identifier
25026 || TYPE_IDENTIFIER (type
) == decltype_auto_identifier
25027 || CLASS_PLACEHOLDER_TEMPLATE (type
)))
25033 /* for_each_template_parm callback for type_uses_auto. */
25036 is_auto_r (tree tp
, void */
*data*/
)
25038 return is_auto_or_concept (tp
);
25041 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25042 a use of `auto'. Returns NULL_TREE otherwise. */
25045 type_uses_auto (tree type
)
25047 if (type
== NULL_TREE
)
25049 else if (flag_concepts
)
25051 /* The Concepts TS allows multiple autos in one type-specifier; just
25052 return the first one we find, do_auto_deduction will collect all of
25054 if (uses_template_parms (type
))
25055 return for_each_template_parm (type
, is_auto_r
, /*data*/NULL
,
25056 /*visited*/NULL
, /*nondeduced*/true);
25061 return find_type_usage (type
, is_auto
);
25064 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25065 'decltype(auto)' or a concept. */
25068 is_auto_or_concept (const_tree type
)
25070 return is_auto (type
); // or concept
25073 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25074 a concept identifier) iff TYPE contains a use of a generic type. Returns
25075 NULL_TREE otherwise. */
25078 type_uses_auto_or_concept (tree type
)
25080 return find_type_usage (type
, is_auto_or_concept
);
25084 /* For a given template T, return the vector of typedefs referenced
25085 in T for which access check is needed at T instantiation time.
25086 T is either a FUNCTION_DECL or a RECORD_TYPE.
25087 Those typedefs were added to T by the function
25088 append_type_to_template_for_access_check. */
25090 vec
<qualified_typedef_usage_t
, va_gc
> *
25091 get_types_needing_access_check (tree t
)
25094 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
25096 if (!t
|| t
== error_mark_node
)
25099 if (!(ti
= get_template_info (t
)))
25102 if (CLASS_TYPE_P (t
)
25103 || TREE_CODE (t
) == FUNCTION_DECL
)
25105 if (!TI_TEMPLATE (ti
))
25108 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
25114 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25115 tied to T. That list of typedefs will be access checked at
25116 T instantiation time.
25117 T is either a FUNCTION_DECL or a RECORD_TYPE.
25118 TYPE_DECL is a TYPE_DECL node representing a typedef.
25119 SCOPE is the scope through which TYPE_DECL is accessed.
25120 LOCATION is the location of the usage point of TYPE_DECL.
25122 This function is a subroutine of
25123 append_type_to_template_for_access_check. */
25126 append_type_to_template_for_access_check_1 (tree t
,
25129 location_t location
)
25131 qualified_typedef_usage_t typedef_usage
;
25134 if (!t
|| t
== error_mark_node
)
25137 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
25138 || CLASS_TYPE_P (t
))
25140 && TREE_CODE (type_decl
) == TYPE_DECL
25143 if (!(ti
= get_template_info (t
)))
25146 gcc_assert (TI_TEMPLATE (ti
));
25148 typedef_usage
.typedef_decl
= type_decl
;
25149 typedef_usage
.context
= scope
;
25150 typedef_usage
.locus
= location
;
25152 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
25155 /* Append TYPE_DECL to the template TEMPL.
25156 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25157 At TEMPL instanciation time, TYPE_DECL will be checked to see
25158 if it can be accessed through SCOPE.
25159 LOCATION is the location of the usage point of TYPE_DECL.
25161 e.g. consider the following code snippet:
25168 template<class U> struct S
25170 C::myint mi; // <-- usage point of the typedef C::myint
25175 At S<char> instantiation time, we need to check the access of C::myint
25176 In other words, we need to check the access of the myint typedef through
25177 the C scope. For that purpose, this function will add the myint typedef
25178 and the scope C through which its being accessed to a list of typedefs
25179 tied to the template S. That list will be walked at template instantiation
25180 time and access check performed on each typedefs it contains.
25181 Note that this particular code snippet should yield an error because
25182 myint is private to C. */
25185 append_type_to_template_for_access_check (tree templ
,
25188 location_t location
)
25190 qualified_typedef_usage_t
*iter
;
25193 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
25195 /* Make sure we don't append the type to the template twice. */
25196 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
25197 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
25200 append_type_to_template_for_access_check_1 (templ
, type_decl
,
25204 /* Convert the generic type parameters in PARM that match the types given in the
25205 range [START_IDX, END_IDX) from the current_template_parms into generic type
25209 convert_generic_types_to_packs (tree parm
, int start_idx
, int end_idx
)
25211 tree current
= current_template_parms
;
25212 int depth
= TMPL_PARMS_DEPTH (current
);
25213 current
= INNERMOST_TEMPLATE_PARMS (current
);
25214 tree replacement
= make_tree_vec (TREE_VEC_LENGTH (current
));
25216 for (int i
= 0; i
< start_idx
; ++i
)
25217 TREE_VEC_ELT (replacement
, i
)
25218 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25220 for (int i
= start_idx
; i
< end_idx
; ++i
)
25222 /* Create a distinct parameter pack type from the current parm and add it
25223 to the replacement args to tsubst below into the generic function
25226 tree o
= TREE_TYPE (TREE_VALUE
25227 (TREE_VEC_ELT (current
, i
)));
25228 tree t
= copy_type (o
);
25229 TEMPLATE_TYPE_PARM_INDEX (t
)
25230 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o
),
25232 TREE_TYPE (TEMPLATE_TYPE_DECL (t
)) = t
;
25233 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = TEMPLATE_TYPE_DECL (t
);
25234 TYPE_MAIN_VARIANT (t
) = t
;
25235 TEMPLATE_TYPE_PARAMETER_PACK (t
) = true;
25236 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
25237 TREE_VEC_ELT (replacement
, i
) = t
;
25238 TREE_VALUE (TREE_VEC_ELT (current
, i
)) = TREE_CHAIN (t
);
25241 for (int i
= end_idx
, e
= TREE_VEC_LENGTH (current
); i
< e
; ++i
)
25242 TREE_VEC_ELT (replacement
, i
)
25243 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current
, i
)));
25245 /* If there are more levels then build up the replacement with the outer
25248 replacement
= add_to_template_args (template_parms_to_args
25249 (TREE_CHAIN (current_template_parms
)),
25252 return tsubst (parm
, replacement
, tf_none
, NULL_TREE
);
25255 /* Entries in the decl_constraint hash table. */
25256 struct GTY((for_user
)) constr_entry
25262 /* Hashing function and equality for constraint entries. */
25263 struct constr_hasher
: ggc_ptr_hash
<constr_entry
>
25265 static hashval_t
hash (constr_entry
*e
)
25267 return (hashval_t
)DECL_UID (e
->decl
);
25270 static bool equal (constr_entry
*e1
, constr_entry
*e2
)
25272 return e1
->decl
== e2
->decl
;
25276 /* A mapping from declarations to constraint information. Note that
25277 both templates and their underlying declarations are mapped to the
25278 same constraint information.
25280 FIXME: This is defined in pt.c because garbage collection
25281 code is not being generated for constraint.cc. */
25283 static GTY (()) hash_table
<constr_hasher
> *decl_constraints
;
25285 /* Returns the template constraints of declaration T. If T is not
25286 constrained, return NULL_TREE. Note that T must be non-null. */
25289 get_constraints (tree t
)
25291 if (!flag_concepts
)
25294 gcc_assert (DECL_P (t
));
25295 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25296 t
= DECL_TEMPLATE_RESULT (t
);
25297 constr_entry elt
= { t
, NULL_TREE
};
25298 constr_entry
* found
= decl_constraints
->find (&elt
);
25305 /* Associate the given constraint information CI with the declaration
25306 T. If T is a template, then the constraints are associated with
25307 its underlying declaration. Don't build associations if CI is
25311 set_constraints (tree t
, tree ci
)
25315 gcc_assert (t
&& flag_concepts
);
25316 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25317 t
= DECL_TEMPLATE_RESULT (t
);
25318 gcc_assert (!get_constraints (t
));
25319 constr_entry elt
= {t
, ci
};
25320 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, INSERT
);
25321 constr_entry
* entry
= ggc_alloc
<constr_entry
> ();
25326 /* Remove the associated constraints of the declaration T. */
25329 remove_constraints (tree t
)
25331 gcc_assert (DECL_P (t
));
25332 if (TREE_CODE (t
) == TEMPLATE_DECL
)
25333 t
= DECL_TEMPLATE_RESULT (t
);
25335 constr_entry elt
= {t
, NULL_TREE
};
25336 constr_entry
** slot
= decl_constraints
->find_slot (&elt
, NO_INSERT
);
25338 decl_constraints
->clear_slot (slot
);
25341 /* Memoized satisfaction results for declarations. This
25342 maps the pair (constraint_info, arguments) to the result computed
25343 by constraints_satisfied_p. */
25345 struct GTY((for_user
)) constraint_sat_entry
25352 /* Hashing function and equality for constraint entries. */
25354 struct constraint_sat_hasher
: ggc_ptr_hash
<constraint_sat_entry
>
25356 static hashval_t
hash (constraint_sat_entry
*e
)
25358 hashval_t val
= iterative_hash_object(e
->ci
, 0);
25359 return iterative_hash_template_arg (e
->args
, val
);
25362 static bool equal (constraint_sat_entry
*e1
, constraint_sat_entry
*e2
)
25364 return e1
->ci
== e2
->ci
&& comp_template_args (e1
->args
, e2
->args
);
25368 /* Memoized satisfaction results for concept checks. */
25370 struct GTY((for_user
)) concept_spec_entry
25377 /* Hashing function and equality for constraint entries. */
25379 struct concept_spec_hasher
: ggc_ptr_hash
<concept_spec_entry
>
25381 static hashval_t
hash (concept_spec_entry
*e
)
25383 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
25386 static bool equal (concept_spec_entry
*e1
, concept_spec_entry
*e2
)
25388 ++comparing_specializations
;
25389 bool eq
= e1
->tmpl
== e2
->tmpl
&& comp_template_args (e1
->args
, e2
->args
);
25390 --comparing_specializations
;
25395 static GTY (()) hash_table
<constraint_sat_hasher
> *constraint_memos
;
25396 static GTY (()) hash_table
<concept_spec_hasher
> *concept_memos
;
25398 /* Search for a memoized satisfaction result. Returns one of the
25399 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25402 lookup_constraint_satisfaction (tree ci
, tree args
)
25404 constraint_sat_entry elt
= { ci
, args
, NULL_TREE
};
25405 constraint_sat_entry
* found
= constraint_memos
->find (&elt
);
25407 return found
->result
;
25412 /* Memoize the result of a satisfication test. Returns the saved result. */
25415 memoize_constraint_satisfaction (tree ci
, tree args
, tree result
)
25417 constraint_sat_entry elt
= {ci
, args
, result
};
25418 constraint_sat_entry
** slot
= constraint_memos
->find_slot (&elt
, INSERT
);
25419 constraint_sat_entry
* entry
= ggc_alloc
<constraint_sat_entry
> ();
25425 /* Search for a memoized satisfaction result for a concept. */
25428 lookup_concept_satisfaction (tree tmpl
, tree args
)
25430 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
25431 concept_spec_entry
* found
= concept_memos
->find (&elt
);
25433 return found
->result
;
25438 /* Memoize the result of a concept check. Returns the saved result. */
25441 memoize_concept_satisfaction (tree tmpl
, tree args
, tree result
)
25443 concept_spec_entry elt
= {tmpl
, args
, result
};
25444 concept_spec_entry
** slot
= concept_memos
->find_slot (&elt
, INSERT
);
25445 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
25451 static GTY (()) hash_table
<concept_spec_hasher
> *concept_expansions
;
25453 /* Returns a prior concept specialization. This returns the substituted
25454 and normalized constraints defined by the concept. */
25457 get_concept_expansion (tree tmpl
, tree args
)
25459 concept_spec_entry elt
= { tmpl
, args
, NULL_TREE
};
25460 concept_spec_entry
* found
= concept_expansions
->find (&elt
);
25462 return found
->result
;
25467 /* Save a concept expansion for later. */
25470 save_concept_expansion (tree tmpl
, tree args
, tree def
)
25472 concept_spec_entry elt
= {tmpl
, args
, def
};
25473 concept_spec_entry
** slot
= concept_expansions
->find_slot (&elt
, INSERT
);
25474 concept_spec_entry
* entry
= ggc_alloc
<concept_spec_entry
> ();
25481 hash_subsumption_args (tree t1
, tree t2
)
25483 gcc_assert (TREE_CODE (t1
) == CHECK_CONSTR
);
25484 gcc_assert (TREE_CODE (t2
) == CHECK_CONSTR
);
25486 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t1
), val
);
25487 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1
), val
);
25488 val
= iterative_hash_object (CHECK_CONSTR_CONCEPT (t2
), val
);
25489 val
= iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2
), val
);
25493 /* Compare the constraints of two subsumption entries. The LEFT1 and
25494 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25495 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25498 comp_subsumption_args (tree left1
, tree left2
, tree right1
, tree right2
)
25500 if (CHECK_CONSTR_CONCEPT (left1
) == CHECK_CONSTR_CONCEPT (right1
))
25501 if (CHECK_CONSTR_CONCEPT (left2
) == CHECK_CONSTR_CONCEPT (right2
))
25502 if (comp_template_args (CHECK_CONSTR_ARGS (left1
),
25503 CHECK_CONSTR_ARGS (right1
)))
25504 return comp_template_args (CHECK_CONSTR_ARGS (left2
),
25505 CHECK_CONSTR_ARGS (right2
));
25509 /* Key/value pair for learning and memoizing subsumption results. This
25510 associates a pair of check constraints (including arguments) with
25511 a boolean value indicating the result. */
25513 struct GTY((for_user
)) subsumption_entry
25520 /* Hashing function and equality for constraint entries. */
25522 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
25524 static hashval_t
hash (subsumption_entry
*e
)
25526 return hash_subsumption_args (e
->t1
, e
->t2
);
25529 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
25531 ++comparing_specializations
;
25532 bool eq
= comp_subsumption_args(e1
->t1
, e1
->t2
, e2
->t1
, e2
->t2
);
25533 --comparing_specializations
;
25538 static GTY (()) hash_table
<subsumption_hasher
> *subsumption_table
;
25540 /* Search for a previously cached subsumption result. */
25543 lookup_subsumption_result (tree t1
, tree t2
)
25545 subsumption_entry elt
= { t1
, t2
, false };
25546 subsumption_entry
* found
= subsumption_table
->find (&elt
);
25548 return &found
->result
;
25553 /* Save a subsumption result. */
25556 save_subsumption_result (tree t1
, tree t2
, bool result
)
25558 subsumption_entry elt
= {t1
, t2
, result
};
25559 subsumption_entry
** slot
= subsumption_table
->find_slot (&elt
, INSERT
);
25560 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
25566 /* Set up the hash table for constraint association. */
25569 init_constraint_processing (void)
25571 if (!flag_concepts
)
25574 decl_constraints
= hash_table
<constr_hasher
>::create_ggc(37);
25575 constraint_memos
= hash_table
<constraint_sat_hasher
>::create_ggc(37);
25576 concept_memos
= hash_table
<concept_spec_hasher
>::create_ggc(37);
25577 concept_expansions
= hash_table
<concept_spec_hasher
>::create_ggc(37);
25578 subsumption_table
= hash_table
<subsumption_hasher
>::create_ggc(37);
25581 /* Set up the hash tables for template instantiations. */
25584 init_template_processing (void)
25586 decl_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
25587 type_specializations
= hash_table
<spec_hasher
>::create_ggc (37);
25590 /* Print stats about the template hash tables for -fstats. */
25593 print_template_statistics (void)
25595 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
25596 "%f collisions\n", (long) decl_specializations
->size (),
25597 (long) decl_specializations
->elements (),
25598 decl_specializations
->collisions ());
25599 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
25600 "%f collisions\n", (long) type_specializations
->size (),
25601 (long) type_specializations
->elements (),
25602 type_specializations
->collisions ());
25605 #include "gt-cp-pt.h"