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,
151 static tree determine_specialization
PROTO((tree
, tree
, tree
*, int));
152 static int template_args_equal
PROTO((tree
, tree
));
153 static void print_template_context
PROTO((int));
155 /* We use TREE_VECs to hold template arguments. If there is only one
156 level of template arguments, then the TREE_VEC contains the
157 arguments directly. If there is more than one level of template
158 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
159 containing the template arguments for a single level. The first
160 entry in the outer TREE_VEC is the outermost level of template
161 parameters; the last is the innermost.
163 It is incorrect to ever form a template argument vector containing
164 only one level of arguments, but which is a TREE_VEC containing as
165 its only entry the TREE_VEC for that level. */
167 /* Non-zero if the template arguments is actually a vector of vectors,
168 rather than just a vector. */
169 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
171 && TREE_CODE (NODE) == TREE_VEC \
172 && TREE_VEC_LENGTH (NODE) > 0 \
173 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
174 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
176 /* The depth of a template argument vector. When called directly by
177 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
178 template arguments. In fact, we may even see NULL_TREE if there
179 are no template arguments. In both of those cases, there is only
180 one level of template arguments. */
181 #define TMPL_ARGS_DEPTH(NODE) \
182 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
184 /* The LEVELth level of the template ARGS. Note that template
185 parameter levels are indexed from 1, not from 0. */
186 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
187 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
188 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
190 /* Set the LEVELth level of the template ARGS to VAL. This macro does
191 not work with single-level argument vectors. */
192 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
193 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
195 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
196 #define TMPL_ARG(ARGS, LEVEL, IDX) \
197 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
199 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
200 macro does not work with single-level argument vectors. */
201 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
202 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
204 /* Given a single level of template arguments in NODE, return the
205 number of arguments. */
206 #define NUM_TMPL_ARGS(NODE) \
207 ((NODE) == NULL_TREE ? 0 \
208 : (TREE_CODE (NODE) == TREE_VEC \
209 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
211 /* The number of levels of template parameters given by NODE. */
212 #define TMPL_PARMS_DEPTH(NODE) \
213 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
215 /* Do any processing required when DECL (a member template declaration
216 using TEMPLATE_PARAMETERS as its innermost parameter list) is
217 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
218 it is a specialization, in which case the DECL itself is returned. */
221 finish_member_template_decl (decl
)
224 if (decl
== NULL_TREE
|| decl
== void_type_node
)
226 else if (decl
== error_mark_node
)
227 /* By returning NULL_TREE, the parser will just ignore this
228 declaration. We have already issued the error. */
230 else if (TREE_CODE (decl
) == TREE_LIST
)
232 /* Assume that the class is the only declspec. */
233 decl
= TREE_VALUE (decl
);
234 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
235 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
237 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
238 check_member_template (tmpl
);
243 else if (DECL_TEMPLATE_INFO (decl
))
245 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
247 check_member_template (DECL_TI_TEMPLATE (decl
));
248 return DECL_TI_TEMPLATE (decl
);
254 cp_error ("invalid member template declaration `%D'", decl
);
256 return error_mark_node
;
259 /* Returns the template nesting level of the indicated class TYPE.
269 A<T>::B<U> has depth two, while A<T> has depth one.
270 Both A<T>::B<int> and A<int>::B<U> have depth one, if
271 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
274 This function is guaranteed to return 0 if passed NULL_TREE so
275 that, for example, `template_class_depth (current_class_type)' is
279 template_class_depth_real (type
, count_specializations
)
281 int count_specializations
;
286 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
287 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
288 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
290 if (TREE_CODE (type
) != FUNCTION_DECL
)
292 if (CLASSTYPE_TEMPLATE_INFO (type
)
293 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
294 && ((count_specializations
295 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
296 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
301 if (DECL_TEMPLATE_INFO (type
)
302 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
303 && ((count_specializations
304 && DECL_TEMPLATE_SPECIALIZATION (type
))
305 || uses_template_parms (DECL_TI_ARGS (type
))))
313 /* Returns the template nesting level of the indicated class TYPE.
314 Like template_class_depth_real, but instantiations do not count in
318 template_class_depth (type
)
321 return template_class_depth_real (type
, /*count_specializations=*/0);
324 /* Returns 1 if processing DECL as part of do_pending_inlines
325 needs us to push template parms. */
328 inline_needs_template_parms (decl
)
331 if (! DECL_TEMPLATE_INFO (decl
))
334 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
335 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
338 /* Subroutine of maybe_begin_member_template_processing.
339 Push the template parms in PARMS, starting from LEVELS steps into the
340 chain, and ending at the beginning, since template parms are listed
344 push_inline_template_parms_recursive (parmlist
, levels
)
348 tree parms
= TREE_VALUE (parmlist
);
352 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
354 ++processing_template_decl
;
355 current_template_parms
356 = tree_cons (build_int_2 (0, processing_template_decl
),
357 parms
, current_template_parms
);
358 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
361 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
363 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
364 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
366 switch (TREE_CODE (parm
))
375 /* Make a CONST_DECL as is done in process_template_parm.
376 It is ugly that we recreate this here; the original
377 version built in process_template_parm is no longer
379 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
381 SET_DECL_ARTIFICIAL (decl
);
382 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
383 DECL_TEMPLATE_PARM_P (decl
) = 1;
389 my_friendly_abort (0);
394 /* Restore the template parameter context for a member template or
395 a friend template defined in a class definition. */
398 maybe_begin_member_template_processing (decl
)
404 if (! inline_needs_template_parms (decl
))
407 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
409 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
411 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
414 parms
= TREE_CHAIN (parms
);
417 push_inline_template_parms_recursive (parms
, levels
);
420 /* Undo the effects of begin_member_template_processing. */
423 maybe_end_member_template_processing ()
425 if (! processing_template_decl
)
428 while (current_template_parms
429 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
431 --processing_template_decl
;
432 current_template_parms
= TREE_CHAIN (current_template_parms
);
437 /* Returns non-zero iff T is a member template function. We must be
440 template <class T> class C { void f(); }
442 Here, f is a template function, and a member, but not a member
443 template. This function does not concern itself with the origin of
444 T, only its present state. So if we have
446 template <class T> class C { template <class U> void f(U); }
448 then neither C<int>::f<char> nor C<T>::f<double> is considered
449 to be a member template. But, `template <class U> void
450 C<int>::f(U)' is considered a member template. */
453 is_member_template (t
)
456 if (!DECL_FUNCTION_TEMPLATE_P (t
))
457 /* Anything that isn't a function or a template function is
458 certainly not a member template. */
461 /* A local class can't have member templates. */
462 if (hack_decl_function_context (t
))
465 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
466 /* If there are more levels of template parameters than
467 there are template classes surrounding the declaration,
468 then we have a member template. */
469 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
470 template_class_depth (DECL_CLASS_CONTEXT (t
))));
474 /* Returns non-zero iff T is a member template class. See
475 is_member_template for a description of what precisely constitutes
476 a member template. */
479 is_member_template_class (t
)
482 if (!DECL_CLASS_TEMPLATE_P (t
))
483 /* Anything that isn't a class template, is certainly not a member
487 if (!DECL_CLASS_SCOPE_P (t
))
488 /* Anything whose context isn't a class type is surely not a
492 /* If there are more levels of template parameters than there are
493 template classes surrounding the declaration, then we have a
495 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
496 template_class_depth (DECL_CONTEXT (t
)));
500 /* Return a new template argument vector which contains all of ARGS,
501 but has as its innermost set of arguments the EXTRA_ARGS. The
502 resulting vector will be built on a temporary obstack, and so must
503 be explicitly copied to the permanent obstack, if required. */
506 add_to_template_args (args
, extra_args
)
515 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
516 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
518 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
519 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
521 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
522 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
527 /* Like add_to_template_args, but only the outermost ARGS are added to
528 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
529 (EXTRA_ARGS) levels are added. This function is used to combine
530 the template arguments from a partial instantiation with the
531 template arguments used to attain the full instantiation from the
532 partial instantiation. */
535 add_outermost_template_args (args
, extra_args
)
541 /* If there are more levels of EXTRA_ARGS than there are ARGS,
542 something very fishy is going on. */
543 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
546 /* If *all* the new arguments will be the EXTRA_ARGS, just return
548 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
551 /* For the moment, we make ARGS look like it contains fewer levels. */
552 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
554 new_args
= add_to_template_args (args
, extra_args
);
556 /* Now, we restore ARGS to its full dimensions. */
557 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
562 /* We've got a template header coming up; push to a new level for storing
566 begin_template_parm_list ()
568 /* We use a non-tag-transparent scope here, which causes pushtag to
569 put tags in this scope, rather than in the enclosing class or
570 namespace scope. This is the right thing, since we want
571 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
572 global template class, push_template_decl handles putting the
573 TEMPLATE_DECL into top-level scope. For a nested template class,
576 template <class T> struct S1 {
577 template <class T> struct S2 {};
580 pushtag contains special code to call pushdecl_with_scope on the
581 TEMPLATE_DECL for S2. */
583 declare_pseudo_global_level ();
584 ++processing_template_decl
;
585 ++processing_template_parmlist
;
586 note_template_header (0);
589 /* This routine is called when a specialization is declared. If it is
590 illegal to declare a specialization here, an error is reported. */
593 check_specialization_scope ()
595 tree scope
= current_scope ();
599 An explicit specialization shall be declared in the namespace of
600 which the template is a member, or, for member templates, in the
601 namespace of which the enclosing class or enclosing class
602 template is a member. An explicit specialization of a member
603 function, member class or static data member of a class template
604 shall be declared in the namespace of which the class template
606 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
607 cp_error ("explicit specialization in non-namespace scope `%D'",
612 In an explicit specialization declaration for a member of a class
613 template or a member template that appears in namespace scope,
614 the member template and some of its enclosing class templates may
615 remain unspecialized, except that the declaration shall not
616 explicitly specialize a class member template if its enclosing
617 class templates are not explicitly specialized as well. */
618 if (current_template_parms
)
619 cp_error ("enclosing class templates are not explicitly specialized");
622 /* We've just seen template <>. */
625 begin_specialization ()
627 note_template_header (1);
628 check_specialization_scope ();
631 /* Called at then end of processing a declaration preceeded by
635 end_specialization ()
637 reset_specialization ();
640 /* Any template <>'s that we have seen thus far are not referring to a
641 function specialization. */
644 reset_specialization ()
646 processing_specialization
= 0;
647 template_header_count
= 0;
650 /* We've just seen a template header. If SPECIALIZATION is non-zero,
651 it was of the form template <>. */
654 note_template_header (specialization
)
657 processing_specialization
= specialization
;
658 template_header_count
++;
661 /* We're beginning an explicit instantiation. */
664 begin_explicit_instantiation ()
666 ++processing_explicit_instantiation
;
671 end_explicit_instantiation ()
673 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
674 --processing_explicit_instantiation
;
677 /* The TYPE is being declared. If it is a template type, that means it
678 is a partial specialization. Do appropriate error-checking. */
681 maybe_process_partial_specialization (type
)
684 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
686 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
687 && TYPE_SIZE (type
) == NULL_TREE
)
689 if (current_namespace
690 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
692 cp_pedwarn ("specializing `%#T' in different namespace", type
);
693 cp_pedwarn_at (" from definition of `%#D'",
694 CLASSTYPE_TI_TEMPLATE (type
));
696 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
697 if (processing_template_decl
)
698 push_template_decl (TYPE_MAIN_DECL (type
));
700 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
701 cp_error ("specialization of `%T' after instantiation", type
);
703 else if (processing_specialization
)
704 cp_error ("explicit specialization of non-template `%T'", type
);
707 /* Retrieve the specialization (in the sense of [temp.spec] - a
708 specialization is either an instantiation or an explicit
709 specialization) of TMPL for the given template ARGS. If there is
710 no such specialization, return NULL_TREE. The ARGS are a vector of
711 arguments, or a vector of vectors of arguments, in the case of
712 templates with more than one level of parameters. */
715 retrieve_specialization (tmpl
, args
)
721 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
723 /* There should be as many levels of arguments as there are
724 levels of parameters. */
725 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
726 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
729 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
732 if (comp_template_args (TREE_PURPOSE (s
), args
))
733 return TREE_VALUE (s
);
738 /* Returns non-zero iff DECL is a specialization of TMPL. */
741 is_specialization_of (decl
, tmpl
)
747 if (TREE_CODE (decl
) == FUNCTION_DECL
)
751 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
757 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
759 for (t
= TREE_TYPE (decl
);
761 t
= CLASSTYPE_USE_TEMPLATE (t
)
762 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
763 if (same_type_p (TYPE_MAIN_VARIANT (t
),
764 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
771 /* Register the specialization SPEC as a specialization of TMPL with
772 the indicated ARGS. Returns SPEC, or an equivalent prior
773 declaration, if available. */
776 register_specialization (spec
, tmpl
, args
)
783 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
785 if (TREE_CODE (spec
) == FUNCTION_DECL
786 && uses_template_parms (DECL_TI_ARGS (spec
)))
787 /* This is the FUNCTION_DECL for a partial instantiation. Don't
788 register it; we want the corresponding TEMPLATE_DECL instead.
789 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
790 the more obvious `uses_template_parms (spec)' to avoid problems
791 with default function arguments. In particular, given
794 template <class T> void f(T t1, T t = T())
796 the default argument expression is not substituted for in an
797 instantiation unless and until it is actually needed. */
800 /* There should be as many levels of arguments as there are
801 levels of parameters. */
802 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
803 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
806 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
809 if (comp_template_args (TREE_PURPOSE (s
), args
))
811 tree fn
= TREE_VALUE (s
);
813 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
815 if (DECL_TEMPLATE_INSTANTIATION (fn
))
818 || DECL_EXPLICIT_INSTANTIATION (fn
))
820 cp_error ("specialization of %D after instantiation",
826 /* This situation should occur only if the first
827 specialization is an implicit instantiation,
828 the second is an explicit specialization, and
829 the implicit instantiation has not yet been
830 used. That situation can occur if we have
831 implicitly instantiated a member function and
832 then specialized it later.
834 We can also wind up here if a friend
835 declaration that looked like an instantiation
836 turns out to be a specialization:
838 template <class T> void foo(T);
839 class S { friend void foo<>(int) };
840 template <> void foo(int);
842 We transform the existing DECL in place so that
843 any pointers to it become pointers to the
846 If there was a definition for the template, but
847 not for the specialization, we want this to
848 look as if there is no definition, and vice
850 DECL_INITIAL (fn
) = NULL_TREE
;
851 duplicate_decls (spec
, fn
);
856 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
858 duplicate_decls (spec
, fn
);
864 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
865 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
870 /* Unregister the specialization SPEC as a specialization of TMPL.
871 Returns nonzero if the SPEC was listed as a specialization of
875 unregister_specialization (spec
, tmpl
)
881 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
883 s
= &TREE_CHAIN (*s
))
884 if (TREE_VALUE (*s
) == spec
)
886 *s
= TREE_CHAIN (*s
);
893 /* Print the list of candidate FNS in an error message. */
896 print_candidates (fns
)
901 const char *str
= "candidates are:";
903 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
907 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
908 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
913 /* Returns the template (one of the functions given by TEMPLATE_ID)
914 which can be specialized to match the indicated DECL with the
915 explicit template args given in TEMPLATE_ID. The DECL may be
916 NULL_TREE if none is available. In that case, the functions in
917 TEMPLATE_ID are non-members.
919 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
920 specialization of a member template.
922 The template args (those explicitly specified and those deduced)
923 are output in a newly created vector *TARGS_OUT.
925 If it is impossible to determine the result, an error message is
926 issued. The error_mark_node is returned to indicate failure. */
929 determine_specialization (template_id
, decl
, targs_out
,
930 need_member_template
)
934 int need_member_template
;
940 tree candidates
= NULL_TREE
;
941 tree templates
= NULL_TREE
;
943 *targs_out
= NULL_TREE
;
945 if (template_id
== error_mark_node
)
946 return error_mark_node
;
948 fns
= TREE_OPERAND (template_id
, 0);
949 explicit_targs
= TREE_OPERAND (template_id
, 1);
951 if (fns
== error_mark_node
)
952 return error_mark_node
;
954 /* Check for baselinks. */
955 if (TREE_CODE (fns
) == TREE_LIST
)
956 fns
= TREE_VALUE (fns
);
958 for (; fns
; fns
= OVL_NEXT (fns
))
962 fn
= OVL_CURRENT (fns
);
964 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
965 /* DECL might be a specialization of FN. */
967 else if (need_member_template
)
968 /* FN is an ordinary member function, and we need a
969 specialization of a member template. */
971 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
972 /* We can get IDENTIFIER_NODEs here in certain erroneous
975 else if (!DECL_FUNCTION_MEMBER_P (fn
))
976 /* This is just an ordinary non-member function. Nothing can
977 be a specialization of that. */
983 /* This is an ordinary member function. However, since
984 we're here, we can assume it's enclosing class is a
985 template class. For example,
987 template <typename T> struct S { void f(); };
988 template <> void S<int>::f() {}
990 Here, S<int>::f is a non-template, but S<int> is a
991 template class. If FN has the same type as DECL, we
992 might be in business. */
993 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
994 TREE_TYPE (TREE_TYPE (fn
))))
995 /* The return types differ. */
998 /* Adjust the type of DECL in case FN is a static member. */
999 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1000 if (DECL_STATIC_FUNCTION_P (fn
)
1001 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1002 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1004 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1007 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1012 /* See whether this function might be a specialization of this
1014 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1017 /* We cannot deduce template arguments that when used to
1018 specialize TMPL will produce DECL. */
1021 /* Save this template, and the arguments deduced. */
1022 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
1025 if (templates
&& TREE_CHAIN (templates
))
1031 It is possible for a specialization with a given function
1032 signature to be instantiated from more than one function
1033 template. In such cases, explicit specification of the
1034 template arguments must be used to uniquely identify the
1035 function template specialization being specialized.
1037 Note that here, there's no suggestion that we're supposed to
1038 determine which of the candidate templates is most
1039 specialized. However, we, also have:
1043 Partial ordering of overloaded function template
1044 declarations is used in the following contexts to select
1045 the function template to which a function template
1046 specialization refers:
1048 -- when an explicit specialization refers to a function
1051 So, we do use the partial ordering rules, at least for now.
1052 This extension can only serve to make illegal programs legal,
1053 so it's safe. And, there is strong anecdotal evidence that
1054 the committee intended the partial ordering rules to apply;
1055 the EDG front-end has that behavior, and John Spicer claims
1056 that the committee simply forgot to delete the wording in
1057 [temp.expl.spec]. */
1058 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1059 if (tmpl
&& tmpl
!= error_mark_node
)
1061 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1062 templates
= scratch_tree_cons (targs
, tmpl
, NULL_TREE
);
1066 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1068 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1070 return error_mark_node
;
1072 else if ((templates
&& TREE_CHAIN (templates
))
1073 || (candidates
&& TREE_CHAIN (candidates
))
1074 || (templates
&& candidates
))
1076 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1078 chainon (candidates
, templates
);
1079 print_candidates (candidates
);
1080 return error_mark_node
;
1083 /* We have one, and exactly one, match. */
1086 /* It was a specialization of an ordinary member function in a
1088 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1089 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1092 /* It was a specialization of a template. */
1093 targs
= DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates
)));
1094 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1096 *targs_out
= copy_node (targs
);
1097 SET_TMPL_ARGS_LEVEL (*targs_out
,
1098 TMPL_ARGS_DEPTH (*targs_out
),
1099 TREE_PURPOSE (templates
));
1102 *targs_out
= TREE_PURPOSE (templates
);
1103 return TREE_VALUE (templates
);
1106 /* Check to see if the function just declared, as indicated in
1107 DECLARATOR, and in DECL, is a specialization of a function
1108 template. We may also discover that the declaration is an explicit
1109 instantiation at this point.
1111 Returns DECL, or an equivalent declaration that should be used
1112 instead if all goes well. Issues an error message if something is
1113 amiss. Returns error_mark_node if the error is not easily
1116 FLAGS is a bitmask consisting of the following flags:
1118 2: The function has a definition.
1119 4: The function is a friend.
1121 The TEMPLATE_COUNT is the number of references to qualifying
1122 template classes that appeared in the name of the function. For
1125 template <class T> struct S { void f(); };
1128 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1129 classes are not counted in the TEMPLATE_COUNT, so that in
1131 template <class T> struct S {};
1132 template <> struct S<int> { void f(); }
1133 template <> void S<int>::f();
1135 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1136 illegal; there should be no template <>.)
1138 If the function is a specialization, it is marked as such via
1139 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1140 is set up correctly, and it is added to the list of specializations
1141 for that template. */
1144 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1150 int have_def
= flags
& 2;
1151 int is_friend
= flags
& 4;
1152 int specialization
= 0;
1153 int explicit_instantiation
= 0;
1154 int member_specialization
= 0;
1156 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1157 tree dname
= DECL_NAME (decl
);
1159 if (processing_specialization
)
1161 /* The last template header was of the form template <>. */
1163 if (template_header_count
> template_count
)
1165 /* There were more template headers than qualifying template
1167 if (template_header_count
- template_count
> 1)
1168 /* There shouldn't be that many template parameter lists.
1169 There can be at most one parameter list for every
1170 qualifying class, plus one for the function itself. */
1171 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1173 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1175 member_specialization
= 1;
1179 else if (template_header_count
== template_count
)
1181 /* The counts are equal. So, this might be a
1182 specialization, but it is not a specialization of a
1183 member template. It might be something like
1185 template <class T> struct S {
1189 void S<int>::f(int i) {} */
1191 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1195 /* This cannot be an explicit specialization. There are not
1196 enough headers for all of the qualifying classes. For
1197 example, we might have:
1200 void S<int>::T<char>::f();
1202 But, we're missing another template <>. */
1203 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1207 else if (processing_explicit_instantiation
)
1209 if (template_header_count
)
1210 cp_error ("template parameter list used in explicit instantiation");
1213 cp_error ("definition provided for explicit instantiation");
1215 explicit_instantiation
= 1;
1217 else if (ctype
!= NULL_TREE
1218 && !TYPE_BEING_DEFINED (ctype
)
1219 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1222 /* This case catches outdated code that looks like this:
1224 template <class T> struct S { void f(); };
1225 void S<int>::f() {} // Missing template <>
1227 We disable this check when the type is being defined to
1228 avoid complaining about default compiler-generated
1229 constructors, destructors, and assignment operators.
1230 Since the type is an instantiation, not a specialization,
1231 these are the only functions that can be defined before
1232 the class is complete. */
1235 template <class T> void S<int>::f() {}
1237 if (template_header_count
)
1239 cp_error ("template parameters specified in specialization");
1245 ("explicit specialization not preceded by `template <>'");
1247 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1249 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1252 /* This could be something like:
1254 template <class T> void f(T);
1255 class S { friend void f<>(int); } */
1259 /* This case handles bogus declarations like template <>
1260 template <class T> void f<int>(); */
1262 cp_error ("template-id `%D' in declaration of primary template",
1268 if (specialization
|| member_specialization
)
1270 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1271 for (; t
; t
= TREE_CHAIN (t
))
1272 if (TREE_PURPOSE (t
))
1275 ("default argument specified in explicit specialization");
1278 if (current_lang_name
== lang_name_c
)
1279 cp_error ("template specialization with C linkage");
1282 if (specialization
|| member_specialization
|| explicit_instantiation
)
1285 tree tmpl
= NULL_TREE
;
1286 tree targs
= NULL_TREE
;
1288 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1289 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1293 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1296 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1301 lookup_template_function (fns
, NULL_TREE
);
1304 if (declarator
== error_mark_node
)
1305 return error_mark_node
;
1307 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1309 /* A friend declaration. We can't do much, because we don't
1310 know what this resolves to, yet. */
1311 my_friendly_assert (is_friend
!= 0, 0);
1312 my_friendly_assert (!explicit_instantiation
, 0);
1313 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1317 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1319 if (!explicit_instantiation
)
1320 /* A specialization in class scope. This is illegal,
1321 but the error will already have been flagged by
1322 check_specialization_scope. */
1323 return error_mark_node
;
1326 /* It's not legal to write an explicit instantiation in
1329 class C { template void f(); }
1331 This case is caught by the parser. However, on
1334 template class C { void f(); };
1336 (which is illegal) we can get here. The error will be
1343 else if (ctype
!= NULL_TREE
1344 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1347 /* Find the list of functions in ctype that have the same
1348 name as the declared function. */
1349 tree name
= TREE_OPERAND (declarator
, 0);
1350 tree fns
= NULL_TREE
;
1353 if (name
== constructor_name (ctype
)
1354 || name
== constructor_name_full (ctype
))
1356 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1358 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1359 : !TYPE_HAS_DESTRUCTOR (ctype
))
1361 /* From [temp.expl.spec]:
1363 If such an explicit specialization for the member
1364 of a class template names an implicitly-declared
1365 special member function (clause _special_), the
1366 program is ill-formed.
1368 Similar language is found in [temp.explicit]. */
1369 cp_error ("specialization of implicitly-declared special member function");
1370 return error_mark_node
;
1373 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1376 if (!IDENTIFIER_TYPENAME_P (name
))
1378 idx
= lookup_fnfields_1 (ctype
, name
);
1380 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1386 /* For a type-conversion operator, we cannot do a
1387 name-based lookup. We might be looking for `operator
1388 int' which will be a specialization of `operator T'.
1389 So, we find *all* the conversion operators, and then
1390 select from them. */
1393 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1395 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1397 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1399 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1400 /* There are no more conversion functions. */
1403 /* Glue all these conversion functions together
1404 with those we already have. */
1405 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1406 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1410 if (fns
== NULL_TREE
)
1412 cp_error ("no member function `%s' declared in `%T'",
1413 IDENTIFIER_POINTER (name
),
1415 return error_mark_node
;
1418 TREE_OPERAND (declarator
, 0) = fns
;
1421 /* Figure out what exactly is being specialized at this point.
1422 Note that for an explicit instantiation, even one for a
1423 member function, we cannot tell apriori whether the
1424 instantiation is for a member template, or just a member
1425 function of a template class. Even if a member template is
1426 being instantiated, the member template arguments may be
1427 elided if they can be deduced from the rest of the
1429 tmpl
= determine_specialization (declarator
, decl
,
1431 member_specialization
);
1433 if (!tmpl
|| tmpl
== error_mark_node
)
1434 /* We couldn't figure out what this declaration was
1436 return error_mark_node
;
1439 gen_tmpl
= most_general_template (tmpl
);
1441 if (explicit_instantiation
)
1443 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1444 is done by do_decl_instantiation later. */
1445 decl
= instantiate_template (tmpl
, innermost_args (targs
));
1449 /* If we though that the DECL was a member function, but it
1450 turns out to be specializing a static member function,
1451 make DECL a static member function as well. */
1452 if (DECL_STATIC_FUNCTION_P (tmpl
)
1453 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1455 revert_static_member_fn (&decl
, 0, 0);
1456 last_function_parms
= TREE_CHAIN (last_function_parms
);
1459 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1460 DECL_TEMPLATE_INFO (decl
)
1461 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1463 /* Mangle the function name appropriately. Note that we do
1464 not mangle specializations of non-template member
1465 functions of template classes, e.g. with
1467 template <class T> struct S { void f(); }
1469 and given the specialization
1471 template <> void S<int>::f() {}
1473 we do not mangle S<int>::f() here. That's because it's
1474 just an ordinary member function and doesn't need special
1475 treatment. We do this here so that the ordinary,
1476 non-template, name-mangling algorith will not be used
1478 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1479 && name_mangling_version
>= 1)
1480 set_mangled_name_for_template_decl (decl
);
1482 if (is_friend
&& !have_def
)
1483 /* This is not really a declaration of a specialization.
1484 It's just the name of an instantiation. But, it's not
1485 a request for an instantiation, either. */
1486 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1488 /* Register this specialization so that we can find it
1490 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1497 /* TYPE is being declared. Verify that the use of template headers
1498 and such is reasonable. Issue error messages if not. */
1501 maybe_check_template_type (type
)
1504 if (template_header_count
)
1506 /* We are in the scope of some `template <...>' header. */
1509 = template_class_depth_real (TYPE_CONTEXT (type
),
1510 /*count_specializations=*/1);
1512 if (template_header_count
<= context_depth
)
1513 /* This is OK; the template headers are for the context. We
1514 are actually too lenient here; like
1515 check_explicit_specialization we should consider the number
1516 of template types included in the actual declaration. For
1519 template <class T> struct S {
1520 template <class U> template <class V>
1526 template <class T> struct S {
1527 template <class U> struct I;
1530 template <class T> template <class U.
1535 else if (template_header_count
> context_depth
+ 1)
1536 /* There are two many template parameter lists. */
1537 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1541 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1542 parameters. These are represented in the same format used for
1543 DECL_TEMPLATE_PARMS. */
1545 int comp_template_parms (parms1
, parms2
)
1552 if (parms1
== parms2
)
1555 for (p1
= parms1
, p2
= parms2
;
1556 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1557 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1559 tree t1
= TREE_VALUE (p1
);
1560 tree t2
= TREE_VALUE (p2
);
1563 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1564 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1566 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1569 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1571 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1572 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1574 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1577 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1579 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1584 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1585 /* One set of parameters has more parameters lists than the
1592 /* Complain if DECL shadows a template parameter.
1594 [temp.local]: A template-parameter shall not be redeclared within its
1595 scope (including nested scopes). */
1598 check_template_shadow (decl
)
1601 tree olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1603 if (current_template_parms
&& olddecl
)
1605 /* We check for decl != olddecl to avoid bogus errors for using a
1606 name inside a class. We check TPFI to avoid duplicate errors for
1607 inline member templates. */
1608 if (decl
!= olddecl
&& DECL_TEMPLATE_PARM_P (olddecl
)
1609 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1611 cp_error_at ("declaration of `%#D'", decl
);
1612 cp_error_at (" shadows template parm `%#D'", olddecl
);
1617 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1618 ORIG_LEVEL, DECL, and TYPE. */
1621 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1628 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1629 TEMPLATE_PARM_IDX (t
) = index
;
1630 TEMPLATE_PARM_LEVEL (t
) = level
;
1631 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1632 TEMPLATE_PARM_DECL (t
) = decl
;
1633 TREE_TYPE (t
) = type
;
1638 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1639 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1640 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1641 new one is created. */
1644 reduce_template_parm_level (index
, type
, levels
)
1649 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1650 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1651 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1654 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1655 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1658 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1659 TEMPLATE_PARM_LEVEL (index
) - levels
,
1660 TEMPLATE_PARM_ORIG_LEVEL (index
),
1662 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1664 /* Template template parameters need this. */
1665 DECL_TEMPLATE_PARMS (decl
)
1666 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1669 return TEMPLATE_PARM_DESCENDANTS (index
);
1672 /* Process information from new template parameter NEXT and append it to the
1673 LIST being built. */
1676 process_template_parm (list
, next
)
1685 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1686 defval
= TREE_PURPOSE (parm
);
1687 parm
= TREE_VALUE (parm
);
1688 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1692 tree p
= TREE_VALUE (tree_last (list
));
1694 if (TREE_CODE (p
) == TYPE_DECL
)
1695 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1696 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1697 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1699 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1707 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1708 /* is a const-param */
1709 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1710 PARM
, 0, NULL_TREE
);
1714 The top-level cv-qualifiers on the template-parameter are
1715 ignored when determining its type. */
1716 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1718 /* A template parameter is not modifiable. */
1719 TREE_READONLY (parm
) = 1;
1720 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1721 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1722 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1724 cp_error ("`%#T' is not a valid type for a template constant parameter",
1726 if (DECL_NAME (parm
) == NULL_TREE
)
1727 error (" a template type parameter must begin with `class' or `typename'");
1728 TREE_TYPE (parm
) = void_type_node
;
1731 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1732 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1733 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1735 if (TREE_PERMANENT (parm
) == 0)
1737 parm
= copy_node (parm
);
1738 TREE_PERMANENT (parm
) = 1;
1740 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1741 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1742 = build_template_parm_index (idx
, processing_template_decl
,
1743 processing_template_decl
,
1744 decl
, TREE_TYPE (parm
));
1749 parm
= TREE_VALUE (parm
);
1751 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1753 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1754 /* This is for distinguishing between real templates and template
1755 template parameters */
1756 TREE_TYPE (parm
) = t
;
1757 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1762 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1763 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1764 decl
= build_decl (TYPE_DECL
, parm
, t
);
1767 TYPE_NAME (t
) = decl
;
1768 TYPE_STUB_DECL (t
) = decl
;
1770 TEMPLATE_TYPE_PARM_INDEX (t
)
1771 = build_template_parm_index (idx
, processing_template_decl
,
1772 processing_template_decl
,
1773 decl
, TREE_TYPE (parm
));
1775 SET_DECL_ARTIFICIAL (decl
);
1776 DECL_TEMPLATE_PARM_P (decl
) = 1;
1778 parm
= build_tree_list (defval
, parm
);
1779 return chainon (list
, parm
);
1782 /* The end of a template parameter list has been reached. Process the
1783 tree list into a parameter vector, converting each parameter into a more
1784 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1788 end_template_parm_list (parms
)
1793 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1795 current_template_parms
1796 = tree_cons (build_int_2 (0, processing_template_decl
),
1797 saved_parmlist
, current_template_parms
);
1799 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1800 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1802 --processing_template_parmlist
;
1804 return saved_parmlist
;
1807 /* end_template_decl is called after a template declaration is seen. */
1810 end_template_decl ()
1812 reset_specialization ();
1814 if (! processing_template_decl
)
1817 /* This matches the pushlevel in begin_template_parm_list. */
1820 --processing_template_decl
;
1821 current_template_parms
= TREE_CHAIN (current_template_parms
);
1822 (void) get_pending_sizes (); /* Why? */
1825 /* Given a template argument vector containing the template PARMS.
1826 The innermost PARMS are given first. */
1829 current_template_args ()
1832 tree args
= NULL_TREE
;
1833 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1836 /* If there is only one level of template parameters, we do not
1837 create a TREE_VEC of TREE_VECs. Instead, we return a single
1838 TREE_VEC containing the arguments. */
1840 args
= make_tree_vec (length
);
1842 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1844 tree a
= copy_node (TREE_VALUE (header
));
1847 TREE_TYPE (a
) = NULL_TREE
;
1848 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1850 tree t
= TREE_VEC_ELT (a
, i
);
1852 /* T will be a list if we are called from within a
1853 begin/end_template_parm_list pair, but a vector directly
1854 if within a begin/end_member_template_processing pair. */
1855 if (TREE_CODE (t
) == TREE_LIST
)
1859 if (TREE_CODE (t
) == TYPE_DECL
1860 || TREE_CODE (t
) == TEMPLATE_DECL
)
1863 t
= DECL_INITIAL (t
);
1864 TREE_VEC_ELT (a
, i
) = t
;
1869 TREE_VEC_ELT (args
, --l
) = a
;
1877 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1878 template PARMS. Used by push_template_decl below. */
1881 build_template_decl (decl
, parms
)
1885 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1886 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1887 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1888 if (DECL_LANG_SPECIFIC (decl
))
1890 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1891 DECL_STATIC_FUNCTION_P (tmpl
) =
1892 DECL_STATIC_FUNCTION_P (decl
);
1898 struct template_parm_data
1900 /* The level of the template parameters we are currently
1904 /* The index of the specialization argument we are currently
1908 /* An array whose size is the number of template parameters. The
1909 elements are non-zero if the parameter has been used in any one
1910 of the arguments processed so far. */
1913 /* An array whose size is the number of template arguments. The
1914 elements are non-zero if the argument makes use of template
1915 parameters of this level. */
1916 int* arg_uses_template_parms
;
1919 /* Subroutine of push_template_decl used to see if each template
1920 parameter in a partial specialization is used in the explicit
1921 argument list. If T is of the LEVEL given in DATA (which is
1922 treated as a template_parm_data*), then DATA->PARMS is marked
1926 mark_template_parm (t
, data
)
1932 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1934 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1936 level
= TEMPLATE_PARM_LEVEL (t
);
1937 idx
= TEMPLATE_PARM_IDX (t
);
1941 level
= TEMPLATE_TYPE_LEVEL (t
);
1942 idx
= TEMPLATE_TYPE_IDX (t
);
1945 if (level
== tpd
->level
)
1947 tpd
->parms
[idx
] = 1;
1948 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
1951 /* Return zero so that for_each_template_parm will continue the
1952 traversal of the tree; we want to mark *every* template parm. */
1956 /* Process the partial specialization DECL. */
1959 process_partial_specialization (decl
)
1962 tree type
= TREE_TYPE (decl
);
1963 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1964 tree specargs
= CLASSTYPE_TI_ARGS (type
);
1965 tree inner_args
= innermost_args (specargs
);
1966 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1967 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
1968 int nargs
= TREE_VEC_LENGTH (inner_args
);
1969 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
1971 int did_error_intro
= 0;
1972 struct template_parm_data tpd
;
1973 struct template_parm_data tpd2
;
1975 /* We check that each of the template parameters given in the
1976 partial specialization is used in the argument list to the
1977 specialization. For example:
1979 template <class T> struct S;
1980 template <class T> struct S<T*>;
1982 The second declaration is OK because `T*' uses the template
1983 parameter T, whereas
1985 template <class T> struct S<int>;
1987 is no good. Even trickier is:
1998 The S2<T> declaration is actually illegal; it is a
1999 full-specialization. Of course,
2002 struct S2<T (*)(U)>;
2004 or some such would have been OK. */
2005 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2006 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2007 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
2009 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2010 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
2011 for (i
= 0; i
< nargs
; ++i
)
2013 tpd
.current_arg
= i
;
2014 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2015 &mark_template_parm
,
2018 for (i
= 0; i
< ntparms
; ++i
)
2019 if (tpd
.parms
[i
] == 0)
2021 /* One of the template parms was not used in the
2023 if (!did_error_intro
)
2025 cp_error ("template parameters not used in partial specialization:");
2026 did_error_intro
= 1;
2030 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2033 /* [temp.class.spec]
2035 The argument list of the specialization shall not be identical to
2036 the implicit argument list of the primary template. */
2037 if (comp_template_args (inner_args
,
2038 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2040 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2042 /* [temp.class.spec]
2044 A partially specialized non-type argument expression shall not
2045 involve template parameters of the partial specialization except
2046 when the argument expression is a simple identifier.
2048 The type of a template parameter corresponding to a specialized
2049 non-type argument shall not be dependent on a parameter of the
2051 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2053 for (i
= 0; i
< nargs
; ++i
)
2055 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2056 if (/* These first two lines are the `non-type' bit. */
2057 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2058 && TREE_CODE (arg
) != TEMPLATE_DECL
2059 /* This next line is the `argument expression is not just a
2060 simple identifier' condition and also the `specialized
2061 non-type argument' bit. */
2062 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2064 if (tpd
.arg_uses_template_parms
[i
])
2065 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2068 /* Look at the corresponding template parameter,
2069 marking which template parameters its type depends
2072 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2077 /* We haven't yet initialized TPD2. Do so now. */
2078 tpd2
.arg_uses_template_parms
2079 = (int*) alloca (sizeof (int) * nargs
);
2080 /* The number of parameters here is the number in the
2081 main template, which, as checked in the assertion
2083 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2085 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2088 /* Mark the template parameters. But this time, we're
2089 looking for the template parameters of the main
2090 template, not in the specialization. */
2091 tpd2
.current_arg
= i
;
2092 tpd2
.arg_uses_template_parms
[i
] = 0;
2093 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
2094 for_each_template_parm (type
,
2095 &mark_template_parm
,
2098 if (tpd2
.arg_uses_template_parms
[i
])
2100 /* The type depended on some template parameters.
2101 If they are fully specialized in the
2102 specialization, that's OK. */
2104 for (j
= 0; j
< nargs
; ++j
)
2105 if (tpd2
.parms
[j
] != 0
2106 && tpd
.arg_uses_template_parms
[j
])
2108 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2118 if (retrieve_specialization (maintmpl
, specargs
))
2119 /* We've already got this specialization. */
2122 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2123 = perm_tree_cons (inner_args
, inner_parms
,
2124 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2125 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2129 /* Check that a template declaration's use of default arguments is not
2130 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2131 non-zero if DECL is the thing declared by a primary template.
2132 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2135 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2142 int last_level_to_check
;
2146 A default template-argument shall not be specified in a
2147 function template declaration or a function template definition, nor
2148 in the template-parameter-list of the definition of a member of a
2151 if (current_class_type
2152 && !TYPE_BEING_DEFINED (current_class_type
)
2153 && DECL_REAL_CONTEXT (decl
) == current_class_type
2154 && DECL_DEFINED_IN_CLASS_P (decl
))
2155 /* We already checked these parameters when the template was
2156 declared, so there's no need to do it again now. This is an
2157 inline member function definition. */
2160 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2161 /* For an ordinary class template, default template arguments are
2162 allowed at the innermost level, e.g.:
2163 template <class T = int>
2165 but, in a partial specialization, they're not allowed even
2166 there, as we have in [temp.class.spec]:
2168 The template parameter list of a specialization shall not
2169 contain default template argument values.
2171 So, for a partial specialization, or for a function template,
2172 we look at all of them. */
2175 /* But, for a primary class template that is not a partial
2176 specialization we look at all template parameters except the
2178 parms
= TREE_CHAIN (parms
);
2180 /* Figure out what error message to issue. */
2181 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2182 msg
= "default argument for template parameter in function template `%D'";
2183 else if (is_partial
)
2184 msg
= "default argument in partial specialization `%D'";
2186 msg
= "default argument for template parameter for class enclosing `%D'";
2188 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2189 /* If we're inside a class definition, there's no need to
2190 examine the parameters to the class itself. On the one
2191 hand, they will be checked when the class is defined, and,
2192 on the other, default arguments are legal in things like:
2193 template <class T = double>
2194 struct S { template <class U> void f(U); };
2195 Here the default argument for `S' has no bearing on the
2196 declaration of `f'. */
2197 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2199 /* Check everything. */
2200 last_level_to_check
= 0;
2202 for (; parms
&& TMPL_PARMS_DEPTH (parms
) >= last_level_to_check
;
2203 parms
= TREE_CHAIN (parms
))
2205 tree inner_parms
= TREE_VALUE (parms
);
2208 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2209 for (i
= 0; i
< ntparms
; ++i
)
2210 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2214 cp_error (msg
, decl
);
2218 /* Clear out the default argument so that we are not
2220 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2223 /* At this point, if we're still interested in issuing messages,
2224 they must apply to classes surrounding the object declared. */
2226 msg
= "default argument for template parameter for class enclosing `%D'";
2230 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2231 parameters given by current_template_args, or reuses a
2232 previously existing one, if appropriate. Returns the DECL, or an
2233 equivalent one, if it is replaced via a call to duplicate_decls.
2235 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2238 push_template_decl_real (decl
, is_friend
)
2249 /* See if this is a partial specialization. */
2250 is_partial
= (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
2251 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2252 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2254 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2257 /* For a friend, we want the context of the friend function, not
2258 the type of which it is a friend. */
2259 ctx
= DECL_CONTEXT (decl
);
2260 else if (DECL_REAL_CONTEXT (decl
)
2261 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2262 /* In the case of a virtual function, we want the class in which
2264 ctx
= DECL_REAL_CONTEXT (decl
);
2266 /* Otherwise, if we're currently definining some class, the DECL
2267 is assumed to be a member of the class. */
2268 ctx
= current_class_type
;
2270 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2273 if (!DECL_CONTEXT (decl
))
2274 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2276 /* For determining whether this is a primary template or not, we're really
2277 interested in the lexical context, not the true context. */
2279 info
= current_class_type
;
2283 /* See if this is a primary template. */
2284 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
2286 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2287 this next line works even when we are at global scope. */
2288 else if (processing_template_decl
> template_class_depth (info
))
2295 if (current_lang_name
== lang_name_c
)
2296 cp_error ("template with C linkage");
2297 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
2298 cp_error ("template class without a name");
2299 if (TREE_CODE (decl
) == TYPE_DECL
2300 && TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2301 cp_error ("template declaration of `%#T'", TREE_TYPE (decl
));
2304 /* Check to see that the rules regarding the use of default
2305 arguments are not being violated. */
2306 check_default_tmpl_args (decl
, current_template_parms
,
2307 primary
, is_partial
);
2310 return process_partial_specialization (decl
);
2312 args
= current_template_args ();
2315 || TREE_CODE (ctx
) == FUNCTION_DECL
2316 || TYPE_BEING_DEFINED (ctx
)
2317 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2319 if (DECL_LANG_SPECIFIC (decl
)
2320 && DECL_TEMPLATE_INFO (decl
)
2321 && DECL_TI_TEMPLATE (decl
))
2322 tmpl
= DECL_TI_TEMPLATE (decl
);
2325 tmpl
= build_template_decl (decl
, current_template_parms
);
2327 if (DECL_LANG_SPECIFIC (decl
)
2328 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2330 /* A specialization of a member template of a template
2332 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2333 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2334 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2340 tree a
, t
, current
, parms
;
2343 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2344 cp_error ("must specialize `%#T' before defining member `%#D'",
2346 if (TREE_CODE (decl
) == TYPE_DECL
)
2348 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2349 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2350 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2351 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2352 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2355 cp_error ("`%D' does not declare a template type", decl
);
2359 else if (! DECL_TEMPLATE_INFO (decl
))
2361 cp_error ("template definition of non-template `%#D'", decl
);
2365 tmpl
= DECL_TI_TEMPLATE (decl
);
2367 if (is_member_template (tmpl
)
2368 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2369 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2370 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2374 /* The declaration is a specialization of a member
2375 template, declared outside the class. Therefore, the
2376 innermost template arguments will be NULL, so we
2377 replace them with the arguments determined by the
2378 earlier call to check_explicit_specialization. */
2379 args
= DECL_TI_ARGS (decl
);
2382 = build_template_decl (decl
, current_template_parms
);
2383 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2384 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2385 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2386 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2387 DECL_TEMPLATE_INFO (new_tmpl
) =
2388 perm_tree_cons (tmpl
, args
, NULL_TREE
);
2390 register_specialization (new_tmpl
, tmpl
, args
);
2394 /* Make sure the template headers we got make sense. */
2396 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2397 i
= TMPL_PARMS_DEPTH (parms
);
2398 if (TMPL_ARGS_DEPTH (args
) != i
)
2400 cp_error ("expected %d levels of template parms for `%#D', got %d",
2401 i
, decl
, TMPL_ARGS_DEPTH (args
));
2404 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2406 a
= TMPL_ARGS_LEVEL (args
, i
);
2407 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2409 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2411 if (current
== decl
)
2412 cp_error ("got %d template parameters for `%#D'",
2413 TREE_VEC_LENGTH (a
), decl
);
2415 cp_error ("got %d template parameters for `%#T'",
2416 TREE_VEC_LENGTH (a
), current
);
2417 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2420 /* Perhaps we should also check that the parms are used in the
2421 appropriate qualifying scopes in the declarator? */
2423 if (current
== decl
)
2426 current
= TYPE_CONTEXT (current
);
2430 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2431 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2433 /* Push template declarations for global functions and types. Note
2434 that we do not try to push a global template friend declared in a
2435 template class; such a thing may well depend on the template
2436 parameters of the class. */
2438 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2439 tmpl
= pushdecl_namespace_level (tmpl
);
2442 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2444 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
2446 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2448 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2449 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2450 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2451 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2453 else if (! DECL_LANG_SPECIFIC (decl
))
2454 cp_error ("template declaration of `%#D'", decl
);
2456 DECL_TEMPLATE_INFO (decl
) = info
;
2458 return DECL_TEMPLATE_RESULT (tmpl
);
2462 push_template_decl (decl
)
2465 return push_template_decl_real (decl
, 0);
2468 /* Called when a class template TYPE is redeclared with the indicated
2469 template PARMS, e.g.:
2471 template <class T> struct S;
2472 template <class T> struct S {}; */
2475 redeclare_class_template (type
, parms
)
2479 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2483 if (!PRIMARY_TEMPLATE_P (tmpl
))
2484 /* The type is nested in some template class. Nothing to worry
2485 about here; there are no new template parameters for the nested
2489 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2490 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2492 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2494 cp_error_at ("previous declaration `%D'", tmpl
);
2495 cp_error ("used %d template parameter%s instead of %d",
2496 TREE_VEC_LENGTH (tmpl_parms
),
2497 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2498 TREE_VEC_LENGTH (parms
));
2502 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2504 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2505 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2506 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2507 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2509 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2511 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2512 cp_error ("redeclared here as `%#D'", parm
);
2516 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2518 /* We have in [temp.param]:
2520 A template-parameter may not be given default arguments
2521 by two different declarations in the same scope. */
2522 cp_error ("redefinition of default argument for `%#D'", parm
);
2523 cp_error_at (" original definition appeared here", tmpl_parm
);
2527 if (parm_default
!= NULL_TREE
)
2528 /* Update the previous template parameters (which are the ones
2529 that will really count) with the new default value. */
2530 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2534 /* Attempt to convert the non-type template parameter EXPR to the
2535 indicated TYPE. If the conversion is successful, return the
2536 converted value. If the conversion is unsuccesful, return
2537 NULL_TREE if we issued an error message, or error_mark_node if we
2538 did not. We issue error messages for out-and-out bad template
2539 parameters, but not simply because the conversion failed, since we
2540 might be just trying to do argument deduction. By the time this
2541 function is called, neither TYPE nor EXPR may make use of template
2545 convert_nontype_argument (type
, expr
)
2549 tree expr_type
= TREE_TYPE (expr
);
2551 /* A template-argument for a non-type, non-template
2552 template-parameter shall be one of:
2554 --an integral constant-expression of integral or enumeration
2557 --the name of a non-type template-parameter; or
2559 --the name of an object or function with external linkage,
2560 including function templates and function template-ids but
2561 excluding non-static class members, expressed as id-expression;
2564 --the address of an object or function with external linkage,
2565 including function templates and function template-ids but
2566 excluding non-static class members, expressed as & id-expression
2567 where the & is optional if the name refers to a function or
2570 --a pointer to member expressed as described in _expr.unary.op_. */
2572 /* An integral constant-expression can include const variables
2574 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
2575 expr
= decl_constant_value (expr
);
2577 if (is_overloaded_fn (expr
))
2578 /* OK for now. We'll check that it has external linkage later.
2579 Check this first since if expr_type is the unknown_type_node
2580 we would otherwise complain below. */
2582 else if (TYPE_PTRMEM_P (expr_type
)
2583 || TYPE_PTRMEMFUNC_P (expr_type
))
2585 if (TREE_CODE (expr
) != PTRMEM_CST
)
2588 else if (TYPE_PTR_P (expr_type
)
2589 || TYPE_PTRMEM_P (expr_type
)
2590 || TREE_CODE (expr_type
) == ARRAY_TYPE
2591 || TREE_CODE (type
) == REFERENCE_TYPE
2592 /* If expr is the address of an overloaded function, we
2593 will get the unknown_type_node at this point. */
2594 || expr_type
== unknown_type_node
)
2600 if (TREE_CODE (type
) == REFERENCE_TYPE
2601 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2605 if (TREE_CODE (e
) != ADDR_EXPR
)
2608 cp_error ("`%E' is not a valid template argument", expr
);
2609 if (TYPE_PTR_P (expr_type
))
2611 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2612 cp_error ("it must be the address of a function with external linkage");
2614 cp_error ("it must be the address of an object with external linkage");
2616 else if (TYPE_PTRMEM_P (expr_type
)
2617 || TYPE_PTRMEMFUNC_P (expr_type
))
2618 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2623 referent
= TREE_OPERAND (e
, 0);
2624 STRIP_NOPS (referent
);
2627 if (TREE_CODE (referent
) == STRING_CST
)
2629 cp_error ("string literal %E is not a valid template argument",
2631 error ("because it is the address of an object with static linkage");
2635 if (is_overloaded_fn (referent
))
2636 /* We'll check that it has external linkage later. */
2638 else if (TREE_CODE (referent
) != VAR_DECL
)
2640 else if (!TREE_PUBLIC (referent
))
2642 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2643 return error_mark_node
;
2646 else if (INTEGRAL_TYPE_P (expr_type
)
2647 || TYPE_PTRMEM_P (expr_type
)
2648 || TYPE_PTRMEMFUNC_P (expr_type
)
2649 /* The next two are g++ extensions. */
2650 || TREE_CODE (expr_type
) == REAL_TYPE
2651 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2653 if (! TREE_CONSTANT (expr
))
2656 cp_error ("non-constant `%E' cannot be used as template argument",
2663 cp_error ("object `%E' cannot be used as template argument", expr
);
2667 switch (TREE_CODE (type
))
2672 /* For a non-type template-parameter of integral or enumeration
2673 type, integral promotions (_conv.prom_) and integral
2674 conversions (_conv.integral_) are applied. */
2675 if (!INTEGRAL_TYPE_P (expr_type
))
2676 return error_mark_node
;
2678 /* It's safe to call digest_init in this case; we know we're
2679 just converting one integral constant expression to another. */
2680 expr
= digest_init (type
, expr
, (tree
*) 0);
2682 if (TREE_CODE (expr
) != INTEGER_CST
)
2683 /* Curiously, some TREE_CONSTANT integral expressions do not
2684 simplify to integer constants. For example, `3 % 0',
2685 remains a TRUNC_MOD_EXPR. */
2692 /* These are g++ extensions. */
2693 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2694 return error_mark_node
;
2696 expr
= digest_init (type
, expr
, (tree
*) 0);
2698 if (TREE_CODE (expr
) != REAL_CST
)
2705 tree type_pointed_to
= TREE_TYPE (type
);
2707 if (TYPE_PTRMEM_P (type
))
2711 /* For a non-type template-parameter of type pointer to data
2712 member, qualification conversions (_conv.qual_) are
2714 e
= perform_qualification_conversions (type
, expr
);
2715 if (TREE_CODE (e
) == NOP_EXPR
)
2717 /* The call to perform_qualification_conversions will
2718 insert a NOP_EXPR over EXPR to do express
2719 conversion, if necessary. But, that will confuse
2720 us if we use this (converted) template parameter to
2721 instantiate another template; then the thing will
2722 not look like a valid template argument. So, just
2723 make a new constant, of the appropriate type. */
2724 e
= make_node (PTRMEM_CST
);
2725 TREE_TYPE (e
) = type
;
2726 PTRMEM_CST_MEMBER (e
) = PTRMEM_CST_MEMBER (expr
);
2730 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2732 /* For a non-type template-parameter of type pointer to
2733 function, only the function-to-pointer conversion
2734 (_conv.func_) is applied. If the template-argument
2735 represents a set of overloaded functions (or a pointer to
2736 such), the matching function is selected from the set
2741 if (TREE_CODE (expr
) == ADDR_EXPR
)
2742 fns
= TREE_OPERAND (expr
, 0);
2746 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2748 if (fn
== error_mark_node
)
2749 return error_mark_node
;
2751 if (!TREE_PUBLIC (fn
))
2753 if (really_overloaded_fn (fns
))
2754 return error_mark_node
;
2759 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2761 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2767 /* For a non-type template-parameter of type pointer to
2768 object, qualification conversions (_conv.qual_) and the
2769 array-to-pointer conversion (_conv.array_) are applied.
2770 [Note: In particular, neither the null pointer conversion
2771 (_conv.ptr_) nor the derived-to-base conversion
2772 (_conv.ptr_) are applied. Although 0 is a valid
2773 template-argument for a non-type template-parameter of
2774 integral type, it is not a valid template-argument for a
2775 non-type template-parameter of pointer type.]
2777 The call to decay_conversion performs the
2778 array-to-pointer conversion, if appropriate. */
2779 expr
= decay_conversion (expr
);
2781 if (expr
== error_mark_node
)
2782 return error_mark_node
;
2784 return perform_qualification_conversions (type
, expr
);
2789 case REFERENCE_TYPE
:
2791 tree type_referred_to
= TREE_TYPE (type
);
2793 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2795 /* For a non-type template-parameter of type reference to
2796 function, no conversions apply. If the
2797 template-argument represents a set of overloaded
2798 functions, the matching function is selected from the
2799 set (_over.over_). */
2803 fn
= instantiate_type (type_referred_to
, fns
, 0);
2805 if (fn
== error_mark_node
)
2806 return error_mark_node
;
2808 if (!TREE_PUBLIC (fn
))
2810 if (really_overloaded_fn (fns
))
2811 /* Don't issue an error here; we might get a different
2812 function if the overloading had worked out
2814 return error_mark_node
;
2819 my_friendly_assert (same_type_p (type_referred_to
,
2827 /* For a non-type template-parameter of type reference to
2828 object, no conversions apply. The type referred to by the
2829 reference may be more cv-qualified than the (otherwise
2830 identical) type of the template-argument. The
2831 template-parameter is bound directly to the
2832 template-argument, which must be an lvalue. */
2833 if ((TYPE_MAIN_VARIANT (expr_type
)
2834 != TYPE_MAIN_VARIANT (type_referred_to
))
2835 || !at_least_as_qualified_p (type_referred_to
,
2837 || !real_lvalue_p (expr
))
2838 return error_mark_node
;
2847 if (!TYPE_PTRMEMFUNC_P (type
))
2848 /* This handles templates like
2849 template<class T, T t> void f();
2850 when T is substituted with any class. The second template
2851 parameter becomes invalid and the template candidate is
2853 return error_mark_node
;
2855 /* For a non-type template-parameter of type pointer to member
2856 function, no conversions apply. If the template-argument
2857 represents a set of overloaded member functions, the
2858 matching member function is selected from the set
2861 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2862 expr_type
!= unknown_type_node
)
2863 return error_mark_node
;
2865 if (TREE_CODE (expr
) == PTRMEM_CST
)
2867 /* A ptr-to-member constant. */
2868 if (!same_type_p (type
, expr_type
))
2869 return error_mark_node
;
2874 if (TREE_CODE (expr
) != ADDR_EXPR
)
2875 return error_mark_node
;
2877 expr
= instantiate_type (type
, expr
, 0);
2879 if (expr
== error_mark_node
)
2880 return error_mark_node
;
2882 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2889 /* All non-type parameters must have one of these types. */
2890 my_friendly_abort (0);
2894 return error_mark_node
;
2897 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2898 template template parameters. Both PARM_PARMS and ARG_PARMS are
2899 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2902 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2903 the case, then extra parameters must have default arguments.
2905 Consider the example:
2906 template <class T, class Allocator = allocator> class vector;
2907 template<template <class U> class TT> class C;
2909 C<vector> is a valid instantiation. PARM_PARMS for the above code
2910 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2911 T and Allocator) and OUTER_ARGS contains the argument that is used to
2912 substitute the TT parameter. */
2915 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
2916 in_decl
, outer_args
)
2917 tree parm_parms
, arg_parms
;
2919 tree in_decl
, outer_args
;
2921 int nparms
, nargs
, i
;
2924 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
2925 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
2927 nparms
= TREE_VEC_LENGTH (parm_parms
);
2928 nargs
= TREE_VEC_LENGTH (arg_parms
);
2930 /* The rule here is opposite of coerce_template_parms. */
2933 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
2936 for (i
= 0; i
< nparms
; ++i
)
2938 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
2939 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
2941 if (arg
== NULL_TREE
|| arg
== error_mark_node
2942 || parm
== NULL_TREE
|| parm
== error_mark_node
)
2945 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2948 switch (TREE_CODE (parm
))
2954 /* We encounter instantiations of templates like
2955 template <template <template <class> class> class TT>
2958 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2959 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2961 if (!coerce_template_template_parms (parmparm
, argparm
,
2969 /* The tsubst call is used to handle cases such as
2970 template <class T, template <T> class TT> class D;
2971 i.e. the parameter list of TT depends on earlier parameters. */
2972 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
2979 my_friendly_abort (0);
2985 /* Convert the indicated template ARG as necessary to match the
2986 indicated template PARM. Returns the converted ARG, or
2987 error_mark_node if the conversion was unsuccessful. Error messages
2988 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2989 parameter in the parameter list. ARGS is the full set of template
2990 arguments deduced so far. */
2993 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3003 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3005 inner_args
= innermost_args (args
);
3007 if (TREE_CODE (arg
) == TREE_LIST
3008 && TREE_TYPE (arg
) != NULL_TREE
3009 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3011 /* The template argument was the name of some
3012 member function. That's usually
3013 illegal, but static members are OK. In any
3014 case, grab the underlying fields/functions
3015 and issue an error later if required. */
3016 arg
= TREE_VALUE (arg
);
3017 TREE_TYPE (arg
) = unknown_type_node
;
3020 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3021 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3022 || requires_tmpl_type
);
3024 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3025 we also accept implicitly created TYPE_DECL as a valid argument.
3026 This is necessary to handle the case where we pass a template name
3027 to a template template parameter in a scope where we've derived from
3028 in instantiation of that template, so the template name refers to that
3029 instantiation. We really ought to handle this better. */
3031 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3032 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3033 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3034 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
3035 || (TREE_CODE (arg
) == RECORD_TYPE
3036 && CLASSTYPE_TEMPLATE_INFO (arg
)
3037 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3038 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3039 && requires_tmpl_type
3040 && is_base_of_enclosing_class (arg
, current_class_type
)));
3041 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3042 arg
= TYPE_STUB_DECL (arg
);
3043 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3044 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3046 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
3048 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3049 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3051 cp_pedwarn ("to refer to a type member of a template parameter,");
3052 cp_pedwarn (" use `typename %E'", arg
);
3054 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3055 TREE_OPERAND (arg
, 1));
3058 if (is_type
!= requires_type
)
3064 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3067 cp_error (" expected a constant of type `%T', got `%T'",
3069 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3071 cp_error (" expected a type, got `%E'", arg
);
3074 return error_mark_node
;
3076 if (is_tmpl_type
^ requires_tmpl_type
)
3078 if (in_decl
&& complain
)
3080 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3083 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3085 cp_error (" expected a class template, got `%T'", arg
);
3087 return error_mark_node
;
3092 if (requires_tmpl_type
)
3094 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3095 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3097 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3098 in_decl
, inner_args
))
3102 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3104 if (val
!= error_mark_node
3105 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3106 val
= TREE_TYPE (val
);
3110 if (in_decl
&& complain
)
3112 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3114 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3117 val
= error_mark_node
;
3122 val
= groktypename (arg
);
3123 if (! processing_template_decl
)
3125 /* [basic.link]: A name with no linkage (notably, the
3126 name of a class or enumeration declared in a local
3127 scope) shall not be used to declare an entity with
3128 linkage. This implies that names with no linkage
3129 cannot be used as template arguments. */
3130 tree t
= no_linkage_check (val
);
3133 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3135 ("template-argument `%T' uses anonymous type", val
);
3138 ("template-argument `%T' uses local type `%T'",
3140 return error_mark_node
;
3147 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3149 if (processing_template_decl
)
3150 arg
= maybe_fold_nontype_arg (arg
);
3152 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3153 /* We used to call digest_init here. However, digest_init
3154 will report errors, which we don't want when complain
3155 is zero. More importantly, digest_init will try too
3156 hard to convert things: for example, `0' should not be
3157 converted to pointer type at this point according to
3158 the standard. Accepting this is not merely an
3159 extension, since deciding whether or not these
3160 conversions can occur is part of determining which
3161 function template to call, or whether a given epxlicit
3162 argument specification is legal. */
3163 val
= convert_nontype_argument (t
, arg
);
3167 if (val
== NULL_TREE
)
3168 val
= error_mark_node
;
3169 else if (val
== error_mark_node
&& complain
)
3170 cp_error ("could not convert template argument `%E' to `%T'",
3177 /* Convert all template arguments to their appropriate types, and
3178 return a vector containing the innermost resulting template
3179 arguments. If any error occurs, return error_mark_node, and, if
3180 COMPLAIN is non-zero, issue an error message. Some error messages
3181 are issued even if COMPLAIN is zero; for instance, if a template
3182 argument is composed from a local class.
3184 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3185 provided in ARGLIST, or else trailing parameters must have default
3186 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3187 deduction for any unspecified trailing arguments.
3189 The resulting TREE_VEC is allocated on a temporary obstack, and
3190 must be explicitly copied if it will be permanent. */
3193 coerce_template_parms (parms
, args
, in_decl
,
3195 require_all_arguments
)
3199 int require_all_arguments
;
3201 int nparms
, nargs
, i
, lost
= 0;
3204 tree new_inner_args
;
3206 inner_args
= innermost_args (args
);
3207 nargs
= NUM_TMPL_ARGS (inner_args
);
3208 nparms
= TREE_VEC_LENGTH (parms
);
3212 && require_all_arguments
3213 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3217 cp_error ("wrong number of template arguments (%d, should be %d)",
3221 cp_error_at ("provided for `%D'", in_decl
);
3224 return error_mark_node
;
3227 new_inner_args
= make_temp_vec (nparms
);
3228 new_args
= add_outermost_template_args (args
, new_inner_args
);
3229 for (i
= 0; i
< nparms
; i
++)
3234 /* Get the Ith template parameter. */
3235 parm
= TREE_VEC_ELT (parms
, i
);
3237 /* Calculate the Ith argument. */
3238 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3240 arg
= TREE_VALUE (inner_args
);
3241 inner_args
= TREE_CHAIN (inner_args
);
3244 arg
= TREE_VEC_ELT (inner_args
, i
);
3245 /* If no template argument was supplied, look for a default
3247 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3249 /* There was no default value. */
3250 my_friendly_assert (!require_all_arguments
, 0);
3253 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3254 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3256 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3259 /* Now, convert the Ith argument, as necessary. */
3260 if (arg
== NULL_TREE
)
3261 /* We're out of arguments. */
3263 my_friendly_assert (!require_all_arguments
, 0);
3266 else if (arg
== error_mark_node
)
3268 cp_error ("template argument %d is invalid", i
+ 1);
3269 arg
= error_mark_node
;
3272 arg
= convert_template_argument (TREE_VALUE (parm
),
3273 arg
, new_args
, complain
, i
,
3276 if (arg
== error_mark_node
)
3278 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3282 return error_mark_node
;
3284 return new_inner_args
;
3287 /* Returns 1 if template args OT and NT are equivalent. */
3290 template_args_equal (ot
, nt
)
3295 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3297 if (TREE_CODE (nt
) == TREE_VEC
)
3298 /* For member templates */
3299 return comp_template_args (ot
, nt
);
3300 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3301 return same_type_p (ot
, nt
);
3303 return (cp_tree_equal (ot
, nt
) > 0);
3306 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3307 of template arguments. Returns 0 otherwise. */
3310 comp_template_args (oldargs
, newargs
)
3311 tree oldargs
, newargs
;
3315 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3318 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3320 tree nt
= TREE_VEC_ELT (newargs
, i
);
3321 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3323 if (! template_args_equal (ot
, nt
))
3329 /* Given class template name and parameter list, produce a user-friendly name
3330 for the instantiation. */
3333 mangle_class_name_for_template (name
, parms
, arglist
)
3335 tree parms
, arglist
;
3337 static struct obstack scratch_obstack
;
3338 static char *scratch_firstobj
;
3341 if (!scratch_firstobj
)
3342 gcc_obstack_init (&scratch_obstack
);
3344 obstack_free (&scratch_obstack
, scratch_firstobj
);
3345 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3347 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3348 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3352 nparms
= TREE_VEC_LENGTH (parms
);
3353 arglist
= innermost_args (arglist
);
3354 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3355 for (i
= 0; i
< nparms
; i
++)
3357 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3358 tree arg
= TREE_VEC_ELT (arglist
, i
);
3363 if (TREE_CODE (parm
) == TYPE_DECL
)
3365 cat (type_as_string_real (arg
, 0, 1));
3368 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3370 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3372 /* Already substituted with real template. Just output
3373 the template name here */
3374 tree context
= DECL_CONTEXT (arg
);
3377 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
, 980422);
3378 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3381 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3384 /* Output the parameter declaration */
3385 cat (type_as_string_real (arg
, 0, 1));
3389 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3391 if (TREE_CODE (arg
) == TREE_LIST
)
3393 /* New list cell was built because old chain link was in
3395 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3396 arg
= TREE_VALUE (arg
);
3398 /* No need to check arglist against parmlist here; we did that
3399 in coerce_template_parms, called from lookup_template_class. */
3400 cat (expr_as_string (arg
, 0));
3403 char *bufp
= obstack_next_free (&scratch_obstack
);
3405 while (bufp
[offset
- 1] == ' ')
3407 obstack_blank_fast (&scratch_obstack
, offset
);
3409 /* B<C<char> >, not B<C<char>> */
3410 if (bufp
[offset
- 1] == '>')
3415 return (char *) obstack_base (&scratch_obstack
);
3419 classtype_mangled_name (t
)
3422 if (CLASSTYPE_TEMPLATE_INFO (t
)
3423 /* Specializations have already had their names set up in
3424 lookup_template_class. */
3425 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3427 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3429 /* For non-primary templates, the template parameters are
3430 implicit from their surrounding context. */
3431 if (PRIMARY_TEMPLATE_P (tmpl
))
3433 tree name
= DECL_NAME (tmpl
);
3434 char *mangled_name
= mangle_class_name_for_template
3435 (IDENTIFIER_POINTER (name
),
3436 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3437 CLASSTYPE_TI_ARGS (t
));
3438 tree id
= get_identifier (mangled_name
);
3439 IDENTIFIER_TEMPLATE (id
) = name
;
3444 return TYPE_IDENTIFIER (t
);
3448 add_pending_template (d
)
3453 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3454 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3456 ti
= DECL_TEMPLATE_INFO (d
);
3458 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3461 *template_tail
= perm_tree_cons
3462 (build_srcloc_here (), d
, NULL_TREE
);
3463 template_tail
= &TREE_CHAIN (*template_tail
);
3464 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3468 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3469 may be either a _DECL or an overloaded function or an
3470 IDENTIFIER_NODE), and ARGLIST. */
3473 lookup_template_function (fns
, arglist
)
3478 if (fns
== NULL_TREE
)
3480 cp_error ("non-template used as template");
3481 return error_mark_node
;
3484 type
= TREE_TYPE (fns
);
3485 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3486 type
= unknown_type_node
;
3488 if (processing_template_decl
)
3489 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3491 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3494 /* Within the scope of a template class S<T>, the name S gets bound
3495 (in build_self_reference) to a TYPE_DECL for the class, not a
3496 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3497 or one of its enclosing classes, and that type is a template,
3498 return the associated TEMPLATE_DECL. Otherwise, the original
3499 DECL is returned. */
3502 maybe_get_template_decl_from_type_decl (decl
)
3505 return (decl
!= NULL_TREE
3506 && TREE_CODE (decl
) == TYPE_DECL
3507 && DECL_ARTIFICIAL (decl
)
3508 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3509 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3512 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3513 parameters, find the desired type.
3515 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3516 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3517 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3518 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3519 copy it here to keep it from being reclaimed when the decl storage
3522 IN_DECL, if non-NULL, is the template declaration we are trying to
3525 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3526 the class we are looking up.
3528 If the template class is really a local class in a template
3529 function, then the FUNCTION_CONTEXT is the function in which it is
3530 being instantiated. */
3533 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3539 tree
template = NULL_TREE
, parmlist
;
3542 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3544 if (IDENTIFIER_VALUE (d1
)
3545 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3546 template = IDENTIFIER_VALUE (d1
);
3550 push_decl_namespace (context
);
3551 if (current_class_type
!= NULL_TREE
)
3553 maybe_get_template_decl_from_type_decl
3554 (IDENTIFIER_CLASS_VALUE (d1
));
3555 if (template == NULL_TREE
)
3556 template = lookup_name_nonclass (d1
);
3558 pop_decl_namespace ();
3561 context
= DECL_CONTEXT (template);
3563 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3565 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)))
3567 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
3568 d1
= DECL_NAME (template);
3571 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3572 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3573 && IS_AGGR_TYPE (d1
)))
3575 template = TYPE_TI_TEMPLATE (d1
);
3576 d1
= DECL_NAME (template);
3578 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3579 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3582 d1
= DECL_NAME (template);
3583 context
= DECL_CONTEXT (template);
3586 my_friendly_abort (272);
3588 /* With something like `template <class T> class X class X { ... };'
3589 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3590 We don't want to do that, but we have to deal with the situation,
3591 so let's give them some syntax errors to chew on instead of a
3595 cp_error ("`%T' is not a template", d1
);
3596 return error_mark_node
;
3599 if (context
== NULL_TREE
)
3600 context
= global_namespace
;
3602 if (TREE_CODE (template) != TEMPLATE_DECL
)
3604 cp_error ("non-template type `%T' used as a template", d1
);
3606 cp_error_at ("for template declaration `%D'", in_decl
);
3607 return error_mark_node
;
3610 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3612 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3613 template arguments */
3615 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3616 tree template2
= TYPE_STUB_DECL (parm
);
3619 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3621 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3622 if (arglist2
== error_mark_node
)
3623 return error_mark_node
;
3625 arglist2
= copy_to_permanent (arglist2
);
3626 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3627 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
3628 TYPE_SIZE (parm
) = 0;
3633 tree template_type
= TREE_TYPE (template);
3635 tree found
= NULL_TREE
;
3638 int is_partial_instantiation
;
3640 template = most_general_template (template);
3641 parmlist
= DECL_TEMPLATE_PARMS (template);
3642 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3643 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3645 /* We build up the coerced arguments and such on the
3646 momentary_obstack. */
3649 if (arg_depth
== 1 && parm_depth
> 1)
3651 /* We've been given an incomplete set of template arguments.
3654 template <class T> struct S1 {
3655 template <class U> struct S2 {};
3656 template <class U> struct S2<U*> {};
3659 we will be called with an ARGLIST of `U*', but the
3660 TEMPLATE will be `template <class T> template
3661 <class U> struct S1<T>::S2'. We must fill in the missing
3663 my_friendly_assert (context
!= NULL_TREE
, 0);
3664 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context
))
3665 && context
!= global_namespace
)
3666 context
= DECL_REAL_CONTEXT (context
);
3668 if (context
== global_namespace
)
3669 /* This is bad. We cannot get enough arguments, even from
3670 the surrounding context, to resolve this class. One
3671 case where this might happen is (illegal) code like:
3679 We should catch this error sooner (at the opening curly
3680 for `S', but it is better to be safe than sorry here. */
3682 cp_error ("invalid use of `%D'", template);
3683 return error_mark_node
;
3686 arglist
= add_to_template_args (TYPE_TI_ARGS (context
),
3688 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3691 my_friendly_assert (parm_depth
== arg_depth
, 0);
3693 /* Calculate the BOUND_ARGS. These will be the args that are
3694 actually tsubst'd into the definition to create the
3698 /* We have multiple levels of arguments to coerce, at once. */
3700 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3702 tree bound_args
= make_temp_vec (parm_depth
);
3704 for (i
= saved_depth
,
3705 t
= DECL_TEMPLATE_PARMS (template);
3706 i
> 0 && t
!= NULL_TREE
;
3707 --i
, t
= TREE_CHAIN (t
))
3709 tree a
= coerce_template_parms (TREE_VALUE (t
),
3710 arglist
, template, 1, 1);
3711 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3713 /* We temporarily reduce the length of the ARGLIST so
3714 that coerce_template_parms will see only the arguments
3715 corresponding to the template parameters it is
3717 TREE_VEC_LENGTH (arglist
)--;
3720 /* Restore the ARGLIST to its full size. */
3721 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3723 arglist
= bound_args
;
3727 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3728 innermost_args (arglist
),
3731 if (arglist
== error_mark_node
)
3732 /* We were unable to bind the arguments. */
3733 return error_mark_node
;
3735 /* In the scope of a template class, explicit references to the
3736 template class refer to the type of the template, not any
3737 instantiation of it. For example, in:
3739 template <class T> class C { void f(C<T>); }
3741 the `C<T>' is just the same as `C'. Outside of the
3742 class, however, such a reference is an instantiation. */
3743 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3746 found
= template_type
;
3748 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3752 /* Note that we use DECL_CONTEXT, rather than
3753 CP_DECL_CONTEXT, so that the termination test is
3754 always just `ctx'. We're not interested in namepace
3756 for (ctx
= current_class_type
;
3758 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3759 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3760 if (same_type_p (ctx
, template_type
))
3764 /* We're not in the scope of the class, so the
3765 TEMPLATE_TYPE is not the type we want after
3773 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3774 found
; found
= TREE_CHAIN (found
))
3775 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3779 found
= TREE_VALUE (found
);
3788 /* Since we didn't find the type, we'll have to create it.
3789 Since we'll be saving this type on the
3790 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3791 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3793 /* This type is a "partial instantiation" if any of the template
3794 arguments still inolve template parameters. Note that we set
3795 IS_PARTIAL_INSTANTIATION for partial specializations as
3797 is_partial_instantiation
= uses_template_parms (arglist
);
3799 /* Create the type. */
3800 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3802 if (!is_partial_instantiation
)
3803 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3805 /* We don't want to call start_enum for this type, since
3806 the values for the enumeration constants may involve
3807 template parameters. And, no one should be interested
3808 in the enumeration constants for such a type. */
3809 t
= make_node (ENUMERAL_TYPE
);
3813 t
= make_lang_type (TREE_CODE (template_type
));
3814 CLASSTYPE_DECLARED_CLASS (t
)
3815 = CLASSTYPE_DECLARED_CLASS (template_type
);
3816 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3817 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3818 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3821 /* If we called start_enum above, this information will already
3825 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3827 /* Create a stub TYPE_DECL for it. */
3828 type_decl
= build_decl (TYPE_DECL
, DECL_NAME (template), t
);
3829 SET_DECL_ARTIFICIAL (type_decl
);
3830 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3831 DECL_SOURCE_FILE (type_decl
)
3832 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
3833 DECL_SOURCE_LINE (type_decl
)
3834 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
3835 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = type_decl
;
3838 type_decl
= TYPE_NAME (t
);
3840 /* Set up the template information. We have to figure out which
3841 template is the immediate parent if this is a full
3843 if (parm_depth
== 1 || is_partial_instantiation
3844 || !PRIMARY_TEMPLATE_P (template))
3845 /* This case is easy; there are no member templates involved. */
3849 /* This is a full instantiation of a member template. There
3850 should be some partial instantiation of which this is an
3853 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3854 found
; found
= TREE_CHAIN (found
))
3857 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
3859 /* We only want partial instantiations, here, not
3860 specializations or full instantiations. */
3861 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
3862 || !uses_template_parms (TREE_VALUE (found
)))
3865 /* Temporarily reduce by one the number of levels in the
3866 ARGLIST and in FOUND so as to avoid comparing the
3867 last set of arguments. */
3868 TREE_VEC_LENGTH (arglist
)--;
3869 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
3871 /* See if the arguments match. If they do, then TMPL is
3872 the partial instantiation we want. */
3873 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
3875 /* Restore the argument vectors to their full size. */
3876 TREE_VEC_LENGTH (arglist
)++;
3877 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
3887 my_friendly_abort (0);
3890 arglist
= copy_to_permanent (arglist
);
3891 SET_TYPE_TEMPLATE_INFO (t
,
3892 tree_cons (found
, arglist
, NULL_TREE
));
3893 DECL_TEMPLATE_INSTANTIATIONS (template)
3894 = tree_cons (arglist
, t
,
3895 DECL_TEMPLATE_INSTANTIATIONS (template));
3897 if (TREE_CODE (t
) == ENUMERAL_TYPE
3898 && !is_partial_instantiation
)
3899 /* Now that the type has been registered on the instantiations
3900 list, we set up the enumerators. Because the enumeration
3901 constants may involve the enumeration type itself, we make
3902 sure to register the type first, and then create the
3903 constants. That way, doing tsubst_expr for the enumeration
3904 constants won't result in recursive calls here; we'll find
3905 the instantiation and exit above. */
3906 tsubst_enum (template_type
, t
, arglist
);
3908 /* We're done with the permanent obstack, now. */
3910 /* We're also done with the momentary allocation we started
3914 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3916 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
3917 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
3918 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
3919 if (!is_partial_instantiation
)
3921 DECL_ASSEMBLER_NAME (type_decl
)
3922 = get_identifier (build_overload_name (t
, 1, 1));
3924 /* For backwards compatibility; code that uses
3925 -fexternal-templates expects looking up a template to
3926 instantiate it. I think DDD still relies on this.
3927 (jason 8/20/1998) */
3928 if (TREE_CODE (t
) != ENUMERAL_TYPE
3929 && flag_external_templates
3930 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3931 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3932 add_pending_template (t
);
3935 /* If the type makes use of template parameters, the
3936 code that generates debugging information will crash. */
3937 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
3943 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3944 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3945 If FN returns non-zero, the iteration is terminated, and
3946 for_each_template_parm returns 1. Otherwise, the iteration
3947 continues. If FN never returns a non-zero value, the value
3948 returned by for_each_template_parm is 0. If FN is NULL, it is
3949 considered to be the function which always returns 1. */
3952 for_each_template_parm (t
, fn
, data
)
3960 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
3961 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
3964 switch (TREE_CODE (t
))
3968 /* We assume that the object must be instantiated in order to build
3969 the COMPONENT_REF, so we test only whether the type of the
3970 COMPONENT_REF uses template parms. */
3971 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3974 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
3975 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
3977 case IDENTIFIER_NODE
:
3978 if (!IDENTIFIER_TEMPLATE (t
))
3980 my_friendly_abort (42);
3982 /* aggregates of tree nodes */
3985 int i
= TREE_VEC_LENGTH (t
);
3987 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
3992 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
3993 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
3995 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
3998 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
4000 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
4002 /* constructed type nodes */
4004 case REFERENCE_TYPE
:
4005 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4008 if (TYPE_PTRMEMFUNC_FLAG (t
))
4009 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
4015 if (! TYPE_TEMPLATE_INFO (t
))
4017 return for_each_template_parm (TREE_VALUE
4018 (TYPE_TEMPLATE_INFO (t
)),
4021 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4026 /* Check the parameter types. Since default arguments are not
4027 instantiated until they are needed, the TYPE_ARG_TYPES may
4028 contain expressions that involve template parameters. But,
4029 no-one should be looking at them yet. And, once they're
4030 instantiated, they don't contain template parameters, so
4031 there's no point in looking at them then, either. */
4035 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4036 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4040 /* Check the return type, too. */
4041 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4044 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
4046 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4048 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
4050 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4054 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4057 /* A template template parameter is encountered */
4058 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4059 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4060 /* Already substituted template template parameter */
4064 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
4066 goto check_type_and_context
;
4070 /* ??? What about FIELD_DECLs? */
4071 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4072 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4076 check_type_and_context
:
4077 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4079 if (DECL_CONTEXT (t
)
4080 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4085 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4086 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4089 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4091 /* template parm nodes */
4092 case TEMPLATE_TEMPLATE_PARM
:
4093 /* Record template parameters such as `T' inside `TT<T>'. */
4094 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
4095 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4097 case TEMPLATE_TYPE_PARM
:
4098 case TEMPLATE_PARM_INDEX
:
4100 return (*fn
)(t
, data
);
4104 /* simple type nodes */
4106 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
4108 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
4114 case NAMESPACE_DECL
:
4124 /* Non-error_mark_node ERROR_MARKs are bad things. */
4125 my_friendly_assert (t
== error_mark_node
, 274);
4134 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4137 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4140 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4141 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4142 (TREE_TYPE (t
)), fn
, data
);
4143 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
4147 case REINTERPRET_CAST_EXPR
:
4148 case CONST_CAST_EXPR
:
4149 case STATIC_CAST_EXPR
:
4150 case DYNAMIC_CAST_EXPR
:
4158 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4161 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
4169 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
4170 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
4177 sorry ("testing %s for template parms",
4178 tree_code_name
[(int) TREE_CODE (t
)]);
4179 my_friendly_abort (82);
4186 uses_template_parms (t
)
4189 return for_each_template_parm (t
, 0, 0);
4192 static struct tinst_level
*current_tinst_level
;
4193 static struct tinst_level
*free_tinst_level
;
4194 static int tinst_depth
;
4195 extern int max_tinst_depth
;
4196 #ifdef GATHER_STATISTICS
4199 int tinst_level_tick
;
4200 int last_template_error_tick
;
4202 /* Print out all the template instantiations that we are currently
4203 working on. If ERR, we are being called from cp_thing, so do
4204 the right thing for an error message. */
4207 print_template_context (err
)
4210 struct tinst_level
*p
= current_tinst_level
;
4212 char *file
= input_filename
;
4216 if (current_function_decl
!= p
->decl
4217 && current_function_decl
!= NULL_TREE
)
4218 /* We can get here during the processing of some synthesized
4219 method. Then, p->decl will be the function that's causing
4224 if (current_function_decl
== p
->decl
)
4225 /* Avoid redundancy with the the "In function" line. */;
4227 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4228 file
, decl_as_string (p
->decl
, 0));
4236 for (; p
; p
= p
->next
)
4238 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4239 decl_as_string (p
->decl
, 0));
4243 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4246 /* Called from cp_thing to print the template context for an error. */
4249 maybe_print_template_context ()
4251 if (last_template_error_tick
== tinst_level_tick
4252 || current_tinst_level
== 0)
4255 last_template_error_tick
= tinst_level_tick
;
4256 print_template_context (1);
4260 push_tinst_level (d
)
4263 struct tinst_level
*new;
4265 if (tinst_depth
>= max_tinst_depth
)
4267 /* If the instantiation in question still has unbound template parms,
4268 we don't really care if we can't instantiate it, so just return.
4269 This happens with base instantiation for implicit `typename'. */
4270 if (uses_template_parms (d
))
4273 last_template_error_tick
= tinst_level_tick
;
4274 error ("template instantiation depth exceeds maximum of %d",
4276 error (" (use -ftemplate-depth-NN to increase the maximum)");
4277 cp_error (" instantiating `%D'", d
);
4279 print_template_context (0);
4284 if (free_tinst_level
)
4286 new = free_tinst_level
;
4287 free_tinst_level
= new->next
;
4290 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4294 new->file
= input_filename
;
4295 new->next
= current_tinst_level
;
4296 current_tinst_level
= new;
4299 #ifdef GATHER_STATISTICS
4300 if (tinst_depth
> depth_reached
)
4301 depth_reached
= tinst_depth
;
4311 struct tinst_level
*old
= current_tinst_level
;
4313 /* Restore the filename and line number stashed away when we started
4314 this instantiation. */
4316 input_filename
= old
->file
;
4317 extract_interface_info ();
4319 current_tinst_level
= old
->next
;
4320 old
->next
= free_tinst_level
;
4321 free_tinst_level
= old
;
4326 struct tinst_level
*
4329 struct tinst_level
*p
= current_tinst_level
;
4332 for (; p
->next
; p
= p
->next
)
4337 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4338 vector of template arguments, as for tsubst.
4340 Returns an appropriate tsbust'd friend declaration. */
4343 tsubst_friend_function (decl
, args
)
4349 char *file
= input_filename
;
4351 lineno
= DECL_SOURCE_LINE (decl
);
4352 input_filename
= DECL_SOURCE_FILE (decl
);
4354 if (TREE_CODE (decl
) == FUNCTION_DECL
4355 && DECL_TEMPLATE_INSTANTIATION (decl
)
4356 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4357 /* This was a friend declared with an explicit template
4358 argument list, e.g.:
4362 to indicate that f was a template instantiation, not a new
4363 function declaration. Now, we have to figure out what
4364 instantiation of what template. */
4371 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4372 args
, /*complain=*/1,
4374 tsubst (DECL_TI_ARGS (decl
),
4375 args
, /*complain=*/1,
4377 /* FIXME: The decl we create via the next tsubst could be
4378 created on a temporary obstack. */
4379 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4380 tmpl
= determine_specialization (template_id
, new_friend
,
4382 /*need_member_template=*/0);
4383 new_friend
= instantiate_template (tmpl
, new_args
);
4387 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4389 /* The NEW_FRIEND will look like an instantiation, to the
4390 compiler, but is not an instantiation from the point of view of
4391 the language. For example, we might have had:
4393 template <class T> struct S {
4394 template <class U> friend void f(T, U);
4397 Then, in S<int>, template <class U> void f(int, U) is not an
4398 instantiation of anything. */
4399 DECL_USE_TEMPLATE (new_friend
) = 0;
4400 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4401 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4403 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4404 tsubst will have resulted in a call to
4405 set_mangled_name_for_template_decl. But, the function is not a
4406 template instantiation and should not be mangled like one.
4407 Therefore, we remangle the function name. We don't have to do
4408 this if the NEW_FRIEND is a template since
4409 set_mangled_name_for_template_decl doesn't do anything if the
4410 function declaration still uses template arguments. */
4411 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4413 set_mangled_name_for_decl (new_friend
);
4414 DECL_RTL (new_friend
) = 0;
4415 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4418 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4421 tree new_friend_template_info
;
4422 tree new_friend_result_template_info
;
4423 int new_friend_is_defn
;
4425 /* We must save some information from NEW_FRIEND before calling
4426 duplicate decls since that function will free NEW_FRIEND if
4428 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4429 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4431 /* This declaration is a `primary' template. */
4432 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4435 = DECL_INITIAL (DECL_RESULT (new_friend
)) != NULL_TREE
;
4436 new_friend_result_template_info
4437 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend
));
4441 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4442 new_friend_result_template_info
= NULL_TREE
;
4445 old_decl
= pushdecl_namespace_level (new_friend
);
4447 if (old_decl
!= new_friend
)
4449 /* This new friend declaration matched an existing
4450 declaration. For example, given:
4452 template <class T> void f(T);
4453 template <class U> class C {
4454 template <class T> friend void f(T) {}
4457 the friend declaration actually provides the definition
4458 of `f', once C has been instantiated for some type. So,
4459 old_decl will be the out-of-class template declaration,
4460 while new_friend is the in-class definition.
4462 But, if `f' was called before this point, the
4463 instantiation of `f' will have DECL_TI_ARGS corresponding
4464 to `T' but not to `U', references to which might appear
4465 in the definition of `f'. Previously, the most general
4466 template for an instantiation of `f' was the out-of-class
4467 version; now it is the in-class version. Therefore, we
4468 run through all specialization of `f', adding to their
4469 DECL_TI_ARGS appropriately. In particular, they need a
4470 new set of outer arguments, corresponding to the
4471 arguments for this class instantiation.
4473 The same situation can arise with something like this:
4476 template <class T> class C {
4480 when `C<int>' is instantiated. Now, `f(int)' is defined
4483 if (!new_friend_is_defn
)
4484 /* On the other hand, if the in-class declaration does
4485 *not* provide a definition, then we don't want to alter
4486 existing definitions. We can just leave everything
4491 /* Overwrite whatever template info was there before, if
4492 any, with the new template information pertaining to
4494 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4496 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4497 /* duplicate_decls will take care of this case. */
4502 tree new_friend_args
;
4504 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl
))
4505 = new_friend_result_template_info
;
4507 new_friend_args
= TI_ARGS (new_friend_template_info
);
4508 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4512 tree spec
= TREE_VALUE (t
);
4515 = add_outermost_template_args (new_friend_args
,
4516 DECL_TI_ARGS (spec
));
4518 = copy_to_permanent (DECL_TI_ARGS (spec
));
4521 /* Now, since specializations are always supposed to
4522 hang off of the most general template, we must move
4524 t
= most_general_template (old_decl
);
4527 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4528 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4529 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4530 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4535 /* The information from NEW_FRIEND has been merged into OLD_DECL
4536 by duplicate_decls. */
4537 new_friend
= old_decl
;
4540 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4542 /* Check to see that the declaration is really present, and,
4543 possibly obtain an improved declaration. */
4544 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4553 input_filename
= file
;
4557 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4558 template arguments, as for tsubst.
4560 Returns an appropriate tsbust'd friend type. */
4563 tsubst_friend_class (friend_tmpl
, args
)
4568 tree tmpl
= lookup_name (DECL_NAME (friend_tmpl
), 1);
4570 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4572 if (tmpl
!= NULL_TREE
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4574 /* The friend template has already been declared. Just
4575 check to see that the declarations match, and install any new
4576 default parameters. We must tsubst the default parameters,
4577 of course. We only need the innermost template parameters
4578 because that is all that redeclare_class_template will look
4581 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4582 args
, /*complain=*/1);
4583 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4584 friend_type
= TREE_TYPE (tmpl
);
4588 /* The friend template has not already been declared. In this
4589 case, the instantiation of the template class will cause the
4590 injection of this template into the global scope. */
4591 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4593 /* The new TMPL is not an instantiation of anything, so we
4594 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4595 the new type because that is supposed to be the corresponding
4596 template decl, i.e., TMPL. */
4597 DECL_USE_TEMPLATE (tmpl
) = 0;
4598 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4599 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4601 /* Inject this template into the global scope. */
4602 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4609 instantiate_class_template (type
)
4612 tree
template, args
, pattern
, t
;
4615 if (type
== error_mark_node
)
4616 return error_mark_node
;
4618 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4621 /* We want to allocate temporary vectors of template arguments and
4622 template argument expressions on the momentary obstack, not on
4623 the expression obstack. Otherwise, all the space allocated in
4624 argument coercion and such is simply lost. */
4627 /* Figure out which template is being instantiated. */
4628 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4629 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4631 /* Figure out which arguments are being used to do the
4633 args
= CLASSTYPE_TI_ARGS (type
);
4634 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4636 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4637 /* If this is a partial instantiation, then we can't instantiate
4638 the type; there's no telling whether or not one of the
4639 template parameters might eventually be instantiated to some
4640 value that results in a specialization being used. For
4652 Now, the `S<U>' in `f<int>' is the specialization, not an
4653 instantiation of the original template. */
4656 /* Determine what specialization of the original template to
4658 if (PARTIAL_INSTANTIATION_P (type
))
4659 /* There's no telling which specialization is appropriate at this
4660 point. Since all peeking at the innards of this partial
4661 instantiation are extensions (like the "implicit typename"
4662 extension, which allows users to omit the keyword `typename' on
4663 names that are declared as types in template base classes), we
4664 are free to do what we please.
4666 Trying to figure out which partial instantiation to use can
4667 cause a crash. (Some of the template arguments don't even have
4668 types.) So, we just use the most general version. */
4672 t
= most_specialized_class (template, args
);
4674 if (t
== error_mark_node
)
4676 const char *str
= "candidates are:";
4677 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4678 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4681 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4684 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4688 TYPE_BEING_DEFINED (type
) = 1;
4689 type
= error_mark_node
;
4695 pattern
= TREE_TYPE (t
);
4697 pattern
= TREE_TYPE (template);
4699 /* If the template we're instantiating is incomplete, then clearly
4700 there's nothing we can do. */
4701 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4704 /* If this is a partial instantiation, don't tsubst anything. We will
4705 only use this type for implicit typename, so the actual contents don't
4706 matter. All that matters is whether a particular name is a type. */
4707 if (PARTIAL_INSTANTIATION_P (type
))
4709 /* The fields set here must be kept in sync with those cleared
4710 in begin_class_definition. */
4711 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4712 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4713 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4714 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4715 /* Pretend that the type is complete, so that we will look
4716 inside it during name lookup and such. */
4717 TYPE_SIZE (type
) = integer_zero_node
;
4721 /* If we've recursively instantiated too many templates, stop. */
4722 if (! push_tinst_level (type
))
4725 /* Now we're really doing the instantiation. Mark the type as in
4726 the process of being defined. */
4727 TYPE_BEING_DEFINED (type
) = 1;
4729 maybe_push_to_top_level (uses_template_parms (type
));
4730 pushclass (type
, 0);
4734 /* This TYPE is actually a instantiation of of a partial
4735 specialization. We replace the innermost set of ARGS with
4736 the arguments appropriate for substitution. For example,
4739 template <class T> struct S {};
4740 template <class T> struct S<T*> {};
4742 and supposing that we are instantiating S<int*>, ARGS will
4743 present be {int*} but we need {int}. */
4745 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4748 /* If there were multiple levels in ARGS, replacing the
4749 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4750 want, so we make a copy first. */
4751 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4753 args
= copy_node (args
);
4754 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4760 if (flag_external_templates
)
4762 if (flag_alt_external_templates
)
4764 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4765 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4766 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4767 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4768 && CLASSTYPE_INTERFACE_KNOWN (type
));
4772 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4773 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4774 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4775 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4776 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4777 && CLASSTYPE_INTERFACE_KNOWN (type
));
4782 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4783 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4786 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4787 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4788 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4789 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4790 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4791 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4792 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4793 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4794 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4795 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4796 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
4797 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
4798 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
4799 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
4800 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
4801 TYPE_USES_COMPLEX_INHERITANCE (type
)
4802 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
4803 TYPE_USES_MULTIPLE_INHERITANCE (type
)
4804 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
4805 TYPE_USES_VIRTUAL_BASECLASSES (type
)
4806 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
4807 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
4808 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
4809 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
4811 /* We must copy the arguments to the permanent obstack since
4812 during the tsubst'ing below they may wind up in the
4813 DECL_TI_ARGS of some instantiated member template. */
4814 args
= copy_to_permanent (args
);
4816 if (TYPE_BINFO_BASETYPES (pattern
))
4818 tree base_list
= NULL_TREE
;
4819 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
4822 /* Substitute into each of the bases to determine the actual
4824 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
4830 pbase
= TREE_VEC_ELT (pbases
, i
);
4832 /* Substitue to figure out the base class. */
4833 base
= tsubst (BINFO_TYPE (pbase
), args
,
4834 /*complain=*/1, NULL_TREE
);
4835 if (base
== error_mark_node
)
4838 /* Calculate the correct access node. */
4839 if (TREE_VIA_VIRTUAL (pbase
))
4841 if (TREE_VIA_PUBLIC (pbase
))
4842 access
= access_public_virtual_node
;
4843 else if (TREE_VIA_PROTECTED (pbase
))
4844 access
= access_protected_virtual_node
;
4846 access
= access_private_virtual_node
;
4850 if (TREE_VIA_PUBLIC (pbase
))
4851 access
= access_public_node
;
4852 else if (TREE_VIA_PROTECTED (pbase
))
4853 access
= access_protected_node
;
4855 access
= access_private_node
;
4858 base_list
= tree_cons (access
, base
, base_list
);
4861 /* The list is now in reverse order; correct that. */
4862 base_list
= nreverse (base_list
);
4864 /* Now call xref_basetypes to set up all the base-class
4866 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
4867 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
4868 ? class_type_node
: record_type_node
)
4870 DECL_NAME (TYPE_NAME (pattern
)),
4875 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
4877 tree tag
= TREE_VALUE (t
);
4878 tree name
= TYPE_IDENTIFIER (tag
);
4881 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
4882 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
4884 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
4885 /* Unfortunately, lookup_template_class sets
4886 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4887 instantiation (i.e., for the type of a member template
4888 class nested within a template class.) This behavior is
4889 required for maybe_process_partial_specialization to work
4890 correctly, but is not accurate in this case; the TAG is not
4891 an instantiation of anything. (The corresponding
4892 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4893 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
4895 /* Now, we call pushtag to put this NEWTAG into the scope of
4896 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4897 pushtag calling push_template_decl. We don't have to do
4898 this for enums because it will already have been done in
4901 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
4902 pushtag (name
, newtag
, /*globalize=*/0);
4906 /* Don't replace enum constants here. */
4907 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
4908 if (TREE_CODE (t
) != CONST_DECL
)
4912 /* The the file and line for this declaration, to assist in
4913 error message reporting. Since we called push_tinst_level
4914 above, we don't need to restore these. */
4915 lineno
= DECL_SOURCE_LINE (t
);
4916 input_filename
= DECL_SOURCE_FILE (t
);
4918 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
4919 if (TREE_CODE (r
) == VAR_DECL
)
4921 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
4922 /* Perhaps we should do more of grokfield here. */
4923 if (DECL_DEFINED_IN_CLASS_P (r
))
4924 /* Set up DECL_INITIAL, since tsubst doesn't. */
4925 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
4926 /*complain=*/1, NULL_TREE
);
4928 DECL_IN_AGGR_P (r
) = 1;
4929 DECL_EXTERNAL (r
) = 1;
4930 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
4931 if (DECL_DEFINED_IN_CLASS_P (r
))
4932 check_static_variable_definition (r
, TREE_TYPE (r
));
4935 /* R will have a TREE_CHAIN if and only if it has already been
4936 processed by finish_member_declaration. This can happen
4937 if, for example, it is a TYPE_DECL for a class-scoped
4938 ENUMERAL_TYPE; such a thing will already have been added to
4939 the field list by tsubst_enum above. */
4940 if (!TREE_CHAIN (r
))
4942 set_current_access_from_decl (r
);
4943 finish_member_declaration (r
);
4947 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4948 for this instantiation. */
4949 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
4951 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
4952 set_current_access_from_decl (r
);
4953 finish_member_declaration (r
);
4956 /* Construct the DECL_FRIENDLIST for the new class type. */
4957 typedecl
= TYPE_MAIN_DECL (type
);
4958 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
4964 for (friends
= TREE_VALUE (t
);
4965 friends
!= NULL_TREE
;
4966 friends
= TREE_CHAIN (friends
))
4967 if (TREE_PURPOSE (friends
) == error_mark_node
)
4969 tsubst_friend_function (TREE_VALUE (friends
),
4973 tsubst_copy (TREE_PURPOSE (t
), args
,
4974 /*complain=*/1, NULL_TREE
),
4975 tsubst (TREE_PURPOSE (friends
), args
,
4976 /*complain=*/1, NULL_TREE
));
4979 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
4983 tree friend_type
= TREE_VALUE (t
);
4984 tree new_friend_type
;
4986 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4987 new_friend_type
= tsubst_friend_class (friend_type
, args
);
4988 else if (uses_template_parms (friend_type
))
4989 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
4992 /* The call to xref_tag_from_type does injection for friend
4995 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
4998 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4999 /* Trick make_friend_class into realizing that the friend
5000 we're adding is a template, not an ordinary class. It's
5001 important that we use make_friend_class since it will
5002 perform some error-checking and output cross-reference
5004 ++processing_template_decl
;
5006 make_friend_class (type
, new_friend_type
);
5008 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5009 --processing_template_decl
;
5012 /* This does injection for friend functions. */
5013 if (!processing_template_decl
)
5015 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
5016 /*complain=*/1, NULL_TREE
);
5018 for (; t
; t
= TREE_CHAIN (t
))
5020 tree d
= TREE_VALUE (t
);
5022 if (TREE_CODE (d
) == TYPE_DECL
)
5023 /* Already injected. */;
5029 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5030 if (TREE_CODE (t
) == FIELD_DECL
)
5032 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5033 require_complete_type (t
);
5036 /* Set the file and line number information to whatever is given for
5037 the class itself. This puts error messages involving generated
5038 implicit functions at a predictable point, and the same point
5039 that would be used for non-template classes. */
5040 lineno
= DECL_SOURCE_LINE (typedecl
);
5041 input_filename
= DECL_SOURCE_FILE (typedecl
);
5043 unreverse_member_declarations (type
);
5044 type
= finish_struct_1 (type
, 0);
5045 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5047 /* Clear this now so repo_template_used is happy. */
5048 TYPE_BEING_DEFINED (type
) = 0;
5049 repo_template_used (type
);
5052 pop_from_top_level ();
5065 if (t1
== NULL_TREE
)
5066 return t2
== NULL_TREE
;
5067 if (t2
== NULL_TREE
)
5069 /* Don't care if one declares its arg const and the other doesn't -- the
5070 main variant of the arg type is all that matters. */
5071 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5072 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5074 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5077 /* If arg is a non-type template parameter that does not depend on template
5078 arguments, fold it like we weren't in the body of a template. */
5081 maybe_fold_nontype_arg (arg
)
5084 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
5085 && !uses_template_parms (arg
))
5087 /* Sometimes, one of the args was an expression involving a
5088 template constant parameter, like N - 1. Now that we've
5089 tsubst'd, we might have something like 2 - 1. This will
5090 confuse lookup_template_class, so we do constant folding
5091 here. We have to unset processing_template_decl, to
5092 fool build_expr_from_tree() into building an actual
5095 int saved_processing_template_decl
= processing_template_decl
;
5096 processing_template_decl
= 0;
5097 arg
= fold (build_expr_from_tree (arg
));
5098 processing_template_decl
= saved_processing_template_decl
;
5103 /* Return the TREE_VEC with the arguments for the innermost template header,
5104 where ARGS is either that or the VEC of VECs for all the
5108 innermost_args (args
)
5111 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
5114 /* Substitute ARGS into the vector of template arguments T. */
5117 tsubst_template_arg_vector (t
, args
, complain
)
5122 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5123 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5125 bzero ((char *) elts
, len
* sizeof (tree
));
5127 for (i
= 0; i
< len
; i
++)
5129 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5130 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5131 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5134 elts
[i
] = maybe_fold_nontype_arg
5135 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5138 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5145 t
= make_temp_vec (len
);
5146 for (i
= 0; i
< len
; i
++)
5147 TREE_VEC_ELT (t
, i
) = elts
[i
];
5152 /* Return the result of substituting ARGS into the template parameters
5153 given by PARMS. If there are m levels of ARGS and m + n levels of
5154 PARMS, then the result will contain n levels of PARMS. For
5155 example, if PARMS is `template <class T> template <class U>
5156 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5157 result will be `template <int*, double, class V>'. */
5160 tsubst_template_parms (parms
, args
, complain
)
5166 tree
* new_parms
= &r
;
5168 for (new_parms
= &r
;
5169 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5170 new_parms
= &(TREE_CHAIN (*new_parms
)),
5171 parms
= TREE_CHAIN (parms
))
5174 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5177 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5179 tree default_value
=
5180 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5182 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5184 TREE_VEC_ELT (new_vec
, i
)
5185 = build_tree_list (tsubst (default_value
, args
, complain
,
5187 tsubst (parm_decl
, args
, complain
,
5192 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5193 - TMPL_ARGS_DEPTH (args
))),
5194 new_vec
, NULL_TREE
);
5200 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5201 type T. If T is not an aggregate or enumeration type, it is
5202 handled as if by tsubst. IN_DECL is as for tsubst. If
5203 ENTERING_SCOPE is non-zero, T is the context for a template which
5204 we are presently tsubst'ing. Return the subsituted value. */
5207 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5217 switch (TREE_CODE (t
))
5220 if (TYPE_PTRMEMFUNC_P (t
))
5222 tree r
= build_ptrmemfunc_type
5223 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5224 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5227 /* else fall through */
5230 if (TYPE_TEMPLATE_INFO (t
))
5236 /* First, determine the context for the type we are looking
5238 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5239 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5241 in_decl
, /*entering_scope=*/1);
5243 context
= NULL_TREE
;
5245 /* Then, figure out what arguments are appropriate for the
5246 type we are trying to find. For example, given:
5248 template <class T> struct S;
5249 template <class T, class U> void f(T, U) { S<U> su; }
5251 and supposing that we are instantiating f<int, double>,
5252 then our ARGS will be {int, double}, but, when looking up
5253 S we only want {double}. */
5255 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5258 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5262 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5265 /* This is not a template type, so there's nothing to do. */
5269 return tsubst (t
, args
, complain
, in_decl
);
5273 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5274 (already computed) substitution of ARGS into TREE_TYPE (T), if
5275 appropriate. Return the result of the substitution. IN_DECL is as
5279 tsubst_decl (t
, args
, type
, in_decl
)
5286 char* saved_filename
;
5289 /* Set the filename and linenumber to improve error-reporting. */
5290 saved_lineno
= lineno
;
5291 saved_filename
= input_filename
;
5292 lineno
= DECL_SOURCE_LINE (t
);
5293 input_filename
= DECL_SOURCE_FILE (t
);
5295 switch (TREE_CODE (t
))
5299 /* We can get here when processing a member template function
5300 of a template class. */
5301 tree decl
= DECL_TEMPLATE_RESULT (t
);
5303 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5305 if (!is_template_template_parm
)
5307 /* We might already have an instance of this template.
5308 The ARGS are for the surrounding class type, so the
5309 full args contain the tsubst'd args for the context,
5310 plus the innermost args from the template decl. */
5311 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5312 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5313 : DECL_TI_ARGS (DECL_RESULT (t
));
5317 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5320 /* tsubst_template_arg_vector doesn't copy the vector if
5321 nothing changed. But, *something* should have
5323 my_friendly_assert (full_args
!= tmpl_args
, 0);
5325 spec
= retrieve_specialization (t
, full_args
);
5327 if (spec
!= NULL_TREE
)
5334 /* Make a new template decl. It will be similar to the
5335 original, but will record the current template arguments.
5336 We also create a new function declaration, which is just
5337 like the old one, but points to this new template, rather
5338 than the old one. */
5341 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5342 TREE_CHAIN (r
) = NULL_TREE
;
5344 if (is_template_template_parm
)
5346 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5347 DECL_RESULT (r
) = new_decl
;
5348 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5353 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1,
5354 in_decl
, /*entering_scope=*/1);
5355 DECL_CLASS_CONTEXT (r
)
5356 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5357 /*complain=*/1, in_decl
,
5358 /*entering_scope=*/1);
5359 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5361 if (TREE_CODE (decl
) == TYPE_DECL
)
5363 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5364 /*complain=*/1, in_decl
);
5365 TREE_TYPE (r
) = new_type
;
5366 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5367 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5368 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5372 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5373 DECL_RESULT (r
) = new_decl
;
5374 DECL_TI_TEMPLATE (new_decl
) = r
;
5375 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5376 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5379 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5380 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5381 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5383 /* The template parameters for this new template are all the
5384 template parameters for the old template, except the
5385 outermost level of parameters. */
5386 DECL_TEMPLATE_PARMS (r
)
5387 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5390 if (PRIMARY_TEMPLATE_P (t
))
5391 DECL_PRIMARY_TEMPLATE (r
) = r
;
5393 /* We don't partially instantiate partial specializations. */
5394 if (TREE_CODE (decl
) == TYPE_DECL
)
5397 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5399 spec
= TREE_CHAIN (spec
))
5401 /* It helps to consider example here. Consider:
5412 Now, for example, we are instantiating S<int>::f(U u).
5413 We want to make a template:
5418 It will have a specialization, for the case U = int*, of
5422 void S<int>::f<int*>(int*);
5424 This specialization will be an instantiation of
5425 the specialization given in the declaration of S, with
5426 argument list int*. */
5428 tree fn
= TREE_VALUE (spec
);
5432 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5433 /* Instantiations are on the same list, but they're of
5434 no concern to us. */
5437 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5438 /* A full specialization. There's no need to record
5442 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
5443 /*complain=*/1, in_decl
);
5444 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5445 spec_args
, /*complain=*/1, in_decl
);
5446 DECL_TI_TEMPLATE (new_fn
) = fn
;
5447 register_specialization (new_fn
, r
,
5448 innermost_args (spec_args
));
5451 /* Record this partial instantiation. */
5452 register_specialization (r
, t
,
5453 DECL_TI_ARGS (DECL_RESULT (r
)));
5461 tree argvec
= NULL_TREE
;
5468 /* Nobody should be tsubst'ing into non-template functions. */
5469 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5471 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5475 /* Allocate template arguments on the momentary obstack,
5476 in case we don't need to keep them. */
5479 /* Calculate the most general template of which R is a
5480 specialization, and the complete set of arguments used to
5482 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5484 = tsubst_template_arg_vector (DECL_TI_ARGS
5485 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5486 args
, /*complain=*/1);
5488 /* Check to see if we already have this specialization. */
5489 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5498 /* We're going to need to keep the ARGVEC, so we copy it
5500 argvec
= copy_to_permanent (argvec
);
5503 /* Here, we deal with the peculiar case:
5505 template <class T> struct S {
5506 template <class U> friend void f();
5508 template <class U> friend void f() {}
5510 template void f<double>();
5512 Here, the ARGS for the instantiation of will be {int,
5513 double}. But, we only need as many ARGS as there are
5514 levels of template parameters in CODE_PATTERN. We are
5515 careful not to get fooled into reducing the ARGS in
5518 template <class T> struct S { template <class U> void f(U); }
5519 template <class T> template <> void S<T>::f(int) {}
5521 which we can spot because the pattern will be a
5522 specialization in this case. */
5523 args_depth
= TMPL_ARGS_DEPTH (args
);
5525 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5526 if (args_depth
> parms_depth
5527 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5529 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5531 if (parms_depth
> 1)
5535 args
= make_temp_vec (parms_depth
);
5536 for (i
= 0; i
< parms_depth
; ++i
)
5537 TREE_VEC_ELT (args
, i
) =
5538 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5541 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5546 /* This special case arises when we have something like this:
5548 template <class T> struct S {
5549 friend void f<int>(int, double);
5552 Here, the DECL_TI_TEMPLATE for the friend declaration
5553 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5554 being called from tsubst_friend_function, and we want
5555 only to create a new decl (R) with appropriate types so
5556 that we can call determine_specialization. */
5557 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5559 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5560 == IDENTIFIER_NODE
), 0);
5561 gen_tmpl
= NULL_TREE
;
5564 if (DECL_CLASS_SCOPE_P (t
))
5566 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5570 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5572 /*entering_scope=*/1);
5579 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5581 /* We do NOT check for matching decls pushed separately at this
5582 point, as they may not represent instantiations of this
5583 template, and in any case are considered separate under the
5584 discrete model. Instead, see add_maybe_template. */
5588 DECL_USE_TEMPLATE (r
) = 0;
5589 TREE_TYPE (r
) = type
;
5592 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1, t
,
5593 /*entering_scope=*/1);
5594 DECL_CLASS_CONTEXT (r
) = ctx
;
5596 if (member
&& IDENTIFIER_TYPENAME_P (DECL_NAME (r
)))
5597 /* Type-conversion operator. Reconstruct the name, in
5598 case it's the name of one of the template's parameters. */
5599 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5601 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5603 DECL_MAIN_VARIANT (r
) = r
;
5604 DECL_RESULT (r
) = NULL_TREE
;
5606 TREE_STATIC (r
) = 0;
5607 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5608 DECL_EXTERNAL (r
) = 1;
5609 DECL_INTERFACE_KNOWN (r
) = 0;
5610 DECL_DEFER_OUTPUT (r
) = 0;
5611 TREE_CHAIN (r
) = NULL_TREE
;
5612 DECL_PENDING_INLINE_INFO (r
) = 0;
5615 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5616 name. There's no need to do this in the special friend
5617 case mentioned above where GEN_TMPL is NULL. */
5620 /* The ARGVEC was built on the momentary obstack. Make it
5622 argvec
= copy_to_permanent (argvec
);
5623 DECL_TEMPLATE_INFO (r
)
5624 = perm_tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5625 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5626 register_specialization (r
, gen_tmpl
, argvec
);
5628 /* Set the mangled name for R. */
5629 if (DECL_DESTRUCTOR_P (t
))
5630 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5633 /* Instantiations of template functions must be mangled
5634 specially, in order to conform to 14.5.5.1
5635 [temp.over.link]. */
5636 tree tmpl
= DECL_TI_TEMPLATE (t
);
5638 /* TMPL will be NULL if this is a specialization of a
5639 member function of a template class. */
5640 if (name_mangling_version
< 1
5641 || tmpl
== NULL_TREE
5642 || (member
&& !is_member_template (tmpl
)
5643 && !DECL_TEMPLATE_INFO (tmpl
)))
5644 set_mangled_name_for_decl (r
);
5646 set_mangled_name_for_template_decl (r
);
5650 make_decl_rtl (r
, NULL_PTR
, 1);
5652 /* Like grokfndecl. If we don't do this, pushdecl will
5653 mess up our TREE_CHAIN because it doesn't find a
5654 previous decl. Sigh. */
5656 && ! uses_template_parms (r
)
5657 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5659 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5662 /* Copy the list of befriending classes. */
5663 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5665 friends
= &TREE_CHAIN (*friends
))
5667 *friends
= copy_node (*friends
);
5668 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5669 args
, /*complain=*/1,
5673 if (DECL_CONSTRUCTOR_P (r
))
5675 maybe_retrofit_in_chrg (r
);
5676 grok_ctor_properties (ctx
, r
);
5678 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5679 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5686 TREE_TYPE (r
) = type
;
5687 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5688 DECL_INITIAL (r
) = TREE_TYPE (r
);
5690 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5691 /*complain=*/1, in_decl
);
5693 DECL_CONTEXT (r
) = NULL_TREE
;
5694 #ifdef PROMOTE_PROTOTYPES
5695 if ((TREE_CODE (type
) == INTEGER_TYPE
5696 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5697 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5698 DECL_ARG_TYPE (r
) = integer_type_node
;
5701 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5702 /*complain=*/1, TREE_CHAIN (t
));
5709 TREE_TYPE (r
) = type
;
5712 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
,
5713 /*complain=*/1, in_decl
);
5715 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
5716 /*complain=*/1, in_decl
);
5717 TREE_CHAIN (r
) = NULL_TREE
;
5718 if (TREE_CODE (type
) == VOID_TYPE
)
5719 cp_error_at ("instantiation of `%D' as type void", r
);
5727 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
5728 TREE_CHAIN (r
) = NULL_TREE
;
5738 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
5740 in_decl
, /*entering_scope=*/1);
5742 /* Nobody should be tsubst'ing into non-template variables. */
5743 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
5744 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5746 /* Check to see if we already have this specialization. */
5747 tmpl
= DECL_TI_TEMPLATE (t
);
5748 gen_tmpl
= most_general_template (tmpl
);
5749 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
5750 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5759 TREE_TYPE (r
) = type
;
5760 DECL_CONTEXT (r
) = ctx
;
5761 if (TREE_STATIC (r
))
5762 DECL_ASSEMBLER_NAME (r
)
5763 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
5765 /* Don't try to expand the initializer until someone tries to use
5766 this variable; otherwise we run into circular dependencies. */
5767 DECL_INITIAL (r
) = NULL_TREE
;
5771 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
5773 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
5774 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5775 register_specialization (r
, gen_tmpl
, argvec
);
5777 TREE_CHAIN (r
) = NULL_TREE
;
5778 if (TREE_CODE (type
) == VOID_TYPE
)
5779 cp_error_at ("instantiation of `%D' as type void", r
);
5784 if (t
== TYPE_NAME (TREE_TYPE (t
)))
5785 r
= TYPE_NAME (type
);
5789 TREE_TYPE (r
) = type
;
5790 DECL_CONTEXT (r
) = current_class_type
;
5791 TREE_CHAIN (r
) = NULL_TREE
;
5796 my_friendly_abort (0);
5799 /* Restore the file and line information. */
5800 lineno
= saved_lineno
;
5801 input_filename
= saved_filename
;
5806 /* Substitue into the ARG_TYPES of a function type. */
5809 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
5815 tree remaining_arg_types
;
5818 if (!arg_types
|| arg_types
== void_list_node
)
5821 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
5822 args
, complain
, in_decl
);
5823 if (remaining_arg_types
== error_mark_node
)
5824 return error_mark_node
;
5826 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
5827 if (type
== error_mark_node
)
5828 return error_mark_node
;
5830 /* Do array-to-pointer, function-to-pointer conversion, and ignore
5831 top-level qualifiers as required. */
5832 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
5834 /* Note that we do not substitute into default arguments here. The
5835 standard mandates that they be instantiated only when needed,
5836 which is done in build_over_call. */
5837 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
5838 remaining_arg_types
);
5842 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
5843 *not* handle the exception-specification for FNTYPE, because the
5844 initial substitution of explicitly provided template parameters
5845 during argument deduction forbids substitution into the
5846 exception-specification:
5850 All references in the function type of the function template to the
5851 corresponding template parameters are replaced by the specified tem-
5852 plate argument values. If a substitution in a template parameter or
5853 in the function type of the function template results in an invalid
5854 type, type deduction fails. [Note: The equivalent substitution in
5855 exception specifications is done only when the function is instanti-
5856 ated, at which point a program is ill-formed if the substitution
5857 results in an invalid type.] */
5860 tsubst_function_type (t
, args
, complain
, in_decl
)
5870 /* The TYPE_CONTEXT is not used for function/method types. */
5871 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
5873 /* Substitue the return type. */
5874 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
5875 if (return_type
== error_mark_node
)
5876 return error_mark_node
;
5878 /* Substitue the argument types. */
5879 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
5881 if (arg_types
== error_mark_node
)
5882 return error_mark_node
;
5884 /* Construct a new type node and return it. */
5885 if (TREE_CODE (t
) == FUNCTION_TYPE
)
5886 fntype
= build_function_type (return_type
, arg_types
);
5889 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
5890 if (! IS_AGGR_TYPE (r
))
5894 Type deduction may fail for any of the following
5897 -- Attempting to create "pointer to member of T" when T
5898 is not a class type. */
5900 cp_error ("creating pointer to member function of non-class type `%T'",
5902 return error_mark_node
;
5905 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
5908 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
5913 /* Substitute into the PARMS of a call-declarator. */
5916 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
5926 if (!parms
|| parms
== void_list_node
)
5929 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
5930 args
, complain
, in_decl
);
5932 /* Figure out the type of this parameter. */
5933 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
5935 /* Figure out the default argument as well. Note that we use
5936 tsubst_expr since the default argument is really an expression. */
5937 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
5939 /* Chain this parameter on to the front of those we have already
5940 processed. We don't use hash_tree_cons because that function
5941 doesn't check TREE_PARMLIST. */
5942 new_parms
= tree_cons (defarg
, type
, new_parms
);
5944 /* And note that these are parameters. */
5945 TREE_PARMLIST (new_parms
) = 1;
5950 /* Take the tree structure T and replace template parameters used
5951 therein with the argument vector ARGS. IN_DECL is an associated
5952 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
5953 An appropriate error message is issued only if COMPLAIN is
5954 non-zero. Note that we must be relatively non-tolerant of
5955 extensions here, in order to preserve conformance; if we allow
5956 substitutions that should not be allowed, we may allow argument
5957 deductions that should not succeed, and therefore report ambiguous
5958 overload situations where there are none. In theory, we could
5959 allow the substitution, but indicate that it should have failed,
5960 and allow our caller to make sure that the right thing happens, but
5961 we don't try to do this yet.
5963 This function is used for dealing with types, decls and the like;
5964 for expressions, use tsubst_expr or tsubst_copy. */
5967 tsubst (t
, args
, complain
, in_decl
)
5974 if (t
== NULL_TREE
|| t
== error_mark_node
5975 || t
== integer_type_node
5976 || t
== void_type_node
5977 || t
== char_type_node
5978 || TREE_CODE (t
) == NAMESPACE_DECL
)
5981 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
5982 type
= IDENTIFIER_TYPE_VALUE (t
);
5984 type
= TREE_TYPE (t
);
5985 if (type
== unknown_type_node
)
5986 my_friendly_abort (42);
5988 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
5989 && TREE_CODE (t
) != TYPENAME_TYPE
5990 && TREE_CODE (t
) != TEMPLATE_DECL
5991 && TREE_CODE (t
) != IDENTIFIER_NODE
5992 && TREE_CODE (t
) != FUNCTION_TYPE
5993 && TREE_CODE (t
) != METHOD_TYPE
)
5994 type
= tsubst (type
, args
, complain
, in_decl
);
5995 if (type
== error_mark_node
)
5996 return error_mark_node
;
5998 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
5999 return tsubst_decl (t
, args
, type
, in_decl
);
6001 switch (TREE_CODE (t
))
6006 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6007 /*entering_scope=*/0);
6010 case IDENTIFIER_NODE
:
6022 if (t
== integer_type_node
)
6025 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6026 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6030 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6032 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6033 if (max
== error_mark_node
)
6034 return error_mark_node
;
6036 if (processing_template_decl
)
6038 tree itype
= make_node (INTEGER_TYPE
);
6039 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6040 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6045 if (integer_zerop (omax
))
6047 /* Still allow an explicit array of size zero. */
6049 pedwarn ("creating array with size zero");
6051 else if (integer_zerop (max
) || INT_CST_LT (max
, integer_zero_node
))
6055 Type deduction may fail for any of the following
6058 Attempting to create an array with a size that is
6059 zero or negative. */
6061 cp_error ("creating array with size `%E'", max
);
6063 return error_mark_node
;
6066 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
6067 if (!TREE_PERMANENT (max
) && !allocation_temporary_p ())
6068 max
= copy_to_permanent (max
);
6069 return build_index_type (max
);
6072 case TEMPLATE_TYPE_PARM
:
6073 case TEMPLATE_TEMPLATE_PARM
:
6074 case TEMPLATE_PARM_INDEX
:
6082 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6083 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6085 idx
= TEMPLATE_TYPE_IDX (t
);
6086 level
= TEMPLATE_TYPE_LEVEL (t
);
6090 idx
= TEMPLATE_PARM_IDX (t
);
6091 level
= TEMPLATE_PARM_LEVEL (t
);
6094 if (TREE_VEC_LENGTH (args
) > 0)
6096 tree arg
= NULL_TREE
;
6098 levels
= TMPL_ARGS_DEPTH (args
);
6099 if (level
<= levels
)
6100 arg
= TMPL_ARG (args
, level
, idx
);
6102 if (arg
== error_mark_node
)
6103 return error_mark_node
;
6104 else if (arg
!= NULL_TREE
)
6106 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6108 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
6110 return cp_build_qualified_type
6111 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
));
6113 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6115 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6117 /* We are processing a type constructed from
6118 a template template parameter */
6119 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6120 args
, complain
, in_decl
);
6121 if (argvec
== error_mark_node
)
6122 return error_mark_node
;
6124 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6125 we are resolving nested-types in the signature of
6126 a member function templates.
6127 Otherwise ARG is a TEMPLATE_DECL and is the real
6128 template to be instantiated. */
6129 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6130 arg
= TYPE_NAME (arg
);
6132 r
= lookup_template_class (DECL_NAME (arg
),
6135 /*entering_scope=*/0);
6136 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
6139 /* We are processing a template argument list. */
6147 my_friendly_abort (981018);
6150 /* This can happen during the attempted tsubst'ing in
6151 unify. This means that we don't yet have any information
6152 about the template parameter in question. */
6155 /* If we get here, we must have been looking at a parm for a
6156 more deeply nested template. Make a new version of this
6157 template parameter, but with a lower level. */
6158 switch (TREE_CODE (t
))
6160 case TEMPLATE_TYPE_PARM
:
6161 case TEMPLATE_TEMPLATE_PARM
:
6163 TEMPLATE_TYPE_PARM_INDEX (r
)
6164 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6166 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6167 TYPE_MAIN_VARIANT (r
) = r
;
6168 TYPE_POINTER_TO (r
) = NULL_TREE
;
6169 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6171 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6172 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6174 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6176 if (argvec
== error_mark_node
)
6177 return error_mark_node
;
6179 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6180 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
6184 case TEMPLATE_PARM_INDEX
:
6185 r
= reduce_template_parm_level (t
, type
, levels
);
6189 my_friendly_abort (0);
6197 tree purpose
, value
, chain
, result
;
6199 if (t
== void_list_node
)
6202 purpose
= TREE_PURPOSE (t
);
6205 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6206 if (purpose
== error_mark_node
)
6207 return error_mark_node
;
6209 value
= TREE_VALUE (t
);
6212 value
= tsubst (value
, args
, complain
, in_decl
);
6213 if (value
== error_mark_node
)
6214 return error_mark_node
;
6216 chain
= TREE_CHAIN (t
);
6217 if (chain
&& chain
!= void_type_node
)
6219 chain
= tsubst (chain
, args
, complain
, in_decl
);
6220 if (chain
== error_mark_node
)
6221 return error_mark_node
;
6223 if (purpose
== TREE_PURPOSE (t
)
6224 && value
== TREE_VALUE (t
)
6225 && chain
== TREE_CHAIN (t
))
6227 result
= hash_tree_cons (purpose
, value
, chain
);
6228 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
6232 if (type
!= NULL_TREE
)
6234 /* A binfo node. We always need to make a copy, of the node
6235 itself and of its BINFO_BASETYPES. */
6239 /* Make sure type isn't a typedef copy. */
6240 type
= BINFO_TYPE (TYPE_BINFO (type
));
6242 TREE_TYPE (t
) = complete_type (type
);
6243 if (IS_AGGR_TYPE (type
))
6245 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6246 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6247 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6248 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6253 /* Otherwise, a vector of template arguments. */
6254 return tsubst_template_arg_vector (t
, args
, complain
);
6257 case REFERENCE_TYPE
:
6259 enum tree_code code
;
6261 if (type
== TREE_TYPE (t
))
6264 code
= TREE_CODE (t
);
6269 Type deduction may fail for any of the following
6272 -- Attempting to create a pointer to reference type.
6273 -- Attempting to create a reference to a reference type or
6274 a reference to void. */
6275 if (TREE_CODE (type
) == REFERENCE_TYPE
6276 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6278 static int last_line
= 0;
6279 static char* last_file
= 0;
6281 /* We keep track of the last time we issued this error
6282 message to avoid spewing a ton of messages during a
6283 single bad template instantiation. */
6284 if (complain
&& (last_line
!= lineno
||
6285 last_file
!= input_filename
))
6287 if (TREE_CODE (type
) == VOID_TYPE
)
6288 cp_error ("forming reference to void");
6290 cp_error ("forming %s to reference type `%T'",
6291 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6294 last_file
= input_filename
;
6297 return error_mark_node
;
6299 else if (code
== POINTER_TYPE
)
6300 r
= build_pointer_type (type
);
6302 r
= build_reference_type (type
);
6303 r
= cp_build_qualified_type (r
, TYPE_QUALS (t
));
6305 /* Will this ever be needed for TYPE_..._TO values? */
6311 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6312 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6316 Type deduction may fail for any of the following
6319 -- Attempting to create "pointer to member of T" when T
6320 is not a class type. */
6322 cp_error ("creating pointer to member of non-class type `%T'",
6324 return error_mark_node
;
6326 return build_offset_type (r
, type
);
6334 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6335 if (fntype
== error_mark_node
)
6336 return error_mark_node
;
6338 /* Substitue the exception specification. */
6339 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6342 raises
= tsubst (raises
, args
, complain
, in_decl
);
6343 if (raises
== error_mark_node
)
6345 fntype
= build_exception_variant (fntype
, raises
);
6351 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6352 if (domain
== error_mark_node
)
6353 return error_mark_node
;
6355 /* As an optimization, we avoid regenerating the array type if
6356 it will obviously be the same as T. */
6357 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6360 /* These checks should match the ones in grokdeclarator.
6364 The deduction may fail for any of the following reasons:
6366 -- Attempting to create an array with an element type that
6367 is void, a function type, or a reference type. */
6368 if (TREE_CODE (type
) == VOID_TYPE
6369 || TREE_CODE (type
) == FUNCTION_TYPE
6370 || TREE_CODE (type
) == REFERENCE_TYPE
)
6373 cp_error ("creating array of `%T'", type
);
6374 return error_mark_node
;
6377 r
= build_cplus_array_type (type
, domain
);
6384 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6386 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6389 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6390 return error_mark_node
;
6392 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6398 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6400 if (e
== error_mark_node
)
6401 return error_mark_node
;
6403 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6408 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6409 in_decl
, /*entering_scope=*/1);
6410 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6413 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6414 return error_mark_node
;
6416 if (!IS_AGGR_TYPE (ctx
))
6419 cp_error ("`%T' is not a class, struct, or union type",
6421 return error_mark_node
;
6423 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6425 /* Normally, make_typename_type does not require that the CTX
6426 have complete type in order to allow things like:
6428 template <class T> struct S { typename S<T>::X Y; };
6430 But, such constructs have already been resolved by this
6431 point, so here CTX really should have complete type, unless
6432 it's a partial instantiation. */
6433 ctx
= complete_type (ctx
);
6434 if (!TYPE_SIZE (ctx
))
6437 incomplete_type_error (NULL_TREE
, ctx
);
6438 return error_mark_node
;
6442 f
= make_typename_type (ctx
, f
);
6443 return cp_build_qualified_type (f
,
6445 | CP_TYPE_QUALS (t
));
6450 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6452 if (e
== error_mark_node
)
6453 return error_mark_node
;
6454 return make_pointer_declarator (type
, e
);
6459 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6461 if (e
== error_mark_node
)
6462 return error_mark_node
;
6463 return make_reference_declarator (type
, e
);
6468 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6470 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
,
6472 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6473 return error_mark_node
;
6475 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6480 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6482 tree e2
= tsubst_call_declarator_parms (TREE_OPERAND (t
, 1), args
,
6484 tree e3
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6486 if (e1
== error_mark_node
|| e2
== error_mark_node
6487 || e3
== error_mark_node
)
6488 return error_mark_node
;
6490 return make_call_declarator (e1
, e2
, TREE_OPERAND (t
, 2), e3
);
6495 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6497 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6498 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6499 return error_mark_node
;
6501 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6506 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6508 if (e1
== error_mark_node
)
6509 return error_mark_node
;
6511 return TREE_TYPE (e1
);
6515 sorry ("use of `%s' in template",
6516 tree_code_name
[(int) TREE_CODE (t
)]);
6517 return error_mark_node
;
6524 emit_line_note (input_filename
, lineno
);
6528 expand_start_bindings (0);
6535 int saved_warn_unused
= 0;
6537 if (processing_template_decl
)
6539 saved_warn_unused
= warn_unused
;
6542 expand_end_bindings (getdecls (), kept_level_p (), 0);
6543 if (processing_template_decl
)
6544 warn_unused
= saved_warn_unused
;
6545 t
= poplevel (kept_level_p (), 1, 0);
6550 /* Like tsubst, but deals with expressions. This function just replaces
6551 template parms; to finish processing the resultant expression, use
6555 tsubst_copy (t
, args
, complain
, in_decl
)
6560 enum tree_code code
;
6563 if (t
== NULL_TREE
|| t
== error_mark_node
)
6566 code
= TREE_CODE (t
);
6571 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6578 if (!DECL_CONTEXT (t
))
6579 /* This is a global enumeration constant. */
6582 /* Unfortunately, we cannot just call lookup_name here.
6585 template <int I> int f() {
6587 struct S { void g() { E e = a; } };
6590 When we instantiate f<7>::S::g(), say, lookup_name is not
6591 clever enough to find f<7>::a. */
6593 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6594 /*entering_scope=*/0);
6596 for (v
= TYPE_VALUES (enum_type
);
6599 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6600 return TREE_VALUE (v
);
6602 /* We didn't find the name. That should never happen; if
6603 name-lookup found it during preliminary parsing, we
6604 should find it again here during instantiation. */
6605 my_friendly_abort (0);
6610 if (DECL_CONTEXT (t
))
6614 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6615 /*entering_scope=*/1);
6616 if (ctx
!= DECL_CONTEXT (t
))
6617 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6623 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6624 t
= tsubst (t
, args
, complain
, in_decl
);
6629 if (is_member_template (t
))
6630 return tsubst (t
, args
, complain
, in_decl
);
6636 /* We must tsbust into a LOOKUP_EXPR in case the names to
6637 which it refers is a conversion operator; in that case the
6638 name will change. We avoid making unnecessary copies,
6641 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6643 if (id
!= TREE_OPERAND (t
, 0))
6645 r
= build_nt (LOOKUP_EXPR
, id
);
6646 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6654 case REINTERPRET_CAST_EXPR
:
6655 case CONST_CAST_EXPR
:
6656 case STATIC_CAST_EXPR
:
6657 case DYNAMIC_CAST_EXPR
:
6660 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6661 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6664 case PREDECREMENT_EXPR
:
6665 case PREINCREMENT_EXPR
:
6666 case POSTDECREMENT_EXPR
:
6667 case POSTINCREMENT_EXPR
:
6669 case TRUTH_NOT_EXPR
:
6672 case CONVERT_EXPR
: /* Unary + */
6679 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6680 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6685 case TRUNC_DIV_EXPR
:
6687 case FLOOR_DIV_EXPR
:
6688 case ROUND_DIV_EXPR
:
6689 case EXACT_DIV_EXPR
:
6691 case BIT_ANDTC_EXPR
:
6694 case TRUNC_MOD_EXPR
:
6695 case FLOOR_MOD_EXPR
:
6696 case TRUTH_ANDIF_EXPR
:
6697 case TRUTH_ORIF_EXPR
:
6698 case TRUTH_AND_EXPR
:
6719 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6720 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
6724 tree fn
= TREE_OPERAND (t
, 0);
6725 if (is_overloaded_fn (fn
))
6726 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
6728 /* Sometimes FN is a LOOKUP_EXPR. */
6729 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
6731 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
6736 case METHOD_CALL_EXPR
:
6738 tree name
= TREE_OPERAND (t
, 0);
6739 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
6741 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6743 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6745 else if (TREE_CODE (name
) == SCOPE_REF
6746 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
6748 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6750 name
= TREE_OPERAND (name
, 1);
6751 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
6753 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6754 name
= build_nt (SCOPE_REF
, base
, name
);
6757 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6759 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
6761 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
6770 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6771 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6772 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
6774 if (code
== BIND_EXPR
&& !processing_template_decl
)
6776 /* This processing should really occur in tsubst_expr,
6777 However, tsubst_expr does not recurse into expressions,
6778 since it assumes that there aren't any statements
6779 inside them. Instead, it simply calls
6780 build_expr_from_tree. So, we need to expand the
6782 tree rtl_expr
= begin_stmt_expr ();
6783 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
,
6785 r
= finish_stmt_expr (rtl_expr
, block
);
6794 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6795 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6796 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
6797 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
6804 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6805 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
6806 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
6807 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
6811 case TEMPLATE_ID_EXPR
:
6813 /* Substituted template arguments */
6814 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
6817 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
6818 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
6820 return lookup_template_function
6821 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
6826 tree purpose
, value
, chain
;
6828 if (t
== void_list_node
)
6831 purpose
= TREE_PURPOSE (t
);
6833 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
6834 value
= TREE_VALUE (t
);
6836 value
= tsubst_copy (value
, args
, complain
, in_decl
);
6837 chain
= TREE_CHAIN (t
);
6838 if (chain
&& chain
!= void_type_node
)
6839 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
6840 if (purpose
== TREE_PURPOSE (t
)
6841 && value
== TREE_VALUE (t
)
6842 && chain
== TREE_CHAIN (t
))
6844 return tree_cons (purpose
, value
, chain
);
6851 case TEMPLATE_TYPE_PARM
:
6852 case TEMPLATE_TEMPLATE_PARM
:
6853 case TEMPLATE_PARM_INDEX
:
6855 case REFERENCE_TYPE
:
6862 return tsubst (t
, args
, complain
, in_decl
);
6864 case IDENTIFIER_NODE
:
6865 if (IDENTIFIER_TYPENAME_P (t
)
6866 /* Make sure it's not just a variable named `__opr', for instance,
6867 which can occur in some existing code. */
6869 return build_typename_overload
6870 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
6877 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6878 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
6879 complain
, in_decl
));
6880 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
6889 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6892 tsubst_expr (t
, args
, complain
, in_decl
)
6897 if (t
== NULL_TREE
|| t
== error_mark_node
)
6900 if (processing_template_decl
)
6901 return tsubst_copy (t
, args
, complain
, in_decl
);
6903 switch (TREE_CODE (t
))
6906 lineno
= TREE_COMPLEXITY (t
);
6907 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
6908 args
, complain
, in_decl
));
6912 lineno
= TREE_COMPLEXITY (t
);
6913 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
6914 args
, complain
, in_decl
));
6919 int i
= suspend_momentary ();
6922 lineno
= TREE_COMPLEXITY (t
);
6923 emit_line_note (input_filename
, lineno
);
6925 (tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
6926 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
6927 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
6928 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, complain
, in_decl
);
6930 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
6931 resume_momentary (i
);
6938 lineno
= TREE_COMPLEXITY (t
);
6941 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6942 tsubst_expr (tmp
, args
, complain
, in_decl
);
6943 finish_for_init_stmt (NULL_TREE
);
6944 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
6947 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
6948 finish_for_expr (tmp
, NULL_TREE
);
6949 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
6950 finish_for_stmt (tmp
, NULL_TREE
);
6956 lineno
= TREE_COMPLEXITY (t
);
6957 begin_while_stmt ();
6958 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
6959 args
, complain
, in_decl
),
6961 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
6962 finish_while_stmt (NULL_TREE
);
6968 lineno
= TREE_COMPLEXITY (t
);
6970 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
6971 finish_do_body (NULL_TREE
);
6972 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
6982 lineno
= TREE_COMPLEXITY (t
);
6984 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
6985 args
, complain
, in_decl
),
6988 if (tmp
= THEN_CLAUSE (t
), tmp
)
6990 tsubst_expr (tmp
, args
, complain
, in_decl
);
6991 finish_then_clause (NULL_TREE
);
6994 if (tmp
= ELSE_CLAUSE (t
), tmp
)
6996 begin_else_clause ();
6997 tsubst_expr (tmp
, args
, complain
, in_decl
);
6998 finish_else_clause (NULL_TREE
);
7009 lineno
= TREE_COMPLEXITY (t
);
7010 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7011 for (substmt
= COMPOUND_BODY (t
);
7012 substmt
!= NULL_TREE
;
7013 substmt
= TREE_CHAIN (substmt
))
7014 tsubst_expr (substmt
, args
, complain
, in_decl
);
7015 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
7021 lineno
= TREE_COMPLEXITY (t
);
7022 finish_break_stmt ();
7026 lineno
= TREE_COMPLEXITY (t
);
7027 finish_continue_stmt ();
7034 lineno
= TREE_COMPLEXITY (t
);
7035 begin_switch_stmt ();
7036 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7037 finish_switch_cond (val
);
7039 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
7040 tsubst_expr (tmp
, args
, complain
, in_decl
);
7042 finish_switch_stmt (val
, NULL_TREE
);
7047 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7048 tsubst_expr (CASE_HIGH (t
), args
, complain
, in_decl
));
7052 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
7059 lineno
= TREE_COMPLEXITY (t
);
7060 t
= GOTO_DESTINATION (t
);
7061 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
7062 /* Computed goto's must be tsubst'd into. On the other hand,
7063 non-computed gotos must not be; the identifier in question
7064 will have no binding. */
7065 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7066 finish_goto_stmt (t
);
7070 lineno
= TREE_COMPLEXITY (t
);
7071 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, complain
, in_decl
),
7072 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7073 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7074 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7075 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7080 lineno
= TREE_COMPLEXITY (t
);
7082 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7083 finish_try_block (NULL_TREE
);
7085 tree handler
= TRY_HANDLERS (t
);
7086 for (; handler
; handler
= TREE_CHAIN (handler
))
7087 tsubst_expr (handler
, args
, complain
, in_decl
);
7089 finish_handler_sequence (NULL_TREE
);
7093 lineno
= TREE_COMPLEXITY (t
);
7095 if (HANDLER_PARMS (t
))
7097 tree d
= HANDLER_PARMS (t
);
7098 expand_start_catch_block
7099 (tsubst (TREE_OPERAND (d
, 1), args
, complain
, in_decl
),
7100 tsubst (TREE_OPERAND (d
, 0), args
, complain
, in_decl
));
7103 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
7104 finish_handler_parms (NULL_TREE
);
7105 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7106 finish_handler (NULL_TREE
);
7110 lineno
= TREE_COMPLEXITY (t
);
7112 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
7113 tsubst (t
, args
, complain
, NULL_TREE
);
7117 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7122 /* Instantiate the indicated variable or function template TMPL with
7123 the template arguments in TARG_PTR. */
7126 instantiate_template (tmpl
, targ_ptr
)
7127 tree tmpl
, targ_ptr
;
7133 struct obstack
*old_fmp_obstack
;
7134 extern struct obstack
*function_maybepermanent_obstack
;
7137 if (tmpl
== error_mark_node
)
7138 return error_mark_node
;
7140 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7142 /* Check to see if we already have this specialization. */
7143 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7144 if (spec
!= NULL_TREE
)
7147 if (DECL_TEMPLATE_INFO (tmpl
))
7149 /* The TMPL is a partial instantiation. To get a full set of
7150 arguments we must add the arguments used to perform the
7151 partial instantiation. */
7152 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7154 gen_tmpl
= most_general_template (tmpl
);
7156 /* Check to see if we already have this specialization. */
7157 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7158 if (spec
!= NULL_TREE
)
7164 push_obstacks (&permanent_obstack
, &permanent_obstack
);
7165 old_fmp_obstack
= function_maybepermanent_obstack
;
7166 function_maybepermanent_obstack
= &permanent_obstack
;
7168 len
= DECL_NTPARMS (gen_tmpl
);
7169 inner_args
= innermost_args (targ_ptr
);
7173 tree t
= TREE_VEC_ELT (inner_args
, i
);
7174 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
7176 tree nt
= target_type (t
);
7177 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7179 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7180 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7181 fndecl
= error_mark_node
;
7186 targ_ptr
= copy_to_permanent (targ_ptr
);
7188 /* substitute template parameters */
7189 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, /*complain=*/1, gen_tmpl
);
7190 /* The DECL_TI_TEMPLATE should always be the immediate parent
7191 template, not the most general template. */
7192 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7194 if (flag_external_templates
)
7195 add_pending_template (fndecl
);
7198 function_maybepermanent_obstack
= old_fmp_obstack
;
7204 /* Push the name of the class template into the scope of the instantiation. */
7207 overload_template_name (type
)
7210 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
7213 if (IDENTIFIER_CLASS_VALUE (id
)
7214 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
7217 decl
= build_decl (TYPE_DECL
, id
, type
);
7218 SET_DECL_ARTIFICIAL (decl
);
7219 pushdecl_class_level (decl
);
7222 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7223 arguments that are being used when calling it. TARGS is a vector
7224 into which the deduced template arguments are placed.
7226 Return zero for success, 2 for an incomplete match that doesn't resolve
7227 all the types, and 1 for complete failure. An error message will be
7228 printed only for an incomplete match.
7230 If FN is a conversion operator, RETURN_TYPE is the type desired as
7231 the result of the conversion operator.
7233 TPARMS is a vector of template parameters.
7235 The EXPLICIT_TARGS are explicit template arguments provided via a
7238 The parameter STRICT is one of:
7241 We are deducing arguments for a function call, as in
7245 We are deducing arguments for a conversion function, as in
7249 We are deducing arguments when calculating the partial
7250 ordering between specializations of function or class
7251 templates, as in [temp.func.order] and [temp.class.order],
7252 when doing an explicit instantiation as in [temp.explicit],
7253 when determining an explicit specialization as in
7254 [temp.expl.spec], or when taking the address of a function
7255 template, as in [temp.deduct.funcaddr].
7257 The other arguments are as for type_unification. */
7260 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7262 tree fn
, explicit_targs
, targs
, args
, return_type
;
7263 unification_kind_t strict
;
7268 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7270 fntype
= TREE_TYPE (fn
);
7275 The specified template arguments must match the template
7276 parameters in kind (i.e., type, nontype, template), and there
7277 must not be more arguments than there are parameters;
7278 otherwise type deduction fails.
7280 Nontype arguments must match the types of the corresponding
7281 nontype template parameters, or must be convertible to the
7282 types of the corresponding nontype parameters as specified in
7283 _temp.arg.nontype_, otherwise type deduction fails.
7285 All references in the function type of the function template
7286 to the corresponding template parameters are replaced by the
7287 specified template argument values. If a substitution in a
7288 template parameter or in the function type of the function
7289 template results in an invalid type, type deduction fails. */
7291 tree converted_args
;
7294 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7295 explicit_targs
, NULL_TREE
, /*complain=*/0,
7296 /*require_all_arguments=*/0));
7297 if (converted_args
== error_mark_node
)
7300 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7301 if (fntype
== error_mark_node
)
7304 /* Place the explicitly specified arguments in TARGS. */
7305 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7306 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7309 parms
= TYPE_ARG_TYPES (fntype
);
7311 if (DECL_CONV_FN_P (fn
))
7313 /* This is a template conversion operator. Use the return types
7314 as well as the argument types. We use it instead of 'this', since
7315 we could be comparing conversions from different classes. */
7316 parms
= scratch_tree_cons (NULL_TREE
, TREE_TYPE (fntype
),
7317 TREE_CHAIN (parms
));
7318 args
= scratch_tree_cons (NULL_TREE
, return_type
, TREE_CHAIN (args
));
7321 /* We allow incomplete unification without an error message here
7322 because the standard doesn't seem to explicitly prohibit it. Our
7323 callers must be ready to deal with unification failures in any
7325 return type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7326 targs
, parms
, args
, /*subr=*/0,
7327 strict
, /*allow_incomplete*/1);
7330 /* Adjust types before performing type deduction, as described in
7331 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7332 sections are symmetric. PARM is the type of a function parameter
7333 or the return type of the conversion function. ARG is the type of
7334 the argument passed to the call, or the type of the value
7335 intialized with the result of the conversion function. */
7338 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7339 unification_kind_t strict
;
7350 /* Swap PARM and ARG throughout the remainder of this
7351 function; the handling is precisely symmetric since PARM
7352 will initialize ARG rather than vice versa. */
7360 /* There is nothing to do in this case. */
7364 my_friendly_abort (0);
7367 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7369 /* [temp.deduct.call]
7371 If P is not a reference type:
7373 --If A is an array type, the pointer type produced by the
7374 array-to-pointer standard conversion (_conv.array_) is
7375 used in place of A for type deduction; otherwise,
7377 --If A is a function type, the pointer type produced by
7378 the function-to-pointer standard conversion
7379 (_conv.func_) is used in place of A for type deduction;
7382 --If A is a cv-qualified type, the top level
7383 cv-qualifiers of A's type are ignored for type
7385 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7386 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7387 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7388 *arg
= build_pointer_type (*arg
);
7390 *arg
= TYPE_MAIN_VARIANT (*arg
);
7393 /* [temp.deduct.call]
7395 If P is a cv-qualified type, the top level cv-qualifiers
7396 of P's type are ignored for type deduction. If P is a
7397 reference type, the type referred to by P is used for
7399 *parm
= TYPE_MAIN_VARIANT (*parm
);
7400 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7401 *parm
= TREE_TYPE (*parm
);
7404 /* Like type_unfication.
7406 If SUBR is 1, we're being called recursively (to unify the
7407 arguments of a function or method parameter of a function
7411 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7412 strict
, allow_incomplete
)
7413 tree tparms
, targs
, parms
, args
;
7415 unification_kind_t strict
;
7416 int allow_incomplete
;
7420 int ntparms
= TREE_VEC_LENGTH (tparms
);
7423 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7424 my_friendly_assert (parms
== NULL_TREE
7425 || TREE_CODE (parms
) == TREE_LIST
, 290);
7426 /* ARGS could be NULL (via a call from parse.y to
7427 build_x_function_call). */
7429 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7430 my_friendly_assert (ntparms
> 0, 292);
7435 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7439 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7443 sub_strict
= UNIFY_ALLOW_NONE
;
7447 my_friendly_abort (0);
7451 && parms
!= void_list_node
7453 && args
!= void_list_node
)
7455 parm
= TREE_VALUE (parms
);
7456 parms
= TREE_CHAIN (parms
);
7457 arg
= TREE_VALUE (args
);
7458 args
= TREE_CHAIN (args
);
7460 if (arg
== error_mark_node
)
7462 if (arg
== unknown_type_node
)
7463 /* We can't deduce anything from this, but we might get all the
7464 template args from other function args. */
7467 /* Conversions will be performed on a function argument that
7468 corresponds with a function parameter that contains only
7469 non-deducible template parameters and explicitly specified
7470 template parameters. */
7471 if (! uses_template_parms (parm
))
7475 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7476 type
= TREE_TYPE (arg
);
7483 if (strict
== DEDUCE_EXACT
)
7485 if (same_type_p (parm
, type
))
7489 /* It might work; we shouldn't check now, because we might
7490 get into infinite recursion. Overload resolution will
7497 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7499 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7500 if (type_unknown_p (arg
))
7502 /* [temp.deduct.type] A template-argument can be deduced from
7503 a pointer to function or pointer to member function
7504 argument if the set of overloaded functions does not
7505 contain function templates and at most one of a set of
7506 overloaded functions provides a unique match. */
7508 if (resolve_overloaded_unification
7509 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
7514 arg
= TREE_TYPE (arg
);
7518 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7520 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
))
7528 /* Fail if we've reached the end of the parm list, and more args
7529 are present, and the parm list isn't variadic. */
7530 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7532 /* Fail if parms are left and they don't have default values. */
7534 && parms
!= void_list_node
7535 && TREE_PURPOSE (parms
) == NULL_TREE
)
7538 for (i
= 0; i
< ntparms
; i
++)
7539 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7541 if (!allow_incomplete
)
7542 error ("incomplete type unification");
7548 /* Subroutine of type_unification_real. Args are like the variables at the
7549 call site. ARG is an overloaded function (or template-id); we try
7550 deducing template args from each of the overloads, and if only one
7551 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7554 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7556 tree tparms
, targs
, parm
, arg
;
7557 unification_kind_t strict
;
7560 tree tempargs
= copy_node (targs
);
7563 if (TREE_CODE (arg
) == ADDR_EXPR
)
7564 arg
= TREE_OPERAND (arg
, 0);
7566 if (TREE_CODE (arg
) == COMPONENT_REF
)
7567 /* Handle `&x' where `x' is some static or non-static member
7569 arg
= TREE_OPERAND (arg
, 1);
7571 /* Strip baselink information. */
7572 while (TREE_CODE (arg
) == TREE_LIST
)
7573 arg
= TREE_VALUE (arg
);
7575 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7577 /* If we got some explicit template args, we need to plug them into
7578 the affected templates before we try to unify, in case the
7579 explicit args will completely resolve the templates in question. */
7581 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7582 arg
= TREE_OPERAND (arg
, 0);
7584 for (; arg
; arg
= OVL_NEXT (arg
))
7586 tree fn
= OVL_CURRENT (arg
);
7589 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7592 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
7595 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
7597 if (TREE_CODE (elem
) == METHOD_TYPE
)
7598 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7599 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7600 strict
, sub_strict
);
7604 else if (TREE_CODE (arg
) == OVERLOAD
)
7606 for (; arg
; arg
= OVL_NEXT (arg
))
7608 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
7609 if (TREE_CODE (type
) == METHOD_TYPE
)
7610 type
= build_ptrmemfunc_type (build_pointer_type (type
));
7611 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
7613 strict
, sub_strict
);
7617 my_friendly_abort (981006);
7619 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7620 to function or pointer to member function argument if the set of
7621 overloaded functions does not contain function templates and at most
7622 one of a set of overloaded functions provides a unique match.
7624 So if we found multiple possibilities, we return success but don't
7629 int i
= TREE_VEC_LENGTH (targs
);
7631 if (TREE_VEC_ELT (tempargs
, i
))
7632 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
7640 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7641 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7642 different overloads deduce different arguments for a given parm.
7643 Returns 1 on success. */
7646 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
7648 tree tparms
, orig_targs
, targs
, parm
, arg
;
7649 unification_kind_t strict
;
7656 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7657 to function or pointer to member function argument if the set of
7658 overloaded functions does not contain function templates and at most
7659 one of a set of overloaded functions provides a unique match.
7661 So if this is a template, just return success. */
7663 if (uses_template_parms (arg
))
7666 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7668 /* We don't copy orig_targs for this because if we have already deduced
7669 some template args from previous args, unify would complain when we
7670 try to deduce a template parameter for the same argument, even though
7671 there isn't really a conflict. */
7672 nargs
= TREE_VEC_LENGTH (targs
);
7673 tempargs
= make_scratch_vec (nargs
);
7675 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
7678 /* First make sure we didn't deduce anything that conflicts with
7679 explicitly specified args. */
7680 for (i
= nargs
; i
--; )
7682 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7683 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
7685 if (elt
== NULL_TREE
)
7687 else if (uses_template_parms (elt
))
7689 /* Since we're unifying against ourselves, we will fill in template
7690 args used in the function parm list with our own template parms.
7692 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
7695 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
7699 for (i
= nargs
; i
--; )
7701 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7704 TREE_VEC_ELT (targs
, i
) = elt
;
7710 /* PARM is a template class (perhaps with unbound template
7711 parameters). ARG is a fully instantiated type. If ARG can be
7712 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
7713 TARGS are as for unify. */
7716 try_class_unification (tparms
, targs
, parm
, arg
)
7725 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
7726 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
7729 /* We need to make a new template argument vector for the call to
7730 unify. If we used TARGS, we'd clutter it up with the result of
7731 the attempted unification, even if this class didn't work out.
7732 We also don't want to commit ourselves to all the unifications
7733 we've already done, since unification is supposed to be done on
7734 an argument-by-argument basis. In other words, consider the
7735 following pathological case:
7737 template <int I, int J, int K>
7740 template <int I, int J>
7741 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
7743 template <int I, int J, int K>
7744 void f(S<I, J, K>, S<I, I, I>);
7753 Now, by the time we consider the unification involving `s2', we
7754 already know that we must have `f<0, 0, 0>'. But, even though
7755 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
7756 because there are two ways to unify base classes of S<0, 1, 2>
7757 with S<I, I, I>. If we kept the already deduced knowledge, we
7758 would reject the possibility I=1. */
7760 copy_of_targs
= make_temp_vec (TREE_VEC_LENGTH (targs
));
7761 i
= unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
7762 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
);
7765 /* If unification failed, we're done. */
7772 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
7773 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
7774 for the base class of ARG that we are currently examining. */
7777 get_template_base_recursive (tparms
, targs
, parm
,
7778 arg_binfo
, rval
, flags
)
7788 tree arg
= BINFO_TYPE (arg_binfo
);
7790 if (!(flags
& GTB_IGNORE_TYPE
))
7792 tree r
= try_class_unification (tparms
, targs
,
7795 /* If there is more than one satisfactory baseclass, then:
7799 If they yield more than one possible deduced A, the type
7803 if (r
&& rval
&& !same_type_p (r
, rval
))
7804 return error_mark_node
;
7809 binfos
= BINFO_BASETYPES (arg_binfo
);
7810 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
7812 /* Process base types. */
7813 for (i
= 0; i
< n_baselinks
; i
++)
7815 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
7818 /* Skip this base, if we've already seen it. */
7819 if (BINFO_MARKED (base_binfo
))
7823 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
7825 /* When searching for a non-virtual, we cannot mark virtually
7828 SET_BINFO_MARKED (base_binfo
);
7830 rval
= get_template_base_recursive (tparms
, targs
,
7834 GTB_VIA_VIRTUAL
* this_virtual
);
7836 /* If we discovered more than one matching base class, we can
7838 if (rval
== error_mark_node
)
7839 return error_mark_node
;
7845 /* Given a template type PARM and a class type ARG, find the unique
7846 base type in ARG that is an instance of PARM. We do not examine
7847 ARG itself; only its base-classes. If there is no appropriate base
7848 class, return NULL_TREE. If there is more than one, return
7849 error_mark_node. PARM may be the type of a partial specialization,
7850 as well as a plain template type. Used by unify. */
7853 get_template_base (tparms
, targs
, parm
, arg
)
7862 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
7864 arg_binfo
= TYPE_BINFO (complete_type (arg
));
7865 rval
= get_template_base_recursive (tparms
, targs
,
7870 /* Since get_template_base_recursive marks the bases classes, we
7871 must unmark them here. */
7872 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
7877 /* Returns the level of DECL, which declares a template parameter. */
7880 template_decl_level (decl
)
7883 switch (TREE_CODE (decl
))
7887 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
7890 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
7893 my_friendly_abort (0);
7898 /* Decide whether ARG can be unified with PARM, considering only the
7899 cv-qualifiers of each type, given STRICT as documented for unify.
7900 Returns non-zero iff the unification is OK on that basis.*/
7903 check_cv_quals_for_unify (strict
, arg
, parm
)
7908 return !((!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
7909 && !at_least_as_qualified_p (arg
, parm
))
7910 || (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
7911 && (!at_least_as_qualified_p (parm
, arg
))));
7914 /* Takes parameters as for type_unification. Returns 0 if the
7915 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7916 bitwise or of the following flags:
7919 Require an exact match between PARM and ARG.
7920 UNIFY_ALLOW_MORE_CV_QUAL:
7921 Allow the deduced ARG to be more cv-qualified than ARG.
7922 UNIFY_ALLOW_LESS_CV_QUAL:
7923 Allow the deduced ARG to be less cv-qualified than ARG.
7924 UNIFY_ALLOW_DERIVED:
7925 Allow the deduced ARG to be a template base class of ARG,
7926 or a pointer to a template base class of the type pointed to by
7928 UNIFY_ALLOW_INTEGER:
7929 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7930 case for more information. */
7933 unify (tparms
, targs
, parm
, arg
, strict
)
7934 tree tparms
, targs
, parm
, arg
;
7941 /* I don't think this will do the right thing with respect to types.
7942 But the only case I've seen it in so far has been array bounds, where
7943 signedness is the only information lost, and I think that will be
7945 while (TREE_CODE (parm
) == NOP_EXPR
)
7946 parm
= TREE_OPERAND (parm
, 0);
7948 if (arg
== error_mark_node
)
7950 if (arg
== unknown_type_node
)
7951 /* We can't deduce anything from this, but we might get all the
7952 template args from other function args. */
7955 /* If PARM uses template parameters, then we can't bail out here,
7956 even if ARG == PARM, since we won't record unifications for the
7957 template parameters. We might need them if we're trying to
7958 figure out which of two things is more specialized. */
7959 if (arg
== parm
&& !uses_template_parms (parm
))
7962 /* Immediately reject some pairs that won't unify because of
7963 cv-qualification mismatches. */
7964 if (TREE_CODE (arg
) == TREE_CODE (parm
)
7965 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
7966 /* We check the cv-qualifiers when unifying with template type
7967 parameters below. We want to allow ARG `const T' to unify with
7968 PARM `T' for example, when computing which of two templates
7969 is more specialized, for example. */
7970 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
7971 && !check_cv_quals_for_unify (strict
, arg
, parm
))
7974 switch (TREE_CODE (parm
))
7977 /* In a type which contains a nested-name-specifier, template
7978 argument values cannot be deduced for template parameters used
7979 within the nested-name-specifier. */
7982 case TEMPLATE_TYPE_PARM
:
7983 case TEMPLATE_TEMPLATE_PARM
:
7984 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7986 if (TEMPLATE_TYPE_LEVEL (parm
)
7987 != template_decl_level (tparm
))
7988 /* The PARM is not one we're trying to unify. Just check
7989 to see if it matches ARG. */
7990 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7991 && same_type_p (parm
, arg
)) ? 0 : 1;
7992 idx
= TEMPLATE_TYPE_IDX (parm
);
7993 targ
= TREE_VEC_ELT (targs
, idx
);
7994 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
7996 /* Check for mixed types and values. */
7997 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
7998 && TREE_CODE (tparm
) != TYPE_DECL
)
7999 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8000 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8003 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
8005 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
8007 /* We arrive here when PARM does not involve template
8010 /* ARG must be constructed from a template class. */
8011 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
8015 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8016 tree parmvec
= TYPE_TI_ARGS (parm
);
8017 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
8019 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
8022 /* The parameter and argument roles have to be switched here
8023 in order to handle default arguments properly. For example,
8024 template<template <class> class TT> void f(TT<int>)
8025 should be able to accept vector<int> which comes from
8026 template <class T, class Allocator = allocator>
8029 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8033 /* Deduce arguments T, i from TT<T> or TT<i>.
8034 We check each element of PARMVEC and ARGVEC individually
8035 rather than the whole TREE_VEC since they can have
8036 different number of elements. */
8038 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8040 tree t
= TREE_VEC_ELT (parmvec
, i
);
8042 if (unify (tparms
, targs
, t
,
8043 TREE_VEC_ELT (argvec
, i
),
8048 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
8053 /* If PARM is `const T' and ARG is only `int', we don't have
8054 a match unless we are allowing additional qualification.
8055 If ARG is `const int' and PARM is just `T' that's OK;
8056 that binds `const int' to `T'. */
8057 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
8061 /* Consider the case where ARG is `const volatile int' and
8062 PARM is `const T'. Then, T should be `volatile int'. */
8064 cp_build_qualified_type (arg
,
8066 & ~CP_TYPE_QUALS (parm
));
8069 /* Simple cases: Value already set, does match or doesn't. */
8070 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8075 /* Make sure that ARG is not a variable-sized array. (Note that
8076 were talking about variable-sized arrays (like `int[n]'),
8077 rather than arrays of unknown size (like `int[]').) We'll
8078 get very confused by such a type since the bound of the array
8079 will not be computable in an instantiation. Besides, such
8080 types are not allowed in ISO C++, so we can do as we please
8082 if (TREE_CODE (arg
) == ARRAY_TYPE
8083 && !uses_template_parms (arg
)
8084 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8088 TREE_VEC_ELT (targs
, idx
) = arg
;
8091 case TEMPLATE_PARM_INDEX
:
8092 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8094 if (TEMPLATE_PARM_LEVEL (parm
)
8095 != template_decl_level (tparm
))
8096 /* The PARM is not one we're trying to unify. Just check
8097 to see if it matches ARG. */
8098 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8099 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8101 idx
= TEMPLATE_PARM_IDX (parm
);
8102 targ
= TREE_VEC_ELT (targs
, idx
);
8106 int i
= (cp_tree_equal (targ
, arg
) > 0);
8112 my_friendly_abort (42);
8115 /* [temp.deduct.type] If, in the declaration of a function template
8116 with a non-type template-parameter, the non-type
8117 template-parameter is used in an expression in the function
8118 parameter-list and, if the corresponding template-argument is
8119 deduced, the template-argument type shall match the type of the
8120 template-parameter exactly, except that a template-argument
8121 deduced from an array bound may be of any integral type. */
8122 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
8124 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8125 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8126 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8131 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
8138 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
8139 return (unify (tparms
, targs
, parm
,
8140 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
));
8142 if (TREE_CODE (arg
) != POINTER_TYPE
)
8145 /* [temp.deduct.call]
8147 A can be another pointer or pointer to member type that can
8148 be converted to the deduced A via a qualification
8149 conversion (_conv.qual_).
8151 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8152 This will allow for additional cv-qualification of the
8153 pointed-to types if appropriate. In general, this is a bit
8154 too generous; we are only supposed to allow qualification
8155 conversions and this method will allow an ARG of char** and
8156 a deduced ARG of const char**. However, overload
8157 resolution will subsequently invalidate the candidate, so
8158 this is probably OK. */
8159 sub_strict
= strict
;
8161 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
8162 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg
)))
8163 /* The derived-to-base conversion only persists through one
8164 level of pointers. */
8165 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
8167 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE
8171 case REFERENCE_TYPE
:
8172 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8174 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8178 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8180 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8181 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8183 if (TYPE_DOMAIN (parm
) != NULL_TREE
8184 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8185 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8187 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8195 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8198 if (TREE_CODE (parm
) == INTEGER_TYPE
8199 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8201 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8202 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8203 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8205 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8206 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8207 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8210 /* We use the TYPE_MAIN_VARIANT since we have already
8211 checked cv-qualification at the top of the
8213 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
8214 TYPE_MAIN_VARIANT (parm
)))
8217 /* As far as unification is concerned, this wins. Later checks
8218 will invalidate it if necessary. */
8221 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8222 /* Type INTEGER_CST can come from ordinary constant template args. */
8224 while (TREE_CODE (arg
) == NOP_EXPR
)
8225 arg
= TREE_OPERAND (arg
, 0);
8227 if (TREE_CODE (arg
) != INTEGER_CST
)
8229 return !tree_int_cst_equal (parm
, arg
);
8234 if (TREE_CODE (arg
) != TREE_VEC
)
8236 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8238 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
8239 if (unify (tparms
, targs
,
8240 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8248 if (TYPE_PTRMEMFUNC_FLAG (parm
))
8249 return unify (tparms
, targs
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8252 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8255 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8259 if (strict
& UNIFY_ALLOW_DERIVED
)
8261 /* First, we try to unify the PARM and ARG directly. */
8262 t
= try_class_unification (tparms
, targs
,
8267 /* Fallback to the special case allowed in
8270 If P is a class, and P has the form
8271 template-id, then A can be a derived class of
8272 the deduced A. Likewise, if P is a pointer to
8273 a class of the form template-id, A can be a
8274 pointer to a derived class pointed to by the
8276 t
= get_template_base (tparms
, targs
,
8279 if (! t
|| t
== error_mark_node
)
8283 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8284 && (CLASSTYPE_TI_TEMPLATE (parm
)
8285 == CLASSTYPE_TI_TEMPLATE (arg
)))
8286 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8287 Then, we should unify `int' and `U'. */
8290 /* There's no chance of unication succeeding. */
8293 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8294 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8296 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
8297 TYPE_MAIN_VARIANT (arg
)))
8303 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8306 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8307 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8309 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8310 TYPE_ARG_TYPES (arg
), 1,
8314 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8316 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8317 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8319 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8323 if (arg
!= decl_constant_value (parm
))
8328 /* Matched cases are handled by the ARG == PARM test above. */
8332 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8334 /* We handle this case specially, since it comes up with
8335 arrays. In particular, something like:
8337 template <int N> void f(int (&x)[N]);
8339 Here, we are trying to unify the range type, which
8340 looks like [0 ... (N - 1)]. */
8342 t1
= TREE_OPERAND (parm
, 0);
8343 t2
= TREE_OPERAND (parm
, 1);
8345 /* Should this be a regular fold? */
8346 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
8350 return unify (tparms
, targs
, t1
, t
, strict
);
8352 /* else fall through */
8355 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8356 /* We're looking at an expression. This can happen with
8360 void foo(S<I>, S<I + 2>);
8362 This is a "nondeduced context":
8366 The nondeduced contexts are:
8368 --A type that is a template-id in which one or more of
8369 the template-arguments is an expression that references
8370 a template-parameter.
8372 In these cases, we assume deduction succeeded, but don't
8373 actually infer any unifications. */
8376 sorry ("use of `%s' in template type unification",
8377 tree_code_name
[(int) TREE_CODE (parm
)]);
8383 /* Called if RESULT is explicitly instantiated, or is a member of an
8384 explicitly instantiated class, or if using -frepo and the
8385 instantiation of RESULT has been assigned to this file. */
8388 mark_decl_instantiated (result
, extern_p
)
8392 if (TREE_CODE (result
) != FUNCTION_DECL
)
8393 /* The TREE_PUBLIC flag for function declarations will have been
8394 set correctly by tsubst. */
8395 TREE_PUBLIC (result
) = 1;
8399 DECL_INTERFACE_KNOWN (result
) = 1;
8400 DECL_NOT_REALLY_EXTERN (result
) = 1;
8402 /* Always make artificials weak. */
8403 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8404 comdat_linkage (result
);
8405 /* For WIN32 we also want to put explicit instantiations in
8406 linkonce sections. */
8407 else if (TREE_PUBLIC (result
))
8408 maybe_make_one_only (result
);
8410 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8411 mark_inline_for_output (result
);
8414 /* Given two function templates PAT1 and PAT2, and explicit template
8415 arguments EXPLICIT_ARGS return:
8417 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8418 -1 if PAT2 is more specialized than PAT1.
8419 0 if neither is more specialized. */
8422 more_specialized (pat1
, pat2
, explicit_args
)
8423 tree pat1
, pat2
, explicit_args
;
8428 targs
= get_bindings_overload (pat1
, DECL_RESULT (pat2
), explicit_args
);
8432 targs
= get_bindings_overload (pat2
, DECL_RESULT (pat1
), explicit_args
);
8439 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8441 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8442 -1 if PAT2 is more specialized than PAT1.
8443 0 if neither is more specialized. */
8446 more_specialized_class (pat1
, pat2
)
8452 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8453 TREE_PURPOSE (pat2
));
8457 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8458 TREE_PURPOSE (pat1
));
8465 /* Return the template arguments that will produce the function signature
8466 DECL from the function template FN, with the explicit template
8467 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8468 also match. Return NULL_TREE if no satisfactory arguments could be
8472 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8473 tree fn
, decl
, explicit_args
;
8476 int ntparms
= DECL_NTPARMS (fn
);
8477 tree targs
= make_scratch_vec (ntparms
);
8479 tree decl_arg_types
;
8482 /* Substitute the explicit template arguments into the type of DECL.
8483 The call to fn_type_unification will handle substitution into the
8485 decl_type
= TREE_TYPE (decl
);
8486 if (explicit_args
&& uses_template_parms (decl_type
))
8489 tree converted_args
;
8491 if (DECL_TEMPLATE_INFO (decl
))
8492 tmpl
= DECL_TI_TEMPLATE (decl
);
8494 /* We can get here for some illegal specializations. */
8498 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
8499 explicit_args
, NULL_TREE
,
8501 /*require_all_arguments=*/0));
8502 if (converted_args
== error_mark_node
)
8505 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
8507 if (decl_type
== error_mark_node
)
8511 /* If FN is a static member function, adjust the type of DECL
8513 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
8514 if (DECL_STATIC_FUNCTION_P (fn
)
8515 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8516 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8518 i
= fn_type_unification (fn
, explicit_args
, targs
,
8520 TREE_TYPE (decl_type
),
8528 /* Check to see that the resulting return type is also OK. */
8529 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
8530 /*complain=*/0, NULL_TREE
);
8532 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
8539 /* For most uses, we want to check the return type. */
8542 get_bindings (fn
, decl
, explicit_args
)
8543 tree fn
, decl
, explicit_args
;
8545 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8548 /* But for more_specialized, we only care about the parameter types. */
8551 get_bindings_overload (fn
, decl
, explicit_args
)
8552 tree fn
, decl
, explicit_args
;
8554 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8557 /* Return the innermost template arguments that, when applied to a
8558 template specialization whose innermost template parameters are
8559 TPARMS, and whose specialization arguments are ARGS, yield the
8562 For example, suppose we have:
8564 template <class T, class U> struct S {};
8565 template <class T> struct S<T*, int> {};
8567 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8568 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8569 int}. The resulting vector will be {double}, indicating that `T'
8570 is bound to `double'. */
8573 get_class_bindings (tparms
, parms
, args
)
8574 tree tparms
, parms
, args
;
8576 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8577 tree vec
= make_temp_vec (ntparms
);
8579 args
= innermost_args (args
);
8581 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
))
8584 for (i
= 0; i
< ntparms
; ++i
)
8585 if (! TREE_VEC_ELT (vec
, i
))
8591 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8592 Pick the most specialized template, and return the corresponding
8593 instantiation, or if there is no corresponding instantiation, the
8594 template itself. EXPLICIT_ARGS is any template arguments explicity
8595 mentioned in a template-id. If there is no most specialized
8596 tempalte, error_mark_node is returned. If there are no templates
8597 at all, NULL_TREE is returned. */
8600 most_specialized_instantiation (instantiations
, explicit_args
)
8601 tree instantiations
;
8607 if (!instantiations
)
8610 champ
= instantiations
;
8611 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
8613 fate
= more_specialized (TREE_VALUE (champ
),
8614 TREE_VALUE (fn
), explicit_args
);
8621 fn
= TREE_CHAIN (fn
);
8623 return error_mark_node
;
8629 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
8631 fate
= more_specialized (TREE_VALUE (champ
),
8632 TREE_VALUE (fn
), explicit_args
);
8634 return error_mark_node
;
8637 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
8640 /* Return the most specialized of the list of templates in FNS that can
8641 produce an instantiation matching DECL, given the explicit template
8642 arguments EXPLICIT_ARGS. */
8645 most_specialized (fns
, decl
, explicit_args
)
8646 tree fns
, decl
, explicit_args
;
8648 tree candidates
= NULL_TREE
;
8651 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
8653 tree candidate
= TREE_VALUE (fn
);
8655 args
= get_bindings (candidate
, decl
, explicit_args
);
8657 candidates
= scratch_tree_cons (NULL_TREE
, candidate
,
8661 return most_specialized_instantiation (candidates
, explicit_args
);
8664 /* If DECL is a specialization of some template, return the most
8665 general such template. For example, given:
8667 template <class T> struct S { template <class U> void f(U); };
8669 if TMPL is `template <class U> void S<int>::f(U)' this will return
8670 the full template. This function will not trace past partial
8671 specializations, however. For example, given in addition:
8673 template <class T> struct S<T*> { template <class U> void f(U); };
8675 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8676 `template <class T> template <class U> S<T*>::f(U)'. */
8679 most_general_template (decl
)
8682 while (DECL_TEMPLATE_INFO (decl
))
8683 decl
= DECL_TI_TEMPLATE (decl
);
8688 /* Return the most specialized of the class template specializations
8689 of TMPL which can produce an instantiation matching ARGS, or
8690 error_mark_node if the choice is ambiguous. */
8693 most_specialized_class (tmpl
, args
)
8697 tree list
= NULL_TREE
;
8702 tmpl
= most_general_template (tmpl
);
8703 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
8706 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
8709 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
8710 TREE_TYPE (list
) = TREE_TYPE (t
);
8720 for (; t
; t
= TREE_CHAIN (t
))
8722 fate
= more_specialized_class (champ
, t
);
8731 return error_mark_node
;
8737 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
8739 fate
= more_specialized_class (champ
, t
);
8741 return error_mark_node
;
8747 /* called from the parser. */
8750 do_decl_instantiation (declspecs
, declarator
, storage
)
8751 tree declspecs
, declarator
, storage
;
8753 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
8754 tree result
= NULL_TREE
;
8757 if (! DECL_LANG_SPECIFIC (decl
))
8759 cp_error ("explicit instantiation of non-template `%#D'", decl
);
8762 else if (TREE_CODE (decl
) == VAR_DECL
)
8764 /* There is an asymmetry here in the way VAR_DECLs and
8765 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8766 the latter, the DECL we get back will be marked as a
8767 template instantiation, and the appropriate
8768 DECL_TEMPLATE_INFO will be set up. This does not happen for
8769 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8770 should handle VAR_DECLs as it currently handles
8772 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
8773 if (result
&& TREE_CODE (result
) != VAR_DECL
)
8775 cp_error ("no matching template for `%D' found", result
);
8779 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
8781 cp_error ("explicit instantiation of `%#D'", decl
);
8787 /* Check for various error cases. Note that if the explicit
8788 instantiation is legal the RESULT will currently be marked as an
8789 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8790 until we get here. */
8792 if (DECL_TEMPLATE_SPECIALIZATION (result
))
8796 No program shall both explicitly instantiate and explicitly
8797 specialize a template. */
8798 cp_error ("explicit instantiation of `%#D' after", result
);
8799 cp_error_at ("explicit specialization here", result
);
8802 else if (DECL_EXPLICIT_INSTANTIATION (result
))
8806 No program shall explicitly instantiate any template more
8809 We check DECL_INTERFACE_KNOWN so as not to complain when the
8810 first instantiation was `extern' and the second is not, and
8811 EXTERN_P for the opposite case. */
8812 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
)
8813 cp_error ("duplicate explicit instantiation of `%#D'", result
);
8815 /* If we've already instantiated the template, just return now. */
8816 if (DECL_INTERFACE_KNOWN (result
))
8819 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
8821 cp_error ("no matching template for `%D' found", result
);
8824 else if (!DECL_TEMPLATE_INFO (result
))
8826 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
8830 if (flag_external_templates
)
8833 if (storage
== NULL_TREE
)
8835 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8838 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
8842 cp_error ("storage class `%D' applied to template instantiation",
8845 SET_DECL_EXPLICIT_INSTANTIATION (result
);
8846 mark_decl_instantiated (result
, extern_p
);
8847 repo_template_instantiated (result
, extern_p
);
8849 instantiate_decl (result
);
8853 mark_class_instantiated (t
, extern_p
)
8857 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
8858 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
8859 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
8860 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
8861 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
8864 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
8865 rest_of_type_compilation (t
, 1);
8870 do_type_instantiation (t
, storage
)
8877 if (TREE_CODE (t
) == TYPE_DECL
)
8880 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
8882 cp_error ("explicit instantiation of non-template type `%T'", t
);
8888 /* With -fexternal-templates, explicit instantiations are treated the same
8889 as implicit ones. */
8890 if (flag_external_templates
)
8893 if (TYPE_SIZE (t
) == NULL_TREE
)
8895 cp_error ("explicit instantiation of `%#T' before definition of template",
8900 if (storage
!= NULL_TREE
)
8903 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8904 IDENTIFIER_POINTER (storage
));
8906 if (storage
== ridpointers
[(int) RID_INLINE
])
8908 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8910 else if (storage
== ridpointers
[(int) RID_STATIC
])
8914 cp_error ("storage class `%D' applied to template instantiation",
8920 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
8924 No program shall both explicitly instantiate and explicitly
8925 specialize a template. */
8926 cp_error ("explicit instantiation of `%#T' after", t
);
8927 cp_error_at ("explicit specialization here", t
);
8930 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
8934 No program shall explicitly instantiate any template more
8937 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8938 instantiation was `extern', and if EXTERN_P then the second
8939 is. Both cases are OK. */
8940 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
)
8941 cp_error ("duplicate explicit instantiation of `%#T'", t
);
8943 /* If we've already instantiated the template, just return now. */
8944 if (!CLASSTYPE_INTERFACE_ONLY (t
))
8948 mark_class_instantiated (t
, extern_p
);
8949 repo_template_instantiated (t
, extern_p
);
8957 /* In contrast to implicit instantiation, where only the
8958 declarations, and not the definitions, of members are
8959 instantiated, we have here:
8963 The explicit instantiation of a class template specialization
8964 implies the instantiation of all of its members not
8965 previously explicitly specialized in the translation unit
8966 containing the explicit instantiation.
8968 Of course, we can't instantiate member template classes, since
8969 we don't have any arguments for them. Note that the standard
8970 is unclear on whether the instatiation of the members are
8971 *explicit* instantiations or not. We choose to be generous,
8972 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8973 the explicit instantiation of a class where some of the members
8974 have no definition in the current translation unit. */
8977 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8978 if (TREE_CODE (tmp
) == FUNCTION_DECL
8979 && DECL_TEMPLATE_INSTANTIATION (tmp
))
8981 mark_decl_instantiated (tmp
, extern_p
);
8982 repo_template_instantiated (tmp
, extern_p
);
8984 instantiate_decl (tmp
);
8987 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8988 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
8990 mark_decl_instantiated (tmp
, extern_p
);
8991 repo_template_instantiated (tmp
, extern_p
);
8993 instantiate_decl (tmp
);
8996 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8997 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
8998 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
8999 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
9003 /* Given a function DECL, which is a specialization of TMPL, modify
9004 DECL to be a re-instantiation of TMPL with the same template
9005 arguments. TMPL should be the template into which tsubst'ing
9006 should occur for DECL, not the most general template.
9008 One reason for doing this is a scenario like this:
9011 void f(const T&, int i);
9013 void g() { f(3, 7); }
9016 void f(const T& t, const int i) { }
9018 Note that when the template is first instantiated, with
9019 instantiate_template, the resulting DECL will have no name for the
9020 first parameter, and the wrong type for the second. So, when we go
9021 to instantiate the DECL, we regenerate it. */
9024 regenerate_decl_from_template (decl
, tmpl
)
9034 args
= DECL_TI_ARGS (decl
);
9035 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9037 /* Unregister the specialization so that when we tsubst we will not
9038 just return DECL. We don't have to unregister DECL from TMPL
9039 because if would only be registered there if it were a partial
9040 instantiation of a specialization, which it isn't: it's a full
9042 gen_tmpl
= most_general_template (tmpl
);
9043 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9045 /* If the DECL was not unregistered then something peculiar is
9046 happening: we created a specialization but did not call
9047 register_specialization for it. */
9048 my_friendly_assert (unregistered
, 0);
9050 if (TREE_CODE (decl
) == VAR_DECL
)
9051 /* Make sure that we can see identifiers, and compute access
9052 correctly, for the class members used in the declaration of
9053 this static variable. */
9054 pushclass (DECL_CONTEXT (decl
), 2);
9056 /* Do the substitution to get the new declaration. */
9057 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9059 if (TREE_CODE (decl
) == VAR_DECL
)
9061 /* Set up DECL_INITIAL, since tsubst doesn't. */
9062 DECL_INITIAL (new_decl
) =
9063 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9064 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9065 /* Pop the class context we pushed above. */
9068 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9070 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9072 DECL_INITIAL (new_decl
) = error_mark_node
;
9073 /* And don't complain about a duplicate definition. */
9074 DECL_INITIAL (decl
) = NULL_TREE
;
9077 /* The immediate parent of the new template is still whatever it was
9078 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9079 general template. We also reset the DECL_ASSEMBLER_NAME since
9080 tsubst always calculates the name as if the function in question
9081 were really a template instance, and sometimes, with friend
9082 functions, this is not so. See tsubst_friend_function for
9084 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9085 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9086 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9088 /* Call duplicate decls to merge the old and new declarations. */
9089 duplicate_decls (new_decl
, decl
);
9091 /* Now, re-register the specialization. */
9092 register_specialization (decl
, gen_tmpl
, args
);
9095 /* Produce the definition of D, a _DECL generated from a template. */
9098 instantiate_decl (d
)
9101 tree tmpl
= DECL_TI_TEMPLATE (d
);
9102 tree args
= DECL_TI_ARGS (d
);
9107 int nested
= in_function_p ();
9108 int pattern_defined
;
9110 char *file
= input_filename
;
9112 /* This function should only be used to instantiate templates for
9113 functions and static member variables. */
9114 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9115 || TREE_CODE (d
) == VAR_DECL
, 0);
9117 if (DECL_TEMPLATE_INSTANTIATED (d
))
9118 /* D has already been instantiated. It might seem reasonable to
9119 check whether or not D is an explict instantiation, and, if so,
9120 stop here. But when an explicit instantiation is deferred
9121 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9122 is set, even though we still need to do the instantiation. */
9125 /* If we already have a specialization of this declaration, then
9126 there's no reason to instantiate it. Note that
9127 retrieve_specialization gives us both instantiations and
9128 specializations, so we must explicitly check
9129 DECL_TEMPLATE_SPECIALIZATION. */
9130 gen_tmpl
= most_general_template (tmpl
);
9131 spec
= retrieve_specialization (gen_tmpl
, args
);
9132 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9135 /* This needs to happen before any tsubsting. */
9136 if (! push_tinst_level (d
))
9139 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9140 for the instantiation. This is not always the most general
9141 template. Consider, for example:
9144 struct S { template <class U> void f();
9145 template <> void f<int>(); };
9147 and an instantiation of S<double>::f<int>. We want TD to be the
9148 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9151 /* An instantiation cannot have a definition, so we need a
9152 more general template. */
9153 DECL_TEMPLATE_INSTANTIATION (td
)
9154 /* We must also deal with friend templates. Given:
9156 template <class T> struct S {
9157 template <class U> friend void f() {};
9160 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9161 so far as the language is concerned, but that's still
9162 where we get the pattern for the instantiation from. On
9163 ther hand, if the definition comes outside the class, say:
9165 template <class T> struct S {
9166 template <class U> friend void f();
9168 template <class U> friend void f() {}
9170 we don't need to look any further. That's what the check for
9171 DECL_INITIAL is for. */
9172 || (TREE_CODE (d
) == FUNCTION_DECL
9173 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9174 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
9177 /* The present template, TD, should not be a definition. If it
9178 were a definition, we should be using it! Note that we
9179 cannot restructure the loop to just keep going until we find
9180 a template with a definition, since that might go too far if
9181 a specialization was declared, but not defined. */
9182 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9183 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9186 /* Fetch the more general template. */
9187 td
= DECL_TI_TEMPLATE (td
);
9190 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9192 if (TREE_CODE (d
) == FUNCTION_DECL
)
9193 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
9195 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9197 push_to_top_level ();
9198 lineno
= DECL_SOURCE_LINE (d
);
9199 input_filename
= DECL_SOURCE_FILE (d
);
9201 if (pattern_defined
)
9203 repo_template_used (d
);
9205 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9207 if (flag_alt_external_templates
)
9209 if (interface_unknown
)
9210 warn_if_unknown_interface (d
);
9212 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9214 DECL_INTERFACE_KNOWN (d
) = 1;
9215 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9218 warn_if_unknown_interface (code_pattern
);
9222 import_export_decl (d
);
9225 /* Reject all external templates except inline functions. */
9226 if (DECL_INTERFACE_KNOWN (d
)
9227 && ! DECL_NOT_REALLY_EXTERN (d
)
9228 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9231 if (TREE_CODE (d
) == VAR_DECL
9232 && TREE_READONLY (d
)
9233 && DECL_INITIAL (d
) == NULL_TREE
9234 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
9235 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9236 the variable is a static const initialized in the class body. */;
9237 else if (! pattern_defined
9238 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
9241 /* Defer all templates except inline functions used in another
9242 function. We restore the source position here because it's used
9243 by add_pending_template. */
9245 input_filename
= file
;
9247 if (at_eof
&& !pattern_defined
9248 && DECL_EXPLICIT_INSTANTIATION (d
))
9251 The definition of a non-exported function template, a
9252 non-exported member function template, or a non-exported
9253 member function or static data member of a class template
9254 shall be present in every translation unit in which it is
9255 explicitly instantiated. */
9256 cp_error ("explicit instantiation of `%D' but no definition available",
9259 add_pending_template (d
);
9263 /* We're now committed to instantiating this template. Mark it as
9264 instantiated so that recursive calls to instantiate_decl do not
9265 try to instantiate it again. */
9266 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9268 /* Regenerate the declaration in case the template has been modified
9269 by a subsequent redeclaration. */
9270 regenerate_decl_from_template (d
, td
);
9272 /* We already set the file and line above. Reset them now in case
9273 they changed as a result of calling regenerate_decl_from_template. */
9274 lineno
= DECL_SOURCE_LINE (d
);
9275 input_filename
= DECL_SOURCE_FILE (d
);
9277 if (TREE_CODE (d
) == VAR_DECL
)
9279 DECL_IN_AGGR_P (d
) = 0;
9280 if (DECL_INTERFACE_KNOWN (d
))
9281 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9284 DECL_EXTERNAL (d
) = 1;
9285 DECL_NOT_REALLY_EXTERN (d
) = 1;
9287 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
9289 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9291 tree t
= DECL_SAVED_TREE (code_pattern
);
9293 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
9294 store_parm_decls ();
9296 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
9299 (TREE_OPERAND (t
, 0),
9300 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, tmpl
));
9304 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
9306 current_member_init_list
9307 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
9308 current_base_init_list
9309 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
9314 /* Always keep the BLOCK node associated with the outermost
9315 pair of curly braces of a function. These are needed
9316 for correct operation of dwarfout.c. */
9319 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
9320 tsubst_expr (t
, args
, /*complain=*/1, tmpl
);
9322 finish_function (lineno
, 0, nested
);
9327 input_filename
= file
;
9329 pop_from_top_level ();
9335 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9336 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9337 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9340 tsubst_expr_values (t
, argvec
)
9343 tree first
= NULL_TREE
;
9346 for (; t
; t
= TREE_CHAIN (t
))
9348 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
9349 /*complain=*/1, NULL_TREE
);
9350 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
9352 *p
= build_tree_list (pur
, val
);
9353 p
= &TREE_CHAIN (*p
);
9364 last_tree
= TREE_CHAIN (last_tree
) = t
;
9371 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
9372 last_tree
= NULL_TREE
;
9379 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
9381 last_tree
= TREE_VALUE (saved_trees
);
9382 saved_trees
= TREE_CHAIN (saved_trees
);
9385 /* D is an undefined function declaration in the presence of templates with
9386 the same name, listed in FNS. If one of them can produce D as an
9387 instantiation, remember this so we can instantiate it at EOF if D has
9388 not been defined by that time. */
9391 add_maybe_template (d
, fns
)
9396 if (DECL_MAYBE_TEMPLATE (d
))
9399 t
= most_specialized (fns
, d
, NULL_TREE
);
9402 if (t
== error_mark_node
)
9404 cp_error ("ambiguous template instantiation for `%D'", d
);
9408 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
9409 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
9410 DECL_MAYBE_TEMPLATE (d
) = 1;
9413 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9416 set_current_access_from_decl (decl
)
9419 if (TREE_PRIVATE (decl
))
9420 current_access_specifier
= access_private_node
;
9421 else if (TREE_PROTECTED (decl
))
9422 current_access_specifier
= access_protected_node
;
9424 current_access_specifier
= access_public_node
;
9427 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9428 is the instantiation (which should have been created with
9429 start_enum) and ARGS are the template arguments to use. */
9432 tsubst_enum (tag
, newtag
, args
)
9439 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9444 /* Note that in a template enum, the TREE_VALUE is the
9445 CONST_DECL, not the corresponding INTEGER_CST. */
9446 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9447 args
, /*complain=*/1,
9450 /* Give this enumeration constant the correct access. */
9451 set_current_access_from_decl (TREE_VALUE (e
));
9453 /* Actually build the enumerator itself. */
9454 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9456 /* We save the enumerators we have built so far in the
9457 TYPE_VALUES so that if the enumeration constants for
9458 subsequent enumerators involve those for previous ones,
9459 tsubst_copy will be able to find them. */
9460 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
9461 TYPE_VALUES (newtag
) = elt
;
9464 finish_enum (newtag
);
9467 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9468 is either an instantiation or specialization of a template
9472 set_mangled_name_for_template_decl (decl
)
9475 tree saved_namespace
;
9476 tree context
= NULL_TREE
;
9485 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
9486 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
9488 /* The names of template functions must be mangled so as to indicate
9489 what template is being specialized with what template arguments.
9490 For example, each of the following three functions must get
9491 different mangled names:
9494 template <> void f<7>(int);
9495 template <> void f<8>(int); */
9497 targs
= DECL_TI_ARGS (decl
);
9498 if (uses_template_parms (targs
))
9499 /* This DECL is for a partial instantiation. There's no need to
9500 mangle the name of such an entity. */
9503 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9504 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
9505 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
9507 /* There should be as many levels of arguments as there are levels
9509 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
9511 /* We now compute the PARMS and RET_TYPE to give to
9512 build_decl_overload_real. The PARMS and RET_TYPE are the
9513 parameter and return types of the template, after all but the
9514 innermost template arguments have been substituted, not the
9515 parameter and return types of the function DECL. For example,
9518 template <class T> T f(T);
9520 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
9521 A more subtle example is:
9523 template <class T> struct S { template <class U> void f(T, U); }
9525 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
9526 {int, U}. Thus, the args that we want to subsitute into the
9527 return and parameter type for the function are those in TARGS,
9528 with the innermost level omitted. */
9529 fn_type
= TREE_TYPE (tmpl
);
9530 if (DECL_STATIC_FUNCTION_P (decl
))
9531 context
= DECL_CLASS_CONTEXT (decl
);
9533 if (parm_depth
== 1)
9534 /* No substitution is necessary. */
9541 /* Replace the innermost level of the TARGS with NULL_TREEs to
9542 let tsubst know not to subsitute for those parameters. */
9543 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
9544 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
9545 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
9546 TMPL_ARGS_LEVEL (targs
, i
));
9547 SET_TMPL_ARGS_LEVEL (partial_args
,
9548 TMPL_ARGS_DEPTH (targs
),
9549 make_temp_vec (DECL_NTPARMS (tmpl
)));
9551 /* Now, do the (partial) substitution to figure out the
9552 appropriate function type. */
9553 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
9554 if (DECL_STATIC_FUNCTION_P (decl
))
9555 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
9557 /* Substitute into the template parameters to obtain the real
9558 innermost set of parameters. This step is important if the
9559 innermost set of template parameters contains value
9560 parameters whose types depend on outer template parameters. */
9561 TREE_VEC_LENGTH (partial_args
)--;
9562 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
9565 /* Now, get the innermost parameters and arguments, and figure out
9566 the parameter and return types. */
9567 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
9568 targs
= innermost_args (targs
);
9569 ret_type
= TREE_TYPE (fn_type
);
9570 parm_types
= TYPE_ARG_TYPES (fn_type
);
9572 /* For a static member function, we generate a fake `this' pointer,
9573 for the purposes of mangling. This indicates of which class the
9574 function is a member. Because of:
9578 There shall not be a static and a nonstatic member function
9579 with the same name and the same parameter types
9581 we don't have to worry that this will result in a clash with a
9582 non-static member function. */
9583 if (DECL_STATIC_FUNCTION_P (decl
))
9584 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
9586 /* There should be the same number of template parameters as
9587 template arguments. */
9588 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
9591 /* If the template is in a namespace, we need to put that into the
9592 mangled name. Unfortunately, build_decl_overload_real does not
9593 get the decl to mangle, so it relies on the current
9594 namespace. Therefore, we set that here temporarily. */
9595 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
9596 saved_namespace
= current_namespace
;
9597 current_namespace
= CP_DECL_CONTEXT (decl
);
9599 /* Actually set the DCL_ASSEMBLER_NAME. */
9600 DECL_ASSEMBLER_NAME (decl
)
9601 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
9603 DECL_FUNCTION_MEMBER_P (decl
)
9604 + DECL_CONSTRUCTOR_P (decl
));
9606 /* Restore the previously active namespace. */
9607 current_namespace
= saved_namespace
;