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, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
47 #include "tree-iterator.h"
50 /* The type of functions taking a tree, and some additional data, and
52 typedef int (*tree_fn_t
) (tree
, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct pending_template
GTY (()) {
58 struct pending_template
*next
;
59 struct tinst_level
*tinst
;
62 static GTY(()) struct pending_template
*pending_templates
;
63 static GTY(()) struct pending_template
*last_pending_template
;
65 /* The PENDING_ATTRIBUTE is a list node that records an attribute whose
66 instantiation has been deferred until the whole class has been
67 instantiated. This deferral currently only happens to the lock attributes
68 whose arguments could be data members declared later in the class
69 specification, as shown in the following example:
73 T shared_var GUARDED_BY(lock);
76 struct pending_attribute
GTY (()) {
81 tsubst_flags_t complain
;
83 struct pending_attribute
*next
;
86 static GTY(()) struct pending_attribute
*pending_lock_attributes
= NULL
;
88 int processing_template_parmlist
;
89 static int template_header_count
;
91 static GTY(()) tree saved_trees
;
92 static VEC(int,heap
) *inline_parm_levels
;
94 static GTY(()) struct tinst_level
*current_tinst_level
;
96 static GTY(()) tree saved_access_scope
;
98 /* Live only within one (recursive) call to tsubst_expr. We use
99 this to pass the statement expression node from the STMT_EXPR
100 to the EXPR_STMT that is its result. */
101 static tree cur_stmt_expr
;
103 /* A map from local variable declarations in the body of the template
104 presently being instantiated to the corresponding instantiated
106 static htab_t local_specializations
;
108 /* Contains canonical template parameter types. The vector is indexed by
109 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
110 TREE_LIST, whose TREE_VALUEs contain the canonical template
111 parameters of various types and levels. */
112 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
114 #define UNIFY_ALLOW_NONE 0
115 #define UNIFY_ALLOW_MORE_CV_QUAL 1
116 #define UNIFY_ALLOW_LESS_CV_QUAL 2
117 #define UNIFY_ALLOW_DERIVED 4
118 #define UNIFY_ALLOW_INTEGER 8
119 #define UNIFY_ALLOW_OUTER_LEVEL 16
120 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
121 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
123 static void push_access_scope (tree
);
124 static void pop_access_scope (tree
);
125 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
126 unification_kind_t
, int);
127 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
128 unification_kind_t
, int, bool);
129 static int unify (tree
, tree
, tree
, tree
, int);
130 static void add_pending_template (tree
);
131 static int push_tinst_level (tree
);
132 static void pop_tinst_level (void);
133 static tree
reopen_tinst_level (struct tinst_level
*);
134 static tree
tsubst_initializer_list (tree
, tree
);
135 static tree
get_class_bindings (tree
, tree
, tree
);
136 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
138 static void tsubst_enum (tree
, tree
, tree
);
139 static tree
add_to_template_args (tree
, tree
);
140 static tree
add_outermost_template_args (tree
, tree
);
141 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
142 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
144 static int type_unification_real (tree
, tree
, tree
, tree
,
145 int, unification_kind_t
, int);
146 static void note_template_header (int);
147 static tree
convert_nontype_argument_function (tree
, tree
);
148 static tree
convert_nontype_argument (tree
, tree
);
149 static tree
convert_template_argument (tree
, tree
, tree
,
150 tsubst_flags_t
, int, tree
);
151 static int for_each_template_parm (tree
, tree_fn_t
, void*,
152 struct pointer_set_t
*, bool);
153 static tree
expand_template_argument_pack (tree
);
154 static tree
build_template_parm_index (int, int, int, tree
, tree
);
155 static bool inline_needs_template_parms (tree
);
156 static void push_inline_template_parms_recursive (tree
, int);
157 static tree
retrieve_local_specialization (tree
);
158 static void register_local_specialization (tree
, tree
);
159 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
160 static int mark_template_parm (tree
, void *);
161 static int template_parm_this_level_p (tree
, void *);
162 static tree
tsubst_friend_function (tree
, tree
);
163 static tree
tsubst_friend_class (tree
, tree
);
164 static int can_complete_type_without_circularity (tree
);
165 static tree
get_bindings (tree
, tree
, tree
, bool);
166 static int template_decl_level (tree
);
167 static int check_cv_quals_for_unify (int, tree
, tree
);
168 static void template_parm_level_and_index (tree
, int*, int*);
169 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
170 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
173 static void regenerate_decl_from_template (tree
, tree
);
174 static tree
most_specialized_class (tree
, tree
);
175 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
176 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
177 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
178 static bool check_specialization_scope (void);
179 static tree
process_partial_specialization (tree
);
180 static void set_current_access_from_decl (tree
);
181 static tree
get_template_base (tree
, tree
, tree
, tree
);
182 static tree
try_class_unification (tree
, tree
, tree
, tree
);
183 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
185 static bool template_template_parm_bindings_ok_p (tree
, tree
);
186 static int template_args_equal (tree
, tree
);
187 static void tsubst_default_arguments (tree
);
188 static tree
for_each_template_parm_r (tree
*, int *, void *);
189 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
190 static void copy_default_args_to_explicit_spec (tree
);
191 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
192 static int eq_local_specializations (const void *, const void *);
193 static bool dependent_template_arg_p (tree
);
194 static bool any_template_arguments_need_structural_equality_p (tree
);
195 static bool dependent_type_p_r (tree
);
196 static tree
tsubst (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
198 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
201 /* Make the current scope suitable for access checking when we are
202 processing T. T can be FUNCTION_DECL for instantiated function
203 template, or VAR_DECL for static member variable (need by
204 instantiate_decl). */
207 push_access_scope (tree t
)
209 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
210 || TREE_CODE (t
) == VAR_DECL
);
212 if (DECL_FRIEND_CONTEXT (t
))
213 push_nested_class (DECL_FRIEND_CONTEXT (t
));
214 else if (DECL_CLASS_SCOPE_P (t
))
215 push_nested_class (DECL_CONTEXT (t
));
217 push_to_top_level ();
219 if (TREE_CODE (t
) == FUNCTION_DECL
)
221 saved_access_scope
= tree_cons
222 (NULL_TREE
, current_function_decl
, saved_access_scope
);
223 current_function_decl
= t
;
227 /* Restore the scope set up by push_access_scope. T is the node we
231 pop_access_scope (tree t
)
233 if (TREE_CODE (t
) == FUNCTION_DECL
)
235 current_function_decl
= TREE_VALUE (saved_access_scope
);
236 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
239 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
242 pop_from_top_level ();
245 /* Do any processing required when DECL (a member template
246 declaration) is finished. Returns the TEMPLATE_DECL corresponding
247 to DECL, unless it is a specialization, in which case the DECL
248 itself is returned. */
251 finish_member_template_decl (tree decl
)
253 if (decl
== error_mark_node
)
254 return error_mark_node
;
256 gcc_assert (DECL_P (decl
));
258 if (TREE_CODE (decl
) == TYPE_DECL
)
262 type
= TREE_TYPE (decl
);
263 if (type
== error_mark_node
)
264 return error_mark_node
;
265 if (MAYBE_CLASS_TYPE_P (type
)
266 && CLASSTYPE_TEMPLATE_INFO (type
)
267 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
269 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
270 check_member_template (tmpl
);
275 else if (TREE_CODE (decl
) == FIELD_DECL
)
276 error ("data member %qD cannot be a member template", decl
);
277 else if (DECL_TEMPLATE_INFO (decl
))
279 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
281 check_member_template (DECL_TI_TEMPLATE (decl
));
282 return DECL_TI_TEMPLATE (decl
);
288 error ("invalid member template declaration %qD", decl
);
290 return error_mark_node
;
293 /* Return the template info node corresponding to T, whatever T is. */
296 get_template_info (tree t
)
298 tree tinfo
= NULL_TREE
;
300 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
301 tinfo
= DECL_TEMPLATE_INFO (t
);
303 if (!tinfo
&& TREE_CODE (t
) == TYPE_DECL
)
306 if (TAGGED_TYPE_P (t
))
307 tinfo
= TYPE_TEMPLATE_INFO (t
);
312 /* Returns the template nesting level of the indicated class TYPE.
322 A<T>::B<U> has depth two, while A<T> has depth one.
323 Both A<T>::B<int> and A<int>::B<U> have depth one, if
324 they are instantiations, not specializations.
326 This function is guaranteed to return 0 if passed NULL_TREE so
327 that, for example, `template_class_depth (current_class_type)' is
331 template_class_depth (tree type
)
336 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
337 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
338 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
340 tree tinfo
= get_template_info (type
);
342 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
343 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
350 /* Subroutine of maybe_begin_member_template_processing.
351 Returns true if processing DECL needs us to push template parms. */
354 inline_needs_template_parms (tree decl
)
356 if (! DECL_TEMPLATE_INFO (decl
))
359 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
360 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
363 /* Subroutine of maybe_begin_member_template_processing.
364 Push the template parms in PARMS, starting from LEVELS steps into the
365 chain, and ending at the beginning, since template parms are listed
369 push_inline_template_parms_recursive (tree parmlist
, int levels
)
371 tree parms
= TREE_VALUE (parmlist
);
375 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
377 ++processing_template_decl
;
378 current_template_parms
379 = tree_cons (size_int (processing_template_decl
),
380 parms
, current_template_parms
);
381 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
383 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
385 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
387 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
389 if (parm
== error_mark_node
)
392 gcc_assert (DECL_P (parm
));
394 switch (TREE_CODE (parm
))
403 /* Make a CONST_DECL as is done in process_template_parm.
404 It is ugly that we recreate this here; the original
405 version built in process_template_parm is no longer
407 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
409 DECL_ARTIFICIAL (decl
) = 1;
410 TREE_CONSTANT (decl
) = 1;
411 TREE_READONLY (decl
) = 1;
412 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
413 SET_DECL_TEMPLATE_PARM_P (decl
);
424 /* Restore the template parameter context for a member template or
425 a friend template defined in a class definition. */
428 maybe_begin_member_template_processing (tree decl
)
433 if (inline_needs_template_parms (decl
))
435 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
436 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
438 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
441 parms
= TREE_CHAIN (parms
);
444 push_inline_template_parms_recursive (parms
, levels
);
447 /* Remember how many levels of template parameters we pushed so that
448 we can pop them later. */
449 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
452 /* Undo the effects of maybe_begin_member_template_processing. */
455 maybe_end_member_template_processing (void)
460 if (VEC_length (int, inline_parm_levels
) == 0)
463 last
= VEC_pop (int, inline_parm_levels
);
464 for (i
= 0; i
< last
; ++i
)
466 --processing_template_decl
;
467 current_template_parms
= TREE_CHAIN (current_template_parms
);
472 /* Return a new template argument vector which contains all of ARGS,
473 but has as its innermost set of arguments the EXTRA_ARGS. */
476 add_to_template_args (tree args
, tree extra_args
)
483 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
484 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
486 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
487 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
489 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
490 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
495 /* Like add_to_template_args, but only the outermost ARGS are added to
496 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
497 (EXTRA_ARGS) levels are added. This function is used to combine
498 the template arguments from a partial instantiation with the
499 template arguments used to attain the full instantiation from the
500 partial instantiation. */
503 add_outermost_template_args (tree args
, tree extra_args
)
507 /* If there are more levels of EXTRA_ARGS than there are ARGS,
508 something very fishy is going on. */
509 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
511 /* If *all* the new arguments will be the EXTRA_ARGS, just return
513 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
516 /* For the moment, we make ARGS look like it contains fewer levels. */
517 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
519 new_args
= add_to_template_args (args
, extra_args
);
521 /* Now, we restore ARGS to its full dimensions. */
522 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
527 /* Return the N levels of innermost template arguments from the ARGS. */
530 get_innermost_template_args (tree args
, int n
)
538 /* If N is 1, just return the innermost set of template arguments. */
540 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
542 /* If we're not removing anything, just return the arguments we were
544 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
545 gcc_assert (extra_levels
>= 0);
546 if (extra_levels
== 0)
549 /* Make a new set of arguments, not containing the outer arguments. */
550 new_args
= make_tree_vec (n
);
551 for (i
= 1; i
<= n
; ++i
)
552 SET_TMPL_ARGS_LEVEL (new_args
, i
,
553 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
558 /* The inverse of get_innermost_template_args: Return all but the innermost
559 EXTRA_LEVELS levels of template arguments from the ARGS. */
562 strip_innermost_template_args (tree args
, int extra_levels
)
565 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
570 /* If N is 1, just return the outermost set of template arguments. */
572 return TMPL_ARGS_LEVEL (args
, 1);
574 /* If we're not removing anything, just return the arguments we were
576 gcc_assert (extra_levels
>= 0);
577 if (extra_levels
== 0)
580 /* Make a new set of arguments, not containing the inner arguments. */
581 new_args
= make_tree_vec (n
);
582 for (i
= 1; i
<= n
; ++i
)
583 SET_TMPL_ARGS_LEVEL (new_args
, i
,
584 TMPL_ARGS_LEVEL (args
, i
));
589 /* We've got a template header coming up; push to a new level for storing
593 begin_template_parm_list (void)
595 /* We use a non-tag-transparent scope here, which causes pushtag to
596 put tags in this scope, rather than in the enclosing class or
597 namespace scope. This is the right thing, since we want
598 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
599 global template class, push_template_decl handles putting the
600 TEMPLATE_DECL into top-level scope. For a nested template class,
603 template <class T> struct S1 {
604 template <class T> struct S2 {};
607 pushtag contains special code to call pushdecl_with_scope on the
608 TEMPLATE_DECL for S2. */
609 begin_scope (sk_template_parms
, NULL
);
610 ++processing_template_decl
;
611 ++processing_template_parmlist
;
612 note_template_header (0);
615 /* This routine is called when a specialization is declared. If it is
616 invalid to declare a specialization here, an error is reported and
617 false is returned, otherwise this routine will return true. */
620 check_specialization_scope (void)
622 tree scope
= current_scope ();
626 An explicit specialization shall be declared in the namespace of
627 which the template is a member, or, for member templates, in the
628 namespace of which the enclosing class or enclosing class
629 template is a member. An explicit specialization of a member
630 function, member class or static data member of a class template
631 shall be declared in the namespace of which the class template
633 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
635 error ("explicit specialization in non-namespace scope %qD", scope
);
641 In an explicit specialization declaration for a member of a class
642 template or a member template that appears in namespace scope,
643 the member template and some of its enclosing class templates may
644 remain unspecialized, except that the declaration shall not
645 explicitly specialize a class member template if its enclosing
646 class templates are not explicitly specialized as well. */
647 if (current_template_parms
)
649 error ("enclosing class templates are not explicitly specialized");
656 /* We've just seen template <>. */
659 begin_specialization (void)
661 begin_scope (sk_template_spec
, NULL
);
662 note_template_header (1);
663 return check_specialization_scope ();
666 /* Called at then end of processing a declaration preceded by
670 end_specialization (void)
673 reset_specialization ();
676 /* Any template <>'s that we have seen thus far are not referring to a
677 function specialization. */
680 reset_specialization (void)
682 processing_specialization
= 0;
683 template_header_count
= 0;
686 /* We've just seen a template header. If SPECIALIZATION is nonzero,
687 it was of the form template <>. */
690 note_template_header (int specialization
)
692 processing_specialization
= specialization
;
693 template_header_count
++;
696 /* We're beginning an explicit instantiation. */
699 begin_explicit_instantiation (void)
701 gcc_assert (!processing_explicit_instantiation
);
702 processing_explicit_instantiation
= true;
707 end_explicit_instantiation (void)
709 gcc_assert (processing_explicit_instantiation
);
710 processing_explicit_instantiation
= false;
713 /* An explicit specialization or partial specialization TMPL is being
714 declared. Check that the namespace in which the specialization is
715 occurring is permissible. Returns false iff it is invalid to
716 specialize TMPL in the current namespace. */
719 check_specialization_namespace (tree tmpl
)
721 tree tpl_ns
= decl_namespace_context (tmpl
);
725 An explicit specialization shall be declared in the namespace of
726 which the template is a member, or, for member templates, in the
727 namespace of which the enclosing class or enclosing class
728 template is a member. An explicit specialization of a member
729 function, member class or static data member of a class template
730 shall be declared in the namespace of which the class template is
732 if (is_associated_namespace (current_namespace
, tpl_ns
))
733 /* Same or super-using namespace. */
737 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
738 permerror (input_location
, " from definition of %q+#D", tmpl
);
743 /* SPEC is an explicit instantiation. Check that it is valid to
744 perform this explicit instantiation in the current namespace. */
747 check_explicit_instantiation_namespace (tree spec
)
751 /* DR 275: An explicit instantiation shall appear in an enclosing
752 namespace of its template. */
753 ns
= decl_namespace_context (spec
);
754 if (!is_ancestor (current_namespace
, ns
))
755 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
756 "(which does not enclose namespace %qD)",
757 spec
, current_namespace
, ns
);
760 /* The TYPE is being declared. If it is a template type, that means it
761 is a partial specialization. Do appropriate error-checking. */
764 maybe_process_partial_specialization (tree type
)
768 if (type
== error_mark_node
)
769 return error_mark_node
;
771 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
773 error ("name of class shadows template template parameter %qD",
775 return error_mark_node
;
778 context
= TYPE_CONTEXT (type
);
780 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
782 /* This is for ordinary explicit specialization and partial
783 specialization of a template class such as:
785 template <> class C<int>;
789 template <class T> class C<T*>;
791 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
793 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
794 && !COMPLETE_TYPE_P (type
))
796 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
797 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
798 if (processing_template_decl
)
800 if (push_template_decl (TYPE_MAIN_DECL (type
))
802 return error_mark_node
;
805 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
806 error ("specialization of %qT after instantiation", type
);
808 else if (CLASS_TYPE_P (type
)
809 && !CLASSTYPE_USE_TEMPLATE (type
)
810 && CLASSTYPE_TEMPLATE_INFO (type
)
811 && context
&& CLASS_TYPE_P (context
)
812 && CLASSTYPE_TEMPLATE_INFO (context
))
814 /* This is for an explicit specialization of member class
815 template according to [temp.expl.spec/18]:
817 template <> template <class U> class C<int>::D;
819 The context `C<int>' must be an implicit instantiation.
820 Otherwise this is just a member class template declared
823 template <> class C<int> { template <class U> class D; };
824 template <> template <class U> class C<int>::D;
826 In the first case, `C<int>::D' is a specialization of `C<T>::D'
827 while in the second case, `C<int>::D' is a primary template
828 and `C<T>::D' may not exist. */
830 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
831 && !COMPLETE_TYPE_P (type
))
835 if (current_namespace
836 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
838 permerror (input_location
, "specializing %q#T in different namespace", type
);
839 permerror (input_location
, " from definition of %q+#D",
840 CLASSTYPE_TI_TEMPLATE (type
));
843 /* Check for invalid specialization after instantiation:
845 template <> template <> class C<int>::D<int>;
846 template <> template <class U> class C<int>::D; */
848 for (t
= DECL_TEMPLATE_INSTANTIATIONS
849 (most_general_template (CLASSTYPE_TI_TEMPLATE (type
)));
850 t
; t
= TREE_CHAIN (t
))
851 if (TREE_VALUE (t
) != type
852 && TYPE_CONTEXT (TREE_VALUE (t
)) == context
)
853 error ("specialization %qT after instantiation %qT",
854 type
, TREE_VALUE (t
));
856 /* Mark TYPE as a specialization. And as a result, we only
857 have one level of template argument for the innermost
859 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
860 CLASSTYPE_TI_ARGS (type
)
861 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
864 else if (processing_specialization
)
866 error ("explicit specialization of non-template %qT", type
);
867 return error_mark_node
;
873 /* Returns nonzero if we can optimize the retrieval of specializations
874 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
875 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
878 optimize_specialization_lookup_p (tree tmpl
)
880 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
881 && DECL_CLASS_SCOPE_P (tmpl
)
882 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
884 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
885 /* The optimized lookup depends on the fact that the
886 template arguments for the member function template apply
887 purely to the containing class, which is not true if the
888 containing class is an explicit or partial
890 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
891 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
892 && !DECL_CONV_FN_P (tmpl
)
893 /* It is possible to have a template that is not a member
894 template and is not a member of a template class:
896 template <typename T>
897 struct S { friend A::f(); };
899 Here, the friend function is a template, but the context does
900 not have template information. The optimized lookup relies
901 on having ARGS be the template arguments for both the class
902 and the function template. */
903 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
906 /* Retrieve the specialization (in the sense of [temp.spec] - a
907 specialization is either an instantiation or an explicit
908 specialization) of TMPL for the given template ARGS. If there is
909 no such specialization, return NULL_TREE. The ARGS are a vector of
910 arguments, or a vector of vectors of arguments, in the case of
911 templates with more than one level of parameters.
913 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
914 then we search for a partial specialization matching ARGS. This
915 parameter is ignored if TMPL is not a class template. */
918 retrieve_specialization (tree tmpl
, tree args
,
919 bool class_specializations_p
)
921 if (args
== error_mark_node
)
924 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
926 /* There should be as many levels of arguments as there are
927 levels of parameters. */
928 gcc_assert (TMPL_ARGS_DEPTH (args
)
929 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
931 if (optimize_specialization_lookup_p (tmpl
))
934 tree class_specialization
;
935 VEC(tree
,gc
) *methods
;
939 /* The template arguments actually apply to the containing
940 class. Find the class specialization with those
942 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
944 = retrieve_specialization (class_template
, args
,
945 /*class_specializations_p=*/false);
946 if (!class_specialization
)
948 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
949 for the specialization. */
950 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
953 /* Iterate through the methods with the indicated name, looking
954 for the one that has an instance of TMPL. */
955 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
956 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
958 tree fn
= OVL_CURRENT (fns
);
959 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
)
969 /* Class templates store their instantiations on the
970 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
971 DECL_TEMPLATE_SPECIALIZATIONS list. */
972 if (!class_specializations_p
973 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl
)) == TYPE_DECL
974 && TAGGED_TYPE_P (TREE_TYPE (tmpl
)))
975 sp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
977 sp
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
979 /* Iterate through the list until we find a matching template. */
980 while (*sp
!= NULL_TREE
)
984 if (comp_template_args (TREE_PURPOSE (spec
), args
))
986 /* Use the move-to-front heuristic to speed up future
990 *sp
= TREE_CHAIN (*sp
);
991 TREE_CHAIN (spec
) = *head
;
994 return TREE_VALUE (spec
);
996 sp
= &TREE_CHAIN (spec
);
1003 /* Like retrieve_specialization, but for local declarations. */
1006 retrieve_local_specialization (tree tmpl
)
1010 if (local_specializations
== NULL
)
1013 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1014 htab_hash_pointer (tmpl
));
1015 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1018 /* Returns nonzero iff DECL is a specialization of TMPL. */
1021 is_specialization_of (tree decl
, tree tmpl
)
1025 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1029 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1035 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1037 for (t
= TREE_TYPE (decl
);
1039 t
= CLASSTYPE_USE_TEMPLATE (t
)
1040 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1041 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1048 /* Returns nonzero iff DECL is a specialization of friend declaration
1049 FRIEND_DECL according to [temp.friend]. */
1052 is_specialization_of_friend (tree decl
, tree friend_decl
)
1054 bool need_template
= true;
1057 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1058 || TREE_CODE (decl
) == TYPE_DECL
);
1060 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1061 of a template class, we want to check if DECL is a specialization
1063 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1064 && DECL_TEMPLATE_INFO (friend_decl
)
1065 && !DECL_USE_TEMPLATE (friend_decl
))
1067 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1068 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1069 need_template
= false;
1071 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1072 && !PRIMARY_TEMPLATE_P (friend_decl
))
1073 need_template
= false;
1075 /* There is nothing to do if this is not a template friend. */
1076 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1079 if (is_specialization_of (decl
, friend_decl
))
1083 A member of a class template may be declared to be a friend of a
1084 non-template class. In this case, the corresponding member of
1085 every specialization of the class template is a friend of the
1086 class granting friendship.
1088 For example, given a template friend declaration
1090 template <class T> friend void A<T>::f();
1092 the member function below is considered a friend
1094 template <> struct A<int> {
1098 For this type of template friend, TEMPLATE_DEPTH below will be
1099 nonzero. To determine if DECL is a friend of FRIEND, we first
1100 check if the enclosing class is a specialization of another. */
1102 template_depth
= template_class_depth (DECL_CONTEXT (friend_decl
));
1104 && DECL_CLASS_SCOPE_P (decl
)
1105 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1106 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1108 /* Next, we check the members themselves. In order to handle
1109 a few tricky cases, such as when FRIEND_DECL's are
1111 template <class T> friend void A<T>::g(T t);
1112 template <class T> template <T t> friend void A<T>::h();
1116 void A<int>::g(int);
1117 template <int> void A<int>::h();
1119 we need to figure out ARGS, the template arguments from
1120 the context of DECL. This is required for template substitution
1121 of `T' in the function parameter of `g' and template parameter
1122 of `h' in the above examples. Here ARGS corresponds to `int'. */
1124 tree context
= DECL_CONTEXT (decl
);
1125 tree args
= NULL_TREE
;
1126 int current_depth
= 0;
1128 while (current_depth
< template_depth
)
1130 if (CLASSTYPE_TEMPLATE_INFO (context
))
1132 if (current_depth
== 0)
1133 args
= TYPE_TI_ARGS (context
);
1135 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1138 context
= TYPE_CONTEXT (context
);
1141 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1146 tree friend_args_type
;
1147 tree decl_args_type
;
1149 /* Make sure that both DECL and FRIEND_DECL are templates or
1151 is_template
= DECL_TEMPLATE_INFO (decl
)
1152 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1153 if (need_template
^ is_template
)
1155 else if (is_template
)
1157 /* If both are templates, check template parameter list. */
1159 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1161 if (!comp_template_parms
1162 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1166 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1169 decl_type
= TREE_TYPE (decl
);
1171 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1172 tf_none
, NULL_TREE
);
1173 if (friend_type
== error_mark_node
)
1176 /* Check if return types match. */
1177 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1180 /* Check if function parameter types match, ignoring the
1181 `this' parameter. */
1182 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1183 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1184 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1185 friend_args_type
= TREE_CHAIN (friend_args_type
);
1186 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1187 decl_args_type
= TREE_CHAIN (decl_args_type
);
1189 return compparms (decl_args_type
, friend_args_type
);
1193 /* DECL is a TYPE_DECL */
1195 tree decl_type
= TREE_TYPE (decl
);
1197 /* Make sure that both DECL and FRIEND_DECL are templates or
1200 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1201 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1203 if (need_template
^ is_template
)
1205 else if (is_template
)
1208 /* If both are templates, check the name of the two
1209 TEMPLATE_DECL's first because is_friend didn't. */
1210 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1211 != DECL_NAME (friend_decl
))
1214 /* Now check template parameter list. */
1216 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1218 return comp_template_parms
1219 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1223 return (DECL_NAME (decl
)
1224 == DECL_NAME (friend_decl
));
1230 /* Register the specialization SPEC as a specialization of TMPL with
1231 the indicated ARGS. IS_FRIEND indicates whether the specialization
1232 is actually just a friend declaration. Returns SPEC, or an
1233 equivalent prior declaration, if available. */
1236 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
)
1240 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1242 if (TREE_CODE (spec
) == FUNCTION_DECL
1243 && uses_template_parms (DECL_TI_ARGS (spec
)))
1244 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1245 register it; we want the corresponding TEMPLATE_DECL instead.
1246 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1247 the more obvious `uses_template_parms (spec)' to avoid problems
1248 with default function arguments. In particular, given
1249 something like this:
1251 template <class T> void f(T t1, T t = T())
1253 the default argument expression is not substituted for in an
1254 instantiation unless and until it is actually needed. */
1257 fn
= retrieve_specialization (tmpl
, args
,
1258 /*class_specializations_p=*/false);
1259 /* We can sometimes try to re-register a specialization that we've
1260 already got. In particular, regenerate_decl_from_template calls
1261 duplicate_decls which will update the specialization list. But,
1262 we'll still get called again here anyhow. It's more convenient
1263 to simply allow this than to try to prevent it. */
1266 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1268 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1271 || DECL_EXPLICIT_INSTANTIATION (fn
))
1273 error ("specialization of %qD after instantiation",
1275 return error_mark_node
;
1280 /* This situation should occur only if the first
1281 specialization is an implicit instantiation, the
1282 second is an explicit specialization, and the
1283 implicit instantiation has not yet been used. That
1284 situation can occur if we have implicitly
1285 instantiated a member function and then specialized
1288 We can also wind up here if a friend declaration that
1289 looked like an instantiation turns out to be a
1292 template <class T> void foo(T);
1293 class S { friend void foo<>(int) };
1294 template <> void foo(int);
1296 We transform the existing DECL in place so that any
1297 pointers to it become pointers to the updated
1300 If there was a definition for the template, but not
1301 for the specialization, we want this to look as if
1302 there were no definition, and vice versa. */
1303 DECL_INITIAL (fn
) = NULL_TREE
;
1304 duplicate_decls (spec
, fn
, is_friend
);
1305 /* The call to duplicate_decls will have applied
1308 An explicit specialization of a function template
1309 is inline only if it is explicitly declared to be,
1310 and independently of whether its function template
1313 to the primary function; now copy the inline bits to
1314 the various clones. */
1315 FOR_EACH_CLONE (clone
, fn
)
1316 DECL_DECLARED_INLINE_P (clone
)
1317 = DECL_DECLARED_INLINE_P (fn
);
1318 check_specialization_namespace (fn
);
1323 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1325 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1326 /* Dup decl failed, but this is a new definition. Set the
1327 line number so any errors match this new
1329 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1335 /* A specialization must be declared in the same namespace as the
1336 template it is specializing. */
1337 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1338 && !check_specialization_namespace (tmpl
))
1339 DECL_CONTEXT (spec
) = FROB_CONTEXT (decl_namespace_context (tmpl
));
1341 if (!optimize_specialization_lookup_p (tmpl
))
1342 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
1343 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
1348 /* Unregister the specialization SPEC as a specialization of TMPL.
1349 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1350 if the SPEC was listed as a specialization of TMPL. */
1353 reregister_specialization (tree spec
, tree tmpl
, tree new_spec
)
1357 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
1359 s
= &TREE_CHAIN (*s
))
1360 if (TREE_VALUE (*s
) == spec
)
1363 *s
= TREE_CHAIN (*s
);
1365 TREE_VALUE (*s
) = new_spec
;
1372 /* Compare an entry in the local specializations hash table P1 (which
1373 is really a pointer to a TREE_LIST) with P2 (which is really a
1377 eq_local_specializations (const void *p1
, const void *p2
)
1379 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1382 /* Hash P1, an entry in the local specializations table. */
1385 hash_local_specialization (const void* p1
)
1387 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1390 /* Like register_specialization, but for local declarations. We are
1391 registering SPEC, an instantiation of TMPL. */
1394 register_local_specialization (tree spec
, tree tmpl
)
1398 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1399 htab_hash_pointer (tmpl
), INSERT
);
1400 *slot
= build_tree_list (spec
, tmpl
);
1403 /* TYPE is a class type. Returns true if TYPE is an explicitly
1404 specialized class. */
1407 explicit_class_specialization_p (tree type
)
1409 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1411 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1414 /* Print the list of candidate FNS in an error message. */
1417 print_candidates (tree fns
)
1421 const char *str
= "candidates are:";
1423 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
1427 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
1428 error ("%s %+#D", str
, OVL_CURRENT (f
));
1433 /* Returns the template (one of the functions given by TEMPLATE_ID)
1434 which can be specialized to match the indicated DECL with the
1435 explicit template args given in TEMPLATE_ID. The DECL may be
1436 NULL_TREE if none is available. In that case, the functions in
1437 TEMPLATE_ID are non-members.
1439 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1440 specialization of a member template.
1442 The TEMPLATE_COUNT is the number of references to qualifying
1443 template classes that appeared in the name of the function. See
1444 check_explicit_specialization for a more accurate description.
1446 TSK indicates what kind of template declaration (if any) is being
1447 declared. TSK_TEMPLATE indicates that the declaration given by
1448 DECL, though a FUNCTION_DECL, has template parameters, and is
1449 therefore a template function.
1451 The template args (those explicitly specified and those deduced)
1452 are output in a newly created vector *TARGS_OUT.
1454 If it is impossible to determine the result, an error message is
1455 issued. The error_mark_node is returned to indicate failure. */
1458 determine_specialization (tree template_id
,
1461 int need_member_template
,
1467 tree explicit_targs
;
1468 tree candidates
= NULL_TREE
;
1469 /* A TREE_LIST of templates of which DECL may be a specialization.
1470 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1471 corresponding TREE_PURPOSE is the set of template arguments that,
1472 when used to instantiate the template, would produce a function
1473 with the signature of DECL. */
1474 tree templates
= NULL_TREE
;
1476 struct cp_binding_level
*b
;
1478 *targs_out
= NULL_TREE
;
1480 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1481 return error_mark_node
;
1483 fns
= TREE_OPERAND (template_id
, 0);
1484 explicit_targs
= TREE_OPERAND (template_id
, 1);
1486 if (fns
== error_mark_node
)
1487 return error_mark_node
;
1489 /* Check for baselinks. */
1490 if (BASELINK_P (fns
))
1491 fns
= BASELINK_FUNCTIONS (fns
);
1493 if (!is_overloaded_fn (fns
))
1495 error ("%qD is not a function template", fns
);
1496 return error_mark_node
;
1499 /* Count the number of template headers specified for this
1502 for (b
= current_binding_level
;
1503 b
->kind
== sk_template_parms
;
1507 for (; fns
; fns
= OVL_NEXT (fns
))
1509 tree fn
= OVL_CURRENT (fns
);
1511 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1513 tree decl_arg_types
;
1516 /* In case of explicit specialization, we need to check if
1517 the number of template headers appearing in the specialization
1518 is correct. This is usually done in check_explicit_specialization,
1519 but the check done there cannot be exhaustive when specializing
1520 member functions. Consider the following code:
1522 template <> void A<int>::f(int);
1523 template <> template <> void A<int>::f(int);
1525 Assuming that A<int> is not itself an explicit specialization
1526 already, the first line specializes "f" which is a non-template
1527 member function, whilst the second line specializes "f" which
1528 is a template member function. So both lines are syntactically
1529 correct, and check_explicit_specialization does not reject
1532 Here, we can do better, as we are matching the specialization
1533 against the declarations. We count the number of template
1534 headers, and we check if they match TEMPLATE_COUNT + 1
1535 (TEMPLATE_COUNT is the number of qualifying template classes,
1536 plus there must be another header for the member template
1539 Notice that if header_count is zero, this is not a
1540 specialization but rather a template instantiation, so there
1541 is no check we can perform here. */
1542 if (header_count
&& header_count
!= template_count
+ 1)
1545 /* Check that the number of template arguments at the
1546 innermost level for DECL is the same as for FN. */
1547 if (current_binding_level
->kind
== sk_template_parms
1548 && !current_binding_level
->explicit_spec_p
1549 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1550 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1551 (current_template_parms
))))
1554 /* DECL might be a specialization of FN. */
1555 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1556 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1558 /* For a non-static member function, we need to make sure
1559 that the const qualification is the same. Since
1560 get_bindings does not try to merge the "this" parameter,
1561 we must do the comparison explicitly. */
1562 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1563 && !same_type_p (TREE_VALUE (fn_arg_types
),
1564 TREE_VALUE (decl_arg_types
)))
1567 /* Skip the "this" parameter and, for constructors of
1568 classes with virtual bases, the VTT parameter. A
1569 full specialization of a constructor will have a VTT
1570 parameter, but a template never will. */
1572 = skip_artificial_parms_for (decl
, decl_arg_types
);
1574 = skip_artificial_parms_for (fn
, fn_arg_types
);
1576 /* Check that the number of function parameters matches.
1578 template <class T> void f(int i = 0);
1579 template <> void f<int>();
1580 The specialization f<int> is invalid but is not caught
1581 by get_bindings below. */
1582 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1585 /* Function templates cannot be specializations; there are
1586 no partial specializations of functions. Therefore, if
1587 the type of DECL does not match FN, there is no
1589 if (tsk
== tsk_template
)
1591 if (compparms (fn_arg_types
, decl_arg_types
))
1592 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1596 /* See whether this function might be a specialization of this
1598 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1601 /* We cannot deduce template arguments that when used to
1602 specialize TMPL will produce DECL. */
1605 /* Save this template, and the arguments deduced. */
1606 templates
= tree_cons (targs
, fn
, templates
);
1608 else if (need_member_template
)
1609 /* FN is an ordinary member function, and we need a
1610 specialization of a member template. */
1612 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1613 /* We can get IDENTIFIER_NODEs here in certain erroneous
1616 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1617 /* This is just an ordinary non-member function. Nothing can
1618 be a specialization of that. */
1620 else if (DECL_ARTIFICIAL (fn
))
1621 /* Cannot specialize functions that are created implicitly. */
1625 tree decl_arg_types
;
1627 /* This is an ordinary member function. However, since
1628 we're here, we can assume it's enclosing class is a
1629 template class. For example,
1631 template <typename T> struct S { void f(); };
1632 template <> void S<int>::f() {}
1634 Here, S<int>::f is a non-template, but S<int> is a
1635 template class. If FN has the same type as DECL, we
1636 might be in business. */
1638 if (!DECL_TEMPLATE_INFO (fn
))
1639 /* Its enclosing class is an explicit specialization
1640 of a template class. This is not a candidate. */
1643 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1644 TREE_TYPE (TREE_TYPE (fn
))))
1645 /* The return types differ. */
1648 /* Adjust the type of DECL in case FN is a static member. */
1649 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1650 if (DECL_STATIC_FUNCTION_P (fn
)
1651 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1652 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1654 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1657 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1661 if (templates
&& TREE_CHAIN (templates
))
1667 It is possible for a specialization with a given function
1668 signature to be instantiated from more than one function
1669 template. In such cases, explicit specification of the
1670 template arguments must be used to uniquely identify the
1671 function template specialization being specialized.
1673 Note that here, there's no suggestion that we're supposed to
1674 determine which of the candidate templates is most
1675 specialized. However, we, also have:
1679 Partial ordering of overloaded function template
1680 declarations is used in the following contexts to select
1681 the function template to which a function template
1682 specialization refers:
1684 -- when an explicit specialization refers to a function
1687 So, we do use the partial ordering rules, at least for now.
1688 This extension can only serve to make invalid programs valid,
1689 so it's safe. And, there is strong anecdotal evidence that
1690 the committee intended the partial ordering rules to apply;
1691 the EDG front end has that behavior, and John Spicer claims
1692 that the committee simply forgot to delete the wording in
1693 [temp.expl.spec]. */
1694 tree tmpl
= most_specialized_instantiation (templates
);
1695 if (tmpl
!= error_mark_node
)
1698 TREE_CHAIN (templates
) = NULL_TREE
;
1702 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1704 error ("template-id %qD for %q+D does not match any template "
1705 "declaration", template_id
, decl
);
1706 return error_mark_node
;
1708 else if ((templates
&& TREE_CHAIN (templates
))
1709 || (candidates
&& TREE_CHAIN (candidates
))
1710 || (templates
&& candidates
))
1712 error ("ambiguous template specialization %qD for %q+D",
1714 chainon (candidates
, templates
);
1715 print_candidates (candidates
);
1716 return error_mark_node
;
1719 /* We have one, and exactly one, match. */
1722 tree fn
= TREE_VALUE (candidates
);
1723 /* DECL is a re-declaration of a template function. */
1724 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1726 /* It was a specialization of an ordinary member function in a
1728 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
1729 return DECL_TI_TEMPLATE (fn
);
1732 /* It was a specialization of a template. */
1733 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1734 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1736 *targs_out
= copy_node (targs
);
1737 SET_TMPL_ARGS_LEVEL (*targs_out
,
1738 TMPL_ARGS_DEPTH (*targs_out
),
1739 TREE_PURPOSE (templates
));
1742 *targs_out
= TREE_PURPOSE (templates
);
1743 return TREE_VALUE (templates
);
1746 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1747 but with the default argument values filled in from those in the
1751 copy_default_args_to_explicit_spec_1 (tree spec_types
,
1754 tree new_spec_types
;
1759 if (spec_types
== void_list_node
)
1760 return void_list_node
;
1762 /* Substitute into the rest of the list. */
1764 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1765 TREE_CHAIN (tmpl_types
));
1767 /* Add the default argument for this parameter. */
1768 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1769 TREE_VALUE (spec_types
),
1773 /* DECL is an explicit specialization. Replicate default arguments
1774 from the template it specializes. (That way, code like:
1776 template <class T> void f(T = 3);
1777 template <> void f(double);
1780 works, as required.) An alternative approach would be to look up
1781 the correct default arguments at the call-site, but this approach
1782 is consistent with how implicit instantiations are handled. */
1785 copy_default_args_to_explicit_spec (tree decl
)
1790 tree new_spec_types
;
1794 tree object_type
= NULL_TREE
;
1795 tree in_charge
= NULL_TREE
;
1796 tree vtt
= NULL_TREE
;
1798 /* See if there's anything we need to do. */
1799 tmpl
= DECL_TI_TEMPLATE (decl
);
1800 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1801 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1802 if (TREE_PURPOSE (t
))
1807 old_type
= TREE_TYPE (decl
);
1808 spec_types
= TYPE_ARG_TYPES (old_type
);
1810 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1812 /* Remove the this pointer, but remember the object's type for
1814 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1815 spec_types
= TREE_CHAIN (spec_types
);
1816 tmpl_types
= TREE_CHAIN (tmpl_types
);
1818 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1820 /* DECL may contain more parameters than TMPL due to the extra
1821 in-charge parameter in constructors and destructors. */
1822 in_charge
= spec_types
;
1823 spec_types
= TREE_CHAIN (spec_types
);
1825 if (DECL_HAS_VTT_PARM_P (decl
))
1828 spec_types
= TREE_CHAIN (spec_types
);
1832 /* Compute the merged default arguments. */
1834 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1836 /* Compute the new FUNCTION_TYPE. */
1840 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1845 /* Put the in-charge parameter back. */
1846 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1847 TREE_VALUE (in_charge
),
1850 new_type
= build_method_type_directly (object_type
,
1851 TREE_TYPE (old_type
),
1855 new_type
= build_function_type (TREE_TYPE (old_type
),
1857 new_type
= cp_build_type_attribute_variant (new_type
,
1858 TYPE_ATTRIBUTES (old_type
));
1859 new_type
= build_exception_variant (new_type
,
1860 TYPE_RAISES_EXCEPTIONS (old_type
));
1861 TREE_TYPE (decl
) = new_type
;
1864 /* Check to see if the function just declared, as indicated in
1865 DECLARATOR, and in DECL, is a specialization of a function
1866 template. We may also discover that the declaration is an explicit
1867 instantiation at this point.
1869 Returns DECL, or an equivalent declaration that should be used
1870 instead if all goes well. Issues an error message if something is
1871 amiss. Returns error_mark_node if the error is not easily
1874 FLAGS is a bitmask consisting of the following flags:
1876 2: The function has a definition.
1877 4: The function is a friend.
1879 The TEMPLATE_COUNT is the number of references to qualifying
1880 template classes that appeared in the name of the function. For
1883 template <class T> struct S { void f(); };
1886 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1887 classes are not counted in the TEMPLATE_COUNT, so that in
1889 template <class T> struct S {};
1890 template <> struct S<int> { void f(); }
1891 template <> void S<int>::f();
1893 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1894 invalid; there should be no template <>.)
1896 If the function is a specialization, it is marked as such via
1897 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1898 is set up correctly, and it is added to the list of specializations
1899 for that template. */
1902 check_explicit_specialization (tree declarator
,
1907 int have_def
= flags
& 2;
1908 int is_friend
= flags
& 4;
1909 int specialization
= 0;
1910 int explicit_instantiation
= 0;
1911 int member_specialization
= 0;
1912 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1913 tree dname
= DECL_NAME (decl
);
1918 if (!processing_specialization
)
1921 tsk
= tsk_excessive_parms
;
1924 tsk
= current_tmpl_spec_kind (template_count
);
1929 if (processing_specialization
)
1932 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1934 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1937 /* This could be something like:
1939 template <class T> void f(T);
1940 class S { friend void f<>(int); } */
1944 /* This case handles bogus declarations like template <>
1945 template <class T> void f<int>(); */
1947 error ("template-id %qD in declaration of primary template",
1954 case tsk_invalid_member_spec
:
1955 /* The error has already been reported in
1956 check_specialization_scope. */
1957 return error_mark_node
;
1959 case tsk_invalid_expl_inst
:
1960 error ("template parameter list used in explicit instantiation");
1966 error ("definition provided for explicit instantiation");
1968 explicit_instantiation
= 1;
1971 case tsk_excessive_parms
:
1972 case tsk_insufficient_parms
:
1973 if (tsk
== tsk_excessive_parms
)
1974 error ("too many template parameter lists in declaration of %qD",
1976 else if (template_header_count
)
1977 error("too few template parameter lists in declaration of %qD", decl
);
1979 error("explicit specialization of %qD must be introduced by "
1980 "%<template <>%>", decl
);
1984 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1986 member_specialization
= 1;
1992 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1994 /* This case handles bogus declarations like template <>
1995 template <class T> void f<int>(); */
1997 if (uses_template_parms (declarator
))
1998 error ("function template partial specialization %qD "
1999 "is not allowed", declarator
);
2001 error ("template-id %qD in declaration of primary template",
2006 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2007 /* This is a specialization of a member template, without
2008 specialization the containing class. Something like:
2010 template <class T> struct S {
2011 template <class U> void f (U);
2013 template <> template <class U> void S<int>::f(U) {}
2015 That's a specialization -- but of the entire template. */
2023 if (specialization
|| member_specialization
)
2025 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2026 for (; t
; t
= TREE_CHAIN (t
))
2027 if (TREE_PURPOSE (t
))
2029 permerror (input_location
,
2030 "default argument specified in explicit specialization");
2035 if (specialization
|| member_specialization
|| explicit_instantiation
)
2037 tree tmpl
= NULL_TREE
;
2038 tree targs
= NULL_TREE
;
2040 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2041 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2045 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2050 /* If there is no class context, the explicit instantiation
2051 must be at namespace scope. */
2052 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2054 /* Find the namespace binding, using the declaration
2056 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2058 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2060 error ("%qD is not a template function", dname
);
2061 fns
= error_mark_node
;
2065 tree fn
= OVL_CURRENT (fns
);
2066 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2067 CP_DECL_CONTEXT (fn
)))
2068 error ("%qD is not declared in %qD",
2069 decl
, current_namespace
);
2073 declarator
= lookup_template_function (fns
, NULL_TREE
);
2076 if (declarator
== error_mark_node
)
2077 return error_mark_node
;
2079 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2081 if (!explicit_instantiation
)
2082 /* A specialization in class scope. This is invalid,
2083 but the error will already have been flagged by
2084 check_specialization_scope. */
2085 return error_mark_node
;
2088 /* It's not valid to write an explicit instantiation in
2091 class C { template void f(); }
2093 This case is caught by the parser. However, on
2096 template class C { void f(); };
2098 (which is invalid) we can get here. The error will be
2105 else if (ctype
!= NULL_TREE
2106 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2109 /* Find the list of functions in ctype that have the same
2110 name as the declared function. */
2111 tree name
= TREE_OPERAND (declarator
, 0);
2112 tree fns
= NULL_TREE
;
2115 if (constructor_name_p (name
, ctype
))
2117 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2119 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2120 : !CLASSTYPE_DESTRUCTORS (ctype
))
2122 /* From [temp.expl.spec]:
2124 If such an explicit specialization for the member
2125 of a class template names an implicitly-declared
2126 special member function (clause _special_), the
2127 program is ill-formed.
2129 Similar language is found in [temp.explicit]. */
2130 error ("specialization of implicitly-declared special member function");
2131 return error_mark_node
;
2134 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2137 if (!DECL_CONV_FN_P (decl
))
2139 idx
= lookup_fnfields_1 (ctype
, name
);
2141 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2145 VEC(tree
,gc
) *methods
;
2148 /* For a type-conversion operator, we cannot do a
2149 name-based lookup. We might be looking for `operator
2150 int' which will be a specialization of `operator T'.
2151 So, we find *all* the conversion operators, and then
2152 select from them. */
2155 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2157 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2158 VEC_iterate (tree
, methods
, idx
, ovl
);
2161 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2162 /* There are no more conversion functions. */
2165 /* Glue all these conversion functions together
2166 with those we already have. */
2167 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2168 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2172 if (fns
== NULL_TREE
)
2174 error ("no member function %qD declared in %qT", name
, ctype
);
2175 return error_mark_node
;
2178 TREE_OPERAND (declarator
, 0) = fns
;
2181 /* Figure out what exactly is being specialized at this point.
2182 Note that for an explicit instantiation, even one for a
2183 member function, we cannot tell apriori whether the
2184 instantiation is for a member template, or just a member
2185 function of a template class. Even if a member template is
2186 being instantiated, the member template arguments may be
2187 elided if they can be deduced from the rest of the
2189 tmpl
= determine_specialization (declarator
, decl
,
2191 member_specialization
,
2195 if (!tmpl
|| tmpl
== error_mark_node
)
2196 /* We couldn't figure out what this declaration was
2198 return error_mark_node
;
2201 tree gen_tmpl
= most_general_template (tmpl
);
2203 if (explicit_instantiation
)
2205 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2206 is done by do_decl_instantiation later. */
2208 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2209 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2211 if (arg_depth
> parm_depth
)
2213 /* If TMPL is not the most general template (for
2214 example, if TMPL is a friend template that is
2215 injected into namespace scope), then there will
2216 be too many levels of TARGS. Remove some of them
2221 new_targs
= make_tree_vec (parm_depth
);
2222 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2223 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2224 = TREE_VEC_ELT (targs
, i
);
2228 return instantiate_template (tmpl
, targs
, tf_error
);
2231 /* If we thought that the DECL was a member function, but it
2232 turns out to be specializing a static member function,
2233 make DECL a static member function as well. */
2234 if (DECL_STATIC_FUNCTION_P (tmpl
)
2235 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2236 revert_static_member_fn (decl
);
2238 /* If this is a specialization of a member template of a
2239 template class, we want to return the TEMPLATE_DECL, not
2240 the specialization of it. */
2241 if (tsk
== tsk_template
)
2243 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2244 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
2247 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2248 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl
))
2249 = DECL_SOURCE_LOCATION (decl
);
2250 /* We want to use the argument list specified in the
2251 definition, not in the original declaration. */
2252 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl
))
2253 = DECL_ARGUMENTS (decl
);
2258 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2259 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
2261 /* Inherit default function arguments from the template
2262 DECL is specializing. */
2263 copy_default_args_to_explicit_spec (decl
);
2265 /* This specialization has the same protection as the
2266 template it specializes. */
2267 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2268 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2270 /* 7.1.1-1 [dcl.stc]
2272 A storage-class-specifier shall not be specified in an
2273 explicit specialization...
2275 The parser rejects these, so unless action is taken here,
2276 explicit function specializations will always appear with
2279 The action recommended by the C++ CWG in response to C++
2280 defect report 605 is to make the storage class and linkage
2281 of the explicit specialization match the templated function:
2283 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2285 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2287 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2288 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2290 /* This specialization has the same linkage and visibility as
2291 the function template it specializes. */
2292 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2293 if (! TREE_PUBLIC (decl
))
2295 DECL_INTERFACE_KNOWN (decl
) = 1;
2296 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2298 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2299 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2301 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2302 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2306 /* If DECL is a friend declaration, declared using an
2307 unqualified name, the namespace associated with DECL may
2308 have been set incorrectly. For example, in:
2310 template <typename T> void f(T);
2312 struct S { friend void f<int>(int); }
2315 we will have set the DECL_CONTEXT for the friend
2316 declaration to N, rather than to the global namespace. */
2317 if (DECL_NAMESPACE_SCOPE_P (decl
))
2318 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2320 if (is_friend
&& !have_def
)
2321 /* This is not really a declaration of a specialization.
2322 It's just the name of an instantiation. But, it's not
2323 a request for an instantiation, either. */
2324 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2325 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2326 /* This is indeed a specialization. In case of constructors
2327 and destructors, we need in-charge and not-in-charge
2328 versions in V3 ABI. */
2329 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2331 /* Register this specialization so that we can find it
2333 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
);
2340 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2341 parameters. These are represented in the same format used for
2342 DECL_TEMPLATE_PARMS. */
2345 comp_template_parms (const_tree parms1
, const_tree parms2
)
2350 if (parms1
== parms2
)
2353 for (p1
= parms1
, p2
= parms2
;
2354 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2355 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2357 tree t1
= TREE_VALUE (p1
);
2358 tree t2
= TREE_VALUE (p2
);
2361 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2362 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2364 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2367 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2369 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2370 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2372 /* If either of the template parameters are invalid, assume
2373 they match for the sake of error recovery. */
2374 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2377 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2380 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2381 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2382 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2384 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2389 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2390 /* One set of parameters has more parameters lists than the
2397 /* Determine whether PARM is a parameter pack. */
2399 template_parameter_pack_p (const_tree parm
)
2401 /* Determine if we have a non-type template parameter pack. */
2402 if (TREE_CODE (parm
) == PARM_DECL
)
2403 return (DECL_TEMPLATE_PARM_P (parm
)
2404 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2406 /* If this is a list of template parameters, we could get a
2407 TYPE_DECL or a TEMPLATE_DECL. */
2408 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2409 parm
= TREE_TYPE (parm
);
2411 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2412 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2413 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2416 /* Determine whether ARGS describes a variadic template args list,
2417 i.e., one that is terminated by a template argument pack. */
2419 template_args_variadic_p (tree args
)
2424 if (args
== NULL_TREE
)
2427 args
= INNERMOST_TEMPLATE_ARGS (args
);
2428 nargs
= TREE_VEC_LENGTH (args
);
2433 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2435 return ARGUMENT_PACK_P (last_parm
);
2438 /* Generate a new name for the parameter pack name NAME (an
2439 IDENTIFIER_NODE) that incorporates its */
2441 make_ith_pack_parameter_name (tree name
, int i
)
2443 /* Munge the name to include the parameter index. */
2447 sprintf(numbuf
, "%i", i
);
2448 newname
= (char*)alloca (IDENTIFIER_LENGTH (name
) + strlen(numbuf
) + 2);
2449 sprintf(newname
, "%s#%i", IDENTIFIER_POINTER (name
), i
);
2450 return get_identifier (newname
);
2453 /* Structure used to track the progress of find_parameter_packs_r. */
2454 struct find_parameter_pack_data
2456 /* TREE_LIST that will contain all of the parameter packs found by
2458 tree
* parameter_packs
;
2460 /* Set of AST nodes that have been visited by the traversal. */
2461 struct pointer_set_t
*visited
;
2464 /* Identifies all of the argument packs that occur in a template
2465 argument and appends them to the TREE_LIST inside DATA, which is a
2466 find_parameter_pack_data structure. This is a subroutine of
2467 make_pack_expansion and uses_parameter_packs. */
2469 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2472 struct find_parameter_pack_data
* ppd
=
2473 (struct find_parameter_pack_data
*)data
;
2474 bool parameter_pack_p
= false;
2476 /* Identify whether this is a parameter pack or not. */
2477 switch (TREE_CODE (t
))
2479 case TEMPLATE_PARM_INDEX
:
2480 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2481 parameter_pack_p
= true;
2484 case TEMPLATE_TYPE_PARM
:
2485 case TEMPLATE_TEMPLATE_PARM
:
2486 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2487 parameter_pack_p
= true;
2491 if (FUNCTION_PARAMETER_PACK_P (t
))
2493 /* We don't want to walk into the type of a PARM_DECL,
2494 because we don't want to see the type parameter pack. */
2496 parameter_pack_p
= true;
2501 /* Not a parameter pack. */
2505 if (parameter_pack_p
)
2507 /* Add this parameter pack to the list. */
2508 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2512 cp_walk_tree (&TYPE_CONTEXT (t
),
2513 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2515 /* This switch statement will return immediately if we don't find a
2517 switch (TREE_CODE (t
))
2519 case TEMPLATE_PARM_INDEX
:
2522 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2523 /* Check the template itself. */
2524 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2525 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2526 /* Check the template arguments. */
2527 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2532 case TEMPLATE_TYPE_PARM
:
2533 case TEMPLATE_TEMPLATE_PARM
:
2540 if (TYPE_PTRMEMFUNC_P (t
))
2546 if (TYPE_TEMPLATE_INFO (t
))
2547 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
2548 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2554 cp_walk_tree (&TREE_TYPE (t
),
2555 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2559 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
2564 case TYPE_PACK_EXPANSION
:
2565 case EXPR_PACK_EXPANSION
:
2570 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
2575 case IDENTIFIER_NODE
:
2576 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
2581 /* If T is a tree list node whose purpose field is an error_mark_node,
2582 T actually contains the tokens for an lock attribute argument list in
2583 its value field. (See cp_parser_save_attribute_arg_list in cp/parser.c.)
2584 In that case, don't try to walk the tree value field as it is not a
2587 if (TREE_PURPOSE (t
) == error_mark_node
)
2598 /* Determines if the expression or type T uses any parameter packs. */
2600 uses_parameter_packs (tree t
)
2602 tree parameter_packs
= NULL_TREE
;
2603 struct find_parameter_pack_data ppd
;
2604 ppd
.parameter_packs
= ¶meter_packs
;
2605 ppd
.visited
= pointer_set_create ();
2606 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2607 pointer_set_destroy (ppd
.visited
);
2608 return parameter_packs
!= NULL_TREE
;
2611 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2612 representation a base-class initializer into a parameter pack
2613 expansion. If all goes well, the resulting node will be an
2614 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2617 make_pack_expansion (tree arg
)
2620 tree parameter_packs
= NULL_TREE
;
2621 bool for_types
= false;
2622 struct find_parameter_pack_data ppd
;
2624 if (!arg
|| arg
== error_mark_node
)
2627 if (TREE_CODE (arg
) == TREE_LIST
)
2629 /* The only time we will see a TREE_LIST here is for a base
2630 class initializer. In this case, the TREE_PURPOSE will be a
2631 _TYPE node (representing the base class expansion we're
2632 initializing) and the TREE_VALUE will be a TREE_LIST
2633 containing the initialization arguments.
2635 The resulting expansion looks somewhat different from most
2636 expansions. Rather than returning just one _EXPANSION, we
2637 return a TREE_LIST whose TREE_PURPOSE is a
2638 TYPE_PACK_EXPANSION containing the bases that will be
2639 initialized. The TREE_VALUE will be identical to the
2640 original TREE_VALUE, which is a list of arguments that will
2641 be passed to each base. We do not introduce any new pack
2642 expansion nodes into the TREE_VALUE (although it is possible
2643 that some already exist), because the TREE_PURPOSE and
2644 TREE_VALUE all need to be expanded together with the same
2645 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2646 resulting TREE_PURPOSE will mention the parameter packs in
2647 both the bases and the arguments to the bases. */
2650 tree parameter_packs
= NULL_TREE
;
2652 /* Determine which parameter packs will be used by the base
2654 ppd
.visited
= pointer_set_create ();
2655 ppd
.parameter_packs
= ¶meter_packs
;
2656 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
2659 if (parameter_packs
== NULL_TREE
)
2661 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
2662 pointer_set_destroy (ppd
.visited
);
2663 return error_mark_node
;
2666 if (TREE_VALUE (arg
) != void_type_node
)
2668 /* Collect the sets of parameter packs used in each of the
2669 initialization arguments. */
2670 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
2672 /* Determine which parameter packs will be expanded in this
2674 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
2679 pointer_set_destroy (ppd
.visited
);
2681 /* Create the pack expansion type for the base type. */
2682 purpose
= make_node (TYPE_PACK_EXPANSION
);
2683 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
2684 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
2686 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2687 they will rarely be compared to anything. */
2688 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
2690 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
2693 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
2696 /* Build the PACK_EXPANSION_* node. */
2697 result
= make_node (for_types
? TYPE_PACK_EXPANSION
: EXPR_PACK_EXPANSION
);
2698 SET_PACK_EXPANSION_PATTERN (result
, arg
);
2699 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
2701 /* Propagate type and const-expression information. */
2702 TREE_TYPE (result
) = TREE_TYPE (arg
);
2703 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
2706 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2707 they will rarely be compared to anything. */
2708 SET_TYPE_STRUCTURAL_EQUALITY (result
);
2710 /* Determine which parameter packs will be expanded. */
2711 ppd
.parameter_packs
= ¶meter_packs
;
2712 ppd
.visited
= pointer_set_create ();
2713 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2714 pointer_set_destroy (ppd
.visited
);
2716 /* Make sure we found some parameter packs. */
2717 if (parameter_packs
== NULL_TREE
)
2720 error ("expansion pattern %<%T%> contains no argument packs", arg
);
2722 error ("expansion pattern %<%E%> contains no argument packs", arg
);
2723 return error_mark_node
;
2725 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
2730 /* Checks T for any "bare" parameter packs, which have not yet been
2731 expanded, and issues an error if any are found. This operation can
2732 only be done on full expressions or types (e.g., an expression
2733 statement, "if" condition, etc.), because we could have expressions like:
2735 foo(f(g(h(args)))...)
2737 where "args" is a parameter pack. check_for_bare_parameter_packs
2738 should not be called for the subexpressions args, h(args),
2739 g(h(args)), or f(g(h(args))), because we would produce erroneous
2742 Returns TRUE and emits an error if there were bare parameter packs,
2743 returns FALSE otherwise. */
2745 check_for_bare_parameter_packs (tree t
)
2747 tree parameter_packs
= NULL_TREE
;
2748 struct find_parameter_pack_data ppd
;
2750 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
2753 if (TREE_CODE (t
) == TYPE_DECL
)
2756 ppd
.parameter_packs
= ¶meter_packs
;
2757 ppd
.visited
= pointer_set_create ();
2758 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2759 pointer_set_destroy (ppd
.visited
);
2761 if (parameter_packs
)
2763 error ("parameter packs not expanded with %<...%>:");
2764 while (parameter_packs
)
2766 tree pack
= TREE_VALUE (parameter_packs
);
2767 tree name
= NULL_TREE
;
2769 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
2770 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
2771 name
= TYPE_NAME (pack
);
2772 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
2773 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
2775 name
= DECL_NAME (pack
);
2778 inform (input_location
, " %qD", name
);
2780 inform (input_location
, " <anonymous>");
2782 parameter_packs
= TREE_CHAIN (parameter_packs
);
2791 /* Expand any parameter packs that occur in the template arguments in
2794 expand_template_argument_pack (tree args
)
2796 tree result_args
= NULL_TREE
;
2797 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
2798 int num_result_args
= -1;
2800 /* First, determine if we need to expand anything, and the number of
2801 slots we'll need. */
2802 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2804 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2805 if (ARGUMENT_PACK_P (arg
))
2807 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
2808 if (num_result_args
< 0)
2809 num_result_args
= in_arg
+ num_packed
;
2811 num_result_args
+= num_packed
;
2815 if (num_result_args
>= 0)
2820 /* If no expansion is necessary, we're done. */
2821 if (num_result_args
< 0)
2824 /* Expand arguments. */
2825 result_args
= make_tree_vec (num_result_args
);
2826 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2828 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2829 if (ARGUMENT_PACK_P (arg
))
2831 tree packed
= ARGUMENT_PACK_ARGS (arg
);
2832 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
2833 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
2834 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
2838 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
2846 /* Checks if DECL shadows a template parameter.
2848 [temp.local]: A template-parameter shall not be redeclared within its
2849 scope (including nested scopes).
2851 Emits an error and returns TRUE if the DECL shadows a parameter,
2852 returns FALSE otherwise. */
2855 check_template_shadow (tree decl
)
2859 /* If we're not in a template, we can't possibly shadow a template
2861 if (!current_template_parms
)
2864 /* Figure out what we're shadowing. */
2865 if (TREE_CODE (decl
) == OVERLOAD
)
2866 decl
= OVL_CURRENT (decl
);
2867 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
2869 /* If there's no previous binding for this name, we're not shadowing
2870 anything, let alone a template parameter. */
2874 /* If we're not shadowing a template parameter, we're done. Note
2875 that OLDDECL might be an OVERLOAD (or perhaps even an
2876 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2878 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
2881 /* We check for decl != olddecl to avoid bogus errors for using a
2882 name inside a class. We check TPFI to avoid duplicate errors for
2883 inline member templates. */
2885 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
2888 error ("declaration of %q+#D", decl
);
2889 error (" shadows template parm %q+#D", olddecl
);
2893 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2894 ORIG_LEVEL, DECL, and TYPE. */
2897 build_template_parm_index (int index
,
2903 tree t
= make_node (TEMPLATE_PARM_INDEX
);
2904 TEMPLATE_PARM_IDX (t
) = index
;
2905 TEMPLATE_PARM_LEVEL (t
) = level
;
2906 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
2907 TEMPLATE_PARM_DECL (t
) = decl
;
2908 TREE_TYPE (t
) = type
;
2909 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
2910 TREE_READONLY (t
) = TREE_READONLY (decl
);
2915 /* Find the canonical type parameter for the given template type
2916 parameter. Returns the canonical type parameter, which may be TYPE
2917 if no such parameter existed. */
2919 canonical_type_parameter (tree type
)
2922 int idx
= TEMPLATE_TYPE_IDX (type
);
2923 if (!canonical_template_parms
)
2924 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
2926 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
2927 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
2929 list
= VEC_index (tree
, canonical_template_parms
, idx
);
2930 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
2931 list
= TREE_CHAIN (list
);
2934 return TREE_VALUE (list
);
2937 VEC_replace(tree
, canonical_template_parms
, idx
,
2938 tree_cons (NULL_TREE
, type
,
2939 VEC_index (tree
, canonical_template_parms
, idx
)));
2944 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2945 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2946 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2947 new one is created. */
2950 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
2951 tsubst_flags_t complain
)
2953 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
2954 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
2955 != TEMPLATE_PARM_LEVEL (index
) - levels
))
2957 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
2960 decl
= build_decl (TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
2961 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
2962 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
2963 DECL_ARTIFICIAL (decl
) = 1;
2964 SET_DECL_TEMPLATE_PARM_P (decl
);
2966 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
2967 TEMPLATE_PARM_LEVEL (index
) - levels
,
2968 TEMPLATE_PARM_ORIG_LEVEL (index
),
2970 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
2971 TEMPLATE_PARM_PARAMETER_PACK (t
)
2972 = TEMPLATE_PARM_PARAMETER_PACK (index
);
2974 /* Template template parameters need this. */
2975 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2976 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
2977 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
2981 return TEMPLATE_PARM_DESCENDANTS (index
);
2984 /* Process information from new template parameter PARM and append it to the
2985 LIST being built. This new parameter is a non-type parameter iff
2986 IS_NON_TYPE is true. This new parameter is a parameter
2987 pack iff IS_PARAMETER_PACK is true. */
2990 process_template_parm (tree list
, tree parm
, bool is_non_type
,
2991 bool is_parameter_pack
)
2998 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
2999 defval
= TREE_PURPOSE (parm
);
3003 tree p
= tree_last (list
);
3005 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3008 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3009 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3011 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3021 parm
= TREE_VALUE (parm
);
3023 SET_DECL_TEMPLATE_PARM_P (parm
);
3025 if (TREE_TYPE (parm
) == error_mark_node
)
3027 err_parm_list
= build_tree_list (defval
, parm
);
3028 TREE_VALUE (err_parm_list
) = error_mark_node
;
3029 return chainon (list
, err_parm_list
);
3035 The top-level cv-qualifiers on the template-parameter are
3036 ignored when determining its type. */
3037 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3038 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3040 err_parm_list
= build_tree_list (defval
, parm
);
3041 TREE_VALUE (err_parm_list
) = error_mark_node
;
3042 return chainon (list
, err_parm_list
);
3045 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3047 /* This template parameter is not a parameter pack, but it
3048 should be. Complain about "bare" parameter packs. */
3049 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3051 /* Recover by calling this a parameter pack. */
3052 is_parameter_pack
= true;
3056 /* A template parameter is not modifiable. */
3057 TREE_CONSTANT (parm
) = 1;
3058 TREE_READONLY (parm
) = 1;
3059 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3060 TREE_CONSTANT (decl
) = 1;
3061 TREE_READONLY (decl
) = 1;
3062 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3063 = build_template_parm_index (idx
, processing_template_decl
,
3064 processing_template_decl
,
3065 decl
, TREE_TYPE (parm
));
3067 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3068 = is_parameter_pack
;
3073 parm
= TREE_VALUE (TREE_VALUE (parm
));
3075 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3077 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3078 /* This is for distinguishing between real templates and template
3079 template parameters */
3080 TREE_TYPE (parm
) = t
;
3081 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3086 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3087 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3088 decl
= build_decl (TYPE_DECL
, parm
, t
);
3091 TYPE_NAME (t
) = decl
;
3092 TYPE_STUB_DECL (t
) = decl
;
3094 TEMPLATE_TYPE_PARM_INDEX (t
)
3095 = build_template_parm_index (idx
, processing_template_decl
,
3096 processing_template_decl
,
3097 decl
, TREE_TYPE (parm
));
3098 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3099 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3101 DECL_ARTIFICIAL (decl
) = 1;
3102 SET_DECL_TEMPLATE_PARM_P (decl
);
3104 parm
= build_tree_list (defval
, parm
);
3105 return chainon (list
, parm
);
3108 /* The end of a template parameter list has been reached. Process the
3109 tree list into a parameter vector, converting each parameter into a more
3110 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3114 end_template_parm_list (tree parms
)
3118 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3120 current_template_parms
3121 = tree_cons (size_int (processing_template_decl
),
3122 saved_parmlist
, current_template_parms
);
3124 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3126 next
= TREE_CHAIN (parm
);
3127 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3128 TREE_CHAIN (parm
) = NULL_TREE
;
3131 --processing_template_parmlist
;
3133 return saved_parmlist
;
3136 /* end_template_decl is called after a template declaration is seen. */
3139 end_template_decl (void)
3141 reset_specialization ();
3143 if (! processing_template_decl
)
3146 /* This matches the pushlevel in begin_template_parm_list. */
3149 --processing_template_decl
;
3150 current_template_parms
= TREE_CHAIN (current_template_parms
);
3153 /* Within the declaration of a template, return all levels of template
3154 parameters that apply. The template parameters are represented as
3155 a TREE_VEC, in the form documented in cp-tree.h for template
3159 current_template_args (void)
3162 tree args
= NULL_TREE
;
3163 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3166 /* If there is only one level of template parameters, we do not
3167 create a TREE_VEC of TREE_VECs. Instead, we return a single
3168 TREE_VEC containing the arguments. */
3170 args
= make_tree_vec (length
);
3172 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3174 tree a
= copy_node (TREE_VALUE (header
));
3177 TREE_TYPE (a
) = NULL_TREE
;
3178 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3180 tree t
= TREE_VEC_ELT (a
, i
);
3182 /* T will be a list if we are called from within a
3183 begin/end_template_parm_list pair, but a vector directly
3184 if within a begin/end_member_template_processing pair. */
3185 if (TREE_CODE (t
) == TREE_LIST
)
3189 if (!error_operand_p (t
))
3191 if (TREE_CODE (t
) == TYPE_DECL
3192 || TREE_CODE (t
) == TEMPLATE_DECL
)
3196 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3198 /* Turn this argument into a TYPE_ARGUMENT_PACK
3199 with a single element, which expands T. */
3200 tree vec
= make_tree_vec (1);
3201 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3203 t
= make_node (TYPE_ARGUMENT_PACK
);
3204 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3209 t
= DECL_INITIAL (t
);
3211 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3213 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3214 with a single element, which expands T. */
3215 tree vec
= make_tree_vec (1);
3216 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3217 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3219 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3220 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3221 TREE_TYPE (t
) = type
;
3224 TREE_VEC_ELT (a
, i
) = t
;
3230 TREE_VEC_ELT (args
, --l
) = a
;
3238 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3239 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3240 a member template. Used by push_template_decl below. */
3243 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3245 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3246 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3247 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3248 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3249 if (DECL_LANG_SPECIFIC (decl
))
3251 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
3252 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
3253 DECL_DESTRUCTOR_P (tmpl
) = DECL_DESTRUCTOR_P (decl
);
3254 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
3255 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
3256 if (DECL_OVERLOADED_OPERATOR_P (decl
))
3257 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
3258 DECL_OVERLOADED_OPERATOR_P (decl
));
3264 struct template_parm_data
3266 /* The level of the template parameters we are currently
3270 /* The index of the specialization argument we are currently
3274 /* An array whose size is the number of template parameters. The
3275 elements are nonzero if the parameter has been used in any one
3276 of the arguments processed so far. */
3279 /* An array whose size is the number of template arguments. The
3280 elements are nonzero if the argument makes use of template
3281 parameters of this level. */
3282 int* arg_uses_template_parms
;
3285 /* Subroutine of push_template_decl used to see if each template
3286 parameter in a partial specialization is used in the explicit
3287 argument list. If T is of the LEVEL given in DATA (which is
3288 treated as a template_parm_data*), then DATA->PARMS is marked
3292 mark_template_parm (tree t
, void* data
)
3296 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3298 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3300 level
= TEMPLATE_PARM_LEVEL (t
);
3301 idx
= TEMPLATE_PARM_IDX (t
);
3305 level
= TEMPLATE_TYPE_LEVEL (t
);
3306 idx
= TEMPLATE_TYPE_IDX (t
);
3309 if (level
== tpd
->level
)
3311 tpd
->parms
[idx
] = 1;
3312 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3315 /* Return zero so that for_each_template_parm will continue the
3316 traversal of the tree; we want to mark *every* template parm. */
3320 /* Process the partial specialization DECL. */
3323 process_partial_specialization (tree decl
)
3325 tree type
= TREE_TYPE (decl
);
3326 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3327 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3328 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3329 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3331 int nargs
= TREE_VEC_LENGTH (inner_args
);
3334 int did_error_intro
= 0;
3335 struct template_parm_data tpd
;
3336 struct template_parm_data tpd2
;
3338 gcc_assert (current_template_parms
);
3340 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3341 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3343 /* We check that each of the template parameters given in the
3344 partial specialization is used in the argument list to the
3345 specialization. For example:
3347 template <class T> struct S;
3348 template <class T> struct S<T*>;
3350 The second declaration is OK because `T*' uses the template
3351 parameter T, whereas
3353 template <class T> struct S<int>;
3355 is no good. Even trickier is:
3366 The S2<T> declaration is actually invalid; it is a
3367 full-specialization. Of course,
3370 struct S2<T (*)(U)>;
3372 or some such would have been OK. */
3373 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3374 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
3375 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3377 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
3378 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3379 for (i
= 0; i
< nargs
; ++i
)
3381 tpd
.current_arg
= i
;
3382 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3383 &mark_template_parm
,
3386 /*include_nondeduced_p=*/false);
3388 for (i
= 0; i
< ntparms
; ++i
)
3389 if (tpd
.parms
[i
] == 0)
3391 /* One of the template parms was not used in the
3393 if (!did_error_intro
)
3395 error ("template parameters not used in partial specialization:");
3396 did_error_intro
= 1;
3399 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3402 /* [temp.class.spec]
3404 The argument list of the specialization shall not be identical to
3405 the implicit argument list of the primary template. */
3406 if (comp_template_args
3408 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3410 error ("partial specialization %qT does not specialize any template arguments", type
);
3412 /* [temp.class.spec]
3414 A partially specialized non-type argument expression shall not
3415 involve template parameters of the partial specialization except
3416 when the argument expression is a simple identifier.
3418 The type of a template parameter corresponding to a specialized
3419 non-type argument shall not be dependent on a parameter of the
3422 Also, we verify that pack expansions only occur at the
3423 end of the argument list. */
3424 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3426 for (i
= 0; i
< nargs
; ++i
)
3428 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3429 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3430 tree packed_args
= NULL_TREE
;
3433 if (ARGUMENT_PACK_P (arg
))
3435 /* Extract the arguments from the argument pack. We'll be
3436 iterating over these in the following loop. */
3437 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3438 len
= TREE_VEC_LENGTH (packed_args
);
3441 for (j
= 0; j
< len
; j
++)
3444 /* Get the Jth argument in the parameter pack. */
3445 arg
= TREE_VEC_ELT (packed_args
, j
);
3447 if (PACK_EXPANSION_P (arg
))
3449 /* Pack expansions must come at the end of the
3451 if ((packed_args
&& j
< len
- 1)
3452 || (!packed_args
&& i
< nargs
- 1))
3454 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3455 error ("parameter pack argument %qE must be at the end of the template argument list", arg
);
3457 error ("parameter pack argument %qT must be at the end of the template argument list", arg
);
3460 TREE_VEC_ELT (packed_args
, j
) = error_mark_node
;
3464 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3465 /* We only care about the pattern. */
3466 arg
= PACK_EXPANSION_PATTERN (arg
);
3468 if (/* These first two lines are the `non-type' bit. */
3470 && TREE_CODE (arg
) != TEMPLATE_DECL
3471 /* This next line is the `argument expression is not just a
3472 simple identifier' condition and also the `specialized
3473 non-type argument' bit. */
3474 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
3476 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
3477 || (packed_args
&& uses_template_parms (arg
)))
3478 error ("template argument %qE involves template parameter(s)",
3482 /* Look at the corresponding template parameter,
3483 marking which template parameters its type depends
3485 tree type
= TREE_TYPE (parm
);
3489 /* We haven't yet initialized TPD2. Do so now. */
3490 tpd2
.arg_uses_template_parms
3491 = (int *) alloca (sizeof (int) * nargs
);
3492 /* The number of parameters here is the number in the
3493 main template, which, as checked in the assertion
3495 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
3497 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
3500 /* Mark the template parameters. But this time, we're
3501 looking for the template parameters of the main
3502 template, not in the specialization. */
3503 tpd2
.current_arg
= i
;
3504 tpd2
.arg_uses_template_parms
[i
] = 0;
3505 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
3506 for_each_template_parm (type
,
3507 &mark_template_parm
,
3510 /*include_nondeduced_p=*/false);
3512 if (tpd2
.arg_uses_template_parms
[i
])
3514 /* The type depended on some template parameters.
3515 If they are fully specialized in the
3516 specialization, that's OK. */
3518 for (j
= 0; j
< nargs
; ++j
)
3519 if (tpd2
.parms
[j
] != 0
3520 && tpd
.arg_uses_template_parms
[j
])
3522 error ("type %qT of template argument %qE depends "
3523 "on template parameter(s)",
3534 if (retrieve_specialization (maintmpl
, specargs
,
3535 /*class_specializations_p=*/true))
3536 /* We've already got this specialization. */
3539 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
3540 = tree_cons (specargs
, inner_parms
,
3541 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
3542 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
3546 /* Check that a template declaration's use of default arguments and
3547 parameter packs is not invalid. Here, PARMS are the template
3548 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3549 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3553 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3554 declaration (but not a definition); 1 indicates a declaration, 2
3555 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3556 emitted for extraneous default arguments.
3558 Returns TRUE if there were no errors found, FALSE otherwise. */
3561 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
3562 int is_partial
, int is_friend_decl
)
3565 int last_level_to_check
;
3567 bool no_errors
= true;
3571 A default template-argument shall not be specified in a
3572 function template declaration or a function template definition, nor
3573 in the template-parameter-list of the definition of a member of a
3576 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
3577 /* You can't have a function template declaration in a local
3578 scope, nor you can you define a member of a class template in a
3582 if (current_class_type
3583 && !TYPE_BEING_DEFINED (current_class_type
)
3584 && DECL_LANG_SPECIFIC (decl
)
3585 /* If this is either a friend defined in the scope of the class
3586 or a member function. */
3587 && (DECL_FUNCTION_MEMBER_P (decl
)
3588 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
3589 : DECL_FRIEND_CONTEXT (decl
)
3590 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
3592 /* And, if it was a member function, it really was defined in
3593 the scope of the class. */
3594 && (!DECL_FUNCTION_MEMBER_P (decl
)
3595 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
3596 /* We already checked these parameters when the template was
3597 declared, so there's no need to do it again now. This function
3598 was defined in class scope, but we're processing it's body now
3599 that the class is complete. */
3602 /* Core issue 226 (C++0x only): the following only applies to class
3604 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
3608 If a template-parameter has a default template-argument, all
3609 subsequent template-parameters shall have a default
3610 template-argument supplied. */
3611 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
3613 tree inner_parms
= TREE_VALUE (parm_level
);
3614 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
3615 int seen_def_arg_p
= 0;
3618 for (i
= 0; i
< ntparms
; ++i
)
3620 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
3622 if (parm
== error_mark_node
)
3625 if (TREE_PURPOSE (parm
))
3627 else if (seen_def_arg_p
3628 && !template_parameter_pack_p (TREE_VALUE (parm
)))
3630 error ("no default argument for %qD", TREE_VALUE (parm
));
3631 /* For better subsequent error-recovery, we indicate that
3632 there should have been a default argument. */
3633 TREE_PURPOSE (parm
) = error_mark_node
;
3639 /* Don't complain about an enclosing partial
3641 && parm_level
== parms
3642 && TREE_CODE (decl
) == TYPE_DECL
3644 && template_parameter_pack_p (TREE_VALUE (parm
)))
3646 /* A primary class template can only have one
3647 parameter pack, at the end of the template
3650 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
3651 error ("parameter pack %qE must be at the end of the"
3652 " template parameter list", TREE_VALUE (parm
));
3654 error ("parameter pack %qT must be at the end of the"
3655 " template parameter list",
3656 TREE_TYPE (TREE_VALUE (parm
)));
3658 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
3666 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
3670 /* For an ordinary class template, default template arguments are
3671 allowed at the innermost level, e.g.:
3672 template <class T = int>
3674 but, in a partial specialization, they're not allowed even
3675 there, as we have in [temp.class.spec]:
3677 The template parameter list of a specialization shall not
3678 contain default template argument values.
3680 So, for a partial specialization, or for a function template
3681 (in C++98/C++03), we look at all of them. */
3684 /* But, for a primary class template that is not a partial
3685 specialization we look at all template parameters except the
3687 parms
= TREE_CHAIN (parms
);
3689 /* Figure out what error message to issue. */
3690 if (is_friend_decl
== 2)
3691 msg
= "default template arguments may not be used in function template friend re-declaration";
3692 else if (is_friend_decl
)
3693 msg
= "default template arguments may not be used in function template friend declarations";
3694 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
3695 msg
= "default template arguments may not be used in function templates";
3696 else if (is_partial
)
3697 msg
= "default template arguments may not be used in partial specializations";
3699 msg
= "default argument for template parameter for class enclosing %qD";
3701 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
3702 /* If we're inside a class definition, there's no need to
3703 examine the parameters to the class itself. On the one
3704 hand, they will be checked when the class is defined, and,
3705 on the other, default arguments are valid in things like:
3706 template <class T = double>
3707 struct S { template <class U> void f(U); };
3708 Here the default argument for `S' has no bearing on the
3709 declaration of `f'. */
3710 last_level_to_check
= template_class_depth (current_class_type
) + 1;
3712 /* Check everything. */
3713 last_level_to_check
= 0;
3715 for (parm_level
= parms
;
3716 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
3717 parm_level
= TREE_CHAIN (parm_level
))
3719 tree inner_parms
= TREE_VALUE (parm_level
);
3723 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3724 for (i
= 0; i
< ntparms
; ++i
)
3726 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
3729 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
3734 if (is_friend_decl
== 2)
3741 /* Clear out the default argument so that we are not
3743 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
3747 /* At this point, if we're still interested in issuing messages,
3748 they must apply to classes surrounding the object declared. */
3750 msg
= "default argument for template parameter for class enclosing %qD";
3756 /* Worker for push_template_decl_real, called via
3757 for_each_template_parm. DATA is really an int, indicating the
3758 level of the parameters we are interested in. If T is a template
3759 parameter of that level, return nonzero. */
3762 template_parm_this_level_p (tree t
, void* data
)
3764 int this_level
= *(int *)data
;
3767 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3768 level
= TEMPLATE_PARM_LEVEL (t
);
3770 level
= TEMPLATE_TYPE_LEVEL (t
);
3771 return level
== this_level
;
3774 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3775 parameters given by current_template_args, or reuses a
3776 previously existing one, if appropriate. Returns the DECL, or an
3777 equivalent one, if it is replaced via a call to duplicate_decls.
3779 If IS_FRIEND is true, DECL is a friend declaration. */
3782 push_template_decl_real (tree decl
, bool is_friend
)
3790 int new_template_p
= 0;
3791 /* True if the template is a member template, in the sense of
3793 bool member_template_p
= false;
3795 if (decl
== error_mark_node
|| !current_template_parms
)
3796 return error_mark_node
;
3798 /* See if this is a partial specialization. */
3799 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
3800 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
3801 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
3803 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
3807 /* For a friend, we want the context of the friend function, not
3808 the type of which it is a friend. */
3809 ctx
= DECL_CONTEXT (decl
);
3810 else if (CP_DECL_CONTEXT (decl
)
3811 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
3812 /* In the case of a virtual function, we want the class in which
3814 ctx
= CP_DECL_CONTEXT (decl
);
3816 /* Otherwise, if we're currently defining some class, the DECL
3817 is assumed to be a member of the class. */
3818 ctx
= current_scope ();
3820 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
3823 if (!DECL_CONTEXT (decl
))
3824 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
3826 /* See if this is a primary template. */
3827 if (is_friend
&& ctx
)
3828 /* A friend template that specifies a class context, i.e.
3829 template <typename T> friend void A<T>::f();
3833 primary
= template_parm_scope_p ();
3837 if (DECL_CLASS_SCOPE_P (decl
))
3838 member_template_p
= true;
3839 if (TREE_CODE (decl
) == TYPE_DECL
3840 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
3842 error ("template class without a name");
3843 return error_mark_node
;
3845 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3847 if (DECL_DESTRUCTOR_P (decl
))
3851 A destructor shall not be a member template. */
3852 error ("destructor %qD declared as member template", decl
);
3853 return error_mark_node
;
3855 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
3856 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
3857 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
3858 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
3859 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
3860 == void_list_node
)))
3862 /* [basic.stc.dynamic.allocation]
3864 An allocation function can be a function
3865 template. ... Template allocation functions shall
3866 have two or more parameters. */
3867 error ("invalid template declaration of %qD", decl
);
3868 return error_mark_node
;
3871 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3872 && CLASS_TYPE_P (TREE_TYPE (decl
)))
3876 error ("template declaration of %q#D", decl
);
3877 return error_mark_node
;
3881 /* Check to see that the rules regarding the use of default
3882 arguments are not being violated. */
3883 check_default_tmpl_args (decl
, current_template_parms
,
3884 primary
, is_partial
, /*is_friend_decl=*/0);
3886 /* Ensure that there are no parameter packs in the type of this
3887 declaration that have not been expanded. */
3888 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3890 /* Check each of the arguments individually to see if there are
3891 any bare parameter packs. */
3892 tree type
= TREE_TYPE (decl
);
3893 tree arg
= DECL_ARGUMENTS (decl
);
3894 tree argtype
= TYPE_ARG_TYPES (type
);
3896 while (arg
&& argtype
)
3898 if (!FUNCTION_PARAMETER_PACK_P (arg
)
3899 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
3901 /* This is a PARM_DECL that contains unexpanded parameter
3902 packs. We have already complained about this in the
3903 check_for_bare_parameter_packs call, so just replace
3904 these types with ERROR_MARK_NODE. */
3905 TREE_TYPE (arg
) = error_mark_node
;
3906 TREE_VALUE (argtype
) = error_mark_node
;
3909 arg
= TREE_CHAIN (arg
);
3910 argtype
= TREE_CHAIN (argtype
);
3913 /* Check for bare parameter packs in the return type and the
3914 exception specifiers. */
3915 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
3916 /* Errors were already issued, set return type to int
3917 as the frontend doesn't expect error_mark_node as
3919 TREE_TYPE (type
) = integer_type_node
;
3920 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
3921 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
3923 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
3925 TREE_TYPE (decl
) = error_mark_node
;
3926 return error_mark_node
;
3930 return process_partial_specialization (decl
);
3932 args
= current_template_args ();
3935 || TREE_CODE (ctx
) == FUNCTION_DECL
3936 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
3937 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
3939 if (DECL_LANG_SPECIFIC (decl
)
3940 && DECL_TEMPLATE_INFO (decl
)
3941 && DECL_TI_TEMPLATE (decl
))
3942 tmpl
= DECL_TI_TEMPLATE (decl
);
3943 /* If DECL is a TYPE_DECL for a class-template, then there won't
3944 be DECL_LANG_SPECIFIC. The information equivalent to
3945 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3946 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3947 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3948 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3950 /* Since a template declaration already existed for this
3951 class-type, we must be redeclaring it here. Make sure
3952 that the redeclaration is valid. */
3953 redeclare_class_template (TREE_TYPE (decl
),
3954 current_template_parms
);
3955 /* We don't need to create a new TEMPLATE_DECL; just use the
3956 one we already had. */
3957 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3961 tmpl
= build_template_decl (decl
, current_template_parms
,
3965 if (DECL_LANG_SPECIFIC (decl
)
3966 && DECL_TEMPLATE_SPECIALIZATION (decl
))
3968 /* A specialization of a member template of a template
3970 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3971 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
3972 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3978 tree a
, t
, current
, parms
;
3980 tree tinfo
= get_template_info (decl
);
3984 error ("template definition of non-template %q#D", decl
);
3985 return error_mark_node
;
3988 tmpl
= TI_TEMPLATE (tinfo
);
3990 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3991 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
3992 && DECL_TEMPLATE_SPECIALIZATION (decl
)
3993 && DECL_MEMBER_TEMPLATE_P (tmpl
))
3997 /* The declaration is a specialization of a member
3998 template, declared outside the class. Therefore, the
3999 innermost template arguments will be NULL, so we
4000 replace them with the arguments determined by the
4001 earlier call to check_explicit_specialization. */
4002 args
= DECL_TI_ARGS (decl
);
4005 = build_template_decl (decl
, current_template_parms
,
4007 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4008 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4009 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4010 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4011 DECL_TEMPLATE_INFO (new_tmpl
)
4012 = tree_cons (tmpl
, args
, NULL_TREE
);
4014 register_specialization (new_tmpl
,
4015 most_general_template (tmpl
),
4021 /* Make sure the template headers we got make sense. */
4023 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4024 i
= TMPL_PARMS_DEPTH (parms
);
4025 if (TMPL_ARGS_DEPTH (args
) != i
)
4027 error ("expected %d levels of template parms for %q#D, got %d",
4028 i
, decl
, TMPL_ARGS_DEPTH (args
));
4031 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4033 a
= TMPL_ARGS_LEVEL (args
, i
);
4034 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4036 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4038 if (current
== decl
)
4039 error ("got %d template parameters for %q#D",
4040 TREE_VEC_LENGTH (a
), decl
);
4042 error ("got %d template parameters for %q#T",
4043 TREE_VEC_LENGTH (a
), current
);
4044 error (" but %d required", TREE_VEC_LENGTH (t
));
4045 return error_mark_node
;
4048 if (current
== decl
)
4051 current
= (TYPE_P (current
)
4052 ? TYPE_CONTEXT (current
)
4053 : DECL_CONTEXT (current
));
4056 /* Check that the parms are used in the appropriate qualifying scopes
4057 in the declarator. */
4058 if (!comp_template_args
4060 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4063 template arguments to %qD do not match original template %qD",
4064 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4065 if (!uses_template_parms (TI_ARGS (tinfo
)))
4066 inform (input_location
, "use template<> for an explicit specialization");
4067 /* Avoid crash in import_export_decl. */
4068 DECL_INTERFACE_KNOWN (decl
) = 1;
4069 return error_mark_node
;
4073 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4074 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4076 /* Push template declarations for global functions and types. Note
4077 that we do not try to push a global template friend declared in a
4078 template class; such a thing may well depend on the template
4079 parameters of the class. */
4080 if (new_template_p
&& !ctx
4081 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4083 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4084 if (tmpl
== error_mark_node
)
4085 return error_mark_node
;
4087 /* Hide template friend classes that haven't been declared yet. */
4088 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4090 DECL_ANTICIPATED (tmpl
) = 1;
4091 DECL_FRIEND_P (tmpl
) = 1;
4097 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4100 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4101 if (DECL_CONV_FN_P (tmpl
))
4103 int depth
= TMPL_PARMS_DEPTH (parms
);
4105 /* It is a conversion operator. See if the type converted to
4106 depends on innermost template operands. */
4108 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4110 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4113 /* Give template template parms a DECL_CONTEXT of the template
4114 for which they are a parameter. */
4115 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4116 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4118 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4119 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4120 DECL_CONTEXT (parm
) = tmpl
;
4124 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4125 back to its most general template. If TMPL is a specialization,
4126 ARGS may only have the innermost set of arguments. Add the missing
4127 argument levels if necessary. */
4128 if (DECL_TEMPLATE_INFO (tmpl
))
4129 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4131 info
= tree_cons (tmpl
, args
, NULL_TREE
);
4133 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4134 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4135 else if (DECL_LANG_SPECIFIC (decl
))
4136 DECL_TEMPLATE_INFO (decl
) = info
;
4138 return DECL_TEMPLATE_RESULT (tmpl
);
4142 push_template_decl (tree decl
)
4144 return push_template_decl_real (decl
, false);
4147 /* Called when a class template TYPE is redeclared with the indicated
4148 template PARMS, e.g.:
4150 template <class T> struct S;
4151 template <class T> struct S {}; */
4154 redeclare_class_template (tree type
, tree parms
)
4160 if (!TYPE_TEMPLATE_INFO (type
))
4162 error ("%qT is not a template type", type
);
4166 tmpl
= TYPE_TI_TEMPLATE (type
);
4167 if (!PRIMARY_TEMPLATE_P (tmpl
))
4168 /* The type is nested in some template class. Nothing to worry
4169 about here; there are no new template parameters for the nested
4175 error ("template specifiers not specified in declaration of %qD",
4180 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4181 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4183 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4185 error ("redeclared with %d template parameter(s)",
4186 TREE_VEC_LENGTH (parms
));
4187 inform (input_location
, "previous declaration %q+D used %d template parameter(s)",
4188 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4192 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4199 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4200 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4203 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4204 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4205 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4206 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4208 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4210 if (tmpl_parm
!= error_mark_node
4211 && (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4212 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4213 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4214 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4215 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4216 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4217 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4218 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4219 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))))))
4221 error ("template parameter %q+#D", tmpl_parm
);
4222 error ("redeclared here as %q#D", parm
);
4226 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4228 /* We have in [temp.param]:
4230 A template-parameter may not be given default arguments
4231 by two different declarations in the same scope. */
4232 error ("redefinition of default argument for %q#D", parm
);
4233 inform (input_location
, "%Joriginal definition appeared here", tmpl_parm
);
4237 if (parm_default
!= NULL_TREE
)
4238 /* Update the previous template parameters (which are the ones
4239 that will really count) with the new default value. */
4240 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4241 else if (tmpl_default
!= NULL_TREE
)
4242 /* Update the new parameters, too; they'll be used as the
4243 parameters for any members. */
4244 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4250 /* Simplify EXPR if it is a non-dependent expression. Returns the
4251 (possibly simplified) expression. */
4254 fold_non_dependent_expr (tree expr
)
4256 if (expr
== NULL_TREE
)
4259 /* If we're in a template, but EXPR isn't value dependent, simplify
4260 it. We're supposed to treat:
4262 template <typename T> void f(T[1 + 1]);
4263 template <typename T> void f(T[2]);
4265 as two declarations of the same function, for example. */
4266 if (processing_template_decl
4267 && !type_dependent_expression_p (expr
)
4268 && !value_dependent_expression_p (expr
))
4270 HOST_WIDE_INT saved_processing_template_decl
;
4272 saved_processing_template_decl
= processing_template_decl
;
4273 processing_template_decl
= 0;
4274 expr
= tsubst_copy_and_build (expr
,
4277 /*in_decl=*/NULL_TREE
,
4278 /*function_p=*/false,
4279 /*integral_constant_expression_p=*/true);
4280 processing_template_decl
= saved_processing_template_decl
;
4285 /* EXPR is an expression which is used in a constant-expression context.
4286 For instance, it could be a VAR_DECL with a constant initializer.
4287 Extract the innermost constant expression.
4289 This is basically a more powerful version of
4290 integral_constant_value, which can be used also in templates where
4291 initializers can maintain a syntactic rather than semantic form
4292 (even if they are non-dependent, for access-checking purposes). */
4295 fold_decl_constant_value (tree expr
)
4297 tree const_expr
= expr
;
4300 expr
= fold_non_dependent_expr (const_expr
);
4301 const_expr
= integral_constant_value (expr
);
4303 while (expr
!= const_expr
);
4308 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4309 must be a function or a pointer-to-function type, as specified
4310 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4311 and check that the resulting function has external linkage. */
4314 convert_nontype_argument_function (tree type
, tree expr
)
4319 fn
= instantiate_type (type
, fns
, tf_none
);
4320 if (fn
== error_mark_node
)
4321 return error_mark_node
;
4324 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4325 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4326 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4327 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4329 /* [temp.arg.nontype]/1
4331 A template-argument for a non-type, non-template template-parameter
4334 -- the address of an object or function with external linkage. */
4335 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4337 error ("%qE is not a valid template argument for type %qT "
4338 "because function %qD has not external linkage",
4339 expr
, type
, fn_no_ptr
);
4346 /* Attempt to convert the non-type template parameter EXPR to the
4347 indicated TYPE. If the conversion is successful, return the
4348 converted value. If the conversion is unsuccessful, return
4349 NULL_TREE if we issued an error message, or error_mark_node if we
4350 did not. We issue error messages for out-and-out bad template
4351 parameters, but not simply because the conversion failed, since we
4352 might be just trying to do argument deduction. Both TYPE and EXPR
4353 must be non-dependent.
4355 The conversion follows the special rules described in
4356 [temp.arg.nontype], and it is much more strict than an implicit
4359 This function is called twice for each template argument (see
4360 lookup_template_class for a more accurate description of this
4361 problem). This means that we need to handle expressions which
4362 are not valid in a C++ source, but can be created from the
4363 first call (for instance, casts to perform conversions). These
4364 hacks can go away after we fix the double coercion problem. */
4367 convert_nontype_argument (tree type
, tree expr
)
4371 /* Detect immediately string literals as invalid non-type argument.
4372 This special-case is not needed for correctness (we would easily
4373 catch this later), but only to provide better diagnostic for this
4374 common user mistake. As suggested by DR 100, we do not mention
4375 linkage issues in the diagnostic as this is not the point. */
4376 if (TREE_CODE (expr
) == STRING_CST
)
4378 error ("%qE is not a valid template argument for type %qT "
4379 "because string literals can never be used in this context",
4384 /* If we are in a template, EXPR may be non-dependent, but still
4385 have a syntactic, rather than semantic, form. For example, EXPR
4386 might be a SCOPE_REF, rather than the VAR_DECL to which the
4387 SCOPE_REF refers. Preserving the qualifying scope is necessary
4388 so that access checking can be performed when the template is
4389 instantiated -- but here we need the resolved form so that we can
4390 convert the argument. */
4391 expr
= fold_non_dependent_expr (expr
);
4392 if (error_operand_p (expr
))
4393 return error_mark_node
;
4394 expr_type
= TREE_TYPE (expr
);
4396 /* HACK: Due to double coercion, we can get a
4397 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4398 which is the tree that we built on the first call (see
4399 below when coercing to reference to object or to reference to
4400 function). We just strip everything and get to the arg.
4401 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4403 if (TREE_CODE (expr
) == NOP_EXPR
)
4405 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
4407 /* ??? Maybe we could use convert_from_reference here, but we
4408 would need to relax its constraints because the NOP_EXPR
4409 could actually change the type to something more cv-qualified,
4410 and this is not folded by convert_from_reference. */
4411 tree addr
= TREE_OPERAND (expr
, 0);
4412 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
4413 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
4414 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
4415 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4416 (TREE_TYPE (expr_type
),
4417 TREE_TYPE (TREE_TYPE (addr
))));
4419 expr
= TREE_OPERAND (addr
, 0);
4420 expr_type
= TREE_TYPE (expr
);
4423 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4424 parameter is a pointer to object, through decay and
4425 qualification conversion. Let's strip everything. */
4426 else if (TYPE_PTROBV_P (type
))
4429 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
4430 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
4431 /* Skip the ADDR_EXPR only if it is part of the decay for
4432 an array. Otherwise, it is part of the original argument
4433 in the source code. */
4434 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
4435 expr
= TREE_OPERAND (expr
, 0);
4436 expr_type
= TREE_TYPE (expr
);
4440 /* [temp.arg.nontype]/5, bullet 1
4442 For a non-type template-parameter of integral or enumeration type,
4443 integral promotions (_conv.prom_) and integral conversions
4444 (_conv.integral_) are applied. */
4445 if (INTEGRAL_TYPE_P (type
))
4447 if (!INTEGRAL_TYPE_P (expr_type
))
4448 return error_mark_node
;
4450 expr
= fold_decl_constant_value (expr
);
4451 /* Notice that there are constant expressions like '4 % 0' which
4452 do not fold into integer constants. */
4453 if (TREE_CODE (expr
) != INTEGER_CST
)
4455 error ("%qE is not a valid template argument for type %qT "
4456 "because it is a non-constant expression", expr
, type
);
4460 /* At this point, an implicit conversion does what we want,
4461 because we already know that the expression is of integral
4463 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
4464 if (expr
== error_mark_node
)
4465 return error_mark_node
;
4467 /* Conversion was allowed: fold it to a bare integer constant. */
4470 /* [temp.arg.nontype]/5, bullet 2
4472 For a non-type template-parameter of type pointer to object,
4473 qualification conversions (_conv.qual_) and the array-to-pointer
4474 conversion (_conv.array_) are applied. */
4475 else if (TYPE_PTROBV_P (type
))
4477 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4479 A template-argument for a non-type, non-template template-parameter
4480 shall be one of: [...]
4482 -- the name of a non-type template-parameter;
4483 -- the address of an object or function with external linkage, [...]
4484 expressed as "& id-expression" where the & is optional if the name
4485 refers to a function or array, or if the corresponding
4486 template-parameter is a reference.
4488 Here, we do not care about functions, as they are invalid anyway
4489 for a parameter of type pointer-to-object. */
4491 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
4492 /* Non-type template parameters are OK. */
4494 else if (TREE_CODE (expr
) != ADDR_EXPR
4495 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
4497 if (TREE_CODE (expr
) == VAR_DECL
)
4499 error ("%qD is not a valid template argument "
4500 "because %qD is a variable, not the address of "
4505 /* Other values, like integer constants, might be valid
4506 non-type arguments of some other type. */
4507 return error_mark_node
;
4513 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
4514 ? TREE_OPERAND (expr
, 0) : expr
);
4515 if (TREE_CODE (decl
) != VAR_DECL
)
4517 error ("%qE is not a valid template argument of type %qT "
4518 "because %qE is not a variable",
4522 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
4524 error ("%qE is not a valid template argument of type %qT "
4525 "because %qD does not have external linkage",
4531 expr
= decay_conversion (expr
);
4532 if (expr
== error_mark_node
)
4533 return error_mark_node
;
4535 expr
= perform_qualification_conversions (type
, expr
);
4536 if (expr
== error_mark_node
)
4537 return error_mark_node
;
4539 /* [temp.arg.nontype]/5, bullet 3
4541 For a non-type template-parameter of type reference to object, no
4542 conversions apply. The type referred to by the reference may be more
4543 cv-qualified than the (otherwise identical) type of the
4544 template-argument. The template-parameter is bound directly to the
4545 template-argument, which must be an lvalue. */
4546 else if (TYPE_REF_OBJ_P (type
))
4548 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
4550 return error_mark_node
;
4552 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
4554 error ("%qE is not a valid template argument for type %qT "
4555 "because of conflicts in cv-qualification", expr
, type
);
4559 if (!real_lvalue_p (expr
))
4561 error ("%qE is not a valid template argument for type %qT "
4562 "because it is not an lvalue", expr
, type
);
4566 /* [temp.arg.nontype]/1
4568 A template-argument for a non-type, non-template template-parameter
4569 shall be one of: [...]
4571 -- the address of an object or function with external linkage. */
4572 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
4574 error ("%qE is not a valid template argument for type %qT "
4575 "because object %qD has not external linkage",
4580 expr
= build_nop (type
, build_address (expr
));
4582 /* [temp.arg.nontype]/5, bullet 4
4584 For a non-type template-parameter of type pointer to function, only
4585 the function-to-pointer conversion (_conv.func_) is applied. If the
4586 template-argument represents a set of overloaded functions (or a
4587 pointer to such), the matching function is selected from the set
4589 else if (TYPE_PTRFN_P (type
))
4591 /* If the argument is a template-id, we might not have enough
4592 context information to decay the pointer. */
4593 if (!type_unknown_p (expr_type
))
4595 expr
= decay_conversion (expr
);
4596 if (expr
== error_mark_node
)
4597 return error_mark_node
;
4600 expr
= convert_nontype_argument_function (type
, expr
);
4601 if (!expr
|| expr
== error_mark_node
)
4604 if (TREE_CODE (expr
) != ADDR_EXPR
)
4606 error ("%qE is not a valid template argument for type %qT", expr
, type
);
4607 error ("it must be the address of a function with external linkage");
4611 /* [temp.arg.nontype]/5, bullet 5
4613 For a non-type template-parameter of type reference to function, no
4614 conversions apply. If the template-argument represents a set of
4615 overloaded functions, the matching function is selected from the set
4617 else if (TYPE_REFFN_P (type
))
4619 if (TREE_CODE (expr
) == ADDR_EXPR
)
4621 error ("%qE is not a valid template argument for type %qT "
4622 "because it is a pointer", expr
, type
);
4623 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
4627 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
4628 if (!expr
|| expr
== error_mark_node
)
4631 expr
= build_nop (type
, build_address (expr
));
4633 /* [temp.arg.nontype]/5, bullet 6
4635 For a non-type template-parameter of type pointer to member function,
4636 no conversions apply. If the template-argument represents a set of
4637 overloaded member functions, the matching member function is selected
4638 from the set (_over.over_). */
4639 else if (TYPE_PTRMEMFUNC_P (type
))
4641 expr
= instantiate_type (type
, expr
, tf_none
);
4642 if (expr
== error_mark_node
)
4643 return error_mark_node
;
4645 /* There is no way to disable standard conversions in
4646 resolve_address_of_overloaded_function (called by
4647 instantiate_type). It is possible that the call succeeded by
4648 converting &B::I to &D::I (where B is a base of D), so we need
4649 to reject this conversion here.
4651 Actually, even if there was a way to disable standard conversions,
4652 it would still be better to reject them here so that we can
4653 provide a superior diagnostic. */
4654 if (!same_type_p (TREE_TYPE (expr
), type
))
4656 /* Make sure we are just one standard conversion off. */
4657 gcc_assert (can_convert (type
, TREE_TYPE (expr
)));
4658 error ("%qE is not a valid template argument for type %qT "
4659 "because it is of type %qT", expr
, type
,
4661 inform (input_location
, "standard conversions are not allowed in this context");
4665 /* [temp.arg.nontype]/5, bullet 7
4667 For a non-type template-parameter of type pointer to data member,
4668 qualification conversions (_conv.qual_) are applied. */
4669 else if (TYPE_PTRMEM_P (type
))
4671 expr
= perform_qualification_conversions (type
, expr
);
4672 if (expr
== error_mark_node
)
4675 /* A template non-type parameter must be one of the above. */
4679 /* Sanity check: did we actually convert the argument to the
4681 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
4685 /* Subroutine of coerce_template_template_parms, which returns 1 if
4686 PARM_PARM and ARG_PARM match using the rule for the template
4687 parameters of template template parameters. Both PARM and ARG are
4688 template parameters; the rest of the arguments are the same as for
4689 coerce_template_template_parms.
4692 coerce_template_template_parm (tree parm
,
4694 tsubst_flags_t complain
,
4698 if (arg
== NULL_TREE
|| arg
== error_mark_node
4699 || parm
== NULL_TREE
|| parm
== error_mark_node
)
4702 if (TREE_CODE (arg
) != TREE_CODE (parm
))
4705 switch (TREE_CODE (parm
))
4708 /* We encounter instantiations of templates like
4709 template <template <template <class> class> class TT>
4712 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4713 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
4715 if (!coerce_template_template_parms
4716 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
4722 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
4723 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
4724 /* Argument is a parameter pack but parameter is not. */
4729 /* The tsubst call is used to handle cases such as
4731 template <int> class C {};
4732 template <class T, template <T> class TT> class D {};
4735 i.e. the parameter list of TT depends on earlier parameters. */
4736 if (!uses_template_parms (TREE_TYPE (arg
))
4738 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
4742 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
4743 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
4744 /* Argument is a parameter pack but parameter is not. */
4757 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4758 template template parameters. Both PARM_PARMS and ARG_PARMS are
4759 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4762 Consider the example:
4763 template <class T> class A;
4764 template<template <class U> class TT> class B;
4766 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4767 the parameters to A, and OUTER_ARGS contains A. */
4770 coerce_template_template_parms (tree parm_parms
,
4772 tsubst_flags_t complain
,
4776 int nparms
, nargs
, i
;
4780 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
4781 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
4783 nparms
= TREE_VEC_LENGTH (parm_parms
);
4784 nargs
= TREE_VEC_LENGTH (arg_parms
);
4786 /* Determine whether we have a parameter pack at the end of the
4787 template template parameter's template parameter list. */
4788 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
4790 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
4792 if (parm
== error_mark_node
)
4795 switch (TREE_CODE (parm
))
4799 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
4804 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
4814 && !(variadic_p
&& nargs
>= nparms
- 1))
4817 /* Check all of the template parameters except the parameter pack at
4818 the end (if any). */
4819 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
4821 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
4822 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
4825 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
4826 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
4828 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
4836 /* Check each of the template parameters in the template
4837 argument against the template parameter pack at the end of
4838 the template template parameter. */
4839 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
4842 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
4844 for (; i
< nargs
; ++i
)
4846 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
4849 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
4851 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
4860 /* Verifies that the deduced template arguments (in TARGS) for the
4861 template template parameters (in TPARMS) represent valid bindings,
4862 by comparing the template parameter list of each template argument
4863 to the template parameter list of its corresponding template
4864 template parameter, in accordance with DR150. This
4865 routine can only be called after all template arguments have been
4866 deduced. It will return TRUE if all of the template template
4867 parameter bindings are okay, FALSE otherwise. */
4869 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
4871 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
4874 /* We're dealing with template parms in this process. */
4875 ++processing_template_decl
;
4877 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
4879 for (i
= 0; i
< ntparms
; ++i
)
4881 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
4882 tree targ
= TREE_VEC_ELT (targs
, i
);
4884 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
4886 tree packed_args
= NULL_TREE
;
4889 if (ARGUMENT_PACK_P (targ
))
4891 /* Look inside the argument pack. */
4892 packed_args
= ARGUMENT_PACK_ARGS (targ
);
4893 len
= TREE_VEC_LENGTH (packed_args
);
4896 for (idx
= 0; idx
< len
; ++idx
)
4898 tree targ_parms
= NULL_TREE
;
4901 /* Extract the next argument from the argument
4903 targ
= TREE_VEC_ELT (packed_args
, idx
);
4905 if (PACK_EXPANSION_P (targ
))
4906 /* Look at the pattern of the pack expansion. */
4907 targ
= PACK_EXPANSION_PATTERN (targ
);
4909 /* Extract the template parameters from the template
4911 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
4912 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
4913 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
4914 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
4916 /* Verify that we can coerce the template template
4917 parameters from the template argument to the template
4918 parameter. This requires an exact match. */
4920 && !coerce_template_template_parms
4921 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
4936 --processing_template_decl
;
4940 /* Convert the indicated template ARG as necessary to match the
4941 indicated template PARM. Returns the converted ARG, or
4942 error_mark_node if the conversion was unsuccessful. Error and
4943 warning messages are issued under control of COMPLAIN. This
4944 conversion is for the Ith parameter in the parameter list. ARGS is
4945 the full set of template arguments deduced so far. */
4948 convert_template_argument (tree parm
,
4951 tsubst_flags_t complain
,
4957 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
4959 if (TREE_CODE (arg
) == TREE_LIST
4960 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
4962 /* The template argument was the name of some
4963 member function. That's usually
4964 invalid, but static members are OK. In any
4965 case, grab the underlying fields/functions
4966 and issue an error later if required. */
4967 orig_arg
= TREE_VALUE (arg
);
4968 TREE_TYPE (arg
) = unknown_type_node
;
4973 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
4974 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
4975 || requires_tmpl_type
);
4977 /* When determining whether an argument pack expansion is a template,
4978 look at the pattern. */
4979 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
4980 arg
= PACK_EXPANSION_PATTERN (arg
);
4983 ((TREE_CODE (arg
) == TEMPLATE_DECL
4984 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
4985 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4986 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
4989 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4990 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
4991 arg
= TYPE_STUB_DECL (arg
);
4993 is_type
= TYPE_P (arg
) || is_tmpl_type
;
4995 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
4996 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
4998 permerror (input_location
, "to refer to a type member of a template parameter, "
4999 "use %<typename %E%>", orig_arg
);
5001 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5002 TREE_OPERAND (arg
, 1),
5004 complain
& tf_error
);
5008 if (is_type
!= requires_type
)
5012 if (complain
& tf_error
)
5014 error ("type/value mismatch at argument %d in template "
5015 "parameter list for %qD",
5018 error (" expected a constant of type %qT, got %qT",
5020 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
5021 else if (requires_tmpl_type
)
5022 error (" expected a class template, got %qE", orig_arg
);
5024 error (" expected a type, got %qE", orig_arg
);
5027 return error_mark_node
;
5029 if (is_tmpl_type
^ requires_tmpl_type
)
5031 if (in_decl
&& (complain
& tf_error
))
5033 error ("type/value mismatch at argument %d in template "
5034 "parameter list for %qD",
5037 error (" expected a type, got %qT", DECL_NAME (arg
));
5039 error (" expected a class template, got %qT", orig_arg
);
5041 return error_mark_node
;
5046 if (requires_tmpl_type
)
5048 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
5049 /* The number of argument required is not known yet.
5050 Just accept it for now. */
5051 val
= TREE_TYPE (arg
);
5054 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5057 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5059 if (coerce_template_template_parms (parmparm
, argparm
,
5065 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5067 if (val
!= error_mark_node
)
5069 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
5070 val
= TREE_TYPE (val
);
5071 else if (TREE_CODE (val
) == TYPE_PACK_EXPANSION
5072 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg
))
5074 val
= TREE_TYPE (arg
);
5075 val
= make_pack_expansion (val
);
5081 if (in_decl
&& (complain
& tf_error
))
5083 error ("type/value mismatch at argument %d in "
5084 "template parameter list for %qD",
5086 error (" expected a template of type %qD, got %qD",
5090 val
= error_mark_node
;
5096 /* We only form one instance of each template specialization.
5097 Therefore, if we use a non-canonical variant (i.e., a
5098 typedef), any future messages referring to the type will use
5099 the typedef, which is confusing if those future uses do not
5100 themselves also use the typedef. */
5102 val
= canonical_type_variant (val
);
5106 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
5108 if (invalid_nontype_parm_type_p (t
, complain
))
5109 return error_mark_node
;
5111 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
5113 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
5117 /* Not sure if this is reachable, but it doesn't hurt
5119 error ("type mismatch in nontype parameter pack");
5120 val
= error_mark_node
;
5123 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
5124 /* We used to call digest_init here. However, digest_init
5125 will report errors, which we don't want when complain
5126 is zero. More importantly, digest_init will try too
5127 hard to convert things: for example, `0' should not be
5128 converted to pointer type at this point according to
5129 the standard. Accepting this is not merely an
5130 extension, since deciding whether or not these
5131 conversions can occur is part of determining which
5132 function template to call, or whether a given explicit
5133 argument specification is valid. */
5134 val
= convert_nontype_argument (t
, orig_arg
);
5138 if (val
== NULL_TREE
)
5139 val
= error_mark_node
;
5140 else if (val
== error_mark_node
&& (complain
& tf_error
))
5141 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
5147 /* Coerces the remaining template arguments in INNER_ARGS (from
5148 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5149 Returns the coerced argument pack. PARM_IDX is the position of this
5150 parameter in the template parameter list. ARGS is the original
5151 template argument list. */
5153 coerce_template_parameter_pack (tree parms
,
5161 tsubst_flags_t complain
)
5163 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
5164 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5167 tree packed_types
= NULL_TREE
;
5169 if (arg_idx
> nargs
)
5172 packed_args
= make_tree_vec (nargs
- arg_idx
);
5174 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
5175 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
5177 /* When the template parameter is a non-type template
5178 parameter pack whose type uses parameter packs, we need
5179 to look at each of the template arguments
5180 separately. Build a vector of the types for these
5181 non-type template parameters in PACKED_TYPES. */
5183 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
5184 packed_types
= tsubst_pack_expansion (expansion
, args
,
5187 if (packed_types
== error_mark_node
)
5188 return error_mark_node
;
5190 /* Check that we have the right number of arguments. */
5192 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
5193 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
5196 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
5197 error ("wrong number of template arguments (%d, should be %d)",
5198 nargs
, needed_parms
);
5199 return error_mark_node
;
5202 /* If we aren't able to check the actual arguments now
5203 (because they haven't been expanded yet), we can at least
5204 verify that all of the types used for the non-type
5205 template parameter pack are, in fact, valid for non-type
5206 template parameters. */
5208 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
5210 int j
, len
= TREE_VEC_LENGTH (packed_types
);
5211 for (j
= 0; j
< len
; ++j
)
5213 tree t
= TREE_VEC_ELT (packed_types
, j
);
5214 if (invalid_nontype_parm_type_p (t
, complain
))
5215 return error_mark_node
;
5220 /* Convert the remaining arguments, which will be a part of the
5221 parameter pack "parm". */
5222 for (; arg_idx
< nargs
; ++arg_idx
)
5224 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5225 tree actual_parm
= TREE_VALUE (parm
);
5227 if (packed_types
&& !PACK_EXPANSION_P (arg
))
5229 /* When we have a vector of types (corresponding to the
5230 non-type template parameter pack that uses parameter
5231 packs in its type, as mention above), and the
5232 argument is not an expansion (which expands to a
5233 currently unknown number of arguments), clone the
5234 parm and give it the next type in PACKED_TYPES. */
5235 actual_parm
= copy_node (actual_parm
);
5236 TREE_TYPE (actual_parm
) =
5237 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
5240 if (arg
!= error_mark_node
)
5241 arg
= convert_template_argument (actual_parm
,
5242 arg
, new_args
, complain
, parm_idx
,
5244 if (arg
== error_mark_node
)
5246 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
5249 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
5250 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
5251 argument_pack
= make_node (TYPE_ARGUMENT_PACK
);
5254 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
5255 TREE_TYPE (argument_pack
)
5256 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
5257 TREE_CONSTANT (argument_pack
) = 1;
5260 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
5261 return argument_pack
;
5264 /* Convert all template arguments to their appropriate types, and
5265 return a vector containing the innermost resulting template
5266 arguments. If any error occurs, return error_mark_node. Error and
5267 warning messages are issued under control of COMPLAIN.
5269 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5270 for arguments not specified in ARGS. Otherwise, if
5271 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5272 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5273 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5277 coerce_template_parms (tree parms
,
5280 tsubst_flags_t complain
,
5281 bool require_all_args
,
5282 bool use_default_args
)
5284 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
5287 tree new_inner_args
;
5288 bool saved_skip_evaluation
;
5290 /* When used as a boolean value, indicates whether this is a
5291 variadic template parameter list. Since it's an int, we can also
5292 subtract it from nparms to get the number of non-variadic
5296 nparms
= TREE_VEC_LENGTH (parms
);
5298 /* Determine if there are any parameter packs. */
5299 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
5301 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
5302 if (template_parameter_pack_p (tparm
))
5306 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
5307 /* If there are 0 or 1 parameter packs, we need to expand any argument
5308 packs so that we can deduce a parameter pack from some non-packed args
5309 followed by an argument pack, as in variadic85.C. If there are more
5310 than that, we need to leave argument packs intact so the arguments are
5311 assigned to the right parameter packs. This should only happen when
5312 dealing with a nested class inside a partial specialization of a class
5313 template, as in variadic92.C. */
5314 if (variadic_p
<= 1)
5315 inner_args
= expand_template_argument_pack (inner_args
);
5317 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5318 if ((nargs
> nparms
&& !variadic_p
)
5319 || (nargs
< nparms
- variadic_p
5321 && (!use_default_args
5322 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
5323 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
5325 if (complain
& tf_error
)
5327 const char *or_more
= "";
5330 or_more
= " or more";
5334 error ("wrong number of template arguments (%d, should be %d%s)",
5335 nargs
, nparms
, or_more
);
5338 error ("provided for %q+D", in_decl
);
5341 return error_mark_node
;
5344 /* We need to evaluate the template arguments, even though this
5345 template-id may be nested within a "sizeof". */
5346 saved_skip_evaluation
= skip_evaluation
;
5347 skip_evaluation
= false;
5348 new_inner_args
= make_tree_vec (nparms
);
5349 new_args
= add_outermost_template_args (args
, new_inner_args
);
5350 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
5355 /* Get the Ith template parameter. */
5356 parm
= TREE_VEC_ELT (parms
, parm_idx
);
5358 if (parm
== error_mark_node
)
5360 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
5364 /* Calculate the next argument. */
5365 if (arg_idx
< nargs
)
5366 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5370 if (template_parameter_pack_p (TREE_VALUE (parm
))
5371 && !(arg
&& ARGUMENT_PACK_P (arg
)))
5373 /* All remaining arguments will be placed in the
5374 template parameter pack PARM. */
5375 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
5376 inner_args
, arg_idx
,
5380 /* Store this argument. */
5381 if (arg
== error_mark_node
)
5383 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
5385 /* We are done with all of the arguments. */
5392 if (PACK_EXPANSION_P (arg
))
5394 if (complain
& tf_error
)
5396 /* FIXME this restriction was removed by N2555; see
5398 /* If ARG is a pack expansion, but PARM is not a
5399 template parameter pack (if it were, we would have
5400 handled it above), we're trying to expand into a
5401 fixed-length argument list. */
5402 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5403 sorry ("cannot expand %<%E%> into a fixed-length "
5404 "argument list", arg
);
5406 sorry ("cannot expand %<%T%> into a fixed-length "
5407 "argument list", arg
);
5409 return error_mark_node
;
5412 else if (require_all_args
)
5413 /* There must be a default arg in this case. */
5414 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
5419 if (arg
== error_mark_node
)
5421 if (complain
& tf_error
)
5422 error ("template argument %d is invalid", arg_idx
+ 1);
5425 /* This only occurs if there was an error in the template
5426 parameter list itself (which we would already have
5427 reported) that we are trying to recover from, e.g., a class
5428 template with a parameter list such as
5429 template<typename..., typename>. */
5430 return error_mark_node
;
5432 arg
= convert_template_argument (TREE_VALUE (parm
),
5433 arg
, new_args
, complain
,
5436 if (arg
== error_mark_node
)
5438 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
5440 skip_evaluation
= saved_skip_evaluation
;
5443 return error_mark_node
;
5445 return new_inner_args
;
5448 /* Returns 1 if template args OT and NT are equivalent. */
5451 template_args_equal (tree ot
, tree nt
)
5456 if (TREE_CODE (nt
) == TREE_VEC
)
5457 /* For member templates */
5458 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
5459 else if (PACK_EXPANSION_P (ot
))
5460 return PACK_EXPANSION_P (nt
)
5461 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
5462 PACK_EXPANSION_PATTERN (nt
));
5463 else if (ARGUMENT_PACK_P (ot
))
5468 if (!ARGUMENT_PACK_P (nt
))
5471 opack
= ARGUMENT_PACK_ARGS (ot
);
5472 npack
= ARGUMENT_PACK_ARGS (nt
);
5473 len
= TREE_VEC_LENGTH (opack
);
5474 if (TREE_VEC_LENGTH (npack
) != len
)
5476 for (i
= 0; i
< len
; ++i
)
5477 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
5478 TREE_VEC_ELT (npack
, i
)))
5482 else if (TYPE_P (nt
))
5483 return TYPE_P (ot
) && same_type_p (ot
, nt
);
5484 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
5487 return cp_tree_equal (ot
, nt
);
5490 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5491 of template arguments. Returns 0 otherwise. */
5494 comp_template_args (tree oldargs
, tree newargs
)
5498 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
5501 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
5503 tree nt
= TREE_VEC_ELT (newargs
, i
);
5504 tree ot
= TREE_VEC_ELT (oldargs
, i
);
5506 if (! template_args_equal (ot
, nt
))
5513 add_pending_template (tree d
)
5515 tree ti
= (TYPE_P (d
)
5516 ? CLASSTYPE_TEMPLATE_INFO (d
)
5517 : DECL_TEMPLATE_INFO (d
));
5518 struct pending_template
*pt
;
5521 if (TI_PENDING_TEMPLATE_FLAG (ti
))
5524 /* We are called both from instantiate_decl, where we've already had a
5525 tinst_level pushed, and instantiate_template, where we haven't.
5527 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
5530 push_tinst_level (d
);
5532 pt
= GGC_NEW (struct pending_template
);
5534 pt
->tinst
= current_tinst_level
;
5535 if (last_pending_template
)
5536 last_pending_template
->next
= pt
;
5538 pending_templates
= pt
;
5540 last_pending_template
= pt
;
5542 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
5549 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5550 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5551 documentation for TEMPLATE_ID_EXPR. */
5554 lookup_template_function (tree fns
, tree arglist
)
5558 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
5559 return error_mark_node
;
5561 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
5562 gcc_assert (fns
&& (is_overloaded_fn (fns
)
5563 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
5565 if (BASELINK_P (fns
))
5567 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
5569 BASELINK_FUNCTIONS (fns
),
5574 type
= TREE_TYPE (fns
);
5575 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
5576 type
= unknown_type_node
;
5578 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
5581 /* Within the scope of a template class S<T>, the name S gets bound
5582 (in build_self_reference) to a TYPE_DECL for the class, not a
5583 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5584 or one of its enclosing classes, and that type is a template,
5585 return the associated TEMPLATE_DECL. Otherwise, the original
5586 DECL is returned. */
5589 maybe_get_template_decl_from_type_decl (tree decl
)
5591 return (decl
!= NULL_TREE
5592 && TREE_CODE (decl
) == TYPE_DECL
5593 && DECL_ARTIFICIAL (decl
)
5594 && CLASS_TYPE_P (TREE_TYPE (decl
))
5595 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
5596 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
5599 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5600 parameters, find the desired type.
5602 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5604 IN_DECL, if non-NULL, is the template declaration we are trying to
5607 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5608 the class we are looking up.
5610 Issue error and warning messages under control of COMPLAIN.
5612 If the template class is really a local class in a template
5613 function, then the FUNCTION_CONTEXT is the function in which it is
5616 ??? Note that this function is currently called *twice* for each
5617 template-id: the first time from the parser, while creating the
5618 incomplete type (finish_template_type), and the second type during the
5619 real instantiation (instantiate_template_class). This is surely something
5620 that we want to avoid. It also causes some problems with argument
5621 coercion (see convert_nontype_argument for more information on this). */
5624 lookup_template_class (tree d1
,
5629 tsubst_flags_t complain
)
5631 tree templ
= NULL_TREE
, parmlist
;
5634 timevar_push (TV_NAME_LOOKUP
);
5636 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
5638 tree value
= innermost_non_namespace_value (d1
);
5639 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
5644 push_decl_namespace (context
);
5645 templ
= lookup_name (d1
);
5646 templ
= maybe_get_template_decl_from_type_decl (templ
);
5648 pop_decl_namespace ();
5651 context
= DECL_CONTEXT (templ
);
5653 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
5655 tree type
= TREE_TYPE (d1
);
5657 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5658 an implicit typename for the second A. Deal with it. */
5659 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
5660 type
= TREE_TYPE (type
);
5662 if (CLASSTYPE_TEMPLATE_INFO (type
))
5664 templ
= CLASSTYPE_TI_TEMPLATE (type
);
5665 d1
= DECL_NAME (templ
);
5668 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
5669 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
5671 templ
= TYPE_TI_TEMPLATE (d1
);
5672 d1
= DECL_NAME (templ
);
5674 else if (TREE_CODE (d1
) == TEMPLATE_DECL
5675 && DECL_TEMPLATE_RESULT (d1
)
5676 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
5679 d1
= DECL_NAME (templ
);
5680 context
= DECL_CONTEXT (templ
);
5683 /* Issue an error message if we didn't find a template. */
5686 if (complain
& tf_error
)
5687 error ("%qT is not a template", d1
);
5688 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5691 if (TREE_CODE (templ
) != TEMPLATE_DECL
5692 /* Make sure it's a user visible template, if it was named by
5694 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
5695 && !PRIMARY_TEMPLATE_P (templ
)))
5697 if (complain
& tf_error
)
5699 error ("non-template type %qT used as a template", d1
);
5701 error ("for template declaration %q+D", in_decl
);
5703 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5706 complain
&= ~tf_user
;
5708 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
5710 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5711 template arguments */
5717 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
5719 /* Consider an example where a template template parameter declared as
5721 template <class T, class U = std::allocator<T> > class TT
5723 The template parameter level of T and U are one level larger than
5724 of TT. To proper process the default argument of U, say when an
5725 instantiation `TT<int>' is seen, we need to build the full
5726 arguments containing {int} as the innermost level. Outer levels,
5727 available when not appearing as default template argument, can be
5728 obtained from the arguments of the enclosing template.
5730 Suppose that TT is later substituted with std::vector. The above
5731 instantiation is `TT<int, std::allocator<T> >' with TT at
5732 level 1, and T at level 2, while the template arguments at level 1
5733 becomes {std::vector} and the inner level 2 is {int}. */
5735 outer
= DECL_CONTEXT (templ
);
5737 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
5738 else if (current_template_parms
)
5739 /* This is an argument of the current template, so we haven't set
5740 DECL_CONTEXT yet. */
5741 outer
= current_template_args ();
5744 arglist
= add_to_template_args (outer
, arglist
);
5746 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
5748 /*require_all_args=*/true,
5749 /*use_default_args=*/true);
5750 if (arglist2
== error_mark_node
5751 || (!uses_template_parms (arglist2
)
5752 && check_instantiated_args (templ
, arglist2
, complain
)))
5753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5755 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
5756 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
5760 tree template_type
= TREE_TYPE (templ
);
5763 tree found
= NULL_TREE
;
5766 int is_partial_instantiation
;
5768 gen_tmpl
= most_general_template (templ
);
5769 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
5770 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
5771 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5773 if (arg_depth
== 1 && parm_depth
> 1)
5775 /* We've been given an incomplete set of template arguments.
5778 template <class T> struct S1 {
5779 template <class U> struct S2 {};
5780 template <class U> struct S2<U*> {};
5783 we will be called with an ARGLIST of `U*', but the
5784 TEMPLATE will be `template <class T> template
5785 <class U> struct S1<T>::S2'. We must fill in the missing
5788 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
5790 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5793 /* Now we should have enough arguments. */
5794 gcc_assert (parm_depth
== arg_depth
);
5796 /* From here on, we're only interested in the most general
5800 /* Calculate the BOUND_ARGS. These will be the args that are
5801 actually tsubst'd into the definition to create the
5805 /* We have multiple levels of arguments to coerce, at once. */
5807 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
5809 tree bound_args
= make_tree_vec (parm_depth
);
5811 for (i
= saved_depth
,
5812 t
= DECL_TEMPLATE_PARMS (templ
);
5813 i
> 0 && t
!= NULL_TREE
;
5814 --i
, t
= TREE_CHAIN (t
))
5816 tree a
= coerce_template_parms (TREE_VALUE (t
),
5819 /*require_all_args=*/true,
5820 /*use_default_args=*/true);
5822 /* Don't process further if one of the levels fails. */
5823 if (a
== error_mark_node
)
5825 /* Restore the ARGLIST to its full size. */
5826 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5827 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5830 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
5832 /* We temporarily reduce the length of the ARGLIST so
5833 that coerce_template_parms will see only the arguments
5834 corresponding to the template parameters it is
5836 TREE_VEC_LENGTH (arglist
)--;
5839 /* Restore the ARGLIST to its full size. */
5840 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5842 arglist
= bound_args
;
5846 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
5847 INNERMOST_TEMPLATE_ARGS (arglist
),
5850 /*require_all_args=*/true,
5851 /*use_default_args=*/true);
5853 if (arglist
== error_mark_node
)
5854 /* We were unable to bind the arguments. */
5855 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5857 /* In the scope of a template class, explicit references to the
5858 template class refer to the type of the template, not any
5859 instantiation of it. For example, in:
5861 template <class T> class C { void f(C<T>); }
5863 the `C<T>' is just the same as `C'. Outside of the
5864 class, however, such a reference is an instantiation. */
5865 if (comp_template_args (TYPE_TI_ARGS (template_type
),
5868 found
= template_type
;
5870 if (!entering_scope
&& PRIMARY_TEMPLATE_P (templ
))
5874 for (ctx
= current_class_type
;
5875 ctx
&& TREE_CODE (ctx
) != NAMESPACE_DECL
;
5877 ? TYPE_CONTEXT (ctx
)
5878 : DECL_CONTEXT (ctx
)))
5879 if (TYPE_P (ctx
) && same_type_p (ctx
, template_type
))
5882 /* We're not in the scope of the class, so the
5883 TEMPLATE_TYPE is not the type we want after all. */
5889 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5891 /* If we already have this specialization, return it. */
5892 found
= retrieve_specialization (templ
, arglist
,
5893 /*class_specializations_p=*/false);
5895 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5897 /* This type is a "partial instantiation" if any of the template
5898 arguments still involve template parameters. Note that we set
5899 IS_PARTIAL_INSTANTIATION for partial specializations as
5901 is_partial_instantiation
= uses_template_parms (arglist
);
5903 /* If the deduced arguments are invalid, then the binding
5905 if (!is_partial_instantiation
5906 && check_instantiated_args (templ
,
5907 INNERMOST_TEMPLATE_ARGS (arglist
),
5909 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5911 if (!is_partial_instantiation
5912 && !PRIMARY_TEMPLATE_P (templ
)
5913 && TREE_CODE (CP_DECL_CONTEXT (templ
)) == NAMESPACE_DECL
)
5915 found
= xref_tag_from_type (TREE_TYPE (templ
),
5917 /*tag_scope=*/ts_global
);
5918 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5921 context
= tsubst (DECL_CONTEXT (templ
), arglist
,
5924 context
= global_namespace
;
5926 /* Create the type. */
5927 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
5929 if (!is_partial_instantiation
)
5931 set_current_access_from_decl (TYPE_NAME (template_type
));
5932 t
= start_enum (TYPE_IDENTIFIER (template_type
),
5933 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
5934 arglist
, complain
, in_decl
),
5935 SCOPED_ENUM_P (template_type
));
5939 /* We don't want to call start_enum for this type, since
5940 the values for the enumeration constants may involve
5941 template parameters. And, no one should be interested
5942 in the enumeration constants for such a type. */
5943 t
= make_node (ENUMERAL_TYPE
);
5944 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
5949 t
= make_class_type (TREE_CODE (template_type
));
5950 CLASSTYPE_DECLARED_CLASS (t
)
5951 = CLASSTYPE_DECLARED_CLASS (template_type
);
5952 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
5953 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
5955 /* A local class. Make sure the decl gets registered properly. */
5956 if (context
== current_function_decl
)
5957 pushtag (DECL_NAME (templ
), t
, /*tag_scope=*/ts_current
);
5959 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
5960 /* This instantiation is another name for the primary
5961 template type. Set the TYPE_CANONICAL field
5963 TYPE_CANONICAL (t
) = template_type
;
5964 else if (any_template_arguments_need_structural_equality_p (arglist
))
5965 /* Some of the template arguments require structural
5966 equality testing, so this template class requires
5967 structural equality testing. */
5968 SET_TYPE_STRUCTURAL_EQUALITY (t
);
5971 /* If we called start_enum or pushtag above, this information
5972 will already be set up. */
5975 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
5977 type_decl
= create_implicit_typedef (DECL_NAME (templ
), t
);
5978 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
5979 TYPE_STUB_DECL (t
) = type_decl
;
5980 DECL_SOURCE_LOCATION (type_decl
)
5981 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
5984 type_decl
= TYPE_NAME (t
);
5986 TREE_PRIVATE (type_decl
)
5987 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
5988 TREE_PROTECTED (type_decl
)
5989 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
5990 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
5992 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
5993 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
5996 /* Set up the template information. We have to figure out which
5997 template is the immediate parent if this is a full
5999 if (parm_depth
== 1 || is_partial_instantiation
6000 || !PRIMARY_TEMPLATE_P (templ
))
6001 /* This case is easy; there are no member templates involved. */
6005 /* This is a full instantiation of a member template. Look
6006 for a partial instantiation of which this is an instance. */
6008 for (found
= DECL_TEMPLATE_INSTANTIATIONS (templ
);
6009 found
; found
= TREE_CHAIN (found
))
6012 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
6014 /* We only want partial instantiations, here, not
6015 specializations or full instantiations. */
6016 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
6017 || !uses_template_parms (TREE_VALUE (found
)))
6020 /* Temporarily reduce by one the number of levels in the
6021 ARGLIST and in FOUND so as to avoid comparing the
6022 last set of arguments. */
6023 TREE_VEC_LENGTH (arglist
)--;
6024 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
6026 /* See if the arguments match. If they do, then TMPL is
6027 the partial instantiation we want. */
6028 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
6030 /* Restore the argument vectors to their full size. */
6031 TREE_VEC_LENGTH (arglist
)++;
6032 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
6043 /* There was no partial instantiation. This happens
6044 where C<T> is a member template of A<T> and it's used
6047 template <typename T> struct B { A<T>::C<int> m; };
6050 Create the partial instantiation.
6052 TREE_VEC_LENGTH (arglist
)--;
6053 found
= tsubst (templ
, arglist
, complain
, NULL_TREE
);
6054 TREE_VEC_LENGTH (arglist
)++;
6058 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
6059 DECL_TEMPLATE_INSTANTIATIONS (templ
)
6060 = tree_cons (arglist
, t
,
6061 DECL_TEMPLATE_INSTANTIATIONS (templ
));
6063 if (TREE_CODE (t
) == ENUMERAL_TYPE
6064 && !is_partial_instantiation
)
6065 /* Now that the type has been registered on the instantiations
6066 list, we set up the enumerators. Because the enumeration
6067 constants may involve the enumeration type itself, we make
6068 sure to register the type first, and then create the
6069 constants. That way, doing tsubst_expr for the enumeration
6070 constants won't result in recursive calls here; we'll find
6071 the instantiation and exit above. */
6072 tsubst_enum (template_type
, t
, arglist
);
6074 if (is_partial_instantiation
)
6075 /* If the type makes use of template parameters, the
6076 code that generates debugging information will crash. */
6077 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
6079 /* Possibly limit visibility based on template args. */
6080 TREE_PUBLIC (type_decl
) = 1;
6081 determine_visibility (type_decl
);
6083 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
6085 timevar_pop (TV_NAME_LOOKUP
);
6092 /* True when we should also visit template parameters that occur in
6093 non-deduced contexts. */
6094 bool include_nondeduced_p
;
6095 struct pointer_set_t
*visited
;
6098 /* Called from for_each_template_parm via walk_tree. */
6101 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
6104 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
6105 tree_fn_t fn
= pfd
->fn
;
6106 void *data
= pfd
->data
;
6109 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
6110 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
6111 pfd
->include_nondeduced_p
))
6112 return error_mark_node
;
6114 switch (TREE_CODE (t
))
6117 if (TYPE_PTRMEMFUNC_P (t
))
6123 if (!TYPE_TEMPLATE_INFO (t
))
6125 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
6126 fn
, data
, pfd
->visited
,
6127 pfd
->include_nondeduced_p
))
6128 return error_mark_node
;
6132 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
6133 fn
, data
, pfd
->visited
,
6134 pfd
->include_nondeduced_p
)
6135 || for_each_template_parm (TYPE_MAX_VALUE (t
),
6136 fn
, data
, pfd
->visited
,
6137 pfd
->include_nondeduced_p
))
6138 return error_mark_node
;
6142 /* Since we're not going to walk subtrees, we have to do this
6144 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
6145 pfd
->visited
, pfd
->include_nondeduced_p
))
6146 return error_mark_node
;
6150 /* Check the return type. */
6151 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6152 pfd
->include_nondeduced_p
))
6153 return error_mark_node
;
6155 /* Check the parameter types. Since default arguments are not
6156 instantiated until they are needed, the TYPE_ARG_TYPES may
6157 contain expressions that involve template parameters. But,
6158 no-one should be looking at them yet. And, once they're
6159 instantiated, they don't contain template parameters, so
6160 there's no point in looking at them then, either. */
6164 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
6165 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
6166 pfd
->visited
, pfd
->include_nondeduced_p
))
6167 return error_mark_node
;
6169 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6170 want walk_tree walking into them itself. */
6176 if (pfd
->include_nondeduced_p
6177 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
6179 pfd
->include_nondeduced_p
))
6180 return error_mark_node
;
6185 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
6186 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
6187 pfd
->visited
, pfd
->include_nondeduced_p
))
6188 return error_mark_node
;
6193 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
6194 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
6195 pfd
->visited
, pfd
->include_nondeduced_p
))
6196 return error_mark_node
;
6197 if (DECL_CONTEXT (t
)
6198 && pfd
->include_nondeduced_p
6199 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
6200 pfd
->visited
, pfd
->include_nondeduced_p
))
6201 return error_mark_node
;
6204 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6205 /* Record template parameters such as `T' inside `TT<T>'. */
6206 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
6207 pfd
->include_nondeduced_p
))
6208 return error_mark_node
;
6211 case TEMPLATE_TEMPLATE_PARM
:
6212 case TEMPLATE_TYPE_PARM
:
6213 case TEMPLATE_PARM_INDEX
:
6214 if (fn
&& (*fn
)(t
, data
))
6215 return error_mark_node
;
6217 return error_mark_node
;
6221 /* A template template parameter is encountered. */
6222 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
6223 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6224 pfd
->include_nondeduced_p
))
6225 return error_mark_node
;
6227 /* Already substituted template template parameter */
6233 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
6235 pfd
->include_nondeduced_p
))
6236 return error_mark_node
;
6240 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
6241 && pfd
->include_nondeduced_p
6242 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6243 (TREE_TYPE (t
)), fn
, data
,
6244 pfd
->visited
, pfd
->include_nondeduced_p
))
6245 return error_mark_node
;
6250 /* If there's no type, then this thing must be some expression
6251 involving template parameters. */
6252 if (!fn
&& !TREE_TYPE (t
))
6253 return error_mark_node
;
6258 case REINTERPRET_CAST_EXPR
:
6259 case CONST_CAST_EXPR
:
6260 case STATIC_CAST_EXPR
:
6261 case DYNAMIC_CAST_EXPR
:
6265 case PSEUDO_DTOR_EXPR
:
6267 return error_mark_node
;
6274 /* We didn't find any template parameters we liked. */
6278 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6279 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6280 call FN with the parameter and the DATA.
6281 If FN returns nonzero, the iteration is terminated, and
6282 for_each_template_parm returns 1. Otherwise, the iteration
6283 continues. If FN never returns a nonzero value, the value
6284 returned by for_each_template_parm is 0. If FN is NULL, it is
6285 considered to be the function which always returns 1.
6287 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6288 parameters that occur in non-deduced contexts. When false, only
6289 visits those template parameters that can be deduced. */
6292 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
6293 struct pointer_set_t
*visited
,
6294 bool include_nondeduced_p
)
6296 struct pair_fn_data pfd
;
6302 pfd
.include_nondeduced_p
= include_nondeduced_p
;
6304 /* Walk the tree. (Conceptually, we would like to walk without
6305 duplicates, but for_each_template_parm_r recursively calls
6306 for_each_template_parm, so we would need to reorganize a fair
6307 bit to use walk_tree_without_duplicates, so we keep our own
6310 pfd
.visited
= visited
;
6312 pfd
.visited
= pointer_set_create ();
6313 result
= cp_walk_tree (&t
,
6314 for_each_template_parm_r
,
6316 pfd
.visited
) != NULL_TREE
;
6321 pointer_set_destroy (pfd
.visited
);
6328 /* Returns true if T depends on any template parameter. */
6331 uses_template_parms (tree t
)
6334 int saved_processing_template_decl
;
6336 saved_processing_template_decl
= processing_template_decl
;
6337 if (!saved_processing_template_decl
)
6338 processing_template_decl
= 1;
6340 dependent_p
= dependent_type_p (t
);
6341 else if (TREE_CODE (t
) == TREE_VEC
)
6342 dependent_p
= any_dependent_template_arguments_p (t
);
6343 else if (TREE_CODE (t
) == TREE_LIST
)
6344 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
6345 || uses_template_parms (TREE_CHAIN (t
)));
6346 else if (TREE_CODE (t
) == TYPE_DECL
)
6347 dependent_p
= dependent_type_p (TREE_TYPE (t
));
6350 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
6351 || TREE_CODE (t
) == OVERLOAD
6352 || TREE_CODE (t
) == BASELINK
6353 || TREE_CODE (t
) == IDENTIFIER_NODE
6354 || TREE_CODE (t
) == TRAIT_EXPR
6355 || CONSTANT_CLASS_P (t
))
6356 dependent_p
= (type_dependent_expression_p (t
)
6357 || value_dependent_expression_p (t
));
6360 gcc_assert (t
== error_mark_node
);
6361 dependent_p
= false;
6364 processing_template_decl
= saved_processing_template_decl
;
6369 /* Returns true if T depends on any template parameter with level LEVEL. */
6372 uses_template_parms_level (tree t
, int level
)
6374 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
6375 /*include_nondeduced_p=*/true);
6378 static int tinst_depth
;
6379 extern int max_tinst_depth
;
6380 #ifdef GATHER_STATISTICS
6383 static int tinst_level_tick
;
6384 static int last_template_error_tick
;
6386 /* We're starting to instantiate D; record the template instantiation context
6387 for diagnostics and to restore it later. */
6390 push_tinst_level (tree d
)
6392 struct tinst_level
*new_level
;
6394 if (tinst_depth
>= max_tinst_depth
)
6396 /* If the instantiation in question still has unbound template parms,
6397 we don't really care if we can't instantiate it, so just return.
6398 This happens with base instantiation for implicit `typename'. */
6399 if (uses_template_parms (d
))
6402 last_template_error_tick
= tinst_level_tick
;
6403 error ("template instantiation depth exceeds maximum of %d (use "
6404 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6405 max_tinst_depth
, d
);
6407 print_instantiation_context ();
6412 new_level
= GGC_NEW (struct tinst_level
);
6413 new_level
->decl
= d
;
6414 new_level
->locus
= input_location
;
6415 new_level
->in_system_header_p
= in_system_header
;
6416 new_level
->next
= current_tinst_level
;
6417 current_tinst_level
= new_level
;
6420 #ifdef GATHER_STATISTICS
6421 if (tinst_depth
> depth_reached
)
6422 depth_reached
= tinst_depth
;
6429 /* We're done instantiating this template; return to the instantiation
6433 pop_tinst_level (void)
6435 /* Restore the filename and line number stashed away when we started
6436 this instantiation. */
6437 input_location
= current_tinst_level
->locus
;
6438 current_tinst_level
= current_tinst_level
->next
;
6443 /* We're instantiating a deferred template; restore the template
6444 instantiation context in which the instantiation was requested, which
6445 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6448 reopen_tinst_level (struct tinst_level
*level
)
6450 struct tinst_level
*t
;
6453 for (t
= level
; t
; t
= t
->next
)
6456 current_tinst_level
= level
;
6461 /* Returns the TINST_LEVEL which gives the original instantiation
6464 struct tinst_level
*
6465 outermost_tinst_level (void)
6467 struct tinst_level
*level
= current_tinst_level
;
6470 level
= level
->next
;
6474 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6477 parameter_of_template_p (tree parm
, tree templ
)
6482 if (!parm
|| !templ
)
6485 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
6486 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
6488 parms
= DECL_TEMPLATE_PARMS (templ
);
6489 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
6491 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
6492 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
6498 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6499 vector of template arguments, as for tsubst.
6501 Returns an appropriate tsubst'd friend declaration. */
6504 tsubst_friend_function (tree decl
, tree args
)
6508 if (TREE_CODE (decl
) == FUNCTION_DECL
6509 && DECL_TEMPLATE_INSTANTIATION (decl
)
6510 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
6511 /* This was a friend declared with an explicit template
6512 argument list, e.g.:
6516 to indicate that f was a template instantiation, not a new
6517 function declaration. Now, we have to figure out what
6518 instantiation of what template. */
6520 tree template_id
, arglist
, fns
;
6523 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
6525 /* Friend functions are looked up in the containing namespace scope.
6526 We must enter that scope, to avoid finding member functions of the
6527 current class with same name. */
6528 push_nested_namespace (ns
);
6529 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
6530 tf_warning_or_error
, NULL_TREE
,
6531 /*integral_constant_expression_p=*/false);
6532 pop_nested_namespace (ns
);
6533 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
6534 tf_warning_or_error
, NULL_TREE
);
6535 template_id
= lookup_template_function (fns
, arglist
);
6537 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6538 tmpl
= determine_specialization (template_id
, new_friend
,
6540 /*need_member_template=*/0,
6541 TREE_VEC_LENGTH (args
),
6543 return instantiate_template (tmpl
, new_args
, tf_error
);
6546 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6548 /* The NEW_FRIEND will look like an instantiation, to the
6549 compiler, but is not an instantiation from the point of view of
6550 the language. For example, we might have had:
6552 template <class T> struct S {
6553 template <class U> friend void f(T, U);
6556 Then, in S<int>, template <class U> void f(int, U) is not an
6557 instantiation of anything. */
6558 if (new_friend
== error_mark_node
)
6559 return error_mark_node
;
6561 DECL_USE_TEMPLATE (new_friend
) = 0;
6562 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6564 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
6565 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
6566 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
6569 /* The mangled name for the NEW_FRIEND is incorrect. The function
6570 is not a template instantiation and should not be mangled like
6571 one. Therefore, we forget the mangling here; we'll recompute it
6572 later if we need it. */
6573 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
6575 SET_DECL_RTL (new_friend
, NULL_RTX
);
6576 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
6579 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
6582 tree new_friend_template_info
;
6583 tree new_friend_result_template_info
;
6585 int new_friend_is_defn
;
6587 /* We must save some information from NEW_FRIEND before calling
6588 duplicate decls since that function will free NEW_FRIEND if
6590 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
6591 new_friend_is_defn
=
6592 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6593 (template_for_substitution (new_friend
)))
6595 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
6597 /* This declaration is a `primary' template. */
6598 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
6600 new_friend_result_template_info
6601 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
6604 new_friend_result_template_info
= NULL_TREE
;
6606 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6607 if (new_friend_is_defn
)
6608 DECL_INITIAL (new_friend
) = error_mark_node
;
6610 /* Inside pushdecl_namespace_level, we will push into the
6611 current namespace. However, the friend function should go
6612 into the namespace of the template. */
6613 ns
= decl_namespace_context (new_friend
);
6614 push_nested_namespace (ns
);
6615 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
6616 pop_nested_namespace (ns
);
6618 if (old_decl
== error_mark_node
)
6619 return error_mark_node
;
6621 if (old_decl
!= new_friend
)
6623 /* This new friend declaration matched an existing
6624 declaration. For example, given:
6626 template <class T> void f(T);
6627 template <class U> class C {
6628 template <class T> friend void f(T) {}
6631 the friend declaration actually provides the definition
6632 of `f', once C has been instantiated for some type. So,
6633 old_decl will be the out-of-class template declaration,
6634 while new_friend is the in-class definition.
6636 But, if `f' was called before this point, the
6637 instantiation of `f' will have DECL_TI_ARGS corresponding
6638 to `T' but not to `U', references to which might appear
6639 in the definition of `f'. Previously, the most general
6640 template for an instantiation of `f' was the out-of-class
6641 version; now it is the in-class version. Therefore, we
6642 run through all specialization of `f', adding to their
6643 DECL_TI_ARGS appropriately. In particular, they need a
6644 new set of outer arguments, corresponding to the
6645 arguments for this class instantiation.
6647 The same situation can arise with something like this:
6650 template <class T> class C {
6654 when `C<int>' is instantiated. Now, `f(int)' is defined
6657 if (!new_friend_is_defn
)
6658 /* On the other hand, if the in-class declaration does
6659 *not* provide a definition, then we don't want to alter
6660 existing definitions. We can just leave everything
6665 /* Overwrite whatever template info was there before, if
6666 any, with the new template information pertaining to
6668 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
6670 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
6671 reregister_specialization (new_friend
,
6672 most_general_template (old_decl
),
6677 tree new_friend_args
;
6679 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
6680 = new_friend_result_template_info
;
6682 new_friend_args
= TI_ARGS (new_friend_template_info
);
6683 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
6687 tree spec
= TREE_VALUE (t
);
6690 = add_outermost_template_args (new_friend_args
,
6691 DECL_TI_ARGS (spec
));
6694 /* Now, since specializations are always supposed to
6695 hang off of the most general template, we must move
6697 t
= most_general_template (old_decl
);
6700 DECL_TEMPLATE_SPECIALIZATIONS (t
)
6701 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
6702 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
6703 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
6708 /* The information from NEW_FRIEND has been merged into OLD_DECL
6709 by duplicate_decls. */
6710 new_friend
= old_decl
;
6715 tree context
= DECL_CONTEXT (new_friend
);
6719 template <class T> class C {
6720 template <class U> friend void C1<U>::f (); // case 1
6721 friend void C2<T>::f (); // case 2
6723 we only need to make sure CONTEXT is a complete type for
6724 case 2. To distinguish between the two cases, we note that
6725 CONTEXT of case 1 remains dependent type after tsubst while
6726 this isn't true for case 2. */
6727 ++processing_template_decl
;
6728 dependent_p
= dependent_type_p (context
);
6729 --processing_template_decl
;
6732 && !complete_type_or_else (context
, NULL_TREE
))
6733 return error_mark_node
;
6735 if (COMPLETE_TYPE_P (context
))
6737 /* Check to see that the declaration is really present, and,
6738 possibly obtain an improved declaration. */
6739 tree fn
= check_classfn (context
,
6740 new_friend
, NULL_TREE
);
6750 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6751 template arguments, as for tsubst.
6753 Returns an appropriate tsubst'd friend type or error_mark_node on
6757 tsubst_friend_class (tree friend_tmpl
, tree args
)
6763 context
= DECL_CONTEXT (friend_tmpl
);
6767 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6768 push_nested_namespace (context
);
6770 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
6773 /* Look for a class template declaration. We look for hidden names
6774 because two friend declarations of the same template are the
6775 same. For example, in:
6778 template <typename> friend class F;
6780 template <typename> struct B {
6781 template <typename> friend class F;
6784 both F templates are the same. */
6785 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
6786 /*block_p=*/true, 0,
6787 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
6789 /* But, if we don't find one, it might be because we're in a
6790 situation like this:
6798 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6799 for `S<int>', not the TEMPLATE_DECL. */
6800 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
6802 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
6803 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
6806 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
6808 /* The friend template has already been declared. Just
6809 check to see that the declarations match, and install any new
6810 default parameters. We must tsubst the default parameters,
6811 of course. We only need the innermost template parameters
6812 because that is all that redeclare_class_template will look
6814 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
6815 > TMPL_ARGS_DEPTH (args
))
6818 location_t saved_input_location
;
6819 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
6820 args
, tf_warning_or_error
);
6822 saved_input_location
= input_location
;
6823 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
6824 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
6825 input_location
= saved_input_location
;
6829 friend_type
= TREE_TYPE (tmpl
);
6833 /* The friend template has not already been declared. In this
6834 case, the instantiation of the template class will cause the
6835 injection of this template into the global scope. */
6836 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
6837 if (tmpl
== error_mark_node
)
6838 return error_mark_node
;
6840 /* The new TMPL is not an instantiation of anything, so we
6841 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6842 the new type because that is supposed to be the corresponding
6843 template decl, i.e., TMPL. */
6844 DECL_USE_TEMPLATE (tmpl
) = 0;
6845 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
6846 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
6847 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
6848 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
6850 /* Inject this template into the global scope. */
6851 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
6856 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6857 pop_nested_namespace (context
);
6859 pop_nested_class ();
6865 /* Returns zero if TYPE cannot be completed later due to circularity.
6866 Otherwise returns one. */
6869 can_complete_type_without_circularity (tree type
)
6871 if (type
== NULL_TREE
|| type
== error_mark_node
)
6873 else if (COMPLETE_TYPE_P (type
))
6875 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
6876 return can_complete_type_without_circularity (TREE_TYPE (type
));
6877 else if (CLASS_TYPE_P (type
)
6878 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
6884 /* Apply any attributes which had to be deferred until instantiation
6885 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6886 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6889 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
6890 tree args
, tsubst_flags_t complain
, tree in_decl
)
6892 tree last_dep
= NULL_TREE
;
6896 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
6897 if (ATTR_IS_DEPENDENT (t
))
6900 attributes
= copy_list (attributes
);
6904 if (DECL_P (*decl_p
))
6906 if (TREE_TYPE (*decl_p
) == error_mark_node
)
6908 p
= &DECL_ATTRIBUTES (*decl_p
);
6911 p
= &TYPE_ATTRIBUTES (*decl_p
);
6915 tree late_attrs
= NULL_TREE
;
6916 tree
*q
= &late_attrs
;
6918 for (*p
= attributes
; *p
; )
6921 if (ATTR_IS_DEPENDENT (t
))
6923 *p
= TREE_CHAIN (t
);
6924 TREE_CHAIN (t
) = NULL_TREE
;
6925 /* If this is a lock attribute with arguments, we defer the
6926 instantiation until the whole class specification has been
6927 instantiated because the lock attribute arguments could
6928 reference data members declared later (lexically). */
6930 && is_lock_attribute_with_args (TREE_PURPOSE (t
)))
6932 struct pending_attribute
*pa
=
6933 GGC_NEW (struct pending_attribute
);
6936 pa
->attr_flags
= attr_flags
;
6938 pa
->complain
= complain
;
6939 pa
->in_decl
= in_decl
;
6940 pa
->next
= pending_lock_attributes
;
6941 pending_lock_attributes
= pa
;
6944 /* If the first attribute argument is an identifier, don't
6945 pass it through tsubst. Attributes like mode, format,
6946 cleanup and several target specific attributes expect it
6949 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
6950 && TREE_VALUE (TREE_VALUE (t
))
6951 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
6952 == IDENTIFIER_NODE
))
6955 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
6957 /*integral_constant_expression_p=*/false);
6958 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
6960 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
6965 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
6966 /*integral_constant_expression_p=*/false);
6968 q
= &TREE_CHAIN (t
);
6971 p
= &TREE_CHAIN (t
);
6974 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
6978 /* Reverse the order of elements in the pending attribute list, PA_LIST,
6979 and return the new head of the list (old last element). */
6981 static struct pending_attribute
*
6982 pa_reverse (struct pending_attribute
*pa_list
)
6984 struct pending_attribute
*prev
= NULL
, *curr
, *next
;
6985 for (curr
= pa_list
; curr
; curr
= next
)
6995 instantiate_class_template (tree type
)
6997 tree templ
, args
, pattern
, t
, member
;
7002 if (type
== error_mark_node
)
7003 return error_mark_node
;
7005 if (TYPE_BEING_DEFINED (type
)
7006 || COMPLETE_TYPE_P (type
)
7007 || dependent_type_p (type
))
7010 /* Figure out which template is being instantiated. */
7011 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
7012 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7014 /* Determine what specialization of the original template to
7016 t
= most_specialized_class (type
, templ
);
7017 if (t
== error_mark_node
)
7019 TYPE_BEING_DEFINED (type
) = 1;
7020 return error_mark_node
;
7024 /* This TYPE is actually an instantiation of a partial
7025 specialization. We replace the innermost set of ARGS with
7026 the arguments appropriate for substitution. For example,
7029 template <class T> struct S {};
7030 template <class T> struct S<T*> {};
7032 and supposing that we are instantiating S<int*>, ARGS will
7033 presently be {int*} -- but we need {int}. */
7034 pattern
= TREE_TYPE (t
);
7035 args
= TREE_PURPOSE (t
);
7039 pattern
= TREE_TYPE (templ
);
7040 args
= CLASSTYPE_TI_ARGS (type
);
7043 /* If the template we're instantiating is incomplete, then clearly
7044 there's nothing we can do. */
7045 if (!COMPLETE_TYPE_P (pattern
))
7048 /* If we've recursively instantiated too many templates, stop. */
7049 if (! push_tinst_level (type
))
7052 /* Now we're really doing the instantiation. Mark the type as in
7053 the process of being defined. */
7054 TYPE_BEING_DEFINED (type
) = 1;
7056 /* We may be in the middle of deferred access check. Disable
7058 push_deferring_access_checks (dk_no_deferred
);
7060 push_to_top_level ();
7062 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
7064 /* Set the input location to the template definition. This is needed
7065 if tsubsting causes an error. */
7066 typedecl
= TYPE_MAIN_DECL (type
);
7067 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7069 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
7070 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
7071 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
7072 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
7073 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
7074 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
7075 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
7076 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
7077 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
7078 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
7079 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
7080 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
7081 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
7082 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
7083 if (ANON_AGGR_TYPE_P (pattern
))
7084 SET_ANON_AGGR_TYPE_P (type
);
7085 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
7087 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
7088 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
7091 pbinfo
= TYPE_BINFO (pattern
);
7093 /* We should never instantiate a nested class before its enclosing
7094 class; we need to look up the nested class by name before we can
7095 instantiate it, and that lookup should instantiate the enclosing
7097 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
7098 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
7099 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
7101 base_list
= NULL_TREE
;
7102 if (BINFO_N_BASE_BINFOS (pbinfo
))
7105 tree context
= TYPE_CONTEXT (type
);
7109 /* We must enter the scope containing the type, as that is where
7110 the accessibility of types named in dependent bases are
7112 pushed_scope
= push_scope (context
? context
: global_namespace
);
7114 /* Substitute into each of the bases to determine the actual
7116 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
7119 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
7120 tree expanded_bases
= NULL_TREE
;
7123 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
7126 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
7127 args
, tf_error
, NULL_TREE
);
7128 if (expanded_bases
== error_mark_node
)
7131 len
= TREE_VEC_LENGTH (expanded_bases
);
7134 for (idx
= 0; idx
< len
; idx
++)
7137 /* Extract the already-expanded base class. */
7138 base
= TREE_VEC_ELT (expanded_bases
, idx
);
7140 /* Substitute to figure out the base class. */
7141 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
7144 if (base
== error_mark_node
)
7147 base_list
= tree_cons (access
, base
, base_list
);
7148 if (BINFO_VIRTUAL_P (pbase_binfo
))
7149 TREE_TYPE (base_list
) = integer_type_node
;
7153 /* The list is now in reverse order; correct that. */
7154 base_list
= nreverse (base_list
);
7157 pop_scope (pushed_scope
);
7159 /* Now call xref_basetypes to set up all the base-class
7161 xref_basetypes (type
, base_list
);
7163 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
7164 (int) ATTR_FLAG_TYPE_IN_PLACE
,
7165 args
, tf_error
, NULL_TREE
);
7167 /* Now that our base classes are set up, enter the scope of the
7168 class, so that name lookups into base classes, etc. will work
7169 correctly. This is precisely analogous to what we do in
7170 begin_class_definition when defining an ordinary non-template
7171 class, except we also need to push the enclosing classes. */
7172 push_nested_class (type
);
7174 /* Now members are processed in the order of declaration. */
7175 for (member
= CLASSTYPE_DECL_LIST (pattern
);
7176 member
; member
= TREE_CHAIN (member
))
7178 tree t
= TREE_VALUE (member
);
7180 if (TREE_PURPOSE (member
))
7184 /* Build new CLASSTYPE_NESTED_UTDS. */
7187 bool class_template_p
;
7189 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
7190 && TYPE_LANG_SPECIFIC (t
)
7191 && CLASSTYPE_IS_TEMPLATE (t
));
7192 /* If the member is a class template, then -- even after
7193 substitution -- there may be dependent types in the
7194 template argument list for the class. We increment
7195 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7196 that function will assume that no types are dependent
7197 when outside of a template. */
7198 if (class_template_p
)
7199 ++processing_template_decl
;
7200 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7201 if (class_template_p
)
7202 --processing_template_decl
;
7203 if (newtag
== error_mark_node
)
7206 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
7208 tree name
= TYPE_IDENTIFIER (t
);
7210 if (class_template_p
)
7211 /* Unfortunately, lookup_template_class sets
7212 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7213 instantiation (i.e., for the type of a member
7214 template class nested within a template class.)
7215 This behavior is required for
7216 maybe_process_partial_specialization to work
7217 correctly, but is not accurate in this case;
7218 the TAG is not an instantiation of anything.
7219 (The corresponding TEMPLATE_DECL is an
7220 instantiation, but the TYPE is not.) */
7221 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
7223 /* Now, we call pushtag to put this NEWTAG into the scope of
7224 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7225 pushtag calling push_template_decl. We don't have to do
7226 this for enums because it will already have been done in
7229 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
7230 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
7233 else if (TREE_CODE (t
) == FUNCTION_DECL
7234 || DECL_FUNCTION_TEMPLATE_P (t
))
7236 /* Build new TYPE_METHODS. */
7239 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7240 ++processing_template_decl
;
7241 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7242 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7243 --processing_template_decl
;
7244 set_current_access_from_decl (r
);
7245 finish_member_declaration (r
);
7249 /* Build new TYPE_FIELDS. */
7250 if (TREE_CODE (t
) == STATIC_ASSERT
)
7253 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
7254 tf_warning_or_error
, NULL_TREE
,
7255 /*integral_constant_expression_p=*/true);
7256 finish_static_assert (condition
,
7257 STATIC_ASSERT_MESSAGE (t
),
7258 STATIC_ASSERT_SOURCE_LOCATION (t
),
7261 else if (TREE_CODE (t
) != CONST_DECL
)
7265 /* The file and line for this declaration, to
7266 assist in error message reporting. Since we
7267 called push_tinst_level above, we don't need to
7269 input_location
= DECL_SOURCE_LOCATION (t
);
7271 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7272 ++processing_template_decl
;
7273 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
7274 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7275 --processing_template_decl
;
7276 if (TREE_CODE (r
) == VAR_DECL
)
7280 [t]he initialization (and any associated
7281 side-effects) of a static data member does
7282 not occur unless the static data member is
7283 itself used in a way that requires the
7284 definition of the static data member to
7287 Therefore, we do not substitute into the
7288 initialized for the static data member here. */
7289 finish_static_data_member_decl
7292 /*init_const_expr_p=*/false,
7293 /*asmspec_tree=*/NULL_TREE
,
7295 if (DECL_INITIALIZED_IN_CLASS_P (r
))
7296 check_static_variable_definition (r
, TREE_TYPE (r
));
7298 else if (TREE_CODE (r
) == FIELD_DECL
)
7300 /* Determine whether R has a valid type and can be
7301 completed later. If R is invalid, then it is
7302 replaced by error_mark_node so that it will not be
7303 added to TYPE_FIELDS. */
7304 tree rtype
= TREE_TYPE (r
);
7305 if (can_complete_type_without_circularity (rtype
))
7306 complete_type (rtype
);
7308 if (!COMPLETE_TYPE_P (rtype
))
7310 cxx_incomplete_type_error (r
, rtype
);
7311 r
= error_mark_node
;
7315 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7316 such a thing will already have been added to the field
7317 list by tsubst_enum in finish_member_declaration in the
7318 CLASSTYPE_NESTED_UTDS case above. */
7319 if (!(TREE_CODE (r
) == TYPE_DECL
7320 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
7321 && DECL_ARTIFICIAL (r
)))
7323 set_current_access_from_decl (r
);
7324 finish_member_declaration (r
);
7331 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
7333 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7335 tree friend_type
= t
;
7336 bool adjust_processing_template_decl
= false;
7338 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7340 /* template <class T> friend class C; */
7341 friend_type
= tsubst_friend_class (friend_type
, args
);
7342 adjust_processing_template_decl
= true;
7344 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
7346 /* template <class T> friend class C::D; */
7347 friend_type
= tsubst (friend_type
, args
,
7348 tf_warning_or_error
, NULL_TREE
);
7349 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
7350 friend_type
= TREE_TYPE (friend_type
);
7351 adjust_processing_template_decl
= true;
7353 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
7355 /* This could be either
7359 when dependent_type_p is false or
7361 template <class U> friend class T::C;
7364 friend_type
= tsubst (friend_type
, args
,
7365 tf_warning_or_error
, NULL_TREE
);
7366 /* Bump processing_template_decl for correct
7367 dependent_type_p calculation. */
7368 ++processing_template_decl
;
7369 if (dependent_type_p (friend_type
))
7370 adjust_processing_template_decl
= true;
7371 --processing_template_decl
;
7373 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
7374 && hidden_name_p (TYPE_NAME (friend_type
)))
7378 where C hasn't been declared yet. Let's lookup name
7379 from namespace scope directly, bypassing any name that
7380 come from dependent base class. */
7381 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
7383 /* The call to xref_tag_from_type does injection for friend
7385 push_nested_namespace (ns
);
7387 xref_tag_from_type (friend_type
, NULL_TREE
,
7388 /*tag_scope=*/ts_current
);
7389 pop_nested_namespace (ns
);
7391 else if (uses_template_parms (friend_type
))
7392 /* friend class C<T>; */
7393 friend_type
= tsubst (friend_type
, args
,
7394 tf_warning_or_error
, NULL_TREE
);
7399 where C is already declared or
7401 friend class C<int>;
7403 We don't have to do anything in these cases. */
7405 if (adjust_processing_template_decl
)
7406 /* Trick make_friend_class into realizing that the friend
7407 we're adding is a template, not an ordinary class. It's
7408 important that we use make_friend_class since it will
7409 perform some error-checking and output cross-reference
7411 ++processing_template_decl
;
7413 if (friend_type
!= error_mark_node
)
7414 make_friend_class (type
, friend_type
, /*complain=*/false);
7416 if (adjust_processing_template_decl
)
7417 --processing_template_decl
;
7421 /* Build new DECL_FRIENDLIST. */
7424 /* The file and line for this declaration, to
7425 assist in error message reporting. Since we
7426 called push_tinst_level above, we don't need to
7428 input_location
= DECL_SOURCE_LOCATION (t
);
7430 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7432 ++processing_template_decl
;
7433 push_deferring_access_checks (dk_no_check
);
7436 r
= tsubst_friend_function (t
, args
);
7437 add_friend (type
, r
, /*complain=*/false);
7438 if (TREE_CODE (t
) == TEMPLATE_DECL
)
7440 pop_deferring_access_checks ();
7441 --processing_template_decl
;
7447 /* Set the file and line number information to whatever is given for
7448 the class itself. This puts error messages involving generated
7449 implicit functions at a predictable point, and the same point
7450 that would be used for non-template classes. */
7451 input_location
= DECL_SOURCE_LOCATION (typedecl
);
7453 unreverse_member_declarations (type
);
7454 finish_struct_1 (type
);
7455 TYPE_BEING_DEFINED (type
) = 0;
7457 /* Now that the class is complete, instantiate default arguments for
7458 any member functions. We don't do this earlier because the
7459 default arguments may reference members of the class. */
7460 if (!PRIMARY_TEMPLATE_P (templ
))
7461 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
7462 if (TREE_CODE (t
) == FUNCTION_DECL
7463 /* Implicitly generated member functions will not have template
7464 information; they are not instantiations, but instead are
7465 created "fresh" for each instantiation. */
7466 && DECL_TEMPLATE_INFO (t
))
7467 tsubst_default_arguments (t
);
7469 /* Instantiate the deferred lock attributes and apply them to the
7470 corresponding decls. We don't do this earlier because the lock
7471 attribute arguments may reference data members of the class. */
7472 if (pending_lock_attributes
)
7474 struct pending_attribute
*pa
= pa_reverse (pending_lock_attributes
);
7475 location_t saved_location
= input_location
;
7476 parsing_lock_attribute
= true;
7477 for ( ; pa
; pa
= pa
->next
)
7479 tree t
= pa
->attributes
;
7480 input_location
= DECL_SOURCE_LOCATION (pa
->decl
);
7482 = tsubst_expr (TREE_VALUE (t
), pa
->args
, pa
->complain
,
7484 /*integral_constant_expression_p=*/false);
7485 cplus_decl_attributes (&pa
->decl
, t
, pa
->attr_flags
);
7487 parsing_lock_attribute
= false;
7488 pending_lock_attributes
= NULL
;
7489 input_location
= saved_location
;
7492 pop_nested_class ();
7493 pop_from_top_level ();
7494 pop_deferring_access_checks ();
7497 /* The vtable for a template class can be emitted in any translation
7498 unit in which the class is instantiated. When there is no key
7499 method, however, finish_struct_1 will already have added TYPE to
7500 the keyed_classes list. */
7501 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
7502 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
7508 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7514 else if (TYPE_P (t
))
7515 r
= tsubst (t
, args
, complain
, in_decl
);
7518 r
= tsubst_expr (t
, args
, complain
, in_decl
,
7519 /*integral_constant_expression_p=*/true);
7520 r
= fold_non_dependent_expr (r
);
7525 /* Substitute ARGS into T, which is an pack expansion
7526 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7527 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7528 (if only a partial substitution could be performed) or
7529 ERROR_MARK_NODE if there was an error. */
7531 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
7535 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
7536 tree first_arg_pack
; int i
, len
= -1;
7540 gcc_assert (PACK_EXPANSION_P (t
));
7541 pattern
= PACK_EXPANSION_PATTERN (t
);
7543 /* Determine the argument packs that will instantiate the parameter
7544 packs used in the expansion expression. While we're at it,
7545 compute the number of arguments to be expanded and make sure it
7547 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
7548 pack
= TREE_CHAIN (pack
))
7550 tree parm_pack
= TREE_VALUE (pack
);
7551 tree arg_pack
= NULL_TREE
;
7552 tree orig_arg
= NULL_TREE
;
7554 if (TREE_CODE (parm_pack
) == PARM_DECL
)
7555 arg_pack
= retrieve_local_specialization (parm_pack
);
7558 int level
, idx
, levels
;
7559 template_parm_level_and_index (parm_pack
, &level
, &idx
);
7561 levels
= TMPL_ARGS_DEPTH (args
);
7562 if (level
<= levels
)
7563 arg_pack
= TMPL_ARG (args
, level
, idx
);
7566 orig_arg
= arg_pack
;
7567 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
7568 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
7570 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
7571 /* This can only happen if we forget to expand an argument
7572 pack somewhere else. Just return an error, silently. */
7574 result
= make_tree_vec (1);
7575 TREE_VEC_ELT (result
, 0) = error_mark_node
;
7580 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
7581 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
7583 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
7584 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
7585 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
7586 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
7587 /* The argument pack that the parameter maps to is just an
7588 expansion of the parameter itself, such as one would
7589 find in the implicit typedef of a class inside the
7590 class itself. Consider this parameter "unsubstituted",
7591 so that we will maintain the outer pack expansion. */
7592 arg_pack
= NULL_TREE
;
7598 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
7600 /* It's all-or-nothing with incomplete argument packs. */
7601 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7602 return error_mark_node
;
7604 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7610 first_arg_pack
= arg_pack
;
7612 else if (len
!= my_len
)
7614 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
7615 error ("mismatched argument pack lengths while expanding "
7619 error ("mismatched argument pack lengths while expanding "
7622 return error_mark_node
;
7625 /* Keep track of the parameter packs and their corresponding
7627 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
7628 TREE_TYPE (packs
) = orig_arg
;
7631 /* We can't substitute for this parameter pack. */
7632 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
7634 unsubstituted_packs
);
7637 /* We cannot expand this expansion expression, because we don't have
7638 all of the argument packs we need. Substitute into the pattern
7639 and return a PACK_EXPANSION_*. The caller will need to deal with
7641 if (unsubstituted_packs
)
7642 return make_pack_expansion (tsubst (pattern
, args
, complain
,
7645 /* We could not find any argument packs that work. */
7647 return error_mark_node
;
7649 /* For each argument in each argument pack, substitute into the
7651 result
= make_tree_vec (len
+ incomplete
);
7652 for (i
= 0; i
< len
+ incomplete
; ++i
)
7654 /* For parameter pack, change the substitution of the parameter
7655 pack to the ith argument in its argument pack, then expand
7657 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7659 tree parm
= TREE_PURPOSE (pack
);
7661 if (TREE_CODE (parm
) == PARM_DECL
)
7663 /* Select the Ith argument from the pack. */
7664 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
7665 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
7666 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
7668 register_local_specialization (arg
, parm
);
7674 template_parm_level_and_index (parm
, &level
, &idx
);
7678 /* Select the Ith argument from the pack. */
7679 value
= make_node (ARGUMENT_PACK_SELECT
);
7680 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
7681 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
7684 /* Update the corresponding argument. */
7685 TMPL_ARG (args
, level
, idx
) = value
;
7689 /* Substitute into the PATTERN with the altered arguments. */
7690 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
7691 TREE_VEC_ELT (result
, i
) =
7692 tsubst_expr (pattern
, args
, complain
, in_decl
,
7693 /*integral_constant_expression_p=*/false);
7695 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
7698 /* When we have incomplete argument packs, the last "expanded"
7699 result is itself a pack expansion, which allows us
7700 to deduce more arguments. */
7701 TREE_VEC_ELT (result
, i
) =
7702 make_pack_expansion (TREE_VEC_ELT (result
, i
));
7704 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
7706 result
= error_mark_node
;
7711 /* Update ARGS to restore the substitution from parameter packs to
7712 their argument packs. */
7713 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7715 tree parm
= TREE_PURPOSE (pack
);
7717 if (TREE_CODE (parm
) == PARM_DECL
)
7718 register_local_specialization (TREE_TYPE (pack
), parm
);
7722 template_parm_level_and_index (parm
, &level
, &idx
);
7724 /* Update the corresponding argument. */
7725 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
7726 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
7729 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
7736 /* Substitute ARGS into the vector or list of template arguments T. */
7739 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7742 int len
= TREE_VEC_LENGTH (t
);
7743 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
7744 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
7746 for (i
= 0; i
< len
; i
++)
7748 tree orig_arg
= TREE_VEC_ELT (t
, i
);
7751 if (TREE_CODE (orig_arg
) == TREE_VEC
)
7752 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
7753 else if (PACK_EXPANSION_P (orig_arg
))
7755 /* Substitute into an expansion expression. */
7756 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
7758 if (TREE_CODE (new_arg
) == TREE_VEC
)
7759 /* Add to the expanded length adjustment the number of
7760 expanded arguments. We subtract one from this
7761 measurement, because the argument pack expression
7762 itself is already counted as 1 in
7763 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7764 the argument pack is empty. */
7765 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
7767 else if (ARGUMENT_PACK_P (orig_arg
))
7769 /* Substitute into each of the arguments. */
7770 new_arg
= make_node (TREE_CODE (orig_arg
));
7772 SET_ARGUMENT_PACK_ARGS (
7774 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
7775 args
, complain
, in_decl
));
7777 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
7778 new_arg
= error_mark_node
;
7780 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
7781 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
7783 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
7785 if (TREE_TYPE (new_arg
) == error_mark_node
)
7786 new_arg
= error_mark_node
;
7790 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
7792 if (new_arg
== error_mark_node
)
7793 return error_mark_node
;
7796 if (new_arg
!= orig_arg
)
7803 /* Make space for the expanded arguments coming from template
7805 t
= make_tree_vec (len
+ expanded_len_adjust
);
7806 for (i
= 0, out
= 0; i
< len
; i
++)
7808 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
7809 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
7810 && TREE_CODE (elts
[i
]) == TREE_VEC
)
7814 /* Now expand the template argument pack "in place". */
7815 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
7816 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
7820 TREE_VEC_ELT (t
, out
) = elts
[i
];
7828 /* Return the result of substituting ARGS into the template parameters
7829 given by PARMS. If there are m levels of ARGS and m + n levels of
7830 PARMS, then the result will contain n levels of PARMS. For
7831 example, if PARMS is `template <class T> template <class U>
7832 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7833 result will be `template <int*, double, class V>'. */
7836 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
7841 /* When substituting into a template, we must set
7842 PROCESSING_TEMPLATE_DECL as the template parameters may be
7843 dependent if they are based on one-another, and the dependency
7844 predicates are short-circuit outside of templates. */
7845 ++processing_template_decl
;
7847 for (new_parms
= &r
;
7848 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
7849 new_parms
= &(TREE_CHAIN (*new_parms
)),
7850 parms
= TREE_CHAIN (parms
))
7853 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
7856 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
7862 if (parms
== error_mark_node
)
7865 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
7867 if (tuple
== error_mark_node
)
7870 default_value
= TREE_PURPOSE (tuple
);
7871 parm_decl
= TREE_VALUE (tuple
);
7873 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
7874 if (TREE_CODE (parm_decl
) == PARM_DECL
7875 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
7876 parm_decl
= error_mark_node
;
7877 default_value
= tsubst_template_arg (default_value
, args
,
7878 complain
, NULL_TREE
);
7880 tuple
= build_tree_list (default_value
, parm_decl
);
7881 TREE_VEC_ELT (new_vec
, i
) = tuple
;
7885 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
7886 - TMPL_ARGS_DEPTH (args
)),
7887 new_vec
, NULL_TREE
);
7890 --processing_template_decl
;
7895 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7896 type T. If T is not an aggregate or enumeration type, it is
7897 handled as if by tsubst. IN_DECL is as for tsubst. If
7898 ENTERING_SCOPE is nonzero, T is the context for a template which
7899 we are presently tsubst'ing. Return the substituted value. */
7902 tsubst_aggr_type (tree t
,
7904 tsubst_flags_t complain
,
7911 switch (TREE_CODE (t
))
7914 if (TYPE_PTRMEMFUNC_P (t
))
7915 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
7917 /* Else fall through. */
7920 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
7925 bool saved_skip_evaluation
;
7927 /* In "sizeof(X<I>)" we need to evaluate "I". */
7928 saved_skip_evaluation
= skip_evaluation
;
7929 skip_evaluation
= false;
7931 /* First, determine the context for the type we are looking
7933 context
= TYPE_CONTEXT (t
);
7936 context
= tsubst_aggr_type (context
, args
, complain
,
7937 in_decl
, /*entering_scope=*/1);
7938 /* If context is a nested class inside a class template,
7939 it may still need to be instantiated (c++/33959). */
7940 if (TYPE_P (context
))
7941 context
= complete_type (context
);
7944 /* Then, figure out what arguments are appropriate for the
7945 type we are trying to find. For example, given:
7947 template <class T> struct S;
7948 template <class T, class U> void f(T, U) { S<U> su; }
7950 and supposing that we are instantiating f<int, double>,
7951 then our ARGS will be {int, double}, but, when looking up
7952 S we only want {double}. */
7953 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
7955 if (argvec
== error_mark_node
)
7956 r
= error_mark_node
;
7959 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
7960 entering_scope
, complain
);
7961 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
7964 skip_evaluation
= saved_skip_evaluation
;
7969 /* This is not a template type, so there's nothing to do. */
7973 return tsubst (t
, args
, complain
, in_decl
);
7977 /* Substitute into the default argument ARG (a default argument for
7978 FN), which has the indicated TYPE. */
7981 tsubst_default_argument (tree fn
, tree type
, tree arg
)
7983 tree saved_class_ptr
= NULL_TREE
;
7984 tree saved_class_ref
= NULL_TREE
;
7986 /* This default argument came from a template. Instantiate the
7987 default argument here, not in tsubst. In the case of
7996 we must be careful to do name lookup in the scope of S<T>,
7997 rather than in the current class. */
7998 push_access_scope (fn
);
7999 /* The "this" pointer is not valid in a default argument. */
8002 saved_class_ptr
= current_class_ptr
;
8003 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
8004 saved_class_ref
= current_class_ref
;
8005 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
8008 push_deferring_access_checks(dk_no_deferred
);
8009 /* The default argument expression may cause implicitly defined
8010 member functions to be synthesized, which will result in garbage
8011 collection. We must treat this situation as if we were within
8012 the body of function so as to avoid collecting live data on the
8015 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
8016 tf_warning_or_error
, NULL_TREE
,
8017 /*integral_constant_expression_p=*/false);
8019 pop_deferring_access_checks();
8021 /* Restore the "this" pointer. */
8024 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
8025 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
8028 pop_access_scope (fn
);
8030 /* Make sure the default argument is reasonable. */
8031 arg
= check_default_argument (type
, arg
);
8036 /* Substitute into all the default arguments for FN. */
8039 tsubst_default_arguments (tree fn
)
8044 tmpl_args
= DECL_TI_ARGS (fn
);
8046 /* If this function is not yet instantiated, we certainly don't need
8047 its default arguments. */
8048 if (uses_template_parms (tmpl_args
))
8051 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8053 arg
= TREE_CHAIN (arg
))
8054 if (TREE_PURPOSE (arg
))
8055 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
8057 TREE_PURPOSE (arg
));
8060 /* Substitute the ARGS into the T, which is a _DECL. Return the
8061 result of the substitution. Issue error and warning messages under
8062 control of COMPLAIN. */
8065 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
8067 location_t saved_loc
;
8071 /* Set the filename and linenumber to improve error-reporting. */
8072 saved_loc
= input_location
;
8073 input_location
= DECL_SOURCE_LOCATION (t
);
8075 switch (TREE_CODE (t
))
8079 /* We can get here when processing a member function template,
8080 member class template, and template template parameter of
8081 a template class. */
8082 tree decl
= DECL_TEMPLATE_RESULT (t
);
8087 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8089 /* Template template parameter is treated here. */
8090 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8091 if (new_type
== error_mark_node
)
8092 return error_mark_node
;
8095 TREE_CHAIN (r
) = NULL_TREE
;
8096 TREE_TYPE (r
) = new_type
;
8097 DECL_TEMPLATE_RESULT (r
)
8098 = build_decl (TYPE_DECL
, DECL_NAME (decl
), new_type
);
8099 DECL_TEMPLATE_PARMS (r
)
8100 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8102 TYPE_NAME (new_type
) = r
;
8106 /* We might already have an instance of this template.
8107 The ARGS are for the surrounding class type, so the
8108 full args contain the tsubst'd args for the context,
8109 plus the innermost args from the template decl. */
8110 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
8111 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
8112 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
8113 /* Because this is a template, the arguments will still be
8114 dependent, even after substitution. If
8115 PROCESSING_TEMPLATE_DECL is not set, the dependency
8116 predicates will short-circuit. */
8117 ++processing_template_decl
;
8118 full_args
= tsubst_template_args (tmpl_args
, args
,
8120 --processing_template_decl
;
8121 if (full_args
== error_mark_node
)
8122 return error_mark_node
;
8124 /* tsubst_template_args doesn't copy the vector if
8125 nothing changed. But, *something* should have
8127 gcc_assert (full_args
!= tmpl_args
);
8129 spec
= retrieve_specialization (t
, full_args
,
8130 /*class_specializations_p=*/true);
8131 if (spec
!= NULL_TREE
)
8137 /* Make a new template decl. It will be similar to the
8138 original, but will record the current template arguments.
8139 We also create a new function declaration, which is just
8140 like the old one, but points to this new template, rather
8141 than the old one. */
8143 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
8144 TREE_CHAIN (r
) = NULL_TREE
;
8146 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
8148 if (TREE_CODE (decl
) == TYPE_DECL
)
8151 ++processing_template_decl
;
8152 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8153 --processing_template_decl
;
8154 if (new_type
== error_mark_node
)
8155 return error_mark_node
;
8157 TREE_TYPE (r
) = new_type
;
8158 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
8159 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
8160 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
8161 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
8166 ++processing_template_decl
;
8167 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
8168 --processing_template_decl
;
8169 if (new_decl
== error_mark_node
)
8170 return error_mark_node
;
8172 DECL_TEMPLATE_RESULT (r
) = new_decl
;
8173 DECL_TI_TEMPLATE (new_decl
) = r
;
8174 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
8175 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
8176 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
8179 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8180 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
8181 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
8183 /* The template parameters for this new template are all the
8184 template parameters for the old template, except the
8185 outermost level of parameters. */
8186 DECL_TEMPLATE_PARMS (r
)
8187 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
8190 if (PRIMARY_TEMPLATE_P (t
))
8191 DECL_PRIMARY_TEMPLATE (r
) = r
;
8193 if (TREE_CODE (decl
) != TYPE_DECL
)
8194 /* Record this non-type partial instantiation. */
8195 register_specialization (r
, t
,
8196 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
8204 tree argvec
= NULL_TREE
;
8212 /* Nobody should be tsubst'ing into non-template functions. */
8213 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
8215 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
8220 /* If T is not dependent, just return it. We have to
8221 increment PROCESSING_TEMPLATE_DECL because
8222 value_dependent_expression_p assumes that nothing is
8223 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8224 ++processing_template_decl
;
8225 dependent_p
= value_dependent_expression_p (t
);
8226 --processing_template_decl
;
8230 /* Calculate the most general template of which R is a
8231 specialization, and the complete set of arguments used to
8233 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
8234 argvec
= tsubst_template_args (DECL_TI_ARGS
8235 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
8236 args
, complain
, in_decl
);
8238 /* Check to see if we already have this specialization. */
8239 spec
= retrieve_specialization (gen_tmpl
, argvec
,
8240 /*class_specializations_p=*/false);
8248 /* We can see more levels of arguments than parameters if
8249 there was a specialization of a member template, like
8252 template <class T> struct S { template <class U> void f(); }
8253 template <> template <class U> void S<int>::f(U);
8255 Here, we'll be substituting into the specialization,
8256 because that's where we can find the code we actually
8257 want to generate, but we'll have enough arguments for
8258 the most general template.
8260 We also deal with the peculiar case:
8262 template <class T> struct S {
8263 template <class U> friend void f();
8265 template <class U> void f() {}
8267 template void f<double>();
8269 Here, the ARGS for the instantiation of will be {int,
8270 double}. But, we only need as many ARGS as there are
8271 levels of template parameters in CODE_PATTERN. We are
8272 careful not to get fooled into reducing the ARGS in
8275 template <class T> struct S { template <class U> void f(U); }
8276 template <class T> template <> void S<T>::f(int) {}
8278 which we can spot because the pattern will be a
8279 specialization in this case. */
8280 args_depth
= TMPL_ARGS_DEPTH (args
);
8282 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
8283 if (args_depth
> parms_depth
8284 && !DECL_TEMPLATE_SPECIALIZATION (t
))
8285 args
= get_innermost_template_args (args
, parms_depth
);
8289 /* This special case arises when we have something like this:
8291 template <class T> struct S {
8292 friend void f<int>(int, double);
8295 Here, the DECL_TI_TEMPLATE for the friend declaration
8296 will be an IDENTIFIER_NODE. We are being called from
8297 tsubst_friend_function, and we want only to create a
8298 new decl (R) with appropriate types so that we can call
8299 determine_specialization. */
8300 gen_tmpl
= NULL_TREE
;
8303 if (DECL_CLASS_SCOPE_P (t
))
8305 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
8309 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
8310 complain
, t
, /*entering_scope=*/1);
8315 ctx
= DECL_CONTEXT (t
);
8317 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8318 if (type
== error_mark_node
)
8319 return error_mark_node
;
8321 /* We do NOT check for matching decls pushed separately at this
8322 point, as they may not represent instantiations of this
8323 template, and in any case are considered separate under the
8326 DECL_USE_TEMPLATE (r
) = 0;
8327 TREE_TYPE (r
) = type
;
8328 /* Clear out the mangled name and RTL for the instantiation. */
8329 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8330 SET_DECL_RTL (r
, NULL_RTX
);
8331 /* Leave DECL_INITIAL set on deleted instantiations. */
8332 if (!DECL_DELETED_FN (r
))
8333 DECL_INITIAL (r
) = NULL_TREE
;
8334 DECL_CONTEXT (r
) = ctx
;
8336 if (member
&& DECL_CONV_FN_P (r
))
8337 /* Type-conversion operator. Reconstruct the name, in
8338 case it's the name of one of the template's parameters. */
8339 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
8341 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
8343 DECL_RESULT (r
) = NULL_TREE
;
8345 TREE_STATIC (r
) = 0;
8346 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
8347 DECL_EXTERNAL (r
) = 1;
8348 /* If this is an instantiation of a function with internal
8349 linkage, we already know what object file linkage will be
8350 assigned to the instantiation. */
8351 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
8352 DECL_DEFER_OUTPUT (r
) = 0;
8353 TREE_CHAIN (r
) = NULL_TREE
;
8354 DECL_PENDING_INLINE_INFO (r
) = 0;
8355 DECL_PENDING_INLINE_P (r
) = 0;
8356 DECL_SAVED_TREE (r
) = NULL_TREE
;
8357 DECL_STRUCT_FUNCTION (r
) = NULL
;
8359 if (DECL_CLONED_FUNCTION (r
))
8361 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
8363 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
8364 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
8367 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8368 this in the special friend case mentioned above where
8369 GEN_TMPL is NULL. */
8372 DECL_TEMPLATE_INFO (r
)
8373 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
8374 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8375 register_specialization (r
, gen_tmpl
, argvec
, false);
8377 /* We're not supposed to instantiate default arguments
8378 until they are called, for a template. But, for a
8381 template <class T> void f ()
8382 { extern void g(int i = T()); }
8384 we should do the substitution when the template is
8385 instantiated. We handle the member function case in
8386 instantiate_class_template since the default arguments
8387 might refer to other members of the class. */
8389 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
8390 && !uses_template_parms (argvec
))
8391 tsubst_default_arguments (r
);
8394 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
8396 /* Copy the list of befriending classes. */
8397 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
8399 friends
= &TREE_CHAIN (*friends
))
8401 *friends
= copy_node (*friends
);
8402 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
8407 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
8409 maybe_retrofit_in_chrg (r
);
8410 if (DECL_CONSTRUCTOR_P (r
))
8411 grok_ctor_properties (ctx
, r
);
8412 /* If this is an instantiation of a member template, clone it.
8413 If it isn't, that'll be handled by
8414 clone_constructors_and_destructors. */
8415 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
8416 clone_function_decl (r
, /*update_method_vec_p=*/0);
8418 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
))
8419 && !grok_op_properties (r
, (complain
& tf_error
) != 0))
8420 return error_mark_node
;
8422 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
8423 SET_DECL_FRIEND_CONTEXT (r
,
8424 tsubst (DECL_FRIEND_CONTEXT (t
),
8425 args
, complain
, in_decl
));
8427 /* Possibly limit visibility based on template args. */
8428 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
8429 if (DECL_VISIBILITY_SPECIFIED (t
))
8431 DECL_VISIBILITY_SPECIFIED (r
) = 0;
8433 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
8435 determine_visibility (r
);
8437 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8438 args
, complain
, in_decl
);
8444 tree type
= NULL_TREE
;
8446 tree expanded_types
= NULL_TREE
;
8447 tree prev_r
= NULL_TREE
;
8448 tree first_r
= NULL_TREE
;
8450 if (FUNCTION_PARAMETER_PACK_P (t
))
8452 /* If there is a local specialization that isn't a
8453 parameter pack, it means that we're doing a "simple"
8454 substitution from inside tsubst_pack_expansion. Just
8455 return the local specialization (which will be a single
8457 tree spec
= retrieve_local_specialization (t
);
8459 && TREE_CODE (spec
) == PARM_DECL
8460 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
8463 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8464 the parameters in this function parameter pack. */
8465 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
8467 if (TREE_CODE (expanded_types
) == TREE_VEC
)
8469 len
= TREE_VEC_LENGTH (expanded_types
);
8471 /* Zero-length parameter packs are boring. Just substitute
8474 return tsubst (TREE_CHAIN (t
), args
, complain
,
8479 /* All we did was update the type. Make a note of that. */
8480 type
= expanded_types
;
8481 expanded_types
= NULL_TREE
;
8485 /* Loop through all of the parameter's we'll build. When T is
8486 a function parameter pack, LEN is the number of expanded
8487 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8489 for (i
= 0; i
< len
; ++i
)
8493 if (DECL_TEMPLATE_PARM_P (t
))
8494 SET_DECL_TEMPLATE_PARM_P (r
);
8497 /* We're on the Ith parameter of the function parameter
8500 /* Get the Ith type. */
8501 type
= TREE_VEC_ELT (expanded_types
, i
);
8504 /* Rename the parameter to include the index. */
8506 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
8509 /* We're dealing with a normal parameter. */
8510 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8512 type
= type_decays_to (type
);
8513 TREE_TYPE (r
) = type
;
8514 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8516 if (DECL_INITIAL (r
))
8518 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
8519 DECL_INITIAL (r
) = TREE_TYPE (r
);
8521 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
8525 DECL_CONTEXT (r
) = NULL_TREE
;
8527 if (!DECL_TEMPLATE_PARM_P (r
))
8528 DECL_ARG_TYPE (r
) = type_passed_as (type
);
8530 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8531 args
, complain
, in_decl
);
8533 /* Keep track of the first new parameter we
8534 generate. That's what will be returned to the
8539 /* Build a proper chain of parameters when substituting
8540 into a function parameter pack. */
8542 TREE_CHAIN (prev_r
) = r
;
8546 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
8547 complain
, TREE_CHAIN (t
));
8549 /* FIRST_R contains the start of the chain we've built. */
8559 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8560 if (type
== error_mark_node
)
8561 return error_mark_node
;
8562 TREE_TYPE (r
) = type
;
8563 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8565 /* DECL_INITIAL gives the number of bits in a bit-field. */
8567 = tsubst_expr (DECL_INITIAL (t
), args
,
8569 /*integral_constant_expression_p=*/true);
8570 /* We don't have to set DECL_CONTEXT here; it is set by
8571 finish_member_declaration. */
8572 TREE_CHAIN (r
) = NULL_TREE
;
8573 if (VOID_TYPE_P (type
))
8574 error ("instantiation of %q+D as type %qT", r
, type
);
8576 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8577 args
, complain
, in_decl
);
8582 /* We reach here only for member using decls. */
8583 if (DECL_DEPENDENT_P (t
))
8585 r
= do_class_using_decl
8586 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
8587 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
8589 r
= error_mark_node
;
8592 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
8593 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
8599 TREE_CHAIN (r
) = NULL_TREE
;
8606 tree argvec
= NULL_TREE
;
8607 tree gen_tmpl
= NULL_TREE
;
8609 tree tmpl
= NULL_TREE
;
8611 tree type
= NULL_TREE
;
8614 if (TREE_CODE (t
) == TYPE_DECL
8615 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
8617 /* If this is the canonical decl, we don't have to
8618 mess with instantiations, and often we can't (for
8619 typename, template type parms and such). Note that
8620 TYPE_NAME is not correct for the above test if
8621 we've copied the type for a typedef. */
8622 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8623 if (type
== error_mark_node
)
8624 return error_mark_node
;
8625 r
= TYPE_NAME (type
);
8629 /* Check to see if we already have the specialization we
8632 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
8634 /* T is a static data member or namespace-scope entity.
8635 We have to substitute into namespace-scope variables
8636 (even though such entities are never templates) because
8639 template <class T> void f() { extern T t; }
8641 where the entity referenced is not known until
8642 instantiation time. */
8644 ctx
= DECL_CONTEXT (t
);
8645 if (DECL_CLASS_SCOPE_P (t
))
8647 ctx
= tsubst_aggr_type (ctx
, args
,
8649 in_decl
, /*entering_scope=*/1);
8650 /* If CTX is unchanged, then T is in fact the
8651 specialization we want. That situation occurs when
8652 referencing a static data member within in its own
8653 class. We can use pointer equality, rather than
8654 same_type_p, because DECL_CONTEXT is always
8656 if (ctx
== DECL_CONTEXT (t
))
8662 tmpl
= DECL_TI_TEMPLATE (t
);
8663 gen_tmpl
= most_general_template (tmpl
);
8664 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
8665 spec
= (retrieve_specialization
8667 /*class_specializations_p=*/false));
8672 /* A local variable. */
8674 /* Subsequent calls to pushdecl will fill this in. */
8676 spec
= retrieve_local_specialization (t
);
8678 /* If we already have the specialization we need, there is
8679 nothing more to do. */
8686 /* Create a new node for the specialization we need. */
8688 if (type
== NULL_TREE
)
8689 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8690 if (TREE_CODE (r
) == VAR_DECL
)
8692 /* Even if the original location is out of scope, the
8693 newly substituted one is not. */
8694 DECL_DEAD_FOR_LOCAL (r
) = 0;
8695 DECL_INITIALIZED_P (r
) = 0;
8696 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
8697 if (type
== error_mark_node
)
8698 return error_mark_node
;
8699 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8701 /* It may seem that this case cannot occur, since:
8706 declares a function, not a variable. However:
8709 template <typename T> void g() { T t; }
8710 template void g<f>();
8712 is an attempt to declare a variable with function
8714 error ("variable %qD has function type",
8715 /* R is not yet sufficiently initialized, so we
8716 just use its name. */
8718 return error_mark_node
;
8720 type
= complete_type (type
);
8721 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
8722 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
8723 type
= check_var_type (DECL_NAME (r
), type
);
8725 if (DECL_HAS_VALUE_EXPR_P (t
))
8727 tree ve
= DECL_VALUE_EXPR (t
);
8728 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
8729 /*constant_expression_p=*/false);
8730 SET_DECL_VALUE_EXPR (r
, ve
);
8733 else if (DECL_SELF_REFERENCE_P (t
))
8734 SET_DECL_SELF_REFERENCE_P (r
);
8735 TREE_TYPE (r
) = type
;
8736 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8737 DECL_CONTEXT (r
) = ctx
;
8738 /* Clear out the mangled name and RTL for the instantiation. */
8739 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8740 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8741 SET_DECL_RTL (r
, NULL_RTX
);
8742 /* The initializer must not be expanded until it is required;
8744 DECL_INITIAL (r
) = NULL_TREE
;
8745 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8746 SET_DECL_RTL (r
, NULL_RTX
);
8747 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
8748 if (TREE_CODE (r
) == VAR_DECL
)
8750 /* Possibly limit visibility based on template args. */
8751 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
8752 if (DECL_VISIBILITY_SPECIFIED (t
))
8754 DECL_VISIBILITY_SPECIFIED (r
) = 0;
8756 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
8758 determine_visibility (r
);
8760 /* Preserve a typedef that names a type. */
8761 else if (TREE_CODE (r
) == TYPE_DECL
8762 && DECL_ORIGINAL_TYPE (t
)
8763 && type
!= error_mark_node
)
8765 DECL_ORIGINAL_TYPE (r
) = tsubst (DECL_ORIGINAL_TYPE (t
),
8766 args
, complain
, in_decl
);
8767 TREE_TYPE (r
) = type
= build_variant_type_copy (type
);
8768 TYPE_NAME (type
) = r
;
8773 /* A static data member declaration is always marked
8774 external when it is declared in-class, even if an
8775 initializer is present. We mimic the non-template
8777 DECL_EXTERNAL (r
) = 1;
8779 register_specialization (r
, gen_tmpl
, argvec
, false);
8780 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
8781 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8784 register_local_specialization (r
, t
);
8786 TREE_CHAIN (r
) = NULL_TREE
;
8788 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
8789 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8790 args
, complain
, in_decl
);
8799 /* Restore the file and line information. */
8800 input_location
= saved_loc
;
8805 /* Substitute into the ARG_TYPES of a function type. */
8808 tsubst_arg_types (tree arg_types
,
8810 tsubst_flags_t complain
,
8813 tree remaining_arg_types
;
8814 tree type
= NULL_TREE
;
8816 tree expanded_args
= NULL_TREE
;
8819 if (!arg_types
|| arg_types
== void_list_node
)
8822 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
8823 args
, complain
, in_decl
);
8824 if (remaining_arg_types
== error_mark_node
)
8825 return error_mark_node
;
8827 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
8829 /* For a pack expansion, perform substitution on the
8830 entire expression. Later on, we'll handle the arguments
8832 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
8833 args
, complain
, in_decl
);
8835 if (TREE_CODE (expanded_args
) == TREE_VEC
)
8836 /* So that we'll spin through the parameters, one by one. */
8837 i
= TREE_VEC_LENGTH (expanded_args
);
8840 /* We only partially substituted into the parameter
8841 pack. Our type is TYPE_PACK_EXPANSION. */
8842 type
= expanded_args
;
8843 expanded_args
= NULL_TREE
;
8851 type
= TREE_VEC_ELT (expanded_args
, i
);
8853 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
8855 if (type
== error_mark_node
)
8856 return error_mark_node
;
8857 if (VOID_TYPE_P (type
))
8859 if (complain
& tf_error
)
8861 error ("invalid parameter type %qT", type
);
8863 error ("in declaration %q+D", in_decl
);
8865 return error_mark_node
;
8868 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8869 top-level qualifiers as required. */
8870 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
8872 /* We do not substitute into default arguments here. The standard
8873 mandates that they be instantiated only when needed, which is
8874 done in build_over_call. */
8875 default_arg
= TREE_PURPOSE (arg_types
);
8877 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
8879 /* We've instantiated a template before its default arguments
8880 have been parsed. This can happen for a nested template
8881 class, and is not an error unless we require the default
8882 argument in a call of this function. */
8883 remaining_arg_types
=
8884 tree_cons (default_arg
, type
, remaining_arg_types
);
8885 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
8886 remaining_arg_types
);
8889 remaining_arg_types
=
8890 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
8893 return remaining_arg_types
;
8896 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8897 *not* handle the exception-specification for FNTYPE, because the
8898 initial substitution of explicitly provided template parameters
8899 during argument deduction forbids substitution into the
8900 exception-specification:
8904 All references in the function type of the function template to the
8905 corresponding template parameters are replaced by the specified tem-
8906 plate argument values. If a substitution in a template parameter or
8907 in the function type of the function template results in an invalid
8908 type, type deduction fails. [Note: The equivalent substitution in
8909 exception specifications is done only when the function is instanti-
8910 ated, at which point a program is ill-formed if the substitution
8911 results in an invalid type.] */
8914 tsubst_function_type (tree t
,
8916 tsubst_flags_t complain
,
8923 /* The TYPE_CONTEXT is not used for function/method types. */
8924 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
8926 /* Substitute the return type. */
8927 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8928 if (return_type
== error_mark_node
)
8929 return error_mark_node
;
8930 /* The standard does not presently indicate that creation of a
8931 function type with an invalid return type is a deduction failure.
8932 However, that is clearly analogous to creating an array of "void"
8933 or a reference to a reference. This is core issue #486. */
8934 if (TREE_CODE (return_type
) == ARRAY_TYPE
8935 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
8937 if (complain
& tf_error
)
8939 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
8940 error ("function returning an array");
8942 error ("function returning a function");
8944 return error_mark_node
;
8947 /* Substitute the argument types. */
8948 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
8950 if (arg_types
== error_mark_node
)
8951 return error_mark_node
;
8953 /* Construct a new type node and return it. */
8954 if (TREE_CODE (t
) == FUNCTION_TYPE
)
8955 fntype
= build_function_type (return_type
, arg_types
);
8958 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
8959 if (! MAYBE_CLASS_TYPE_P (r
))
8963 Type deduction may fail for any of the following
8966 -- Attempting to create "pointer to member of T" when T
8967 is not a class type. */
8968 if (complain
& tf_error
)
8969 error ("creating pointer to member function of non-class type %qT",
8971 return error_mark_node
;
8974 fntype
= build_method_type_directly (r
, return_type
,
8975 TREE_CHAIN (arg_types
));
8977 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
8978 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
8983 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8984 ARGS into that specification, and return the substituted
8985 specification. If there is no specification, return NULL_TREE. */
8988 tsubst_exception_specification (tree fntype
,
8990 tsubst_flags_t complain
,
8996 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
8997 new_specs
= NULL_TREE
;
9000 if (! TREE_VALUE (specs
))
9007 tree expanded_specs
= NULL_TREE
;
9009 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
9011 /* Expand the pack expansion type. */
9012 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
9016 if (expanded_specs
== error_mark_node
)
9017 return error_mark_node
;
9018 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
9019 len
= TREE_VEC_LENGTH (expanded_specs
);
9022 /* We're substituting into a member template, so
9023 we got a TYPE_PACK_EXPANSION back. Add that
9024 expansion and move on. */
9025 gcc_assert (TREE_CODE (expanded_specs
)
9026 == TYPE_PACK_EXPANSION
);
9027 new_specs
= add_exception_specifier (new_specs
,
9030 specs
= TREE_CHAIN (specs
);
9035 for (i
= 0; i
< len
; ++i
)
9038 spec
= TREE_VEC_ELT (expanded_specs
, i
);
9040 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
9041 if (spec
== error_mark_node
)
9043 new_specs
= add_exception_specifier (new_specs
, spec
,
9047 specs
= TREE_CHAIN (specs
);
9053 /* Take the tree structure T and replace template parameters used
9054 therein with the argument vector ARGS. IN_DECL is an associated
9055 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9056 Issue error and warning messages under control of COMPLAIN. Note
9057 that we must be relatively non-tolerant of extensions here, in
9058 order to preserve conformance; if we allow substitutions that
9059 should not be allowed, we may allow argument deductions that should
9060 not succeed, and therefore report ambiguous overload situations
9061 where there are none. In theory, we could allow the substitution,
9062 but indicate that it should have failed, and allow our caller to
9063 make sure that the right thing happens, but we don't try to do this
9066 This function is used for dealing with types, decls and the like;
9067 for expressions, use tsubst_expr or tsubst_copy. */
9070 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9074 if (t
== NULL_TREE
|| t
== error_mark_node
9075 || t
== integer_type_node
9076 || t
== void_type_node
9077 || t
== char_type_node
9078 || t
== unknown_type_node
9079 || TREE_CODE (t
) == NAMESPACE_DECL
)
9083 return tsubst_decl (t
, args
, complain
);
9085 if (args
== NULL_TREE
)
9088 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
9089 type
= IDENTIFIER_TYPE_VALUE (t
);
9091 type
= TREE_TYPE (t
);
9093 gcc_assert (type
!= unknown_type_node
);
9095 /* Reuse typedefs. We need to do this to handle dependent attributes,
9096 such as attribute aligned. */
9099 && TYPE_NAME (t
) != TYPE_MAIN_DECL (t
))
9101 tree decl
= TYPE_NAME (t
);
9103 if (DECL_CLASS_SCOPE_P (decl
)
9104 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9105 && uses_template_parms (DECL_CONTEXT (decl
)))
9107 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9108 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
9109 r
= retrieve_specialization (tmpl
, gen_args
, false);
9111 else if (DECL_FUNCTION_SCOPE_P (decl
)
9112 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
9113 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
9114 r
= retrieve_local_specialization (decl
);
9116 /* The typedef is from a non-template context. */
9122 r
= cp_build_qualified_type_real
9123 (r
, cp_type_quals (t
) | cp_type_quals (r
),
9124 complain
| tf_ignore_bad_quals
);
9127 /* Else we must be instantiating the typedef, so fall through. */
9131 && TREE_CODE (t
) != TYPENAME_TYPE
9132 && TREE_CODE (t
) != IDENTIFIER_NODE
9133 && TREE_CODE (t
) != FUNCTION_TYPE
9134 && TREE_CODE (t
) != METHOD_TYPE
)
9135 type
= tsubst (type
, args
, complain
, in_decl
);
9136 if (type
== error_mark_node
)
9137 return error_mark_node
;
9139 switch (TREE_CODE (t
))
9144 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
9145 /*entering_scope=*/0);
9148 case IDENTIFIER_NODE
:
9160 if (t
== integer_type_node
)
9163 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
9164 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
9168 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
9170 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
9171 /*integral_constant_expression_p=*/false);
9172 max
= fold_decl_constant_value (max
);
9174 if (TREE_CODE (max
) != INTEGER_CST
9175 && !at_function_scope_p ()
9176 && !value_dependent_expression_p (max
))
9178 if (complain
& tf_error
)
9179 error ("array bound is not an integer constant");
9180 return error_mark_node
;
9185 Type deduction may fail for any of the following
9188 Attempting to create an array with a size that is
9189 zero or negative. */
9190 if (integer_zerop (max
) && !(complain
& tf_error
))
9191 /* We must fail if performing argument deduction (as
9192 indicated by the state of complain), so that
9193 another substitution can be found. */
9194 return error_mark_node
;
9195 else if (TREE_CODE (max
) == INTEGER_CST
9196 && INT_CST_LT (max
, integer_zero_node
))
9198 if (complain
& tf_error
)
9199 error ("creating array with negative size (%qE)", max
);
9201 return error_mark_node
;
9204 return compute_array_index_type (NULL_TREE
, max
);
9207 case TEMPLATE_TYPE_PARM
:
9208 case TEMPLATE_TEMPLATE_PARM
:
9209 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9210 case TEMPLATE_PARM_INDEX
:
9215 tree arg
= NULL_TREE
;
9219 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
9220 template_parm_level_and_index (t
, &level
, &idx
);
9222 levels
= TMPL_ARGS_DEPTH (args
);
9223 if (level
<= levels
)
9225 arg
= TMPL_ARG (args
, level
, idx
);
9227 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
9228 /* See through ARGUMENT_PACK_SELECT arguments. */
9229 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
9232 if (arg
== error_mark_node
)
9233 return error_mark_node
;
9234 else if (arg
!= NULL_TREE
)
9236 if (ARGUMENT_PACK_P (arg
))
9237 /* If ARG is an argument pack, we don't actually want to
9238 perform a substitution here, because substitutions
9239 for argument packs are only done
9240 element-by-element. We can get to this point when
9241 substituting the type of a non-type template
9242 parameter pack, when that type actually contains
9243 template parameter packs from an outer template, e.g.,
9245 template<typename... Types> struct A {
9246 template<Types... Values> struct B { };
9250 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
9253 gcc_assert (TYPE_P (arg
));
9255 /* cv-quals from the template are discarded when
9256 substituting in a function or reference type. */
9257 if (TREE_CODE (arg
) == FUNCTION_TYPE
9258 || TREE_CODE (arg
) == METHOD_TYPE
9259 || TREE_CODE (arg
) == REFERENCE_TYPE
)
9260 quals
= cp_type_quals (arg
);
9262 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
9264 return cp_build_qualified_type_real
9265 (arg
, quals
, complain
| tf_ignore_bad_quals
);
9267 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9269 /* We are processing a type constructed from a
9270 template template parameter. */
9271 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
9272 args
, complain
, in_decl
);
9273 if (argvec
== error_mark_node
)
9274 return error_mark_node
;
9276 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9277 are resolving nested-types in the signature of a
9278 member function templates. Otherwise ARG is a
9279 TEMPLATE_DECL and is the real template to be
9281 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
9282 arg
= TYPE_NAME (arg
);
9284 r
= lookup_template_class (arg
,
9287 /*entering_scope=*/0,
9289 return cp_build_qualified_type_real
9290 (r
, TYPE_QUALS (t
), complain
);
9293 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9298 /* This can happen during the attempted tsubst'ing in
9299 unify. This means that we don't yet have any information
9300 about the template parameter in question. */
9303 /* If we get here, we must have been looking at a parm for a
9304 more deeply nested template. Make a new version of this
9305 template parameter, but with a lower level. */
9306 switch (TREE_CODE (t
))
9308 case TEMPLATE_TYPE_PARM
:
9309 case TEMPLATE_TEMPLATE_PARM
:
9310 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9311 if (cp_type_quals (t
))
9313 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
9314 r
= cp_build_qualified_type_real
9315 (r
, cp_type_quals (t
),
9316 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
9317 ? tf_ignore_bad_quals
: 0));
9322 TEMPLATE_TYPE_PARM_INDEX (r
)
9323 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
9324 r
, levels
, args
, complain
);
9325 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
9326 TYPE_MAIN_VARIANT (r
) = r
;
9327 TYPE_POINTER_TO (r
) = NULL_TREE
;
9328 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
9330 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
9331 /* We have reduced the level of the template
9332 template parameter, but not the levels of its
9333 template parameters, so canonical_type_parameter
9334 will not be able to find the canonical template
9335 template parameter for this level. Thus, we
9336 require structural equality checking to compare
9337 TEMPLATE_TEMPLATE_PARMs. */
9338 SET_TYPE_STRUCTURAL_EQUALITY (r
);
9339 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
9340 SET_TYPE_STRUCTURAL_EQUALITY (r
);
9342 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
9344 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
9346 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
9348 if (argvec
== error_mark_node
)
9349 return error_mark_node
;
9351 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
9352 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
9357 case TEMPLATE_PARM_INDEX
:
9358 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
9370 tree purpose
, value
, chain
;
9372 if (t
== void_list_node
)
9375 purpose
= TREE_PURPOSE (t
);
9378 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
9379 if (purpose
== error_mark_node
)
9380 return error_mark_node
;
9382 value
= TREE_VALUE (t
);
9385 value
= tsubst (value
, args
, complain
, in_decl
);
9386 if (value
== error_mark_node
)
9387 return error_mark_node
;
9389 chain
= TREE_CHAIN (t
);
9390 if (chain
&& chain
!= void_type_node
)
9392 chain
= tsubst (chain
, args
, complain
, in_decl
);
9393 if (chain
== error_mark_node
)
9394 return error_mark_node
;
9396 if (purpose
== TREE_PURPOSE (t
)
9397 && value
== TREE_VALUE (t
)
9398 && chain
== TREE_CHAIN (t
))
9400 return hash_tree_cons (purpose
, value
, chain
);
9404 /* We should never be tsubsting a binfo. */
9408 /* A vector of template arguments. */
9410 return tsubst_template_args (t
, args
, complain
, in_decl
);
9413 case REFERENCE_TYPE
:
9415 enum tree_code code
;
9417 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
9420 code
= TREE_CODE (t
);
9425 Type deduction may fail for any of the following
9428 -- Attempting to create a pointer to reference type.
9429 -- Attempting to create a reference to a reference type or
9430 a reference to void.
9432 Core issue 106 says that creating a reference to a reference
9433 during instantiation is no longer a cause for failure. We
9434 only enforce this check in strict C++98 mode. */
9435 if ((TREE_CODE (type
) == REFERENCE_TYPE
9436 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
9437 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
9439 static location_t last_loc
;
9441 /* We keep track of the last time we issued this error
9442 message to avoid spewing a ton of messages during a
9443 single bad template instantiation. */
9444 if (complain
& tf_error
9445 && last_loc
!= input_location
)
9447 if (TREE_CODE (type
) == VOID_TYPE
)
9448 error ("forming reference to void");
9450 error ("forming %s to reference type %qT",
9451 (code
== POINTER_TYPE
) ? "pointer" : "reference",
9453 last_loc
= input_location
;
9456 return error_mark_node
;
9458 else if (code
== POINTER_TYPE
)
9460 r
= build_pointer_type (type
);
9461 if (TREE_CODE (type
) == METHOD_TYPE
)
9462 r
= build_ptrmemfunc_type (r
);
9464 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
9465 /* In C++0x, during template argument substitution, when there is an
9466 attempt to create a reference to a reference type, reference
9467 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9469 "If a template-argument for a template-parameter T names a type
9470 that is a reference to a type A, an attempt to create the type
9471 'lvalue reference to cv T' creates the type 'lvalue reference to
9472 A,' while an attempt to create the type type rvalue reference to
9473 cv T' creates the type T"
9475 r
= cp_build_reference_type
9477 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
9479 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
9480 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
9482 if (r
!= error_mark_node
)
9483 /* Will this ever be needed for TYPE_..._TO values? */
9490 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
9491 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
9495 Type deduction may fail for any of the following
9498 -- Attempting to create "pointer to member of T" when T
9499 is not a class type. */
9500 if (complain
& tf_error
)
9501 error ("creating pointer to member of non-class type %qT", r
);
9502 return error_mark_node
;
9504 if (TREE_CODE (type
) == REFERENCE_TYPE
)
9506 if (complain
& tf_error
)
9507 error ("creating pointer to member reference type %qT", type
);
9508 return error_mark_node
;
9510 if (TREE_CODE (type
) == VOID_TYPE
)
9512 if (complain
& tf_error
)
9513 error ("creating pointer to member of type void");
9514 return error_mark_node
;
9516 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
9517 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9519 /* The type of the implicit object parameter gets its
9520 cv-qualifiers from the FUNCTION_TYPE. */
9522 tree this_type
= cp_build_qualified_type (TYPE_MAIN_VARIANT (r
),
9523 cp_type_quals (type
));
9525 method_type
= build_method_type_directly (this_type
,
9527 TYPE_ARG_TYPES (type
));
9528 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
9529 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
9533 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
9542 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
9543 if (fntype
== error_mark_node
)
9544 return error_mark_node
;
9546 /* Substitute the exception specification. */
9547 specs
= tsubst_exception_specification (t
, args
, complain
,
9549 if (specs
== error_mark_node
)
9550 return error_mark_node
;
9552 fntype
= build_exception_variant (fntype
, specs
);
9557 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
9558 if (domain
== error_mark_node
)
9559 return error_mark_node
;
9561 /* As an optimization, we avoid regenerating the array type if
9562 it will obviously be the same as T. */
9563 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
9566 /* These checks should match the ones in grokdeclarator.
9570 The deduction may fail for any of the following reasons:
9572 -- Attempting to create an array with an element type that
9573 is void, a function type, or a reference type, or [DR337]
9574 an abstract class type. */
9575 if (TREE_CODE (type
) == VOID_TYPE
9576 || TREE_CODE (type
) == FUNCTION_TYPE
9577 || TREE_CODE (type
) == REFERENCE_TYPE
)
9579 if (complain
& tf_error
)
9580 error ("creating array of %qT", type
);
9581 return error_mark_node
;
9583 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
9585 if (complain
& tf_error
)
9586 error ("creating array of %qT, which is an abstract class type",
9588 return error_mark_node
;
9591 r
= build_cplus_array_type (type
, domain
);
9593 if (TYPE_USER_ALIGN (t
))
9595 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
9596 TYPE_USER_ALIGN (r
) = 1;
9605 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9606 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9608 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9609 return error_mark_node
;
9611 return fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
9617 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9618 if (e
== error_mark_node
)
9619 return error_mark_node
;
9621 return fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), e
);
9626 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9627 in_decl
, /*entering_scope=*/1);
9628 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
9631 if (ctx
== error_mark_node
|| f
== error_mark_node
)
9632 return error_mark_node
;
9634 if (!MAYBE_CLASS_TYPE_P (ctx
))
9636 if (complain
& tf_error
)
9637 error ("%qT is not a class, struct, or union type", ctx
);
9638 return error_mark_node
;
9640 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
9642 /* Normally, make_typename_type does not require that the CTX
9643 have complete type in order to allow things like:
9645 template <class T> struct S { typename S<T>::X Y; };
9647 But, such constructs have already been resolved by this
9648 point, so here CTX really should have complete type, unless
9649 it's a partial instantiation. */
9650 ctx
= complete_type (ctx
);
9651 if (!COMPLETE_TYPE_P (ctx
))
9653 if (complain
& tf_error
)
9654 cxx_incomplete_type_error (NULL_TREE
, ctx
);
9655 return error_mark_node
;
9659 f
= make_typename_type (ctx
, f
, typename_type
,
9660 (complain
& tf_error
) | tf_keep_type_decl
);
9661 if (f
== error_mark_node
)
9663 if (TREE_CODE (f
) == TYPE_DECL
)
9665 complain
|= tf_ignore_bad_quals
;
9669 if (TREE_CODE (f
) != TYPENAME_TYPE
)
9671 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
9672 error ("%qT resolves to %qT, which is not an enumeration type",
9674 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
9675 error ("%qT resolves to %qT, which is is not a class type",
9679 return cp_build_qualified_type_real
9680 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
9683 case UNBOUND_CLASS_TEMPLATE
:
9685 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9686 in_decl
, /*entering_scope=*/1);
9687 tree name
= TYPE_IDENTIFIER (t
);
9688 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
9690 if (ctx
== error_mark_node
|| name
== error_mark_node
)
9691 return error_mark_node
;
9694 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
9695 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
9705 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9706 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
9707 /*integral_constant_expression_p=*/false);
9708 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9709 return error_mark_node
;
9711 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
9716 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9717 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9718 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9719 return error_mark_node
;
9721 return build_qualified_name (/*type=*/NULL_TREE
,
9722 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
9729 type
= finish_typeof (tsubst_expr
9730 (TYPEOF_TYPE_EXPR (t
), args
,
9732 /*integral_constant_expression_p=*/false));
9733 return cp_build_qualified_type_real (type
,
9735 | cp_type_quals (type
),
9745 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
9747 /*integral_constant_expression_p=*/false);
9752 finish_decltype_type (type
,
9753 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
9754 return cp_build_qualified_type_real (type
,
9756 | cp_type_quals (type
),
9760 case TYPE_ARGUMENT_PACK
:
9761 case NONTYPE_ARGUMENT_PACK
:
9763 tree r
= make_node (TREE_CODE (t
));
9765 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
9769 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
9771 /* For template nontype argument packs, also substitute into
9773 if (TREE_CODE (t
) == NONTYPE_ARGUMENT_PACK
)
9774 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9781 sorry ("use of %qs in template",
9782 tree_code_name
[(int) TREE_CODE (t
)]);
9783 return error_mark_node
;
9787 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9788 type of the expression on the left-hand side of the "." or "->"
9792 tsubst_baselink (tree baselink
, tree object_type
,
9793 tree args
, tsubst_flags_t complain
, tree in_decl
)
9796 tree qualifying_scope
;
9799 tree template_args
= 0;
9800 bool template_id_p
= false;
9802 /* A baselink indicates a function from a base class. Both the
9803 BASELINK_ACCESS_BINFO and the base class referenced may
9804 indicate bases of the template class, rather than the
9805 instantiated class. In addition, lookups that were not
9806 ambiguous before may be ambiguous now. Therefore, we perform
9807 the lookup again. */
9808 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
9809 qualifying_scope
= tsubst (qualifying_scope
, args
,
9811 fns
= BASELINK_FUNCTIONS (baselink
);
9812 optype
= BASELINK_OPTYPE (baselink
);
9813 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
9815 template_id_p
= true;
9816 template_args
= TREE_OPERAND (fns
, 1);
9817 fns
= TREE_OPERAND (fns
, 0);
9819 template_args
= tsubst_template_args (template_args
, args
,
9822 name
= DECL_NAME (get_first_fn (fns
));
9823 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
9825 /* If lookup found a single function, mark it as used at this
9826 point. (If it lookup found multiple functions the one selected
9827 later by overload resolution will be marked as used at that
9829 if (BASELINK_P (baselink
))
9830 fns
= BASELINK_FUNCTIONS (baselink
);
9831 if (!template_id_p
&& !really_overloaded_fn (fns
))
9832 mark_used (OVL_CURRENT (fns
));
9834 /* Add back the template arguments, if present. */
9835 if (BASELINK_P (baselink
) && template_id_p
)
9836 BASELINK_FUNCTIONS (baselink
)
9837 = build_nt (TEMPLATE_ID_EXPR
,
9838 BASELINK_FUNCTIONS (baselink
),
9840 /* Update the conversion operator type. */
9841 BASELINK_OPTYPE (baselink
)
9842 = tsubst (optype
, args
, complain
, in_decl
);
9845 object_type
= current_class_type
;
9846 return adjust_result_of_qualified_name_lookup (baselink
,
9851 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9852 true if the qualified-id will be a postfix-expression in-and-of
9853 itself; false if more of the postfix-expression follows the
9854 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9858 tsubst_qualified_id (tree qualified_id
, tree args
,
9859 tsubst_flags_t complain
, tree in_decl
,
9860 bool done
, bool address_p
)
9868 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
9870 /* Figure out what name to look up. */
9871 name
= TREE_OPERAND (qualified_id
, 1);
9872 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
9875 template_args
= TREE_OPERAND (name
, 1);
9877 template_args
= tsubst_template_args (template_args
, args
,
9879 name
= TREE_OPERAND (name
, 0);
9883 is_template
= false;
9884 template_args
= NULL_TREE
;
9887 /* Substitute into the qualifying scope. When there are no ARGS, we
9888 are just trying to simplify a non-dependent expression. In that
9889 case the qualifying scope may be dependent, and, in any case,
9890 substituting will not help. */
9891 scope
= TREE_OPERAND (qualified_id
, 0);
9894 scope
= tsubst (scope
, args
, complain
, in_decl
);
9895 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
9900 if (dependent_type_p (scope
))
9901 return build_qualified_name (/*type=*/NULL_TREE
,
9903 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
9905 if (!BASELINK_P (name
) && !DECL_P (expr
))
9907 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
9908 /* If this were actually a destructor call, it would have been
9909 parsed as such by the parser. */
9910 expr
= error_mark_node
;
9912 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
9913 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
9914 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
9916 if (complain
& tf_error
)
9918 error ("dependent-name %qE is parsed as a non-type, but "
9919 "instantiation yields a type", qualified_id
);
9920 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
9922 return error_mark_node
;
9928 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
9930 /* Remember that there was a reference to this entity. */
9934 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
9936 if (complain
& tf_error
)
9937 qualified_name_lookup_error (scope
,
9938 TREE_OPERAND (qualified_id
, 1),
9939 expr
, input_location
);
9940 return error_mark_node
;
9944 expr
= lookup_template_function (expr
, template_args
);
9946 if (expr
== error_mark_node
&& complain
& tf_error
)
9947 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
9948 expr
, input_location
);
9949 else if (TYPE_P (scope
))
9951 expr
= (adjust_result_of_qualified_name_lookup
9952 (expr
, scope
, current_class_type
));
9953 expr
= (finish_qualified_id_expr
9954 (scope
, expr
, done
, address_p
,
9955 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
9956 /*template_arg_p=*/false));
9959 /* Expressions do not generally have reference type. */
9960 if (TREE_CODE (expr
) != SCOPE_REF
9961 /* However, if we're about to form a pointer-to-member, we just
9962 want the referenced member referenced. */
9963 && TREE_CODE (expr
) != OFFSET_REF
)
9964 expr
= convert_from_reference (expr
);
9969 /* Like tsubst, but deals with expressions. This function just replaces
9970 template parms; to finish processing the resultant expression, use
9974 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9976 enum tree_code code
;
9979 if (t
== NULL_TREE
|| t
== error_mark_node
)
9982 code
= TREE_CODE (t
);
9987 r
= retrieve_local_specialization (t
);
9991 /* This can happen for a parameter name used later in a function
9992 declaration (such as in a late-specified return type).
9993 Replace it with an arbitrary expression with the same type
9994 (*(T*)0). This should only occur in an unevaluated context
9996 gcc_assert (skip_evaluation
);
9997 r
= non_reference (TREE_TYPE (t
));
9998 r
= tsubst (r
, args
, complain
, in_decl
);
9999 r
= build_pointer_type (r
);
10000 r
= build_c_cast (r
, null_node
);
10001 return cp_build_indirect_ref (r
, NULL
, tf_warning_or_error
);
10004 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
10005 r
= ARGUMENT_PACK_SELECT_ARG (r
);
10014 if (DECL_TEMPLATE_PARM_P (t
))
10015 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
10016 /* There is no need to substitute into namespace-scope
10018 if (DECL_NAMESPACE_SCOPE_P (t
))
10020 /* If ARGS is NULL, then T is known to be non-dependent. */
10021 if (args
== NULL_TREE
)
10022 return integral_constant_value (t
);
10024 /* Unfortunately, we cannot just call lookup_name here.
10027 template <int I> int f() {
10029 struct S { void g() { E e = a; } };
10032 When we instantiate f<7>::S::g(), say, lookup_name is not
10033 clever enough to find f<7>::a. */
10035 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
10036 /*entering_scope=*/0);
10038 for (v
= TYPE_VALUES (enum_type
);
10040 v
= TREE_CHAIN (v
))
10041 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
10042 return TREE_VALUE (v
);
10044 /* We didn't find the name. That should never happen; if
10045 name-lookup found it during preliminary parsing, we
10046 should find it again here during instantiation. */
10047 gcc_unreachable ();
10052 if (DECL_CONTEXT (t
))
10056 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
10057 /*entering_scope=*/1);
10058 if (ctx
!= DECL_CONTEXT (t
))
10060 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
10063 /* Suppress the error message if we are processing a lock
10065 if (parsing_lock_attribute
)
10067 if (complain
& tf_error
)
10068 error ("using invalid field %qD", t
);
10069 return error_mark_node
;
10078 case FUNCTION_DECL
:
10079 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
10080 || local_variable_p (t
))
10081 t
= tsubst (t
, args
, complain
, in_decl
);
10086 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
10088 case TEMPLATE_DECL
:
10089 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10090 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
10091 args
, complain
, in_decl
);
10092 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
10093 return tsubst (t
, args
, complain
, in_decl
);
10094 else if (DECL_CLASS_SCOPE_P (t
)
10095 && uses_template_parms (DECL_CONTEXT (t
)))
10097 /* Template template argument like the following example need
10100 template <template <class> class TT> struct C {};
10101 template <class T> struct D {
10102 template <class U> struct E {};
10107 We are processing the template argument `E' in #1 for
10108 the template instantiation #2. Originally, `E' is a
10109 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10110 have to substitute this with one having context `D<int>'. */
10112 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
10113 return lookup_field (context
, DECL_NAME(t
), 0, false);
10116 /* Ordinary template template argument. */
10120 case REINTERPRET_CAST_EXPR
:
10121 case CONST_CAST_EXPR
:
10122 case STATIC_CAST_EXPR
:
10123 case DYNAMIC_CAST_EXPR
:
10126 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10127 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
10130 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
10132 /* We only want to compute the number of arguments. */
10133 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
10134 complain
, in_decl
);
10137 if (TREE_CODE (expanded
) == TREE_VEC
)
10138 len
= TREE_VEC_LENGTH (expanded
);
10140 if (expanded
== error_mark_node
)
10141 return error_mark_node
;
10142 else if (PACK_EXPANSION_P (expanded
)
10143 || (TREE_CODE (expanded
) == TREE_VEC
10145 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
10147 if (TREE_CODE (expanded
) == TREE_VEC
)
10148 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
10150 if (TYPE_P (expanded
))
10151 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
10152 complain
& tf_error
);
10154 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
10155 complain
& tf_error
);
10158 return build_int_cst (size_type_node
, len
);
10164 case TRUTH_NOT_EXPR
:
10167 case UNARY_PLUS_EXPR
: /* Unary + */
10172 case REALPART_EXPR
:
10173 case IMAGPART_EXPR
:
10175 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10176 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
10178 case COMPONENT_REF
:
10183 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
10184 name
= TREE_OPERAND (t
, 1);
10185 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
10187 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10188 complain
, in_decl
);
10189 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
10191 else if (TREE_CODE (name
) == SCOPE_REF
10192 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
10194 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10195 complain
, in_decl
);
10196 name
= TREE_OPERAND (name
, 1);
10197 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
10198 complain
, in_decl
);
10199 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
10200 name
= build_qualified_name (/*type=*/NULL_TREE
,
10202 /*template_p=*/false);
10204 else if (TREE_CODE (name
) == BASELINK
)
10205 name
= tsubst_baselink (name
,
10206 non_reference (TREE_TYPE (object
)),
10210 name
= tsubst_copy (name
, args
, complain
, in_decl
);
10211 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
10217 case TRUNC_DIV_EXPR
:
10218 case CEIL_DIV_EXPR
:
10219 case FLOOR_DIV_EXPR
:
10220 case ROUND_DIV_EXPR
:
10221 case EXACT_DIV_EXPR
:
10225 case TRUNC_MOD_EXPR
:
10226 case FLOOR_MOD_EXPR
:
10227 case TRUTH_ANDIF_EXPR
:
10228 case TRUTH_ORIF_EXPR
:
10229 case TRUTH_AND_EXPR
:
10230 case TRUTH_OR_EXPR
:
10243 case COMPOUND_EXPR
:
10246 case PREDECREMENT_EXPR
:
10247 case PREINCREMENT_EXPR
:
10248 case POSTDECREMENT_EXPR
:
10249 case POSTINCREMENT_EXPR
:
10251 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10252 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
10255 return build_qualified_name (/*type=*/NULL_TREE
,
10256 tsubst_copy (TREE_OPERAND (t
, 0),
10257 args
, complain
, in_decl
),
10258 tsubst_copy (TREE_OPERAND (t
, 1),
10259 args
, complain
, in_decl
),
10260 QUALIFIED_NAME_IS_TEMPLATE (t
));
10265 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10266 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
10267 NULL_TREE
, NULL_TREE
);
10271 int n
= VL_EXP_OPERAND_LENGTH (t
);
10272 tree result
= build_vl_exp (CALL_EXPR
, n
);
10274 for (i
= 0; i
< n
; i
++)
10275 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
10276 complain
, in_decl
);
10282 case PSEUDO_DTOR_EXPR
:
10285 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10286 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
10287 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
10288 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
10295 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10296 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
10297 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
10298 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
10305 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
10306 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
10307 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
10308 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
10312 case TEMPLATE_ID_EXPR
:
10314 /* Substituted template arguments */
10315 tree fn
= TREE_OPERAND (t
, 0);
10316 tree targs
= TREE_OPERAND (t
, 1);
10318 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
10320 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
10322 return lookup_template_function (fn
, targs
);
10327 tree purpose
, value
, chain
;
10329 if (t
== void_list_node
)
10332 purpose
= TREE_PURPOSE (t
);
10334 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
10335 value
= TREE_VALUE (t
);
10337 value
= tsubst_copy (value
, args
, complain
, in_decl
);
10338 chain
= TREE_CHAIN (t
);
10339 if (chain
&& chain
!= void_type_node
)
10340 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
10341 if (purpose
== TREE_PURPOSE (t
)
10342 && value
== TREE_VALUE (t
)
10343 && chain
== TREE_CHAIN (t
))
10345 return tree_cons (purpose
, value
, chain
);
10350 case ENUMERAL_TYPE
:
10352 case TEMPLATE_TYPE_PARM
:
10353 case TEMPLATE_TEMPLATE_PARM
:
10354 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10355 case TEMPLATE_PARM_INDEX
:
10357 case REFERENCE_TYPE
:
10359 case FUNCTION_TYPE
:
10362 case TYPENAME_TYPE
:
10363 case UNBOUND_CLASS_TEMPLATE
:
10365 case DECLTYPE_TYPE
:
10367 return tsubst (t
, args
, complain
, in_decl
);
10369 case IDENTIFIER_NODE
:
10370 if (IDENTIFIER_TYPENAME_P (t
))
10372 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10373 return mangle_conv_op_name_for_type (new_type
);
10379 /* This is handled by tsubst_copy_and_build. */
10380 gcc_unreachable ();
10383 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
10385 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
10387 case CLEANUP_POINT_EXPR
:
10388 /* We shouldn't have built any of these during initial template
10389 generation. Instead, they should be built during instantiation
10390 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10391 gcc_unreachable ();
10394 mark_used (TREE_OPERAND (t
, 1));
10397 case EXPR_PACK_EXPANSION
:
10398 error ("invalid use of pack expansion expression");
10399 return error_mark_node
;
10401 case NONTYPE_ARGUMENT_PACK
:
10402 error ("use %<...%> to expand argument pack");
10403 return error_mark_node
;
10410 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10413 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
10416 tree new_clauses
= NULL
, nc
, oc
;
10418 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
10420 nc
= copy_node (oc
);
10421 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
10424 switch (OMP_CLAUSE_CODE (nc
))
10426 case OMP_CLAUSE_LASTPRIVATE
:
10427 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
10429 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
10430 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
10431 in_decl
, /*integral_constant_expression_p=*/false);
10432 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
10433 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
10436 case OMP_CLAUSE_PRIVATE
:
10437 case OMP_CLAUSE_SHARED
:
10438 case OMP_CLAUSE_FIRSTPRIVATE
:
10439 case OMP_CLAUSE_REDUCTION
:
10440 case OMP_CLAUSE_COPYIN
:
10441 case OMP_CLAUSE_COPYPRIVATE
:
10442 case OMP_CLAUSE_IF
:
10443 case OMP_CLAUSE_NUM_THREADS
:
10444 case OMP_CLAUSE_SCHEDULE
:
10445 case OMP_CLAUSE_COLLAPSE
:
10446 OMP_CLAUSE_OPERAND (nc
, 0)
10447 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
10448 in_decl
, /*integral_constant_expression_p=*/false);
10450 case OMP_CLAUSE_NOWAIT
:
10451 case OMP_CLAUSE_ORDERED
:
10452 case OMP_CLAUSE_DEFAULT
:
10453 case OMP_CLAUSE_UNTIED
:
10456 gcc_unreachable ();
10460 return finish_omp_clauses (nreverse (new_clauses
));
10463 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10466 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
10469 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10471 tree purpose
, value
, chain
;
10476 if (TREE_CODE (t
) != TREE_LIST
)
10477 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10478 /*function_p=*/false,
10479 /*integral_constant_expression_p=*/false);
10481 if (t
== void_list_node
)
10484 purpose
= TREE_PURPOSE (t
);
10486 purpose
= RECUR (purpose
);
10487 value
= TREE_VALUE (t
);
10489 value
= RECUR (value
);
10490 chain
= TREE_CHAIN (t
);
10491 if (chain
&& chain
!= void_type_node
)
10492 chain
= RECUR (chain
);
10493 return tree_cons (purpose
, value
, chain
);
10497 /* Substitute one OMP_FOR iterator. */
10500 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
10501 tree condv
, tree incrv
, tree
*clauses
,
10502 tree args
, tsubst_flags_t complain
, tree in_decl
,
10503 bool integral_constant_expression_p
)
10505 #define RECUR(NODE) \
10506 tsubst_expr ((NODE), args, complain, in_decl, \
10507 integral_constant_expression_p)
10508 tree decl
, init
, cond
, incr
, auto_node
;
10510 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
10511 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
10512 decl
= RECUR (TREE_OPERAND (init
, 0));
10513 init
= TREE_OPERAND (init
, 1);
10514 auto_node
= type_uses_auto (TREE_TYPE (decl
));
10515 if (auto_node
&& init
)
10517 tree init_expr
= init
;
10518 if (TREE_CODE (init_expr
) == DECL_EXPR
)
10519 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
10520 init_expr
= RECUR (init_expr
);
10522 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
10524 gcc_assert (!type_dependent_expression_p (decl
));
10526 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
10528 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
10529 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
10530 if (TREE_CODE (incr
) == MODIFY_EXPR
)
10531 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
10532 RECUR (TREE_OPERAND (incr
, 1)),
10535 incr
= RECUR (incr
);
10536 TREE_VEC_ELT (declv
, i
) = decl
;
10537 TREE_VEC_ELT (initv
, i
) = init
;
10538 TREE_VEC_ELT (condv
, i
) = cond
;
10539 TREE_VEC_ELT (incrv
, i
) = incr
;
10543 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
10546 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
10548 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
10549 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
10550 && OMP_CLAUSE_DECL (c
) == decl
)
10552 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
10553 && OMP_CLAUSE_DECL (c
) == decl
)
10554 error ("iteration variable %qD should not be firstprivate", decl
);
10555 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
10556 && OMP_CLAUSE_DECL (c
) == decl
)
10557 error ("iteration variable %qD should not be reduction", decl
);
10561 c
= build_omp_clause (OMP_CLAUSE_PRIVATE
);
10562 OMP_CLAUSE_DECL (c
) = decl
;
10563 c
= finish_omp_clauses (c
);
10566 OMP_CLAUSE_CHAIN (c
) = *clauses
;
10571 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
10572 if (COMPARISON_CLASS_P (cond
))
10573 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
10574 RECUR (TREE_OPERAND (cond
, 0)),
10575 RECUR (TREE_OPERAND (cond
, 1)));
10577 cond
= RECUR (cond
);
10578 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
10579 switch (TREE_CODE (incr
))
10581 case PREINCREMENT_EXPR
:
10582 case PREDECREMENT_EXPR
:
10583 case POSTINCREMENT_EXPR
:
10584 case POSTDECREMENT_EXPR
:
10585 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
10586 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
10589 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
10590 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
10592 tree rhs
= TREE_OPERAND (incr
, 1);
10593 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
10594 RECUR (TREE_OPERAND (incr
, 0)),
10595 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
10596 RECUR (TREE_OPERAND (rhs
, 0)),
10597 RECUR (TREE_OPERAND (rhs
, 1))));
10600 incr
= RECUR (incr
);
10603 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
10604 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
10606 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
10607 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
10608 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
10609 TREE_TYPE (decl
), lhs
,
10610 RECUR (TREE_OPERAND (incr
, 2))));
10612 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
10613 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
10614 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
10616 tree rhs
= TREE_OPERAND (incr
, 2);
10617 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
10618 RECUR (TREE_OPERAND (incr
, 0)),
10619 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
10620 RECUR (TREE_OPERAND (rhs
, 0)),
10621 RECUR (TREE_OPERAND (rhs
, 1))));
10624 incr
= RECUR (incr
);
10627 incr
= RECUR (incr
);
10631 TREE_VEC_ELT (declv
, i
) = decl
;
10632 TREE_VEC_ELT (initv
, i
) = init
;
10633 TREE_VEC_ELT (condv
, i
) = cond
;
10634 TREE_VEC_ELT (incrv
, i
) = incr
;
10638 /* Like tsubst_copy for expressions, etc. but also does semantic
10642 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
10643 bool integral_constant_expression_p
)
10645 #define RECUR(NODE) \
10646 tsubst_expr ((NODE), args, complain, in_decl, \
10647 integral_constant_expression_p)
10651 if (t
== NULL_TREE
|| t
== error_mark_node
)
10654 if (EXPR_HAS_LOCATION (t
))
10655 input_location
= EXPR_LOCATION (t
);
10656 if (STATEMENT_CODE_P (TREE_CODE (t
)))
10657 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
10659 switch (TREE_CODE (t
))
10661 case STATEMENT_LIST
:
10663 tree_stmt_iterator i
;
10664 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
10665 RECUR (tsi_stmt (i
));
10669 case CTOR_INITIALIZER
:
10670 finish_mem_initializers (tsubst_initializer_list
10671 (TREE_OPERAND (t
, 0), args
));
10675 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
10679 tmp
= RECUR (EXPR_STMT_EXPR (t
));
10680 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
10681 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
10683 finish_expr_stmt (tmp
);
10687 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
10695 decl
= DECL_EXPR_DECL (t
);
10696 if (TREE_CODE (decl
) == LABEL_DECL
)
10697 finish_label_decl (DECL_NAME (decl
));
10698 else if (TREE_CODE (decl
) == USING_DECL
)
10700 tree scope
= USING_DECL_SCOPE (decl
);
10701 tree name
= DECL_NAME (decl
);
10704 scope
= RECUR (scope
);
10705 decl
= lookup_qualified_name (scope
, name
,
10706 /*is_type_p=*/false,
10707 /*complain=*/false);
10708 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
10709 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
10711 do_local_using_decl (decl
, scope
, name
);
10715 init
= DECL_INITIAL (decl
);
10716 decl
= tsubst (decl
, args
, complain
, in_decl
);
10717 if (decl
!= error_mark_node
)
10719 /* By marking the declaration as instantiated, we avoid
10720 trying to instantiate it. Since instantiate_decl can't
10721 handle local variables, and since we've already done
10722 all that needs to be done, that's the right thing to
10724 if (TREE_CODE (decl
) == VAR_DECL
)
10725 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10726 if (TREE_CODE (decl
) == VAR_DECL
10727 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
10728 /* Anonymous aggregates are a special case. */
10729 finish_anon_union (decl
);
10732 maybe_push_decl (decl
);
10733 if (TREE_CODE (decl
) == VAR_DECL
10734 && DECL_PRETTY_FUNCTION_P (decl
))
10736 /* For __PRETTY_FUNCTION__ we have to adjust the
10738 const char *const name
10739 = cxx_printable_name (current_function_decl
, 2);
10740 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
10744 tree t
= RECUR (init
);
10747 /* If we had an initializer but it
10748 instantiated to nothing,
10749 value-initialize the object. This will
10750 only occur when the initializer was a
10751 pack expansion where the parameter packs
10752 used in that expansion were of length
10754 init
= build_value_init (TREE_TYPE (decl
));
10759 finish_decl (decl
, init
, NULL_TREE
);
10764 /* A DECL_EXPR can also be used as an expression, in the condition
10765 clause of an if/for/while construct. */
10770 stmt
= begin_for_stmt ();
10771 RECUR (FOR_INIT_STMT (t
));
10772 finish_for_init_stmt (stmt
);
10773 tmp
= RECUR (FOR_COND (t
));
10774 finish_for_cond (tmp
, stmt
);
10775 tmp
= RECUR (FOR_EXPR (t
));
10776 finish_for_expr (tmp
, stmt
);
10777 RECUR (FOR_BODY (t
));
10778 finish_for_stmt (stmt
);
10782 stmt
= begin_while_stmt ();
10783 tmp
= RECUR (WHILE_COND (t
));
10784 finish_while_stmt_cond (tmp
, stmt
);
10785 RECUR (WHILE_BODY (t
));
10786 finish_while_stmt (stmt
);
10790 stmt
= begin_do_stmt ();
10791 RECUR (DO_BODY (t
));
10792 finish_do_body (stmt
);
10793 tmp
= RECUR (DO_COND (t
));
10794 finish_do_stmt (tmp
, stmt
);
10798 stmt
= begin_if_stmt ();
10799 tmp
= RECUR (IF_COND (t
));
10800 finish_if_stmt_cond (tmp
, stmt
);
10801 RECUR (THEN_CLAUSE (t
));
10802 finish_then_clause (stmt
);
10804 if (ELSE_CLAUSE (t
))
10806 begin_else_clause (stmt
);
10807 RECUR (ELSE_CLAUSE (t
));
10808 finish_else_clause (stmt
);
10811 finish_if_stmt (stmt
);
10815 if (BIND_EXPR_BODY_BLOCK (t
))
10816 stmt
= begin_function_body ();
10818 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
10819 ? BCS_TRY_BLOCK
: 0);
10821 RECUR (BIND_EXPR_BODY (t
));
10823 if (BIND_EXPR_BODY_BLOCK (t
))
10824 finish_function_body (stmt
);
10826 finish_compound_stmt (stmt
);
10830 finish_break_stmt ();
10833 case CONTINUE_STMT
:
10834 finish_continue_stmt ();
10838 stmt
= begin_switch_stmt ();
10839 tmp
= RECUR (SWITCH_STMT_COND (t
));
10840 finish_switch_cond (tmp
, stmt
);
10841 RECUR (SWITCH_STMT_BODY (t
));
10842 finish_switch_stmt (stmt
);
10845 case CASE_LABEL_EXPR
:
10846 finish_case_label (RECUR (CASE_LOW (t
)),
10847 RECUR (CASE_HIGH (t
)));
10851 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t
)));
10855 tmp
= GOTO_DESTINATION (t
);
10856 if (TREE_CODE (tmp
) != LABEL_DECL
)
10857 /* Computed goto's must be tsubst'd into. On the other hand,
10858 non-computed gotos must not be; the identifier in question
10859 will have no binding. */
10862 tmp
= DECL_NAME (tmp
);
10863 finish_goto_stmt (tmp
);
10867 tmp
= finish_asm_stmt
10868 (ASM_VOLATILE_P (t
),
10869 RECUR (ASM_STRING (t
)),
10870 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
10871 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
10872 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
10874 tree asm_expr
= tmp
;
10875 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
10876 asm_expr
= TREE_OPERAND (asm_expr
, 0);
10877 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
10884 stmt
= begin_try_block ();
10885 RECUR (TRY_STMTS (t
));
10886 finish_cleanup_try_block (stmt
);
10887 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
10891 tree compound_stmt
= NULL_TREE
;
10893 if (FN_TRY_BLOCK_P (t
))
10894 stmt
= begin_function_try_block (&compound_stmt
);
10896 stmt
= begin_try_block ();
10898 RECUR (TRY_STMTS (t
));
10900 if (FN_TRY_BLOCK_P (t
))
10901 finish_function_try_block (stmt
);
10903 finish_try_block (stmt
);
10905 RECUR (TRY_HANDLERS (t
));
10906 if (FN_TRY_BLOCK_P (t
))
10907 finish_function_handler_sequence (stmt
, compound_stmt
);
10909 finish_handler_sequence (stmt
);
10915 tree decl
= HANDLER_PARMS (t
);
10919 decl
= tsubst (decl
, args
, complain
, in_decl
);
10920 /* Prevent instantiate_decl from trying to instantiate
10921 this variable. We've already done all that needs to be
10923 if (decl
!= error_mark_node
)
10924 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10926 stmt
= begin_handler ();
10927 finish_handler_parms (decl
, stmt
);
10928 RECUR (HANDLER_BODY (t
));
10929 finish_handler (stmt
);
10934 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
10937 case STATIC_ASSERT
:
10940 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
10943 /*integral_constant_expression_p=*/true);
10944 finish_static_assert (condition
,
10945 STATIC_ASSERT_MESSAGE (t
),
10946 STATIC_ASSERT_SOURCE_LOCATION (t
),
10947 /*member_p=*/false);
10952 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
10953 args
, complain
, in_decl
);
10954 stmt
= begin_omp_parallel ();
10955 RECUR (OMP_PARALLEL_BODY (t
));
10956 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
10957 = OMP_PARALLEL_COMBINED (t
);
10961 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
10962 args
, complain
, in_decl
);
10963 stmt
= begin_omp_task ();
10964 RECUR (OMP_TASK_BODY (t
));
10965 finish_omp_task (tmp
, stmt
);
10970 tree clauses
, body
, pre_body
;
10971 tree declv
, initv
, condv
, incrv
;
10974 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
10975 args
, complain
, in_decl
);
10976 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
10977 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
10978 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
10979 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
10981 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
10982 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
10983 &clauses
, args
, complain
, in_decl
,
10984 integral_constant_expression_p
);
10986 stmt
= begin_omp_structured_block ();
10988 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
10989 if (TREE_VEC_ELT (initv
, i
) == NULL
10990 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
10991 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
10992 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
10994 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
10995 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
10996 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
11000 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
11001 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
11002 gcc_assert (init
!= NULL
);
11003 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
11004 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
11006 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
11009 pre_body
= push_stmt_list ();
11010 RECUR (OMP_FOR_PRE_BODY (t
));
11011 pre_body
= pop_stmt_list (pre_body
);
11013 body
= push_stmt_list ();
11014 RECUR (OMP_FOR_BODY (t
));
11015 body
= pop_stmt_list (body
);
11017 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
11018 body
, pre_body
, clauses
);
11020 add_stmt (finish_omp_structured_block (stmt
));
11026 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
11027 stmt
= push_stmt_list ();
11028 RECUR (OMP_BODY (t
));
11029 stmt
= pop_stmt_list (stmt
);
11032 OMP_BODY (t
) = stmt
;
11033 OMP_CLAUSES (t
) = tmp
;
11041 stmt
= push_stmt_list ();
11042 RECUR (OMP_BODY (t
));
11043 stmt
= pop_stmt_list (stmt
);
11046 OMP_BODY (t
) = stmt
;
11051 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
11053 tree op1
= TREE_OPERAND (t
, 1);
11054 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
11055 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
11056 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
11060 case EXPR_PACK_EXPANSION
:
11061 error ("invalid use of pack expansion expression");
11062 return error_mark_node
;
11064 case NONTYPE_ARGUMENT_PACK
:
11065 error ("use %<...%> to expand argument pack");
11066 return error_mark_node
;
11069 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
11071 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11072 /*function_p=*/false,
11073 integral_constant_expression_p
);
11080 /* T is a postfix-expression that is not being used in a function
11081 call. Return the substituted version of T. */
11084 tsubst_non_call_postfix_expression (tree t
, tree args
,
11085 tsubst_flags_t complain
,
11088 if (TREE_CODE (t
) == SCOPE_REF
)
11089 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
11090 /*done=*/false, /*address_p=*/false);
11092 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11093 /*function_p=*/false,
11094 /*integral_constant_expression_p=*/false);
11099 /* Like tsubst but deals with expressions and performs semantic
11100 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11103 tsubst_copy_and_build (tree t
,
11105 tsubst_flags_t complain
,
11108 bool integral_constant_expression_p
)
11110 #define RECUR(NODE) \
11111 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11112 /*function_p=*/false, \
11113 integral_constant_expression_p)
11117 if (t
== NULL_TREE
|| t
== error_mark_node
)
11120 switch (TREE_CODE (t
))
11124 /* Fall through. */
11125 case IDENTIFIER_NODE
:
11129 bool non_integral_constant_expression_p
;
11130 const char *error_msg
;
11132 if (IDENTIFIER_TYPENAME_P (t
))
11134 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11135 t
= mangle_conv_op_name_for_type (new_type
);
11138 /* Look up the name. */
11139 decl
= lookup_name (t
);
11141 /* By convention, expressions use ERROR_MARK_NODE to indicate
11142 failure, not NULL_TREE. */
11143 if (decl
== NULL_TREE
)
11144 decl
= error_mark_node
;
11146 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
11148 integral_constant_expression_p
,
11149 /*allow_non_integral_constant_expression_p=*/false,
11150 &non_integral_constant_expression_p
,
11151 /*template_p=*/false,
11153 /*address_p=*/false,
11154 /*template_arg_p=*/false,
11159 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
11160 decl
= unqualified_name_lookup_error (decl
);
11164 case TEMPLATE_ID_EXPR
:
11167 tree templ
= RECUR (TREE_OPERAND (t
, 0));
11168 tree targs
= TREE_OPERAND (t
, 1);
11171 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11173 if (TREE_CODE (templ
) == COMPONENT_REF
)
11175 object
= TREE_OPERAND (templ
, 0);
11176 templ
= TREE_OPERAND (templ
, 1);
11179 object
= NULL_TREE
;
11180 templ
= lookup_template_function (templ
, targs
);
11183 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
11184 object
, templ
, NULL_TREE
);
11186 return baselink_for_fns (templ
);
11191 tree r
= RECUR (TREE_OPERAND (t
, 0));
11193 if (REFERENCE_REF_P (t
))
11195 /* A type conversion to reference type will be enclosed in
11196 such an indirect ref, but the substitution of the cast
11197 will have also added such an indirect ref. */
11198 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
11199 r
= convert_from_reference (r
);
11202 r
= build_x_indirect_ref (r
, "unary *", complain
);
11208 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11209 RECUR (TREE_OPERAND (t
, 0)));
11212 case REINTERPRET_CAST_EXPR
:
11213 case CONST_CAST_EXPR
:
11214 case DYNAMIC_CAST_EXPR
:
11215 case STATIC_CAST_EXPR
:
11220 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11221 if (integral_constant_expression_p
11222 && !cast_valid_in_integral_constant_expression_p (type
))
11224 if (complain
& tf_error
)
11225 error ("a cast to a type other than an integral or "
11226 "enumeration type cannot appear in a constant-expression");
11227 return error_mark_node
;
11230 op
= RECUR (TREE_OPERAND (t
, 0));
11232 switch (TREE_CODE (t
))
11235 return build_functional_cast (type
, op
, complain
);
11236 case REINTERPRET_CAST_EXPR
:
11237 return build_reinterpret_cast (type
, op
, complain
);
11238 case CONST_CAST_EXPR
:
11239 return build_const_cast (type
, op
, complain
);
11240 case DYNAMIC_CAST_EXPR
:
11241 return build_dynamic_cast (type
, op
, complain
);
11242 case STATIC_CAST_EXPR
:
11243 return build_static_cast (type
, op
, complain
);
11245 gcc_unreachable ();
11249 case POSTDECREMENT_EXPR
:
11250 case POSTINCREMENT_EXPR
:
11251 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11252 args
, complain
, in_decl
);
11253 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
11255 case PREDECREMENT_EXPR
:
11256 case PREINCREMENT_EXPR
:
11260 case TRUTH_NOT_EXPR
:
11261 case UNARY_PLUS_EXPR
: /* Unary + */
11262 case REALPART_EXPR
:
11263 case IMAGPART_EXPR
:
11264 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
11268 op1
= TREE_OPERAND (t
, 0);
11269 if (TREE_CODE (op1
) == SCOPE_REF
)
11270 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
11271 /*done=*/true, /*address_p=*/true);
11273 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
11275 if (TREE_CODE (op1
) == LABEL_DECL
)
11276 return finish_label_address_expr (DECL_NAME (op1
),
11277 EXPR_LOCATION (op1
));
11278 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
11283 case TRUNC_DIV_EXPR
:
11284 case CEIL_DIV_EXPR
:
11285 case FLOOR_DIV_EXPR
:
11286 case ROUND_DIV_EXPR
:
11287 case EXACT_DIV_EXPR
:
11291 case TRUNC_MOD_EXPR
:
11292 case FLOOR_MOD_EXPR
:
11293 case TRUTH_ANDIF_EXPR
:
11294 case TRUTH_ORIF_EXPR
:
11295 case TRUTH_AND_EXPR
:
11296 case TRUTH_OR_EXPR
:
11311 return build_x_binary_op
11313 RECUR (TREE_OPERAND (t
, 0)),
11314 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
11316 : TREE_CODE (TREE_OPERAND (t
, 0))),
11317 RECUR (TREE_OPERAND (t
, 1)),
11318 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
11320 : TREE_CODE (TREE_OPERAND (t
, 1))),
11321 /*overloaded_p=*/NULL
,
11325 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
11326 /*address_p=*/false);
11328 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11329 args
, complain
, in_decl
);
11330 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
11333 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11334 return tsubst_copy (t
, args
, complain
, in_decl
);
11338 op1
= TREE_OPERAND (t
, 0);
11341 /* When there are no ARGS, we are trying to evaluate a
11342 non-dependent expression from the parser. Trying to do
11343 the substitutions may not work. */
11345 op1
= TREE_TYPE (op1
);
11350 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
11351 /*function_p=*/false,
11352 /*integral_constant_expression_p=*/false);
11356 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
11357 complain
& tf_error
);
11359 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
11360 complain
& tf_error
);
11364 tree r
= build_x_modify_expr
11365 (RECUR (TREE_OPERAND (t
, 0)),
11366 TREE_CODE (TREE_OPERAND (t
, 1)),
11367 RECUR (TREE_OPERAND (t
, 2)),
11369 /* TREE_NO_WARNING must be set if either the expression was
11370 parenthesized or it uses an operator such as >>= rather
11371 than plain assignment. In the former case, it was already
11372 set and must be copied. In the latter case,
11373 build_x_modify_expr sets it and it must not be reset
11375 if (TREE_NO_WARNING (t
))
11376 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11381 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11382 args
, complain
, in_decl
);
11383 /* Remember that there was a reference to this entity. */
11386 return build_x_arrow (op1
);
11390 tree init
= RECUR (TREE_OPERAND (t
, 3));
11392 if (TREE_OPERAND (t
, 3) && !init
)
11393 /* If there was an initializer in the original tree, but
11394 it instantiated to an empty list, then we should pass on
11395 VOID_ZERO_NODE to tell build_new that it was an empty
11396 initializer () rather than no initializer. This can only
11397 happen when the initializer is a pack expansion whose
11398 parameter packs are of length zero. */
11399 init
= void_zero_node
;
11402 (RECUR (TREE_OPERAND (t
, 0)),
11403 RECUR (TREE_OPERAND (t
, 1)),
11404 RECUR (TREE_OPERAND (t
, 2)),
11406 NEW_EXPR_USE_GLOBAL (t
),
11411 return delete_sanity
11412 (RECUR (TREE_OPERAND (t
, 0)),
11413 RECUR (TREE_OPERAND (t
, 1)),
11414 DELETE_EXPR_USE_VEC (t
),
11415 DELETE_EXPR_USE_GLOBAL (t
));
11417 case COMPOUND_EXPR
:
11418 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
11419 RECUR (TREE_OPERAND (t
, 1)),
11429 function
= CALL_EXPR_FN (t
);
11430 /* When we parsed the expression, we determined whether or
11431 not Koenig lookup should be performed. */
11432 koenig_p
= KOENIG_LOOKUP_P (t
);
11433 if (TREE_CODE (function
) == SCOPE_REF
)
11435 qualified_p
= true;
11436 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
11438 /*address_p=*/false);
11442 if (TREE_CODE (function
) == COMPONENT_REF
)
11444 tree op
= TREE_OPERAND (function
, 1);
11446 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
11447 || (BASELINK_P (op
)
11448 && BASELINK_QUALIFIED_P (op
)));
11451 qualified_p
= false;
11453 function
= tsubst_copy_and_build (function
, args
, complain
,
11456 integral_constant_expression_p
);
11458 if (BASELINK_P (function
))
11459 qualified_p
= true;
11462 /* FIXME: Rewrite this so as not to construct an arglist. */
11463 call_args
= RECUR (CALL_EXPR_ARGS (t
));
11465 /* We do not perform argument-dependent lookup if normal
11466 lookup finds a non-function, in accordance with the
11467 expected resolution of DR 218. */
11469 && ((is_overloaded_fn (function
)
11470 /* If lookup found a member function, the Koenig lookup is
11471 not appropriate, even if an unqualified-name was used
11472 to denote the function. */
11473 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
11474 || TREE_CODE (function
) == IDENTIFIER_NODE
))
11475 function
= perform_koenig_lookup (function
, call_args
);
11477 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
11479 unqualified_name_lookup_error (function
);
11480 return error_mark_node
;
11483 /* Remember that there was a reference to this entity. */
11484 if (DECL_P (function
))
11485 mark_used (function
);
11487 if (TREE_CODE (function
) == OFFSET_REF
)
11488 return build_offset_ref_call_from_tree (function
, call_args
);
11489 if (TREE_CODE (function
) == COMPONENT_REF
)
11491 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
11492 return finish_call_expr (function
, call_args
,
11493 /*disallow_virtual=*/false,
11494 /*koenig_p=*/false,
11497 return (build_new_method_call
11498 (TREE_OPERAND (function
, 0),
11499 TREE_OPERAND (function
, 1),
11500 call_args
, NULL_TREE
,
11501 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
11505 /* Pass -1 for koenig_p so that build_new_function_call will
11506 allow hidden friends found by arg-dependent lookup at template
11508 return finish_call_expr (function
, call_args
,
11509 /*disallow_virtual=*/qualified_p
,
11515 return build_x_conditional_expr
11516 (RECUR (TREE_OPERAND (t
, 0)),
11517 RECUR (TREE_OPERAND (t
, 1)),
11518 RECUR (TREE_OPERAND (t
, 2)),
11521 case PSEUDO_DTOR_EXPR
:
11522 return finish_pseudo_destructor_expr
11523 (RECUR (TREE_OPERAND (t
, 0)),
11524 RECUR (TREE_OPERAND (t
, 1)),
11525 RECUR (TREE_OPERAND (t
, 2)));
11529 tree purpose
, value
, chain
;
11531 if (t
== void_list_node
)
11534 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
11535 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
11537 /* We have pack expansions, so expand those and
11538 create a new list out of it. */
11539 tree purposevec
= NULL_TREE
;
11540 tree valuevec
= NULL_TREE
;
11544 /* Expand the argument expressions. */
11545 if (TREE_PURPOSE (t
))
11546 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
11547 complain
, in_decl
);
11548 if (TREE_VALUE (t
))
11549 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
11550 complain
, in_decl
);
11552 /* Build the rest of the list. */
11553 chain
= TREE_CHAIN (t
);
11554 if (chain
&& chain
!= void_type_node
)
11555 chain
= RECUR (chain
);
11557 /* Determine the number of arguments. */
11558 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
11560 len
= TREE_VEC_LENGTH (purposevec
);
11561 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
11563 else if (TREE_CODE (valuevec
) == TREE_VEC
)
11564 len
= TREE_VEC_LENGTH (valuevec
);
11567 /* Since we only performed a partial substitution into
11568 the argument pack, we only return a single list
11570 if (purposevec
== TREE_PURPOSE (t
)
11571 && valuevec
== TREE_VALUE (t
)
11572 && chain
== TREE_CHAIN (t
))
11575 return tree_cons (purposevec
, valuevec
, chain
);
11578 /* Convert the argument vectors into a TREE_LIST */
11582 /* Grab the Ith values. */
11584 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
11587 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
11590 /* Build the list (backwards). */
11591 chain
= tree_cons (purpose
, value
, chain
);
11597 purpose
= TREE_PURPOSE (t
);
11599 purpose
= RECUR (purpose
);
11600 value
= TREE_VALUE (t
);
11602 value
= RECUR (value
);
11603 chain
= TREE_CHAIN (t
);
11604 if (chain
&& chain
!= void_type_node
)
11605 chain
= RECUR (chain
);
11606 if (purpose
== TREE_PURPOSE (t
)
11607 && value
== TREE_VALUE (t
)
11608 && chain
== TREE_CHAIN (t
))
11610 return tree_cons (purpose
, value
, chain
);
11613 case COMPONENT_REF
:
11619 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
11620 args
, complain
, in_decl
);
11621 /* Remember that there was a reference to this entity. */
11622 if (DECL_P (object
))
11623 mark_used (object
);
11624 object_type
= TREE_TYPE (object
);
11626 member
= TREE_OPERAND (t
, 1);
11627 if (BASELINK_P (member
))
11628 member
= tsubst_baselink (member
,
11629 non_reference (TREE_TYPE (object
)),
11630 args
, complain
, in_decl
);
11632 member
= tsubst_copy (member
, args
, complain
, in_decl
);
11633 if (member
== error_mark_node
)
11634 return error_mark_node
;
11636 if (object_type
&& !CLASS_TYPE_P (object_type
))
11638 if (SCALAR_TYPE_P (object_type
))
11640 tree s
= NULL_TREE
;
11641 tree dtor
= member
;
11643 if (TREE_CODE (dtor
) == SCOPE_REF
)
11645 s
= TREE_OPERAND (dtor
, 0);
11646 dtor
= TREE_OPERAND (dtor
, 1);
11648 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
11650 dtor
= TREE_OPERAND (dtor
, 0);
11652 return finish_pseudo_destructor_expr (object
, s
, dtor
);
11656 else if (TREE_CODE (member
) == SCOPE_REF
11657 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
11662 /* Lookup the template functions now that we know what the
11664 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
11665 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
11666 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
11667 /*is_type_p=*/false,
11668 /*complain=*/false);
11669 if (BASELINK_P (member
))
11671 BASELINK_FUNCTIONS (member
)
11672 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
11674 member
= (adjust_result_of_qualified_name_lookup
11675 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
11680 qualified_name_lookup_error (object_type
, tmpl
, member
,
11682 return error_mark_node
;
11685 else if (TREE_CODE (member
) == SCOPE_REF
11686 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
11687 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
11689 if (complain
& tf_error
)
11691 if (TYPE_P (TREE_OPERAND (member
, 0)))
11692 error ("%qT is not a class or namespace",
11693 TREE_OPERAND (member
, 0));
11695 error ("%qD is not a class or namespace",
11696 TREE_OPERAND (member
, 0));
11698 return error_mark_node
;
11700 else if (TREE_CODE (member
) == FIELD_DECL
)
11701 return finish_non_static_data_member (member
, object
, NULL_TREE
);
11703 return finish_class_member_access_expr (object
, member
,
11704 /*template_p=*/false,
11710 (RECUR (TREE_OPERAND (t
, 0)));
11714 VEC(constructor_elt
,gc
) *n
;
11715 constructor_elt
*ce
;
11716 unsigned HOST_WIDE_INT idx
;
11717 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11718 bool process_index_p
;
11720 bool need_copy_p
= false;
11723 if (type
== error_mark_node
)
11724 return error_mark_node
;
11726 /* digest_init will do the wrong thing if we let it. */
11727 if (type
&& TYPE_PTRMEMFUNC_P (type
))
11730 /* We do not want to process the index of aggregate
11731 initializers as they are identifier nodes which will be
11732 looked up by digest_init. */
11733 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
11735 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
11736 newlen
= VEC_length (constructor_elt
, n
);
11737 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
11739 if (ce
->index
&& process_index_p
)
11740 ce
->index
= RECUR (ce
->index
);
11742 if (PACK_EXPANSION_P (ce
->value
))
11744 /* Substitute into the pack expansion. */
11745 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
11748 if (ce
->value
== error_mark_node
)
11750 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
11751 /* Just move the argument into place. */
11752 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
11755 /* Update the length of the final CONSTRUCTOR
11756 arguments vector, and note that we will need to
11758 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
11759 need_copy_p
= true;
11763 ce
->value
= RECUR (ce
->value
);
11768 VEC(constructor_elt
,gc
) *old_n
= n
;
11770 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
11771 for (idx
= 0; VEC_iterate (constructor_elt
, old_n
, idx
, ce
);
11774 if (TREE_CODE (ce
->value
) == TREE_VEC
)
11776 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
11777 for (i
= 0; i
< len
; ++i
)
11778 CONSTRUCTOR_APPEND_ELT (n
, 0,
11779 TREE_VEC_ELT (ce
->value
, i
));
11782 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
11786 r
= build_constructor (init_list_type_node
, n
);
11787 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
11789 if (TREE_HAS_CONSTRUCTOR (t
))
11790 return finish_compound_literal (type
, r
);
11797 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
11798 if (TYPE_P (operand_0
))
11799 return get_typeid (operand_0
);
11800 return build_typeid (operand_0
);
11810 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
11812 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
11813 /* If the original type was a reference, we'll be wrapped in
11814 the appropriate INDIRECT_REF. */
11815 r
= convert_from_reference (r
);
11820 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
11821 tsubst_copy (TREE_TYPE (t
), args
, complain
,
11824 case OFFSETOF_EXPR
:
11825 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
11829 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
11830 complain
, in_decl
);
11832 tree type2
= TRAIT_EXPR_TYPE2 (t
);
11834 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
11836 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
11841 tree old_stmt_expr
= cur_stmt_expr
;
11842 tree stmt_expr
= begin_stmt_expr ();
11844 cur_stmt_expr
= stmt_expr
;
11845 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
11846 integral_constant_expression_p
);
11847 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
11848 cur_stmt_expr
= old_stmt_expr
;
11854 t
= tsubst_copy (t
, args
, complain
, in_decl
);
11855 /* As in finish_id_expression, we resolve enumeration constants
11856 to their underlying values. */
11857 if (TREE_CODE (t
) == CONST_DECL
)
11859 used_types_insert (TREE_TYPE (t
));
11860 return DECL_INITIAL (t
);
11865 /* Handle Objective-C++ constructs, if appropriate. */
11868 = objcp_tsubst_copy_and_build (t
, args
, complain
,
11869 in_decl
, /*function_p=*/false);
11873 return tsubst_copy (t
, args
, complain
, in_decl
);
11879 /* Verify that the instantiated ARGS are valid. For type arguments,
11880 make sure that the type's linkage is ok. For non-type arguments,
11881 make sure they are constants if they are integral or enumerations.
11882 Emit an error under control of COMPLAIN, and return TRUE on error. */
11885 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
11887 if (ARGUMENT_PACK_P (t
))
11889 tree vec
= ARGUMENT_PACK_ARGS (t
);
11890 int len
= TREE_VEC_LENGTH (vec
);
11891 bool result
= false;
11894 for (i
= 0; i
< len
; ++i
)
11895 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
11899 else if (TYPE_P (t
))
11901 /* [basic.link]: A name with no linkage (notably, the name
11902 of a class or enumeration declared in a local scope)
11903 shall not be used to declare an entity with linkage.
11904 This implies that names with no linkage cannot be used as
11905 template arguments. */
11906 tree nt
= no_linkage_check (t
, /*relaxed_p=*/false);
11910 /* DR 488 makes use of a type with no linkage cause
11911 type deduction to fail. */
11912 if (complain
& tf_error
)
11914 if (TYPE_ANONYMOUS_P (nt
))
11915 error ("%qT is/uses anonymous type", t
);
11917 error ("template argument for %qD uses local type %qT",
11922 /* In order to avoid all sorts of complications, we do not
11923 allow variably-modified types as template arguments. */
11924 else if (variably_modified_type_p (t
, NULL_TREE
))
11926 if (complain
& tf_error
)
11927 error ("%qT is a variably modified type", t
);
11931 /* A non-type argument of integral or enumerated type must be a
11933 else if (TREE_TYPE (t
)
11934 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
11935 && !TREE_CONSTANT (t
))
11937 if (complain
& tf_error
)
11938 error ("integral expression %qE is not constant", t
);
11945 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
11947 int ix
, len
= DECL_NTPARMS (tmpl
);
11948 bool result
= false;
11950 for (ix
= 0; ix
!= len
; ix
++)
11952 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
11955 if (result
&& (complain
& tf_error
))
11956 error (" trying to instantiate %qD", tmpl
);
11960 /* Instantiate the indicated variable or function template TMPL with
11961 the template arguments in TARG_PTR. */
11964 instantiate_template (tree tmpl
, tree targ_ptr
, tsubst_flags_t complain
)
11969 HOST_WIDE_INT saved_processing_template_decl
;
11971 if (tmpl
== error_mark_node
)
11972 return error_mark_node
;
11974 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
11976 /* If this function is a clone, handle it specially. */
11977 if (DECL_CLONED_FUNCTION_P (tmpl
))
11982 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
,
11984 if (spec
== error_mark_node
)
11985 return error_mark_node
;
11987 /* Look for the clone. */
11988 FOR_EACH_CLONE (clone
, spec
)
11989 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
11991 /* We should always have found the clone by now. */
11992 gcc_unreachable ();
11996 /* Check to see if we already have this specialization. */
11997 spec
= retrieve_specialization (tmpl
, targ_ptr
,
11998 /*class_specializations_p=*/false);
11999 if (spec
!= NULL_TREE
)
12002 gen_tmpl
= most_general_template (tmpl
);
12003 if (tmpl
!= gen_tmpl
)
12005 /* The TMPL is a partial instantiation. To get a full set of
12006 arguments we must add the arguments used to perform the
12007 partial instantiation. */
12008 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
12011 /* Check to see if we already have this specialization. */
12012 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
,
12013 /*class_specializations_p=*/false);
12014 if (spec
!= NULL_TREE
)
12018 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
12020 return error_mark_node
;
12022 /* We are building a FUNCTION_DECL, during which the access of its
12023 parameters and return types have to be checked. However this
12024 FUNCTION_DECL which is the desired context for access checking
12025 is not built yet. We solve this chicken-and-egg problem by
12026 deferring all checks until we have the FUNCTION_DECL. */
12027 push_deferring_access_checks (dk_deferred
);
12029 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12030 (because, for example, we have encountered a non-dependent
12031 function call in the body of a template function and must now
12032 determine which of several overloaded functions will be called),
12033 within the instantiation itself we are not processing a
12035 saved_processing_template_decl
= processing_template_decl
;
12036 processing_template_decl
= 0;
12037 /* Substitute template parameters to obtain the specialization. */
12038 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
12039 targ_ptr
, complain
, gen_tmpl
);
12040 processing_template_decl
= saved_processing_template_decl
;
12041 if (fndecl
== error_mark_node
)
12042 return error_mark_node
;
12044 /* Now we know the specialization, compute access previously
12046 push_access_scope (fndecl
);
12047 perform_deferred_access_checks ();
12048 pop_access_scope (fndecl
);
12049 pop_deferring_access_checks ();
12051 /* The DECL_TI_TEMPLATE should always be the immediate parent
12052 template, not the most general template. */
12053 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
12055 /* If we've just instantiated the main entry point for a function,
12056 instantiate all the alternate entry points as well. We do this
12057 by cloning the instantiation of the main entry point, not by
12058 instantiating the template clones. */
12059 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
12060 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
12065 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
12066 arguments that are being used when calling it. TARGS is a vector
12067 into which the deduced template arguments are placed.
12069 Return zero for success, 2 for an incomplete match that doesn't resolve
12070 all the types, and 1 for complete failure. An error message will be
12071 printed only for an incomplete match.
12073 If FN is a conversion operator, or we are trying to produce a specific
12074 specialization, RETURN_TYPE is the return type desired.
12076 The EXPLICIT_TARGS are explicit template arguments provided via a
12079 The parameter STRICT is one of:
12082 We are deducing arguments for a function call, as in
12083 [temp.deduct.call].
12086 We are deducing arguments for a conversion function, as in
12087 [temp.deduct.conv].
12090 We are deducing arguments when doing an explicit instantiation
12091 as in [temp.explicit], when determining an explicit specialization
12092 as in [temp.expl.spec], or when taking the address of a function
12093 template, as in [temp.deduct.funcaddr]. */
12096 fn_type_unification (tree fn
,
12097 tree explicit_targs
,
12101 unification_kind_t strict
,
12107 bool incomplete_argument_packs_p
= false;
12109 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
12111 fntype
= TREE_TYPE (fn
);
12112 if (explicit_targs
)
12116 The specified template arguments must match the template
12117 parameters in kind (i.e., type, nontype, template), and there
12118 must not be more arguments than there are parameters;
12119 otherwise type deduction fails.
12121 Nontype arguments must match the types of the corresponding
12122 nontype template parameters, or must be convertible to the
12123 types of the corresponding nontype parameters as specified in
12124 _temp.arg.nontype_, otherwise type deduction fails.
12126 All references in the function type of the function template
12127 to the corresponding template parameters are replaced by the
12128 specified template argument values. If a substitution in a
12129 template parameter or in the function type of the function
12130 template results in an invalid type, type deduction fails. */
12131 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
12132 int i
, len
= TREE_VEC_LENGTH (tparms
);
12133 tree converted_args
;
12134 bool incomplete
= false;
12136 if (explicit_targs
== error_mark_node
)
12140 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
12141 /*require_all_args=*/false,
12142 /*use_default_args=*/false));
12143 if (converted_args
== error_mark_node
)
12146 /* Substitute the explicit args into the function type. This is
12147 necessary so that, for instance, explicitly declared function
12148 arguments can match null pointed constants. If we were given
12149 an incomplete set of explicit args, we must not do semantic
12150 processing during substitution as we could create partial
12152 for (i
= 0; i
< len
; i
++)
12154 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
12155 bool parameter_pack
= false;
12157 /* Dig out the actual parm. */
12158 if (TREE_CODE (parm
) == TYPE_DECL
12159 || TREE_CODE (parm
) == TEMPLATE_DECL
)
12161 parm
= TREE_TYPE (parm
);
12162 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
12164 else if (TREE_CODE (parm
) == PARM_DECL
)
12166 parm
= DECL_INITIAL (parm
);
12167 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
12170 if (parameter_pack
)
12174 template_parm_level_and_index (parm
, &level
, &idx
);
12176 /* Mark the argument pack as "incomplete". We could
12177 still deduce more arguments during unification. */
12178 targ
= TMPL_ARG (converted_args
, level
, idx
);
12181 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
12182 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
12183 = ARGUMENT_PACK_ARGS (targ
);
12186 /* We have some incomplete argument packs. */
12187 incomplete_argument_packs_p
= true;
12191 if (incomplete_argument_packs_p
)
12192 /* Any substitution is guaranteed to be incomplete if there
12193 are incomplete argument packs, because we can still deduce
12197 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
12199 processing_template_decl
+= incomplete
;
12200 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
12201 processing_template_decl
-= incomplete
;
12203 if (fntype
== error_mark_node
)
12206 /* Place the explicitly specified arguments in TARGS. */
12207 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
12208 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
12211 /* Never do unification on the 'this' parameter. */
12212 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
12216 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
12217 args
= tree_cons (NULL_TREE
, return_type
, args
);
12220 /* We allow incomplete unification without an error message here
12221 because the standard doesn't seem to explicitly prohibit it. Our
12222 callers must be ready to deal with unification failures in any
12224 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
12225 targs
, parms
, args
, /*subr=*/0,
12228 if (result
== 0 && incomplete_argument_packs_p
)
12230 int i
, len
= NUM_TMPL_ARGS (targs
);
12232 /* Clear the "incomplete" flags on all argument packs. */
12233 for (i
= 0; i
< len
; i
++)
12235 tree arg
= TREE_VEC_ELT (targs
, i
);
12236 if (ARGUMENT_PACK_P (arg
))
12238 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
12239 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
12244 /* Now that we have bindings for all of the template arguments,
12245 ensure that the arguments deduced for the template template
12246 parameters have compatible template parameter lists. We cannot
12247 check this property before we have deduced all template
12248 arguments, because the template parameter types of a template
12249 template parameter might depend on prior template parameters
12250 deduced after the template template parameter. The following
12251 ill-formed example illustrates this issue:
12253 template<typename T, template<T> class C> void f(C<5>, T);
12255 template<int N> struct X {};
12258 f(X<5>(), 5l); // error: template argument deduction fails
12261 The template parameter list of 'C' depends on the template type
12262 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12263 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12264 time that we deduce 'C'. */
12266 && !template_template_parm_bindings_ok_p
12267 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
12271 /* All is well so far. Now, check:
12275 When all template arguments have been deduced, all uses of
12276 template parameters in nondeduced contexts are replaced with
12277 the corresponding deduced argument values. If the
12278 substitution results in an invalid type, as described above,
12279 type deduction fails. */
12280 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
12281 == error_mark_node
)
12287 /* Adjust types before performing type deduction, as described in
12288 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12289 sections are symmetric. PARM is the type of a function parameter
12290 or the return type of the conversion function. ARG is the type of
12291 the argument passed to the call, or the type of the value
12292 initialized with the result of the conversion function.
12293 ARG_EXPR is the original argument expression, which may be null. */
12296 maybe_adjust_types_for_deduction (unification_kind_t strict
,
12310 /* Swap PARM and ARG throughout the remainder of this
12311 function; the handling is precisely symmetric since PARM
12312 will initialize ARG rather than vice versa. */
12320 /* There is nothing to do in this case. */
12324 gcc_unreachable ();
12327 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
12329 /* [temp.deduct.call]
12331 If P is not a reference type:
12333 --If A is an array type, the pointer type produced by the
12334 array-to-pointer standard conversion (_conv.array_) is
12335 used in place of A for type deduction; otherwise,
12337 --If A is a function type, the pointer type produced by
12338 the function-to-pointer standard conversion
12339 (_conv.func_) is used in place of A for type deduction;
12342 --If A is a cv-qualified type, the top level
12343 cv-qualifiers of A's type are ignored for type
12345 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
12346 *arg
= build_pointer_type (TREE_TYPE (*arg
));
12347 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
12348 *arg
= build_pointer_type (*arg
);
12350 *arg
= TYPE_MAIN_VARIANT (*arg
);
12353 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12354 of the form T&&, where T is a template parameter, and the argument
12355 is an lvalue, T is deduced as A& */
12356 if (TREE_CODE (*parm
) == REFERENCE_TYPE
12357 && TYPE_REF_IS_RVALUE (*parm
)
12358 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
12359 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
12360 && arg_expr
&& real_lvalue_p (arg_expr
))
12361 *arg
= build_reference_type (*arg
);
12363 /* [temp.deduct.call]
12365 If P is a cv-qualified type, the top level cv-qualifiers
12366 of P's type are ignored for type deduction. If P is a
12367 reference type, the type referred to by P is used for
12369 *parm
= TYPE_MAIN_VARIANT (*parm
);
12370 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
12372 *parm
= TREE_TYPE (*parm
);
12373 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
12376 /* DR 322. For conversion deduction, remove a reference type on parm
12377 too (which has been swapped into ARG). */
12378 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
12379 *arg
= TREE_TYPE (*arg
);
12384 /* Most parms like fn_type_unification.
12386 If SUBR is 1, we're being called recursively (to unify the
12387 arguments of a function or method parameter of a function
12391 type_unification_real (tree tparms
,
12396 unification_kind_t strict
,
12399 tree parm
, arg
, arg_expr
;
12401 int ntparms
= TREE_VEC_LENGTH (tparms
);
12403 int saw_undeduced
= 0;
12406 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
12407 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
12408 gcc_assert (!xargs
|| TREE_CODE (xargs
) == TREE_LIST
);
12409 gcc_assert (ntparms
> 0);
12414 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
12415 | UNIFY_ALLOW_DERIVED
);
12419 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
12423 sub_strict
= UNIFY_ALLOW_NONE
;
12427 gcc_unreachable ();
12434 while (parms
&& parms
!= void_list_node
12435 && args
&& args
!= void_list_node
)
12437 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
12440 parm
= TREE_VALUE (parms
);
12441 parms
= TREE_CHAIN (parms
);
12442 arg
= TREE_VALUE (args
);
12443 args
= TREE_CHAIN (args
);
12446 if (arg
== error_mark_node
)
12448 if (arg
== unknown_type_node
)
12449 /* We can't deduce anything from this, but we might get all the
12450 template args from other function args. */
12453 /* Conversions will be performed on a function argument that
12454 corresponds with a function parameter that contains only
12455 non-deducible template parameters and explicitly specified
12456 template parameters. */
12457 if (!uses_template_parms (parm
))
12462 type
= TREE_TYPE (arg
);
12466 if (same_type_p (parm
, type
))
12468 if (strict
!= DEDUCE_EXACT
12469 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
12478 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
12479 if (type_unknown_p (arg
))
12481 /* [temp.deduct.type]
12483 A template-argument can be deduced from a pointer to
12484 function or pointer to member function argument if
12485 the set of overloaded functions does not contain
12486 function templates and at most one of a set of
12487 overloaded functions provides a unique match. */
12488 if (resolve_overloaded_unification
12489 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
12495 arg
= unlowered_expr_type (arg
);
12496 if (arg
== error_mark_node
)
12501 int arg_strict
= sub_strict
;
12504 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
12507 if (arg
== init_list_type_node
&& arg_expr
)
12509 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
12516 && parms
!= void_list_node
12517 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
12519 /* Unify the remaining arguments with the pack expansion type. */
12521 tree parmvec
= make_tree_vec (1);
12525 /* Count the number of arguments that remain. */
12526 for (t
= args
; t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
12529 /* Allocate a TREE_VEC and copy in all of the arguments */
12530 argvec
= make_tree_vec (len
);
12531 for (i
= 0; args
&& args
!= void_list_node
; args
= TREE_CHAIN (args
))
12533 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (args
);
12537 /* Copy the parameter into parmvec. */
12538 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
12539 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
12540 /*call_args_p=*/true, /*subr=*/subr
))
12543 /* Advance to the end of the list of parameters. */
12544 parms
= TREE_CHAIN (parms
);
12547 /* Fail if we've reached the end of the parm list, and more args
12548 are present, and the parm list isn't variadic. */
12549 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
12551 /* Fail if parms are left and they don't have default values. */
12552 if (parms
&& parms
!= void_list_node
12553 && TREE_PURPOSE (parms
) == NULL_TREE
)
12557 for (i
= 0; i
< ntparms
; i
++)
12558 if (!TREE_VEC_ELT (targs
, i
))
12562 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
12565 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
12567 /* If this is an undeduced nontype parameter that depends on
12568 a type parameter, try another pass; its type may have been
12569 deduced from a later argument than the one from which
12570 this parameter can be deduced. */
12571 if (TREE_CODE (tparm
) == PARM_DECL
12572 && uses_template_parms (TREE_TYPE (tparm
))
12573 && !saw_undeduced
++)
12576 /* Core issue #226 (C++0x) [temp.deduct]:
12578 If a template argument has not been deduced, its
12579 default template argument, if any, is used.
12581 When we are in C++98 mode, TREE_PURPOSE will either
12582 be NULL_TREE or ERROR_MARK_NODE, so we do not need
12583 to explicitly check cxx_dialect here. */
12584 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
12586 tree arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)),
12587 targs
, tf_none
, NULL_TREE
);
12588 if (arg
== error_mark_node
)
12592 TREE_VEC_ELT (targs
, i
) = arg
;
12597 /* If the type parameter is a parameter pack, then it will
12598 be deduced to an empty parameter pack. */
12599 if (template_parameter_pack_p (tparm
))
12603 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
12605 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
12606 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
12607 TREE_CONSTANT (arg
) = 1;
12610 arg
= make_node (TYPE_ARGUMENT_PACK
);
12612 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
12614 TREE_VEC_ELT (targs
, i
) = arg
;
12624 /* Subroutine of type_unification_real. Args are like the variables
12625 at the call site. ARG is an overloaded function (or template-id);
12626 we try deducing template args from each of the overloads, and if
12627 only one succeeds, we go with that. Modifies TARGS and returns
12628 true on success. */
12631 resolve_overloaded_unification (tree tparms
,
12635 unification_kind_t strict
,
12638 tree tempargs
= copy_node (targs
);
12642 if (TREE_CODE (arg
) == ADDR_EXPR
)
12644 arg
= TREE_OPERAND (arg
, 0);
12650 if (TREE_CODE (arg
) == COMPONENT_REF
)
12651 /* Handle `&x' where `x' is some static or non-static member
12653 arg
= TREE_OPERAND (arg
, 1);
12655 if (TREE_CODE (arg
) == OFFSET_REF
)
12656 arg
= TREE_OPERAND (arg
, 1);
12658 /* Strip baselink information. */
12659 if (BASELINK_P (arg
))
12660 arg
= BASELINK_FUNCTIONS (arg
);
12662 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
12664 /* If we got some explicit template args, we need to plug them into
12665 the affected templates before we try to unify, in case the
12666 explicit args will completely resolve the templates in question. */
12668 tree expl_subargs
= TREE_OPERAND (arg
, 1);
12669 arg
= TREE_OPERAND (arg
, 0);
12671 for (; arg
; arg
= OVL_NEXT (arg
))
12673 tree fn
= OVL_CURRENT (arg
);
12674 tree subargs
, elem
;
12676 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
12679 ++processing_template_decl
;
12680 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
12681 expl_subargs
, /*check_ret=*/false);
12684 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
12685 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
12686 elem
, strict
, sub_strict
, addr_p
);
12688 --processing_template_decl
;
12691 else if (TREE_CODE (arg
) != OVERLOAD
12692 && TREE_CODE (arg
) != FUNCTION_DECL
)
12693 /* If ARG is, for example, "(0, &f)" then its type will be unknown
12694 -- but the deduction does not succeed because the expression is
12695 not just the function on its own. */
12698 for (; arg
; arg
= OVL_NEXT (arg
))
12699 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
12700 TREE_TYPE (OVL_CURRENT (arg
)),
12701 strict
, sub_strict
, addr_p
);
12703 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12704 to function or pointer to member function argument if the set of
12705 overloaded functions does not contain function templates and at most
12706 one of a set of overloaded functions provides a unique match.
12708 So if we found multiple possibilities, we return success but don't
12709 deduce anything. */
12713 int i
= TREE_VEC_LENGTH (targs
);
12715 if (TREE_VEC_ELT (tempargs
, i
))
12716 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
12724 /* Subroutine of resolve_overloaded_unification; does deduction for a single
12725 overload. Fills TARGS with any deduced arguments, or error_mark_node if
12726 different overloads deduce different arguments for a given parm.
12727 ADDR_P is true if the expression for which deduction is being
12728 performed was of the form "& fn" rather than simply "fn".
12730 Returns 1 on success. */
12733 try_one_overload (tree tparms
,
12738 unification_kind_t strict
,
12746 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12747 to function or pointer to member function argument if the set of
12748 overloaded functions does not contain function templates and at most
12749 one of a set of overloaded functions provides a unique match.
12751 So if this is a template, just return success. */
12753 if (uses_template_parms (arg
))
12756 if (TREE_CODE (arg
) == METHOD_TYPE
)
12757 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
12759 arg
= build_pointer_type (arg
);
12761 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
12763 /* We don't copy orig_targs for this because if we have already deduced
12764 some template args from previous args, unify would complain when we
12765 try to deduce a template parameter for the same argument, even though
12766 there isn't really a conflict. */
12767 nargs
= TREE_VEC_LENGTH (targs
);
12768 tempargs
= make_tree_vec (nargs
);
12770 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
12773 /* First make sure we didn't deduce anything that conflicts with
12774 explicitly specified args. */
12775 for (i
= nargs
; i
--; )
12777 tree elt
= TREE_VEC_ELT (tempargs
, i
);
12778 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
12782 else if (uses_template_parms (elt
))
12783 /* Since we're unifying against ourselves, we will fill in
12784 template args used in the function parm list with our own
12785 template parms. Discard them. */
12786 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
12787 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
12791 for (i
= nargs
; i
--; )
12793 tree elt
= TREE_VEC_ELT (tempargs
, i
);
12796 TREE_VEC_ELT (targs
, i
) = elt
;
12802 /* PARM is a template class (perhaps with unbound template
12803 parameters). ARG is a fully instantiated type. If ARG can be
12804 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
12805 TARGS are as for unify. */
12808 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
12810 tree copy_of_targs
;
12812 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
12813 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
12814 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
12817 /* We need to make a new template argument vector for the call to
12818 unify. If we used TARGS, we'd clutter it up with the result of
12819 the attempted unification, even if this class didn't work out.
12820 We also don't want to commit ourselves to all the unifications
12821 we've already done, since unification is supposed to be done on
12822 an argument-by-argument basis. In other words, consider the
12823 following pathological case:
12825 template <int I, int J, int K>
12828 template <int I, int J>
12829 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12831 template <int I, int J, int K>
12832 void f(S<I, J, K>, S<I, I, I>);
12841 Now, by the time we consider the unification involving `s2', we
12842 already know that we must have `f<0, 0, 0>'. But, even though
12843 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12844 because there are two ways to unify base classes of S<0, 1, 2>
12845 with S<I, I, I>. If we kept the already deduced knowledge, we
12846 would reject the possibility I=1. */
12847 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
12849 /* If unification failed, we're done. */
12850 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
12851 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
12857 /* Given a template type PARM and a class type ARG, find the unique
12858 base type in ARG that is an instance of PARM. We do not examine
12859 ARG itself; only its base-classes. If there is not exactly one
12860 appropriate base class, return NULL_TREE. PARM may be the type of
12861 a partial specialization, as well as a plain template type. Used
12865 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
12867 tree rval
= NULL_TREE
;
12870 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
12872 binfo
= TYPE_BINFO (complete_type (arg
));
12874 /* The type could not be completed. */
12877 /* Walk in inheritance graph order. The search order is not
12878 important, and this avoids multiple walks of virtual bases. */
12879 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
12881 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
12885 /* If there is more than one satisfactory baseclass, then:
12889 If they yield more than one possible deduced A, the type
12893 if (rval
&& !same_type_p (r
, rval
))
12903 /* Returns the level of DECL, which declares a template parameter. */
12906 template_decl_level (tree decl
)
12908 switch (TREE_CODE (decl
))
12911 case TEMPLATE_DECL
:
12912 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
12915 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
12918 gcc_unreachable ();
12923 /* Decide whether ARG can be unified with PARM, considering only the
12924 cv-qualifiers of each type, given STRICT as documented for unify.
12925 Returns nonzero iff the unification is OK on that basis. */
12928 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
12930 int arg_quals
= cp_type_quals (arg
);
12931 int parm_quals
= cp_type_quals (parm
);
12933 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12934 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12936 /* Although a CVR qualifier is ignored when being applied to a
12937 substituted template parameter ([8.3.2]/1 for example), that
12938 does not apply during deduction [14.8.2.4]/1, (even though
12939 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12940 this). Except when we're allowing additional CV qualifiers
12941 at the outer level [14.8.2.1]/3,1st bullet. */
12942 if ((TREE_CODE (arg
) == REFERENCE_TYPE
12943 || TREE_CODE (arg
) == FUNCTION_TYPE
12944 || TREE_CODE (arg
) == METHOD_TYPE
)
12945 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
12948 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
12949 && (parm_quals
& TYPE_QUAL_RESTRICT
))
12953 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12954 && (arg_quals
& parm_quals
) != parm_quals
)
12957 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
12958 && (parm_quals
& arg_quals
) != arg_quals
)
12964 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12966 template_parm_level_and_index (tree parm
, int* level
, int* index
)
12968 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12969 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12970 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12972 *index
= TEMPLATE_TYPE_IDX (parm
);
12973 *level
= TEMPLATE_TYPE_LEVEL (parm
);
12977 *index
= TEMPLATE_PARM_IDX (parm
);
12978 *level
= TEMPLATE_PARM_LEVEL (parm
);
12982 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12983 expansion at the end of PACKED_PARMS. Returns 0 if the type
12984 deduction succeeds, 1 otherwise. STRICT is the same as in
12985 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12986 call argument list. We'll need to adjust the arguments to make them
12987 types. SUBR tells us if this is from a recursive call to
12988 type_unification_real. */
12990 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
12991 tree packed_args
, int strict
, bool call_args_p
,
12995 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
12996 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
12997 tree pack
, packs
= NULL_TREE
;
12998 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
12999 int len
= TREE_VEC_LENGTH (packed_args
);
13001 /* Determine the parameter packs we will be deducing from the
13002 pattern, and record their current deductions. */
13003 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
13004 pack
; pack
= TREE_CHAIN (pack
))
13006 tree parm_pack
= TREE_VALUE (pack
);
13009 /* Determine the index and level of this parameter pack. */
13010 template_parm_level_and_index (parm_pack
, &level
, &idx
);
13012 /* Keep track of the parameter packs and their corresponding
13014 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
13015 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
13018 /* Loop through all of the arguments that have not yet been
13019 unified and unify each with the pattern. */
13020 for (i
= start
; i
< len
; i
++)
13022 tree parm
= pattern
;
13024 /* For each parameter pack, clear out the deduced value so that
13025 we can deduce it again. */
13026 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13029 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
13031 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
13034 /* Unify the pattern with the current argument. */
13036 tree arg
= TREE_VEC_ELT (packed_args
, i
);
13037 tree arg_expr
= NULL_TREE
;
13038 int arg_strict
= strict
;
13039 bool skip_arg_p
= false;
13045 /* This mirrors what we do in type_unification_real. */
13049 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
13050 | UNIFY_ALLOW_MORE_CV_QUAL
13051 | UNIFY_ALLOW_DERIVED
);
13055 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
13059 sub_strict
= UNIFY_ALLOW_NONE
;
13063 gcc_unreachable ();
13068 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
13069 if (type_unknown_p (arg
))
13071 /* [temp.deduct.type] A template-argument can be
13072 deduced from a pointer to function or pointer
13073 to member function argument if the set of
13074 overloaded functions does not contain function
13075 templates and at most one of a set of
13076 overloaded functions provides a unique
13079 if (resolve_overloaded_unification
13080 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
13089 arg
= unlowered_expr_type (arg
);
13090 if (arg
== error_mark_node
)
13095 arg_strict
= sub_strict
;
13099 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
13105 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
13110 /* For each parameter pack, collect the deduced value. */
13111 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13114 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
13116 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
13117 TMPL_ARG (targs
, level
, idx
);
13121 /* Verify that the results of unification with the parameter packs
13122 produce results consistent with what we've seen before, and make
13123 the deduced argument packs available. */
13124 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
13126 tree old_pack
= TREE_VALUE (pack
);
13127 tree new_args
= TREE_TYPE (pack
);
13128 int i
, len
= TREE_VEC_LENGTH (new_args
);
13129 bool nondeduced_p
= false;
13131 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13132 actually deduce anything. */
13133 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
13134 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
13135 nondeduced_p
= true;
13139 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
13141 /* Prepend the explicit arguments onto NEW_ARGS. */
13142 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
13143 tree old_args
= new_args
;
13144 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
13145 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
13147 /* Copy the explicit arguments. */
13148 new_args
= make_tree_vec (len
);
13149 for (i
= 0; i
< explicit_len
; i
++)
13150 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
13152 /* Copy the deduced arguments. */
13153 for (; i
< len
; i
++)
13154 TREE_VEC_ELT (new_args
, i
) =
13155 TREE_VEC_ELT (old_args
, i
- explicit_len
);
13163 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
13165 /* Build the deduced *_ARGUMENT_PACK. */
13166 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
13168 result
= make_node (NONTYPE_ARGUMENT_PACK
);
13169 TREE_TYPE (result
) =
13170 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
13171 TREE_CONSTANT (result
) = 1;
13174 result
= make_node (TYPE_ARGUMENT_PACK
);
13176 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
13178 /* Note the deduced argument packs for this parameter
13180 TMPL_ARG (targs
, level
, idx
) = result
;
13182 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
13183 && (ARGUMENT_PACK_ARGS (old_pack
)
13184 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
13186 /* We only had the explicitly-provided arguments before, but
13187 now we have a complete set of arguments. */
13189 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
13190 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
13192 /* Keep the original deduced argument pack. */
13193 TMPL_ARG (targs
, level
, idx
) = old_pack
;
13195 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
13196 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
13197 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
13199 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
13201 /* Inconsistent unification of this parameter pack. */
13207 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
13209 /* Keep the original deduced argument pack. */
13210 TMPL_ARG (targs
, level
, idx
) = old_pack
;
13217 /* Deduce the value of template parameters. TPARMS is the (innermost)
13218 set of template parameters to a template. TARGS is the bindings
13219 for those template parameters, as determined thus far; TARGS may
13220 include template arguments for outer levels of template parameters
13221 as well. PARM is a parameter to a template function, or a
13222 subcomponent of that parameter; ARG is the corresponding argument.
13223 This function attempts to match PARM with ARG in a manner
13224 consistent with the existing assignments in TARGS. If more values
13225 are deduced, then TARGS is updated.
13227 Returns 0 if the type deduction succeeds, 1 otherwise. The
13228 parameter STRICT is a bitwise or of the following flags:
13231 Require an exact match between PARM and ARG.
13232 UNIFY_ALLOW_MORE_CV_QUAL:
13233 Allow the deduced ARG to be more cv-qualified (by qualification
13234 conversion) than ARG.
13235 UNIFY_ALLOW_LESS_CV_QUAL:
13236 Allow the deduced ARG to be less cv-qualified than ARG.
13237 UNIFY_ALLOW_DERIVED:
13238 Allow the deduced ARG to be a template base class of ARG,
13239 or a pointer to a template base class of the type pointed to by
13241 UNIFY_ALLOW_INTEGER:
13242 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13243 case for more information.
13244 UNIFY_ALLOW_OUTER_LEVEL:
13245 This is the outermost level of a deduction. Used to determine validity
13246 of qualification conversions. A valid qualification conversion must
13247 have const qualified pointers leading up to the inner type which
13248 requires additional CV quals, except at the outer level, where const
13249 is not required [conv.qual]. It would be normal to set this flag in
13250 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13251 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13252 This is the outermost level of a deduction, and PARM can be more CV
13253 qualified at this point.
13254 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13255 This is the outermost level of a deduction, and PARM can be less CV
13256 qualified at this point. */
13259 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
13264 int strict_in
= strict
;
13266 /* I don't think this will do the right thing with respect to types.
13267 But the only case I've seen it in so far has been array bounds, where
13268 signedness is the only information lost, and I think that will be
13270 while (TREE_CODE (parm
) == NOP_EXPR
)
13271 parm
= TREE_OPERAND (parm
, 0);
13273 if (arg
== error_mark_node
)
13275 if (arg
== unknown_type_node
13276 || arg
== init_list_type_node
)
13277 /* We can't deduce anything from this, but we might get all the
13278 template args from other function args. */
13281 /* If PARM uses template parameters, then we can't bail out here,
13282 even if ARG == PARM, since we won't record unifications for the
13283 template parameters. We might need them if we're trying to
13284 figure out which of two things is more specialized. */
13285 if (arg
== parm
&& !uses_template_parms (parm
))
13288 /* Handle init lists early, so the rest of the function can assume
13289 we're dealing with a type. */
13290 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
13295 if (!is_std_init_list (parm
))
13296 /* We can only deduce from an initializer list argument if the
13297 parameter is std::initializer_list; otherwise this is a
13298 non-deduced context. */
13301 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
13303 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
13305 int elt_strict
= strict
;
13306 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
13308 tree type
= TREE_TYPE (elt
);
13309 /* It should only be possible to get here for a call. */
13310 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
13311 elt_strict
|= maybe_adjust_types_for_deduction
13312 (DEDUCE_CALL
, &elttype
, &type
, elt
);
13316 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
13322 /* Immediately reject some pairs that won't unify because of
13323 cv-qualification mismatches. */
13324 if (TREE_CODE (arg
) == TREE_CODE (parm
)
13326 /* It is the elements of the array which hold the cv quals of an array
13327 type, and the elements might be template type parms. We'll check
13328 when we recurse. */
13329 && TREE_CODE (arg
) != ARRAY_TYPE
13330 /* We check the cv-qualifiers when unifying with template type
13331 parameters below. We want to allow ARG `const T' to unify with
13332 PARM `T' for example, when computing which of two templates
13333 is more specialized, for example. */
13334 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
13335 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
13338 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
13339 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
13340 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
13341 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
13342 strict
&= ~UNIFY_ALLOW_DERIVED
;
13343 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
13344 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
13346 switch (TREE_CODE (parm
))
13348 case TYPENAME_TYPE
:
13350 case UNBOUND_CLASS_TEMPLATE
:
13351 /* In a type which contains a nested-name-specifier, template
13352 argument values cannot be deduced for template parameters used
13353 within the nested-name-specifier. */
13356 case TEMPLATE_TYPE_PARM
:
13357 case TEMPLATE_TEMPLATE_PARM
:
13358 case BOUND_TEMPLATE_TEMPLATE_PARM
:
13359 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
13360 if (tparm
== error_mark_node
)
13363 if (TEMPLATE_TYPE_LEVEL (parm
)
13364 != template_decl_level (tparm
))
13365 /* The PARM is not one we're trying to unify. Just check
13366 to see if it matches ARG. */
13367 return (TREE_CODE (arg
) == TREE_CODE (parm
)
13368 && same_type_p (parm
, arg
)) ? 0 : 1;
13369 idx
= TEMPLATE_TYPE_IDX (parm
);
13370 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
13371 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
13373 /* Check for mixed types and values. */
13374 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
13375 && TREE_CODE (tparm
) != TYPE_DECL
)
13376 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
13377 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
13380 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
13382 /* ARG must be constructed from a template class or a template
13383 template parameter. */
13384 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
13385 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
13389 tree parmvec
= TYPE_TI_ARGS (parm
);
13390 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
13392 = DECL_INNERMOST_TEMPLATE_PARMS
13393 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
13395 int parm_variadic_p
= 0;
13397 /* The resolution to DR150 makes clear that default
13398 arguments for an N-argument may not be used to bind T
13399 to a template template parameter with fewer than N
13400 parameters. It is not safe to permit the binding of
13401 default arguments as an extension, as that may change
13402 the meaning of a conforming program. Consider:
13404 struct Dense { static const unsigned int dim = 1; };
13406 template <template <typename> class View,
13408 void operator+(float, View<Block> const&);
13410 template <typename Block,
13411 unsigned int Dim = Block::dim>
13412 struct Lvalue_proxy { operator float() const; };
13416 Lvalue_proxy<Dense> p;
13421 Here, if Lvalue_proxy is permitted to bind to View, then
13422 the global operator+ will be used; if they are not, the
13423 Lvalue_proxy will be converted to float. */
13424 if (coerce_template_parms (parm_parms
,
13426 TYPE_TI_TEMPLATE (parm
),
13428 /*require_all_args=*/true,
13429 /*use_default_args=*/false)
13430 == error_mark_node
)
13433 /* Deduce arguments T, i from TT<T> or TT<i>.
13434 We check each element of PARMVEC and ARGVEC individually
13435 rather than the whole TREE_VEC since they can have
13436 different number of elements. */
13438 parmvec
= expand_template_argument_pack (parmvec
);
13439 argvec
= expand_template_argument_pack (argvec
);
13441 len
= TREE_VEC_LENGTH (parmvec
);
13443 /* Check if the parameters end in a pack, making them
13446 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
13447 parm_variadic_p
= 1;
13449 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
13452 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
13454 if (unify (tparms
, targs
,
13455 TREE_VEC_ELT (parmvec
, i
),
13456 TREE_VEC_ELT (argvec
, i
),
13461 if (parm_variadic_p
13462 && unify_pack_expansion (tparms
, targs
,
13465 /*call_args_p=*/false,
13469 arg
= TYPE_TI_TEMPLATE (arg
);
13471 /* Fall through to deduce template name. */
13474 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
13475 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
13477 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
13479 /* Simple cases: Value already set, does match or doesn't. */
13480 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
13487 /* If PARM is `const T' and ARG is only `int', we don't have
13488 a match unless we are allowing additional qualification.
13489 If ARG is `const int' and PARM is just `T' that's OK;
13490 that binds `const int' to `T'. */
13491 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
13495 /* Consider the case where ARG is `const volatile int' and
13496 PARM is `const T'. Then, T should be `volatile int'. */
13497 arg
= cp_build_qualified_type_real
13498 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
13499 if (arg
== error_mark_node
)
13502 /* Simple cases: Value already set, does match or doesn't. */
13503 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
13508 /* Make sure that ARG is not a variable-sized array. (Note
13509 that were talking about variable-sized arrays (like
13510 `int[n]'), rather than arrays of unknown size (like
13511 `int[]').) We'll get very confused by such a type since
13512 the bound of the array will not be computable in an
13513 instantiation. Besides, such types are not allowed in
13514 ISO C++, so we can do as we please here. */
13515 if (variably_modified_type_p (arg
, NULL_TREE
))
13519 /* If ARG is a parameter pack or an expansion, we cannot unify
13520 against it unless PARM is also a parameter pack. */
13521 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
13522 && !template_parameter_pack_p (parm
))
13525 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
13528 case TEMPLATE_PARM_INDEX
:
13529 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
13530 if (tparm
== error_mark_node
)
13533 if (TEMPLATE_PARM_LEVEL (parm
)
13534 != template_decl_level (tparm
))
13535 /* The PARM is not one we're trying to unify. Just check
13536 to see if it matches ARG. */
13537 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
13538 && cp_tree_equal (parm
, arg
));
13540 idx
= TEMPLATE_PARM_IDX (parm
);
13541 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
13544 return !cp_tree_equal (targ
, arg
);
13546 /* [temp.deduct.type] If, in the declaration of a function template
13547 with a non-type template-parameter, the non-type
13548 template-parameter is used in an expression in the function
13549 parameter-list and, if the corresponding template-argument is
13550 deduced, the template-argument type shall match the type of the
13551 template-parameter exactly, except that a template-argument
13552 deduced from an array bound may be of any integral type.
13553 The non-type parameter might use already deduced type parameters. */
13554 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
13555 if (!TREE_TYPE (arg
))
13556 /* Template-parameter dependent expression. Just accept it for now.
13557 It will later be processed in convert_template_argument. */
13559 else if (same_type_p (TREE_TYPE (arg
), tparm
))
13561 else if ((strict
& UNIFY_ALLOW_INTEGER
)
13562 && (TREE_CODE (tparm
) == INTEGER_TYPE
13563 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
13564 /* Convert the ARG to the type of PARM; the deduced non-type
13565 template argument must exactly match the types of the
13566 corresponding parameter. */
13567 arg
= fold (build_nop (tparm
, arg
));
13568 else if (uses_template_parms (tparm
))
13569 /* We haven't deduced the type of this parameter yet. Try again
13575 /* If ARG is a parameter pack or an expansion, we cannot unify
13576 against it unless PARM is also a parameter pack. */
13577 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
13578 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
13581 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
13586 /* A pointer-to-member constant can be unified only with
13587 another constant. */
13588 if (TREE_CODE (arg
) != PTRMEM_CST
)
13591 /* Just unify the class member. It would be useless (and possibly
13592 wrong, depending on the strict flags) to unify also
13593 PTRMEM_CST_CLASS, because we want to be sure that both parm and
13594 arg refer to the same variable, even if through different
13595 classes. For instance:
13597 struct A { int x; };
13600 Unification of &A::x and &B::x must succeed. */
13601 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
13602 PTRMEM_CST_MEMBER (arg
), strict
);
13607 if (TREE_CODE (arg
) != POINTER_TYPE
)
13610 /* [temp.deduct.call]
13612 A can be another pointer or pointer to member type that can
13613 be converted to the deduced A via a qualification
13614 conversion (_conv.qual_).
13616 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
13617 This will allow for additional cv-qualification of the
13618 pointed-to types if appropriate. */
13620 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
13621 /* The derived-to-base conversion only persists through one
13622 level of pointers. */
13623 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
13625 return unify (tparms
, targs
, TREE_TYPE (parm
),
13626 TREE_TYPE (arg
), strict
);
13629 case REFERENCE_TYPE
:
13630 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
13632 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13633 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
13636 if (TREE_CODE (arg
) != ARRAY_TYPE
)
13638 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
13639 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
13641 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
13648 /* Our representation of array types uses "N - 1" as the
13649 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
13650 not an integer constant. We cannot unify arbitrarily
13651 complex expressions, so we eliminate the MINUS_EXPRs
13653 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
13654 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
13657 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
13658 parm_max
= TREE_OPERAND (parm_max
, 0);
13660 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
13661 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
13664 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
13665 trying to unify the type of a variable with the type
13666 of a template parameter. For example:
13668 template <unsigned int N>
13669 void f (char (&) [N]);
13676 Here, the type of the ARG will be "int [g(i)]", and
13677 may be a SAVE_EXPR, etc. */
13678 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
13680 arg_max
= TREE_OPERAND (arg_max
, 0);
13683 /* If only one of the bounds used a MINUS_EXPR, compensate
13684 by adding one to the other bound. */
13685 if (parm_cst
&& !arg_cst
)
13686 parm_max
= fold_build2 (PLUS_EXPR
,
13690 else if (arg_cst
&& !parm_cst
)
13691 arg_max
= fold_build2 (PLUS_EXPR
,
13696 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
13699 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13700 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
13707 case ENUMERAL_TYPE
:
13709 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13712 /* We have already checked cv-qualification at the top of the
13714 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
13717 /* As far as unification is concerned, this wins. Later checks
13718 will invalidate it if necessary. */
13721 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
13722 /* Type INTEGER_CST can come from ordinary constant template args. */
13724 while (TREE_CODE (arg
) == NOP_EXPR
)
13725 arg
= TREE_OPERAND (arg
, 0);
13727 if (TREE_CODE (arg
) != INTEGER_CST
)
13729 return !tree_int_cst_equal (parm
, arg
);
13734 if (TREE_CODE (arg
) != TREE_VEC
)
13736 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
13738 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
13739 if (unify (tparms
, targs
,
13740 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
13748 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13751 if (TYPE_PTRMEMFUNC_P (parm
))
13753 if (!TYPE_PTRMEMFUNC_P (arg
))
13756 return unify (tparms
, targs
,
13757 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
13758 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
13762 if (CLASSTYPE_TEMPLATE_INFO (parm
))
13764 tree t
= NULL_TREE
;
13766 if (strict_in
& UNIFY_ALLOW_DERIVED
)
13768 /* First, we try to unify the PARM and ARG directly. */
13769 t
= try_class_unification (tparms
, targs
,
13774 /* Fallback to the special case allowed in
13775 [temp.deduct.call]:
13777 If P is a class, and P has the form
13778 template-id, then A can be a derived class of
13779 the deduced A. Likewise, if P is a pointer to
13780 a class of the form template-id, A can be a
13781 pointer to a derived class pointed to by the
13783 t
= get_template_base (tparms
, targs
, parm
, arg
);
13789 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
13790 && (CLASSTYPE_TI_TEMPLATE (parm
)
13791 == CLASSTYPE_TI_TEMPLATE (arg
)))
13792 /* Perhaps PARM is something like S<U> and ARG is S<int>.
13793 Then, we should unify `int' and `U'. */
13796 /* There's no chance of unification succeeding. */
13799 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
13800 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
13802 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
13807 case FUNCTION_TYPE
:
13808 if (TREE_CODE (arg
) != TREE_CODE (parm
))
13811 /* CV qualifications for methods can never be deduced, they must
13812 match exactly. We need to check them explicitly here,
13813 because type_unification_real treats them as any other
13814 cv-qualified parameter. */
13815 if (TREE_CODE (parm
) == METHOD_TYPE
13816 && (!check_cv_quals_for_unify
13818 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
13819 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
13822 if (unify (tparms
, targs
, TREE_TYPE (parm
),
13823 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
13825 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
13826 TYPE_ARG_TYPES (arg
), 1, DEDUCE_EXACT
,
13830 /* Unify a pointer to member with a pointer to member function, which
13831 deduces the type of the member as a function type. */
13832 if (TYPE_PTRMEMFUNC_P (arg
))
13836 cp_cv_quals cv_quals
;
13838 /* Check top-level cv qualifiers */
13839 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
13842 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
13843 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
13846 /* Determine the type of the function we are unifying against. */
13847 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
13849 build_function_type (TREE_TYPE (method_type
),
13850 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
13852 /* Extract the cv-qualifiers of the member function from the
13853 implicit object parameter and place them on the function
13854 type to be restored later. */
13856 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type
))));
13857 fntype
= build_qualified_type (fntype
, cv_quals
);
13858 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
13861 if (TREE_CODE (arg
) != OFFSET_TYPE
)
13863 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
13864 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
13866 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
13870 if (DECL_TEMPLATE_PARM_P (parm
))
13871 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
13872 if (arg
!= integral_constant_value (parm
))
13877 case TEMPLATE_DECL
:
13878 /* Matched cases are handled by the ARG == PARM test above. */
13881 case TYPE_ARGUMENT_PACK
:
13882 case NONTYPE_ARGUMENT_PACK
:
13884 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
13885 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
13886 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
13887 int argslen
= TREE_VEC_LENGTH (packed_args
);
13888 int parm_variadic_p
= 0;
13890 for (i
= 0; i
< len
; ++i
)
13892 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
13895 /* We can unify against something with a trailing
13897 parm_variadic_p
= 1;
13899 /* Since there is something following the pack
13900 expansion, we cannot unify this template argument
13907 /* If we don't have enough arguments to satisfy the parameters
13908 (not counting the pack expression at the end), or we have
13909 too many arguments for a parameter list that doesn't end in
13910 a pack expression, we can't unify. */
13911 if (argslen
< (len
- parm_variadic_p
)
13912 || (argslen
> len
&& !parm_variadic_p
))
13915 /* Unify all of the parameters that precede the (optional)
13916 pack expression. */
13917 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
13919 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
13920 TREE_VEC_ELT (packed_args
, i
), strict
))
13924 if (parm_variadic_p
)
13925 return unify_pack_expansion (tparms
, targs
,
13926 packed_parms
, packed_args
,
13927 strict
, /*call_args_p=*/false,
13935 case DECLTYPE_TYPE
:
13936 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
13941 /* Unification fails if we hit an error node. */
13945 gcc_assert (EXPR_P (parm
));
13947 /* We must be looking at an expression. This can happen with
13951 void foo(S<I>, S<I + 2>);
13953 This is a "nondeduced context":
13957 The nondeduced contexts are:
13959 --A type that is a template-id in which one or more of
13960 the template-arguments is an expression that references
13961 a template-parameter.
13963 In these cases, we assume deduction succeeded, but don't
13964 actually infer any unifications. */
13966 if (!uses_template_parms (parm
)
13967 && !template_args_equal (parm
, arg
))
13974 /* Note that DECL can be defined in this translation unit, if
13978 mark_definable (tree decl
)
13981 DECL_NOT_REALLY_EXTERN (decl
) = 1;
13982 FOR_EACH_CLONE (clone
, decl
)
13983 DECL_NOT_REALLY_EXTERN (clone
) = 1;
13986 /* Called if RESULT is explicitly instantiated, or is a member of an
13987 explicitly instantiated class. */
13990 mark_decl_instantiated (tree result
, int extern_p
)
13992 SET_DECL_EXPLICIT_INSTANTIATION (result
);
13994 /* If this entity has already been written out, it's too late to
13995 make any modifications. */
13996 if (TREE_ASM_WRITTEN (result
))
13999 if (TREE_CODE (result
) != FUNCTION_DECL
)
14000 /* The TREE_PUBLIC flag for function declarations will have been
14001 set correctly by tsubst. */
14002 TREE_PUBLIC (result
) = 1;
14004 /* This might have been set by an earlier implicit instantiation. */
14005 DECL_COMDAT (result
) = 0;
14008 DECL_NOT_REALLY_EXTERN (result
) = 0;
14011 mark_definable (result
);
14012 /* Always make artificials weak. */
14013 if (DECL_ARTIFICIAL (result
) && flag_weak
)
14014 comdat_linkage (result
);
14015 /* For WIN32 we also want to put explicit instantiations in
14016 linkonce sections. */
14017 else if (TREE_PUBLIC (result
))
14018 maybe_make_one_only (result
);
14021 /* If EXTERN_P, then this function will not be emitted -- unless
14022 followed by an explicit instantiation, at which point its linkage
14023 will be adjusted. If !EXTERN_P, then this function will be
14024 emitted here. In neither circumstance do we want
14025 import_export_decl to adjust the linkage. */
14026 DECL_INTERFACE_KNOWN (result
) = 1;
14029 /* Given two function templates PAT1 and PAT2, return:
14031 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14032 -1 if PAT2 is more specialized than PAT1.
14033 0 if neither is more specialized.
14035 LEN indicates the number of parameters we should consider
14036 (defaulted parameters should not be considered).
14038 The 1998 std underspecified function template partial ordering, and
14039 DR214 addresses the issue. We take pairs of arguments, one from
14040 each of the templates, and deduce them against each other. One of
14041 the templates will be more specialized if all the *other*
14042 template's arguments deduce against its arguments and at least one
14043 of its arguments *does* *not* deduce against the other template's
14044 corresponding argument. Deduction is done as for class templates.
14045 The arguments used in deduction have reference and top level cv
14046 qualifiers removed. Iff both arguments were originally reference
14047 types *and* deduction succeeds in both directions, the template
14048 with the more cv-qualified argument wins for that pairing (if
14049 neither is more cv-qualified, they both are equal). Unlike regular
14050 deduction, after all the arguments have been deduced in this way,
14051 we do *not* verify the deduced template argument values can be
14052 substituted into non-deduced contexts, nor do we have to verify
14053 that all template arguments have been deduced. */
14056 more_specialized_fn (tree pat1
, tree pat2
, int len
)
14058 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
14059 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
14060 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
14061 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
14062 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
14063 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
14064 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
14065 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
14069 /* Remove the this parameter from non-static member functions. If
14070 one is a non-static member function and the other is not a static
14071 member function, remove the first parameter from that function
14072 also. This situation occurs for operator functions where we
14073 locate both a member function (with this pointer) and non-member
14074 operator (with explicit first operand). */
14075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
14077 len
--; /* LEN is the number of significant arguments for DECL1 */
14078 args1
= TREE_CHAIN (args1
);
14079 if (!DECL_STATIC_FUNCTION_P (decl2
))
14080 args2
= TREE_CHAIN (args2
);
14082 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
14084 args2
= TREE_CHAIN (args2
);
14085 if (!DECL_STATIC_FUNCTION_P (decl1
))
14088 args1
= TREE_CHAIN (args1
);
14092 /* If only one is a conversion operator, they are unordered. */
14093 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
14096 /* Consider the return type for a conversion function */
14097 if (DECL_CONV_FN_P (decl1
))
14099 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
14100 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
14104 processing_template_decl
++;
14107 /* Stop when an ellipsis is seen. */
14108 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
14110 tree arg1
= TREE_VALUE (args1
);
14111 tree arg2
= TREE_VALUE (args2
);
14112 int deduce1
, deduce2
;
14116 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
14117 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
14119 /* When both arguments are pack expansions, we need only
14120 unify the patterns themselves. */
14121 arg1
= PACK_EXPANSION_PATTERN (arg1
);
14122 arg2
= PACK_EXPANSION_PATTERN (arg2
);
14124 /* This is the last comparison we need to do. */
14128 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
14130 arg1
= TREE_TYPE (arg1
);
14131 quals1
= cp_type_quals (arg1
);
14134 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
14136 arg2
= TREE_TYPE (arg2
);
14137 quals2
= cp_type_quals (arg2
);
14140 if ((quals1
< 0) != (quals2
< 0))
14142 /* Only of the args is a reference, see if we should apply
14143 array/function pointer decay to it. This is not part of
14144 DR214, but is, IMHO, consistent with the deduction rules
14145 for the function call itself, and with our earlier
14146 implementation of the underspecified partial ordering
14147 rules. (nathan). */
14150 switch (TREE_CODE (arg1
))
14153 arg1
= TREE_TYPE (arg1
);
14155 case FUNCTION_TYPE
:
14156 arg1
= build_pointer_type (arg1
);
14165 switch (TREE_CODE (arg2
))
14168 arg2
= TREE_TYPE (arg2
);
14170 case FUNCTION_TYPE
:
14171 arg2
= build_pointer_type (arg2
);
14180 arg1
= TYPE_MAIN_VARIANT (arg1
);
14181 arg2
= TYPE_MAIN_VARIANT (arg2
);
14183 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
14185 int i
, len2
= list_length (args2
);
14186 tree parmvec
= make_tree_vec (1);
14187 tree argvec
= make_tree_vec (len2
);
14190 /* Setup the parameter vector, which contains only ARG1. */
14191 TREE_VEC_ELT (parmvec
, 0) = arg1
;
14193 /* Setup the argument vector, which contains the remaining
14195 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
14196 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
14198 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
14199 argvec
, UNIFY_ALLOW_NONE
,
14200 /*call_args_p=*/false,
14203 /* We cannot deduce in the other direction, because ARG1 is
14204 a pack expansion but ARG2 is not. */
14207 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
14209 int i
, len1
= list_length (args1
);
14210 tree parmvec
= make_tree_vec (1);
14211 tree argvec
= make_tree_vec (len1
);
14214 /* Setup the parameter vector, which contains only ARG1. */
14215 TREE_VEC_ELT (parmvec
, 0) = arg2
;
14217 /* Setup the argument vector, which contains the remaining
14219 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
14220 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
14222 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
14223 argvec
, UNIFY_ALLOW_NONE
,
14224 /*call_args_p=*/false,
14227 /* We cannot deduce in the other direction, because ARG2 is
14228 a pack expansion but ARG1 is not.*/
14234 /* The normal case, where neither argument is a pack
14236 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
14237 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
14244 if (better1
< 0 && better2
< 0)
14245 /* We've failed to deduce something in either direction.
14246 These must be unordered. */
14249 if (deduce1
&& deduce2
&& quals1
>= 0 && quals2
>= 0)
14251 /* Deduces in both directions, see if quals can
14252 disambiguate. Pretend the worse one failed to deduce. */
14253 if ((quals1
& quals2
) == quals2
)
14255 if ((quals1
& quals2
) == quals1
)
14258 if (deduce1
&& !deduce2
&& !better2
)
14260 if (deduce2
&& !deduce1
&& !better1
)
14263 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
14264 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
14265 /* We have already processed all of the arguments in our
14266 handing of the pack expansion type. */
14269 args1
= TREE_CHAIN (args1
);
14270 args2
= TREE_CHAIN (args2
);
14273 processing_template_decl
--;
14275 /* All things being equal, if the next argument is a pack expansion
14276 for one function but not for the other, prefer the
14277 non-variadic function. */
14278 if ((better1
> 0) - (better2
> 0) == 0
14279 && args1
&& TREE_VALUE (args1
)
14280 && args2
&& TREE_VALUE (args2
))
14282 if (TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
)
14283 return TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
? 0 : -1;
14284 else if (TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
)
14288 return (better1
> 0) - (better2
> 0);
14291 /* Determine which of two partial specializations is more specialized.
14293 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14294 to the first partial specialization. The TREE_VALUE is the
14295 innermost set of template parameters for the partial
14296 specialization. PAT2 is similar, but for the second template.
14298 Return 1 if the first partial specialization is more specialized;
14299 -1 if the second is more specialized; 0 if neither is more
14302 See [temp.class.order] for information about determining which of
14303 two templates is more specialized. */
14306 more_specialized_class (tree pat1
, tree pat2
)
14311 bool any_deductions
= false;
14313 tmpl1
= TREE_TYPE (pat1
);
14314 tmpl2
= TREE_TYPE (pat2
);
14316 /* Just like what happens for functions, if we are ordering between
14317 different class template specializations, we may encounter dependent
14318 types in the arguments, and we need our dependency check functions
14319 to behave correctly. */
14320 ++processing_template_decl
;
14321 targs
= get_class_bindings (TREE_VALUE (pat1
),
14322 CLASSTYPE_TI_ARGS (tmpl1
),
14323 CLASSTYPE_TI_ARGS (tmpl2
));
14327 any_deductions
= true;
14330 targs
= get_class_bindings (TREE_VALUE (pat2
),
14331 CLASSTYPE_TI_ARGS (tmpl2
),
14332 CLASSTYPE_TI_ARGS (tmpl1
));
14336 any_deductions
= true;
14338 --processing_template_decl
;
14340 /* In the case of a tie where at least one of the class templates
14341 has a parameter pack at the end, the template with the most
14342 non-packed parameters wins. */
14345 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
14346 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
14348 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
14349 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
14350 int len1
= TREE_VEC_LENGTH (args1
);
14351 int len2
= TREE_VEC_LENGTH (args2
);
14353 /* We don't count the pack expansion at the end. */
14354 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
14356 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
14361 else if (len1
< len2
)
14368 /* Return the template arguments that will produce the function signature
14369 DECL from the function template FN, with the explicit template
14370 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
14371 also match. Return NULL_TREE if no satisfactory arguments could be
14375 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
14377 int ntparms
= DECL_NTPARMS (fn
);
14378 tree targs
= make_tree_vec (ntparms
);
14380 tree decl_arg_types
;
14382 /* Substitute the explicit template arguments into the type of DECL.
14383 The call to fn_type_unification will handle substitution into the
14385 decl_type
= TREE_TYPE (decl
);
14386 if (explicit_args
&& uses_template_parms (decl_type
))
14389 tree converted_args
;
14391 if (DECL_TEMPLATE_INFO (decl
))
14392 tmpl
= DECL_TI_TEMPLATE (decl
);
14394 /* We can get here for some invalid specializations. */
14398 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
14399 explicit_args
, NULL_TREE
,
14401 /*require_all_args=*/false,
14402 /*use_default_args=*/false);
14403 if (converted_args
== error_mark_node
)
14406 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
14407 if (decl_type
== error_mark_node
)
14411 /* Never do unification on the 'this' parameter. */
14412 decl_arg_types
= skip_artificial_parms_for (decl
,
14413 TYPE_ARG_TYPES (decl_type
));
14415 if (fn_type_unification (fn
, explicit_args
, targs
,
14417 (check_rettype
|| DECL_CONV_FN_P (fn
)
14418 ? TREE_TYPE (decl_type
) : NULL_TREE
),
14419 DEDUCE_EXACT
, LOOKUP_NORMAL
))
14425 /* Return the innermost template arguments that, when applied to a
14426 template specialization whose innermost template parameters are
14427 TPARMS, and whose specialization arguments are PARMS, yield the
14430 For example, suppose we have:
14432 template <class T, class U> struct S {};
14433 template <class T> struct S<T*, int> {};
14435 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
14436 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
14437 int}. The resulting vector will be {double}, indicating that `T'
14438 is bound to `double'. */
14441 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
14443 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
14445 tree innermost_deduced_args
;
14447 innermost_deduced_args
= make_tree_vec (ntparms
);
14448 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
14450 deduced_args
= copy_node (args
);
14451 SET_TMPL_ARGS_LEVEL (deduced_args
,
14452 TMPL_ARGS_DEPTH (deduced_args
),
14453 innermost_deduced_args
);
14456 deduced_args
= innermost_deduced_args
;
14458 if (unify (tparms
, deduced_args
,
14459 INNERMOST_TEMPLATE_ARGS (spec_args
),
14460 INNERMOST_TEMPLATE_ARGS (args
),
14464 for (i
= 0; i
< ntparms
; ++i
)
14465 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
14468 /* Verify that nondeduced template arguments agree with the type
14469 obtained from argument deduction.
14473 struct A { typedef int X; };
14474 template <class T, class U> struct C {};
14475 template <class T> struct C<T, typename T::X> {};
14477 Then with the instantiation `C<A, int>', we can deduce that
14478 `T' is `A' but unify () does not check whether `typename T::X'
14480 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
14481 if (spec_args
== error_mark_node
14482 /* We only need to check the innermost arguments; the other
14483 arguments will always agree. */
14484 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
14485 INNERMOST_TEMPLATE_ARGS (args
)))
14488 /* Now that we have bindings for all of the template arguments,
14489 ensure that the arguments deduced for the template template
14490 parameters have compatible template parameter lists. See the use
14491 of template_template_parm_bindings_ok_p in fn_type_unification
14492 for more information. */
14493 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
14496 return deduced_args
;
14499 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
14500 Return the TREE_LIST node with the most specialized template, if
14501 any. If there is no most specialized template, the error_mark_node
14504 Note that this function does not look at, or modify, the
14505 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
14506 returned is one of the elements of INSTANTIATIONS, callers may
14507 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
14508 and retrieve it from the value returned. */
14511 most_specialized_instantiation (tree templates
)
14515 ++processing_template_decl
;
14518 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
14522 if (get_bindings (TREE_VALUE (champ
),
14523 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
14524 NULL_TREE
, /*check_ret=*/false))
14527 if (get_bindings (TREE_VALUE (fn
),
14528 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
14529 NULL_TREE
, /*check_ret=*/false))
14536 /* Equally specialized, move to next function. If there
14537 is no next function, nothing's most specialized. */
14538 fn
= TREE_CHAIN (fn
);
14546 /* Now verify that champ is better than everything earlier in the
14547 instantiation list. */
14548 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
14549 if (get_bindings (TREE_VALUE (champ
),
14550 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
14551 NULL_TREE
, /*check_ret=*/false)
14552 || !get_bindings (TREE_VALUE (fn
),
14553 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
14554 NULL_TREE
, /*check_ret=*/false))
14560 processing_template_decl
--;
14563 return error_mark_node
;
14568 /* If DECL is a specialization of some template, return the most
14569 general such template. Otherwise, returns NULL_TREE.
14571 For example, given:
14573 template <class T> struct S { template <class U> void f(U); };
14575 if TMPL is `template <class U> void S<int>::f(U)' this will return
14576 the full template. This function will not trace past partial
14577 specializations, however. For example, given in addition:
14579 template <class T> struct S<T*> { template <class U> void f(U); };
14581 if TMPL is `template <class U> void S<int*>::f(U)' this will return
14582 `template <class T> template <class U> S<T*>::f(U)'. */
14585 most_general_template (tree decl
)
14587 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
14588 an immediate specialization. */
14589 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14591 if (DECL_TEMPLATE_INFO (decl
)) {
14592 decl
= DECL_TI_TEMPLATE (decl
);
14594 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
14595 template friend. */
14596 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
14602 /* Look for more and more general templates. */
14603 while (DECL_TEMPLATE_INFO (decl
))
14605 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
14606 (See cp-tree.h for details.) */
14607 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
14610 if (CLASS_TYPE_P (TREE_TYPE (decl
))
14611 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
14614 /* Stop if we run into an explicitly specialized class template. */
14615 if (!DECL_NAMESPACE_SCOPE_P (decl
)
14616 && DECL_CONTEXT (decl
)
14617 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
14620 decl
= DECL_TI_TEMPLATE (decl
);
14626 /* Return the most specialized of the class template partial
14627 specializations of TMPL which can produce TYPE, a specialization of
14628 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
14629 a _TYPE node corresponding to the partial specialization, while the
14630 TREE_PURPOSE is the set of template arguments that must be
14631 substituted into the TREE_TYPE in order to generate TYPE.
14633 If the choice of partial specialization is ambiguous, a diagnostic
14634 is issued, and the error_mark_node is returned. If there are no
14635 partial specializations of TMPL matching TYPE, then NULL_TREE is
14639 most_specialized_class (tree type
, tree tmpl
)
14641 tree list
= NULL_TREE
;
14647 tree outer_args
= NULL_TREE
;
14649 tmpl
= most_general_template (tmpl
);
14650 args
= CLASSTYPE_TI_ARGS (type
);
14652 /* For determining which partial specialization to use, only the
14653 innermost args are interesting. */
14654 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
14656 outer_args
= strip_innermost_template_args (args
, 1);
14657 args
= INNERMOST_TEMPLATE_ARGS (args
);
14660 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
14662 tree partial_spec_args
;
14664 tree parms
= TREE_VALUE (t
);
14666 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
14671 ++processing_template_decl
;
14673 /* Discard the outer levels of args, and then substitute in the
14674 template args from the enclosing class. */
14675 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
14676 partial_spec_args
= tsubst_template_args
14677 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
14679 /* PARMS already refers to just the innermost parms, but the
14680 template parms in partial_spec_args had their levels lowered
14681 by tsubst, so we need to do the same for the parm list. We
14682 can't just tsubst the TREE_VEC itself, as tsubst wants to
14683 treat a TREE_VEC as an argument vector. */
14684 parms
= copy_node (parms
);
14685 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
14686 TREE_VEC_ELT (parms
, i
) =
14687 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
14689 --processing_template_decl
;
14691 spec_args
= get_class_bindings (parms
,
14697 spec_args
= add_to_template_args (outer_args
, spec_args
);
14698 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
14699 TREE_TYPE (list
) = TREE_TYPE (t
);
14706 ambiguous_p
= false;
14709 t
= TREE_CHAIN (t
);
14710 for (; t
; t
= TREE_CHAIN (t
))
14712 fate
= more_specialized_class (champ
, t
);
14719 t
= TREE_CHAIN (t
);
14722 ambiguous_p
= true;
14731 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
14733 fate
= more_specialized_class (champ
, t
);
14736 ambiguous_p
= true;
14743 const char *str
= "candidates are:";
14744 error ("ambiguous class template instantiation for %q#T", type
);
14745 for (t
= list
; t
; t
= TREE_CHAIN (t
))
14747 error ("%s %+#T", str
, TREE_TYPE (t
));
14750 return error_mark_node
;
14756 /* Explicitly instantiate DECL. */
14759 do_decl_instantiation (tree decl
, tree storage
)
14761 tree result
= NULL_TREE
;
14764 if (!decl
|| decl
== error_mark_node
)
14765 /* An error occurred, for which grokdeclarator has already issued
14766 an appropriate message. */
14768 else if (! DECL_LANG_SPECIFIC (decl
))
14770 error ("explicit instantiation of non-template %q#D", decl
);
14773 else if (TREE_CODE (decl
) == VAR_DECL
)
14775 /* There is an asymmetry here in the way VAR_DECLs and
14776 FUNCTION_DECLs are handled by grokdeclarator. In the case of
14777 the latter, the DECL we get back will be marked as a
14778 template instantiation, and the appropriate
14779 DECL_TEMPLATE_INFO will be set up. This does not happen for
14780 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
14781 should handle VAR_DECLs as it currently handles
14783 if (!DECL_CLASS_SCOPE_P (decl
))
14785 error ("%qD is not a static data member of a class template", decl
);
14788 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
14789 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
14791 error ("no matching template for %qD found", decl
);
14794 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
14796 error ("type %qT for explicit instantiation %qD does not match "
14797 "declared type %qT", TREE_TYPE (result
), decl
,
14802 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
14804 error ("explicit instantiation of %q#D", decl
);
14810 /* Check for various error cases. Note that if the explicit
14811 instantiation is valid the RESULT will currently be marked as an
14812 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
14813 until we get here. */
14815 if (DECL_TEMPLATE_SPECIALIZATION (result
))
14817 /* DR 259 [temp.spec].
14819 Both an explicit instantiation and a declaration of an explicit
14820 specialization shall not appear in a program unless the explicit
14821 instantiation follows a declaration of the explicit specialization.
14823 For a given set of template parameters, if an explicit
14824 instantiation of a template appears after a declaration of an
14825 explicit specialization for that template, the explicit
14826 instantiation has no effect. */
14829 else if (DECL_EXPLICIT_INSTANTIATION (result
))
14833 No program shall explicitly instantiate any template more
14836 We check DECL_NOT_REALLY_EXTERN so as not to complain when
14837 the first instantiation was `extern' and the second is not,
14838 and EXTERN_P for the opposite case. */
14839 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
14840 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
14841 /* If an "extern" explicit instantiation follows an ordinary
14842 explicit instantiation, the template is instantiated. */
14846 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
14848 error ("no matching template for %qD found", result
);
14851 else if (!DECL_TEMPLATE_INFO (result
))
14853 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
14857 if (storage
== NULL_TREE
)
14859 else if (storage
== ridpointers
[(int) RID_EXTERN
])
14861 if (!in_system_header
&& (cxx_dialect
== cxx98
))
14862 pedwarn (input_location
, OPT_pedantic
,
14863 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
14868 error ("storage class %qD applied to template instantiation", storage
);
14870 check_explicit_instantiation_namespace (result
);
14871 mark_decl_instantiated (result
, extern_p
);
14873 instantiate_decl (result
, /*defer_ok=*/1,
14874 /*expl_inst_class_mem_p=*/false);
14878 mark_class_instantiated (tree t
, int extern_p
)
14880 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
14881 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
14882 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
14883 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
14886 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
14887 rest_of_type_compilation (t
, 1);
14891 /* Called from do_type_instantiation through binding_table_foreach to
14892 do recursive instantiation for the type bound in ENTRY. */
14894 bt_instantiate_type_proc (binding_entry entry
, void *data
)
14896 tree storage
= *(tree
*) data
;
14898 if (MAYBE_CLASS_TYPE_P (entry
->type
)
14899 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
14900 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
14903 /* Called from do_type_instantiation to instantiate a member
14904 (a member function or a static member variable) of an
14905 explicitly instantiated class template. */
14907 instantiate_class_member (tree decl
, int extern_p
)
14909 mark_decl_instantiated (decl
, extern_p
);
14911 instantiate_decl (decl
, /*defer_ok=*/1,
14912 /*expl_inst_class_mem_p=*/true);
14915 /* Perform an explicit instantiation of template class T. STORAGE, if
14916 non-null, is the RID for extern, inline or static. COMPLAIN is
14917 nonzero if this is called from the parser, zero if called recursively,
14918 since the standard is unclear (as detailed below). */
14921 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
14926 int previous_instantiation_extern_p
= 0;
14928 if (TREE_CODE (t
) == TYPE_DECL
)
14931 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
14933 error ("explicit instantiation of non-template type %qT", t
);
14939 if (!COMPLETE_TYPE_P (t
))
14941 if (complain
& tf_error
)
14942 error ("explicit instantiation of %q#T before definition of template",
14947 if (storage
!= NULL_TREE
)
14949 if (!in_system_header
)
14951 if (storage
== ridpointers
[(int) RID_EXTERN
])
14953 if (cxx_dialect
== cxx98
)
14954 pedwarn (input_location
, OPT_pedantic
,
14955 "ISO C++ 1998 forbids the use of %<extern%> on "
14956 "explicit instantiations");
14959 pedwarn (input_location
, OPT_pedantic
,
14960 "ISO C++ forbids the use of %qE"
14961 " on explicit instantiations", storage
);
14964 if (storage
== ridpointers
[(int) RID_INLINE
])
14966 else if (storage
== ridpointers
[(int) RID_EXTERN
])
14968 else if (storage
== ridpointers
[(int) RID_STATIC
])
14972 error ("storage class %qD applied to template instantiation",
14978 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
14980 /* DR 259 [temp.spec].
14982 Both an explicit instantiation and a declaration of an explicit
14983 specialization shall not appear in a program unless the explicit
14984 instantiation follows a declaration of the explicit specialization.
14986 For a given set of template parameters, if an explicit
14987 instantiation of a template appears after a declaration of an
14988 explicit specialization for that template, the explicit
14989 instantiation has no effect. */
14992 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
14996 No program shall explicitly instantiate any template more
14999 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15000 instantiation was `extern'. If EXTERN_P then the second is.
15001 These cases are OK. */
15002 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
15004 if (!previous_instantiation_extern_p
&& !extern_p
15005 && (complain
& tf_error
))
15006 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
15008 /* If we've already instantiated the template, just return now. */
15009 if (!CLASSTYPE_INTERFACE_ONLY (t
))
15013 check_explicit_instantiation_namespace (TYPE_NAME (t
));
15014 mark_class_instantiated (t
, extern_p
);
15022 /* In contrast to implicit instantiation, where only the
15023 declarations, and not the definitions, of members are
15024 instantiated, we have here:
15028 The explicit instantiation of a class template specialization
15029 implies the instantiation of all of its members not
15030 previously explicitly specialized in the translation unit
15031 containing the explicit instantiation.
15033 Of course, we can't instantiate member template classes, since
15034 we don't have any arguments for them. Note that the standard
15035 is unclear on whether the instantiation of the members are
15036 *explicit* instantiations or not. However, the most natural
15037 interpretation is that it should be an explicit instantiation. */
15040 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
15041 if (TREE_CODE (tmp
) == FUNCTION_DECL
15042 && DECL_TEMPLATE_INSTANTIATION (tmp
))
15043 instantiate_class_member (tmp
, extern_p
);
15045 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
15046 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
15047 instantiate_class_member (tmp
, extern_p
);
15049 if (CLASSTYPE_NESTED_UTDS (t
))
15050 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
15051 bt_instantiate_type_proc
, &storage
);
15055 /* Given a function DECL, which is a specialization of TMPL, modify
15056 DECL to be a re-instantiation of TMPL with the same template
15057 arguments. TMPL should be the template into which tsubst'ing
15058 should occur for DECL, not the most general template.
15060 One reason for doing this is a scenario like this:
15063 void f(const T&, int i);
15065 void g() { f(3, 7); }
15068 void f(const T& t, const int i) { }
15070 Note that when the template is first instantiated, with
15071 instantiate_template, the resulting DECL will have no name for the
15072 first parameter, and the wrong type for the second. So, when we go
15073 to instantiate the DECL, we regenerate it. */
15076 regenerate_decl_from_template (tree decl
, tree tmpl
)
15078 /* The arguments used to instantiate DECL, from the most general
15083 args
= DECL_TI_ARGS (decl
);
15084 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
15086 /* Make sure that we can see identifiers, and compute access
15088 push_access_scope (decl
);
15090 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15098 args_depth
= TMPL_ARGS_DEPTH (args
);
15099 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
15100 if (args_depth
> parms_depth
)
15101 args
= get_innermost_template_args (args
, parms_depth
);
15103 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
15104 args
, tf_error
, NULL_TREE
);
15106 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
15109 /* Merge parameter declarations. */
15110 decl_parm
= skip_artificial_parms_for (decl
,
15111 DECL_ARGUMENTS (decl
));
15113 = skip_artificial_parms_for (code_pattern
,
15114 DECL_ARGUMENTS (code_pattern
));
15115 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
15120 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
15121 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
15122 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
15124 parm_type
= type_decays_to (parm_type
);
15125 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
15126 TREE_TYPE (decl_parm
) = parm_type
;
15127 attributes
= DECL_ATTRIBUTES (pattern_parm
);
15128 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
15130 DECL_ATTRIBUTES (decl_parm
) = attributes
;
15131 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
15133 decl_parm
= TREE_CHAIN (decl_parm
);
15134 pattern_parm
= TREE_CHAIN (pattern_parm
);
15136 /* Merge any parameters that match with the function parameter
15138 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
15141 tree expanded_types
;
15142 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15143 the parameters in this function parameter pack. */
15144 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
15145 args
, tf_error
, NULL_TREE
);
15146 len
= TREE_VEC_LENGTH (expanded_types
);
15147 for (i
= 0; i
< len
; i
++)
15152 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
15153 /* Rename the parameter to include the index. */
15154 DECL_NAME (decl_parm
) =
15155 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
15156 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
15157 parm_type
= type_decays_to (parm_type
);
15158 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
15159 TREE_TYPE (decl_parm
) = parm_type
;
15160 attributes
= DECL_ATTRIBUTES (pattern_parm
);
15161 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
15163 DECL_ATTRIBUTES (decl_parm
) = attributes
;
15164 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
15166 decl_parm
= TREE_CHAIN (decl_parm
);
15169 /* Merge additional specifiers from the CODE_PATTERN. */
15170 if (DECL_DECLARED_INLINE_P (code_pattern
)
15171 && !DECL_DECLARED_INLINE_P (decl
))
15172 DECL_DECLARED_INLINE_P (decl
) = 1;
15174 else if (TREE_CODE (decl
) == VAR_DECL
)
15175 DECL_INITIAL (decl
) =
15176 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
15177 tf_error
, DECL_TI_TEMPLATE (decl
),
15178 /*integral_constant_expression_p=*/false);
15180 gcc_unreachable ();
15182 pop_access_scope (decl
);
15185 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15186 substituted to get DECL. */
15189 template_for_substitution (tree decl
)
15191 tree tmpl
= DECL_TI_TEMPLATE (decl
);
15193 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15194 for the instantiation. This is not always the most general
15195 template. Consider, for example:
15198 struct S { template <class U> void f();
15199 template <> void f<int>(); };
15201 and an instantiation of S<double>::f<int>. We want TD to be the
15202 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15203 while (/* An instantiation cannot have a definition, so we need a
15204 more general template. */
15205 DECL_TEMPLATE_INSTANTIATION (tmpl
)
15206 /* We must also deal with friend templates. Given:
15208 template <class T> struct S {
15209 template <class U> friend void f() {};
15212 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15213 so far as the language is concerned, but that's still
15214 where we get the pattern for the instantiation from. On
15215 other hand, if the definition comes outside the class, say:
15217 template <class T> struct S {
15218 template <class U> friend void f();
15220 template <class U> friend void f() {}
15222 we don't need to look any further. That's what the check for
15223 DECL_INITIAL is for. */
15224 || (TREE_CODE (decl
) == FUNCTION_DECL
15225 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
15226 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
15228 /* The present template, TD, should not be a definition. If it
15229 were a definition, we should be using it! Note that we
15230 cannot restructure the loop to just keep going until we find
15231 a template with a definition, since that might go too far if
15232 a specialization was declared, but not defined. */
15233 gcc_assert (TREE_CODE (decl
) != VAR_DECL
15234 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
15236 /* Fetch the more general template. */
15237 tmpl
= DECL_TI_TEMPLATE (tmpl
);
15243 /* Produce the definition of D, a _DECL generated from a template. If
15244 DEFER_OK is nonzero, then we don't have to actually do the
15245 instantiation now; we just have to do it sometime. Normally it is
15246 an error if this is an explicit instantiation but D is undefined.
15247 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15248 explicitly instantiated class template. */
15251 instantiate_decl (tree d
, int defer_ok
,
15252 bool expl_inst_class_mem_p
)
15254 tree tmpl
= DECL_TI_TEMPLATE (d
);
15261 bool pattern_defined
;
15263 location_t saved_loc
= input_location
;
15266 /* This function should only be used to instantiate templates for
15267 functions and static member variables. */
15268 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
15269 || TREE_CODE (d
) == VAR_DECL
);
15271 /* Variables are never deferred; if instantiation is required, they
15272 are instantiated right away. That allows for better code in the
15273 case that an expression refers to the value of the variable --
15274 if the variable has a constant value the referring expression can
15275 take advantage of that fact. */
15276 if (TREE_CODE (d
) == VAR_DECL
)
15279 /* Don't instantiate cloned functions. Instead, instantiate the
15280 functions they cloned. */
15281 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
15282 d
= DECL_CLONED_FUNCTION (d
);
15284 if (DECL_TEMPLATE_INSTANTIATED (d
))
15285 /* D has already been instantiated. It might seem reasonable to
15286 check whether or not D is an explicit instantiation, and, if so,
15287 stop here. But when an explicit instantiation is deferred
15288 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
15289 is set, even though we still need to do the instantiation. */
15292 /* If we already have a specialization of this declaration, then
15293 there's no reason to instantiate it. Note that
15294 retrieve_specialization gives us both instantiations and
15295 specializations, so we must explicitly check
15296 DECL_TEMPLATE_SPECIALIZATION. */
15297 gen_tmpl
= most_general_template (tmpl
);
15298 gen_args
= DECL_TI_ARGS (d
);
15299 spec
= retrieve_specialization (gen_tmpl
, gen_args
,
15300 /*class_specializations_p=*/false);
15301 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
15304 /* This needs to happen before any tsubsting. */
15305 if (! push_tinst_level (d
))
15308 timevar_push (TV_PARSE
);
15310 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15311 for the instantiation. */
15312 td
= template_for_substitution (d
);
15313 code_pattern
= DECL_TEMPLATE_RESULT (td
);
15315 /* We should never be trying to instantiate a member of a class
15316 template or partial specialization. */
15317 gcc_assert (d
!= code_pattern
);
15319 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
15320 || DECL_TEMPLATE_SPECIALIZATION (td
))
15321 /* In the case of a friend template whose definition is provided
15322 outside the class, we may have too many arguments. Drop the
15323 ones we don't need. The same is true for specializations. */
15324 args
= get_innermost_template_args
15325 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
15329 if (TREE_CODE (d
) == FUNCTION_DECL
)
15330 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
15332 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
15334 /* We may be in the middle of deferred access check. Disable it now. */
15335 push_deferring_access_checks (dk_no_deferred
);
15337 /* Unless an explicit instantiation directive has already determined
15338 the linkage of D, remember that a definition is available for
15340 if (pattern_defined
15341 && !DECL_INTERFACE_KNOWN (d
)
15342 && !DECL_NOT_REALLY_EXTERN (d
))
15343 mark_definable (d
);
15345 input_location
= DECL_SOURCE_LOCATION (d
);
15347 /* If D is a member of an explicitly instantiated class template,
15348 and no definition is available, treat it like an implicit
15350 if (!pattern_defined
&& expl_inst_class_mem_p
15351 && DECL_EXPLICIT_INSTANTIATION (d
))
15353 DECL_NOT_REALLY_EXTERN (d
) = 0;
15354 DECL_INTERFACE_KNOWN (d
) = 0;
15355 SET_DECL_IMPLICIT_INSTANTIATION (d
);
15360 /* Recheck the substitutions to obtain any warning messages
15361 about ignoring cv qualifiers. */
15362 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
15363 tree type
= TREE_TYPE (gen
);
15365 /* Make sure that we can see identifiers, and compute access
15366 correctly. D is already the target FUNCTION_DECL with the
15368 push_access_scope (d
);
15370 if (TREE_CODE (gen
) == FUNCTION_DECL
)
15372 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
15373 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
15375 /* Don't simply tsubst the function type, as that will give
15376 duplicate warnings about poor parameter qualifications.
15377 The function arguments are the same as the decl_arguments
15378 without the top level cv qualifiers. */
15379 type
= TREE_TYPE (type
);
15381 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
15383 pop_access_scope (d
);
15386 /* Check to see whether we know that this template will be
15387 instantiated in some other file, as with "extern template"
15389 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
15390 /* In general, we do not instantiate such templates... */
15392 /* ... but we instantiate inline functions so that we can inline
15393 them. An explicit instantiation declaration prohibits implicit
15394 instantiation of non-inline functions. With high levels of
15395 optimization, we would normally inline non-inline functions
15396 -- but we're not allowed to do that for "extern template" functions.
15397 Therefore, we check DECL_DECLARED_INLINE_P, rather than
15398 possibly_inlined_p. And ... */
15399 && ! (TREE_CODE (d
) == FUNCTION_DECL
15400 && DECL_DECLARED_INLINE_P (d
))
15401 /* ... we instantiate static data members whose values are
15402 needed in integral constant expressions. */
15403 && ! (TREE_CODE (d
) == VAR_DECL
15404 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d
)))
15406 /* Defer all other templates, unless we have been explicitly
15407 forbidden from doing so. */
15408 if (/* If there is no definition, we cannot instantiate the
15411 /* If it's OK to postpone instantiation, do so. */
15413 /* If this is a static data member that will be defined
15414 elsewhere, we don't want to instantiate the entire data
15415 member, but we do want to instantiate the initializer so that
15416 we can substitute that elsewhere. */
15417 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
15419 /* The definition of the static data member is now required so
15420 we must substitute the initializer. */
15421 if (TREE_CODE (d
) == VAR_DECL
15422 && !DECL_INITIAL (d
)
15423 && DECL_INITIAL (code_pattern
))
15428 ns
= decl_namespace_context (d
);
15429 push_nested_namespace (ns
);
15430 push_nested_class (DECL_CONTEXT (d
));
15431 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
15433 tf_warning_or_error
, NULL_TREE
,
15434 /*integral_constant_expression_p=*/false);
15435 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
15436 /*asmspec_tree=*/NULL_TREE
,
15437 LOOKUP_ONLYCONVERTING
);
15438 pop_nested_class ();
15439 pop_nested_namespace (ns
);
15442 /* We restore the source position here because it's used by
15443 add_pending_template. */
15444 input_location
= saved_loc
;
15446 if (at_eof
&& !pattern_defined
15447 && DECL_EXPLICIT_INSTANTIATION (d
)
15448 && DECL_NOT_REALLY_EXTERN (d
))
15451 The definition of a non-exported function template, a
15452 non-exported member function template, or a non-exported
15453 member function or static data member of a class template
15454 shall be present in every translation unit in which it is
15455 explicitly instantiated. */
15456 permerror (input_location
, "explicit instantiation of %qD "
15457 "but no definition available", d
);
15459 /* ??? Historically, we have instantiated inline functions, even
15460 when marked as "extern template". */
15461 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
15462 add_pending_template (d
);
15465 /* Tell the repository that D is available in this translation unit
15466 -- and see if it is supposed to be instantiated here. */
15467 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
15469 /* In a PCH file, despite the fact that the repository hasn't
15470 requested instantiation in the PCH it is still possible that
15471 an instantiation will be required in a file that includes the
15474 add_pending_template (d
);
15475 /* Instantiate inline functions so that the inliner can do its
15476 job, even though we'll not be emitting a copy of this
15478 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
15482 need_push
= !cfun
|| !global_bindings_p ();
15484 push_to_top_level ();
15486 /* Mark D as instantiated so that recursive calls to
15487 instantiate_decl do not try to instantiate it again. */
15488 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
15490 /* Regenerate the declaration in case the template has been modified
15491 by a subsequent redeclaration. */
15492 regenerate_decl_from_template (d
, td
);
15494 /* We already set the file and line above. Reset them now in case
15495 they changed as a result of calling regenerate_decl_from_template. */
15496 input_location
= DECL_SOURCE_LOCATION (d
);
15498 if (TREE_CODE (d
) == VAR_DECL
)
15502 /* Clear out DECL_RTL; whatever was there before may not be right
15503 since we've reset the type of the declaration. */
15504 SET_DECL_RTL (d
, NULL_RTX
);
15505 DECL_IN_AGGR_P (d
) = 0;
15507 /* The initializer is placed in DECL_INITIAL by
15508 regenerate_decl_from_template. Pull it out so that
15509 finish_decl can process it. */
15510 init
= DECL_INITIAL (d
);
15511 DECL_INITIAL (d
) = NULL_TREE
;
15512 DECL_INITIALIZED_P (d
) = 0;
15514 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
15515 initializer. That function will defer actual emission until
15516 we have a chance to determine linkage. */
15517 DECL_EXTERNAL (d
) = 0;
15519 /* Enter the scope of D so that access-checking works correctly. */
15520 push_nested_class (DECL_CONTEXT (d
));
15521 finish_decl (d
, init
, NULL_TREE
);
15522 pop_nested_class ();
15524 else if (TREE_CODE (d
) == FUNCTION_DECL
)
15526 htab_t saved_local_specializations
;
15531 /* Save away the current list, in case we are instantiating one
15532 template from within the body of another. */
15533 saved_local_specializations
= local_specializations
;
15535 /* Set up the list of local specializations. */
15536 local_specializations
= htab_create (37,
15537 hash_local_specialization
,
15538 eq_local_specializations
,
15541 /* Set up context. */
15542 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
15544 /* Create substitution entries for the parameters. */
15545 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
15546 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
15547 spec_parm
= DECL_ARGUMENTS (d
);
15548 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
15550 register_local_specialization (spec_parm
, tmpl_parm
);
15551 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
15552 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
15554 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
15556 register_local_specialization (spec_parm
, tmpl_parm
);
15557 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
15558 spec_parm
= TREE_CHAIN (spec_parm
);
15560 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
15562 /* Collect all of the extra "packed" parameters into an
15566 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
15567 tree argtypepack
= make_node (TYPE_ARGUMENT_PACK
);
15571 /* Count how many parameters remain. */
15572 for (t
= spec_parm
; t
; t
= TREE_CHAIN (t
))
15575 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
15576 parmvec
= make_tree_vec (len
);
15577 parmtypevec
= make_tree_vec (len
);
15578 for(i
= 0; i
< len
; i
++, spec_parm
= TREE_CHAIN (spec_parm
))
15580 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
15581 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
15584 /* Build the argument packs. */
15585 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
15586 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
15587 TREE_TYPE (argpack
) = argtypepack
;
15589 /* Register the (value) argument pack as a specialization of
15590 TMPL_PARM, then move on. */
15591 register_local_specialization (argpack
, tmpl_parm
);
15592 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
15594 gcc_assert (!spec_parm
);
15596 /* Substitute into the body of the function. */
15597 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
15598 tf_warning_or_error
, tmpl
,
15599 /*integral_constant_expression_p=*/false);
15601 /* Set the current input_location to the end of the function
15602 so that finish_function knows where we are. */
15603 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
15605 /* We don't need the local specializations any more. */
15606 htab_delete (local_specializations
);
15607 local_specializations
= saved_local_specializations
;
15609 /* Finish the function. */
15610 d
= finish_function (0);
15611 expand_or_defer_fn (d
);
15614 /* We're not deferring instantiation any more. */
15615 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
15618 pop_from_top_level ();
15621 input_location
= saved_loc
;
15622 pop_deferring_access_checks ();
15623 pop_tinst_level ();
15625 timevar_pop (TV_PARSE
);
15630 /* Run through the list of templates that we wish we could
15631 instantiate, and instantiate any we can. RETRIES is the
15632 number of times we retry pending template instantiation. */
15635 instantiate_pending_templates (int retries
)
15638 location_t saved_loc
= input_location
;
15640 /* Instantiating templates may trigger vtable generation. This in turn
15641 may require further template instantiations. We place a limit here
15642 to avoid infinite loop. */
15643 if (pending_templates
&& retries
>= max_tinst_depth
)
15645 tree decl
= pending_templates
->tinst
->decl
;
15647 error ("template instantiation depth exceeds maximum of %d"
15648 " instantiating %q+D, possibly from virtual table generation"
15649 " (use -ftemplate-depth-NN to increase the maximum)",
15650 max_tinst_depth
, decl
);
15651 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15652 /* Pretend that we defined it. */
15653 DECL_INITIAL (decl
) = error_mark_node
;
15659 struct pending_template
**t
= &pending_templates
;
15660 struct pending_template
*last
= NULL
;
15664 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
15665 bool complete
= false;
15667 if (TYPE_P (instantiation
))
15671 if (!COMPLETE_TYPE_P (instantiation
))
15673 instantiate_class_template (instantiation
);
15674 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
15675 for (fn
= TYPE_METHODS (instantiation
);
15677 fn
= TREE_CHAIN (fn
))
15678 if (! DECL_ARTIFICIAL (fn
))
15679 instantiate_decl (fn
,
15681 /*expl_inst_class_mem_p=*/false);
15682 if (COMPLETE_TYPE_P (instantiation
))
15686 complete
= COMPLETE_TYPE_P (instantiation
);
15690 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
15691 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
15694 = instantiate_decl (instantiation
,
15696 /*expl_inst_class_mem_p=*/false);
15697 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
15701 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
15702 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
15706 /* If INSTANTIATION has been instantiated, then we don't
15707 need to consider it again in the future. */
15715 current_tinst_level
= NULL
;
15717 last_pending_template
= last
;
15719 while (reconsider
);
15721 input_location
= saved_loc
;
15724 /* Substitute ARGVEC into T, which is a list of initializers for
15725 either base class or a non-static data member. The TREE_PURPOSEs
15726 are DECLs, and the TREE_VALUEs are the initializer values. Used by
15727 instantiate_decl. */
15730 tsubst_initializer_list (tree t
, tree argvec
)
15732 tree inits
= NULL_TREE
;
15734 for (; t
; t
= TREE_CHAIN (t
))
15738 tree expanded_bases
= NULL_TREE
;
15739 tree expanded_arguments
= NULL_TREE
;
15742 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
15747 /* Expand the base class expansion type into separate base
15749 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
15750 tf_warning_or_error
,
15752 if (expanded_bases
== error_mark_node
)
15755 /* We'll be building separate TREE_LISTs of arguments for
15757 len
= TREE_VEC_LENGTH (expanded_bases
);
15758 expanded_arguments
= make_tree_vec (len
);
15759 for (i
= 0; i
< len
; i
++)
15760 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
15762 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
15763 expand each argument in the TREE_VALUE of t. */
15764 expr
= make_node (EXPR_PACK_EXPANSION
);
15765 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
15766 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
15768 if (TREE_VALUE (t
) == void_type_node
)
15769 /* VOID_TYPE_NODE is used to indicate
15770 value-initialization. */
15772 for (i
= 0; i
< len
; i
++)
15773 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
15777 /* Substitute parameter packs into each argument in the
15779 in_base_initializer
= 1;
15780 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
15782 tree expanded_exprs
;
15784 /* Expand the argument. */
15785 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
15787 = tsubst_pack_expansion (expr
, argvec
,
15788 tf_warning_or_error
,
15790 if (expanded_exprs
== error_mark_node
)
15793 /* Prepend each of the expanded expressions to the
15794 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
15795 for (i
= 0; i
< len
; i
++)
15797 TREE_VEC_ELT (expanded_arguments
, i
) =
15798 tree_cons (NULL_TREE
,
15799 TREE_VEC_ELT (expanded_exprs
, i
),
15800 TREE_VEC_ELT (expanded_arguments
, i
));
15803 in_base_initializer
= 0;
15805 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
15806 since we built them backwards. */
15807 for (i
= 0; i
< len
; i
++)
15809 TREE_VEC_ELT (expanded_arguments
, i
) =
15810 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
15815 for (i
= 0; i
< len
; ++i
)
15817 if (expanded_bases
)
15819 decl
= TREE_VEC_ELT (expanded_bases
, i
);
15820 decl
= expand_member_init (decl
);
15821 init
= TREE_VEC_ELT (expanded_arguments
, i
);
15825 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
15826 tf_warning_or_error
, NULL_TREE
);
15828 decl
= expand_member_init (decl
);
15829 if (decl
&& !DECL_P (decl
))
15830 in_base_initializer
= 1;
15832 init
= tsubst_expr (TREE_VALUE (t
), argvec
,
15833 tf_warning_or_error
, NULL_TREE
,
15834 /*integral_constant_expression_p=*/false);
15835 in_base_initializer
= 0;
15840 init
= build_tree_list (decl
, init
);
15841 TREE_CHAIN (init
) = inits
;
15849 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
15852 set_current_access_from_decl (tree decl
)
15854 if (TREE_PRIVATE (decl
))
15855 current_access_specifier
= access_private_node
;
15856 else if (TREE_PROTECTED (decl
))
15857 current_access_specifier
= access_protected_node
;
15859 current_access_specifier
= access_public_node
;
15862 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
15863 is the instantiation (which should have been created with
15864 start_enum) and ARGS are the template arguments to use. */
15867 tsubst_enum (tree tag
, tree newtag
, tree args
)
15871 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
15876 decl
= TREE_VALUE (e
);
15877 /* Note that in a template enum, the TREE_VALUE is the
15878 CONST_DECL, not the corresponding INTEGER_CST. */
15879 value
= tsubst_expr (DECL_INITIAL (decl
),
15880 args
, tf_warning_or_error
, NULL_TREE
,
15881 /*integral_constant_expression_p=*/true);
15883 /* Give this enumeration constant the correct access. */
15884 set_current_access_from_decl (decl
);
15886 /* Actually build the enumerator itself. */
15887 build_enumerator (DECL_NAME (decl
), value
, newtag
);
15890 finish_enum (newtag
);
15891 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
15892 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
15895 /* DECL is a FUNCTION_DECL that is a template specialization. Return
15896 its type -- but without substituting the innermost set of template
15897 arguments. So, innermost set of template parameters will appear in
15901 get_mostly_instantiated_function_type (tree decl
)
15909 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
15910 targs
= DECL_TI_ARGS (decl
);
15911 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
15912 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
15914 /* There should be as many levels of arguments as there are levels
15916 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
15918 fn_type
= TREE_TYPE (tmpl
);
15920 if (parm_depth
== 1)
15921 /* No substitution is necessary. */
15925 int i
, save_access_control
;
15928 /* Replace the innermost level of the TARGS with NULL_TREEs to
15929 let tsubst know not to substitute for those parameters. */
15930 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15931 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
15932 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
15933 TMPL_ARGS_LEVEL (targs
, i
));
15934 SET_TMPL_ARGS_LEVEL (partial_args
,
15935 TMPL_ARGS_DEPTH (targs
),
15936 make_tree_vec (DECL_NTPARMS (tmpl
)));
15938 /* Disable access control as this function is used only during
15940 save_access_control
= flag_access_control
;
15941 flag_access_control
= 0;
15943 ++processing_template_decl
;
15944 /* Now, do the (partial) substitution to figure out the
15945 appropriate function type. */
15946 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
15947 --processing_template_decl
;
15949 /* Substitute into the template parameters to obtain the real
15950 innermost set of parameters. This step is important if the
15951 innermost set of template parameters contains value
15952 parameters whose types depend on outer template parameters. */
15953 TREE_VEC_LENGTH (partial_args
)--;
15954 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
15956 flag_access_control
= save_access_control
;
15962 /* Return truthvalue if we're processing a template different from
15963 the last one involved in diagnostics. */
15965 problematic_instantiation_changed (void)
15967 return last_template_error_tick
!= tinst_level_tick
;
15970 /* Remember current template involved in diagnostics. */
15972 record_last_problematic_instantiation (void)
15974 last_template_error_tick
= tinst_level_tick
;
15977 struct tinst_level
*
15978 current_instantiation (void)
15980 return current_tinst_level
;
15983 /* [temp.param] Check that template non-type parm TYPE is of an allowable
15984 type. Return zero for ok, nonzero for disallowed. Issue error and
15985 warning messages under control of COMPLAIN. */
15988 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
15990 if (INTEGRAL_TYPE_P (type
))
15992 else if (POINTER_TYPE_P (type
))
15994 else if (TYPE_PTR_TO_MEMBER_P (type
))
15996 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
15998 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
16001 if (complain
& tf_error
)
16002 error ("%q#T is not a valid type for a template constant parameter", type
);
16006 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16007 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16010 dependent_type_p_r (tree type
)
16016 A type is dependent if it is:
16018 -- a template parameter. Template template parameters are types
16019 for us (since TYPE_P holds true for them) so we handle
16021 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
16022 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
16024 /* -- a qualified-id with a nested-name-specifier which contains a
16025 class-name that names a dependent type or whose unqualified-id
16026 names a dependent type. */
16027 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16029 /* -- a cv-qualified type where the cv-unqualified type is
16031 type
= TYPE_MAIN_VARIANT (type
);
16032 /* -- a compound type constructed from any dependent type. */
16033 if (TYPE_PTR_TO_MEMBER_P (type
))
16034 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
16035 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16037 else if (TREE_CODE (type
) == POINTER_TYPE
16038 || TREE_CODE (type
) == REFERENCE_TYPE
)
16039 return dependent_type_p (TREE_TYPE (type
));
16040 else if (TREE_CODE (type
) == FUNCTION_TYPE
16041 || TREE_CODE (type
) == METHOD_TYPE
)
16045 if (dependent_type_p (TREE_TYPE (type
)))
16047 for (arg_type
= TYPE_ARG_TYPES (type
);
16049 arg_type
= TREE_CHAIN (arg_type
))
16050 if (dependent_type_p (TREE_VALUE (arg_type
)))
16054 /* -- an array type constructed from any dependent type or whose
16055 size is specified by a constant expression that is
16056 value-dependent. */
16057 if (TREE_CODE (type
) == ARRAY_TYPE
)
16059 if (TYPE_DOMAIN (type
)
16060 && dependent_type_p (TYPE_DOMAIN (type
)))
16062 return dependent_type_p (TREE_TYPE (type
));
16064 else if (TREE_CODE (type
) == INTEGER_TYPE
16065 && !TREE_CONSTANT (TYPE_MAX_VALUE (type
)))
16067 /* If this is the TYPE_DOMAIN of an array type, consider it
16069 return (value_dependent_expression_p (TYPE_MAX_VALUE (type
))
16070 || type_dependent_expression_p (TYPE_MAX_VALUE (type
)));
16073 /* -- a template-id in which either the template name is a template
16075 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16077 /* ... or any of the template arguments is a dependent type or
16078 an expression that is type-dependent or value-dependent. */
16079 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
16080 && (any_dependent_template_arguments_p
16081 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
16084 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16085 argument of the `typeof' expression is not type-dependent, then
16086 it should already been have resolved. */
16087 if (TREE_CODE (type
) == TYPEOF_TYPE
16088 || TREE_CODE (type
) == DECLTYPE_TYPE
)
16091 /* A template argument pack is dependent if any of its packed
16093 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
16095 tree args
= ARGUMENT_PACK_ARGS (type
);
16096 int i
, len
= TREE_VEC_LENGTH (args
);
16097 for (i
= 0; i
< len
; ++i
)
16098 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
16102 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16103 be template parameters. */
16104 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
16107 /* The standard does not specifically mention types that are local
16108 to template functions or local classes, but they should be
16109 considered dependent too. For example:
16111 template <int I> void f() {
16116 The size of `E' cannot be known until the value of `I' has been
16117 determined. Therefore, `E' must be considered dependent. */
16118 scope
= TYPE_CONTEXT (type
);
16119 if (scope
&& TYPE_P (scope
))
16120 return dependent_type_p (scope
);
16121 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
16122 return type_dependent_expression_p (scope
);
16124 /* Other types are non-dependent. */
16128 /* Returns TRUE if TYPE is dependent, in the sense of
16129 [temp.dep.type]. */
16132 dependent_type_p (tree type
)
16134 /* If there are no template parameters in scope, then there can't be
16135 any dependent types. */
16136 if (!processing_template_decl
)
16138 /* If we are not processing a template, then nobody should be
16139 providing us with a dependent type. */
16141 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
16145 /* If the type is NULL, we have not computed a type for the entity
16146 in question; in that case, the type is dependent. */
16150 /* Erroneous types can be considered non-dependent. */
16151 if (type
== error_mark_node
)
16154 /* If we have not already computed the appropriate value for TYPE,
16156 if (!TYPE_DEPENDENT_P_VALID (type
))
16158 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
16159 TYPE_DEPENDENT_P_VALID (type
) = 1;
16162 return TYPE_DEPENDENT_P (type
);
16165 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16166 lookup. In other words, a dependent type that is not the current
16170 dependent_scope_p (tree scope
)
16172 return dependent_type_p (scope
) && !currently_open_class (scope
);
16175 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16178 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
16183 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
16185 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
16188 scope
= TREE_OPERAND (expression
, 0);
16189 name
= TREE_OPERAND (expression
, 1);
16193 An id-expression is type-dependent if it contains a
16194 nested-name-specifier that contains a class-name that names a
16196 /* The suggested resolution to Core Issue 224 implies that if the
16197 qualifying type is the current class, then we must peek
16200 && currently_open_class (scope
)
16201 && !criterion (name
))
16203 if (dependent_type_p (scope
))
16209 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16210 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16214 value_dependent_expression_p (tree expression
)
16216 if (!processing_template_decl
)
16219 /* A name declared with a dependent type. */
16220 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
16223 switch (TREE_CODE (expression
))
16225 case IDENTIFIER_NODE
:
16226 /* A name that has not been looked up -- must be dependent. */
16229 case TEMPLATE_PARM_INDEX
:
16230 /* A non-type template parm. */
16234 /* A non-type template parm. */
16235 if (DECL_TEMPLATE_PARM_P (expression
))
16237 return value_dependent_expression_p (DECL_INITIAL (expression
));
16240 /* A constant with integral or enumeration type and is initialized
16241 with an expression that is value-dependent. */
16242 if (DECL_INITIAL (expression
)
16243 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
16244 && value_dependent_expression_p (DECL_INITIAL (expression
)))
16248 case DYNAMIC_CAST_EXPR
:
16249 case STATIC_CAST_EXPR
:
16250 case CONST_CAST_EXPR
:
16251 case REINTERPRET_CAST_EXPR
:
16253 /* These expressions are value-dependent if the type to which
16254 the cast occurs is dependent or the expression being casted
16255 is value-dependent. */
16257 tree type
= TREE_TYPE (expression
);
16259 if (dependent_type_p (type
))
16262 /* A functional cast has a list of operands. */
16263 expression
= TREE_OPERAND (expression
, 0);
16266 /* If there are no operands, it must be an expression such
16267 as "int()". This should not happen for aggregate types
16268 because it would form non-constant expressions. */
16269 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
16274 if (TREE_CODE (expression
) == TREE_LIST
)
16275 return any_value_dependent_elements_p (expression
);
16277 return value_dependent_expression_p (expression
);
16282 /* A `sizeof' expression is value-dependent if the operand is
16283 type-dependent or is a pack expansion. */
16284 expression
= TREE_OPERAND (expression
, 0);
16285 if (PACK_EXPANSION_P (expression
))
16287 else if (TYPE_P (expression
))
16288 return dependent_type_p (expression
);
16289 return type_dependent_expression_p (expression
);
16292 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
16294 case COMPONENT_REF
:
16295 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
16296 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
16299 /* A CALL_EXPR may appear in a constant expression if it is a
16300 call to a builtin function, e.g., __builtin_constant_p. All
16301 such calls are value-dependent. */
16304 case NONTYPE_ARGUMENT_PACK
:
16305 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16306 is value-dependent. */
16308 tree values
= ARGUMENT_PACK_ARGS (expression
);
16309 int i
, len
= TREE_VEC_LENGTH (values
);
16311 for (i
= 0; i
< len
; ++i
)
16312 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
16320 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
16321 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
16322 || (type2
? dependent_type_p (type2
) : false));
16326 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
16327 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
16330 /* A constant expression is value-dependent if any subexpression is
16331 value-dependent. */
16332 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
16334 case tcc_reference
:
16336 return (value_dependent_expression_p
16337 (TREE_OPERAND (expression
, 0)));
16339 case tcc_comparison
:
16341 return ((value_dependent_expression_p
16342 (TREE_OPERAND (expression
, 0)))
16343 || (value_dependent_expression_p
16344 (TREE_OPERAND (expression
, 1))));
16346 case tcc_expression
:
16350 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
16351 /* In some cases, some of the operands may be missing.
16352 (For example, in the case of PREDECREMENT_EXPR, the
16353 amount to increment by may be missing.) That doesn't
16354 make the expression dependent. */
16355 if (TREE_OPERAND (expression
, i
)
16356 && (value_dependent_expression_p
16357 (TREE_OPERAND (expression
, i
))))
16367 /* The expression is not value-dependent. */
16371 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16372 [temp.dep.expr]. */
16375 type_dependent_expression_p (tree expression
)
16377 if (!processing_template_decl
)
16380 if (expression
== error_mark_node
)
16383 /* An unresolved name is always dependent. */
16384 if (TREE_CODE (expression
) == IDENTIFIER_NODE
16385 || TREE_CODE (expression
) == USING_DECL
)
16388 /* Some expression forms are never type-dependent. */
16389 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
16390 || TREE_CODE (expression
) == SIZEOF_EXPR
16391 || TREE_CODE (expression
) == ALIGNOF_EXPR
16392 || TREE_CODE (expression
) == TRAIT_EXPR
16393 || TREE_CODE (expression
) == TYPEID_EXPR
16394 || TREE_CODE (expression
) == DELETE_EXPR
16395 || TREE_CODE (expression
) == VEC_DELETE_EXPR
16396 || TREE_CODE (expression
) == THROW_EXPR
)
16399 /* The types of these expressions depends only on the type to which
16400 the cast occurs. */
16401 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
16402 || TREE_CODE (expression
) == STATIC_CAST_EXPR
16403 || TREE_CODE (expression
) == CONST_CAST_EXPR
16404 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
16405 || TREE_CODE (expression
) == CAST_EXPR
)
16406 return dependent_type_p (TREE_TYPE (expression
));
16408 /* The types of these expressions depends only on the type created
16409 by the expression. */
16410 if (TREE_CODE (expression
) == NEW_EXPR
16411 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
16413 /* For NEW_EXPR tree nodes created inside a template, either
16414 the object type itself or a TREE_LIST may appear as the
16416 tree type
= TREE_OPERAND (expression
, 1);
16417 if (TREE_CODE (type
) == TREE_LIST
)
16418 /* This is an array type. We need to check array dimensions
16420 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
16421 || value_dependent_expression_p
16422 (TREE_OPERAND (TREE_VALUE (type
), 1));
16424 return dependent_type_p (type
);
16427 if (TREE_CODE (expression
) == SCOPE_REF
16428 && dependent_scope_ref_p (expression
,
16429 type_dependent_expression_p
))
16432 if (TREE_CODE (expression
) == FUNCTION_DECL
16433 && DECL_LANG_SPECIFIC (expression
)
16434 && DECL_TEMPLATE_INFO (expression
)
16435 && (any_dependent_template_arguments_p
16436 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
16439 if (TREE_CODE (expression
) == TEMPLATE_DECL
16440 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
16443 if (TREE_CODE (expression
) == STMT_EXPR
)
16444 expression
= stmt_expr_value_expr (expression
);
16446 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
16451 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
16453 if (type_dependent_expression_p (elt
))
16459 if (TREE_TYPE (expression
) == unknown_type_node
)
16461 if (TREE_CODE (expression
) == ADDR_EXPR
)
16462 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
16463 if (TREE_CODE (expression
) == COMPONENT_REF
16464 || TREE_CODE (expression
) == OFFSET_REF
)
16466 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
16468 expression
= TREE_OPERAND (expression
, 1);
16469 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
16472 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
16473 if (TREE_CODE (expression
) == SCOPE_REF
)
16476 if (TREE_CODE (expression
) == BASELINK
)
16477 expression
= BASELINK_FUNCTIONS (expression
);
16479 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
16481 if (any_dependent_template_arguments_p
16482 (TREE_OPERAND (expression
, 1)))
16484 expression
= TREE_OPERAND (expression
, 0);
16486 gcc_assert (TREE_CODE (expression
) == OVERLOAD
16487 || TREE_CODE (expression
) == FUNCTION_DECL
);
16491 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
16493 expression
= OVL_NEXT (expression
);
16498 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
16500 return (dependent_type_p (TREE_TYPE (expression
)));
16503 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
16504 contains a type-dependent expression. */
16507 any_type_dependent_arguments_p (const_tree args
)
16511 tree arg
= TREE_VALUE (args
);
16513 if (type_dependent_expression_p (arg
))
16515 args
= TREE_CHAIN (args
);
16520 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
16521 expressions) contains any value-dependent expressions. */
16524 any_value_dependent_elements_p (const_tree list
)
16526 for (; list
; list
= TREE_CHAIN (list
))
16527 if (value_dependent_expression_p (TREE_VALUE (list
)))
16533 /* Returns TRUE if the ARG (a template argument) is dependent. */
16536 dependent_template_arg_p (tree arg
)
16538 if (!processing_template_decl
)
16541 if (TREE_CODE (arg
) == TEMPLATE_DECL
16542 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
16543 return dependent_template_p (arg
);
16544 else if (ARGUMENT_PACK_P (arg
))
16546 tree args
= ARGUMENT_PACK_ARGS (arg
);
16547 int i
, len
= TREE_VEC_LENGTH (args
);
16548 for (i
= 0; i
< len
; ++i
)
16550 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
16556 else if (TYPE_P (arg
))
16557 return dependent_type_p (arg
);
16559 return (type_dependent_expression_p (arg
)
16560 || value_dependent_expression_p (arg
));
16563 /* Returns true if ARGS (a collection of template arguments) contains
16564 any types that require structural equality testing. */
16567 any_template_arguments_need_structural_equality_p (tree args
)
16574 if (args
== error_mark_node
)
16577 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
16579 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
16580 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
16582 tree arg
= TREE_VEC_ELT (level
, j
);
16583 tree packed_args
= NULL_TREE
;
16586 if (ARGUMENT_PACK_P (arg
))
16588 /* Look inside the argument pack. */
16589 packed_args
= ARGUMENT_PACK_ARGS (arg
);
16590 len
= TREE_VEC_LENGTH (packed_args
);
16593 for (k
= 0; k
< len
; ++k
)
16596 arg
= TREE_VEC_ELT (packed_args
, k
);
16598 if (error_operand_p (arg
))
16600 else if (TREE_CODE (arg
) == TEMPLATE_DECL
16601 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
16603 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
16605 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
16606 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
16615 /* Returns true if ARGS (a collection of template arguments) contains
16616 any dependent arguments. */
16619 any_dependent_template_arguments_p (const_tree args
)
16626 if (args
== error_mark_node
)
16629 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
16631 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
16632 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
16633 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
16640 /* Returns TRUE if the template TMPL is dependent. */
16643 dependent_template_p (tree tmpl
)
16645 if (TREE_CODE (tmpl
) == OVERLOAD
)
16649 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
16651 tmpl
= OVL_CHAIN (tmpl
);
16656 /* Template template parameters are dependent. */
16657 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
16658 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
16660 /* So are names that have not been looked up. */
16661 if (TREE_CODE (tmpl
) == SCOPE_REF
16662 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
16664 /* So are member templates of dependent classes. */
16665 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
16666 return dependent_type_p (DECL_CONTEXT (tmpl
));
16670 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
16673 dependent_template_id_p (tree tmpl
, tree args
)
16675 return (dependent_template_p (tmpl
)
16676 || any_dependent_template_arguments_p (args
));
16679 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
16683 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
16687 if (!processing_template_decl
)
16690 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
16692 tree decl
= TREE_VEC_ELT (declv
, i
);
16693 tree init
= TREE_VEC_ELT (initv
, i
);
16694 tree cond
= TREE_VEC_ELT (condv
, i
);
16695 tree incr
= TREE_VEC_ELT (incrv
, i
);
16697 if (type_dependent_expression_p (decl
))
16700 if (init
&& type_dependent_expression_p (init
))
16703 if (type_dependent_expression_p (cond
))
16706 if (COMPARISON_CLASS_P (cond
)
16707 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
16708 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
16711 if (TREE_CODE (incr
) == MODOP_EXPR
)
16713 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
16714 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
16717 else if (type_dependent_expression_p (incr
))
16719 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
16721 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
16723 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
16725 tree t
= TREE_OPERAND (incr
, 1);
16726 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
16727 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
16736 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
16737 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
16738 no such TYPE can be found. Note that this function peers inside
16739 uninstantiated templates and therefore should be used only in
16740 extremely limited situations. ONLY_CURRENT_P restricts this
16741 peering to the currently open classes hierarchy (which is required
16742 when comparing types). */
16745 resolve_typename_type (tree type
, bool only_current_p
)
16754 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
16756 scope
= TYPE_CONTEXT (type
);
16757 name
= TYPE_IDENTIFIER (type
);
16759 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
16760 it first before we can figure out what NAME refers to. */
16761 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
16762 scope
= resolve_typename_type (scope
, only_current_p
);
16763 /* If we don't know what SCOPE refers to, then we cannot resolve the
16765 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
16767 /* If the SCOPE is a template type parameter, we have no way of
16768 resolving the name. */
16769 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
16771 /* If the SCOPE is not the current instantiation, there's no reason
16772 to look inside it. */
16773 if (only_current_p
&& !currently_open_class (scope
))
16775 /* If SCOPE isn't the template itself, it will not have a valid
16776 TYPE_FIELDS list. */
16777 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
16778 /* scope is either the template itself or a compatible instantiation
16779 like X<T>, so look up the name in the original template. */
16780 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
16782 /* scope is a partial instantiation, so we can't do the lookup or we
16783 will lose the template arguments. */
16785 /* Enter the SCOPE so that name lookup will be resolved as if we
16786 were in the class definition. In particular, SCOPE will no
16787 longer be considered a dependent type. */
16788 pushed_scope
= push_scope (scope
);
16789 /* Look up the declaration. */
16790 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
16792 result
= NULL_TREE
;
16794 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
16795 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
16798 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
16799 && TREE_CODE (decl
) == TYPE_DECL
)
16801 result
= TREE_TYPE (decl
);
16802 if (result
== error_mark_node
)
16803 result
= NULL_TREE
;
16805 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
16806 && DECL_CLASS_TEMPLATE_P (decl
))
16810 /* Obtain the template and the arguments. */
16811 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
16812 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
16813 /* Instantiate the template. */
16814 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
16815 /*entering_scope=*/0,
16816 tf_error
| tf_user
);
16817 if (result
== error_mark_node
)
16818 result
= NULL_TREE
;
16821 /* Leave the SCOPE. */
16823 pop_scope (pushed_scope
);
16825 /* If we failed to resolve it, return the original typename. */
16829 /* If lookup found a typename type, resolve that too. */
16830 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
16832 /* Ill-formed programs can cause infinite recursion here, so we
16833 must catch that. */
16834 TYPENAME_IS_RESOLVING_P (type
) = 1;
16835 result
= resolve_typename_type (result
, only_current_p
);
16836 TYPENAME_IS_RESOLVING_P (type
) = 0;
16839 /* Qualify the resulting type. */
16840 quals
= cp_type_quals (type
);
16842 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
16847 /* EXPR is an expression which is not type-dependent. Return a proxy
16848 for EXPR that can be used to compute the types of larger
16849 expressions containing EXPR. */
16852 build_non_dependent_expr (tree expr
)
16856 /* Preserve null pointer constants so that the type of things like
16857 "p == 0" where "p" is a pointer can be determined. */
16858 if (null_ptr_cst_p (expr
))
16860 /* Preserve OVERLOADs; the functions must be available to resolve
16863 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
16864 inner_expr
= stmt_expr_value_expr (inner_expr
);
16865 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
16866 inner_expr
= TREE_OPERAND (inner_expr
, 0);
16867 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
16868 inner_expr
= TREE_OPERAND (inner_expr
, 1);
16869 if (is_overloaded_fn (inner_expr
)
16870 || TREE_CODE (inner_expr
) == OFFSET_REF
)
16872 /* There is no need to return a proxy for a variable. */
16873 if (TREE_CODE (expr
) == VAR_DECL
)
16875 /* Preserve string constants; conversions from string constants to
16876 "char *" are allowed, even though normally a "const char *"
16877 cannot be used to initialize a "char *". */
16878 if (TREE_CODE (expr
) == STRING_CST
)
16880 /* Preserve arithmetic constants, as an optimization -- there is no
16881 reason to create a new node. */
16882 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
16884 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
16885 There is at least one place where we want to know that a
16886 particular expression is a throw-expression: when checking a ?:
16887 expression, there are special rules if the second or third
16888 argument is a throw-expression. */
16889 if (TREE_CODE (expr
) == THROW_EXPR
)
16892 if (TREE_CODE (expr
) == COND_EXPR
)
16893 return build3 (COND_EXPR
,
16895 TREE_OPERAND (expr
, 0),
16896 (TREE_OPERAND (expr
, 1)
16897 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
16898 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
16899 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
16900 if (TREE_CODE (expr
) == COMPOUND_EXPR
16901 && !COMPOUND_EXPR_OVERLOADED (expr
))
16902 return build2 (COMPOUND_EXPR
,
16904 TREE_OPERAND (expr
, 0),
16905 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
16907 /* If the type is unknown, it can't really be non-dependent */
16908 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
16910 /* Otherwise, build a NON_DEPENDENT_EXPR.
16912 REFERENCE_TYPEs are not stripped for expressions in templates
16913 because doing so would play havoc with mangling. Consider, for
16916 template <typename T> void f<T& g>() { g(); }
16918 In the body of "f", the expression for "g" will have
16919 REFERENCE_TYPE, even though the standard says that it should
16920 not. The reason is that we must preserve the syntactic form of
16921 the expression so that mangling (say) "f<g>" inside the body of
16922 "f" works out correctly. Therefore, the REFERENCE_TYPE is
16924 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
16927 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
16928 Return a new TREE_LIST with the various arguments replaced with
16929 equivalent non-dependent expressions. */
16932 build_non_dependent_args (tree args
)
16937 new_args
= NULL_TREE
;
16938 for (a
= args
; a
; a
= TREE_CHAIN (a
))
16939 new_args
= tree_cons (NULL_TREE
,
16940 build_non_dependent_expr (TREE_VALUE (a
)),
16942 return nreverse (new_args
);
16945 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
16946 with a level one deeper than the actual template parms. */
16953 /* ??? Is it worth caching this for multiple autos at the same level? */
16954 au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
16955 TYPE_NAME (au
) = build_decl (TYPE_DECL
, get_identifier ("auto"), au
);
16956 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
16957 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
16958 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
16959 TYPE_NAME (au
), NULL_TREE
);
16960 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
16961 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
16962 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
16967 /* Replace auto in TYPE with std::initializer_list<auto>. */
16970 listify_autos (tree type
, tree auto_node
)
16972 tree std_init_list
= namespace_binding
16973 (get_identifier ("initializer_list"), std_node
);
16976 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
16978 error ("deducing auto from brace-enclosed initializer list requires "
16979 "#include <initializer_list>");
16980 return error_mark_node
;
16982 argvec
= make_tree_vec (1);
16983 TREE_VEC_ELT (argvec
, 0) = auto_node
;
16984 init_auto
= lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
16985 NULL_TREE
, 0, tf_warning_or_error
);
16987 TREE_VEC_ELT (argvec
, 0) = init_auto
;
16988 if (processing_template_decl
)
16989 argvec
= add_to_template_args (current_template_args (), argvec
);
16990 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
16993 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
16994 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
16997 do_auto_deduction (tree type
, tree init
, tree auto_node
)
16999 tree parms
, args
, tparms
, targs
;
17002 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17003 with either a new invented type template parameter U or, if the
17004 initializer is a braced-init-list (8.5.4), with
17005 std::initializer_list<U>. */
17006 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
17007 type
= listify_autos (type
, auto_node
);
17009 parms
= build_tree_list (NULL_TREE
, type
);
17010 args
= build_tree_list (NULL_TREE
, init
);
17011 tparms
= make_tree_vec (1);
17012 targs
= make_tree_vec (1);
17013 TREE_VEC_ELT (tparms
, 0)
17014 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
17015 val
= type_unification_real (tparms
, targs
, parms
, args
, 0,
17016 DEDUCE_CALL
, LOOKUP_NORMAL
);
17019 error ("unable to deduce %qT from %qE", type
, init
);
17020 return error_mark_node
;
17023 if (processing_template_decl
)
17024 targs
= add_to_template_args (current_template_args (), targs
);
17025 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
17028 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17032 splice_late_return_type (tree type
, tree late_return_type
)
17036 if (late_return_type
== NULL_TREE
)
17038 argvec
= make_tree_vec (1);
17039 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
17040 if (processing_template_decl
)
17041 argvec
= add_to_template_args (current_template_args (), argvec
);
17042 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
17045 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17048 is_auto (const_tree type
)
17050 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17051 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
17057 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17058 appear as a type-specifier for the declaration in question, we don't
17059 have to look through the whole type. */
17062 type_uses_auto (tree type
)
17064 enum tree_code code
;
17065 if (is_auto (type
))
17068 code
= TREE_CODE (type
);
17070 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
17071 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
17072 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
17073 return type_uses_auto (TREE_TYPE (type
));
17075 if (TYPE_PTRMEMFUNC_P (type
))
17076 return type_uses_auto (TREE_TYPE (TREE_TYPE
17077 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
17082 #include "gt-cp-pt.h"