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". */
46 /* The type of functions taking a tree, and some additional data, and
48 typedef int (*tree_fn_t
) PROTO((tree
, void*));
50 extern struct obstack permanent_obstack
;
53 extern char *input_filename
;
55 tree current_template_parms
;
56 HOST_WIDE_INT processing_template_decl
;
58 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
59 instantiations have been deferred, either because their definitions
60 were not yet available, or because we were putting off doing the
61 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
62 the instantiate request occurred; the TREE_VALUE is a either a DECL
63 (for a function or static data member), or a TYPE (for a class)
64 indicating what we are hoping to instantiate. */
65 static tree pending_templates
;
66 static tree
*template_tail
= &pending_templates
;
68 static tree maybe_templates
;
69 static tree
*maybe_template_tail
= &maybe_templates
;
71 int processing_specialization
;
72 int processing_explicit_instantiation
;
73 int processing_template_parmlist
;
74 static int template_header_count
;
76 static tree saved_trees
;
77 static varray_type inline_parm_levels
;
78 static size_t inline_parm_levels_used
;
80 #define obstack_chunk_alloc xmalloc
81 #define obstack_chunk_free free
83 #define UNIFY_ALLOW_NONE 0
84 #define UNIFY_ALLOW_MORE_CV_QUAL 1
85 #define UNIFY_ALLOW_LESS_CV_QUAL 2
86 #define UNIFY_ALLOW_DERIVED 4
87 #define UNIFY_ALLOW_INTEGER 8
89 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
90 virtual, or a base class of a virtual
92 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
93 type with the desired type. */
95 static int resolve_overloaded_unification
PROTO((tree
, tree
, tree
, tree
,
96 unification_kind_t
, int));
97 static int try_one_overload
PROTO((tree
, tree
, tree
, tree
, tree
,
98 unification_kind_t
, int));
99 static int unify
PROTO((tree
, tree
, tree
, tree
, int));
100 static void add_pending_template
PROTO((tree
));
101 static int push_tinst_level
PROTO((tree
));
102 static tree classtype_mangled_name
PROTO((tree
));
103 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
104 static tree tsubst_expr_values
PROTO((tree
, tree
));
105 static int list_eq
PROTO((tree
, tree
));
106 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
107 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int));
108 static void tsubst_enum
PROTO((tree
, tree
, tree
));
109 static tree add_to_template_args
PROTO((tree
, tree
));
110 static tree add_outermost_template_args
PROTO((tree
, tree
));
111 static void maybe_adjust_types_for_deduction
PROTO((unification_kind_t
, tree
*,
113 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
114 int, unification_kind_t
, int));
115 static void note_template_header
PROTO((int));
116 static tree maybe_fold_nontype_arg
PROTO((tree
));
117 static tree convert_nontype_argument
PROTO((tree
, tree
));
118 static tree convert_template_argument
PROTO ((tree
, tree
, tree
, int,
120 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
121 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
122 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
123 static int inline_needs_template_parms
PROTO((tree
));
124 static void push_inline_template_parms_recursive
PROTO((tree
, int));
125 static tree retrieve_specialization
PROTO((tree
, tree
));
126 static tree retrieve_local_specialization
PROTO((tree
, tree
));
127 static tree register_specialization
PROTO((tree
, tree
, tree
));
128 static tree register_local_specialization
PROTO((tree
, tree
, tree
));
129 static int unregister_specialization
PROTO((tree
, tree
));
130 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
131 static tree build_template_decl
PROTO((tree
, tree
));
132 static int mark_template_parm
PROTO((tree
, void *));
133 static tree tsubst_friend_function
PROTO((tree
, tree
));
134 static tree tsubst_friend_class
PROTO((tree
, tree
));
135 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
136 static int template_decl_level
PROTO((tree
));
137 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
138 static int check_cv_quals_for_unify
PROTO((int, tree
, tree
));
139 static tree tsubst_template_arg_vector
PROTO((tree
, tree
, int));
140 static tree tsubst_template_parms
PROTO((tree
, tree
, int));
141 static void regenerate_decl_from_template
PROTO((tree
, tree
));
142 static tree most_specialized
PROTO((tree
, tree
, tree
));
143 static tree most_specialized_class
PROTO((tree
, tree
));
144 static tree most_general_template
PROTO((tree
));
145 static void set_mangled_name_for_template_decl
PROTO((tree
));
146 static int template_class_depth_real
PROTO((tree
, int));
147 static tree tsubst_aggr_type
PROTO((tree
, tree
, int, tree
, int));
148 static tree tsubst_decl
PROTO((tree
, tree
, tree
, tree
));
149 static tree tsubst_arg_types
PROTO((tree
, tree
, int, tree
));
150 static tree tsubst_function_type
PROTO((tree
, tree
, int, tree
));
151 static void check_specialization_scope
PROTO((void));
152 static tree process_partial_specialization
PROTO((tree
));
153 static void set_current_access_from_decl
PROTO((tree
));
154 static void check_default_tmpl_args
PROTO((tree
, tree
, int, int));
155 static tree tsubst_call_declarator_parms
PROTO((tree
, tree
, int, tree
));
156 static tree get_template_base_recursive
PROTO((tree
, tree
,
157 tree
, tree
, tree
, int));
158 static tree get_template_base
PROTO((tree
, tree
, tree
, tree
));
159 static tree try_class_unification
PROTO((tree
, tree
, tree
, tree
));
160 static int coerce_template_template_parms
PROTO((tree
, tree
, int,
162 static tree determine_specialization
PROTO((tree
, tree
, tree
*, int));
163 static int template_args_equal
PROTO((tree
, tree
));
164 static void print_template_context
PROTO((int));
165 static void tsubst_default_arguments
PROTO((tree
));
167 /* We use TREE_VECs to hold template arguments. If there is only one
168 level of template arguments, then the TREE_VEC contains the
169 arguments directly. If there is more than one level of template
170 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
171 containing the template arguments for a single level. The first
172 entry in the outer TREE_VEC is the outermost level of template
173 parameters; the last is the innermost.
175 It is incorrect to ever form a template argument vector containing
176 only one level of arguments, but which is a TREE_VEC containing as
177 its only entry the TREE_VEC for that level. */
179 /* Non-zero if the template arguments is actually a vector of vectors,
180 rather than just a vector. */
181 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
183 && TREE_CODE (NODE) == TREE_VEC \
184 && TREE_VEC_LENGTH (NODE) > 0 \
185 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
186 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
188 /* The depth of a template argument vector. When called directly by
189 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
190 template arguments. In fact, we may even see NULL_TREE if there
191 are no template arguments. In both of those cases, there is only
192 one level of template arguments. */
193 #define TMPL_ARGS_DEPTH(NODE) \
194 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
196 /* The LEVELth level of the template ARGS. Note that template
197 parameter levels are indexed from 1, not from 0. */
198 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
199 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
200 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
202 /* Set the LEVELth level of the template ARGS to VAL. This macro does
203 not work with single-level argument vectors. */
204 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
205 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
207 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
208 #define TMPL_ARG(ARGS, LEVEL, IDX) \
209 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
211 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
212 macro does not work with single-level argument vectors. */
213 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
214 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
216 /* Given a single level of template arguments in NODE, return the
217 number of arguments. */
218 #define NUM_TMPL_ARGS(NODE) \
219 ((NODE) == NULL_TREE ? 0 \
220 : (TREE_CODE (NODE) == TREE_VEC \
221 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
223 /* The number of levels of template parameters given by NODE. */
224 #define TMPL_PARMS_DEPTH(NODE) \
225 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
227 /* Do any processing required when DECL (a member template declaration
228 using TEMPLATE_PARAMETERS as its innermost parameter list) is
229 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
230 it is a specialization, in which case the DECL itself is returned. */
233 finish_member_template_decl (decl
)
236 if (decl
== NULL_TREE
|| decl
== void_type_node
)
238 else if (decl
== error_mark_node
)
239 /* By returning NULL_TREE, the parser will just ignore this
240 declaration. We have already issued the error. */
242 else if (TREE_CODE (decl
) == TREE_LIST
)
244 /* Assume that the class is the only declspec. */
245 decl
= TREE_VALUE (decl
);
246 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
247 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
249 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
250 check_member_template (tmpl
);
255 else if (DECL_TEMPLATE_INFO (decl
))
257 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
259 check_member_template (DECL_TI_TEMPLATE (decl
));
260 return DECL_TI_TEMPLATE (decl
);
266 cp_error ("invalid member template declaration `%D'", decl
);
268 return error_mark_node
;
271 /* Returns the template nesting level of the indicated class TYPE.
281 A<T>::B<U> has depth two, while A<T> has depth one.
282 Both A<T>::B<int> and A<int>::B<U> have depth one, if
283 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
286 This function is guaranteed to return 0 if passed NULL_TREE so
287 that, for example, `template_class_depth (current_class_type)' is
291 template_class_depth_real (type
, count_specializations
)
293 int count_specializations
;
298 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
299 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
300 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
302 if (TREE_CODE (type
) != FUNCTION_DECL
)
304 if (CLASSTYPE_TEMPLATE_INFO (type
)
305 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
306 && ((count_specializations
307 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
308 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
313 if (DECL_TEMPLATE_INFO (type
)
314 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
315 && ((count_specializations
316 && DECL_TEMPLATE_SPECIALIZATION (type
))
317 || uses_template_parms (DECL_TI_ARGS (type
))))
325 /* Returns the template nesting level of the indicated class TYPE.
326 Like template_class_depth_real, but instantiations do not count in
330 template_class_depth (type
)
333 return template_class_depth_real (type
, /*count_specializations=*/0);
336 /* Returns 1 if processing DECL as part of do_pending_inlines
337 needs us to push template parms. */
340 inline_needs_template_parms (decl
)
343 if (! DECL_TEMPLATE_INFO (decl
))
346 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
347 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
350 /* Subroutine of maybe_begin_member_template_processing.
351 Push the template parms in PARMS, starting from LEVELS steps into the
352 chain, and ending at the beginning, since template parms are listed
356 push_inline_template_parms_recursive (parmlist
, levels
)
360 tree parms
= TREE_VALUE (parmlist
);
364 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
366 ++processing_template_decl
;
367 current_template_parms
368 = tree_cons (build_int_2 (0, processing_template_decl
),
369 parms
, current_template_parms
);
370 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
373 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
375 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
376 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
378 switch (TREE_CODE (parm
))
387 /* Make a CONST_DECL as is done in process_template_parm.
388 It is ugly that we recreate this here; the original
389 version built in process_template_parm is no longer
391 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
393 SET_DECL_ARTIFICIAL (decl
);
394 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
395 DECL_TEMPLATE_PARM_P (decl
) = 1;
401 my_friendly_abort (0);
406 /* Restore the template parameter context for a member template or
407 a friend template defined in a class definition. */
410 maybe_begin_member_template_processing (decl
)
416 if (inline_needs_template_parms (decl
))
418 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
419 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
421 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
424 parms
= TREE_CHAIN (parms
);
427 push_inline_template_parms_recursive (parms
, levels
);
430 /* Remember how many levels of template parameters we pushed so that
431 we can pop them later. */
432 if (!inline_parm_levels
)
433 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
434 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
435 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
436 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
437 ++inline_parm_levels_used
;
440 /* Undo the effects of begin_member_template_processing. */
443 maybe_end_member_template_processing ()
447 if (!inline_parm_levels_used
)
450 --inline_parm_levels_used
;
452 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
455 --processing_template_decl
;
456 current_template_parms
= TREE_CHAIN (current_template_parms
);
461 /* Returns non-zero iff T is a member template function. We must be
464 template <class T> class C { void f(); }
466 Here, f is a template function, and a member, but not a member
467 template. This function does not concern itself with the origin of
468 T, only its present state. So if we have
470 template <class T> class C { template <class U> void f(U); }
472 then neither C<int>::f<char> nor C<T>::f<double> is considered
473 to be a member template. But, `template <class U> void
474 C<int>::f(U)' is considered a member template. */
477 is_member_template (t
)
480 if (!DECL_FUNCTION_TEMPLATE_P (t
))
481 /* Anything that isn't a function or a template function is
482 certainly not a member template. */
485 /* A local class can't have member templates. */
486 if (hack_decl_function_context (t
))
489 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
490 /* If there are more levels of template parameters than
491 there are template classes surrounding the declaration,
492 then we have a member template. */
493 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
494 template_class_depth (DECL_CLASS_CONTEXT (t
))));
498 /* Returns non-zero iff T is a member template class. See
499 is_member_template for a description of what precisely constitutes
500 a member template. */
503 is_member_template_class (t
)
506 if (!DECL_CLASS_TEMPLATE_P (t
))
507 /* Anything that isn't a class template, is certainly not a member
511 if (!DECL_CLASS_SCOPE_P (t
))
512 /* Anything whose context isn't a class type is surely not a
516 /* If there are more levels of template parameters than there are
517 template classes surrounding the declaration, then we have a
519 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
520 template_class_depth (DECL_CONTEXT (t
)));
524 /* Return a new template argument vector which contains all of ARGS,
525 but has as its innermost set of arguments the EXTRA_ARGS. The
526 resulting vector will be built on a temporary obstack, and so must
527 be explicitly copied to the permanent obstack, if required. */
530 add_to_template_args (args
, extra_args
)
539 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
540 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
542 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
543 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
545 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
546 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
551 /* Like add_to_template_args, but only the outermost ARGS are added to
552 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
553 (EXTRA_ARGS) levels are added. This function is used to combine
554 the template arguments from a partial instantiation with the
555 template arguments used to attain the full instantiation from the
556 partial instantiation. */
559 add_outermost_template_args (args
, extra_args
)
565 /* If there are more levels of EXTRA_ARGS than there are ARGS,
566 something very fishy is going on. */
567 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
570 /* If *all* the new arguments will be the EXTRA_ARGS, just return
572 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
575 /* For the moment, we make ARGS look like it contains fewer levels. */
576 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
578 new_args
= add_to_template_args (args
, extra_args
);
580 /* Now, we restore ARGS to its full dimensions. */
581 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
586 /* We've got a template header coming up; push to a new level for storing
590 begin_template_parm_list ()
592 /* We use a non-tag-transparent scope here, which causes pushtag to
593 put tags in this scope, rather than in the enclosing class or
594 namespace scope. This is the right thing, since we want
595 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
596 global template class, push_template_decl handles putting the
597 TEMPLATE_DECL into top-level scope. For a nested template class,
600 template <class T> struct S1 {
601 template <class T> struct S2 {};
604 pushtag contains special code to call pushdecl_with_scope on the
605 TEMPLATE_DECL for S2. */
607 declare_pseudo_global_level ();
608 ++processing_template_decl
;
609 ++processing_template_parmlist
;
610 note_template_header (0);
613 /* This routine is called when a specialization is declared. If it is
614 illegal to declare a specialization here, an error is reported. */
617 check_specialization_scope ()
619 tree scope
= current_scope ();
623 An explicit specialization shall be declared in the namespace of
624 which the template is a member, or, for member templates, in the
625 namespace of which the enclosing class or enclosing class
626 template is a member. An explicit specialization of a member
627 function, member class or static data member of a class template
628 shall be declared in the namespace of which the class template
630 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
631 cp_error ("explicit specialization in non-namespace scope `%D'",
636 In an explicit specialization declaration for a member of a class
637 template or a member template that appears in namespace scope,
638 the member template and some of its enclosing class templates may
639 remain unspecialized, except that the declaration shall not
640 explicitly specialize a class member template if its enclosing
641 class templates are not explicitly specialized as well. */
642 if (current_template_parms
)
643 cp_error ("enclosing class templates are not explicitly specialized");
646 /* We've just seen template <>. */
649 begin_specialization ()
651 note_template_header (1);
652 check_specialization_scope ();
655 /* Called at then end of processing a declaration preceeded by
659 end_specialization ()
661 reset_specialization ();
664 /* Any template <>'s that we have seen thus far are not referring to a
665 function specialization. */
668 reset_specialization ()
670 processing_specialization
= 0;
671 template_header_count
= 0;
674 /* We've just seen a template header. If SPECIALIZATION is non-zero,
675 it was of the form template <>. */
678 note_template_header (specialization
)
681 processing_specialization
= specialization
;
682 template_header_count
++;
685 /* We're beginning an explicit instantiation. */
688 begin_explicit_instantiation ()
690 ++processing_explicit_instantiation
;
695 end_explicit_instantiation ()
697 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
698 --processing_explicit_instantiation
;
701 /* The TYPE is being declared. If it is a template type, that means it
702 is a partial specialization. Do appropriate error-checking. */
705 maybe_process_partial_specialization (type
)
708 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
710 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
711 && TYPE_SIZE (type
) == NULL_TREE
)
713 if (current_namespace
714 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
716 cp_pedwarn ("specializing `%#T' in different namespace", type
);
717 cp_pedwarn_at (" from definition of `%#D'",
718 CLASSTYPE_TI_TEMPLATE (type
));
720 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
721 if (processing_template_decl
)
722 push_template_decl (TYPE_MAIN_DECL (type
));
724 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
725 cp_error ("specialization of `%T' after instantiation", type
);
727 else if (processing_specialization
)
728 cp_error ("explicit specialization of non-template `%T'", type
);
731 /* Retrieve the specialization (in the sense of [temp.spec] - a
732 specialization is either an instantiation or an explicit
733 specialization) of TMPL for the given template ARGS. If there is
734 no such specialization, return NULL_TREE. The ARGS are a vector of
735 arguments, or a vector of vectors of arguments, in the case of
736 templates with more than one level of parameters. */
739 retrieve_specialization (tmpl
, args
)
745 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
747 /* There should be as many levels of arguments as there are
748 levels of parameters. */
749 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
750 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
753 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
756 if (comp_template_args (TREE_PURPOSE (s
), args
))
757 return TREE_VALUE (s
);
762 /* Like retrieve_speciailization, but for local declarations. FN is
763 the function in which we are looking for an instantiation. */
766 retrieve_local_specialization (tmpl
, fn
)
770 tree s
= purpose_member (fn
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
771 return s
? TREE_VALUE (s
) : NULL_TREE
;
774 /* Returns non-zero iff DECL is a specialization of TMPL. */
777 is_specialization_of (decl
, tmpl
)
783 if (TREE_CODE (decl
) == FUNCTION_DECL
)
787 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
793 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
795 for (t
= TREE_TYPE (decl
);
797 t
= CLASSTYPE_USE_TEMPLATE (t
)
798 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
799 if (same_type_p (TYPE_MAIN_VARIANT (t
),
800 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
807 /* Register the specialization SPEC as a specialization of TMPL with
808 the indicated ARGS. Returns SPEC, or an equivalent prior
809 declaration, if available. */
812 register_specialization (spec
, tmpl
, args
)
819 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
821 if (TREE_CODE (spec
) == FUNCTION_DECL
822 && uses_template_parms (DECL_TI_ARGS (spec
)))
823 /* This is the FUNCTION_DECL for a partial instantiation. Don't
824 register it; we want the corresponding TEMPLATE_DECL instead.
825 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
826 the more obvious `uses_template_parms (spec)' to avoid problems
827 with default function arguments. In particular, given
830 template <class T> void f(T t1, T t = T())
832 the default argument expression is not substituted for in an
833 instantiation unless and until it is actually needed. */
836 /* There should be as many levels of arguments as there are
837 levels of parameters. */
838 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
839 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
842 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
846 tree fn
= TREE_VALUE (s
);
848 /* We can sometimes try to re-register a specialization that we've
849 already got. In particular, regenerate_decl_from_template
850 calls duplicate_decls which will update the specialization
851 list. But, we'll still get called again here anyhow. It's
852 more convenient to simply allow this than to try to prevent it. */
855 else if (comp_template_args (TREE_PURPOSE (s
), args
))
857 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
859 if (DECL_TEMPLATE_INSTANTIATION (fn
))
862 || DECL_EXPLICIT_INSTANTIATION (fn
))
864 cp_error ("specialization of %D after instantiation",
870 /* This situation should occur only if the first
871 specialization is an implicit instantiation,
872 the second is an explicit specialization, and
873 the implicit instantiation has not yet been
874 used. That situation can occur if we have
875 implicitly instantiated a member function and
876 then specialized it later.
878 We can also wind up here if a friend
879 declaration that looked like an instantiation
880 turns out to be a specialization:
882 template <class T> void foo(T);
883 class S { friend void foo<>(int) };
884 template <> void foo(int);
886 We transform the existing DECL in place so that
887 any pointers to it become pointers to the
890 If there was a definition for the template, but
891 not for the specialization, we want this to
892 look as if there is no definition, and vice
894 DECL_INITIAL (fn
) = NULL_TREE
;
895 duplicate_decls (spec
, fn
);
900 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
902 duplicate_decls (spec
, fn
);
909 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
910 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
915 /* Unregister the specialization SPEC as a specialization of TMPL.
916 Returns nonzero if the SPEC was listed as a specialization of
920 unregister_specialization (spec
, tmpl
)
926 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
928 s
= &TREE_CHAIN (*s
))
929 if (TREE_VALUE (*s
) == spec
)
931 *s
= TREE_CHAIN (*s
);
938 /* Like register_specialization, but for local declarations. FN is
939 the function in which we are registering SPEC, an instantiation of
943 register_local_specialization (spec
, tmpl
, fn
)
948 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
949 = perm_tree_cons (fn
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
954 /* Print the list of candidate FNS in an error message. */
957 print_candidates (fns
)
962 const char *str
= "candidates are:";
964 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
968 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
969 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
974 /* Returns the template (one of the functions given by TEMPLATE_ID)
975 which can be specialized to match the indicated DECL with the
976 explicit template args given in TEMPLATE_ID. The DECL may be
977 NULL_TREE if none is available. In that case, the functions in
978 TEMPLATE_ID are non-members.
980 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
981 specialization of a member template.
983 The template args (those explicitly specified and those deduced)
984 are output in a newly created vector *TARGS_OUT.
986 If it is impossible to determine the result, an error message is
987 issued. The error_mark_node is returned to indicate failure. */
990 determine_specialization (template_id
, decl
, targs_out
,
991 need_member_template
)
995 int need_member_template
;
1000 tree candidates
= NULL_TREE
;
1001 tree templates
= NULL_TREE
;
1003 *targs_out
= NULL_TREE
;
1005 if (template_id
== error_mark_node
)
1006 return error_mark_node
;
1008 fns
= TREE_OPERAND (template_id
, 0);
1009 explicit_targs
= TREE_OPERAND (template_id
, 1);
1011 if (fns
== error_mark_node
)
1012 return error_mark_node
;
1014 /* Check for baselinks. */
1015 if (BASELINK_P (fns
))
1016 fns
= TREE_VALUE (fns
);
1018 if (!is_overloaded_fn (fns
))
1020 cp_error ("`%D' is not a function template", fns
);
1021 return error_mark_node
;
1024 for (; fns
; fns
= OVL_NEXT (fns
))
1028 tree fn
= OVL_CURRENT (fns
);
1030 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1031 /* DECL might be a specialization of FN. */
1033 else if (need_member_template
)
1034 /* FN is an ordinary member function, and we need a
1035 specialization of a member template. */
1037 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1038 /* We can get IDENTIFIER_NODEs here in certain erroneous
1041 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1042 /* This is just an ordinary non-member function. Nothing can
1043 be a specialization of that. */
1047 tree decl_arg_types
;
1049 /* This is an ordinary member function. However, since
1050 we're here, we can assume it's enclosing class is a
1051 template class. For example,
1053 template <typename T> struct S { void f(); };
1054 template <> void S<int>::f() {}
1056 Here, S<int>::f is a non-template, but S<int> is a
1057 template class. If FN has the same type as DECL, we
1058 might be in business. */
1059 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1060 TREE_TYPE (TREE_TYPE (fn
))))
1061 /* The return types differ. */
1064 /* Adjust the type of DECL in case FN is a static member. */
1065 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1066 if (DECL_STATIC_FUNCTION_P (fn
)
1067 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1068 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1070 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1073 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1078 /* See whether this function might be a specialization of this
1080 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1083 /* We cannot deduce template arguments that when used to
1084 specialize TMPL will produce DECL. */
1087 /* Save this template, and the arguments deduced. */
1088 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
1091 if (templates
&& TREE_CHAIN (templates
))
1097 It is possible for a specialization with a given function
1098 signature to be instantiated from more than one function
1099 template. In such cases, explicit specification of the
1100 template arguments must be used to uniquely identify the
1101 function template specialization being specialized.
1103 Note that here, there's no suggestion that we're supposed to
1104 determine which of the candidate templates is most
1105 specialized. However, we, also have:
1109 Partial ordering of overloaded function template
1110 declarations is used in the following contexts to select
1111 the function template to which a function template
1112 specialization refers:
1114 -- when an explicit specialization refers to a function
1117 So, we do use the partial ordering rules, at least for now.
1118 This extension can only serve to make illegal programs legal,
1119 so it's safe. And, there is strong anecdotal evidence that
1120 the committee intended the partial ordering rules to apply;
1121 the EDG front-end has that behavior, and John Spicer claims
1122 that the committee simply forgot to delete the wording in
1123 [temp.expl.spec]. */
1124 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1125 if (tmpl
&& tmpl
!= error_mark_node
)
1127 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1128 templates
= scratch_tree_cons (targs
, tmpl
, NULL_TREE
);
1132 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1134 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1136 return error_mark_node
;
1138 else if ((templates
&& TREE_CHAIN (templates
))
1139 || (candidates
&& TREE_CHAIN (candidates
))
1140 || (templates
&& candidates
))
1142 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1144 chainon (candidates
, templates
);
1145 print_candidates (candidates
);
1146 return error_mark_node
;
1149 /* We have one, and exactly one, match. */
1152 /* It was a specialization of an ordinary member function in a
1154 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1155 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1158 /* It was a specialization of a template. */
1159 targs
= DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates
)));
1160 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1162 *targs_out
= copy_node (targs
);
1163 SET_TMPL_ARGS_LEVEL (*targs_out
,
1164 TMPL_ARGS_DEPTH (*targs_out
),
1165 TREE_PURPOSE (templates
));
1168 *targs_out
= TREE_PURPOSE (templates
);
1169 return TREE_VALUE (templates
);
1172 /* Check to see if the function just declared, as indicated in
1173 DECLARATOR, and in DECL, is a specialization of a function
1174 template. We may also discover that the declaration is an explicit
1175 instantiation at this point.
1177 Returns DECL, or an equivalent declaration that should be used
1178 instead if all goes well. Issues an error message if something is
1179 amiss. Returns error_mark_node if the error is not easily
1182 FLAGS is a bitmask consisting of the following flags:
1184 2: The function has a definition.
1185 4: The function is a friend.
1187 The TEMPLATE_COUNT is the number of references to qualifying
1188 template classes that appeared in the name of the function. For
1191 template <class T> struct S { void f(); };
1194 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1195 classes are not counted in the TEMPLATE_COUNT, so that in
1197 template <class T> struct S {};
1198 template <> struct S<int> { void f(); }
1199 template <> void S<int>::f();
1201 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1202 illegal; there should be no template <>.)
1204 If the function is a specialization, it is marked as such via
1205 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1206 is set up correctly, and it is added to the list of specializations
1207 for that template. */
1210 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1216 int have_def
= flags
& 2;
1217 int is_friend
= flags
& 4;
1218 int specialization
= 0;
1219 int explicit_instantiation
= 0;
1220 int member_specialization
= 0;
1222 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1223 tree dname
= DECL_NAME (decl
);
1225 if (processing_specialization
)
1227 /* The last template header was of the form template <>. */
1229 if (template_header_count
> template_count
)
1231 /* There were more template headers than qualifying template
1233 if (template_header_count
- template_count
> 1)
1234 /* There shouldn't be that many template parameter lists.
1235 There can be at most one parameter list for every
1236 qualifying class, plus one for the function itself. */
1237 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1239 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1241 member_specialization
= 1;
1245 else if (template_header_count
== template_count
)
1247 /* The counts are equal. So, this might be a
1248 specialization, but it is not a specialization of a
1249 member template. It might be something like
1251 template <class T> struct S {
1255 void S<int>::f(int i) {} */
1257 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1261 /* This cannot be an explicit specialization. There are not
1262 enough headers for all of the qualifying classes. For
1263 example, we might have:
1266 void S<int>::T<char>::f();
1268 But, we're missing another template <>. */
1269 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1273 else if (processing_explicit_instantiation
)
1275 if (template_header_count
)
1276 cp_error ("template parameter list used in explicit instantiation");
1279 cp_error ("definition provided for explicit instantiation");
1281 explicit_instantiation
= 1;
1283 else if (ctype
!= NULL_TREE
1284 && !TYPE_BEING_DEFINED (ctype
)
1285 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1288 /* This case catches outdated code that looks like this:
1290 template <class T> struct S { void f(); };
1291 void S<int>::f() {} // Missing template <>
1293 We disable this check when the type is being defined to
1294 avoid complaining about default compiler-generated
1295 constructors, destructors, and assignment operators.
1296 Since the type is an instantiation, not a specialization,
1297 these are the only functions that can be defined before
1298 the class is complete. */
1301 template <class T> void S<int>::f() {}
1303 if (template_header_count
)
1305 cp_error ("template parameters specified in specialization");
1311 ("explicit specialization not preceded by `template <>'");
1313 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1315 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1318 /* This could be something like:
1320 template <class T> void f(T);
1321 class S { friend void f<>(int); } */
1325 /* This case handles bogus declarations like template <>
1326 template <class T> void f<int>(); */
1328 cp_error ("template-id `%D' in declaration of primary template",
1334 if (specialization
|| member_specialization
)
1336 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1337 for (; t
; t
= TREE_CHAIN (t
))
1338 if (TREE_PURPOSE (t
))
1341 ("default argument specified in explicit specialization");
1344 if (current_lang_name
== lang_name_c
)
1345 cp_error ("template specialization with C linkage");
1348 if (specialization
|| member_specialization
|| explicit_instantiation
)
1350 tree tmpl
= NULL_TREE
;
1351 tree targs
= NULL_TREE
;
1353 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1354 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1358 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1361 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1366 lookup_template_function (fns
, NULL_TREE
);
1369 if (declarator
== error_mark_node
)
1370 return error_mark_node
;
1372 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1374 if (!explicit_instantiation
)
1375 /* A specialization in class scope. This is illegal,
1376 but the error will already have been flagged by
1377 check_specialization_scope. */
1378 return error_mark_node
;
1381 /* It's not legal to write an explicit instantiation in
1384 class C { template void f(); }
1386 This case is caught by the parser. However, on
1389 template class C { void f(); };
1391 (which is illegal) we can get here. The error will be
1398 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1400 /* A friend declaration. We can't do much, because we don't
1401 know what this resolves to, yet. */
1402 my_friendly_assert (is_friend
!= 0, 0);
1403 my_friendly_assert (!explicit_instantiation
, 0);
1404 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1407 else if (ctype
!= NULL_TREE
1408 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1411 /* Find the list of functions in ctype that have the same
1412 name as the declared function. */
1413 tree name
= TREE_OPERAND (declarator
, 0);
1414 tree fns
= NULL_TREE
;
1417 if (name
== constructor_name (ctype
)
1418 || name
== constructor_name_full (ctype
))
1420 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1422 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1423 : !TYPE_HAS_DESTRUCTOR (ctype
))
1425 /* From [temp.expl.spec]:
1427 If such an explicit specialization for the member
1428 of a class template names an implicitly-declared
1429 special member function (clause _special_), the
1430 program is ill-formed.
1432 Similar language is found in [temp.explicit]. */
1433 cp_error ("specialization of implicitly-declared special member function");
1434 return error_mark_node
;
1437 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1440 if (!IDENTIFIER_TYPENAME_P (name
))
1442 idx
= lookup_fnfields_1 (ctype
, name
);
1444 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1450 /* For a type-conversion operator, we cannot do a
1451 name-based lookup. We might be looking for `operator
1452 int' which will be a specialization of `operator T'.
1453 So, we find *all* the conversion operators, and then
1454 select from them. */
1457 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1459 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1461 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1463 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1464 /* There are no more conversion functions. */
1467 /* Glue all these conversion functions together
1468 with those we already have. */
1469 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1470 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1474 if (fns
== NULL_TREE
)
1476 cp_error ("no member function `%D' declared in `%T'",
1478 return error_mark_node
;
1481 TREE_OPERAND (declarator
, 0) = fns
;
1484 /* Figure out what exactly is being specialized at this point.
1485 Note that for an explicit instantiation, even one for a
1486 member function, we cannot tell apriori whether the
1487 instantiation is for a member template, or just a member
1488 function of a template class. Even if a member template is
1489 being instantiated, the member template arguments may be
1490 elided if they can be deduced from the rest of the
1492 tmpl
= determine_specialization (declarator
, decl
,
1494 member_specialization
);
1496 if (!tmpl
|| tmpl
== error_mark_node
)
1497 /* We couldn't figure out what this declaration was
1499 return error_mark_node
;
1502 tree gen_tmpl
= most_general_template (tmpl
);
1504 if (explicit_instantiation
)
1506 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1507 is done by do_decl_instantiation later. */
1509 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1510 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1512 if (arg_depth
> parm_depth
)
1514 /* If TMPL is not the most general template (for
1515 example, if TMPL is a friend template that is
1516 injected into namespace scope), then there will
1517 be too many levels fo TARGS. Remove some of them
1522 new_targs
= make_temp_vec (parm_depth
);
1523 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1524 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1525 = TREE_VEC_ELT (targs
, i
);
1529 decl
= instantiate_template (tmpl
, targs
);
1533 /* If we though that the DECL was a member function, but it
1534 turns out to be specializing a static member function,
1535 make DECL a static member function as well. */
1536 if (DECL_STATIC_FUNCTION_P (tmpl
)
1537 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1539 revert_static_member_fn (&decl
, 0, 0);
1540 last_function_parms
= TREE_CHAIN (last_function_parms
);
1543 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1544 DECL_TEMPLATE_INFO (decl
)
1545 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1547 /* Mangle the function name appropriately. Note that we do
1548 not mangle specializations of non-template member
1549 functions of template classes, e.g. with
1551 template <class T> struct S { void f(); }
1553 and given the specialization
1555 template <> void S<int>::f() {}
1557 we do not mangle S<int>::f() here. That's because it's
1558 just an ordinary member function and doesn't need special
1559 treatment. We do this here so that the ordinary,
1560 non-template, name-mangling algorith will not be used
1562 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1563 && name_mangling_version
>= 1)
1564 set_mangled_name_for_template_decl (decl
);
1566 if (is_friend
&& !have_def
)
1567 /* This is not really a declaration of a specialization.
1568 It's just the name of an instantiation. But, it's not
1569 a request for an instantiation, either. */
1570 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1572 /* Register this specialization so that we can find it
1574 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1581 /* TYPE is being declared. Verify that the use of template headers
1582 and such is reasonable. Issue error messages if not. */
1585 maybe_check_template_type (type
)
1588 if (template_header_count
)
1590 /* We are in the scope of some `template <...>' header. */
1593 = template_class_depth_real (TYPE_CONTEXT (type
),
1594 /*count_specializations=*/1);
1596 if (template_header_count
<= context_depth
)
1597 /* This is OK; the template headers are for the context. We
1598 are actually too lenient here; like
1599 check_explicit_specialization we should consider the number
1600 of template types included in the actual declaration. For
1603 template <class T> struct S {
1604 template <class U> template <class V>
1610 template <class T> struct S {
1611 template <class U> struct I;
1614 template <class T> template <class U.
1619 else if (template_header_count
> context_depth
+ 1)
1620 /* There are two many template parameter lists. */
1621 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1625 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1626 parameters. These are represented in the same format used for
1627 DECL_TEMPLATE_PARMS. */
1629 int comp_template_parms (parms1
, parms2
)
1636 if (parms1
== parms2
)
1639 for (p1
= parms1
, p2
= parms2
;
1640 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1641 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1643 tree t1
= TREE_VALUE (p1
);
1644 tree t2
= TREE_VALUE (p2
);
1647 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1648 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1650 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1653 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1655 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1656 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1658 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1661 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1663 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1668 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1669 /* One set of parameters has more parameters lists than the
1676 /* Complain if DECL shadows a template parameter.
1678 [temp.local]: A template-parameter shall not be redeclared within its
1679 scope (including nested scopes). */
1682 check_template_shadow (decl
)
1687 /* If we're not in a template, we can't possibly shadow a template
1689 if (!current_template_parms
)
1692 /* Figure out what we're shadowing. */
1693 if (TREE_CODE (decl
) == OVERLOAD
)
1694 decl
= OVL_CURRENT (decl
);
1695 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1697 /* If there's no previous binding for this name, we're not shadowing
1698 anything, let alone a template parameter. */
1702 /* If we're not shadowing a template parameter, we're done. Note
1703 that OLDDECL might be an OVERLOAD (or perhaps even an
1704 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1706 if (TREE_CODE_CLASS (TREE_CODE (olddecl
)) != 'd'
1707 || !DECL_TEMPLATE_PARM_P (olddecl
))
1710 /* We check for decl != olddecl to avoid bogus errors for using a
1711 name inside a class. We check TPFI to avoid duplicate errors for
1712 inline member templates. */
1714 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1717 cp_error_at ("declaration of `%#D'", decl
);
1718 cp_error_at (" shadows template parm `%#D'", olddecl
);
1721 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1722 ORIG_LEVEL, DECL, and TYPE. */
1725 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1732 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1733 TEMPLATE_PARM_IDX (t
) = index
;
1734 TEMPLATE_PARM_LEVEL (t
) = level
;
1735 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1736 TEMPLATE_PARM_DECL (t
) = decl
;
1737 TREE_TYPE (t
) = type
;
1742 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1743 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1744 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1745 new one is created. */
1748 reduce_template_parm_level (index
, type
, levels
)
1753 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1754 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1755 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1758 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1759 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1762 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1763 TEMPLATE_PARM_LEVEL (index
) - levels
,
1764 TEMPLATE_PARM_ORIG_LEVEL (index
),
1766 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1768 /* Template template parameters need this. */
1769 DECL_TEMPLATE_PARMS (decl
)
1770 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1773 return TEMPLATE_PARM_DESCENDANTS (index
);
1776 /* Process information from new template parameter NEXT and append it to the
1777 LIST being built. */
1780 process_template_parm (list
, next
)
1789 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1790 defval
= TREE_PURPOSE (parm
);
1791 parm
= TREE_VALUE (parm
);
1792 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1796 tree p
= TREE_VALUE (tree_last (list
));
1798 if (TREE_CODE (p
) == TYPE_DECL
)
1799 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1800 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1801 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1803 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1811 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1812 /* is a const-param */
1813 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1814 PARM
, 0, NULL_TREE
);
1818 The top-level cv-qualifiers on the template-parameter are
1819 ignored when determining its type. */
1820 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1822 /* A template parameter is not modifiable. */
1823 TREE_READONLY (parm
) = 1;
1824 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1825 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1826 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1828 cp_error ("`%#T' is not a valid type for a template constant parameter",
1830 if (DECL_NAME (parm
) == NULL_TREE
)
1831 error (" a template type parameter must begin with `class' or `typename'");
1832 TREE_TYPE (parm
) = void_type_node
;
1835 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1836 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1837 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1839 if (TREE_PERMANENT (parm
) == 0)
1841 parm
= copy_node (parm
);
1842 TREE_PERMANENT (parm
) = 1;
1844 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1845 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1846 = build_template_parm_index (idx
, processing_template_decl
,
1847 processing_template_decl
,
1848 decl
, TREE_TYPE (parm
));
1853 parm
= TREE_VALUE (parm
);
1855 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1857 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1858 /* This is for distinguishing between real templates and template
1859 template parameters */
1860 TREE_TYPE (parm
) = t
;
1861 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1866 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1867 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1868 decl
= build_decl (TYPE_DECL
, parm
, t
);
1871 TYPE_NAME (t
) = decl
;
1872 TYPE_STUB_DECL (t
) = decl
;
1874 TEMPLATE_TYPE_PARM_INDEX (t
)
1875 = build_template_parm_index (idx
, processing_template_decl
,
1876 processing_template_decl
,
1877 decl
, TREE_TYPE (parm
));
1879 SET_DECL_ARTIFICIAL (decl
);
1880 DECL_TEMPLATE_PARM_P (decl
) = 1;
1882 parm
= build_tree_list (defval
, parm
);
1883 return chainon (list
, parm
);
1886 /* The end of a template parameter list has been reached. Process the
1887 tree list into a parameter vector, converting each parameter into a more
1888 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1892 end_template_parm_list (parms
)
1897 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1899 current_template_parms
1900 = tree_cons (build_int_2 (0, processing_template_decl
),
1901 saved_parmlist
, current_template_parms
);
1903 for (parm
= parms
, nparms
= 0;
1905 parm
= TREE_CHAIN (parm
), nparms
++)
1906 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1908 --processing_template_parmlist
;
1910 return saved_parmlist
;
1913 /* end_template_decl is called after a template declaration is seen. */
1916 end_template_decl ()
1918 reset_specialization ();
1920 if (! processing_template_decl
)
1923 /* This matches the pushlevel in begin_template_parm_list. */
1926 --processing_template_decl
;
1927 current_template_parms
= TREE_CHAIN (current_template_parms
);
1928 (void) get_pending_sizes (); /* Why? */
1931 /* Given a template argument vector containing the template PARMS.
1932 The innermost PARMS are given first. */
1935 current_template_args ()
1938 tree args
= NULL_TREE
;
1939 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1942 /* If there is only one level of template parameters, we do not
1943 create a TREE_VEC of TREE_VECs. Instead, we return a single
1944 TREE_VEC containing the arguments. */
1946 args
= make_tree_vec (length
);
1948 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1950 tree a
= copy_node (TREE_VALUE (header
));
1953 TREE_TYPE (a
) = NULL_TREE
;
1954 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1956 tree t
= TREE_VEC_ELT (a
, i
);
1958 /* T will be a list if we are called from within a
1959 begin/end_template_parm_list pair, but a vector directly
1960 if within a begin/end_member_template_processing pair. */
1961 if (TREE_CODE (t
) == TREE_LIST
)
1965 if (TREE_CODE (t
) == TYPE_DECL
1966 || TREE_CODE (t
) == TEMPLATE_DECL
)
1969 t
= DECL_INITIAL (t
);
1970 TREE_VEC_ELT (a
, i
) = t
;
1975 TREE_VEC_ELT (args
, --l
) = a
;
1983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1984 template PARMS. Used by push_template_decl below. */
1987 build_template_decl (decl
, parms
)
1991 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1992 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1993 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1994 if (DECL_LANG_SPECIFIC (decl
))
1996 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1997 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
1998 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
1999 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2005 struct template_parm_data
2007 /* The level of the template parameters we are currently
2011 /* The index of the specialization argument we are currently
2015 /* An array whose size is the number of template parameters. The
2016 elements are non-zero if the parameter has been used in any one
2017 of the arguments processed so far. */
2020 /* An array whose size is the number of template arguments. The
2021 elements are non-zero if the argument makes use of template
2022 parameters of this level. */
2023 int* arg_uses_template_parms
;
2026 /* Subroutine of push_template_decl used to see if each template
2027 parameter in a partial specialization is used in the explicit
2028 argument list. If T is of the LEVEL given in DATA (which is
2029 treated as a template_parm_data*), then DATA->PARMS is marked
2033 mark_template_parm (t
, data
)
2039 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2041 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2043 level
= TEMPLATE_PARM_LEVEL (t
);
2044 idx
= TEMPLATE_PARM_IDX (t
);
2048 level
= TEMPLATE_TYPE_LEVEL (t
);
2049 idx
= TEMPLATE_TYPE_IDX (t
);
2052 if (level
== tpd
->level
)
2054 tpd
->parms
[idx
] = 1;
2055 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2058 /* Return zero so that for_each_template_parm will continue the
2059 traversal of the tree; we want to mark *every* template parm. */
2063 /* Process the partial specialization DECL. */
2066 process_partial_specialization (decl
)
2069 tree type
= TREE_TYPE (decl
);
2070 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2071 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2072 tree inner_args
= innermost_args (specargs
);
2073 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2074 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2075 int nargs
= TREE_VEC_LENGTH (inner_args
);
2076 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2078 int did_error_intro
= 0;
2079 struct template_parm_data tpd
;
2080 struct template_parm_data tpd2
;
2082 /* We check that each of the template parameters given in the
2083 partial specialization is used in the argument list to the
2084 specialization. For example:
2086 template <class T> struct S;
2087 template <class T> struct S<T*>;
2089 The second declaration is OK because `T*' uses the template
2090 parameter T, whereas
2092 template <class T> struct S<int>;
2094 is no good. Even trickier is:
2105 The S2<T> declaration is actually illegal; it is a
2106 full-specialization. Of course,
2109 struct S2<T (*)(U)>;
2111 or some such would have been OK. */
2112 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2113 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2114 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
2116 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2117 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
2118 for (i
= 0; i
< nargs
; ++i
)
2120 tpd
.current_arg
= i
;
2121 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2122 &mark_template_parm
,
2125 for (i
= 0; i
< ntparms
; ++i
)
2126 if (tpd
.parms
[i
] == 0)
2128 /* One of the template parms was not used in the
2130 if (!did_error_intro
)
2132 cp_error ("template parameters not used in partial specialization:");
2133 did_error_intro
= 1;
2137 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2140 /* [temp.class.spec]
2142 The argument list of the specialization shall not be identical to
2143 the implicit argument list of the primary template. */
2144 if (comp_template_args (inner_args
,
2145 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2147 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2149 /* [temp.class.spec]
2151 A partially specialized non-type argument expression shall not
2152 involve template parameters of the partial specialization except
2153 when the argument expression is a simple identifier.
2155 The type of a template parameter corresponding to a specialized
2156 non-type argument shall not be dependent on a parameter of the
2158 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2160 for (i
= 0; i
< nargs
; ++i
)
2162 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2163 if (/* These first two lines are the `non-type' bit. */
2164 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2165 && TREE_CODE (arg
) != TEMPLATE_DECL
2166 /* This next line is the `argument expression is not just a
2167 simple identifier' condition and also the `specialized
2168 non-type argument' bit. */
2169 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2171 if (tpd
.arg_uses_template_parms
[i
])
2172 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2175 /* Look at the corresponding template parameter,
2176 marking which template parameters its type depends
2179 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2184 /* We haven't yet initialized TPD2. Do so now. */
2185 tpd2
.arg_uses_template_parms
2186 = (int*) alloca (sizeof (int) * nargs
);
2187 /* The number of parameters here is the number in the
2188 main template, which, as checked in the assertion
2190 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2192 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2195 /* Mark the template parameters. But this time, we're
2196 looking for the template parameters of the main
2197 template, not in the specialization. */
2198 tpd2
.current_arg
= i
;
2199 tpd2
.arg_uses_template_parms
[i
] = 0;
2200 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
2201 for_each_template_parm (type
,
2202 &mark_template_parm
,
2205 if (tpd2
.arg_uses_template_parms
[i
])
2207 /* The type depended on some template parameters.
2208 If they are fully specialized in the
2209 specialization, that's OK. */
2211 for (j
= 0; j
< nargs
; ++j
)
2212 if (tpd2
.parms
[j
] != 0
2213 && tpd
.arg_uses_template_parms
[j
])
2215 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2225 if (retrieve_specialization (maintmpl
, specargs
))
2226 /* We've already got this specialization. */
2229 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2230 = perm_tree_cons (inner_args
, inner_parms
,
2231 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2232 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2236 /* Check that a template declaration's use of default arguments is not
2237 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2238 non-zero if DECL is the thing declared by a primary template.
2239 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2242 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2249 int last_level_to_check
;
2254 A default template-argument shall not be specified in a
2255 function template declaration or a function template definition, nor
2256 in the template-parameter-list of the definition of a member of a
2259 if (TREE_CODE (DECL_REAL_CONTEXT (decl
)) == FUNCTION_DECL
)
2260 /* You can't have a function template declaration in a local
2261 scope, nor you can you define a member of a class template in a
2265 if (current_class_type
2266 && !TYPE_BEING_DEFINED (current_class_type
)
2267 && DECL_LANG_SPECIFIC (decl
)
2268 /* If this is either a friend defined in the scope of the class
2269 or a member function. */
2270 && DECL_CLASS_CONTEXT (decl
) == current_class_type
2271 /* And, if it was a member function, it really was defined in
2272 the scope of the class. */
2273 && (!DECL_FUNCTION_MEMBER_P (decl
) || DECL_DEFINED_IN_CLASS_P (decl
)))
2274 /* We already checked these parameters when the template was
2275 declared, so there's no need to do it again now. This function
2276 was defined in class scope, but we're processing it's body now
2277 that the class is complete. */
2282 If a template-parameter has a default template-argument, all
2283 subsequent template-parameters shall have a default
2284 template-argument supplied. */
2285 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2287 tree inner_parms
= TREE_VALUE (parm_level
);
2288 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2289 int seen_def_arg_p
= 0;
2292 for (i
= 0; i
< ntparms
; ++i
)
2294 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2295 if (TREE_PURPOSE (parm
))
2297 else if (seen_def_arg_p
)
2299 cp_error ("no default argument for `%D'", TREE_VALUE (parm
));
2300 /* For better subsequent error-recovery, we indicate that
2301 there should have been a default argument. */
2302 TREE_PURPOSE (parm
) = error_mark_node
;
2307 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2308 /* For an ordinary class template, default template arguments are
2309 allowed at the innermost level, e.g.:
2310 template <class T = int>
2312 but, in a partial specialization, they're not allowed even
2313 there, as we have in [temp.class.spec]:
2315 The template parameter list of a specialization shall not
2316 contain default template argument values.
2318 So, for a partial specialization, or for a function template,
2319 we look at all of them. */
2322 /* But, for a primary class template that is not a partial
2323 specialization we look at all template parameters except the
2325 parms
= TREE_CHAIN (parms
);
2327 /* Figure out what error message to issue. */
2328 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2329 msg
= "default argument for template parameter in function template `%D'";
2330 else if (is_partial
)
2331 msg
= "default argument in partial specialization `%D'";
2333 msg
= "default argument for template parameter for class enclosing `%D'";
2335 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2336 /* If we're inside a class definition, there's no need to
2337 examine the parameters to the class itself. On the one
2338 hand, they will be checked when the class is defined, and,
2339 on the other, default arguments are legal in things like:
2340 template <class T = double>
2341 struct S { template <class U> void f(U); };
2342 Here the default argument for `S' has no bearing on the
2343 declaration of `f'. */
2344 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2346 /* Check everything. */
2347 last_level_to_check
= 0;
2349 for (parm_level
= parms
;
2350 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2351 parm_level
= TREE_CHAIN (parm_level
))
2353 tree inner_parms
= TREE_VALUE (parm_level
);
2357 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2358 for (i
= 0; i
< ntparms
; ++i
)
2359 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2363 cp_error (msg
, decl
);
2367 /* Clear out the default argument so that we are not
2369 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2372 /* At this point, if we're still interested in issuing messages,
2373 they must apply to classes surrounding the object declared. */
2375 msg
= "default argument for template parameter for class enclosing `%D'";
2379 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2380 parameters given by current_template_args, or reuses a
2381 previously existing one, if appropriate. Returns the DECL, or an
2382 equivalent one, if it is replaced via a call to duplicate_decls.
2384 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2387 push_template_decl_real (decl
, is_friend
)
2397 int new_template_p
= 0;
2399 /* See if this is a partial specialization. */
2400 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2401 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2402 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2404 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2407 /* For a friend, we want the context of the friend function, not
2408 the type of which it is a friend. */
2409 ctx
= DECL_CONTEXT (decl
);
2410 else if (DECL_REAL_CONTEXT (decl
)
2411 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2412 /* In the case of a virtual function, we want the class in which
2414 ctx
= DECL_REAL_CONTEXT (decl
);
2416 /* Otherwise, if we're currently definining some class, the DECL
2417 is assumed to be a member of the class. */
2418 ctx
= current_scope ();
2420 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2423 if (!DECL_CONTEXT (decl
))
2424 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2426 /* See if this is a primary template. */
2427 primary
= pseudo_global_level_p ();
2431 if (current_lang_name
== lang_name_c
)
2432 cp_error ("template with C linkage");
2433 else if (TREE_CODE (decl
) == TYPE_DECL
2434 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2435 cp_error ("template class without a name");
2436 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2437 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2438 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2439 || TREE_CODE (decl
) == FUNCTION_DECL
)
2442 cp_error ("template declaration of `%#D'", decl
);
2445 /* Check to see that the rules regarding the use of default
2446 arguments are not being violated. */
2447 check_default_tmpl_args (decl
, current_template_parms
,
2448 primary
, is_partial
);
2451 return process_partial_specialization (decl
);
2453 args
= current_template_args ();
2456 || TREE_CODE (ctx
) == FUNCTION_DECL
2457 || TYPE_BEING_DEFINED (ctx
)
2458 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2460 if (DECL_LANG_SPECIFIC (decl
)
2461 && DECL_TEMPLATE_INFO (decl
)
2462 && DECL_TI_TEMPLATE (decl
))
2463 tmpl
= DECL_TI_TEMPLATE (decl
);
2464 /* If DECL is a TYPE_DECL for a class-template, then there won't
2465 be DECL_LANG_SPECIFIC. The information equivalent to
2466 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2467 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2468 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2469 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2471 /* Since a template declaration already existed for this
2472 class-type, we must be redeclaring it here. Make sure
2473 that the redeclaration is legal. */
2474 redeclare_class_template (TREE_TYPE (decl
),
2475 current_template_parms
);
2476 /* We don't need to create a new TEMPLATE_DECL; just use the
2477 one we already had. */
2478 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2482 tmpl
= build_template_decl (decl
, current_template_parms
);
2485 if (DECL_LANG_SPECIFIC (decl
)
2486 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2488 /* A specialization of a member template of a template
2490 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2491 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2492 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2498 tree a
, t
, current
, parms
;
2501 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2502 cp_error ("must specialize `%#T' before defining member `%#D'",
2504 if (TREE_CODE (decl
) == TYPE_DECL
)
2506 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2507 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2508 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2509 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2510 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2513 cp_error ("`%D' does not declare a template type", decl
);
2517 else if (! DECL_TEMPLATE_INFO (decl
))
2519 cp_error ("template definition of non-template `%#D'", decl
);
2523 tmpl
= DECL_TI_TEMPLATE (decl
);
2525 if (is_member_template (tmpl
)
2526 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2527 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2528 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2532 /* The declaration is a specialization of a member
2533 template, declared outside the class. Therefore, the
2534 innermost template arguments will be NULL, so we
2535 replace them with the arguments determined by the
2536 earlier call to check_explicit_specialization. */
2537 args
= DECL_TI_ARGS (decl
);
2540 = build_template_decl (decl
, current_template_parms
);
2541 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2542 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2543 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2544 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2545 DECL_TEMPLATE_INFO (new_tmpl
) =
2546 perm_tree_cons (tmpl
, args
, NULL_TREE
);
2548 register_specialization (new_tmpl
, tmpl
, args
);
2552 /* Make sure the template headers we got make sense. */
2554 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2555 i
= TMPL_PARMS_DEPTH (parms
);
2556 if (TMPL_ARGS_DEPTH (args
) != i
)
2558 cp_error ("expected %d levels of template parms for `%#D', got %d",
2559 i
, decl
, TMPL_ARGS_DEPTH (args
));
2562 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2564 a
= TMPL_ARGS_LEVEL (args
, i
);
2565 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2567 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2569 if (current
== decl
)
2570 cp_error ("got %d template parameters for `%#D'",
2571 TREE_VEC_LENGTH (a
), decl
);
2573 cp_error ("got %d template parameters for `%#T'",
2574 TREE_VEC_LENGTH (a
), current
);
2575 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2578 /* Perhaps we should also check that the parms are used in the
2579 appropriate qualifying scopes in the declarator? */
2581 if (current
== decl
)
2584 current
= TYPE_CONTEXT (current
);
2588 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2589 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2591 /* Push template declarations for global functions and types. Note
2592 that we do not try to push a global template friend declared in a
2593 template class; such a thing may well depend on the template
2594 parameters of the class. */
2595 if (new_template_p
&& !ctx
2596 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2597 tmpl
= pushdecl_namespace_level (tmpl
);
2600 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2602 info
= perm_tree_cons (tmpl
, copy_to_permanent (args
), NULL_TREE
);
2604 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2606 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2607 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2608 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2609 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2611 else if (DECL_LANG_SPECIFIC (decl
))
2612 DECL_TEMPLATE_INFO (decl
) = info
;
2614 return DECL_TEMPLATE_RESULT (tmpl
);
2618 push_template_decl (decl
)
2621 return push_template_decl_real (decl
, 0);
2624 /* Called when a class template TYPE is redeclared with the indicated
2625 template PARMS, e.g.:
2627 template <class T> struct S;
2628 template <class T> struct S {}; */
2631 redeclare_class_template (type
, parms
)
2639 if (!TYPE_TEMPLATE_INFO (type
))
2641 cp_error ("`%T' is not a template type", type
);
2645 tmpl
= TYPE_TI_TEMPLATE (type
);
2646 if (!PRIMARY_TEMPLATE_P (tmpl
))
2647 /* The type is nested in some template class. Nothing to worry
2648 about here; there are no new template parameters for the nested
2652 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2653 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2655 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2657 cp_error_at ("previous declaration `%D'", tmpl
);
2658 cp_error ("used %d template parameter%s instead of %d",
2659 TREE_VEC_LENGTH (tmpl_parms
),
2660 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2661 TREE_VEC_LENGTH (parms
));
2665 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2667 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2668 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2669 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2670 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2672 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2674 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2675 cp_error ("redeclared here as `%#D'", parm
);
2679 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2681 /* We have in [temp.param]:
2683 A template-parameter may not be given default arguments
2684 by two different declarations in the same scope. */
2685 cp_error ("redefinition of default argument for `%#D'", parm
);
2686 cp_error_at (" original definition appeared here", tmpl_parm
);
2690 if (parm_default
!= NULL_TREE
)
2691 /* Update the previous template parameters (which are the ones
2692 that will really count) with the new default value. */
2693 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2694 else if (tmpl_default
!= NULL_TREE
)
2695 /* Update the new parameters, too; they'll be used as the
2696 parameters for any members. */
2697 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
2701 /* Attempt to convert the non-type template parameter EXPR to the
2702 indicated TYPE. If the conversion is successful, return the
2703 converted value. If the conversion is unsuccesful, return
2704 NULL_TREE if we issued an error message, or error_mark_node if we
2705 did not. We issue error messages for out-and-out bad template
2706 parameters, but not simply because the conversion failed, since we
2707 might be just trying to do argument deduction. By the time this
2708 function is called, neither TYPE nor EXPR may make use of template
2712 convert_nontype_argument (type
, expr
)
2716 tree expr_type
= TREE_TYPE (expr
);
2718 /* A template-argument for a non-type, non-template
2719 template-parameter shall be one of:
2721 --an integral constant-expression of integral or enumeration
2724 --the name of a non-type template-parameter; or
2726 --the name of an object or function with external linkage,
2727 including function templates and function template-ids but
2728 excluding non-static class members, expressed as id-expression;
2731 --the address of an object or function with external linkage,
2732 including function templates and function template-ids but
2733 excluding non-static class members, expressed as & id-expression
2734 where the & is optional if the name refers to a function or
2737 --a pointer to member expressed as described in _expr.unary.op_. */
2739 /* An integral constant-expression can include const variables or
2740 enumerators. Simplify things by folding them to their values,
2741 unless we're about to bind the declaration to a reference
2743 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
)
2744 && TREE_CODE (type
) != REFERENCE_TYPE
)
2745 expr
= decl_constant_value (expr
);
2747 if (is_overloaded_fn (expr
))
2748 /* OK for now. We'll check that it has external linkage later.
2749 Check this first since if expr_type is the unknown_type_node
2750 we would otherwise complain below. */
2752 else if (TYPE_PTRMEM_P (expr_type
)
2753 || TYPE_PTRMEMFUNC_P (expr_type
))
2755 if (TREE_CODE (expr
) != PTRMEM_CST
)
2758 else if (TYPE_PTR_P (expr_type
)
2759 || TYPE_PTRMEM_P (expr_type
)
2760 || TREE_CODE (expr_type
) == ARRAY_TYPE
2761 || TREE_CODE (type
) == REFERENCE_TYPE
2762 /* If expr is the address of an overloaded function, we
2763 will get the unknown_type_node at this point. */
2764 || expr_type
== unknown_type_node
)
2770 if (TREE_CODE (type
) == REFERENCE_TYPE
2771 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2775 if (TREE_CODE (e
) != ADDR_EXPR
)
2778 cp_error ("`%E' is not a valid template argument", expr
);
2779 if (TYPE_PTR_P (expr_type
))
2781 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2782 cp_error ("it must be the address of a function with external linkage");
2784 cp_error ("it must be the address of an object with external linkage");
2786 else if (TYPE_PTRMEM_P (expr_type
)
2787 || TYPE_PTRMEMFUNC_P (expr_type
))
2788 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2793 referent
= TREE_OPERAND (e
, 0);
2794 STRIP_NOPS (referent
);
2797 if (TREE_CODE (referent
) == STRING_CST
)
2799 cp_error ("string literal %E is not a valid template argument",
2801 error ("because it is the address of an object with static linkage");
2805 if (is_overloaded_fn (referent
))
2806 /* We'll check that it has external linkage later. */
2808 else if (TREE_CODE (referent
) != VAR_DECL
)
2810 else if (!TREE_PUBLIC (referent
))
2812 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2813 return error_mark_node
;
2816 else if (INTEGRAL_TYPE_P (expr_type
)
2817 || TYPE_PTRMEM_P (expr_type
)
2818 || TYPE_PTRMEMFUNC_P (expr_type
)
2819 /* The next two are g++ extensions. */
2820 || TREE_CODE (expr_type
) == REAL_TYPE
2821 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2823 if (! TREE_CONSTANT (expr
))
2826 cp_error ("non-constant `%E' cannot be used as template argument",
2833 cp_error ("object `%E' cannot be used as template argument", expr
);
2837 switch (TREE_CODE (type
))
2842 /* For a non-type template-parameter of integral or enumeration
2843 type, integral promotions (_conv.prom_) and integral
2844 conversions (_conv.integral_) are applied. */
2845 if (!INTEGRAL_TYPE_P (expr_type
))
2846 return error_mark_node
;
2848 /* It's safe to call digest_init in this case; we know we're
2849 just converting one integral constant expression to another. */
2850 expr
= digest_init (type
, expr
, (tree
*) 0);
2852 if (TREE_CODE (expr
) != INTEGER_CST
)
2853 /* Curiously, some TREE_CONSTANT integral expressions do not
2854 simplify to integer constants. For example, `3 % 0',
2855 remains a TRUNC_MOD_EXPR. */
2862 /* These are g++ extensions. */
2863 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2864 return error_mark_node
;
2866 expr
= digest_init (type
, expr
, (tree
*) 0);
2868 if (TREE_CODE (expr
) != REAL_CST
)
2875 tree type_pointed_to
= TREE_TYPE (type
);
2877 if (TYPE_PTRMEM_P (type
))
2881 /* For a non-type template-parameter of type pointer to data
2882 member, qualification conversions (_conv.qual_) are
2884 e
= perform_qualification_conversions (type
, expr
);
2885 if (TREE_CODE (e
) == NOP_EXPR
)
2886 /* The call to perform_qualification_conversions will
2887 insert a NOP_EXPR over EXPR to do express conversion,
2888 if necessary. But, that will confuse us if we use
2889 this (converted) template parameter to instantiate
2890 another template; then the thing will not look like a
2891 valid template argument. So, just make a new
2892 constant, of the appropriate type. */
2893 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
2896 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2898 /* For a non-type template-parameter of type pointer to
2899 function, only the function-to-pointer conversion
2900 (_conv.func_) is applied. If the template-argument
2901 represents a set of overloaded functions (or a pointer to
2902 such), the matching function is selected from the set
2907 if (TREE_CODE (expr
) == ADDR_EXPR
)
2908 fns
= TREE_OPERAND (expr
, 0);
2912 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2914 if (fn
== error_mark_node
)
2915 return error_mark_node
;
2917 if (!TREE_PUBLIC (fn
))
2919 if (really_overloaded_fn (fns
))
2920 return error_mark_node
;
2925 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2927 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2933 /* For a non-type template-parameter of type pointer to
2934 object, qualification conversions (_conv.qual_) and the
2935 array-to-pointer conversion (_conv.array_) are applied.
2936 [Note: In particular, neither the null pointer conversion
2937 (_conv.ptr_) nor the derived-to-base conversion
2938 (_conv.ptr_) are applied. Although 0 is a valid
2939 template-argument for a non-type template-parameter of
2940 integral type, it is not a valid template-argument for a
2941 non-type template-parameter of pointer type.]
2943 The call to decay_conversion performs the
2944 array-to-pointer conversion, if appropriate. */
2945 expr
= decay_conversion (expr
);
2947 if (expr
== error_mark_node
)
2948 return error_mark_node
;
2950 return perform_qualification_conversions (type
, expr
);
2955 case REFERENCE_TYPE
:
2957 tree type_referred_to
= TREE_TYPE (type
);
2959 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2961 /* For a non-type template-parameter of type reference to
2962 function, no conversions apply. If the
2963 template-argument represents a set of overloaded
2964 functions, the matching function is selected from the
2965 set (_over.over_). */
2969 fn
= instantiate_type (type_referred_to
, fns
, 0);
2971 if (fn
== error_mark_node
)
2972 return error_mark_node
;
2974 if (!TREE_PUBLIC (fn
))
2976 if (really_overloaded_fn (fns
))
2977 /* Don't issue an error here; we might get a different
2978 function if the overloading had worked out
2980 return error_mark_node
;
2985 my_friendly_assert (same_type_p (type_referred_to
,
2993 /* For a non-type template-parameter of type reference to
2994 object, no conversions apply. The type referred to by the
2995 reference may be more cv-qualified than the (otherwise
2996 identical) type of the template-argument. The
2997 template-parameter is bound directly to the
2998 template-argument, which must be an lvalue. */
2999 if ((TYPE_MAIN_VARIANT (expr_type
)
3000 != TYPE_MAIN_VARIANT (type_referred_to
))
3001 || !at_least_as_qualified_p (type_referred_to
,
3003 || !real_lvalue_p (expr
))
3004 return error_mark_node
;
3013 if (!TYPE_PTRMEMFUNC_P (type
))
3014 /* This handles templates like
3015 template<class T, T t> void f();
3016 when T is substituted with any class. The second template
3017 parameter becomes invalid and the template candidate is
3019 return error_mark_node
;
3021 /* For a non-type template-parameter of type pointer to member
3022 function, no conversions apply. If the template-argument
3023 represents a set of overloaded member functions, the
3024 matching member function is selected from the set
3027 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3028 expr_type
!= unknown_type_node
)
3029 return error_mark_node
;
3031 if (TREE_CODE (expr
) == PTRMEM_CST
)
3033 /* A ptr-to-member constant. */
3034 if (!same_type_p (type
, expr_type
))
3035 return error_mark_node
;
3040 if (TREE_CODE (expr
) != ADDR_EXPR
)
3041 return error_mark_node
;
3043 expr
= instantiate_type (type
, expr
, 0);
3045 if (expr
== error_mark_node
)
3046 return error_mark_node
;
3048 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3055 /* All non-type parameters must have one of these types. */
3056 my_friendly_abort (0);
3060 return error_mark_node
;
3063 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3064 template template parameters. Both PARM_PARMS and ARG_PARMS are
3065 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3068 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3069 the case, then extra parameters must have default arguments.
3071 Consider the example:
3072 template <class T, class Allocator = allocator> class vector;
3073 template<template <class U> class TT> class C;
3075 C<vector> is a valid instantiation. PARM_PARMS for the above code
3076 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3077 T and Allocator) and OUTER_ARGS contains the argument that is used to
3078 substitute the TT parameter. */
3081 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
3082 in_decl
, outer_args
)
3083 tree parm_parms
, arg_parms
;
3085 tree in_decl
, outer_args
;
3087 int nparms
, nargs
, i
;
3090 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3091 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3093 nparms
= TREE_VEC_LENGTH (parm_parms
);
3094 nargs
= TREE_VEC_LENGTH (arg_parms
);
3096 /* The rule here is opposite of coerce_template_parms. */
3099 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3102 for (i
= 0; i
< nparms
; ++i
)
3104 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3105 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3107 if (arg
== NULL_TREE
|| arg
== error_mark_node
3108 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3111 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3114 switch (TREE_CODE (parm
))
3120 /* We encounter instantiations of templates like
3121 template <template <template <class> class> class TT>
3124 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3125 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3127 if (!coerce_template_template_parms (parmparm
, argparm
,
3135 /* The tsubst call is used to handle cases such as
3136 template <class T, template <T> class TT> class D;
3137 i.e. the parameter list of TT depends on earlier parameters. */
3138 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3145 my_friendly_abort (0);
3151 /* Convert the indicated template ARG as necessary to match the
3152 indicated template PARM. Returns the converted ARG, or
3153 error_mark_node if the conversion was unsuccessful. Error messages
3154 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3155 parameter in the parameter list. ARGS is the full set of template
3156 arguments deduced so far. */
3159 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3169 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3171 inner_args
= innermost_args (args
);
3173 if (TREE_CODE (arg
) == TREE_LIST
3174 && TREE_TYPE (arg
) != NULL_TREE
3175 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3177 /* The template argument was the name of some
3178 member function. That's usually
3179 illegal, but static members are OK. In any
3180 case, grab the underlying fields/functions
3181 and issue an error later if required. */
3182 arg
= TREE_VALUE (arg
);
3183 TREE_TYPE (arg
) = unknown_type_node
;
3186 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3187 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3188 || requires_tmpl_type
);
3190 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3191 we also accept implicitly created TYPE_DECL as a valid argument.
3192 This is necessary to handle the case where we pass a template name
3193 to a template template parameter in a scope where we've derived from
3194 in instantiation of that template, so the template name refers to that
3195 instantiation. We really ought to handle this better. */
3197 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3198 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3199 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3200 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
3201 || (TREE_CODE (arg
) == RECORD_TYPE
3202 && CLASSTYPE_TEMPLATE_INFO (arg
)
3203 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3204 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3205 && requires_tmpl_type
3206 && is_base_of_enclosing_class (arg
, current_class_type
)));
3207 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3208 arg
= TYPE_STUB_DECL (arg
);
3209 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3210 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3212 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
3214 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3215 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3217 cp_pedwarn ("to refer to a type member of a template parameter,");
3218 cp_pedwarn (" use `typename %E'", arg
);
3220 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3221 TREE_OPERAND (arg
, 1));
3224 if (is_type
!= requires_type
)
3230 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3233 cp_error (" expected a constant of type `%T', got `%T'",
3235 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3237 cp_error (" expected a type, got `%E'", arg
);
3240 return error_mark_node
;
3242 if (is_tmpl_type
^ requires_tmpl_type
)
3244 if (in_decl
&& complain
)
3246 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3249 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3251 cp_error (" expected a class template, got `%T'", arg
);
3253 return error_mark_node
;
3258 if (requires_tmpl_type
)
3260 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3261 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3263 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3264 in_decl
, inner_args
))
3268 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3270 if (val
!= error_mark_node
3271 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3272 val
= TREE_TYPE (val
);
3276 if (in_decl
&& complain
)
3278 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3280 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3283 val
= error_mark_node
;
3288 val
= groktypename (arg
);
3289 if (! processing_template_decl
)
3291 /* [basic.link]: A name with no linkage (notably, the
3292 name of a class or enumeration declared in a local
3293 scope) shall not be used to declare an entity with
3294 linkage. This implies that names with no linkage
3295 cannot be used as template arguments. */
3296 tree t
= no_linkage_check (val
);
3299 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3301 ("template-argument `%T' uses anonymous type", val
);
3304 ("template-argument `%T' uses local type `%T'",
3306 return error_mark_node
;
3313 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3315 if (processing_template_decl
)
3316 arg
= maybe_fold_nontype_arg (arg
);
3318 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3319 /* We used to call digest_init here. However, digest_init
3320 will report errors, which we don't want when complain
3321 is zero. More importantly, digest_init will try too
3322 hard to convert things: for example, `0' should not be
3323 converted to pointer type at this point according to
3324 the standard. Accepting this is not merely an
3325 extension, since deciding whether or not these
3326 conversions can occur is part of determining which
3327 function template to call, or whether a given epxlicit
3328 argument specification is legal. */
3329 val
= convert_nontype_argument (t
, arg
);
3333 if (val
== NULL_TREE
)
3334 val
= error_mark_node
;
3335 else if (val
== error_mark_node
&& complain
)
3336 cp_error ("could not convert template argument `%E' to `%T'",
3343 /* Convert all template arguments to their appropriate types, and
3344 return a vector containing the innermost resulting template
3345 arguments. If any error occurs, return error_mark_node, and, if
3346 COMPLAIN is non-zero, issue an error message. Some error messages
3347 are issued even if COMPLAIN is zero; for instance, if a template
3348 argument is composed from a local class.
3350 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3351 provided in ARGLIST, or else trailing parameters must have default
3352 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3353 deduction for any unspecified trailing arguments.
3355 The resulting TREE_VEC is allocated on a temporary obstack, and
3356 must be explicitly copied if it will be permanent. */
3359 coerce_template_parms (parms
, args
, in_decl
,
3361 require_all_arguments
)
3365 int require_all_arguments
;
3367 int nparms
, nargs
, i
, lost
= 0;
3370 tree new_inner_args
;
3372 inner_args
= innermost_args (args
);
3373 nargs
= NUM_TMPL_ARGS (inner_args
);
3374 nparms
= TREE_VEC_LENGTH (parms
);
3378 && require_all_arguments
3379 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3383 cp_error ("wrong number of template arguments (%d, should be %d)",
3387 cp_error_at ("provided for `%D'", in_decl
);
3390 return error_mark_node
;
3393 new_inner_args
= make_temp_vec (nparms
);
3394 new_args
= add_outermost_template_args (args
, new_inner_args
);
3395 for (i
= 0; i
< nparms
; i
++)
3400 /* Get the Ith template parameter. */
3401 parm
= TREE_VEC_ELT (parms
, i
);
3403 /* Calculate the Ith argument. */
3404 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3406 arg
= TREE_VALUE (inner_args
);
3407 inner_args
= TREE_CHAIN (inner_args
);
3410 arg
= TREE_VEC_ELT (inner_args
, i
);
3411 /* If no template argument was supplied, look for a default
3413 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3415 /* There was no default value. */
3416 my_friendly_assert (!require_all_arguments
, 0);
3419 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3420 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3422 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3425 /* Now, convert the Ith argument, as necessary. */
3426 if (arg
== NULL_TREE
)
3427 /* We're out of arguments. */
3429 my_friendly_assert (!require_all_arguments
, 0);
3432 else if (arg
== error_mark_node
)
3434 cp_error ("template argument %d is invalid", i
+ 1);
3435 arg
= error_mark_node
;
3438 arg
= convert_template_argument (TREE_VALUE (parm
),
3439 arg
, new_args
, complain
, i
,
3442 if (arg
== error_mark_node
)
3444 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3448 return error_mark_node
;
3450 return new_inner_args
;
3453 /* Returns 1 if template args OT and NT are equivalent. */
3456 template_args_equal (ot
, nt
)
3461 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3463 if (TREE_CODE (nt
) == TREE_VEC
)
3464 /* For member templates */
3465 return comp_template_args (ot
, nt
);
3466 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3467 return same_type_p (ot
, nt
);
3469 return (cp_tree_equal (ot
, nt
) > 0);
3472 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3473 of template arguments. Returns 0 otherwise. */
3476 comp_template_args (oldargs
, newargs
)
3477 tree oldargs
, newargs
;
3481 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3484 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3486 tree nt
= TREE_VEC_ELT (newargs
, i
);
3487 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3489 if (! template_args_equal (ot
, nt
))
3495 /* Given class template name and parameter list, produce a user-friendly name
3496 for the instantiation. */
3499 mangle_class_name_for_template (name
, parms
, arglist
)
3501 tree parms
, arglist
;
3503 static struct obstack scratch_obstack
;
3504 static char *scratch_firstobj
;
3507 if (!scratch_firstobj
)
3508 gcc_obstack_init (&scratch_obstack
);
3510 obstack_free (&scratch_obstack
, scratch_firstobj
);
3511 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3513 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3514 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3518 nparms
= TREE_VEC_LENGTH (parms
);
3519 arglist
= innermost_args (arglist
);
3520 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3521 for (i
= 0; i
< nparms
; i
++)
3523 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3524 tree arg
= TREE_VEC_ELT (arglist
, i
);
3529 if (TREE_CODE (parm
) == TYPE_DECL
)
3531 cat (type_as_string_real (arg
, 0, 1));
3534 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3536 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3538 /* Already substituted with real template. Just output
3539 the template name here */
3540 tree context
= DECL_CONTEXT (arg
);
3543 /* The template may be defined in a namespace, or
3544 may be a member template. */
3545 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3546 || CLASS_TYPE_P (context
),
3548 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3551 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3554 /* Output the parameter declaration */
3555 cat (type_as_string_real (arg
, 0, 1));
3559 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3561 if (TREE_CODE (arg
) == TREE_LIST
)
3563 /* New list cell was built because old chain link was in
3565 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3566 arg
= TREE_VALUE (arg
);
3568 /* No need to check arglist against parmlist here; we did that
3569 in coerce_template_parms, called from lookup_template_class. */
3570 cat (expr_as_string (arg
, 0));
3573 char *bufp
= obstack_next_free (&scratch_obstack
);
3575 while (bufp
[offset
- 1] == ' ')
3577 obstack_blank_fast (&scratch_obstack
, offset
);
3579 /* B<C<char> >, not B<C<char>> */
3580 if (bufp
[offset
- 1] == '>')
3585 return (char *) obstack_base (&scratch_obstack
);
3589 classtype_mangled_name (t
)
3592 if (CLASSTYPE_TEMPLATE_INFO (t
)
3593 /* Specializations have already had their names set up in
3594 lookup_template_class. */
3595 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3597 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3599 /* For non-primary templates, the template parameters are
3600 implicit from their surrounding context. */
3601 if (PRIMARY_TEMPLATE_P (tmpl
))
3603 tree name
= DECL_NAME (tmpl
);
3604 char *mangled_name
= mangle_class_name_for_template
3605 (IDENTIFIER_POINTER (name
),
3606 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3607 CLASSTYPE_TI_ARGS (t
));
3608 tree id
= get_identifier (mangled_name
);
3609 IDENTIFIER_TEMPLATE (id
) = name
;
3614 return TYPE_IDENTIFIER (t
);
3618 add_pending_template (d
)
3623 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3624 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3626 ti
= DECL_TEMPLATE_INFO (d
);
3628 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3631 *template_tail
= perm_tree_cons
3632 (build_srcloc_here (), d
, NULL_TREE
);
3633 template_tail
= &TREE_CHAIN (*template_tail
);
3634 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3638 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3639 may be either a _DECL or an overloaded function or an
3640 IDENTIFIER_NODE), and ARGLIST. */
3643 lookup_template_function (fns
, arglist
)
3648 if (fns
== NULL_TREE
)
3650 cp_error ("non-template used as template");
3651 return error_mark_node
;
3654 type
= TREE_TYPE (fns
);
3655 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3656 type
= unknown_type_node
;
3658 if (processing_template_decl
)
3659 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3661 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3664 /* Within the scope of a template class S<T>, the name S gets bound
3665 (in build_self_reference) to a TYPE_DECL for the class, not a
3666 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3667 or one of its enclosing classes, and that type is a template,
3668 return the associated TEMPLATE_DECL. Otherwise, the original
3669 DECL is returned. */
3672 maybe_get_template_decl_from_type_decl (decl
)
3675 return (decl
!= NULL_TREE
3676 && TREE_CODE (decl
) == TYPE_DECL
3677 && DECL_ARTIFICIAL (decl
)
3678 && CLASS_TYPE_P (TREE_TYPE (decl
))
3679 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3680 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3683 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3684 parameters, find the desired type.
3686 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3687 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3688 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3689 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3690 copy it here to keep it from being reclaimed when the decl storage
3693 IN_DECL, if non-NULL, is the template declaration we are trying to
3696 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3697 the class we are looking up.
3699 If the template class is really a local class in a template
3700 function, then the FUNCTION_CONTEXT is the function in which it is
3701 being instantiated. */
3704 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3710 tree
template = NULL_TREE
, parmlist
;
3713 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3715 if (IDENTIFIER_VALUE (d1
)
3716 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3717 template = IDENTIFIER_VALUE (d1
);
3721 push_decl_namespace (context
);
3722 template = lookup_name (d1
, /*prefer_type=*/0);
3723 template = maybe_get_template_decl_from_type_decl (template);
3725 pop_decl_namespace ();
3728 context
= DECL_CONTEXT (template);
3730 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3732 tree type
= TREE_TYPE (d1
);
3734 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3735 an implicit typename for the second A. Deal with it. */
3736 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
3737 type
= TREE_TYPE (type
);
3739 if (CLASSTYPE_TEMPLATE_INFO (type
))
3741 template = CLASSTYPE_TI_TEMPLATE (type
);
3742 d1
= DECL_NAME (template);
3745 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3746 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3747 && IS_AGGR_TYPE (d1
)))
3749 template = TYPE_TI_TEMPLATE (d1
);
3750 d1
= DECL_NAME (template);
3752 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3753 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3756 d1
= DECL_NAME (template);
3757 context
= DECL_CONTEXT (template);
3760 my_friendly_abort (272);
3762 /* With something like `template <class T> class X class X { ... };'
3763 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3764 We don't want to do that, but we have to deal with the situation,
3765 so let's give them some syntax errors to chew on instead of a
3769 cp_error ("`%T' is not a template", d1
);
3770 return error_mark_node
;
3773 if (context
== NULL_TREE
)
3774 context
= global_namespace
;
3776 if (TREE_CODE (template) != TEMPLATE_DECL
)
3778 cp_error ("non-template type `%T' used as a template", d1
);
3780 cp_error_at ("for template declaration `%D'", in_decl
);
3781 return error_mark_node
;
3784 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3786 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3787 template arguments */
3789 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3790 tree template2
= TYPE_STUB_DECL (parm
);
3793 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3795 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3796 if (arglist2
== error_mark_node
)
3797 return error_mark_node
;
3799 arglist2
= copy_to_permanent (arglist2
);
3800 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3801 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
3802 TYPE_SIZE (parm
) = 0;
3807 tree template_type
= TREE_TYPE (template);
3810 tree found
= NULL_TREE
;
3813 int is_partial_instantiation
;
3815 gen_tmpl
= most_general_template (template);
3816 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
3817 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3818 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3820 /* We build up the coerced arguments and such on the
3821 momentary_obstack. */
3824 if (arg_depth
== 1 && parm_depth
> 1)
3826 /* We've been given an incomplete set of template arguments.
3829 template <class T> struct S1 {
3830 template <class U> struct S2 {};
3831 template <class U> struct S2<U*> {};
3834 we will be called with an ARGLIST of `U*', but the
3835 TEMPLATE will be `template <class T> template
3836 <class U> struct S1<T>::S2'. We must fill in the missing
3839 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3841 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3844 /* Now we should enough arguments. */
3845 my_friendly_assert (parm_depth
== arg_depth
, 0);
3847 /* From here on, we're only interested in the most general
3849 template = gen_tmpl
;
3851 /* Calculate the BOUND_ARGS. These will be the args that are
3852 actually tsubst'd into the definition to create the
3856 /* We have multiple levels of arguments to coerce, at once. */
3858 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3860 tree bound_args
= make_temp_vec (parm_depth
);
3862 for (i
= saved_depth
,
3863 t
= DECL_TEMPLATE_PARMS (template);
3864 i
> 0 && t
!= NULL_TREE
;
3865 --i
, t
= TREE_CHAIN (t
))
3867 tree a
= coerce_template_parms (TREE_VALUE (t
),
3868 arglist
, template, 1, 1);
3869 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3871 /* We temporarily reduce the length of the ARGLIST so
3872 that coerce_template_parms will see only the arguments
3873 corresponding to the template parameters it is
3875 TREE_VEC_LENGTH (arglist
)--;
3878 /* Restore the ARGLIST to its full size. */
3879 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3881 arglist
= bound_args
;
3885 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3886 innermost_args (arglist
),
3889 if (arglist
== error_mark_node
)
3890 /* We were unable to bind the arguments. */
3891 return error_mark_node
;
3893 /* In the scope of a template class, explicit references to the
3894 template class refer to the type of the template, not any
3895 instantiation of it. For example, in:
3897 template <class T> class C { void f(C<T>); }
3899 the `C<T>' is just the same as `C'. Outside of the
3900 class, however, such a reference is an instantiation. */
3901 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3904 found
= template_type
;
3906 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3910 /* Note that we use DECL_CONTEXT, rather than
3911 CP_DECL_CONTEXT, so that the termination test is
3912 always just `ctx'. We're not interested in namepace
3914 for (ctx
= current_class_type
;
3916 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3917 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3918 if (same_type_p (ctx
, template_type
))
3922 /* We're not in the scope of the class, so the
3923 TEMPLATE_TYPE is not the type we want after
3931 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3932 found
; found
= TREE_CHAIN (found
))
3933 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3937 found
= TREE_VALUE (found
);
3946 /* This type is a "partial instantiation" if any of the template
3947 arguments still inolve template parameters. Note that we set
3948 IS_PARTIAL_INSTANTIATION for partial specializations as
3950 is_partial_instantiation
= uses_template_parms (arglist
);
3952 if (!is_partial_instantiation
3953 && !PRIMARY_TEMPLATE_P (template)
3954 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
3957 found
= xref_tag_from_type (TREE_TYPE (template),
3958 DECL_NAME (template),
3963 /* Since we didn't find the type, we'll have to create it.
3964 Since we'll be saving this type on the
3965 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3966 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3968 /* Create the type. */
3969 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3971 if (!is_partial_instantiation
)
3972 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3974 /* We don't want to call start_enum for this type, since
3975 the values for the enumeration constants may involve
3976 template parameters. And, no one should be interested
3977 in the enumeration constants for such a type. */
3978 t
= make_node (ENUMERAL_TYPE
);
3982 t
= make_lang_type (TREE_CODE (template_type
));
3983 CLASSTYPE_DECLARED_CLASS (t
)
3984 = CLASSTYPE_DECLARED_CLASS (template_type
);
3985 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3986 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3987 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3990 /* If we called start_enum above, this information will already
3994 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3996 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
3997 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
3998 TYPE_STUB_DECL (t
) = type_decl
;
3999 DECL_SOURCE_FILE (type_decl
)
4000 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
4001 DECL_SOURCE_LINE (type_decl
)
4002 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
4005 type_decl
= TYPE_NAME (t
);
4007 /* Set up the template information. We have to figure out which
4008 template is the immediate parent if this is a full
4010 if (parm_depth
== 1 || is_partial_instantiation
4011 || !PRIMARY_TEMPLATE_P (template))
4012 /* This case is easy; there are no member templates involved. */
4016 /* This is a full instantiation of a member template. There
4017 should be some partial instantiation of which this is an
4020 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4021 found
; found
= TREE_CHAIN (found
))
4024 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4026 /* We only want partial instantiations, here, not
4027 specializations or full instantiations. */
4028 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4029 || !uses_template_parms (TREE_VALUE (found
)))
4032 /* Temporarily reduce by one the number of levels in the
4033 ARGLIST and in FOUND so as to avoid comparing the
4034 last set of arguments. */
4035 TREE_VEC_LENGTH (arglist
)--;
4036 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4038 /* See if the arguments match. If they do, then TMPL is
4039 the partial instantiation we want. */
4040 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4042 /* Restore the argument vectors to their full size. */
4043 TREE_VEC_LENGTH (arglist
)++;
4044 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4054 my_friendly_abort (0);
4057 arglist
= copy_to_permanent (arglist
);
4058 SET_TYPE_TEMPLATE_INFO (t
,
4059 tree_cons (found
, arglist
, NULL_TREE
));
4060 DECL_TEMPLATE_INSTANTIATIONS (template)
4061 = tree_cons (arglist
, t
,
4062 DECL_TEMPLATE_INSTANTIATIONS (template));
4064 if (TREE_CODE (t
) == ENUMERAL_TYPE
4065 && !is_partial_instantiation
)
4066 /* Now that the type has been registered on the instantiations
4067 list, we set up the enumerators. Because the enumeration
4068 constants may involve the enumeration type itself, we make
4069 sure to register the type first, and then create the
4070 constants. That way, doing tsubst_expr for the enumeration
4071 constants won't result in recursive calls here; we'll find
4072 the instantiation and exit above. */
4073 tsubst_enum (template_type
, t
, arglist
);
4075 /* We're done with the permanent obstack, now. */
4077 /* We're also done with the momentary allocation we started
4081 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4083 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4084 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4085 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
4086 if (!is_partial_instantiation
)
4088 DECL_ASSEMBLER_NAME (type_decl
)
4089 = get_identifier (build_overload_name (t
, 1, 1));
4091 /* For backwards compatibility; code that uses
4092 -fexternal-templates expects looking up a template to
4093 instantiate it. I think DDD still relies on this.
4094 (jason 8/20/1998) */
4095 if (TREE_CODE (t
) != ENUMERAL_TYPE
4096 && flag_external_templates
4097 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4098 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4099 add_pending_template (t
);
4102 /* If the type makes use of template parameters, the
4103 code that generates debugging information will crash. */
4104 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4110 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
4111 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
4112 If FN returns non-zero, the iteration is terminated, and
4113 for_each_template_parm returns 1. Otherwise, the iteration
4114 continues. If FN never returns a non-zero value, the value
4115 returned by for_each_template_parm is 0. If FN is NULL, it is
4116 considered to be the function which always returns 1. */
4119 for_each_template_parm (t
, fn
, data
)
4127 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
4128 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
4131 switch (TREE_CODE (t
))
4135 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4136 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4138 case IDENTIFIER_NODE
:
4139 if (!IDENTIFIER_TEMPLATE (t
))
4141 my_friendly_abort (42);
4143 /* aggregates of tree nodes */
4146 int i
= TREE_VEC_LENGTH (t
);
4148 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
4153 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
4154 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
4156 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
4159 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
4161 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
4163 /* constructed type nodes */
4165 case REFERENCE_TYPE
:
4166 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4169 if (TYPE_PTRMEMFUNC_FLAG (t
))
4170 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
4176 if (! TYPE_TEMPLATE_INFO (t
))
4178 return for_each_template_parm (TREE_VALUE
4179 (TYPE_TEMPLATE_INFO (t
)),
4182 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4187 /* Check the parameter types. Since default arguments are not
4188 instantiated until they are needed, the TYPE_ARG_TYPES may
4189 contain expressions that involve template parameters. But,
4190 no-one should be looking at them yet. And, once they're
4191 instantiated, they don't contain template parameters, so
4192 there's no point in looking at them then, either. */
4196 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4197 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4201 /* Check the return type, too. */
4202 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4205 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
4207 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4209 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
4211 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4215 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4218 /* A template template parameter is encountered */
4219 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4220 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4221 /* Already substituted template template parameter */
4225 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
4227 goto check_type_and_context
;
4231 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4232 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4236 check_type_and_context
:
4237 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4239 if (DECL_CONTEXT (t
)
4240 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4245 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4246 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4249 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4251 /* template parm nodes */
4252 case TEMPLATE_TEMPLATE_PARM
:
4253 /* Record template parameters such as `T' inside `TT<T>'. */
4254 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
4255 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4257 case TEMPLATE_TYPE_PARM
:
4258 case TEMPLATE_PARM_INDEX
:
4260 return (*fn
)(t
, data
);
4264 /* simple type nodes */
4266 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
4268 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
4274 case NAMESPACE_DECL
:
4285 /* Non-error_mark_node ERROR_MARKs are bad things. */
4286 my_friendly_assert (t
== error_mark_node
, 274);
4291 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4294 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4297 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4298 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4299 (TREE_TYPE (t
)), fn
, data
);
4300 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
4304 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4309 return (for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
)
4310 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
),
4315 /* If there's no type, then this thing must be some expression
4316 involving template parameters. */
4317 if (!fn
&& !TREE_TYPE (t
))
4319 if (TREE_CODE (t
) == COMPONENT_REF
)
4320 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4321 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4323 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4327 case REINTERPRET_CAST_EXPR
:
4328 case CONST_CAST_EXPR
:
4329 case STATIC_CAST_EXPR
:
4330 case DYNAMIC_CAST_EXPR
:
4335 case PSEUDO_DTOR_EXPR
:
4341 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
4349 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
4350 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
4357 sorry ("testing %s for template parms",
4358 tree_code_name
[(int) TREE_CODE (t
)]);
4359 my_friendly_abort (82);
4366 uses_template_parms (t
)
4369 return for_each_template_parm (t
, 0, 0);
4372 static struct tinst_level
*current_tinst_level
;
4373 static struct tinst_level
*free_tinst_level
;
4374 static int tinst_depth
;
4375 extern int max_tinst_depth
;
4376 #ifdef GATHER_STATISTICS
4379 int tinst_level_tick
;
4380 int last_template_error_tick
;
4382 /* Print out all the template instantiations that we are currently
4383 working on. If ERR, we are being called from cp_thing, so do
4384 the right thing for an error message. */
4387 print_template_context (err
)
4390 struct tinst_level
*p
= current_tinst_level
;
4392 char *file
= input_filename
;
4396 if (current_function_decl
!= p
->decl
4397 && current_function_decl
!= NULL_TREE
)
4398 /* We can get here during the processing of some synthesized
4399 method. Then, p->decl will be the function that's causing
4404 if (current_function_decl
== p
->decl
)
4405 /* Avoid redundancy with the the "In function" line. */;
4407 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4408 file
, decl_as_string (p
->decl
, 0));
4416 for (; p
; p
= p
->next
)
4418 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4419 decl_as_string (p
->decl
, 0));
4423 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4426 /* Called from cp_thing to print the template context for an error. */
4429 maybe_print_template_context ()
4431 if (last_template_error_tick
== tinst_level_tick
4432 || current_tinst_level
== 0)
4435 last_template_error_tick
= tinst_level_tick
;
4436 print_template_context (1);
4440 push_tinst_level (d
)
4443 struct tinst_level
*new;
4445 if (tinst_depth
>= max_tinst_depth
)
4447 /* If the instantiation in question still has unbound template parms,
4448 we don't really care if we can't instantiate it, so just return.
4449 This happens with base instantiation for implicit `typename'. */
4450 if (uses_template_parms (d
))
4453 last_template_error_tick
= tinst_level_tick
;
4454 error ("template instantiation depth exceeds maximum of %d",
4456 error (" (use -ftemplate-depth-NN to increase the maximum)");
4457 cp_error (" instantiating `%D'", d
);
4459 print_template_context (0);
4464 if (free_tinst_level
)
4466 new = free_tinst_level
;
4467 free_tinst_level
= new->next
;
4470 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4474 new->file
= input_filename
;
4475 new->next
= current_tinst_level
;
4476 current_tinst_level
= new;
4479 #ifdef GATHER_STATISTICS
4480 if (tinst_depth
> depth_reached
)
4481 depth_reached
= tinst_depth
;
4491 struct tinst_level
*old
= current_tinst_level
;
4493 /* Restore the filename and line number stashed away when we started
4494 this instantiation. */
4496 input_filename
= old
->file
;
4497 extract_interface_info ();
4499 current_tinst_level
= old
->next
;
4500 old
->next
= free_tinst_level
;
4501 free_tinst_level
= old
;
4506 struct tinst_level
*
4509 struct tinst_level
*p
= current_tinst_level
;
4512 for (; p
->next
; p
= p
->next
)
4517 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4518 vector of template arguments, as for tsubst.
4520 Returns an appropriate tsbust'd friend declaration. */
4523 tsubst_friend_function (decl
, args
)
4529 char *file
= input_filename
;
4531 lineno
= DECL_SOURCE_LINE (decl
);
4532 input_filename
= DECL_SOURCE_FILE (decl
);
4534 if (TREE_CODE (decl
) == FUNCTION_DECL
4535 && DECL_TEMPLATE_INSTANTIATION (decl
)
4536 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4537 /* This was a friend declared with an explicit template
4538 argument list, e.g.:
4542 to indicate that f was a template instantiation, not a new
4543 function declaration. Now, we have to figure out what
4544 instantiation of what template. */
4551 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4552 args
, /*complain=*/1,
4554 tsubst (DECL_TI_ARGS (decl
),
4555 args
, /*complain=*/1,
4557 /* FIXME: The decl we create via the next tsubst could be
4558 created on a temporary obstack. */
4559 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4560 tmpl
= determine_specialization (template_id
, new_friend
,
4562 /*need_member_template=*/0);
4563 new_friend
= instantiate_template (tmpl
, new_args
);
4567 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4569 /* The NEW_FRIEND will look like an instantiation, to the
4570 compiler, but is not an instantiation from the point of view of
4571 the language. For example, we might have had:
4573 template <class T> struct S {
4574 template <class U> friend void f(T, U);
4577 Then, in S<int>, template <class U> void f(int, U) is not an
4578 instantiation of anything. */
4579 DECL_USE_TEMPLATE (new_friend
) = 0;
4580 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4581 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4583 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4584 tsubst will have resulted in a call to
4585 set_mangled_name_for_template_decl. But, the function is not a
4586 template instantiation and should not be mangled like one.
4587 Therefore, we remangle the function name. We don't have to do
4588 this if the NEW_FRIEND is a template since
4589 set_mangled_name_for_template_decl doesn't do anything if the
4590 function declaration still uses template arguments. */
4591 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4593 set_mangled_name_for_decl (new_friend
);
4594 DECL_RTL (new_friend
) = 0;
4595 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4598 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4601 tree new_friend_template_info
;
4602 tree new_friend_result_template_info
;
4604 int new_friend_is_defn
;
4606 /* We must save some information from NEW_FRIEND before calling
4607 duplicate decls since that function will free NEW_FRIEND if
4609 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4610 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4612 /* This declaration is a `primary' template. */
4613 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4616 = DECL_INITIAL (DECL_RESULT (new_friend
)) != NULL_TREE
;
4617 new_friend_result_template_info
4618 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend
));
4622 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4623 new_friend_result_template_info
= NULL_TREE
;
4626 /* Inside pushdecl_namespace_level, we will push into the
4627 current namespace. However, the friend function should
4628 tyically go into the namespace of the template. */
4629 ns
= decl_namespace_context (new_friend
);
4630 push_nested_namespace (ns
);
4631 old_decl
= pushdecl_namespace_level (new_friend
);
4632 pop_nested_namespace (ns
);
4634 if (old_decl
!= new_friend
)
4636 /* This new friend declaration matched an existing
4637 declaration. For example, given:
4639 template <class T> void f(T);
4640 template <class U> class C {
4641 template <class T> friend void f(T) {}
4644 the friend declaration actually provides the definition
4645 of `f', once C has been instantiated for some type. So,
4646 old_decl will be the out-of-class template declaration,
4647 while new_friend is the in-class definition.
4649 But, if `f' was called before this point, the
4650 instantiation of `f' will have DECL_TI_ARGS corresponding
4651 to `T' but not to `U', references to which might appear
4652 in the definition of `f'. Previously, the most general
4653 template for an instantiation of `f' was the out-of-class
4654 version; now it is the in-class version. Therefore, we
4655 run through all specialization of `f', adding to their
4656 DECL_TI_ARGS appropriately. In particular, they need a
4657 new set of outer arguments, corresponding to the
4658 arguments for this class instantiation.
4660 The same situation can arise with something like this:
4663 template <class T> class C {
4667 when `C<int>' is instantiated. Now, `f(int)' is defined
4670 if (!new_friend_is_defn
)
4671 /* On the other hand, if the in-class declaration does
4672 *not* provide a definition, then we don't want to alter
4673 existing definitions. We can just leave everything
4678 /* Overwrite whatever template info was there before, if
4679 any, with the new template information pertaining to
4681 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4683 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4684 /* duplicate_decls will take care of this case. */
4689 tree new_friend_args
;
4691 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl
))
4692 = new_friend_result_template_info
;
4694 new_friend_args
= TI_ARGS (new_friend_template_info
);
4695 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4699 tree spec
= TREE_VALUE (t
);
4702 = add_outermost_template_args (new_friend_args
,
4703 DECL_TI_ARGS (spec
));
4705 = copy_to_permanent (DECL_TI_ARGS (spec
));
4708 /* Now, since specializations are always supposed to
4709 hang off of the most general template, we must move
4711 t
= most_general_template (old_decl
);
4714 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4715 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4716 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4717 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4722 /* The information from NEW_FRIEND has been merged into OLD_DECL
4723 by duplicate_decls. */
4724 new_friend
= old_decl
;
4727 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4729 /* Check to see that the declaration is really present, and,
4730 possibly obtain an improved declaration. */
4731 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4740 input_filename
= file
;
4744 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4745 template arguments, as for tsubst.
4747 Returns an appropriate tsbust'd friend type. */
4750 tsubst_friend_class (friend_tmpl
, args
)
4757 /* First, we look for a class template. */
4758 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
4760 /* But, if we don't find one, it might be because we're in a
4761 situation like this:
4769 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4770 for `S<int>', not the TEMPLATE_DECL. */
4771 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
4773 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
4774 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4777 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4779 /* The friend template has already been declared. Just
4780 check to see that the declarations match, and install any new
4781 default parameters. We must tsubst the default parameters,
4782 of course. We only need the innermost template parameters
4783 because that is all that redeclare_class_template will look
4786 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4787 args
, /*complain=*/1);
4788 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4789 friend_type
= TREE_TYPE (tmpl
);
4793 /* The friend template has not already been declared. In this
4794 case, the instantiation of the template class will cause the
4795 injection of this template into the global scope. */
4796 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4798 /* The new TMPL is not an instantiation of anything, so we
4799 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4800 the new type because that is supposed to be the corresponding
4801 template decl, i.e., TMPL. */
4802 DECL_USE_TEMPLATE (tmpl
) = 0;
4803 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4804 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4806 /* Inject this template into the global scope. */
4807 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4814 instantiate_class_template (type
)
4817 tree
template, args
, pattern
, t
;
4820 if (type
== error_mark_node
)
4821 return error_mark_node
;
4823 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4826 /* We want to allocate temporary vectors of template arguments and
4827 template argument expressions on the momentary obstack, not on
4828 the expression obstack. Otherwise, all the space allocated in
4829 argument coercion and such is simply lost. */
4832 /* Figure out which template is being instantiated. */
4833 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4834 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4836 /* Figure out which arguments are being used to do the
4838 args
= CLASSTYPE_TI_ARGS (type
);
4839 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4841 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4842 /* If this is a partial instantiation, then we can't instantiate
4843 the type; there's no telling whether or not one of the
4844 template parameters might eventually be instantiated to some
4845 value that results in a specialization being used. For
4857 Now, the `S<U>' in `f<int>' is the specialization, not an
4858 instantiation of the original template. */
4861 /* Determine what specialization of the original template to
4863 if (PARTIAL_INSTANTIATION_P (type
))
4864 /* There's no telling which specialization is appropriate at this
4865 point. Since all peeking at the innards of this partial
4866 instantiation are extensions (like the "implicit typename"
4867 extension, which allows users to omit the keyword `typename' on
4868 names that are declared as types in template base classes), we
4869 are free to do what we please.
4871 Trying to figure out which partial instantiation to use can
4872 cause a crash. (Some of the template arguments don't even have
4873 types.) So, we just use the most general version. */
4877 t
= most_specialized_class (template, args
);
4879 if (t
== error_mark_node
)
4881 const char *str
= "candidates are:";
4882 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4883 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4886 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4889 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4893 TYPE_BEING_DEFINED (type
) = 1;
4894 type
= error_mark_node
;
4900 pattern
= TREE_TYPE (t
);
4902 pattern
= TREE_TYPE (template);
4904 /* If the template we're instantiating is incomplete, then clearly
4905 there's nothing we can do. */
4906 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4909 /* If this is a partial instantiation, don't tsubst anything. We will
4910 only use this type for implicit typename, so the actual contents don't
4911 matter. All that matters is whether a particular name is a type. */
4912 if (PARTIAL_INSTANTIATION_P (type
))
4914 /* The fields set here must be kept in sync with those cleared
4915 in begin_class_definition. */
4916 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4917 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4918 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4919 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4920 /* Pretend that the type is complete, so that we will look
4921 inside it during name lookup and such. */
4922 TYPE_SIZE (type
) = integer_zero_node
;
4926 /* If we've recursively instantiated too many templates, stop. */
4927 if (! push_tinst_level (type
))
4930 /* Now we're really doing the instantiation. Mark the type as in
4931 the process of being defined. */
4932 TYPE_BEING_DEFINED (type
) = 1;
4934 maybe_push_to_top_level (uses_template_parms (type
));
4938 /* This TYPE is actually a instantiation of of a partial
4939 specialization. We replace the innermost set of ARGS with
4940 the arguments appropriate for substitution. For example,
4943 template <class T> struct S {};
4944 template <class T> struct S<T*> {};
4946 and supposing that we are instantiating S<int*>, ARGS will
4947 present be {int*} but we need {int}. */
4949 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4952 /* If there were multiple levels in ARGS, replacing the
4953 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4954 want, so we make a copy first. */
4955 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4957 args
= copy_node (args
);
4958 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4964 if (flag_external_templates
)
4966 if (flag_alt_external_templates
)
4968 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4969 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4970 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4971 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4972 && CLASSTYPE_INTERFACE_KNOWN (type
));
4976 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4977 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4978 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4979 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4980 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4981 && CLASSTYPE_INTERFACE_KNOWN (type
));
4986 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4987 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4990 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4991 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4992 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4993 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4994 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4995 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4996 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4997 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4998 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4999 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
5000 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
5001 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5002 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5003 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5004 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5005 TYPE_USES_COMPLEX_INHERITANCE (type
)
5006 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
5007 TYPE_USES_MULTIPLE_INHERITANCE (type
)
5008 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
5009 TYPE_USES_VIRTUAL_BASECLASSES (type
)
5010 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
5011 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5012 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5013 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5014 if (ANON_AGGR_TYPE_P (pattern
))
5015 SET_ANON_AGGR_TYPE_P (type
);
5017 /* We must copy the arguments to the permanent obstack since
5018 during the tsubst'ing below they may wind up in the
5019 DECL_TI_ARGS of some instantiated member template. */
5020 args
= copy_to_permanent (args
);
5022 if (TYPE_BINFO_BASETYPES (pattern
))
5024 tree base_list
= NULL_TREE
;
5025 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
5028 /* Substitute into each of the bases to determine the actual
5030 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
5036 pbase
= TREE_VEC_ELT (pbases
, i
);
5038 /* Substitue to figure out the base class. */
5039 base
= tsubst (BINFO_TYPE (pbase
), args
,
5040 /*complain=*/1, NULL_TREE
);
5041 if (base
== error_mark_node
)
5044 /* Calculate the correct access node. */
5045 if (TREE_VIA_VIRTUAL (pbase
))
5047 if (TREE_VIA_PUBLIC (pbase
))
5048 access
= access_public_virtual_node
;
5049 else if (TREE_VIA_PROTECTED (pbase
))
5050 access
= access_protected_virtual_node
;
5052 access
= access_private_virtual_node
;
5056 if (TREE_VIA_PUBLIC (pbase
))
5057 access
= access_public_node
;
5058 else if (TREE_VIA_PROTECTED (pbase
))
5059 access
= access_protected_node
;
5061 access
= access_private_node
;
5064 base_list
= tree_cons (access
, base
, base_list
);
5067 /* The list is now in reverse order; correct that. */
5068 base_list
= nreverse (base_list
);
5070 /* Now call xref_basetypes to set up all the base-class
5072 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
5073 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
5074 ? class_type_node
: record_type_node
)
5076 DECL_NAME (TYPE_NAME (pattern
)),
5081 /* Now that our base classes are set up, enter the scope of the
5082 class, so that name lookups into base classes, etc. will work
5083 corectly. This is precisely analagous to what we do in
5084 begin_class_definition when defining an ordinary non-template
5086 pushclass (type
, 1);
5088 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
5090 tree tag
= TREE_VALUE (t
);
5091 tree name
= TYPE_IDENTIFIER (tag
);
5094 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
5095 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5097 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
5098 /* Unfortunately, lookup_template_class sets
5099 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5100 instantiation (i.e., for the type of a member template
5101 class nested within a template class.) This behavior is
5102 required for maybe_process_partial_specialization to work
5103 correctly, but is not accurate in this case; the TAG is not
5104 an instantiation of anything. (The corresponding
5105 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5106 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5108 /* Now, we call pushtag to put this NEWTAG into the scope of
5109 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5110 pushtag calling push_template_decl. We don't have to do
5111 this for enums because it will already have been done in
5114 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5115 pushtag (name
, newtag
, /*globalize=*/0);
5119 /* Don't replace enum constants here. */
5120 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
5121 if (TREE_CODE (t
) != CONST_DECL
)
5125 /* The the file and line for this declaration, to assist in
5126 error message reporting. Since we called push_tinst_level
5127 above, we don't need to restore these. */
5128 lineno
= DECL_SOURCE_LINE (t
);
5129 input_filename
= DECL_SOURCE_FILE (t
);
5131 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5132 if (TREE_CODE (r
) == VAR_DECL
)
5136 if (DECL_DEFINED_IN_CLASS_P (r
))
5137 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5138 /*complain=*/1, NULL_TREE
);
5142 finish_static_data_member_decl (r
, init
,
5143 /*asmspec_tree=*/NULL_TREE
,
5147 if (DECL_DEFINED_IN_CLASS_P (r
))
5148 check_static_variable_definition (r
, TREE_TYPE (r
));
5151 /* R will have a TREE_CHAIN if and only if it has already been
5152 processed by finish_member_declaration. This can happen
5153 if, for example, it is a TYPE_DECL for a class-scoped
5154 ENUMERAL_TYPE; such a thing will already have been added to
5155 the field list by tsubst_enum above. */
5156 if (!TREE_CHAIN (r
))
5158 set_current_access_from_decl (r
);
5159 finish_member_declaration (r
);
5163 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5164 for this instantiation. */
5165 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
5167 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5168 set_current_access_from_decl (r
);
5169 finish_member_declaration (r
);
5172 /* Construct the DECL_FRIENDLIST for the new class type. */
5173 typedecl
= TYPE_MAIN_DECL (type
);
5174 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
5180 for (friends
= TREE_VALUE (t
);
5181 friends
!= NULL_TREE
;
5182 friends
= TREE_CHAIN (friends
))
5183 if (TREE_PURPOSE (friends
) == error_mark_node
)
5185 tsubst_friend_function (TREE_VALUE (friends
),
5189 tsubst_copy (TREE_PURPOSE (t
), args
,
5190 /*complain=*/1, NULL_TREE
),
5191 tsubst (TREE_PURPOSE (friends
), args
,
5192 /*complain=*/1, NULL_TREE
));
5195 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
5199 tree friend_type
= TREE_VALUE (t
);
5200 tree new_friend_type
;
5202 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5203 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5204 else if (uses_template_parms (friend_type
))
5205 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
5209 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5211 /* The call to xref_tag_from_type does injection for friend
5213 push_nested_namespace (ns
);
5215 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5216 pop_nested_namespace (ns
);
5219 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5220 /* Trick make_friend_class into realizing that the friend
5221 we're adding is a template, not an ordinary class. It's
5222 important that we use make_friend_class since it will
5223 perform some error-checking and output cross-reference
5225 ++processing_template_decl
;
5227 make_friend_class (type
, new_friend_type
);
5229 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5230 --processing_template_decl
;
5233 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5234 if (TREE_CODE (t
) == FIELD_DECL
)
5236 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5237 require_complete_type (t
);
5240 /* Set the file and line number information to whatever is given for
5241 the class itself. This puts error messages involving generated
5242 implicit functions at a predictable point, and the same point
5243 that would be used for non-template classes. */
5244 lineno
= DECL_SOURCE_LINE (typedecl
);
5245 input_filename
= DECL_SOURCE_FILE (typedecl
);
5247 unreverse_member_declarations (type
);
5248 finish_struct_1 (type
);
5249 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5251 /* Clear this now so repo_template_used is happy. */
5252 TYPE_BEING_DEFINED (type
) = 0;
5253 repo_template_used (type
);
5255 /* Now that the class is complete, instantiate default arguments for
5256 any member functions. We don't do this earlier because the
5257 default arguments may reference members of the class. */
5258 if (!PRIMARY_TEMPLATE_P (template))
5259 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5260 if (TREE_CODE (t
) == FUNCTION_DECL
5261 /* Implicitly generated member functions will not have tmplate
5262 information; they are not instantiations, but instead are
5263 created "fresh" for each instantiation. */
5264 && DECL_TEMPLATE_INFO (t
))
5265 tsubst_default_arguments (t
);
5268 pop_from_top_level ();
5281 if (t1
== NULL_TREE
)
5282 return t2
== NULL_TREE
;
5283 if (t2
== NULL_TREE
)
5285 /* Don't care if one declares its arg const and the other doesn't -- the
5286 main variant of the arg type is all that matters. */
5287 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5288 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5290 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5293 /* If arg is a non-type template parameter that does not depend on template
5294 arguments, fold it like we weren't in the body of a template. */
5297 maybe_fold_nontype_arg (arg
)
5300 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
5301 && !uses_template_parms (arg
))
5303 /* Sometimes, one of the args was an expression involving a
5304 template constant parameter, like N - 1. Now that we've
5305 tsubst'd, we might have something like 2 - 1. This will
5306 confuse lookup_template_class, so we do constant folding
5307 here. We have to unset processing_template_decl, to
5308 fool build_expr_from_tree() into building an actual
5311 int saved_processing_template_decl
= processing_template_decl
;
5312 processing_template_decl
= 0;
5313 arg
= fold (build_expr_from_tree (arg
));
5314 processing_template_decl
= saved_processing_template_decl
;
5319 /* Return the TREE_VEC with the arguments for the innermost template header,
5320 where ARGS is either that or the VEC of VECs for all the
5324 innermost_args (args
)
5327 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
5330 /* Substitute ARGS into the vector of template arguments T. */
5333 tsubst_template_arg_vector (t
, args
, complain
)
5338 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5339 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5341 bzero ((char *) elts
, len
* sizeof (tree
));
5343 for (i
= 0; i
< len
; i
++)
5345 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5346 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5347 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5350 elts
[i
] = maybe_fold_nontype_arg
5351 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5354 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5361 t
= make_temp_vec (len
);
5362 for (i
= 0; i
< len
; i
++)
5363 TREE_VEC_ELT (t
, i
) = elts
[i
];
5368 /* Return the result of substituting ARGS into the template parameters
5369 given by PARMS. If there are m levels of ARGS and m + n levels of
5370 PARMS, then the result will contain n levels of PARMS. For
5371 example, if PARMS is `template <class T> template <class U>
5372 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5373 result will be `template <int*, double, class V>'. */
5376 tsubst_template_parms (parms
, args
, complain
)
5384 for (new_parms
= &r
;
5385 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5386 new_parms
= &(TREE_CHAIN (*new_parms
)),
5387 parms
= TREE_CHAIN (parms
))
5390 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5393 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5395 tree default_value
=
5396 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5398 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5400 TREE_VEC_ELT (new_vec
, i
)
5401 = build_tree_list (tsubst (default_value
, args
, complain
,
5403 tsubst (parm_decl
, args
, complain
,
5408 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5409 - TMPL_ARGS_DEPTH (args
))),
5410 new_vec
, NULL_TREE
);
5416 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5417 type T. If T is not an aggregate or enumeration type, it is
5418 handled as if by tsubst. IN_DECL is as for tsubst. If
5419 ENTERING_SCOPE is non-zero, T is the context for a template which
5420 we are presently tsubst'ing. Return the subsituted value. */
5423 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5433 switch (TREE_CODE (t
))
5436 if (TYPE_PTRMEMFUNC_P (t
))
5438 tree r
= build_ptrmemfunc_type
5439 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5440 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5444 /* else fall through */
5447 if (TYPE_TEMPLATE_INFO (t
))
5453 /* First, determine the context for the type we are looking
5455 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5456 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5458 in_decl
, /*entering_scope=*/1);
5460 context
= NULL_TREE
;
5462 /* Then, figure out what arguments are appropriate for the
5463 type we are trying to find. For example, given:
5465 template <class T> struct S;
5466 template <class T, class U> void f(T, U) { S<U> su; }
5468 and supposing that we are instantiating f<int, double>,
5469 then our ARGS will be {int, double}, but, when looking up
5470 S we only want {double}. */
5472 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5475 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5479 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5483 /* This is not a template type, so there's nothing to do. */
5487 return tsubst (t
, args
, complain
, in_decl
);
5491 /* Substitute into the default argument ARG (a default argument for
5492 FN), which has the indicated TYPE. */
5495 tsubst_default_argument (fn
, type
, arg
)
5500 /* This default argument came from a template. Instantiate the
5501 default argument here, not in tsubst. In the case of
5510 we must be careful to do name lookup in the scope of S<T>,
5511 rather than in the current class. */
5512 if (DECL_CLASS_SCOPE_P (fn
))
5513 pushclass (DECL_REAL_CONTEXT (fn
), 2);
5515 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), /*complain=*/1, NULL_TREE
);
5517 if (DECL_CLASS_SCOPE_P (fn
))
5520 /* Make sure the default argument is reasonable. */
5521 arg
= check_default_argument (type
, arg
);
5526 /* Substitute into all the default arguments for FN. */
5529 tsubst_default_arguments (fn
)
5535 tmpl_args
= DECL_TI_ARGS (fn
);
5537 /* If this function is not yet instantiated, we certainly don't need
5538 its default arguments. */
5539 if (uses_template_parms (tmpl_args
))
5542 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5544 arg
= TREE_CHAIN (arg
))
5545 if (TREE_PURPOSE (arg
))
5546 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5548 TREE_PURPOSE (arg
));
5551 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5552 (already computed) substitution of ARGS into TREE_TYPE (T), if
5553 appropriate. Return the result of the substitution. IN_DECL is as
5557 tsubst_decl (t
, args
, type
, in_decl
)
5564 char* saved_filename
;
5567 /* Set the filename and linenumber to improve error-reporting. */
5568 saved_lineno
= lineno
;
5569 saved_filename
= input_filename
;
5570 lineno
= DECL_SOURCE_LINE (t
);
5571 input_filename
= DECL_SOURCE_FILE (t
);
5573 switch (TREE_CODE (t
))
5577 /* We can get here when processing a member template function
5578 of a template class. */
5579 tree decl
= DECL_TEMPLATE_RESULT (t
);
5581 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5583 if (!is_template_template_parm
)
5585 /* We might already have an instance of this template.
5586 The ARGS are for the surrounding class type, so the
5587 full args contain the tsubst'd args for the context,
5588 plus the innermost args from the template decl. */
5589 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5590 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5591 : DECL_TI_ARGS (DECL_RESULT (t
));
5595 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5598 /* tsubst_template_arg_vector doesn't copy the vector if
5599 nothing changed. But, *something* should have
5601 my_friendly_assert (full_args
!= tmpl_args
, 0);
5603 spec
= retrieve_specialization (t
, full_args
);
5605 if (spec
!= NULL_TREE
)
5612 /* Make a new template decl. It will be similar to the
5613 original, but will record the current template arguments.
5614 We also create a new function declaration, which is just
5615 like the old one, but points to this new template, rather
5616 than the old one. */
5619 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5620 TREE_CHAIN (r
) = NULL_TREE
;
5622 if (is_template_template_parm
)
5624 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5625 DECL_RESULT (r
) = new_decl
;
5626 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5631 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1,
5632 in_decl
, /*entering_scope=*/1);
5633 DECL_CLASS_CONTEXT (r
)
5634 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5635 /*complain=*/1, in_decl
,
5636 /*entering_scope=*/1);
5637 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5639 if (TREE_CODE (decl
) == TYPE_DECL
)
5641 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5642 /*complain=*/1, in_decl
);
5643 TREE_TYPE (r
) = new_type
;
5644 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5645 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5646 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5650 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5651 DECL_RESULT (r
) = new_decl
;
5652 DECL_TI_TEMPLATE (new_decl
) = r
;
5653 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5654 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5657 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5658 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5659 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5661 /* The template parameters for this new template are all the
5662 template parameters for the old template, except the
5663 outermost level of parameters. */
5664 DECL_TEMPLATE_PARMS (r
)
5665 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5668 if (PRIMARY_TEMPLATE_P (t
))
5669 DECL_PRIMARY_TEMPLATE (r
) = r
;
5671 /* We don't partially instantiate partial specializations. */
5672 if (TREE_CODE (decl
) == TYPE_DECL
)
5675 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5677 spec
= TREE_CHAIN (spec
))
5679 /* It helps to consider example here. Consider:
5690 Now, for example, we are instantiating S<int>::f(U u).
5691 We want to make a template:
5696 It will have a specialization, for the case U = int*, of
5700 void S<int>::f<int*>(int*);
5702 This specialization will be an instantiation of
5703 the specialization given in the declaration of S, with
5704 argument list int*. */
5706 tree fn
= TREE_VALUE (spec
);
5710 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5711 /* Instantiations are on the same list, but they're of
5712 no concern to us. */
5715 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5716 /* A full specialization. There's no need to record
5720 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
5721 /*complain=*/1, in_decl
);
5722 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5723 spec_args
, /*complain=*/1, in_decl
);
5724 DECL_TI_TEMPLATE (new_fn
) = fn
;
5725 register_specialization (new_fn
, r
,
5726 innermost_args (spec_args
));
5729 /* Record this partial instantiation. */
5730 register_specialization (r
, t
,
5731 DECL_TI_ARGS (DECL_RESULT (r
)));
5739 tree argvec
= NULL_TREE
;
5746 /* Nobody should be tsubst'ing into non-template functions. */
5747 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5749 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5753 /* Allocate template arguments on the momentary obstack,
5754 in case we don't need to keep them. */
5757 /* Calculate the most general template of which R is a
5758 specialization, and the complete set of arguments used to
5760 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5762 = tsubst_template_arg_vector (DECL_TI_ARGS
5763 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5764 args
, /*complain=*/1);
5766 /* Check to see if we already have this specialization. */
5767 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5776 /* We're going to need to keep the ARGVEC, so we copy it
5778 argvec
= copy_to_permanent (argvec
);
5781 /* Here, we deal with the peculiar case:
5783 template <class T> struct S {
5784 template <class U> friend void f();
5786 template <class U> friend void f() {}
5788 template void f<double>();
5790 Here, the ARGS for the instantiation of will be {int,
5791 double}. But, we only need as many ARGS as there are
5792 levels of template parameters in CODE_PATTERN. We are
5793 careful not to get fooled into reducing the ARGS in
5796 template <class T> struct S { template <class U> void f(U); }
5797 template <class T> template <> void S<T>::f(int) {}
5799 which we can spot because the pattern will be a
5800 specialization in this case. */
5801 args_depth
= TMPL_ARGS_DEPTH (args
);
5803 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5804 if (args_depth
> parms_depth
5805 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5807 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5809 if (parms_depth
> 1)
5813 args
= make_temp_vec (parms_depth
);
5814 for (i
= 0; i
< parms_depth
; ++i
)
5815 TREE_VEC_ELT (args
, i
) =
5816 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5819 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5824 /* This special case arises when we have something like this:
5826 template <class T> struct S {
5827 friend void f<int>(int, double);
5830 Here, the DECL_TI_TEMPLATE for the friend declaration
5831 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5832 being called from tsubst_friend_function, and we want
5833 only to create a new decl (R) with appropriate types so
5834 that we can call determine_specialization. */
5835 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5837 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5838 == IDENTIFIER_NODE
), 0);
5839 gen_tmpl
= NULL_TREE
;
5842 if (DECL_CLASS_SCOPE_P (t
))
5844 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5848 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5850 /*entering_scope=*/1);
5857 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5859 /* We do NOT check for matching decls pushed separately at this
5860 point, as they may not represent instantiations of this
5861 template, and in any case are considered separate under the
5862 discrete model. Instead, see add_maybe_template. */
5866 DECL_USE_TEMPLATE (r
) = 0;
5867 TREE_TYPE (r
) = type
;
5870 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1, t
,
5871 /*entering_scope=*/1);
5872 DECL_CLASS_CONTEXT (r
) = ctx
;
5874 if (member
&& IDENTIFIER_TYPENAME_P (DECL_NAME (r
)))
5875 /* Type-conversion operator. Reconstruct the name, in
5876 case it's the name of one of the template's parameters. */
5877 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5879 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5881 DECL_MAIN_VARIANT (r
) = r
;
5882 DECL_RESULT (r
) = NULL_TREE
;
5884 TREE_STATIC (r
) = 0;
5885 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5886 DECL_EXTERNAL (r
) = 1;
5887 DECL_INTERFACE_KNOWN (r
) = 0;
5888 DECL_DEFER_OUTPUT (r
) = 0;
5889 TREE_CHAIN (r
) = NULL_TREE
;
5890 DECL_PENDING_INLINE_INFO (r
) = 0;
5893 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5894 name. There's no need to do this in the special friend
5895 case mentioned above where GEN_TMPL is NULL. */
5898 /* The ARGVEC was built on the momentary obstack. Make it
5900 argvec
= copy_to_permanent (argvec
);
5901 DECL_TEMPLATE_INFO (r
)
5902 = perm_tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5903 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5904 register_specialization (r
, gen_tmpl
, argvec
);
5906 /* Set the mangled name for R. */
5907 if (DECL_DESTRUCTOR_P (t
))
5908 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5911 /* Instantiations of template functions must be mangled
5912 specially, in order to conform to 14.5.5.1
5913 [temp.over.link]. */
5914 tree tmpl
= DECL_TI_TEMPLATE (t
);
5916 /* TMPL will be NULL if this is a specialization of a
5917 member function of a template class. */
5918 if (name_mangling_version
< 1
5919 || tmpl
== NULL_TREE
5920 || (member
&& !is_member_template (tmpl
)
5921 && !DECL_TEMPLATE_INFO (tmpl
)))
5922 set_mangled_name_for_decl (r
);
5924 set_mangled_name_for_template_decl (r
);
5928 make_decl_rtl (r
, NULL_PTR
, 1);
5930 /* Like grokfndecl. If we don't do this, pushdecl will
5931 mess up our TREE_CHAIN because it doesn't find a
5932 previous decl. Sigh. */
5934 && ! uses_template_parms (r
)
5935 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5937 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5939 /* We're not supposed to instantiate default arguments
5940 until they are called, for a template. But, for a
5943 template <class T> void f ()
5944 { extern void g(int i = T()); }
5946 we should do the substitution when the template is
5947 instantiated. We handle the member function case in
5948 instantiate_class_template since the default arguments
5949 might refer to other members of the class. */
5951 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
5952 && !uses_template_parms (argvec
))
5953 tsubst_default_arguments (r
);
5956 /* Copy the list of befriending classes. */
5957 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5959 friends
= &TREE_CHAIN (*friends
))
5961 *friends
= copy_node (*friends
);
5962 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5963 args
, /*complain=*/1,
5967 if (DECL_CONSTRUCTOR_P (r
))
5969 maybe_retrofit_in_chrg (r
);
5970 grok_ctor_properties (ctx
, r
);
5972 else if (DECL_OVERLOADED_OPERATOR_P (r
))
5973 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5980 TREE_TYPE (r
) = type
;
5981 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5983 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5984 DECL_INITIAL (r
) = TREE_TYPE (r
);
5986 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5987 /*complain=*/1, in_decl
);
5989 DECL_CONTEXT (r
) = NULL_TREE
;
5990 if (PROMOTE_PROTOTYPES
5991 && (TREE_CODE (type
) == INTEGER_TYPE
5992 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5993 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5994 DECL_ARG_TYPE (r
) = integer_type_node
;
5996 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5997 /*complain=*/1, TREE_CHAIN (t
));
6005 TREE_TYPE (r
) = type
;
6006 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
6008 /* We don't have to set DECL_CONTEXT here; it is set by
6009 finish_member_declaration. */
6010 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
6011 /*complain=*/1, in_decl
);
6012 TREE_CHAIN (r
) = NULL_TREE
;
6013 if (TREE_CODE (type
) == VOID_TYPE
)
6014 cp_error_at ("instantiation of `%D' as type void", r
);
6022 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
6023 TREE_CHAIN (r
) = NULL_TREE
;
6028 if (DECL_IMPLICIT_TYPEDEF_P (t
))
6030 /* For an implicit typedef, we just want the implicit
6031 typedef for the tsubst'd type. We've already got the
6032 tsubst'd type, as TYPE, so we just need it's associated
6034 r
= TYPE_NAME (type
);
6037 else if (!DECL_LANG_SPECIFIC (t
))
6039 /* For a template type parameter, we don't have to do
6040 anything special. */
6041 r
= TYPE_NAME (type
);
6055 /* Nobody should be tsubst'ing into non-template variables. */
6056 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
6057 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
6059 if (TYPE_P (DECL_CONTEXT (t
)))
6060 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6062 in_decl
, /*entering_scope=*/1);
6064 /* Subsequent calls to pushdecl will fill this in. */
6067 /* Check to see if we already have this specialization. */
6068 tmpl
= DECL_TI_TEMPLATE (t
);
6069 gen_tmpl
= most_general_template (tmpl
);
6070 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
6072 spec
= retrieve_specialization (gen_tmpl
, argvec
);
6074 spec
= retrieve_local_specialization (gen_tmpl
,
6075 current_function_decl
);
6083 /* This declaration is going to have to be around for a while,
6084 so me make sure it is on a saveable obstack. */
6085 push_obstacks_nochange ();
6086 saveable_allocation ();
6090 TREE_TYPE (r
) = type
;
6091 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
6092 DECL_CONTEXT (r
) = ctx
;
6094 /* Don't try to expand the initializer until someone tries to use
6095 this variable; otherwise we run into circular dependencies. */
6096 DECL_INITIAL (r
) = NULL_TREE
;
6100 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
6102 /* Even if the original location is out of scope, the newly
6103 substituted one is not. */
6104 if (TREE_CODE (r
) == VAR_DECL
)
6105 DECL_DEAD_FOR_LOCAL (r
) = 0;
6107 /* A static data member declaration is always marked external
6108 when it is declared in-class, even if an initializer is
6109 present. We mimic the non-template processing here. */
6111 DECL_EXTERNAL (r
) = 1;
6113 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
6114 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6116 register_specialization (r
, gen_tmpl
, argvec
);
6118 register_local_specialization (r
, gen_tmpl
,
6119 current_function_decl
);
6121 TREE_CHAIN (r
) = NULL_TREE
;
6122 if (TREE_CODE (r
) == VAR_DECL
&& TREE_CODE (type
) == VOID_TYPE
)
6123 cp_error_at ("instantiation of `%D' as type void", r
);
6128 my_friendly_abort (0);
6131 /* Restore the file and line information. */
6132 lineno
= saved_lineno
;
6133 input_filename
= saved_filename
;
6138 /* Substitue into the ARG_TYPES of a function type. */
6141 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
6147 tree remaining_arg_types
;
6150 if (!arg_types
|| arg_types
== void_list_node
)
6153 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6154 args
, complain
, in_decl
);
6155 if (remaining_arg_types
== error_mark_node
)
6156 return error_mark_node
;
6158 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6159 if (type
== error_mark_node
)
6160 return error_mark_node
;
6162 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6163 top-level qualifiers as required. */
6164 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6166 /* Note that we do not substitute into default arguments here. The
6167 standard mandates that they be instantiated only when needed,
6168 which is done in build_over_call. */
6169 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6170 remaining_arg_types
);
6174 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6175 *not* handle the exception-specification for FNTYPE, because the
6176 initial substitution of explicitly provided template parameters
6177 during argument deduction forbids substitution into the
6178 exception-specification:
6182 All references in the function type of the function template to the
6183 corresponding template parameters are replaced by the specified tem-
6184 plate argument values. If a substitution in a template parameter or
6185 in the function type of the function template results in an invalid
6186 type, type deduction fails. [Note: The equivalent substitution in
6187 exception specifications is done only when the function is instanti-
6188 ated, at which point a program is ill-formed if the substitution
6189 results in an invalid type.] */
6192 tsubst_function_type (t
, args
, complain
, in_decl
)
6202 /* The TYPE_CONTEXT is not used for function/method types. */
6203 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6205 /* Substitue the return type. */
6206 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6207 if (return_type
== error_mark_node
)
6208 return error_mark_node
;
6210 /* Substitue the argument types. */
6211 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6213 if (arg_types
== error_mark_node
)
6214 return error_mark_node
;
6216 /* Construct a new type node and return it. */
6217 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6218 fntype
= build_function_type (return_type
, arg_types
);
6221 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6222 if (! IS_AGGR_TYPE (r
))
6226 Type deduction may fail for any of the following
6229 -- Attempting to create "pointer to member of T" when T
6230 is not a class type. */
6232 cp_error ("creating pointer to member function of non-class type `%T'",
6234 return error_mark_node
;
6237 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6240 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
6241 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6246 /* Substitute into the PARMS of a call-declarator. */
6249 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
6259 if (!parms
|| parms
== void_list_node
)
6262 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6263 args
, complain
, in_decl
);
6265 /* Figure out the type of this parameter. */
6266 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6268 /* Figure out the default argument as well. Note that we use
6269 tsubst_expr since the default argument is really an expression. */
6270 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6272 /* Chain this parameter on to the front of those we have already
6273 processed. We don't use hash_tree_cons because that function
6274 doesn't check TREE_PARMLIST. */
6275 new_parms
= tree_cons (defarg
, type
, new_parms
);
6277 /* And note that these are parameters. */
6278 TREE_PARMLIST (new_parms
) = 1;
6283 /* Take the tree structure T and replace template parameters used
6284 therein with the argument vector ARGS. IN_DECL is an associated
6285 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6286 An appropriate error message is issued only if COMPLAIN is
6287 non-zero. Note that we must be relatively non-tolerant of
6288 extensions here, in order to preserve conformance; if we allow
6289 substitutions that should not be allowed, we may allow argument
6290 deductions that should not succeed, and therefore report ambiguous
6291 overload situations where there are none. In theory, we could
6292 allow the substitution, but indicate that it should have failed,
6293 and allow our caller to make sure that the right thing happens, but
6294 we don't try to do this yet.
6296 This function is used for dealing with types, decls and the like;
6297 for expressions, use tsubst_expr or tsubst_copy. */
6300 tsubst (t
, args
, complain
, in_decl
)
6307 if (t
== NULL_TREE
|| t
== error_mark_node
6308 || t
== integer_type_node
6309 || t
== void_type_node
6310 || t
== char_type_node
6311 || TREE_CODE (t
) == NAMESPACE_DECL
)
6314 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6315 type
= IDENTIFIER_TYPE_VALUE (t
);
6317 type
= TREE_TYPE (t
);
6318 if (type
== unknown_type_node
)
6319 my_friendly_abort (42);
6321 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6322 && TREE_CODE (t
) != TYPENAME_TYPE
6323 && TREE_CODE (t
) != TEMPLATE_DECL
6324 && TREE_CODE (t
) != IDENTIFIER_NODE
6325 && TREE_CODE (t
) != FUNCTION_TYPE
6326 && TREE_CODE (t
) != METHOD_TYPE
)
6327 type
= tsubst (type
, args
, complain
, in_decl
);
6328 if (type
== error_mark_node
)
6329 return error_mark_node
;
6331 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
6332 return tsubst_decl (t
, args
, type
, in_decl
);
6334 switch (TREE_CODE (t
))
6339 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6340 /*entering_scope=*/0);
6343 case IDENTIFIER_NODE
:
6355 if (t
== integer_type_node
)
6358 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6359 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6363 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6365 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6366 if (max
== error_mark_node
)
6367 return error_mark_node
;
6369 /* See if we can reduce this expression to something simpler. */
6370 max
= maybe_fold_nontype_arg (max
);
6371 if (!processing_template_decl
&& TREE_READONLY_DECL_P (max
))
6372 max
= decl_constant_value (max
);
6374 if (processing_template_decl
6375 /* When providing explicit arguments to a template
6376 function, but leaving some arguments for subsequent
6377 deduction, MAX may be template-dependent even if we're
6378 not PROCESSING_TEMPLATE_DECL. */
6379 || TREE_CODE (max
) != INTEGER_CST
)
6381 tree itype
= make_node (INTEGER_TYPE
);
6382 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6383 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6388 if (integer_zerop (omax
))
6390 /* Still allow an explicit array of size zero. */
6392 pedwarn ("creating array with size zero");
6394 else if (integer_zerop (max
) || INT_CST_LT (max
, integer_zero_node
))
6398 Type deduction may fail for any of the following
6401 Attempting to create an array with a size that is
6402 zero or negative. */
6404 cp_error ("creating array with size `%E'", max
);
6406 return error_mark_node
;
6409 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
));
6410 if (!TREE_PERMANENT (max
) && !allocation_temporary_p ())
6411 max
= copy_to_permanent (max
);
6412 return build_index_type (max
);
6415 case TEMPLATE_TYPE_PARM
:
6416 case TEMPLATE_TEMPLATE_PARM
:
6417 case TEMPLATE_PARM_INDEX
:
6425 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6426 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6428 idx
= TEMPLATE_TYPE_IDX (t
);
6429 level
= TEMPLATE_TYPE_LEVEL (t
);
6433 idx
= TEMPLATE_PARM_IDX (t
);
6434 level
= TEMPLATE_PARM_LEVEL (t
);
6437 if (TREE_VEC_LENGTH (args
) > 0)
6439 tree arg
= NULL_TREE
;
6441 levels
= TMPL_ARGS_DEPTH (args
);
6442 if (level
<= levels
)
6443 arg
= TMPL_ARG (args
, level
, idx
);
6445 if (arg
== error_mark_node
)
6446 return error_mark_node
;
6447 else if (arg
!= NULL_TREE
)
6449 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6451 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
6453 return cp_build_qualified_type_real
6454 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
),
6457 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6459 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6461 /* We are processing a type constructed from
6462 a template template parameter */
6463 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6464 args
, complain
, in_decl
);
6465 if (argvec
== error_mark_node
)
6466 return error_mark_node
;
6468 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6469 we are resolving nested-types in the signature of
6470 a member function templates.
6471 Otherwise ARG is a TEMPLATE_DECL and is the real
6472 template to be instantiated. */
6473 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6474 arg
= TYPE_NAME (arg
);
6476 r
= lookup_template_class (DECL_NAME (arg
),
6479 /*entering_scope=*/0);
6480 return cp_build_qualified_type_real (r
,
6485 /* We are processing a template argument list. */
6493 my_friendly_abort (981018);
6496 /* This can happen during the attempted tsubst'ing in
6497 unify. This means that we don't yet have any information
6498 about the template parameter in question. */
6501 /* If we get here, we must have been looking at a parm for a
6502 more deeply nested template. Make a new version of this
6503 template parameter, but with a lower level. */
6504 switch (TREE_CODE (t
))
6506 case TEMPLATE_TYPE_PARM
:
6507 case TEMPLATE_TEMPLATE_PARM
:
6509 TEMPLATE_TYPE_PARM_INDEX (r
)
6510 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6512 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6513 TYPE_MAIN_VARIANT (r
) = r
;
6514 TYPE_POINTER_TO (r
) = NULL_TREE
;
6515 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6517 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6518 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6520 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6522 if (argvec
== error_mark_node
)
6523 return error_mark_node
;
6525 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6526 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
6530 case TEMPLATE_PARM_INDEX
:
6531 r
= reduce_template_parm_level (t
, type
, levels
);
6535 my_friendly_abort (0);
6543 tree purpose
, value
, chain
, result
;
6545 if (t
== void_list_node
)
6548 purpose
= TREE_PURPOSE (t
);
6551 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6552 if (purpose
== error_mark_node
)
6553 return error_mark_node
;
6555 value
= TREE_VALUE (t
);
6558 value
= tsubst (value
, args
, complain
, in_decl
);
6559 if (value
== error_mark_node
)
6560 return error_mark_node
;
6562 chain
= TREE_CHAIN (t
);
6563 if (chain
&& chain
!= void_type_node
)
6565 chain
= tsubst (chain
, args
, complain
, in_decl
);
6566 if (chain
== error_mark_node
)
6567 return error_mark_node
;
6569 if (purpose
== TREE_PURPOSE (t
)
6570 && value
== TREE_VALUE (t
)
6571 && chain
== TREE_CHAIN (t
))
6573 result
= hash_tree_cons (purpose
, value
, chain
);
6574 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
6578 if (type
!= NULL_TREE
)
6580 /* A binfo node. We always need to make a copy, of the node
6581 itself and of its BINFO_BASETYPES. */
6585 /* Make sure type isn't a typedef copy. */
6586 type
= BINFO_TYPE (TYPE_BINFO (type
));
6588 TREE_TYPE (t
) = complete_type (type
);
6589 if (IS_AGGR_TYPE (type
))
6591 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6592 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6593 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6594 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6599 /* Otherwise, a vector of template arguments. */
6600 return tsubst_template_arg_vector (t
, args
, complain
);
6603 case REFERENCE_TYPE
:
6605 enum tree_code code
;
6607 if (type
== TREE_TYPE (t
))
6610 code
= TREE_CODE (t
);
6615 Type deduction may fail for any of the following
6618 -- Attempting to create a pointer to reference type.
6619 -- Attempting to create a reference to a reference type or
6620 a reference to void. */
6621 if (TREE_CODE (type
) == REFERENCE_TYPE
6622 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6624 static int last_line
= 0;
6625 static char* last_file
= 0;
6627 /* We keep track of the last time we issued this error
6628 message to avoid spewing a ton of messages during a
6629 single bad template instantiation. */
6630 if (complain
&& (last_line
!= lineno
||
6631 last_file
!= input_filename
))
6633 if (TREE_CODE (type
) == VOID_TYPE
)
6634 cp_error ("forming reference to void");
6636 cp_error ("forming %s to reference type `%T'",
6637 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6640 last_file
= input_filename
;
6643 return error_mark_node
;
6645 else if (code
== POINTER_TYPE
)
6646 r
= build_pointer_type (type
);
6648 r
= build_reference_type (type
);
6649 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6651 /* Will this ever be needed for TYPE_..._TO values? */
6657 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6658 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6662 Type deduction may fail for any of the following
6665 -- Attempting to create "pointer to member of T" when T
6666 is not a class type. */
6668 cp_error ("creating pointer to member of non-class type `%T'",
6670 return error_mark_node
;
6672 return build_offset_type (r
, type
);
6680 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6681 if (fntype
== error_mark_node
)
6682 return error_mark_node
;
6684 /* Substitue the exception specification. */
6685 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6688 tree list
= NULL_TREE
;
6690 if (! TREE_VALUE (raises
))
6693 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6695 tree spec
= TREE_VALUE (raises
);
6697 spec
= tsubst (spec
, args
, complain
, in_decl
);
6698 if (spec
== error_mark_node
)
6700 list
= add_exception_specifier (list
, spec
, complain
);
6702 fntype
= build_exception_variant (fntype
, list
);
6708 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6709 if (domain
== error_mark_node
)
6710 return error_mark_node
;
6712 /* As an optimization, we avoid regenerating the array type if
6713 it will obviously be the same as T. */
6714 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6717 /* These checks should match the ones in grokdeclarator.
6721 The deduction may fail for any of the following reasons:
6723 -- Attempting to create an array with an element type that
6724 is void, a function type, or a reference type. */
6725 if (TREE_CODE (type
) == VOID_TYPE
6726 || TREE_CODE (type
) == FUNCTION_TYPE
6727 || TREE_CODE (type
) == REFERENCE_TYPE
)
6730 cp_error ("creating array of `%T'", type
);
6731 return error_mark_node
;
6734 r
= build_cplus_array_type (type
, domain
);
6741 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6743 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6746 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6747 return error_mark_node
;
6749 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6755 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6757 if (e
== error_mark_node
)
6758 return error_mark_node
;
6760 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6765 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6766 in_decl
, /*entering_scope=*/1);
6767 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6770 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6771 return error_mark_node
;
6773 if (!IS_AGGR_TYPE (ctx
))
6776 cp_error ("`%T' is not a class, struct, or union type",
6778 return error_mark_node
;
6780 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6782 /* Normally, make_typename_type does not require that the CTX
6783 have complete type in order to allow things like:
6785 template <class T> struct S { typename S<T>::X Y; };
6787 But, such constructs have already been resolved by this
6788 point, so here CTX really should have complete type, unless
6789 it's a partial instantiation. */
6790 ctx
= complete_type (ctx
);
6791 if (!TYPE_SIZE (ctx
))
6794 incomplete_type_error (NULL_TREE
, ctx
);
6795 return error_mark_node
;
6799 f
= make_typename_type (ctx
, f
);
6800 if (f
== error_mark_node
)
6802 return cp_build_qualified_type_real (f
,
6804 | CP_TYPE_QUALS (t
),
6810 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6812 if (e
== error_mark_node
)
6813 return error_mark_node
;
6814 return make_pointer_declarator (type
, e
);
6819 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6821 if (e
== error_mark_node
)
6822 return error_mark_node
;
6823 return make_reference_declarator (type
, e
);
6828 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6830 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
,
6832 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6833 return error_mark_node
;
6835 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6840 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6842 tree e2
= (tsubst_call_declarator_parms
6843 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
6844 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
6847 if (e1
== error_mark_node
|| e2
== error_mark_node
6848 || e3
== error_mark_node
)
6849 return error_mark_node
;
6851 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
6856 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6858 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6859 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6860 return error_mark_node
;
6862 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6867 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6869 if (e1
== error_mark_node
)
6870 return error_mark_node
;
6872 return TREE_TYPE (e1
);
6876 sorry ("use of `%s' in template",
6877 tree_code_name
[(int) TREE_CODE (t
)]);
6878 return error_mark_node
;
6882 /* Like tsubst, but deals with expressions. This function just replaces
6883 template parms; to finish processing the resultant expression, use
6887 tsubst_copy (t
, args
, complain
, in_decl
)
6892 enum tree_code code
;
6895 if (t
== NULL_TREE
|| t
== error_mark_node
)
6898 code
= TREE_CODE (t
);
6903 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6910 if (!DECL_CONTEXT (t
))
6911 /* This is a global enumeration constant. */
6914 /* Unfortunately, we cannot just call lookup_name here.
6917 template <int I> int f() {
6919 struct S { void g() { E e = a; } };
6922 When we instantiate f<7>::S::g(), say, lookup_name is not
6923 clever enough to find f<7>::a. */
6925 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6926 /*entering_scope=*/0);
6928 for (v
= TYPE_VALUES (enum_type
);
6931 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6932 return TREE_VALUE (v
);
6934 /* We didn't find the name. That should never happen; if
6935 name-lookup found it during preliminary parsing, we
6936 should find it again here during instantiation. */
6937 my_friendly_abort (0);
6942 if (DECL_CONTEXT (t
))
6946 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6947 /*entering_scope=*/1);
6948 if (ctx
!= DECL_CONTEXT (t
))
6949 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6955 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6956 t
= tsubst (t
, args
, complain
, in_decl
);
6961 if (is_member_template (t
))
6962 return tsubst (t
, args
, complain
, in_decl
);
6968 /* We must tsbust into a LOOKUP_EXPR in case the names to
6969 which it refers is a conversion operator; in that case the
6970 name will change. We avoid making unnecessary copies,
6973 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6975 if (id
!= TREE_OPERAND (t
, 0))
6977 r
= build_nt (LOOKUP_EXPR
, id
);
6978 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6986 case REINTERPRET_CAST_EXPR
:
6987 case CONST_CAST_EXPR
:
6988 case STATIC_CAST_EXPR
:
6989 case DYNAMIC_CAST_EXPR
:
6992 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6993 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6996 case PREDECREMENT_EXPR
:
6997 case PREINCREMENT_EXPR
:
6998 case POSTDECREMENT_EXPR
:
6999 case POSTINCREMENT_EXPR
:
7001 case TRUTH_NOT_EXPR
:
7004 case CONVERT_EXPR
: /* Unary + */
7011 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7012 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7017 case TRUNC_DIV_EXPR
:
7019 case FLOOR_DIV_EXPR
:
7020 case ROUND_DIV_EXPR
:
7021 case EXACT_DIV_EXPR
:
7023 case BIT_ANDTC_EXPR
:
7026 case TRUNC_MOD_EXPR
:
7027 case FLOOR_MOD_EXPR
:
7028 case TRUTH_ANDIF_EXPR
:
7029 case TRUTH_ORIF_EXPR
:
7030 case TRUTH_AND_EXPR
:
7051 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7052 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7056 tree fn
= TREE_OPERAND (t
, 0);
7057 if (is_overloaded_fn (fn
))
7058 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
7060 /* Sometimes FN is a LOOKUP_EXPR. */
7061 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
7063 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7068 case METHOD_CALL_EXPR
:
7070 tree name
= TREE_OPERAND (t
, 0);
7071 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
7073 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7075 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7077 else if (TREE_CODE (name
) == SCOPE_REF
7078 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
7080 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7082 name
= TREE_OPERAND (name
, 1);
7083 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7085 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7086 name
= build_nt (SCOPE_REF
, base
, name
);
7089 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7091 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
7093 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
7098 /* This processing should really occur in tsubst_expr, However,
7099 tsubst_expr does not recurse into expressions, since it
7100 assumes that there aren't any statements inside them.
7101 Instead, it simply calls build_expr_from_tree. So, we need
7102 to expand the STMT_EXPR here. */
7103 if (!processing_template_decl
)
7105 tree rtl_expr
= begin_stmt_expr ();
7106 tree block
= tsubst_expr (STMT_EXPR_STMT (t
), args
,
7108 return finish_stmt_expr (rtl_expr
, block
);
7115 case PSEUDO_DTOR_EXPR
:
7118 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7119 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7120 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7127 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7128 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7129 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7130 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7137 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7138 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7139 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7140 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7144 case TEMPLATE_ID_EXPR
:
7146 /* Substituted template arguments */
7147 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7150 if (targs
&& TREE_CODE (targs
) == TREE_LIST
)
7153 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
7154 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
7159 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
7160 TREE_VEC_ELT (targs
, i
)
7161 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
7164 return lookup_template_function
7165 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7170 tree purpose
, value
, chain
;
7172 if (t
== void_list_node
)
7175 purpose
= TREE_PURPOSE (t
);
7177 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7178 value
= TREE_VALUE (t
);
7180 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7181 chain
= TREE_CHAIN (t
);
7182 if (chain
&& chain
!= void_type_node
)
7183 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7184 if (purpose
== TREE_PURPOSE (t
)
7185 && value
== TREE_VALUE (t
)
7186 && chain
== TREE_CHAIN (t
))
7188 return tree_cons (purpose
, value
, chain
);
7195 case TEMPLATE_TYPE_PARM
:
7196 case TEMPLATE_TEMPLATE_PARM
:
7197 case TEMPLATE_PARM_INDEX
:
7199 case REFERENCE_TYPE
:
7206 return tsubst (t
, args
, complain
, in_decl
);
7208 case IDENTIFIER_NODE
:
7209 if (IDENTIFIER_TYPENAME_P (t
)
7210 /* Make sure it's not just a variable named `__opr', for instance,
7211 which can occur in some existing code. */
7213 return build_typename_overload
7214 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7221 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7222 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
7223 complain
, in_decl
));
7224 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7233 /* Like tsubst_copy, but also does semantic processing. */
7236 tsubst_expr (t
, args
, complain
, in_decl
)
7243 if (t
== NULL_TREE
|| t
== error_mark_node
)
7246 if (processing_template_decl
)
7247 return tsubst_copy (t
, args
, complain
, in_decl
);
7249 switch (TREE_CODE (t
))
7252 finish_named_return_value
7253 (TREE_OPERAND (t
, 0),
7254 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, in_decl
));
7255 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7258 case CTOR_INITIALIZER
:
7259 current_member_init_list
7260 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
7261 current_base_init_list
7262 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
7264 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7268 lineno
= STMT_LINENO (t
);
7269 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
7270 args
, complain
, in_decl
));
7274 lineno
= STMT_LINENO (t
);
7275 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7276 args
, complain
, in_decl
));
7281 int i
= suspend_momentary ();
7285 lineno
= STMT_LINENO (t
);
7286 decl
= DECL_STMT_DECL (t
);
7287 if (TREE_CODE (decl
) == LABEL_DECL
)
7288 finish_label_decl (DECL_NAME (decl
));
7291 init
= DECL_INITIAL (decl
);
7292 decl
= tsubst (decl
, args
, complain
, in_decl
);
7293 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7294 DECL_INITIAL (decl
) = init
;
7295 /* By marking the declaration as instantiated, we avoid
7296 trying to instantiate it. Since instantiate_decl can't
7297 handle local variables, and since we've already done
7298 all that needs to be done, that's the right thing to
7300 if (TREE_CODE (decl
) == VAR_DECL
)
7301 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7302 maybe_push_decl (decl
);
7303 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0, 0);
7304 add_decl_stmt (decl
);
7306 resume_momentary (i
);
7313 lineno
= STMT_LINENO (t
);
7315 stmt
= begin_for_stmt ();
7316 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
7317 tsubst_expr (tmp
, args
, complain
, in_decl
);
7318 finish_for_init_stmt (stmt
);
7319 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
7322 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7323 finish_for_expr (tmp
, stmt
);
7324 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7325 finish_for_stmt (tmp
, stmt
);
7331 lineno
= STMT_LINENO (t
);
7332 stmt
= begin_while_stmt ();
7333 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7334 args
, complain
, in_decl
),
7336 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7337 finish_while_stmt (stmt
);
7343 lineno
= STMT_LINENO (t
);
7344 stmt
= begin_do_stmt ();
7345 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7346 finish_do_body (stmt
);
7347 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
7357 lineno
= STMT_LINENO (t
);
7358 stmt
= begin_if_stmt ();
7359 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7360 args
, complain
, in_decl
),
7363 if (tmp
= THEN_CLAUSE (t
), tmp
)
7365 tsubst_expr (tmp
, args
, complain
, in_decl
);
7366 finish_then_clause (stmt
);
7369 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7371 begin_else_clause ();
7372 tsubst_expr (tmp
, args
, complain
, in_decl
);
7373 finish_else_clause (stmt
);
7384 lineno
= STMT_LINENO (t
);
7385 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7386 for (substmt
= COMPOUND_BODY (t
);
7387 substmt
!= NULL_TREE
;
7388 substmt
= TREE_CHAIN (substmt
))
7389 tsubst_expr (substmt
, args
, complain
, in_decl
);
7390 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7395 lineno
= STMT_LINENO (t
);
7396 finish_break_stmt ();
7400 lineno
= STMT_LINENO (t
);
7401 finish_continue_stmt ();
7408 lineno
= STMT_LINENO (t
);
7409 begin_switch_stmt ();
7410 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7411 stmt
= finish_switch_cond (val
);
7412 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7413 finish_switch_stmt (val
, stmt
);
7418 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7419 tsubst_expr (CASE_HIGH (t
), args
, complain
, in_decl
));
7423 lineno
= STMT_LINENO (t
);
7424 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7428 lineno
= STMT_LINENO (t
);
7429 t
= GOTO_DESTINATION (t
);
7430 if (TREE_CODE (t
) != LABEL_DECL
)
7431 /* Computed goto's must be tsubst'd into. On the other hand,
7432 non-computed gotos must not be; the identifier in question
7433 will have no binding. */
7434 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7437 finish_goto_stmt (t
);
7441 lineno
= STMT_LINENO (t
);
7442 finish_asm_stmt (ASM_CV_QUAL (t
),
7443 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7444 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7445 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7446 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7451 lineno
= STMT_LINENO (t
);
7452 stmt
= begin_try_block ();
7453 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7454 finish_try_block (stmt
);
7456 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7461 tree handler
= TRY_HANDLERS (t
);
7462 for (; handler
; handler
= TREE_CHAIN (handler
))
7463 tsubst_expr (handler
, args
, complain
, in_decl
);
7464 finish_handler_sequence (stmt
);
7469 lineno
= STMT_LINENO (t
);
7470 stmt
= begin_handler ();
7471 if (HANDLER_PARMS (t
))
7472 expand_start_catch_block
7473 (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t
)),
7474 args
, complain
, in_decl
));
7476 expand_start_catch_block (NULL_TREE
);
7477 finish_handler_parms (stmt
);
7478 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7479 finish_handler (stmt
);
7483 lineno
= STMT_LINENO (t
);
7485 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
7486 tsubst (t
, args
, complain
, NULL_TREE
);
7490 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7495 /* Instantiate the indicated variable or function template TMPL with
7496 the template arguments in TARG_PTR. */
7499 instantiate_template (tmpl
, targ_ptr
)
7500 tree tmpl
, targ_ptr
;
7506 struct obstack
*old_fmp_obstack
;
7507 extern struct obstack
*function_maybepermanent_obstack
;
7510 if (tmpl
== error_mark_node
)
7511 return error_mark_node
;
7513 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7515 /* Check to see if we already have this specialization. */
7516 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7517 if (spec
!= NULL_TREE
)
7520 if (DECL_TEMPLATE_INFO (tmpl
))
7522 /* The TMPL is a partial instantiation. To get a full set of
7523 arguments we must add the arguments used to perform the
7524 partial instantiation. */
7525 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7527 gen_tmpl
= most_general_template (tmpl
);
7529 /* Check to see if we already have this specialization. */
7530 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7531 if (spec
!= NULL_TREE
)
7537 push_obstacks (&permanent_obstack
, &permanent_obstack
);
7538 old_fmp_obstack
= function_maybepermanent_obstack
;
7539 function_maybepermanent_obstack
= &permanent_obstack
;
7541 len
= DECL_NTPARMS (gen_tmpl
);
7542 inner_args
= innermost_args (targ_ptr
);
7546 tree t
= TREE_VEC_ELT (inner_args
, i
);
7547 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
7549 tree nt
= target_type (t
);
7550 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7552 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7553 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7554 fndecl
= error_mark_node
;
7559 targ_ptr
= copy_to_permanent (targ_ptr
);
7561 /* substitute template parameters */
7562 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, /*complain=*/1, gen_tmpl
);
7563 /* The DECL_TI_TEMPLATE should always be the immediate parent
7564 template, not the most general template. */
7565 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7567 if (flag_external_templates
)
7568 add_pending_template (fndecl
);
7571 function_maybepermanent_obstack
= old_fmp_obstack
;
7577 /* Push the name of the class template into the scope of the instantiation. */
7580 overload_template_name (type
)
7583 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
7586 if (IDENTIFIER_CLASS_VALUE (id
)
7587 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
7590 decl
= build_decl (TYPE_DECL
, id
, type
);
7591 SET_DECL_ARTIFICIAL (decl
);
7592 pushdecl_class_level (decl
);
7595 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7596 arguments that are being used when calling it. TARGS is a vector
7597 into which the deduced template arguments are placed.
7599 Return zero for success, 2 for an incomplete match that doesn't resolve
7600 all the types, and 1 for complete failure. An error message will be
7601 printed only for an incomplete match.
7603 If FN is a conversion operator, RETURN_TYPE is the type desired as
7604 the result of the conversion operator.
7606 TPARMS is a vector of template parameters.
7608 The EXPLICIT_TARGS are explicit template arguments provided via a
7611 The parameter STRICT is one of:
7614 We are deducing arguments for a function call, as in
7618 We are deducing arguments for a conversion function, as in
7622 We are deducing arguments when calculating the partial
7623 ordering between specializations of function or class
7624 templates, as in [temp.func.order] and [temp.class.order],
7625 when doing an explicit instantiation as in [temp.explicit],
7626 when determining an explicit specialization as in
7627 [temp.expl.spec], or when taking the address of a function
7628 template, as in [temp.deduct.funcaddr].
7630 The other arguments are as for type_unification. */
7633 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7635 tree fn
, explicit_targs
, targs
, args
, return_type
;
7636 unification_kind_t strict
;
7642 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7644 fntype
= TREE_TYPE (fn
);
7649 The specified template arguments must match the template
7650 parameters in kind (i.e., type, nontype, template), and there
7651 must not be more arguments than there are parameters;
7652 otherwise type deduction fails.
7654 Nontype arguments must match the types of the corresponding
7655 nontype template parameters, or must be convertible to the
7656 types of the corresponding nontype parameters as specified in
7657 _temp.arg.nontype_, otherwise type deduction fails.
7659 All references in the function type of the function template
7660 to the corresponding template parameters are replaced by the
7661 specified template argument values. If a substitution in a
7662 template parameter or in the function type of the function
7663 template results in an invalid type, type deduction fails. */
7665 tree converted_args
;
7668 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7669 explicit_targs
, NULL_TREE
, /*complain=*/0,
7670 /*require_all_arguments=*/0));
7671 if (converted_args
== error_mark_node
)
7674 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7675 if (fntype
== error_mark_node
)
7678 /* Place the explicitly specified arguments in TARGS. */
7679 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7680 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7683 parms
= TYPE_ARG_TYPES (fntype
);
7685 if (DECL_CONV_FN_P (fn
))
7687 /* This is a template conversion operator. Use the return types
7688 as well as the argument types. We use it instead of 'this', since
7689 we could be comparing conversions from different classes. */
7690 parms
= scratch_tree_cons (NULL_TREE
, TREE_TYPE (fntype
),
7691 TREE_CHAIN (parms
));
7692 args
= scratch_tree_cons (NULL_TREE
, return_type
, TREE_CHAIN (args
));
7695 /* We allow incomplete unification without an error message here
7696 because the standard doesn't seem to explicitly prohibit it. Our
7697 callers must be ready to deal with unification failures in any
7699 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7700 targs
, parms
, args
, /*subr=*/0,
7701 strict
, /*allow_incomplete*/1);
7704 /* All is well so far. Now, check:
7708 When all template arguments have been deduced, all uses of
7709 template parameters in nondeduced contexts are replaced with
7710 the corresponding deduced argument values. If the
7711 substitution results in an invalid type, as described above,
7712 type deduction fails. */
7713 if (tsubst (TREE_TYPE (fn
), targs
, /*complain=*/0, NULL_TREE
)
7720 /* Adjust types before performing type deduction, as described in
7721 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7722 sections are symmetric. PARM is the type of a function parameter
7723 or the return type of the conversion function. ARG is the type of
7724 the argument passed to the call, or the type of the value
7725 intialized with the result of the conversion function. */
7728 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7729 unification_kind_t strict
;
7740 /* Swap PARM and ARG throughout the remainder of this
7741 function; the handling is precisely symmetric since PARM
7742 will initialize ARG rather than vice versa. */
7750 /* There is nothing to do in this case. */
7754 my_friendly_abort (0);
7757 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7759 /* [temp.deduct.call]
7761 If P is not a reference type:
7763 --If A is an array type, the pointer type produced by the
7764 array-to-pointer standard conversion (_conv.array_) is
7765 used in place of A for type deduction; otherwise,
7767 --If A is a function type, the pointer type produced by
7768 the function-to-pointer standard conversion
7769 (_conv.func_) is used in place of A for type deduction;
7772 --If A is a cv-qualified type, the top level
7773 cv-qualifiers of A's type are ignored for type
7775 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7776 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7777 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7778 *arg
= build_pointer_type (*arg
);
7780 *arg
= TYPE_MAIN_VARIANT (*arg
);
7783 /* [temp.deduct.call]
7785 If P is a cv-qualified type, the top level cv-qualifiers
7786 of P's type are ignored for type deduction. If P is a
7787 reference type, the type referred to by P is used for
7789 *parm
= TYPE_MAIN_VARIANT (*parm
);
7790 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7791 *parm
= TREE_TYPE (*parm
);
7794 /* Like type_unfication.
7796 If SUBR is 1, we're being called recursively (to unify the
7797 arguments of a function or method parameter of a function
7801 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7802 strict
, allow_incomplete
)
7803 tree tparms
, targs
, parms
, args
;
7805 unification_kind_t strict
;
7806 int allow_incomplete
;
7810 int ntparms
= TREE_VEC_LENGTH (tparms
);
7813 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7814 my_friendly_assert (parms
== NULL_TREE
7815 || TREE_CODE (parms
) == TREE_LIST
, 290);
7816 /* ARGS could be NULL (via a call from parse.y to
7817 build_x_function_call). */
7819 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7820 my_friendly_assert (ntparms
> 0, 292);
7825 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7829 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7833 sub_strict
= UNIFY_ALLOW_NONE
;
7837 my_friendly_abort (0);
7841 && parms
!= void_list_node
7843 && args
!= void_list_node
)
7845 parm
= TREE_VALUE (parms
);
7846 parms
= TREE_CHAIN (parms
);
7847 arg
= TREE_VALUE (args
);
7848 args
= TREE_CHAIN (args
);
7850 if (arg
== error_mark_node
)
7852 if (arg
== unknown_type_node
)
7853 /* We can't deduce anything from this, but we might get all the
7854 template args from other function args. */
7857 /* Conversions will be performed on a function argument that
7858 corresponds with a function parameter that contains only
7859 non-deducible template parameters and explicitly specified
7860 template parameters. */
7861 if (! uses_template_parms (parm
))
7865 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7866 type
= TREE_TYPE (arg
);
7873 if (strict
== DEDUCE_EXACT
)
7875 if (same_type_p (parm
, type
))
7879 /* It might work; we shouldn't check now, because we might
7880 get into infinite recursion. Overload resolution will
7887 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7889 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7890 if (type_unknown_p (arg
))
7892 /* [temp.deduct.type] A template-argument can be deduced from
7893 a pointer to function or pointer to member function
7894 argument if the set of overloaded functions does not
7895 contain function templates and at most one of a set of
7896 overloaded functions provides a unique match. */
7898 if (resolve_overloaded_unification
7899 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
7904 arg
= TREE_TYPE (arg
);
7908 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7910 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
))
7918 /* Fail if we've reached the end of the parm list, and more args
7919 are present, and the parm list isn't variadic. */
7920 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7922 /* Fail if parms are left and they don't have default values. */
7924 && parms
!= void_list_node
7925 && TREE_PURPOSE (parms
) == NULL_TREE
)
7928 for (i
= 0; i
< ntparms
; i
++)
7929 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7931 if (!allow_incomplete
)
7932 error ("incomplete type unification");
7938 /* Subroutine of type_unification_real. Args are like the variables at the
7939 call site. ARG is an overloaded function (or template-id); we try
7940 deducing template args from each of the overloads, and if only one
7941 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7944 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7946 tree tparms
, targs
, parm
, arg
;
7947 unification_kind_t strict
;
7950 tree tempargs
= copy_node (targs
);
7953 if (TREE_CODE (arg
) == ADDR_EXPR
)
7954 arg
= TREE_OPERAND (arg
, 0);
7956 if (TREE_CODE (arg
) == COMPONENT_REF
)
7957 /* Handle `&x' where `x' is some static or non-static member
7959 arg
= TREE_OPERAND (arg
, 1);
7961 if (TREE_CODE (arg
) == OFFSET_REF
)
7962 arg
= TREE_OPERAND (arg
, 1);
7964 /* Strip baselink information. */
7965 while (TREE_CODE (arg
) == TREE_LIST
)
7966 arg
= TREE_VALUE (arg
);
7968 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7970 /* If we got some explicit template args, we need to plug them into
7971 the affected templates before we try to unify, in case the
7972 explicit args will completely resolve the templates in question. */
7974 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7975 arg
= TREE_OPERAND (arg
, 0);
7977 for (; arg
; arg
= OVL_NEXT (arg
))
7979 tree fn
= OVL_CURRENT (arg
);
7982 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7985 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
7988 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
7990 if (TREE_CODE (elem
) == METHOD_TYPE
)
7991 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7992 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7993 strict
, sub_strict
);
7997 else if (TREE_CODE (arg
) == OVERLOAD
)
7999 for (; arg
; arg
= OVL_NEXT (arg
))
8001 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
8002 if (TREE_CODE (type
) == METHOD_TYPE
)
8003 type
= build_ptrmemfunc_type (build_pointer_type (type
));
8004 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
8006 strict
, sub_strict
);
8010 my_friendly_abort (981006);
8012 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8013 to function or pointer to member function argument if the set of
8014 overloaded functions does not contain function templates and at most
8015 one of a set of overloaded functions provides a unique match.
8017 So if we found multiple possibilities, we return success but don't
8022 int i
= TREE_VEC_LENGTH (targs
);
8024 if (TREE_VEC_ELT (tempargs
, i
))
8025 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
8033 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8034 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8035 different overloads deduce different arguments for a given parm.
8036 Returns 1 on success. */
8039 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
8041 tree tparms
, orig_targs
, targs
, parm
, arg
;
8042 unification_kind_t strict
;
8049 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8050 to function or pointer to member function argument if the set of
8051 overloaded functions does not contain function templates and at most
8052 one of a set of overloaded functions provides a unique match.
8054 So if this is a template, just return success. */
8056 if (uses_template_parms (arg
))
8059 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8061 /* We don't copy orig_targs for this because if we have already deduced
8062 some template args from previous args, unify would complain when we
8063 try to deduce a template parameter for the same argument, even though
8064 there isn't really a conflict. */
8065 nargs
= TREE_VEC_LENGTH (targs
);
8066 tempargs
= make_scratch_vec (nargs
);
8068 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
8071 /* First make sure we didn't deduce anything that conflicts with
8072 explicitly specified args. */
8073 for (i
= nargs
; i
--; )
8075 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8076 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
8078 if (elt
== NULL_TREE
)
8080 else if (uses_template_parms (elt
))
8082 /* Since we're unifying against ourselves, we will fill in template
8083 args used in the function parm list with our own template parms.
8085 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
8088 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
8092 for (i
= nargs
; i
--; )
8094 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8097 TREE_VEC_ELT (targs
, i
) = elt
;
8103 /* PARM is a template class (perhaps with unbound template
8104 parameters). ARG is a fully instantiated type. If ARG can be
8105 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8106 TARGS are as for unify. */
8109 try_class_unification (tparms
, targs
, parm
, arg
)
8118 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
8119 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
8122 /* We need to make a new template argument vector for the call to
8123 unify. If we used TARGS, we'd clutter it up with the result of
8124 the attempted unification, even if this class didn't work out.
8125 We also don't want to commit ourselves to all the unifications
8126 we've already done, since unification is supposed to be done on
8127 an argument-by-argument basis. In other words, consider the
8128 following pathological case:
8130 template <int I, int J, int K>
8133 template <int I, int J>
8134 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8136 template <int I, int J, int K>
8137 void f(S<I, J, K>, S<I, I, I>);
8146 Now, by the time we consider the unification involving `s2', we
8147 already know that we must have `f<0, 0, 0>'. But, even though
8148 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8149 because there are two ways to unify base classes of S<0, 1, 2>
8150 with S<I, I, I>. If we kept the already deduced knowledge, we
8151 would reject the possibility I=1. */
8153 copy_of_targs
= make_temp_vec (TREE_VEC_LENGTH (targs
));
8154 i
= unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
8155 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
);
8158 /* If unification failed, we're done. */
8165 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8166 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
8167 for the base class of ARG that we are currently examining. */
8170 get_template_base_recursive (tparms
, targs
, parm
,
8171 arg_binfo
, rval
, flags
)
8181 tree arg
= BINFO_TYPE (arg_binfo
);
8183 if (!(flags
& GTB_IGNORE_TYPE
))
8185 tree r
= try_class_unification (tparms
, targs
,
8188 /* If there is more than one satisfactory baseclass, then:
8192 If they yield more than one possible deduced A, the type
8196 if (r
&& rval
&& !same_type_p (r
, rval
))
8197 return error_mark_node
;
8202 binfos
= BINFO_BASETYPES (arg_binfo
);
8203 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
8205 /* Process base types. */
8206 for (i
= 0; i
< n_baselinks
; i
++)
8208 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
8211 /* Skip this base, if we've already seen it. */
8212 if (BINFO_MARKED (base_binfo
))
8216 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
8218 /* When searching for a non-virtual, we cannot mark virtually
8221 SET_BINFO_MARKED (base_binfo
);
8223 rval
= get_template_base_recursive (tparms
, targs
,
8227 GTB_VIA_VIRTUAL
* this_virtual
);
8229 /* If we discovered more than one matching base class, we can
8231 if (rval
== error_mark_node
)
8232 return error_mark_node
;
8238 /* Given a template type PARM and a class type ARG, find the unique
8239 base type in ARG that is an instance of PARM. We do not examine
8240 ARG itself; only its base-classes. If there is no appropriate base
8241 class, return NULL_TREE. If there is more than one, return
8242 error_mark_node. PARM may be the type of a partial specialization,
8243 as well as a plain template type. Used by unify. */
8246 get_template_base (tparms
, targs
, parm
, arg
)
8255 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
8257 arg_binfo
= TYPE_BINFO (complete_type (arg
));
8258 rval
= get_template_base_recursive (tparms
, targs
,
8263 /* Since get_template_base_recursive marks the bases classes, we
8264 must unmark them here. */
8265 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
8270 /* Returns the level of DECL, which declares a template parameter. */
8273 template_decl_level (decl
)
8276 switch (TREE_CODE (decl
))
8280 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
8283 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
8286 my_friendly_abort (0);
8291 /* Decide whether ARG can be unified with PARM, considering only the
8292 cv-qualifiers of each type, given STRICT as documented for unify.
8293 Returns non-zero iff the unification is OK on that basis.*/
8296 check_cv_quals_for_unify (strict
, arg
, parm
)
8301 if (!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
8302 && !at_least_as_qualified_p (arg
, parm
))
8305 if (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
8306 && !at_least_as_qualified_p (parm
, arg
))
8312 /* Takes parameters as for type_unification. Returns 0 if the
8313 type deduction suceeds, 1 otherwise. The parameter STRICT is a
8314 bitwise or of the following flags:
8317 Require an exact match between PARM and ARG.
8318 UNIFY_ALLOW_MORE_CV_QUAL:
8319 Allow the deduced ARG to be more cv-qualified than ARG.
8320 UNIFY_ALLOW_LESS_CV_QUAL:
8321 Allow the deduced ARG to be less cv-qualified than ARG.
8322 UNIFY_ALLOW_DERIVED:
8323 Allow the deduced ARG to be a template base class of ARG,
8324 or a pointer to a template base class of the type pointed to by
8326 UNIFY_ALLOW_INTEGER:
8327 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8328 case for more information. */
8331 unify (tparms
, targs
, parm
, arg
, strict
)
8332 tree tparms
, targs
, parm
, arg
;
8339 /* I don't think this will do the right thing with respect to types.
8340 But the only case I've seen it in so far has been array bounds, where
8341 signedness is the only information lost, and I think that will be
8343 while (TREE_CODE (parm
) == NOP_EXPR
)
8344 parm
= TREE_OPERAND (parm
, 0);
8346 if (arg
== error_mark_node
)
8348 if (arg
== unknown_type_node
)
8349 /* We can't deduce anything from this, but we might get all the
8350 template args from other function args. */
8353 /* If PARM uses template parameters, then we can't bail out here,
8354 even if ARG == PARM, since we won't record unifications for the
8355 template parameters. We might need them if we're trying to
8356 figure out which of two things is more specialized. */
8357 if (arg
== parm
&& !uses_template_parms (parm
))
8360 /* Immediately reject some pairs that won't unify because of
8361 cv-qualification mismatches. */
8362 if (TREE_CODE (arg
) == TREE_CODE (parm
)
8363 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
8364 /* We check the cv-qualifiers when unifying with template type
8365 parameters below. We want to allow ARG `const T' to unify with
8366 PARM `T' for example, when computing which of two templates
8367 is more specialized, for example. */
8368 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
8369 && !check_cv_quals_for_unify (strict
, arg
, parm
))
8372 switch (TREE_CODE (parm
))
8375 /* In a type which contains a nested-name-specifier, template
8376 argument values cannot be deduced for template parameters used
8377 within the nested-name-specifier. */
8380 case TEMPLATE_TYPE_PARM
:
8381 case TEMPLATE_TEMPLATE_PARM
:
8382 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8384 if (TEMPLATE_TYPE_LEVEL (parm
)
8385 != template_decl_level (tparm
))
8386 /* The PARM is not one we're trying to unify. Just check
8387 to see if it matches ARG. */
8388 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8389 && same_type_p (parm
, arg
)) ? 0 : 1;
8390 idx
= TEMPLATE_TYPE_IDX (parm
);
8391 targ
= TREE_VEC_ELT (targs
, idx
);
8392 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
8394 /* Check for mixed types and values. */
8395 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
8396 && TREE_CODE (tparm
) != TYPE_DECL
)
8397 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8398 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8401 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
8403 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
8405 /* We arrive here when PARM does not involve template
8408 /* ARG must be constructed from a template class. */
8409 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
8413 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8414 tree parmvec
= TYPE_TI_ARGS (parm
);
8415 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
8417 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
8420 /* The parameter and argument roles have to be switched here
8421 in order to handle default arguments properly. For example,
8422 template<template <class> class TT> void f(TT<int>)
8423 should be able to accept vector<int> which comes from
8424 template <class T, class Allocator = allocator>
8427 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8431 /* Deduce arguments T, i from TT<T> or TT<i>.
8432 We check each element of PARMVEC and ARGVEC individually
8433 rather than the whole TREE_VEC since they can have
8434 different number of elements. */
8436 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8438 tree t
= TREE_VEC_ELT (parmvec
, i
);
8440 if (unify (tparms
, targs
, t
,
8441 TREE_VEC_ELT (argvec
, i
),
8446 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
8451 /* If PARM is `const T' and ARG is only `int', we don't have
8452 a match unless we are allowing additional qualification.
8453 If ARG is `const int' and PARM is just `T' that's OK;
8454 that binds `const int' to `T'. */
8455 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
8459 /* Consider the case where ARG is `const volatile int' and
8460 PARM is `const T'. Then, T should be `volatile int'. */
8462 cp_build_qualified_type_real (arg
,
8464 & ~CP_TYPE_QUALS (parm
),
8466 if (arg
== error_mark_node
)
8470 /* Simple cases: Value already set, does match or doesn't. */
8471 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8476 /* Make sure that ARG is not a variable-sized array. (Note that
8477 were talking about variable-sized arrays (like `int[n]'),
8478 rather than arrays of unknown size (like `int[]').) We'll
8479 get very confused by such a type since the bound of the array
8480 will not be computable in an instantiation. Besides, such
8481 types are not allowed in ISO C++, so we can do as we please
8483 if (TREE_CODE (arg
) == ARRAY_TYPE
8484 && !uses_template_parms (arg
)
8485 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8489 TREE_VEC_ELT (targs
, idx
) = arg
;
8492 case TEMPLATE_PARM_INDEX
:
8493 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8495 if (TEMPLATE_PARM_LEVEL (parm
)
8496 != template_decl_level (tparm
))
8497 /* The PARM is not one we're trying to unify. Just check
8498 to see if it matches ARG. */
8499 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8500 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8502 idx
= TEMPLATE_PARM_IDX (parm
);
8503 targ
= TREE_VEC_ELT (targs
, idx
);
8507 int i
= (cp_tree_equal (targ
, arg
) > 0);
8513 my_friendly_abort (42);
8516 /* [temp.deduct.type] If, in the declaration of a function template
8517 with a non-type template-parameter, the non-type
8518 template-parameter is used in an expression in the function
8519 parameter-list and, if the corresponding template-argument is
8520 deduced, the template-argument type shall match the type of the
8521 template-parameter exactly, except that a template-argument
8522 deduced from an array bound may be of any integral type. */
8523 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
8525 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8526 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8527 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8532 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
8539 if (TREE_CODE (arg
) != POINTER_TYPE
)
8542 /* [temp.deduct.call]
8544 A can be another pointer or pointer to member type that can
8545 be converted to the deduced A via a qualification
8546 conversion (_conv.qual_).
8548 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8549 This will allow for additional cv-qualification of the
8550 pointed-to types if appropriate. In general, this is a bit
8551 too generous; we are only supposed to allow qualification
8552 conversions and this method will allow an ARG of char** and
8553 a deduced ARG of const char**. However, overload
8554 resolution will subsequently invalidate the candidate, so
8555 this is probably OK. */
8556 sub_strict
= strict
;
8558 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
)
8559 /* The derived-to-base conversion only persists through one
8560 level of pointers. */
8561 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
8563 return unify (tparms
, targs
, TREE_TYPE (parm
),
8564 TREE_TYPE (arg
), sub_strict
);
8567 case REFERENCE_TYPE
:
8568 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8570 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8574 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8576 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8577 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8579 if (TYPE_DOMAIN (parm
) != NULL_TREE
8580 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8581 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8583 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8591 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8594 if (TREE_CODE (parm
) == INTEGER_TYPE
8595 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8597 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8598 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8599 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8601 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8602 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8603 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8606 /* We use the TYPE_MAIN_VARIANT since we have already
8607 checked cv-qualification at the top of the
8609 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
8610 TYPE_MAIN_VARIANT (parm
)))
8613 /* As far as unification is concerned, this wins. Later checks
8614 will invalidate it if necessary. */
8617 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8618 /* Type INTEGER_CST can come from ordinary constant template args. */
8620 while (TREE_CODE (arg
) == NOP_EXPR
)
8621 arg
= TREE_OPERAND (arg
, 0);
8623 if (TREE_CODE (arg
) != INTEGER_CST
)
8625 return !tree_int_cst_equal (parm
, arg
);
8630 if (TREE_CODE (arg
) != TREE_VEC
)
8632 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8634 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
8635 if (unify (tparms
, targs
,
8636 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8644 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8647 if (TYPE_PTRMEMFUNC_P (parm
))
8649 if (!TYPE_PTRMEMFUNC_P (arg
))
8652 return unify (tparms
, targs
,
8653 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8654 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
8658 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8662 if (strict
& UNIFY_ALLOW_DERIVED
)
8664 /* First, we try to unify the PARM and ARG directly. */
8665 t
= try_class_unification (tparms
, targs
,
8670 /* Fallback to the special case allowed in
8673 If P is a class, and P has the form
8674 template-id, then A can be a derived class of
8675 the deduced A. Likewise, if P is a pointer to
8676 a class of the form template-id, A can be a
8677 pointer to a derived class pointed to by the
8679 t
= get_template_base (tparms
, targs
,
8682 if (! t
|| t
== error_mark_node
)
8686 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8687 && (CLASSTYPE_TI_TEMPLATE (parm
)
8688 == CLASSTYPE_TI_TEMPLATE (arg
)))
8689 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8690 Then, we should unify `int' and `U'. */
8693 /* There's no chance of unication succeeding. */
8696 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8697 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8699 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
8700 TYPE_MAIN_VARIANT (arg
)))
8706 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8709 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8710 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8712 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8713 TYPE_ARG_TYPES (arg
), 1,
8717 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8719 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8720 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8722 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8726 if (arg
!= decl_constant_value (parm
))
8731 /* Matched cases are handled by the ARG == PARM test above. */
8735 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8737 /* We handle this case specially, since it comes up with
8738 arrays. In particular, something like:
8740 template <int N> void f(int (&x)[N]);
8742 Here, we are trying to unify the range type, which
8743 looks like [0 ... (N - 1)]. */
8745 t1
= TREE_OPERAND (parm
, 0);
8746 t2
= TREE_OPERAND (parm
, 1);
8748 /* Should this be a regular fold? */
8749 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
8753 return unify (tparms
, targs
, t1
, t
, strict
);
8755 /* else fall through */
8758 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8759 /* We're looking at an expression. This can happen with
8763 void foo(S<I>, S<I + 2>);
8765 This is a "nondeduced context":
8769 The nondeduced contexts are:
8771 --A type that is a template-id in which one or more of
8772 the template-arguments is an expression that references
8773 a template-parameter.
8775 In these cases, we assume deduction succeeded, but don't
8776 actually infer any unifications. */
8779 sorry ("use of `%s' in template type unification",
8780 tree_code_name
[(int) TREE_CODE (parm
)]);
8786 /* Called if RESULT is explicitly instantiated, or is a member of an
8787 explicitly instantiated class, or if using -frepo and the
8788 instantiation of RESULT has been assigned to this file. */
8791 mark_decl_instantiated (result
, extern_p
)
8795 if (TREE_CODE (result
) != FUNCTION_DECL
)
8796 /* The TREE_PUBLIC flag for function declarations will have been
8797 set correctly by tsubst. */
8798 TREE_PUBLIC (result
) = 1;
8802 DECL_INTERFACE_KNOWN (result
) = 1;
8803 DECL_NOT_REALLY_EXTERN (result
) = 1;
8805 /* Always make artificials weak. */
8806 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8807 comdat_linkage (result
);
8808 /* For WIN32 we also want to put explicit instantiations in
8809 linkonce sections. */
8810 else if (TREE_PUBLIC (result
))
8811 maybe_make_one_only (result
);
8813 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8814 mark_inline_for_output (result
);
8817 /* Given two function templates PAT1 and PAT2, and explicit template
8818 arguments EXPLICIT_ARGS return:
8820 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8821 -1 if PAT2 is more specialized than PAT1.
8822 0 if neither is more specialized. */
8825 more_specialized (pat1
, pat2
, explicit_args
)
8826 tree pat1
, pat2
, explicit_args
;
8831 targs
= get_bindings_overload (pat1
, DECL_RESULT (pat2
), explicit_args
);
8835 targs
= get_bindings_overload (pat2
, DECL_RESULT (pat1
), explicit_args
);
8842 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8844 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8845 -1 if PAT2 is more specialized than PAT1.
8846 0 if neither is more specialized. */
8849 more_specialized_class (pat1
, pat2
)
8855 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8856 TREE_PURPOSE (pat2
));
8860 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8861 TREE_PURPOSE (pat1
));
8868 /* Return the template arguments that will produce the function signature
8869 DECL from the function template FN, with the explicit template
8870 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8871 also match. Return NULL_TREE if no satisfactory arguments could be
8875 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8876 tree fn
, decl
, explicit_args
;
8879 int ntparms
= DECL_NTPARMS (fn
);
8880 tree targs
= make_scratch_vec (ntparms
);
8882 tree decl_arg_types
;
8885 /* Substitute the explicit template arguments into the type of DECL.
8886 The call to fn_type_unification will handle substitution into the
8888 decl_type
= TREE_TYPE (decl
);
8889 if (explicit_args
&& uses_template_parms (decl_type
))
8892 tree converted_args
;
8894 if (DECL_TEMPLATE_INFO (decl
))
8895 tmpl
= DECL_TI_TEMPLATE (decl
);
8897 /* We can get here for some illegal specializations. */
8901 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
8902 explicit_args
, NULL_TREE
,
8904 /*require_all_arguments=*/0));
8905 if (converted_args
== error_mark_node
)
8908 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
8910 if (decl_type
== error_mark_node
)
8914 /* If FN is a static member function, adjust the type of DECL
8916 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
8917 if (DECL_STATIC_FUNCTION_P (fn
)
8918 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8919 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8921 i
= fn_type_unification (fn
, explicit_args
, targs
,
8923 TREE_TYPE (decl_type
),
8931 /* Check to see that the resulting return type is also OK. */
8932 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
8933 /*complain=*/0, NULL_TREE
);
8935 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
8942 /* For most uses, we want to check the return type. */
8945 get_bindings (fn
, decl
, explicit_args
)
8946 tree fn
, decl
, explicit_args
;
8948 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8951 /* But for more_specialized, we only care about the parameter types. */
8954 get_bindings_overload (fn
, decl
, explicit_args
)
8955 tree fn
, decl
, explicit_args
;
8957 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8960 /* Return the innermost template arguments that, when applied to a
8961 template specialization whose innermost template parameters are
8962 TPARMS, and whose specialization arguments are ARGS, yield the
8965 For example, suppose we have:
8967 template <class T, class U> struct S {};
8968 template <class T> struct S<T*, int> {};
8970 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8971 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8972 int}. The resulting vector will be {double}, indicating that `T'
8973 is bound to `double'. */
8976 get_class_bindings (tparms
, parms
, args
)
8977 tree tparms
, parms
, args
;
8979 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8980 tree vec
= make_temp_vec (ntparms
);
8982 args
= innermost_args (args
);
8984 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
))
8987 for (i
= 0; i
< ntparms
; ++i
)
8988 if (! TREE_VEC_ELT (vec
, i
))
8994 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8995 Pick the most specialized template, and return the corresponding
8996 instantiation, or if there is no corresponding instantiation, the
8997 template itself. EXPLICIT_ARGS is any template arguments explicity
8998 mentioned in a template-id. If there is no most specialized
8999 tempalte, error_mark_node is returned. If there are no templates
9000 at all, NULL_TREE is returned. */
9003 most_specialized_instantiation (instantiations
, explicit_args
)
9004 tree instantiations
;
9010 if (!instantiations
)
9013 champ
= instantiations
;
9014 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
9016 fate
= more_specialized (TREE_VALUE (champ
),
9017 TREE_VALUE (fn
), explicit_args
);
9024 fn
= TREE_CHAIN (fn
);
9026 return error_mark_node
;
9032 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
9034 fate
= more_specialized (TREE_VALUE (champ
),
9035 TREE_VALUE (fn
), explicit_args
);
9037 return error_mark_node
;
9040 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
9043 /* Return the most specialized of the list of templates in FNS that can
9044 produce an instantiation matching DECL, given the explicit template
9045 arguments EXPLICIT_ARGS. */
9048 most_specialized (fns
, decl
, explicit_args
)
9049 tree fns
, decl
, explicit_args
;
9051 tree candidates
= NULL_TREE
;
9054 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
9056 tree candidate
= TREE_VALUE (fn
);
9058 args
= get_bindings (candidate
, decl
, explicit_args
);
9060 candidates
= scratch_tree_cons (NULL_TREE
, candidate
,
9064 return most_specialized_instantiation (candidates
, explicit_args
);
9067 /* If DECL is a specialization of some template, return the most
9068 general such template. For example, given:
9070 template <class T> struct S { template <class U> void f(U); };
9072 if TMPL is `template <class U> void S<int>::f(U)' this will return
9073 the full template. This function will not trace past partial
9074 specializations, however. For example, given in addition:
9076 template <class T> struct S<T*> { template <class U> void f(U); };
9078 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9079 `template <class T> template <class U> S<T*>::f(U)'. */
9082 most_general_template (decl
)
9085 while (DECL_TEMPLATE_INFO (decl
))
9086 decl
= DECL_TI_TEMPLATE (decl
);
9091 /* Return the most specialized of the class template specializations
9092 of TMPL which can produce an instantiation matching ARGS, or
9093 error_mark_node if the choice is ambiguous. */
9096 most_specialized_class (tmpl
, args
)
9100 tree list
= NULL_TREE
;
9105 tmpl
= most_general_template (tmpl
);
9106 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
9109 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
9112 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
9113 TREE_TYPE (list
) = TREE_TYPE (t
);
9123 for (; t
; t
= TREE_CHAIN (t
))
9125 fate
= more_specialized_class (champ
, t
);
9134 return error_mark_node
;
9140 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
9142 fate
= more_specialized_class (champ
, t
);
9144 return error_mark_node
;
9150 /* called from the parser. */
9153 do_decl_instantiation (declspecs
, declarator
, storage
)
9154 tree declspecs
, declarator
, storage
;
9156 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
9157 tree result
= NULL_TREE
;
9161 /* An error ocurred, for which grokdeclarator has already issued
9162 an appropriate message. */
9164 else if (! DECL_LANG_SPECIFIC (decl
))
9166 cp_error ("explicit instantiation of non-template `%#D'", decl
);
9169 else if (TREE_CODE (decl
) == VAR_DECL
)
9171 /* There is an asymmetry here in the way VAR_DECLs and
9172 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9173 the latter, the DECL we get back will be marked as a
9174 template instantiation, and the appropriate
9175 DECL_TEMPLATE_INFO will be set up. This does not happen for
9176 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9177 should handle VAR_DECLs as it currently handles
9179 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
9180 if (result
&& TREE_CODE (result
) != VAR_DECL
)
9182 cp_error ("no matching template for `%D' found", result
);
9186 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
9188 cp_error ("explicit instantiation of `%#D'", decl
);
9194 /* Check for various error cases. Note that if the explicit
9195 instantiation is legal the RESULT will currently be marked as an
9196 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9197 until we get here. */
9199 if (DECL_TEMPLATE_SPECIALIZATION (result
))
9203 No program shall both explicitly instantiate and explicitly
9204 specialize a template. */
9205 cp_pedwarn ("explicit instantiation of `%#D' after", result
);
9206 cp_pedwarn_at ("explicit specialization here", result
);
9209 else if (DECL_EXPLICIT_INSTANTIATION (result
))
9213 No program shall explicitly instantiate any template more
9216 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9217 instantiation was `extern' and the second is not, and EXTERN_P for
9218 the opposite case. If -frepo, chances are we already got marked
9219 as an explicit instantion because of the repo file. */
9220 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
9221 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result
);
9223 /* If we've already instantiated the template, just return now. */
9224 if (DECL_INTERFACE_KNOWN (result
))
9227 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
9229 cp_error ("no matching template for `%D' found", result
);
9232 else if (!DECL_TEMPLATE_INFO (result
))
9234 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
9238 if (flag_external_templates
)
9241 if (storage
== NULL_TREE
)
9243 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9246 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
9250 cp_error ("storage class `%D' applied to template instantiation",
9253 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9254 mark_decl_instantiated (result
, extern_p
);
9255 repo_template_instantiated (result
, extern_p
);
9257 instantiate_decl (result
);
9261 mark_class_instantiated (t
, extern_p
)
9265 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
9266 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
9267 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
9268 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
9269 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
9272 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
9273 rest_of_type_compilation (t
, 1);
9278 do_type_instantiation (t
, storage
)
9285 if (TREE_CODE (t
) == TYPE_DECL
)
9288 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
9290 cp_error ("explicit instantiation of non-template type `%T'", t
);
9296 /* With -fexternal-templates, explicit instantiations are treated the same
9297 as implicit ones. */
9298 if (flag_external_templates
)
9301 if (TYPE_SIZE (t
) == NULL_TREE
)
9303 cp_error ("explicit instantiation of `%#T' before definition of template",
9308 if (storage
!= NULL_TREE
)
9311 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
9312 IDENTIFIER_POINTER (storage
));
9314 if (storage
== ridpointers
[(int) RID_INLINE
])
9316 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9318 else if (storage
== ridpointers
[(int) RID_STATIC
])
9322 cp_error ("storage class `%D' applied to template instantiation",
9328 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
9332 No program shall both explicitly instantiate and explicitly
9333 specialize a template. */
9334 cp_error ("explicit instantiation of `%#T' after", t
);
9335 cp_error_at ("explicit specialization here", t
);
9338 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
9342 No program shall explicitly instantiate any template more
9345 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9346 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9347 are we already got marked as an explicit instantion because of the
9348 repo file. All these cases are OK. */
9349 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
)
9350 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t
);
9352 /* If we've already instantiated the template, just return now. */
9353 if (!CLASSTYPE_INTERFACE_ONLY (t
))
9357 mark_class_instantiated (t
, extern_p
);
9358 repo_template_instantiated (t
, extern_p
);
9366 /* In contrast to implicit instantiation, where only the
9367 declarations, and not the definitions, of members are
9368 instantiated, we have here:
9372 The explicit instantiation of a class template specialization
9373 implies the instantiation of all of its members not
9374 previously explicitly specialized in the translation unit
9375 containing the explicit instantiation.
9377 Of course, we can't instantiate member template classes, since
9378 we don't have any arguments for them. Note that the standard
9379 is unclear on whether the instatiation of the members are
9380 *explicit* instantiations or not. We choose to be generous,
9381 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9382 the explicit instantiation of a class where some of the members
9383 have no definition in the current translation unit. */
9386 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9387 if (TREE_CODE (tmp
) == FUNCTION_DECL
9388 && DECL_TEMPLATE_INSTANTIATION (tmp
))
9390 mark_decl_instantiated (tmp
, extern_p
);
9391 repo_template_instantiated (tmp
, extern_p
);
9393 instantiate_decl (tmp
);
9396 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9397 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
9399 mark_decl_instantiated (tmp
, extern_p
);
9400 repo_template_instantiated (tmp
, extern_p
);
9402 instantiate_decl (tmp
);
9405 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9406 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
9407 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
9408 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
9412 /* Given a function DECL, which is a specialization of TMPL, modify
9413 DECL to be a re-instantiation of TMPL with the same template
9414 arguments. TMPL should be the template into which tsubst'ing
9415 should occur for DECL, not the most general template.
9417 One reason for doing this is a scenario like this:
9420 void f(const T&, int i);
9422 void g() { f(3, 7); }
9425 void f(const T& t, const int i) { }
9427 Note that when the template is first instantiated, with
9428 instantiate_template, the resulting DECL will have no name for the
9429 first parameter, and the wrong type for the second. So, when we go
9430 to instantiate the DECL, we regenerate it. */
9433 regenerate_decl_from_template (decl
, tmpl
)
9443 args
= DECL_TI_ARGS (decl
);
9444 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9446 /* Unregister the specialization so that when we tsubst we will not
9447 just return DECL. We don't have to unregister DECL from TMPL
9448 because if would only be registered there if it were a partial
9449 instantiation of a specialization, which it isn't: it's a full
9451 gen_tmpl
= most_general_template (tmpl
);
9452 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9454 /* If the DECL was not unregistered then something peculiar is
9455 happening: we created a specialization but did not call
9456 register_specialization for it. */
9457 my_friendly_assert (unregistered
, 0);
9459 if (TREE_CODE (decl
) == VAR_DECL
)
9460 /* Make sure that we can see identifiers, and compute access
9461 correctly, for the class members used in the declaration of
9462 this static variable. */
9463 pushclass (DECL_CONTEXT (decl
), 2);
9465 /* Do the substitution to get the new declaration. */
9466 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9468 if (TREE_CODE (decl
) == VAR_DECL
)
9470 /* Set up DECL_INITIAL, since tsubst doesn't. */
9471 DECL_INITIAL (new_decl
) =
9472 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9473 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9474 /* Pop the class context we pushed above. */
9477 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9479 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9481 DECL_INITIAL (new_decl
) = error_mark_node
;
9482 /* And don't complain about a duplicate definition. */
9483 DECL_INITIAL (decl
) = NULL_TREE
;
9486 /* The immediate parent of the new template is still whatever it was
9487 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9488 general template. We also reset the DECL_ASSEMBLER_NAME since
9489 tsubst always calculates the name as if the function in question
9490 were really a template instance, and sometimes, with friend
9491 functions, this is not so. See tsubst_friend_function for
9493 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9494 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9495 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9497 /* Call duplicate decls to merge the old and new declarations. */
9498 duplicate_decls (new_decl
, decl
);
9500 /* Now, re-register the specialization. */
9501 register_specialization (decl
, gen_tmpl
, args
);
9504 /* Produce the definition of D, a _DECL generated from a template. */
9507 instantiate_decl (d
)
9510 tree tmpl
= DECL_TI_TEMPLATE (d
);
9511 tree args
= DECL_TI_ARGS (d
);
9516 int nested
= in_function_p ();
9517 int pattern_defined
;
9519 char *file
= input_filename
;
9521 /* This function should only be used to instantiate templates for
9522 functions and static member variables. */
9523 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9524 || TREE_CODE (d
) == VAR_DECL
, 0);
9526 if (DECL_TEMPLATE_INSTANTIATED (d
))
9527 /* D has already been instantiated. It might seem reasonable to
9528 check whether or not D is an explict instantiation, and, if so,
9529 stop here. But when an explicit instantiation is deferred
9530 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9531 is set, even though we still need to do the instantiation. */
9534 /* If we already have a specialization of this declaration, then
9535 there's no reason to instantiate it. Note that
9536 retrieve_specialization gives us both instantiations and
9537 specializations, so we must explicitly check
9538 DECL_TEMPLATE_SPECIALIZATION. */
9539 gen_tmpl
= most_general_template (tmpl
);
9540 spec
= retrieve_specialization (gen_tmpl
, args
);
9541 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9544 /* This needs to happen before any tsubsting. */
9545 if (! push_tinst_level (d
))
9548 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9549 for the instantiation. This is not always the most general
9550 template. Consider, for example:
9553 struct S { template <class U> void f();
9554 template <> void f<int>(); };
9556 and an instantiation of S<double>::f<int>. We want TD to be the
9557 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9560 /* An instantiation cannot have a definition, so we need a
9561 more general template. */
9562 DECL_TEMPLATE_INSTANTIATION (td
)
9563 /* We must also deal with friend templates. Given:
9565 template <class T> struct S {
9566 template <class U> friend void f() {};
9569 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9570 so far as the language is concerned, but that's still
9571 where we get the pattern for the instantiation from. On
9572 ther hand, if the definition comes outside the class, say:
9574 template <class T> struct S {
9575 template <class U> friend void f();
9577 template <class U> friend void f() {}
9579 we don't need to look any further. That's what the check for
9580 DECL_INITIAL is for. */
9581 || (TREE_CODE (d
) == FUNCTION_DECL
9582 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9583 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
9586 /* The present template, TD, should not be a definition. If it
9587 were a definition, we should be using it! Note that we
9588 cannot restructure the loop to just keep going until we find
9589 a template with a definition, since that might go too far if
9590 a specialization was declared, but not defined. */
9591 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9592 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9595 /* Fetch the more general template. */
9596 td
= DECL_TI_TEMPLATE (td
);
9599 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9601 if (TREE_CODE (d
) == FUNCTION_DECL
)
9602 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
9604 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9606 push_to_top_level ();
9607 lineno
= DECL_SOURCE_LINE (d
);
9608 input_filename
= DECL_SOURCE_FILE (d
);
9610 if (pattern_defined
)
9612 repo_template_used (d
);
9614 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9616 if (flag_alt_external_templates
)
9618 if (interface_unknown
)
9619 warn_if_unknown_interface (d
);
9621 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9623 DECL_INTERFACE_KNOWN (d
) = 1;
9624 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9627 warn_if_unknown_interface (code_pattern
);
9631 import_export_decl (d
);
9634 /* Reject all external templates except inline functions. */
9635 if (DECL_INTERFACE_KNOWN (d
)
9636 && ! DECL_NOT_REALLY_EXTERN (d
)
9637 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9640 if (TREE_CODE (d
) == VAR_DECL
9641 && TREE_READONLY (d
)
9642 && DECL_INITIAL (d
) == NULL_TREE
9643 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
9644 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9645 the variable is a static const initialized in the class body. */;
9646 else if (! pattern_defined
9647 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
9650 /* Defer all templates except inline functions used in another
9651 function. We restore the source position here because it's used
9652 by add_pending_template. */
9654 input_filename
= file
;
9656 if (at_eof
&& !pattern_defined
9657 && DECL_EXPLICIT_INSTANTIATION (d
))
9660 The definition of a non-exported function template, a
9661 non-exported member function template, or a non-exported
9662 member function or static data member of a class template
9663 shall be present in every translation unit in which it is
9664 explicitly instantiated. */
9665 cp_error ("explicit instantiation of `%D' but no definition available",
9668 add_pending_template (d
);
9672 /* We're now committed to instantiating this template. Mark it as
9673 instantiated so that recursive calls to instantiate_decl do not
9674 try to instantiate it again. */
9675 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9677 /* Regenerate the declaration in case the template has been modified
9678 by a subsequent redeclaration. */
9679 regenerate_decl_from_template (d
, td
);
9681 /* We already set the file and line above. Reset them now in case
9682 they changed as a result of calling regenerate_decl_from_template. */
9683 lineno
= DECL_SOURCE_LINE (d
);
9684 input_filename
= DECL_SOURCE_FILE (d
);
9686 if (TREE_CODE (d
) == VAR_DECL
)
9688 DECL_IN_AGGR_P (d
) = 0;
9689 if (DECL_INTERFACE_KNOWN (d
))
9690 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9693 DECL_EXTERNAL (d
) = 1;
9694 DECL_NOT_REALLY_EXTERN (d
) = 1;
9696 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
9698 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9700 extern struct obstack
*saveable_obstack
;
9701 extern struct obstack
*rtl_obstack
;
9702 int saved_expanding_p
= expanding_p
;
9704 /* We're not expanding all the way to RTL here. */
9707 /* Set up context. */
9708 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
9709 store_parm_decls ();
9711 /* Anything we might
9712 want to save is going to have to be saved forever. Note that
9713 we don't want to save all kinds of temporary clutter that
9714 might end up on the temporary obstack so we don't want to
9715 call push_permanent_obstack. */
9716 push_obstacks_nochange ();
9717 saveable_obstack
= &permanent_obstack
;
9718 /* We only need this because of the cases where we generate
9719 RTL_EXPRs. We should really be generating RTL_EXPRs until
9720 final expansion time; when that is fixed, this can go. */
9721 rtl_obstack
= &permanent_obstack
;
9722 /* Substitute into the body of the function. */
9723 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
9724 /*complain=*/1, tmpl
);
9728 finish_function (lineno
, 0, nested
);
9729 expanding_p
= saved_expanding_p
;
9731 /* Now, generate RTL for the function. */
9737 input_filename
= file
;
9739 pop_from_top_level ();
9745 /* Run through the list of templates that we wish we could
9746 instantiate, and instantiate any we can. */
9749 instantiate_pending_templates ()
9752 int instantiated_something
= 0;
9759 t
= &pending_templates
;
9762 tree srcloc
= TREE_PURPOSE (*t
);
9763 tree instantiation
= TREE_VALUE (*t
);
9765 input_filename
= SRCLOC_FILE (srcloc
);
9766 lineno
= SRCLOC_LINE (srcloc
);
9768 if (TREE_CODE_CLASS (TREE_CODE (instantiation
)) == 't')
9772 if (!TYPE_SIZE (instantiation
))
9774 instantiate_class_template (instantiation
);
9775 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
9776 for (fn
= TYPE_METHODS (instantiation
);
9778 fn
= TREE_CHAIN (fn
))
9779 if (! DECL_ARTIFICIAL (fn
))
9780 instantiate_decl (fn
);
9781 if (TYPE_SIZE (instantiation
))
9783 instantiated_something
= 1;
9788 if (TYPE_SIZE (instantiation
))
9789 /* If INSTANTIATION has been instantiated, then we don't
9790 need to consider it again in the future. */
9791 *t
= TREE_CHAIN (*t
);
9793 t
= &TREE_CHAIN (*t
);
9797 if (DECL_TEMPLATE_INSTANTIATION (instantiation
)
9798 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
9800 instantiation
= instantiate_decl (instantiation
);
9801 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
9803 instantiated_something
= 1;
9808 if (!DECL_TEMPLATE_INSTANTIATION (instantiation
)
9809 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
9810 /* If INSTANTIATION has been instantiated, then we don't
9811 need to consider it again in the future. */
9812 *t
= TREE_CHAIN (*t
);
9814 t
= &TREE_CHAIN (*t
);
9819 /* Go through the things that are template instantiations if we are
9820 using guiding declarations. */
9821 t
= &maybe_templates
;
9828 fn
= TREE_VALUE (*t
);
9830 if (DECL_INITIAL (fn
))
9831 /* If the FN is already defined, then it was either already
9832 instantiated or, even though guiding declarations were
9833 allowed, a non-template definition was provided. */
9837 template = TREE_PURPOSE (*t
);
9838 args
= get_bindings (template, fn
, NULL_TREE
);
9839 fn
= instantiate_template (template, args
);
9840 instantiate_decl (fn
);
9844 /* Remove this entry from the chain. */
9845 *t
= TREE_CHAIN (*t
);
9847 maybe_template_tail
= t
;
9851 return instantiated_something
;
9854 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9855 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9856 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9859 tsubst_expr_values (t
, argvec
)
9862 tree first
= NULL_TREE
;
9865 for (; t
; t
= TREE_CHAIN (t
))
9867 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
9868 /*complain=*/1, NULL_TREE
);
9869 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
9871 *p
= build_tree_list (pur
, val
);
9872 p
= &TREE_CHAIN (*p
);
9881 last_tree
= TREE_CHAIN (last_tree
) = t
;
9888 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
9889 last_tree
= NULL_TREE
;
9896 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
9898 last_tree
= TREE_VALUE (saved_trees
);
9899 saved_trees
= TREE_CHAIN (saved_trees
);
9902 /* D is an undefined function declaration in the presence of templates with
9903 the same name, listed in FNS. If one of them can produce D as an
9904 instantiation, remember this so we can instantiate it at EOF if D has
9905 not been defined by that time. */
9908 add_maybe_template (d
, fns
)
9913 if (DECL_MAYBE_TEMPLATE (d
))
9916 t
= most_specialized (fns
, d
, NULL_TREE
);
9919 if (t
== error_mark_node
)
9921 cp_error ("ambiguous template instantiation for `%D'", d
);
9925 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
9926 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
9927 DECL_MAYBE_TEMPLATE (d
) = 1;
9930 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9933 set_current_access_from_decl (decl
)
9936 if (TREE_PRIVATE (decl
))
9937 current_access_specifier
= access_private_node
;
9938 else if (TREE_PROTECTED (decl
))
9939 current_access_specifier
= access_protected_node
;
9941 current_access_specifier
= access_public_node
;
9944 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9945 is the instantiation (which should have been created with
9946 start_enum) and ARGS are the template arguments to use. */
9949 tsubst_enum (tag
, newtag
, args
)
9956 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9961 /* Note that in a template enum, the TREE_VALUE is the
9962 CONST_DECL, not the corresponding INTEGER_CST. */
9963 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9964 args
, /*complain=*/1,
9967 /* Give this enumeration constant the correct access. */
9968 set_current_access_from_decl (TREE_VALUE (e
));
9970 /* Actually build the enumerator itself. */
9971 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9973 /* We save the enumerators we have built so far in the
9974 TYPE_VALUES so that if the enumeration constants for
9975 subsequent enumerators involve those for previous ones,
9976 tsubst_copy will be able to find them. */
9977 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
9978 TYPE_VALUES (newtag
) = elt
;
9981 finish_enum (newtag
);
9984 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9985 is either an instantiation or specialization of a template
9989 set_mangled_name_for_template_decl (decl
)
9992 tree saved_namespace
;
9993 tree context
= NULL_TREE
;
10002 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
10003 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
10005 /* The names of template functions must be mangled so as to indicate
10006 what template is being specialized with what template arguments.
10007 For example, each of the following three functions must get
10008 different mangled names:
10011 template <> void f<7>(int);
10012 template <> void f<8>(int); */
10014 targs
= DECL_TI_ARGS (decl
);
10015 if (uses_template_parms (targs
))
10016 /* This DECL is for a partial instantiation. There's no need to
10017 mangle the name of such an entity. */
10020 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10021 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
10022 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
10024 /* There should be as many levels of arguments as there are levels
10026 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
10028 /* We now compute the PARMS and RET_TYPE to give to
10029 build_decl_overload_real. The PARMS and RET_TYPE are the
10030 parameter and return types of the template, after all but the
10031 innermost template arguments have been substituted, not the
10032 parameter and return types of the function DECL. For example,
10035 template <class T> T f(T);
10037 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10038 A more subtle example is:
10040 template <class T> struct S { template <class U> void f(T, U); }
10042 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10043 {int, U}. Thus, the args that we want to subsitute into the
10044 return and parameter type for the function are those in TARGS,
10045 with the innermost level omitted. */
10046 fn_type
= TREE_TYPE (tmpl
);
10047 if (DECL_STATIC_FUNCTION_P (decl
))
10048 context
= DECL_CLASS_CONTEXT (decl
);
10050 if (parm_depth
== 1)
10051 /* No substitution is necessary. */
10058 /* Replace the innermost level of the TARGS with NULL_TREEs to
10059 let tsubst know not to subsitute for those parameters. */
10060 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
10061 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
10062 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
10063 TMPL_ARGS_LEVEL (targs
, i
));
10064 SET_TMPL_ARGS_LEVEL (partial_args
,
10065 TMPL_ARGS_DEPTH (targs
),
10066 make_temp_vec (DECL_NTPARMS (tmpl
)));
10068 /* Now, do the (partial) substitution to figure out the
10069 appropriate function type. */
10070 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
10071 if (DECL_STATIC_FUNCTION_P (decl
))
10072 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
10074 /* Substitute into the template parameters to obtain the real
10075 innermost set of parameters. This step is important if the
10076 innermost set of template parameters contains value
10077 parameters whose types depend on outer template parameters. */
10078 TREE_VEC_LENGTH (partial_args
)--;
10079 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
10082 /* Now, get the innermost parameters and arguments, and figure out
10083 the parameter and return types. */
10084 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
10085 targs
= innermost_args (targs
);
10086 ret_type
= TREE_TYPE (fn_type
);
10087 parm_types
= TYPE_ARG_TYPES (fn_type
);
10089 /* For a static member function, we generate a fake `this' pointer,
10090 for the purposes of mangling. This indicates of which class the
10091 function is a member. Because of:
10095 There shall not be a static and a nonstatic member function
10096 with the same name and the same parameter types
10098 we don't have to worry that this will result in a clash with a
10099 non-static member function. */
10100 if (DECL_STATIC_FUNCTION_P (decl
))
10101 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
10103 /* There should be the same number of template parameters as
10104 template arguments. */
10105 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
10108 /* If the template is in a namespace, we need to put that into the
10109 mangled name. Unfortunately, build_decl_overload_real does not
10110 get the decl to mangle, so it relies on the current
10111 namespace. Therefore, we set that here temporarily. */
10112 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
10113 saved_namespace
= current_namespace
;
10114 current_namespace
= CP_DECL_CONTEXT (decl
);
10116 /* Actually set the DCL_ASSEMBLER_NAME. */
10117 DECL_ASSEMBLER_NAME (decl
)
10118 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
10120 DECL_FUNCTION_MEMBER_P (decl
)
10121 + DECL_CONSTRUCTOR_P (decl
));
10123 /* Restore the previously active namespace. */
10124 current_namespace
= saved_namespace
;