1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
43 /* The type of functions taking a tree, and some additional data, and
45 typedef int (*tree_fn_t
) PROTO((tree
, void*));
47 extern struct obstack permanent_obstack
;
50 extern char *input_filename
;
51 struct pending_inline
*pending_template_expansions
;
53 tree current_template_parms
;
54 HOST_WIDE_INT processing_template_decl
;
56 tree pending_templates
;
57 static tree
*template_tail
= &pending_templates
;
60 static tree
*maybe_template_tail
= &maybe_templates
;
62 int minimal_parse_mode
;
64 int processing_specialization
;
65 int processing_explicit_instantiation
;
66 int processing_template_parmlist
;
67 static int template_header_count
;
69 static tree saved_trees
;
71 #define obstack_chunk_alloc xmalloc
72 #define obstack_chunk_free free
74 #define UNIFY_ALLOW_NONE 0
75 #define UNIFY_ALLOW_MORE_CV_QUAL 1
76 #define UNIFY_ALLOW_LESS_CV_QUAL 2
77 #define UNIFY_ALLOW_DERIVED 4
78 #define UNIFY_ALLOW_INTEGER 8
80 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
, 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 tree reduce_template_parm_level
PROTO((tree
, tree
, int));
115 static tree build_template_decl
PROTO((tree
, tree
));
116 static int mark_template_parm
PROTO((tree
, void *));
117 static tree tsubst_friend_function
PROTO((tree
, tree
));
118 static tree tsubst_friend_class
PROTO((tree
, tree
));
119 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
120 static int template_decl_level
PROTO((tree
));
121 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
122 static int check_cv_quals_for_unify
PROTO((int, tree
, tree
));
123 static tree tsubst_template_arg_vector
PROTO((tree
, tree
));
124 static tree tsubst_template_parms
PROTO((tree
, tree
));
125 static void regenerate_decl_from_template
PROTO((tree
, tree
));
126 static tree most_specialized
PROTO((tree
, tree
, tree
));
127 static tree most_specialized_class
PROTO((tree
, tree
));
128 static tree most_general_template
PROTO((tree
));
129 static void set_mangled_name_for_template_decl
PROTO((tree
));
130 static int template_class_depth_real
PROTO((tree
, int));
131 static tree tsubst_aggr_type
PROTO((tree
, tree
, tree
, int));
132 static tree tsubst_decl
PROTO((tree
, tree
, tree
, tree
));
133 static tree tsubst_arg_types
PROTO((tree
, tree
, tree
));
134 static void check_specialization_scope
PROTO((void));
135 static tree process_partial_specialization
PROTO((tree
));
136 static void set_current_access_from_decl
PROTO((tree
));
137 static void check_default_tmpl_args
PROTO((tree
, tree
, int, int));
138 static tree tsubst_call_declarator_parms
PROTO((tree
, tree
, tree
));
139 static tree get_template_base_recursive
PROTO((tree
, tree
, int *, tree
, tree
,
141 static tree get_template_base
PROTO((tree
, tree
, int *, tree
, tree
));
143 /* We use TREE_VECs to hold template arguments. If there is only one
144 level of template arguments, then the TREE_VEC contains the
145 arguments directly. If there is more than one level of template
146 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
147 containing the template arguments for a single level. The first
148 entry in the outer TREE_VEC is the outermost level of template
149 parameters; the last is the innermost.
151 It is incorrect to ever form a template argument vector containing
152 only one level of arguments, but which is a TREE_VEC containing as
153 its only entry the TREE_VEC for that level. */
155 /* Non-zero if the template arguments is actually a vector of vectors,
156 rather than just a vector. */
157 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
159 && TREE_CODE (NODE) == TREE_VEC \
160 && TREE_VEC_LENGTH (NODE) > 0 \
161 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
162 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
164 /* The depth of a template argument vector. When called directly by
165 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
166 template arguments. In fact, we may even see NULL_TREE if there
167 are no template arguments. In both of those cases, there is only
168 one level of template arguments. */
169 #define TMPL_ARGS_DEPTH(NODE) \
170 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
172 /* The LEVELth level of the template ARGS. Note that template
173 parameter levels are indexed from 1, not from 0. */
174 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
175 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
176 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
178 /* Set the LEVELth level of the template ARGS to VAL. This macro does
179 not work with single-level argument vectors. */
180 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
181 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
183 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
184 #define TMPL_ARG(ARGS, LEVEL, IDX) \
185 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
187 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
188 macro does not work with single-level argument vectors. */
189 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
190 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
192 /* Given a single level of template arguments in NODE, return the
193 number of arguments. */
194 #define NUM_TMPL_ARGS(NODE) \
195 ((NODE) == NULL_TREE ? 0 \
196 : (TREE_CODE (NODE) == TREE_VEC \
197 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
199 /* The number of levels of template parameters given by NODE. */
200 #define TMPL_PARMS_DEPTH(NODE) \
201 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
203 /* Do any processing required when DECL (a member template declaration
204 using TEMPLATE_PARAMETERS as its innermost parameter list) is
205 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
206 it is a specialization, in which case the DECL itself is returned. */
209 finish_member_template_decl (decl
)
212 if (decl
== NULL_TREE
|| decl
== void_type_node
)
214 else if (decl
== error_mark_node
)
215 /* By returning NULL_TREE, the parser will just ignore this
216 declaration. We have already issued the error. */
218 else if (TREE_CODE (decl
) == TREE_LIST
)
220 /* Assume that the class is the only declspec. */
221 decl
= TREE_VALUE (decl
);
222 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
223 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
225 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
226 check_member_template (tmpl
);
231 else if (DECL_TEMPLATE_INFO (decl
))
233 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
235 check_member_template (DECL_TI_TEMPLATE (decl
));
236 return DECL_TI_TEMPLATE (decl
);
242 cp_error ("invalid member template declaration `%D'", decl
);
244 return error_mark_node
;
247 /* Returns the template nesting level of the indicated class TYPE.
257 A<T>::B<U> has depth two, while A<T> has depth one.
258 Both A<T>::B<int> and A<int>::B<U> have depth one, if
259 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
262 This function is guaranteed to return 0 if passed NULL_TREE so
263 that, for example, `template_class_depth (current_class_type)' is
267 template_class_depth_real (type
, count_specializations
)
269 int count_specializations
;
274 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
275 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
276 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
278 if (TREE_CODE (type
) != FUNCTION_DECL
)
280 if (CLASSTYPE_TEMPLATE_INFO (type
)
281 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
282 && ((count_specializations
283 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
284 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
289 if (DECL_TEMPLATE_INFO (type
)
290 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
291 && ((count_specializations
292 && DECL_TEMPLATE_SPECIALIZATION (type
))
293 || uses_template_parms (DECL_TI_ARGS (type
))))
301 /* Returns the template nesting level of the indicated class TYPE.
302 Like template_class_depth_real, but instantiations do not count in
306 template_class_depth (type
)
309 return template_class_depth_real (type
, /*count_specializations=*/0);
312 /* Returns 1 if processing DECL as part of do_pending_inlines
313 needs us to push template parms. */
316 inline_needs_template_parms (decl
)
319 if (! DECL_TEMPLATE_INFO (decl
))
322 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
323 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
326 /* Subroutine of maybe_begin_member_template_processing.
327 Push the template parms in PARMS, starting from LEVELS steps into the
328 chain, and ending at the beginning, since template parms are listed
332 push_inline_template_parms_recursive (parmlist
, levels
)
336 tree parms
= TREE_VALUE (parmlist
);
340 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
342 ++processing_template_decl
;
343 current_template_parms
344 = tree_cons (build_int_2 (0, processing_template_decl
),
345 parms
, current_template_parms
);
346 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
349 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
351 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
352 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
354 switch (TREE_CODE (parm
))
363 /* Make a CONST_DECL as is done in process_template_parm.
364 It is ugly that we recreate this here; the original
365 version built in process_template_parm is no longer
367 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
369 SET_DECL_ARTIFICIAL (decl
);
370 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
371 DECL_TEMPLATE_PARM_P (decl
) = 1;
377 my_friendly_abort (0);
382 /* Restore the template parameter context for a member template or
383 a friend template defined in a class definition. */
386 maybe_begin_member_template_processing (decl
)
392 if (! inline_needs_template_parms (decl
))
395 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
397 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
399 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
402 parms
= TREE_CHAIN (parms
);
405 push_inline_template_parms_recursive (parms
, levels
);
408 /* Undo the effects of begin_member_template_processing. */
411 maybe_end_member_template_processing ()
413 if (! processing_template_decl
)
416 while (current_template_parms
417 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
419 --processing_template_decl
;
420 current_template_parms
= TREE_CHAIN (current_template_parms
);
425 /* Returns non-zero iff T is a member template function. We must be
428 template <class T> class C { void f(); }
430 Here, f is a template function, and a member, but not a member
431 template. This function does not concern itself with the origin of
432 T, only its present state. So if we have
434 template <class T> class C { template <class U> void f(U); }
436 then neither C<int>::f<char> nor C<T>::f<double> is considered
437 to be a member template. But, `template <class U> void
438 C<int>::f(U)' is considered a member template. */
441 is_member_template (t
)
444 if (!DECL_FUNCTION_TEMPLATE_P (t
))
445 /* Anything that isn't a function or a template function is
446 certainly not a member template. */
449 /* A local class can't have member templates. */
450 if (hack_decl_function_context (t
))
453 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
454 /* If there are more levels of template parameters than
455 there are template classes surrounding the declaration,
456 then we have a member template. */
457 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
458 template_class_depth (DECL_CLASS_CONTEXT (t
))));
462 /* Returns non-zero iff T is a member template class. See
463 is_member_template for a description of what precisely constitutes
464 a member template. */
467 is_member_template_class (t
)
470 if (!DECL_CLASS_TEMPLATE_P (t
))
471 /* Anything that isn't a class template, is certainly not a member
475 if (!DECL_CLASS_SCOPE_P (t
))
476 /* Anything whose context isn't a class type is surely not a
480 /* If there are more levels of template parameters than there are
481 template classes surrounding the declaration, then we have a
483 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
484 template_class_depth (DECL_CONTEXT (t
)));
488 /* Return a new template argument vector which contains all of ARGS,
489 but has as its innermost set of arguments the EXTRA_ARGS. The
490 resulting vector will be built on a temporary obstack, and so must
491 be explicitly copied to the permanent obstack, if required. */
494 add_to_template_args (args
, extra_args
)
503 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
504 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
506 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
507 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
509 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
510 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
515 /* Like add_to_template_args, but only the outermost ARGS are added to
516 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
517 (EXTRA_ARGS) levels are added. This function is used to combine
518 the template arguments from a partial instantiation with the
519 template arguments used to attain the full instantiation from the
520 partial instantiation. */
523 add_outermost_template_args (args
, extra_args
)
529 /* If there are more levels of EXTRA_ARGS than there are ARGS,
530 something very fishy is going on. */
531 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
534 /* If *all* the new arguments will be the EXTRA_ARGS, just return
536 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
539 /* For the moment, we make ARGS look like it contains fewer levels. */
540 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
542 new_args
= add_to_template_args (args
, extra_args
);
544 /* Now, we restore ARGS to its full dimensions. */
545 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
550 /* We've got a template header coming up; push to a new level for storing
554 begin_template_parm_list ()
556 /* We use a non-tag-transparent scope here, which causes pushtag to
557 put tags in this scope, rather than in the enclosing class or
558 namespace scope. This is the right thing, since we want
559 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
560 global template class, push_template_decl handles putting the
561 TEMPLATE_DECL into top-level scope. For a nested template class,
564 template <class T> struct S1 {
565 template <class T> struct S2 {};
568 pushtag contains special code to call pushdecl_with_scope on the
569 TEMPLATE_DECL for S2. */
571 declare_pseudo_global_level ();
572 ++processing_template_decl
;
573 ++processing_template_parmlist
;
574 note_template_header (0);
577 /* This routine is called when a specialization is declared. If it is
578 illegal to declare a specialization here, an error is reported. */
581 check_specialization_scope ()
583 tree scope
= current_scope ();
587 An explicit specialization shall be declared in the namespace of
588 which the template is a member, or, for member templates, in the
589 namespace of which the enclosing class or enclosing class
590 template is a member. An explicit specialization of a member
591 function, member class or static data member of a class template
592 shall be declared in the namespace of which the class template
594 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
595 cp_error ("explicit specialization in non-namespace scope `%D'",
600 In an explicit specialization declaration for a member of a class
601 template or a member template that appears in namespace scope,
602 the member template and some of its enclosing class templates may
603 remain unspecialized, except that the declaration shall not
604 explicitly specialize a class member template if its enclosing
605 class templates are not explicitly specialized as well. */
606 if (current_template_parms
)
607 cp_error ("enclosing class templates are not explicitly specialized");
610 /* We've just seen template <>. */
613 begin_specialization ()
615 note_template_header (1);
616 check_specialization_scope ();
619 /* Called at then end of processing a declaration preceeded by
623 end_specialization ()
625 reset_specialization ();
628 /* Any template <>'s that we have seen thus far are not referring to a
629 function specialization. */
632 reset_specialization ()
634 processing_specialization
= 0;
635 template_header_count
= 0;
638 /* We've just seen a template header. If SPECIALIZATION is non-zero,
639 it was of the form template <>. */
642 note_template_header (specialization
)
645 processing_specialization
= specialization
;
646 template_header_count
++;
649 /* We're beginning an explicit instantiation. */
652 begin_explicit_instantiation ()
654 ++processing_explicit_instantiation
;
659 end_explicit_instantiation ()
661 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
662 --processing_explicit_instantiation
;
665 /* The TYPE is being declared. If it is a template type, that means it
666 is a partial specialization. Do appropriate error-checking. */
669 maybe_process_partial_specialization (type
)
672 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
674 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
675 && TYPE_SIZE (type
) == NULL_TREE
)
677 if (current_namespace
678 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
680 cp_pedwarn ("specializing `%#T' in different namespace", type
);
681 cp_pedwarn_at (" from definition of `%#D'",
682 CLASSTYPE_TI_TEMPLATE (type
));
684 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
685 if (processing_template_decl
)
686 push_template_decl (TYPE_MAIN_DECL (type
));
688 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
689 cp_error ("specialization of `%T' after instantiation", type
);
693 /* Retrieve the specialization (in the sense of [temp.spec] - a
694 specialization is either an instantiation or an explicit
695 specialization) of TMPL for the given template ARGS. If there is
696 no such specialization, return NULL_TREE. The ARGS are a vector of
697 arguments, or a vector of vectors of arguments, in the case of
698 templates with more than one level of parameters. */
701 retrieve_specialization (tmpl
, args
)
707 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
709 /* There should be as many levels of arguments as there are
710 levels of parameters. */
711 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
712 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
715 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
718 if (comp_template_args (TREE_PURPOSE (s
), args
))
719 return TREE_VALUE (s
);
724 /* Returns non-zero iff DECL is a specialization of TMPL. */
727 is_specialization_of (decl
, tmpl
)
733 if (TREE_CODE (decl
) == FUNCTION_DECL
)
737 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
743 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
745 for (t
= TREE_TYPE (decl
);
747 t
= CLASSTYPE_USE_TEMPLATE (t
)
748 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
749 if (same_type_p (TYPE_MAIN_VARIANT (t
),
750 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
757 /* Register the specialization SPEC as a specialization of TMPL with
758 the indicated ARGS. Returns SPEC, or an equivalent prior
759 declaration, if available. */
762 register_specialization (spec
, tmpl
, args
)
769 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
771 if (TREE_CODE (spec
) == FUNCTION_DECL
772 && uses_template_parms (DECL_TI_ARGS (spec
)))
773 /* This is the FUNCTION_DECL for a partial instantiation. Don't
774 register it; we want the corresponding TEMPLATE_DECL instead.
775 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
776 the more obvious `uses_template_parms (spec)' to avoid problems
777 with default function arguments. In particular, given
780 template <class T> void f(T t1, T t = T())
782 the default argument expression is not substituted for in an
783 instantiation unless and until it is actually needed. */
786 /* There should be as many levels of arguments as there are
787 levels of parameters. */
788 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
789 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
792 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
795 if (comp_template_args (TREE_PURPOSE (s
), args
))
797 tree fn
= TREE_VALUE (s
);
799 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
801 if (DECL_TEMPLATE_INSTANTIATION (fn
))
804 || DECL_EXPLICIT_INSTANTIATION (fn
))
806 cp_error ("specialization of %D after instantiation",
812 /* This situation should occur only if the first
813 specialization is an implicit instantiation,
814 the second is an explicit specialization, and
815 the implicit instantiation has not yet been
816 used. That situation can occur if we have
817 implicitly instantiated a member function and
818 then specialized it later.
820 We can also wind up here if a friend
821 declaration that looked like an instantiation
822 turns out to be a specialization:
824 template <class T> void foo(T);
825 class S { friend void foo<>(int) };
826 template <> void foo(int);
828 We transform the existing DECL in place so that
829 any pointers to it become pointers to the
832 If there was a definition for the template, but
833 not for the specialization, we want this to
834 look as if there is no definition, and vice
836 DECL_INITIAL (fn
) = NULL_TREE
;
837 duplicate_decls (spec
, fn
);
842 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
844 duplicate_decls (spec
, fn
);
850 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
851 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
856 /* Unregister the specialization SPEC as a specialization of TMPL.
857 Returns nonzero if the SPEC was listed as a specialization of
861 unregister_specialization (spec
, tmpl
)
867 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
869 s
= &TREE_CHAIN (*s
))
870 if (TREE_VALUE (*s
) == spec
)
872 *s
= TREE_CHAIN (*s
);
879 /* Print the list of candidate FNS in an error message. */
882 print_candidates (fns
)
887 char* str
= "candidates are:";
889 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
891 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
896 /* Returns the template (one of the functions given by TEMPLATE_ID)
897 which can be specialized to match the indicated DECL with the
898 explicit template args given in TEMPLATE_ID. If
899 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
900 member template. The template args (those explicitly specified and
901 those deduced) are output in a newly created vector *TARGS_OUT. If
902 it is impossible to determine the result, an error message is
903 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
907 determine_specialization (template_id
, decl
, targs_out
,
908 need_member_template
,
913 int need_member_template
;
917 tree templates
= NULL_TREE
;
920 *targs_out
= NULL_TREE
;
922 if (template_id
== error_mark_node
)
923 return error_mark_node
;
925 fns
= TREE_OPERAND (template_id
, 0);
926 targs_in
= TREE_OPERAND (template_id
, 1);
928 if (fns
== error_mark_node
)
929 return error_mark_node
;
931 /* Check for baselinks. */
932 if (TREE_CODE (fns
) == TREE_LIST
)
933 fns
= TREE_VALUE (fns
);
935 for (; fns
; fns
= OVL_NEXT (fns
))
939 fn
= OVL_CURRENT (fns
);
940 if (!need_member_template
941 && TREE_CODE (fn
) == FUNCTION_DECL
942 && DECL_FUNCTION_MEMBER_P (fn
)
943 && DECL_USE_TEMPLATE (fn
)
944 && DECL_TI_TEMPLATE (fn
))
945 /* We can get here when processing something like:
946 template <class T> class X { void f(); }
947 template <> void X<int>::f() {}
948 We're specializing a member function, but not a member
950 tmpl
= DECL_TI_TEMPLATE (fn
);
951 else if (TREE_CODE (fn
) != TEMPLATE_DECL
952 || (need_member_template
&& !is_member_template (fn
)))
957 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
960 if (decl
== NULL_TREE
)
962 /* Unify against ourselves to make sure that the args we have
963 make sense and there aren't any undeducible parms. It's OK if
964 not all the parms are specified; they might be deduced
966 tree targs
= get_bindings_overload (tmpl
, DECL_RESULT (tmpl
),
970 /* Unification was successful. */
971 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
974 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
977 if (decl
!= NULL_TREE
)
979 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
983 if (tmpl
== error_mark_node
)
985 else if (tmpl
== NULL_TREE
)
988 inner_args
= get_bindings (tmpl
, decl
, targs_in
);
989 tmpl_args
= DECL_TI_ARGS (DECL_RESULT (tmpl
));
990 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args
))
992 *targs_out
= copy_node (tmpl_args
);
993 SET_TMPL_ARGS_LEVEL (*targs_out
,
994 TMPL_ARGS_DEPTH (*targs_out
),
998 *targs_out
= inner_args
;
1003 if (templates
== NULL_TREE
)
1008 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1010 return error_mark_node
;
1014 else if (TREE_CHAIN (templates
) != NULL_TREE
1015 || uses_template_parms (TREE_PURPOSE (templates
)))
1020 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1022 print_candidates (templates
);
1023 return error_mark_node
;
1028 /* We have one, and exactly one, match. */
1029 *targs_out
= TREE_PURPOSE (templates
);
1030 return TREE_VALUE (templates
);
1033 /* Check to see if the function just declared, as indicated in
1034 DECLARATOR, and in DECL, is a specialization of a function
1035 template. We may also discover that the declaration is an explicit
1036 instantiation at this point.
1038 Returns DECL, or an equivalent declaration that should be used
1041 FLAGS is a bitmask consisting of the following flags:
1043 2: The function has a definition.
1044 4: The function is a friend.
1046 The TEMPLATE_COUNT is the number of references to qualifying
1047 template classes that appeared in the name of the function. For
1050 template <class T> struct S { void f(); };
1053 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1054 classes are not counted in the TEMPLATE_COUNT, so that in
1056 template <class T> struct S {};
1057 template <> struct S<int> { void f(); }
1058 template <> void S<int>::f();
1060 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1061 illegal; there should be no template <>.)
1063 If the function is a specialization, it is marked as such via
1064 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1065 is set up correctly, and it is added to the list of specializations
1066 for that template. */
1069 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1075 int have_def
= flags
& 2;
1076 int is_friend
= flags
& 4;
1077 int specialization
= 0;
1078 int explicit_instantiation
= 0;
1079 int member_specialization
= 0;
1081 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1082 tree dname
= DECL_NAME (decl
);
1084 if (processing_specialization
)
1086 /* The last template header was of the form template <>. */
1088 if (template_header_count
> template_count
)
1090 /* There were more template headers than qualifying template
1092 if (template_header_count
- template_count
> 1)
1093 /* There shouldn't be that many template parameter
1094 lists. There can be at most one parameter list for
1095 every qualifying class, plus one for the function
1097 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1099 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1101 member_specialization
= 1;
1105 else if (template_header_count
== template_count
)
1107 /* The counts are equal. So, this might be a
1108 specialization, but it is not a specialization of a
1109 member template. It might be something like
1111 template <class T> struct S {
1115 void S<int>::f(int i) {} */
1117 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1121 /* This cannot be an explicit specialization. There are not
1122 enough headers for all of the qualifying classes. For
1123 example, we might have:
1126 void S<int>::T<char>::f();
1128 But, we're missing another template <>. */
1129 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1133 else if (processing_explicit_instantiation
)
1135 if (template_header_count
)
1136 cp_error ("template parameter list used in explicit instantiation");
1139 cp_error ("definition provided for explicit instantiation");
1141 explicit_instantiation
= 1;
1143 else if (ctype
!= NULL_TREE
1144 && !TYPE_BEING_DEFINED (ctype
)
1145 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1148 /* This case catches outdated code that looks like this:
1150 template <class T> struct S { void f(); };
1151 void S<int>::f() {} // Missing template <>
1153 We disable this check when the type is being defined to
1154 avoid complaining about default compiler-generated
1155 constructors, destructors, and assignment operators.
1156 Since the type is an instantiation, not a specialization,
1157 these are the only functions that can be defined before
1158 the class is complete. */
1161 template <class T> void S<int>::f() {}
1163 if (template_header_count
)
1165 cp_error ("template parameters specified in specialization");
1171 ("explicit specialization not preceded by `template <>'");
1173 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1175 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1178 /* This could be something like:
1180 template <class T> void f(T);
1181 class S { friend void f<>(int); } */
1185 /* This case handles bogus declarations like template <>
1186 template <class T> void f<int>(); */
1188 cp_error ("template-id `%D' in declaration of primary template",
1194 if (specialization
|| member_specialization
)
1196 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1197 for (; t
; t
= TREE_CHAIN (t
))
1198 if (TREE_PURPOSE (t
))
1201 ("default argument specified in explicit specialization");
1204 if (current_lang_name
== lang_name_c
)
1205 cp_error ("template specialization with C linkage");
1208 if (specialization
|| member_specialization
|| explicit_instantiation
)
1211 tree tmpl
= NULL_TREE
;
1212 tree targs
= NULL_TREE
;
1214 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1215 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1219 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1222 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1227 lookup_template_function (fns
, NULL_TREE
);
1230 if (declarator
== error_mark_node
)
1231 return error_mark_node
;
1233 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1235 /* A friend declaration. We can't do much, because we don't
1236 know what this resolves to, yet. */
1237 my_friendly_assert (is_friend
!= 0, 0);
1238 my_friendly_assert (!explicit_instantiation
, 0);
1239 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1243 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1245 if (!explicit_instantiation
)
1246 /* A specialization in class scope. This is illegal,
1247 but the error will already have been flagged by
1248 check_specialization_scope. */
1249 return error_mark_node
;
1252 /* It's not legal to write an explicit instantiation in
1255 class C { template void f(); }
1257 This case is caught by the parser. However, on
1260 template class C { void f(); };
1262 (which is illegal) we can get here. The error will be
1269 else if (ctype
!= NULL_TREE
1270 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1273 /* Find the list of functions in ctype that have the same
1274 name as the declared function. */
1275 tree name
= TREE_OPERAND (declarator
, 0);
1278 if (name
== constructor_name (ctype
)
1279 || name
== constructor_name_full (ctype
))
1281 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1283 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1284 : !TYPE_HAS_DESTRUCTOR (ctype
))
1286 /* From [temp.expl.spec]:
1288 If such an explicit specialization for the member
1289 of a class template names an implicitly-declared
1290 special member function (clause _special_), the
1291 program is ill-formed.
1293 Similar language is found in [temp.explicit]. */
1294 cp_error ("specialization of implicitly-declared special member function");
1299 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1302 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
1304 if (fns
== NULL_TREE
)
1306 cp_error ("no member function `%s' declared in `%T'",
1307 IDENTIFIER_POINTER (name
),
1312 TREE_OPERAND (declarator
, 0) = fns
;
1315 /* Figure out what exactly is being specialized at this point.
1316 Note that for an explicit instantiation, even one for a
1317 member function, we cannot tell apriori whether the
1318 instantiation is for a member template, or just a member
1319 function of a template class. Even if a member template is
1320 being instantiated, the member template arguments may be
1321 elided if they can be deduced from the rest of the
1323 tmpl
= determine_specialization (declarator
, decl
,
1325 member_specialization
,
1328 if (tmpl
&& tmpl
!= error_mark_node
)
1330 gen_tmpl
= most_general_template (tmpl
);
1332 if (explicit_instantiation
)
1334 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1335 is done by do_decl_instantiation later. */
1336 decl
= instantiate_template (tmpl
, innermost_args (targs
));
1340 /* If we though that the DECL was a member function, but it
1341 turns out to be specializing a static member function,
1342 make DECL a static member function as well. */
1343 if (DECL_STATIC_FUNCTION_P (tmpl
)
1344 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1346 revert_static_member_fn (&decl
, 0, 0);
1347 last_function_parms
= TREE_CHAIN (last_function_parms
);
1350 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1351 DECL_TEMPLATE_INFO (decl
)
1352 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1354 /* Mangle the function name appropriately. Note that we do
1355 not mangle specializations of non-template member
1356 functions of template classes, e.g. with
1358 template <class T> struct S { void f(); }
1360 and given the specialization
1362 template <> void S<int>::f() {}
1364 we do not mangle S<int>::f() here. That's because it's
1365 just an ordinary member function and doesn't need special
1366 treatment. We do this here so that the ordinary,
1367 non-template, name-mangling algorith will not be used
1369 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1370 && name_mangling_version
>= 1)
1371 set_mangled_name_for_template_decl (decl
);
1373 if (is_friend
&& !have_def
)
1374 /* This is not really a declaration of a specialization.
1375 It's just the name of an instantiation. But, it's not
1376 a request for an instantiation, either. */
1377 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1379 /* Register this specialization so that we can find it
1381 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1390 /* TYPE is being declared. Verify that the use of template headers
1391 and such is reasonable. Issue error messages if not. */
1394 maybe_check_template_type (type
)
1397 if (template_header_count
)
1399 /* We are in the scope of some `template <...>' header. */
1402 = template_class_depth_real (TYPE_CONTEXT (type
),
1403 /*count_specializations=*/1);
1405 if (template_header_count
<= context_depth
)
1406 /* This is OK; the template headers are for the context. We
1407 are actually too lenient here; like
1408 check_explicit_specialization we should consider the number
1409 of template types included in the actual declaration. For
1412 template <class T> struct S {
1413 template <class U> template <class V>
1419 template <class T> struct S {
1420 template <class U> struct I;
1423 template <class T> template <class U.
1428 else if (template_header_count
> context_depth
+ 1)
1429 /* There are two many template parameter lists. */
1430 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1434 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1435 parameters. These are represented in the same format used for
1436 DECL_TEMPLATE_PARMS. */
1438 int comp_template_parms (parms1
, parms2
)
1445 if (parms1
== parms2
)
1448 for (p1
= parms1
, p2
= parms2
;
1449 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1450 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1452 tree t1
= TREE_VALUE (p1
);
1453 tree t2
= TREE_VALUE (p2
);
1456 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1457 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1459 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1462 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1464 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1465 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1467 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1470 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1472 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1477 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1478 /* One set of parameters has more parameters lists than the
1485 /* Complain if DECL shadows a template parameter.
1487 [temp.local]: A template-parameter shall not be redeclared within its
1488 scope (including nested scopes). */
1491 check_template_shadow (decl
)
1494 tree olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1496 if (current_template_parms
&& olddecl
)
1498 /* We check for decl != olddecl to avoid bogus errors for using a
1499 name inside a class. We check TPFI to avoid duplicate errors for
1500 inline member templates. */
1501 if (decl
!= olddecl
&& DECL_TEMPLATE_PARM_P (olddecl
)
1502 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1504 cp_error_at ("declaration of `%#D'", decl
);
1505 cp_error_at (" shadows template parm `%#D'", olddecl
);
1510 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1511 ORIG_LEVEL, DECL, and TYPE. */
1514 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1521 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1522 TEMPLATE_PARM_IDX (t
) = index
;
1523 TEMPLATE_PARM_LEVEL (t
) = level
;
1524 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1525 TEMPLATE_PARM_DECL (t
) = decl
;
1526 TREE_TYPE (t
) = type
;
1531 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1532 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1533 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1534 new one is created. */
1537 reduce_template_parm_level (index
, type
, levels
)
1542 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1543 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1544 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1547 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1548 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1551 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1552 TEMPLATE_PARM_LEVEL (index
) - levels
,
1553 TEMPLATE_PARM_ORIG_LEVEL (index
),
1555 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1557 /* Template template parameters need this. */
1558 DECL_TEMPLATE_PARMS (decl
)
1559 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1562 return TEMPLATE_PARM_DESCENDANTS (index
);
1565 /* Process information from new template parameter NEXT and append it to the
1566 LIST being built. */
1569 process_template_parm (list
, next
)
1578 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1579 defval
= TREE_PURPOSE (parm
);
1580 parm
= TREE_VALUE (parm
);
1581 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1585 tree p
= TREE_VALUE (tree_last (list
));
1587 if (TREE_CODE (p
) == TYPE_DECL
)
1588 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1589 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1590 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1592 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1600 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1601 /* is a const-param */
1602 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1603 PARM
, 0, NULL_TREE
);
1607 The top-level cv-qualifiers on the template-parameter are
1608 ignored when determining its type. */
1609 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1611 /* A template parameter is not modifiable. */
1612 TREE_READONLY (parm
) = 1;
1613 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1614 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1615 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1617 cp_error ("`%#T' is not a valid type for a template constant parameter",
1619 if (DECL_NAME (parm
) == NULL_TREE
)
1620 error (" a template type parameter must begin with `class' or `typename'");
1621 TREE_TYPE (parm
) = void_type_node
;
1624 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1625 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1626 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1628 if (TREE_PERMANENT (parm
) == 0)
1630 parm
= copy_node (parm
);
1631 TREE_PERMANENT (parm
) = 1;
1633 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1634 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1635 = build_template_parm_index (idx
, processing_template_decl
,
1636 processing_template_decl
,
1637 decl
, TREE_TYPE (parm
));
1642 parm
= TREE_VALUE (parm
);
1644 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1646 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1647 /* This is for distinguishing between real templates and template
1648 template parameters */
1649 TREE_TYPE (parm
) = t
;
1650 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1655 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1656 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1657 decl
= build_decl (TYPE_DECL
, parm
, t
);
1660 TYPE_NAME (t
) = decl
;
1661 TYPE_STUB_DECL (t
) = decl
;
1663 TEMPLATE_TYPE_PARM_INDEX (t
)
1664 = build_template_parm_index (idx
, processing_template_decl
,
1665 processing_template_decl
,
1666 decl
, TREE_TYPE (parm
));
1668 SET_DECL_ARTIFICIAL (decl
);
1669 DECL_TEMPLATE_PARM_P (decl
) = 1;
1671 parm
= build_tree_list (defval
, parm
);
1672 return chainon (list
, parm
);
1675 /* The end of a template parameter list has been reached. Process the
1676 tree list into a parameter vector, converting each parameter into a more
1677 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1681 end_template_parm_list (parms
)
1686 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1688 current_template_parms
1689 = tree_cons (build_int_2 (0, processing_template_decl
),
1690 saved_parmlist
, current_template_parms
);
1692 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1693 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1695 --processing_template_parmlist
;
1697 return saved_parmlist
;
1700 /* end_template_decl is called after a template declaration is seen. */
1703 end_template_decl ()
1705 reset_specialization ();
1707 if (! processing_template_decl
)
1710 /* This matches the pushlevel in begin_template_parm_list. */
1713 --processing_template_decl
;
1714 current_template_parms
= TREE_CHAIN (current_template_parms
);
1715 (void) get_pending_sizes (); /* Why? */
1718 /* Given a template argument vector containing the template PARMS.
1719 The innermost PARMS are given first. */
1722 current_template_args ()
1725 tree args
= NULL_TREE
;
1726 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1729 /* If there is only one level of template parameters, we do not
1730 create a TREE_VEC of TREE_VECs. Instead, we return a single
1731 TREE_VEC containing the arguments. */
1733 args
= make_tree_vec (length
);
1735 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1737 tree a
= copy_node (TREE_VALUE (header
));
1740 TREE_TYPE (a
) = NULL_TREE
;
1741 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1743 tree t
= TREE_VEC_ELT (a
, i
);
1745 /* T will be a list if we are called from within a
1746 begin/end_template_parm_list pair, but a vector directly
1747 if within a begin/end_member_template_processing pair. */
1748 if (TREE_CODE (t
) == TREE_LIST
)
1752 if (TREE_CODE (t
) == TYPE_DECL
1753 || TREE_CODE (t
) == TEMPLATE_DECL
)
1756 t
= DECL_INITIAL (t
);
1757 TREE_VEC_ELT (a
, i
) = t
;
1762 TREE_VEC_ELT (args
, --l
) = a
;
1770 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1771 template PARMS. Used by push_template_decl below. */
1774 build_template_decl (decl
, parms
)
1778 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1779 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1780 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1781 if (DECL_LANG_SPECIFIC (decl
))
1783 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1784 DECL_STATIC_FUNCTION_P (tmpl
) =
1785 DECL_STATIC_FUNCTION_P (decl
);
1791 struct template_parm_data
1793 /* The level of the template parameters we are currently
1797 /* The index of the specialization argument we are currently
1801 /* An array whose size is the number of template parameters. The
1802 elements are non-zero if the parameter has been used in any one
1803 of the arguments processed so far. */
1806 /* An array whose size is the number of template arguments. The
1807 elements are non-zero if the argument makes use of template
1808 parameters of this level. */
1809 int* arg_uses_template_parms
;
1812 /* Subroutine of push_template_decl used to see if each template
1813 parameter in a partial specialization is used in the explicit
1814 argument list. If T is of the LEVEL given in DATA (which is
1815 treated as a template_parm_data*), then DATA->PARMS is marked
1819 mark_template_parm (t
, data
)
1825 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1827 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1829 level
= TEMPLATE_PARM_LEVEL (t
);
1830 idx
= TEMPLATE_PARM_IDX (t
);
1834 level
= TEMPLATE_TYPE_LEVEL (t
);
1835 idx
= TEMPLATE_TYPE_IDX (t
);
1838 if (level
== tpd
->level
)
1840 tpd
->parms
[idx
] = 1;
1841 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
1844 /* Return zero so that for_each_template_parm will continue the
1845 traversal of the tree; we want to mark *every* template parm. */
1849 /* Process the partial specialization DECL. */
1852 process_partial_specialization (decl
)
1855 tree type
= TREE_TYPE (decl
);
1856 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1857 tree specargs
= CLASSTYPE_TI_ARGS (type
);
1858 tree inner_args
= innermost_args (specargs
);
1859 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1860 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
1861 int nargs
= TREE_VEC_LENGTH (inner_args
);
1862 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
1864 int did_error_intro
= 0;
1865 struct template_parm_data tpd
;
1866 struct template_parm_data tpd2
;
1868 /* We check that each of the template parameters given in the
1869 partial specialization is used in the argument list to the
1870 specialization. For example:
1872 template <class T> struct S;
1873 template <class T> struct S<T*>;
1875 The second declaration is OK because `T*' uses the template
1876 parameter T, whereas
1878 template <class T> struct S<int>;
1880 is no good. Even trickier is:
1891 The S2<T> declaration is actually illegal; it is a
1892 full-specialization. Of course,
1895 struct S2<T (*)(U)>;
1897 or some such would have been OK. */
1898 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
1899 tpd
.parms
= alloca (sizeof (int) * ntparms
);
1900 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
1902 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
1903 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
1904 for (i
= 0; i
< nargs
; ++i
)
1906 tpd
.current_arg
= i
;
1907 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
1908 &mark_template_parm
,
1911 for (i
= 0; i
< ntparms
; ++i
)
1912 if (tpd
.parms
[i
] == 0)
1914 /* One of the template parms was not used in the
1916 if (!did_error_intro
)
1918 cp_error ("template parameters not used in partial specialization:");
1919 did_error_intro
= 1;
1923 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
1926 /* [temp.class.spec]
1928 The argument list of the specialization shall not be identical to
1929 the implicit argument list of the primary template. */
1930 if (comp_template_args (inner_args
,
1931 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
1933 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
1935 /* [temp.class.spec]
1937 A partially specialized non-type argument expression shall not
1938 involve template parameters of the partial specialization except
1939 when the argument expression is a simple identifier.
1941 The type of a template parameter corresponding to a specialized
1942 non-type argument shall not be dependent on a parameter of the
1944 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
1946 for (i
= 0; i
< nargs
; ++i
)
1948 tree arg
= TREE_VEC_ELT (inner_args
, i
);
1949 if (/* These first two lines are the `non-type' bit. */
1950 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
1951 && TREE_CODE (arg
) != TEMPLATE_DECL
1952 /* This next line is the `argument expression is not just a
1953 simple identifier' condition and also the `specialized
1954 non-type argument' bit. */
1955 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
1957 if (tpd
.arg_uses_template_parms
[i
])
1958 cp_error ("template argument `%E' involves template parameter(s)", arg
);
1961 /* Look at the corresponding template parameter,
1962 marking which template parameters its type depends
1965 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
1970 /* We haven't yet initialized TPD2. Do so now. */
1971 tpd2
.arg_uses_template_parms
1972 = (int*) alloca (sizeof (int) * nargs
);
1973 /* The number of parameters here is the number in the
1974 main template, which, as checked in the assertion
1976 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
1978 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
1981 /* Mark the template parameters. But this time, we're
1982 looking for the template parameters of the main
1983 template, not in the specialization. */
1984 tpd2
.current_arg
= i
;
1985 tpd2
.arg_uses_template_parms
[i
] = 0;
1986 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
1987 for_each_template_parm (type
,
1988 &mark_template_parm
,
1991 if (tpd2
.arg_uses_template_parms
[i
])
1993 /* The type depended on some template parameters.
1994 If they are fully specialized in the
1995 specialization, that's OK. */
1997 for (j
= 0; j
< nargs
; ++j
)
1998 if (tpd2
.parms
[j
] != 0
1999 && tpd
.arg_uses_template_parms
[j
])
2001 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2011 if (retrieve_specialization (maintmpl
, specargs
))
2012 /* We've already got this specialization. */
2015 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2016 = perm_tree_cons (inner_args
, inner_parms
,
2017 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2018 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2022 /* Check that a template declaration's use of default arguments is not
2023 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2024 non-zero if DECL is the thing declared by a primary template.
2025 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2028 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2035 int last_level_to_check
;
2039 A default template-argument shall not be specified in a
2040 function template declaration or a function template definition, nor
2041 in the template-parameter-list of the definition of a member of a
2044 if (current_class_type
2045 && !TYPE_BEING_DEFINED (current_class_type
)
2046 && DECL_REAL_CONTEXT (decl
) == current_class_type
2047 && DECL_DEFINED_IN_CLASS_P (decl
))
2048 /* We already checked these parameters when the template was
2049 declared, so there's no need to do it again now. This is an
2050 inline member function definition. */
2053 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2054 /* For an ordinary class template, default template arguments are
2055 allowed at the innermost level, e.g.:
2056 template <class T = int>
2058 but, in a partial specialization, they're not allowed even
2059 there, as we have in [temp.class.spec]:
2061 The template parameter list of a specialization shall not
2062 contain default template argument values.
2064 So, for a partial specialization, or for a function template,
2065 we look at all of them. */
2068 /* But, for a primary class template that is not a partial
2069 specialization we look at all template parameters except the
2071 parms
= TREE_CHAIN (parms
);
2073 /* Figure out what error message to issue. */
2074 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2075 msg
= "default argument for template parameter in function template `%D'";
2076 else if (is_partial
)
2077 msg
= "default argument in partial specialization `%D'";
2079 msg
= "default argument for template parameter for class enclosing `%D'";
2081 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2082 /* If we're inside a class definition, there's no need to
2083 examine the parameters to the class itself. On the one
2084 hand, they will be checked when the class is defined, and,
2085 on the other, default arguments are legal in things like:
2086 template <class T = double>
2087 struct S { template <class U> void f(U); };
2088 Here the default argument for `S' has no bearing on the
2089 declaration of `f'. */
2090 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2092 /* Check everything. */
2093 last_level_to_check
= 0;
2095 for (; parms
&& TMPL_PARMS_DEPTH (parms
) >= last_level_to_check
;
2096 parms
= TREE_CHAIN (parms
))
2098 tree inner_parms
= TREE_VALUE (parms
);
2101 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2102 for (i
= 0; i
< ntparms
; ++i
)
2103 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2107 cp_error (msg
, decl
);
2111 /* Clear out the default argument so that we are not
2113 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2116 /* At this point, if we're still interested in issuing messages,
2117 they must apply to classes surrounding the object declared. */
2119 msg
= "default argument for template parameter for class enclosing `%D'";
2123 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2124 parameters given by current_template_args, or reuses a
2125 previously existing one, if appropriate. Returns the DECL, or an
2126 equivalent one, if it is replaced via a call to duplicate_decls.
2128 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2131 push_template_decl_real (decl
, is_friend
)
2142 /* See if this is a partial specialization. */
2143 is_partial
= (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
2144 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2145 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2147 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2150 /* For a friend, we want the context of the friend function, not
2151 the type of which it is a friend. */
2152 ctx
= DECL_CONTEXT (decl
);
2153 else if (DECL_REAL_CONTEXT (decl
)
2154 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2155 /* In the case of a virtual function, we want the class in which
2157 ctx
= DECL_REAL_CONTEXT (decl
);
2159 /* Otherwise, if we're currently definining some class, the DECL
2160 is assumed to be a member of the class. */
2161 ctx
= current_class_type
;
2163 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2166 if (!DECL_CONTEXT (decl
))
2167 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2169 /* For determining whether this is a primary template or not, we're really
2170 interested in the lexical context, not the true context. */
2172 info
= current_class_type
;
2176 /* See if this is a primary template. */
2177 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
2179 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2180 this next line works even when we are at global scope. */
2181 else if (processing_template_decl
> template_class_depth (info
))
2188 if (current_lang_name
== lang_name_c
)
2189 cp_error ("template with C linkage");
2190 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
2191 cp_error ("template class without a name");
2192 if (TREE_CODE (decl
) == TYPE_DECL
2193 && TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2194 cp_error ("template declaration of `%#T'", TREE_TYPE (decl
));
2197 /* Check to see that the rules regarding the use of default
2198 arguments are not being violated. */
2199 check_default_tmpl_args (decl
, current_template_parms
,
2200 primary
, is_partial
);
2203 return process_partial_specialization (decl
);
2205 args
= current_template_args ();
2208 || TREE_CODE (ctx
) == FUNCTION_DECL
2209 || TYPE_BEING_DEFINED (ctx
)
2210 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2212 if (DECL_LANG_SPECIFIC (decl
)
2213 && DECL_TEMPLATE_INFO (decl
)
2214 && DECL_TI_TEMPLATE (decl
))
2215 tmpl
= DECL_TI_TEMPLATE (decl
);
2218 tmpl
= build_template_decl (decl
, current_template_parms
);
2220 if (DECL_LANG_SPECIFIC (decl
)
2221 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2223 /* A specialization of a member template of a template
2225 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2226 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2227 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2233 tree a
, t
, current
, parms
;
2236 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2237 cp_error ("must specialize `%#T' before defining member `%#D'",
2239 if (TREE_CODE (decl
) == TYPE_DECL
)
2241 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2242 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2243 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2244 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2245 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2248 cp_error ("`%D' does not declare a template type", decl
);
2252 else if (! DECL_TEMPLATE_INFO (decl
))
2254 cp_error ("template definition of non-template `%#D'", decl
);
2258 tmpl
= DECL_TI_TEMPLATE (decl
);
2260 if (is_member_template (tmpl
)
2261 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2262 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2263 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2267 /* The declaration is a specialization of a member
2268 template, declared outside the class. Therefore, the
2269 innermost template arguments will be NULL, so we
2270 replace them with the arguments determined by the
2271 earlier call to check_explicit_specialization. */
2272 args
= DECL_TI_ARGS (decl
);
2275 = build_template_decl (decl
, current_template_parms
);
2276 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2277 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2278 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2279 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2280 DECL_TEMPLATE_INFO (new_tmpl
) =
2281 perm_tree_cons (tmpl
, args
, NULL_TREE
);
2283 register_specialization (new_tmpl
, tmpl
, args
);
2287 /* Make sure the template headers we got make sense. */
2289 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2290 i
= TMPL_PARMS_DEPTH (parms
);
2291 if (TMPL_ARGS_DEPTH (args
) != i
)
2293 cp_error ("expected %d levels of template parms for `%#D', got %d",
2294 i
, decl
, TMPL_ARGS_DEPTH (args
));
2297 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2299 a
= TMPL_ARGS_LEVEL (args
, i
);
2300 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2302 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2304 if (current
== decl
)
2305 cp_error ("got %d template parameters for `%#D'",
2306 TREE_VEC_LENGTH (a
), decl
);
2308 cp_error ("got %d template parameters for `%#T'",
2309 TREE_VEC_LENGTH (a
), current
);
2310 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2313 /* Perhaps we should also check that the parms are used in the
2314 appropriate qualifying scopes in the declarator? */
2316 if (current
== decl
)
2319 current
= TYPE_CONTEXT (current
);
2323 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2324 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2326 /* Push template declarations for global functions and types. Note
2327 that we do not try to push a global template friend declared in a
2328 template class; such a thing may well depend on the template
2329 parameters of the class. */
2331 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2332 tmpl
= pushdecl_namespace_level (tmpl
);
2335 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2337 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
2339 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2341 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2342 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2343 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2344 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2346 else if (! DECL_LANG_SPECIFIC (decl
))
2347 cp_error ("template declaration of `%#D'", decl
);
2349 DECL_TEMPLATE_INFO (decl
) = info
;
2351 return DECL_TEMPLATE_RESULT (tmpl
);
2355 push_template_decl (decl
)
2358 return push_template_decl_real (decl
, 0);
2361 /* Called when a class template TYPE is redeclared with the indicated
2362 template PARMS, e.g.:
2364 template <class T> struct S;
2365 template <class T> struct S {}; */
2368 redeclare_class_template (type
, parms
)
2372 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2376 if (!PRIMARY_TEMPLATE_P (tmpl
))
2377 /* The type is nested in some template class. Nothing to worry
2378 about here; there are no new template parameters for the nested
2382 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2383 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2385 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2387 cp_error_at ("previous declaration `%D'", tmpl
);
2388 cp_error ("used %d template parameter%s instead of %d",
2389 TREE_VEC_LENGTH (tmpl_parms
),
2390 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2391 TREE_VEC_LENGTH (parms
));
2395 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2397 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2398 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2399 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2400 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2402 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2404 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2405 cp_error ("redeclared here as `%#D'", parm
);
2409 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2411 /* We have in [temp.param]:
2413 A template-parameter may not be given default arguments
2414 by two different declarations in the same scope. */
2415 cp_error ("redefinition of default argument for `%#D'", parm
);
2416 cp_error_at (" original definition appeared here", tmpl_parm
);
2420 if (parm_default
!= NULL_TREE
)
2421 /* Update the previous template parameters (which are the ones
2422 that will really count) with the new default value. */
2423 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2427 /* Attempt to convert the non-type template parameter EXPR to the
2428 indicated TYPE. If the conversion is successful, return the
2429 converted value. If the conversion is unsuccesful, return
2430 NULL_TREE if we issued an error message, or error_mark_node if we
2431 did not. We issue error messages for out-and-out bad template
2432 parameters, but not simply because the conversion failed, since we
2433 might be just trying to do argument deduction. By the time this
2434 function is called, neither TYPE nor EXPR may make use of template
2438 convert_nontype_argument (type
, expr
)
2442 tree expr_type
= TREE_TYPE (expr
);
2444 /* A template-argument for a non-type, non-template
2445 template-parameter shall be one of:
2447 --an integral constant-expression of integral or enumeration
2450 --the name of a non-type template-parameter; or
2452 --the name of an object or function with external linkage,
2453 including function templates and function template-ids but
2454 excluding non-static class members, expressed as id-expression;
2457 --the address of an object or function with external linkage,
2458 including function templates and function template-ids but
2459 excluding non-static class members, expressed as & id-expression
2460 where the & is optional if the name refers to a function or
2463 --a pointer to member expressed as described in _expr.unary.op_. */
2465 /* An integral constant-expression can include const variables
2467 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
2468 expr
= decl_constant_value (expr
);
2470 if (is_overloaded_fn (expr
))
2471 /* OK for now. We'll check that it has external linkage later.
2472 Check this first since if expr_type is the unknown_type_node
2473 we would otherwise complain below. */
2475 else if (TYPE_PTR_P (expr_type
)
2476 || TREE_CODE (expr_type
) == ARRAY_TYPE
2477 || TREE_CODE (type
) == REFERENCE_TYPE
2478 /* If expr is the address of an overloaded function, we
2479 will get the unknown_type_node at this point. */
2480 || expr_type
== unknown_type_node
)
2486 if (TREE_CODE (type
) == REFERENCE_TYPE
2487 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2491 if (TREE_CODE (e
) != ADDR_EXPR
)
2494 cp_error ("`%E' is not a valid template argument", expr
);
2495 error ("it must be %s%s with external linkage",
2496 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
2497 ? "a pointer to " : "",
2498 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
2499 ? "a function" : "an object");
2503 referent
= TREE_OPERAND (e
, 0);
2504 STRIP_NOPS (referent
);
2507 if (TREE_CODE (referent
) == STRING_CST
)
2509 cp_error ("string literal %E is not a valid template argument",
2511 error ("because it is the address of an object with static linkage");
2515 if (is_overloaded_fn (referent
))
2516 /* We'll check that it has external linkage later. */
2518 else if (TREE_CODE (referent
) != VAR_DECL
)
2520 else if (!TREE_PUBLIC (referent
))
2522 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2523 return error_mark_node
;
2526 else if (INTEGRAL_TYPE_P (expr_type
)
2527 || TYPE_PTRMEM_P (expr_type
)
2528 || TYPE_PTRMEMFUNC_P (expr_type
)
2529 /* The next two are g++ extensions. */
2530 || TREE_CODE (expr_type
) == REAL_TYPE
2531 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2533 if (! TREE_CONSTANT (expr
))
2536 cp_error ("non-constant `%E' cannot be used as template argument",
2543 cp_error ("object `%E' cannot be used as template argument", expr
);
2547 switch (TREE_CODE (type
))
2552 /* For a non-type template-parameter of integral or enumeration
2553 type, integral promotions (_conv.prom_) and integral
2554 conversions (_conv.integral_) are applied. */
2555 if (!INTEGRAL_TYPE_P (expr_type
))
2556 return error_mark_node
;
2558 /* It's safe to call digest_init in this case; we know we're
2559 just converting one integral constant expression to another. */
2560 expr
= digest_init (type
, expr
, (tree
*) 0);
2562 if (TREE_CODE (expr
) != INTEGER_CST
)
2563 /* Curiously, some TREE_CONSTANT integral expressions do not
2564 simplify to integer constants. For example, `3 % 0',
2565 remains a TRUNC_MOD_EXPR. */
2572 /* These are g++ extensions. */
2573 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2574 return error_mark_node
;
2576 expr
= digest_init (type
, expr
, (tree
*) 0);
2578 if (TREE_CODE (expr
) != REAL_CST
)
2585 tree type_pointed_to
= TREE_TYPE (type
);
2587 if (TYPE_PTRMEM_P (type
))
2588 /* For a non-type template-parameter of type pointer to data
2589 member, qualification conversions (_conv.qual_) are
2591 return perform_qualification_conversions (type
, expr
);
2592 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2594 /* For a non-type template-parameter of type pointer to
2595 function, only the function-to-pointer conversion
2596 (_conv.func_) is applied. If the template-argument
2597 represents a set of overloaded functions (or a pointer to
2598 such), the matching function is selected from the set
2603 if (TREE_CODE (expr
) == ADDR_EXPR
)
2604 fns
= TREE_OPERAND (expr
, 0);
2608 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2610 if (fn
== error_mark_node
)
2611 return error_mark_node
;
2613 if (!TREE_PUBLIC (fn
))
2615 if (really_overloaded_fn (fns
))
2616 return error_mark_node
;
2621 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2623 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2629 /* For a non-type template-parameter of type pointer to
2630 object, qualification conversions (_conv.qual_) and the
2631 array-to-pointer conversion (_conv.array_) are applied.
2632 [Note: In particular, neither the null pointer conversion
2633 (_conv.ptr_) nor the derived-to-base conversion
2634 (_conv.ptr_) are applied. Although 0 is a valid
2635 template-argument for a non-type template-parameter of
2636 integral type, it is not a valid template-argument for a
2637 non-type template-parameter of pointer type.]
2639 The call to decay_conversion performs the
2640 array-to-pointer conversion, if appropriate. */
2641 expr
= decay_conversion (expr
);
2643 if (expr
== error_mark_node
)
2644 return error_mark_node
;
2646 return perform_qualification_conversions (type
, expr
);
2651 case REFERENCE_TYPE
:
2653 tree type_referred_to
= TREE_TYPE (type
);
2655 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2657 /* For a non-type template-parameter of type reference to
2658 function, no conversions apply. If the
2659 template-argument represents a set of overloaded
2660 functions, the matching function is selected from the
2661 set (_over.over_). */
2665 fn
= instantiate_type (type_referred_to
, fns
, 0);
2667 if (fn
== error_mark_node
)
2668 return error_mark_node
;
2670 if (!TREE_PUBLIC (fn
))
2672 if (really_overloaded_fn (fns
))
2673 /* Don't issue an error here; we might get a different
2674 function if the overloading had worked out
2676 return error_mark_node
;
2681 my_friendly_assert (same_type_p (type_referred_to
,
2689 /* For a non-type template-parameter of type reference to
2690 object, no conversions apply. The type referred to by the
2691 reference may be more cv-qualified than the (otherwise
2692 identical) type of the template-argument. The
2693 template-parameter is bound directly to the
2694 template-argument, which must be an lvalue. */
2695 if ((TYPE_MAIN_VARIANT (expr_type
)
2696 != TYPE_MAIN_VARIANT (type_referred_to
))
2697 || !at_least_as_qualified_p (type_referred_to
,
2699 || !real_lvalue_p (expr
))
2700 return error_mark_node
;
2709 if (!TYPE_PTRMEMFUNC_P (type
))
2710 /* This handles templates like
2711 template<class T, T t> void f();
2712 when T is substituted with any class. The second template
2713 parameter becomes invalid and the template candidate is
2715 return error_mark_node
;
2717 /* For a non-type template-parameter of type pointer to member
2718 function, no conversions apply. If the template-argument
2719 represents a set of overloaded member functions, the
2720 matching member function is selected from the set
2723 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2724 expr_type
!= unknown_type_node
)
2725 return error_mark_node
;
2727 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2729 /* A ptr-to-member constant. */
2730 if (!same_type_p (type
, expr_type
))
2731 return error_mark_node
;
2736 if (TREE_CODE (expr
) != ADDR_EXPR
)
2737 return error_mark_node
;
2739 expr
= instantiate_type (type
, expr
, 0);
2741 if (expr
== error_mark_node
)
2742 return error_mark_node
;
2744 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2751 /* All non-type parameters must have one of these types. */
2752 my_friendly_abort (0);
2756 return error_mark_node
;
2759 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2760 template template parameters. Both PARM_PARMS and ARG_PARMS are
2761 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2764 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2765 the case, then extra parameters must have default arguments.
2767 Consider the example:
2768 template <class T, class Allocator = allocator> class vector;
2769 template<template <class U> class TT> class C;
2771 C<vector> is a valid instantiation. PARM_PARMS for the above code
2772 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2773 T and Allocator) and OUTER_ARGS contains the argument that is used to
2774 substitute the TT parameter. */
2777 coerce_template_template_parms (parm_parms
, arg_parms
, in_decl
, outer_args
)
2778 tree parm_parms
, arg_parms
, in_decl
, outer_args
;
2780 int nparms
, nargs
, i
;
2783 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
2784 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
2786 nparms
= TREE_VEC_LENGTH (parm_parms
);
2787 nargs
= TREE_VEC_LENGTH (arg_parms
);
2789 /* The rule here is opposite of coerce_template_parms. */
2792 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
2795 for (i
= 0; i
< nparms
; ++i
)
2797 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
2798 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
2800 if (arg
== NULL_TREE
|| arg
== error_mark_node
2801 || parm
== NULL_TREE
|| parm
== error_mark_node
)
2804 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2807 switch (TREE_CODE (parm
))
2813 /* We encounter instantiations of templates like
2814 template <template <template <class> class> class TT>
2817 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2818 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2820 if (!coerce_template_template_parms (parmparm
, argparm
,
2821 in_decl
, outer_args
))
2827 /* The tsubst call is used to handle cases such as
2828 template <class T, template <T> class TT> class D;
2829 i.e. the parameter list of TT depends on earlier parameters. */
2830 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
, in_decl
),
2836 my_friendly_abort (0);
2842 /* Convert the indicated template ARG as necessary to match the
2843 indicated template PARM. Returns the converted ARG, or
2844 error_mark_node if the conversion was unsuccessful. Error messages
2845 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2846 parameter in the parameter list. ARGS is the full set of template
2847 arguments deduced so far. */
2850 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
2860 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2862 inner_args
= innermost_args (args
);
2864 if (TREE_CODE (arg
) == TREE_LIST
2865 && TREE_TYPE (arg
) != NULL_TREE
2866 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2868 /* The template argument was the name of some
2869 member function. That's usually
2870 illegal, but static members are OK. In any
2871 case, grab the underlying fields/functions
2872 and issue an error later if required. */
2873 arg
= TREE_VALUE (arg
);
2874 TREE_TYPE (arg
) = unknown_type_node
;
2877 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
2878 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
2879 || requires_tmpl_type
);
2881 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2882 we also accept implicitly created TYPE_DECL as a valid argument.
2883 This is necessary to handle the case where we pass a template name
2884 to a template template parameter in a scope where we've derived from
2885 in instantiation of that template, so the template name refers to that
2886 instantiation. We really ought to handle this better. */
2888 = ((TREE_CODE (arg
) == TEMPLATE_DECL
2889 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
2890 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
2891 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
2892 || (TREE_CODE (arg
) == RECORD_TYPE
2893 && CLASSTYPE_TEMPLATE_INFO (arg
)
2894 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
2895 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
2896 && requires_tmpl_type
2897 && is_base_of_enclosing_class (arg
, current_class_type
)));
2898 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
2899 arg
= TYPE_STUB_DECL (arg
);
2900 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
2901 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
2903 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
2905 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
2906 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
2908 cp_pedwarn ("to refer to a type member of a template parameter,");
2909 cp_pedwarn (" use `typename %E'", arg
);
2911 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
2912 TREE_OPERAND (arg
, 1));
2915 if (is_type
!= requires_type
)
2921 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2924 cp_error (" expected a constant of type `%T', got `%T'",
2926 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2928 cp_error (" expected a type, got `%E'", arg
);
2931 return error_mark_node
;
2933 if (is_tmpl_type
^ requires_tmpl_type
)
2935 if (in_decl
&& complain
)
2937 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2940 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2942 cp_error (" expected a class template, got `%T'", arg
);
2944 return error_mark_node
;
2949 if (requires_tmpl_type
)
2951 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2952 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2954 if (coerce_template_template_parms (parmparm
, argparm
,
2955 in_decl
, inner_args
))
2959 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2961 if (val
!= error_mark_node
2962 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2963 val
= TREE_TYPE (val
);
2967 if (in_decl
&& complain
)
2969 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2971 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
2974 val
= error_mark_node
;
2979 val
= groktypename (arg
);
2980 if (! processing_template_decl
)
2982 /* [basic.link]: A name with no linkage (notably, the
2983 name of a class or enumeration declared in a local
2984 scope) shall not be used to declare an entity with
2985 linkage. This implies that names with no linkage
2986 cannot be used as template arguments. */
2987 tree t
= no_linkage_check (val
);
2990 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
2992 ("template-argument `%T' uses anonymous type", val
);
2995 ("template-argument `%T' uses local type `%T'",
2997 return error_mark_node
;
3004 tree t
= tsubst (TREE_TYPE (parm
), args
, in_decl
);
3006 if (processing_template_decl
)
3007 arg
= maybe_fold_nontype_arg (arg
);
3009 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3010 /* We used to call digest_init here. However, digest_init
3011 will report errors, which we don't want when complain
3012 is zero. More importantly, digest_init will try too
3013 hard to convert things: for example, `0' should not be
3014 converted to pointer type at this point according to
3015 the standard. Accepting this is not merely an
3016 extension, since deciding whether or not these
3017 conversions can occur is part of determining which
3018 function template to call, or whether a given epxlicit
3019 argument specification is legal. */
3020 val
= convert_nontype_argument (t
, arg
);
3024 if (val
== NULL_TREE
)
3025 val
= error_mark_node
;
3026 else if (val
== error_mark_node
&& complain
)
3027 cp_error ("could not convert template argument `%E' to `%T'",
3034 /* Convert all template arguments to their appropriate types, and
3035 return a vector containing the innermost resulting template
3036 arguments. If any error occurs, return error_mark_node, and, if
3037 COMPLAIN is non-zero, issue an error message. Some error messages
3038 are issued even if COMPLAIN is zero; for instance, if a template
3039 argument is composed from a local class.
3041 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3042 provided in ARGLIST, or else trailing parameters must have default
3043 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3044 deduction for any unspecified trailing arguments.
3046 The resulting TREE_VEC is allocated on a temporary obstack, and
3047 must be explicitly copied if it will be permanent. */
3050 coerce_template_parms (parms
, args
, in_decl
,
3052 require_all_arguments
)
3056 int require_all_arguments
;
3058 int nparms
, nargs
, i
, lost
= 0;
3061 tree new_inner_args
;
3063 inner_args
= innermost_args (args
);
3064 nargs
= NUM_TMPL_ARGS (inner_args
);
3065 nparms
= TREE_VEC_LENGTH (parms
);
3069 && require_all_arguments
3070 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3074 cp_error ("wrong number of template arguments (%d, should be %d)",
3078 cp_error_at ("provided for `%D'", in_decl
);
3081 return error_mark_node
;
3084 new_inner_args
= make_temp_vec (nparms
);
3085 new_args
= add_outermost_template_args (args
, new_inner_args
);
3086 for (i
= 0; i
< nparms
; i
++)
3091 /* Get the Ith template parameter. */
3092 parm
= TREE_VEC_ELT (parms
, i
);
3094 /* Calculate the Ith argument. */
3095 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3097 arg
= TREE_VALUE (inner_args
);
3098 inner_args
= TREE_CHAIN (inner_args
);
3101 arg
= TREE_VEC_ELT (inner_args
, i
);
3102 /* If no template argument was supplied, look for a default
3104 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3106 /* There was no default value. */
3107 my_friendly_assert (!require_all_arguments
, 0);
3110 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3111 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, in_decl
);
3113 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, in_decl
);
3115 /* Now, convert the Ith argument, as necessary. */
3116 if (arg
== NULL_TREE
)
3117 /* We're out of arguments. */
3119 my_friendly_assert (!require_all_arguments
, 0);
3122 else if (arg
== error_mark_node
)
3124 cp_error ("template argument %d is invalid", i
+ 1);
3125 arg
= error_mark_node
;
3128 arg
= convert_template_argument (TREE_VALUE (parm
),
3129 arg
, new_args
, complain
, i
,
3132 if (arg
== error_mark_node
)
3134 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3138 return error_mark_node
;
3140 return new_inner_args
;
3143 /* Returns 1 if template args OT and NT are equivalent. */
3146 template_args_equal (ot
, nt
)
3151 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3153 if (TREE_CODE (nt
) == TREE_VEC
)
3154 /* For member templates */
3155 return comp_template_args (ot
, nt
);
3156 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3157 return same_type_p (ot
, nt
);
3159 return (cp_tree_equal (ot
, nt
) > 0);
3162 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3163 of template arguments. Returns 0 otherwise. */
3166 comp_template_args (oldargs
, newargs
)
3167 tree oldargs
, newargs
;
3171 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3174 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3176 tree nt
= TREE_VEC_ELT (newargs
, i
);
3177 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3179 if (! template_args_equal (ot
, nt
))
3185 /* Given class template name and parameter list, produce a user-friendly name
3186 for the instantiation. */
3189 mangle_class_name_for_template (name
, parms
, arglist
)
3191 tree parms
, arglist
;
3193 static struct obstack scratch_obstack
;
3194 static char *scratch_firstobj
;
3197 if (!scratch_firstobj
)
3198 gcc_obstack_init (&scratch_obstack
);
3200 obstack_free (&scratch_obstack
, scratch_firstobj
);
3201 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3203 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3204 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3208 nparms
= TREE_VEC_LENGTH (parms
);
3209 arglist
= innermost_args (arglist
);
3210 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3211 for (i
= 0; i
< nparms
; i
++)
3213 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3214 tree arg
= TREE_VEC_ELT (arglist
, i
);
3219 if (TREE_CODE (parm
) == TYPE_DECL
)
3221 cat (type_as_string_real (arg
, 0, 1));
3224 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3226 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3228 /* Already substituted with real template. Just output
3229 the template name here */
3230 tree context
= DECL_CONTEXT (arg
);
3233 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
, 980422);
3234 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3237 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3240 /* Output the parameter declaration */
3241 cat (type_as_string_real (arg
, 0, 1));
3245 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3247 if (TREE_CODE (arg
) == TREE_LIST
)
3249 /* New list cell was built because old chain link was in
3251 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3252 arg
= TREE_VALUE (arg
);
3254 /* No need to check arglist against parmlist here; we did that
3255 in coerce_template_parms, called from lookup_template_class. */
3256 cat (expr_as_string (arg
, 0));
3259 char *bufp
= obstack_next_free (&scratch_obstack
);
3261 while (bufp
[offset
- 1] == ' ')
3263 obstack_blank_fast (&scratch_obstack
, offset
);
3265 /* B<C<char> >, not B<C<char>> */
3266 if (bufp
[offset
- 1] == '>')
3271 return (char *) obstack_base (&scratch_obstack
);
3275 classtype_mangled_name (t
)
3278 if (CLASSTYPE_TEMPLATE_INFO (t
)
3279 /* Specializations have already had their names set up in
3280 lookup_template_class. */
3281 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3283 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3285 /* For non-primary templates, the template parameters are
3286 implicit from their surrounding context. */
3287 if (PRIMARY_TEMPLATE_P (tmpl
))
3289 tree name
= DECL_NAME (tmpl
);
3290 char *mangled_name
= mangle_class_name_for_template
3291 (IDENTIFIER_POINTER (name
),
3292 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3293 CLASSTYPE_TI_ARGS (t
));
3294 tree id
= get_identifier (mangled_name
);
3295 IDENTIFIER_TEMPLATE (id
) = name
;
3300 return TYPE_IDENTIFIER (t
);
3304 add_pending_template (d
)
3309 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3310 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3312 ti
= DECL_TEMPLATE_INFO (d
);
3314 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3317 *template_tail
= perm_tree_cons
3318 (build_srcloc_here (), d
, NULL_TREE
);
3319 template_tail
= &TREE_CHAIN (*template_tail
);
3320 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3324 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3325 may be either a _DECL or an overloaded function or an
3326 IDENTIFIER_NODE), and ARGLIST. */
3329 lookup_template_function (fns
, arglist
)
3334 if (fns
== NULL_TREE
)
3336 cp_error ("non-template used as template");
3337 return error_mark_node
;
3340 type
= TREE_TYPE (fns
);
3341 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3342 type
= unknown_type_node
;
3344 if (processing_template_decl
)
3345 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3347 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3350 /* Within the scope of a template class S<T>, the name S gets bound
3351 (in build_self_reference) to a TYPE_DECL for the class, not a
3352 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3353 or one of its enclosing classes, and that type is a template,
3354 return the associated TEMPLATE_DECL. Otherwise, the original
3355 DECL is returned. */
3358 maybe_get_template_decl_from_type_decl (decl
)
3361 return (decl
!= NULL_TREE
3362 && TREE_CODE (decl
) == TYPE_DECL
3363 && DECL_ARTIFICIAL (decl
)
3364 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3365 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3368 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3369 parameters, find the desired type.
3371 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3372 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3373 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3374 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3375 copy it here to keep it from being reclaimed when the decl storage
3378 IN_DECL, if non-NULL, is the template declaration we are trying to
3381 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3382 the class we are looking up.
3384 If the template class is really a local class in a template
3385 function, then the FUNCTION_CONTEXT is the function in which it is
3386 being instantiated. */
3389 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3395 tree
template = NULL_TREE
, parmlist
;
3398 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3400 if (IDENTIFIER_VALUE (d1
)
3401 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3402 template = IDENTIFIER_VALUE (d1
);
3406 push_decl_namespace (context
);
3407 if (current_class_type
!= NULL_TREE
)
3409 maybe_get_template_decl_from_type_decl
3410 (IDENTIFIER_CLASS_VALUE (d1
));
3411 if (template == NULL_TREE
)
3412 template = lookup_name_nonclass (d1
);
3414 pop_decl_namespace ();
3417 context
= DECL_CONTEXT (template);
3419 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3421 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)))
3423 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
3424 d1
= DECL_NAME (template);
3427 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3428 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3429 && IS_AGGR_TYPE (d1
)))
3431 template = TYPE_TI_TEMPLATE (d1
);
3432 d1
= DECL_NAME (template);
3434 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3435 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3438 d1
= DECL_NAME (template);
3439 context
= DECL_CONTEXT (template);
3442 my_friendly_abort (272);
3444 /* With something like `template <class T> class X class X { ... };'
3445 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3446 We don't want to do that, but we have to deal with the situation,
3447 so let's give them some syntax errors to chew on instead of a
3451 cp_error ("`%T' is not a template", d1
);
3452 return error_mark_node
;
3455 if (context
== NULL_TREE
)
3456 context
= global_namespace
;
3458 if (TREE_CODE (template) != TEMPLATE_DECL
)
3460 cp_error ("non-template type `%T' used as a template", d1
);
3462 cp_error_at ("for template declaration `%D'", in_decl
);
3463 return error_mark_node
;
3466 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3468 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3469 template arguments */
3471 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3472 tree template2
= TYPE_STUB_DECL (parm
);
3475 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3477 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3478 if (arglist2
== error_mark_node
)
3479 return error_mark_node
;
3481 arglist2
= copy_to_permanent (arglist2
);
3482 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3483 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
3484 TYPE_SIZE (parm
) = 0;
3489 tree template_type
= TREE_TYPE (template);
3491 tree found
= NULL_TREE
;
3494 int is_partial_instantiation
;
3496 template = most_general_template (template);
3497 parmlist
= DECL_TEMPLATE_PARMS (template);
3498 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3499 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3501 /* We build up the coerced arguments and such on the
3502 momentary_obstack. */
3505 if (arg_depth
== 1 && parm_depth
> 1)
3507 /* We've been given an incomplete set of template arguments.
3510 template <class T> struct S1 {
3511 template <class U> struct S2 {};
3512 template <class U> struct S2<U*> {};
3515 we will be called with an ARGLIST of `U*', but the
3516 TEMPLATE will be `template <class T> template
3517 <class U> struct S1<T>::S2'. We must fill in the missing
3519 my_friendly_assert (context
!= NULL_TREE
, 0);
3520 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context
))
3521 && context
!= global_namespace
)
3522 context
= DECL_REAL_CONTEXT (context
);
3524 if (context
== global_namespace
)
3525 /* This is bad. We cannot get enough arguments, even from
3526 the surrounding context, to resolve this class. One
3527 case where this might happen is (illegal) code like:
3535 We should catch this error sooner (at the opening curly
3536 for `S', but it is better to be safe than sorry here. */
3538 cp_error ("invalid use of `%D'", template);
3539 return error_mark_node
;
3542 arglist
= add_to_template_args (TYPE_TI_ARGS (context
),
3544 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3547 my_friendly_assert (parm_depth
== arg_depth
, 0);
3549 /* Calculate the BOUND_ARGS. These will be the args that are
3550 actually tsubst'd into the definition to create the
3554 /* We have multiple levels of arguments to coerce, at once. */
3556 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3558 tree bound_args
= make_temp_vec (parm_depth
);
3560 for (i
= saved_depth
,
3561 t
= DECL_TEMPLATE_PARMS (template);
3562 i
> 0 && t
!= NULL_TREE
;
3563 --i
, t
= TREE_CHAIN (t
))
3565 tree a
= coerce_template_parms (TREE_VALUE (t
),
3566 arglist
, template, 1, 1);
3567 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3569 /* We temporarily reduce the length of the ARGLIST so
3570 that coerce_template_parms will see only the arguments
3571 corresponding to the template parameters it is
3573 TREE_VEC_LENGTH (arglist
)--;
3576 /* Restore the ARGLIST to its full size. */
3577 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3579 arglist
= bound_args
;
3583 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3584 innermost_args (arglist
),
3587 if (arglist
== error_mark_node
)
3588 /* We were unable to bind the arguments. */
3589 return error_mark_node
;
3591 /* In the scope of a template class, explicit references to the
3592 template class refer to the type of the template, not any
3593 instantiation of it. For example, in:
3595 template <class T> class C { void f(C<T>); }
3597 the `C<T>' is just the same as `C'. Outside of the
3598 class, however, such a reference is an instantiation. */
3599 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3602 found
= template_type
;
3604 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3608 /* Note that we use DECL_CONTEXT, rather than
3609 CP_DECL_CONTEXT, so that the termination test is
3610 always just `ctx'. We're not interested in namepace
3612 for (ctx
= current_class_type
;
3614 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3615 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3616 if (same_type_p (ctx
, template_type
))
3620 /* We're not in the scope of the class, so the
3621 TEMPLATE_TYPE is not the type we want after
3629 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3630 found
; found
= TREE_CHAIN (found
))
3631 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3635 found
= TREE_VALUE (found
);
3644 /* Since we didn't find the type, we'll have to create it.
3645 Since we'll be saving this type on the
3646 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3647 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3649 /* This type is a "partial instantiation" if any of the template
3650 arguments still inolve template parameters. Note that we set
3651 IS_PARTIAL_INSTANTIATION for partial specializations as
3653 is_partial_instantiation
= uses_template_parms (arglist
);
3655 /* Create the type. */
3656 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3658 if (!is_partial_instantiation
)
3659 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3661 /* We don't want to call start_enum for this type, since
3662 the values for the enumeration constants may involve
3663 template parameters. And, no one should be interested
3664 in the enumeration constants for such a type. */
3665 t
= make_node (ENUMERAL_TYPE
);
3669 t
= make_lang_type (TREE_CODE (template_type
));
3670 CLASSTYPE_DECLARED_CLASS (t
)
3671 = CLASSTYPE_DECLARED_CLASS (template_type
);
3672 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3673 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3674 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3677 /* If we called start_enum above, this information will already
3681 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3683 /* Create a stub TYPE_DECL for it. */
3684 type_decl
= build_decl (TYPE_DECL
, DECL_NAME (template), t
);
3685 SET_DECL_ARTIFICIAL (type_decl
);
3686 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3687 DECL_SOURCE_FILE (type_decl
)
3688 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
3689 DECL_SOURCE_LINE (type_decl
)
3690 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
3691 TYPE_STUB_DECL (t
) = TYPE_NAME (t
) = type_decl
;
3694 type_decl
= TYPE_NAME (t
);
3696 /* Set up the template information. We have to figure out which
3697 template is the immediate parent if this is a full
3699 if (parm_depth
== 1 || is_partial_instantiation
3700 || !PRIMARY_TEMPLATE_P (template))
3701 /* This case is easy; there are no member templates involved. */
3705 /* This is a full instantiation of a member template. There
3706 should be some partial instantiation of which this is an
3709 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3710 found
; found
= TREE_CHAIN (found
))
3713 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
3715 /* We only want partial instantiations, here, not
3716 specializations or full instantiations. */
3717 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
3718 || !uses_template_parms (TREE_VALUE (found
)))
3721 /* Temporarily reduce by one the number of levels in the
3722 ARGLIST and in FOUND so as to avoid comparing the
3723 last set of arguments. */
3724 TREE_VEC_LENGTH (arglist
)--;
3725 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
3727 /* See if the arguments match. If they do, then TMPL is
3728 the partial instantiation we want. */
3729 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
3731 /* Restore the argument vectors to their full size. */
3732 TREE_VEC_LENGTH (arglist
)++;
3733 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
3743 my_friendly_abort (0);
3746 arglist
= copy_to_permanent (arglist
);
3747 SET_TYPE_TEMPLATE_INFO (t
,
3748 tree_cons (found
, arglist
, NULL_TREE
));
3749 DECL_TEMPLATE_INSTANTIATIONS (template)
3750 = tree_cons (arglist
, t
,
3751 DECL_TEMPLATE_INSTANTIATIONS (template));
3753 if (TREE_CODE (t
) == ENUMERAL_TYPE
3754 && !is_partial_instantiation
)
3755 /* Now that the type has been registered on the instantiations
3756 list, we set up the enumerators. Because the enumeration
3757 constants may involve the enumeration type itself, we make
3758 sure to register the type first, and then create the
3759 constants. That way, doing tsubst_expr for the enumeration
3760 constants won't result in recursive calls here; we'll find
3761 the instantiation and exit above. */
3762 tsubst_enum (template_type
, t
, arglist
);
3764 /* We're done with the permanent obstack, now. */
3766 /* We're also done with the momentary allocation we started
3770 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3772 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
3773 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
3774 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
3775 if (!is_partial_instantiation
)
3777 DECL_ASSEMBLER_NAME (type_decl
)
3778 = get_identifier (build_overload_name (t
, 1, 1));
3780 /* For backwards compatibility; code that uses
3781 -fexternal-templates expects looking up a template to
3782 instantiate it. I think DDD still relies on this.
3783 (jason 8/20/1998) */
3784 if (TREE_CODE (t
) != ENUMERAL_TYPE
3785 && flag_external_templates
3786 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3787 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3788 add_pending_template (t
);
3791 /* If the type makes use of template parameters, the
3792 code that generates debugging information will crash. */
3793 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
3799 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3800 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3801 If FN returns non-zero, the iteration is terminated, and
3802 for_each_template_parm returns 1. Otherwise, the iteration
3803 continues. If FN never returns a non-zero value, the value
3804 returned by for_each_template_parm is 0. If FN is NULL, it is
3805 considered to be the function which always returns 1. */
3808 for_each_template_parm (t
, fn
, data
)
3816 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
3817 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
3820 switch (TREE_CODE (t
))
3824 /* We assume that the object must be instantiated in order to build
3825 the COMPONENT_REF, so we test only whether the type of the
3826 COMPONENT_REF uses template parms. */
3827 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3830 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
3831 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
3833 case IDENTIFIER_NODE
:
3834 if (!IDENTIFIER_TEMPLATE (t
))
3836 my_friendly_abort (42);
3838 /* aggregates of tree nodes */
3841 int i
= TREE_VEC_LENGTH (t
);
3843 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
3848 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
3849 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
3851 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
3854 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
3856 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
3858 /* constructed type nodes */
3860 case REFERENCE_TYPE
:
3861 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3864 if (TYPE_PTRMEMFUNC_FLAG (t
))
3865 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
3871 if (! TYPE_TEMPLATE_INFO (t
))
3873 return for_each_template_parm (TREE_VALUE
3874 (TYPE_TEMPLATE_INFO (t
)),
3877 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
3882 /* Check the parameter types. Since default arguments are not
3883 instantiated until they are needed, the TYPE_ARG_TYPES may
3884 contain expressions that involve template parameters. But,
3885 no-one should be looking at them yet. And, once they're
3886 instantiated, they don't contain template parameters, so
3887 there's no point in looking at them then, either. */
3891 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
3892 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
3896 /* Check the return type, too. */
3897 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3900 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
3902 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3904 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
3906 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3910 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3913 /* A template template parameter is encountered */
3914 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3915 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3916 /* Already substituted template template parameter */
3920 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
3922 goto check_type_and_context
;
3926 /* ??? What about FIELD_DECLs? */
3927 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
3928 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
3932 check_type_and_context
:
3933 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
3935 if (DECL_CONTEXT (t
)
3936 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
3941 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
3942 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
3945 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3947 /* template parm nodes */
3948 case TEMPLATE_TEMPLATE_PARM
:
3949 /* Record template parameters such as `T' inside `TT<T>'. */
3950 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
3951 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
3953 case TEMPLATE_TYPE_PARM
:
3954 case TEMPLATE_PARM_INDEX
:
3956 return (*fn
)(t
, data
);
3960 /* simple type nodes */
3962 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
3964 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
3970 case NAMESPACE_DECL
:
3980 /* Non-error_mark_node ERROR_MARKs are bad things. */
3981 my_friendly_assert (t
== error_mark_node
, 274);
3990 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3993 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3996 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
3997 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3998 (TREE_TYPE (t
)), fn
, data
);
3999 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
4003 case REINTERPRET_CAST_EXPR
:
4004 case CONST_CAST_EXPR
:
4005 case STATIC_CAST_EXPR
:
4006 case DYNAMIC_CAST_EXPR
:
4014 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4017 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
4025 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
4026 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
4033 sorry ("testing %s for template parms",
4034 tree_code_name
[(int) TREE_CODE (t
)]);
4035 my_friendly_abort (82);
4042 uses_template_parms (t
)
4045 return for_each_template_parm (t
, 0, 0);
4048 static struct tinst_level
*current_tinst_level
;
4049 static struct tinst_level
*free_tinst_level
;
4050 static int tinst_depth
;
4051 extern int max_tinst_depth
;
4052 #ifdef GATHER_STATISTICS
4055 int tinst_level_tick
;
4056 int last_template_error_tick
;
4058 /* Print out all the template instantiations that we are currently
4059 working on. If ERR, we are being called from cp_thing, so do
4060 the right thing for an error message. */
4063 print_template_context (err
)
4066 struct tinst_level
*p
= current_tinst_level
;
4068 char *file
= input_filename
;
4072 if (current_function_decl
!= p
->decl
4073 && current_function_decl
!= NULL_TREE
)
4074 /* We can get here during the processing of some synthesized
4075 method. Then, p->decl will be the function that's causing
4080 if (current_function_decl
== p
->decl
)
4081 /* Avoid redundancy with the the "In function" line. */;
4083 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4084 file
, decl_as_string (p
->decl
, 0));
4092 for (; p
; p
= p
->next
)
4094 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4095 decl_as_string (p
->decl
, 0));
4099 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4102 /* Called from cp_thing to print the template context for an error. */
4105 maybe_print_template_context ()
4107 if (last_template_error_tick
== tinst_level_tick
4108 || current_tinst_level
== 0)
4111 last_template_error_tick
= tinst_level_tick
;
4112 print_template_context (1);
4116 push_tinst_level (d
)
4119 struct tinst_level
*new;
4121 if (tinst_depth
>= max_tinst_depth
)
4123 /* If the instantiation in question still has unbound template parms,
4124 we don't really care if we can't instantiate it, so just return.
4125 This happens with base instantiation for implicit `typename'. */
4126 if (uses_template_parms (d
))
4129 last_template_error_tick
= tinst_level_tick
;
4130 error ("template instantiation depth exceeds maximum of %d",
4132 error (" (use -ftemplate-depth-NN to increase the maximum)");
4133 cp_error (" instantiating `%D'", d
);
4135 print_template_context (0);
4140 if (free_tinst_level
)
4142 new = free_tinst_level
;
4143 free_tinst_level
= new->next
;
4146 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4150 new->file
= input_filename
;
4151 new->next
= current_tinst_level
;
4152 current_tinst_level
= new;
4155 #ifdef GATHER_STATISTICS
4156 if (tinst_depth
> depth_reached
)
4157 depth_reached
= tinst_depth
;
4167 struct tinst_level
*old
= current_tinst_level
;
4169 /* Restore the filename and line number stashed away when we started
4170 this instantiation. */
4172 input_filename
= old
->file
;
4173 extract_interface_info ();
4175 current_tinst_level
= old
->next
;
4176 old
->next
= free_tinst_level
;
4177 free_tinst_level
= old
;
4182 struct tinst_level
*
4185 struct tinst_level
*p
= current_tinst_level
;
4188 for (; p
->next
; p
= p
->next
)
4193 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4194 vector of template arguments, as for tsubst.
4196 Returns an appropriate tsbust'd friend declaration. */
4199 tsubst_friend_function (decl
, args
)
4205 char *file
= input_filename
;
4207 lineno
= DECL_SOURCE_LINE (decl
);
4208 input_filename
= DECL_SOURCE_FILE (decl
);
4210 if (TREE_CODE (decl
) == FUNCTION_DECL
4211 && DECL_TEMPLATE_INSTANTIATION (decl
)
4212 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4213 /* This was a friend declared with an explicit template
4214 argument list, e.g.:
4218 to indicate that f was a template instantiation, not a new
4219 function declaration. Now, we have to figure out what
4220 instantiation of what template. */
4227 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4229 tsubst (DECL_TI_ARGS (decl
),
4231 /* FIXME: The decl we create via the next tsubst could be
4232 created on a temporary obstack. */
4233 new_friend
= tsubst (decl
, args
, NULL_TREE
);
4234 tmpl
= determine_specialization (template_id
, new_friend
,
4236 /*need_member_template=*/0,
4238 new_friend
= instantiate_template (tmpl
, new_args
);
4242 new_friend
= tsubst (decl
, args
, NULL_TREE
);
4244 /* The NEW_FRIEND will look like an instantiation, to the
4245 compiler, but is not an instantiation from the point of view of
4246 the language. For example, we might have had:
4248 template <class T> struct S {
4249 template <class U> friend void f(T, U);
4252 Then, in S<int>, template <class U> void f(int, U) is not an
4253 instantiation of anything. */
4254 DECL_USE_TEMPLATE (new_friend
) = 0;
4255 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4256 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4258 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4259 tsubst will have resulted in a call to
4260 set_mangled_name_for_template_decl. But, the function is not a
4261 template instantiation and should not be mangled like one.
4262 Therefore, we remangle the function name. We don't have to do
4263 this if the NEW_FRIEND is a template since
4264 set_mangled_name_for_template_decl doesn't do anything if the
4265 function declaration still uses template arguments. */
4266 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4268 set_mangled_name_for_decl (new_friend
);
4269 DECL_RTL (new_friend
) = 0;
4270 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4273 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4276 tree new_friend_template_info
;
4277 tree new_friend_result_template_info
;
4278 int new_friend_is_defn
;
4280 /* We must save some information from NEW_FRIEND before calling
4281 duplicate decls since that function will free NEW_FRIEND if
4283 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4284 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4286 /* This declaration is a `primary' template. */
4287 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4290 = DECL_INITIAL (DECL_RESULT (new_friend
)) != NULL_TREE
;
4291 new_friend_result_template_info
4292 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend
));
4296 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4297 new_friend_result_template_info
= NULL_TREE
;
4300 old_decl
= pushdecl_namespace_level (new_friend
);
4302 if (old_decl
!= new_friend
)
4304 /* This new friend declaration matched an existing
4305 declaration. For example, given:
4307 template <class T> void f(T);
4308 template <class U> class C {
4309 template <class T> friend void f(T) {}
4312 the friend declaration actually provides the definition
4313 of `f', once C has been instantiated for some type. So,
4314 old_decl will be the out-of-class template declaration,
4315 while new_friend is the in-class definition.
4317 But, if `f' was called before this point, the
4318 instantiation of `f' will have DECL_TI_ARGS corresponding
4319 to `T' but not to `U', references to which might appear
4320 in the definition of `f'. Previously, the most general
4321 template for an instantiation of `f' was the out-of-class
4322 version; now it is the in-class version. Therefore, we
4323 run through all specialization of `f', adding to their
4324 DECL_TI_ARGS appropriately. In particular, they need a
4325 new set of outer arguments, corresponding to the
4326 arguments for this class instantiation.
4328 The same situation can arise with something like this:
4331 template <class T> class C {
4335 when `C<int>' is instantiated. Now, `f(int)' is defined
4338 if (!new_friend_is_defn
)
4339 /* On the other hand, if the in-class declaration does
4340 *not* provide a definition, then we don't want to alter
4341 existing definitions. We can just leave everything
4346 /* Overwrite whatever template info was there before, if
4347 any, with the new template information pertaining to
4349 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4351 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4352 /* duplicate_decls will take care of this case. */
4357 tree new_friend_args
;
4359 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl
))
4360 = new_friend_result_template_info
;
4362 new_friend_args
= TI_ARGS (new_friend_template_info
);
4363 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4367 tree spec
= TREE_VALUE (t
);
4370 = add_outermost_template_args (new_friend_args
,
4371 DECL_TI_ARGS (spec
));
4373 = copy_to_permanent (DECL_TI_ARGS (spec
));
4376 /* Now, since specializations are always supposed to
4377 hang off of the most general template, we must move
4379 t
= most_general_template (old_decl
);
4382 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4383 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4384 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4385 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4390 /* The information from NEW_FRIEND has been merged into OLD_DECL
4391 by duplicate_decls. */
4392 new_friend
= old_decl
;
4395 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4397 /* Check to see that the declaration is really present, and,
4398 possibly obtain an improved declaration. */
4399 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4408 input_filename
= file
;
4412 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4413 template arguments, as for tsubst.
4415 Returns an appropriate tsbust'd friend type. */
4418 tsubst_friend_class (friend_tmpl
, args
)
4423 tree tmpl
= lookup_name (DECL_NAME (friend_tmpl
), 1);
4425 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4427 if (tmpl
!= NULL_TREE
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4429 /* The friend template has already been declared. Just
4430 check to see that the declarations match, and install any new
4431 default parameters. We must tsubst the default parameters,
4432 of course. We only need the innermost template parameters
4433 because that is all that redeclare_class_template will look
4436 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4438 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4439 friend_type
= TREE_TYPE (tmpl
);
4443 /* The friend template has not already been declared. In this
4444 case, the instantiation of the template class will cause the
4445 injection of this template into the global scope. */
4446 tmpl
= tsubst (friend_tmpl
, args
, NULL_TREE
);
4448 /* The new TMPL is not an instantiation of anything, so we
4449 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4450 the new type because that is supposed to be the corresponding
4451 template decl, i.e., TMPL. */
4452 DECL_USE_TEMPLATE (tmpl
) = 0;
4453 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4454 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4456 /* Inject this template into the global scope. */
4457 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4464 instantiate_class_template (type
)
4467 tree
template, args
, pattern
, t
;
4470 if (type
== error_mark_node
)
4471 return error_mark_node
;
4473 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4476 /* We want to allocate temporary vectors of template arguments and
4477 template argument expressions on the momentary obstack, not on
4478 the expression obstack. Otherwise, all the space allocated in
4479 argument coercion and such is simply lost. */
4482 /* Figure out which template is being instantiated. */
4483 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4484 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4486 /* Figure out which arguments are being used to do the
4488 args
= CLASSTYPE_TI_ARGS (type
);
4489 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4491 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4492 /* If this is a partial instantiation, then we can't instantiate
4493 the type; there's no telling whether or not one of the
4494 template parameters might eventually be instantiated to some
4495 value that results in a specialization being used. For
4507 Now, the `S<U>' in `f<int>' is the specialization, not an
4508 instantiation of the original template. */
4511 /* Determine what specialization of the original template to
4513 if (PARTIAL_INSTANTIATION_P (type
))
4514 /* There's no telling which specialization is appropriate at this
4515 point. Since all peeking at the innards of this partial
4516 instantiation are extensions (like the "implicit typename"
4517 extension, which allows users to omit the keyword `typename' on
4518 names that are declared as types in template base classes), we
4519 are free to do what we please.
4521 Trying to figure out which partial instantiation to use can
4522 cause a crash. (Some of the template arguments don't even have
4523 types.) So, we just use the most general version. */
4527 t
= most_specialized_class (template, args
);
4529 if (t
== error_mark_node
)
4531 char *str
= "candidates are:";
4532 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4533 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4536 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4539 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4543 TYPE_BEING_DEFINED (type
) = 1;
4544 type
= error_mark_node
;
4550 pattern
= TREE_TYPE (t
);
4552 pattern
= TREE_TYPE (template);
4554 /* If the template we're instantiating is incomplete, then clearly
4555 there's nothing we can do. */
4556 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4559 /* If this is a partial instantiation, don't tsubst anything. We will
4560 only use this type for implicit typename, so the actual contents don't
4561 matter. All that matters is whether a particular name is a type. */
4562 if (PARTIAL_INSTANTIATION_P (type
))
4564 /* The fields set here must be kept in sync with those cleared
4565 in begin_class_definition. */
4566 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4567 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4568 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4569 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4570 /* Pretend that the type is complete, so that we will look
4571 inside it during name lookup and such. */
4572 TYPE_SIZE (type
) = integer_zero_node
;
4576 /* If we've recursively instantiated too many templates, stop. */
4577 if (! push_tinst_level (type
))
4580 /* Now we're really doing the instantiation. Mark the type as in
4581 the process of being defined. */
4582 TYPE_BEING_DEFINED (type
) = 1;
4584 maybe_push_to_top_level (uses_template_parms (type
));
4585 pushclass (type
, 0);
4589 /* This TYPE is actually a instantiation of of a partial
4590 specialization. We replace the innermost set of ARGS with
4591 the arguments appropriate for substitution. For example,
4594 template <class T> struct S {};
4595 template <class T> struct S<T*> {};
4597 and supposing that we are instantiating S<int*>, ARGS will
4598 present be {int*} but we need {int}. */
4600 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4603 /* If there were multiple levels in ARGS, replacing the
4604 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4605 want, so we make a copy first. */
4606 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4608 args
= copy_node (args
);
4609 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4615 if (flag_external_templates
)
4617 if (flag_alt_external_templates
)
4619 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4620 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4621 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4622 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4623 && CLASSTYPE_INTERFACE_KNOWN (type
));
4627 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4628 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4629 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4630 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4631 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4632 && CLASSTYPE_INTERFACE_KNOWN (type
));
4637 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4638 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4641 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4642 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4643 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
4644 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4645 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4646 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4647 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4648 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4649 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4650 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4651 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4652 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
4653 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
4654 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
4655 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
4656 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
4657 TYPE_USES_COMPLEX_INHERITANCE (type
)
4658 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
4659 TYPE_USES_MULTIPLE_INHERITANCE (type
)
4660 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
4661 TYPE_USES_VIRTUAL_BASECLASSES (type
)
4662 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
4663 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
4664 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
4665 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
4667 /* We must copy the arguments to the permanent obstack since
4668 during the tsubst'ing below they may wind up in the
4669 DECL_TI_ARGS of some instantiated member template. */
4670 args
= copy_to_permanent (args
);
4673 tree binfo
= TYPE_BINFO (type
);
4674 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
4680 int len
= TREE_VEC_LENGTH (pbases
);
4681 bases
= make_tree_vec (len
);
4682 for (i
= 0; i
< len
; ++i
)
4686 TREE_VEC_ELT (bases
, i
) = elt
4687 = tsubst (TREE_VEC_ELT (pbases
, i
), args
, NULL_TREE
);
4688 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
4690 basetype
= TREE_TYPE (elt
);
4692 if (! IS_AGGR_TYPE (basetype
))
4694 ("base type `%T' of `%T' fails to be a struct or class type",
4696 else if (TYPE_SIZE (complete_type (basetype
)) == NULL_TREE
)
4697 cp_error ("base class `%T' of `%T' has incomplete type",
4700 /* These are set up in xref_basetypes for normal classes, so
4701 we have to handle them here for template bases. */
4703 unshare_base_binfos (elt
);
4705 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
4707 TYPE_USES_VIRTUAL_BASECLASSES (type
) = 1;
4708 TYPE_USES_COMPLEX_INHERITANCE (type
) = 1;
4710 TYPE_GETS_NEW (type
) |= TYPE_GETS_NEW (basetype
);
4711 TYPE_GETS_DELETE (type
) |= TYPE_GETS_DELETE (basetype
);
4713 /* Don't initialize this until the vector is filled out, or
4714 lookups will crash. */
4715 BINFO_BASETYPES (binfo
) = bases
;
4719 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
4721 tree tag
= TREE_VALUE (t
);
4722 tree name
= TYPE_IDENTIFIER (tag
);
4725 newtag
= tsubst (tag
, args
, NULL_TREE
);
4726 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
4728 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
4729 /* Unfortunately, lookup_template_class sets
4730 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4731 instantiation (i.e., for the type of a member template
4732 class nested within a template class.) This behavior is
4733 required for maybe_process_partial_specialization to work
4734 correctly, but is not accurate in this case; the TAG is not
4735 an instantiation of anything. (The corresponding
4736 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4737 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
4739 /* Now, we call pushtag to put this NEWTAG into the scope of
4740 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4741 pushtag calling push_template_decl. We don't have to do
4742 this for enums because it will already have been done in
4745 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
4746 pushtag (name
, newtag
, /*globalize=*/0);
4750 /* Don't replace enum constants here. */
4751 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
4752 if (TREE_CODE (t
) != CONST_DECL
)
4756 /* The the file and line for this declaration, to assist in
4757 error message reporting. Since we called push_tinst_level
4758 above, we don't need to restore these. */
4759 lineno
= DECL_SOURCE_LINE (t
);
4760 input_filename
= DECL_SOURCE_FILE (t
);
4762 r
= tsubst (t
, args
, NULL_TREE
);
4763 if (TREE_CODE (r
) == VAR_DECL
)
4765 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
4766 /* Perhaps we should do more of grokfield here. */
4767 if (DECL_DEFINED_IN_CLASS_P (r
))
4768 /* Set up DECL_INITIAL, since tsubst doesn't. */
4769 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
4772 DECL_IN_AGGR_P (r
) = 1;
4773 DECL_EXTERNAL (r
) = 1;
4774 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
4775 if (DECL_DEFINED_IN_CLASS_P (r
))
4776 check_static_variable_definition (r
, TREE_TYPE (r
));
4779 /* R will have a TREE_CHAIN if and only if it has already been
4780 processed by finish_member_declaration. This can happen
4781 if, for example, it is a TYPE_DECL for a class-scoped
4782 ENUMERAL_TYPE; such a thing will already have been added to
4783 the field list by tsubst_enum above. */
4784 if (!TREE_CHAIN (r
))
4786 set_current_access_from_decl (r
);
4787 finish_member_declaration (r
);
4791 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4792 for this instantiation. */
4793 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
4795 tree r
= tsubst (t
, args
, NULL_TREE
);
4796 set_current_access_from_decl (r
);
4797 finish_member_declaration (r
);
4800 /* Construct the DECL_FRIENDLIST for the new class type. */
4801 typedecl
= TYPE_MAIN_DECL (type
);
4802 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
4808 DECL_FRIENDLIST (typedecl
)
4809 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
4810 DECL_FRIENDLIST (typedecl
));
4812 for (friends
= TREE_VALUE (t
);
4813 friends
!= NULL_TREE
;
4814 friends
= TREE_CHAIN (friends
))
4816 if (TREE_PURPOSE (friends
) == error_mark_node
)
4818 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4819 = tree_cons (error_mark_node
,
4820 tsubst_friend_function (TREE_VALUE (friends
),
4822 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4826 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
4827 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
, NULL_TREE
),
4829 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
4835 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
4839 tree friend_type
= TREE_VALUE (t
);
4840 tree new_friend_type
;
4842 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4843 new_friend_type
= tsubst_friend_class (friend_type
, args
);
4844 else if (uses_template_parms (friend_type
))
4845 new_friend_type
= tsubst (friend_type
, args
, NULL_TREE
);
4847 /* The call to xref_tag_from_type does injection for friend
4850 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
4853 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4854 /* Trick make_friend_class into realizing that the friend
4855 we're adding is a template, not an ordinary class. It's
4856 important that we use make_friend_class since it will
4857 perform some error-checking and output cross-reference
4859 ++processing_template_decl
;
4861 make_friend_class (type
, new_friend_type
);
4863 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
4864 --processing_template_decl
;
4867 /* This does injection for friend functions. */
4868 if (!processing_template_decl
)
4870 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
, NULL_TREE
);
4872 for (; t
; t
= TREE_CHAIN (t
))
4874 tree d
= TREE_VALUE (t
);
4876 if (TREE_CODE (d
) == TYPE_DECL
)
4877 /* Already injected. */;
4883 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
4884 if (TREE_CODE (t
) == FIELD_DECL
)
4886 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
4887 require_complete_type (t
);
4890 /* Set the file and line number information to whatever is given for
4891 the class itself. This puts error messages involving generated
4892 implicit functions at a predictable point, and the same point
4893 that would be used for non-template classes. */
4894 lineno
= DECL_SOURCE_LINE (typedecl
);
4895 input_filename
= DECL_SOURCE_FILE (typedecl
);
4897 unreverse_member_declarations (type
);
4898 type
= finish_struct_1 (type
, 0);
4899 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
4901 /* Clear this now so repo_template_used is happy. */
4902 TYPE_BEING_DEFINED (type
) = 0;
4903 repo_template_used (type
);
4906 pop_from_top_level ();
4919 if (t1
== NULL_TREE
)
4920 return t2
== NULL_TREE
;
4921 if (t2
== NULL_TREE
)
4923 /* Don't care if one declares its arg const and the other doesn't -- the
4924 main variant of the arg type is all that matters. */
4925 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
4926 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
4928 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
4931 /* If arg is a non-type template parameter that does not depend on template
4932 arguments, fold it like we weren't in the body of a template. */
4935 maybe_fold_nontype_arg (arg
)
4938 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
4939 && !uses_template_parms (arg
))
4941 /* Sometimes, one of the args was an expression involving a
4942 template constant parameter, like N - 1. Now that we've
4943 tsubst'd, we might have something like 2 - 1. This will
4944 confuse lookup_template_class, so we do constant folding
4945 here. We have to unset processing_template_decl, to
4946 fool build_expr_from_tree() into building an actual
4949 int saved_processing_template_decl
= processing_template_decl
;
4950 processing_template_decl
= 0;
4951 arg
= fold (build_expr_from_tree (arg
));
4952 processing_template_decl
= saved_processing_template_decl
;
4957 /* Return the TREE_VEC with the arguments for the innermost template header,
4958 where ARGS is either that or the VEC of VECs for all the
4962 innermost_args (args
)
4965 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
4968 /* Substitute ARGS into the vector of template arguments T. */
4971 tsubst_template_arg_vector (t
, args
)
4975 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4976 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4978 bzero ((char *) elts
, len
* sizeof (tree
));
4980 for (i
= 0; i
< len
; i
++)
4982 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
4983 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
4984 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
), args
);
4986 elts
[i
] = maybe_fold_nontype_arg
4987 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, NULL_TREE
));
4989 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4996 t
= make_temp_vec (len
);
4997 for (i
= 0; i
< len
; i
++)
4998 TREE_VEC_ELT (t
, i
) = elts
[i
];
5003 /* Return the result of substituting ARGS into the template parameters
5004 given by PARMS. If there are m levels of ARGS and m + n levels of
5005 PARMS, then the result will contain n levels of PARMS. For
5006 example, if PARMS is `template <class T> template <class U>
5007 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5008 result will be `template <int*, double, class V>'. */
5011 tsubst_template_parms (parms
, args
)
5016 tree
* new_parms
= &r
;
5018 for (new_parms
= &r
;
5019 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5020 new_parms
= &(TREE_CHAIN (*new_parms
)),
5021 parms
= TREE_CHAIN (parms
))
5024 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5027 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5029 tree default_value
=
5030 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5032 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5034 TREE_VEC_ELT (new_vec
, i
)
5035 = build_tree_list (tsubst (default_value
, args
, NULL_TREE
),
5036 tsubst (parm_decl
, args
, NULL_TREE
));
5041 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5042 - TMPL_ARGS_DEPTH (args
))),
5043 new_vec
, NULL_TREE
);
5049 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5050 type T. If T is not an aggregate or enumeration type, it is
5051 handled as if by tsubst. IN_DECL is as for tsubst. If
5052 ENTERING_SCOPE is non-zero, T is the context for a template which
5053 we are presently tsubst'ing. Return the subsituted value. */
5056 tsubst_aggr_type (t
, args
, in_decl
, entering_scope
)
5065 switch (TREE_CODE (t
))
5068 if (TYPE_PTRMEMFUNC_P (t
))
5070 tree r
= build_ptrmemfunc_type
5071 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, in_decl
));
5072 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5075 /* else fall through */
5078 if (TYPE_TEMPLATE_INFO (t
))
5084 /* First, determine the context for the type we are looking
5086 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5087 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5088 in_decl
, /*entering_scope=*/1);
5090 context
= NULL_TREE
;
5092 /* Then, figure out what arguments are appropriate for the
5093 type we are trying to find. For example, given:
5095 template <class T> struct S;
5096 template <class T, class U> void f(T, U) { S<U> su; }
5098 and supposing that we are instantiating f<int, double>,
5099 then our ARGS will be {int, double}, but, when looking up
5100 S we only want {double}. */
5102 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
);
5104 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5108 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5111 /* This is not a template type, so there's nothing to do. */
5115 return tsubst (t
, args
, in_decl
);
5119 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5120 (already computed) substitution of ARGS into TREE_TYPE (T), if
5121 appropriate. Return the result of the substitution. IN_DECL is as
5125 tsubst_decl (t
, args
, type
, in_decl
)
5132 char* saved_filename
;
5135 /* Set the filename and linenumber to improve error-reporting. */
5136 saved_lineno
= lineno
;
5137 saved_filename
= input_filename
;
5138 lineno
= DECL_SOURCE_LINE (t
);
5139 input_filename
= DECL_SOURCE_FILE (t
);
5141 switch (TREE_CODE (t
))
5145 /* We can get here when processing a member template function
5146 of a template class. */
5147 tree decl
= DECL_TEMPLATE_RESULT (t
);
5149 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5151 if (!is_template_template_parm
)
5153 /* We might already have an instance of this template.
5154 The ARGS are for the surrounding class type, so the
5155 full args contain the tsubst'd args for the context,
5156 plus the innermost args from the template decl. */
5157 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5158 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5159 : DECL_TI_ARGS (DECL_RESULT (t
));
5163 full_args
= tsubst_template_arg_vector (tmpl_args
, args
);
5165 /* tsubst_template_arg_vector doesn't copy the vector if
5166 nothing changed. But, *something* should have
5168 my_friendly_assert (full_args
!= tmpl_args
, 0);
5170 spec
= retrieve_specialization (t
, full_args
);
5172 if (spec
!= NULL_TREE
)
5179 /* Make a new template decl. It will be similar to the
5180 original, but will record the current template arguments.
5181 We also create a new function declaration, which is just
5182 like the old one, but points to this new template, rather
5183 than the old one. */
5186 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5187 TREE_CHAIN (r
) = NULL_TREE
;
5189 if (is_template_template_parm
)
5191 tree new_decl
= tsubst (decl
, args
, in_decl
);
5192 DECL_RESULT (r
) = new_decl
;
5193 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5198 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
5199 /*entering_scope=*/1);
5200 DECL_CLASS_CONTEXT (r
)
5201 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
, in_decl
,
5202 /*entering_scope=*/1);
5203 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5205 if (TREE_CODE (decl
) == TYPE_DECL
)
5207 tree new_type
= tsubst (TREE_TYPE (t
), args
, in_decl
);
5208 TREE_TYPE (r
) = new_type
;
5209 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5210 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5211 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5215 tree new_decl
= tsubst (decl
, args
, in_decl
);
5216 DECL_RESULT (r
) = new_decl
;
5217 DECL_TI_TEMPLATE (new_decl
) = r
;
5218 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5219 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5222 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5223 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5224 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5226 /* The template parameters for this new template are all the
5227 template parameters for the old template, except the
5228 outermost level of parameters. */
5229 DECL_TEMPLATE_PARMS (r
)
5230 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
);
5232 if (PRIMARY_TEMPLATE_P (t
))
5233 DECL_PRIMARY_TEMPLATE (r
) = r
;
5235 /* We don't partially instantiate partial specializations. */
5236 if (TREE_CODE (decl
) == TYPE_DECL
)
5239 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5241 spec
= TREE_CHAIN (spec
))
5243 /* It helps to consider example here. Consider:
5254 Now, for example, we are instantiating S<int>::f(U u).
5255 We want to make a template:
5260 It will have a specialization, for the case U = int*, of
5264 void S<int>::f<int*>(int*);
5266 This specialization will be an instantiation of
5267 the specialization given in the declaration of S, with
5268 argument list int*. */
5270 tree fn
= TREE_VALUE (spec
);
5274 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5275 /* Instantiations are on the same list, but they're of
5276 no concern to us. */
5279 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5280 /* A full specialization. There's no need to record
5284 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
, in_decl
);
5285 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5286 spec_args
, in_decl
);
5287 DECL_TI_TEMPLATE (new_fn
) = fn
;
5288 register_specialization (new_fn
, r
,
5289 innermost_args (spec_args
));
5292 /* Record this partial instantiation. */
5293 register_specialization (r
, t
,
5294 DECL_TI_ARGS (DECL_RESULT (r
)));
5302 tree argvec
= NULL_TREE
;
5308 /* Nobody should be tsubst'ing into non-template functions. */
5309 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5311 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5315 /* Allocate template arguments on the momentary obstack,
5316 in case we don't need to keep them. */
5319 /* Calculate the most general template of which R is a
5320 specialization, and the complete set of arguments used to
5322 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5324 = tsubst_template_arg_vector (DECL_TI_ARGS
5325 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5328 /* Check to see if we already have this specialization. */
5329 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5338 /* We're going to need to keep the ARGVEC, so we copy it
5340 argvec
= copy_to_permanent (argvec
);
5343 /* Here, we deal with the peculiar case:
5345 template <class T> struct S {
5346 template <class U> friend void f();
5348 template <class U> friend void f() {}
5350 template void f<double>();
5352 Here, the ARGS for the instantiation of will be {int,
5353 double}. But, we only need as many ARGS as there are
5354 levels of template parameters in CODE_PATTERN. We are
5355 careful not to get fooled into reducing the ARGS in
5358 template <class T> struct S { template <class U> void f(U); }
5359 template <class T> template <> void S<T>::f(int) {}
5361 which we can spot because the pattern will be a
5362 specialization in this case. */
5363 args_depth
= TMPL_ARGS_DEPTH (args
);
5365 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5366 if (args_depth
> parms_depth
5367 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5369 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5371 if (parms_depth
> 1)
5375 args
= make_temp_vec (parms_depth
);
5376 for (i
= 0; i
< parms_depth
; ++i
)
5377 TREE_VEC_ELT (args
, i
) =
5378 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5381 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5386 /* This special case arises when we have something like this:
5388 template <class T> struct S {
5389 friend void f<int>(int, double);
5392 Here, the DECL_TI_TEMPLATE for the friend declaration
5393 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5394 being called from tsubst_friend_function, and we want
5395 only to create a new decl (R) with appropriate types so
5396 that we can call determine_specialization. */
5397 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5399 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5400 == IDENTIFIER_NODE
), 0);
5401 gen_tmpl
= NULL_TREE
;
5404 if (DECL_CLASS_SCOPE_P (t
))
5406 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5410 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
, t
,
5411 /*entering_scope=*/1);
5418 type
= tsubst (type
, args
, in_decl
);
5420 /* We do NOT check for matching decls pushed separately at this
5421 point, as they may not represent instantiations of this
5422 template, and in any case are considered separate under the
5423 discrete model. Instead, see add_maybe_template. */
5427 DECL_USE_TEMPLATE (r
) = 0;
5428 TREE_TYPE (r
) = type
;
5431 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, t
, /*entering_scope=*/1);
5432 DECL_CLASS_CONTEXT (r
) = ctx
;
5434 if (member
&& IDENTIFIER_TYPENAME_P (DECL_NAME (r
)))
5435 /* Type-conversion operator. Reconstruct the name, in
5436 case it's the name of one of the template's parameters. */
5437 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5439 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, t
);
5440 DECL_MAIN_VARIANT (r
) = r
;
5441 DECL_RESULT (r
) = NULL_TREE
;
5443 TREE_STATIC (r
) = 0;
5444 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5445 DECL_EXTERNAL (r
) = 1;
5446 DECL_INTERFACE_KNOWN (r
) = 0;
5447 DECL_DEFER_OUTPUT (r
) = 0;
5448 TREE_CHAIN (r
) = NULL_TREE
;
5449 DECL_PENDING_INLINE_INFO (r
) = 0;
5452 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5453 name. There's no need to do this in the special friend
5454 case mentioned above where GEN_TMPL is NULL. */
5457 /* The ARGVEC was built on the momentary obstack. Make it
5459 argvec
= copy_to_permanent (argvec
);
5460 DECL_TEMPLATE_INFO (r
)
5461 = perm_tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5462 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5463 register_specialization (r
, gen_tmpl
, argvec
);
5465 /* Set the mangled name for R. */
5466 if (DECL_DESTRUCTOR_P (t
))
5467 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5470 /* Instantiations of template functions must be mangled
5471 specially, in order to conform to 14.5.5.1
5472 [temp.over.link]. */
5473 tree tmpl
= DECL_TI_TEMPLATE (t
);
5475 /* TMPL will be NULL if this is a specialization of a
5476 member function of a template class. */
5477 if (name_mangling_version
< 1
5478 || tmpl
== NULL_TREE
5479 || (member
&& !is_member_template (tmpl
)
5480 && !DECL_TEMPLATE_INFO (tmpl
)))
5481 set_mangled_name_for_decl (r
);
5483 set_mangled_name_for_template_decl (r
);
5487 make_decl_rtl (r
, NULL_PTR
, 1);
5489 /* Like grokfndecl. If we don't do this, pushdecl will
5490 mess up our TREE_CHAIN because it doesn't find a
5491 previous decl. Sigh. */
5493 && ! uses_template_parms (r
)
5494 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5496 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5499 if (DECL_CONSTRUCTOR_P (r
))
5501 maybe_retrofit_in_chrg (r
);
5502 grok_ctor_properties (ctx
, r
);
5504 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
5505 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5512 TREE_TYPE (r
) = type
;
5513 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5514 DECL_INITIAL (r
) = TREE_TYPE (r
);
5516 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
, in_decl
);
5518 DECL_CONTEXT (r
) = NULL_TREE
;
5519 #ifdef PROMOTE_PROTOTYPES
5520 if ((TREE_CODE (type
) == INTEGER_TYPE
5521 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5522 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5523 DECL_ARG_TYPE (r
) = integer_type_node
;
5526 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, TREE_CHAIN (t
));
5533 TREE_TYPE (r
) = type
;
5536 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, in_decl
);
5538 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, in_decl
);
5539 TREE_CHAIN (r
) = NULL_TREE
;
5540 if (TREE_CODE (type
) == VOID_TYPE
)
5541 cp_error_at ("instantiation of `%D' as type void", r
);
5549 = tsubst_copy (DECL_INITIAL (t
), args
, in_decl
);
5550 TREE_CHAIN (r
) = NULL_TREE
;
5560 tree ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
5561 /*entering_scope=*/1);
5563 /* Nobody should be tsubst'ing into non-template variables. */
5564 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
5565 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5567 /* Check to see if we already have this specialization. */
5568 tmpl
= DECL_TI_TEMPLATE (t
);
5569 gen_tmpl
= most_general_template (tmpl
);
5570 argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
5571 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5580 TREE_TYPE (r
) = type
;
5581 DECL_CONTEXT (r
) = ctx
;
5582 if (TREE_STATIC (r
))
5583 DECL_ASSEMBLER_NAME (r
)
5584 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
5586 /* Don't try to expand the initializer until someone tries to use
5587 this variable; otherwise we run into circular dependencies. */
5588 DECL_INITIAL (r
) = NULL_TREE
;
5592 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
5594 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
5595 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5596 register_specialization (r
, gen_tmpl
, argvec
);
5598 TREE_CHAIN (r
) = NULL_TREE
;
5599 if (TREE_CODE (type
) == VOID_TYPE
)
5600 cp_error_at ("instantiation of `%D' as type void", r
);
5605 if (t
== TYPE_NAME (TREE_TYPE (t
)))
5606 r
= TYPE_NAME (type
);
5610 TREE_TYPE (r
) = type
;
5611 DECL_CONTEXT (r
) = current_class_type
;
5612 TREE_CHAIN (r
) = NULL_TREE
;
5617 my_friendly_abort (0);
5620 /* Restore the file and line information. */
5621 lineno
= saved_lineno
;
5622 input_filename
= saved_filename
;
5627 /* Substitue into the ARG_TYPES of a function type. */
5630 tsubst_arg_types (arg_types
, args
, in_decl
)
5635 tree remaining_arg_types
;
5638 if (!arg_types
|| arg_types
== void_list_node
)
5641 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
5644 /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't
5645 matter on function types. */
5646 type
= TYPE_MAIN_VARIANT (type_decays_to
5647 (tsubst (TREE_VALUE (arg_types
),
5650 /* Note that we do not substitute into default arguments here. The
5651 standard mandates that they be instantiated only when needed,
5652 which is done in build_over_call. */
5653 return hash_tree_cons_simple (TREE_PURPOSE (arg_types
), type
,
5654 remaining_arg_types
);
5658 /* Substitute into the PARMS of a call-declarator. */
5661 tsubst_call_declarator_parms (parms
, args
, in_decl
)
5670 if (!parms
|| parms
== void_list_node
)
5673 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
5676 /* Figure out the type of this parameter. */
5677 type
= tsubst (TREE_VALUE (parms
), args
, in_decl
);
5679 /* Figure out the default argument as well. Note that we use
5680 tsubst_copy since the default argument is really an
5682 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, in_decl
);
5684 /* Chain this parameter on to the front of those we have already
5685 processed. We don't use hash_tree_cons because that function
5686 doesn't check TREE_PARMLIST. */
5687 new_parms
= tree_cons (defarg
, type
, new_parms
);
5689 /* And note that these are parameters. */
5690 TREE_PARMLIST (new_parms
) = 1;
5695 /* Take the tree structure T and replace template parameters used therein
5696 with the argument vector ARGS. IN_DECL is an associated decl for
5699 tsubst is used for dealing with types, decls and the like; for
5700 expressions, use tsubst_expr or tsubst_copy. */
5703 tsubst (t
, args
, in_decl
)
5709 if (t
== NULL_TREE
|| t
== error_mark_node
5710 || t
== integer_type_node
5711 || t
== void_type_node
5712 || t
== char_type_node
5713 || TREE_CODE (t
) == NAMESPACE_DECL
)
5716 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
5717 type
= IDENTIFIER_TYPE_VALUE (t
);
5719 type
= TREE_TYPE (t
);
5720 if (type
== unknown_type_node
)
5721 my_friendly_abort (42);
5723 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
5724 && TREE_CODE (t
) != TYPENAME_TYPE
5725 && TREE_CODE (t
) != TEMPLATE_DECL
5726 && TREE_CODE (t
) != IDENTIFIER_NODE
)
5727 type
= tsubst (type
, args
, in_decl
);
5729 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
5730 return tsubst_decl (t
, args
, type
, in_decl
);
5732 switch (TREE_CODE (t
))
5737 return tsubst_aggr_type (t
, args
, in_decl
, /*entering_scope=*/0);
5740 case IDENTIFIER_NODE
:
5752 if (t
== integer_type_node
)
5755 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
5756 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
5760 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
5762 max
= tsubst_expr (max
, args
, in_decl
);
5763 if (processing_template_decl
)
5765 tree itype
= make_node (INTEGER_TYPE
);
5766 TYPE_MIN_VALUE (itype
) = size_zero_node
;
5767 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
5772 if (pedantic
&& integer_zerop (max
))
5773 pedwarn ("creating array with size zero");
5774 else if (INT_CST_LT (max
, integer_zero_node
))
5776 cp_error ("creating array with size `%E'", max
);
5777 max
= integer_one_node
;
5780 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
5781 if (!TREE_PERMANENT (max
) && !allocation_temporary_p ())
5782 max
= copy_to_permanent (max
);
5783 return build_index_type (max
);
5786 case TEMPLATE_TYPE_PARM
:
5787 case TEMPLATE_TEMPLATE_PARM
:
5788 case TEMPLATE_PARM_INDEX
:
5796 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
5797 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
5799 idx
= TEMPLATE_TYPE_IDX (t
);
5800 level
= TEMPLATE_TYPE_LEVEL (t
);
5804 idx
= TEMPLATE_PARM_IDX (t
);
5805 level
= TEMPLATE_PARM_LEVEL (t
);
5808 if (TREE_VEC_LENGTH (args
) > 0)
5810 tree arg
= NULL_TREE
;
5812 levels
= TMPL_ARGS_DEPTH (args
);
5813 if (level
<= levels
)
5814 arg
= TMPL_ARG (args
, level
, idx
);
5816 if (arg
== error_mark_node
)
5817 return error_mark_node
;
5818 else if (arg
!= NULL_TREE
)
5820 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
5822 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
5824 return cp_build_qualified_type
5825 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
));
5827 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
5829 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
5831 /* We are processing a type constructed from
5832 a template template parameter */
5833 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
5836 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5837 we are resolving nested-types in the signature of
5838 a member function templates.
5839 Otherwise ARG is a TEMPLATE_DECL and is the real
5840 template to be instantiated. */
5841 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
5842 arg
= TYPE_NAME (arg
);
5844 r
= lookup_template_class (DECL_NAME (arg
),
5847 /*entering_scope=*/0);
5848 return cp_build_qualified_type (r
, TYPE_QUALS (t
));
5851 /* We are processing a template argument list. */
5859 my_friendly_abort (981018);
5862 /* This can happen during the attempted tsubst'ing in
5863 unify. This means that we don't yet have any information
5864 about the template parameter in question. */
5867 /* If we get here, we must have been looking at a parm for a
5868 more deeply nested template. Make a new version of this
5869 template parameter, but with a lower level. */
5870 switch (TREE_CODE (t
))
5872 case TEMPLATE_TYPE_PARM
:
5873 case TEMPLATE_TEMPLATE_PARM
:
5875 TEMPLATE_TYPE_PARM_INDEX (r
)
5876 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
5878 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
5879 TYPE_MAIN_VARIANT (r
) = r
;
5880 TYPE_POINTER_TO (r
) = NULL_TREE
;
5881 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
5883 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
5884 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
5886 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
, in_decl
);
5887 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
5888 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
5892 case TEMPLATE_PARM_INDEX
:
5893 r
= reduce_template_parm_level (t
, type
, levels
);
5897 my_friendly_abort (0);
5905 tree purpose
, value
, chain
, result
;
5906 int via_public
, via_virtual
, via_protected
;
5908 if (t
== void_list_node
)
5911 via_public
= TREE_VIA_PUBLIC (t
);
5912 via_protected
= TREE_VIA_PROTECTED (t
);
5913 via_virtual
= TREE_VIA_VIRTUAL (t
);
5915 purpose
= TREE_PURPOSE (t
);
5917 purpose
= tsubst (purpose
, args
, in_decl
);
5918 value
= TREE_VALUE (t
);
5920 value
= tsubst (value
, args
, in_decl
);
5921 chain
= TREE_CHAIN (t
);
5922 if (chain
&& chain
!= void_type_node
)
5923 chain
= tsubst (chain
, args
, in_decl
);
5924 if (purpose
== TREE_PURPOSE (t
)
5925 && value
== TREE_VALUE (t
)
5926 && chain
== TREE_CHAIN (t
))
5928 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
5929 purpose
, value
, chain
);
5930 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
5934 if (type
!= NULL_TREE
)
5936 /* A binfo node. We always need to make a copy, of the node
5937 itself and of its BINFO_BASETYPES. */
5941 /* Make sure type isn't a typedef copy. */
5942 type
= BINFO_TYPE (TYPE_BINFO (type
));
5944 TREE_TYPE (t
) = complete_type (type
);
5945 if (IS_AGGR_TYPE (type
))
5947 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
5948 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
5949 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
5950 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
5955 /* Otherwise, a vector of template arguments. */
5956 return tsubst_template_arg_vector (t
, args
);
5959 case REFERENCE_TYPE
:
5961 enum tree_code code
;
5963 if (type
== TREE_TYPE (t
))
5966 code
= TREE_CODE (t
);
5967 if (TREE_CODE (type
) == REFERENCE_TYPE
5968 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
5970 static int last_line
= 0;
5971 static char* last_file
= 0;
5973 /* We keep track of the last time we issued this error
5974 message to avoid spewing a ton of messages during a
5975 single bad template instantiation. */
5976 if (last_line
!= lineno
||
5977 last_file
!= input_filename
)
5979 if (TREE_CODE (type
) == VOID_TYPE
)
5980 cp_error ("forming reference to void");
5982 cp_error ("forming %s to reference type `%T'",
5983 (code
== POINTER_TYPE
) ? "pointer" : "reference",
5986 last_file
= input_filename
;
5989 /* Use the underlying type in an attempt at error
5990 recovery; maybe the user meant vector<int> and wrote
5991 vector<int&>, or some such. */
5992 if (code
== REFERENCE_TYPE
)
5995 r
= build_pointer_type (TREE_TYPE (type
));
5997 else if (code
== POINTER_TYPE
)
5998 r
= build_pointer_type (type
);
6000 r
= build_reference_type (type
);
6001 r
= cp_build_qualified_type (r
, TYPE_QUALS (t
));
6003 /* Will this ever be needed for TYPE_..._TO values? */
6009 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, in_decl
);
6010 if (! IS_AGGR_TYPE (r
))
6011 cp_error ("creating pointer to member of non-class type `%T'", r
);
6012 return build_offset_type (r
, type
);
6021 /* The TYPE_CONTEXT is not used for function/method types. */
6022 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6024 /* Substitue the argument types. */
6025 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, in_decl
);
6027 /* Construct a new type node and return it. */
6028 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6029 fntype
= build_function_type (type
, arg_types
);
6032 r
= TREE_TYPE (TREE_VALUE (arg_types
));
6033 if (! IS_AGGR_TYPE (r
))
6034 cp_error ("creating pointer to member function of non-class type `%T'",
6037 fntype
= build_cplus_method_type (r
, type
, TREE_CHAIN (arg_types
));
6039 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
6041 /* Substitue the exception specification. */
6042 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6045 raises
= tsubst (raises
, args
, in_decl
);
6046 fntype
= build_exception_variant (fntype
, raises
);
6052 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, in_decl
);
6053 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6056 /* These checks should match the ones in grokdeclarator. */
6057 if (TREE_CODE (type
) == VOID_TYPE
)
6059 cp_error ("creating array of void");
6060 type
= build_pointer_type (type
);
6062 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
6064 cp_error ("creating array of functions `%T'", type
);
6065 type
= build_pointer_type (type
);
6067 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
6069 cp_error ("creating array of references `%T'", type
);
6070 type
= TREE_TYPE (type
);
6073 r
= build_cplus_array_type (type
, domain
);
6079 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
6080 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6081 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
)));
6085 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
6086 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
)));
6090 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, in_decl
,
6091 /*entering_scope=*/1);
6092 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
, in_decl
);
6094 /* Normally, make_typename_type does not require that the CTX
6095 have complete type in order to allow things like:
6097 template <class T> struct S { typename S<T>::X Y; };
6099 But, such constructs have already been resolved by this
6100 point, so here CTX really should have complete type, unless
6101 it's a partial instantiation. */
6102 if (!uses_template_parms (ctx
)
6103 && !TYPE_BEING_DEFINED (ctx
)
6104 && !complete_type_or_else (ctx
))
6105 return error_mark_node
;
6107 f
= make_typename_type (ctx
, f
);
6108 return cp_build_qualified_type (f
,
6110 | CP_TYPE_QUALS (t
));
6114 return make_pointer_declarator
6115 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
6118 return make_reference_declarator
6119 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
6122 return build_parse_node
6123 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6124 tsubst_expr (TREE_OPERAND (t
, 1), args
, in_decl
));
6127 return make_call_declarator
6128 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6129 tsubst_call_declarator_parms (TREE_OPERAND (t
, 1), args
, in_decl
),
6130 TREE_OPERAND (t
, 2),
6131 tsubst (TREE_TYPE (t
), args
, in_decl
));
6134 return build_parse_node
6135 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6136 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
));
6139 return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t
), args
, in_decl
));
6142 sorry ("use of `%s' in template",
6143 tree_code_name
[(int) TREE_CODE (t
)]);
6144 return error_mark_node
;
6151 emit_line_note (input_filename
, lineno
);
6155 expand_start_bindings (0);
6162 int saved_warn_unused
= 0;
6164 if (processing_template_decl
)
6166 saved_warn_unused
= warn_unused
;
6169 expand_end_bindings (getdecls (), kept_level_p (), 0);
6170 if (processing_template_decl
)
6171 warn_unused
= saved_warn_unused
;
6172 t
= poplevel (kept_level_p (), 1, 0);
6177 /* Like tsubst, but deals with expressions. This function just replaces
6178 template parms; to finish processing the resultant expression, use
6182 tsubst_copy (t
, args
, in_decl
)
6186 enum tree_code code
;
6189 if (t
== NULL_TREE
|| t
== error_mark_node
)
6192 code
= TREE_CODE (t
);
6197 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6204 if (!DECL_CONTEXT (t
))
6205 /* This is a global enumeration constant. */
6208 /* Unfortunately, we cannot just call lookup_name here.
6211 template <int I> int f() {
6213 struct S { void g() { E e = a; } };
6216 When we instantiate f<7>::S::g(), say, lookup_name is not
6217 clever enough to find f<7>::a. */
6219 = tsubst_aggr_type (TREE_TYPE (t
), args
, in_decl
,
6220 /*entering_scope=*/0);
6222 for (v
= TYPE_VALUES (enum_type
);
6225 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6226 return TREE_VALUE (v
);
6228 /* We didn't find the name. That should never happen; if
6229 name-lookup found it during preliminary parsing, we
6230 should find it again here during instantiation. */
6231 my_friendly_abort (0);
6236 if (DECL_CONTEXT (t
))
6240 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, in_decl
,
6241 /*entering_scope=*/1);
6242 if (ctx
!= DECL_CONTEXT (t
))
6243 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6249 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6250 t
= tsubst (t
, args
, in_decl
);
6255 if (is_member_template (t
))
6256 return tsubst (t
, args
, in_decl
);
6262 /* We must tsbust into a LOOKUP_EXPR in case the names to
6263 which it refers is a conversion operator; in that case the
6264 name will change. We avoid making unnecessary copies,
6267 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
6269 if (id
!= TREE_OPERAND (t
, 0))
6271 r
= build_nt (LOOKUP_EXPR
, id
);
6272 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6280 case REINTERPRET_CAST_EXPR
:
6281 case CONST_CAST_EXPR
:
6282 case STATIC_CAST_EXPR
:
6283 case DYNAMIC_CAST_EXPR
:
6286 (code
, tsubst (TREE_TYPE (t
), args
, in_decl
),
6287 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
6290 case PREDECREMENT_EXPR
:
6291 case PREINCREMENT_EXPR
:
6292 case POSTDECREMENT_EXPR
:
6293 case POSTINCREMENT_EXPR
:
6295 case TRUTH_NOT_EXPR
:
6298 case CONVERT_EXPR
: /* Unary + */
6306 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
6311 case TRUNC_DIV_EXPR
:
6313 case FLOOR_DIV_EXPR
:
6314 case ROUND_DIV_EXPR
:
6315 case EXACT_DIV_EXPR
:
6317 case BIT_ANDTC_EXPR
:
6320 case TRUNC_MOD_EXPR
:
6321 case FLOOR_MOD_EXPR
:
6322 case TRUTH_ANDIF_EXPR
:
6323 case TRUTH_ORIF_EXPR
:
6324 case TRUTH_AND_EXPR
:
6345 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6346 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
6350 tree fn
= TREE_OPERAND (t
, 0);
6351 if (is_overloaded_fn (fn
))
6352 fn
= tsubst_copy (get_first_fn (fn
), args
, in_decl
);
6354 /* Sometimes FN is a LOOKUP_EXPR. */
6355 fn
= tsubst_copy (fn
, args
, in_decl
);
6357 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6361 case METHOD_CALL_EXPR
:
6363 tree name
= TREE_OPERAND (t
, 0);
6364 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
6366 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6367 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6369 else if (TREE_CODE (name
) == SCOPE_REF
6370 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
6372 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6373 name
= TREE_OPERAND (name
, 1);
6374 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
6375 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
6376 name
= build_nt (SCOPE_REF
, base
, name
);
6379 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
6381 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6382 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
),
6391 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6392 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6393 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
6395 if (code
== BIND_EXPR
&& !processing_template_decl
)
6397 /* This processing should really occur in tsubst_expr,
6398 However, tsubst_expr does not recurse into expressions,
6399 since it assumes that there aren't any statements
6400 inside them. Instead, it simply calls
6401 build_expr_from_tree. So, we need to expand the
6403 tree rtl_expr
= begin_stmt_expr ();
6404 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
, in_decl
);
6405 r
= finish_stmt_expr (rtl_expr
, block
);
6414 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6415 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
6416 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
6417 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
6424 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
6425 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
6426 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
6427 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
6431 case TEMPLATE_ID_EXPR
:
6433 /* Substituted template arguments */
6434 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
);
6436 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
6437 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
6439 return lookup_template_function
6440 (tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
), targs
);
6445 tree purpose
, value
, chain
;
6447 if (t
== void_list_node
)
6450 purpose
= TREE_PURPOSE (t
);
6452 purpose
= tsubst_copy (purpose
, args
, in_decl
);
6453 value
= TREE_VALUE (t
);
6455 value
= tsubst_copy (value
, args
, in_decl
);
6456 chain
= TREE_CHAIN (t
);
6457 if (chain
&& chain
!= void_type_node
)
6458 chain
= tsubst_copy (chain
, args
, in_decl
);
6459 if (purpose
== TREE_PURPOSE (t
)
6460 && value
== TREE_VALUE (t
)
6461 && chain
== TREE_CHAIN (t
))
6463 return tree_cons (purpose
, value
, chain
);
6470 case TEMPLATE_TYPE_PARM
:
6471 case TEMPLATE_TEMPLATE_PARM
:
6472 case TEMPLATE_PARM_INDEX
:
6474 case REFERENCE_TYPE
:
6481 return tsubst (t
, args
, in_decl
);
6483 case IDENTIFIER_NODE
:
6484 if (IDENTIFIER_TYPENAME_P (t
)
6485 /* Make sure it's not just a variable named `__opr', for instance,
6486 which can occur in some existing code. */
6488 return build_typename_overload
6489 (tsubst (TREE_TYPE (t
), args
, in_decl
));
6496 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, in_decl
), NULL_TREE
,
6497 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, in_decl
));
6498 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
6507 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6510 tsubst_expr (t
, args
, in_decl
)
6514 if (t
== NULL_TREE
|| t
== error_mark_node
)
6517 if (processing_template_decl
)
6518 return tsubst_copy (t
, args
, in_decl
);
6520 switch (TREE_CODE (t
))
6523 lineno
= TREE_COMPLEXITY (t
);
6524 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
6529 lineno
= TREE_COMPLEXITY (t
);
6530 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
6536 int i
= suspend_momentary ();
6539 lineno
= TREE_COMPLEXITY (t
);
6540 emit_line_note (input_filename
, lineno
);
6542 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
6543 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
6544 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
6545 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, in_decl
);
6547 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
6548 resume_momentary (i
);
6555 lineno
= TREE_COMPLEXITY (t
);
6558 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6559 tsubst_expr (tmp
, args
, in_decl
);
6560 finish_for_init_stmt (NULL_TREE
);
6561 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
6564 tmp
= tsubst_expr (FOR_EXPR (t
), args
, in_decl
);
6565 finish_for_expr (tmp
, NULL_TREE
);
6566 tsubst_expr (FOR_BODY (t
), args
, in_decl
);
6567 finish_for_stmt (tmp
, NULL_TREE
);
6573 lineno
= TREE_COMPLEXITY (t
);
6574 begin_while_stmt ();
6575 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
6578 tsubst_expr (WHILE_BODY (t
), args
, in_decl
);
6579 finish_while_stmt (NULL_TREE
);
6585 lineno
= TREE_COMPLEXITY (t
);
6587 tsubst_expr (DO_BODY (t
), args
, in_decl
);
6588 finish_do_body (NULL_TREE
);
6589 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
6599 lineno
= TREE_COMPLEXITY (t
);
6601 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
6605 if (tmp
= THEN_CLAUSE (t
), tmp
)
6607 tsubst_expr (tmp
, args
, in_decl
);
6608 finish_then_clause (NULL_TREE
);
6611 if (tmp
= ELSE_CLAUSE (t
), tmp
)
6613 begin_else_clause ();
6614 tsubst_expr (tmp
, args
, in_decl
);
6615 finish_else_clause (NULL_TREE
);
6626 lineno
= TREE_COMPLEXITY (t
);
6627 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
6628 for (substmt
= COMPOUND_BODY (t
);
6629 substmt
!= NULL_TREE
;
6630 substmt
= TREE_CHAIN (substmt
))
6631 tsubst_expr (substmt
, args
, in_decl
);
6632 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
6638 lineno
= TREE_COMPLEXITY (t
);
6639 finish_break_stmt ();
6643 lineno
= TREE_COMPLEXITY (t
);
6644 finish_continue_stmt ();
6651 lineno
= TREE_COMPLEXITY (t
);
6652 begin_switch_stmt ();
6653 val
= tsubst_expr (SWITCH_COND (t
), args
, in_decl
);
6654 finish_switch_cond (val
);
6656 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
6657 tsubst_expr (tmp
, args
, in_decl
);
6659 finish_switch_stmt (val
, NULL_TREE
);
6664 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, in_decl
),
6665 tsubst_expr (CASE_HIGH (t
), args
, in_decl
));
6669 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
6676 lineno
= TREE_COMPLEXITY (t
);
6677 t
= GOTO_DESTINATION (t
);
6678 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
6679 /* Computed goto's must be tsubst'd into. On the other hand,
6680 non-computed gotos must not be; the identifier in question
6681 will have no binding. */
6682 t
= tsubst_expr (t
, args
, in_decl
);
6683 finish_goto_stmt (t
);
6687 lineno
= TREE_COMPLEXITY (t
);
6688 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, in_decl
),
6689 tsubst_expr (ASM_STRING (t
), args
, in_decl
),
6690 tsubst_expr (ASM_OUTPUTS (t
), args
, in_decl
),
6691 tsubst_expr (ASM_INPUTS (t
), args
, in_decl
),
6692 tsubst_expr (ASM_CLOBBERS (t
), args
, in_decl
));
6696 lineno
= TREE_COMPLEXITY (t
);
6698 tsubst_expr (TRY_STMTS (t
), args
, in_decl
);
6699 finish_try_block (NULL_TREE
);
6701 tree handler
= TRY_HANDLERS (t
);
6702 for (; handler
; handler
= TREE_CHAIN (handler
))
6703 tsubst_expr (handler
, args
, in_decl
);
6705 finish_handler_sequence (NULL_TREE
);
6709 lineno
= TREE_COMPLEXITY (t
);
6711 if (HANDLER_PARMS (t
))
6713 tree d
= HANDLER_PARMS (t
);
6714 expand_start_catch_block
6715 (tsubst (TREE_OPERAND (d
, 1), args
, in_decl
),
6716 tsubst (TREE_OPERAND (d
, 0), args
, in_decl
));
6719 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
6720 finish_handler_parms (NULL_TREE
);
6721 tsubst_expr (HANDLER_BODY (t
), args
, in_decl
);
6722 finish_handler (NULL_TREE
);
6726 lineno
= TREE_COMPLEXITY (t
);
6728 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
6729 tsubst (t
, args
, NULL_TREE
);
6733 return build_expr_from_tree (tsubst_copy (t
, args
, in_decl
));
6738 /* Instantiate the indicated variable or function template TMPL with
6739 the template arguments in TARG_PTR. */
6742 instantiate_template (tmpl
, targ_ptr
)
6743 tree tmpl
, targ_ptr
;
6749 struct obstack
*old_fmp_obstack
;
6750 extern struct obstack
*function_maybepermanent_obstack
;
6753 if (tmpl
== error_mark_node
)
6754 return error_mark_node
;
6756 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
6758 /* Check to see if we already have this specialization. */
6759 spec
= retrieve_specialization (tmpl
, targ_ptr
);
6760 if (spec
!= NULL_TREE
)
6763 if (DECL_TEMPLATE_INFO (tmpl
))
6765 /* The TMPL is a partial instantiation. To get a full set of
6766 arguments we must add the arguments used to perform the
6767 partial instantiation. */
6768 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
6770 gen_tmpl
= most_general_template (tmpl
);
6772 /* Check to see if we already have this specialization. */
6773 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
6774 if (spec
!= NULL_TREE
)
6780 push_obstacks (&permanent_obstack
, &permanent_obstack
);
6781 old_fmp_obstack
= function_maybepermanent_obstack
;
6782 function_maybepermanent_obstack
= &permanent_obstack
;
6784 len
= DECL_NTPARMS (gen_tmpl
);
6785 inner_args
= innermost_args (targ_ptr
);
6789 tree t
= TREE_VEC_ELT (inner_args
, i
);
6790 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
6792 tree nt
= target_type (t
);
6793 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
6795 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
6796 cp_error (" trying to instantiate `%D'", gen_tmpl
);
6797 fndecl
= error_mark_node
;
6802 targ_ptr
= copy_to_permanent (targ_ptr
);
6804 /* substitute template parameters */
6805 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, gen_tmpl
);
6806 /* The DECL_TI_TEMPLATE should always be the immediate parent
6807 template, not the most general template. */
6808 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
6810 if (flag_external_templates
)
6811 add_pending_template (fndecl
);
6814 function_maybepermanent_obstack
= old_fmp_obstack
;
6820 /* Push the name of the class template into the scope of the instantiation. */
6823 overload_template_name (type
)
6826 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
6829 if (IDENTIFIER_CLASS_VALUE (id
)
6830 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
6833 decl
= build_decl (TYPE_DECL
, id
, type
);
6834 SET_DECL_ARTIFICIAL (decl
);
6835 pushdecl_class_level (decl
);
6838 /* Like type_unification but designed specially to handle conversion
6841 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6842 arguments that are being used when calling it.
6844 If FN is a conversion operator, RETURN_TYPE is the type desired as
6845 the result of the conversion operator.
6847 The EXTRA_FN_ARG, if any, is the type of an additional
6848 parameter to be added to the beginning of FN's parameter list.
6850 The other arguments are as for type_unification. */
6853 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
6854 strict
, extra_fn_arg
)
6855 tree fn
, explicit_targs
, targs
, args
, return_type
;
6856 unification_kind_t strict
;
6861 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
6863 parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6865 if (DECL_CONV_FN_P (fn
))
6867 /* This is a template conversion operator. Use the return types
6868 as well as the argument types. */
6869 parms
= scratch_tree_cons (NULL_TREE
,
6870 TREE_TYPE (TREE_TYPE (fn
)),
6872 args
= scratch_tree_cons (NULL_TREE
, return_type
, args
);
6875 if (extra_fn_arg
!= NULL_TREE
)
6876 parms
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
, parms
);
6878 /* We allow incomplete unification without an error message here
6879 because the standard doesn't seem to explicitly prohibit it. Our
6880 callers must be ready to deal with unification failures in any
6882 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
6891 /* Type unification.
6893 We have a function template signature with one or more references to
6894 template parameters, and a parameter list we wish to fit to this
6895 template. If possible, produce a list of parameters for the template
6896 which will cause it to fit the supplied parameter list.
6898 Return zero for success, 2 for an incomplete match that doesn't resolve
6899 all the types, and 1 for complete failure. An error message will be
6900 printed only for an incomplete match.
6902 TPARMS[NTPARMS] is an array of template parameter types.
6904 TARGS[NTPARMS] is the array into which the deduced template
6905 parameter values are placed. PARMS is the function template's
6906 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6907 list we're trying to match against it.
6909 The EXPLICIT_TARGS are explicit template arguments provided via a
6912 The parameter STRICT is one of:
6915 We are deducing arguments for a function call, as in
6919 We are deducing arguments for a conversion function, as in
6923 We are deducing arguments when calculating the partial
6924 ordering between specializations of function or class
6925 templates, as in [temp.func.order] and [temp.class.order],
6926 when doing an explicit instantiation as in [temp.explicit],
6927 when determining an explicit specialization as in
6928 [temp.expl.spec], or when taking the address of a function
6929 template, as in [temp.deduct.funcaddr]. */
6932 type_unification (tparms
, targs
, parms
, args
, explicit_targs
,
6933 strict
, allow_incomplete
)
6934 tree tparms
, targs
, parms
, args
, explicit_targs
;
6935 unification_kind_t strict
;
6936 int allow_incomplete
;
6941 for (i
= 0; i
< TREE_VEC_LENGTH (tparms
); i
++)
6942 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
6944 if (explicit_targs
!= NULL_TREE
)
6947 arg_vec
= coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, 0,
6950 if (arg_vec
== error_mark_node
)
6953 explicit_mask
= alloca (sizeof (int) * TREE_VEC_LENGTH (targs
));
6954 bzero ((char *) explicit_mask
, sizeof(int) * TREE_VEC_LENGTH (targs
));
6957 i
< TREE_VEC_LENGTH (arg_vec
)
6958 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
6961 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (arg_vec
, i
);
6962 /* Let unify know that this argument was explicit. */
6963 explicit_mask
[i
] = 1;
6970 type_unification_real (tparms
, targs
, parms
, args
, 0,
6971 strict
, allow_incomplete
, explicit_mask
);
6974 /* Adjust types before performing type deduction, as described in
6975 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
6976 sections are symmetric. PARM is the type of a function parameter
6977 or the return type of the conversion function. ARG is the type of
6978 the argument passed to the call, or the type of the value
6979 intialized with the result of the conversion function. */
6982 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
6983 unification_kind_t strict
;
6994 /* Swap PARM and ARG throughout the remainder of this
6995 function; the handling is precisely symmetric since PARM
6996 will initialize ARG rather than vice versa. */
7004 /* There is nothing to do in this case. */
7008 my_friendly_abort (0);
7011 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7013 /* [temp.deduct.call]
7015 If P is not a reference type:
7017 --If A is an array type, the pointer type produced by the
7018 array-to-pointer standard conversion (_conv.array_) is
7019 used in place of A for type deduction; otherwise,
7021 --If A is a function type, the pointer type produced by
7022 the function-to-pointer standard conversion
7023 (_conv.func_) is used in place of A for type deduction;
7026 --If A is a cv-qualified type, the top level
7027 cv-qualifiers of A's type are ignored for type
7029 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7030 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7031 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7032 *arg
= build_pointer_type (*arg
);
7034 *arg
= TYPE_MAIN_VARIANT (*arg
);
7037 /* [temp.deduct.call]
7039 If P is a cv-qualified type, the top level cv-qualifiers
7040 of P's type are ignored for type deduction. If P is a
7041 reference type, the type referred to by P is used for
7043 *parm
= TYPE_MAIN_VARIANT (*parm
);
7044 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7045 *parm
= TREE_TYPE (*parm
);
7048 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
7049 integers, with ones in positions corresponding to arguments in
7050 targs that were provided explicitly, and zeros elsewhere.
7052 If SUBR is 1, we're being called recursively (to unify the
7053 arguments of a function or method parameter of a function
7057 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7058 strict
, allow_incomplete
, explicit_mask
)
7059 tree tparms
, targs
, parms
, args
;
7061 unification_kind_t strict
;
7062 int allow_incomplete
;
7067 int ntparms
= TREE_VEC_LENGTH (tparms
);
7070 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7071 my_friendly_assert (parms
== NULL_TREE
7072 || TREE_CODE (parms
) == TREE_LIST
, 290);
7073 /* ARGS could be NULL (via a call from parse.y to
7074 build_x_function_call). */
7076 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7077 my_friendly_assert (ntparms
> 0, 292);
7082 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7086 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7090 sub_strict
= UNIFY_ALLOW_NONE
;
7094 my_friendly_abort (0);
7098 && parms
!= void_list_node
7100 && args
!= void_list_node
)
7102 parm
= TREE_VALUE (parms
);
7103 parms
= TREE_CHAIN (parms
);
7104 arg
= TREE_VALUE (args
);
7105 args
= TREE_CHAIN (args
);
7107 if (arg
== error_mark_node
)
7109 if (arg
== unknown_type_node
)
7110 /* We can't deduce anything from this, but we might get all the
7111 template args from other function args. */
7114 /* Conversions will be performed on a function argument that
7115 corresponds with a function parameter that contains only
7116 non-deducible template parameters and explicitly specified
7117 template parameters. */
7118 if (! uses_template_parms (parm
))
7122 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7123 type
= TREE_TYPE (arg
);
7130 if (strict
== DEDUCE_EXACT
)
7132 if (same_type_p (parm
, type
))
7136 /* It might work; we shouldn't check now, because we might
7137 get into infinite recursion. Overload resolution will
7145 if (TREE_CODE (arg
) == VAR_DECL
)
7146 arg
= TREE_TYPE (arg
);
7147 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
7148 arg
= TREE_TYPE (arg
);
7150 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7152 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7153 if (type_unknown_p (arg
))
7155 /* [temp.deduct.type] A template-argument can be deduced from
7156 a pointer to function or pointer to member function
7157 argument if the set of overloaded functions does not
7158 contain function templates and at most one of a set of
7159 overloaded functions provides a unique match. */
7161 if (resolve_overloaded_unification
7162 (tparms
, targs
, parm
, arg
, strict
, sub_strict
, explicit_mask
)
7167 arg
= TREE_TYPE (arg
);
7171 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7173 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
,
7182 /* Fail if we've reached the end of the parm list, and more args
7183 are present, and the parm list isn't variadic. */
7184 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7186 /* Fail if parms are left and they don't have default values. */
7188 && parms
!= void_list_node
7189 && TREE_PURPOSE (parms
) == NULL_TREE
)
7192 for (i
= 0; i
< ntparms
; i
++)
7193 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7195 if (!allow_incomplete
)
7196 error ("incomplete type unification");
7202 /* Subroutine of type_unification_real. Args are like the variables at the
7203 call site. ARG is an overloaded function (or template-id); we try
7204 deducing template args from each of the overloads, and if only one
7205 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7208 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7209 sub_strict
, explicit_mask
)
7210 tree tparms
, targs
, parm
, arg
;
7211 unification_kind_t strict
;
7215 tree tempargs
= copy_node (targs
);
7218 if (TREE_CODE (arg
) == ADDR_EXPR
)
7219 arg
= TREE_OPERAND (arg
, 0);
7221 if (TREE_CODE (arg
) == COMPONENT_REF
)
7222 /* Handle `&x' where `x' is some static or non-static member
7224 arg
= TREE_OPERAND (arg
, 1);
7226 /* Strip baselink information. */
7227 while (TREE_CODE (arg
) == TREE_LIST
)
7228 arg
= TREE_VALUE (arg
);
7230 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7232 /* If we got some explicit template args, we need to plug them into
7233 the affected templates before we try to unify, in case the
7234 explicit args will completely resolve the templates in question. */
7236 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7237 arg
= TREE_OPERAND (arg
, 0);
7239 for (; arg
; arg
= OVL_NEXT (arg
))
7241 tree fn
= OVL_CURRENT (arg
);
7244 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7247 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
7250 elem
= tsubst (TREE_TYPE (fn
), subargs
, NULL_TREE
);
7251 if (TREE_CODE (elem
) == METHOD_TYPE
)
7252 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7253 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7254 strict
, sub_strict
, explicit_mask
);
7258 else if (TREE_CODE (arg
) == OVERLOAD
)
7260 for (; arg
; arg
= OVL_NEXT (arg
))
7262 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
7263 if (TREE_CODE (type
) == METHOD_TYPE
)
7264 type
= build_ptrmemfunc_type (build_pointer_type (type
));
7265 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
7267 strict
, sub_strict
, explicit_mask
);
7271 my_friendly_abort (981006);
7273 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7274 to function or pointer to member function argument if the set of
7275 overloaded functions does not contain function templates and at most
7276 one of a set of overloaded functions provides a unique match.
7278 So if we found multiple possibilities, we return success but don't
7283 int i
= TREE_VEC_LENGTH (targs
);
7285 if (TREE_VEC_ELT (tempargs
, i
))
7286 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
7294 /* Subroutine of resolve_overloaded_unification; does deduction for a single
7295 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7296 different overloads deduce different arguments for a given parm.
7297 Returns 1 on success. */
7300 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
7301 sub_strict
, explicit_mask
)
7302 tree tparms
, orig_targs
, targs
, parm
, arg
;
7303 unification_kind_t strict
;
7311 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7312 to function or pointer to member function argument if the set of
7313 overloaded functions does not contain function templates and at most
7314 one of a set of overloaded functions provides a unique match.
7316 So if this is a template, just return success. */
7318 if (uses_template_parms (arg
))
7321 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7323 /* We don't copy orig_targs for this because if we have already deduced
7324 some template args from previous args, unify would complain when we
7325 try to deduce a template parameter for the same argument, even though
7326 there isn't really a conflict. */
7327 nargs
= TREE_VEC_LENGTH (targs
);
7328 tempargs
= make_scratch_vec (nargs
);
7330 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explicit_mask
) != 0)
7333 /* First make sure we didn't deduce anything that conflicts with
7334 explicitly specified args. */
7335 for (i
= nargs
; i
--; )
7337 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7338 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
7340 if (elt
== NULL_TREE
)
7342 else if (uses_template_parms (elt
))
7344 /* Since we're unifying against ourselves, we will fill in template
7345 args used in the function parm list with our own template parms.
7347 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
7350 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
7354 for (i
= nargs
; i
--; )
7356 tree elt
= TREE_VEC_ELT (tempargs
, i
);
7359 TREE_VEC_ELT (targs
, i
) = elt
;
7365 /* Subroutine of get_template_base. */
7368 get_template_base_recursive (tparms
, targs
, explicit_mask
,
7369 binfo
, rval
, template,
7381 tree type
= BINFO_TYPE (binfo
);
7382 tree tmpl
= CLASSTYPE_TI_TEMPLATE (template);
7384 if (CLASSTYPE_TEMPLATE_INFO (type
)
7385 && CLASSTYPE_TI_TEMPLATE (type
) == tmpl
)
7387 /* Copy the TPARMS and TARGS since we're only doing a
7388 speculative unification here. */
7389 tree copy_of_tparms
;
7393 push_expression_obstack ();
7394 copy_of_tparms
= copy_node (tparms
);
7395 copy_of_targs
= copy_node (targs
);
7398 i
= unify (copy_of_tparms
,
7400 CLASSTYPE_TI_ARGS (template),
7401 CLASSTYPE_TI_ARGS (type
),
7402 UNIFY_ALLOW_NONE
, explicit_mask
);
7408 if (rval
== NULL_TREE
|| rval
== type
)
7411 return error_mark_node
;
7415 binfos
= BINFO_BASETYPES (binfo
);
7416 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
7418 /* Process base types. */
7419 for (i
= 0; i
< n_baselinks
; i
++)
7421 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
7423 /* Find any specific instance of a virtual base, when searching with
7425 if (BINFO_MARKED (base_binfo
) == 0)
7427 int this_virtual
= via_virtual
|| TREE_VIA_VIRTUAL (base_binfo
);
7429 /* When searching for a non-virtual, we cannot mark
7430 virtually found binfos. */
7432 SET_BINFO_MARKED (base_binfo
);
7434 rval
= get_template_base_recursive (tparms
, targs
, explicit_mask
,
7436 template, this_virtual
);
7437 if (rval
== error_mark_node
)
7445 /* Given a template type TEMPLATE and a class type or binfo node BINFO,
7446 find the unique base type in BINFO that is an instance of TEMPLATE.
7447 If there are more than one, return error_mark_node. TEMPLATE may
7448 be the type of a partial specialization, as well as a plain
7449 template type. Used by unify. */
7452 get_template_base (tparms
, targs
, explicit_mask
, template, binfo
)
7459 tree type
= NULL_TREE
, rval
;
7461 if (TREE_CODE (binfo
) == TREE_VEC
)
7462 type
= BINFO_TYPE (binfo
);
7463 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo
)))
7465 type
= complete_type (binfo
);
7466 binfo
= TYPE_BINFO (type
);
7469 my_friendly_abort (92);
7471 rval
= get_template_base_recursive (tparms
, targs
, explicit_mask
,
7474 dfs_walk (binfo
, dfs_unmark
, markedp
);
7479 /* Returns the level of DECL, which declares a template parameter. */
7482 template_decl_level (decl
)
7485 switch (TREE_CODE (decl
))
7489 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
7492 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
7495 my_friendly_abort (0);
7500 /* Decide whether ARG can be unified with PARM, considering only the
7501 cv-qualifiers of each type, given STRICT as documented for unify.
7502 Returns non-zero iff the unification is OK on that basis.*/
7505 check_cv_quals_for_unify (strict
, arg
, parm
)
7510 return !((!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
7511 && !at_least_as_qualified_p (arg
, parm
))
7512 || (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
7513 && (!at_least_as_qualified_p (parm
, arg
))));
7516 /* Takes parameters as for type_unification. Returns 0 if the
7517 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7518 bitwise or of the following flags:
7521 Require an exact match between PARM and ARG.
7522 UNIFY_ALLOW_MORE_CV_QUAL:
7523 Allow the deduced ARG to be more cv-qualified than ARG.
7524 UNIFY_ALLOW_LESS_CV_QUAL:
7525 Allow the deduced ARG to be less cv-qualified than ARG.
7526 UNIFY_ALLOW_DERIVED:
7527 Allow the deduced ARG to be a template base class of ARG,
7528 or a pointer to a template base class of the type pointed to by
7530 UNIFY_ALLOW_INTEGER:
7531 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7532 case for more information. */
7535 unify (tparms
, targs
, parm
, arg
, strict
, explicit_mask
)
7536 tree tparms
, targs
, parm
, arg
;
7544 /* I don't think this will do the right thing with respect to types.
7545 But the only case I've seen it in so far has been array bounds, where
7546 signedness is the only information lost, and I think that will be
7548 while (TREE_CODE (parm
) == NOP_EXPR
)
7549 parm
= TREE_OPERAND (parm
, 0);
7551 if (arg
== error_mark_node
)
7553 if (arg
== unknown_type_node
)
7554 /* We can't deduce anything from this, but we might get all the
7555 template args from other function args. */
7558 /* If PARM uses template parameters, then we can't bail out here,
7559 even if ARG == PARM, since we won't record unifications for the
7560 template parameters. We might need them if we're trying to
7561 figure out which of two things is more specialized. */
7562 if (arg
== parm
&& !uses_template_parms (parm
))
7565 /* Immediately reject some pairs that won't unify because of
7566 cv-qualification mismatches. */
7567 if (TREE_CODE (arg
) == TREE_CODE (parm
)
7568 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
7569 /* We check the cv-qualifiers when unifying with template type
7570 parameters below. We want to allow ARG `const T' to unify with
7571 PARM `T' for example, when computing which of two templates
7572 is more specialized, for example. */
7573 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
7574 && !check_cv_quals_for_unify (strict
, arg
, parm
))
7577 switch (TREE_CODE (parm
))
7580 /* In a type which contains a nested-name-specifier, template
7581 argument values cannot be deduced for template parameters used
7582 within the nested-name-specifier. */
7585 case TEMPLATE_TYPE_PARM
:
7586 case TEMPLATE_TEMPLATE_PARM
:
7587 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7589 if (TEMPLATE_TYPE_LEVEL (parm
)
7590 != template_decl_level (tparm
))
7591 /* The PARM is not one we're trying to unify. Just check
7592 to see if it matches ARG. */
7593 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7594 && same_type_p (parm
, arg
)) ? 0 : 1;
7595 idx
= TEMPLATE_TYPE_IDX (parm
);
7596 targ
= TREE_VEC_ELT (targs
, idx
);
7597 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
7599 /* Check for mixed types and values. */
7600 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
7601 && TREE_CODE (tparm
) != TYPE_DECL
)
7602 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
7603 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
7606 if (!strict
&& targ
!= NULL_TREE
7607 && explicit_mask
&& explicit_mask
[idx
])
7608 /* An explicit template argument. Don't even try to match
7609 here; the overload resolution code will manage check to
7610 see whether the call is legal. */
7613 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
7615 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
7617 /* We arrive here when PARM does not involve template
7620 /* ARG must be constructed from a template class. */
7621 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
7625 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
7626 tree parmvec
= TYPE_TI_ARGS (parm
);
7627 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
7629 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
7632 /* The parameter and argument roles have to be switched here
7633 in order to handle default arguments properly. For example,
7634 template<template <class> class TT> void f(TT<int>)
7635 should be able to accept vector<int> which comes from
7636 template <class T, class Allocator = allocator>
7639 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
7643 /* Deduce arguments T, i from TT<T> or TT<i>.
7644 We check each element of PARMVEC and ARGVEC individually
7645 rather than the whole TREE_VEC since they can have
7646 different number of elements. */
7648 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
7650 tree t
= TREE_VEC_ELT (parmvec
, i
);
7652 if (unify (tparms
, targs
, t
,
7653 TREE_VEC_ELT (argvec
, i
),
7654 UNIFY_ALLOW_NONE
, explicit_mask
))
7658 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
7663 /* If PARM is `const T' and ARG is only `int', we don't have
7664 a match unless we are allowing additional qualification.
7665 If ARG is `const int' and PARM is just `T' that's OK;
7666 that binds `const int' to `T'. */
7667 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
7671 /* Consider the case where ARG is `const volatile int' and
7672 PARM is `const T'. Then, T should be `volatile int'. */
7674 cp_build_qualified_type (arg
,
7676 & ~CP_TYPE_QUALS (parm
));
7679 /* Simple cases: Value already set, does match or doesn't. */
7680 if (targ
!= NULL_TREE
7681 && (same_type_p (targ
, arg
)
7682 || (explicit_mask
&& explicit_mask
[idx
])))
7687 /* Make sure that ARG is not a variable-sized array. (Note that
7688 were talking about variable-sized arrays (like `int[n]'),
7689 rather than arrays of unknown size (like `int[]').) We'll
7690 get very confused by such a type since the bound of the array
7691 will not be computable in an instantiation. Besides, such
7692 types are not allowed in ISO C++, so we can do as we please
7694 if (TREE_CODE (arg
) == ARRAY_TYPE
7695 && !uses_template_parms (arg
)
7696 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
7700 TREE_VEC_ELT (targs
, idx
) = arg
;
7703 case TEMPLATE_PARM_INDEX
:
7704 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
7706 if (TEMPLATE_PARM_LEVEL (parm
)
7707 != template_decl_level (tparm
))
7708 /* The PARM is not one we're trying to unify. Just check
7709 to see if it matches ARG. */
7710 return (TREE_CODE (arg
) == TREE_CODE (parm
)
7711 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
7713 idx
= TEMPLATE_PARM_IDX (parm
);
7714 targ
= TREE_VEC_ELT (targs
, idx
);
7718 int i
= (cp_tree_equal (targ
, arg
) > 0);
7724 my_friendly_abort (42);
7727 /* [temp.deduct.type] If, in the declaration of a function template
7728 with a non-type template-parameter, the non-type
7729 template-parameter is used in an expression in the function
7730 parameter-list and, if the corresponding template-argument is
7731 deduced, the template-argument type shall match the type of the
7732 template-parameter exactly, except that a template-argument
7733 deduced from an array bound may be of any integral type. */
7734 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
7736 else if ((strict
& UNIFY_ALLOW_INTEGER
)
7737 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
7738 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
7743 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
7750 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
7751 return (unify (tparms
, targs
, parm
,
7752 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
,
7755 if (TREE_CODE (arg
) != POINTER_TYPE
)
7758 /* [temp.deduct.call]
7760 A can be another pointer or pointer to member type that can
7761 be converted to the deduced A via a qualification
7762 conversion (_conv.qual_).
7764 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
7765 This will allow for additional cv-qualification of the
7766 pointed-to types if appropriate. In general, this is a bit
7767 too generous; we are only supposed to allow qualification
7768 conversions and this method will allow an ARG of char** and
7769 a deduced ARG of const char**. However, overload
7770 resolution will subsequently invalidate the candidate, so
7771 this is probably OK. */
7772 sub_strict
= strict
;
7774 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
7775 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg
)))
7776 /* The derived-to-base conversion only persists through one
7777 level of pointers. */
7778 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
7780 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE
7781 (arg
), sub_strict
, explicit_mask
);
7784 case REFERENCE_TYPE
:
7785 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
7787 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7788 UNIFY_ALLOW_NONE
, explicit_mask
);
7791 if (TREE_CODE (arg
) != ARRAY_TYPE
)
7793 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
7794 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
7796 if (TYPE_DOMAIN (parm
) != NULL_TREE
7797 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
7798 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
, explicit_mask
) != 0)
7800 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7801 UNIFY_ALLOW_NONE
, explicit_mask
);
7808 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7811 if (TREE_CODE (parm
) == INTEGER_TYPE
7812 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
7814 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
7815 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
7816 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
,
7819 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
7820 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
7821 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
,
7825 /* We use the TYPE_MAIN_VARIANT since we have already
7826 checked cv-qualification at the top of the
7828 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
7829 TYPE_MAIN_VARIANT (parm
)))
7832 /* As far as unification is concerned, this wins. Later checks
7833 will invalidate it if necessary. */
7836 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
7837 /* Type INTEGER_CST can come from ordinary constant template args. */
7839 while (TREE_CODE (arg
) == NOP_EXPR
)
7840 arg
= TREE_OPERAND (arg
, 0);
7842 if (TREE_CODE (arg
) != INTEGER_CST
)
7844 return !tree_int_cst_equal (parm
, arg
);
7849 if (TREE_CODE (arg
) != TREE_VEC
)
7851 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
7853 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
7854 if (unify (tparms
, targs
,
7855 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
7856 UNIFY_ALLOW_NONE
, explicit_mask
))
7863 if (TYPE_PTRMEMFUNC_FLAG (parm
))
7864 return unify (tparms
, targs
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
7865 arg
, strict
, explicit_mask
);
7867 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7870 if (CLASSTYPE_TEMPLATE_INFO (parm
))
7873 if (strict
& UNIFY_ALLOW_DERIVED
)
7874 /* [temp.deduct.call]
7876 If P is a class, and P has the form template-id, then A
7877 can be a derived class of the deduced A. Likewise, if
7878 P is a pointer to a class of the form template-id, A
7879 can be a pointer to a derived class pointed to by the
7882 The call to get_template_base also handles the case
7883 where PARM and ARG are the same type, i.e., where no
7884 derivation is involved. */
7885 t
= get_template_base (tparms
, targs
, explicit_mask
,
7887 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
7888 && (CLASSTYPE_TI_TEMPLATE (parm
)
7889 == CLASSTYPE_TI_TEMPLATE (arg
)))
7890 /* Perhaps PARM is something like S<U> and ARG is S<int>.
7891 Then, we should unify `int' and `U'. */
7894 if (! t
|| t
== error_mark_node
)
7897 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
7898 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
,
7901 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
7902 TYPE_MAIN_VARIANT (arg
)))
7908 if (TREE_CODE (arg
) != TREE_CODE (parm
))
7911 if (unify (tparms
, targs
, TREE_TYPE (parm
),
7912 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7914 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
7915 TYPE_ARG_TYPES (arg
), 1,
7916 DEDUCE_EXACT
, 0, explicit_mask
);
7919 if (TREE_CODE (arg
) != OFFSET_TYPE
)
7921 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
7922 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
, explicit_mask
))
7924 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
7925 strict
, explicit_mask
);
7928 if (arg
!= decl_constant_value (parm
))
7933 /* Matched cases are handled by the ARG == PARM test above. */
7937 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
7939 /* We handle this case specially, since it comes up with
7940 arrays. In particular, something like:
7942 template <int N> void f(int (&x)[N]);
7944 Here, we are trying to unify the range type, which
7945 looks like [0 ... (N - 1)]. */
7947 t1
= TREE_OPERAND (parm
, 0);
7948 t2
= TREE_OPERAND (parm
, 1);
7950 /* Should this be a regular fold? */
7951 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
7955 return unify (tparms
, targs
, t1
, t
, strict
, explicit_mask
);
7957 /* else fall through */
7960 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
7962 /* We're looking at an expression. This can happen with
7966 void foo(S<I>, S<I + 2>);
7968 If the call looked like:
7970 foo(S<2>(), S<4>());
7972 we would have already matched `I' with `2'. Now, we'd
7973 like to know if `4' matches `I + 2'. So, we substitute
7974 into that expression, and fold constants, in the hope of
7977 maybe_fold_nontype_arg (tsubst_expr (parm
, targs
, NULL_TREE
));
7978 tree a
= maybe_fold_nontype_arg (arg
);
7980 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t
))))
7981 /* Good, we mangaged to simplify the exression. */
7982 return unify (tparms
, targs
, t
, a
, UNIFY_ALLOW_NONE
,
7985 /* Bad, we couldn't simplify this. Assume it doesn't
7990 sorry ("use of `%s' in template type unification",
7991 tree_code_name
[(int) TREE_CODE (parm
)]);
7997 /* Called if RESULT is explicitly instantiated, or is a member of an
7998 explicitly instantiated class, or if using -frepo and the
7999 instantiation of RESULT has been assigned to this file. */
8002 mark_decl_instantiated (result
, extern_p
)
8006 if (TREE_CODE (result
) != FUNCTION_DECL
)
8007 /* The TREE_PUBLIC flag for function declarations will have been
8008 set correctly by tsubst. */
8009 TREE_PUBLIC (result
) = 1;
8013 DECL_INTERFACE_KNOWN (result
) = 1;
8014 DECL_NOT_REALLY_EXTERN (result
) = 1;
8016 /* Always make artificials weak. */
8017 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8018 comdat_linkage (result
);
8019 /* For WIN32 we also want to put explicit instantiations in
8020 linkonce sections. */
8021 else if (TREE_PUBLIC (result
))
8022 maybe_make_one_only (result
);
8024 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8025 mark_inline_for_output (result
);
8028 /* Given two function templates PAT1 and PAT2, and explicit template
8029 arguments EXPLICIT_ARGS return:
8031 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8032 -1 if PAT2 is more specialized than PAT1.
8033 0 if neither is more specialized. */
8036 more_specialized (pat1
, pat2
, explicit_args
)
8037 tree pat1
, pat2
, explicit_args
;
8042 targs
= get_bindings_overload (pat1
, pat2
, explicit_args
);
8046 targs
= get_bindings_overload (pat2
, pat1
, explicit_args
);
8053 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8055 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8056 -1 if PAT2 is more specialized than PAT1.
8057 0 if neither is more specialized. */
8060 more_specialized_class (pat1
, pat2
)
8066 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8067 TREE_PURPOSE (pat2
));
8071 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8072 TREE_PURPOSE (pat1
));
8079 /* Return the template arguments that will produce the function signature
8080 DECL from the function template FN, with the explicit template
8081 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8085 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8086 tree fn
, decl
, explicit_args
;
8089 int ntparms
= DECL_NTPARMS (fn
);
8090 tree targs
= make_scratch_vec (ntparms
);
8091 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
8092 tree extra_fn_arg
= NULL_TREE
;
8095 if (DECL_STATIC_FUNCTION_P (fn
)
8096 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8098 /* Sometimes we are trying to figure out what's being
8099 specialized by a declaration that looks like a method, and it
8100 turns out to be a static member function. */
8101 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
8102 && !is_member_template (fn
))
8103 /* The natural thing to do here seems to be to remove the
8104 spurious `this' parameter from the DECL, but that prevents
8105 unification from making use of the class type. So,
8106 instead, we have fn_type_unification add to the parameters
8108 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
8110 /* In this case, though, adding the extra_fn_arg can confuse
8111 things, so we remove from decl_arg_types instead. */
8112 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8115 i
= fn_type_unification (fn
, explicit_args
, targs
,
8117 TREE_TYPE (TREE_TYPE (decl
)),
8126 /* Check to see that the resulting return type is also OK. */
8127 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
8130 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
8137 /* For most uses, we want to check the return type. */
8140 get_bindings (fn
, decl
, explicit_args
)
8141 tree fn
, decl
, explicit_args
;
8143 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8146 /* But for more_specialized, we only care about the parameter types. */
8149 get_bindings_overload (fn
, decl
, explicit_args
)
8150 tree fn
, decl
, explicit_args
;
8152 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8155 /* Return the innermost template arguments that, when applied to a
8156 template specialization whose innermost template parameters are
8157 TPARMS, and whose specialization arguments are ARGS, yield the
8160 For example, suppose we have:
8162 template <class T, class U> struct S {};
8163 template <class T> struct S<T*, int> {};
8165 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8166 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8167 int}. The resulting vector will be {double}, indicating that `T'
8168 is bound to `double'. */
8171 get_class_bindings (tparms
, parms
, args
)
8172 tree tparms
, parms
, args
;
8174 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8175 tree vec
= make_temp_vec (ntparms
);
8177 args
= innermost_args (args
);
8179 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
, 0))
8182 for (i
= 0; i
< ntparms
; ++i
)
8183 if (! TREE_VEC_ELT (vec
, i
))
8189 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8190 Pick the most specialized template, and return the corresponding
8191 instantiation, or if there is no corresponding instantiation, the
8192 template itself. EXPLICIT_ARGS is any template arguments explicity
8193 mentioned in a template-id. If there is no most specialized
8194 tempalte, error_mark_node is returned. If there are no templates
8195 at all, NULL_TREE is returned. */
8198 most_specialized_instantiation (instantiations
, explicit_args
)
8199 tree instantiations
;
8205 if (!instantiations
)
8208 champ
= instantiations
;
8209 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
8211 fate
= more_specialized (TREE_VALUE (champ
),
8212 TREE_VALUE (fn
), explicit_args
);
8219 fn
= TREE_CHAIN (fn
);
8221 return error_mark_node
;
8227 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
8229 fate
= more_specialized (TREE_VALUE (champ
),
8230 TREE_VALUE (fn
), explicit_args
);
8232 return error_mark_node
;
8235 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
8238 /* Return the most specialized of the list of templates in FNS that can
8239 produce an instantiation matching DECL, given the explicit template
8240 arguments EXPLICIT_ARGS. */
8243 most_specialized (fns
, decl
, explicit_args
)
8244 tree fns
, decl
, explicit_args
;
8246 tree candidates
= NULL_TREE
;
8249 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
8251 tree candidate
= TREE_VALUE (fn
);
8253 args
= get_bindings (candidate
, decl
, explicit_args
);
8255 candidates
= scratch_tree_cons (NULL_TREE
, candidate
,
8259 return most_specialized_instantiation (candidates
, explicit_args
);
8262 /* If DECL is a specialization of some template, return the most
8263 general such template. For example, given:
8265 template <class T> struct S { template <class U> void f(U); };
8267 if TMPL is `template <class U> void S<int>::f(U)' this will return
8268 the full template. This function will not trace past partial
8269 specializations, however. For example, given in addition:
8271 template <class T> struct S<T*> { template <class U> void f(U); };
8273 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8274 `template <class T> template <class U> S<T*>::f(U)'. */
8277 most_general_template (decl
)
8280 while (DECL_TEMPLATE_INFO (decl
))
8281 decl
= DECL_TI_TEMPLATE (decl
);
8286 /* Return the most specialized of the class template specializations
8287 of TMPL which can produce an instantiation matching ARGS, or
8288 error_mark_node if the choice is ambiguous. */
8291 most_specialized_class (tmpl
, args
)
8295 tree list
= NULL_TREE
;
8300 tmpl
= most_general_template (tmpl
);
8301 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
8304 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
8307 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
8308 TREE_TYPE (list
) = TREE_TYPE (t
);
8318 for (; t
; t
= TREE_CHAIN (t
))
8320 fate
= more_specialized_class (champ
, t
);
8329 return error_mark_node
;
8335 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
8337 fate
= more_specialized_class (champ
, t
);
8339 return error_mark_node
;
8345 /* called from the parser. */
8348 do_decl_instantiation (declspecs
, declarator
, storage
)
8349 tree declspecs
, declarator
, storage
;
8351 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
8352 tree result
= NULL_TREE
;
8355 if (! DECL_LANG_SPECIFIC (decl
))
8357 cp_error ("explicit instantiation of non-template `%#D'", decl
);
8360 else if (TREE_CODE (decl
) == VAR_DECL
)
8362 /* There is an asymmetry here in the way VAR_DECLs and
8363 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8364 the latter, the DECL we get back will be marked as a
8365 template instantiation, and the appropriate
8366 DECL_TEMPLATE_INFO will be set up. This does not happen for
8367 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8368 should handle VAR_DECLs as it currently handles
8370 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
8371 if (result
&& TREE_CODE (result
) != VAR_DECL
)
8373 cp_error ("no matching template for `%D' found", result
);
8377 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
8379 cp_error ("explicit instantiation of `%#D'", decl
);
8385 /* Check for various error cases. Note that if the explicit
8386 instantiation is legal the RESULT will currently be marked as an
8387 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8388 until we get here. */
8390 if (DECL_TEMPLATE_SPECIALIZATION (result
))
8394 No program shall both explicitly instantiate and explicitly
8395 specialize a template. */
8396 cp_error ("explicit instantiation of `%#D' after", result
);
8397 cp_error_at ("explicit specialization here", result
);
8400 else if (DECL_EXPLICIT_INSTANTIATION (result
))
8404 No program shall explicitly instantiate any template more
8407 We check DECL_INTERFACE_KNOWN so as not to complain when the
8408 first instantiation was `extern' and the second is not, and
8409 EXTERN_P for the opposite case. */
8410 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
)
8411 cp_error ("duplicate explicit instantiation of `%#D'", result
);
8413 /* If we've already instantiated the template, just return now. */
8414 if (DECL_INTERFACE_KNOWN (result
))
8417 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
8419 cp_error ("no matching template for `%D' found", result
);
8422 else if (!DECL_TEMPLATE_INFO (result
))
8424 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
8428 if (flag_external_templates
)
8431 if (storage
== NULL_TREE
)
8433 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8436 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
8440 cp_error ("storage class `%D' applied to template instantiation",
8443 SET_DECL_EXPLICIT_INSTANTIATION (result
);
8444 mark_decl_instantiated (result
, extern_p
);
8445 repo_template_instantiated (result
, extern_p
);
8447 instantiate_decl (result
);
8451 mark_class_instantiated (t
, extern_p
)
8455 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
8456 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
8457 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
8458 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
8459 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
8462 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
8463 rest_of_type_compilation (t
, 1);
8468 do_type_instantiation (t
, storage
)
8475 if (TREE_CODE (t
) == TYPE_DECL
)
8478 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
8480 cp_error ("explicit instantiation of non-template type `%T'", t
);
8486 /* With -fexternal-templates, explicit instantiations are treated the same
8487 as implicit ones. */
8488 if (flag_external_templates
)
8491 if (TYPE_SIZE (t
) == NULL_TREE
)
8493 cp_error ("explicit instantiation of `%#T' before definition of template",
8498 if (storage
!= NULL_TREE
)
8501 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8502 IDENTIFIER_POINTER (storage
));
8504 if (storage
== ridpointers
[(int) RID_INLINE
])
8506 else if (storage
== ridpointers
[(int) RID_EXTERN
])
8508 else if (storage
== ridpointers
[(int) RID_STATIC
])
8512 cp_error ("storage class `%D' applied to template instantiation",
8518 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
8522 No program shall both explicitly instantiate and explicitly
8523 specialize a template. */
8524 cp_error ("explicit instantiation of `%#T' after", t
);
8525 cp_error_at ("explicit specialization here", t
);
8528 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
8532 No program shall explicitly instantiate any template more
8535 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8536 instantiation was `extern', and if EXTERN_P then the second
8537 is. Both cases are OK. */
8538 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
)
8539 cp_error ("duplicate explicit instantiation of `%#T'", t
);
8541 /* If we've already instantiated the template, just return now. */
8542 if (!CLASSTYPE_INTERFACE_ONLY (t
))
8546 mark_class_instantiated (t
, extern_p
);
8547 repo_template_instantiated (t
, extern_p
);
8555 /* In contrast to implicit instantiation, where only the
8556 declarations, and not the definitions, of members are
8557 instantiated, we have here:
8561 The explicit instantiation of a class template specialization
8562 implies the instantiation of all of its members not
8563 previously explicitly specialized in the translation unit
8564 containing the explicit instantiation.
8566 Of course, we can't instantiate member template classes, since
8567 we don't have any arguments for them. Note that the standard
8568 is unclear on whether the instatiation of the members are
8569 *explicit* instantiations or not. We choose to be generous,
8570 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8571 the explicit instantiation of a class where some of the members
8572 have no definition in the current translation unit. */
8575 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8576 if (TREE_CODE (tmp
) == FUNCTION_DECL
8577 && DECL_TEMPLATE_INSTANTIATION (tmp
))
8579 mark_decl_instantiated (tmp
, extern_p
);
8580 repo_template_instantiated (tmp
, extern_p
);
8582 instantiate_decl (tmp
);
8585 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8586 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
8588 mark_decl_instantiated (tmp
, extern_p
);
8589 repo_template_instantiated (tmp
, extern_p
);
8591 instantiate_decl (tmp
);
8594 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
8595 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
8596 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
8597 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
8601 /* Given a function DECL, which is a specialization of TMPL, modify
8602 DECL to be a re-instantiation of TMPL with the same template
8603 arguments. TMPL should be the template into which tsubst'ing
8604 should occur for DECL, not the most general template.
8606 One reason for doing this is a scenario like this:
8609 void f(const T&, int i);
8611 void g() { f(3, 7); }
8614 void f(const T& t, const int i) { }
8616 Note that when the template is first instantiated, with
8617 instantiate_template, the resulting DECL will have no name for the
8618 first parameter, and the wrong type for the second. So, when we go
8619 to instantiate the DECL, we regenerate it. */
8622 regenerate_decl_from_template (decl
, tmpl
)
8632 args
= DECL_TI_ARGS (decl
);
8633 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8635 /* Unregister the specialization so that when we tsubst we will not
8636 just return DECL. We don't have to unregister DECL from TMPL
8637 because if would only be registered there if it were a partial
8638 instantiation of a specialization, which it isn't: it's a full
8640 gen_tmpl
= most_general_template (tmpl
);
8641 unregistered
= unregister_specialization (decl
, gen_tmpl
);
8643 /* If the DECL was not unregistered then something peculiar is
8644 happening: we created a specialization but did not call
8645 register_specialization for it. */
8646 my_friendly_assert (unregistered
, 0);
8648 if (TREE_CODE (decl
) == VAR_DECL
)
8649 /* Make sure that we can see identifiers, and compute access
8650 correctly, for the class members used in the declaration of
8651 this static variable. */
8652 pushclass (DECL_CONTEXT (decl
), 2);
8654 /* Do the substitution to get the new declaration. */
8655 new_decl
= tsubst (code_pattern
, args
, NULL_TREE
);
8657 if (TREE_CODE (decl
) == VAR_DECL
)
8659 /* Set up DECL_INITIAL, since tsubst doesn't. */
8660 DECL_INITIAL (new_decl
) =
8661 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
8662 DECL_TI_TEMPLATE (decl
));
8663 /* Pop the class context we pushed above. */
8667 if (TREE_CODE (decl
) == FUNCTION_DECL
)
8669 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
8671 DECL_INITIAL (new_decl
) = error_mark_node
;
8672 /* And don't complain about a duplicate definition. */
8673 DECL_INITIAL (decl
) = NULL_TREE
;
8676 /* The immediate parent of the new template is still whatever it was
8677 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
8678 general template. We also reset the DECL_ASSEMBLER_NAME since
8679 tsubst always calculates the name as if the function in question
8680 were really a template instance, and sometimes, with friend
8681 functions, this is not so. See tsubst_friend_function for
8683 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
8684 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
8685 DECL_RTL (new_decl
) = DECL_RTL (decl
);
8687 /* Call duplicate decls to merge the old and new declarations. */
8688 duplicate_decls (new_decl
, decl
);
8690 /* Now, re-register the specialization. */
8691 register_specialization (decl
, gen_tmpl
, args
);
8694 /* Produce the definition of D, a _DECL generated from a template. */
8697 instantiate_decl (d
)
8700 tree tmpl
= DECL_TI_TEMPLATE (d
);
8701 tree args
= DECL_TI_ARGS (d
);
8706 int nested
= in_function_p ();
8707 int pattern_defined
;
8709 char *file
= input_filename
;
8711 /* This function should only be used to instantiate templates for
8712 functions and static member variables. */
8713 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
8714 || TREE_CODE (d
) == VAR_DECL
, 0);
8716 if (DECL_TEMPLATE_INSTANTIATED (d
))
8717 /* D has already been instantiated. It might seem reasonable to
8718 check whether or not D is an explict instantiation, and, if so,
8719 stop here. But when an explicit instantiation is deferred
8720 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
8721 is set, even though we still need to do the instantiation. */
8724 /* If we already have a specialization of this declaration, then
8725 there's no reason to instantiate it. Note that
8726 retrieve_specialization gives us both instantiations and
8727 specializations, so we must explicitly check
8728 DECL_TEMPLATE_SPECIALIZATION. */
8729 gen_tmpl
= most_general_template (tmpl
);
8730 spec
= retrieve_specialization (gen_tmpl
, args
);
8731 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
8734 /* This needs to happen before any tsubsting. */
8735 if (! push_tinst_level (d
))
8738 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
8739 for the instantiation. This is not always the most general
8740 template. Consider, for example:
8743 struct S { template <class U> void f();
8744 template <> void f<int>(); };
8746 and an instantiation of S<double>::f<int>. We want TD to be the
8747 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
8750 /* An instantiation cannot have a definition, so we need a
8751 more general template. */
8752 DECL_TEMPLATE_INSTANTIATION (td
)
8753 /* We must also deal with friend templates. Given:
8755 template <class T> struct S {
8756 template <class U> friend void f() {};
8759 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
8760 so far as the language is concerned, but that's still
8761 where we get the pattern for the instantiation from. On
8762 ther hand, if the definition comes outside the class, say:
8764 template <class T> struct S {
8765 template <class U> friend void f();
8767 template <class U> friend void f() {}
8769 we don't need to look any further. That's what the check for
8770 DECL_INITIAL is for. */
8771 || (TREE_CODE (d
) == FUNCTION_DECL
8772 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
8773 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
8776 /* The present template, TD, should not be a definition. If it
8777 were a definition, we should be using it! Note that we
8778 cannot restructure the loop to just keep going until we find
8779 a template with a definition, since that might go too far if
8780 a specialization was declared, but not defined. */
8781 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
8782 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
8785 /* Fetch the more general template. */
8786 td
= DECL_TI_TEMPLATE (td
);
8789 code_pattern
= DECL_TEMPLATE_RESULT (td
);
8791 if (TREE_CODE (d
) == FUNCTION_DECL
)
8792 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
8794 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
8796 push_to_top_level ();
8797 lineno
= DECL_SOURCE_LINE (d
);
8798 input_filename
= DECL_SOURCE_FILE (d
);
8800 if (pattern_defined
)
8802 repo_template_used (d
);
8804 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
8806 if (flag_alt_external_templates
)
8808 if (interface_unknown
)
8809 warn_if_unknown_interface (d
);
8811 else if (DECL_INTERFACE_KNOWN (code_pattern
))
8813 DECL_INTERFACE_KNOWN (d
) = 1;
8814 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
8817 warn_if_unknown_interface (code_pattern
);
8821 import_export_decl (d
);
8824 /* Reject all external templates except inline functions. */
8825 if (DECL_INTERFACE_KNOWN (d
)
8826 && ! DECL_NOT_REALLY_EXTERN (d
)
8827 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
8830 if (TREE_CODE (d
) == VAR_DECL
8831 && TREE_READONLY (d
)
8832 && DECL_INITIAL (d
) == NULL_TREE
8833 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
8834 /* We need to set up DECL_INITIAL regardless of pattern_defined if
8835 the variable is a static const initialized in the class body. */;
8836 else if (! pattern_defined
8837 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
8840 /* Defer all templates except inline functions used in another
8841 function. We restore the source position here because it's used
8842 by add_pending_template. */
8844 input_filename
= file
;
8846 if (at_eof
&& !pattern_defined
8847 && DECL_EXPLICIT_INSTANTIATION (d
))
8850 The definition of a non-exported function template, a
8851 non-exported member function template, or a non-exported
8852 member function or static data member of a class template
8853 shall be present in every translation unit in which it is
8854 explicitly instantiated. */
8855 cp_error ("explicit instantiation of `%D' but no definition available",
8858 add_pending_template (d
);
8862 regenerate_decl_from_template (d
, td
);
8863 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
8865 /* We already set the file and line above. Reset them now in case
8866 they changed as a result of calling regenerate_decl_from_template. */
8867 lineno
= DECL_SOURCE_LINE (d
);
8868 input_filename
= DECL_SOURCE_FILE (d
);
8870 if (TREE_CODE (d
) == VAR_DECL
)
8872 DECL_IN_AGGR_P (d
) = 0;
8873 if (DECL_INTERFACE_KNOWN (d
))
8874 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
8877 DECL_EXTERNAL (d
) = 1;
8878 DECL_NOT_REALLY_EXTERN (d
) = 1;
8880 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
8882 else if (TREE_CODE (d
) == FUNCTION_DECL
)
8884 tree t
= DECL_SAVED_TREE (code_pattern
);
8886 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
8887 store_parm_decls ();
8889 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
8892 (TREE_OPERAND (t
, 0),
8893 tsubst_expr (TREE_OPERAND (t
, 1), args
, tmpl
));
8897 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
8899 current_member_init_list
8900 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
8901 current_base_init_list
8902 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
8907 /* Always keep the BLOCK node associated with the outermost
8908 pair of curly braces of a function. These are needed
8909 for correct operation of dwarfout.c. */
8912 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
8913 tsubst_expr (t
, args
, tmpl
);
8915 finish_function (lineno
, 0, nested
);
8920 input_filename
= file
;
8922 pop_from_top_level ();
8929 tsubst_expr_values (t
, argvec
)
8932 tree first
= NULL_TREE
;
8935 for (; t
; t
= TREE_CHAIN (t
))
8937 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
, NULL_TREE
);
8938 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, NULL_TREE
);
8939 *p
= build_tree_list (pur
, val
);
8940 p
= &TREE_CHAIN (*p
);
8951 last_tree
= TREE_CHAIN (last_tree
) = t
;
8958 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
8959 last_tree
= NULL_TREE
;
8966 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
8968 last_tree
= TREE_VALUE (saved_trees
);
8969 saved_trees
= TREE_CHAIN (saved_trees
);
8972 /* D is an undefined function declaration in the presence of templates with
8973 the same name, listed in FNS. If one of them can produce D as an
8974 instantiation, remember this so we can instantiate it at EOF if D has
8975 not been defined by that time. */
8978 add_maybe_template (d
, fns
)
8983 if (DECL_MAYBE_TEMPLATE (d
))
8986 t
= most_specialized (fns
, d
, NULL_TREE
);
8989 if (t
== error_mark_node
)
8991 cp_error ("ambiguous template instantiation for `%D'", d
);
8995 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
8996 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
8997 DECL_MAYBE_TEMPLATE (d
) = 1;
9000 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9003 set_current_access_from_decl (decl
)
9006 if (TREE_PRIVATE (decl
))
9007 current_access_specifier
= access_private_node
;
9008 else if (TREE_PROTECTED (decl
))
9009 current_access_specifier
= access_protected_node
;
9011 current_access_specifier
= access_public_node
;
9014 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9015 is the instantiation (which should have been created with
9016 start_enum) and ARGS are the template arguments to use. */
9019 tsubst_enum (tag
, newtag
, args
)
9026 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9031 /* Note that in a template enum, the TREE_VALUE is the
9032 CONST_DECL, not the corresponding INTEGER_CST. */
9033 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9037 /* Give this enumeration constant the correct access. */
9038 set_current_access_from_decl (TREE_VALUE (e
));
9040 /* Actually build the enumerator itself. */
9041 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9043 /* We save the enumerators we have built so far in the
9044 TYPE_VALUES so that if the enumeration constants for
9045 subsequent enumerators involve those for previous ones,
9046 tsubst_copy will be able to find them. */
9047 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
9048 TYPE_VALUES (newtag
) = elt
;
9051 finish_enum (newtag
);
9054 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9055 is either an instantiation or specialization of a template
9059 set_mangled_name_for_template_decl (decl
)
9062 tree saved_namespace
;
9063 tree context
= NULL_TREE
;
9072 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
9073 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
9075 /* The names of template functions must be mangled so as to indicate
9076 what template is being specialized with what template arguments.
9077 For example, each of the following three functions must get
9078 different mangled names:
9081 template <> void f<7>(int);
9082 template <> void f<8>(int); */
9084 targs
= DECL_TI_ARGS (decl
);
9085 if (uses_template_parms (targs
))
9086 /* This DECL is for a partial instantiation. There's no need to
9087 mangle the name of such an entity. */
9090 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
9091 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
9092 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
9094 /* There should be as many levels of arguments as there are levels
9096 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
9098 /* We now compute the PARMS and RET_TYPE to give to
9099 build_decl_overload_real. The PARMS and RET_TYPE are the
9100 parameter and return types of the template, after all but the
9101 innermost template arguments have been substituted, not the
9102 parameter and return types of the function DECL. For example,
9105 template <class T> T f(T);
9107 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
9108 A more subtle example is:
9110 template <class T> struct S { template <class U> void f(T, U); }
9112 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
9113 {int, U}. Thus, the args that we want to subsitute into the
9114 return and parameter type for the function are those in TARGS,
9115 with the innermost level omitted. */
9116 fn_type
= TREE_TYPE (tmpl
);
9117 if (DECL_STATIC_FUNCTION_P (decl
))
9118 context
= DECL_CLASS_CONTEXT (decl
);
9120 if (parm_depth
== 1)
9121 /* No substitution is necessary. */
9128 /* Replace the innermost level of the TARGS with NULL_TREEs to
9129 let tsubst know not to subsitute for those parameters. */
9130 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
9131 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
9132 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
9133 TMPL_ARGS_LEVEL (targs
, i
));
9134 SET_TMPL_ARGS_LEVEL (partial_args
,
9135 TMPL_ARGS_DEPTH (targs
),
9136 make_temp_vec (DECL_NTPARMS (tmpl
)));
9138 /* Now, do the (partial) substitution to figure out the
9139 appropriate function type. */
9140 fn_type
= tsubst (fn_type
, partial_args
, NULL_TREE
);
9141 if (DECL_STATIC_FUNCTION_P (decl
))
9142 context
= tsubst (context
, partial_args
, NULL_TREE
);
9144 /* Substitute into the template parameters to obtain the real
9145 innermost set of parameters. This step is important if the
9146 innermost set of template parameters contains value
9147 parameters whose types depend on outer template parameters. */
9148 TREE_VEC_LENGTH (partial_args
)--;
9149 tparms
= tsubst_template_parms (tparms
, partial_args
);
9152 /* Now, get the innermost parameters and arguments, and figure out
9153 the parameter and return types. */
9154 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
9155 targs
= innermost_args (targs
);
9156 ret_type
= TREE_TYPE (fn_type
);
9157 parm_types
= TYPE_ARG_TYPES (fn_type
);
9159 /* For a static member function, we generate a fake `this' pointer,
9160 for the purposes of mangling. This indicates of which class the
9161 function is a member. Because of:
9165 There shall not be a static and a nonstatic member function
9166 with the same name and the same parameter types
9168 we don't have to worry that this will result in a clash with a
9169 non-static member function. */
9170 if (DECL_STATIC_FUNCTION_P (decl
))
9171 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
9173 /* There should be the same number of template parameters as
9174 template arguments. */
9175 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
9178 /* If the template is in a namespace, we need to put that into the
9179 mangled name. Unfortunately, build_decl_overload_real does not
9180 get the decl to mangle, so it relies on the current
9181 namespace. Therefore, we set that here temporarily. */
9182 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
9183 saved_namespace
= current_namespace
;
9184 current_namespace
= CP_DECL_CONTEXT (decl
);
9186 /* Actually set the DCL_ASSEMBLER_NAME. */
9187 DECL_ASSEMBLER_NAME (decl
)
9188 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
9190 DECL_FUNCTION_MEMBER_P (decl
)
9191 + DECL_CONSTRUCTOR_P (decl
));
9193 /* Restore the previously active namespace. */
9194 current_namespace
= saved_namespace
;