1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 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
79 static int unify
PROTO((tree
, tree
, tree
, tree
, int, int*));
80 static int resolve_overloaded_unification
PROTO((tree
, tree
, tree
, tree
,
81 unification_kind_t
, int,
83 static int try_one_overload
PROTO((tree
, tree
, tree
, tree
,
84 unification_kind_t
, int, int*));
85 static int unify
PROTO((tree
, tree
, tree
, tree
, int, int*));
86 static void add_pending_template
PROTO((tree
));
87 static int push_tinst_level
PROTO((tree
));
88 static tree classtype_mangled_name
PROTO((tree
));
89 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
90 static tree tsubst_expr_values
PROTO((tree
, tree
));
91 static int list_eq
PROTO((tree
, tree
));
92 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
93 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int));
94 static void tsubst_enum
PROTO((tree
, tree
, tree
));
95 static tree add_to_template_args
PROTO((tree
, tree
));
96 static tree add_outermost_template_args
PROTO((tree
, tree
));
97 static void maybe_adjust_types_for_deduction
PROTO((unification_kind_t
, tree
*,
99 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
100 int, unification_kind_t
, int, int*));
101 static void note_template_header
PROTO((int));
102 static tree maybe_fold_nontype_arg
PROTO((tree
));
103 static tree convert_nontype_argument
PROTO((tree
, tree
));
104 static tree convert_template_argument
PROTO ((tree
, tree
, tree
, int,
106 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
107 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
108 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
109 static int inline_needs_template_parms
PROTO((tree
));
110 static void push_inline_template_parms_recursive
PROTO((tree
, int));
111 static tree retrieve_specialization
PROTO((tree
, tree
));
112 static tree register_specialization
PROTO((tree
, tree
, tree
));
113 static int unregister_specialization
PROTO((tree
, tree
));
114 static void print_candidates
PROTO((tree
));
115 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
116 static tree build_template_decl
PROTO((tree
, tree
));
117 static int mark_template_parm
PROTO((tree
, void *));
118 static tree tsubst_friend_function
PROTO((tree
, tree
));
119 static tree tsubst_friend_class
PROTO((tree
, tree
));
120 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
121 static int template_decl_level
PROTO((tree
));
122 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
123 static int check_cv_quals_for_unify
PROTO((int, tree
, tree
));
124 static tree tsubst_template_arg_vector
PROTO((tree
, tree
));
125 static tree tsubst_template_parms
PROTO((tree
, tree
));
126 static void regenerate_decl_from_template
PROTO((tree
, tree
));
127 static tree most_specialized
PROTO((tree
, tree
, tree
));
128 static tree most_specialized_class
PROTO((tree
, tree
));
129 static tree most_general_template
PROTO((tree
));
130 static void set_mangled_name_for_template_decl
PROTO((tree
));
131 static int template_class_depth_real
PROTO((tree
, int));
132 static tree tsubst_aggr_type
PROTO((tree
, tree
, tree
, int));
133 static tree tsubst_decl
PROTO((tree
, tree
, tree
, tree
));
134 static tree tsubst_arg_types
PROTO((tree
, tree
, tree
));
135 static void check_specialization_scope
PROTO((void));
136 static tree process_partial_specialization
PROTO((tree
));
137 static void set_current_access_from_decl
PROTO((tree
));
139 /* We use TREE_VECs to hold template arguments. If there is only one
140 level of template arguments, then the TREE_VEC contains the
141 arguments directly. If there is more than one level of template
142 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
143 containing the template arguments for a single level. The first
144 entry in the outer TREE_VEC is the outermost level of template
145 parameters; the last is the innermost.
147 It is incorrect to ever form a template argument vector containing
148 only one level of arguments, but which is a TREE_VEC containing as
149 its only entry the TREE_VEC for that level. */
151 /* Non-zero if the template arguments is actually a vector of vectors,
152 rather than just a vector. */
153 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
155 && TREE_CODE (NODE) == TREE_VEC \
156 && TREE_VEC_LENGTH (NODE) > 0 \
157 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
158 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
160 /* The depth of a template argument vector. When called directly by
161 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
162 template arguments. In fact, we may even see NULL_TREE if there
163 are no template arguments. In both of those cases, there is only
164 one level of template arguments. */
165 #define TMPL_ARGS_DEPTH(NODE) \
166 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
168 /* The LEVELth level of the template ARGS. Note that template
169 parameter levels are indexed from 1, not from 0. */
170 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
171 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
172 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
174 /* Set the LEVELth level of the template ARGS to VAL. This macro does
175 not work with single-level argument vectors. */
176 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
177 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
179 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
180 #define TMPL_ARG(ARGS, LEVEL, IDX) \
181 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
183 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
184 macro does not work with single-level argument vectors. */
185 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
186 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
188 /* Given a single level of template arguments in NODE, return the
189 number of arguments. */
190 #define NUM_TMPL_ARGS(NODE) \
191 ((NODE) == NULL_TREE ? 0 \
192 : (TREE_CODE (NODE) == TREE_VEC \
193 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
195 /* The number of levels of template parameters given by NODE. */
196 #define TMPL_PARMS_DEPTH(NODE) \
197 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
199 /* Do any processing required when DECL (a member template declaration
200 using TEMPLATE_PARAMETERS as its innermost parameter list) is
201 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
202 it is a specialization, in which case the DECL itself is returned. */
205 finish_member_template_decl (decl
)
208 if (decl
== NULL_TREE
|| decl
== void_type_node
)
210 else if (decl
== error_mark_node
)
211 /* By returning NULL_TREE, the parser will just ignore this
212 declaration. We have already issued the error. */
214 else if (TREE_CODE (decl
) == TREE_LIST
)
216 /* Assume that the class is the only declspec. */
217 decl
= TREE_VALUE (decl
);
218 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
219 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
221 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
222 check_member_template (tmpl
);
227 else if (DECL_TEMPLATE_INFO (decl
))
229 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
231 check_member_template (DECL_TI_TEMPLATE (decl
));
232 return DECL_TI_TEMPLATE (decl
);
238 cp_error ("invalid member template declaration `%D'", decl
);
240 return error_mark_node
;
243 /* Returns the template nesting level of the indicated class TYPE.
253 A<T>::B<U> has depth two, while A<T> has depth one.
254 Both A<T>::B<int> and A<int>::B<U> have depth one, if
255 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
258 This function is guaranteed to return 0 if passed NULL_TREE so
259 that, for example, `template_class_depth (current_class_type)' is
263 template_class_depth_real (type
, count_specializations
)
265 int count_specializations
;
270 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
271 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
272 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
274 if (TREE_CODE (type
) != FUNCTION_DECL
)
276 if (CLASSTYPE_TEMPLATE_INFO (type
)
277 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
278 && ((count_specializations
279 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
280 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
285 if (DECL_TEMPLATE_INFO (type
)
286 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
287 && ((count_specializations
288 && DECL_TEMPLATE_SPECIALIZATION (type
))
289 || uses_template_parms (DECL_TI_ARGS (type
))))
297 /* Returns the template nesting level of the indicated class TYPE.
298 Like template_class_depth_real, but instantiations do not count in
302 template_class_depth (type
)
305 return template_class_depth_real (type
, /*count_specializations=*/0);
308 /* Returns 1 if processing DECL as part of do_pending_inlines
309 needs us to push template parms. */
312 inline_needs_template_parms (decl
)
315 if (! DECL_TEMPLATE_INFO (decl
))
318 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
319 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
322 /* Subroutine of maybe_begin_member_template_processing.
323 Push the template parms in PARMS, starting from LEVELS steps into the
324 chain, and ending at the beginning, since template parms are listed
328 push_inline_template_parms_recursive (parmlist
, levels
)
332 tree parms
= TREE_VALUE (parmlist
);
336 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
338 ++processing_template_decl
;
339 current_template_parms
340 = tree_cons (build_int_2 (0, processing_template_decl
),
341 parms
, current_template_parms
);
342 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
345 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
347 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
348 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
350 switch (TREE_CODE (parm
))
359 /* Make a CONST_DECL as is done in process_template_parm. */
360 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
362 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
368 my_friendly_abort (0);
373 /* Restore the template parameter context for a member template or
374 a friend template defined in a class definition. */
377 maybe_begin_member_template_processing (decl
)
383 if (! inline_needs_template_parms (decl
))
386 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
388 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
390 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
393 parms
= TREE_CHAIN (parms
);
396 push_inline_template_parms_recursive (parms
, levels
);
399 /* Undo the effects of begin_member_template_processing. */
402 maybe_end_member_template_processing ()
404 if (! processing_template_decl
)
407 while (current_template_parms
408 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
410 --processing_template_decl
;
411 current_template_parms
= TREE_CHAIN (current_template_parms
);
416 /* Returns non-zero iff T is a member template function. We must be
419 template <class T> class C { void f(); }
421 Here, f is a template function, and a member, but not a member
422 template. This function does not concern itself with the origin of
423 T, only its present state. So if we have
425 template <class T> class C { template <class U> void f(U); }
427 then neither C<int>::f<char> nor C<T>::f<double> is considered
428 to be a member template. */
431 is_member_template (t
)
434 if (TREE_CODE (t
) != FUNCTION_DECL
435 && !DECL_FUNCTION_TEMPLATE_P (t
))
436 /* Anything that isn't a function or a template function is
437 certainly not a member template. */
440 /* A local class can't have member templates. */
441 if (hack_decl_function_context (t
))
444 if ((DECL_FUNCTION_MEMBER_P (t
)
445 && !DECL_TEMPLATE_SPECIALIZATION (t
))
446 || (TREE_CODE (t
) == TEMPLATE_DECL
447 && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
451 if (DECL_FUNCTION_TEMPLATE_P (t
))
453 else if (DECL_TEMPLATE_INFO (t
)
454 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
455 tmpl
= DECL_TI_TEMPLATE (t
);
460 /* If there are more levels of template parameters than
461 there are template classes surrounding the declaration,
462 then we have a member template. */
463 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)) >
464 template_class_depth (DECL_CLASS_CONTEXT (t
))))
472 /* Returns non-zero iff T is a member template class. See
473 is_member_template for a description of what precisely constitutes
474 a member template. */
477 is_member_template_class (t
)
480 if (!DECL_CLASS_TEMPLATE_P (t
))
481 /* Anything that isn't a class template, is certainly not a member
485 if (!DECL_CLASS_SCOPE_P (t
))
486 /* Anything whose context isn't a class type is surely not a
490 /* If there are more levels of template parameters than there are
491 template classes surrounding the declaration, then we have a
493 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
494 template_class_depth (DECL_CONTEXT (t
)));
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. The
500 resulting vector will be built on a temporary obstack, and so must
501 be explicitly copied to the permanent obstack, if required. */
504 add_to_template_args (args
, extra_args
)
513 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
514 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
516 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
517 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
519 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
520 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
533 add_outermost_template_args (args
, extra_args
)
539 /* If there are more levels of EXTRA_ARGS than there are ARGS,
540 something very fishy is going on. */
541 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
544 /* If *all* the new arguments will be the EXTRA_ARGS, just return
546 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
549 /* For the moment, we make ARGS look like it contains fewer levels. */
550 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
552 new_args
= add_to_template_args (args
, extra_args
);
554 /* Now, we restore ARGS to its full dimensions. */
555 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
560 /* We've got a template header coming up; push to a new level for storing
564 begin_template_parm_list ()
566 /* We use a non-tag-transparent scope here, which causes pushtag to
567 put tags in this scope, rather than in the enclosing class or
568 namespace scope. This is the right thing, since we want
569 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
570 global template class, push_template_decl handles putting the
571 TEMPLATE_DECL into top-level scope. For a nested template class,
574 template <class T> struct S1 {
575 template <class T> struct S2 {};
578 pushtag contains special code to call pushdecl_with_scope on the
579 TEMPLATE_DECL for S2. */
581 declare_pseudo_global_level ();
582 ++processing_template_decl
;
583 ++processing_template_parmlist
;
584 note_template_header (0);
587 /* This routine is called when a specialization is declared. If it is
588 illegal to declare a specialization here, an error is reported. */
591 check_specialization_scope ()
593 tree scope
= current_scope ();
597 An explicit specialization shall be declared in the namespace of
598 which the template is a member, or, for member templates, in the
599 namespace of which the enclosing class or enclosing class
600 template is a member. An explicit specialization of a member
601 function, member class or static data member of a class template
602 shall be declared in the namespace of which the class template
604 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
605 cp_error ("explicit specialization in non-namespace scope `%D'",
610 In an explicit specialization declaration for a member of a class
611 template or a member template that appears in namespace scope,
612 the member template and some of its enclosing class templates may
613 remain unspecialized, except that the declaration shall not
614 explicitly specialize a class member template if its enclosing
615 class templates are not explicitly specialized as well. */
616 if (current_template_parms
)
617 cp_error ("enclosing class templates are not explicitly specialized");
620 /* We've just seen template <>. */
623 begin_specialization ()
625 note_template_header (1);
626 check_specialization_scope ();
629 /* Called at then end of processing a declaration preceeded by
633 end_specialization ()
635 reset_specialization ();
638 /* Any template <>'s that we have seen thus far are not referring to a
639 function specialization. */
642 reset_specialization ()
644 processing_specialization
= 0;
645 template_header_count
= 0;
648 /* We've just seen a template header. If SPECIALIZATION is non-zero,
649 it was of the form template <>. */
652 note_template_header (specialization
)
655 processing_specialization
= specialization
;
656 template_header_count
++;
659 /* We're beginning an explicit instantiation. */
662 begin_explicit_instantiation ()
664 ++processing_explicit_instantiation
;
669 end_explicit_instantiation ()
671 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
672 --processing_explicit_instantiation
;
675 /* The TYPE is being declared. If it is a template type, that means it
676 is a partial specialization. Do appropriate error-checking. */
679 maybe_process_partial_specialization (type
)
682 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
684 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
685 && TYPE_SIZE (type
) == NULL_TREE
)
687 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
688 if (processing_template_decl
)
689 push_template_decl (TYPE_MAIN_DECL (type
));
691 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
692 cp_error ("specialization of `%T' after instantiation", type
);
696 /* Retrieve the specialization (in the sense of [temp.spec] - a
697 specialization is either an instantiation or an explicit
698 specialization) of TMPL for the given template ARGS. If there is
699 no such specialization, return NULL_TREE. The ARGS are a vector of
700 arguments, or a vector of vectors of arguments, in the case of
701 templates with more than one level of parameters. */
704 retrieve_specialization (tmpl
, args
)
710 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
712 /* There should be as many levels of arguments as there are
713 levels of parameters. */
714 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
715 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
718 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
721 if (comp_template_args (TREE_PURPOSE (s
), args
))
722 return TREE_VALUE (s
);
727 /* Returns non-zero iff DECL is a specialization of TMPL. */
730 is_specialization_of (decl
, tmpl
)
736 if (TREE_CODE (decl
) == FUNCTION_DECL
)
740 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
746 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
748 for (t
= TREE_TYPE (decl
);
750 t
= CLASSTYPE_USE_TEMPLATE (t
)
751 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
752 if (same_type_p (TYPE_MAIN_VARIANT (t
),
753 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
760 /* Register the specialization SPEC as a specialization of TMPL with
761 the indicated ARGS. Returns SPEC, or an equivalent prior
762 declaration, if available. */
765 register_specialization (spec
, tmpl
, args
)
772 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
774 if (TREE_CODE (spec
) == FUNCTION_DECL
775 && uses_template_parms (DECL_TI_ARGS (spec
)))
776 /* This is the FUNCTION_DECL for a partial instantiation. Don't
777 register it; we want the corresponding TEMPLATE_DECL instead.
778 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
779 the more obvious `uses_template_parms (spec)' to avoid problems
780 with default function arguments. In particular, given
783 template <class T> void f(T t1, T t = T())
785 the default argument expression is not substituted for in an
786 instantiation unless and until it is actually needed. */
789 /* There should be as many levels of arguments as there are
790 levels of parameters. */
791 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
792 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
795 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
798 if (comp_template_args (TREE_PURPOSE (s
), args
))
800 tree fn
= TREE_VALUE (s
);
802 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
804 if (DECL_TEMPLATE_INSTANTIATION (fn
))
807 || DECL_EXPLICIT_INSTANTIATION (fn
))
809 cp_error ("specialization of %D after instantiation",
815 /* This situation should occur only if the first
816 specialization is an implicit instantiation,
817 the second is an explicit specialization, and
818 the implicit instantiation has not yet been
819 used. That situation can occur if we have
820 implicitly instantiated a member function and
821 then specialized it later.
823 We can also wind up here if a friend
824 declaration that looked like an instantiation
825 turns out to be a specialization:
827 template <class T> void foo(T);
828 class S { friend void foo<>(int) };
829 template <> void foo(int);
831 We transform the existing DECL in place so that
832 any pointers to it become pointers to the
833 updated declaration. */
834 duplicate_decls (spec
, TREE_VALUE (s
));
835 return TREE_VALUE (s
);
838 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
840 duplicate_decls (spec
, TREE_VALUE (s
));
841 return TREE_VALUE (s
);
846 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
847 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
852 /* Unregister the specialization SPEC as a specialization of TMPL.
853 Returns nonzero if the SPEC was listed as a specialization of
857 unregister_specialization (spec
, tmpl
)
863 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
865 s
= &TREE_CHAIN (*s
))
866 if (TREE_VALUE (*s
) == spec
)
868 *s
= TREE_CHAIN (*s
);
875 /* Print the list of candidate FNS in an error message. */
878 print_candidates (fns
)
883 char* str
= "candidates are:";
885 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
887 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
892 /* Returns the template (one of the functions given by TEMPLATE_ID)
893 which can be specialized to match the indicated DECL with the
894 explicit template args given in TEMPLATE_ID. If
895 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
896 member template. The template args (those explicitly specified and
897 those deduced) are output in a newly created vector *TARGS_OUT. If
898 it is impossible to determine the result, an error message is
899 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
903 determine_specialization (template_id
, decl
, targs_out
,
904 need_member_template
,
909 int need_member_template
;
913 tree templates
= NULL_TREE
;
916 *targs_out
= NULL_TREE
;
918 if (template_id
== error_mark_node
)
919 return error_mark_node
;
921 fns
= TREE_OPERAND (template_id
, 0);
922 targs_in
= TREE_OPERAND (template_id
, 1);
924 if (fns
== error_mark_node
)
925 return error_mark_node
;
927 /* Check for baselinks. */
928 if (TREE_CODE (fns
) == TREE_LIST
)
929 fns
= TREE_VALUE (fns
);
931 for (; fns
; fns
= OVL_NEXT (fns
))
935 fn
= OVL_CURRENT (fns
);
936 if (!need_member_template
937 && TREE_CODE (fn
) == FUNCTION_DECL
938 && DECL_FUNCTION_MEMBER_P (fn
)
939 && DECL_USE_TEMPLATE (fn
)
940 && DECL_TI_TEMPLATE (fn
))
941 /* We can get here when processing something like:
942 template <class T> class X { void f(); }
943 template <> void X<int>::f() {}
944 We're specializing a member function, but not a member
946 tmpl
= DECL_TI_TEMPLATE (fn
);
947 else if (TREE_CODE (fn
) != TEMPLATE_DECL
948 || (need_member_template
&& !is_member_template (fn
)))
953 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
956 if (decl
== NULL_TREE
)
958 /* Unify against ourselves to make sure that the args we have
959 make sense and there aren't any undeducible parms. It's OK if
960 not all the parms are specified; they might be deduced
962 tree targs
= get_bindings_overload (tmpl
, DECL_RESULT (tmpl
),
966 /* Unification was successful. */
967 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
970 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
973 if (decl
!= NULL_TREE
)
975 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
979 if (tmpl
== error_mark_node
)
981 else if (tmpl
== NULL_TREE
)
984 inner_args
= get_bindings (tmpl
, decl
, targs_in
);
985 tmpl_args
= DECL_TI_ARGS (DECL_RESULT (tmpl
));
986 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args
))
988 *targs_out
= copy_node (tmpl_args
);
989 SET_TMPL_ARGS_LEVEL (*targs_out
,
990 TMPL_ARGS_DEPTH (*targs_out
),
994 *targs_out
= inner_args
;
999 if (templates
== NULL_TREE
)
1004 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1006 return error_mark_node
;
1010 else if (TREE_CHAIN (templates
) != NULL_TREE
1011 || uses_template_parms (TREE_PURPOSE (templates
)))
1016 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1018 print_candidates (templates
);
1019 return error_mark_node
;
1024 /* We have one, and exactly one, match. */
1025 *targs_out
= TREE_PURPOSE (templates
);
1026 return TREE_VALUE (templates
);
1029 /* Check to see if the function just declared, as indicated in
1030 DECLARATOR, and in DECL, is a specialization of a function
1031 template. We may also discover that the declaration is an explicit
1032 instantiation at this point.
1034 Returns DECL, or an equivalent declaration that should be used
1037 FLAGS is a bitmask consisting of the following flags:
1039 2: The function has a definition.
1040 4: The function is a friend.
1042 The TEMPLATE_COUNT is the number of references to qualifying
1043 template classes that appeared in the name of the function. For
1046 template <class T> struct S { void f(); };
1049 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1050 classes are not counted in the TEMPLATE_COUNT, so that in
1052 template <class T> struct S {};
1053 template <> struct S<int> { void f(); }
1054 template <> void S<int>::f();
1056 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1057 illegal; there should be no template <>.)
1059 If the function is a specialization, it is marked as such via
1060 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1061 is set up correctly, and it is added to the list of specializations
1062 for that template. */
1065 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1071 int have_def
= flags
& 2;
1072 int is_friend
= flags
& 4;
1073 int specialization
= 0;
1074 int explicit_instantiation
= 0;
1075 int member_specialization
= 0;
1077 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1078 tree dname
= DECL_NAME (decl
);
1080 if (processing_specialization
)
1082 /* The last template header was of the form template <>. */
1084 if (template_header_count
> template_count
)
1086 /* There were more template headers than qualifying template
1088 if (template_header_count
- template_count
> 1)
1089 /* There shouldn't be that many template parameter
1090 lists. There can be at most one parameter list for
1091 every qualifying class, plus one for the function
1093 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1095 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1097 member_specialization
= 1;
1101 else if (template_header_count
== template_count
)
1103 /* The counts are equal. So, this might be a
1104 specialization, but it is not a specialization of a
1105 member template. It might be something like
1107 template <class T> struct S {
1111 void S<int>::f(int i) {} */
1113 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1117 /* This cannot be an explicit specialization. There are not
1118 enough headers for all of the qualifying classes. For
1119 example, we might have:
1122 void S<int>::T<char>::f();
1124 But, we're missing another template <>. */
1125 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1129 else if (processing_explicit_instantiation
)
1131 if (template_header_count
)
1132 cp_error ("template parameter list used in explicit instantiation");
1135 cp_error ("definition provided for explicit instantiation");
1137 explicit_instantiation
= 1;
1139 else if (ctype
!= NULL_TREE
1140 && !TYPE_BEING_DEFINED (ctype
)
1141 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1144 /* This case catches outdated code that looks like this:
1146 template <class T> struct S { void f(); };
1147 void S<int>::f() {} // Missing template <>
1149 We disable this check when the type is being defined to
1150 avoid complaining about default compiler-generated
1151 constructors, destructors, and assignment operators.
1152 Since the type is an instantiation, not a specialization,
1153 these are the only functions that can be defined before
1154 the class is complete. */
1157 template <class T> void S<int>::f() {}
1159 if (template_header_count
)
1161 cp_error ("template parameters specified in specialization");
1167 ("explicit specialization not preceded by `template <>'");
1169 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1171 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1174 /* This could be something like:
1176 template <class T> void f(T);
1177 class S { friend void f<>(int); } */
1181 /* This case handles bogus declarations like template <>
1182 template <class T> void f<int>(); */
1184 cp_error ("template-id `%D' in declaration of primary template",
1190 if (specialization
|| member_specialization
)
1192 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1193 for (; t
; t
= TREE_CHAIN (t
))
1194 if (TREE_PURPOSE (t
))
1197 ("default argument specified in explicit specialization");
1202 if (specialization
|| member_specialization
|| explicit_instantiation
)
1205 tree tmpl
= NULL_TREE
;
1206 tree targs
= NULL_TREE
;
1208 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1209 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1213 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1216 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1221 lookup_template_function (fns
, NULL_TREE
);
1224 if (declarator
== error_mark_node
)
1225 return error_mark_node
;
1227 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1229 /* A friend declaration. We can't do much, because we don't
1230 know what this resolves to, yet. */
1231 my_friendly_assert (is_friend
!= 0, 0);
1232 my_friendly_assert (!explicit_instantiation
, 0);
1233 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1237 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1239 if (!explicit_instantiation
)
1240 /* A specialization in class scope. This is illegal,
1241 but the error will already have been flagged by
1242 check_specialization_scope. */
1243 return error_mark_node
;
1246 /* It's not legal to write an explicit instantiation in
1249 class C { template void f(); }
1251 This case is caught by the parser. However, on
1254 template class C { void f(); };
1256 (which is illegal) we can get here. The error will be
1263 else if (ctype
!= NULL_TREE
1264 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1267 /* Find the list of functions in ctype that have the same
1268 name as the declared function. */
1269 tree name
= TREE_OPERAND (declarator
, 0);
1272 if (name
== constructor_name (ctype
)
1273 || name
== constructor_name_full (ctype
))
1275 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1277 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1278 : !TYPE_HAS_DESTRUCTOR (ctype
))
1280 /* From [temp.expl.spec]:
1282 If such an explicit specialization for the member
1283 of a class template names an implicitly-declared
1284 special member function (clause _special_), the
1285 program is ill-formed.
1287 Similar language is found in [temp.explicit]. */
1288 cp_error ("specialization of implicitly-declared special member function");
1293 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1296 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
1298 if (fns
== NULL_TREE
)
1300 cp_error ("no member function `%s' declared in `%T'",
1301 IDENTIFIER_POINTER (name
),
1306 TREE_OPERAND (declarator
, 0) = fns
;
1309 /* Figure out what exactly is being specialized at this point.
1310 Note that for an explicit instantiation, even one for a
1311 member function, we cannot tell apriori whether the
1312 instantiation is for a member template, or just a member
1313 function of a template class. Even if a member template is
1314 being instantiated, the member template arguments may be
1315 elided if they can be deduced from the rest of the
1317 tmpl
= determine_specialization (declarator
, decl
,
1319 member_specialization
,
1322 if (tmpl
&& tmpl
!= error_mark_node
)
1324 gen_tmpl
= most_general_template (tmpl
);
1326 if (explicit_instantiation
)
1328 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1329 is done by do_decl_instantiation later. */
1330 decl
= instantiate_template (tmpl
, innermost_args (targs
));
1334 /* If we though that the DECL was a member function, but it
1335 turns out to be specializing a static member function,
1336 make DECL a static member function as well. */
1337 if (DECL_STATIC_FUNCTION_P (tmpl
)
1338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1340 revert_static_member_fn (&decl
, 0, 0);
1341 last_function_parms
= TREE_CHAIN (last_function_parms
);
1344 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1345 DECL_TEMPLATE_INFO (decl
)
1346 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1348 /* Mangle the function name appropriately. Note that we do
1349 not mangle specializations of non-template member
1350 functions of template classes, e.g. with
1352 template <class T> struct S { void f(); }
1354 and given the specialization
1356 template <> void S<int>::f() {}
1358 we do not mangle S<int>::f() here. That's because it's
1359 just an ordinary member function and doesn't need special
1360 treatment. We do this here so that the ordinary,
1361 non-template, name-mangling algorith will not be used
1363 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1364 && name_mangling_version
>= 1)
1365 set_mangled_name_for_template_decl (decl
);
1367 if (is_friend
&& !have_def
)
1368 /* This is not really a declaration of a specialization.
1369 It's just the name of an instantiation. But, it's not
1370 a request for an instantiation, either. */
1371 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1373 /* Register this specialization so that we can find it
1375 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1384 /* TYPE is being declared. Verify that the use of template headers
1385 and such is reasonable. Issue error messages if not. */
1388 maybe_check_template_type (type
)
1391 if (template_header_count
)
1393 /* We are in the scope of some `template <...>' header. */
1396 = template_class_depth_real (TYPE_CONTEXT (type
),
1397 /*count_specializations=*/1);
1399 if (template_header_count
<= context_depth
)
1400 /* This is OK; the template headers are for the context. We
1401 are actually too lenient here; like
1402 check_explicit_specialization we should consider the number
1403 of template types included in the actual declaration. For
1406 template <class T> struct S {
1407 template <class U> template <class V>
1413 template <class T> struct S {
1414 template <class U> struct I;
1417 template <class T> template <class U.
1422 else if (template_header_count
> context_depth
+ 1)
1423 /* There are two many template parameter lists. */
1424 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1428 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1429 parameters. These are represented in the same format used for
1430 DECL_TEMPLATE_PARMS. */
1432 int comp_template_parms (parms1
, parms2
)
1439 if (parms1
== parms2
)
1442 for (p1
= parms1
, p2
= parms2
;
1443 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1444 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1446 tree t1
= TREE_VALUE (p1
);
1447 tree t2
= TREE_VALUE (p2
);
1450 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1451 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1453 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1456 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1458 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1459 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1461 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1464 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1466 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1471 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1472 /* One set of parameters has more parameters lists than the
1480 /* Returns 1 iff old_id is a template parameter. OLD_DECL is the decl
1481 from IDENTIFIER_LOCAL_VALUE (new identifier). */
1483 int decl_template_parm_p (old_decl
)
1486 /* For template template parms. */
1487 if (TREE_CODE (old_decl
) == TEMPLATE_DECL
1488 && TREE_TYPE (old_decl
)
1489 && TREE_CODE (TREE_TYPE (old_decl
)) == TEMPLATE_TEMPLATE_PARM
)
1492 /* For template type parms. */
1493 if (TREE_CODE (old_decl
) == TYPE_DECL
1494 && TREE_TYPE (old_decl
)
1495 && TREE_CODE (TREE_TYPE (old_decl
)) == TEMPLATE_TYPE_PARM
)
1498 /* For template non-type parms. */
1499 if (TREE_CODE (old_decl
) == CONST_DECL
1500 && DECL_INITIAL (old_decl
)
1501 && TREE_CODE (DECL_INITIAL (old_decl
)) == TEMPLATE_PARM_INDEX
)
1508 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1509 ORIG_LEVEL, DECL, and TYPE. */
1512 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1519 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1520 TEMPLATE_PARM_IDX (t
) = index
;
1521 TEMPLATE_PARM_LEVEL (t
) = level
;
1522 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1523 TEMPLATE_PARM_DECL (t
) = decl
;
1524 TREE_TYPE (t
) = type
;
1529 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1530 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1531 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1532 new one is created. */
1535 reduce_template_parm_level (index
, type
, levels
)
1540 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1541 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1542 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1545 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1546 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1549 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1550 TEMPLATE_PARM_LEVEL (index
) - levels
,
1551 TEMPLATE_PARM_ORIG_LEVEL (index
),
1553 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1555 /* Template template parameters need this. */
1556 DECL_TEMPLATE_PARMS (decl
)
1557 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1560 return TEMPLATE_PARM_DESCENDANTS (index
);
1563 /* Process information from new template parameter NEXT and append it to the
1564 LIST being built. */
1567 process_template_parm (list
, next
)
1576 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1577 defval
= TREE_PURPOSE (parm
);
1578 parm
= TREE_VALUE (parm
);
1579 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1583 tree p
= TREE_VALUE (tree_last (list
));
1585 if (TREE_CODE (p
) == TYPE_DECL
)
1586 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1587 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1588 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1590 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1598 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1599 /* is a const-param */
1600 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1601 PARM
, 0, NULL_TREE
);
1602 /* A template parameter is not modifiable. */
1603 TREE_READONLY (parm
) = 1;
1604 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1605 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1606 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1608 cp_error ("`%#T' is not a valid type for a template constant parameter",
1610 if (DECL_NAME (parm
) == NULL_TREE
)
1611 error (" a template type parameter must begin with `class' or `typename'");
1612 TREE_TYPE (parm
) = void_type_node
;
1615 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1616 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1617 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1619 if (TREE_PERMANENT (parm
) == 0)
1621 parm
= copy_node (parm
);
1622 TREE_PERMANENT (parm
) = 1;
1624 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1625 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1626 = build_template_parm_index (idx
, processing_template_decl
,
1627 processing_template_decl
,
1628 decl
, TREE_TYPE (parm
));
1633 parm
= TREE_VALUE (parm
);
1635 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1637 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1638 /* This is for distinguishing between real templates and template
1639 template parameters */
1640 TREE_TYPE (parm
) = t
;
1641 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1646 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1647 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1648 decl
= build_decl (TYPE_DECL
, parm
, t
);
1651 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1652 TYPE_NAME (t
) = decl
;
1653 TYPE_STUB_DECL (t
) = decl
;
1655 TEMPLATE_TYPE_PARM_INDEX (t
)
1656 = build_template_parm_index (idx
, processing_template_decl
,
1657 processing_template_decl
,
1658 decl
, TREE_TYPE (parm
));
1660 SET_DECL_ARTIFICIAL (decl
);
1662 parm
= build_tree_list (defval
, parm
);
1663 return chainon (list
, parm
);
1666 /* The end of a template parameter list has been reached. Process the
1667 tree list into a parameter vector, converting each parameter into a more
1668 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1672 end_template_parm_list (parms
)
1677 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1679 current_template_parms
1680 = tree_cons (build_int_2 (0, processing_template_decl
),
1681 saved_parmlist
, current_template_parms
);
1683 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1684 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1686 --processing_template_parmlist
;
1688 return saved_parmlist
;
1691 /* end_template_decl is called after a template declaration is seen. */
1694 end_template_decl ()
1696 reset_specialization ();
1698 if (! processing_template_decl
)
1701 /* This matches the pushlevel in begin_template_parm_list. */
1704 --processing_template_decl
;
1705 current_template_parms
= TREE_CHAIN (current_template_parms
);
1706 (void) get_pending_sizes (); /* Why? */
1709 /* Given a template argument vector containing the template PARMS.
1710 The innermost PARMS are given first. */
1713 current_template_args ()
1716 tree args
= NULL_TREE
;
1717 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1720 /* If there is only one level of template parameters, we do not
1721 create a TREE_VEC of TREE_VECs. Instead, we return a single
1722 TREE_VEC containing the arguments. */
1724 args
= make_tree_vec (length
);
1726 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1728 tree a
= copy_node (TREE_VALUE (header
));
1731 TREE_TYPE (a
) = NULL_TREE
;
1732 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1734 tree t
= TREE_VEC_ELT (a
, i
);
1736 /* T will be a list if we are called from within a
1737 begin/end_template_parm_list pair, but a vector directly
1738 if within a begin/end_member_template_processing pair. */
1739 if (TREE_CODE (t
) == TREE_LIST
)
1743 if (TREE_CODE (t
) == TYPE_DECL
1744 || TREE_CODE (t
) == TEMPLATE_DECL
)
1747 t
= DECL_INITIAL (t
);
1748 TREE_VEC_ELT (a
, i
) = t
;
1753 TREE_VEC_ELT (args
, --l
) = a
;
1761 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1762 template PARMS. Used by push_template_decl below. */
1765 build_template_decl (decl
, parms
)
1769 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1770 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1771 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1772 if (DECL_LANG_SPECIFIC (decl
))
1774 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1775 DECL_STATIC_FUNCTION_P (tmpl
) =
1776 DECL_STATIC_FUNCTION_P (decl
);
1782 struct template_parm_data
1784 /* The level of the template parameters we are currently
1788 /* The index of the specialization argument we are currently
1792 /* An array whose size is the number of template parameters. The
1793 elements are non-zero if the parameter has been used in any one
1794 of the arguments processed so far. */
1797 /* An array whose size is the number of template arguments. The
1798 elements are non-zero if the argument makes use of template
1799 parameters of this level. */
1800 int* arg_uses_template_parms
;
1803 /* Subroutine of push_template_decl used to see if each template
1804 parameter in a partial specialization is used in the explicit
1805 argument list. If T is of the LEVEL given in DATA (which is
1806 treated as a template_parm_data*), then DATA->PARMS is marked
1810 mark_template_parm (t
, data
)
1816 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1818 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1820 level
= TEMPLATE_PARM_LEVEL (t
);
1821 idx
= TEMPLATE_PARM_IDX (t
);
1825 level
= TEMPLATE_TYPE_LEVEL (t
);
1826 idx
= TEMPLATE_TYPE_IDX (t
);
1829 if (level
== tpd
->level
)
1831 tpd
->parms
[idx
] = 1;
1832 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
1835 /* Return zero so that for_each_template_parm will continue the
1836 traversal of the tree; we want to mark *every* template parm. */
1840 /* Process the partial specialization DECL. */
1843 process_partial_specialization (decl
)
1846 tree type
= TREE_TYPE (decl
);
1847 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1848 tree specargs
= CLASSTYPE_TI_ARGS (type
);
1849 tree inner_args
= innermost_args (specargs
);
1850 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1851 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
1852 int nargs
= TREE_VEC_LENGTH (inner_args
);
1853 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
1855 int did_error_intro
= 0;
1856 int issued_default_arg_message
= 0;
1857 struct template_parm_data tpd
;
1858 struct template_parm_data tpd2
;
1860 /* [temp.class.spec]
1862 The template parameter list of a specialization shall not
1863 contain default template argument values. */
1864 for (i
= 0; i
< ntparms
; ++i
)
1866 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
1868 if (!issued_default_arg_message
)
1870 cp_error ("default argument in partial specialization `%T'",
1872 issued_default_arg_message
= 1;
1874 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
1878 /* We check that each of the template parameters given in the
1879 partial specialization is used in the argument list to the
1880 specialization. For example:
1882 template <class T> struct S;
1883 template <class T> struct S<T*>;
1885 The second declaration is OK because `T*' uses the template
1886 parameter T, whereas
1888 template <class T> struct S<int>;
1890 is no good. Even trickier is:
1901 The S2<T> declaration is actually illegal; it is a
1902 full-specialization. Of course,
1905 struct S2<T (*)(U)>;
1907 or some such would have been OK. */
1908 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
1909 tpd
.parms
= alloca (sizeof (int) * ntparms
);
1910 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
1912 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
1913 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
1914 for (i
= 0; i
< nargs
; ++i
)
1916 tpd
.current_arg
= i
;
1917 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
1918 &mark_template_parm
,
1921 for (i
= 0; i
< ntparms
; ++i
)
1922 if (tpd
.parms
[i
] == 0)
1924 /* One of the template parms was not used in the
1926 if (!did_error_intro
)
1928 cp_error ("template parameters not used in partial specialization:");
1929 did_error_intro
= 1;
1933 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
1936 /* [temp.class.spec]
1938 The argument list of the specialization shall not be identical to
1939 the implicit argument list of the primary template. */
1940 if (comp_template_args (inner_args
,
1941 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
1943 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
1945 /* [temp.class.spec]
1947 A partially specialized non-type argument expression shall not
1948 involve template parameters of the partial specialization except
1949 when the argument expression is a simple identifier.
1951 The type of a template parameter corresponding to a specialized
1952 non-type argument shall not be dependent on a parameter of the
1954 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
1956 for (i
= 0; i
< nargs
; ++i
)
1958 tree arg
= TREE_VEC_ELT (inner_args
, i
);
1959 if (/* These first two lines are the `non-type' bit. */
1960 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
1961 && TREE_CODE (arg
) != TEMPLATE_DECL
1962 /* This next line is the `argument expression is not just a
1963 simple identifier' condition and also the `specialized
1964 non-type argument' bit. */
1965 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
1967 if (tpd
.arg_uses_template_parms
[i
])
1968 cp_error ("template argument `%E' involves template parameter(s)", arg
);
1971 /* Look at the corresponding template parameter,
1972 marking which template parameters its type depends
1975 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
1980 /* We haven't yet initialized TPD2. Do so now. */
1981 tpd2
.arg_uses_template_parms
1982 = (int*) alloca (sizeof (int) * nargs
);
1983 /* The number of paramters here is the number in the
1984 main template, which, as checked in the assertion
1986 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
1988 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
1991 /* Mark the template paramters. But this time, we're
1992 looking for the template parameters of the main
1993 template, not in the specialization. */
1994 tpd2
.current_arg
= i
;
1995 tpd2
.arg_uses_template_parms
[i
] = 0;
1996 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
1997 for_each_template_parm (type
,
1998 &mark_template_parm
,
2001 if (tpd2
.arg_uses_template_parms
[i
])
2003 /* The type depended on some template parameters.
2004 If they are fully specialized in the
2005 specialization, that's OK. */
2007 for (j
= 0; j
< nargs
; ++j
)
2008 if (tpd2
.parms
[j
] != 0
2009 && tpd
.arg_uses_template_parms
[j
])
2011 cp_error ("type `%T' of template argument `%E' depends on template paramter(s)",
2021 if (retrieve_specialization (maintmpl
, specargs
))
2022 /* We've already got this specialization. */
2025 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2026 = perm_tree_cons (inner_args
, inner_parms
,
2027 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2028 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2032 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2033 parameters given by current_template_args, or reuses a
2034 previously existing one, if appropriate. Returns the DECL, or an
2035 equivalent one, if it is replaced via a call to duplicate_decls.
2037 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2040 push_template_decl_real (decl
, is_friend
)
2050 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2053 /* For a friend, we want the context of the friend function, not
2054 the type of which it is a friend. */
2055 ctx
= DECL_CONTEXT (decl
);
2056 else if (DECL_REAL_CONTEXT (decl
)
2057 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2058 /* In the case of a virtual function, we want the class in which
2060 ctx
= DECL_REAL_CONTEXT (decl
);
2062 /* Otherwise, if we're currently definining some class, the DECL
2063 is assumed to be a member of the class. */
2064 ctx
= current_class_type
;
2066 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2069 if (!DECL_CONTEXT (decl
))
2070 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2072 /* For determining whether this is a primary template or not, we're really
2073 interested in the lexical context, not the true context. */
2075 info
= current_class_type
;
2079 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
2081 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2082 this next line works even when we are at global scope. */
2083 else if (processing_template_decl
> template_class_depth (info
))
2090 if (current_lang_name
== lang_name_c
)
2091 cp_error ("template with C linkage");
2092 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
2093 cp_error ("template class without a name");
2094 if (TREE_CODE (decl
) == TYPE_DECL
2095 && TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2096 cp_error ("template declaration of `%#T'", TREE_TYPE (decl
));
2099 /* Partial specialization. */
2100 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
2101 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2102 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
2103 return process_partial_specialization (decl
);
2105 /* [temp.param] A default template-argument shall not be specified in a
2106 function template declaration or a function template definition, nor
2107 in the template-parameter-list of the definition of a member of a
2111 int issued_default_arg_message
= 0;
2113 parms
= current_template_parms
;
2115 parms
= TREE_CHAIN (parms
);
2116 for (; parms
; parms
= TREE_CHAIN (parms
))
2118 tree inner_parms
= TREE_VALUE (parms
);
2121 if (TREE_TYPE (inner_parms
))
2124 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2125 for (i
= 0; i
< ntparms
; ++i
)
2127 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2129 if (!issued_default_arg_message
)
2131 cp_error ("default argument for template parameter of class enclosing `%D'",
2133 issued_default_arg_message
= 1;
2135 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2141 args
= current_template_args ();
2144 || TREE_CODE (ctx
) == FUNCTION_DECL
2145 || TYPE_BEING_DEFINED (ctx
)
2146 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2148 if (DECL_LANG_SPECIFIC (decl
)
2149 && DECL_TEMPLATE_INFO (decl
)
2150 && DECL_TI_TEMPLATE (decl
))
2151 tmpl
= DECL_TI_TEMPLATE (decl
);
2154 tmpl
= build_template_decl (decl
, current_template_parms
);
2156 if (DECL_LANG_SPECIFIC (decl
)
2157 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2159 /* A specialization of a member template of a template
2161 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2162 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2163 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2169 tree a
, t
, current
, parms
;
2172 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2173 cp_error ("must specialize `%#T' before defining member `%#D'",
2175 if (TREE_CODE (decl
) == TYPE_DECL
)
2177 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2178 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2179 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2180 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2181 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2184 cp_error ("`%D' does not declare a template type", decl
);
2188 else if (! DECL_TEMPLATE_INFO (decl
))
2190 cp_error ("template definition of non-template `%#D'", decl
);
2194 tmpl
= DECL_TI_TEMPLATE (decl
);
2196 if (is_member_template (tmpl
)
2197 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2198 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2199 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2203 /* The declaration is a specialization of a member
2204 template, declared outside the class. Therefore, the
2205 innermost template arguments will be NULL, so we
2206 replace them with the arguments determined by the
2207 earlier call to check_explicit_specialization. */
2208 args
= DECL_TI_ARGS (decl
);
2211 = build_template_decl (decl
, current_template_parms
);
2212 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2213 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2214 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2215 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2216 DECL_TEMPLATE_INFO (new_tmpl
) =
2217 perm_tree_cons (tmpl
, args
, NULL_TREE
);
2219 register_specialization (new_tmpl
, tmpl
, args
);
2223 /* Make sure the template headers we got make sense. */
2225 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2226 i
= TMPL_PARMS_DEPTH (parms
);
2227 if (TMPL_ARGS_DEPTH (args
) != i
)
2229 cp_error ("expected %d levels of template parms for `%#D', got %d",
2230 i
, decl
, TMPL_ARGS_DEPTH (args
));
2233 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2235 a
= TMPL_ARGS_LEVEL (args
, i
);
2236 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2238 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2240 if (current
== decl
)
2241 cp_error ("got %d template parameters for `%#D'",
2242 TREE_VEC_LENGTH (a
), decl
);
2244 cp_error ("got %d template parameters for `%#T'",
2245 TREE_VEC_LENGTH (a
), current
);
2246 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2249 /* Perhaps we should also check that the parms are used in the
2250 appropriate qualifying scopes in the declarator? */
2252 if (current
== decl
)
2255 current
= TYPE_CONTEXT (current
);
2259 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2260 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2262 /* Push template declarations for global functions and types. Note
2263 that we do not try to push a global template friend declared in a
2264 template class; such a thing may well depend on the template
2265 parameters of the class. */
2267 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2268 tmpl
= pushdecl_namespace_level (tmpl
);
2271 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2273 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
2275 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2277 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2278 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2279 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2280 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2282 else if (! DECL_LANG_SPECIFIC (decl
))
2283 cp_error ("template declaration of `%#D'", decl
);
2285 DECL_TEMPLATE_INFO (decl
) = info
;
2287 return DECL_TEMPLATE_RESULT (tmpl
);
2291 push_template_decl (decl
)
2294 return push_template_decl_real (decl
, 0);
2297 /* Called when a class template TYPE is redeclared with the indicated
2298 template PARMS, e.g.:
2300 template <class T> struct S;
2301 template <class T> struct S {}; */
2304 redeclare_class_template (type
, parms
)
2308 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2312 if (!PRIMARY_TEMPLATE_P (tmpl
))
2313 /* The type is nested in some template class. Nothing to worry
2314 about here; there are no new template parameters for the nested
2318 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2319 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2321 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2323 cp_error_at ("previous declaration `%D'", tmpl
);
2324 cp_error ("used %d template parameter%s instead of %d",
2325 TREE_VEC_LENGTH (tmpl_parms
),
2326 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2327 TREE_VEC_LENGTH (parms
));
2331 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2333 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2334 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2335 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2336 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2338 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2340 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2341 cp_error ("redeclared here as `%#D'", parm
);
2345 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2347 /* We have in [temp.param]:
2349 A template-parameter may not be given default arguments
2350 by two different declarations in the same scope. */
2351 cp_error ("redefinition of default argument for `%#D'", parm
);
2352 cp_error_at (" original definition appeared here", tmpl_parm
);
2356 if (parm_default
!= NULL_TREE
)
2357 /* Update the previous template parameters (which are the ones
2358 that will really count) with the new default value. */
2359 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2363 /* Attempt to convert the non-type template parameter EXPR to the
2364 indicated TYPE. If the conversion is successful, return the
2365 converted value. If the conversion is unsuccesful, return
2366 NULL_TREE if we issued an error message, or error_mark_node if we
2367 did not. We issue error messages for out-and-out bad template
2368 parameters, but not simply because the conversion failed, since we
2369 might be just trying to do argument deduction. By the time this
2370 function is called, neither TYPE nor EXPR may make use of template
2374 convert_nontype_argument (type
, expr
)
2378 tree expr_type
= TREE_TYPE (expr
);
2380 /* A template-argument for a non-type, non-template
2381 template-parameter shall be one of:
2383 --an integral constant-expression of integral or enumeration
2386 --the name of a non-type template-parameter; or
2388 --the name of an object or function with external linkage,
2389 including function templates and function template-ids but
2390 excluding non-static class members, expressed as id-expression;
2393 --the address of an object or function with external linkage,
2394 including function templates and function template-ids but
2395 excluding non-static class members, expressed as & id-expression
2396 where the & is optional if the name refers to a function or
2399 --a pointer to member expressed as described in _expr.unary.op_. */
2401 /* An integral constant-expression can include const variables
2403 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
2404 expr
= decl_constant_value (expr
);
2406 if (is_overloaded_fn (expr
))
2407 /* OK for now. We'll check that it has external linkage later.
2408 Check this first since if expr_type is the unknown_type_node
2409 we would otherwise complain below. */
2411 else if (INTEGRAL_TYPE_P (expr_type
)
2412 || TYPE_PTRMEM_P (expr_type
)
2413 || TYPE_PTRMEMFUNC_P (expr_type
)
2414 /* The next two are g++ extensions. */
2415 || TREE_CODE (expr_type
) == REAL_TYPE
2416 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2418 if (! TREE_CONSTANT (expr
))
2421 cp_error ("non-constant `%E' cannot be used as template argument",
2426 else if (TYPE_PTR_P (expr_type
)
2427 /* If expr is the address of an overloaded function, we
2428 will get the unknown_type_node at this point. */
2429 || expr_type
== unknown_type_node
)
2435 if (TREE_CODE (e
) != ADDR_EXPR
)
2438 cp_error ("`%E' is not a valid template argument", expr
);
2439 error ("it must be %s%s with external linkage",
2440 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
2441 ? "a pointer to " : "",
2442 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
2443 ? "a function" : "an object");
2447 referent
= TREE_OPERAND (e
, 0);
2448 STRIP_NOPS (referent
);
2450 if (TREE_CODE (referent
) == STRING_CST
)
2452 cp_error ("string literal %E is not a valid template argument",
2454 error ("because it is the address of an object with static linkage");
2458 if (is_overloaded_fn (referent
))
2459 /* We'll check that it has external linkage later. */
2461 else if (TREE_CODE (referent
) != VAR_DECL
)
2463 else if (!TREE_PUBLIC (referent
))
2465 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2466 return error_mark_node
;
2469 else if (TREE_CODE (expr
) == VAR_DECL
)
2471 if (!TREE_PUBLIC (expr
))
2476 cp_error ("object `%E' cannot be used as template argument", expr
);
2480 switch (TREE_CODE (type
))
2485 /* For a non-type template-parameter of integral or enumeration
2486 type, integral promotions (_conv.prom_) and integral
2487 conversions (_conv.integral_) are applied. */
2488 if (!INTEGRAL_TYPE_P (expr_type
))
2489 return error_mark_node
;
2491 /* It's safe to call digest_init in this case; we know we're
2492 just converting one integral constant expression to another. */
2493 expr
= digest_init (type
, expr
, (tree
*) 0);
2495 if (TREE_CODE (expr
) != INTEGER_CST
)
2496 /* Curiously, some TREE_CONSTNAT integral expressions do not
2497 simplify to integer constants. For example, `3 % 0',
2498 remains a TRUNC_MOD_EXPR. */
2505 /* These are g++ extensions. */
2506 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2507 return error_mark_node
;
2509 expr
= digest_init (type
, expr
, (tree
*) 0);
2511 if (TREE_CODE (expr
) != REAL_CST
)
2518 tree type_pointed_to
= TREE_TYPE (type
);
2520 if (TYPE_PTRMEM_P (type
))
2521 /* For a non-type template-parameter of type pointer to data
2522 member, qualification conversions (_conv.qual_) are
2524 return perform_qualification_conversions (type
, expr
);
2525 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2527 /* For a non-type template-parameter of type pointer to
2528 function, only the function-to-pointer conversion
2529 (_conv.func_) is applied. If the template-argument
2530 represents a set of overloaded functions (or a pointer to
2531 such), the matching function is selected from the set
2536 if (TREE_CODE (expr
) == ADDR_EXPR
)
2537 fns
= TREE_OPERAND (expr
, 0);
2541 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2543 if (fn
== error_mark_node
)
2544 return error_mark_node
;
2546 if (!TREE_PUBLIC (fn
))
2548 if (really_overloaded_fn (fns
))
2549 return error_mark_node
;
2554 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2556 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2562 /* For a non-type template-parameter of type pointer to
2563 object, qualification conversions (_conv.qual_) and the
2564 array-to-pointer conversion (_conv.array_) are applied.
2565 [Note: In particular, neither the null pointer conversion
2566 (_conv.ptr_) nor the derived-to-base conversion
2567 (_conv.ptr_) are applied. Although 0 is a valid
2568 template-argument for a non-type template-parameter of
2569 integral type, it is not a valid template-argument for a
2570 non-type template-parameter of pointer type.]
2572 The call to decay_conversion performs the
2573 array-to-pointer conversion, if appropriate. */
2574 expr
= decay_conversion (expr
);
2576 if (expr
== error_mark_node
)
2577 return error_mark_node
;
2579 return perform_qualification_conversions (type
, expr
);
2584 case REFERENCE_TYPE
:
2586 tree type_referred_to
= TREE_TYPE (type
);
2588 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2590 /* For a non-type template-parameter of type reference to
2591 function, no conversions apply. If the
2592 template-argument represents a set of overloaded
2593 functions, the matching function is selected from the
2594 set (_over.over_). */
2598 fn
= instantiate_type (type_referred_to
, fns
, 0);
2600 if (fn
== error_mark_node
)
2601 return error_mark_node
;
2603 if (!TREE_PUBLIC (fn
))
2605 if (really_overloaded_fn (fns
))
2606 /* Don't issue an error here; we might get a different
2607 function if the overloading had worked out
2609 return error_mark_node
;
2614 my_friendly_assert (same_type_p (type_referred_to
,
2622 /* For a non-type template-parameter of type reference to
2623 object, no conversions apply. The type referred to by the
2624 reference may be more cv-qualified than the (otherwise
2625 identical) type of the template-argument. The
2626 template-parameter is bound directly to the
2627 template-argument, which must be an lvalue. */
2628 if (!same_type_p (TYPE_MAIN_VARIANT (expr_type
),
2629 TYPE_MAIN_VARIANT (type
))
2630 || !at_least_as_qualified_p (type_referred_to
,
2632 || !real_lvalue_p (expr
))
2633 return error_mark_node
;
2645 if (!TYPE_PTRMEMFUNC_P (type
))
2646 /* This handles templates like
2647 template<class T, T t> void f();
2648 when T is substituted with any class. The second template
2649 parameter becomes invalid and the template candidate is
2651 return error_mark_node
;
2653 /* For a non-type template-parameter of type pointer to member
2654 function, no conversions apply. If the template-argument
2655 represents a set of overloaded member functions, the
2656 matching member function is selected from the set
2659 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2660 expr_type
!= unknown_type_node
)
2661 return error_mark_node
;
2663 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2665 /* A ptr-to-member constant. */
2666 if (!same_type_p (type
, expr_type
))
2667 return error_mark_node
;
2672 if (TREE_CODE (expr
) != ADDR_EXPR
)
2673 return error_mark_node
;
2675 fns
= TREE_OPERAND (expr
, 0);
2677 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
2680 if (fn
== error_mark_node
)
2681 return error_mark_node
;
2683 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2685 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2692 /* All non-type parameters must have one of these types. */
2693 my_friendly_abort (0);
2697 return error_mark_node
;
2700 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2701 template template parameters. Both PARM_PARMS and ARG_PARMS are
2702 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2705 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2706 the case, then extra parameters must have default arguments.
2708 Consider the example:
2709 template <class T, class Allocator = allocator> class vector;
2710 template<template <class U> class TT> class C;
2712 C<vector> is a valid instantiation. PARM_PARMS for the above code
2713 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2714 T and Allocator) and OUTER_ARGS contains the argument that is used to
2715 substitute the TT parameter. */
2718 coerce_template_template_parms (parm_parms
, arg_parms
, in_decl
, outer_args
)
2719 tree parm_parms
, arg_parms
, in_decl
, outer_args
;
2721 int nparms
, nargs
, i
;
2724 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
2725 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
2727 nparms
= TREE_VEC_LENGTH (parm_parms
);
2728 nargs
= TREE_VEC_LENGTH (arg_parms
);
2730 /* The rule here is opposite of coerce_template_parms. */
2733 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
2736 for (i
= 0; i
< nparms
; ++i
)
2738 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
2739 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
2741 if (arg
== NULL_TREE
|| arg
== error_mark_node
2742 || parm
== NULL_TREE
|| parm
== error_mark_node
)
2745 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2748 switch (TREE_CODE (parm
))
2754 /* We encounter instantiations of templates like
2755 template <template <template <class> class> class TT>
2757 sorry ("nested template template parameter");
2761 /* The tsubst call is used to handle cases such as
2762 template <class T, template <T> class TT> class D;
2763 i.e. the parameter list of TT depends on earlier parameters. */
2764 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
, in_decl
),
2770 my_friendly_abort (0);
2776 /* Convert the indicated template ARG as necessary to match the
2777 indicated template PARM. Returns the converted ARG, or
2778 error_mark_node if the conversion was unsuccessful. Error messages
2779 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2780 parameter in the parameter list. ARGS is the full set of template
2781 arguments deduced so far. */
2784 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
2794 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2796 inner_args
= innermost_args (args
);
2798 if (TREE_CODE (arg
) == TREE_LIST
2799 && TREE_TYPE (arg
) != NULL_TREE
2800 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2802 /* The template argument was the name of some
2803 member function. That's usually
2804 illegal, but static members are OK. In any
2805 case, grab the underlying fields/functions
2806 and issue an error later if required. */
2807 arg
= TREE_VALUE (arg
);
2808 TREE_TYPE (arg
) = unknown_type_node
;
2811 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
2812 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
2813 || requires_tmpl_type
);
2815 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2816 we also accept implicitly created TYPE_DECL as a valid argument.
2817 This is necessary to handle the case where we pass a template name
2818 to a template template parameter in a scope where we've derived from
2819 in instantiation of that template, so the template name refers to that
2820 instantiation. We really ought to handle this better. */
2822 = ((TREE_CODE (arg
) == TEMPLATE_DECL
2823 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
2824 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
2825 && !CLASSTYPE_TEMPLATE_INFO (arg
))
2826 || (TREE_CODE (arg
) == RECORD_TYPE
2827 && CLASSTYPE_TEMPLATE_INFO (arg
)
2828 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
2829 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
2830 && requires_tmpl_type
2831 && is_base_of_enclosing_class (arg
, current_class_type
)));
2832 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
2833 arg
= TYPE_STUB_DECL (arg
);
2834 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
2835 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
2837 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
2839 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
2840 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
2842 cp_pedwarn ("to refer to a type member of a template parameter,");
2843 cp_pedwarn (" use `typename %E'", arg
);
2845 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
2846 TREE_OPERAND (arg
, 1));
2849 if (is_type
!= requires_type
)
2855 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2858 cp_error (" expected a constant of type `%T', got `%T'",
2860 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2862 cp_error (" expected a type, got `%E'", arg
);
2865 return error_mark_node
;
2867 if (is_tmpl_type
^ requires_tmpl_type
)
2869 if (in_decl
&& complain
)
2871 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2874 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2876 cp_error (" expected a class template, got `%T'", arg
);
2878 return error_mark_node
;
2883 if (requires_tmpl_type
)
2885 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2886 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2888 if (coerce_template_template_parms (parmparm
, argparm
,
2889 in_decl
, inner_args
))
2893 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2895 if (val
!= error_mark_node
2896 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2897 val
= TREE_TYPE (val
);
2901 if (in_decl
&& complain
)
2903 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2905 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
2908 val
= error_mark_node
;
2913 val
= groktypename (arg
);
2914 if (! processing_template_decl
)
2916 /* [basic.link]: A name with no linkage (notably, the
2917 name of a class or enumeration declared in a local
2918 scope) shall not be used to declare an entity with
2919 linkage. This implies that names with no linkage
2920 cannot be used as template arguments. */
2921 tree t
= no_linkage_check (val
);
2924 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
2926 ("template-argument `%T' uses anonymous type", val
);
2929 ("template-argument `%T' uses local type `%T'",
2931 return error_mark_node
;
2938 tree t
= tsubst (TREE_TYPE (parm
), args
, in_decl
);
2940 if (processing_template_decl
)
2941 arg
= maybe_fold_nontype_arg (arg
);
2943 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
2944 /* We used to call digest_init here. However, digest_init
2945 will report errors, which we don't want when complain
2946 is zero. More importantly, digest_init will try too
2947 hard to convert things: for example, `0' should not be
2948 converted to pointer type at this point according to
2949 the standard. Accepting this is not merely an
2950 extension, since deciding whether or not these
2951 conversions can occur is part of determining which
2952 function template to call, or whether a given epxlicit
2953 argument specification is legal. */
2954 val
= convert_nontype_argument (t
, arg
);
2958 if (val
== NULL_TREE
)
2959 val
= error_mark_node
;
2960 else if (val
== error_mark_node
&& complain
)
2961 cp_error ("could not convert template argument `%E' to `%T'",
2968 /* Convert all template arguments to their appropriate types, and
2969 return a vector containing the innermost resulting template
2970 arguments. If any error occurs, return error_mark_node, and, if
2971 COMPLAIN is non-zero, issue an error message. Some error messages
2972 are issued even if COMPLAIN is zero; for instance, if a template
2973 argument is composed from a local class.
2975 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2976 provided in ARGLIST, or else trailing parameters must have default
2977 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2978 deduction for any unspecified trailing arguments.
2980 The resulting TREE_VEC is allocated on a temporary obstack, and
2981 must be explicitly copied if it will be permanent. */
2984 coerce_template_parms (parms
, args
, in_decl
,
2986 require_all_arguments
)
2990 int require_all_arguments
;
2992 int nparms
, nargs
, i
, lost
= 0;
2995 tree new_inner_args
;
2997 inner_args
= innermost_args (args
);
2998 nargs
= NUM_TMPL_ARGS (inner_args
);
2999 nparms
= TREE_VEC_LENGTH (parms
);
3003 && require_all_arguments
3004 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3008 cp_error ("wrong number of template arguments (%d, should be %d)",
3012 cp_error_at ("provided for `%D'", in_decl
);
3015 return error_mark_node
;
3018 new_inner_args
= make_temp_vec (nparms
);
3019 new_args
= add_outermost_template_args (args
, new_inner_args
);
3020 for (i
= 0; i
< nparms
; i
++)
3025 /* Get the Ith template parameter. */
3026 parm
= TREE_VEC_ELT (parms
, i
);
3028 /* Calculate the Ith argument. */
3029 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3031 arg
= TREE_VALUE (inner_args
);
3032 inner_args
= TREE_CHAIN (inner_args
);
3035 arg
= TREE_VEC_ELT (inner_args
, i
);
3036 /* If no template argument was supplied, look for a default
3038 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3040 /* There was no default value. */
3041 my_friendly_assert (!require_all_arguments
, 0);
3044 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3045 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, in_decl
);
3047 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, in_decl
);
3049 /* Now, convert the Ith argument, as necessary. */
3050 if (arg
== NULL_TREE
)
3051 /* We're out of arguments. */
3053 my_friendly_assert (!require_all_arguments
, 0);
3056 else if (arg
== error_mark_node
)
3058 cp_error ("template argument %d is invalid", i
+ 1);
3059 arg
= error_mark_node
;
3062 arg
= convert_template_argument (TREE_VALUE (parm
),
3063 arg
, new_args
, complain
, i
,
3066 if (arg
== error_mark_node
)
3068 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3072 return error_mark_node
;
3074 return new_inner_args
;
3077 /* Returns 1 if template args OT and NT are equivalent. */
3080 template_args_equal (ot
, nt
)
3085 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3087 if (TREE_CODE (nt
) == TREE_VEC
)
3088 /* For member templates */
3089 return comp_template_args (ot
, nt
);
3090 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3091 return same_type_p (ot
, nt
);
3093 return (cp_tree_equal (ot
, nt
) > 0);
3096 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3097 of template arguments. Returns 0 otherwise. */
3100 comp_template_args (oldargs
, newargs
)
3101 tree oldargs
, newargs
;
3105 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3108 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3110 tree nt
= TREE_VEC_ELT (newargs
, i
);
3111 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3113 if (! template_args_equal (ot
, nt
))
3119 /* Given class template name and parameter list, produce a user-friendly name
3120 for the instantiation. */
3123 mangle_class_name_for_template (name
, parms
, arglist
)
3125 tree parms
, arglist
;
3127 static struct obstack scratch_obstack
;
3128 static char *scratch_firstobj
;
3131 if (!scratch_firstobj
)
3132 gcc_obstack_init (&scratch_obstack
);
3134 obstack_free (&scratch_obstack
, scratch_firstobj
);
3135 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3137 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3138 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3142 nparms
= TREE_VEC_LENGTH (parms
);
3143 arglist
= innermost_args (arglist
);
3144 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3145 for (i
= 0; i
< nparms
; i
++)
3147 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3148 tree arg
= TREE_VEC_ELT (arglist
, i
);
3153 if (TREE_CODE (parm
) == TYPE_DECL
)
3155 cat (type_as_string_real (arg
, 0, 1));
3158 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3160 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3162 /* Already substituted with real template. Just output
3163 the template name here */
3164 tree context
= DECL_CONTEXT (arg
);
3167 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
, 980422);
3168 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3171 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3174 /* Output the parameter declaration */
3175 cat (type_as_string_real (arg
, 0, 1));
3179 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3181 if (TREE_CODE (arg
) == TREE_LIST
)
3183 /* New list cell was built because old chain link was in
3185 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3186 arg
= TREE_VALUE (arg
);
3188 /* No need to check arglist against parmlist here; we did that
3189 in coerce_template_parms, called from lookup_template_class. */
3190 cat (expr_as_string (arg
, 0));
3193 char *bufp
= obstack_next_free (&scratch_obstack
);
3195 while (bufp
[offset
- 1] == ' ')
3197 obstack_blank_fast (&scratch_obstack
, offset
);
3199 /* B<C<char> >, not B<C<char>> */
3200 if (bufp
[offset
- 1] == '>')
3205 return (char *) obstack_base (&scratch_obstack
);
3209 classtype_mangled_name (t
)
3212 if (CLASSTYPE_TEMPLATE_INFO (t
)
3213 /* Specializations have already had their names set up in
3214 lookup_template_class. */
3215 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3217 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3219 /* For non-primary templates, the template parameters are
3220 implicit from their surrounding context. */
3221 if (PRIMARY_TEMPLATE_P (tmpl
))
3223 tree name
= DECL_NAME (tmpl
);
3224 char *mangled_name
= mangle_class_name_for_template
3225 (IDENTIFIER_POINTER (name
),
3226 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3227 CLASSTYPE_TI_ARGS (t
));
3228 tree id
= get_identifier (mangled_name
);
3229 IDENTIFIER_TEMPLATE (id
) = name
;
3234 return TYPE_IDENTIFIER (t
);
3238 add_pending_template (d
)
3243 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3244 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3246 ti
= DECL_TEMPLATE_INFO (d
);
3248 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3251 *template_tail
= perm_tree_cons
3252 (build_srcloc_here (), d
, NULL_TREE
);
3253 template_tail
= &TREE_CHAIN (*template_tail
);
3254 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3258 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3259 may be either a _DECL or an overloaded function or an
3260 IDENTIFIER_NODE), and ARGLIST. */
3263 lookup_template_function (fns
, arglist
)
3268 if (fns
== NULL_TREE
)
3270 cp_error ("non-template used as template");
3271 return error_mark_node
;
3274 type
= TREE_TYPE (fns
);
3275 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3276 type
= unknown_type_node
;
3278 if (processing_template_decl
)
3279 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3281 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3284 /* Within the scope of a template class S<T>, the name S gets bound
3285 (in build_self_reference) to a TYPE_DECL for the class, not a
3286 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3287 or one of its enclosing classes, and that type is a template,
3288 return the associated TEMPLATE_DECL. Otherwise, the original
3289 DECL is returned. */
3292 maybe_get_template_decl_from_type_decl (decl
)
3295 return (decl
!= NULL_TREE
3296 && TREE_CODE (decl
) == TYPE_DECL
3297 && DECL_ARTIFICIAL (decl
)
3298 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3299 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3302 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3303 parameters, find the desired type.
3305 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3306 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3307 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3308 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3309 copy it here to keep it from being reclaimed when the decl storage
3312 IN_DECL, if non-NULL, is the template declaration we are trying to
3315 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3316 the class we are looking up.
3318 If the template class is really a local class in a template
3319 function, then the FUNCTION_CONTEXT is the function in which it is
3320 being instantiated. */
3323 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3329 tree
template = NULL_TREE
, parmlist
;
3332 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3334 if (IDENTIFIER_LOCAL_VALUE (d1
)
3335 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
3336 template = IDENTIFIER_LOCAL_VALUE (d1
);
3340 push_decl_namespace (context
);
3341 if (current_class_type
!= NULL_TREE
)
3343 maybe_get_template_decl_from_type_decl
3344 (IDENTIFIER_CLASS_VALUE (d1
));
3345 if (template == NULL_TREE
)
3346 template = lookup_name_nonclass (d1
);
3348 pop_decl_namespace ();
3351 context
= DECL_CONTEXT (template);
3353 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3355 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
3356 return error_mark_node
;
3357 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
3358 d1
= DECL_NAME (template);
3360 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3361 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3362 && IS_AGGR_TYPE (d1
)))
3364 template = TYPE_TI_TEMPLATE (d1
);
3365 d1
= DECL_NAME (template);
3367 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3368 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3371 d1
= DECL_NAME (template);
3372 context
= DECL_CONTEXT (template);
3375 my_friendly_abort (272);
3377 /* With something like `template <class T> class X class X { ... };'
3378 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
3379 We don't want to do that, but we have to deal with the situation, so
3380 let's give them some syntax errors to chew on instead of a crash. */
3382 return error_mark_node
;
3384 if (context
== NULL_TREE
)
3385 context
= global_namespace
;
3387 if (TREE_CODE (template) != TEMPLATE_DECL
)
3389 cp_error ("non-template type `%T' used as a template", d1
);
3391 cp_error_at ("for template declaration `%D'", in_decl
);
3392 return error_mark_node
;
3395 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3397 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3398 template arguments */
3400 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3401 tree template2
= TYPE_STUB_DECL (parm
);
3404 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
3405 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3407 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3408 if (arglist2
== error_mark_node
)
3409 return error_mark_node
;
3411 arglist2
= copy_to_permanent (arglist2
);
3412 CLASSTYPE_TEMPLATE_INFO (parm
)
3413 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
3414 TYPE_SIZE (parm
) = 0;
3419 tree template_type
= TREE_TYPE (template);
3421 tree found
= NULL_TREE
;
3424 int is_partial_instantiation
;
3426 template = most_general_template (template);
3427 parmlist
= DECL_TEMPLATE_PARMS (template);
3428 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3429 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3431 if (arg_depth
== 1 && parm_depth
> 1)
3433 /* We've been given an incomplete set of template arguments.
3436 template <class T> struct S1 {
3437 template <class U> struct S2 {};
3438 template <class U> struct S2<U*> {};
3441 we will be called with an ARGLIST of `U*', but the
3442 TEMPLATE will be `template <class T> template
3443 <class U> struct S1<T>::S2'. We must fill in the missing
3445 my_friendly_assert (context
!= NULL_TREE
, 0);
3446 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context
))
3447 && context
!= global_namespace
)
3448 context
= DECL_REAL_CONTEXT (context
);
3450 if (context
== global_namespace
)
3451 /* This is bad. We cannot get enough arguments, even from
3452 the surrounding context, to resolve this class. One
3453 case where this might happen is (illegal) code like:
3461 We should catch this error sooner (at the opening curly
3462 for `S', but it is better to be safe than sorry here. */
3464 cp_error ("invalid use of `%D'", template);
3465 return error_mark_node
;
3468 arglist
= add_to_template_args (TYPE_TI_ARGS (context
),
3470 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3473 my_friendly_assert (parm_depth
== arg_depth
, 0);
3475 /* Calculate the BOUND_ARGS. These will be the args that are
3476 actually tsubst'd into the definition to create the
3480 /* We have multiple levels of arguments to coerce, at once. */
3482 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3484 tree bound_args
= make_temp_vec (parm_depth
);
3486 for (i
= saved_depth
,
3487 t
= DECL_TEMPLATE_PARMS (template);
3488 i
> 0 && t
!= NULL_TREE
;
3489 --i
, t
= TREE_CHAIN (t
))
3491 tree a
= coerce_template_parms (TREE_VALUE (t
),
3492 arglist
, template, 1, 1);
3493 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3495 /* We temporarily reduce the length of the ARGLIST so
3496 that coerce_template_parms will see only the arguments
3497 corresponding to the template parameters it is
3499 TREE_VEC_LENGTH (arglist
)--;
3502 /* Restore the ARGLIST to its full size. */
3503 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3505 arglist
= bound_args
;
3509 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3510 innermost_args (arglist
),
3513 if (arglist
== error_mark_node
)
3514 /* We were unable to bind the arguments. */
3515 return error_mark_node
;
3517 /* In the scope of a template class, explicit references to the
3518 template class refer to the type of the template, not any
3519 instantiation of it. For example, in:
3521 template <class T> class C { void f(C<T>); }
3523 the `C<T>' is just the same as `C'. Outside of the
3524 class, however, such a reference is an instantiation. */
3525 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3528 found
= template_type
;
3530 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3534 /* Note that we use DECL_CONTEXT, rather than
3535 CP_DECL_CONTEXT, so that the termination test is
3536 always just `ctx'. We're not interested in namepace
3538 for (ctx
= current_class_type
;
3540 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3541 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3542 if (same_type_p (ctx
, template_type
))
3546 /* We're not in the scope of the class, so the
3547 TEMPLATE_TYPE is not the type we want after
3555 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3556 found
; found
= TREE_CHAIN (found
))
3557 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3561 found
= TREE_VALUE (found
);
3566 if (can_free (&permanent_obstack
, arglist
))
3567 obstack_free (&permanent_obstack
, arglist
);
3571 /* Since we didn't find the type, we'll have to create it.
3572 Since we'll be saving this type on the
3573 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3574 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3576 /* This type is a "partial instantiation" if any of the template
3577 arguments still inolve template parameters. Note that we set
3578 IS_PARTIAL_INSTANTIATION for partial specializations as
3580 is_partial_instantiation
= uses_template_parms (arglist
);
3582 /* Create the type. */
3583 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3585 if (!is_partial_instantiation
)
3586 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3588 /* We don't want to call start_enum for this type, since
3589 the values for the enumeration constants may involve
3590 template parameters. And, no one should be interested
3591 in the enumeration constants for such a type. */
3592 t
= make_node (ENUMERAL_TYPE
);
3596 t
= make_lang_type (TREE_CODE (template_type
));
3597 CLASSTYPE_DECLARED_CLASS (t
)
3598 = CLASSTYPE_DECLARED_CLASS (template_type
);
3599 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3600 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3601 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3604 /* If we called start_enum above, this information will already
3608 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3610 /* Create a stub TYPE_DECL for it. */
3611 type_decl
= build_decl (TYPE_DECL
, DECL_NAME (template), t
);
3612 SET_DECL_ARTIFICIAL (type_decl
);
3613 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3614 DECL_SOURCE_FILE (type_decl
)
3615 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
3616 DECL_SOURCE_LINE (type_decl
)
3617 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
3618 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = type_decl
;
3621 type_decl
= TYPE_NAME (t
);
3623 /* Set up the template information. We have to figure out which
3624 template is the immediate parent if this is a full
3626 if (parm_depth
== 1 || is_partial_instantiation
3627 || !PRIMARY_TEMPLATE_P (template))
3628 /* This case is easy; there are no member templates involved. */
3632 /* This is a full instantiation of a member template. There
3633 should be some partial instantiation of which this is an
3636 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3637 found
; found
= TREE_CHAIN (found
))
3640 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
3642 /* We only want partial instantiations, here, not
3643 specializations or full instantiations. */
3644 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
3645 || !uses_template_parms (TREE_VALUE (found
)))
3648 /* Temporarily reduce by one the number of levels in the
3649 ARGLIST and in FOUND so as to avoid comparing the
3650 last set of arguments. */
3651 TREE_VEC_LENGTH (arglist
)--;
3652 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
3654 /* See if the arguments match. If they do, then TMPL is
3655 the partial instantiation we want. */
3656 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
3658 /* Restore the argument vectors to their full size. */
3659 TREE_VEC_LENGTH (arglist
)++;
3660 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
3670 my_friendly_abort (0);
3673 arglist
= copy_to_permanent (arglist
);
3674 SET_TYPE_TEMPLATE_INFO (t
,
3675 tree_cons (found
, arglist
, NULL_TREE
));
3676 DECL_TEMPLATE_INSTANTIATIONS (template)
3677 = tree_cons (arglist
, t
,
3678 DECL_TEMPLATE_INSTANTIATIONS (template));
3680 if (TREE_CODE (t
) == ENUMERAL_TYPE
3681 && !is_partial_instantiation
)
3682 /* Now that the type has been registered on the instantiations
3683 list, we set up the enumerators. Because the enumeration
3684 constants may involve the enumeration type itself, we make
3685 sure to register the type first, and then create the
3686 constants. That way, doing tsubst_expr for the enumeration
3687 constants won't result in recursive calls here; we'll find
3688 the instantiation and exit above. */
3689 tsubst_enum (template_type
, t
, arglist
);
3691 /* We're done with the permanent obstack, now. */
3694 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3696 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
3697 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
3698 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
3699 if (!is_partial_instantiation
)
3701 DECL_ASSEMBLER_NAME (type_decl
)
3702 = get_identifier (build_overload_name (t
, 1, 1));
3704 /* For backwards compatibility; code that uses
3705 -fexternal-templates expects looking up a template to
3706 instantiate it. I think DDD still relies on this.
3707 (jason 8/20/1998) */
3708 if (TREE_CODE (t
) != ENUMERAL_TYPE
3709 && flag_external_templates
3710 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3711 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3712 add_pending_template (t
);
3715 /* If the type makes use of template parameters, the
3716 code that generates debugging information will crash. */
3717 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
3723 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3724 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3725 If FN returns non-zero, the iteration is terminated, and
3726 for_each_template_parm returns 1. Otherwise, the iteration
3727 continues. If FN never returns a non-zero value, the value
3728 returned by for_each_template_parm is 0. If FN is NULL, it is
3729 considered to be the function which always returns 1. */
3732 for_each_template_parm (t
, fn
, data
)
3740 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
3741 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
3744 switch (TREE_CODE (t
))
3748 /* We assume that the object must be instantiated in order to build
3749 the COMPONENT_REF, so we test only whether the type of the
3750 COMPONENT_REF uses template parms. */
3751 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3753 case IDENTIFIER_NODE
:
3754 if (!IDENTIFIER_TEMPLATE (t
))
3756 my_friendly_abort (42);
3758 /* aggregates of tree nodes */
3761 int i
= TREE_VEC_LENGTH (t
);
3763 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
3768 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
3769 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
3771 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
3774 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
3776 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
3778 /* constructed type nodes */
3780 case REFERENCE_TYPE
:
3781 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3784 if (TYPE_PTRMEMFUNC_FLAG (t
))
3785 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
3791 if (! TYPE_TEMPLATE_INFO (t
))
3793 return for_each_template_parm (TREE_VALUE
3794 (TYPE_TEMPLATE_INFO (t
)),
3797 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
3802 /* Check the parameter types. Since default arguments are not
3803 instantiated until they are needed, the TYPE_ARG_TYPES may
3804 contain expressions that involve template parameters. But,
3805 no-one should be looking at them yet. And, once they're
3806 instantiated, they don't contain template parameters, so
3807 there's no point in looking at them then, either. */
3811 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
3812 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
3816 /* Check the return type, too. */
3817 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3820 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
3822 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3824 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
3826 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3830 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3833 /* A template template parameter is encountered */
3834 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3835 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3836 /* Already substituted template template parameter */
3840 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
3842 goto check_type_and_context
;
3846 /* ??? What about FIELD_DECLs? */
3847 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
3848 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
3852 check_type_and_context
:
3853 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
3855 if (DECL_CONTEXT (t
)
3856 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
3861 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3863 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3865 /* template parm nodes */
3866 case TEMPLATE_TEMPLATE_PARM
:
3867 /* Record template parameters such as `T' inside `TT<T>'. */
3868 if (CLASSTYPE_TEMPLATE_INFO (t
)
3869 && for_each_template_parm (CLASSTYPE_TI_ARGS (t
), fn
, data
))
3871 case TEMPLATE_TYPE_PARM
:
3872 case TEMPLATE_PARM_INDEX
:
3874 return (*fn
)(t
, data
);
3878 /* simple type nodes */
3880 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
3882 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
3888 case NAMESPACE_DECL
:
3898 /* Non-error_mark_node ERROR_MARKs are bad things. */
3899 my_friendly_assert (t
== error_mark_node
, 274);
3908 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3911 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3914 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
3915 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3916 (TREE_TYPE (t
)), fn
, data
);
3917 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
3921 case REINTERPRET_CAST_EXPR
:
3922 case CONST_CAST_EXPR
:
3923 case STATIC_CAST_EXPR
:
3924 case DYNAMIC_CAST_EXPR
:
3932 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3935 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3943 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
3944 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
3951 sorry ("testing %s for template parms",
3952 tree_code_name
[(int) TREE_CODE (t
)]);
3953 my_friendly_abort (82);
3960 uses_template_parms (t
)
3963 return for_each_template_parm (t
, 0, 0);
3966 static struct tinst_level
*current_tinst_level
;
3967 static struct tinst_level
*free_tinst_level
;
3968 static int tinst_depth
;
3969 extern int max_tinst_depth
;
3970 #ifdef GATHER_STATISTICS
3973 int tinst_level_tick
;
3974 int last_template_error_tick
;
3976 /* Print out all the template instantiations that we are currently
3977 working on. If ERR, we are being called from cp_thing, so do
3978 the right thing for an error message. */
3981 print_template_context (err
)
3984 struct tinst_level
*p
= current_tinst_level
;
3986 char *file
= input_filename
;
3990 if (current_function_decl
!= p
->decl
3991 && current_function_decl
!= NULL_TREE
)
3992 /* We can get here during the processing of some synthesized
3993 method. Then, p->decl will be the function that's causing
3998 if (current_function_decl
== p
->decl
)
3999 /* Avoid redundancy with the the "In function" line. */;
4001 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4002 file
, decl_as_string (p
->decl
, 0));
4010 for (; p
; p
= p
->next
)
4012 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4013 decl_as_string (p
->decl
, 0));
4017 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4020 /* Called from cp_thing to print the template context for an error. */
4023 maybe_print_template_context ()
4025 if (last_template_error_tick
== tinst_level_tick
4026 || current_tinst_level
== 0)
4029 last_template_error_tick
= tinst_level_tick
;
4030 print_template_context (1);
4034 push_tinst_level (d
)
4037 struct tinst_level
*new;
4039 if (tinst_depth
>= max_tinst_depth
)
4041 /* If the instantiation in question still has unbound template parms,
4042 we don't really care if we can't instantiate it, so just return.
4043 This happens with base instantiation for implicit `typename'. */
4044 if (uses_template_parms (d
))
4047 last_template_error_tick
= tinst_level_tick
;
4048 error ("template instantiation depth exceeds maximum of %d",
4050 error (" (use -ftemplate-depth-NN to increase the maximum)");
4051 cp_error (" instantiating `%D'", d
);
4053 print_template_context (0);
4058 if (free_tinst_level
)
4060 new = free_tinst_level
;
4061 free_tinst_level
= new->next
;
4064 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4068 new->file
= input_filename
;
4069 new->next
= current_tinst_level
;
4070 current_tinst_level
= new;
4073 #ifdef GATHER_STATISTICS
4074 if (tinst_depth
> depth_reached
)
4075 depth_reached
= tinst_depth
;
4085 struct tinst_level
*old
= current_tinst_level
;
4087 /* Restore the filename and line number stashed away when we started
4088 this instantiation. */
4090 input_filename
= old
->file
;
4091 extract_interface_info ();
4093 current_tinst_level
= old
->next
;
4094 old
->next
= free_tinst_level
;
4095 free_tinst_level
= old
;
4100 struct tinst_level
*
4103 struct tinst_level
*p
= current_tinst_level
;
4106 for (; p
->next
; p
= p
->next
)
4111 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4112 vector of template arguments, as for tsubst.
4114 Returns an appropriate tsbust'd friend declaration. */
4117 tsubst_friend_function (decl
, args
)
4123 char *file
= input_filename
;
4125 lineno
= DECL_SOURCE_LINE (decl
);
4126 input_filename
= DECL_SOURCE_FILE (decl
);
4128 if (TREE_CODE (decl
) == FUNCTION_DECL
4129 && DECL_TEMPLATE_INSTANTIATION (decl
)
4130 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4131 /* This was a friend declared with an explicit template
4132 argument list, e.g.:
4136 to indicate that f was a template instantiation, not a new
4137 function declaration. Now, we have to figure out what
4138 instantiation of what template. */
4145 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4147 tsubst (DECL_TI_ARGS (decl
),
4149 /* FIXME: The decl we create via the next tsubst could be
4150 created on a temporary obstack. */
4151 new_friend
= tsubst (decl
, args
, NULL_TREE
);
4152 tmpl
= determine_specialization (template_id
, new_friend
,
4154 /*need_member_template=*/0,
4156 new_friend
= instantiate_template (tmpl
, new_args
);
4160 new_friend
= tsubst (decl
, args
, NULL_TREE
);
4162 /* The NEW_FRIEND will look like an instantiation, to the
4163 compiler, but is not an instantiation from the point of view of
4164 the language. For example, we might have had:
4166 template <class T> struct S {
4167 template <class U> friend void f(T, U);
4170 Then, in S<int>, template <class U> void f(int, U) is not an
4171 instantiation of anything. */
4172 DECL_USE_TEMPLATE (new_friend
) = 0;
4173 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4174 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4176 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4177 tsubst will have resulted in a call to
4178 set_mangled_name_for_template_decl. But, the function is not a
4179 template instantiation and should not be mangled like one.
4180 Therefore, we remangle the function name. We don't have to do
4181 this if the NEW_FRIEND is a template since
4182 set_mangled_name_for_template_decl doesn't do anything if the
4183 function declaration still uses template arguments. */
4184 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4186 set_mangled_name_for_decl (new_friend
);
4187 DECL_RTL (new_friend
) = 0;
4188 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4191 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4194 tree new_friend_args
;
4196 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4197 /* This declaration is a `primary' template. */
4198 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4200 /* We must save the DECL_TI_ARGS for NEW_FRIEND here because
4201 pushdecl may call duplicate_decls which will free NEW_FRIEND
4203 new_friend_args
= DECL_TI_ARGS (new_friend
);
4204 old_decl
= pushdecl_namespace_level (new_friend
);
4206 if (old_decl
!= new_friend
)
4208 /* This new friend declaration matched an existing
4209 declaration. For example, given:
4211 template <class T> void f(T);
4212 template <class U> class C {
4213 template <class T> friend void f(T) {}
4216 the friend declaration actually provides the definition
4217 of `f', once C has been instantiated for some type. So,
4218 old_decl will be the out-of-class template declaration,
4219 while new_friend is the in-class definition.
4221 But, if `f' was called before this point, the
4222 instantiation of `f' will have DECL_TI_ARGS corresponding
4223 to `T' but not to `U', references to which might appear
4224 in the definition of `f'. Previously, the most general
4225 template for an instantiation of `f' was the out-of-class
4226 version; now it is the in-class version. Therefore, we
4227 run through all specialization of `f', adding to their
4228 DECL_TI_ARGS appropriately. In particular, they need a
4229 new set of outer arguments, corresponding to the
4230 arguments for this class instantiation.
4232 The same situation can arise with something like this:
4235 template <class T> class C {
4239 when `C<int>' is instantiated. Now, `f(int)' is defined
4242 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4243 /* duplicate_decls will take care of this case. */
4249 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4253 tree spec
= TREE_VALUE (t
);
4256 = add_outermost_template_args (new_friend_args
,
4257 DECL_TI_ARGS (spec
));
4259 = copy_to_permanent (DECL_TI_ARGS (spec
));
4262 /* Now, since specializations are always supposed to
4263 hang off of the most general template, we must move
4265 t
= most_general_template (old_decl
);
4268 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4269 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4270 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4271 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4275 /* The information from NEW_FRIEND has been merged into OLD_DECL
4276 by duplicate_decls. */
4277 new_friend
= old_decl
;
4280 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4282 /* Check to see that the declaration is really present, and,
4283 possibly obtain an improved declaration. */
4284 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4293 input_filename
= file
;
4297 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4298 template arguments, as for tsubst.
4300 Returns an appropriate tsbust'd friend type. */
4303 tsubst_friend_class (friend_tmpl
, args
)
4308 tree tmpl
= lookup_name (DECL_NAME (friend_tmpl
), 1);
4310 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4312 if (tmpl
!= NULL_TREE
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4314 /* The friend template has already been declared. Just
4315 check to see that the declarations match, and install any new
4316 default parameters. We must tsubst the default parameters,
4317 of course. We only need the innermost template parameters
4318 because that is all that redeclare_class_template will look
4321 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4323 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4324 friend_type
= TREE_TYPE (tmpl
);
4328 /* The friend template has not already been declared. In this
4329 case, the instantiation of the template class will cause the
4330 injection of this template into the global scope. */
4331 tmpl
= tsubst (friend_tmpl
, args
, NULL_TREE
);
4333 /* The new TMPL is not an instantiation of anything, so we
4334 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4335 the new type because that is supposed to be the corresponding
4336 template decl, i.e., TMPL. */
4337 DECL_USE_TEMPLATE (tmpl
) = 0;
4338 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4339 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4341 /* Inject this template into the global scope. */
4342 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4349 instantiate_class_template (type
)
4352 tree
template, args
, pattern
, t
;
4355 if (type
== error_mark_node
)
4356 return error_mark_node
;
4358 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4361 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4362 args
= CLASSTYPE_TI_ARGS (type
);
4363 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4364 t
= most_specialized_class (template, args
);
4366 if (t
== error_mark_node
)
4368 char *str
= "candidates are:";
4369 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4370 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
4372 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4375 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4379 TYPE_BEING_DEFINED (type
) = 1;
4380 return error_mark_node
;
4383 pattern
= TREE_TYPE (t
);
4385 pattern
= TREE_TYPE (template);
4387 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4392 /* This TYPE is actually a instantiation of of a partial
4393 specialization. We replace the innermost set of ARGS with
4394 the arguments appropriate for substitution. For example,
4397 template <class T> struct S {};
4398 template <class T> struct S<T*> {};
4400 and supposing that we are instantiating S<int*>, ARGS will
4401 present be {int*} but we need {int}. */
4403 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4406 /* If there were multiple levels in ARGS, replacing the
4407 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4408 want, so we make a copy first. */
4409 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4411 args
= copy_node (args
);
4412 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4418 if (pedantic
&& uses_template_parms (args
))
4420 /* If there are still template parameters amongst the args, then
4421 we can't instantiate the type; there's no telling whether or not one
4422 of the template parameters might eventually be instantiated to some
4423 value that results in a specialization being used. We do the
4424 type as complete so that, for example, declaring one of its
4425 members to be a friend will not be rejected. */
4426 TYPE_SIZE (type
) = integer_zero_node
;
4430 TYPE_BEING_DEFINED (type
) = 1;
4432 if (! push_tinst_level (type
))
4435 maybe_push_to_top_level (uses_template_parms (type
));
4436 pushclass (type
, 0);
4438 /* We must copy the arguments to the permanent obstack since
4439 during the tsubst'ing below they may wind up in the
4440 DECL_TI_ARGS of some instantiated member template. */
4441 args
= copy_to_permanent (args
);
4443 if (flag_external_templates
)
4445 if (flag_alt_external_templates
)
4447 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4448 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4449 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4450 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4451 && CLASSTYPE_INTERFACE_KNOWN (type
));
4455 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4456 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4457 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4458 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4459 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4460 && CLASSTYPE_INTERFACE_KNOWN (type
));
4465 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4466 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4469 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4470 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4471 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
4472 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4473 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4474 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4475 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4476 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4477 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4478 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4479 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4480 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
4481 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
4482 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
4483 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
4484 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
4485 TYPE_USES_COMPLEX_INHERITANCE (type
)
4486 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
4487 TYPE_USES_MULTIPLE_INHERITANCE (type
)
4488 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
4489 TYPE_USES_VIRTUAL_BASECLASSES (type
)
4490 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
4491 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
4492 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
4493 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
4495 /* If this is a partial instantiation, don't tsubst anything. We will
4496 only use this type for implicit typename, so the actual contents don't
4497 matter. All that matters is whether a particular name is a type. */
4498 if (uses_template_parms (type
))
4500 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4501 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4502 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4503 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4504 TYPE_SIZE (type
) = integer_zero_node
;
4509 tree binfo
= TYPE_BINFO (type
);
4510 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
4516 int len
= TREE_VEC_LENGTH (pbases
);
4517 bases
= make_tree_vec (len
);
4518 for (i
= 0; i
< len
; ++i
)
4522 TREE_VEC_ELT (bases
, i
) = elt
4523 = tsubst (TREE_VEC_ELT (pbases
, i
), args
, NULL_TREE
);
4524 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
4526 basetype
= TREE_TYPE (elt
);
4528 if (! IS_AGGR_TYPE (basetype
))
4530 ("base type `%T' of `%T' fails to be a struct or class type",
4532 else if (TYPE_SIZE (complete_type (basetype
)) == NULL_TREE
)
4533 cp_error ("base class `%T' of `%T' has incomplete type",
4536 /* These are set up in xref_basetypes for normal classes, so
4537 we have to handle them here for template bases. */
4539 unshare_base_binfos (elt
);
4541 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4543 TYPE_USES_VIRTUAL_BASECLASSES (type
) = 1;
4544 TYPE_USES_COMPLEX_INHERITANCE (type
) = 1;
4546 TYPE_GETS_NEW (type
) |= TYPE_GETS_NEW (basetype
);
4547 TYPE_GETS_DELETE (type
) |= TYPE_GETS_DELETE (basetype
);
4549 /* Don't initialize this until the vector is filled out, or
4550 lookups will crash. */
4551 BINFO_BASETYPES (binfo
) = bases
;
4555 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
4557 tree tag
= TREE_VALUE (t
);
4558 tree name
= TYPE_IDENTIFIER (tag
);
4561 newtag
= tsubst (tag
, args
, NULL_TREE
);
4562 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
4564 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
4565 /* Unfortunately, lookup_template_class sets
4566 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4567 instantiation (i.e., for the type of a member template
4568 class nested within a template class.) This behavior is
4569 required for maybe_process_partial_specialization to work
4570 correctly, but is not accurate in this case; the TAG is not
4571 an instantiation of anything. (The corresponding
4572 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4573 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
4575 /* Now, we call pushtag to put this NEWTAG into the scope of
4576 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4577 pushtag calling push_template_decl. We don't have to do
4578 this for enums because it will already have been done in
4581 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
4582 pushtag (name
, newtag
, /*globalize=*/0);
4586 /* Don't replace enum constants here. */
4587 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
4588 if (TREE_CODE (t
) != CONST_DECL
)
4592 /* The the file and line for this declaration, to assist in
4593 error message reporting. Since we called push_tinst_level
4594 above, we don't need to restore these. */
4595 lineno
= DECL_SOURCE_LINE (t
);
4596 input_filename
= DECL_SOURCE_FILE (t
);
4598 r
= tsubst (t
, args
, NULL_TREE
);
4599 if (TREE_CODE (r
) == VAR_DECL
)
4601 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
4602 /* Perhaps we should do more of grokfield here. */
4604 DECL_IN_AGGR_P (r
) = 1;
4605 DECL_EXTERNAL (r
) = 1;
4606 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
4609 /* R will have a TREE_CHAIN if and only if it has already been
4610 processed by finish_member_declaration. This can happen
4611 if, for example, it is a TYPE_DECL for a class-scoped
4612 ENUMERAL_TYPE; such a thing will already have been added to
4613 the field list by tsubst_enum above. */
4614 if (!TREE_CHAIN (r
))
4616 set_current_access_from_decl (r
);
4617 finish_member_declaration (r
);
4621 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4622 for this instantiation. */
4623 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
4625 tree r
= tsubst (t
, args
, NULL_TREE
);
4626 set_current_access_from_decl (r
);
4627 finish_member_declaration (r
);
4630 /* Construct the DECL_FRIENDLIST for the new class type. */
4631 typedecl
= TYPE_MAIN_DECL (type
);
4632 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
4638 DECL_FRIENDLIST (typedecl
)
4639 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
4640 DECL_FRIENDLIST (typedecl
));
4642 for (friends
= TREE_VALUE (t
);
4643 friends
!= NULL_TREE
;
4644 friends
= TREE_CHAIN (friends
))
4646 if (TREE_PURPOSE (friends
) == error_mark_node
)
4648 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4649 = tree_cons (error_mark_node
,
4650 tsubst_friend_function (TREE_VALUE (friends
),
4652 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4656 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4657 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
, NULL_TREE
),
4659 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4665 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
4669 tree friend_type
= TREE_VALUE (t
);
4670 tree new_friend_type
;
4672 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4673 new_friend_type
= tsubst_friend_class (friend_type
, args
);
4674 else if (uses_template_parms (friend_type
))
4675 new_friend_type
= tsubst (friend_type
, args
, NULL_TREE
);
4677 /* The call to xref_tag_from_type does injection for friend
4680 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
4683 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4684 /* Trick make_friend_class into realizing that the friend
4685 we're adding is a template, not an ordinary class. It's
4686 important that we use make_friend_class since it will
4687 perform some error-checking and output cross-reference
4689 ++processing_template_decl
;
4691 make_friend_class (type
, new_friend_type
);
4693 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4694 --processing_template_decl
;
4697 /* This does injection for friend functions. */
4698 if (!processing_template_decl
)
4700 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
, NULL_TREE
);
4702 for (; t
; t
= TREE_CHAIN (t
))
4704 tree d
= TREE_VALUE (t
);
4706 if (TREE_CODE (d
) == TYPE_DECL
)
4707 /* Already injected. */;
4713 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
4714 if (TREE_CODE (t
) == FIELD_DECL
)
4716 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
4717 require_complete_type (t
);
4720 /* Set the file and line number information to whatever is given for
4721 the class itself. This puts error messages involving generated
4722 implicit functions at a predictable point, and the same point
4723 that would be used for non-template classes. */
4724 lineno
= DECL_SOURCE_LINE (typedecl
);
4725 input_filename
= DECL_SOURCE_FILE (typedecl
);
4727 unreverse_member_declarations (type
);
4728 type
= finish_struct_1 (type
, 0);
4729 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
4731 repo_template_used (type
);
4734 TYPE_BEING_DEFINED (type
) = 0;
4737 pop_from_top_level ();
4747 if (t1
== NULL_TREE
)
4748 return t2
== NULL_TREE
;
4749 if (t2
== NULL_TREE
)
4751 /* Don't care if one declares its arg const and the other doesn't -- the
4752 main variant of the arg type is all that matters. */
4753 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
4754 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
4756 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
4759 /* If arg is a non-type template parameter that does not depend on template
4760 arguments, fold it like we weren't in the body of a template. */
4763 maybe_fold_nontype_arg (arg
)
4766 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
4767 && !uses_template_parms (arg
))
4769 /* Sometimes, one of the args was an expression involving a
4770 template constant parameter, like N - 1. Now that we've
4771 tsubst'd, we might have something like 2 - 1. This will
4772 confuse lookup_template_class, so we do constant folding
4773 here. We have to unset processing_template_decl, to
4774 fool build_expr_from_tree() into building an actual
4777 int saved_processing_template_decl
= processing_template_decl
;
4778 processing_template_decl
= 0;
4779 arg
= fold (build_expr_from_tree (arg
));
4780 processing_template_decl
= saved_processing_template_decl
;
4785 /* Return the TREE_VEC with the arguments for the innermost template header,
4786 where ARGS is either that or the VEC of VECs for all the
4790 innermost_args (args
)
4793 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
4796 /* Substitute ARGS into the vector of template arguments T. */
4799 tsubst_template_arg_vector (t
, args
)
4803 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4804 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4806 bzero ((char *) elts
, len
* sizeof (tree
));
4808 for (i
= 0; i
< len
; i
++)
4810 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
4811 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
4812 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
), args
);
4814 elts
[i
] = maybe_fold_nontype_arg
4815 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, NULL_TREE
));
4817 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4824 t
= make_tree_vec (len
);
4825 for (i
= 0; i
< len
; i
++)
4826 TREE_VEC_ELT (t
, i
) = elts
[i
];
4831 /* Return the result of substituting ARGS into the template parameters
4832 given by PARMS. If there are m levels of ARGS and m + n levels of
4833 PARMS, then the result will contain n levels of PARMS. For
4834 example, if PARMS is `template <class T> template <class U>
4835 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
4836 result will be `template <int*, double, class V>'. */
4839 tsubst_template_parms (parms
, args
)
4844 tree
* new_parms
= &r
;
4846 for (new_parms
= &r
;
4847 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
4848 new_parms
= &(TREE_CHAIN (*new_parms
)),
4849 parms
= TREE_CHAIN (parms
))
4852 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
4855 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
4857 tree default_value
=
4858 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4860 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4862 TREE_VEC_ELT (new_vec
, i
)
4863 = build_tree_list (tsubst (default_value
, args
, NULL_TREE
),
4864 tsubst (parm_decl
, args
, NULL_TREE
));
4869 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
4870 - TMPL_ARGS_DEPTH (args
))),
4871 new_vec
, NULL_TREE
);
4877 /* Substitute the ARGS into the indicated aggregate (or enumeration)
4878 type T. If T is not an aggregate or enumeration type, it is
4879 handled as if by tsubst. IN_DECL is as for tsubst. If
4880 ENTERING_SCOPE is non-zero, T is the context for a template which
4881 we are presently tsubst'ing. Return the subsituted value. */
4884 tsubst_aggr_type (t
, args
, in_decl
, entering_scope
)
4893 switch (TREE_CODE (t
))
4896 if (TYPE_PTRMEMFUNC_P (t
))
4898 tree r
= build_ptrmemfunc_type
4899 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, in_decl
));
4900 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
4903 /* else fall through */
4906 if (uses_template_parms (t
))
4912 /* First, determine the context for the type we are looking
4914 if (TYPE_CONTEXT (t
) != NULL_TREE
)
4915 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
4916 in_decl
, /*entering_scope=*/1);
4918 context
= NULL_TREE
;
4920 /* Then, figure out what arguments are appropriate for the
4921 type we are trying to find. For example, given:
4923 template <class T> struct S;
4924 template <class T, class U> void f(T, U) { S<U> su; }
4926 and supposing that we are instantiating f<int, double>,
4927 then our ARGS will be {int, double}, but, when looking up
4928 S we only want {double}. */
4929 argvec
= tsubst (TYPE_TI_ARGS (t
), args
, in_decl
);
4931 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
4934 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
4937 /* This is not a template type, so there's nothing to do. */
4941 return tsubst (t
, args
, in_decl
);
4945 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
4946 (already computed) substitution of ARGS into TREE_TYPE (T), if
4947 appropriate. Return the result of the substitution. IN_DECL is as
4951 tsubst_decl (t
, args
, type
, in_decl
)
4958 char* saved_filename
;
4961 /* Set the filename and linenumber to improve error-reporting. */
4962 saved_lineno
= lineno
;
4963 saved_filename
= input_filename
;
4964 lineno
= DECL_SOURCE_LINE (t
);
4965 input_filename
= DECL_SOURCE_FILE (t
);
4967 switch (TREE_CODE (t
))
4971 /* We can get here when processing a member template function
4972 of a template class. */
4973 tree decl
= DECL_TEMPLATE_RESULT (t
);
4975 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
4977 if (!is_template_template_parm
)
4979 /* We might already have an instance of this template.
4980 The ARGS are for the surrounding class type, so the
4981 full args contain the tsubst'd args for the context,
4982 plus the innermost args from the template decl. */
4983 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
4984 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
4985 : DECL_TI_ARGS (DECL_RESULT (t
));
4986 tree full_args
= tsubst (tmpl_args
, args
, in_decl
);
4988 /* tsubst_template_arg_vector doesn't copy the vector if
4989 nothing changed. But, *something* should have
4991 my_friendly_assert (full_args
!= tmpl_args
, 0);
4993 spec
= retrieve_specialization (t
, full_args
);
4994 if (spec
!= NULL_TREE
)
5001 /* Make a new template decl. It will be similar to the
5002 original, but will record the current template arguments.
5003 We also create a new function declaration, which is just
5004 like the old one, but points to this new template, rather
5005 than the old one. */
5008 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5009 TREE_CHAIN (r
) = NULL_TREE
;
5011 if (is_template_template_parm
)
5013 tree new_decl
= tsubst (decl
, args
, in_decl
);
5014 DECL_RESULT (r
) = new_decl
;
5015 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5020 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
5021 /*entering_scope=*/1);
5022 DECL_CLASS_CONTEXT (r
)
5023 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
, in_decl
,
5024 /*entering_scope=*/1);
5025 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5027 if (TREE_CODE (decl
) == TYPE_DECL
)
5029 tree new_type
= tsubst (TREE_TYPE (t
), args
, in_decl
);
5030 TREE_TYPE (r
) = new_type
;
5031 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5032 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5033 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5037 tree new_decl
= tsubst (decl
, args
, in_decl
);
5038 DECL_RESULT (r
) = new_decl
;
5039 DECL_TI_TEMPLATE (new_decl
) = r
;
5040 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5041 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5044 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5045 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5046 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5048 /* The template parameters for this new template are all the
5049 template parameters for the old template, except the
5050 outermost level of parameters. */
5051 DECL_TEMPLATE_PARMS (r
)
5052 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
);
5054 if (PRIMARY_TEMPLATE_P (t
))
5055 DECL_PRIMARY_TEMPLATE (r
) = r
;
5057 /* We don't partially instantiate partial specializations. */
5058 if (TREE_CODE (decl
) == TYPE_DECL
)
5061 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5063 spec
= TREE_CHAIN (spec
))
5065 /* It helps to consider example here. Consider:
5076 Now, for example, we are instantiating S<int>::f(U u).
5077 We want to make a template:
5082 It will have a specialization, for the case U = int*, of
5086 void S<int>::f<int*>(int*);
5088 This specialization will be an instantiation of
5089 the specialization given in the declaration of S, with
5090 argument list int*. */
5092 tree fn
= TREE_VALUE (spec
);
5096 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5097 /* Instantiations are on the same list, but they're of
5098 no concern to us. */
5101 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5102 /* A full specialization. There's no need to record
5106 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
, in_decl
);
5107 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5108 spec_args
, in_decl
);
5109 DECL_TI_TEMPLATE (new_fn
) = fn
;
5110 register_specialization (new_fn
, r
,
5111 innermost_args (spec_args
));
5114 /* Record this partial instantiation. */
5115 register_specialization (r
, t
,
5116 DECL_TI_ARGS (DECL_RESULT (r
)));
5124 tree argvec
= NULL_TREE
;
5130 /* Nobody should be tsubst'ing into non-template functions. */
5131 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5133 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5137 /* Calculate the most general template of which R is a
5138 specialization, and the complete set of arguments used to
5140 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5141 argvec
= tsubst (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5144 /* Check to see if we already have this specialization. */
5145 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5152 /* Here, we deal with the peculiar case:
5154 template <class T> struct S {
5155 template <class U> friend void f();
5157 template <class U> friend void f() {}
5159 template void f<double>();
5161 Here, the ARGS for the instantiation of will be {int,
5162 double}. But, we only need as many ARGS as there are
5163 levels of template parameters in CODE_PATTERN. We are
5164 careful not to get fooled into reducing the ARGS in
5167 template <class T> struct S { template <class U> void f(U); }
5168 template <class T> template <> void S<T>::f(int) {}
5170 which we can spot because the pattern will be a
5171 specialization in this case. */
5172 args_depth
= TMPL_ARGS_DEPTH (args
);
5174 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5175 if (args_depth
> parms_depth
5176 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5178 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5180 if (parms_depth
> 1)
5184 args
= make_temp_vec (parms_depth
);
5185 for (i
= 0; i
< parms_depth
; ++i
)
5186 TREE_VEC_ELT (args
, i
) =
5187 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5190 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5195 /* This special case arises when we have something like this:
5197 template <class T> struct S {
5198 friend void f<int>(int, double);
5201 Here, the DECL_TI_TEMPLATE for the friend declaration
5202 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5203 being called from tsubst_friend_function, and we want
5204 only to create a new decl (R) with appropriate types so
5205 that we can call determine_specialization. */
5206 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5208 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5209 == IDENTIFIER_NODE
), 0);
5210 gen_tmpl
= NULL_TREE
;
5213 if (DECL_CLASS_SCOPE_P (t
))
5215 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5219 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
, t
,
5220 /*entering_scope=*/1);
5227 type
= tsubst (type
, args
, in_decl
);
5229 /* We do NOT check for matching decls pushed separately at this
5230 point, as they may not represent instantiations of this
5231 template, and in any case are considered separate under the
5232 discrete model. Instead, see add_maybe_template. */
5236 DECL_USE_TEMPLATE (r
) = 0;
5237 TREE_TYPE (r
) = type
;
5240 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, t
, /*entering_scope=*/1);
5241 DECL_CLASS_CONTEXT (r
) = ctx
;
5243 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
5244 IDENTIFIER_POINTER (DECL_NAME (r
)),
5245 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
5247 /* Type-conversion operator. Reconstruct the name, in
5248 case it's the name of one of the template's parameters. */
5249 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5252 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, t
);
5253 DECL_MAIN_VARIANT (r
) = r
;
5254 DECL_RESULT (r
) = NULL_TREE
;
5255 DECL_INITIAL (r
) = NULL_TREE
;
5257 TREE_STATIC (r
) = 0;
5258 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5259 DECL_EXTERNAL (r
) = 1;
5260 DECL_INTERFACE_KNOWN (r
) = 0;
5261 DECL_DEFER_OUTPUT (r
) = 0;
5262 TREE_CHAIN (r
) = NULL_TREE
;
5263 DECL_PENDING_INLINE_INFO (r
) = 0;
5266 if (DECL_CONSTRUCTOR_P (r
))
5268 maybe_retrofit_in_chrg (r
);
5269 grok_ctor_properties (ctx
, r
);
5271 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5272 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5274 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5275 name. There's no need to do this in the special friend
5276 case mentioned above where GEN_TMPL is NULL. */
5279 DECL_TEMPLATE_INFO (r
)
5280 = perm_tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5281 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5282 register_specialization (r
, gen_tmpl
, argvec
);
5284 /* Set the mangled name for R. */
5285 if (DECL_DESTRUCTOR_P (t
))
5286 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5289 /* Instantiations of template functions must be mangled
5290 specially, in order to conform to 14.5.5.1
5291 [temp.over.link]. */
5292 tree tmpl
= DECL_TI_TEMPLATE (t
);
5294 /* TMPL will be NULL if this is a specialization of a
5295 member function of a template class. */
5296 if (name_mangling_version
< 1
5297 || tmpl
== NULL_TREE
5298 || (member
&& !is_member_template (tmpl
)
5299 && !DECL_TEMPLATE_INFO (tmpl
)))
5300 set_mangled_name_for_decl (r
);
5302 set_mangled_name_for_template_decl (r
);
5306 make_decl_rtl (r
, NULL_PTR
, 1);
5308 /* Like grokfndecl. If we don't do this, pushdecl will
5309 mess up our TREE_CHAIN because it doesn't find a
5310 previous decl. Sigh. */
5312 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5314 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5322 TREE_TYPE (r
) = type
;
5323 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5324 DECL_INITIAL (r
) = TREE_TYPE (r
);
5326 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
, in_decl
);
5328 DECL_CONTEXT (r
) = NULL_TREE
;
5329 #ifdef PROMOTE_PROTOTYPES
5330 if ((TREE_CODE (type
) == INTEGER_TYPE
5331 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5332 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5333 DECL_ARG_TYPE (r
) = integer_type_node
;
5336 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, TREE_CHAIN (t
));
5343 TREE_TYPE (r
) = type
;
5346 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, in_decl
);
5348 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, in_decl
);
5349 TREE_CHAIN (r
) = NULL_TREE
;
5350 if (TREE_CODE (type
) == VOID_TYPE
)
5351 cp_error_at ("instantiation of `%D' as type void", r
);
5359 = tsubst_copy (DECL_INITIAL (t
), args
, in_decl
);
5360 TREE_CHAIN (r
) = NULL_TREE
;
5370 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
5371 /*entering_scope=*/1);
5373 /* Nobody should be tsubst'ing into non-template variables. */
5374 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
5375 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5377 /* Check to see if we already have this specialization. */
5378 tmpl
= DECL_TI_TEMPLATE (t
);
5379 gen_tmpl
= most_general_template (tmpl
);
5380 argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
5381 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5390 TREE_TYPE (r
) = type
;
5391 DECL_CONTEXT (r
) = ctx
;
5392 if (TREE_STATIC (r
))
5393 DECL_ASSEMBLER_NAME (r
)
5394 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
5396 /* Don't try to expand the initializer until someone tries to use
5397 this variable; otherwise we run into circular dependencies. */
5398 DECL_INITIAL (r
) = NULL_TREE
;
5402 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
5404 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
5405 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5406 register_specialization (r
, gen_tmpl
, argvec
);
5408 TREE_CHAIN (r
) = NULL_TREE
;
5409 if (TREE_CODE (type
) == VOID_TYPE
)
5410 cp_error_at ("instantiation of `%D' as type void", r
);
5415 if (t
== TYPE_NAME (TREE_TYPE (t
)))
5416 r
= TYPE_NAME (type
);
5420 TREE_TYPE (r
) = type
;
5421 DECL_CONTEXT (r
) = current_class_type
;
5422 TREE_CHAIN (r
) = NULL_TREE
;
5427 my_friendly_abort (0);
5430 /* Restore the file and line information. */
5431 lineno
= saved_lineno
;
5432 input_filename
= saved_filename
;
5437 /* Substitue into the ARG_TYPES of a function type. */
5440 tsubst_arg_types (arg_types
, args
, in_decl
)
5445 tree remaining_arg_types
;
5448 if (!arg_types
|| arg_types
== void_list_node
)
5451 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
5454 /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't
5455 matter on function types. */
5456 type
= TYPE_MAIN_VARIANT (type_decays_to
5457 (tsubst (TREE_VALUE (arg_types
),
5460 /* Note that we do not substitute into default arguments here. The
5461 standard mandates that they be instantiated only when needed,
5462 which is done in build_over_call. */
5463 return hash_tree_cons_simple (TREE_PURPOSE (arg_types
), type
,
5464 remaining_arg_types
);
5468 /* Take the tree structure T and replace template parameters used therein
5469 with the argument vector ARGS. IN_DECL is an associated decl for
5472 tsubst is used for dealing with types, decls and the like; for
5473 expressions, use tsubst_expr or tsubst_copy. */
5476 tsubst (t
, args
, in_decl
)
5482 if (t
== NULL_TREE
|| t
== error_mark_node
5483 || t
== integer_type_node
5484 || t
== void_type_node
5485 || t
== char_type_node
5486 || TREE_CODE (t
) == NAMESPACE_DECL
)
5489 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
5490 type
= IDENTIFIER_TYPE_VALUE (t
);
5492 type
= TREE_TYPE (t
);
5493 if (type
== unknown_type_node
)
5494 my_friendly_abort (42);
5496 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
5497 && TREE_CODE (t
) != TYPENAME_TYPE
5498 && TREE_CODE (t
) != TEMPLATE_DECL
5499 && TREE_CODE (t
) != IDENTIFIER_NODE
)
5500 type
= tsubst (type
, args
, in_decl
);
5502 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
5503 return tsubst_decl (t
, args
, type
, in_decl
);
5505 switch (TREE_CODE (t
))
5510 return tsubst_aggr_type (t
, args
, in_decl
, /*entering_scope=*/0);
5513 case IDENTIFIER_NODE
:
5525 if (t
== integer_type_node
)
5528 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
5529 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
5533 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
5534 max
= tsubst_expr (max
, args
, in_decl
);
5535 if (processing_template_decl
)
5537 tree itype
= make_node (INTEGER_TYPE
);
5538 TYPE_MIN_VALUE (itype
) = size_zero_node
;
5539 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
5544 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
5545 return build_index_2_type (size_zero_node
, max
);
5548 case TEMPLATE_TYPE_PARM
:
5549 case TEMPLATE_TEMPLATE_PARM
:
5550 case TEMPLATE_PARM_INDEX
:
5557 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
5558 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
5560 idx
= TEMPLATE_TYPE_IDX (t
);
5561 level
= TEMPLATE_TYPE_LEVEL (t
);
5565 idx
= TEMPLATE_PARM_IDX (t
);
5566 level
= TEMPLATE_PARM_LEVEL (t
);
5569 if (TREE_VEC_LENGTH (args
) > 0)
5571 tree arg
= NULL_TREE
;
5573 levels
= TMPL_ARGS_DEPTH (args
);
5574 if (level
<= levels
)
5575 arg
= TMPL_ARG (args
, level
, idx
);
5577 if (arg
== error_mark_node
)
5578 return error_mark_node
;
5579 else if (arg
!= NULL_TREE
)
5581 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5583 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
5585 return cp_build_qualified_type
5586 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
));
5588 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
5590 if (CLASSTYPE_TEMPLATE_INFO (t
))
5592 /* We are processing a type constructed from
5593 a template template parameter */
5594 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
5598 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5599 we are resolving nested-types in the signature of
5600 a member function templates.
5601 Otherwise ARG is a TEMPLATE_DECL and is the real
5602 template to be instantiated. */
5603 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
5604 arg
= TYPE_NAME (arg
);
5606 r
= lookup_template_class (DECL_NAME (arg
),
5609 /*entering_scope=*/0);
5610 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5613 /* We are processing a template argument list. */
5621 my_friendly_abort (981018);
5624 /* This can happen during the attempted tsubst'ing in
5625 unify. This means that we don't yet have any information
5626 about the template parameter in question. */
5629 /* If we get here, we must have been looking at a parm for a
5630 more deeply nested template. Make a new version of this
5631 template parameter, but with a lower level. */
5632 switch (TREE_CODE (t
))
5634 case TEMPLATE_TYPE_PARM
:
5635 case TEMPLATE_TEMPLATE_PARM
:
5637 TEMPLATE_TYPE_PARM_INDEX (r
)
5638 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
5640 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
5641 TYPE_MAIN_VARIANT (r
) = r
;
5642 TYPE_POINTER_TO (r
) = NULL_TREE
;
5643 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
5645 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
5646 && CLASSTYPE_TEMPLATE_INFO (t
))
5648 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
5649 CLASSTYPE_TEMPLATE_INFO (r
)
5650 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
5654 case TEMPLATE_PARM_INDEX
:
5655 r
= reduce_template_parm_level (t
, type
, levels
);
5659 my_friendly_abort (0);
5667 tree purpose
, value
, chain
, result
;
5668 int via_public
, via_virtual
, via_protected
;
5670 if (t
== void_list_node
)
5673 via_public
= TREE_VIA_PUBLIC (t
);
5674 via_protected
= TREE_VIA_PROTECTED (t
);
5675 via_virtual
= TREE_VIA_VIRTUAL (t
);
5677 purpose
= TREE_PURPOSE (t
);
5679 purpose
= tsubst (purpose
, args
, in_decl
);
5680 value
= TREE_VALUE (t
);
5682 value
= tsubst (value
, args
, in_decl
);
5683 chain
= TREE_CHAIN (t
);
5684 if (chain
&& chain
!= void_type_node
)
5685 chain
= tsubst (chain
, args
, in_decl
);
5686 if (purpose
== TREE_PURPOSE (t
)
5687 && value
== TREE_VALUE (t
)
5688 && chain
== TREE_CHAIN (t
))
5690 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
5691 purpose
, value
, chain
);
5692 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
5696 if (type
!= NULL_TREE
)
5698 /* A binfo node. We always need to make a copy, of the node
5699 itself and of its BINFO_BASETYPES. */
5703 /* Make sure type isn't a typedef copy. */
5704 type
= BINFO_TYPE (TYPE_BINFO (type
));
5706 TREE_TYPE (t
) = complete_type (type
);
5707 if (IS_AGGR_TYPE (type
))
5709 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
5710 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
5711 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
5712 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
5717 /* Otherwise, a vector of template arguments. */
5718 return tsubst_template_arg_vector (t
, args
);
5721 case REFERENCE_TYPE
:
5724 enum tree_code code
;
5726 if (type
== TREE_TYPE (t
))
5729 code
= TREE_CODE (t
);
5730 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5732 static int last_line
= 0;
5733 static char* last_file
= 0;
5735 /* We keep track of the last time we issued this error
5736 message to avoid spewing a ton of messages during a
5737 single bad template instantiation. */
5738 if (last_line
!= lineno
||
5739 last_file
!= input_filename
)
5741 cp_error ("cannot form type %s to reference type %T during template instantiation",
5742 (code
== POINTER_TYPE
) ? "pointer" : "reference",
5745 last_file
= input_filename
;
5748 /* Use the underlying type in an attempt at error
5749 recovery; maybe the user meant vector<int> and wrote
5750 vector<int&>, or some such. */
5751 if (code
== REFERENCE_TYPE
)
5754 r
= build_pointer_type (TREE_TYPE (type
));
5756 else if (code
== POINTER_TYPE
)
5757 r
= build_pointer_type (type
);
5759 r
= build_reference_type (type
);
5760 r
= cp_build_qualified_type (r
, TYPE_QUALS (t
));
5762 /* Will this ever be needed for TYPE_..._TO values? */
5767 return build_offset_type
5768 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, in_decl
), type
);
5776 /* The TYPE_CONTEXT is not used for function/method types. */
5777 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
5779 /* Substitue the argument types. */
5780 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, in_decl
);
5782 /* Construct a new type node and return it. */
5783 if (TREE_CODE (t
) == FUNCTION_TYPE
)
5784 fntype
= build_function_type (type
, arg_types
);
5787 = build_cplus_method_type (TREE_TYPE (TREE_VALUE (arg_types
)),
5789 TREE_CHAIN (arg_types
));
5790 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
5792 /* Substitue the exception specification. */
5793 raises
= TYPE_RAISES_EXCEPTIONS (t
);
5796 raises
= tsubst (raises
, args
, in_decl
);
5797 fntype
= build_exception_variant (fntype
, raises
);
5803 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, in_decl
);
5805 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
5807 r
= build_cplus_array_type (type
, domain
);
5813 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
5814 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5815 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
)));
5819 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
5820 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
)));
5824 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, in_decl
,
5825 /*entering_scope=*/1);
5826 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
, in_decl
);
5828 /* Normally, make_typename_type does not require that the CTX
5829 have complete type in order to allow things like:
5831 template <class T> struct S { typename S<T>::X Y; };
5833 But, such constructs have already been resolved by this
5834 point, so here CTX really should have complete type, unless
5835 it's a partial instantiation. */
5836 if (!uses_template_parms (ctx
)
5837 && !complete_type_or_else (ctx
))
5838 return error_mark_node
;
5840 f
= make_typename_type (ctx
, f
);
5841 return cp_build_qualified_type (f
,
5843 | CP_TYPE_QUALS (t
));
5847 return make_pointer_declarator
5848 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
5851 return make_reference_declarator
5852 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
5855 return build_parse_node
5856 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5857 tsubst_expr (TREE_OPERAND (t
, 1), args
, in_decl
));
5860 return make_call_declarator
5861 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5862 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
5863 TREE_OPERAND (t
, 2),
5864 tsubst (TREE_TYPE (t
), args
, in_decl
));
5867 return build_parse_node
5868 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5869 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
));
5872 return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t
), args
, in_decl
));
5875 sorry ("use of `%s' in template",
5876 tree_code_name
[(int) TREE_CODE (t
)]);
5877 return error_mark_node
;
5884 emit_line_note (input_filename
, lineno
);
5888 expand_start_bindings (0);
5895 int saved_warn_unused
= 0;
5897 if (processing_template_decl
)
5899 saved_warn_unused
= warn_unused
;
5902 expand_end_bindings (getdecls (), kept_level_p (), 0);
5903 if (processing_template_decl
)
5904 warn_unused
= saved_warn_unused
;
5905 t
= poplevel (kept_level_p (), 1, 0);
5910 /* Like tsubst, but deals with expressions. This function just replaces
5911 template parms; to finish processing the resultant expression, use
5915 tsubst_copy (t
, args
, in_decl
)
5919 enum tree_code code
;
5921 if (t
== NULL_TREE
|| t
== error_mark_node
)
5924 code
= TREE_CODE (t
);
5929 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
5936 if (!DECL_CONTEXT (t
))
5937 /* This is a global enumeration constant. */
5940 /* Unfortunately, we cannot just call lookup_name here.
5943 template <int I> int f() {
5945 struct S { void g() { E e = a; } };
5948 When we instantiate f<7>::S::g(), say, lookup_name is not
5949 clever enough to find f<7>::a. */
5951 = tsubst_aggr_type (TREE_TYPE (t
), args
, in_decl
,
5952 /*entering_scope=*/0);
5954 for (v
= TYPE_VALUES (enum_type
);
5957 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
5958 return TREE_VALUE (v
);
5960 /* We didn't find the name. That should never happen; if
5961 name-lookup found it during preliminary parsing, we
5962 should find it again here during instantiation. */
5963 my_friendly_abort (0);
5968 if (DECL_CONTEXT (t
))
5972 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
5973 /*entering_scope=*/1);
5974 if (ctx
!= DECL_CONTEXT (t
))
5975 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
5981 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
5982 t
= tsubst (t
, args
, in_decl
);
5987 if (is_member_template (t
))
5988 return tsubst (t
, args
, in_decl
);
5994 /* We must tsbust into a LOOKUP_EXPR in case the names to
5995 which it refers is a conversion operator; in that case the
5996 name will change. We avoid making unnecessary copies,
5999 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
6001 if (id
!= TREE_OPERAND (t
, 0))
6003 tree r
= build_nt (LOOKUP_EXPR
, id
);
6004 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6012 case REINTERPRET_CAST_EXPR
:
6013 case CONST_CAST_EXPR
:
6014 case STATIC_CAST_EXPR
:
6015 case DYNAMIC_CAST_EXPR
:
6018 (code
, tsubst (TREE_TYPE (t
), args
, in_decl
),
6019 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
6022 case PREDECREMENT_EXPR
:
6023 case PREINCREMENT_EXPR
:
6024 case POSTDECREMENT_EXPR
:
6025 case POSTINCREMENT_EXPR
:
6027 case TRUTH_NOT_EXPR
:
6030 case CONVERT_EXPR
: /* Unary + */
6038 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
6043 case TRUNC_DIV_EXPR
:
6045 case FLOOR_DIV_EXPR
:
6046 case ROUND_DIV_EXPR
:
6047 case EXACT_DIV_EXPR
:
6049 case BIT_ANDTC_EXPR
:
6052 case TRUNC_MOD_EXPR
:
6053 case FLOOR_MOD_EXPR
:
6054 case TRUTH_ANDIF_EXPR
:
6055 case TRUTH_ORIF_EXPR
:
6056 case TRUTH_AND_EXPR
:
6077 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6078 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
6082 tree fn
= TREE_OPERAND (t
, 0);
6083 if (is_overloaded_fn (fn
))
6084 fn
= tsubst_copy (get_first_fn (fn
), args
, in_decl
);
6086 /* Sometimes FN is a LOOKUP_EXPR. */
6087 fn
= tsubst_copy (fn
, args
, in_decl
);
6089 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6093 case METHOD_CALL_EXPR
:
6095 tree name
= TREE_OPERAND (t
, 0);
6096 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
6098 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6099 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6101 else if (TREE_CODE (name
) == SCOPE_REF
6102 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
6104 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6105 name
= TREE_OPERAND (name
, 1);
6106 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6107 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6108 name
= build_nt (SCOPE_REF
, base
, name
);
6111 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
6113 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6114 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
),
6123 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6124 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6125 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
6127 if (code
== BIND_EXPR
&& !processing_template_decl
)
6129 /* This processing should really occur in tsubst_expr,
6130 However, tsubst_expr does not recurse into expressions,
6131 since it assumes that there aren't any statements
6132 inside them. Instead, it simply calls
6133 build_expr_from_tree. So, we need to expand the
6135 tree rtl_expr
= begin_stmt_expr ();
6136 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
, in_decl
);
6137 r
= finish_stmt_expr (rtl_expr
, block
);
6146 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6147 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6148 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
6149 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
6156 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6157 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
6158 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
6159 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
6163 case TEMPLATE_ID_EXPR
:
6165 /* Substituted template arguments */
6166 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
);
6168 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
6169 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
6171 return lookup_template_function
6172 (tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
), targs
);
6177 tree purpose
, value
, chain
;
6179 if (t
== void_list_node
)
6182 purpose
= TREE_PURPOSE (t
);
6184 purpose
= tsubst_copy (purpose
, args
, in_decl
);
6185 value
= TREE_VALUE (t
);
6187 value
= tsubst_copy (value
, args
, in_decl
);
6188 chain
= TREE_CHAIN (t
);
6189 if (chain
&& chain
!= void_type_node
)
6190 chain
= tsubst_copy (chain
, args
, in_decl
);
6191 if (purpose
== TREE_PURPOSE (t
)
6192 && value
== TREE_VALUE (t
)
6193 && chain
== TREE_CHAIN (t
))
6195 return tree_cons (purpose
, value
, chain
);
6202 case TEMPLATE_TYPE_PARM
:
6203 case TEMPLATE_TEMPLATE_PARM
:
6204 case TEMPLATE_PARM_INDEX
:
6206 case REFERENCE_TYPE
:
6213 return tsubst (t
, args
, in_decl
);
6215 case IDENTIFIER_NODE
:
6216 if (IDENTIFIER_TYPENAME_P (t
)
6217 /* Make sure it's not just a variable named `__opr', for instance,
6218 which can occur in some existing code. */
6220 return build_typename_overload
6221 (tsubst (TREE_TYPE (t
), args
, in_decl
));
6227 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, in_decl
), NULL_TREE
,
6228 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, in_decl
));
6235 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6238 tsubst_expr (t
, args
, in_decl
)
6242 if (t
== NULL_TREE
|| t
== error_mark_node
)
6245 if (processing_template_decl
)
6246 return tsubst_copy (t
, args
, in_decl
);
6248 switch (TREE_CODE (t
))
6251 lineno
= TREE_COMPLEXITY (t
);
6252 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
6257 lineno
= TREE_COMPLEXITY (t
);
6258 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
6264 int i
= suspend_momentary ();
6267 lineno
= TREE_COMPLEXITY (t
);
6268 emit_line_note (input_filename
, lineno
);
6270 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6271 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
6272 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
6273 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, in_decl
);
6275 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
6276 resume_momentary (i
);
6283 lineno
= TREE_COMPLEXITY (t
);
6286 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6287 tsubst_expr (tmp
, args
, in_decl
);
6288 finish_for_init_stmt (NULL_TREE
);
6289 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
6292 tmp
= tsubst_expr (FOR_EXPR (t
), args
, in_decl
);
6293 finish_for_expr (tmp
, NULL_TREE
);
6294 tsubst_expr (FOR_BODY (t
), args
, in_decl
);
6295 finish_for_stmt (tmp
, NULL_TREE
);
6301 lineno
= TREE_COMPLEXITY (t
);
6302 begin_while_stmt ();
6303 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
6306 tsubst_expr (WHILE_BODY (t
), args
, in_decl
);
6307 finish_while_stmt (NULL_TREE
);
6313 lineno
= TREE_COMPLEXITY (t
);
6315 tsubst_expr (DO_BODY (t
), args
, in_decl
);
6316 finish_do_body (NULL_TREE
);
6317 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
6327 lineno
= TREE_COMPLEXITY (t
);
6329 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
6333 if (tmp
= THEN_CLAUSE (t
), tmp
)
6335 tsubst_expr (tmp
, args
, in_decl
);
6336 finish_then_clause (NULL_TREE
);
6339 if (tmp
= ELSE_CLAUSE (t
), tmp
)
6341 begin_else_clause ();
6342 tsubst_expr (tmp
, args
, in_decl
);
6343 finish_else_clause (NULL_TREE
);
6354 lineno
= TREE_COMPLEXITY (t
);
6355 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
6356 for (substmt
= COMPOUND_BODY (t
);
6357 substmt
!= NULL_TREE
;
6358 substmt
= TREE_CHAIN (substmt
))
6359 tsubst_expr (substmt
, args
, in_decl
);
6360 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
6366 lineno
= TREE_COMPLEXITY (t
);
6367 finish_break_stmt ();
6371 lineno
= TREE_COMPLEXITY (t
);
6372 finish_continue_stmt ();
6379 lineno
= TREE_COMPLEXITY (t
);
6380 begin_switch_stmt ();
6381 val
= tsubst_expr (SWITCH_COND (t
), args
, in_decl
);
6382 finish_switch_cond (val
);
6384 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
6385 tsubst_expr (tmp
, args
, in_decl
);
6387 finish_switch_stmt (val
, NULL_TREE
);
6392 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, in_decl
),
6393 tsubst_expr (CASE_HIGH (t
), args
, in_decl
));
6397 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
6404 lineno
= TREE_COMPLEXITY (t
);
6405 t
= GOTO_DESTINATION (t
);
6406 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
6407 /* Computed goto's must be tsubst'd into. On the other hand,
6408 non-computed gotos must not be; the identifier in question
6409 will have no binding. */
6410 t
= tsubst_expr (t
, args
, in_decl
);
6411 finish_goto_stmt (t
);
6415 lineno
= TREE_COMPLEXITY (t
);
6416 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, in_decl
),
6417 tsubst_expr (ASM_STRING (t
), args
, in_decl
),
6418 tsubst_expr (ASM_OUTPUTS (t
), args
, in_decl
),
6419 tsubst_expr (ASM_INPUTS (t
), args
, in_decl
),
6420 tsubst_expr (ASM_CLOBBERS (t
), args
, in_decl
));
6424 lineno
= TREE_COMPLEXITY (t
);
6426 tsubst_expr (TRY_STMTS (t
), args
, in_decl
);
6427 finish_try_block (NULL_TREE
);
6429 tree handler
= TRY_HANDLERS (t
);
6430 for (; handler
; handler
= TREE_CHAIN (handler
))
6431 tsubst_expr (handler
, args
, in_decl
);
6433 finish_handler_sequence (NULL_TREE
);
6437 lineno
= TREE_COMPLEXITY (t
);
6439 if (HANDLER_PARMS (t
))
6441 tree d
= HANDLER_PARMS (t
);
6442 expand_start_catch_block
6443 (tsubst (TREE_OPERAND (d
, 1), args
, in_decl
),
6444 tsubst (TREE_OPERAND (d
, 0), args
, in_decl
));
6447 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
6448 finish_handler_parms (NULL_TREE
);
6449 tsubst_expr (HANDLER_BODY (t
), args
, in_decl
);
6450 finish_handler (NULL_TREE
);
6454 lineno
= TREE_COMPLEXITY (t
);
6456 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
6457 tsubst (t
, args
, NULL_TREE
);
6461 return build_expr_from_tree (tsubst_copy (t
, args
, in_decl
));
6466 /* Instantiate the indicated variable of function template TMPL with
6467 the template arguments in TARG_PTR. */
6470 instantiate_template (tmpl
, targ_ptr
)
6471 tree tmpl
, targ_ptr
;
6477 struct obstack
*old_fmp_obstack
;
6478 extern struct obstack
*function_maybepermanent_obstack
;
6481 if (tmpl
== error_mark_node
)
6482 return error_mark_node
;
6484 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
6486 /* Check to see if we already have this specialization. */
6487 spec
= retrieve_specialization (tmpl
, targ_ptr
);
6488 if (spec
!= NULL_TREE
)
6491 if (DECL_TEMPLATE_INFO (tmpl
))
6493 /* The TMPL is a partial instantiation. To get a full set of
6494 arguments we must add the arguments used to perform the
6495 partial instantiation. */
6496 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
6498 gen_tmpl
= most_general_template (tmpl
);
6500 /* Check to see if we already have this specialization. */
6501 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
6502 if (spec
!= NULL_TREE
)
6508 push_obstacks (&permanent_obstack
, &permanent_obstack
);
6509 old_fmp_obstack
= function_maybepermanent_obstack
;
6510 function_maybepermanent_obstack
= &permanent_obstack
;
6512 len
= DECL_NTPARMS (gen_tmpl
);
6513 inner_args
= innermost_args (targ_ptr
);
6517 tree t
= TREE_VEC_ELT (inner_args
, i
);
6518 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
6520 tree nt
= target_type (t
);
6521 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
6523 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
6524 cp_error (" trying to instantiate `%D'", gen_tmpl
);
6525 fndecl
= error_mark_node
;
6530 targ_ptr
= copy_to_permanent (targ_ptr
);
6532 /* substitute template parameters */
6533 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, gen_tmpl
);
6534 /* The DECL_TI_TEMPLATE should always be the immediate parent
6535 template, not the most general template. */
6536 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
6538 if (flag_external_templates
)
6539 add_pending_template (fndecl
);
6542 function_maybepermanent_obstack
= old_fmp_obstack
;
6548 /* Push the name of the class template into the scope of the instantiation. */
6551 overload_template_name (type
)
6554 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
6557 if (IDENTIFIER_CLASS_VALUE (id
)
6558 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
6561 decl
= build_decl (TYPE_DECL
, id
, type
);
6562 SET_DECL_ARTIFICIAL (decl
);
6563 pushdecl_class_level (decl
);
6566 /* Like type_unification but designed specially to handle conversion
6569 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6570 arguments that are being used when calling it.
6572 If FN is a conversion operator, RETURN_TYPE is the type desired as
6573 the result of the conversion operator.
6575 The EXTRA_FN_ARG, if any, is the type of an additional
6576 parameter to be added to the beginning of FN's parameter list.
6578 The other arguments are as for type_unification. */
6581 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
6582 strict
, extra_fn_arg
)
6583 tree fn
, explicit_targs
, targs
, args
, return_type
;
6584 unification_kind_t strict
;
6589 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
6591 parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6593 if (DECL_CONV_FN_P (fn
))
6595 /* This is a template conversion operator. Use the return types
6596 as well as the argument types. */
6597 parms
= scratch_tree_cons (NULL_TREE
,
6598 TREE_TYPE (TREE_TYPE (fn
)),
6600 args
= scratch_tree_cons (NULL_TREE
, return_type
, args
);
6603 if (extra_fn_arg
!= NULL_TREE
)
6604 parms
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
, parms
);
6606 /* We allow incomplete unification without an error message here
6607 because the standard doesn't seem to explicitly prohibit it. Our
6608 callers must be ready to deal with unification failures in any
6610 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
6619 /* Type unification.
6621 We have a function template signature with one or more references to
6622 template parameters, and a parameter list we wish to fit to this
6623 template. If possible, produce a list of parameters for the template
6624 which will cause it to fit the supplied parameter list.
6626 Return zero for success, 2 for an incomplete match that doesn't resolve
6627 all the types, and 1 for complete failure. An error message will be
6628 printed only for an incomplete match.
6630 TPARMS[NTPARMS] is an array of template parameter types.
6632 TARGS[NTPARMS] is the array into which the deduced template
6633 parameter values are placed. PARMS is the function template's
6634 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6635 list we're trying to match against it.
6637 The EXPLICIT_TARGS are explicit template arguments provided via a
6640 The parameter STRICT is one of:
6643 We are deducing arguments for a function call, as in
6647 We are deducing arguments for a conversion function, as in
6651 We are deducing arguments when calculating the partial
6652 ordering between specializations of function or class
6653 templates, as in [temp.func.order] and [temp.class.order],
6654 when doing an explicit instantiation as in [temp.explicit],
6655 when determining an explicit specialization as in
6656 [temp.expl.spec], or when taking the address of a function
6657 template, as in [temp.deduct.funcaddr]. */
6660 type_unification (tparms
, targs
, parms
, args
, explicit_targs
,
6661 strict
, allow_incomplete
)
6662 tree tparms
, targs
, parms
, args
, explicit_targs
;
6663 unification_kind_t strict
;
6664 int allow_incomplete
;
6669 for (i
= 0; i
< TREE_VEC_LENGTH (tparms
); i
++)
6670 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
6672 if (explicit_targs
!= NULL_TREE
)
6675 arg_vec
= coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, 0,
6678 if (arg_vec
== error_mark_node
)
6681 explicit_mask
= alloca (sizeof (int) * TREE_VEC_LENGTH (targs
));
6682 bzero ((char *) explicit_mask
, sizeof(int) * TREE_VEC_LENGTH (targs
));
6685 i
< TREE_VEC_LENGTH (arg_vec
)
6686 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
6689 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (arg_vec
, i
);
6690 /* Let unify know that this argument was explicit. */
6691 explicit_mask
[i
] = 1;
6698 type_unification_real (tparms
, targs
, parms
, args
, 0,
6699 strict
, allow_incomplete
, explicit_mask
);
6702 /* Adjust types before performing type deduction, as described in
6703 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
6704 sections are symmetric. PARM is the type of a function parameter
6705 or the return type of the conversion function. ARG is the type of
6706 the argument passed to the call, or the type of the value
6707 intialized with the result of the conversion function. */
6710 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
6711 unification_kind_t strict
;
6722 /* Swap PARM and ARG throughout the remainder of this
6723 function; the handling is precisely symmetric since PARM
6724 will initialize ARG rather than vice versa. */
6732 /* There is nothing to do in this case. */
6736 my_friendly_abort (0);
6739 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
6741 /* [temp.deduct.call]
6743 If P is not a reference type:
6745 --If A is an array type, the pointer type produced by the
6746 array-to-pointer standard conversion (_conv.array_) is
6747 used in place of A for type deduction; otherwise,
6749 --If A is a function type, the pointer type produced by
6750 the function-to-pointer standard conversion
6751 (_conv.func_) is used in place of A for type deduction;
6754 --If A is a cv-qualified type, the top level
6755 cv-qualifiers of A's type are ignored for type
6757 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
6758 *arg
= build_pointer_type (TREE_TYPE (*arg
));
6759 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
6760 || TREE_CODE (*arg
) == METHOD_TYPE
)
6761 *arg
= build_pointer_type (*arg
);
6763 *arg
= TYPE_MAIN_VARIANT (*arg
);
6766 /* [temp.deduct.call]
6768 If P is a cv-qualified type, the top level cv-qualifiers
6769 of P's type are ignored for type deduction. If P is a
6770 reference type, the type referred to by P is used for
6772 *parm
= TYPE_MAIN_VARIANT (*parm
);
6773 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
6774 *parm
= TREE_TYPE (*parm
);
6777 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
6778 integers, with ones in positions corresponding to arguments in
6779 targs that were provided explicitly, and zeros elsewhere.
6781 If SUBR is 1, we're being called recursively (to unify the
6782 arguments of a function or method parameter of a function
6786 type_unification_real (tparms
, targs
, parms
, args
, subr
,
6787 strict
, allow_incomplete
, explicit_mask
)
6788 tree tparms
, targs
, parms
, args
;
6790 unification_kind_t strict
;
6791 int allow_incomplete
;
6796 int ntparms
= TREE_VEC_LENGTH (tparms
);
6799 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
6800 my_friendly_assert (parms
== NULL_TREE
6801 || TREE_CODE (parms
) == TREE_LIST
, 290);
6802 /* ARGS could be NULL (via a call from parse.y to
6803 build_x_function_call). */
6805 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
6806 my_friendly_assert (ntparms
> 0, 292);
6811 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
6815 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
6819 sub_strict
= UNIFY_ALLOW_NONE
;
6823 my_friendly_abort (0);
6827 && parms
!= void_list_node
6829 && args
!= void_list_node
)
6831 parm
= TREE_VALUE (parms
);
6832 parms
= TREE_CHAIN (parms
);
6833 arg
= TREE_VALUE (args
);
6834 args
= TREE_CHAIN (args
);
6836 if (arg
== error_mark_node
)
6838 if (arg
== unknown_type_node
)
6839 /* We can't deduce anything from this, but we might get all the
6840 template args from other function args. */
6843 /* Conversions will be performed on a function argument that
6844 corresponds with a function parameter that contains only
6845 non-deducible template parameters and explicitly specified
6846 template parameters. */
6847 if (! uses_template_parms (parm
))
6851 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
6852 type
= TREE_TYPE (arg
);
6859 if (strict
== DEDUCE_EXACT
)
6861 if (same_type_p (parm
, type
))
6865 /* It might work; we shouldn't check now, because we might
6866 get into infinite recursion. Overload resolution will
6874 if (TREE_CODE (arg
) == VAR_DECL
)
6875 arg
= TREE_TYPE (arg
);
6876 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
6877 arg
= TREE_TYPE (arg
);
6879 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
6881 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
6882 if (type_unknown_p (arg
))
6884 /* [temp.deduct.type] A template-argument can be deduced from
6885 a pointer to function or pointer to member function
6886 argument if the set of overloaded functions does not
6887 contain function templates and at most one of a set of
6888 overloaded functions provides a unique match. */
6890 if (resolve_overloaded_unification
6891 (tparms
, targs
, parm
, arg
, strict
, sub_strict
, explicit_mask
)
6896 arg
= TREE_TYPE (arg
);
6900 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
6902 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
,
6911 /* Fail if we've reached the end of the parm list, and more args
6912 are present, and the parm list isn't variadic. */
6913 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
6915 /* Fail if parms are left and they don't have default values. */
6917 && parms
!= void_list_node
6918 && TREE_PURPOSE (parms
) == NULL_TREE
)
6921 for (i
= 0; i
< ntparms
; i
++)
6922 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
6924 if (!allow_incomplete
)
6925 error ("incomplete type unification");
6931 /* Subroutine of type_unification_real. Args are like the variables at the
6932 call site. ARG is an overloaded function (or template-id); we try
6933 deducing template args from each of the overloads, and if only one
6934 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
6937 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
6938 sub_strict
, explicit_mask
)
6939 tree tparms
, targs
, parm
, arg
;
6940 unification_kind_t strict
;
6944 tree tempargs
= copy_node (targs
);
6947 if (TREE_CODE (arg
) == ADDR_EXPR
)
6948 arg
= TREE_OPERAND (arg
, 0);
6949 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
6951 /* If we got some explicit template args, we need to plug them into
6952 the affected templates before we try to unify, in case the
6953 explicit args will completely resolve the templates in question. */
6955 tree expl_subargs
= TREE_OPERAND (arg
, 1);
6956 arg
= TREE_OPERAND (arg
, 0);
6958 for (; arg
; arg
= OVL_NEXT (arg
))
6960 tree fn
= OVL_CURRENT (arg
);
6963 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
6966 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
6969 elem
= tsubst (TREE_TYPE (fn
), subargs
, NULL_TREE
);
6970 good
+= try_one_overload (tparms
, tempargs
, parm
, elem
,
6971 strict
, sub_strict
, explicit_mask
);
6975 else if (TREE_CODE (arg
) == OVERLOAD
)
6977 for (; arg
; arg
= OVL_NEXT (arg
))
6978 good
+= try_one_overload (tparms
, tempargs
, parm
,
6979 TREE_TYPE (OVL_CURRENT (arg
)),
6980 strict
, sub_strict
, explicit_mask
);
6983 my_friendly_abort (981006);
6985 /* [temp.deduct.type] A template-argument can be deduced from a pointer
6986 to function or pointer to member function argument if the set of
6987 overloaded functions does not contain function templates and at most
6988 one of a set of overloaded functions provides a unique match.
6990 So if we found multiple possibilities, we return success but don't
6995 int i
= TREE_VEC_LENGTH (targs
);
6997 if (TREE_VEC_ELT (tempargs
, i
))
6998 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
7006 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7007 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7008 different overloads deduce different arguments for a given parm.
7009 Returns 1 on success. */
7012 try_one_overload (tparms
, targs
, parm
, arg
, strict
,
7013 sub_strict
, explicit_mask
)
7014 tree tparms
, targs
, parm
, arg
;
7015 unification_kind_t strict
;
7023 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7024 to function or pointer to member function argument if the set of
7025 overloaded functions does not contain function templates and at most
7026 one of a set of overloaded functions provides a unique match.
7028 So if this is a template, just return success. */
7030 if (uses_template_parms (arg
))
7033 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7035 /* We don't copy orig_targs for this because if we have already deduced
7036 some template args from previous args, unify would complain when we
7037 try to deduce a template parameter for the same argument, even though
7038 there isn't really a conflict. */
7039 nargs
= TREE_VEC_LENGTH (targs
);
7040 tempargs
= make_scratch_vec (nargs
);
7042 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explicit_mask
) != 0)
7045 /* First make sure we didn't deduce anything that conflicts with
7046 previously deduced/specified args. */
7047 for (i
= nargs
; i
--; )
7049 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7050 tree oldelt
= TREE_VEC_ELT (targs
, i
);
7052 if (elt
== NULL_TREE
)
7054 else if (uses_template_parms (elt
))
7056 /* Since we're unifying against ourselves, we will fill in template
7057 args used in the function parm list with our own template parms.
7059 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
7062 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
7066 for (i
= nargs
; i
--; )
7068 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7071 TREE_VEC_ELT (targs
, i
) = elt
;
7077 /* Returns the level of DECL, which declares a template parameter. */
7080 template_decl_level (decl
)
7083 switch (TREE_CODE (decl
))
7087 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
7090 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
7093 my_friendly_abort (0);
7098 /* Decide whether ARG can be unified with PARM, considering only the
7099 cv-qualifiers of each type, given STRICT as documented for unify.
7100 Returns non-zero iff the unification is OK on that basis.*/
7103 check_cv_quals_for_unify (strict
, arg
, parm
)
7108 return !((!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
7109 && !at_least_as_qualified_p (arg
, parm
))
7110 || (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
7111 && (!at_least_as_qualified_p (parm
, arg
))));
7114 /* Takes parameters as for type_unification. Returns 0 if the
7115 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7116 bitwise or of the following flags:
7119 Require an exact match between PARM and ARG.
7120 UNIFY_ALLOW_MORE_CV_QUAL:
7121 Allow the deduced ARG to be more cv-qualified than ARG.
7122 UNIFY_ALLOW_LESS_CV_QUAL:
7123 Allow the deduced ARG to be less cv-qualified than ARG.
7124 UNIFY_ALLOW_DERIVED:
7125 Allow the deduced ARG to be a template base class of ARG,
7126 or a pointer to a template base class of the type pointed to by
7130 unify (tparms
, targs
, parm
, arg
, strict
, explicit_mask
)
7131 tree tparms
, targs
, parm
, arg
;
7139 /* I don't think this will do the right thing with respect to types.
7140 But the only case I've seen it in so far has been array bounds, where
7141 signedness is the only information lost, and I think that will be
7143 while (TREE_CODE (parm
) == NOP_EXPR
)
7144 parm
= TREE_OPERAND (parm
, 0);
7146 if (arg
== error_mark_node
)
7148 if (arg
== unknown_type_node
)
7149 /* We can't deduce anything from this, but we might get all the
7150 template args from other function args. */
7153 /* If PARM uses template parameters, then we can't bail out here,
7154 even in ARG == PARM, since we won't record unifications for the
7155 template parameters. We might need them if we're trying to
7156 figure out which of two things is more specialized. */
7157 if (arg
== parm
&& !uses_template_parms (parm
))
7160 /* Immediately reject some pairs that won't unify because of
7161 cv-qualification mismatches. */
7162 if (TREE_CODE (arg
) == TREE_CODE (parm
)
7163 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
7164 /* We check the cv-qualifiers when unifying with template type
7165 parameters below. We want to allow ARG `const T' to unify with
7166 PARM `T' for example, when computing which of two templates
7167 is more specialized, for example. */
7168 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
7169 && !check_cv_quals_for_unify (strict
, arg
, parm
))
7172 switch (TREE_CODE (parm
))
7175 /* In a type which contains a nested-name-specifier, template
7176 argument values cannot be deduced for template parameters used
7177 within the nested-name-specifier. */
7180 case TEMPLATE_TYPE_PARM
:
7181 case TEMPLATE_TEMPLATE_PARM
:
7182 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7184 if (TEMPLATE_TYPE_LEVEL (parm
)
7185 != template_decl_level (tparm
))
7186 /* The PARM is not one we're trying to unify. Just check
7187 to see if it matches ARG. */
7188 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7189 && same_type_p (parm
, arg
)) ? 0 : 1;
7190 idx
= TEMPLATE_TYPE_IDX (parm
);
7191 targ
= TREE_VEC_ELT (targs
, idx
);
7192 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
7194 /* Check for mixed types and values. */
7195 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
7196 && TREE_CODE (tparm
) != TYPE_DECL
)
7197 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
7198 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
7201 if (!strict
&& targ
!= NULL_TREE
7202 && explicit_mask
&& explicit_mask
[idx
])
7203 /* An explicit template argument. Don't even try to match
7204 here; the overload resolution code will manage check to
7205 see whether the call is legal. */
7208 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
7210 if (CLASSTYPE_TEMPLATE_INFO (parm
))
7212 /* We arrive here when PARM does not involve template
7215 /* ARG must be constructed from a template class. */
7216 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
7220 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
7221 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
7222 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
7224 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
7227 /* The parameter and argument roles have to be switched here
7228 in order to handle default arguments properly. For example,
7229 template<template <class> class TT> void f(TT<int>)
7230 should be able to accept vector<int> which comes from
7231 template <class T, class Allocator = allocator>
7234 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1)
7238 /* Deduce arguments T, i from TT<T> or TT<i>. */
7239 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
7241 tree t
= TREE_VEC_ELT (parmvec
, i
);
7242 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
7243 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
7244 && TREE_CODE (t
) != TEMPLATE_PARM_INDEX
)
7247 /* This argument can be deduced. */
7249 if (unify (tparms
, targs
, t
,
7250 TREE_VEC_ELT (argvec
, i
),
7251 UNIFY_ALLOW_NONE
, explicit_mask
))
7255 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
7260 /* If PARM is `const T' and ARG is only `int', we don't have
7261 a match unless we are allowing additional qualification.
7262 If ARG is `const int' and PARM is just `T' that's OK;
7263 that binds `const int' to `T'. */
7264 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
7268 /* Consider the case where ARG is `const volatile int' and
7269 PARM is `const T'. Then, T should be `volatile int'. */
7271 cp_build_qualified_type (arg
,
7273 & ~CP_TYPE_QUALS (parm
));
7276 /* Simple cases: Value already set, does match or doesn't. */
7277 if (targ
!= NULL_TREE
7278 && (same_type_p (targ
, arg
)
7279 || (explicit_mask
&& explicit_mask
[idx
])))
7283 TREE_VEC_ELT (targs
, idx
) = arg
;
7286 case TEMPLATE_PARM_INDEX
:
7287 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7289 if (TEMPLATE_PARM_LEVEL (parm
)
7290 != template_decl_level (tparm
))
7291 /* The PARM is not one we're trying to unify. Just check
7292 to see if it matches ARG. */
7293 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7294 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
7296 idx
= TEMPLATE_PARM_IDX (parm
);
7297 targ
= TREE_VEC_ELT (targs
, idx
);
7301 int i
= (cp_tree_equal (targ
, arg
) > 0);
7307 my_friendly_abort (42);
7310 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
7317 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
7318 return (unify (tparms
, targs
, parm
,
7319 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
,
7322 if (TREE_CODE (arg
) != POINTER_TYPE
)
7325 /* [temp.deduct.call]
7327 A can be another pointer or pointer to member type that can
7328 be converted to the deduced A via a qualification
7329 conversion (_conv.qual_).
7331 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
7332 This will allow for additional cv-qualification of the
7333 pointed-to types if appropriate. In general, this is a bit
7334 too generous; we are only supposed to allow qualification
7335 conversions and this method will allow an ARG of char** and
7336 a deduced ARG of const char**. However, overload
7337 resolution will subsequently invalidate the candidate, so
7338 this is probably OK. */
7339 sub_strict
= strict
;
7341 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
7342 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg
)))
7343 /* The derived-to-base conversion only persists through one
7344 level of pointers. */
7345 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
7347 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE
7348 (arg
), sub_strict
, explicit_mask
);
7351 case REFERENCE_TYPE
:
7352 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
7354 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7355 UNIFY_ALLOW_NONE
, explicit_mask
);
7358 if (TREE_CODE (arg
) != ARRAY_TYPE
)
7360 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
7361 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
7363 if (TYPE_DOMAIN (parm
) != NULL_TREE
7364 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
7365 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
, explicit_mask
) != 0)
7367 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7368 UNIFY_ALLOW_NONE
, explicit_mask
);
7375 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7378 if (TREE_CODE (parm
) == INTEGER_TYPE
7379 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
7381 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
7382 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
7383 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7385 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
7386 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
7387 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7390 /* We use the TYPE_MAIN_VARIANT since we have already
7391 checked cv-qualification at the top of the
7393 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
7394 TYPE_MAIN_VARIANT (parm
)))
7397 /* As far as unification is concerned, this wins. Later checks
7398 will invalidate it if necessary. */
7401 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
7402 /* Type INTEGER_CST can come from ordinary constant template args. */
7404 while (TREE_CODE (arg
) == NOP_EXPR
)
7405 arg
= TREE_OPERAND (arg
, 0);
7407 if (TREE_CODE (arg
) != INTEGER_CST
)
7409 return !tree_int_cst_equal (parm
, arg
);
7414 if (TREE_CODE (arg
) != TREE_VEC
)
7416 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
7418 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
7419 if (unify (tparms
, targs
,
7420 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
7421 UNIFY_ALLOW_NONE
, explicit_mask
))
7427 if (TYPE_PTRMEMFUNC_FLAG (parm
))
7428 return unify (tparms
, targs
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
7429 arg
, strict
, explicit_mask
);
7431 if (TREE_CODE (arg
) != RECORD_TYPE
)
7434 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
7437 if (strict
& UNIFY_ALLOW_DERIVED
)
7438 /* [temp.deduct.call]
7440 If P is a class, and P has the form template-id, then A
7441 can be a derived class of the deduced A. Likewise, if
7442 P is a pointer to a class of the form template-id, A
7443 can be a pointer to a derived class pointed to by the
7446 The call to get_template_base also handles the case
7447 where PARM and ARG are the same type, i.e., where no
7448 derivation is involved. */
7449 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
7450 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
7451 && (CLASSTYPE_TI_TEMPLATE (parm
)
7452 == CLASSTYPE_TI_TEMPLATE (arg
)))
7453 /* Perhaps PARM is something like S<U> and ARG is S<int>.
7454 Then, we should unify `int' and `U'. */
7457 if (! t
|| t
== error_mark_node
)
7460 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
7461 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
,
7464 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
7465 TYPE_MAIN_VARIANT (arg
)))
7471 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7474 if (unify (tparms
, targs
, TREE_TYPE (parm
),
7475 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7477 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
7478 TYPE_ARG_TYPES (arg
), 1,
7479 DEDUCE_EXACT
, 0, explicit_mask
);
7482 if (TREE_CODE (arg
) != OFFSET_TYPE
)
7484 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
7485 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7487 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7488 UNIFY_ALLOW_NONE
, explicit_mask
);
7491 if (arg
!= decl_constant_value (parm
))
7496 /* Matched cases are handled by the ARG == PARM test above. */
7500 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
7502 /* We handle this case specially, since it comes up with
7503 arrays. In particular, something like:
7505 template <int N> void f(int (&x)[N]);
7507 Here, we are trying to unify the range type, which
7508 looks like [0 ... (N - 1)]. */
7510 t1
= TREE_OPERAND (parm
, 0);
7511 t2
= TREE_OPERAND (parm
, 1);
7513 /* Should this be a regular fold? */
7514 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
7518 return unify (tparms
, targs
, t1
, t
, UNIFY_ALLOW_NONE
,
7521 /* else fall through */
7524 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
7526 /* We're looking at an expression. This can happen with
7530 void foo(S<I>, S<I + 2>);
7532 If the call looked like:
7534 foo(S<2>(), S<4>());
7536 we would have already matched `I' with `2'. Now, we'd
7537 like to know if `4' matches `I + 2'. So, we substitute
7538 into that expression, and fold constants, in the hope of
7541 maybe_fold_nontype_arg (tsubst_expr (parm
, targs
, NULL_TREE
));
7542 tree a
= maybe_fold_nontype_arg (arg
);
7544 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t
))))
7545 /* Good, we mangaged to simplify the exression. */
7546 return unify (tparms
, targs
, t
, a
, UNIFY_ALLOW_NONE
,
7549 /* Bad, we couldn't simplify this. Assume it doesn't
7554 sorry ("use of `%s' in template type unification",
7555 tree_code_name
[(int) TREE_CODE (parm
)]);
7561 /* Called if RESULT is explicitly instantiated, or is a member of an
7562 explicitly instantiated class, or if using -frepo and the
7563 instantiation of RESULT has been assigned to this file. */
7566 mark_decl_instantiated (result
, extern_p
)
7570 if (TREE_CODE (result
) != FUNCTION_DECL
)
7571 /* The TREE_PUBLIC flag for function declarations will have been
7572 set correctly by tsubst. */
7573 TREE_PUBLIC (result
) = 1;
7577 DECL_INTERFACE_KNOWN (result
) = 1;
7578 DECL_NOT_REALLY_EXTERN (result
) = 1;
7580 /* Always make artificials weak. */
7581 if (DECL_ARTIFICIAL (result
) && flag_weak
)
7582 comdat_linkage (result
);
7583 /* For WIN32 we also want to put explicit instantiations in
7584 linkonce sections. */
7585 else if (TREE_PUBLIC (result
))
7586 maybe_make_one_only (result
);
7588 else if (TREE_CODE (result
) == FUNCTION_DECL
)
7589 mark_inline_for_output (result
);
7592 /* Given two function templates PAT1 and PAT2, and explicit template
7593 arguments EXPLICIT_ARGS return:
7595 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
7596 -1 if PAT2 is more specialized than PAT1.
7597 0 if neither is more specialized. */
7600 more_specialized (pat1
, pat2
, explicit_args
)
7601 tree pat1
, pat2
, explicit_args
;
7606 targs
= get_bindings_overload (pat1
, pat2
, explicit_args
);
7610 targs
= get_bindings_overload (pat2
, pat1
, explicit_args
);
7617 /* Given two class template specialization list nodes PAT1 and PAT2, return:
7619 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
7620 -1 if PAT2 is more specialized than PAT1.
7621 0 if neither is more specialized. */
7624 more_specialized_class (pat1
, pat2
)
7630 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
7631 TREE_PURPOSE (pat2
));
7635 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
7636 TREE_PURPOSE (pat1
));
7643 /* Return the template arguments that will produce the function signature
7644 DECL from the function template FN, with the explicit template
7645 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
7649 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
7650 tree fn
, decl
, explicit_args
;
7653 int ntparms
= DECL_NTPARMS (fn
);
7654 tree targs
= make_scratch_vec (ntparms
);
7655 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
7656 tree extra_fn_arg
= NULL_TREE
;
7659 if (DECL_STATIC_FUNCTION_P (fn
)
7660 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
7662 /* Sometimes we are trying to figure out what's being
7663 specialized by a declaration that looks like a method, and it
7664 turns out to be a static member function. */
7665 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
7666 && !is_member_template (fn
))
7667 /* The natural thing to do here seems to be to remove the
7668 spurious `this' parameter from the DECL, but that prevents
7669 unification from making use of the class type. So,
7670 instead, we have fn_type_unification add to the parameters
7672 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
7674 /* In this case, though, adding the extra_fn_arg can confuse
7675 things, so we remove from decl_arg_types instead. */
7676 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
7679 i
= fn_type_unification (fn
, explicit_args
, targs
,
7681 TREE_TYPE (TREE_TYPE (decl
)),
7690 /* Check to see that the resulting return type is also OK. */
7691 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
7694 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
7701 /* For most uses, we want to check the return type. */
7704 get_bindings (fn
, decl
, explicit_args
)
7705 tree fn
, decl
, explicit_args
;
7707 return get_bindings_real (fn
, decl
, explicit_args
, 1);
7710 /* But for more_specialized, we only care about the parameter types. */
7713 get_bindings_overload (fn
, decl
, explicit_args
)
7714 tree fn
, decl
, explicit_args
;
7716 return get_bindings_real (fn
, decl
, explicit_args
, 0);
7719 /* Return the innermost template arguments that, when applied to a
7720 template specialization whose innermost template parameters are
7721 TPARMS, and whose specialization arguments are ARGS, yield the
7724 For example, suppose we have:
7726 template <class T, class U> struct S {};
7727 template <class T> struct S<T*, int> {};
7729 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
7730 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
7731 int}. The resulting vector will be {double}, indicating that `T'
7732 is bound to `double'. */
7735 get_class_bindings (tparms
, parms
, args
)
7736 tree tparms
, parms
, args
;
7738 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
7739 tree vec
= make_temp_vec (ntparms
);
7741 args
= innermost_args (args
);
7743 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7745 switch (unify (tparms
, vec
,
7746 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
7747 UNIFY_ALLOW_NONE
, 0))
7756 for (i
= 0; i
< ntparms
; ++i
)
7757 if (! TREE_VEC_ELT (vec
, i
))
7763 /* Return the most specialized of the list of templates in FNS that can
7764 produce an instantiation matching DECL, given the explicit template
7765 arguments EXPLICIT_ARGS. */
7768 most_specialized (fns
, decl
, explicit_args
)
7769 tree fns
, decl
, explicit_args
;
7771 tree candidates
= NULL_TREE
;
7772 tree fn
, champ
, args
;
7775 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
7777 tree candidate
= TREE_VALUE (fn
);
7779 args
= get_bindings (candidate
, decl
, explicit_args
);
7781 candidates
= scratch_tree_cons (NULL_TREE
, candidate
,
7788 champ
= TREE_VALUE (candidates
);
7789 for (fn
= TREE_CHAIN (candidates
); fn
; fn
= TREE_CHAIN (fn
))
7791 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
7798 fn
= TREE_CHAIN (fn
);
7800 return error_mark_node
;
7802 champ
= TREE_VALUE (fn
);
7806 for (fn
= candidates
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
7808 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
7810 return error_mark_node
;
7816 /* If DECL is a specialization of some template, return the most
7817 general such template. For example, given:
7819 template <class T> struct S { template <class U> void f(U); };
7821 if TMPL is `template <class U> void S<int>::f(U)' this will return
7822 the full template. This function will not trace past partial
7823 specializations, however. For example, given in addition:
7825 template <class T> struct S<T*> { template <class U> void f(U); };
7827 if TMPL is `template <class U> void S<int*>::f(U)' this will return
7828 `template <class T> template <class U> S<T*>::f(U)'. */
7831 most_general_template (decl
)
7834 while (DECL_TEMPLATE_INFO (decl
))
7835 decl
= DECL_TI_TEMPLATE (decl
);
7840 /* Return the most specialized of the class template specializations
7841 of TMPL which can produce an instantiation matching ARGS, or
7842 error_mark_node if the choice is ambiguous. */
7845 most_specialized_class (tmpl
, args
)
7849 tree list
= NULL_TREE
;
7854 tmpl
= most_general_template (tmpl
);
7855 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
7858 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
7861 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
7862 TREE_TYPE (list
) = TREE_TYPE (t
);
7872 for (; t
; t
= TREE_CHAIN (t
))
7874 fate
= more_specialized_class (champ
, t
);
7883 return error_mark_node
;
7889 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
7891 fate
= more_specialized_class (champ
, t
);
7893 return error_mark_node
;
7899 /* called from the parser. */
7902 do_decl_instantiation (declspecs
, declarator
, storage
)
7903 tree declspecs
, declarator
, storage
;
7905 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
7906 tree result
= NULL_TREE
;
7909 if (! DECL_LANG_SPECIFIC (decl
))
7911 cp_error ("explicit instantiation of non-template `%#D'", decl
);
7914 else if (TREE_CODE (decl
) == VAR_DECL
)
7916 /* There is an asymmetry here in the way VAR_DECLs and
7917 FUNCTION_DECLs are handled by grokdeclarator. In the case of
7918 the latter, the DECL we get back will be marked as a
7919 template instantiation, and the appropriate
7920 DECL_TEMPLATE_INFO will be set up. This does not happen for
7921 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
7922 should handle VAR_DECLs as it currently handles
7924 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
7925 if (result
&& TREE_CODE (result
) != VAR_DECL
)
7927 cp_error ("no matching template for `%D' found", result
);
7931 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
7933 cp_error ("explicit instantiation of `%#D'", decl
);
7939 /* Check for various error cases. Note that if the explicit
7940 instantiation is legal the RESULT will currently be marked as an
7941 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
7942 until we get here. */
7944 if (DECL_TEMPLATE_SPECIALIZATION (result
))
7948 No program shall both explicitly instantiate and explicitly
7949 specialize a template. */
7950 cp_error ("explicit instantiation of `%#D' after", result
);
7951 cp_error_at ("explicit specialization here", result
);
7954 else if (DECL_EXPLICIT_INSTANTIATION (result
))
7958 No program shall explicitly instantiate any template more
7961 We check DECL_INTERFACE_KNOWN so as not to complain when the
7962 first instantiation was `extern' and the second is not, and
7963 EXTERN_P for the opposite case. */
7964 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
)
7965 cp_error ("duplicate explicit instantiation of `%#D'", result
);
7967 /* If we've already instantiated the template, just return now. */
7968 if (DECL_INTERFACE_KNOWN (result
))
7971 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
7973 cp_error ("no matching template for `%D' found", result
);
7976 else if (!DECL_TEMPLATE_INFO (result
))
7978 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
7982 if (flag_external_templates
)
7985 if (storage
== NULL_TREE
)
7987 else if (storage
== ridpointers
[(int) RID_EXTERN
])
7990 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
7994 cp_error ("storage class `%D' applied to template instantiation",
7997 SET_DECL_EXPLICIT_INSTANTIATION (result
);
7998 mark_decl_instantiated (result
, extern_p
);
7999 repo_template_instantiated (result
, extern_p
);
8001 instantiate_decl (result
);
8005 mark_class_instantiated (t
, extern_p
)
8009 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
8010 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
8011 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
8012 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
8013 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
8016 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
8017 rest_of_type_compilation (t
, 1);
8022 do_type_instantiation (t
, storage
)
8029 if (TREE_CODE (t
) == TYPE_DECL
)
8032 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
8034 cp_error ("explicit instantiation of non-template type `%T'", t
);
8040 /* With -fexternal-templates, explicit instantiations are treated the same
8041 as implicit ones. */
8042 if (flag_external_templates
)
8045 if (TYPE_SIZE (t
) == NULL_TREE
)
8047 cp_error ("explicit instantiation of `%#T' before definition of template",
8052 if (storage
!= NULL_TREE
)
8055 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8056 IDENTIFIER_POINTER (storage
));
8058 if (storage
== ridpointers
[(int) RID_INLINE
])
8060 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8062 else if (storage
== ridpointers
[(int) RID_STATIC
])
8066 cp_error ("storage class `%D' applied to template instantiation",
8072 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
8076 No program shall both explicitly instantiate and explicitly
8077 specialize a template. */
8078 cp_error ("explicit instantiation of `%#T' after", t
);
8079 cp_error_at ("explicit specialization here", t
);
8082 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
8086 No program shall explicitly instantiate any template more
8089 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8090 instantiation was `extern', and if EXTERN_P then the second
8091 is. Both cases are OK. */
8092 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
)
8093 cp_error ("duplicate explicit instantiation of `%#T'", t
);
8095 /* If we've already instantiated the template, just return now. */
8096 if (!CLASSTYPE_INTERFACE_ONLY (t
))
8100 mark_class_instantiated (t
, extern_p
);
8101 repo_template_instantiated (t
, extern_p
);
8109 /* In contrast to implicit instantiation, where only the
8110 declarations, and not the definitions, of members are
8111 instantiated, we have here:
8115 The explicit instantiation of a class template specialization
8116 implies the instantiation of all of its members not
8117 previously explicitly specialized in the translation unit
8118 containing the explicit instantiation.
8120 Of course, we can't instantiate member template classes, since
8121 we don't have any arguments for them. Note that the standard
8122 is unclear on whether the instatiation of the members are
8123 *explicit* instantiations or not. We choose to be generous,
8124 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8125 the explicit instantiation of a class where some of the members
8126 have no definition in the current translation unit. */
8129 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8130 if (TREE_CODE (tmp
) == FUNCTION_DECL
8131 && DECL_TEMPLATE_INSTANTIATION (tmp
))
8133 mark_decl_instantiated (tmp
, extern_p
);
8134 repo_template_instantiated (tmp
, extern_p
);
8136 instantiate_decl (tmp
);
8139 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8140 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
8142 mark_decl_instantiated (tmp
, extern_p
);
8143 repo_template_instantiated (tmp
, extern_p
);
8145 instantiate_decl (tmp
);
8148 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8149 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
8150 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
8151 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
8155 /* Given a function DECL, which is a specialization of TMPL, modify
8156 DECL to be a re-instantiation of TMPL with the same template
8157 arguments. TMPL should be the template into which tsubst'ing
8158 should occur for DECL, not the most general template.
8160 One reason for doing this is a scenario like this:
8163 void f(const T&, int i);
8165 void g() { f(3, 7); }
8168 void f(const T& t, const int i) { }
8170 Note that when the template is first instantiated, with
8171 instantiate_template, the resulting DECL will have no name for the
8172 first parameter, and the wrong type for the second. So, when we go
8173 to instantiate the DECL, we regenerate it. */
8176 regenerate_decl_from_template (decl
, tmpl
)
8186 args
= DECL_TI_ARGS (decl
);
8187 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8189 /* Unregister the specialization so that when we tsubst we will not
8190 just return DECL. We don't have to unregister DECL from TMPL
8191 because if would only be registered there if it were a partial
8192 instantiation of a specialization, which it isn't: it's a full
8194 gen_tmpl
= most_general_template (tmpl
);
8195 unregistered
= unregister_specialization (decl
, gen_tmpl
);
8197 /* If the DECL was not unregistered then something peculiar is
8198 happening: we created a specialization but did not call
8199 register_specialization for it. */
8200 my_friendly_assert (unregistered
, 0);
8202 if (TREE_CODE (decl
) == VAR_DECL
)
8203 /* Make sure that we can see identifiers, and compute access
8204 correctly, for the class members used in the declaration of
8205 this static variable. */
8206 pushclass (DECL_CONTEXT (decl
), 2);
8208 /* Do the substitution to get the new declaration. */
8209 new_decl
= tsubst (code_pattern
, args
, NULL_TREE
);
8211 if (TREE_CODE (decl
) == VAR_DECL
)
8213 /* Set up DECL_INITIAL, since tsubst doesn't. */
8214 DECL_INITIAL (new_decl
) =
8215 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
8216 DECL_TI_TEMPLATE (decl
));
8217 /* Pop the class context we pushed above. */
8221 if (TREE_CODE (decl
) == FUNCTION_DECL
)
8222 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
8224 DECL_INITIAL (new_decl
) = error_mark_node
;
8226 /* The immediate parent of the new template is still whatever it was
8227 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
8228 general template. We also reset the DECL_ASSEMBLER_NAME since
8229 tsubst always calculates the name as if the function in question
8230 were really a template instance, and sometimes, with friend
8231 functions, this is not so. See tsubst_friend_function for
8233 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
8234 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
8235 DECL_RTL (new_decl
) = DECL_RTL (decl
);
8237 /* Call duplicate decls to merge the old and new declarations. */
8238 duplicate_decls (new_decl
, decl
);
8240 if (TREE_CODE (decl
) == FUNCTION_DECL
)
8241 DECL_INITIAL (new_decl
) = NULL_TREE
;
8243 /* Now, re-register the specialization. */
8244 register_specialization (decl
, gen_tmpl
, args
);
8247 /* Produce the definition of D, a _DECL generated from a template. */
8250 instantiate_decl (d
)
8253 tree tmpl
= DECL_TI_TEMPLATE (d
);
8254 tree args
= DECL_TI_ARGS (d
);
8259 int nested
= in_function_p ();
8260 int pattern_defined
;
8262 char *file
= input_filename
;
8264 /* This function should only be used to instantiate templates for
8265 functions and static member variables. */
8266 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
8267 || TREE_CODE (d
) == VAR_DECL
, 0);
8269 if ((TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INITIAL (d
))
8270 || (TREE_CODE (d
) == VAR_DECL
&& !DECL_IN_AGGR_P (d
)))
8271 /* D has already been instantiated. It might seem reasonable to
8272 check whether or not D is an explict instantiation, and, if so,
8273 stop here. But when an explicit instantiation is deferred
8274 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
8275 is set, even though we still need to do the instantiation. */
8278 /* If we already have a specialization of this declaration, then
8279 there's no reason to instantiate it. Note that
8280 retrieve_specialization gives us both instantiations and
8281 specializations, so we must explicitly check
8282 DECL_TEMPLATE_SPECIALIZATION. */
8283 gen_tmpl
= most_general_template (tmpl
);
8284 spec
= retrieve_specialization (gen_tmpl
, args
);
8285 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
8288 /* This needs to happen before any tsubsting. */
8289 if (! push_tinst_level (d
))
8292 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
8293 for the instantiation. This is not always the most general
8294 template. Consider, for example:
8297 struct S { template <class U> void f();
8298 template <> void f<int>(); };
8300 and an instantiation of S<double>::f<int>. We want TD to be the
8301 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
8304 /* An instantiation cannot have a definition, so we need a
8305 more general template. */
8306 DECL_TEMPLATE_INSTANTIATION (td
)
8307 /* We must also deal with friend templates. Given:
8309 template <class T> struct S {
8310 template <class U> friend void f() {};
8313 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
8314 so far as the language is concerned, but that's still
8315 where we get the pattern for the instantiation from. On
8316 ther hand, if the definition comes outside the class, say:
8318 template <class T> struct S {
8319 template <class U> friend void f();
8321 template <class U> friend void f() {}
8323 we don't need to look any further. That's what the check for
8324 DECL_INITIAL is for. */
8325 || (TREE_CODE (d
) == FUNCTION_DECL
8326 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
8327 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
8330 /* The present template, TD, should not be a definition. If it
8331 were a definition, we should be using it! Note that we
8332 cannot restructure the loop to just keep going until we find
8333 a template with a definition, since that might go too far if
8334 a specialization was declared, but not defined. */
8335 my_friendly_assert (!(TREE_CODE (d
) == FUNCTION_DECL
8336 && DECL_INITIAL (DECL_TEMPLATE_RESULT (td
))),
8338 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
8339 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
8342 /* Fetch the more general template. */
8343 td
= DECL_TI_TEMPLATE (td
);
8346 code_pattern
= DECL_TEMPLATE_RESULT (td
);
8348 if (TREE_CODE (d
) == FUNCTION_DECL
)
8349 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
8351 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
8353 push_to_top_level ();
8354 lineno
= DECL_SOURCE_LINE (d
);
8355 input_filename
= DECL_SOURCE_FILE (d
);
8357 if (pattern_defined
)
8359 repo_template_used (d
);
8361 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
8363 if (flag_alt_external_templates
)
8365 if (interface_unknown
)
8366 warn_if_unknown_interface (d
);
8368 else if (DECL_INTERFACE_KNOWN (code_pattern
))
8370 DECL_INTERFACE_KNOWN (d
) = 1;
8371 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
8374 warn_if_unknown_interface (code_pattern
);
8378 import_export_decl (d
);
8381 /* Reject all external templates except inline functions. */
8382 if (DECL_INTERFACE_KNOWN (d
)
8383 && ! DECL_NOT_REALLY_EXTERN (d
)
8384 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
8387 if (TREE_CODE (d
) == VAR_DECL
8388 && TREE_READONLY (d
)
8389 && DECL_INITIAL (d
) == NULL_TREE
8390 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
8391 /* We need to set up DECL_INITIAL regardless of pattern_defined if
8392 the variable is a static const initialized in the class body. */;
8393 else if (! pattern_defined
8394 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
8397 /* Defer all templates except inline functions used in another
8398 function. We restore the source position here because it's used
8399 by add_pending_template. */
8401 input_filename
= file
;
8403 if (at_eof
&& !pattern_defined
8404 && DECL_EXPLICIT_INSTANTIATION (d
))
8407 The definition of a non-exported function template, a
8408 non-exported member function template, or a non-exported
8409 member function or static data member of a class template
8410 shall be present in every translation unit in which it is
8411 explicitly instantiated. */
8412 cp_error ("explicit instantiation of `%D' but no definition available",
8415 add_pending_template (d
);
8419 regenerate_decl_from_template (d
, td
);
8421 /* We already set the file and line above. Reset them now in case
8422 they changed as a result of calling regenerate_decl_from_template. */
8423 lineno
= DECL_SOURCE_LINE (d
);
8424 input_filename
= DECL_SOURCE_FILE (d
);
8426 if (TREE_CODE (d
) == VAR_DECL
)
8428 DECL_IN_AGGR_P (d
) = 0;
8429 if (DECL_INTERFACE_KNOWN (d
))
8430 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
8433 DECL_EXTERNAL (d
) = 1;
8434 DECL_NOT_REALLY_EXTERN (d
) = 1;
8436 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
8438 else if (TREE_CODE (d
) == FUNCTION_DECL
)
8440 tree t
= DECL_SAVED_TREE (code_pattern
);
8442 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
8443 store_parm_decls ();
8445 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
8448 (TREE_OPERAND (t
, 0),
8449 tsubst_expr (TREE_OPERAND (t
, 1), args
, tmpl
));
8453 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
8455 current_member_init_list
8456 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
8457 current_base_init_list
8458 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
8463 /* Always keep the BLOCK node associated with the outermost
8464 pair of curly braces of a function. These are needed
8465 for correct operation of dwarfout.c. */
8468 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
8469 tsubst_expr (t
, args
, tmpl
);
8471 finish_function (lineno
, 0, nested
);
8476 input_filename
= file
;
8478 pop_from_top_level ();
8485 tsubst_expr_values (t
, argvec
)
8488 tree first
= NULL_TREE
;
8491 for (; t
; t
= TREE_CHAIN (t
))
8493 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
, NULL_TREE
);
8494 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, NULL_TREE
);
8495 *p
= build_tree_list (pur
, val
);
8496 p
= &TREE_CHAIN (*p
);
8507 last_tree
= TREE_CHAIN (last_tree
) = t
;
8514 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
8515 last_tree
= NULL_TREE
;
8522 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
8524 last_tree
= TREE_VALUE (saved_trees
);
8525 saved_trees
= TREE_CHAIN (saved_trees
);
8528 /* D is an undefined function declaration in the presence of templates with
8529 the same name, listed in FNS. If one of them can produce D as an
8530 instantiation, remember this so we can instantiate it at EOF if D has
8531 not been defined by that time. */
8534 add_maybe_template (d
, fns
)
8539 if (DECL_MAYBE_TEMPLATE (d
))
8542 t
= most_specialized (fns
, d
, NULL_TREE
);
8545 if (t
== error_mark_node
)
8547 cp_error ("ambiguous template instantiation for `%D'", d
);
8551 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
8552 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
8553 DECL_MAYBE_TEMPLATE (d
) = 1;
8556 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
8559 set_current_access_from_decl (decl
)
8562 if (TREE_PRIVATE (decl
))
8563 current_access_specifier
= access_private_node
;
8564 else if (TREE_PROTECTED (decl
))
8565 current_access_specifier
= access_protected_node
;
8567 current_access_specifier
= access_public_node
;
8570 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
8571 is the instantiation (which should have been created with
8572 start_enum) and ARGS are the template arguments to use. */
8575 tsubst_enum (tag
, newtag
, args
)
8582 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
8587 /* Note that in a template enum, the TREE_VALUE is the
8588 CONST_DECL, not the corresponding INTEGER_CST. */
8589 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
8593 /* Give this enumeration constant the correct access. */
8594 set_current_access_from_decl (TREE_VALUE (e
));
8596 /* Actually build the enumerator itself. */
8597 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
8599 /* We save the enumerators we have built so far in the
8600 TYPE_VALUES so that if the enumeration constants for
8601 subsequent enumerators involve those for previous ones,
8602 tsubst_copy will be able to find them. */
8603 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
8604 TYPE_VALUES (newtag
) = elt
;
8607 finish_enum (newtag
);
8610 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
8611 is either an instantiation or specialization of a template
8615 set_mangled_name_for_template_decl (decl
)
8618 tree saved_namespace
;
8619 tree context
= NULL_TREE
;
8628 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
8629 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
8631 /* The names of template functions must be mangled so as to indicate
8632 what template is being specialized with what template arguments.
8633 For example, each of the following three functions must get
8634 different mangled names:
8637 template <> void f<7>(int);
8638 template <> void f<8>(int); */
8640 targs
= DECL_TI_ARGS (decl
);
8641 if (uses_template_parms (targs
))
8642 /* This DECL is for a partial instantiation. There's no need to
8643 mangle the name of such an entity. */
8646 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
8647 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
8648 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
8650 /* There should be as many levels of arguments as there are levels
8652 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
8654 /* We now compute the PARMS and RET_TYPE to give to
8655 build_decl_overload_real. The PARMS and RET_TYPE are the
8656 parameter and return types of the template, after all but the
8657 innermost template arguments have been substituted, not the
8658 parameter and return types of the function DECL. For example,
8661 template <class T> T f(T);
8663 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
8664 A more subtle example is:
8666 template <class T> struct S { template <class U> void f(T, U); }
8668 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
8669 {int, U}. Thus, the args that we want to subsitute into the
8670 return and parameter type for the function are those in TARGS,
8671 with the innermost level omitted. */
8672 fn_type
= TREE_TYPE (tmpl
);
8673 if (DECL_STATIC_FUNCTION_P (decl
))
8674 context
= DECL_CLASS_CONTEXT (decl
);
8676 if (parm_depth
== 1)
8677 /* No substitution is necessary. */
8684 /* Replace the innermost level of the TARGS with NULL_TREEs to
8685 let tsubst know not to subsitute for those parameters. */
8686 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
8687 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
8688 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
8689 TMPL_ARGS_LEVEL (targs
, i
));
8690 SET_TMPL_ARGS_LEVEL (partial_args
,
8691 TMPL_ARGS_DEPTH (targs
),
8692 make_temp_vec (DECL_NTPARMS (tmpl
)));
8694 /* Now, do the (partial) substitution to figure out the
8695 appropriate function type. */
8696 fn_type
= tsubst (fn_type
, partial_args
, NULL_TREE
);
8697 if (DECL_STATIC_FUNCTION_P (decl
))
8698 context
= tsubst (context
, partial_args
, NULL_TREE
);
8700 /* Substitute into the template parameters to obtain the real
8701 innermost set of parameters. This step is important if the
8702 innermost set of template parameters contains value
8703 parameters whose types depend on outer template parameters. */
8704 TREE_VEC_LENGTH (partial_args
)--;
8705 tparms
= tsubst_template_parms (tparms
, partial_args
);
8708 /* Now, get the innermost parameters and arguments, and figure out
8709 the parameter and return types. */
8710 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
8711 targs
= innermost_args (targs
);
8712 ret_type
= TREE_TYPE (fn_type
);
8713 parm_types
= TYPE_ARG_TYPES (fn_type
);
8715 /* For a static member function, we generate a fake `this' pointer,
8716 for the purposes of mangling. This indicates of which class the
8717 function is a member. Because of:
8721 There shall not be a static and a nonstatic member function
8722 with the same name and the same parameter types
8724 we don't have to worry that this will result in a clash with a
8725 non-static member function. */
8726 if (DECL_STATIC_FUNCTION_P (decl
))
8727 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
8729 /* There should be the same number of template parameters as
8730 template arguments. */
8731 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
8734 /* If the template is in a namespace, we need to put that into the
8735 mangled name. Unfortunately, build_decl_overload_real does not
8736 get the decl to mangle, so it relies on the current
8737 namespace. Therefore, we set that here temporarily. */
8738 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
8739 saved_namespace
= current_namespace
;
8740 current_namespace
= CP_DECL_CONTEXT (decl
);
8742 /* Actually set the DCL_ASSEMBLER_NAME. */
8743 DECL_ASSEMBLER_NAME (decl
)
8744 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
8746 DECL_FUNCTION_MEMBER_P (decl
)
8747 + DECL_CONSTRUCTOR_P (decl
));
8749 /* Restore the previously active namespace. */
8750 current_namespace
= saved_namespace
;