1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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". */
43 /* The type of functions taking a tree, and some additional data, and
45 typedef int (*tree_fn_t
) PROTO((tree
, void*));
47 extern struct obstack permanent_obstack
;
50 extern char *input_filename
;
51 struct pending_inline
*pending_template_expansions
;
53 tree current_template_parms
;
54 HOST_WIDE_INT processing_template_decl
;
56 tree pending_templates
;
57 static tree
*template_tail
= &pending_templates
;
60 static tree
*maybe_template_tail
= &maybe_templates
;
62 int minimal_parse_mode
;
64 int processing_specialization
;
65 int processing_explicit_instantiation
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static tree saved_trees
;
71 #define obstack_chunk_alloc xmalloc
72 #define obstack_chunk_free free
74 #define UNIFY_ALLOW_NONE 0
75 #define UNIFY_ALLOW_MORE_CV_QUAL 1
76 #define UNIFY_ALLOW_LESS_CV_QUAL 2
77 #define UNIFY_ALLOW_DERIVED 4
78 #define UNIFY_ALLOW_INTEGER 8
80 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
81 virtual, or a base class of a virtual
83 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
84 type with the desired type. */
86 static int resolve_overloaded_unification
PROTO((tree
, tree
, tree
, tree
,
87 unification_kind_t
, int));
88 static int try_one_overload
PROTO((tree
, tree
, tree
, tree
, tree
,
89 unification_kind_t
, int));
90 static int unify
PROTO((tree
, tree
, tree
, tree
, int));
91 static void add_pending_template
PROTO((tree
));
92 static int push_tinst_level
PROTO((tree
));
93 static tree classtype_mangled_name
PROTO((tree
));
94 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
95 static tree tsubst_expr_values
PROTO((tree
, tree
));
96 static int list_eq
PROTO((tree
, tree
));
97 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
98 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int));
99 static void tsubst_enum
PROTO((tree
, tree
, tree
));
100 static tree add_to_template_args
PROTO((tree
, tree
));
101 static tree add_outermost_template_args
PROTO((tree
, tree
));
102 static void maybe_adjust_types_for_deduction
PROTO((unification_kind_t
, tree
*,
104 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
105 int, unification_kind_t
, int));
106 static void note_template_header
PROTO((int));
107 static tree maybe_fold_nontype_arg
PROTO((tree
));
108 static tree convert_nontype_argument
PROTO((tree
, tree
));
109 static tree convert_template_argument
PROTO ((tree
, tree
, tree
, int,
111 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
112 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
113 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
114 static int inline_needs_template_parms
PROTO((tree
));
115 static void push_inline_template_parms_recursive
PROTO((tree
, int));
116 static tree retrieve_specialization
PROTO((tree
, tree
));
117 static tree register_specialization
PROTO((tree
, tree
, tree
));
118 static int unregister_specialization
PROTO((tree
, tree
));
119 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
120 static tree build_template_decl
PROTO((tree
, tree
));
121 static int mark_template_parm
PROTO((tree
, void *));
122 static tree tsubst_friend_function
PROTO((tree
, tree
));
123 static tree tsubst_friend_class
PROTO((tree
, tree
));
124 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
125 static int template_decl_level
PROTO((tree
));
126 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
127 static int check_cv_quals_for_unify
PROTO((int, tree
, tree
));
128 static tree tsubst_template_arg_vector
PROTO((tree
, tree
, int));
129 static tree tsubst_template_parms
PROTO((tree
, tree
, int));
130 static void regenerate_decl_from_template
PROTO((tree
, tree
));
131 static tree most_specialized
PROTO((tree
, tree
, tree
));
132 static tree most_specialized_class
PROTO((tree
, tree
));
133 static tree most_general_template
PROTO((tree
));
134 static void set_mangled_name_for_template_decl
PROTO((tree
));
135 static int template_class_depth_real
PROTO((tree
, int));
136 static tree tsubst_aggr_type
PROTO((tree
, tree
, int, tree
, int));
137 static tree tsubst_decl
PROTO((tree
, tree
, tree
, tree
));
138 static tree tsubst_arg_types
PROTO((tree
, tree
, int, tree
));
139 static tree tsubst_function_type
PROTO((tree
, tree
, int, tree
));
140 static void check_specialization_scope
PROTO((void));
141 static tree process_partial_specialization
PROTO((tree
));
142 static void set_current_access_from_decl
PROTO((tree
));
143 static void check_default_tmpl_args
PROTO((tree
, tree
, int, int));
144 static tree tsubst_call_declarator_parms
PROTO((tree
, tree
, int, tree
));
145 static tree get_template_base_recursive
PROTO((tree
, tree
,
146 tree
, tree
, tree
, int));
147 static tree get_template_base
PROTO((tree
, tree
, tree
, tree
));
148 static tree try_class_unification
PROTO((tree
, tree
, tree
, tree
));
149 static int coerce_template_template_parms
PROTO((tree
, tree
, int,
152 /* We use TREE_VECs to hold template arguments. If there is only one
153 level of template arguments, then the TREE_VEC contains the
154 arguments directly. If there is more than one level of template
155 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
156 containing the template arguments for a single level. The first
157 entry in the outer TREE_VEC is the outermost level of template
158 parameters; the last is the innermost.
160 It is incorrect to ever form a template argument vector containing
161 only one level of arguments, but which is a TREE_VEC containing as
162 its only entry the TREE_VEC for that level. */
164 /* Non-zero if the template arguments is actually a vector of vectors,
165 rather than just a vector. */
166 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
168 && TREE_CODE (NODE) == TREE_VEC \
169 && TREE_VEC_LENGTH (NODE) > 0 \
170 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
171 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
173 /* The depth of a template argument vector. When called directly by
174 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
175 template arguments. In fact, we may even see NULL_TREE if there
176 are no template arguments. In both of those cases, there is only
177 one level of template arguments. */
178 #define TMPL_ARGS_DEPTH(NODE) \
179 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
181 /* The LEVELth level of the template ARGS. Note that template
182 parameter levels are indexed from 1, not from 0. */
183 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
184 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
185 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
187 /* Set the LEVELth level of the template ARGS to VAL. This macro does
188 not work with single-level argument vectors. */
189 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
190 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
192 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
193 #define TMPL_ARG(ARGS, LEVEL, IDX) \
194 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
196 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
197 macro does not work with single-level argument vectors. */
198 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
199 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
201 /* Given a single level of template arguments in NODE, return the
202 number of arguments. */
203 #define NUM_TMPL_ARGS(NODE) \
204 ((NODE) == NULL_TREE ? 0 \
205 : (TREE_CODE (NODE) == TREE_VEC \
206 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
208 /* The number of levels of template parameters given by NODE. */
209 #define TMPL_PARMS_DEPTH(NODE) \
210 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
212 /* Do any processing required when DECL (a member template declaration
213 using TEMPLATE_PARAMETERS as its innermost parameter list) is
214 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
215 it is a specialization, in which case the DECL itself is returned. */
218 finish_member_template_decl (decl
)
221 if (decl
== NULL_TREE
|| decl
== void_type_node
)
223 else if (decl
== error_mark_node
)
224 /* By returning NULL_TREE, the parser will just ignore this
225 declaration. We have already issued the error. */
227 else if (TREE_CODE (decl
) == TREE_LIST
)
229 /* Assume that the class is the only declspec. */
230 decl
= TREE_VALUE (decl
);
231 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
232 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
234 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
235 check_member_template (tmpl
);
240 else if (DECL_TEMPLATE_INFO (decl
))
242 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
244 check_member_template (DECL_TI_TEMPLATE (decl
));
245 return DECL_TI_TEMPLATE (decl
);
251 cp_error ("invalid member template declaration `%D'", decl
);
253 return error_mark_node
;
256 /* Returns the template nesting level of the indicated class TYPE.
266 A<T>::B<U> has depth two, while A<T> has depth one.
267 Both A<T>::B<int> and A<int>::B<U> have depth one, if
268 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
271 This function is guaranteed to return 0 if passed NULL_TREE so
272 that, for example, `template_class_depth (current_class_type)' is
276 template_class_depth_real (type
, count_specializations
)
278 int count_specializations
;
283 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
284 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
285 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
287 if (TREE_CODE (type
) != FUNCTION_DECL
)
289 if (CLASSTYPE_TEMPLATE_INFO (type
)
290 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
291 && ((count_specializations
292 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
293 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
298 if (DECL_TEMPLATE_INFO (type
)
299 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
300 && ((count_specializations
301 && DECL_TEMPLATE_SPECIALIZATION (type
))
302 || uses_template_parms (DECL_TI_ARGS (type
))))
310 /* Returns the template nesting level of the indicated class TYPE.
311 Like template_class_depth_real, but instantiations do not count in
315 template_class_depth (type
)
318 return template_class_depth_real (type
, /*count_specializations=*/0);
321 /* Returns 1 if processing DECL as part of do_pending_inlines
322 needs us to push template parms. */
325 inline_needs_template_parms (decl
)
328 if (! DECL_TEMPLATE_INFO (decl
))
331 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
332 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
335 /* Subroutine of maybe_begin_member_template_processing.
336 Push the template parms in PARMS, starting from LEVELS steps into the
337 chain, and ending at the beginning, since template parms are listed
341 push_inline_template_parms_recursive (parmlist
, levels
)
345 tree parms
= TREE_VALUE (parmlist
);
349 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
351 ++processing_template_decl
;
352 current_template_parms
353 = tree_cons (build_int_2 (0, processing_template_decl
),
354 parms
, current_template_parms
);
355 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
358 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
360 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
361 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
363 switch (TREE_CODE (parm
))
372 /* Make a CONST_DECL as is done in process_template_parm.
373 It is ugly that we recreate this here; the original
374 version built in process_template_parm is no longer
376 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
378 SET_DECL_ARTIFICIAL (decl
);
379 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
380 DECL_TEMPLATE_PARM_P (decl
) = 1;
386 my_friendly_abort (0);
391 /* Restore the template parameter context for a member template or
392 a friend template defined in a class definition. */
395 maybe_begin_member_template_processing (decl
)
401 if (! inline_needs_template_parms (decl
))
404 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
406 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
408 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
411 parms
= TREE_CHAIN (parms
);
414 push_inline_template_parms_recursive (parms
, levels
);
417 /* Undo the effects of begin_member_template_processing. */
420 maybe_end_member_template_processing ()
422 if (! processing_template_decl
)
425 while (current_template_parms
426 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
428 --processing_template_decl
;
429 current_template_parms
= TREE_CHAIN (current_template_parms
);
434 /* Returns non-zero iff T is a member template function. We must be
437 template <class T> class C { void f(); }
439 Here, f is a template function, and a member, but not a member
440 template. This function does not concern itself with the origin of
441 T, only its present state. So if we have
443 template <class T> class C { template <class U> void f(U); }
445 then neither C<int>::f<char> nor C<T>::f<double> is considered
446 to be a member template. But, `template <class U> void
447 C<int>::f(U)' is considered a member template. */
450 is_member_template (t
)
453 if (!DECL_FUNCTION_TEMPLATE_P (t
))
454 /* Anything that isn't a function or a template function is
455 certainly not a member template. */
458 /* A local class can't have member templates. */
459 if (hack_decl_function_context (t
))
462 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
463 /* If there are more levels of template parameters than
464 there are template classes surrounding the declaration,
465 then we have a member template. */
466 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
467 template_class_depth (DECL_CLASS_CONTEXT (t
))));
471 /* Returns non-zero iff T is a member template class. See
472 is_member_template for a description of what precisely constitutes
473 a member template. */
476 is_member_template_class (t
)
479 if (!DECL_CLASS_TEMPLATE_P (t
))
480 /* Anything that isn't a class template, is certainly not a member
484 if (!DECL_CLASS_SCOPE_P (t
))
485 /* Anything whose context isn't a class type is surely not a
489 /* If there are more levels of template parameters than there are
490 template classes surrounding the declaration, then we have a
492 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
493 template_class_depth (DECL_CONTEXT (t
)));
497 /* Return a new template argument vector which contains all of ARGS,
498 but has as its innermost set of arguments the EXTRA_ARGS. The
499 resulting vector will be built on a temporary obstack, and so must
500 be explicitly copied to the permanent obstack, if required. */
503 add_to_template_args (args
, extra_args
)
512 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
513 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
515 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
516 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
518 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
519 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
532 add_outermost_template_args (args
, extra_args
)
538 /* If there are more levels of EXTRA_ARGS than there are ARGS,
539 something very fishy is going on. */
540 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
543 /* If *all* the new arguments will be the EXTRA_ARGS, just return
545 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
548 /* For the moment, we make ARGS look like it contains fewer levels. */
549 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
551 new_args
= add_to_template_args (args
, extra_args
);
553 /* Now, we restore ARGS to its full dimensions. */
554 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
559 /* We've got a template header coming up; push to a new level for storing
563 begin_template_parm_list ()
565 /* We use a non-tag-transparent scope here, which causes pushtag to
566 put tags in this scope, rather than in the enclosing class or
567 namespace scope. This is the right thing, since we want
568 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
569 global template class, push_template_decl handles putting the
570 TEMPLATE_DECL into top-level scope. For a nested template class,
573 template <class T> struct S1 {
574 template <class T> struct S2 {};
577 pushtag contains special code to call pushdecl_with_scope on the
578 TEMPLATE_DECL for S2. */
580 declare_pseudo_global_level ();
581 ++processing_template_decl
;
582 ++processing_template_parmlist
;
583 note_template_header (0);
586 /* This routine is called when a specialization is declared. If it is
587 illegal to declare a specialization here, an error is reported. */
590 check_specialization_scope ()
592 tree scope
= current_scope ();
596 An explicit specialization shall be declared in the namespace of
597 which the template is a member, or, for member templates, in the
598 namespace of which the enclosing class or enclosing class
599 template is a member. An explicit specialization of a member
600 function, member class or static data member of a class template
601 shall be declared in the namespace of which the class template
603 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
604 cp_error ("explicit specialization in non-namespace scope `%D'",
609 In an explicit specialization declaration for a member of a class
610 template or a member template that appears in namespace scope,
611 the member template and some of its enclosing class templates may
612 remain unspecialized, except that the declaration shall not
613 explicitly specialize a class member template if its enclosing
614 class templates are not explicitly specialized as well. */
615 if (current_template_parms
)
616 cp_error ("enclosing class templates are not explicitly specialized");
619 /* We've just seen template <>. */
622 begin_specialization ()
624 note_template_header (1);
625 check_specialization_scope ();
628 /* Called at then end of processing a declaration preceeded by
632 end_specialization ()
634 reset_specialization ();
637 /* Any template <>'s that we have seen thus far are not referring to a
638 function specialization. */
641 reset_specialization ()
643 processing_specialization
= 0;
644 template_header_count
= 0;
647 /* We've just seen a template header. If SPECIALIZATION is non-zero,
648 it was of the form template <>. */
651 note_template_header (specialization
)
654 processing_specialization
= specialization
;
655 template_header_count
++;
658 /* We're beginning an explicit instantiation. */
661 begin_explicit_instantiation ()
663 ++processing_explicit_instantiation
;
668 end_explicit_instantiation ()
670 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
671 --processing_explicit_instantiation
;
674 /* The TYPE is being declared. If it is a template type, that means it
675 is a partial specialization. Do appropriate error-checking. */
678 maybe_process_partial_specialization (type
)
681 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
683 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
684 && TYPE_SIZE (type
) == NULL_TREE
)
686 if (current_namespace
687 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
689 cp_pedwarn ("specializing `%#T' in different namespace", type
);
690 cp_pedwarn_at (" from definition of `%#D'",
691 CLASSTYPE_TI_TEMPLATE (type
));
693 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
694 if (processing_template_decl
)
695 push_template_decl (TYPE_MAIN_DECL (type
));
697 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
698 cp_error ("specialization of `%T' after instantiation", type
);
700 else if (processing_specialization
)
701 cp_error ("explicit specialization of non-template `%T'", type
);
704 /* Retrieve the specialization (in the sense of [temp.spec] - a
705 specialization is either an instantiation or an explicit
706 specialization) of TMPL for the given template ARGS. If there is
707 no such specialization, return NULL_TREE. The ARGS are a vector of
708 arguments, or a vector of vectors of arguments, in the case of
709 templates with more than one level of parameters. */
712 retrieve_specialization (tmpl
, args
)
718 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
720 /* There should be as many levels of arguments as there are
721 levels of parameters. */
722 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
723 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
726 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
729 if (comp_template_args (TREE_PURPOSE (s
), args
))
730 return TREE_VALUE (s
);
735 /* Returns non-zero iff DECL is a specialization of TMPL. */
738 is_specialization_of (decl
, tmpl
)
744 if (TREE_CODE (decl
) == FUNCTION_DECL
)
748 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
754 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
756 for (t
= TREE_TYPE (decl
);
758 t
= CLASSTYPE_USE_TEMPLATE (t
)
759 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
760 if (same_type_p (TYPE_MAIN_VARIANT (t
),
761 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
768 /* Register the specialization SPEC as a specialization of TMPL with
769 the indicated ARGS. Returns SPEC, or an equivalent prior
770 declaration, if available. */
773 register_specialization (spec
, tmpl
, args
)
780 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
782 if (TREE_CODE (spec
) == FUNCTION_DECL
783 && uses_template_parms (DECL_TI_ARGS (spec
)))
784 /* This is the FUNCTION_DECL for a partial instantiation. Don't
785 register it; we want the corresponding TEMPLATE_DECL instead.
786 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
787 the more obvious `uses_template_parms (spec)' to avoid problems
788 with default function arguments. In particular, given
791 template <class T> void f(T t1, T t = T())
793 the default argument expression is not substituted for in an
794 instantiation unless and until it is actually needed. */
797 /* There should be as many levels of arguments as there are
798 levels of parameters. */
799 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
800 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
803 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
806 if (comp_template_args (TREE_PURPOSE (s
), args
))
808 tree fn
= TREE_VALUE (s
);
810 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
812 if (DECL_TEMPLATE_INSTANTIATION (fn
))
815 || DECL_EXPLICIT_INSTANTIATION (fn
))
817 cp_error ("specialization of %D after instantiation",
823 /* This situation should occur only if the first
824 specialization is an implicit instantiation,
825 the second is an explicit specialization, and
826 the implicit instantiation has not yet been
827 used. That situation can occur if we have
828 implicitly instantiated a member function and
829 then specialized it later.
831 We can also wind up here if a friend
832 declaration that looked like an instantiation
833 turns out to be a specialization:
835 template <class T> void foo(T);
836 class S { friend void foo<>(int) };
837 template <> void foo(int);
839 We transform the existing DECL in place so that
840 any pointers to it become pointers to the
843 If there was a definition for the template, but
844 not for the specialization, we want this to
845 look as if there is no definition, and vice
847 DECL_INITIAL (fn
) = NULL_TREE
;
848 duplicate_decls (spec
, fn
);
853 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
855 duplicate_decls (spec
, fn
);
861 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
862 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
867 /* Unregister the specialization SPEC as a specialization of TMPL.
868 Returns nonzero if the SPEC was listed as a specialization of
872 unregister_specialization (spec
, tmpl
)
878 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
880 s
= &TREE_CHAIN (*s
))
881 if (TREE_VALUE (*s
) == spec
)
883 *s
= TREE_CHAIN (*s
);
890 /* Print the list of candidate FNS in an error message. */
893 print_candidates (fns
)
898 char* str
= "candidates are:";
900 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
902 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
907 /* Returns the template (one of the functions given by TEMPLATE_ID)
908 which can be specialized to match the indicated DECL with the
909 explicit template args given in TEMPLATE_ID. The DECL may be
910 NULL_TREE if none is available. In that case, the functions in
911 TEMPLATE_ID are non-members.
913 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
914 specialization of a member template.
916 The template args (those explicitly specified and those deduced)
917 are output in a newly created vector *TARGS_OUT.
919 If it is impossible to determine the result, an error message is
920 issued, unless COMPLAIN is 0. In any case, error_mark_node is
921 returned to indicate failure. */
924 determine_specialization (template_id
, decl
, targs_out
,
925 need_member_template
,
930 int need_member_template
;
937 tree candidates
= NULL_TREE
;
938 tree templates
= NULL_TREE
;
940 *targs_out
= NULL_TREE
;
942 if (template_id
== error_mark_node
)
943 return error_mark_node
;
945 fns
= TREE_OPERAND (template_id
, 0);
946 explicit_targs
= TREE_OPERAND (template_id
, 1);
948 if (fns
== error_mark_node
)
949 return error_mark_node
;
951 /* Check for baselinks. */
952 if (TREE_CODE (fns
) == TREE_LIST
)
953 fns
= TREE_VALUE (fns
);
955 for (; fns
; fns
= OVL_NEXT (fns
))
959 fn
= OVL_CURRENT (fns
);
961 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
962 /* DECL might be a specialization of FN. */
964 else if (need_member_template
)
965 /* FN is an ordinary member function, and we need a
966 specialization of a member template. */
968 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
969 /* We can get IDENTIFIER_NODEs here in certain erroneous
972 else if (!DECL_FUNCTION_MEMBER_P (fn
))
973 /* This is just an ordinary non-member function. Nothing can
974 be a specialization of that. */
977 /* When there's no DECL to match, we know we're looking for
984 /* This is an ordinary member function. However, since
985 we're here, we can assume it's enclosing class is a
986 template class. For example,
988 template <typename T> struct S { void f(); };
989 template <> void S<int>::f() {}
991 Here, S<int>::f is a non-template, but S<int> is a
992 template class. If FN has the same type as DECL, we
993 might be in business. */
994 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
995 TREE_TYPE (TREE_TYPE (fn
))))
996 /* The return types differ. */
999 /* Adjust the type of DECL in case FN is a static member. */
1000 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1001 if (DECL_STATIC_FUNCTION_P (fn
)
1002 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1003 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1005 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1008 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1013 if (decl
== NULL_TREE
)
1015 /* Unify against ourselves to make sure that the args we have
1016 make sense and there aren't any undeducible parms. It's OK if
1017 not all the parms are specified; they might be deduced
1019 targs
= get_bindings_overload (tmpl
, DECL_RESULT (tmpl
),
1021 if (uses_template_parms (targs
))
1022 /* We couldn't deduce all the arguments. */
1026 /* See whether this function might be a specialization of this
1028 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1031 /* Wwe cannot deduce template arguments that when used to
1032 specialize TMPL will produce DECL. */
1035 /* Save this template, and the arguments deduced. */
1036 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
1039 if (decl
&& templates
&& TREE_CHAIN (templates
))
1045 It is possible for a specialization with a given function
1046 signature to be instantiated from more than one function
1047 template. In such cases, explicit specification of the
1048 template arguments must be used to uniquely identify the
1049 function template specialization being specialized.
1051 Note that here, there's no suggestion that we're supposed to
1052 determine which of the candidate templates is most
1053 specialized. However, we, also have:
1057 Partial ordering of overloaded function template
1058 declarations is used in the following contexts to select
1059 the function template to which a function template
1060 specialization refers:
1062 -- when an explicit specialization refers to a function
1065 So, we do use the partial ordering rules, at least for now.
1066 This extension can only serve to make illegal programs legal,
1067 so it's safe. And, there is strong anecdotal evidence that
1068 the committee intended the partial ordering rules to apply;
1069 the EDG front-end has that behavior, and John Spicer claims
1070 that the committee simply forgot to delete the wording in
1071 [temp.expl.spec]. */
1072 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1073 if (tmpl
&& tmpl
!= error_mark_node
)
1075 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1076 templates
= scratch_tree_cons (targs
, tmpl
, NULL_TREE
);
1080 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1083 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1085 return error_mark_node
;
1087 else if ((templates
&& TREE_CHAIN (templates
))
1088 || (candidates
&& TREE_CHAIN (candidates
)))
1092 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1094 chainon (candidates
, templates
);
1095 print_candidates (candidates
);
1097 return error_mark_node
;
1100 /* We have one, and exactly one, match. */
1103 /* It was a specialization of an ordinary member function in a
1105 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1106 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1109 /* It was a specialization of a template. */
1110 targs
= DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates
)));
1111 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1113 *targs_out
= copy_node (targs
);
1114 SET_TMPL_ARGS_LEVEL (*targs_out
,
1115 TMPL_ARGS_DEPTH (*targs_out
),
1116 TREE_PURPOSE (templates
));
1119 *targs_out
= TREE_PURPOSE (templates
);
1120 return TREE_VALUE (templates
);
1123 /* Check to see if the function just declared, as indicated in
1124 DECLARATOR, and in DECL, is a specialization of a function
1125 template. We may also discover that the declaration is an explicit
1126 instantiation at this point.
1128 Returns DECL, or an equivalent declaration that should be used
1129 instead if all goes well. Issues an error message if something is
1130 amiss. Returns error_mark_node if the error is not easily
1133 FLAGS is a bitmask consisting of the following flags:
1135 2: The function has a definition.
1136 4: The function is a friend.
1138 The TEMPLATE_COUNT is the number of references to qualifying
1139 template classes that appeared in the name of the function. For
1142 template <class T> struct S { void f(); };
1145 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1146 classes are not counted in the TEMPLATE_COUNT, so that in
1148 template <class T> struct S {};
1149 template <> struct S<int> { void f(); }
1150 template <> void S<int>::f();
1152 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1153 illegal; there should be no template <>.)
1155 If the function is a specialization, it is marked as such via
1156 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1157 is set up correctly, and it is added to the list of specializations
1158 for that template. */
1161 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1167 int have_def
= flags
& 2;
1168 int is_friend
= flags
& 4;
1169 int specialization
= 0;
1170 int explicit_instantiation
= 0;
1171 int member_specialization
= 0;
1173 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1174 tree dname
= DECL_NAME (decl
);
1176 if (processing_specialization
)
1178 /* The last template header was of the form template <>. */
1180 if (template_header_count
> template_count
)
1182 /* There were more template headers than qualifying template
1184 if (template_header_count
- template_count
> 1)
1185 /* There shouldn't be that many template parameter lists.
1186 There can be at most one parameter list for every
1187 qualifying class, plus one for the function itself. */
1188 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1190 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1192 member_specialization
= 1;
1196 else if (template_header_count
== template_count
)
1198 /* The counts are equal. So, this might be a
1199 specialization, but it is not a specialization of a
1200 member template. It might be something like
1202 template <class T> struct S {
1206 void S<int>::f(int i) {} */
1208 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1212 /* This cannot be an explicit specialization. There are not
1213 enough headers for all of the qualifying classes. For
1214 example, we might have:
1217 void S<int>::T<char>::f();
1219 But, we're missing another template <>. */
1220 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1224 else if (processing_explicit_instantiation
)
1226 if (template_header_count
)
1227 cp_error ("template parameter list used in explicit instantiation");
1230 cp_error ("definition provided for explicit instantiation");
1232 explicit_instantiation
= 1;
1234 else if (ctype
!= NULL_TREE
1235 && !TYPE_BEING_DEFINED (ctype
)
1236 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1239 /* This case catches outdated code that looks like this:
1241 template <class T> struct S { void f(); };
1242 void S<int>::f() {} // Missing template <>
1244 We disable this check when the type is being defined to
1245 avoid complaining about default compiler-generated
1246 constructors, destructors, and assignment operators.
1247 Since the type is an instantiation, not a specialization,
1248 these are the only functions that can be defined before
1249 the class is complete. */
1252 template <class T> void S<int>::f() {}
1254 if (template_header_count
)
1256 cp_error ("template parameters specified in specialization");
1262 ("explicit specialization not preceded by `template <>'");
1264 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1266 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1269 /* This could be something like:
1271 template <class T> void f(T);
1272 class S { friend void f<>(int); } */
1276 /* This case handles bogus declarations like template <>
1277 template <class T> void f<int>(); */
1279 cp_error ("template-id `%D' in declaration of primary template",
1285 if (specialization
|| member_specialization
)
1287 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1288 for (; t
; t
= TREE_CHAIN (t
))
1289 if (TREE_PURPOSE (t
))
1292 ("default argument specified in explicit specialization");
1295 if (current_lang_name
== lang_name_c
)
1296 cp_error ("template specialization with C linkage");
1299 if (specialization
|| member_specialization
|| explicit_instantiation
)
1302 tree tmpl
= NULL_TREE
;
1303 tree targs
= NULL_TREE
;
1305 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1306 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1310 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1313 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1318 lookup_template_function (fns
, NULL_TREE
);
1321 if (declarator
== error_mark_node
)
1322 return error_mark_node
;
1324 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1326 /* A friend declaration. We can't do much, because we don't
1327 know what this resolves to, yet. */
1328 my_friendly_assert (is_friend
!= 0, 0);
1329 my_friendly_assert (!explicit_instantiation
, 0);
1330 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1334 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1336 if (!explicit_instantiation
)
1337 /* A specialization in class scope. This is illegal,
1338 but the error will already have been flagged by
1339 check_specialization_scope. */
1340 return error_mark_node
;
1343 /* It's not legal to write an explicit instantiation in
1346 class C { template void f(); }
1348 This case is caught by the parser. However, on
1351 template class C { void f(); };
1353 (which is illegal) we can get here. The error will be
1360 else if (ctype
!= NULL_TREE
1361 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1364 /* Find the list of functions in ctype that have the same
1365 name as the declared function. */
1366 tree name
= TREE_OPERAND (declarator
, 0);
1367 tree fns
= NULL_TREE
;
1370 if (name
== constructor_name (ctype
)
1371 || name
== constructor_name_full (ctype
))
1373 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1375 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1376 : !TYPE_HAS_DESTRUCTOR (ctype
))
1378 /* From [temp.expl.spec]:
1380 If such an explicit specialization for the member
1381 of a class template names an implicitly-declared
1382 special member function (clause _special_), the
1383 program is ill-formed.
1385 Similar language is found in [temp.explicit]. */
1386 cp_error ("specialization of implicitly-declared special member function");
1387 return error_mark_node
;
1390 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1393 if (!IDENTIFIER_TYPENAME_P (name
))
1395 idx
= lookup_fnfields_1 (ctype
, name
);
1397 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1403 /* For a type-conversion operator, we cannot do a
1404 name-based lookup. We might be looking for `operator
1405 int' which will be a specialization of `operator T'.
1406 So, we find *all* the conversion operators, and then
1407 select from them. */
1410 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1412 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1414 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1416 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1417 /* There are no more conversion functions. */
1420 /* Glue all these conversion functions together
1421 with those we already have. */
1422 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1423 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1427 if (fns
== NULL_TREE
)
1429 cp_error ("no member function `%s' declared in `%T'",
1430 IDENTIFIER_POINTER (name
),
1432 return error_mark_node
;
1435 TREE_OPERAND (declarator
, 0) = fns
;
1438 /* Figure out what exactly is being specialized at this point.
1439 Note that for an explicit instantiation, even one for a
1440 member function, we cannot tell apriori whether the
1441 instantiation is for a member template, or just a member
1442 function of a template class. Even if a member template is
1443 being instantiated, the member template arguments may be
1444 elided if they can be deduced from the rest of the
1446 tmpl
= determine_specialization (declarator
, decl
,
1448 member_specialization
,
1451 if (!tmpl
|| tmpl
== error_mark_node
)
1452 /* We couldn't figure out what this declaration was
1454 return error_mark_node
;
1457 gen_tmpl
= most_general_template (tmpl
);
1459 if (explicit_instantiation
)
1461 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1462 is done by do_decl_instantiation later. */
1463 decl
= instantiate_template (tmpl
, innermost_args (targs
));
1467 /* If we though that the DECL was a member function, but it
1468 turns out to be specializing a static member function,
1469 make DECL a static member function as well. */
1470 if (DECL_STATIC_FUNCTION_P (tmpl
)
1471 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1473 revert_static_member_fn (&decl
, 0, 0);
1474 last_function_parms
= TREE_CHAIN (last_function_parms
);
1477 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1478 DECL_TEMPLATE_INFO (decl
)
1479 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1481 /* Mangle the function name appropriately. Note that we do
1482 not mangle specializations of non-template member
1483 functions of template classes, e.g. with
1485 template <class T> struct S { void f(); }
1487 and given the specialization
1489 template <> void S<int>::f() {}
1491 we do not mangle S<int>::f() here. That's because it's
1492 just an ordinary member function and doesn't need special
1493 treatment. We do this here so that the ordinary,
1494 non-template, name-mangling algorith will not be used
1496 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1497 && name_mangling_version
>= 1)
1498 set_mangled_name_for_template_decl (decl
);
1500 if (is_friend
&& !have_def
)
1501 /* This is not really a declaration of a specialization.
1502 It's just the name of an instantiation. But, it's not
1503 a request for an instantiation, either. */
1504 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1506 /* Register this specialization so that we can find it
1508 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1515 /* TYPE is being declared. Verify that the use of template headers
1516 and such is reasonable. Issue error messages if not. */
1519 maybe_check_template_type (type
)
1522 if (template_header_count
)
1524 /* We are in the scope of some `template <...>' header. */
1527 = template_class_depth_real (TYPE_CONTEXT (type
),
1528 /*count_specializations=*/1);
1530 if (template_header_count
<= context_depth
)
1531 /* This is OK; the template headers are for the context. We
1532 are actually too lenient here; like
1533 check_explicit_specialization we should consider the number
1534 of template types included in the actual declaration. For
1537 template <class T> struct S {
1538 template <class U> template <class V>
1544 template <class T> struct S {
1545 template <class U> struct I;
1548 template <class T> template <class U.
1553 else if (template_header_count
> context_depth
+ 1)
1554 /* There are two many template parameter lists. */
1555 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1559 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1560 parameters. These are represented in the same format used for
1561 DECL_TEMPLATE_PARMS. */
1563 int comp_template_parms (parms1
, parms2
)
1570 if (parms1
== parms2
)
1573 for (p1
= parms1
, p2
= parms2
;
1574 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1575 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1577 tree t1
= TREE_VALUE (p1
);
1578 tree t2
= TREE_VALUE (p2
);
1581 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1582 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1584 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1587 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1589 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1590 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1592 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1595 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1597 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1602 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1603 /* One set of parameters has more parameters lists than the
1610 /* Complain if DECL shadows a template parameter.
1612 [temp.local]: A template-parameter shall not be redeclared within its
1613 scope (including nested scopes). */
1616 check_template_shadow (decl
)
1619 tree olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1621 if (current_template_parms
&& olddecl
)
1623 /* We check for decl != olddecl to avoid bogus errors for using a
1624 name inside a class. We check TPFI to avoid duplicate errors for
1625 inline member templates. */
1626 if (decl
!= olddecl
&& DECL_TEMPLATE_PARM_P (olddecl
)
1627 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1629 cp_error_at ("declaration of `%#D'", decl
);
1630 cp_error_at (" shadows template parm `%#D'", olddecl
);
1635 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1636 ORIG_LEVEL, DECL, and TYPE. */
1639 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1646 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1647 TEMPLATE_PARM_IDX (t
) = index
;
1648 TEMPLATE_PARM_LEVEL (t
) = level
;
1649 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1650 TEMPLATE_PARM_DECL (t
) = decl
;
1651 TREE_TYPE (t
) = type
;
1656 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1657 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1658 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1659 new one is created. */
1662 reduce_template_parm_level (index
, type
, levels
)
1667 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1668 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1669 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1672 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1673 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1676 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1677 TEMPLATE_PARM_LEVEL (index
) - levels
,
1678 TEMPLATE_PARM_ORIG_LEVEL (index
),
1680 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1682 /* Template template parameters need this. */
1683 DECL_TEMPLATE_PARMS (decl
)
1684 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1687 return TEMPLATE_PARM_DESCENDANTS (index
);
1690 /* Process information from new template parameter NEXT and append it to the
1691 LIST being built. */
1694 process_template_parm (list
, next
)
1703 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1704 defval
= TREE_PURPOSE (parm
);
1705 parm
= TREE_VALUE (parm
);
1706 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1710 tree p
= TREE_VALUE (tree_last (list
));
1712 if (TREE_CODE (p
) == TYPE_DECL
)
1713 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1714 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1715 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1717 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1725 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1726 /* is a const-param */
1727 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1728 PARM
, 0, NULL_TREE
);
1732 The top-level cv-qualifiers on the template-parameter are
1733 ignored when determining its type. */
1734 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1736 /* A template parameter is not modifiable. */
1737 TREE_READONLY (parm
) = 1;
1738 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1739 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1740 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1742 cp_error ("`%#T' is not a valid type for a template constant parameter",
1744 if (DECL_NAME (parm
) == NULL_TREE
)
1745 error (" a template type parameter must begin with `class' or `typename'");
1746 TREE_TYPE (parm
) = void_type_node
;
1749 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1750 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1751 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1753 if (TREE_PERMANENT (parm
) == 0)
1755 parm
= copy_node (parm
);
1756 TREE_PERMANENT (parm
) = 1;
1758 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1759 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1760 = build_template_parm_index (idx
, processing_template_decl
,
1761 processing_template_decl
,
1762 decl
, TREE_TYPE (parm
));
1767 parm
= TREE_VALUE (parm
);
1769 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1771 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1772 /* This is for distinguishing between real templates and template
1773 template parameters */
1774 TREE_TYPE (parm
) = t
;
1775 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1780 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1781 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1782 decl
= build_decl (TYPE_DECL
, parm
, t
);
1785 TYPE_NAME (t
) = decl
;
1786 TYPE_STUB_DECL (t
) = decl
;
1788 TEMPLATE_TYPE_PARM_INDEX (t
)
1789 = build_template_parm_index (idx
, processing_template_decl
,
1790 processing_template_decl
,
1791 decl
, TREE_TYPE (parm
));
1793 SET_DECL_ARTIFICIAL (decl
);
1794 DECL_TEMPLATE_PARM_P (decl
) = 1;
1796 parm
= build_tree_list (defval
, parm
);
1797 return chainon (list
, parm
);
1800 /* The end of a template parameter list has been reached. Process the
1801 tree list into a parameter vector, converting each parameter into a more
1802 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1806 end_template_parm_list (parms
)
1811 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1813 current_template_parms
1814 = tree_cons (build_int_2 (0, processing_template_decl
),
1815 saved_parmlist
, current_template_parms
);
1817 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1818 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1820 --processing_template_parmlist
;
1822 return saved_parmlist
;
1825 /* end_template_decl is called after a template declaration is seen. */
1828 end_template_decl ()
1830 reset_specialization ();
1832 if (! processing_template_decl
)
1835 /* This matches the pushlevel in begin_template_parm_list. */
1838 --processing_template_decl
;
1839 current_template_parms
= TREE_CHAIN (current_template_parms
);
1840 (void) get_pending_sizes (); /* Why? */
1843 /* Given a template argument vector containing the template PARMS.
1844 The innermost PARMS are given first. */
1847 current_template_args ()
1850 tree args
= NULL_TREE
;
1851 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1854 /* If there is only one level of template parameters, we do not
1855 create a TREE_VEC of TREE_VECs. Instead, we return a single
1856 TREE_VEC containing the arguments. */
1858 args
= make_tree_vec (length
);
1860 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1862 tree a
= copy_node (TREE_VALUE (header
));
1865 TREE_TYPE (a
) = NULL_TREE
;
1866 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1868 tree t
= TREE_VEC_ELT (a
, i
);
1870 /* T will be a list if we are called from within a
1871 begin/end_template_parm_list pair, but a vector directly
1872 if within a begin/end_member_template_processing pair. */
1873 if (TREE_CODE (t
) == TREE_LIST
)
1877 if (TREE_CODE (t
) == TYPE_DECL
1878 || TREE_CODE (t
) == TEMPLATE_DECL
)
1881 t
= DECL_INITIAL (t
);
1882 TREE_VEC_ELT (a
, i
) = t
;
1887 TREE_VEC_ELT (args
, --l
) = a
;
1895 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1896 template PARMS. Used by push_template_decl below. */
1899 build_template_decl (decl
, parms
)
1903 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1904 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1905 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1906 if (DECL_LANG_SPECIFIC (decl
))
1908 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1909 DECL_STATIC_FUNCTION_P (tmpl
) =
1910 DECL_STATIC_FUNCTION_P (decl
);
1916 struct template_parm_data
1918 /* The level of the template parameters we are currently
1922 /* The index of the specialization argument we are currently
1926 /* An array whose size is the number of template parameters. The
1927 elements are non-zero if the parameter has been used in any one
1928 of the arguments processed so far. */
1931 /* An array whose size is the number of template arguments. The
1932 elements are non-zero if the argument makes use of template
1933 parameters of this level. */
1934 int* arg_uses_template_parms
;
1937 /* Subroutine of push_template_decl used to see if each template
1938 parameter in a partial specialization is used in the explicit
1939 argument list. If T is of the LEVEL given in DATA (which is
1940 treated as a template_parm_data*), then DATA->PARMS is marked
1944 mark_template_parm (t
, data
)
1950 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1952 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1954 level
= TEMPLATE_PARM_LEVEL (t
);
1955 idx
= TEMPLATE_PARM_IDX (t
);
1959 level
= TEMPLATE_TYPE_LEVEL (t
);
1960 idx
= TEMPLATE_TYPE_IDX (t
);
1963 if (level
== tpd
->level
)
1965 tpd
->parms
[idx
] = 1;
1966 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
1969 /* Return zero so that for_each_template_parm will continue the
1970 traversal of the tree; we want to mark *every* template parm. */
1974 /* Process the partial specialization DECL. */
1977 process_partial_specialization (decl
)
1980 tree type
= TREE_TYPE (decl
);
1981 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1982 tree specargs
= CLASSTYPE_TI_ARGS (type
);
1983 tree inner_args
= innermost_args (specargs
);
1984 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1985 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
1986 int nargs
= TREE_VEC_LENGTH (inner_args
);
1987 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
1989 int did_error_intro
= 0;
1990 struct template_parm_data tpd
;
1991 struct template_parm_data tpd2
;
1993 /* We check that each of the template parameters given in the
1994 partial specialization is used in the argument list to the
1995 specialization. For example:
1997 template <class T> struct S;
1998 template <class T> struct S<T*>;
2000 The second declaration is OK because `T*' uses the template
2001 parameter T, whereas
2003 template <class T> struct S<int>;
2005 is no good. Even trickier is:
2016 The S2<T> declaration is actually illegal; it is a
2017 full-specialization. Of course,
2020 struct S2<T (*)(U)>;
2022 or some such would have been OK. */
2023 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2024 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2025 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
2027 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2028 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
2029 for (i
= 0; i
< nargs
; ++i
)
2031 tpd
.current_arg
= i
;
2032 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2033 &mark_template_parm
,
2036 for (i
= 0; i
< ntparms
; ++i
)
2037 if (tpd
.parms
[i
] == 0)
2039 /* One of the template parms was not used in the
2041 if (!did_error_intro
)
2043 cp_error ("template parameters not used in partial specialization:");
2044 did_error_intro
= 1;
2048 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2051 /* [temp.class.spec]
2053 The argument list of the specialization shall not be identical to
2054 the implicit argument list of the primary template. */
2055 if (comp_template_args (inner_args
,
2056 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2058 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2060 /* [temp.class.spec]
2062 A partially specialized non-type argument expression shall not
2063 involve template parameters of the partial specialization except
2064 when the argument expression is a simple identifier.
2066 The type of a template parameter corresponding to a specialized
2067 non-type argument shall not be dependent on a parameter of the
2069 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2071 for (i
= 0; i
< nargs
; ++i
)
2073 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2074 if (/* These first two lines are the `non-type' bit. */
2075 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2076 && TREE_CODE (arg
) != TEMPLATE_DECL
2077 /* This next line is the `argument expression is not just a
2078 simple identifier' condition and also the `specialized
2079 non-type argument' bit. */
2080 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2082 if (tpd
.arg_uses_template_parms
[i
])
2083 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2086 /* Look at the corresponding template parameter,
2087 marking which template parameters its type depends
2090 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2095 /* We haven't yet initialized TPD2. Do so now. */
2096 tpd2
.arg_uses_template_parms
2097 = (int*) alloca (sizeof (int) * nargs
);
2098 /* The number of parameters here is the number in the
2099 main template, which, as checked in the assertion
2101 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2103 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2106 /* Mark the template parameters. But this time, we're
2107 looking for the template parameters of the main
2108 template, not in the specialization. */
2109 tpd2
.current_arg
= i
;
2110 tpd2
.arg_uses_template_parms
[i
] = 0;
2111 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
2112 for_each_template_parm (type
,
2113 &mark_template_parm
,
2116 if (tpd2
.arg_uses_template_parms
[i
])
2118 /* The type depended on some template parameters.
2119 If they are fully specialized in the
2120 specialization, that's OK. */
2122 for (j
= 0; j
< nargs
; ++j
)
2123 if (tpd2
.parms
[j
] != 0
2124 && tpd
.arg_uses_template_parms
[j
])
2126 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2136 if (retrieve_specialization (maintmpl
, specargs
))
2137 /* We've already got this specialization. */
2140 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2141 = perm_tree_cons (inner_args
, inner_parms
,
2142 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2143 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2147 /* Check that a template declaration's use of default arguments is not
2148 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2149 non-zero if DECL is the thing declared by a primary template.
2150 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2153 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2160 int last_level_to_check
;
2164 A default template-argument shall not be specified in a
2165 function template declaration or a function template definition, nor
2166 in the template-parameter-list of the definition of a member of a
2169 if (current_class_type
2170 && !TYPE_BEING_DEFINED (current_class_type
)
2171 && DECL_REAL_CONTEXT (decl
) == current_class_type
2172 && DECL_DEFINED_IN_CLASS_P (decl
))
2173 /* We already checked these parameters when the template was
2174 declared, so there's no need to do it again now. This is an
2175 inline member function definition. */
2178 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2179 /* For an ordinary class template, default template arguments are
2180 allowed at the innermost level, e.g.:
2181 template <class T = int>
2183 but, in a partial specialization, they're not allowed even
2184 there, as we have in [temp.class.spec]:
2186 The template parameter list of a specialization shall not
2187 contain default template argument values.
2189 So, for a partial specialization, or for a function template,
2190 we look at all of them. */
2193 /* But, for a primary class template that is not a partial
2194 specialization we look at all template parameters except the
2196 parms
= TREE_CHAIN (parms
);
2198 /* Figure out what error message to issue. */
2199 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2200 msg
= "default argument for template parameter in function template `%D'";
2201 else if (is_partial
)
2202 msg
= "default argument in partial specialization `%D'";
2204 msg
= "default argument for template parameter for class enclosing `%D'";
2206 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2207 /* If we're inside a class definition, there's no need to
2208 examine the parameters to the class itself. On the one
2209 hand, they will be checked when the class is defined, and,
2210 on the other, default arguments are legal in things like:
2211 template <class T = double>
2212 struct S { template <class U> void f(U); };
2213 Here the default argument for `S' has no bearing on the
2214 declaration of `f'. */
2215 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2217 /* Check everything. */
2218 last_level_to_check
= 0;
2220 for (; parms
&& TMPL_PARMS_DEPTH (parms
) >= last_level_to_check
;
2221 parms
= TREE_CHAIN (parms
))
2223 tree inner_parms
= TREE_VALUE (parms
);
2226 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2227 for (i
= 0; i
< ntparms
; ++i
)
2228 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2232 cp_error (msg
, decl
);
2236 /* Clear out the default argument so that we are not
2238 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2241 /* At this point, if we're still interested in issuing messages,
2242 they must apply to classes surrounding the object declared. */
2244 msg
= "default argument for template parameter for class enclosing `%D'";
2248 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2249 parameters given by current_template_args, or reuses a
2250 previously existing one, if appropriate. Returns the DECL, or an
2251 equivalent one, if it is replaced via a call to duplicate_decls.
2253 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2256 push_template_decl_real (decl
, is_friend
)
2267 /* See if this is a partial specialization. */
2268 is_partial
= (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
2269 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2270 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2272 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2275 /* For a friend, we want the context of the friend function, not
2276 the type of which it is a friend. */
2277 ctx
= DECL_CONTEXT (decl
);
2278 else if (DECL_REAL_CONTEXT (decl
)
2279 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2280 /* In the case of a virtual function, we want the class in which
2282 ctx
= DECL_REAL_CONTEXT (decl
);
2284 /* Otherwise, if we're currently definining some class, the DECL
2285 is assumed to be a member of the class. */
2286 ctx
= current_class_type
;
2288 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2291 if (!DECL_CONTEXT (decl
))
2292 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2294 /* For determining whether this is a primary template or not, we're really
2295 interested in the lexical context, not the true context. */
2297 info
= current_class_type
;
2301 /* See if this is a primary template. */
2302 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
2304 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2305 this next line works even when we are at global scope. */
2306 else if (processing_template_decl
> template_class_depth (info
))
2313 if (current_lang_name
== lang_name_c
)
2314 cp_error ("template with C linkage");
2315 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
2316 cp_error ("template class without a name");
2317 if (TREE_CODE (decl
) == TYPE_DECL
2318 && TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2319 cp_error ("template declaration of `%#T'", TREE_TYPE (decl
));
2322 /* Check to see that the rules regarding the use of default
2323 arguments are not being violated. */
2324 check_default_tmpl_args (decl
, current_template_parms
,
2325 primary
, is_partial
);
2328 return process_partial_specialization (decl
);
2330 args
= current_template_args ();
2333 || TREE_CODE (ctx
) == FUNCTION_DECL
2334 || TYPE_BEING_DEFINED (ctx
)
2335 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2337 if (DECL_LANG_SPECIFIC (decl
)
2338 && DECL_TEMPLATE_INFO (decl
)
2339 && DECL_TI_TEMPLATE (decl
))
2340 tmpl
= DECL_TI_TEMPLATE (decl
);
2343 tmpl
= build_template_decl (decl
, current_template_parms
);
2345 if (DECL_LANG_SPECIFIC (decl
)
2346 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2348 /* A specialization of a member template of a template
2350 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2351 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2352 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2358 tree a
, t
, current
, parms
;
2361 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2362 cp_error ("must specialize `%#T' before defining member `%#D'",
2364 if (TREE_CODE (decl
) == TYPE_DECL
)
2366 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2367 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2368 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2369 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2370 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2373 cp_error ("`%D' does not declare a template type", decl
);
2377 else if (! DECL_TEMPLATE_INFO (decl
))
2379 cp_error ("template definition of non-template `%#D'", decl
);
2383 tmpl
= DECL_TI_TEMPLATE (decl
);
2385 if (is_member_template (tmpl
)
2386 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2387 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2388 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2392 /* The declaration is a specialization of a member
2393 template, declared outside the class. Therefore, the
2394 innermost template arguments will be NULL, so we
2395 replace them with the arguments determined by the
2396 earlier call to check_explicit_specialization. */
2397 args
= DECL_TI_ARGS (decl
);
2400 = build_template_decl (decl
, current_template_parms
);
2401 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2402 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2403 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2404 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2405 DECL_TEMPLATE_INFO (new_tmpl
) =
2406 perm_tree_cons (tmpl
, args
, NULL_TREE
);
2408 register_specialization (new_tmpl
, tmpl
, args
);
2412 /* Make sure the template headers we got make sense. */
2414 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2415 i
= TMPL_PARMS_DEPTH (parms
);
2416 if (TMPL_ARGS_DEPTH (args
) != i
)
2418 cp_error ("expected %d levels of template parms for `%#D', got %d",
2419 i
, decl
, TMPL_ARGS_DEPTH (args
));
2422 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2424 a
= TMPL_ARGS_LEVEL (args
, i
);
2425 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2427 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2429 if (current
== decl
)
2430 cp_error ("got %d template parameters for `%#D'",
2431 TREE_VEC_LENGTH (a
), decl
);
2433 cp_error ("got %d template parameters for `%#T'",
2434 TREE_VEC_LENGTH (a
), current
);
2435 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2438 /* Perhaps we should also check that the parms are used in the
2439 appropriate qualifying scopes in the declarator? */
2441 if (current
== decl
)
2444 current
= TYPE_CONTEXT (current
);
2448 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2449 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2451 /* Push template declarations for global functions and types. Note
2452 that we do not try to push a global template friend declared in a
2453 template class; such a thing may well depend on the template
2454 parameters of the class. */
2456 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2457 tmpl
= pushdecl_namespace_level (tmpl
);
2460 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2462 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
2464 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2466 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2467 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2468 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2469 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2471 else if (! DECL_LANG_SPECIFIC (decl
))
2472 cp_error ("template declaration of `%#D'", decl
);
2474 DECL_TEMPLATE_INFO (decl
) = info
;
2476 return DECL_TEMPLATE_RESULT (tmpl
);
2480 push_template_decl (decl
)
2483 return push_template_decl_real (decl
, 0);
2486 /* Called when a class template TYPE is redeclared with the indicated
2487 template PARMS, e.g.:
2489 template <class T> struct S;
2490 template <class T> struct S {}; */
2493 redeclare_class_template (type
, parms
)
2497 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2501 if (!PRIMARY_TEMPLATE_P (tmpl
))
2502 /* The type is nested in some template class. Nothing to worry
2503 about here; there are no new template parameters for the nested
2507 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2508 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2510 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2512 cp_error_at ("previous declaration `%D'", tmpl
);
2513 cp_error ("used %d template parameter%s instead of %d",
2514 TREE_VEC_LENGTH (tmpl_parms
),
2515 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2516 TREE_VEC_LENGTH (parms
));
2520 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2522 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2523 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2524 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2525 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2527 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2529 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2530 cp_error ("redeclared here as `%#D'", parm
);
2534 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2536 /* We have in [temp.param]:
2538 A template-parameter may not be given default arguments
2539 by two different declarations in the same scope. */
2540 cp_error ("redefinition of default argument for `%#D'", parm
);
2541 cp_error_at (" original definition appeared here", tmpl_parm
);
2545 if (parm_default
!= NULL_TREE
)
2546 /* Update the previous template parameters (which are the ones
2547 that will really count) with the new default value. */
2548 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2552 /* Attempt to convert the non-type template parameter EXPR to the
2553 indicated TYPE. If the conversion is successful, return the
2554 converted value. If the conversion is unsuccesful, return
2555 NULL_TREE if we issued an error message, or error_mark_node if we
2556 did not. We issue error messages for out-and-out bad template
2557 parameters, but not simply because the conversion failed, since we
2558 might be just trying to do argument deduction. By the time this
2559 function is called, neither TYPE nor EXPR may make use of template
2563 convert_nontype_argument (type
, expr
)
2567 tree expr_type
= TREE_TYPE (expr
);
2569 /* A template-argument for a non-type, non-template
2570 template-parameter shall be one of:
2572 --an integral constant-expression of integral or enumeration
2575 --the name of a non-type template-parameter; or
2577 --the name of an object or function with external linkage,
2578 including function templates and function template-ids but
2579 excluding non-static class members, expressed as id-expression;
2582 --the address of an object or function with external linkage,
2583 including function templates and function template-ids but
2584 excluding non-static class members, expressed as & id-expression
2585 where the & is optional if the name refers to a function or
2588 --a pointer to member expressed as described in _expr.unary.op_. */
2590 /* An integral constant-expression can include const variables
2592 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
2593 expr
= decl_constant_value (expr
);
2595 if (is_overloaded_fn (expr
))
2596 /* OK for now. We'll check that it has external linkage later.
2597 Check this first since if expr_type is the unknown_type_node
2598 we would otherwise complain below. */
2600 else if (TYPE_PTR_P (expr_type
)
2601 || TREE_CODE (expr_type
) == ARRAY_TYPE
2602 || TREE_CODE (type
) == REFERENCE_TYPE
2603 /* If expr is the address of an overloaded function, we
2604 will get the unknown_type_node at this point. */
2605 || expr_type
== unknown_type_node
)
2611 if (TREE_CODE (type
) == REFERENCE_TYPE
2612 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2616 if (TREE_CODE (e
) != ADDR_EXPR
)
2619 cp_error ("`%E' is not a valid template argument", expr
);
2620 error ("it must be %s%s with external linkage",
2621 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
2622 ? "a pointer to " : "",
2623 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
2624 ? "a function" : "an object");
2628 referent
= TREE_OPERAND (e
, 0);
2629 STRIP_NOPS (referent
);
2632 if (TREE_CODE (referent
) == STRING_CST
)
2634 cp_error ("string literal %E is not a valid template argument",
2636 error ("because it is the address of an object with static linkage");
2640 if (is_overloaded_fn (referent
))
2641 /* We'll check that it has external linkage later. */
2643 else if (TREE_CODE (referent
) != VAR_DECL
)
2645 else if (!TREE_PUBLIC (referent
))
2647 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2648 return error_mark_node
;
2651 else if (INTEGRAL_TYPE_P (expr_type
)
2652 || TYPE_PTRMEM_P (expr_type
)
2653 || TYPE_PTRMEMFUNC_P (expr_type
)
2654 /* The next two are g++ extensions. */
2655 || TREE_CODE (expr_type
) == REAL_TYPE
2656 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2658 if (! TREE_CONSTANT (expr
))
2661 cp_error ("non-constant `%E' cannot be used as template argument",
2668 cp_error ("object `%E' cannot be used as template argument", expr
);
2672 switch (TREE_CODE (type
))
2677 /* For a non-type template-parameter of integral or enumeration
2678 type, integral promotions (_conv.prom_) and integral
2679 conversions (_conv.integral_) are applied. */
2680 if (!INTEGRAL_TYPE_P (expr_type
))
2681 return error_mark_node
;
2683 /* It's safe to call digest_init in this case; we know we're
2684 just converting one integral constant expression to another. */
2685 expr
= digest_init (type
, expr
, (tree
*) 0);
2687 if (TREE_CODE (expr
) != INTEGER_CST
)
2688 /* Curiously, some TREE_CONSTANT integral expressions do not
2689 simplify to integer constants. For example, `3 % 0',
2690 remains a TRUNC_MOD_EXPR. */
2697 /* These are g++ extensions. */
2698 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2699 return error_mark_node
;
2701 expr
= digest_init (type
, expr
, (tree
*) 0);
2703 if (TREE_CODE (expr
) != REAL_CST
)
2710 tree type_pointed_to
= TREE_TYPE (type
);
2712 if (TYPE_PTRMEM_P (type
))
2713 /* For a non-type template-parameter of type pointer to data
2714 member, qualification conversions (_conv.qual_) are
2716 return perform_qualification_conversions (type
, expr
);
2717 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2719 /* For a non-type template-parameter of type pointer to
2720 function, only the function-to-pointer conversion
2721 (_conv.func_) is applied. If the template-argument
2722 represents a set of overloaded functions (or a pointer to
2723 such), the matching function is selected from the set
2728 if (TREE_CODE (expr
) == ADDR_EXPR
)
2729 fns
= TREE_OPERAND (expr
, 0);
2733 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2735 if (fn
== error_mark_node
)
2736 return error_mark_node
;
2738 if (!TREE_PUBLIC (fn
))
2740 if (really_overloaded_fn (fns
))
2741 return error_mark_node
;
2746 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2748 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2754 /* For a non-type template-parameter of type pointer to
2755 object, qualification conversions (_conv.qual_) and the
2756 array-to-pointer conversion (_conv.array_) are applied.
2757 [Note: In particular, neither the null pointer conversion
2758 (_conv.ptr_) nor the derived-to-base conversion
2759 (_conv.ptr_) are applied. Although 0 is a valid
2760 template-argument for a non-type template-parameter of
2761 integral type, it is not a valid template-argument for a
2762 non-type template-parameter of pointer type.]
2764 The call to decay_conversion performs the
2765 array-to-pointer conversion, if appropriate. */
2766 expr
= decay_conversion (expr
);
2768 if (expr
== error_mark_node
)
2769 return error_mark_node
;
2771 return perform_qualification_conversions (type
, expr
);
2776 case REFERENCE_TYPE
:
2778 tree type_referred_to
= TREE_TYPE (type
);
2780 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2782 /* For a non-type template-parameter of type reference to
2783 function, no conversions apply. If the
2784 template-argument represents a set of overloaded
2785 functions, the matching function is selected from the
2786 set (_over.over_). */
2790 fn
= instantiate_type (type_referred_to
, fns
, 0);
2792 if (fn
== error_mark_node
)
2793 return error_mark_node
;
2795 if (!TREE_PUBLIC (fn
))
2797 if (really_overloaded_fn (fns
))
2798 /* Don't issue an error here; we might get a different
2799 function if the overloading had worked out
2801 return error_mark_node
;
2806 my_friendly_assert (same_type_p (type_referred_to
,
2814 /* For a non-type template-parameter of type reference to
2815 object, no conversions apply. The type referred to by the
2816 reference may be more cv-qualified than the (otherwise
2817 identical) type of the template-argument. The
2818 template-parameter is bound directly to the
2819 template-argument, which must be an lvalue. */
2820 if ((TYPE_MAIN_VARIANT (expr_type
)
2821 != TYPE_MAIN_VARIANT (type_referred_to
))
2822 || !at_least_as_qualified_p (type_referred_to
,
2824 || !real_lvalue_p (expr
))
2825 return error_mark_node
;
2834 if (!TYPE_PTRMEMFUNC_P (type
))
2835 /* This handles templates like
2836 template<class T, T t> void f();
2837 when T is substituted with any class. The second template
2838 parameter becomes invalid and the template candidate is
2840 return error_mark_node
;
2842 /* For a non-type template-parameter of type pointer to member
2843 function, no conversions apply. If the template-argument
2844 represents a set of overloaded member functions, the
2845 matching member function is selected from the set
2848 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2849 expr_type
!= unknown_type_node
)
2850 return error_mark_node
;
2852 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2854 /* A ptr-to-member constant. */
2855 if (!same_type_p (type
, expr_type
))
2856 return error_mark_node
;
2861 if (TREE_CODE (expr
) != ADDR_EXPR
)
2862 return error_mark_node
;
2864 expr
= instantiate_type (type
, expr
, 0);
2866 if (expr
== error_mark_node
)
2867 return error_mark_node
;
2869 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2876 /* All non-type parameters must have one of these types. */
2877 my_friendly_abort (0);
2881 return error_mark_node
;
2884 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2885 template template parameters. Both PARM_PARMS and ARG_PARMS are
2886 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2889 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2890 the case, then extra parameters must have default arguments.
2892 Consider the example:
2893 template <class T, class Allocator = allocator> class vector;
2894 template<template <class U> class TT> class C;
2896 C<vector> is a valid instantiation. PARM_PARMS for the above code
2897 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2898 T and Allocator) and OUTER_ARGS contains the argument that is used to
2899 substitute the TT parameter. */
2902 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
2903 in_decl
, outer_args
)
2904 tree parm_parms
, arg_parms
;
2906 tree in_decl
, outer_args
;
2908 int nparms
, nargs
, i
;
2911 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
2912 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
2914 nparms
= TREE_VEC_LENGTH (parm_parms
);
2915 nargs
= TREE_VEC_LENGTH (arg_parms
);
2917 /* The rule here is opposite of coerce_template_parms. */
2920 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
2923 for (i
= 0; i
< nparms
; ++i
)
2925 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
2926 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
2928 if (arg
== NULL_TREE
|| arg
== error_mark_node
2929 || parm
== NULL_TREE
|| parm
== error_mark_node
)
2932 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2935 switch (TREE_CODE (parm
))
2941 /* We encounter instantiations of templates like
2942 template <template <template <class> class> class TT>
2945 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2946 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2948 if (!coerce_template_template_parms (parmparm
, argparm
,
2956 /* The tsubst call is used to handle cases such as
2957 template <class T, template <T> class TT> class D;
2958 i.e. the parameter list of TT depends on earlier parameters. */
2959 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
2966 my_friendly_abort (0);
2972 /* Convert the indicated template ARG as necessary to match the
2973 indicated template PARM. Returns the converted ARG, or
2974 error_mark_node if the conversion was unsuccessful. Error messages
2975 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2976 parameter in the parameter list. ARGS is the full set of template
2977 arguments deduced so far. */
2980 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
2990 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2992 inner_args
= innermost_args (args
);
2994 if (TREE_CODE (arg
) == TREE_LIST
2995 && TREE_TYPE (arg
) != NULL_TREE
2996 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2998 /* The template argument was the name of some
2999 member function. That's usually
3000 illegal, but static members are OK. In any
3001 case, grab the underlying fields/functions
3002 and issue an error later if required. */
3003 arg
= TREE_VALUE (arg
);
3004 TREE_TYPE (arg
) = unknown_type_node
;
3007 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3008 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3009 || requires_tmpl_type
);
3011 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3012 we also accept implicitly created TYPE_DECL as a valid argument.
3013 This is necessary to handle the case where we pass a template name
3014 to a template template parameter in a scope where we've derived from
3015 in instantiation of that template, so the template name refers to that
3016 instantiation. We really ought to handle this better. */
3018 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3019 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3020 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3021 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
3022 || (TREE_CODE (arg
) == RECORD_TYPE
3023 && CLASSTYPE_TEMPLATE_INFO (arg
)
3024 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3025 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3026 && requires_tmpl_type
3027 && is_base_of_enclosing_class (arg
, current_class_type
)));
3028 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3029 arg
= TYPE_STUB_DECL (arg
);
3030 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3031 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3033 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
3035 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3036 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3038 cp_pedwarn ("to refer to a type member of a template parameter,");
3039 cp_pedwarn (" use `typename %E'", arg
);
3041 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3042 TREE_OPERAND (arg
, 1));
3045 if (is_type
!= requires_type
)
3051 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3054 cp_error (" expected a constant of type `%T', got `%T'",
3056 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3058 cp_error (" expected a type, got `%E'", arg
);
3061 return error_mark_node
;
3063 if (is_tmpl_type
^ requires_tmpl_type
)
3065 if (in_decl
&& complain
)
3067 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3070 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3072 cp_error (" expected a class template, got `%T'", arg
);
3074 return error_mark_node
;
3079 if (requires_tmpl_type
)
3081 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3082 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3084 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3085 in_decl
, inner_args
))
3089 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3091 if (val
!= error_mark_node
3092 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3093 val
= TREE_TYPE (val
);
3097 if (in_decl
&& complain
)
3099 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3101 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3104 val
= error_mark_node
;
3109 val
= groktypename (arg
);
3110 if (! processing_template_decl
)
3112 /* [basic.link]: A name with no linkage (notably, the
3113 name of a class or enumeration declared in a local
3114 scope) shall not be used to declare an entity with
3115 linkage. This implies that names with no linkage
3116 cannot be used as template arguments. */
3117 tree t
= no_linkage_check (val
);
3120 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3122 ("template-argument `%T' uses anonymous type", val
);
3125 ("template-argument `%T' uses local type `%T'",
3127 return error_mark_node
;
3134 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3136 if (processing_template_decl
)
3137 arg
= maybe_fold_nontype_arg (arg
);
3139 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3140 /* We used to call digest_init here. However, digest_init
3141 will report errors, which we don't want when complain
3142 is zero. More importantly, digest_init will try too
3143 hard to convert things: for example, `0' should not be
3144 converted to pointer type at this point according to
3145 the standard. Accepting this is not merely an
3146 extension, since deciding whether or not these
3147 conversions can occur is part of determining which
3148 function template to call, or whether a given epxlicit
3149 argument specification is legal. */
3150 val
= convert_nontype_argument (t
, arg
);
3154 if (val
== NULL_TREE
)
3155 val
= error_mark_node
;
3156 else if (val
== error_mark_node
&& complain
)
3157 cp_error ("could not convert template argument `%E' to `%T'",
3164 /* Convert all template arguments to their appropriate types, and
3165 return a vector containing the innermost resulting template
3166 arguments. If any error occurs, return error_mark_node, and, if
3167 COMPLAIN is non-zero, issue an error message. Some error messages
3168 are issued even if COMPLAIN is zero; for instance, if a template
3169 argument is composed from a local class.
3171 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3172 provided in ARGLIST, or else trailing parameters must have default
3173 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3174 deduction for any unspecified trailing arguments.
3176 The resulting TREE_VEC is allocated on a temporary obstack, and
3177 must be explicitly copied if it will be permanent. */
3180 coerce_template_parms (parms
, args
, in_decl
,
3182 require_all_arguments
)
3186 int require_all_arguments
;
3188 int nparms
, nargs
, i
, lost
= 0;
3191 tree new_inner_args
;
3193 inner_args
= innermost_args (args
);
3194 nargs
= NUM_TMPL_ARGS (inner_args
);
3195 nparms
= TREE_VEC_LENGTH (parms
);
3199 && require_all_arguments
3200 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3204 cp_error ("wrong number of template arguments (%d, should be %d)",
3208 cp_error_at ("provided for `%D'", in_decl
);
3211 return error_mark_node
;
3214 new_inner_args
= make_temp_vec (nparms
);
3215 new_args
= add_outermost_template_args (args
, new_inner_args
);
3216 for (i
= 0; i
< nparms
; i
++)
3221 /* Get the Ith template parameter. */
3222 parm
= TREE_VEC_ELT (parms
, i
);
3224 /* Calculate the Ith argument. */
3225 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3227 arg
= TREE_VALUE (inner_args
);
3228 inner_args
= TREE_CHAIN (inner_args
);
3231 arg
= TREE_VEC_ELT (inner_args
, i
);
3232 /* If no template argument was supplied, look for a default
3234 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3236 /* There was no default value. */
3237 my_friendly_assert (!require_all_arguments
, 0);
3240 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3241 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3243 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3246 /* Now, convert the Ith argument, as necessary. */
3247 if (arg
== NULL_TREE
)
3248 /* We're out of arguments. */
3250 my_friendly_assert (!require_all_arguments
, 0);
3253 else if (arg
== error_mark_node
)
3255 cp_error ("template argument %d is invalid", i
+ 1);
3256 arg
= error_mark_node
;
3259 arg
= convert_template_argument (TREE_VALUE (parm
),
3260 arg
, new_args
, complain
, i
,
3263 if (arg
== error_mark_node
)
3265 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3269 return error_mark_node
;
3271 return new_inner_args
;
3274 /* Returns 1 if template args OT and NT are equivalent. */
3277 template_args_equal (ot
, nt
)
3282 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3284 if (TREE_CODE (nt
) == TREE_VEC
)
3285 /* For member templates */
3286 return comp_template_args (ot
, nt
);
3287 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3288 return same_type_p (ot
, nt
);
3290 return (cp_tree_equal (ot
, nt
) > 0);
3293 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3294 of template arguments. Returns 0 otherwise. */
3297 comp_template_args (oldargs
, newargs
)
3298 tree oldargs
, newargs
;
3302 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3305 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3307 tree nt
= TREE_VEC_ELT (newargs
, i
);
3308 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3310 if (! template_args_equal (ot
, nt
))
3316 /* Given class template name and parameter list, produce a user-friendly name
3317 for the instantiation. */
3320 mangle_class_name_for_template (name
, parms
, arglist
)
3322 tree parms
, arglist
;
3324 static struct obstack scratch_obstack
;
3325 static char *scratch_firstobj
;
3328 if (!scratch_firstobj
)
3329 gcc_obstack_init (&scratch_obstack
);
3331 obstack_free (&scratch_obstack
, scratch_firstobj
);
3332 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3334 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3335 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3339 nparms
= TREE_VEC_LENGTH (parms
);
3340 arglist
= innermost_args (arglist
);
3341 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3342 for (i
= 0; i
< nparms
; i
++)
3344 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3345 tree arg
= TREE_VEC_ELT (arglist
, i
);
3350 if (TREE_CODE (parm
) == TYPE_DECL
)
3352 cat (type_as_string_real (arg
, 0, 1));
3355 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3357 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3359 /* Already substituted with real template. Just output
3360 the template name here */
3361 tree context
= DECL_CONTEXT (arg
);
3364 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
, 980422);
3365 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3368 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3371 /* Output the parameter declaration */
3372 cat (type_as_string_real (arg
, 0, 1));
3376 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3378 if (TREE_CODE (arg
) == TREE_LIST
)
3380 /* New list cell was built because old chain link was in
3382 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3383 arg
= TREE_VALUE (arg
);
3385 /* No need to check arglist against parmlist here; we did that
3386 in coerce_template_parms, called from lookup_template_class. */
3387 cat (expr_as_string (arg
, 0));
3390 char *bufp
= obstack_next_free (&scratch_obstack
);
3392 while (bufp
[offset
- 1] == ' ')
3394 obstack_blank_fast (&scratch_obstack
, offset
);
3396 /* B<C<char> >, not B<C<char>> */
3397 if (bufp
[offset
- 1] == '>')
3402 return (char *) obstack_base (&scratch_obstack
);
3406 classtype_mangled_name (t
)
3409 if (CLASSTYPE_TEMPLATE_INFO (t
)
3410 /* Specializations have already had their names set up in
3411 lookup_template_class. */
3412 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3414 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3416 /* For non-primary templates, the template parameters are
3417 implicit from their surrounding context. */
3418 if (PRIMARY_TEMPLATE_P (tmpl
))
3420 tree name
= DECL_NAME (tmpl
);
3421 char *mangled_name
= mangle_class_name_for_template
3422 (IDENTIFIER_POINTER (name
),
3423 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3424 CLASSTYPE_TI_ARGS (t
));
3425 tree id
= get_identifier (mangled_name
);
3426 IDENTIFIER_TEMPLATE (id
) = name
;
3431 return TYPE_IDENTIFIER (t
);
3435 add_pending_template (d
)
3440 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3441 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3443 ti
= DECL_TEMPLATE_INFO (d
);
3445 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3448 *template_tail
= perm_tree_cons
3449 (build_srcloc_here (), d
, NULL_TREE
);
3450 template_tail
= &TREE_CHAIN (*template_tail
);
3451 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3455 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3456 may be either a _DECL or an overloaded function or an
3457 IDENTIFIER_NODE), and ARGLIST. */
3460 lookup_template_function (fns
, arglist
)
3465 if (fns
== NULL_TREE
)
3467 cp_error ("non-template used as template");
3468 return error_mark_node
;
3471 type
= TREE_TYPE (fns
);
3472 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3473 type
= unknown_type_node
;
3475 if (processing_template_decl
)
3476 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3478 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3481 /* Within the scope of a template class S<T>, the name S gets bound
3482 (in build_self_reference) to a TYPE_DECL for the class, not a
3483 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3484 or one of its enclosing classes, and that type is a template,
3485 return the associated TEMPLATE_DECL. Otherwise, the original
3486 DECL is returned. */
3489 maybe_get_template_decl_from_type_decl (decl
)
3492 return (decl
!= NULL_TREE
3493 && TREE_CODE (decl
) == TYPE_DECL
3494 && DECL_ARTIFICIAL (decl
)
3495 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3496 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3499 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3500 parameters, find the desired type.
3502 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3503 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3504 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3505 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3506 copy it here to keep it from being reclaimed when the decl storage
3509 IN_DECL, if non-NULL, is the template declaration we are trying to
3512 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3513 the class we are looking up.
3515 If the template class is really a local class in a template
3516 function, then the FUNCTION_CONTEXT is the function in which it is
3517 being instantiated. */
3520 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3526 tree
template = NULL_TREE
, parmlist
;
3529 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3531 if (IDENTIFIER_VALUE (d1
)
3532 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3533 template = IDENTIFIER_VALUE (d1
);
3537 push_decl_namespace (context
);
3538 if (current_class_type
!= NULL_TREE
)
3540 maybe_get_template_decl_from_type_decl
3541 (IDENTIFIER_CLASS_VALUE (d1
));
3542 if (template == NULL_TREE
)
3543 template = lookup_name_nonclass (d1
);
3545 pop_decl_namespace ();
3548 context
= DECL_CONTEXT (template);
3550 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3552 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)))
3554 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
3555 d1
= DECL_NAME (template);
3558 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3559 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3560 && IS_AGGR_TYPE (d1
)))
3562 template = TYPE_TI_TEMPLATE (d1
);
3563 d1
= DECL_NAME (template);
3565 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3566 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3569 d1
= DECL_NAME (template);
3570 context
= DECL_CONTEXT (template);
3573 my_friendly_abort (272);
3575 /* With something like `template <class T> class X class X { ... };'
3576 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3577 We don't want to do that, but we have to deal with the situation,
3578 so let's give them some syntax errors to chew on instead of a
3582 cp_error ("`%T' is not a template", d1
);
3583 return error_mark_node
;
3586 if (context
== NULL_TREE
)
3587 context
= global_namespace
;
3589 if (TREE_CODE (template) != TEMPLATE_DECL
)
3591 cp_error ("non-template type `%T' used as a template", d1
);
3593 cp_error_at ("for template declaration `%D'", in_decl
);
3594 return error_mark_node
;
3597 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3599 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3600 template arguments */
3602 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3603 tree template2
= TYPE_STUB_DECL (parm
);
3606 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3608 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3609 if (arglist2
== error_mark_node
)
3610 return error_mark_node
;
3612 arglist2
= copy_to_permanent (arglist2
);
3613 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3614 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
3615 TYPE_SIZE (parm
) = 0;
3620 tree template_type
= TREE_TYPE (template);
3622 tree found
= NULL_TREE
;
3625 int is_partial_instantiation
;
3627 template = most_general_template (template);
3628 parmlist
= DECL_TEMPLATE_PARMS (template);
3629 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3630 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3632 /* We build up the coerced arguments and such on the
3633 momentary_obstack. */
3636 if (arg_depth
== 1 && parm_depth
> 1)
3638 /* We've been given an incomplete set of template arguments.
3641 template <class T> struct S1 {
3642 template <class U> struct S2 {};
3643 template <class U> struct S2<U*> {};
3646 we will be called with an ARGLIST of `U*', but the
3647 TEMPLATE will be `template <class T> template
3648 <class U> struct S1<T>::S2'. We must fill in the missing
3650 my_friendly_assert (context
!= NULL_TREE
, 0);
3651 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context
))
3652 && context
!= global_namespace
)
3653 context
= DECL_REAL_CONTEXT (context
);
3655 if (context
== global_namespace
)
3656 /* This is bad. We cannot get enough arguments, even from
3657 the surrounding context, to resolve this class. One
3658 case where this might happen is (illegal) code like:
3666 We should catch this error sooner (at the opening curly
3667 for `S', but it is better to be safe than sorry here. */
3669 cp_error ("invalid use of `%D'", template);
3670 return error_mark_node
;
3673 arglist
= add_to_template_args (TYPE_TI_ARGS (context
),
3675 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3678 my_friendly_assert (parm_depth
== arg_depth
, 0);
3680 /* Calculate the BOUND_ARGS. These will be the args that are
3681 actually tsubst'd into the definition to create the
3685 /* We have multiple levels of arguments to coerce, at once. */
3687 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3689 tree bound_args
= make_temp_vec (parm_depth
);
3691 for (i
= saved_depth
,
3692 t
= DECL_TEMPLATE_PARMS (template);
3693 i
> 0 && t
!= NULL_TREE
;
3694 --i
, t
= TREE_CHAIN (t
))
3696 tree a
= coerce_template_parms (TREE_VALUE (t
),
3697 arglist
, template, 1, 1);
3698 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3700 /* We temporarily reduce the length of the ARGLIST so
3701 that coerce_template_parms will see only the arguments
3702 corresponding to the template parameters it is
3704 TREE_VEC_LENGTH (arglist
)--;
3707 /* Restore the ARGLIST to its full size. */
3708 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3710 arglist
= bound_args
;
3714 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3715 innermost_args (arglist
),
3718 if (arglist
== error_mark_node
)
3719 /* We were unable to bind the arguments. */
3720 return error_mark_node
;
3722 /* In the scope of a template class, explicit references to the
3723 template class refer to the type of the template, not any
3724 instantiation of it. For example, in:
3726 template <class T> class C { void f(C<T>); }
3728 the `C<T>' is just the same as `C'. Outside of the
3729 class, however, such a reference is an instantiation. */
3730 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3733 found
= template_type
;
3735 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3739 /* Note that we use DECL_CONTEXT, rather than
3740 CP_DECL_CONTEXT, so that the termination test is
3741 always just `ctx'. We're not interested in namepace
3743 for (ctx
= current_class_type
;
3745 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3746 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3747 if (same_type_p (ctx
, template_type
))
3751 /* We're not in the scope of the class, so the
3752 TEMPLATE_TYPE is not the type we want after
3760 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3761 found
; found
= TREE_CHAIN (found
))
3762 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3766 found
= TREE_VALUE (found
);
3775 /* Since we didn't find the type, we'll have to create it.
3776 Since we'll be saving this type on the
3777 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3778 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3780 /* This type is a "partial instantiation" if any of the template
3781 arguments still inolve template parameters. Note that we set
3782 IS_PARTIAL_INSTANTIATION for partial specializations as
3784 is_partial_instantiation
= uses_template_parms (arglist
);
3786 /* Create the type. */
3787 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3789 if (!is_partial_instantiation
)
3790 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3792 /* We don't want to call start_enum for this type, since
3793 the values for the enumeration constants may involve
3794 template parameters. And, no one should be interested
3795 in the enumeration constants for such a type. */
3796 t
= make_node (ENUMERAL_TYPE
);
3800 t
= make_lang_type (TREE_CODE (template_type
));
3801 CLASSTYPE_DECLARED_CLASS (t
)
3802 = CLASSTYPE_DECLARED_CLASS (template_type
);
3803 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3804 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3805 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3808 /* If we called start_enum above, this information will already
3812 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3814 /* Create a stub TYPE_DECL for it. */
3815 type_decl
= build_decl (TYPE_DECL
, DECL_NAME (template), t
);
3816 SET_DECL_ARTIFICIAL (type_decl
);
3817 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3818 DECL_SOURCE_FILE (type_decl
)
3819 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
3820 DECL_SOURCE_LINE (type_decl
)
3821 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
3822 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = type_decl
;
3825 type_decl
= TYPE_NAME (t
);
3827 /* Set up the template information. We have to figure out which
3828 template is the immediate parent if this is a full
3830 if (parm_depth
== 1 || is_partial_instantiation
3831 || !PRIMARY_TEMPLATE_P (template))
3832 /* This case is easy; there are no member templates involved. */
3836 /* This is a full instantiation of a member template. There
3837 should be some partial instantiation of which this is an
3840 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3841 found
; found
= TREE_CHAIN (found
))
3844 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
3846 /* We only want partial instantiations, here, not
3847 specializations or full instantiations. */
3848 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
3849 || !uses_template_parms (TREE_VALUE (found
)))
3852 /* Temporarily reduce by one the number of levels in the
3853 ARGLIST and in FOUND so as to avoid comparing the
3854 last set of arguments. */
3855 TREE_VEC_LENGTH (arglist
)--;
3856 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
3858 /* See if the arguments match. If they do, then TMPL is
3859 the partial instantiation we want. */
3860 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
3862 /* Restore the argument vectors to their full size. */
3863 TREE_VEC_LENGTH (arglist
)++;
3864 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
3874 my_friendly_abort (0);
3877 arglist
= copy_to_permanent (arglist
);
3878 SET_TYPE_TEMPLATE_INFO (t
,
3879 tree_cons (found
, arglist
, NULL_TREE
));
3880 DECL_TEMPLATE_INSTANTIATIONS (template)
3881 = tree_cons (arglist
, t
,
3882 DECL_TEMPLATE_INSTANTIATIONS (template));
3884 if (TREE_CODE (t
) == ENUMERAL_TYPE
3885 && !is_partial_instantiation
)
3886 /* Now that the type has been registered on the instantiations
3887 list, we set up the enumerators. Because the enumeration
3888 constants may involve the enumeration type itself, we make
3889 sure to register the type first, and then create the
3890 constants. That way, doing tsubst_expr for the enumeration
3891 constants won't result in recursive calls here; we'll find
3892 the instantiation and exit above. */
3893 tsubst_enum (template_type
, t
, arglist
);
3895 /* We're done with the permanent obstack, now. */
3897 /* We're also done with the momentary allocation we started
3901 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3903 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
3904 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
3905 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
3906 if (!is_partial_instantiation
)
3908 DECL_ASSEMBLER_NAME (type_decl
)
3909 = get_identifier (build_overload_name (t
, 1, 1));
3911 /* For backwards compatibility; code that uses
3912 -fexternal-templates expects looking up a template to
3913 instantiate it. I think DDD still relies on this.
3914 (jason 8/20/1998) */
3915 if (TREE_CODE (t
) != ENUMERAL_TYPE
3916 && flag_external_templates
3917 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3918 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3919 add_pending_template (t
);
3922 /* If the type makes use of template parameters, the
3923 code that generates debugging information will crash. */
3924 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
3930 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3931 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3932 If FN returns non-zero, the iteration is terminated, and
3933 for_each_template_parm returns 1. Otherwise, the iteration
3934 continues. If FN never returns a non-zero value, the value
3935 returned by for_each_template_parm is 0. If FN is NULL, it is
3936 considered to be the function which always returns 1. */
3939 for_each_template_parm (t
, fn
, data
)
3947 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
3948 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
3951 switch (TREE_CODE (t
))
3955 /* We assume that the object must be instantiated in order to build
3956 the COMPONENT_REF, so we test only whether the type of the
3957 COMPONENT_REF uses template parms. */
3958 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3961 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
3962 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
3964 case IDENTIFIER_NODE
:
3965 if (!IDENTIFIER_TEMPLATE (t
))
3967 my_friendly_abort (42);
3969 /* aggregates of tree nodes */
3972 int i
= TREE_VEC_LENGTH (t
);
3974 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
3979 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
3980 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
3982 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
3985 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
3987 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
3989 /* constructed type nodes */
3991 case REFERENCE_TYPE
:
3992 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3995 if (TYPE_PTRMEMFUNC_FLAG (t
))
3996 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
4002 if (! TYPE_TEMPLATE_INFO (t
))
4004 return for_each_template_parm (TREE_VALUE
4005 (TYPE_TEMPLATE_INFO (t
)),
4008 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4013 /* Check the parameter types. Since default arguments are not
4014 instantiated until they are needed, the TYPE_ARG_TYPES may
4015 contain expressions that involve template parameters. But,
4016 no-one should be looking at them yet. And, once they're
4017 instantiated, they don't contain template parameters, so
4018 there's no point in looking at them then, either. */
4022 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4023 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4027 /* Check the return type, too. */
4028 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4031 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
4033 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4035 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
4037 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4041 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4044 /* A template template parameter is encountered */
4045 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4046 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4047 /* Already substituted template template parameter */
4051 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
4053 goto check_type_and_context
;
4057 /* ??? What about FIELD_DECLs? */
4058 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4059 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4063 check_type_and_context
:
4064 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4066 if (DECL_CONTEXT (t
)
4067 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4072 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4073 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4076 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4078 /* template parm nodes */
4079 case TEMPLATE_TEMPLATE_PARM
:
4080 /* Record template parameters such as `T' inside `TT<T>'. */
4081 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
4082 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4084 case TEMPLATE_TYPE_PARM
:
4085 case TEMPLATE_PARM_INDEX
:
4087 return (*fn
)(t
, data
);
4091 /* simple type nodes */
4093 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
4095 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
4101 case NAMESPACE_DECL
:
4111 /* Non-error_mark_node ERROR_MARKs are bad things. */
4112 my_friendly_assert (t
== error_mark_node
, 274);
4121 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4124 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4127 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4128 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4129 (TREE_TYPE (t
)), fn
, data
);
4130 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
4134 case REINTERPRET_CAST_EXPR
:
4135 case CONST_CAST_EXPR
:
4136 case STATIC_CAST_EXPR
:
4137 case DYNAMIC_CAST_EXPR
:
4145 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4148 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
4156 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
4157 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
4164 sorry ("testing %s for template parms",
4165 tree_code_name
[(int) TREE_CODE (t
)]);
4166 my_friendly_abort (82);
4173 uses_template_parms (t
)
4176 return for_each_template_parm (t
, 0, 0);
4179 static struct tinst_level
*current_tinst_level
;
4180 static struct tinst_level
*free_tinst_level
;
4181 static int tinst_depth
;
4182 extern int max_tinst_depth
;
4183 #ifdef GATHER_STATISTICS
4186 int tinst_level_tick
;
4187 int last_template_error_tick
;
4189 /* Print out all the template instantiations that we are currently
4190 working on. If ERR, we are being called from cp_thing, so do
4191 the right thing for an error message. */
4194 print_template_context (err
)
4197 struct tinst_level
*p
= current_tinst_level
;
4199 char *file
= input_filename
;
4203 if (current_function_decl
!= p
->decl
4204 && current_function_decl
!= NULL_TREE
)
4205 /* We can get here during the processing of some synthesized
4206 method. Then, p->decl will be the function that's causing
4211 if (current_function_decl
== p
->decl
)
4212 /* Avoid redundancy with the the "In function" line. */;
4214 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4215 file
, decl_as_string (p
->decl
, 0));
4223 for (; p
; p
= p
->next
)
4225 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4226 decl_as_string (p
->decl
, 0));
4230 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4233 /* Called from cp_thing to print the template context for an error. */
4236 maybe_print_template_context ()
4238 if (last_template_error_tick
== tinst_level_tick
4239 || current_tinst_level
== 0)
4242 last_template_error_tick
= tinst_level_tick
;
4243 print_template_context (1);
4247 push_tinst_level (d
)
4250 struct tinst_level
*new;
4252 if (tinst_depth
>= max_tinst_depth
)
4254 /* If the instantiation in question still has unbound template parms,
4255 we don't really care if we can't instantiate it, so just return.
4256 This happens with base instantiation for implicit `typename'. */
4257 if (uses_template_parms (d
))
4260 last_template_error_tick
= tinst_level_tick
;
4261 error ("template instantiation depth exceeds maximum of %d",
4263 error (" (use -ftemplate-depth-NN to increase the maximum)");
4264 cp_error (" instantiating `%D'", d
);
4266 print_template_context (0);
4271 if (free_tinst_level
)
4273 new = free_tinst_level
;
4274 free_tinst_level
= new->next
;
4277 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4281 new->file
= input_filename
;
4282 new->next
= current_tinst_level
;
4283 current_tinst_level
= new;
4286 #ifdef GATHER_STATISTICS
4287 if (tinst_depth
> depth_reached
)
4288 depth_reached
= tinst_depth
;
4298 struct tinst_level
*old
= current_tinst_level
;
4300 /* Restore the filename and line number stashed away when we started
4301 this instantiation. */
4303 input_filename
= old
->file
;
4304 extract_interface_info ();
4306 current_tinst_level
= old
->next
;
4307 old
->next
= free_tinst_level
;
4308 free_tinst_level
= old
;
4313 struct tinst_level
*
4316 struct tinst_level
*p
= current_tinst_level
;
4319 for (; p
->next
; p
= p
->next
)
4324 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4325 vector of template arguments, as for tsubst.
4327 Returns an appropriate tsbust'd friend declaration. */
4330 tsubst_friend_function (decl
, args
)
4336 char *file
= input_filename
;
4338 lineno
= DECL_SOURCE_LINE (decl
);
4339 input_filename
= DECL_SOURCE_FILE (decl
);
4341 if (TREE_CODE (decl
) == FUNCTION_DECL
4342 && DECL_TEMPLATE_INSTANTIATION (decl
)
4343 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4344 /* This was a friend declared with an explicit template
4345 argument list, e.g.:
4349 to indicate that f was a template instantiation, not a new
4350 function declaration. Now, we have to figure out what
4351 instantiation of what template. */
4358 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4359 args
, /*complain=*/1,
4361 tsubst (DECL_TI_ARGS (decl
),
4362 args
, /*complain=*/1,
4364 /* FIXME: The decl we create via the next tsubst could be
4365 created on a temporary obstack. */
4366 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4367 tmpl
= determine_specialization (template_id
, new_friend
,
4369 /*need_member_template=*/0,
4371 new_friend
= instantiate_template (tmpl
, new_args
);
4375 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4377 /* The NEW_FRIEND will look like an instantiation, to the
4378 compiler, but is not an instantiation from the point of view of
4379 the language. For example, we might have had:
4381 template <class T> struct S {
4382 template <class U> friend void f(T, U);
4385 Then, in S<int>, template <class U> void f(int, U) is not an
4386 instantiation of anything. */
4387 DECL_USE_TEMPLATE (new_friend
) = 0;
4388 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4389 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4391 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4392 tsubst will have resulted in a call to
4393 set_mangled_name_for_template_decl. But, the function is not a
4394 template instantiation and should not be mangled like one.
4395 Therefore, we remangle the function name. We don't have to do
4396 this if the NEW_FRIEND is a template since
4397 set_mangled_name_for_template_decl doesn't do anything if the
4398 function declaration still uses template arguments. */
4399 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4401 set_mangled_name_for_decl (new_friend
);
4402 DECL_RTL (new_friend
) = 0;
4403 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4406 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4409 tree new_friend_template_info
;
4410 tree new_friend_result_template_info
;
4411 int new_friend_is_defn
;
4413 /* We must save some information from NEW_FRIEND before calling
4414 duplicate decls since that function will free NEW_FRIEND if
4416 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4417 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4419 /* This declaration is a `primary' template. */
4420 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4423 = DECL_INITIAL (DECL_RESULT (new_friend
)) != NULL_TREE
;
4424 new_friend_result_template_info
4425 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend
));
4429 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4430 new_friend_result_template_info
= NULL_TREE
;
4433 old_decl
= pushdecl_namespace_level (new_friend
);
4435 if (old_decl
!= new_friend
)
4437 /* This new friend declaration matched an existing
4438 declaration. For example, given:
4440 template <class T> void f(T);
4441 template <class U> class C {
4442 template <class T> friend void f(T) {}
4445 the friend declaration actually provides the definition
4446 of `f', once C has been instantiated for some type. So,
4447 old_decl will be the out-of-class template declaration,
4448 while new_friend is the in-class definition.
4450 But, if `f' was called before this point, the
4451 instantiation of `f' will have DECL_TI_ARGS corresponding
4452 to `T' but not to `U', references to which might appear
4453 in the definition of `f'. Previously, the most general
4454 template for an instantiation of `f' was the out-of-class
4455 version; now it is the in-class version. Therefore, we
4456 run through all specialization of `f', adding to their
4457 DECL_TI_ARGS appropriately. In particular, they need a
4458 new set of outer arguments, corresponding to the
4459 arguments for this class instantiation.
4461 The same situation can arise with something like this:
4464 template <class T> class C {
4468 when `C<int>' is instantiated. Now, `f(int)' is defined
4471 if (!new_friend_is_defn
)
4472 /* On the other hand, if the in-class declaration does
4473 *not* provide a definition, then we don't want to alter
4474 existing definitions. We can just leave everything
4479 /* Overwrite whatever template info was there before, if
4480 any, with the new template information pertaining to
4482 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4484 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4485 /* duplicate_decls will take care of this case. */
4490 tree new_friend_args
;
4492 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl
))
4493 = new_friend_result_template_info
;
4495 new_friend_args
= TI_ARGS (new_friend_template_info
);
4496 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4500 tree spec
= TREE_VALUE (t
);
4503 = add_outermost_template_args (new_friend_args
,
4504 DECL_TI_ARGS (spec
));
4506 = copy_to_permanent (DECL_TI_ARGS (spec
));
4509 /* Now, since specializations are always supposed to
4510 hang off of the most general template, we must move
4512 t
= most_general_template (old_decl
);
4515 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4516 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4517 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4518 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4523 /* The information from NEW_FRIEND has been merged into OLD_DECL
4524 by duplicate_decls. */
4525 new_friend
= old_decl
;
4528 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4530 /* Check to see that the declaration is really present, and,
4531 possibly obtain an improved declaration. */
4532 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4541 input_filename
= file
;
4545 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4546 template arguments, as for tsubst.
4548 Returns an appropriate tsbust'd friend type. */
4551 tsubst_friend_class (friend_tmpl
, args
)
4556 tree tmpl
= lookup_name (DECL_NAME (friend_tmpl
), 1);
4558 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4560 if (tmpl
!= NULL_TREE
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4562 /* The friend template has already been declared. Just
4563 check to see that the declarations match, and install any new
4564 default parameters. We must tsubst the default parameters,
4565 of course. We only need the innermost template parameters
4566 because that is all that redeclare_class_template will look
4569 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4570 args
, /*complain=*/1);
4571 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4572 friend_type
= TREE_TYPE (tmpl
);
4576 /* The friend template has not already been declared. In this
4577 case, the instantiation of the template class will cause the
4578 injection of this template into the global scope. */
4579 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4581 /* The new TMPL is not an instantiation of anything, so we
4582 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4583 the new type because that is supposed to be the corresponding
4584 template decl, i.e., TMPL. */
4585 DECL_USE_TEMPLATE (tmpl
) = 0;
4586 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4587 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4589 /* Inject this template into the global scope. */
4590 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4597 instantiate_class_template (type
)
4600 tree
template, args
, pattern
, t
;
4603 if (type
== error_mark_node
)
4604 return error_mark_node
;
4606 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4609 /* We want to allocate temporary vectors of template arguments and
4610 template argument expressions on the momentary obstack, not on
4611 the expression obstack. Otherwise, all the space allocated in
4612 argument coercion and such is simply lost. */
4615 /* Figure out which template is being instantiated. */
4616 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4617 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4619 /* Figure out which arguments are being used to do the
4621 args
= CLASSTYPE_TI_ARGS (type
);
4622 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4624 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4625 /* If this is a partial instantiation, then we can't instantiate
4626 the type; there's no telling whether or not one of the
4627 template parameters might eventually be instantiated to some
4628 value that results in a specialization being used. For
4640 Now, the `S<U>' in `f<int>' is the specialization, not an
4641 instantiation of the original template. */
4644 /* Determine what specialization of the original template to
4646 if (PARTIAL_INSTANTIATION_P (type
))
4647 /* There's no telling which specialization is appropriate at this
4648 point. Since all peeking at the innards of this partial
4649 instantiation are extensions (like the "implicit typename"
4650 extension, which allows users to omit the keyword `typename' on
4651 names that are declared as types in template base classes), we
4652 are free to do what we please.
4654 Trying to figure out which partial instantiation to use can
4655 cause a crash. (Some of the template arguments don't even have
4656 types.) So, we just use the most general version. */
4660 t
= most_specialized_class (template, args
);
4662 if (t
== error_mark_node
)
4664 char *str
= "candidates are:";
4665 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4666 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4669 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4672 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4676 TYPE_BEING_DEFINED (type
) = 1;
4677 type
= error_mark_node
;
4683 pattern
= TREE_TYPE (t
);
4685 pattern
= TREE_TYPE (template);
4687 /* If the template we're instantiating is incomplete, then clearly
4688 there's nothing we can do. */
4689 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4692 /* If this is a partial instantiation, don't tsubst anything. We will
4693 only use this type for implicit typename, so the actual contents don't
4694 matter. All that matters is whether a particular name is a type. */
4695 if (PARTIAL_INSTANTIATION_P (type
))
4697 /* The fields set here must be kept in sync with those cleared
4698 in begin_class_definition. */
4699 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4700 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4701 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4702 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4703 /* Pretend that the type is complete, so that we will look
4704 inside it during name lookup and such. */
4705 TYPE_SIZE (type
) = integer_zero_node
;
4709 /* If we've recursively instantiated too many templates, stop. */
4710 if (! push_tinst_level (type
))
4713 /* Now we're really doing the instantiation. Mark the type as in
4714 the process of being defined. */
4715 TYPE_BEING_DEFINED (type
) = 1;
4717 maybe_push_to_top_level (uses_template_parms (type
));
4718 pushclass (type
, 0);
4722 /* This TYPE is actually a instantiation of of a partial
4723 specialization. We replace the innermost set of ARGS with
4724 the arguments appropriate for substitution. For example,
4727 template <class T> struct S {};
4728 template <class T> struct S<T*> {};
4730 and supposing that we are instantiating S<int*>, ARGS will
4731 present be {int*} but we need {int}. */
4733 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4736 /* If there were multiple levels in ARGS, replacing the
4737 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4738 want, so we make a copy first. */
4739 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4741 args
= copy_node (args
);
4742 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4748 if (flag_external_templates
)
4750 if (flag_alt_external_templates
)
4752 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4753 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4754 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4755 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4756 && CLASSTYPE_INTERFACE_KNOWN (type
));
4760 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4761 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4762 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4763 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4764 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4765 && CLASSTYPE_INTERFACE_KNOWN (type
));
4770 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4771 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4774 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4775 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4776 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
4777 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4778 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4779 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4780 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4781 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4782 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4783 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4784 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4785 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
4786 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
4787 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
4788 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
4789 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
4790 TYPE_USES_COMPLEX_INHERITANCE (type
)
4791 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
4792 TYPE_USES_MULTIPLE_INHERITANCE (type
)
4793 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
4794 TYPE_USES_VIRTUAL_BASECLASSES (type
)
4795 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
4796 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
4797 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
4798 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
4800 /* We must copy the arguments to the permanent obstack since
4801 during the tsubst'ing below they may wind up in the
4802 DECL_TI_ARGS of some instantiated member template. */
4803 args
= copy_to_permanent (args
);
4805 if (TYPE_BINFO_BASETYPES (pattern
))
4807 tree base_list
= NULL_TREE
;
4808 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
4811 /* Substitute into each of the bases to determine the actual
4813 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
4819 pbase
= TREE_VEC_ELT (pbases
, i
);
4821 /* Substitue to figure out the base class. */
4822 base
= tsubst (BINFO_TYPE (pbase
), args
,
4823 /*complain=*/1, NULL_TREE
);
4824 if (base
== error_mark_node
)
4827 /* Calculate the correct access node. */
4828 if (TREE_VIA_VIRTUAL (pbase
))
4830 if (TREE_VIA_PUBLIC (pbase
))
4831 access
= access_public_virtual_node
;
4832 else if (TREE_VIA_PROTECTED (pbase
))
4833 access
= access_protected_virtual_node
;
4834 else if (TREE_VIA_PRIVATE (pbase
))
4835 access
= access_private_virtual_node
;
4839 if (TREE_VIA_PUBLIC (pbase
))
4840 access
= access_public_node
;
4841 else if (TREE_VIA_PROTECTED (pbase
))
4842 access
= access_protected_node
;
4843 else if (TREE_VIA_PRIVATE (pbase
))
4844 access
= access_private_node
;
4847 base_list
= tree_cons (access
, base
, base_list
);
4850 /* The list is now in reverse order; correct that. */
4851 base_list
= nreverse (base_list
);
4853 /* Now call xref_basetypes to set up all the base-class
4855 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
4856 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
4857 ? class_type_node
: record_type_node
)
4859 DECL_NAME (TYPE_NAME (pattern
)),
4864 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
4866 tree tag
= TREE_VALUE (t
);
4867 tree name
= TYPE_IDENTIFIER (tag
);
4870 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
4871 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
4873 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
4874 /* Unfortunately, lookup_template_class sets
4875 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4876 instantiation (i.e., for the type of a member template
4877 class nested within a template class.) This behavior is
4878 required for maybe_process_partial_specialization to work
4879 correctly, but is not accurate in this case; the TAG is not
4880 an instantiation of anything. (The corresponding
4881 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4882 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
4884 /* Now, we call pushtag to put this NEWTAG into the scope of
4885 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4886 pushtag calling push_template_decl. We don't have to do
4887 this for enums because it will already have been done in
4890 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
4891 pushtag (name
, newtag
, /*globalize=*/0);
4895 /* Don't replace enum constants here. */
4896 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
4897 if (TREE_CODE (t
) != CONST_DECL
)
4901 /* The the file and line for this declaration, to assist in
4902 error message reporting. Since we called push_tinst_level
4903 above, we don't need to restore these. */
4904 lineno
= DECL_SOURCE_LINE (t
);
4905 input_filename
= DECL_SOURCE_FILE (t
);
4907 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
4908 if (TREE_CODE (r
) == VAR_DECL
)
4910 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
4911 /* Perhaps we should do more of grokfield here. */
4912 if (DECL_DEFINED_IN_CLASS_P (r
))
4913 /* Set up DECL_INITIAL, since tsubst doesn't. */
4914 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
4915 /*complain=*/1, NULL_TREE
);
4917 DECL_IN_AGGR_P (r
) = 1;
4918 DECL_EXTERNAL (r
) = 1;
4919 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
4920 if (DECL_DEFINED_IN_CLASS_P (r
))
4921 check_static_variable_definition (r
, TREE_TYPE (r
));
4924 /* R will have a TREE_CHAIN if and only if it has already been
4925 processed by finish_member_declaration. This can happen
4926 if, for example, it is a TYPE_DECL for a class-scoped
4927 ENUMERAL_TYPE; such a thing will already have been added to
4928 the field list by tsubst_enum above. */
4929 if (!TREE_CHAIN (r
))
4931 set_current_access_from_decl (r
);
4932 finish_member_declaration (r
);
4936 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4937 for this instantiation. */
4938 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
4940 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
4941 set_current_access_from_decl (r
);
4942 finish_member_declaration (r
);
4945 /* Construct the DECL_FRIENDLIST for the new class type. */
4946 typedecl
= TYPE_MAIN_DECL (type
);
4947 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
4953 DECL_FRIENDLIST (typedecl
)
4954 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
4955 DECL_FRIENDLIST (typedecl
));
4957 for (friends
= TREE_VALUE (t
);
4958 friends
!= NULL_TREE
;
4959 friends
= TREE_CHAIN (friends
))
4961 if (TREE_PURPOSE (friends
) == error_mark_node
)
4963 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4964 = tree_cons (error_mark_node
,
4965 tsubst_friend_function (TREE_VALUE (friends
),
4967 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4971 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4972 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
,
4973 /*complain=*/1, NULL_TREE
),
4975 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4981 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
4985 tree friend_type
= TREE_VALUE (t
);
4986 tree new_friend_type
;
4988 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4989 new_friend_type
= tsubst_friend_class (friend_type
, args
);
4990 else if (uses_template_parms (friend_type
))
4991 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
4994 /* The call to xref_tag_from_type does injection for friend
4997 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5000 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5001 /* Trick make_friend_class into realizing that the friend
5002 we're adding is a template, not an ordinary class. It's
5003 important that we use make_friend_class since it will
5004 perform some error-checking and output cross-reference
5006 ++processing_template_decl
;
5008 make_friend_class (type
, new_friend_type
);
5010 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5011 --processing_template_decl
;
5014 /* This does injection for friend functions. */
5015 if (!processing_template_decl
)
5017 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
5018 /*complain=*/1, NULL_TREE
);
5020 for (; t
; t
= TREE_CHAIN (t
))
5022 tree d
= TREE_VALUE (t
);
5024 if (TREE_CODE (d
) == TYPE_DECL
)
5025 /* Already injected. */;
5031 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5032 if (TREE_CODE (t
) == FIELD_DECL
)
5034 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5035 require_complete_type (t
);
5038 /* Set the file and line number information to whatever is given for
5039 the class itself. This puts error messages involving generated
5040 implicit functions at a predictable point, and the same point
5041 that would be used for non-template classes. */
5042 lineno
= DECL_SOURCE_LINE (typedecl
);
5043 input_filename
= DECL_SOURCE_FILE (typedecl
);
5045 unreverse_member_declarations (type
);
5046 type
= finish_struct_1 (type
, 0);
5047 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5049 /* Clear this now so repo_template_used is happy. */
5050 TYPE_BEING_DEFINED (type
) = 0;
5051 repo_template_used (type
);
5054 pop_from_top_level ();
5067 if (t1
== NULL_TREE
)
5068 return t2
== NULL_TREE
;
5069 if (t2
== NULL_TREE
)
5071 /* Don't care if one declares its arg const and the other doesn't -- the
5072 main variant of the arg type is all that matters. */
5073 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5074 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5076 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5079 /* If arg is a non-type template parameter that does not depend on template
5080 arguments, fold it like we weren't in the body of a template. */
5083 maybe_fold_nontype_arg (arg
)
5086 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
5087 && !uses_template_parms (arg
))
5089 /* Sometimes, one of the args was an expression involving a
5090 template constant parameter, like N - 1. Now that we've
5091 tsubst'd, we might have something like 2 - 1. This will
5092 confuse lookup_template_class, so we do constant folding
5093 here. We have to unset processing_template_decl, to
5094 fool build_expr_from_tree() into building an actual
5097 int saved_processing_template_decl
= processing_template_decl
;
5098 processing_template_decl
= 0;
5099 arg
= fold (build_expr_from_tree (arg
));
5100 processing_template_decl
= saved_processing_template_decl
;
5105 /* Return the TREE_VEC with the arguments for the innermost template header,
5106 where ARGS is either that or the VEC of VECs for all the
5110 innermost_args (args
)
5113 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
5116 /* Substitute ARGS into the vector of template arguments T. */
5119 tsubst_template_arg_vector (t
, args
, complain
)
5124 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5125 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5127 bzero ((char *) elts
, len
* sizeof (tree
));
5129 for (i
= 0; i
< len
; i
++)
5131 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5132 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5133 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5136 elts
[i
] = maybe_fold_nontype_arg
5137 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5140 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5147 t
= make_temp_vec (len
);
5148 for (i
= 0; i
< len
; i
++)
5149 TREE_VEC_ELT (t
, i
) = elts
[i
];
5154 /* Return the result of substituting ARGS into the template parameters
5155 given by PARMS. If there are m levels of ARGS and m + n levels of
5156 PARMS, then the result will contain n levels of PARMS. For
5157 example, if PARMS is `template <class T> template <class U>
5158 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5159 result will be `template <int*, double, class V>'. */
5162 tsubst_template_parms (parms
, args
, complain
)
5168 tree
* new_parms
= &r
;
5170 for (new_parms
= &r
;
5171 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5172 new_parms
= &(TREE_CHAIN (*new_parms
)),
5173 parms
= TREE_CHAIN (parms
))
5176 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5179 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5181 tree default_value
=
5182 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5184 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5186 TREE_VEC_ELT (new_vec
, i
)
5187 = build_tree_list (tsubst (default_value
, args
, complain
,
5189 tsubst (parm_decl
, args
, complain
,
5194 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5195 - TMPL_ARGS_DEPTH (args
))),
5196 new_vec
, NULL_TREE
);
5202 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5203 type T. If T is not an aggregate or enumeration type, it is
5204 handled as if by tsubst. IN_DECL is as for tsubst. If
5205 ENTERING_SCOPE is non-zero, T is the context for a template which
5206 we are presently tsubst'ing. Return the subsituted value. */
5209 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5219 switch (TREE_CODE (t
))
5222 if (TYPE_PTRMEMFUNC_P (t
))
5224 tree r
= build_ptrmemfunc_type
5225 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5226 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5229 /* else fall through */
5232 if (TYPE_TEMPLATE_INFO (t
))
5238 /* First, determine the context for the type we are looking
5240 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5241 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5243 in_decl
, /*entering_scope=*/1);
5245 context
= NULL_TREE
;
5247 /* Then, figure out what arguments are appropriate for the
5248 type we are trying to find. For example, given:
5250 template <class T> struct S;
5251 template <class T, class U> void f(T, U) { S<U> su; }
5253 and supposing that we are instantiating f<int, double>,
5254 then our ARGS will be {int, double}, but, when looking up
5255 S we only want {double}. */
5257 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5260 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5264 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5267 /* This is not a template type, so there's nothing to do. */
5271 return tsubst (t
, args
, complain
, in_decl
);
5275 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5276 (already computed) substitution of ARGS into TREE_TYPE (T), if
5277 appropriate. Return the result of the substitution. IN_DECL is as
5281 tsubst_decl (t
, args
, type
, in_decl
)
5288 char* saved_filename
;
5291 /* Set the filename and linenumber to improve error-reporting. */
5292 saved_lineno
= lineno
;
5293 saved_filename
= input_filename
;
5294 lineno
= DECL_SOURCE_LINE (t
);
5295 input_filename
= DECL_SOURCE_FILE (t
);
5297 switch (TREE_CODE (t
))
5301 /* We can get here when processing a member template function
5302 of a template class. */
5303 tree decl
= DECL_TEMPLATE_RESULT (t
);
5305 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5307 if (!is_template_template_parm
)
5309 /* We might already have an instance of this template.
5310 The ARGS are for the surrounding class type, so the
5311 full args contain the tsubst'd args for the context,
5312 plus the innermost args from the template decl. */
5313 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5314 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5315 : DECL_TI_ARGS (DECL_RESULT (t
));
5319 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5322 /* tsubst_template_arg_vector doesn't copy the vector if
5323 nothing changed. But, *something* should have
5325 my_friendly_assert (full_args
!= tmpl_args
, 0);
5327 spec
= retrieve_specialization (t
, full_args
);
5329 if (spec
!= NULL_TREE
)
5336 /* Make a new template decl. It will be similar to the
5337 original, but will record the current template arguments.
5338 We also create a new function declaration, which is just
5339 like the old one, but points to this new template, rather
5340 than the old one. */
5343 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5344 TREE_CHAIN (r
) = NULL_TREE
;
5346 if (is_template_template_parm
)
5348 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5349 DECL_RESULT (r
) = new_decl
;
5350 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5355 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1,
5356 in_decl
, /*entering_scope=*/1);
5357 DECL_CLASS_CONTEXT (r
)
5358 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5359 /*complain=*/1, in_decl
,
5360 /*entering_scope=*/1);
5361 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5363 if (TREE_CODE (decl
) == TYPE_DECL
)
5365 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5366 /*complain=*/1, in_decl
);
5367 TREE_TYPE (r
) = new_type
;
5368 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5369 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5370 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5374 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5375 DECL_RESULT (r
) = new_decl
;
5376 DECL_TI_TEMPLATE (new_decl
) = r
;
5377 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5378 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5381 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5382 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5383 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5385 /* The template parameters for this new template are all the
5386 template parameters for the old template, except the
5387 outermost level of parameters. */
5388 DECL_TEMPLATE_PARMS (r
)
5389 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5392 if (PRIMARY_TEMPLATE_P (t
))
5393 DECL_PRIMARY_TEMPLATE (r
) = r
;
5395 /* We don't partially instantiate partial specializations. */
5396 if (TREE_CODE (decl
) == TYPE_DECL
)
5399 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5401 spec
= TREE_CHAIN (spec
))
5403 /* It helps to consider example here. Consider:
5414 Now, for example, we are instantiating S<int>::f(U u).
5415 We want to make a template:
5420 It will have a specialization, for the case U = int*, of
5424 void S<int>::f<int*>(int*);
5426 This specialization will be an instantiation of
5427 the specialization given in the declaration of S, with
5428 argument list int*. */
5430 tree fn
= TREE_VALUE (spec
);
5434 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5435 /* Instantiations are on the same list, but they're of
5436 no concern to us. */
5439 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5440 /* A full specialization. There's no need to record
5444 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
5445 /*complain=*/1, in_decl
);
5446 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5447 spec_args
, /*complain=*/1, in_decl
);
5448 DECL_TI_TEMPLATE (new_fn
) = fn
;
5449 register_specialization (new_fn
, r
,
5450 innermost_args (spec_args
));
5453 /* Record this partial instantiation. */
5454 register_specialization (r
, t
,
5455 DECL_TI_ARGS (DECL_RESULT (r
)));
5463 tree argvec
= NULL_TREE
;
5469 /* Nobody should be tsubst'ing into non-template functions. */
5470 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5472 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5476 /* Allocate template arguments on the momentary obstack,
5477 in case we don't need to keep them. */
5480 /* Calculate the most general template of which R is a
5481 specialization, and the complete set of arguments used to
5483 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5485 = tsubst_template_arg_vector (DECL_TI_ARGS
5486 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5487 args
, /*complain=*/1);
5489 /* Check to see if we already have this specialization. */
5490 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5499 /* We're going to need to keep the ARGVEC, so we copy it
5501 argvec
= copy_to_permanent (argvec
);
5504 /* Here, we deal with the peculiar case:
5506 template <class T> struct S {
5507 template <class U> friend void f();
5509 template <class U> friend void f() {}
5511 template void f<double>();
5513 Here, the ARGS for the instantiation of will be {int,
5514 double}. But, we only need as many ARGS as there are
5515 levels of template parameters in CODE_PATTERN. We are
5516 careful not to get fooled into reducing the ARGS in
5519 template <class T> struct S { template <class U> void f(U); }
5520 template <class T> template <> void S<T>::f(int) {}
5522 which we can spot because the pattern will be a
5523 specialization in this case. */
5524 args_depth
= TMPL_ARGS_DEPTH (args
);
5526 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5527 if (args_depth
> parms_depth
5528 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5530 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5532 if (parms_depth
> 1)
5536 args
= make_temp_vec (parms_depth
);
5537 for (i
= 0; i
< parms_depth
; ++i
)
5538 TREE_VEC_ELT (args
, i
) =
5539 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5542 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5547 /* This special case arises when we have something like this:
5549 template <class T> struct S {
5550 friend void f<int>(int, double);
5553 Here, the DECL_TI_TEMPLATE for the friend declaration
5554 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5555 being called from tsubst_friend_function, and we want
5556 only to create a new decl (R) with appropriate types so
5557 that we can call determine_specialization. */
5558 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5560 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5561 == IDENTIFIER_NODE
), 0);
5562 gen_tmpl
= NULL_TREE
;
5565 if (DECL_CLASS_SCOPE_P (t
))
5567 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5571 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5573 /*entering_scope=*/1);
5580 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5582 /* We do NOT check for matching decls pushed separately at this
5583 point, as they may not represent instantiations of this
5584 template, and in any case are considered separate under the
5585 discrete model. Instead, see add_maybe_template. */
5589 DECL_USE_TEMPLATE (r
) = 0;
5590 TREE_TYPE (r
) = type
;
5593 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1, t
,
5594 /*entering_scope=*/1);
5595 DECL_CLASS_CONTEXT (r
) = ctx
;
5597 if (member
&& IDENTIFIER_TYPENAME_P (DECL_NAME (r
)))
5598 /* Type-conversion operator. Reconstruct the name, in
5599 case it's the name of one of the template's parameters. */
5600 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5602 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5604 DECL_MAIN_VARIANT (r
) = r
;
5605 DECL_RESULT (r
) = NULL_TREE
;
5607 TREE_STATIC (r
) = 0;
5608 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5609 DECL_EXTERNAL (r
) = 1;
5610 DECL_INTERFACE_KNOWN (r
) = 0;
5611 DECL_DEFER_OUTPUT (r
) = 0;
5612 TREE_CHAIN (r
) = NULL_TREE
;
5613 DECL_PENDING_INLINE_INFO (r
) = 0;
5616 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5617 name. There's no need to do this in the special friend
5618 case mentioned above where GEN_TMPL is NULL. */
5621 /* The ARGVEC was built on the momentary obstack. Make it
5623 argvec
= copy_to_permanent (argvec
);
5624 DECL_TEMPLATE_INFO (r
)
5625 = perm_tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5626 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5627 register_specialization (r
, gen_tmpl
, argvec
);
5629 /* Set the mangled name for R. */
5630 if (DECL_DESTRUCTOR_P (t
))
5631 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5634 /* Instantiations of template functions must be mangled
5635 specially, in order to conform to 14.5.5.1
5636 [temp.over.link]. */
5637 tree tmpl
= DECL_TI_TEMPLATE (t
);
5639 /* TMPL will be NULL if this is a specialization of a
5640 member function of a template class. */
5641 if (name_mangling_version
< 1
5642 || tmpl
== NULL_TREE
5643 || (member
&& !is_member_template (tmpl
)
5644 && !DECL_TEMPLATE_INFO (tmpl
)))
5645 set_mangled_name_for_decl (r
);
5647 set_mangled_name_for_template_decl (r
);
5651 make_decl_rtl (r
, NULL_PTR
, 1);
5653 /* Like grokfndecl. If we don't do this, pushdecl will
5654 mess up our TREE_CHAIN because it doesn't find a
5655 previous decl. Sigh. */
5657 && ! uses_template_parms (r
)
5658 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5660 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5663 if (DECL_CONSTRUCTOR_P (r
))
5665 maybe_retrofit_in_chrg (r
);
5666 grok_ctor_properties (ctx
, r
);
5668 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5669 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5676 TREE_TYPE (r
) = type
;
5677 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5678 DECL_INITIAL (r
) = TREE_TYPE (r
);
5680 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5681 /*complain=*/1, in_decl
);
5683 DECL_CONTEXT (r
) = NULL_TREE
;
5684 #ifdef PROMOTE_PROTOTYPES
5685 if ((TREE_CODE (type
) == INTEGER_TYPE
5686 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5687 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5688 DECL_ARG_TYPE (r
) = integer_type_node
;
5691 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5692 /*complain=*/1, TREE_CHAIN (t
));
5699 TREE_TYPE (r
) = type
;
5702 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
,
5703 /*complain=*/1, in_decl
);
5705 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
5706 /*complain=*/1, in_decl
);
5707 TREE_CHAIN (r
) = NULL_TREE
;
5708 if (TREE_CODE (type
) == VOID_TYPE
)
5709 cp_error_at ("instantiation of `%D' as type void", r
);
5717 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
5718 TREE_CHAIN (r
) = NULL_TREE
;
5728 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5730 in_decl
, /*entering_scope=*/1);
5732 /* Nobody should be tsubst'ing into non-template variables. */
5733 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
5734 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5736 /* Check to see if we already have this specialization. */
5737 tmpl
= DECL_TI_TEMPLATE (t
);
5738 gen_tmpl
= most_general_template (tmpl
);
5739 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
5740 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5749 TREE_TYPE (r
) = type
;
5750 DECL_CONTEXT (r
) = ctx
;
5751 if (TREE_STATIC (r
))
5752 DECL_ASSEMBLER_NAME (r
)
5753 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
5755 /* Don't try to expand the initializer until someone tries to use
5756 this variable; otherwise we run into circular dependencies. */
5757 DECL_INITIAL (r
) = NULL_TREE
;
5761 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
5763 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
5764 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5765 register_specialization (r
, gen_tmpl
, argvec
);
5767 TREE_CHAIN (r
) = NULL_TREE
;
5768 if (TREE_CODE (type
) == VOID_TYPE
)
5769 cp_error_at ("instantiation of `%D' as type void", r
);
5774 if (t
== TYPE_NAME (TREE_TYPE (t
)))
5775 r
= TYPE_NAME (type
);
5779 TREE_TYPE (r
) = type
;
5780 DECL_CONTEXT (r
) = current_class_type
;
5781 TREE_CHAIN (r
) = NULL_TREE
;
5786 my_friendly_abort (0);
5789 /* Restore the file and line information. */
5790 lineno
= saved_lineno
;
5791 input_filename
= saved_filename
;
5796 /* Substitue into the ARG_TYPES of a function type. */
5799 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
5805 tree remaining_arg_types
;
5808 if (!arg_types
|| arg_types
== void_list_node
)
5811 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
5812 args
, complain
, in_decl
);
5813 if (remaining_arg_types
== error_mark_node
)
5814 return error_mark_node
;
5816 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
5817 if (type
== error_mark_node
)
5818 return error_mark_node
;
5820 /* Do array-to-pointer, function-to-pointer conversion, and ignore
5821 top-level qualifiers as required. */
5822 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
5824 /* Note that we do not substitute into default arguments here. The
5825 standard mandates that they be instantiated only when needed,
5826 which is done in build_over_call. */
5827 return hash_tree_cons_simple (TREE_PURPOSE (arg_types
), type
,
5828 remaining_arg_types
);
5832 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
5833 *not* handle the exception-specification for FNTYPE, because the
5834 initial substitution of explicitly provided template parameters
5835 during argument deduction forbids substitution into the
5836 exception-specification:
5840 All references in the function type of the function template to the
5841 corresponding template parameters are replaced by the specified tem-
5842 plate argument values. If a substitution in a template parameter or
5843 in the function type of the function template results in an invalid
5844 type, type deduction fails. [Note: The equivalent substitution in
5845 exception specifications is done only when the function is instanti-
5846 ated, at which point a program is ill-formed if the substitution
5847 results in an invalid type.] */
5850 tsubst_function_type (t
, args
, complain
, in_decl
)
5860 /* The TYPE_CONTEXT is not used for function/method types. */
5861 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
5863 /* Substitue the return type. */
5864 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
5865 if (return_type
== error_mark_node
)
5866 return error_mark_node
;
5868 /* Substitue the argument types. */
5869 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
5871 if (arg_types
== error_mark_node
)
5872 return error_mark_node
;
5874 /* Construct a new type node and return it. */
5875 if (TREE_CODE (t
) == FUNCTION_TYPE
)
5876 fntype
= build_function_type (return_type
, arg_types
);
5879 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
5880 if (! IS_AGGR_TYPE (r
))
5884 Type deduction may fail for any of the following
5887 -- Attempting to create "pointer to member of T" when T
5888 is not a class type. */
5890 cp_error ("creating pointer to member function of non-class type `%T'",
5892 return error_mark_node
;
5895 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
5898 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
5903 /* Substitute into the PARMS of a call-declarator. */
5906 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
5916 if (!parms
|| parms
== void_list_node
)
5919 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
5920 args
, complain
, in_decl
);
5922 /* Figure out the type of this parameter. */
5923 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
5925 /* Figure out the default argument as well. Note that we use
5926 tsubst_expr since the default argument is really an expression. */
5927 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
5929 /* Chain this parameter on to the front of those we have already
5930 processed. We don't use hash_tree_cons because that function
5931 doesn't check TREE_PARMLIST. */
5932 new_parms
= tree_cons (defarg
, type
, new_parms
);
5934 /* And note that these are parameters. */
5935 TREE_PARMLIST (new_parms
) = 1;
5940 /* Take the tree structure T and replace template parameters used
5941 therein with the argument vector ARGS. IN_DECL is an associated
5942 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
5943 An appropriate error message is issued only if COMPLAIN is
5944 non-zero. Note that we must be relatively non-tolerant of
5945 extensions here, in order to preserve conformance; if we allow
5946 substitutions that should not be allowed, we may allow argument
5947 deductions that should not succeed, and therefore report ambiguous
5948 overload situations where there are none. In theory, we could
5949 allow the substitution, but indicate that it should have failed,
5950 and allow our caller to make sure that the right thing happens, but
5951 we don't try to do this yet.
5953 This function is used for dealing with types, decls and the like;
5954 for expressions, use tsubst_expr or tsubst_copy. */
5957 tsubst (t
, args
, complain
, in_decl
)
5964 if (t
== NULL_TREE
|| t
== error_mark_node
5965 || t
== integer_type_node
5966 || t
== void_type_node
5967 || t
== char_type_node
5968 || TREE_CODE (t
) == NAMESPACE_DECL
)
5971 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
5972 type
= IDENTIFIER_TYPE_VALUE (t
);
5974 type
= TREE_TYPE (t
);
5975 if (type
== unknown_type_node
)
5976 my_friendly_abort (42);
5978 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
5979 && TREE_CODE (t
) != TYPENAME_TYPE
5980 && TREE_CODE (t
) != TEMPLATE_DECL
5981 && TREE_CODE (t
) != IDENTIFIER_NODE
5982 && TREE_CODE (t
) != FUNCTION_TYPE
5983 && TREE_CODE (t
) != METHOD_TYPE
)
5984 type
= tsubst (type
, args
, complain
, in_decl
);
5985 if (type
== error_mark_node
)
5986 return error_mark_node
;
5988 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
5989 return tsubst_decl (t
, args
, type
, in_decl
);
5991 switch (TREE_CODE (t
))
5996 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
5997 /*entering_scope=*/0);
6000 case IDENTIFIER_NODE
:
6012 if (t
== integer_type_node
)
6015 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6016 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6020 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6022 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6023 if (max
== error_mark_node
)
6024 return error_mark_node
;
6026 if (processing_template_decl
)
6028 tree itype
= make_node (INTEGER_TYPE
);
6029 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6030 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6035 if (integer_zerop (omax
))
6037 /* Still allow an explicit array of size zero. */
6039 pedwarn ("creating array with size zero");
6041 else if (integer_zerop (max
) || INT_CST_LT (max
, integer_zero_node
))
6045 Type deduction may fail for any of the following
6048 Attempting to create an array with a size that is
6049 zero or negative. */
6051 cp_error ("creating array with size `%E'", max
);
6053 return error_mark_node
;
6056 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
6057 if (!TREE_PERMANENT (max
) && !allocation_temporary_p ())
6058 max
= copy_to_permanent (max
);
6059 return build_index_type (max
);
6062 case TEMPLATE_TYPE_PARM
:
6063 case TEMPLATE_TEMPLATE_PARM
:
6064 case TEMPLATE_PARM_INDEX
:
6072 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6073 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6075 idx
= TEMPLATE_TYPE_IDX (t
);
6076 level
= TEMPLATE_TYPE_LEVEL (t
);
6080 idx
= TEMPLATE_PARM_IDX (t
);
6081 level
= TEMPLATE_PARM_LEVEL (t
);
6084 if (TREE_VEC_LENGTH (args
) > 0)
6086 tree arg
= NULL_TREE
;
6088 levels
= TMPL_ARGS_DEPTH (args
);
6089 if (level
<= levels
)
6090 arg
= TMPL_ARG (args
, level
, idx
);
6092 if (arg
== error_mark_node
)
6093 return error_mark_node
;
6094 else if (arg
!= NULL_TREE
)
6096 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6098 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
6100 return cp_build_qualified_type
6101 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
));
6103 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6105 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6107 /* We are processing a type constructed from
6108 a template template parameter */
6109 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6110 args
, complain
, in_decl
);
6111 if (argvec
== error_mark_node
)
6112 return error_mark_node
;
6114 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6115 we are resolving nested-types in the signature of
6116 a member function templates.
6117 Otherwise ARG is a TEMPLATE_DECL and is the real
6118 template to be instantiated. */
6119 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6120 arg
= TYPE_NAME (arg
);
6122 r
= lookup_template_class (DECL_NAME (arg
),
6125 /*entering_scope=*/0);
6126 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
6129 /* We are processing a template argument list. */
6137 my_friendly_abort (981018);
6140 /* This can happen during the attempted tsubst'ing in
6141 unify. This means that we don't yet have any information
6142 about the template parameter in question. */
6145 /* If we get here, we must have been looking at a parm for a
6146 more deeply nested template. Make a new version of this
6147 template parameter, but with a lower level. */
6148 switch (TREE_CODE (t
))
6150 case TEMPLATE_TYPE_PARM
:
6151 case TEMPLATE_TEMPLATE_PARM
:
6153 TEMPLATE_TYPE_PARM_INDEX (r
)
6154 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6156 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6157 TYPE_MAIN_VARIANT (r
) = r
;
6158 TYPE_POINTER_TO (r
) = NULL_TREE
;
6159 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6161 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6162 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6164 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6166 if (argvec
== error_mark_node
)
6167 return error_mark_node
;
6169 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6170 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
6174 case TEMPLATE_PARM_INDEX
:
6175 r
= reduce_template_parm_level (t
, type
, levels
);
6179 my_friendly_abort (0);
6187 tree purpose
, value
, chain
, result
;
6188 int via_public
, via_virtual
, via_protected
;
6190 if (t
== void_list_node
)
6193 via_public
= TREE_VIA_PUBLIC (t
);
6194 via_protected
= TREE_VIA_PROTECTED (t
);
6195 via_virtual
= TREE_VIA_VIRTUAL (t
);
6197 purpose
= TREE_PURPOSE (t
);
6200 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6201 if (purpose
== error_mark_node
)
6202 return error_mark_node
;
6204 value
= TREE_VALUE (t
);
6207 value
= tsubst (value
, args
, complain
, in_decl
);
6208 if (value
== error_mark_node
)
6209 return error_mark_node
;
6211 chain
= TREE_CHAIN (t
);
6212 if (chain
&& chain
!= void_type_node
)
6214 chain
= tsubst (chain
, args
, complain
, in_decl
);
6215 if (chain
== error_mark_node
)
6216 return error_mark_node
;
6218 if (purpose
== TREE_PURPOSE (t
)
6219 && value
== TREE_VALUE (t
)
6220 && chain
== TREE_CHAIN (t
))
6222 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
6223 purpose
, value
, chain
);
6224 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
6228 if (type
!= NULL_TREE
)
6230 /* A binfo node. We always need to make a copy, of the node
6231 itself and of its BINFO_BASETYPES. */
6235 /* Make sure type isn't a typedef copy. */
6236 type
= BINFO_TYPE (TYPE_BINFO (type
));
6238 TREE_TYPE (t
) = complete_type (type
);
6239 if (IS_AGGR_TYPE (type
))
6241 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6242 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6243 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6244 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6249 /* Otherwise, a vector of template arguments. */
6250 return tsubst_template_arg_vector (t
, args
, complain
);
6253 case REFERENCE_TYPE
:
6255 enum tree_code code
;
6257 if (type
== TREE_TYPE (t
))
6260 code
= TREE_CODE (t
);
6265 Type deduction may fail for any of the following
6268 -- Attempting to create a pointer to reference type.
6269 -- Attempting to create a reference to a reference type or
6270 a reference to void. */
6271 if (TREE_CODE (type
) == REFERENCE_TYPE
6272 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6274 static int last_line
= 0;
6275 static char* last_file
= 0;
6277 /* We keep track of the last time we issued this error
6278 message to avoid spewing a ton of messages during a
6279 single bad template instantiation. */
6280 if (complain
&& (last_line
!= lineno
||
6281 last_file
!= input_filename
))
6283 if (TREE_CODE (type
) == VOID_TYPE
)
6284 cp_error ("forming reference to void");
6286 cp_error ("forming %s to reference type `%T'",
6287 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6290 last_file
= input_filename
;
6293 return error_mark_node
;
6295 else if (code
== POINTER_TYPE
)
6296 r
= build_pointer_type (type
);
6298 r
= build_reference_type (type
);
6299 r
= cp_build_qualified_type (r
, TYPE_QUALS (t
));
6301 /* Will this ever be needed for TYPE_..._TO values? */
6307 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6308 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6312 Type deduction may fail for any of the following
6315 -- Attempting to create "pointer to member of T" when T
6316 is not a class type. */
6318 cp_error ("creating pointer to member of non-class type `%T'",
6320 return error_mark_node
;
6322 return build_offset_type (r
, type
);
6330 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6331 if (fntype
== error_mark_node
)
6332 return error_mark_node
;
6334 /* Substitue the exception specification. */
6335 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6338 raises
= tsubst (raises
, args
, complain
, in_decl
);
6339 if (raises
== error_mark_node
)
6341 fntype
= build_exception_variant (fntype
, raises
);
6347 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6348 if (domain
== error_mark_node
)
6349 return error_mark_node
;
6351 /* As an optimization, we avoid regenerating the array type if
6352 it will obviously be the same as T. */
6353 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6356 /* These checks should match the ones in grokdeclarator.
6360 The deduction may fail for any of the following reasons:
6362 -- Attempting to create an array with an element type that
6363 is void, a function type, or a reference type. */
6364 if (TREE_CODE (type
) == VOID_TYPE
6365 || TREE_CODE (type
) == FUNCTION_TYPE
6366 || TREE_CODE (type
) == REFERENCE_TYPE
)
6369 cp_error ("creating array of `%T'", type
);
6370 return error_mark_node
;
6373 r
= build_cplus_array_type (type
, domain
);
6380 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6382 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6385 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6386 return error_mark_node
;
6388 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6394 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6396 if (e
== error_mark_node
)
6397 return error_mark_node
;
6399 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6404 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6405 in_decl
, /*entering_scope=*/1);
6406 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6409 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6410 return error_mark_node
;
6412 /* Normally, make_typename_type does not require that the CTX
6413 have complete type in order to allow things like:
6415 template <class T> struct S { typename S<T>::X Y; };
6417 But, such constructs have already been resolved by this
6418 point, so here CTX really should have complete type, unless
6419 it's a partial instantiation. */
6420 if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6422 ctx
= complete_type (ctx
);
6423 if (!TYPE_SIZE (ctx
))
6426 incomplete_type_error (NULL_TREE
, ctx
);
6427 return error_mark_node
;
6431 f
= make_typename_type (ctx
, f
);
6432 return cp_build_qualified_type (f
,
6434 | CP_TYPE_QUALS (t
));
6439 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6441 if (e
== error_mark_node
)
6442 return error_mark_node
;
6443 return make_pointer_declarator (type
, e
);
6448 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6450 if (e
== error_mark_node
)
6451 return error_mark_node
;
6452 return make_reference_declarator (type
, e
);
6457 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6459 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6461 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6462 return error_mark_node
;
6464 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6469 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6471 tree e2
= tsubst_call_declarator_parms (TREE_OPERAND (t
, 1), args
,
6473 tree e3
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6475 if (e1
== error_mark_node
|| e2
== error_mark_node
6476 || e3
== error_mark_node
)
6477 return error_mark_node
;
6479 return make_call_declarator (e1
, e2
, TREE_OPERAND (t
, 2), e3
);
6484 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6486 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6487 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6488 return error_mark_node
;
6490 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6495 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6497 if (e1
== error_mark_node
)
6498 return error_mark_node
;
6500 return TREE_TYPE (e1
);
6504 sorry ("use of `%s' in template",
6505 tree_code_name
[(int) TREE_CODE (t
)]);
6506 return error_mark_node
;
6513 emit_line_note (input_filename
, lineno
);
6517 expand_start_bindings (0);
6524 int saved_warn_unused
= 0;
6526 if (processing_template_decl
)
6528 saved_warn_unused
= warn_unused
;
6531 expand_end_bindings (getdecls (), kept_level_p (), 0);
6532 if (processing_template_decl
)
6533 warn_unused
= saved_warn_unused
;
6534 t
= poplevel (kept_level_p (), 1, 0);
6539 /* Like tsubst, but deals with expressions. This function just replaces
6540 template parms; to finish processing the resultant expression, use
6544 tsubst_copy (t
, args
, complain
, in_decl
)
6549 enum tree_code code
;
6552 if (t
== NULL_TREE
|| t
== error_mark_node
)
6555 code
= TREE_CODE (t
);
6560 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6567 if (!DECL_CONTEXT (t
))
6568 /* This is a global enumeration constant. */
6571 /* Unfortunately, we cannot just call lookup_name here.
6574 template <int I> int f() {
6576 struct S { void g() { E e = a; } };
6579 When we instantiate f<7>::S::g(), say, lookup_name is not
6580 clever enough to find f<7>::a. */
6582 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6583 /*entering_scope=*/0);
6585 for (v
= TYPE_VALUES (enum_type
);
6588 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6589 return TREE_VALUE (v
);
6591 /* We didn't find the name. That should never happen; if
6592 name-lookup found it during preliminary parsing, we
6593 should find it again here during instantiation. */
6594 my_friendly_abort (0);
6599 if (DECL_CONTEXT (t
))
6603 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6604 /*entering_scope=*/1);
6605 if (ctx
!= DECL_CONTEXT (t
))
6606 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6612 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6613 t
= tsubst (t
, args
, complain
, in_decl
);
6618 if (is_member_template (t
))
6619 return tsubst (t
, args
, complain
, in_decl
);
6625 /* We must tsbust into a LOOKUP_EXPR in case the names to
6626 which it refers is a conversion operator; in that case the
6627 name will change. We avoid making unnecessary copies,
6630 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6632 if (id
!= TREE_OPERAND (t
, 0))
6634 r
= build_nt (LOOKUP_EXPR
, id
);
6635 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6643 case REINTERPRET_CAST_EXPR
:
6644 case CONST_CAST_EXPR
:
6645 case STATIC_CAST_EXPR
:
6646 case DYNAMIC_CAST_EXPR
:
6649 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6650 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6653 case PREDECREMENT_EXPR
:
6654 case PREINCREMENT_EXPR
:
6655 case POSTDECREMENT_EXPR
:
6656 case POSTINCREMENT_EXPR
:
6658 case TRUTH_NOT_EXPR
:
6661 case CONVERT_EXPR
: /* Unary + */
6669 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6674 case TRUNC_DIV_EXPR
:
6676 case FLOOR_DIV_EXPR
:
6677 case ROUND_DIV_EXPR
:
6678 case EXACT_DIV_EXPR
:
6680 case BIT_ANDTC_EXPR
:
6683 case TRUNC_MOD_EXPR
:
6684 case FLOOR_MOD_EXPR
:
6685 case TRUTH_ANDIF_EXPR
:
6686 case TRUTH_ORIF_EXPR
:
6687 case TRUTH_AND_EXPR
:
6708 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6709 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
6713 tree fn
= TREE_OPERAND (t
, 0);
6714 if (is_overloaded_fn (fn
))
6715 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
6717 /* Sometimes FN is a LOOKUP_EXPR. */
6718 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
6720 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
6725 case METHOD_CALL_EXPR
:
6727 tree name
= TREE_OPERAND (t
, 0);
6728 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
6730 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6732 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6734 else if (TREE_CODE (name
) == SCOPE_REF
6735 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
6737 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6739 name
= TREE_OPERAND (name
, 1);
6740 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6742 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6743 name
= build_nt (SCOPE_REF
, base
, name
);
6746 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6748 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
6750 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
6759 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6760 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6761 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
6763 if (code
== BIND_EXPR
&& !processing_template_decl
)
6765 /* This processing should really occur in tsubst_expr,
6766 However, tsubst_expr does not recurse into expressions,
6767 since it assumes that there aren't any statements
6768 inside them. Instead, it simply calls
6769 build_expr_from_tree. So, we need to expand the
6771 tree rtl_expr
= begin_stmt_expr ();
6772 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
,
6774 r
= finish_stmt_expr (rtl_expr
, block
);
6783 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6784 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6785 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
6786 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
6793 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6794 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
6795 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
6796 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
6800 case TEMPLATE_ID_EXPR
:
6802 /* Substituted template arguments */
6803 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
6806 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
6807 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
6809 return lookup_template_function
6810 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
6815 tree purpose
, value
, chain
;
6817 if (t
== void_list_node
)
6820 purpose
= TREE_PURPOSE (t
);
6822 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
6823 value
= TREE_VALUE (t
);
6825 value
= tsubst_copy (value
, args
, complain
, in_decl
);
6826 chain
= TREE_CHAIN (t
);
6827 if (chain
&& chain
!= void_type_node
)
6828 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
6829 if (purpose
== TREE_PURPOSE (t
)
6830 && value
== TREE_VALUE (t
)
6831 && chain
== TREE_CHAIN (t
))
6833 return tree_cons (purpose
, value
, chain
);
6840 case TEMPLATE_TYPE_PARM
:
6841 case TEMPLATE_TEMPLATE_PARM
:
6842 case TEMPLATE_PARM_INDEX
:
6844 case REFERENCE_TYPE
:
6851 return tsubst (t
, args
, complain
, in_decl
);
6853 case IDENTIFIER_NODE
:
6854 if (IDENTIFIER_TYPENAME_P (t
)
6855 /* Make sure it's not just a variable named `__opr', for instance,
6856 which can occur in some existing code. */
6858 return build_typename_overload
6859 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
6866 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6867 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
6868 complain
, in_decl
));
6869 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
6878 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6881 tsubst_expr (t
, args
, complain
, in_decl
)
6886 if (t
== NULL_TREE
|| t
== error_mark_node
)
6889 if (processing_template_decl
)
6890 return tsubst_copy (t
, args
, complain
, in_decl
);
6892 switch (TREE_CODE (t
))
6895 lineno
= TREE_COMPLEXITY (t
);
6896 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
6897 args
, complain
, in_decl
));
6901 lineno
= TREE_COMPLEXITY (t
);
6902 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
6903 args
, complain
, in_decl
));
6908 int i
= suspend_momentary ();
6911 lineno
= TREE_COMPLEXITY (t
);
6912 emit_line_note (input_filename
, lineno
);
6914 (tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6915 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6916 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
6917 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
6919 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
6920 resume_momentary (i
);
6927 lineno
= TREE_COMPLEXITY (t
);
6930 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6931 tsubst_expr (tmp
, args
, complain
, in_decl
);
6932 finish_for_init_stmt (NULL_TREE
);
6933 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
6936 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
6937 finish_for_expr (tmp
, NULL_TREE
);
6938 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
6939 finish_for_stmt (tmp
, NULL_TREE
);
6945 lineno
= TREE_COMPLEXITY (t
);
6946 begin_while_stmt ();
6947 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
6948 args
, complain
, in_decl
),
6950 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
6951 finish_while_stmt (NULL_TREE
);
6957 lineno
= TREE_COMPLEXITY (t
);
6959 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
6960 finish_do_body (NULL_TREE
);
6961 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
6971 lineno
= TREE_COMPLEXITY (t
);
6973 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
6974 args
, complain
, in_decl
),
6977 if (tmp
= THEN_CLAUSE (t
), tmp
)
6979 tsubst_expr (tmp
, args
, complain
, in_decl
);
6980 finish_then_clause (NULL_TREE
);
6983 if (tmp
= ELSE_CLAUSE (t
), tmp
)
6985 begin_else_clause ();
6986 tsubst_expr (tmp
, args
, complain
, in_decl
);
6987 finish_else_clause (NULL_TREE
);
6998 lineno
= TREE_COMPLEXITY (t
);
6999 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7000 for (substmt
= COMPOUND_BODY (t
);
7001 substmt
!= NULL_TREE
;
7002 substmt
= TREE_CHAIN (substmt
))
7003 tsubst_expr (substmt
, args
, complain
, in_decl
);
7004 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
7010 lineno
= TREE_COMPLEXITY (t
);
7011 finish_break_stmt ();
7015 lineno
= TREE_COMPLEXITY (t
);
7016 finish_continue_stmt ();
7023 lineno
= TREE_COMPLEXITY (t
);
7024 begin_switch_stmt ();
7025 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7026 finish_switch_cond (val
);
7028 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
7029 tsubst_expr (tmp
, args
, complain
, in_decl
);
7031 finish_switch_stmt (val
, NULL_TREE
);
7036 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7037 tsubst_expr (CASE_HIGH (t
), args
, complain
, in_decl
));
7041 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
7048 lineno
= TREE_COMPLEXITY (t
);
7049 t
= GOTO_DESTINATION (t
);
7050 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
7051 /* Computed goto's must be tsubst'd into. On the other hand,
7052 non-computed gotos must not be; the identifier in question
7053 will have no binding. */
7054 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7055 finish_goto_stmt (t
);
7059 lineno
= TREE_COMPLEXITY (t
);
7060 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, complain
, in_decl
),
7061 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7062 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7063 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7064 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7069 lineno
= TREE_COMPLEXITY (t
);
7071 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7072 finish_try_block (NULL_TREE
);
7074 tree handler
= TRY_HANDLERS (t
);
7075 for (; handler
; handler
= TREE_CHAIN (handler
))
7076 tsubst_expr (handler
, args
, complain
, in_decl
);
7078 finish_handler_sequence (NULL_TREE
);
7082 lineno
= TREE_COMPLEXITY (t
);
7084 if (HANDLER_PARMS (t
))
7086 tree d
= HANDLER_PARMS (t
);
7087 expand_start_catch_block
7088 (tsubst (TREE_OPERAND (d
, 1), args
, complain
, in_decl
),
7089 tsubst (TREE_OPERAND (d
, 0), args
, complain
, in_decl
));
7092 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
7093 finish_handler_parms (NULL_TREE
);
7094 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7095 finish_handler (NULL_TREE
);
7099 lineno
= TREE_COMPLEXITY (t
);
7101 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
7102 tsubst (t
, args
, complain
, NULL_TREE
);
7106 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7111 /* Instantiate the indicated variable or function template TMPL with
7112 the template arguments in TARG_PTR. */
7115 instantiate_template (tmpl
, targ_ptr
)
7116 tree tmpl
, targ_ptr
;
7122 struct obstack
*old_fmp_obstack
;
7123 extern struct obstack
*function_maybepermanent_obstack
;
7126 if (tmpl
== error_mark_node
)
7127 return error_mark_node
;
7129 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7131 /* Check to see if we already have this specialization. */
7132 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7133 if (spec
!= NULL_TREE
)
7136 if (DECL_TEMPLATE_INFO (tmpl
))
7138 /* The TMPL is a partial instantiation. To get a full set of
7139 arguments we must add the arguments used to perform the
7140 partial instantiation. */
7141 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7143 gen_tmpl
= most_general_template (tmpl
);
7145 /* Check to see if we already have this specialization. */
7146 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7147 if (spec
!= NULL_TREE
)
7153 push_obstacks (&permanent_obstack
, &permanent_obstack
);
7154 old_fmp_obstack
= function_maybepermanent_obstack
;
7155 function_maybepermanent_obstack
= &permanent_obstack
;
7157 len
= DECL_NTPARMS (gen_tmpl
);
7158 inner_args
= innermost_args (targ_ptr
);
7162 tree t
= TREE_VEC_ELT (inner_args
, i
);
7163 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
7165 tree nt
= target_type (t
);
7166 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7168 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7169 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7170 fndecl
= error_mark_node
;
7175 targ_ptr
= copy_to_permanent (targ_ptr
);
7177 /* substitute template parameters */
7178 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, /*complain=*/1, gen_tmpl
);
7179 /* The DECL_TI_TEMPLATE should always be the immediate parent
7180 template, not the most general template. */
7181 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7183 if (flag_external_templates
)
7184 add_pending_template (fndecl
);
7187 function_maybepermanent_obstack
= old_fmp_obstack
;
7193 /* Push the name of the class template into the scope of the instantiation. */
7196 overload_template_name (type
)
7199 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
7202 if (IDENTIFIER_CLASS_VALUE (id
)
7203 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
7206 decl
= build_decl (TYPE_DECL
, id
, type
);
7207 SET_DECL_ARTIFICIAL (decl
);
7208 pushdecl_class_level (decl
);
7211 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7212 arguments that are being used when calling it. TARGS is a vector
7213 into which the deduced template arguments are placed.
7215 Return zero for success, 2 for an incomplete match that doesn't resolve
7216 all the types, and 1 for complete failure. An error message will be
7217 printed only for an incomplete match.
7219 If FN is a conversion operator, RETURN_TYPE is the type desired as
7220 the result of the conversion operator.
7222 TPARMS is a vector of template parameters.
7224 The EXPLICIT_TARGS are explicit template arguments provided via a
7227 The parameter STRICT is one of:
7230 We are deducing arguments for a function call, as in
7234 We are deducing arguments for a conversion function, as in
7238 We are deducing arguments when calculating the partial
7239 ordering between specializations of function or class
7240 templates, as in [temp.func.order] and [temp.class.order],
7241 when doing an explicit instantiation as in [temp.explicit],
7242 when determining an explicit specialization as in
7243 [temp.expl.spec], or when taking the address of a function
7244 template, as in [temp.deduct.funcaddr].
7246 The other arguments are as for type_unification. */
7249 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7251 tree fn
, explicit_targs
, targs
, args
, return_type
;
7252 unification_kind_t strict
;
7257 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7259 fntype
= TREE_TYPE (fn
);
7264 The specified template arguments must match the template
7265 parameters in kind (i.e., type, nontype, template), and there
7266 must not be more arguments than there are parameters;
7267 otherwise type deduction fails.
7269 Nontype arguments must match the types of the corresponding
7270 nontype template parameters, or must be convertible to the
7271 types of the corresponding nontype parameters as specified in
7272 _temp.arg.nontype_, otherwise type deduction fails.
7274 All references in the function type of the function template
7275 to the corresponding template parameters are replaced by the
7276 specified template argument values. If a substitution in a
7277 template parameter or in the function type of the function
7278 template results in an invalid type, type deduction fails. */
7280 tree converted_args
;
7283 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7284 explicit_targs
, NULL_TREE
, /*complain=*/0,
7285 /*require_all_arguments=*/0));
7286 if (converted_args
== error_mark_node
)
7289 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7290 if (fntype
== error_mark_node
)
7293 /* Place the explicitly specified arguments in TARGS. */
7294 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7295 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7298 parms
= TYPE_ARG_TYPES (fntype
);
7300 if (DECL_CONV_FN_P (fn
))
7302 /* This is a template conversion operator. Use the return types
7303 as well as the argument types. */
7304 parms
= scratch_tree_cons (NULL_TREE
, TREE_TYPE (fntype
),
7306 args
= scratch_tree_cons (NULL_TREE
, return_type
, args
);
7309 /* We allow incomplete unification without an error message here
7310 because the standard doesn't seem to explicitly prohibit it. Our
7311 callers must be ready to deal with unification failures in any
7313 return type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7314 targs
, parms
, args
, /*subr=*/0,
7315 strict
, /*allow_incomplete*/1);
7318 /* Adjust types before performing type deduction, as described in
7319 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7320 sections are symmetric. PARM is the type of a function parameter
7321 or the return type of the conversion function. ARG is the type of
7322 the argument passed to the call, or the type of the value
7323 intialized with the result of the conversion function. */
7326 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7327 unification_kind_t strict
;
7338 /* Swap PARM and ARG throughout the remainder of this
7339 function; the handling is precisely symmetric since PARM
7340 will initialize ARG rather than vice versa. */
7348 /* There is nothing to do in this case. */
7352 my_friendly_abort (0);
7355 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7357 /* [temp.deduct.call]
7359 If P is not a reference type:
7361 --If A is an array type, the pointer type produced by the
7362 array-to-pointer standard conversion (_conv.array_) is
7363 used in place of A for type deduction; otherwise,
7365 --If A is a function type, the pointer type produced by
7366 the function-to-pointer standard conversion
7367 (_conv.func_) is used in place of A for type deduction;
7370 --If A is a cv-qualified type, the top level
7371 cv-qualifiers of A's type are ignored for type
7373 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7374 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7375 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7376 *arg
= build_pointer_type (*arg
);
7378 *arg
= TYPE_MAIN_VARIANT (*arg
);
7381 /* [temp.deduct.call]
7383 If P is a cv-qualified type, the top level cv-qualifiers
7384 of P's type are ignored for type deduction. If P is a
7385 reference type, the type referred to by P is used for
7387 *parm
= TYPE_MAIN_VARIANT (*parm
);
7388 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7389 *parm
= TREE_TYPE (*parm
);
7392 /* Like type_unfication.
7394 If SUBR is 1, we're being called recursively (to unify the
7395 arguments of a function or method parameter of a function
7399 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7400 strict
, allow_incomplete
)
7401 tree tparms
, targs
, parms
, args
;
7403 unification_kind_t strict
;
7404 int allow_incomplete
;
7408 int ntparms
= TREE_VEC_LENGTH (tparms
);
7411 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7412 my_friendly_assert (parms
== NULL_TREE
7413 || TREE_CODE (parms
) == TREE_LIST
, 290);
7414 /* ARGS could be NULL (via a call from parse.y to
7415 build_x_function_call). */
7417 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7418 my_friendly_assert (ntparms
> 0, 292);
7423 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7427 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7431 sub_strict
= UNIFY_ALLOW_NONE
;
7435 my_friendly_abort (0);
7439 && parms
!= void_list_node
7441 && args
!= void_list_node
)
7443 parm
= TREE_VALUE (parms
);
7444 parms
= TREE_CHAIN (parms
);
7445 arg
= TREE_VALUE (args
);
7446 args
= TREE_CHAIN (args
);
7448 if (arg
== error_mark_node
)
7450 if (arg
== unknown_type_node
)
7451 /* We can't deduce anything from this, but we might get all the
7452 template args from other function args. */
7455 /* Conversions will be performed on a function argument that
7456 corresponds with a function parameter that contains only
7457 non-deducible template parameters and explicitly specified
7458 template parameters. */
7459 if (! uses_template_parms (parm
))
7463 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7464 type
= TREE_TYPE (arg
);
7471 if (strict
== DEDUCE_EXACT
)
7473 if (same_type_p (parm
, type
))
7477 /* It might work; we shouldn't check now, because we might
7478 get into infinite recursion. Overload resolution will
7485 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7487 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7488 if (type_unknown_p (arg
))
7490 /* [temp.deduct.type] A template-argument can be deduced from
7491 a pointer to function or pointer to member function
7492 argument if the set of overloaded functions does not
7493 contain function templates and at most one of a set of
7494 overloaded functions provides a unique match. */
7496 if (resolve_overloaded_unification
7497 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
7502 arg
= TREE_TYPE (arg
);
7506 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7508 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
))
7516 /* Fail if we've reached the end of the parm list, and more args
7517 are present, and the parm list isn't variadic. */
7518 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7520 /* Fail if parms are left and they don't have default values. */
7522 && parms
!= void_list_node
7523 && TREE_PURPOSE (parms
) == NULL_TREE
)
7526 for (i
= 0; i
< ntparms
; i
++)
7527 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7529 if (!allow_incomplete
)
7530 error ("incomplete type unification");
7536 /* Subroutine of type_unification_real. Args are like the variables at the
7537 call site. ARG is an overloaded function (or template-id); we try
7538 deducing template args from each of the overloads, and if only one
7539 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7542 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7544 tree tparms
, targs
, parm
, arg
;
7545 unification_kind_t strict
;
7548 tree tempargs
= copy_node (targs
);
7551 if (TREE_CODE (arg
) == ADDR_EXPR
)
7552 arg
= TREE_OPERAND (arg
, 0);
7554 if (TREE_CODE (arg
) == COMPONENT_REF
)
7555 /* Handle `&x' where `x' is some static or non-static member
7557 arg
= TREE_OPERAND (arg
, 1);
7559 /* Strip baselink information. */
7560 while (TREE_CODE (arg
) == TREE_LIST
)
7561 arg
= TREE_VALUE (arg
);
7563 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7565 /* If we got some explicit template args, we need to plug them into
7566 the affected templates before we try to unify, in case the
7567 explicit args will completely resolve the templates in question. */
7569 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7570 arg
= TREE_OPERAND (arg
, 0);
7572 for (; arg
; arg
= OVL_NEXT (arg
))
7574 tree fn
= OVL_CURRENT (arg
);
7577 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7580 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
7583 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
7585 if (TREE_CODE (elem
) == METHOD_TYPE
)
7586 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7587 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7588 strict
, sub_strict
);
7592 else if (TREE_CODE (arg
) == OVERLOAD
)
7594 for (; arg
; arg
= OVL_NEXT (arg
))
7596 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
7597 if (TREE_CODE (type
) == METHOD_TYPE
)
7598 type
= build_ptrmemfunc_type (build_pointer_type (type
));
7599 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
7601 strict
, sub_strict
);
7605 my_friendly_abort (981006);
7607 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7608 to function or pointer to member function argument if the set of
7609 overloaded functions does not contain function templates and at most
7610 one of a set of overloaded functions provides a unique match.
7612 So if we found multiple possibilities, we return success but don't
7617 int i
= TREE_VEC_LENGTH (targs
);
7619 if (TREE_VEC_ELT (tempargs
, i
))
7620 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
7628 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7629 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7630 different overloads deduce different arguments for a given parm.
7631 Returns 1 on success. */
7634 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
7636 tree tparms
, orig_targs
, targs
, parm
, arg
;
7637 unification_kind_t strict
;
7644 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7645 to function or pointer to member function argument if the set of
7646 overloaded functions does not contain function templates and at most
7647 one of a set of overloaded functions provides a unique match.
7649 So if this is a template, just return success. */
7651 if (uses_template_parms (arg
))
7654 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7656 /* We don't copy orig_targs for this because if we have already deduced
7657 some template args from previous args, unify would complain when we
7658 try to deduce a template parameter for the same argument, even though
7659 there isn't really a conflict. */
7660 nargs
= TREE_VEC_LENGTH (targs
);
7661 tempargs
= make_scratch_vec (nargs
);
7663 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
7666 /* First make sure we didn't deduce anything that conflicts with
7667 explicitly specified args. */
7668 for (i
= nargs
; i
--; )
7670 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7671 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
7673 if (elt
== NULL_TREE
)
7675 else if (uses_template_parms (elt
))
7677 /* Since we're unifying against ourselves, we will fill in template
7678 args used in the function parm list with our own template parms.
7680 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
7683 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
7687 for (i
= nargs
; i
--; )
7689 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7692 TREE_VEC_ELT (targs
, i
) = elt
;
7698 /* PARM is a template class (perhaps with unbound template
7699 parameters). ARG is a fully instantiated type. If ARG can be
7700 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
7701 TARGS are as for unify. */
7704 try_class_unification (tparms
, targs
, parm
, arg
)
7713 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
7714 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
7717 /* We need to make a new template argument vector for the call to
7718 unify. If we used TARGS, we'd clutter it up with the result of
7719 the attempted unification, even if this class didn't work out.
7720 We also don't want to commit ourselves to all the unifications
7721 we've already done, since unification is supposed to be done on
7722 an argument-by-argument basis. In other words, consider the
7723 following pathological case:
7725 template <int I, int J, int K>
7728 template <int I, int J>
7729 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
7731 template <int I, int J, int K>
7732 void f(S<I, J, K>, S<I, I, I>);
7741 Now, by the time we consider the unification involving `s2', we
7742 already know that we must have `f<0, 0, 0>'. But, even though
7743 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
7744 because there are two ways to unify base classes of S<0, 1, 2>
7745 with S<I, I, I>. If we kept the already deduced knowledge, we
7746 would reject the possibility I=1. */
7748 copy_of_targs
= make_temp_vec (TREE_VEC_LENGTH (targs
));
7749 i
= unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
7750 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
);
7753 /* If unification failed, we're done. */
7760 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
7761 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
7762 for the base class of ARG that we are currently examining. */
7765 get_template_base_recursive (tparms
, targs
, parm
,
7766 arg_binfo
, rval
, flags
)
7776 tree arg
= BINFO_TYPE (arg_binfo
);
7778 if (!(flags
& GTB_IGNORE_TYPE
))
7780 tree r
= try_class_unification (tparms
, targs
,
7783 /* If there is more than one satisfactory baseclass, then:
7787 If they yield more than one possible deduced A, the type
7791 if (r
&& rval
&& !same_type_p (r
, rval
))
7792 return error_mark_node
;
7797 binfos
= BINFO_BASETYPES (arg_binfo
);
7798 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
7800 /* Process base types. */
7801 for (i
= 0; i
< n_baselinks
; i
++)
7803 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
7806 /* Skip this base, if we've already seen it. */
7807 if (BINFO_MARKED (base_binfo
))
7811 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
7813 /* When searching for a non-virtual, we cannot mark virtually
7816 SET_BINFO_MARKED (base_binfo
);
7818 rval
= get_template_base_recursive (tparms
, targs
,
7822 GTB_VIA_VIRTUAL
* this_virtual
);
7824 /* If we discovered more than one matching base class, we can
7826 if (rval
== error_mark_node
)
7827 return error_mark_node
;
7833 /* Given a template type PARM and a class type ARG, find the unique
7834 base type in ARG that is an instance of PARM. We do not examine
7835 ARG itself; only its base-classes. If there is no appropriate base
7836 class, return NULL_TREE. If there is more than one, return
7837 error_mark_node. PARM may be the type of a partial specialization,
7838 as well as a plain template type. Used by unify. */
7841 get_template_base (tparms
, targs
, parm
, arg
)
7850 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
7852 arg_binfo
= TYPE_BINFO (complete_type (arg
));
7853 rval
= get_template_base_recursive (tparms
, targs
,
7858 /* Since get_template_base_recursive marks the bases classes, we
7859 must unmark them here. */
7860 dfs_walk (arg_binfo
, dfs_unmark
, markedp
);
7865 /* Returns the level of DECL, which declares a template parameter. */
7868 template_decl_level (decl
)
7871 switch (TREE_CODE (decl
))
7875 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
7878 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
7881 my_friendly_abort (0);
7886 /* Decide whether ARG can be unified with PARM, considering only the
7887 cv-qualifiers of each type, given STRICT as documented for unify.
7888 Returns non-zero iff the unification is OK on that basis.*/
7891 check_cv_quals_for_unify (strict
, arg
, parm
)
7896 return !((!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
7897 && !at_least_as_qualified_p (arg
, parm
))
7898 || (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
7899 && (!at_least_as_qualified_p (parm
, arg
))));
7902 /* Takes parameters as for type_unification. Returns 0 if the
7903 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7904 bitwise or of the following flags:
7907 Require an exact match between PARM and ARG.
7908 UNIFY_ALLOW_MORE_CV_QUAL:
7909 Allow the deduced ARG to be more cv-qualified than ARG.
7910 UNIFY_ALLOW_LESS_CV_QUAL:
7911 Allow the deduced ARG to be less cv-qualified than ARG.
7912 UNIFY_ALLOW_DERIVED:
7913 Allow the deduced ARG to be a template base class of ARG,
7914 or a pointer to a template base class of the type pointed to by
7916 UNIFY_ALLOW_INTEGER:
7917 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7918 case for more information. */
7921 unify (tparms
, targs
, parm
, arg
, strict
)
7922 tree tparms
, targs
, parm
, arg
;
7929 /* I don't think this will do the right thing with respect to types.
7930 But the only case I've seen it in so far has been array bounds, where
7931 signedness is the only information lost, and I think that will be
7933 while (TREE_CODE (parm
) == NOP_EXPR
)
7934 parm
= TREE_OPERAND (parm
, 0);
7936 if (arg
== error_mark_node
)
7938 if (arg
== unknown_type_node
)
7939 /* We can't deduce anything from this, but we might get all the
7940 template args from other function args. */
7943 /* If PARM uses template parameters, then we can't bail out here,
7944 even if ARG == PARM, since we won't record unifications for the
7945 template parameters. We might need them if we're trying to
7946 figure out which of two things is more specialized. */
7947 if (arg
== parm
&& !uses_template_parms (parm
))
7950 /* Immediately reject some pairs that won't unify because of
7951 cv-qualification mismatches. */
7952 if (TREE_CODE (arg
) == TREE_CODE (parm
)
7953 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
7954 /* We check the cv-qualifiers when unifying with template type
7955 parameters below. We want to allow ARG `const T' to unify with
7956 PARM `T' for example, when computing which of two templates
7957 is more specialized, for example. */
7958 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
7959 && !check_cv_quals_for_unify (strict
, arg
, parm
))
7962 switch (TREE_CODE (parm
))
7965 /* In a type which contains a nested-name-specifier, template
7966 argument values cannot be deduced for template parameters used
7967 within the nested-name-specifier. */
7970 case TEMPLATE_TYPE_PARM
:
7971 case TEMPLATE_TEMPLATE_PARM
:
7972 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7974 if (TEMPLATE_TYPE_LEVEL (parm
)
7975 != template_decl_level (tparm
))
7976 /* The PARM is not one we're trying to unify. Just check
7977 to see if it matches ARG. */
7978 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7979 && same_type_p (parm
, arg
)) ? 0 : 1;
7980 idx
= TEMPLATE_TYPE_IDX (parm
);
7981 targ
= TREE_VEC_ELT (targs
, idx
);
7982 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
7984 /* Check for mixed types and values. */
7985 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
7986 && TREE_CODE (tparm
) != TYPE_DECL
)
7987 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
7988 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
7991 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
7993 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
7995 /* We arrive here when PARM does not involve template
7998 /* ARG must be constructed from a template class. */
7999 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
8003 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8004 tree parmvec
= TYPE_TI_ARGS (parm
);
8005 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
8007 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
8010 /* The parameter and argument roles have to be switched here
8011 in order to handle default arguments properly. For example,
8012 template<template <class> class TT> void f(TT<int>)
8013 should be able to accept vector<int> which comes from
8014 template <class T, class Allocator = allocator>
8017 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8021 /* Deduce arguments T, i from TT<T> or TT<i>.
8022 We check each element of PARMVEC and ARGVEC individually
8023 rather than the whole TREE_VEC since they can have
8024 different number of elements. */
8026 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8028 tree t
= TREE_VEC_ELT (parmvec
, i
);
8030 if (unify (tparms
, targs
, t
,
8031 TREE_VEC_ELT (argvec
, i
),
8036 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
8041 /* If PARM is `const T' and ARG is only `int', we don't have
8042 a match unless we are allowing additional qualification.
8043 If ARG is `const int' and PARM is just `T' that's OK;
8044 that binds `const int' to `T'. */
8045 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
8049 /* Consider the case where ARG is `const volatile int' and
8050 PARM is `const T'. Then, T should be `volatile int'. */
8052 cp_build_qualified_type (arg
,
8054 & ~CP_TYPE_QUALS (parm
));
8057 /* Simple cases: Value already set, does match or doesn't. */
8058 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8063 /* Make sure that ARG is not a variable-sized array. (Note that
8064 were talking about variable-sized arrays (like `int[n]'),
8065 rather than arrays of unknown size (like `int[]').) We'll
8066 get very confused by such a type since the bound of the array
8067 will not be computable in an instantiation. Besides, such
8068 types are not allowed in ISO C++, so we can do as we please
8070 if (TREE_CODE (arg
) == ARRAY_TYPE
8071 && !uses_template_parms (arg
)
8072 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8076 TREE_VEC_ELT (targs
, idx
) = arg
;
8079 case TEMPLATE_PARM_INDEX
:
8080 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8082 if (TEMPLATE_PARM_LEVEL (parm
)
8083 != template_decl_level (tparm
))
8084 /* The PARM is not one we're trying to unify. Just check
8085 to see if it matches ARG. */
8086 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8087 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8089 idx
= TEMPLATE_PARM_IDX (parm
);
8090 targ
= TREE_VEC_ELT (targs
, idx
);
8094 int i
= (cp_tree_equal (targ
, arg
) > 0);
8100 my_friendly_abort (42);
8103 /* [temp.deduct.type] If, in the declaration of a function template
8104 with a non-type template-parameter, the non-type
8105 template-parameter is used in an expression in the function
8106 parameter-list and, if the corresponding template-argument is
8107 deduced, the template-argument type shall match the type of the
8108 template-parameter exactly, except that a template-argument
8109 deduced from an array bound may be of any integral type. */
8110 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
8112 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8113 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8114 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8119 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
8126 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
8127 return (unify (tparms
, targs
, parm
,
8128 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
));
8130 if (TREE_CODE (arg
) != POINTER_TYPE
)
8133 /* [temp.deduct.call]
8135 A can be another pointer or pointer to member type that can
8136 be converted to the deduced A via a qualification
8137 conversion (_conv.qual_).
8139 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8140 This will allow for additional cv-qualification of the
8141 pointed-to types if appropriate. In general, this is a bit
8142 too generous; we are only supposed to allow qualification
8143 conversions and this method will allow an ARG of char** and
8144 a deduced ARG of const char**. However, overload
8145 resolution will subsequently invalidate the candidate, so
8146 this is probably OK. */
8147 sub_strict
= strict
;
8149 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
8150 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg
)))
8151 /* The derived-to-base conversion only persists through one
8152 level of pointers. */
8153 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
8155 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE
8159 case REFERENCE_TYPE
:
8160 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8162 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8166 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8168 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8169 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8171 if (TYPE_DOMAIN (parm
) != NULL_TREE
8172 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8173 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8175 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8183 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8186 if (TREE_CODE (parm
) == INTEGER_TYPE
8187 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8189 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8190 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8191 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8193 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8194 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8195 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8198 /* We use the TYPE_MAIN_VARIANT since we have already
8199 checked cv-qualification at the top of the
8201 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
8202 TYPE_MAIN_VARIANT (parm
)))
8205 /* As far as unification is concerned, this wins. Later checks
8206 will invalidate it if necessary. */
8209 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8210 /* Type INTEGER_CST can come from ordinary constant template args. */
8212 while (TREE_CODE (arg
) == NOP_EXPR
)
8213 arg
= TREE_OPERAND (arg
, 0);
8215 if (TREE_CODE (arg
) != INTEGER_CST
)
8217 return !tree_int_cst_equal (parm
, arg
);
8222 if (TREE_CODE (arg
) != TREE_VEC
)
8224 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8226 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
8227 if (unify (tparms
, targs
,
8228 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8236 if (TYPE_PTRMEMFUNC_FLAG (parm
))
8237 return unify (tparms
, targs
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8240 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8243 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8247 if (strict
& UNIFY_ALLOW_DERIVED
)
8249 /* First, we try to unify the PARM and ARG directly. */
8250 t
= try_class_unification (tparms
, targs
,
8255 /* Fallback to the special case allowed in
8258 If P is a class, and P has the form
8259 template-id, then A can be a derived class of
8260 the deduced A. Likewise, if P is a pointer to
8261 a class of the form template-id, A can be a
8262 pointer to a derived class pointed to by the
8264 t
= get_template_base (tparms
, targs
,
8267 if (! t
|| t
== error_mark_node
)
8271 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8272 && (CLASSTYPE_TI_TEMPLATE (parm
)
8273 == CLASSTYPE_TI_TEMPLATE (arg
)))
8274 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8275 Then, we should unify `int' and `U'. */
8278 /* There's no chance of unication succeeding. */
8281 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8282 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8284 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
8285 TYPE_MAIN_VARIANT (arg
)))
8291 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8294 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8295 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8297 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8298 TYPE_ARG_TYPES (arg
), 1,
8302 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8304 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8305 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8307 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8311 if (arg
!= decl_constant_value (parm
))
8316 /* Matched cases are handled by the ARG == PARM test above. */
8320 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8322 /* We handle this case specially, since it comes up with
8323 arrays. In particular, something like:
8325 template <int N> void f(int (&x)[N]);
8327 Here, we are trying to unify the range type, which
8328 looks like [0 ... (N - 1)]. */
8330 t1
= TREE_OPERAND (parm
, 0);
8331 t2
= TREE_OPERAND (parm
, 1);
8333 /* Should this be a regular fold? */
8334 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
8338 return unify (tparms
, targs
, t1
, t
, strict
);
8340 /* else fall through */
8343 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8344 /* We're looking at an expression. This can happen with
8348 void foo(S<I>, S<I + 2>);
8350 This is a "nondeduced context":
8354 The nondeduced contexts are:
8356 --A type that is a template-id in which one or more of
8357 the template-arguments is an expression that references
8358 a template-parameter.
8360 In these cases, we assume deduction succeeded, but don't
8361 actually infer any unifications. */
8364 sorry ("use of `%s' in template type unification",
8365 tree_code_name
[(int) TREE_CODE (parm
)]);
8371 /* Called if RESULT is explicitly instantiated, or is a member of an
8372 explicitly instantiated class, or if using -frepo and the
8373 instantiation of RESULT has been assigned to this file. */
8376 mark_decl_instantiated (result
, extern_p
)
8380 if (TREE_CODE (result
) != FUNCTION_DECL
)
8381 /* The TREE_PUBLIC flag for function declarations will have been
8382 set correctly by tsubst. */
8383 TREE_PUBLIC (result
) = 1;
8387 DECL_INTERFACE_KNOWN (result
) = 1;
8388 DECL_NOT_REALLY_EXTERN (result
) = 1;
8390 /* Always make artificials weak. */
8391 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8392 comdat_linkage (result
);
8393 /* For WIN32 we also want to put explicit instantiations in
8394 linkonce sections. */
8395 else if (TREE_PUBLIC (result
))
8396 maybe_make_one_only (result
);
8398 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8399 mark_inline_for_output (result
);
8402 /* Given two function templates PAT1 and PAT2, and explicit template
8403 arguments EXPLICIT_ARGS return:
8405 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8406 -1 if PAT2 is more specialized than PAT1.
8407 0 if neither is more specialized. */
8410 more_specialized (pat1
, pat2
, explicit_args
)
8411 tree pat1
, pat2
, explicit_args
;
8416 targs
= get_bindings_overload (pat1
, DECL_RESULT (pat2
), explicit_args
);
8420 targs
= get_bindings_overload (pat2
, DECL_RESULT (pat1
), explicit_args
);
8427 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8429 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8430 -1 if PAT2 is more specialized than PAT1.
8431 0 if neither is more specialized. */
8434 more_specialized_class (pat1
, pat2
)
8440 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8441 TREE_PURPOSE (pat2
));
8445 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8446 TREE_PURPOSE (pat1
));
8453 /* Return the template arguments that will produce the function signature
8454 DECL from the function template FN, with the explicit template
8455 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8456 also match. Return NULL_TREE if no satisfactory arguments could be
8460 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8461 tree fn
, decl
, explicit_args
;
8464 int ntparms
= DECL_NTPARMS (fn
);
8465 tree targs
= make_scratch_vec (ntparms
);
8467 tree decl_arg_types
;
8470 /* Substitute the explicit template arguments into the type of DECL.
8471 The call to fn_type_unification will handle substitution into the
8473 decl_type
= TREE_TYPE (decl
);
8474 if (explicit_args
&& uses_template_parms (decl_type
))
8477 tree converted_args
;
8479 if (DECL_TEMPLATE_INFO (decl
))
8480 tmpl
= DECL_TI_TEMPLATE (decl
);
8482 /* We can get here for some illegal specializations. */
8486 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
8487 explicit_args
, NULL_TREE
,
8489 /*require_all_arguments=*/0));
8490 if (converted_args
== error_mark_node
)
8493 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
8495 if (decl_type
== error_mark_node
)
8499 /* If FN is a static member function, adjust the type of DECL
8501 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
8502 if (DECL_STATIC_FUNCTION_P (fn
)
8503 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8504 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8506 i
= fn_type_unification (fn
, explicit_args
, targs
,
8508 TREE_TYPE (decl_type
),
8516 /* Check to see that the resulting return type is also OK. */
8517 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
8518 /*complain=*/0, NULL_TREE
);
8520 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
8527 /* For most uses, we want to check the return type. */
8530 get_bindings (fn
, decl
, explicit_args
)
8531 tree fn
, decl
, explicit_args
;
8533 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8536 /* But for more_specialized, we only care about the parameter types. */
8539 get_bindings_overload (fn
, decl
, explicit_args
)
8540 tree fn
, decl
, explicit_args
;
8542 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8545 /* Return the innermost template arguments that, when applied to a
8546 template specialization whose innermost template parameters are
8547 TPARMS, and whose specialization arguments are ARGS, yield the
8550 For example, suppose we have:
8552 template <class T, class U> struct S {};
8553 template <class T> struct S<T*, int> {};
8555 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8556 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8557 int}. The resulting vector will be {double}, indicating that `T'
8558 is bound to `double'. */
8561 get_class_bindings (tparms
, parms
, args
)
8562 tree tparms
, parms
, args
;
8564 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8565 tree vec
= make_temp_vec (ntparms
);
8567 args
= innermost_args (args
);
8569 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
))
8572 for (i
= 0; i
< ntparms
; ++i
)
8573 if (! TREE_VEC_ELT (vec
, i
))
8579 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8580 Pick the most specialized template, and return the corresponding
8581 instantiation, or if there is no corresponding instantiation, the
8582 template itself. EXPLICIT_ARGS is any template arguments explicity
8583 mentioned in a template-id. If there is no most specialized
8584 tempalte, error_mark_node is returned. If there are no templates
8585 at all, NULL_TREE is returned. */
8588 most_specialized_instantiation (instantiations
, explicit_args
)
8589 tree instantiations
;
8595 if (!instantiations
)
8598 champ
= instantiations
;
8599 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
8601 fate
= more_specialized (TREE_VALUE (champ
),
8602 TREE_VALUE (fn
), explicit_args
);
8609 fn
= TREE_CHAIN (fn
);
8611 return error_mark_node
;
8617 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
8619 fate
= more_specialized (TREE_VALUE (champ
),
8620 TREE_VALUE (fn
), explicit_args
);
8622 return error_mark_node
;
8625 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
8628 /* Return the most specialized of the list of templates in FNS that can
8629 produce an instantiation matching DECL, given the explicit template
8630 arguments EXPLICIT_ARGS. */
8633 most_specialized (fns
, decl
, explicit_args
)
8634 tree fns
, decl
, explicit_args
;
8636 tree candidates
= NULL_TREE
;
8639 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
8641 tree candidate
= TREE_VALUE (fn
);
8643 args
= get_bindings (candidate
, decl
, explicit_args
);
8645 candidates
= scratch_tree_cons (NULL_TREE
, candidate
,
8649 return most_specialized_instantiation (candidates
, explicit_args
);
8652 /* If DECL is a specialization of some template, return the most
8653 general such template. For example, given:
8655 template <class T> struct S { template <class U> void f(U); };
8657 if TMPL is `template <class U> void S<int>::f(U)' this will return
8658 the full template. This function will not trace past partial
8659 specializations, however. For example, given in addition:
8661 template <class T> struct S<T*> { template <class U> void f(U); };
8663 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8664 `template <class T> template <class U> S<T*>::f(U)'. */
8667 most_general_template (decl
)
8670 while (DECL_TEMPLATE_INFO (decl
))
8671 decl
= DECL_TI_TEMPLATE (decl
);
8676 /* Return the most specialized of the class template specializations
8677 of TMPL which can produce an instantiation matching ARGS, or
8678 error_mark_node if the choice is ambiguous. */
8681 most_specialized_class (tmpl
, args
)
8685 tree list
= NULL_TREE
;
8690 tmpl
= most_general_template (tmpl
);
8691 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
8694 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
8697 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
8698 TREE_TYPE (list
) = TREE_TYPE (t
);
8708 for (; t
; t
= TREE_CHAIN (t
))
8710 fate
= more_specialized_class (champ
, t
);
8719 return error_mark_node
;
8725 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
8727 fate
= more_specialized_class (champ
, t
);
8729 return error_mark_node
;
8735 /* called from the parser. */
8738 do_decl_instantiation (declspecs
, declarator
, storage
)
8739 tree declspecs
, declarator
, storage
;
8741 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
8742 tree result
= NULL_TREE
;
8745 if (! DECL_LANG_SPECIFIC (decl
))
8747 cp_error ("explicit instantiation of non-template `%#D'", decl
);
8750 else if (TREE_CODE (decl
) == VAR_DECL
)
8752 /* There is an asymmetry here in the way VAR_DECLs and
8753 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8754 the latter, the DECL we get back will be marked as a
8755 template instantiation, and the appropriate
8756 DECL_TEMPLATE_INFO will be set up. This does not happen for
8757 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8758 should handle VAR_DECLs as it currently handles
8760 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
8761 if (result
&& TREE_CODE (result
) != VAR_DECL
)
8763 cp_error ("no matching template for `%D' found", result
);
8767 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
8769 cp_error ("explicit instantiation of `%#D'", decl
);
8775 /* Check for various error cases. Note that if the explicit
8776 instantiation is legal the RESULT will currently be marked as an
8777 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8778 until we get here. */
8780 if (DECL_TEMPLATE_SPECIALIZATION (result
))
8784 No program shall both explicitly instantiate and explicitly
8785 specialize a template. */
8786 cp_error ("explicit instantiation of `%#D' after", result
);
8787 cp_error_at ("explicit specialization here", result
);
8790 else if (DECL_EXPLICIT_INSTANTIATION (result
))
8794 No program shall explicitly instantiate any template more
8797 We check DECL_INTERFACE_KNOWN so as not to complain when the
8798 first instantiation was `extern' and the second is not, and
8799 EXTERN_P for the opposite case. */
8800 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
)
8801 cp_error ("duplicate explicit instantiation of `%#D'", result
);
8803 /* If we've already instantiated the template, just return now. */
8804 if (DECL_INTERFACE_KNOWN (result
))
8807 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
8809 cp_error ("no matching template for `%D' found", result
);
8812 else if (!DECL_TEMPLATE_INFO (result
))
8814 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
8818 if (flag_external_templates
)
8821 if (storage
== NULL_TREE
)
8823 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8826 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
8830 cp_error ("storage class `%D' applied to template instantiation",
8833 SET_DECL_EXPLICIT_INSTANTIATION (result
);
8834 mark_decl_instantiated (result
, extern_p
);
8835 repo_template_instantiated (result
, extern_p
);
8837 instantiate_decl (result
);
8841 mark_class_instantiated (t
, extern_p
)
8845 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
8846 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
8847 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
8848 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
8849 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
8852 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
8853 rest_of_type_compilation (t
, 1);
8858 do_type_instantiation (t
, storage
)
8865 if (TREE_CODE (t
) == TYPE_DECL
)
8868 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
8870 cp_error ("explicit instantiation of non-template type `%T'", t
);
8876 /* With -fexternal-templates, explicit instantiations are treated the same
8877 as implicit ones. */
8878 if (flag_external_templates
)
8881 if (TYPE_SIZE (t
) == NULL_TREE
)
8883 cp_error ("explicit instantiation of `%#T' before definition of template",
8888 if (storage
!= NULL_TREE
)
8891 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8892 IDENTIFIER_POINTER (storage
));
8894 if (storage
== ridpointers
[(int) RID_INLINE
])
8896 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8898 else if (storage
== ridpointers
[(int) RID_STATIC
])
8902 cp_error ("storage class `%D' applied to template instantiation",
8908 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
8912 No program shall both explicitly instantiate and explicitly
8913 specialize a template. */
8914 cp_error ("explicit instantiation of `%#T' after", t
);
8915 cp_error_at ("explicit specialization here", t
);
8918 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
8922 No program shall explicitly instantiate any template more
8925 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8926 instantiation was `extern', and if EXTERN_P then the second
8927 is. Both cases are OK. */
8928 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
)
8929 cp_error ("duplicate explicit instantiation of `%#T'", t
);
8931 /* If we've already instantiated the template, just return now. */
8932 if (!CLASSTYPE_INTERFACE_ONLY (t
))
8936 mark_class_instantiated (t
, extern_p
);
8937 repo_template_instantiated (t
, extern_p
);
8945 /* In contrast to implicit instantiation, where only the
8946 declarations, and not the definitions, of members are
8947 instantiated, we have here:
8951 The explicit instantiation of a class template specialization
8952 implies the instantiation of all of its members not
8953 previously explicitly specialized in the translation unit
8954 containing the explicit instantiation.
8956 Of course, we can't instantiate member template classes, since
8957 we don't have any arguments for them. Note that the standard
8958 is unclear on whether the instatiation of the members are
8959 *explicit* instantiations or not. We choose to be generous,
8960 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8961 the explicit instantiation of a class where some of the members
8962 have no definition in the current translation unit. */
8965 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8966 if (TREE_CODE (tmp
) == FUNCTION_DECL
8967 && DECL_TEMPLATE_INSTANTIATION (tmp
))
8969 mark_decl_instantiated (tmp
, extern_p
);
8970 repo_template_instantiated (tmp
, extern_p
);
8972 instantiate_decl (tmp
);
8975 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8976 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
8978 mark_decl_instantiated (tmp
, extern_p
);
8979 repo_template_instantiated (tmp
, extern_p
);
8981 instantiate_decl (tmp
);
8984 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8985 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
8986 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
8987 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
8991 /* Given a function DECL, which is a specialization of TMPL, modify
8992 DECL to be a re-instantiation of TMPL with the same template
8993 arguments. TMPL should be the template into which tsubst'ing
8994 should occur for DECL, not the most general template.
8996 One reason for doing this is a scenario like this:
8999 void f(const T&, int i);
9001 void g() { f(3, 7); }
9004 void f(const T& t, const int i) { }
9006 Note that when the template is first instantiated, with
9007 instantiate_template, the resulting DECL will have no name for the
9008 first parameter, and the wrong type for the second. So, when we go
9009 to instantiate the DECL, we regenerate it. */
9012 regenerate_decl_from_template (decl
, tmpl
)
9022 args
= DECL_TI_ARGS (decl
);
9023 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9025 /* Unregister the specialization so that when we tsubst we will not
9026 just return DECL. We don't have to unregister DECL from TMPL
9027 because if would only be registered there if it were a partial
9028 instantiation of a specialization, which it isn't: it's a full
9030 gen_tmpl
= most_general_template (tmpl
);
9031 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9033 /* If the DECL was not unregistered then something peculiar is
9034 happening: we created a specialization but did not call
9035 register_specialization for it. */
9036 my_friendly_assert (unregistered
, 0);
9038 if (TREE_CODE (decl
) == VAR_DECL
)
9039 /* Make sure that we can see identifiers, and compute access
9040 correctly, for the class members used in the declaration of
9041 this static variable. */
9042 pushclass (DECL_CONTEXT (decl
), 2);
9044 /* Do the substitution to get the new declaration. */
9045 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9047 if (TREE_CODE (decl
) == VAR_DECL
)
9049 /* Set up DECL_INITIAL, since tsubst doesn't. */
9050 DECL_INITIAL (new_decl
) =
9051 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9052 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9053 /* Pop the class context we pushed above. */
9056 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9058 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9060 DECL_INITIAL (new_decl
) = error_mark_node
;
9061 /* And don't complain about a duplicate definition. */
9062 DECL_INITIAL (decl
) = NULL_TREE
;
9065 /* The immediate parent of the new template is still whatever it was
9066 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9067 general template. We also reset the DECL_ASSEMBLER_NAME since
9068 tsubst always calculates the name as if the function in question
9069 were really a template instance, and sometimes, with friend
9070 functions, this is not so. See tsubst_friend_function for
9072 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9073 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9074 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9076 /* Call duplicate decls to merge the old and new declarations. */
9077 duplicate_decls (new_decl
, decl
);
9079 /* Now, re-register the specialization. */
9080 register_specialization (decl
, gen_tmpl
, args
);
9083 /* Produce the definition of D, a _DECL generated from a template. */
9086 instantiate_decl (d
)
9089 tree tmpl
= DECL_TI_TEMPLATE (d
);
9090 tree args
= DECL_TI_ARGS (d
);
9095 int nested
= in_function_p ();
9096 int pattern_defined
;
9098 char *file
= input_filename
;
9100 /* This function should only be used to instantiate templates for
9101 functions and static member variables. */
9102 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9103 || TREE_CODE (d
) == VAR_DECL
, 0);
9105 if (DECL_TEMPLATE_INSTANTIATED (d
))
9106 /* D has already been instantiated. It might seem reasonable to
9107 check whether or not D is an explict instantiation, and, if so,
9108 stop here. But when an explicit instantiation is deferred
9109 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9110 is set, even though we still need to do the instantiation. */
9113 /* If we already have a specialization of this declaration, then
9114 there's no reason to instantiate it. Note that
9115 retrieve_specialization gives us both instantiations and
9116 specializations, so we must explicitly check
9117 DECL_TEMPLATE_SPECIALIZATION. */
9118 gen_tmpl
= most_general_template (tmpl
);
9119 spec
= retrieve_specialization (gen_tmpl
, args
);
9120 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9123 /* This needs to happen before any tsubsting. */
9124 if (! push_tinst_level (d
))
9127 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9128 for the instantiation. This is not always the most general
9129 template. Consider, for example:
9132 struct S { template <class U> void f();
9133 template <> void f<int>(); };
9135 and an instantiation of S<double>::f<int>. We want TD to be the
9136 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9139 /* An instantiation cannot have a definition, so we need a
9140 more general template. */
9141 DECL_TEMPLATE_INSTANTIATION (td
)
9142 /* We must also deal with friend templates. Given:
9144 template <class T> struct S {
9145 template <class U> friend void f() {};
9148 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9149 so far as the language is concerned, but that's still
9150 where we get the pattern for the instantiation from. On
9151 ther hand, if the definition comes outside the class, say:
9153 template <class T> struct S {
9154 template <class U> friend void f();
9156 template <class U> friend void f() {}
9158 we don't need to look any further. That's what the check for
9159 DECL_INITIAL is for. */
9160 || (TREE_CODE (d
) == FUNCTION_DECL
9161 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9162 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
9165 /* The present template, TD, should not be a definition. If it
9166 were a definition, we should be using it! Note that we
9167 cannot restructure the loop to just keep going until we find
9168 a template with a definition, since that might go too far if
9169 a specialization was declared, but not defined. */
9170 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9171 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9174 /* Fetch the more general template. */
9175 td
= DECL_TI_TEMPLATE (td
);
9178 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9180 if (TREE_CODE (d
) == FUNCTION_DECL
)
9181 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
9183 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9185 push_to_top_level ();
9186 lineno
= DECL_SOURCE_LINE (d
);
9187 input_filename
= DECL_SOURCE_FILE (d
);
9189 if (pattern_defined
)
9191 repo_template_used (d
);
9193 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9195 if (flag_alt_external_templates
)
9197 if (interface_unknown
)
9198 warn_if_unknown_interface (d
);
9200 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9202 DECL_INTERFACE_KNOWN (d
) = 1;
9203 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9206 warn_if_unknown_interface (code_pattern
);
9210 import_export_decl (d
);
9213 /* Reject all external templates except inline functions. */
9214 if (DECL_INTERFACE_KNOWN (d
)
9215 && ! DECL_NOT_REALLY_EXTERN (d
)
9216 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9219 if (TREE_CODE (d
) == VAR_DECL
9220 && TREE_READONLY (d
)
9221 && DECL_INITIAL (d
) == NULL_TREE
9222 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
9223 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9224 the variable is a static const initialized in the class body. */;
9225 else if (! pattern_defined
9226 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
9229 /* Defer all templates except inline functions used in another
9230 function. We restore the source position here because it's used
9231 by add_pending_template. */
9233 input_filename
= file
;
9235 if (at_eof
&& !pattern_defined
9236 && DECL_EXPLICIT_INSTANTIATION (d
))
9239 The definition of a non-exported function template, a
9240 non-exported member function template, or a non-exported
9241 member function or static data member of a class template
9242 shall be present in every translation unit in which it is
9243 explicitly instantiated. */
9244 cp_error ("explicit instantiation of `%D' but no definition available",
9247 add_pending_template (d
);
9251 /* We're now committed to instantiating this template. Mark it as
9252 instantiated so that recursive calls to instantiate_decl do not
9253 try to instantiate it again. */
9254 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9256 /* Regenerate the declaration in case the template has been modified
9257 by a subsequent redeclaration. */
9258 regenerate_decl_from_template (d
, td
);
9260 /* We already set the file and line above. Reset them now in case
9261 they changed as a result of calling regenerate_decl_from_template. */
9262 lineno
= DECL_SOURCE_LINE (d
);
9263 input_filename
= DECL_SOURCE_FILE (d
);
9265 if (TREE_CODE (d
) == VAR_DECL
)
9267 DECL_IN_AGGR_P (d
) = 0;
9268 if (DECL_INTERFACE_KNOWN (d
))
9269 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9272 DECL_EXTERNAL (d
) = 1;
9273 DECL_NOT_REALLY_EXTERN (d
) = 1;
9275 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
9277 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9279 tree t
= DECL_SAVED_TREE (code_pattern
);
9281 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
9282 store_parm_decls ();
9284 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
9287 (TREE_OPERAND (t
, 0),
9288 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, tmpl
));
9292 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
9294 current_member_init_list
9295 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
9296 current_base_init_list
9297 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
9302 /* Always keep the BLOCK node associated with the outermost
9303 pair of curly braces of a function. These are needed
9304 for correct operation of dwarfout.c. */
9307 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
9308 tsubst_expr (t
, args
, /*complain=*/1, tmpl
);
9310 finish_function (lineno
, 0, nested
);
9315 input_filename
= file
;
9317 pop_from_top_level ();
9323 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9324 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9325 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9328 tsubst_expr_values (t
, argvec
)
9331 tree first
= NULL_TREE
;
9334 for (; t
; t
= TREE_CHAIN (t
))
9336 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
9337 /*complain=*/1, NULL_TREE
);
9338 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
9340 *p
= build_tree_list (pur
, val
);
9341 p
= &TREE_CHAIN (*p
);
9352 last_tree
= TREE_CHAIN (last_tree
) = t
;
9359 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
9360 last_tree
= NULL_TREE
;
9367 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
9369 last_tree
= TREE_VALUE (saved_trees
);
9370 saved_trees
= TREE_CHAIN (saved_trees
);
9373 /* D is an undefined function declaration in the presence of templates with
9374 the same name, listed in FNS. If one of them can produce D as an
9375 instantiation, remember this so we can instantiate it at EOF if D has
9376 not been defined by that time. */
9379 add_maybe_template (d
, fns
)
9384 if (DECL_MAYBE_TEMPLATE (d
))
9387 t
= most_specialized (fns
, d
, NULL_TREE
);
9390 if (t
== error_mark_node
)
9392 cp_error ("ambiguous template instantiation for `%D'", d
);
9396 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
9397 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
9398 DECL_MAYBE_TEMPLATE (d
) = 1;
9401 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9404 set_current_access_from_decl (decl
)
9407 if (TREE_PRIVATE (decl
))
9408 current_access_specifier
= access_private_node
;
9409 else if (TREE_PROTECTED (decl
))
9410 current_access_specifier
= access_protected_node
;
9412 current_access_specifier
= access_public_node
;
9415 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9416 is the instantiation (which should have been created with
9417 start_enum) and ARGS are the template arguments to use. */
9420 tsubst_enum (tag
, newtag
, args
)
9427 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9432 /* Note that in a template enum, the TREE_VALUE is the
9433 CONST_DECL, not the corresponding INTEGER_CST. */
9434 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9435 args
, /*complain=*/1,
9438 /* Give this enumeration constant the correct access. */
9439 set_current_access_from_decl (TREE_VALUE (e
));
9441 /* Actually build the enumerator itself. */
9442 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9444 /* We save the enumerators we have built so far in the
9445 TYPE_VALUES so that if the enumeration constants for
9446 subsequent enumerators involve those for previous ones,
9447 tsubst_copy will be able to find them. */
9448 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
9449 TYPE_VALUES (newtag
) = elt
;
9452 finish_enum (newtag
);
9455 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9456 is either an instantiation or specialization of a template
9460 set_mangled_name_for_template_decl (decl
)
9463 tree saved_namespace
;
9464 tree context
= NULL_TREE
;
9473 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
9474 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
9476 /* The names of template functions must be mangled so as to indicate
9477 what template is being specialized with what template arguments.
9478 For example, each of the following three functions must get
9479 different mangled names:
9482 template <> void f<7>(int);
9483 template <> void f<8>(int); */
9485 targs
= DECL_TI_ARGS (decl
);
9486 if (uses_template_parms (targs
))
9487 /* This DECL is for a partial instantiation. There's no need to
9488 mangle the name of such an entity. */
9491 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9492 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
9493 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
9495 /* There should be as many levels of arguments as there are levels
9497 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
9499 /* We now compute the PARMS and RET_TYPE to give to
9500 build_decl_overload_real. The PARMS and RET_TYPE are the
9501 parameter and return types of the template, after all but the
9502 innermost template arguments have been substituted, not the
9503 parameter and return types of the function DECL. For example,
9506 template <class T> T f(T);
9508 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
9509 A more subtle example is:
9511 template <class T> struct S { template <class U> void f(T, U); }
9513 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
9514 {int, U}. Thus, the args that we want to subsitute into the
9515 return and parameter type for the function are those in TARGS,
9516 with the innermost level omitted. */
9517 fn_type
= TREE_TYPE (tmpl
);
9518 if (DECL_STATIC_FUNCTION_P (decl
))
9519 context
= DECL_CLASS_CONTEXT (decl
);
9521 if (parm_depth
== 1)
9522 /* No substitution is necessary. */
9529 /* Replace the innermost level of the TARGS with NULL_TREEs to
9530 let tsubst know not to subsitute for those parameters. */
9531 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
9532 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
9533 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
9534 TMPL_ARGS_LEVEL (targs
, i
));
9535 SET_TMPL_ARGS_LEVEL (partial_args
,
9536 TMPL_ARGS_DEPTH (targs
),
9537 make_temp_vec (DECL_NTPARMS (tmpl
)));
9539 /* Now, do the (partial) substitution to figure out the
9540 appropriate function type. */
9541 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
9542 if (DECL_STATIC_FUNCTION_P (decl
))
9543 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
9545 /* Substitute into the template parameters to obtain the real
9546 innermost set of parameters. This step is important if the
9547 innermost set of template parameters contains value
9548 parameters whose types depend on outer template parameters. */
9549 TREE_VEC_LENGTH (partial_args
)--;
9550 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
9553 /* Now, get the innermost parameters and arguments, and figure out
9554 the parameter and return types. */
9555 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
9556 targs
= innermost_args (targs
);
9557 ret_type
= TREE_TYPE (fn_type
);
9558 parm_types
= TYPE_ARG_TYPES (fn_type
);
9560 /* For a static member function, we generate a fake `this' pointer,
9561 for the purposes of mangling. This indicates of which class the
9562 function is a member. Because of:
9566 There shall not be a static and a nonstatic member function
9567 with the same name and the same parameter types
9569 we don't have to worry that this will result in a clash with a
9570 non-static member function. */
9571 if (DECL_STATIC_FUNCTION_P (decl
))
9572 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
9574 /* There should be the same number of template parameters as
9575 template arguments. */
9576 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
9579 /* If the template is in a namespace, we need to put that into the
9580 mangled name. Unfortunately, build_decl_overload_real does not
9581 get the decl to mangle, so it relies on the current
9582 namespace. Therefore, we set that here temporarily. */
9583 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
9584 saved_namespace
= current_namespace
;
9585 current_namespace
= CP_DECL_CONTEXT (decl
);
9587 /* Actually set the DCL_ASSEMBLER_NAME. */
9588 DECL_ASSEMBLER_NAME (decl
)
9589 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
9591 DECL_FUNCTION_MEMBER_P (decl
)
9592 + DECL_CONSTRUCTOR_P (decl
));
9594 /* Restore the previously active namespace. */
9595 current_namespace
= saved_namespace
;