1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 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"
33 #include "pointer-set.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) (tree
, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
{
54 struct pending_template
*next
;
55 struct tinst_level
*tinst
;
58 static GTY(()) struct pending_template
*pending_templates
;
59 static GTY(()) struct pending_template
*last_pending_template
;
61 int processing_template_parmlist
;
62 static int template_header_count
;
64 static GTY(()) tree saved_trees
;
65 static vec
<int> inline_parm_levels
;
67 static GTY(()) struct tinst_level
*current_tinst_level
;
69 static GTY(()) tree saved_access_scope
;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr
;
76 /* A map from local variable declarations in the body of the template
77 presently being instantiated to the corresponding instantiated
79 static struct pointer_map_t
*local_specializations
;
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) vec
<tree
, va_gc
> *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
121 unification_kind_t
, int,
123 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
124 unification_kind_t
, int, bool, bool);
125 static int unify (tree
, tree
, tree
, tree
, int, bool);
126 static void add_pending_template (tree
);
127 static tree
reopen_tinst_level (struct tinst_level
*);
128 static tree
tsubst_initializer_list (tree
, tree
);
129 static tree
get_class_bindings (tree
, tree
, tree
, tree
);
130 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
132 static tree
coerce_innermost_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
142 vec
<deferred_access_check
, va_gc
> **,
144 static void note_template_header (int);
145 static tree
convert_nontype_argument_function (tree
, tree
);
146 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
147 static tree
convert_template_argument (tree
, tree
, tree
,
148 tsubst_flags_t
, int, tree
);
149 static int for_each_template_parm (tree
, tree_fn_t
, void*,
150 struct pointer_set_t
*, bool);
151 static tree
expand_template_argument_pack (tree
);
152 static tree
build_template_parm_index (int, int, int, tree
, tree
);
153 static bool inline_needs_template_parms (tree
);
154 static void push_inline_template_parms_recursive (tree
, int);
155 static tree
retrieve_local_specialization (tree
);
156 static void register_local_specialization (tree
, tree
);
157 static hashval_t
hash_specialization (const void *p
);
158 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
159 static int mark_template_parm (tree
, void *);
160 static int template_parm_this_level_p (tree
, void *);
161 static tree
tsubst_friend_function (tree
, tree
);
162 static tree
tsubst_friend_class (tree
, tree
);
163 static int can_complete_type_without_circularity (tree
);
164 static tree
get_bindings (tree
, tree
, tree
, bool);
165 static int template_decl_level (tree
);
166 static int check_cv_quals_for_unify (int, tree
, tree
);
167 static void template_parm_level_and_index (tree
, int*, int*);
168 static int unify_pack_expansion (tree
, tree
, tree
,
169 tree
, unification_kind_t
, bool, bool);
170 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
173 static void regenerate_decl_from_template (tree
, tree
);
174 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
175 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
176 static tree
tsubst_arg_types (tree
, tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
178 static bool check_specialization_scope (void);
179 static tree
process_partial_specialization (tree
);
180 static void set_current_access_from_decl (tree
);
181 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
183 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
184 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
186 static bool template_template_parm_bindings_ok_p (tree
, tree
);
187 static int template_args_equal (tree
, tree
);
188 static void tsubst_default_arguments (tree
, tsubst_flags_t
);
189 static tree
for_each_template_parm_r (tree
*, int *, void *);
190 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
191 static void copy_default_args_to_explicit_spec (tree
);
192 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
193 static bool dependent_template_arg_p (tree
);
194 static bool any_template_arguments_need_structural_equality_p (tree
);
195 static bool dependent_type_p_r (tree
);
196 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
197 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
200 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
201 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
203 static tree
listify (tree
);
204 static tree
listify_autos (tree
, tree
);
205 static tree
template_parm_to_arg (tree t
);
206 static tree
current_template_args (void);
207 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
208 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
216 push_access_scope (tree t
)
218 gcc_assert (VAR_OR_FUNCTION_DECL_P (t
)
219 || TREE_CODE (t
) == TYPE_DECL
);
221 if (DECL_FRIEND_CONTEXT (t
))
222 push_nested_class (DECL_FRIEND_CONTEXT (t
));
223 else if (DECL_CLASS_SCOPE_P (t
))
224 push_nested_class (DECL_CONTEXT (t
));
226 push_to_top_level ();
228 if (TREE_CODE (t
) == FUNCTION_DECL
)
230 saved_access_scope
= tree_cons
231 (NULL_TREE
, current_function_decl
, saved_access_scope
);
232 current_function_decl
= t
;
236 /* Restore the scope set up by push_access_scope. T is the node we
240 pop_access_scope (tree t
)
242 if (TREE_CODE (t
) == FUNCTION_DECL
)
244 current_function_decl
= TREE_VALUE (saved_access_scope
);
245 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
248 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
260 finish_member_template_decl (tree decl
)
262 if (decl
== error_mark_node
)
263 return error_mark_node
;
265 gcc_assert (DECL_P (decl
));
267 if (TREE_CODE (decl
) == TYPE_DECL
)
271 type
= TREE_TYPE (decl
);
272 if (type
== error_mark_node
)
273 return error_mark_node
;
274 if (MAYBE_CLASS_TYPE_P (type
)
275 && CLASSTYPE_TEMPLATE_INFO (type
)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
278 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
279 check_member_template (tmpl
);
284 else if (TREE_CODE (decl
) == FIELD_DECL
)
285 error ("data member %qD cannot be a member template", decl
);
286 else if (DECL_TEMPLATE_INFO (decl
))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
290 check_member_template (DECL_TI_TEMPLATE (decl
));
291 return DECL_TI_TEMPLATE (decl
);
297 error ("invalid member template declaration %qD", decl
);
299 return error_mark_node
;
302 /* Create a template info node. */
305 build_template_info (tree template_decl
, tree template_args
)
307 tree result
= make_node (TEMPLATE_INFO
);
308 TI_TEMPLATE (result
) = template_decl
;
309 TI_ARGS (result
) = template_args
;
313 /* Return the template info node corresponding to T, whatever T is. */
316 get_template_info (const_tree t
)
318 tree tinfo
= NULL_TREE
;
320 if (!t
|| t
== error_mark_node
)
323 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
324 tinfo
= DECL_TEMPLATE_INFO (t
);
326 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
329 if (OVERLOAD_TYPE_P (t
))
330 tinfo
= TYPE_TEMPLATE_INFO (t
);
331 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
332 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
337 /* Returns the template nesting level of the indicated class TYPE.
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
356 template_class_depth (tree type
)
361 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
362 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
363 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
365 tree tinfo
= get_template_info (type
);
367 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
379 inline_needs_template_parms (tree decl
)
381 if (! DECL_TEMPLATE_INFO (decl
))
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
385 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
394 push_inline_template_parms_recursive (tree parmlist
, int levels
)
396 tree parms
= TREE_VALUE (parmlist
);
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
402 ++processing_template_decl
;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl
),
405 parms
, current_template_parms
);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
408 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
410 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
412 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
414 if (parm
== error_mark_node
)
417 gcc_assert (DECL_P (parm
));
419 switch (TREE_CODE (parm
))
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
432 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
433 CONST_DECL
, DECL_NAME (parm
),
435 DECL_ARTIFICIAL (decl
) = 1;
436 TREE_CONSTANT (decl
) = 1;
437 TREE_READONLY (decl
) = 1;
438 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
439 SET_DECL_TEMPLATE_PARM_P (decl
);
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
454 maybe_begin_member_template_processing (tree decl
)
459 if (inline_needs_template_parms (decl
))
461 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
462 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
464 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
467 parms
= TREE_CHAIN (parms
);
470 push_inline_template_parms_recursive (parms
, levels
);
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 inline_parm_levels
.safe_push (levels
);
478 /* Undo the effects of maybe_begin_member_template_processing. */
481 maybe_end_member_template_processing (void)
486 if (inline_parm_levels
.length () == 0)
489 last
= inline_parm_levels
.pop ();
490 for (i
= 0; i
< last
; ++i
)
492 --processing_template_decl
;
493 current_template_parms
= TREE_CHAIN (current_template_parms
);
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
502 add_to_template_args (tree args
, tree extra_args
)
509 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
512 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
513 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
515 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
516 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
518 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
519 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
532 add_outermost_template_args (tree args
, tree extra_args
)
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
542 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
548 new_args
= add_to_template_args (args
, extra_args
);
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
556 /* Return the N levels of innermost template arguments from the ARGS. */
559 get_innermost_template_args (tree args
, int n
)
567 /* If N is 1, just return the innermost set of template arguments. */
569 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
571 /* If we're not removing anything, just return the arguments we were
573 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
574 gcc_assert (extra_levels
>= 0);
575 if (extra_levels
== 0)
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args
= make_tree_vec (n
);
580 for (i
= 1; i
<= n
; ++i
)
581 SET_TMPL_ARGS_LEVEL (new_args
, i
,
582 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
591 strip_innermost_template_args (tree args
, int extra_levels
)
594 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
599 /* If N is 1, just return the outermost set of template arguments. */
601 return TMPL_ARGS_LEVEL (args
, 1);
603 /* If we're not removing anything, just return the arguments we were
605 gcc_assert (extra_levels
>= 0);
606 if (extra_levels
== 0)
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args
= make_tree_vec (n
);
611 for (i
= 1; i
<= n
; ++i
)
612 SET_TMPL_ARGS_LEVEL (new_args
, i
,
613 TMPL_ARGS_LEVEL (args
, i
));
618 /* We've got a template header coming up; push to a new level for storing
622 begin_template_parm_list (void)
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms
, NULL
);
639 ++processing_template_decl
;
640 ++processing_template_parmlist
;
641 note_template_header (0);
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
649 check_specialization_scope (void)
651 tree scope
= current_scope ();
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
662 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
664 error ("explicit specialization in non-namespace scope %qD", scope
);
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms
)
678 error ("enclosing class templates are not explicitly specialized");
685 /* We've just seen template <>. */
688 begin_specialization (void)
690 begin_scope (sk_template_spec
, NULL
);
691 note_template_header (1);
692 return check_specialization_scope ();
695 /* Called at then end of processing a declaration preceded by
699 end_specialization (void)
702 reset_specialization ();
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
709 reset_specialization (void)
711 processing_specialization
= 0;
712 template_header_count
= 0;
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
719 note_template_header (int specialization
)
721 processing_specialization
= specialization
;
722 template_header_count
++;
725 /* We're beginning an explicit instantiation. */
728 begin_explicit_instantiation (void)
730 gcc_assert (!processing_explicit_instantiation
);
731 processing_explicit_instantiation
= true;
736 end_explicit_instantiation (void)
738 gcc_assert (processing_explicit_instantiation
);
739 processing_explicit_instantiation
= false;
742 /* An explicit specialization or partial specialization of TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
748 check_specialization_namespace (tree tmpl
)
750 tree tpl_ns
= decl_namespace_context (tmpl
);
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
761 if (current_scope() != DECL_CONTEXT (tmpl
)
762 && !at_namespace_scope_p ())
764 error ("specialization of %qD must appear at namespace scope", tmpl
);
767 if (is_associated_namespace (current_namespace
, tpl_ns
))
768 /* Same or super-using namespace. */
772 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
773 permerror (input_location
, " from definition of %q+#D", tmpl
);
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
782 check_explicit_instantiation_namespace (tree spec
)
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns
= decl_namespace_context (spec
);
789 if (!is_ancestor (current_namespace
, ns
))
790 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec
, current_namespace
, ns
);
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
799 maybe_process_partial_specialization (tree type
)
803 if (type
== error_mark_node
)
804 return error_mark_node
;
806 /* A lambda that appears in specialization context is not itself a
808 if (CLASS_TYPE_P (type
) && CLASSTYPE_LAMBDA_EXPR (type
))
811 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
813 error ("name of class shadows template template parameter %qD",
815 return error_mark_node
;
818 context
= TYPE_CONTEXT (type
);
820 if (TYPE_ALIAS_P (type
))
822 if (TYPE_TEMPLATE_INFO (type
)
823 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
824 error ("specialization of alias template %qD",
825 TYPE_TI_TEMPLATE (type
));
827 error ("explicit specialization of non-template %qT", type
);
828 return error_mark_node
;
830 else if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
832 /* This is for ordinary explicit specialization and partial
833 specialization of a template class such as:
835 template <> class C<int>;
839 template <class T> class C<T*>;
841 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
843 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
844 && !COMPLETE_TYPE_P (type
))
846 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
847 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
848 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
849 if (processing_template_decl
)
851 if (push_template_decl (TYPE_MAIN_DECL (type
))
853 return error_mark_node
;
856 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
857 error ("specialization of %qT after instantiation", type
);
858 else if (errorcount
&& !processing_specialization
859 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
)
860 && !uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
861 /* Trying to define a specialization either without a template<> header
862 or in an inappropriate place. We've already given an error, so just
863 bail now so we don't actually define the specialization. */
864 return error_mark_node
;
866 else if (CLASS_TYPE_P (type
)
867 && !CLASSTYPE_USE_TEMPLATE (type
)
868 && CLASSTYPE_TEMPLATE_INFO (type
)
869 && context
&& CLASS_TYPE_P (context
)
870 && CLASSTYPE_TEMPLATE_INFO (context
))
872 /* This is for an explicit specialization of member class
873 template according to [temp.expl.spec/18]:
875 template <> template <class U> class C<int>::D;
877 The context `C<int>' must be an implicit instantiation.
878 Otherwise this is just a member class template declared
881 template <> class C<int> { template <class U> class D; };
882 template <> template <class U> class C<int>::D;
884 In the first case, `C<int>::D' is a specialization of `C<T>::D'
885 while in the second case, `C<int>::D' is a primary template
886 and `C<T>::D' may not exist. */
888 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
889 && !COMPLETE_TYPE_P (type
))
892 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
894 if (current_namespace
895 != decl_namespace_context (tmpl
))
897 permerror (input_location
, "specializing %q#T in different namespace", type
);
898 permerror (input_location
, " from definition of %q+#D", tmpl
);
901 /* Check for invalid specialization after instantiation:
903 template <> template <> class C<int>::D<int>;
904 template <> template <class U> class C<int>::D; */
906 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
907 t
; t
= TREE_CHAIN (t
))
909 tree inst
= TREE_VALUE (t
);
910 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
912 /* We already have a full specialization of this partial
913 instantiation. Reassign it to the new member
914 specialization template. */
919 elt
.tmpl
= most_general_template (tmpl
);
920 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
923 htab_remove_elt (type_specializations
, &elt
);
926 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
928 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
929 entry
= ggc_alloc_spec_entry ();
933 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
934 /* But if we've had an implicit instantiation, that's a
935 problem ([temp.expl.spec]/6). */
936 error ("specialization %qT after instantiation %qT",
940 /* Mark TYPE as a specialization. And as a result, we only
941 have one level of template argument for the innermost
943 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
944 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)) = input_location
;
945 CLASSTYPE_TI_ARGS (type
)
946 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
949 else if (processing_specialization
)
951 /* Someday C++0x may allow for enum template specialization. */
952 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
953 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
954 pedwarn (input_location
, OPT_Wpedantic
, "template specialization "
955 "of %qD not allowed by ISO C++", type
);
958 error ("explicit specialization of non-template %qT", type
);
959 return error_mark_node
;
966 /* Returns nonzero if we can optimize the retrieval of specializations
967 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
968 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
971 optimize_specialization_lookup_p (tree tmpl
)
973 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
974 && DECL_CLASS_SCOPE_P (tmpl
)
975 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
977 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
978 /* The optimized lookup depends on the fact that the
979 template arguments for the member function template apply
980 purely to the containing class, which is not true if the
981 containing class is an explicit or partial
983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
984 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
985 && !DECL_CONV_FN_P (tmpl
)
986 /* It is possible to have a template that is not a member
987 template and is not a member of a template class:
989 template <typename T>
990 struct S { friend A::f(); };
992 Here, the friend function is a template, but the context does
993 not have template information. The optimized lookup relies
994 on having ARGS be the template arguments for both the class
995 and the function template. */
996 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
999 /* Retrieve the specialization (in the sense of [temp.spec] - a
1000 specialization is either an instantiation or an explicit
1001 specialization) of TMPL for the given template ARGS. If there is
1002 no such specialization, return NULL_TREE. The ARGS are a vector of
1003 arguments, or a vector of vectors of arguments, in the case of
1004 templates with more than one level of parameters.
1006 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1007 then we search for a partial specialization matching ARGS. This
1008 parameter is ignored if TMPL is not a class template.
1010 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1011 result is a NONTYPE_ARGUMENT_PACK. */
1014 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1016 if (tmpl
== NULL_TREE
)
1019 if (args
== error_mark_node
)
1022 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
1023 || TREE_CODE (tmpl
) == FIELD_DECL
);
1025 /* There should be as many levels of arguments as there are
1026 levels of parameters. */
1027 gcc_assert (TMPL_ARGS_DEPTH (args
)
1028 == (TREE_CODE (tmpl
) == TEMPLATE_DECL
1029 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
))
1030 : template_class_depth (DECL_CONTEXT (tmpl
))));
1032 if (optimize_specialization_lookup_p (tmpl
))
1034 tree class_template
;
1035 tree class_specialization
;
1036 vec
<tree
, va_gc
> *methods
;
1040 /* The template arguments actually apply to the containing
1041 class. Find the class specialization with those
1043 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1044 class_specialization
1045 = retrieve_specialization (class_template
, args
, 0);
1046 if (!class_specialization
)
1048 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1049 for the specialization. */
1050 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1053 /* Iterate through the methods with the indicated name, looking
1054 for the one that has an instance of TMPL. */
1055 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1056 for (fns
= (*methods
)[idx
]; fns
; fns
= OVL_NEXT (fns
))
1058 tree fn
= OVL_CURRENT (fns
);
1059 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1060 /* using-declarations can add base methods to the method vec,
1061 and we don't want those here. */
1062 && DECL_CONTEXT (fn
) == class_specialization
)
1071 htab_t specializations
;
1075 elt
.spec
= NULL_TREE
;
1077 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1078 specializations
= type_specializations
;
1080 specializations
= decl_specializations
;
1083 hash
= hash_specialization (&elt
);
1084 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1092 /* Like retrieve_specialization, but for local declarations. */
1095 retrieve_local_specialization (tree tmpl
)
1099 if (local_specializations
== NULL
)
1102 slot
= pointer_map_contains (local_specializations
, tmpl
);
1103 return slot
? (tree
) *slot
: NULL_TREE
;
1106 /* Returns nonzero iff DECL is a specialization of TMPL. */
1109 is_specialization_of (tree decl
, tree tmpl
)
1113 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1117 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1123 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1125 for (t
= TREE_TYPE (decl
);
1127 t
= CLASSTYPE_USE_TEMPLATE (t
)
1128 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1129 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1136 /* Returns nonzero iff DECL is a specialization of friend declaration
1137 FRIEND_DECL according to [temp.friend]. */
1140 is_specialization_of_friend (tree decl
, tree friend_decl
)
1142 bool need_template
= true;
1145 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1146 || TREE_CODE (decl
) == TYPE_DECL
);
1148 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1149 of a template class, we want to check if DECL is a specialization
1151 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1152 && DECL_TEMPLATE_INFO (friend_decl
)
1153 && !DECL_USE_TEMPLATE (friend_decl
))
1155 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1156 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1157 need_template
= false;
1159 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1160 && !PRIMARY_TEMPLATE_P (friend_decl
))
1161 need_template
= false;
1163 /* There is nothing to do if this is not a template friend. */
1164 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1167 if (is_specialization_of (decl
, friend_decl
))
1171 A member of a class template may be declared to be a friend of a
1172 non-template class. In this case, the corresponding member of
1173 every specialization of the class template is a friend of the
1174 class granting friendship.
1176 For example, given a template friend declaration
1178 template <class T> friend void A<T>::f();
1180 the member function below is considered a friend
1182 template <> struct A<int> {
1186 For this type of template friend, TEMPLATE_DEPTH below will be
1187 nonzero. To determine if DECL is a friend of FRIEND, we first
1188 check if the enclosing class is a specialization of another. */
1190 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1192 && DECL_CLASS_SCOPE_P (decl
)
1193 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1194 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1196 /* Next, we check the members themselves. In order to handle
1197 a few tricky cases, such as when FRIEND_DECL's are
1199 template <class T> friend void A<T>::g(T t);
1200 template <class T> template <T t> friend void A<T>::h();
1204 void A<int>::g(int);
1205 template <int> void A<int>::h();
1207 we need to figure out ARGS, the template arguments from
1208 the context of DECL. This is required for template substitution
1209 of `T' in the function parameter of `g' and template parameter
1210 of `h' in the above examples. Here ARGS corresponds to `int'. */
1212 tree context
= DECL_CONTEXT (decl
);
1213 tree args
= NULL_TREE
;
1214 int current_depth
= 0;
1216 while (current_depth
< template_depth
)
1218 if (CLASSTYPE_TEMPLATE_INFO (context
))
1220 if (current_depth
== 0)
1221 args
= TYPE_TI_ARGS (context
);
1223 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1226 context
= TYPE_CONTEXT (context
);
1229 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1234 tree friend_args_type
;
1235 tree decl_args_type
;
1237 /* Make sure that both DECL and FRIEND_DECL are templates or
1239 is_template
= DECL_TEMPLATE_INFO (decl
)
1240 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1241 if (need_template
^ is_template
)
1243 else if (is_template
)
1245 /* If both are templates, check template parameter list. */
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1249 if (!comp_template_parms
1250 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1254 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1257 decl_type
= TREE_TYPE (decl
);
1259 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1260 tf_none
, NULL_TREE
);
1261 if (friend_type
== error_mark_node
)
1264 /* Check if return types match. */
1265 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1268 /* Check if function parameter types match, ignoring the
1269 `this' parameter. */
1270 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1271 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1272 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1273 friend_args_type
= TREE_CHAIN (friend_args_type
);
1274 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1275 decl_args_type
= TREE_CHAIN (decl_args_type
);
1277 return compparms (decl_args_type
, friend_args_type
);
1281 /* DECL is a TYPE_DECL */
1283 tree decl_type
= TREE_TYPE (decl
);
1285 /* Make sure that both DECL and FRIEND_DECL are templates or
1288 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1289 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1291 if (need_template
^ is_template
)
1293 else if (is_template
)
1296 /* If both are templates, check the name of the two
1297 TEMPLATE_DECL's first because is_friend didn't. */
1298 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1299 != DECL_NAME (friend_decl
))
1302 /* Now check template parameter list. */
1304 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1306 return comp_template_parms
1307 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1311 return (DECL_NAME (decl
)
1312 == DECL_NAME (friend_decl
));
1318 /* Register the specialization SPEC as a specialization of TMPL with
1319 the indicated ARGS. IS_FRIEND indicates whether the specialization
1320 is actually just a friend declaration. Returns SPEC, or an
1321 equivalent prior declaration, if available.
1323 We also store instantiations of field packs in the hash table, even
1324 though they are not themselves templates, to make lookup easier. */
1327 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1334 gcc_assert ((TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
))
1335 || (TREE_CODE (tmpl
) == FIELD_DECL
1336 && TREE_CODE (spec
) == NONTYPE_ARGUMENT_PACK
));
1338 if (TREE_CODE (spec
) == FUNCTION_DECL
1339 && uses_template_parms (DECL_TI_ARGS (spec
)))
1340 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1341 register it; we want the corresponding TEMPLATE_DECL instead.
1342 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1343 the more obvious `uses_template_parms (spec)' to avoid problems
1344 with default function arguments. In particular, given
1345 something like this:
1347 template <class T> void f(T t1, T t = T())
1349 the default argument expression is not substituted for in an
1350 instantiation unless and until it is actually needed. */
1353 if (optimize_specialization_lookup_p (tmpl
))
1354 /* We don't put these specializations in the hash table, but we might
1355 want to give an error about a mismatch. */
1356 fn
= retrieve_specialization (tmpl
, args
, 0);
1364 hash
= hash_specialization (&elt
);
1367 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1369 fn
= ((spec_entry
*) *slot
)->spec
;
1374 /* We can sometimes try to re-register a specialization that we've
1375 already got. In particular, regenerate_decl_from_template calls
1376 duplicate_decls which will update the specialization list. But,
1377 we'll still get called again here anyhow. It's more convenient
1378 to simply allow this than to try to prevent it. */
1381 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1383 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1385 if (DECL_ODR_USED (fn
)
1386 || DECL_EXPLICIT_INSTANTIATION (fn
))
1388 error ("specialization of %qD after instantiation",
1390 return error_mark_node
;
1395 /* This situation should occur only if the first
1396 specialization is an implicit instantiation, the
1397 second is an explicit specialization, and the
1398 implicit instantiation has not yet been used. That
1399 situation can occur if we have implicitly
1400 instantiated a member function and then specialized
1403 We can also wind up here if a friend declaration that
1404 looked like an instantiation turns out to be a
1407 template <class T> void foo(T);
1408 class S { friend void foo<>(int) };
1409 template <> void foo(int);
1411 We transform the existing DECL in place so that any
1412 pointers to it become pointers to the updated
1415 If there was a definition for the template, but not
1416 for the specialization, we want this to look as if
1417 there were no definition, and vice versa. */
1418 DECL_INITIAL (fn
) = NULL_TREE
;
1419 duplicate_decls (spec
, fn
, is_friend
);
1420 /* The call to duplicate_decls will have applied
1423 An explicit specialization of a function template
1424 is inline only if it is explicitly declared to be,
1425 and independently of whether its function template
1428 to the primary function; now copy the inline bits to
1429 the various clones. */
1430 FOR_EACH_CLONE (clone
, fn
)
1432 DECL_DECLARED_INLINE_P (clone
)
1433 = DECL_DECLARED_INLINE_P (fn
);
1434 DECL_SOURCE_LOCATION (clone
)
1435 = DECL_SOURCE_LOCATION (fn
);
1437 check_specialization_namespace (tmpl
);
1442 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1444 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1445 /* Dup decl failed, but this is a new definition. Set the
1446 line number so any errors match this new
1448 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1454 return duplicate_decls (spec
, fn
, is_friend
);
1456 /* A specialization must be declared in the same namespace as the
1457 template it is specializing. */
1458 if (DECL_P (spec
) && DECL_TEMPLATE_SPECIALIZATION (spec
)
1459 && !check_specialization_namespace (tmpl
))
1460 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1462 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1464 spec_entry
*entry
= ggc_alloc_spec_entry ();
1465 gcc_assert (tmpl
&& args
&& spec
);
1468 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1469 && PRIMARY_TEMPLATE_P (tmpl
)
1470 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1471 /* TMPL is a forward declaration of a template function; keep a list
1472 of all specializations in case we need to reassign them to a friend
1473 template later in tsubst_friend_function. */
1474 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1475 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1481 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1482 TMPL and ARGS members, ignores SPEC. */
1484 int comparing_specializations
;
1487 eq_specializations (const void *p1
, const void *p2
)
1489 const spec_entry
*e1
= (const spec_entry
*)p1
;
1490 const spec_entry
*e2
= (const spec_entry
*)p2
;
1493 ++comparing_specializations
;
1494 equal
= (e1
->tmpl
== e2
->tmpl
1495 && comp_template_args (e1
->args
, e2
->args
));
1496 --comparing_specializations
;
1501 /* Returns a hash for a template TMPL and template arguments ARGS. */
1504 hash_tmpl_and_args (tree tmpl
, tree args
)
1506 hashval_t val
= DECL_UID (tmpl
);
1507 return iterative_hash_template_arg (args
, val
);
1510 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1514 hash_specialization (const void *p
)
1516 const spec_entry
*e
= (const spec_entry
*)p
;
1517 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1520 /* Recursively calculate a hash value for a template argument ARG, for use
1521 in the hash tables of template specializations. */
1524 iterative_hash_template_arg (tree arg
, hashval_t val
)
1526 unsigned HOST_WIDE_INT i
;
1527 enum tree_code code
;
1530 if (arg
== NULL_TREE
)
1531 return iterative_hash_object (arg
, val
);
1536 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1537 /* We can get one of these when re-hashing a previous entry in the middle
1538 of substituting into a pack expansion. Just look through it. */
1539 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1541 code
= TREE_CODE (arg
);
1542 tclass
= TREE_CODE_CLASS (code
);
1544 val
= iterative_hash_object (code
, val
);
1551 case IDENTIFIER_NODE
:
1552 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1556 int i
, len
= TREE_VEC_LENGTH (arg
);
1557 for (i
= 0; i
< len
; ++i
)
1558 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1562 case TYPE_PACK_EXPANSION
:
1563 case EXPR_PACK_EXPANSION
:
1564 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1565 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1567 case TYPE_ARGUMENT_PACK
:
1568 case NONTYPE_ARGUMENT_PACK
:
1569 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1572 for (; arg
; arg
= TREE_CHAIN (arg
))
1573 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1577 for (; arg
; arg
= OVL_NEXT (arg
))
1578 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1584 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1586 val
= iterative_hash_template_arg (field
, val
);
1587 val
= iterative_hash_template_arg (value
, val
);
1593 if (!DECL_ARTIFICIAL (arg
))
1595 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1596 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1598 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1601 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1604 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1605 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1607 case TEMPLATE_PARM_INDEX
:
1608 val
= iterative_hash_template_arg
1609 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1610 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1611 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1614 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1615 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1616 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1619 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1621 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1625 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1626 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1627 val
= iterative_hash_object (code
, val
);
1628 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1631 /* A lambda can't appear in a template arg, but don't crash on
1633 gcc_assert (seen_error ());
1637 case IMPLICIT_CONV_EXPR
:
1638 case STATIC_CAST_EXPR
:
1639 case REINTERPRET_CAST_EXPR
:
1640 case CONST_CAST_EXPR
:
1641 case DYNAMIC_CAST_EXPR
:
1643 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1644 /* Now hash operands as usual. */
1654 if (TYPE_CANONICAL (arg
))
1655 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1657 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1658 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1659 /* Otherwise just compare the types during lookup. */
1662 case tcc_declaration
:
1664 return iterative_hash_expr (arg
, val
);
1667 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1669 unsigned n
= cp_tree_operand_length (arg
);
1670 for (i
= 0; i
< n
; ++i
)
1671 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1679 /* Unregister the specialization SPEC as a specialization of TMPL.
1680 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1681 if the SPEC was listed as a specialization of TMPL.
1683 Note that SPEC has been ggc_freed, so we can't look inside it. */
1686 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1691 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1692 elt
.args
= TI_ARGS (tinfo
);
1693 elt
.spec
= NULL_TREE
;
1695 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1698 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1699 gcc_assert (new_spec
!= NULL_TREE
);
1700 entry
->spec
= new_spec
;
1707 /* Like register_specialization, but for local declarations. We are
1708 registering SPEC, an instantiation of TMPL. */
1711 register_local_specialization (tree spec
, tree tmpl
)
1715 slot
= pointer_map_insert (local_specializations
, tmpl
);
1719 /* TYPE is a class type. Returns true if TYPE is an explicitly
1720 specialized class. */
1723 explicit_class_specialization_p (tree type
)
1725 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1727 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1730 /* Print the list of functions at FNS, going through all the overloads
1731 for each element of the list. Alternatively, FNS can not be a
1732 TREE_LIST, in which case it will be printed together with all the
1735 MORE and *STR should respectively be FALSE and NULL when the function
1736 is called from the outside. They are used internally on recursive
1737 calls. print_candidates manages the two parameters and leaves NULL
1738 in *STR when it ends. */
1741 print_candidates_1 (tree fns
, bool more
, const char **str
)
1744 char *spaces
= NULL
;
1746 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1747 if (TREE_CODE (fn
) == TREE_LIST
)
1749 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1750 print_candidates_1 (TREE_VALUE (fn2
),
1751 TREE_CHAIN (fn2
) || more
, str
);
1755 tree cand
= OVL_CURRENT (fn
);
1758 /* Pick the prefix string. */
1759 if (!more
&& !OVL_NEXT (fns
))
1761 inform (DECL_SOURCE_LOCATION (cand
),
1762 "candidate is: %#D", cand
);
1766 *str
= _("candidates are:");
1767 spaces
= get_spaces (*str
);
1769 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1770 *str
= spaces
? spaces
: *str
;
1780 /* Print the list of candidate FNS in an error message. FNS can also
1781 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1784 print_candidates (tree fns
)
1786 const char *str
= NULL
;
1787 print_candidates_1 (fns
, false, &str
);
1788 gcc_assert (str
== NULL
);
1791 /* Returns the template (one of the functions given by TEMPLATE_ID)
1792 which can be specialized to match the indicated DECL with the
1793 explicit template args given in TEMPLATE_ID. The DECL may be
1794 NULL_TREE if none is available. In that case, the functions in
1795 TEMPLATE_ID are non-members.
1797 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1798 specialization of a member template.
1800 The TEMPLATE_COUNT is the number of references to qualifying
1801 template classes that appeared in the name of the function. See
1802 check_explicit_specialization for a more accurate description.
1804 TSK indicates what kind of template declaration (if any) is being
1805 declared. TSK_TEMPLATE indicates that the declaration given by
1806 DECL, though a FUNCTION_DECL, has template parameters, and is
1807 therefore a template function.
1809 The template args (those explicitly specified and those deduced)
1810 are output in a newly created vector *TARGS_OUT.
1812 If it is impossible to determine the result, an error message is
1813 issued. The error_mark_node is returned to indicate failure. */
1816 determine_specialization (tree template_id
,
1819 int need_member_template
,
1825 tree explicit_targs
;
1826 tree candidates
= NULL_TREE
;
1827 /* A TREE_LIST of templates of which DECL may be a specialization.
1828 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1829 corresponding TREE_PURPOSE is the set of template arguments that,
1830 when used to instantiate the template, would produce a function
1831 with the signature of DECL. */
1832 tree templates
= NULL_TREE
;
1834 cp_binding_level
*b
;
1836 *targs_out
= NULL_TREE
;
1838 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1839 return error_mark_node
;
1841 /* We shouldn't be specializing a member template of an
1842 unspecialized class template; we already gave an error in
1843 check_specialization_scope, now avoid crashing. */
1844 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1845 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1847 gcc_assert (errorcount
);
1848 return error_mark_node
;
1851 fns
= TREE_OPERAND (template_id
, 0);
1852 explicit_targs
= TREE_OPERAND (template_id
, 1);
1854 if (fns
== error_mark_node
)
1855 return error_mark_node
;
1857 /* Check for baselinks. */
1858 if (BASELINK_P (fns
))
1859 fns
= BASELINK_FUNCTIONS (fns
);
1861 if (!is_overloaded_fn (fns
))
1863 error ("%qD is not a function template", fns
);
1864 return error_mark_node
;
1867 /* Count the number of template headers specified for this
1870 for (b
= current_binding_level
;
1871 b
->kind
== sk_template_parms
;
1875 for (; fns
; fns
= OVL_NEXT (fns
))
1877 tree fn
= OVL_CURRENT (fns
);
1879 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1881 tree decl_arg_types
;
1885 /* In case of explicit specialization, we need to check if
1886 the number of template headers appearing in the specialization
1887 is correct. This is usually done in check_explicit_specialization,
1888 but the check done there cannot be exhaustive when specializing
1889 member functions. Consider the following code:
1891 template <> void A<int>::f(int);
1892 template <> template <> void A<int>::f(int);
1894 Assuming that A<int> is not itself an explicit specialization
1895 already, the first line specializes "f" which is a non-template
1896 member function, whilst the second line specializes "f" which
1897 is a template member function. So both lines are syntactically
1898 correct, and check_explicit_specialization does not reject
1901 Here, we can do better, as we are matching the specialization
1902 against the declarations. We count the number of template
1903 headers, and we check if they match TEMPLATE_COUNT + 1
1904 (TEMPLATE_COUNT is the number of qualifying template classes,
1905 plus there must be another header for the member template
1908 Notice that if header_count is zero, this is not a
1909 specialization but rather a template instantiation, so there
1910 is no check we can perform here. */
1911 if (header_count
&& header_count
!= template_count
+ 1)
1914 /* Check that the number of template arguments at the
1915 innermost level for DECL is the same as for FN. */
1916 if (current_binding_level
->kind
== sk_template_parms
1917 && !current_binding_level
->explicit_spec_p
1918 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1919 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1920 (current_template_parms
))))
1923 /* DECL might be a specialization of FN. */
1924 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1925 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1927 /* For a non-static member function, we need to make sure
1928 that the const qualification is the same. Since
1929 get_bindings does not try to merge the "this" parameter,
1930 we must do the comparison explicitly. */
1931 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1932 && !same_type_p (TREE_VALUE (fn_arg_types
),
1933 TREE_VALUE (decl_arg_types
)))
1936 /* Skip the "this" parameter and, for constructors of
1937 classes with virtual bases, the VTT parameter. A
1938 full specialization of a constructor will have a VTT
1939 parameter, but a template never will. */
1941 = skip_artificial_parms_for (decl
, decl_arg_types
);
1943 = skip_artificial_parms_for (fn
, fn_arg_types
);
1945 /* Function templates cannot be specializations; there are
1946 no partial specializations of functions. Therefore, if
1947 the type of DECL does not match FN, there is no
1949 if (tsk
== tsk_template
)
1951 if (compparms (fn_arg_types
, decl_arg_types
))
1952 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1956 /* See whether this function might be a specialization of this
1957 template. Suppress access control because we might be trying
1958 to make this specialization a friend, and we have already done
1959 access control for the declaration of the specialization. */
1960 push_deferring_access_checks (dk_no_check
);
1961 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1962 pop_deferring_access_checks ();
1965 /* We cannot deduce template arguments that when used to
1966 specialize TMPL will produce DECL. */
1969 /* Make sure that the deduced arguments actually work. */
1970 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1971 if (insttype
== error_mark_node
)
1974 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1975 if (!compparms (fn_arg_types
, decl_arg_types
))
1978 /* Save this template, and the arguments deduced. */
1979 templates
= tree_cons (targs
, fn
, templates
);
1981 else if (need_member_template
)
1982 /* FN is an ordinary member function, and we need a
1983 specialization of a member template. */
1985 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1986 /* We can get IDENTIFIER_NODEs here in certain erroneous
1989 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1990 /* This is just an ordinary non-member function. Nothing can
1991 be a specialization of that. */
1993 else if (DECL_ARTIFICIAL (fn
))
1994 /* Cannot specialize functions that are created implicitly. */
1998 tree decl_arg_types
;
2000 /* This is an ordinary member function. However, since
2001 we're here, we can assume its enclosing class is a
2002 template class. For example,
2004 template <typename T> struct S { void f(); };
2005 template <> void S<int>::f() {}
2007 Here, S<int>::f is a non-template, but S<int> is a
2008 template class. If FN has the same type as DECL, we
2009 might be in business. */
2011 if (!DECL_TEMPLATE_INFO (fn
))
2012 /* Its enclosing class is an explicit specialization
2013 of a template class. This is not a candidate. */
2016 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
2017 TREE_TYPE (TREE_TYPE (fn
))))
2018 /* The return types differ. */
2021 /* Adjust the type of DECL in case FN is a static member. */
2022 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2023 if (DECL_STATIC_FUNCTION_P (fn
)
2024 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2025 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
2027 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2030 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2034 if (templates
&& TREE_CHAIN (templates
))
2040 It is possible for a specialization with a given function
2041 signature to be instantiated from more than one function
2042 template. In such cases, explicit specification of the
2043 template arguments must be used to uniquely identify the
2044 function template specialization being specialized.
2046 Note that here, there's no suggestion that we're supposed to
2047 determine which of the candidate templates is most
2048 specialized. However, we, also have:
2052 Partial ordering of overloaded function template
2053 declarations is used in the following contexts to select
2054 the function template to which a function template
2055 specialization refers:
2057 -- when an explicit specialization refers to a function
2060 So, we do use the partial ordering rules, at least for now.
2061 This extension can only serve to make invalid programs valid,
2062 so it's safe. And, there is strong anecdotal evidence that
2063 the committee intended the partial ordering rules to apply;
2064 the EDG front end has that behavior, and John Spicer claims
2065 that the committee simply forgot to delete the wording in
2066 [temp.expl.spec]. */
2067 tree tmpl
= most_specialized_instantiation (templates
);
2068 if (tmpl
!= error_mark_node
)
2071 TREE_CHAIN (templates
) = NULL_TREE
;
2075 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2077 error ("template-id %qD for %q+D does not match any template "
2078 "declaration", template_id
, decl
);
2079 if (header_count
&& header_count
!= template_count
+ 1)
2080 inform (input_location
, "saw %d %<template<>%>, need %d for "
2081 "specializing a member function template",
2082 header_count
, template_count
+ 1);
2083 return error_mark_node
;
2085 else if ((templates
&& TREE_CHAIN (templates
))
2086 || (candidates
&& TREE_CHAIN (candidates
))
2087 || (templates
&& candidates
))
2089 error ("ambiguous template specialization %qD for %q+D",
2091 candidates
= chainon (candidates
, templates
);
2092 print_candidates (candidates
);
2093 return error_mark_node
;
2096 /* We have one, and exactly one, match. */
2099 tree fn
= TREE_VALUE (candidates
);
2100 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2101 /* DECL is a re-declaration or partial instantiation of a template
2103 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2105 /* It was a specialization of an ordinary member function in a
2107 return DECL_TI_TEMPLATE (fn
);
2110 /* It was a specialization of a template. */
2111 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2112 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2114 *targs_out
= copy_node (targs
);
2115 SET_TMPL_ARGS_LEVEL (*targs_out
,
2116 TMPL_ARGS_DEPTH (*targs_out
),
2117 TREE_PURPOSE (templates
));
2120 *targs_out
= TREE_PURPOSE (templates
);
2121 return TREE_VALUE (templates
);
2124 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2125 but with the default argument values filled in from those in the
2129 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2132 tree new_spec_types
;
2137 if (spec_types
== void_list_node
)
2138 return void_list_node
;
2140 /* Substitute into the rest of the list. */
2142 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2143 TREE_CHAIN (tmpl_types
));
2145 /* Add the default argument for this parameter. */
2146 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2147 TREE_VALUE (spec_types
),
2151 /* DECL is an explicit specialization. Replicate default arguments
2152 from the template it specializes. (That way, code like:
2154 template <class T> void f(T = 3);
2155 template <> void f(double);
2158 works, as required.) An alternative approach would be to look up
2159 the correct default arguments at the call-site, but this approach
2160 is consistent with how implicit instantiations are handled. */
2163 copy_default_args_to_explicit_spec (tree decl
)
2168 tree new_spec_types
;
2172 tree object_type
= NULL_TREE
;
2173 tree in_charge
= NULL_TREE
;
2174 tree vtt
= NULL_TREE
;
2176 /* See if there's anything we need to do. */
2177 tmpl
= DECL_TI_TEMPLATE (decl
);
2178 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2179 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2180 if (TREE_PURPOSE (t
))
2185 old_type
= TREE_TYPE (decl
);
2186 spec_types
= TYPE_ARG_TYPES (old_type
);
2188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2190 /* Remove the this pointer, but remember the object's type for
2192 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2193 spec_types
= TREE_CHAIN (spec_types
);
2194 tmpl_types
= TREE_CHAIN (tmpl_types
);
2196 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2198 /* DECL may contain more parameters than TMPL due to the extra
2199 in-charge parameter in constructors and destructors. */
2200 in_charge
= spec_types
;
2201 spec_types
= TREE_CHAIN (spec_types
);
2203 if (DECL_HAS_VTT_PARM_P (decl
))
2206 spec_types
= TREE_CHAIN (spec_types
);
2210 /* Compute the merged default arguments. */
2212 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2214 /* Compute the new FUNCTION_TYPE. */
2218 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2223 /* Put the in-charge parameter back. */
2224 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2225 TREE_VALUE (in_charge
),
2228 new_type
= build_method_type_directly (object_type
,
2229 TREE_TYPE (old_type
),
2233 new_type
= build_function_type (TREE_TYPE (old_type
),
2235 new_type
= cp_build_type_attribute_variant (new_type
,
2236 TYPE_ATTRIBUTES (old_type
));
2237 new_type
= build_exception_variant (new_type
,
2238 TYPE_RAISES_EXCEPTIONS (old_type
));
2239 TREE_TYPE (decl
) = new_type
;
2242 /* Return the number of template headers we expect to see for a definition
2243 or specialization of CTYPE or one of its non-template members. */
2246 num_template_headers_for_class (tree ctype
)
2248 int num_templates
= 0;
2250 while (ctype
&& CLASS_TYPE_P (ctype
))
2252 /* You're supposed to have one `template <...>' for every
2253 template class, but you don't need one for a full
2254 specialization. For example:
2256 template <class T> struct S{};
2257 template <> struct S<int> { void f(); };
2258 void S<int>::f () {}
2260 is correct; there shouldn't be a `template <>' for the
2261 definition of `S<int>::f'. */
2262 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2263 /* If CTYPE does not have template information of any
2264 kind, then it is not a template, nor is it nested
2265 within a template. */
2267 if (explicit_class_specialization_p (ctype
))
2269 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2272 ctype
= TYPE_CONTEXT (ctype
);
2275 return num_templates
;
2278 /* Do a simple sanity check on the template headers that precede the
2279 variable declaration DECL. */
2282 check_template_variable (tree decl
)
2284 tree ctx
= CP_DECL_CONTEXT (decl
);
2285 int wanted
= num_template_headers_for_class (ctx
);
2286 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2287 permerror (DECL_SOURCE_LOCATION (decl
),
2288 "%qD is not a static data member of a class template", decl
);
2289 else if (template_header_count
> wanted
)
2291 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2292 "too many template headers for %D (should be %d)",
2294 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2295 inform (DECL_SOURCE_LOCATION (decl
),
2296 "members of an explicitly specialized class are defined "
2297 "without a template header");
2301 /* Check to see if the function just declared, as indicated in
2302 DECLARATOR, and in DECL, is a specialization of a function
2303 template. We may also discover that the declaration is an explicit
2304 instantiation at this point.
2306 Returns DECL, or an equivalent declaration that should be used
2307 instead if all goes well. Issues an error message if something is
2308 amiss. Returns error_mark_node if the error is not easily
2311 FLAGS is a bitmask consisting of the following flags:
2313 2: The function has a definition.
2314 4: The function is a friend.
2316 The TEMPLATE_COUNT is the number of references to qualifying
2317 template classes that appeared in the name of the function. For
2320 template <class T> struct S { void f(); };
2323 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2324 classes are not counted in the TEMPLATE_COUNT, so that in
2326 template <class T> struct S {};
2327 template <> struct S<int> { void f(); }
2328 template <> void S<int>::f();
2330 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2331 invalid; there should be no template <>.)
2333 If the function is a specialization, it is marked as such via
2334 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2335 is set up correctly, and it is added to the list of specializations
2336 for that template. */
2339 check_explicit_specialization (tree declarator
,
2344 int have_def
= flags
& 2;
2345 int is_friend
= flags
& 4;
2346 int specialization
= 0;
2347 int explicit_instantiation
= 0;
2348 int member_specialization
= 0;
2349 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2350 tree dname
= DECL_NAME (decl
);
2355 if (!processing_specialization
)
2358 tsk
= tsk_excessive_parms
;
2361 tsk
= current_tmpl_spec_kind (template_count
);
2366 if (processing_specialization
)
2369 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2371 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2374 /* This could be something like:
2376 template <class T> void f(T);
2377 class S { friend void f<>(int); } */
2381 /* This case handles bogus declarations like template <>
2382 template <class T> void f<int>(); */
2384 error ("template-id %qD in declaration of primary template",
2391 case tsk_invalid_member_spec
:
2392 /* The error has already been reported in
2393 check_specialization_scope. */
2394 return error_mark_node
;
2396 case tsk_invalid_expl_inst
:
2397 error ("template parameter list used in explicit instantiation");
2403 error ("definition provided for explicit instantiation");
2405 explicit_instantiation
= 1;
2408 case tsk_excessive_parms
:
2409 case tsk_insufficient_parms
:
2410 if (tsk
== tsk_excessive_parms
)
2411 error ("too many template parameter lists in declaration of %qD",
2413 else if (template_header_count
)
2414 error("too few template parameter lists in declaration of %qD", decl
);
2416 error("explicit specialization of %qD must be introduced by "
2417 "%<template <>%>", decl
);
2421 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2423 member_specialization
= 1;
2429 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2431 /* This case handles bogus declarations like template <>
2432 template <class T> void f<int>(); */
2434 if (uses_template_parms (declarator
))
2435 error ("function template partial specialization %qD "
2436 "is not allowed", declarator
);
2438 error ("template-id %qD in declaration of primary template",
2443 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2444 /* This is a specialization of a member template, without
2445 specialization the containing class. Something like:
2447 template <class T> struct S {
2448 template <class U> void f (U);
2450 template <> template <class U> void S<int>::f(U) {}
2452 That's a specialization -- but of the entire template. */
2460 if (specialization
|| member_specialization
)
2462 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2463 for (; t
; t
= TREE_CHAIN (t
))
2464 if (TREE_PURPOSE (t
))
2466 permerror (input_location
,
2467 "default argument specified in explicit specialization");
2472 if (specialization
|| member_specialization
|| explicit_instantiation
)
2474 tree tmpl
= NULL_TREE
;
2475 tree targs
= NULL_TREE
;
2477 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2478 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2482 gcc_assert (identifier_p (declarator
));
2487 /* If there is no class context, the explicit instantiation
2488 must be at namespace scope. */
2489 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2491 /* Find the namespace binding, using the declaration
2493 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2495 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2497 error ("%qD is not a template function", dname
);
2498 fns
= error_mark_node
;
2502 tree fn
= OVL_CURRENT (fns
);
2503 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2504 CP_DECL_CONTEXT (fn
)))
2505 error ("%qD is not declared in %qD",
2506 decl
, current_namespace
);
2510 declarator
= lookup_template_function (fns
, NULL_TREE
);
2513 if (declarator
== error_mark_node
)
2514 return error_mark_node
;
2516 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2518 if (!explicit_instantiation
)
2519 /* A specialization in class scope. This is invalid,
2520 but the error will already have been flagged by
2521 check_specialization_scope. */
2522 return error_mark_node
;
2525 /* It's not valid to write an explicit instantiation in
2528 class C { template void f(); }
2530 This case is caught by the parser. However, on
2533 template class C { void f(); };
2535 (which is invalid) we can get here. The error will be
2542 else if (ctype
!= NULL_TREE
2543 && (identifier_p (TREE_OPERAND (declarator
, 0))))
2545 /* Find the list of functions in ctype that have the same
2546 name as the declared function. */
2547 tree name
= TREE_OPERAND (declarator
, 0);
2548 tree fns
= NULL_TREE
;
2551 if (constructor_name_p (name
, ctype
))
2553 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2555 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2556 : !CLASSTYPE_DESTRUCTORS (ctype
))
2558 /* From [temp.expl.spec]:
2560 If such an explicit specialization for the member
2561 of a class template names an implicitly-declared
2562 special member function (clause _special_), the
2563 program is ill-formed.
2565 Similar language is found in [temp.explicit]. */
2566 error ("specialization of implicitly-declared special member function");
2567 return error_mark_node
;
2570 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2573 if (!DECL_CONV_FN_P (decl
))
2575 idx
= lookup_fnfields_1 (ctype
, name
);
2577 fns
= (*CLASSTYPE_METHOD_VEC (ctype
))[idx
];
2581 vec
<tree
, va_gc
> *methods
;
2584 /* For a type-conversion operator, we cannot do a
2585 name-based lookup. We might be looking for `operator
2586 int' which will be a specialization of `operator T'.
2587 So, we find *all* the conversion operators, and then
2588 select from them. */
2591 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2593 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2594 methods
->iterate (idx
, &ovl
);
2597 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2598 /* There are no more conversion functions. */
2601 /* Glue all these conversion functions together
2602 with those we already have. */
2603 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2604 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2608 if (fns
== NULL_TREE
)
2610 error ("no member function %qD declared in %qT", name
, ctype
);
2611 return error_mark_node
;
2614 TREE_OPERAND (declarator
, 0) = fns
;
2617 /* Figure out what exactly is being specialized at this point.
2618 Note that for an explicit instantiation, even one for a
2619 member function, we cannot tell apriori whether the
2620 instantiation is for a member template, or just a member
2621 function of a template class. Even if a member template is
2622 being instantiated, the member template arguments may be
2623 elided if they can be deduced from the rest of the
2625 tmpl
= determine_specialization (declarator
, decl
,
2627 member_specialization
,
2631 if (!tmpl
|| tmpl
== error_mark_node
)
2632 /* We couldn't figure out what this declaration was
2634 return error_mark_node
;
2637 tree gen_tmpl
= most_general_template (tmpl
);
2639 if (explicit_instantiation
)
2641 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2642 is done by do_decl_instantiation later. */
2644 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2645 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2647 if (arg_depth
> parm_depth
)
2649 /* If TMPL is not the most general template (for
2650 example, if TMPL is a friend template that is
2651 injected into namespace scope), then there will
2652 be too many levels of TARGS. Remove some of them
2657 new_targs
= make_tree_vec (parm_depth
);
2658 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2659 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2660 = TREE_VEC_ELT (targs
, i
);
2664 return instantiate_template (tmpl
, targs
, tf_error
);
2667 /* If we thought that the DECL was a member function, but it
2668 turns out to be specializing a static member function,
2669 make DECL a static member function as well. */
2670 if (DECL_STATIC_FUNCTION_P (tmpl
)
2671 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2672 revert_static_member_fn (decl
);
2674 /* If this is a specialization of a member template of a
2675 template class, we want to return the TEMPLATE_DECL, not
2676 the specialization of it. */
2677 if (tsk
== tsk_template
)
2679 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2680 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2681 DECL_INITIAL (result
) = NULL_TREE
;
2685 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2686 DECL_SOURCE_LOCATION (result
)
2687 = DECL_SOURCE_LOCATION (decl
);
2688 /* We want to use the argument list specified in the
2689 definition, not in the original declaration. */
2690 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2691 for (parm
= DECL_ARGUMENTS (result
); parm
;
2692 parm
= DECL_CHAIN (parm
))
2693 DECL_CONTEXT (parm
) = result
;
2695 return register_specialization (tmpl
, gen_tmpl
, targs
,
2699 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2700 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2702 /* Inherit default function arguments from the template
2703 DECL is specializing. */
2704 copy_default_args_to_explicit_spec (decl
);
2706 /* This specialization has the same protection as the
2707 template it specializes. */
2708 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2709 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2711 /* 7.1.1-1 [dcl.stc]
2713 A storage-class-specifier shall not be specified in an
2714 explicit specialization...
2716 The parser rejects these, so unless action is taken here,
2717 explicit function specializations will always appear with
2720 The action recommended by the C++ CWG in response to C++
2721 defect report 605 is to make the storage class and linkage
2722 of the explicit specialization match the templated function:
2724 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2726 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2728 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2729 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2731 /* This specialization has the same linkage and visibility as
2732 the function template it specializes. */
2733 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2734 if (! TREE_PUBLIC (decl
))
2736 DECL_INTERFACE_KNOWN (decl
) = 1;
2737 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2739 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2740 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2742 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2743 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2747 /* If DECL is a friend declaration, declared using an
2748 unqualified name, the namespace associated with DECL may
2749 have been set incorrectly. For example, in:
2751 template <typename T> void f(T);
2753 struct S { friend void f<int>(int); }
2756 we will have set the DECL_CONTEXT for the friend
2757 declaration to N, rather than to the global namespace. */
2758 if (DECL_NAMESPACE_SCOPE_P (decl
))
2759 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2761 if (is_friend
&& !have_def
)
2762 /* This is not really a declaration of a specialization.
2763 It's just the name of an instantiation. But, it's not
2764 a request for an instantiation, either. */
2765 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2766 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2767 /* This is indeed a specialization. In case of constructors
2768 and destructors, we need in-charge and not-in-charge
2769 versions in V3 ABI. */
2770 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2772 /* Register this specialization so that we can find it
2774 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2781 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2782 parameters. These are represented in the same format used for
2783 DECL_TEMPLATE_PARMS. */
2786 comp_template_parms (const_tree parms1
, const_tree parms2
)
2791 if (parms1
== parms2
)
2794 for (p1
= parms1
, p2
= parms2
;
2795 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2796 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2798 tree t1
= TREE_VALUE (p1
);
2799 tree t2
= TREE_VALUE (p2
);
2802 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2803 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2805 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2808 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2810 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2811 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2813 /* If either of the template parameters are invalid, assume
2814 they match for the sake of error recovery. */
2815 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2818 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2821 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2822 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2823 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2825 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2830 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2831 /* One set of parameters has more parameters lists than the
2838 /* Determine whether PARM is a parameter pack. */
2841 template_parameter_pack_p (const_tree parm
)
2843 /* Determine if we have a non-type template parameter pack. */
2844 if (TREE_CODE (parm
) == PARM_DECL
)
2845 return (DECL_TEMPLATE_PARM_P (parm
)
2846 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2847 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2848 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2850 /* If this is a list of template parameters, we could get a
2851 TYPE_DECL or a TEMPLATE_DECL. */
2852 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2853 parm
= TREE_TYPE (parm
);
2855 /* Otherwise it must be a type template parameter. */
2856 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2857 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2858 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2861 /* Determine if T is a function parameter pack. */
2864 function_parameter_pack_p (const_tree t
)
2866 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2867 return DECL_PACK_P (t
);
2871 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2872 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2875 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2877 if (! primary_func_tmpl_inst
2878 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2879 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2882 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2885 /* Return true iff the function parameter PARAM_DECL was expanded
2886 from the function parameter pack PACK. */
2889 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2891 if (DECL_ARTIFICIAL (param_decl
)
2892 || !function_parameter_pack_p (pack
))
2895 /* The parameter pack and its pack arguments have the same
2897 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2900 /* Determine whether ARGS describes a variadic template args list,
2901 i.e., one that is terminated by a template argument pack. */
2904 template_args_variadic_p (tree args
)
2909 if (args
== NULL_TREE
)
2912 args
= INNERMOST_TEMPLATE_ARGS (args
);
2913 nargs
= TREE_VEC_LENGTH (args
);
2918 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2920 return ARGUMENT_PACK_P (last_parm
);
2923 /* Generate a new name for the parameter pack name NAME (an
2924 IDENTIFIER_NODE) that incorporates its */
2927 make_ith_pack_parameter_name (tree name
, int i
)
2929 /* Munge the name to include the parameter index. */
2930 #define NUMBUF_LEN 128
2931 char numbuf
[NUMBUF_LEN
];
2935 if (name
== NULL_TREE
)
2937 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2938 newname_len
= IDENTIFIER_LENGTH (name
)
2939 + strlen (numbuf
) + 2;
2940 newname
= (char*)alloca (newname_len
);
2941 snprintf (newname
, newname_len
,
2942 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2943 return get_identifier (newname
);
2946 /* Return true if T is a primary function, class or alias template
2950 primary_template_instantiation_p (const_tree t
)
2955 if (TREE_CODE (t
) == FUNCTION_DECL
)
2956 return DECL_LANG_SPECIFIC (t
)
2957 && DECL_TEMPLATE_INSTANTIATION (t
)
2958 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2959 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2960 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2961 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2962 else if (alias_template_specialization_p (t
))
2967 /* Return true if PARM is a template template parameter. */
2970 template_template_parameter_p (const_tree parm
)
2972 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2975 /* Return true iff PARM is a DECL representing a type template
2979 template_type_parameter_p (const_tree parm
)
2982 && (TREE_CODE (parm
) == TYPE_DECL
2983 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2984 && DECL_TEMPLATE_PARM_P (parm
));
2987 /* Return the template parameters of T if T is a
2988 primary template instantiation, NULL otherwise. */
2991 get_primary_template_innermost_parameters (const_tree t
)
2993 tree parms
= NULL
, template_info
= NULL
;
2995 if ((template_info
= get_template_info (t
))
2996 && primary_template_instantiation_p (t
))
2997 parms
= INNERMOST_TEMPLATE_PARMS
2998 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
3003 /* Return the template parameters of the LEVELth level from the full list
3004 of template parameters PARMS. */
3007 get_template_parms_at_level (tree parms
, int level
)
3011 || TREE_CODE (parms
) != TREE_LIST
3012 || level
> TMPL_PARMS_DEPTH (parms
))
3015 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
3016 if (TMPL_PARMS_DEPTH (p
) == level
)
3022 /* Returns the template arguments of T if T is a template instantiation,
3026 get_template_innermost_arguments (const_tree t
)
3028 tree args
= NULL
, template_info
= NULL
;
3030 if ((template_info
= get_template_info (t
))
3031 && TI_ARGS (template_info
))
3032 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3037 /* Return the argument pack elements of T if T is a template argument pack,
3041 get_template_argument_pack_elems (const_tree t
)
3043 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3044 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3047 return ARGUMENT_PACK_ARGS (t
);
3050 /* Structure used to track the progress of find_parameter_packs_r. */
3051 struct find_parameter_pack_data
3053 /* TREE_LIST that will contain all of the parameter packs found by
3055 tree
* parameter_packs
;
3057 /* Set of AST nodes that have been visited by the traversal. */
3058 struct pointer_set_t
*visited
;
3061 /* Identifies all of the argument packs that occur in a template
3062 argument and appends them to the TREE_LIST inside DATA, which is a
3063 find_parameter_pack_data structure. This is a subroutine of
3064 make_pack_expansion and uses_parameter_packs. */
3066 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3069 struct find_parameter_pack_data
* ppd
=
3070 (struct find_parameter_pack_data
*)data
;
3071 bool parameter_pack_p
= false;
3073 /* Handle type aliases/typedefs. */
3074 if (TYPE_ALIAS_P (t
))
3076 if (TYPE_TEMPLATE_INFO (t
))
3077 cp_walk_tree (&TYPE_TI_ARGS (t
),
3078 &find_parameter_packs_r
,
3084 /* Identify whether this is a parameter pack or not. */
3085 switch (TREE_CODE (t
))
3087 case TEMPLATE_PARM_INDEX
:
3088 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3089 parameter_pack_p
= true;
3092 case TEMPLATE_TYPE_PARM
:
3093 t
= TYPE_MAIN_VARIANT (t
);
3094 case TEMPLATE_TEMPLATE_PARM
:
3095 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3096 parameter_pack_p
= true;
3101 if (DECL_PACK_P (t
))
3103 /* We don't want to walk into the type of a PARM_DECL,
3104 because we don't want to see the type parameter pack. */
3106 parameter_pack_p
= true;
3110 /* Look through a lambda capture proxy to the field pack. */
3112 if (DECL_HAS_VALUE_EXPR_P (t
))
3114 tree v
= DECL_VALUE_EXPR (t
);
3116 &find_parameter_packs_r
,
3123 parameter_pack_p
= true;
3126 /* Not a parameter pack. */
3130 if (parameter_pack_p
)
3132 /* Add this parameter pack to the list. */
3133 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3137 cp_walk_tree (&TYPE_CONTEXT (t
),
3138 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3140 /* This switch statement will return immediately if we don't find a
3142 switch (TREE_CODE (t
))
3144 case TEMPLATE_PARM_INDEX
:
3147 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3148 /* Check the template itself. */
3149 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3150 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3151 /* Check the template arguments. */
3152 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3157 case TEMPLATE_TYPE_PARM
:
3158 case TEMPLATE_TEMPLATE_PARM
:
3165 if (TYPE_PTRMEMFUNC_P (t
))
3171 if (TYPE_TEMPLATE_INFO (t
))
3172 cp_walk_tree (&TYPE_TI_ARGS (t
),
3173 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3180 cp_walk_tree (&TREE_TYPE (t
),
3181 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3185 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3190 case TYPE_PACK_EXPANSION
:
3191 case EXPR_PACK_EXPANSION
:
3196 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3201 case IDENTIFIER_NODE
:
3202 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3214 /* Determines if the expression or type T uses any parameter packs. */
3216 uses_parameter_packs (tree t
)
3218 tree parameter_packs
= NULL_TREE
;
3219 struct find_parameter_pack_data ppd
;
3220 ppd
.parameter_packs
= ¶meter_packs
;
3221 ppd
.visited
= pointer_set_create ();
3222 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3223 pointer_set_destroy (ppd
.visited
);
3224 return parameter_packs
!= NULL_TREE
;
3227 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3228 representation a base-class initializer into a parameter pack
3229 expansion. If all goes well, the resulting node will be an
3230 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3233 make_pack_expansion (tree arg
)
3236 tree parameter_packs
= NULL_TREE
;
3237 bool for_types
= false;
3238 struct find_parameter_pack_data ppd
;
3240 if (!arg
|| arg
== error_mark_node
)
3243 if (TREE_CODE (arg
) == TREE_LIST
)
3245 /* The only time we will see a TREE_LIST here is for a base
3246 class initializer. In this case, the TREE_PURPOSE will be a
3247 _TYPE node (representing the base class expansion we're
3248 initializing) and the TREE_VALUE will be a TREE_LIST
3249 containing the initialization arguments.
3251 The resulting expansion looks somewhat different from most
3252 expansions. Rather than returning just one _EXPANSION, we
3253 return a TREE_LIST whose TREE_PURPOSE is a
3254 TYPE_PACK_EXPANSION containing the bases that will be
3255 initialized. The TREE_VALUE will be identical to the
3256 original TREE_VALUE, which is a list of arguments that will
3257 be passed to each base. We do not introduce any new pack
3258 expansion nodes into the TREE_VALUE (although it is possible
3259 that some already exist), because the TREE_PURPOSE and
3260 TREE_VALUE all need to be expanded together with the same
3261 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3262 resulting TREE_PURPOSE will mention the parameter packs in
3263 both the bases and the arguments to the bases. */
3266 tree parameter_packs
= NULL_TREE
;
3268 /* Determine which parameter packs will be used by the base
3270 ppd
.visited
= pointer_set_create ();
3271 ppd
.parameter_packs
= ¶meter_packs
;
3272 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3275 if (parameter_packs
== NULL_TREE
)
3277 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3278 pointer_set_destroy (ppd
.visited
);
3279 return error_mark_node
;
3282 if (TREE_VALUE (arg
) != void_type_node
)
3284 /* Collect the sets of parameter packs used in each of the
3285 initialization arguments. */
3286 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3288 /* Determine which parameter packs will be expanded in this
3290 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3295 pointer_set_destroy (ppd
.visited
);
3297 /* Create the pack expansion type for the base type. */
3298 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3299 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3300 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3302 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3303 they will rarely be compared to anything. */
3304 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3306 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3309 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3312 /* Build the PACK_EXPANSION_* node. */
3314 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3315 : make_node (EXPR_PACK_EXPANSION
);
3316 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3317 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3319 /* Propagate type and const-expression information. */
3320 TREE_TYPE (result
) = TREE_TYPE (arg
);
3321 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3324 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3325 they will rarely be compared to anything. */
3326 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3328 /* Determine which parameter packs will be expanded. */
3329 ppd
.parameter_packs
= ¶meter_packs
;
3330 ppd
.visited
= pointer_set_create ();
3331 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3332 pointer_set_destroy (ppd
.visited
);
3334 /* Make sure we found some parameter packs. */
3335 if (parameter_packs
== NULL_TREE
)
3338 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3340 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3341 return error_mark_node
;
3343 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3345 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3350 /* Checks T for any "bare" parameter packs, which have not yet been
3351 expanded, and issues an error if any are found. This operation can
3352 only be done on full expressions or types (e.g., an expression
3353 statement, "if" condition, etc.), because we could have expressions like:
3355 foo(f(g(h(args)))...)
3357 where "args" is a parameter pack. check_for_bare_parameter_packs
3358 should not be called for the subexpressions args, h(args),
3359 g(h(args)), or f(g(h(args))), because we would produce erroneous
3362 Returns TRUE and emits an error if there were bare parameter packs,
3363 returns FALSE otherwise. */
3365 check_for_bare_parameter_packs (tree t
)
3367 tree parameter_packs
= NULL_TREE
;
3368 struct find_parameter_pack_data ppd
;
3370 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3373 if (TREE_CODE (t
) == TYPE_DECL
)
3376 ppd
.parameter_packs
= ¶meter_packs
;
3377 ppd
.visited
= pointer_set_create ();
3378 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3379 pointer_set_destroy (ppd
.visited
);
3381 if (parameter_packs
)
3383 error ("parameter packs not expanded with %<...%>:");
3384 while (parameter_packs
)
3386 tree pack
= TREE_VALUE (parameter_packs
);
3387 tree name
= NULL_TREE
;
3389 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3390 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3391 name
= TYPE_NAME (pack
);
3392 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3393 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3395 name
= DECL_NAME (pack
);
3398 inform (input_location
, " %qD", name
);
3400 inform (input_location
, " <anonymous>");
3402 parameter_packs
= TREE_CHAIN (parameter_packs
);
3411 /* Expand any parameter packs that occur in the template arguments in
3414 expand_template_argument_pack (tree args
)
3416 tree result_args
= NULL_TREE
;
3417 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3418 int num_result_args
= -1;
3419 int non_default_args_count
= -1;
3421 /* First, determine if we need to expand anything, and the number of
3422 slots we'll need. */
3423 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3425 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3426 if (arg
== NULL_TREE
)
3428 if (ARGUMENT_PACK_P (arg
))
3430 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3431 if (num_result_args
< 0)
3432 num_result_args
= in_arg
+ num_packed
;
3434 num_result_args
+= num_packed
;
3438 if (num_result_args
>= 0)
3443 /* If no expansion is necessary, we're done. */
3444 if (num_result_args
< 0)
3447 /* Expand arguments. */
3448 result_args
= make_tree_vec (num_result_args
);
3449 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3450 non_default_args_count
=
3451 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3452 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3454 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3455 if (ARGUMENT_PACK_P (arg
))
3457 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3458 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3459 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3460 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3461 if (non_default_args_count
> 0)
3462 non_default_args_count
+= num_packed
;
3466 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3470 if (non_default_args_count
>= 0)
3471 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3475 /* Checks if DECL shadows a template parameter.
3477 [temp.local]: A template-parameter shall not be redeclared within its
3478 scope (including nested scopes).
3480 Emits an error and returns TRUE if the DECL shadows a parameter,
3481 returns FALSE otherwise. */
3484 check_template_shadow (tree decl
)
3488 /* If we're not in a template, we can't possibly shadow a template
3490 if (!current_template_parms
)
3493 /* Figure out what we're shadowing. */
3494 if (TREE_CODE (decl
) == OVERLOAD
)
3495 decl
= OVL_CURRENT (decl
);
3496 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3498 /* If there's no previous binding for this name, we're not shadowing
3499 anything, let alone a template parameter. */
3503 /* If we're not shadowing a template parameter, we're done. Note
3504 that OLDDECL might be an OVERLOAD (or perhaps even an
3505 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3507 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3510 /* We check for decl != olddecl to avoid bogus errors for using a
3511 name inside a class. We check TPFI to avoid duplicate errors for
3512 inline member templates. */
3514 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3517 error ("declaration of %q+#D", decl
);
3518 error (" shadows template parm %q+#D", olddecl
);
3522 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3523 ORIG_LEVEL, DECL, and TYPE. */
3526 build_template_parm_index (int index
,
3532 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3533 TEMPLATE_PARM_IDX (t
) = index
;
3534 TEMPLATE_PARM_LEVEL (t
) = level
;
3535 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3536 TEMPLATE_PARM_DECL (t
) = decl
;
3537 TREE_TYPE (t
) = type
;
3538 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3539 TREE_READONLY (t
) = TREE_READONLY (decl
);
3544 /* Find the canonical type parameter for the given template type
3545 parameter. Returns the canonical type parameter, which may be TYPE
3546 if no such parameter existed. */
3549 canonical_type_parameter (tree type
)
3552 int idx
= TEMPLATE_TYPE_IDX (type
);
3553 if (!canonical_template_parms
)
3554 vec_alloc (canonical_template_parms
, idx
+1);
3556 while (canonical_template_parms
->length () <= (unsigned)idx
)
3557 vec_safe_push (canonical_template_parms
, NULL_TREE
);
3559 list
= (*canonical_template_parms
)[idx
];
3560 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3561 list
= TREE_CHAIN (list
);
3564 return TREE_VALUE (list
);
3567 (*canonical_template_parms
)[idx
]
3568 = tree_cons (NULL_TREE
, type
,
3569 (*canonical_template_parms
)[idx
]);
3574 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3575 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3576 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3577 new one is created. */
3580 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3581 tsubst_flags_t complain
)
3583 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3584 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3585 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3586 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3588 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3591 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3592 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3593 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3594 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3595 DECL_ARTIFICIAL (decl
) = 1;
3596 SET_DECL_TEMPLATE_PARM_P (decl
);
3598 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3599 TEMPLATE_PARM_LEVEL (index
) - levels
,
3600 TEMPLATE_PARM_ORIG_LEVEL (index
),
3602 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3603 TEMPLATE_PARM_PARAMETER_PACK (t
)
3604 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3606 /* Template template parameters need this. */
3607 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3608 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3609 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3613 return TEMPLATE_PARM_DESCENDANTS (index
);
3616 /* Process information from new template parameter PARM and append it
3617 to the LIST being built. This new parameter is a non-type
3618 parameter iff IS_NON_TYPE is true. This new parameter is a
3619 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3620 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3621 parameter list PARM belongs to. This is used used to create a
3622 proper canonical type for the type of PARM that is to be created,
3623 iff PARM is a type. If the size is not known, this parameter shall
3627 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3628 bool is_non_type
, bool is_parameter_pack
)
3635 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3636 defval
= TREE_PURPOSE (parm
);
3640 tree p
= tree_last (list
);
3642 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3645 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3646 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3648 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3658 parm
= TREE_VALUE (parm
);
3660 SET_DECL_TEMPLATE_PARM_P (parm
);
3662 if (TREE_TYPE (parm
) == error_mark_node
)
3664 err_parm_list
= build_tree_list (defval
, parm
);
3665 TREE_VALUE (err_parm_list
) = error_mark_node
;
3666 return chainon (list
, err_parm_list
);
3672 The top-level cv-qualifiers on the template-parameter are
3673 ignored when determining its type. */
3674 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3675 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3677 err_parm_list
= build_tree_list (defval
, parm
);
3678 TREE_VALUE (err_parm_list
) = error_mark_node
;
3679 return chainon (list
, err_parm_list
);
3682 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3684 /* This template parameter is not a parameter pack, but it
3685 should be. Complain about "bare" parameter packs. */
3686 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3688 /* Recover by calling this a parameter pack. */
3689 is_parameter_pack
= true;
3693 /* A template parameter is not modifiable. */
3694 TREE_CONSTANT (parm
) = 1;
3695 TREE_READONLY (parm
) = 1;
3696 decl
= build_decl (parm_loc
,
3697 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3698 TREE_CONSTANT (decl
) = 1;
3699 TREE_READONLY (decl
) = 1;
3700 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3701 = build_template_parm_index (idx
, processing_template_decl
,
3702 processing_template_decl
,
3703 decl
, TREE_TYPE (parm
));
3705 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3706 = is_parameter_pack
;
3711 parm
= TREE_VALUE (TREE_VALUE (parm
));
3713 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3715 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3716 /* This is for distinguishing between real templates and template
3717 template parameters */
3718 TREE_TYPE (parm
) = t
;
3719 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3724 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3725 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3726 decl
= build_decl (parm_loc
,
3727 TYPE_DECL
, parm
, t
);
3730 TYPE_NAME (t
) = decl
;
3731 TYPE_STUB_DECL (t
) = decl
;
3733 TEMPLATE_TYPE_PARM_INDEX (t
)
3734 = build_template_parm_index (idx
, processing_template_decl
,
3735 processing_template_decl
,
3736 decl
, TREE_TYPE (parm
));
3737 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3738 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3740 DECL_ARTIFICIAL (decl
) = 1;
3741 SET_DECL_TEMPLATE_PARM_P (decl
);
3743 parm
= build_tree_list (defval
, parm
);
3744 return chainon (list
, parm
);
3747 /* The end of a template parameter list has been reached. Process the
3748 tree list into a parameter vector, converting each parameter into a more
3749 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3753 end_template_parm_list (tree parms
)
3757 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3759 current_template_parms
3760 = tree_cons (size_int (processing_template_decl
),
3761 saved_parmlist
, current_template_parms
);
3763 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3765 next
= TREE_CHAIN (parm
);
3766 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3767 TREE_CHAIN (parm
) = NULL_TREE
;
3770 --processing_template_parmlist
;
3772 return saved_parmlist
;
3775 /* end_template_decl is called after a template declaration is seen. */
3778 end_template_decl (void)
3780 reset_specialization ();
3782 if (! processing_template_decl
)
3785 /* This matches the pushlevel in begin_template_parm_list. */
3788 --processing_template_decl
;
3789 current_template_parms
= TREE_CHAIN (current_template_parms
);
3792 /* Takes a TREE_LIST representing a template parameter and convert it
3793 into an argument suitable to be passed to the type substitution
3794 functions. Note that If the TREE_LIST contains an error_mark
3795 node, the returned argument is error_mark_node. */
3798 template_parm_to_arg (tree t
)
3802 || TREE_CODE (t
) != TREE_LIST
)
3805 if (error_operand_p (TREE_VALUE (t
)))
3806 return error_mark_node
;
3810 if (TREE_CODE (t
) == TYPE_DECL
3811 || TREE_CODE (t
) == TEMPLATE_DECL
)
3815 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3817 /* Turn this argument into a TYPE_ARGUMENT_PACK
3818 with a single element, which expands T. */
3819 tree vec
= make_tree_vec (1);
3820 #ifdef ENABLE_CHECKING
3821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3822 (vec
, TREE_VEC_LENGTH (vec
));
3824 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3826 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3827 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3832 t
= DECL_INITIAL (t
);
3834 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3836 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3837 with a single element, which expands T. */
3838 tree vec
= make_tree_vec (1);
3839 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3840 #ifdef ENABLE_CHECKING
3841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3842 (vec
, TREE_VEC_LENGTH (vec
));
3844 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3846 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3847 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3848 TREE_TYPE (t
) = type
;
3854 /* Given a set of template parameters, return them as a set of template
3855 arguments. The template parameters are represented as a TREE_VEC, in
3856 the form documented in cp-tree.h for template arguments. */
3859 template_parms_to_args (tree parms
)
3862 tree args
= NULL_TREE
;
3863 int length
= TMPL_PARMS_DEPTH (parms
);
3866 /* If there is only one level of template parameters, we do not
3867 create a TREE_VEC of TREE_VECs. Instead, we return a single
3868 TREE_VEC containing the arguments. */
3870 args
= make_tree_vec (length
);
3872 for (header
= parms
; header
; header
= TREE_CHAIN (header
))
3874 tree a
= copy_node (TREE_VALUE (header
));
3877 TREE_TYPE (a
) = NULL_TREE
;
3878 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3879 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3881 #ifdef ENABLE_CHECKING
3882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3886 TREE_VEC_ELT (args
, --l
) = a
;
3891 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3892 /* This can happen for template parms of a template template
3895 template<template<class T, class U> class TT> struct S;
3897 Consider the level of the parms of TT; T and U both have
3898 level 2; TT has no template parm of level 1. So in this case
3899 the first element of full_template_args is NULL_TREE. If we
3900 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3901 of 2. This will make tsubst wrongly consider that T and U
3902 have level 1. Instead, let's create a dummy vector as the
3903 first element of full_template_args so that TMPL_ARGS_DEPTH
3904 returns the correct depth for args. */
3905 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3909 /* Within the declaration of a template, return the currently active
3910 template parameters as an argument TREE_VEC. */
3913 current_template_args (void)
3915 return template_parms_to_args (current_template_parms
);
3918 /* Update the declared TYPE by doing any lookups which were thought to be
3919 dependent, but are not now that we know the SCOPE of the declarator. */
3922 maybe_update_decl_type (tree orig_type
, tree scope
)
3924 tree type
= orig_type
;
3926 if (type
== NULL_TREE
)
3929 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3930 type
= TREE_TYPE (type
);
3932 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3933 && dependent_type_p (type
)
3934 /* Don't bother building up the args in this case. */
3935 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3937 /* tsubst in the args corresponding to the template parameters,
3938 including auto if present. Most things will be unchanged, but
3939 make_typename_type and tsubst_qualified_id will resolve
3940 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3941 tree args
= current_template_args ();
3942 tree auto_node
= type_uses_auto (type
);
3946 tree auto_vec
= make_tree_vec (1);
3947 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3948 args
= add_to_template_args (args
, auto_vec
);
3950 pushed
= push_scope (scope
);
3951 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3956 if (type
== error_mark_node
)
3959 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3961 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3964 type
= TYPE_NAME (type
);
3969 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3970 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3971 a member template. Used by push_template_decl below. */
3974 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3976 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3977 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3978 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3979 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3980 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3985 struct template_parm_data
3987 /* The level of the template parameters we are currently
3991 /* The index of the specialization argument we are currently
3995 /* An array whose size is the number of template parameters. The
3996 elements are nonzero if the parameter has been used in any one
3997 of the arguments processed so far. */
4000 /* An array whose size is the number of template arguments. The
4001 elements are nonzero if the argument makes use of template
4002 parameters of this level. */
4003 int* arg_uses_template_parms
;
4006 /* Subroutine of push_template_decl used to see if each template
4007 parameter in a partial specialization is used in the explicit
4008 argument list. If T is of the LEVEL given in DATA (which is
4009 treated as a template_parm_data*), then DATA->PARMS is marked
4013 mark_template_parm (tree t
, void* data
)
4017 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4019 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4021 level
= TEMPLATE_PARM_LEVEL (t
);
4022 idx
= TEMPLATE_PARM_IDX (t
);
4026 level
= TEMPLATE_TYPE_LEVEL (t
);
4027 idx
= TEMPLATE_TYPE_IDX (t
);
4030 if (level
== tpd
->level
)
4032 tpd
->parms
[idx
] = 1;
4033 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4036 /* Return zero so that for_each_template_parm will continue the
4037 traversal of the tree; we want to mark *every* template parm. */
4041 /* Process the partial specialization DECL. */
4044 process_partial_specialization (tree decl
)
4046 tree type
= TREE_TYPE (decl
);
4047 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4048 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4049 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4050 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4053 int nargs
= TREE_VEC_LENGTH (inner_args
);
4056 bool did_error_intro
= false;
4057 struct template_parm_data tpd
;
4058 struct template_parm_data tpd2
;
4060 gcc_assert (current_template_parms
);
4062 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4063 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4065 /* We check that each of the template parameters given in the
4066 partial specialization is used in the argument list to the
4067 specialization. For example:
4069 template <class T> struct S;
4070 template <class T> struct S<T*>;
4072 The second declaration is OK because `T*' uses the template
4073 parameter T, whereas
4075 template <class T> struct S<int>;
4077 is no good. Even trickier is:
4088 The S2<T> declaration is actually invalid; it is a
4089 full-specialization. Of course,
4092 struct S2<T (*)(U)>;
4094 or some such would have been OK. */
4095 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4096 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4097 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4099 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4100 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4101 for (i
= 0; i
< nargs
; ++i
)
4103 tpd
.current_arg
= i
;
4104 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4105 &mark_template_parm
,
4108 /*include_nondeduced_p=*/false);
4110 for (i
= 0; i
< ntparms
; ++i
)
4111 if (tpd
.parms
[i
] == 0)
4113 /* One of the template parms was not used in the
4115 if (!did_error_intro
)
4117 error ("template parameters not used in partial specialization:");
4118 did_error_intro
= true;
4121 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4124 if (did_error_intro
)
4125 return error_mark_node
;
4127 /* [temp.class.spec]
4129 The argument list of the specialization shall not be identical to
4130 the implicit argument list of the primary template. */
4131 if (comp_template_args
4133 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4135 error ("partial specialization %qT does not specialize any template arguments", type
);
4137 /* A partial specialization that replaces multiple parameters of the
4138 primary template with a pack expansion is less specialized for those
4140 if (nargs
< DECL_NTPARMS (maintmpl
))
4142 error ("partial specialization is not more specialized than the "
4143 "primary template because it replaces multiple parameters "
4144 "with a pack expansion");
4145 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4149 /* [temp.class.spec]
4151 A partially specialized non-type argument expression shall not
4152 involve template parameters of the partial specialization except
4153 when the argument expression is a simple identifier.
4155 The type of a template parameter corresponding to a specialized
4156 non-type argument shall not be dependent on a parameter of the
4159 Also, we verify that pack expansions only occur at the
4160 end of the argument list. */
4161 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4163 for (i
= 0; i
< nargs
; ++i
)
4165 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4166 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4167 tree packed_args
= NULL_TREE
;
4170 if (ARGUMENT_PACK_P (arg
))
4172 /* Extract the arguments from the argument pack. We'll be
4173 iterating over these in the following loop. */
4174 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4175 len
= TREE_VEC_LENGTH (packed_args
);
4178 for (j
= 0; j
< len
; j
++)
4181 /* Get the Jth argument in the parameter pack. */
4182 arg
= TREE_VEC_ELT (packed_args
, j
);
4184 if (PACK_EXPANSION_P (arg
))
4186 /* Pack expansions must come at the end of the
4188 if ((packed_args
&& j
< len
- 1)
4189 || (!packed_args
&& i
< nargs
- 1))
4191 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4192 error ("parameter pack argument %qE must be at the "
4193 "end of the template argument list", arg
);
4195 error ("parameter pack argument %qT must be at the "
4196 "end of the template argument list", arg
);
4200 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4201 /* We only care about the pattern. */
4202 arg
= PACK_EXPANSION_PATTERN (arg
);
4204 if (/* These first two lines are the `non-type' bit. */
4206 && TREE_CODE (arg
) != TEMPLATE_DECL
4207 /* This next line is the `argument expression is not just a
4208 simple identifier' condition and also the `specialized
4209 non-type argument' bit. */
4210 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4212 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4213 || (packed_args
&& uses_template_parms (arg
)))
4214 error ("template argument %qE involves template parameter(s)",
4218 /* Look at the corresponding template parameter,
4219 marking which template parameters its type depends
4221 tree type
= TREE_TYPE (parm
);
4225 /* We haven't yet initialized TPD2. Do so now. */
4226 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4227 /* The number of parameters here is the number in the
4228 main template, which, as checked in the assertion
4230 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4232 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4235 /* Mark the template parameters. But this time, we're
4236 looking for the template parameters of the main
4237 template, not in the specialization. */
4238 tpd2
.current_arg
= i
;
4239 tpd2
.arg_uses_template_parms
[i
] = 0;
4240 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4241 for_each_template_parm (type
,
4242 &mark_template_parm
,
4245 /*include_nondeduced_p=*/false);
4247 if (tpd2
.arg_uses_template_parms
[i
])
4249 /* The type depended on some template parameters.
4250 If they are fully specialized in the
4251 specialization, that's OK. */
4254 for (j
= 0; j
< nargs
; ++j
)
4255 if (tpd2
.parms
[j
] != 0
4256 && tpd
.arg_uses_template_parms
[j
])
4259 error_n (input_location
, count
,
4260 "type %qT of template argument %qE depends "
4261 "on a template parameter",
4262 "type %qT of template argument %qE depends "
4263 "on template parameters",
4272 /* We should only get here once. */
4273 gcc_assert (!COMPLETE_TYPE_P (type
));
4275 tree tmpl
= build_template_decl (decl
, current_template_parms
,
4276 DECL_MEMBER_TEMPLATE_P (maintmpl
));
4277 TREE_TYPE (tmpl
) = type
;
4278 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4279 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4280 DECL_TEMPLATE_INFO (tmpl
) = build_template_info (maintmpl
, specargs
);
4281 DECL_PRIMARY_TEMPLATE (tmpl
) = maintmpl
;
4283 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4284 = tree_cons (specargs
, tmpl
,
4285 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4286 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4288 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4289 inst
= TREE_CHAIN (inst
))
4291 tree inst_type
= TREE_VALUE (inst
);
4292 if (COMPLETE_TYPE_P (inst_type
)
4293 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4295 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4296 if (spec
&& TREE_TYPE (spec
) == type
)
4297 permerror (input_location
,
4298 "partial specialization of %qT after instantiation "
4299 "of %qT", type
, inst_type
);
4306 /* Check that a template declaration's use of default arguments and
4307 parameter packs is not invalid. Here, PARMS are the template
4308 parameters. IS_PRIMARY is true if DECL is the thing declared by
4309 a primary template. IS_PARTIAL is true if DECL is a partial
4312 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4313 declaration (but not a definition); 1 indicates a declaration, 2
4314 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4315 emitted for extraneous default arguments.
4317 Returns TRUE if there were no errors found, FALSE otherwise. */
4320 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4321 bool is_partial
, int is_friend_decl
)
4324 int last_level_to_check
;
4326 bool no_errors
= true;
4330 A default template-argument shall not be specified in a
4331 function template declaration or a function template definition, nor
4332 in the template-parameter-list of the definition of a member of a
4335 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4336 /* You can't have a function template declaration in a local
4337 scope, nor you can you define a member of a class template in a
4341 if (TREE_CODE (decl
) == TYPE_DECL
4343 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4344 /* A lambda doesn't have an explicit declaration; don't complain
4345 about the parms of the enclosing class. */
4348 if (current_class_type
4349 && !TYPE_BEING_DEFINED (current_class_type
)
4350 && DECL_LANG_SPECIFIC (decl
)
4351 && DECL_DECLARES_FUNCTION_P (decl
)
4352 /* If this is either a friend defined in the scope of the class
4353 or a member function. */
4354 && (DECL_FUNCTION_MEMBER_P (decl
)
4355 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4356 : DECL_FRIEND_CONTEXT (decl
)
4357 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4359 /* And, if it was a member function, it really was defined in
4360 the scope of the class. */
4361 && (!DECL_FUNCTION_MEMBER_P (decl
)
4362 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4363 /* We already checked these parameters when the template was
4364 declared, so there's no need to do it again now. This function
4365 was defined in class scope, but we're processing its body now
4366 that the class is complete. */
4369 /* Core issue 226 (C++0x only): the following only applies to class
4372 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4376 If a template-parameter has a default template-argument, all
4377 subsequent template-parameters shall have a default
4378 template-argument supplied. */
4379 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4381 tree inner_parms
= TREE_VALUE (parm_level
);
4382 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4383 int seen_def_arg_p
= 0;
4386 for (i
= 0; i
< ntparms
; ++i
)
4388 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4390 if (parm
== error_mark_node
)
4393 if (TREE_PURPOSE (parm
))
4395 else if (seen_def_arg_p
4396 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4398 error ("no default argument for %qD", TREE_VALUE (parm
));
4399 /* For better subsequent error-recovery, we indicate that
4400 there should have been a default argument. */
4401 TREE_PURPOSE (parm
) = error_mark_node
;
4404 else if (!is_partial
4406 /* Don't complain about an enclosing partial
4408 && parm_level
== parms
4409 && TREE_CODE (decl
) == TYPE_DECL
4411 && template_parameter_pack_p (TREE_VALUE (parm
)))
4413 /* A primary class template can only have one
4414 parameter pack, at the end of the template
4417 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4418 error ("parameter pack %qE must be at the end of the"
4419 " template parameter list", TREE_VALUE (parm
));
4421 error ("parameter pack %qT must be at the end of the"
4422 " template parameter list",
4423 TREE_TYPE (TREE_VALUE (parm
)));
4425 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4433 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4437 /* For an ordinary class template, default template arguments are
4438 allowed at the innermost level, e.g.:
4439 template <class T = int>
4441 but, in a partial specialization, they're not allowed even
4442 there, as we have in [temp.class.spec]:
4444 The template parameter list of a specialization shall not
4445 contain default template argument values.
4447 So, for a partial specialization, or for a function template
4448 (in C++98/C++03), we look at all of them. */
4451 /* But, for a primary class template that is not a partial
4452 specialization we look at all template parameters except the
4454 parms
= TREE_CHAIN (parms
);
4456 /* Figure out what error message to issue. */
4457 if (is_friend_decl
== 2)
4458 msg
= G_("default template arguments may not be used in function template "
4459 "friend re-declaration");
4460 else if (is_friend_decl
)
4461 msg
= G_("default template arguments may not be used in function template "
4462 "friend declarations");
4463 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4464 msg
= G_("default template arguments may not be used in function templates "
4465 "without -std=c++11 or -std=gnu++11");
4466 else if (is_partial
)
4467 msg
= G_("default template arguments may not be used in "
4468 "partial specializations");
4470 msg
= G_("default argument for template parameter for class enclosing %qD");
4472 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4473 /* If we're inside a class definition, there's no need to
4474 examine the parameters to the class itself. On the one
4475 hand, they will be checked when the class is defined, and,
4476 on the other, default arguments are valid in things like:
4477 template <class T = double>
4478 struct S { template <class U> void f(U); };
4479 Here the default argument for `S' has no bearing on the
4480 declaration of `f'. */
4481 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4483 /* Check everything. */
4484 last_level_to_check
= 0;
4486 for (parm_level
= parms
;
4487 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4488 parm_level
= TREE_CHAIN (parm_level
))
4490 tree inner_parms
= TREE_VALUE (parm_level
);
4494 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4495 for (i
= 0; i
< ntparms
; ++i
)
4497 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4500 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4505 if (is_friend_decl
== 2)
4512 /* Clear out the default argument so that we are not
4514 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4518 /* At this point, if we're still interested in issuing messages,
4519 they must apply to classes surrounding the object declared. */
4521 msg
= G_("default argument for template parameter for class "
4528 /* Worker for push_template_decl_real, called via
4529 for_each_template_parm. DATA is really an int, indicating the
4530 level of the parameters we are interested in. If T is a template
4531 parameter of that level, return nonzero. */
4534 template_parm_this_level_p (tree t
, void* data
)
4536 int this_level
= *(int *)data
;
4539 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4540 level
= TEMPLATE_PARM_LEVEL (t
);
4542 level
= TEMPLATE_TYPE_LEVEL (t
);
4543 return level
== this_level
;
4546 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4547 parameters given by current_template_args, or reuses a
4548 previously existing one, if appropriate. Returns the DECL, or an
4549 equivalent one, if it is replaced via a call to duplicate_decls.
4551 If IS_FRIEND is true, DECL is a friend declaration. */
4554 push_template_decl_real (tree decl
, bool is_friend
)
4562 int new_template_p
= 0;
4563 /* True if the template is a member template, in the sense of
4565 bool member_template_p
= false;
4567 if (decl
== error_mark_node
|| !current_template_parms
)
4568 return error_mark_node
;
4570 /* See if this is a partial specialization. */
4571 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4572 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4573 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4575 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4579 /* For a friend, we want the context of the friend function, not
4580 the type of which it is a friend. */
4581 ctx
= CP_DECL_CONTEXT (decl
);
4582 else if (CP_DECL_CONTEXT (decl
)
4583 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4584 /* In the case of a virtual function, we want the class in which
4586 ctx
= CP_DECL_CONTEXT (decl
);
4588 /* Otherwise, if we're currently defining some class, the DECL
4589 is assumed to be a member of the class. */
4590 ctx
= current_scope ();
4592 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4595 if (!DECL_CONTEXT (decl
))
4596 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4598 /* See if this is a primary template. */
4599 if (is_friend
&& ctx
)
4600 /* A friend template that specifies a class context, i.e.
4601 template <typename T> friend void A<T>::f();
4605 is_primary
= template_parm_scope_p ();
4609 if (DECL_CLASS_SCOPE_P (decl
))
4610 member_template_p
= true;
4611 if (TREE_CODE (decl
) == TYPE_DECL
4612 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4614 error ("template class without a name");
4615 return error_mark_node
;
4617 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4619 if (DECL_DESTRUCTOR_P (decl
))
4623 A destructor shall not be a member template. */
4624 error ("destructor %qD declared as member template", decl
);
4625 return error_mark_node
;
4627 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4628 && (!prototype_p (TREE_TYPE (decl
))
4629 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4630 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4631 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4632 == void_list_node
)))
4634 /* [basic.stc.dynamic.allocation]
4636 An allocation function can be a function
4637 template. ... Template allocation functions shall
4638 have two or more parameters. */
4639 error ("invalid template declaration of %qD", decl
);
4640 return error_mark_node
;
4643 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4644 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4646 else if (TREE_CODE (decl
) == TYPE_DECL
4647 && TYPE_DECL_ALIAS_P (decl
))
4648 /* alias-declaration */
4649 gcc_assert (!DECL_ARTIFICIAL (decl
));
4652 error ("template declaration of %q#D", decl
);
4653 return error_mark_node
;
4657 /* Check to see that the rules regarding the use of default
4658 arguments are not being violated. */
4659 check_default_tmpl_args (decl
, current_template_parms
,
4660 is_primary
, is_partial
, /*is_friend_decl=*/0);
4662 /* Ensure that there are no parameter packs in the type of this
4663 declaration that have not been expanded. */
4664 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4666 /* Check each of the arguments individually to see if there are
4667 any bare parameter packs. */
4668 tree type
= TREE_TYPE (decl
);
4669 tree arg
= DECL_ARGUMENTS (decl
);
4670 tree argtype
= TYPE_ARG_TYPES (type
);
4672 while (arg
&& argtype
)
4674 if (!DECL_PACK_P (arg
)
4675 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4677 /* This is a PARM_DECL that contains unexpanded parameter
4678 packs. We have already complained about this in the
4679 check_for_bare_parameter_packs call, so just replace
4680 these types with ERROR_MARK_NODE. */
4681 TREE_TYPE (arg
) = error_mark_node
;
4682 TREE_VALUE (argtype
) = error_mark_node
;
4685 arg
= DECL_CHAIN (arg
);
4686 argtype
= TREE_CHAIN (argtype
);
4689 /* Check for bare parameter packs in the return type and the
4690 exception specifiers. */
4691 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4692 /* Errors were already issued, set return type to int
4693 as the frontend doesn't expect error_mark_node as
4695 TREE_TYPE (type
) = integer_type_node
;
4696 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4697 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4699 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4700 && TYPE_DECL_ALIAS_P (decl
))
4701 ? DECL_ORIGINAL_TYPE (decl
)
4702 : TREE_TYPE (decl
)))
4704 TREE_TYPE (decl
) = error_mark_node
;
4705 return error_mark_node
;
4709 return process_partial_specialization (decl
);
4711 args
= current_template_args ();
4714 || TREE_CODE (ctx
) == FUNCTION_DECL
4715 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4716 || (TREE_CODE (decl
) == TYPE_DECL
4717 && LAMBDA_TYPE_P (TREE_TYPE (decl
)))
4718 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4720 if (DECL_LANG_SPECIFIC (decl
)
4721 && DECL_TEMPLATE_INFO (decl
)
4722 && DECL_TI_TEMPLATE (decl
))
4723 tmpl
= DECL_TI_TEMPLATE (decl
);
4724 /* If DECL is a TYPE_DECL for a class-template, then there won't
4725 be DECL_LANG_SPECIFIC. The information equivalent to
4726 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4727 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4728 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4729 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4731 /* Since a template declaration already existed for this
4732 class-type, we must be redeclaring it here. Make sure
4733 that the redeclaration is valid. */
4734 redeclare_class_template (TREE_TYPE (decl
),
4735 current_template_parms
);
4736 /* We don't need to create a new TEMPLATE_DECL; just use the
4737 one we already had. */
4738 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4742 tmpl
= build_template_decl (decl
, current_template_parms
,
4746 if (DECL_LANG_SPECIFIC (decl
)
4747 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4749 /* A specialization of a member template of a template
4751 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4752 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4753 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4759 tree a
, t
, current
, parms
;
4761 tree tinfo
= get_template_info (decl
);
4765 error ("template definition of non-template %q#D", decl
);
4766 return error_mark_node
;
4769 tmpl
= TI_TEMPLATE (tinfo
);
4771 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4772 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4773 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4774 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4778 /* The declaration is a specialization of a member
4779 template, declared outside the class. Therefore, the
4780 innermost template arguments will be NULL, so we
4781 replace them with the arguments determined by the
4782 earlier call to check_explicit_specialization. */
4783 args
= DECL_TI_ARGS (decl
);
4786 = build_template_decl (decl
, current_template_parms
,
4788 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4789 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4790 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4791 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4792 DECL_TEMPLATE_INFO (new_tmpl
)
4793 = build_template_info (tmpl
, args
);
4795 register_specialization (new_tmpl
,
4796 most_general_template (tmpl
),
4802 /* Make sure the template headers we got make sense. */
4804 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4805 i
= TMPL_PARMS_DEPTH (parms
);
4806 if (TMPL_ARGS_DEPTH (args
) != i
)
4808 error ("expected %d levels of template parms for %q#D, got %d",
4809 i
, decl
, TMPL_ARGS_DEPTH (args
));
4812 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4814 a
= TMPL_ARGS_LEVEL (args
, i
);
4815 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4817 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4819 if (current
== decl
)
4820 error ("got %d template parameters for %q#D",
4821 TREE_VEC_LENGTH (a
), decl
);
4823 error ("got %d template parameters for %q#T",
4824 TREE_VEC_LENGTH (a
), current
);
4825 error (" but %d required", TREE_VEC_LENGTH (t
));
4826 /* Avoid crash in import_export_decl. */
4827 DECL_INTERFACE_KNOWN (decl
) = 1;
4828 return error_mark_node
;
4831 if (current
== decl
)
4833 else if (current
== NULL_TREE
)
4834 /* Can happen in erroneous input. */
4837 current
= get_containing_scope (current
);
4840 /* Check that the parms are used in the appropriate qualifying scopes
4841 in the declarator. */
4842 if (!comp_template_args
4844 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4847 template arguments to %qD do not match original template %qD",
4848 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4849 if (!uses_template_parms (TI_ARGS (tinfo
)))
4850 inform (input_location
, "use template<> for an explicit specialization");
4851 /* Avoid crash in import_export_decl. */
4852 DECL_INTERFACE_KNOWN (decl
) = 1;
4853 return error_mark_node
;
4857 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4858 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4860 /* Push template declarations for global functions and types. Note
4861 that we do not try to push a global template friend declared in a
4862 template class; such a thing may well depend on the template
4863 parameters of the class. */
4864 if (new_template_p
&& !ctx
4865 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4867 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4868 if (tmpl
== error_mark_node
)
4869 return error_mark_node
;
4871 /* Hide template friend classes that haven't been declared yet. */
4872 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4874 DECL_ANTICIPATED (tmpl
) = 1;
4875 DECL_FRIEND_P (tmpl
) = 1;
4881 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4884 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4885 if (DECL_CONV_FN_P (tmpl
))
4887 int depth
= TMPL_PARMS_DEPTH (parms
);
4889 /* It is a conversion operator. See if the type converted to
4890 depends on innermost template operands. */
4892 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4894 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4897 /* Give template template parms a DECL_CONTEXT of the template
4898 for which they are a parameter. */
4899 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4900 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4902 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4903 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4904 DECL_CONTEXT (parm
) = tmpl
;
4908 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4909 back to its most general template. If TMPL is a specialization,
4910 ARGS may only have the innermost set of arguments. Add the missing
4911 argument levels if necessary. */
4912 if (DECL_TEMPLATE_INFO (tmpl
))
4913 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4915 info
= build_template_info (tmpl
, args
);
4917 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4918 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4921 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4922 retrofit_lang_decl (decl
);
4923 if (DECL_LANG_SPECIFIC (decl
))
4924 DECL_TEMPLATE_INFO (decl
) = info
;
4927 return DECL_TEMPLATE_RESULT (tmpl
);
4931 push_template_decl (tree decl
)
4933 return push_template_decl_real (decl
, false);
4936 /* FN is an inheriting constructor that inherits from the constructor
4937 template INHERITED; turn FN into a constructor template with a matching
4941 add_inherited_template_parms (tree fn
, tree inherited
)
4944 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited
));
4945 inner_parms
= copy_node (inner_parms
);
4947 = tree_cons (size_int (processing_template_decl
+ 1),
4948 inner_parms
, current_template_parms
);
4949 tree tmpl
= build_template_decl (fn
, parms
, /*member*/true);
4950 tree args
= template_parms_to_args (parms
);
4951 DECL_TEMPLATE_INFO (fn
) = build_template_info (tmpl
, args
);
4952 TREE_TYPE (tmpl
) = TREE_TYPE (fn
);
4953 DECL_TEMPLATE_RESULT (tmpl
) = fn
;
4954 DECL_ARTIFICIAL (tmpl
) = true;
4955 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4959 /* Called when a class template TYPE is redeclared with the indicated
4960 template PARMS, e.g.:
4962 template <class T> struct S;
4963 template <class T> struct S {}; */
4966 redeclare_class_template (tree type
, tree parms
)
4972 if (!TYPE_TEMPLATE_INFO (type
))
4974 error ("%qT is not a template type", type
);
4978 tmpl
= TYPE_TI_TEMPLATE (type
);
4979 if (!PRIMARY_TEMPLATE_P (tmpl
))
4980 /* The type is nested in some template class. Nothing to worry
4981 about here; there are no new template parameters for the nested
4987 error ("template specifiers not specified in declaration of %qD",
4992 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4993 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4995 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4997 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4998 "redeclared with %d template parameter",
4999 "redeclared with %d template parameters",
5000 TREE_VEC_LENGTH (parms
));
5001 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5002 "previous declaration %q+D used %d template parameter",
5003 "previous declaration %q+D used %d template parameters",
5004 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5008 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5015 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5016 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5019 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5020 if (tmpl_parm
== error_mark_node
)
5023 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5024 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5025 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5027 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5029 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5030 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5031 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5032 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5033 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5034 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5035 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5036 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5037 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5039 error ("template parameter %q+#D", tmpl_parm
);
5040 error ("redeclared here as %q#D", parm
);
5044 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5046 /* We have in [temp.param]:
5048 A template-parameter may not be given default arguments
5049 by two different declarations in the same scope. */
5050 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5051 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5052 "original definition appeared here");
5056 if (parm_default
!= NULL_TREE
)
5057 /* Update the previous template parameters (which are the ones
5058 that will really count) with the new default value. */
5059 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5060 else if (tmpl_default
!= NULL_TREE
)
5061 /* Update the new parameters, too; they'll be used as the
5062 parameters for any members. */
5063 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5069 /* Simplify EXPR if it is a non-dependent expression. Returns the
5070 (possibly simplified) expression. */
5073 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5075 if (expr
== NULL_TREE
)
5078 /* If we're in a template, but EXPR isn't value dependent, simplify
5079 it. We're supposed to treat:
5081 template <typename T> void f(T[1 + 1]);
5082 template <typename T> void f(T[2]);
5084 as two declarations of the same function, for example. */
5085 if (processing_template_decl
5086 && !instantiation_dependent_expression_p (expr
)
5087 && potential_constant_expression (expr
))
5089 HOST_WIDE_INT saved_processing_template_decl
;
5091 saved_processing_template_decl
= processing_template_decl
;
5092 processing_template_decl
= 0;
5093 expr
= tsubst_copy_and_build (expr
,
5096 /*in_decl=*/NULL_TREE
,
5097 /*function_p=*/false,
5098 /*integral_constant_expression_p=*/true);
5099 processing_template_decl
= saved_processing_template_decl
;
5105 fold_non_dependent_expr (tree expr
)
5107 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5110 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5111 template declaration, or a TYPE_DECL for an alias declaration. */
5114 alias_type_or_template_p (tree t
)
5118 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5121 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5122 || DECL_ALIAS_TEMPLATE_P (t
));
5125 /* Return TRUE iff is a specialization of an alias template. */
5128 alias_template_specialization_p (const_tree t
)
5134 && TYPE_TEMPLATE_INFO (t
)
5135 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
5136 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5139 /* Return either TMPL or another template that it is equivalent to under DR
5140 1286: An alias that just changes the name of a template is equivalent to
5141 the other template. */
5144 get_underlying_template (tree tmpl
)
5146 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
5147 while (DECL_ALIAS_TEMPLATE_P (tmpl
))
5149 tree result
= DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl
));
5150 if (TYPE_TEMPLATE_INFO (result
))
5152 tree sub
= TYPE_TI_TEMPLATE (result
);
5153 if (PRIMARY_TEMPLATE_P (sub
)
5154 && same_type_p (result
, TREE_TYPE (sub
)))
5156 /* The alias type is equivalent to the pattern of the
5157 underlying template, so strip the alias. */
5167 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5168 must be a function or a pointer-to-function type, as specified
5169 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5170 and check that the resulting function has external linkage. */
5173 convert_nontype_argument_function (tree type
, tree expr
)
5177 linkage_kind linkage
;
5179 fn
= instantiate_type (type
, fns
, tf_none
);
5180 if (fn
== error_mark_node
)
5181 return error_mark_node
;
5184 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5185 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5186 if (BASELINK_P (fn_no_ptr
))
5187 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5189 /* [temp.arg.nontype]/1
5191 A template-argument for a non-type, non-template template-parameter
5194 -- the address of an object or function with external [C++11: or
5195 internal] linkage. */
5197 if (TREE_CODE (fn_no_ptr
) != FUNCTION_DECL
)
5199 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5200 if (TYPE_PTR_P (type
))
5201 error ("it must be the address of a function with external linkage");
5203 error ("it must be the name of a function with external linkage");
5207 linkage
= decl_linkage (fn_no_ptr
);
5208 if (cxx_dialect
>= cxx11
? linkage
== lk_none
: linkage
!= lk_external
)
5210 if (cxx_dialect
>= cxx11
)
5211 error ("%qE is not a valid template argument for type %qT "
5212 "because %qD has no linkage",
5213 expr
, type
, fn_no_ptr
);
5215 error ("%qE is not a valid template argument for type %qT "
5216 "because %qD does not have external linkage",
5217 expr
, type
, fn_no_ptr
);
5224 /* Subroutine of convert_nontype_argument.
5225 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5226 Emit an error otherwise. */
5229 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5230 tsubst_flags_t complain
)
5233 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5235 if (cxx_dialect
>= cxx11
&& null_member_pointer_value_p (expr
))
5237 if (complain
& tf_error
)
5239 error ("%qE is not a valid template argument for type %qT",
5241 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5246 /* Returns TRUE iff the address of OP is value-dependent.
5248 14.6.2.4 [temp.dep.temp]:
5249 A non-integral non-type template-argument is dependent if its type is
5250 dependent or it has either of the following forms
5253 and contains a nested-name-specifier which specifies a class-name that
5254 names a dependent type.
5256 We generalize this to just say that the address of a member of a
5257 dependent class is value-dependent; the above doesn't cover the
5258 address of a static data member named with an unqualified-id. */
5261 has_value_dependent_address (tree op
)
5263 /* We could use get_inner_reference here, but there's no need;
5264 this is only relevant for template non-type arguments, which
5265 can only be expressed as &id-expression. */
5268 tree ctx
= CP_DECL_CONTEXT (op
);
5269 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5276 /* The next set of functions are used for providing helpful explanatory
5277 diagnostics for failed overload resolution. Their messages should be
5278 indented by two spaces for consistency with the messages in
5282 unify_success (bool /*explain_p*/)
5288 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5291 inform (input_location
,
5292 " couldn't deduce template parameter %qD", parm
);
5297 unify_invalid (bool /*explain_p*/)
5303 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5306 inform (input_location
,
5307 " types %qT and %qT have incompatible cv-qualifiers",
5313 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5316 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5321 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5324 inform (input_location
,
5325 " template parameter %qD is not a parameter pack, but "
5332 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5335 inform (input_location
,
5336 " template argument %qE does not match "
5337 "pointer-to-member constant %qE",
5343 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5346 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5351 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5354 inform (input_location
,
5355 " inconsistent parameter pack deduction with %qT and %qT",
5361 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5366 inform (input_location
,
5367 " deduced conflicting types for parameter %qT (%qT and %qT)",
5368 parm
, first
, second
);
5370 inform (input_location
,
5371 " deduced conflicting values for non-type parameter "
5372 "%qE (%qE and %qE)", parm
, first
, second
);
5378 unify_vla_arg (bool explain_p
, tree arg
)
5381 inform (input_location
,
5382 " variable-sized array type %qT is not "
5383 "a valid template argument",
5389 unify_method_type_error (bool explain_p
, tree arg
)
5392 inform (input_location
,
5393 " member function type %qT is not a valid template argument",
5399 unify_arity (bool explain_p
, int have
, int wanted
)
5402 inform_n (input_location
, wanted
,
5403 " candidate expects %d argument, %d provided",
5404 " candidate expects %d arguments, %d provided",
5410 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5412 return unify_arity (explain_p
, have
, wanted
);
5416 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5418 return unify_arity (explain_p
, have
, wanted
);
5422 unify_arg_conversion (bool explain_p
, tree to_type
,
5423 tree from_type
, tree arg
)
5426 inform (EXPR_LOC_OR_HERE (arg
),
5427 " cannot convert %qE (type %qT) to type %qT",
5428 arg
, from_type
, to_type
);
5433 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5434 tree parm
, tree arg
)
5439 case tbr_ambiguous_baseclass
:
5440 inform (input_location
, " %qT is an ambiguous base class of %qT",
5444 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5451 unify_inconsistent_template_template_parameters (bool explain_p
)
5454 inform (input_location
,
5455 " template parameters of a template template argument are "
5456 "inconsistent with other deduced template arguments");
5461 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5464 inform (input_location
,
5465 " can't deduce a template for %qT from non-template type %qT",
5471 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5474 inform (input_location
,
5475 " template argument %qE does not match %qD", arg
, parm
);
5480 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5483 inform (input_location
,
5484 " could not resolve address from overloaded function %qE",
5489 /* Attempt to convert the non-type template parameter EXPR to the
5490 indicated TYPE. If the conversion is successful, return the
5491 converted value. If the conversion is unsuccessful, return
5492 NULL_TREE if we issued an error message, or error_mark_node if we
5493 did not. We issue error messages for out-and-out bad template
5494 parameters, but not simply because the conversion failed, since we
5495 might be just trying to do argument deduction. Both TYPE and EXPR
5496 must be non-dependent.
5498 The conversion follows the special rules described in
5499 [temp.arg.nontype], and it is much more strict than an implicit
5502 This function is called twice for each template argument (see
5503 lookup_template_class for a more accurate description of this
5504 problem). This means that we need to handle expressions which
5505 are not valid in a C++ source, but can be created from the
5506 first call (for instance, casts to perform conversions). These
5507 hacks can go away after we fix the double coercion problem. */
5510 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5514 /* Detect immediately string literals as invalid non-type argument.
5515 This special-case is not needed for correctness (we would easily
5516 catch this later), but only to provide better diagnostic for this
5517 common user mistake. As suggested by DR 100, we do not mention
5518 linkage issues in the diagnostic as this is not the point. */
5519 /* FIXME we're making this OK. */
5520 if (TREE_CODE (expr
) == STRING_CST
)
5522 if (complain
& tf_error
)
5523 error ("%qE is not a valid template argument for type %qT "
5524 "because string literals can never be used in this context",
5529 /* Add the ADDR_EXPR now for the benefit of
5530 value_dependent_expression_p. */
5531 if (TYPE_PTROBV_P (type
)
5532 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5534 expr
= decay_conversion (expr
, complain
);
5535 if (expr
== error_mark_node
)
5536 return error_mark_node
;
5539 /* If we are in a template, EXPR may be non-dependent, but still
5540 have a syntactic, rather than semantic, form. For example, EXPR
5541 might be a SCOPE_REF, rather than the VAR_DECL to which the
5542 SCOPE_REF refers. Preserving the qualifying scope is necessary
5543 so that access checking can be performed when the template is
5544 instantiated -- but here we need the resolved form so that we can
5545 convert the argument. */
5546 if (TYPE_REF_OBJ_P (type
)
5547 && has_value_dependent_address (expr
))
5548 /* If we want the address and it's value-dependent, don't fold. */;
5549 else if (!type_unknown_p (expr
))
5550 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5551 if (error_operand_p (expr
))
5552 return error_mark_node
;
5553 expr_type
= TREE_TYPE (expr
);
5554 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5555 expr
= mark_lvalue_use (expr
);
5557 expr
= mark_rvalue_use (expr
);
5559 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5560 to a non-type argument of "nullptr". */
5561 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5562 expr
= convert (type
, expr
);
5564 /* In C++11, integral or enumeration non-type template arguments can be
5565 arbitrary constant expressions. Pointer and pointer to
5566 member arguments can be general constant expressions that evaluate
5567 to a null value, but otherwise still need to be of a specific form. */
5568 if (cxx_dialect
>= cxx11
)
5570 if (TREE_CODE (expr
) == PTRMEM_CST
)
5571 /* A PTRMEM_CST is already constant, and a valid template
5572 argument for a parameter of pointer to member type, we just want
5573 to leave it in that form rather than lower it to a
5575 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5576 expr
= maybe_constant_value (expr
);
5577 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5579 tree folded
= maybe_constant_value (expr
);
5580 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5581 : null_member_pointer_value_p (folded
))
5586 /* HACK: Due to double coercion, we can get a
5587 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5588 which is the tree that we built on the first call (see
5589 below when coercing to reference to object or to reference to
5590 function). We just strip everything and get to the arg.
5591 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5593 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5595 tree probe_type
, probe
= expr
;
5596 if (REFERENCE_REF_P (probe
))
5597 probe
= TREE_OPERAND (probe
, 0);
5598 probe_type
= TREE_TYPE (probe
);
5599 if (TREE_CODE (probe
) == NOP_EXPR
)
5601 /* ??? Maybe we could use convert_from_reference here, but we
5602 would need to relax its constraints because the NOP_EXPR
5603 could actually change the type to something more cv-qualified,
5604 and this is not folded by convert_from_reference. */
5605 tree addr
= TREE_OPERAND (probe
, 0);
5606 if (TREE_CODE (probe_type
) == REFERENCE_TYPE
5607 && TREE_CODE (addr
) == ADDR_EXPR
5608 && TYPE_PTR_P (TREE_TYPE (addr
))
5609 && (same_type_ignoring_top_level_qualifiers_p
5610 (TREE_TYPE (probe_type
),
5611 TREE_TYPE (TREE_TYPE (addr
)))))
5613 expr
= TREE_OPERAND (addr
, 0);
5614 expr_type
= TREE_TYPE (expr
);
5619 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5620 parameter is a pointer to object, through decay and
5621 qualification conversion. Let's strip everything. */
5622 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5626 if (TREE_CODE (probe
) == ADDR_EXPR
5627 && TYPE_PTR_P (TREE_TYPE (probe
)))
5629 /* Skip the ADDR_EXPR only if it is part of the decay for
5630 an array. Otherwise, it is part of the original argument
5631 in the source code. */
5632 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe
, 0))) == ARRAY_TYPE
)
5633 probe
= TREE_OPERAND (probe
, 0);
5635 expr_type
= TREE_TYPE (expr
);
5639 /* [temp.arg.nontype]/5, bullet 1
5641 For a non-type template-parameter of integral or enumeration type,
5642 integral promotions (_conv.prom_) and integral conversions
5643 (_conv.integral_) are applied. */
5644 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5646 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5647 t
= maybe_constant_value (t
);
5648 if (t
!= error_mark_node
)
5651 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5652 return error_mark_node
;
5654 /* Notice that there are constant expressions like '4 % 0' which
5655 do not fold into integer constants. */
5656 if (TREE_CODE (expr
) != INTEGER_CST
)
5658 if (complain
& tf_error
)
5660 int errs
= errorcount
, warns
= warningcount
+ werrorcount
;
5661 if (processing_template_decl
5662 && !require_potential_constant_expression (expr
))
5664 expr
= cxx_constant_value (expr
);
5665 if (errorcount
> errs
|| warningcount
+ werrorcount
> warns
)
5666 inform (EXPR_LOC_OR_HERE (expr
),
5667 "in template argument for type %qT ", type
);
5668 if (expr
== error_mark_node
)
5670 /* else cxx_constant_value complained but gave us
5671 a real constant, so go ahead. */
5672 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5678 /* Avoid typedef problems. */
5679 if (TREE_TYPE (expr
) != type
)
5680 expr
= fold_convert (type
, expr
);
5682 /* [temp.arg.nontype]/5, bullet 2
5684 For a non-type template-parameter of type pointer to object,
5685 qualification conversions (_conv.qual_) and the array-to-pointer
5686 conversion (_conv.array_) are applied. */
5687 else if (TYPE_PTROBV_P (type
))
5689 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5691 A template-argument for a non-type, non-template template-parameter
5692 shall be one of: [...]
5694 -- the name of a non-type template-parameter;
5695 -- the address of an object or function with external linkage, [...]
5696 expressed as "& id-expression" where the & is optional if the name
5697 refers to a function or array, or if the corresponding
5698 template-parameter is a reference.
5700 Here, we do not care about functions, as they are invalid anyway
5701 for a parameter of type pointer-to-object. */
5703 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5704 /* Non-type template parameters are OK. */
5706 else if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5707 /* Null pointer values are OK in C++11. */;
5708 else if (TREE_CODE (expr
) != ADDR_EXPR
5709 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5713 error ("%qD is not a valid template argument "
5714 "because %qD is a variable, not the address of "
5719 if (POINTER_TYPE_P (expr_type
))
5721 error ("%qE is not a valid template argument for %qT "
5722 "because it is not the address of a variable",
5726 /* Other values, like integer constants, might be valid
5727 non-type arguments of some other type. */
5728 return error_mark_node
;
5734 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5735 ? TREE_OPERAND (expr
, 0) : expr
);
5738 error ("%qE is not a valid template argument of type %qT "
5739 "because %qE is not a variable",
5743 else if (cxx_dialect
< cxx11
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5745 error ("%qE is not a valid template argument of type %qT "
5746 "because %qD does not have external linkage",
5750 else if (cxx_dialect
>= cxx11
&& decl_linkage (decl
) == lk_none
)
5752 error ("%qE is not a valid template argument of type %qT "
5753 "because %qD has no linkage",
5759 expr
= decay_conversion (expr
, complain
);
5760 if (expr
== error_mark_node
)
5761 return error_mark_node
;
5763 expr
= perform_qualification_conversions (type
, expr
);
5764 if (expr
== error_mark_node
)
5765 return error_mark_node
;
5767 /* [temp.arg.nontype]/5, bullet 3
5769 For a non-type template-parameter of type reference to object, no
5770 conversions apply. The type referred to by the reference may be more
5771 cv-qualified than the (otherwise identical) type of the
5772 template-argument. The template-parameter is bound directly to the
5773 template-argument, which must be an lvalue. */
5774 else if (TYPE_REF_OBJ_P (type
))
5776 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5778 return error_mark_node
;
5780 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5782 error ("%qE is not a valid template argument for type %qT "
5783 "because of conflicts in cv-qualification", expr
, type
);
5787 if (!real_lvalue_p (expr
))
5789 error ("%qE is not a valid template argument for type %qT "
5790 "because it is not an lvalue", expr
, type
);
5794 /* [temp.arg.nontype]/1
5796 A template-argument for a non-type, non-template template-parameter
5797 shall be one of: [...]
5799 -- the address of an object or function with external linkage. */
5800 if (INDIRECT_REF_P (expr
)
5801 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5803 expr
= TREE_OPERAND (expr
, 0);
5806 error ("%q#D is not a valid template argument for type %qT "
5807 "because a reference variable does not have a constant "
5808 "address", expr
, type
);
5815 error ("%qE is not a valid template argument for type %qT "
5816 "because it is not an object with external linkage",
5821 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5823 error ("%qE is not a valid template argument for type %qT "
5824 "because object %qD has not external linkage",
5829 expr
= build_nop (type
, build_address (expr
));
5831 /* [temp.arg.nontype]/5, bullet 4
5833 For a non-type template-parameter of type pointer to function, only
5834 the function-to-pointer conversion (_conv.func_) is applied. If the
5835 template-argument represents a set of overloaded functions (or a
5836 pointer to such), the matching function is selected from the set
5838 else if (TYPE_PTRFN_P (type
))
5840 /* If the argument is a template-id, we might not have enough
5841 context information to decay the pointer. */
5842 if (!type_unknown_p (expr_type
))
5844 expr
= decay_conversion (expr
, complain
);
5845 if (expr
== error_mark_node
)
5846 return error_mark_node
;
5849 if (cxx_dialect
>= cxx11
&& integer_zerop (expr
))
5850 /* Null pointer values are OK in C++11. */
5851 return perform_qualification_conversions (type
, expr
);
5853 expr
= convert_nontype_argument_function (type
, expr
);
5854 if (!expr
|| expr
== error_mark_node
)
5857 /* [temp.arg.nontype]/5, bullet 5
5859 For a non-type template-parameter of type reference to function, no
5860 conversions apply. If the template-argument represents a set of
5861 overloaded functions, the matching function is selected from the set
5863 else if (TYPE_REFFN_P (type
))
5865 if (TREE_CODE (expr
) == ADDR_EXPR
)
5867 error ("%qE is not a valid template argument for type %qT "
5868 "because it is a pointer", expr
, type
);
5869 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5873 expr
= convert_nontype_argument_function (type
, expr
);
5874 if (!expr
|| expr
== error_mark_node
)
5877 expr
= build_nop (type
, build_address (expr
));
5879 /* [temp.arg.nontype]/5, bullet 6
5881 For a non-type template-parameter of type pointer to member function,
5882 no conversions apply. If the template-argument represents a set of
5883 overloaded member functions, the matching member function is selected
5884 from the set (_over.over_). */
5885 else if (TYPE_PTRMEMFUNC_P (type
))
5887 expr
= instantiate_type (type
, expr
, tf_none
);
5888 if (expr
== error_mark_node
)
5889 return error_mark_node
;
5891 /* [temp.arg.nontype] bullet 1 says the pointer to member
5892 expression must be a pointer-to-member constant. */
5893 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5894 return error_mark_node
;
5896 /* There is no way to disable standard conversions in
5897 resolve_address_of_overloaded_function (called by
5898 instantiate_type). It is possible that the call succeeded by
5899 converting &B::I to &D::I (where B is a base of D), so we need
5900 to reject this conversion here.
5902 Actually, even if there was a way to disable standard conversions,
5903 it would still be better to reject them here so that we can
5904 provide a superior diagnostic. */
5905 if (!same_type_p (TREE_TYPE (expr
), type
))
5907 error ("%qE is not a valid template argument for type %qT "
5908 "because it is of type %qT", expr
, type
,
5910 /* If we are just one standard conversion off, explain. */
5911 if (can_convert_standard (type
, TREE_TYPE (expr
), complain
))
5912 inform (input_location
,
5913 "standard conversions are not allowed in this context");
5917 /* [temp.arg.nontype]/5, bullet 7
5919 For a non-type template-parameter of type pointer to data member,
5920 qualification conversions (_conv.qual_) are applied. */
5921 else if (TYPE_PTRDATAMEM_P (type
))
5923 /* [temp.arg.nontype] bullet 1 says the pointer to member
5924 expression must be a pointer-to-member constant. */
5925 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5926 return error_mark_node
;
5928 expr
= perform_qualification_conversions (type
, expr
);
5929 if (expr
== error_mark_node
)
5932 else if (NULLPTR_TYPE_P (type
))
5934 if (expr
!= nullptr_node
)
5936 error ("%qE is not a valid template argument for type %qT "
5937 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5942 /* A template non-type parameter must be one of the above. */
5946 /* Sanity check: did we actually convert the argument to the
5948 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5949 (type
, TREE_TYPE (expr
)));
5953 /* Subroutine of coerce_template_template_parms, which returns 1 if
5954 PARM_PARM and ARG_PARM match using the rule for the template
5955 parameters of template template parameters. Both PARM and ARG are
5956 template parameters; the rest of the arguments are the same as for
5957 coerce_template_template_parms.
5960 coerce_template_template_parm (tree parm
,
5962 tsubst_flags_t complain
,
5966 if (arg
== NULL_TREE
|| arg
== error_mark_node
5967 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5970 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5973 switch (TREE_CODE (parm
))
5976 /* We encounter instantiations of templates like
5977 template <template <template <class> class> class TT>
5980 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5981 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5983 if (!coerce_template_template_parms
5984 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5990 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5991 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5992 /* Argument is a parameter pack but parameter is not. */
5997 /* The tsubst call is used to handle cases such as
5999 template <int> class C {};
6000 template <class T, template <T> class TT> class D {};
6003 i.e. the parameter list of TT depends on earlier parameters. */
6004 if (!uses_template_parms (TREE_TYPE (arg
))
6006 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6010 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6011 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6012 /* Argument is a parameter pack but parameter is not. */
6025 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6026 template template parameters. Both PARM_PARMS and ARG_PARMS are
6027 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6030 Consider the example:
6031 template <class T> class A;
6032 template<template <class U> class TT> class B;
6034 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6035 the parameters to A, and OUTER_ARGS contains A. */
6038 coerce_template_template_parms (tree parm_parms
,
6040 tsubst_flags_t complain
,
6044 int nparms
, nargs
, i
;
6048 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6049 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6051 nparms
= TREE_VEC_LENGTH (parm_parms
);
6052 nargs
= TREE_VEC_LENGTH (arg_parms
);
6054 /* Determine whether we have a parameter pack at the end of the
6055 template template parameter's template parameter list. */
6056 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6058 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6060 if (parm
== error_mark_node
)
6063 switch (TREE_CODE (parm
))
6067 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6072 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6082 && !(variadic_p
&& nargs
>= nparms
- 1))
6085 /* Check all of the template parameters except the parameter pack at
6086 the end (if any). */
6087 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6089 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6090 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6093 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6094 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6096 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6104 /* Check each of the template parameters in the template
6105 argument against the template parameter pack at the end of
6106 the template template parameter. */
6107 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6110 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6112 for (; i
< nargs
; ++i
)
6114 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6117 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6119 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6128 /* Verifies that the deduced template arguments (in TARGS) for the
6129 template template parameters (in TPARMS) represent valid bindings,
6130 by comparing the template parameter list of each template argument
6131 to the template parameter list of its corresponding template
6132 template parameter, in accordance with DR150. This
6133 routine can only be called after all template arguments have been
6134 deduced. It will return TRUE if all of the template template
6135 parameter bindings are okay, FALSE otherwise. */
6137 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6139 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6142 /* We're dealing with template parms in this process. */
6143 ++processing_template_decl
;
6145 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6147 for (i
= 0; i
< ntparms
; ++i
)
6149 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6150 tree targ
= TREE_VEC_ELT (targs
, i
);
6152 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6154 tree packed_args
= NULL_TREE
;
6157 if (ARGUMENT_PACK_P (targ
))
6159 /* Look inside the argument pack. */
6160 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6161 len
= TREE_VEC_LENGTH (packed_args
);
6164 for (idx
= 0; idx
< len
; ++idx
)
6166 tree targ_parms
= NULL_TREE
;
6169 /* Extract the next argument from the argument
6171 targ
= TREE_VEC_ELT (packed_args
, idx
);
6173 if (PACK_EXPANSION_P (targ
))
6174 /* Look at the pattern of the pack expansion. */
6175 targ
= PACK_EXPANSION_PATTERN (targ
);
6177 /* Extract the template parameters from the template
6179 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6180 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6181 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6182 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6184 /* Verify that we can coerce the template template
6185 parameters from the template argument to the template
6186 parameter. This requires an exact match. */
6188 && !coerce_template_template_parms
6189 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6204 --processing_template_decl
;
6208 /* Since type attributes aren't mangled, we need to strip them from
6209 template type arguments. */
6212 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6215 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6217 mv
= TYPE_MAIN_VARIANT (arg
);
6218 arg
= strip_typedefs (arg
);
6219 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6220 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6222 if (complain
& tf_warning
)
6223 warning (0, "ignoring attributes on template argument %qT", arg
);
6224 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6225 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6230 /* Convert the indicated template ARG as necessary to match the
6231 indicated template PARM. Returns the converted ARG, or
6232 error_mark_node if the conversion was unsuccessful. Error and
6233 warning messages are issued under control of COMPLAIN. This
6234 conversion is for the Ith parameter in the parameter list. ARGS is
6235 the full set of template arguments deduced so far. */
6238 convert_template_argument (tree parm
,
6241 tsubst_flags_t complain
,
6247 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6249 if (TREE_CODE (arg
) == TREE_LIST
6250 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6252 /* The template argument was the name of some
6253 member function. That's usually
6254 invalid, but static members are OK. In any
6255 case, grab the underlying fields/functions
6256 and issue an error later if required. */
6257 orig_arg
= TREE_VALUE (arg
);
6258 TREE_TYPE (arg
) = unknown_type_node
;
6263 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6264 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6265 || requires_tmpl_type
);
6267 /* When determining whether an argument pack expansion is a template,
6268 look at the pattern. */
6269 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6270 arg
= PACK_EXPANSION_PATTERN (arg
);
6272 /* Deal with an injected-class-name used as a template template arg. */
6273 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6275 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6276 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6278 if (cxx_dialect
>= cxx11
)
6279 /* OK under DR 1004. */;
6280 else if (complain
& tf_warning_or_error
)
6281 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6282 " used as template template argument", TYPE_NAME (arg
));
6283 else if (flag_pedantic_errors
)
6291 ((TREE_CODE (arg
) == TEMPLATE_DECL
6292 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6293 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6294 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6295 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6298 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6299 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6300 arg
= TYPE_STUB_DECL (arg
);
6302 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6304 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6305 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6307 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6309 if (complain
& tf_error
)
6310 error ("invalid use of destructor %qE as a type", orig_arg
);
6311 return error_mark_node
;
6314 permerror (input_location
,
6315 "to refer to a type member of a template parameter, "
6316 "use %<typename %E%>", orig_arg
);
6318 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6319 TREE_OPERAND (arg
, 1),
6325 if (is_type
!= requires_type
)
6329 if (complain
& tf_error
)
6331 error ("type/value mismatch at argument %d in template "
6332 "parameter list for %qD",
6335 error (" expected a constant of type %qT, got %qT",
6337 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6338 else if (requires_tmpl_type
)
6339 error (" expected a class template, got %qE", orig_arg
);
6341 error (" expected a type, got %qE", orig_arg
);
6344 return error_mark_node
;
6346 if (is_tmpl_type
^ requires_tmpl_type
)
6348 if (in_decl
&& (complain
& tf_error
))
6350 error ("type/value mismatch at argument %d in template "
6351 "parameter list for %qD",
6354 error (" expected a type, got %qT", DECL_NAME (arg
));
6356 error (" expected a class template, got %qT", orig_arg
);
6358 return error_mark_node
;
6363 if (requires_tmpl_type
)
6365 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6367 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6368 /* The number of argument required is not known yet.
6369 Just accept it for now. */
6370 val
= TREE_TYPE (arg
);
6373 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6376 /* Strip alias templates that are equivalent to another
6378 arg
= get_underlying_template (arg
);
6379 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6381 if (coerce_template_template_parms (parmparm
, argparm
,
6387 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6389 if (val
!= error_mark_node
)
6391 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6392 val
= TREE_TYPE (val
);
6393 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6394 val
= make_pack_expansion (val
);
6399 if (in_decl
&& (complain
& tf_error
))
6401 error ("type/value mismatch at argument %d in "
6402 "template parameter list for %qD",
6404 error (" expected a template of type %qD, got %qT",
6408 val
= error_mark_node
;
6414 /* We only form one instance of each template specialization.
6415 Therefore, if we use a non-canonical variant (i.e., a
6416 typedef), any future messages referring to the type will use
6417 the typedef, which is confusing if those future uses do not
6418 themselves also use the typedef. */
6420 val
= canonicalize_type_argument (val
, complain
);
6424 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6426 if (invalid_nontype_parm_type_p (t
, complain
))
6427 return error_mark_node
;
6429 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6431 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6435 /* Not sure if this is reachable, but it doesn't hurt
6437 error ("type mismatch in nontype parameter pack");
6438 val
= error_mark_node
;
6441 else if (!dependent_template_arg_p (orig_arg
)
6442 && !uses_template_parms (t
))
6443 /* We used to call digest_init here. However, digest_init
6444 will report errors, which we don't want when complain
6445 is zero. More importantly, digest_init will try too
6446 hard to convert things: for example, `0' should not be
6447 converted to pointer type at this point according to
6448 the standard. Accepting this is not merely an
6449 extension, since deciding whether or not these
6450 conversions can occur is part of determining which
6451 function template to call, or whether a given explicit
6452 argument specification is valid. */
6453 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6455 val
= strip_typedefs_expr (orig_arg
);
6457 if (val
== NULL_TREE
)
6458 val
= error_mark_node
;
6459 else if (val
== error_mark_node
&& (complain
& tf_error
))
6460 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6462 if (TREE_CODE (val
) == SCOPE_REF
)
6464 /* Strip typedefs from the SCOPE_REF. */
6465 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6466 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6468 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6469 QUALIFIED_NAME_IS_TEMPLATE (val
));
6476 /* Coerces the remaining template arguments in INNER_ARGS (from
6477 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6478 Returns the coerced argument pack. PARM_IDX is the position of this
6479 parameter in the template parameter list. ARGS is the original
6480 template argument list. */
6482 coerce_template_parameter_pack (tree parms
,
6490 tsubst_flags_t complain
)
6492 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6493 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6496 tree packed_types
= NULL_TREE
;
6498 if (arg_idx
> nargs
)
6501 packed_args
= make_tree_vec (nargs
- arg_idx
);
6503 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6504 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6506 /* When the template parameter is a non-type template
6507 parameter pack whose type uses parameter packs, we need
6508 to look at each of the template arguments
6509 separately. Build a vector of the types for these
6510 non-type template parameters in PACKED_TYPES. */
6512 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6513 packed_types
= tsubst_pack_expansion (expansion
, args
,
6516 if (packed_types
== error_mark_node
)
6517 return error_mark_node
;
6519 /* Check that we have the right number of arguments. */
6521 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6522 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6525 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6526 error ("wrong number of template arguments (%d, should be %d)",
6527 nargs
, needed_parms
);
6528 return error_mark_node
;
6531 /* If we aren't able to check the actual arguments now
6532 (because they haven't been expanded yet), we can at least
6533 verify that all of the types used for the non-type
6534 template parameter pack are, in fact, valid for non-type
6535 template parameters. */
6537 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6539 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6540 for (j
= 0; j
< len
; ++j
)
6542 tree t
= TREE_VEC_ELT (packed_types
, j
);
6543 if (invalid_nontype_parm_type_p (t
, complain
))
6544 return error_mark_node
;
6549 /* Convert the remaining arguments, which will be a part of the
6550 parameter pack "parm". */
6551 for (; arg_idx
< nargs
; ++arg_idx
)
6553 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6554 tree actual_parm
= TREE_VALUE (parm
);
6556 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6558 /* When we have a vector of types (corresponding to the
6559 non-type template parameter pack that uses parameter
6560 packs in its type, as mention above), and the
6561 argument is not an expansion (which expands to a
6562 currently unknown number of arguments), clone the
6563 parm and give it the next type in PACKED_TYPES. */
6564 actual_parm
= copy_node (actual_parm
);
6565 TREE_TYPE (actual_parm
) =
6566 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6569 if (arg
== error_mark_node
)
6571 if (complain
& tf_error
)
6572 error ("template argument %d is invalid", arg_idx
+ 1);
6575 arg
= convert_template_argument (actual_parm
,
6576 arg
, new_args
, complain
, parm_idx
,
6578 if (arg
== error_mark_node
)
6580 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6583 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6584 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6585 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6588 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6589 TREE_TYPE (argument_pack
)
6590 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6591 TREE_CONSTANT (argument_pack
) = 1;
6594 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6595 #ifdef ENABLE_CHECKING
6596 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6597 TREE_VEC_LENGTH (packed_args
));
6599 return argument_pack
;
6602 /* Returns the number of pack expansions in the template argument vector
6606 pack_expansion_args_count (tree args
)
6611 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6613 tree elt
= TREE_VEC_ELT (args
, i
);
6614 if (elt
&& PACK_EXPANSION_P (elt
))
6620 /* Convert all template arguments to their appropriate types, and
6621 return a vector containing the innermost resulting template
6622 arguments. If any error occurs, return error_mark_node. Error and
6623 warning messages are issued under control of COMPLAIN.
6625 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6626 for arguments not specified in ARGS. Otherwise, if
6627 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6628 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6629 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6633 coerce_template_parms (tree parms
,
6636 tsubst_flags_t complain
,
6637 bool require_all_args
,
6638 bool use_default_args
)
6640 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6643 tree new_inner_args
;
6644 int saved_unevaluated_operand
;
6645 int saved_inhibit_evaluation_warnings
;
6647 /* When used as a boolean value, indicates whether this is a
6648 variadic template parameter list. Since it's an int, we can also
6649 subtract it from nparms to get the number of non-variadic
6652 int variadic_args_p
= 0;
6653 int post_variadic_parms
= 0;
6655 if (args
== error_mark_node
)
6656 return error_mark_node
;
6658 nparms
= TREE_VEC_LENGTH (parms
);
6660 /* Determine if there are any parameter packs. */
6661 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6663 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6665 ++post_variadic_parms
;
6666 if (template_parameter_pack_p (tparm
))
6670 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6671 /* If there are no parameters that follow a parameter pack, we need to
6672 expand any argument packs so that we can deduce a parameter pack from
6673 some non-packed args followed by an argument pack, as in variadic85.C.
6674 If there are such parameters, we need to leave argument packs intact
6675 so the arguments are assigned properly. This can happen when dealing
6676 with a nested class inside a partial specialization of a class
6677 template, as in variadic92.C, or when deducing a template parameter pack
6678 from a sub-declarator, as in variadic114.C. */
6679 if (!post_variadic_parms
)
6680 inner_args
= expand_template_argument_pack (inner_args
);
6682 /* Count any pack expansion args. */
6683 variadic_args_p
= pack_expansion_args_count (inner_args
);
6685 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6686 if ((nargs
> nparms
&& !variadic_p
)
6687 || (nargs
< nparms
- variadic_p
6690 && (!use_default_args
6691 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6692 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6694 if (complain
& tf_error
)
6698 nparms
-= variadic_p
;
6699 error ("wrong number of template arguments "
6700 "(%d, should be %d or more)", nargs
, nparms
);
6703 error ("wrong number of template arguments "
6704 "(%d, should be %d)", nargs
, nparms
);
6707 error ("provided for %q+D", in_decl
);
6710 return error_mark_node
;
6712 /* We can't pass a pack expansion to a non-pack parameter of an alias
6713 template (DR 1430). */
6714 else if (in_decl
&& DECL_ALIAS_TEMPLATE_P (in_decl
)
6716 && nargs
- variadic_args_p
< nparms
- variadic_p
)
6718 if (complain
& tf_error
)
6720 for (int i
= 0; i
< TREE_VEC_LENGTH (inner_args
); ++i
)
6722 tree arg
= TREE_VEC_ELT (inner_args
, i
);
6723 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
6725 if (PACK_EXPANSION_P (arg
)
6726 && !template_parameter_pack_p (parm
))
6728 error ("pack expansion argument for non-pack parameter "
6729 "%qD of alias template %qD", parm
, in_decl
);
6730 inform (DECL_SOURCE_LOCATION (parm
), "declared here");
6737 return error_mark_node
;
6740 /* We need to evaluate the template arguments, even though this
6741 template-id may be nested within a "sizeof". */
6742 saved_unevaluated_operand
= cp_unevaluated_operand
;
6743 cp_unevaluated_operand
= 0;
6744 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6745 c_inhibit_evaluation_warnings
= 0;
6746 new_inner_args
= make_tree_vec (nparms
);
6747 new_args
= add_outermost_template_args (args
, new_inner_args
);
6748 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6753 /* Get the Ith template parameter. */
6754 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6756 if (parm
== error_mark_node
)
6758 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6762 /* Calculate the next argument. */
6763 if (arg_idx
< nargs
)
6764 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6768 if (template_parameter_pack_p (TREE_VALUE (parm
))
6769 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6771 /* All remaining arguments will be placed in the
6772 template parameter pack PARM. */
6773 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6774 inner_args
, arg_idx
,
6778 /* Store this argument. */
6779 if (arg
== error_mark_node
)
6781 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6783 /* We are done with all of the arguments. */
6790 if (PACK_EXPANSION_P (arg
))
6792 /* We don't know how many args we have yet, just
6793 use the unconverted ones for now. */
6794 new_inner_args
= inner_args
;
6798 else if (require_all_args
)
6800 /* There must be a default arg in this case. */
6801 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6803 /* The position of the first default template argument,
6804 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6806 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6807 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6812 if (arg
== error_mark_node
)
6814 if (complain
& tf_error
)
6815 error ("template argument %d is invalid", arg_idx
+ 1);
6818 /* This only occurs if there was an error in the template
6819 parameter list itself (which we would already have
6820 reported) that we are trying to recover from, e.g., a class
6821 template with a parameter list such as
6822 template<typename..., typename>. */
6825 arg
= convert_template_argument (TREE_VALUE (parm
),
6826 arg
, new_args
, complain
,
6829 if (arg
== error_mark_node
)
6831 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6833 cp_unevaluated_operand
= saved_unevaluated_operand
;
6834 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6837 return error_mark_node
;
6839 #ifdef ENABLE_CHECKING
6840 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6841 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6842 TREE_VEC_LENGTH (new_inner_args
));
6845 return new_inner_args
;
6848 /* Like coerce_template_parms. If PARMS represents all template
6849 parameters levels, this function returns a vector of vectors
6850 representing all the resulting argument levels. Note that in this
6851 case, only the innermost arguments are coerced because the
6852 outermost ones are supposed to have been coerced already.
6854 Otherwise, if PARMS represents only (the innermost) vector of
6855 parameters, this function returns a vector containing just the
6856 innermost resulting arguments. */
6859 coerce_innermost_template_parms (tree parms
,
6862 tsubst_flags_t complain
,
6863 bool require_all_args
,
6864 bool use_default_args
)
6866 int parms_depth
= TMPL_PARMS_DEPTH (parms
);
6867 int args_depth
= TMPL_ARGS_DEPTH (args
);
6870 if (parms_depth
> 1)
6872 coerced_args
= make_tree_vec (parms_depth
);
6876 for (level
= parms
, cur_depth
= parms_depth
;
6877 parms_depth
> 0 && level
!= NULL_TREE
;
6878 level
= TREE_CHAIN (level
), --cur_depth
)
6881 if (cur_depth
== args_depth
)
6882 l
= coerce_template_parms (TREE_VALUE (level
),
6883 args
, in_decl
, complain
,
6887 l
= TMPL_ARGS_LEVEL (args
, cur_depth
);
6889 if (l
== error_mark_node
)
6890 return error_mark_node
;
6892 SET_TMPL_ARGS_LEVEL (coerced_args
, cur_depth
, l
);
6896 coerced_args
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms
),
6897 args
, in_decl
, complain
,
6900 return coerced_args
;
6903 /* Returns 1 if template args OT and NT are equivalent. */
6906 template_args_equal (tree ot
, tree nt
)
6910 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6913 if (TREE_CODE (nt
) == TREE_VEC
)
6914 /* For member templates */
6915 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6916 else if (PACK_EXPANSION_P (ot
))
6917 return (PACK_EXPANSION_P (nt
)
6918 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6919 PACK_EXPANSION_PATTERN (nt
))
6920 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6921 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6922 else if (ARGUMENT_PACK_P (ot
))
6927 if (!ARGUMENT_PACK_P (nt
))
6930 opack
= ARGUMENT_PACK_ARGS (ot
);
6931 npack
= ARGUMENT_PACK_ARGS (nt
);
6932 len
= TREE_VEC_LENGTH (opack
);
6933 if (TREE_VEC_LENGTH (npack
) != len
)
6935 for (i
= 0; i
< len
; ++i
)
6936 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6937 TREE_VEC_ELT (npack
, i
)))
6941 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6943 /* We get here probably because we are in the middle of substituting
6944 into the pattern of a pack expansion. In that case the
6945 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6946 interested in. So we want to use the initial pack argument for
6948 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6949 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6950 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6951 return template_args_equal (ot
, nt
);
6953 else if (TYPE_P (nt
))
6954 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6955 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6958 return cp_tree_equal (ot
, nt
);
6961 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6962 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6963 NEWARG_PTR with the offending arguments if they are non-NULL. */
6966 comp_template_args_with_info (tree oldargs
, tree newargs
,
6967 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6971 if (oldargs
== newargs
)
6974 if (!oldargs
|| !newargs
)
6977 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6980 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6982 tree nt
= TREE_VEC_ELT (newargs
, i
);
6983 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6985 if (! template_args_equal (ot
, nt
))
6987 if (oldarg_ptr
!= NULL
)
6989 if (newarg_ptr
!= NULL
)
6997 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6998 of template arguments. Returns 0 otherwise. */
7001 comp_template_args (tree oldargs
, tree newargs
)
7003 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7007 add_pending_template (tree d
)
7009 tree ti
= (TYPE_P (d
)
7010 ? CLASSTYPE_TEMPLATE_INFO (d
)
7011 : DECL_TEMPLATE_INFO (d
));
7012 struct pending_template
*pt
;
7015 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7018 /* We are called both from instantiate_decl, where we've already had a
7019 tinst_level pushed, and instantiate_template, where we haven't.
7021 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7024 push_tinst_level (d
);
7026 pt
= ggc_alloc_pending_template ();
7028 pt
->tinst
= current_tinst_level
;
7029 if (last_pending_template
)
7030 last_pending_template
->next
= pt
;
7032 pending_templates
= pt
;
7034 last_pending_template
= pt
;
7036 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7043 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7044 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7045 documentation for TEMPLATE_ID_EXPR. */
7048 lookup_template_function (tree fns
, tree arglist
)
7052 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7053 return error_mark_node
;
7055 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7057 if (!is_overloaded_fn (fns
) && !identifier_p (fns
))
7059 error ("%q#D is not a function template", fns
);
7060 return error_mark_node
;
7063 if (BASELINK_P (fns
))
7065 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7067 BASELINK_FUNCTIONS (fns
),
7072 type
= TREE_TYPE (fns
);
7073 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7074 type
= unknown_type_node
;
7076 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7079 /* Within the scope of a template class S<T>, the name S gets bound
7080 (in build_self_reference) to a TYPE_DECL for the class, not a
7081 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7082 or one of its enclosing classes, and that type is a template,
7083 return the associated TEMPLATE_DECL. Otherwise, the original
7086 Also handle the case when DECL is a TREE_LIST of ambiguous
7087 injected-class-names from different bases. */
7090 maybe_get_template_decl_from_type_decl (tree decl
)
7092 if (decl
== NULL_TREE
)
7095 /* DR 176: A lookup that finds an injected-class-name (10.2
7096 [class.member.lookup]) can result in an ambiguity in certain cases
7097 (for example, if it is found in more than one base class). If all of
7098 the injected-class-names that are found refer to specializations of
7099 the same class template, and if the name is followed by a
7100 template-argument-list, the reference refers to the class template
7101 itself and not a specialization thereof, and is not ambiguous. */
7102 if (TREE_CODE (decl
) == TREE_LIST
)
7104 tree t
, tmpl
= NULL_TREE
;
7105 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7107 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7110 else if (tmpl
!= elt
)
7113 if (tmpl
&& t
== NULL_TREE
)
7119 return (decl
!= NULL_TREE
7120 && DECL_SELF_REFERENCE_P (decl
)
7121 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7122 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7125 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7126 parameters, find the desired type.
7128 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7130 IN_DECL, if non-NULL, is the template declaration we are trying to
7133 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7134 the class we are looking up.
7136 Issue error and warning messages under control of COMPLAIN.
7138 If the template class is really a local class in a template
7139 function, then the FUNCTION_CONTEXT is the function in which it is
7142 ??? Note that this function is currently called *twice* for each
7143 template-id: the first time from the parser, while creating the
7144 incomplete type (finish_template_type), and the second type during the
7145 real instantiation (instantiate_template_class). This is surely something
7146 that we want to avoid. It also causes some problems with argument
7147 coercion (see convert_nontype_argument for more information on this). */
7150 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7151 int entering_scope
, tsubst_flags_t complain
)
7153 tree templ
= NULL_TREE
, parmlist
;
7160 if (identifier_p (d1
))
7162 tree value
= innermost_non_namespace_value (d1
);
7163 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7168 push_decl_namespace (context
);
7169 templ
= lookup_name (d1
);
7170 templ
= maybe_get_template_decl_from_type_decl (templ
);
7172 pop_decl_namespace ();
7175 context
= DECL_CONTEXT (templ
);
7177 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7179 tree type
= TREE_TYPE (d1
);
7181 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7182 an implicit typename for the second A. Deal with it. */
7183 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7184 type
= TREE_TYPE (type
);
7186 if (CLASSTYPE_TEMPLATE_INFO (type
))
7188 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7189 d1
= DECL_NAME (templ
);
7192 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7193 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7195 templ
= TYPE_TI_TEMPLATE (d1
);
7196 d1
= DECL_NAME (templ
);
7198 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7199 && DECL_TEMPLATE_RESULT (d1
)
7200 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7203 d1
= DECL_NAME (templ
);
7204 context
= DECL_CONTEXT (templ
);
7206 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1
))
7209 d1
= DECL_NAME (templ
);
7212 /* Issue an error message if we didn't find a template. */
7215 if (complain
& tf_error
)
7216 error ("%qT is not a template", d1
);
7217 return error_mark_node
;
7220 if (TREE_CODE (templ
) != TEMPLATE_DECL
7221 /* Make sure it's a user visible template, if it was named by
7223 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7224 && !PRIMARY_TEMPLATE_P (templ
)))
7226 if (complain
& tf_error
)
7228 error ("non-template type %qT used as a template", d1
);
7230 error ("for template declaration %q+D", in_decl
);
7232 return error_mark_node
;
7235 complain
&= ~tf_user
;
7237 /* An alias that just changes the name of a template is equivalent to the
7238 other template, so if any of the arguments are pack expansions, strip
7239 the alias to avoid problems with a pack expansion passed to a non-pack
7240 alias template parameter (DR 1430). */
7241 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist
)))
7242 templ
= get_underlying_template (templ
);
7244 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7246 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7247 template arguments */
7253 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7255 /* Consider an example where a template template parameter declared as
7257 template <class T, class U = std::allocator<T> > class TT
7259 The template parameter level of T and U are one level larger than
7260 of TT. To proper process the default argument of U, say when an
7261 instantiation `TT<int>' is seen, we need to build the full
7262 arguments containing {int} as the innermost level. Outer levels,
7263 available when not appearing as default template argument, can be
7264 obtained from the arguments of the enclosing template.
7266 Suppose that TT is later substituted with std::vector. The above
7267 instantiation is `TT<int, std::allocator<T> >' with TT at
7268 level 1, and T at level 2, while the template arguments at level 1
7269 becomes {std::vector} and the inner level 2 is {int}. */
7271 outer
= DECL_CONTEXT (templ
);
7273 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7274 else if (current_template_parms
)
7275 /* This is an argument of the current template, so we haven't set
7276 DECL_CONTEXT yet. */
7277 outer
= current_template_args ();
7280 arglist
= add_to_template_args (outer
, arglist
);
7282 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7284 /*require_all_args=*/true,
7285 /*use_default_args=*/true);
7286 if (arglist2
== error_mark_node
7287 || (!uses_template_parms (arglist2
)
7288 && check_instantiated_args (templ
, arglist2
, complain
)))
7289 return error_mark_node
;
7291 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7296 tree template_type
= TREE_TYPE (templ
);
7299 tree found
= NULL_TREE
;
7302 int is_dependent_type
;
7303 int use_partial_inst_tmpl
= false;
7305 if (template_type
== error_mark_node
)
7306 /* An error occurred while building the template TEMPL, and a
7307 diagnostic has most certainly been emitted for that
7308 already. Let's propagate that error. */
7309 return error_mark_node
;
7311 gen_tmpl
= most_general_template (templ
);
7312 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7313 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7314 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7316 if (arg_depth
== 1 && parm_depth
> 1)
7318 /* We've been given an incomplete set of template arguments.
7321 template <class T> struct S1 {
7322 template <class U> struct S2 {};
7323 template <class U> struct S2<U*> {};
7326 we will be called with an ARGLIST of `U*', but the
7327 TEMPLATE will be `template <class T> template
7328 <class U> struct S1<T>::S2'. We must fill in the missing
7331 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7333 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7336 /* Now we should have enough arguments. */
7337 gcc_assert (parm_depth
== arg_depth
);
7339 /* From here on, we're only interested in the most general
7342 /* Calculate the BOUND_ARGS. These will be the args that are
7343 actually tsubst'd into the definition to create the
7347 /* We have multiple levels of arguments to coerce, at once. */
7349 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7351 tree bound_args
= make_tree_vec (parm_depth
);
7353 for (i
= saved_depth
,
7354 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7355 i
> 0 && t
!= NULL_TREE
;
7356 --i
, t
= TREE_CHAIN (t
))
7359 if (i
== saved_depth
)
7360 a
= coerce_template_parms (TREE_VALUE (t
),
7363 /*require_all_args=*/true,
7364 /*use_default_args=*/true);
7366 /* Outer levels should have already been coerced. */
7367 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7369 /* Don't process further if one of the levels fails. */
7370 if (a
== error_mark_node
)
7372 /* Restore the ARGLIST to its full size. */
7373 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7374 return error_mark_node
;
7377 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7379 /* We temporarily reduce the length of the ARGLIST so
7380 that coerce_template_parms will see only the arguments
7381 corresponding to the template parameters it is
7383 TREE_VEC_LENGTH (arglist
)--;
7386 /* Restore the ARGLIST to its full size. */
7387 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7389 arglist
= bound_args
;
7393 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7394 INNERMOST_TEMPLATE_ARGS (arglist
),
7397 /*require_all_args=*/true,
7398 /*use_default_args=*/true);
7400 if (arglist
== error_mark_node
)
7401 /* We were unable to bind the arguments. */
7402 return error_mark_node
;
7404 /* In the scope of a template class, explicit references to the
7405 template class refer to the type of the template, not any
7406 instantiation of it. For example, in:
7408 template <class T> class C { void f(C<T>); }
7410 the `C<T>' is just the same as `C'. Outside of the
7411 class, however, such a reference is an instantiation. */
7413 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7414 || currently_open_class (template_type
))
7415 /* comp_template_args is expensive, check it last. */
7416 && comp_template_args (TYPE_TI_ARGS (template_type
),
7418 return template_type
;
7420 /* If we already have this specialization, return it. */
7421 elt
.tmpl
= gen_tmpl
;
7423 hash
= hash_specialization (&elt
);
7424 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7430 is_dependent_type
= uses_template_parms (arglist
);
7432 /* If the deduced arguments are invalid, then the binding
7434 if (!is_dependent_type
7435 && check_instantiated_args (gen_tmpl
,
7436 INNERMOST_TEMPLATE_ARGS (arglist
),
7438 return error_mark_node
;
7440 if (!is_dependent_type
7441 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7442 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7443 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7445 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7446 DECL_NAME (gen_tmpl
),
7447 /*tag_scope=*/ts_global
);
7451 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7453 if (context
== error_mark_node
)
7454 return error_mark_node
;
7457 context
= global_namespace
;
7459 /* Create the type. */
7460 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7462 if (!is_dependent_type
)
7464 set_current_access_from_decl (TYPE_NAME (template_type
));
7465 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7466 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7467 arglist
, complain
, in_decl
),
7468 SCOPED_ENUM_P (template_type
), NULL
);
7472 /* We don't want to call start_enum for this type, since
7473 the values for the enumeration constants may involve
7474 template parameters. And, no one should be interested
7475 in the enumeration constants for such a type. */
7476 t
= cxx_make_type (ENUMERAL_TYPE
);
7477 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7479 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7480 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7481 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7483 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7485 /* The user referred to a specialization of an alias
7486 template represented by GEN_TMPL.
7488 [temp.alias]/2 says:
7490 When a template-id refers to the specialization of an
7491 alias template, it is equivalent to the associated
7492 type obtained by substitution of its
7493 template-arguments for the template-parameters in the
7494 type-id of the alias template. */
7496 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7497 /* Note that the call above (by indirectly calling
7498 register_specialization in tsubst_decl) registers the
7499 TYPE_DECL representing the specialization of the alias
7500 template. So next time someone substitutes ARGLIST for
7501 the template parms into the alias template (GEN_TMPL),
7502 she'll get that TYPE_DECL back. */
7504 if (t
== error_mark_node
)
7507 else if (CLASS_TYPE_P (template_type
))
7509 t
= make_class_type (TREE_CODE (template_type
));
7510 CLASSTYPE_DECLARED_CLASS (t
)
7511 = CLASSTYPE_DECLARED_CLASS (template_type
);
7512 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7513 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7515 /* A local class. Make sure the decl gets registered properly. */
7516 if (context
== current_function_decl
)
7517 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7519 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7520 /* This instantiation is another name for the primary
7521 template type. Set the TYPE_CANONICAL field
7523 TYPE_CANONICAL (t
) = template_type
;
7524 else if (any_template_arguments_need_structural_equality_p (arglist
))
7525 /* Some of the template arguments require structural
7526 equality testing, so this template class requires
7527 structural equality testing. */
7528 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7533 /* If we called start_enum or pushtag above, this information
7534 will already be set up. */
7537 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7539 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7540 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7541 DECL_SOURCE_LOCATION (type_decl
)
7542 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7545 type_decl
= TYPE_NAME (t
);
7547 if (CLASS_TYPE_P (template_type
))
7549 TREE_PRIVATE (type_decl
)
7550 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type
));
7551 TREE_PROTECTED (type_decl
)
7552 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type
));
7553 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7555 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7556 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7560 /* Let's consider the explicit specialization of a member
7561 of a class template specialization that is implicitely instantiated,
7566 template<class U> struct M {}; //#0
7571 struct S<int>::M<char> //#1
7575 [temp.expl.spec]/4 says this is valid.
7577 In this case, when we write:
7580 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7583 When we encounter #1, we want to store the partial instantiation
7584 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7586 For all cases other than this "explicit specialization of member of a
7587 class template", we just want to store the most general template into
7588 the CLASSTYPE_TI_TEMPLATE of M.
7590 This case of "explicit specialization of member of a class template"
7592 1/ the enclosing class is an instantiation of, and therefore not
7593 the same as, the context of the most general template, and
7594 2/ we aren't looking at the partial instantiation itself, i.e.
7595 the innermost arguments are not the same as the innermost parms of
7596 the most general template.
7598 So it's only when 1/ and 2/ happens that we want to use the partial
7599 instantiation of the member template in lieu of its most general
7602 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7603 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7604 /* the enclosing class must be an instantiation... */
7605 && CLASS_TYPE_P (context
)
7606 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7608 tree partial_inst_args
;
7609 TREE_VEC_LENGTH (arglist
)--;
7610 ++processing_template_decl
;
7612 tsubst (INNERMOST_TEMPLATE_ARGS
7613 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7614 arglist
, complain
, NULL_TREE
);
7615 --processing_template_decl
;
7616 TREE_VEC_LENGTH (arglist
)++;
7617 use_partial_inst_tmpl
=
7618 /*...and we must not be looking at the partial instantiation
7620 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7624 if (!use_partial_inst_tmpl
)
7625 /* This case is easy; there are no member templates involved. */
7629 /* This is a full instantiation of a member template. Find
7630 the partial instantiation of which this is an instance. */
7632 /* Temporarily reduce by one the number of levels in the ARGLIST
7633 so as to avoid comparing the last set of arguments. */
7634 TREE_VEC_LENGTH (arglist
)--;
7635 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7636 TREE_VEC_LENGTH (arglist
)++;
7637 /* FOUND is either a proper class type, or an alias
7638 template specialization. In the later case, it's a
7639 TYPE_DECL, resulting from the substituting of arguments
7640 for parameters in the TYPE_DECL of the alias template
7641 done earlier. So be careful while getting the template
7643 found
= TREE_CODE (found
) == TYPE_DECL
7644 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7645 : CLASSTYPE_TI_TEMPLATE (found
);
7648 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7651 slot
= htab_find_slot_with_hash (type_specializations
,
7652 &elt
, hash
, INSERT
);
7653 entry
= ggc_alloc_spec_entry ();
7657 /* Note this use of the partial instantiation so we can check it
7658 later in maybe_process_partial_specialization. */
7659 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7660 = tree_cons (arglist
, t
,
7661 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7663 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7664 /* Now that the type has been registered on the instantiations
7665 list, we set up the enumerators. Because the enumeration
7666 constants may involve the enumeration type itself, we make
7667 sure to register the type first, and then create the
7668 constants. That way, doing tsubst_expr for the enumeration
7669 constants won't result in recursive calls here; we'll find
7670 the instantiation and exit above. */
7671 tsubst_enum (template_type
, t
, arglist
);
7673 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7674 /* If the type makes use of template parameters, the
7675 code that generates debugging information will crash. */
7676 DECL_IGNORED_P (TYPE_MAIN_DECL (t
)) = 1;
7678 /* Possibly limit visibility based on template args. */
7679 TREE_PUBLIC (type_decl
) = 1;
7680 determine_visibility (type_decl
);
7686 /* Wrapper for lookup_template_class_1. */
7689 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7690 int entering_scope
, tsubst_flags_t complain
)
7693 timevar_push (TV_TEMPLATE_INST
);
7694 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7695 entering_scope
, complain
);
7696 timevar_pop (TV_TEMPLATE_INST
);
7704 /* True when we should also visit template parameters that occur in
7705 non-deduced contexts. */
7706 bool include_nondeduced_p
;
7707 struct pointer_set_t
*visited
;
7710 /* Called from for_each_template_parm via walk_tree. */
7713 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7716 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7717 tree_fn_t fn
= pfd
->fn
;
7718 void *data
= pfd
->data
;
7721 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7722 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7723 pfd
->include_nondeduced_p
))
7724 return error_mark_node
;
7726 switch (TREE_CODE (t
))
7729 if (TYPE_PTRMEMFUNC_P (t
))
7735 if (!TYPE_TEMPLATE_INFO (t
))
7737 else if (for_each_template_parm (TYPE_TI_ARGS (t
),
7738 fn
, data
, pfd
->visited
,
7739 pfd
->include_nondeduced_p
))
7740 return error_mark_node
;
7744 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7745 fn
, data
, pfd
->visited
,
7746 pfd
->include_nondeduced_p
)
7747 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7748 fn
, data
, pfd
->visited
,
7749 pfd
->include_nondeduced_p
))
7750 return error_mark_node
;
7754 /* Since we're not going to walk subtrees, we have to do this
7756 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7757 pfd
->visited
, pfd
->include_nondeduced_p
))
7758 return error_mark_node
;
7762 /* Check the return type. */
7763 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7764 pfd
->include_nondeduced_p
))
7765 return error_mark_node
;
7767 /* Check the parameter types. Since default arguments are not
7768 instantiated until they are needed, the TYPE_ARG_TYPES may
7769 contain expressions that involve template parameters. But,
7770 no-one should be looking at them yet. And, once they're
7771 instantiated, they don't contain template parameters, so
7772 there's no point in looking at them then, either. */
7776 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7777 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7778 pfd
->visited
, pfd
->include_nondeduced_p
))
7779 return error_mark_node
;
7781 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7782 want walk_tree walking into them itself. */
7788 case UNDERLYING_TYPE
:
7789 if (pfd
->include_nondeduced_p
7790 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7792 pfd
->include_nondeduced_p
))
7793 return error_mark_node
;
7798 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7799 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7800 pfd
->visited
, pfd
->include_nondeduced_p
))
7801 return error_mark_node
;
7806 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7807 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7808 pfd
->visited
, pfd
->include_nondeduced_p
))
7809 return error_mark_node
;
7810 if (DECL_CONTEXT (t
)
7811 && pfd
->include_nondeduced_p
7812 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7813 pfd
->visited
, pfd
->include_nondeduced_p
))
7814 return error_mark_node
;
7817 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7818 /* Record template parameters such as `T' inside `TT<T>'. */
7819 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7820 pfd
->include_nondeduced_p
))
7821 return error_mark_node
;
7824 case TEMPLATE_TEMPLATE_PARM
:
7825 case TEMPLATE_TYPE_PARM
:
7826 case TEMPLATE_PARM_INDEX
:
7827 if (fn
&& (*fn
)(t
, data
))
7828 return error_mark_node
;
7830 return error_mark_node
;
7834 /* A template template parameter is encountered. */
7835 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7836 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7837 pfd
->include_nondeduced_p
))
7838 return error_mark_node
;
7840 /* Already substituted template template parameter */
7846 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7848 pfd
->include_nondeduced_p
))
7849 return error_mark_node
;
7853 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7854 && pfd
->include_nondeduced_p
7855 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7856 (TREE_TYPE (t
)), fn
, data
,
7857 pfd
->visited
, pfd
->include_nondeduced_p
))
7858 return error_mark_node
;
7863 /* If there's no type, then this thing must be some expression
7864 involving template parameters. */
7865 if (!fn
&& !TREE_TYPE (t
))
7866 return error_mark_node
;
7871 case IMPLICIT_CONV_EXPR
:
7872 case REINTERPRET_CAST_EXPR
:
7873 case CONST_CAST_EXPR
:
7874 case STATIC_CAST_EXPR
:
7875 case DYNAMIC_CAST_EXPR
:
7879 case PSEUDO_DTOR_EXPR
:
7881 return error_mark_node
;
7888 /* We didn't find any template parameters we liked. */
7892 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7893 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7894 call FN with the parameter and the DATA.
7895 If FN returns nonzero, the iteration is terminated, and
7896 for_each_template_parm returns 1. Otherwise, the iteration
7897 continues. If FN never returns a nonzero value, the value
7898 returned by for_each_template_parm is 0. If FN is NULL, it is
7899 considered to be the function which always returns 1.
7901 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7902 parameters that occur in non-deduced contexts. When false, only
7903 visits those template parameters that can be deduced. */
7906 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7907 struct pointer_set_t
*visited
,
7908 bool include_nondeduced_p
)
7910 struct pair_fn_data pfd
;
7916 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7918 /* Walk the tree. (Conceptually, we would like to walk without
7919 duplicates, but for_each_template_parm_r recursively calls
7920 for_each_template_parm, so we would need to reorganize a fair
7921 bit to use walk_tree_without_duplicates, so we keep our own
7924 pfd
.visited
= visited
;
7926 pfd
.visited
= pointer_set_create ();
7927 result
= cp_walk_tree (&t
,
7928 for_each_template_parm_r
,
7930 pfd
.visited
) != NULL_TREE
;
7935 pointer_set_destroy (pfd
.visited
);
7942 /* Returns true if T depends on any template parameter. */
7945 uses_template_parms (tree t
)
7948 int saved_processing_template_decl
;
7950 saved_processing_template_decl
= processing_template_decl
;
7951 if (!saved_processing_template_decl
)
7952 processing_template_decl
= 1;
7954 dependent_p
= dependent_type_p (t
);
7955 else if (TREE_CODE (t
) == TREE_VEC
)
7956 dependent_p
= any_dependent_template_arguments_p (t
);
7957 else if (TREE_CODE (t
) == TREE_LIST
)
7958 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7959 || uses_template_parms (TREE_CHAIN (t
)));
7960 else if (TREE_CODE (t
) == TYPE_DECL
)
7961 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7964 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7965 || TREE_CODE (t
) == OVERLOAD
7968 || TREE_CODE (t
) == TRAIT_EXPR
7969 || TREE_CODE (t
) == CONSTRUCTOR
7970 || CONSTANT_CLASS_P (t
))
7971 dependent_p
= (type_dependent_expression_p (t
)
7972 || value_dependent_expression_p (t
));
7975 gcc_assert (t
== error_mark_node
);
7976 dependent_p
= false;
7979 processing_template_decl
= saved_processing_template_decl
;
7984 /* Returns true iff current_function_decl is an incompletely instantiated
7985 template. Useful instead of processing_template_decl because the latter
7986 is set to 0 during fold_non_dependent_expr. */
7989 in_template_function (void)
7991 tree fn
= current_function_decl
;
7993 ++processing_template_decl
;
7994 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7995 && DECL_TEMPLATE_INFO (fn
)
7996 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7997 --processing_template_decl
;
8001 /* Returns true if T depends on any template parameter with level LEVEL. */
8004 uses_template_parms_level (tree t
, int level
)
8006 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8007 /*include_nondeduced_p=*/true);
8010 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8011 ill-formed translation unit, i.e. a variable or function that isn't
8012 usable in a constant expression. */
8015 neglectable_inst_p (tree d
)
8018 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8019 : decl_maybe_constant_var_p (d
)));
8022 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8023 neglectable and instantiated from within an erroneous instantiation. */
8026 limit_bad_template_recursion (tree decl
)
8028 struct tinst_level
*lev
= current_tinst_level
;
8029 int errs
= errorcount
+ sorrycount
;
8030 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8033 for (; lev
; lev
= lev
->next
)
8034 if (neglectable_inst_p (lev
->decl
))
8037 return (lev
&& errs
> lev
->errors
);
8040 static int tinst_depth
;
8041 extern int max_tinst_depth
;
8044 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8046 /* We're starting to instantiate D; record the template instantiation context
8047 for diagnostics and to restore it later. */
8050 push_tinst_level (tree d
)
8052 struct tinst_level
*new_level
;
8054 if (tinst_depth
>= max_tinst_depth
)
8056 last_error_tinst_level
= current_tinst_level
;
8057 if (TREE_CODE (d
) == TREE_LIST
)
8058 error ("template instantiation depth exceeds maximum of %d (use "
8059 "-ftemplate-depth= to increase the maximum) substituting %qS",
8060 max_tinst_depth
, d
);
8062 error ("template instantiation depth exceeds maximum of %d (use "
8063 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8064 max_tinst_depth
, d
);
8066 print_instantiation_context ();
8071 /* If the current instantiation caused problems, don't let it instantiate
8072 anything else. Do allow deduction substitution and decls usable in
8073 constant expressions. */
8074 if (limit_bad_template_recursion (d
))
8077 new_level
= ggc_alloc_tinst_level ();
8078 new_level
->decl
= d
;
8079 new_level
->locus
= input_location
;
8080 new_level
->errors
= errorcount
+sorrycount
;
8081 new_level
->in_system_header_p
= in_system_header
;
8082 new_level
->next
= current_tinst_level
;
8083 current_tinst_level
= new_level
;
8086 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
8087 depth_reached
= tinst_depth
;
8092 /* We're done instantiating this template; return to the instantiation
8096 pop_tinst_level (void)
8098 /* Restore the filename and line number stashed away when we started
8099 this instantiation. */
8100 input_location
= current_tinst_level
->locus
;
8101 current_tinst_level
= current_tinst_level
->next
;
8105 /* We're instantiating a deferred template; restore the template
8106 instantiation context in which the instantiation was requested, which
8107 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8110 reopen_tinst_level (struct tinst_level
*level
)
8112 struct tinst_level
*t
;
8115 for (t
= level
; t
; t
= t
->next
)
8118 current_tinst_level
= level
;
8120 if (current_tinst_level
)
8121 current_tinst_level
->errors
= errorcount
+sorrycount
;
8125 /* Returns the TINST_LEVEL which gives the original instantiation
8128 struct tinst_level
*
8129 outermost_tinst_level (void)
8131 struct tinst_level
*level
= current_tinst_level
;
8134 level
= level
->next
;
8138 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8139 vector of template arguments, as for tsubst.
8141 Returns an appropriate tsubst'd friend declaration. */
8144 tsubst_friend_function (tree decl
, tree args
)
8148 if (TREE_CODE (decl
) == FUNCTION_DECL
8149 && DECL_TEMPLATE_INSTANTIATION (decl
)
8150 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8151 /* This was a friend declared with an explicit template
8152 argument list, e.g.:
8156 to indicate that f was a template instantiation, not a new
8157 function declaration. Now, we have to figure out what
8158 instantiation of what template. */
8160 tree template_id
, arglist
, fns
;
8163 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8165 /* Friend functions are looked up in the containing namespace scope.
8166 We must enter that scope, to avoid finding member functions of the
8167 current class with same name. */
8168 push_nested_namespace (ns
);
8169 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8170 tf_warning_or_error
, NULL_TREE
,
8171 /*integral_constant_expression_p=*/false);
8172 pop_nested_namespace (ns
);
8173 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8174 tf_warning_or_error
, NULL_TREE
);
8175 template_id
= lookup_template_function (fns
, arglist
);
8177 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8178 tmpl
= determine_specialization (template_id
, new_friend
,
8180 /*need_member_template=*/0,
8181 TREE_VEC_LENGTH (args
),
8183 return instantiate_template (tmpl
, new_args
, tf_error
);
8186 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8188 /* The NEW_FRIEND will look like an instantiation, to the
8189 compiler, but is not an instantiation from the point of view of
8190 the language. For example, we might have had:
8192 template <class T> struct S {
8193 template <class U> friend void f(T, U);
8196 Then, in S<int>, template <class U> void f(int, U) is not an
8197 instantiation of anything. */
8198 if (new_friend
== error_mark_node
)
8199 return error_mark_node
;
8201 DECL_USE_TEMPLATE (new_friend
) = 0;
8202 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8204 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8205 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8206 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8209 /* The mangled name for the NEW_FRIEND is incorrect. The function
8210 is not a template instantiation and should not be mangled like
8211 one. Therefore, we forget the mangling here; we'll recompute it
8212 later if we need it. */
8213 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8215 SET_DECL_RTL (new_friend
, NULL
);
8216 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8219 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8222 tree new_friend_template_info
;
8223 tree new_friend_result_template_info
;
8225 int new_friend_is_defn
;
8227 /* We must save some information from NEW_FRIEND before calling
8228 duplicate decls since that function will free NEW_FRIEND if
8230 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8231 new_friend_is_defn
=
8232 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8233 (template_for_substitution (new_friend
)))
8235 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8237 /* This declaration is a `primary' template. */
8238 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8240 new_friend_result_template_info
8241 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8244 new_friend_result_template_info
= NULL_TREE
;
8246 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8247 if (new_friend_is_defn
)
8248 DECL_INITIAL (new_friend
) = error_mark_node
;
8250 /* Inside pushdecl_namespace_level, we will push into the
8251 current namespace. However, the friend function should go
8252 into the namespace of the template. */
8253 ns
= decl_namespace_context (new_friend
);
8254 push_nested_namespace (ns
);
8255 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8256 pop_nested_namespace (ns
);
8258 if (old_decl
== error_mark_node
)
8259 return error_mark_node
;
8261 if (old_decl
!= new_friend
)
8263 /* This new friend declaration matched an existing
8264 declaration. For example, given:
8266 template <class T> void f(T);
8267 template <class U> class C {
8268 template <class T> friend void f(T) {}
8271 the friend declaration actually provides the definition
8272 of `f', once C has been instantiated for some type. So,
8273 old_decl will be the out-of-class template declaration,
8274 while new_friend is the in-class definition.
8276 But, if `f' was called before this point, the
8277 instantiation of `f' will have DECL_TI_ARGS corresponding
8278 to `T' but not to `U', references to which might appear
8279 in the definition of `f'. Previously, the most general
8280 template for an instantiation of `f' was the out-of-class
8281 version; now it is the in-class version. Therefore, we
8282 run through all specialization of `f', adding to their
8283 DECL_TI_ARGS appropriately. In particular, they need a
8284 new set of outer arguments, corresponding to the
8285 arguments for this class instantiation.
8287 The same situation can arise with something like this:
8290 template <class T> class C {
8294 when `C<int>' is instantiated. Now, `f(int)' is defined
8297 if (!new_friend_is_defn
)
8298 /* On the other hand, if the in-class declaration does
8299 *not* provide a definition, then we don't want to alter
8300 existing definitions. We can just leave everything
8305 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8306 tree new_args
= TI_ARGS (new_friend_template_info
);
8308 /* Overwrite whatever template info was there before, if
8309 any, with the new template information pertaining to
8311 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8313 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8315 /* We should have called reregister_specialization in
8317 gcc_assert (retrieve_specialization (new_template
,
8321 /* Instantiate it if the global has already been used. */
8322 if (DECL_ODR_USED (old_decl
))
8323 instantiate_decl (old_decl
, /*defer_ok=*/true,
8324 /*expl_inst_class_mem_p=*/false);
8330 /* Indicate that the old function template is a partial
8332 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8333 = new_friend_result_template_info
;
8335 gcc_assert (new_template
8336 == most_general_template (new_template
));
8337 gcc_assert (new_template
!= old_decl
);
8339 /* Reassign any specializations already in the hash table
8340 to the new more general template, and add the
8341 additional template args. */
8342 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8346 tree spec
= TREE_VALUE (t
);
8349 elt
.tmpl
= old_decl
;
8350 elt
.args
= DECL_TI_ARGS (spec
);
8351 elt
.spec
= NULL_TREE
;
8353 htab_remove_elt (decl_specializations
, &elt
);
8356 = add_outermost_template_args (new_args
,
8357 DECL_TI_ARGS (spec
));
8359 register_specialization
8360 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8363 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8367 /* The information from NEW_FRIEND has been merged into OLD_DECL
8368 by duplicate_decls. */
8369 new_friend
= old_decl
;
8374 tree context
= DECL_CONTEXT (new_friend
);
8378 template <class T> class C {
8379 template <class U> friend void C1<U>::f (); // case 1
8380 friend void C2<T>::f (); // case 2
8382 we only need to make sure CONTEXT is a complete type for
8383 case 2. To distinguish between the two cases, we note that
8384 CONTEXT of case 1 remains dependent type after tsubst while
8385 this isn't true for case 2. */
8386 ++processing_template_decl
;
8387 dependent_p
= dependent_type_p (context
);
8388 --processing_template_decl
;
8391 && !complete_type_or_else (context
, NULL_TREE
))
8392 return error_mark_node
;
8394 if (COMPLETE_TYPE_P (context
))
8396 /* Check to see that the declaration is really present, and,
8397 possibly obtain an improved declaration. */
8398 tree fn
= check_classfn (context
,
8399 new_friend
, NULL_TREE
);
8409 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8410 template arguments, as for tsubst.
8412 Returns an appropriate tsubst'd friend type or error_mark_node on
8416 tsubst_friend_class (tree friend_tmpl
, tree args
)
8422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8424 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8425 return TREE_TYPE (t
);
8428 context
= CP_DECL_CONTEXT (friend_tmpl
);
8430 if (context
!= global_namespace
)
8432 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8433 push_nested_namespace (context
);
8435 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8438 /* Look for a class template declaration. We look for hidden names
8439 because two friend declarations of the same template are the
8440 same. For example, in:
8443 template <typename> friend class F;
8445 template <typename> struct B {
8446 template <typename> friend class F;
8449 both F templates are the same. */
8450 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8451 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8453 /* But, if we don't find one, it might be because we're in a
8454 situation like this:
8462 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8463 for `S<int>', not the TEMPLATE_DECL. */
8464 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8466 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8467 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8470 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8472 /* The friend template has already been declared. Just
8473 check to see that the declarations match, and install any new
8474 default parameters. We must tsubst the default parameters,
8475 of course. We only need the innermost template parameters
8476 because that is all that redeclare_class_template will look
8478 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8479 > TMPL_ARGS_DEPTH (args
))
8482 location_t saved_input_location
;
8483 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8484 args
, tf_warning_or_error
);
8486 saved_input_location
= input_location
;
8487 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8488 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8489 input_location
= saved_input_location
;
8493 friend_type
= TREE_TYPE (tmpl
);
8497 /* The friend template has not already been declared. In this
8498 case, the instantiation of the template class will cause the
8499 injection of this template into the global scope. */
8500 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8501 if (tmpl
== error_mark_node
)
8502 return error_mark_node
;
8504 /* The new TMPL is not an instantiation of anything, so we
8505 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8506 the new type because that is supposed to be the corresponding
8507 template decl, i.e., TMPL. */
8508 DECL_USE_TEMPLATE (tmpl
) = 0;
8509 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8510 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8511 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8512 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8514 /* Inject this template into the global scope. */
8515 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8518 if (context
!= global_namespace
)
8520 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8521 pop_nested_namespace (context
);
8523 pop_nested_class ();
8529 /* Returns zero if TYPE cannot be completed later due to circularity.
8530 Otherwise returns one. */
8533 can_complete_type_without_circularity (tree type
)
8535 if (type
== NULL_TREE
|| type
== error_mark_node
)
8537 else if (COMPLETE_TYPE_P (type
))
8539 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8540 return can_complete_type_without_circularity (TREE_TYPE (type
));
8541 else if (CLASS_TYPE_P (type
)
8542 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8548 /* Apply any attributes which had to be deferred until instantiation
8549 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8550 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8553 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8554 tree args
, tsubst_flags_t complain
, tree in_decl
)
8556 tree last_dep
= NULL_TREE
;
8560 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8561 if (ATTR_IS_DEPENDENT (t
))
8564 attributes
= copy_list (attributes
);
8568 if (DECL_P (*decl_p
))
8570 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8572 p
= &DECL_ATTRIBUTES (*decl_p
);
8575 p
= &TYPE_ATTRIBUTES (*decl_p
);
8579 tree late_attrs
= NULL_TREE
;
8580 tree
*q
= &late_attrs
;
8582 for (*p
= attributes
; *p
; )
8585 if (ATTR_IS_DEPENDENT (t
))
8587 *p
= TREE_CHAIN (t
);
8588 TREE_CHAIN (t
) = NULL_TREE
;
8589 /* If the first attribute argument is an identifier, don't
8590 pass it through tsubst. Attributes like mode, format,
8591 cleanup and several target specific attributes expect it
8594 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8595 && TREE_VALUE (TREE_VALUE (t
))
8596 && (identifier_p (TREE_VALUE (TREE_VALUE (t
)))))
8599 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8601 /*integral_constant_expression_p=*/false);
8602 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8604 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8609 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8610 /*integral_constant_expression_p=*/false);
8612 q
= &TREE_CHAIN (t
);
8615 p
= &TREE_CHAIN (t
);
8618 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8622 /* Perform (or defer) access check for typedefs that were referenced
8623 from within the template TMPL code.
8624 This is a subroutine of instantiate_decl and instantiate_class_template.
8625 TMPL is the template to consider and TARGS is the list of arguments of
8629 perform_typedefs_access_check (tree tmpl
, tree targs
)
8631 location_t saved_location
;
8633 qualified_typedef_usage_t
*iter
;
8636 || (!CLASS_TYPE_P (tmpl
)
8637 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8640 saved_location
= input_location
;
8641 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl
), i
, iter
)
8643 tree type_decl
= iter
->typedef_decl
;
8644 tree type_scope
= iter
->context
;
8646 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8649 if (uses_template_parms (type_decl
))
8650 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8651 if (uses_template_parms (type_scope
))
8652 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8654 /* Make access check error messages point to the location
8655 of the use of the typedef. */
8656 input_location
= iter
->locus
;
8657 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8658 type_decl
, type_decl
,
8659 tf_warning_or_error
);
8661 input_location
= saved_location
;
8665 instantiate_class_template_1 (tree type
)
8667 tree templ
, args
, pattern
, t
, member
;
8671 unsigned int saved_maximum_field_alignment
;
8674 if (type
== error_mark_node
)
8675 return error_mark_node
;
8677 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8678 || uses_template_parms (type
))
8681 /* Figure out which template is being instantiated. */
8682 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8683 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8685 /* Determine what specialization of the original template to
8687 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8688 if (t
== error_mark_node
)
8690 TYPE_BEING_DEFINED (type
) = 1;
8691 return error_mark_node
;
8695 /* This TYPE is actually an instantiation of a partial
8696 specialization. We replace the innermost set of ARGS with
8697 the arguments appropriate for substitution. For example,
8700 template <class T> struct S {};
8701 template <class T> struct S<T*> {};
8703 and supposing that we are instantiating S<int*>, ARGS will
8704 presently be {int*} -- but we need {int}. */
8705 pattern
= TREE_TYPE (t
);
8706 args
= TREE_PURPOSE (t
);
8710 pattern
= TREE_TYPE (templ
);
8711 args
= CLASSTYPE_TI_ARGS (type
);
8714 /* If the template we're instantiating is incomplete, then clearly
8715 there's nothing we can do. */
8716 if (!COMPLETE_TYPE_P (pattern
))
8719 /* If we've recursively instantiated too many templates, stop. */
8720 if (! push_tinst_level (type
))
8723 /* Now we're really doing the instantiation. Mark the type as in
8724 the process of being defined. */
8725 TYPE_BEING_DEFINED (type
) = 1;
8727 /* We may be in the middle of deferred access check. Disable
8729 push_deferring_access_checks (dk_no_deferred
);
8731 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8733 push_to_top_level ();
8734 /* Use #pragma pack from the template context. */
8735 saved_maximum_field_alignment
= maximum_field_alignment
;
8736 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8738 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8740 /* Set the input location to the most specialized template definition.
8741 This is needed if tsubsting causes an error. */
8742 typedecl
= TYPE_MAIN_DECL (pattern
);
8743 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8744 DECL_SOURCE_LOCATION (typedecl
);
8746 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8747 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8748 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8749 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8750 if (ANON_AGGR_TYPE_P (pattern
))
8751 SET_ANON_AGGR_TYPE_P (type
);
8752 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8754 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8755 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8756 /* Adjust visibility for template arguments. */
8757 determine_visibility (TYPE_MAIN_DECL (type
));
8759 if (CLASS_TYPE_P (type
))
8760 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8762 pbinfo
= TYPE_BINFO (pattern
);
8764 /* We should never instantiate a nested class before its enclosing
8765 class; we need to look up the nested class by name before we can
8766 instantiate it, and that lookup should instantiate the enclosing
8768 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8769 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8771 base_list
= NULL_TREE
;
8772 if (BINFO_N_BASE_BINFOS (pbinfo
))
8778 /* We must enter the scope containing the type, as that is where
8779 the accessibility of types named in dependent bases are
8781 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8783 /* Substitute into each of the bases to determine the actual
8785 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8788 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8789 tree expanded_bases
= NULL_TREE
;
8792 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8795 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8796 args
, tf_error
, NULL_TREE
);
8797 if (expanded_bases
== error_mark_node
)
8800 len
= TREE_VEC_LENGTH (expanded_bases
);
8803 for (idx
= 0; idx
< len
; idx
++)
8806 /* Extract the already-expanded base class. */
8807 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8809 /* Substitute to figure out the base class. */
8810 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8813 if (base
== error_mark_node
)
8816 base_list
= tree_cons (access
, base
, base_list
);
8817 if (BINFO_VIRTUAL_P (pbase_binfo
))
8818 TREE_TYPE (base_list
) = integer_type_node
;
8822 /* The list is now in reverse order; correct that. */
8823 base_list
= nreverse (base_list
);
8826 pop_scope (pushed_scope
);
8828 /* Now call xref_basetypes to set up all the base-class
8830 xref_basetypes (type
, base_list
);
8832 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8833 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8834 args
, tf_error
, NULL_TREE
);
8835 fixup_attribute_variants (type
);
8837 /* Now that our base classes are set up, enter the scope of the
8838 class, so that name lookups into base classes, etc. will work
8839 correctly. This is precisely analogous to what we do in
8840 begin_class_definition when defining an ordinary non-template
8841 class, except we also need to push the enclosing classes. */
8842 push_nested_class (type
);
8844 /* Now members are processed in the order of declaration. */
8845 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8846 member
; member
= TREE_CHAIN (member
))
8848 tree t
= TREE_VALUE (member
);
8850 if (TREE_PURPOSE (member
))
8854 /* Build new CLASSTYPE_NESTED_UTDS. */
8857 bool class_template_p
;
8859 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8860 && TYPE_LANG_SPECIFIC (t
)
8861 && CLASSTYPE_IS_TEMPLATE (t
));
8862 /* If the member is a class template, then -- even after
8863 substitution -- there may be dependent types in the
8864 template argument list for the class. We increment
8865 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8866 that function will assume that no types are dependent
8867 when outside of a template. */
8868 if (class_template_p
)
8869 ++processing_template_decl
;
8870 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8871 if (class_template_p
)
8872 --processing_template_decl
;
8873 if (newtag
== error_mark_node
)
8876 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8878 tree name
= TYPE_IDENTIFIER (t
);
8880 if (class_template_p
)
8881 /* Unfortunately, lookup_template_class sets
8882 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8883 instantiation (i.e., for the type of a member
8884 template class nested within a template class.)
8885 This behavior is required for
8886 maybe_process_partial_specialization to work
8887 correctly, but is not accurate in this case;
8888 the TAG is not an instantiation of anything.
8889 (The corresponding TEMPLATE_DECL is an
8890 instantiation, but the TYPE is not.) */
8891 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8893 /* Now, we call pushtag to put this NEWTAG into the scope of
8894 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8895 pushtag calling push_template_decl. We don't have to do
8896 this for enums because it will already have been done in
8899 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8900 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8903 else if (DECL_DECLARES_FUNCTION_P (t
))
8905 /* Build new TYPE_METHODS. */
8908 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8909 ++processing_template_decl
;
8910 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8911 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8912 --processing_template_decl
;
8913 set_current_access_from_decl (r
);
8914 finish_member_declaration (r
);
8915 /* Instantiate members marked with attribute used. */
8916 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8921 /* Build new TYPE_FIELDS. */
8922 if (TREE_CODE (t
) == STATIC_ASSERT
)
8926 ++c_inhibit_evaluation_warnings
;
8928 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8929 tf_warning_or_error
, NULL_TREE
,
8930 /*integral_constant_expression_p=*/true);
8931 --c_inhibit_evaluation_warnings
;
8933 finish_static_assert (condition
,
8934 STATIC_ASSERT_MESSAGE (t
),
8935 STATIC_ASSERT_SOURCE_LOCATION (t
),
8938 else if (TREE_CODE (t
) != CONST_DECL
)
8941 tree vec
= NULL_TREE
;
8944 /* The file and line for this declaration, to
8945 assist in error message reporting. Since we
8946 called push_tinst_level above, we don't need to
8948 input_location
= DECL_SOURCE_LOCATION (t
);
8950 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8951 ++processing_template_decl
;
8952 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8953 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8954 --processing_template_decl
;
8956 if (TREE_CODE (r
) == TREE_VEC
)
8958 /* A capture pack became multiple fields. */
8960 len
= TREE_VEC_LENGTH (vec
);
8963 for (int i
= 0; i
< len
; ++i
)
8966 r
= TREE_VEC_ELT (vec
, i
);
8971 [t]he initialization (and any associated
8972 side-effects) of a static data member does
8973 not occur unless the static data member is
8974 itself used in a way that requires the
8975 definition of the static data member to
8978 Therefore, we do not substitute into the
8979 initialized for the static data member here. */
8980 finish_static_data_member_decl
8983 /*init_const_expr_p=*/false,
8984 /*asmspec_tree=*/NULL_TREE
,
8986 /* Instantiate members marked with attribute used. */
8987 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8990 else if (TREE_CODE (r
) == FIELD_DECL
)
8992 /* Determine whether R has a valid type and can be
8993 completed later. If R is invalid, then its type
8994 is replaced by error_mark_node. */
8995 tree rtype
= TREE_TYPE (r
);
8996 if (can_complete_type_without_circularity (rtype
))
8997 complete_type (rtype
);
8999 if (!COMPLETE_TYPE_P (rtype
))
9001 cxx_incomplete_type_error (r
, rtype
);
9002 TREE_TYPE (r
) = error_mark_node
;
9006 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9007 such a thing will already have been added to the field
9008 list by tsubst_enum in finish_member_declaration in the
9009 CLASSTYPE_NESTED_UTDS case above. */
9010 if (!(TREE_CODE (r
) == TYPE_DECL
9011 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9012 && DECL_ARTIFICIAL (r
)))
9014 set_current_access_from_decl (r
);
9015 finish_member_declaration (r
);
9023 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
9024 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9026 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9028 tree friend_type
= t
;
9029 bool adjust_processing_template_decl
= false;
9031 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9033 /* template <class T> friend class C; */
9034 friend_type
= tsubst_friend_class (friend_type
, args
);
9035 adjust_processing_template_decl
= true;
9037 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9039 /* template <class T> friend class C::D; */
9040 friend_type
= tsubst (friend_type
, args
,
9041 tf_warning_or_error
, NULL_TREE
);
9042 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9043 friend_type
= TREE_TYPE (friend_type
);
9044 adjust_processing_template_decl
= true;
9046 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9047 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9049 /* This could be either
9053 when dependent_type_p is false or
9055 template <class U> friend class T::C;
9058 friend_type
= tsubst (friend_type
, args
,
9059 tf_warning_or_error
, NULL_TREE
);
9060 /* Bump processing_template_decl for correct
9061 dependent_type_p calculation. */
9062 ++processing_template_decl
;
9063 if (dependent_type_p (friend_type
))
9064 adjust_processing_template_decl
= true;
9065 --processing_template_decl
;
9067 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9068 && hidden_name_p (TYPE_NAME (friend_type
)))
9072 where C hasn't been declared yet. Let's lookup name
9073 from namespace scope directly, bypassing any name that
9074 come from dependent base class. */
9075 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9077 /* The call to xref_tag_from_type does injection for friend
9079 push_nested_namespace (ns
);
9081 xref_tag_from_type (friend_type
, NULL_TREE
,
9082 /*tag_scope=*/ts_current
);
9083 pop_nested_namespace (ns
);
9085 else if (uses_template_parms (friend_type
))
9086 /* friend class C<T>; */
9087 friend_type
= tsubst (friend_type
, args
,
9088 tf_warning_or_error
, NULL_TREE
);
9093 where C is already declared or
9095 friend class C<int>;
9097 We don't have to do anything in these cases. */
9099 if (adjust_processing_template_decl
)
9100 /* Trick make_friend_class into realizing that the friend
9101 we're adding is a template, not an ordinary class. It's
9102 important that we use make_friend_class since it will
9103 perform some error-checking and output cross-reference
9105 ++processing_template_decl
;
9107 if (friend_type
!= error_mark_node
)
9108 make_friend_class (type
, friend_type
, /*complain=*/false);
9110 if (adjust_processing_template_decl
)
9111 --processing_template_decl
;
9115 /* Build new DECL_FRIENDLIST. */
9118 /* The file and line for this declaration, to
9119 assist in error message reporting. Since we
9120 called push_tinst_level above, we don't need to
9122 input_location
= DECL_SOURCE_LOCATION (t
);
9124 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9126 ++processing_template_decl
;
9127 push_deferring_access_checks (dk_no_check
);
9130 r
= tsubst_friend_function (t
, args
);
9131 add_friend (type
, r
, /*complain=*/false);
9132 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9134 pop_deferring_access_checks ();
9135 --processing_template_decl
;
9141 if (tree expr
= CLASSTYPE_LAMBDA_EXPR (type
))
9143 tree decl
= lambda_function (type
);
9146 if (!DECL_TEMPLATE_INFO (decl
)
9147 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl
)) != decl
)
9148 instantiate_decl (decl
, false, false);
9150 /* We need to instantiate the capture list from the template
9151 after we've instantiated the closure members, but before we
9152 consider adding the conversion op. Also keep any captures
9153 that may have been added during instantiation of the op(). */
9154 tree tmpl_expr
= CLASSTYPE_LAMBDA_EXPR (pattern
);
9156 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr
),
9157 args
, tf_warning_or_error
, NULL_TREE
,
9160 LAMBDA_EXPR_CAPTURE_LIST (expr
)
9161 = chainon (tmpl_cap
, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr
)));
9163 maybe_add_lambda_conv_op (type
);
9166 gcc_assert (errorcount
);
9169 /* Set the file and line number information to whatever is given for
9170 the class itself. This puts error messages involving generated
9171 implicit functions at a predictable point, and the same point
9172 that would be used for non-template classes. */
9173 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9175 unreverse_member_declarations (type
);
9176 finish_struct_1 (type
);
9177 TYPE_BEING_DEFINED (type
) = 0;
9179 /* We don't instantiate default arguments for member functions. 14.7.1:
9181 The implicit instantiation of a class template specialization causes
9182 the implicit instantiation of the declarations, but not of the
9183 definitions or default arguments, of the class member functions,
9184 member classes, static data members and member templates.... */
9186 /* Some typedefs referenced from within the template code need to be access
9187 checked at template instantiation time, i.e now. These types were
9188 added to the template at parsing time. Let's get those and perform
9189 the access checks then. */
9190 perform_typedefs_access_check (pattern
, args
);
9191 perform_deferred_access_checks (tf_warning_or_error
);
9192 pop_nested_class ();
9193 maximum_field_alignment
= saved_maximum_field_alignment
;
9195 pop_from_top_level ();
9196 pop_deferring_access_checks ();
9199 /* The vtable for a template class can be emitted in any translation
9200 unit in which the class is instantiated. When there is no key
9201 method, however, finish_struct_1 will already have added TYPE to
9202 the keyed_classes list. */
9203 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9204 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9209 /* Wrapper for instantiate_class_template_1. */
9212 instantiate_class_template (tree type
)
9215 timevar_push (TV_TEMPLATE_INST
);
9216 ret
= instantiate_class_template_1 (type
);
9217 timevar_pop (TV_TEMPLATE_INST
);
9222 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9228 else if (TYPE_P (t
))
9229 r
= tsubst (t
, args
, complain
, in_decl
);
9232 if (!(complain
& tf_warning
))
9233 ++c_inhibit_evaluation_warnings
;
9234 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9235 /*integral_constant_expression_p=*/true);
9236 if (!(complain
& tf_warning
))
9237 --c_inhibit_evaluation_warnings
;
9238 /* Preserve the raw-reference nature of T. */
9239 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9240 && REFERENCE_REF_P (r
))
9241 r
= TREE_OPERAND (r
, 0);
9246 /* Given a function parameter pack TMPL_PARM and some function parameters
9247 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9248 and set *SPEC_P to point at the next point in the list. */
9251 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9253 /* Collect all of the extra "packed" parameters into an
9257 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9258 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9259 tree spec_parm
= *spec_p
;
9262 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9264 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9267 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9268 parmvec
= make_tree_vec (len
);
9269 parmtypevec
= make_tree_vec (len
);
9270 spec_parm
= *spec_p
;
9271 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9273 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9274 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9277 /* Build the argument packs. */
9278 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9279 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9280 TREE_TYPE (argpack
) = argtypepack
;
9281 *spec_p
= spec_parm
;
9286 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9287 NONTYPE_ARGUMENT_PACK. */
9290 make_fnparm_pack (tree spec_parm
)
9292 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9295 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9299 argument_pack_element_is_expansion_p (tree arg_pack
, int i
)
9301 tree vec
= ARGUMENT_PACK_ARGS (arg_pack
);
9302 if (i
>= TREE_VEC_LENGTH (vec
))
9304 return PACK_EXPANSION_P (TREE_VEC_ELT (vec
, i
));
9308 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9311 make_argument_pack_select (tree arg_pack
, unsigned index
)
9313 tree aps
= make_node (ARGUMENT_PACK_SELECT
);
9315 ARGUMENT_PACK_SELECT_FROM_PACK (aps
) = arg_pack
;
9316 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9321 /* This is a subroutine of tsubst_pack_expansion.
9323 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9324 mechanism to store the (non complete list of) arguments of the
9325 substitution and return a non substituted pack expansion, in order
9326 to wait for when we have enough arguments to really perform the
9330 use_pack_expansion_extra_args_p (tree parm_packs
,
9334 /* If one pack has an expansion and another pack has a normal
9335 argument or if one pack has an empty argument and an another
9336 one hasn't then tsubst_pack_expansion cannot perform the
9337 substitution and need to fall back on the
9338 PACK_EXPANSION_EXTRA mechanism. */
9339 if (parm_packs
== NULL_TREE
)
9341 else if (has_empty_arg
)
9344 bool has_expansion_arg
= false;
9345 for (int i
= 0 ; i
< arg_pack_len
; ++i
)
9347 bool has_non_expansion_arg
= false;
9348 for (tree parm_pack
= parm_packs
;
9350 parm_pack
= TREE_CHAIN (parm_pack
))
9352 tree arg
= TREE_VALUE (parm_pack
);
9354 if (argument_pack_element_is_expansion_p (arg
, i
))
9355 has_expansion_arg
= true;
9357 has_non_expansion_arg
= true;
9360 if (has_expansion_arg
&& has_non_expansion_arg
)
9366 /* [temp.variadic]/6 says that:
9368 The instantiation of a pack expansion [...]
9369 produces a list E1,E2, ..., En, where N is the number of elements
9370 in the pack expansion parameters.
9372 This subroutine of tsubst_pack_expansion produces one of these Ei.
9374 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9375 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9376 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9377 INDEX is the index 'i' of the element Ei to produce. ARGS,
9378 COMPLAIN, and IN_DECL are the same parameters as for the
9379 tsubst_pack_expansion function.
9381 The function returns the resulting Ei upon successful completion,
9384 Note that this function possibly modifies the ARGS parameter, so
9385 it's the responsibility of the caller to restore it. */
9388 gen_elem_of_pack_expansion_instantiation (tree pattern
,
9391 tree args
/* This parm gets
9393 tsubst_flags_t complain
,
9397 bool ith_elem_is_expansion
= false;
9399 /* For each parameter pack, change the substitution of the parameter
9400 pack to the ith argument in its argument pack, then expand the
9402 for (tree pack
= parm_packs
; pack
; pack
= TREE_CHAIN (pack
))
9404 tree parm
= TREE_PURPOSE (pack
);
9405 tree arg_pack
= TREE_VALUE (pack
);
9406 tree aps
; /* instance of ARGUMENT_PACK_SELECT. */
9408 ith_elem_is_expansion
|=
9409 argument_pack_element_is_expansion_p (arg_pack
, index
);
9411 /* Select the Ith argument from the pack. */
9412 if (TREE_CODE (parm
) == PARM_DECL
9413 || TREE_CODE (parm
) == FIELD_DECL
)
9417 aps
= make_argument_pack_select (arg_pack
, index
);
9419 register_local_specialization (aps
, parm
);
9422 aps
= retrieve_local_specialization (parm
);
9427 template_parm_level_and_index (parm
, &level
, &idx
);
9431 aps
= make_argument_pack_select (arg_pack
, index
);
9432 /* Update the corresponding argument. */
9433 TMPL_ARG (args
, level
, idx
) = aps
;
9436 /* Re-use the ARGUMENT_PACK_SELECT. */
9437 aps
= TMPL_ARG (args
, level
, idx
);
9439 ARGUMENT_PACK_SELECT_INDEX (aps
) = index
;
9442 /* Substitute into the PATTERN with the (possibly altered)
9444 if (!TYPE_P (pattern
))
9445 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9446 /*integral_constant_expression_p=*/false);
9448 t
= tsubst (pattern
, args
, complain
, in_decl
);
9450 /* If the Ith argument pack element is a pack expansion, then
9451 the Ith element resulting from the substituting is going to
9452 be a pack expansion as well. */
9453 if (ith_elem_is_expansion
)
9454 t
= make_pack_expansion (t
);
9459 /* Substitute ARGS into T, which is an pack expansion
9460 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9461 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9462 (if only a partial substitution could be performed) or
9463 ERROR_MARK_NODE if there was an error. */
9465 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9469 tree pack
, packs
= NULL_TREE
;
9470 bool unsubstituted_packs
= false;
9473 struct pointer_map_t
*saved_local_specializations
= NULL
;
9474 bool need_local_specializations
= false;
9477 gcc_assert (PACK_EXPANSION_P (t
));
9478 pattern
= PACK_EXPANSION_PATTERN (t
);
9480 /* Add in any args remembered from an earlier partial instantiation. */
9481 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9483 levels
= TMPL_ARGS_DEPTH (args
);
9485 /* Determine the argument packs that will instantiate the parameter
9486 packs used in the expansion expression. While we're at it,
9487 compute the number of arguments to be expanded and make sure it
9489 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9490 pack
= TREE_CHAIN (pack
))
9492 tree parm_pack
= TREE_VALUE (pack
);
9493 tree arg_pack
= NULL_TREE
;
9494 tree orig_arg
= NULL_TREE
;
9497 if (TREE_CODE (parm_pack
) == BASES
)
9499 if (BASES_DIRECT (parm_pack
))
9500 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9501 args
, complain
, in_decl
, false));
9503 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9504 args
, complain
, in_decl
, false));
9506 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9508 if (PACK_EXPANSION_LOCAL_P (t
))
9509 arg_pack
= retrieve_local_specialization (parm_pack
);
9512 /* We can't rely on local_specializations for a parameter
9513 name used later in a function declaration (such as in a
9514 late-specified return type). Even if it exists, it might
9515 have the wrong value for a recursive call. Just make a
9516 dummy decl, since it's only used for its type. */
9517 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9518 if (arg_pack
&& DECL_PACK_P (arg_pack
))
9519 /* Partial instantiation of the parm_pack, we can't build
9520 up an argument pack yet. */
9521 arg_pack
= NULL_TREE
;
9523 arg_pack
= make_fnparm_pack (arg_pack
);
9524 need_local_specializations
= true;
9527 else if (TREE_CODE (parm_pack
) == FIELD_DECL
)
9528 arg_pack
= tsubst_copy (parm_pack
, args
, complain
, in_decl
);
9532 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9534 if (level
<= levels
)
9535 arg_pack
= TMPL_ARG (args
, level
, idx
);
9538 orig_arg
= arg_pack
;
9539 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9540 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9542 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9543 /* This can only happen if we forget to expand an argument
9544 pack somewhere else. Just return an error, silently. */
9546 result
= make_tree_vec (1);
9547 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9554 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9556 /* Don't bother trying to do a partial substitution with
9557 incomplete packs; we'll try again after deduction. */
9558 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9563 else if (len
!= my_len
)
9565 if (!(complain
& tf_error
))
9566 /* Fail quietly. */;
9567 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9568 error ("mismatched argument pack lengths while expanding "
9572 error ("mismatched argument pack lengths while expanding "
9575 return error_mark_node
;
9578 /* Keep track of the parameter packs and their corresponding
9580 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9581 TREE_TYPE (packs
) = orig_arg
;
9585 /* We can't substitute for this parameter pack. We use a flag as
9586 well as the missing_level counter because function parameter
9587 packs don't have a level. */
9588 unsubstituted_packs
= true;
9592 /* We cannot expand this expansion expression, because we don't have
9593 all of the argument packs we need. */
9594 if (use_pack_expansion_extra_args_p (packs
, len
, unsubstituted_packs
))
9596 /* We got some full packs, but we can't substitute them in until we
9597 have values for all the packs. So remember these until then. */
9599 t
= make_pack_expansion (pattern
);
9600 PACK_EXPANSION_EXTRA_ARGS (t
) = args
;
9603 else if (unsubstituted_packs
)
9605 /* There were no real arguments, we're just replacing a parameter
9606 pack with another version of itself. Substitute into the
9607 pattern and return a PACK_EXPANSION_*. The caller will need to
9609 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9610 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9611 /*integral_constant_expression_p=*/false);
9613 t
= tsubst (pattern
, args
, complain
, in_decl
);
9614 t
= make_pack_expansion (t
);
9618 gcc_assert (len
>= 0);
9620 if (need_local_specializations
)
9622 /* We're in a late-specified return type, so create our own local
9623 specializations map; the current map is either NULL or (in the
9624 case of recursive unification) might have bindings that we don't
9625 want to use or alter. */
9626 saved_local_specializations
= local_specializations
;
9627 local_specializations
= pointer_map_create ();
9630 /* For each argument in each argument pack, substitute into the
9632 result
= make_tree_vec (len
);
9633 for (i
= 0; i
< len
; ++i
)
9635 t
= gen_elem_of_pack_expansion_instantiation (pattern
, packs
,
9639 TREE_VEC_ELT (result
, i
) = t
;
9640 if (t
== error_mark_node
)
9642 result
= error_mark_node
;
9647 /* Update ARGS to restore the substitution from parameter packs to
9648 their argument packs. */
9649 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9651 tree parm
= TREE_PURPOSE (pack
);
9653 if (TREE_CODE (parm
) == PARM_DECL
9654 || TREE_CODE (parm
) == FIELD_DECL
)
9655 register_local_specialization (TREE_TYPE (pack
), parm
);
9660 if (TREE_VALUE (pack
) == NULL_TREE
)
9663 template_parm_level_and_index (parm
, &level
, &idx
);
9665 /* Update the corresponding argument. */
9666 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9667 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9670 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9674 if (need_local_specializations
)
9676 pointer_map_destroy (local_specializations
);
9677 local_specializations
= saved_local_specializations
;
9683 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9684 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9685 parameter packs; all parms generated from a function parameter pack will
9686 have the same DECL_PARM_INDEX. */
9689 get_pattern_parm (tree parm
, tree tmpl
)
9691 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9694 if (DECL_ARTIFICIAL (parm
))
9696 for (patparm
= DECL_ARGUMENTS (pattern
);
9697 patparm
; patparm
= DECL_CHAIN (patparm
))
9698 if (DECL_ARTIFICIAL (patparm
)
9699 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9704 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9705 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9706 gcc_assert (DECL_PARM_INDEX (patparm
)
9707 == DECL_PARM_INDEX (parm
));
9713 /* Substitute ARGS into the vector or list of template arguments T. */
9716 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9719 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9722 if (t
== error_mark_node
)
9723 return error_mark_node
;
9725 len
= TREE_VEC_LENGTH (t
);
9726 elts
= XALLOCAVEC (tree
, len
);
9728 for (i
= 0; i
< len
; i
++)
9730 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9733 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9734 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9735 else if (PACK_EXPANSION_P (orig_arg
))
9737 /* Substitute into an expansion expression. */
9738 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9740 if (TREE_CODE (new_arg
) == TREE_VEC
)
9741 /* Add to the expanded length adjustment the number of
9742 expanded arguments. We subtract one from this
9743 measurement, because the argument pack expression
9744 itself is already counted as 1 in
9745 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9746 the argument pack is empty. */
9747 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9749 else if (ARGUMENT_PACK_P (orig_arg
))
9751 /* Substitute into each of the arguments. */
9752 new_arg
= TYPE_P (orig_arg
)
9753 ? cxx_make_type (TREE_CODE (orig_arg
))
9754 : make_node (TREE_CODE (orig_arg
));
9756 SET_ARGUMENT_PACK_ARGS (
9758 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9759 args
, complain
, in_decl
));
9761 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9762 new_arg
= error_mark_node
;
9764 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9765 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9767 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9769 if (TREE_TYPE (new_arg
) == error_mark_node
)
9770 new_arg
= error_mark_node
;
9774 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9776 if (new_arg
== error_mark_node
)
9777 return error_mark_node
;
9780 if (new_arg
!= orig_arg
)
9787 /* Make space for the expanded arguments coming from template
9789 t
= make_tree_vec (len
+ expanded_len_adjust
);
9790 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9791 arguments for a member template.
9792 In that case each TREE_VEC in ORIG_T represents a level of template
9793 arguments, and ORIG_T won't carry any non defaulted argument count.
9794 It will rather be the nested TREE_VECs that will carry one.
9795 In other words, ORIG_T carries a non defaulted argument count only
9796 if it doesn't contain any nested TREE_VEC. */
9797 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9799 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9800 count
+= expanded_len_adjust
;
9801 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9803 for (i
= 0, out
= 0; i
< len
; i
++)
9805 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9806 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9807 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9811 /* Now expand the template argument pack "in place". */
9812 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9813 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9817 TREE_VEC_ELT (t
, out
) = elts
[i
];
9825 /* Return the result of substituting ARGS into the template parameters
9826 given by PARMS. If there are m levels of ARGS and m + n levels of
9827 PARMS, then the result will contain n levels of PARMS. For
9828 example, if PARMS is `template <class T> template <class U>
9829 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9830 result will be `template <int*, double, class V>'. */
9833 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9838 /* When substituting into a template, we must set
9839 PROCESSING_TEMPLATE_DECL as the template parameters may be
9840 dependent if they are based on one-another, and the dependency
9841 predicates are short-circuit outside of templates. */
9842 ++processing_template_decl
;
9844 for (new_parms
= &r
;
9845 parms
&& TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9846 new_parms
= &(TREE_CHAIN (*new_parms
)),
9847 parms
= TREE_CHAIN (parms
))
9850 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9853 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9857 if (parms
== error_mark_node
)
9860 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9862 if (tuple
== error_mark_node
)
9865 TREE_VEC_ELT (new_vec
, i
) =
9866 tsubst_template_parm (tuple
, args
, complain
);
9870 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9871 - TMPL_ARGS_DEPTH (args
)),
9872 new_vec
, NULL_TREE
);
9875 --processing_template_decl
;
9880 /* Return the result of substituting ARGS into one template parameter
9881 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9882 parameter and which TREE_PURPOSE is the default argument of the
9883 template parameter. */
9886 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9888 tree default_value
, parm_decl
;
9890 if (args
== NULL_TREE
9892 || t
== error_mark_node
)
9895 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9897 default_value
= TREE_PURPOSE (t
);
9898 parm_decl
= TREE_VALUE (t
);
9900 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9901 if (TREE_CODE (parm_decl
) == PARM_DECL
9902 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9903 parm_decl
= error_mark_node
;
9904 default_value
= tsubst_template_arg (default_value
, args
,
9905 complain
, NULL_TREE
);
9907 return build_tree_list (default_value
, parm_decl
);
9910 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9911 type T. If T is not an aggregate or enumeration type, it is
9912 handled as if by tsubst. IN_DECL is as for tsubst. If
9913 ENTERING_SCOPE is nonzero, T is the context for a template which
9914 we are presently tsubst'ing. Return the substituted value. */
9917 tsubst_aggr_type (tree t
,
9919 tsubst_flags_t complain
,
9926 switch (TREE_CODE (t
))
9929 if (TYPE_PTRMEMFUNC_P (t
))
9930 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9932 /* Else fall through. */
9935 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9940 int saved_unevaluated_operand
;
9941 int saved_inhibit_evaluation_warnings
;
9943 /* In "sizeof(X<I>)" we need to evaluate "I". */
9944 saved_unevaluated_operand
= cp_unevaluated_operand
;
9945 cp_unevaluated_operand
= 0;
9946 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9947 c_inhibit_evaluation_warnings
= 0;
9949 /* First, determine the context for the type we are looking
9951 context
= TYPE_CONTEXT (t
);
9952 if (context
&& TYPE_P (context
))
9954 context
= tsubst_aggr_type (context
, args
, complain
,
9955 in_decl
, /*entering_scope=*/1);
9956 /* If context is a nested class inside a class template,
9957 it may still need to be instantiated (c++/33959). */
9958 context
= complete_type (context
);
9961 /* Then, figure out what arguments are appropriate for the
9962 type we are trying to find. For example, given:
9964 template <class T> struct S;
9965 template <class T, class U> void f(T, U) { S<U> su; }
9967 and supposing that we are instantiating f<int, double>,
9968 then our ARGS will be {int, double}, but, when looking up
9969 S we only want {double}. */
9970 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9972 if (argvec
== error_mark_node
)
9973 r
= error_mark_node
;
9976 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9977 entering_scope
, complain
);
9978 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9981 cp_unevaluated_operand
= saved_unevaluated_operand
;
9982 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9987 /* This is not a template type, so there's nothing to do. */
9991 return tsubst (t
, args
, complain
, in_decl
);
9995 /* Substitute into the default argument ARG (a default argument for
9996 FN), which has the indicated TYPE. */
9999 tsubst_default_argument (tree fn
, tree type
, tree arg
, tsubst_flags_t complain
)
10001 tree saved_class_ptr
= NULL_TREE
;
10002 tree saved_class_ref
= NULL_TREE
;
10003 int errs
= errorcount
+ sorrycount
;
10005 /* This can happen in invalid code. */
10006 if (TREE_CODE (arg
) == DEFAULT_ARG
)
10009 /* This default argument came from a template. Instantiate the
10010 default argument here, not in tsubst. In the case of
10019 we must be careful to do name lookup in the scope of S<T>,
10020 rather than in the current class. */
10021 push_access_scope (fn
);
10022 /* The "this" pointer is not valid in a default argument. */
10025 saved_class_ptr
= current_class_ptr
;
10026 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
10027 saved_class_ref
= current_class_ref
;
10028 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
10031 push_deferring_access_checks(dk_no_deferred
);
10032 /* The default argument expression may cause implicitly defined
10033 member functions to be synthesized, which will result in garbage
10034 collection. We must treat this situation as if we were within
10035 the body of function so as to avoid collecting live data on the
10038 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
10039 complain
, NULL_TREE
,
10040 /*integral_constant_expression_p=*/false);
10042 pop_deferring_access_checks();
10044 /* Restore the "this" pointer. */
10047 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
10048 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
10051 if (errorcount
+sorrycount
> errs
10052 && (complain
& tf_warning_or_error
))
10053 inform (input_location
,
10054 " when instantiating default argument for call to %D", fn
);
10056 /* Make sure the default argument is reasonable. */
10057 arg
= check_default_argument (type
, arg
, complain
);
10059 pop_access_scope (fn
);
10064 /* Substitute into all the default arguments for FN. */
10067 tsubst_default_arguments (tree fn
, tsubst_flags_t complain
)
10072 tmpl_args
= DECL_TI_ARGS (fn
);
10074 /* If this function is not yet instantiated, we certainly don't need
10075 its default arguments. */
10076 if (uses_template_parms (tmpl_args
))
10078 /* Don't do this again for clones. */
10079 if (DECL_CLONED_FUNCTION_P (fn
))
10082 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
10084 arg
= TREE_CHAIN (arg
))
10085 if (TREE_PURPOSE (arg
))
10086 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
10088 TREE_PURPOSE (arg
),
10092 /* Substitute the ARGS into the T, which is a _DECL. Return the
10093 result of the substitution. Issue error and warning messages under
10094 control of COMPLAIN. */
10097 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10099 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10100 location_t saved_loc
;
10101 tree r
= NULL_TREE
;
10103 hashval_t hash
= 0;
10105 /* Set the filename and linenumber to improve error-reporting. */
10106 saved_loc
= input_location
;
10107 input_location
= DECL_SOURCE_LOCATION (t
);
10109 switch (TREE_CODE (t
))
10111 case TEMPLATE_DECL
:
10113 /* We can get here when processing a member function template,
10114 member class template, or template template parameter. */
10115 tree decl
= DECL_TEMPLATE_RESULT (t
);
10120 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10122 /* Template template parameter is treated here. */
10123 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10124 if (new_type
== error_mark_node
)
10125 RETURN (error_mark_node
);
10126 /* If we get a real template back, return it. This can happen in
10127 the context of most_specialized_class. */
10128 if (TREE_CODE (new_type
) == TEMPLATE_DECL
)
10132 DECL_CHAIN (r
) = NULL_TREE
;
10133 TREE_TYPE (r
) = new_type
;
10134 DECL_TEMPLATE_RESULT (r
)
10135 = build_decl (DECL_SOURCE_LOCATION (decl
),
10136 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10137 DECL_TEMPLATE_PARMS (r
)
10138 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10140 TYPE_NAME (new_type
) = r
;
10144 /* We might already have an instance of this template.
10145 The ARGS are for the surrounding class type, so the
10146 full args contain the tsubst'd args for the context,
10147 plus the innermost args from the template decl. */
10148 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10149 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10150 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10151 /* Because this is a template, the arguments will still be
10152 dependent, even after substitution. If
10153 PROCESSING_TEMPLATE_DECL is not set, the dependency
10154 predicates will short-circuit. */
10155 ++processing_template_decl
;
10156 full_args
= tsubst_template_args (tmpl_args
, args
,
10157 complain
, in_decl
);
10158 --processing_template_decl
;
10159 if (full_args
== error_mark_node
)
10160 RETURN (error_mark_node
);
10162 /* If this is a default template template argument,
10163 tsubst might not have changed anything. */
10164 if (full_args
== tmpl_args
)
10167 hash
= hash_tmpl_and_args (t
, full_args
);
10168 spec
= retrieve_specialization (t
, full_args
, hash
);
10169 if (spec
!= NULL_TREE
)
10175 /* Make a new template decl. It will be similar to the
10176 original, but will record the current template arguments.
10177 We also create a new function declaration, which is just
10178 like the old one, but points to this new template, rather
10179 than the old one. */
10181 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10182 DECL_CHAIN (r
) = NULL_TREE
;
10184 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10186 if (TREE_CODE (decl
) == TYPE_DECL
10187 && !TYPE_DECL_ALIAS_P (decl
))
10190 ++processing_template_decl
;
10191 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10192 --processing_template_decl
;
10193 if (new_type
== error_mark_node
)
10194 RETURN (error_mark_node
);
10196 TREE_TYPE (r
) = new_type
;
10197 /* For a partial specialization, we need to keep pointing to
10198 the primary template. */
10199 if (!DECL_TEMPLATE_SPECIALIZATION (t
))
10200 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10201 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10202 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10203 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10208 ++processing_template_decl
;
10209 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10210 --processing_template_decl
;
10211 if (new_decl
== error_mark_node
)
10212 RETURN (error_mark_node
);
10214 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10215 DECL_TI_TEMPLATE (new_decl
) = r
;
10216 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10217 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10218 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10221 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10222 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10223 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10225 /* The template parameters for this new template are all the
10226 template parameters for the old template, except the
10227 outermost level of parameters. */
10228 DECL_TEMPLATE_PARMS (r
)
10229 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10232 if (PRIMARY_TEMPLATE_P (t
))
10233 DECL_PRIMARY_TEMPLATE (r
) = r
;
10235 if (TREE_CODE (decl
) != TYPE_DECL
)
10236 /* Record this non-type partial instantiation. */
10237 register_specialization (r
, t
,
10238 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10243 case FUNCTION_DECL
:
10246 tree argvec
= NULL_TREE
;
10254 /* Nobody should be tsubst'ing into non-template functions. */
10255 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10257 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10262 /* If T is not dependent, just return it. We have to
10263 increment PROCESSING_TEMPLATE_DECL because
10264 value_dependent_expression_p assumes that nothing is
10265 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10266 ++processing_template_decl
;
10267 dependent_p
= value_dependent_expression_p (t
);
10268 --processing_template_decl
;
10272 /* Calculate the most general template of which R is a
10273 specialization, and the complete set of arguments used to
10275 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10276 argvec
= tsubst_template_args (DECL_TI_ARGS
10277 (DECL_TEMPLATE_RESULT
10278 (DECL_TI_TEMPLATE (t
))),
10279 args
, complain
, in_decl
);
10280 if (argvec
== error_mark_node
)
10281 RETURN (error_mark_node
);
10283 /* Check to see if we already have this specialization. */
10284 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10285 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10293 /* We can see more levels of arguments than parameters if
10294 there was a specialization of a member template, like
10297 template <class T> struct S { template <class U> void f(); }
10298 template <> template <class U> void S<int>::f(U);
10300 Here, we'll be substituting into the specialization,
10301 because that's where we can find the code we actually
10302 want to generate, but we'll have enough arguments for
10303 the most general template.
10305 We also deal with the peculiar case:
10307 template <class T> struct S {
10308 template <class U> friend void f();
10310 template <class U> void f() {}
10312 template void f<double>();
10314 Here, the ARGS for the instantiation of will be {int,
10315 double}. But, we only need as many ARGS as there are
10316 levels of template parameters in CODE_PATTERN. We are
10317 careful not to get fooled into reducing the ARGS in
10320 template <class T> struct S { template <class U> void f(U); }
10321 template <class T> template <> void S<T>::f(int) {}
10323 which we can spot because the pattern will be a
10324 specialization in this case. */
10325 args_depth
= TMPL_ARGS_DEPTH (args
);
10327 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10328 if (args_depth
> parms_depth
10329 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10330 args
= get_innermost_template_args (args
, parms_depth
);
10334 /* This special case arises when we have something like this:
10336 template <class T> struct S {
10337 friend void f<int>(int, double);
10340 Here, the DECL_TI_TEMPLATE for the friend declaration
10341 will be an IDENTIFIER_NODE. We are being called from
10342 tsubst_friend_function, and we want only to create a
10343 new decl (R) with appropriate types so that we can call
10344 determine_specialization. */
10345 gen_tmpl
= NULL_TREE
;
10348 if (DECL_CLASS_SCOPE_P (t
))
10350 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10354 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10355 complain
, t
, /*entering_scope=*/1);
10360 ctx
= DECL_CONTEXT (t
);
10362 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10363 if (type
== error_mark_node
)
10364 RETURN (error_mark_node
);
10366 /* If we hit excessive deduction depth, the type is bogus even if
10367 it isn't error_mark_node, so don't build a decl. */
10368 if (excessive_deduction_depth
)
10369 RETURN (error_mark_node
);
10371 /* We do NOT check for matching decls pushed separately at this
10372 point, as they may not represent instantiations of this
10373 template, and in any case are considered separate under the
10376 DECL_USE_TEMPLATE (r
) = 0;
10377 TREE_TYPE (r
) = type
;
10378 /* Clear out the mangled name and RTL for the instantiation. */
10379 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10380 SET_DECL_RTL (r
, NULL
);
10381 /* Leave DECL_INITIAL set on deleted instantiations. */
10382 if (!DECL_DELETED_FN (r
))
10383 DECL_INITIAL (r
) = NULL_TREE
;
10384 DECL_CONTEXT (r
) = ctx
;
10386 if (member
&& DECL_CONV_FN_P (r
))
10387 /* Type-conversion operator. Reconstruct the name, in
10388 case it's the name of one of the template's parameters. */
10389 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10391 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10393 DECL_RESULT (r
) = NULL_TREE
;
10395 TREE_STATIC (r
) = 0;
10396 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10397 DECL_EXTERNAL (r
) = 1;
10398 /* If this is an instantiation of a function with internal
10399 linkage, we already know what object file linkage will be
10400 assigned to the instantiation. */
10401 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10402 DECL_DEFER_OUTPUT (r
) = 0;
10403 DECL_CHAIN (r
) = NULL_TREE
;
10404 DECL_PENDING_INLINE_INFO (r
) = 0;
10405 DECL_PENDING_INLINE_P (r
) = 0;
10406 DECL_SAVED_TREE (r
) = NULL_TREE
;
10407 DECL_STRUCT_FUNCTION (r
) = NULL
;
10409 /* We'll re-clone as appropriate in instantiate_template. */
10410 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10412 /* If we aren't complaining now, return on error before we register
10413 the specialization so that we'll complain eventually. */
10414 if ((complain
& tf_error
) == 0
10415 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10416 && !grok_op_properties (r
, /*complain=*/false))
10417 RETURN (error_mark_node
);
10419 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10420 this in the special friend case mentioned above where
10421 GEN_TMPL is NULL. */
10424 DECL_TEMPLATE_INFO (r
)
10425 = build_template_info (gen_tmpl
, argvec
);
10426 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10429 = register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10431 /* We instantiated this while substituting into
10432 the type earlier (template/friend54.C). */
10435 /* We're not supposed to instantiate default arguments
10436 until they are called, for a template. But, for a
10439 template <class T> void f ()
10440 { extern void g(int i = T()); }
10442 we should do the substitution when the template is
10443 instantiated. We handle the member function case in
10444 instantiate_class_template since the default arguments
10445 might refer to other members of the class. */
10447 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10448 && !uses_template_parms (argvec
))
10449 tsubst_default_arguments (r
, complain
);
10452 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10454 /* Copy the list of befriending classes. */
10455 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10457 friends
= &TREE_CHAIN (*friends
))
10459 *friends
= copy_node (*friends
);
10460 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10465 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10467 maybe_retrofit_in_chrg (r
);
10468 if (DECL_CONSTRUCTOR_P (r
))
10469 grok_ctor_properties (ctx
, r
);
10470 if (DECL_INHERITED_CTOR_BASE (r
))
10471 deduce_inheriting_ctor (r
);
10472 /* If this is an instantiation of a member template, clone it.
10473 If it isn't, that'll be handled by
10474 clone_constructors_and_destructors. */
10475 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10476 clone_function_decl (r
, /*update_method_vec_p=*/0);
10478 else if ((complain
& tf_error
) != 0
10479 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10480 && !grok_op_properties (r
, /*complain=*/true))
10481 RETURN (error_mark_node
);
10483 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10484 SET_DECL_FRIEND_CONTEXT (r
,
10485 tsubst (DECL_FRIEND_CONTEXT (t
),
10486 args
, complain
, in_decl
));
10488 /* Possibly limit visibility based on template args. */
10489 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10490 if (DECL_VISIBILITY_SPECIFIED (t
))
10492 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10493 DECL_ATTRIBUTES (r
)
10494 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10496 determine_visibility (r
);
10497 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10498 && !processing_template_decl
)
10499 defaulted_late_check (r
);
10501 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10502 args
, complain
, in_decl
);
10508 tree type
= NULL_TREE
;
10510 tree expanded_types
= NULL_TREE
;
10511 tree prev_r
= NULL_TREE
;
10512 tree first_r
= NULL_TREE
;
10514 if (DECL_PACK_P (t
))
10516 /* If there is a local specialization that isn't a
10517 parameter pack, it means that we're doing a "simple"
10518 substitution from inside tsubst_pack_expansion. Just
10519 return the local specialization (which will be a single
10521 tree spec
= retrieve_local_specialization (t
);
10523 && TREE_CODE (spec
) == PARM_DECL
10524 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10527 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10528 the parameters in this function parameter pack. */
10529 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10530 complain
, in_decl
);
10531 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10533 len
= TREE_VEC_LENGTH (expanded_types
);
10535 /* Zero-length parameter packs are boring. Just substitute
10538 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10543 /* All we did was update the type. Make a note of that. */
10544 type
= expanded_types
;
10545 expanded_types
= NULL_TREE
;
10549 /* Loop through all of the parameters we'll build. When T is
10550 a function parameter pack, LEN is the number of expanded
10551 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10553 for (i
= 0; i
< len
; ++i
)
10557 if (DECL_TEMPLATE_PARM_P (t
))
10558 SET_DECL_TEMPLATE_PARM_P (r
);
10560 if (expanded_types
)
10561 /* We're on the Ith parameter of the function parameter
10564 /* Get the Ith type. */
10565 type
= TREE_VEC_ELT (expanded_types
, i
);
10567 /* Rename the parameter to include the index. */
10569 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10572 /* We're dealing with a normal parameter. */
10573 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10575 type
= type_decays_to (type
);
10576 TREE_TYPE (r
) = type
;
10577 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10579 if (DECL_INITIAL (r
))
10581 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10582 DECL_INITIAL (r
) = TREE_TYPE (r
);
10584 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10585 complain
, in_decl
);
10588 DECL_CONTEXT (r
) = NULL_TREE
;
10590 if (!DECL_TEMPLATE_PARM_P (r
))
10591 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10593 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10594 args
, complain
, in_decl
);
10596 /* Keep track of the first new parameter we
10597 generate. That's what will be returned to the
10602 /* Build a proper chain of parameters when substituting
10603 into a function parameter pack. */
10605 DECL_CHAIN (prev_r
) = r
;
10608 /* If cp_unevaluated_operand is set, we're just looking for a
10609 single dummy parameter, so don't keep going. */
10610 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10611 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10612 complain
, DECL_CHAIN (t
));
10614 /* FIRST_R contains the start of the chain we've built. */
10621 tree type
= NULL_TREE
;
10622 tree vec
= NULL_TREE
;
10623 tree expanded_types
= NULL_TREE
;
10626 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
10628 /* This field is a lambda capture pack. Return a TREE_VEC of
10629 the expanded fields to instantiate_class_template_1 and
10630 store them in the specializations hash table as a
10631 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10632 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10633 complain
, in_decl
);
10634 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10636 len
= TREE_VEC_LENGTH (expanded_types
);
10637 vec
= make_tree_vec (len
);
10641 /* All we did was update the type. Make a note of that. */
10642 type
= expanded_types
;
10643 expanded_types
= NULL_TREE
;
10647 for (int i
= 0; i
< len
; ++i
)
10650 if (expanded_types
)
10652 type
= TREE_VEC_ELT (expanded_types
, i
);
10654 = make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10657 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10659 if (type
== error_mark_node
)
10660 RETURN (error_mark_node
);
10661 TREE_TYPE (r
) = type
;
10662 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10664 if (DECL_C_BIT_FIELD (r
))
10665 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10666 non-bit-fields DECL_INITIAL is a non-static data member
10667 initializer, which gets deferred instantiation. */
10669 = tsubst_expr (DECL_INITIAL (t
), args
,
10671 /*integral_constant_expression_p=*/true);
10672 else if (DECL_INITIAL (t
))
10674 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10675 NSDMI in perform_member_init. Still set DECL_INITIAL
10676 so that we know there is one. */
10677 DECL_INITIAL (r
) = void_zero_node
;
10678 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10679 retrofit_lang_decl (r
);
10680 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10682 /* We don't have to set DECL_CONTEXT here; it is set by
10683 finish_member_declaration. */
10684 DECL_CHAIN (r
) = NULL_TREE
;
10686 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10687 args
, complain
, in_decl
);
10690 TREE_VEC_ELT (vec
, i
) = r
;
10696 tree pack
= make_node (NONTYPE_ARGUMENT_PACK
);
10697 tree tpack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
10698 SET_ARGUMENT_PACK_ARGS (pack
, vec
);
10699 SET_ARGUMENT_PACK_ARGS (tpack
, expanded_types
);
10700 TREE_TYPE (pack
) = tpack
;
10701 register_specialization (pack
, t
, args
, false, 0);
10707 /* We reach here only for member using decls. We also need to check
10708 uses_template_parms because DECL_DEPENDENT_P is not set for a
10709 using-declaration that designates a member of the current
10710 instantiation (c++/53549). */
10711 if (DECL_DEPENDENT_P (t
)
10712 || uses_template_parms (USING_DECL_SCOPE (t
)))
10714 tree inst_scope
= tsubst_copy (USING_DECL_SCOPE (t
), args
,
10715 complain
, in_decl
);
10716 tree name
= tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
);
10717 r
= do_class_using_decl (inst_scope
, name
);
10719 r
= error_mark_node
;
10722 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10723 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10729 DECL_CHAIN (r
) = NULL_TREE
;
10736 tree argvec
= NULL_TREE
;
10737 tree gen_tmpl
= NULL_TREE
;
10739 tree tmpl
= NULL_TREE
;
10741 tree type
= NULL_TREE
;
10744 if (TREE_CODE (t
) == TYPE_DECL
10745 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10747 /* If this is the canonical decl, we don't have to
10748 mess with instantiations, and often we can't (for
10749 typename, template type parms and such). Note that
10750 TYPE_NAME is not correct for the above test if
10751 we've copied the type for a typedef. */
10752 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10753 if (type
== error_mark_node
)
10754 RETURN (error_mark_node
);
10755 r
= TYPE_NAME (type
);
10759 /* Check to see if we already have the specialization we
10762 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10764 /* T is a static data member or namespace-scope entity.
10765 We have to substitute into namespace-scope variables
10766 (even though such entities are never templates) because
10769 template <class T> void f() { extern T t; }
10771 where the entity referenced is not known until
10772 instantiation time. */
10774 ctx
= DECL_CONTEXT (t
);
10775 if (DECL_CLASS_SCOPE_P (t
))
10777 ctx
= tsubst_aggr_type (ctx
, args
,
10779 in_decl
, /*entering_scope=*/1);
10780 /* If CTX is unchanged, then T is in fact the
10781 specialization we want. That situation occurs when
10782 referencing a static data member within in its own
10783 class. We can use pointer equality, rather than
10784 same_type_p, because DECL_CONTEXT is always
10786 if (ctx
== DECL_CONTEXT (t
)
10787 && (TREE_CODE (t
) != TYPE_DECL
10788 /* ... unless T is a member template; in which
10789 case our caller can be willing to create a
10790 specialization of that template represented
10792 || !(DECL_TI_TEMPLATE (t
)
10793 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10799 tmpl
= DECL_TI_TEMPLATE (t
);
10800 gen_tmpl
= most_general_template (tmpl
);
10801 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10802 if (argvec
== error_mark_node
)
10803 RETURN (error_mark_node
);
10804 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10805 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10810 /* A local variable. */
10812 /* Subsequent calls to pushdecl will fill this in. */
10814 spec
= retrieve_local_specialization (t
);
10816 /* If we already have the specialization we need, there is
10817 nothing more to do. */
10824 /* Create a new node for the specialization we need. */
10826 if (type
== NULL_TREE
)
10828 if (is_typedef_decl (t
))
10829 type
= DECL_ORIGINAL_TYPE (t
);
10831 type
= TREE_TYPE (t
);
10833 && VAR_HAD_UNKNOWN_BOUND (t
)
10834 && type
!= error_mark_node
)
10835 type
= strip_array_domain (type
);
10836 type
= tsubst (type
, args
, complain
, in_decl
);
10840 /* Even if the original location is out of scope, the
10841 newly substituted one is not. */
10842 DECL_DEAD_FOR_LOCAL (r
) = 0;
10843 DECL_INITIALIZED_P (r
) = 0;
10844 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10845 if (type
== error_mark_node
)
10846 RETURN (error_mark_node
);
10847 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10849 /* It may seem that this case cannot occur, since:
10854 declares a function, not a variable. However:
10857 template <typename T> void g() { T t; }
10858 template void g<f>();
10860 is an attempt to declare a variable with function
10862 error ("variable %qD has function type",
10863 /* R is not yet sufficiently initialized, so we
10864 just use its name. */
10866 RETURN (error_mark_node
);
10868 type
= complete_type (type
);
10869 /* Wait until cp_finish_decl to set this again, to handle
10870 circular dependency (template/instantiate6.C). */
10871 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10872 type
= check_var_type (DECL_NAME (r
), type
);
10874 if (DECL_HAS_VALUE_EXPR_P (t
))
10876 tree ve
= DECL_VALUE_EXPR (t
);
10877 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10878 /*constant_expression_p=*/false);
10879 if (REFERENCE_REF_P (ve
))
10881 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10882 ve
= TREE_OPERAND (ve
, 0);
10884 SET_DECL_VALUE_EXPR (r
, ve
);
10887 else if (DECL_SELF_REFERENCE_P (t
))
10888 SET_DECL_SELF_REFERENCE_P (r
);
10889 TREE_TYPE (r
) = type
;
10890 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10891 DECL_CONTEXT (r
) = ctx
;
10892 /* Clear out the mangled name and RTL for the instantiation. */
10893 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10894 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10895 SET_DECL_RTL (r
, NULL
);
10896 /* The initializer must not be expanded until it is required;
10897 see [temp.inst]. */
10898 DECL_INITIAL (r
) = NULL_TREE
;
10899 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10900 SET_DECL_RTL (r
, NULL
);
10901 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10904 /* Possibly limit visibility based on template args. */
10905 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10906 if (DECL_VISIBILITY_SPECIFIED (t
))
10908 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10909 DECL_ATTRIBUTES (r
)
10910 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10912 determine_visibility (r
);
10917 /* A static data member declaration is always marked
10918 external when it is declared in-class, even if an
10919 initializer is present. We mimic the non-template
10920 processing here. */
10921 DECL_EXTERNAL (r
) = 1;
10923 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10924 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10925 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10927 else if (cp_unevaluated_operand
)
10928 gcc_unreachable ();
10930 register_local_specialization (r
, t
);
10932 DECL_CHAIN (r
) = NULL_TREE
;
10934 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10936 args
, complain
, in_decl
);
10938 /* Preserve a typedef that names a type. */
10939 if (is_typedef_decl (r
))
10941 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10942 set_underlying_type (r
);
10945 layout_decl (r
, 0);
10950 gcc_unreachable ();
10955 /* Restore the file and line information. */
10956 input_location
= saved_loc
;
10961 /* Substitute into the ARG_TYPES of a function type.
10962 If END is a TREE_CHAIN, leave it and any following types
10966 tsubst_arg_types (tree arg_types
,
10969 tsubst_flags_t complain
,
10972 tree remaining_arg_types
;
10973 tree type
= NULL_TREE
;
10975 tree expanded_args
= NULL_TREE
;
10978 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10981 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10982 args
, end
, complain
, in_decl
);
10983 if (remaining_arg_types
== error_mark_node
)
10984 return error_mark_node
;
10986 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10988 /* For a pack expansion, perform substitution on the
10989 entire expression. Later on, we'll handle the arguments
10991 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10992 args
, complain
, in_decl
);
10994 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10995 /* So that we'll spin through the parameters, one by one. */
10996 i
= TREE_VEC_LENGTH (expanded_args
);
10999 /* We only partially substituted into the parameter
11000 pack. Our type is TYPE_PACK_EXPANSION. */
11001 type
= expanded_args
;
11002 expanded_args
= NULL_TREE
;
11010 type
= TREE_VEC_ELT (expanded_args
, i
);
11012 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
11014 if (type
== error_mark_node
)
11015 return error_mark_node
;
11016 if (VOID_TYPE_P (type
))
11018 if (complain
& tf_error
)
11020 error ("invalid parameter type %qT", type
);
11022 error ("in declaration %q+D", in_decl
);
11024 return error_mark_node
;
11027 if (abstract_virtuals_error_sfinae (ACU_PARM
, type
, complain
))
11028 return error_mark_node
;
11030 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11031 top-level qualifiers as required. */
11032 type
= cv_unqualified (type_decays_to (type
));
11034 /* We do not substitute into default arguments here. The standard
11035 mandates that they be instantiated only when needed, which is
11036 done in build_over_call. */
11037 default_arg
= TREE_PURPOSE (arg_types
);
11039 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
11041 /* We've instantiated a template before its default arguments
11042 have been parsed. This can happen for a nested template
11043 class, and is not an error unless we require the default
11044 argument in a call of this function. */
11045 remaining_arg_types
=
11046 tree_cons (default_arg
, type
, remaining_arg_types
);
11047 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg
), remaining_arg_types
);
11050 remaining_arg_types
=
11051 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
11054 return remaining_arg_types
;
11057 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11058 *not* handle the exception-specification for FNTYPE, because the
11059 initial substitution of explicitly provided template parameters
11060 during argument deduction forbids substitution into the
11061 exception-specification:
11065 All references in the function type of the function template to the
11066 corresponding template parameters are replaced by the specified tem-
11067 plate argument values. If a substitution in a template parameter or
11068 in the function type of the function template results in an invalid
11069 type, type deduction fails. [Note: The equivalent substitution in
11070 exception specifications is done only when the function is instanti-
11071 ated, at which point a program is ill-formed if the substitution
11072 results in an invalid type.] */
11075 tsubst_function_type (tree t
,
11077 tsubst_flags_t complain
,
11084 /* The TYPE_CONTEXT is not used for function/method types. */
11085 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
11087 /* Substitute the return type. */
11088 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11089 if (return_type
== error_mark_node
)
11090 return error_mark_node
;
11091 /* DR 486 clarifies that creation of a function type with an
11092 invalid return type is a deduction failure. */
11093 if (TREE_CODE (return_type
) == ARRAY_TYPE
11094 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
11096 if (complain
& tf_error
)
11098 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
11099 error ("function returning an array");
11101 error ("function returning a function");
11103 return error_mark_node
;
11106 if (abstract_virtuals_error_sfinae (ACU_RETURN
, return_type
, complain
))
11107 return error_mark_node
;
11109 /* Substitute the argument types. */
11110 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
11111 complain
, in_decl
);
11112 if (arg_types
== error_mark_node
)
11113 return error_mark_node
;
11115 /* Construct a new type node and return it. */
11116 if (TREE_CODE (t
) == FUNCTION_TYPE
)
11118 fntype
= build_function_type (return_type
, arg_types
);
11119 fntype
= apply_memfn_quals (fntype
,
11120 type_memfn_quals (t
),
11121 type_memfn_rqual (t
));
11125 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
11126 if (! MAYBE_CLASS_TYPE_P (r
))
11130 Type deduction may fail for any of the following
11133 -- Attempting to create "pointer to member of T" when T
11134 is not a class type. */
11135 if (complain
& tf_error
)
11136 error ("creating pointer to member function of non-class type %qT",
11138 return error_mark_node
;
11141 fntype
= build_method_type_directly (r
, return_type
,
11142 TREE_CHAIN (arg_types
));
11143 fntype
= build_ref_qualified_type (fntype
, type_memfn_rqual (t
));
11145 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
11150 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11151 ARGS into that specification, and return the substituted
11152 specification. If there is no specification, return NULL_TREE. */
11155 tsubst_exception_specification (tree fntype
,
11157 tsubst_flags_t complain
,
11164 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11165 new_specs
= NULL_TREE
;
11166 if (specs
&& TREE_PURPOSE (specs
))
11168 /* A noexcept-specifier. */
11169 tree expr
= TREE_PURPOSE (specs
);
11170 if (TREE_CODE (expr
) == INTEGER_CST
)
11174 /* Defer instantiation of noexcept-specifiers to avoid
11175 excessive instantiations (c++/49107). */
11176 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11177 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11179 /* We already partially instantiated this member template,
11180 so combine the new args with the old. */
11181 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11182 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11183 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11184 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11188 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11189 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11193 new_specs
= tsubst_copy_and_build
11194 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11195 /*integral_constant_expression_p=*/true);
11196 new_specs
= build_noexcept_spec (new_specs
, complain
);
11200 if (! TREE_VALUE (specs
))
11207 tree expanded_specs
= NULL_TREE
;
11209 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11211 /* Expand the pack expansion type. */
11212 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11216 if (expanded_specs
== error_mark_node
)
11217 return error_mark_node
;
11218 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11219 len
= TREE_VEC_LENGTH (expanded_specs
);
11222 /* We're substituting into a member template, so
11223 we got a TYPE_PACK_EXPANSION back. Add that
11224 expansion and move on. */
11225 gcc_assert (TREE_CODE (expanded_specs
)
11226 == TYPE_PACK_EXPANSION
);
11227 new_specs
= add_exception_specifier (new_specs
,
11230 specs
= TREE_CHAIN (specs
);
11235 for (i
= 0; i
< len
; ++i
)
11237 if (expanded_specs
)
11238 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11240 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11241 if (spec
== error_mark_node
)
11243 new_specs
= add_exception_specifier (new_specs
, spec
,
11247 specs
= TREE_CHAIN (specs
);
11253 /* Take the tree structure T and replace template parameters used
11254 therein with the argument vector ARGS. IN_DECL is an associated
11255 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11256 Issue error and warning messages under control of COMPLAIN. Note
11257 that we must be relatively non-tolerant of extensions here, in
11258 order to preserve conformance; if we allow substitutions that
11259 should not be allowed, we may allow argument deductions that should
11260 not succeed, and therefore report ambiguous overload situations
11261 where there are none. In theory, we could allow the substitution,
11262 but indicate that it should have failed, and allow our caller to
11263 make sure that the right thing happens, but we don't try to do this
11266 This function is used for dealing with types, decls and the like;
11267 for expressions, use tsubst_expr or tsubst_copy. */
11270 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11272 enum tree_code code
;
11273 tree type
, r
= NULL_TREE
;
11275 if (t
== NULL_TREE
|| t
== error_mark_node
11276 || t
== integer_type_node
11277 || t
== void_type_node
11278 || t
== char_type_node
11279 || t
== unknown_type_node
11280 || TREE_CODE (t
) == NAMESPACE_DECL
11281 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11285 return tsubst_decl (t
, args
, complain
);
11287 if (args
== NULL_TREE
)
11290 code
= TREE_CODE (t
);
11292 if (code
== IDENTIFIER_NODE
)
11293 type
= IDENTIFIER_TYPE_VALUE (t
);
11295 type
= TREE_TYPE (t
);
11297 gcc_assert (type
!= unknown_type_node
);
11299 /* Reuse typedefs. We need to do this to handle dependent attributes,
11300 such as attribute aligned. */
11302 && typedef_variant_p (t
))
11304 tree decl
= TYPE_NAME (t
);
11306 if (alias_template_specialization_p (t
))
11308 /* DECL represents an alias template and we want to
11310 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11311 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11312 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
11314 else if (DECL_CLASS_SCOPE_P (decl
)
11315 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11316 && uses_template_parms (DECL_CONTEXT (decl
)))
11318 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11319 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11320 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11322 else if (DECL_FUNCTION_SCOPE_P (decl
)
11323 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11324 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11325 r
= retrieve_local_specialization (decl
);
11327 /* The typedef is from a non-template context. */
11333 r
= cp_build_qualified_type_real
11334 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11335 complain
| tf_ignore_bad_quals
);
11340 /* We don't have an instantiation yet, so drop the typedef. */
11341 int quals
= cp_type_quals (t
);
11342 t
= DECL_ORIGINAL_TYPE (decl
);
11343 t
= cp_build_qualified_type_real (t
, quals
,
11344 complain
| tf_ignore_bad_quals
);
11349 && code
!= TYPENAME_TYPE
11350 && code
!= TEMPLATE_TYPE_PARM
11351 && code
!= IDENTIFIER_NODE
11352 && code
!= FUNCTION_TYPE
11353 && code
!= METHOD_TYPE
)
11354 type
= tsubst (type
, args
, complain
, in_decl
);
11355 if (type
== error_mark_node
)
11356 return error_mark_node
;
11362 case ENUMERAL_TYPE
:
11363 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11364 /*entering_scope=*/0);
11367 case IDENTIFIER_NODE
:
11378 if (t
== integer_type_node
)
11381 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11382 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11386 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11388 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11389 /*integral_constant_expression_p=*/false);
11391 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11393 if (TREE_CODE (max
) == NOP_EXPR
11394 && TREE_SIDE_EFFECTS (omax
)
11395 && !TREE_TYPE (max
))
11396 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11398 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11399 with TREE_SIDE_EFFECTS that indicates this is not an integral
11400 constant expression. */
11401 if (processing_template_decl
11402 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11404 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11405 TREE_SIDE_EFFECTS (max
) = 1;
11408 return compute_array_index_type (NULL_TREE
, max
, complain
);
11411 case TEMPLATE_TYPE_PARM
:
11412 case TEMPLATE_TEMPLATE_PARM
:
11413 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11414 case TEMPLATE_PARM_INDEX
:
11419 tree arg
= NULL_TREE
;
11423 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11424 template_parm_level_and_index (t
, &level
, &idx
);
11426 levels
= TMPL_ARGS_DEPTH (args
);
11427 if (level
<= levels
)
11429 arg
= TMPL_ARG (args
, level
, idx
);
11431 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11433 /* See through ARGUMENT_PACK_SELECT arguments. */
11434 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11435 /* If the selected argument is an expansion E, that most
11436 likely means we were called from
11437 gen_elem_of_pack_expansion_instantiation during the
11438 substituting of pack an argument pack (which Ith
11439 element is a pack expansion, where I is
11440 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11441 In this case, the Ith element resulting from this
11442 substituting is going to be a pack expansion, which
11443 pattern is the pattern of E. Let's return the
11445 gen_elem_of_pack_expansion_instantiation will
11446 build the resulting pack expansion from it. */
11447 if (PACK_EXPANSION_P (arg
))
11448 arg
= PACK_EXPANSION_PATTERN (arg
);
11452 if (arg
== error_mark_node
)
11453 return error_mark_node
;
11454 else if (arg
!= NULL_TREE
)
11456 if (ARGUMENT_PACK_P (arg
))
11457 /* If ARG is an argument pack, we don't actually want to
11458 perform a substitution here, because substitutions
11459 for argument packs are only done
11460 element-by-element. We can get to this point when
11461 substituting the type of a non-type template
11462 parameter pack, when that type actually contains
11463 template parameter packs from an outer template, e.g.,
11465 template<typename... Types> struct A {
11466 template<Types... Values> struct B { };
11470 if (code
== TEMPLATE_TYPE_PARM
)
11473 gcc_assert (TYPE_P (arg
));
11475 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11477 return cp_build_qualified_type_real
11478 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11480 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11482 /* We are processing a type constructed from a
11483 template template parameter. */
11484 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11485 args
, complain
, in_decl
);
11486 if (argvec
== error_mark_node
)
11487 return error_mark_node
;
11489 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11490 || TREE_CODE (arg
) == TEMPLATE_DECL
11491 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11493 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11494 /* Consider this code:
11496 template <template <class> class Template>
11498 template <class Arg> using Bind = Template<Arg>;
11501 template <template <class> class Template, class Arg>
11502 using Instantiate = Template<Arg>; //#0
11504 template <template <class> class Template,
11507 Instantiate<Internal<Template>::template Bind,
11510 When #1 is parsed, the
11511 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11512 parameter `Template' in #0 matches the
11513 UNBOUND_CLASS_TEMPLATE representing the argument
11514 `Internal<Template>::template Bind'; We then want
11515 to assemble the type `Bind<Argument>' that can't
11516 be fully created right now, because
11517 `Internal<Template>' not being complete, the Bind
11518 template cannot be looked up in that context. So
11519 we need to "store" `Bind<Argument>' for later
11520 when the context of Bind becomes complete. Let's
11521 store that in a TYPENAME_TYPE. */
11522 return make_typename_type (TYPE_CONTEXT (arg
),
11523 build_nt (TEMPLATE_ID_EXPR
,
11524 TYPE_IDENTIFIER (arg
),
11529 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11530 are resolving nested-types in the signature of a
11531 member function templates. Otherwise ARG is a
11532 TEMPLATE_DECL and is the real template to be
11534 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11535 arg
= TYPE_NAME (arg
);
11537 r
= lookup_template_class (arg
,
11539 DECL_CONTEXT (arg
),
11540 /*entering_scope=*/0,
11542 return cp_build_qualified_type_real
11543 (r
, cp_type_quals (t
), complain
);
11546 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11547 return convert_from_reference (unshare_expr (arg
));
11551 /* This can happen during the attempted tsubst'ing in
11552 unify. This means that we don't yet have any information
11553 about the template parameter in question. */
11556 /* Early in template argument deduction substitution, we don't
11557 want to reduce the level of 'auto', or it will be confused
11558 with a normal template parm in subsequent deduction. */
11559 if (is_auto (t
) && (complain
& tf_partial
))
11562 /* If we get here, we must have been looking at a parm for a
11563 more deeply nested template. Make a new version of this
11564 template parameter, but with a lower level. */
11567 case TEMPLATE_TYPE_PARM
:
11568 case TEMPLATE_TEMPLATE_PARM
:
11569 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11570 if (cp_type_quals (t
))
11572 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11573 r
= cp_build_qualified_type_real
11574 (r
, cp_type_quals (t
),
11575 complain
| (code
== TEMPLATE_TYPE_PARM
11576 ? tf_ignore_bad_quals
: 0));
11581 TEMPLATE_TYPE_PARM_INDEX (r
)
11582 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11583 r
, levels
, args
, complain
);
11584 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11585 TYPE_MAIN_VARIANT (r
) = r
;
11586 TYPE_POINTER_TO (r
) = NULL_TREE
;
11587 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11589 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11590 /* We have reduced the level of the template
11591 template parameter, but not the levels of its
11592 template parameters, so canonical_type_parameter
11593 will not be able to find the canonical template
11594 template parameter for this level. Thus, we
11595 require structural equality checking to compare
11596 TEMPLATE_TEMPLATE_PARMs. */
11597 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11598 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11599 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11601 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11603 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11605 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11606 complain
, in_decl
);
11607 if (argvec
== error_mark_node
)
11608 return error_mark_node
;
11610 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11611 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11616 case TEMPLATE_PARM_INDEX
:
11617 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11621 gcc_unreachable ();
11629 tree purpose
, value
, chain
;
11631 if (t
== void_list_node
)
11634 purpose
= TREE_PURPOSE (t
);
11637 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11638 if (purpose
== error_mark_node
)
11639 return error_mark_node
;
11641 value
= TREE_VALUE (t
);
11644 value
= tsubst (value
, args
, complain
, in_decl
);
11645 if (value
== error_mark_node
)
11646 return error_mark_node
;
11648 chain
= TREE_CHAIN (t
);
11649 if (chain
&& chain
!= void_type_node
)
11651 chain
= tsubst (chain
, args
, complain
, in_decl
);
11652 if (chain
== error_mark_node
)
11653 return error_mark_node
;
11655 if (purpose
== TREE_PURPOSE (t
)
11656 && value
== TREE_VALUE (t
)
11657 && chain
== TREE_CHAIN (t
))
11659 return hash_tree_cons (purpose
, value
, chain
);
11663 /* We should never be tsubsting a binfo. */
11664 gcc_unreachable ();
11667 /* A vector of template arguments. */
11668 gcc_assert (!type
);
11669 return tsubst_template_args (t
, args
, complain
, in_decl
);
11672 case REFERENCE_TYPE
:
11674 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11679 Type deduction may fail for any of the following
11682 -- Attempting to create a pointer to reference type.
11683 -- Attempting to create a reference to a reference type or
11684 a reference to void.
11686 Core issue 106 says that creating a reference to a reference
11687 during instantiation is no longer a cause for failure. We
11688 only enforce this check in strict C++98 mode. */
11689 if ((TREE_CODE (type
) == REFERENCE_TYPE
11690 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11691 || (code
== REFERENCE_TYPE
&& VOID_TYPE_P (type
)))
11693 static location_t last_loc
;
11695 /* We keep track of the last time we issued this error
11696 message to avoid spewing a ton of messages during a
11697 single bad template instantiation. */
11698 if (complain
& tf_error
11699 && last_loc
!= input_location
)
11701 if (VOID_TYPE_P (type
))
11702 error ("forming reference to void");
11703 else if (code
== POINTER_TYPE
)
11704 error ("forming pointer to reference type %qT", type
);
11706 error ("forming reference to reference type %qT", type
);
11707 last_loc
= input_location
;
11710 return error_mark_node
;
11712 else if (TREE_CODE (type
) == FUNCTION_TYPE
11713 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
11714 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
11716 if (complain
& tf_error
)
11718 if (code
== POINTER_TYPE
)
11719 error ("forming pointer to qualified function type %qT",
11722 error ("forming reference to qualified function type %qT",
11725 return error_mark_node
;
11727 else if (code
== POINTER_TYPE
)
11729 r
= build_pointer_type (type
);
11730 if (TREE_CODE (type
) == METHOD_TYPE
)
11731 r
= build_ptrmemfunc_type (r
);
11733 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11734 /* In C++0x, during template argument substitution, when there is an
11735 attempt to create a reference to a reference type, reference
11736 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11738 "If a template-argument for a template-parameter T names a type
11739 that is a reference to a type A, an attempt to create the type
11740 'lvalue reference to cv T' creates the type 'lvalue reference to
11741 A,' while an attempt to create the type type rvalue reference to
11742 cv T' creates the type T"
11744 r
= cp_build_reference_type
11746 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11748 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11749 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11751 if (cxx_dialect
>= cxx1y
11752 && !(TREE_CODE (t
) == REFERENCE_TYPE
&& REFERENCE_VLA_OK (t
))
11753 && array_of_runtime_bound_p (type
))
11755 if (complain
& tf_warning_or_error
)
11757 (input_location
, OPT_Wvla
,
11758 code
== REFERENCE_TYPE
11759 ? G_("cannot declare reference to array of runtime bound")
11760 : G_("cannot declare pointer to array of runtime bound"));
11762 r
= error_mark_node
;
11765 if (r
!= error_mark_node
)
11766 /* Will this ever be needed for TYPE_..._TO values? */
11773 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11774 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11778 Type deduction may fail for any of the following
11781 -- Attempting to create "pointer to member of T" when T
11782 is not a class type. */
11783 if (complain
& tf_error
)
11784 error ("creating pointer to member of non-class type %qT", r
);
11785 return error_mark_node
;
11787 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11789 if (complain
& tf_error
)
11790 error ("creating pointer to member reference type %qT", type
);
11791 return error_mark_node
;
11793 if (VOID_TYPE_P (type
))
11795 if (complain
& tf_error
)
11796 error ("creating pointer to member of type void");
11797 return error_mark_node
;
11799 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11800 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11802 /* The type of the implicit object parameter gets its
11803 cv-qualifiers from the FUNCTION_TYPE. */
11806 = build_memfn_type (type
, r
, type_memfn_quals (type
),
11807 type_memfn_rqual (type
));
11808 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11809 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11813 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11817 case FUNCTION_TYPE
:
11822 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11823 if (fntype
== error_mark_node
)
11824 return error_mark_node
;
11826 /* Substitute the exception specification. */
11827 specs
= tsubst_exception_specification (t
, args
, complain
,
11828 in_decl
, /*defer_ok*/true);
11829 if (specs
== error_mark_node
)
11830 return error_mark_node
;
11832 fntype
= build_exception_variant (fntype
, specs
);
11837 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11838 if (domain
== error_mark_node
)
11839 return error_mark_node
;
11841 /* As an optimization, we avoid regenerating the array type if
11842 it will obviously be the same as T. */
11843 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11846 /* These checks should match the ones in grokdeclarator.
11850 The deduction may fail for any of the following reasons:
11852 -- Attempting to create an array with an element type that
11853 is void, a function type, or a reference type, or [DR337]
11854 an abstract class type. */
11855 if (VOID_TYPE_P (type
)
11856 || TREE_CODE (type
) == FUNCTION_TYPE
11857 || TREE_CODE (type
) == REFERENCE_TYPE
)
11859 if (complain
& tf_error
)
11860 error ("creating array of %qT", type
);
11861 return error_mark_node
;
11864 if (abstract_virtuals_error_sfinae (ACU_ARRAY
, type
, complain
))
11865 return error_mark_node
;
11867 r
= build_cplus_array_type (type
, domain
);
11869 if (TYPE_USER_ALIGN (t
))
11871 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11872 TYPE_USER_ALIGN (r
) = 1;
11878 case TYPENAME_TYPE
:
11880 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11881 in_decl
, /*entering_scope=*/1);
11882 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11883 complain
, in_decl
);
11885 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11886 return error_mark_node
;
11888 if (!MAYBE_CLASS_TYPE_P (ctx
))
11890 if (complain
& tf_error
)
11891 error ("%qT is not a class, struct, or union type", ctx
);
11892 return error_mark_node
;
11894 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11896 /* Normally, make_typename_type does not require that the CTX
11897 have complete type in order to allow things like:
11899 template <class T> struct S { typename S<T>::X Y; };
11901 But, such constructs have already been resolved by this
11902 point, so here CTX really should have complete type, unless
11903 it's a partial instantiation. */
11904 ctx
= complete_type (ctx
);
11905 if (!COMPLETE_TYPE_P (ctx
))
11907 if (complain
& tf_error
)
11908 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11909 return error_mark_node
;
11913 f
= make_typename_type (ctx
, f
, typename_type
,
11914 complain
| tf_keep_type_decl
);
11915 if (f
== error_mark_node
)
11917 if (TREE_CODE (f
) == TYPE_DECL
)
11919 complain
|= tf_ignore_bad_quals
;
11923 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11925 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11927 if (complain
& tf_error
)
11928 error ("%qT resolves to %qT, which is not an enumeration type",
11931 return error_mark_node
;
11933 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11935 if (complain
& tf_error
)
11936 error ("%qT resolves to %qT, which is is not a class type",
11939 return error_mark_node
;
11943 return cp_build_qualified_type_real
11944 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11947 case UNBOUND_CLASS_TEMPLATE
:
11949 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11950 in_decl
, /*entering_scope=*/1);
11951 tree name
= TYPE_IDENTIFIER (t
);
11952 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11954 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11955 return error_mark_node
;
11958 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11959 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11966 ++cp_unevaluated_operand
;
11967 ++c_inhibit_evaluation_warnings
;
11969 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11971 /*integral_constant_expression_p=*/false);
11973 --cp_unevaluated_operand
;
11974 --c_inhibit_evaluation_warnings
;
11976 type
= finish_typeof (type
);
11977 return cp_build_qualified_type_real (type
,
11979 | cp_type_quals (type
),
11983 case DECLTYPE_TYPE
:
11987 ++cp_unevaluated_operand
;
11988 ++c_inhibit_evaluation_warnings
;
11990 type
= tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t
), args
,
11991 complain
|tf_decltype
, in_decl
,
11992 /*function_p*/false,
11993 /*integral_constant_expression*/false);
11995 --cp_unevaluated_operand
;
11996 --c_inhibit_evaluation_warnings
;
11998 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11999 type
= lambda_capture_field_type (type
,
12000 DECLTYPE_FOR_INIT_CAPTURE (t
));
12001 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
12002 type
= lambda_proxy_type (type
);
12005 bool id
= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
);
12006 if (id
&& TREE_CODE (DECLTYPE_TYPE_EXPR (t
)) == BIT_NOT_EXPR
12008 /* In a template ~id could be either a complement expression
12009 or an unqualified-id naming a destructor; if instantiating
12010 it produces an expression, it's not an id-expression or
12013 type
= finish_decltype_type (type
, id
, complain
);
12015 return cp_build_qualified_type_real (type
,
12017 | cp_type_quals (type
),
12021 case UNDERLYING_TYPE
:
12023 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
12024 complain
, in_decl
);
12025 return finish_underlying_type (type
);
12028 case TYPE_ARGUMENT_PACK
:
12029 case NONTYPE_ARGUMENT_PACK
:
12031 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
12033 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
12037 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
12039 /* For template nontype argument packs, also substitute into
12041 if (code
== NONTYPE_ARGUMENT_PACK
)
12042 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12060 /* We should use one of the expression tsubsts for these codes. */
12061 gcc_unreachable ();
12064 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
12065 return error_mark_node
;
12069 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12070 type of the expression on the left-hand side of the "." or "->"
12074 tsubst_baselink (tree baselink
, tree object_type
,
12075 tree args
, tsubst_flags_t complain
, tree in_decl
)
12078 tree qualifying_scope
;
12081 tree template_args
= 0;
12082 bool template_id_p
= false;
12083 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
12085 /* A baselink indicates a function from a base class. Both the
12086 BASELINK_ACCESS_BINFO and the base class referenced may
12087 indicate bases of the template class, rather than the
12088 instantiated class. In addition, lookups that were not
12089 ambiguous before may be ambiguous now. Therefore, we perform
12090 the lookup again. */
12091 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
12092 qualifying_scope
= tsubst (qualifying_scope
, args
,
12093 complain
, in_decl
);
12094 fns
= BASELINK_FUNCTIONS (baselink
);
12095 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
12096 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
12098 template_id_p
= true;
12099 template_args
= TREE_OPERAND (fns
, 1);
12100 fns
= TREE_OPERAND (fns
, 0);
12102 template_args
= tsubst_template_args (template_args
, args
,
12103 complain
, in_decl
);
12105 name
= DECL_NAME (get_first_fn (fns
));
12106 if (IDENTIFIER_TYPENAME_P (name
))
12107 name
= mangle_conv_op_name_for_type (optype
);
12108 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
12110 return error_mark_node
;
12112 /* If lookup found a single function, mark it as used at this
12113 point. (If it lookup found multiple functions the one selected
12114 later by overload resolution will be marked as used at that
12116 if (BASELINK_P (baselink
))
12117 fns
= BASELINK_FUNCTIONS (baselink
);
12118 if (!template_id_p
&& !really_overloaded_fn (fns
))
12119 mark_used (OVL_CURRENT (fns
));
12121 /* Add back the template arguments, if present. */
12122 if (BASELINK_P (baselink
) && template_id_p
)
12123 BASELINK_FUNCTIONS (baselink
)
12124 = build_nt (TEMPLATE_ID_EXPR
,
12125 BASELINK_FUNCTIONS (baselink
),
12127 /* Update the conversion operator type. */
12128 BASELINK_OPTYPE (baselink
) = optype
;
12131 object_type
= current_class_type
;
12134 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
12140 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12141 true if the qualified-id will be a postfix-expression in-and-of
12142 itself; false if more of the postfix-expression follows the
12143 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12147 tsubst_qualified_id (tree qualified_id
, tree args
,
12148 tsubst_flags_t complain
, tree in_decl
,
12149 bool done
, bool address_p
)
12155 tree template_args
;
12156 location_t loc
= UNKNOWN_LOCATION
;
12158 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
12160 /* Figure out what name to look up. */
12161 name
= TREE_OPERAND (qualified_id
, 1);
12162 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
12164 is_template
= true;
12165 loc
= EXPR_LOCATION (name
);
12166 template_args
= TREE_OPERAND (name
, 1);
12168 template_args
= tsubst_template_args (template_args
, args
,
12169 complain
, in_decl
);
12170 name
= TREE_OPERAND (name
, 0);
12174 is_template
= false;
12175 template_args
= NULL_TREE
;
12178 /* Substitute into the qualifying scope. When there are no ARGS, we
12179 are just trying to simplify a non-dependent expression. In that
12180 case the qualifying scope may be dependent, and, in any case,
12181 substituting will not help. */
12182 scope
= TREE_OPERAND (qualified_id
, 0);
12185 scope
= tsubst (scope
, args
, complain
, in_decl
);
12186 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
12191 if (dependent_scope_p (scope
))
12194 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
12195 return build_qualified_name (NULL_TREE
, scope
, expr
,
12196 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
12199 if (!BASELINK_P (name
) && !DECL_P (expr
))
12201 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
12203 /* A BIT_NOT_EXPR is used to represent a destructor. */
12204 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
12206 error ("qualifying type %qT does not match destructor name ~%qT",
12207 scope
, TREE_OPERAND (expr
, 0));
12208 expr
= error_mark_node
;
12211 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
12212 /*is_type_p=*/0, false);
12215 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
12216 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
12217 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
12219 if (complain
& tf_error
)
12221 error ("dependent-name %qE is parsed as a non-type, but "
12222 "instantiation yields a type", qualified_id
);
12223 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12225 return error_mark_node
;
12231 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12233 /* Remember that there was a reference to this entity. */
12237 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12239 if (complain
& tf_error
)
12240 qualified_name_lookup_error (scope
,
12241 TREE_OPERAND (qualified_id
, 1),
12242 expr
, input_location
);
12243 return error_mark_node
;
12247 expr
= lookup_template_function (expr
, template_args
);
12249 if (expr
== error_mark_node
&& complain
& tf_error
)
12250 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12251 expr
, input_location
);
12252 else if (TYPE_P (scope
))
12254 expr
= (adjust_result_of_qualified_name_lookup
12255 (expr
, scope
, current_nonlambda_class_type ()));
12256 expr
= (finish_qualified_id_expr
12257 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12258 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12259 /*template_arg_p=*/false, complain
));
12262 /* Expressions do not generally have reference type. */
12263 if (TREE_CODE (expr
) != SCOPE_REF
12264 /* However, if we're about to form a pointer-to-member, we just
12265 want the referenced member referenced. */
12266 && TREE_CODE (expr
) != OFFSET_REF
)
12267 expr
= convert_from_reference (expr
);
12272 /* Like tsubst, but deals with expressions. This function just replaces
12273 template parms; to finish processing the resultant expression, use
12274 tsubst_copy_and_build or tsubst_expr. */
12277 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12279 enum tree_code code
;
12282 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12285 code
= TREE_CODE (t
);
12290 r
= retrieve_local_specialization (t
);
12292 if (r
== NULL_TREE
)
12294 /* We get here for a use of 'this' in an NSDMI. */
12295 if (DECL_NAME (t
) == this_identifier
12296 && at_function_scope_p ()
12297 && DECL_CONSTRUCTOR_P (current_function_decl
))
12298 return current_class_ptr
;
12300 /* This can happen for a parameter name used later in a function
12301 declaration (such as in a late-specified return type). Just
12302 make a dummy decl, since it's only used for its type. */
12303 gcc_assert (cp_unevaluated_operand
!= 0);
12304 r
= tsubst_decl (t
, args
, complain
);
12305 /* Give it the template pattern as its context; its true context
12306 hasn't been instantiated yet and this is good enough for
12308 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12311 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12312 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12321 if (DECL_TEMPLATE_PARM_P (t
))
12322 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12323 /* There is no need to substitute into namespace-scope
12325 if (DECL_NAMESPACE_SCOPE_P (t
))
12327 /* If ARGS is NULL, then T is known to be non-dependent. */
12328 if (args
== NULL_TREE
)
12329 return integral_constant_value (t
);
12331 /* Unfortunately, we cannot just call lookup_name here.
12334 template <int I> int f() {
12336 struct S { void g() { E e = a; } };
12339 When we instantiate f<7>::S::g(), say, lookup_name is not
12340 clever enough to find f<7>::a. */
12342 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12343 /*entering_scope=*/0);
12345 for (v
= TYPE_VALUES (enum_type
);
12347 v
= TREE_CHAIN (v
))
12348 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12349 return TREE_VALUE (v
);
12351 /* We didn't find the name. That should never happen; if
12352 name-lookup found it during preliminary parsing, we
12353 should find it again here during instantiation. */
12354 gcc_unreachable ();
12359 if (PACK_EXPANSION_P (TREE_TYPE (t
)))
12361 /* Check for a local specialization set up by
12362 tsubst_pack_expansion. */
12363 tree r
= retrieve_local_specialization (t
);
12366 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12367 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12371 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12372 tsubst_decl put in the hash table. */
12373 return retrieve_specialization (t
, args
, 0);
12376 if (DECL_CONTEXT (t
))
12380 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12381 /*entering_scope=*/1);
12382 if (ctx
!= DECL_CONTEXT (t
))
12384 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12387 if (complain
& tf_error
)
12388 error ("using invalid field %qD", t
);
12389 return error_mark_node
;
12398 case FUNCTION_DECL
:
12399 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12400 r
= tsubst (t
, args
, complain
, in_decl
);
12401 else if (local_variable_p (t
))
12403 r
= retrieve_local_specialization (t
);
12404 if (r
== NULL_TREE
)
12406 if (DECL_ANON_UNION_VAR_P (t
))
12408 /* Just use name lookup to find a member alias for an
12409 anonymous union, but then add it to the hash table. */
12410 r
= lookup_name (DECL_NAME (t
));
12411 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
12412 register_local_specialization (r
, t
);
12416 gcc_assert (errorcount
|| sorrycount
);
12417 return error_mark_node
;
12426 case NAMESPACE_DECL
:
12430 /* An OVERLOAD will always be a non-dependent overload set; an
12431 overload set from function scope will just be represented with an
12432 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12433 gcc_assert (!uses_template_parms (t
));
12437 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12439 case TEMPLATE_DECL
:
12440 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12441 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12442 args
, complain
, in_decl
);
12443 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12444 return tsubst (t
, args
, complain
, in_decl
);
12445 else if (DECL_CLASS_SCOPE_P (t
)
12446 && uses_template_parms (DECL_CONTEXT (t
)))
12448 /* Template template argument like the following example need
12451 template <template <class> class TT> struct C {};
12452 template <class T> struct D {
12453 template <class U> struct E {};
12458 We are processing the template argument `E' in #1 for
12459 the template instantiation #2. Originally, `E' is a
12460 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12461 have to substitute this with one having context `D<int>'. */
12463 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12464 return lookup_field (context
, DECL_NAME(t
), 0, false);
12467 /* Ordinary template template argument. */
12471 case REINTERPRET_CAST_EXPR
:
12472 case CONST_CAST_EXPR
:
12473 case STATIC_CAST_EXPR
:
12474 case DYNAMIC_CAST_EXPR
:
12475 case IMPLICIT_CONV_EXPR
:
12479 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12480 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12483 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12486 tree expanded
, op
= TREE_OPERAND (t
, 0);
12489 if (SIZEOF_EXPR_TYPE_P (t
))
12490 op
= TREE_TYPE (op
);
12492 ++cp_unevaluated_operand
;
12493 ++c_inhibit_evaluation_warnings
;
12494 /* We only want to compute the number of arguments. */
12495 expanded
= tsubst_pack_expansion (op
, args
, complain
, in_decl
);
12496 --cp_unevaluated_operand
;
12497 --c_inhibit_evaluation_warnings
;
12499 if (TREE_CODE (expanded
) == TREE_VEC
)
12500 len
= TREE_VEC_LENGTH (expanded
);
12502 if (expanded
== error_mark_node
)
12503 return error_mark_node
;
12504 else if (PACK_EXPANSION_P (expanded
)
12505 || (TREE_CODE (expanded
) == TREE_VEC
12507 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12509 if (TREE_CODE (expanded
) == TREE_VEC
)
12510 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12512 if (TYPE_P (expanded
))
12513 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12514 complain
& tf_error
);
12516 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12517 complain
& tf_error
);
12520 return build_int_cst (size_type_node
, len
);
12522 if (SIZEOF_EXPR_TYPE_P (t
))
12524 r
= tsubst (TREE_TYPE (TREE_OPERAND (t
, 0)),
12525 args
, complain
, in_decl
);
12526 r
= build1 (NOP_EXPR
, r
, error_mark_node
);
12527 r
= build1 (SIZEOF_EXPR
,
12528 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
), r
);
12529 SIZEOF_EXPR_TYPE_P (r
) = 1;
12536 case TRUTH_NOT_EXPR
:
12539 case UNARY_PLUS_EXPR
: /* Unary + */
12541 case AT_ENCODE_EXPR
:
12545 case REALPART_EXPR
:
12546 case IMAGPART_EXPR
:
12549 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12550 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12552 case COMPONENT_REF
:
12557 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12558 name
= TREE_OPERAND (t
, 1);
12559 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12561 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12562 complain
, in_decl
);
12563 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12565 else if (TREE_CODE (name
) == SCOPE_REF
12566 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12568 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12569 complain
, in_decl
);
12570 name
= TREE_OPERAND (name
, 1);
12571 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12572 complain
, in_decl
);
12573 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12574 name
= build_qualified_name (/*type=*/NULL_TREE
,
12576 /*template_p=*/false);
12578 else if (BASELINK_P (name
))
12579 name
= tsubst_baselink (name
,
12580 non_reference (TREE_TYPE (object
)),
12584 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12585 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12591 case TRUNC_DIV_EXPR
:
12592 case CEIL_DIV_EXPR
:
12593 case FLOOR_DIV_EXPR
:
12594 case ROUND_DIV_EXPR
:
12595 case EXACT_DIV_EXPR
:
12599 case TRUNC_MOD_EXPR
:
12600 case FLOOR_MOD_EXPR
:
12601 case TRUTH_ANDIF_EXPR
:
12602 case TRUTH_ORIF_EXPR
:
12603 case TRUTH_AND_EXPR
:
12604 case TRUTH_OR_EXPR
:
12617 case COMPOUND_EXPR
:
12620 case PREDECREMENT_EXPR
:
12621 case PREINCREMENT_EXPR
:
12622 case POSTDECREMENT_EXPR
:
12623 case POSTINCREMENT_EXPR
:
12625 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12626 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12629 return build_qualified_name (/*type=*/NULL_TREE
,
12630 tsubst_copy (TREE_OPERAND (t
, 0),
12631 args
, complain
, in_decl
),
12632 tsubst_copy (TREE_OPERAND (t
, 1),
12633 args
, complain
, in_decl
),
12634 QUALIFIED_NAME_IS_TEMPLATE (t
));
12639 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12640 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12641 NULL_TREE
, NULL_TREE
);
12645 int n
= VL_EXP_OPERAND_LENGTH (t
);
12646 tree result
= build_vl_exp (CALL_EXPR
, n
);
12648 for (i
= 0; i
< n
; i
++)
12649 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12650 complain
, in_decl
);
12656 case PSEUDO_DTOR_EXPR
:
12657 case VEC_PERM_EXPR
:
12660 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12661 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12662 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12663 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12670 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12671 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12672 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12673 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12680 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12681 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12682 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12683 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12687 case TEMPLATE_ID_EXPR
:
12689 /* Substituted template arguments */
12690 tree fn
= TREE_OPERAND (t
, 0);
12691 tree targs
= TREE_OPERAND (t
, 1);
12693 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12695 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12697 return lookup_template_function (fn
, targs
);
12702 tree purpose
, value
, chain
;
12704 if (t
== void_list_node
)
12707 purpose
= TREE_PURPOSE (t
);
12709 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12710 value
= TREE_VALUE (t
);
12712 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12713 chain
= TREE_CHAIN (t
);
12714 if (chain
&& chain
!= void_type_node
)
12715 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12716 if (purpose
== TREE_PURPOSE (t
)
12717 && value
== TREE_VALUE (t
)
12718 && chain
== TREE_CHAIN (t
))
12720 return tree_cons (purpose
, value
, chain
);
12725 case ENUMERAL_TYPE
:
12727 case TEMPLATE_TYPE_PARM
:
12728 case TEMPLATE_TEMPLATE_PARM
:
12729 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12730 case TEMPLATE_PARM_INDEX
:
12732 case REFERENCE_TYPE
:
12734 case FUNCTION_TYPE
:
12737 case TYPENAME_TYPE
:
12738 case UNBOUND_CLASS_TEMPLATE
:
12740 case DECLTYPE_TYPE
:
12742 return tsubst (t
, args
, complain
, in_decl
);
12746 /* Fall through. */
12747 case IDENTIFIER_NODE
:
12748 if (IDENTIFIER_TYPENAME_P (t
))
12750 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12751 return mangle_conv_op_name_for_type (new_type
);
12757 /* This is handled by tsubst_copy_and_build. */
12758 gcc_unreachable ();
12761 return build_x_va_arg (EXPR_LOCATION (t
),
12762 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12764 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12766 case CLEANUP_POINT_EXPR
:
12767 /* We shouldn't have built any of these during initial template
12768 generation. Instead, they should be built during instantiation
12769 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12770 gcc_unreachable ();
12774 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12775 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12776 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12777 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12778 mark_used (TREE_OPERAND (r
, 1));
12781 case EXPR_PACK_EXPANSION
:
12782 error ("invalid use of pack expansion expression");
12783 return error_mark_node
;
12785 case NONTYPE_ARGUMENT_PACK
:
12786 error ("use %<...%> to expand argument pack");
12787 return error_mark_node
;
12794 /* Instantiate any typedefs in the type. */
12795 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12796 r
= fold_convert (type
, t
);
12797 gcc_assert (TREE_CODE (r
) == code
);
12802 /* These can sometimes show up in a partial instantiation, but never
12803 involve template parms. */
12804 gcc_assert (!uses_template_parms (t
));
12808 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12809 gcc_checking_assert (false);
12814 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12817 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12820 tree new_clauses
= NULL
, nc
, oc
;
12822 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12824 nc
= copy_node (oc
);
12825 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12828 switch (OMP_CLAUSE_CODE (nc
))
12830 case OMP_CLAUSE_LASTPRIVATE
:
12831 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12833 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12834 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12835 in_decl
, /*integral_constant_expression_p=*/false);
12836 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12837 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12840 case OMP_CLAUSE_PRIVATE
:
12841 case OMP_CLAUSE_SHARED
:
12842 case OMP_CLAUSE_FIRSTPRIVATE
:
12843 case OMP_CLAUSE_REDUCTION
:
12844 case OMP_CLAUSE_COPYIN
:
12845 case OMP_CLAUSE_COPYPRIVATE
:
12846 case OMP_CLAUSE_IF
:
12847 case OMP_CLAUSE_NUM_THREADS
:
12848 case OMP_CLAUSE_SCHEDULE
:
12849 case OMP_CLAUSE_COLLAPSE
:
12850 case OMP_CLAUSE_FINAL
:
12851 OMP_CLAUSE_OPERAND (nc
, 0)
12852 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12853 in_decl
, /*integral_constant_expression_p=*/false);
12855 case OMP_CLAUSE_NOWAIT
:
12856 case OMP_CLAUSE_ORDERED
:
12857 case OMP_CLAUSE_DEFAULT
:
12858 case OMP_CLAUSE_UNTIED
:
12859 case OMP_CLAUSE_MERGEABLE
:
12862 gcc_unreachable ();
12866 return finish_omp_clauses (nreverse (new_clauses
));
12869 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12872 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12875 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12877 tree purpose
, value
, chain
;
12882 if (TREE_CODE (t
) != TREE_LIST
)
12883 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12884 /*function_p=*/false,
12885 /*integral_constant_expression_p=*/false);
12887 if (t
== void_list_node
)
12890 purpose
= TREE_PURPOSE (t
);
12892 purpose
= RECUR (purpose
);
12893 value
= TREE_VALUE (t
);
12896 if (TREE_CODE (value
) != LABEL_DECL
)
12897 value
= RECUR (value
);
12900 value
= lookup_label (DECL_NAME (value
));
12901 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12902 TREE_USED (value
) = 1;
12905 chain
= TREE_CHAIN (t
);
12906 if (chain
&& chain
!= void_type_node
)
12907 chain
= RECUR (chain
);
12908 return tree_cons (purpose
, value
, chain
);
12912 /* Substitute one OMP_FOR iterator. */
12915 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12916 tree condv
, tree incrv
, tree
*clauses
,
12917 tree args
, tsubst_flags_t complain
, tree in_decl
,
12918 bool integral_constant_expression_p
)
12920 #define RECUR(NODE) \
12921 tsubst_expr ((NODE), args, complain, in_decl, \
12922 integral_constant_expression_p)
12923 tree decl
, init
, cond
, incr
;
12926 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12927 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12928 decl
= TREE_OPERAND (init
, 0);
12929 init
= TREE_OPERAND (init
, 1);
12930 /* Do this before substituting into decl to handle 'auto'. */
12931 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12932 init
= RECUR (init
);
12933 decl
= RECUR (decl
);
12936 gcc_assert (!processing_template_decl
);
12937 init
= DECL_INITIAL (decl
);
12938 DECL_INITIAL (decl
) = NULL_TREE
;
12941 gcc_assert (!type_dependent_expression_p (decl
));
12943 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12945 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12946 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12947 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12948 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12949 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12950 RECUR (TREE_OPERAND (incr
, 1)),
12953 incr
= RECUR (incr
);
12954 TREE_VEC_ELT (declv
, i
) = decl
;
12955 TREE_VEC_ELT (initv
, i
) = init
;
12956 TREE_VEC_ELT (condv
, i
) = cond
;
12957 TREE_VEC_ELT (incrv
, i
) = incr
;
12961 if (init
&& !init_decl
)
12964 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12966 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12967 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12968 && OMP_CLAUSE_DECL (c
) == decl
)
12970 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12971 && OMP_CLAUSE_DECL (c
) == decl
)
12972 error ("iteration variable %qD should not be firstprivate", decl
);
12973 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12974 && OMP_CLAUSE_DECL (c
) == decl
)
12975 error ("iteration variable %qD should not be reduction", decl
);
12979 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12980 OMP_CLAUSE_DECL (c
) = decl
;
12981 c
= finish_omp_clauses (c
);
12984 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12989 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12990 if (COMPARISON_CLASS_P (cond
))
12991 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12992 RECUR (TREE_OPERAND (cond
, 0)),
12993 RECUR (TREE_OPERAND (cond
, 1)));
12995 cond
= RECUR (cond
);
12996 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12997 switch (TREE_CODE (incr
))
12999 case PREINCREMENT_EXPR
:
13000 case PREDECREMENT_EXPR
:
13001 case POSTINCREMENT_EXPR
:
13002 case POSTDECREMENT_EXPR
:
13003 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
13004 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
13007 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13008 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13010 tree rhs
= TREE_OPERAND (incr
, 1);
13011 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13012 RECUR (TREE_OPERAND (incr
, 0)),
13013 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13014 RECUR (TREE_OPERAND (rhs
, 0)),
13015 RECUR (TREE_OPERAND (rhs
, 1))));
13018 incr
= RECUR (incr
);
13021 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
13022 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
13024 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
13025 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
13026 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
13027 TREE_TYPE (decl
), lhs
,
13028 RECUR (TREE_OPERAND (incr
, 2))));
13030 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
13031 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
13032 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
13034 tree rhs
= TREE_OPERAND (incr
, 2);
13035 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
13036 RECUR (TREE_OPERAND (incr
, 0)),
13037 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
13038 RECUR (TREE_OPERAND (rhs
, 0)),
13039 RECUR (TREE_OPERAND (rhs
, 1))));
13042 incr
= RECUR (incr
);
13045 incr
= RECUR (incr
);
13049 TREE_VEC_ELT (declv
, i
) = decl
;
13050 TREE_VEC_ELT (initv
, i
) = init
;
13051 TREE_VEC_ELT (condv
, i
) = cond
;
13052 TREE_VEC_ELT (incrv
, i
) = incr
;
13056 /* Like tsubst_copy for expressions, etc. but also does semantic
13060 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
13061 bool integral_constant_expression_p
)
13063 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13064 #define RECUR(NODE) \
13065 tsubst_expr ((NODE), args, complain, in_decl, \
13066 integral_constant_expression_p)
13072 if (t
== NULL_TREE
|| t
== error_mark_node
)
13075 loc
= input_location
;
13076 if (EXPR_HAS_LOCATION (t
))
13077 input_location
= EXPR_LOCATION (t
);
13078 if (STATEMENT_CODE_P (TREE_CODE (t
)))
13079 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
13081 switch (TREE_CODE (t
))
13083 case STATEMENT_LIST
:
13085 tree_stmt_iterator i
;
13086 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
13087 RECUR (tsi_stmt (i
));
13091 case CTOR_INITIALIZER
:
13092 finish_mem_initializers (tsubst_initializer_list
13093 (TREE_OPERAND (t
, 0), args
));
13097 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
13101 tmp
= RECUR (EXPR_STMT_EXPR (t
));
13102 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
13103 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
13105 finish_expr_stmt (tmp
);
13109 do_using_directive (USING_STMT_NAMESPACE (t
));
13114 tree decl
, pattern_decl
;
13117 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
13118 if (TREE_CODE (decl
) == LABEL_DECL
)
13119 finish_label_decl (DECL_NAME (decl
));
13120 else if (TREE_CODE (decl
) == USING_DECL
)
13122 tree scope
= USING_DECL_SCOPE (decl
);
13123 tree name
= DECL_NAME (decl
);
13126 scope
= tsubst (scope
, args
, complain
, in_decl
);
13127 decl
= lookup_qualified_name (scope
, name
,
13128 /*is_type_p=*/false,
13129 /*complain=*/false);
13130 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
13131 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
13133 do_local_using_decl (decl
, scope
, name
);
13135 else if (DECL_PACK_P (decl
))
13137 /* Don't build up decls for a variadic capture proxy, we'll
13138 instantiate the elements directly as needed. */
13143 init
= DECL_INITIAL (decl
);
13144 decl
= tsubst (decl
, args
, complain
, in_decl
);
13145 if (decl
!= error_mark_node
)
13147 /* By marking the declaration as instantiated, we avoid
13148 trying to instantiate it. Since instantiate_decl can't
13149 handle local variables, and since we've already done
13150 all that needs to be done, that's the right thing to
13153 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13155 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
13156 /* Anonymous aggregates are a special case. */
13157 finish_anon_union (decl
);
13158 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
13160 DECL_CONTEXT (decl
) = current_function_decl
;
13161 if (DECL_NAME (decl
) == this_identifier
)
13163 tree lam
= DECL_CONTEXT (current_function_decl
);
13164 lam
= CLASSTYPE_LAMBDA_EXPR (lam
);
13165 LAMBDA_EXPR_THIS_CAPTURE (lam
) = decl
;
13167 insert_capture_proxy (decl
);
13169 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
13170 /* We already did a pushtag. */;
13173 int const_init
= false;
13174 maybe_push_decl (decl
);
13176 && DECL_PRETTY_FUNCTION_P (decl
))
13178 /* For __PRETTY_FUNCTION__ we have to adjust the
13180 const char *const name
13181 = cxx_printable_name (current_function_decl
, 2);
13182 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
13186 tree t
= RECUR (init
);
13190 /* If we had an initializer but it
13191 instantiated to nothing,
13192 value-initialize the object. This will
13193 only occur when the initializer was a
13194 pack expansion where the parameter packs
13195 used in that expansion were of length
13197 init
= build_value_init (TREE_TYPE (decl
),
13199 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
13200 init
= get_target_expr_sfinae (init
, complain
);
13207 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13209 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
13218 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13219 RECUR (FOR_INIT_STMT (t
));
13220 finish_for_init_stmt (stmt
);
13221 tmp
= RECUR (FOR_COND (t
));
13222 finish_for_cond (tmp
, stmt
);
13223 tmp
= RECUR (FOR_EXPR (t
));
13224 finish_for_expr (tmp
, stmt
);
13225 RECUR (FOR_BODY (t
));
13226 finish_for_stmt (stmt
);
13229 case RANGE_FOR_STMT
:
13232 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
13233 decl
= RANGE_FOR_DECL (t
);
13234 decl
= tsubst (decl
, args
, complain
, in_decl
);
13235 maybe_push_decl (decl
);
13236 expr
= RECUR (RANGE_FOR_EXPR (t
));
13237 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
13238 RECUR (RANGE_FOR_BODY (t
));
13239 finish_for_stmt (stmt
);
13244 stmt
= begin_while_stmt ();
13245 tmp
= RECUR (WHILE_COND (t
));
13246 finish_while_stmt_cond (tmp
, stmt
);
13247 RECUR (WHILE_BODY (t
));
13248 finish_while_stmt (stmt
);
13252 stmt
= begin_do_stmt ();
13253 RECUR (DO_BODY (t
));
13254 finish_do_body (stmt
);
13255 tmp
= RECUR (DO_COND (t
));
13256 finish_do_stmt (tmp
, stmt
);
13260 stmt
= begin_if_stmt ();
13261 tmp
= RECUR (IF_COND (t
));
13262 finish_if_stmt_cond (tmp
, stmt
);
13263 RECUR (THEN_CLAUSE (t
));
13264 finish_then_clause (stmt
);
13266 if (ELSE_CLAUSE (t
))
13268 begin_else_clause (stmt
);
13269 RECUR (ELSE_CLAUSE (t
));
13270 finish_else_clause (stmt
);
13273 finish_if_stmt (stmt
);
13277 if (BIND_EXPR_BODY_BLOCK (t
))
13278 stmt
= begin_function_body ();
13280 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
13281 ? BCS_TRY_BLOCK
: 0);
13283 RECUR (BIND_EXPR_BODY (t
));
13285 if (BIND_EXPR_BODY_BLOCK (t
))
13286 finish_function_body (stmt
);
13288 finish_compound_stmt (stmt
);
13292 finish_break_stmt ();
13295 case CONTINUE_STMT
:
13296 finish_continue_stmt ();
13300 stmt
= begin_switch_stmt ();
13301 tmp
= RECUR (SWITCH_STMT_COND (t
));
13302 finish_switch_cond (tmp
, stmt
);
13303 RECUR (SWITCH_STMT_BODY (t
));
13304 finish_switch_stmt (stmt
);
13307 case CASE_LABEL_EXPR
:
13308 finish_case_label (EXPR_LOCATION (t
),
13309 RECUR (CASE_LOW (t
)),
13310 RECUR (CASE_HIGH (t
)));
13315 tree decl
= LABEL_EXPR_LABEL (t
);
13318 label
= finish_label_stmt (DECL_NAME (decl
));
13319 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13320 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13325 tmp
= GOTO_DESTINATION (t
);
13326 if (TREE_CODE (tmp
) != LABEL_DECL
)
13327 /* Computed goto's must be tsubst'd into. On the other hand,
13328 non-computed gotos must not be; the identifier in question
13329 will have no binding. */
13332 tmp
= DECL_NAME (tmp
);
13333 finish_goto_stmt (tmp
);
13337 tmp
= finish_asm_stmt
13338 (ASM_VOLATILE_P (t
),
13339 RECUR (ASM_STRING (t
)),
13340 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13341 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13342 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13343 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13345 tree asm_expr
= tmp
;
13346 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13347 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13348 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13355 stmt
= begin_try_block ();
13356 RECUR (TRY_STMTS (t
));
13357 finish_cleanup_try_block (stmt
);
13358 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13362 tree compound_stmt
= NULL_TREE
;
13364 if (FN_TRY_BLOCK_P (t
))
13365 stmt
= begin_function_try_block (&compound_stmt
);
13367 stmt
= begin_try_block ();
13369 RECUR (TRY_STMTS (t
));
13371 if (FN_TRY_BLOCK_P (t
))
13372 finish_function_try_block (stmt
);
13374 finish_try_block (stmt
);
13376 RECUR (TRY_HANDLERS (t
));
13377 if (FN_TRY_BLOCK_P (t
))
13378 finish_function_handler_sequence (stmt
, compound_stmt
);
13380 finish_handler_sequence (stmt
);
13386 tree decl
= HANDLER_PARMS (t
);
13390 decl
= tsubst (decl
, args
, complain
, in_decl
);
13391 /* Prevent instantiate_decl from trying to instantiate
13392 this variable. We've already done all that needs to be
13394 if (decl
!= error_mark_node
)
13395 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13397 stmt
= begin_handler ();
13398 finish_handler_parms (decl
, stmt
);
13399 RECUR (HANDLER_BODY (t
));
13400 finish_handler (stmt
);
13405 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13406 if (CLASS_TYPE_P (tmp
))
13408 /* Local classes are not independent templates; they are
13409 instantiated along with their containing function. And this
13410 way we don't have to deal with pushing out of one local class
13411 to instantiate a member of another local class. */
13413 /* Closures are handled by the LAMBDA_EXPR. */
13414 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
13415 complete_type (tmp
);
13416 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
13417 if (!DECL_ARTIFICIAL (fn
))
13418 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
13422 case STATIC_ASSERT
:
13426 ++c_inhibit_evaluation_warnings
;
13428 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13431 /*integral_constant_expression_p=*/true);
13432 --c_inhibit_evaluation_warnings
;
13434 finish_static_assert (condition
,
13435 STATIC_ASSERT_MESSAGE (t
),
13436 STATIC_ASSERT_SOURCE_LOCATION (t
),
13437 /*member_p=*/false);
13442 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13443 args
, complain
, in_decl
);
13444 stmt
= begin_omp_parallel ();
13445 RECUR (OMP_PARALLEL_BODY (t
));
13446 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13447 = OMP_PARALLEL_COMBINED (t
);
13451 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13452 args
, complain
, in_decl
);
13453 stmt
= begin_omp_task ();
13454 RECUR (OMP_TASK_BODY (t
));
13455 finish_omp_task (tmp
, stmt
);
13460 tree clauses
, body
, pre_body
;
13461 tree declv
, initv
, condv
, incrv
;
13464 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13465 args
, complain
, in_decl
);
13466 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13467 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13468 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13469 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13471 stmt
= begin_omp_structured_block ();
13473 pre_body
= push_stmt_list ();
13474 RECUR (OMP_FOR_PRE_BODY (t
));
13475 pre_body
= pop_stmt_list (pre_body
);
13477 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13478 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13479 &clauses
, args
, complain
, in_decl
,
13480 integral_constant_expression_p
);
13482 body
= push_stmt_list ();
13483 RECUR (OMP_FOR_BODY (t
));
13484 body
= pop_stmt_list (body
);
13486 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13487 body
, pre_body
, clauses
);
13489 add_stmt (finish_omp_structured_block (stmt
));
13495 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13496 stmt
= push_stmt_list ();
13497 RECUR (OMP_BODY (t
));
13498 stmt
= pop_stmt_list (stmt
);
13501 OMP_BODY (t
) = stmt
;
13502 OMP_CLAUSES (t
) = tmp
;
13510 stmt
= push_stmt_list ();
13511 RECUR (OMP_BODY (t
));
13512 stmt
= pop_stmt_list (stmt
);
13515 OMP_BODY (t
) = stmt
;
13520 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13521 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13523 tree op1
= TREE_OPERAND (t
, 1);
13524 tree rhs1
= NULL_TREE
;
13526 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13528 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13529 op1
= TREE_OPERAND (op1
, 1);
13531 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13532 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13533 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13534 NULL_TREE
, NULL_TREE
, rhs1
);
13538 tree op1
= TREE_OPERAND (t
, 1);
13539 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13540 tree rhs1
= NULL_TREE
;
13541 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13542 enum tree_code opcode
= NOP_EXPR
;
13543 if (code
== OMP_ATOMIC_READ
)
13545 v
= RECUR (TREE_OPERAND (op1
, 0));
13546 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13548 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13549 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13551 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13552 v
= RECUR (TREE_OPERAND (op1
, 0));
13553 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13554 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13556 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13557 op11
= TREE_OPERAND (op11
, 1);
13559 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13560 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13561 opcode
= TREE_CODE (op11
);
13566 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13567 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13569 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13573 case TRANSACTION_EXPR
:
13576 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13577 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13579 if (TRANSACTION_EXPR_IS_STMT (t
))
13581 tree body
= TRANSACTION_EXPR_BODY (t
);
13582 tree noex
= NULL_TREE
;
13583 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13585 noex
= MUST_NOT_THROW_COND (body
);
13586 if (noex
== NULL_TREE
)
13587 noex
= boolean_true_node
;
13588 body
= TREE_OPERAND (body
, 0);
13590 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13592 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13596 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13597 RECUR (TRANSACTION_EXPR_BODY (t
)),
13604 case MUST_NOT_THROW_EXPR
:
13605 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13606 RECUR (MUST_NOT_THROW_COND (t
))));
13608 case EXPR_PACK_EXPANSION
:
13609 error ("invalid use of pack expansion expression");
13610 RETURN (error_mark_node
);
13612 case NONTYPE_ARGUMENT_PACK
:
13613 error ("use %<...%> to expand argument pack");
13614 RETURN (error_mark_node
);
13616 case COMPOUND_EXPR
:
13617 tmp
= RECUR (TREE_OPERAND (t
, 0));
13618 if (tmp
== NULL_TREE
)
13619 /* If the first operand was a statement, we're done with it. */
13620 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13621 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13622 RECUR (TREE_OPERAND (t
, 1)),
13626 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13628 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13629 /*function_p=*/false,
13630 integral_constant_expression_p
));
13633 RETURN (NULL_TREE
);
13635 input_location
= loc
;
13641 /* T is a postfix-expression that is not being used in a function
13642 call. Return the substituted version of T. */
13645 tsubst_non_call_postfix_expression (tree t
, tree args
,
13646 tsubst_flags_t complain
,
13649 if (TREE_CODE (t
) == SCOPE_REF
)
13650 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13651 /*done=*/false, /*address_p=*/false);
13653 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13654 /*function_p=*/false,
13655 /*integral_constant_expression_p=*/false);
13660 /* Like tsubst but deals with expressions and performs semantic
13661 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13664 tsubst_copy_and_build (tree t
,
13666 tsubst_flags_t complain
,
13669 bool integral_constant_expression_p
)
13671 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13672 #define RECUR(NODE) \
13673 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13674 /*function_p=*/false, \
13675 integral_constant_expression_p)
13680 if (t
== NULL_TREE
|| t
== error_mark_node
)
13683 loc
= input_location
;
13684 if (EXPR_HAS_LOCATION (t
))
13685 input_location
= EXPR_LOCATION (t
);
13687 /* N3276 decltype magic only applies to calls at the top level or on the
13688 right side of a comma. */
13689 tsubst_flags_t decltype_flag
= (complain
& tf_decltype
);
13690 complain
&= ~tf_decltype
;
13692 switch (TREE_CODE (t
))
13696 /* Fall through. */
13697 case IDENTIFIER_NODE
:
13701 bool non_integral_constant_expression_p
;
13702 const char *error_msg
;
13704 if (IDENTIFIER_TYPENAME_P (t
))
13706 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13707 t
= mangle_conv_op_name_for_type (new_type
);
13710 /* Look up the name. */
13711 decl
= lookup_name (t
);
13713 /* By convention, expressions use ERROR_MARK_NODE to indicate
13714 failure, not NULL_TREE. */
13715 if (decl
== NULL_TREE
)
13716 decl
= error_mark_node
;
13718 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13720 integral_constant_expression_p
,
13721 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx11
),
13722 &non_integral_constant_expression_p
,
13723 /*template_p=*/false,
13725 /*address_p=*/false,
13726 /*template_arg_p=*/false,
13731 if (!function_p
&& identifier_p (decl
))
13733 if (complain
& tf_error
)
13734 unqualified_name_lookup_error (decl
);
13735 decl
= error_mark_node
;
13740 case TEMPLATE_ID_EXPR
:
13743 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13744 tree targs
= TREE_OPERAND (t
, 1);
13747 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13749 if (TREE_CODE (templ
) == COMPONENT_REF
)
13751 object
= TREE_OPERAND (templ
, 0);
13752 templ
= TREE_OPERAND (templ
, 1);
13755 object
= NULL_TREE
;
13756 templ
= lookup_template_function (templ
, targs
);
13759 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13760 object
, templ
, NULL_TREE
));
13762 RETURN (baselink_for_fns (templ
));
13767 tree r
= RECUR (TREE_OPERAND (t
, 0));
13769 if (REFERENCE_REF_P (t
))
13771 /* A type conversion to reference type will be enclosed in
13772 such an indirect ref, but the substitution of the cast
13773 will have also added such an indirect ref. */
13774 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13775 r
= convert_from_reference (r
);
13778 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
,
13779 complain
|decltype_flag
);
13785 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13786 RECUR (TREE_OPERAND (t
, 0))));
13788 case IMPLICIT_CONV_EXPR
:
13790 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13791 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13792 int flags
= LOOKUP_IMPLICIT
;
13793 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13794 flags
= LOOKUP_NORMAL
;
13795 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13802 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13803 RECUR (TREE_OPERAND (t
, 0))));
13806 case REINTERPRET_CAST_EXPR
:
13807 case CONST_CAST_EXPR
:
13808 case DYNAMIC_CAST_EXPR
:
13809 case STATIC_CAST_EXPR
:
13812 tree op
, r
= NULL_TREE
;
13814 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13815 if (integral_constant_expression_p
13816 && !cast_valid_in_integral_constant_expression_p (type
))
13818 if (complain
& tf_error
)
13819 error ("a cast to a type other than an integral or "
13820 "enumeration type cannot appear in a constant-expression");
13821 RETURN (error_mark_node
);
13824 op
= RECUR (TREE_OPERAND (t
, 0));
13826 ++c_inhibit_evaluation_warnings
;
13827 switch (TREE_CODE (t
))
13830 r
= build_functional_cast (type
, op
, complain
);
13832 case REINTERPRET_CAST_EXPR
:
13833 r
= build_reinterpret_cast (type
, op
, complain
);
13835 case CONST_CAST_EXPR
:
13836 r
= build_const_cast (type
, op
, complain
);
13838 case DYNAMIC_CAST_EXPR
:
13839 r
= build_dynamic_cast (type
, op
, complain
);
13841 case STATIC_CAST_EXPR
:
13842 r
= build_static_cast (type
, op
, complain
);
13845 gcc_unreachable ();
13847 --c_inhibit_evaluation_warnings
;
13852 case POSTDECREMENT_EXPR
:
13853 case POSTINCREMENT_EXPR
:
13854 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13855 args
, complain
, in_decl
);
13856 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
,
13857 complain
|decltype_flag
));
13859 case PREDECREMENT_EXPR
:
13860 case PREINCREMENT_EXPR
:
13864 case TRUTH_NOT_EXPR
:
13865 case UNARY_PLUS_EXPR
: /* Unary + */
13866 case REALPART_EXPR
:
13867 case IMAGPART_EXPR
:
13868 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13869 RECUR (TREE_OPERAND (t
, 0)),
13870 complain
|decltype_flag
));
13872 case FIX_TRUNC_EXPR
:
13873 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13877 op1
= TREE_OPERAND (t
, 0);
13878 if (TREE_CODE (op1
) == LABEL_DECL
)
13879 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13880 EXPR_LOCATION (op1
)));
13881 if (TREE_CODE (op1
) == SCOPE_REF
)
13882 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13883 /*done=*/true, /*address_p=*/true);
13885 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13887 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
,
13888 complain
|decltype_flag
));
13893 case TRUNC_DIV_EXPR
:
13894 case CEIL_DIV_EXPR
:
13895 case FLOOR_DIV_EXPR
:
13896 case ROUND_DIV_EXPR
:
13897 case EXACT_DIV_EXPR
:
13901 case TRUNC_MOD_EXPR
:
13902 case FLOOR_MOD_EXPR
:
13903 case TRUTH_ANDIF_EXPR
:
13904 case TRUTH_ORIF_EXPR
:
13905 case TRUTH_AND_EXPR
:
13906 case TRUTH_OR_EXPR
:
13924 ++c_inhibit_evaluation_warnings
;
13926 r
= build_x_binary_op
13927 (input_location
, TREE_CODE (t
),
13928 RECUR (TREE_OPERAND (t
, 0)),
13929 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13931 : TREE_CODE (TREE_OPERAND (t
, 0))),
13932 RECUR (TREE_OPERAND (t
, 1)),
13933 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13935 : TREE_CODE (TREE_OPERAND (t
, 1))),
13937 complain
|decltype_flag
);
13938 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13939 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13941 --c_inhibit_evaluation_warnings
;
13947 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13948 /*address_p=*/false));
13950 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13951 args
, complain
, in_decl
);
13952 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13953 RECUR (TREE_OPERAND (t
, 1)),
13954 complain
|decltype_flag
));
13956 case ARRAY_NOTATION_REF
:
13958 tree start_index
, length
, stride
;
13959 op1
= tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t
),
13960 args
, complain
, in_decl
);
13961 start_index
= RECUR (ARRAY_NOTATION_START (t
));
13962 length
= RECUR (ARRAY_NOTATION_LENGTH (t
));
13963 stride
= RECUR (ARRAY_NOTATION_STRIDE (t
));
13964 RETURN (build_array_notation_ref (EXPR_LOCATION (t
), op1
, start_index
,
13965 length
, stride
, TREE_TYPE (op1
)));
13968 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13969 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13976 op1
= TREE_OPERAND (t
, 0);
13977 if (TREE_CODE (t
) == SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (t
))
13978 op1
= TREE_TYPE (op1
);
13981 /* When there are no ARGS, we are trying to evaluate a
13982 non-dependent expression from the parser. Trying to do
13983 the substitutions may not work. */
13985 op1
= TREE_TYPE (op1
);
13989 ++cp_unevaluated_operand
;
13990 ++c_inhibit_evaluation_warnings
;
13992 op1
= tsubst (op1
, args
, complain
, in_decl
);
13994 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13995 /*function_p=*/false,
13996 /*integral_constant_expression_p=*/
13998 --cp_unevaluated_operand
;
13999 --c_inhibit_evaluation_warnings
;
14002 r
= cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
14003 complain
& tf_error
);
14005 r
= cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
14006 complain
& tf_error
);
14007 if (TREE_CODE (t
) == SIZEOF_EXPR
&& r
!= error_mark_node
)
14009 if (TREE_CODE (r
) != SIZEOF_EXPR
|| TYPE_P (op1
))
14011 if (!processing_template_decl
&& TYPE_P (op1
))
14013 r
= build_min (SIZEOF_EXPR
, size_type_node
,
14014 build1 (NOP_EXPR
, op1
, error_mark_node
));
14015 SIZEOF_EXPR_TYPE_P (r
) = 1;
14018 r
= build_min (SIZEOF_EXPR
, size_type_node
, op1
);
14019 TREE_SIDE_EFFECTS (r
) = 0;
14020 TREE_READONLY (r
) = 1;
14022 SET_EXPR_LOCATION (r
, EXPR_LOCATION (t
));
14027 case AT_ENCODE_EXPR
:
14029 op1
= TREE_OPERAND (t
, 0);
14030 ++cp_unevaluated_operand
;
14031 ++c_inhibit_evaluation_warnings
;
14032 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14033 /*function_p=*/false,
14034 /*integral_constant_expression_p=*/false);
14035 --cp_unevaluated_operand
;
14036 --c_inhibit_evaluation_warnings
;
14037 RETURN (objc_build_encode_expr (op1
));
14040 case NOEXCEPT_EXPR
:
14041 op1
= TREE_OPERAND (t
, 0);
14042 ++cp_unevaluated_operand
;
14043 ++c_inhibit_evaluation_warnings
;
14044 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
14045 /*function_p=*/false,
14046 /*integral_constant_expression_p=*/false);
14047 --cp_unevaluated_operand
;
14048 --c_inhibit_evaluation_warnings
;
14049 RETURN (finish_noexcept_expr (op1
, complain
));
14055 ++c_inhibit_evaluation_warnings
;
14057 r
= build_x_modify_expr
14058 (EXPR_LOCATION (t
),
14059 RECUR (TREE_OPERAND (t
, 0)),
14060 TREE_CODE (TREE_OPERAND (t
, 1)),
14061 RECUR (TREE_OPERAND (t
, 2)),
14062 complain
|decltype_flag
);
14063 /* TREE_NO_WARNING must be set if either the expression was
14064 parenthesized or it uses an operator such as >>= rather
14065 than plain assignment. In the former case, it was already
14066 set and must be copied. In the latter case,
14067 build_x_modify_expr sets it and it must not be reset
14069 if (TREE_NO_WARNING (t
))
14070 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
14072 --c_inhibit_evaluation_warnings
;
14078 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14079 args
, complain
, in_decl
);
14080 /* Remember that there was a reference to this entity. */
14083 RETURN (build_x_arrow (input_location
, op1
, complain
));
14087 tree placement
= RECUR (TREE_OPERAND (t
, 0));
14088 tree init
= RECUR (TREE_OPERAND (t
, 3));
14089 vec
<tree
, va_gc
> *placement_vec
;
14090 vec
<tree
, va_gc
> *init_vec
;
14093 if (placement
== NULL_TREE
)
14094 placement_vec
= NULL
;
14097 placement_vec
= make_tree_vector ();
14098 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
14099 vec_safe_push (placement_vec
, TREE_VALUE (placement
));
14102 /* If there was an initializer in the original tree, but it
14103 instantiated to an empty list, then we should pass a
14104 non-NULL empty vector to tell build_new that it was an
14105 empty initializer() rather than no initializer. This can
14106 only happen when the initializer is a pack expansion whose
14107 parameter packs are of length zero. */
14108 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
14112 init_vec
= make_tree_vector ();
14113 if (init
== void_zero_node
)
14114 gcc_assert (init_vec
!= NULL
);
14117 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
14118 vec_safe_push (init_vec
, TREE_VALUE (init
));
14122 ret
= build_new (&placement_vec
,
14123 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
14124 RECUR (TREE_OPERAND (t
, 2)),
14126 NEW_EXPR_USE_GLOBAL (t
),
14129 if (placement_vec
!= NULL
)
14130 release_tree_vector (placement_vec
);
14131 if (init_vec
!= NULL
)
14132 release_tree_vector (init_vec
);
14138 RETURN (delete_sanity
14139 (RECUR (TREE_OPERAND (t
, 0)),
14140 RECUR (TREE_OPERAND (t
, 1)),
14141 DELETE_EXPR_USE_VEC (t
),
14142 DELETE_EXPR_USE_GLOBAL (t
),
14145 case COMPOUND_EXPR
:
14147 tree op0
= tsubst_copy_and_build (TREE_OPERAND (t
, 0), args
,
14148 complain
& ~tf_decltype
, in_decl
,
14149 /*function_p=*/false,
14150 integral_constant_expression_p
);
14151 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
14153 RECUR (TREE_OPERAND (t
, 1)),
14154 complain
|decltype_flag
));
14160 vec
<tree
, va_gc
> *call_args
;
14161 unsigned int nargs
, i
;
14166 function
= CALL_EXPR_FN (t
);
14167 /* When we parsed the expression, we determined whether or
14168 not Koenig lookup should be performed. */
14169 koenig_p
= KOENIG_LOOKUP_P (t
);
14170 if (TREE_CODE (function
) == SCOPE_REF
)
14172 qualified_p
= true;
14173 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
14175 /*address_p=*/false);
14177 else if (koenig_p
&& identifier_p (function
))
14179 /* Do nothing; calling tsubst_copy_and_build on an identifier
14180 would incorrectly perform unqualified lookup again.
14182 Note that we can also have an IDENTIFIER_NODE if the earlier
14183 unqualified lookup found a member function; in that case
14184 koenig_p will be false and we do want to do the lookup
14185 again to find the instantiated member function.
14187 FIXME but doing that causes c++/15272, so we need to stop
14188 using IDENTIFIER_NODE in that situation. */
14189 qualified_p
= false;
14193 if (TREE_CODE (function
) == COMPONENT_REF
)
14195 tree op
= TREE_OPERAND (function
, 1);
14197 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
14198 || (BASELINK_P (op
)
14199 && BASELINK_QUALIFIED_P (op
)));
14202 qualified_p
= false;
14204 if (TREE_CODE (function
) == ADDR_EXPR
14205 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
14206 /* Avoid error about taking the address of a constructor. */
14207 function
= TREE_OPERAND (function
, 0);
14209 function
= tsubst_copy_and_build (function
, args
, complain
,
14212 integral_constant_expression_p
);
14214 if (BASELINK_P (function
))
14215 qualified_p
= true;
14218 nargs
= call_expr_nargs (t
);
14219 call_args
= make_tree_vector ();
14220 for (i
= 0; i
< nargs
; ++i
)
14222 tree arg
= CALL_EXPR_ARG (t
, i
);
14224 if (!PACK_EXPANSION_P (arg
))
14225 vec_safe_push (call_args
, RECUR (CALL_EXPR_ARG (t
, i
)));
14228 /* Expand the pack expansion and push each entry onto
14230 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
14231 if (TREE_CODE (arg
) == TREE_VEC
)
14233 unsigned int len
, j
;
14235 len
= TREE_VEC_LENGTH (arg
);
14236 for (j
= 0; j
< len
; ++j
)
14238 tree value
= TREE_VEC_ELT (arg
, j
);
14239 if (value
!= NULL_TREE
)
14240 value
= convert_from_reference (value
);
14241 vec_safe_push (call_args
, value
);
14246 /* A partial substitution. Add one entry. */
14247 vec_safe_push (call_args
, arg
);
14252 /* We do not perform argument-dependent lookup if normal
14253 lookup finds a non-function, in accordance with the
14254 expected resolution of DR 218. */
14256 && ((is_overloaded_fn (function
)
14257 /* If lookup found a member function, the Koenig lookup is
14258 not appropriate, even if an unqualified-name was used
14259 to denote the function. */
14260 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
14261 || identifier_p (function
))
14262 /* Only do this when substitution turns a dependent call
14263 into a non-dependent call. */
14264 && type_dependent_expression_p_push (t
)
14265 && !any_type_dependent_arguments_p (call_args
))
14266 function
= perform_koenig_lookup (function
, call_args
, false,
14269 if (identifier_p (function
)
14270 && !any_type_dependent_arguments_p (call_args
))
14272 if (koenig_p
&& (complain
& tf_warning_or_error
))
14274 /* For backwards compatibility and good diagnostics, try
14275 the unqualified lookup again if we aren't in SFINAE
14277 tree unq
= (tsubst_copy_and_build
14278 (function
, args
, complain
, in_decl
, true,
14279 integral_constant_expression_p
));
14280 if (unq
== error_mark_node
)
14281 RETURN (error_mark_node
);
14283 if (unq
!= function
)
14286 if (INDIRECT_REF_P (fn
))
14287 fn
= TREE_OPERAND (fn
, 0);
14288 if (TREE_CODE (fn
) == COMPONENT_REF
)
14289 fn
= TREE_OPERAND (fn
, 1);
14290 if (is_overloaded_fn (fn
))
14291 fn
= get_first_fn (fn
);
14292 if (permerror (EXPR_LOC_OR_HERE (t
),
14293 "%qD was not declared in this scope, "
14294 "and no declarations were found by "
14295 "argument-dependent lookup at the point "
14296 "of instantiation", function
))
14299 /* Can't say anything more. */;
14300 else if (DECL_CLASS_SCOPE_P (fn
))
14302 inform (EXPR_LOC_OR_HERE (t
),
14303 "declarations in dependent base %qT are "
14304 "not found by unqualified lookup",
14305 DECL_CLASS_CONTEXT (fn
));
14306 if (current_class_ptr
)
14307 inform (EXPR_LOC_OR_HERE (t
),
14308 "use %<this->%D%> instead", function
);
14310 inform (EXPR_LOC_OR_HERE (t
),
14311 "use %<%T::%D%> instead",
14312 current_class_name
, function
);
14315 inform (0, "%q+D declared here, later in the "
14316 "translation unit", fn
);
14321 if (identifier_p (function
))
14323 if (complain
& tf_error
)
14324 unqualified_name_lookup_error (function
);
14325 release_tree_vector (call_args
);
14326 RETURN (error_mark_node
);
14330 /* Remember that there was a reference to this entity. */
14331 if (DECL_P (function
))
14332 mark_used (function
);
14334 /* Put back tf_decltype for the actual call. */
14335 complain
|= decltype_flag
;
14337 if (TREE_CODE (function
) == OFFSET_REF
)
14338 ret
= build_offset_ref_call_from_tree (function
, &call_args
,
14340 else if (TREE_CODE (function
) == COMPONENT_REF
)
14342 tree instance
= TREE_OPERAND (function
, 0);
14343 tree fn
= TREE_OPERAND (function
, 1);
14345 if (processing_template_decl
14346 && (type_dependent_expression_p (instance
)
14347 || (!BASELINK_P (fn
)
14348 && TREE_CODE (fn
) != FIELD_DECL
)
14349 || type_dependent_expression_p (fn
)
14350 || any_type_dependent_arguments_p (call_args
)))
14351 ret
= build_nt_call_vec (function
, call_args
);
14352 else if (!BASELINK_P (fn
))
14353 ret
= finish_call_expr (function
, &call_args
,
14354 /*disallow_virtual=*/false,
14355 /*koenig_p=*/false,
14358 ret
= (build_new_method_call
14360 &call_args
, NULL_TREE
,
14361 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
14366 ret
= finish_call_expr (function
, &call_args
,
14367 /*disallow_virtual=*/qualified_p
,
14371 release_tree_vector (call_args
);
14378 tree cond
= RECUR (TREE_OPERAND (t
, 0));
14381 if (TREE_CODE (cond
) == INTEGER_CST
)
14383 if (integer_zerop (cond
))
14385 ++c_inhibit_evaluation_warnings
;
14386 exp1
= RECUR (TREE_OPERAND (t
, 1));
14387 --c_inhibit_evaluation_warnings
;
14388 exp2
= RECUR (TREE_OPERAND (t
, 2));
14392 exp1
= RECUR (TREE_OPERAND (t
, 1));
14393 ++c_inhibit_evaluation_warnings
;
14394 exp2
= RECUR (TREE_OPERAND (t
, 2));
14395 --c_inhibit_evaluation_warnings
;
14400 exp1
= RECUR (TREE_OPERAND (t
, 1));
14401 exp2
= RECUR (TREE_OPERAND (t
, 2));
14404 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
14405 cond
, exp1
, exp2
, complain
));
14408 case PSEUDO_DTOR_EXPR
:
14409 RETURN (finish_pseudo_destructor_expr
14410 (RECUR (TREE_OPERAND (t
, 0)),
14411 RECUR (TREE_OPERAND (t
, 1)),
14412 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
14417 tree purpose
, value
, chain
;
14419 if (t
== void_list_node
)
14422 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14423 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14425 /* We have pack expansions, so expand those and
14426 create a new list out of it. */
14427 tree purposevec
= NULL_TREE
;
14428 tree valuevec
= NULL_TREE
;
14432 /* Expand the argument expressions. */
14433 if (TREE_PURPOSE (t
))
14434 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14435 complain
, in_decl
);
14436 if (TREE_VALUE (t
))
14437 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14438 complain
, in_decl
);
14440 /* Build the rest of the list. */
14441 chain
= TREE_CHAIN (t
);
14442 if (chain
&& chain
!= void_type_node
)
14443 chain
= RECUR (chain
);
14445 /* Determine the number of arguments. */
14446 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14448 len
= TREE_VEC_LENGTH (purposevec
);
14449 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14451 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14452 len
= TREE_VEC_LENGTH (valuevec
);
14455 /* Since we only performed a partial substitution into
14456 the argument pack, we only RETURN (a single list
14458 if (purposevec
== TREE_PURPOSE (t
)
14459 && valuevec
== TREE_VALUE (t
)
14460 && chain
== TREE_CHAIN (t
))
14463 RETURN (tree_cons (purposevec
, valuevec
, chain
));
14466 /* Convert the argument vectors into a TREE_LIST */
14470 /* Grab the Ith values. */
14472 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14475 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14478 /* Build the list (backwards). */
14479 chain
= tree_cons (purpose
, value
, chain
);
14485 purpose
= TREE_PURPOSE (t
);
14487 purpose
= RECUR (purpose
);
14488 value
= TREE_VALUE (t
);
14490 value
= RECUR (value
);
14491 chain
= TREE_CHAIN (t
);
14492 if (chain
&& chain
!= void_type_node
)
14493 chain
= RECUR (chain
);
14494 if (purpose
== TREE_PURPOSE (t
)
14495 && value
== TREE_VALUE (t
)
14496 && chain
== TREE_CHAIN (t
))
14498 RETURN (tree_cons (purpose
, value
, chain
));
14501 case COMPONENT_REF
:
14507 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14508 args
, complain
, in_decl
);
14509 /* Remember that there was a reference to this entity. */
14510 if (DECL_P (object
))
14511 mark_used (object
);
14512 object_type
= TREE_TYPE (object
);
14514 member
= TREE_OPERAND (t
, 1);
14515 if (BASELINK_P (member
))
14516 member
= tsubst_baselink (member
,
14517 non_reference (TREE_TYPE (object
)),
14518 args
, complain
, in_decl
);
14520 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14521 if (member
== error_mark_node
)
14522 RETURN (error_mark_node
);
14524 if (type_dependent_expression_p (object
))
14525 /* We can't do much here. */;
14526 else if (!CLASS_TYPE_P (object_type
))
14528 if (scalarish_type_p (object_type
))
14530 tree s
= NULL_TREE
;
14531 tree dtor
= member
;
14533 if (TREE_CODE (dtor
) == SCOPE_REF
)
14535 s
= TREE_OPERAND (dtor
, 0);
14536 dtor
= TREE_OPERAND (dtor
, 1);
14538 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14540 dtor
= TREE_OPERAND (dtor
, 0);
14542 RETURN (finish_pseudo_destructor_expr
14543 (object
, s
, dtor
, input_location
));
14547 else if (TREE_CODE (member
) == SCOPE_REF
14548 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14550 /* Lookup the template functions now that we know what the
14552 tree scope
= TREE_OPERAND (member
, 0);
14553 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14554 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14555 member
= lookup_qualified_name (scope
, tmpl
,
14556 /*is_type_p=*/false,
14557 /*complain=*/false);
14558 if (BASELINK_P (member
))
14560 BASELINK_FUNCTIONS (member
)
14561 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14563 member
= (adjust_result_of_qualified_name_lookup
14564 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14569 qualified_name_lookup_error (scope
, tmpl
, member
,
14571 RETURN (error_mark_node
);
14574 else if (TREE_CODE (member
) == SCOPE_REF
14575 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14576 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14578 if (complain
& tf_error
)
14580 if (TYPE_P (TREE_OPERAND (member
, 0)))
14581 error ("%qT is not a class or namespace",
14582 TREE_OPERAND (member
, 0));
14584 error ("%qD is not a class or namespace",
14585 TREE_OPERAND (member
, 0));
14587 RETURN (error_mark_node
);
14589 else if (TREE_CODE (member
) == FIELD_DECL
)
14590 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14592 RETURN (finish_class_member_access_expr (object
, member
,
14593 /*template_p=*/false,
14598 RETURN (build_throw
14599 (RECUR (TREE_OPERAND (t
, 0))));
14603 vec
<constructor_elt
, va_gc
> *n
;
14604 constructor_elt
*ce
;
14605 unsigned HOST_WIDE_INT idx
;
14606 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14607 bool process_index_p
;
14609 bool need_copy_p
= false;
14612 if (type
== error_mark_node
)
14613 RETURN (error_mark_node
);
14615 /* digest_init will do the wrong thing if we let it. */
14616 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14619 /* We do not want to process the index of aggregate
14620 initializers as they are identifier nodes which will be
14621 looked up by digest_init. */
14622 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14624 n
= vec_safe_copy (CONSTRUCTOR_ELTS (t
));
14625 newlen
= vec_safe_length (n
);
14626 FOR_EACH_VEC_SAFE_ELT (n
, idx
, ce
)
14628 if (ce
->index
&& process_index_p
14629 /* An identifier index is looked up in the type
14630 being initialized, not the current scope. */
14631 && TREE_CODE (ce
->index
) != IDENTIFIER_NODE
)
14632 ce
->index
= RECUR (ce
->index
);
14634 if (PACK_EXPANSION_P (ce
->value
))
14636 /* Substitute into the pack expansion. */
14637 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14640 if (ce
->value
== error_mark_node
14641 || PACK_EXPANSION_P (ce
->value
))
14643 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14644 /* Just move the argument into place. */
14645 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14648 /* Update the length of the final CONSTRUCTOR
14649 arguments vector, and note that we will need to
14651 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14652 need_copy_p
= true;
14656 ce
->value
= RECUR (ce
->value
);
14661 vec
<constructor_elt
, va_gc
> *old_n
= n
;
14663 vec_alloc (n
, newlen
);
14664 FOR_EACH_VEC_ELT (*old_n
, idx
, ce
)
14666 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14668 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14669 for (i
= 0; i
< len
; ++i
)
14670 CONSTRUCTOR_APPEND_ELT (n
, 0,
14671 TREE_VEC_ELT (ce
->value
, i
));
14674 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14678 r
= build_constructor (init_list_type_node
, n
);
14679 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14681 if (TREE_HAS_CONSTRUCTOR (t
))
14682 RETURN (finish_compound_literal (type
, r
, complain
));
14684 TREE_TYPE (r
) = type
;
14690 tree operand_0
= TREE_OPERAND (t
, 0);
14691 if (TYPE_P (operand_0
))
14693 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14694 RETURN (get_typeid (operand_0
, complain
));
14698 operand_0
= RECUR (operand_0
);
14699 RETURN (build_typeid (operand_0
, complain
));
14706 else if (DECL_PACK_P (t
))
14708 /* We don't build decls for an instantiation of a
14709 variadic capture proxy, we instantiate the elements
14711 gcc_assert (DECL_HAS_VALUE_EXPR_P (t
));
14712 return RECUR (DECL_VALUE_EXPR (t
));
14718 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14720 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14721 /* If the original type was a reference, we'll be wrapped in
14722 the appropriate INDIRECT_REF. */
14723 r
= convert_from_reference (r
);
14728 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14729 RECUR (TREE_OPERAND (t
, 0)),
14730 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14732 case OFFSETOF_EXPR
:
14733 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14737 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14738 complain
, in_decl
);
14740 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14742 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14744 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14749 tree old_stmt_expr
= cur_stmt_expr
;
14750 tree stmt_expr
= begin_stmt_expr ();
14752 cur_stmt_expr
= stmt_expr
;
14753 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14754 integral_constant_expression_p
);
14755 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14756 cur_stmt_expr
= old_stmt_expr
;
14758 /* If the resulting list of expression statement is empty,
14759 fold it further into void_zero_node. */
14760 if (empty_expr_stmt_p (stmt_expr
))
14761 stmt_expr
= void_zero_node
;
14763 RETURN (stmt_expr
);
14768 tree r
= build_lambda_expr ();
14770 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14771 LAMBDA_EXPR_CLOSURE (r
) = type
;
14772 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14774 LAMBDA_EXPR_LOCATION (r
)
14775 = LAMBDA_EXPR_LOCATION (t
);
14776 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14777 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14778 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14779 LAMBDA_EXPR_DISCRIMINATOR (r
)
14780 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14781 /* For a function scope, we want to use tsubst so that we don't
14782 complain about referring to an auto function before its return
14783 type has been deduced. Otherwise, we want to use tsubst_copy so
14784 that we look up the existing field/parameter/variable rather
14785 than build a new one. */
14786 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14787 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14788 scope
= tsubst (scope
, args
, complain
, in_decl
);
14789 else if (scope
&& TREE_CODE (scope
) == PARM_DECL
)
14791 /* Look up the parameter we want directly, as tsubst_copy
14792 doesn't do what we need. */
14793 tree fn
= tsubst (DECL_CONTEXT (scope
), args
, complain
, in_decl
);
14794 tree parm
= FUNCTION_FIRST_USER_PARM (fn
);
14795 while (DECL_PARM_INDEX (parm
) != DECL_PARM_INDEX (scope
))
14796 parm
= DECL_CHAIN (parm
);
14798 /* FIXME Work around the parm not having DECL_CONTEXT set. */
14799 if (DECL_CONTEXT (scope
) == NULL_TREE
)
14800 DECL_CONTEXT (scope
) = fn
;
14803 scope
= RECUR (scope
);
14804 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14805 LAMBDA_EXPR_RETURN_TYPE (r
)
14806 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14808 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14809 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14811 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14812 determine_visibility (TYPE_NAME (type
));
14813 /* Now that we know visibility, instantiate the type so we have a
14814 declaration of the op() for later calls to lambda_function. */
14815 complete_type (type
);
14817 LAMBDA_EXPR_THIS_CAPTURE (r
) = NULL_TREE
;
14819 RETURN (build_lambda_object (r
));
14823 /* We can get here for a constant initializer of non-dependent type.
14824 FIXME stop folding in cp_parser_initializer_clause. */
14826 tree r
= get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t
)),
14831 case TRANSACTION_EXPR
:
14832 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14833 integral_constant_expression_p
));
14836 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t
, 0))));
14838 case VEC_PERM_EXPR
:
14839 RETURN (build_x_vec_perm_expr (input_location
,
14840 RECUR (TREE_OPERAND (t
, 0)),
14841 RECUR (TREE_OPERAND (t
, 1)),
14842 RECUR (TREE_OPERAND (t
, 2)),
14846 /* Handle Objective-C++ constructs, if appropriate. */
14849 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14850 in_decl
, /*function_p=*/false);
14854 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14860 input_location
= loc
;
14864 /* Verify that the instantiated ARGS are valid. For type arguments,
14865 make sure that the type's linkage is ok. For non-type arguments,
14866 make sure they are constants if they are integral or enumerations.
14867 Emit an error under control of COMPLAIN, and return TRUE on error. */
14870 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14872 if (dependent_template_arg_p (t
))
14874 if (ARGUMENT_PACK_P (t
))
14876 tree vec
= ARGUMENT_PACK_ARGS (t
);
14877 int len
= TREE_VEC_LENGTH (vec
);
14878 bool result
= false;
14881 for (i
= 0; i
< len
; ++i
)
14882 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14886 else if (TYPE_P (t
))
14888 /* [basic.link]: A name with no linkage (notably, the name
14889 of a class or enumeration declared in a local scope)
14890 shall not be used to declare an entity with linkage.
14891 This implies that names with no linkage cannot be used as
14894 DR 757 relaxes this restriction for C++0x. */
14895 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14896 : no_linkage_check (t
, /*relaxed_p=*/false));
14900 /* DR 488 makes use of a type with no linkage cause
14901 type deduction to fail. */
14902 if (complain
& tf_error
)
14904 if (TYPE_ANONYMOUS_P (nt
))
14905 error ("%qT is/uses anonymous type", t
);
14907 error ("template argument for %qD uses local type %qT",
14912 /* In order to avoid all sorts of complications, we do not
14913 allow variably-modified types as template arguments. */
14914 else if (variably_modified_type_p (t
, NULL_TREE
))
14916 if (complain
& tf_error
)
14917 error ("%qT is a variably modified type", t
);
14921 /* Class template and alias template arguments should be OK. */
14922 else if (DECL_TYPE_TEMPLATE_P (t
))
14924 /* A non-type argument of integral or enumerated type must be a
14926 else if (TREE_TYPE (t
)
14927 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14928 && !TREE_CONSTANT (t
))
14930 if (complain
& tf_error
)
14931 error ("integral expression %qE is not constant", t
);
14938 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14940 int ix
, len
= DECL_NTPARMS (tmpl
);
14941 bool result
= false;
14943 for (ix
= 0; ix
!= len
; ix
++)
14945 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14948 if (result
&& (complain
& tf_error
))
14949 error (" trying to instantiate %qD", tmpl
);
14953 /* We're out of SFINAE context now, so generate diagnostics for the access
14954 errors we saw earlier when instantiating D from TMPL and ARGS. */
14957 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14959 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14960 tree type
= TREE_TYPE (pattern
);
14961 location_t loc
= input_location
;
14963 push_access_scope (d
);
14964 push_deferring_access_checks (dk_no_deferred
);
14965 input_location
= DECL_SOURCE_LOCATION (pattern
);
14966 tsubst (type
, args
, tf_warning_or_error
, d
);
14967 input_location
= loc
;
14968 pop_deferring_access_checks ();
14969 pop_access_scope (d
);
14972 /* Instantiate the indicated variable, function, or alias template TMPL with
14973 the template arguments in TARG_PTR. */
14976 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14978 tree targ_ptr
= orig_args
;
14982 bool access_ok
= true;
14984 if (tmpl
== error_mark_node
)
14985 return error_mark_node
;
14987 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14989 /* If this function is a clone, handle it specially. */
14990 if (DECL_CLONED_FUNCTION_P (tmpl
))
14995 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14996 DECL_CLONED_FUNCTION. */
14997 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14998 targ_ptr
, complain
);
14999 if (spec
== error_mark_node
)
15000 return error_mark_node
;
15002 /* Look for the clone. */
15003 FOR_EACH_CLONE (clone
, spec
)
15004 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
15006 /* We should always have found the clone by now. */
15007 gcc_unreachable ();
15011 /* Check to see if we already have this specialization. */
15012 gen_tmpl
= most_general_template (tmpl
);
15013 if (tmpl
!= gen_tmpl
)
15014 /* The TMPL is a partial instantiation. To get a full set of
15015 arguments we must add the arguments used to perform the
15016 partial instantiation. */
15017 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
15020 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15021 but it doesn't seem to be on the hot path. */
15022 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
15024 gcc_assert (tmpl
== gen_tmpl
15025 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
15027 || fndecl
== NULL_TREE
);
15029 if (spec
!= NULL_TREE
)
15031 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
15033 if (complain
& tf_error
)
15034 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
15035 return error_mark_node
;
15040 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
15042 return error_mark_node
;
15044 /* We are building a FUNCTION_DECL, during which the access of its
15045 parameters and return types have to be checked. However this
15046 FUNCTION_DECL which is the desired context for access checking
15047 is not built yet. We solve this chicken-and-egg problem by
15048 deferring all checks until we have the FUNCTION_DECL. */
15049 push_deferring_access_checks (dk_deferred
);
15051 /* Instantiation of the function happens in the context of the function
15052 template, not the context of the overload resolution we're doing. */
15053 push_to_top_level ();
15054 /* If there are dependent arguments, e.g. because we're doing partial
15055 ordering, make sure processing_template_decl stays set. */
15056 if (uses_template_parms (targ_ptr
))
15057 ++processing_template_decl
;
15058 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15060 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
15061 complain
, gen_tmpl
);
15062 push_nested_class (ctx
);
15064 /* Substitute template parameters to obtain the specialization. */
15065 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
15066 targ_ptr
, complain
, gen_tmpl
);
15067 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
15068 pop_nested_class ();
15069 pop_from_top_level ();
15071 if (fndecl
== error_mark_node
)
15073 pop_deferring_access_checks ();
15074 return error_mark_node
;
15077 /* The DECL_TI_TEMPLATE should always be the immediate parent
15078 template, not the most general template. */
15079 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
15081 /* Now we know the specialization, compute access previously
15083 push_access_scope (fndecl
);
15084 if (!perform_deferred_access_checks (complain
))
15086 pop_access_scope (fndecl
);
15087 pop_deferring_access_checks ();
15089 /* If we've just instantiated the main entry point for a function,
15090 instantiate all the alternate entry points as well. We do this
15091 by cloning the instantiation of the main entry point, not by
15092 instantiating the template clones. */
15093 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
15094 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
15098 if (!(complain
& tf_error
))
15100 /* Remember to reinstantiate when we're out of SFINAE so the user
15101 can see the errors. */
15102 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
15104 return error_mark_node
;
15109 /* Wrapper for instantiate_template_1. */
15112 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
15115 timevar_push (TV_TEMPLATE_INST
);
15116 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
15117 timevar_pop (TV_TEMPLATE_INST
);
15121 /* Instantiate the alias template TMPL with ARGS. Also push a template
15122 instantiation level, which instantiate_template doesn't do because
15123 functions and variables have sufficient context established by the
15127 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
15129 struct pending_template
*old_last_pend
= last_pending_template
;
15130 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15131 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
15132 return error_mark_node
;
15133 tree tinst
= build_tree_list (tmpl
, args
);
15134 if (!push_tinst_level (tinst
))
15137 return error_mark_node
;
15141 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl
),
15142 args
, tmpl
, complain
,
15143 /*require_all_args=*/true,
15144 /*use_default_args=*/true);
15146 tree r
= instantiate_template (tmpl
, args
, complain
);
15147 pop_tinst_level ();
15148 /* We can't free this if a pending_template entry or last_error_tinst_level
15149 is pointing at it. */
15150 if (last_pending_template
== old_last_pend
15151 && last_error_tinst_level
== old_error_tinst
)
15157 /* PARM is a template parameter pack for FN. Returns true iff
15158 PARM is used in a deducible way in the argument list of FN. */
15161 pack_deducible_p (tree parm
, tree fn
)
15163 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
15164 for (; t
; t
= TREE_CHAIN (t
))
15166 tree type
= TREE_VALUE (t
);
15168 if (!PACK_EXPANSION_P (type
))
15170 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
15171 packs
; packs
= TREE_CHAIN (packs
))
15172 if (TREE_VALUE (packs
) == parm
)
15174 /* The template parameter pack is used in a function parameter
15175 pack. If this is the end of the parameter list, the
15176 template parameter pack is deducible. */
15177 if (TREE_CHAIN (t
) == void_list_node
)
15180 /* Otherwise, not. Well, it could be deduced from
15181 a non-pack parameter, but doing so would end up with
15182 a deduction mismatch, so don't bother. */
15186 /* The template parameter pack isn't used in any function parameter
15187 packs, but it might be used deeper, e.g. tuple<Args...>. */
15191 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15192 NARGS elements of the arguments that are being used when calling
15193 it. TARGS is a vector into which the deduced template arguments
15196 Return zero for success, 2 for an incomplete match that doesn't resolve
15197 all the types, and 1 for complete failure. An error message will be
15198 printed only for an incomplete match.
15200 If FN is a conversion operator, or we are trying to produce a specific
15201 specialization, RETURN_TYPE is the return type desired.
15203 The EXPLICIT_TARGS are explicit template arguments provided via a
15206 The parameter STRICT is one of:
15209 We are deducing arguments for a function call, as in
15210 [temp.deduct.call].
15213 We are deducing arguments for a conversion function, as in
15214 [temp.deduct.conv].
15217 We are deducing arguments when doing an explicit instantiation
15218 as in [temp.explicit], when determining an explicit specialization
15219 as in [temp.expl.spec], or when taking the address of a function
15220 template, as in [temp.deduct.funcaddr]. */
15223 fn_type_unification (tree fn
,
15224 tree explicit_targs
,
15227 unsigned int nargs
,
15229 unification_kind_t strict
,
15236 tree decl
= NULL_TREE
;
15237 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
15239 static int deduction_depth
;
15240 struct pending_template
*old_last_pend
= last_pending_template
;
15241 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
15242 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
15244 tree r
= error_mark_node
;
15247 complain
|= tf_decltype
;
15249 /* In C++0x, it's possible to have a function template whose type depends
15250 on itself recursively. This is most obvious with decltype, but can also
15251 occur with enumeration scope (c++/48969). So we need to catch infinite
15252 recursion and reject the substitution at deduction time; this function
15253 will return error_mark_node for any repeated substitution.
15255 This also catches excessive recursion such as when f<N> depends on
15256 f<N-1> across all integers, and returns error_mark_node for all the
15257 substitutions back up to the initial one.
15259 This is, of course, not reentrant. */
15260 if (excessive_deduction_depth
)
15261 return error_mark_node
;
15262 tinst
= build_tree_list (fn
, NULL_TREE
);
15265 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
15267 fntype
= TREE_TYPE (fn
);
15268 if (explicit_targs
)
15272 The specified template arguments must match the template
15273 parameters in kind (i.e., type, nontype, template), and there
15274 must not be more arguments than there are parameters;
15275 otherwise type deduction fails.
15277 Nontype arguments must match the types of the corresponding
15278 nontype template parameters, or must be convertible to the
15279 types of the corresponding nontype parameters as specified in
15280 _temp.arg.nontype_, otherwise type deduction fails.
15282 All references in the function type of the function template
15283 to the corresponding template parameters are replaced by the
15284 specified template argument values. If a substitution in a
15285 template parameter or in the function type of the function
15286 template results in an invalid type, type deduction fails. */
15287 int i
, len
= TREE_VEC_LENGTH (tparms
);
15288 location_t loc
= input_location
;
15289 bool incomplete
= false;
15291 /* Adjust any explicit template arguments before entering the
15292 substitution context. */
15294 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
15296 /*require_all_args=*/false,
15297 /*use_default_args=*/false));
15298 if (explicit_targs
== error_mark_node
)
15301 /* Substitute the explicit args into the function type. This is
15302 necessary so that, for instance, explicitly declared function
15303 arguments can match null pointed constants. If we were given
15304 an incomplete set of explicit args, we must not do semantic
15305 processing during substitution as we could create partial
15307 for (i
= 0; i
< len
; i
++)
15309 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15310 bool parameter_pack
= false;
15311 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
15313 /* Dig out the actual parm. */
15314 if (TREE_CODE (parm
) == TYPE_DECL
15315 || TREE_CODE (parm
) == TEMPLATE_DECL
)
15317 parm
= TREE_TYPE (parm
);
15318 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
15320 else if (TREE_CODE (parm
) == PARM_DECL
)
15322 parm
= DECL_INITIAL (parm
);
15323 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
15326 if (!parameter_pack
&& targ
== NULL_TREE
)
15327 /* No explicit argument for this template parameter. */
15330 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
15332 /* Mark the argument pack as "incomplete". We could
15333 still deduce more arguments during unification.
15334 We remove this mark in type_unification_real. */
15337 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
15338 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
15339 = ARGUMENT_PACK_ARGS (targ
);
15342 /* We have some incomplete argument packs. */
15347 TREE_VALUE (tinst
) = explicit_targs
;
15348 if (!push_tinst_level (tinst
))
15350 excessive_deduction_depth
= true;
15353 processing_template_decl
+= incomplete
;
15354 input_location
= DECL_SOURCE_LOCATION (fn
);
15355 /* Ignore any access checks; we'll see them again in
15356 instantiate_template and they might have the wrong
15357 access path at this point. */
15358 push_deferring_access_checks (dk_deferred
);
15359 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
15360 complain
| tf_partial
, NULL_TREE
);
15361 pop_deferring_access_checks ();
15362 input_location
= loc
;
15363 processing_template_decl
-= incomplete
;
15364 pop_tinst_level ();
15366 if (fntype
== error_mark_node
)
15369 /* Place the explicitly specified arguments in TARGS. */
15370 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
15371 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
15374 /* Never do unification on the 'this' parameter. */
15375 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
15381 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
15382 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
15383 new_args
[0] = return_type
;
15384 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
15389 /* We allow incomplete unification without an error message here
15390 because the standard doesn't seem to explicitly prohibit it. Our
15391 callers must be ready to deal with unification failures in any
15394 TREE_VALUE (tinst
) = targs
;
15395 /* If we aren't explaining yet, push tinst context so we can see where
15396 any errors (e.g. from class instantiations triggered by instantiation
15397 of default template arguments) come from. If we are explaining, this
15398 context is redundant. */
15399 if (!explain_p
&& !push_tinst_level (tinst
))
15401 excessive_deduction_depth
= true;
15405 /* type_unification_real will pass back any access checks from default
15406 template argument substitution. */
15407 vec
<deferred_access_check
, va_gc
> *checks
;
15410 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15411 targs
, parms
, args
, nargs
, /*subr=*/0,
15412 strict
, flags
, &checks
, explain_p
);
15414 pop_tinst_level ();
15418 /* Now that we have bindings for all of the template arguments,
15419 ensure that the arguments deduced for the template template
15420 parameters have compatible template parameter lists. We cannot
15421 check this property before we have deduced all template
15422 arguments, because the template parameter types of a template
15423 template parameter might depend on prior template parameters
15424 deduced after the template template parameter. The following
15425 ill-formed example illustrates this issue:
15427 template<typename T, template<T> class C> void f(C<5>, T);
15429 template<int N> struct X {};
15432 f(X<5>(), 5l); // error: template argument deduction fails
15435 The template parameter list of 'C' depends on the template type
15436 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15437 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15438 time that we deduce 'C'. */
15439 if (!template_template_parm_bindings_ok_p
15440 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
15442 unify_inconsistent_template_template_parameters (explain_p
);
15446 /* All is well so far. Now, check:
15450 When all template arguments have been deduced, all uses of
15451 template parameters in nondeduced contexts are replaced with
15452 the corresponding deduced argument values. If the
15453 substitution results in an invalid type, as described above,
15454 type deduction fails. */
15455 TREE_VALUE (tinst
) = targs
;
15456 if (!push_tinst_level (tinst
))
15458 excessive_deduction_depth
= true;
15462 /* Also collect access checks from the instantiation. */
15463 reopen_deferring_access_checks (checks
);
15465 decl
= instantiate_template (fn
, targs
, complain
);
15467 checks
= get_deferred_access_checks ();
15468 pop_deferring_access_checks ();
15470 pop_tinst_level ();
15472 if (decl
== error_mark_node
)
15475 /* Now perform any access checks encountered during substitution. */
15476 push_access_scope (decl
);
15477 ok
= perform_access_checks (checks
, complain
);
15478 pop_access_scope (decl
);
15482 /* If we're looking for an exact match, check that what we got
15483 is indeed an exact match. It might not be if some template
15484 parameters are used in non-deduced contexts. */
15485 if (strict
== DEDUCE_EXACT
)
15487 tree substed
= TREE_TYPE (decl
);
15491 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
15493 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15494 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15495 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15497 unify_type_mismatch (explain_p
, args
[i
],
15498 TREE_VALUE (sarg
));
15507 if (excessive_deduction_depth
)
15509 if (deduction_depth
== 0)
15510 /* Reset once we're all the way out. */
15511 excessive_deduction_depth
= false;
15514 /* We can't free this if a pending_template entry or last_error_tinst_level
15515 is pointing at it. */
15516 if (last_pending_template
== old_last_pend
15517 && last_error_tinst_level
== old_error_tinst
)
15523 /* Adjust types before performing type deduction, as described in
15524 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15525 sections are symmetric. PARM is the type of a function parameter
15526 or the return type of the conversion function. ARG is the type of
15527 the argument passed to the call, or the type of the value
15528 initialized with the result of the conversion function.
15529 ARG_EXPR is the original argument expression, which may be null. */
15532 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15546 /* Swap PARM and ARG throughout the remainder of this
15547 function; the handling is precisely symmetric since PARM
15548 will initialize ARG rather than vice versa. */
15556 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15557 too, but here handle it by stripping the reference from PARM
15558 rather than by adding it to ARG. */
15559 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15560 && TYPE_REF_IS_RVALUE (*parm
)
15561 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15562 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15563 && TREE_CODE (*arg
) == REFERENCE_TYPE
15564 && !TYPE_REF_IS_RVALUE (*arg
))
15565 *parm
= TREE_TYPE (*parm
);
15566 /* Nothing else to do in this case. */
15570 gcc_unreachable ();
15573 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15575 /* [temp.deduct.call]
15577 If P is not a reference type:
15579 --If A is an array type, the pointer type produced by the
15580 array-to-pointer standard conversion (_conv.array_) is
15581 used in place of A for type deduction; otherwise,
15583 --If A is a function type, the pointer type produced by
15584 the function-to-pointer standard conversion
15585 (_conv.func_) is used in place of A for type deduction;
15588 --If A is a cv-qualified type, the top level
15589 cv-qualifiers of A's type are ignored for type
15591 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15592 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15593 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15594 *arg
= build_pointer_type (*arg
);
15596 *arg
= TYPE_MAIN_VARIANT (*arg
);
15599 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15600 of the form T&&, where T is a template parameter, and the argument
15601 is an lvalue, T is deduced as A& */
15602 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15603 && TYPE_REF_IS_RVALUE (*parm
)
15604 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15605 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15606 && (arg_expr
? real_lvalue_p (arg_expr
)
15607 /* try_one_overload doesn't provide an arg_expr, but
15608 functions are always lvalues. */
15609 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15610 *arg
= build_reference_type (*arg
);
15612 /* [temp.deduct.call]
15614 If P is a cv-qualified type, the top level cv-qualifiers
15615 of P's type are ignored for type deduction. If P is a
15616 reference type, the type referred to by P is used for
15618 *parm
= TYPE_MAIN_VARIANT (*parm
);
15619 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15621 *parm
= TREE_TYPE (*parm
);
15622 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15625 /* DR 322. For conversion deduction, remove a reference type on parm
15626 too (which has been swapped into ARG). */
15627 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15628 *arg
= TREE_TYPE (*arg
);
15633 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15634 template which does contain any deducible template parameters; check if
15635 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15636 unify_one_argument. */
15639 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15640 int flags
, bool explain_p
)
15645 type
= TREE_TYPE (arg
);
15649 if (same_type_p (parm
, type
))
15650 return unify_success (explain_p
);
15652 if (strict
== DEDUCE_CONV
)
15654 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15655 explain_p
? tf_warning_or_error
: tf_none
))
15656 return unify_success (explain_p
);
15658 else if (strict
!= DEDUCE_EXACT
)
15660 if (can_convert_arg (parm
, type
,
15661 TYPE_P (arg
) ? NULL_TREE
: arg
,
15662 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15663 return unify_success (explain_p
);
15666 if (strict
== DEDUCE_EXACT
)
15667 return unify_type_mismatch (explain_p
, parm
, arg
);
15669 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15672 static bool uses_deducible_template_parms (tree type
);
15674 /* Returns true iff the expression EXPR is one from which a template
15675 argument can be deduced. In other words, if it's an undecorated
15676 use of a template non-type parameter. */
15679 deducible_expression (tree expr
)
15681 return (TREE_CODE (expr
) == TEMPLATE_PARM_INDEX
);
15684 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15685 deducible way; that is, if it has a max value of <PARM> - 1. */
15688 deducible_array_bound (tree domain
)
15690 if (domain
== NULL_TREE
)
15693 tree max
= TYPE_MAX_VALUE (domain
);
15694 if (TREE_CODE (max
) != MINUS_EXPR
)
15697 return deducible_expression (TREE_OPERAND (max
, 0));
15700 /* Returns true iff the template arguments ARGS use a template parameter
15701 in a deducible way. */
15704 deducible_template_args (tree args
)
15706 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
15709 tree elt
= TREE_VEC_ELT (args
, i
);
15710 if (ARGUMENT_PACK_P (elt
))
15711 deducible
= deducible_template_args (ARGUMENT_PACK_ARGS (elt
));
15714 if (PACK_EXPANSION_P (elt
))
15715 elt
= PACK_EXPANSION_PATTERN (elt
);
15716 if (TREE_CODE (elt
) == TEMPLATE_TEMPLATE_PARM
)
15718 else if (TYPE_P (elt
))
15719 deducible
= uses_deducible_template_parms (elt
);
15721 deducible
= deducible_expression (elt
);
15729 /* Returns true iff TYPE contains any deducible references to template
15730 parameters, as per 14.8.2.5. */
15733 uses_deducible_template_parms (tree type
)
15735 if (PACK_EXPANSION_P (type
))
15736 type
= PACK_EXPANSION_PATTERN (type
);
15743 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15744 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15750 if (POINTER_TYPE_P (type
))
15751 return uses_deducible_template_parms (TREE_TYPE (type
));
15753 /* T[integer-constant ]
15755 if (TREE_CODE (type
) == ARRAY_TYPE
)
15756 return (uses_deducible_template_parms (TREE_TYPE (type
))
15757 || deducible_array_bound (TYPE_DOMAIN (type
)));
15769 if (TYPE_PTRMEM_P (type
))
15770 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type
))
15771 || (uses_deducible_template_parms
15772 (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
15774 /* template-name <T> (where template-name refers to a class template)
15775 template-name <i> (where template-name refers to a class template) */
15776 if (CLASS_TYPE_P (type
)
15777 && CLASSTYPE_TEMPLATE_INFO (type
)
15778 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
15779 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15780 (CLASSTYPE_TI_ARGS (type
)));
15785 if (TREE_CODE (type
) == FUNCTION_TYPE
15786 || TREE_CODE (type
) == METHOD_TYPE
)
15788 if (uses_deducible_template_parms (TREE_TYPE (type
)))
15790 tree parm
= TYPE_ARG_TYPES (type
);
15791 if (TREE_CODE (type
) == METHOD_TYPE
)
15792 parm
= TREE_CHAIN (parm
);
15793 for (; parm
; parm
= TREE_CHAIN (parm
))
15794 if (uses_deducible_template_parms (TREE_VALUE (parm
)))
15801 /* Subroutine of type_unification_real and unify_pack_expansion to
15802 handle unification of a single P/A pair. Parameters are as
15803 for those functions. */
15806 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15807 int subr
, unification_kind_t strict
, int flags
,
15810 tree arg_expr
= NULL_TREE
;
15813 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15814 return unify_invalid (explain_p
);
15815 if (arg
== unknown_type_node
)
15816 /* We can't deduce anything from this, but we might get all the
15817 template args from other function args. */
15818 return unify_success (explain_p
);
15820 /* Implicit conversions (Clause 4) will be performed on a function
15821 argument to convert it to the type of the corresponding function
15822 parameter if the parameter type contains no template-parameters that
15823 participate in template argument deduction. */
15824 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15825 /* For function parameters that contain no template-parameters at all,
15826 we have historically checked for convertibility in order to shortcut
15827 consideration of this candidate. */
15828 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15830 else if (strict
== DEDUCE_CALL
15831 && TYPE_P (parm
) && !uses_deducible_template_parms (parm
))
15832 /* For function parameters with only non-deducible template parameters,
15834 return unify_success (explain_p
);
15839 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15840 | UNIFY_ALLOW_MORE_CV_QUAL
15841 | UNIFY_ALLOW_DERIVED
);
15845 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15849 arg_strict
= UNIFY_ALLOW_NONE
;
15853 gcc_unreachable ();
15856 /* We only do these transformations if this is the top-level
15857 parameter_type_list in a call or declaration matching; in other
15858 situations (nested function declarators, template argument lists) we
15859 won't be comparing a type to an expression, and we don't do any type
15865 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15866 if (type_unknown_p (arg
))
15868 /* [temp.deduct.type] A template-argument can be
15869 deduced from a pointer to function or pointer
15870 to member function argument if the set of
15871 overloaded functions does not contain function
15872 templates and at most one of a set of
15873 overloaded functions provides a unique
15876 if (resolve_overloaded_unification
15877 (tparms
, targs
, parm
, arg
, strict
,
15878 arg_strict
, explain_p
))
15879 return unify_success (explain_p
);
15880 return unify_overload_resolution_failure (explain_p
, arg
);
15884 arg
= unlowered_expr_type (arg
);
15885 if (arg
== error_mark_node
)
15886 return unify_invalid (explain_p
);
15890 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15893 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15894 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15896 /* For deduction from an init-list we need the actual list. */
15897 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15899 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15902 /* Most parms like fn_type_unification.
15904 If SUBR is 1, we're being called recursively (to unify the
15905 arguments of a function or method parameter of a function
15908 CHECKS is a pointer to a vector of access checks encountered while
15909 substituting default template arguments. */
15912 type_unification_real (tree tparms
,
15916 unsigned int xnargs
,
15918 unification_kind_t strict
,
15920 vec
<deferred_access_check
, va_gc
> **checks
,
15925 int ntparms
= TREE_VEC_LENGTH (tparms
);
15926 int saw_undeduced
= 0;
15929 unsigned int nargs
;
15932 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15933 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15934 gcc_assert (ntparms
> 0);
15936 /* Reset the number of non-defaulted template arguments contained
15938 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15946 while (parms
&& parms
!= void_list_node
15949 parm
= TREE_VALUE (parms
);
15951 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15952 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15953 /* For a function parameter pack that occurs at the end of the
15954 parameter-declaration-list, the type A of each remaining
15955 argument of the call is compared with the type P of the
15956 declarator-id of the function parameter pack. */
15959 parms
= TREE_CHAIN (parms
);
15961 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15962 /* For a function parameter pack that does not occur at the
15963 end of the parameter-declaration-list, the type of the
15964 parameter pack is a non-deduced context. */
15970 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15976 && parms
!= void_list_node
15977 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15979 /* Unify the remaining arguments with the pack expansion type. */
15981 tree parmvec
= make_tree_vec (1);
15983 /* Allocate a TREE_VEC and copy in all of the arguments */
15984 argvec
= make_tree_vec (nargs
- ia
);
15985 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15986 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15988 /* Copy the parameter into parmvec. */
15989 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15990 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15991 /*subr=*/subr
, explain_p
))
15994 /* Advance to the end of the list of parameters. */
15995 parms
= TREE_CHAIN (parms
);
15998 /* Fail if we've reached the end of the parm list, and more args
15999 are present, and the parm list isn't variadic. */
16000 if (ia
< nargs
&& parms
== void_list_node
)
16001 return unify_too_many_arguments (explain_p
, nargs
, ia
);
16002 /* Fail if parms are left and they don't have default values. */
16003 if (parms
&& parms
!= void_list_node
16004 && TREE_PURPOSE (parms
) == NULL_TREE
)
16006 unsigned int count
= nargs
;
16008 while (p
&& p
!= void_list_node
)
16011 p
= TREE_CHAIN (p
);
16013 return unify_too_few_arguments (explain_p
, ia
, count
);
16018 tsubst_flags_t complain
= (explain_p
16019 ? tf_warning_or_error
16022 for (i
= 0; i
< ntparms
; i
++)
16024 tree targ
= TREE_VEC_ELT (targs
, i
);
16025 tree tparm
= TREE_VEC_ELT (tparms
, i
);
16027 /* Clear the "incomplete" flags on all argument packs now so that
16028 substituting them into later default arguments works. */
16029 if (targ
&& ARGUMENT_PACK_P (targ
))
16031 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
16032 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
16035 if (targ
|| tparm
== error_mark_node
)
16037 tparm
= TREE_VALUE (tparm
);
16039 /* If this is an undeduced nontype parameter that depends on
16040 a type parameter, try another pass; its type may have been
16041 deduced from a later argument than the one from which
16042 this parameter can be deduced. */
16043 if (TREE_CODE (tparm
) == PARM_DECL
16044 && uses_template_parms (TREE_TYPE (tparm
))
16045 && !saw_undeduced
++)
16048 /* Core issue #226 (C++0x) [temp.deduct]:
16050 If a template argument has not been deduced, its
16051 default template argument, if any, is used.
16053 When we are in C++98 mode, TREE_PURPOSE will either
16054 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16055 to explicitly check cxx_dialect here. */
16056 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
16058 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
16059 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
16060 reopen_deferring_access_checks (*checks
);
16061 location_t save_loc
= input_location
;
16063 input_location
= DECL_SOURCE_LOCATION (parm
);
16064 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
16065 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
16067 input_location
= save_loc
;
16068 *checks
= get_deferred_access_checks ();
16069 pop_deferring_access_checks ();
16070 if (arg
== error_mark_node
)
16074 TREE_VEC_ELT (targs
, i
) = arg
;
16075 /* The position of the first default template argument,
16076 is also the number of non-defaulted arguments in TARGS.
16078 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16079 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
16084 /* If the type parameter is a parameter pack, then it will
16085 be deduced to an empty parameter pack. */
16086 if (template_parameter_pack_p (tparm
))
16090 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
16092 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
16093 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
16094 TREE_CONSTANT (arg
) = 1;
16097 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16099 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
16101 TREE_VEC_ELT (targs
, i
) = arg
;
16105 return unify_parameter_deduction_failure (explain_p
, tparm
);
16108 #ifdef ENABLE_CHECKING
16109 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
16110 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
16113 return unify_success (explain_p
);
16116 /* Subroutine of type_unification_real. Args are like the variables
16117 at the call site. ARG is an overloaded function (or template-id);
16118 we try deducing template args from each of the overloads, and if
16119 only one succeeds, we go with that. Modifies TARGS and returns
16120 true on success. */
16123 resolve_overloaded_unification (tree tparms
,
16127 unification_kind_t strict
,
16131 tree tempargs
= copy_node (targs
);
16133 tree goodfn
= NULL_TREE
;
16136 if (TREE_CODE (arg
) == ADDR_EXPR
)
16138 arg
= TREE_OPERAND (arg
, 0);
16144 if (TREE_CODE (arg
) == COMPONENT_REF
)
16145 /* Handle `&x' where `x' is some static or non-static member
16147 arg
= TREE_OPERAND (arg
, 1);
16149 if (TREE_CODE (arg
) == OFFSET_REF
)
16150 arg
= TREE_OPERAND (arg
, 1);
16152 /* Strip baselink information. */
16153 if (BASELINK_P (arg
))
16154 arg
= BASELINK_FUNCTIONS (arg
);
16156 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
16158 /* If we got some explicit template args, we need to plug them into
16159 the affected templates before we try to unify, in case the
16160 explicit args will completely resolve the templates in question. */
16163 tree expl_subargs
= TREE_OPERAND (arg
, 1);
16164 arg
= TREE_OPERAND (arg
, 0);
16166 for (; arg
; arg
= OVL_NEXT (arg
))
16168 tree fn
= OVL_CURRENT (arg
);
16169 tree subargs
, elem
;
16171 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16174 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16175 expl_subargs
, NULL_TREE
, tf_none
,
16176 /*require_all_args=*/true,
16177 /*use_default_args=*/true);
16178 if (subargs
!= error_mark_node
16179 && !any_dependent_template_arguments_p (subargs
))
16181 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
16182 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16183 elem
, strict
, sub_strict
, addr_p
, explain_p
)
16184 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
16193 /* If no templates (or more than one) are fully resolved by the
16194 explicit arguments, this template-id is a non-deduced context; it
16195 could still be OK if we deduce all template arguments for the
16196 enclosing call through other arguments. */
16200 else if (TREE_CODE (arg
) != OVERLOAD
16201 && TREE_CODE (arg
) != FUNCTION_DECL
)
16202 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16203 -- but the deduction does not succeed because the expression is
16204 not just the function on its own. */
16207 for (; arg
; arg
= OVL_NEXT (arg
))
16208 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
16209 TREE_TYPE (OVL_CURRENT (arg
)),
16210 strict
, sub_strict
, addr_p
, explain_p
)
16211 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
16213 goodfn
= OVL_CURRENT (arg
);
16217 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16218 to function or pointer to member function argument if the set of
16219 overloaded functions does not contain function templates and at most
16220 one of a set of overloaded functions provides a unique match.
16222 So if we found multiple possibilities, we return success but don't
16223 deduce anything. */
16227 int i
= TREE_VEC_LENGTH (targs
);
16229 if (TREE_VEC_ELT (tempargs
, i
))
16230 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
16238 /* Core DR 115: In contexts where deduction is done and fails, or in
16239 contexts where deduction is not done, if a template argument list is
16240 specified and it, along with any default template arguments, identifies
16241 a single function template specialization, then the template-id is an
16242 lvalue for the function template specialization. */
16245 resolve_nondeduced_context (tree orig_expr
)
16247 tree expr
, offset
, baselink
;
16250 if (!type_unknown_p (orig_expr
))
16255 offset
= NULL_TREE
;
16256 baselink
= NULL_TREE
;
16258 if (TREE_CODE (expr
) == ADDR_EXPR
)
16260 expr
= TREE_OPERAND (expr
, 0);
16263 if (TREE_CODE (expr
) == OFFSET_REF
)
16266 expr
= TREE_OPERAND (expr
, 1);
16268 if (BASELINK_P (expr
))
16271 expr
= BASELINK_FUNCTIONS (expr
);
16274 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
16277 tree goodfn
= NULL_TREE
;
16279 /* If we got some explicit template args, we need to plug them into
16280 the affected templates before we try to unify, in case the
16281 explicit args will completely resolve the templates in question. */
16283 tree expl_subargs
= TREE_OPERAND (expr
, 1);
16284 tree arg
= TREE_OPERAND (expr
, 0);
16285 tree badfn
= NULL_TREE
;
16286 tree badargs
= NULL_TREE
;
16288 for (; arg
; arg
= OVL_NEXT (arg
))
16290 tree fn
= OVL_CURRENT (arg
);
16291 tree subargs
, elem
;
16293 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
16296 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
16297 expl_subargs
, NULL_TREE
, tf_none
,
16298 /*require_all_args=*/true,
16299 /*use_default_args=*/true);
16300 if (subargs
!= error_mark_node
16301 && !any_dependent_template_arguments_p (subargs
))
16303 elem
= instantiate_template (fn
, subargs
, tf_none
);
16304 if (elem
== error_mark_node
)
16309 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
16318 mark_used (goodfn
);
16321 expr
= build_baselink (BASELINK_BINFO (baselink
),
16322 BASELINK_ACCESS_BINFO (baselink
),
16323 expr
, BASELINK_OPTYPE (baselink
));
16327 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
16328 expr
= build_offset_ref (base
, expr
, addr
, tf_warning_or_error
);
16331 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
16334 else if (good
== 0 && badargs
)
16335 /* There were no good options and at least one bad one, so let the
16336 user know what the problem is. */
16337 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
16342 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16343 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16344 different overloads deduce different arguments for a given parm.
16345 ADDR_P is true if the expression for which deduction is being
16346 performed was of the form "& fn" rather than simply "fn".
16348 Returns 1 on success. */
16351 try_one_overload (tree tparms
,
16356 unification_kind_t strict
,
16365 if (arg
== error_mark_node
)
16368 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16369 to function or pointer to member function argument if the set of
16370 overloaded functions does not contain function templates and at most
16371 one of a set of overloaded functions provides a unique match.
16373 So if this is a template, just return success. */
16375 if (uses_template_parms (arg
))
16378 if (TREE_CODE (arg
) == METHOD_TYPE
)
16379 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
16381 arg
= build_pointer_type (arg
);
16383 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
16385 /* We don't copy orig_targs for this because if we have already deduced
16386 some template args from previous args, unify would complain when we
16387 try to deduce a template parameter for the same argument, even though
16388 there isn't really a conflict. */
16389 nargs
= TREE_VEC_LENGTH (targs
);
16390 tempargs
= make_tree_vec (nargs
);
16392 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
16395 /* First make sure we didn't deduce anything that conflicts with
16396 explicitly specified args. */
16397 for (i
= nargs
; i
--; )
16399 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16400 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
16404 else if (uses_template_parms (elt
))
16405 /* Since we're unifying against ourselves, we will fill in
16406 template args used in the function parm list with our own
16407 template parms. Discard them. */
16408 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
16409 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
16413 for (i
= nargs
; i
--; )
16415 tree elt
= TREE_VEC_ELT (tempargs
, i
);
16418 TREE_VEC_ELT (targs
, i
) = elt
;
16424 /* PARM is a template class (perhaps with unbound template
16425 parameters). ARG is a fully instantiated type. If ARG can be
16426 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16427 TARGS are as for unify. */
16430 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
16433 tree copy_of_targs
;
16435 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
16436 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
16437 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
16440 /* We need to make a new template argument vector for the call to
16441 unify. If we used TARGS, we'd clutter it up with the result of
16442 the attempted unification, even if this class didn't work out.
16443 We also don't want to commit ourselves to all the unifications
16444 we've already done, since unification is supposed to be done on
16445 an argument-by-argument basis. In other words, consider the
16446 following pathological case:
16448 template <int I, int J, int K>
16451 template <int I, int J>
16452 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16454 template <int I, int J, int K>
16455 void f(S<I, J, K>, S<I, I, I>);
16464 Now, by the time we consider the unification involving `s2', we
16465 already know that we must have `f<0, 0, 0>'. But, even though
16466 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16467 because there are two ways to unify base classes of S<0, 1, 2>
16468 with S<I, I, I>. If we kept the already deduced knowledge, we
16469 would reject the possibility I=1. */
16470 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
16472 /* If unification failed, we're done. */
16473 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
16474 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
16480 /* Given a template type PARM and a class type ARG, find the unique
16481 base type in ARG that is an instance of PARM. We do not examine
16482 ARG itself; only its base-classes. If there is not exactly one
16483 appropriate base class, return NULL_TREE. PARM may be the type of
16484 a partial specialization, as well as a plain template type. Used
16487 static enum template_base_result
16488 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
16489 bool explain_p
, tree
*result
)
16491 tree rval
= NULL_TREE
;
16494 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
16496 binfo
= TYPE_BINFO (complete_type (arg
));
16499 /* The type could not be completed. */
16500 *result
= NULL_TREE
;
16501 return tbr_incomplete_type
;
16504 /* Walk in inheritance graph order. The search order is not
16505 important, and this avoids multiple walks of virtual bases. */
16506 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
16508 tree r
= try_class_unification (tparms
, targs
, parm
,
16509 BINFO_TYPE (binfo
), explain_p
);
16513 /* If there is more than one satisfactory baseclass, then:
16517 If they yield more than one possible deduced A, the type
16521 if (rval
&& !same_type_p (r
, rval
))
16523 *result
= NULL_TREE
;
16524 return tbr_ambiguous_baseclass
;
16532 return tbr_success
;
16535 /* Returns the level of DECL, which declares a template parameter. */
16538 template_decl_level (tree decl
)
16540 switch (TREE_CODE (decl
))
16543 case TEMPLATE_DECL
:
16544 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
16547 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
16550 gcc_unreachable ();
16555 /* Decide whether ARG can be unified with PARM, considering only the
16556 cv-qualifiers of each type, given STRICT as documented for unify.
16557 Returns nonzero iff the unification is OK on that basis. */
16560 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
16562 int arg_quals
= cp_type_quals (arg
);
16563 int parm_quals
= cp_type_quals (parm
);
16565 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16566 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16568 /* Although a CVR qualifier is ignored when being applied to a
16569 substituted template parameter ([8.3.2]/1 for example), that
16570 does not allow us to unify "const T" with "int&" because both
16571 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16572 It is ok when we're allowing additional CV qualifiers
16573 at the outer level [14.8.2.1]/3,1st bullet. */
16574 if ((TREE_CODE (arg
) == REFERENCE_TYPE
16575 || TREE_CODE (arg
) == FUNCTION_TYPE
16576 || TREE_CODE (arg
) == METHOD_TYPE
)
16577 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
16580 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
16581 && (parm_quals
& TYPE_QUAL_RESTRICT
))
16585 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
16586 && (arg_quals
& parm_quals
) != parm_quals
)
16589 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
16590 && (parm_quals
& arg_quals
) != arg_quals
)
16596 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16598 template_parm_level_and_index (tree parm
, int* level
, int* index
)
16600 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16601 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16602 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16604 *index
= TEMPLATE_TYPE_IDX (parm
);
16605 *level
= TEMPLATE_TYPE_LEVEL (parm
);
16609 *index
= TEMPLATE_PARM_IDX (parm
);
16610 *level
= TEMPLATE_PARM_LEVEL (parm
);
16614 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16616 if (unify (TP, TA, P, A, S, EP)) \
16620 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16621 expansion at the end of PACKED_PARMS. Returns 0 if the type
16622 deduction succeeds, 1 otherwise. STRICT is the same as in
16623 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16624 call argument list. We'll need to adjust the arguments to make them
16625 types. SUBR tells us if this is from a recursive call to
16626 type_unification_real, or for comparing two template argument
16630 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
16631 tree packed_args
, unification_kind_t strict
,
16632 bool subr
, bool explain_p
)
16635 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
16636 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
16637 tree pack
, packs
= NULL_TREE
;
16638 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
16639 int len
= TREE_VEC_LENGTH (packed_args
);
16641 /* Determine the parameter packs we will be deducing from the
16642 pattern, and record their current deductions. */
16643 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
16644 pack
; pack
= TREE_CHAIN (pack
))
16646 tree parm_pack
= TREE_VALUE (pack
);
16649 /* Determine the index and level of this parameter pack. */
16650 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16652 /* Keep track of the parameter packs and their corresponding
16654 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16655 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16658 /* Loop through all of the arguments that have not yet been
16659 unified and unify each with the pattern. */
16660 for (i
= start
; i
< len
; i
++)
16663 bool any_explicit
= false;
16664 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16666 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16667 or the element of its argument pack at the current index if
16668 this argument was explicitly specified. */
16669 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16673 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16676 if (TREE_VALUE (pack
)
16677 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16678 && (i
- start
< TREE_VEC_LENGTH (pargs
)))
16680 any_explicit
= true;
16681 arg
= TREE_VEC_ELT (pargs
, i
- start
);
16683 TMPL_ARG (targs
, level
, idx
) = arg
;
16686 /* If we had explicit template arguments, substitute them into the
16687 pattern before deduction. */
16690 /* Some arguments might still be unspecified or dependent. */
16692 ++processing_template_decl
;
16693 dependent
= any_dependent_template_arguments_p (targs
);
16695 --processing_template_decl
;
16696 parm
= tsubst (pattern
, targs
,
16697 explain_p
? tf_warning_or_error
: tf_none
,
16700 --processing_template_decl
;
16701 if (parm
== error_mark_node
)
16707 /* Unify the pattern with the current argument. */
16708 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16709 LOOKUP_IMPLICIT
, explain_p
))
16712 /* For each parameter pack, collect the deduced value. */
16713 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16716 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16718 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16719 TMPL_ARG (targs
, level
, idx
);
16723 /* Verify that the results of unification with the parameter packs
16724 produce results consistent with what we've seen before, and make
16725 the deduced argument packs available. */
16726 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16728 tree old_pack
= TREE_VALUE (pack
);
16729 tree new_args
= TREE_TYPE (pack
);
16730 int i
, len
= TREE_VEC_LENGTH (new_args
);
16732 bool nondeduced_p
= false;
16734 /* By default keep the original deduced argument pack.
16735 If necessary, more specific code is going to update the
16736 resulting deduced argument later down in this function. */
16737 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16738 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16740 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16741 actually deduce anything. */
16742 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16743 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16744 nondeduced_p
= true;
16748 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16750 /* If we had fewer function args than explicit template args,
16751 just use the explicits. */
16752 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16753 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16754 if (len
< explicit_len
)
16755 new_args
= explicit_args
;
16761 /* Build the deduced *_ARGUMENT_PACK. */
16762 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16764 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16765 TREE_TYPE (result
) =
16766 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16767 TREE_CONSTANT (result
) = 1;
16770 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16772 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16774 /* Note the deduced argument packs for this parameter
16776 TMPL_ARG (targs
, level
, idx
) = result
;
16778 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16779 && (ARGUMENT_PACK_ARGS (old_pack
)
16780 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16782 /* We only had the explicitly-provided arguments before, but
16783 now we have a complete set of arguments. */
16784 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16786 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16787 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16788 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16792 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16793 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16795 if (!comp_template_args_with_info (old_args
, new_args
,
16796 &bad_old_arg
, &bad_new_arg
))
16797 /* Inconsistent unification of this parameter pack. */
16798 return unify_parameter_pack_inconsistent (explain_p
,
16804 return unify_success (explain_p
);
16807 /* Deduce the value of template parameters. TPARMS is the (innermost)
16808 set of template parameters to a template. TARGS is the bindings
16809 for those template parameters, as determined thus far; TARGS may
16810 include template arguments for outer levels of template parameters
16811 as well. PARM is a parameter to a template function, or a
16812 subcomponent of that parameter; ARG is the corresponding argument.
16813 This function attempts to match PARM with ARG in a manner
16814 consistent with the existing assignments in TARGS. If more values
16815 are deduced, then TARGS is updated.
16817 Returns 0 if the type deduction succeeds, 1 otherwise. The
16818 parameter STRICT is a bitwise or of the following flags:
16821 Require an exact match between PARM and ARG.
16822 UNIFY_ALLOW_MORE_CV_QUAL:
16823 Allow the deduced ARG to be more cv-qualified (by qualification
16824 conversion) than ARG.
16825 UNIFY_ALLOW_LESS_CV_QUAL:
16826 Allow the deduced ARG to be less cv-qualified than ARG.
16827 UNIFY_ALLOW_DERIVED:
16828 Allow the deduced ARG to be a template base class of ARG,
16829 or a pointer to a template base class of the type pointed to by
16831 UNIFY_ALLOW_INTEGER:
16832 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16833 case for more information.
16834 UNIFY_ALLOW_OUTER_LEVEL:
16835 This is the outermost level of a deduction. Used to determine validity
16836 of qualification conversions. A valid qualification conversion must
16837 have const qualified pointers leading up to the inner type which
16838 requires additional CV quals, except at the outer level, where const
16839 is not required [conv.qual]. It would be normal to set this flag in
16840 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16841 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16842 This is the outermost level of a deduction, and PARM can be more CV
16843 qualified at this point.
16844 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16845 This is the outermost level of a deduction, and PARM can be less CV
16846 qualified at this point. */
16849 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16855 int strict_in
= strict
;
16857 /* I don't think this will do the right thing with respect to types.
16858 But the only case I've seen it in so far has been array bounds, where
16859 signedness is the only information lost, and I think that will be
16861 while (TREE_CODE (parm
) == NOP_EXPR
)
16862 parm
= TREE_OPERAND (parm
, 0);
16864 if (arg
== error_mark_node
)
16865 return unify_invalid (explain_p
);
16866 if (arg
== unknown_type_node
16867 || arg
== init_list_type_node
)
16868 /* We can't deduce anything from this, but we might get all the
16869 template args from other function args. */
16870 return unify_success (explain_p
);
16872 /* If PARM uses template parameters, then we can't bail out here,
16873 even if ARG == PARM, since we won't record unifications for the
16874 template parameters. We might need them if we're trying to
16875 figure out which of two things is more specialized. */
16876 if (arg
== parm
&& !uses_template_parms (parm
))
16877 return unify_success (explain_p
);
16879 /* Handle init lists early, so the rest of the function can assume
16880 we're dealing with a type. */
16881 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16885 tree orig_parm
= parm
;
16887 /* Replace T with std::initializer_list<T> for deduction. */
16888 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16889 && flag_deduce_init_list
)
16890 parm
= listify (parm
);
16892 if (!is_std_init_list (parm
))
16893 /* We can only deduce from an initializer list argument if the
16894 parameter is std::initializer_list; otherwise this is a
16895 non-deduced context. */
16896 return unify_success (explain_p
);
16898 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16900 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16902 int elt_strict
= strict
;
16904 if (elt
== error_mark_node
)
16905 return unify_invalid (explain_p
);
16907 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16909 tree type
= TREE_TYPE (elt
);
16910 /* It should only be possible to get here for a call. */
16911 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16912 elt_strict
|= maybe_adjust_types_for_deduction
16913 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16917 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16921 /* If the std::initializer_list<T> deduction worked, replace the
16922 deduced A with std::initializer_list<A>. */
16923 if (orig_parm
!= parm
)
16925 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16926 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16927 targ
= listify (targ
);
16928 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16930 return unify_success (explain_p
);
16933 /* Immediately reject some pairs that won't unify because of
16934 cv-qualification mismatches. */
16935 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16937 /* It is the elements of the array which hold the cv quals of an array
16938 type, and the elements might be template type parms. We'll check
16939 when we recurse. */
16940 && TREE_CODE (arg
) != ARRAY_TYPE
16941 /* We check the cv-qualifiers when unifying with template type
16942 parameters below. We want to allow ARG `const T' to unify with
16943 PARM `T' for example, when computing which of two templates
16944 is more specialized, for example. */
16945 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16946 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16947 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16949 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16950 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16951 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16952 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16953 strict
&= ~UNIFY_ALLOW_DERIVED
;
16954 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16955 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16957 switch (TREE_CODE (parm
))
16959 case TYPENAME_TYPE
:
16961 case UNBOUND_CLASS_TEMPLATE
:
16962 /* In a type which contains a nested-name-specifier, template
16963 argument values cannot be deduced for template parameters used
16964 within the nested-name-specifier. */
16965 return unify_success (explain_p
);
16967 case TEMPLATE_TYPE_PARM
:
16968 case TEMPLATE_TEMPLATE_PARM
:
16969 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16970 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16971 if (tparm
== error_mark_node
)
16972 return unify_invalid (explain_p
);
16974 if (TEMPLATE_TYPE_LEVEL (parm
)
16975 != template_decl_level (tparm
))
16976 /* The PARM is not one we're trying to unify. Just check
16977 to see if it matches ARG. */
16979 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16980 && (is_auto (parm
) ? is_auto (arg
)
16981 : same_type_p (parm
, arg
)))
16982 return unify_success (explain_p
);
16984 return unify_type_mismatch (explain_p
, parm
, arg
);
16986 idx
= TEMPLATE_TYPE_IDX (parm
);
16987 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16988 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16989 if (tparm
== error_mark_node
)
16990 return unify_invalid (explain_p
);
16992 /* Check for mixed types and values. */
16993 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16994 && TREE_CODE (tparm
) != TYPE_DECL
)
16995 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16996 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16997 gcc_unreachable ();
16999 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17001 /* ARG must be constructed from a template class or a template
17002 template parameter. */
17003 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
17004 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
17005 return unify_template_deduction_failure (explain_p
, parm
, arg
);
17008 tree parmvec
= TYPE_TI_ARGS (parm
);
17009 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
17010 tree full_argvec
= add_to_template_args (targs
, argvec
);
17012 = DECL_INNERMOST_TEMPLATE_PARMS
17013 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
17015 int parm_variadic_p
= 0;
17017 /* The resolution to DR150 makes clear that default
17018 arguments for an N-argument may not be used to bind T
17019 to a template template parameter with fewer than N
17020 parameters. It is not safe to permit the binding of
17021 default arguments as an extension, as that may change
17022 the meaning of a conforming program. Consider:
17024 struct Dense { static const unsigned int dim = 1; };
17026 template <template <typename> class View,
17028 void operator+(float, View<Block> const&);
17030 template <typename Block,
17031 unsigned int Dim = Block::dim>
17032 struct Lvalue_proxy { operator float() const; };
17036 Lvalue_proxy<Dense> p;
17041 Here, if Lvalue_proxy is permitted to bind to View, then
17042 the global operator+ will be used; if they are not, the
17043 Lvalue_proxy will be converted to float. */
17044 if (coerce_template_parms (parm_parms
,
17046 TYPE_TI_TEMPLATE (parm
),
17048 ? tf_warning_or_error
17050 /*require_all_args=*/true,
17051 /*use_default_args=*/false)
17052 == error_mark_node
)
17055 /* Deduce arguments T, i from TT<T> or TT<i>.
17056 We check each element of PARMVEC and ARGVEC individually
17057 rather than the whole TREE_VEC since they can have
17058 different number of elements. */
17060 parmvec
= expand_template_argument_pack (parmvec
);
17061 argvec
= expand_template_argument_pack (argvec
);
17063 len
= TREE_VEC_LENGTH (parmvec
);
17065 /* Check if the parameters end in a pack, making them
17068 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
17069 parm_variadic_p
= 1;
17071 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17072 /* If the template argument list of P contains a pack
17073 expansion that is not the last template argument, the
17074 entire template argument list is a non-deduced
17076 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, i
)))
17077 return unify_success (explain_p
);
17079 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
17080 return unify_too_few_arguments (explain_p
,
17081 TREE_VEC_LENGTH (argvec
), len
);
17083 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17085 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17086 TREE_VEC_ELT (parmvec
, i
),
17087 TREE_VEC_ELT (argvec
, i
),
17088 UNIFY_ALLOW_NONE
, explain_p
);
17091 if (parm_variadic_p
17092 && unify_pack_expansion (tparms
, targs
,
17095 /*subr=*/true, explain_p
))
17098 arg
= TYPE_TI_TEMPLATE (arg
);
17100 /* Fall through to deduce template name. */
17103 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
17104 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17106 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17108 /* Simple cases: Value already set, does match or doesn't. */
17109 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
17110 return unify_success (explain_p
);
17112 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17116 /* If PARM is `const T' and ARG is only `int', we don't have
17117 a match unless we are allowing additional qualification.
17118 If ARG is `const int' and PARM is just `T' that's OK;
17119 that binds `const int' to `T'. */
17120 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
17122 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17124 /* Consider the case where ARG is `const volatile int' and
17125 PARM is `const T'. Then, T should be `volatile int'. */
17126 arg
= cp_build_qualified_type_real
17127 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
17128 if (arg
== error_mark_node
)
17129 return unify_invalid (explain_p
);
17131 /* Simple cases: Value already set, does match or doesn't. */
17132 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
17133 return unify_success (explain_p
);
17135 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
17137 /* Make sure that ARG is not a variable-sized array. (Note
17138 that were talking about variable-sized arrays (like
17139 `int[n]'), rather than arrays of unknown size (like
17140 `int[]').) We'll get very confused by such a type since
17141 the bound of the array is not constant, and therefore
17142 not mangleable. Besides, such types are not allowed in
17143 ISO C++, so we can do as we please here. We do allow
17144 them for 'auto' deduction, since that isn't ABI-exposed. */
17145 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
17146 return unify_vla_arg (explain_p
, arg
);
17148 /* Strip typedefs as in convert_template_argument. */
17149 arg
= canonicalize_type_argument (arg
, tf_none
);
17152 /* If ARG is a parameter pack or an expansion, we cannot unify
17153 against it unless PARM is also a parameter pack. */
17154 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17155 && !template_parameter_pack_p (parm
))
17156 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17158 /* If the argument deduction results is a METHOD_TYPE,
17159 then there is a problem.
17160 METHOD_TYPE doesn't map to any real C++ type the result of
17161 the deduction can not be of that type. */
17162 if (TREE_CODE (arg
) == METHOD_TYPE
)
17163 return unify_method_type_error (explain_p
, arg
);
17165 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17166 return unify_success (explain_p
);
17168 case TEMPLATE_PARM_INDEX
:
17169 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
17170 if (tparm
== error_mark_node
)
17171 return unify_invalid (explain_p
);
17173 if (TEMPLATE_PARM_LEVEL (parm
)
17174 != template_decl_level (tparm
))
17176 /* The PARM is not one we're trying to unify. Just check
17177 to see if it matches ARG. */
17178 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
17179 && cp_tree_equal (parm
, arg
));
17181 unify_expression_unequal (explain_p
, parm
, arg
);
17185 idx
= TEMPLATE_PARM_IDX (parm
);
17186 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
17190 int x
= !cp_tree_equal (targ
, arg
);
17192 unify_inconsistency (explain_p
, parm
, targ
, arg
);
17196 /* [temp.deduct.type] If, in the declaration of a function template
17197 with a non-type template-parameter, the non-type
17198 template-parameter is used in an expression in the function
17199 parameter-list and, if the corresponding template-argument is
17200 deduced, the template-argument type shall match the type of the
17201 template-parameter exactly, except that a template-argument
17202 deduced from an array bound may be of any integral type.
17203 The non-type parameter might use already deduced type parameters. */
17204 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
17205 if (!TREE_TYPE (arg
))
17206 /* Template-parameter dependent expression. Just accept it for now.
17207 It will later be processed in convert_template_argument. */
17209 else if (same_type_p (TREE_TYPE (arg
), tparm
))
17211 else if ((strict
& UNIFY_ALLOW_INTEGER
)
17212 && CP_INTEGRAL_TYPE_P (tparm
))
17213 /* Convert the ARG to the type of PARM; the deduced non-type
17214 template argument must exactly match the types of the
17215 corresponding parameter. */
17216 arg
= fold (build_nop (tparm
, arg
));
17217 else if (uses_template_parms (tparm
))
17218 /* We haven't deduced the type of this parameter yet. Try again
17220 return unify_success (explain_p
);
17222 return unify_type_mismatch (explain_p
, tparm
, TREE_TYPE (arg
));
17224 /* If ARG is a parameter pack or an expansion, we cannot unify
17225 against it unless PARM is also a parameter pack. */
17226 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
17227 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
17228 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
17230 arg
= strip_typedefs_expr (arg
);
17231 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
17232 return unify_success (explain_p
);
17236 /* A pointer-to-member constant can be unified only with
17237 another constant. */
17238 if (TREE_CODE (arg
) != PTRMEM_CST
)
17239 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
17241 /* Just unify the class member. It would be useless (and possibly
17242 wrong, depending on the strict flags) to unify also
17243 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17244 arg refer to the same variable, even if through different
17245 classes. For instance:
17247 struct A { int x; };
17250 Unification of &A::x and &B::x must succeed. */
17251 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
17252 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
17257 if (!TYPE_PTR_P (arg
))
17258 return unify_type_mismatch (explain_p
, parm
, arg
);
17260 /* [temp.deduct.call]
17262 A can be another pointer or pointer to member type that can
17263 be converted to the deduced A via a qualification
17264 conversion (_conv.qual_).
17266 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17267 This will allow for additional cv-qualification of the
17268 pointed-to types if appropriate. */
17270 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
17271 /* The derived-to-base conversion only persists through one
17272 level of pointers. */
17273 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
17275 return unify (tparms
, targs
, TREE_TYPE (parm
),
17276 TREE_TYPE (arg
), strict
, explain_p
);
17279 case REFERENCE_TYPE
:
17280 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
17281 return unify_type_mismatch (explain_p
, parm
, arg
);
17282 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17283 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17286 if (TREE_CODE (arg
) != ARRAY_TYPE
)
17287 return unify_type_mismatch (explain_p
, parm
, arg
);
17288 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
17289 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
17290 return unify_type_mismatch (explain_p
, parm
, arg
);
17291 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17292 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
17293 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
17300 /* Our representation of array types uses "N - 1" as the
17301 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17302 not an integer constant. We cannot unify arbitrarily
17303 complex expressions, so we eliminate the MINUS_EXPRs
17305 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
17306 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
17309 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
17310 parm_max
= TREE_OPERAND (parm_max
, 0);
17312 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
17313 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
17316 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17317 trying to unify the type of a variable with the type
17318 of a template parameter. For example:
17320 template <unsigned int N>
17321 void f (char (&) [N]);
17328 Here, the type of the ARG will be "int [g(i)]", and
17329 may be a SAVE_EXPR, etc. */
17330 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
17331 return unify_vla_arg (explain_p
, arg
);
17332 arg_max
= TREE_OPERAND (arg_max
, 0);
17335 /* If only one of the bounds used a MINUS_EXPR, compensate
17336 by adding one to the other bound. */
17337 if (parm_cst
&& !arg_cst
)
17338 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17342 else if (arg_cst
&& !parm_cst
)
17343 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
17348 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
17349 UNIFY_ALLOW_INTEGER
, explain_p
);
17351 return unify_success (explain_p
);
17358 case ENUMERAL_TYPE
:
17361 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17362 return unify_type_mismatch (explain_p
, parm
, arg
);
17364 /* We have already checked cv-qualification at the top of the
17366 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
17367 return unify_type_mismatch (explain_p
, parm
, arg
);
17369 /* As far as unification is concerned, this wins. Later checks
17370 will invalidate it if necessary. */
17371 return unify_success (explain_p
);
17373 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17374 /* Type INTEGER_CST can come from ordinary constant template args. */
17376 while (TREE_CODE (arg
) == NOP_EXPR
)
17377 arg
= TREE_OPERAND (arg
, 0);
17379 if (TREE_CODE (arg
) != INTEGER_CST
)
17380 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17381 return (tree_int_cst_equal (parm
, arg
)
17382 ? unify_success (explain_p
)
17383 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
17387 int i
, len
, argslen
;
17388 int parm_variadic_p
= 0;
17390 if (TREE_CODE (arg
) != TREE_VEC
)
17391 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17393 len
= TREE_VEC_LENGTH (parm
);
17394 argslen
= TREE_VEC_LENGTH (arg
);
17396 /* Check for pack expansions in the parameters. */
17397 for (i
= 0; i
< len
; ++i
)
17399 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
17402 /* We can unify against something with a trailing
17404 parm_variadic_p
= 1;
17406 /* [temp.deduct.type]/9: If the template argument list of
17407 P contains a pack expansion that is not the last
17408 template argument, the entire template argument list
17409 is a non-deduced context. */
17410 return unify_success (explain_p
);
17414 /* If we don't have enough arguments to satisfy the parameters
17415 (not counting the pack expression at the end), or we have
17416 too many arguments for a parameter list that doesn't end in
17417 a pack expression, we can't unify. */
17418 if (parm_variadic_p
17419 ? argslen
< len
- parm_variadic_p
17421 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
17423 /* Unify all of the parameters that precede the (optional)
17424 pack expression. */
17425 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
17427 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
17428 TREE_VEC_ELT (parm
, i
),
17429 TREE_VEC_ELT (arg
, i
),
17430 UNIFY_ALLOW_NONE
, explain_p
);
17432 if (parm_variadic_p
)
17433 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
17435 /*subr=*/true, explain_p
);
17436 return unify_success (explain_p
);
17441 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17442 return unify_type_mismatch (explain_p
, parm
, arg
);
17444 if (TYPE_PTRMEMFUNC_P (parm
))
17446 if (!TYPE_PTRMEMFUNC_P (arg
))
17447 return unify_type_mismatch (explain_p
, parm
, arg
);
17449 return unify (tparms
, targs
,
17450 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
17451 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
17452 strict
, explain_p
);
17455 if (CLASSTYPE_TEMPLATE_INFO (parm
))
17457 tree t
= NULL_TREE
;
17459 if (strict_in
& UNIFY_ALLOW_DERIVED
)
17461 /* First, we try to unify the PARM and ARG directly. */
17462 t
= try_class_unification (tparms
, targs
,
17463 parm
, arg
, explain_p
);
17467 /* Fallback to the special case allowed in
17468 [temp.deduct.call]:
17470 If P is a class, and P has the form
17471 template-id, then A can be a derived class of
17472 the deduced A. Likewise, if P is a pointer to
17473 a class of the form template-id, A can be a
17474 pointer to a derived class pointed to by the
17476 enum template_base_result r
;
17477 r
= get_template_base (tparms
, targs
, parm
, arg
,
17481 return unify_no_common_base (explain_p
, r
, parm
, arg
);
17484 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
17485 && (CLASSTYPE_TI_TEMPLATE (parm
)
17486 == CLASSTYPE_TI_TEMPLATE (arg
)))
17487 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17488 Then, we should unify `int' and `U'. */
17491 /* There's no chance of unification succeeding. */
17492 return unify_type_mismatch (explain_p
, parm
, arg
);
17494 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
17495 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
17497 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
17498 return unify_type_mismatch (explain_p
, parm
, arg
);
17499 return unify_success (explain_p
);
17502 case FUNCTION_TYPE
:
17504 unsigned int nargs
;
17509 if (TREE_CODE (arg
) != TREE_CODE (parm
))
17510 return unify_type_mismatch (explain_p
, parm
, arg
);
17512 /* CV qualifications for methods can never be deduced, they must
17513 match exactly. We need to check them explicitly here,
17514 because type_unification_real treats them as any other
17515 cv-qualified parameter. */
17516 if (TREE_CODE (parm
) == METHOD_TYPE
17517 && (!check_cv_quals_for_unify
17519 class_of_this_parm (arg
),
17520 class_of_this_parm (parm
))))
17521 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17523 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
17524 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
17526 nargs
= list_length (TYPE_ARG_TYPES (arg
));
17527 args
= XALLOCAVEC (tree
, nargs
);
17528 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
17529 a
!= NULL_TREE
&& a
!= void_list_node
;
17530 a
= TREE_CHAIN (a
), ++i
)
17531 args
[i
] = TREE_VALUE (a
);
17534 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
17535 args
, nargs
, 1, DEDUCE_EXACT
,
17536 LOOKUP_NORMAL
, NULL
, explain_p
);
17540 /* Unify a pointer to member with a pointer to member function, which
17541 deduces the type of the member as a function type. */
17542 if (TYPE_PTRMEMFUNC_P (arg
))
17544 /* Check top-level cv qualifiers */
17545 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
17546 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
17548 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17549 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
17550 UNIFY_ALLOW_NONE
, explain_p
);
17552 /* Determine the type of the function we are unifying against. */
17553 tree fntype
= static_fn_type (arg
);
17555 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
17558 if (TREE_CODE (arg
) != OFFSET_TYPE
)
17559 return unify_type_mismatch (explain_p
, parm
, arg
);
17560 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
17561 TYPE_OFFSET_BASETYPE (arg
),
17562 UNIFY_ALLOW_NONE
, explain_p
);
17563 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
17564 strict
, explain_p
);
17567 if (DECL_TEMPLATE_PARM_P (parm
))
17568 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
17569 if (arg
!= integral_constant_value (parm
))
17570 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17571 return unify_success (explain_p
);
17574 case TEMPLATE_DECL
:
17575 /* Matched cases are handled by the ARG == PARM test above. */
17576 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
17579 /* A non-type template parameter that is a variable should be a
17580 an integral constant, in which case, it whould have been
17581 folded into its (constant) value. So we should not be getting
17582 a variable here. */
17583 gcc_unreachable ();
17585 case TYPE_ARGUMENT_PACK
:
17586 case NONTYPE_ARGUMENT_PACK
:
17587 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
17588 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
17591 case DECLTYPE_TYPE
:
17592 case UNDERLYING_TYPE
:
17593 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17594 or UNDERLYING_TYPE nodes. */
17595 return unify_success (explain_p
);
17598 /* Unification fails if we hit an error node. */
17599 return unify_invalid (explain_p
);
17602 /* An unresolved overload is a nondeduced context. */
17603 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
17604 return unify_success (explain_p
);
17605 gcc_assert (EXPR_P (parm
));
17607 /* We must be looking at an expression. This can happen with
17611 void foo(S<I>, S<I + 2>);
17613 This is a "nondeduced context":
17617 The nondeduced contexts are:
17619 --A type that is a template-id in which one or more of
17620 the template-arguments is an expression that references
17621 a template-parameter.
17623 In these cases, we assume deduction succeeded, but don't
17624 actually infer any unifications. */
17626 if (!uses_template_parms (parm
)
17627 && !template_args_equal (parm
, arg
))
17628 return unify_expression_unequal (explain_p
, parm
, arg
);
17630 return unify_success (explain_p
);
17633 #undef RECUR_AND_CHECK_FAILURE
17635 /* Note that DECL can be defined in this translation unit, if
17639 mark_definable (tree decl
)
17642 DECL_NOT_REALLY_EXTERN (decl
) = 1;
17643 FOR_EACH_CLONE (clone
, decl
)
17644 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17647 /* Called if RESULT is explicitly instantiated, or is a member of an
17648 explicitly instantiated class. */
17651 mark_decl_instantiated (tree result
, int extern_p
)
17653 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17655 /* If this entity has already been written out, it's too late to
17656 make any modifications. */
17657 if (TREE_ASM_WRITTEN (result
))
17660 /* For anonymous namespace we don't need to do anything. */
17661 if (decl_anon_ns_mem_p (result
))
17663 gcc_assert (!TREE_PUBLIC (result
));
17667 if (TREE_CODE (result
) != FUNCTION_DECL
)
17668 /* The TREE_PUBLIC flag for function declarations will have been
17669 set correctly by tsubst. */
17670 TREE_PUBLIC (result
) = 1;
17672 /* This might have been set by an earlier implicit instantiation. */
17673 DECL_COMDAT (result
) = 0;
17676 DECL_NOT_REALLY_EXTERN (result
) = 0;
17679 mark_definable (result
);
17680 /* Always make artificials weak. */
17681 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17682 comdat_linkage (result
);
17683 /* For WIN32 we also want to put explicit instantiations in
17684 linkonce sections. */
17685 else if (TREE_PUBLIC (result
))
17686 maybe_make_one_only (result
);
17689 /* If EXTERN_P, then this function will not be emitted -- unless
17690 followed by an explicit instantiation, at which point its linkage
17691 will be adjusted. If !EXTERN_P, then this function will be
17692 emitted here. In neither circumstance do we want
17693 import_export_decl to adjust the linkage. */
17694 DECL_INTERFACE_KNOWN (result
) = 1;
17697 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17698 important template arguments. If any are missing, we check whether
17699 they're important by using error_mark_node for substituting into any
17700 args that were used for partial ordering (the ones between ARGS and END)
17701 and seeing if it bubbles up. */
17704 check_undeduced_parms (tree targs
, tree args
, tree end
)
17706 bool found
= false;
17708 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17709 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17712 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17716 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
17717 if (substed
== error_mark_node
)
17723 /* Given two function templates PAT1 and PAT2, return:
17725 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17726 -1 if PAT2 is more specialized than PAT1.
17727 0 if neither is more specialized.
17729 LEN indicates the number of parameters we should consider
17730 (defaulted parameters should not be considered).
17732 The 1998 std underspecified function template partial ordering, and
17733 DR214 addresses the issue. We take pairs of arguments, one from
17734 each of the templates, and deduce them against each other. One of
17735 the templates will be more specialized if all the *other*
17736 template's arguments deduce against its arguments and at least one
17737 of its arguments *does* *not* deduce against the other template's
17738 corresponding argument. Deduction is done as for class templates.
17739 The arguments used in deduction have reference and top level cv
17740 qualifiers removed. Iff both arguments were originally reference
17741 types *and* deduction succeeds in both directions, an lvalue reference
17742 wins against an rvalue reference and otherwise the template
17743 with the more cv-qualified argument wins for that pairing (if
17744 neither is more cv-qualified, they both are equal). Unlike regular
17745 deduction, after all the arguments have been deduced in this way,
17746 we do *not* verify the deduced template argument values can be
17747 substituted into non-deduced contexts.
17749 The logic can be a bit confusing here, because we look at deduce1 and
17750 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17751 can find template arguments for pat1 to make arg1 look like arg2, that
17752 means that arg2 is at least as specialized as arg1. */
17755 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17757 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17758 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17759 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17760 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17761 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17762 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17763 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17764 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17765 tree origs1
, origs2
;
17766 bool lose1
= false;
17767 bool lose2
= false;
17769 /* Remove the this parameter from non-static member functions. If
17770 one is a non-static member function and the other is not a static
17771 member function, remove the first parameter from that function
17772 also. This situation occurs for operator functions where we
17773 locate both a member function (with this pointer) and non-member
17774 operator (with explicit first operand). */
17775 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17777 len
--; /* LEN is the number of significant arguments for DECL1 */
17778 args1
= TREE_CHAIN (args1
);
17779 if (!DECL_STATIC_FUNCTION_P (decl2
))
17780 args2
= TREE_CHAIN (args2
);
17782 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17784 args2
= TREE_CHAIN (args2
);
17785 if (!DECL_STATIC_FUNCTION_P (decl1
))
17788 args1
= TREE_CHAIN (args1
);
17792 /* If only one is a conversion operator, they are unordered. */
17793 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17796 /* Consider the return type for a conversion function */
17797 if (DECL_CONV_FN_P (decl1
))
17799 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17800 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17804 processing_template_decl
++;
17810 /* Stop when an ellipsis is seen. */
17811 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17813 tree arg1
= TREE_VALUE (args1
);
17814 tree arg2
= TREE_VALUE (args2
);
17815 int deduce1
, deduce2
;
17821 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17822 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17824 /* When both arguments are pack expansions, we need only
17825 unify the patterns themselves. */
17826 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17827 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17829 /* This is the last comparison we need to do. */
17833 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17835 ref1
= TYPE_REF_IS_RVALUE (arg1
) + 1;
17836 arg1
= TREE_TYPE (arg1
);
17837 quals1
= cp_type_quals (arg1
);
17840 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17842 ref2
= TYPE_REF_IS_RVALUE (arg2
) + 1;
17843 arg2
= TREE_TYPE (arg2
);
17844 quals2
= cp_type_quals (arg2
);
17847 arg1
= TYPE_MAIN_VARIANT (arg1
);
17848 arg2
= TYPE_MAIN_VARIANT (arg2
);
17850 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17852 int i
, len2
= list_length (args2
);
17853 tree parmvec
= make_tree_vec (1);
17854 tree argvec
= make_tree_vec (len2
);
17857 /* Setup the parameter vector, which contains only ARG1. */
17858 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17860 /* Setup the argument vector, which contains the remaining
17862 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17863 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17865 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17866 argvec
, DEDUCE_EXACT
,
17867 /*subr=*/true, /*explain_p=*/false)
17870 /* We cannot deduce in the other direction, because ARG1 is
17871 a pack expansion but ARG2 is not. */
17874 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17876 int i
, len1
= list_length (args1
);
17877 tree parmvec
= make_tree_vec (1);
17878 tree argvec
= make_tree_vec (len1
);
17881 /* Setup the parameter vector, which contains only ARG1. */
17882 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17884 /* Setup the argument vector, which contains the remaining
17886 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17887 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17889 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17890 argvec
, DEDUCE_EXACT
,
17891 /*subr=*/true, /*explain_p=*/false)
17894 /* We cannot deduce in the other direction, because ARG2 is
17895 a pack expansion but ARG1 is not.*/
17901 /* The normal case, where neither argument is a pack
17903 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17904 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17906 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17907 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17911 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17912 arg2, then arg2 is not as specialized as arg1. */
17918 /* "If, for a given type, deduction succeeds in both directions
17919 (i.e., the types are identical after the transformations above)
17920 and both P and A were reference types (before being replaced with
17921 the type referred to above):
17922 - if the type from the argument template was an lvalue reference and
17923 the type from the parameter template was not, the argument type is
17924 considered to be more specialized than the other; otherwise,
17925 - if the type from the argument template is more cv-qualified
17926 than the type from the parameter template (as described above),
17927 the argument type is considered to be more specialized than the other;
17929 - neither type is more specialized than the other." */
17931 if (deduce1
&& deduce2
)
17933 if (ref1
&& ref2
&& ref1
!= ref2
)
17940 else if (quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17942 if ((quals1
& quals2
) == quals2
)
17944 if ((quals1
& quals2
) == quals1
)
17949 if (lose1
&& lose2
)
17950 /* We've failed to deduce something in either direction.
17951 These must be unordered. */
17954 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17955 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17956 /* We have already processed all of the arguments in our
17957 handing of the pack expansion type. */
17960 args1
= TREE_CHAIN (args1
);
17961 args2
= TREE_CHAIN (args2
);
17964 /* "In most cases, all template parameters must have values in order for
17965 deduction to succeed, but for partial ordering purposes a template
17966 parameter may remain without a value provided it is not used in the
17967 types being used for partial ordering."
17969 Thus, if we are missing any of the targs1 we need to substitute into
17970 origs1, then pat2 is not as specialized as pat1. This can happen when
17971 there is a nondeduced context. */
17972 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17974 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17977 processing_template_decl
--;
17979 /* All things being equal, if the next argument is a pack expansion
17980 for one function but not for the other, prefer the
17981 non-variadic function. FIXME this is bogus; see c++/41958. */
17983 && args1
&& TREE_VALUE (args1
)
17984 && args2
&& TREE_VALUE (args2
))
17986 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17987 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17990 if (lose1
== lose2
)
17998 /* Determine which of two partial specializations of MAIN_TMPL is more
18001 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18002 to the first partial specialization. The TREE_VALUE is the
18003 innermost set of template parameters for the partial
18004 specialization. PAT2 is similar, but for the second template.
18006 Return 1 if the first partial specialization is more specialized;
18007 -1 if the second is more specialized; 0 if neither is more
18010 See [temp.class.order] for information about determining which of
18011 two templates is more specialized. */
18014 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
18019 bool any_deductions
= false;
18021 tmpl1
= TREE_TYPE (pat1
);
18022 tmpl2
= TREE_TYPE (pat2
);
18024 /* Just like what happens for functions, if we are ordering between
18025 different class template specializations, we may encounter dependent
18026 types in the arguments, and we need our dependency check functions
18027 to behave correctly. */
18028 ++processing_template_decl
;
18029 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
18030 CLASSTYPE_TI_ARGS (tmpl1
),
18031 CLASSTYPE_TI_ARGS (tmpl2
));
18035 any_deductions
= true;
18038 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
18039 CLASSTYPE_TI_ARGS (tmpl2
),
18040 CLASSTYPE_TI_ARGS (tmpl1
));
18044 any_deductions
= true;
18046 --processing_template_decl
;
18048 /* In the case of a tie where at least one of the class templates
18049 has a parameter pack at the end, the template with the most
18050 non-packed parameters wins. */
18053 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
18054 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
18056 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
18057 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
18058 int len1
= TREE_VEC_LENGTH (args1
);
18059 int len2
= TREE_VEC_LENGTH (args2
);
18061 /* We don't count the pack expansion at the end. */
18062 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
18064 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
18069 else if (len1
< len2
)
18076 /* Return the template arguments that will produce the function signature
18077 DECL from the function template FN, with the explicit template
18078 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18079 also match. Return NULL_TREE if no satisfactory arguments could be
18083 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
18085 int ntparms
= DECL_NTPARMS (fn
);
18086 tree targs
= make_tree_vec (ntparms
);
18087 tree decl_type
= TREE_TYPE (decl
);
18088 tree decl_arg_types
;
18090 unsigned int nargs
, ix
;
18093 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
18095 /* Never do unification on the 'this' parameter. */
18096 decl_arg_types
= skip_artificial_parms_for (decl
,
18097 TYPE_ARG_TYPES (decl_type
));
18099 nargs
= list_length (decl_arg_types
);
18100 args
= XALLOCAVEC (tree
, nargs
);
18101 for (arg
= decl_arg_types
, ix
= 0;
18102 arg
!= NULL_TREE
&& arg
!= void_list_node
;
18103 arg
= TREE_CHAIN (arg
), ++ix
)
18104 args
[ix
] = TREE_VALUE (arg
);
18106 if (fn_type_unification (fn
, explicit_args
, targs
,
18108 (check_rettype
|| DECL_CONV_FN_P (fn
)
18109 ? TREE_TYPE (decl_type
) : NULL_TREE
),
18110 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false,
18112 == error_mark_node
)
18118 /* Return the innermost template arguments that, when applied to a partial
18119 specialization of MAIN_TMPL whose innermost template parameters are
18120 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18123 For example, suppose we have:
18125 template <class T, class U> struct S {};
18126 template <class T> struct S<T*, int> {};
18128 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18129 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18130 int}. The resulting vector will be {double}, indicating that `T'
18131 is bound to `double'. */
18134 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
18136 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
18138 tree innermost_deduced_args
;
18140 innermost_deduced_args
= make_tree_vec (ntparms
);
18141 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18143 deduced_args
= copy_node (args
);
18144 SET_TMPL_ARGS_LEVEL (deduced_args
,
18145 TMPL_ARGS_DEPTH (deduced_args
),
18146 innermost_deduced_args
);
18149 deduced_args
= innermost_deduced_args
;
18151 if (unify (tparms
, deduced_args
,
18152 INNERMOST_TEMPLATE_ARGS (spec_args
),
18153 INNERMOST_TEMPLATE_ARGS (args
),
18154 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
18157 for (i
= 0; i
< ntparms
; ++i
)
18158 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
18161 /* Verify that nondeduced template arguments agree with the type
18162 obtained from argument deduction.
18166 struct A { typedef int X; };
18167 template <class T, class U> struct C {};
18168 template <class T> struct C<T, typename T::X> {};
18170 Then with the instantiation `C<A, int>', we can deduce that
18171 `T' is `A' but unify () does not check whether `typename T::X'
18173 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
18174 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
18175 spec_args
, main_tmpl
,
18176 tf_none
, false, false);
18177 if (spec_args
== error_mark_node
18178 /* We only need to check the innermost arguments; the other
18179 arguments will always agree. */
18180 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
18181 INNERMOST_TEMPLATE_ARGS (args
)))
18184 /* Now that we have bindings for all of the template arguments,
18185 ensure that the arguments deduced for the template template
18186 parameters have compatible template parameter lists. See the use
18187 of template_template_parm_bindings_ok_p in fn_type_unification
18188 for more information. */
18189 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
18192 return deduced_args
;
18195 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18196 Return the TREE_LIST node with the most specialized template, if
18197 any. If there is no most specialized template, the error_mark_node
18200 Note that this function does not look at, or modify, the
18201 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18202 returned is one of the elements of INSTANTIATIONS, callers may
18203 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18204 and retrieve it from the value returned. */
18207 most_specialized_instantiation (tree templates
)
18211 ++processing_template_decl
;
18214 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
18218 if (get_bindings (TREE_VALUE (champ
),
18219 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18220 NULL_TREE
, /*check_ret=*/true))
18223 if (get_bindings (TREE_VALUE (fn
),
18224 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18225 NULL_TREE
, /*check_ret=*/true))
18232 /* Equally specialized, move to next function. If there
18233 is no next function, nothing's most specialized. */
18234 fn
= TREE_CHAIN (fn
);
18242 /* Now verify that champ is better than everything earlier in the
18243 instantiation list. */
18244 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
18245 if (get_bindings (TREE_VALUE (champ
),
18246 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
18247 NULL_TREE
, /*check_ret=*/true)
18248 || !get_bindings (TREE_VALUE (fn
),
18249 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
18250 NULL_TREE
, /*check_ret=*/true))
18256 processing_template_decl
--;
18259 return error_mark_node
;
18264 /* If DECL is a specialization of some template, return the most
18265 general such template. Otherwise, returns NULL_TREE.
18267 For example, given:
18269 template <class T> struct S { template <class U> void f(U); };
18271 if TMPL is `template <class U> void S<int>::f(U)' this will return
18272 the full template. This function will not trace past partial
18273 specializations, however. For example, given in addition:
18275 template <class T> struct S<T*> { template <class U> void f(U); };
18277 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18278 `template <class T> template <class U> S<T*>::f(U)'. */
18281 most_general_template (tree decl
)
18283 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18284 an immediate specialization. */
18285 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18287 if (DECL_TEMPLATE_INFO (decl
)) {
18288 decl
= DECL_TI_TEMPLATE (decl
);
18290 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18291 template friend. */
18292 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
18298 /* Look for more and more general templates. */
18299 while (DECL_TEMPLATE_INFO (decl
))
18301 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18302 (See cp-tree.h for details.) */
18303 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
18306 if (CLASS_TYPE_P (TREE_TYPE (decl
))
18307 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
18310 /* Stop if we run into an explicitly specialized class template. */
18311 if (!DECL_NAMESPACE_SCOPE_P (decl
)
18312 && DECL_CONTEXT (decl
)
18313 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
18316 decl
= DECL_TI_TEMPLATE (decl
);
18322 /* Return the most specialized of the class template partial
18323 specializations of TMPL which can produce TYPE, a specialization of
18324 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18325 a _TYPE node corresponding to the partial specialization, while the
18326 TREE_PURPOSE is the set of template arguments that must be
18327 substituted into the TREE_TYPE in order to generate TYPE.
18329 If the choice of partial specialization is ambiguous, a diagnostic
18330 is issued, and the error_mark_node is returned. If there are no
18331 partial specializations of TMPL matching TYPE, then NULL_TREE is
18335 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
18337 tree list
= NULL_TREE
;
18343 tree outer_args
= NULL_TREE
;
18345 tmpl
= most_general_template (tmpl
);
18346 args
= CLASSTYPE_TI_ARGS (type
);
18348 /* For determining which partial specialization to use, only the
18349 innermost args are interesting. */
18350 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
18352 outer_args
= strip_innermost_template_args (args
, 1);
18353 args
= INNERMOST_TEMPLATE_ARGS (args
);
18356 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
18358 tree partial_spec_args
;
18360 tree spec_tmpl
= TREE_VALUE (t
);
18361 tree orig_parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18363 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
18365 ++processing_template_decl
;
18369 /* Discard the outer levels of args, and then substitute in the
18370 template args from the enclosing class. */
18371 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
18372 partial_spec_args
= tsubst_template_args
18373 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
18375 /* And the same for the partial specialization TEMPLATE_DECL. */
18376 spec_tmpl
= tsubst (spec_tmpl
, outer_args
, tf_none
, NULL_TREE
);
18379 partial_spec_args
=
18380 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
18381 add_to_template_args (outer_args
,
18382 partial_spec_args
),
18384 /*require_all_args=*/true,
18385 /*use_default_args=*/true);
18387 --processing_template_decl
;
18389 if (partial_spec_args
== error_mark_node
)
18390 return error_mark_node
;
18391 if (spec_tmpl
== error_mark_node
)
18392 return error_mark_node
;
18394 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl
);
18395 spec_args
= get_class_bindings (tmpl
, parms
,
18401 spec_args
= add_to_template_args (outer_args
, spec_args
);
18402 list
= tree_cons (spec_args
, orig_parms
, list
);
18403 TREE_TYPE (list
) = TREE_TYPE (t
);
18410 ambiguous_p
= false;
18413 t
= TREE_CHAIN (t
);
18414 for (; t
; t
= TREE_CHAIN (t
))
18416 fate
= more_specialized_class (tmpl
, champ
, t
);
18423 t
= TREE_CHAIN (t
);
18426 ambiguous_p
= true;
18435 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
18437 fate
= more_specialized_class (tmpl
, champ
, t
);
18440 ambiguous_p
= true;
18448 char *spaces
= NULL
;
18449 if (!(complain
& tf_error
))
18450 return error_mark_node
;
18451 error ("ambiguous class template instantiation for %q#T", type
);
18452 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
18453 for (t
= list
; t
; t
= TREE_CHAIN (t
))
18455 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
18456 spaces
= spaces
? spaces
: get_spaces (str
);
18459 return error_mark_node
;
18465 /* Explicitly instantiate DECL. */
18468 do_decl_instantiation (tree decl
, tree storage
)
18470 tree result
= NULL_TREE
;
18473 if (!decl
|| decl
== error_mark_node
)
18474 /* An error occurred, for which grokdeclarator has already issued
18475 an appropriate message. */
18477 else if (! DECL_LANG_SPECIFIC (decl
))
18479 error ("explicit instantiation of non-template %q#D", decl
);
18482 else if (VAR_P (decl
))
18484 /* There is an asymmetry here in the way VAR_DECLs and
18485 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18486 the latter, the DECL we get back will be marked as a
18487 template instantiation, and the appropriate
18488 DECL_TEMPLATE_INFO will be set up. This does not happen for
18489 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18490 should handle VAR_DECLs as it currently handles
18492 if (!DECL_CLASS_SCOPE_P (decl
))
18494 error ("%qD is not a static data member of a class template", decl
);
18497 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
18498 if (!result
|| !VAR_P (result
))
18500 error ("no matching template for %qD found", decl
);
18503 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
18505 error ("type %qT for explicit instantiation %qD does not match "
18506 "declared type %qT", TREE_TYPE (result
), decl
,
18511 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
18513 error ("explicit instantiation of %q#D", decl
);
18519 /* Check for various error cases. Note that if the explicit
18520 instantiation is valid the RESULT will currently be marked as an
18521 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18522 until we get here. */
18524 if (DECL_TEMPLATE_SPECIALIZATION (result
))
18526 /* DR 259 [temp.spec].
18528 Both an explicit instantiation and a declaration of an explicit
18529 specialization shall not appear in a program unless the explicit
18530 instantiation follows a declaration of the explicit specialization.
18532 For a given set of template parameters, if an explicit
18533 instantiation of a template appears after a declaration of an
18534 explicit specialization for that template, the explicit
18535 instantiation has no effect. */
18538 else if (DECL_EXPLICIT_INSTANTIATION (result
))
18542 No program shall explicitly instantiate any template more
18545 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18546 the first instantiation was `extern' and the second is not,
18547 and EXTERN_P for the opposite case. */
18548 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
18549 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
18550 /* If an "extern" explicit instantiation follows an ordinary
18551 explicit instantiation, the template is instantiated. */
18555 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
18557 error ("no matching template for %qD found", result
);
18560 else if (!DECL_TEMPLATE_INFO (result
))
18562 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
18566 if (storage
== NULL_TREE
)
18568 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18570 if (!in_system_header
&& (cxx_dialect
== cxx98
))
18571 pedwarn (input_location
, OPT_Wpedantic
,
18572 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18577 error ("storage class %qD applied to template instantiation", storage
);
18579 check_explicit_instantiation_namespace (result
);
18580 mark_decl_instantiated (result
, extern_p
);
18582 instantiate_decl (result
, /*defer_ok=*/1,
18583 /*expl_inst_class_mem_p=*/false);
18587 mark_class_instantiated (tree t
, int extern_p
)
18589 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
18590 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
18591 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
18592 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
18595 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
18596 rest_of_type_compilation (t
, 1);
18600 /* Called from do_type_instantiation through binding_table_foreach to
18601 do recursive instantiation for the type bound in ENTRY. */
18603 bt_instantiate_type_proc (binding_entry entry
, void *data
)
18605 tree storage
= *(tree
*) data
;
18607 if (MAYBE_CLASS_TYPE_P (entry
->type
)
18608 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
18609 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
18612 /* Called from do_type_instantiation to instantiate a member
18613 (a member function or a static member variable) of an
18614 explicitly instantiated class template. */
18616 instantiate_class_member (tree decl
, int extern_p
)
18618 mark_decl_instantiated (decl
, extern_p
);
18620 instantiate_decl (decl
, /*defer_ok=*/1,
18621 /*expl_inst_class_mem_p=*/true);
18624 /* Perform an explicit instantiation of template class T. STORAGE, if
18625 non-null, is the RID for extern, inline or static. COMPLAIN is
18626 nonzero if this is called from the parser, zero if called recursively,
18627 since the standard is unclear (as detailed below). */
18630 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
18635 int previous_instantiation_extern_p
= 0;
18637 if (TREE_CODE (t
) == TYPE_DECL
)
18640 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18643 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18645 error ("explicit instantiation of non-class template %qD", tmpl
);
18647 error ("explicit instantiation of non-template type %qT", t
);
18653 if (!COMPLETE_TYPE_P (t
))
18655 if (complain
& tf_error
)
18656 error ("explicit instantiation of %q#T before definition of template",
18661 if (storage
!= NULL_TREE
)
18663 if (!in_system_header
)
18665 if (storage
== ridpointers
[(int) RID_EXTERN
])
18667 if (cxx_dialect
== cxx98
)
18668 pedwarn (input_location
, OPT_Wpedantic
,
18669 "ISO C++ 1998 forbids the use of %<extern%> on "
18670 "explicit instantiations");
18673 pedwarn (input_location
, OPT_Wpedantic
,
18674 "ISO C++ forbids the use of %qE"
18675 " on explicit instantiations", storage
);
18678 if (storage
== ridpointers
[(int) RID_INLINE
])
18680 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18682 else if (storage
== ridpointers
[(int) RID_STATIC
])
18686 error ("storage class %qD applied to template instantiation",
18692 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18694 /* DR 259 [temp.spec].
18696 Both an explicit instantiation and a declaration of an explicit
18697 specialization shall not appear in a program unless the explicit
18698 instantiation follows a declaration of the explicit specialization.
18700 For a given set of template parameters, if an explicit
18701 instantiation of a template appears after a declaration of an
18702 explicit specialization for that template, the explicit
18703 instantiation has no effect. */
18706 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18710 No program shall explicitly instantiate any template more
18713 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18714 instantiation was `extern'. If EXTERN_P then the second is.
18715 These cases are OK. */
18716 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18718 if (!previous_instantiation_extern_p
&& !extern_p
18719 && (complain
& tf_error
))
18720 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18722 /* If we've already instantiated the template, just return now. */
18723 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18727 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18728 mark_class_instantiated (t
, extern_p
);
18736 /* In contrast to implicit instantiation, where only the
18737 declarations, and not the definitions, of members are
18738 instantiated, we have here:
18742 The explicit instantiation of a class template specialization
18743 implies the instantiation of all of its members not
18744 previously explicitly specialized in the translation unit
18745 containing the explicit instantiation.
18747 Of course, we can't instantiate member template classes, since
18748 we don't have any arguments for them. Note that the standard
18749 is unclear on whether the instantiation of the members are
18750 *explicit* instantiations or not. However, the most natural
18751 interpretation is that it should be an explicit instantiation. */
18754 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18755 if (TREE_CODE (tmp
) == FUNCTION_DECL
18756 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18757 instantiate_class_member (tmp
, extern_p
);
18759 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18760 if (VAR_P (tmp
) && DECL_TEMPLATE_INSTANTIATION (tmp
))
18761 instantiate_class_member (tmp
, extern_p
);
18763 if (CLASSTYPE_NESTED_UTDS (t
))
18764 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18765 bt_instantiate_type_proc
, &storage
);
18769 /* Given a function DECL, which is a specialization of TMPL, modify
18770 DECL to be a re-instantiation of TMPL with the same template
18771 arguments. TMPL should be the template into which tsubst'ing
18772 should occur for DECL, not the most general template.
18774 One reason for doing this is a scenario like this:
18777 void f(const T&, int i);
18779 void g() { f(3, 7); }
18782 void f(const T& t, const int i) { }
18784 Note that when the template is first instantiated, with
18785 instantiate_template, the resulting DECL will have no name for the
18786 first parameter, and the wrong type for the second. So, when we go
18787 to instantiate the DECL, we regenerate it. */
18790 regenerate_decl_from_template (tree decl
, tree tmpl
)
18792 /* The arguments used to instantiate DECL, from the most general
18797 args
= DECL_TI_ARGS (decl
);
18798 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18800 /* Make sure that we can see identifiers, and compute access
18802 push_access_scope (decl
);
18804 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18812 args_depth
= TMPL_ARGS_DEPTH (args
);
18813 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18814 if (args_depth
> parms_depth
)
18815 args
= get_innermost_template_args (args
, parms_depth
);
18817 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18818 args
, tf_error
, NULL_TREE
,
18819 /*defer_ok*/false);
18820 if (specs
&& specs
!= error_mark_node
)
18821 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18824 /* Merge parameter declarations. */
18825 decl_parm
= skip_artificial_parms_for (decl
,
18826 DECL_ARGUMENTS (decl
));
18828 = skip_artificial_parms_for (code_pattern
,
18829 DECL_ARGUMENTS (code_pattern
));
18830 while (decl_parm
&& !DECL_PACK_P (pattern_parm
))
18835 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18836 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18837 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18839 parm_type
= type_decays_to (parm_type
);
18840 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18841 TREE_TYPE (decl_parm
) = parm_type
;
18842 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18843 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18845 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18846 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18848 decl_parm
= DECL_CHAIN (decl_parm
);
18849 pattern_parm
= DECL_CHAIN (pattern_parm
);
18851 /* Merge any parameters that match with the function parameter
18853 if (pattern_parm
&& DECL_PACK_P (pattern_parm
))
18856 tree expanded_types
;
18857 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18858 the parameters in this function parameter pack. */
18859 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18860 args
, tf_error
, NULL_TREE
);
18861 len
= TREE_VEC_LENGTH (expanded_types
);
18862 for (i
= 0; i
< len
; i
++)
18867 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18868 /* Rename the parameter to include the index. */
18869 DECL_NAME (decl_parm
) =
18870 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18871 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18872 parm_type
= type_decays_to (parm_type
);
18873 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18874 TREE_TYPE (decl_parm
) = parm_type
;
18875 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18876 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18878 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18879 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18881 decl_parm
= DECL_CHAIN (decl_parm
);
18884 /* Merge additional specifiers from the CODE_PATTERN. */
18885 if (DECL_DECLARED_INLINE_P (code_pattern
)
18886 && !DECL_DECLARED_INLINE_P (decl
))
18887 DECL_DECLARED_INLINE_P (decl
) = 1;
18889 else if (VAR_P (decl
))
18891 DECL_INITIAL (decl
) =
18892 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18893 tf_error
, DECL_TI_TEMPLATE (decl
),
18894 /*integral_constant_expression_p=*/false);
18895 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18896 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18897 tf_error
, DECL_TI_TEMPLATE (decl
));
18900 gcc_unreachable ();
18902 pop_access_scope (decl
);
18905 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18906 substituted to get DECL. */
18909 template_for_substitution (tree decl
)
18911 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18913 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18914 for the instantiation. This is not always the most general
18915 template. Consider, for example:
18918 struct S { template <class U> void f();
18919 template <> void f<int>(); };
18921 and an instantiation of S<double>::f<int>. We want TD to be the
18922 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18923 while (/* An instantiation cannot have a definition, so we need a
18924 more general template. */
18925 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18926 /* We must also deal with friend templates. Given:
18928 template <class T> struct S {
18929 template <class U> friend void f() {};
18932 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18933 so far as the language is concerned, but that's still
18934 where we get the pattern for the instantiation from. On
18935 other hand, if the definition comes outside the class, say:
18937 template <class T> struct S {
18938 template <class U> friend void f();
18940 template <class U> friend void f() {}
18942 we don't need to look any further. That's what the check for
18943 DECL_INITIAL is for. */
18944 || (TREE_CODE (decl
) == FUNCTION_DECL
18945 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18946 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18948 /* The present template, TD, should not be a definition. If it
18949 were a definition, we should be using it! Note that we
18950 cannot restructure the loop to just keep going until we find
18951 a template with a definition, since that might go too far if
18952 a specialization was declared, but not defined. */
18953 gcc_assert (!VAR_P (decl
)
18954 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18956 /* Fetch the more general template. */
18957 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18963 /* Returns true if we need to instantiate this template instance even if we
18964 know we aren't going to emit it.. */
18967 always_instantiate_p (tree decl
)
18969 /* We always instantiate inline functions so that we can inline them. An
18970 explicit instantiation declaration prohibits implicit instantiation of
18971 non-inline functions. With high levels of optimization, we would
18972 normally inline non-inline functions -- but we're not allowed to do
18973 that for "extern template" functions. Therefore, we check
18974 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18975 return ((TREE_CODE (decl
) == FUNCTION_DECL
18976 && (DECL_DECLARED_INLINE_P (decl
)
18977 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18978 /* And we need to instantiate static data members so that
18979 their initializers are available in integral constant
18982 && decl_maybe_constant_var_p (decl
)));
18985 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18986 instantiate it now, modifying TREE_TYPE (fn). */
18989 maybe_instantiate_noexcept (tree fn
)
18991 tree fntype
, spec
, noex
, clone
;
18993 if (DECL_CLONED_FUNCTION_P (fn
))
18994 fn
= DECL_CLONED_FUNCTION (fn
);
18995 fntype
= TREE_TYPE (fn
);
18996 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18998 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
19001 noex
= TREE_PURPOSE (spec
);
19003 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
19005 if (push_tinst_level (fn
))
19007 push_access_scope (fn
);
19008 push_deferring_access_checks (dk_no_deferred
);
19009 input_location
= DECL_SOURCE_LOCATION (fn
);
19010 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
19011 DEFERRED_NOEXCEPT_ARGS (noex
),
19012 tf_warning_or_error
, fn
,
19013 /*function_p=*/false,
19014 /*integral_constant_expression_p=*/true);
19015 pop_deferring_access_checks ();
19016 pop_access_scope (fn
);
19017 pop_tinst_level ();
19018 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
19019 if (spec
== error_mark_node
)
19020 spec
= noexcept_false_spec
;
19023 spec
= noexcept_false_spec
;
19027 /* This is an implicitly declared function, so NOEX is a list of
19028 other functions to evaluate and merge. */
19030 spec
= noexcept_true_spec
;
19031 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
19033 tree fn
= OVL_CURRENT (elt
);
19035 maybe_instantiate_noexcept (fn
);
19036 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
19037 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
19041 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
19043 FOR_EACH_CLONE (clone
, fn
)
19045 if (TREE_TYPE (clone
) == fntype
)
19046 TREE_TYPE (clone
) = TREE_TYPE (fn
);
19048 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
19052 /* Produce the definition of D, a _DECL generated from a template. If
19053 DEFER_OK is nonzero, then we don't have to actually do the
19054 instantiation now; we just have to do it sometime. Normally it is
19055 an error if this is an explicit instantiation but D is undefined.
19056 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19057 explicitly instantiated class template. */
19060 instantiate_decl (tree d
, int defer_ok
,
19061 bool expl_inst_class_mem_p
)
19063 tree tmpl
= DECL_TI_TEMPLATE (d
);
19070 bool pattern_defined
;
19071 location_t saved_loc
= input_location
;
19072 int saved_unevaluated_operand
= cp_unevaluated_operand
;
19073 int saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19078 /* This function should only be used to instantiate templates for
19079 functions and static member variables. */
19080 gcc_assert (VAR_OR_FUNCTION_DECL_P (d
));
19082 /* Variables are never deferred; if instantiation is required, they
19083 are instantiated right away. That allows for better code in the
19084 case that an expression refers to the value of the variable --
19085 if the variable has a constant value the referring expression can
19086 take advantage of that fact. */
19088 || DECL_DECLARED_CONSTEXPR_P (d
))
19091 /* Don't instantiate cloned functions. Instead, instantiate the
19092 functions they cloned. */
19093 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
19094 d
= DECL_CLONED_FUNCTION (d
);
19096 if (DECL_TEMPLATE_INSTANTIATED (d
)
19097 || (TREE_CODE (d
) == FUNCTION_DECL
19098 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
19099 || DECL_TEMPLATE_SPECIALIZATION (d
))
19100 /* D has already been instantiated or explicitly specialized, so
19101 there's nothing for us to do here.
19103 It might seem reasonable to check whether or not D is an explicit
19104 instantiation, and, if so, stop here. But when an explicit
19105 instantiation is deferred until the end of the compilation,
19106 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19107 the instantiation. */
19110 /* Check to see whether we know that this template will be
19111 instantiated in some other file, as with "extern template"
19113 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
19115 /* In general, we do not instantiate such templates. */
19116 if (external_p
&& !always_instantiate_p (d
))
19119 gen_tmpl
= most_general_template (tmpl
);
19120 gen_args
= DECL_TI_ARGS (d
);
19122 if (tmpl
!= gen_tmpl
)
19123 /* We should already have the extra args. */
19124 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
19125 == TMPL_ARGS_DEPTH (gen_args
));
19126 /* And what's in the hash table should match D. */
19127 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
19128 || spec
== NULL_TREE
);
19130 /* This needs to happen before any tsubsting. */
19131 if (! push_tinst_level (d
))
19134 timevar_push (TV_TEMPLATE_INST
);
19136 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19137 for the instantiation. */
19138 td
= template_for_substitution (d
);
19139 code_pattern
= DECL_TEMPLATE_RESULT (td
);
19141 /* We should never be trying to instantiate a member of a class
19142 template or partial specialization. */
19143 gcc_assert (d
!= code_pattern
);
19145 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
19146 || DECL_TEMPLATE_SPECIALIZATION (td
))
19147 /* In the case of a friend template whose definition is provided
19148 outside the class, we may have too many arguments. Drop the
19149 ones we don't need. The same is true for specializations. */
19150 args
= get_innermost_template_args
19151 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
19155 if (TREE_CODE (d
) == FUNCTION_DECL
)
19156 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
19157 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
19159 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
19161 /* We may be in the middle of deferred access check. Disable it now. */
19162 push_deferring_access_checks (dk_no_deferred
);
19164 /* Unless an explicit instantiation directive has already determined
19165 the linkage of D, remember that a definition is available for
19167 if (pattern_defined
19168 && !DECL_INTERFACE_KNOWN (d
)
19169 && !DECL_NOT_REALLY_EXTERN (d
))
19170 mark_definable (d
);
19172 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
19173 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
19174 input_location
= DECL_SOURCE_LOCATION (d
);
19176 /* If D is a member of an explicitly instantiated class template,
19177 and no definition is available, treat it like an implicit
19179 if (!pattern_defined
&& expl_inst_class_mem_p
19180 && DECL_EXPLICIT_INSTANTIATION (d
))
19182 /* Leave linkage flags alone on instantiations with anonymous
19184 if (TREE_PUBLIC (d
))
19186 DECL_NOT_REALLY_EXTERN (d
) = 0;
19187 DECL_INTERFACE_KNOWN (d
) = 0;
19189 SET_DECL_IMPLICIT_INSTANTIATION (d
);
19192 if (TREE_CODE (d
) == FUNCTION_DECL
)
19193 maybe_instantiate_noexcept (d
);
19195 /* Defer all other templates, unless we have been explicitly
19196 forbidden from doing so. */
19197 if (/* If there is no definition, we cannot instantiate the
19200 /* If it's OK to postpone instantiation, do so. */
19202 /* If this is a static data member that will be defined
19203 elsewhere, we don't want to instantiate the entire data
19204 member, but we do want to instantiate the initializer so that
19205 we can substitute that elsewhere. */
19206 || (external_p
&& VAR_P (d
)))
19208 /* The definition of the static data member is now required so
19209 we must substitute the initializer. */
19211 && !DECL_INITIAL (d
)
19212 && DECL_INITIAL (code_pattern
))
19216 bool const_init
= false;
19218 ns
= decl_namespace_context (d
);
19219 push_nested_namespace (ns
);
19220 push_nested_class (DECL_CONTEXT (d
));
19221 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
19223 tf_warning_or_error
, NULL_TREE
,
19224 /*integral_constant_expression_p=*/false);
19225 /* Make sure the initializer is still constant, in case of
19226 circular dependency (template/instantiate6.C). */
19228 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19229 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
19230 /*asmspec_tree=*/NULL_TREE
,
19231 LOOKUP_ONLYCONVERTING
);
19232 pop_nested_class ();
19233 pop_nested_namespace (ns
);
19236 /* We restore the source position here because it's used by
19237 add_pending_template. */
19238 input_location
= saved_loc
;
19240 if (at_eof
&& !pattern_defined
19241 && DECL_EXPLICIT_INSTANTIATION (d
)
19242 && DECL_NOT_REALLY_EXTERN (d
))
19245 The definition of a non-exported function template, a
19246 non-exported member function template, or a non-exported
19247 member function or static data member of a class template
19248 shall be present in every translation unit in which it is
19249 explicitly instantiated. */
19250 permerror (input_location
, "explicit instantiation of %qD "
19251 "but no definition available", d
);
19253 /* If we're in unevaluated context, we just wanted to get the
19254 constant value; this isn't an odr use, so don't queue
19255 a full instantiation. */
19256 if (cp_unevaluated_operand
!= 0)
19258 /* ??? Historically, we have instantiated inline functions, even
19259 when marked as "extern template". */
19260 if (!(external_p
&& VAR_P (d
)))
19261 add_pending_template (d
);
19264 /* Tell the repository that D is available in this translation unit
19265 -- and see if it is supposed to be instantiated here. */
19266 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
19268 /* In a PCH file, despite the fact that the repository hasn't
19269 requested instantiation in the PCH it is still possible that
19270 an instantiation will be required in a file that includes the
19273 add_pending_template (d
);
19274 /* Instantiate inline functions so that the inliner can do its
19275 job, even though we'll not be emitting a copy of this
19277 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
19281 fn_context
= decl_function_context (d
);
19282 nested
= (current_function_decl
!= NULL_TREE
);
19284 push_to_top_level ();
19288 push_function_context ();
19289 cp_unevaluated_operand
= 0;
19290 c_inhibit_evaluation_warnings
= 0;
19293 /* Mark D as instantiated so that recursive calls to
19294 instantiate_decl do not try to instantiate it again. */
19295 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
19297 /* Regenerate the declaration in case the template has been modified
19298 by a subsequent redeclaration. */
19299 regenerate_decl_from_template (d
, td
);
19301 /* We already set the file and line above. Reset them now in case
19302 they changed as a result of calling regenerate_decl_from_template. */
19303 input_location
= DECL_SOURCE_LOCATION (d
);
19308 bool const_init
= false;
19310 /* Clear out DECL_RTL; whatever was there before may not be right
19311 since we've reset the type of the declaration. */
19312 SET_DECL_RTL (d
, NULL
);
19313 DECL_IN_AGGR_P (d
) = 0;
19315 /* The initializer is placed in DECL_INITIAL by
19316 regenerate_decl_from_template so we don't need to
19317 push/pop_access_scope again here. Pull it out so that
19318 cp_finish_decl can process it. */
19319 init
= DECL_INITIAL (d
);
19320 DECL_INITIAL (d
) = NULL_TREE
;
19321 DECL_INITIALIZED_P (d
) = 0;
19323 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19324 initializer. That function will defer actual emission until
19325 we have a chance to determine linkage. */
19326 DECL_EXTERNAL (d
) = 0;
19328 /* Enter the scope of D so that access-checking works correctly. */
19329 push_nested_class (DECL_CONTEXT (d
));
19330 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
19331 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
19332 pop_nested_class ();
19334 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
19335 synthesize_method (d
);
19336 else if (TREE_CODE (d
) == FUNCTION_DECL
)
19338 struct pointer_map_t
*saved_local_specializations
;
19343 /* Save away the current list, in case we are instantiating one
19344 template from within the body of another. */
19345 saved_local_specializations
= local_specializations
;
19347 /* Set up the list of local specializations. */
19348 local_specializations
= pointer_map_create ();
19350 /* Set up context. */
19351 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
19353 /* Some typedefs referenced from within the template code need to be
19354 access checked at template instantiation time, i.e now. These
19355 types were added to the template at parsing time. Let's get those
19356 and perform the access checks then. */
19357 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
19360 /* Create substitution entries for the parameters. */
19361 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
19362 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
19363 spec_parm
= DECL_ARGUMENTS (d
);
19364 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
19366 register_local_specialization (spec_parm
, tmpl_parm
);
19367 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
19368 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
19370 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
19372 if (!DECL_PACK_P (tmpl_parm
))
19374 register_local_specialization (spec_parm
, tmpl_parm
);
19375 spec_parm
= DECL_CHAIN (spec_parm
);
19379 /* Register the (value) argument pack as a specialization of
19380 TMPL_PARM, then move on. */
19381 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
19382 register_local_specialization (argpack
, tmpl_parm
);
19385 gcc_assert (!spec_parm
);
19387 /* Substitute into the body of the function. */
19388 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
19389 tf_warning_or_error
, tmpl
,
19390 /*integral_constant_expression_p=*/false);
19392 /* Set the current input_location to the end of the function
19393 so that finish_function knows where we are. */
19394 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
19396 /* We don't need the local specializations any more. */
19397 pointer_map_destroy (local_specializations
);
19398 local_specializations
= saved_local_specializations
;
19400 /* Finish the function. */
19401 d
= finish_function (0);
19402 expand_or_defer_fn (d
);
19405 /* We're not deferring instantiation any more. */
19406 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
19409 pop_from_top_level ();
19411 pop_function_context ();
19414 input_location
= saved_loc
;
19415 cp_unevaluated_operand
= saved_unevaluated_operand
;
19416 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19417 pop_deferring_access_checks ();
19418 pop_tinst_level ();
19420 timevar_pop (TV_TEMPLATE_INST
);
19425 /* Run through the list of templates that we wish we could
19426 instantiate, and instantiate any we can. RETRIES is the
19427 number of times we retry pending template instantiation. */
19430 instantiate_pending_templates (int retries
)
19433 location_t saved_loc
= input_location
;
19435 /* Instantiating templates may trigger vtable generation. This in turn
19436 may require further template instantiations. We place a limit here
19437 to avoid infinite loop. */
19438 if (pending_templates
&& retries
>= max_tinst_depth
)
19440 tree decl
= pending_templates
->tinst
->decl
;
19442 error ("template instantiation depth exceeds maximum of %d"
19443 " instantiating %q+D, possibly from virtual table generation"
19444 " (use -ftemplate-depth= to increase the maximum)",
19445 max_tinst_depth
, decl
);
19446 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19447 /* Pretend that we defined it. */
19448 DECL_INITIAL (decl
) = error_mark_node
;
19454 struct pending_template
**t
= &pending_templates
;
19455 struct pending_template
*last
= NULL
;
19459 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
19460 bool complete
= false;
19462 if (TYPE_P (instantiation
))
19466 if (!COMPLETE_TYPE_P (instantiation
))
19468 instantiate_class_template (instantiation
);
19469 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
19470 for (fn
= TYPE_METHODS (instantiation
);
19472 fn
= TREE_CHAIN (fn
))
19473 if (! DECL_ARTIFICIAL (fn
))
19474 instantiate_decl (fn
,
19476 /*expl_inst_class_mem_p=*/false);
19477 if (COMPLETE_TYPE_P (instantiation
))
19481 complete
= COMPLETE_TYPE_P (instantiation
);
19485 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19486 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
19489 = instantiate_decl (instantiation
,
19491 /*expl_inst_class_mem_p=*/false);
19492 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
19496 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
19497 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
19501 /* If INSTANTIATION has been instantiated, then we don't
19502 need to consider it again in the future. */
19510 current_tinst_level
= NULL
;
19512 last_pending_template
= last
;
19514 while (reconsider
);
19516 input_location
= saved_loc
;
19519 /* Substitute ARGVEC into T, which is a list of initializers for
19520 either base class or a non-static data member. The TREE_PURPOSEs
19521 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19522 instantiate_decl. */
19525 tsubst_initializer_list (tree t
, tree argvec
)
19527 tree inits
= NULL_TREE
;
19529 for (; t
; t
= TREE_CHAIN (t
))
19533 tree expanded_bases
= NULL_TREE
;
19534 tree expanded_arguments
= NULL_TREE
;
19537 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
19542 /* Expand the base class expansion type into separate base
19544 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
19545 tf_warning_or_error
,
19547 if (expanded_bases
== error_mark_node
)
19550 /* We'll be building separate TREE_LISTs of arguments for
19552 len
= TREE_VEC_LENGTH (expanded_bases
);
19553 expanded_arguments
= make_tree_vec (len
);
19554 for (i
= 0; i
< len
; i
++)
19555 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
19557 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19558 expand each argument in the TREE_VALUE of t. */
19559 expr
= make_node (EXPR_PACK_EXPANSION
);
19560 PACK_EXPANSION_LOCAL_P (expr
) = true;
19561 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
19562 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
19564 if (TREE_VALUE (t
) == void_type_node
)
19565 /* VOID_TYPE_NODE is used to indicate
19566 value-initialization. */
19568 for (i
= 0; i
< len
; i
++)
19569 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
19573 /* Substitute parameter packs into each argument in the
19575 in_base_initializer
= 1;
19576 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
19578 tree expanded_exprs
;
19580 /* Expand the argument. */
19581 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
19583 = tsubst_pack_expansion (expr
, argvec
,
19584 tf_warning_or_error
,
19586 if (expanded_exprs
== error_mark_node
)
19589 /* Prepend each of the expanded expressions to the
19590 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19591 for (i
= 0; i
< len
; i
++)
19593 TREE_VEC_ELT (expanded_arguments
, i
) =
19594 tree_cons (NULL_TREE
,
19595 TREE_VEC_ELT (expanded_exprs
, i
),
19596 TREE_VEC_ELT (expanded_arguments
, i
));
19599 in_base_initializer
= 0;
19601 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19602 since we built them backwards. */
19603 for (i
= 0; i
< len
; i
++)
19605 TREE_VEC_ELT (expanded_arguments
, i
) =
19606 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
19611 for (i
= 0; i
< len
; ++i
)
19613 if (expanded_bases
)
19615 decl
= TREE_VEC_ELT (expanded_bases
, i
);
19616 decl
= expand_member_init (decl
);
19617 init
= TREE_VEC_ELT (expanded_arguments
, i
);
19622 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
19623 tf_warning_or_error
, NULL_TREE
);
19625 decl
= expand_member_init (decl
);
19626 if (decl
&& !DECL_P (decl
))
19627 in_base_initializer
= 1;
19629 init
= TREE_VALUE (t
);
19631 if (init
!= void_type_node
)
19632 init
= tsubst_expr (init
, argvec
,
19633 tf_warning_or_error
, NULL_TREE
,
19634 /*integral_constant_expression_p=*/false);
19635 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19636 /* If we had an initializer but it instantiated to nothing,
19637 value-initialize the object. This will only occur when
19638 the initializer was a pack expansion where the parameter
19639 packs used in that expansion were of length zero. */
19640 init
= void_type_node
;
19641 in_base_initializer
= 0;
19646 init
= build_tree_list (decl
, init
);
19647 TREE_CHAIN (init
) = inits
;
19655 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19658 set_current_access_from_decl (tree decl
)
19660 if (TREE_PRIVATE (decl
))
19661 current_access_specifier
= access_private_node
;
19662 else if (TREE_PROTECTED (decl
))
19663 current_access_specifier
= access_protected_node
;
19665 current_access_specifier
= access_public_node
;
19668 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19669 is the instantiation (which should have been created with
19670 start_enum) and ARGS are the template arguments to use. */
19673 tsubst_enum (tree tag
, tree newtag
, tree args
)
19677 if (SCOPED_ENUM_P (newtag
))
19678 begin_scope (sk_scoped_enum
, newtag
);
19680 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19685 decl
= TREE_VALUE (e
);
19686 /* Note that in a template enum, the TREE_VALUE is the
19687 CONST_DECL, not the corresponding INTEGER_CST. */
19688 value
= tsubst_expr (DECL_INITIAL (decl
),
19689 args
, tf_warning_or_error
, NULL_TREE
,
19690 /*integral_constant_expression_p=*/true);
19692 /* Give this enumeration constant the correct access. */
19693 set_current_access_from_decl (decl
);
19695 /* Actually build the enumerator itself. */
19697 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19700 if (SCOPED_ENUM_P (newtag
))
19703 finish_enum_value_list (newtag
);
19704 finish_enum (newtag
);
19706 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19707 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19710 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19711 its type -- but without substituting the innermost set of template
19712 arguments. So, innermost set of template parameters will appear in
19716 get_mostly_instantiated_function_type (tree decl
)
19724 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19725 targs
= DECL_TI_ARGS (decl
);
19726 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19727 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19729 /* There should be as many levels of arguments as there are levels
19731 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19733 fn_type
= TREE_TYPE (tmpl
);
19735 if (parm_depth
== 1)
19736 /* No substitution is necessary. */
19743 /* Replace the innermost level of the TARGS with NULL_TREEs to
19744 let tsubst know not to substitute for those parameters. */
19745 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19746 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19747 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19748 TMPL_ARGS_LEVEL (targs
, i
));
19749 SET_TMPL_ARGS_LEVEL (partial_args
,
19750 TMPL_ARGS_DEPTH (targs
),
19751 make_tree_vec (DECL_NTPARMS (tmpl
)));
19753 /* Make sure that we can see identifiers, and compute access
19755 push_access_scope (decl
);
19757 ++processing_template_decl
;
19758 /* Now, do the (partial) substitution to figure out the
19759 appropriate function type. */
19760 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19761 --processing_template_decl
;
19763 /* Substitute into the template parameters to obtain the real
19764 innermost set of parameters. This step is important if the
19765 innermost set of template parameters contains value
19766 parameters whose types depend on outer template parameters. */
19767 TREE_VEC_LENGTH (partial_args
)--;
19768 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19770 pop_access_scope (decl
);
19776 /* Return truthvalue if we're processing a template different from
19777 the last one involved in diagnostics. */
19779 problematic_instantiation_changed (void)
19781 return current_tinst_level
!= last_error_tinst_level
;
19784 /* Remember current template involved in diagnostics. */
19786 record_last_problematic_instantiation (void)
19788 last_error_tinst_level
= current_tinst_level
;
19791 struct tinst_level
*
19792 current_instantiation (void)
19794 return current_tinst_level
;
19797 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19798 type. Return zero for ok, nonzero for disallowed. Issue error and
19799 warning messages under control of COMPLAIN. */
19802 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19804 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19806 else if (POINTER_TYPE_P (type
))
19808 else if (TYPE_PTRMEM_P (type
))
19810 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19812 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19814 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19816 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19819 if (complain
& tf_error
)
19821 if (type
== error_mark_node
)
19822 inform (input_location
, "invalid template non-type parameter");
19824 error ("%q#T is not a valid type for a template non-type parameter",
19830 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19831 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19834 dependent_type_p_r (tree type
)
19840 A type is dependent if it is:
19842 -- a template parameter. Template template parameters are types
19843 for us (since TYPE_P holds true for them) so we handle
19845 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19846 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19848 /* -- a qualified-id with a nested-name-specifier which contains a
19849 class-name that names a dependent type or whose unqualified-id
19850 names a dependent type. */
19851 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19853 /* -- a cv-qualified type where the cv-unqualified type is
19855 type
= TYPE_MAIN_VARIANT (type
);
19856 /* -- a compound type constructed from any dependent type. */
19857 if (TYPE_PTRMEM_P (type
))
19858 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19859 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19861 else if (TYPE_PTR_P (type
)
19862 || TREE_CODE (type
) == REFERENCE_TYPE
)
19863 return dependent_type_p (TREE_TYPE (type
));
19864 else if (TREE_CODE (type
) == FUNCTION_TYPE
19865 || TREE_CODE (type
) == METHOD_TYPE
)
19869 if (dependent_type_p (TREE_TYPE (type
)))
19871 for (arg_type
= TYPE_ARG_TYPES (type
);
19873 arg_type
= TREE_CHAIN (arg_type
))
19874 if (dependent_type_p (TREE_VALUE (arg_type
)))
19878 /* -- an array type constructed from any dependent type or whose
19879 size is specified by a constant expression that is
19882 We checked for type- and value-dependence of the bounds in
19883 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19884 if (TREE_CODE (type
) == ARRAY_TYPE
)
19886 if (TYPE_DOMAIN (type
)
19887 && dependent_type_p (TYPE_DOMAIN (type
)))
19889 return dependent_type_p (TREE_TYPE (type
));
19892 /* -- a template-id in which either the template name is a template
19894 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19896 /* ... or any of the template arguments is a dependent type or
19897 an expression that is type-dependent or value-dependent. */
19898 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19899 && (any_dependent_template_arguments_p
19900 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19903 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19904 dependent; if the argument of the `typeof' expression is not
19905 type-dependent, then it should already been have resolved. */
19906 if (TREE_CODE (type
) == TYPEOF_TYPE
19907 || TREE_CODE (type
) == DECLTYPE_TYPE
19908 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19911 /* A template argument pack is dependent if any of its packed
19913 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19915 tree args
= ARGUMENT_PACK_ARGS (type
);
19916 int i
, len
= TREE_VEC_LENGTH (args
);
19917 for (i
= 0; i
< len
; ++i
)
19918 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19922 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19923 be template parameters. */
19924 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19927 /* The standard does not specifically mention types that are local
19928 to template functions or local classes, but they should be
19929 considered dependent too. For example:
19931 template <int I> void f() {
19936 The size of `E' cannot be known until the value of `I' has been
19937 determined. Therefore, `E' must be considered dependent. */
19938 scope
= TYPE_CONTEXT (type
);
19939 if (scope
&& TYPE_P (scope
))
19940 return dependent_type_p (scope
);
19941 /* Don't use type_dependent_expression_p here, as it can lead
19942 to infinite recursion trying to determine whether a lambda
19943 nested in a lambda is dependent (c++/47687). */
19944 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19945 && DECL_LANG_SPECIFIC (scope
)
19946 && DECL_TEMPLATE_INFO (scope
)
19947 && (any_dependent_template_arguments_p
19948 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19951 /* Other types are non-dependent. */
19955 /* Returns TRUE if TYPE is dependent, in the sense of
19956 [temp.dep.type]. Note that a NULL type is considered dependent. */
19959 dependent_type_p (tree type
)
19961 /* If there are no template parameters in scope, then there can't be
19962 any dependent types. */
19963 if (!processing_template_decl
)
19965 /* If we are not processing a template, then nobody should be
19966 providing us with a dependent type. */
19968 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19972 /* If the type is NULL, we have not computed a type for the entity
19973 in question; in that case, the type is dependent. */
19977 /* Erroneous types can be considered non-dependent. */
19978 if (type
== error_mark_node
)
19981 /* If we have not already computed the appropriate value for TYPE,
19983 if (!TYPE_DEPENDENT_P_VALID (type
))
19985 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19986 TYPE_DEPENDENT_P_VALID (type
) = 1;
19989 return TYPE_DEPENDENT_P (type
);
19992 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19993 lookup. In other words, a dependent type that is not the current
19997 dependent_scope_p (tree scope
)
19999 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
20000 && !currently_open_class (scope
));
20003 /* T is a SCOPE_REF; return whether we need to consider it
20004 instantiation-dependent so that we can check access at instantiation
20005 time even though we know which member it resolves to. */
20008 instantiation_dependent_scope_ref_p (tree t
)
20010 if (DECL_P (TREE_OPERAND (t
, 1))
20011 && CLASS_TYPE_P (TREE_OPERAND (t
, 0))
20012 && accessible_in_template_p (TREE_OPERAND (t
, 0),
20013 TREE_OPERAND (t
, 1)))
20019 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20020 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20023 /* Note that this predicate is not appropriate for general expressions;
20024 only constant expressions (that satisfy potential_constant_expression)
20025 can be tested for value dependence. */
20028 value_dependent_expression_p (tree expression
)
20030 if (!processing_template_decl
)
20033 /* A name declared with a dependent type. */
20034 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
20037 switch (TREE_CODE (expression
))
20039 case IDENTIFIER_NODE
:
20040 /* A name that has not been looked up -- must be dependent. */
20043 case TEMPLATE_PARM_INDEX
:
20044 /* A non-type template parm. */
20048 /* A non-type template parm. */
20049 if (DECL_TEMPLATE_PARM_P (expression
))
20051 return value_dependent_expression_p (DECL_INITIAL (expression
));
20054 /* A constant with literal type and is initialized
20055 with an expression that is value-dependent.
20057 Note that a non-dependent parenthesized initializer will have
20058 already been replaced with its constant value, so if we see
20059 a TREE_LIST it must be dependent. */
20060 if (DECL_INITIAL (expression
)
20061 && decl_constant_var_p (expression
)
20062 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
20063 || value_dependent_expression_p (DECL_INITIAL (expression
))))
20067 case DYNAMIC_CAST_EXPR
:
20068 case STATIC_CAST_EXPR
:
20069 case CONST_CAST_EXPR
:
20070 case REINTERPRET_CAST_EXPR
:
20072 /* These expressions are value-dependent if the type to which
20073 the cast occurs is dependent or the expression being casted
20074 is value-dependent. */
20076 tree type
= TREE_TYPE (expression
);
20078 if (dependent_type_p (type
))
20081 /* A functional cast has a list of operands. */
20082 expression
= TREE_OPERAND (expression
, 0);
20085 /* If there are no operands, it must be an expression such
20086 as "int()". This should not happen for aggregate types
20087 because it would form non-constant expressions. */
20088 gcc_assert (cxx_dialect
>= cxx11
20089 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
20094 if (TREE_CODE (expression
) == TREE_LIST
)
20095 return any_value_dependent_elements_p (expression
);
20097 return value_dependent_expression_p (expression
);
20101 if (SIZEOF_EXPR_TYPE_P (expression
))
20102 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression
, 0)));
20106 /* A `sizeof' expression is value-dependent if the operand is
20107 type-dependent or is a pack expansion. */
20108 expression
= TREE_OPERAND (expression
, 0);
20109 if (PACK_EXPANSION_P (expression
))
20111 else if (TYPE_P (expression
))
20112 return dependent_type_p (expression
);
20113 return instantiation_dependent_expression_p (expression
);
20115 case AT_ENCODE_EXPR
:
20116 /* An 'encode' expression is value-dependent if the operand is
20118 expression
= TREE_OPERAND (expression
, 0);
20119 return dependent_type_p (expression
);
20121 case NOEXCEPT_EXPR
:
20122 expression
= TREE_OPERAND (expression
, 0);
20123 return instantiation_dependent_expression_p (expression
);
20126 /* All instantiation-dependent expressions should also be considered
20127 value-dependent. */
20128 return instantiation_dependent_scope_ref_p (expression
);
20130 case COMPONENT_REF
:
20131 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
20132 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
20134 case NONTYPE_ARGUMENT_PACK
:
20135 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20136 is value-dependent. */
20138 tree values
= ARGUMENT_PACK_ARGS (expression
);
20139 int i
, len
= TREE_VEC_LENGTH (values
);
20141 for (i
= 0; i
< len
; ++i
)
20142 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
20150 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
20151 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
20152 || (type2
? dependent_type_p (type2
) : false));
20156 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20157 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
20160 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20161 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
20165 tree op
= TREE_OPERAND (expression
, 0);
20166 return (value_dependent_expression_p (op
)
20167 || has_value_dependent_address (op
));
20172 tree fn
= get_callee_fndecl (expression
);
20174 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
20176 nargs
= call_expr_nargs (expression
);
20177 for (i
= 0; i
< nargs
; ++i
)
20179 tree op
= CALL_EXPR_ARG (expression
, i
);
20180 /* In a call to a constexpr member function, look through the
20181 implicit ADDR_EXPR on the object argument so that it doesn't
20182 cause the call to be considered value-dependent. We also
20183 look through it in potential_constant_expression. */
20184 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
20185 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
20186 && TREE_CODE (op
) == ADDR_EXPR
)
20187 op
= TREE_OPERAND (op
, 0);
20188 if (value_dependent_expression_p (op
))
20194 case TEMPLATE_ID_EXPR
:
20195 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20197 return type_dependent_expression_p (expression
);
20203 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
20204 if (value_dependent_expression_p (val
))
20210 /* Treat a GNU statement expression as dependent to avoid crashing
20211 under fold_non_dependent_expr; it can't be constant. */
20215 /* A constant expression is value-dependent if any subexpression is
20216 value-dependent. */
20217 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
20219 case tcc_reference
:
20221 case tcc_comparison
:
20223 case tcc_expression
:
20226 int i
, len
= cp_tree_operand_length (expression
);
20228 for (i
= 0; i
< len
; i
++)
20230 tree t
= TREE_OPERAND (expression
, i
);
20232 /* In some cases, some of the operands may be missing.l
20233 (For example, in the case of PREDECREMENT_EXPR, the
20234 amount to increment by may be missing.) That doesn't
20235 make the expression dependent. */
20236 if (t
&& value_dependent_expression_p (t
))
20247 /* The expression is not value-dependent. */
20251 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20252 [temp.dep.expr]. Note that an expression with no type is
20253 considered dependent. Other parts of the compiler arrange for an
20254 expression with type-dependent subexpressions to have no type, so
20255 this function doesn't have to be fully recursive. */
20258 type_dependent_expression_p (tree expression
)
20260 if (!processing_template_decl
)
20263 if (expression
== error_mark_node
)
20266 /* An unresolved name is always dependent. */
20267 if (identifier_p (expression
) || TREE_CODE (expression
) == USING_DECL
)
20270 /* Some expression forms are never type-dependent. */
20271 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
20272 || TREE_CODE (expression
) == SIZEOF_EXPR
20273 || TREE_CODE (expression
) == ALIGNOF_EXPR
20274 || TREE_CODE (expression
) == AT_ENCODE_EXPR
20275 || TREE_CODE (expression
) == NOEXCEPT_EXPR
20276 || TREE_CODE (expression
) == TRAIT_EXPR
20277 || TREE_CODE (expression
) == TYPEID_EXPR
20278 || TREE_CODE (expression
) == DELETE_EXPR
20279 || TREE_CODE (expression
) == VEC_DELETE_EXPR
20280 || TREE_CODE (expression
) == THROW_EXPR
)
20283 /* The types of these expressions depends only on the type to which
20284 the cast occurs. */
20285 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
20286 || TREE_CODE (expression
) == STATIC_CAST_EXPR
20287 || TREE_CODE (expression
) == CONST_CAST_EXPR
20288 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
20289 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
20290 || TREE_CODE (expression
) == CAST_EXPR
)
20291 return dependent_type_p (TREE_TYPE (expression
));
20293 /* The types of these expressions depends only on the type created
20294 by the expression. */
20295 if (TREE_CODE (expression
) == NEW_EXPR
20296 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
20298 /* For NEW_EXPR tree nodes created inside a template, either
20299 the object type itself or a TREE_LIST may appear as the
20301 tree type
= TREE_OPERAND (expression
, 1);
20302 if (TREE_CODE (type
) == TREE_LIST
)
20303 /* This is an array type. We need to check array dimensions
20305 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
20306 || value_dependent_expression_p
20307 (TREE_OPERAND (TREE_VALUE (type
), 1));
20309 return dependent_type_p (type
);
20312 if (TREE_CODE (expression
) == SCOPE_REF
)
20314 tree scope
= TREE_OPERAND (expression
, 0);
20315 tree name
= TREE_OPERAND (expression
, 1);
20317 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20318 contains an identifier associated by name lookup with one or more
20319 declarations declared with a dependent type, or...a
20320 nested-name-specifier or qualified-id that names a member of an
20321 unknown specialization. */
20322 return (type_dependent_expression_p (name
)
20323 || dependent_scope_p (scope
));
20326 if (TREE_CODE (expression
) == FUNCTION_DECL
20327 && DECL_LANG_SPECIFIC (expression
)
20328 && DECL_TEMPLATE_INFO (expression
)
20329 && (any_dependent_template_arguments_p
20330 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
20333 if (TREE_CODE (expression
) == TEMPLATE_DECL
20334 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
20337 if (TREE_CODE (expression
) == STMT_EXPR
)
20338 expression
= stmt_expr_value_expr (expression
);
20340 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
20345 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
20347 if (type_dependent_expression_p (elt
))
20353 /* A static data member of the current instantiation with incomplete
20354 array type is type-dependent, as the definition and specializations
20355 can have different bounds. */
20356 if (VAR_P (expression
)
20357 && DECL_CLASS_SCOPE_P (expression
)
20358 && dependent_type_p (DECL_CONTEXT (expression
))
20359 && VAR_HAD_UNKNOWN_BOUND (expression
))
20362 /* An array of unknown bound depending on a variadic parameter, eg:
20364 template<typename... Args>
20365 void foo (Args... args)
20367 int arr[] = { args... };
20370 template<int... vals>
20373 int arr[] = { vals... };
20376 If the array has no length and has an initializer, it must be that
20377 we couldn't determine its length in cp_complete_array_type because
20378 it is dependent. */
20379 if (VAR_P (expression
)
20380 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
20381 && !TYPE_DOMAIN (TREE_TYPE (expression
))
20382 && DECL_INITIAL (expression
))
20385 if (TREE_TYPE (expression
) == unknown_type_node
)
20387 if (TREE_CODE (expression
) == ADDR_EXPR
)
20388 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
20389 if (TREE_CODE (expression
) == COMPONENT_REF
20390 || TREE_CODE (expression
) == OFFSET_REF
)
20392 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
20394 expression
= TREE_OPERAND (expression
, 1);
20395 if (identifier_p (expression
))
20398 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20399 if (TREE_CODE (expression
) == SCOPE_REF
)
20402 if (BASELINK_P (expression
))
20403 expression
= BASELINK_FUNCTIONS (expression
);
20405 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
20407 if (any_dependent_template_arguments_p
20408 (TREE_OPERAND (expression
, 1)))
20410 expression
= TREE_OPERAND (expression
, 0);
20412 gcc_assert (TREE_CODE (expression
) == OVERLOAD
20413 || TREE_CODE (expression
) == FUNCTION_DECL
);
20417 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
20419 expression
= OVL_NEXT (expression
);
20424 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
20426 return (dependent_type_p (TREE_TYPE (expression
)));
20429 /* walk_tree callback function for instantiation_dependent_expression_p,
20430 below. Returns non-zero if a dependent subexpression is found. */
20433 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
20438 /* We don't have to worry about decltype currently because decltype
20439 of an instantiation-dependent expr is a dependent type. This
20440 might change depending on the resolution of DR 1172. */
20441 *walk_subtrees
= false;
20444 enum tree_code code
= TREE_CODE (*tp
);
20447 /* Don't treat an argument list as dependent just because it has no
20455 /* A constant with a dependent initializer is dependent. */
20456 if (value_dependent_expression_p (*tp
))
20460 case TEMPLATE_PARM_INDEX
:
20463 /* Handle expressions with type operands. */
20467 case AT_ENCODE_EXPR
:
20469 tree op
= TREE_OPERAND (*tp
, 0);
20470 if (code
== SIZEOF_EXPR
&& SIZEOF_EXPR_TYPE_P (*tp
))
20471 op
= TREE_TYPE (op
);
20474 if (dependent_type_p (op
))
20478 *walk_subtrees
= false;
20486 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp
))
20487 || (TRAIT_EXPR_TYPE2 (*tp
)
20488 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp
))))
20490 *walk_subtrees
= false;
20493 case COMPONENT_REF
:
20494 if (identifier_p (TREE_OPERAND (*tp
, 1)))
20495 /* In a template, finish_class_member_access_expr creates a
20496 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20497 type-dependent, so that we can check access control at
20498 instantiation time (PR 42277). See also Core issue 1273. */
20503 if (instantiation_dependent_scope_ref_p (*tp
))
20508 /* Treat statement-expressions as dependent. */
20516 if (type_dependent_expression_p (*tp
))
20522 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20523 sense defined by the ABI:
20525 "An expression is instantiation-dependent if it is type-dependent
20526 or value-dependent, or it has a subexpression that is type-dependent
20527 or value-dependent." */
20530 instantiation_dependent_expression_p (tree expression
)
20534 if (!processing_template_decl
)
20537 if (expression
== error_mark_node
)
20540 result
= cp_walk_tree_without_duplicates (&expression
,
20541 instantiation_dependent_r
, NULL
);
20542 return result
!= NULL_TREE
;
20545 /* Like type_dependent_expression_p, but it also works while not processing
20546 a template definition, i.e. during substitution or mangling. */
20549 type_dependent_expression_p_push (tree expr
)
20552 ++processing_template_decl
;
20553 b
= type_dependent_expression_p (expr
);
20554 --processing_template_decl
;
20558 /* Returns TRUE if ARGS contains a type-dependent expression. */
20561 any_type_dependent_arguments_p (const vec
<tree
, va_gc
> *args
)
20566 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
20568 if (type_dependent_expression_p (arg
))
20574 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20575 expressions) contains any type-dependent expressions. */
20578 any_type_dependent_elements_p (const_tree list
)
20580 for (; list
; list
= TREE_CHAIN (list
))
20581 if (type_dependent_expression_p (TREE_VALUE (list
)))
20587 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20588 expressions) contains any value-dependent expressions. */
20591 any_value_dependent_elements_p (const_tree list
)
20593 for (; list
; list
= TREE_CHAIN (list
))
20594 if (value_dependent_expression_p (TREE_VALUE (list
)))
20600 /* Returns TRUE if the ARG (a template argument) is dependent. */
20603 dependent_template_arg_p (tree arg
)
20605 if (!processing_template_decl
)
20608 /* Assume a template argument that was wrongly written by the user
20609 is dependent. This is consistent with what
20610 any_dependent_template_arguments_p [that calls this function]
20612 if (!arg
|| arg
== error_mark_node
)
20615 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
20616 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
20618 if (TREE_CODE (arg
) == TEMPLATE_DECL
20619 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
20620 return dependent_template_p (arg
);
20621 else if (ARGUMENT_PACK_P (arg
))
20623 tree args
= ARGUMENT_PACK_ARGS (arg
);
20624 int i
, len
= TREE_VEC_LENGTH (args
);
20625 for (i
= 0; i
< len
; ++i
)
20627 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
20633 else if (TYPE_P (arg
))
20634 return dependent_type_p (arg
);
20636 return (type_dependent_expression_p (arg
)
20637 || value_dependent_expression_p (arg
));
20640 /* Returns true if ARGS (a collection of template arguments) contains
20641 any types that require structural equality testing. */
20644 any_template_arguments_need_structural_equality_p (tree args
)
20651 if (args
== error_mark_node
)
20654 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20656 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20657 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20659 tree arg
= TREE_VEC_ELT (level
, j
);
20660 tree packed_args
= NULL_TREE
;
20663 if (ARGUMENT_PACK_P (arg
))
20665 /* Look inside the argument pack. */
20666 packed_args
= ARGUMENT_PACK_ARGS (arg
);
20667 len
= TREE_VEC_LENGTH (packed_args
);
20670 for (k
= 0; k
< len
; ++k
)
20673 arg
= TREE_VEC_ELT (packed_args
, k
);
20675 if (error_operand_p (arg
))
20677 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
20679 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
20681 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
20682 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
20691 /* Returns true if ARGS (a collection of template arguments) contains
20692 any dependent arguments. */
20695 any_dependent_template_arguments_p (const_tree args
)
20702 if (args
== error_mark_node
)
20705 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
20707 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
20708 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
20709 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
20716 /* Returns TRUE if the template TMPL is dependent. */
20719 dependent_template_p (tree tmpl
)
20721 if (TREE_CODE (tmpl
) == OVERLOAD
)
20725 if (dependent_template_p (OVL_CURRENT (tmpl
)))
20727 tmpl
= OVL_NEXT (tmpl
);
20732 /* Template template parameters are dependent. */
20733 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
20734 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
20736 /* So are names that have not been looked up. */
20737 if (TREE_CODE (tmpl
) == SCOPE_REF
|| identifier_p (tmpl
))
20739 /* So are member templates of dependent classes. */
20740 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
20741 return dependent_type_p (DECL_CONTEXT (tmpl
));
20745 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
20748 dependent_template_id_p (tree tmpl
, tree args
)
20750 return (dependent_template_p (tmpl
)
20751 || any_dependent_template_arguments_p (args
));
20754 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20758 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
20762 if (!processing_template_decl
)
20765 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
20767 tree decl
= TREE_VEC_ELT (declv
, i
);
20768 tree init
= TREE_VEC_ELT (initv
, i
);
20769 tree cond
= TREE_VEC_ELT (condv
, i
);
20770 tree incr
= TREE_VEC_ELT (incrv
, i
);
20772 if (type_dependent_expression_p (decl
))
20775 if (init
&& type_dependent_expression_p (init
))
20778 if (type_dependent_expression_p (cond
))
20781 if (COMPARISON_CLASS_P (cond
)
20782 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20783 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20786 if (TREE_CODE (incr
) == MODOP_EXPR
)
20788 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20789 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20792 else if (type_dependent_expression_p (incr
))
20794 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20796 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20798 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20800 tree t
= TREE_OPERAND (incr
, 1);
20801 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20802 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20811 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20812 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20813 no such TYPE can be found. Note that this function peers inside
20814 uninstantiated templates and therefore should be used only in
20815 extremely limited situations. ONLY_CURRENT_P restricts this
20816 peering to the currently open classes hierarchy (which is required
20817 when comparing types). */
20820 resolve_typename_type (tree type
, bool only_current_p
)
20829 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20831 scope
= TYPE_CONTEXT (type
);
20832 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20833 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20834 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20835 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20836 identifier of the TYPENAME_TYPE anymore.
20837 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20838 TYPENAME_TYPE instead, we avoid messing up with a possible
20839 typedef variant case. */
20840 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20842 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20843 it first before we can figure out what NAME refers to. */
20844 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20846 if (TYPENAME_IS_RESOLVING_P (scope
))
20847 /* Given a class template A with a dependent base with nested type C,
20848 typedef typename A::C::C C will land us here, as trying to resolve
20849 the initial A::C leads to the local C typedef, which leads back to
20850 A::C::C. So we break the recursion now. */
20853 scope
= resolve_typename_type (scope
, only_current_p
);
20855 /* If we don't know what SCOPE refers to, then we cannot resolve the
20857 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20859 /* If the SCOPE is a template type parameter, we have no way of
20860 resolving the name. */
20861 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20863 /* If the SCOPE is not the current instantiation, there's no reason
20864 to look inside it. */
20865 if (only_current_p
&& !currently_open_class (scope
))
20867 /* If this is a typedef, we don't want to look inside (c++/11987). */
20868 if (typedef_variant_p (type
))
20870 /* If SCOPE isn't the template itself, it will not have a valid
20871 TYPE_FIELDS list. */
20872 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20873 /* scope is either the template itself or a compatible instantiation
20874 like X<T>, so look up the name in the original template. */
20875 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20877 /* scope is a partial instantiation, so we can't do the lookup or we
20878 will lose the template arguments. */
20880 /* Enter the SCOPE so that name lookup will be resolved as if we
20881 were in the class definition. In particular, SCOPE will no
20882 longer be considered a dependent type. */
20883 pushed_scope
= push_scope (scope
);
20884 /* Look up the declaration. */
20885 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20886 tf_warning_or_error
);
20888 result
= NULL_TREE
;
20890 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20891 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20894 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type
))
20895 && TREE_CODE (decl
) == TYPE_DECL
)
20897 result
= TREE_TYPE (decl
);
20898 if (result
== error_mark_node
)
20899 result
= NULL_TREE
;
20901 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20902 && DECL_CLASS_TEMPLATE_P (decl
))
20906 /* Obtain the template and the arguments. */
20907 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20908 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20909 /* Instantiate the template. */
20910 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20911 /*entering_scope=*/0,
20912 tf_error
| tf_user
);
20913 if (result
== error_mark_node
)
20914 result
= NULL_TREE
;
20917 /* Leave the SCOPE. */
20919 pop_scope (pushed_scope
);
20921 /* If we failed to resolve it, return the original typename. */
20925 /* If lookup found a typename type, resolve that too. */
20926 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20928 /* Ill-formed programs can cause infinite recursion here, so we
20929 must catch that. */
20930 TYPENAME_IS_RESOLVING_P (type
) = 1;
20931 result
= resolve_typename_type (result
, only_current_p
);
20932 TYPENAME_IS_RESOLVING_P (type
) = 0;
20935 /* Qualify the resulting type. */
20936 quals
= cp_type_quals (type
);
20938 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20943 /* EXPR is an expression which is not type-dependent. Return a proxy
20944 for EXPR that can be used to compute the types of larger
20945 expressions containing EXPR. */
20948 build_non_dependent_expr (tree expr
)
20952 #ifdef ENABLE_CHECKING
20953 /* Try to get a constant value for all non-dependent expressions in
20954 order to expose bugs in *_dependent_expression_p and constexpr. */
20955 if (cxx_dialect
>= cxx11
)
20956 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20959 /* Preserve OVERLOADs; the functions must be available to resolve
20962 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20963 inner_expr
= stmt_expr_value_expr (inner_expr
);
20964 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20965 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20966 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20967 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20968 if (is_overloaded_fn (inner_expr
)
20969 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20971 /* There is no need to return a proxy for a variable. */
20974 /* Preserve string constants; conversions from string constants to
20975 "char *" are allowed, even though normally a "const char *"
20976 cannot be used to initialize a "char *". */
20977 if (TREE_CODE (expr
) == STRING_CST
)
20979 /* Preserve arithmetic constants, as an optimization -- there is no
20980 reason to create a new node. */
20981 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20983 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20984 There is at least one place where we want to know that a
20985 particular expression is a throw-expression: when checking a ?:
20986 expression, there are special rules if the second or third
20987 argument is a throw-expression. */
20988 if (TREE_CODE (expr
) == THROW_EXPR
)
20991 /* Don't wrap an initializer list, we need to be able to look inside. */
20992 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20995 /* Don't wrap a dummy object, we need to be able to test for it. */
20996 if (is_dummy_object (expr
))
20999 if (TREE_CODE (expr
) == COND_EXPR
)
21000 return build3 (COND_EXPR
,
21002 TREE_OPERAND (expr
, 0),
21003 (TREE_OPERAND (expr
, 1)
21004 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
21005 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
21006 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
21007 if (TREE_CODE (expr
) == COMPOUND_EXPR
21008 && !COMPOUND_EXPR_OVERLOADED (expr
))
21009 return build2 (COMPOUND_EXPR
,
21011 TREE_OPERAND (expr
, 0),
21012 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
21014 /* If the type is unknown, it can't really be non-dependent */
21015 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
21017 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21018 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
21021 /* ARGS is a vector of expressions as arguments to a function call.
21022 Replace the arguments with equivalent non-dependent expressions.
21023 This modifies ARGS in place. */
21026 make_args_non_dependent (vec
<tree
, va_gc
> *args
)
21031 FOR_EACH_VEC_SAFE_ELT (args
, ix
, arg
)
21033 tree newarg
= build_non_dependent_expr (arg
);
21035 (*args
)[ix
] = newarg
;
21039 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21040 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21044 make_auto_1 (tree name
)
21046 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
21047 TYPE_NAME (au
) = build_decl (input_location
,
21048 TYPE_DECL
, name
, au
);
21049 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
21050 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
21051 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
21052 TYPE_NAME (au
), NULL_TREE
);
21053 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
21054 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
21055 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
21061 make_decltype_auto (void)
21063 return make_auto_1 (get_identifier ("decltype(auto)"));
21069 return make_auto_1 (get_identifier ("auto"));
21072 /* Given type ARG, return std::initializer_list<ARG>. */
21077 tree std_init_list
= namespace_binding
21078 (get_identifier ("initializer_list"), std_node
);
21080 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
21082 error ("deducing from brace-enclosed initializer list requires "
21083 "#include <initializer_list>");
21084 return error_mark_node
;
21086 argvec
= make_tree_vec (1);
21087 TREE_VEC_ELT (argvec
, 0) = arg
;
21088 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
21089 NULL_TREE
, 0, tf_warning_or_error
);
21092 /* Replace auto in TYPE with std::initializer_list<auto>. */
21095 listify_autos (tree type
, tree auto_node
)
21097 tree init_auto
= listify (auto_node
);
21098 tree argvec
= make_tree_vec (1);
21099 TREE_VEC_ELT (argvec
, 0) = init_auto
;
21100 if (processing_template_decl
)
21101 argvec
= add_to_template_args (current_template_args (), argvec
);
21102 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21105 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21106 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21109 do_auto_deduction (tree type
, tree init
, tree auto_node
)
21113 if (init
== error_mark_node
)
21114 return error_mark_node
;
21116 if (type_dependent_expression_p (init
))
21117 /* Defining a subset of type-dependent expressions that we can deduce
21118 from ahead of time isn't worth the trouble. */
21121 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21122 with either a new invented type template parameter U or, if the
21123 initializer is a braced-init-list (8.5.4), with
21124 std::initializer_list<U>. */
21125 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
21126 type
= listify_autos (type
, auto_node
);
21128 init
= resolve_nondeduced_context (init
);
21130 targs
= make_tree_vec (1);
21131 if (AUTO_IS_DECLTYPE (auto_node
))
21133 bool id
= (DECL_P (init
) || TREE_CODE (init
) == COMPONENT_REF
);
21134 TREE_VEC_ELT (targs
, 0)
21135 = finish_decltype_type (init
, id
, tf_warning_or_error
);
21136 if (type
!= auto_node
)
21138 error ("%qT as type rather than plain %<decltype(auto)%>", type
);
21139 return error_mark_node
;
21144 tree parms
= build_tree_list (NULL_TREE
, type
);
21145 tree tparms
= make_tree_vec (1);
21148 TREE_VEC_ELT (tparms
, 0)
21149 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
21150 val
= type_unification_real (tparms
, targs
, parms
, &init
, 1, 0,
21151 DEDUCE_CALL
, LOOKUP_NORMAL
,
21152 NULL
, /*explain_p=*/false);
21155 if (processing_template_decl
)
21156 /* Try again at instantiation time. */
21158 if (type
&& type
!= error_mark_node
)
21159 /* If type is error_mark_node a diagnostic must have been
21160 emitted by now. Also, having a mention to '<type error>'
21161 in the diagnostic is not really useful to the user. */
21163 if (cfun
&& auto_node
== current_function_auto_return_pattern
21164 && LAMBDA_FUNCTION_P (current_function_decl
))
21165 error ("unable to deduce lambda return type from %qE", init
);
21167 error ("unable to deduce %qT from %qE", type
, init
);
21169 return error_mark_node
;
21173 /* If the list of declarators contains more than one declarator, the type
21174 of each declared variable is determined as described above. If the
21175 type deduced for the template parameter U is not the same in each
21176 deduction, the program is ill-formed. */
21177 if (TREE_TYPE (auto_node
)
21178 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
21180 if (cfun
&& auto_node
== current_function_auto_return_pattern
21181 && LAMBDA_FUNCTION_P (current_function_decl
))
21182 error ("inconsistent types %qT and %qT deduced for "
21183 "lambda return type", TREE_TYPE (auto_node
),
21184 TREE_VEC_ELT (targs
, 0));
21186 error ("inconsistent deduction for %qT: %qT and then %qT",
21187 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
21188 return error_mark_node
;
21190 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
21192 if (processing_template_decl
)
21193 targs
= add_to_template_args (current_template_args (), targs
);
21194 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
21197 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21201 splice_late_return_type (tree type
, tree late_return_type
)
21205 if (late_return_type
== NULL_TREE
)
21207 argvec
= make_tree_vec (1);
21208 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
21209 if (processing_template_parmlist
)
21210 /* For a late-specified return type in a template type-parameter, we
21211 need to add a dummy argument level for its parmlist. */
21212 argvec
= add_to_template_args
21213 (make_tree_vec (processing_template_parmlist
), argvec
);
21214 if (current_template_parms
)
21215 argvec
= add_to_template_args (current_template_args (), argvec
);
21216 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
21219 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21220 'decltype(auto)'. */
21223 is_auto (const_tree type
)
21225 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
21226 && (TYPE_IDENTIFIER (type
) == get_identifier ("auto")
21227 || TYPE_IDENTIFIER (type
) == get_identifier ("decltype(auto)")))
21233 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21234 a use of `auto'. Returns NULL_TREE otherwise. */
21237 type_uses_auto (tree type
)
21239 return find_type_usage (type
, is_auto
);
21242 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21243 'decltype(auto)' or a concept. */
21246 is_auto_or_concept (const_tree type
)
21248 return is_auto (type
); // or concept
21251 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21252 a concept identifier) iff TYPE contains a use of a generic type. Returns
21253 NULL_TREE otherwise. */
21256 type_uses_auto_or_concept (tree type
)
21258 return find_type_usage (type
, is_auto_or_concept
);
21262 /* For a given template T, return the vector of typedefs referenced
21263 in T for which access check is needed at T instantiation time.
21264 T is either a FUNCTION_DECL or a RECORD_TYPE.
21265 Those typedefs were added to T by the function
21266 append_type_to_template_for_access_check. */
21268 vec
<qualified_typedef_usage_t
, va_gc
> *
21269 get_types_needing_access_check (tree t
)
21272 vec
<qualified_typedef_usage_t
, va_gc
> *result
= NULL
;
21274 if (!t
|| t
== error_mark_node
)
21277 if (!(ti
= get_template_info (t
)))
21280 if (CLASS_TYPE_P (t
)
21281 || TREE_CODE (t
) == FUNCTION_DECL
)
21283 if (!TI_TEMPLATE (ti
))
21286 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
21292 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21293 tied to T. That list of typedefs will be access checked at
21294 T instantiation time.
21295 T is either a FUNCTION_DECL or a RECORD_TYPE.
21296 TYPE_DECL is a TYPE_DECL node representing a typedef.
21297 SCOPE is the scope through which TYPE_DECL is accessed.
21298 LOCATION is the location of the usage point of TYPE_DECL.
21300 This function is a subroutine of
21301 append_type_to_template_for_access_check. */
21304 append_type_to_template_for_access_check_1 (tree t
,
21307 location_t location
)
21309 qualified_typedef_usage_t typedef_usage
;
21312 if (!t
|| t
== error_mark_node
)
21315 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
21316 || CLASS_TYPE_P (t
))
21318 && TREE_CODE (type_decl
) == TYPE_DECL
21321 if (!(ti
= get_template_info (t
)))
21324 gcc_assert (TI_TEMPLATE (ti
));
21326 typedef_usage
.typedef_decl
= type_decl
;
21327 typedef_usage
.context
= scope
;
21328 typedef_usage
.locus
= location
;
21330 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
), typedef_usage
);
21333 /* Append TYPE_DECL to the template TEMPL.
21334 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21335 At TEMPL instanciation time, TYPE_DECL will be checked to see
21336 if it can be accessed through SCOPE.
21337 LOCATION is the location of the usage point of TYPE_DECL.
21339 e.g. consider the following code snippet:
21346 template<class U> struct S
21348 C::myint mi; // <-- usage point of the typedef C::myint
21353 At S<char> instantiation time, we need to check the access of C::myint
21354 In other words, we need to check the access of the myint typedef through
21355 the C scope. For that purpose, this function will add the myint typedef
21356 and the scope C through which its being accessed to a list of typedefs
21357 tied to the template S. That list will be walked at template instantiation
21358 time and access check performed on each typedefs it contains.
21359 Note that this particular code snippet should yield an error because
21360 myint is private to C. */
21363 append_type_to_template_for_access_check (tree templ
,
21366 location_t location
)
21368 qualified_typedef_usage_t
*iter
;
21371 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
21373 /* Make sure we don't append the type to the template twice. */
21374 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ
), i
, iter
)
21375 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
21378 append_type_to_template_for_access_check_1 (templ
, type_decl
,
21382 /* Set up the hash tables for template instantiations. */
21385 init_template_processing (void)
21387 decl_specializations
= htab_create_ggc (37,
21388 hash_specialization
,
21389 eq_specializations
,
21391 type_specializations
= htab_create_ggc (37,
21392 hash_specialization
,
21393 eq_specializations
,
21397 /* Print stats about the template hash tables for -fstats. */
21400 print_template_statistics (void)
21402 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
21403 "%f collisions\n", (long) htab_size (decl_specializations
),
21404 (long) htab_elements (decl_specializations
),
21405 htab_collisions (decl_specializations
));
21406 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
21407 "%f collisions\n", (long) htab_size (type_specializations
),
21408 (long) htab_elements (type_specializations
),
21409 htab_collisions (type_specializations
));
21412 #include "gt-cp-pt.h"