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 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
30 #include "coretypes.h"
34 #include "pointer-set.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct pending_template
GTY (()) {
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations
;
84 /* Contains canonical template parameter types. The vector is indexed by
85 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
86 TREE_LIST, whose TREE_VALUEs contain the canonical template
87 parameters of various types and levels. */
88 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
90 #define UNIFY_ALLOW_NONE 0
91 #define UNIFY_ALLOW_MORE_CV_QUAL 1
92 #define UNIFY_ALLOW_LESS_CV_QUAL 2
93 #define UNIFY_ALLOW_DERIVED 4
94 #define UNIFY_ALLOW_INTEGER 8
95 #define UNIFY_ALLOW_OUTER_LEVEL 16
96 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
97 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
99 static void push_access_scope (tree
);
100 static void pop_access_scope (tree
);
101 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
102 unification_kind_t
, int);
103 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
104 unification_kind_t
, int, bool);
105 static int unify (tree
, tree
, tree
, tree
, int);
106 static void add_pending_template (tree
);
107 static int push_tinst_level (tree
);
108 static void pop_tinst_level (void);
109 static tree
reopen_tinst_level (struct tinst_level
*);
110 static tree
tsubst_initializer_list (tree
, tree
);
111 static tree
get_class_bindings (tree
, tree
, tree
);
112 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
114 static void tsubst_enum (tree
, tree
, tree
);
115 static tree
add_to_template_args (tree
, tree
);
116 static tree
add_outermost_template_args (tree
, tree
);
117 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
118 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
120 static int type_unification_real (tree
, tree
, tree
, tree
,
121 int, unification_kind_t
, int);
122 static void note_template_header (int);
123 static tree
convert_nontype_argument_function (tree
, tree
);
124 static tree
convert_nontype_argument (tree
, tree
);
125 static tree
convert_template_argument (tree
, tree
, tree
,
126 tsubst_flags_t
, int, tree
);
127 static int for_each_template_parm (tree
, tree_fn_t
, void*,
128 struct pointer_set_t
*);
129 static tree
expand_template_argument_pack (tree
);
130 static tree
build_template_parm_index (int, int, int, tree
, tree
);
131 static bool inline_needs_template_parms (tree
);
132 static void push_inline_template_parms_recursive (tree
, int);
133 static tree
retrieve_local_specialization (tree
);
134 static void register_local_specialization (tree
, tree
);
135 static tree
reduce_template_parm_level (tree
, tree
, int);
136 static int mark_template_parm (tree
, void *);
137 static int template_parm_this_level_p (tree
, void *);
138 static tree
tsubst_friend_function (tree
, tree
);
139 static tree
tsubst_friend_class (tree
, tree
);
140 static int can_complete_type_without_circularity (tree
);
141 static tree
get_bindings (tree
, tree
, tree
, bool);
142 static int template_decl_level (tree
);
143 static int check_cv_quals_for_unify (int, tree
, tree
);
144 static void template_parm_level_and_index (tree
, int*, int*);
145 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
146 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
147 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
148 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
149 static void regenerate_decl_from_template (tree
, tree
);
150 static tree
most_specialized_class (tree
, tree
);
151 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
152 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
153 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
154 static bool check_specialization_scope (void);
155 static tree
process_partial_specialization (tree
);
156 static void set_current_access_from_decl (tree
);
157 static tree
get_template_base (tree
, tree
, tree
, tree
);
158 static tree
try_class_unification (tree
, tree
, tree
, tree
);
159 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
161 static int template_args_equal (tree
, tree
);
162 static void tsubst_default_arguments (tree
);
163 static tree
for_each_template_parm_r (tree
*, int *, void *);
164 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
165 static void copy_default_args_to_explicit_spec (tree
);
166 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
167 static int eq_local_specializations (const void *, const void *);
168 static bool dependent_template_arg_p (tree
);
169 static bool any_template_arguments_need_structural_equality_p (tree
);
170 static bool dependent_type_p_r (tree
);
171 static tree
tsubst (tree
, tree
, tsubst_flags_t
, tree
);
172 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
173 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
174 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
176 /* Make the current scope suitable for access checking when we are
177 processing T. T can be FUNCTION_DECL for instantiated function
178 template, or VAR_DECL for static member variable (need by
179 instantiate_decl). */
182 push_access_scope (tree t
)
184 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
185 || TREE_CODE (t
) == VAR_DECL
);
187 if (DECL_FRIEND_CONTEXT (t
))
188 push_nested_class (DECL_FRIEND_CONTEXT (t
));
189 else if (DECL_CLASS_SCOPE_P (t
))
190 push_nested_class (DECL_CONTEXT (t
));
192 push_to_top_level ();
194 if (TREE_CODE (t
) == FUNCTION_DECL
)
196 saved_access_scope
= tree_cons
197 (NULL_TREE
, current_function_decl
, saved_access_scope
);
198 current_function_decl
= t
;
202 /* Restore the scope set up by push_access_scope. T is the node we
206 pop_access_scope (tree t
)
208 if (TREE_CODE (t
) == FUNCTION_DECL
)
210 current_function_decl
= TREE_VALUE (saved_access_scope
);
211 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
214 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
217 pop_from_top_level ();
220 /* Do any processing required when DECL (a member template
221 declaration) is finished. Returns the TEMPLATE_DECL corresponding
222 to DECL, unless it is a specialization, in which case the DECL
223 itself is returned. */
226 finish_member_template_decl (tree decl
)
228 if (decl
== error_mark_node
)
229 return error_mark_node
;
231 gcc_assert (DECL_P (decl
));
233 if (TREE_CODE (decl
) == TYPE_DECL
)
237 type
= TREE_TYPE (decl
);
238 if (IS_AGGR_TYPE (type
)
239 && CLASSTYPE_TEMPLATE_INFO (type
)
240 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
242 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
243 check_member_template (tmpl
);
248 else if (TREE_CODE (decl
) == FIELD_DECL
)
249 error ("data member %qD cannot be a member template", decl
);
250 else if (DECL_TEMPLATE_INFO (decl
))
252 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
254 check_member_template (DECL_TI_TEMPLATE (decl
));
255 return DECL_TI_TEMPLATE (decl
);
261 error ("invalid member template declaration %qD", decl
);
263 return error_mark_node
;
266 /* Returns the template nesting level of the indicated class TYPE.
276 A<T>::B<U> has depth two, while A<T> has depth one.
277 Both A<T>::B<int> and A<int>::B<U> have depth one, if
278 they are instantiations, not specializations.
280 This function is guaranteed to return 0 if passed NULL_TREE so
281 that, for example, `template_class_depth (current_class_type)' is
285 template_class_depth (tree type
)
290 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
291 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
292 ? CP_DECL_CONTEXT (type
) : TYPE_CONTEXT (type
))
294 if (TREE_CODE (type
) != FUNCTION_DECL
)
296 if (CLASSTYPE_TEMPLATE_INFO (type
)
297 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
298 && uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
303 if (DECL_TEMPLATE_INFO (type
)
304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
305 && uses_template_parms (DECL_TI_ARGS (type
)))
313 /* Subroutine of maybe_begin_member_template_processing.
314 Returns true if processing DECL needs us to push template parms. */
317 inline_needs_template_parms (tree decl
)
319 if (! DECL_TEMPLATE_INFO (decl
))
322 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
323 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
326 /* Subroutine of maybe_begin_member_template_processing.
327 Push the template parms in PARMS, starting from LEVELS steps into the
328 chain, and ending at the beginning, since template parms are listed
332 push_inline_template_parms_recursive (tree parmlist
, int levels
)
334 tree parms
= TREE_VALUE (parmlist
);
338 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
340 ++processing_template_decl
;
341 current_template_parms
342 = tree_cons (size_int (processing_template_decl
),
343 parms
, current_template_parms
);
344 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
346 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
348 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
350 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
352 if (parm
== error_mark_node
)
355 gcc_assert (DECL_P (parm
));
357 switch (TREE_CODE (parm
))
366 /* Make a CONST_DECL as is done in process_template_parm.
367 It is ugly that we recreate this here; the original
368 version built in process_template_parm is no longer
370 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
372 DECL_ARTIFICIAL (decl
) = 1;
373 TREE_CONSTANT (decl
) = 1;
374 TREE_INVARIANT (decl
) = 1;
375 TREE_READONLY (decl
) = 1;
376 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
377 SET_DECL_TEMPLATE_PARM_P (decl
);
388 /* Restore the template parameter context for a member template or
389 a friend template defined in a class definition. */
392 maybe_begin_member_template_processing (tree decl
)
397 if (inline_needs_template_parms (decl
))
399 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
400 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
402 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
405 parms
= TREE_CHAIN (parms
);
408 push_inline_template_parms_recursive (parms
, levels
);
411 /* Remember how many levels of template parameters we pushed so that
412 we can pop them later. */
413 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
416 /* Undo the effects of maybe_begin_member_template_processing. */
419 maybe_end_member_template_processing (void)
424 if (VEC_length (int, inline_parm_levels
) == 0)
427 last
= VEC_pop (int, inline_parm_levels
);
428 for (i
= 0; i
< last
; ++i
)
430 --processing_template_decl
;
431 current_template_parms
= TREE_CHAIN (current_template_parms
);
436 /* Return a new template argument vector which contains all of ARGS,
437 but has as its innermost set of arguments the EXTRA_ARGS. */
440 add_to_template_args (tree args
, tree extra_args
)
447 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
448 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
450 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
451 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
453 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
454 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
459 /* Like add_to_template_args, but only the outermost ARGS are added to
460 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
461 (EXTRA_ARGS) levels are added. This function is used to combine
462 the template arguments from a partial instantiation with the
463 template arguments used to attain the full instantiation from the
464 partial instantiation. */
467 add_outermost_template_args (tree args
, tree extra_args
)
471 /* If there are more levels of EXTRA_ARGS than there are ARGS,
472 something very fishy is going on. */
473 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
475 /* If *all* the new arguments will be the EXTRA_ARGS, just return
477 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
480 /* For the moment, we make ARGS look like it contains fewer levels. */
481 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
483 new_args
= add_to_template_args (args
, extra_args
);
485 /* Now, we restore ARGS to its full dimensions. */
486 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
491 /* Return the N levels of innermost template arguments from the ARGS. */
494 get_innermost_template_args (tree args
, int n
)
502 /* If N is 1, just return the innermost set of template arguments. */
504 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
506 /* If we're not removing anything, just return the arguments we were
508 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
509 gcc_assert (extra_levels
>= 0);
510 if (extra_levels
== 0)
513 /* Make a new set of arguments, not containing the outer arguments. */
514 new_args
= make_tree_vec (n
);
515 for (i
= 1; i
<= n
; ++i
)
516 SET_TMPL_ARGS_LEVEL (new_args
, i
,
517 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
522 /* The inverse of get_innermost_template_args: Return all but the innermost
523 EXTRA_LEVELS levels of template arguments from the ARGS. */
526 strip_innermost_template_args (tree args
, int extra_levels
)
529 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
534 /* If N is 1, just return the outermost set of template arguments. */
536 return TMPL_ARGS_LEVEL (args
, 1);
538 /* If we're not removing anything, just return the arguments we were
540 gcc_assert (extra_levels
>= 0);
541 if (extra_levels
== 0)
544 /* Make a new set of arguments, not containing the inner arguments. */
545 new_args
= make_tree_vec (n
);
546 for (i
= 1; i
<= n
; ++i
)
547 SET_TMPL_ARGS_LEVEL (new_args
, i
,
548 TMPL_ARGS_LEVEL (args
, i
));
553 /* We've got a template header coming up; push to a new level for storing
557 begin_template_parm_list (void)
559 /* We use a non-tag-transparent scope here, which causes pushtag to
560 put tags in this scope, rather than in the enclosing class or
561 namespace scope. This is the right thing, since we want
562 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
563 global template class, push_template_decl handles putting the
564 TEMPLATE_DECL into top-level scope. For a nested template class,
567 template <class T> struct S1 {
568 template <class T> struct S2 {};
571 pushtag contains special code to call pushdecl_with_scope on the
572 TEMPLATE_DECL for S2. */
573 begin_scope (sk_template_parms
, NULL
);
574 ++processing_template_decl
;
575 ++processing_template_parmlist
;
576 note_template_header (0);
579 /* This routine is called when a specialization is declared. If it is
580 invalid to declare a specialization here, an error is reported and
581 false is returned, otherwise this routine will return true. */
584 check_specialization_scope (void)
586 tree scope
= current_scope ();
590 An explicit specialization shall be declared in the namespace of
591 which the template is a member, or, for member templates, in the
592 namespace of which the enclosing class or enclosing class
593 template is a member. An explicit specialization of a member
594 function, member class or static data member of a class template
595 shall be declared in the namespace of which the class template
597 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
599 error ("explicit specialization in non-namespace scope %qD", scope
);
605 In an explicit specialization declaration for a member of a class
606 template or a member template that appears in namespace scope,
607 the member template and some of its enclosing class templates may
608 remain unspecialized, except that the declaration shall not
609 explicitly specialize a class member template if its enclosing
610 class templates are not explicitly specialized as well. */
611 if (current_template_parms
)
613 error ("enclosing class templates are not explicitly specialized");
620 /* We've just seen template <>. */
623 begin_specialization (void)
625 begin_scope (sk_template_spec
, NULL
);
626 note_template_header (1);
627 return check_specialization_scope ();
630 /* Called at then end of processing a declaration preceded by
634 end_specialization (void)
637 reset_specialization ();
640 /* Any template <>'s that we have seen thus far are not referring to a
641 function specialization. */
644 reset_specialization (void)
646 processing_specialization
= 0;
647 template_header_count
= 0;
650 /* We've just seen a template header. If SPECIALIZATION is nonzero,
651 it was of the form template <>. */
654 note_template_header (int specialization
)
656 processing_specialization
= specialization
;
657 template_header_count
++;
660 /* We're beginning an explicit instantiation. */
663 begin_explicit_instantiation (void)
665 gcc_assert (!processing_explicit_instantiation
);
666 processing_explicit_instantiation
= true;
671 end_explicit_instantiation (void)
673 gcc_assert (processing_explicit_instantiation
);
674 processing_explicit_instantiation
= false;
677 /* An explicit specialization or partial specialization TMPL is being
678 declared. Check that the namespace in which the specialization is
679 occurring is permissible. Returns false iff it is invalid to
680 specialize TMPL in the current namespace. */
683 check_specialization_namespace (tree tmpl
)
685 tree tpl_ns
= decl_namespace_context (tmpl
);
689 An explicit specialization shall be declared in the namespace of
690 which the template is a member, or, for member templates, in the
691 namespace of which the enclosing class or enclosing class
692 template is a member. An explicit specialization of a member
693 function, member class or static data member of a class template
694 shall be declared in the namespace of which the class template is
696 if (is_associated_namespace (current_namespace
, tpl_ns
))
697 /* Same or super-using namespace. */
701 pedwarn ("specialization of %qD in different namespace", tmpl
);
702 pedwarn (" from definition of %q+#D", tmpl
);
707 /* SPEC is an explicit instantiation. Check that it is valid to
708 perform this explicit instantiation in the current namespace. */
711 check_explicit_instantiation_namespace (tree spec
)
715 /* DR 275: An explicit instantiation shall appear in an enclosing
716 namespace of its template. */
717 ns
= decl_namespace_context (spec
);
718 if (!is_ancestor (current_namespace
, ns
))
719 pedwarn ("explicit instantiation of %qD in namespace %qD "
720 "(which does not enclose namespace %qD)",
721 spec
, current_namespace
, ns
);
724 /* The TYPE is being declared. If it is a template type, that means it
725 is a partial specialization. Do appropriate error-checking. */
728 maybe_process_partial_specialization (tree type
)
732 if (type
== error_mark_node
)
733 return error_mark_node
;
735 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
737 error ("name of class shadows template template parameter %qD",
739 return error_mark_node
;
742 context
= TYPE_CONTEXT (type
);
744 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
746 /* This is for ordinary explicit specialization and partial
747 specialization of a template class such as:
749 template <> class C<int>;
753 template <class T> class C<T*>;
755 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
757 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
758 && !COMPLETE_TYPE_P (type
))
760 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
761 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
762 if (processing_template_decl
)
763 push_template_decl (TYPE_MAIN_DECL (type
));
765 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
766 error ("specialization of %qT after instantiation", type
);
768 else if (CLASS_TYPE_P (type
)
769 && !CLASSTYPE_USE_TEMPLATE (type
)
770 && CLASSTYPE_TEMPLATE_INFO (type
)
771 && context
&& CLASS_TYPE_P (context
)
772 && CLASSTYPE_TEMPLATE_INFO (context
))
774 /* This is for an explicit specialization of member class
775 template according to [temp.expl.spec/18]:
777 template <> template <class U> class C<int>::D;
779 The context `C<int>' must be an implicit instantiation.
780 Otherwise this is just a member class template declared
783 template <> class C<int> { template <class U> class D; };
784 template <> template <class U> class C<int>::D;
786 In the first case, `C<int>::D' is a specialization of `C<T>::D'
787 while in the second case, `C<int>::D' is a primary template
788 and `C<T>::D' may not exist. */
790 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
791 && !COMPLETE_TYPE_P (type
))
795 if (current_namespace
796 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
798 pedwarn ("specializing %q#T in different namespace", type
);
799 pedwarn (" from definition of %q+#D",
800 CLASSTYPE_TI_TEMPLATE (type
));
803 /* Check for invalid specialization after instantiation:
805 template <> template <> class C<int>::D<int>;
806 template <> template <class U> class C<int>::D; */
808 for (t
= DECL_TEMPLATE_INSTANTIATIONS
809 (most_general_template (CLASSTYPE_TI_TEMPLATE (type
)));
810 t
; t
= TREE_CHAIN (t
))
811 if (TREE_VALUE (t
) != type
812 && TYPE_CONTEXT (TREE_VALUE (t
)) == context
)
813 error ("specialization %qT after instantiation %qT",
814 type
, TREE_VALUE (t
));
816 /* Mark TYPE as a specialization. And as a result, we only
817 have one level of template argument for the innermost
819 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
820 CLASSTYPE_TI_ARGS (type
)
821 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
824 else if (processing_specialization
)
826 error ("explicit specialization of non-template %qT", type
);
827 return error_mark_node
;
833 /* Returns nonzero if we can optimize the retrieval of specializations
834 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
835 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
838 optimize_specialization_lookup_p (tree tmpl
)
840 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
841 && DECL_CLASS_SCOPE_P (tmpl
)
842 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
844 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
845 /* The optimized lookup depends on the fact that the
846 template arguments for the member function template apply
847 purely to the containing class, which is not true if the
848 containing class is an explicit or partial
850 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
851 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
852 && !DECL_CONV_FN_P (tmpl
)
853 /* It is possible to have a template that is not a member
854 template and is not a member of a template class:
856 template <typename T>
857 struct S { friend A::f(); };
859 Here, the friend function is a template, but the context does
860 not have template information. The optimized lookup relies
861 on having ARGS be the template arguments for both the class
862 and the function template. */
863 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
866 /* Retrieve the specialization (in the sense of [temp.spec] - a
867 specialization is either an instantiation or an explicit
868 specialization) of TMPL for the given template ARGS. If there is
869 no such specialization, return NULL_TREE. The ARGS are a vector of
870 arguments, or a vector of vectors of arguments, in the case of
871 templates with more than one level of parameters.
873 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
874 then we search for a partial specialization matching ARGS. This
875 parameter is ignored if TMPL is not a class template. */
878 retrieve_specialization (tree tmpl
, tree args
,
879 bool class_specializations_p
)
881 if (args
== error_mark_node
)
884 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
886 /* There should be as many levels of arguments as there are
887 levels of parameters. */
888 gcc_assert (TMPL_ARGS_DEPTH (args
)
889 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
891 if (optimize_specialization_lookup_p (tmpl
))
894 tree class_specialization
;
895 VEC(tree
,gc
) *methods
;
899 /* The template arguments actually apply to the containing
900 class. Find the class specialization with those
902 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
904 = retrieve_specialization (class_template
, args
,
905 /*class_specializations_p=*/false);
906 if (!class_specialization
)
908 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
909 for the specialization. */
910 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
913 /* Iterate through the methods with the indicated name, looking
914 for the one that has an instance of TMPL. */
915 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
916 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
918 tree fn
= OVL_CURRENT (fns
);
919 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
)
929 /* Class templates store their instantiations on the
930 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
931 DECL_TEMPLATE_SPECIALIZATIONS list. */
932 if (!class_specializations_p
933 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl
)) == TYPE_DECL
)
934 sp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
936 sp
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
938 /* Iterate through the list until we find a matching template. */
939 while (*sp
!= NULL_TREE
)
943 if (comp_template_args (TREE_PURPOSE (spec
), args
))
945 /* Use the move-to-front heuristic to speed up future
949 *sp
= TREE_CHAIN (*sp
);
950 TREE_CHAIN (spec
) = *head
;
953 return TREE_VALUE (spec
);
955 sp
= &TREE_CHAIN (spec
);
962 /* Like retrieve_specialization, but for local declarations. */
965 retrieve_local_specialization (tree tmpl
)
967 tree spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
968 htab_hash_pointer (tmpl
));
969 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
972 /* Returns nonzero iff DECL is a specialization of TMPL. */
975 is_specialization_of (tree decl
, tree tmpl
)
979 if (TREE_CODE (decl
) == FUNCTION_DECL
)
983 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
989 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
991 for (t
= TREE_TYPE (decl
);
993 t
= CLASSTYPE_USE_TEMPLATE (t
)
994 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
995 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1002 /* Returns nonzero iff DECL is a specialization of friend declaration
1003 FRIEND according to [temp.friend]. */
1006 is_specialization_of_friend (tree decl
, tree
friend)
1008 bool need_template
= true;
1011 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1012 || TREE_CODE (decl
) == TYPE_DECL
);
1014 /* For [temp.friend/6] when FRIEND is an ordinary member function
1015 of a template class, we want to check if DECL is a specialization
1017 if (TREE_CODE (friend) == FUNCTION_DECL
1018 && DECL_TEMPLATE_INFO (friend)
1019 && !DECL_USE_TEMPLATE (friend))
1021 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1022 friend = DECL_TI_TEMPLATE (friend);
1023 need_template
= false;
1025 else if (TREE_CODE (friend) == TEMPLATE_DECL
1026 && !PRIMARY_TEMPLATE_P (friend))
1027 need_template
= false;
1029 /* There is nothing to do if this is not a template friend. */
1030 if (TREE_CODE (friend) != TEMPLATE_DECL
)
1033 if (is_specialization_of (decl
, friend))
1037 A member of a class template may be declared to be a friend of a
1038 non-template class. In this case, the corresponding member of
1039 every specialization of the class template is a friend of the
1040 class granting friendship.
1042 For example, given a template friend declaration
1044 template <class T> friend void A<T>::f();
1046 the member function below is considered a friend
1048 template <> struct A<int> {
1052 For this type of template friend, TEMPLATE_DEPTH below will be
1053 nonzero. To determine if DECL is a friend of FRIEND, we first
1054 check if the enclosing class is a specialization of another. */
1056 template_depth
= template_class_depth (DECL_CONTEXT (friend));
1058 && DECL_CLASS_SCOPE_P (decl
)
1059 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1060 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1062 /* Next, we check the members themselves. In order to handle
1063 a few tricky cases, such as when FRIEND's are
1065 template <class T> friend void A<T>::g(T t);
1066 template <class T> template <T t> friend void A<T>::h();
1070 void A<int>::g(int);
1071 template <int> void A<int>::h();
1073 we need to figure out ARGS, the template arguments from
1074 the context of DECL. This is required for template substitution
1075 of `T' in the function parameter of `g' and template parameter
1076 of `h' in the above examples. Here ARGS corresponds to `int'. */
1078 tree context
= DECL_CONTEXT (decl
);
1079 tree args
= NULL_TREE
;
1080 int current_depth
= 0;
1082 while (current_depth
< template_depth
)
1084 if (CLASSTYPE_TEMPLATE_INFO (context
))
1086 if (current_depth
== 0)
1087 args
= TYPE_TI_ARGS (context
);
1089 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1092 context
= TYPE_CONTEXT (context
);
1095 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1100 tree friend_args_type
;
1101 tree decl_args_type
;
1103 /* Make sure that both DECL and FRIEND are templates or
1105 is_template
= DECL_TEMPLATE_INFO (decl
)
1106 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1107 if (need_template
^ is_template
)
1109 else if (is_template
)
1111 /* If both are templates, check template parameter list. */
1113 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1115 if (!comp_template_parms
1116 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1120 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1123 decl_type
= TREE_TYPE (decl
);
1125 friend_type
= tsubst_function_type (TREE_TYPE (friend), args
,
1126 tf_none
, NULL_TREE
);
1127 if (friend_type
== error_mark_node
)
1130 /* Check if return types match. */
1131 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1134 /* Check if function parameter types match, ignoring the
1135 `this' parameter. */
1136 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1137 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1139 friend_args_type
= TREE_CHAIN (friend_args_type
);
1140 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1141 decl_args_type
= TREE_CHAIN (decl_args_type
);
1143 return compparms (decl_args_type
, friend_args_type
);
1147 /* DECL is a TYPE_DECL */
1149 tree decl_type
= TREE_TYPE (decl
);
1151 /* Make sure that both DECL and FRIEND are templates or
1154 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1155 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1157 if (need_template
^ is_template
)
1159 else if (is_template
)
1162 /* If both are templates, check the name of the two
1163 TEMPLATE_DECL's first because is_friend didn't. */
1164 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1165 != DECL_NAME (friend))
1168 /* Now check template parameter list. */
1170 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1172 return comp_template_parms
1173 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1177 return (DECL_NAME (decl
)
1178 == DECL_NAME (friend));
1184 /* Register the specialization SPEC as a specialization of TMPL with
1185 the indicated ARGS. IS_FRIEND indicates whether the specialization
1186 is actually just a friend declaration. Returns SPEC, or an
1187 equivalent prior declaration, if available. */
1190 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
)
1194 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1196 if (TREE_CODE (spec
) == FUNCTION_DECL
1197 && uses_template_parms (DECL_TI_ARGS (spec
)))
1198 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1199 register it; we want the corresponding TEMPLATE_DECL instead.
1200 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1201 the more obvious `uses_template_parms (spec)' to avoid problems
1202 with default function arguments. In particular, given
1203 something like this:
1205 template <class T> void f(T t1, T t = T())
1207 the default argument expression is not substituted for in an
1208 instantiation unless and until it is actually needed. */
1211 fn
= retrieve_specialization (tmpl
, args
,
1212 /*class_specializations_p=*/false);
1213 /* We can sometimes try to re-register a specialization that we've
1214 already got. In particular, regenerate_decl_from_template calls
1215 duplicate_decls which will update the specialization list. But,
1216 we'll still get called again here anyhow. It's more convenient
1217 to simply allow this than to try to prevent it. */
1220 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1222 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1225 || DECL_EXPLICIT_INSTANTIATION (fn
))
1227 error ("specialization of %qD after instantiation",
1229 return error_mark_node
;
1234 /* This situation should occur only if the first
1235 specialization is an implicit instantiation, the
1236 second is an explicit specialization, and the
1237 implicit instantiation has not yet been used. That
1238 situation can occur if we have implicitly
1239 instantiated a member function and then specialized
1242 We can also wind up here if a friend declaration that
1243 looked like an instantiation turns out to be a
1246 template <class T> void foo(T);
1247 class S { friend void foo<>(int) };
1248 template <> void foo(int);
1250 We transform the existing DECL in place so that any
1251 pointers to it become pointers to the updated
1254 If there was a definition for the template, but not
1255 for the specialization, we want this to look as if
1256 there were no definition, and vice versa. */
1257 DECL_INITIAL (fn
) = NULL_TREE
;
1258 duplicate_decls (spec
, fn
, is_friend
);
1259 /* The call to duplicate_decls will have applied
1262 An explicit specialization of a function template
1263 is inline only if it is explicitly declared to be,
1264 and independently of whether its function template
1267 to the primary function; now copy the inline bits to
1268 the various clones. */
1269 FOR_EACH_CLONE (clone
, fn
)
1271 DECL_DECLARED_INLINE_P (clone
)
1272 = DECL_DECLARED_INLINE_P (fn
);
1276 check_specialization_namespace (fn
);
1281 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1283 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1284 /* Dup decl failed, but this is a new definition. Set the
1285 line number so any errors match this new
1287 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1293 /* A specialization must be declared in the same namespace as the
1294 template it is specializing. */
1295 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1296 && !check_specialization_namespace (tmpl
))
1297 DECL_CONTEXT (spec
) = FROB_CONTEXT (decl_namespace_context (tmpl
));
1299 if (!optimize_specialization_lookup_p (tmpl
))
1300 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
1301 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
1306 /* Unregister the specialization SPEC as a specialization of TMPL.
1307 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1308 if the SPEC was listed as a specialization of TMPL. */
1311 reregister_specialization (tree spec
, tree tmpl
, tree new_spec
)
1315 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
1317 s
= &TREE_CHAIN (*s
))
1318 if (TREE_VALUE (*s
) == spec
)
1321 *s
= TREE_CHAIN (*s
);
1323 TREE_VALUE (*s
) = new_spec
;
1330 /* Compare an entry in the local specializations hash table P1 (which
1331 is really a pointer to a TREE_LIST) with P2 (which is really a
1335 eq_local_specializations (const void *p1
, const void *p2
)
1337 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1340 /* Hash P1, an entry in the local specializations table. */
1343 hash_local_specialization (const void* p1
)
1345 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1348 /* Like register_specialization, but for local declarations. We are
1349 registering SPEC, an instantiation of TMPL. */
1352 register_local_specialization (tree spec
, tree tmpl
)
1356 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1357 htab_hash_pointer (tmpl
), INSERT
);
1358 *slot
= build_tree_list (spec
, tmpl
);
1361 /* TYPE is a class type. Returns true if TYPE is an explicitly
1362 specialized class. */
1365 explicit_class_specialization_p (tree type
)
1367 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1369 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1372 /* Print the list of candidate FNS in an error message. */
1375 print_candidates (tree fns
)
1379 const char *str
= "candidates are:";
1381 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
1385 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
1386 error ("%s %+#D", str
, OVL_CURRENT (f
));
1391 /* Returns the template (one of the functions given by TEMPLATE_ID)
1392 which can be specialized to match the indicated DECL with the
1393 explicit template args given in TEMPLATE_ID. The DECL may be
1394 NULL_TREE if none is available. In that case, the functions in
1395 TEMPLATE_ID are non-members.
1397 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1398 specialization of a member template.
1400 The TEMPLATE_COUNT is the number of references to qualifying
1401 template classes that appeared in the name of the function. See
1402 check_explicit_specialization for a more accurate description.
1404 TSK indicates what kind of template declaration (if any) is being
1405 declared. TSK_TEMPLATE indicates that the declaration given by
1406 DECL, though a FUNCTION_DECL, has template parameters, and is
1407 therefore a template function.
1409 The template args (those explicitly specified and those deduced)
1410 are output in a newly created vector *TARGS_OUT.
1412 If it is impossible to determine the result, an error message is
1413 issued. The error_mark_node is returned to indicate failure. */
1416 determine_specialization (tree template_id
,
1419 int need_member_template
,
1425 tree explicit_targs
;
1426 tree candidates
= NULL_TREE
;
1427 /* A TREE_LIST of templates of which DECL may be a specialization.
1428 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1429 corresponding TREE_PURPOSE is the set of template arguments that,
1430 when used to instantiate the template, would produce a function
1431 with the signature of DECL. */
1432 tree templates
= NULL_TREE
;
1434 struct cp_binding_level
*b
;
1436 *targs_out
= NULL_TREE
;
1438 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1439 return error_mark_node
;
1441 fns
= TREE_OPERAND (template_id
, 0);
1442 explicit_targs
= TREE_OPERAND (template_id
, 1);
1444 if (fns
== error_mark_node
)
1445 return error_mark_node
;
1447 /* Check for baselinks. */
1448 if (BASELINK_P (fns
))
1449 fns
= BASELINK_FUNCTIONS (fns
);
1451 if (!is_overloaded_fn (fns
))
1453 error ("%qD is not a function template", fns
);
1454 return error_mark_node
;
1457 /* Count the number of template headers specified for this
1460 for (b
= current_binding_level
;
1461 b
->kind
== sk_template_parms
;
1465 for (; fns
; fns
= OVL_NEXT (fns
))
1467 tree fn
= OVL_CURRENT (fns
);
1469 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1471 tree decl_arg_types
;
1474 /* In case of explicit specialization, we need to check if
1475 the number of template headers appearing in the specialization
1476 is correct. This is usually done in check_explicit_specialization,
1477 but the check done there cannot be exhaustive when specializing
1478 member functions. Consider the following code:
1480 template <> void A<int>::f(int);
1481 template <> template <> void A<int>::f(int);
1483 Assuming that A<int> is not itself an explicit specialization
1484 already, the first line specializes "f" which is a non-template
1485 member function, whilst the second line specializes "f" which
1486 is a template member function. So both lines are syntactically
1487 correct, and check_explicit_specialization does not reject
1490 Here, we can do better, as we are matching the specialization
1491 against the declarations. We count the number of template
1492 headers, and we check if they match TEMPLATE_COUNT + 1
1493 (TEMPLATE_COUNT is the number of qualifying template classes,
1494 plus there must be another header for the member template
1497 Notice that if header_count is zero, this is not a
1498 specialization but rather a template instantiation, so there
1499 is no check we can perform here. */
1500 if (header_count
&& header_count
!= template_count
+ 1)
1503 /* Check that the number of template arguments at the
1504 innermost level for DECL is the same as for FN. */
1505 if (current_binding_level
->kind
== sk_template_parms
1506 && !current_binding_level
->explicit_spec_p
1507 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1508 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1509 (current_template_parms
))))
1512 /* DECL might be a specialization of FN. */
1513 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1514 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1516 /* For a non-static member function, we need to make sure
1517 that the const qualification is the same. Since
1518 get_bindings does not try to merge the "this" parameter,
1519 we must do the comparison explicitly. */
1520 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1521 && !same_type_p (TREE_VALUE (fn_arg_types
),
1522 TREE_VALUE (decl_arg_types
)))
1525 /* Skip the "this" parameter and, for constructors of
1526 classes with virtual bases, the VTT parameter. A
1527 full specialization of a constructor will have a VTT
1528 parameter, but a template never will. */
1530 = skip_artificial_parms_for (decl
, decl_arg_types
);
1532 = skip_artificial_parms_for (fn
, fn_arg_types
);
1534 /* Check that the number of function parameters matches.
1536 template <class T> void f(int i = 0);
1537 template <> void f<int>();
1538 The specialization f<int> is invalid but is not caught
1539 by get_bindings below. */
1540 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1543 /* Function templates cannot be specializations; there are
1544 no partial specializations of functions. Therefore, if
1545 the type of DECL does not match FN, there is no
1547 if (tsk
== tsk_template
)
1549 if (compparms (fn_arg_types
, decl_arg_types
))
1550 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1554 /* See whether this function might be a specialization of this
1556 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1559 /* We cannot deduce template arguments that when used to
1560 specialize TMPL will produce DECL. */
1563 /* Save this template, and the arguments deduced. */
1564 templates
= tree_cons (targs
, fn
, templates
);
1566 else if (need_member_template
)
1567 /* FN is an ordinary member function, and we need a
1568 specialization of a member template. */
1570 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1571 /* We can get IDENTIFIER_NODEs here in certain erroneous
1574 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1575 /* This is just an ordinary non-member function. Nothing can
1576 be a specialization of that. */
1578 else if (DECL_ARTIFICIAL (fn
))
1579 /* Cannot specialize functions that are created implicitly. */
1583 tree decl_arg_types
;
1585 /* This is an ordinary member function. However, since
1586 we're here, we can assume it's enclosing class is a
1587 template class. For example,
1589 template <typename T> struct S { void f(); };
1590 template <> void S<int>::f() {}
1592 Here, S<int>::f is a non-template, but S<int> is a
1593 template class. If FN has the same type as DECL, we
1594 might be in business. */
1596 if (!DECL_TEMPLATE_INFO (fn
))
1597 /* Its enclosing class is an explicit specialization
1598 of a template class. This is not a candidate. */
1601 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1602 TREE_TYPE (TREE_TYPE (fn
))))
1603 /* The return types differ. */
1606 /* Adjust the type of DECL in case FN is a static member. */
1607 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1608 if (DECL_STATIC_FUNCTION_P (fn
)
1609 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1610 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1612 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1615 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1619 if (templates
&& TREE_CHAIN (templates
))
1625 It is possible for a specialization with a given function
1626 signature to be instantiated from more than one function
1627 template. In such cases, explicit specification of the
1628 template arguments must be used to uniquely identify the
1629 function template specialization being specialized.
1631 Note that here, there's no suggestion that we're supposed to
1632 determine which of the candidate templates is most
1633 specialized. However, we, also have:
1637 Partial ordering of overloaded function template
1638 declarations is used in the following contexts to select
1639 the function template to which a function template
1640 specialization refers:
1642 -- when an explicit specialization refers to a function
1645 So, we do use the partial ordering rules, at least for now.
1646 This extension can only serve to make invalid programs valid,
1647 so it's safe. And, there is strong anecdotal evidence that
1648 the committee intended the partial ordering rules to apply;
1649 the EDG front end has that behavior, and John Spicer claims
1650 that the committee simply forgot to delete the wording in
1651 [temp.expl.spec]. */
1652 tree tmpl
= most_specialized_instantiation (templates
);
1653 if (tmpl
!= error_mark_node
)
1656 TREE_CHAIN (templates
) = NULL_TREE
;
1660 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1662 error ("template-id %qD for %q+D does not match any template "
1663 "declaration", template_id
, decl
);
1664 return error_mark_node
;
1666 else if ((templates
&& TREE_CHAIN (templates
))
1667 || (candidates
&& TREE_CHAIN (candidates
))
1668 || (templates
&& candidates
))
1670 error ("ambiguous template specialization %qD for %q+D",
1672 chainon (candidates
, templates
);
1673 print_candidates (candidates
);
1674 return error_mark_node
;
1677 /* We have one, and exactly one, match. */
1680 tree fn
= TREE_VALUE (candidates
);
1681 /* DECL is a re-declaration of a template function. */
1682 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1684 /* It was a specialization of an ordinary member function in a
1686 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
1687 return DECL_TI_TEMPLATE (fn
);
1690 /* It was a specialization of a template. */
1691 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1692 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1694 *targs_out
= copy_node (targs
);
1695 SET_TMPL_ARGS_LEVEL (*targs_out
,
1696 TMPL_ARGS_DEPTH (*targs_out
),
1697 TREE_PURPOSE (templates
));
1700 *targs_out
= TREE_PURPOSE (templates
);
1701 return TREE_VALUE (templates
);
1704 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1705 but with the default argument values filled in from those in the
1709 copy_default_args_to_explicit_spec_1 (tree spec_types
,
1712 tree new_spec_types
;
1717 if (spec_types
== void_list_node
)
1718 return void_list_node
;
1720 /* Substitute into the rest of the list. */
1722 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1723 TREE_CHAIN (tmpl_types
));
1725 /* Add the default argument for this parameter. */
1726 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1727 TREE_VALUE (spec_types
),
1731 /* DECL is an explicit specialization. Replicate default arguments
1732 from the template it specializes. (That way, code like:
1734 template <class T> void f(T = 3);
1735 template <> void f(double);
1738 works, as required.) An alternative approach would be to look up
1739 the correct default arguments at the call-site, but this approach
1740 is consistent with how implicit instantiations are handled. */
1743 copy_default_args_to_explicit_spec (tree decl
)
1748 tree new_spec_types
;
1752 tree object_type
= NULL_TREE
;
1753 tree in_charge
= NULL_TREE
;
1754 tree vtt
= NULL_TREE
;
1756 /* See if there's anything we need to do. */
1757 tmpl
= DECL_TI_TEMPLATE (decl
);
1758 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1759 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1760 if (TREE_PURPOSE (t
))
1765 old_type
= TREE_TYPE (decl
);
1766 spec_types
= TYPE_ARG_TYPES (old_type
);
1768 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1770 /* Remove the this pointer, but remember the object's type for
1772 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1773 spec_types
= TREE_CHAIN (spec_types
);
1774 tmpl_types
= TREE_CHAIN (tmpl_types
);
1776 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1778 /* DECL may contain more parameters than TMPL due to the extra
1779 in-charge parameter in constructors and destructors. */
1780 in_charge
= spec_types
;
1781 spec_types
= TREE_CHAIN (spec_types
);
1783 if (DECL_HAS_VTT_PARM_P (decl
))
1786 spec_types
= TREE_CHAIN (spec_types
);
1790 /* Compute the merged default arguments. */
1792 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1794 /* Compute the new FUNCTION_TYPE. */
1798 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1803 /* Put the in-charge parameter back. */
1804 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1805 TREE_VALUE (in_charge
),
1808 new_type
= build_method_type_directly (object_type
,
1809 TREE_TYPE (old_type
),
1813 new_type
= build_function_type (TREE_TYPE (old_type
),
1815 new_type
= cp_build_type_attribute_variant (new_type
,
1816 TYPE_ATTRIBUTES (old_type
));
1817 new_type
= build_exception_variant (new_type
,
1818 TYPE_RAISES_EXCEPTIONS (old_type
));
1819 TREE_TYPE (decl
) = new_type
;
1822 /* Check to see if the function just declared, as indicated in
1823 DECLARATOR, and in DECL, is a specialization of a function
1824 template. We may also discover that the declaration is an explicit
1825 instantiation at this point.
1827 Returns DECL, or an equivalent declaration that should be used
1828 instead if all goes well. Issues an error message if something is
1829 amiss. Returns error_mark_node if the error is not easily
1832 FLAGS is a bitmask consisting of the following flags:
1834 2: The function has a definition.
1835 4: The function is a friend.
1837 The TEMPLATE_COUNT is the number of references to qualifying
1838 template classes that appeared in the name of the function. For
1841 template <class T> struct S { void f(); };
1844 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1845 classes are not counted in the TEMPLATE_COUNT, so that in
1847 template <class T> struct S {};
1848 template <> struct S<int> { void f(); }
1849 template <> void S<int>::f();
1851 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1852 invalid; there should be no template <>.)
1854 If the function is a specialization, it is marked as such via
1855 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1856 is set up correctly, and it is added to the list of specializations
1857 for that template. */
1860 check_explicit_specialization (tree declarator
,
1865 int have_def
= flags
& 2;
1866 int is_friend
= flags
& 4;
1867 int specialization
= 0;
1868 int explicit_instantiation
= 0;
1869 int member_specialization
= 0;
1870 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1871 tree dname
= DECL_NAME (decl
);
1876 if (!processing_specialization
)
1879 tsk
= tsk_excessive_parms
;
1882 tsk
= current_tmpl_spec_kind (template_count
);
1887 if (processing_specialization
)
1890 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1892 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1895 /* This could be something like:
1897 template <class T> void f(T);
1898 class S { friend void f<>(int); } */
1902 /* This case handles bogus declarations like template <>
1903 template <class T> void f<int>(); */
1905 error ("template-id %qD in declaration of primary template",
1912 case tsk_invalid_member_spec
:
1913 /* The error has already been reported in
1914 check_specialization_scope. */
1915 return error_mark_node
;
1917 case tsk_invalid_expl_inst
:
1918 error ("template parameter list used in explicit instantiation");
1924 error ("definition provided for explicit instantiation");
1926 explicit_instantiation
= 1;
1929 case tsk_excessive_parms
:
1930 case tsk_insufficient_parms
:
1931 if (tsk
== tsk_excessive_parms
)
1932 error ("too many template parameter lists in declaration of %qD",
1934 else if (template_header_count
)
1935 error("too few template parameter lists in declaration of %qD", decl
);
1937 error("explicit specialization of %qD must be introduced by "
1938 "%<template <>%>", decl
);
1942 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1944 member_specialization
= 1;
1950 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1952 /* This case handles bogus declarations like template <>
1953 template <class T> void f<int>(); */
1955 if (uses_template_parms (declarator
))
1956 error ("function template partial specialization %qD "
1957 "is not allowed", declarator
);
1959 error ("template-id %qD in declaration of primary template",
1964 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1965 /* This is a specialization of a member template, without
1966 specialization the containing class. Something like:
1968 template <class T> struct S {
1969 template <class U> void f (U);
1971 template <> template <class U> void S<int>::f(U) {}
1973 That's a specialization -- but of the entire template. */
1981 if (specialization
|| member_specialization
)
1983 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1984 for (; t
; t
= TREE_CHAIN (t
))
1985 if (TREE_PURPOSE (t
))
1988 ("default argument specified in explicit specialization");
1993 if (specialization
|| member_specialization
|| explicit_instantiation
)
1995 tree tmpl
= NULL_TREE
;
1996 tree targs
= NULL_TREE
;
1998 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1999 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2003 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2008 /* If there is no class context, the explicit instantiation
2009 must be at namespace scope. */
2010 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2012 /* Find the namespace binding, using the declaration
2014 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2016 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2018 error ("%qD is not a template function", dname
);
2019 fns
= error_mark_node
;
2023 tree fn
= OVL_CURRENT (fns
);
2024 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2025 CP_DECL_CONTEXT (fn
)))
2026 error ("%qD is not declared in %qD",
2027 decl
, current_namespace
);
2031 declarator
= lookup_template_function (fns
, NULL_TREE
);
2034 if (declarator
== error_mark_node
)
2035 return error_mark_node
;
2037 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2039 if (!explicit_instantiation
)
2040 /* A specialization in class scope. This is invalid,
2041 but the error will already have been flagged by
2042 check_specialization_scope. */
2043 return error_mark_node
;
2046 /* It's not valid to write an explicit instantiation in
2049 class C { template void f(); }
2051 This case is caught by the parser. However, on
2054 template class C { void f(); };
2056 (which is invalid) we can get here. The error will be
2063 else if (ctype
!= NULL_TREE
2064 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2067 /* Find the list of functions in ctype that have the same
2068 name as the declared function. */
2069 tree name
= TREE_OPERAND (declarator
, 0);
2070 tree fns
= NULL_TREE
;
2073 if (constructor_name_p (name
, ctype
))
2075 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2077 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
2078 : !CLASSTYPE_DESTRUCTORS (ctype
))
2080 /* From [temp.expl.spec]:
2082 If such an explicit specialization for the member
2083 of a class template names an implicitly-declared
2084 special member function (clause _special_), the
2085 program is ill-formed.
2087 Similar language is found in [temp.explicit]. */
2088 error ("specialization of implicitly-declared special member function");
2089 return error_mark_node
;
2092 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2095 if (!DECL_CONV_FN_P (decl
))
2097 idx
= lookup_fnfields_1 (ctype
, name
);
2099 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2103 VEC(tree
,gc
) *methods
;
2106 /* For a type-conversion operator, we cannot do a
2107 name-based lookup. We might be looking for `operator
2108 int' which will be a specialization of `operator T'.
2109 So, we find *all* the conversion operators, and then
2110 select from them. */
2113 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2115 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2116 VEC_iterate (tree
, methods
, idx
, ovl
);
2119 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2120 /* There are no more conversion functions. */
2123 /* Glue all these conversion functions together
2124 with those we already have. */
2125 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2126 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2130 if (fns
== NULL_TREE
)
2132 error ("no member function %qD declared in %qT", name
, ctype
);
2133 return error_mark_node
;
2136 TREE_OPERAND (declarator
, 0) = fns
;
2139 /* Figure out what exactly is being specialized at this point.
2140 Note that for an explicit instantiation, even one for a
2141 member function, we cannot tell apriori whether the
2142 instantiation is for a member template, or just a member
2143 function of a template class. Even if a member template is
2144 being instantiated, the member template arguments may be
2145 elided if they can be deduced from the rest of the
2147 tmpl
= determine_specialization (declarator
, decl
,
2149 member_specialization
,
2153 if (!tmpl
|| tmpl
== error_mark_node
)
2154 /* We couldn't figure out what this declaration was
2156 return error_mark_node
;
2159 tree gen_tmpl
= most_general_template (tmpl
);
2161 if (explicit_instantiation
)
2163 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2164 is done by do_decl_instantiation later. */
2166 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2167 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2169 if (arg_depth
> parm_depth
)
2171 /* If TMPL is not the most general template (for
2172 example, if TMPL is a friend template that is
2173 injected into namespace scope), then there will
2174 be too many levels of TARGS. Remove some of them
2179 new_targs
= make_tree_vec (parm_depth
);
2180 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2181 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2182 = TREE_VEC_ELT (targs
, i
);
2186 return instantiate_template (tmpl
, targs
, tf_error
);
2189 /* If we thought that the DECL was a member function, but it
2190 turns out to be specializing a static member function,
2191 make DECL a static member function as well. */
2192 if (DECL_STATIC_FUNCTION_P (tmpl
)
2193 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2194 revert_static_member_fn (decl
);
2196 /* If this is a specialization of a member template of a
2197 template class, we want to return the TEMPLATE_DECL, not
2198 the specialization of it. */
2199 if (tsk
== tsk_template
)
2201 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2202 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
2205 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2206 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl
))
2207 = DECL_SOURCE_LOCATION (decl
);
2208 /* We want to use the argument list specified in the
2209 definition, not in the original declaration. */
2210 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl
))
2211 = DECL_ARGUMENTS (decl
);
2216 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2217 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
2219 /* Inherit default function arguments from the template
2220 DECL is specializing. */
2221 copy_default_args_to_explicit_spec (decl
);
2223 /* This specialization has the same protection as the
2224 template it specializes. */
2225 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2226 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2228 /* 7.1.1-1 [dcl.stc]
2230 A storage-class-specifier shall not be specified in an
2231 explicit specialization...
2233 The parser rejects these, so unless action is taken here,
2234 explicit function specializations will always appear with
2237 The action recommended by the C++ CWG in response to C++
2238 defect report 605 is to make the storage class and linkage
2239 of the explicit specialization match the templated function:
2241 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2243 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2245 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2246 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2248 /* This specialization has the same linkage and visibility as
2249 the function template it specializes. */
2250 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2251 if (! TREE_PUBLIC (decl
))
2253 DECL_INTERFACE_KNOWN (decl
) = 1;
2254 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2256 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2257 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2259 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2260 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2264 /* If DECL is a friend declaration, declared using an
2265 unqualified name, the namespace associated with DECL may
2266 have been set incorrectly. For example, in:
2268 template <typename T> void f(T);
2270 struct S { friend void f<int>(int); }
2273 we will have set the DECL_CONTEXT for the friend
2274 declaration to N, rather than to the global namespace. */
2275 if (DECL_NAMESPACE_SCOPE_P (decl
))
2276 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2278 if (is_friend
&& !have_def
)
2279 /* This is not really a declaration of a specialization.
2280 It's just the name of an instantiation. But, it's not
2281 a request for an instantiation, either. */
2282 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2283 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2284 /* This is indeed a specialization. In case of constructors
2285 and destructors, we need in-charge and not-in-charge
2286 versions in V3 ABI. */
2287 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2289 /* Register this specialization so that we can find it
2291 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
);
2298 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2299 parameters. These are represented in the same format used for
2300 DECL_TEMPLATE_PARMS. */
2303 comp_template_parms (const_tree parms1
, const_tree parms2
)
2308 if (parms1
== parms2
)
2311 for (p1
= parms1
, p2
= parms2
;
2312 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2313 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2315 tree t1
= TREE_VALUE (p1
);
2316 tree t2
= TREE_VALUE (p2
);
2319 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2320 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2322 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2325 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2327 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2328 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2330 /* If either of the template parameters are invalid, assume
2331 they match for the sake of error recovery. */
2332 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2335 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2338 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2339 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2340 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2342 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2347 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2348 /* One set of parameters has more parameters lists than the
2355 /* Determine whether PARM is a parameter pack. */
2357 template_parameter_pack_p (const_tree parm
)
2359 /* Determine if we have a non-type template parameter pack. */
2360 if (TREE_CODE (parm
) == PARM_DECL
)
2361 return (DECL_TEMPLATE_PARM_P (parm
)
2362 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2364 /* If this is a list of template parameters, we could get a
2365 TYPE_DECL or a TEMPLATE_DECL. */
2366 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2367 parm
= TREE_TYPE (parm
);
2369 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2370 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2371 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2374 /* Determine whether ARGS describes a variadic template args list,
2375 i.e., one that is terminated by a template argument pack. */
2377 template_args_variadic_p (tree args
)
2382 if (args
== NULL_TREE
)
2385 args
= INNERMOST_TEMPLATE_ARGS (args
);
2386 nargs
= TREE_VEC_LENGTH (args
);
2391 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2393 return ARGUMENT_PACK_P (last_parm
);
2396 /* Generate a new name for the parameter pack name NAME (an
2397 IDENTIFIER_NODE) that incorporates its */
2399 make_ith_pack_parameter_name (tree name
, int i
)
2401 /* Munge the name to include the parameter index. */
2405 sprintf(numbuf
, "%i", i
);
2406 newname
= (char*)alloca (IDENTIFIER_LENGTH (name
) + strlen(numbuf
) + 2);
2407 sprintf(newname
, "%s#%i", IDENTIFIER_POINTER (name
), i
);
2408 return get_identifier (newname
);
2411 /* Structure used to track the progress of find_parameter_pack_r. */
2412 struct find_parameter_pack_data
2414 tree
* parameter_packs
;
2415 struct pointer_set_t
*visited
;
2418 /* Identifiers all of the argument packs that occur in a template
2419 argument and appends them to the TREE_LIST inside DATA, which is a
2420 find_parameter_pack_Data structure. This is a subroutine of
2421 make_pack_expansion and uses_parameter_packs. */
2423 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2426 struct find_parameter_pack_data
* ppd
=
2427 (struct find_parameter_pack_data
*)data
;
2431 tree context
= TYPE_CONTEXT (t
);
2432 cp_walk_tree (&context
, &find_parameter_packs_r
, ppd
, ppd
->visited
);
2435 /* This switch statement will return immediately if we don't find a
2437 switch (TREE_CODE (t
))
2439 case TEMPLATE_PARM_INDEX
:
2440 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2444 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2445 /* Check the template arguments. */
2446 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2449 /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */
2450 t
= TYPE_TI_TEMPLATE (t
);
2451 if (DECL_P (t
) && TREE_TYPE (t
))
2457 case TEMPLATE_TYPE_PARM
:
2458 case TEMPLATE_TEMPLATE_PARM
:
2459 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2464 if (FUNCTION_PARAMETER_PACK_P (t
))
2466 /* We don't want to walk into the type of a PARM_DECL,
2467 because we don't want to see the type parameter pack.*/
2474 if (TYPE_PTRMEMFUNC_P (t
))
2480 if (TYPE_TEMPLATE_INFO (t
))
2482 tree args
= TREE_VALUE (TYPE_TEMPLATE_INFO (t
));
2483 cp_walk_tree (&args
, &find_parameter_packs_r
, ppd
, ppd
->visited
);
2490 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
2491 && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t
)))
2497 case TYPE_PACK_EXPANSION
:
2498 case EXPR_PACK_EXPANSION
:
2503 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
2512 /* Add this parameter pack to the list. */
2513 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2518 /* Determines if the expression or type T uses any parameter packs. */
2520 uses_parameter_packs (tree t
)
2522 tree parameter_packs
= NULL_TREE
;
2523 struct find_parameter_pack_data ppd
;
2524 ppd
.parameter_packs
= ¶meter_packs
;
2525 ppd
.visited
= pointer_set_create ();
2526 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2527 pointer_set_destroy (ppd
.visited
);
2528 return parameter_packs
!= NULL_TREE
;
2531 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2532 representation a base-class initializer into a parameter pack
2533 expansion. If all goes well, the resulting node will be an
2534 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2537 make_pack_expansion (tree arg
)
2540 tree parameter_packs
= NULL_TREE
;
2541 bool for_types
= false;
2542 struct find_parameter_pack_data ppd
;
2544 if (!arg
|| arg
== error_mark_node
)
2547 if (TREE_CODE (arg
) == TREE_LIST
)
2549 /* The only time we will see a TREE_LIST here is for a base
2550 class initializer. In this case, the TREE_PURPOSE will be a
2551 _TYPE node (representing the base class expansion we're
2552 initializing) and the TREE_VALUE will be a TREE_LIST
2553 containing the initialization arguments.
2555 The resulting expansion looks somewhat different from most
2556 expansions. Rather than returning just one _EXPANSION, we
2557 return a TREE_LIST whose TREE_PURPOSE is a
2558 TYPE_PACK_EXPANSION containing the bases that will be
2559 initialized. The TREE_VALUE will be identical to the
2560 original TREE_VALUE, which is a list of arguments that will
2561 be passed to each base. We do not introduce any new pack
2562 expansion nodes into the TREE_VALUE (although it is possible
2563 that some already exist), because the TREE_PURPOSE and
2564 TREE_VALUE all need to be expanded together with the same
2565 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2566 resulting TREE_PURPOSE will mention the parameter packs in
2567 both the bases and the arguments to the bases. */
2570 tree parameter_packs
= NULL_TREE
;
2572 /* Determine which parameter packs will be used by the base
2574 ppd
.visited
= pointer_set_create ();
2575 ppd
.parameter_packs
= ¶meter_packs
;
2576 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
2579 if (parameter_packs
== NULL_TREE
)
2581 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
2582 pointer_set_destroy (ppd
.visited
);
2583 return error_mark_node
;
2586 if (TREE_VALUE (arg
) != void_type_node
)
2588 /* Collect the sets of parameter packs used in each of the
2589 initialization arguments. */
2590 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
2592 /* Determine which parameter packs will be expanded in this
2594 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
2599 pointer_set_destroy (ppd
.visited
);
2601 /* Create the pack expansion type for the base type. */
2602 purpose
= make_node (TYPE_PACK_EXPANSION
);
2603 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
2604 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
2606 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2607 they will rarely be compared to anything. */
2608 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
2610 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
2613 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
2616 /* Build the PACK_EXPANSION_* node. */
2617 result
= make_node (for_types
? TYPE_PACK_EXPANSION
: EXPR_PACK_EXPANSION
);
2618 SET_PACK_EXPANSION_PATTERN (result
, arg
);
2619 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
2621 /* Propagate type and const-expression information. */
2622 TREE_TYPE (result
) = TREE_TYPE (arg
);
2623 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
2626 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2627 they will rarely be compared to anything. */
2628 SET_TYPE_STRUCTURAL_EQUALITY (result
);
2630 /* Determine which parameter packs will be expanded. */
2631 ppd
.parameter_packs
= ¶meter_packs
;
2632 ppd
.visited
= pointer_set_create ();
2633 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2634 pointer_set_destroy (ppd
.visited
);
2636 /* Make sure we found some parameter packs. */
2637 if (parameter_packs
== NULL_TREE
)
2640 error ("expansion pattern %<%T%> contains no argument packs", arg
);
2642 error ("expansion pattern %<%E%> contains no argument packs", arg
);
2643 return error_mark_node
;
2645 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
2650 /* Checks T for any "bare" parameter packs, which have not yet been
2651 expanded, and issues an error if any are found. This operation can
2652 only be done on full expressions or types (e.g., an expression
2653 statement, "if" condition, etc.), because we could have expressions like:
2655 foo(f(g(h(args)))...)
2657 where "args" is a parameter pack. check_for_bare_parameter_packs
2658 should not be called for the subexpressions args, h(args),
2659 g(h(args)), or f(g(h(args))), because we would produce erroneous
2662 Returns TRUE if there were no bare parameter packs, returns FALSE
2663 (and emits an error) if there were bare parameter packs.*/
2665 check_for_bare_parameter_packs (tree t
)
2667 tree parameter_packs
= NULL_TREE
;
2668 struct find_parameter_pack_data ppd
;
2670 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
2673 if (TREE_CODE (t
) == TYPE_DECL
)
2676 ppd
.parameter_packs
= ¶meter_packs
;
2677 ppd
.visited
= pointer_set_create ();
2678 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
2679 pointer_set_destroy (ppd
.visited
);
2681 if (parameter_packs
)
2683 error ("parameter packs not expanded with `...':");
2684 while (parameter_packs
)
2686 tree pack
= TREE_VALUE (parameter_packs
);
2687 tree name
= NULL_TREE
;
2689 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
2690 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
2691 name
= TYPE_NAME (pack
);
2692 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
2693 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
2695 name
= DECL_NAME (pack
);
2696 inform (" %qD", name
);
2698 parameter_packs
= TREE_CHAIN (parameter_packs
);
2707 /* Expand any parameter packs that occur in the template arguments in
2710 expand_template_argument_pack (tree args
)
2712 tree result_args
= NULL_TREE
;
2713 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
2714 int num_result_args
= -1;
2716 /* First, determine if we need to expand anything, and the number of
2717 slots we'll need. */
2718 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2720 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2721 if (ARGUMENT_PACK_P (arg
))
2723 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
2724 if (num_result_args
< 0)
2725 num_result_args
= in_arg
+ num_packed
;
2727 num_result_args
+= num_packed
;
2731 if (num_result_args
>= 0)
2736 /* If no expansion is necessary, we're done. */
2737 if (num_result_args
< 0)
2740 /* Expand arguments. */
2741 result_args
= make_tree_vec (num_result_args
);
2742 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
2744 tree arg
= TREE_VEC_ELT (args
, in_arg
);
2745 if (ARGUMENT_PACK_P (arg
))
2747 tree packed
= ARGUMENT_PACK_ARGS (arg
);
2748 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
2749 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
2750 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
2754 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
2762 /* Complain if DECL shadows a template parameter.
2764 [temp.local]: A template-parameter shall not be redeclared within its
2765 scope (including nested scopes). */
2768 check_template_shadow (tree decl
)
2772 /* If we're not in a template, we can't possibly shadow a template
2774 if (!current_template_parms
)
2777 /* Figure out what we're shadowing. */
2778 if (TREE_CODE (decl
) == OVERLOAD
)
2779 decl
= OVL_CURRENT (decl
);
2780 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
2782 /* If there's no previous binding for this name, we're not shadowing
2783 anything, let alone a template parameter. */
2787 /* If we're not shadowing a template parameter, we're done. Note
2788 that OLDDECL might be an OVERLOAD (or perhaps even an
2789 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2791 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
2794 /* We check for decl != olddecl to avoid bogus errors for using a
2795 name inside a class. We check TPFI to avoid duplicate errors for
2796 inline member templates. */
2798 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
2801 error ("declaration of %q+#D", decl
);
2802 error (" shadows template parm %q+#D", olddecl
);
2805 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2806 ORIG_LEVEL, DECL, and TYPE. */
2809 build_template_parm_index (int index
,
2815 tree t
= make_node (TEMPLATE_PARM_INDEX
);
2816 TEMPLATE_PARM_IDX (t
) = index
;
2817 TEMPLATE_PARM_LEVEL (t
) = level
;
2818 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
2819 TEMPLATE_PARM_DECL (t
) = decl
;
2820 TREE_TYPE (t
) = type
;
2821 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
2822 TREE_INVARIANT (t
) = TREE_INVARIANT (decl
);
2823 TREE_READONLY (t
) = TREE_READONLY (decl
);
2828 /* Find the canonical type parameter for the given template type
2829 parameter. Returns the canonical type parameter, which may be TYPE
2830 if no such parameter existed. */
2832 canonical_type_parameter (tree type
)
2835 int idx
= TEMPLATE_TYPE_IDX (type
);
2836 if (!canonical_template_parms
)
2837 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
2839 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
2840 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
2842 list
= VEC_index (tree
, canonical_template_parms
, idx
);
2843 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
2844 list
= TREE_CHAIN (list
);
2847 return TREE_VALUE (list
);
2850 VEC_replace(tree
, canonical_template_parms
, idx
,
2851 tree_cons (NULL_TREE
, type
,
2852 VEC_index (tree
, canonical_template_parms
, idx
)));
2857 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2858 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2859 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2860 new one is created. */
2863 reduce_template_parm_level (tree index
, tree type
, int levels
)
2865 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
2866 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
2867 != TEMPLATE_PARM_LEVEL (index
) - levels
))
2869 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
2872 decl
= build_decl (TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
2873 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
2874 TREE_INVARIANT (decl
) = TREE_INVARIANT (orig_decl
);
2875 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
2876 DECL_ARTIFICIAL (decl
) = 1;
2877 SET_DECL_TEMPLATE_PARM_P (decl
);
2879 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
2880 TEMPLATE_PARM_LEVEL (index
) - levels
,
2881 TEMPLATE_PARM_ORIG_LEVEL (index
),
2883 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
2884 TEMPLATE_PARM_PARAMETER_PACK (t
)
2885 = TEMPLATE_PARM_PARAMETER_PACK (index
);
2887 /* Template template parameters need this. */
2888 if (TREE_CODE (decl
) != CONST_DECL
)
2889 DECL_TEMPLATE_PARMS (decl
)
2890 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
2893 return TEMPLATE_PARM_DESCENDANTS (index
);
2896 /* Process information from new template parameter PARM and append it to the
2897 LIST being built. This new parameter is a non-type parameter iff
2898 IS_NON_TYPE is true. This new parameter is a parameter
2899 pack iff IS_PARAMETER_PACK is true. */
2902 process_template_parm (tree list
, tree parm
, bool is_non_type
,
2903 bool is_parameter_pack
)
2910 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
2911 defval
= TREE_PURPOSE (parm
);
2915 tree p
= tree_last (list
);
2917 if (p
&& TREE_VALUE (p
) != error_mark_node
)
2920 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
2921 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
2923 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
2933 parm
= TREE_VALUE (parm
);
2935 SET_DECL_TEMPLATE_PARM_P (parm
);
2937 if (TREE_TYPE (parm
) == error_mark_node
)
2939 err_parm_list
= build_tree_list (defval
, parm
);
2940 TREE_VALUE (err_parm_list
) = error_mark_node
;
2941 return chainon (list
, err_parm_list
);
2947 The top-level cv-qualifiers on the template-parameter are
2948 ignored when determining its type. */
2949 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
2950 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
2952 err_parm_list
= build_tree_list (defval
, parm
);
2953 TREE_VALUE (err_parm_list
) = error_mark_node
;
2954 return chainon (list
, err_parm_list
);
2957 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
2959 /* This template parameter is not a parameter pack, but it
2960 should be. Complain about "bare" parameter packs. */
2961 check_for_bare_parameter_packs (TREE_TYPE (parm
));
2963 /* Recover by calling this a parameter pack. */
2964 is_parameter_pack
= true;
2968 /* A template parameter is not modifiable. */
2969 TREE_CONSTANT (parm
) = 1;
2970 TREE_INVARIANT (parm
) = 1;
2971 TREE_READONLY (parm
) = 1;
2972 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
2973 TREE_CONSTANT (decl
) = 1;
2974 TREE_INVARIANT (decl
) = 1;
2975 TREE_READONLY (decl
) = 1;
2976 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
2977 = build_template_parm_index (idx
, processing_template_decl
,
2978 processing_template_decl
,
2979 decl
, TREE_TYPE (parm
));
2981 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
2982 = is_parameter_pack
;
2987 parm
= TREE_VALUE (TREE_VALUE (parm
));
2989 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
2991 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
2992 /* This is for distinguishing between real templates and template
2993 template parameters */
2994 TREE_TYPE (parm
) = t
;
2995 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3000 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
3001 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3002 decl
= build_decl (TYPE_DECL
, parm
, t
);
3005 TYPE_NAME (t
) = decl
;
3006 TYPE_STUB_DECL (t
) = decl
;
3008 TEMPLATE_TYPE_PARM_INDEX (t
)
3009 = build_template_parm_index (idx
, processing_template_decl
,
3010 processing_template_decl
,
3011 decl
, TREE_TYPE (parm
));
3012 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3013 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3015 DECL_ARTIFICIAL (decl
) = 1;
3016 SET_DECL_TEMPLATE_PARM_P (decl
);
3018 parm
= build_tree_list (defval
, parm
);
3019 return chainon (list
, parm
);
3022 /* The end of a template parameter list has been reached. Process the
3023 tree list into a parameter vector, converting each parameter into a more
3024 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3028 end_template_parm_list (tree parms
)
3032 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3034 current_template_parms
3035 = tree_cons (size_int (processing_template_decl
),
3036 saved_parmlist
, current_template_parms
);
3038 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3040 next
= TREE_CHAIN (parm
);
3041 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3042 TREE_CHAIN (parm
) = NULL_TREE
;
3045 --processing_template_parmlist
;
3047 return saved_parmlist
;
3050 /* end_template_decl is called after a template declaration is seen. */
3053 end_template_decl (void)
3055 reset_specialization ();
3057 if (! processing_template_decl
)
3060 /* This matches the pushlevel in begin_template_parm_list. */
3063 --processing_template_decl
;
3064 current_template_parms
= TREE_CHAIN (current_template_parms
);
3067 /* Within the declaration of a template, return all levels of template
3068 parameters that apply. The template parameters are represented as
3069 a TREE_VEC, in the form documented in cp-tree.h for template
3073 current_template_args (void)
3076 tree args
= NULL_TREE
;
3077 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3080 /* If there is only one level of template parameters, we do not
3081 create a TREE_VEC of TREE_VECs. Instead, we return a single
3082 TREE_VEC containing the arguments. */
3084 args
= make_tree_vec (length
);
3086 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3088 tree a
= copy_node (TREE_VALUE (header
));
3091 TREE_TYPE (a
) = NULL_TREE
;
3092 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3094 tree t
= TREE_VEC_ELT (a
, i
);
3096 /* T will be a list if we are called from within a
3097 begin/end_template_parm_list pair, but a vector directly
3098 if within a begin/end_member_template_processing pair. */
3099 if (TREE_CODE (t
) == TREE_LIST
)
3103 if (t
!= error_mark_node
)
3105 if (TREE_CODE (t
) == TYPE_DECL
3106 || TREE_CODE (t
) == TEMPLATE_DECL
)
3110 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3112 /* Turn this argument into a TYPE_ARGUMENT_PACK
3113 with a single element, which expands T. */
3114 tree vec
= make_tree_vec (1);
3115 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3117 t
= make_node (TYPE_ARGUMENT_PACK
);
3118 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3123 t
= DECL_INITIAL (t
);
3125 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3127 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3128 with a single element, which expands T. */
3129 tree vec
= make_tree_vec (1);
3130 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3131 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3133 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3134 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3135 TREE_TYPE (t
) = type
;
3139 TREE_VEC_ELT (a
, i
) = t
;
3144 TREE_VEC_ELT (args
, --l
) = a
;
3152 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3153 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3154 a member template. Used by push_template_decl below. */
3157 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3159 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3160 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3161 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3162 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3163 if (DECL_LANG_SPECIFIC (decl
))
3165 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
3166 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
3167 DECL_DESTRUCTOR_P (tmpl
) = DECL_DESTRUCTOR_P (decl
);
3168 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
3169 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
3170 if (DECL_OVERLOADED_OPERATOR_P (decl
))
3171 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
3172 DECL_OVERLOADED_OPERATOR_P (decl
));
3178 struct template_parm_data
3180 /* The level of the template parameters we are currently
3184 /* The index of the specialization argument we are currently
3188 /* An array whose size is the number of template parameters. The
3189 elements are nonzero if the parameter has been used in any one
3190 of the arguments processed so far. */
3193 /* An array whose size is the number of template arguments. The
3194 elements are nonzero if the argument makes use of template
3195 parameters of this level. */
3196 int* arg_uses_template_parms
;
3199 /* Subroutine of push_template_decl used to see if each template
3200 parameter in a partial specialization is used in the explicit
3201 argument list. If T is of the LEVEL given in DATA (which is
3202 treated as a template_parm_data*), then DATA->PARMS is marked
3206 mark_template_parm (tree t
, void* data
)
3210 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3212 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3214 level
= TEMPLATE_PARM_LEVEL (t
);
3215 idx
= TEMPLATE_PARM_IDX (t
);
3219 level
= TEMPLATE_TYPE_LEVEL (t
);
3220 idx
= TEMPLATE_TYPE_IDX (t
);
3223 if (level
== tpd
->level
)
3225 tpd
->parms
[idx
] = 1;
3226 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3229 /* Return zero so that for_each_template_parm will continue the
3230 traversal of the tree; we want to mark *every* template parm. */
3234 /* Process the partial specialization DECL. */
3237 process_partial_specialization (tree decl
)
3239 tree type
= TREE_TYPE (decl
);
3240 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3241 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3242 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3243 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3244 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3245 int nargs
= TREE_VEC_LENGTH (inner_args
);
3246 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
3248 int did_error_intro
= 0;
3249 struct template_parm_data tpd
;
3250 struct template_parm_data tpd2
;
3252 /* We check that each of the template parameters given in the
3253 partial specialization is used in the argument list to the
3254 specialization. For example:
3256 template <class T> struct S;
3257 template <class T> struct S<T*>;
3259 The second declaration is OK because `T*' uses the template
3260 parameter T, whereas
3262 template <class T> struct S<int>;
3264 is no good. Even trickier is:
3275 The S2<T> declaration is actually invalid; it is a
3276 full-specialization. Of course,
3279 struct S2<T (*)(U)>;
3281 or some such would have been OK. */
3282 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3283 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
3284 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3286 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
3287 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3288 for (i
= 0; i
< nargs
; ++i
)
3290 tpd
.current_arg
= i
;
3291 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3292 &mark_template_parm
,
3296 for (i
= 0; i
< ntparms
; ++i
)
3297 if (tpd
.parms
[i
] == 0)
3299 /* One of the template parms was not used in the
3301 if (!did_error_intro
)
3303 error ("template parameters not used in partial specialization:");
3304 did_error_intro
= 1;
3307 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3310 /* [temp.class.spec]
3312 The argument list of the specialization shall not be identical to
3313 the implicit argument list of the primary template. */
3314 if (comp_template_args
3316 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3318 error ("partial specialization %qT does not specialize any template arguments", type
);
3320 /* [temp.class.spec]
3322 A partially specialized non-type argument expression shall not
3323 involve template parameters of the partial specialization except
3324 when the argument expression is a simple identifier.
3326 The type of a template parameter corresponding to a specialized
3327 non-type argument shall not be dependent on a parameter of the
3330 Also, we verify that pack expansions only occur at the
3331 end of the argument list. */
3332 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3334 for (i
= 0; i
< nargs
; ++i
)
3336 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3337 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3338 tree packed_args
= NULL_TREE
;
3341 if (ARGUMENT_PACK_P (arg
))
3343 /* Extract the arguments from the argument pack. We'll be
3344 iterating over these in the following loop. */
3345 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3346 len
= TREE_VEC_LENGTH (packed_args
);
3349 for (j
= 0; j
< len
; j
++)
3352 /* Get the Jth argument in the parameter pack. */
3353 arg
= TREE_VEC_ELT (packed_args
, j
);
3355 if (PACK_EXPANSION_P (arg
))
3357 /* Pack expansions must come at the end of the
3359 if ((packed_args
&& j
< len
- 1)
3360 || (!packed_args
&& i
< nargs
- 1))
3362 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3363 error ("parameter pack argument %qE must be at the end of the template argument list", arg
);
3365 error ("parameter pack argument %qT must be at the end of the template argument list", arg
);
3369 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3370 /* We only care about the pattern. */
3371 arg
= PACK_EXPANSION_PATTERN (arg
);
3373 if (/* These first two lines are the `non-type' bit. */
3375 && TREE_CODE (arg
) != TEMPLATE_DECL
3376 /* This next line is the `argument expression is not just a
3377 simple identifier' condition and also the `specialized
3378 non-type argument' bit. */
3379 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
3381 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
3382 || (packed_args
&& uses_template_parms (arg
)))
3383 error ("template argument %qE involves template parameter(s)",
3387 /* Look at the corresponding template parameter,
3388 marking which template parameters its type depends
3390 tree type
= TREE_TYPE (parm
);
3394 /* We haven't yet initialized TPD2. Do so now. */
3395 tpd2
.arg_uses_template_parms
3396 = (int *) alloca (sizeof (int) * nargs
);
3397 /* The number of parameters here is the number in the
3398 main template, which, as checked in the assertion
3400 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
3402 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
3405 /* Mark the template parameters. But this time, we're
3406 looking for the template parameters of the main
3407 template, not in the specialization. */
3408 tpd2
.current_arg
= i
;
3409 tpd2
.arg_uses_template_parms
[i
] = 0;
3410 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
3411 for_each_template_parm (type
,
3412 &mark_template_parm
,
3416 if (tpd2
.arg_uses_template_parms
[i
])
3418 /* The type depended on some template parameters.
3419 If they are fully specialized in the
3420 specialization, that's OK. */
3422 for (j
= 0; j
< nargs
; ++j
)
3423 if (tpd2
.parms
[j
] != 0
3424 && tpd
.arg_uses_template_parms
[j
])
3426 error ("type %qT of template argument %qE depends "
3427 "on template parameter(s)",
3438 if (retrieve_specialization (maintmpl
, specargs
,
3439 /*class_specializations_p=*/true))
3440 /* We've already got this specialization. */
3443 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
3444 = tree_cons (specargs
, inner_parms
,
3445 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
3446 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
3450 /* Check that a template declaration's use of default arguments is not
3451 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
3452 nonzero if DECL is the thing declared by a primary template.
3453 IS_PARTIAL is nonzero if DECL is a partial specialization.
3456 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3457 declaration (but not a definition); 1 indicates a declaration, 2
3458 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3459 emitted for extraneous default arguments.
3461 Returns TRUE if there were no errors found, FALSE otherwise. */
3464 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
3465 int is_partial
, int is_friend_decl
)
3468 int last_level_to_check
;
3470 bool no_errors
= true;
3474 A default template-argument shall not be specified in a
3475 function template declaration or a function template definition, nor
3476 in the template-parameter-list of the definition of a member of a
3479 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
3480 /* You can't have a function template declaration in a local
3481 scope, nor you can you define a member of a class template in a
3485 if (current_class_type
3486 && !TYPE_BEING_DEFINED (current_class_type
)
3487 && DECL_LANG_SPECIFIC (decl
)
3488 /* If this is either a friend defined in the scope of the class
3489 or a member function. */
3490 && (DECL_FUNCTION_MEMBER_P (decl
)
3491 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
3492 : DECL_FRIEND_CONTEXT (decl
)
3493 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
3495 /* And, if it was a member function, it really was defined in
3496 the scope of the class. */
3497 && (!DECL_FUNCTION_MEMBER_P (decl
)
3498 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
3499 /* We already checked these parameters when the template was
3500 declared, so there's no need to do it again now. This function
3501 was defined in class scope, but we're processing it's body now
3502 that the class is complete. */
3505 /* Core issue 226 (C++0x only): the following only applies to class
3507 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
3511 If a template-parameter has a default template-argument, all
3512 subsequent template-parameters shall have a default
3513 template-argument supplied. */
3514 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
3516 tree inner_parms
= TREE_VALUE (parm_level
);
3517 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
3518 int seen_def_arg_p
= 0;
3521 for (i
= 0; i
< ntparms
; ++i
)
3523 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
3525 if (parm
== error_mark_node
)
3528 if (TREE_PURPOSE (parm
))
3530 else if (seen_def_arg_p
)
3532 error ("no default argument for %qD", TREE_VALUE (parm
));
3533 /* For better subsequent error-recovery, we indicate that
3534 there should have been a default argument. */
3535 TREE_PURPOSE (parm
) = error_mark_node
;
3542 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
3546 /* For an ordinary class template, default template arguments are
3547 allowed at the innermost level, e.g.:
3548 template <class T = int>
3550 but, in a partial specialization, they're not allowed even
3551 there, as we have in [temp.class.spec]:
3553 The template parameter list of a specialization shall not
3554 contain default template argument values.
3556 So, for a partial specialization, or for a function template
3557 (in C++98/C++03), we look at all of them. */
3560 /* But, for a primary class template that is not a partial
3561 specialization we look at all template parameters except the
3563 parms
= TREE_CHAIN (parms
);
3565 /* Figure out what error message to issue. */
3566 if (is_friend_decl
== 2)
3567 msg
= "default template arguments may not be used in function template friend re-declaration";
3568 else if (is_friend_decl
)
3569 msg
= "default template arguments may not be used in function template friend declarations";
3570 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
3571 msg
= "default template arguments may not be used in function templates";
3572 else if (is_partial
)
3573 msg
= "default template arguments may not be used in partial specializations";
3575 msg
= "default argument for template parameter for class enclosing %qD";
3577 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
3578 /* If we're inside a class definition, there's no need to
3579 examine the parameters to the class itself. On the one
3580 hand, they will be checked when the class is defined, and,
3581 on the other, default arguments are valid in things like:
3582 template <class T = double>
3583 struct S { template <class U> void f(U); };
3584 Here the default argument for `S' has no bearing on the
3585 declaration of `f'. */
3586 last_level_to_check
= template_class_depth (current_class_type
) + 1;
3588 /* Check everything. */
3589 last_level_to_check
= 0;
3591 for (parm_level
= parms
;
3592 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
3593 parm_level
= TREE_CHAIN (parm_level
))
3595 tree inner_parms
= TREE_VALUE (parm_level
);
3599 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3600 for (i
= 0; i
< ntparms
; ++i
)
3602 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
3605 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
3610 if (is_friend_decl
== 2)
3617 /* Clear out the default argument so that we are not
3619 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
3623 /* At this point, if we're still interested in issuing messages,
3624 they must apply to classes surrounding the object declared. */
3626 msg
= "default argument for template parameter for class enclosing %qD";
3632 /* Worker for push_template_decl_real, called via
3633 for_each_template_parm. DATA is really an int, indicating the
3634 level of the parameters we are interested in. If T is a template
3635 parameter of that level, return nonzero. */
3638 template_parm_this_level_p (tree t
, void* data
)
3640 int this_level
= *(int *)data
;
3643 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3644 level
= TEMPLATE_PARM_LEVEL (t
);
3646 level
= TEMPLATE_TYPE_LEVEL (t
);
3647 return level
== this_level
;
3650 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3651 parameters given by current_template_args, or reuses a
3652 previously existing one, if appropriate. Returns the DECL, or an
3653 equivalent one, if it is replaced via a call to duplicate_decls.
3655 If IS_FRIEND is true, DECL is a friend declaration. */
3658 push_template_decl_real (tree decl
, bool is_friend
)
3666 int new_template_p
= 0;
3667 /* True if the template is a member template, in the sense of
3669 bool member_template_p
= false;
3671 if (decl
== error_mark_node
)
3674 /* See if this is a partial specialization. */
3675 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
3676 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
3677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
3679 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
3683 /* For a friend, we want the context of the friend function, not
3684 the type of which it is a friend. */
3685 ctx
= DECL_CONTEXT (decl
);
3686 else if (CP_DECL_CONTEXT (decl
)
3687 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
3688 /* In the case of a virtual function, we want the class in which
3690 ctx
= CP_DECL_CONTEXT (decl
);
3692 /* Otherwise, if we're currently defining some class, the DECL
3693 is assumed to be a member of the class. */
3694 ctx
= current_scope ();
3696 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
3699 if (!DECL_CONTEXT (decl
))
3700 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
3702 /* See if this is a primary template. */
3703 if (is_friend
&& ctx
)
3704 /* A friend template that specifies a class context, i.e.
3705 template <typename T> friend void A<T>::f();
3709 primary
= template_parm_scope_p ();
3713 if (DECL_CLASS_SCOPE_P (decl
))
3714 member_template_p
= true;
3715 if (TREE_CODE (decl
) == TYPE_DECL
3716 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
3717 error ("template class without a name");
3718 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3720 if (DECL_DESTRUCTOR_P (decl
))
3724 A destructor shall not be a member template. */
3725 error ("destructor %qD declared as member template", decl
);
3726 return error_mark_node
;
3728 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
3729 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
3730 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
3731 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
3732 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
3733 == void_list_node
)))
3735 /* [basic.stc.dynamic.allocation]
3737 An allocation function can be a function
3738 template. ... Template allocation functions shall
3739 have two or more parameters. */
3740 error ("invalid template declaration of %qD", decl
);
3741 return error_mark_node
;
3744 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3745 && CLASS_TYPE_P (TREE_TYPE (decl
)))
3749 error ("template declaration of %q#D", decl
);
3750 return error_mark_node
;
3754 /* Check to see that the rules regarding the use of default
3755 arguments are not being violated. */
3756 check_default_tmpl_args (decl
, current_template_parms
,
3757 primary
, is_partial
, /*is_friend_decl=*/0);
3759 /* Ensure that there are no parameter packs in the type of this
3760 declaration that have not been expanded. */
3761 if (TREE_CODE (decl
) == FUNCTION_DECL
)
3763 /* Check each of the arguments individually to see if there are
3764 any bare parameter packs. */
3765 tree type
= TREE_TYPE (decl
);
3766 tree arg
= DECL_ARGUMENTS (decl
);
3767 tree argtype
= TYPE_ARG_TYPES (type
);
3769 while (arg
&& argtype
)
3771 if (!FUNCTION_PARAMETER_PACK_P (arg
)
3772 && !check_for_bare_parameter_packs (TREE_TYPE (arg
)))
3774 /* This is a PARM_DECL that contains unexpanded parameter
3775 packs. We have already complained about this in the
3776 check_for_bare_parameter_packs call, so just replace
3777 these types with ERROR_MARK_NODE. */
3778 TREE_TYPE (arg
) = error_mark_node
;
3779 TREE_VALUE (argtype
) = error_mark_node
;
3782 arg
= TREE_CHAIN (arg
);
3783 argtype
= TREE_CHAIN (argtype
);
3786 /* Check for bare parameter packs in the return type and the
3787 exception specifiers. */
3788 check_for_bare_parameter_packs (TREE_TYPE (type
));
3789 check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
));
3792 check_for_bare_parameter_packs (TREE_TYPE (decl
));
3795 return process_partial_specialization (decl
);
3797 /* A primary class template can only have one parameter pack, at the
3798 end of the template parameter list. */
3799 if (primary
&& TREE_CODE (decl
) == TYPE_DECL
)
3802 = INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3803 int i
, len
= TREE_VEC_LENGTH (inner_parms
);
3804 for (i
= 0; i
< len
- 1; i
++)
3806 tree parm
= TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
));
3808 if (template_parameter_pack_p (parm
))
3810 if (TREE_CODE (parm
) == PARM_DECL
)
3811 error ("parameter pack %qE must be at the end of the"
3812 " template parameter list", parm
);
3814 error ("parameter pack %qT must be at the end of the"
3815 " template parameter list", TREE_TYPE (parm
));
3820 args
= current_template_args ();
3823 || TREE_CODE (ctx
) == FUNCTION_DECL
3824 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
3825 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
3827 if (DECL_LANG_SPECIFIC (decl
)
3828 && DECL_TEMPLATE_INFO (decl
)
3829 && DECL_TI_TEMPLATE (decl
))
3830 tmpl
= DECL_TI_TEMPLATE (decl
);
3831 /* If DECL is a TYPE_DECL for a class-template, then there won't
3832 be DECL_LANG_SPECIFIC. The information equivalent to
3833 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3834 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3835 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3836 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3838 /* Since a template declaration already existed for this
3839 class-type, we must be redeclaring it here. Make sure
3840 that the redeclaration is valid. */
3841 redeclare_class_template (TREE_TYPE (decl
),
3842 current_template_parms
);
3843 /* We don't need to create a new TEMPLATE_DECL; just use the
3844 one we already had. */
3845 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3849 tmpl
= build_template_decl (decl
, current_template_parms
,
3853 if (DECL_LANG_SPECIFIC (decl
)
3854 && DECL_TEMPLATE_SPECIALIZATION (decl
))
3856 /* A specialization of a member template of a template
3858 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3859 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
3860 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3866 tree a
, t
, current
, parms
;
3869 if (TREE_CODE (decl
) == TYPE_DECL
)
3871 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
3872 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
3873 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3874 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3875 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3878 error ("%qD does not declare a template type", decl
);
3882 else if (!DECL_LANG_SPECIFIC (decl
) || !DECL_TEMPLATE_INFO (decl
))
3884 error ("template definition of non-template %q#D", decl
);
3888 tmpl
= DECL_TI_TEMPLATE (decl
);
3890 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3891 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
3892 && DECL_TEMPLATE_SPECIALIZATION (decl
)
3893 && DECL_MEMBER_TEMPLATE_P (tmpl
))
3897 /* The declaration is a specialization of a member
3898 template, declared outside the class. Therefore, the
3899 innermost template arguments will be NULL, so we
3900 replace them with the arguments determined by the
3901 earlier call to check_explicit_specialization. */
3902 args
= DECL_TI_ARGS (decl
);
3905 = build_template_decl (decl
, current_template_parms
,
3907 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
3908 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
3909 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
3910 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
3911 DECL_TEMPLATE_INFO (new_tmpl
)
3912 = tree_cons (tmpl
, args
, NULL_TREE
);
3914 register_specialization (new_tmpl
,
3915 most_general_template (tmpl
),
3921 /* Make sure the template headers we got make sense. */
3923 parms
= DECL_TEMPLATE_PARMS (tmpl
);
3924 i
= TMPL_PARMS_DEPTH (parms
);
3925 if (TMPL_ARGS_DEPTH (args
) != i
)
3927 error ("expected %d levels of template parms for %q#D, got %d",
3928 i
, decl
, TMPL_ARGS_DEPTH (args
));
3931 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
3933 a
= TMPL_ARGS_LEVEL (args
, i
);
3934 t
= INNERMOST_TEMPLATE_PARMS (parms
);
3936 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
3938 if (current
== decl
)
3939 error ("got %d template parameters for %q#D",
3940 TREE_VEC_LENGTH (a
), decl
);
3942 error ("got %d template parameters for %q#T",
3943 TREE_VEC_LENGTH (a
), current
);
3944 error (" but %d required", TREE_VEC_LENGTH (t
));
3945 return error_mark_node
;
3948 /* Perhaps we should also check that the parms are used in the
3949 appropriate qualifying scopes in the declarator? */
3951 if (current
== decl
)
3954 current
= (TYPE_P (current
)
3955 ? TYPE_CONTEXT (current
)
3956 : DECL_CONTEXT (current
));
3960 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
3961 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
3963 /* Push template declarations for global functions and types. Note
3964 that we do not try to push a global template friend declared in a
3965 template class; such a thing may well depend on the template
3966 parameters of the class. */
3967 if (new_template_p
&& !ctx
3968 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
3970 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
3971 if (tmpl
== error_mark_node
)
3972 return error_mark_node
;
3974 /* Hide template friend classes that haven't been declared yet. */
3975 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
3977 DECL_ANTICIPATED (tmpl
) = 1;
3978 DECL_FRIEND_P (tmpl
) = 1;
3984 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
3985 if (DECL_CONV_FN_P (tmpl
))
3987 int depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3989 /* It is a conversion operator. See if the type converted to
3990 depends on innermost template operands. */
3992 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
3994 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
3998 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3999 back to its most general template. If TMPL is a specialization,
4000 ARGS may only have the innermost set of arguments. Add the missing
4001 argument levels if necessary. */
4002 if (DECL_TEMPLATE_INFO (tmpl
))
4003 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4005 info
= tree_cons (tmpl
, args
, NULL_TREE
);
4007 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4008 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4009 else if (DECL_LANG_SPECIFIC (decl
))
4010 DECL_TEMPLATE_INFO (decl
) = info
;
4012 return DECL_TEMPLATE_RESULT (tmpl
);
4016 push_template_decl (tree decl
)
4018 return push_template_decl_real (decl
, false);
4021 /* Called when a class template TYPE is redeclared with the indicated
4022 template PARMS, e.g.:
4024 template <class T> struct S;
4025 template <class T> struct S {}; */
4028 redeclare_class_template (tree type
, tree parms
)
4034 if (!TYPE_TEMPLATE_INFO (type
))
4036 error ("%qT is not a template type", type
);
4040 tmpl
= TYPE_TI_TEMPLATE (type
);
4041 if (!PRIMARY_TEMPLATE_P (tmpl
))
4042 /* The type is nested in some template class. Nothing to worry
4043 about here; there are no new template parameters for the nested
4049 error ("template specifiers not specified in declaration of %qD",
4054 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4055 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4057 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4059 error ("previous declaration %q+D", tmpl
);
4060 error ("used %d template parameter(s) instead of %d",
4061 TREE_VEC_LENGTH (tmpl_parms
),
4062 TREE_VEC_LENGTH (parms
));
4066 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4073 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4074 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4077 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4078 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4079 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4080 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4082 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4084 if (tmpl_parm
!= error_mark_node
4085 && (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4086 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4087 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))))
4089 error ("template parameter %q+#D", tmpl_parm
);
4090 error ("redeclared here as %q#D", parm
);
4094 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4096 /* We have in [temp.param]:
4098 A template-parameter may not be given default arguments
4099 by two different declarations in the same scope. */
4100 error ("redefinition of default argument for %q#D", parm
);
4101 error ("%J original definition appeared here", tmpl_parm
);
4105 if (parm_default
!= NULL_TREE
)
4106 /* Update the previous template parameters (which are the ones
4107 that will really count) with the new default value. */
4108 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4109 else if (tmpl_default
!= NULL_TREE
)
4110 /* Update the new parameters, too; they'll be used as the
4111 parameters for any members. */
4112 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4118 /* Simplify EXPR if it is a non-dependent expression. Returns the
4119 (possibly simplified) expression. */
4122 fold_non_dependent_expr (tree expr
)
4124 if (expr
== NULL_TREE
)
4127 /* If we're in a template, but EXPR isn't value dependent, simplify
4128 it. We're supposed to treat:
4130 template <typename T> void f(T[1 + 1]);
4131 template <typename T> void f(T[2]);
4133 as two declarations of the same function, for example. */
4134 if (processing_template_decl
4135 && !type_dependent_expression_p (expr
)
4136 && !value_dependent_expression_p (expr
))
4138 HOST_WIDE_INT saved_processing_template_decl
;
4140 saved_processing_template_decl
= processing_template_decl
;
4141 processing_template_decl
= 0;
4142 expr
= tsubst_copy_and_build (expr
,
4145 /*in_decl=*/NULL_TREE
,
4146 /*function_p=*/false,
4147 /*integral_constant_expression_p=*/true);
4148 processing_template_decl
= saved_processing_template_decl
;
4153 /* EXPR is an expression which is used in a constant-expression context.
4154 For instance, it could be a VAR_DECL with a constant initializer.
4155 Extract the innest constant expression.
4157 This is basically a more powerful version of
4158 integral_constant_value, which can be used also in templates where
4159 initializers can maintain a syntactic rather than semantic form
4160 (even if they are non-dependent, for access-checking purposes). */
4163 fold_decl_constant_value (tree expr
)
4165 tree const_expr
= expr
;
4168 expr
= fold_non_dependent_expr (const_expr
);
4169 const_expr
= integral_constant_value (expr
);
4171 while (expr
!= const_expr
);
4176 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4177 must be a function or a pointer-to-function type, as specified
4178 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4179 and check that the resulting function has external linkage. */
4182 convert_nontype_argument_function (tree type
, tree expr
)
4187 fn
= instantiate_type (type
, fns
, tf_none
);
4188 if (fn
== error_mark_node
)
4189 return error_mark_node
;
4192 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4193 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4194 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4195 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4197 /* [temp.arg.nontype]/1
4199 A template-argument for a non-type, non-template template-parameter
4202 -- the address of an object or function with external linkage. */
4203 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4205 error ("%qE is not a valid template argument for type %qT "
4206 "because function %qD has not external linkage",
4207 expr
, type
, fn_no_ptr
);
4214 /* Attempt to convert the non-type template parameter EXPR to the
4215 indicated TYPE. If the conversion is successful, return the
4216 converted value. If the conversion is unsuccessful, return
4217 NULL_TREE if we issued an error message, or error_mark_node if we
4218 did not. We issue error messages for out-and-out bad template
4219 parameters, but not simply because the conversion failed, since we
4220 might be just trying to do argument deduction. Both TYPE and EXPR
4221 must be non-dependent.
4223 The conversion follows the special rules described in
4224 [temp.arg.nontype], and it is much more strict than an implicit
4227 This function is called twice for each template argument (see
4228 lookup_template_class for a more accurate description of this
4229 problem). This means that we need to handle expressions which
4230 are not valid in a C++ source, but can be created from the
4231 first call (for instance, casts to perform conversions). These
4232 hacks can go away after we fix the double coercion problem. */
4235 convert_nontype_argument (tree type
, tree expr
)
4239 /* Detect immediately string literals as invalid non-type argument.
4240 This special-case is not needed for correctness (we would easily
4241 catch this later), but only to provide better diagnostic for this
4242 common user mistake. As suggested by DR 100, we do not mention
4243 linkage issues in the diagnostic as this is not the point. */
4244 if (TREE_CODE (expr
) == STRING_CST
)
4246 error ("%qE is not a valid template argument for type %qT "
4247 "because string literals can never be used in this context",
4252 /* If we are in a template, EXPR may be non-dependent, but still
4253 have a syntactic, rather than semantic, form. For example, EXPR
4254 might be a SCOPE_REF, rather than the VAR_DECL to which the
4255 SCOPE_REF refers. Preserving the qualifying scope is necessary
4256 so that access checking can be performed when the template is
4257 instantiated -- but here we need the resolved form so that we can
4258 convert the argument. */
4259 expr
= fold_non_dependent_expr (expr
);
4260 if (error_operand_p (expr
))
4261 return error_mark_node
;
4262 expr_type
= TREE_TYPE (expr
);
4264 /* HACK: Due to double coercion, we can get a
4265 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4266 which is the tree that we built on the first call (see
4267 below when coercing to reference to object or to reference to
4268 function). We just strip everything and get to the arg.
4269 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4271 if (TREE_CODE (expr
) == NOP_EXPR
)
4273 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
4275 /* ??? Maybe we could use convert_from_reference here, but we
4276 would need to relax its constraints because the NOP_EXPR
4277 could actually change the type to something more cv-qualified,
4278 and this is not folded by convert_from_reference. */
4279 tree addr
= TREE_OPERAND (expr
, 0);
4280 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
4281 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
4282 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
4283 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4284 (TREE_TYPE (expr_type
),
4285 TREE_TYPE (TREE_TYPE (addr
))));
4287 expr
= TREE_OPERAND (addr
, 0);
4288 expr_type
= TREE_TYPE (expr
);
4291 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4292 parameter is a pointer to object, through decay and
4293 qualification conversion. Let's strip everything. */
4294 else if (TYPE_PTROBV_P (type
))
4297 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
4298 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
4299 /* Skip the ADDR_EXPR only if it is part of the decay for
4300 an array. Otherwise, it is part of the original argument
4301 in the source code. */
4302 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
4303 expr
= TREE_OPERAND (expr
, 0);
4304 expr_type
= TREE_TYPE (expr
);
4308 /* [temp.arg.nontype]/5, bullet 1
4310 For a non-type template-parameter of integral or enumeration type,
4311 integral promotions (_conv.prom_) and integral conversions
4312 (_conv.integral_) are applied. */
4313 if (INTEGRAL_TYPE_P (type
))
4315 if (!INTEGRAL_TYPE_P (expr_type
))
4316 return error_mark_node
;
4318 expr
= fold_decl_constant_value (expr
);
4319 /* Notice that there are constant expressions like '4 % 0' which
4320 do not fold into integer constants. */
4321 if (TREE_CODE (expr
) != INTEGER_CST
)
4323 error ("%qE is not a valid template argument for type %qT "
4324 "because it is a non-constant expression", expr
, type
);
4328 /* At this point, an implicit conversion does what we want,
4329 because we already know that the expression is of integral
4331 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
4332 if (expr
== error_mark_node
)
4333 return error_mark_node
;
4335 /* Conversion was allowed: fold it to a bare integer constant. */
4338 /* [temp.arg.nontype]/5, bullet 2
4340 For a non-type template-parameter of type pointer to object,
4341 qualification conversions (_conv.qual_) and the array-to-pointer
4342 conversion (_conv.array_) are applied. */
4343 else if (TYPE_PTROBV_P (type
))
4345 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4347 A template-argument for a non-type, non-template template-parameter
4348 shall be one of: [...]
4350 -- the name of a non-type template-parameter;
4351 -- the address of an object or function with external linkage, [...]
4352 expressed as "& id-expression" where the & is optional if the name
4353 refers to a function or array, or if the corresponding
4354 template-parameter is a reference.
4356 Here, we do not care about functions, as they are invalid anyway
4357 for a parameter of type pointer-to-object. */
4359 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
4360 /* Non-type template parameters are OK. */
4362 else if (TREE_CODE (expr
) != ADDR_EXPR
4363 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
4365 if (TREE_CODE (expr
) == VAR_DECL
)
4367 error ("%qD is not a valid template argument "
4368 "because %qD is a variable, not the address of "
4373 /* Other values, like integer constants, might be valid
4374 non-type arguments of some other type. */
4375 return error_mark_node
;
4381 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
4382 ? TREE_OPERAND (expr
, 0) : expr
);
4383 if (TREE_CODE (decl
) != VAR_DECL
)
4385 error ("%qE is not a valid template argument of type %qT "
4386 "because %qE is not a variable",
4390 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
4392 error ("%qE is not a valid template argument of type %qT "
4393 "because %qD does not have external linkage",
4399 expr
= decay_conversion (expr
);
4400 if (expr
== error_mark_node
)
4401 return error_mark_node
;
4403 expr
= perform_qualification_conversions (type
, expr
);
4404 if (expr
== error_mark_node
)
4405 return error_mark_node
;
4407 /* [temp.arg.nontype]/5, bullet 3
4409 For a non-type template-parameter of type reference to object, no
4410 conversions apply. The type referred to by the reference may be more
4411 cv-qualified than the (otherwise identical) type of the
4412 template-argument. The template-parameter is bound directly to the
4413 template-argument, which must be an lvalue. */
4414 else if (TYPE_REF_OBJ_P (type
))
4416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
4418 return error_mark_node
;
4420 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
4422 error ("%qE is not a valid template argument for type %qT "
4423 "because of conflicts in cv-qualification", expr
, type
);
4427 if (!real_lvalue_p (expr
))
4429 error ("%qE is not a valid template argument for type %qT "
4430 "because it is not an lvalue", expr
, type
);
4434 /* [temp.arg.nontype]/1
4436 A template-argument for a non-type, non-template template-parameter
4437 shall be one of: [...]
4439 -- the address of an object or function with external linkage. */
4440 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
4442 error ("%qE is not a valid template argument for type %qT "
4443 "because object %qD has not external linkage",
4448 expr
= build_nop (type
, build_address (expr
));
4450 /* [temp.arg.nontype]/5, bullet 4
4452 For a non-type template-parameter of type pointer to function, only
4453 the function-to-pointer conversion (_conv.func_) is applied. If the
4454 template-argument represents a set of overloaded functions (or a
4455 pointer to such), the matching function is selected from the set
4457 else if (TYPE_PTRFN_P (type
))
4459 /* If the argument is a template-id, we might not have enough
4460 context information to decay the pointer. */
4461 if (!type_unknown_p (expr_type
))
4463 expr
= decay_conversion (expr
);
4464 if (expr
== error_mark_node
)
4465 return error_mark_node
;
4468 expr
= convert_nontype_argument_function (type
, expr
);
4469 if (!expr
|| expr
== error_mark_node
)
4472 /* [temp.arg.nontype]/5, bullet 5
4474 For a non-type template-parameter of type reference to function, no
4475 conversions apply. If the template-argument represents a set of
4476 overloaded functions, the matching function is selected from the set
4478 else if (TYPE_REFFN_P (type
))
4480 if (TREE_CODE (expr
) == ADDR_EXPR
)
4482 error ("%qE is not a valid template argument for type %qT "
4483 "because it is a pointer", expr
, type
);
4484 inform ("try using %qE instead", TREE_OPERAND (expr
, 0));
4488 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
4489 if (!expr
|| expr
== error_mark_node
)
4492 expr
= build_nop (type
, build_address (expr
));
4494 /* [temp.arg.nontype]/5, bullet 6
4496 For a non-type template-parameter of type pointer to member function,
4497 no conversions apply. If the template-argument represents a set of
4498 overloaded member functions, the matching member function is selected
4499 from the set (_over.over_). */
4500 else if (TYPE_PTRMEMFUNC_P (type
))
4502 expr
= instantiate_type (type
, expr
, tf_none
);
4503 if (expr
== error_mark_node
)
4504 return error_mark_node
;
4506 /* There is no way to disable standard conversions in
4507 resolve_address_of_overloaded_function (called by
4508 instantiate_type). It is possible that the call succeeded by
4509 converting &B::I to &D::I (where B is a base of D), so we need
4510 to reject this conversion here.
4512 Actually, even if there was a way to disable standard conversions,
4513 it would still be better to reject them here so that we can
4514 provide a superior diagnostic. */
4515 if (!same_type_p (TREE_TYPE (expr
), type
))
4517 /* Make sure we are just one standard conversion off. */
4518 gcc_assert (can_convert (type
, TREE_TYPE (expr
)));
4519 error ("%qE is not a valid template argument for type %qT "
4520 "because it is of type %qT", expr
, type
,
4522 inform ("standard conversions are not allowed in this context");
4526 /* [temp.arg.nontype]/5, bullet 7
4528 For a non-type template-parameter of type pointer to data member,
4529 qualification conversions (_conv.qual_) are applied. */
4530 else if (TYPE_PTRMEM_P (type
))
4532 expr
= perform_qualification_conversions (type
, expr
);
4533 if (expr
== error_mark_node
)
4536 /* A template non-type parameter must be one of the above. */
4540 /* Sanity check: did we actually convert the argument to the
4542 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
4547 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4548 template template parameters. Both PARM_PARMS and ARG_PARMS are
4549 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4552 Consider the example:
4553 template <class T> class A;
4554 template<template <class U> class TT> class B;
4556 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4557 the parameters to A, and OUTER_ARGS contains A. */
4560 coerce_template_template_parms (tree parm_parms
,
4562 tsubst_flags_t complain
,
4566 int nparms
, nargs
, i
;
4569 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
4570 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
4572 nparms
= TREE_VEC_LENGTH (parm_parms
);
4573 nargs
= TREE_VEC_LENGTH (arg_parms
);
4575 if (nargs
!= nparms
)
4578 for (i
= 0; i
< nparms
; ++i
)
4580 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
4581 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
4584 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
4585 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
4587 if (arg
== NULL_TREE
|| arg
== error_mark_node
4588 || parm
== NULL_TREE
|| parm
== error_mark_node
)
4591 if (TREE_CODE (arg
) != TREE_CODE (parm
))
4594 switch (TREE_CODE (parm
))
4597 /* We encounter instantiations of templates like
4598 template <template <template <class> class> class TT>
4601 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4602 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
4604 if (!coerce_template_template_parms
4605 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
4611 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))
4612 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
)))
4613 /* One is a parameter pack, the other is not. */
4618 /* The tsubst call is used to handle cases such as
4620 template <int> class C {};
4621 template <class T, template <T> class TT> class D {};
4624 i.e. the parameter list of TT depends on earlier parameters. */
4625 if (!dependent_type_p (TREE_TYPE (arg
))
4627 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
4631 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
4632 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
)))
4633 /* One is a parameter pack, the other is not. */
4644 /* Convert the indicated template ARG as necessary to match the
4645 indicated template PARM. Returns the converted ARG, or
4646 error_mark_node if the conversion was unsuccessful. Error and
4647 warning messages are issued under control of COMPLAIN. This
4648 conversion is for the Ith parameter in the parameter list. ARGS is
4649 the full set of template arguments deduced so far. */
4652 convert_template_argument (tree parm
,
4655 tsubst_flags_t complain
,
4660 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
4661 tree check_arg
= arg
;
4663 if (TREE_CODE (arg
) == TREE_LIST
4664 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
4666 /* The template argument was the name of some
4667 member function. That's usually
4668 invalid, but static members are OK. In any
4669 case, grab the underlying fields/functions
4670 and issue an error later if required. */
4671 arg
= TREE_VALUE (arg
);
4672 TREE_TYPE (arg
) = unknown_type_node
;
4675 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
4676 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
4677 || requires_tmpl_type
);
4679 /* When determining whether an argument pack expansion is a template,
4680 look at the pattern. */
4681 if (TREE_CODE (check_arg
) == TYPE_PACK_EXPANSION
)
4682 check_arg
= PACK_EXPANSION_PATTERN (check_arg
);
4685 ((TREE_CODE (check_arg
) == TEMPLATE_DECL
4686 && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg
)) == TYPE_DECL
)
4687 || TREE_CODE (check_arg
) == TEMPLATE_TEMPLATE_PARM
4688 || TREE_CODE (check_arg
) == UNBOUND_CLASS_TEMPLATE
);
4691 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
4692 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
4693 arg
= TYPE_STUB_DECL (arg
);
4695 is_type
= TYPE_P (arg
) || is_tmpl_type
;
4697 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
4698 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
4700 pedwarn ("to refer to a type member of a template parameter, "
4701 "use %<typename %E%>", arg
);
4703 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
4704 TREE_OPERAND (arg
, 1),
4706 complain
& tf_error
);
4709 if (is_type
!= requires_type
)
4713 if (complain
& tf_error
)
4715 error ("type/value mismatch at argument %d in template "
4716 "parameter list for %qD",
4719 error (" expected a constant of type %qT, got %qT",
4721 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
4722 else if (requires_tmpl_type
)
4723 error (" expected a class template, got %qE", arg
);
4725 error (" expected a type, got %qE", arg
);
4728 return error_mark_node
;
4730 if (is_tmpl_type
^ requires_tmpl_type
)
4732 if (in_decl
&& (complain
& tf_error
))
4734 error ("type/value mismatch at argument %d in template "
4735 "parameter list for %qD",
4738 error (" expected a type, got %qT", DECL_NAME (arg
));
4740 error (" expected a class template, got %qT", arg
);
4742 return error_mark_node
;
4747 if (requires_tmpl_type
)
4749 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
4750 /* The number of argument required is not known yet.
4751 Just accept it for now. */
4752 val
= TREE_TYPE (arg
);
4755 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4759 /* When determining whether a pack expansion is a template,
4760 look at the pattern. */
4761 if (TREE_CODE (check_arg
) == TYPE_PACK_EXPANSION
)
4762 check_arg
= PACK_EXPANSION_PATTERN (check_arg
);
4764 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (check_arg
);
4766 if (coerce_template_template_parms (parmparm
, argparm
,
4772 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4774 if (val
!= error_mark_node
)
4776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
4777 val
= TREE_TYPE (val
);
4778 else if (TREE_CODE (val
) == TYPE_PACK_EXPANSION
4779 && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg
))
4781 val
= TREE_TYPE (check_arg
);
4782 val
= make_pack_expansion (val
);
4788 if (in_decl
&& (complain
& tf_error
))
4790 error ("type/value mismatch at argument %d in "
4791 "template parameter list for %qD",
4793 error (" expected a template of type %qD, got %qD",
4797 val
= error_mark_node
;
4803 /* We only form one instance of each template specialization.
4804 Therefore, if we use a non-canonical variant (i.e., a
4805 typedef), any future messages referring to the type will use
4806 the typedef, which is confusing if those future uses do not
4807 themselves also use the typedef. */
4809 val
= canonical_type_variant (val
);
4813 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
4815 if (invalid_nontype_parm_type_p (t
, complain
))
4816 return error_mark_node
;
4818 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
4819 /* We used to call digest_init here. However, digest_init
4820 will report errors, which we don't want when complain
4821 is zero. More importantly, digest_init will try too
4822 hard to convert things: for example, `0' should not be
4823 converted to pointer type at this point according to
4824 the standard. Accepting this is not merely an
4825 extension, since deciding whether or not these
4826 conversions can occur is part of determining which
4827 function template to call, or whether a given explicit
4828 argument specification is valid. */
4829 val
= convert_nontype_argument (t
, arg
);
4833 if (val
== NULL_TREE
)
4834 val
= error_mark_node
;
4835 else if (val
== error_mark_node
&& (complain
& tf_error
))
4836 error ("could not convert template argument %qE to %qT", arg
, t
);
4842 /* Coerces the remaining template arguments in INNER_ARGS (from
4843 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
4844 Returns the coerced argument pack. PARM_IDX is the position of this
4845 parameter in the template parameter list. ARGS is the original
4846 template argument list. */
4848 coerce_template_parameter_pack (tree parms
,
4856 tsubst_flags_t complain
)
4858 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
4859 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
4862 tree packed_types
= NULL_TREE
;
4864 if (arg_idx
> nargs
)
4867 packed_args
= make_tree_vec (nargs
- arg_idx
);
4869 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
4870 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
4872 /* When the template parameter is a non-type template
4873 parameter pack whose type uses parameter packs, we need
4874 to look at each of the template arguments
4875 separately. Build a vector of the types for these
4876 non-type template parameters in PACKED_TYPES. */
4878 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
4879 packed_types
= tsubst_pack_expansion (expansion
, args
,
4882 if (packed_types
== error_mark_node
)
4883 return error_mark_node
;
4885 /* Check that we have the right number of arguments. */
4887 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
4888 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
4891 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
4892 error ("wrong number of template arguments (%d, should be %d)",
4893 nargs
, needed_parms
);
4894 return error_mark_node
;
4897 /* If we aren't able to check the actual arguments now
4898 (because they haven't been expanded yet), we can at least
4899 verify that all of the types used for the non-type
4900 template parameter pack are, in fact, valid for non-type
4901 template parameters. */
4903 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
4905 int j
, len
= TREE_VEC_LENGTH (packed_types
);
4906 for (j
= 0; j
< len
; ++j
)
4908 tree t
= TREE_VEC_ELT (packed_types
, j
);
4909 if (invalid_nontype_parm_type_p (t
, complain
))
4910 return error_mark_node
;
4915 /* Convert the remaining arguments, which will be a part of the
4916 parameter pack "parm". */
4917 for (; arg_idx
< nargs
; ++arg_idx
)
4919 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
4920 tree actual_parm
= TREE_VALUE (parm
);
4922 if (packed_types
&& !PACK_EXPANSION_P (arg
))
4924 /* When we have a vector of types (corresponding to the
4925 non-type template parameter pack that uses parameter
4926 packs in its type, as mention above), and the
4927 argument is not an expansion (which expands to a
4928 currently unknown number of arguments), clone the
4929 parm and give it the next type in PACKED_TYPES. */
4930 actual_parm
= copy_node (actual_parm
);
4931 TREE_TYPE (actual_parm
) =
4932 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
4935 arg
= convert_template_argument (actual_parm
,
4936 arg
, new_args
, complain
, parm_idx
,
4938 if (arg
== error_mark_node
)
4940 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
4943 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
4944 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
4945 argument_pack
= make_node (TYPE_ARGUMENT_PACK
);
4948 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
4949 TREE_TYPE (argument_pack
) = TREE_TYPE (TREE_VALUE (parm
));
4950 TREE_CONSTANT (argument_pack
) = 1;
4953 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
4954 return argument_pack
;
4957 /* Convert all template arguments to their appropriate types, and
4958 return a vector containing the innermost resulting template
4959 arguments. If any error occurs, return error_mark_node. Error and
4960 warning messages are issued under control of COMPLAIN.
4962 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4963 for arguments not specified in ARGS. Otherwise, if
4964 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4965 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4966 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4970 coerce_template_parms (tree parms
,
4973 tsubst_flags_t complain
,
4974 bool require_all_args
,
4975 bool use_default_args
)
4977 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
4980 tree new_inner_args
;
4981 bool saved_skip_evaluation
;
4983 /* When used as a boolean value, indicates whether this is a
4984 variadic template parameter list. Since it's an int, we can also
4985 subtract it from nparms to get the number of non-variadic
4990 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args
));
4992 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
4993 nparms
= TREE_VEC_LENGTH (parms
);
4995 /* Determine if there are any parameter packs. */
4996 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
4998 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
4999 if (template_parameter_pack_p (tparm
))
5006 if ((nargs
> nparms
- variadic_p
&& !variadic_p
)
5007 || (nargs
< nparms
- variadic_p
5009 && (!use_default_args
5010 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
5011 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
5013 if (complain
& tf_error
)
5015 const char *or_more
= "";
5018 or_more
= " or more";
5022 error ("wrong number of template arguments (%d, should be %d%s)",
5023 nargs
, nparms
, or_more
);
5026 error ("provided for %q+D", in_decl
);
5029 return error_mark_node
;
5032 /* We need to evaluate the template arguments, even though this
5033 template-id may be nested within a "sizeof". */
5034 saved_skip_evaluation
= skip_evaluation
;
5035 skip_evaluation
= false;
5036 new_inner_args
= make_tree_vec (nparms
);
5037 new_args
= add_outermost_template_args (args
, new_inner_args
);
5038 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
5043 /* Get the Ith template parameter. */
5044 parm
= TREE_VEC_ELT (parms
, parm_idx
);
5046 if (parm
== error_mark_node
)
5048 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
5052 /* Calculate the next argument. */
5053 if (template_parameter_pack_p (TREE_VALUE (parm
)))
5055 /* All remaining arguments will be placed in the
5056 template parameter pack PARM. */
5057 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
5058 inner_args
, arg_idx
,
5062 /* Store this argument. */
5063 if (arg
== error_mark_node
)
5065 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
5067 /* We are done with all of the arguments. */
5072 else if (arg_idx
< nargs
)
5074 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5076 if (arg
&& PACK_EXPANSION_P (arg
))
5078 /* If ARG is a pack expansion, but PARM is not a
5079 template parameter pack (if it were, we would have
5080 handled it above), we're trying to expand into a
5081 fixed-length argument list. */
5082 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
5083 error ("cannot expand %<%E%> into a fixed-length "
5084 "argument list", arg
);
5086 error ("cannot expand %<%T%> into a fixed-length "
5087 "argument list", arg
);
5090 else if (require_all_args
)
5091 /* There must be a default arg in this case. */
5092 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
5097 if (arg
== error_mark_node
)
5099 if (complain
& tf_error
)
5100 error ("template argument %d is invalid", arg_idx
+ 1);
5103 /* This only occurs if there was an error in the template
5104 parameter list itself (which we would already have
5105 reported) that we are trying to recover from, e.g., a class
5106 template with a parameter list such as
5107 template<typename..., typename>. */
5108 return error_mark_node
;
5110 arg
= convert_template_argument (TREE_VALUE (parm
),
5111 arg
, new_args
, complain
,
5114 if (arg
== error_mark_node
)
5116 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
5118 skip_evaluation
= saved_skip_evaluation
;
5121 return error_mark_node
;
5123 return new_inner_args
;
5126 /* Returns 1 if template args OT and NT are equivalent. */
5129 template_args_equal (tree ot
, tree nt
)
5134 if (TREE_CODE (nt
) == TREE_VEC
)
5135 /* For member templates */
5136 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
5137 else if (PACK_EXPANSION_P (ot
))
5138 return PACK_EXPANSION_P (nt
)
5139 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
5140 PACK_EXPANSION_PATTERN (nt
));
5141 else if (TYPE_P (nt
))
5142 return TYPE_P (ot
) && same_type_p (ot
, nt
);
5143 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
5146 return cp_tree_equal (ot
, nt
);
5149 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5150 of template arguments. Returns 0 otherwise. */
5153 comp_template_args (tree oldargs
, tree newargs
)
5157 oldargs
= expand_template_argument_pack (oldargs
);
5158 newargs
= expand_template_argument_pack (newargs
);
5160 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
5163 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
5165 tree nt
= TREE_VEC_ELT (newargs
, i
);
5166 tree ot
= TREE_VEC_ELT (oldargs
, i
);
5168 if (! template_args_equal (ot
, nt
))
5175 add_pending_template (tree d
)
5177 tree ti
= (TYPE_P (d
)
5178 ? CLASSTYPE_TEMPLATE_INFO (d
)
5179 : DECL_TEMPLATE_INFO (d
));
5180 struct pending_template
*pt
;
5183 if (TI_PENDING_TEMPLATE_FLAG (ti
))
5186 /* We are called both from instantiate_decl, where we've already had a
5187 tinst_level pushed, and instantiate_template, where we haven't.
5189 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
5192 push_tinst_level (d
);
5194 pt
= GGC_NEW (struct pending_template
);
5196 pt
->tinst
= current_tinst_level
;
5197 if (last_pending_template
)
5198 last_pending_template
->next
= pt
;
5200 pending_templates
= pt
;
5202 last_pending_template
= pt
;
5204 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
5211 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5212 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5213 documentation for TEMPLATE_ID_EXPR. */
5216 lookup_template_function (tree fns
, tree arglist
)
5220 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
5221 return error_mark_node
;
5223 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
5224 gcc_assert (fns
&& (is_overloaded_fn (fns
)
5225 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
5227 if (BASELINK_P (fns
))
5229 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
5231 BASELINK_FUNCTIONS (fns
),
5236 type
= TREE_TYPE (fns
);
5237 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
5238 type
= unknown_type_node
;
5240 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
5243 /* Within the scope of a template class S<T>, the name S gets bound
5244 (in build_self_reference) to a TYPE_DECL for the class, not a
5245 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5246 or one of its enclosing classes, and that type is a template,
5247 return the associated TEMPLATE_DECL. Otherwise, the original
5248 DECL is returned. */
5251 maybe_get_template_decl_from_type_decl (tree decl
)
5253 return (decl
!= NULL_TREE
5254 && TREE_CODE (decl
) == TYPE_DECL
5255 && DECL_ARTIFICIAL (decl
)
5256 && CLASS_TYPE_P (TREE_TYPE (decl
))
5257 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
5258 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
5261 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5262 parameters, find the desired type.
5264 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5266 IN_DECL, if non-NULL, is the template declaration we are trying to
5269 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5270 the class we are looking up.
5272 Issue error and warning messages under control of COMPLAIN.
5274 If the template class is really a local class in a template
5275 function, then the FUNCTION_CONTEXT is the function in which it is
5278 ??? Note that this function is currently called *twice* for each
5279 template-id: the first time from the parser, while creating the
5280 incomplete type (finish_template_type), and the second type during the
5281 real instantiation (instantiate_template_class). This is surely something
5282 that we want to avoid. It also causes some problems with argument
5283 coercion (see convert_nontype_argument for more information on this). */
5286 lookup_template_class (tree d1
,
5291 tsubst_flags_t complain
)
5293 tree
template = NULL_TREE
, parmlist
;
5296 timevar_push (TV_NAME_LOOKUP
);
5298 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
5300 tree value
= innermost_non_namespace_value (d1
);
5301 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
5306 push_decl_namespace (context
);
5307 template = lookup_name (d1
);
5308 template = maybe_get_template_decl_from_type_decl (template);
5310 pop_decl_namespace ();
5313 context
= DECL_CONTEXT (template);
5315 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
5317 tree type
= TREE_TYPE (d1
);
5319 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5320 an implicit typename for the second A. Deal with it. */
5321 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
5322 type
= TREE_TYPE (type
);
5324 if (CLASSTYPE_TEMPLATE_INFO (type
))
5326 template = CLASSTYPE_TI_TEMPLATE (type
);
5327 d1
= DECL_NAME (template);
5330 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
5331 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
5333 template = TYPE_TI_TEMPLATE (d1
);
5334 d1
= DECL_NAME (template);
5336 else if (TREE_CODE (d1
) == TEMPLATE_DECL
5337 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
5340 d1
= DECL_NAME (template);
5341 context
= DECL_CONTEXT (template);
5344 /* Issue an error message if we didn't find a template. */
5347 if (complain
& tf_error
)
5348 error ("%qT is not a template", d1
);
5349 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5352 if (TREE_CODE (template) != TEMPLATE_DECL
5353 /* Make sure it's a user visible template, if it was named by
5355 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (template)
5356 && !PRIMARY_TEMPLATE_P (template)))
5358 if (complain
& tf_error
)
5360 error ("non-template type %qT used as a template", d1
);
5362 error ("for template declaration %q+D", in_decl
);
5364 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5367 complain
&= ~tf_user
;
5369 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5371 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5372 template arguments */
5377 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
5379 /* Consider an example where a template template parameter declared as
5381 template <class T, class U = std::allocator<T> > class TT
5383 The template parameter level of T and U are one level larger than
5384 of TT. To proper process the default argument of U, say when an
5385 instantiation `TT<int>' is seen, we need to build the full
5386 arguments containing {int} as the innermost level. Outer levels,
5387 available when not appearing as default template argument, can be
5388 obtained from `current_template_args ()'.
5390 Suppose that TT is later substituted with std::vector. The above
5391 instantiation is `TT<int, std::allocator<T> >' with TT at
5392 level 1, and T at level 2, while the template arguments at level 1
5393 becomes {std::vector} and the inner level 2 is {int}. */
5395 if (current_template_parms
)
5396 arglist
= add_to_template_args (current_template_args (), arglist
);
5398 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
5400 /*require_all_args=*/true,
5401 /*use_default_args=*/true);
5402 if (arglist2
== error_mark_node
5403 || (!uses_template_parms (arglist2
)
5404 && check_instantiated_args (template, arglist2
, complain
)))
5405 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5407 parm
= bind_template_template_parm (TREE_TYPE (template), arglist2
);
5408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
5412 tree template_type
= TREE_TYPE (template);
5415 tree found
= NULL_TREE
;
5418 int is_partial_instantiation
;
5420 gen_tmpl
= most_general_template (template);
5421 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
5422 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
5423 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5425 if (arg_depth
== 1 && parm_depth
> 1)
5427 /* We've been given an incomplete set of template arguments.
5430 template <class T> struct S1 {
5431 template <class U> struct S2 {};
5432 template <class U> struct S2<U*> {};
5435 we will be called with an ARGLIST of `U*', but the
5436 TEMPLATE will be `template <class T> template
5437 <class U> struct S1<T>::S2'. We must fill in the missing
5440 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5442 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
5445 /* Now we should have enough arguments. */
5446 gcc_assert (parm_depth
== arg_depth
);
5448 /* From here on, we're only interested in the most general
5450 template = gen_tmpl
;
5452 /* Calculate the BOUND_ARGS. These will be the args that are
5453 actually tsubst'd into the definition to create the
5457 /* We have multiple levels of arguments to coerce, at once. */
5459 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
5461 tree bound_args
= make_tree_vec (parm_depth
);
5463 for (i
= saved_depth
,
5464 t
= DECL_TEMPLATE_PARMS (template);
5465 i
> 0 && t
!= NULL_TREE
;
5466 --i
, t
= TREE_CHAIN (t
))
5468 tree a
= coerce_template_parms (TREE_VALUE (t
),
5471 /*require_all_args=*/true,
5472 /*use_default_args=*/true);
5474 /* Don't process further if one of the levels fails. */
5475 if (a
== error_mark_node
)
5477 /* Restore the ARGLIST to its full size. */
5478 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5479 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5482 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
5484 /* We temporarily reduce the length of the ARGLIST so
5485 that coerce_template_parms will see only the arguments
5486 corresponding to the template parameters it is
5488 TREE_VEC_LENGTH (arglist
)--;
5491 /* Restore the ARGLIST to its full size. */
5492 TREE_VEC_LENGTH (arglist
) = saved_depth
;
5494 arglist
= bound_args
;
5498 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
5499 INNERMOST_TEMPLATE_ARGS (arglist
),
5502 /*require_all_args=*/true,
5503 /*use_default_args=*/true);
5505 if (arglist
== error_mark_node
)
5506 /* We were unable to bind the arguments. */
5507 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5509 /* In the scope of a template class, explicit references to the
5510 template class refer to the type of the template, not any
5511 instantiation of it. For example, in:
5513 template <class T> class C { void f(C<T>); }
5515 the `C<T>' is just the same as `C'. Outside of the
5516 class, however, such a reference is an instantiation. */
5517 if (comp_template_args (TYPE_TI_ARGS (template_type
),
5520 found
= template_type
;
5522 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
5526 for (ctx
= current_class_type
;
5527 ctx
&& TREE_CODE (ctx
) != NAMESPACE_DECL
;
5529 ? TYPE_CONTEXT (ctx
)
5530 : DECL_CONTEXT (ctx
)))
5531 if (TYPE_P (ctx
) && same_type_p (ctx
, template_type
))
5534 /* We're not in the scope of the class, so the
5535 TEMPLATE_TYPE is not the type we want after all. */
5541 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5543 /* If we already have this specialization, return it. */
5544 found
= retrieve_specialization (template, arglist
,
5545 /*class_specializations_p=*/false);
5547 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5549 /* This type is a "partial instantiation" if any of the template
5550 arguments still involve template parameters. Note that we set
5551 IS_PARTIAL_INSTANTIATION for partial specializations as
5553 is_partial_instantiation
= uses_template_parms (arglist
);
5555 /* If the deduced arguments are invalid, then the binding
5557 if (!is_partial_instantiation
5558 && check_instantiated_args (template,
5559 INNERMOST_TEMPLATE_ARGS (arglist
),
5561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
5563 if (!is_partial_instantiation
5564 && !PRIMARY_TEMPLATE_P (template)
5565 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
5567 found
= xref_tag_from_type (TREE_TYPE (template),
5568 DECL_NAME (template),
5569 /*tag_scope=*/ts_global
);
5570 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
5573 context
= tsubst (DECL_CONTEXT (template), arglist
,
5576 context
= global_namespace
;
5578 /* Create the type. */
5579 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
5581 if (!is_partial_instantiation
)
5583 set_current_access_from_decl (TYPE_NAME (template_type
));
5584 t
= start_enum (TYPE_IDENTIFIER (template_type
));
5587 /* We don't want to call start_enum for this type, since
5588 the values for the enumeration constants may involve
5589 template parameters. And, no one should be interested
5590 in the enumeration constants for such a type. */
5591 t
= make_node (ENUMERAL_TYPE
);
5595 t
= make_aggr_type (TREE_CODE (template_type
));
5596 CLASSTYPE_DECLARED_CLASS (t
)
5597 = CLASSTYPE_DECLARED_CLASS (template_type
);
5598 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
5599 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
5601 /* A local class. Make sure the decl gets registered properly. */
5602 if (context
== current_function_decl
)
5603 pushtag (DECL_NAME (template), t
, /*tag_scope=*/ts_current
);
5605 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
5606 /* This instantiation is another name for the primary
5607 template type. Set the TYPE_CANONICAL field
5609 TYPE_CANONICAL (t
) = template_type
;
5610 else if (any_template_arguments_need_structural_equality_p (arglist
))
5611 /* Some of the template arguments require structural
5612 equality testing, so this template class requires
5613 structural equality testing. */
5614 SET_TYPE_STRUCTURAL_EQUALITY (t
);
5617 /* If we called start_enum or pushtag above, this information
5618 will already be set up. */
5621 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
5623 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
5624 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
5625 TYPE_STUB_DECL (t
) = type_decl
;
5626 DECL_SOURCE_LOCATION (type_decl
)
5627 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
5630 type_decl
= TYPE_NAME (t
);
5632 TREE_PRIVATE (type_decl
)
5633 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
5634 TREE_PROTECTED (type_decl
)
5635 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
5636 DECL_IN_SYSTEM_HEADER (type_decl
)
5637 = DECL_IN_SYSTEM_HEADER (template);
5638 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
5640 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
5641 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
5644 /* Set up the template information. We have to figure out which
5645 template is the immediate parent if this is a full
5647 if (parm_depth
== 1 || is_partial_instantiation
5648 || !PRIMARY_TEMPLATE_P (template))
5649 /* This case is easy; there are no member templates involved. */
5653 /* This is a full instantiation of a member template. Look
5654 for a partial instantiation of which this is an instance. */
5656 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
5657 found
; found
= TREE_CHAIN (found
))
5660 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
5662 /* We only want partial instantiations, here, not
5663 specializations or full instantiations. */
5664 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
5665 || !uses_template_parms (TREE_VALUE (found
)))
5668 /* Temporarily reduce by one the number of levels in the
5669 ARGLIST and in FOUND so as to avoid comparing the
5670 last set of arguments. */
5671 TREE_VEC_LENGTH (arglist
)--;
5672 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
5674 /* See if the arguments match. If they do, then TMPL is
5675 the partial instantiation we want. */
5676 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
5678 /* Restore the argument vectors to their full size. */
5679 TREE_VEC_LENGTH (arglist
)++;
5680 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
5691 /* There was no partial instantiation. This happens
5692 where C<T> is a member template of A<T> and it's used
5695 template <typename T> struct B { A<T>::C<int> m; };
5698 Create the partial instantiation.
5700 TREE_VEC_LENGTH (arglist
)--;
5701 found
= tsubst (template, arglist
, complain
, NULL_TREE
);
5702 TREE_VEC_LENGTH (arglist
)++;
5706 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
5707 DECL_TEMPLATE_INSTANTIATIONS (template)
5708 = tree_cons (arglist
, t
,
5709 DECL_TEMPLATE_INSTANTIATIONS (template));
5711 if (TREE_CODE (t
) == ENUMERAL_TYPE
5712 && !is_partial_instantiation
)
5713 /* Now that the type has been registered on the instantiations
5714 list, we set up the enumerators. Because the enumeration
5715 constants may involve the enumeration type itself, we make
5716 sure to register the type first, and then create the
5717 constants. That way, doing tsubst_expr for the enumeration
5718 constants won't result in recursive calls here; we'll find
5719 the instantiation and exit above. */
5720 tsubst_enum (template_type
, t
, arglist
);
5722 if (is_partial_instantiation
)
5723 /* If the type makes use of template parameters, the
5724 code that generates debugging information will crash. */
5725 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
5727 /* Possibly limit visibility based on template args. */
5728 TREE_PUBLIC (type_decl
) = 1;
5729 determine_visibility (type_decl
);
5731 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
5733 timevar_pop (TV_NAME_LOOKUP
);
5740 struct pointer_set_t
*visited
;
5743 /* Called from for_each_template_parm via walk_tree. */
5746 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
5749 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
5750 tree_fn_t fn
= pfd
->fn
;
5751 void *data
= pfd
->data
;
5754 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
))
5755 return error_mark_node
;
5757 switch (TREE_CODE (t
))
5760 if (TYPE_PTRMEMFUNC_P (t
))
5766 if (!TYPE_TEMPLATE_INFO (t
))
5768 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
5769 fn
, data
, pfd
->visited
))
5770 return error_mark_node
;
5774 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
5775 fn
, data
, pfd
->visited
)
5776 || for_each_template_parm (TYPE_MAX_VALUE (t
),
5777 fn
, data
, pfd
->visited
))
5778 return error_mark_node
;
5782 /* Since we're not going to walk subtrees, we have to do this
5784 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
5786 return error_mark_node
;
5790 /* Check the return type. */
5791 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
5792 return error_mark_node
;
5794 /* Check the parameter types. Since default arguments are not
5795 instantiated until they are needed, the TYPE_ARG_TYPES may
5796 contain expressions that involve template parameters. But,
5797 no-one should be looking at them yet. And, once they're
5798 instantiated, they don't contain template parameters, so
5799 there's no point in looking at them then, either. */
5803 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
5804 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
5806 return error_mark_node
;
5808 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5809 want walk_tree walking into them itself. */
5815 if (for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
5817 return error_mark_node
;
5822 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
5823 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
5825 return error_mark_node
;
5830 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
5831 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
5833 return error_mark_node
;
5834 if (DECL_CONTEXT (t
)
5835 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
5837 return error_mark_node
;
5840 case BOUND_TEMPLATE_TEMPLATE_PARM
:
5841 /* Record template parameters such as `T' inside `TT<T>'. */
5842 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
))
5843 return error_mark_node
;
5846 case TEMPLATE_TEMPLATE_PARM
:
5847 case TEMPLATE_TYPE_PARM
:
5848 case TEMPLATE_PARM_INDEX
:
5849 if (fn
&& (*fn
)(t
, data
))
5850 return error_mark_node
;
5852 return error_mark_node
;
5856 /* A template template parameter is encountered. */
5857 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
5858 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
5859 return error_mark_node
;
5861 /* Already substituted template template parameter */
5867 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
5868 data
, pfd
->visited
))
5869 return error_mark_node
;
5873 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
5874 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5875 (TREE_TYPE (t
)), fn
, data
,
5877 return error_mark_node
;
5882 /* If there's no type, then this thing must be some expression
5883 involving template parameters. */
5884 if (!fn
&& !TREE_TYPE (t
))
5885 return error_mark_node
;
5890 case REINTERPRET_CAST_EXPR
:
5891 case CONST_CAST_EXPR
:
5892 case STATIC_CAST_EXPR
:
5893 case DYNAMIC_CAST_EXPR
:
5897 case PSEUDO_DTOR_EXPR
:
5899 return error_mark_node
;
5906 /* We didn't find any template parameters we liked. */
5910 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5911 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5912 call FN with the parameter and the DATA.
5913 If FN returns nonzero, the iteration is terminated, and
5914 for_each_template_parm returns 1. Otherwise, the iteration
5915 continues. If FN never returns a nonzero value, the value
5916 returned by for_each_template_parm is 0. If FN is NULL, it is
5917 considered to be the function which always returns 1. */
5920 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
5921 struct pointer_set_t
*visited
)
5923 struct pair_fn_data pfd
;
5930 /* Walk the tree. (Conceptually, we would like to walk without
5931 duplicates, but for_each_template_parm_r recursively calls
5932 for_each_template_parm, so we would need to reorganize a fair
5933 bit to use walk_tree_without_duplicates, so we keep our own
5936 pfd
.visited
= visited
;
5938 pfd
.visited
= pointer_set_create ();
5939 result
= cp_walk_tree (&t
,
5940 for_each_template_parm_r
,
5942 pfd
.visited
) != NULL_TREE
;
5947 pointer_set_destroy (pfd
.visited
);
5954 /* Returns true if T depends on any template parameter. */
5957 uses_template_parms (tree t
)
5960 int saved_processing_template_decl
;
5962 saved_processing_template_decl
= processing_template_decl
;
5963 if (!saved_processing_template_decl
)
5964 processing_template_decl
= 1;
5966 dependent_p
= dependent_type_p (t
);
5967 else if (TREE_CODE (t
) == TREE_VEC
)
5968 dependent_p
= any_dependent_template_arguments_p (t
);
5969 else if (TREE_CODE (t
) == TREE_LIST
)
5970 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
5971 || uses_template_parms (TREE_CHAIN (t
)));
5972 else if (TREE_CODE (t
) == TYPE_DECL
)
5973 dependent_p
= dependent_type_p (TREE_TYPE (t
));
5976 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
5977 || TREE_CODE (t
) == OVERLOAD
5978 || TREE_CODE (t
) == BASELINK
5979 || TREE_CODE (t
) == IDENTIFIER_NODE
5980 || TREE_CODE (t
) == TRAIT_EXPR
5981 || CONSTANT_CLASS_P (t
))
5982 dependent_p
= (type_dependent_expression_p (t
)
5983 || value_dependent_expression_p (t
));
5986 gcc_assert (t
== error_mark_node
);
5987 dependent_p
= false;
5990 processing_template_decl
= saved_processing_template_decl
;
5995 /* Returns true if T depends on any template parameter with level LEVEL. */
5998 uses_template_parms_level (tree t
, int level
)
6000 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
);
6003 static int tinst_depth
;
6004 extern int max_tinst_depth
;
6005 #ifdef GATHER_STATISTICS
6008 static int tinst_level_tick
;
6009 static int last_template_error_tick
;
6011 /* We're starting to instantiate D; record the template instantiation context
6012 for diagnostics and to restore it later. */
6015 push_tinst_level (tree d
)
6017 struct tinst_level
*new;
6019 if (tinst_depth
>= max_tinst_depth
)
6021 /* If the instantiation in question still has unbound template parms,
6022 we don't really care if we can't instantiate it, so just return.
6023 This happens with base instantiation for implicit `typename'. */
6024 if (uses_template_parms (d
))
6027 last_template_error_tick
= tinst_level_tick
;
6028 error ("template instantiation depth exceeds maximum of %d (use "
6029 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6030 max_tinst_depth
, d
);
6032 print_instantiation_context ();
6037 new = GGC_NEW (struct tinst_level
);
6039 new->locus
= input_location
;
6040 new->in_system_header_p
= in_system_header
;
6041 new->next
= current_tinst_level
;
6042 current_tinst_level
= new;
6045 #ifdef GATHER_STATISTICS
6046 if (tinst_depth
> depth_reached
)
6047 depth_reached
= tinst_depth
;
6054 /* We're done instantiating this template; return to the instantiation
6058 pop_tinst_level (void)
6060 /* Restore the filename and line number stashed away when we started
6061 this instantiation. */
6062 input_location
= current_tinst_level
->locus
;
6063 in_system_header
= current_tinst_level
->in_system_header_p
;
6064 current_tinst_level
= current_tinst_level
->next
;
6069 /* We're instantiating a deferred template; restore the template
6070 instantiation context in which the instantiation was requested, which
6071 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6074 reopen_tinst_level (struct tinst_level
*level
)
6076 struct tinst_level
*t
;
6079 for (t
= level
; t
; t
= t
->next
)
6082 current_tinst_level
= level
;
6087 /* Returns the TINST_LEVEL which gives the original instantiation
6090 struct tinst_level
*
6091 outermost_tinst_level (void)
6093 struct tinst_level
*level
= current_tinst_level
;
6096 level
= level
->next
;
6100 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6101 vector of template arguments, as for tsubst.
6103 Returns an appropriate tsubst'd friend declaration. */
6106 tsubst_friend_function (tree decl
, tree args
)
6110 if (TREE_CODE (decl
) == FUNCTION_DECL
6111 && DECL_TEMPLATE_INSTANTIATION (decl
)
6112 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
6113 /* This was a friend declared with an explicit template
6114 argument list, e.g.:
6118 to indicate that f was a template instantiation, not a new
6119 function declaration. Now, we have to figure out what
6120 instantiation of what template. */
6122 tree template_id
, arglist
, fns
;
6125 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
6127 /* Friend functions are looked up in the containing namespace scope.
6128 We must enter that scope, to avoid finding member functions of the
6129 current cless with same name. */
6130 push_nested_namespace (ns
);
6131 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
6132 tf_warning_or_error
, NULL_TREE
,
6133 /*integral_constant_expression_p=*/false);
6134 pop_nested_namespace (ns
);
6135 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
6136 tf_warning_or_error
, NULL_TREE
);
6137 template_id
= lookup_template_function (fns
, arglist
);
6139 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6140 tmpl
= determine_specialization (template_id
, new_friend
,
6142 /*need_member_template=*/0,
6143 TREE_VEC_LENGTH (args
),
6145 return instantiate_template (tmpl
, new_args
, tf_error
);
6148 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
6150 /* The NEW_FRIEND will look like an instantiation, to the
6151 compiler, but is not an instantiation from the point of view of
6152 the language. For example, we might have had:
6154 template <class T> struct S {
6155 template <class U> friend void f(T, U);
6158 Then, in S<int>, template <class U> void f(int, U) is not an
6159 instantiation of anything. */
6160 if (new_friend
== error_mark_node
)
6161 return error_mark_node
;
6163 DECL_USE_TEMPLATE (new_friend
) = 0;
6164 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6166 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
6167 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
6168 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
6171 /* The mangled name for the NEW_FRIEND is incorrect. The function
6172 is not a template instantiation and should not be mangled like
6173 one. Therefore, we forget the mangling here; we'll recompute it
6174 later if we need it. */
6175 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
6177 SET_DECL_RTL (new_friend
, NULL_RTX
);
6178 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
6181 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
6184 tree new_friend_template_info
;
6185 tree new_friend_result_template_info
;
6187 int new_friend_is_defn
;
6189 /* We must save some information from NEW_FRIEND before calling
6190 duplicate decls since that function will free NEW_FRIEND if
6192 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
6193 new_friend_is_defn
=
6194 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6195 (template_for_substitution (new_friend
)))
6197 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
6199 /* This declaration is a `primary' template. */
6200 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
6202 new_friend_result_template_info
6203 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
6206 new_friend_result_template_info
= NULL_TREE
;
6208 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6209 if (new_friend_is_defn
)
6210 DECL_INITIAL (new_friend
) = error_mark_node
;
6212 /* Inside pushdecl_namespace_level, we will push into the
6213 current namespace. However, the friend function should go
6214 into the namespace of the template. */
6215 ns
= decl_namespace_context (new_friend
);
6216 push_nested_namespace (ns
);
6217 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
6218 pop_nested_namespace (ns
);
6220 if (old_decl
== error_mark_node
)
6221 return error_mark_node
;
6223 if (old_decl
!= new_friend
)
6225 /* This new friend declaration matched an existing
6226 declaration. For example, given:
6228 template <class T> void f(T);
6229 template <class U> class C {
6230 template <class T> friend void f(T) {}
6233 the friend declaration actually provides the definition
6234 of `f', once C has been instantiated for some type. So,
6235 old_decl will be the out-of-class template declaration,
6236 while new_friend is the in-class definition.
6238 But, if `f' was called before this point, the
6239 instantiation of `f' will have DECL_TI_ARGS corresponding
6240 to `T' but not to `U', references to which might appear
6241 in the definition of `f'. Previously, the most general
6242 template for an instantiation of `f' was the out-of-class
6243 version; now it is the in-class version. Therefore, we
6244 run through all specialization of `f', adding to their
6245 DECL_TI_ARGS appropriately. In particular, they need a
6246 new set of outer arguments, corresponding to the
6247 arguments for this class instantiation.
6249 The same situation can arise with something like this:
6252 template <class T> class C {
6256 when `C<int>' is instantiated. Now, `f(int)' is defined
6259 if (!new_friend_is_defn
)
6260 /* On the other hand, if the in-class declaration does
6261 *not* provide a definition, then we don't want to alter
6262 existing definitions. We can just leave everything
6267 /* Overwrite whatever template info was there before, if
6268 any, with the new template information pertaining to
6270 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
6272 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
6273 reregister_specialization (new_friend
,
6274 most_general_template (old_decl
),
6279 tree new_friend_args
;
6281 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
6282 = new_friend_result_template_info
;
6284 new_friend_args
= TI_ARGS (new_friend_template_info
);
6285 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
6289 tree spec
= TREE_VALUE (t
);
6292 = add_outermost_template_args (new_friend_args
,
6293 DECL_TI_ARGS (spec
));
6296 /* Now, since specializations are always supposed to
6297 hang off of the most general template, we must move
6299 t
= most_general_template (old_decl
);
6302 DECL_TEMPLATE_SPECIALIZATIONS (t
)
6303 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
6304 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
6305 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
6310 /* The information from NEW_FRIEND has been merged into OLD_DECL
6311 by duplicate_decls. */
6312 new_friend
= old_decl
;
6317 tree context
= DECL_CONTEXT (new_friend
);
6321 template <class T> class C {
6322 template <class U> friend void C1<U>::f (); // case 1
6323 friend void C2<T>::f (); // case 2
6325 we only need to make sure CONTEXT is a complete type for
6326 case 2. To distinguish between the two cases, we note that
6327 CONTEXT of case 1 remains dependent type after tsubst while
6328 this isn't true for case 2. */
6329 ++processing_template_decl
;
6330 dependent_p
= dependent_type_p (context
);
6331 --processing_template_decl
;
6334 && !complete_type_or_else (context
, NULL_TREE
))
6335 return error_mark_node
;
6337 if (COMPLETE_TYPE_P (context
))
6339 /* Check to see that the declaration is really present, and,
6340 possibly obtain an improved declaration. */
6341 tree fn
= check_classfn (context
,
6342 new_friend
, NULL_TREE
);
6352 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6353 template arguments, as for tsubst.
6355 Returns an appropriate tsubst'd friend type or error_mark_node on
6359 tsubst_friend_class (tree friend_tmpl
, tree args
)
6365 context
= DECL_CONTEXT (friend_tmpl
);
6369 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6370 push_nested_namespace (context
);
6372 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
6375 /* Look for a class template declaration. We look for hidden names
6376 because two friend declarations of the same template are the
6377 same. For example, in:
6380 template <typename> friend class F;
6382 template <typename> struct B {
6383 template <typename> friend class F;
6386 both F templates are the same. */
6387 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
6388 /*block_p=*/true, 0,
6389 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
6391 /* But, if we don't find one, it might be because we're in a
6392 situation like this:
6400 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6401 for `S<int>', not the TEMPLATE_DECL. */
6402 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
6404 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
6405 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
6408 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
6410 /* The friend template has already been declared. Just
6411 check to see that the declarations match, and install any new
6412 default parameters. We must tsubst the default parameters,
6413 of course. We only need the innermost template parameters
6414 because that is all that redeclare_class_template will look
6416 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
6417 > TMPL_ARGS_DEPTH (args
))
6420 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
6421 args
, tf_warning_or_error
);
6422 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
6425 friend_type
= TREE_TYPE (tmpl
);
6429 /* The friend template has not already been declared. In this
6430 case, the instantiation of the template class will cause the
6431 injection of this template into the global scope. */
6432 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
6433 if (tmpl
== error_mark_node
)
6434 return error_mark_node
;
6436 /* The new TMPL is not an instantiation of anything, so we
6437 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6438 the new type because that is supposed to be the corresponding
6439 template decl, i.e., TMPL. */
6440 DECL_USE_TEMPLATE (tmpl
) = 0;
6441 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
6442 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
6443 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
6444 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
6446 /* Inject this template into the global scope. */
6447 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
6452 if (TREE_CODE (context
) == NAMESPACE_DECL
)
6453 pop_nested_namespace (context
);
6455 pop_nested_class ();
6461 /* Returns zero if TYPE cannot be completed later due to circularity.
6462 Otherwise returns one. */
6465 can_complete_type_without_circularity (tree type
)
6467 if (type
== NULL_TREE
|| type
== error_mark_node
)
6469 else if (COMPLETE_TYPE_P (type
))
6471 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
6472 return can_complete_type_without_circularity (TREE_TYPE (type
));
6473 else if (CLASS_TYPE_P (type
)
6474 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
6480 /* Apply any attributes which had to be deferred until instantiation
6481 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6482 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6485 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
6486 tree args
, tsubst_flags_t complain
, tree in_decl
)
6488 tree late_attrs
= NULL_TREE
;
6491 if (DECL_P (*decl_p
))
6492 DECL_ATTRIBUTES (*decl_p
) = attributes
;
6494 TYPE_ATTRIBUTES (*decl_p
) = attributes
;
6496 /* Set processing_template_decl so we can check for dependent
6498 ++processing_template_decl
;
6500 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
6501 if (is_late_template_attribute (t
))
6502 late_attrs
= tree_cons
6504 tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
6505 /*integral_constant_expression_p=*/false),
6508 --processing_template_decl
;
6510 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
6514 instantiate_class_template (tree type
)
6516 tree
template, args
, pattern
, t
, member
;
6521 if (type
== error_mark_node
)
6522 return error_mark_node
;
6524 if (TYPE_BEING_DEFINED (type
)
6525 || COMPLETE_TYPE_P (type
)
6526 || dependent_type_p (type
))
6529 /* Figure out which template is being instantiated. */
6530 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
6531 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL
);
6533 /* Determine what specialization of the original template to
6535 t
= most_specialized_class (type
, template);
6536 if (t
== error_mark_node
)
6538 TYPE_BEING_DEFINED (type
) = 1;
6539 return error_mark_node
;
6543 /* This TYPE is actually an instantiation of a partial
6544 specialization. We replace the innermost set of ARGS with
6545 the arguments appropriate for substitution. For example,
6548 template <class T> struct S {};
6549 template <class T> struct S<T*> {};
6551 and supposing that we are instantiating S<int*>, ARGS will
6552 presently be {int*} -- but we need {int}. */
6553 pattern
= TREE_TYPE (t
);
6554 args
= TREE_PURPOSE (t
);
6558 pattern
= TREE_TYPE (template);
6559 args
= CLASSTYPE_TI_ARGS (type
);
6562 /* If the template we're instantiating is incomplete, then clearly
6563 there's nothing we can do. */
6564 if (!COMPLETE_TYPE_P (pattern
))
6567 /* If we've recursively instantiated too many templates, stop. */
6568 if (! push_tinst_level (type
))
6571 /* Now we're really doing the instantiation. Mark the type as in
6572 the process of being defined. */
6573 TYPE_BEING_DEFINED (type
) = 1;
6575 /* We may be in the middle of deferred access check. Disable
6577 push_deferring_access_checks (dk_no_deferred
);
6579 push_to_top_level ();
6581 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
6583 /* Set the input location to the template definition. This is needed
6584 if tsubsting causes an error. */
6585 typedecl
= TYPE_MAIN_DECL (type
);
6586 input_location
= DECL_SOURCE_LOCATION (typedecl
);
6587 in_system_header
= DECL_IN_SYSTEM_HEADER (typedecl
);
6589 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
6590 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
6591 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
6592 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
6593 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
6594 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
6595 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
6596 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
6597 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
6598 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
6599 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
6600 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
6601 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
6602 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
6603 if (ANON_AGGR_TYPE_P (pattern
))
6604 SET_ANON_AGGR_TYPE_P (type
);
6605 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
6607 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
6608 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
6611 pbinfo
= TYPE_BINFO (pattern
);
6613 /* We should never instantiate a nested class before its enclosing
6614 class; we need to look up the nested class by name before we can
6615 instantiate it, and that lookup should instantiate the enclosing
6617 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
6618 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
6619 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
6621 base_list
= NULL_TREE
;
6622 if (BINFO_N_BASE_BINFOS (pbinfo
))
6625 tree context
= TYPE_CONTEXT (type
);
6629 /* We must enter the scope containing the type, as that is where
6630 the accessibility of types named in dependent bases are
6632 pushed_scope
= push_scope (context
? context
: global_namespace
);
6634 /* Substitute into each of the bases to determine the actual
6636 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
6639 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
6640 tree expanded_bases
= NULL_TREE
;
6643 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
6646 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
6647 args
, tf_error
, NULL_TREE
);
6648 if (expanded_bases
== error_mark_node
)
6651 len
= TREE_VEC_LENGTH (expanded_bases
);
6654 for (idx
= 0; idx
< len
; idx
++)
6657 /* Extract the already-expanded base class. */
6658 base
= TREE_VEC_ELT (expanded_bases
, idx
);
6660 /* Substitute to figure out the base class. */
6661 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
6664 if (base
== error_mark_node
)
6667 base_list
= tree_cons (access
, base
, base_list
);
6668 if (BINFO_VIRTUAL_P (pbase_binfo
))
6669 TREE_TYPE (base_list
) = integer_type_node
;
6673 /* The list is now in reverse order; correct that. */
6674 base_list
= nreverse (base_list
);
6677 pop_scope (pushed_scope
);
6679 /* Now call xref_basetypes to set up all the base-class
6681 xref_basetypes (type
, base_list
);
6683 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
6684 (int) ATTR_FLAG_TYPE_IN_PLACE
,
6685 args
, tf_error
, NULL_TREE
);
6687 /* Now that our base classes are set up, enter the scope of the
6688 class, so that name lookups into base classes, etc. will work
6689 correctly. This is precisely analogous to what we do in
6690 begin_class_definition when defining an ordinary non-template
6694 /* Now members are processed in the order of declaration. */
6695 for (member
= CLASSTYPE_DECL_LIST (pattern
);
6696 member
; member
= TREE_CHAIN (member
))
6698 tree t
= TREE_VALUE (member
);
6700 if (TREE_PURPOSE (member
))
6704 /* Build new CLASSTYPE_NESTED_UTDS. */
6707 bool class_template_p
;
6709 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
6710 && TYPE_LANG_SPECIFIC (t
)
6711 && CLASSTYPE_IS_TEMPLATE (t
));
6712 /* If the member is a class template, then -- even after
6713 substitution -- there may be dependent types in the
6714 template argument list for the class. We increment
6715 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6716 that function will assume that no types are dependent
6717 when outside of a template. */
6718 if (class_template_p
)
6719 ++processing_template_decl
;
6720 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
6721 if (class_template_p
)
6722 --processing_template_decl
;
6723 if (newtag
== error_mark_node
)
6726 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
6728 tree name
= TYPE_IDENTIFIER (t
);
6730 if (class_template_p
)
6731 /* Unfortunately, lookup_template_class sets
6732 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6733 instantiation (i.e., for the type of a member
6734 template class nested within a template class.)
6735 This behavior is required for
6736 maybe_process_partial_specialization to work
6737 correctly, but is not accurate in this case;
6738 the TAG is not an instantiation of anything.
6739 (The corresponding TEMPLATE_DECL is an
6740 instantiation, but the TYPE is not.) */
6741 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
6743 /* Now, we call pushtag to put this NEWTAG into the scope of
6744 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6745 pushtag calling push_template_decl. We don't have to do
6746 this for enums because it will already have been done in
6749 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
6750 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
6753 else if (TREE_CODE (t
) == FUNCTION_DECL
6754 || DECL_FUNCTION_TEMPLATE_P (t
))
6756 /* Build new TYPE_METHODS. */
6759 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6760 ++processing_template_decl
;
6761 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
6762 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6763 --processing_template_decl
;
6764 set_current_access_from_decl (r
);
6765 finish_member_declaration (r
);
6769 /* Build new TYPE_FIELDS. */
6770 if (TREE_CODE (t
) == STATIC_ASSERT
)
6773 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
6774 tf_warning_or_error
, NULL_TREE
,
6775 /*integral_constant_expression_p=*/true);
6776 finish_static_assert (condition
,
6777 STATIC_ASSERT_MESSAGE (t
),
6778 STATIC_ASSERT_SOURCE_LOCATION (t
),
6781 else if (TREE_CODE (t
) != CONST_DECL
)
6785 /* The the file and line for this declaration, to
6786 assist in error message reporting. Since we
6787 called push_tinst_level above, we don't need to
6789 input_location
= DECL_SOURCE_LOCATION (t
);
6791 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6792 ++processing_template_decl
;
6793 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
6794 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6795 --processing_template_decl
;
6796 if (TREE_CODE (r
) == VAR_DECL
)
6800 [t]he initialization (and any associated
6801 side-effects) of a static data member does
6802 not occur unless the static data member is
6803 itself used in a way that requires the
6804 definition of the static data member to
6807 Therefore, we do not substitute into the
6808 initialized for the static data member here. */
6809 finish_static_data_member_decl
6812 /*init_const_expr_p=*/false,
6813 /*asmspec_tree=*/NULL_TREE
,
6815 if (DECL_INITIALIZED_IN_CLASS_P (r
))
6816 check_static_variable_definition (r
, TREE_TYPE (r
));
6818 else if (TREE_CODE (r
) == FIELD_DECL
)
6820 /* Determine whether R has a valid type and can be
6821 completed later. If R is invalid, then it is
6822 replaced by error_mark_node so that it will not be
6823 added to TYPE_FIELDS. */
6824 tree rtype
= TREE_TYPE (r
);
6825 if (can_complete_type_without_circularity (rtype
))
6826 complete_type (rtype
);
6828 if (!COMPLETE_TYPE_P (rtype
))
6830 cxx_incomplete_type_error (r
, rtype
);
6831 r
= error_mark_node
;
6835 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6836 such a thing will already have been added to the field
6837 list by tsubst_enum in finish_member_declaration in the
6838 CLASSTYPE_NESTED_UTDS case above. */
6839 if (!(TREE_CODE (r
) == TYPE_DECL
6840 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
6841 && DECL_ARTIFICIAL (r
)))
6843 set_current_access_from_decl (r
);
6844 finish_member_declaration (r
);
6851 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
6853 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6855 tree friend_type
= t
;
6856 bool adjust_processing_template_decl
= false;
6858 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
6860 /* template <class T> friend class C; */
6861 friend_type
= tsubst_friend_class (friend_type
, args
);
6862 adjust_processing_template_decl
= true;
6864 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
6866 /* template <class T> friend class C::D; */
6867 friend_type
= tsubst (friend_type
, args
,
6868 tf_warning_or_error
, NULL_TREE
);
6869 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
6870 friend_type
= TREE_TYPE (friend_type
);
6871 adjust_processing_template_decl
= true;
6873 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
6875 /* This could be either
6879 when dependent_type_p is false or
6881 template <class U> friend class T::C;
6884 friend_type
= tsubst (friend_type
, args
,
6885 tf_warning_or_error
, NULL_TREE
);
6886 /* Bump processing_template_decl for correct
6887 dependent_type_p calculation. */
6888 ++processing_template_decl
;
6889 if (dependent_type_p (friend_type
))
6890 adjust_processing_template_decl
= true;
6891 --processing_template_decl
;
6893 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
6894 && hidden_name_p (TYPE_NAME (friend_type
)))
6898 where C hasn't been declared yet. Let's lookup name
6899 from namespace scope directly, bypassing any name that
6900 come from dependent base class. */
6901 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
6903 /* The call to xref_tag_from_type does injection for friend
6905 push_nested_namespace (ns
);
6907 xref_tag_from_type (friend_type
, NULL_TREE
,
6908 /*tag_scope=*/ts_current
);
6909 pop_nested_namespace (ns
);
6911 else if (uses_template_parms (friend_type
))
6912 /* friend class C<T>; */
6913 friend_type
= tsubst (friend_type
, args
,
6914 tf_warning_or_error
, NULL_TREE
);
6919 where C is already declared or
6921 friend class C<int>;
6923 We don't have to do anything in these cases. */
6925 if (adjust_processing_template_decl
)
6926 /* Trick make_friend_class into realizing that the friend
6927 we're adding is a template, not an ordinary class. It's
6928 important that we use make_friend_class since it will
6929 perform some error-checking and output cross-reference
6931 ++processing_template_decl
;
6933 if (friend_type
!= error_mark_node
)
6934 make_friend_class (type
, friend_type
, /*complain=*/false);
6936 if (adjust_processing_template_decl
)
6937 --processing_template_decl
;
6941 /* Build new DECL_FRIENDLIST. */
6944 /* The the file and line for this declaration, to
6945 assist in error message reporting. Since we
6946 called push_tinst_level above, we don't need to
6948 input_location
= DECL_SOURCE_LOCATION (t
);
6950 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6952 ++processing_template_decl
;
6953 push_deferring_access_checks (dk_no_check
);
6956 r
= tsubst_friend_function (t
, args
);
6957 add_friend (type
, r
, /*complain=*/false);
6958 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6960 pop_deferring_access_checks ();
6961 --processing_template_decl
;
6967 /* Set the file and line number information to whatever is given for
6968 the class itself. This puts error messages involving generated
6969 implicit functions at a predictable point, and the same point
6970 that would be used for non-template classes. */
6971 input_location
= DECL_SOURCE_LOCATION (typedecl
);
6973 unreverse_member_declarations (type
);
6974 finish_struct_1 (type
);
6975 TYPE_BEING_DEFINED (type
) = 0;
6977 /* Now that the class is complete, instantiate default arguments for
6978 any member functions. We don't do this earlier because the
6979 default arguments may reference members of the class. */
6980 if (!PRIMARY_TEMPLATE_P (template))
6981 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
6982 if (TREE_CODE (t
) == FUNCTION_DECL
6983 /* Implicitly generated member functions will not have template
6984 information; they are not instantiations, but instead are
6985 created "fresh" for each instantiation. */
6986 && DECL_TEMPLATE_INFO (t
))
6987 tsubst_default_arguments (t
);
6990 pop_from_top_level ();
6991 pop_deferring_access_checks ();
6994 /* The vtable for a template class can be emitted in any translation
6995 unit in which the class is instantiated. When there is no key
6996 method, however, finish_struct_1 will already have added TYPE to
6997 the keyed_classes list. */
6998 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
6999 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
7005 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7011 else if (TYPE_P (t
))
7012 r
= tsubst (t
, args
, complain
, in_decl
);
7015 r
= tsubst_expr (t
, args
, complain
, in_decl
,
7016 /*integral_constant_expression_p=*/true);
7017 r
= fold_non_dependent_expr (r
);
7022 /* Substitute ARGS into T, which is an pack expansion
7023 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7024 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7025 (if only a partial substitution could be performed) or
7026 ERROR_MARK_NODE if there was an error. */
7028 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
7032 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
7033 tree first_arg_pack
; int i
, len
= -1;
7037 gcc_assert (PACK_EXPANSION_P (t
));
7038 pattern
= PACK_EXPANSION_PATTERN (t
);
7040 /* Determine the argument packs that will instantiate the parameter
7041 packs used in the expansion expression. While we're at it,
7042 compute the number of arguments to be expanded and make sure it
7044 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
7045 pack
= TREE_CHAIN (pack
))
7047 tree parm_pack
= TREE_VALUE (pack
);
7048 tree arg_pack
= NULL_TREE
;
7049 tree orig_arg
= NULL_TREE
;
7051 if (TREE_CODE (parm_pack
) == PARM_DECL
)
7053 if (local_specializations
)
7054 arg_pack
= retrieve_local_specialization (parm_pack
);
7058 int level
, idx
, levels
;
7059 template_parm_level_and_index (parm_pack
, &level
, &idx
);
7061 levels
= TMPL_ARGS_DEPTH (args
);
7062 if (level
<= levels
)
7063 arg_pack
= TMPL_ARG (args
, level
, idx
);
7066 orig_arg
= arg_pack
;
7067 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
7068 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
7070 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
7071 /* This can only happen if we forget to expand an argument
7072 pack somewhere else. Just return an error, silently. */
7074 result
= make_tree_vec (1);
7075 TREE_VEC_ELT (result
, 0) = error_mark_node
;
7082 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
7084 /* It's all-or-nothing with incomplete argument packs. */
7085 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7086 return error_mark_node
;
7088 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
7094 first_arg_pack
= arg_pack
;
7096 else if (len
!= my_len
)
7098 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
7099 error ("mismatched argument pack lengths while expanding "
7103 error ("mismatched argument pack lengths while expanding "
7106 return error_mark_node
;
7109 /* Keep track of the parameter packs and their corresponding
7111 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
7112 TREE_TYPE (packs
) = orig_arg
;
7115 /* We can't substitute for this parameter pack. */
7116 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
7118 unsubstituted_packs
);
7121 /* We cannot expand this expansion expression, because we don't have
7122 all of the argument packs we need. Substitute into the pattern
7123 and return a PACK_EXPANSION_*. The caller will need to deal with
7125 if (unsubstituted_packs
)
7126 return make_pack_expansion (tsubst (pattern
, args
, complain
,
7129 /* We could not find any argument packs that work. */
7131 return error_mark_node
;
7133 /* For each argument in each argument pack, substitute into the
7135 result
= make_tree_vec (len
+ incomplete
);
7136 for (i
= 0; i
< len
+ incomplete
; ++i
)
7138 /* For parameter pack, change the substitution of the parameter
7139 pack to the ith argument in its argument pack, then expand
7141 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7143 tree parm
= TREE_PURPOSE (pack
);
7145 if (TREE_CODE (parm
) == PARM_DECL
)
7147 /* Select the Ith argument from the pack. */
7148 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
7149 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
7150 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
7152 register_local_specialization (arg
, parm
);
7158 template_parm_level_and_index (parm
, &level
, &idx
);
7162 /* Select the Ith argument from the pack. */
7163 value
= make_node (ARGUMENT_PACK_SELECT
);
7164 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
7165 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
7168 /* Update the corresponding argument. */
7169 TMPL_ARG (args
, level
, idx
) = value
;
7173 /* Substitute into the PATTERN with the altered arguments. */
7174 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
7175 TREE_VEC_ELT (result
, i
) =
7176 tsubst_expr (pattern
, args
, complain
, in_decl
,
7177 /*integral_constant_expression_p=*/false);
7179 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
7182 /* When we have incomplete argument packs, the last "expanded"
7183 result is itself a pack expansion, which allows us
7184 to deduce more arguments. */
7185 TREE_VEC_ELT (result
, i
) =
7186 make_pack_expansion (TREE_VEC_ELT (result
, i
));
7188 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
7190 result
= error_mark_node
;
7195 /* Update ARGS to restore the substitution from parameter packs to
7196 their argument packs. */
7197 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
7199 tree parm
= TREE_PURPOSE (pack
);
7201 if (TREE_CODE (parm
) == PARM_DECL
)
7202 register_local_specialization (TREE_TYPE (pack
), parm
);
7206 template_parm_level_and_index (parm
, &level
, &idx
);
7208 /* Update the corresponding argument. */
7209 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
7210 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
7213 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
7220 /* Substitute ARGS into the vector or list of template arguments T. */
7223 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7226 int len
= TREE_VEC_LENGTH (t
);
7227 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
7228 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
7230 for (i
= 0; i
< len
; i
++)
7232 tree orig_arg
= TREE_VEC_ELT (t
, i
);
7235 if (TREE_CODE (orig_arg
) == TREE_VEC
)
7236 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
7237 else if (PACK_EXPANSION_P (orig_arg
))
7239 /* Substitute into an expansion expression. */
7240 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
7242 if (TREE_CODE (new_arg
) == TREE_VEC
)
7243 /* Add to the expanded length adjustment the number of
7244 expanded arguments. We subtract one from this
7245 measurement, because the argument pack expression
7246 itself is already counted as 1 in
7247 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7248 the argument pack is empty. */
7249 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
7251 else if (ARGUMENT_PACK_P (orig_arg
))
7253 /* Substitute into each of the arguments. */
7254 new_arg
= make_node (TREE_CODE (orig_arg
));
7256 SET_ARGUMENT_PACK_ARGS (
7258 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
7259 args
, complain
, in_decl
));
7261 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
7262 new_arg
= error_mark_node
;
7264 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
7265 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
7267 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
7269 if (TREE_TYPE (new_arg
) == error_mark_node
)
7270 new_arg
= error_mark_node
;
7274 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
7276 if (new_arg
== error_mark_node
)
7277 return error_mark_node
;
7280 if (new_arg
!= orig_arg
)
7287 /* Make space for the expanded arguments coming from template
7289 t
= make_tree_vec (len
+ expanded_len_adjust
);
7290 for (i
= 0, out
= 0; i
< len
; i
++)
7292 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
7293 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
7294 && TREE_CODE (elts
[i
]) == TREE_VEC
)
7298 /* Now expand the template argument pack "in place". */
7299 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
7300 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
7304 TREE_VEC_ELT (t
, out
) = elts
[i
];
7312 /* Return the result of substituting ARGS into the template parameters
7313 given by PARMS. If there are m levels of ARGS and m + n levels of
7314 PARMS, then the result will contain n levels of PARMS. For
7315 example, if PARMS is `template <class T> template <class U>
7316 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7317 result will be `template <int*, double, class V>'. */
7320 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
7325 /* When substituting into a template, we must set
7326 PROCESSING_TEMPLATE_DECL as the template parameters may be
7327 dependent if they are based on one-another, and the dependency
7328 predicates are short-circuit outside of templates. */
7329 ++processing_template_decl
;
7331 for (new_parms
= &r
;
7332 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
7333 new_parms
= &(TREE_CHAIN (*new_parms
)),
7334 parms
= TREE_CHAIN (parms
))
7337 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
7340 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
7346 if (parms
== error_mark_node
)
7349 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
7351 if (tuple
== error_mark_node
)
7354 default_value
= TREE_PURPOSE (tuple
);
7355 parm_decl
= TREE_VALUE (tuple
);
7357 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
7358 if (TREE_CODE (parm_decl
) == PARM_DECL
7359 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
7360 parm_decl
= error_mark_node
;
7361 default_value
= tsubst_template_arg (default_value
, args
,
7362 complain
, NULL_TREE
);
7364 tuple
= build_tree_list (default_value
, parm_decl
);
7365 TREE_VEC_ELT (new_vec
, i
) = tuple
;
7369 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
7370 - TMPL_ARGS_DEPTH (args
)),
7371 new_vec
, NULL_TREE
);
7374 --processing_template_decl
;
7379 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7380 type T. If T is not an aggregate or enumeration type, it is
7381 handled as if by tsubst. IN_DECL is as for tsubst. If
7382 ENTERING_SCOPE is nonzero, T is the context for a template which
7383 we are presently tsubst'ing. Return the substituted value. */
7386 tsubst_aggr_type (tree t
,
7388 tsubst_flags_t complain
,
7395 switch (TREE_CODE (t
))
7398 if (TYPE_PTRMEMFUNC_P (t
))
7399 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
7401 /* Else fall through. */
7404 if (TYPE_TEMPLATE_INFO (t
))
7409 bool saved_skip_evaluation
;
7411 /* In "sizeof(X<I>)" we need to evaluate "I". */
7412 saved_skip_evaluation
= skip_evaluation
;
7413 skip_evaluation
= false;
7415 /* First, determine the context for the type we are looking
7417 context
= TYPE_CONTEXT (t
);
7419 context
= tsubst_aggr_type (context
, args
, complain
,
7420 in_decl
, /*entering_scope=*/1);
7422 /* Then, figure out what arguments are appropriate for the
7423 type we are trying to find. For example, given:
7425 template <class T> struct S;
7426 template <class T, class U> void f(T, U) { S<U> su; }
7428 and supposing that we are instantiating f<int, double>,
7429 then our ARGS will be {int, double}, but, when looking up
7430 S we only want {double}. */
7431 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
7433 if (argvec
== error_mark_node
)
7434 r
= error_mark_node
;
7437 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
7438 entering_scope
, complain
);
7439 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
7442 skip_evaluation
= saved_skip_evaluation
;
7447 /* This is not a template type, so there's nothing to do. */
7451 return tsubst (t
, args
, complain
, in_decl
);
7455 /* Substitute into the default argument ARG (a default argument for
7456 FN), which has the indicated TYPE. */
7459 tsubst_default_argument (tree fn
, tree type
, tree arg
)
7461 tree saved_class_ptr
= NULL_TREE
;
7462 tree saved_class_ref
= NULL_TREE
;
7464 /* This default argument came from a template. Instantiate the
7465 default argument here, not in tsubst. In the case of
7474 we must be careful to do name lookup in the scope of S<T>,
7475 rather than in the current class. */
7476 push_access_scope (fn
);
7477 /* The "this" pointer is not valid in a default argument. */
7480 saved_class_ptr
= current_class_ptr
;
7481 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
7482 saved_class_ref
= current_class_ref
;
7483 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
7486 push_deferring_access_checks(dk_no_deferred
);
7487 /* The default argument expression may cause implicitly defined
7488 member functions to be synthesized, which will result in garbage
7489 collection. We must treat this situation as if we were within
7490 the body of function so as to avoid collecting live data on the
7493 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
7494 tf_warning_or_error
, NULL_TREE
,
7495 /*integral_constant_expression_p=*/false);
7497 pop_deferring_access_checks();
7499 /* Restore the "this" pointer. */
7502 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
7503 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
7506 pop_access_scope (fn
);
7508 /* Make sure the default argument is reasonable. */
7509 arg
= check_default_argument (type
, arg
);
7514 /* Substitute into all the default arguments for FN. */
7517 tsubst_default_arguments (tree fn
)
7522 tmpl_args
= DECL_TI_ARGS (fn
);
7524 /* If this function is not yet instantiated, we certainly don't need
7525 its default arguments. */
7526 if (uses_template_parms (tmpl_args
))
7529 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7531 arg
= TREE_CHAIN (arg
))
7532 if (TREE_PURPOSE (arg
))
7533 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
7535 TREE_PURPOSE (arg
));
7538 /* Substitute the ARGS into the T, which is a _DECL. Return the
7539 result of the substitution. Issue error and warning messages under
7540 control of COMPLAIN. */
7543 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
7545 location_t saved_loc
;
7549 /* Set the filename and linenumber to improve error-reporting. */
7550 saved_loc
= input_location
;
7551 input_location
= DECL_SOURCE_LOCATION (t
);
7553 switch (TREE_CODE (t
))
7557 /* We can get here when processing a member function template,
7558 member class template, and template template parameter of
7559 a template class. */
7560 tree decl
= DECL_TEMPLATE_RESULT (t
);
7565 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
7567 /* Template template parameter is treated here. */
7568 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7569 if (new_type
== error_mark_node
)
7570 return error_mark_node
;
7573 TREE_CHAIN (r
) = NULL_TREE
;
7574 TREE_TYPE (r
) = new_type
;
7575 DECL_TEMPLATE_RESULT (r
)
7576 = build_decl (TYPE_DECL
, DECL_NAME (decl
), new_type
);
7577 DECL_TEMPLATE_PARMS (r
)
7578 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
7580 TYPE_NAME (new_type
) = r
;
7584 /* We might already have an instance of this template.
7585 The ARGS are for the surrounding class type, so the
7586 full args contain the tsubst'd args for the context,
7587 plus the innermost args from the template decl. */
7588 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
7589 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
7590 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
7591 /* Because this is a template, the arguments will still be
7592 dependent, even after substitution. If
7593 PROCESSING_TEMPLATE_DECL is not set, the dependency
7594 predicates will short-circuit. */
7595 ++processing_template_decl
;
7596 full_args
= tsubst_template_args (tmpl_args
, args
,
7598 --processing_template_decl
;
7599 if (full_args
== error_mark_node
)
7600 return error_mark_node
;
7602 /* tsubst_template_args doesn't copy the vector if
7603 nothing changed. But, *something* should have
7605 gcc_assert (full_args
!= tmpl_args
);
7607 spec
= retrieve_specialization (t
, full_args
,
7608 /*class_specializations_p=*/true);
7609 if (spec
!= NULL_TREE
)
7615 /* Make a new template decl. It will be similar to the
7616 original, but will record the current template arguments.
7617 We also create a new function declaration, which is just
7618 like the old one, but points to this new template, rather
7619 than the old one. */
7621 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
7622 TREE_CHAIN (r
) = NULL_TREE
;
7624 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
7626 if (TREE_CODE (decl
) == TYPE_DECL
)
7629 ++processing_template_decl
;
7630 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7631 --processing_template_decl
;
7632 if (new_type
== error_mark_node
)
7633 return error_mark_node
;
7635 TREE_TYPE (r
) = new_type
;
7636 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
7637 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
7638 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
7639 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
7644 ++processing_template_decl
;
7645 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
7646 --processing_template_decl
;
7647 if (new_decl
== error_mark_node
)
7648 return error_mark_node
;
7650 DECL_TEMPLATE_RESULT (r
) = new_decl
;
7651 DECL_TI_TEMPLATE (new_decl
) = r
;
7652 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
7653 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
7654 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
7657 SET_DECL_IMPLICIT_INSTANTIATION (r
);
7658 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
7659 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
7661 /* The template parameters for this new template are all the
7662 template parameters for the old template, except the
7663 outermost level of parameters. */
7664 DECL_TEMPLATE_PARMS (r
)
7665 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
7668 if (PRIMARY_TEMPLATE_P (t
))
7669 DECL_PRIMARY_TEMPLATE (r
) = r
;
7671 if (TREE_CODE (decl
) != TYPE_DECL
)
7672 /* Record this non-type partial instantiation. */
7673 register_specialization (r
, t
,
7674 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
7682 tree argvec
= NULL_TREE
;
7690 /* Nobody should be tsubst'ing into non-template functions. */
7691 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
7693 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
7698 /* If T is not dependent, just return it. We have to
7699 increment PROCESSING_TEMPLATE_DECL because
7700 value_dependent_expression_p assumes that nothing is
7701 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7702 ++processing_template_decl
;
7703 dependent_p
= value_dependent_expression_p (t
);
7704 --processing_template_decl
;
7708 /* Calculate the most general template of which R is a
7709 specialization, and the complete set of arguments used to
7711 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
7712 argvec
= tsubst_template_args (DECL_TI_ARGS
7713 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
7714 args
, complain
, in_decl
);
7716 /* Check to see if we already have this specialization. */
7717 spec
= retrieve_specialization (gen_tmpl
, argvec
,
7718 /*class_specializations_p=*/false);
7726 /* We can see more levels of arguments than parameters if
7727 there was a specialization of a member template, like
7730 template <class T> struct S { template <class U> void f(); }
7731 template <> template <class U> void S<int>::f(U);
7733 Here, we'll be substituting into the specialization,
7734 because that's where we can find the code we actually
7735 want to generate, but we'll have enough arguments for
7736 the most general template.
7738 We also deal with the peculiar case:
7740 template <class T> struct S {
7741 template <class U> friend void f();
7743 template <class U> void f() {}
7745 template void f<double>();
7747 Here, the ARGS for the instantiation of will be {int,
7748 double}. But, we only need as many ARGS as there are
7749 levels of template parameters in CODE_PATTERN. We are
7750 careful not to get fooled into reducing the ARGS in
7753 template <class T> struct S { template <class U> void f(U); }
7754 template <class T> template <> void S<T>::f(int) {}
7756 which we can spot because the pattern will be a
7757 specialization in this case. */
7758 args_depth
= TMPL_ARGS_DEPTH (args
);
7760 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
7761 if (args_depth
> parms_depth
7762 && !DECL_TEMPLATE_SPECIALIZATION (t
))
7763 args
= get_innermost_template_args (args
, parms_depth
);
7767 /* This special case arises when we have something like this:
7769 template <class T> struct S {
7770 friend void f<int>(int, double);
7773 Here, the DECL_TI_TEMPLATE for the friend declaration
7774 will be an IDENTIFIER_NODE. We are being called from
7775 tsubst_friend_function, and we want only to create a
7776 new decl (R) with appropriate types so that we can call
7777 determine_specialization. */
7778 gen_tmpl
= NULL_TREE
;
7781 if (DECL_CLASS_SCOPE_P (t
))
7783 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
7787 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
7788 complain
, t
, /*entering_scope=*/1);
7793 ctx
= DECL_CONTEXT (t
);
7795 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7796 if (type
== error_mark_node
)
7797 return error_mark_node
;
7799 /* We do NOT check for matching decls pushed separately at this
7800 point, as they may not represent instantiations of this
7801 template, and in any case are considered separate under the
7804 DECL_USE_TEMPLATE (r
) = 0;
7805 TREE_TYPE (r
) = type
;
7806 /* Clear out the mangled name and RTL for the instantiation. */
7807 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
7808 SET_DECL_RTL (r
, NULL_RTX
);
7809 DECL_INITIAL (r
) = NULL_TREE
;
7810 DECL_CONTEXT (r
) = ctx
;
7812 if (member
&& DECL_CONV_FN_P (r
))
7813 /* Type-conversion operator. Reconstruct the name, in
7814 case it's the name of one of the template's parameters. */
7815 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
7817 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
7819 DECL_RESULT (r
) = NULL_TREE
;
7821 TREE_STATIC (r
) = 0;
7822 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
7823 DECL_EXTERNAL (r
) = 1;
7824 /* If this is an instantiation of a function with internal
7825 linkage, we already know what object file linkage will be
7826 assigned to the instantiation. */
7827 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
7828 DECL_DEFER_OUTPUT (r
) = 0;
7829 TREE_CHAIN (r
) = NULL_TREE
;
7830 DECL_PENDING_INLINE_INFO (r
) = 0;
7831 DECL_PENDING_INLINE_P (r
) = 0;
7832 DECL_SAVED_TREE (r
) = NULL_TREE
;
7834 if (DECL_CLONED_FUNCTION (r
))
7836 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
7838 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
7839 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
7842 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
7843 this in the special friend case mentioned above where
7844 GEN_TMPL is NULL. */
7847 DECL_TEMPLATE_INFO (r
)
7848 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
7849 SET_DECL_IMPLICIT_INSTANTIATION (r
);
7850 register_specialization (r
, gen_tmpl
, argvec
, false);
7852 /* We're not supposed to instantiate default arguments
7853 until they are called, for a template. But, for a
7856 template <class T> void f ()
7857 { extern void g(int i = T()); }
7859 we should do the substitution when the template is
7860 instantiated. We handle the member function case in
7861 instantiate_class_template since the default arguments
7862 might refer to other members of the class. */
7864 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7865 && !uses_template_parms (argvec
))
7866 tsubst_default_arguments (r
);
7869 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
7871 /* Copy the list of befriending classes. */
7872 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
7874 friends
= &TREE_CHAIN (*friends
))
7876 *friends
= copy_node (*friends
);
7877 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
7882 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
7884 maybe_retrofit_in_chrg (r
);
7885 if (DECL_CONSTRUCTOR_P (r
))
7886 grok_ctor_properties (ctx
, r
);
7887 /* If this is an instantiation of a member template, clone it.
7888 If it isn't, that'll be handled by
7889 clone_constructors_and_destructors. */
7890 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
7891 clone_function_decl (r
, /*update_method_vec_p=*/0);
7893 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
))
7894 && !grok_op_properties (r
, (complain
& tf_error
) != 0))
7895 return error_mark_node
;
7897 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
7898 SET_DECL_FRIEND_CONTEXT (r
,
7899 tsubst (DECL_FRIEND_CONTEXT (t
),
7900 args
, complain
, in_decl
));
7902 /* Possibly limit visibility based on template args. */
7903 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
7904 if (DECL_VISIBILITY_SPECIFIED (t
))
7906 DECL_VISIBILITY_SPECIFIED (r
) = 0;
7908 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
7910 determine_visibility (r
);
7912 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
7913 args
, complain
, in_decl
);
7919 tree type
= NULL_TREE
;
7921 tree expanded_types
= NULL_TREE
;
7922 tree prev_r
= NULL_TREE
;
7923 tree first_r
= NULL_TREE
;
7925 if (FUNCTION_PARAMETER_PACK_P (t
))
7927 /* If there is a local specialization that isn't a
7928 parameter pack, it means that we're doing a "simple"
7929 substitution from inside tsubst_pack_expansion. Just
7930 return the local specialization (which will be a single
7932 tree spec
= NULL_TREE
;
7933 if (local_specializations
)
7934 spec
= retrieve_local_specialization (t
);
7936 && TREE_CODE (spec
) == PARM_DECL
7937 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
7940 /* Expand the TYPE_PACK_EXPANSION that provides the types for
7941 the parameters in this function parameter pack. */
7942 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
7944 if (TREE_CODE (expanded_types
) == TREE_VEC
)
7946 len
= TREE_VEC_LENGTH (expanded_types
);
7948 /* Zero-length parameter packs are boring. Just substitute
7951 return tsubst (TREE_CHAIN (t
), args
, complain
,
7956 /* All we did was update the type. Make a note of that. */
7957 type
= expanded_types
;
7958 expanded_types
= NULL_TREE
;
7962 /* Loop through all of the parameter's we'll build. When T is
7963 a function parameter pack, LEN is the number of expanded
7964 types in EXPANDED_TYPES; otherwise, LEN is 1. */
7966 for (i
= 0; i
< len
; ++i
)
7970 if (DECL_TEMPLATE_PARM_P (t
))
7971 SET_DECL_TEMPLATE_PARM_P (r
);
7974 /* We're on the Ith parameter of the function parameter
7977 /* Get the Ith type. */
7978 type
= TREE_VEC_ELT (expanded_types
, i
);
7981 /* Rename the parameter to include the index. */
7983 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
7986 /* We're dealing with a normal parameter. */
7987 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7989 type
= type_decays_to (type
);
7990 TREE_TYPE (r
) = type
;
7991 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
7993 if (DECL_INITIAL (r
))
7995 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
7996 DECL_INITIAL (r
) = TREE_TYPE (r
);
7998 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
8002 DECL_CONTEXT (r
) = NULL_TREE
;
8004 if (!DECL_TEMPLATE_PARM_P (r
))
8005 DECL_ARG_TYPE (r
) = type_passed_as (type
);
8007 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8008 args
, complain
, in_decl
);
8010 /* Keep track of the first new parameter we
8011 generate. That's what will be returned to the
8016 /* Build a proper chain of parameters when substituting
8017 into a function parameter pack. */
8019 TREE_CHAIN (prev_r
) = r
;
8023 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
8024 complain
, TREE_CHAIN (t
));
8026 /* FIRST_R contains the start of the chain we've built. */
8036 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8037 if (type
== error_mark_node
)
8038 return error_mark_node
;
8039 TREE_TYPE (r
) = type
;
8040 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8042 /* DECL_INITIAL gives the number of bits in a bit-field. */
8044 = tsubst_expr (DECL_INITIAL (t
), args
,
8046 /*integral_constant_expression_p=*/true);
8047 /* We don't have to set DECL_CONTEXT here; it is set by
8048 finish_member_declaration. */
8049 TREE_CHAIN (r
) = NULL_TREE
;
8050 if (VOID_TYPE_P (type
))
8051 error ("instantiation of %q+D as type %qT", r
, type
);
8053 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
8054 args
, complain
, in_decl
);
8059 /* We reach here only for member using decls. */
8060 if (DECL_DEPENDENT_P (t
))
8062 r
= do_class_using_decl
8063 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
8064 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
8066 r
= error_mark_node
;
8071 TREE_CHAIN (r
) = NULL_TREE
;
8078 tree argvec
= NULL_TREE
;
8079 tree gen_tmpl
= NULL_TREE
;
8081 tree tmpl
= NULL_TREE
;
8083 tree type
= NULL_TREE
;
8086 if (TREE_CODE (t
) == TYPE_DECL
)
8088 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8089 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
8090 || t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
8092 /* If this is the canonical decl, we don't have to
8093 mess with instantiations, and often we can't (for
8094 typename, template type parms and such). Note that
8095 TYPE_NAME is not correct for the above test if
8096 we've copied the type for a typedef. */
8097 r
= TYPE_NAME (type
);
8102 /* Check to see if we already have the specialization we
8105 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
8107 /* T is a static data member or namespace-scope entity.
8108 We have to substitute into namespace-scope variables
8109 (even though such entities are never templates) because
8112 template <class T> void f() { extern T t; }
8114 where the entity referenced is not known until
8115 instantiation time. */
8117 ctx
= DECL_CONTEXT (t
);
8118 if (DECL_CLASS_SCOPE_P (t
))
8120 ctx
= tsubst_aggr_type (ctx
, args
,
8122 in_decl
, /*entering_scope=*/1);
8123 /* If CTX is unchanged, then T is in fact the
8124 specialization we want. That situation occurs when
8125 referencing a static data member within in its own
8126 class. We can use pointer equality, rather than
8127 same_type_p, because DECL_CONTEXT is always
8129 if (ctx
== DECL_CONTEXT (t
))
8135 tmpl
= DECL_TI_TEMPLATE (t
);
8136 gen_tmpl
= most_general_template (tmpl
);
8137 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
8138 spec
= (retrieve_specialization
8140 /*class_specializations_p=*/false));
8145 /* A local variable. */
8147 /* Subsequent calls to pushdecl will fill this in. */
8149 spec
= retrieve_local_specialization (t
);
8151 /* If we already have the specialization we need, there is
8152 nothing more to do. */
8159 /* Create a new node for the specialization we need. */
8161 if (TREE_CODE (r
) == VAR_DECL
)
8163 /* Even if the original location is out of scope, the
8164 newly substituted one is not. */
8165 DECL_DEAD_FOR_LOCAL (r
) = 0;
8166 DECL_INITIALIZED_P (r
) = 0;
8167 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
8168 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8169 if (type
== error_mark_node
)
8170 return error_mark_node
;
8171 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8173 /* It may seem that this case cannot occur, since:
8178 declares a function, not a variable. However:
8181 template <typename T> void g() { T t; }
8182 template void g<f>();
8184 is an attempt to declare a variable with function
8186 error ("variable %qD has function type",
8187 /* R is not yet sufficiently initialized, so we
8188 just use its name. */
8190 return error_mark_node
;
8192 type
= complete_type (type
);
8193 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
8194 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
8195 type
= check_var_type (DECL_NAME (r
), type
);
8197 if (DECL_HAS_VALUE_EXPR_P (t
))
8199 tree ve
= DECL_VALUE_EXPR (t
);
8200 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
8201 /*constant_expression_p=*/false);
8202 SET_DECL_VALUE_EXPR (r
, ve
);
8205 else if (DECL_SELF_REFERENCE_P (t
))
8206 SET_DECL_SELF_REFERENCE_P (r
);
8207 TREE_TYPE (r
) = type
;
8208 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
8209 DECL_CONTEXT (r
) = ctx
;
8210 /* Clear out the mangled name and RTL for the instantiation. */
8211 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
8212 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8213 SET_DECL_RTL (r
, NULL_RTX
);
8214 /* The initializer must not be expanded until it is required;
8216 DECL_INITIAL (r
) = NULL_TREE
;
8217 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
8218 SET_DECL_RTL (r
, NULL_RTX
);
8219 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
8220 if (TREE_CODE (r
) == VAR_DECL
)
8222 /* Possibly limit visibility based on template args. */
8223 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
8224 if (DECL_VISIBILITY_SPECIFIED (t
))
8226 DECL_VISIBILITY_SPECIFIED (r
) = 0;
8228 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
8230 determine_visibility (r
);
8232 /* Preserve a typedef that names a type. */
8233 else if (TREE_CODE (r
) == TYPE_DECL
8234 && DECL_ORIGINAL_TYPE (t
)
8235 && type
!= error_mark_node
)
8237 DECL_ORIGINAL_TYPE (r
) = tsubst (DECL_ORIGINAL_TYPE (t
),
8238 args
, complain
, in_decl
);
8239 TREE_TYPE (r
) = type
= build_variant_type_copy (type
);
8240 TYPE_NAME (type
) = r
;
8245 /* A static data member declaration is always marked
8246 external when it is declared in-class, even if an
8247 initializer is present. We mimic the non-template
8249 DECL_EXTERNAL (r
) = 1;
8251 register_specialization (r
, gen_tmpl
, argvec
, false);
8252 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
8253 SET_DECL_IMPLICIT_INSTANTIATION (r
);
8256 register_local_specialization (r
, t
);
8258 TREE_CHAIN (r
) = NULL_TREE
;
8260 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
8261 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8262 args
, complain
, in_decl
);
8271 /* Restore the file and line information. */
8272 input_location
= saved_loc
;
8277 /* Substitute into the ARG_TYPES of a function type. */
8280 tsubst_arg_types (tree arg_types
,
8282 tsubst_flags_t complain
,
8285 tree remaining_arg_types
;
8286 tree type
= NULL_TREE
;
8288 tree expanded_args
= NULL_TREE
;
8291 if (!arg_types
|| arg_types
== void_list_node
)
8294 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
8295 args
, complain
, in_decl
);
8296 if (remaining_arg_types
== error_mark_node
)
8297 return error_mark_node
;
8299 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
8301 /* For a pack expansion, perform substitution on the
8302 entire expression. Later on, we'll handle the arguments
8304 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
8305 args
, complain
, in_decl
);
8307 if (TREE_CODE (expanded_args
) == TREE_VEC
)
8308 /* So that we'll spin through the parameters, one by one. */
8309 i
= TREE_VEC_LENGTH (expanded_args
);
8312 /* We only partially substituted into the parameter
8313 pack. Our type is TYPE_PACK_EXPANSION. */
8314 type
= expanded_args
;
8315 expanded_args
= NULL_TREE
;
8323 type
= TREE_VEC_ELT (expanded_args
, i
);
8325 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
8327 if (type
== error_mark_node
)
8328 return error_mark_node
;
8329 if (VOID_TYPE_P (type
))
8331 if (complain
& tf_error
)
8333 error ("invalid parameter type %qT", type
);
8335 error ("in declaration %q+D", in_decl
);
8337 return error_mark_node
;
8340 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8341 top-level qualifiers as required. */
8342 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
8344 /* We do not substitute into default arguments here. The standard
8345 mandates that they be instantiated only when needed, which is
8346 done in build_over_call. */
8347 default_arg
= TREE_PURPOSE (arg_types
);
8349 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
8351 /* We've instantiated a template before its default arguments
8352 have been parsed. This can happen for a nested template
8353 class, and is not an error unless we require the default
8354 argument in a call of this function. */
8355 remaining_arg_types
=
8356 tree_cons (default_arg
, type
, remaining_arg_types
);
8357 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
8358 remaining_arg_types
);
8361 remaining_arg_types
=
8362 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
8365 return remaining_arg_types
;
8368 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8369 *not* handle the exception-specification for FNTYPE, because the
8370 initial substitution of explicitly provided template parameters
8371 during argument deduction forbids substitution into the
8372 exception-specification:
8376 All references in the function type of the function template to the
8377 corresponding template parameters are replaced by the specified tem-
8378 plate argument values. If a substitution in a template parameter or
8379 in the function type of the function template results in an invalid
8380 type, type deduction fails. [Note: The equivalent substitution in
8381 exception specifications is done only when the function is instanti-
8382 ated, at which point a program is ill-formed if the substitution
8383 results in an invalid type.] */
8386 tsubst_function_type (tree t
,
8388 tsubst_flags_t complain
,
8395 /* The TYPE_CONTEXT is not used for function/method types. */
8396 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
8398 /* Substitute the return type. */
8399 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8400 if (return_type
== error_mark_node
)
8401 return error_mark_node
;
8402 /* The standard does not presently indicate that creation of a
8403 function type with an invalid return type is a deduction failure.
8404 However, that is clearly analogous to creating an array of "void"
8405 or a reference to a reference. This is core issue #486. */
8406 if (TREE_CODE (return_type
) == ARRAY_TYPE
8407 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
8409 if (complain
& tf_error
)
8411 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
8412 error ("function returning an array");
8414 error ("function returning a function");
8416 return error_mark_node
;
8419 /* Substitute the argument types. */
8420 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
8422 if (arg_types
== error_mark_node
)
8423 return error_mark_node
;
8425 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
8426 && in_decl
!= NULL_TREE
8427 && !TREE_NO_WARNING (in_decl
)
8428 && (SCALAR_TYPE_P (return_type
) || VOID_TYPE_P (return_type
)))
8429 warning (OPT_Wreturn_type
,
8430 "type qualifiers ignored on function return type");
8432 /* Construct a new type node and return it. */
8433 if (TREE_CODE (t
) == FUNCTION_TYPE
)
8434 fntype
= build_function_type (return_type
, arg_types
);
8437 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
8438 if (! IS_AGGR_TYPE (r
))
8442 Type deduction may fail for any of the following
8445 -- Attempting to create "pointer to member of T" when T
8446 is not a class type. */
8447 if (complain
& tf_error
)
8448 error ("creating pointer to member function of non-class type %qT",
8450 return error_mark_node
;
8453 fntype
= build_method_type_directly (r
, return_type
,
8454 TREE_CHAIN (arg_types
));
8456 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
8457 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
8462 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8463 ARGS into that specification, and return the substituted
8464 specification. If there is no specification, return NULL_TREE. */
8467 tsubst_exception_specification (tree fntype
,
8469 tsubst_flags_t complain
,
8475 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
8476 new_specs
= NULL_TREE
;
8479 if (! TREE_VALUE (specs
))
8486 tree expanded_specs
= NULL_TREE
;
8488 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
8490 /* Expand the pack expansion type. */
8491 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
8494 len
= TREE_VEC_LENGTH (expanded_specs
);
8497 for (i
= 0; i
< len
; ++i
)
8500 spec
= TREE_VEC_ELT (expanded_specs
, i
);
8502 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
8503 if (spec
== error_mark_node
)
8505 new_specs
= add_exception_specifier (new_specs
, spec
,
8509 specs
= TREE_CHAIN (specs
);
8515 /* Take the tree structure T and replace template parameters used
8516 therein with the argument vector ARGS. IN_DECL is an associated
8517 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8518 Issue error and warning messages under control of COMPLAIN. Note
8519 that we must be relatively non-tolerant of extensions here, in
8520 order to preserve conformance; if we allow substitutions that
8521 should not be allowed, we may allow argument deductions that should
8522 not succeed, and therefore report ambiguous overload situations
8523 where there are none. In theory, we could allow the substitution,
8524 but indicate that it should have failed, and allow our caller to
8525 make sure that the right thing happens, but we don't try to do this
8528 This function is used for dealing with types, decls and the like;
8529 for expressions, use tsubst_expr or tsubst_copy. */
8532 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8536 if (t
== NULL_TREE
|| t
== error_mark_node
8537 || t
== integer_type_node
8538 || t
== void_type_node
8539 || t
== char_type_node
8540 || t
== unknown_type_node
8541 || TREE_CODE (t
) == NAMESPACE_DECL
)
8545 return tsubst_decl (t
, args
, complain
);
8547 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
8548 type
= IDENTIFIER_TYPE_VALUE (t
);
8550 type
= TREE_TYPE (t
);
8552 gcc_assert (type
!= unknown_type_node
);
8554 /* Reuse typedefs. This is a rather complicated way to check whether the
8555 type is a typedef from the same class template as the current scope,
8556 but I can't think of a better one.
8558 We need to do this to handle dependent attributes, specifically
8559 attribute aligned. */
8562 && !IS_AGGR_TYPE (t
)
8563 && current_class_type
8564 && CLASSTYPE_TEMPLATE_INFO (current_class_type
))
8566 tree decl
= TYPE_NAME (t
);
8567 tree context
= DECL_CONTEXT (decl
);
8569 && CLASS_TYPE_P (context
)
8570 && CLASSTYPE_TEMPLATE_INFO (context
)
8571 && (CLASSTYPE_TI_TEMPLATE (context
)
8572 == CLASSTYPE_TI_TEMPLATE (current_class_type
))
8573 && (tsubst_aggr_type (context
, args
, complain
, in_decl
,
8574 /*entering_scope=*/0)
8575 == current_class_type
))
8577 r
= lookup_name (DECL_NAME (decl
));
8578 if (r
&& TREE_CODE (r
) == TYPE_DECL
8579 && DECL_CONTEXT (r
) == current_class_type
)
8582 r
= cp_build_qualified_type_real
8583 (r
, cp_type_quals (t
) | cp_type_quals (r
),
8584 complain
| tf_ignore_bad_quals
);
8586 /* Else we're instantiating the typedef, so fall through. */
8592 && TREE_CODE (t
) != TYPENAME_TYPE
8593 && TREE_CODE (t
) != IDENTIFIER_NODE
8594 && TREE_CODE (t
) != FUNCTION_TYPE
8595 && TREE_CODE (t
) != METHOD_TYPE
)
8596 type
= tsubst (type
, args
, complain
, in_decl
);
8597 if (type
== error_mark_node
)
8598 return error_mark_node
;
8600 switch (TREE_CODE (t
))
8605 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
8606 /*entering_scope=*/0);
8609 case IDENTIFIER_NODE
:
8621 if (t
== integer_type_node
)
8624 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
8625 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
8629 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
8631 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
8632 /*integral_constant_expression_p=*/false);
8633 max
= fold_decl_constant_value (max
);
8635 if (TREE_CODE (max
) != INTEGER_CST
8636 && TREE_CODE (max
) != TEMPLATE_PARM_INDEX
8637 && !at_function_scope_p ())
8639 if (complain
& tf_error
)
8640 error ("array bound is not an integer constant");
8641 return error_mark_node
;
8646 Type deduction may fail for any of the following
8649 Attempting to create an array with a size that is
8650 zero or negative. */
8651 if (integer_zerop (max
) && !(complain
& tf_error
))
8652 /* We must fail if performing argument deduction (as
8653 indicated by the state of complain), so that
8654 another substitution can be found. */
8655 return error_mark_node
;
8656 else if (TREE_CODE (max
) == INTEGER_CST
8657 && INT_CST_LT (max
, integer_zero_node
))
8659 if (complain
& tf_error
)
8660 error ("creating array with negative size (%qE)", max
);
8662 return error_mark_node
;
8665 return compute_array_index_type (NULL_TREE
, max
);
8668 case TEMPLATE_TYPE_PARM
:
8669 case TEMPLATE_TEMPLATE_PARM
:
8670 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8671 case TEMPLATE_PARM_INDEX
:
8676 tree arg
= NULL_TREE
;
8680 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
8681 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
8682 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
8683 || TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8685 idx
= TEMPLATE_TYPE_IDX (t
);
8686 level
= TEMPLATE_TYPE_LEVEL (t
);
8690 idx
= TEMPLATE_PARM_IDX (t
);
8691 level
= TEMPLATE_PARM_LEVEL (t
);
8694 levels
= TMPL_ARGS_DEPTH (args
);
8695 if (level
<= levels
)
8697 arg
= TMPL_ARG (args
, level
, idx
);
8699 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
8700 /* See through ARGUMENT_PACK_SELECT arguments. */
8701 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
8704 if (arg
== error_mark_node
)
8705 return error_mark_node
;
8706 else if (arg
!= NULL_TREE
)
8708 if (ARGUMENT_PACK_P (arg
))
8709 /* If ARG is an argument pack, we don't actually want to
8710 perform a substitution here, because substitutions
8711 for argument packs are only done
8712 element-by-element. We can get to this point when
8713 substituting the type of a non-type template
8714 parameter pack, when that type actually contains
8715 template parameter packs from an outer template, e.g.,
8717 template<typename... Types> struct A {
8718 template<Types... Values> struct B { };
8722 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
8725 gcc_assert (TYPE_P (arg
));
8727 /* cv-quals from the template are discarded when
8728 substituting in a function or reference type. */
8729 if (TREE_CODE (arg
) == FUNCTION_TYPE
8730 || TREE_CODE (arg
) == METHOD_TYPE
8731 || TREE_CODE (arg
) == REFERENCE_TYPE
)
8732 quals
= cp_type_quals (arg
);
8734 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
8736 return cp_build_qualified_type_real
8737 (arg
, quals
, complain
| tf_ignore_bad_quals
);
8739 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8741 /* We are processing a type constructed from a
8742 template template parameter. */
8743 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
8744 args
, complain
, in_decl
);
8745 if (argvec
== error_mark_node
)
8746 return error_mark_node
;
8748 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
8749 are resolving nested-types in the signature of a
8750 member function templates. Otherwise ARG is a
8751 TEMPLATE_DECL and is the real template to be
8753 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
8754 arg
= TYPE_NAME (arg
);
8756 r
= lookup_template_class (arg
,
8759 /*entering_scope=*/0,
8761 return cp_build_qualified_type_real
8762 (r
, TYPE_QUALS (t
), complain
);
8765 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
8770 /* This can happen during the attempted tsubst'ing in
8771 unify. This means that we don't yet have any information
8772 about the template parameter in question. */
8775 /* If we get here, we must have been looking at a parm for a
8776 more deeply nested template. Make a new version of this
8777 template parameter, but with a lower level. */
8778 switch (TREE_CODE (t
))
8780 case TEMPLATE_TYPE_PARM
:
8781 case TEMPLATE_TEMPLATE_PARM
:
8782 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8783 if (cp_type_quals (t
))
8785 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
8786 r
= cp_build_qualified_type_real
8787 (r
, cp_type_quals (t
),
8788 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
8789 ? tf_ignore_bad_quals
: 0));
8794 TEMPLATE_TYPE_PARM_INDEX (r
)
8795 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
8797 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
8798 TYPE_MAIN_VARIANT (r
) = r
;
8799 TYPE_POINTER_TO (r
) = NULL_TREE
;
8800 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
8802 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
8803 /* We have reduced the level of the template
8804 template parameter, but not the levels of its
8805 template parameters, so canonical_type_parameter
8806 will not be able to find the canonical template
8807 template parameter for this level. Thus, we
8808 require structural equality checking to compare
8809 TEMPLATE_TEMPLATE_PARMs. */
8810 SET_TYPE_STRUCTURAL_EQUALITY (r
);
8811 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
8812 SET_TYPE_STRUCTURAL_EQUALITY (r
);
8814 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
8816 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
8818 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
8820 if (argvec
== error_mark_node
)
8821 return error_mark_node
;
8823 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
8824 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
8829 case TEMPLATE_PARM_INDEX
:
8830 r
= reduce_template_parm_level (t
, type
, levels
);
8842 tree purpose
, value
, chain
;
8844 if (t
== void_list_node
)
8847 purpose
= TREE_PURPOSE (t
);
8850 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
8851 if (purpose
== error_mark_node
)
8852 return error_mark_node
;
8854 value
= TREE_VALUE (t
);
8857 value
= tsubst (value
, args
, complain
, in_decl
);
8858 if (value
== error_mark_node
)
8859 return error_mark_node
;
8861 chain
= TREE_CHAIN (t
);
8862 if (chain
&& chain
!= void_type_node
)
8864 chain
= tsubst (chain
, args
, complain
, in_decl
);
8865 if (chain
== error_mark_node
)
8866 return error_mark_node
;
8868 if (purpose
== TREE_PURPOSE (t
)
8869 && value
== TREE_VALUE (t
)
8870 && chain
== TREE_CHAIN (t
))
8872 return hash_tree_cons (purpose
, value
, chain
);
8876 /* We should never be tsubsting a binfo. */
8880 /* A vector of template arguments. */
8882 return tsubst_template_args (t
, args
, complain
, in_decl
);
8885 case REFERENCE_TYPE
:
8887 enum tree_code code
;
8889 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
8892 code
= TREE_CODE (t
);
8897 Type deduction may fail for any of the following
8900 -- Attempting to create a pointer to reference type.
8901 -- Attempting to create a reference to a reference type or
8902 a reference to void.
8904 Core issue 106 says that creating a reference to a reference
8905 during instantiation is no longer a cause for failure. We
8906 only enforce this check in strict C++98 mode. */
8907 if ((TREE_CODE (type
) == REFERENCE_TYPE
8908 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
8909 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
8911 static location_t last_loc
;
8913 /* We keep track of the last time we issued this error
8914 message to avoid spewing a ton of messages during a
8915 single bad template instantiation. */
8916 if (complain
& tf_error
8917 #ifdef USE_MAPPED_LOCATION
8918 && last_loc
!= input_location
8920 && (last_loc
.line
!= input_line
8921 || last_loc
.file
!= input_filename
)
8925 if (TREE_CODE (type
) == VOID_TYPE
)
8926 error ("forming reference to void");
8928 error ("forming %s to reference type %qT",
8929 (code
== POINTER_TYPE
) ? "pointer" : "reference",
8931 last_loc
= input_location
;
8934 return error_mark_node
;
8936 else if (code
== POINTER_TYPE
)
8938 r
= build_pointer_type (type
);
8939 if (TREE_CODE (type
) == METHOD_TYPE
)
8940 r
= build_ptrmemfunc_type (r
);
8942 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
8943 /* In C++0x, during template argument substitution, when there is an
8944 attempt to create a reference to a reference type, reference
8945 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
8947 "If a template-argument for a template-parameter T names a type
8948 that is a reference to a type A, an attempt to create the type
8949 'lvalue reference to cv T' creates the type 'lvalue reference to
8950 A,' while an attempt to create the type type rvalue reference to
8951 cv T' creates the type T"
8953 r
= cp_build_reference_type
8955 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
8957 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
8958 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
8960 if (r
!= error_mark_node
)
8961 /* Will this ever be needed for TYPE_..._TO values? */
8968 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
8969 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
8973 Type deduction may fail for any of the following
8976 -- Attempting to create "pointer to member of T" when T
8977 is not a class type. */
8978 if (complain
& tf_error
)
8979 error ("creating pointer to member of non-class type %qT", r
);
8980 return error_mark_node
;
8982 if (TREE_CODE (type
) == REFERENCE_TYPE
)
8984 if (complain
& tf_error
)
8985 error ("creating pointer to member reference type %qT", type
);
8986 return error_mark_node
;
8988 if (TREE_CODE (type
) == VOID_TYPE
)
8990 if (complain
& tf_error
)
8991 error ("creating pointer to member of type void");
8992 return error_mark_node
;
8994 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
8995 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8997 /* The type of the implicit object parameter gets its
8998 cv-qualifiers from the FUNCTION_TYPE. */
9000 tree this_type
= cp_build_qualified_type (TYPE_MAIN_VARIANT (r
),
9001 cp_type_quals (type
));
9003 method_type
= build_method_type_directly (this_type
,
9005 TYPE_ARG_TYPES (type
));
9006 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
9007 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
9011 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
9020 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
9021 if (fntype
== error_mark_node
)
9022 return error_mark_node
;
9024 /* Substitute the exception specification. */
9025 specs
= tsubst_exception_specification (t
, args
, complain
,
9027 if (specs
== error_mark_node
)
9028 return error_mark_node
;
9030 fntype
= build_exception_variant (fntype
, specs
);
9035 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
9036 if (domain
== error_mark_node
)
9037 return error_mark_node
;
9039 /* As an optimization, we avoid regenerating the array type if
9040 it will obviously be the same as T. */
9041 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
9044 /* These checks should match the ones in grokdeclarator.
9048 The deduction may fail for any of the following reasons:
9050 -- Attempting to create an array with an element type that
9051 is void, a function type, or a reference type, or [DR337]
9052 an abstract class type. */
9053 if (TREE_CODE (type
) == VOID_TYPE
9054 || TREE_CODE (type
) == FUNCTION_TYPE
9055 || TREE_CODE (type
) == REFERENCE_TYPE
)
9057 if (complain
& tf_error
)
9058 error ("creating array of %qT", type
);
9059 return error_mark_node
;
9061 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
9063 if (complain
& tf_error
)
9064 error ("creating array of %qT, which is an abstract class type",
9066 return error_mark_node
;
9069 r
= build_cplus_array_type (type
, domain
);
9071 if (TYPE_USER_ALIGN (t
))
9073 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
9074 TYPE_USER_ALIGN (r
) = 1;
9083 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9084 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9086 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9087 return error_mark_node
;
9089 return fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
9095 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9096 if (e
== error_mark_node
)
9097 return error_mark_node
;
9099 return fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), e
);
9104 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9105 in_decl
, /*entering_scope=*/1);
9106 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
9109 if (ctx
== error_mark_node
|| f
== error_mark_node
)
9110 return error_mark_node
;
9112 if (!IS_AGGR_TYPE (ctx
))
9114 if (complain
& tf_error
)
9115 error ("%qT is not a class, struct, or union type", ctx
);
9116 return error_mark_node
;
9118 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
9120 /* Normally, make_typename_type does not require that the CTX
9121 have complete type in order to allow things like:
9123 template <class T> struct S { typename S<T>::X Y; };
9125 But, such constructs have already been resolved by this
9126 point, so here CTX really should have complete type, unless
9127 it's a partial instantiation. */
9128 ctx
= complete_type (ctx
);
9129 if (!COMPLETE_TYPE_P (ctx
))
9131 if (complain
& tf_error
)
9132 cxx_incomplete_type_error (NULL_TREE
, ctx
);
9133 return error_mark_node
;
9137 f
= make_typename_type (ctx
, f
, typename_type
,
9138 (complain
& tf_error
) | tf_keep_type_decl
);
9139 if (f
== error_mark_node
)
9141 if (TREE_CODE (f
) == TYPE_DECL
)
9143 complain
|= tf_ignore_bad_quals
;
9147 if (TREE_CODE (f
) != TYPENAME_TYPE
)
9149 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
9150 error ("%qT resolves to %qT, which is not an enumeration type",
9152 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
9153 error ("%qT resolves to %qT, which is is not a class type",
9157 return cp_build_qualified_type_real
9158 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
9161 case UNBOUND_CLASS_TEMPLATE
:
9163 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
9164 in_decl
, /*entering_scope=*/1);
9165 tree name
= TYPE_IDENTIFIER (t
);
9166 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
9168 if (ctx
== error_mark_node
|| name
== error_mark_node
)
9169 return error_mark_node
;
9172 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
9173 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
9183 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9184 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
9185 /*integral_constant_expression_p=*/false);
9186 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9187 return error_mark_node
;
9189 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
9194 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9195 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
9196 if (e1
== error_mark_node
|| e2
== error_mark_node
)
9197 return error_mark_node
;
9199 return build_qualified_name (/*type=*/NULL_TREE
,
9200 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
9207 type
= finish_typeof (tsubst_expr
9208 (TYPEOF_TYPE_EXPR (t
), args
,
9210 /*integral_constant_expression_p=*/false));
9211 return cp_build_qualified_type_real (type
,
9213 | cp_type_quals (type
),
9222 finish_decltype_type (tsubst_expr
9223 (DECLTYPE_TYPE_EXPR (t
), args
,
9225 /*integral_constant_expression_p=*/false),
9226 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
9227 return cp_build_qualified_type_real (type
,
9229 | cp_type_quals (type
),
9233 case TYPE_ARGUMENT_PACK
:
9234 case NONTYPE_ARGUMENT_PACK
:
9236 tree r
= make_node (TREE_CODE (t
));
9238 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
9242 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
9244 /* For template nontype argument packs, also substitute into
9246 if (TREE_CODE (t
) == NONTYPE_ARGUMENT_PACK
)
9247 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9254 sorry ("use of %qs in template",
9255 tree_code_name
[(int) TREE_CODE (t
)]);
9256 return error_mark_node
;
9260 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9261 type of the expression on the left-hand side of the "." or "->"
9265 tsubst_baselink (tree baselink
, tree object_type
,
9266 tree args
, tsubst_flags_t complain
, tree in_decl
)
9269 tree qualifying_scope
;
9272 tree template_args
= 0;
9273 bool template_id_p
= false;
9275 /* A baselink indicates a function from a base class. Both the
9276 BASELINK_ACCESS_BINFO and the base class referenced may
9277 indicate bases of the template class, rather than the
9278 instantiated class. In addition, lookups that were not
9279 ambiguous before may be ambiguous now. Therefore, we perform
9280 the lookup again. */
9281 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
9282 qualifying_scope
= tsubst (qualifying_scope
, args
,
9284 fns
= BASELINK_FUNCTIONS (baselink
);
9285 optype
= BASELINK_OPTYPE (baselink
);
9286 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
9288 template_id_p
= true;
9289 template_args
= TREE_OPERAND (fns
, 1);
9290 fns
= TREE_OPERAND (fns
, 0);
9292 template_args
= tsubst_template_args (template_args
, args
,
9295 name
= DECL_NAME (get_first_fn (fns
));
9296 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
9298 /* If lookup found a single function, mark it as used at this
9299 point. (If it lookup found multiple functions the one selected
9300 later by overload resolution will be marked as used at that
9302 if (BASELINK_P (baselink
))
9303 fns
= BASELINK_FUNCTIONS (baselink
);
9304 if (!template_id_p
&& !really_overloaded_fn (fns
))
9305 mark_used (OVL_CURRENT (fns
));
9307 /* Add back the template arguments, if present. */
9308 if (BASELINK_P (baselink
) && template_id_p
)
9309 BASELINK_FUNCTIONS (baselink
)
9310 = build_nt (TEMPLATE_ID_EXPR
,
9311 BASELINK_FUNCTIONS (baselink
),
9313 /* Update the conversion operator type. */
9314 BASELINK_OPTYPE (baselink
)
9315 = tsubst (optype
, args
, complain
, in_decl
);
9318 object_type
= current_class_type
;
9319 return adjust_result_of_qualified_name_lookup (baselink
,
9324 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9325 true if the qualified-id will be a postfix-expression in-and-of
9326 itself; false if more of the postfix-expression follows the
9327 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9331 tsubst_qualified_id (tree qualified_id
, tree args
,
9332 tsubst_flags_t complain
, tree in_decl
,
9333 bool done
, bool address_p
)
9341 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
9343 /* Figure out what name to look up. */
9344 name
= TREE_OPERAND (qualified_id
, 1);
9345 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
9348 template_args
= TREE_OPERAND (name
, 1);
9350 template_args
= tsubst_template_args (template_args
, args
,
9352 name
= TREE_OPERAND (name
, 0);
9356 is_template
= false;
9357 template_args
= NULL_TREE
;
9360 /* Substitute into the qualifying scope. When there are no ARGS, we
9361 are just trying to simplify a non-dependent expression. In that
9362 case the qualifying scope may be dependent, and, in any case,
9363 substituting will not help. */
9364 scope
= TREE_OPERAND (qualified_id
, 0);
9367 scope
= tsubst (scope
, args
, complain
, in_decl
);
9368 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
9373 if (dependent_type_p (scope
))
9374 return build_qualified_name (/*type=*/NULL_TREE
,
9376 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
9378 if (!BASELINK_P (name
) && !DECL_P (expr
))
9380 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
9381 /* If this were actually a destructor call, it would have been
9382 parsed as such by the parser. */
9383 expr
= error_mark_node
;
9385 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
9386 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
9387 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
9389 if (complain
& tf_error
)
9391 error ("dependent-name %qE is parsed as a non-type, but "
9392 "instantiation yields a type", qualified_id
);
9393 inform ("say %<typename %E%> if a type is meant", qualified_id
);
9395 return error_mark_node
;
9401 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
9403 /* Remember that there was a reference to this entity. */
9407 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
9409 if (complain
& tf_error
)
9410 qualified_name_lookup_error (scope
,
9411 TREE_OPERAND (qualified_id
, 1),
9413 return error_mark_node
;
9417 expr
= lookup_template_function (expr
, template_args
);
9419 if (expr
== error_mark_node
&& complain
& tf_error
)
9420 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
9422 else if (TYPE_P (scope
))
9424 expr
= (adjust_result_of_qualified_name_lookup
9425 (expr
, scope
, current_class_type
));
9426 expr
= (finish_qualified_id_expr
9427 (scope
, expr
, done
, address_p
,
9428 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
9429 /*template_arg_p=*/false));
9432 /* Expressions do not generally have reference type. */
9433 if (TREE_CODE (expr
) != SCOPE_REF
9434 /* However, if we're about to form a pointer-to-member, we just
9435 want the referenced member referenced. */
9436 && TREE_CODE (expr
) != OFFSET_REF
)
9437 expr
= convert_from_reference (expr
);
9442 /* Like tsubst, but deals with expressions. This function just replaces
9443 template parms; to finish processing the resultant expression, use
9447 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9449 enum tree_code code
;
9452 if (t
== NULL_TREE
|| t
== error_mark_node
)
9455 code
= TREE_CODE (t
);
9460 r
= retrieve_local_specialization (t
);
9461 gcc_assert (r
!= NULL
);
9462 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
9463 r
= ARGUMENT_PACK_SELECT_ARG (r
);
9472 if (DECL_TEMPLATE_PARM_P (t
))
9473 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
9474 /* There is no need to substitute into namespace-scope
9476 if (DECL_NAMESPACE_SCOPE_P (t
))
9478 /* If ARGS is NULL, then T is known to be non-dependent. */
9479 if (args
== NULL_TREE
)
9480 return integral_constant_value (t
);
9482 /* Unfortunately, we cannot just call lookup_name here.
9485 template <int I> int f() {
9487 struct S { void g() { E e = a; } };
9490 When we instantiate f<7>::S::g(), say, lookup_name is not
9491 clever enough to find f<7>::a. */
9493 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
9494 /*entering_scope=*/0);
9496 for (v
= TYPE_VALUES (enum_type
);
9499 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
9500 return TREE_VALUE (v
);
9502 /* We didn't find the name. That should never happen; if
9503 name-lookup found it during preliminary parsing, we
9504 should find it again here during instantiation. */
9510 if (DECL_CONTEXT (t
))
9514 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
9515 /*entering_scope=*/1);
9516 if (ctx
!= DECL_CONTEXT (t
))
9518 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
9521 if (complain
& tf_error
)
9522 error ("using invalid field %qD", t
);
9523 return error_mark_node
;
9533 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
9534 || local_variable_p (t
))
9535 t
= tsubst (t
, args
, complain
, in_decl
);
9540 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
9543 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9544 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
9545 args
, complain
, in_decl
);
9546 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
9547 return tsubst (t
, args
, complain
, in_decl
);
9548 else if (DECL_CLASS_SCOPE_P (t
)
9549 && uses_template_parms (DECL_CONTEXT (t
)))
9551 /* Template template argument like the following example need
9554 template <template <class> class TT> struct C {};
9555 template <class T> struct D {
9556 template <class U> struct E {};
9561 We are processing the template argument `E' in #1 for
9562 the template instantiation #2. Originally, `E' is a
9563 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9564 have to substitute this with one having context `D<int>'. */
9566 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
9567 return lookup_field (context
, DECL_NAME(t
), 0, false);
9570 /* Ordinary template template argument. */
9574 case REINTERPRET_CAST_EXPR
:
9575 case CONST_CAST_EXPR
:
9576 case STATIC_CAST_EXPR
:
9577 case DYNAMIC_CAST_EXPR
:
9580 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
9581 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
9584 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
9586 /* We only want to compute the number of arguments. */
9587 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
9589 return build_int_cst (size_type_node
, TREE_VEC_LENGTH (expanded
));
9595 case TRUTH_NOT_EXPR
:
9598 case UNARY_PLUS_EXPR
: /* Unary + */
9606 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
9607 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
9614 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
9615 name
= TREE_OPERAND (t
, 1);
9616 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
9618 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9620 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
9622 else if (TREE_CODE (name
) == SCOPE_REF
9623 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
9625 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9627 name
= TREE_OPERAND (name
, 1);
9628 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
9630 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
9631 name
= build_qualified_name (/*type=*/NULL_TREE
,
9633 /*template_p=*/false);
9635 else if (TREE_CODE (name
) == BASELINK
)
9636 name
= tsubst_baselink (name
,
9637 non_reference (TREE_TYPE (object
)),
9641 name
= tsubst_copy (name
, args
, complain
, in_decl
);
9642 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
9648 case TRUNC_DIV_EXPR
:
9650 case FLOOR_DIV_EXPR
:
9651 case ROUND_DIV_EXPR
:
9652 case EXACT_DIV_EXPR
:
9656 case TRUNC_MOD_EXPR
:
9657 case FLOOR_MOD_EXPR
:
9658 case TRUTH_ANDIF_EXPR
:
9659 case TRUTH_ORIF_EXPR
:
9660 case TRUTH_AND_EXPR
:
9677 case PREDECREMENT_EXPR
:
9678 case PREINCREMENT_EXPR
:
9679 case POSTDECREMENT_EXPR
:
9680 case POSTINCREMENT_EXPR
:
9682 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9683 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
9686 return build_qualified_name (/*type=*/NULL_TREE
,
9687 tsubst_copy (TREE_OPERAND (t
, 0),
9688 args
, complain
, in_decl
),
9689 tsubst_copy (TREE_OPERAND (t
, 1),
9690 args
, complain
, in_decl
),
9691 QUALIFIED_NAME_IS_TEMPLATE (t
));
9696 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9697 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9698 NULL_TREE
, NULL_TREE
);
9702 int n
= VL_EXP_OPERAND_LENGTH (t
);
9703 tree result
= build_vl_exp (CALL_EXPR
, n
);
9705 for (i
= 0; i
< n
; i
++)
9706 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
9713 case PSEUDO_DTOR_EXPR
:
9716 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9717 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9718 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
9719 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
9726 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9727 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
9728 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
9729 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
9736 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
9737 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
9738 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
9739 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
9743 case TEMPLATE_ID_EXPR
:
9745 /* Substituted template arguments */
9746 tree fn
= TREE_OPERAND (t
, 0);
9747 tree targs
= TREE_OPERAND (t
, 1);
9749 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
9751 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
9753 return lookup_template_function (fn
, targs
);
9758 tree purpose
, value
, chain
;
9760 if (t
== void_list_node
)
9763 purpose
= TREE_PURPOSE (t
);
9765 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
9766 value
= TREE_VALUE (t
);
9768 value
= tsubst_copy (value
, args
, complain
, in_decl
);
9769 chain
= TREE_CHAIN (t
);
9770 if (chain
&& chain
!= void_type_node
)
9771 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
9772 if (purpose
== TREE_PURPOSE (t
)
9773 && value
== TREE_VALUE (t
)
9774 && chain
== TREE_CHAIN (t
))
9776 return tree_cons (purpose
, value
, chain
);
9783 case TEMPLATE_TYPE_PARM
:
9784 case TEMPLATE_TEMPLATE_PARM
:
9785 case BOUND_TEMPLATE_TEMPLATE_PARM
:
9786 case TEMPLATE_PARM_INDEX
:
9788 case REFERENCE_TYPE
:
9794 case UNBOUND_CLASS_TEMPLATE
:
9798 return tsubst (t
, args
, complain
, in_decl
);
9800 case IDENTIFIER_NODE
:
9801 if (IDENTIFIER_TYPENAME_P (t
))
9803 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9804 return mangle_conv_op_name_for_type (new_type
);
9810 /* This is handled by tsubst_copy_and_build. */
9814 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
9816 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
9818 case CLEANUP_POINT_EXPR
:
9819 /* We shouldn't have built any of these during initial template
9820 generation. Instead, they should be built during instantiation
9821 in response to the saved STMT_IS_FULL_EXPR_P setting. */
9825 mark_used (TREE_OPERAND (t
, 1));
9828 case EXPR_PACK_EXPANSION
:
9829 error ("invalid use of pack expansion expression");
9830 return error_mark_node
;
9832 case NONTYPE_ARGUMENT_PACK
:
9833 error ("use %<...%> to expand argument pack");
9834 return error_mark_node
;
9841 /* Like tsubst_copy, but specifically for OpenMP clauses. */
9844 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
9847 tree new_clauses
= NULL
, nc
, oc
;
9849 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
9851 nc
= copy_node (oc
);
9852 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
9855 switch (OMP_CLAUSE_CODE (nc
))
9857 case OMP_CLAUSE_PRIVATE
:
9858 case OMP_CLAUSE_SHARED
:
9859 case OMP_CLAUSE_FIRSTPRIVATE
:
9860 case OMP_CLAUSE_LASTPRIVATE
:
9861 case OMP_CLAUSE_REDUCTION
:
9862 case OMP_CLAUSE_COPYIN
:
9863 case OMP_CLAUSE_COPYPRIVATE
:
9865 case OMP_CLAUSE_NUM_THREADS
:
9866 case OMP_CLAUSE_SCHEDULE
:
9867 OMP_CLAUSE_OPERAND (nc
, 0)
9868 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
9869 in_decl
, /*integral_constant_expression_p=*/false);
9871 case OMP_CLAUSE_NOWAIT
:
9872 case OMP_CLAUSE_ORDERED
:
9873 case OMP_CLAUSE_DEFAULT
:
9880 return finish_omp_clauses (nreverse (new_clauses
));
9883 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
9886 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
9889 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
9891 tree purpose
, value
, chain
;
9896 if (TREE_CODE (t
) != TREE_LIST
)
9897 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
9898 /*function_p=*/false,
9899 /*integral_constant_expression_p=*/false);
9901 if (t
== void_list_node
)
9904 purpose
= TREE_PURPOSE (t
);
9906 purpose
= RECUR (purpose
);
9907 value
= TREE_VALUE (t
);
9909 value
= RECUR (value
);
9910 chain
= TREE_CHAIN (t
);
9911 if (chain
&& chain
!= void_type_node
)
9912 chain
= RECUR (chain
);
9913 return tree_cons (purpose
, value
, chain
);
9917 /* Like tsubst_copy for expressions, etc. but also does semantic
9921 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
9922 bool integral_constant_expression_p
)
9924 #define RECUR(NODE) \
9925 tsubst_expr ((NODE), args, complain, in_decl, \
9926 integral_constant_expression_p)
9930 if (t
== NULL_TREE
|| t
== error_mark_node
)
9933 if (EXPR_HAS_LOCATION (t
))
9934 input_location
= EXPR_LOCATION (t
);
9935 if (STATEMENT_CODE_P (TREE_CODE (t
)))
9936 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
9938 switch (TREE_CODE (t
))
9940 case STATEMENT_LIST
:
9942 tree_stmt_iterator i
;
9943 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
9944 RECUR (tsi_stmt (i
));
9948 case CTOR_INITIALIZER
:
9949 finish_mem_initializers (tsubst_initializer_list
9950 (TREE_OPERAND (t
, 0), args
));
9954 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
9958 tmp
= RECUR (EXPR_STMT_EXPR (t
));
9959 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
9960 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
9962 finish_expr_stmt (tmp
);
9966 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
9974 decl
= DECL_EXPR_DECL (t
);
9975 if (TREE_CODE (decl
) == LABEL_DECL
)
9976 finish_label_decl (DECL_NAME (decl
));
9977 else if (TREE_CODE (decl
) == USING_DECL
)
9979 tree scope
= USING_DECL_SCOPE (decl
);
9980 tree name
= DECL_NAME (decl
);
9983 scope
= RECUR (scope
);
9984 decl
= lookup_qualified_name (scope
, name
,
9985 /*is_type_p=*/false,
9986 /*complain=*/false);
9987 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
9988 qualified_name_lookup_error (scope
, name
, decl
);
9990 do_local_using_decl (decl
, scope
, name
);
9994 init
= DECL_INITIAL (decl
);
9995 decl
= tsubst (decl
, args
, complain
, in_decl
);
9996 if (decl
!= error_mark_node
)
9998 /* By marking the declaration as instantiated, we avoid
9999 trying to instantiate it. Since instantiate_decl can't
10000 handle local variables, and since we've already done
10001 all that needs to be done, that's the right thing to
10003 if (TREE_CODE (decl
) == VAR_DECL
)
10004 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10005 if (TREE_CODE (decl
) == VAR_DECL
10006 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
10007 /* Anonymous aggregates are a special case. */
10008 finish_anon_union (decl
);
10011 maybe_push_decl (decl
);
10012 if (TREE_CODE (decl
) == VAR_DECL
10013 && DECL_PRETTY_FUNCTION_P (decl
))
10015 /* For __PRETTY_FUNCTION__ we have to adjust the
10017 const char *const name
10018 = cxx_printable_name (current_function_decl
, 2);
10019 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
10023 tree t
= RECUR (init
);
10026 /* If we had an initializer but it
10027 instantiated to nothing,
10028 value-initialize the object. This will
10029 only occur when the initializer was a
10030 pack expansion where the parameter packs
10031 used in that expansion were of length
10033 init
= build_default_init (TREE_TYPE (decl
),
10039 finish_decl (decl
, init
, NULL_TREE
);
10044 /* A DECL_EXPR can also be used as an expression, in the condition
10045 clause of an if/for/while construct. */
10050 stmt
= begin_for_stmt ();
10051 RECUR (FOR_INIT_STMT (t
));
10052 finish_for_init_stmt (stmt
);
10053 tmp
= RECUR (FOR_COND (t
));
10054 finish_for_cond (tmp
, stmt
);
10055 tmp
= RECUR (FOR_EXPR (t
));
10056 finish_for_expr (tmp
, stmt
);
10057 RECUR (FOR_BODY (t
));
10058 finish_for_stmt (stmt
);
10062 stmt
= begin_while_stmt ();
10063 tmp
= RECUR (WHILE_COND (t
));
10064 finish_while_stmt_cond (tmp
, stmt
);
10065 RECUR (WHILE_BODY (t
));
10066 finish_while_stmt (stmt
);
10070 stmt
= begin_do_stmt ();
10071 RECUR (DO_BODY (t
));
10072 finish_do_body (stmt
);
10073 tmp
= RECUR (DO_COND (t
));
10074 finish_do_stmt (tmp
, stmt
);
10078 stmt
= begin_if_stmt ();
10079 tmp
= RECUR (IF_COND (t
));
10080 finish_if_stmt_cond (tmp
, stmt
);
10081 RECUR (THEN_CLAUSE (t
));
10082 finish_then_clause (stmt
);
10084 if (ELSE_CLAUSE (t
))
10086 begin_else_clause (stmt
);
10087 RECUR (ELSE_CLAUSE (t
));
10088 finish_else_clause (stmt
);
10091 finish_if_stmt (stmt
);
10095 if (BIND_EXPR_BODY_BLOCK (t
))
10096 stmt
= begin_function_body ();
10098 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
10099 ? BCS_TRY_BLOCK
: 0);
10101 RECUR (BIND_EXPR_BODY (t
));
10103 if (BIND_EXPR_BODY_BLOCK (t
))
10104 finish_function_body (stmt
);
10106 finish_compound_stmt (stmt
);
10110 finish_break_stmt ();
10113 case CONTINUE_STMT
:
10114 finish_continue_stmt ();
10118 stmt
= begin_switch_stmt ();
10119 tmp
= RECUR (SWITCH_STMT_COND (t
));
10120 finish_switch_cond (tmp
, stmt
);
10121 RECUR (SWITCH_STMT_BODY (t
));
10122 finish_switch_stmt (stmt
);
10125 case CASE_LABEL_EXPR
:
10126 finish_case_label (RECUR (CASE_LOW (t
)),
10127 RECUR (CASE_HIGH (t
)));
10131 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t
)));
10135 tmp
= GOTO_DESTINATION (t
);
10136 if (TREE_CODE (tmp
) != LABEL_DECL
)
10137 /* Computed goto's must be tsubst'd into. On the other hand,
10138 non-computed gotos must not be; the identifier in question
10139 will have no binding. */
10142 tmp
= DECL_NAME (tmp
);
10143 finish_goto_stmt (tmp
);
10147 tmp
= finish_asm_stmt
10148 (ASM_VOLATILE_P (t
),
10149 RECUR (ASM_STRING (t
)),
10150 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
10151 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
10152 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
10154 tree asm_expr
= tmp
;
10155 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
10156 asm_expr
= TREE_OPERAND (asm_expr
, 0);
10157 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
10164 stmt
= begin_try_block ();
10165 RECUR (TRY_STMTS (t
));
10166 finish_cleanup_try_block (stmt
);
10167 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
10171 tree compound_stmt
= NULL_TREE
;
10173 if (FN_TRY_BLOCK_P (t
))
10174 stmt
= begin_function_try_block (&compound_stmt
);
10176 stmt
= begin_try_block ();
10178 RECUR (TRY_STMTS (t
));
10180 if (FN_TRY_BLOCK_P (t
))
10181 finish_function_try_block (stmt
);
10183 finish_try_block (stmt
);
10185 RECUR (TRY_HANDLERS (t
));
10186 if (FN_TRY_BLOCK_P (t
))
10187 finish_function_handler_sequence (stmt
, compound_stmt
);
10189 finish_handler_sequence (stmt
);
10195 tree decl
= HANDLER_PARMS (t
);
10199 decl
= tsubst (decl
, args
, complain
, in_decl
);
10200 /* Prevent instantiate_decl from trying to instantiate
10201 this variable. We've already done all that needs to be
10203 if (decl
!= error_mark_node
)
10204 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
10206 stmt
= begin_handler ();
10207 finish_handler_parms (decl
, stmt
);
10208 RECUR (HANDLER_BODY (t
));
10209 finish_handler (stmt
);
10214 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
10217 case STATIC_ASSERT
:
10220 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
10223 /*integral_constant_expression_p=*/true);
10224 finish_static_assert (condition
,
10225 STATIC_ASSERT_MESSAGE (t
),
10226 STATIC_ASSERT_SOURCE_LOCATION (t
),
10227 /*member_p=*/false);
10232 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
10233 args
, complain
, in_decl
);
10234 stmt
= begin_omp_parallel ();
10235 RECUR (OMP_PARALLEL_BODY (t
));
10236 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
10237 = OMP_PARALLEL_COMBINED (t
);
10242 tree clauses
, decl
, init
, cond
, incr
, body
, pre_body
;
10244 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
10245 args
, complain
, in_decl
);
10246 init
= OMP_FOR_INIT (t
);
10247 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
10248 decl
= RECUR (TREE_OPERAND (init
, 0));
10249 init
= RECUR (TREE_OPERAND (init
, 1));
10250 cond
= RECUR (OMP_FOR_COND (t
));
10251 incr
= RECUR (OMP_FOR_INCR (t
));
10253 stmt
= begin_omp_structured_block ();
10255 pre_body
= push_stmt_list ();
10256 RECUR (OMP_FOR_PRE_BODY (t
));
10257 pre_body
= pop_stmt_list (pre_body
);
10259 body
= push_stmt_list ();
10260 RECUR (OMP_FOR_BODY (t
));
10261 body
= pop_stmt_list (body
);
10263 t
= finish_omp_for (EXPR_LOCATION (t
), decl
, init
, cond
, incr
, body
,
10266 OMP_FOR_CLAUSES (t
) = clauses
;
10268 add_stmt (finish_omp_structured_block (stmt
));
10274 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
10275 stmt
= push_stmt_list ();
10276 RECUR (OMP_BODY (t
));
10277 stmt
= pop_stmt_list (stmt
);
10280 OMP_BODY (t
) = stmt
;
10281 OMP_CLAUSES (t
) = tmp
;
10289 stmt
= push_stmt_list ();
10290 RECUR (OMP_BODY (t
));
10291 stmt
= pop_stmt_list (stmt
);
10294 OMP_BODY (t
) = stmt
;
10299 if (OMP_ATOMIC_DEPENDENT_P (t
))
10301 tree op1
= TREE_OPERAND (t
, 1);
10302 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
10303 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
10304 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
10308 case EXPR_PACK_EXPANSION
:
10309 error ("invalid use of pack expansion expression");
10310 return error_mark_node
;
10312 case NONTYPE_ARGUMENT_PACK
:
10313 error ("use %<...%> to expand argument pack");
10314 return error_mark_node
;
10317 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
10319 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10320 /*function_p=*/false,
10321 integral_constant_expression_p
);
10328 /* T is a postfix-expression that is not being used in a function
10329 call. Return the substituted version of T. */
10332 tsubst_non_call_postfix_expression (tree t
, tree args
,
10333 tsubst_flags_t complain
,
10336 if (TREE_CODE (t
) == SCOPE_REF
)
10337 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
10338 /*done=*/false, /*address_p=*/false);
10340 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
10341 /*function_p=*/false,
10342 /*integral_constant_expression_p=*/false);
10347 /* Like tsubst but deals with expressions and performs semantic
10348 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10351 tsubst_copy_and_build (tree t
,
10353 tsubst_flags_t complain
,
10356 bool integral_constant_expression_p
)
10358 #define RECUR(NODE) \
10359 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10360 /*function_p=*/false, \
10361 integral_constant_expression_p)
10365 if (t
== NULL_TREE
|| t
== error_mark_node
)
10368 switch (TREE_CODE (t
))
10372 /* Fall through. */
10373 case IDENTIFIER_NODE
:
10377 bool non_integral_constant_expression_p
;
10378 const char *error_msg
;
10380 if (IDENTIFIER_TYPENAME_P (t
))
10382 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10383 t
= mangle_conv_op_name_for_type (new_type
);
10386 /* Look up the name. */
10387 decl
= lookup_name (t
);
10389 /* By convention, expressions use ERROR_MARK_NODE to indicate
10390 failure, not NULL_TREE. */
10391 if (decl
== NULL_TREE
)
10392 decl
= error_mark_node
;
10394 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
10396 integral_constant_expression_p
,
10397 /*allow_non_integral_constant_expression_p=*/false,
10398 &non_integral_constant_expression_p
,
10399 /*template_p=*/false,
10401 /*address_p=*/false,
10402 /*template_arg_p=*/false,
10406 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
10407 decl
= unqualified_name_lookup_error (decl
);
10411 case TEMPLATE_ID_EXPR
:
10414 tree
template = RECUR (TREE_OPERAND (t
, 0));
10415 tree targs
= TREE_OPERAND (t
, 1);
10418 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
10420 if (TREE_CODE (template) == COMPONENT_REF
)
10422 object
= TREE_OPERAND (template, 0);
10423 template = TREE_OPERAND (template, 1);
10426 object
= NULL_TREE
;
10427 template = lookup_template_function (template, targs
);
10430 return build3 (COMPONENT_REF
, TREE_TYPE (template),
10431 object
, template, NULL_TREE
);
10433 return baselink_for_fns (template);
10438 tree r
= RECUR (TREE_OPERAND (t
, 0));
10440 if (REFERENCE_REF_P (t
))
10442 /* A type conversion to reference type will be enclosed in
10443 such an indirect ref, but the substitution of the cast
10444 will have also added such an indirect ref. */
10445 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
10446 r
= convert_from_reference (r
);
10449 r
= build_x_indirect_ref (r
, "unary *");
10455 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
10456 RECUR (TREE_OPERAND (t
, 0)));
10459 case REINTERPRET_CAST_EXPR
:
10460 case CONST_CAST_EXPR
:
10461 case DYNAMIC_CAST_EXPR
:
10462 case STATIC_CAST_EXPR
:
10467 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10468 if (integral_constant_expression_p
10469 && !cast_valid_in_integral_constant_expression_p (type
))
10471 error ("a cast to a type other than an integral or "
10472 "enumeration type cannot appear in a constant-expression");
10473 return error_mark_node
;
10476 op
= RECUR (TREE_OPERAND (t
, 0));
10478 switch (TREE_CODE (t
))
10481 return build_functional_cast (type
, op
);
10482 case REINTERPRET_CAST_EXPR
:
10483 return build_reinterpret_cast (type
, op
);
10484 case CONST_CAST_EXPR
:
10485 return build_const_cast (type
, op
);
10486 case DYNAMIC_CAST_EXPR
:
10487 return build_dynamic_cast (type
, op
);
10488 case STATIC_CAST_EXPR
:
10489 return build_static_cast (type
, op
);
10491 gcc_unreachable ();
10495 case POSTDECREMENT_EXPR
:
10496 case POSTINCREMENT_EXPR
:
10497 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10498 args
, complain
, in_decl
);
10499 return build_x_unary_op (TREE_CODE (t
), op1
);
10501 case PREDECREMENT_EXPR
:
10502 case PREINCREMENT_EXPR
:
10506 case TRUTH_NOT_EXPR
:
10507 case UNARY_PLUS_EXPR
: /* Unary + */
10508 case REALPART_EXPR
:
10509 case IMAGPART_EXPR
:
10510 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)));
10513 op1
= TREE_OPERAND (t
, 0);
10514 if (TREE_CODE (op1
) == SCOPE_REF
)
10515 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
10516 /*done=*/true, /*address_p=*/true);
10518 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
10520 if (TREE_CODE (op1
) == LABEL_DECL
)
10521 return finish_label_address_expr (DECL_NAME (op1
));
10522 return build_x_unary_op (ADDR_EXPR
, op1
);
10527 case TRUNC_DIV_EXPR
:
10528 case CEIL_DIV_EXPR
:
10529 case FLOOR_DIV_EXPR
:
10530 case ROUND_DIV_EXPR
:
10531 case EXACT_DIV_EXPR
:
10535 case TRUNC_MOD_EXPR
:
10536 case FLOOR_MOD_EXPR
:
10537 case TRUTH_ANDIF_EXPR
:
10538 case TRUTH_ORIF_EXPR
:
10539 case TRUTH_AND_EXPR
:
10540 case TRUTH_OR_EXPR
:
10555 return build_x_binary_op
10557 RECUR (TREE_OPERAND (t
, 0)),
10558 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
10560 : TREE_CODE (TREE_OPERAND (t
, 0))),
10561 RECUR (TREE_OPERAND (t
, 1)),
10562 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
10564 : TREE_CODE (TREE_OPERAND (t
, 1))),
10565 /*overloaded_p=*/NULL
);
10568 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
10569 /*address_p=*/false);
10571 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10572 args
, complain
, in_decl
);
10573 return build_x_binary_op (ARRAY_REF
, op1
,
10574 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
10576 : TREE_CODE (TREE_OPERAND (t
, 0))),
10577 RECUR (TREE_OPERAND (t
, 1)),
10578 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
10580 : TREE_CODE (TREE_OPERAND (t
, 1))),
10581 /*overloaded_p=*/NULL
);
10584 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
10586 /* We only want to compute the number of arguments. */
10587 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
10588 complain
, in_decl
);
10589 return build_int_cst (size_type_node
, TREE_VEC_LENGTH (expanded
));
10594 op1
= TREE_OPERAND (t
, 0);
10597 /* When there are no ARGS, we are trying to evaluate a
10598 non-dependent expression from the parser. Trying to do
10599 the substitutions may not work. */
10601 op1
= TREE_TYPE (op1
);
10606 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
10607 /*function_p=*/false,
10608 /*integral_constant_expression_p=*/false);
10612 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), true);
10614 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
));
10618 tree r
= build_x_modify_expr
10619 (RECUR (TREE_OPERAND (t
, 0)),
10620 TREE_CODE (TREE_OPERAND (t
, 1)),
10621 RECUR (TREE_OPERAND (t
, 2)));
10622 /* TREE_NO_WARNING must be set if either the expression was
10623 parenthesized or it uses an operator such as >>= rather
10624 than plain assignment. In the former case, it was already
10625 set and must be copied. In the latter case,
10626 build_x_modify_expr sets it and it must not be reset
10628 if (TREE_NO_WARNING (t
))
10629 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
10634 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10635 args
, complain
, in_decl
);
10636 /* Remember that there was a reference to this entity. */
10639 return build_x_arrow (op1
);
10643 tree init
= RECUR (TREE_OPERAND (t
, 3));
10645 if (TREE_OPERAND (t
, 3) && !init
)
10646 /* If there was an initializer in the the original tree, but
10647 it instantiated to an empty list, then we should pass on
10648 VOID_ZERO_NODE to tell build_new that it was an empty
10649 initializer () rather than no initializer. This can only
10650 happen when the initializer is a pack expansion whose
10651 parameter packs are of length zero. */
10652 init
= void_zero_node
;
10655 (RECUR (TREE_OPERAND (t
, 0)),
10656 RECUR (TREE_OPERAND (t
, 1)),
10657 RECUR (TREE_OPERAND (t
, 2)),
10659 NEW_EXPR_USE_GLOBAL (t
));
10663 return delete_sanity
10664 (RECUR (TREE_OPERAND (t
, 0)),
10665 RECUR (TREE_OPERAND (t
, 1)),
10666 DELETE_EXPR_USE_VEC (t
),
10667 DELETE_EXPR_USE_GLOBAL (t
));
10669 case COMPOUND_EXPR
:
10670 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
10671 RECUR (TREE_OPERAND (t
, 1)));
10680 function
= CALL_EXPR_FN (t
);
10681 /* When we parsed the expression, we determined whether or
10682 not Koenig lookup should be performed. */
10683 koenig_p
= KOENIG_LOOKUP_P (t
);
10684 if (TREE_CODE (function
) == SCOPE_REF
)
10686 qualified_p
= true;
10687 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
10689 /*address_p=*/false);
10693 if (TREE_CODE (function
) == COMPONENT_REF
)
10695 tree op
= TREE_OPERAND (function
, 1);
10697 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
10698 || (BASELINK_P (op
)
10699 && BASELINK_QUALIFIED_P (op
)));
10702 qualified_p
= false;
10704 function
= tsubst_copy_and_build (function
, args
, complain
,
10707 integral_constant_expression_p
);
10709 if (BASELINK_P (function
))
10710 qualified_p
= true;
10713 /* FIXME: Rewrite this so as not to construct an arglist. */
10714 call_args
= RECUR (CALL_EXPR_ARGS (t
));
10716 /* We do not perform argument-dependent lookup if normal
10717 lookup finds a non-function, in accordance with the
10718 expected resolution of DR 218. */
10720 && ((is_overloaded_fn (function
)
10721 /* If lookup found a member function, the Koenig lookup is
10722 not appropriate, even if an unqualified-name was used
10723 to denote the function. */
10724 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
10725 || TREE_CODE (function
) == IDENTIFIER_NODE
))
10726 function
= perform_koenig_lookup (function
, call_args
);
10728 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
10730 unqualified_name_lookup_error (function
);
10731 return error_mark_node
;
10734 /* Remember that there was a reference to this entity. */
10735 if (DECL_P (function
))
10736 mark_used (function
);
10738 if (TREE_CODE (function
) == OFFSET_REF
)
10739 return build_offset_ref_call_from_tree (function
, call_args
);
10740 if (TREE_CODE (function
) == COMPONENT_REF
)
10742 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
10743 return finish_call_expr (function
, call_args
,
10744 /*disallow_virtual=*/false,
10745 /*koenig_p=*/false);
10747 return (build_new_method_call
10748 (TREE_OPERAND (function
, 0),
10749 TREE_OPERAND (function
, 1),
10750 call_args
, NULL_TREE
,
10751 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
10754 return finish_call_expr (function
, call_args
,
10755 /*disallow_virtual=*/qualified_p
,
10760 return build_x_conditional_expr
10761 (RECUR (TREE_OPERAND (t
, 0)),
10762 RECUR (TREE_OPERAND (t
, 1)),
10763 RECUR (TREE_OPERAND (t
, 2)));
10765 case PSEUDO_DTOR_EXPR
:
10766 return finish_pseudo_destructor_expr
10767 (RECUR (TREE_OPERAND (t
, 0)),
10768 RECUR (TREE_OPERAND (t
, 1)),
10769 RECUR (TREE_OPERAND (t
, 2)));
10773 tree purpose
, value
, chain
;
10775 if (t
== void_list_node
)
10778 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
10779 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
10781 /* We have pack expansions, so expand those and
10782 create a new list out of it. */
10783 tree purposevec
= NULL_TREE
;
10784 tree valuevec
= NULL_TREE
;
10788 /* Expand the argument expressions. */
10789 if (TREE_PURPOSE (t
))
10790 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
10791 complain
, in_decl
);
10792 if (TREE_VALUE (t
))
10793 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
10794 complain
, in_decl
);
10796 /* Build the rest of the list. */
10797 chain
= TREE_CHAIN (t
);
10798 if (chain
&& chain
!= void_type_node
)
10799 chain
= RECUR (chain
);
10801 /* Determine the number of arguments. */
10802 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
10804 len
= TREE_VEC_LENGTH (purposevec
);
10805 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
10807 else if (TREE_CODE (valuevec
) == TREE_VEC
)
10808 len
= TREE_VEC_LENGTH (valuevec
);
10811 /* Since we only performed a partial substitution into
10812 the argument pack, we only return a single list
10814 if (purposevec
== TREE_PURPOSE (t
)
10815 && valuevec
== TREE_VALUE (t
)
10816 && chain
== TREE_CHAIN (t
))
10819 return tree_cons (purposevec
, valuevec
, chain
);
10822 /* Convert the argument vectors into a TREE_LIST */
10826 /* Grab the Ith values. */
10828 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
10831 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
10834 /* Build the list (backwards). */
10835 chain
= tree_cons (purpose
, value
, chain
);
10841 purpose
= TREE_PURPOSE (t
);
10843 purpose
= RECUR (purpose
);
10844 value
= TREE_VALUE (t
);
10846 value
= RECUR (value
);
10847 chain
= TREE_CHAIN (t
);
10848 if (chain
&& chain
!= void_type_node
)
10849 chain
= RECUR (chain
);
10850 if (purpose
== TREE_PURPOSE (t
)
10851 && value
== TREE_VALUE (t
)
10852 && chain
== TREE_CHAIN (t
))
10854 return tree_cons (purpose
, value
, chain
);
10857 case COMPONENT_REF
:
10863 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
10864 args
, complain
, in_decl
);
10865 /* Remember that there was a reference to this entity. */
10866 if (DECL_P (object
))
10867 mark_used (object
);
10868 object_type
= TREE_TYPE (object
);
10870 member
= TREE_OPERAND (t
, 1);
10871 if (BASELINK_P (member
))
10872 member
= tsubst_baselink (member
,
10873 non_reference (TREE_TYPE (object
)),
10874 args
, complain
, in_decl
);
10876 member
= tsubst_copy (member
, args
, complain
, in_decl
);
10877 if (member
== error_mark_node
)
10878 return error_mark_node
;
10880 if (object_type
&& !CLASS_TYPE_P (object_type
))
10882 if (TREE_CODE (member
) == BIT_NOT_EXPR
)
10883 return finish_pseudo_destructor_expr (object
,
10886 else if (TREE_CODE (member
) == SCOPE_REF
10887 && (TREE_CODE (TREE_OPERAND (member
, 1)) == BIT_NOT_EXPR
))
10888 return finish_pseudo_destructor_expr (object
,
10892 else if (TREE_CODE (member
) == SCOPE_REF
10893 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
10898 /* Lookup the template functions now that we know what the
10900 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
10901 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
10902 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
10903 /*is_type_p=*/false,
10904 /*complain=*/false);
10905 if (BASELINK_P (member
))
10907 BASELINK_FUNCTIONS (member
)
10908 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
10910 member
= (adjust_result_of_qualified_name_lookup
10911 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
10916 qualified_name_lookup_error (object_type
, tmpl
, member
);
10917 return error_mark_node
;
10920 else if (TREE_CODE (member
) == SCOPE_REF
10921 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
10922 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
10924 if (complain
& tf_error
)
10926 if (TYPE_P (TREE_OPERAND (member
, 0)))
10927 error ("%qT is not a class or namespace",
10928 TREE_OPERAND (member
, 0));
10930 error ("%qD is not a class or namespace",
10931 TREE_OPERAND (member
, 0));
10933 return error_mark_node
;
10935 else if (TREE_CODE (member
) == FIELD_DECL
)
10936 return finish_non_static_data_member (member
, object
, NULL_TREE
);
10938 return finish_class_member_access_expr (object
, member
,
10939 /*template_p=*/false);
10944 (RECUR (TREE_OPERAND (t
, 0)));
10948 VEC(constructor_elt
,gc
) *n
;
10949 constructor_elt
*ce
;
10950 unsigned HOST_WIDE_INT idx
;
10951 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10952 bool process_index_p
;
10954 bool need_copy_p
= false;
10956 if (type
== error_mark_node
)
10957 return error_mark_node
;
10959 /* digest_init will do the wrong thing if we let it. */
10960 if (type
&& TYPE_PTRMEMFUNC_P (type
))
10963 /* We do not want to process the index of aggregate
10964 initializers as they are identifier nodes which will be
10965 looked up by digest_init. */
10966 process_index_p
= !(type
&& IS_AGGR_TYPE (type
));
10968 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
10969 newlen
= VEC_length (constructor_elt
, n
);
10970 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
10972 if (ce
->index
&& process_index_p
)
10973 ce
->index
= RECUR (ce
->index
);
10975 if (PACK_EXPANSION_P (ce
->value
))
10977 /* Substitute into the pack expansion. */
10978 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
10981 if (TREE_VEC_LENGTH (ce
->value
) == 1)
10982 /* Just move the argument into place. */
10983 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
10986 /* Update the length of the final CONSTRUCTOR
10987 arguments vector, and note that we will need to
10989 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
10990 need_copy_p
= true;
10994 ce
->value
= RECUR (ce
->value
);
10999 VEC(constructor_elt
,gc
) *old_n
= n
;
11001 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
11002 for (idx
= 0; VEC_iterate (constructor_elt
, old_n
, idx
, ce
);
11005 if (TREE_CODE (ce
->value
) == TREE_VEC
)
11007 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
11008 for (i
= 0; i
< len
; ++i
)
11009 CONSTRUCTOR_APPEND_ELT (n
, 0,
11010 TREE_VEC_ELT (ce
->value
, i
));
11013 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
11017 if (TREE_HAS_CONSTRUCTOR (t
))
11018 return finish_compound_literal (type
, n
);
11020 return build_constructor (NULL_TREE
, n
);
11025 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
11026 if (TYPE_P (operand_0
))
11027 return get_typeid (operand_0
);
11028 return build_typeid (operand_0
);
11038 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
11040 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
11041 /* If the original type was a reference, we'll be wrapped in
11042 the appropriate INDIRECT_REF. */
11043 r
= convert_from_reference (r
);
11048 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
11049 tsubst_copy (TREE_TYPE (t
), args
, complain
,
11052 case OFFSETOF_EXPR
:
11053 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
11057 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
11058 complain
, in_decl
);
11060 tree type2
= TRAIT_EXPR_TYPE2 (t
);
11062 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
11064 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
11069 tree old_stmt_expr
= cur_stmt_expr
;
11070 tree stmt_expr
= begin_stmt_expr ();
11072 cur_stmt_expr
= stmt_expr
;
11073 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
11074 integral_constant_expression_p
);
11075 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
11076 cur_stmt_expr
= old_stmt_expr
;
11082 t
= tsubst_copy (t
, args
, complain
, in_decl
);
11083 /* As in finish_id_expression, we resolve enumeration constants
11084 to their underlying values. */
11085 if (TREE_CODE (t
) == CONST_DECL
)
11087 used_types_insert (TREE_TYPE (t
));
11088 return DECL_INITIAL (t
);
11093 /* Handle Objective-C++ constructs, if appropriate. */
11096 = objcp_tsubst_copy_and_build (t
, args
, complain
,
11097 in_decl
, /*function_p=*/false);
11101 return tsubst_copy (t
, args
, complain
, in_decl
);
11107 /* Verify that the instantiated ARGS are valid. For type arguments,
11108 make sure that the type's linkage is ok. For non-type arguments,
11109 make sure they are constants if they are integral or enumerations.
11110 Emit an error under control of COMPLAIN, and return TRUE on error. */
11113 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
11115 int ix
, len
= DECL_NTPARMS (tmpl
);
11116 bool result
= false;
11118 for (ix
= 0; ix
!= len
; ix
++)
11120 tree t
= TREE_VEC_ELT (args
, ix
);
11124 /* [basic.link]: A name with no linkage (notably, the name
11125 of a class or enumeration declared in a local scope)
11126 shall not be used to declare an entity with linkage.
11127 This implies that names with no linkage cannot be used as
11128 template arguments. */
11129 tree nt
= no_linkage_check (t
, /*relaxed_p=*/false);
11133 /* DR 488 makes use of a type with no linkage cause
11134 type deduction to fail. */
11135 if (complain
& tf_error
)
11137 if (TYPE_ANONYMOUS_P (nt
))
11138 error ("%qT is/uses anonymous type", t
);
11140 error ("template argument for %qD uses local type %qT",
11145 /* In order to avoid all sorts of complications, we do not
11146 allow variably-modified types as template arguments. */
11147 else if (variably_modified_type_p (t
, NULL_TREE
))
11149 if (complain
& tf_error
)
11150 error ("%qT is a variably modified type", t
);
11154 /* A non-type argument of integral or enumerated type must be a
11156 else if (TREE_TYPE (t
)
11157 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
11158 && !TREE_CONSTANT (t
))
11160 if (complain
& tf_error
)
11161 error ("integral expression %qE is not constant", t
);
11165 if (result
&& (complain
& tf_error
))
11166 error (" trying to instantiate %qD", tmpl
);
11170 /* Instantiate the indicated variable or function template TMPL with
11171 the template arguments in TARG_PTR. */
11174 instantiate_template (tree tmpl
, tree targ_ptr
, tsubst_flags_t complain
)
11179 HOST_WIDE_INT saved_processing_template_decl
;
11181 if (tmpl
== error_mark_node
)
11182 return error_mark_node
;
11184 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
11186 /* If this function is a clone, handle it specially. */
11187 if (DECL_CLONED_FUNCTION_P (tmpl
))
11192 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
,
11194 if (spec
== error_mark_node
)
11195 return error_mark_node
;
11197 /* Look for the clone. */
11198 FOR_EACH_CLONE (clone
, spec
)
11199 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
11201 /* We should always have found the clone by now. */
11202 gcc_unreachable ();
11206 /* Check to see if we already have this specialization. */
11207 spec
= retrieve_specialization (tmpl
, targ_ptr
,
11208 /*class_specializations_p=*/false);
11209 if (spec
!= NULL_TREE
)
11212 gen_tmpl
= most_general_template (tmpl
);
11213 if (tmpl
!= gen_tmpl
)
11215 /* The TMPL is a partial instantiation. To get a full set of
11216 arguments we must add the arguments used to perform the
11217 partial instantiation. */
11218 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
11221 /* Check to see if we already have this specialization. */
11222 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
,
11223 /*class_specializations_p=*/false);
11224 if (spec
!= NULL_TREE
)
11228 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
11230 return error_mark_node
;
11232 /* We are building a FUNCTION_DECL, during which the access of its
11233 parameters and return types have to be checked. However this
11234 FUNCTION_DECL which is the desired context for access checking
11235 is not built yet. We solve this chicken-and-egg problem by
11236 deferring all checks until we have the FUNCTION_DECL. */
11237 push_deferring_access_checks (dk_deferred
);
11239 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11240 (because, for example, we have encountered a non-dependent
11241 function call in the body of a template function and must now
11242 determine which of several overloaded functions will be called),
11243 within the instantiation itself we are not processing a
11245 saved_processing_template_decl
= processing_template_decl
;
11246 processing_template_decl
= 0;
11247 /* Substitute template parameters to obtain the specialization. */
11248 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
11249 targ_ptr
, complain
, gen_tmpl
);
11250 processing_template_decl
= saved_processing_template_decl
;
11251 if (fndecl
== error_mark_node
)
11252 return error_mark_node
;
11254 /* Now we know the specialization, compute access previously
11256 push_access_scope (fndecl
);
11257 perform_deferred_access_checks ();
11258 pop_access_scope (fndecl
);
11259 pop_deferring_access_checks ();
11261 /* The DECL_TI_TEMPLATE should always be the immediate parent
11262 template, not the most general template. */
11263 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
11265 /* If we've just instantiated the main entry point for a function,
11266 instantiate all the alternate entry points as well. We do this
11267 by cloning the instantiation of the main entry point, not by
11268 instantiating the template clones. */
11269 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
11270 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
11275 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11276 arguments that are being used when calling it. TARGS is a vector
11277 into which the deduced template arguments are placed.
11279 Return zero for success, 2 for an incomplete match that doesn't resolve
11280 all the types, and 1 for complete failure. An error message will be
11281 printed only for an incomplete match.
11283 If FN is a conversion operator, or we are trying to produce a specific
11284 specialization, RETURN_TYPE is the return type desired.
11286 The EXPLICIT_TARGS are explicit template arguments provided via a
11289 The parameter STRICT is one of:
11292 We are deducing arguments for a function call, as in
11293 [temp.deduct.call].
11296 We are deducing arguments for a conversion function, as in
11297 [temp.deduct.conv].
11300 We are deducing arguments when doing an explicit instantiation
11301 as in [temp.explicit], when determining an explicit specialization
11302 as in [temp.expl.spec], or when taking the address of a function
11303 template, as in [temp.deduct.funcaddr]. */
11306 fn_type_unification (tree fn
,
11307 tree explicit_targs
,
11311 unification_kind_t strict
,
11317 bool incomplete_argument_packs_p
= false;
11319 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
11321 fntype
= TREE_TYPE (fn
);
11322 if (explicit_targs
)
11326 The specified template arguments must match the template
11327 parameters in kind (i.e., type, nontype, template), and there
11328 must not be more arguments than there are parameters;
11329 otherwise type deduction fails.
11331 Nontype arguments must match the types of the corresponding
11332 nontype template parameters, or must be convertible to the
11333 types of the corresponding nontype parameters as specified in
11334 _temp.arg.nontype_, otherwise type deduction fails.
11336 All references in the function type of the function template
11337 to the corresponding template parameters are replaced by the
11338 specified template argument values. If a substitution in a
11339 template parameter or in the function type of the function
11340 template results in an invalid type, type deduction fails. */
11341 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
11342 int i
, len
= TREE_VEC_LENGTH (tparms
);
11343 tree converted_args
;
11344 bool incomplete
= false;
11346 if (explicit_targs
== error_mark_node
)
11350 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
11351 /*require_all_args=*/false,
11352 /*use_default_args=*/false));
11353 if (converted_args
== error_mark_node
)
11356 /* Substitute the explicit args into the function type. This is
11357 necessary so that, for instance, explicitly declared function
11358 arguments can match null pointed constants. If we were given
11359 an incomplete set of explicit args, we must not do semantic
11360 processing during substitution as we could create partial
11362 for (i
= 0; i
< len
; i
++)
11364 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
11365 bool parameter_pack
= false;
11367 /* Dig out the actual parm. */
11368 if (TREE_CODE (parm
) == TYPE_DECL
11369 || TREE_CODE (parm
) == TEMPLATE_DECL
)
11371 parm
= TREE_TYPE (parm
);
11372 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
11374 else if (TREE_CODE (parm
) == PARM_DECL
)
11376 parm
= DECL_INITIAL (parm
);
11377 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
11380 if (parameter_pack
)
11384 template_parm_level_and_index (parm
, &level
, &idx
);
11386 /* Mark the argument pack as "incomplete". We could
11387 still deduce more arguments during unification. */
11388 targ
= TMPL_ARG (converted_args
, level
, idx
);
11391 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
11392 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
11393 = ARGUMENT_PACK_ARGS (targ
);
11396 /* We have some incomplete argument packs. */
11397 incomplete_argument_packs_p
= true;
11401 if (incomplete_argument_packs_p
)
11402 /* Any substitution is guaranteed to be incomplete if there
11403 are incomplete argument packs, because we can still deduce
11407 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
11409 processing_template_decl
+= incomplete
;
11410 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
11411 processing_template_decl
-= incomplete
;
11413 if (fntype
== error_mark_node
)
11416 /* Place the explicitly specified arguments in TARGS. */
11417 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
11418 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
11421 /* Never do unification on the 'this' parameter. */
11422 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
11426 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
11427 args
= tree_cons (NULL_TREE
, return_type
, args
);
11430 /* We allow incomplete unification without an error message here
11431 because the standard doesn't seem to explicitly prohibit it. Our
11432 callers must be ready to deal with unification failures in any
11434 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
11435 targs
, parms
, args
, /*subr=*/0,
11438 if (result
== 0 && incomplete_argument_packs_p
)
11440 int i
, len
= NUM_TMPL_ARGS (targs
);
11442 /* Clear the "incomplete" flags on all argument packs. */
11443 for (i
= 0; i
< len
; i
++)
11445 tree arg
= TREE_VEC_ELT (targs
, i
);
11446 if (ARGUMENT_PACK_P (arg
))
11448 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
11449 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
11455 /* All is well so far. Now, check:
11459 When all template arguments have been deduced, all uses of
11460 template parameters in nondeduced contexts are replaced with
11461 the corresponding deduced argument values. If the
11462 substitution results in an invalid type, as described above,
11463 type deduction fails. */
11464 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
11465 == error_mark_node
)
11471 /* Adjust types before performing type deduction, as described in
11472 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11473 sections are symmetric. PARM is the type of a function parameter
11474 or the return type of the conversion function. ARG is the type of
11475 the argument passed to the call, or the type of the value
11476 initialized with the result of the conversion function.
11477 ARG_EXPR is the original argument expression, which may be null. */
11480 maybe_adjust_types_for_deduction (unification_kind_t strict
,
11494 /* Swap PARM and ARG throughout the remainder of this
11495 function; the handling is precisely symmetric since PARM
11496 will initialize ARG rather than vice versa. */
11504 /* There is nothing to do in this case. */
11508 gcc_unreachable ();
11511 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
11513 /* [temp.deduct.call]
11515 If P is not a reference type:
11517 --If A is an array type, the pointer type produced by the
11518 array-to-pointer standard conversion (_conv.array_) is
11519 used in place of A for type deduction; otherwise,
11521 --If A is a function type, the pointer type produced by
11522 the function-to-pointer standard conversion
11523 (_conv.func_) is used in place of A for type deduction;
11526 --If A is a cv-qualified type, the top level
11527 cv-qualifiers of A's type are ignored for type
11529 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
11530 *arg
= build_pointer_type (TREE_TYPE (*arg
));
11531 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
11532 *arg
= build_pointer_type (*arg
);
11534 *arg
= TYPE_MAIN_VARIANT (*arg
);
11537 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
11538 of the form T&&, where T is a template parameter, and the argument
11539 is an lvalue, T is deduced as A& */
11540 if (TREE_CODE (*parm
) == REFERENCE_TYPE
11541 && TYPE_REF_IS_RVALUE (*parm
)
11542 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
11543 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
11544 && arg_expr
&& real_lvalue_p (arg_expr
))
11545 *arg
= build_reference_type (*arg
);
11547 /* [temp.deduct.call]
11549 If P is a cv-qualified type, the top level cv-qualifiers
11550 of P's type are ignored for type deduction. If P is a
11551 reference type, the type referred to by P is used for
11553 *parm
= TYPE_MAIN_VARIANT (*parm
);
11554 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
11556 *parm
= TREE_TYPE (*parm
);
11557 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
11560 /* DR 322. For conversion deduction, remove a reference type on parm
11561 too (which has been swapped into ARG). */
11562 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
11563 *arg
= TREE_TYPE (*arg
);
11568 /* Most parms like fn_type_unification.
11570 If SUBR is 1, we're being called recursively (to unify the
11571 arguments of a function or method parameter of a function
11575 type_unification_real (tree tparms
,
11580 unification_kind_t strict
,
11583 tree parm
, arg
, arg_expr
;
11585 int ntparms
= TREE_VEC_LENGTH (tparms
);
11587 int saw_undeduced
= 0;
11590 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
11591 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
11592 gcc_assert (!xargs
|| TREE_CODE (xargs
) == TREE_LIST
);
11593 gcc_assert (ntparms
> 0);
11598 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
11599 | UNIFY_ALLOW_DERIVED
);
11603 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
11607 sub_strict
= UNIFY_ALLOW_NONE
;
11611 gcc_unreachable ();
11618 while (parms
&& parms
!= void_list_node
11619 && args
&& args
!= void_list_node
)
11621 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
11624 parm
= TREE_VALUE (parms
);
11625 parms
= TREE_CHAIN (parms
);
11626 arg
= TREE_VALUE (args
);
11627 args
= TREE_CHAIN (args
);
11630 if (arg
== error_mark_node
)
11632 if (arg
== unknown_type_node
)
11633 /* We can't deduce anything from this, but we might get all the
11634 template args from other function args. */
11637 /* Conversions will be performed on a function argument that
11638 corresponds with a function parameter that contains only
11639 non-deducible template parameters and explicitly specified
11640 template parameters. */
11641 if (!uses_template_parms (parm
))
11646 type
= TREE_TYPE (arg
);
11650 if (same_type_p (parm
, type
))
11652 if (strict
!= DEDUCE_EXACT
11653 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
11662 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
11663 if (type_unknown_p (arg
))
11665 /* [temp.deduct.type]
11667 A template-argument can be deduced from a pointer to
11668 function or pointer to member function argument if
11669 the set of overloaded functions does not contain
11670 function templates and at most one of a set of
11671 overloaded functions provides a unique match. */
11672 if (resolve_overloaded_unification
11673 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
11679 arg
= unlowered_expr_type (arg
);
11680 if (arg
== error_mark_node
)
11685 int arg_strict
= sub_strict
;
11688 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
11691 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
11698 && parms
!= void_list_node
11699 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
11701 /* Unify the remaining arguments with the pack expansion type. */
11703 tree parmvec
= make_tree_vec (1);
11707 /* Count the number of arguments that remain. */
11708 for (t
= args
; t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
11711 /* Allocate a TREE_VEC and copy in all of the arguments */
11712 argvec
= make_tree_vec (len
);
11713 for (i
= 0; args
&& args
!= void_list_node
; args
= TREE_CHAIN (args
))
11715 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (args
);
11719 /* Copy the parameter into parmvec. */
11720 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
11721 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
11722 /*call_args_p=*/true, /*subr=*/subr
))
11725 /* Advance to the end of the list of parameters. */
11726 parms
= TREE_CHAIN (parms
);
11729 /* Fail if we've reached the end of the parm list, and more args
11730 are present, and the parm list isn't variadic. */
11731 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
11733 /* Fail if parms are left and they don't have default values. */
11734 if (parms
&& parms
!= void_list_node
11735 && TREE_PURPOSE (parms
) == NULL_TREE
)
11739 for (i
= 0; i
< ntparms
; i
++)
11740 if (!TREE_VEC_ELT (targs
, i
))
11744 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
11747 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
11749 /* If this is an undeduced nontype parameter that depends on
11750 a type parameter, try another pass; its type may have been
11751 deduced from a later argument than the one from which
11752 this parameter can be deduced. */
11753 if (TREE_CODE (tparm
) == PARM_DECL
11754 && uses_template_parms (TREE_TYPE (tparm
))
11755 && !saw_undeduced
++)
11758 /* Core issue #226 (C++0x) [temp.deduct]:
11760 If a template argument has not been deduced, its
11761 default template argument, if any, is used.
11763 When we are in C++98 mode, TREE_PURPOSE will either
11764 be NULL_TREE or ERROR_MARK_NODE, so we do not need
11765 to explicitly check cxx_dialect here. */
11766 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
11768 tree arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)),
11769 targs
, tf_none
, NULL_TREE
);
11770 if (arg
== error_mark_node
)
11774 TREE_VEC_ELT (targs
, i
) = arg
;
11779 /* If the type parameter is a parameter pack, then it will
11780 be deduced to an empty parameter pack. */
11781 if (template_parameter_pack_p (tparm
))
11785 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
11787 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
11788 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
11789 TREE_CONSTANT (arg
) = 1;
11792 arg
= make_node (TYPE_ARGUMENT_PACK
);
11794 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
11796 TREE_VEC_ELT (targs
, i
) = arg
;
11806 /* Subroutine of type_unification_real. Args are like the variables
11807 at the call site. ARG is an overloaded function (or template-id);
11808 we try deducing template args from each of the overloads, and if
11809 only one succeeds, we go with that. Modifies TARGS and returns
11810 true on success. */
11813 resolve_overloaded_unification (tree tparms
,
11817 unification_kind_t strict
,
11820 tree tempargs
= copy_node (targs
);
11824 if (TREE_CODE (arg
) == ADDR_EXPR
)
11826 arg
= TREE_OPERAND (arg
, 0);
11832 if (TREE_CODE (arg
) == COMPONENT_REF
)
11833 /* Handle `&x' where `x' is some static or non-static member
11835 arg
= TREE_OPERAND (arg
, 1);
11837 if (TREE_CODE (arg
) == OFFSET_REF
)
11838 arg
= TREE_OPERAND (arg
, 1);
11840 /* Strip baselink information. */
11841 if (BASELINK_P (arg
))
11842 arg
= BASELINK_FUNCTIONS (arg
);
11844 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
11846 /* If we got some explicit template args, we need to plug them into
11847 the affected templates before we try to unify, in case the
11848 explicit args will completely resolve the templates in question. */
11850 tree expl_subargs
= TREE_OPERAND (arg
, 1);
11851 arg
= TREE_OPERAND (arg
, 0);
11853 for (; arg
; arg
= OVL_NEXT (arg
))
11855 tree fn
= OVL_CURRENT (arg
);
11856 tree subargs
, elem
;
11858 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
11861 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
11862 expl_subargs
, /*check_ret=*/false);
11865 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
11866 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
11867 elem
, strict
, sub_strict
, addr_p
);
11871 else if (TREE_CODE (arg
) != OVERLOAD
11872 && TREE_CODE (arg
) != FUNCTION_DECL
)
11873 /* If ARG is, for example, "(0, &f)" then its type will be unknown
11874 -- but the deduction does not succeed because the expression is
11875 not just the function on its own. */
11878 for (; arg
; arg
= OVL_NEXT (arg
))
11879 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
11880 TREE_TYPE (OVL_CURRENT (arg
)),
11881 strict
, sub_strict
, addr_p
);
11883 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11884 to function or pointer to member function argument if the set of
11885 overloaded functions does not contain function templates and at most
11886 one of a set of overloaded functions provides a unique match.
11888 So if we found multiple possibilities, we return success but don't
11889 deduce anything. */
11893 int i
= TREE_VEC_LENGTH (targs
);
11895 if (TREE_VEC_ELT (tempargs
, i
))
11896 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
11904 /* Subroutine of resolve_overloaded_unification; does deduction for a single
11905 overload. Fills TARGS with any deduced arguments, or error_mark_node if
11906 different overloads deduce different arguments for a given parm.
11907 ADDR_P is true if the expression for which deduction is being
11908 performed was of the form "& fn" rather than simply "fn".
11910 Returns 1 on success. */
11913 try_one_overload (tree tparms
,
11918 unification_kind_t strict
,
11926 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11927 to function or pointer to member function argument if the set of
11928 overloaded functions does not contain function templates and at most
11929 one of a set of overloaded functions provides a unique match.
11931 So if this is a template, just return success. */
11933 if (uses_template_parms (arg
))
11936 if (TREE_CODE (arg
) == METHOD_TYPE
)
11937 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
11939 arg
= build_pointer_type (arg
);
11941 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
11943 /* We don't copy orig_targs for this because if we have already deduced
11944 some template args from previous args, unify would complain when we
11945 try to deduce a template parameter for the same argument, even though
11946 there isn't really a conflict. */
11947 nargs
= TREE_VEC_LENGTH (targs
);
11948 tempargs
= make_tree_vec (nargs
);
11950 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
11953 /* First make sure we didn't deduce anything that conflicts with
11954 explicitly specified args. */
11955 for (i
= nargs
; i
--; )
11957 tree elt
= TREE_VEC_ELT (tempargs
, i
);
11958 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
11962 else if (uses_template_parms (elt
))
11963 /* Since we're unifying against ourselves, we will fill in
11964 template args used in the function parm list with our own
11965 template parms. Discard them. */
11966 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
11967 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
11971 for (i
= nargs
; i
--; )
11973 tree elt
= TREE_VEC_ELT (tempargs
, i
);
11976 TREE_VEC_ELT (targs
, i
) = elt
;
11982 /* PARM is a template class (perhaps with unbound template
11983 parameters). ARG is a fully instantiated type. If ARG can be
11984 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
11985 TARGS are as for unify. */
11988 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
11990 tree copy_of_targs
;
11992 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
11993 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
11994 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
11997 /* We need to make a new template argument vector for the call to
11998 unify. If we used TARGS, we'd clutter it up with the result of
11999 the attempted unification, even if this class didn't work out.
12000 We also don't want to commit ourselves to all the unifications
12001 we've already done, since unification is supposed to be done on
12002 an argument-by-argument basis. In other words, consider the
12003 following pathological case:
12005 template <int I, int J, int K>
12008 template <int I, int J>
12009 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12011 template <int I, int J, int K>
12012 void f(S<I, J, K>, S<I, I, I>);
12021 Now, by the time we consider the unification involving `s2', we
12022 already know that we must have `f<0, 0, 0>'. But, even though
12023 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12024 because there are two ways to unify base classes of S<0, 1, 2>
12025 with S<I, I, I>. If we kept the already deduced knowledge, we
12026 would reject the possibility I=1. */
12027 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
12029 /* If unification failed, we're done. */
12030 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
12031 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
12037 /* Given a template type PARM and a class type ARG, find the unique
12038 base type in ARG that is an instance of PARM. We do not examine
12039 ARG itself; only its base-classes. If there is not exactly one
12040 appropriate base class, return NULL_TREE. PARM may be the type of
12041 a partial specialization, as well as a plain template type. Used
12045 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
12047 tree rval
= NULL_TREE
;
12050 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)));
12052 binfo
= TYPE_BINFO (complete_type (arg
));
12054 /* The type could not be completed. */
12057 /* Walk in inheritance graph order. The search order is not
12058 important, and this avoids multiple walks of virtual bases. */
12059 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
12061 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
12065 /* If there is more than one satisfactory baseclass, then:
12069 If they yield more than one possible deduced A, the type
12073 if (rval
&& !same_type_p (r
, rval
))
12083 /* Returns the level of DECL, which declares a template parameter. */
12086 template_decl_level (tree decl
)
12088 switch (TREE_CODE (decl
))
12091 case TEMPLATE_DECL
:
12092 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
12095 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
12098 gcc_unreachable ();
12103 /* Decide whether ARG can be unified with PARM, considering only the
12104 cv-qualifiers of each type, given STRICT as documented for unify.
12105 Returns nonzero iff the unification is OK on that basis. */
12108 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
12110 int arg_quals
= cp_type_quals (arg
);
12111 int parm_quals
= cp_type_quals (parm
);
12113 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12114 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12116 /* Although a CVR qualifier is ignored when being applied to a
12117 substituted template parameter ([8.3.2]/1 for example), that
12118 does not apply during deduction [14.8.2.4]/1, (even though
12119 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12120 this). Except when we're allowing additional CV qualifiers
12121 at the outer level [14.8.2.1]/3,1st bullet. */
12122 if ((TREE_CODE (arg
) == REFERENCE_TYPE
12123 || TREE_CODE (arg
) == FUNCTION_TYPE
12124 || TREE_CODE (arg
) == METHOD_TYPE
)
12125 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
12128 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
12129 && (parm_quals
& TYPE_QUAL_RESTRICT
))
12133 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
12134 && (arg_quals
& parm_quals
) != parm_quals
)
12137 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
12138 && (parm_quals
& arg_quals
) != arg_quals
)
12144 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12146 template_parm_level_and_index (tree parm
, int* level
, int* index
)
12148 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12149 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12150 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12152 *index
= TEMPLATE_TYPE_IDX (parm
);
12153 *level
= TEMPLATE_TYPE_LEVEL (parm
);
12157 *index
= TEMPLATE_PARM_IDX (parm
);
12158 *level
= TEMPLATE_PARM_LEVEL (parm
);
12162 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12163 expansion at the end of PACKED_PARMS. Returns 0 if the type
12164 deduction succeeds, 1 otherwise. STRICT is the same as in
12165 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12166 call argument list. We'll need to adjust the arguments to make them
12167 types. SUBR tells us if this is from a recursive call to
12168 type_unification_real. */
12170 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
12171 tree packed_args
, int strict
, bool call_args_p
,
12175 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
12176 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
12177 tree pack
, packs
= NULL_TREE
;
12178 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
12179 int len
= TREE_VEC_LENGTH (packed_args
);
12181 /* Determine the parameter packs we will be deducing from the
12182 pattern, and record their current deductions. */
12183 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
12184 pack
; pack
= TREE_CHAIN (pack
))
12186 tree parm_pack
= TREE_VALUE (pack
);
12189 /* Determine the index and level of this parameter pack. */
12190 template_parm_level_and_index (parm_pack
, &level
, &idx
);
12192 /* Keep track of the parameter packs and their corresponding
12194 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
12195 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
12198 /* Loop through all of the arguments that have not yet been
12199 unified and unify each with the pattern. */
12200 for (i
= start
; i
< len
; i
++)
12202 tree parm
= pattern
;
12204 /* For each parameter pack, clear out the deduced value so that
12205 we can deduce it again. */
12206 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12209 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12211 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
12214 /* Unify the pattern with the current argument. */
12216 tree arg
= TREE_VEC_ELT (packed_args
, i
);
12217 int arg_strict
= strict
;
12218 bool skip_arg_p
= false;
12224 /* This mirrors what we do in type_unification_real. */
12228 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
12229 | UNIFY_ALLOW_MORE_CV_QUAL
12230 | UNIFY_ALLOW_DERIVED
);
12234 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
12238 sub_strict
= UNIFY_ALLOW_NONE
;
12242 gcc_unreachable ();
12247 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
12248 if (type_unknown_p (arg
))
12250 /* [temp.deduct.type] A template-argument can be
12251 deduced from a pointer to function or pointer
12252 to member function argument if the set of
12253 overloaded functions does not contain function
12254 templates and at most one of a set of
12255 overloaded functions provides a unique
12258 if (resolve_overloaded_unification
12259 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
12267 arg
= TREE_TYPE (arg
);
12268 if (arg
== error_mark_node
)
12273 arg_strict
= sub_strict
;
12277 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
12282 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
12287 /* For each parameter pack, collect the deduced value. */
12288 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12291 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12293 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
12294 TMPL_ARG (targs
, level
, idx
);
12298 /* Verify that the results of unification with the parameter packs
12299 produce results consistent with what we've seen before, and make
12300 the deduced argument packs available. */
12301 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
12303 tree old_pack
= TREE_VALUE (pack
);
12304 tree new_args
= TREE_TYPE (pack
);
12306 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
12308 /* Prepend the explicit arguments onto NEW_ARGS. */
12309 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
12310 tree old_args
= new_args
;
12311 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
12312 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
12314 /* Copy the explicit arguments. */
12315 new_args
= make_tree_vec (len
);
12316 for (i
= 0; i
< explicit_len
; i
++)
12317 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
12319 /* Copy the deduced arguments. */
12320 for (; i
< len
; i
++)
12321 TREE_VEC_ELT (new_args
, i
) =
12322 TREE_VEC_ELT (old_args
, i
- explicit_len
);
12330 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12332 /* Build the deduced *_ARGUMENT_PACK. */
12333 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
12335 result
= make_node (NONTYPE_ARGUMENT_PACK
);
12336 TREE_TYPE (result
) =
12337 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
12338 TREE_CONSTANT (result
) = 1;
12341 result
= make_node (TYPE_ARGUMENT_PACK
);
12343 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
12345 /* Note the deduced argument packs for this parameter
12347 TMPL_ARG (targs
, level
, idx
) = result
;
12349 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
12350 && (ARGUMENT_PACK_ARGS (old_pack
)
12351 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
12353 /* We only had the explicitly-provided arguments before, but
12354 now we have a complete set of arguments. */
12356 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
12357 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12359 /* Keep the original deduced argument pack. */
12360 TMPL_ARG (targs
, level
, idx
) = old_pack
;
12362 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
12363 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
12364 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
12366 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
12368 /* Inconsistent unification of this parameter pack. */
12374 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
12376 /* Keep the original deduced argument pack. */
12377 TMPL_ARG (targs
, level
, idx
) = old_pack
;
12384 /* Deduce the value of template parameters. TPARMS is the (innermost)
12385 set of template parameters to a template. TARGS is the bindings
12386 for those template parameters, as determined thus far; TARGS may
12387 include template arguments for outer levels of template parameters
12388 as well. PARM is a parameter to a template function, or a
12389 subcomponent of that parameter; ARG is the corresponding argument.
12390 This function attempts to match PARM with ARG in a manner
12391 consistent with the existing assignments in TARGS. If more values
12392 are deduced, then TARGS is updated.
12394 Returns 0 if the type deduction succeeds, 1 otherwise. The
12395 parameter STRICT is a bitwise or of the following flags:
12398 Require an exact match between PARM and ARG.
12399 UNIFY_ALLOW_MORE_CV_QUAL:
12400 Allow the deduced ARG to be more cv-qualified (by qualification
12401 conversion) than ARG.
12402 UNIFY_ALLOW_LESS_CV_QUAL:
12403 Allow the deduced ARG to be less cv-qualified than ARG.
12404 UNIFY_ALLOW_DERIVED:
12405 Allow the deduced ARG to be a template base class of ARG,
12406 or a pointer to a template base class of the type pointed to by
12408 UNIFY_ALLOW_INTEGER:
12409 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12410 case for more information.
12411 UNIFY_ALLOW_OUTER_LEVEL:
12412 This is the outermost level of a deduction. Used to determine validity
12413 of qualification conversions. A valid qualification conversion must
12414 have const qualified pointers leading up to the inner type which
12415 requires additional CV quals, except at the outer level, where const
12416 is not required [conv.qual]. It would be normal to set this flag in
12417 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12418 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12419 This is the outermost level of a deduction, and PARM can be more CV
12420 qualified at this point.
12421 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12422 This is the outermost level of a deduction, and PARM can be less CV
12423 qualified at this point. */
12426 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
12431 int strict_in
= strict
;
12433 /* I don't think this will do the right thing with respect to types.
12434 But the only case I've seen it in so far has been array bounds, where
12435 signedness is the only information lost, and I think that will be
12437 while (TREE_CODE (parm
) == NOP_EXPR
)
12438 parm
= TREE_OPERAND (parm
, 0);
12440 if (arg
== error_mark_node
)
12442 if (arg
== unknown_type_node
)
12443 /* We can't deduce anything from this, but we might get all the
12444 template args from other function args. */
12447 /* If PARM uses template parameters, then we can't bail out here,
12448 even if ARG == PARM, since we won't record unifications for the
12449 template parameters. We might need them if we're trying to
12450 figure out which of two things is more specialized. */
12451 if (arg
== parm
&& !uses_template_parms (parm
))
12454 /* Immediately reject some pairs that won't unify because of
12455 cv-qualification mismatches. */
12456 if (TREE_CODE (arg
) == TREE_CODE (parm
)
12458 /* It is the elements of the array which hold the cv quals of an array
12459 type, and the elements might be template type parms. We'll check
12460 when we recurse. */
12461 && TREE_CODE (arg
) != ARRAY_TYPE
12462 /* We check the cv-qualifiers when unifying with template type
12463 parameters below. We want to allow ARG `const T' to unify with
12464 PARM `T' for example, when computing which of two templates
12465 is more specialized, for example. */
12466 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
12467 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
12470 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
12471 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
12472 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
12473 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
12474 strict
&= ~UNIFY_ALLOW_DERIVED
;
12475 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
12476 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
12478 switch (TREE_CODE (parm
))
12480 case TYPENAME_TYPE
:
12482 case UNBOUND_CLASS_TEMPLATE
:
12483 /* In a type which contains a nested-name-specifier, template
12484 argument values cannot be deduced for template parameters used
12485 within the nested-name-specifier. */
12488 case TEMPLATE_TYPE_PARM
:
12489 case TEMPLATE_TEMPLATE_PARM
:
12490 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12491 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
12492 if (tparm
== error_mark_node
)
12495 if (TEMPLATE_TYPE_LEVEL (parm
)
12496 != template_decl_level (tparm
))
12497 /* The PARM is not one we're trying to unify. Just check
12498 to see if it matches ARG. */
12499 return (TREE_CODE (arg
) == TREE_CODE (parm
)
12500 && same_type_p (parm
, arg
)) ? 0 : 1;
12501 idx
= TEMPLATE_TYPE_IDX (parm
);
12502 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
12503 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
12505 /* Check for mixed types and values. */
12506 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
12507 && TREE_CODE (tparm
) != TYPE_DECL
)
12508 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12509 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
12512 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12514 /* ARG must be constructed from a template class or a template
12515 template parameter. */
12516 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
12517 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
12521 tree parmvec
= TYPE_TI_ARGS (parm
);
12522 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
12524 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
12527 /* The resolution to DR150 makes clear that default
12528 arguments for an N-argument may not be used to bind T
12529 to a template template parameter with fewer than N
12530 parameters. It is not safe to permit the binding of
12531 default arguments as an extension, as that may change
12532 the meaning of a conforming program. Consider:
12534 struct Dense { static const unsigned int dim = 1; };
12536 template <template <typename> class View,
12538 void operator+(float, View<Block> const&);
12540 template <typename Block,
12541 unsigned int Dim = Block::dim>
12542 struct Lvalue_proxy { operator float() const; };
12546 Lvalue_proxy<Dense> p;
12551 Here, if Lvalue_proxy is permitted to bind to View, then
12552 the global operator+ will be used; if they are not, the
12553 Lvalue_proxy will be converted to float. */
12554 if (coerce_template_parms (argtmplvec
, parmvec
,
12555 TYPE_TI_TEMPLATE (parm
),
12557 /*require_all_args=*/true,
12558 /*use_default_args=*/false)
12559 == error_mark_node
)
12562 /* Deduce arguments T, i from TT<T> or TT<i>.
12563 We check each element of PARMVEC and ARGVEC individually
12564 rather than the whole TREE_VEC since they can have
12565 different number of elements. */
12567 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
12569 if (unify (tparms
, targs
,
12570 TREE_VEC_ELT (parmvec
, i
),
12571 TREE_VEC_ELT (argvec
, i
),
12576 arg
= TYPE_TI_TEMPLATE (arg
);
12578 /* Fall through to deduce template name. */
12581 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
12582 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12584 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12586 /* Simple cases: Value already set, does match or doesn't. */
12587 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
12594 /* If PARM is `const T' and ARG is only `int', we don't have
12595 a match unless we are allowing additional qualification.
12596 If ARG is `const int' and PARM is just `T' that's OK;
12597 that binds `const int' to `T'. */
12598 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
12602 /* Consider the case where ARG is `const volatile int' and
12603 PARM is `const T'. Then, T should be `volatile int'. */
12604 arg
= cp_build_qualified_type_real
12605 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
12606 if (arg
== error_mark_node
)
12609 /* Simple cases: Value already set, does match or doesn't. */
12610 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
12615 /* Make sure that ARG is not a variable-sized array. (Note
12616 that were talking about variable-sized arrays (like
12617 `int[n]'), rather than arrays of unknown size (like
12618 `int[]').) We'll get very confused by such a type since
12619 the bound of the array will not be computable in an
12620 instantiation. Besides, such types are not allowed in
12621 ISO C++, so we can do as we please here. */
12622 if (variably_modified_type_p (arg
, NULL_TREE
))
12626 /* If ARG is a parameter pack or an expansion, we cannot unify
12627 against it unless PARM is also a parameter pack. */
12628 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
12629 && !template_parameter_pack_p (parm
))
12632 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
12635 case TEMPLATE_PARM_INDEX
:
12636 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
12637 if (tparm
== error_mark_node
)
12640 if (TEMPLATE_PARM_LEVEL (parm
)
12641 != template_decl_level (tparm
))
12642 /* The PARM is not one we're trying to unify. Just check
12643 to see if it matches ARG. */
12644 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
12645 && cp_tree_equal (parm
, arg
));
12647 idx
= TEMPLATE_PARM_IDX (parm
);
12648 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
12651 return !cp_tree_equal (targ
, arg
);
12653 /* [temp.deduct.type] If, in the declaration of a function template
12654 with a non-type template-parameter, the non-type
12655 template-parameter is used in an expression in the function
12656 parameter-list and, if the corresponding template-argument is
12657 deduced, the template-argument type shall match the type of the
12658 template-parameter exactly, except that a template-argument
12659 deduced from an array bound may be of any integral type.
12660 The non-type parameter might use already deduced type parameters. */
12661 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
12662 if (!TREE_TYPE (arg
))
12663 /* Template-parameter dependent expression. Just accept it for now.
12664 It will later be processed in convert_template_argument. */
12666 else if (same_type_p (TREE_TYPE (arg
), tparm
))
12668 else if ((strict
& UNIFY_ALLOW_INTEGER
)
12669 && (TREE_CODE (tparm
) == INTEGER_TYPE
12670 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
12671 /* Convert the ARG to the type of PARM; the deduced non-type
12672 template argument must exactly match the types of the
12673 corresponding parameter. */
12674 arg
= fold (build_nop (TREE_TYPE (parm
), arg
));
12675 else if (uses_template_parms (tparm
))
12676 /* We haven't deduced the type of this parameter yet. Try again
12682 /* If ARG is a parameter pack or an expansion, we cannot unify
12683 against it unless PARM is also a parameter pack. */
12684 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
12685 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
12688 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
12693 /* A pointer-to-member constant can be unified only with
12694 another constant. */
12695 if (TREE_CODE (arg
) != PTRMEM_CST
)
12698 /* Just unify the class member. It would be useless (and possibly
12699 wrong, depending on the strict flags) to unify also
12700 PTRMEM_CST_CLASS, because we want to be sure that both parm and
12701 arg refer to the same variable, even if through different
12702 classes. For instance:
12704 struct A { int x; };
12707 Unification of &A::x and &B::x must succeed. */
12708 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
12709 PTRMEM_CST_MEMBER (arg
), strict
);
12714 if (TREE_CODE (arg
) != POINTER_TYPE
)
12717 /* [temp.deduct.call]
12719 A can be another pointer or pointer to member type that can
12720 be converted to the deduced A via a qualification
12721 conversion (_conv.qual_).
12723 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
12724 This will allow for additional cv-qualification of the
12725 pointed-to types if appropriate. */
12727 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
12728 /* The derived-to-base conversion only persists through one
12729 level of pointers. */
12730 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
12732 return unify (tparms
, targs
, TREE_TYPE (parm
),
12733 TREE_TYPE (arg
), strict
);
12736 case REFERENCE_TYPE
:
12737 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
12739 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
12740 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
12743 if (TREE_CODE (arg
) != ARRAY_TYPE
)
12745 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
12746 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
12748 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
12755 /* Our representation of array types uses "N - 1" as the
12756 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
12757 not an integer constant. We cannot unify arbitrarily
12758 complex expressions, so we eliminate the MINUS_EXPRs
12760 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
12761 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
12764 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
12765 parm_max
= TREE_OPERAND (parm_max
, 0);
12767 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
12768 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
12771 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
12772 trying to unify the type of a variable with the type
12773 of a template parameter. For example:
12775 template <unsigned int N>
12776 void f (char (&) [N]);
12783 Here, the type of the ARG will be "int [g(i)]", and
12784 may be a SAVE_EXPR, etc. */
12785 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
12787 arg_max
= TREE_OPERAND (arg_max
, 0);
12790 /* If only one of the bounds used a MINUS_EXPR, compensate
12791 by adding one to the other bound. */
12792 if (parm_cst
&& !arg_cst
)
12793 parm_max
= fold_build2 (PLUS_EXPR
,
12797 else if (arg_cst
&& !parm_cst
)
12798 arg_max
= fold_build2 (PLUS_EXPR
,
12803 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
12806 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
12807 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
12814 case ENUMERAL_TYPE
:
12816 if (TREE_CODE (arg
) != TREE_CODE (parm
))
12819 /* We have already checked cv-qualification at the top of the
12821 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
12824 /* As far as unification is concerned, this wins. Later checks
12825 will invalidate it if necessary. */
12828 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
12829 /* Type INTEGER_CST can come from ordinary constant template args. */
12831 while (TREE_CODE (arg
) == NOP_EXPR
)
12832 arg
= TREE_OPERAND (arg
, 0);
12834 if (TREE_CODE (arg
) != INTEGER_CST
)
12836 return !tree_int_cst_equal (parm
, arg
);
12841 if (TREE_CODE (arg
) != TREE_VEC
)
12843 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
12845 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
12846 if (unify (tparms
, targs
,
12847 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
12855 if (TREE_CODE (arg
) != TREE_CODE (parm
))
12858 if (TYPE_PTRMEMFUNC_P (parm
))
12860 if (!TYPE_PTRMEMFUNC_P (arg
))
12863 return unify (tparms
, targs
,
12864 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
12865 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
12869 if (CLASSTYPE_TEMPLATE_INFO (parm
))
12871 tree t
= NULL_TREE
;
12873 if (strict_in
& UNIFY_ALLOW_DERIVED
)
12875 /* First, we try to unify the PARM and ARG directly. */
12876 t
= try_class_unification (tparms
, targs
,
12881 /* Fallback to the special case allowed in
12882 [temp.deduct.call]:
12884 If P is a class, and P has the form
12885 template-id, then A can be a derived class of
12886 the deduced A. Likewise, if P is a pointer to
12887 a class of the form template-id, A can be a
12888 pointer to a derived class pointed to by the
12890 t
= get_template_base (tparms
, targs
, parm
, arg
);
12896 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
12897 && (CLASSTYPE_TI_TEMPLATE (parm
)
12898 == CLASSTYPE_TI_TEMPLATE (arg
)))
12899 /* Perhaps PARM is something like S<U> and ARG is S<int>.
12900 Then, we should unify `int' and `U'. */
12903 /* There's no chance of unification succeeding. */
12906 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
12907 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
12909 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
12914 case FUNCTION_TYPE
:
12915 if (TREE_CODE (arg
) != TREE_CODE (parm
))
12918 /* CV qualifications for methods can never be deduced, they must
12919 match exactly. We need to check them explicitly here,
12920 because type_unification_real treats them as any other
12921 cvqualified parameter. */
12922 if (TREE_CODE (parm
) == METHOD_TYPE
12923 && (!check_cv_quals_for_unify
12925 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
12926 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
12929 if (unify (tparms
, targs
, TREE_TYPE (parm
),
12930 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
12932 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
12933 TYPE_ARG_TYPES (arg
), 1, DEDUCE_EXACT
,
12937 /* Unify a pointer to member with a pointer to member function, which
12938 deduces the type of the member as a function type. */
12939 if (TYPE_PTRMEMFUNC_P (arg
))
12943 cp_cv_quals cv_quals
;
12945 /* Check top-level cv qualifiers */
12946 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
12949 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
12950 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
12953 /* Determine the type of the function we are unifying against. */
12954 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
12956 build_function_type (TREE_TYPE (method_type
),
12957 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
12959 /* Extract the cv-qualifiers of the member function from the
12960 implicit object parameter and place them on the function
12961 type to be restored later. */
12963 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type
))));
12964 fntype
= build_qualified_type (fntype
, cv_quals
);
12965 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
12968 if (TREE_CODE (arg
) != OFFSET_TYPE
)
12970 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
12971 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
12973 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
12977 if (DECL_TEMPLATE_PARM_P (parm
))
12978 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
12979 if (arg
!= integral_constant_value (parm
))
12984 case TEMPLATE_DECL
:
12985 /* Matched cases are handled by the ARG == PARM test above. */
12988 case TYPE_ARGUMENT_PACK
:
12989 case NONTYPE_ARGUMENT_PACK
:
12991 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
12992 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
12993 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
12994 int argslen
= TREE_VEC_LENGTH (packed_args
);
12995 int parm_variadic_p
= 0;
12997 /* Check if the parameters end in a pack, making them variadic. */
12999 && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, len
- 1)))
13000 parm_variadic_p
= 1;
13002 /* If we don't have enough arguments to satisfy the parameters
13003 (not counting the pack expression at the end), or we have
13004 too many arguments for a parameter list that doesn't end in
13005 a pack expression, we can't unify. */
13006 if (argslen
< (len
- parm_variadic_p
)
13007 || (argslen
> len
&& !parm_variadic_p
))
13010 /* Unify all of the parameters that precede the (optional)
13011 pack expression. */
13012 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
13014 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
13015 TREE_VEC_ELT (packed_args
, i
), strict
))
13019 if (parm_variadic_p
)
13020 return unify_pack_expansion (tparms
, targs
,
13021 packed_parms
, packed_args
,
13022 strict
, /*call_args_p=*/false,
13030 case DECLTYPE_TYPE
:
13031 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
13036 gcc_assert (EXPR_P (parm
));
13038 /* We must be looking at an expression. This can happen with
13042 void foo(S<I>, S<I + 2>);
13044 This is a "nondeduced context":
13048 The nondeduced contexts are:
13050 --A type that is a template-id in which one or more of
13051 the template-arguments is an expression that references
13052 a template-parameter.
13054 In these cases, we assume deduction succeeded, but don't
13055 actually infer any unifications. */
13057 if (!uses_template_parms (parm
)
13058 && !template_args_equal (parm
, arg
))
13065 /* Note that DECL can be defined in this translation unit, if
13069 mark_definable (tree decl
)
13072 DECL_NOT_REALLY_EXTERN (decl
) = 1;
13073 FOR_EACH_CLONE (clone
, decl
)
13074 DECL_NOT_REALLY_EXTERN (clone
) = 1;
13077 /* Called if RESULT is explicitly instantiated, or is a member of an
13078 explicitly instantiated class. */
13081 mark_decl_instantiated (tree result
, int extern_p
)
13083 SET_DECL_EXPLICIT_INSTANTIATION (result
);
13085 /* If this entity has already been written out, it's too late to
13086 make any modifications. */
13087 if (TREE_ASM_WRITTEN (result
))
13090 if (TREE_CODE (result
) != FUNCTION_DECL
)
13091 /* The TREE_PUBLIC flag for function declarations will have been
13092 set correctly by tsubst. */
13093 TREE_PUBLIC (result
) = 1;
13095 /* This might have been set by an earlier implicit instantiation. */
13096 DECL_COMDAT (result
) = 0;
13099 DECL_NOT_REALLY_EXTERN (result
) = 0;
13102 mark_definable (result
);
13103 /* Always make artificials weak. */
13104 if (DECL_ARTIFICIAL (result
) && flag_weak
)
13105 comdat_linkage (result
);
13106 /* For WIN32 we also want to put explicit instantiations in
13107 linkonce sections. */
13108 else if (TREE_PUBLIC (result
))
13109 maybe_make_one_only (result
);
13112 /* If EXTERN_P, then this function will not be emitted -- unless
13113 followed by an explicit instantiation, at which point its linkage
13114 will be adjusted. If !EXTERN_P, then this function will be
13115 emitted here. In neither circumstance do we want
13116 import_export_decl to adjust the linkage. */
13117 DECL_INTERFACE_KNOWN (result
) = 1;
13120 /* Given two function templates PAT1 and PAT2, return:
13122 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
13123 -1 if PAT2 is more specialized than PAT1.
13124 0 if neither is more specialized.
13126 LEN indicates the number of parameters we should consider
13127 (defaulted parameters should not be considered).
13129 The 1998 std underspecified function template partial ordering, and
13130 DR214 addresses the issue. We take pairs of arguments, one from
13131 each of the templates, and deduce them against each other. One of
13132 the templates will be more specialized if all the *other*
13133 template's arguments deduce against its arguments and at least one
13134 of its arguments *does* *not* deduce against the other template's
13135 corresponding argument. Deduction is done as for class templates.
13136 The arguments used in deduction have reference and top level cv
13137 qualifiers removed. Iff both arguments were originally reference
13138 types *and* deduction succeeds in both directions, the template
13139 with the more cv-qualified argument wins for that pairing (if
13140 neither is more cv-qualified, they both are equal). Unlike regular
13141 deduction, after all the arguments have been deduced in this way,
13142 we do *not* verify the deduced template argument values can be
13143 substituted into non-deduced contexts, nor do we have to verify
13144 that all template arguments have been deduced. */
13147 more_specialized_fn (tree pat1
, tree pat2
, int len
)
13149 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
13150 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
13151 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
13152 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
13153 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
13154 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
13155 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
13156 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
13160 /* Remove the this parameter from non-static member functions. If
13161 one is a non-static member function and the other is not a static
13162 member function, remove the first parameter from that function
13163 also. This situation occurs for operator functions where we
13164 locate both a member function (with this pointer) and non-member
13165 operator (with explicit first operand). */
13166 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
13168 len
--; /* LEN is the number of significant arguments for DECL1 */
13169 args1
= TREE_CHAIN (args1
);
13170 if (!DECL_STATIC_FUNCTION_P (decl2
))
13171 args2
= TREE_CHAIN (args2
);
13173 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
13175 args2
= TREE_CHAIN (args2
);
13176 if (!DECL_STATIC_FUNCTION_P (decl1
))
13179 args1
= TREE_CHAIN (args1
);
13183 /* If only one is a conversion operator, they are unordered. */
13184 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
13187 /* Consider the return type for a conversion function */
13188 if (DECL_CONV_FN_P (decl1
))
13190 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
13191 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
13195 processing_template_decl
++;
13199 tree arg1
= TREE_VALUE (args1
);
13200 tree arg2
= TREE_VALUE (args2
);
13201 int deduce1
, deduce2
;
13205 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
13206 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13208 /* When both arguments are pack expansions, we need only
13209 unify the patterns themselves. */
13210 arg1
= PACK_EXPANSION_PATTERN (arg1
);
13211 arg2
= PACK_EXPANSION_PATTERN (arg2
);
13213 /* This is the last comparison we need to do. */
13217 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
13219 arg1
= TREE_TYPE (arg1
);
13220 quals1
= cp_type_quals (arg1
);
13223 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
13225 arg2
= TREE_TYPE (arg2
);
13226 quals2
= cp_type_quals (arg2
);
13229 if ((quals1
< 0) != (quals2
< 0))
13231 /* Only of the args is a reference, see if we should apply
13232 array/function pointer decay to it. This is not part of
13233 DR214, but is, IMHO, consistent with the deduction rules
13234 for the function call itself, and with our earlier
13235 implementation of the underspecified partial ordering
13236 rules. (nathan). */
13239 switch (TREE_CODE (arg1
))
13242 arg1
= TREE_TYPE (arg1
);
13244 case FUNCTION_TYPE
:
13245 arg1
= build_pointer_type (arg1
);
13254 switch (TREE_CODE (arg2
))
13257 arg2
= TREE_TYPE (arg2
);
13259 case FUNCTION_TYPE
:
13260 arg2
= build_pointer_type (arg2
);
13269 arg1
= TYPE_MAIN_VARIANT (arg1
);
13270 arg2
= TYPE_MAIN_VARIANT (arg2
);
13272 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
13274 int i
, len2
= list_length (args2
);
13275 tree parmvec
= make_tree_vec (1);
13276 tree argvec
= make_tree_vec (len2
);
13279 /* Setup the parameter vector, which contains only ARG1. */
13280 TREE_VEC_ELT (parmvec
, 0) = arg1
;
13282 /* Setup the argument vector, which contains the remaining
13284 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
13285 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
13287 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
13288 argvec
, UNIFY_ALLOW_NONE
,
13289 /*call_args_p=*/false,
13292 /* We cannot deduce in the other direction, because ARG1 is
13293 a pack expansion but ARG2 is not. */
13296 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13298 int i
, len1
= list_length (args1
);
13299 tree parmvec
= make_tree_vec (1);
13300 tree argvec
= make_tree_vec (len1
);
13303 /* Setup the parameter vector, which contains only ARG1. */
13304 TREE_VEC_ELT (parmvec
, 0) = arg2
;
13306 /* Setup the argument vector, which contains the remaining
13308 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
13309 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
13311 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
13312 argvec
, UNIFY_ALLOW_NONE
,
13313 /*call_args_p=*/false,
13316 /* We cannot deduce in the other direction, because ARG2 is
13317 a pack expansion but ARG1 is not.*/
13323 /* The normal case, where neither argument is a pack
13325 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
13326 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
13333 if (better1
< 0 && better2
< 0)
13334 /* We've failed to deduce something in either direction.
13335 These must be unordered. */
13338 if (deduce1
&& deduce2
&& quals1
>= 0 && quals2
>= 0)
13340 /* Deduces in both directions, see if quals can
13341 disambiguate. Pretend the worse one failed to deduce. */
13342 if ((quals1
& quals2
) == quals2
)
13344 if ((quals1
& quals2
) == quals1
)
13347 if (deduce1
&& !deduce2
&& !better2
)
13349 if (deduce2
&& !deduce1
&& !better1
)
13352 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
13353 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
13354 /* We have already processed all of the arguments in our
13355 handing of the pack expansion type. */
13358 args1
= TREE_CHAIN (args1
);
13359 args2
= TREE_CHAIN (args2
);
13362 processing_template_decl
--;
13364 /* All things being equal, if the next argument is a pack expansion
13365 for one function but not for the other, prefer the
13366 non-variadic function. */
13367 if ((better1
> 0) - (better2
> 0) == 0
13368 && args1
&& TREE_VALUE (args1
)
13369 && args2
&& TREE_VALUE (args2
))
13371 if (TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
)
13372 return TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
? 0 : -1;
13373 else if (TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
)
13377 return (better1
> 0) - (better2
> 0);
13380 /* Determine which of two partial specializations is more specialized.
13382 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13383 to the first partial specialization. The TREE_VALUE is the
13384 innermost set of template parameters for the partial
13385 specialization. PAT2 is similar, but for the second template.
13387 Return 1 if the first partial specialization is more specialized;
13388 -1 if the second is more specialized; 0 if neither is more
13391 See [temp.class.order] for information about determining which of
13392 two templates is more specialized. */
13395 more_specialized_class (tree pat1
, tree pat2
)
13400 bool any_deductions
= false;
13402 tmpl1
= TREE_TYPE (pat1
);
13403 tmpl2
= TREE_TYPE (pat2
);
13405 /* Just like what happens for functions, if we are ordering between
13406 different class template specializations, we may encounter dependent
13407 types in the arguments, and we need our dependency check functions
13408 to behave correctly. */
13409 ++processing_template_decl
;
13410 targs
= get_class_bindings (TREE_VALUE (pat1
),
13411 CLASSTYPE_TI_ARGS (tmpl1
),
13412 CLASSTYPE_TI_ARGS (tmpl2
));
13416 any_deductions
= true;
13419 targs
= get_class_bindings (TREE_VALUE (pat2
),
13420 CLASSTYPE_TI_ARGS (tmpl2
),
13421 CLASSTYPE_TI_ARGS (tmpl1
));
13425 any_deductions
= true;
13427 --processing_template_decl
;
13429 /* In the case of a tie where at least one of the class templates
13430 has a parameter pack at the end, the template with the most
13431 non-packed parameters wins. */
13434 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
13435 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
13437 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
13438 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
13439 int len1
= TREE_VEC_LENGTH (args1
);
13440 int len2
= TREE_VEC_LENGTH (args2
);
13442 /* We don't count the pack expansion at the end. */
13443 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
13445 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
13450 else if (len1
< len2
)
13457 /* Return the template arguments that will produce the function signature
13458 DECL from the function template FN, with the explicit template
13459 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13460 also match. Return NULL_TREE if no satisfactory arguments could be
13464 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
13466 int ntparms
= DECL_NTPARMS (fn
);
13467 tree targs
= make_tree_vec (ntparms
);
13469 tree decl_arg_types
;
13471 /* Substitute the explicit template arguments into the type of DECL.
13472 The call to fn_type_unification will handle substitution into the
13474 decl_type
= TREE_TYPE (decl
);
13475 if (explicit_args
&& uses_template_parms (decl_type
))
13478 tree converted_args
;
13480 if (DECL_TEMPLATE_INFO (decl
))
13481 tmpl
= DECL_TI_TEMPLATE (decl
);
13483 /* We can get here for some invalid specializations. */
13487 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
13488 explicit_args
, NULL_TREE
,
13490 /*require_all_args=*/false,
13491 /*use_default_args=*/false);
13492 if (converted_args
== error_mark_node
)
13495 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
13496 if (decl_type
== error_mark_node
)
13500 /* Never do unification on the 'this' parameter. */
13501 decl_arg_types
= skip_artificial_parms_for (decl
,
13502 TYPE_ARG_TYPES (decl_type
));
13504 if (fn_type_unification (fn
, explicit_args
, targs
,
13506 (check_rettype
|| DECL_CONV_FN_P (fn
)
13507 ? TREE_TYPE (decl_type
) : NULL_TREE
),
13508 DEDUCE_EXACT
, LOOKUP_NORMAL
))
13514 /* Return the innermost template arguments that, when applied to a
13515 template specialization whose innermost template parameters are
13516 TPARMS, and whose specialization arguments are PARMS, yield the
13519 For example, suppose we have:
13521 template <class T, class U> struct S {};
13522 template <class T> struct S<T*, int> {};
13524 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13525 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13526 int}. The resulting vector will be {double}, indicating that `T'
13527 is bound to `double'. */
13530 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
13532 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
13534 tree innermost_deduced_args
;
13536 innermost_deduced_args
= make_tree_vec (ntparms
);
13537 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13539 deduced_args
= copy_node (args
);
13540 SET_TMPL_ARGS_LEVEL (deduced_args
,
13541 TMPL_ARGS_DEPTH (deduced_args
),
13542 innermost_deduced_args
);
13545 deduced_args
= innermost_deduced_args
;
13547 if (unify (tparms
, deduced_args
,
13548 INNERMOST_TEMPLATE_ARGS (spec_args
),
13549 INNERMOST_TEMPLATE_ARGS (args
),
13553 for (i
= 0; i
< ntparms
; ++i
)
13554 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
13557 /* Verify that nondeduced template arguments agree with the type
13558 obtained from argument deduction.
13562 struct A { typedef int X; };
13563 template <class T, class U> struct C {};
13564 template <class T> struct C<T, typename T::X> {};
13566 Then with the instantiation `C<A, int>', we can deduce that
13567 `T' is `A' but unify () does not check whether `typename T::X'
13569 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
13570 if (spec_args
== error_mark_node
13571 /* We only need to check the innermost arguments; the other
13572 arguments will always agree. */
13573 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
13574 INNERMOST_TEMPLATE_ARGS (args
)))
13577 return deduced_args
;
13580 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13581 Return the TREE_LIST node with the most specialized template, if
13582 any. If there is no most specialized template, the error_mark_node
13585 Note that this function does not look at, or modify, the
13586 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13587 returned is one of the elements of INSTANTIATIONS, callers may
13588 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13589 and retrieve it from the value returned. */
13592 most_specialized_instantiation (tree templates
)
13596 ++processing_template_decl
;
13599 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
13603 if (get_bindings (TREE_VALUE (champ
),
13604 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
13605 NULL_TREE
, /*check_ret=*/false))
13608 if (get_bindings (TREE_VALUE (fn
),
13609 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
13610 NULL_TREE
, /*check_ret=*/false))
13617 /* Equally specialized, move to next function. If there
13618 is no next function, nothing's most specialized. */
13619 fn
= TREE_CHAIN (fn
);
13627 /* Now verify that champ is better than everything earlier in the
13628 instantiation list. */
13629 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
13630 if (get_bindings (TREE_VALUE (champ
),
13631 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
13632 NULL_TREE
, /*check_ret=*/false)
13633 || !get_bindings (TREE_VALUE (fn
),
13634 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
13635 NULL_TREE
, /*check_ret=*/false))
13641 processing_template_decl
--;
13644 return error_mark_node
;
13649 /* If DECL is a specialization of some template, return the most
13650 general such template. Otherwise, returns NULL_TREE.
13652 For example, given:
13654 template <class T> struct S { template <class U> void f(U); };
13656 if TMPL is `template <class U> void S<int>::f(U)' this will return
13657 the full template. This function will not trace past partial
13658 specializations, however. For example, given in addition:
13660 template <class T> struct S<T*> { template <class U> void f(U); };
13662 if TMPL is `template <class U> void S<int*>::f(U)' this will return
13663 `template <class T> template <class U> S<T*>::f(U)'. */
13666 most_general_template (tree decl
)
13668 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
13669 an immediate specialization. */
13670 if (TREE_CODE (decl
) == FUNCTION_DECL
)
13672 if (DECL_TEMPLATE_INFO (decl
)) {
13673 decl
= DECL_TI_TEMPLATE (decl
);
13675 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
13676 template friend. */
13677 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
13683 /* Look for more and more general templates. */
13684 while (DECL_TEMPLATE_INFO (decl
))
13686 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
13687 (See cp-tree.h for details.) */
13688 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
13691 if (CLASS_TYPE_P (TREE_TYPE (decl
))
13692 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
13695 /* Stop if we run into an explicitly specialized class template. */
13696 if (!DECL_NAMESPACE_SCOPE_P (decl
)
13697 && DECL_CONTEXT (decl
)
13698 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
13701 decl
= DECL_TI_TEMPLATE (decl
);
13707 /* Return the most specialized of the class template partial
13708 specializations of TMPL which can produce TYPE, a specialization of
13709 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
13710 a _TYPE node corresponding to the partial specialization, while the
13711 TREE_PURPOSE is the set of template arguments that must be
13712 substituted into the TREE_TYPE in order to generate TYPE.
13714 If the choice of partial specialization is ambiguous, a diagnostic
13715 is issued, and the error_mark_node is returned. If there are no
13716 partial specializations of TMPL matching TYPE, then NULL_TREE is
13720 most_specialized_class (tree type
, tree tmpl
)
13722 tree list
= NULL_TREE
;
13728 tree outer_args
= NULL_TREE
;
13730 tmpl
= most_general_template (tmpl
);
13731 args
= CLASSTYPE_TI_ARGS (type
);
13733 /* For determining which partial specialization to use, only the
13734 innermost args are interesting. */
13735 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
13737 outer_args
= strip_innermost_template_args (args
, 1);
13738 args
= INNERMOST_TEMPLATE_ARGS (args
);
13741 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
13743 tree partial_spec_args
;
13745 tree parms
= TREE_VALUE (t
);
13747 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
13752 ++processing_template_decl
;
13754 /* Discard the outer levels of args, and then substitute in the
13755 template args from the enclosing class. */
13756 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
13757 partial_spec_args
= tsubst_template_args
13758 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
13760 /* PARMS already refers to just the innermost parms, but the
13761 template parms in partial_spec_args had their levels lowered
13762 by tsubst, so we need to do the same for the parm list. We
13763 can't just tsubst the TREE_VEC itself, as tsubst wants to
13764 treat a TREE_VEC as an argument vector. */
13765 parms
= copy_node (parms
);
13766 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
13767 TREE_VEC_ELT (parms
, i
) =
13768 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
13770 --processing_template_decl
;
13772 spec_args
= get_class_bindings (parms
,
13778 spec_args
= add_to_template_args (outer_args
, spec_args
);
13779 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
13780 TREE_TYPE (list
) = TREE_TYPE (t
);
13787 ambiguous_p
= false;
13790 t
= TREE_CHAIN (t
);
13791 for (; t
; t
= TREE_CHAIN (t
))
13793 fate
= more_specialized_class (champ
, t
);
13800 t
= TREE_CHAIN (t
);
13803 ambiguous_p
= true;
13812 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
13814 fate
= more_specialized_class (champ
, t
);
13817 ambiguous_p
= true;
13824 const char *str
= "candidates are:";
13825 error ("ambiguous class template instantiation for %q#T", type
);
13826 for (t
= list
; t
; t
= TREE_CHAIN (t
))
13828 error ("%s %+#T", str
, TREE_TYPE (t
));
13831 return error_mark_node
;
13837 /* Explicitly instantiate DECL. */
13840 do_decl_instantiation (tree decl
, tree storage
)
13842 tree result
= NULL_TREE
;
13845 if (!decl
|| decl
== error_mark_node
)
13846 /* An error occurred, for which grokdeclarator has already issued
13847 an appropriate message. */
13849 else if (! DECL_LANG_SPECIFIC (decl
))
13851 error ("explicit instantiation of non-template %q#D", decl
);
13854 else if (TREE_CODE (decl
) == VAR_DECL
)
13856 /* There is an asymmetry here in the way VAR_DECLs and
13857 FUNCTION_DECLs are handled by grokdeclarator. In the case of
13858 the latter, the DECL we get back will be marked as a
13859 template instantiation, and the appropriate
13860 DECL_TEMPLATE_INFO will be set up. This does not happen for
13861 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
13862 should handle VAR_DECLs as it currently handles
13864 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
13865 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
13867 error ("no matching template for %qD found", decl
);
13870 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
13872 error ("type %qT for explicit instantiation %qD does not match "
13873 "declared type %qT", TREE_TYPE (result
), decl
,
13878 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
13880 error ("explicit instantiation of %q#D", decl
);
13886 /* Check for various error cases. Note that if the explicit
13887 instantiation is valid the RESULT will currently be marked as an
13888 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
13889 until we get here. */
13891 if (DECL_TEMPLATE_SPECIALIZATION (result
))
13893 /* DR 259 [temp.spec].
13895 Both an explicit instantiation and a declaration of an explicit
13896 specialization shall not appear in a program unless the explicit
13897 instantiation follows a declaration of the explicit specialization.
13899 For a given set of template parameters, if an explicit
13900 instantiation of a template appears after a declaration of an
13901 explicit specialization for that template, the explicit
13902 instantiation has no effect. */
13905 else if (DECL_EXPLICIT_INSTANTIATION (result
))
13909 No program shall explicitly instantiate any template more
13912 We check DECL_NOT_REALLY_EXTERN so as not to complain when
13913 the first instantiation was `extern' and the second is not,
13914 and EXTERN_P for the opposite case. */
13915 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
13916 pedwarn ("duplicate explicit instantiation of %q#D", result
);
13917 /* If an "extern" explicit instantiation follows an ordinary
13918 explicit instantiation, the template is instantiated. */
13922 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
13924 error ("no matching template for %qD found", result
);
13927 else if (!DECL_TEMPLATE_INFO (result
))
13929 pedwarn ("explicit instantiation of non-template %q#D", result
);
13933 if (storage
== NULL_TREE
)
13935 else if (storage
== ridpointers
[(int) RID_EXTERN
])
13937 if (pedantic
&& !in_system_header
)
13938 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
13943 error ("storage class %qD applied to template instantiation", storage
);
13945 check_explicit_instantiation_namespace (result
);
13946 mark_decl_instantiated (result
, extern_p
);
13948 instantiate_decl (result
, /*defer_ok=*/1,
13949 /*expl_inst_class_mem_p=*/false);
13953 mark_class_instantiated (tree t
, int extern_p
)
13955 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
13956 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
13957 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
13958 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
13961 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
13962 rest_of_type_compilation (t
, 1);
13966 /* Called from do_type_instantiation through binding_table_foreach to
13967 do recursive instantiation for the type bound in ENTRY. */
13969 bt_instantiate_type_proc (binding_entry entry
, void *data
)
13971 tree storage
= *(tree
*) data
;
13973 if (IS_AGGR_TYPE (entry
->type
)
13974 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
13975 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
13978 /* Called from do_type_instantiation to instantiate a member
13979 (a member function or a static member variable) of an
13980 explicitly instantiated class template. */
13982 instantiate_class_member (tree decl
, int extern_p
)
13984 mark_decl_instantiated (decl
, extern_p
);
13986 instantiate_decl (decl
, /*defer_ok=*/1,
13987 /*expl_inst_class_mem_p=*/true);
13990 /* Perform an explicit instantiation of template class T. STORAGE, if
13991 non-null, is the RID for extern, inline or static. COMPLAIN is
13992 nonzero if this is called from the parser, zero if called recursively,
13993 since the standard is unclear (as detailed below). */
13996 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
14001 int previous_instantiation_extern_p
= 0;
14003 if (TREE_CODE (t
) == TYPE_DECL
)
14006 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
14008 error ("explicit instantiation of non-template type %qT", t
);
14014 if (!COMPLETE_TYPE_P (t
))
14016 if (complain
& tf_error
)
14017 error ("explicit instantiation of %q#T before definition of template",
14022 if (storage
!= NULL_TREE
)
14024 if (pedantic
&& !in_system_header
)
14025 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
14028 if (storage
== ridpointers
[(int) RID_INLINE
])
14030 else if (storage
== ridpointers
[(int) RID_EXTERN
])
14032 else if (storage
== ridpointers
[(int) RID_STATIC
])
14036 error ("storage class %qD applied to template instantiation",
14042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
14044 /* DR 259 [temp.spec].
14046 Both an explicit instantiation and a declaration of an explicit
14047 specialization shall not appear in a program unless the explicit
14048 instantiation follows a declaration of the explicit specialization.
14050 For a given set of template parameters, if an explicit
14051 instantiation of a template appears after a declaration of an
14052 explicit specialization for that template, the explicit
14053 instantiation has no effect. */
14056 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
14060 No program shall explicitly instantiate any template more
14063 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
14064 instantiation was `extern'. If EXTERN_P then the second is.
14065 These cases are OK. */
14066 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
14068 if (!previous_instantiation_extern_p
&& !extern_p
14069 && (complain
& tf_error
))
14070 pedwarn ("duplicate explicit instantiation of %q#T", t
);
14072 /* If we've already instantiated the template, just return now. */
14073 if (!CLASSTYPE_INTERFACE_ONLY (t
))
14077 check_explicit_instantiation_namespace (TYPE_NAME (t
));
14078 mark_class_instantiated (t
, extern_p
);
14086 /* In contrast to implicit instantiation, where only the
14087 declarations, and not the definitions, of members are
14088 instantiated, we have here:
14092 The explicit instantiation of a class template specialization
14093 implies the instantiation of all of its members not
14094 previously explicitly specialized in the translation unit
14095 containing the explicit instantiation.
14097 Of course, we can't instantiate member template classes, since
14098 we don't have any arguments for them. Note that the standard
14099 is unclear on whether the instantiation of the members are
14100 *explicit* instantiations or not. However, the most natural
14101 interpretation is that it should be an explicit instantiation. */
14104 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
14105 if (TREE_CODE (tmp
) == FUNCTION_DECL
14106 && DECL_TEMPLATE_INSTANTIATION (tmp
))
14107 instantiate_class_member (tmp
, extern_p
);
14109 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
14110 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
14111 instantiate_class_member (tmp
, extern_p
);
14113 if (CLASSTYPE_NESTED_UTDS (t
))
14114 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
14115 bt_instantiate_type_proc
, &storage
);
14119 /* Given a function DECL, which is a specialization of TMPL, modify
14120 DECL to be a re-instantiation of TMPL with the same template
14121 arguments. TMPL should be the template into which tsubst'ing
14122 should occur for DECL, not the most general template.
14124 One reason for doing this is a scenario like this:
14127 void f(const T&, int i);
14129 void g() { f(3, 7); }
14132 void f(const T& t, const int i) { }
14134 Note that when the template is first instantiated, with
14135 instantiate_template, the resulting DECL will have no name for the
14136 first parameter, and the wrong type for the second. So, when we go
14137 to instantiate the DECL, we regenerate it. */
14140 regenerate_decl_from_template (tree decl
, tree tmpl
)
14142 /* The arguments used to instantiate DECL, from the most general
14147 args
= DECL_TI_ARGS (decl
);
14148 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14150 /* Make sure that we can see identifiers, and compute access
14152 push_access_scope (decl
);
14154 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14162 args_depth
= TMPL_ARGS_DEPTH (args
);
14163 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
14164 if (args_depth
> parms_depth
)
14165 args
= get_innermost_template_args (args
, parms_depth
);
14167 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
14168 args
, tf_error
, NULL_TREE
);
14170 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
14173 /* Merge parameter declarations. */
14174 decl_parm
= skip_artificial_parms_for (decl
,
14175 DECL_ARGUMENTS (decl
));
14177 = skip_artificial_parms_for (code_pattern
,
14178 DECL_ARGUMENTS (code_pattern
));
14179 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
14184 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
14185 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
14186 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
14188 parm_type
= type_decays_to (parm_type
);
14189 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
14190 TREE_TYPE (decl_parm
) = parm_type
;
14191 attributes
= DECL_ATTRIBUTES (pattern_parm
);
14192 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
14194 DECL_ATTRIBUTES (decl_parm
) = attributes
;
14195 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
14197 decl_parm
= TREE_CHAIN (decl_parm
);
14198 pattern_parm
= TREE_CHAIN (pattern_parm
);
14200 /* Merge any parameters that match with the function parameter
14202 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
14205 tree expanded_types
;
14206 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14207 the parameters in this function parameter pack. */
14208 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
14209 args
, tf_error
, NULL_TREE
);
14210 len
= TREE_VEC_LENGTH (expanded_types
);
14211 for (i
= 0; i
< len
; i
++)
14216 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
14217 /* Rename the parameter to include the index. */
14218 DECL_NAME (decl_parm
) =
14219 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
14220 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
14221 parm_type
= type_decays_to (parm_type
);
14222 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
14223 TREE_TYPE (decl_parm
) = parm_type
;
14224 attributes
= DECL_ATTRIBUTES (pattern_parm
);
14225 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
14227 DECL_ATTRIBUTES (decl_parm
) = attributes
;
14228 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
14230 decl_parm
= TREE_CHAIN (decl_parm
);
14233 /* Merge additional specifiers from the CODE_PATTERN. */
14234 if (DECL_DECLARED_INLINE_P (code_pattern
)
14235 && !DECL_DECLARED_INLINE_P (decl
))
14236 DECL_DECLARED_INLINE_P (decl
) = 1;
14237 if (DECL_INLINE (code_pattern
) && !DECL_INLINE (decl
))
14238 DECL_INLINE (decl
) = 1;
14240 else if (TREE_CODE (decl
) == VAR_DECL
)
14241 DECL_INITIAL (decl
) =
14242 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
14243 tf_error
, DECL_TI_TEMPLATE (decl
),
14244 /*integral_constant_expression_p=*/false);
14246 gcc_unreachable ();
14248 pop_access_scope (decl
);
14251 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
14252 substituted to get DECL. */
14255 template_for_substitution (tree decl
)
14257 tree tmpl
= DECL_TI_TEMPLATE (decl
);
14259 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
14260 for the instantiation. This is not always the most general
14261 template. Consider, for example:
14264 struct S { template <class U> void f();
14265 template <> void f<int>(); };
14267 and an instantiation of S<double>::f<int>. We want TD to be the
14268 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
14269 while (/* An instantiation cannot have a definition, so we need a
14270 more general template. */
14271 DECL_TEMPLATE_INSTANTIATION (tmpl
)
14272 /* We must also deal with friend templates. Given:
14274 template <class T> struct S {
14275 template <class U> friend void f() {};
14278 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
14279 so far as the language is concerned, but that's still
14280 where we get the pattern for the instantiation from. On
14281 other hand, if the definition comes outside the class, say:
14283 template <class T> struct S {
14284 template <class U> friend void f();
14286 template <class U> friend void f() {}
14288 we don't need to look any further. That's what the check for
14289 DECL_INITIAL is for. */
14290 || (TREE_CODE (decl
) == FUNCTION_DECL
14291 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
14292 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
14294 /* The present template, TD, should not be a definition. If it
14295 were a definition, we should be using it! Note that we
14296 cannot restructure the loop to just keep going until we find
14297 a template with a definition, since that might go too far if
14298 a specialization was declared, but not defined. */
14299 gcc_assert (TREE_CODE (decl
) != VAR_DECL
14300 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
14302 /* Fetch the more general template. */
14303 tmpl
= DECL_TI_TEMPLATE (tmpl
);
14309 /* Produce the definition of D, a _DECL generated from a template. If
14310 DEFER_OK is nonzero, then we don't have to actually do the
14311 instantiation now; we just have to do it sometime. Normally it is
14312 an error if this is an explicit instantiation but D is undefined.
14313 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
14314 explicitly instantiated class template. */
14317 instantiate_decl (tree d
, int defer_ok
,
14318 bool expl_inst_class_mem_p
)
14320 tree tmpl
= DECL_TI_TEMPLATE (d
);
14327 bool pattern_defined
;
14329 location_t saved_loc
= input_location
;
14330 int saved_in_system_header
= in_system_header
;
14333 /* This function should only be used to instantiate templates for
14334 functions and static member variables. */
14335 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
14336 || TREE_CODE (d
) == VAR_DECL
);
14338 /* Variables are never deferred; if instantiation is required, they
14339 are instantiated right away. That allows for better code in the
14340 case that an expression refers to the value of the variable --
14341 if the variable has a constant value the referring expression can
14342 take advantage of that fact. */
14343 if (TREE_CODE (d
) == VAR_DECL
)
14346 /* Don't instantiate cloned functions. Instead, instantiate the
14347 functions they cloned. */
14348 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
14349 d
= DECL_CLONED_FUNCTION (d
);
14351 if (DECL_TEMPLATE_INSTANTIATED (d
))
14352 /* D has already been instantiated. It might seem reasonable to
14353 check whether or not D is an explicit instantiation, and, if so,
14354 stop here. But when an explicit instantiation is deferred
14355 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
14356 is set, even though we still need to do the instantiation. */
14359 /* If we already have a specialization of this declaration, then
14360 there's no reason to instantiate it. Note that
14361 retrieve_specialization gives us both instantiations and
14362 specializations, so we must explicitly check
14363 DECL_TEMPLATE_SPECIALIZATION. */
14364 gen_tmpl
= most_general_template (tmpl
);
14365 gen_args
= DECL_TI_ARGS (d
);
14366 spec
= retrieve_specialization (gen_tmpl
, gen_args
,
14367 /*class_specializations_p=*/false);
14368 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
14371 /* This needs to happen before any tsubsting. */
14372 if (! push_tinst_level (d
))
14375 timevar_push (TV_PARSE
);
14377 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
14378 for the instantiation. */
14379 td
= template_for_substitution (d
);
14380 code_pattern
= DECL_TEMPLATE_RESULT (td
);
14382 /* We should never be trying to instantiate a member of a class
14383 template or partial specialization. */
14384 gcc_assert (d
!= code_pattern
);
14386 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
14387 || DECL_TEMPLATE_SPECIALIZATION (td
))
14388 /* In the case of a friend template whose definition is provided
14389 outside the class, we may have too many arguments. Drop the
14390 ones we don't need. The same is true for specializations. */
14391 args
= get_innermost_template_args
14392 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
14396 if (TREE_CODE (d
) == FUNCTION_DECL
)
14397 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
14399 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
14401 /* We may be in the middle of deferred access check. Disable it now. */
14402 push_deferring_access_checks (dk_no_deferred
);
14404 /* Unless an explicit instantiation directive has already determined
14405 the linkage of D, remember that a definition is available for
14407 if (pattern_defined
14408 && !DECL_INTERFACE_KNOWN (d
)
14409 && !DECL_NOT_REALLY_EXTERN (d
))
14410 mark_definable (d
);
14412 input_location
= DECL_SOURCE_LOCATION (d
);
14413 in_system_header
= DECL_IN_SYSTEM_HEADER (d
);
14415 /* If D is a member of an explicitly instantiated class template,
14416 and no definition is available, treat it like an implicit
14418 if (!pattern_defined
&& expl_inst_class_mem_p
14419 && DECL_EXPLICIT_INSTANTIATION (d
))
14421 DECL_NOT_REALLY_EXTERN (d
) = 0;
14422 DECL_INTERFACE_KNOWN (d
) = 0;
14423 SET_DECL_IMPLICIT_INSTANTIATION (d
);
14428 /* Recheck the substitutions to obtain any warning messages
14429 about ignoring cv qualifiers. */
14430 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
14431 tree type
= TREE_TYPE (gen
);
14433 /* Make sure that we can see identifiers, and compute access
14434 correctly. D is already the target FUNCTION_DECL with the
14436 push_access_scope (d
);
14438 if (TREE_CODE (gen
) == FUNCTION_DECL
)
14440 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
14441 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
14443 /* Don't simply tsubst the function type, as that will give
14444 duplicate warnings about poor parameter qualifications.
14445 The function arguments are the same as the decl_arguments
14446 without the top level cv qualifiers. */
14447 type
= TREE_TYPE (type
);
14449 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
14451 pop_access_scope (d
);
14454 /* Check to see whether we know that this template will be
14455 instantiated in some other file, as with "extern template"
14457 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
14458 /* In general, we do not instantiate such templates... */
14460 /* ... but we instantiate inline functions so that we can inline
14462 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
))
14463 /* ... we instantiate static data members whose values are
14464 needed in integral constant expressions. */
14465 && ! (TREE_CODE (d
) == VAR_DECL
14466 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d
)))
14468 /* Defer all other templates, unless we have been explicitly
14469 forbidden from doing so. */
14470 if (/* If there is no definition, we cannot instantiate the
14473 /* If it's OK to postpone instantiation, do so. */
14475 /* If this is a static data member that will be defined
14476 elsewhere, we don't want to instantiate the entire data
14477 member, but we do want to instantiate the initializer so that
14478 we can substitute that elsewhere. */
14479 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
14481 /* The definition of the static data member is now required so
14482 we must substitute the initializer. */
14483 if (TREE_CODE (d
) == VAR_DECL
14484 && !DECL_INITIAL (d
)
14485 && DECL_INITIAL (code_pattern
))
14490 ns
= decl_namespace_context (d
);
14491 push_nested_namespace (ns
);
14492 push_nested_class (DECL_CONTEXT (d
));
14493 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
14495 tf_warning_or_error
, NULL_TREE
,
14496 /*integral_constant_expression_p=*/false);
14497 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
14498 /*asmspec_tree=*/NULL_TREE
,
14499 LOOKUP_ONLYCONVERTING
);
14500 pop_nested_class ();
14501 pop_nested_namespace (ns
);
14504 /* We restore the source position here because it's used by
14505 add_pending_template. */
14506 input_location
= saved_loc
;
14508 if (at_eof
&& !pattern_defined
14509 && DECL_EXPLICIT_INSTANTIATION (d
))
14512 The definition of a non-exported function template, a
14513 non-exported member function template, or a non-exported
14514 member function or static data member of a class template
14515 shall be present in every translation unit in which it is
14516 explicitly instantiated. */
14518 ("explicit instantiation of %qD but no definition available", d
);
14520 /* ??? Historically, we have instantiated inline functions, even
14521 when marked as "extern template". */
14522 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
14523 add_pending_template (d
);
14526 /* Tell the repository that D is available in this translation unit
14527 -- and see if it is supposed to be instantiated here. */
14528 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
14530 /* In a PCH file, despite the fact that the repository hasn't
14531 requested instantiation in the PCH it is still possible that
14532 an instantiation will be required in a file that includes the
14535 add_pending_template (d
);
14536 /* Instantiate inline functions so that the inliner can do its
14537 job, even though we'll not be emitting a copy of this
14539 if (!(TREE_CODE (d
) == FUNCTION_DECL
14540 && flag_inline_trees
14541 && DECL_DECLARED_INLINE_P (d
)))
14545 need_push
= !cfun
|| !global_bindings_p ();
14547 push_to_top_level ();
14549 /* Mark D as instantiated so that recursive calls to
14550 instantiate_decl do not try to instantiate it again. */
14551 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
14553 /* Regenerate the declaration in case the template has been modified
14554 by a subsequent redeclaration. */
14555 regenerate_decl_from_template (d
, td
);
14557 /* We already set the file and line above. Reset them now in case
14558 they changed as a result of calling regenerate_decl_from_template. */
14559 input_location
= DECL_SOURCE_LOCATION (d
);
14561 if (TREE_CODE (d
) == VAR_DECL
)
14565 /* Clear out DECL_RTL; whatever was there before may not be right
14566 since we've reset the type of the declaration. */
14567 SET_DECL_RTL (d
, NULL_RTX
);
14568 DECL_IN_AGGR_P (d
) = 0;
14570 /* The initializer is placed in DECL_INITIAL by
14571 regenerate_decl_from_template. Pull it out so that
14572 finish_decl can process it. */
14573 init
= DECL_INITIAL (d
);
14574 DECL_INITIAL (d
) = NULL_TREE
;
14575 DECL_INITIALIZED_P (d
) = 0;
14577 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14578 initializer. That function will defer actual emission until
14579 we have a chance to determine linkage. */
14580 DECL_EXTERNAL (d
) = 0;
14582 /* Enter the scope of D so that access-checking works correctly. */
14583 push_nested_class (DECL_CONTEXT (d
));
14584 finish_decl (d
, init
, NULL_TREE
);
14585 pop_nested_class ();
14587 else if (TREE_CODE (d
) == FUNCTION_DECL
)
14589 htab_t saved_local_specializations
;
14594 /* Save away the current list, in case we are instantiating one
14595 template from within the body of another. */
14596 saved_local_specializations
= local_specializations
;
14598 /* Set up the list of local specializations. */
14599 local_specializations
= htab_create (37,
14600 hash_local_specialization
,
14601 eq_local_specializations
,
14604 /* Set up context. */
14605 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
14607 /* Create substitution entries for the parameters. */
14608 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
14609 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
14610 spec_parm
= DECL_ARGUMENTS (d
);
14611 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
14613 register_local_specialization (spec_parm
, tmpl_parm
);
14614 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
14615 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
14617 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
14619 register_local_specialization (spec_parm
, tmpl_parm
);
14620 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
14621 spec_parm
= TREE_CHAIN (spec_parm
);
14623 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
14625 /* Collect all of the extra "packed" parameters into an
14629 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
14630 tree argtypepack
= make_node (TYPE_ARGUMENT_PACK
);
14634 /* Count how many parameters remain. */
14635 for (t
= spec_parm
; t
; t
= TREE_CHAIN (t
))
14638 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
14639 parmvec
= make_tree_vec (len
);
14640 parmtypevec
= make_tree_vec (len
);
14641 for(i
= 0; i
< len
; i
++, spec_parm
= TREE_CHAIN (spec_parm
))
14643 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
14644 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
14647 /* Build the argument packs. */
14648 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
14649 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
14650 TREE_TYPE (argpack
) = argtypepack
;
14652 /* Register the (value) argument pack as a specialization of
14653 TMPL_PARM, then move on. */
14654 register_local_specialization (argpack
, tmpl_parm
);
14655 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
14657 gcc_assert (!spec_parm
);
14659 /* Substitute into the body of the function. */
14660 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
14661 tf_warning_or_error
, tmpl
,
14662 /*integral_constant_expression_p=*/false);
14664 /* Set the current input_location to the end of the function
14665 so that finish_function knows where we are. */
14666 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
14668 /* We don't need the local specializations any more. */
14669 htab_delete (local_specializations
);
14670 local_specializations
= saved_local_specializations
;
14672 /* Finish the function. */
14673 d
= finish_function (0);
14674 expand_or_defer_fn (d
);
14677 /* We're not deferring instantiation any more. */
14678 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
14681 pop_from_top_level ();
14684 input_location
= saved_loc
;
14685 in_system_header
= saved_in_system_header
;
14686 pop_deferring_access_checks ();
14687 pop_tinst_level ();
14689 timevar_pop (TV_PARSE
);
14694 /* Run through the list of templates that we wish we could
14695 instantiate, and instantiate any we can. RETRIES is the
14696 number of times we retry pending template instantiation. */
14699 instantiate_pending_templates (int retries
)
14702 location_t saved_loc
= input_location
;
14703 int saved_in_system_header
= in_system_header
;
14705 /* Instantiating templates may trigger vtable generation. This in turn
14706 may require further template instantiations. We place a limit here
14707 to avoid infinite loop. */
14708 if (pending_templates
&& retries
>= max_tinst_depth
)
14710 tree decl
= pending_templates
->tinst
->decl
;
14712 error ("template instantiation depth exceeds maximum of %d"
14713 " instantiating %q+D, possibly from virtual table generation"
14714 " (use -ftemplate-depth-NN to increase the maximum)",
14715 max_tinst_depth
, decl
);
14716 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14717 /* Pretend that we defined it. */
14718 DECL_INITIAL (decl
) = error_mark_node
;
14724 struct pending_template
**t
= &pending_templates
;
14725 struct pending_template
*last
= NULL
;
14729 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
14730 bool complete
= false;
14732 if (TYPE_P (instantiation
))
14736 if (!COMPLETE_TYPE_P (instantiation
))
14738 instantiate_class_template (instantiation
);
14739 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
14740 for (fn
= TYPE_METHODS (instantiation
);
14742 fn
= TREE_CHAIN (fn
))
14743 if (! DECL_ARTIFICIAL (fn
))
14744 instantiate_decl (fn
,
14746 /*expl_inst_class_mem_p=*/false);
14747 if (COMPLETE_TYPE_P (instantiation
))
14751 complete
= COMPLETE_TYPE_P (instantiation
);
14755 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
14756 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
14759 = instantiate_decl (instantiation
,
14761 /*expl_inst_class_mem_p=*/false);
14762 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
14766 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
14767 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
14771 /* If INSTANTIATION has been instantiated, then we don't
14772 need to consider it again in the future. */
14780 current_tinst_level
= NULL
;
14782 last_pending_template
= last
;
14784 while (reconsider
);
14786 input_location
= saved_loc
;
14787 in_system_header
= saved_in_system_header
;
14790 /* Substitute ARGVEC into T, which is a list of initializers for
14791 either base class or a non-static data member. The TREE_PURPOSEs
14792 are DECLs, and the TREE_VALUEs are the initializer values. Used by
14793 instantiate_decl. */
14796 tsubst_initializer_list (tree t
, tree argvec
)
14798 tree inits
= NULL_TREE
;
14800 for (; t
; t
= TREE_CHAIN (t
))
14804 tree expanded_bases
= NULL_TREE
;
14805 tree expanded_arguments
= NULL_TREE
;
14808 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
14813 /* Expand the base class expansion type into separate base
14815 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
14816 tf_warning_or_error
,
14818 if (expanded_bases
== error_mark_node
)
14821 /* We'll be building separate TREE_LISTs of arguments for
14823 len
= TREE_VEC_LENGTH (expanded_bases
);
14824 expanded_arguments
= make_tree_vec (len
);
14825 for (i
= 0; i
< len
; i
++)
14826 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
14828 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
14829 expand each argument in the TREE_VALUE of t. */
14830 expr
= make_node (EXPR_PACK_EXPANSION
);
14831 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
14832 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
14834 /* Substitute parameter packs into each argument in the
14836 in_base_initializer
= 1;
14837 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
14839 tree expanded_exprs
;
14841 /* Expand the argument. */
14842 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
14843 expanded_exprs
= tsubst_pack_expansion (expr
, argvec
,
14844 tf_warning_or_error
,
14847 /* Prepend each of the expanded expressions to the
14848 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
14849 for (i
= 0; i
< len
; i
++)
14851 TREE_VEC_ELT (expanded_arguments
, i
) =
14852 tree_cons (NULL_TREE
, TREE_VEC_ELT (expanded_exprs
, i
),
14853 TREE_VEC_ELT (expanded_arguments
, i
));
14856 in_base_initializer
= 0;
14858 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
14859 since we built them backwards. */
14860 for (i
= 0; i
< len
; i
++)
14862 TREE_VEC_ELT (expanded_arguments
, i
) =
14863 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
14867 for (i
= 0; i
< len
; ++i
)
14869 if (expanded_bases
)
14871 decl
= TREE_VEC_ELT (expanded_bases
, i
);
14872 decl
= expand_member_init (decl
);
14873 init
= TREE_VEC_ELT (expanded_arguments
, i
);
14877 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
14878 tf_warning_or_error
, NULL_TREE
);
14880 decl
= expand_member_init (decl
);
14881 if (decl
&& !DECL_P (decl
))
14882 in_base_initializer
= 1;
14884 init
= tsubst_expr (TREE_VALUE (t
), argvec
,
14885 tf_warning_or_error
, NULL_TREE
,
14886 /*integral_constant_expression_p=*/false);
14887 in_base_initializer
= 0;
14892 init
= build_tree_list (decl
, init
);
14893 TREE_CHAIN (init
) = inits
;
14901 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
14904 set_current_access_from_decl (tree decl
)
14906 if (TREE_PRIVATE (decl
))
14907 current_access_specifier
= access_private_node
;
14908 else if (TREE_PROTECTED (decl
))
14909 current_access_specifier
= access_protected_node
;
14911 current_access_specifier
= access_public_node
;
14914 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
14915 is the instantiation (which should have been created with
14916 start_enum) and ARGS are the template arguments to use. */
14919 tsubst_enum (tree tag
, tree newtag
, tree args
)
14923 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
14928 decl
= TREE_VALUE (e
);
14929 /* Note that in a template enum, the TREE_VALUE is the
14930 CONST_DECL, not the corresponding INTEGER_CST. */
14931 value
= tsubst_expr (DECL_INITIAL (decl
),
14932 args
, tf_warning_or_error
, NULL_TREE
,
14933 /*integral_constant_expression_p=*/true);
14935 /* Give this enumeration constant the correct access. */
14936 set_current_access_from_decl (decl
);
14938 /* Actually build the enumerator itself. */
14939 build_enumerator (DECL_NAME (decl
), value
, newtag
);
14942 finish_enum (newtag
);
14943 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
14944 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
14947 /* DECL is a FUNCTION_DECL that is a template specialization. Return
14948 its type -- but without substituting the innermost set of template
14949 arguments. So, innermost set of template parameters will appear in
14953 get_mostly_instantiated_function_type (tree decl
)
14961 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
14962 targs
= DECL_TI_ARGS (decl
);
14963 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
14964 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
14966 /* There should be as many levels of arguments as there are levels
14968 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
14970 fn_type
= TREE_TYPE (tmpl
);
14972 if (parm_depth
== 1)
14973 /* No substitution is necessary. */
14977 int i
, save_access_control
;
14980 /* Replace the innermost level of the TARGS with NULL_TREEs to
14981 let tsubst know not to substitute for those parameters. */
14982 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
14983 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
14984 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
14985 TMPL_ARGS_LEVEL (targs
, i
));
14986 SET_TMPL_ARGS_LEVEL (partial_args
,
14987 TMPL_ARGS_DEPTH (targs
),
14988 make_tree_vec (DECL_NTPARMS (tmpl
)));
14990 /* Disable access control as this function is used only during
14992 save_access_control
= flag_access_control
;
14993 flag_access_control
= 0;
14995 ++processing_template_decl
;
14996 /* Now, do the (partial) substitution to figure out the
14997 appropriate function type. */
14998 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
14999 --processing_template_decl
;
15001 /* Substitute into the template parameters to obtain the real
15002 innermost set of parameters. This step is important if the
15003 innermost set of template parameters contains value
15004 parameters whose types depend on outer template parameters. */
15005 TREE_VEC_LENGTH (partial_args
)--;
15006 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
15008 flag_access_control
= save_access_control
;
15014 /* Return truthvalue if we're processing a template different from
15015 the last one involved in diagnostics. */
15017 problematic_instantiation_changed (void)
15019 return last_template_error_tick
!= tinst_level_tick
;
15022 /* Remember current template involved in diagnostics. */
15024 record_last_problematic_instantiation (void)
15026 last_template_error_tick
= tinst_level_tick
;
15029 struct tinst_level
*
15030 current_instantiation (void)
15032 return current_tinst_level
;
15035 /* [temp.param] Check that template non-type parm TYPE is of an allowable
15036 type. Return zero for ok, nonzero for disallowed. Issue error and
15037 warning messages under control of COMPLAIN. */
15040 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
15042 if (INTEGRAL_TYPE_P (type
))
15044 else if (POINTER_TYPE_P (type
))
15046 else if (TYPE_PTR_TO_MEMBER_P (type
))
15048 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
15050 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
15053 if (complain
& tf_error
)
15054 error ("%q#T is not a valid type for a template constant parameter", type
);
15058 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
15059 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
15062 dependent_type_p_r (tree type
)
15068 A type is dependent if it is:
15070 -- a template parameter. Template template parameters are types
15071 for us (since TYPE_P holds true for them) so we handle
15073 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
15074 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
15076 /* -- a qualified-id with a nested-name-specifier which contains a
15077 class-name that names a dependent type or whose unqualified-id
15078 names a dependent type. */
15079 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15081 /* -- a cv-qualified type where the cv-unqualified type is
15083 type
= TYPE_MAIN_VARIANT (type
);
15084 /* -- a compound type constructed from any dependent type. */
15085 if (TYPE_PTR_TO_MEMBER_P (type
))
15086 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
15087 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
15089 else if (TREE_CODE (type
) == POINTER_TYPE
15090 || TREE_CODE (type
) == REFERENCE_TYPE
)
15091 return dependent_type_p (TREE_TYPE (type
));
15092 else if (TREE_CODE (type
) == FUNCTION_TYPE
15093 || TREE_CODE (type
) == METHOD_TYPE
)
15097 if (dependent_type_p (TREE_TYPE (type
)))
15099 for (arg_type
= TYPE_ARG_TYPES (type
);
15101 arg_type
= TREE_CHAIN (arg_type
))
15102 if (dependent_type_p (TREE_VALUE (arg_type
)))
15106 /* -- an array type constructed from any dependent type or whose
15107 size is specified by a constant expression that is
15108 value-dependent. */
15109 if (TREE_CODE (type
) == ARRAY_TYPE
)
15111 if (TYPE_DOMAIN (type
)
15112 && ((value_dependent_expression_p
15113 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
15114 || (type_dependent_expression_p
15115 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))))
15117 return dependent_type_p (TREE_TYPE (type
));
15120 /* -- a template-id in which either the template name is a template
15122 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15124 /* ... or any of the template arguments is a dependent type or
15125 an expression that is type-dependent or value-dependent. */
15126 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
15127 && (any_dependent_template_arguments_p
15128 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
15131 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
15132 argument of the `typeof' expression is not type-dependent, then
15133 it should already been have resolved. */
15134 if (TREE_CODE (type
) == TYPEOF_TYPE
15135 || TREE_CODE (type
) == DECLTYPE_TYPE
)
15138 /* A template argument pack is dependent if any of its packed
15140 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
15142 tree args
= ARGUMENT_PACK_ARGS (type
);
15143 int i
, len
= TREE_VEC_LENGTH (args
);
15144 for (i
= 0; i
< len
; ++i
)
15145 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
15149 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
15150 be template parameters. */
15151 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
15154 /* The standard does not specifically mention types that are local
15155 to template functions or local classes, but they should be
15156 considered dependent too. For example:
15158 template <int I> void f() {
15163 The size of `E' cannot be known until the value of `I' has been
15164 determined. Therefore, `E' must be considered dependent. */
15165 scope
= TYPE_CONTEXT (type
);
15166 if (scope
&& TYPE_P (scope
))
15167 return dependent_type_p (scope
);
15168 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
15169 return type_dependent_expression_p (scope
);
15171 /* Other types are non-dependent. */
15175 /* Returns TRUE if TYPE is dependent, in the sense of
15176 [temp.dep.type]. */
15179 dependent_type_p (tree type
)
15181 /* If there are no template parameters in scope, then there can't be
15182 any dependent types. */
15183 if (!processing_template_decl
)
15185 /* If we are not processing a template, then nobody should be
15186 providing us with a dependent type. */
15188 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
);
15192 /* If the type is NULL, we have not computed a type for the entity
15193 in question; in that case, the type is dependent. */
15197 /* Erroneous types can be considered non-dependent. */
15198 if (type
== error_mark_node
)
15201 /* If we have not already computed the appropriate value for TYPE,
15203 if (!TYPE_DEPENDENT_P_VALID (type
))
15205 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
15206 TYPE_DEPENDENT_P_VALID (type
) = 1;
15209 return TYPE_DEPENDENT_P (type
);
15212 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
15215 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
15220 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
15222 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
15225 scope
= TREE_OPERAND (expression
, 0);
15226 name
= TREE_OPERAND (expression
, 1);
15230 An id-expression is type-dependent if it contains a
15231 nested-name-specifier that contains a class-name that names a
15233 /* The suggested resolution to Core Issue 2 implies that if the
15234 qualifying type is the current class, then we must peek
15237 && currently_open_class (scope
)
15238 && !criterion (name
))
15240 if (dependent_type_p (scope
))
15246 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
15247 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
15251 value_dependent_expression_p (tree expression
)
15253 if (!processing_template_decl
)
15256 /* A name declared with a dependent type. */
15257 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
15260 switch (TREE_CODE (expression
))
15262 case IDENTIFIER_NODE
:
15263 /* A name that has not been looked up -- must be dependent. */
15266 case TEMPLATE_PARM_INDEX
:
15267 /* A non-type template parm. */
15271 /* A non-type template parm. */
15272 if (DECL_TEMPLATE_PARM_P (expression
))
15277 /* A constant with integral or enumeration type and is initialized
15278 with an expression that is value-dependent. */
15279 if (DECL_INITIAL (expression
)
15280 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
15281 && value_dependent_expression_p (DECL_INITIAL (expression
)))
15285 case DYNAMIC_CAST_EXPR
:
15286 case STATIC_CAST_EXPR
:
15287 case CONST_CAST_EXPR
:
15288 case REINTERPRET_CAST_EXPR
:
15290 /* These expressions are value-dependent if the type to which
15291 the cast occurs is dependent or the expression being casted
15292 is value-dependent. */
15294 tree type
= TREE_TYPE (expression
);
15296 if (dependent_type_p (type
))
15299 /* A functional cast has a list of operands. */
15300 expression
= TREE_OPERAND (expression
, 0);
15303 /* If there are no operands, it must be an expression such
15304 as "int()". This should not happen for aggregate types
15305 because it would form non-constant expressions. */
15306 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
15311 if (TREE_CODE (expression
) == TREE_LIST
)
15312 return any_value_dependent_elements_p (expression
);
15314 return value_dependent_expression_p (expression
);
15319 /* A `sizeof' expression is value-dependent if the operand is
15320 type-dependent or is a pack expansion. */
15321 expression
= TREE_OPERAND (expression
, 0);
15322 if (PACK_EXPANSION_P (expression
))
15324 else if (TYPE_P (expression
))
15325 return dependent_type_p (expression
);
15326 return type_dependent_expression_p (expression
);
15329 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
15331 case COMPONENT_REF
:
15332 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
15333 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
15336 /* A CALL_EXPR may appear in a constant expression if it is a
15337 call to a builtin function, e.g., __builtin_constant_p. All
15338 such calls are value-dependent. */
15341 case NONTYPE_ARGUMENT_PACK
:
15342 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
15343 is value-dependent. */
15345 tree values
= ARGUMENT_PACK_ARGS (expression
);
15346 int i
, len
= TREE_VEC_LENGTH (values
);
15348 for (i
= 0; i
< len
; ++i
)
15349 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
15357 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
15358 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
15359 || (type2
? dependent_type_p (type2
) : false));
15363 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
15364 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
15367 /* A constant expression is value-dependent if any subexpression is
15368 value-dependent. */
15369 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
15371 case tcc_reference
:
15373 return (value_dependent_expression_p
15374 (TREE_OPERAND (expression
, 0)));
15376 case tcc_comparison
:
15378 return ((value_dependent_expression_p
15379 (TREE_OPERAND (expression
, 0)))
15380 || (value_dependent_expression_p
15381 (TREE_OPERAND (expression
, 1))));
15383 case tcc_expression
:
15387 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
15388 /* In some cases, some of the operands may be missing.
15389 (For example, in the case of PREDECREMENT_EXPR, the
15390 amount to increment by may be missing.) That doesn't
15391 make the expression dependent. */
15392 if (TREE_OPERAND (expression
, i
)
15393 && (value_dependent_expression_p
15394 (TREE_OPERAND (expression
, i
))))
15404 /* The expression is not value-dependent. */
15408 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
15409 [temp.dep.expr]. */
15412 type_dependent_expression_p (tree expression
)
15414 if (!processing_template_decl
)
15417 if (expression
== error_mark_node
)
15420 /* An unresolved name is always dependent. */
15421 if (TREE_CODE (expression
) == IDENTIFIER_NODE
15422 || TREE_CODE (expression
) == USING_DECL
)
15425 /* Some expression forms are never type-dependent. */
15426 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
15427 || TREE_CODE (expression
) == SIZEOF_EXPR
15428 || TREE_CODE (expression
) == ALIGNOF_EXPR
15429 || TREE_CODE (expression
) == TRAIT_EXPR
15430 || TREE_CODE (expression
) == TYPEID_EXPR
15431 || TREE_CODE (expression
) == DELETE_EXPR
15432 || TREE_CODE (expression
) == VEC_DELETE_EXPR
15433 || TREE_CODE (expression
) == THROW_EXPR
)
15436 /* The types of these expressions depends only on the type to which
15437 the cast occurs. */
15438 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
15439 || TREE_CODE (expression
) == STATIC_CAST_EXPR
15440 || TREE_CODE (expression
) == CONST_CAST_EXPR
15441 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
15442 || TREE_CODE (expression
) == CAST_EXPR
)
15443 return dependent_type_p (TREE_TYPE (expression
));
15445 /* The types of these expressions depends only on the type created
15446 by the expression. */
15447 if (TREE_CODE (expression
) == NEW_EXPR
15448 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
15450 /* For NEW_EXPR tree nodes created inside a template, either
15451 the object type itself or a TREE_LIST may appear as the
15453 tree type
= TREE_OPERAND (expression
, 1);
15454 if (TREE_CODE (type
) == TREE_LIST
)
15455 /* This is an array type. We need to check array dimensions
15457 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
15458 || value_dependent_expression_p
15459 (TREE_OPERAND (TREE_VALUE (type
), 1));
15461 return dependent_type_p (type
);
15464 if (TREE_CODE (expression
) == SCOPE_REF
15465 && dependent_scope_ref_p (expression
,
15466 type_dependent_expression_p
))
15469 if (TREE_CODE (expression
) == FUNCTION_DECL
15470 && DECL_LANG_SPECIFIC (expression
)
15471 && DECL_TEMPLATE_INFO (expression
)
15472 && (any_dependent_template_arguments_p
15473 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
15476 if (TREE_CODE (expression
) == TEMPLATE_DECL
15477 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
15480 if (TREE_CODE (expression
) == STMT_EXPR
)
15481 expression
= stmt_expr_value_expr (expression
);
15483 if (TREE_TYPE (expression
) == unknown_type_node
)
15485 if (TREE_CODE (expression
) == ADDR_EXPR
)
15486 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
15487 if (TREE_CODE (expression
) == COMPONENT_REF
15488 || TREE_CODE (expression
) == OFFSET_REF
)
15490 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
15492 expression
= TREE_OPERAND (expression
, 1);
15493 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
15496 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15497 if (TREE_CODE (expression
) == SCOPE_REF
)
15500 if (TREE_CODE (expression
) == BASELINK
)
15501 expression
= BASELINK_FUNCTIONS (expression
);
15503 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
15505 if (any_dependent_template_arguments_p
15506 (TREE_OPERAND (expression
, 1)))
15508 expression
= TREE_OPERAND (expression
, 0);
15510 gcc_assert (TREE_CODE (expression
) == OVERLOAD
15511 || TREE_CODE (expression
) == FUNCTION_DECL
);
15515 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
15517 expression
= OVL_NEXT (expression
);
15522 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
15524 return (dependent_type_p (TREE_TYPE (expression
)));
15527 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15528 contains a type-dependent expression. */
15531 any_type_dependent_arguments_p (const_tree args
)
15535 tree arg
= TREE_VALUE (args
);
15537 if (type_dependent_expression_p (arg
))
15539 args
= TREE_CHAIN (args
);
15544 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
15545 expressions) contains any value-dependent expressions. */
15548 any_value_dependent_elements_p (const_tree list
)
15550 for (; list
; list
= TREE_CHAIN (list
))
15551 if (value_dependent_expression_p (TREE_VALUE (list
)))
15557 /* Returns TRUE if the ARG (a template argument) is dependent. */
15560 dependent_template_arg_p (tree arg
)
15562 if (!processing_template_decl
)
15565 if (TREE_CODE (arg
) == TEMPLATE_DECL
15566 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15567 return dependent_template_p (arg
);
15568 else if (ARGUMENT_PACK_P (arg
))
15570 tree args
= ARGUMENT_PACK_ARGS (arg
);
15571 int i
, len
= TREE_VEC_LENGTH (args
);
15572 for (i
= 0; i
< len
; ++i
)
15574 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
15580 else if (TYPE_P (arg
))
15581 return dependent_type_p (arg
);
15583 return (type_dependent_expression_p (arg
)
15584 || value_dependent_expression_p (arg
));
15587 /* Returns true if ARGS (a collection of template arguments) contains
15588 any types that require structural equality testing. */
15591 any_template_arguments_need_structural_equality_p (tree args
)
15598 if (args
== error_mark_node
)
15601 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
15603 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
15604 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
15606 tree arg
= TREE_VEC_ELT (level
, j
);
15607 tree packed_args
= NULL_TREE
;
15610 if (ARGUMENT_PACK_P (arg
))
15612 /* Look inside the argument pack. */
15613 packed_args
= ARGUMENT_PACK_ARGS (arg
);
15614 len
= TREE_VEC_LENGTH (packed_args
);
15617 for (k
= 0; k
< len
; ++k
)
15620 arg
= TREE_VEC_ELT (packed_args
, k
);
15622 if (error_operand_p (arg
))
15624 else if (TREE_CODE (arg
) == TEMPLATE_DECL
15625 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
15627 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
15629 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
15630 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
15639 /* Returns true if ARGS (a collection of template arguments) contains
15640 any dependent arguments. */
15643 any_dependent_template_arguments_p (const_tree args
)
15650 if (args
== error_mark_node
)
15653 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
15655 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
15656 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
15657 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
15664 /* Returns TRUE if the template TMPL is dependent. */
15667 dependent_template_p (tree tmpl
)
15669 if (TREE_CODE (tmpl
) == OVERLOAD
)
15673 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
15675 tmpl
= OVL_CHAIN (tmpl
);
15680 /* Template template parameters are dependent. */
15681 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
15682 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
15684 /* So are names that have not been looked up. */
15685 if (TREE_CODE (tmpl
) == SCOPE_REF
15686 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
15688 /* So are member templates of dependent classes. */
15689 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
15690 return dependent_type_p (DECL_CONTEXT (tmpl
));
15694 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
15697 dependent_template_id_p (tree tmpl
, tree args
)
15699 return (dependent_template_p (tmpl
)
15700 || any_dependent_template_arguments_p (args
));
15703 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
15704 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
15705 no such TYPE can be found. Note that this function peers inside
15706 uninstantiated templates and therefore should be used only in
15707 extremely limited situations. ONLY_CURRENT_P restricts this
15708 peering to the currently open classes hierarchy (which is required
15709 when comparing types). */
15712 resolve_typename_type (tree type
, bool only_current_p
)
15721 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
15723 scope
= TYPE_CONTEXT (type
);
15724 name
= TYPE_IDENTIFIER (type
);
15726 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
15727 it first before we can figure out what NAME refers to. */
15728 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
15729 scope
= resolve_typename_type (scope
, only_current_p
);
15730 /* If we don't know what SCOPE refers to, then we cannot resolve the
15732 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
15734 /* If the SCOPE is a template type parameter, we have no way of
15735 resolving the name. */
15736 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
15738 /* If the SCOPE is not the current instantiation, there's no reason
15739 to look inside it. */
15740 if (only_current_p
&& !currently_open_class (scope
))
15742 /* If SCOPE is a partial instantiation, it will not have a valid
15743 TYPE_FIELDS list, so use the original template. */
15744 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
15745 /* Enter the SCOPE so that name lookup will be resolved as if we
15746 were in the class definition. In particular, SCOPE will no
15747 longer be considered a dependent type. */
15748 pushed_scope
= push_scope (scope
);
15749 /* Look up the declaration. */
15750 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
15752 result
= NULL_TREE
;
15754 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
15755 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
15758 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
15759 && TREE_CODE (decl
) == TYPE_DECL
)
15761 result
= TREE_TYPE (decl
);
15762 if (result
== error_mark_node
)
15763 result
= NULL_TREE
;
15765 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
15766 && DECL_CLASS_TEMPLATE_P (decl
))
15770 /* Obtain the template and the arguments. */
15771 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
15772 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
15773 /* Instantiate the template. */
15774 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
15775 /*entering_scope=*/0,
15776 tf_error
| tf_user
);
15777 if (result
== error_mark_node
)
15778 result
= NULL_TREE
;
15781 /* Leave the SCOPE. */
15783 pop_scope (pushed_scope
);
15785 /* If we failed to resolve it, return the original typename. */
15789 /* If lookup found a typename type, resolve that too. */
15790 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
15792 /* Ill-formed programs can cause infinite recursion here, so we
15793 must catch that. */
15794 TYPENAME_IS_RESOLVING_P (type
) = 1;
15795 result
= resolve_typename_type (result
, only_current_p
);
15796 TYPENAME_IS_RESOLVING_P (type
) = 0;
15799 /* Qualify the resulting type. */
15800 quals
= cp_type_quals (type
);
15802 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
15807 /* EXPR is an expression which is not type-dependent. Return a proxy
15808 for EXPR that can be used to compute the types of larger
15809 expressions containing EXPR. */
15812 build_non_dependent_expr (tree expr
)
15816 /* Preserve null pointer constants so that the type of things like
15817 "p == 0" where "p" is a pointer can be determined. */
15818 if (null_ptr_cst_p (expr
))
15820 /* Preserve OVERLOADs; the functions must be available to resolve
15823 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
15824 inner_expr
= stmt_expr_value_expr (inner_expr
);
15825 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
15826 inner_expr
= TREE_OPERAND (inner_expr
, 0);
15827 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
15828 inner_expr
= TREE_OPERAND (inner_expr
, 1);
15829 if (is_overloaded_fn (inner_expr
)
15830 || TREE_CODE (inner_expr
) == OFFSET_REF
)
15832 /* There is no need to return a proxy for a variable. */
15833 if (TREE_CODE (expr
) == VAR_DECL
)
15835 /* Preserve string constants; conversions from string constants to
15836 "char *" are allowed, even though normally a "const char *"
15837 cannot be used to initialize a "char *". */
15838 if (TREE_CODE (expr
) == STRING_CST
)
15840 /* Preserve arithmetic constants, as an optimization -- there is no
15841 reason to create a new node. */
15842 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
15844 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
15845 There is at least one place where we want to know that a
15846 particular expression is a throw-expression: when checking a ?:
15847 expression, there are special rules if the second or third
15848 argument is a throw-expression. */
15849 if (TREE_CODE (expr
) == THROW_EXPR
)
15852 if (TREE_CODE (expr
) == COND_EXPR
)
15853 return build3 (COND_EXPR
,
15855 TREE_OPERAND (expr
, 0),
15856 (TREE_OPERAND (expr
, 1)
15857 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
15858 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
15859 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
15860 if (TREE_CODE (expr
) == COMPOUND_EXPR
15861 && !COMPOUND_EXPR_OVERLOADED (expr
))
15862 return build2 (COMPOUND_EXPR
,
15864 TREE_OPERAND (expr
, 0),
15865 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
15867 /* If the type is unknown, it can't really be non-dependent */
15868 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
15870 /* Otherwise, build a NON_DEPENDENT_EXPR.
15872 REFERENCE_TYPEs are not stripped for expressions in templates
15873 because doing so would play havoc with mangling. Consider, for
15876 template <typename T> void f<T& g>() { g(); }
15878 In the body of "f", the expression for "g" will have
15879 REFERENCE_TYPE, even though the standard says that it should
15880 not. The reason is that we must preserve the syntactic form of
15881 the expression so that mangling (say) "f<g>" inside the body of
15882 "f" works out correctly. Therefore, the REFERENCE_TYPE is
15884 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
15887 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
15888 Return a new TREE_LIST with the various arguments replaced with
15889 equivalent non-dependent expressions. */
15892 build_non_dependent_args (tree args
)
15897 new_args
= NULL_TREE
;
15898 for (a
= args
; a
; a
= TREE_CHAIN (a
))
15899 new_args
= tree_cons (NULL_TREE
,
15900 build_non_dependent_expr (TREE_VALUE (a
)),
15902 return nreverse (new_args
);
15905 #include "gt-cp-pt.h"