1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
30 #include "coretypes.h"
34 #include "pointer-set.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct pending_template
GTY (()) {
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations
;
84 /* Contains canonical template parameter types. The vector is indexed by
85 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
86 TREE_LIST, whose TREE_VALUEs contain the canonical template
87 parameters of various types and levels. */
88 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
90 #define UNIFY_ALLOW_NONE 0
91 #define UNIFY_ALLOW_MORE_CV_QUAL 1
92 #define UNIFY_ALLOW_LESS_CV_QUAL 2
93 #define UNIFY_ALLOW_DERIVED 4
94 #define UNIFY_ALLOW_INTEGER 8
95 #define UNIFY_ALLOW_OUTER_LEVEL 16
96 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
97 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
99 static void push_access_scope (tree
);
100 static void pop_access_scope (tree
);
101 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
102 unification_kind_t
, int);
103 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
104 unification_kind_t
, int, bool);
105 static int unify (tree
, tree
, tree
, tree
, int);
106 static void add_pending_template (tree
);
107 static int push_tinst_level (tree
);
108 static void pop_tinst_level (void);
109 static tree
reopen_tinst_level (struct tinst_level
*);
110 static tree
tsubst_initializer_list (tree
, tree
);
111 static tree
get_class_bindings (tree
, tree
, tree
);
112 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
114 static void tsubst_enum (tree
, tree
, tree
);
115 static tree
add_to_template_args (tree
, tree
);
116 static tree
add_outermost_template_args (tree
, tree
);
117 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
118 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
120 static int type_unification_real (tree
, tree
, tree
, tree
,
121 int, unification_kind_t
, int);
122 static void note_template_header (int);
123 static tree
convert_nontype_argument_function (tree
, tree
);
124 static tree
convert_nontype_argument (tree
, tree
);
125 static tree
convert_template_argument (tree
, tree
, tree
,
126 tsubst_flags_t
, int, tree
);
127 static int for_each_template_parm (tree
, tree_fn_t
, void*,
128 struct pointer_set_t
*, bool);
129 static tree
expand_template_argument_pack (tree
);
130 static tree
build_template_parm_index (int, int, int, tree
, tree
);
131 static bool inline_needs_template_parms (tree
);
132 static void push_inline_template_parms_recursive (tree
, int);
133 static tree
retrieve_local_specialization (tree
);
134 static void register_local_specialization (tree
, tree
);
135 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
136 static int mark_template_parm (tree
, void *);
137 static int template_parm_this_level_p (tree
, void *);
138 static tree
tsubst_friend_function (tree
, tree
);
139 static tree
tsubst_friend_class (tree
, tree
);
140 static int can_complete_type_without_circularity (tree
);
141 static tree
get_bindings (tree
, tree
, tree
, bool);
142 static int template_decl_level (tree
);
143 static int check_cv_quals_for_unify (int, tree
, tree
);
144 static void template_parm_level_and_index (tree
, int*, int*);
145 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
146 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
147 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
148 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
149 static void regenerate_decl_from_template (tree
, tree
);
150 static tree
most_specialized_class (tree
, tree
);
151 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
152 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
153 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
154 static bool check_specialization_scope (void);
155 static tree
process_partial_specialization (tree
);
156 static void set_current_access_from_decl (tree
);
157 static tree
get_template_base (tree
, tree
, tree
, tree
);
158 static tree
try_class_unification (tree
, tree
, tree
, tree
);
159 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
161 static bool template_template_parm_bindings_ok_p (tree
, tree
);
162 static int template_args_equal (tree
, tree
);
163 static void tsubst_default_arguments (tree
);
164 static tree
for_each_template_parm_r (tree
*, int *, void *);
165 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
166 static void copy_default_args_to_explicit_spec (tree
);
167 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
168 static int eq_local_specializations (const void *, const void *);
169 static bool dependent_template_arg_p (tree
);
170 static bool any_template_arguments_need_structural_equality_p (tree
);
171 static bool dependent_type_p_r (tree
);
172 static tree
tsubst (tree
, tree
, tsubst_flags_t
, tree
);
173 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
174 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
175 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
177 /* Make the current scope suitable for access checking when we are
178 processing T. T can be FUNCTION_DECL for instantiated function
179 template, or VAR_DECL for static member variable (need by
180 instantiate_decl). */
183 push_access_scope (tree t
)
185 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
186 || TREE_CODE (t
) == VAR_DECL
);
188 if (DECL_FRIEND_CONTEXT (t
))
189 push_nested_class (DECL_FRIEND_CONTEXT (t
));
190 else if (DECL_CLASS_SCOPE_P (t
))
191 push_nested_class (DECL_CONTEXT (t
));
193 push_to_top_level ();
195 if (TREE_CODE (t
) == FUNCTION_DECL
)
197 saved_access_scope
= tree_cons
198 (NULL_TREE
, current_function_decl
, saved_access_scope
);
199 current_function_decl
= t
;
203 /* Restore the scope set up by push_access_scope. T is the node we
207 pop_access_scope (tree t
)
209 if (TREE_CODE (t
) == FUNCTION_DECL
)
211 current_function_decl
= TREE_VALUE (saved_access_scope
);
212 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
215 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
218 pop_from_top_level ();
221 /* Do any processing required when DECL (a member template
222 declaration) is finished. Returns the TEMPLATE_DECL corresponding
223 to DECL, unless it is a specialization, in which case the DECL
224 itself is returned. */
227 finish_member_template_decl (tree decl
)
229 if (decl
== error_mark_node
)
230 return error_mark_node
;
232 gcc_assert (DECL_P (decl
));
234 if (TREE_CODE (decl
) == TYPE_DECL
)
238 type
= TREE_TYPE (decl
);
239 if (IS_AGGR_TYPE (type
)
240 && CLASSTYPE_TEMPLATE_INFO (type
)
241 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
243 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
244 check_member_template (tmpl
);
249 else if (TREE_CODE (decl
) == FIELD_DECL
)
250 error ("data member %qD cannot be a member template", decl
);
251 else if (DECL_TEMPLATE_INFO (decl
))
253 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
255 check_member_template (DECL_TI_TEMPLATE (decl
));
256 return DECL_TI_TEMPLATE (decl
);
262 error ("invalid member template declaration %qD", decl
);
264 return error_mark_node
;
267 /* Return the template info node corresponding to T, whatever T is. */
270 get_template_info (tree t
)
272 tree tinfo
= NULL_TREE
;
274 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
275 tinfo
= DECL_TEMPLATE_INFO (t
);
277 if (!tinfo
&& TREE_CODE (t
) == TYPE_DECL
)
280 if (TAGGED_TYPE_P (t
))
281 tinfo
= TYPE_TEMPLATE_INFO (t
);
286 /* Returns the template nesting level of the indicated class TYPE.
296 A<T>::B<U> has depth two, while A<T> has depth one.
297 Both A<T>::B<int> and A<int>::B<U> have depth one, if
298 they are instantiations, not specializations.
300 This function is guaranteed to return 0 if passed NULL_TREE so
301 that, for example, `template_class_depth (current_class_type)' is
305 template_class_depth (tree type
)
310 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
311 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
312 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
314 tree tinfo
= get_template_info (type
);
316 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
317 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
324 /* Subroutine of maybe_begin_member_template_processing.
325 Returns true if processing DECL needs us to push template parms. */
328 inline_needs_template_parms (tree decl
)
330 if (! DECL_TEMPLATE_INFO (decl
))
333 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
334 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
337 /* Subroutine of maybe_begin_member_template_processing.
338 Push the template parms in PARMS, starting from LEVELS steps into the
339 chain, and ending at the beginning, since template parms are listed
343 push_inline_template_parms_recursive (tree parmlist
, int levels
)
345 tree parms
= TREE_VALUE (parmlist
);
349 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
351 ++processing_template_decl
;
352 current_template_parms
353 = tree_cons (size_int (processing_template_decl
),
354 parms
, current_template_parms
);
355 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
357 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
359 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
361 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
363 if (parm
== error_mark_node
)
366 gcc_assert (DECL_P (parm
));
368 switch (TREE_CODE (parm
))
377 /* Make a CONST_DECL as is done in process_template_parm.
378 It is ugly that we recreate this here; the original
379 version built in process_template_parm is no longer
381 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
383 DECL_ARTIFICIAL (decl
) = 1;
384 TREE_CONSTANT (decl
) = 1;
385 TREE_INVARIANT (decl
) = 1;
386 TREE_READONLY (decl
) = 1;
387 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
388 SET_DECL_TEMPLATE_PARM_P (decl
);
399 /* Restore the template parameter context for a member template or
400 a friend template defined in a class definition. */
403 maybe_begin_member_template_processing (tree decl
)
408 if (inline_needs_template_parms (decl
))
410 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
411 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
413 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
416 parms
= TREE_CHAIN (parms
);
419 push_inline_template_parms_recursive (parms
, levels
);
422 /* Remember how many levels of template parameters we pushed so that
423 we can pop them later. */
424 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
427 /* Undo the effects of maybe_begin_member_template_processing. */
430 maybe_end_member_template_processing (void)
435 if (VEC_length (int, inline_parm_levels
) == 0)
438 last
= VEC_pop (int, inline_parm_levels
);
439 for (i
= 0; i
< last
; ++i
)
441 --processing_template_decl
;
442 current_template_parms
= TREE_CHAIN (current_template_parms
);
447 /* Return a new template argument vector which contains all of ARGS,
448 but has as its innermost set of arguments the EXTRA_ARGS. */
451 add_to_template_args (tree args
, tree extra_args
)
458 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
459 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
461 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
462 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
464 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
465 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
470 /* Like add_to_template_args, but only the outermost ARGS are added to
471 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
472 (EXTRA_ARGS) levels are added. This function is used to combine
473 the template arguments from a partial instantiation with the
474 template arguments used to attain the full instantiation from the
475 partial instantiation. */
478 add_outermost_template_args (tree args
, tree extra_args
)
482 /* If there are more levels of EXTRA_ARGS than there are ARGS,
483 something very fishy is going on. */
484 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
486 /* If *all* the new arguments will be the EXTRA_ARGS, just return
488 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
491 /* For the moment, we make ARGS look like it contains fewer levels. */
492 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
494 new_args
= add_to_template_args (args
, extra_args
);
496 /* Now, we restore ARGS to its full dimensions. */
497 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
502 /* Return the N levels of innermost template arguments from the ARGS. */
505 get_innermost_template_args (tree args
, int n
)
513 /* If N is 1, just return the innermost set of template arguments. */
515 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
517 /* If we're not removing anything, just return the arguments we were
519 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
520 gcc_assert (extra_levels
>= 0);
521 if (extra_levels
== 0)
524 /* Make a new set of arguments, not containing the outer arguments. */
525 new_args
= make_tree_vec (n
);
526 for (i
= 1; i
<= n
; ++i
)
527 SET_TMPL_ARGS_LEVEL (new_args
, i
,
528 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
533 /* The inverse of get_innermost_template_args: Return all but the innermost
534 EXTRA_LEVELS levels of template arguments from the ARGS. */
537 strip_innermost_template_args (tree args
, int extra_levels
)
540 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
545 /* If N is 1, just return the outermost set of template arguments. */
547 return TMPL_ARGS_LEVEL (args
, 1);
549 /* If we're not removing anything, just return the arguments we were
551 gcc_assert (extra_levels
>= 0);
552 if (extra_levels
== 0)
555 /* Make a new set of arguments, not containing the inner arguments. */
556 new_args
= make_tree_vec (n
);
557 for (i
= 1; i
<= n
; ++i
)
558 SET_TMPL_ARGS_LEVEL (new_args
, i
,
559 TMPL_ARGS_LEVEL (args
, i
));
564 /* We've got a template header coming up; push to a new level for storing
568 begin_template_parm_list (void)
570 /* We use a non-tag-transparent scope here, which causes pushtag to
571 put tags in this scope, rather than in the enclosing class or
572 namespace scope. This is the right thing, since we want
573 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
574 global template class, push_template_decl handles putting the
575 TEMPLATE_DECL into top-level scope. For a nested template class,
578 template <class T> struct S1 {
579 template <class T> struct S2 {};
582 pushtag contains special code to call pushdecl_with_scope on the
583 TEMPLATE_DECL for S2. */
584 begin_scope (sk_template_parms
, NULL
);
585 ++processing_template_decl
;
586 ++processing_template_parmlist
;
587 note_template_header (0);
590 /* This routine is called when a specialization is declared. If it is
591 invalid to declare a specialization here, an error is reported and
592 false is returned, otherwise this routine will return true. */
595 check_specialization_scope (void)
597 tree scope
= current_scope ();
601 An explicit specialization shall be declared in the namespace of
602 which the template is a member, or, for member templates, in the
603 namespace of which the enclosing class or enclosing class
604 template is a member. An explicit specialization of a member
605 function, member class or static data member of a class template
606 shall be declared in the namespace of which the class template
608 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
610 error ("explicit specialization in non-namespace scope %qD", scope
);
616 In an explicit specialization declaration for a member of a class
617 template or a member template that appears in namespace scope,
618 the member template and some of its enclosing class templates may
619 remain unspecialized, except that the declaration shall not
620 explicitly specialize a class member template if its enclosing
621 class templates are not explicitly specialized as well. */
622 if (current_template_parms
)
624 error ("enclosing class templates are not explicitly specialized");
631 /* We've just seen template <>. */
634 begin_specialization (void)
636 begin_scope (sk_template_spec
, NULL
);
637 note_template_header (1);
638 return check_specialization_scope ();
641 /* Called at then end of processing a declaration preceded by
645 end_specialization (void)
648 reset_specialization ();
651 /* Any template <>'s that we have seen thus far are not referring to a
652 function specialization. */
655 reset_specialization (void)
657 processing_specialization
= 0;
658 template_header_count
= 0;
661 /* We've just seen a template header. If SPECIALIZATION is nonzero,
662 it was of the form template <>. */
665 note_template_header (int specialization
)
667 processing_specialization
= specialization
;
668 template_header_count
++;
671 /* We're beginning an explicit instantiation. */
674 begin_explicit_instantiation (void)
676 gcc_assert (!processing_explicit_instantiation
);
677 processing_explicit_instantiation
= true;
682 end_explicit_instantiation (void)
684 gcc_assert (processing_explicit_instantiation
);
685 processing_explicit_instantiation
= false;
688 /* An explicit specialization or partial specialization TMPL is being
689 declared. Check that the namespace in which the specialization is
690 occurring is permissible. Returns false iff it is invalid to
691 specialize TMPL in the current namespace. */
694 check_specialization_namespace (tree tmpl
)
696 tree tpl_ns
= decl_namespace_context (tmpl
);
700 An explicit specialization shall be declared in the namespace of
701 which the template is a member, or, for member templates, in the
702 namespace of which the enclosing class or enclosing class
703 template is a member. An explicit specialization of a member
704 function, member class or static data member of a class template
705 shall be declared in the namespace of which the class template is
707 if (is_associated_namespace (current_namespace
, tpl_ns
))
708 /* Same or super-using namespace. */
712 pedwarn ("specialization of %qD in different namespace", tmpl
);
713 pedwarn (" from definition of %q+#D", tmpl
);
718 /* SPEC is an explicit instantiation. Check that it is valid to
719 perform this explicit instantiation in the current namespace. */
722 check_explicit_instantiation_namespace (tree spec
)
726 /* DR 275: An explicit instantiation shall appear in an enclosing
727 namespace of its template. */
728 ns
= decl_namespace_context (spec
);
729 if (!is_ancestor (current_namespace
, ns
))
730 pedwarn ("explicit instantiation of %qD in namespace %qD "
731 "(which does not enclose namespace %qD)",
732 spec
, current_namespace
, ns
);
735 /* The TYPE is being declared. If it is a template type, that means it
736 is a partial specialization. Do appropriate error-checking. */
739 maybe_process_partial_specialization (tree type
)
743 if (type
== error_mark_node
)
744 return error_mark_node
;
746 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
748 error ("name of class shadows template template parameter %qD",
750 return error_mark_node
;
753 context
= TYPE_CONTEXT (type
);
755 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
757 /* This is for ordinary explicit specialization and partial
758 specialization of a template class such as:
760 template <> class C<int>;
764 template <class T> class C<T*>;
766 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
768 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
769 && !COMPLETE_TYPE_P (type
))
771 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
772 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
773 if (processing_template_decl
)
774 push_template_decl (TYPE_MAIN_DECL (type
));
776 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
777 error ("specialization of %qT after instantiation", type
);
779 else if (CLASS_TYPE_P (type
)
780 && !CLASSTYPE_USE_TEMPLATE (type
)
781 && CLASSTYPE_TEMPLATE_INFO (type
)
782 && context
&& CLASS_TYPE_P (context
)
783 && CLASSTYPE_TEMPLATE_INFO (context
))
785 /* This is for an explicit specialization of member class
786 template according to [temp.expl.spec/18]:
788 template <> template <class U> class C<int>::D;
790 The context `C<int>' must be an implicit instantiation.
791 Otherwise this is just a member class template declared
794 template <> class C<int> { template <class U> class D; };
795 template <> template <class U> class C<int>::D;
797 In the first case, `C<int>::D' is a specialization of `C<T>::D'
798 while in the second case, `C<int>::D' is a primary template
799 and `C<T>::D' may not exist. */
801 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
802 && !COMPLETE_TYPE_P (type
))
806 if (current_namespace
807 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
809 pedwarn ("specializing %q#T in different namespace", type
);
810 pedwarn (" from definition of %q+#D",
811 CLASSTYPE_TI_TEMPLATE (type
));
814 /* Check for invalid specialization after instantiation:
816 template <> template <> class C<int>::D<int>;
817 template <> template <class U> class C<int>::D; */
819 for (t
= DECL_TEMPLATE_INSTANTIATIONS
820 (most_general_template (CLASSTYPE_TI_TEMPLATE (type
)));
821 t
; t
= TREE_CHAIN (t
))
822 if (TREE_VALUE (t
) != type
823 && TYPE_CONTEXT (TREE_VALUE (t
)) == context
)
824 error ("specialization %qT after instantiation %qT",
825 type
, TREE_VALUE (t
));
827 /* Mark TYPE as a specialization. And as a result, we only
828 have one level of template argument for the innermost
830 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
831 CLASSTYPE_TI_ARGS (type
)
832 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
835 else if (processing_specialization
)
837 error ("explicit specialization of non-template %qT", type
);
838 return error_mark_node
;
844 /* Returns nonzero if we can optimize the retrieval of specializations
845 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
846 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
849 optimize_specialization_lookup_p (tree tmpl
)
851 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
852 && DECL_CLASS_SCOPE_P (tmpl
)
853 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
855 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
856 /* The optimized lookup depends on the fact that the
857 template arguments for the member function template apply
858 purely to the containing class, which is not true if the
859 containing class is an explicit or partial
861 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
862 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
863 && !DECL_CONV_FN_P (tmpl
)
864 /* It is possible to have a template that is not a member
865 template and is not a member of a template class:
867 template <typename T>
868 struct S { friend A::f(); };
870 Here, the friend function is a template, but the context does
871 not have template information. The optimized lookup relies
872 on having ARGS be the template arguments for both the class
873 and the function template. */
874 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
877 /* Retrieve the specialization (in the sense of [temp.spec] - a
878 specialization is either an instantiation or an explicit
879 specialization) of TMPL for the given template ARGS. If there is
880 no such specialization, return NULL_TREE. The ARGS are a vector of
881 arguments, or a vector of vectors of arguments, in the case of
882 templates with more than one level of parameters.
884 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
885 then we search for a partial specialization matching ARGS. This
886 parameter is ignored if TMPL is not a class template. */
889 retrieve_specialization (tree tmpl
, tree args
,
890 bool class_specializations_p
)
892 if (args
== error_mark_node
)
895 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
897 /* There should be as many levels of arguments as there are
898 levels of parameters. */
899 gcc_assert (TMPL_ARGS_DEPTH (args
)
900 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
902 if (optimize_specialization_lookup_p (tmpl
))
905 tree class_specialization
;
906 VEC(tree
,gc
) *methods
;
910 /* The template arguments actually apply to the containing
911 class. Find the class specialization with those
913 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
915 = retrieve_specialization (class_template
, args
,
916 /*class_specializations_p=*/false);
917 if (!class_specialization
)
919 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
920 for the specialization. */
921 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
924 /* Iterate through the methods with the indicated name, looking
925 for the one that has an instance of TMPL. */
926 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
927 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
929 tree fn
= OVL_CURRENT (fns
);
930 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
)
940 /* Class templates store their instantiations on the
941 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
942 DECL_TEMPLATE_SPECIALIZATIONS list. */
943 if (!class_specializations_p
944 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl
)) == TYPE_DECL
945 && TAGGED_TYPE_P (TREE_TYPE (tmpl
)))
946 sp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
948 sp
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
950 /* Iterate through the list until we find a matching template. */
951 while (*sp
!= NULL_TREE
)
955 if (comp_template_args (TREE_PURPOSE (spec
), args
))
957 /* Use the move-to-front heuristic to speed up future
961 *sp
= TREE_CHAIN (*sp
);
962 TREE_CHAIN (spec
) = *head
;
965 return TREE_VALUE (spec
);
967 sp
= &TREE_CHAIN (spec
);
974 /* Like retrieve_specialization, but for local declarations. */
977 retrieve_local_specialization (tree tmpl
)
981 if (local_specializations
== NULL
)
984 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
985 htab_hash_pointer (tmpl
));
986 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
989 /* Returns nonzero iff DECL is a specialization of TMPL. */
992 is_specialization_of (tree decl
, tree tmpl
)
996 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1000 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1006 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1008 for (t
= TREE_TYPE (decl
);
1010 t
= CLASSTYPE_USE_TEMPLATE (t
)
1011 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1012 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1019 /* Returns nonzero iff DECL is a specialization of friend declaration
1020 FRIEND according to [temp.friend]. */
1023 is_specialization_of_friend (tree decl
, tree
friend)
1025 bool need_template
= true;
1028 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1029 || TREE_CODE (decl
) == TYPE_DECL
);
1031 /* For [temp.friend/6] when FRIEND is an ordinary member function
1032 of a template class, we want to check if DECL is a specialization
1034 if (TREE_CODE (friend) == FUNCTION_DECL
1035 && DECL_TEMPLATE_INFO (friend)
1036 && !DECL_USE_TEMPLATE (friend))
1038 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1039 friend = DECL_TI_TEMPLATE (friend);
1040 need_template
= false;
1042 else if (TREE_CODE (friend) == TEMPLATE_DECL
1043 && !PRIMARY_TEMPLATE_P (friend))
1044 need_template
= false;
1046 /* There is nothing to do if this is not a template friend. */
1047 if (TREE_CODE (friend) != TEMPLATE_DECL
)
1050 if (is_specialization_of (decl
, friend))
1054 A member of a class template may be declared to be a friend of a
1055 non-template class. In this case, the corresponding member of
1056 every specialization of the class template is a friend of the
1057 class granting friendship.
1059 For example, given a template friend declaration
1061 template <class T> friend void A<T>::f();
1063 the member function below is considered a friend
1065 template <> struct A<int> {
1069 For this type of template friend, TEMPLATE_DEPTH below will be
1070 nonzero. To determine if DECL is a friend of FRIEND, we first
1071 check if the enclosing class is a specialization of another. */
1073 template_depth
= template_class_depth (DECL_CONTEXT (friend));
1075 && DECL_CLASS_SCOPE_P (decl
)
1076 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1077 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1079 /* Next, we check the members themselves. In order to handle
1080 a few tricky cases, such as when FRIEND's are
1082 template <class T> friend void A<T>::g(T t);
1083 template <class T> template <T t> friend void A<T>::h();
1087 void A<int>::g(int);
1088 template <int> void A<int>::h();
1090 we need to figure out ARGS, the template arguments from
1091 the context of DECL. This is required for template substitution
1092 of `T' in the function parameter of `g' and template parameter
1093 of `h' in the above examples. Here ARGS corresponds to `int'. */
1095 tree context
= DECL_CONTEXT (decl
);
1096 tree args
= NULL_TREE
;
1097 int current_depth
= 0;
1099 while (current_depth
< template_depth
)
1101 if (CLASSTYPE_TEMPLATE_INFO (context
))
1103 if (current_depth
== 0)
1104 args
= TYPE_TI_ARGS (context
);
1106 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1109 context
= TYPE_CONTEXT (context
);
1112 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1117 tree friend_args_type
;
1118 tree decl_args_type
;
1120 /* Make sure that both DECL and FRIEND are templates or
1122 is_template
= DECL_TEMPLATE_INFO (decl
)
1123 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1124 if (need_template
^ is_template
)
1126 else if (is_template
)
1128 /* If both are templates, check template parameter list. */
1130 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1132 if (!comp_template_parms
1133 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1137 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1140 decl_type
= TREE_TYPE (decl
);
1142 friend_type
= tsubst_function_type (TREE_TYPE (friend), args
,
1143 tf_none
, NULL_TREE
);
1144 if (friend_type
== error_mark_node
)
1147 /* Check if return types match. */
1148 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1151 /* Check if function parameter types match, ignoring the
1152 `this' parameter. */
1153 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1154 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1155 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1156 friend_args_type
= TREE_CHAIN (friend_args_type
);
1157 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1158 decl_args_type
= TREE_CHAIN (decl_args_type
);
1160 return compparms (decl_args_type
, friend_args_type
);
1164 /* DECL is a TYPE_DECL */
1166 tree decl_type
= TREE_TYPE (decl
);
1168 /* Make sure that both DECL and FRIEND are templates or
1171 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1172 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1174 if (need_template
^ is_template
)
1176 else if (is_template
)
1179 /* If both are templates, check the name of the two
1180 TEMPLATE_DECL's first because is_friend didn't. */
1181 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1182 != DECL_NAME (friend))
1185 /* Now check template parameter list. */
1187 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1189 return comp_template_parms
1190 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1194 return (DECL_NAME (decl
)
1195 == DECL_NAME (friend));
1201 /* Register the specialization SPEC as a specialization of TMPL with
1202 the indicated ARGS. IS_FRIEND indicates whether the specialization
1203 is actually just a friend declaration. Returns SPEC, or an
1204 equivalent prior declaration, if available. */
1207 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
)
1211 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1213 if (TREE_CODE (spec
) == FUNCTION_DECL
1214 && uses_template_parms (DECL_TI_ARGS (spec
)))
1215 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1216 register it; we want the corresponding TEMPLATE_DECL instead.
1217 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1218 the more obvious `uses_template_parms (spec)' to avoid problems
1219 with default function arguments. In particular, given
1220 something like this:
1222 template <class T> void f(T t1, T t = T())
1224 the default argument expression is not substituted for in an
1225 instantiation unless and until it is actually needed. */
1228 fn
= retrieve_specialization (tmpl
, args
,
1229 /*class_specializations_p=*/false);
1230 /* We can sometimes try to re-register a specialization that we've
1231 already got. In particular, regenerate_decl_from_template calls
1232 duplicate_decls which will update the specialization list. But,
1233 we'll still get called again here anyhow. It's more convenient
1234 to simply allow this than to try to prevent it. */
1237 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1239 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1242 || DECL_EXPLICIT_INSTANTIATION (fn
))
1244 error ("specialization of %qD after instantiation",
1246 return error_mark_node
;
1251 /* This situation should occur only if the first
1252 specialization is an implicit instantiation, the
1253 second is an explicit specialization, and the
1254 implicit instantiation has not yet been used. That
1255 situation can occur if we have implicitly
1256 instantiated a member function and then specialized
1259 We can also wind up here if a friend declaration that
1260 looked like an instantiation turns out to be a
1263 template <class T> void foo(T);
1264 class S { friend void foo<>(int) };
1265 template <> void foo(int);
1267 We transform the existing DECL in place so that any
1268 pointers to it become pointers to the updated
1271 If there was a definition for the template, but not
1272 for the specialization, we want this to look as if
1273 there were no definition, and vice versa. */
1274 DECL_INITIAL (fn
) = NULL_TREE
;
1275 duplicate_decls (spec
, fn
, is_friend
);
1276 /* The call to duplicate_decls will have applied
1279 An explicit specialization of a function template
1280 is inline only if it is explicitly declared to be,
1281 and independently of whether its function template
1284 to the primary function; now copy the inline bits to
1285 the various clones. */
1286 FOR_EACH_CLONE (clone
, fn
)
1288 DECL_DECLARED_INLINE_P (clone
)
1289 = DECL_DECLARED_INLINE_P (fn
);
1293 check_specialization_namespace (fn
);
1298 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1300 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1301 /* Dup decl failed, but this is a new definition. Set the
1302 line number so any errors match this new
1304 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1310 /* A specialization must be declared in the same namespace as the
1311 template it is specializing. */
1312 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1313 && !check_specialization_namespace (tmpl
))
1314 DECL_CONTEXT (spec
) = FROB_CONTEXT (decl_namespace_context (tmpl
));
1316 if (!optimize_specialization_lookup_p (tmpl
))
1317 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
1318 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
1323 /* Unregister the specialization SPEC as a specialization of TMPL.
1324 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1325 if the SPEC was listed as a specialization of TMPL. */
1328 reregister_specialization (tree spec
, tree tmpl
, tree new_spec
)
1332 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
1334 s
= &TREE_CHAIN (*s
))
1335 if (TREE_VALUE (*s
) == spec
)
1338 *s
= TREE_CHAIN (*s
);
1340 TREE_VALUE (*s
) = new_spec
;
1347 /* Compare an entry in the local specializations hash table P1 (which
1348 is really a pointer to a TREE_LIST) with P2 (which is really a
1352 eq_local_specializations (const void *p1
, const void *p2
)
1354 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1357 /* Hash P1, an entry in the local specializations table. */
1360 hash_local_specialization (const void* p1
)
1362 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1365 /* Like register_specialization, but for local declarations. We are
1366 registering SPEC, an instantiation of TMPL. */
1369 register_local_specialization (tree spec
, tree tmpl
)
1373 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1374 htab_hash_pointer (tmpl
), INSERT
);
1375 *slot
= build_tree_list (spec
, tmpl
);
1378 /* TYPE is a class type. Returns true if TYPE is an explicitly
1379 specialized class. */
1382 explicit_class_specialization_p (tree type
)
1384 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1386 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1389 /* Print the list of candidate FNS in an error message. */
1392 print_candidates (tree fns
)
1396 const char *str
= "candidates are:";
1398 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
1402 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
1403 error ("%s %+#D", str
, OVL_CURRENT (f
));
1408 /* Returns the template (one of the functions given by TEMPLATE_ID)
1409 which can be specialized to match the indicated DECL with the
1410 explicit template args given in TEMPLATE_ID. The DECL may be
1411 NULL_TREE if none is available. In that case, the functions in
1412 TEMPLATE_ID are non-members.
1414 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1415 specialization of a member template.
1417 The TEMPLATE_COUNT is the number of references to qualifying
1418 template classes that appeared in the name of the function. See
1419 check_explicit_specialization for a more accurate description.
1421 TSK indicates what kind of template declaration (if any) is being
1422 declared. TSK_TEMPLATE indicates that the declaration given by
1423 DECL, though a FUNCTION_DECL, has template parameters, and is
1424 therefore a template function.
1426 The template args (those explicitly specified and those deduced)
1427 are output in a newly created vector *TARGS_OUT.
1429 If it is impossible to determine the result, an error message is
1430 issued. The error_mark_node is returned to indicate failure. */
1433 determine_specialization (tree template_id
,
1436 int need_member_template
,
1442 tree explicit_targs
;
1443 tree candidates
= NULL_TREE
;
1444 /* A TREE_LIST of templates of which DECL may be a specialization.
1445 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1446 corresponding TREE_PURPOSE is the set of template arguments that,
1447 when used to instantiate the template, would produce a function
1448 with the signature of DECL. */
1449 tree templates
= NULL_TREE
;
1451 struct cp_binding_level
*b
;
1453 *targs_out
= NULL_TREE
;
1455 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1456 return error_mark_node
;
1458 fns
= TREE_OPERAND (template_id
, 0);
1459 explicit_targs
= TREE_OPERAND (template_id
, 1);
1461 if (fns
== error_mark_node
)
1462 return error_mark_node
;
1464 /* Check for baselinks. */
1465 if (BASELINK_P (fns
))
1466 fns
= BASELINK_FUNCTIONS (fns
);
1468 if (!is_overloaded_fn (fns
))
1470 error ("%qD is not a function template", fns
);
1471 return error_mark_node
;
1474 /* Count the number of template headers specified for this
1477 for (b
= current_binding_level
;
1478 b
->kind
== sk_template_parms
;
1482 for (; fns
; fns
= OVL_NEXT (fns
))
1484 tree fn
= OVL_CURRENT (fns
);
1486 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1488 tree decl_arg_types
;
1491 /* In case of explicit specialization, we need to check if
1492 the number of template headers appearing in the specialization
1493 is correct. This is usually done in check_explicit_specialization,
1494 but the check done there cannot be exhaustive when specializing
1495 member functions. Consider the following code:
1497 template <> void A<int>::f(int);
1498 template <> template <> void A<int>::f(int);
1500 Assuming that A<int> is not itself an explicit specialization
1501 already, the first line specializes "f" which is a non-template
1502 member function, whilst the second line specializes "f" which
1503 is a template member function. So both lines are syntactically
1504 correct, and check_explicit_specialization does not reject
1507 Here, we can do better, as we are matching the specialization
1508 against the declarations. We count the number of template
1509 headers, and we check if they match TEMPLATE_COUNT + 1
1510 (TEMPLATE_COUNT is the number of qualifying template classes,
1511 plus there must be another header for the member template
1514 Notice that if header_count is zero, this is not a
1515 specialization but rather a template instantiation, so there
1516 is no check we can perform here. */
1517 if (header_count
&& header_count
!= template_count
+ 1)
1520 /* Check that the number of template arguments at the
1521 innermost level for DECL is the same as for FN. */
1522 if (current_binding_level
->kind
== sk_template_parms
1523 && !current_binding_level
->explicit_spec_p
1524 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1525 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1526 (current_template_parms
))))
1529 /* DECL might be a specialization of FN. */
1530 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1531 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1533 /* For a non-static member function, we need to make sure
1534 that the const qualification is the same. Since
1535 get_bindings does not try to merge the "this" parameter,
1536 we must do the comparison explicitly. */
1537 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1538 && !same_type_p (TREE_VALUE (fn_arg_types
),
1539 TREE_VALUE (decl_arg_types
)))
1542 /* Skip the "this" parameter and, for constructors of
1543 classes with virtual bases, the VTT parameter. A
1544 full specialization of a constructor will have a VTT
1545 parameter, but a template never will. */
1547 = skip_artificial_parms_for (decl
, decl_arg_types
);
1549 = skip_artificial_parms_for (fn
, fn_arg_types
);
1551 /* Check that the number of function parameters matches.
1553 template <class T> void f(int i = 0);
1554 template <> void f<int>();
1555 The specialization f<int> is invalid but is not caught
1556 by get_bindings below. */
1557 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1560 /* Function templates cannot be specializations; there are
1561 no partial specializations of functions. Therefore, if
1562 the type of DECL does not match FN, there is no
1564 if (tsk
== tsk_template
)
1566 if (compparms (fn_arg_types
, decl_arg_types
))
1567 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1571 /* See whether this function might be a specialization of this
1573 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1576 /* We cannot deduce template arguments that when used to
1577 specialize TMPL will produce DECL. */
1580 /* Save this template, and the arguments deduced. */
1581 templates
= tree_cons (targs
, fn
, templates
);
1583 else if (need_member_template
)
1584 /* FN is an ordinary member function, and we need a
1585 specialization of a member template. */
1587 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1588 /* We can get IDENTIFIER_NODEs here in certain erroneous
1591 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1592 /* This is just an ordinary non-member function. Nothing can
1593 be a specialization of that. */
1595 else if (DECL_ARTIFICIAL (fn
))
1596 /* Cannot specialize functions that are created implicitly. */
1600 tree decl_arg_types
;
1602 /* This is an ordinary member function. However, since
1603 we're here, we can assume it's enclosing class is a
1604 template class. For example,
1606 template <typename T> struct S { void f(); };
1607 template <> void S<int>::f() {}
1609 Here, S<int>::f is a non-template, but S<int> is a
1610 template class. If FN has the same type as DECL, we
1611 might be in business. */
1613 if (!DECL_TEMPLATE_INFO (fn
))
1614 /* Its enclosing class is an explicit specialization
1615 of a template class. This is not a candidate. */
1618 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1619 TREE_TYPE (TREE_TYPE (fn
))))
1620 /* The return types differ. */
1623 /* Adjust the type of DECL in case FN is a static member. */
1624 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1625 if (DECL_STATIC_FUNCTION_P (fn
)
1626 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1627 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1629 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1632 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1636 if (templates
&& TREE_CHAIN (templates
))
1642 It is possible for a specialization with a given function
1643 signature to be instantiated from more than one function
1644 template. In such cases, explicit specification of the
1645 template arguments must be used to uniquely identify the
1646 function template specialization being specialized.
1648 Note that here, there's no suggestion that we're supposed to
1649 determine which of the candidate templates is most
1650 specialized. However, we, also have:
1654 Partial ordering of overloaded function template
1655 declarations is used in the following contexts to select
1656 the function template to which a function template
1657 specialization refers:
1659 -- when an explicit specialization refers to a function
1662 So, we do use the partial ordering rules, at least for now.
1663 This extension can only serve to make invalid programs valid,
1664 so it's safe. And, there is strong anecdotal evidence that
1665 the committee intended the partial ordering rules to apply;
1666 the EDG front end has that behavior, and John Spicer claims
1667 that the committee simply forgot to delete the wording in
1668 [temp.expl.spec]. */
1669 tree tmpl
= most_specialized_instantiation (templates
);
1670 if (tmpl
!= error_mark_node
)
1673 TREE_CHAIN (templates
) = NULL_TREE
;
1677 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1679 error ("template-id %qD for %q+D does not match any template "
1680 "declaration", template_id
, decl
);
1681 return error_mark_node
;
1683 else if ((templates
&& TREE_CHAIN (templates
))
1684 || (candidates
&& TREE_CHAIN (candidates
))
1685 || (templates
&& candidates
))
1687 error ("ambiguous template specialization %qD for %q+D",
1689 chainon (candidates
, templates
);
1690 print_candidates (candidates
);
1691 return error_mark_node
;
1694 /* We have one, and exactly one, match. */
1697 tree fn
= TREE_VALUE (candidates
);
1698 /* DECL is a re-declaration of a template function. */
1699 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1701 /* It was a specialization of an ordinary member function in a
1703 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
1704 return DECL_TI_TEMPLATE (fn
);
1707 /* It was a specialization of a template. */
1708 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1709 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1711 *targs_out
= copy_node (targs
);
1712 SET_TMPL_ARGS_LEVEL (*targs_out
,
1713 TMPL_ARGS_DEPTH (*targs_out
),
1714 TREE_PURPOSE (templates
));
1717 *targs_out
= TREE_PURPOSE (templates
);
1718 return TREE_VALUE (templates
);
1721 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1722 but with the default argument values filled in from those in the
1726 copy_default_args_to_explicit_spec_1 (tree spec_types
,
1729 tree new_spec_types
;
1734 if (spec_types
== void_list_node
)
1735 return void_list_node
;
1737 /* Substitute into the rest of the list. */
1739 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1740 TREE_CHAIN (tmpl_types
));
1742 /* Add the default argument for this parameter. */
1743 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1744 TREE_VALUE (spec_types
),
1748 /* DECL is an explicit specialization. Replicate default arguments
1749 from the template it specializes. (That way, code like:
1751 template <class T> void f(T = 3);
1752 template <> void f(double);
1755 works, as required.) An alternative approach would be to look up
1756 the correct default arguments at the call-site, but this approach
1757 is consistent with how implicit instantiations are handled. */
1760 copy_default_args_to_explicit_spec (tree decl
)
1765 tree new_spec_types
;
1769 tree object_type
= NULL_TREE
;
1770 tree in_charge
= NULL_TREE
;
1771 tree vtt
= NULL_TREE
;
1773 /* See if there's anything we need to do. */
1774 tmpl
= DECL_TI_TEMPLATE (decl
);
1775 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1776 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1777 if (TREE_PURPOSE (t
))
1782 old_type
= TREE_TYPE (decl
);
1783 spec_types
= TYPE_ARG_TYPES (old_type
);
1785 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1787 /* Remove the this pointer, but remember the object's type for
1789 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1790 spec_types
= TREE_CHAIN (spec_types
);
1791 tmpl_types
= TREE_CHAIN (tmpl_types
);
1793 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1795 /* DECL may contain more parameters than TMPL due to the extra
1796 in-charge parameter in constructors and destructors. */
1797 in_charge
= spec_types
;
1798 spec_types
= TREE_CHAIN (spec_types
);
1800 if (DECL_HAS_VTT_PARM_P (decl
))
1803 spec_types
= TREE_CHAIN (spec_types
);
1807 /* Compute the merged default arguments. */
1809 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1811 /* Compute the new FUNCTION_TYPE. */
1815 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1820 /* Put the in-charge parameter back. */
1821 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1822 TREE_VALUE (in_charge
),
1825 new_type
= build_method_type_directly (object_type
,
1826 TREE_TYPE (old_type
),
1830 new_type
= build_function_type (TREE_TYPE (old_type
),
1832 new_type
= cp_build_type_attribute_variant (new_type
,
1833 TYPE_ATTRIBUTES (old_type
));
1834 new_type
= build_exception_variant (new_type
,
1835 TYPE_RAISES_EXCEPTIONS (old_type
));
1836 TREE_TYPE (decl
) = new_type
;
1839 /* Check to see if the function just declared, as indicated in
1840 DECLARATOR, and in DECL, is a specialization of a function
1841 template. We may also discover that the declaration is an explicit
1842 instantiation at this point.
1844 Returns DECL, or an equivalent declaration that should be used
1845 instead if all goes well. Issues an error message if something is
1846 amiss. Returns error_mark_node if the error is not easily
1849 FLAGS is a bitmask consisting of the following flags:
1851 2: The function has a definition.
1852 4: The function is a friend.
1854 The TEMPLATE_COUNT is the number of references to qualifying
1855 template classes that appeared in the name of the function. For
1858 template <class T> struct S { void f(); };
1861 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1862 classes are not counted in the TEMPLATE_COUNT, so that in
1864 template <class T> struct S {};
1865 template <> struct S<int> { void f(); }
1866 template <> void S<int>::f();
1868 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1869 invalid; there should be no template <>.)
1871 If the function is a specialization, it is marked as such via
1872 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1873 is set up correctly, and it is added to the list of specializations
1874 for that template. */
1877 check_explicit_specialization (tree declarator
,
1882 int have_def
= flags
& 2;
1883 int is_friend
= flags
& 4;
1884 int specialization
= 0;
1885 int explicit_instantiation
= 0;
1886 int member_specialization
= 0;
1887 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1888 tree dname
= DECL_NAME (decl
);
1893 if (!processing_specialization
)
1896 tsk
= tsk_excessive_parms
;
1899 tsk
= current_tmpl_spec_kind (template_count
);
1904 if (processing_specialization
)
1907 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1909 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1912 /* This could be something like:
1914 template <class T> void f(T);
1915 class S { friend void f<>(int); } */
1919 /* This case handles bogus declarations like template <>
1920 template <class T> void f<int>(); */
1922 error ("template-id %qD in declaration of primary template",
1929 case tsk_invalid_member_spec
:
1930 /* The error has already been reported in
1931 check_specialization_scope. */
1932 return error_mark_node
;
1934 case tsk_invalid_expl_inst
:
1935 error ("template parameter list used in explicit instantiation");
1941 error ("definition provided for explicit instantiation");
1943 explicit_instantiation
= 1;
1946 case tsk_excessive_parms
:
1947 case tsk_insufficient_parms
:
1948 if (tsk
== tsk_excessive_parms
)
1949 error ("too many template parameter lists in declaration of %qD",
1951 else if (template_header_count
)
1952 error("too few template parameter lists in declaration of %qD", decl
);
1954 error("explicit specialization of %qD must be introduced by "
1955 "%<template <>%>", decl
);
1959 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1961 member_specialization
= 1;
1967 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1969 /* This case handles bogus declarations like template <>
1970 template <class T> void f<int>(); */
1972 if (uses_template_parms (declarator
))
1973 error ("function template partial specialization %qD "
1974 "is not allowed", declarator
);
1976 error ("template-id %qD in declaration of primary template",
1981 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1982 /* This is a specialization of a member template, without
1983 specialization the containing class. Something like:
1985 template <class T> struct S {
1986 template <class U> void f (U);
1988 template <> template <class U> void S<int>::f(U) {}
1990 That's a specialization -- but of the entire template. */
1998 if (specialization
|| member_specialization
)
2000 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2001 for (; t
; t
= TREE_CHAIN (t
))
2002 if (TREE_PURPOSE (t
))
2005 ("default argument specified in explicit specialization");
2010 if (specialization
|| member_specialization
|| explicit_instantiation
)
2012 tree tmpl
= NULL_TREE
;
2013 tree targs
= NULL_TREE
;
2015 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2016 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2020 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2025 /* If there is no class context, the explicit instantiation
2026 must be at namespace scope. */
2027 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2029 /* Find the namespace binding, using the declaration
2031 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2033 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2035 error ("%qD is not a template function", dname
);
2036 fns
= error_mark_node
;
2040 tree fn
= OVL_CURRENT (fns
);
2041 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2042 CP_DECL_CONTEXT (fn
)))
2043 error ("%qD is not declared in %qD",
2044 decl
, current_namespace
);
2048 declarator
= lookup_template_function (fns
, NULL_TREE
);
2051 if (declarator
== error_mark_node
)
2052 return error_mark_node
;
2054 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2056 if (!explicit_instantiation
)
2057 /* A specialization in class scope. This is invalid,
2058 but the error will already have been flagged by
2059 check_specialization_scope. */
2060 return error_mark_node
;
2063 /* It's not valid to write an explicit instantiation in
2066 class C { template void f(); }
2068 This case is caught by the parser. However, on
2071 template class C { void f(); };
2073 (which is invalid) we can get here. The error will be
2080 else if (ctype
!= NULL_TREE
2081 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2084 /* Find the list of functions in ctype that have the same
2085 name as the declared function. */
2086 tree name
= TREE_OPERAND (declarator
, 0);
2087 tree fns
= NULL_TREE
;
2090 if (constructor_name_p (name
, ctype
))
2092 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2094 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2095 : !CLASSTYPE_DESTRUCTORS (ctype
))
2097 /* From [temp.expl.spec]:
2099 If such an explicit specialization for the member
2100 of a class template names an implicitly-declared
2101 special member function (clause _special_), the
2102 program is ill-formed.
2104 Similar language is found in [temp.explicit]. */
2105 error ("specialization of implicitly-declared special member function");
2106 return error_mark_node
;
2109 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2112 if (!DECL_CONV_FN_P (decl
))
2114 idx
= lookup_fnfields_1 (ctype
, name
);
2116 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2120 VEC(tree
,gc
) *methods
;
2123 /* For a type-conversion operator, we cannot do a
2124 name-based lookup. We might be looking for `operator
2125 int' which will be a specialization of `operator T'.
2126 So, we find *all* the conversion operators, and then
2127 select from them. */
2130 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2132 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2133 VEC_iterate (tree
, methods
, idx
, ovl
);
2136 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2137 /* There are no more conversion functions. */
2140 /* Glue all these conversion functions together
2141 with those we already have. */
2142 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2143 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2147 if (fns
== NULL_TREE
)
2149 error ("no member function %qD declared in %qT", name
, ctype
);
2150 return error_mark_node
;
2153 TREE_OPERAND (declarator
, 0) = fns
;
2156 /* Figure out what exactly is being specialized at this point.
2157 Note that for an explicit instantiation, even one for a
2158 member function, we cannot tell apriori whether the
2159 instantiation is for a member template, or just a member
2160 function of a template class. Even if a member template is
2161 being instantiated, the member template arguments may be
2162 elided if they can be deduced from the rest of the
2164 tmpl
= determine_specialization (declarator
, decl
,
2166 member_specialization
,
2170 if (!tmpl
|| tmpl
== error_mark_node
)
2171 /* We couldn't figure out what this declaration was
2173 return error_mark_node
;
2176 tree gen_tmpl
= most_general_template (tmpl
);
2178 if (explicit_instantiation
)
2180 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2181 is done by do_decl_instantiation later. */
2183 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2184 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2186 if (arg_depth
> parm_depth
)
2188 /* If TMPL is not the most general template (for
2189 example, if TMPL is a friend template that is
2190 injected into namespace scope), then there will
2191 be too many levels of TARGS. Remove some of them
2196 new_targs
= make_tree_vec (parm_depth
);
2197 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2198 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2199 = TREE_VEC_ELT (targs
, i
);
2203 return instantiate_template (tmpl
, targs
, tf_error
);
2206 /* If we thought that the DECL was a member function, but it
2207 turns out to be specializing a static member function,
2208 make DECL a static member function as well. */
2209 if (DECL_STATIC_FUNCTION_P (tmpl
)
2210 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2211 revert_static_member_fn (decl
);
2213 /* If this is a specialization of a member template of a
2214 template class, we want to return the TEMPLATE_DECL, not
2215 the specialization of it. */
2216 if (tsk
== tsk_template
)
2218 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2219 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
2222 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2223 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl
))
2224 = DECL_SOURCE_LOCATION (decl
);
2225 /* We want to use the argument list specified in the
2226 definition, not in the original declaration. */
2227 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl
))
2228 = DECL_ARGUMENTS (decl
);
2233 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2234 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
2236 /* Inherit default function arguments from the template
2237 DECL is specializing. */
2238 copy_default_args_to_explicit_spec (decl
);
2240 /* This specialization has the same protection as the
2241 template it specializes. */
2242 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2243 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2245 /* 7.1.1-1 [dcl.stc]
2247 A storage-class-specifier shall not be specified in an
2248 explicit specialization...
2250 The parser rejects these, so unless action is taken here,
2251 explicit function specializations will always appear with
2254 The action recommended by the C++ CWG in response to C++
2255 defect report 605 is to make the storage class and linkage
2256 of the explicit specialization match the templated function:
2258 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2260 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2262 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2263 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2265 /* This specialization has the same linkage and visibility as
2266 the function template it specializes. */
2267 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2268 if (! TREE_PUBLIC (decl
))
2270 DECL_INTERFACE_KNOWN (decl
) = 1;
2271 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2273 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2274 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2276 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2277 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2281 /* If DECL is a friend declaration, declared using an
2282 unqualified name, the namespace associated with DECL may
2283 have been set incorrectly. For example, in:
2285 template <typename T> void f(T);
2287 struct S { friend void f<int>(int); }
2290 we will have set the DECL_CONTEXT for the friend
2291 declaration to N, rather than to the global namespace. */
2292 if (DECL_NAMESPACE_SCOPE_P (decl
))
2293 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2295 if (is_friend
&& !have_def
)
2296 /* This is not really a declaration of a specialization.
2297 It's just the name of an instantiation. But, it's not
2298 a request for an instantiation, either. */
2299 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2300 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2301 /* This is indeed a specialization. In case of constructors
2302 and destructors, we need in-charge and not-in-charge
2303 versions in V3 ABI. */
2304 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2306 /* Register this specialization so that we can find it
2308 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
);
2315 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2316 parameters. These are represented in the same format used for
2317 DECL_TEMPLATE_PARMS. */
2320 comp_template_parms (const_tree parms1
, const_tree parms2
)
2325 if (parms1
== parms2
)
2328 for (p1
= parms1
, p2
= parms2
;
2329 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2330 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2332 tree t1
= TREE_VALUE (p1
);
2333 tree t2
= TREE_VALUE (p2
);
2336 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2337 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2339 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2342 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2344 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2345 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2347 /* If either of the template parameters are invalid, assume
2348 they match for the sake of error recovery. */
2349 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2352 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2355 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2356 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2357 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2359 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2364 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2365 /* One set of parameters has more parameters lists than the
2372 /* Determine whether PARM is a parameter pack. */
2374 template_parameter_pack_p (const_tree parm
)
2376 /* Determine if we have a non-type template parameter pack. */
2377 if (TREE_CODE (parm
) == PARM_DECL
)
2378 return (DECL_TEMPLATE_PARM_P (parm
)
2379 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2381 /* If this is a list of template parameters, we could get a
2382 TYPE_DECL or a TEMPLATE_DECL. */
2383 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2384 parm
= TREE_TYPE (parm
);
2386 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2387 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2388 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2391 /* Determine whether ARGS describes a variadic template args list,
2392 i.e., one that is terminated by a template argument pack. */
2394 template_args_variadic_p (tree args
)
2399 if (args
== NULL_TREE
)
2402 args
= INNERMOST_TEMPLATE_ARGS (args
);
2403 nargs
= TREE_VEC_LENGTH (args
);
2408 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2410 return ARGUMENT_PACK_P (last_parm
);
2413 /* Generate a new name for the parameter pack name NAME (an
2414 IDENTIFIER_NODE) that incorporates its */
2416 make_ith_pack_parameter_name (tree name
, int i
)
2418 /* Munge the name to include the parameter index. */
2422 sprintf(numbuf
, "%i", i
);
2423 newname
= (char*)alloca (IDENTIFIER_LENGTH (name
) + strlen(numbuf
) + 2);
2424 sprintf(newname
, "%s#%i", IDENTIFIER_POINTER (name
), i
);
2425 return get_identifier (newname
);
2428 /* Structure used to track the progress of find_parameter_packs_r. */
2429 struct find_parameter_pack_data
2431 /* TREE_LIST that will contain all of the parameter packs found by
2433 tree
* parameter_packs
;
2435 /* Set of AST nodes that have been visited by the traversal. */
2436 struct pointer_set_t
*visited
;
2439 /* Identifies all of the argument packs that occur in a template
2440 argument and appends them to the TREE_LIST inside DATA, which is a
2441 find_parameter_pack_data structure. This is a subroutine of
2442 make_pack_expansion and uses_parameter_packs. */
2444 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2447 struct find_parameter_pack_data
* ppd
=
2448 (struct find_parameter_pack_data
*)data
;
2449 bool parameter_pack_p
= false;
2451 /* Don't visit nodes twice. */
2452 if (pointer_set_contains (ppd
->visited
, *tp
))
2458 /* Identify whether this is a parameter pack or not. */
2459 switch (TREE_CODE (t
))
2461 case TEMPLATE_PARM_INDEX
:
2462 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2463 parameter_pack_p
= true;
2466 case TEMPLATE_TYPE_PARM
:
2467 case TEMPLATE_TEMPLATE_PARM
:
2468 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2469 parameter_pack_p
= true;
2473 if (FUNCTION_PARAMETER_PACK_P (t
))
2475 /* We don't want to walk into the type of a PARM_DECL,
2476 because we don't want to see the type parameter pack. */
2478 parameter_pack_p
= true;
2483 /* Not a parameter pack. */
2487 if (parameter_pack_p
)
2489 /* Add this parameter pack to the list. */
2490 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2493 /* Make sure we do not visit this node again. */
2494 pointer_set_insert (ppd
->visited
, *tp
);
2497 cp_walk_tree (&TYPE_CONTEXT (t
),
2498 &find_parameter_packs_r
, ppd
, NULL
);
2500 /* This switch statement will return immediately if we don't find a
2502 switch (TREE_CODE (t
))
2504 case TEMPLATE_PARM_INDEX
:
2507 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2508 /* Check the template itself. */
2509 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2510 &find_parameter_packs_r
, ppd
, NULL
);
2511 /* Check the template arguments. */
2512 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2517 case TEMPLATE_TYPE_PARM
:
2518 case TEMPLATE_TEMPLATE_PARM
:
2525 if (TYPE_PTRMEMFUNC_P (t
))
2531 if (TYPE_TEMPLATE_INFO (t
))
2532 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
2533 &find_parameter_packs_r
, ppd
, NULL
);
2539 cp_walk_tree (&TREE_TYPE (t
),
2540 &find_parameter_packs_r
, ppd
, NULL
);
2544 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
2549 case TYPE_PACK_EXPANSION
:
2550 case EXPR_PACK_EXPANSION
:
2555 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
2560 case IDENTIFIER_NODE
:
2561 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
, NULL
);
2572 /* Determines if the expression or type T uses any parameter packs. */
2574 uses_parameter_packs (tree t
)
2576 tree parameter_packs
= NULL_TREE
;
2577 struct find_parameter_pack_data ppd
;
2578 ppd
.parameter_packs
= ¶meter_packs
;
2579 ppd
.visited
= pointer_set_create ();
2580 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, NULL
);
2581 pointer_set_destroy (ppd
.visited
);
2582 return parameter_packs
!= NULL_TREE
;
2585 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2586 representation a base-class initializer into a parameter pack
2587 expansion. If all goes well, the resulting node will be an
2588 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2591 make_pack_expansion (tree arg
)
2594 tree parameter_packs
= NULL_TREE
;
2595 bool for_types
= false;
2596 struct find_parameter_pack_data ppd
;
2598 if (!arg
|| arg
== error_mark_node
)
2601 if (TREE_CODE (arg
) == TREE_LIST
)
2603 /* The only time we will see a TREE_LIST here is for a base
2604 class initializer. In this case, the TREE_PURPOSE will be a
2605 _TYPE node (representing the base class expansion we're
2606 initializing) and the TREE_VALUE will be a TREE_LIST
2607 containing the initialization arguments.
2609 The resulting expansion looks somewhat different from most
2610 expansions. Rather than returning just one _EXPANSION, we
2611 return a TREE_LIST whose TREE_PURPOSE is a
2612 TYPE_PACK_EXPANSION containing the bases that will be
2613 initialized. The TREE_VALUE will be identical to the
2614 original TREE_VALUE, which is a list of arguments that will
2615 be passed to each base. We do not introduce any new pack
2616 expansion nodes into the TREE_VALUE (although it is possible
2617 that some already exist), because the TREE_PURPOSE and
2618 TREE_VALUE all need to be expanded together with the same
2619 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2620 resulting TREE_PURPOSE will mention the parameter packs in
2621 both the bases and the arguments to the bases. */
2624 tree parameter_packs
= NULL_TREE
;
2626 /* Determine which parameter packs will be used by the base
2628 ppd
.visited
= pointer_set_create ();
2629 ppd
.parameter_packs
= ¶meter_packs
;
2630 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
2633 if (parameter_packs
== NULL_TREE
)
2635 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
2636 pointer_set_destroy (ppd
.visited
);
2637 return error_mark_node
;
2640 if (TREE_VALUE (arg
) != void_type_node
)
2642 /* Collect the sets of parameter packs used in each of the
2643 initialization arguments. */
2644 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
2646 /* Determine which parameter packs will be expanded in this
2648 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
2653 pointer_set_destroy (ppd
.visited
);
2655 /* Create the pack expansion type for the base type. */
2656 purpose
= make_node (TYPE_PACK_EXPANSION
);
2657 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
2658 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
2660 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2661 they will rarely be compared to anything. */
2662 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
2664 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
2667 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
2670 /* Build the PACK_EXPANSION_* node. */
2671 result
= make_node (for_types
? TYPE_PACK_EXPANSION
: EXPR_PACK_EXPANSION
);
2672 SET_PACK_EXPANSION_PATTERN (result
, arg
);
2673 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
2675 /* Propagate type and const-expression information. */
2676 TREE_TYPE (result
) = TREE_TYPE (arg
);
2677 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
2680 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2681 they will rarely be compared to anything. */
2682 SET_TYPE_STRUCTURAL_EQUALITY (result
);
2684 /* Determine which parameter packs will be expanded. */
2685 ppd
.parameter_packs
= ¶meter_packs
;
2686 ppd
.visited
= pointer_set_create ();
2687 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, NULL
);
2688 pointer_set_destroy (ppd
.visited
);
2690 /* Make sure we found some parameter packs. */
2691 if (parameter_packs
== NULL_TREE
)
2694 error ("expansion pattern %<%T%> contains no argument packs", arg
);
2696 error ("expansion pattern %<%E%> contains no argument packs", arg
);
2697 return error_mark_node
;
2699 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
2704 /* Checks T for any "bare" parameter packs, which have not yet been
2705 expanded, and issues an error if any are found. This operation can
2706 only be done on full expressions or types (e.g., an expression
2707 statement, "if" condition, etc.), because we could have expressions like:
2709 foo(f(g(h(args)))...)
2711 where "args" is a parameter pack. check_for_bare_parameter_packs
2712 should not be called for the subexpressions args, h(args),
2713 g(h(args)), or f(g(h(args))), because we would produce erroneous
2716 Returns TRUE and emits an error if there were bare parameter packs,
2717 returns FALSE otherwise. */
2719 check_for_bare_parameter_packs (tree t
)
2721 tree parameter_packs
= NULL_TREE
;
2722 struct find_parameter_pack_data ppd
;
2724 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
2727 if (TREE_CODE (t
) == TYPE_DECL
)
2730 ppd
.parameter_packs
= ¶meter_packs
;
2731 ppd
.visited
= pointer_set_create ();
2732 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, NULL
);
2733 pointer_set_destroy (ppd
.visited
);
2735 if (parameter_packs
)
2737 error ("parameter packs not expanded with `...':");
2738 while (parameter_packs
)
2740 tree pack
= TREE_VALUE (parameter_packs
);
2741 tree name
= NULL_TREE
;
2743 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
2744 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
2745 name
= TYPE_NAME (pack
);
2746 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
2747 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
2749 name
= DECL_NAME (pack
);
2752 inform (" %qD", name
);
2754 inform (" <anonymous>");
2756 parameter_packs
= TREE_CHAIN (parameter_packs
);
2759 /* Clean up any references to these parameter packs within the
2761 ppd
.parameter_packs
= ¶meter_packs
;
2762 ppd
.visited
= pointer_set_create ();
2763 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, NULL
);
2764 pointer_set_destroy (ppd
.visited
);
2772 /* Expand any parameter packs that occur in the template arguments in
2775 expand_template_argument_pack (tree args
)
2777 tree result_args
= NULL_TREE
;
2778 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
2779 int num_result_args
= -1;
2781 /* First, determine if we need to expand anything, and the number of
2782 slots we'll need. */
2783 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2785 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2786 if (ARGUMENT_PACK_P (arg
))
2788 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
2789 if (num_result_args
< 0)
2790 num_result_args
= in_arg
+ num_packed
;
2792 num_result_args
+= num_packed
;
2796 if (num_result_args
>= 0)
2801 /* If no expansion is necessary, we're done. */
2802 if (num_result_args
< 0)
2805 /* Expand arguments. */
2806 result_args
= make_tree_vec (num_result_args
);
2807 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2809 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2810 if (ARGUMENT_PACK_P (arg
))
2812 tree packed
= ARGUMENT_PACK_ARGS (arg
);
2813 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
2814 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
2815 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
2819 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
2827 /* Complain if DECL shadows a template parameter.
2829 [temp.local]: A template-parameter shall not be redeclared within its
2830 scope (including nested scopes). */
2833 check_template_shadow (tree decl
)
2837 /* If we're not in a template, we can't possibly shadow a template
2839 if (!current_template_parms
)
2842 /* Figure out what we're shadowing. */
2843 if (TREE_CODE (decl
) == OVERLOAD
)
2844 decl
= OVL_CURRENT (decl
);
2845 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
2847 /* If there's no previous binding for this name, we're not shadowing
2848 anything, let alone a template parameter. */
2852 /* If we're not shadowing a template parameter, we're done. Note
2853 that OLDDECL might be an OVERLOAD (or perhaps even an
2854 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2856 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
2859 /* We check for decl != olddecl to avoid bogus errors for using a
2860 name inside a class. We check TPFI to avoid duplicate errors for
2861 inline member templates. */
2863 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
2866 error ("declaration of %q+#D", decl
);
2867 error (" shadows template parm %q+#D", olddecl
);
2870 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2871 ORIG_LEVEL, DECL, and TYPE. */
2874 build_template_parm_index (int index
,
2880 tree t
= make_node (TEMPLATE_PARM_INDEX
);
2881 TEMPLATE_PARM_IDX (t
) = index
;
2882 TEMPLATE_PARM_LEVEL (t
) = level
;
2883 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
2884 TEMPLATE_PARM_DECL (t
) = decl
;
2885 TREE_TYPE (t
) = type
;
2886 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
2887 TREE_INVARIANT (t
) = TREE_INVARIANT (decl
);
2888 TREE_READONLY (t
) = TREE_READONLY (decl
);
2893 /* Find the canonical type parameter for the given template type
2894 parameter. Returns the canonical type parameter, which may be TYPE
2895 if no such parameter existed. */
2897 canonical_type_parameter (tree type
)
2900 int idx
= TEMPLATE_TYPE_IDX (type
);
2901 if (!canonical_template_parms
)
2902 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
2904 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
2905 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
2907 list
= VEC_index (tree
, canonical_template_parms
, idx
);
2908 while (list
&& !cp_comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
2909 list
= TREE_CHAIN (list
);
2912 return TREE_VALUE (list
);
2915 VEC_replace(tree
, canonical_template_parms
, idx
,
2916 tree_cons (NULL_TREE
, type
,
2917 VEC_index (tree
, canonical_template_parms
, idx
)));
2922 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2923 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2924 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2925 new one is created. */
2928 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
2929 tsubst_flags_t complain
)
2931 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
2932 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
2933 != TEMPLATE_PARM_LEVEL (index
) - levels
))
2935 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
2938 decl
= build_decl (TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
2939 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
2940 TREE_INVARIANT (decl
) = TREE_INVARIANT (orig_decl
);
2941 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
2942 DECL_ARTIFICIAL (decl
) = 1;
2943 SET_DECL_TEMPLATE_PARM_P (decl
);
2945 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
2946 TEMPLATE_PARM_LEVEL (index
) - levels
,
2947 TEMPLATE_PARM_ORIG_LEVEL (index
),
2949 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
2950 TEMPLATE_PARM_PARAMETER_PACK (t
)
2951 = TEMPLATE_PARM_PARAMETER_PACK (index
);
2953 /* Template template parameters need this. */
2954 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2955 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
2956 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
2960 return TEMPLATE_PARM_DESCENDANTS (index
);
2963 /* Process information from new template parameter PARM and append it to the
2964 LIST being built. This new parameter is a non-type parameter iff
2965 IS_NON_TYPE is true. This new parameter is a parameter
2966 pack iff IS_PARAMETER_PACK is true. */
2969 process_template_parm (tree list
, tree parm
, bool is_non_type
,
2970 bool is_parameter_pack
)
2977 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
2978 defval
= TREE_PURPOSE (parm
);
2982 tree p
= tree_last (list
);
2984 if (p
&& TREE_VALUE (p
) != error_mark_node
)
2987 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
2988 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
2990 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3000 parm
= TREE_VALUE (parm
);
3002 SET_DECL_TEMPLATE_PARM_P (parm
);
3004 if (TREE_TYPE (parm
) == error_mark_node
)
3006 err_parm_list
= build_tree_list (defval
, parm
);
3007 TREE_VALUE (err_parm_list
) = error_mark_node
;
3008 return chainon (list
, err_parm_list
);
3014 The top-level cv-qualifiers on the template-parameter are
3015 ignored when determining its type. */
3016 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3017 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3019 err_parm_list
= build_tree_list (defval
, parm
);
3020 TREE_VALUE (err_parm_list
) = error_mark_node
;
3021 return chainon (list
, err_parm_list
);
3024 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3026 /* This template parameter is not a parameter pack, but it
3027 should be. Complain about "bare" parameter packs. */
3028 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3030 /* Recover by calling this a parameter pack. */
3031 is_parameter_pack
= true;
3035 /* A template parameter is not modifiable. */
3036 TREE_CONSTANT (parm
) = 1;
3037 TREE_INVARIANT (parm
) = 1;
3038 TREE_READONLY (parm
) = 1;
3039 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3040 TREE_CONSTANT (decl
) = 1;
3041 TREE_INVARIANT (decl
) = 1;
3042 TREE_READONLY (decl
) = 1;
3043 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3044 = build_template_parm_index (idx
, processing_template_decl
,
3045 processing_template_decl
,
3046 decl
, TREE_TYPE (parm
));
3048 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3049 = is_parameter_pack
;
3054 parm
= TREE_VALUE (TREE_VALUE (parm
));
3056 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3058 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
3059 /* This is for distinguishing between real templates and template
3060 template parameters */
3061 TREE_TYPE (parm
) = t
;
3062 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3067 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
3068 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3069 decl
= build_decl (TYPE_DECL
, parm
, t
);
3072 TYPE_NAME (t
) = decl
;
3073 TYPE_STUB_DECL (t
) = decl
;
3075 TEMPLATE_TYPE_PARM_INDEX (t
)
3076 = build_template_parm_index (idx
, processing_template_decl
,
3077 processing_template_decl
,
3078 decl
, TREE_TYPE (parm
));
3079 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3080 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3082 DECL_ARTIFICIAL (decl
) = 1;
3083 SET_DECL_TEMPLATE_PARM_P (decl
);
3085 parm
= build_tree_list (defval
, parm
);
3086 return chainon (list
, parm
);
3089 /* The end of a template parameter list has been reached. Process the
3090 tree list into a parameter vector, converting each parameter into a more
3091 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3095 end_template_parm_list (tree parms
)
3099 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3101 current_template_parms
3102 = tree_cons (size_int (processing_template_decl
),
3103 saved_parmlist
, current_template_parms
);
3105 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3107 next
= TREE_CHAIN (parm
);
3108 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3109 TREE_CHAIN (parm
) = NULL_TREE
;
3112 --processing_template_parmlist
;
3114 return saved_parmlist
;
3117 /* end_template_decl is called after a template declaration is seen. */
3120 end_template_decl (void)
3122 reset_specialization ();
3124 if (! processing_template_decl
)
3127 /* This matches the pushlevel in begin_template_parm_list. */
3130 --processing_template_decl
;
3131 current_template_parms
= TREE_CHAIN (current_template_parms
);
3134 /* Within the declaration of a template, return all levels of template
3135 parameters that apply. The template parameters are represented as
3136 a TREE_VEC, in the form documented in cp-tree.h for template
3140 current_template_args (void)
3143 tree args
= NULL_TREE
;
3144 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3147 /* If there is only one level of template parameters, we do not
3148 create a TREE_VEC of TREE_VECs. Instead, we return a single
3149 TREE_VEC containing the arguments. */
3151 args
= make_tree_vec (length
);
3153 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3155 tree a
= copy_node (TREE_VALUE (header
));
3158 TREE_TYPE (a
) = NULL_TREE
;
3159 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3161 tree t
= TREE_VEC_ELT (a
, i
);
3163 /* T will be a list if we are called from within a
3164 begin/end_template_parm_list pair, but a vector directly
3165 if within a begin/end_member_template_processing pair. */
3166 if (TREE_CODE (t
) == TREE_LIST
)
3170 if (!error_operand_p (t
))
3172 if (TREE_CODE (t
) == TYPE_DECL
3173 || TREE_CODE (t
) == TEMPLATE_DECL
)
3177 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3179 /* Turn this argument into a TYPE_ARGUMENT_PACK
3180 with a single element, which expands T. */
3181 tree vec
= make_tree_vec (1);
3182 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3184 t
= make_node (TYPE_ARGUMENT_PACK
);
3185 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3190 t
= DECL_INITIAL (t
);
3192 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3194 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3195 with a single element, which expands T. */
3196 tree vec
= make_tree_vec (1);
3197 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3198 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3200 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3201 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3202 TREE_TYPE (t
) = type
;
3205 TREE_VEC_ELT (a
, i
) = t
;
3211 TREE_VEC_ELT (args
, --l
) = a
;
3219 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3220 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3221 a member template. Used by push_template_decl below. */
3224 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3226 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3227 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3228 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3229 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3230 if (DECL_LANG_SPECIFIC (decl
))
3232 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
3233 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
3234 DECL_DESTRUCTOR_P (tmpl
) = DECL_DESTRUCTOR_P (decl
);
3235 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
3236 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
3237 if (DECL_OVERLOADED_OPERATOR_P (decl
))
3238 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
3239 DECL_OVERLOADED_OPERATOR_P (decl
));
3245 struct template_parm_data
3247 /* The level of the template parameters we are currently
3251 /* The index of the specialization argument we are currently
3255 /* An array whose size is the number of template parameters. The
3256 elements are nonzero if the parameter has been used in any one
3257 of the arguments processed so far. */
3260 /* An array whose size is the number of template arguments. The
3261 elements are nonzero if the argument makes use of template
3262 parameters of this level. */
3263 int* arg_uses_template_parms
;
3266 /* Subroutine of push_template_decl used to see if each template
3267 parameter in a partial specialization is used in the explicit
3268 argument list. If T is of the LEVEL given in DATA (which is
3269 treated as a template_parm_data*), then DATA->PARMS is marked
3273 mark_template_parm (tree t
, void* data
)
3277 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3279 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3281 level
= TEMPLATE_PARM_LEVEL (t
);
3282 idx
= TEMPLATE_PARM_IDX (t
);
3286 level
= TEMPLATE_TYPE_LEVEL (t
);
3287 idx
= TEMPLATE_TYPE_IDX (t
);
3290 if (level
== tpd
->level
)
3292 tpd
->parms
[idx
] = 1;
3293 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3296 /* Return zero so that for_each_template_parm will continue the
3297 traversal of the tree; we want to mark *every* template parm. */
3301 /* Process the partial specialization DECL. */
3304 process_partial_specialization (tree decl
)
3306 tree type
= TREE_TYPE (decl
);
3307 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3308 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3309 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3310 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3311 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3312 int nargs
= TREE_VEC_LENGTH (inner_args
);
3313 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
3315 int did_error_intro
= 0;
3316 struct template_parm_data tpd
;
3317 struct template_parm_data tpd2
;
3319 /* We check that each of the template parameters given in the
3320 partial specialization is used in the argument list to the
3321 specialization. For example:
3323 template <class T> struct S;
3324 template <class T> struct S<T*>;
3326 The second declaration is OK because `T*' uses the template
3327 parameter T, whereas
3329 template <class T> struct S<int>;
3331 is no good. Even trickier is:
3342 The S2<T> declaration is actually invalid; it is a
3343 full-specialization. Of course,
3346 struct S2<T (*)(U)>;
3348 or some such would have been OK. */
3349 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3350 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
3351 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3353 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
3354 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3355 for (i
= 0; i
< nargs
; ++i
)
3357 tpd
.current_arg
= i
;
3358 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3359 &mark_template_parm
,
3362 /*include_nondeduced_p=*/false);
3364 for (i
= 0; i
< ntparms
; ++i
)
3365 if (tpd
.parms
[i
] == 0)
3367 /* One of the template parms was not used in the
3369 if (!did_error_intro
)
3371 error ("template parameters not used in partial specialization:");
3372 did_error_intro
= 1;
3375 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3378 /* [temp.class.spec]
3380 The argument list of the specialization shall not be identical to
3381 the implicit argument list of the primary template. */
3382 if (comp_template_args
3384 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3386 error ("partial specialization %qT does not specialize any template arguments", type
);
3388 /* [temp.class.spec]
3390 A partially specialized non-type argument expression shall not
3391 involve template parameters of the partial specialization except
3392 when the argument expression is a simple identifier.
3394 The type of a template parameter corresponding to a specialized
3395 non-type argument shall not be dependent on a parameter of the
3398 Also, we verify that pack expansions only occur at the
3399 end of the argument list. */
3400 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3402 for (i
= 0; i
< nargs
; ++i
)
3404 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3405 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3406 tree packed_args
= NULL_TREE
;
3409 if (ARGUMENT_PACK_P (arg
))
3411 /* Extract the arguments from the argument pack. We'll be
3412 iterating over these in the following loop. */
3413 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3414 len
= TREE_VEC_LENGTH (packed_args
);
3417 for (j
= 0; j
< len
; j
++)
3420 /* Get the Jth argument in the parameter pack. */
3421 arg
= TREE_VEC_ELT (packed_args
, j
);
3423 if (PACK_EXPANSION_P (arg
))
3425 /* Pack expansions must come at the end of the
3427 if ((packed_args
&& j
< len
- 1)
3428 || (!packed_args
&& i
< nargs
- 1))
3430 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3431 error ("parameter pack argument %qE must be at the end of the template argument list", arg
);
3433 error ("parameter pack argument %qT must be at the end of the template argument list", arg
);
3436 TREE_VEC_ELT (packed_args
, j
) = error_mark_node
;
3440 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3441 /* We only care about the pattern. */
3442 arg
= PACK_EXPANSION_PATTERN (arg
);
3444 if (/* These first two lines are the `non-type' bit. */
3446 && TREE_CODE (arg
) != TEMPLATE_DECL
3447 /* This next line is the `argument expression is not just a
3448 simple identifier' condition and also the `specialized
3449 non-type argument' bit. */
3450 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
3452 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
3453 || (packed_args
&& uses_template_parms (arg
)))
3454 error ("template argument %qE involves template parameter(s)",
3458 /* Look at the corresponding template parameter,
3459 marking which template parameters its type depends
3461 tree type
= TREE_TYPE (parm
);
3465 /* We haven't yet initialized TPD2. Do so now. */
3466 tpd2
.arg_uses_template_parms
3467 = (int *) alloca (sizeof (int) * nargs
);
3468 /* The number of parameters here is the number in the
3469 main template, which, as checked in the assertion
3471 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
3473 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
3476 /* Mark the template parameters. But this time, we're
3477 looking for the template parameters of the main
3478 template, not in the specialization. */
3479 tpd2
.current_arg
= i
;
3480 tpd2
.arg_uses_template_parms
[i
] = 0;
3481 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
3482 for_each_template_parm (type
,
3483 &mark_template_parm
,
3486 /*include_nondeduced_p=*/false);
3488 if (tpd2
.arg_uses_template_parms
[i
])
3490 /* The type depended on some template parameters.
3491 If they are fully specialized in the
3492 specialization, that's OK. */
3494 for (j
= 0; j
< nargs
; ++j
)
3495 if (tpd2
.parms
[j
] != 0
3496 && tpd
.arg_uses_template_parms
[j
])
3498 error ("type %qT of template argument %qE depends "
3499 "on template parameter(s)",
3510 if (retrieve_specialization (maintmpl
, specargs
,
3511 /*class_specializations_p=*/true))
3512 /* We've already got this specialization. */
3515 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
3516 = tree_cons (specargs
, inner_parms
,
3517 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
3518 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
3522 /* Check that a template declaration's use of default arguments and
3523 parameter packs is not invalid. Here, PARMS are the template
3524 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3525 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3529 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3530 declaration (but not a definition); 1 indicates a declaration, 2
3531 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3532 emitted for extraneous default arguments.
3534 Returns TRUE if there were no errors found, FALSE otherwise. */
3537 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
3538 int is_partial
, int is_friend_decl
)
3541 int last_level_to_check
;
3543 bool no_errors
= true;
3547 A default template-argument shall not be specified in a
3548 function template declaration or a function template definition, nor
3549 in the template-parameter-list of the definition of a member of a
3552 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
3553 /* You can't have a function template declaration in a local
3554 scope, nor you can you define a member of a class template in a
3558 if (current_class_type
3559 && !TYPE_BEING_DEFINED (current_class_type
)
3560 && DECL_LANG_SPECIFIC (decl
)
3561 /* If this is either a friend defined in the scope of the class
3562 or a member function. */
3563 && (DECL_FUNCTION_MEMBER_P (decl
)
3564 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
3565 : DECL_FRIEND_CONTEXT (decl
)
3566 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
3568 /* And, if it was a member function, it really was defined in
3569 the scope of the class. */
3570 && (!DECL_FUNCTION_MEMBER_P (decl
)
3571 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
3572 /* We already checked these parameters when the template was
3573 declared, so there's no need to do it again now. This function
3574 was defined in class scope, but we're processing it's body now
3575 that the class is complete. */
3578 /* Core issue 226 (C++0x only): the following only applies to class
3580 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
3584 If a template-parameter has a default template-argument, all
3585 subsequent template-parameters shall have a default
3586 template-argument supplied. */
3587 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
3589 tree inner_parms
= TREE_VALUE (parm_level
);
3590 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
3591 int seen_def_arg_p
= 0;
3594 for (i
= 0; i
< ntparms
; ++i
)
3596 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
3598 if (parm
== error_mark_node
)
3601 if (TREE_PURPOSE (parm
))
3603 else if (seen_def_arg_p
)
3605 error ("no default argument for %qD", TREE_VALUE (parm
));
3606 /* For better subsequent error-recovery, we indicate that
3607 there should have been a default argument. */
3608 TREE_PURPOSE (parm
) = error_mark_node
;
3614 && TREE_CODE (decl
) == TYPE_DECL
3616 && template_parameter_pack_p (TREE_VALUE (parm
)))
3618 /* A primary class template can only have one
3619 parameter pack, at the end of the template
3622 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
3623 error ("parameter pack %qE must be at the end of the"
3624 " template parameter list", TREE_VALUE (parm
));
3626 error ("parameter pack %qT must be at the end of the"
3627 " template parameter list",
3628 TREE_TYPE (TREE_VALUE (parm
)));
3630 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
3638 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
3642 /* For an ordinary class template, default template arguments are
3643 allowed at the innermost level, e.g.:
3644 template <class T = int>
3646 but, in a partial specialization, they're not allowed even
3647 there, as we have in [temp.class.spec]:
3649 The template parameter list of a specialization shall not
3650 contain default template argument values.
3652 So, for a partial specialization, or for a function template
3653 (in C++98/C++03), we look at all of them. */
3656 /* But, for a primary class template that is not a partial
3657 specialization we look at all template parameters except the
3659 parms
= TREE_CHAIN (parms
);
3661 /* Figure out what error message to issue. */
3662 if (is_friend_decl
== 2)
3663 msg
= "default template arguments may not be used in function template friend re-declaration";
3664 else if (is_friend_decl
)
3665 msg
= "default template arguments may not be used in function template friend declarations";
3666 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
3667 msg
= "default template arguments may not be used in function templates";
3668 else if (is_partial
)
3669 msg
= "default template arguments may not be used in partial specializations";
3671 msg
= "default argument for template parameter for class enclosing %qD";
3673 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
3674 /* If we're inside a class definition, there's no need to
3675 examine the parameters to the class itself. On the one
3676 hand, they will be checked when the class is defined, and,
3677 on the other, default arguments are valid in things like:
3678 template <class T = double>
3679 struct S { template <class U> void f(U); };
3680 Here the default argument for `S' has no bearing on the
3681 declaration of `f'. */
3682 last_level_to_check
= template_class_depth (current_class_type
) + 1;
3684 /* Check everything. */
3685 last_level_to_check
= 0;
3687 for (parm_level
= parms
;
3688 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
3689 parm_level
= TREE_CHAIN (parm_level
))
3691 tree inner_parms
= TREE_VALUE (parm_level
);
3695 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3696 for (i
= 0; i
< ntparms
; ++i
)
3698 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
3701 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
3706 if (is_friend_decl
== 2)
3713 /* Clear out the default argument so that we are not
3715 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
3719 /* At this point, if we're still interested in issuing messages,
3720 they must apply to classes surrounding the object declared. */
3722 msg
= "default argument for template parameter for class enclosing %qD";
3728 /* Worker for push_template_decl_real, called via
3729 for_each_template_parm. DATA is really an int, indicating the
3730 level of the parameters we are interested in. If T is a template
3731 parameter of that level, return nonzero. */
3734 template_parm_this_level_p (tree t
, void* data
)
3736 int this_level
= *(int *)data
;
3739 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3740 level
= TEMPLATE_PARM_LEVEL (t
);
3742 level
= TEMPLATE_TYPE_LEVEL (t
);
3743 return level
== this_level
;
3746 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3747 parameters given by current_template_args, or reuses a
3748 previously existing one, if appropriate. Returns the DECL, or an
3749 equivalent one, if it is replaced via a call to duplicate_decls.
3751 If IS_FRIEND is true, DECL is a friend declaration. */
3754 push_template_decl_real (tree decl
, bool is_friend
)
3762 int new_template_p
= 0;
3763 /* True if the template is a member template, in the sense of
3765 bool member_template_p
= false;
3767 if (decl
== error_mark_node
)
3770 /* See if this is a partial specialization. */
3771 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
3772 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
3773 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
3775 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
3779 /* For a friend, we want the context of the friend function, not
3780 the type of which it is a friend. */
3781 ctx
= DECL_CONTEXT (decl
);
3782 else if (CP_DECL_CONTEXT (decl
)
3783 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
3784 /* In the case of a virtual function, we want the class in which
3786 ctx
= CP_DECL_CONTEXT (decl
);
3788 /* Otherwise, if we're currently defining some class, the DECL
3789 is assumed to be a member of the class. */
3790 ctx
= current_scope ();
3792 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
3795 if (!DECL_CONTEXT (decl
))
3796 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
3798 /* See if this is a primary template. */
3799 if (is_friend
&& ctx
)
3800 /* A friend template that specifies a class context, i.e.
3801 template <typename T> friend void A<T>::f();
3805 primary
= template_parm_scope_p ();
3809 if (DECL_CLASS_SCOPE_P (decl
))
3810 member_template_p
= true;
3811 if (TREE_CODE (decl
) == TYPE_DECL
3812 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
3813 error ("template class without a name");
3814 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3816 if (DECL_DESTRUCTOR_P (decl
))
3820 A destructor shall not be a member template. */
3821 error ("destructor %qD declared as member template", decl
);
3822 return error_mark_node
;
3824 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
3825 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
3826 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
3827 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
3828 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
3829 == void_list_node
)))
3831 /* [basic.stc.dynamic.allocation]
3833 An allocation function can be a function
3834 template. ... Template allocation functions shall
3835 have two or more parameters. */
3836 error ("invalid template declaration of %qD", decl
);
3837 return error_mark_node
;
3840 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3841 && CLASS_TYPE_P (TREE_TYPE (decl
)))
3845 error ("template declaration of %q#D", decl
);
3846 return error_mark_node
;
3850 /* Check to see that the rules regarding the use of default
3851 arguments are not being violated. */
3852 check_default_tmpl_args (decl
, current_template_parms
,
3853 primary
, is_partial
, /*is_friend_decl=*/0);
3855 /* Ensure that there are no parameter packs in the type of this
3856 declaration that have not been expanded. */
3857 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3859 /* Check each of the arguments individually to see if there are
3860 any bare parameter packs. */
3861 tree type
= TREE_TYPE (decl
);
3862 tree arg
= DECL_ARGUMENTS (decl
);
3863 tree argtype
= TYPE_ARG_TYPES (type
);
3865 while (arg
&& argtype
)
3867 if (!FUNCTION_PARAMETER_PACK_P (arg
)
3868 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
3870 /* This is a PARM_DECL that contains unexpanded parameter
3871 packs. We have already complained about this in the
3872 check_for_bare_parameter_packs call, so just replace
3873 these types with ERROR_MARK_NODE. */
3874 TREE_TYPE (arg
) = error_mark_node
;
3875 TREE_VALUE (argtype
) = error_mark_node
;
3878 arg
= TREE_CHAIN (arg
);
3879 argtype
= TREE_CHAIN (argtype
);
3882 /* Check for bare parameter packs in the return type and the
3883 exception specifiers. */
3884 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
3885 /* Errors were already issued, set return type to int
3886 as the frontend doesn't expect error_mark_node as
3888 TREE_TYPE (type
) = integer_type_node
;
3889 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
3890 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
3892 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
3893 return error_mark_node
;
3896 return process_partial_specialization (decl
);
3898 args
= current_template_args ();
3901 || TREE_CODE (ctx
) == FUNCTION_DECL
3902 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
3903 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
3905 if (DECL_LANG_SPECIFIC (decl
)
3906 && DECL_TEMPLATE_INFO (decl
)
3907 && DECL_TI_TEMPLATE (decl
))
3908 tmpl
= DECL_TI_TEMPLATE (decl
);
3909 /* If DECL is a TYPE_DECL for a class-template, then there won't
3910 be DECL_LANG_SPECIFIC. The information equivalent to
3911 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3912 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3913 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3914 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3916 /* Since a template declaration already existed for this
3917 class-type, we must be redeclaring it here. Make sure
3918 that the redeclaration is valid. */
3919 redeclare_class_template (TREE_TYPE (decl
),
3920 current_template_parms
);
3921 /* We don't need to create a new TEMPLATE_DECL; just use the
3922 one we already had. */
3923 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3927 tmpl
= build_template_decl (decl
, current_template_parms
,
3931 if (DECL_LANG_SPECIFIC (decl
)
3932 && DECL_TEMPLATE_SPECIALIZATION (decl
))
3934 /* A specialization of a member template of a template
3936 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3937 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
3938 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3944 tree a
, t
, current
, parms
;
3946 tree tinfo
= get_template_info (decl
);
3950 error ("template definition of non-template %q#D", decl
);
3951 return error_mark_node
;
3954 tmpl
= TI_TEMPLATE (tinfo
);
3956 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3957 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
3958 && DECL_TEMPLATE_SPECIALIZATION (decl
)
3959 && DECL_MEMBER_TEMPLATE_P (tmpl
))
3963 /* The declaration is a specialization of a member
3964 template, declared outside the class. Therefore, the
3965 innermost template arguments will be NULL, so we
3966 replace them with the arguments determined by the
3967 earlier call to check_explicit_specialization. */
3968 args
= DECL_TI_ARGS (decl
);
3971 = build_template_decl (decl
, current_template_parms
,
3973 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
3974 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
3975 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
3976 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
3977 DECL_TEMPLATE_INFO (new_tmpl
)
3978 = tree_cons (tmpl
, args
, NULL_TREE
);
3980 register_specialization (new_tmpl
,
3981 most_general_template (tmpl
),
3987 /* Make sure the template headers we got make sense. */
3989 parms
= DECL_TEMPLATE_PARMS (tmpl
);
3990 i
= TMPL_PARMS_DEPTH (parms
);
3991 if (TMPL_ARGS_DEPTH (args
) != i
)
3993 error ("expected %d levels of template parms for %q#D, got %d",
3994 i
, decl
, TMPL_ARGS_DEPTH (args
));
3997 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
3999 a
= TMPL_ARGS_LEVEL (args
, i
);
4000 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4002 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4004 if (current
== decl
)
4005 error ("got %d template parameters for %q#D",
4006 TREE_VEC_LENGTH (a
), decl
);
4008 error ("got %d template parameters for %q#T",
4009 TREE_VEC_LENGTH (a
), current
);
4010 error (" but %d required", TREE_VEC_LENGTH (t
));
4011 return error_mark_node
;
4014 if (current
== decl
)
4017 current
= (TYPE_P (current
)
4018 ? TYPE_CONTEXT (current
)
4019 : DECL_CONTEXT (current
));
4022 /* Check that the parms are used in the appropriate qualifying scopes
4023 in the declarator. */
4024 if (!comp_template_args
4026 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4029 template arguments to %qD do not match original template %qD",
4030 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4031 if (!uses_template_parms (TI_ARGS (tinfo
)))
4032 inform ("use template<> for an explicit specialization");
4033 /* Avoid crash in import_export_decl. */
4034 DECL_INTERFACE_KNOWN (decl
) = 1;
4035 return error_mark_node
;
4039 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4040 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4042 /* Push template declarations for global functions and types. Note
4043 that we do not try to push a global template friend declared in a
4044 template class; such a thing may well depend on the template
4045 parameters of the class. */
4046 if (new_template_p
&& !ctx
4047 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4049 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4050 if (tmpl
== error_mark_node
)
4051 return error_mark_node
;
4053 /* Hide template friend classes that haven't been declared yet. */
4054 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4056 DECL_ANTICIPATED (tmpl
) = 1;
4057 DECL_FRIEND_P (tmpl
) = 1;
4063 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4066 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4067 if (DECL_CONV_FN_P (tmpl
))
4069 int depth
= TMPL_PARMS_DEPTH (parms
);
4071 /* It is a conversion operator. See if the type converted to
4072 depends on innermost template operands. */
4074 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4076 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4079 /* Give template template parms a DECL_CONTEXT of the template
4080 for which they are a parameter. */
4081 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4082 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4084 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4085 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4086 DECL_CONTEXT (parm
) = tmpl
;
4090 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4091 back to its most general template. If TMPL is a specialization,
4092 ARGS may only have the innermost set of arguments. Add the missing
4093 argument levels if necessary. */
4094 if (DECL_TEMPLATE_INFO (tmpl
))
4095 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4097 info
= tree_cons (tmpl
, args
, NULL_TREE
);
4099 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4100 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4101 else if (DECL_LANG_SPECIFIC (decl
))
4102 DECL_TEMPLATE_INFO (decl
) = info
;
4104 return DECL_TEMPLATE_RESULT (tmpl
);
4108 push_template_decl (tree decl
)
4110 return push_template_decl_real (decl
, false);
4113 /* Called when a class template TYPE is redeclared with the indicated
4114 template PARMS, e.g.:
4116 template <class T> struct S;
4117 template <class T> struct S {}; */
4120 redeclare_class_template (tree type
, tree parms
)
4126 if (!TYPE_TEMPLATE_INFO (type
))
4128 error ("%qT is not a template type", type
);
4132 tmpl
= TYPE_TI_TEMPLATE (type
);
4133 if (!PRIMARY_TEMPLATE_P (tmpl
))
4134 /* The type is nested in some template class. Nothing to worry
4135 about here; there are no new template parameters for the nested
4141 error ("template specifiers not specified in declaration of %qD",
4146 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4147 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4149 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4151 error ("redeclared with %d template parameter(s)",
4152 TREE_VEC_LENGTH (parms
));
4153 inform ("previous declaration %q+D used %d template parameter(s)",
4154 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4158 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4165 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4166 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4169 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4170 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4171 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4172 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4174 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4176 if (tmpl_parm
!= error_mark_node
4177 && (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4178 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4179 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4180 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4181 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4182 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4183 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4184 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4185 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))))))
4187 error ("template parameter %q+#D", tmpl_parm
);
4188 error ("redeclared here as %q#D", parm
);
4192 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4194 /* We have in [temp.param]:
4196 A template-parameter may not be given default arguments
4197 by two different declarations in the same scope. */
4198 error ("redefinition of default argument for %q#D", parm
);
4199 inform ("%Joriginal definition appeared here", tmpl_parm
);
4203 if (parm_default
!= NULL_TREE
)
4204 /* Update the previous template parameters (which are the ones
4205 that will really count) with the new default value. */
4206 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4207 else if (tmpl_default
!= NULL_TREE
)
4208 /* Update the new parameters, too; they'll be used as the
4209 parameters for any members. */
4210 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4216 /* Simplify EXPR if it is a non-dependent expression. Returns the
4217 (possibly simplified) expression. */
4220 fold_non_dependent_expr (tree expr
)
4222 if (expr
== NULL_TREE
)
4225 /* If we're in a template, but EXPR isn't value dependent, simplify
4226 it. We're supposed to treat:
4228 template <typename T> void f(T[1 + 1]);
4229 template <typename T> void f(T[2]);
4231 as two declarations of the same function, for example. */
4232 if (processing_template_decl
4233 && !type_dependent_expression_p (expr
)
4234 && !value_dependent_expression_p (expr
))
4236 HOST_WIDE_INT saved_processing_template_decl
;
4238 saved_processing_template_decl
= processing_template_decl
;
4239 processing_template_decl
= 0;
4240 expr
= tsubst_copy_and_build (expr
,
4243 /*in_decl=*/NULL_TREE
,
4244 /*function_p=*/false,
4245 /*integral_constant_expression_p=*/true);
4246 processing_template_decl
= saved_processing_template_decl
;
4251 /* EXPR is an expression which is used in a constant-expression context.
4252 For instance, it could be a VAR_DECL with a constant initializer.
4253 Extract the innest constant expression.
4255 This is basically a more powerful version of
4256 integral_constant_value, which can be used also in templates where
4257 initializers can maintain a syntactic rather than semantic form
4258 (even if they are non-dependent, for access-checking purposes). */
4261 fold_decl_constant_value (tree expr
)
4263 tree const_expr
= expr
;
4266 expr
= fold_non_dependent_expr (const_expr
);
4267 const_expr
= integral_constant_value (expr
);
4269 while (expr
!= const_expr
);
4274 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4275 must be a function or a pointer-to-function type, as specified
4276 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4277 and check that the resulting function has external linkage. */
4280 convert_nontype_argument_function (tree type
, tree expr
)
4285 fn
= instantiate_type (type
, fns
, tf_none
);
4286 if (fn
== error_mark_node
)
4287 return error_mark_node
;
4290 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4291 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4292 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4293 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4295 /* [temp.arg.nontype]/1
4297 A template-argument for a non-type, non-template template-parameter
4300 -- the address of an object or function with external linkage. */
4301 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4303 error ("%qE is not a valid template argument for type %qT "
4304 "because function %qD has not external linkage",
4305 expr
, type
, fn_no_ptr
);
4312 /* Attempt to convert the non-type template parameter EXPR to the
4313 indicated TYPE. If the conversion is successful, return the
4314 converted value. If the conversion is unsuccessful, return
4315 NULL_TREE if we issued an error message, or error_mark_node if we
4316 did not. We issue error messages for out-and-out bad template
4317 parameters, but not simply because the conversion failed, since we
4318 might be just trying to do argument deduction. Both TYPE and EXPR
4319 must be non-dependent.
4321 The conversion follows the special rules described in
4322 [temp.arg.nontype], and it is much more strict than an implicit
4325 This function is called twice for each template argument (see
4326 lookup_template_class for a more accurate description of this
4327 problem). This means that we need to handle expressions which
4328 are not valid in a C++ source, but can be created from the
4329 first call (for instance, casts to perform conversions). These
4330 hacks can go away after we fix the double coercion problem. */
4333 convert_nontype_argument (tree type
, tree expr
)
4337 /* Detect immediately string literals as invalid non-type argument.
4338 This special-case is not needed for correctness (we would easily
4339 catch this later), but only to provide better diagnostic for this
4340 common user mistake. As suggested by DR 100, we do not mention
4341 linkage issues in the diagnostic as this is not the point. */
4342 if (TREE_CODE (expr
) == STRING_CST
)
4344 error ("%qE is not a valid template argument for type %qT "
4345 "because string literals can never be used in this context",
4350 /* If we are in a template, EXPR may be non-dependent, but still
4351 have a syntactic, rather than semantic, form. For example, EXPR
4352 might be a SCOPE_REF, rather than the VAR_DECL to which the
4353 SCOPE_REF refers. Preserving the qualifying scope is necessary
4354 so that access checking can be performed when the template is
4355 instantiated -- but here we need the resolved form so that we can
4356 convert the argument. */
4357 expr
= fold_non_dependent_expr (expr
);
4358 if (error_operand_p (expr
))
4359 return error_mark_node
;
4360 expr_type
= TREE_TYPE (expr
);
4362 /* HACK: Due to double coercion, we can get a
4363 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4364 which is the tree that we built on the first call (see
4365 below when coercing to reference to object or to reference to
4366 function). We just strip everything and get to the arg.
4367 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4369 if (TREE_CODE (expr
) == NOP_EXPR
)
4371 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
4373 /* ??? Maybe we could use convert_from_reference here, but we
4374 would need to relax its constraints because the NOP_EXPR
4375 could actually change the type to something more cv-qualified,
4376 and this is not folded by convert_from_reference. */
4377 tree addr
= TREE_OPERAND (expr
, 0);
4378 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
4379 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
4380 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
4381 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4382 (TREE_TYPE (expr_type
),
4383 TREE_TYPE (TREE_TYPE (addr
))));
4385 expr
= TREE_OPERAND (addr
, 0);
4386 expr_type
= TREE_TYPE (expr
);
4389 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4390 parameter is a pointer to object, through decay and
4391 qualification conversion. Let's strip everything. */
4392 else if (TYPE_PTROBV_P (type
))
4395 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
4396 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
4397 /* Skip the ADDR_EXPR only if it is part of the decay for
4398 an array. Otherwise, it is part of the original argument
4399 in the source code. */
4400 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
4401 expr
= TREE_OPERAND (expr
, 0);
4402 expr_type
= TREE_TYPE (expr
);
4406 /* [temp.arg.nontype]/5, bullet 1
4408 For a non-type template-parameter of integral or enumeration type,
4409 integral promotions (_conv.prom_) and integral conversions
4410 (_conv.integral_) are applied. */
4411 if (INTEGRAL_TYPE_P (type
))
4413 if (!INTEGRAL_TYPE_P (expr_type
))
4414 return error_mark_node
;
4416 expr
= fold_decl_constant_value (expr
);
4417 /* Notice that there are constant expressions like '4 % 0' which
4418 do not fold into integer constants. */
4419 if (TREE_CODE (expr
) != INTEGER_CST
)
4421 error ("%qE is not a valid template argument for type %qT "
4422 "because it is a non-constant expression", expr
, type
);
4426 /* At this point, an implicit conversion does what we want,
4427 because we already know that the expression is of integral
4429 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
4430 if (expr
== error_mark_node
)
4431 return error_mark_node
;
4433 /* Conversion was allowed: fold it to a bare integer constant. */
4436 /* [temp.arg.nontype]/5, bullet 2
4438 For a non-type template-parameter of type pointer to object,
4439 qualification conversions (_conv.qual_) and the array-to-pointer
4440 conversion (_conv.array_) are applied. */
4441 else if (TYPE_PTROBV_P (type
))
4443 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4445 A template-argument for a non-type, non-template template-parameter
4446 shall be one of: [...]
4448 -- the name of a non-type template-parameter;
4449 -- the address of an object or function with external linkage, [...]
4450 expressed as "& id-expression" where the & is optional if the name
4451 refers to a function or array, or if the corresponding
4452 template-parameter is a reference.
4454 Here, we do not care about functions, as they are invalid anyway
4455 for a parameter of type pointer-to-object. */
4457 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
4458 /* Non-type template parameters are OK. */
4460 else if (TREE_CODE (expr
) != ADDR_EXPR
4461 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
4463 if (TREE_CODE (expr
) == VAR_DECL
)
4465 error ("%qD is not a valid template argument "
4466 "because %qD is a variable, not the address of "
4471 /* Other values, like integer constants, might be valid
4472 non-type arguments of some other type. */
4473 return error_mark_node
;
4479 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
4480 ? TREE_OPERAND (expr
, 0) : expr
);
4481 if (TREE_CODE (decl
) != VAR_DECL
)
4483 error ("%qE is not a valid template argument of type %qT "
4484 "because %qE is not a variable",
4488 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
4490 error ("%qE is not a valid template argument of type %qT "
4491 "because %qD does not have external linkage",
4497 expr
= decay_conversion (expr
);
4498 if (expr
== error_mark_node
)
4499 return error_mark_node
;
4501 expr
= perform_qualification_conversions (type
, expr
);
4502 if (expr
== error_mark_node
)
4503 return error_mark_node
;
4505 /* [temp.arg.nontype]/5, bullet 3
4507 For a non-type template-parameter of type reference to object, no
4508 conversions apply. The type referred to by the reference may be more
4509 cv-qualified than the (otherwise identical) type of the
4510 template-argument. The template-parameter is bound directly to the
4511 template-argument, which must be an lvalue. */
4512 else if (TYPE_REF_OBJ_P (type
))
4514 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
4516 return error_mark_node
;
4518 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
4520 error ("%qE is not a valid template argument for type %qT "
4521 "because of conflicts in cv-qualification", expr
, type
);
4525 if (!real_lvalue_p (expr
))
4527 error ("%qE is not a valid template argument for type %qT "
4528 "because it is not an lvalue", expr
, type
);
4532 /* [temp.arg.nontype]/1
4534 A template-argument for a non-type, non-template template-parameter
4535 shall be one of: [...]
4537 -- the address of an object or function with external linkage. */
4538 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
4540 error ("%qE is not a valid template argument for type %qT "
4541 "because object %qD has not external linkage",
4546 expr
= build_nop (type
, build_address (expr
));
4548 /* [temp.arg.nontype]/5, bullet 4
4550 For a non-type template-parameter of type pointer to function, only
4551 the function-to-pointer conversion (_conv.func_) is applied. If the
4552 template-argument represents a set of overloaded functions (or a
4553 pointer to such), the matching function is selected from the set
4555 else if (TYPE_PTRFN_P (type
))
4557 /* If the argument is a template-id, we might not have enough
4558 context information to decay the pointer. */
4559 if (!type_unknown_p (expr_type
))
4561 expr
= decay_conversion (expr
);
4562 if (expr
== error_mark_node
)
4563 return error_mark_node
;
4566 expr
= convert_nontype_argument_function (type
, expr
);
4567 if (!expr
|| expr
== error_mark_node
)
4570 /* [temp.arg.nontype]/5, bullet 5
4572 For a non-type template-parameter of type reference to function, no
4573 conversions apply. If the template-argument represents a set of
4574 overloaded functions, the matching function is selected from the set
4576 else if (TYPE_REFFN_P (type
))
4578 if (TREE_CODE (expr
) == ADDR_EXPR
)
4580 error ("%qE is not a valid template argument for type %qT "
4581 "because it is a pointer", expr
, type
);
4582 inform ("try using %qE instead", TREE_OPERAND (expr
, 0));
4586 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
4587 if (!expr
|| expr
== error_mark_node
)
4590 expr
= build_nop (type
, build_address (expr
));
4592 /* [temp.arg.nontype]/5, bullet 6
4594 For a non-type template-parameter of type pointer to member function,
4595 no conversions apply. If the template-argument represents a set of
4596 overloaded member functions, the matching member function is selected
4597 from the set (_over.over_). */
4598 else if (TYPE_PTRMEMFUNC_P (type
))
4600 expr
= instantiate_type (type
, expr
, tf_none
);
4601 if (expr
== error_mark_node
)
4602 return error_mark_node
;
4604 /* There is no way to disable standard conversions in
4605 resolve_address_of_overloaded_function (called by
4606 instantiate_type). It is possible that the call succeeded by
4607 converting &B::I to &D::I (where B is a base of D), so we need
4608 to reject this conversion here.
4610 Actually, even if there was a way to disable standard conversions,
4611 it would still be better to reject them here so that we can
4612 provide a superior diagnostic. */
4613 if (!same_type_p (TREE_TYPE (expr
), type
))
4615 /* Make sure we are just one standard conversion off. */
4616 gcc_assert (can_convert (type
, TREE_TYPE (expr
)));
4617 error ("%qE is not a valid template argument for type %qT "
4618 "because it is of type %qT", expr
, type
,
4620 inform ("standard conversions are not allowed in this context");
4624 /* [temp.arg.nontype]/5, bullet 7
4626 For a non-type template-parameter of type pointer to data member,
4627 qualification conversions (_conv.qual_) are applied. */
4628 else if (TYPE_PTRMEM_P (type
))
4630 expr
= perform_qualification_conversions (type
, expr
);
4631 if (expr
== error_mark_node
)
4634 /* A template non-type parameter must be one of the above. */
4638 /* Sanity check: did we actually convert the argument to the
4640 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
4645 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4646 template template parameters. Both PARM_PARMS and ARG_PARMS are
4647 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4650 Consider the example:
4651 template <class T> class A;
4652 template<template <class U> class TT> class B;
4654 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4655 the parameters to A, and OUTER_ARGS contains A. */
4658 coerce_template_template_parms (tree parm_parms
,
4660 tsubst_flags_t complain
,
4664 int nparms
, nargs
, i
;
4667 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
4668 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
4670 nparms
= TREE_VEC_LENGTH (parm_parms
);
4671 nargs
= TREE_VEC_LENGTH (arg_parms
);
4673 if (nargs
!= nparms
)
4676 for (i
= 0; i
< nparms
; ++i
)
4678 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
4679 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
4682 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
4683 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
4685 if (arg
== NULL_TREE
|| arg
== error_mark_node
4686 || parm
== NULL_TREE
|| parm
== error_mark_node
)
4689 if (TREE_CODE (arg
) != TREE_CODE (parm
))
4692 switch (TREE_CODE (parm
))
4695 /* We encounter instantiations of templates like
4696 template <template <template <class> class> class TT>
4699 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4700 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
4702 if (!coerce_template_template_parms
4703 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
4709 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))
4710 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
)))
4711 /* One is a parameter pack, the other is not. */
4716 /* The tsubst call is used to handle cases such as
4718 template <int> class C {};
4719 template <class T, template <T> class TT> class D {};
4722 i.e. the parameter list of TT depends on earlier parameters. */
4723 if (!dependent_type_p (TREE_TYPE (arg
))
4725 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
4729 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4730 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
)))
4731 /* One is a parameter pack, the other is not. */
4742 /* Verifies that the deduced template arguments (in TARGS) for the
4743 template template parameters (in TPARMS) represent valid bindings,
4744 by comparing the template parameter list of each template argument
4745 to the template parameter list of its corresponding template
4746 template parameter, in accordance with DR150. This
4747 routine can only be called after all template arguments have been
4748 deduced. It will return TRUE if all of the template template
4749 parameter bindings are okay, FALSE otherwise. */
4751 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
4753 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
4755 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
4757 for (i
= 0; i
< ntparms
; ++i
)
4759 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
4760 tree targ
= TREE_VEC_ELT (targs
, i
);
4762 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
4764 tree packed_args
= NULL_TREE
;
4767 if (ARGUMENT_PACK_P (targ
))
4769 /* Look inside the argument pack. */
4770 packed_args
= ARGUMENT_PACK_ARGS (targ
);
4771 len
= TREE_VEC_LENGTH (packed_args
);
4774 for (idx
= 0; idx
< len
; ++idx
)
4776 tree targ_parms
= NULL_TREE
;
4779 /* Extract the next argument from the argument
4781 targ
= TREE_VEC_ELT (packed_args
, idx
);
4783 if (PACK_EXPANSION_P (targ
))
4784 /* Look at the pattern of the pack expansion. */
4785 targ
= PACK_EXPANSION_PATTERN (targ
);
4787 /* Extract the template parameters from the template
4789 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
4790 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
4791 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
4792 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
4794 /* Verify that we can coerce the template template
4795 parameters from the template argument to the template
4796 parameter. This requires an exact match. */
4798 && !coerce_template_template_parms
4799 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
4809 /* Everything is okay. */
4813 /* Convert the indicated template ARG as necessary to match the
4814 indicated template PARM. Returns the converted ARG, or
4815 error_mark_node if the conversion was unsuccessful. Error and
4816 warning messages are issued under control of COMPLAIN. This
4817 conversion is for the Ith parameter in the parameter list. ARGS is
4818 the full set of template arguments deduced so far. */
4821 convert_template_argument (tree parm
,
4824 tsubst_flags_t complain
,
4830 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
4832 if (TREE_CODE (arg
) == TREE_LIST
4833 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
4835 /* The template argument was the name of some
4836 member function. That's usually
4837 invalid, but static members are OK. In any
4838 case, grab the underlying fields/functions
4839 and issue an error later if required. */
4840 orig_arg
= TREE_VALUE (arg
);
4841 TREE_TYPE (arg
) = unknown_type_node
;
4846 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
4847 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
4848 || requires_tmpl_type
);
4850 /* When determining whether an argument pack expansion is a template,
4851 look at the pattern. */
4852 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
4853 arg
= PACK_EXPANSION_PATTERN (arg
);
4856 ((TREE_CODE (arg
) == TEMPLATE_DECL
4857 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
4858 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4859 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
4862 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4863 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
4864 arg
= TYPE_STUB_DECL (arg
);
4866 is_type
= TYPE_P (arg
) || is_tmpl_type
;
4868 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
4869 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
4871 pedwarn ("to refer to a type member of a template parameter, "
4872 "use %<typename %E%>", orig_arg
);
4874 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
4875 TREE_OPERAND (arg
, 1),
4877 complain
& tf_error
);
4881 if (is_type
!= requires_type
)
4885 if (complain
& tf_error
)
4887 error ("type/value mismatch at argument %d in template "
4888 "parameter list for %qD",
4891 error (" expected a constant of type %qT, got %qT",
4893 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
4894 else if (requires_tmpl_type
)
4895 error (" expected a class template, got %qE", orig_arg
);
4897 error (" expected a type, got %qE", orig_arg
);
4900 return error_mark_node
;
4902 if (is_tmpl_type
^ requires_tmpl_type
)
4904 if (in_decl
&& (complain
& tf_error
))
4906 error ("type/value mismatch at argument %d in template "
4907 "parameter list for %qD",
4910 error (" expected a type, got %qT", DECL_NAME (arg
));
4912 error (" expected a class template, got %qT", orig_arg
);
4914 return error_mark_node
;
4919 if (requires_tmpl_type
)
4921 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
4922 /* The number of argument required is not known yet.
4923 Just accept it for now. */
4924 val
= TREE_TYPE (arg
);
4927 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4930 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
4932 if (coerce_template_template_parms (parmparm
, argparm
,
4938 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4940 if (val
!= error_mark_node
)
4942 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
4943 val
= TREE_TYPE (val
);
4944 else if (TREE_CODE (val
) == TYPE_PACK_EXPANSION
4945 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
4947 val
= TREE_TYPE (arg
);
4948 val
= make_pack_expansion (val
);
4954 if (in_decl
&& (complain
& tf_error
))
4956 error ("type/value mismatch at argument %d in "
4957 "template parameter list for %qD",
4959 error (" expected a template of type %qD, got %qD",
4963 val
= error_mark_node
;
4969 /* We only form one instance of each template specialization.
4970 Therefore, if we use a non-canonical variant (i.e., a
4971 typedef), any future messages referring to the type will use
4972 the typedef, which is confusing if those future uses do not
4973 themselves also use the typedef. */
4975 val
= canonical_type_variant (val
);
4979 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
4981 if (invalid_nontype_parm_type_p (t
, complain
))
4982 return error_mark_node
;
4984 if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
4985 /* We used to call digest_init here. However, digest_init
4986 will report errors, which we don't want when complain
4987 is zero. More importantly, digest_init will try too
4988 hard to convert things: for example, `0' should not be
4989 converted to pointer type at this point according to
4990 the standard. Accepting this is not merely an
4991 extension, since deciding whether or not these
4992 conversions can occur is part of determining which
4993 function template to call, or whether a given explicit
4994 argument specification is valid. */
4995 val
= convert_nontype_argument (t
, orig_arg
);
4999 if (val
== NULL_TREE
)
5000 val
= error_mark_node
;
5001 else if (val
== error_mark_node
&& (complain
& tf_error
))
5002 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
5008 /* Coerces the remaining template arguments in INNER_ARGS (from
5009 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5010 Returns the coerced argument pack. PARM_IDX is the position of this
5011 parameter in the template parameter list. ARGS is the original
5012 template argument list. */
5014 coerce_template_parameter_pack (tree parms
,
5022 tsubst_flags_t complain
)
5024 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
5025 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5028 tree packed_types
= NULL_TREE
;
5030 if (arg_idx
> nargs
)
5033 packed_args
= make_tree_vec (nargs
- arg_idx
);
5035 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
5036 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
5038 /* When the template parameter is a non-type template
5039 parameter pack whose type uses parameter packs, we need
5040 to look at each of the template arguments
5041 separately. Build a vector of the types for these
5042 non-type template parameters in PACKED_TYPES. */
5044 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
5045 packed_types
= tsubst_pack_expansion (expansion
, args
,
5048 if (packed_types
== error_mark_node
)
5049 return error_mark_node
;
5051 /* Check that we have the right number of arguments. */
5053 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
5054 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
5057 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
5058 error ("wrong number of template arguments (%d, should be %d)",
5059 nargs
, needed_parms
);
5060 return error_mark_node
;
5063 /* If we aren't able to check the actual arguments now
5064 (because they haven't been expanded yet), we can at least
5065 verify that all of the types used for the non-type
5066 template parameter pack are, in fact, valid for non-type
5067 template parameters. */
5069 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
5071 int j
, len
= TREE_VEC_LENGTH (packed_types
);
5072 for (j
= 0; j
< len
; ++j
)
5074 tree t
= TREE_VEC_ELT (packed_types
, j
);
5075 if (invalid_nontype_parm_type_p (t
, complain
))
5076 return error_mark_node
;
5081 /* Convert the remaining arguments, which will be a part of the
5082 parameter pack "parm". */
5083 for (; arg_idx
< nargs
; ++arg_idx
)
5085 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5086 tree actual_parm
= TREE_VALUE (parm
);
5088 if (packed_types
&& !PACK_EXPANSION_P (arg
))
5090 /* When we have a vector of types (corresponding to the
5091 non-type template parameter pack that uses parameter
5092 packs in its type, as mention above), and the
5093 argument is not an expansion (which expands to a
5094 currently unknown number of arguments), clone the
5095 parm and give it the next type in PACKED_TYPES. */
5096 actual_parm
= copy_node (actual_parm
);
5097 TREE_TYPE (actual_parm
) =
5098 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
5101 if (arg
!= error_mark_node
)
5102 arg
= convert_template_argument (actual_parm
,
5103 arg
, new_args
, complain
, parm_idx
,
5105 if (arg
== error_mark_node
)
5107 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
5110 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
5111 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
5112 argument_pack
= make_node (TYPE_ARGUMENT_PACK
);
5115 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
5116 TREE_TYPE (argument_pack
)
5117 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
5118 TREE_CONSTANT (argument_pack
) = 1;
5121 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
5122 return argument_pack
;
5125 /* Convert all template arguments to their appropriate types, and
5126 return a vector containing the innermost resulting template
5127 arguments. If any error occurs, return error_mark_node. Error and
5128 warning messages are issued under control of COMPLAIN.
5130 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5131 for arguments not specified in ARGS. Otherwise, if
5132 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5133 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5134 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5138 coerce_template_parms (tree parms
,
5141 tsubst_flags_t complain
,
5142 bool require_all_args
,
5143 bool use_default_args
)
5145 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
5148 tree new_inner_args
;
5149 bool saved_skip_evaluation
;
5151 /* When used as a boolean value, indicates whether this is a
5152 variadic template parameter list. Since it's an int, we can also
5153 subtract it from nparms to get the number of non-variadic
5158 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args
));
5160 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5161 nparms
= TREE_VEC_LENGTH (parms
);
5163 /* Determine if there are any parameter packs. */
5164 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
5166 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
5167 if (template_parameter_pack_p (tparm
))
5174 if ((nargs
> nparms
- variadic_p
&& !variadic_p
)
5175 || (nargs
< nparms
- variadic_p
5177 && (!use_default_args
5178 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
5179 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
5181 if (complain
& tf_error
)
5183 const char *or_more
= "";
5186 or_more
= " or more";
5190 error ("wrong number of template arguments (%d, should be %d%s)",
5191 nargs
, nparms
, or_more
);
5194 error ("provided for %q+D", in_decl
);
5197 return error_mark_node
;
5200 /* We need to evaluate the template arguments, even though this
5201 template-id may be nested within a "sizeof". */
5202 saved_skip_evaluation
= skip_evaluation
;
5203 skip_evaluation
= false;
5204 new_inner_args
= make_tree_vec (nparms
);
5205 new_args
= add_outermost_template_args (args
, new_inner_args
);
5206 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
5211 /* Get the Ith template parameter. */
5212 parm
= TREE_VEC_ELT (parms
, parm_idx
);
5214 if (parm
== error_mark_node
)
5216 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
5220 /* Calculate the next argument. */
5221 if (template_parameter_pack_p (TREE_VALUE (parm
)))
5223 /* All remaining arguments will be placed in the
5224 template parameter pack PARM. */
5225 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
5226 inner_args
, arg_idx
,
5230 /* Store this argument. */
5231 if (arg
== error_mark_node
)
5233 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
5235 /* We are done with all of the arguments. */
5240 else if (arg_idx
< nargs
)
5242 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5244 if (arg
&& PACK_EXPANSION_P (arg
))
5246 if (complain
& tf_error
)
5248 /* If ARG is a pack expansion, but PARM is not a
5249 template parameter pack (if it were, we would have
5250 handled it above), we're trying to expand into a
5251 fixed-length argument list. */
5252 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5253 error ("cannot expand %<%E%> into a fixed-length "
5254 "argument list", arg
);
5256 error ("cannot expand %<%T%> into a fixed-length "
5257 "argument list", arg
);
5259 return error_mark_node
;
5262 else if (require_all_args
)
5263 /* There must be a default arg in this case. */
5264 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
5269 if (arg
== error_mark_node
)
5271 if (complain
& tf_error
)
5272 error ("template argument %d is invalid", arg_idx
+ 1);
5275 /* This only occurs if there was an error in the template
5276 parameter list itself (which we would already have
5277 reported) that we are trying to recover from, e.g., a class
5278 template with a parameter list such as
5279 template<typename..., typename>. */
5280 return error_mark_node
;
5282 arg
= convert_template_argument (TREE_VALUE (parm
),
5283 arg
, new_args
, complain
,
5286 if (arg
== error_mark_node
)
5288 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
5290 skip_evaluation
= saved_skip_evaluation
;
5293 return error_mark_node
;
5295 return new_inner_args
;
5298 /* Returns 1 if template args OT and NT are equivalent. */
5301 template_args_equal (tree ot
, tree nt
)
5306 if (TREE_CODE (nt
) == TREE_VEC
)
5307 /* For member templates */
5308 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
5309 else if (PACK_EXPANSION_P (ot
))
5310 return PACK_EXPANSION_P (nt
)
5311 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
5312 PACK_EXPANSION_PATTERN (nt
));
5313 else if (TYPE_P (nt
))
5314 return TYPE_P (ot
) && same_type_p (ot
, nt
);
5315 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
5318 return cp_tree_equal (ot
, nt
);
5321 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5322 of template arguments. Returns 0 otherwise. */
5325 comp_template_args (tree oldargs
, tree newargs
)
5329 oldargs
= expand_template_argument_pack (oldargs
);
5330 newargs
= expand_template_argument_pack (newargs
);
5332 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
5335 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
5337 tree nt
= TREE_VEC_ELT (newargs
, i
);
5338 tree ot
= TREE_VEC_ELT (oldargs
, i
);
5340 if (! template_args_equal (ot
, nt
))
5347 add_pending_template (tree d
)
5349 tree ti
= (TYPE_P (d
)
5350 ? CLASSTYPE_TEMPLATE_INFO (d
)
5351 : DECL_TEMPLATE_INFO (d
));
5352 struct pending_template
*pt
;
5355 if (TI_PENDING_TEMPLATE_FLAG (ti
))
5358 /* We are called both from instantiate_decl, where we've already had a
5359 tinst_level pushed, and instantiate_template, where we haven't.
5361 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
5364 push_tinst_level (d
);
5366 pt
= GGC_NEW (struct pending_template
);
5368 pt
->tinst
= current_tinst_level
;
5369 if (last_pending_template
)
5370 last_pending_template
->next
= pt
;
5372 pending_templates
= pt
;
5374 last_pending_template
= pt
;
5376 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
5383 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5384 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5385 documentation for TEMPLATE_ID_EXPR. */
5388 lookup_template_function (tree fns
, tree arglist
)
5392 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
5393 return error_mark_node
;
5395 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
5396 gcc_assert (fns
&& (is_overloaded_fn (fns
)
5397 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
5399 if (BASELINK_P (fns
))
5401 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
5403 BASELINK_FUNCTIONS (fns
),
5408 type
= TREE_TYPE (fns
);
5409 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
5410 type
= unknown_type_node
;
5412 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
5415 /* Within the scope of a template class S<T>, the name S gets bound
5416 (in build_self_reference) to a TYPE_DECL for the class, not a
5417 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5418 or one of its enclosing classes, and that type is a template,
5419 return the associated TEMPLATE_DECL. Otherwise, the original
5420 DECL is returned. */
5423 maybe_get_template_decl_from_type_decl (tree decl
)
5425 return (decl
!= NULL_TREE
5426 && TREE_CODE (decl
) == TYPE_DECL
5427 && DECL_ARTIFICIAL (decl
)
5428 && CLASS_TYPE_P (TREE_TYPE (decl
))
5429 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
5430 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
5433 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5434 parameters, find the desired type.
5436 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5438 IN_DECL, if non-NULL, is the template declaration we are trying to
5441 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5442 the class we are looking up.
5444 Issue error and warning messages under control of COMPLAIN.
5446 If the template class is really a local class in a template
5447 function, then the FUNCTION_CONTEXT is the function in which it is
5450 ??? Note that this function is currently called *twice* for each
5451 template-id: the first time from the parser, while creating the
5452 incomplete type (finish_template_type), and the second type during the
5453 real instantiation (instantiate_template_class). This is surely something
5454 that we want to avoid. It also causes some problems with argument
5455 coercion (see convert_nontype_argument for more information on this). */
5458 lookup_template_class (tree d1
,
5463 tsubst_flags_t complain
)
5465 tree
template = NULL_TREE
, parmlist
;
5468 timevar_push (TV_NAME_LOOKUP
);
5470 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
5472 tree value
= innermost_non_namespace_value (d1
);
5473 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
5478 push_decl_namespace (context
);
5479 template = lookup_name (d1
);
5480 template = maybe_get_template_decl_from_type_decl (template);
5482 pop_decl_namespace ();
5485 context
= DECL_CONTEXT (template);
5487 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
5489 tree type
= TREE_TYPE (d1
);
5491 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5492 an implicit typename for the second A. Deal with it. */
5493 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
5494 type
= TREE_TYPE (type
);
5496 if (CLASSTYPE_TEMPLATE_INFO (type
))
5498 template = CLASSTYPE_TI_TEMPLATE (type
);
5499 d1
= DECL_NAME (template);
5502 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
5503 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
5505 template = TYPE_TI_TEMPLATE (d1
);
5506 d1
= DECL_NAME (template);
5508 else if (TREE_CODE (d1
) == TEMPLATE_DECL
5509 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
5512 d1
= DECL_NAME (template);
5513 context
= DECL_CONTEXT (template);
5516 /* Issue an error message if we didn't find a template. */
5519 if (complain
& tf_error
)
5520 error ("%qT is not a template", d1
);
5521 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5524 if (TREE_CODE (template) != TEMPLATE_DECL
5525 /* Make sure it's a user visible template, if it was named by
5527 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (template)
5528 && !PRIMARY_TEMPLATE_P (template)))
5530 if (complain
& tf_error
)
5532 error ("non-template type %qT used as a template", d1
);
5534 error ("for template declaration %q+D", in_decl
);
5536 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5539 complain
&= ~tf_user
;
5541 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5543 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5544 template arguments */
5550 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
5552 /* Consider an example where a template template parameter declared as
5554 template <class T, class U = std::allocator<T> > class TT
5556 The template parameter level of T and U are one level larger than
5557 of TT. To proper process the default argument of U, say when an
5558 instantiation `TT<int>' is seen, we need to build the full
5559 arguments containing {int} as the innermost level. Outer levels,
5560 available when not appearing as default template argument, can be
5561 obtained from the arguments of the enclosing template.
5563 Suppose that TT is later substituted with std::vector. The above
5564 instantiation is `TT<int, std::allocator<T> >' with TT at
5565 level 1, and T at level 2, while the template arguments at level 1
5566 becomes {std::vector} and the inner level 2 is {int}. */
5568 outer
= DECL_CONTEXT (template);
5570 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
5571 else if (current_template_parms
)
5572 /* This is an argument of the current template, so we haven't set
5573 DECL_CONTEXT yet. */
5574 outer
= current_template_args ();
5577 arglist
= add_to_template_args (outer
, arglist
);
5579 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
5581 /*require_all_args=*/true,
5582 /*use_default_args=*/true);
5583 if (arglist2
== error_mark_node
5584 || (!uses_template_parms (arglist2
)
5585 && check_instantiated_args (template, arglist2
, complain
)))
5586 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5588 parm
= bind_template_template_parm (TREE_TYPE (template), arglist2
);
5589 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
5593 tree template_type
= TREE_TYPE (template);
5596 tree found
= NULL_TREE
;
5599 int is_partial_instantiation
;
5601 gen_tmpl
= most_general_template (template);
5602 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
5603 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
5604 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5606 if (arg_depth
== 1 && parm_depth
> 1)
5608 /* We've been given an incomplete set of template arguments.
5611 template <class T> struct S1 {
5612 template <class U> struct S2 {};
5613 template <class U> struct S2<U*> {};
5616 we will be called with an ARGLIST of `U*', but the
5617 TEMPLATE will be `template <class T> template
5618 <class U> struct S1<T>::S2'. We must fill in the missing
5621 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5623 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5626 /* Now we should have enough arguments. */
5627 gcc_assert (parm_depth
== arg_depth
);
5629 /* From here on, we're only interested in the most general
5631 template = gen_tmpl
;
5633 /* Calculate the BOUND_ARGS. These will be the args that are
5634 actually tsubst'd into the definition to create the
5638 /* We have multiple levels of arguments to coerce, at once. */
5640 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
5642 tree bound_args
= make_tree_vec (parm_depth
);
5644 for (i
= saved_depth
,
5645 t
= DECL_TEMPLATE_PARMS (template);
5646 i
> 0 && t
!= NULL_TREE
;
5647 --i
, t
= TREE_CHAIN (t
))
5649 tree a
= coerce_template_parms (TREE_VALUE (t
),
5652 /*require_all_args=*/true,
5653 /*use_default_args=*/true);
5655 /* Don't process further if one of the levels fails. */
5656 if (a
== error_mark_node
)
5658 /* Restore the ARGLIST to its full size. */
5659 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5663 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
5665 /* We temporarily reduce the length of the ARGLIST so
5666 that coerce_template_parms will see only the arguments
5667 corresponding to the template parameters it is
5669 TREE_VEC_LENGTH (arglist
)--;
5672 /* Restore the ARGLIST to its full size. */
5673 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5675 arglist
= bound_args
;
5679 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
5680 INNERMOST_TEMPLATE_ARGS (arglist
),
5683 /*require_all_args=*/true,
5684 /*use_default_args=*/true);
5686 if (arglist
== error_mark_node
)
5687 /* We were unable to bind the arguments. */
5688 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5690 /* In the scope of a template class, explicit references to the
5691 template class refer to the type of the template, not any
5692 instantiation of it. For example, in:
5694 template <class T> class C { void f(C<T>); }
5696 the `C<T>' is just the same as `C'. Outside of the
5697 class, however, such a reference is an instantiation. */
5698 if (comp_template_args (TYPE_TI_ARGS (template_type
),
5701 found
= template_type
;
5703 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
5707 for (ctx
= current_class_type
;
5708 ctx
&& TREE_CODE (ctx
) != NAMESPACE_DECL
;
5710 ? TYPE_CONTEXT (ctx
)
5711 : DECL_CONTEXT (ctx
)))
5712 if (TYPE_P (ctx
) && same_type_p (ctx
, template_type
))
5715 /* We're not in the scope of the class, so the
5716 TEMPLATE_TYPE is not the type we want after all. */
5722 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5724 /* If we already have this specialization, return it. */
5725 found
= retrieve_specialization (template, arglist
,
5726 /*class_specializations_p=*/false);
5728 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5730 /* This type is a "partial instantiation" if any of the template
5731 arguments still involve template parameters. Note that we set
5732 IS_PARTIAL_INSTANTIATION for partial specializations as
5734 is_partial_instantiation
= uses_template_parms (arglist
);
5736 /* If the deduced arguments are invalid, then the binding
5738 if (!is_partial_instantiation
5739 && check_instantiated_args (template,
5740 INNERMOST_TEMPLATE_ARGS (arglist
),
5742 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5744 if (!is_partial_instantiation
5745 && !PRIMARY_TEMPLATE_P (template)
5746 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
5748 found
= xref_tag_from_type (TREE_TYPE (template),
5749 DECL_NAME (template),
5750 /*tag_scope=*/ts_global
);
5751 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5754 context
= tsubst (DECL_CONTEXT (template), arglist
,
5757 context
= global_namespace
;
5759 /* Create the type. */
5760 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
5762 if (!is_partial_instantiation
)
5764 set_current_access_from_decl (TYPE_NAME (template_type
));
5765 t
= start_enum (TYPE_IDENTIFIER (template_type
));
5768 /* We don't want to call start_enum for this type, since
5769 the values for the enumeration constants may involve
5770 template parameters. And, no one should be interested
5771 in the enumeration constants for such a type. */
5772 t
= make_node (ENUMERAL_TYPE
);
5776 t
= make_aggr_type (TREE_CODE (template_type
));
5777 CLASSTYPE_DECLARED_CLASS (t
)
5778 = CLASSTYPE_DECLARED_CLASS (template_type
);
5779 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
5780 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
5782 /* A local class. Make sure the decl gets registered properly. */
5783 if (context
== current_function_decl
)
5784 pushtag (DECL_NAME (template), t
, /*tag_scope=*/ts_current
);
5786 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
5787 /* This instantiation is another name for the primary
5788 template type. Set the TYPE_CANONICAL field
5790 TYPE_CANONICAL (t
) = template_type
;
5791 else if (any_template_arguments_need_structural_equality_p (arglist
))
5792 /* Some of the template arguments require structural
5793 equality testing, so this template class requires
5794 structural equality testing. */
5795 SET_TYPE_STRUCTURAL_EQUALITY (t
);
5798 /* If we called start_enum or pushtag above, this information
5799 will already be set up. */
5802 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
5804 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
5805 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
5806 TYPE_STUB_DECL (t
) = type_decl
;
5807 DECL_SOURCE_LOCATION (type_decl
)
5808 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
5811 type_decl
= TYPE_NAME (t
);
5813 TREE_PRIVATE (type_decl
)
5814 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
5815 TREE_PROTECTED (type_decl
)
5816 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
5817 DECL_IN_SYSTEM_HEADER (type_decl
)
5818 = DECL_IN_SYSTEM_HEADER (template);
5819 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
5821 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
5822 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
5825 /* Set up the template information. We have to figure out which
5826 template is the immediate parent if this is a full
5828 if (parm_depth
== 1 || is_partial_instantiation
5829 || !PRIMARY_TEMPLATE_P (template))
5830 /* This case is easy; there are no member templates involved. */
5834 /* This is a full instantiation of a member template. Look
5835 for a partial instantiation of which this is an instance. */
5837 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
5838 found
; found
= TREE_CHAIN (found
))
5841 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
5843 /* We only want partial instantiations, here, not
5844 specializations or full instantiations. */
5845 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
5846 || !uses_template_parms (TREE_VALUE (found
)))
5849 /* Temporarily reduce by one the number of levels in the
5850 ARGLIST and in FOUND so as to avoid comparing the
5851 last set of arguments. */
5852 TREE_VEC_LENGTH (arglist
)--;
5853 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
5855 /* See if the arguments match. If they do, then TMPL is
5856 the partial instantiation we want. */
5857 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
5859 /* Restore the argument vectors to their full size. */
5860 TREE_VEC_LENGTH (arglist
)++;
5861 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
5872 /* There was no partial instantiation. This happens
5873 where C<T> is a member template of A<T> and it's used
5876 template <typename T> struct B { A<T>::C<int> m; };
5879 Create the partial instantiation.
5881 TREE_VEC_LENGTH (arglist
)--;
5882 found
= tsubst (template, arglist
, complain
, NULL_TREE
);
5883 TREE_VEC_LENGTH (arglist
)++;
5887 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
5888 DECL_TEMPLATE_INSTANTIATIONS (template)
5889 = tree_cons (arglist
, t
,
5890 DECL_TEMPLATE_INSTANTIATIONS (template));
5892 if (TREE_CODE (t
) == ENUMERAL_TYPE
5893 && !is_partial_instantiation
)
5894 /* Now that the type has been registered on the instantiations
5895 list, we set up the enumerators. Because the enumeration
5896 constants may involve the enumeration type itself, we make
5897 sure to register the type first, and then create the
5898 constants. That way, doing tsubst_expr for the enumeration
5899 constants won't result in recursive calls here; we'll find
5900 the instantiation and exit above. */
5901 tsubst_enum (template_type
, t
, arglist
);
5903 if (is_partial_instantiation
)
5904 /* If the type makes use of template parameters, the
5905 code that generates debugging information will crash. */
5906 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
5908 /* Possibly limit visibility based on template args. */
5909 TREE_PUBLIC (type_decl
) = 1;
5910 determine_visibility (type_decl
);
5912 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
5914 timevar_pop (TV_NAME_LOOKUP
);
5921 /* True when we should also visit template parameters that occur in
5922 non-deduced contexts. */
5923 bool include_nondeduced_p
;
5924 struct pointer_set_t
*visited
;
5927 /* Called from for_each_template_parm via walk_tree. */
5930 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
5933 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
5934 tree_fn_t fn
= pfd
->fn
;
5935 void *data
= pfd
->data
;
5938 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
5939 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
5940 pfd
->include_nondeduced_p
))
5941 return error_mark_node
;
5943 switch (TREE_CODE (t
))
5946 if (TYPE_PTRMEMFUNC_P (t
))
5952 if (!TYPE_TEMPLATE_INFO (t
))
5954 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
5955 fn
, data
, pfd
->visited
,
5956 pfd
->include_nondeduced_p
))
5957 return error_mark_node
;
5961 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
5962 fn
, data
, pfd
->visited
,
5963 pfd
->include_nondeduced_p
)
5964 || for_each_template_parm (TYPE_MAX_VALUE (t
),
5965 fn
, data
, pfd
->visited
,
5966 pfd
->include_nondeduced_p
))
5967 return error_mark_node
;
5971 /* Since we're not going to walk subtrees, we have to do this
5973 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
5974 pfd
->visited
, pfd
->include_nondeduced_p
))
5975 return error_mark_node
;
5979 /* Check the return type. */
5980 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
5981 pfd
->include_nondeduced_p
))
5982 return error_mark_node
;
5984 /* Check the parameter types. Since default arguments are not
5985 instantiated until they are needed, the TYPE_ARG_TYPES may
5986 contain expressions that involve template parameters. But,
5987 no-one should be looking at them yet. And, once they're
5988 instantiated, they don't contain template parameters, so
5989 there's no point in looking at them then, either. */
5993 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
5994 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
5995 pfd
->visited
, pfd
->include_nondeduced_p
))
5996 return error_mark_node
;
5998 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5999 want walk_tree walking into them itself. */
6005 if (pfd
->include_nondeduced_p
6006 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
6008 pfd
->include_nondeduced_p
))
6009 return error_mark_node
;
6014 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
6015 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
6016 pfd
->visited
, pfd
->include_nondeduced_p
))
6017 return error_mark_node
;
6022 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
6023 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
6024 pfd
->visited
, pfd
->include_nondeduced_p
))
6025 return error_mark_node
;
6026 if (DECL_CONTEXT (t
)
6027 && pfd
->include_nondeduced_p
6028 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
6029 pfd
->visited
, pfd
->include_nondeduced_p
))
6030 return error_mark_node
;
6033 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6034 /* Record template parameters such as `T' inside `TT<T>'. */
6035 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
6036 pfd
->include_nondeduced_p
))
6037 return error_mark_node
;
6040 case TEMPLATE_TEMPLATE_PARM
:
6041 case TEMPLATE_TYPE_PARM
:
6042 case TEMPLATE_PARM_INDEX
:
6043 if (fn
&& (*fn
)(t
, data
))
6044 return error_mark_node
;
6046 return error_mark_node
;
6050 /* A template template parameter is encountered. */
6051 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
6052 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6053 pfd
->include_nondeduced_p
))
6054 return error_mark_node
;
6056 /* Already substituted template template parameter */
6062 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
6064 pfd
->include_nondeduced_p
))
6065 return error_mark_node
;
6069 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
6070 && pfd
->include_nondeduced_p
6071 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6072 (TREE_TYPE (t
)), fn
, data
,
6073 pfd
->visited
, pfd
->include_nondeduced_p
))
6074 return error_mark_node
;
6079 /* If there's no type, then this thing must be some expression
6080 involving template parameters. */
6081 if (!fn
&& !TREE_TYPE (t
))
6082 return error_mark_node
;
6087 case REINTERPRET_CAST_EXPR
:
6088 case CONST_CAST_EXPR
:
6089 case STATIC_CAST_EXPR
:
6090 case DYNAMIC_CAST_EXPR
:
6094 case PSEUDO_DTOR_EXPR
:
6096 return error_mark_node
;
6103 /* We didn't find any template parameters we liked. */
6107 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6108 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6109 call FN with the parameter and the DATA.
6110 If FN returns nonzero, the iteration is terminated, and
6111 for_each_template_parm returns 1. Otherwise, the iteration
6112 continues. If FN never returns a nonzero value, the value
6113 returned by for_each_template_parm is 0. If FN is NULL, it is
6114 considered to be the function which always returns 1.
6116 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6117 parameters that occur in non-deduced contexts. When false, only
6118 visits those template parameters that can be deduced. */
6121 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
6122 struct pointer_set_t
*visited
,
6123 bool include_nondeduced_p
)
6125 struct pair_fn_data pfd
;
6131 pfd
.include_nondeduced_p
= include_nondeduced_p
;
6133 /* Walk the tree. (Conceptually, we would like to walk without
6134 duplicates, but for_each_template_parm_r recursively calls
6135 for_each_template_parm, so we would need to reorganize a fair
6136 bit to use walk_tree_without_duplicates, so we keep our own
6139 pfd
.visited
= visited
;
6141 pfd
.visited
= pointer_set_create ();
6142 result
= cp_walk_tree (&t
,
6143 for_each_template_parm_r
,
6145 pfd
.visited
) != NULL_TREE
;
6150 pointer_set_destroy (pfd
.visited
);
6157 /* Returns true if T depends on any template parameter. */
6160 uses_template_parms (tree t
)
6163 int saved_processing_template_decl
;
6165 saved_processing_template_decl
= processing_template_decl
;
6166 if (!saved_processing_template_decl
)
6167 processing_template_decl
= 1;
6169 dependent_p
= dependent_type_p (t
);
6170 else if (TREE_CODE (t
) == TREE_VEC
)
6171 dependent_p
= any_dependent_template_arguments_p (t
);
6172 else if (TREE_CODE (t
) == TREE_LIST
)
6173 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
6174 || uses_template_parms (TREE_CHAIN (t
)));
6175 else if (TREE_CODE (t
) == TYPE_DECL
)
6176 dependent_p
= dependent_type_p (TREE_TYPE (t
));
6179 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
6180 || TREE_CODE (t
) == OVERLOAD
6181 || TREE_CODE (t
) == BASELINK
6182 || TREE_CODE (t
) == IDENTIFIER_NODE
6183 || TREE_CODE (t
) == TRAIT_EXPR
6184 || CONSTANT_CLASS_P (t
))
6185 dependent_p
= (type_dependent_expression_p (t
)
6186 || value_dependent_expression_p (t
));
6189 gcc_assert (t
== error_mark_node
);
6190 dependent_p
= false;
6193 processing_template_decl
= saved_processing_template_decl
;
6198 /* Returns true if T depends on any template parameter with level LEVEL. */
6201 uses_template_parms_level (tree t
, int level
)
6203 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
6204 /*include_nondeduced_p=*/true);
6207 static int tinst_depth
;
6208 extern int max_tinst_depth
;
6209 #ifdef GATHER_STATISTICS
6212 static int tinst_level_tick
;
6213 static int last_template_error_tick
;
6215 /* We're starting to instantiate D; record the template instantiation context
6216 for diagnostics and to restore it later. */
6219 push_tinst_level (tree d
)
6221 struct tinst_level
*new;
6223 if (tinst_depth
>= max_tinst_depth
)
6225 /* If the instantiation in question still has unbound template parms,
6226 we don't really care if we can't instantiate it, so just return.
6227 This happens with base instantiation for implicit `typename'. */
6228 if (uses_template_parms (d
))
6231 last_template_error_tick
= tinst_level_tick
;
6232 error ("template instantiation depth exceeds maximum of %d (use "
6233 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6234 max_tinst_depth
, d
);
6236 print_instantiation_context ();
6241 new = GGC_NEW (struct tinst_level
);
6243 new->locus
= input_location
;
6244 new->in_system_header_p
= in_system_header
;
6245 new->next
= current_tinst_level
;
6246 current_tinst_level
= new;
6249 #ifdef GATHER_STATISTICS
6250 if (tinst_depth
> depth_reached
)
6251 depth_reached
= tinst_depth
;
6258 /* We're done instantiating this template; return to the instantiation
6262 pop_tinst_level (void)
6264 /* Restore the filename and line number stashed away when we started
6265 this instantiation. */
6266 input_location
= current_tinst_level
->locus
;
6267 in_system_header
= current_tinst_level
->in_system_header_p
;
6268 current_tinst_level
= current_tinst_level
->next
;
6273 /* We're instantiating a deferred template; restore the template
6274 instantiation context in which the instantiation was requested, which
6275 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6278 reopen_tinst_level (struct tinst_level
*level
)
6280 struct tinst_level
*t
;
6283 for (t
= level
; t
; t
= t
->next
)
6286 current_tinst_level
= level
;
6291 /* Returns the TINST_LEVEL which gives the original instantiation
6294 struct tinst_level
*
6295 outermost_tinst_level (void)
6297 struct tinst_level
*level
= current_tinst_level
;
6300 level
= level
->next
;
6304 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6305 vector of template arguments, as for tsubst.
6307 Returns an appropriate tsubst'd friend declaration. */
6310 tsubst_friend_function (tree decl
, tree args
)
6314 if (TREE_CODE (decl
) == FUNCTION_DECL
6315 && DECL_TEMPLATE_INSTANTIATION (decl
)
6316 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
6317 /* This was a friend declared with an explicit template
6318 argument list, e.g.:
6322 to indicate that f was a template instantiation, not a new
6323 function declaration. Now, we have to figure out what
6324 instantiation of what template. */
6326 tree template_id
, arglist
, fns
;
6329 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
6331 /* Friend functions are looked up in the containing namespace scope.
6332 We must enter that scope, to avoid finding member functions of the
6333 current cless with same name. */
6334 push_nested_namespace (ns
);
6335 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
6336 tf_warning_or_error
, NULL_TREE
,
6337 /*integral_constant_expression_p=*/false);
6338 pop_nested_namespace (ns
);
6339 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
6340 tf_warning_or_error
, NULL_TREE
);
6341 template_id
= lookup_template_function (fns
, arglist
);
6343 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6344 tmpl
= determine_specialization (template_id
, new_friend
,
6346 /*need_member_template=*/0,
6347 TREE_VEC_LENGTH (args
),
6349 return instantiate_template (tmpl
, new_args
, tf_error
);
6352 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6354 /* The NEW_FRIEND will look like an instantiation, to the
6355 compiler, but is not an instantiation from the point of view of
6356 the language. For example, we might have had:
6358 template <class T> struct S {
6359 template <class U> friend void f(T, U);
6362 Then, in S<int>, template <class U> void f(int, U) is not an
6363 instantiation of anything. */
6364 if (new_friend
== error_mark_node
)
6365 return error_mark_node
;
6367 DECL_USE_TEMPLATE (new_friend
) = 0;
6368 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6370 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
6371 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
6372 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
6375 /* The mangled name for the NEW_FRIEND is incorrect. The function
6376 is not a template instantiation and should not be mangled like
6377 one. Therefore, we forget the mangling here; we'll recompute it
6378 later if we need it. */
6379 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
6381 SET_DECL_RTL (new_friend
, NULL_RTX
);
6382 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
6385 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
6388 tree new_friend_template_info
;
6389 tree new_friend_result_template_info
;
6391 int new_friend_is_defn
;
6393 /* We must save some information from NEW_FRIEND before calling
6394 duplicate decls since that function will free NEW_FRIEND if
6396 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
6397 new_friend_is_defn
=
6398 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6399 (template_for_substitution (new_friend
)))
6401 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
6403 /* This declaration is a `primary' template. */
6404 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
6406 new_friend_result_template_info
6407 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
6410 new_friend_result_template_info
= NULL_TREE
;
6412 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6413 if (new_friend_is_defn
)
6414 DECL_INITIAL (new_friend
) = error_mark_node
;
6416 /* Inside pushdecl_namespace_level, we will push into the
6417 current namespace. However, the friend function should go
6418 into the namespace of the template. */
6419 ns
= decl_namespace_context (new_friend
);
6420 push_nested_namespace (ns
);
6421 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
6422 pop_nested_namespace (ns
);
6424 if (old_decl
== error_mark_node
)
6425 return error_mark_node
;
6427 if (old_decl
!= new_friend
)
6429 /* This new friend declaration matched an existing
6430 declaration. For example, given:
6432 template <class T> void f(T);
6433 template <class U> class C {
6434 template <class T> friend void f(T) {}
6437 the friend declaration actually provides the definition
6438 of `f', once C has been instantiated for some type. So,
6439 old_decl will be the out-of-class template declaration,
6440 while new_friend is the in-class definition.
6442 But, if `f' was called before this point, the
6443 instantiation of `f' will have DECL_TI_ARGS corresponding
6444 to `T' but not to `U', references to which might appear
6445 in the definition of `f'. Previously, the most general
6446 template for an instantiation of `f' was the out-of-class
6447 version; now it is the in-class version. Therefore, we
6448 run through all specialization of `f', adding to their
6449 DECL_TI_ARGS appropriately. In particular, they need a
6450 new set of outer arguments, corresponding to the
6451 arguments for this class instantiation.
6453 The same situation can arise with something like this:
6456 template <class T> class C {
6460 when `C<int>' is instantiated. Now, `f(int)' is defined
6463 if (!new_friend_is_defn
)
6464 /* On the other hand, if the in-class declaration does
6465 *not* provide a definition, then we don't want to alter
6466 existing definitions. We can just leave everything
6471 /* Overwrite whatever template info was there before, if
6472 any, with the new template information pertaining to
6474 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
6476 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
6477 reregister_specialization (new_friend
,
6478 most_general_template (old_decl
),
6483 tree new_friend_args
;
6485 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
6486 = new_friend_result_template_info
;
6488 new_friend_args
= TI_ARGS (new_friend_template_info
);
6489 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
6493 tree spec
= TREE_VALUE (t
);
6496 = add_outermost_template_args (new_friend_args
,
6497 DECL_TI_ARGS (spec
));
6500 /* Now, since specializations are always supposed to
6501 hang off of the most general template, we must move
6503 t
= most_general_template (old_decl
);
6506 DECL_TEMPLATE_SPECIALIZATIONS (t
)
6507 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
6508 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
6509 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
6514 /* The information from NEW_FRIEND has been merged into OLD_DECL
6515 by duplicate_decls. */
6516 new_friend
= old_decl
;
6521 tree context
= DECL_CONTEXT (new_friend
);
6525 template <class T> class C {
6526 template <class U> friend void C1<U>::f (); // case 1
6527 friend void C2<T>::f (); // case 2
6529 we only need to make sure CONTEXT is a complete type for
6530 case 2. To distinguish between the two cases, we note that
6531 CONTEXT of case 1 remains dependent type after tsubst while
6532 this isn't true for case 2. */
6533 ++processing_template_decl
;
6534 dependent_p
= dependent_type_p (context
);
6535 --processing_template_decl
;
6538 && !complete_type_or_else (context
, NULL_TREE
))
6539 return error_mark_node
;
6541 if (COMPLETE_TYPE_P (context
))
6543 /* Check to see that the declaration is really present, and,
6544 possibly obtain an improved declaration. */
6545 tree fn
= check_classfn (context
,
6546 new_friend
, NULL_TREE
);
6556 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6557 template arguments, as for tsubst.
6559 Returns an appropriate tsubst'd friend type or error_mark_node on
6563 tsubst_friend_class (tree friend_tmpl
, tree args
)
6569 context
= DECL_CONTEXT (friend_tmpl
);
6573 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6574 push_nested_namespace (context
);
6576 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
6579 /* Look for a class template declaration. We look for hidden names
6580 because two friend declarations of the same template are the
6581 same. For example, in:
6584 template <typename> friend class F;
6586 template <typename> struct B {
6587 template <typename> friend class F;
6590 both F templates are the same. */
6591 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
6592 /*block_p=*/true, 0,
6593 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
6595 /* But, if we don't find one, it might be because we're in a
6596 situation like this:
6604 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6605 for `S<int>', not the TEMPLATE_DECL. */
6606 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
6608 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
6609 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
6612 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
6614 /* The friend template has already been declared. Just
6615 check to see that the declarations match, and install any new
6616 default parameters. We must tsubst the default parameters,
6617 of course. We only need the innermost template parameters
6618 because that is all that redeclare_class_template will look
6620 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
6621 > TMPL_ARGS_DEPTH (args
))
6624 location_t saved_input_location
;
6625 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
6626 args
, tf_warning_or_error
);
6628 saved_input_location
= input_location
;
6629 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
6630 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
6631 input_location
= saved_input_location
;
6635 friend_type
= TREE_TYPE (tmpl
);
6639 /* The friend template has not already been declared. In this
6640 case, the instantiation of the template class will cause the
6641 injection of this template into the global scope. */
6642 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
6643 if (tmpl
== error_mark_node
)
6644 return error_mark_node
;
6646 /* The new TMPL is not an instantiation of anything, so we
6647 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6648 the new type because that is supposed to be the corresponding
6649 template decl, i.e., TMPL. */
6650 DECL_USE_TEMPLATE (tmpl
) = 0;
6651 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
6652 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
6653 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
6654 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
6656 /* Inject this template into the global scope. */
6657 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
6662 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6663 pop_nested_namespace (context
);
6665 pop_nested_class ();
6671 /* Returns zero if TYPE cannot be completed later due to circularity.
6672 Otherwise returns one. */
6675 can_complete_type_without_circularity (tree type
)
6677 if (type
== NULL_TREE
|| type
== error_mark_node
)
6679 else if (COMPLETE_TYPE_P (type
))
6681 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
6682 return can_complete_type_without_circularity (TREE_TYPE (type
));
6683 else if (CLASS_TYPE_P (type
)
6684 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
6690 /* Apply any attributes which had to be deferred until instantiation
6691 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6692 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6695 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
6696 tree args
, tsubst_flags_t complain
, tree in_decl
)
6698 tree last_dep
= NULL_TREE
;
6702 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
6703 if (ATTR_IS_DEPENDENT (t
))
6706 attributes
= copy_list (attributes
);
6710 if (DECL_P (*decl_p
))
6712 if (TREE_TYPE (*decl_p
) == error_mark_node
)
6714 p
= &DECL_ATTRIBUTES (*decl_p
);
6717 p
= &TYPE_ATTRIBUTES (*decl_p
);
6721 tree late_attrs
= NULL_TREE
;
6722 tree
*q
= &late_attrs
;
6724 for (*p
= attributes
; *p
; )
6727 if (ATTR_IS_DEPENDENT (t
))
6729 *p
= TREE_CHAIN (t
);
6730 TREE_CHAIN (t
) = NULL_TREE
;
6732 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
6733 /*integral_constant_expression_p=*/false);
6735 q
= &TREE_CHAIN (t
);
6738 p
= &TREE_CHAIN (t
);
6741 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
6746 instantiate_class_template (tree type
)
6748 tree
template, args
, pattern
, t
, member
;
6753 if (type
== error_mark_node
)
6754 return error_mark_node
;
6756 if (TYPE_BEING_DEFINED (type
)
6757 || COMPLETE_TYPE_P (type
)
6758 || dependent_type_p (type
))
6761 /* Figure out which template is being instantiated. */
6762 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
6763 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL
);
6765 /* Determine what specialization of the original template to
6767 t
= most_specialized_class (type
, template);
6768 if (t
== error_mark_node
)
6770 TYPE_BEING_DEFINED (type
) = 1;
6771 return error_mark_node
;
6775 /* This TYPE is actually an instantiation of a partial
6776 specialization. We replace the innermost set of ARGS with
6777 the arguments appropriate for substitution. For example,
6780 template <class T> struct S {};
6781 template <class T> struct S<T*> {};
6783 and supposing that we are instantiating S<int*>, ARGS will
6784 presently be {int*} -- but we need {int}. */
6785 pattern
= TREE_TYPE (t
);
6786 args
= TREE_PURPOSE (t
);
6790 pattern
= TREE_TYPE (template);
6791 args
= CLASSTYPE_TI_ARGS (type
);
6794 /* If the template we're instantiating is incomplete, then clearly
6795 there's nothing we can do. */
6796 if (!COMPLETE_TYPE_P (pattern
))
6799 /* If we've recursively instantiated too many templates, stop. */
6800 if (! push_tinst_level (type
))
6803 /* Now we're really doing the instantiation. Mark the type as in
6804 the process of being defined. */
6805 TYPE_BEING_DEFINED (type
) = 1;
6807 /* We may be in the middle of deferred access check. Disable
6809 push_deferring_access_checks (dk_no_deferred
);
6811 push_to_top_level ();
6813 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
6815 /* Set the input location to the template definition. This is needed
6816 if tsubsting causes an error. */
6817 typedecl
= TYPE_MAIN_DECL (type
);
6818 input_location
= DECL_SOURCE_LOCATION (typedecl
);
6819 in_system_header
= DECL_IN_SYSTEM_HEADER (typedecl
);
6821 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
6822 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
6823 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
6824 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
6825 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
6826 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
6827 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
6828 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
6829 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
6830 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
6831 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
6832 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
6833 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
6834 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
6835 if (ANON_AGGR_TYPE_P (pattern
))
6836 SET_ANON_AGGR_TYPE_P (type
);
6837 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
6839 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
6840 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
6843 pbinfo
= TYPE_BINFO (pattern
);
6845 /* We should never instantiate a nested class before its enclosing
6846 class; we need to look up the nested class by name before we can
6847 instantiate it, and that lookup should instantiate the enclosing
6849 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
6850 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
6851 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
6853 base_list
= NULL_TREE
;
6854 if (BINFO_N_BASE_BINFOS (pbinfo
))
6857 tree context
= TYPE_CONTEXT (type
);
6861 /* We must enter the scope containing the type, as that is where
6862 the accessibility of types named in dependent bases are
6864 pushed_scope
= push_scope (context
? context
: global_namespace
);
6866 /* Substitute into each of the bases to determine the actual
6868 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
6871 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
6872 tree expanded_bases
= NULL_TREE
;
6875 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
6878 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
6879 args
, tf_error
, NULL_TREE
);
6880 if (expanded_bases
== error_mark_node
)
6883 len
= TREE_VEC_LENGTH (expanded_bases
);
6886 for (idx
= 0; idx
< len
; idx
++)
6889 /* Extract the already-expanded base class. */
6890 base
= TREE_VEC_ELT (expanded_bases
, idx
);
6892 /* Substitute to figure out the base class. */
6893 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
6896 if (base
== error_mark_node
)
6899 base_list
= tree_cons (access
, base
, base_list
);
6900 if (BINFO_VIRTUAL_P (pbase_binfo
))
6901 TREE_TYPE (base_list
) = integer_type_node
;
6905 /* The list is now in reverse order; correct that. */
6906 base_list
= nreverse (base_list
);
6909 pop_scope (pushed_scope
);
6911 /* Now call xref_basetypes to set up all the base-class
6913 xref_basetypes (type
, base_list
);
6915 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
6916 (int) ATTR_FLAG_TYPE_IN_PLACE
,
6917 args
, tf_error
, NULL_TREE
);
6919 /* Now that our base classes are set up, enter the scope of the
6920 class, so that name lookups into base classes, etc. will work
6921 correctly. This is precisely analogous to what we do in
6922 begin_class_definition when defining an ordinary non-template
6923 class, except we also need to push the enclosing classes. */
6924 push_nested_class (type
);
6926 /* Now members are processed in the order of declaration. */
6927 for (member
= CLASSTYPE_DECL_LIST (pattern
);
6928 member
; member
= TREE_CHAIN (member
))
6930 tree t
= TREE_VALUE (member
);
6932 if (TREE_PURPOSE (member
))
6936 /* Build new CLASSTYPE_NESTED_UTDS. */
6939 bool class_template_p
;
6941 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
6942 && TYPE_LANG_SPECIFIC (t
)
6943 && CLASSTYPE_IS_TEMPLATE (t
));
6944 /* If the member is a class template, then -- even after
6945 substitution -- there may be dependent types in the
6946 template argument list for the class. We increment
6947 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6948 that function will assume that no types are dependent
6949 when outside of a template. */
6950 if (class_template_p
)
6951 ++processing_template_decl
;
6952 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
6953 if (class_template_p
)
6954 --processing_template_decl
;
6955 if (newtag
== error_mark_node
)
6958 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
6960 tree name
= TYPE_IDENTIFIER (t
);
6962 if (class_template_p
)
6963 /* Unfortunately, lookup_template_class sets
6964 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6965 instantiation (i.e., for the type of a member
6966 template class nested within a template class.)
6967 This behavior is required for
6968 maybe_process_partial_specialization to work
6969 correctly, but is not accurate in this case;
6970 the TAG is not an instantiation of anything.
6971 (The corresponding TEMPLATE_DECL is an
6972 instantiation, but the TYPE is not.) */
6973 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
6975 /* Now, we call pushtag to put this NEWTAG into the scope of
6976 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6977 pushtag calling push_template_decl. We don't have to do
6978 this for enums because it will already have been done in
6981 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
6982 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
6985 else if (TREE_CODE (t
) == FUNCTION_DECL
6986 || DECL_FUNCTION_TEMPLATE_P (t
))
6988 /* Build new TYPE_METHODS. */
6991 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6992 ++processing_template_decl
;
6993 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
6994 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6995 --processing_template_decl
;
6996 set_current_access_from_decl (r
);
6997 finish_member_declaration (r
);
7001 /* Build new TYPE_FIELDS. */
7002 if (TREE_CODE (t
) == STATIC_ASSERT
)
7005 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
7006 tf_warning_or_error
, NULL_TREE
,
7007 /*integral_constant_expression_p=*/true);
7008 finish_static_assert (condition
,
7009 STATIC_ASSERT_MESSAGE (t
),
7010 STATIC_ASSERT_SOURCE_LOCATION (t
),
7013 else if (TREE_CODE (t
) != CONST_DECL
)
7017 /* The the file and line for this declaration, to
7018 assist in error message reporting. Since we
7019 called push_tinst_level above, we don't need to
7021 input_location
= DECL_SOURCE_LOCATION (t
);
7023 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7024 ++processing_template_decl
;
7025 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
7026 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7027 --processing_template_decl
;
7028 if (TREE_CODE (r
) == VAR_DECL
)
7032 [t]he initialization (and any associated
7033 side-effects) of a static data member does
7034 not occur unless the static data member is
7035 itself used in a way that requires the
7036 definition of the static data member to
7039 Therefore, we do not substitute into the
7040 initialized for the static data member here. */
7041 finish_static_data_member_decl
7044 /*init_const_expr_p=*/false,
7045 /*asmspec_tree=*/NULL_TREE
,
7047 if (DECL_INITIALIZED_IN_CLASS_P (r
))
7048 check_static_variable_definition (r
, TREE_TYPE (r
));
7050 else if (TREE_CODE (r
) == FIELD_DECL
)
7052 /* Determine whether R has a valid type and can be
7053 completed later. If R is invalid, then it is
7054 replaced by error_mark_node so that it will not be
7055 added to TYPE_FIELDS. */
7056 tree rtype
= TREE_TYPE (r
);
7057 if (can_complete_type_without_circularity (rtype
))
7058 complete_type (rtype
);
7060 if (!COMPLETE_TYPE_P (rtype
))
7062 cxx_incomplete_type_error (r
, rtype
);
7063 r
= error_mark_node
;
7067 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7068 such a thing will already have been added to the field
7069 list by tsubst_enum in finish_member_declaration in the
7070 CLASSTYPE_NESTED_UTDS case above. */
7071 if (!(TREE_CODE (r
) == TYPE_DECL
7072 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
7073 && DECL_ARTIFICIAL (r
)))
7075 set_current_access_from_decl (r
);
7076 finish_member_declaration (r
);
7083 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
7085 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7087 tree friend_type
= t
;
7088 bool adjust_processing_template_decl
= false;
7090 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7092 /* template <class T> friend class C; */
7093 friend_type
= tsubst_friend_class (friend_type
, args
);
7094 adjust_processing_template_decl
= true;
7096 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
7098 /* template <class T> friend class C::D; */
7099 friend_type
= tsubst (friend_type
, args
,
7100 tf_warning_or_error
, NULL_TREE
);
7101 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7102 friend_type
= TREE_TYPE (friend_type
);
7103 adjust_processing_template_decl
= true;
7105 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
7107 /* This could be either
7111 when dependent_type_p is false or
7113 template <class U> friend class T::C;
7116 friend_type
= tsubst (friend_type
, args
,
7117 tf_warning_or_error
, NULL_TREE
);
7118 /* Bump processing_template_decl for correct
7119 dependent_type_p calculation. */
7120 ++processing_template_decl
;
7121 if (dependent_type_p (friend_type
))
7122 adjust_processing_template_decl
= true;
7123 --processing_template_decl
;
7125 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
7126 && hidden_name_p (TYPE_NAME (friend_type
)))
7130 where C hasn't been declared yet. Let's lookup name
7131 from namespace scope directly, bypassing any name that
7132 come from dependent base class. */
7133 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
7135 /* The call to xref_tag_from_type does injection for friend
7137 push_nested_namespace (ns
);
7139 xref_tag_from_type (friend_type
, NULL_TREE
,
7140 /*tag_scope=*/ts_current
);
7141 pop_nested_namespace (ns
);
7143 else if (uses_template_parms (friend_type
))
7144 /* friend class C<T>; */
7145 friend_type
= tsubst (friend_type
, args
,
7146 tf_warning_or_error
, NULL_TREE
);
7151 where C is already declared or
7153 friend class C<int>;
7155 We don't have to do anything in these cases. */
7157 if (adjust_processing_template_decl
)
7158 /* Trick make_friend_class into realizing that the friend
7159 we're adding is a template, not an ordinary class. It's
7160 important that we use make_friend_class since it will
7161 perform some error-checking and output cross-reference
7163 ++processing_template_decl
;
7165 if (friend_type
!= error_mark_node
)
7166 make_friend_class (type
, friend_type
, /*complain=*/false);
7168 if (adjust_processing_template_decl
)
7169 --processing_template_decl
;
7173 /* Build new DECL_FRIENDLIST. */
7176 /* The the file and line for this declaration, to
7177 assist in error message reporting. Since we
7178 called push_tinst_level above, we don't need to
7180 input_location
= DECL_SOURCE_LOCATION (t
);
7182 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7184 ++processing_template_decl
;
7185 push_deferring_access_checks (dk_no_check
);
7188 r
= tsubst_friend_function (t
, args
);
7189 add_friend (type
, r
, /*complain=*/false);
7190 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7192 pop_deferring_access_checks ();
7193 --processing_template_decl
;
7199 /* Set the file and line number information to whatever is given for
7200 the class itself. This puts error messages involving generated
7201 implicit functions at a predictable point, and the same point
7202 that would be used for non-template classes. */
7203 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7205 unreverse_member_declarations (type
);
7206 finish_struct_1 (type
);
7207 TYPE_BEING_DEFINED (type
) = 0;
7209 /* Now that the class is complete, instantiate default arguments for
7210 any member functions. We don't do this earlier because the
7211 default arguments may reference members of the class. */
7212 if (!PRIMARY_TEMPLATE_P (template))
7213 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
7214 if (TREE_CODE (t
) == FUNCTION_DECL
7215 /* Implicitly generated member functions will not have template
7216 information; they are not instantiations, but instead are
7217 created "fresh" for each instantiation. */
7218 && DECL_TEMPLATE_INFO (t
))
7219 tsubst_default_arguments (t
);
7221 pop_nested_class ();
7222 pop_from_top_level ();
7223 pop_deferring_access_checks ();
7226 /* The vtable for a template class can be emitted in any translation
7227 unit in which the class is instantiated. When there is no key
7228 method, however, finish_struct_1 will already have added TYPE to
7229 the keyed_classes list. */
7230 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
7231 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
7237 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7243 else if (TYPE_P (t
))
7244 r
= tsubst (t
, args
, complain
, in_decl
);
7247 r
= tsubst_expr (t
, args
, complain
, in_decl
,
7248 /*integral_constant_expression_p=*/true);
7249 r
= fold_non_dependent_expr (r
);
7254 /* Substitute ARGS into T, which is an pack expansion
7255 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7256 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7257 (if only a partial substitution could be performed) or
7258 ERROR_MARK_NODE if there was an error. */
7260 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
7264 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
7265 tree first_arg_pack
; int i
, len
= -1;
7269 gcc_assert (PACK_EXPANSION_P (t
));
7270 pattern
= PACK_EXPANSION_PATTERN (t
);
7272 /* Determine the argument packs that will instantiate the parameter
7273 packs used in the expansion expression. While we're at it,
7274 compute the number of arguments to be expanded and make sure it
7276 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
7277 pack
= TREE_CHAIN (pack
))
7279 tree parm_pack
= TREE_VALUE (pack
);
7280 tree arg_pack
= NULL_TREE
;
7281 tree orig_arg
= NULL_TREE
;
7283 if (TREE_CODE (parm_pack
) == PARM_DECL
)
7284 arg_pack
= retrieve_local_specialization (parm_pack
);
7287 int level
, idx
, levels
;
7288 template_parm_level_and_index (parm_pack
, &level
, &idx
);
7290 levels
= TMPL_ARGS_DEPTH (args
);
7291 if (level
<= levels
)
7292 arg_pack
= TMPL_ARG (args
, level
, idx
);
7295 orig_arg
= arg_pack
;
7296 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
7297 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
7299 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
7300 /* This can only happen if we forget to expand an argument
7301 pack somewhere else. Just return an error, silently. */
7303 result
= make_tree_vec (1);
7304 TREE_VEC_ELT (result
, 0) = error_mark_node
;
7309 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
7310 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
7312 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
7313 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
7314 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
7315 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
7316 /* The argument pack that the parameter maps to is just an
7317 expansion of the parameter itself, such as one would
7318 find in the implicit typedef of a class inside the
7319 class itself. Consider this parameter "unsubstituted",
7320 so that we will maintain the outer pack expansion. */
7321 arg_pack
= NULL_TREE
;
7327 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
7329 /* It's all-or-nothing with incomplete argument packs. */
7330 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7331 return error_mark_node
;
7333 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7339 first_arg_pack
= arg_pack
;
7341 else if (len
!= my_len
)
7343 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
7344 error ("mismatched argument pack lengths while expanding "
7348 error ("mismatched argument pack lengths while expanding "
7351 return error_mark_node
;
7354 /* Keep track of the parameter packs and their corresponding
7356 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
7357 TREE_TYPE (packs
) = orig_arg
;
7360 /* We can't substitute for this parameter pack. */
7361 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
7363 unsubstituted_packs
);
7366 /* We cannot expand this expansion expression, because we don't have
7367 all of the argument packs we need. Substitute into the pattern
7368 and return a PACK_EXPANSION_*. The caller will need to deal with
7370 if (unsubstituted_packs
)
7371 return make_pack_expansion (tsubst (pattern
, args
, complain
,
7374 /* We could not find any argument packs that work. */
7376 return error_mark_node
;
7378 /* For each argument in each argument pack, substitute into the
7380 result
= make_tree_vec (len
+ incomplete
);
7381 for (i
= 0; i
< len
+ incomplete
; ++i
)
7383 /* For parameter pack, change the substitution of the parameter
7384 pack to the ith argument in its argument pack, then expand
7386 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7388 tree parm
= TREE_PURPOSE (pack
);
7390 if (TREE_CODE (parm
) == PARM_DECL
)
7392 /* Select the Ith argument from the pack. */
7393 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
7394 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
7395 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
7397 register_local_specialization (arg
, parm
);
7403 template_parm_level_and_index (parm
, &level
, &idx
);
7407 /* Select the Ith argument from the pack. */
7408 value
= make_node (ARGUMENT_PACK_SELECT
);
7409 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
7410 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
7413 /* Update the corresponding argument. */
7414 TMPL_ARG (args
, level
, idx
) = value
;
7418 /* Substitute into the PATTERN with the altered arguments. */
7419 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
7420 TREE_VEC_ELT (result
, i
) =
7421 tsubst_expr (pattern
, args
, complain
, in_decl
,
7422 /*integral_constant_expression_p=*/false);
7424 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
7427 /* When we have incomplete argument packs, the last "expanded"
7428 result is itself a pack expansion, which allows us
7429 to deduce more arguments. */
7430 TREE_VEC_ELT (result
, i
) =
7431 make_pack_expansion (TREE_VEC_ELT (result
, i
));
7433 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
7435 result
= error_mark_node
;
7440 /* Update ARGS to restore the substitution from parameter packs to
7441 their argument packs. */
7442 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7444 tree parm
= TREE_PURPOSE (pack
);
7446 if (TREE_CODE (parm
) == PARM_DECL
)
7447 register_local_specialization (TREE_TYPE (pack
), parm
);
7451 template_parm_level_and_index (parm
, &level
, &idx
);
7453 /* Update the corresponding argument. */
7454 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
7455 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
7458 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
7465 /* Substitute ARGS into the vector or list of template arguments T. */
7468 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7471 int len
= TREE_VEC_LENGTH (t
);
7472 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
7473 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
7475 for (i
= 0; i
< len
; i
++)
7477 tree orig_arg
= TREE_VEC_ELT (t
, i
);
7480 if (TREE_CODE (orig_arg
) == TREE_VEC
)
7481 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
7482 else if (PACK_EXPANSION_P (orig_arg
))
7484 /* Substitute into an expansion expression. */
7485 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
7487 if (TREE_CODE (new_arg
) == TREE_VEC
)
7488 /* Add to the expanded length adjustment the number of
7489 expanded arguments. We subtract one from this
7490 measurement, because the argument pack expression
7491 itself is already counted as 1 in
7492 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7493 the argument pack is empty. */
7494 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
7496 else if (ARGUMENT_PACK_P (orig_arg
))
7498 /* Substitute into each of the arguments. */
7499 new_arg
= make_node (TREE_CODE (orig_arg
));
7501 SET_ARGUMENT_PACK_ARGS (
7503 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
7504 args
, complain
, in_decl
));
7506 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
7507 new_arg
= error_mark_node
;
7509 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
7510 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
7512 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
7514 if (TREE_TYPE (new_arg
) == error_mark_node
)
7515 new_arg
= error_mark_node
;
7519 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
7521 if (new_arg
== error_mark_node
)
7522 return error_mark_node
;
7525 if (new_arg
!= orig_arg
)
7532 /* Make space for the expanded arguments coming from template
7534 t
= make_tree_vec (len
+ expanded_len_adjust
);
7535 for (i
= 0, out
= 0; i
< len
; i
++)
7537 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
7538 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
7539 && TREE_CODE (elts
[i
]) == TREE_VEC
)
7543 /* Now expand the template argument pack "in place". */
7544 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
7545 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
7549 TREE_VEC_ELT (t
, out
) = elts
[i
];
7557 /* Return the result of substituting ARGS into the template parameters
7558 given by PARMS. If there are m levels of ARGS and m + n levels of
7559 PARMS, then the result will contain n levels of PARMS. For
7560 example, if PARMS is `template <class T> template <class U>
7561 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7562 result will be `template <int*, double, class V>'. */
7565 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
7570 /* When substituting into a template, we must set
7571 PROCESSING_TEMPLATE_DECL as the template parameters may be
7572 dependent if they are based on one-another, and the dependency
7573 predicates are short-circuit outside of templates. */
7574 ++processing_template_decl
;
7576 for (new_parms
= &r
;
7577 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
7578 new_parms
= &(TREE_CHAIN (*new_parms
)),
7579 parms
= TREE_CHAIN (parms
))
7582 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
7585 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
7591 if (parms
== error_mark_node
)
7594 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
7596 if (tuple
== error_mark_node
)
7599 default_value
= TREE_PURPOSE (tuple
);
7600 parm_decl
= TREE_VALUE (tuple
);
7602 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
7603 if (TREE_CODE (parm_decl
) == PARM_DECL
7604 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
7605 parm_decl
= error_mark_node
;
7606 default_value
= tsubst_template_arg (default_value
, args
,
7607 complain
, NULL_TREE
);
7609 tuple
= build_tree_list (default_value
, parm_decl
);
7610 TREE_VEC_ELT (new_vec
, i
) = tuple
;
7614 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
7615 - TMPL_ARGS_DEPTH (args
)),
7616 new_vec
, NULL_TREE
);
7619 --processing_template_decl
;
7624 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7625 type T. If T is not an aggregate or enumeration type, it is
7626 handled as if by tsubst. IN_DECL is as for tsubst. If
7627 ENTERING_SCOPE is nonzero, T is the context for a template which
7628 we are presently tsubst'ing. Return the substituted value. */
7631 tsubst_aggr_type (tree t
,
7633 tsubst_flags_t complain
,
7640 switch (TREE_CODE (t
))
7643 if (TYPE_PTRMEMFUNC_P (t
))
7644 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
7646 /* Else fall through. */
7649 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
7654 bool saved_skip_evaluation
;
7656 /* In "sizeof(X<I>)" we need to evaluate "I". */
7657 saved_skip_evaluation
= skip_evaluation
;
7658 skip_evaluation
= false;
7660 /* First, determine the context for the type we are looking
7662 context
= TYPE_CONTEXT (t
);
7665 context
= tsubst_aggr_type (context
, args
, complain
,
7666 in_decl
, /*entering_scope=*/1);
7667 /* If context is a nested class inside a class template,
7668 it may still need to be instantiated (c++/33959). */
7669 if (TYPE_P (context
))
7670 context
= complete_type (context
);
7673 /* Then, figure out what arguments are appropriate for the
7674 type we are trying to find. For example, given:
7676 template <class T> struct S;
7677 template <class T, class U> void f(T, U) { S<U> su; }
7679 and supposing that we are instantiating f<int, double>,
7680 then our ARGS will be {int, double}, but, when looking up
7681 S we only want {double}. */
7682 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
7684 if (argvec
== error_mark_node
)
7685 r
= error_mark_node
;
7688 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
7689 entering_scope
, complain
);
7690 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
7693 skip_evaluation
= saved_skip_evaluation
;
7698 /* This is not a template type, so there's nothing to do. */
7702 return tsubst (t
, args
, complain
, in_decl
);
7706 /* Substitute into the default argument ARG (a default argument for
7707 FN), which has the indicated TYPE. */
7710 tsubst_default_argument (tree fn
, tree type
, tree arg
)
7712 tree saved_class_ptr
= NULL_TREE
;
7713 tree saved_class_ref
= NULL_TREE
;
7715 /* This default argument came from a template. Instantiate the
7716 default argument here, not in tsubst. In the case of
7725 we must be careful to do name lookup in the scope of S<T>,
7726 rather than in the current class. */
7727 push_access_scope (fn
);
7728 /* The "this" pointer is not valid in a default argument. */
7731 saved_class_ptr
= current_class_ptr
;
7732 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
7733 saved_class_ref
= current_class_ref
;
7734 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
7737 push_deferring_access_checks(dk_no_deferred
);
7738 /* The default argument expression may cause implicitly defined
7739 member functions to be synthesized, which will result in garbage
7740 collection. We must treat this situation as if we were within
7741 the body of function so as to avoid collecting live data on the
7744 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
7745 tf_warning_or_error
, NULL_TREE
,
7746 /*integral_constant_expression_p=*/false);
7748 pop_deferring_access_checks();
7750 /* Restore the "this" pointer. */
7753 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
7754 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
7757 pop_access_scope (fn
);
7759 /* Make sure the default argument is reasonable. */
7760 arg
= check_default_argument (type
, arg
);
7765 /* Substitute into all the default arguments for FN. */
7768 tsubst_default_arguments (tree fn
)
7773 tmpl_args
= DECL_TI_ARGS (fn
);
7775 /* If this function is not yet instantiated, we certainly don't need
7776 its default arguments. */
7777 if (uses_template_parms (tmpl_args
))
7780 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7782 arg
= TREE_CHAIN (arg
))
7783 if (TREE_PURPOSE (arg
))
7784 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
7786 TREE_PURPOSE (arg
));
7789 /* Substitute the ARGS into the T, which is a _DECL. Return the
7790 result of the substitution. Issue error and warning messages under
7791 control of COMPLAIN. */
7794 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
7796 location_t saved_loc
;
7800 /* Set the filename and linenumber to improve error-reporting. */
7801 saved_loc
= input_location
;
7802 input_location
= DECL_SOURCE_LOCATION (t
);
7804 switch (TREE_CODE (t
))
7808 /* We can get here when processing a member function template,
7809 member class template, and template template parameter of
7810 a template class. */
7811 tree decl
= DECL_TEMPLATE_RESULT (t
);
7816 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
7818 /* Template template parameter is treated here. */
7819 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7820 if (new_type
== error_mark_node
)
7821 return error_mark_node
;
7824 TREE_CHAIN (r
) = NULL_TREE
;
7825 TREE_TYPE (r
) = new_type
;
7826 DECL_TEMPLATE_RESULT (r
)
7827 = build_decl (TYPE_DECL
, DECL_NAME (decl
), new_type
);
7828 DECL_TEMPLATE_PARMS (r
)
7829 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
7831 TYPE_NAME (new_type
) = r
;
7835 /* We might already have an instance of this template.
7836 The ARGS are for the surrounding class type, so the
7837 full args contain the tsubst'd args for the context,
7838 plus the innermost args from the template decl. */
7839 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
7840 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
7841 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
7842 /* Because this is a template, the arguments will still be
7843 dependent, even after substitution. If
7844 PROCESSING_TEMPLATE_DECL is not set, the dependency
7845 predicates will short-circuit. */
7846 ++processing_template_decl
;
7847 full_args
= tsubst_template_args (tmpl_args
, args
,
7849 --processing_template_decl
;
7850 if (full_args
== error_mark_node
)
7851 return error_mark_node
;
7853 /* tsubst_template_args doesn't copy the vector if
7854 nothing changed. But, *something* should have
7856 gcc_assert (full_args
!= tmpl_args
);
7858 spec
= retrieve_specialization (t
, full_args
,
7859 /*class_specializations_p=*/true);
7860 if (spec
!= NULL_TREE
)
7866 /* Make a new template decl. It will be similar to the
7867 original, but will record the current template arguments.
7868 We also create a new function declaration, which is just
7869 like the old one, but points to this new template, rather
7870 than the old one. */
7872 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
7873 TREE_CHAIN (r
) = NULL_TREE
;
7875 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
7877 if (TREE_CODE (decl
) == TYPE_DECL
)
7880 ++processing_template_decl
;
7881 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7882 --processing_template_decl
;
7883 if (new_type
== error_mark_node
)
7884 return error_mark_node
;
7886 TREE_TYPE (r
) = new_type
;
7887 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
7888 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
7889 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
7890 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
7895 ++processing_template_decl
;
7896 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
7897 --processing_template_decl
;
7898 if (new_decl
== error_mark_node
)
7899 return error_mark_node
;
7901 DECL_TEMPLATE_RESULT (r
) = new_decl
;
7902 DECL_TI_TEMPLATE (new_decl
) = r
;
7903 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
7904 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
7905 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
7908 SET_DECL_IMPLICIT_INSTANTIATION (r
);
7909 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
7910 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
7912 /* The template parameters for this new template are all the
7913 template parameters for the old template, except the
7914 outermost level of parameters. */
7915 DECL_TEMPLATE_PARMS (r
)
7916 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
7919 if (PRIMARY_TEMPLATE_P (t
))
7920 DECL_PRIMARY_TEMPLATE (r
) = r
;
7922 if (TREE_CODE (decl
) != TYPE_DECL
)
7923 /* Record this non-type partial instantiation. */
7924 register_specialization (r
, t
,
7925 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
7933 tree argvec
= NULL_TREE
;
7941 /* Nobody should be tsubst'ing into non-template functions. */
7942 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
7944 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
7949 /* If T is not dependent, just return it. We have to
7950 increment PROCESSING_TEMPLATE_DECL because
7951 value_dependent_expression_p assumes that nothing is
7952 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7953 ++processing_template_decl
;
7954 dependent_p
= value_dependent_expression_p (t
);
7955 --processing_template_decl
;
7959 /* Calculate the most general template of which R is a
7960 specialization, and the complete set of arguments used to
7962 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
7963 argvec
= tsubst_template_args (DECL_TI_ARGS
7964 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
7965 args
, complain
, in_decl
);
7967 /* Check to see if we already have this specialization. */
7968 spec
= retrieve_specialization (gen_tmpl
, argvec
,
7969 /*class_specializations_p=*/false);
7977 /* We can see more levels of arguments than parameters if
7978 there was a specialization of a member template, like
7981 template <class T> struct S { template <class U> void f(); }
7982 template <> template <class U> void S<int>::f(U);
7984 Here, we'll be substituting into the specialization,
7985 because that's where we can find the code we actually
7986 want to generate, but we'll have enough arguments for
7987 the most general template.
7989 We also deal with the peculiar case:
7991 template <class T> struct S {
7992 template <class U> friend void f();
7994 template <class U> void f() {}
7996 template void f<double>();
7998 Here, the ARGS for the instantiation of will be {int,
7999 double}. But, we only need as many ARGS as there are
8000 levels of template parameters in CODE_PATTERN. We are
8001 careful not to get fooled into reducing the ARGS in
8004 template <class T> struct S { template <class U> void f(U); }
8005 template <class T> template <> void S<T>::f(int) {}
8007 which we can spot because the pattern will be a
8008 specialization in this case. */
8009 args_depth
= TMPL_ARGS_DEPTH (args
);
8011 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
8012 if (args_depth
> parms_depth
8013 && !DECL_TEMPLATE_SPECIALIZATION (t
))
8014 args
= get_innermost_template_args (args
, parms_depth
);
8018 /* This special case arises when we have something like this:
8020 template <class T> struct S {
8021 friend void f<int>(int, double);
8024 Here, the DECL_TI_TEMPLATE for the friend declaration
8025 will be an IDENTIFIER_NODE. We are being called from
8026 tsubst_friend_function, and we want only to create a
8027 new decl (R) with appropriate types so that we can call
8028 determine_specialization. */
8029 gen_tmpl
= NULL_TREE
;
8032 if (DECL_CLASS_SCOPE_P (t
))
8034 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
8038 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
8039 complain
, t
, /*entering_scope=*/1);
8044 ctx
= DECL_CONTEXT (t
);
8046 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8047 if (type
== error_mark_node
)
8048 return error_mark_node
;
8050 /* We do NOT check for matching decls pushed separately at this
8051 point, as they may not represent instantiations of this
8052 template, and in any case are considered separate under the
8055 DECL_USE_TEMPLATE (r
) = 0;
8056 TREE_TYPE (r
) = type
;
8057 /* Clear out the mangled name and RTL for the instantiation. */
8058 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8059 SET_DECL_RTL (r
, NULL_RTX
);
8060 DECL_INITIAL (r
) = NULL_TREE
;
8061 DECL_CONTEXT (r
) = ctx
;
8063 if (member
&& DECL_CONV_FN_P (r
))
8064 /* Type-conversion operator. Reconstruct the name, in
8065 case it's the name of one of the template's parameters. */
8066 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
8068 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
8070 DECL_RESULT (r
) = NULL_TREE
;
8072 TREE_STATIC (r
) = 0;
8073 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
8074 DECL_EXTERNAL (r
) = 1;
8075 /* If this is an instantiation of a function with internal
8076 linkage, we already know what object file linkage will be
8077 assigned to the instantiation. */
8078 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
8079 DECL_DEFER_OUTPUT (r
) = 0;
8080 TREE_CHAIN (r
) = NULL_TREE
;
8081 DECL_PENDING_INLINE_INFO (r
) = 0;
8082 DECL_PENDING_INLINE_P (r
) = 0;
8083 DECL_SAVED_TREE (r
) = NULL_TREE
;
8085 if (DECL_CLONED_FUNCTION (r
))
8087 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
8089 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
8090 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
8093 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8094 this in the special friend case mentioned above where
8095 GEN_TMPL is NULL. */
8098 DECL_TEMPLATE_INFO (r
)
8099 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
8100 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8101 register_specialization (r
, gen_tmpl
, argvec
, false);
8103 /* We're not supposed to instantiate default arguments
8104 until they are called, for a template. But, for a
8107 template <class T> void f ()
8108 { extern void g(int i = T()); }
8110 we should do the substitution when the template is
8111 instantiated. We handle the member function case in
8112 instantiate_class_template since the default arguments
8113 might refer to other members of the class. */
8115 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8116 && !uses_template_parms (argvec
))
8117 tsubst_default_arguments (r
);
8120 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
8122 /* Copy the list of befriending classes. */
8123 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
8125 friends
= &TREE_CHAIN (*friends
))
8127 *friends
= copy_node (*friends
);
8128 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
8133 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
8135 maybe_retrofit_in_chrg (r
);
8136 if (DECL_CONSTRUCTOR_P (r
))
8137 grok_ctor_properties (ctx
, r
);
8138 /* If this is an instantiation of a member template, clone it.
8139 If it isn't, that'll be handled by
8140 clone_constructors_and_destructors. */
8141 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
8142 clone_function_decl (r
, /*update_method_vec_p=*/0);
8144 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
))
8145 && !grok_op_properties (r
, (complain
& tf_error
) != 0))
8146 return error_mark_node
;
8148 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
8149 SET_DECL_FRIEND_CONTEXT (r
,
8150 tsubst (DECL_FRIEND_CONTEXT (t
),
8151 args
, complain
, in_decl
));
8153 /* Possibly limit visibility based on template args. */
8154 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
8155 if (DECL_VISIBILITY_SPECIFIED (t
))
8157 DECL_VISIBILITY_SPECIFIED (r
) = 0;
8159 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
8161 determine_visibility (r
);
8163 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8164 args
, complain
, in_decl
);
8170 tree type
= NULL_TREE
;
8172 tree expanded_types
= NULL_TREE
;
8173 tree prev_r
= NULL_TREE
;
8174 tree first_r
= NULL_TREE
;
8176 if (FUNCTION_PARAMETER_PACK_P (t
))
8178 /* If there is a local specialization that isn't a
8179 parameter pack, it means that we're doing a "simple"
8180 substitution from inside tsubst_pack_expansion. Just
8181 return the local specialization (which will be a single
8183 tree spec
= retrieve_local_specialization (t
);
8185 && TREE_CODE (spec
) == PARM_DECL
8186 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
8189 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8190 the parameters in this function parameter pack. */
8191 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
8193 if (TREE_CODE (expanded_types
) == TREE_VEC
)
8195 len
= TREE_VEC_LENGTH (expanded_types
);
8197 /* Zero-length parameter packs are boring. Just substitute
8200 return tsubst (TREE_CHAIN (t
), args
, complain
,
8205 /* All we did was update the type. Make a note of that. */
8206 type
= expanded_types
;
8207 expanded_types
= NULL_TREE
;
8211 /* Loop through all of the parameter's we'll build. When T is
8212 a function parameter pack, LEN is the number of expanded
8213 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8215 for (i
= 0; i
< len
; ++i
)
8219 if (DECL_TEMPLATE_PARM_P (t
))
8220 SET_DECL_TEMPLATE_PARM_P (r
);
8223 /* We're on the Ith parameter of the function parameter
8226 /* Get the Ith type. */
8227 type
= TREE_VEC_ELT (expanded_types
, i
);
8230 /* Rename the parameter to include the index. */
8232 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
8235 /* We're dealing with a normal parameter. */
8236 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8238 type
= type_decays_to (type
);
8239 TREE_TYPE (r
) = type
;
8240 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8242 if (DECL_INITIAL (r
))
8244 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
8245 DECL_INITIAL (r
) = TREE_TYPE (r
);
8247 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
8251 DECL_CONTEXT (r
) = NULL_TREE
;
8253 if (!DECL_TEMPLATE_PARM_P (r
))
8254 DECL_ARG_TYPE (r
) = type_passed_as (type
);
8256 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8257 args
, complain
, in_decl
);
8259 /* Keep track of the first new parameter we
8260 generate. That's what will be returned to the
8265 /* Build a proper chain of parameters when substituting
8266 into a function parameter pack. */
8268 TREE_CHAIN (prev_r
) = r
;
8272 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
8273 complain
, TREE_CHAIN (t
));
8275 /* FIRST_R contains the start of the chain we've built. */
8285 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8286 if (type
== error_mark_node
)
8287 return error_mark_node
;
8288 TREE_TYPE (r
) = type
;
8289 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8291 /* DECL_INITIAL gives the number of bits in a bit-field. */
8293 = tsubst_expr (DECL_INITIAL (t
), args
,
8295 /*integral_constant_expression_p=*/true);
8296 /* We don't have to set DECL_CONTEXT here; it is set by
8297 finish_member_declaration. */
8298 TREE_CHAIN (r
) = NULL_TREE
;
8299 if (VOID_TYPE_P (type
))
8300 error ("instantiation of %q+D as type %qT", r
, type
);
8302 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8303 args
, complain
, in_decl
);
8308 /* We reach here only for member using decls. */
8309 if (DECL_DEPENDENT_P (t
))
8311 r
= do_class_using_decl
8312 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
8313 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
8315 r
= error_mark_node
;
8320 TREE_CHAIN (r
) = NULL_TREE
;
8327 tree argvec
= NULL_TREE
;
8328 tree gen_tmpl
= NULL_TREE
;
8330 tree tmpl
= NULL_TREE
;
8332 tree type
= NULL_TREE
;
8335 if (TREE_CODE (t
) == TYPE_DECL
8336 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
8338 /* If this is the canonical decl, we don't have to
8339 mess with instantiations, and often we can't (for
8340 typename, template type parms and such). Note that
8341 TYPE_NAME is not correct for the above test if
8342 we've copied the type for a typedef. */
8343 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8344 if (type
== error_mark_node
)
8345 return error_mark_node
;
8346 r
= TYPE_NAME (type
);
8350 /* Check to see if we already have the specialization we
8353 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
8355 /* T is a static data member or namespace-scope entity.
8356 We have to substitute into namespace-scope variables
8357 (even though such entities are never templates) because
8360 template <class T> void f() { extern T t; }
8362 where the entity referenced is not known until
8363 instantiation time. */
8365 ctx
= DECL_CONTEXT (t
);
8366 if (DECL_CLASS_SCOPE_P (t
))
8368 ctx
= tsubst_aggr_type (ctx
, args
,
8370 in_decl
, /*entering_scope=*/1);
8371 /* If CTX is unchanged, then T is in fact the
8372 specialization we want. That situation occurs when
8373 referencing a static data member within in its own
8374 class. We can use pointer equality, rather than
8375 same_type_p, because DECL_CONTEXT is always
8377 if (ctx
== DECL_CONTEXT (t
))
8383 tmpl
= DECL_TI_TEMPLATE (t
);
8384 gen_tmpl
= most_general_template (tmpl
);
8385 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
8386 spec
= (retrieve_specialization
8388 /*class_specializations_p=*/false));
8393 /* A local variable. */
8395 /* Subsequent calls to pushdecl will fill this in. */
8397 spec
= retrieve_local_specialization (t
);
8399 /* If we already have the specialization we need, there is
8400 nothing more to do. */
8407 /* Create a new node for the specialization we need. */
8409 if (type
== NULL_TREE
)
8410 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8411 if (TREE_CODE (r
) == VAR_DECL
)
8413 /* Even if the original location is out of scope, the
8414 newly substituted one is not. */
8415 DECL_DEAD_FOR_LOCAL (r
) = 0;
8416 DECL_INITIALIZED_P (r
) = 0;
8417 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
8418 if (type
== error_mark_node
)
8419 return error_mark_node
;
8420 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8422 /* It may seem that this case cannot occur, since:
8427 declares a function, not a variable. However:
8430 template <typename T> void g() { T t; }
8431 template void g<f>();
8433 is an attempt to declare a variable with function
8435 error ("variable %qD has function type",
8436 /* R is not yet sufficiently initialized, so we
8437 just use its name. */
8439 return error_mark_node
;
8441 type
= complete_type (type
);
8442 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
8443 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
8444 type
= check_var_type (DECL_NAME (r
), type
);
8446 if (DECL_HAS_VALUE_EXPR_P (t
))
8448 tree ve
= DECL_VALUE_EXPR (t
);
8449 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
8450 /*constant_expression_p=*/false);
8451 SET_DECL_VALUE_EXPR (r
, ve
);
8454 else if (DECL_SELF_REFERENCE_P (t
))
8455 SET_DECL_SELF_REFERENCE_P (r
);
8456 TREE_TYPE (r
) = type
;
8457 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8458 DECL_CONTEXT (r
) = ctx
;
8459 /* Clear out the mangled name and RTL for the instantiation. */
8460 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8461 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8462 SET_DECL_RTL (r
, NULL_RTX
);
8463 /* The initializer must not be expanded until it is required;
8465 DECL_INITIAL (r
) = NULL_TREE
;
8466 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8467 SET_DECL_RTL (r
, NULL_RTX
);
8468 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
8469 if (TREE_CODE (r
) == VAR_DECL
)
8471 /* Possibly limit visibility based on template args. */
8472 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
8473 if (DECL_VISIBILITY_SPECIFIED (t
))
8475 DECL_VISIBILITY_SPECIFIED (r
) = 0;
8477 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
8479 determine_visibility (r
);
8481 /* Preserve a typedef that names a type. */
8482 else if (TREE_CODE (r
) == TYPE_DECL
8483 && DECL_ORIGINAL_TYPE (t
)
8484 && type
!= error_mark_node
)
8486 DECL_ORIGINAL_TYPE (r
) = tsubst (DECL_ORIGINAL_TYPE (t
),
8487 args
, complain
, in_decl
);
8488 TREE_TYPE (r
) = type
= build_variant_type_copy (type
);
8489 TYPE_NAME (type
) = r
;
8494 /* A static data member declaration is always marked
8495 external when it is declared in-class, even if an
8496 initializer is present. We mimic the non-template
8498 DECL_EXTERNAL (r
) = 1;
8500 register_specialization (r
, gen_tmpl
, argvec
, false);
8501 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
8502 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8505 register_local_specialization (r
, t
);
8507 TREE_CHAIN (r
) = NULL_TREE
;
8509 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
8510 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8511 args
, complain
, in_decl
);
8520 /* Restore the file and line information. */
8521 input_location
= saved_loc
;
8526 /* Substitute into the ARG_TYPES of a function type. */
8529 tsubst_arg_types (tree arg_types
,
8531 tsubst_flags_t complain
,
8534 tree remaining_arg_types
;
8535 tree type
= NULL_TREE
;
8537 tree expanded_args
= NULL_TREE
;
8540 if (!arg_types
|| arg_types
== void_list_node
)
8543 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
8544 args
, complain
, in_decl
);
8545 if (remaining_arg_types
== error_mark_node
)
8546 return error_mark_node
;
8548 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
8550 /* For a pack expansion, perform substitution on the
8551 entire expression. Later on, we'll handle the arguments
8553 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
8554 args
, complain
, in_decl
);
8556 if (TREE_CODE (expanded_args
) == TREE_VEC
)
8557 /* So that we'll spin through the parameters, one by one. */
8558 i
= TREE_VEC_LENGTH (expanded_args
);
8561 /* We only partially substituted into the parameter
8562 pack. Our type is TYPE_PACK_EXPANSION. */
8563 type
= expanded_args
;
8564 expanded_args
= NULL_TREE
;
8572 type
= TREE_VEC_ELT (expanded_args
, i
);
8574 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
8576 if (type
== error_mark_node
)
8577 return error_mark_node
;
8578 if (VOID_TYPE_P (type
))
8580 if (complain
& tf_error
)
8582 error ("invalid parameter type %qT", type
);
8584 error ("in declaration %q+D", in_decl
);
8586 return error_mark_node
;
8589 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8590 top-level qualifiers as required. */
8591 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
8593 /* We do not substitute into default arguments here. The standard
8594 mandates that they be instantiated only when needed, which is
8595 done in build_over_call. */
8596 default_arg
= TREE_PURPOSE (arg_types
);
8598 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
8600 /* We've instantiated a template before its default arguments
8601 have been parsed. This can happen for a nested template
8602 class, and is not an error unless we require the default
8603 argument in a call of this function. */
8604 remaining_arg_types
=
8605 tree_cons (default_arg
, type
, remaining_arg_types
);
8606 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
8607 remaining_arg_types
);
8610 remaining_arg_types
=
8611 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
8614 return remaining_arg_types
;
8617 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8618 *not* handle the exception-specification for FNTYPE, because the
8619 initial substitution of explicitly provided template parameters
8620 during argument deduction forbids substitution into the
8621 exception-specification:
8625 All references in the function type of the function template to the
8626 corresponding template parameters are replaced by the specified tem-
8627 plate argument values. If a substitution in a template parameter or
8628 in the function type of the function template results in an invalid
8629 type, type deduction fails. [Note: The equivalent substitution in
8630 exception specifications is done only when the function is instanti-
8631 ated, at which point a program is ill-formed if the substitution
8632 results in an invalid type.] */
8635 tsubst_function_type (tree t
,
8637 tsubst_flags_t complain
,
8644 /* The TYPE_CONTEXT is not used for function/method types. */
8645 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
8647 /* Substitute the return type. */
8648 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8649 if (return_type
== error_mark_node
)
8650 return error_mark_node
;
8651 /* The standard does not presently indicate that creation of a
8652 function type with an invalid return type is a deduction failure.
8653 However, that is clearly analogous to creating an array of "void"
8654 or a reference to a reference. This is core issue #486. */
8655 if (TREE_CODE (return_type
) == ARRAY_TYPE
8656 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
8658 if (complain
& tf_error
)
8660 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
8661 error ("function returning an array");
8663 error ("function returning a function");
8665 return error_mark_node
;
8668 /* Substitute the argument types. */
8669 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
8671 if (arg_types
== error_mark_node
)
8672 return error_mark_node
;
8674 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
8675 && in_decl
!= NULL_TREE
8676 && !TREE_NO_WARNING (in_decl
)
8677 && (SCALAR_TYPE_P (return_type
) || VOID_TYPE_P (return_type
)))
8678 warning (OPT_Wignored_qualifiers
,
8679 "type qualifiers ignored on function return type");
8681 /* Construct a new type node and return it. */
8682 if (TREE_CODE (t
) == FUNCTION_TYPE
)
8683 fntype
= build_function_type (return_type
, arg_types
);
8686 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
8687 if (! IS_AGGR_TYPE (r
))
8691 Type deduction may fail for any of the following
8694 -- Attempting to create "pointer to member of T" when T
8695 is not a class type. */
8696 if (complain
& tf_error
)
8697 error ("creating pointer to member function of non-class type %qT",
8699 return error_mark_node
;
8702 fntype
= build_method_type_directly (r
, return_type
,
8703 TREE_CHAIN (arg_types
));
8705 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
8706 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
8711 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8712 ARGS into that specification, and return the substituted
8713 specification. If there is no specification, return NULL_TREE. */
8716 tsubst_exception_specification (tree fntype
,
8718 tsubst_flags_t complain
,
8724 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
8725 new_specs
= NULL_TREE
;
8728 if (! TREE_VALUE (specs
))
8735 tree expanded_specs
= NULL_TREE
;
8737 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
8739 /* Expand the pack expansion type. */
8740 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
8744 if (expanded_specs
== error_mark_node
)
8745 return error_mark_node
;
8746 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
8747 len
= TREE_VEC_LENGTH (expanded_specs
);
8750 /* We're substituting into a member template, so
8751 we got a TYPE_PACK_EXPANSION back. Add that
8752 expansion and move on. */
8753 gcc_assert (TREE_CODE (expanded_specs
)
8754 == TYPE_PACK_EXPANSION
);
8755 new_specs
= add_exception_specifier (new_specs
,
8758 specs
= TREE_CHAIN (specs
);
8763 for (i
= 0; i
< len
; ++i
)
8766 spec
= TREE_VEC_ELT (expanded_specs
, i
);
8768 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
8769 if (spec
== error_mark_node
)
8771 new_specs
= add_exception_specifier (new_specs
, spec
,
8775 specs
= TREE_CHAIN (specs
);
8781 /* Take the tree structure T and replace template parameters used
8782 therein with the argument vector ARGS. IN_DECL is an associated
8783 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8784 Issue error and warning messages under control of COMPLAIN. Note
8785 that we must be relatively non-tolerant of extensions here, in
8786 order to preserve conformance; if we allow substitutions that
8787 should not be allowed, we may allow argument deductions that should
8788 not succeed, and therefore report ambiguous overload situations
8789 where there are none. In theory, we could allow the substitution,
8790 but indicate that it should have failed, and allow our caller to
8791 make sure that the right thing happens, but we don't try to do this
8794 This function is used for dealing with types, decls and the like;
8795 for expressions, use tsubst_expr or tsubst_copy. */
8798 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8802 if (t
== NULL_TREE
|| t
== error_mark_node
8803 || t
== integer_type_node
8804 || t
== void_type_node
8805 || t
== char_type_node
8806 || t
== unknown_type_node
8807 || TREE_CODE (t
) == NAMESPACE_DECL
)
8811 return tsubst_decl (t
, args
, complain
);
8813 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
8814 type
= IDENTIFIER_TYPE_VALUE (t
);
8816 type
= TREE_TYPE (t
);
8818 gcc_assert (type
!= unknown_type_node
);
8820 /* Reuse typedefs. We need to do this to handle dependent attributes,
8821 such as attribute aligned. */
8824 && TYPE_NAME (t
) != TYPE_MAIN_DECL (t
))
8826 tree decl
= TYPE_NAME (t
);
8828 if (DECL_CLASS_SCOPE_P (decl
)
8829 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
8830 && uses_template_parms (DECL_CONTEXT (decl
)))
8832 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
8833 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
8834 r
= retrieve_specialization (tmpl
, gen_args
, false);
8836 else if (DECL_FUNCTION_SCOPE_P (decl
)
8837 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
8838 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
8839 r
= retrieve_local_specialization (decl
);
8841 /* The typedef is from a non-template context. */
8847 r
= cp_build_qualified_type_real
8848 (r
, cp_type_quals (t
) | cp_type_quals (r
),
8849 complain
| tf_ignore_bad_quals
);
8852 /* Else we must be instantiating the typedef, so fall through. */
8856 && TREE_CODE (t
) != TYPENAME_TYPE
8857 && TREE_CODE (t
) != IDENTIFIER_NODE
8858 && TREE_CODE (t
) != FUNCTION_TYPE
8859 && TREE_CODE (t
) != METHOD_TYPE
)
8860 type
= tsubst (type
, args
, complain
, in_decl
);
8861 if (type
== error_mark_node
)
8862 return error_mark_node
;
8864 switch (TREE_CODE (t
))
8869 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
8870 /*entering_scope=*/0);
8873 case IDENTIFIER_NODE
:
8885 if (t
== integer_type_node
)
8888 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
8889 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
8893 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
8895 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
8896 /*integral_constant_expression_p=*/false);
8897 max
= fold_decl_constant_value (max
);
8899 if (TREE_CODE (max
) != INTEGER_CST
8900 && !at_function_scope_p ()
8901 && !value_dependent_expression_p (max
))
8903 if (complain
& tf_error
)
8904 error ("array bound is not an integer constant");
8905 return error_mark_node
;
8910 Type deduction may fail for any of the following
8913 Attempting to create an array with a size that is
8914 zero or negative. */
8915 if (integer_zerop (max
) && !(complain
& tf_error
))
8916 /* We must fail if performing argument deduction (as
8917 indicated by the state of complain), so that
8918 another substitution can be found. */
8919 return error_mark_node
;
8920 else if (TREE_CODE (max
) == INTEGER_CST
8921 && INT_CST_LT (max
, integer_zero_node
))
8923 if (complain
& tf_error
)
8924 error ("creating array with negative size (%qE)", max
);
8926 return error_mark_node
;
8929 return compute_array_index_type (NULL_TREE
, max
);
8932 case TEMPLATE_TYPE_PARM
:
8933 case TEMPLATE_TEMPLATE_PARM
:
8934 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8935 case TEMPLATE_PARM_INDEX
:
8940 tree arg
= NULL_TREE
;
8944 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
8945 template_parm_level_and_index (t
, &level
, &idx
);
8947 levels
= TMPL_ARGS_DEPTH (args
);
8948 if (level
<= levels
)
8950 arg
= TMPL_ARG (args
, level
, idx
);
8952 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
8953 /* See through ARGUMENT_PACK_SELECT arguments. */
8954 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
8957 if (arg
== error_mark_node
)
8958 return error_mark_node
;
8959 else if (arg
!= NULL_TREE
)
8961 if (ARGUMENT_PACK_P (arg
))
8962 /* If ARG is an argument pack, we don't actually want to
8963 perform a substitution here, because substitutions
8964 for argument packs are only done
8965 element-by-element. We can get to this point when
8966 substituting the type of a non-type template
8967 parameter pack, when that type actually contains
8968 template parameter packs from an outer template, e.g.,
8970 template<typename... Types> struct A {
8971 template<Types... Values> struct B { };
8975 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
8978 gcc_assert (TYPE_P (arg
));
8980 /* cv-quals from the template are discarded when
8981 substituting in a function or reference type. */
8982 if (TREE_CODE (arg
) == FUNCTION_TYPE
8983 || TREE_CODE (arg
) == METHOD_TYPE
8984 || TREE_CODE (arg
) == REFERENCE_TYPE
)
8985 quals
= cp_type_quals (arg
);
8987 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
8989 return cp_build_qualified_type_real
8990 (arg
, quals
, complain
| tf_ignore_bad_quals
);
8992 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8994 /* We are processing a type constructed from a
8995 template template parameter. */
8996 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
8997 args
, complain
, in_decl
);
8998 if (argvec
== error_mark_node
)
8999 return error_mark_node
;
9001 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9002 are resolving nested-types in the signature of a
9003 member function templates. Otherwise ARG is a
9004 TEMPLATE_DECL and is the real template to be
9006 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
9007 arg
= TYPE_NAME (arg
);
9009 r
= lookup_template_class (arg
,
9012 /*entering_scope=*/0,
9014 return cp_build_qualified_type_real
9015 (r
, TYPE_QUALS (t
), complain
);
9018 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9023 /* This can happen during the attempted tsubst'ing in
9024 unify. This means that we don't yet have any information
9025 about the template parameter in question. */
9028 /* If we get here, we must have been looking at a parm for a
9029 more deeply nested template. Make a new version of this
9030 template parameter, but with a lower level. */
9031 switch (TREE_CODE (t
))
9033 case TEMPLATE_TYPE_PARM
:
9034 case TEMPLATE_TEMPLATE_PARM
:
9035 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9036 if (cp_type_quals (t
))
9038 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
9039 r
= cp_build_qualified_type_real
9040 (r
, cp_type_quals (t
),
9041 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
9042 ? tf_ignore_bad_quals
: 0));
9047 TEMPLATE_TYPE_PARM_INDEX (r
)
9048 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
9049 r
, levels
, args
, complain
);
9050 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
9051 TYPE_MAIN_VARIANT (r
) = r
;
9052 TYPE_POINTER_TO (r
) = NULL_TREE
;
9053 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
9055 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
9056 /* We have reduced the level of the template
9057 template parameter, but not the levels of its
9058 template parameters, so canonical_type_parameter
9059 will not be able to find the canonical template
9060 template parameter for this level. Thus, we
9061 require structural equality checking to compare
9062 TEMPLATE_TEMPLATE_PARMs. */
9063 SET_TYPE_STRUCTURAL_EQUALITY (r
);
9064 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
9065 SET_TYPE_STRUCTURAL_EQUALITY (r
);
9067 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
9069 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9071 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
9073 if (argvec
== error_mark_node
)
9074 return error_mark_node
;
9076 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
9077 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
9082 case TEMPLATE_PARM_INDEX
:
9083 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
9095 tree purpose
, value
, chain
;
9097 if (t
== void_list_node
)
9100 purpose
= TREE_PURPOSE (t
);
9103 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
9104 if (purpose
== error_mark_node
)
9105 return error_mark_node
;
9107 value
= TREE_VALUE (t
);
9110 value
= tsubst (value
, args
, complain
, in_decl
);
9111 if (value
== error_mark_node
)
9112 return error_mark_node
;
9114 chain
= TREE_CHAIN (t
);
9115 if (chain
&& chain
!= void_type_node
)
9117 chain
= tsubst (chain
, args
, complain
, in_decl
);
9118 if (chain
== error_mark_node
)
9119 return error_mark_node
;
9121 if (purpose
== TREE_PURPOSE (t
)
9122 && value
== TREE_VALUE (t
)
9123 && chain
== TREE_CHAIN (t
))
9125 return hash_tree_cons (purpose
, value
, chain
);
9129 /* We should never be tsubsting a binfo. */
9133 /* A vector of template arguments. */
9135 return tsubst_template_args (t
, args
, complain
, in_decl
);
9138 case REFERENCE_TYPE
:
9140 enum tree_code code
;
9142 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
9145 code
= TREE_CODE (t
);
9150 Type deduction may fail for any of the following
9153 -- Attempting to create a pointer to reference type.
9154 -- Attempting to create a reference to a reference type or
9155 a reference to void.
9157 Core issue 106 says that creating a reference to a reference
9158 during instantiation is no longer a cause for failure. We
9159 only enforce this check in strict C++98 mode. */
9160 if ((TREE_CODE (type
) == REFERENCE_TYPE
9161 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
9162 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
9164 static location_t last_loc
;
9166 /* We keep track of the last time we issued this error
9167 message to avoid spewing a ton of messages during a
9168 single bad template instantiation. */
9169 if (complain
& tf_error
9170 #ifdef USE_MAPPED_LOCATION
9171 && last_loc
!= input_location
9173 && (last_loc
.line
!= input_line
9174 || last_loc
.file
!= input_filename
)
9178 if (TREE_CODE (type
) == VOID_TYPE
)
9179 error ("forming reference to void");
9181 error ("forming %s to reference type %qT",
9182 (code
== POINTER_TYPE
) ? "pointer" : "reference",
9184 last_loc
= input_location
;
9187 return error_mark_node
;
9189 else if (code
== POINTER_TYPE
)
9191 r
= build_pointer_type (type
);
9192 if (TREE_CODE (type
) == METHOD_TYPE
)
9193 r
= build_ptrmemfunc_type (r
);
9195 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
9196 /* In C++0x, during template argument substitution, when there is an
9197 attempt to create a reference to a reference type, reference
9198 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9200 "If a template-argument for a template-parameter T names a type
9201 that is a reference to a type A, an attempt to create the type
9202 'lvalue reference to cv T' creates the type 'lvalue reference to
9203 A,' while an attempt to create the type type rvalue reference to
9204 cv T' creates the type T"
9206 r
= cp_build_reference_type
9208 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
9210 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
9211 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
9213 if (r
!= error_mark_node
)
9214 /* Will this ever be needed for TYPE_..._TO values? */
9221 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
9222 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
9226 Type deduction may fail for any of the following
9229 -- Attempting to create "pointer to member of T" when T
9230 is not a class type. */
9231 if (complain
& tf_error
)
9232 error ("creating pointer to member of non-class type %qT", r
);
9233 return error_mark_node
;
9235 if (TREE_CODE (type
) == REFERENCE_TYPE
)
9237 if (complain
& tf_error
)
9238 error ("creating pointer to member reference type %qT", type
);
9239 return error_mark_node
;
9241 if (TREE_CODE (type
) == VOID_TYPE
)
9243 if (complain
& tf_error
)
9244 error ("creating pointer to member of type void");
9245 return error_mark_node
;
9247 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
9248 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9250 /* The type of the implicit object parameter gets its
9251 cv-qualifiers from the FUNCTION_TYPE. */
9253 tree this_type
= cp_build_qualified_type (TYPE_MAIN_VARIANT (r
),
9254 cp_type_quals (type
));
9256 method_type
= build_method_type_directly (this_type
,
9258 TYPE_ARG_TYPES (type
));
9259 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
9260 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
9264 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
9273 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
9274 if (fntype
== error_mark_node
)
9275 return error_mark_node
;
9277 /* Substitute the exception specification. */
9278 specs
= tsubst_exception_specification (t
, args
, complain
,
9280 if (specs
== error_mark_node
)
9281 return error_mark_node
;
9283 fntype
= build_exception_variant (fntype
, specs
);
9288 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
9289 if (domain
== error_mark_node
)
9290 return error_mark_node
;
9292 /* As an optimization, we avoid regenerating the array type if
9293 it will obviously be the same as T. */
9294 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
9297 /* These checks should match the ones in grokdeclarator.
9301 The deduction may fail for any of the following reasons:
9303 -- Attempting to create an array with an element type that
9304 is void, a function type, or a reference type, or [DR337]
9305 an abstract class type. */
9306 if (TREE_CODE (type
) == VOID_TYPE
9307 || TREE_CODE (type
) == FUNCTION_TYPE
9308 || TREE_CODE (type
) == REFERENCE_TYPE
)
9310 if (complain
& tf_error
)
9311 error ("creating array of %qT", type
);
9312 return error_mark_node
;
9314 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
9316 if (complain
& tf_error
)
9317 error ("creating array of %qT, which is an abstract class type",
9319 return error_mark_node
;
9322 r
= build_cplus_array_type (type
, domain
);
9324 if (TYPE_USER_ALIGN (t
))
9326 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
9327 TYPE_USER_ALIGN (r
) = 1;
9336 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9337 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9339 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9340 return error_mark_node
;
9342 return fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
9348 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9349 if (e
== error_mark_node
)
9350 return error_mark_node
;
9352 return fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), e
);
9357 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9358 in_decl
, /*entering_scope=*/1);
9359 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
9362 if (ctx
== error_mark_node
|| f
== error_mark_node
)
9363 return error_mark_node
;
9365 if (!IS_AGGR_TYPE (ctx
))
9367 if (complain
& tf_error
)
9368 error ("%qT is not a class, struct, or union type", ctx
);
9369 return error_mark_node
;
9371 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
9373 /* Normally, make_typename_type does not require that the CTX
9374 have complete type in order to allow things like:
9376 template <class T> struct S { typename S<T>::X Y; };
9378 But, such constructs have already been resolved by this
9379 point, so here CTX really should have complete type, unless
9380 it's a partial instantiation. */
9381 ctx
= complete_type (ctx
);
9382 if (!COMPLETE_TYPE_P (ctx
))
9384 if (complain
& tf_error
)
9385 cxx_incomplete_type_error (NULL_TREE
, ctx
);
9386 return error_mark_node
;
9390 f
= make_typename_type (ctx
, f
, typename_type
,
9391 (complain
& tf_error
) | tf_keep_type_decl
);
9392 if (f
== error_mark_node
)
9394 if (TREE_CODE (f
) == TYPE_DECL
)
9396 complain
|= tf_ignore_bad_quals
;
9400 if (TREE_CODE (f
) != TYPENAME_TYPE
)
9402 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
9403 error ("%qT resolves to %qT, which is not an enumeration type",
9405 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
9406 error ("%qT resolves to %qT, which is is not a class type",
9410 return cp_build_qualified_type_real
9411 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
9414 case UNBOUND_CLASS_TEMPLATE
:
9416 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9417 in_decl
, /*entering_scope=*/1);
9418 tree name
= TYPE_IDENTIFIER (t
);
9419 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
9421 if (ctx
== error_mark_node
|| name
== error_mark_node
)
9422 return error_mark_node
;
9425 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
9426 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
9436 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9437 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
9438 /*integral_constant_expression_p=*/false);
9439 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9440 return error_mark_node
;
9442 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
9447 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9448 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9449 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9450 return error_mark_node
;
9452 return build_qualified_name (/*type=*/NULL_TREE
,
9453 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
9460 type
= finish_typeof (tsubst_expr
9461 (TYPEOF_TYPE_EXPR (t
), args
,
9463 /*integral_constant_expression_p=*/false));
9464 return cp_build_qualified_type_real (type
,
9466 | cp_type_quals (type
),
9475 finish_decltype_type (tsubst_expr
9476 (DECLTYPE_TYPE_EXPR (t
), args
,
9478 /*integral_constant_expression_p=*/false),
9479 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
9480 return cp_build_qualified_type_real (type
,
9482 | cp_type_quals (type
),
9486 case TYPE_ARGUMENT_PACK
:
9487 case NONTYPE_ARGUMENT_PACK
:
9489 tree r
= make_node (TREE_CODE (t
));
9491 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
9495 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
9497 /* For template nontype argument packs, also substitute into
9499 if (TREE_CODE (t
) == NONTYPE_ARGUMENT_PACK
)
9500 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9507 sorry ("use of %qs in template",
9508 tree_code_name
[(int) TREE_CODE (t
)]);
9509 return error_mark_node
;
9513 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9514 type of the expression on the left-hand side of the "." or "->"
9518 tsubst_baselink (tree baselink
, tree object_type
,
9519 tree args
, tsubst_flags_t complain
, tree in_decl
)
9522 tree qualifying_scope
;
9525 tree template_args
= 0;
9526 bool template_id_p
= false;
9528 /* A baselink indicates a function from a base class. Both the
9529 BASELINK_ACCESS_BINFO and the base class referenced may
9530 indicate bases of the template class, rather than the
9531 instantiated class. In addition, lookups that were not
9532 ambiguous before may be ambiguous now. Therefore, we perform
9533 the lookup again. */
9534 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
9535 qualifying_scope
= tsubst (qualifying_scope
, args
,
9537 fns
= BASELINK_FUNCTIONS (baselink
);
9538 optype
= BASELINK_OPTYPE (baselink
);
9539 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
9541 template_id_p
= true;
9542 template_args
= TREE_OPERAND (fns
, 1);
9543 fns
= TREE_OPERAND (fns
, 0);
9545 template_args
= tsubst_template_args (template_args
, args
,
9548 name
= DECL_NAME (get_first_fn (fns
));
9549 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
9551 /* If lookup found a single function, mark it as used at this
9552 point. (If it lookup found multiple functions the one selected
9553 later by overload resolution will be marked as used at that
9555 if (BASELINK_P (baselink
))
9556 fns
= BASELINK_FUNCTIONS (baselink
);
9557 if (!template_id_p
&& !really_overloaded_fn (fns
))
9558 mark_used (OVL_CURRENT (fns
));
9560 /* Add back the template arguments, if present. */
9561 if (BASELINK_P (baselink
) && template_id_p
)
9562 BASELINK_FUNCTIONS (baselink
)
9563 = build_nt (TEMPLATE_ID_EXPR
,
9564 BASELINK_FUNCTIONS (baselink
),
9566 /* Update the conversion operator type. */
9567 BASELINK_OPTYPE (baselink
)
9568 = tsubst (optype
, args
, complain
, in_decl
);
9571 object_type
= current_class_type
;
9572 return adjust_result_of_qualified_name_lookup (baselink
,
9577 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9578 true if the qualified-id will be a postfix-expression in-and-of
9579 itself; false if more of the postfix-expression follows the
9580 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9584 tsubst_qualified_id (tree qualified_id
, tree args
,
9585 tsubst_flags_t complain
, tree in_decl
,
9586 bool done
, bool address_p
)
9594 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
9596 /* Figure out what name to look up. */
9597 name
= TREE_OPERAND (qualified_id
, 1);
9598 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
9601 template_args
= TREE_OPERAND (name
, 1);
9603 template_args
= tsubst_template_args (template_args
, args
,
9605 name
= TREE_OPERAND (name
, 0);
9609 is_template
= false;
9610 template_args
= NULL_TREE
;
9613 /* Substitute into the qualifying scope. When there are no ARGS, we
9614 are just trying to simplify a non-dependent expression. In that
9615 case the qualifying scope may be dependent, and, in any case,
9616 substituting will not help. */
9617 scope
= TREE_OPERAND (qualified_id
, 0);
9620 scope
= tsubst (scope
, args
, complain
, in_decl
);
9621 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
9626 if (dependent_type_p (scope
))
9627 return build_qualified_name (/*type=*/NULL_TREE
,
9629 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
9631 if (!BASELINK_P (name
) && !DECL_P (expr
))
9633 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
9634 /* If this were actually a destructor call, it would have been
9635 parsed as such by the parser. */
9636 expr
= error_mark_node
;
9638 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
9639 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
9640 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
9642 if (complain
& tf_error
)
9644 error ("dependent-name %qE is parsed as a non-type, but "
9645 "instantiation yields a type", qualified_id
);
9646 inform ("say %<typename %E%> if a type is meant", qualified_id
);
9648 return error_mark_node
;
9654 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
9656 /* Remember that there was a reference to this entity. */
9660 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
9662 if (complain
& tf_error
)
9663 qualified_name_lookup_error (scope
,
9664 TREE_OPERAND (qualified_id
, 1),
9666 return error_mark_node
;
9670 expr
= lookup_template_function (expr
, template_args
);
9672 if (expr
== error_mark_node
&& complain
& tf_error
)
9673 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
9675 else if (TYPE_P (scope
))
9677 expr
= (adjust_result_of_qualified_name_lookup
9678 (expr
, scope
, current_class_type
));
9679 expr
= (finish_qualified_id_expr
9680 (scope
, expr
, done
, address_p
,
9681 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
9682 /*template_arg_p=*/false));
9685 /* Expressions do not generally have reference type. */
9686 if (TREE_CODE (expr
) != SCOPE_REF
9687 /* However, if we're about to form a pointer-to-member, we just
9688 want the referenced member referenced. */
9689 && TREE_CODE (expr
) != OFFSET_REF
)
9690 expr
= convert_from_reference (expr
);
9695 /* Like tsubst, but deals with expressions. This function just replaces
9696 template parms; to finish processing the resultant expression, use
9700 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9702 enum tree_code code
;
9705 if (t
== NULL_TREE
|| t
== error_mark_node
)
9708 code
= TREE_CODE (t
);
9713 r
= retrieve_local_specialization (t
);
9714 gcc_assert (r
!= NULL
);
9715 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
9716 r
= ARGUMENT_PACK_SELECT_ARG (r
);
9725 if (DECL_TEMPLATE_PARM_P (t
))
9726 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
9727 /* There is no need to substitute into namespace-scope
9729 if (DECL_NAMESPACE_SCOPE_P (t
))
9731 /* If ARGS is NULL, then T is known to be non-dependent. */
9732 if (args
== NULL_TREE
)
9733 return integral_constant_value (t
);
9735 /* Unfortunately, we cannot just call lookup_name here.
9738 template <int I> int f() {
9740 struct S { void g() { E e = a; } };
9743 When we instantiate f<7>::S::g(), say, lookup_name is not
9744 clever enough to find f<7>::a. */
9746 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
9747 /*entering_scope=*/0);
9749 for (v
= TYPE_VALUES (enum_type
);
9752 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
9753 return TREE_VALUE (v
);
9755 /* We didn't find the name. That should never happen; if
9756 name-lookup found it during preliminary parsing, we
9757 should find it again here during instantiation. */
9763 if (DECL_CONTEXT (t
))
9767 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
9768 /*entering_scope=*/1);
9769 if (ctx
!= DECL_CONTEXT (t
))
9771 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
9774 if (complain
& tf_error
)
9775 error ("using invalid field %qD", t
);
9776 return error_mark_node
;
9786 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9787 || local_variable_p (t
))
9788 t
= tsubst (t
, args
, complain
, in_decl
);
9793 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
9796 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9797 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
9798 args
, complain
, in_decl
);
9799 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
9800 return tsubst (t
, args
, complain
, in_decl
);
9801 else if (DECL_CLASS_SCOPE_P (t
)
9802 && uses_template_parms (DECL_CONTEXT (t
)))
9804 /* Template template argument like the following example need
9807 template <template <class> class TT> struct C {};
9808 template <class T> struct D {
9809 template <class U> struct E {};
9814 We are processing the template argument `E' in #1 for
9815 the template instantiation #2. Originally, `E' is a
9816 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9817 have to substitute this with one having context `D<int>'. */
9819 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
9820 return lookup_field (context
, DECL_NAME(t
), 0, false);
9823 /* Ordinary template template argument. */
9827 case REINTERPRET_CAST_EXPR
:
9828 case CONST_CAST_EXPR
:
9829 case STATIC_CAST_EXPR
:
9830 case DYNAMIC_CAST_EXPR
:
9833 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
9834 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
9837 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
9839 /* We only want to compute the number of arguments. */
9840 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
9842 if (expanded
== error_mark_node
)
9843 return error_mark_node
;
9844 return build_int_cst (size_type_node
, TREE_VEC_LENGTH (expanded
));
9850 case TRUTH_NOT_EXPR
:
9853 case UNARY_PLUS_EXPR
: /* Unary + */
9861 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
9862 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
9869 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9870 name
= TREE_OPERAND (t
, 1);
9871 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
9873 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9875 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
9877 else if (TREE_CODE (name
) == SCOPE_REF
9878 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
9880 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9882 name
= TREE_OPERAND (name
, 1);
9883 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9885 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
9886 name
= build_qualified_name (/*type=*/NULL_TREE
,
9888 /*template_p=*/false);
9890 else if (TREE_CODE (name
) == BASELINK
)
9891 name
= tsubst_baselink (name
,
9892 non_reference (TREE_TYPE (object
)),
9896 name
= tsubst_copy (name
, args
, complain
, in_decl
);
9897 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
9903 case TRUNC_DIV_EXPR
:
9905 case FLOOR_DIV_EXPR
:
9906 case ROUND_DIV_EXPR
:
9907 case EXACT_DIV_EXPR
:
9911 case TRUNC_MOD_EXPR
:
9912 case FLOOR_MOD_EXPR
:
9913 case TRUTH_ANDIF_EXPR
:
9914 case TRUTH_ORIF_EXPR
:
9915 case TRUTH_AND_EXPR
:
9932 case PREDECREMENT_EXPR
:
9933 case PREINCREMENT_EXPR
:
9934 case POSTDECREMENT_EXPR
:
9935 case POSTINCREMENT_EXPR
:
9937 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9938 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
9941 return build_qualified_name (/*type=*/NULL_TREE
,
9942 tsubst_copy (TREE_OPERAND (t
, 0),
9943 args
, complain
, in_decl
),
9944 tsubst_copy (TREE_OPERAND (t
, 1),
9945 args
, complain
, in_decl
),
9946 QUALIFIED_NAME_IS_TEMPLATE (t
));
9951 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9952 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9953 NULL_TREE
, NULL_TREE
);
9957 int n
= VL_EXP_OPERAND_LENGTH (t
);
9958 tree result
= build_vl_exp (CALL_EXPR
, n
);
9960 for (i
= 0; i
< n
; i
++)
9961 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
9968 case PSEUDO_DTOR_EXPR
:
9971 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9972 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9973 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
9974 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
9981 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9982 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9983 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
9984 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
9991 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9992 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
9993 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
9994 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
9998 case TEMPLATE_ID_EXPR
:
10000 /* Substituted template arguments */
10001 tree fn
= TREE_OPERAND (t
, 0);
10002 tree targs
= TREE_OPERAND (t
, 1);
10004 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
10006 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
10008 return lookup_template_function (fn
, targs
);
10013 tree purpose
, value
, chain
;
10015 if (t
== void_list_node
)
10018 purpose
= TREE_PURPOSE (t
);
10020 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
10021 value
= TREE_VALUE (t
);
10023 value
= tsubst_copy (value
, args
, complain
, in_decl
);
10024 chain
= TREE_CHAIN (t
);
10025 if (chain
&& chain
!= void_type_node
)
10026 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
10027 if (purpose
== TREE_PURPOSE (t
)
10028 && value
== TREE_VALUE (t
)
10029 && chain
== TREE_CHAIN (t
))
10031 return tree_cons (purpose
, value
, chain
);
10036 case ENUMERAL_TYPE
:
10038 case TEMPLATE_TYPE_PARM
:
10039 case TEMPLATE_TEMPLATE_PARM
:
10040 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10041 case TEMPLATE_PARM_INDEX
:
10043 case REFERENCE_TYPE
:
10045 case FUNCTION_TYPE
:
10048 case TYPENAME_TYPE
:
10049 case UNBOUND_CLASS_TEMPLATE
:
10051 case DECLTYPE_TYPE
:
10053 return tsubst (t
, args
, complain
, in_decl
);
10055 case IDENTIFIER_NODE
:
10056 if (IDENTIFIER_TYPENAME_P (t
))
10058 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10059 return mangle_conv_op_name_for_type (new_type
);
10065 /* This is handled by tsubst_copy_and_build. */
10066 gcc_unreachable ();
10069 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
10071 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
10073 case CLEANUP_POINT_EXPR
:
10074 /* We shouldn't have built any of these during initial template
10075 generation. Instead, they should be built during instantiation
10076 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10077 gcc_unreachable ();
10080 mark_used (TREE_OPERAND (t
, 1));
10083 case EXPR_PACK_EXPANSION
:
10084 error ("invalid use of pack expansion expression");
10085 return error_mark_node
;
10087 case NONTYPE_ARGUMENT_PACK
:
10088 error ("use %<...%> to expand argument pack");
10089 return error_mark_node
;
10096 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10099 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
10102 tree new_clauses
= NULL
, nc
, oc
;
10104 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
10106 nc
= copy_node (oc
);
10107 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
10110 switch (OMP_CLAUSE_CODE (nc
))
10112 case OMP_CLAUSE_PRIVATE
:
10113 case OMP_CLAUSE_SHARED
:
10114 case OMP_CLAUSE_FIRSTPRIVATE
:
10115 case OMP_CLAUSE_LASTPRIVATE
:
10116 case OMP_CLAUSE_REDUCTION
:
10117 case OMP_CLAUSE_COPYIN
:
10118 case OMP_CLAUSE_COPYPRIVATE
:
10119 case OMP_CLAUSE_IF
:
10120 case OMP_CLAUSE_NUM_THREADS
:
10121 case OMP_CLAUSE_SCHEDULE
:
10122 OMP_CLAUSE_OPERAND (nc
, 0)
10123 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
10124 in_decl
, /*integral_constant_expression_p=*/false);
10126 case OMP_CLAUSE_NOWAIT
:
10127 case OMP_CLAUSE_ORDERED
:
10128 case OMP_CLAUSE_DEFAULT
:
10131 gcc_unreachable ();
10135 return finish_omp_clauses (nreverse (new_clauses
));
10138 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10141 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
10144 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10146 tree purpose
, value
, chain
;
10151 if (TREE_CODE (t
) != TREE_LIST
)
10152 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10153 /*function_p=*/false,
10154 /*integral_constant_expression_p=*/false);
10156 if (t
== void_list_node
)
10159 purpose
= TREE_PURPOSE (t
);
10161 purpose
= RECUR (purpose
);
10162 value
= TREE_VALUE (t
);
10164 value
= RECUR (value
);
10165 chain
= TREE_CHAIN (t
);
10166 if (chain
&& chain
!= void_type_node
)
10167 chain
= RECUR (chain
);
10168 return tree_cons (purpose
, value
, chain
);
10172 /* Like tsubst_copy for expressions, etc. but also does semantic
10176 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
10177 bool integral_constant_expression_p
)
10179 #define RECUR(NODE) \
10180 tsubst_expr ((NODE), args, complain, in_decl, \
10181 integral_constant_expression_p)
10185 if (t
== NULL_TREE
|| t
== error_mark_node
)
10188 if (EXPR_HAS_LOCATION (t
))
10189 input_location
= EXPR_LOCATION (t
);
10190 if (STATEMENT_CODE_P (TREE_CODE (t
)))
10191 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
10193 switch (TREE_CODE (t
))
10195 case STATEMENT_LIST
:
10197 tree_stmt_iterator i
;
10198 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
10199 RECUR (tsi_stmt (i
));
10203 case CTOR_INITIALIZER
:
10204 finish_mem_initializers (tsubst_initializer_list
10205 (TREE_OPERAND (t
, 0), args
));
10209 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
10213 tmp
= RECUR (EXPR_STMT_EXPR (t
));
10214 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
10215 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
10217 finish_expr_stmt (tmp
);
10221 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
10229 decl
= DECL_EXPR_DECL (t
);
10230 if (TREE_CODE (decl
) == LABEL_DECL
)
10231 finish_label_decl (DECL_NAME (decl
));
10232 else if (TREE_CODE (decl
) == USING_DECL
)
10234 tree scope
= USING_DECL_SCOPE (decl
);
10235 tree name
= DECL_NAME (decl
);
10238 scope
= RECUR (scope
);
10239 decl
= lookup_qualified_name (scope
, name
,
10240 /*is_type_p=*/false,
10241 /*complain=*/false);
10242 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
10243 qualified_name_lookup_error (scope
, name
, decl
);
10245 do_local_using_decl (decl
, scope
, name
);
10249 init
= DECL_INITIAL (decl
);
10250 decl
= tsubst (decl
, args
, complain
, in_decl
);
10251 if (decl
!= error_mark_node
)
10253 /* By marking the declaration as instantiated, we avoid
10254 trying to instantiate it. Since instantiate_decl can't
10255 handle local variables, and since we've already done
10256 all that needs to be done, that's the right thing to
10258 if (TREE_CODE (decl
) == VAR_DECL
)
10259 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10260 if (TREE_CODE (decl
) == VAR_DECL
10261 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
10262 /* Anonymous aggregates are a special case. */
10263 finish_anon_union (decl
);
10266 maybe_push_decl (decl
);
10267 if (TREE_CODE (decl
) == VAR_DECL
10268 && DECL_PRETTY_FUNCTION_P (decl
))
10270 /* For __PRETTY_FUNCTION__ we have to adjust the
10272 const char *const name
10273 = cxx_printable_name (current_function_decl
, 2);
10274 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
10278 tree t
= RECUR (init
);
10281 /* If we had an initializer but it
10282 instantiated to nothing,
10283 value-initialize the object. This will
10284 only occur when the initializer was a
10285 pack expansion where the parameter packs
10286 used in that expansion were of length
10288 init
= build_default_init (TREE_TYPE (decl
),
10294 finish_decl (decl
, init
, NULL_TREE
);
10299 /* A DECL_EXPR can also be used as an expression, in the condition
10300 clause of an if/for/while construct. */
10305 stmt
= begin_for_stmt ();
10306 RECUR (FOR_INIT_STMT (t
));
10307 finish_for_init_stmt (stmt
);
10308 tmp
= RECUR (FOR_COND (t
));
10309 finish_for_cond (tmp
, stmt
);
10310 tmp
= RECUR (FOR_EXPR (t
));
10311 finish_for_expr (tmp
, stmt
);
10312 RECUR (FOR_BODY (t
));
10313 finish_for_stmt (stmt
);
10317 stmt
= begin_while_stmt ();
10318 tmp
= RECUR (WHILE_COND (t
));
10319 finish_while_stmt_cond (tmp
, stmt
);
10320 RECUR (WHILE_BODY (t
));
10321 finish_while_stmt (stmt
);
10325 stmt
= begin_do_stmt ();
10326 RECUR (DO_BODY (t
));
10327 finish_do_body (stmt
);
10328 tmp
= RECUR (DO_COND (t
));
10329 finish_do_stmt (tmp
, stmt
);
10333 stmt
= begin_if_stmt ();
10334 tmp
= RECUR (IF_COND (t
));
10335 finish_if_stmt_cond (tmp
, stmt
);
10336 RECUR (THEN_CLAUSE (t
));
10337 finish_then_clause (stmt
);
10339 if (ELSE_CLAUSE (t
))
10341 begin_else_clause (stmt
);
10342 RECUR (ELSE_CLAUSE (t
));
10343 finish_else_clause (stmt
);
10346 finish_if_stmt (stmt
);
10350 if (BIND_EXPR_BODY_BLOCK (t
))
10351 stmt
= begin_function_body ();
10353 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
10354 ? BCS_TRY_BLOCK
: 0);
10356 RECUR (BIND_EXPR_BODY (t
));
10358 if (BIND_EXPR_BODY_BLOCK (t
))
10359 finish_function_body (stmt
);
10361 finish_compound_stmt (stmt
);
10365 finish_break_stmt ();
10368 case CONTINUE_STMT
:
10369 finish_continue_stmt ();
10373 stmt
= begin_switch_stmt ();
10374 tmp
= RECUR (SWITCH_STMT_COND (t
));
10375 finish_switch_cond (tmp
, stmt
);
10376 RECUR (SWITCH_STMT_BODY (t
));
10377 finish_switch_stmt (stmt
);
10380 case CASE_LABEL_EXPR
:
10381 finish_case_label (RECUR (CASE_LOW (t
)),
10382 RECUR (CASE_HIGH (t
)));
10386 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t
)));
10390 tmp
= GOTO_DESTINATION (t
);
10391 if (TREE_CODE (tmp
) != LABEL_DECL
)
10392 /* Computed goto's must be tsubst'd into. On the other hand,
10393 non-computed gotos must not be; the identifier in question
10394 will have no binding. */
10397 tmp
= DECL_NAME (tmp
);
10398 finish_goto_stmt (tmp
);
10402 tmp
= finish_asm_stmt
10403 (ASM_VOLATILE_P (t
),
10404 RECUR (ASM_STRING (t
)),
10405 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
10406 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
10407 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
10409 tree asm_expr
= tmp
;
10410 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
10411 asm_expr
= TREE_OPERAND (asm_expr
, 0);
10412 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
10419 stmt
= begin_try_block ();
10420 RECUR (TRY_STMTS (t
));
10421 finish_cleanup_try_block (stmt
);
10422 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
10426 tree compound_stmt
= NULL_TREE
;
10428 if (FN_TRY_BLOCK_P (t
))
10429 stmt
= begin_function_try_block (&compound_stmt
);
10431 stmt
= begin_try_block ();
10433 RECUR (TRY_STMTS (t
));
10435 if (FN_TRY_BLOCK_P (t
))
10436 finish_function_try_block (stmt
);
10438 finish_try_block (stmt
);
10440 RECUR (TRY_HANDLERS (t
));
10441 if (FN_TRY_BLOCK_P (t
))
10442 finish_function_handler_sequence (stmt
, compound_stmt
);
10444 finish_handler_sequence (stmt
);
10450 tree decl
= HANDLER_PARMS (t
);
10454 decl
= tsubst (decl
, args
, complain
, in_decl
);
10455 /* Prevent instantiate_decl from trying to instantiate
10456 this variable. We've already done all that needs to be
10458 if (decl
!= error_mark_node
)
10459 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10461 stmt
= begin_handler ();
10462 finish_handler_parms (decl
, stmt
);
10463 RECUR (HANDLER_BODY (t
));
10464 finish_handler (stmt
);
10469 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
10472 case STATIC_ASSERT
:
10475 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
10478 /*integral_constant_expression_p=*/true);
10479 finish_static_assert (condition
,
10480 STATIC_ASSERT_MESSAGE (t
),
10481 STATIC_ASSERT_SOURCE_LOCATION (t
),
10482 /*member_p=*/false);
10487 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
10488 args
, complain
, in_decl
);
10489 stmt
= begin_omp_parallel ();
10490 RECUR (OMP_PARALLEL_BODY (t
));
10491 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
10492 = OMP_PARALLEL_COMBINED (t
);
10497 tree clauses
, decl
, init
, cond
, incr
, body
, pre_body
;
10499 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
10500 args
, complain
, in_decl
);
10501 init
= OMP_FOR_INIT (t
);
10502 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
10503 decl
= RECUR (TREE_OPERAND (init
, 0));
10504 init
= RECUR (TREE_OPERAND (init
, 1));
10505 cond
= RECUR (OMP_FOR_COND (t
));
10506 incr
= RECUR (OMP_FOR_INCR (t
));
10508 stmt
= begin_omp_structured_block ();
10510 pre_body
= push_stmt_list ();
10511 RECUR (OMP_FOR_PRE_BODY (t
));
10512 pre_body
= pop_stmt_list (pre_body
);
10514 body
= push_stmt_list ();
10515 RECUR (OMP_FOR_BODY (t
));
10516 body
= pop_stmt_list (body
);
10518 t
= finish_omp_for (EXPR_LOCATION (t
), decl
, init
, cond
, incr
, body
,
10521 OMP_FOR_CLAUSES (t
) = clauses
;
10523 add_stmt (finish_omp_structured_block (stmt
));
10529 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
10530 stmt
= push_stmt_list ();
10531 RECUR (OMP_BODY (t
));
10532 stmt
= pop_stmt_list (stmt
);
10535 OMP_BODY (t
) = stmt
;
10536 OMP_CLAUSES (t
) = tmp
;
10544 stmt
= push_stmt_list ();
10545 RECUR (OMP_BODY (t
));
10546 stmt
= pop_stmt_list (stmt
);
10549 OMP_BODY (t
) = stmt
;
10554 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
10556 tree op1
= TREE_OPERAND (t
, 1);
10557 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
10558 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
10559 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
10563 case EXPR_PACK_EXPANSION
:
10564 error ("invalid use of pack expansion expression");
10565 return error_mark_node
;
10567 case NONTYPE_ARGUMENT_PACK
:
10568 error ("use %<...%> to expand argument pack");
10569 return error_mark_node
;
10572 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
10574 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10575 /*function_p=*/false,
10576 integral_constant_expression_p
);
10583 /* T is a postfix-expression that is not being used in a function
10584 call. Return the substituted version of T. */
10587 tsubst_non_call_postfix_expression (tree t
, tree args
,
10588 tsubst_flags_t complain
,
10591 if (TREE_CODE (t
) == SCOPE_REF
)
10592 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
10593 /*done=*/false, /*address_p=*/false);
10595 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10596 /*function_p=*/false,
10597 /*integral_constant_expression_p=*/false);
10602 /* Like tsubst but deals with expressions and performs semantic
10603 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10606 tsubst_copy_and_build (tree t
,
10608 tsubst_flags_t complain
,
10611 bool integral_constant_expression_p
)
10613 #define RECUR(NODE) \
10614 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10615 /*function_p=*/false, \
10616 integral_constant_expression_p)
10620 if (t
== NULL_TREE
|| t
== error_mark_node
)
10623 switch (TREE_CODE (t
))
10627 /* Fall through. */
10628 case IDENTIFIER_NODE
:
10632 bool non_integral_constant_expression_p
;
10633 const char *error_msg
;
10635 if (IDENTIFIER_TYPENAME_P (t
))
10637 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10638 t
= mangle_conv_op_name_for_type (new_type
);
10641 /* Look up the name. */
10642 decl
= lookup_name (t
);
10644 /* By convention, expressions use ERROR_MARK_NODE to indicate
10645 failure, not NULL_TREE. */
10646 if (decl
== NULL_TREE
)
10647 decl
= error_mark_node
;
10649 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
10651 integral_constant_expression_p
,
10652 /*allow_non_integral_constant_expression_p=*/false,
10653 &non_integral_constant_expression_p
,
10654 /*template_p=*/false,
10656 /*address_p=*/false,
10657 /*template_arg_p=*/false,
10661 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
10662 decl
= unqualified_name_lookup_error (decl
);
10666 case TEMPLATE_ID_EXPR
:
10669 tree
template = RECUR (TREE_OPERAND (t
, 0));
10670 tree targs
= TREE_OPERAND (t
, 1);
10673 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
10675 if (TREE_CODE (template) == COMPONENT_REF
)
10677 object
= TREE_OPERAND (template, 0);
10678 template = TREE_OPERAND (template, 1);
10681 object
= NULL_TREE
;
10682 template = lookup_template_function (template, targs
);
10685 return build3 (COMPONENT_REF
, TREE_TYPE (template),
10686 object
, template, NULL_TREE
);
10688 return baselink_for_fns (template);
10693 tree r
= RECUR (TREE_OPERAND (t
, 0));
10695 if (REFERENCE_REF_P (t
))
10697 /* A type conversion to reference type will be enclosed in
10698 such an indirect ref, but the substitution of the cast
10699 will have also added such an indirect ref. */
10700 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
10701 r
= convert_from_reference (r
);
10704 r
= build_x_indirect_ref (r
, "unary *");
10710 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10711 RECUR (TREE_OPERAND (t
, 0)));
10714 case REINTERPRET_CAST_EXPR
:
10715 case CONST_CAST_EXPR
:
10716 case DYNAMIC_CAST_EXPR
:
10717 case STATIC_CAST_EXPR
:
10722 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10723 if (integral_constant_expression_p
10724 && !cast_valid_in_integral_constant_expression_p (type
))
10726 error ("a cast to a type other than an integral or "
10727 "enumeration type cannot appear in a constant-expression");
10728 return error_mark_node
;
10731 op
= RECUR (TREE_OPERAND (t
, 0));
10733 switch (TREE_CODE (t
))
10736 return build_functional_cast (type
, op
);
10737 case REINTERPRET_CAST_EXPR
:
10738 return build_reinterpret_cast (type
, op
);
10739 case CONST_CAST_EXPR
:
10740 return build_const_cast (type
, op
);
10741 case DYNAMIC_CAST_EXPR
:
10742 return build_dynamic_cast (type
, op
);
10743 case STATIC_CAST_EXPR
:
10744 return build_static_cast (type
, op
);
10746 gcc_unreachable ();
10750 case POSTDECREMENT_EXPR
:
10751 case POSTINCREMENT_EXPR
:
10752 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10753 args
, complain
, in_decl
);
10754 return build_x_unary_op (TREE_CODE (t
), op1
);
10756 case PREDECREMENT_EXPR
:
10757 case PREINCREMENT_EXPR
:
10761 case TRUTH_NOT_EXPR
:
10762 case UNARY_PLUS_EXPR
: /* Unary + */
10763 case REALPART_EXPR
:
10764 case IMAGPART_EXPR
:
10765 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)));
10768 op1
= TREE_OPERAND (t
, 0);
10769 if (TREE_CODE (op1
) == SCOPE_REF
)
10770 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
10771 /*done=*/true, /*address_p=*/true);
10773 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
10775 if (TREE_CODE (op1
) == LABEL_DECL
)
10776 return finish_label_address_expr (DECL_NAME (op1
));
10777 return build_x_unary_op (ADDR_EXPR
, op1
);
10782 case TRUNC_DIV_EXPR
:
10783 case CEIL_DIV_EXPR
:
10784 case FLOOR_DIV_EXPR
:
10785 case ROUND_DIV_EXPR
:
10786 case EXACT_DIV_EXPR
:
10790 case TRUNC_MOD_EXPR
:
10791 case FLOOR_MOD_EXPR
:
10792 case TRUTH_ANDIF_EXPR
:
10793 case TRUTH_ORIF_EXPR
:
10794 case TRUTH_AND_EXPR
:
10795 case TRUTH_OR_EXPR
:
10810 return build_x_binary_op
10812 RECUR (TREE_OPERAND (t
, 0)),
10813 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
10815 : TREE_CODE (TREE_OPERAND (t
, 0))),
10816 RECUR (TREE_OPERAND (t
, 1)),
10817 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
10819 : TREE_CODE (TREE_OPERAND (t
, 1))),
10820 /*overloaded_p=*/NULL
);
10823 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
10824 /*address_p=*/false);
10826 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10827 args
, complain
, in_decl
);
10828 return build_x_binary_op (ARRAY_REF
, op1
,
10829 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
10831 : TREE_CODE (TREE_OPERAND (t
, 0))),
10832 RECUR (TREE_OPERAND (t
, 1)),
10833 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
10835 : TREE_CODE (TREE_OPERAND (t
, 1))),
10836 /*overloaded_p=*/NULL
);
10839 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
10841 /* We only want to compute the number of arguments. */
10842 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
10843 complain
, in_decl
);
10844 if (expanded
== error_mark_node
)
10845 return error_mark_node
;
10846 return build_int_cst (size_type_node
, TREE_VEC_LENGTH (expanded
));
10851 op1
= TREE_OPERAND (t
, 0);
10854 /* When there are no ARGS, we are trying to evaluate a
10855 non-dependent expression from the parser. Trying to do
10856 the substitutions may not work. */
10858 op1
= TREE_TYPE (op1
);
10863 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
10864 /*function_p=*/false,
10865 /*integral_constant_expression_p=*/false);
10869 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), true);
10871 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
));
10875 tree r
= build_x_modify_expr
10876 (RECUR (TREE_OPERAND (t
, 0)),
10877 TREE_CODE (TREE_OPERAND (t
, 1)),
10878 RECUR (TREE_OPERAND (t
, 2)));
10879 /* TREE_NO_WARNING must be set if either the expression was
10880 parenthesized or it uses an operator such as >>= rather
10881 than plain assignment. In the former case, it was already
10882 set and must be copied. In the latter case,
10883 build_x_modify_expr sets it and it must not be reset
10885 if (TREE_NO_WARNING (t
))
10886 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
10891 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10892 args
, complain
, in_decl
);
10893 /* Remember that there was a reference to this entity. */
10896 return build_x_arrow (op1
);
10900 tree init
= RECUR (TREE_OPERAND (t
, 3));
10902 if (TREE_OPERAND (t
, 3) && !init
)
10903 /* If there was an initializer in the the original tree, but
10904 it instantiated to an empty list, then we should pass on
10905 VOID_ZERO_NODE to tell build_new that it was an empty
10906 initializer () rather than no initializer. This can only
10907 happen when the initializer is a pack expansion whose
10908 parameter packs are of length zero. */
10909 init
= void_zero_node
;
10912 (RECUR (TREE_OPERAND (t
, 0)),
10913 RECUR (TREE_OPERAND (t
, 1)),
10914 RECUR (TREE_OPERAND (t
, 2)),
10916 NEW_EXPR_USE_GLOBAL (t
));
10920 return delete_sanity
10921 (RECUR (TREE_OPERAND (t
, 0)),
10922 RECUR (TREE_OPERAND (t
, 1)),
10923 DELETE_EXPR_USE_VEC (t
),
10924 DELETE_EXPR_USE_GLOBAL (t
));
10926 case COMPOUND_EXPR
:
10927 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
10928 RECUR (TREE_OPERAND (t
, 1)));
10937 function
= CALL_EXPR_FN (t
);
10938 /* When we parsed the expression, we determined whether or
10939 not Koenig lookup should be performed. */
10940 koenig_p
= KOENIG_LOOKUP_P (t
);
10941 if (TREE_CODE (function
) == SCOPE_REF
)
10943 qualified_p
= true;
10944 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
10946 /*address_p=*/false);
10950 if (TREE_CODE (function
) == COMPONENT_REF
)
10952 tree op
= TREE_OPERAND (function
, 1);
10954 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
10955 || (BASELINK_P (op
)
10956 && BASELINK_QUALIFIED_P (op
)));
10959 qualified_p
= false;
10961 function
= tsubst_copy_and_build (function
, args
, complain
,
10964 integral_constant_expression_p
);
10966 if (BASELINK_P (function
))
10967 qualified_p
= true;
10970 /* FIXME: Rewrite this so as not to construct an arglist. */
10971 call_args
= RECUR (CALL_EXPR_ARGS (t
));
10973 /* We do not perform argument-dependent lookup if normal
10974 lookup finds a non-function, in accordance with the
10975 expected resolution of DR 218. */
10977 && ((is_overloaded_fn (function
)
10978 /* If lookup found a member function, the Koenig lookup is
10979 not appropriate, even if an unqualified-name was used
10980 to denote the function. */
10981 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
10982 || TREE_CODE (function
) == IDENTIFIER_NODE
))
10983 function
= perform_koenig_lookup (function
, call_args
);
10985 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
10987 unqualified_name_lookup_error (function
);
10988 return error_mark_node
;
10991 /* Remember that there was a reference to this entity. */
10992 if (DECL_P (function
))
10993 mark_used (function
);
10995 if (TREE_CODE (function
) == OFFSET_REF
)
10996 return build_offset_ref_call_from_tree (function
, call_args
);
10997 if (TREE_CODE (function
) == COMPONENT_REF
)
10999 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
11000 return finish_call_expr (function
, call_args
,
11001 /*disallow_virtual=*/false,
11002 /*koenig_p=*/false);
11004 return (build_new_method_call
11005 (TREE_OPERAND (function
, 0),
11006 TREE_OPERAND (function
, 1),
11007 call_args
, NULL_TREE
,
11008 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
11011 return finish_call_expr (function
, call_args
,
11012 /*disallow_virtual=*/qualified_p
,
11017 return build_x_conditional_expr
11018 (RECUR (TREE_OPERAND (t
, 0)),
11019 RECUR (TREE_OPERAND (t
, 1)),
11020 RECUR (TREE_OPERAND (t
, 2)));
11022 case PSEUDO_DTOR_EXPR
:
11023 return finish_pseudo_destructor_expr
11024 (RECUR (TREE_OPERAND (t
, 0)),
11025 RECUR (TREE_OPERAND (t
, 1)),
11026 RECUR (TREE_OPERAND (t
, 2)));
11030 tree purpose
, value
, chain
;
11032 if (t
== void_list_node
)
11035 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
11036 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
11038 /* We have pack expansions, so expand those and
11039 create a new list out of it. */
11040 tree purposevec
= NULL_TREE
;
11041 tree valuevec
= NULL_TREE
;
11045 /* Expand the argument expressions. */
11046 if (TREE_PURPOSE (t
))
11047 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
11048 complain
, in_decl
);
11049 if (TREE_VALUE (t
))
11050 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
11051 complain
, in_decl
);
11053 /* Build the rest of the list. */
11054 chain
= TREE_CHAIN (t
);
11055 if (chain
&& chain
!= void_type_node
)
11056 chain
= RECUR (chain
);
11058 /* Determine the number of arguments. */
11059 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
11061 len
= TREE_VEC_LENGTH (purposevec
);
11062 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
11064 else if (TREE_CODE (valuevec
) == TREE_VEC
)
11065 len
= TREE_VEC_LENGTH (valuevec
);
11068 /* Since we only performed a partial substitution into
11069 the argument pack, we only return a single list
11071 if (purposevec
== TREE_PURPOSE (t
)
11072 && valuevec
== TREE_VALUE (t
)
11073 && chain
== TREE_CHAIN (t
))
11076 return tree_cons (purposevec
, valuevec
, chain
);
11079 /* Convert the argument vectors into a TREE_LIST */
11083 /* Grab the Ith values. */
11085 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
11088 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
11091 /* Build the list (backwards). */
11092 chain
= tree_cons (purpose
, value
, chain
);
11098 purpose
= TREE_PURPOSE (t
);
11100 purpose
= RECUR (purpose
);
11101 value
= TREE_VALUE (t
);
11103 value
= RECUR (value
);
11104 chain
= TREE_CHAIN (t
);
11105 if (chain
&& chain
!= void_type_node
)
11106 chain
= RECUR (chain
);
11107 if (purpose
== TREE_PURPOSE (t
)
11108 && value
== TREE_VALUE (t
)
11109 && chain
== TREE_CHAIN (t
))
11111 return tree_cons (purpose
, value
, chain
);
11114 case COMPONENT_REF
:
11120 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11121 args
, complain
, in_decl
);
11122 /* Remember that there was a reference to this entity. */
11123 if (DECL_P (object
))
11124 mark_used (object
);
11125 object_type
= TREE_TYPE (object
);
11127 member
= TREE_OPERAND (t
, 1);
11128 if (BASELINK_P (member
))
11129 member
= tsubst_baselink (member
,
11130 non_reference (TREE_TYPE (object
)),
11131 args
, complain
, in_decl
);
11133 member
= tsubst_copy (member
, args
, complain
, in_decl
);
11134 if (member
== error_mark_node
)
11135 return error_mark_node
;
11137 if (object_type
&& !CLASS_TYPE_P (object_type
))
11139 if (SCALAR_TYPE_P (object_type
))
11141 tree s
= NULL_TREE
;
11142 tree dtor
= member
;
11144 if (TREE_CODE (dtor
) == SCOPE_REF
)
11146 s
= TREE_OPERAND (dtor
, 0);
11147 dtor
= TREE_OPERAND (dtor
, 1);
11149 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
11151 dtor
= TREE_OPERAND (dtor
, 0);
11153 return finish_pseudo_destructor_expr (object
, s
, dtor
);
11157 else if (TREE_CODE (member
) == SCOPE_REF
11158 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
11163 /* Lookup the template functions now that we know what the
11165 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
11166 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
11167 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
11168 /*is_type_p=*/false,
11169 /*complain=*/false);
11170 if (BASELINK_P (member
))
11172 BASELINK_FUNCTIONS (member
)
11173 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
11175 member
= (adjust_result_of_qualified_name_lookup
11176 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
11181 qualified_name_lookup_error (object_type
, tmpl
, member
);
11182 return error_mark_node
;
11185 else if (TREE_CODE (member
) == SCOPE_REF
11186 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
11187 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
11189 if (complain
& tf_error
)
11191 if (TYPE_P (TREE_OPERAND (member
, 0)))
11192 error ("%qT is not a class or namespace",
11193 TREE_OPERAND (member
, 0));
11195 error ("%qD is not a class or namespace",
11196 TREE_OPERAND (member
, 0));
11198 return error_mark_node
;
11200 else if (TREE_CODE (member
) == FIELD_DECL
)
11201 return finish_non_static_data_member (member
, object
, NULL_TREE
);
11203 return finish_class_member_access_expr (object
, member
,
11204 /*template_p=*/false);
11209 (RECUR (TREE_OPERAND (t
, 0)));
11213 VEC(constructor_elt
,gc
) *n
;
11214 constructor_elt
*ce
;
11215 unsigned HOST_WIDE_INT idx
;
11216 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11217 bool process_index_p
;
11219 bool need_copy_p
= false;
11221 if (type
== error_mark_node
)
11222 return error_mark_node
;
11224 /* digest_init will do the wrong thing if we let it. */
11225 if (type
&& TYPE_PTRMEMFUNC_P (type
))
11228 /* We do not want to process the index of aggregate
11229 initializers as they are identifier nodes which will be
11230 looked up by digest_init. */
11231 process_index_p
= !(type
&& IS_AGGR_TYPE (type
));
11233 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
11234 newlen
= VEC_length (constructor_elt
, n
);
11235 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
11237 if (ce
->index
&& process_index_p
)
11238 ce
->index
= RECUR (ce
->index
);
11240 if (PACK_EXPANSION_P (ce
->value
))
11242 /* Substitute into the pack expansion. */
11243 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
11246 if (ce
->value
== error_mark_node
)
11248 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
11249 /* Just move the argument into place. */
11250 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
11253 /* Update the length of the final CONSTRUCTOR
11254 arguments vector, and note that we will need to
11256 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
11257 need_copy_p
= true;
11261 ce
->value
= RECUR (ce
->value
);
11266 VEC(constructor_elt
,gc
) *old_n
= n
;
11268 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
11269 for (idx
= 0; VEC_iterate (constructor_elt
, old_n
, idx
, ce
);
11272 if (TREE_CODE (ce
->value
) == TREE_VEC
)
11274 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
11275 for (i
= 0; i
< len
; ++i
)
11276 CONSTRUCTOR_APPEND_ELT (n
, 0,
11277 TREE_VEC_ELT (ce
->value
, i
));
11280 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
11284 if (TREE_HAS_CONSTRUCTOR (t
))
11285 return finish_compound_literal (type
, n
);
11287 return build_constructor (NULL_TREE
, n
);
11292 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
11293 if (TYPE_P (operand_0
))
11294 return get_typeid (operand_0
);
11295 return build_typeid (operand_0
);
11305 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
11307 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
11308 /* If the original type was a reference, we'll be wrapped in
11309 the appropriate INDIRECT_REF. */
11310 r
= convert_from_reference (r
);
11315 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
11316 tsubst_copy (TREE_TYPE (t
), args
, complain
,
11319 case OFFSETOF_EXPR
:
11320 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
11324 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
11325 complain
, in_decl
);
11327 tree type2
= TRAIT_EXPR_TYPE2 (t
);
11329 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
11331 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
11336 tree old_stmt_expr
= cur_stmt_expr
;
11337 tree stmt_expr
= begin_stmt_expr ();
11339 cur_stmt_expr
= stmt_expr
;
11340 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
11341 integral_constant_expression_p
);
11342 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
11343 cur_stmt_expr
= old_stmt_expr
;
11349 t
= tsubst_copy (t
, args
, complain
, in_decl
);
11350 /* As in finish_id_expression, we resolve enumeration constants
11351 to their underlying values. */
11352 if (TREE_CODE (t
) == CONST_DECL
)
11354 used_types_insert (TREE_TYPE (t
));
11355 return DECL_INITIAL (t
);
11360 /* Handle Objective-C++ constructs, if appropriate. */
11363 = objcp_tsubst_copy_and_build (t
, args
, complain
,
11364 in_decl
, /*function_p=*/false);
11368 return tsubst_copy (t
, args
, complain
, in_decl
);
11374 /* Verify that the instantiated ARGS are valid. For type arguments,
11375 make sure that the type's linkage is ok. For non-type arguments,
11376 make sure they are constants if they are integral or enumerations.
11377 Emit an error under control of COMPLAIN, and return TRUE on error. */
11380 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
11382 int ix
, len
= DECL_NTPARMS (tmpl
);
11383 bool result
= false;
11385 for (ix
= 0; ix
!= len
; ix
++)
11387 tree t
= TREE_VEC_ELT (args
, ix
);
11391 /* [basic.link]: A name with no linkage (notably, the name
11392 of a class or enumeration declared in a local scope)
11393 shall not be used to declare an entity with linkage.
11394 This implies that names with no linkage cannot be used as
11395 template arguments. */
11396 tree nt
= no_linkage_check (t
, /*relaxed_p=*/false);
11400 /* DR 488 makes use of a type with no linkage cause
11401 type deduction to fail. */
11402 if (complain
& tf_error
)
11404 if (TYPE_ANONYMOUS_P (nt
))
11405 error ("%qT is/uses anonymous type", t
);
11407 error ("template argument for %qD uses local type %qT",
11412 /* In order to avoid all sorts of complications, we do not
11413 allow variably-modified types as template arguments. */
11414 else if (variably_modified_type_p (t
, NULL_TREE
))
11416 if (complain
& tf_error
)
11417 error ("%qT is a variably modified type", t
);
11421 /* A non-type argument of integral or enumerated type must be a
11423 else if (TREE_TYPE (t
)
11424 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
11425 && !TREE_CONSTANT (t
))
11427 if (complain
& tf_error
)
11428 error ("integral expression %qE is not constant", t
);
11432 if (result
&& (complain
& tf_error
))
11433 error (" trying to instantiate %qD", tmpl
);
11437 /* Instantiate the indicated variable or function template TMPL with
11438 the template arguments in TARG_PTR. */
11441 instantiate_template (tree tmpl
, tree targ_ptr
, tsubst_flags_t complain
)
11446 HOST_WIDE_INT saved_processing_template_decl
;
11448 if (tmpl
== error_mark_node
)
11449 return error_mark_node
;
11451 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
11453 /* If this function is a clone, handle it specially. */
11454 if (DECL_CLONED_FUNCTION_P (tmpl
))
11459 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
,
11461 if (spec
== error_mark_node
)
11462 return error_mark_node
;
11464 /* Look for the clone. */
11465 FOR_EACH_CLONE (clone
, spec
)
11466 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
11468 /* We should always have found the clone by now. */
11469 gcc_unreachable ();
11473 /* Check to see if we already have this specialization. */
11474 spec
= retrieve_specialization (tmpl
, targ_ptr
,
11475 /*class_specializations_p=*/false);
11476 if (spec
!= NULL_TREE
)
11479 gen_tmpl
= most_general_template (tmpl
);
11480 if (tmpl
!= gen_tmpl
)
11482 /* The TMPL is a partial instantiation. To get a full set of
11483 arguments we must add the arguments used to perform the
11484 partial instantiation. */
11485 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
11488 /* Check to see if we already have this specialization. */
11489 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
,
11490 /*class_specializations_p=*/false);
11491 if (spec
!= NULL_TREE
)
11495 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
11497 return error_mark_node
;
11499 /* We are building a FUNCTION_DECL, during which the access of its
11500 parameters and return types have to be checked. However this
11501 FUNCTION_DECL which is the desired context for access checking
11502 is not built yet. We solve this chicken-and-egg problem by
11503 deferring all checks until we have the FUNCTION_DECL. */
11504 push_deferring_access_checks (dk_deferred
);
11506 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11507 (because, for example, we have encountered a non-dependent
11508 function call in the body of a template function and must now
11509 determine which of several overloaded functions will be called),
11510 within the instantiation itself we are not processing a
11512 saved_processing_template_decl
= processing_template_decl
;
11513 processing_template_decl
= 0;
11514 /* Substitute template parameters to obtain the specialization. */
11515 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
11516 targ_ptr
, complain
, gen_tmpl
);
11517 processing_template_decl
= saved_processing_template_decl
;
11518 if (fndecl
== error_mark_node
)
11519 return error_mark_node
;
11521 /* Now we know the specialization, compute access previously
11523 push_access_scope (fndecl
);
11524 perform_deferred_access_checks ();
11525 pop_access_scope (fndecl
);
11526 pop_deferring_access_checks ();
11528 /* The DECL_TI_TEMPLATE should always be the immediate parent
11529 template, not the most general template. */
11530 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
11532 /* If we've just instantiated the main entry point for a function,
11533 instantiate all the alternate entry points as well. We do this
11534 by cloning the instantiation of the main entry point, not by
11535 instantiating the template clones. */
11536 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
11537 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
11542 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11543 arguments that are being used when calling it. TARGS is a vector
11544 into which the deduced template arguments are placed.
11546 Return zero for success, 2 for an incomplete match that doesn't resolve
11547 all the types, and 1 for complete failure. An error message will be
11548 printed only for an incomplete match.
11550 If FN is a conversion operator, or we are trying to produce a specific
11551 specialization, RETURN_TYPE is the return type desired.
11553 The EXPLICIT_TARGS are explicit template arguments provided via a
11556 The parameter STRICT is one of:
11559 We are deducing arguments for a function call, as in
11560 [temp.deduct.call].
11563 We are deducing arguments for a conversion function, as in
11564 [temp.deduct.conv].
11567 We are deducing arguments when doing an explicit instantiation
11568 as in [temp.explicit], when determining an explicit specialization
11569 as in [temp.expl.spec], or when taking the address of a function
11570 template, as in [temp.deduct.funcaddr]. */
11573 fn_type_unification (tree fn
,
11574 tree explicit_targs
,
11578 unification_kind_t strict
,
11584 bool incomplete_argument_packs_p
= false;
11586 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
11588 fntype
= TREE_TYPE (fn
);
11589 if (explicit_targs
)
11593 The specified template arguments must match the template
11594 parameters in kind (i.e., type, nontype, template), and there
11595 must not be more arguments than there are parameters;
11596 otherwise type deduction fails.
11598 Nontype arguments must match the types of the corresponding
11599 nontype template parameters, or must be convertible to the
11600 types of the corresponding nontype parameters as specified in
11601 _temp.arg.nontype_, otherwise type deduction fails.
11603 All references in the function type of the function template
11604 to the corresponding template parameters are replaced by the
11605 specified template argument values. If a substitution in a
11606 template parameter or in the function type of the function
11607 template results in an invalid type, type deduction fails. */
11608 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
11609 int i
, len
= TREE_VEC_LENGTH (tparms
);
11610 tree converted_args
;
11611 bool incomplete
= false;
11613 if (explicit_targs
== error_mark_node
)
11617 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
11618 /*require_all_args=*/false,
11619 /*use_default_args=*/false));
11620 if (converted_args
== error_mark_node
)
11623 /* Substitute the explicit args into the function type. This is
11624 necessary so that, for instance, explicitly declared function
11625 arguments can match null pointed constants. If we were given
11626 an incomplete set of explicit args, we must not do semantic
11627 processing during substitution as we could create partial
11629 for (i
= 0; i
< len
; i
++)
11631 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
11632 bool parameter_pack
= false;
11634 /* Dig out the actual parm. */
11635 if (TREE_CODE (parm
) == TYPE_DECL
11636 || TREE_CODE (parm
) == TEMPLATE_DECL
)
11638 parm
= TREE_TYPE (parm
);
11639 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
11641 else if (TREE_CODE (parm
) == PARM_DECL
)
11643 parm
= DECL_INITIAL (parm
);
11644 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
11647 if (parameter_pack
)
11651 template_parm_level_and_index (parm
, &level
, &idx
);
11653 /* Mark the argument pack as "incomplete". We could
11654 still deduce more arguments during unification. */
11655 targ
= TMPL_ARG (converted_args
, level
, idx
);
11658 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
11659 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
11660 = ARGUMENT_PACK_ARGS (targ
);
11663 /* We have some incomplete argument packs. */
11664 incomplete_argument_packs_p
= true;
11668 if (incomplete_argument_packs_p
)
11669 /* Any substitution is guaranteed to be incomplete if there
11670 are incomplete argument packs, because we can still deduce
11674 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
11676 processing_template_decl
+= incomplete
;
11677 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
11678 processing_template_decl
-= incomplete
;
11680 if (fntype
== error_mark_node
)
11683 /* Place the explicitly specified arguments in TARGS. */
11684 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
11685 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
11688 /* Never do unification on the 'this' parameter. */
11689 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
11693 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
11694 args
= tree_cons (NULL_TREE
, return_type
, args
);
11697 /* We allow incomplete unification without an error message here
11698 because the standard doesn't seem to explicitly prohibit it. Our
11699 callers must be ready to deal with unification failures in any
11701 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
11702 targs
, parms
, args
, /*subr=*/0,
11705 if (result
== 0 && incomplete_argument_packs_p
)
11707 int i
, len
= NUM_TMPL_ARGS (targs
);
11709 /* Clear the "incomplete" flags on all argument packs. */
11710 for (i
= 0; i
< len
; i
++)
11712 tree arg
= TREE_VEC_ELT (targs
, i
);
11713 if (ARGUMENT_PACK_P (arg
))
11715 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
11716 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
11721 /* Now that we have bindings for all of the template arguments,
11722 ensure that the arguments deduced for the template template
11723 parameters have compatible template parameter lists. We cannot
11724 check this property before we have deduced all template
11725 arguments, because the template parameter types of a template
11726 template parameter might depend on prior template parameters
11727 deduced after the template template parameter. The following
11728 ill-formed example illustrates this issue:
11730 template<typename T, template<T> class C> void f(C<5>, T);
11732 template<int N> struct X {};
11735 f(X<5>(), 5l); // error: template argument deduction fails
11738 The template parameter list of 'C' depends on the template type
11739 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
11740 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
11741 time that we deduce 'C'. */
11743 && !template_template_parm_bindings_ok_p
11744 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
11748 /* All is well so far. Now, check:
11752 When all template arguments have been deduced, all uses of
11753 template parameters in nondeduced contexts are replaced with
11754 the corresponding deduced argument values. If the
11755 substitution results in an invalid type, as described above,
11756 type deduction fails. */
11757 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
11758 == error_mark_node
)
11764 /* Adjust types before performing type deduction, as described in
11765 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11766 sections are symmetric. PARM is the type of a function parameter
11767 or the return type of the conversion function. ARG is the type of
11768 the argument passed to the call, or the type of the value
11769 initialized with the result of the conversion function.
11770 ARG_EXPR is the original argument expression, which may be null. */
11773 maybe_adjust_types_for_deduction (unification_kind_t strict
,
11787 /* Swap PARM and ARG throughout the remainder of this
11788 function; the handling is precisely symmetric since PARM
11789 will initialize ARG rather than vice versa. */
11797 /* There is nothing to do in this case. */
11801 gcc_unreachable ();
11804 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
11806 /* [temp.deduct.call]
11808 If P is not a reference type:
11810 --If A is an array type, the pointer type produced by the
11811 array-to-pointer standard conversion (_conv.array_) is
11812 used in place of A for type deduction; otherwise,
11814 --If A is a function type, the pointer type produced by
11815 the function-to-pointer standard conversion
11816 (_conv.func_) is used in place of A for type deduction;
11819 --If A is a cv-qualified type, the top level
11820 cv-qualifiers of A's type are ignored for type
11822 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
11823 *arg
= build_pointer_type (TREE_TYPE (*arg
));
11824 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
11825 *arg
= build_pointer_type (*arg
);
11827 *arg
= TYPE_MAIN_VARIANT (*arg
);
11830 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
11831 of the form T&&, where T is a template parameter, and the argument
11832 is an lvalue, T is deduced as A& */
11833 if (TREE_CODE (*parm
) == REFERENCE_TYPE
11834 && TYPE_REF_IS_RVALUE (*parm
)
11835 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
11836 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
11837 && arg_expr
&& real_lvalue_p (arg_expr
))
11838 *arg
= build_reference_type (*arg
);
11840 /* [temp.deduct.call]
11842 If P is a cv-qualified type, the top level cv-qualifiers
11843 of P's type are ignored for type deduction. If P is a
11844 reference type, the type referred to by P is used for
11846 *parm
= TYPE_MAIN_VARIANT (*parm
);
11847 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
11849 *parm
= TREE_TYPE (*parm
);
11850 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
11853 /* DR 322. For conversion deduction, remove a reference type on parm
11854 too (which has been swapped into ARG). */
11855 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
11856 *arg
= TREE_TYPE (*arg
);
11861 /* Most parms like fn_type_unification.
11863 If SUBR is 1, we're being called recursively (to unify the
11864 arguments of a function or method parameter of a function
11868 type_unification_real (tree tparms
,
11873 unification_kind_t strict
,
11876 tree parm
, arg
, arg_expr
;
11878 int ntparms
= TREE_VEC_LENGTH (tparms
);
11880 int saw_undeduced
= 0;
11883 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
11884 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
11885 gcc_assert (!xargs
|| TREE_CODE (xargs
) == TREE_LIST
);
11886 gcc_assert (ntparms
> 0);
11891 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
11892 | UNIFY_ALLOW_DERIVED
);
11896 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
11900 sub_strict
= UNIFY_ALLOW_NONE
;
11904 gcc_unreachable ();
11911 while (parms
&& parms
!= void_list_node
11912 && args
&& args
!= void_list_node
)
11914 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
11917 parm
= TREE_VALUE (parms
);
11918 parms
= TREE_CHAIN (parms
);
11919 arg
= TREE_VALUE (args
);
11920 args
= TREE_CHAIN (args
);
11923 if (arg
== error_mark_node
)
11925 if (arg
== unknown_type_node
)
11926 /* We can't deduce anything from this, but we might get all the
11927 template args from other function args. */
11930 /* Conversions will be performed on a function argument that
11931 corresponds with a function parameter that contains only
11932 non-deducible template parameters and explicitly specified
11933 template parameters. */
11934 if (!uses_template_parms (parm
))
11939 type
= TREE_TYPE (arg
);
11943 if (same_type_p (parm
, type
))
11945 if (strict
!= DEDUCE_EXACT
11946 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
11955 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
11956 if (type_unknown_p (arg
))
11958 /* [temp.deduct.type]
11960 A template-argument can be deduced from a pointer to
11961 function or pointer to member function argument if
11962 the set of overloaded functions does not contain
11963 function templates and at most one of a set of
11964 overloaded functions provides a unique match. */
11965 if (resolve_overloaded_unification
11966 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
11972 arg
= unlowered_expr_type (arg
);
11973 if (arg
== error_mark_node
)
11978 int arg_strict
= sub_strict
;
11981 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
11984 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
11991 && parms
!= void_list_node
11992 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
11994 /* Unify the remaining arguments with the pack expansion type. */
11996 tree parmvec
= make_tree_vec (1);
12000 /* Count the number of arguments that remain. */
12001 for (t
= args
; t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
12004 /* Allocate a TREE_VEC and copy in all of the arguments */
12005 argvec
= make_tree_vec (len
);
12006 for (i
= 0; args
&& args
!= void_list_node
; args
= TREE_CHAIN (args
))
12008 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (args
);
12012 /* Copy the parameter into parmvec. */
12013 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
12014 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
12015 /*call_args_p=*/true, /*subr=*/subr
))
12018 /* Advance to the end of the list of parameters. */
12019 parms
= TREE_CHAIN (parms
);
12022 /* Fail if we've reached the end of the parm list, and more args
12023 are present, and the parm list isn't variadic. */
12024 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
12026 /* Fail if parms are left and they don't have default values. */
12027 if (parms
&& parms
!= void_list_node
12028 && TREE_PURPOSE (parms
) == NULL_TREE
)
12032 for (i
= 0; i
< ntparms
; i
++)
12033 if (!TREE_VEC_ELT (targs
, i
))
12037 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
12040 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
12042 /* If this is an undeduced nontype parameter that depends on
12043 a type parameter, try another pass; its type may have been
12044 deduced from a later argument than the one from which
12045 this parameter can be deduced. */
12046 if (TREE_CODE (tparm
) == PARM_DECL
12047 && uses_template_parms (TREE_TYPE (tparm
))
12048 && !saw_undeduced
++)
12051 /* Core issue #226 (C++0x) [temp.deduct]:
12053 If a template argument has not been deduced, its
12054 default template argument, if any, is used.
12056 When we are in C++98 mode, TREE_PURPOSE will either
12057 be NULL_TREE or ERROR_MARK_NODE, so we do not need
12058 to explicitly check cxx_dialect here. */
12059 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
12061 tree arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)),
12062 targs
, tf_none
, NULL_TREE
);
12063 if (arg
== error_mark_node
)
12067 TREE_VEC_ELT (targs
, i
) = arg
;
12072 /* If the type parameter is a parameter pack, then it will
12073 be deduced to an empty parameter pack. */
12074 if (template_parameter_pack_p (tparm
))
12078 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
12080 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
12081 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
12082 TREE_CONSTANT (arg
) = 1;
12085 arg
= make_node (TYPE_ARGUMENT_PACK
);
12087 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
12089 TREE_VEC_ELT (targs
, i
) = arg
;
12099 /* Subroutine of type_unification_real. Args are like the variables
12100 at the call site. ARG is an overloaded function (or template-id);
12101 we try deducing template args from each of the overloads, and if
12102 only one succeeds, we go with that. Modifies TARGS and returns
12103 true on success. */
12106 resolve_overloaded_unification (tree tparms
,
12110 unification_kind_t strict
,
12113 tree tempargs
= copy_node (targs
);
12117 if (TREE_CODE (arg
) == ADDR_EXPR
)
12119 arg
= TREE_OPERAND (arg
, 0);
12125 if (TREE_CODE (arg
) == COMPONENT_REF
)
12126 /* Handle `&x' where `x' is some static or non-static member
12128 arg
= TREE_OPERAND (arg
, 1);
12130 if (TREE_CODE (arg
) == OFFSET_REF
)
12131 arg
= TREE_OPERAND (arg
, 1);
12133 /* Strip baselink information. */
12134 if (BASELINK_P (arg
))
12135 arg
= BASELINK_FUNCTIONS (arg
);
12137 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
12139 /* If we got some explicit template args, we need to plug them into
12140 the affected templates before we try to unify, in case the
12141 explicit args will completely resolve the templates in question. */
12143 tree expl_subargs
= TREE_OPERAND (arg
, 1);
12144 arg
= TREE_OPERAND (arg
, 0);
12146 for (; arg
; arg
= OVL_NEXT (arg
))
12148 tree fn
= OVL_CURRENT (arg
);
12149 tree subargs
, elem
;
12151 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
12154 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
12155 expl_subargs
, /*check_ret=*/false);
12158 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
12159 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
12160 elem
, strict
, sub_strict
, addr_p
);
12164 else if (TREE_CODE (arg
) != OVERLOAD
12165 && TREE_CODE (arg
) != FUNCTION_DECL
)
12166 /* If ARG is, for example, "(0, &f)" then its type will be unknown
12167 -- but the deduction does not succeed because the expression is
12168 not just the function on its own. */
12171 for (; arg
; arg
= OVL_NEXT (arg
))
12172 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
12173 TREE_TYPE (OVL_CURRENT (arg
)),
12174 strict
, sub_strict
, addr_p
);
12176 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12177 to function or pointer to member function argument if the set of
12178 overloaded functions does not contain function templates and at most
12179 one of a set of overloaded functions provides a unique match.
12181 So if we found multiple possibilities, we return success but don't
12182 deduce anything. */
12186 int i
= TREE_VEC_LENGTH (targs
);
12188 if (TREE_VEC_ELT (tempargs
, i
))
12189 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
12197 /* Subroutine of resolve_overloaded_unification; does deduction for a single
12198 overload. Fills TARGS with any deduced arguments, or error_mark_node if
12199 different overloads deduce different arguments for a given parm.
12200 ADDR_P is true if the expression for which deduction is being
12201 performed was of the form "& fn" rather than simply "fn".
12203 Returns 1 on success. */
12206 try_one_overload (tree tparms
,
12211 unification_kind_t strict
,
12219 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12220 to function or pointer to member function argument if the set of
12221 overloaded functions does not contain function templates and at most
12222 one of a set of overloaded functions provides a unique match.
12224 So if this is a template, just return success. */
12226 if (uses_template_parms (arg
))
12229 if (TREE_CODE (arg
) == METHOD_TYPE
)
12230 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
12232 arg
= build_pointer_type (arg
);
12234 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
12236 /* We don't copy orig_targs for this because if we have already deduced
12237 some template args from previous args, unify would complain when we
12238 try to deduce a template parameter for the same argument, even though
12239 there isn't really a conflict. */
12240 nargs
= TREE_VEC_LENGTH (targs
);
12241 tempargs
= make_tree_vec (nargs
);
12243 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
12246 /* First make sure we didn't deduce anything that conflicts with
12247 explicitly specified args. */
12248 for (i
= nargs
; i
--; )
12250 tree elt
= TREE_VEC_ELT (tempargs
, i
);
12251 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
12255 else if (uses_template_parms (elt
))
12256 /* Since we're unifying against ourselves, we will fill in
12257 template args used in the function parm list with our own
12258 template parms. Discard them. */
12259 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
12260 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
12264 for (i
= nargs
; i
--; )
12266 tree elt
= TREE_VEC_ELT (tempargs
, i
);
12269 TREE_VEC_ELT (targs
, i
) = elt
;
12275 /* PARM is a template class (perhaps with unbound template
12276 parameters). ARG is a fully instantiated type. If ARG can be
12277 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
12278 TARGS are as for unify. */
12281 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
12283 tree copy_of_targs
;
12285 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
12286 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
12287 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
12290 /* We need to make a new template argument vector for the call to
12291 unify. If we used TARGS, we'd clutter it up with the result of
12292 the attempted unification, even if this class didn't work out.
12293 We also don't want to commit ourselves to all the unifications
12294 we've already done, since unification is supposed to be done on
12295 an argument-by-argument basis. In other words, consider the
12296 following pathological case:
12298 template <int I, int J, int K>
12301 template <int I, int J>
12302 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12304 template <int I, int J, int K>
12305 void f(S<I, J, K>, S<I, I, I>);
12314 Now, by the time we consider the unification involving `s2', we
12315 already know that we must have `f<0, 0, 0>'. But, even though
12316 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12317 because there are two ways to unify base classes of S<0, 1, 2>
12318 with S<I, I, I>. If we kept the already deduced knowledge, we
12319 would reject the possibility I=1. */
12320 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
12322 /* If unification failed, we're done. */
12323 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
12324 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
12330 /* Given a template type PARM and a class type ARG, find the unique
12331 base type in ARG that is an instance of PARM. We do not examine
12332 ARG itself; only its base-classes. If there is not exactly one
12333 appropriate base class, return NULL_TREE. PARM may be the type of
12334 a partial specialization, as well as a plain template type. Used
12338 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
12340 tree rval
= NULL_TREE
;
12343 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)));
12345 binfo
= TYPE_BINFO (complete_type (arg
));
12347 /* The type could not be completed. */
12350 /* Walk in inheritance graph order. The search order is not
12351 important, and this avoids multiple walks of virtual bases. */
12352 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
12354 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
12358 /* If there is more than one satisfactory baseclass, then:
12362 If they yield more than one possible deduced A, the type
12366 if (rval
&& !same_type_p (r
, rval
))
12376 /* Returns the level of DECL, which declares a template parameter. */
12379 template_decl_level (tree decl
)
12381 switch (TREE_CODE (decl
))
12384 case TEMPLATE_DECL
:
12385 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
12388 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
12391 gcc_unreachable ();
12396 /* Decide whether ARG can be unified with PARM, considering only the
12397 cv-qualifiers of each type, given STRICT as documented for unify.
12398 Returns nonzero iff the unification is OK on that basis. */
12401 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
12403 int arg_quals
= cp_type_quals (arg
);
12404 int parm_quals
= cp_type_quals (parm
);
12406 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12407 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12409 /* Although a CVR qualifier is ignored when being applied to a
12410 substituted template parameter ([8.3.2]/1 for example), that
12411 does not apply during deduction [14.8.2.4]/1, (even though
12412 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12413 this). Except when we're allowing additional CV qualifiers
12414 at the outer level [14.8.2.1]/3,1st bullet. */
12415 if ((TREE_CODE (arg
) == REFERENCE_TYPE
12416 || TREE_CODE (arg
) == FUNCTION_TYPE
12417 || TREE_CODE (arg
) == METHOD_TYPE
)
12418 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
12421 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
12422 && (parm_quals
& TYPE_QUAL_RESTRICT
))
12426 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12427 && (arg_quals
& parm_quals
) != parm_quals
)
12430 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
12431 && (parm_quals
& arg_quals
) != arg_quals
)
12437 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12439 template_parm_level_and_index (tree parm
, int* level
, int* index
)
12441 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12442 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12443 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12445 *index
= TEMPLATE_TYPE_IDX (parm
);
12446 *level
= TEMPLATE_TYPE_LEVEL (parm
);
12450 *index
= TEMPLATE_PARM_IDX (parm
);
12451 *level
= TEMPLATE_PARM_LEVEL (parm
);
12455 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12456 expansion at the end of PACKED_PARMS. Returns 0 if the type
12457 deduction succeeds, 1 otherwise. STRICT is the same as in
12458 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12459 call argument list. We'll need to adjust the arguments to make them
12460 types. SUBR tells us if this is from a recursive call to
12461 type_unification_real. */
12463 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
12464 tree packed_args
, int strict
, bool call_args_p
,
12468 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
12469 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
12470 tree pack
, packs
= NULL_TREE
;
12471 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
12472 int len
= TREE_VEC_LENGTH (packed_args
);
12474 /* Determine the parameter packs we will be deducing from the
12475 pattern, and record their current deductions. */
12476 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
12477 pack
; pack
= TREE_CHAIN (pack
))
12479 tree parm_pack
= TREE_VALUE (pack
);
12482 /* Determine the index and level of this parameter pack. */
12483 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12485 /* Keep track of the parameter packs and their corresponding
12487 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
12488 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
12491 /* Loop through all of the arguments that have not yet been
12492 unified and unify each with the pattern. */
12493 for (i
= start
; i
< len
; i
++)
12495 tree parm
= pattern
;
12497 /* For each parameter pack, clear out the deduced value so that
12498 we can deduce it again. */
12499 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12502 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12504 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
12507 /* Unify the pattern with the current argument. */
12509 tree arg
= TREE_VEC_ELT (packed_args
, i
);
12510 tree arg_expr
= NULL_TREE
;
12511 int arg_strict
= strict
;
12512 bool skip_arg_p
= false;
12518 /* This mirrors what we do in type_unification_real. */
12522 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
12523 | UNIFY_ALLOW_MORE_CV_QUAL
12524 | UNIFY_ALLOW_DERIVED
);
12528 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
12532 sub_strict
= UNIFY_ALLOW_NONE
;
12536 gcc_unreachable ();
12541 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
12542 if (type_unknown_p (arg
))
12544 /* [temp.deduct.type] A template-argument can be
12545 deduced from a pointer to function or pointer
12546 to member function argument if the set of
12547 overloaded functions does not contain function
12548 templates and at most one of a set of
12549 overloaded functions provides a unique
12552 if (resolve_overloaded_unification
12553 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
12562 arg
= unlowered_expr_type (arg
);
12563 if (arg
== error_mark_node
)
12568 arg_strict
= sub_strict
;
12572 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
12578 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
12583 /* For each parameter pack, collect the deduced value. */
12584 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12587 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12589 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
12590 TMPL_ARG (targs
, level
, idx
);
12594 /* Verify that the results of unification with the parameter packs
12595 produce results consistent with what we've seen before, and make
12596 the deduced argument packs available. */
12597 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12599 tree old_pack
= TREE_VALUE (pack
);
12600 tree new_args
= TREE_TYPE (pack
);
12601 int i
, len
= TREE_VEC_LENGTH (new_args
);
12602 bool nondeduced_p
= false;
12604 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
12605 actually deduce anything. */
12606 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
12607 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
12608 nondeduced_p
= true;
12612 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
12614 /* Prepend the explicit arguments onto NEW_ARGS. */
12615 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
12616 tree old_args
= new_args
;
12617 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
12618 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
12620 /* Copy the explicit arguments. */
12621 new_args
= make_tree_vec (len
);
12622 for (i
= 0; i
< explicit_len
; i
++)
12623 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
12625 /* Copy the deduced arguments. */
12626 for (; i
< len
; i
++)
12627 TREE_VEC_ELT (new_args
, i
) =
12628 TREE_VEC_ELT (old_args
, i
- explicit_len
);
12636 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12638 /* Build the deduced *_ARGUMENT_PACK. */
12639 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
12641 result
= make_node (NONTYPE_ARGUMENT_PACK
);
12642 TREE_TYPE (result
) =
12643 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
12644 TREE_CONSTANT (result
) = 1;
12647 result
= make_node (TYPE_ARGUMENT_PACK
);
12649 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
12651 /* Note the deduced argument packs for this parameter
12653 TMPL_ARG (targs
, level
, idx
) = result
;
12655 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
12656 && (ARGUMENT_PACK_ARGS (old_pack
)
12657 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
12659 /* We only had the explicitly-provided arguments before, but
12660 now we have a complete set of arguments. */
12662 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
12663 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12665 /* Keep the original deduced argument pack. */
12666 TMPL_ARG (targs
, level
, idx
) = old_pack
;
12668 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
12669 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
12670 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
12672 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
12674 /* Inconsistent unification of this parameter pack. */
12680 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12682 /* Keep the original deduced argument pack. */
12683 TMPL_ARG (targs
, level
, idx
) = old_pack
;
12690 /* Deduce the value of template parameters. TPARMS is the (innermost)
12691 set of template parameters to a template. TARGS is the bindings
12692 for those template parameters, as determined thus far; TARGS may
12693 include template arguments for outer levels of template parameters
12694 as well. PARM is a parameter to a template function, or a
12695 subcomponent of that parameter; ARG is the corresponding argument.
12696 This function attempts to match PARM with ARG in a manner
12697 consistent with the existing assignments in TARGS. If more values
12698 are deduced, then TARGS is updated.
12700 Returns 0 if the type deduction succeeds, 1 otherwise. The
12701 parameter STRICT is a bitwise or of the following flags:
12704 Require an exact match between PARM and ARG.
12705 UNIFY_ALLOW_MORE_CV_QUAL:
12706 Allow the deduced ARG to be more cv-qualified (by qualification
12707 conversion) than ARG.
12708 UNIFY_ALLOW_LESS_CV_QUAL:
12709 Allow the deduced ARG to be less cv-qualified than ARG.
12710 UNIFY_ALLOW_DERIVED:
12711 Allow the deduced ARG to be a template base class of ARG,
12712 or a pointer to a template base class of the type pointed to by
12714 UNIFY_ALLOW_INTEGER:
12715 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12716 case for more information.
12717 UNIFY_ALLOW_OUTER_LEVEL:
12718 This is the outermost level of a deduction. Used to determine validity
12719 of qualification conversions. A valid qualification conversion must
12720 have const qualified pointers leading up to the inner type which
12721 requires additional CV quals, except at the outer level, where const
12722 is not required [conv.qual]. It would be normal to set this flag in
12723 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12724 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12725 This is the outermost level of a deduction, and PARM can be more CV
12726 qualified at this point.
12727 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12728 This is the outermost level of a deduction, and PARM can be less CV
12729 qualified at this point. */
12732 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
12737 int strict_in
= strict
;
12739 /* I don't think this will do the right thing with respect to types.
12740 But the only case I've seen it in so far has been array bounds, where
12741 signedness is the only information lost, and I think that will be
12743 while (TREE_CODE (parm
) == NOP_EXPR
)
12744 parm
= TREE_OPERAND (parm
, 0);
12746 if (arg
== error_mark_node
)
12748 if (arg
== unknown_type_node
)
12749 /* We can't deduce anything from this, but we might get all the
12750 template args from other function args. */
12753 /* If PARM uses template parameters, then we can't bail out here,
12754 even if ARG == PARM, since we won't record unifications for the
12755 template parameters. We might need them if we're trying to
12756 figure out which of two things is more specialized. */
12757 if (arg
== parm
&& !uses_template_parms (parm
))
12760 /* Immediately reject some pairs that won't unify because of
12761 cv-qualification mismatches. */
12762 if (TREE_CODE (arg
) == TREE_CODE (parm
)
12764 /* It is the elements of the array which hold the cv quals of an array
12765 type, and the elements might be template type parms. We'll check
12766 when we recurse. */
12767 && TREE_CODE (arg
) != ARRAY_TYPE
12768 /* We check the cv-qualifiers when unifying with template type
12769 parameters below. We want to allow ARG `const T' to unify with
12770 PARM `T' for example, when computing which of two templates
12771 is more specialized, for example. */
12772 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
12773 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
12776 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
12777 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
12778 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
12779 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
12780 strict
&= ~UNIFY_ALLOW_DERIVED
;
12781 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
12782 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
12784 switch (TREE_CODE (parm
))
12786 case TYPENAME_TYPE
:
12788 case UNBOUND_CLASS_TEMPLATE
:
12789 /* In a type which contains a nested-name-specifier, template
12790 argument values cannot be deduced for template parameters used
12791 within the nested-name-specifier. */
12794 case TEMPLATE_TYPE_PARM
:
12795 case TEMPLATE_TEMPLATE_PARM
:
12796 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12797 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
12798 if (tparm
== error_mark_node
)
12801 if (TEMPLATE_TYPE_LEVEL (parm
)
12802 != template_decl_level (tparm
))
12803 /* The PARM is not one we're trying to unify. Just check
12804 to see if it matches ARG. */
12805 return (TREE_CODE (arg
) == TREE_CODE (parm
)
12806 && same_type_p (parm
, arg
)) ? 0 : 1;
12807 idx
= TEMPLATE_TYPE_IDX (parm
);
12808 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
12809 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
12811 /* Check for mixed types and values. */
12812 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12813 && TREE_CODE (tparm
) != TYPE_DECL
)
12814 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12815 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
12818 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12820 /* ARG must be constructed from a template class or a template
12821 template parameter. */
12822 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
12823 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
12827 tree parmvec
= TYPE_TI_ARGS (parm
);
12828 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
12830 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
12832 int parm_variadic_p
= 0;
12834 /* The resolution to DR150 makes clear that default
12835 arguments for an N-argument may not be used to bind T
12836 to a template template parameter with fewer than N
12837 parameters. It is not safe to permit the binding of
12838 default arguments as an extension, as that may change
12839 the meaning of a conforming program. Consider:
12841 struct Dense { static const unsigned int dim = 1; };
12843 template <template <typename> class View,
12845 void operator+(float, View<Block> const&);
12847 template <typename Block,
12848 unsigned int Dim = Block::dim>
12849 struct Lvalue_proxy { operator float() const; };
12853 Lvalue_proxy<Dense> p;
12858 Here, if Lvalue_proxy is permitted to bind to View, then
12859 the global operator+ will be used; if they are not, the
12860 Lvalue_proxy will be converted to float. */
12861 if (coerce_template_parms (argtmplvec
, parmvec
,
12862 TYPE_TI_TEMPLATE (parm
),
12864 /*require_all_args=*/true,
12865 /*use_default_args=*/false)
12866 == error_mark_node
)
12869 /* Deduce arguments T, i from TT<T> or TT<i>.
12870 We check each element of PARMVEC and ARGVEC individually
12871 rather than the whole TREE_VEC since they can have
12872 different number of elements. */
12874 parmvec
= expand_template_argument_pack (parmvec
);
12875 argvec
= expand_template_argument_pack (argvec
);
12877 len
= TREE_VEC_LENGTH (parmvec
);
12879 /* Check if the parameters end in a pack, making them
12882 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
12883 parm_variadic_p
= 1;
12885 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
12888 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
12890 if (unify (tparms
, targs
,
12891 TREE_VEC_ELT (parmvec
, i
),
12892 TREE_VEC_ELT (argvec
, i
),
12897 if (parm_variadic_p
12898 && unify_pack_expansion (tparms
, targs
,
12901 /*call_args_p=*/false,
12905 arg
= TYPE_TI_TEMPLATE (arg
);
12907 /* Fall through to deduce template name. */
12910 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12911 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12913 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12915 /* Simple cases: Value already set, does match or doesn't. */
12916 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
12923 /* If PARM is `const T' and ARG is only `int', we don't have
12924 a match unless we are allowing additional qualification.
12925 If ARG is `const int' and PARM is just `T' that's OK;
12926 that binds `const int' to `T'. */
12927 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
12931 /* Consider the case where ARG is `const volatile int' and
12932 PARM is `const T'. Then, T should be `volatile int'. */
12933 arg
= cp_build_qualified_type_real
12934 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
12935 if (arg
== error_mark_node
)
12938 /* Simple cases: Value already set, does match or doesn't. */
12939 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
12944 /* Make sure that ARG is not a variable-sized array. (Note
12945 that were talking about variable-sized arrays (like
12946 `int[n]'), rather than arrays of unknown size (like
12947 `int[]').) We'll get very confused by such a type since
12948 the bound of the array will not be computable in an
12949 instantiation. Besides, such types are not allowed in
12950 ISO C++, so we can do as we please here. */
12951 if (variably_modified_type_p (arg
, NULL_TREE
))
12955 /* If ARG is a parameter pack or an expansion, we cannot unify
12956 against it unless PARM is also a parameter pack. */
12957 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
12958 && !template_parameter_pack_p (parm
))
12961 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
12964 case TEMPLATE_PARM_INDEX
:
12965 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
12966 if (tparm
== error_mark_node
)
12969 if (TEMPLATE_PARM_LEVEL (parm
)
12970 != template_decl_level (tparm
))
12971 /* The PARM is not one we're trying to unify. Just check
12972 to see if it matches ARG. */
12973 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
12974 && cp_tree_equal (parm
, arg
));
12976 idx
= TEMPLATE_PARM_IDX (parm
);
12977 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
12980 return !cp_tree_equal (targ
, arg
);
12982 /* [temp.deduct.type] If, in the declaration of a function template
12983 with a non-type template-parameter, the non-type
12984 template-parameter is used in an expression in the function
12985 parameter-list and, if the corresponding template-argument is
12986 deduced, the template-argument type shall match the type of the
12987 template-parameter exactly, except that a template-argument
12988 deduced from an array bound may be of any integral type.
12989 The non-type parameter might use already deduced type parameters. */
12990 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
12991 if (!TREE_TYPE (arg
))
12992 /* Template-parameter dependent expression. Just accept it for now.
12993 It will later be processed in convert_template_argument. */
12995 else if (same_type_p (TREE_TYPE (arg
), tparm
))
12997 else if ((strict
& UNIFY_ALLOW_INTEGER
)
12998 && (TREE_CODE (tparm
) == INTEGER_TYPE
12999 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
13000 /* Convert the ARG to the type of PARM; the deduced non-type
13001 template argument must exactly match the types of the
13002 corresponding parameter. */
13003 arg
= fold (build_nop (TREE_TYPE (parm
), arg
));
13004 else if (uses_template_parms (tparm
))
13005 /* We haven't deduced the type of this parameter yet. Try again
13011 /* If ARG is a parameter pack or an expansion, we cannot unify
13012 against it unless PARM is also a parameter pack. */
13013 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
13014 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
13017 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
13022 /* A pointer-to-member constant can be unified only with
13023 another constant. */
13024 if (TREE_CODE (arg
) != PTRMEM_CST
)
13027 /* Just unify the class member. It would be useless (and possibly
13028 wrong, depending on the strict flags) to unify also
13029 PTRMEM_CST_CLASS, because we want to be sure that both parm and
13030 arg refer to the same variable, even if through different
13031 classes. For instance:
13033 struct A { int x; };
13036 Unification of &A::x and &B::x must succeed. */
13037 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
13038 PTRMEM_CST_MEMBER (arg
), strict
);
13043 if (TREE_CODE (arg
) != POINTER_TYPE
)
13046 /* [temp.deduct.call]
13048 A can be another pointer or pointer to member type that can
13049 be converted to the deduced A via a qualification
13050 conversion (_conv.qual_).
13052 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
13053 This will allow for additional cv-qualification of the
13054 pointed-to types if appropriate. */
13056 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
13057 /* The derived-to-base conversion only persists through one
13058 level of pointers. */
13059 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
13061 return unify (tparms
, targs
, TREE_TYPE (parm
),
13062 TREE_TYPE (arg
), strict
);
13065 case REFERENCE_TYPE
:
13066 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
13068 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13069 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
13072 if (TREE_CODE (arg
) != ARRAY_TYPE
)
13074 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
13075 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
13077 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
13084 /* Our representation of array types uses "N - 1" as the
13085 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
13086 not an integer constant. We cannot unify arbitrarily
13087 complex expressions, so we eliminate the MINUS_EXPRs
13089 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
13090 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
13093 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
13094 parm_max
= TREE_OPERAND (parm_max
, 0);
13096 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
13097 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
13100 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
13101 trying to unify the type of a variable with the type
13102 of a template parameter. For example:
13104 template <unsigned int N>
13105 void f (char (&) [N]);
13112 Here, the type of the ARG will be "int [g(i)]", and
13113 may be a SAVE_EXPR, etc. */
13114 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
13116 arg_max
= TREE_OPERAND (arg_max
, 0);
13119 /* If only one of the bounds used a MINUS_EXPR, compensate
13120 by adding one to the other bound. */
13121 if (parm_cst
&& !arg_cst
)
13122 parm_max
= fold_build2 (PLUS_EXPR
,
13126 else if (arg_cst
&& !parm_cst
)
13127 arg_max
= fold_build2 (PLUS_EXPR
,
13132 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
13135 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13136 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
13143 case ENUMERAL_TYPE
:
13145 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13148 /* We have already checked cv-qualification at the top of the
13150 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
13153 /* As far as unification is concerned, this wins. Later checks
13154 will invalidate it if necessary. */
13157 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
13158 /* Type INTEGER_CST can come from ordinary constant template args. */
13160 while (TREE_CODE (arg
) == NOP_EXPR
)
13161 arg
= TREE_OPERAND (arg
, 0);
13163 if (TREE_CODE (arg
) != INTEGER_CST
)
13165 return !tree_int_cst_equal (parm
, arg
);
13170 if (TREE_CODE (arg
) != TREE_VEC
)
13172 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
13174 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
13175 if (unify (tparms
, targs
,
13176 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
13184 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13187 if (TYPE_PTRMEMFUNC_P (parm
))
13189 if (!TYPE_PTRMEMFUNC_P (arg
))
13192 return unify (tparms
, targs
,
13193 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
13194 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
13198 if (CLASSTYPE_TEMPLATE_INFO (parm
))
13200 tree t
= NULL_TREE
;
13202 if (strict_in
& UNIFY_ALLOW_DERIVED
)
13204 /* First, we try to unify the PARM and ARG directly. */
13205 t
= try_class_unification (tparms
, targs
,
13210 /* Fallback to the special case allowed in
13211 [temp.deduct.call]:
13213 If P is a class, and P has the form
13214 template-id, then A can be a derived class of
13215 the deduced A. Likewise, if P is a pointer to
13216 a class of the form template-id, A can be a
13217 pointer to a derived class pointed to by the
13219 t
= get_template_base (tparms
, targs
, parm
, arg
);
13225 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
13226 && (CLASSTYPE_TI_TEMPLATE (parm
)
13227 == CLASSTYPE_TI_TEMPLATE (arg
)))
13228 /* Perhaps PARM is something like S<U> and ARG is S<int>.
13229 Then, we should unify `int' and `U'. */
13232 /* There's no chance of unification succeeding. */
13235 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
13236 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
13238 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
13243 case FUNCTION_TYPE
:
13244 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13247 /* CV qualifications for methods can never be deduced, they must
13248 match exactly. We need to check them explicitly here,
13249 because type_unification_real treats them as any other
13250 cvqualified parameter. */
13251 if (TREE_CODE (parm
) == METHOD_TYPE
13252 && (!check_cv_quals_for_unify
13254 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
13255 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
13258 if (unify (tparms
, targs
, TREE_TYPE (parm
),
13259 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
13261 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
13262 TYPE_ARG_TYPES (arg
), 1, DEDUCE_EXACT
,
13266 /* Unify a pointer to member with a pointer to member function, which
13267 deduces the type of the member as a function type. */
13268 if (TYPE_PTRMEMFUNC_P (arg
))
13272 cp_cv_quals cv_quals
;
13274 /* Check top-level cv qualifiers */
13275 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
13278 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
13279 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
13282 /* Determine the type of the function we are unifying against. */
13283 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
13285 build_function_type (TREE_TYPE (method_type
),
13286 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
13288 /* Extract the cv-qualifiers of the member function from the
13289 implicit object parameter and place them on the function
13290 type to be restored later. */
13292 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type
))));
13293 fntype
= build_qualified_type (fntype
, cv_quals
);
13294 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
13297 if (TREE_CODE (arg
) != OFFSET_TYPE
)
13299 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
13300 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
13302 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13306 if (DECL_TEMPLATE_PARM_P (parm
))
13307 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
13308 if (arg
!= integral_constant_value (parm
))
13313 case TEMPLATE_DECL
:
13314 /* Matched cases are handled by the ARG == PARM test above. */
13317 case TYPE_ARGUMENT_PACK
:
13318 case NONTYPE_ARGUMENT_PACK
:
13320 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
13321 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
13322 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
13323 int argslen
= TREE_VEC_LENGTH (packed_args
);
13324 int parm_variadic_p
= 0;
13326 for (i
= 0; i
< len
; ++i
)
13328 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
13331 /* We can unify against something with a trailing
13333 parm_variadic_p
= 1;
13335 /* Since there is something following the pack
13336 expansion, we cannot unify this template argument
13343 /* If we don't have enough arguments to satisfy the parameters
13344 (not counting the pack expression at the end), or we have
13345 too many arguments for a parameter list that doesn't end in
13346 a pack expression, we can't unify. */
13347 if (argslen
< (len
- parm_variadic_p
)
13348 || (argslen
> len
&& !parm_variadic_p
))
13351 /* Unify all of the parameters that precede the (optional)
13352 pack expression. */
13353 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
13355 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
13356 TREE_VEC_ELT (packed_args
, i
), strict
))
13360 if (parm_variadic_p
)
13361 return unify_pack_expansion (tparms
, targs
,
13362 packed_parms
, packed_args
,
13363 strict
, /*call_args_p=*/false,
13371 case DECLTYPE_TYPE
:
13372 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
13377 /* Unification fails if we hit an error node. */
13381 gcc_assert (EXPR_P (parm
));
13383 /* We must be looking at an expression. This can happen with
13387 void foo(S<I>, S<I + 2>);
13389 This is a "nondeduced context":
13393 The nondeduced contexts are:
13395 --A type that is a template-id in which one or more of
13396 the template-arguments is an expression that references
13397 a template-parameter.
13399 In these cases, we assume deduction succeeded, but don't
13400 actually infer any unifications. */
13402 if (!uses_template_parms (parm
)
13403 && !template_args_equal (parm
, arg
))
13410 /* Note that DECL can be defined in this translation unit, if
13414 mark_definable (tree decl
)
13417 DECL_NOT_REALLY_EXTERN (decl
) = 1;
13418 FOR_EACH_CLONE (clone
, decl
)
13419 DECL_NOT_REALLY_EXTERN (clone
) = 1;
13422 /* Called if RESULT is explicitly instantiated, or is a member of an
13423 explicitly instantiated class. */
13426 mark_decl_instantiated (tree result
, int extern_p
)
13428 SET_DECL_EXPLICIT_INSTANTIATION (result
);
13430 /* If this entity has already been written out, it's too late to
13431 make any modifications. */
13432 if (TREE_ASM_WRITTEN (result
))
13435 if (TREE_CODE (result
) != FUNCTION_DECL
)
13436 /* The TREE_PUBLIC flag for function declarations will have been
13437 set correctly by tsubst. */
13438 TREE_PUBLIC (result
) = 1;
13440 /* This might have been set by an earlier implicit instantiation. */
13441 DECL_COMDAT (result
) = 0;
13444 DECL_NOT_REALLY_EXTERN (result
) = 0;
13447 mark_definable (result
);
13448 /* Always make artificials weak. */
13449 if (DECL_ARTIFICIAL (result
) && flag_weak
)
13450 comdat_linkage (result
);
13451 /* For WIN32 we also want to put explicit instantiations in
13452 linkonce sections. */
13453 else if (TREE_PUBLIC (result
))
13454 maybe_make_one_only (result
);
13457 /* If EXTERN_P, then this function will not be emitted -- unless
13458 followed by an explicit instantiation, at which point its linkage
13459 will be adjusted. If !EXTERN_P, then this function will be
13460 emitted here. In neither circumstance do we want
13461 import_export_decl to adjust the linkage. */
13462 DECL_INTERFACE_KNOWN (result
) = 1;
13465 /* Given two function templates PAT1 and PAT2, return:
13467 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
13468 -1 if PAT2 is more specialized than PAT1.
13469 0 if neither is more specialized.
13471 LEN indicates the number of parameters we should consider
13472 (defaulted parameters should not be considered).
13474 The 1998 std underspecified function template partial ordering, and
13475 DR214 addresses the issue. We take pairs of arguments, one from
13476 each of the templates, and deduce them against each other. One of
13477 the templates will be more specialized if all the *other*
13478 template's arguments deduce against its arguments and at least one
13479 of its arguments *does* *not* deduce against the other template's
13480 corresponding argument. Deduction is done as for class templates.
13481 The arguments used in deduction have reference and top level cv
13482 qualifiers removed. Iff both arguments were originally reference
13483 types *and* deduction succeeds in both directions, the template
13484 with the more cv-qualified argument wins for that pairing (if
13485 neither is more cv-qualified, they both are equal). Unlike regular
13486 deduction, after all the arguments have been deduced in this way,
13487 we do *not* verify the deduced template argument values can be
13488 substituted into non-deduced contexts, nor do we have to verify
13489 that all template arguments have been deduced. */
13492 more_specialized_fn (tree pat1
, tree pat2
, int len
)
13494 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
13495 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
13496 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
13497 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
13498 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
13499 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
13500 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
13501 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
13505 /* Remove the this parameter from non-static member functions. If
13506 one is a non-static member function and the other is not a static
13507 member function, remove the first parameter from that function
13508 also. This situation occurs for operator functions where we
13509 locate both a member function (with this pointer) and non-member
13510 operator (with explicit first operand). */
13511 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
13513 len
--; /* LEN is the number of significant arguments for DECL1 */
13514 args1
= TREE_CHAIN (args1
);
13515 if (!DECL_STATIC_FUNCTION_P (decl2
))
13516 args2
= TREE_CHAIN (args2
);
13518 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
13520 args2
= TREE_CHAIN (args2
);
13521 if (!DECL_STATIC_FUNCTION_P (decl1
))
13524 args1
= TREE_CHAIN (args1
);
13528 /* If only one is a conversion operator, they are unordered. */
13529 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
13532 /* Consider the return type for a conversion function */
13533 if (DECL_CONV_FN_P (decl1
))
13535 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
13536 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
13540 processing_template_decl
++;
13544 tree arg1
= TREE_VALUE (args1
);
13545 tree arg2
= TREE_VALUE (args2
);
13546 int deduce1
, deduce2
;
13550 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
13551 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13553 /* When both arguments are pack expansions, we need only
13554 unify the patterns themselves. */
13555 arg1
= PACK_EXPANSION_PATTERN (arg1
);
13556 arg2
= PACK_EXPANSION_PATTERN (arg2
);
13558 /* This is the last comparison we need to do. */
13562 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
13564 arg1
= TREE_TYPE (arg1
);
13565 quals1
= cp_type_quals (arg1
);
13568 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
13570 arg2
= TREE_TYPE (arg2
);
13571 quals2
= cp_type_quals (arg2
);
13574 if ((quals1
< 0) != (quals2
< 0))
13576 /* Only of the args is a reference, see if we should apply
13577 array/function pointer decay to it. This is not part of
13578 DR214, but is, IMHO, consistent with the deduction rules
13579 for the function call itself, and with our earlier
13580 implementation of the underspecified partial ordering
13581 rules. (nathan). */
13584 switch (TREE_CODE (arg1
))
13587 arg1
= TREE_TYPE (arg1
);
13589 case FUNCTION_TYPE
:
13590 arg1
= build_pointer_type (arg1
);
13599 switch (TREE_CODE (arg2
))
13602 arg2
= TREE_TYPE (arg2
);
13604 case FUNCTION_TYPE
:
13605 arg2
= build_pointer_type (arg2
);
13614 arg1
= TYPE_MAIN_VARIANT (arg1
);
13615 arg2
= TYPE_MAIN_VARIANT (arg2
);
13617 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
13619 int i
, len2
= list_length (args2
);
13620 tree parmvec
= make_tree_vec (1);
13621 tree argvec
= make_tree_vec (len2
);
13624 /* Setup the parameter vector, which contains only ARG1. */
13625 TREE_VEC_ELT (parmvec
, 0) = arg1
;
13627 /* Setup the argument vector, which contains the remaining
13629 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
13630 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
13632 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
13633 argvec
, UNIFY_ALLOW_NONE
,
13634 /*call_args_p=*/false,
13637 /* We cannot deduce in the other direction, because ARG1 is
13638 a pack expansion but ARG2 is not. */
13641 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13643 int i
, len1
= list_length (args1
);
13644 tree parmvec
= make_tree_vec (1);
13645 tree argvec
= make_tree_vec (len1
);
13648 /* Setup the parameter vector, which contains only ARG1. */
13649 TREE_VEC_ELT (parmvec
, 0) = arg2
;
13651 /* Setup the argument vector, which contains the remaining
13653 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
13654 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
13656 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
13657 argvec
, UNIFY_ALLOW_NONE
,
13658 /*call_args_p=*/false,
13661 /* We cannot deduce in the other direction, because ARG2 is
13662 a pack expansion but ARG1 is not.*/
13668 /* The normal case, where neither argument is a pack
13670 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
13671 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
13678 if (better1
< 0 && better2
< 0)
13679 /* We've failed to deduce something in either direction.
13680 These must be unordered. */
13683 if (deduce1
&& deduce2
&& quals1
>= 0 && quals2
>= 0)
13685 /* Deduces in both directions, see if quals can
13686 disambiguate. Pretend the worse one failed to deduce. */
13687 if ((quals1
& quals2
) == quals2
)
13689 if ((quals1
& quals2
) == quals1
)
13692 if (deduce1
&& !deduce2
&& !better2
)
13694 if (deduce2
&& !deduce1
&& !better1
)
13697 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
13698 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13699 /* We have already processed all of the arguments in our
13700 handing of the pack expansion type. */
13703 args1
= TREE_CHAIN (args1
);
13704 args2
= TREE_CHAIN (args2
);
13706 /* Stop when an ellipsis is seen. */
13707 if (args1
== NULL_TREE
|| args2
== NULL_TREE
)
13711 processing_template_decl
--;
13713 /* All things being equal, if the next argument is a pack expansion
13714 for one function but not for the other, prefer the
13715 non-variadic function. */
13716 if ((better1
> 0) - (better2
> 0) == 0
13717 && args1
&& TREE_VALUE (args1
)
13718 && args2
&& TREE_VALUE (args2
))
13720 if (TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
)
13721 return TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
? 0 : -1;
13722 else if (TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
)
13726 return (better1
> 0) - (better2
> 0);
13729 /* Determine which of two partial specializations is more specialized.
13731 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13732 to the first partial specialization. The TREE_VALUE is the
13733 innermost set of template parameters for the partial
13734 specialization. PAT2 is similar, but for the second template.
13736 Return 1 if the first partial specialization is more specialized;
13737 -1 if the second is more specialized; 0 if neither is more
13740 See [temp.class.order] for information about determining which of
13741 two templates is more specialized. */
13744 more_specialized_class (tree pat1
, tree pat2
)
13749 bool any_deductions
= false;
13751 tmpl1
= TREE_TYPE (pat1
);
13752 tmpl2
= TREE_TYPE (pat2
);
13754 /* Just like what happens for functions, if we are ordering between
13755 different class template specializations, we may encounter dependent
13756 types in the arguments, and we need our dependency check functions
13757 to behave correctly. */
13758 ++processing_template_decl
;
13759 targs
= get_class_bindings (TREE_VALUE (pat1
),
13760 CLASSTYPE_TI_ARGS (tmpl1
),
13761 CLASSTYPE_TI_ARGS (tmpl2
));
13765 any_deductions
= true;
13768 targs
= get_class_bindings (TREE_VALUE (pat2
),
13769 CLASSTYPE_TI_ARGS (tmpl2
),
13770 CLASSTYPE_TI_ARGS (tmpl1
));
13774 any_deductions
= true;
13776 --processing_template_decl
;
13778 /* In the case of a tie where at least one of the class templates
13779 has a parameter pack at the end, the template with the most
13780 non-packed parameters wins. */
13783 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
13784 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
13786 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
13787 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
13788 int len1
= TREE_VEC_LENGTH (args1
);
13789 int len2
= TREE_VEC_LENGTH (args2
);
13791 /* We don't count the pack expansion at the end. */
13792 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
13794 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
13799 else if (len1
< len2
)
13806 /* Return the template arguments that will produce the function signature
13807 DECL from the function template FN, with the explicit template
13808 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13809 also match. Return NULL_TREE if no satisfactory arguments could be
13813 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
13815 int ntparms
= DECL_NTPARMS (fn
);
13816 tree targs
= make_tree_vec (ntparms
);
13818 tree decl_arg_types
;
13820 /* Substitute the explicit template arguments into the type of DECL.
13821 The call to fn_type_unification will handle substitution into the
13823 decl_type
= TREE_TYPE (decl
);
13824 if (explicit_args
&& uses_template_parms (decl_type
))
13827 tree converted_args
;
13829 if (DECL_TEMPLATE_INFO (decl
))
13830 tmpl
= DECL_TI_TEMPLATE (decl
);
13832 /* We can get here for some invalid specializations. */
13836 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
13837 explicit_args
, NULL_TREE
,
13839 /*require_all_args=*/false,
13840 /*use_default_args=*/false);
13841 if (converted_args
== error_mark_node
)
13844 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
13845 if (decl_type
== error_mark_node
)
13849 /* Never do unification on the 'this' parameter. */
13850 decl_arg_types
= skip_artificial_parms_for (decl
,
13851 TYPE_ARG_TYPES (decl_type
));
13853 if (fn_type_unification (fn
, explicit_args
, targs
,
13855 (check_rettype
|| DECL_CONV_FN_P (fn
)
13856 ? TREE_TYPE (decl_type
) : NULL_TREE
),
13857 DEDUCE_EXACT
, LOOKUP_NORMAL
))
13863 /* Return the innermost template arguments that, when applied to a
13864 template specialization whose innermost template parameters are
13865 TPARMS, and whose specialization arguments are PARMS, yield the
13868 For example, suppose we have:
13870 template <class T, class U> struct S {};
13871 template <class T> struct S<T*, int> {};
13873 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13874 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13875 int}. The resulting vector will be {double}, indicating that `T'
13876 is bound to `double'. */
13879 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
13881 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
13883 tree innermost_deduced_args
;
13885 innermost_deduced_args
= make_tree_vec (ntparms
);
13886 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13888 deduced_args
= copy_node (args
);
13889 SET_TMPL_ARGS_LEVEL (deduced_args
,
13890 TMPL_ARGS_DEPTH (deduced_args
),
13891 innermost_deduced_args
);
13894 deduced_args
= innermost_deduced_args
;
13896 if (unify (tparms
, deduced_args
,
13897 INNERMOST_TEMPLATE_ARGS (spec_args
),
13898 INNERMOST_TEMPLATE_ARGS (args
),
13902 for (i
= 0; i
< ntparms
; ++i
)
13903 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
13906 /* Verify that nondeduced template arguments agree with the type
13907 obtained from argument deduction.
13911 struct A { typedef int X; };
13912 template <class T, class U> struct C {};
13913 template <class T> struct C<T, typename T::X> {};
13915 Then with the instantiation `C<A, int>', we can deduce that
13916 `T' is `A' but unify () does not check whether `typename T::X'
13918 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
13919 if (spec_args
== error_mark_node
13920 /* We only need to check the innermost arguments; the other
13921 arguments will always agree. */
13922 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
13923 INNERMOST_TEMPLATE_ARGS (args
)))
13926 /* Now that we have bindings for all of the template arguments,
13927 ensure that the arguments deduced for the template template
13928 parameters have compatible template parameter lists. See the use
13929 of template_template_parm_bindings_ok_p in fn_type_unification
13930 for more information. */
13931 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
13934 return deduced_args
;
13937 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13938 Return the TREE_LIST node with the most specialized template, if
13939 any. If there is no most specialized template, the error_mark_node
13942 Note that this function does not look at, or modify, the
13943 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13944 returned is one of the elements of INSTANTIATIONS, callers may
13945 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13946 and retrieve it from the value returned. */
13949 most_specialized_instantiation (tree templates
)
13953 ++processing_template_decl
;
13956 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
13960 if (get_bindings (TREE_VALUE (champ
),
13961 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
13962 NULL_TREE
, /*check_ret=*/false))
13965 if (get_bindings (TREE_VALUE (fn
),
13966 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
13967 NULL_TREE
, /*check_ret=*/false))
13974 /* Equally specialized, move to next function. If there
13975 is no next function, nothing's most specialized. */
13976 fn
= TREE_CHAIN (fn
);
13984 /* Now verify that champ is better than everything earlier in the
13985 instantiation list. */
13986 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
13987 if (get_bindings (TREE_VALUE (champ
),
13988 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
13989 NULL_TREE
, /*check_ret=*/false)
13990 || !get_bindings (TREE_VALUE (fn
),
13991 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
13992 NULL_TREE
, /*check_ret=*/false))
13998 processing_template_decl
--;
14001 return error_mark_node
;
14006 /* If DECL is a specialization of some template, return the most
14007 general such template. Otherwise, returns NULL_TREE.
14009 For example, given:
14011 template <class T> struct S { template <class U> void f(U); };
14013 if TMPL is `template <class U> void S<int>::f(U)' this will return
14014 the full template. This function will not trace past partial
14015 specializations, however. For example, given in addition:
14017 template <class T> struct S<T*> { template <class U> void f(U); };
14019 if TMPL is `template <class U> void S<int*>::f(U)' this will return
14020 `template <class T> template <class U> S<T*>::f(U)'. */
14023 most_general_template (tree decl
)
14025 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
14026 an immediate specialization. */
14027 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14029 if (DECL_TEMPLATE_INFO (decl
)) {
14030 decl
= DECL_TI_TEMPLATE (decl
);
14032 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
14033 template friend. */
14034 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
14040 /* Look for more and more general templates. */
14041 while (DECL_TEMPLATE_INFO (decl
))
14043 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
14044 (See cp-tree.h for details.) */
14045 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
14048 if (CLASS_TYPE_P (TREE_TYPE (decl
))
14049 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
14052 /* Stop if we run into an explicitly specialized class template. */
14053 if (!DECL_NAMESPACE_SCOPE_P (decl
)
14054 && DECL_CONTEXT (decl
)
14055 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
14058 decl
= DECL_TI_TEMPLATE (decl
);
14064 /* Return the most specialized of the class template partial
14065 specializations of TMPL which can produce TYPE, a specialization of
14066 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
14067 a _TYPE node corresponding to the partial specialization, while the
14068 TREE_PURPOSE is the set of template arguments that must be
14069 substituted into the TREE_TYPE in order to generate TYPE.
14071 If the choice of partial specialization is ambiguous, a diagnostic
14072 is issued, and the error_mark_node is returned. If there are no
14073 partial specializations of TMPL matching TYPE, then NULL_TREE is
14077 most_specialized_class (tree type
, tree tmpl
)
14079 tree list
= NULL_TREE
;
14085 tree outer_args
= NULL_TREE
;
14087 tmpl
= most_general_template (tmpl
);
14088 args
= CLASSTYPE_TI_ARGS (type
);
14090 /* For determining which partial specialization to use, only the
14091 innermost args are interesting. */
14092 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
14094 outer_args
= strip_innermost_template_args (args
, 1);
14095 args
= INNERMOST_TEMPLATE_ARGS (args
);
14098 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
14100 tree partial_spec_args
;
14102 tree parms
= TREE_VALUE (t
);
14104 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
14109 ++processing_template_decl
;
14111 /* Discard the outer levels of args, and then substitute in the
14112 template args from the enclosing class. */
14113 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
14114 partial_spec_args
= tsubst_template_args
14115 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
14117 /* PARMS already refers to just the innermost parms, but the
14118 template parms in partial_spec_args had their levels lowered
14119 by tsubst, so we need to do the same for the parm list. We
14120 can't just tsubst the TREE_VEC itself, as tsubst wants to
14121 treat a TREE_VEC as an argument vector. */
14122 parms
= copy_node (parms
);
14123 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
14124 TREE_VEC_ELT (parms
, i
) =
14125 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
14127 --processing_template_decl
;
14129 spec_args
= get_class_bindings (parms
,
14135 spec_args
= add_to_template_args (outer_args
, spec_args
);
14136 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
14137 TREE_TYPE (list
) = TREE_TYPE (t
);
14144 ambiguous_p
= false;
14147 t
= TREE_CHAIN (t
);
14148 for (; t
; t
= TREE_CHAIN (t
))
14150 fate
= more_specialized_class (champ
, t
);
14157 t
= TREE_CHAIN (t
);
14160 ambiguous_p
= true;
14169 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
14171 fate
= more_specialized_class (champ
, t
);
14174 ambiguous_p
= true;
14181 const char *str
= "candidates are:";
14182 error ("ambiguous class template instantiation for %q#T", type
);
14183 for (t
= list
; t
; t
= TREE_CHAIN (t
))
14185 error ("%s %+#T", str
, TREE_TYPE (t
));
14188 return error_mark_node
;
14194 /* Explicitly instantiate DECL. */
14197 do_decl_instantiation (tree decl
, tree storage
)
14199 tree result
= NULL_TREE
;
14202 if (!decl
|| decl
== error_mark_node
)
14203 /* An error occurred, for which grokdeclarator has already issued
14204 an appropriate message. */
14206 else if (! DECL_LANG_SPECIFIC (decl
))
14208 error ("explicit instantiation of non-template %q#D", decl
);
14211 else if (TREE_CODE (decl
) == VAR_DECL
)
14213 /* There is an asymmetry here in the way VAR_DECLs and
14214 FUNCTION_DECLs are handled by grokdeclarator. In the case of
14215 the latter, the DECL we get back will be marked as a
14216 template instantiation, and the appropriate
14217 DECL_TEMPLATE_INFO will be set up. This does not happen for
14218 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
14219 should handle VAR_DECLs as it currently handles
14221 if (!DECL_CLASS_SCOPE_P (decl
))
14223 error ("%qD is not a static data member of a class template", decl
);
14226 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
14227 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
14229 error ("no matching template for %qD found", decl
);
14232 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
14234 error ("type %qT for explicit instantiation %qD does not match "
14235 "declared type %qT", TREE_TYPE (result
), decl
,
14240 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
14242 error ("explicit instantiation of %q#D", decl
);
14248 /* Check for various error cases. Note that if the explicit
14249 instantiation is valid the RESULT will currently be marked as an
14250 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
14251 until we get here. */
14253 if (DECL_TEMPLATE_SPECIALIZATION (result
))
14255 /* DR 259 [temp.spec].
14257 Both an explicit instantiation and a declaration of an explicit
14258 specialization shall not appear in a program unless the explicit
14259 instantiation follows a declaration of the explicit specialization.
14261 For a given set of template parameters, if an explicit
14262 instantiation of a template appears after a declaration of an
14263 explicit specialization for that template, the explicit
14264 instantiation has no effect. */
14267 else if (DECL_EXPLICIT_INSTANTIATION (result
))
14271 No program shall explicitly instantiate any template more
14274 We check DECL_NOT_REALLY_EXTERN so as not to complain when
14275 the first instantiation was `extern' and the second is not,
14276 and EXTERN_P for the opposite case. */
14277 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
14278 pedwarn ("duplicate explicit instantiation of %q#D", result
);
14279 /* If an "extern" explicit instantiation follows an ordinary
14280 explicit instantiation, the template is instantiated. */
14284 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
14286 error ("no matching template for %qD found", result
);
14289 else if (!DECL_TEMPLATE_INFO (result
))
14291 pedwarn ("explicit instantiation of non-template %q#D", result
);
14295 if (storage
== NULL_TREE
)
14297 else if (storage
== ridpointers
[(int) RID_EXTERN
])
14299 if (pedantic
&& !in_system_header
)
14300 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
14305 error ("storage class %qD applied to template instantiation", storage
);
14307 check_explicit_instantiation_namespace (result
);
14308 mark_decl_instantiated (result
, extern_p
);
14310 instantiate_decl (result
, /*defer_ok=*/1,
14311 /*expl_inst_class_mem_p=*/false);
14315 mark_class_instantiated (tree t
, int extern_p
)
14317 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
14318 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
14319 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
14320 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
14323 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
14324 rest_of_type_compilation (t
, 1);
14328 /* Called from do_type_instantiation through binding_table_foreach to
14329 do recursive instantiation for the type bound in ENTRY. */
14331 bt_instantiate_type_proc (binding_entry entry
, void *data
)
14333 tree storage
= *(tree
*) data
;
14335 if (IS_AGGR_TYPE (entry
->type
)
14336 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
14337 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
14340 /* Called from do_type_instantiation to instantiate a member
14341 (a member function or a static member variable) of an
14342 explicitly instantiated class template. */
14344 instantiate_class_member (tree decl
, int extern_p
)
14346 mark_decl_instantiated (decl
, extern_p
);
14348 instantiate_decl (decl
, /*defer_ok=*/1,
14349 /*expl_inst_class_mem_p=*/true);
14352 /* Perform an explicit instantiation of template class T. STORAGE, if
14353 non-null, is the RID for extern, inline or static. COMPLAIN is
14354 nonzero if this is called from the parser, zero if called recursively,
14355 since the standard is unclear (as detailed below). */
14358 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
14363 int previous_instantiation_extern_p
= 0;
14365 if (TREE_CODE (t
) == TYPE_DECL
)
14368 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
14370 error ("explicit instantiation of non-template type %qT", t
);
14376 if (!COMPLETE_TYPE_P (t
))
14378 if (complain
& tf_error
)
14379 error ("explicit instantiation of %q#T before definition of template",
14384 if (storage
!= NULL_TREE
)
14386 if (pedantic
&& !in_system_header
)
14387 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
14390 if (storage
== ridpointers
[(int) RID_INLINE
])
14392 else if (storage
== ridpointers
[(int) RID_EXTERN
])
14394 else if (storage
== ridpointers
[(int) RID_STATIC
])
14398 error ("storage class %qD applied to template instantiation",
14404 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
14406 /* DR 259 [temp.spec].
14408 Both an explicit instantiation and a declaration of an explicit
14409 specialization shall not appear in a program unless the explicit
14410 instantiation follows a declaration of the explicit specialization.
14412 For a given set of template parameters, if an explicit
14413 instantiation of a template appears after a declaration of an
14414 explicit specialization for that template, the explicit
14415 instantiation has no effect. */
14418 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
14422 No program shall explicitly instantiate any template more
14425 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
14426 instantiation was `extern'. If EXTERN_P then the second is.
14427 These cases are OK. */
14428 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
14430 if (!previous_instantiation_extern_p
&& !extern_p
14431 && (complain
& tf_error
))
14432 pedwarn ("duplicate explicit instantiation of %q#T", t
);
14434 /* If we've already instantiated the template, just return now. */
14435 if (!CLASSTYPE_INTERFACE_ONLY (t
))
14439 check_explicit_instantiation_namespace (TYPE_NAME (t
));
14440 mark_class_instantiated (t
, extern_p
);
14448 /* In contrast to implicit instantiation, where only the
14449 declarations, and not the definitions, of members are
14450 instantiated, we have here:
14454 The explicit instantiation of a class template specialization
14455 implies the instantiation of all of its members not
14456 previously explicitly specialized in the translation unit
14457 containing the explicit instantiation.
14459 Of course, we can't instantiate member template classes, since
14460 we don't have any arguments for them. Note that the standard
14461 is unclear on whether the instantiation of the members are
14462 *explicit* instantiations or not. However, the most natural
14463 interpretation is that it should be an explicit instantiation. */
14466 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
14467 if (TREE_CODE (tmp
) == FUNCTION_DECL
14468 && DECL_TEMPLATE_INSTANTIATION (tmp
))
14469 instantiate_class_member (tmp
, extern_p
);
14471 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
14472 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
14473 instantiate_class_member (tmp
, extern_p
);
14475 if (CLASSTYPE_NESTED_UTDS (t
))
14476 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
14477 bt_instantiate_type_proc
, &storage
);
14481 /* Given a function DECL, which is a specialization of TMPL, modify
14482 DECL to be a re-instantiation of TMPL with the same template
14483 arguments. TMPL should be the template into which tsubst'ing
14484 should occur for DECL, not the most general template.
14486 One reason for doing this is a scenario like this:
14489 void f(const T&, int i);
14491 void g() { f(3, 7); }
14494 void f(const T& t, const int i) { }
14496 Note that when the template is first instantiated, with
14497 instantiate_template, the resulting DECL will have no name for the
14498 first parameter, and the wrong type for the second. So, when we go
14499 to instantiate the DECL, we regenerate it. */
14502 regenerate_decl_from_template (tree decl
, tree tmpl
)
14504 /* The arguments used to instantiate DECL, from the most general
14509 args
= DECL_TI_ARGS (decl
);
14510 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14512 /* Make sure that we can see identifiers, and compute access
14514 push_access_scope (decl
);
14516 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14524 args_depth
= TMPL_ARGS_DEPTH (args
);
14525 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
14526 if (args_depth
> parms_depth
)
14527 args
= get_innermost_template_args (args
, parms_depth
);
14529 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
14530 args
, tf_error
, NULL_TREE
);
14532 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
14535 /* Merge parameter declarations. */
14536 decl_parm
= skip_artificial_parms_for (decl
,
14537 DECL_ARGUMENTS (decl
));
14539 = skip_artificial_parms_for (code_pattern
,
14540 DECL_ARGUMENTS (code_pattern
));
14541 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
14546 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
14547 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
14548 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
14550 parm_type
= type_decays_to (parm_type
);
14551 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
14552 TREE_TYPE (decl_parm
) = parm_type
;
14553 attributes
= DECL_ATTRIBUTES (pattern_parm
);
14554 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
14556 DECL_ATTRIBUTES (decl_parm
) = attributes
;
14557 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
14559 decl_parm
= TREE_CHAIN (decl_parm
);
14560 pattern_parm
= TREE_CHAIN (pattern_parm
);
14562 /* Merge any parameters that match with the function parameter
14564 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
14567 tree expanded_types
;
14568 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14569 the parameters in this function parameter pack. */
14570 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
14571 args
, tf_error
, NULL_TREE
);
14572 len
= TREE_VEC_LENGTH (expanded_types
);
14573 for (i
= 0; i
< len
; i
++)
14578 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
14579 /* Rename the parameter to include the index. */
14580 DECL_NAME (decl_parm
) =
14581 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
14582 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
14583 parm_type
= type_decays_to (parm_type
);
14584 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
14585 TREE_TYPE (decl_parm
) = parm_type
;
14586 attributes
= DECL_ATTRIBUTES (pattern_parm
);
14587 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
14589 DECL_ATTRIBUTES (decl_parm
) = attributes
;
14590 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
14592 decl_parm
= TREE_CHAIN (decl_parm
);
14595 /* Merge additional specifiers from the CODE_PATTERN. */
14596 if (DECL_DECLARED_INLINE_P (code_pattern
)
14597 && !DECL_DECLARED_INLINE_P (decl
))
14598 DECL_DECLARED_INLINE_P (decl
) = 1;
14599 if (DECL_INLINE (code_pattern
) && !DECL_INLINE (decl
))
14600 DECL_INLINE (decl
) = 1;
14602 else if (TREE_CODE (decl
) == VAR_DECL
)
14603 DECL_INITIAL (decl
) =
14604 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
14605 tf_error
, DECL_TI_TEMPLATE (decl
),
14606 /*integral_constant_expression_p=*/false);
14608 gcc_unreachable ();
14610 pop_access_scope (decl
);
14613 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
14614 substituted to get DECL. */
14617 template_for_substitution (tree decl
)
14619 tree tmpl
= DECL_TI_TEMPLATE (decl
);
14621 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
14622 for the instantiation. This is not always the most general
14623 template. Consider, for example:
14626 struct S { template <class U> void f();
14627 template <> void f<int>(); };
14629 and an instantiation of S<double>::f<int>. We want TD to be the
14630 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
14631 while (/* An instantiation cannot have a definition, so we need a
14632 more general template. */
14633 DECL_TEMPLATE_INSTANTIATION (tmpl
)
14634 /* We must also deal with friend templates. Given:
14636 template <class T> struct S {
14637 template <class U> friend void f() {};
14640 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
14641 so far as the language is concerned, but that's still
14642 where we get the pattern for the instantiation from. On
14643 other hand, if the definition comes outside the class, say:
14645 template <class T> struct S {
14646 template <class U> friend void f();
14648 template <class U> friend void f() {}
14650 we don't need to look any further. That's what the check for
14651 DECL_INITIAL is for. */
14652 || (TREE_CODE (decl
) == FUNCTION_DECL
14653 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
14654 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
14656 /* The present template, TD, should not be a definition. If it
14657 were a definition, we should be using it! Note that we
14658 cannot restructure the loop to just keep going until we find
14659 a template with a definition, since that might go too far if
14660 a specialization was declared, but not defined. */
14661 gcc_assert (TREE_CODE (decl
) != VAR_DECL
14662 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
14664 /* Fetch the more general template. */
14665 tmpl
= DECL_TI_TEMPLATE (tmpl
);
14671 /* Produce the definition of D, a _DECL generated from a template. If
14672 DEFER_OK is nonzero, then we don't have to actually do the
14673 instantiation now; we just have to do it sometime. Normally it is
14674 an error if this is an explicit instantiation but D is undefined.
14675 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
14676 explicitly instantiated class template. */
14679 instantiate_decl (tree d
, int defer_ok
,
14680 bool expl_inst_class_mem_p
)
14682 tree tmpl
= DECL_TI_TEMPLATE (d
);
14689 bool pattern_defined
;
14691 location_t saved_loc
= input_location
;
14692 int saved_in_system_header
= in_system_header
;
14695 /* This function should only be used to instantiate templates for
14696 functions and static member variables. */
14697 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
14698 || TREE_CODE (d
) == VAR_DECL
);
14700 /* Variables are never deferred; if instantiation is required, they
14701 are instantiated right away. That allows for better code in the
14702 case that an expression refers to the value of the variable --
14703 if the variable has a constant value the referring expression can
14704 take advantage of that fact. */
14705 if (TREE_CODE (d
) == VAR_DECL
)
14708 /* Don't instantiate cloned functions. Instead, instantiate the
14709 functions they cloned. */
14710 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
14711 d
= DECL_CLONED_FUNCTION (d
);
14713 if (DECL_TEMPLATE_INSTANTIATED (d
))
14714 /* D has already been instantiated. It might seem reasonable to
14715 check whether or not D is an explicit instantiation, and, if so,
14716 stop here. But when an explicit instantiation is deferred
14717 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
14718 is set, even though we still need to do the instantiation. */
14721 /* If we already have a specialization of this declaration, then
14722 there's no reason to instantiate it. Note that
14723 retrieve_specialization gives us both instantiations and
14724 specializations, so we must explicitly check
14725 DECL_TEMPLATE_SPECIALIZATION. */
14726 gen_tmpl
= most_general_template (tmpl
);
14727 gen_args
= DECL_TI_ARGS (d
);
14728 spec
= retrieve_specialization (gen_tmpl
, gen_args
,
14729 /*class_specializations_p=*/false);
14730 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
14733 /* This needs to happen before any tsubsting. */
14734 if (! push_tinst_level (d
))
14737 timevar_push (TV_PARSE
);
14739 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
14740 for the instantiation. */
14741 td
= template_for_substitution (d
);
14742 code_pattern
= DECL_TEMPLATE_RESULT (td
);
14744 /* We should never be trying to instantiate a member of a class
14745 template or partial specialization. */
14746 gcc_assert (d
!= code_pattern
);
14748 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
14749 || DECL_TEMPLATE_SPECIALIZATION (td
))
14750 /* In the case of a friend template whose definition is provided
14751 outside the class, we may have too many arguments. Drop the
14752 ones we don't need. The same is true for specializations. */
14753 args
= get_innermost_template_args
14754 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
14758 if (TREE_CODE (d
) == FUNCTION_DECL
)
14759 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
14761 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
14763 /* We may be in the middle of deferred access check. Disable it now. */
14764 push_deferring_access_checks (dk_no_deferred
);
14766 /* Unless an explicit instantiation directive has already determined
14767 the linkage of D, remember that a definition is available for
14769 if (pattern_defined
14770 && !DECL_INTERFACE_KNOWN (d
)
14771 && !DECL_NOT_REALLY_EXTERN (d
))
14772 mark_definable (d
);
14774 input_location
= DECL_SOURCE_LOCATION (d
);
14775 in_system_header
= DECL_IN_SYSTEM_HEADER (d
);
14777 /* If D is a member of an explicitly instantiated class template,
14778 and no definition is available, treat it like an implicit
14780 if (!pattern_defined
&& expl_inst_class_mem_p
14781 && DECL_EXPLICIT_INSTANTIATION (d
))
14783 DECL_NOT_REALLY_EXTERN (d
) = 0;
14784 DECL_INTERFACE_KNOWN (d
) = 0;
14785 SET_DECL_IMPLICIT_INSTANTIATION (d
);
14790 /* Recheck the substitutions to obtain any warning messages
14791 about ignoring cv qualifiers. */
14792 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
14793 tree type
= TREE_TYPE (gen
);
14795 /* Make sure that we can see identifiers, and compute access
14796 correctly. D is already the target FUNCTION_DECL with the
14798 push_access_scope (d
);
14800 if (TREE_CODE (gen
) == FUNCTION_DECL
)
14802 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
14803 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
14805 /* Don't simply tsubst the function type, as that will give
14806 duplicate warnings about poor parameter qualifications.
14807 The function arguments are the same as the decl_arguments
14808 without the top level cv qualifiers. */
14809 type
= TREE_TYPE (type
);
14811 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
14813 pop_access_scope (d
);
14816 /* Check to see whether we know that this template will be
14817 instantiated in some other file, as with "extern template"
14819 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
14820 /* In general, we do not instantiate such templates... */
14822 /* ... but we instantiate inline functions so that we can inline
14824 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
))
14825 /* ... we instantiate static data members whose values are
14826 needed in integral constant expressions. */
14827 && ! (TREE_CODE (d
) == VAR_DECL
14828 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d
)))
14830 /* Defer all other templates, unless we have been explicitly
14831 forbidden from doing so. */
14832 if (/* If there is no definition, we cannot instantiate the
14835 /* If it's OK to postpone instantiation, do so. */
14837 /* If this is a static data member that will be defined
14838 elsewhere, we don't want to instantiate the entire data
14839 member, but we do want to instantiate the initializer so that
14840 we can substitute that elsewhere. */
14841 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
14843 /* The definition of the static data member is now required so
14844 we must substitute the initializer. */
14845 if (TREE_CODE (d
) == VAR_DECL
14846 && !DECL_INITIAL (d
)
14847 && DECL_INITIAL (code_pattern
))
14852 ns
= decl_namespace_context (d
);
14853 push_nested_namespace (ns
);
14854 push_nested_class (DECL_CONTEXT (d
));
14855 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
14857 tf_warning_or_error
, NULL_TREE
,
14858 /*integral_constant_expression_p=*/false);
14859 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
14860 /*asmspec_tree=*/NULL_TREE
,
14861 LOOKUP_ONLYCONVERTING
);
14862 pop_nested_class ();
14863 pop_nested_namespace (ns
);
14866 /* We restore the source position here because it's used by
14867 add_pending_template. */
14868 input_location
= saved_loc
;
14870 if (at_eof
&& !pattern_defined
14871 && DECL_EXPLICIT_INSTANTIATION (d
))
14874 The definition of a non-exported function template, a
14875 non-exported member function template, or a non-exported
14876 member function or static data member of a class template
14877 shall be present in every translation unit in which it is
14878 explicitly instantiated. */
14880 ("explicit instantiation of %qD but no definition available", d
);
14882 /* ??? Historically, we have instantiated inline functions, even
14883 when marked as "extern template". */
14884 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
14885 add_pending_template (d
);
14888 /* Tell the repository that D is available in this translation unit
14889 -- and see if it is supposed to be instantiated here. */
14890 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
14892 /* In a PCH file, despite the fact that the repository hasn't
14893 requested instantiation in the PCH it is still possible that
14894 an instantiation will be required in a file that includes the
14897 add_pending_template (d
);
14898 /* Instantiate inline functions so that the inliner can do its
14899 job, even though we'll not be emitting a copy of this
14901 if (!(TREE_CODE (d
) == FUNCTION_DECL
14902 && flag_inline_trees
14903 && DECL_DECLARED_INLINE_P (d
)))
14907 need_push
= !cfun
|| !global_bindings_p ();
14909 push_to_top_level ();
14911 /* Mark D as instantiated so that recursive calls to
14912 instantiate_decl do not try to instantiate it again. */
14913 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
14915 /* Regenerate the declaration in case the template has been modified
14916 by a subsequent redeclaration. */
14917 regenerate_decl_from_template (d
, td
);
14919 /* We already set the file and line above. Reset them now in case
14920 they changed as a result of calling regenerate_decl_from_template. */
14921 input_location
= DECL_SOURCE_LOCATION (d
);
14923 if (TREE_CODE (d
) == VAR_DECL
)
14927 /* Clear out DECL_RTL; whatever was there before may not be right
14928 since we've reset the type of the declaration. */
14929 SET_DECL_RTL (d
, NULL_RTX
);
14930 DECL_IN_AGGR_P (d
) = 0;
14932 /* The initializer is placed in DECL_INITIAL by
14933 regenerate_decl_from_template. Pull it out so that
14934 finish_decl can process it. */
14935 init
= DECL_INITIAL (d
);
14936 DECL_INITIAL (d
) = NULL_TREE
;
14937 DECL_INITIALIZED_P (d
) = 0;
14939 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14940 initializer. That function will defer actual emission until
14941 we have a chance to determine linkage. */
14942 DECL_EXTERNAL (d
) = 0;
14944 /* Enter the scope of D so that access-checking works correctly. */
14945 push_nested_class (DECL_CONTEXT (d
));
14946 finish_decl (d
, init
, NULL_TREE
);
14947 pop_nested_class ();
14949 else if (TREE_CODE (d
) == FUNCTION_DECL
)
14951 htab_t saved_local_specializations
;
14956 /* Save away the current list, in case we are instantiating one
14957 template from within the body of another. */
14958 saved_local_specializations
= local_specializations
;
14960 /* Set up the list of local specializations. */
14961 local_specializations
= htab_create (37,
14962 hash_local_specialization
,
14963 eq_local_specializations
,
14966 /* Set up context. */
14967 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
14969 /* Create substitution entries for the parameters. */
14970 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
14971 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
14972 spec_parm
= DECL_ARGUMENTS (d
);
14973 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
14975 register_local_specialization (spec_parm
, tmpl_parm
);
14976 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
14977 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
14979 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
14981 register_local_specialization (spec_parm
, tmpl_parm
);
14982 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
14983 spec_parm
= TREE_CHAIN (spec_parm
);
14985 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
14987 /* Collect all of the extra "packed" parameters into an
14991 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
14992 tree argtypepack
= make_node (TYPE_ARGUMENT_PACK
);
14996 /* Count how many parameters remain. */
14997 for (t
= spec_parm
; t
; t
= TREE_CHAIN (t
))
15000 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
15001 parmvec
= make_tree_vec (len
);
15002 parmtypevec
= make_tree_vec (len
);
15003 for(i
= 0; i
< len
; i
++, spec_parm
= TREE_CHAIN (spec_parm
))
15005 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
15006 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
15009 /* Build the argument packs. */
15010 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
15011 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
15012 TREE_TYPE (argpack
) = argtypepack
;
15014 /* Register the (value) argument pack as a specialization of
15015 TMPL_PARM, then move on. */
15016 register_local_specialization (argpack
, tmpl_parm
);
15017 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
15019 gcc_assert (!spec_parm
);
15021 /* Substitute into the body of the function. */
15022 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
15023 tf_warning_or_error
, tmpl
,
15024 /*integral_constant_expression_p=*/false);
15026 /* Set the current input_location to the end of the function
15027 so that finish_function knows where we are. */
15028 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
15030 /* We don't need the local specializations any more. */
15031 htab_delete (local_specializations
);
15032 local_specializations
= saved_local_specializations
;
15034 /* Finish the function. */
15035 d
= finish_function (0);
15036 expand_or_defer_fn (d
);
15039 /* We're not deferring instantiation any more. */
15040 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
15043 pop_from_top_level ();
15046 input_location
= saved_loc
;
15047 in_system_header
= saved_in_system_header
;
15048 pop_deferring_access_checks ();
15049 pop_tinst_level ();
15051 timevar_pop (TV_PARSE
);
15056 /* Run through the list of templates that we wish we could
15057 instantiate, and instantiate any we can. RETRIES is the
15058 number of times we retry pending template instantiation. */
15061 instantiate_pending_templates (int retries
)
15064 location_t saved_loc
= input_location
;
15065 int saved_in_system_header
= in_system_header
;
15067 /* Instantiating templates may trigger vtable generation. This in turn
15068 may require further template instantiations. We place a limit here
15069 to avoid infinite loop. */
15070 if (pending_templates
&& retries
>= max_tinst_depth
)
15072 tree decl
= pending_templates
->tinst
->decl
;
15074 error ("template instantiation depth exceeds maximum of %d"
15075 " instantiating %q+D, possibly from virtual table generation"
15076 " (use -ftemplate-depth-NN to increase the maximum)",
15077 max_tinst_depth
, decl
);
15078 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15079 /* Pretend that we defined it. */
15080 DECL_INITIAL (decl
) = error_mark_node
;
15086 struct pending_template
**t
= &pending_templates
;
15087 struct pending_template
*last
= NULL
;
15091 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
15092 bool complete
= false;
15094 if (TYPE_P (instantiation
))
15098 if (!COMPLETE_TYPE_P (instantiation
))
15100 instantiate_class_template (instantiation
);
15101 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
15102 for (fn
= TYPE_METHODS (instantiation
);
15104 fn
= TREE_CHAIN (fn
))
15105 if (! DECL_ARTIFICIAL (fn
))
15106 instantiate_decl (fn
,
15108 /*expl_inst_class_mem_p=*/false);
15109 if (COMPLETE_TYPE_P (instantiation
))
15113 complete
= COMPLETE_TYPE_P (instantiation
);
15117 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
15118 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
15121 = instantiate_decl (instantiation
,
15123 /*expl_inst_class_mem_p=*/false);
15124 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
15128 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
15129 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
15133 /* If INSTANTIATION has been instantiated, then we don't
15134 need to consider it again in the future. */
15142 current_tinst_level
= NULL
;
15144 last_pending_template
= last
;
15146 while (reconsider
);
15148 input_location
= saved_loc
;
15149 in_system_header
= saved_in_system_header
;
15152 /* Substitute ARGVEC into T, which is a list of initializers for
15153 either base class or a non-static data member. The TREE_PURPOSEs
15154 are DECLs, and the TREE_VALUEs are the initializer values. Used by
15155 instantiate_decl. */
15158 tsubst_initializer_list (tree t
, tree argvec
)
15160 tree inits
= NULL_TREE
;
15162 for (; t
; t
= TREE_CHAIN (t
))
15166 tree expanded_bases
= NULL_TREE
;
15167 tree expanded_arguments
= NULL_TREE
;
15170 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
15175 /* Expand the base class expansion type into separate base
15177 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
15178 tf_warning_or_error
,
15180 if (expanded_bases
== error_mark_node
)
15183 /* We'll be building separate TREE_LISTs of arguments for
15185 len
= TREE_VEC_LENGTH (expanded_bases
);
15186 expanded_arguments
= make_tree_vec (len
);
15187 for (i
= 0; i
< len
; i
++)
15188 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
15190 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
15191 expand each argument in the TREE_VALUE of t. */
15192 expr
= make_node (EXPR_PACK_EXPANSION
);
15193 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
15194 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
15196 /* Substitute parameter packs into each argument in the
15198 in_base_initializer
= 1;
15199 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
15201 tree expanded_exprs
;
15203 /* Expand the argument. */
15204 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
15205 expanded_exprs
= tsubst_pack_expansion (expr
, argvec
,
15206 tf_warning_or_error
,
15209 /* Prepend each of the expanded expressions to the
15210 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
15211 for (i
= 0; i
< len
; i
++)
15213 TREE_VEC_ELT (expanded_arguments
, i
) =
15214 tree_cons (NULL_TREE
, TREE_VEC_ELT (expanded_exprs
, i
),
15215 TREE_VEC_ELT (expanded_arguments
, i
));
15218 in_base_initializer
= 0;
15220 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
15221 since we built them backwards. */
15222 for (i
= 0; i
< len
; i
++)
15224 TREE_VEC_ELT (expanded_arguments
, i
) =
15225 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
15229 for (i
= 0; i
< len
; ++i
)
15231 if (expanded_bases
)
15233 decl
= TREE_VEC_ELT (expanded_bases
, i
);
15234 decl
= expand_member_init (decl
);
15235 init
= TREE_VEC_ELT (expanded_arguments
, i
);
15239 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
15240 tf_warning_or_error
, NULL_TREE
);
15242 decl
= expand_member_init (decl
);
15243 if (decl
&& !DECL_P (decl
))
15244 in_base_initializer
= 1;
15246 init
= tsubst_expr (TREE_VALUE (t
), argvec
,
15247 tf_warning_or_error
, NULL_TREE
,
15248 /*integral_constant_expression_p=*/false);
15249 in_base_initializer
= 0;
15254 init
= build_tree_list (decl
, init
);
15255 TREE_CHAIN (init
) = inits
;
15263 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
15266 set_current_access_from_decl (tree decl
)
15268 if (TREE_PRIVATE (decl
))
15269 current_access_specifier
= access_private_node
;
15270 else if (TREE_PROTECTED (decl
))
15271 current_access_specifier
= access_protected_node
;
15273 current_access_specifier
= access_public_node
;
15276 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
15277 is the instantiation (which should have been created with
15278 start_enum) and ARGS are the template arguments to use. */
15281 tsubst_enum (tree tag
, tree newtag
, tree args
)
15285 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
15290 decl
= TREE_VALUE (e
);
15291 /* Note that in a template enum, the TREE_VALUE is the
15292 CONST_DECL, not the corresponding INTEGER_CST. */
15293 value
= tsubst_expr (DECL_INITIAL (decl
),
15294 args
, tf_warning_or_error
, NULL_TREE
,
15295 /*integral_constant_expression_p=*/true);
15297 /* Give this enumeration constant the correct access. */
15298 set_current_access_from_decl (decl
);
15300 /* Actually build the enumerator itself. */
15301 build_enumerator (DECL_NAME (decl
), value
, newtag
);
15304 finish_enum (newtag
);
15305 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
15306 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
15309 /* DECL is a FUNCTION_DECL that is a template specialization. Return
15310 its type -- but without substituting the innermost set of template
15311 arguments. So, innermost set of template parameters will appear in
15315 get_mostly_instantiated_function_type (tree decl
)
15323 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15324 targs
= DECL_TI_ARGS (decl
);
15325 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
15326 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
15328 /* There should be as many levels of arguments as there are levels
15330 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
15332 fn_type
= TREE_TYPE (tmpl
);
15334 if (parm_depth
== 1)
15335 /* No substitution is necessary. */
15339 int i
, save_access_control
;
15342 /* Replace the innermost level of the TARGS with NULL_TREEs to
15343 let tsubst know not to substitute for those parameters. */
15344 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15345 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
15346 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
15347 TMPL_ARGS_LEVEL (targs
, i
));
15348 SET_TMPL_ARGS_LEVEL (partial_args
,
15349 TMPL_ARGS_DEPTH (targs
),
15350 make_tree_vec (DECL_NTPARMS (tmpl
)));
15352 /* Disable access control as this function is used only during
15354 save_access_control
= flag_access_control
;
15355 flag_access_control
= 0;
15357 ++processing_template_decl
;
15358 /* Now, do the (partial) substitution to figure out the
15359 appropriate function type. */
15360 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
15361 --processing_template_decl
;
15363 /* Substitute into the template parameters to obtain the real
15364 innermost set of parameters. This step is important if the
15365 innermost set of template parameters contains value
15366 parameters whose types depend on outer template parameters. */
15367 TREE_VEC_LENGTH (partial_args
)--;
15368 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
15370 flag_access_control
= save_access_control
;
15376 /* Return truthvalue if we're processing a template different from
15377 the last one involved in diagnostics. */
15379 problematic_instantiation_changed (void)
15381 return last_template_error_tick
!= tinst_level_tick
;
15384 /* Remember current template involved in diagnostics. */
15386 record_last_problematic_instantiation (void)
15388 last_template_error_tick
= tinst_level_tick
;
15391 struct tinst_level
*
15392 current_instantiation (void)
15394 return current_tinst_level
;
15397 /* [temp.param] Check that template non-type parm TYPE is of an allowable
15398 type. Return zero for ok, nonzero for disallowed. Issue error and
15399 warning messages under control of COMPLAIN. */
15402 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
15404 if (INTEGRAL_TYPE_P (type
))
15406 else if (POINTER_TYPE_P (type
))
15408 else if (TYPE_PTR_TO_MEMBER_P (type
))
15410 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
15412 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
15415 if (complain
& tf_error
)
15416 error ("%q#T is not a valid type for a template constant parameter", type
);
15420 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
15421 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
15424 dependent_type_p_r (tree type
)
15430 A type is dependent if it is:
15432 -- a template parameter. Template template parameters are types
15433 for us (since TYPE_P holds true for them) so we handle
15435 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15436 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
15438 /* -- a qualified-id with a nested-name-specifier which contains a
15439 class-name that names a dependent type or whose unqualified-id
15440 names a dependent type. */
15441 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15443 /* -- a cv-qualified type where the cv-unqualified type is
15445 type
= TYPE_MAIN_VARIANT (type
);
15446 /* -- a compound type constructed from any dependent type. */
15447 if (TYPE_PTR_TO_MEMBER_P (type
))
15448 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
15449 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
15451 else if (TREE_CODE (type
) == POINTER_TYPE
15452 || TREE_CODE (type
) == REFERENCE_TYPE
)
15453 return dependent_type_p (TREE_TYPE (type
));
15454 else if (TREE_CODE (type
) == FUNCTION_TYPE
15455 || TREE_CODE (type
) == METHOD_TYPE
)
15459 if (dependent_type_p (TREE_TYPE (type
)))
15461 for (arg_type
= TYPE_ARG_TYPES (type
);
15463 arg_type
= TREE_CHAIN (arg_type
))
15464 if (dependent_type_p (TREE_VALUE (arg_type
)))
15468 /* -- an array type constructed from any dependent type or whose
15469 size is specified by a constant expression that is
15470 value-dependent. */
15471 if (TREE_CODE (type
) == ARRAY_TYPE
)
15473 if (TYPE_DOMAIN (type
)
15474 && dependent_type_p (TYPE_DOMAIN (type
)))
15476 return dependent_type_p (TREE_TYPE (type
));
15478 else if (TREE_CODE (type
) == INTEGER_TYPE
15479 && !TREE_CONSTANT (TYPE_MAX_VALUE (type
)))
15481 /* If this is the TYPE_DOMAIN of an array type, consider it
15483 return (value_dependent_expression_p (TYPE_MAX_VALUE (type
))
15484 || type_dependent_expression_p (TYPE_MAX_VALUE (type
)));
15487 /* -- a template-id in which either the template name is a template
15489 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15491 /* ... or any of the template arguments is a dependent type or
15492 an expression that is type-dependent or value-dependent. */
15493 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
15494 && (any_dependent_template_arguments_p
15495 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
15498 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
15499 argument of the `typeof' expression is not type-dependent, then
15500 it should already been have resolved. */
15501 if (TREE_CODE (type
) == TYPEOF_TYPE
15502 || TREE_CODE (type
) == DECLTYPE_TYPE
)
15505 /* A template argument pack is dependent if any of its packed
15507 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
15509 tree args
= ARGUMENT_PACK_ARGS (type
);
15510 int i
, len
= TREE_VEC_LENGTH (args
);
15511 for (i
= 0; i
< len
; ++i
)
15512 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
15516 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
15517 be template parameters. */
15518 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
15521 /* The standard does not specifically mention types that are local
15522 to template functions or local classes, but they should be
15523 considered dependent too. For example:
15525 template <int I> void f() {
15530 The size of `E' cannot be known until the value of `I' has been
15531 determined. Therefore, `E' must be considered dependent. */
15532 scope
= TYPE_CONTEXT (type
);
15533 if (scope
&& TYPE_P (scope
))
15534 return dependent_type_p (scope
);
15535 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15536 return type_dependent_expression_p (scope
);
15538 /* Other types are non-dependent. */
15542 /* Returns TRUE if TYPE is dependent, in the sense of
15543 [temp.dep.type]. */
15546 dependent_type_p (tree type
)
15548 /* If there are no template parameters in scope, then there can't be
15549 any dependent types. */
15550 if (!processing_template_decl
)
15552 /* If we are not processing a template, then nobody should be
15553 providing us with a dependent type. */
15555 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
);
15559 /* If the type is NULL, we have not computed a type for the entity
15560 in question; in that case, the type is dependent. */
15564 /* Erroneous types can be considered non-dependent. */
15565 if (type
== error_mark_node
)
15568 /* If we have not already computed the appropriate value for TYPE,
15570 if (!TYPE_DEPENDENT_P_VALID (type
))
15572 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
15573 TYPE_DEPENDENT_P_VALID (type
) = 1;
15576 return TYPE_DEPENDENT_P (type
);
15579 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
15582 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
15587 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
15589 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
15592 scope
= TREE_OPERAND (expression
, 0);
15593 name
= TREE_OPERAND (expression
, 1);
15597 An id-expression is type-dependent if it contains a
15598 nested-name-specifier that contains a class-name that names a
15600 /* The suggested resolution to Core Issue 2 implies that if the
15601 qualifying type is the current class, then we must peek
15604 && currently_open_class (scope
)
15605 && !criterion (name
))
15607 if (dependent_type_p (scope
))
15613 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
15614 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
15618 value_dependent_expression_p (tree expression
)
15620 if (!processing_template_decl
)
15623 /* A name declared with a dependent type. */
15624 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
15627 switch (TREE_CODE (expression
))
15629 case IDENTIFIER_NODE
:
15630 /* A name that has not been looked up -- must be dependent. */
15633 case TEMPLATE_PARM_INDEX
:
15634 /* A non-type template parm. */
15638 /* A non-type template parm. */
15639 if (DECL_TEMPLATE_PARM_P (expression
))
15644 /* A constant with integral or enumeration type and is initialized
15645 with an expression that is value-dependent. */
15646 if (DECL_INITIAL (expression
)
15647 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
15648 && value_dependent_expression_p (DECL_INITIAL (expression
)))
15652 case DYNAMIC_CAST_EXPR
:
15653 case STATIC_CAST_EXPR
:
15654 case CONST_CAST_EXPR
:
15655 case REINTERPRET_CAST_EXPR
:
15657 /* These expressions are value-dependent if the type to which
15658 the cast occurs is dependent or the expression being casted
15659 is value-dependent. */
15661 tree type
= TREE_TYPE (expression
);
15663 if (dependent_type_p (type
))
15666 /* A functional cast has a list of operands. */
15667 expression
= TREE_OPERAND (expression
, 0);
15670 /* If there are no operands, it must be an expression such
15671 as "int()". This should not happen for aggregate types
15672 because it would form non-constant expressions. */
15673 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
15678 if (TREE_CODE (expression
) == TREE_LIST
)
15679 return any_value_dependent_elements_p (expression
);
15681 return value_dependent_expression_p (expression
);
15686 /* A `sizeof' expression is value-dependent if the operand is
15687 type-dependent or is a pack expansion. */
15688 expression
= TREE_OPERAND (expression
, 0);
15689 if (PACK_EXPANSION_P (expression
))
15691 else if (TYPE_P (expression
))
15692 return dependent_type_p (expression
);
15693 return type_dependent_expression_p (expression
);
15696 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
15698 case COMPONENT_REF
:
15699 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
15700 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
15703 /* A CALL_EXPR may appear in a constant expression if it is a
15704 call to a builtin function, e.g., __builtin_constant_p. All
15705 such calls are value-dependent. */
15708 case NONTYPE_ARGUMENT_PACK
:
15709 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
15710 is value-dependent. */
15712 tree values
= ARGUMENT_PACK_ARGS (expression
);
15713 int i
, len
= TREE_VEC_LENGTH (values
);
15715 for (i
= 0; i
< len
; ++i
)
15716 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
15724 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
15725 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
15726 || (type2
? dependent_type_p (type2
) : false));
15730 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
15731 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
15734 /* A constant expression is value-dependent if any subexpression is
15735 value-dependent. */
15736 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
15738 case tcc_reference
:
15740 return (value_dependent_expression_p
15741 (TREE_OPERAND (expression
, 0)));
15743 case tcc_comparison
:
15745 return ((value_dependent_expression_p
15746 (TREE_OPERAND (expression
, 0)))
15747 || (value_dependent_expression_p
15748 (TREE_OPERAND (expression
, 1))));
15750 case tcc_expression
:
15754 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
15755 /* In some cases, some of the operands may be missing.
15756 (For example, in the case of PREDECREMENT_EXPR, the
15757 amount to increment by may be missing.) That doesn't
15758 make the expression dependent. */
15759 if (TREE_OPERAND (expression
, i
)
15760 && (value_dependent_expression_p
15761 (TREE_OPERAND (expression
, i
))))
15771 /* The expression is not value-dependent. */
15775 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
15776 [temp.dep.expr]. */
15779 type_dependent_expression_p (tree expression
)
15781 if (!processing_template_decl
)
15784 if (expression
== error_mark_node
)
15787 /* An unresolved name is always dependent. */
15788 if (TREE_CODE (expression
) == IDENTIFIER_NODE
15789 || TREE_CODE (expression
) == USING_DECL
)
15792 /* Some expression forms are never type-dependent. */
15793 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
15794 || TREE_CODE (expression
) == SIZEOF_EXPR
15795 || TREE_CODE (expression
) == ALIGNOF_EXPR
15796 || TREE_CODE (expression
) == TRAIT_EXPR
15797 || TREE_CODE (expression
) == TYPEID_EXPR
15798 || TREE_CODE (expression
) == DELETE_EXPR
15799 || TREE_CODE (expression
) == VEC_DELETE_EXPR
15800 || TREE_CODE (expression
) == THROW_EXPR
)
15803 /* The types of these expressions depends only on the type to which
15804 the cast occurs. */
15805 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
15806 || TREE_CODE (expression
) == STATIC_CAST_EXPR
15807 || TREE_CODE (expression
) == CONST_CAST_EXPR
15808 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
15809 || TREE_CODE (expression
) == CAST_EXPR
)
15810 return dependent_type_p (TREE_TYPE (expression
));
15812 /* The types of these expressions depends only on the type created
15813 by the expression. */
15814 if (TREE_CODE (expression
) == NEW_EXPR
15815 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
15817 /* For NEW_EXPR tree nodes created inside a template, either
15818 the object type itself or a TREE_LIST may appear as the
15820 tree type
= TREE_OPERAND (expression
, 1);
15821 if (TREE_CODE (type
) == TREE_LIST
)
15822 /* This is an array type. We need to check array dimensions
15824 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
15825 || value_dependent_expression_p
15826 (TREE_OPERAND (TREE_VALUE (type
), 1));
15828 return dependent_type_p (type
);
15831 if (TREE_CODE (expression
) == SCOPE_REF
15832 && dependent_scope_ref_p (expression
,
15833 type_dependent_expression_p
))
15836 if (TREE_CODE (expression
) == FUNCTION_DECL
15837 && DECL_LANG_SPECIFIC (expression
)
15838 && DECL_TEMPLATE_INFO (expression
)
15839 && (any_dependent_template_arguments_p
15840 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
15843 if (TREE_CODE (expression
) == TEMPLATE_DECL
15844 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
15847 if (TREE_CODE (expression
) == STMT_EXPR
)
15848 expression
= stmt_expr_value_expr (expression
);
15850 if (TREE_TYPE (expression
) == unknown_type_node
)
15852 if (TREE_CODE (expression
) == ADDR_EXPR
)
15853 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
15854 if (TREE_CODE (expression
) == COMPONENT_REF
15855 || TREE_CODE (expression
) == OFFSET_REF
)
15857 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
15859 expression
= TREE_OPERAND (expression
, 1);
15860 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
15863 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15864 if (TREE_CODE (expression
) == SCOPE_REF
)
15867 if (TREE_CODE (expression
) == BASELINK
)
15868 expression
= BASELINK_FUNCTIONS (expression
);
15870 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
15872 if (any_dependent_template_arguments_p
15873 (TREE_OPERAND (expression
, 1)))
15875 expression
= TREE_OPERAND (expression
, 0);
15877 gcc_assert (TREE_CODE (expression
) == OVERLOAD
15878 || TREE_CODE (expression
) == FUNCTION_DECL
);
15882 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
15884 expression
= OVL_NEXT (expression
);
15889 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
15891 return (dependent_type_p (TREE_TYPE (expression
)));
15894 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15895 contains a type-dependent expression. */
15898 any_type_dependent_arguments_p (const_tree args
)
15902 tree arg
= TREE_VALUE (args
);
15904 if (type_dependent_expression_p (arg
))
15906 args
= TREE_CHAIN (args
);
15911 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
15912 expressions) contains any value-dependent expressions. */
15915 any_value_dependent_elements_p (const_tree list
)
15917 for (; list
; list
= TREE_CHAIN (list
))
15918 if (value_dependent_expression_p (TREE_VALUE (list
)))
15924 /* Returns TRUE if the ARG (a template argument) is dependent. */
15927 dependent_template_arg_p (tree arg
)
15929 if (!processing_template_decl
)
15932 if (TREE_CODE (arg
) == TEMPLATE_DECL
15933 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15934 return dependent_template_p (arg
);
15935 else if (ARGUMENT_PACK_P (arg
))
15937 tree args
= ARGUMENT_PACK_ARGS (arg
);
15938 int i
, len
= TREE_VEC_LENGTH (args
);
15939 for (i
= 0; i
< len
; ++i
)
15941 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
15947 else if (TYPE_P (arg
))
15948 return dependent_type_p (arg
);
15950 return (type_dependent_expression_p (arg
)
15951 || value_dependent_expression_p (arg
));
15954 /* Returns true if ARGS (a collection of template arguments) contains
15955 any types that require structural equality testing. */
15958 any_template_arguments_need_structural_equality_p (tree args
)
15965 if (args
== error_mark_node
)
15968 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
15970 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
15971 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
15973 tree arg
= TREE_VEC_ELT (level
, j
);
15974 tree packed_args
= NULL_TREE
;
15977 if (ARGUMENT_PACK_P (arg
))
15979 /* Look inside the argument pack. */
15980 packed_args
= ARGUMENT_PACK_ARGS (arg
);
15981 len
= TREE_VEC_LENGTH (packed_args
);
15984 for (k
= 0; k
< len
; ++k
)
15987 arg
= TREE_VEC_ELT (packed_args
, k
);
15989 if (error_operand_p (arg
))
15991 else if (TREE_CODE (arg
) == TEMPLATE_DECL
15992 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15994 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
15996 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
15997 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
16006 /* Returns true if ARGS (a collection of template arguments) contains
16007 any dependent arguments. */
16010 any_dependent_template_arguments_p (const_tree args
)
16017 if (args
== error_mark_node
)
16020 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
16022 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
16023 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
16024 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
16031 /* Returns TRUE if the template TMPL is dependent. */
16034 dependent_template_p (tree tmpl
)
16036 if (TREE_CODE (tmpl
) == OVERLOAD
)
16040 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
16042 tmpl
= OVL_CHAIN (tmpl
);
16047 /* Template template parameters are dependent. */
16048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
16049 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
16051 /* So are names that have not been looked up. */
16052 if (TREE_CODE (tmpl
) == SCOPE_REF
16053 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
16055 /* So are member templates of dependent classes. */
16056 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
16057 return dependent_type_p (DECL_CONTEXT (tmpl
));
16061 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
16064 dependent_template_id_p (tree tmpl
, tree args
)
16066 return (dependent_template_p (tmpl
)
16067 || any_dependent_template_arguments_p (args
));
16070 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
16071 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
16072 no such TYPE can be found. Note that this function peers inside
16073 uninstantiated templates and therefore should be used only in
16074 extremely limited situations. ONLY_CURRENT_P restricts this
16075 peering to the currently open classes hierarchy (which is required
16076 when comparing types). */
16079 resolve_typename_type (tree type
, bool only_current_p
)
16088 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
16090 scope
= TYPE_CONTEXT (type
);
16091 name
= TYPE_IDENTIFIER (type
);
16093 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
16094 it first before we can figure out what NAME refers to. */
16095 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
16096 scope
= resolve_typename_type (scope
, only_current_p
);
16097 /* If we don't know what SCOPE refers to, then we cannot resolve the
16099 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
16101 /* If the SCOPE is a template type parameter, we have no way of
16102 resolving the name. */
16103 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
16105 /* If the SCOPE is not the current instantiation, there's no reason
16106 to look inside it. */
16107 if (only_current_p
&& !currently_open_class (scope
))
16109 /* If SCOPE isn't the template itself, it will not have a valid
16110 TYPE_FIELDS list. */
16111 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
16112 /* scope is either the template itself or a compatible instantiation
16113 like X<T>, so look up the name in the original template. */
16114 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
16116 /* scope is a partial instantiation, so we can't do the lookup or we
16117 will lose the template arguments. */
16119 /* Enter the SCOPE so that name lookup will be resolved as if we
16120 were in the class definition. In particular, SCOPE will no
16121 longer be considered a dependent type. */
16122 pushed_scope
= push_scope (scope
);
16123 /* Look up the declaration. */
16124 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
16126 result
= NULL_TREE
;
16128 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
16129 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
16132 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
16133 && TREE_CODE (decl
) == TYPE_DECL
)
16135 result
= TREE_TYPE (decl
);
16136 if (result
== error_mark_node
)
16137 result
= NULL_TREE
;
16139 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
16140 && DECL_CLASS_TEMPLATE_P (decl
))
16144 /* Obtain the template and the arguments. */
16145 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
16146 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
16147 /* Instantiate the template. */
16148 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
16149 /*entering_scope=*/0,
16150 tf_error
| tf_user
);
16151 if (result
== error_mark_node
)
16152 result
= NULL_TREE
;
16155 /* Leave the SCOPE. */
16157 pop_scope (pushed_scope
);
16159 /* If we failed to resolve it, return the original typename. */
16163 /* If lookup found a typename type, resolve that too. */
16164 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
16166 /* Ill-formed programs can cause infinite recursion here, so we
16167 must catch that. */
16168 TYPENAME_IS_RESOLVING_P (type
) = 1;
16169 result
= resolve_typename_type (result
, only_current_p
);
16170 TYPENAME_IS_RESOLVING_P (type
) = 0;
16173 /* Qualify the resulting type. */
16174 quals
= cp_type_quals (type
);
16176 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
16181 /* EXPR is an expression which is not type-dependent. Return a proxy
16182 for EXPR that can be used to compute the types of larger
16183 expressions containing EXPR. */
16186 build_non_dependent_expr (tree expr
)
16190 /* Preserve null pointer constants so that the type of things like
16191 "p == 0" where "p" is a pointer can be determined. */
16192 if (null_ptr_cst_p (expr
))
16194 /* Preserve OVERLOADs; the functions must be available to resolve
16197 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
16198 inner_expr
= stmt_expr_value_expr (inner_expr
);
16199 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
16200 inner_expr
= TREE_OPERAND (inner_expr
, 0);
16201 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
16202 inner_expr
= TREE_OPERAND (inner_expr
, 1);
16203 if (is_overloaded_fn (inner_expr
)
16204 || TREE_CODE (inner_expr
) == OFFSET_REF
)
16206 /* There is no need to return a proxy for a variable. */
16207 if (TREE_CODE (expr
) == VAR_DECL
)
16209 /* Preserve string constants; conversions from string constants to
16210 "char *" are allowed, even though normally a "const char *"
16211 cannot be used to initialize a "char *". */
16212 if (TREE_CODE (expr
) == STRING_CST
)
16214 /* Preserve arithmetic constants, as an optimization -- there is no
16215 reason to create a new node. */
16216 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
16218 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
16219 There is at least one place where we want to know that a
16220 particular expression is a throw-expression: when checking a ?:
16221 expression, there are special rules if the second or third
16222 argument is a throw-expression. */
16223 if (TREE_CODE (expr
) == THROW_EXPR
)
16226 if (TREE_CODE (expr
) == COND_EXPR
)
16227 return build3 (COND_EXPR
,
16229 TREE_OPERAND (expr
, 0),
16230 (TREE_OPERAND (expr
, 1)
16231 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
16232 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
16233 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
16234 if (TREE_CODE (expr
) == COMPOUND_EXPR
16235 && !COMPOUND_EXPR_OVERLOADED (expr
))
16236 return build2 (COMPOUND_EXPR
,
16238 TREE_OPERAND (expr
, 0),
16239 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
16241 /* If the type is unknown, it can't really be non-dependent */
16242 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
16244 /* Otherwise, build a NON_DEPENDENT_EXPR.
16246 REFERENCE_TYPEs are not stripped for expressions in templates
16247 because doing so would play havoc with mangling. Consider, for
16250 template <typename T> void f<T& g>() { g(); }
16252 In the body of "f", the expression for "g" will have
16253 REFERENCE_TYPE, even though the standard says that it should
16254 not. The reason is that we must preserve the syntactic form of
16255 the expression so that mangling (say) "f<g>" inside the body of
16256 "f" works out correctly. Therefore, the REFERENCE_TYPE is
16258 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
16261 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
16262 Return a new TREE_LIST with the various arguments replaced with
16263 equivalent non-dependent expressions. */
16266 build_non_dependent_args (tree args
)
16271 new_args
= NULL_TREE
;
16272 for (a
= args
; a
; a
= TREE_CHAIN (a
))
16273 new_args
= tree_cons (NULL_TREE
,
16274 build_non_dependent_expr (TREE_VALUE (a
)),
16276 return nreverse (new_args
);
16279 #include "gt-cp-pt.h"