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 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the
58 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
59 the instantiate request occurred; the TREE_VALUE is a either a DECL
60 (for a function or static data member), or a TYPE (for a class)
61 indicating what we are hoping to instantiate. */
62 static tree pending_templates
;
63 static tree
*template_tail
= &pending_templates
;
65 static tree maybe_templates
;
66 static tree
*maybe_template_tail
= &maybe_templates
;
68 int processing_template_parmlist
;
69 static int template_header_count
;
71 static tree saved_trees
;
72 static varray_type inline_parm_levels
;
73 static size_t inline_parm_levels_used
;
75 #define obstack_chunk_alloc xmalloc
76 #define obstack_chunk_free free
78 #define UNIFY_ALLOW_NONE 0
79 #define UNIFY_ALLOW_MORE_CV_QUAL 1
80 #define UNIFY_ALLOW_LESS_CV_QUAL 2
81 #define UNIFY_ALLOW_DERIVED 4
82 #define UNIFY_ALLOW_INTEGER 8
84 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
85 virtual, or a base class of a virtual
87 #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
88 type with the desired type. */
90 static int resolve_overloaded_unification
PROTO((tree
, tree
, tree
, tree
,
91 unification_kind_t
, int));
92 static int try_one_overload
PROTO((tree
, tree
, tree
, tree
, tree
,
93 unification_kind_t
, int));
94 static int unify
PROTO((tree
, tree
, tree
, tree
, int));
95 static void add_pending_template
PROTO((tree
));
96 static int push_tinst_level
PROTO((tree
));
97 static tree classtype_mangled_name
PROTO((tree
));
98 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
99 static tree tsubst_expr_values
PROTO((tree
, tree
));
100 static int list_eq
PROTO((tree
, tree
));
101 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
102 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int));
103 static void tsubst_enum
PROTO((tree
, tree
, tree
));
104 static tree add_to_template_args
PROTO((tree
, tree
));
105 static tree add_outermost_template_args
PROTO((tree
, tree
));
106 static void maybe_adjust_types_for_deduction
PROTO((unification_kind_t
, tree
*,
108 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
109 int, unification_kind_t
, int));
110 static void note_template_header
PROTO((int));
111 static tree maybe_fold_nontype_arg
PROTO((tree
));
112 static tree convert_nontype_argument
PROTO((tree
, tree
));
113 static tree convert_template_argument
PROTO ((tree
, tree
, tree
, int,
115 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
116 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
117 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
118 static int inline_needs_template_parms
PROTO((tree
));
119 static void push_inline_template_parms_recursive
PROTO((tree
, int));
120 static tree retrieve_specialization
PROTO((tree
, tree
));
121 static tree retrieve_local_specialization
PROTO((tree
, tree
));
122 static tree register_specialization
PROTO((tree
, tree
, tree
));
123 static tree register_local_specialization
PROTO((tree
, tree
, tree
));
124 static int unregister_specialization
PROTO((tree
, tree
));
125 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
126 static tree build_template_decl
PROTO((tree
, tree
));
127 static int mark_template_parm
PROTO((tree
, void *));
128 static tree tsubst_friend_function
PROTO((tree
, tree
));
129 static tree tsubst_friend_class
PROTO((tree
, tree
));
130 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
131 static int template_decl_level
PROTO((tree
));
132 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
133 static int check_cv_quals_for_unify
PROTO((int, tree
, tree
));
134 static tree tsubst_template_arg_vector
PROTO((tree
, tree
, int));
135 static tree tsubst_template_parms
PROTO((tree
, tree
, int));
136 static void regenerate_decl_from_template
PROTO((tree
, tree
));
137 static tree most_specialized
PROTO((tree
, tree
, tree
));
138 static tree most_specialized_class
PROTO((tree
, tree
));
139 static tree most_general_template
PROTO((tree
));
140 static void set_mangled_name_for_template_decl
PROTO((tree
));
141 static int template_class_depth_real
PROTO((tree
, int));
142 static tree tsubst_aggr_type
PROTO((tree
, tree
, int, tree
, int));
143 static tree tsubst_decl
PROTO((tree
, tree
, tree
, tree
));
144 static tree tsubst_arg_types
PROTO((tree
, tree
, int, tree
));
145 static tree tsubst_function_type
PROTO((tree
, tree
, int, tree
));
146 static void check_specialization_scope
PROTO((void));
147 static tree process_partial_specialization
PROTO((tree
));
148 static void set_current_access_from_decl
PROTO((tree
));
149 static void check_default_tmpl_args
PROTO((tree
, tree
, int, int));
150 static tree tsubst_call_declarator_parms
PROTO((tree
, tree
, int, tree
));
151 static tree get_template_base_recursive
PROTO((tree
, tree
,
152 tree
, tree
, tree
, int));
153 static tree get_template_base
PROTO((tree
, tree
, tree
, tree
));
154 static tree try_class_unification
PROTO((tree
, tree
, tree
, tree
));
155 static int coerce_template_template_parms
PROTO((tree
, tree
, int,
157 static tree determine_specialization
PROTO((tree
, tree
, tree
*, int));
158 static int template_args_equal
PROTO((tree
, tree
));
159 static void print_template_context
PROTO((int));
160 static void tsubst_default_arguments
PROTO((tree
));
162 /* We use TREE_VECs to hold template arguments. If there is only one
163 level of template arguments, then the TREE_VEC contains the
164 arguments directly. If there is more than one level of template
165 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
166 containing the template arguments for a single level. The first
167 entry in the outer TREE_VEC is the outermost level of template
168 parameters; the last is the innermost.
170 It is incorrect to ever form a template argument vector containing
171 only one level of arguments, but which is a TREE_VEC containing as
172 its only entry the TREE_VEC for that level. */
174 /* Non-zero if the template arguments is actually a vector of vectors,
175 rather than just a vector. */
176 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
178 && TREE_CODE (NODE) == TREE_VEC \
179 && TREE_VEC_LENGTH (NODE) > 0 \
180 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
181 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
183 /* The depth of a template argument vector. When called directly by
184 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
185 template arguments. In fact, we may even see NULL_TREE if there
186 are no template arguments. In both of those cases, there is only
187 one level of template arguments. */
188 #define TMPL_ARGS_DEPTH(NODE) \
189 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
191 /* The LEVELth level of the template ARGS. Note that template
192 parameter levels are indexed from 1, not from 0. */
193 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
194 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
195 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
197 /* Set the LEVELth level of the template ARGS to VAL. This macro does
198 not work with single-level argument vectors. */
199 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
200 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
202 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
203 #define TMPL_ARG(ARGS, LEVEL, IDX) \
204 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
206 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
207 macro does not work with single-level argument vectors. */
208 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
209 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
211 /* Given a single level of template arguments in NODE, return the
212 number of arguments. */
213 #define NUM_TMPL_ARGS(NODE) \
214 ((NODE) == NULL_TREE ? 0 \
215 : (TREE_CODE (NODE) == TREE_VEC \
216 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
218 /* The number of levels of template parameters given by NODE. */
219 #define TMPL_PARMS_DEPTH(NODE) \
220 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
222 /* Called once to initialize pt.c. */
227 ggc_add_tree_root (&pending_templates
, 1);
228 ggc_add_tree_root (&maybe_templates
, 1);
229 ggc_add_tree_root (&saved_trees
, 1);
232 /* Do any processing required when DECL (a member template declaration
233 using TEMPLATE_PARAMETERS as its innermost parameter list) is
234 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
235 it is a specialization, in which case the DECL itself is returned. */
238 finish_member_template_decl (decl
)
241 if (decl
== NULL_TREE
|| decl
== void_type_node
)
243 else if (decl
== error_mark_node
)
244 /* By returning NULL_TREE, the parser will just ignore this
245 declaration. We have already issued the error. */
247 else if (TREE_CODE (decl
) == TREE_LIST
)
249 /* Assume that the class is the only declspec. */
250 decl
= TREE_VALUE (decl
);
251 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
252 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
254 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
255 check_member_template (tmpl
);
260 else if (DECL_TEMPLATE_INFO (decl
))
262 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
264 check_member_template (DECL_TI_TEMPLATE (decl
));
265 return DECL_TI_TEMPLATE (decl
);
271 cp_error ("invalid member template declaration `%D'", decl
);
273 return error_mark_node
;
276 /* Returns the template nesting level of the indicated class TYPE.
286 A<T>::B<U> has depth two, while A<T> has depth one.
287 Both A<T>::B<int> and A<int>::B<U> have depth one, if
288 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
291 This function is guaranteed to return 0 if passed NULL_TREE so
292 that, for example, `template_class_depth (current_class_type)' is
296 template_class_depth_real (type
, count_specializations
)
298 int count_specializations
;
303 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
304 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
305 ? DECL_REAL_CONTEXT (type
) : TYPE_CONTEXT (type
))
307 if (TREE_CODE (type
) != FUNCTION_DECL
)
309 if (CLASSTYPE_TEMPLATE_INFO (type
)
310 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
311 && ((count_specializations
312 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
313 || uses_template_parms (CLASSTYPE_TI_ARGS (type
))))
318 if (DECL_TEMPLATE_INFO (type
)
319 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type
))
320 && ((count_specializations
321 && DECL_TEMPLATE_SPECIALIZATION (type
))
322 || uses_template_parms (DECL_TI_ARGS (type
))))
330 /* Returns the template nesting level of the indicated class TYPE.
331 Like template_class_depth_real, but instantiations do not count in
335 template_class_depth (type
)
338 return template_class_depth_real (type
, /*count_specializations=*/0);
341 /* Returns 1 if processing DECL as part of do_pending_inlines
342 needs us to push template parms. */
345 inline_needs_template_parms (decl
)
348 if (! DECL_TEMPLATE_INFO (decl
))
351 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
352 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
355 /* Subroutine of maybe_begin_member_template_processing.
356 Push the template parms in PARMS, starting from LEVELS steps into the
357 chain, and ending at the beginning, since template parms are listed
361 push_inline_template_parms_recursive (parmlist
, levels
)
365 tree parms
= TREE_VALUE (parmlist
);
369 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
371 ++processing_template_decl
;
372 current_template_parms
373 = tree_cons (build_int_2 (0, processing_template_decl
),
374 parms
, current_template_parms
);
375 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
378 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
380 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
381 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
383 switch (TREE_CODE (parm
))
392 /* Make a CONST_DECL as is done in process_template_parm.
393 It is ugly that we recreate this here; the original
394 version built in process_template_parm is no longer
396 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
398 SET_DECL_ARTIFICIAL (decl
);
399 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
400 DECL_TEMPLATE_PARM_P (decl
) = 1;
406 my_friendly_abort (0);
411 /* Restore the template parameter context for a member template or
412 a friend template defined in a class definition. */
415 maybe_begin_member_template_processing (decl
)
421 if (inline_needs_template_parms (decl
))
423 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
424 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
426 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
429 parms
= TREE_CHAIN (parms
);
432 push_inline_template_parms_recursive (parms
, levels
);
435 /* Remember how many levels of template parameters we pushed so that
436 we can pop them later. */
437 if (!inline_parm_levels
)
438 VARRAY_INT_INIT (inline_parm_levels
, 4, "inline_parm_levels");
439 if (inline_parm_levels_used
== inline_parm_levels
->num_elements
)
440 VARRAY_GROW (inline_parm_levels
, 2 * inline_parm_levels_used
);
441 VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
) = levels
;
442 ++inline_parm_levels_used
;
445 /* Undo the effects of begin_member_template_processing. */
448 maybe_end_member_template_processing ()
452 if (!inline_parm_levels_used
)
455 --inline_parm_levels_used
;
457 i
< VARRAY_INT (inline_parm_levels
, inline_parm_levels_used
);
460 --processing_template_decl
;
461 current_template_parms
= TREE_CHAIN (current_template_parms
);
466 /* Returns non-zero iff T is a member template function. We must be
469 template <class T> class C { void f(); }
471 Here, f is a template function, and a member, but not a member
472 template. This function does not concern itself with the origin of
473 T, only its present state. So if we have
475 template <class T> class C { template <class U> void f(U); }
477 then neither C<int>::f<char> nor C<T>::f<double> is considered
478 to be a member template. But, `template <class U> void
479 C<int>::f(U)' is considered a member template. */
482 is_member_template (t
)
485 if (!DECL_FUNCTION_TEMPLATE_P (t
))
486 /* Anything that isn't a function or a template function is
487 certainly not a member template. */
490 /* A local class can't have member templates. */
491 if (hack_decl_function_context (t
))
494 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
495 /* If there are more levels of template parameters than
496 there are template classes surrounding the declaration,
497 then we have a member template. */
498 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
499 template_class_depth (DECL_CLASS_CONTEXT (t
))));
503 /* Returns non-zero iff T is a member template class. See
504 is_member_template for a description of what precisely constitutes
505 a member template. */
508 is_member_template_class (t
)
511 if (!DECL_CLASS_TEMPLATE_P (t
))
512 /* Anything that isn't a class template, is certainly not a member
516 if (!DECL_CLASS_SCOPE_P (t
))
517 /* Anything whose context isn't a class type is surely not a
521 /* If there are more levels of template parameters than there are
522 template classes surrounding the declaration, then we have a
524 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t
)) >
525 template_class_depth (DECL_CONTEXT (t
)));
529 /* Return a new template argument vector which contains all of ARGS,
530 but has as its innermost set of arguments the EXTRA_ARGS. The
531 resulting vector will be built on a temporary obstack, and so must
532 be explicitly copied to the permanent obstack, if required. */
535 add_to_template_args (args
, extra_args
)
544 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
545 new_args
= make_temp_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
547 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
548 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
550 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
551 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
556 /* Like add_to_template_args, but only the outermost ARGS are added to
557 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
558 (EXTRA_ARGS) levels are added. This function is used to combine
559 the template arguments from a partial instantiation with the
560 template arguments used to attain the full instantiation from the
561 partial instantiation. */
564 add_outermost_template_args (args
, extra_args
)
570 /* If there are more levels of EXTRA_ARGS than there are ARGS,
571 something very fishy is going on. */
572 my_friendly_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
),
575 /* If *all* the new arguments will be the EXTRA_ARGS, just return
577 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
580 /* For the moment, we make ARGS look like it contains fewer levels. */
581 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
583 new_args
= add_to_template_args (args
, extra_args
);
585 /* Now, we restore ARGS to its full dimensions. */
586 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
591 /* We've got a template header coming up; push to a new level for storing
595 begin_template_parm_list ()
597 /* We use a non-tag-transparent scope here, which causes pushtag to
598 put tags in this scope, rather than in the enclosing class or
599 namespace scope. This is the right thing, since we want
600 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
601 global template class, push_template_decl handles putting the
602 TEMPLATE_DECL into top-level scope. For a nested template class,
605 template <class T> struct S1 {
606 template <class T> struct S2 {};
609 pushtag contains special code to call pushdecl_with_scope on the
610 TEMPLATE_DECL for S2. */
612 declare_pseudo_global_level ();
613 ++processing_template_decl
;
614 ++processing_template_parmlist
;
615 note_template_header (0);
618 /* This routine is called when a specialization is declared. If it is
619 illegal to declare a specialization here, an error is reported. */
622 check_specialization_scope ()
624 tree scope
= current_scope ();
628 An explicit specialization shall be declared in the namespace of
629 which the template is a member, or, for member templates, in the
630 namespace of which the enclosing class or enclosing class
631 template is a member. An explicit specialization of a member
632 function, member class or static data member of a class template
633 shall be declared in the namespace of which the class template
635 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
636 cp_error ("explicit specialization in non-namespace scope `%D'",
641 In an explicit specialization declaration for a member of a class
642 template or a member template that appears in namespace scope,
643 the member template and some of its enclosing class templates may
644 remain unspecialized, except that the declaration shall not
645 explicitly specialize a class member template if its enclosing
646 class templates are not explicitly specialized as well. */
647 if (current_template_parms
)
648 cp_error ("enclosing class templates are not explicitly specialized");
651 /* We've just seen template <>. */
654 begin_specialization ()
656 note_template_header (1);
657 check_specialization_scope ();
660 /* Called at then end of processing a declaration preceeded by
664 end_specialization ()
666 reset_specialization ();
669 /* Any template <>'s that we have seen thus far are not referring to a
670 function specialization. */
673 reset_specialization ()
675 processing_specialization
= 0;
676 template_header_count
= 0;
679 /* We've just seen a template header. If SPECIALIZATION is non-zero,
680 it was of the form template <>. */
683 note_template_header (specialization
)
686 processing_specialization
= specialization
;
687 template_header_count
++;
690 /* We're beginning an explicit instantiation. */
693 begin_explicit_instantiation ()
695 ++processing_explicit_instantiation
;
700 end_explicit_instantiation ()
702 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
703 --processing_explicit_instantiation
;
706 /* The TYPE is being declared. If it is a template type, that means it
707 is a partial specialization. Do appropriate error-checking. */
710 maybe_process_partial_specialization (type
)
713 if (IS_AGGR_TYPE (type
) && CLASSTYPE_USE_TEMPLATE (type
))
715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
716 && TYPE_SIZE (type
) == NULL_TREE
)
718 if (current_namespace
719 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type
)))
721 cp_pedwarn ("specializing `%#T' in different namespace", type
);
722 cp_pedwarn_at (" from definition of `%#D'",
723 CLASSTYPE_TI_TEMPLATE (type
));
725 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
726 if (processing_template_decl
)
727 push_template_decl (TYPE_MAIN_DECL (type
));
729 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
730 cp_error ("specialization of `%T' after instantiation", type
);
732 else if (processing_specialization
)
733 cp_error ("explicit specialization of non-template `%T'", type
);
736 /* Retrieve the specialization (in the sense of [temp.spec] - a
737 specialization is either an instantiation or an explicit
738 specialization) of TMPL for the given template ARGS. If there is
739 no such specialization, return NULL_TREE. The ARGS are a vector of
740 arguments, or a vector of vectors of arguments, in the case of
741 templates with more than one level of parameters. */
744 retrieve_specialization (tmpl
, args
)
750 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
752 /* There should be as many levels of arguments as there are
753 levels of parameters. */
754 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
755 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
758 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
761 if (comp_template_args (TREE_PURPOSE (s
), args
))
762 return TREE_VALUE (s
);
767 /* Like retrieve_speciailization, but for local declarations. FN is
768 the function in which we are looking for an instantiation. */
771 retrieve_local_specialization (tmpl
, fn
)
775 tree s
= purpose_member (fn
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
776 return s
? TREE_VALUE (s
) : NULL_TREE
;
779 /* Returns non-zero iff DECL is a specialization of TMPL. */
782 is_specialization_of (decl
, tmpl
)
788 if (TREE_CODE (decl
) == FUNCTION_DECL
)
792 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
798 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 0);
800 for (t
= TREE_TYPE (decl
);
802 t
= CLASSTYPE_USE_TEMPLATE (t
)
803 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
804 if (same_type_p (TYPE_MAIN_VARIANT (t
),
805 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl
))))
812 /* Register the specialization SPEC as a specialization of TMPL with
813 the indicated ARGS. Returns SPEC, or an equivalent prior
814 declaration, if available. */
817 register_specialization (spec
, tmpl
, args
)
824 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
826 if (TREE_CODE (spec
) == FUNCTION_DECL
827 && uses_template_parms (DECL_TI_ARGS (spec
)))
828 /* This is the FUNCTION_DECL for a partial instantiation. Don't
829 register it; we want the corresponding TEMPLATE_DECL instead.
830 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
831 the more obvious `uses_template_parms (spec)' to avoid problems
832 with default function arguments. In particular, given
835 template <class T> void f(T t1, T t = T())
837 the default argument expression is not substituted for in an
838 instantiation unless and until it is actually needed. */
841 /* There should be as many levels of arguments as there are
842 levels of parameters. */
843 my_friendly_assert (TMPL_ARGS_DEPTH (args
)
844 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)),
847 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
851 tree fn
= TREE_VALUE (s
);
853 /* We can sometimes try to re-register a specialization that we've
854 already got. In particular, regenerate_decl_from_template
855 calls duplicate_decls which will update the specialization
856 list. But, we'll still get called again here anyhow. It's
857 more convenient to simply allow this than to try to prevent it. */
860 else if (comp_template_args (TREE_PURPOSE (s
), args
))
862 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
864 if (DECL_TEMPLATE_INSTANTIATION (fn
))
867 || DECL_EXPLICIT_INSTANTIATION (fn
))
869 cp_error ("specialization of %D after instantiation",
875 /* This situation should occur only if the first
876 specialization is an implicit instantiation,
877 the second is an explicit specialization, and
878 the implicit instantiation has not yet been
879 used. That situation can occur if we have
880 implicitly instantiated a member function and
881 then specialized it later.
883 We can also wind up here if a friend
884 declaration that looked like an instantiation
885 turns out to be a specialization:
887 template <class T> void foo(T);
888 class S { friend void foo<>(int) };
889 template <> void foo(int);
891 We transform the existing DECL in place so that
892 any pointers to it become pointers to the
895 If there was a definition for the template, but
896 not for the specialization, we want this to
897 look as if there is no definition, and vice
899 DECL_INITIAL (fn
) = NULL_TREE
;
900 duplicate_decls (spec
, fn
);
905 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
907 duplicate_decls (spec
, fn
);
914 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
915 = tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
920 /* Unregister the specialization SPEC as a specialization of TMPL.
921 Returns nonzero if the SPEC was listed as a specialization of
925 unregister_specialization (spec
, tmpl
)
931 for (s
= &DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
933 s
= &TREE_CHAIN (*s
))
934 if (TREE_VALUE (*s
) == spec
)
936 *s
= TREE_CHAIN (*s
);
943 /* Like register_specialization, but for local declarations. FN is
944 the function in which we are registering SPEC, an instantiation of
948 register_local_specialization (spec
, tmpl
, fn
)
953 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
954 = tree_cons (fn
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
959 /* Print the list of candidate FNS in an error message. */
962 print_candidates (fns
)
967 const char *str
= "candidates are:";
969 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
973 for (f
= TREE_VALUE (fn
); f
; f
= OVL_NEXT (f
))
974 cp_error_at ("%s %+#D", str
, OVL_CURRENT (f
));
979 /* Returns the template (one of the functions given by TEMPLATE_ID)
980 which can be specialized to match the indicated DECL with the
981 explicit template args given in TEMPLATE_ID. The DECL may be
982 NULL_TREE if none is available. In that case, the functions in
983 TEMPLATE_ID are non-members.
985 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
986 specialization of a member template.
988 The template args (those explicitly specified and those deduced)
989 are output in a newly created vector *TARGS_OUT.
991 If it is impossible to determine the result, an error message is
992 issued. The error_mark_node is returned to indicate failure. */
995 determine_specialization (template_id
, decl
, targs_out
,
996 need_member_template
)
1000 int need_member_template
;
1004 tree explicit_targs
;
1005 tree candidates
= NULL_TREE
;
1006 tree templates
= NULL_TREE
;
1008 *targs_out
= NULL_TREE
;
1010 if (template_id
== error_mark_node
)
1011 return error_mark_node
;
1013 fns
= TREE_OPERAND (template_id
, 0);
1014 explicit_targs
= TREE_OPERAND (template_id
, 1);
1016 if (fns
== error_mark_node
)
1017 return error_mark_node
;
1019 /* Check for baselinks. */
1020 if (BASELINK_P (fns
))
1021 fns
= TREE_VALUE (fns
);
1023 if (!is_overloaded_fn (fns
))
1025 cp_error ("`%D' is not a function template", fns
);
1026 return error_mark_node
;
1029 for (; fns
; fns
= OVL_NEXT (fns
))
1033 tree fn
= OVL_CURRENT (fns
);
1035 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1036 /* DECL might be a specialization of FN. */
1038 else if (need_member_template
)
1039 /* FN is an ordinary member function, and we need a
1040 specialization of a member template. */
1042 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1043 /* We can get IDENTIFIER_NODEs here in certain erroneous
1046 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1047 /* This is just an ordinary non-member function. Nothing can
1048 be a specialization of that. */
1052 tree decl_arg_types
;
1054 /* This is an ordinary member function. However, since
1055 we're here, we can assume it's enclosing class is a
1056 template class. For example,
1058 template <typename T> struct S { void f(); };
1059 template <> void S<int>::f() {}
1061 Here, S<int>::f is a non-template, but S<int> is a
1062 template class. If FN has the same type as DECL, we
1063 might be in business. */
1064 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1065 TREE_TYPE (TREE_TYPE (fn
))))
1066 /* The return types differ. */
1069 /* Adjust the type of DECL in case FN is a static member. */
1070 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1071 if (DECL_STATIC_FUNCTION_P (fn
)
1072 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1073 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1075 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1078 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1083 /* See whether this function might be a specialization of this
1085 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1088 /* We cannot deduce template arguments that when used to
1089 specialize TMPL will produce DECL. */
1092 /* Save this template, and the arguments deduced. */
1093 templates
= tree_cons (targs
, tmpl
, templates
);
1096 if (templates
&& TREE_CHAIN (templates
))
1102 It is possible for a specialization with a given function
1103 signature to be instantiated from more than one function
1104 template. In such cases, explicit specification of the
1105 template arguments must be used to uniquely identify the
1106 function template specialization being specialized.
1108 Note that here, there's no suggestion that we're supposed to
1109 determine which of the candidate templates is most
1110 specialized. However, we, also have:
1114 Partial ordering of overloaded function template
1115 declarations is used in the following contexts to select
1116 the function template to which a function template
1117 specialization refers:
1119 -- when an explicit specialization refers to a function
1122 So, we do use the partial ordering rules, at least for now.
1123 This extension can only serve to make illegal programs legal,
1124 so it's safe. And, there is strong anecdotal evidence that
1125 the committee intended the partial ordering rules to apply;
1126 the EDG front-end has that behavior, and John Spicer claims
1127 that the committee simply forgot to delete the wording in
1128 [temp.expl.spec]. */
1129 tree tmpl
= most_specialized (templates
, decl
, explicit_targs
);
1130 if (tmpl
&& tmpl
!= error_mark_node
)
1132 targs
= get_bindings (tmpl
, decl
, explicit_targs
);
1133 templates
= tree_cons (targs
, tmpl
, NULL_TREE
);
1137 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1139 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1141 return error_mark_node
;
1143 else if ((templates
&& TREE_CHAIN (templates
))
1144 || (candidates
&& TREE_CHAIN (candidates
))
1145 || (templates
&& candidates
))
1147 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1149 chainon (candidates
, templates
);
1150 print_candidates (candidates
);
1151 return error_mark_node
;
1154 /* We have one, and exactly one, match. */
1157 /* It was a specialization of an ordinary member function in a
1159 *targs_out
= copy_node (DECL_TI_ARGS (TREE_VALUE (candidates
)));
1160 return DECL_TI_TEMPLATE (TREE_VALUE (candidates
));
1163 /* It was a specialization of a template. */
1164 targs
= DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates
)));
1165 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1167 *targs_out
= copy_node (targs
);
1168 SET_TMPL_ARGS_LEVEL (*targs_out
,
1169 TMPL_ARGS_DEPTH (*targs_out
),
1170 TREE_PURPOSE (templates
));
1173 *targs_out
= TREE_PURPOSE (templates
);
1174 return TREE_VALUE (templates
);
1177 /* Check to see if the function just declared, as indicated in
1178 DECLARATOR, and in DECL, is a specialization of a function
1179 template. We may also discover that the declaration is an explicit
1180 instantiation at this point.
1182 Returns DECL, or an equivalent declaration that should be used
1183 instead if all goes well. Issues an error message if something is
1184 amiss. Returns error_mark_node if the error is not easily
1187 FLAGS is a bitmask consisting of the following flags:
1189 2: The function has a definition.
1190 4: The function is a friend.
1192 The TEMPLATE_COUNT is the number of references to qualifying
1193 template classes that appeared in the name of the function. For
1196 template <class T> struct S { void f(); };
1199 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1200 classes are not counted in the TEMPLATE_COUNT, so that in
1202 template <class T> struct S {};
1203 template <> struct S<int> { void f(); }
1204 template <> void S<int>::f();
1206 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1207 illegal; there should be no template <>.)
1209 If the function is a specialization, it is marked as such via
1210 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1211 is set up correctly, and it is added to the list of specializations
1212 for that template. */
1215 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
1221 int have_def
= flags
& 2;
1222 int is_friend
= flags
& 4;
1223 int specialization
= 0;
1224 int explicit_instantiation
= 0;
1225 int member_specialization
= 0;
1227 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1228 tree dname
= DECL_NAME (decl
);
1230 if (processing_specialization
)
1232 /* The last template header was of the form template <>. */
1234 if (template_header_count
> template_count
)
1236 /* There were more template headers than qualifying template
1238 if (template_header_count
- template_count
> 1)
1239 /* There shouldn't be that many template parameter lists.
1240 There can be at most one parameter list for every
1241 qualifying class, plus one for the function itself. */
1242 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
1244 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1246 member_specialization
= 1;
1250 else if (template_header_count
== template_count
)
1252 /* The counts are equal. So, this might be a
1253 specialization, but it is not a specialization of a
1254 member template. It might be something like
1256 template <class T> struct S {
1260 void S<int>::f(int i) {} */
1262 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1266 /* This cannot be an explicit specialization. There are not
1267 enough headers for all of the qualifying classes. For
1268 example, we might have:
1271 void S<int>::T<char>::f();
1273 But, we're missing another template <>. */
1274 cp_error("too few template parameter lists in declaration of `%D'", decl
);
1278 else if (processing_explicit_instantiation
)
1280 if (template_header_count
)
1281 cp_error ("template parameter list used in explicit instantiation");
1284 cp_error ("definition provided for explicit instantiation");
1286 explicit_instantiation
= 1;
1288 else if (ctype
!= NULL_TREE
1289 && !TYPE_BEING_DEFINED (ctype
)
1290 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
)
1293 /* This case catches outdated code that looks like this:
1295 template <class T> struct S { void f(); };
1296 void S<int>::f() {} // Missing template <>
1298 We disable this check when the type is being defined to
1299 avoid complaining about default compiler-generated
1300 constructors, destructors, and assignment operators.
1301 Since the type is an instantiation, not a specialization,
1302 these are the only functions that can be defined before
1303 the class is complete. */
1306 template <class T> void S<int>::f() {}
1308 if (template_header_count
)
1310 cp_error ("template parameters specified in specialization");
1316 ("explicit specialization not preceded by `template <>'");
1318 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1320 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1323 /* This could be something like:
1325 template <class T> void f(T);
1326 class S { friend void f<>(int); } */
1330 /* This case handles bogus declarations like template <>
1331 template <class T> void f<int>(); */
1333 cp_error ("template-id `%D' in declaration of primary template",
1339 if (specialization
|| member_specialization
)
1341 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1342 for (; t
; t
= TREE_CHAIN (t
))
1343 if (TREE_PURPOSE (t
))
1346 ("default argument specified in explicit specialization");
1349 if (current_lang_name
== lang_name_c
)
1350 cp_error ("template specialization with C linkage");
1353 if (specialization
|| member_specialization
|| explicit_instantiation
)
1355 tree tmpl
= NULL_TREE
;
1356 tree targs
= NULL_TREE
;
1358 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1359 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1363 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
1366 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
1371 lookup_template_function (fns
, NULL_TREE
);
1374 if (declarator
== error_mark_node
)
1375 return error_mark_node
;
1377 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1379 if (!explicit_instantiation
)
1380 /* A specialization in class scope. This is illegal,
1381 but the error will already have been flagged by
1382 check_specialization_scope. */
1383 return error_mark_node
;
1386 /* It's not legal to write an explicit instantiation in
1389 class C { template void f(); }
1391 This case is caught by the parser. However, on
1394 template class C { void f(); };
1396 (which is illegal) we can get here. The error will be
1403 else if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
1405 /* A friend declaration. We can't do much, because we don't
1406 know what this resolves to, yet. */
1407 my_friendly_assert (is_friend
!= 0, 0);
1408 my_friendly_assert (!explicit_instantiation
, 0);
1409 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1412 else if (ctype
!= NULL_TREE
1413 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1416 /* Find the list of functions in ctype that have the same
1417 name as the declared function. */
1418 tree name
= TREE_OPERAND (declarator
, 0);
1419 tree fns
= NULL_TREE
;
1422 if (name
== constructor_name (ctype
)
1423 || name
== constructor_name_full (ctype
))
1425 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1427 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1428 : !TYPE_HAS_DESTRUCTOR (ctype
))
1430 /* From [temp.expl.spec]:
1432 If such an explicit specialization for the member
1433 of a class template names an implicitly-declared
1434 special member function (clause _special_), the
1435 program is ill-formed.
1437 Similar language is found in [temp.explicit]. */
1438 cp_error ("specialization of implicitly-declared special member function");
1439 return error_mark_node
;
1442 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1445 if (!IDENTIFIER_TYPENAME_P (name
))
1447 idx
= lookup_fnfields_1 (ctype
, name
);
1449 fns
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype
), idx
);
1455 /* For a type-conversion operator, we cannot do a
1456 name-based lookup. We might be looking for `operator
1457 int' which will be a specialization of `operator T'.
1458 So, we find *all* the conversion operators, and then
1459 select from them. */
1462 methods
= CLASSTYPE_METHOD_VEC (ctype
);
1464 for (idx
= 2; idx
< TREE_VEC_LENGTH (methods
); ++idx
)
1466 tree ovl
= TREE_VEC_ELT (methods
, idx
);
1468 if (!ovl
|| !DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
1469 /* There are no more conversion functions. */
1472 /* Glue all these conversion functions together
1473 with those we already have. */
1474 for (; ovl
; ovl
= OVL_NEXT (ovl
))
1475 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
1479 if (fns
== NULL_TREE
)
1481 cp_error ("no member function `%D' declared in `%T'",
1483 return error_mark_node
;
1486 TREE_OPERAND (declarator
, 0) = fns
;
1489 /* Figure out what exactly is being specialized at this point.
1490 Note that for an explicit instantiation, even one for a
1491 member function, we cannot tell apriori whether the
1492 instantiation is for a member template, or just a member
1493 function of a template class. Even if a member template is
1494 being instantiated, the member template arguments may be
1495 elided if they can be deduced from the rest of the
1497 tmpl
= determine_specialization (declarator
, decl
,
1499 member_specialization
);
1501 if (!tmpl
|| tmpl
== error_mark_node
)
1502 /* We couldn't figure out what this declaration was
1504 return error_mark_node
;
1507 tree gen_tmpl
= most_general_template (tmpl
);
1509 if (explicit_instantiation
)
1511 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1512 is done by do_decl_instantiation later. */
1514 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
1515 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
1517 if (arg_depth
> parm_depth
)
1519 /* If TMPL is not the most general template (for
1520 example, if TMPL is a friend template that is
1521 injected into namespace scope), then there will
1522 be too many levels fo TARGS. Remove some of them
1527 new_targs
= make_temp_vec (parm_depth
);
1528 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
1529 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
1530 = TREE_VEC_ELT (targs
, i
);
1534 decl
= instantiate_template (tmpl
, targs
);
1538 /* If we though that the DECL was a member function, but it
1539 turns out to be specializing a static member function,
1540 make DECL a static member function as well. */
1541 if (DECL_STATIC_FUNCTION_P (tmpl
)
1542 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1544 revert_static_member_fn (&decl
, 0, 0);
1545 last_function_parms
= TREE_CHAIN (last_function_parms
);
1548 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1549 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
1551 /* Mangle the function name appropriately. Note that we do
1552 not mangle specializations of non-template member
1553 functions of template classes, e.g. with
1555 template <class T> struct S { void f(); }
1557 and given the specialization
1559 template <> void S<int>::f() {}
1561 we do not mangle S<int>::f() here. That's because it's
1562 just an ordinary member function and doesn't need special
1563 treatment. We do this here so that the ordinary,
1564 non-template, name-mangling algorith will not be used
1566 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1567 && name_mangling_version
>= 1)
1568 set_mangled_name_for_template_decl (decl
);
1570 if (is_friend
&& !have_def
)
1571 /* This is not really a declaration of a specialization.
1572 It's just the name of an instantiation. But, it's not
1573 a request for an instantiation, either. */
1574 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1576 /* Register this specialization so that we can find it
1578 decl
= register_specialization (decl
, gen_tmpl
, targs
);
1585 /* TYPE is being declared. Verify that the use of template headers
1586 and such is reasonable. Issue error messages if not. */
1589 maybe_check_template_type (type
)
1592 if (template_header_count
)
1594 /* We are in the scope of some `template <...>' header. */
1597 = template_class_depth_real (TYPE_CONTEXT (type
),
1598 /*count_specializations=*/1);
1600 if (template_header_count
<= context_depth
)
1601 /* This is OK; the template headers are for the context. We
1602 are actually too lenient here; like
1603 check_explicit_specialization we should consider the number
1604 of template types included in the actual declaration. For
1607 template <class T> struct S {
1608 template <class U> template <class V>
1614 template <class T> struct S {
1615 template <class U> struct I;
1618 template <class T> template <class U.
1623 else if (template_header_count
> context_depth
+ 1)
1624 /* There are two many template parameter lists. */
1625 cp_error ("too many template parameter lists in declaration of `%T'", type
);
1629 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1630 parameters. These are represented in the same format used for
1631 DECL_TEMPLATE_PARMS. */
1633 int comp_template_parms (parms1
, parms2
)
1640 if (parms1
== parms2
)
1643 for (p1
= parms1
, p2
= parms2
;
1644 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1645 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1647 tree t1
= TREE_VALUE (p1
);
1648 tree t2
= TREE_VALUE (p2
);
1651 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1652 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1654 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1657 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1659 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1660 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1662 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1665 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1667 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
1672 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1673 /* One set of parameters has more parameters lists than the
1680 /* Complain if DECL shadows a template parameter.
1682 [temp.local]: A template-parameter shall not be redeclared within its
1683 scope (including nested scopes). */
1686 check_template_shadow (decl
)
1691 /* If we're not in a template, we can't possibly shadow a template
1693 if (!current_template_parms
)
1696 /* Figure out what we're shadowing. */
1697 if (TREE_CODE (decl
) == OVERLOAD
)
1698 decl
= OVL_CURRENT (decl
);
1699 olddecl
= IDENTIFIER_VALUE (DECL_NAME (decl
));
1701 /* If there's no previous binding for this name, we're not shadowing
1702 anything, let alone a template parameter. */
1706 /* If we're not shadowing a template parameter, we're done. Note
1707 that OLDDECL might be an OVERLOAD (or perhaps even an
1708 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1710 if (TREE_CODE_CLASS (TREE_CODE (olddecl
)) != 'd'
1711 || !DECL_TEMPLATE_PARM_P (olddecl
))
1714 /* We check for decl != olddecl to avoid bogus errors for using a
1715 name inside a class. We check TPFI to avoid duplicate errors for
1716 inline member templates. */
1718 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
1721 cp_error_at ("declaration of `%#D'", decl
);
1722 cp_error_at (" shadows template parm `%#D'", olddecl
);
1725 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1726 ORIG_LEVEL, DECL, and TYPE. */
1729 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1736 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1737 TEMPLATE_PARM_IDX (t
) = index
;
1738 TEMPLATE_PARM_LEVEL (t
) = level
;
1739 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1740 TEMPLATE_PARM_DECL (t
) = decl
;
1741 TREE_TYPE (t
) = type
;
1746 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1747 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1748 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1749 new one is created. */
1752 reduce_template_parm_level (index
, type
, levels
)
1757 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1758 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1759 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1762 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1763 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1766 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1767 TEMPLATE_PARM_LEVEL (index
) - levels
,
1768 TEMPLATE_PARM_ORIG_LEVEL (index
),
1770 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1772 /* Template template parameters need this. */
1773 DECL_TEMPLATE_PARMS (decl
)
1774 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1777 return TEMPLATE_PARM_DESCENDANTS (index
);
1780 /* Process information from new template parameter NEXT and append it to the
1781 LIST being built. */
1784 process_template_parm (list
, next
)
1793 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1794 defval
= TREE_PURPOSE (parm
);
1795 parm
= TREE_VALUE (parm
);
1796 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1800 tree p
= TREE_VALUE (tree_last (list
));
1802 if (TREE_CODE (p
) == TYPE_DECL
)
1803 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1804 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1805 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1807 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1815 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1816 /* is a const-param */
1817 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1818 PARM
, 0, NULL_TREE
);
1822 The top-level cv-qualifiers on the template-parameter are
1823 ignored when determining its type. */
1824 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
1826 /* A template parameter is not modifiable. */
1827 TREE_READONLY (parm
) = 1;
1828 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1829 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
1830 && TREE_CODE (TREE_TYPE (parm
)) != TYPENAME_TYPE
)
1832 cp_error ("`%#T' is not a valid type for a template constant parameter",
1834 if (DECL_NAME (parm
) == NULL_TREE
)
1835 error (" a template type parameter must begin with `class' or `typename'");
1836 TREE_TYPE (parm
) = void_type_node
;
1839 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1840 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1841 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1843 if (TREE_PERMANENT (parm
) == 0)
1845 parm
= copy_node (parm
);
1846 TREE_PERMANENT (parm
) = 1;
1848 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1849 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1850 = build_template_parm_index (idx
, processing_template_decl
,
1851 processing_template_decl
,
1852 decl
, TREE_TYPE (parm
));
1857 parm
= TREE_VALUE (parm
);
1859 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1861 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1862 /* This is for distinguishing between real templates and template
1863 template parameters */
1864 TREE_TYPE (parm
) = t
;
1865 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1870 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1871 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1872 decl
= build_decl (TYPE_DECL
, parm
, t
);
1875 TYPE_NAME (t
) = decl
;
1876 TYPE_STUB_DECL (t
) = decl
;
1878 TEMPLATE_TYPE_PARM_INDEX (t
)
1879 = build_template_parm_index (idx
, processing_template_decl
,
1880 processing_template_decl
,
1881 decl
, TREE_TYPE (parm
));
1883 SET_DECL_ARTIFICIAL (decl
);
1884 DECL_TEMPLATE_PARM_P (decl
) = 1;
1886 parm
= build_tree_list (defval
, parm
);
1887 return chainon (list
, parm
);
1890 /* The end of a template parameter list has been reached. Process the
1891 tree list into a parameter vector, converting each parameter into a more
1892 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1896 end_template_parm_list (parms
)
1901 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1903 current_template_parms
1904 = tree_cons (build_int_2 (0, processing_template_decl
),
1905 saved_parmlist
, current_template_parms
);
1907 for (parm
= parms
, nparms
= 0;
1909 parm
= TREE_CHAIN (parm
), nparms
++)
1910 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1912 --processing_template_parmlist
;
1914 return saved_parmlist
;
1917 /* end_template_decl is called after a template declaration is seen. */
1920 end_template_decl ()
1922 reset_specialization ();
1924 if (! processing_template_decl
)
1927 /* This matches the pushlevel in begin_template_parm_list. */
1930 --processing_template_decl
;
1931 current_template_parms
= TREE_CHAIN (current_template_parms
);
1932 (void) get_pending_sizes (); /* Why? */
1935 /* Given a template argument vector containing the template PARMS.
1936 The innermost PARMS are given first. */
1939 current_template_args ()
1942 tree args
= NULL_TREE
;
1943 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
1946 /* If there is only one level of template parameters, we do not
1947 create a TREE_VEC of TREE_VECs. Instead, we return a single
1948 TREE_VEC containing the arguments. */
1950 args
= make_tree_vec (length
);
1952 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
1954 tree a
= copy_node (TREE_VALUE (header
));
1957 TREE_TYPE (a
) = NULL_TREE
;
1958 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
1960 tree t
= TREE_VEC_ELT (a
, i
);
1962 /* T will be a list if we are called from within a
1963 begin/end_template_parm_list pair, but a vector directly
1964 if within a begin/end_member_template_processing pair. */
1965 if (TREE_CODE (t
) == TREE_LIST
)
1969 if (TREE_CODE (t
) == TYPE_DECL
1970 || TREE_CODE (t
) == TEMPLATE_DECL
)
1973 t
= DECL_INITIAL (t
);
1974 TREE_VEC_ELT (a
, i
) = t
;
1979 TREE_VEC_ELT (args
, --l
) = a
;
1987 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1988 template PARMS. Used by push_template_decl below. */
1991 build_template_decl (decl
, parms
)
1995 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1996 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1997 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1998 if (DECL_LANG_SPECIFIC (decl
))
2000 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
2001 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
2002 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
2003 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2009 struct template_parm_data
2011 /* The level of the template parameters we are currently
2015 /* The index of the specialization argument we are currently
2019 /* An array whose size is the number of template parameters. The
2020 elements are non-zero if the parameter has been used in any one
2021 of the arguments processed so far. */
2024 /* An array whose size is the number of template arguments. The
2025 elements are non-zero if the argument makes use of template
2026 parameters of this level. */
2027 int* arg_uses_template_parms
;
2030 /* Subroutine of push_template_decl used to see if each template
2031 parameter in a partial specialization is used in the explicit
2032 argument list. If T is of the LEVEL given in DATA (which is
2033 treated as a template_parm_data*), then DATA->PARMS is marked
2037 mark_template_parm (t
, data
)
2043 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2045 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2047 level
= TEMPLATE_PARM_LEVEL (t
);
2048 idx
= TEMPLATE_PARM_IDX (t
);
2052 level
= TEMPLATE_TYPE_LEVEL (t
);
2053 idx
= TEMPLATE_TYPE_IDX (t
);
2056 if (level
== tpd
->level
)
2058 tpd
->parms
[idx
] = 1;
2059 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2062 /* Return zero so that for_each_template_parm will continue the
2063 traversal of the tree; we want to mark *every* template parm. */
2067 /* Process the partial specialization DECL. */
2070 process_partial_specialization (decl
)
2073 tree type
= TREE_TYPE (decl
);
2074 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2075 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2076 tree inner_args
= innermost_args (specargs
);
2077 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2078 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2079 int nargs
= TREE_VEC_LENGTH (inner_args
);
2080 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2082 int did_error_intro
= 0;
2083 struct template_parm_data tpd
;
2084 struct template_parm_data tpd2
;
2086 /* We check that each of the template parameters given in the
2087 partial specialization is used in the argument list to the
2088 specialization. For example:
2090 template <class T> struct S;
2091 template <class T> struct S<T*>;
2093 The second declaration is OK because `T*' uses the template
2094 parameter T, whereas
2096 template <class T> struct S<int>;
2098 is no good. Even trickier is:
2109 The S2<T> declaration is actually illegal; it is a
2110 full-specialization. Of course,
2113 struct S2<T (*)(U)>;
2115 or some such would have been OK. */
2116 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2117 tpd
.parms
= alloca (sizeof (int) * ntparms
);
2118 bzero ((PTR
) tpd
.parms
, sizeof (int) * ntparms
);
2120 tpd
.arg_uses_template_parms
= alloca (sizeof (int) * nargs
);
2121 bzero ((PTR
) tpd
.arg_uses_template_parms
, sizeof (int) * nargs
);
2122 for (i
= 0; i
< nargs
; ++i
)
2124 tpd
.current_arg
= i
;
2125 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2126 &mark_template_parm
,
2129 for (i
= 0; i
< ntparms
; ++i
)
2130 if (tpd
.parms
[i
] == 0)
2132 /* One of the template parms was not used in the
2134 if (!did_error_intro
)
2136 cp_error ("template parameters not used in partial specialization:");
2137 did_error_intro
= 1;
2141 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2144 /* [temp.class.spec]
2146 The argument list of the specialization shall not be identical to
2147 the implicit argument list of the primary template. */
2148 if (comp_template_args (inner_args
,
2149 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2151 cp_error ("partial specialization `%T' does not specialize any template arguments", type
);
2153 /* [temp.class.spec]
2155 A partially specialized non-type argument expression shall not
2156 involve template parameters of the partial specialization except
2157 when the argument expression is a simple identifier.
2159 The type of a template parameter corresponding to a specialized
2160 non-type argument shall not be dependent on a parameter of the
2162 my_friendly_assert (nargs
== DECL_NTPARMS (maintmpl
), 0);
2164 for (i
= 0; i
< nargs
; ++i
)
2166 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2167 if (/* These first two lines are the `non-type' bit. */
2168 TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2169 && TREE_CODE (arg
) != TEMPLATE_DECL
2170 /* This next line is the `argument expression is not just a
2171 simple identifier' condition and also the `specialized
2172 non-type argument' bit. */
2173 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2175 if (tpd
.arg_uses_template_parms
[i
])
2176 cp_error ("template argument `%E' involves template parameter(s)", arg
);
2179 /* Look at the corresponding template parameter,
2180 marking which template parameters its type depends
2183 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2188 /* We haven't yet initialized TPD2. Do so now. */
2189 tpd2
.arg_uses_template_parms
2190 = (int*) alloca (sizeof (int) * nargs
);
2191 /* The number of parameters here is the number in the
2192 main template, which, as checked in the assertion
2194 tpd2
.parms
= (int*) alloca (sizeof (int) * nargs
);
2196 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2199 /* Mark the template parameters. But this time, we're
2200 looking for the template parameters of the main
2201 template, not in the specialization. */
2202 tpd2
.current_arg
= i
;
2203 tpd2
.arg_uses_template_parms
[i
] = 0;
2204 bzero ((PTR
) tpd2
.parms
, sizeof (int) * nargs
);
2205 for_each_template_parm (type
,
2206 &mark_template_parm
,
2209 if (tpd2
.arg_uses_template_parms
[i
])
2211 /* The type depended on some template parameters.
2212 If they are fully specialized in the
2213 specialization, that's OK. */
2215 for (j
= 0; j
< nargs
; ++j
)
2216 if (tpd2
.parms
[j
] != 0
2217 && tpd
.arg_uses_template_parms
[j
])
2219 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
2229 if (retrieve_specialization (maintmpl
, specargs
))
2230 /* We've already got this specialization. */
2233 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
2234 = tree_cons (inner_args
, inner_parms
,
2235 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2236 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2240 /* Check that a template declaration's use of default arguments is not
2241 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2242 non-zero if DECL is the thing declared by a primary template.
2243 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2246 check_default_tmpl_args (decl
, parms
, is_primary
, is_partial
)
2253 int last_level_to_check
;
2258 A default template-argument shall not be specified in a
2259 function template declaration or a function template definition, nor
2260 in the template-parameter-list of the definition of a member of a
2263 if (TREE_CODE (DECL_REAL_CONTEXT (decl
)) == FUNCTION_DECL
)
2264 /* You can't have a function template declaration in a local
2265 scope, nor you can you define a member of a class template in a
2269 if (current_class_type
2270 && !TYPE_BEING_DEFINED (current_class_type
)
2271 && DECL_LANG_SPECIFIC (decl
)
2272 /* If this is either a friend defined in the scope of the class
2273 or a member function. */
2274 && DECL_CLASS_CONTEXT (decl
) == current_class_type
2275 /* And, if it was a member function, it really was defined in
2276 the scope of the class. */
2277 && (!DECL_FUNCTION_MEMBER_P (decl
) || DECL_DEFINED_IN_CLASS_P (decl
)))
2278 /* We already checked these parameters when the template was
2279 declared, so there's no need to do it again now. This function
2280 was defined in class scope, but we're processing it's body now
2281 that the class is complete. */
2286 If a template-parameter has a default template-argument, all
2287 subsequent template-parameters shall have a default
2288 template-argument supplied. */
2289 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2291 tree inner_parms
= TREE_VALUE (parm_level
);
2292 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2293 int seen_def_arg_p
= 0;
2296 for (i
= 0; i
< ntparms
; ++i
)
2298 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2299 if (TREE_PURPOSE (parm
))
2301 else if (seen_def_arg_p
)
2303 cp_error ("no default argument for `%D'", TREE_VALUE (parm
));
2304 /* For better subsequent error-recovery, we indicate that
2305 there should have been a default argument. */
2306 TREE_PURPOSE (parm
) = error_mark_node
;
2311 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2312 /* For an ordinary class template, default template arguments are
2313 allowed at the innermost level, e.g.:
2314 template <class T = int>
2316 but, in a partial specialization, they're not allowed even
2317 there, as we have in [temp.class.spec]:
2319 The template parameter list of a specialization shall not
2320 contain default template argument values.
2322 So, for a partial specialization, or for a function template,
2323 we look at all of them. */
2326 /* But, for a primary class template that is not a partial
2327 specialization we look at all template parameters except the
2329 parms
= TREE_CHAIN (parms
);
2331 /* Figure out what error message to issue. */
2332 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2333 msg
= "default argument for template parameter in function template `%D'";
2334 else if (is_partial
)
2335 msg
= "default argument in partial specialization `%D'";
2337 msg
= "default argument for template parameter for class enclosing `%D'";
2339 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2340 /* If we're inside a class definition, there's no need to
2341 examine the parameters to the class itself. On the one
2342 hand, they will be checked when the class is defined, and,
2343 on the other, default arguments are legal in things like:
2344 template <class T = double>
2345 struct S { template <class U> void f(U); };
2346 Here the default argument for `S' has no bearing on the
2347 declaration of `f'. */
2348 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2350 /* Check everything. */
2351 last_level_to_check
= 0;
2353 for (parm_level
= parms
;
2354 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2355 parm_level
= TREE_CHAIN (parm_level
))
2357 tree inner_parms
= TREE_VALUE (parm_level
);
2361 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2362 for (i
= 0; i
< ntparms
; ++i
)
2363 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2367 cp_error (msg
, decl
);
2371 /* Clear out the default argument so that we are not
2373 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2376 /* At this point, if we're still interested in issuing messages,
2377 they must apply to classes surrounding the object declared. */
2379 msg
= "default argument for template parameter for class enclosing `%D'";
2383 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
2384 parameters given by current_template_args, or reuses a
2385 previously existing one, if appropriate. Returns the DECL, or an
2386 equivalent one, if it is replaced via a call to duplicate_decls.
2388 If IS_FRIEND is non-zero, DECL is a friend declaration. */
2391 push_template_decl_real (decl
, is_friend
)
2401 int new_template_p
= 0;
2403 /* See if this is a partial specialization. */
2404 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
2405 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
2406 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
2408 is_friend
|= (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
));
2411 /* For a friend, we want the context of the friend function, not
2412 the type of which it is a friend. */
2413 ctx
= DECL_CONTEXT (decl
);
2414 else if (DECL_REAL_CONTEXT (decl
)
2415 && TREE_CODE (DECL_REAL_CONTEXT (decl
)) != NAMESPACE_DECL
)
2416 /* In the case of a virtual function, we want the class in which
2418 ctx
= DECL_REAL_CONTEXT (decl
);
2420 /* Otherwise, if we're currently definining some class, the DECL
2421 is assumed to be a member of the class. */
2422 ctx
= current_scope ();
2424 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
2427 if (!DECL_CONTEXT (decl
))
2428 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
2430 /* See if this is a primary template. */
2431 primary
= pseudo_global_level_p ();
2435 if (current_lang_name
== lang_name_c
)
2436 cp_error ("template with C linkage");
2437 else if (TREE_CODE (decl
) == TYPE_DECL
2438 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
2439 cp_error ("template class without a name");
2440 else if ((DECL_IMPLICIT_TYPEDEF_P (decl
)
2441 && CLASS_TYPE_P (TREE_TYPE (decl
)))
2442 || (TREE_CODE (decl
) == VAR_DECL
&& ctx
&& CLASS_TYPE_P (ctx
))
2443 || TREE_CODE (decl
) == FUNCTION_DECL
)
2446 cp_error ("template declaration of `%#D'", decl
);
2449 /* Check to see that the rules regarding the use of default
2450 arguments are not being violated. */
2451 check_default_tmpl_args (decl
, current_template_parms
,
2452 primary
, is_partial
);
2455 return process_partial_specialization (decl
);
2457 args
= current_template_args ();
2460 || TREE_CODE (ctx
) == FUNCTION_DECL
2461 || TYPE_BEING_DEFINED (ctx
)
2462 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
2464 if (DECL_LANG_SPECIFIC (decl
)
2465 && DECL_TEMPLATE_INFO (decl
)
2466 && DECL_TI_TEMPLATE (decl
))
2467 tmpl
= DECL_TI_TEMPLATE (decl
);
2468 /* If DECL is a TYPE_DECL for a class-template, then there won't
2469 be DECL_LANG_SPECIFIC. The information equivalent to
2470 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2471 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
2472 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2473 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2475 /* Since a template declaration already existed for this
2476 class-type, we must be redeclaring it here. Make sure
2477 that the redeclaration is legal. */
2478 redeclare_class_template (TREE_TYPE (decl
),
2479 current_template_parms
);
2480 /* We don't need to create a new TEMPLATE_DECL; just use the
2481 one we already had. */
2482 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2486 tmpl
= build_template_decl (decl
, current_template_parms
);
2489 if (DECL_LANG_SPECIFIC (decl
)
2490 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2492 /* A specialization of a member template of a template
2494 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2495 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
2496 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2502 tree a
, t
, current
, parms
;
2505 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2506 cp_error ("must specialize `%#T' before defining member `%#D'",
2508 if (TREE_CODE (decl
) == TYPE_DECL
)
2510 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
2511 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
2512 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
2513 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
2514 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
2517 cp_error ("`%D' does not declare a template type", decl
);
2521 else if (! DECL_TEMPLATE_INFO (decl
))
2523 cp_error ("template definition of non-template `%#D'", decl
);
2527 tmpl
= DECL_TI_TEMPLATE (decl
);
2529 if (is_member_template (tmpl
)
2530 && DECL_FUNCTION_TEMPLATE_P (tmpl
)
2531 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
2532 && DECL_TEMPLATE_SPECIALIZATION (decl
))
2536 /* The declaration is a specialization of a member
2537 template, declared outside the class. Therefore, the
2538 innermost template arguments will be NULL, so we
2539 replace them with the arguments determined by the
2540 earlier call to check_explicit_specialization. */
2541 args
= DECL_TI_ARGS (decl
);
2544 = build_template_decl (decl
, current_template_parms
);
2545 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
2546 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
2547 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
2548 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
2549 DECL_TEMPLATE_INFO (new_tmpl
)
2550 = tree_cons (tmpl
, args
, NULL_TREE
);
2552 register_specialization (new_tmpl
, tmpl
, args
);
2556 /* Make sure the template headers we got make sense. */
2558 parms
= DECL_TEMPLATE_PARMS (tmpl
);
2559 i
= TMPL_PARMS_DEPTH (parms
);
2560 if (TMPL_ARGS_DEPTH (args
) != i
)
2562 cp_error ("expected %d levels of template parms for `%#D', got %d",
2563 i
, decl
, TMPL_ARGS_DEPTH (args
));
2566 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
2568 a
= TMPL_ARGS_LEVEL (args
, i
);
2569 t
= INNERMOST_TEMPLATE_PARMS (parms
);
2571 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
2573 if (current
== decl
)
2574 cp_error ("got %d template parameters for `%#D'",
2575 TREE_VEC_LENGTH (a
), decl
);
2577 cp_error ("got %d template parameters for `%#T'",
2578 TREE_VEC_LENGTH (a
), current
);
2579 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
2582 /* Perhaps we should also check that the parms are used in the
2583 appropriate qualifying scopes in the declarator? */
2585 if (current
== decl
)
2588 current
= TYPE_CONTEXT (current
);
2592 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
2593 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
2595 /* Push template declarations for global functions and types. Note
2596 that we do not try to push a global template friend declared in a
2597 template class; such a thing may well depend on the template
2598 parameters of the class. */
2599 if (new_template_p
&& !ctx
2600 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
2601 tmpl
= pushdecl_namespace_level (tmpl
);
2604 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
2606 info
= tree_cons (tmpl
, args
, NULL_TREE
);
2608 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2610 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
2611 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
2612 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
)
2613 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
2615 else if (DECL_LANG_SPECIFIC (decl
))
2616 DECL_TEMPLATE_INFO (decl
) = info
;
2618 return DECL_TEMPLATE_RESULT (tmpl
);
2622 push_template_decl (decl
)
2625 return push_template_decl_real (decl
, 0);
2628 /* Called when a class template TYPE is redeclared with the indicated
2629 template PARMS, e.g.:
2631 template <class T> struct S;
2632 template <class T> struct S {}; */
2635 redeclare_class_template (type
, parms
)
2643 if (!TYPE_TEMPLATE_INFO (type
))
2645 cp_error ("`%T' is not a template type", type
);
2649 tmpl
= TYPE_TI_TEMPLATE (type
);
2650 if (!PRIMARY_TEMPLATE_P (tmpl
))
2651 /* The type is nested in some template class. Nothing to worry
2652 about here; there are no new template parameters for the nested
2656 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
2657 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
2659 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
2661 cp_error_at ("previous declaration `%D'", tmpl
);
2662 cp_error ("used %d template parameter%s instead of %d",
2663 TREE_VEC_LENGTH (tmpl_parms
),
2664 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
2665 TREE_VEC_LENGTH (parms
));
2669 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
2671 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
2672 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2673 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
2674 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
2676 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
2678 cp_error_at ("template parameter `%#D'", tmpl_parm
);
2679 cp_error ("redeclared here as `%#D'", parm
);
2683 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
2685 /* We have in [temp.param]:
2687 A template-parameter may not be given default arguments
2688 by two different declarations in the same scope. */
2689 cp_error ("redefinition of default argument for `%#D'", parm
);
2690 cp_error_at (" original definition appeared here", tmpl_parm
);
2694 if (parm_default
!= NULL_TREE
)
2695 /* Update the previous template parameters (which are the ones
2696 that will really count) with the new default value. */
2697 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
2698 else if (tmpl_default
!= NULL_TREE
)
2699 /* Update the new parameters, too; they'll be used as the
2700 parameters for any members. */
2701 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
2705 /* Attempt to convert the non-type template parameter EXPR to the
2706 indicated TYPE. If the conversion is successful, return the
2707 converted value. If the conversion is unsuccesful, return
2708 NULL_TREE if we issued an error message, or error_mark_node if we
2709 did not. We issue error messages for out-and-out bad template
2710 parameters, but not simply because the conversion failed, since we
2711 might be just trying to do argument deduction. By the time this
2712 function is called, neither TYPE nor EXPR may make use of template
2716 convert_nontype_argument (type
, expr
)
2720 tree expr_type
= TREE_TYPE (expr
);
2722 /* A template-argument for a non-type, non-template
2723 template-parameter shall be one of:
2725 --an integral constant-expression of integral or enumeration
2728 --the name of a non-type template-parameter; or
2730 --the name of an object or function with external linkage,
2731 including function templates and function template-ids but
2732 excluding non-static class members, expressed as id-expression;
2735 --the address of an object or function with external linkage,
2736 including function templates and function template-ids but
2737 excluding non-static class members, expressed as & id-expression
2738 where the & is optional if the name refers to a function or
2741 --a pointer to member expressed as described in _expr.unary.op_. */
2743 /* An integral constant-expression can include const variables or
2744 enumerators. Simplify things by folding them to their values,
2745 unless we're about to bind the declaration to a reference
2747 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
)
2748 && TREE_CODE (type
) != REFERENCE_TYPE
)
2749 expr
= decl_constant_value (expr
);
2751 if (is_overloaded_fn (expr
))
2752 /* OK for now. We'll check that it has external linkage later.
2753 Check this first since if expr_type is the unknown_type_node
2754 we would otherwise complain below. */
2756 else if (TYPE_PTRMEM_P (expr_type
)
2757 || TYPE_PTRMEMFUNC_P (expr_type
))
2759 if (TREE_CODE (expr
) != PTRMEM_CST
)
2762 else if (TYPE_PTR_P (expr_type
)
2763 || TYPE_PTRMEM_P (expr_type
)
2764 || TREE_CODE (expr_type
) == ARRAY_TYPE
2765 || TREE_CODE (type
) == REFERENCE_TYPE
2766 /* If expr is the address of an overloaded function, we
2767 will get the unknown_type_node at this point. */
2768 || expr_type
== unknown_type_node
)
2774 if (TREE_CODE (type
) == REFERENCE_TYPE
2775 || TREE_CODE (expr_type
) == ARRAY_TYPE
)
2779 if (TREE_CODE (e
) != ADDR_EXPR
)
2782 cp_error ("`%E' is not a valid template argument", expr
);
2783 if (TYPE_PTR_P (expr_type
))
2785 if (TREE_CODE (TREE_TYPE (expr_type
)) == FUNCTION_TYPE
)
2786 cp_error ("it must be the address of a function with external linkage");
2788 cp_error ("it must be the address of an object with external linkage");
2790 else if (TYPE_PTRMEM_P (expr_type
)
2791 || TYPE_PTRMEMFUNC_P (expr_type
))
2792 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2797 referent
= TREE_OPERAND (e
, 0);
2798 STRIP_NOPS (referent
);
2801 if (TREE_CODE (referent
) == STRING_CST
)
2803 cp_error ("string literal %E is not a valid template argument",
2805 error ("because it is the address of an object with static linkage");
2809 if (is_overloaded_fn (referent
))
2810 /* We'll check that it has external linkage later. */
2812 else if (TREE_CODE (referent
) != VAR_DECL
)
2814 else if (!TREE_PUBLIC (referent
))
2816 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
2817 return error_mark_node
;
2820 else if (INTEGRAL_TYPE_P (expr_type
)
2821 || TYPE_PTRMEM_P (expr_type
)
2822 || TYPE_PTRMEMFUNC_P (expr_type
)
2823 /* The next two are g++ extensions. */
2824 || TREE_CODE (expr_type
) == REAL_TYPE
2825 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
2827 if (! TREE_CONSTANT (expr
))
2830 cp_error ("non-constant `%E' cannot be used as template argument",
2837 cp_error ("object `%E' cannot be used as template argument", expr
);
2841 switch (TREE_CODE (type
))
2846 /* For a non-type template-parameter of integral or enumeration
2847 type, integral promotions (_conv.prom_) and integral
2848 conversions (_conv.integral_) are applied. */
2849 if (!INTEGRAL_TYPE_P (expr_type
))
2850 return error_mark_node
;
2852 /* It's safe to call digest_init in this case; we know we're
2853 just converting one integral constant expression to another. */
2854 expr
= digest_init (type
, expr
, (tree
*) 0);
2856 if (TREE_CODE (expr
) != INTEGER_CST
)
2857 /* Curiously, some TREE_CONSTANT integral expressions do not
2858 simplify to integer constants. For example, `3 % 0',
2859 remains a TRUNC_MOD_EXPR. */
2866 /* These are g++ extensions. */
2867 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2868 return error_mark_node
;
2870 expr
= digest_init (type
, expr
, (tree
*) 0);
2872 if (TREE_CODE (expr
) != REAL_CST
)
2879 tree type_pointed_to
= TREE_TYPE (type
);
2881 if (TYPE_PTRMEM_P (type
))
2885 /* For a non-type template-parameter of type pointer to data
2886 member, qualification conversions (_conv.qual_) are
2888 e
= perform_qualification_conversions (type
, expr
);
2889 if (TREE_CODE (e
) == NOP_EXPR
)
2890 /* The call to perform_qualification_conversions will
2891 insert a NOP_EXPR over EXPR to do express conversion,
2892 if necessary. But, that will confuse us if we use
2893 this (converted) template parameter to instantiate
2894 another template; then the thing will not look like a
2895 valid template argument. So, just make a new
2896 constant, of the appropriate type. */
2897 e
= make_ptrmem_cst (type
, PTRMEM_CST_MEMBER (expr
));
2900 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2902 /* For a non-type template-parameter of type pointer to
2903 function, only the function-to-pointer conversion
2904 (_conv.func_) is applied. If the template-argument
2905 represents a set of overloaded functions (or a pointer to
2906 such), the matching function is selected from the set
2911 if (TREE_CODE (expr
) == ADDR_EXPR
)
2912 fns
= TREE_OPERAND (expr
, 0);
2916 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2918 if (fn
== error_mark_node
)
2919 return error_mark_node
;
2921 if (!TREE_PUBLIC (fn
))
2923 if (really_overloaded_fn (fns
))
2924 return error_mark_node
;
2929 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2931 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
2937 /* For a non-type template-parameter of type pointer to
2938 object, qualification conversions (_conv.qual_) and the
2939 array-to-pointer conversion (_conv.array_) are applied.
2940 [Note: In particular, neither the null pointer conversion
2941 (_conv.ptr_) nor the derived-to-base conversion
2942 (_conv.ptr_) are applied. Although 0 is a valid
2943 template-argument for a non-type template-parameter of
2944 integral type, it is not a valid template-argument for a
2945 non-type template-parameter of pointer type.]
2947 The call to decay_conversion performs the
2948 array-to-pointer conversion, if appropriate. */
2949 expr
= decay_conversion (expr
);
2951 if (expr
== error_mark_node
)
2952 return error_mark_node
;
2954 return perform_qualification_conversions (type
, expr
);
2959 case REFERENCE_TYPE
:
2961 tree type_referred_to
= TREE_TYPE (type
);
2963 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2965 /* For a non-type template-parameter of type reference to
2966 function, no conversions apply. If the
2967 template-argument represents a set of overloaded
2968 functions, the matching function is selected from the
2969 set (_over.over_). */
2973 fn
= instantiate_type (type_referred_to
, fns
, 0);
2975 if (fn
== error_mark_node
)
2976 return error_mark_node
;
2978 if (!TREE_PUBLIC (fn
))
2980 if (really_overloaded_fn (fns
))
2981 /* Don't issue an error here; we might get a different
2982 function if the overloading had worked out
2984 return error_mark_node
;
2989 my_friendly_assert (same_type_p (type_referred_to
,
2997 /* For a non-type template-parameter of type reference to
2998 object, no conversions apply. The type referred to by the
2999 reference may be more cv-qualified than the (otherwise
3000 identical) type of the template-argument. The
3001 template-parameter is bound directly to the
3002 template-argument, which must be an lvalue. */
3003 if ((TYPE_MAIN_VARIANT (expr_type
)
3004 != TYPE_MAIN_VARIANT (type_referred_to
))
3005 || !at_least_as_qualified_p (type_referred_to
,
3007 || !real_lvalue_p (expr
))
3008 return error_mark_node
;
3017 if (!TYPE_PTRMEMFUNC_P (type
))
3018 /* This handles templates like
3019 template<class T, T t> void f();
3020 when T is substituted with any class. The second template
3021 parameter becomes invalid and the template candidate is
3023 return error_mark_node
;
3025 /* For a non-type template-parameter of type pointer to member
3026 function, no conversions apply. If the template-argument
3027 represents a set of overloaded member functions, the
3028 matching member function is selected from the set
3031 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
3032 expr_type
!= unknown_type_node
)
3033 return error_mark_node
;
3035 if (TREE_CODE (expr
) == PTRMEM_CST
)
3037 /* A ptr-to-member constant. */
3038 if (!same_type_p (type
, expr_type
))
3039 return error_mark_node
;
3044 if (TREE_CODE (expr
) != ADDR_EXPR
)
3045 return error_mark_node
;
3047 expr
= instantiate_type (type
, expr
, 0);
3049 if (expr
== error_mark_node
)
3050 return error_mark_node
;
3052 my_friendly_assert (same_type_p (type
, TREE_TYPE (expr
)),
3059 /* All non-type parameters must have one of these types. */
3060 my_friendly_abort (0);
3064 return error_mark_node
;
3067 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3068 template template parameters. Both PARM_PARMS and ARG_PARMS are
3069 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3072 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3073 the case, then extra parameters must have default arguments.
3075 Consider the example:
3076 template <class T, class Allocator = allocator> class vector;
3077 template<template <class U> class TT> class C;
3079 C<vector> is a valid instantiation. PARM_PARMS for the above code
3080 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3081 T and Allocator) and OUTER_ARGS contains the argument that is used to
3082 substitute the TT parameter. */
3085 coerce_template_template_parms (parm_parms
, arg_parms
, complain
,
3086 in_decl
, outer_args
)
3087 tree parm_parms
, arg_parms
;
3089 tree in_decl
, outer_args
;
3091 int nparms
, nargs
, i
;
3094 my_friendly_assert (TREE_CODE (parm_parms
) == TREE_VEC
, 0);
3095 my_friendly_assert (TREE_CODE (arg_parms
) == TREE_VEC
, 0);
3097 nparms
= TREE_VEC_LENGTH (parm_parms
);
3098 nargs
= TREE_VEC_LENGTH (arg_parms
);
3100 /* The rule here is opposite of coerce_template_parms. */
3103 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms
, nparms
)) == NULL_TREE
))
3106 for (i
= 0; i
< nparms
; ++i
)
3108 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3109 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3111 if (arg
== NULL_TREE
|| arg
== error_mark_node
3112 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3115 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3118 switch (TREE_CODE (parm
))
3124 /* We encounter instantiations of templates like
3125 template <template <template <class> class> class TT>
3128 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3129 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3131 if (!coerce_template_template_parms (parmparm
, argparm
,
3139 /* The tsubst call is used to handle cases such as
3140 template <class T, template <T> class TT> class D;
3141 i.e. the parameter list of TT depends on earlier parameters. */
3142 if (!same_type_p (tsubst (TREE_TYPE (parm
), outer_args
,
3149 my_friendly_abort (0);
3155 /* Convert the indicated template ARG as necessary to match the
3156 indicated template PARM. Returns the converted ARG, or
3157 error_mark_node if the conversion was unsuccessful. Error messages
3158 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3159 parameter in the parameter list. ARGS is the full set of template
3160 arguments deduced so far. */
3163 convert_template_argument (parm
, arg
, args
, complain
, i
, in_decl
)
3173 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3175 inner_args
= innermost_args (args
);
3177 if (TREE_CODE (arg
) == TREE_LIST
3178 && TREE_TYPE (arg
) != NULL_TREE
3179 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
3181 /* The template argument was the name of some
3182 member function. That's usually
3183 illegal, but static members are OK. In any
3184 case, grab the underlying fields/functions
3185 and issue an error later if required. */
3186 arg
= TREE_VALUE (arg
);
3187 TREE_TYPE (arg
) = unknown_type_node
;
3190 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3191 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3192 || requires_tmpl_type
);
3194 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3195 we also accept implicitly created TYPE_DECL as a valid argument.
3196 This is necessary to handle the case where we pass a template name
3197 to a template template parameter in a scope where we've derived from
3198 in instantiation of that template, so the template name refers to that
3199 instantiation. We really ought to handle this better. */
3201 = ((TREE_CODE (arg
) == TEMPLATE_DECL
3202 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3203 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3204 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg
))
3205 || (TREE_CODE (arg
) == RECORD_TYPE
3206 && CLASSTYPE_TEMPLATE_INFO (arg
)
3207 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
3208 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
3209 && requires_tmpl_type
3210 && is_base_of_enclosing_class (arg
, current_class_type
)));
3211 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3212 arg
= TYPE_STUB_DECL (arg
);
3213 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
3214 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
3216 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
3218 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3219 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3221 cp_pedwarn ("to refer to a type member of a template parameter,");
3222 cp_pedwarn (" use `typename %E'", arg
);
3224 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3225 TREE_OPERAND (arg
, 1));
3228 if (is_type
!= requires_type
)
3234 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3237 cp_error (" expected a constant of type `%T', got `%T'",
3239 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
3241 cp_error (" expected a type, got `%E'", arg
);
3244 return error_mark_node
;
3246 if (is_tmpl_type
^ requires_tmpl_type
)
3248 if (in_decl
&& complain
)
3250 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3253 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
3255 cp_error (" expected a class template, got `%T'", arg
);
3257 return error_mark_node
;
3262 if (requires_tmpl_type
)
3264 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3265 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3267 if (coerce_template_template_parms (parmparm
, argparm
, complain
,
3268 in_decl
, inner_args
))
3272 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3274 if (val
!= error_mark_node
3275 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
3276 val
= TREE_TYPE (val
);
3280 if (in_decl
&& complain
)
3282 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
3284 cp_error (" expected a template of type `%D', got `%D'", parm
, arg
);
3287 val
= error_mark_node
;
3292 val
= groktypename (arg
);
3293 if (! processing_template_decl
)
3295 /* [basic.link]: A name with no linkage (notably, the
3296 name of a class or enumeration declared in a local
3297 scope) shall not be used to declare an entity with
3298 linkage. This implies that names with no linkage
3299 cannot be used as template arguments. */
3300 tree t
= no_linkage_check (val
);
3303 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t
)))
3305 ("template-argument `%T' uses anonymous type", val
);
3308 ("template-argument `%T' uses local type `%T'",
3310 return error_mark_node
;
3317 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
3319 if (processing_template_decl
)
3320 arg
= maybe_fold_nontype_arg (arg
);
3322 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
3323 /* We used to call digest_init here. However, digest_init
3324 will report errors, which we don't want when complain
3325 is zero. More importantly, digest_init will try too
3326 hard to convert things: for example, `0' should not be
3327 converted to pointer type at this point according to
3328 the standard. Accepting this is not merely an
3329 extension, since deciding whether or not these
3330 conversions can occur is part of determining which
3331 function template to call, or whether a given epxlicit
3332 argument specification is legal. */
3333 val
= convert_nontype_argument (t
, arg
);
3337 if (val
== NULL_TREE
)
3338 val
= error_mark_node
;
3339 else if (val
== error_mark_node
&& complain
)
3340 cp_error ("could not convert template argument `%E' to `%T'",
3347 /* Convert all template arguments to their appropriate types, and
3348 return a vector containing the innermost resulting template
3349 arguments. If any error occurs, return error_mark_node, and, if
3350 COMPLAIN is non-zero, issue an error message. Some error messages
3351 are issued even if COMPLAIN is zero; for instance, if a template
3352 argument is composed from a local class.
3354 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3355 provided in ARGLIST, or else trailing parameters must have default
3356 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
3357 deduction for any unspecified trailing arguments.
3359 The resulting TREE_VEC is allocated on a temporary obstack, and
3360 must be explicitly copied if it will be permanent. */
3363 coerce_template_parms (parms
, args
, in_decl
,
3365 require_all_arguments
)
3369 int require_all_arguments
;
3371 int nparms
, nargs
, i
, lost
= 0;
3374 tree new_inner_args
;
3376 inner_args
= innermost_args (args
);
3377 nargs
= NUM_TMPL_ARGS (inner_args
);
3378 nparms
= TREE_VEC_LENGTH (parms
);
3382 && require_all_arguments
3383 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
3387 cp_error ("wrong number of template arguments (%d, should be %d)",
3391 cp_error_at ("provided for `%D'", in_decl
);
3394 return error_mark_node
;
3397 new_inner_args
= make_temp_vec (nparms
);
3398 new_args
= add_outermost_template_args (args
, new_inner_args
);
3399 for (i
= 0; i
< nparms
; i
++)
3404 /* Get the Ith template parameter. */
3405 parm
= TREE_VEC_ELT (parms
, i
);
3407 /* Calculate the Ith argument. */
3408 if (inner_args
&& TREE_CODE (inner_args
) == TREE_LIST
)
3410 arg
= TREE_VALUE (inner_args
);
3411 inner_args
= TREE_CHAIN (inner_args
);
3414 arg
= TREE_VEC_ELT (inner_args
, i
);
3415 /* If no template argument was supplied, look for a default
3417 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
3419 /* There was no default value. */
3420 my_friendly_assert (!require_all_arguments
, 0);
3423 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3424 arg
= tsubst (TREE_PURPOSE (parm
), new_args
, complain
, in_decl
);
3426 arg
= tsubst_expr (TREE_PURPOSE (parm
), new_args
, complain
,
3429 /* Now, convert the Ith argument, as necessary. */
3430 if (arg
== NULL_TREE
)
3431 /* We're out of arguments. */
3433 my_friendly_assert (!require_all_arguments
, 0);
3436 else if (arg
== error_mark_node
)
3438 cp_error ("template argument %d is invalid", i
+ 1);
3439 arg
= error_mark_node
;
3442 arg
= convert_template_argument (TREE_VALUE (parm
),
3443 arg
, new_args
, complain
, i
,
3446 if (arg
== error_mark_node
)
3448 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
3452 return error_mark_node
;
3454 return new_inner_args
;
3457 /* Returns 1 if template args OT and NT are equivalent. */
3460 template_args_equal (ot
, nt
)
3465 if (TREE_CODE (nt
) != TREE_CODE (ot
))
3467 if (TREE_CODE (nt
) == TREE_VEC
)
3468 /* For member templates */
3469 return comp_template_args (ot
, nt
);
3470 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
3471 return same_type_p (ot
, nt
);
3473 return (cp_tree_equal (ot
, nt
) > 0);
3476 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
3477 of template arguments. Returns 0 otherwise. */
3480 comp_template_args (oldargs
, newargs
)
3481 tree oldargs
, newargs
;
3485 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
3488 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
3490 tree nt
= TREE_VEC_ELT (newargs
, i
);
3491 tree ot
= TREE_VEC_ELT (oldargs
, i
);
3493 if (! template_args_equal (ot
, nt
))
3499 /* Given class template name and parameter list, produce a user-friendly name
3500 for the instantiation. */
3503 mangle_class_name_for_template (name
, parms
, arglist
)
3505 tree parms
, arglist
;
3507 static struct obstack scratch_obstack
;
3508 static char *scratch_firstobj
;
3511 if (!scratch_firstobj
)
3512 gcc_obstack_init (&scratch_obstack
);
3514 obstack_free (&scratch_obstack
, scratch_firstobj
);
3515 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
3517 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
3518 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
3522 nparms
= TREE_VEC_LENGTH (parms
);
3523 arglist
= innermost_args (arglist
);
3524 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
3525 for (i
= 0; i
< nparms
; i
++)
3527 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3528 tree arg
= TREE_VEC_ELT (arglist
, i
);
3533 if (TREE_CODE (parm
) == TYPE_DECL
)
3535 cat (type_as_string_real (arg
, 0, 1));
3538 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3540 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
3542 /* Already substituted with real template. Just output
3543 the template name here */
3544 tree context
= DECL_CONTEXT (arg
);
3547 /* The template may be defined in a namespace, or
3548 may be a member template. */
3549 my_friendly_assert (TREE_CODE (context
) == NAMESPACE_DECL
3550 || CLASS_TYPE_P (context
),
3552 cat(decl_as_string (DECL_CONTEXT (arg
), 0));
3555 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
3558 /* Output the parameter declaration */
3559 cat (type_as_string_real (arg
, 0, 1));
3563 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
3565 if (TREE_CODE (arg
) == TREE_LIST
)
3567 /* New list cell was built because old chain link was in
3569 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
3570 arg
= TREE_VALUE (arg
);
3572 /* No need to check arglist against parmlist here; we did that
3573 in coerce_template_parms, called from lookup_template_class. */
3574 cat (expr_as_string (arg
, 0));
3577 char *bufp
= obstack_next_free (&scratch_obstack
);
3579 while (bufp
[offset
- 1] == ' ')
3581 obstack_blank_fast (&scratch_obstack
, offset
);
3583 /* B<C<char> >, not B<C<char>> */
3584 if (bufp
[offset
- 1] == '>')
3589 return (char *) obstack_base (&scratch_obstack
);
3593 classtype_mangled_name (t
)
3596 if (CLASSTYPE_TEMPLATE_INFO (t
)
3597 /* Specializations have already had their names set up in
3598 lookup_template_class. */
3599 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3601 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
3603 /* For non-primary templates, the template parameters are
3604 implicit from their surrounding context. */
3605 if (PRIMARY_TEMPLATE_P (tmpl
))
3607 tree name
= DECL_NAME (tmpl
);
3608 char *mangled_name
= mangle_class_name_for_template
3609 (IDENTIFIER_POINTER (name
),
3610 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
3611 CLASSTYPE_TI_ARGS (t
));
3612 tree id
= get_identifier (mangled_name
);
3613 IDENTIFIER_TEMPLATE (id
) = name
;
3618 return TYPE_IDENTIFIER (t
);
3622 add_pending_template (d
)
3627 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
3628 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
3630 ti
= DECL_TEMPLATE_INFO (d
);
3632 if (TI_PENDING_TEMPLATE_FLAG (ti
))
3635 *template_tail
= tree_cons (build_srcloc_here (), d
, NULL_TREE
);
3636 template_tail
= &TREE_CHAIN (*template_tail
);
3637 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
3641 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3642 may be either a _DECL or an overloaded function or an
3643 IDENTIFIER_NODE), and ARGLIST. */
3646 lookup_template_function (fns
, arglist
)
3651 if (fns
== NULL_TREE
)
3653 cp_error ("non-template used as template");
3654 return error_mark_node
;
3657 type
= TREE_TYPE (fns
);
3658 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
3659 type
= unknown_type_node
;
3661 if (processing_template_decl
)
3662 return build_min (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3664 return build (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
3667 /* Within the scope of a template class S<T>, the name S gets bound
3668 (in build_self_reference) to a TYPE_DECL for the class, not a
3669 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3670 or one of its enclosing classes, and that type is a template,
3671 return the associated TEMPLATE_DECL. Otherwise, the original
3672 DECL is returned. */
3675 maybe_get_template_decl_from_type_decl (decl
)
3678 return (decl
!= NULL_TREE
3679 && TREE_CODE (decl
) == TYPE_DECL
3680 && DECL_ARTIFICIAL (decl
)
3681 && CLASS_TYPE_P (TREE_TYPE (decl
))
3682 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
3683 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
3686 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3687 parameters, find the desired type.
3689 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3690 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3691 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3692 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3693 copy it here to keep it from being reclaimed when the decl storage
3696 IN_DECL, if non-NULL, is the template declaration we are trying to
3699 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3700 the class we are looking up.
3702 If the template class is really a local class in a template
3703 function, then the FUNCTION_CONTEXT is the function in which it is
3704 being instantiated. */
3707 lookup_template_class (d1
, arglist
, in_decl
, context
, entering_scope
)
3713 tree
template = NULL_TREE
, parmlist
;
3716 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
3718 if (IDENTIFIER_VALUE (d1
)
3719 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1
)))
3720 template = IDENTIFIER_VALUE (d1
);
3724 push_decl_namespace (context
);
3725 template = lookup_name (d1
, /*prefer_type=*/0);
3726 template = maybe_get_template_decl_from_type_decl (template);
3728 pop_decl_namespace ();
3731 context
= DECL_CONTEXT (template);
3733 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
3735 tree type
= TREE_TYPE (d1
);
3737 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3738 an implicit typename for the second A. Deal with it. */
3739 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
3740 type
= TREE_TYPE (type
);
3742 if (CLASSTYPE_TEMPLATE_INFO (type
))
3744 template = CLASSTYPE_TI_TEMPLATE (type
);
3745 d1
= DECL_NAME (template);
3748 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
3749 || (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't'
3750 && IS_AGGR_TYPE (d1
)))
3752 template = TYPE_TI_TEMPLATE (d1
);
3753 d1
= DECL_NAME (template);
3755 else if (TREE_CODE (d1
) == TEMPLATE_DECL
3756 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
3759 d1
= DECL_NAME (template);
3760 context
= DECL_CONTEXT (template);
3763 my_friendly_abort (272);
3765 /* With something like `template <class T> class X class X { ... };'
3766 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3767 We don't want to do that, but we have to deal with the situation,
3768 so let's give them some syntax errors to chew on instead of a
3772 cp_error ("`%T' is not a template", d1
);
3773 return error_mark_node
;
3776 if (context
== NULL_TREE
)
3777 context
= global_namespace
;
3779 if (TREE_CODE (template) != TEMPLATE_DECL
)
3781 cp_error ("non-template type `%T' used as a template", d1
);
3783 cp_error_at ("for template declaration `%D'", in_decl
);
3784 return error_mark_node
;
3787 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3789 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3790 template arguments */
3792 tree parm
= copy_template_template_parm (TREE_TYPE (template));
3793 tree template2
= TYPE_STUB_DECL (parm
);
3796 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
3798 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
3799 if (arglist2
== error_mark_node
)
3800 return error_mark_node
;
3802 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
)
3803 = tree_cons (template2
, arglist2
, NULL_TREE
);
3804 TYPE_SIZE (parm
) = 0;
3809 tree template_type
= TREE_TYPE (template);
3812 tree found
= NULL_TREE
;
3815 int is_partial_instantiation
;
3817 gen_tmpl
= most_general_template (template);
3818 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
3819 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
3820 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3822 /* We build up the coerced arguments and such on the
3823 momentary_obstack. */
3826 if (arg_depth
== 1 && parm_depth
> 1)
3828 /* We've been given an incomplete set of template arguments.
3831 template <class T> struct S1 {
3832 template <class U> struct S2 {};
3833 template <class U> struct S2<U*> {};
3836 we will be called with an ARGLIST of `U*', but the
3837 TEMPLATE will be `template <class T> template
3838 <class U> struct S1<T>::S2'. We must fill in the missing
3841 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3843 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
3846 /* Now we should enough arguments. */
3847 my_friendly_assert (parm_depth
== arg_depth
, 0);
3849 /* From here on, we're only interested in the most general
3851 template = gen_tmpl
;
3853 /* Calculate the BOUND_ARGS. These will be the args that are
3854 actually tsubst'd into the definition to create the
3858 /* We have multiple levels of arguments to coerce, at once. */
3860 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
3862 tree bound_args
= make_temp_vec (parm_depth
);
3864 for (i
= saved_depth
,
3865 t
= DECL_TEMPLATE_PARMS (template);
3866 i
> 0 && t
!= NULL_TREE
;
3867 --i
, t
= TREE_CHAIN (t
))
3869 tree a
= coerce_template_parms (TREE_VALUE (t
),
3870 arglist
, template, 1, 1);
3871 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
3873 /* We temporarily reduce the length of the ARGLIST so
3874 that coerce_template_parms will see only the arguments
3875 corresponding to the template parameters it is
3877 TREE_VEC_LENGTH (arglist
)--;
3880 /* Restore the ARGLIST to its full size. */
3881 TREE_VEC_LENGTH (arglist
) = saved_depth
;
3883 arglist
= bound_args
;
3887 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
3888 innermost_args (arglist
),
3891 if (arglist
== error_mark_node
)
3892 /* We were unable to bind the arguments. */
3893 return error_mark_node
;
3895 /* In the scope of a template class, explicit references to the
3896 template class refer to the type of the template, not any
3897 instantiation of it. For example, in:
3899 template <class T> class C { void f(C<T>); }
3901 the `C<T>' is just the same as `C'. Outside of the
3902 class, however, such a reference is an instantiation. */
3903 if (comp_template_args (TYPE_TI_ARGS (template_type
),
3906 found
= template_type
;
3908 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
3912 /* Note that we use DECL_CONTEXT, rather than
3913 CP_DECL_CONTEXT, so that the termination test is
3914 always just `ctx'. We're not interested in namepace
3916 for (ctx
= current_class_type
;
3918 ctx
= (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
3919 ? TYPE_CONTEXT (ctx
) : DECL_CONTEXT (ctx
))
3920 if (same_type_p (ctx
, template_type
))
3924 /* We're not in the scope of the class, so the
3925 TEMPLATE_TYPE is not the type we want after
3933 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
3934 found
; found
= TREE_CHAIN (found
))
3935 if (comp_template_args (TREE_PURPOSE (found
), arglist
))
3939 found
= TREE_VALUE (found
);
3948 /* This type is a "partial instantiation" if any of the template
3949 arguments still inolve template parameters. Note that we set
3950 IS_PARTIAL_INSTANTIATION for partial specializations as
3952 is_partial_instantiation
= uses_template_parms (arglist
);
3954 if (!is_partial_instantiation
3955 && !PRIMARY_TEMPLATE_P (template)
3956 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
3959 found
= xref_tag_from_type (TREE_TYPE (template),
3960 DECL_NAME (template),
3965 /* Since we didn't find the type, we'll have to create it.
3966 Since we'll be saving this type on the
3967 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3968 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3970 /* Create the type. */
3971 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
3973 if (!is_partial_instantiation
)
3974 t
= start_enum (TYPE_IDENTIFIER (template_type
));
3976 /* We don't want to call start_enum for this type, since
3977 the values for the enumeration constants may involve
3978 template parameters. And, no one should be interested
3979 in the enumeration constants for such a type. */
3980 t
= make_node (ENUMERAL_TYPE
);
3984 t
= make_lang_type (TREE_CODE (template_type
));
3985 CLASSTYPE_DECLARED_CLASS (t
)
3986 = CLASSTYPE_DECLARED_CLASS (template_type
);
3987 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
3988 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
3989 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
3992 /* If we called start_enum above, this information will already
3996 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
3998 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
3999 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
4000 TYPE_STUB_DECL (t
) = type_decl
;
4001 DECL_SOURCE_FILE (type_decl
)
4002 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type
));
4003 DECL_SOURCE_LINE (type_decl
)
4004 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type
));
4007 type_decl
= TYPE_NAME (t
);
4009 /* Set up the template information. We have to figure out which
4010 template is the immediate parent if this is a full
4012 if (parm_depth
== 1 || is_partial_instantiation
4013 || !PRIMARY_TEMPLATE_P (template))
4014 /* This case is easy; there are no member templates involved. */
4018 /* This is a full instantiation of a member template. There
4019 should be some partial instantiation of which this is an
4022 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4023 found
; found
= TREE_CHAIN (found
))
4026 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4028 /* We only want partial instantiations, here, not
4029 specializations or full instantiations. */
4030 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4031 || !uses_template_parms (TREE_VALUE (found
)))
4034 /* Temporarily reduce by one the number of levels in the
4035 ARGLIST and in FOUND so as to avoid comparing the
4036 last set of arguments. */
4037 TREE_VEC_LENGTH (arglist
)--;
4038 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4040 /* See if the arguments match. If they do, then TMPL is
4041 the partial instantiation we want. */
4042 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4044 /* Restore the argument vectors to their full size. */
4045 TREE_VEC_LENGTH (arglist
)++;
4046 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4056 my_friendly_abort (0);
4059 SET_TYPE_TEMPLATE_INFO (t
,
4060 tree_cons (found
, arglist
, NULL_TREE
));
4061 DECL_TEMPLATE_INSTANTIATIONS (template)
4062 = tree_cons (arglist
, t
,
4063 DECL_TEMPLATE_INSTANTIATIONS (template));
4065 if (TREE_CODE (t
) == ENUMERAL_TYPE
4066 && !is_partial_instantiation
)
4067 /* Now that the type has been registered on the instantiations
4068 list, we set up the enumerators. Because the enumeration
4069 constants may involve the enumeration type itself, we make
4070 sure to register the type first, and then create the
4071 constants. That way, doing tsubst_expr for the enumeration
4072 constants won't result in recursive calls here; we'll find
4073 the instantiation and exit above. */
4074 tsubst_enum (template_type
, t
, arglist
);
4076 /* We're done with the permanent obstack, now. */
4078 /* We're also done with the momentary allocation we started
4082 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4084 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4085 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4086 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
4087 if (!is_partial_instantiation
)
4089 DECL_ASSEMBLER_NAME (type_decl
)
4090 = get_identifier (build_overload_name (t
, 1, 1));
4092 /* For backwards compatibility; code that uses
4093 -fexternal-templates expects looking up a template to
4094 instantiate it. I think DDD still relies on this.
4095 (jason 8/20/1998) */
4096 if (TREE_CODE (t
) != ENUMERAL_TYPE
4097 && flag_external_templates
4098 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4099 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4100 add_pending_template (t
);
4103 /* If the type makes use of template parameters, the
4104 code that generates debugging information will crash. */
4105 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4111 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
4112 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
4113 If FN returns non-zero, the iteration is terminated, and
4114 for_each_template_parm returns 1. Otherwise, the iteration
4115 continues. If FN never returns a non-zero value, the value
4116 returned by for_each_template_parm is 0. If FN is NULL, it is
4117 considered to be the function which always returns 1. */
4120 for_each_template_parm (t
, fn
, data
)
4128 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't'
4129 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
))
4132 switch (TREE_CODE (t
))
4136 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4137 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4139 case IDENTIFIER_NODE
:
4140 if (!IDENTIFIER_TEMPLATE (t
))
4142 my_friendly_abort (42);
4144 /* aggregates of tree nodes */
4147 int i
= TREE_VEC_LENGTH (t
);
4149 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
4154 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
4155 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
4157 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
4160 if (for_each_template_parm (OVL_FUNCTION (t
), fn
, data
))
4162 return for_each_template_parm (OVL_CHAIN (t
), fn
, data
);
4164 /* constructed type nodes */
4166 case REFERENCE_TYPE
:
4167 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4170 if (TYPE_PTRMEMFUNC_FLAG (t
))
4171 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
4177 if (! TYPE_TEMPLATE_INFO (t
))
4179 return for_each_template_parm (TREE_VALUE
4180 (TYPE_TEMPLATE_INFO (t
)),
4183 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
4188 /* Check the parameter types. Since default arguments are not
4189 instantiated until they are needed, the TYPE_ARG_TYPES may
4190 contain expressions that involve template parameters. But,
4191 no-one should be looking at them yet. And, once they're
4192 instantiated, they don't contain template parameters, so
4193 there's no point in looking at them then, either. */
4197 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
4198 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
))
4202 /* Check the return type, too. */
4203 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4206 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
4208 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4210 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
4212 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4216 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4219 /* A template template parameter is encountered */
4220 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
4221 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4222 /* Already substituted template template parameter */
4226 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
4228 goto check_type_and_context
;
4232 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
4233 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
4237 check_type_and_context
:
4238 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
4240 if (DECL_CONTEXT (t
)
4241 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
4246 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4247 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4250 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4252 /* template parm nodes */
4253 case TEMPLATE_TEMPLATE_PARM
:
4254 /* Record template parameters such as `T' inside `TT<T>'. */
4255 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
)
4256 && for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
))
4258 case TEMPLATE_TYPE_PARM
:
4259 case TEMPLATE_PARM_INDEX
:
4261 return (*fn
)(t
, data
);
4265 /* simple type nodes */
4267 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
4269 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
4275 case NAMESPACE_DECL
:
4286 /* Non-error_mark_node ERROR_MARKs are bad things. */
4287 my_friendly_assert (t
== error_mark_node
, 274);
4292 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
4295 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4298 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4299 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4300 (TREE_TYPE (t
)), fn
, data
);
4301 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
4305 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4310 return (for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
)
4311 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
),
4316 /* If there's no type, then this thing must be some expression
4317 involving template parameters. */
4318 if (!fn
&& !TREE_TYPE (t
))
4320 if (TREE_CODE (t
) == COMPONENT_REF
)
4321 return (for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
)
4322 || for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
));
4324 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
4328 case REINTERPRET_CAST_EXPR
:
4329 case CONST_CAST_EXPR
:
4330 case STATIC_CAST_EXPR
:
4331 case DYNAMIC_CAST_EXPR
:
4336 case PSEUDO_DTOR_EXPR
:
4342 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
4350 for (i
= first_rtl_op (TREE_CODE (t
)); --i
>= 0;)
4351 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
4358 sorry ("testing %s for template parms",
4359 tree_code_name
[(int) TREE_CODE (t
)]);
4360 my_friendly_abort (82);
4367 uses_template_parms (t
)
4370 return for_each_template_parm (t
, 0, 0);
4373 static struct tinst_level
*current_tinst_level
;
4374 static struct tinst_level
*free_tinst_level
;
4375 static int tinst_depth
;
4376 extern int max_tinst_depth
;
4377 #ifdef GATHER_STATISTICS
4380 int tinst_level_tick
;
4381 int last_template_error_tick
;
4383 /* Print out all the template instantiations that we are currently
4384 working on. If ERR, we are being called from cp_thing, so do
4385 the right thing for an error message. */
4388 print_template_context (err
)
4391 struct tinst_level
*p
= current_tinst_level
;
4393 char *file
= input_filename
;
4397 if (current_function_decl
!= p
->decl
4398 && current_function_decl
!= NULL_TREE
)
4399 /* We can get here during the processing of some synthesized
4400 method. Then, p->decl will be the function that's causing
4405 if (current_function_decl
== p
->decl
)
4406 /* Avoid redundancy with the the "In function" line. */;
4408 fprintf (stderr
, "%s: In instantiation of `%s':\n",
4409 file
, decl_as_string (p
->decl
, 0));
4417 for (; p
; p
= p
->next
)
4419 fprintf (stderr
, "%s:%d: instantiated from `%s'\n", file
, line
,
4420 decl_as_string (p
->decl
, 0));
4424 fprintf (stderr
, "%s:%d: instantiated from here\n", file
, line
);
4427 /* Called from cp_thing to print the template context for an error. */
4430 maybe_print_template_context ()
4432 if (last_template_error_tick
== tinst_level_tick
4433 || current_tinst_level
== 0)
4436 last_template_error_tick
= tinst_level_tick
;
4437 print_template_context (1);
4441 push_tinst_level (d
)
4444 struct tinst_level
*new;
4446 if (tinst_depth
>= max_tinst_depth
)
4448 /* If the instantiation in question still has unbound template parms,
4449 we don't really care if we can't instantiate it, so just return.
4450 This happens with base instantiation for implicit `typename'. */
4451 if (uses_template_parms (d
))
4454 last_template_error_tick
= tinst_level_tick
;
4455 error ("template instantiation depth exceeds maximum of %d",
4457 error (" (use -ftemplate-depth-NN to increase the maximum)");
4458 cp_error (" instantiating `%D'", d
);
4460 print_template_context (0);
4465 if (free_tinst_level
)
4467 new = free_tinst_level
;
4468 free_tinst_level
= new->next
;
4471 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
4475 new->file
= input_filename
;
4476 new->next
= current_tinst_level
;
4477 current_tinst_level
= new;
4480 #ifdef GATHER_STATISTICS
4481 if (tinst_depth
> depth_reached
)
4482 depth_reached
= tinst_depth
;
4492 struct tinst_level
*old
= current_tinst_level
;
4494 /* Restore the filename and line number stashed away when we started
4495 this instantiation. */
4497 input_filename
= old
->file
;
4498 extract_interface_info ();
4500 current_tinst_level
= old
->next
;
4501 old
->next
= free_tinst_level
;
4502 free_tinst_level
= old
;
4507 struct tinst_level
*
4510 struct tinst_level
*p
= current_tinst_level
;
4513 for (; p
->next
; p
= p
->next
)
4518 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4519 vector of template arguments, as for tsubst.
4521 Returns an appropriate tsbust'd friend declaration. */
4524 tsubst_friend_function (decl
, args
)
4530 char *file
= input_filename
;
4532 lineno
= DECL_SOURCE_LINE (decl
);
4533 input_filename
= DECL_SOURCE_FILE (decl
);
4535 if (TREE_CODE (decl
) == FUNCTION_DECL
4536 && DECL_TEMPLATE_INSTANTIATION (decl
)
4537 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
4538 /* This was a friend declared with an explicit template
4539 argument list, e.g.:
4543 to indicate that f was a template instantiation, not a new
4544 function declaration. Now, we have to figure out what
4545 instantiation of what template. */
4552 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
4553 args
, /*complain=*/1,
4555 tsubst (DECL_TI_ARGS (decl
),
4556 args
, /*complain=*/1,
4558 /* FIXME: The decl we create via the next tsubst could be
4559 created on a temporary obstack. */
4560 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4561 tmpl
= determine_specialization (template_id
, new_friend
,
4563 /*need_member_template=*/0);
4564 new_friend
= instantiate_template (tmpl
, new_args
);
4568 new_friend
= tsubst (decl
, args
, /*complain=*/1, NULL_TREE
);
4570 /* The NEW_FRIEND will look like an instantiation, to the
4571 compiler, but is not an instantiation from the point of view of
4572 the language. For example, we might have had:
4574 template <class T> struct S {
4575 template <class U> friend void f(T, U);
4578 Then, in S<int>, template <class U> void f(int, U) is not an
4579 instantiation of anything. */
4580 DECL_USE_TEMPLATE (new_friend
) = 0;
4581 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4582 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
4584 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4585 tsubst will have resulted in a call to
4586 set_mangled_name_for_template_decl. But, the function is not a
4587 template instantiation and should not be mangled like one.
4588 Therefore, we remangle the function name. We don't have to do
4589 this if the NEW_FRIEND is a template since
4590 set_mangled_name_for_template_decl doesn't do anything if the
4591 function declaration still uses template arguments. */
4592 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
4594 set_mangled_name_for_decl (new_friend
);
4595 DECL_RTL (new_friend
) = 0;
4596 make_decl_rtl (new_friend
, NULL_PTR
, 1);
4599 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
4602 tree new_friend_template_info
;
4603 tree new_friend_result_template_info
;
4605 int new_friend_is_defn
;
4607 /* We must save some information from NEW_FRIEND before calling
4608 duplicate decls since that function will free NEW_FRIEND if
4610 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
4611 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
4613 /* This declaration is a `primary' template. */
4614 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
4617 = DECL_INITIAL (DECL_RESULT (new_friend
)) != NULL_TREE
;
4618 new_friend_result_template_info
4619 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend
));
4623 new_friend_is_defn
= DECL_INITIAL (new_friend
) != NULL_TREE
;
4624 new_friend_result_template_info
= NULL_TREE
;
4627 /* Inside pushdecl_namespace_level, we will push into the
4628 current namespace. However, the friend function should
4629 tyically go into the namespace of the template. */
4630 ns
= decl_namespace_context (new_friend
);
4631 push_nested_namespace (ns
);
4632 old_decl
= pushdecl_namespace_level (new_friend
);
4633 pop_nested_namespace (ns
);
4635 if (old_decl
!= new_friend
)
4637 /* This new friend declaration matched an existing
4638 declaration. For example, given:
4640 template <class T> void f(T);
4641 template <class U> class C {
4642 template <class T> friend void f(T) {}
4645 the friend declaration actually provides the definition
4646 of `f', once C has been instantiated for some type. So,
4647 old_decl will be the out-of-class template declaration,
4648 while new_friend is the in-class definition.
4650 But, if `f' was called before this point, the
4651 instantiation of `f' will have DECL_TI_ARGS corresponding
4652 to `T' but not to `U', references to which might appear
4653 in the definition of `f'. Previously, the most general
4654 template for an instantiation of `f' was the out-of-class
4655 version; now it is the in-class version. Therefore, we
4656 run through all specialization of `f', adding to their
4657 DECL_TI_ARGS appropriately. In particular, they need a
4658 new set of outer arguments, corresponding to the
4659 arguments for this class instantiation.
4661 The same situation can arise with something like this:
4664 template <class T> class C {
4668 when `C<int>' is instantiated. Now, `f(int)' is defined
4671 if (!new_friend_is_defn
)
4672 /* On the other hand, if the in-class declaration does
4673 *not* provide a definition, then we don't want to alter
4674 existing definitions. We can just leave everything
4679 /* Overwrite whatever template info was there before, if
4680 any, with the new template information pertaining to
4682 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
4684 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
4685 /* duplicate_decls will take care of this case. */
4690 tree new_friend_args
;
4692 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl
))
4693 = new_friend_result_template_info
;
4695 new_friend_args
= TI_ARGS (new_friend_template_info
);
4696 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
4700 tree spec
= TREE_VALUE (t
);
4703 = add_outermost_template_args (new_friend_args
,
4704 DECL_TI_ARGS (spec
));
4707 /* Now, since specializations are always supposed to
4708 hang off of the most general template, we must move
4710 t
= most_general_template (old_decl
);
4713 DECL_TEMPLATE_SPECIALIZATIONS (t
)
4714 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
4715 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
4716 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
4721 /* The information from NEW_FRIEND has been merged into OLD_DECL
4722 by duplicate_decls. */
4723 new_friend
= old_decl
;
4726 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
4728 /* Check to see that the declaration is really present, and,
4729 possibly obtain an improved declaration. */
4730 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
4739 input_filename
= file
;
4743 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4744 template arguments, as for tsubst.
4746 Returns an appropriate tsbust'd friend type. */
4749 tsubst_friend_class (friend_tmpl
, args
)
4756 /* First, we look for a class template. */
4757 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/0);
4759 /* But, if we don't find one, it might be because we're in a
4760 situation like this:
4768 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4769 for `S<int>', not the TEMPLATE_DECL. */
4770 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
4772 tmpl
= lookup_name (DECL_NAME (friend_tmpl
), /*prefer_type=*/1);
4773 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
4776 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
4778 /* The friend template has already been declared. Just
4779 check to see that the declarations match, and install any new
4780 default parameters. We must tsubst the default parameters,
4781 of course. We only need the innermost template parameters
4782 because that is all that redeclare_class_template will look
4785 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
4786 args
, /*complain=*/1);
4787 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
4788 friend_type
= TREE_TYPE (tmpl
);
4792 /* The friend template has not already been declared. In this
4793 case, the instantiation of the template class will cause the
4794 injection of this template into the global scope. */
4795 tmpl
= tsubst (friend_tmpl
, args
, /*complain=*/1, NULL_TREE
);
4797 /* The new TMPL is not an instantiation of anything, so we
4798 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4799 the new type because that is supposed to be the corresponding
4800 template decl, i.e., TMPL. */
4801 DECL_USE_TEMPLATE (tmpl
) = 0;
4802 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
4803 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
4805 /* Inject this template into the global scope. */
4806 friend_type
= TREE_TYPE (pushdecl_top_level (tmpl
));
4813 instantiate_class_template (type
)
4816 tree
template, args
, pattern
, t
;
4819 if (type
== error_mark_node
)
4820 return error_mark_node
;
4822 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
4825 /* We want to allocate temporary vectors of template arguments and
4826 template argument expressions on the momentary obstack, not on
4827 the expression obstack. Otherwise, all the space allocated in
4828 argument coercion and such is simply lost. */
4831 /* Figure out which template is being instantiated. */
4832 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
4833 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
4835 /* Figure out which arguments are being used to do the
4837 args
= CLASSTYPE_TI_ARGS (type
);
4838 PARTIAL_INSTANTIATION_P (type
) = uses_template_parms (args
);
4840 if (pedantic
&& PARTIAL_INSTANTIATION_P (type
))
4841 /* If this is a partial instantiation, then we can't instantiate
4842 the type; there's no telling whether or not one of the
4843 template parameters might eventually be instantiated to some
4844 value that results in a specialization being used. For
4856 Now, the `S<U>' in `f<int>' is the specialization, not an
4857 instantiation of the original template. */
4860 /* Determine what specialization of the original template to
4862 if (PARTIAL_INSTANTIATION_P (type
))
4863 /* There's no telling which specialization is appropriate at this
4864 point. Since all peeking at the innards of this partial
4865 instantiation are extensions (like the "implicit typename"
4866 extension, which allows users to omit the keyword `typename' on
4867 names that are declared as types in template base classes), we
4868 are free to do what we please.
4870 Trying to figure out which partial instantiation to use can
4871 cause a crash. (Some of the template arguments don't even have
4872 types.) So, we just use the most general version. */
4876 t
= most_specialized_class (template, args
);
4878 if (t
== error_mark_node
)
4880 const char *str
= "candidates are:";
4881 cp_error ("ambiguous class template instantiation for `%#T'", type
);
4882 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
;
4885 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4888 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
4892 TYPE_BEING_DEFINED (type
) = 1;
4893 type
= error_mark_node
;
4899 pattern
= TREE_TYPE (t
);
4901 pattern
= TREE_TYPE (template);
4903 /* If the template we're instantiating is incomplete, then clearly
4904 there's nothing we can do. */
4905 if (TYPE_SIZE (pattern
) == NULL_TREE
)
4908 /* If this is a partial instantiation, don't tsubst anything. We will
4909 only use this type for implicit typename, so the actual contents don't
4910 matter. All that matters is whether a particular name is a type. */
4911 if (PARTIAL_INSTANTIATION_P (type
))
4913 /* The fields set here must be kept in sync with those cleared
4914 in begin_class_definition. */
4915 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
4916 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
4917 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
4918 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
4919 /* Pretend that the type is complete, so that we will look
4920 inside it during name lookup and such. */
4921 TYPE_SIZE (type
) = integer_zero_node
;
4925 /* If we've recursively instantiated too many templates, stop. */
4926 if (! push_tinst_level (type
))
4929 /* Now we're really doing the instantiation. Mark the type as in
4930 the process of being defined. */
4931 TYPE_BEING_DEFINED (type
) = 1;
4933 maybe_push_to_top_level (uses_template_parms (type
));
4937 /* This TYPE is actually a instantiation of of a partial
4938 specialization. We replace the innermost set of ARGS with
4939 the arguments appropriate for substitution. For example,
4942 template <class T> struct S {};
4943 template <class T> struct S<T*> {};
4945 and supposing that we are instantiating S<int*>, ARGS will
4946 present be {int*} but we need {int}. */
4948 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
4951 /* If there were multiple levels in ARGS, replacing the
4952 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4953 want, so we make a copy first. */
4954 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
4956 args
= copy_node (args
);
4957 SET_TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
), inner_args
);
4963 if (flag_external_templates
)
4965 if (flag_alt_external_templates
)
4967 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
4968 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
4969 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4970 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4971 && CLASSTYPE_INTERFACE_KNOWN (type
));
4975 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
4976 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4977 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
4978 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
4979 = (! CLASSTYPE_INTERFACE_ONLY (type
)
4980 && CLASSTYPE_INTERFACE_KNOWN (type
));
4985 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
4986 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
4989 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
4990 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
4991 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
4992 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
4993 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
4994 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
4995 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
4996 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
4997 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
4998 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
4999 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
5000 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5001 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5002 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5003 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5004 TYPE_USES_COMPLEX_INHERITANCE (type
)
5005 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
5006 TYPE_USES_MULTIPLE_INHERITANCE (type
)
5007 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
5008 TYPE_USES_VIRTUAL_BASECLASSES (type
)
5009 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
5010 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5011 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5012 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5013 if (ANON_AGGR_TYPE_P (pattern
))
5014 SET_ANON_AGGR_TYPE_P (type
);
5016 if (TYPE_BINFO_BASETYPES (pattern
))
5018 tree base_list
= NULL_TREE
;
5019 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
5022 /* Substitute into each of the bases to determine the actual
5024 for (i
= 0; i
< TREE_VEC_LENGTH (pbases
); ++i
)
5030 pbase
= TREE_VEC_ELT (pbases
, i
);
5032 /* Substitue to figure out the base class. */
5033 base
= tsubst (BINFO_TYPE (pbase
), args
,
5034 /*complain=*/1, NULL_TREE
);
5035 if (base
== error_mark_node
)
5038 /* Calculate the correct access node. */
5039 if (TREE_VIA_VIRTUAL (pbase
))
5041 if (TREE_VIA_PUBLIC (pbase
))
5042 access
= access_public_virtual_node
;
5043 else if (TREE_VIA_PROTECTED (pbase
))
5044 access
= access_protected_virtual_node
;
5046 access
= access_private_virtual_node
;
5050 if (TREE_VIA_PUBLIC (pbase
))
5051 access
= access_public_node
;
5052 else if (TREE_VIA_PROTECTED (pbase
))
5053 access
= access_protected_node
;
5055 access
= access_private_node
;
5058 base_list
= tree_cons (access
, base
, base_list
);
5061 /* The list is now in reverse order; correct that. */
5062 base_list
= nreverse (base_list
);
5064 /* Now call xref_basetypes to set up all the base-class
5066 xref_basetypes (TREE_CODE (pattern
) == RECORD_TYPE
5067 ? (CLASSTYPE_DECLARED_CLASS (pattern
)
5068 ? class_type_node
: record_type_node
)
5070 DECL_NAME (TYPE_NAME (pattern
)),
5075 /* Now that our base classes are set up, enter the scope of the
5076 class, so that name lookups into base classes, etc. will work
5077 corectly. This is precisely analagous to what we do in
5078 begin_class_definition when defining an ordinary non-template
5080 pushclass (type
, 1);
5082 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
5084 tree tag
= TREE_VALUE (t
);
5085 tree name
= TYPE_IDENTIFIER (tag
);
5088 newtag
= tsubst (tag
, args
, /*complain=*/1, NULL_TREE
);
5089 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5091 if (TYPE_LANG_SPECIFIC (tag
) && CLASSTYPE_IS_TEMPLATE (tag
))
5092 /* Unfortunately, lookup_template_class sets
5093 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5094 instantiation (i.e., for the type of a member template
5095 class nested within a template class.) This behavior is
5096 required for maybe_process_partial_specialization to work
5097 correctly, but is not accurate in this case; the TAG is not
5098 an instantiation of anything. (The corresponding
5099 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5100 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5102 /* Now, we call pushtag to put this NEWTAG into the scope of
5103 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5104 pushtag calling push_template_decl. We don't have to do
5105 this for enums because it will already have been done in
5108 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5109 pushtag (name
, newtag
, /*globalize=*/0);
5113 /* Don't replace enum constants here. */
5114 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
5115 if (TREE_CODE (t
) != CONST_DECL
)
5119 /* The the file and line for this declaration, to assist in
5120 error message reporting. Since we called push_tinst_level
5121 above, we don't need to restore these. */
5122 lineno
= DECL_SOURCE_LINE (t
);
5123 input_filename
= DECL_SOURCE_FILE (t
);
5125 r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5126 if (TREE_CODE (r
) == VAR_DECL
)
5130 if (DECL_DEFINED_IN_CLASS_P (r
))
5131 init
= tsubst_expr (DECL_INITIAL (t
), args
,
5132 /*complain=*/1, NULL_TREE
);
5136 finish_static_data_member_decl (r
, init
,
5137 /*asmspec_tree=*/NULL_TREE
,
5141 if (DECL_DEFINED_IN_CLASS_P (r
))
5142 check_static_variable_definition (r
, TREE_TYPE (r
));
5145 /* R will have a TREE_CHAIN if and only if it has already been
5146 processed by finish_member_declaration. This can happen
5147 if, for example, it is a TYPE_DECL for a class-scoped
5148 ENUMERAL_TYPE; such a thing will already have been added to
5149 the field list by tsubst_enum above. */
5150 if (!TREE_CHAIN (r
))
5152 set_current_access_from_decl (r
);
5153 finish_member_declaration (r
);
5157 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5158 for this instantiation. */
5159 for (t
= TYPE_METHODS (pattern
); t
; t
= TREE_CHAIN (t
))
5161 tree r
= tsubst (t
, args
, /*complain=*/1, NULL_TREE
);
5162 set_current_access_from_decl (r
);
5163 finish_member_declaration (r
);
5166 /* Construct the DECL_FRIENDLIST for the new class type. */
5167 typedecl
= TYPE_MAIN_DECL (type
);
5168 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
5174 for (friends
= TREE_VALUE (t
);
5175 friends
!= NULL_TREE
;
5176 friends
= TREE_CHAIN (friends
))
5177 if (TREE_PURPOSE (friends
) == error_mark_node
)
5179 tsubst_friend_function (TREE_VALUE (friends
),
5183 tsubst_copy (TREE_PURPOSE (t
), args
,
5184 /*complain=*/1, NULL_TREE
),
5185 tsubst (TREE_PURPOSE (friends
), args
,
5186 /*complain=*/1, NULL_TREE
));
5189 for (t
= CLASSTYPE_FRIEND_CLASSES (pattern
);
5193 tree friend_type
= TREE_VALUE (t
);
5194 tree new_friend_type
;
5196 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5197 new_friend_type
= tsubst_friend_class (friend_type
, args
);
5198 else if (uses_template_parms (friend_type
))
5199 new_friend_type
= tsubst (friend_type
, args
, /*complain=*/1,
5203 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
5205 /* The call to xref_tag_from_type does injection for friend
5207 push_nested_namespace (ns
);
5209 xref_tag_from_type (friend_type
, NULL_TREE
, 1);
5210 pop_nested_namespace (ns
);
5213 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5214 /* Trick make_friend_class into realizing that the friend
5215 we're adding is a template, not an ordinary class. It's
5216 important that we use make_friend_class since it will
5217 perform some error-checking and output cross-reference
5219 ++processing_template_decl
;
5221 make_friend_class (type
, new_friend_type
);
5223 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5224 --processing_template_decl
;
5227 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
5228 if (TREE_CODE (t
) == FIELD_DECL
)
5230 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
5231 require_complete_type (t
);
5234 /* Set the file and line number information to whatever is given for
5235 the class itself. This puts error messages involving generated
5236 implicit functions at a predictable point, and the same point
5237 that would be used for non-template classes. */
5238 lineno
= DECL_SOURCE_LINE (typedecl
);
5239 input_filename
= DECL_SOURCE_FILE (typedecl
);
5241 unreverse_member_declarations (type
);
5242 finish_struct_1 (type
);
5243 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
5245 /* Clear this now so repo_template_used is happy. */
5246 TYPE_BEING_DEFINED (type
) = 0;
5247 repo_template_used (type
);
5249 /* Now that the class is complete, instantiate default arguments for
5250 any member functions. We don't do this earlier because the
5251 default arguments may reference members of the class. */
5252 if (!PRIMARY_TEMPLATE_P (template))
5253 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
5254 if (TREE_CODE (t
) == FUNCTION_DECL
5255 /* Implicitly generated member functions will not have tmplate
5256 information; they are not instantiations, but instead are
5257 created "fresh" for each instantiation. */
5258 && DECL_TEMPLATE_INFO (t
))
5259 tsubst_default_arguments (t
);
5262 pop_from_top_level ();
5275 if (t1
== NULL_TREE
)
5276 return t2
== NULL_TREE
;
5277 if (t2
== NULL_TREE
)
5279 /* Don't care if one declares its arg const and the other doesn't -- the
5280 main variant of the arg type is all that matters. */
5281 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
5282 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
5284 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
5287 /* If arg is a non-type template parameter that does not depend on template
5288 arguments, fold it like we weren't in the body of a template. */
5291 maybe_fold_nontype_arg (arg
)
5294 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
5295 && !uses_template_parms (arg
))
5297 /* Sometimes, one of the args was an expression involving a
5298 template constant parameter, like N - 1. Now that we've
5299 tsubst'd, we might have something like 2 - 1. This will
5300 confuse lookup_template_class, so we do constant folding
5301 here. We have to unset processing_template_decl, to
5302 fool build_expr_from_tree() into building an actual
5305 int saved_processing_template_decl
= processing_template_decl
;
5306 processing_template_decl
= 0;
5307 arg
= fold (build_expr_from_tree (arg
));
5308 processing_template_decl
= saved_processing_template_decl
;
5313 /* Return the TREE_VEC with the arguments for the innermost template header,
5314 where ARGS is either that or the VEC of VECs for all the
5318 innermost_args (args
)
5321 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
5324 /* Substitute ARGS into the vector of template arguments T. */
5327 tsubst_template_arg_vector (t
, args
, complain
)
5332 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
5333 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
5335 bzero ((char *) elts
, len
* sizeof (tree
));
5337 for (i
= 0; i
< len
; i
++)
5339 if (TREE_VEC_ELT (t
, i
) != NULL_TREE
5340 && TREE_CODE (TREE_VEC_ELT (t
, i
)) == TREE_VEC
)
5341 elts
[i
] = tsubst_template_arg_vector (TREE_VEC_ELT (t
, i
),
5344 elts
[i
] = maybe_fold_nontype_arg
5345 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, complain
,
5348 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
5355 t
= make_temp_vec (len
);
5356 for (i
= 0; i
< len
; i
++)
5357 TREE_VEC_ELT (t
, i
) = elts
[i
];
5362 /* Return the result of substituting ARGS into the template parameters
5363 given by PARMS. If there are m levels of ARGS and m + n levels of
5364 PARMS, then the result will contain n levels of PARMS. For
5365 example, if PARMS is `template <class T> template <class U>
5366 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5367 result will be `template <int*, double, class V>'. */
5370 tsubst_template_parms (parms
, args
, complain
)
5378 for (new_parms
= &r
;
5379 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
5380 new_parms
= &(TREE_CHAIN (*new_parms
)),
5381 parms
= TREE_CHAIN (parms
))
5384 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
5387 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
5389 tree default_value
=
5390 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5392 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
5394 TREE_VEC_ELT (new_vec
, i
)
5395 = build_tree_list (tsubst (default_value
, args
, complain
,
5397 tsubst (parm_decl
, args
, complain
,
5402 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms
)
5403 - TMPL_ARGS_DEPTH (args
))),
5404 new_vec
, NULL_TREE
);
5410 /* Substitute the ARGS into the indicated aggregate (or enumeration)
5411 type T. If T is not an aggregate or enumeration type, it is
5412 handled as if by tsubst. IN_DECL is as for tsubst. If
5413 ENTERING_SCOPE is non-zero, T is the context for a template which
5414 we are presently tsubst'ing. Return the subsituted value. */
5417 tsubst_aggr_type (t
, args
, complain
, in_decl
, entering_scope
)
5427 switch (TREE_CODE (t
))
5430 if (TYPE_PTRMEMFUNC_P (t
))
5432 tree r
= build_ptrmemfunc_type
5433 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
));
5434 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5438 /* else fall through */
5441 if (TYPE_TEMPLATE_INFO (t
))
5447 /* First, determine the context for the type we are looking
5449 if (TYPE_CONTEXT (t
) != NULL_TREE
)
5450 context
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
,
5452 in_decl
, /*entering_scope=*/1);
5454 context
= NULL_TREE
;
5456 /* Then, figure out what arguments are appropriate for the
5457 type we are trying to find. For example, given:
5459 template <class T> struct S;
5460 template <class T, class U> void f(T, U) { S<U> su; }
5462 and supposing that we are instantiating f<int, double>,
5463 then our ARGS will be {int, double}, but, when looking up
5464 S we only want {double}. */
5466 argvec
= tsubst_template_arg_vector (TYPE_TI_ARGS (t
), args
,
5469 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
5473 return cp_build_qualified_type_real (r
, TYPE_QUALS (t
),
5477 /* This is not a template type, so there's nothing to do. */
5481 return tsubst (t
, args
, complain
, in_decl
);
5485 /* Substitute into the default argument ARG (a default argument for
5486 FN), which has the indicated TYPE. */
5489 tsubst_default_argument (fn
, type
, arg
)
5494 /* This default argument came from a template. Instantiate the
5495 default argument here, not in tsubst. In the case of
5504 we must be careful to do name lookup in the scope of S<T>,
5505 rather than in the current class. */
5506 if (DECL_CLASS_SCOPE_P (fn
))
5507 pushclass (DECL_REAL_CONTEXT (fn
), 2);
5509 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
), /*complain=*/1, NULL_TREE
);
5511 if (DECL_CLASS_SCOPE_P (fn
))
5514 /* Make sure the default argument is reasonable. */
5515 arg
= check_default_argument (type
, arg
);
5520 /* Substitute into all the default arguments for FN. */
5523 tsubst_default_arguments (fn
)
5529 tmpl_args
= DECL_TI_ARGS (fn
);
5531 /* If this function is not yet instantiated, we certainly don't need
5532 its default arguments. */
5533 if (uses_template_parms (tmpl_args
))
5536 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5538 arg
= TREE_CHAIN (arg
))
5539 if (TREE_PURPOSE (arg
))
5540 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
5542 TREE_PURPOSE (arg
));
5545 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5546 (already computed) substitution of ARGS into TREE_TYPE (T), if
5547 appropriate. Return the result of the substitution. IN_DECL is as
5551 tsubst_decl (t
, args
, type
, in_decl
)
5558 char* saved_filename
;
5561 /* Set the filename and linenumber to improve error-reporting. */
5562 saved_lineno
= lineno
;
5563 saved_filename
= input_filename
;
5564 lineno
= DECL_SOURCE_LINE (t
);
5565 input_filename
= DECL_SOURCE_FILE (t
);
5567 switch (TREE_CODE (t
))
5571 /* We can get here when processing a member template function
5572 of a template class. */
5573 tree decl
= DECL_TEMPLATE_RESULT (t
);
5575 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
5577 if (!is_template_template_parm
)
5579 /* We might already have an instance of this template.
5580 The ARGS are for the surrounding class type, so the
5581 full args contain the tsubst'd args for the context,
5582 plus the innermost args from the template decl. */
5583 tree tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
5584 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
5585 : DECL_TI_ARGS (DECL_RESULT (t
));
5589 full_args
= tsubst_template_arg_vector (tmpl_args
, args
,
5592 /* tsubst_template_arg_vector doesn't copy the vector if
5593 nothing changed. But, *something* should have
5595 my_friendly_assert (full_args
!= tmpl_args
, 0);
5597 spec
= retrieve_specialization (t
, full_args
);
5599 if (spec
!= NULL_TREE
)
5606 /* Make a new template decl. It will be similar to the
5607 original, but will record the current template arguments.
5608 We also create a new function declaration, which is just
5609 like the old one, but points to this new template, rather
5610 than the old one. */
5613 my_friendly_assert (DECL_LANG_SPECIFIC (r
) != 0, 0);
5614 TREE_CHAIN (r
) = NULL_TREE
;
5616 if (is_template_template_parm
)
5618 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5619 DECL_RESULT (r
) = new_decl
;
5620 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5625 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1,
5626 in_decl
, /*entering_scope=*/1);
5627 DECL_CLASS_CONTEXT (r
)
5628 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5629 /*complain=*/1, in_decl
,
5630 /*entering_scope=*/1);
5631 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
5633 if (TREE_CODE (decl
) == TYPE_DECL
)
5635 tree new_type
= tsubst (TREE_TYPE (t
), args
,
5636 /*complain=*/1, in_decl
);
5637 TREE_TYPE (r
) = new_type
;
5638 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
5639 DECL_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
5640 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
5644 tree new_decl
= tsubst (decl
, args
, /*complain=*/1, in_decl
);
5645 DECL_RESULT (r
) = new_decl
;
5646 DECL_TI_TEMPLATE (new_decl
) = r
;
5647 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
5648 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
5651 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5652 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
5653 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
5655 /* The template parameters for this new template are all the
5656 template parameters for the old template, except the
5657 outermost level of parameters. */
5658 DECL_TEMPLATE_PARMS (r
)
5659 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
5662 if (PRIMARY_TEMPLATE_P (t
))
5663 DECL_PRIMARY_TEMPLATE (r
) = r
;
5665 /* We don't partially instantiate partial specializations. */
5666 if (TREE_CODE (decl
) == TYPE_DECL
)
5669 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
5671 spec
= TREE_CHAIN (spec
))
5673 /* It helps to consider example here. Consider:
5684 Now, for example, we are instantiating S<int>::f(U u).
5685 We want to make a template:
5690 It will have a specialization, for the case U = int*, of
5694 void S<int>::f<int*>(int*);
5696 This specialization will be an instantiation of
5697 the specialization given in the declaration of S, with
5698 argument list int*. */
5700 tree fn
= TREE_VALUE (spec
);
5704 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
5705 /* Instantiations are on the same list, but they're of
5706 no concern to us. */
5709 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5710 /* A full specialization. There's no need to record
5714 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
5715 /*complain=*/1, in_decl
);
5716 new_fn
= tsubst (DECL_RESULT (most_general_template (fn
)),
5717 spec_args
, /*complain=*/1, in_decl
);
5718 DECL_TI_TEMPLATE (new_fn
) = fn
;
5719 register_specialization (new_fn
, r
,
5720 innermost_args (spec_args
));
5723 /* Record this partial instantiation. */
5724 register_specialization (r
, t
,
5725 DECL_TI_ARGS (DECL_RESULT (r
)));
5733 tree argvec
= NULL_TREE
;
5740 /* Nobody should be tsubst'ing into non-template functions. */
5741 my_friendly_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
5743 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
5747 /* Allocate template arguments on the momentary obstack,
5748 in case we don't need to keep them. */
5751 /* Calculate the most general template of which R is a
5752 specialization, and the complete set of arguments used to
5754 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
5756 = tsubst_template_arg_vector (DECL_TI_ARGS
5757 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
5758 args
, /*complain=*/1);
5760 /* Check to see if we already have this specialization. */
5761 spec
= retrieve_specialization (gen_tmpl
, argvec
);
5772 /* Here, we deal with the peculiar case:
5774 template <class T> struct S {
5775 template <class U> friend void f();
5777 template <class U> friend void f() {}
5779 template void f<double>();
5781 Here, the ARGS for the instantiation of will be {int,
5782 double}. But, we only need as many ARGS as there are
5783 levels of template parameters in CODE_PATTERN. We are
5784 careful not to get fooled into reducing the ARGS in
5787 template <class T> struct S { template <class U> void f(U); }
5788 template <class T> template <> void S<T>::f(int) {}
5790 which we can spot because the pattern will be a
5791 specialization in this case. */
5792 args_depth
= TMPL_ARGS_DEPTH (args
);
5794 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
5795 if (args_depth
> parms_depth
5796 && !DECL_TEMPLATE_SPECIALIZATION (t
))
5798 my_friendly_assert (DECL_FRIEND_P (t
), 0);
5800 if (parms_depth
> 1)
5804 args
= make_temp_vec (parms_depth
);
5805 for (i
= 0; i
< parms_depth
; ++i
)
5806 TREE_VEC_ELT (args
, i
) =
5807 TREE_VEC_ELT (args
, i
+ (args_depth
- parms_depth
));
5810 args
= TREE_VEC_ELT (args
, args_depth
- parms_depth
);
5815 /* This special case arises when we have something like this:
5817 template <class T> struct S {
5818 friend void f<int>(int, double);
5821 Here, the DECL_TI_TEMPLATE for the friend declaration
5822 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5823 being called from tsubst_friend_function, and we want
5824 only to create a new decl (R) with appropriate types so
5825 that we can call determine_specialization. */
5826 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t
))
5828 || (TREE_CODE (DECL_TI_TEMPLATE (t
))
5829 == IDENTIFIER_NODE
), 0);
5830 gen_tmpl
= NULL_TREE
;
5833 if (DECL_CLASS_SCOPE_P (t
))
5835 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
5839 ctx
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t
), args
,
5841 /*entering_scope=*/1);
5848 type
= tsubst (type
, args
, /*complain=*/1, in_decl
);
5850 /* We do NOT check for matching decls pushed separately at this
5851 point, as they may not represent instantiations of this
5852 template, and in any case are considered separate under the
5853 discrete model. Instead, see add_maybe_template. */
5857 DECL_USE_TEMPLATE (r
) = 0;
5858 TREE_TYPE (r
) = type
;
5861 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, /*complain=*/1, t
,
5862 /*entering_scope=*/1);
5863 DECL_CLASS_CONTEXT (r
) = ctx
;
5865 if (member
&& IDENTIFIER_TYPENAME_P (DECL_NAME (r
)))
5866 /* Type-conversion operator. Reconstruct the name, in
5867 case it's the name of one of the template's parameters. */
5868 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
5870 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
5872 DECL_MAIN_VARIANT (r
) = r
;
5873 DECL_RESULT (r
) = NULL_TREE
;
5875 TREE_STATIC (r
) = 0;
5876 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
5877 DECL_EXTERNAL (r
) = 1;
5878 DECL_INTERFACE_KNOWN (r
) = 0;
5879 DECL_DEFER_OUTPUT (r
) = 0;
5880 TREE_CHAIN (r
) = NULL_TREE
;
5881 DECL_PENDING_INLINE_INFO (r
) = 0;
5884 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5885 name. There's no need to do this in the special friend
5886 case mentioned above where GEN_TMPL is NULL. */
5889 DECL_TEMPLATE_INFO (r
)
5890 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
5891 SET_DECL_IMPLICIT_INSTANTIATION (r
);
5892 register_specialization (r
, gen_tmpl
, argvec
);
5894 /* Set the mangled name for R. */
5895 if (DECL_DESTRUCTOR_P (t
))
5896 DECL_ASSEMBLER_NAME (r
) = build_destructor_name (ctx
);
5899 /* Instantiations of template functions must be mangled
5900 specially, in order to conform to 14.5.5.1
5901 [temp.over.link]. */
5902 tree tmpl
= DECL_TI_TEMPLATE (t
);
5904 /* TMPL will be NULL if this is a specialization of a
5905 member function of a template class. */
5906 if (name_mangling_version
< 1
5907 || tmpl
== NULL_TREE
5908 || (member
&& !is_member_template (tmpl
)
5909 && !DECL_TEMPLATE_INFO (tmpl
)))
5910 set_mangled_name_for_decl (r
);
5912 set_mangled_name_for_template_decl (r
);
5916 make_decl_rtl (r
, NULL_PTR
, 1);
5918 /* Like grokfndecl. If we don't do this, pushdecl will
5919 mess up our TREE_CHAIN because it doesn't find a
5920 previous decl. Sigh. */
5922 && ! uses_template_parms (r
)
5923 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
))
5925 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
), r
);
5927 /* We're not supposed to instantiate default arguments
5928 until they are called, for a template. But, for a
5931 template <class T> void f ()
5932 { extern void g(int i = T()); }
5934 we should do the substitution when the template is
5935 instantiated. We handle the member function case in
5936 instantiate_class_template since the default arguments
5937 might refer to other members of the class. */
5939 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
5940 && !uses_template_parms (argvec
))
5941 tsubst_default_arguments (r
);
5944 /* Copy the list of befriending classes. */
5945 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
5947 friends
= &TREE_CHAIN (*friends
))
5949 *friends
= copy_node (*friends
);
5950 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
5951 args
, /*complain=*/1,
5955 if (DECL_CONSTRUCTOR_P (r
))
5957 maybe_retrofit_in_chrg (r
);
5958 grok_ctor_properties (ctx
, r
);
5960 else if (DECL_OVERLOADED_OPERATOR_P (r
))
5961 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
5968 TREE_TYPE (r
) = type
;
5969 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5971 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
5972 DECL_INITIAL (r
) = TREE_TYPE (r
);
5974 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
5975 /*complain=*/1, in_decl
);
5977 DECL_CONTEXT (r
) = NULL_TREE
;
5978 if (PROMOTE_PROTOTYPES
5979 && (TREE_CODE (type
) == INTEGER_TYPE
5980 || TREE_CODE (type
) == ENUMERAL_TYPE
)
5981 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
5982 DECL_ARG_TYPE (r
) = integer_type_node
;
5984 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
5985 /*complain=*/1, TREE_CHAIN (t
));
5993 TREE_TYPE (r
) = type
;
5994 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
5996 /* We don't have to set DECL_CONTEXT here; it is set by
5997 finish_member_declaration. */
5998 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
,
5999 /*complain=*/1, in_decl
);
6000 TREE_CHAIN (r
) = NULL_TREE
;
6001 if (TREE_CODE (type
) == VOID_TYPE
)
6002 cp_error_at ("instantiation of `%D' as type void", r
);
6010 = tsubst_copy (DECL_INITIAL (t
), args
, /*complain=*/1, in_decl
);
6011 TREE_CHAIN (r
) = NULL_TREE
;
6016 if (DECL_IMPLICIT_TYPEDEF_P (t
))
6018 /* For an implicit typedef, we just want the implicit
6019 typedef for the tsubst'd type. We've already got the
6020 tsubst'd type, as TYPE, so we just need it's associated
6022 r
= TYPE_NAME (type
);
6025 else if (!DECL_LANG_SPECIFIC (t
))
6027 /* For a template type parameter, we don't have to do
6028 anything special. */
6029 r
= TYPE_NAME (type
);
6043 /* Nobody should be tsubst'ing into non-template variables. */
6044 my_friendly_assert (DECL_LANG_SPECIFIC (t
)
6045 && DECL_TEMPLATE_INFO (t
) != NULL_TREE
, 0);
6047 if (TYPE_P (DECL_CONTEXT (t
)))
6048 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6050 in_decl
, /*entering_scope=*/1);
6052 /* Subsequent calls to pushdecl will fill this in. */
6055 /* Check to see if we already have this specialization. */
6056 tmpl
= DECL_TI_TEMPLATE (t
);
6057 gen_tmpl
= most_general_template (tmpl
);
6058 argvec
= tsubst (DECL_TI_ARGS (t
), args
, /*complain=*/1, in_decl
);
6060 spec
= retrieve_specialization (gen_tmpl
, argvec
);
6062 spec
= retrieve_local_specialization (gen_tmpl
,
6063 current_function_decl
);
6071 /* This declaration is going to have to be around for a while,
6072 so me make sure it is on a saveable obstack. */
6073 push_obstacks_nochange ();
6074 saveable_allocation ();
6078 TREE_TYPE (r
) = type
;
6079 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type
), r
);
6080 DECL_CONTEXT (r
) = ctx
;
6082 /* Don't try to expand the initializer until someone tries to use
6083 this variable; otherwise we run into circular dependencies. */
6084 DECL_INITIAL (r
) = NULL_TREE
;
6088 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
6090 /* Even if the original location is out of scope, the newly
6091 substituted one is not. */
6092 if (TREE_CODE (r
) == VAR_DECL
)
6093 DECL_DEAD_FOR_LOCAL (r
) = 0;
6095 /* A static data member declaration is always marked external
6096 when it is declared in-class, even if an initializer is
6097 present. We mimic the non-template processing here. */
6099 DECL_EXTERNAL (r
) = 1;
6101 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
6102 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6104 register_specialization (r
, gen_tmpl
, argvec
);
6106 register_local_specialization (r
, gen_tmpl
,
6107 current_function_decl
);
6109 TREE_CHAIN (r
) = NULL_TREE
;
6110 if (TREE_CODE (r
) == VAR_DECL
&& TREE_CODE (type
) == VOID_TYPE
)
6111 cp_error_at ("instantiation of `%D' as type void", r
);
6116 my_friendly_abort (0);
6119 /* Restore the file and line information. */
6120 lineno
= saved_lineno
;
6121 input_filename
= saved_filename
;
6126 /* Substitue into the ARG_TYPES of a function type. */
6129 tsubst_arg_types (arg_types
, args
, complain
, in_decl
)
6135 tree remaining_arg_types
;
6138 if (!arg_types
|| arg_types
== void_list_node
)
6141 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
6142 args
, complain
, in_decl
);
6143 if (remaining_arg_types
== error_mark_node
)
6144 return error_mark_node
;
6146 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
6147 if (type
== error_mark_node
)
6148 return error_mark_node
;
6150 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6151 top-level qualifiers as required. */
6152 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
6154 /* Note that we do not substitute into default arguments here. The
6155 standard mandates that they be instantiated only when needed,
6156 which is done in build_over_call. */
6157 return hash_tree_cons (TREE_PURPOSE (arg_types
), type
,
6158 remaining_arg_types
);
6162 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6163 *not* handle the exception-specification for FNTYPE, because the
6164 initial substitution of explicitly provided template parameters
6165 during argument deduction forbids substitution into the
6166 exception-specification:
6170 All references in the function type of the function template to the
6171 corresponding template parameters are replaced by the specified tem-
6172 plate argument values. If a substitution in a template parameter or
6173 in the function type of the function template results in an invalid
6174 type, type deduction fails. [Note: The equivalent substitution in
6175 exception specifications is done only when the function is instanti-
6176 ated, at which point a program is ill-formed if the substitution
6177 results in an invalid type.] */
6180 tsubst_function_type (t
, args
, complain
, in_decl
)
6190 /* The TYPE_CONTEXT is not used for function/method types. */
6191 my_friendly_assert (TYPE_CONTEXT (t
) == NULL_TREE
, 0);
6193 /* Substitue the return type. */
6194 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6195 if (return_type
== error_mark_node
)
6196 return error_mark_node
;
6198 /* Substitue the argument types. */
6199 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
6201 if (arg_types
== error_mark_node
)
6202 return error_mark_node
;
6204 /* Construct a new type node and return it. */
6205 if (TREE_CODE (t
) == FUNCTION_TYPE
)
6206 fntype
= build_function_type (return_type
, arg_types
);
6209 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
6210 if (! IS_AGGR_TYPE (r
))
6214 Type deduction may fail for any of the following
6217 -- Attempting to create "pointer to member of T" when T
6218 is not a class type. */
6220 cp_error ("creating pointer to member function of non-class type `%T'",
6222 return error_mark_node
;
6225 fntype
= build_cplus_method_type (r
, return_type
, TREE_CHAIN
6228 fntype
= build_qualified_type (fntype
, TYPE_QUALS (t
));
6229 fntype
= build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
6234 /* Substitute into the PARMS of a call-declarator. */
6237 tsubst_call_declarator_parms (parms
, args
, complain
, in_decl
)
6247 if (!parms
|| parms
== void_list_node
)
6250 new_parms
= tsubst_call_declarator_parms (TREE_CHAIN (parms
),
6251 args
, complain
, in_decl
);
6253 /* Figure out the type of this parameter. */
6254 type
= tsubst (TREE_VALUE (parms
), args
, complain
, in_decl
);
6256 /* Figure out the default argument as well. Note that we use
6257 tsubst_expr since the default argument is really an expression. */
6258 defarg
= tsubst_expr (TREE_PURPOSE (parms
), args
, complain
, in_decl
);
6260 /* Chain this parameter on to the front of those we have already
6261 processed. We don't use hash_tree_cons because that function
6262 doesn't check TREE_PARMLIST. */
6263 new_parms
= tree_cons (defarg
, type
, new_parms
);
6265 /* And note that these are parameters. */
6266 TREE_PARMLIST (new_parms
) = 1;
6271 /* Take the tree structure T and replace template parameters used
6272 therein with the argument vector ARGS. IN_DECL is an associated
6273 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6274 An appropriate error message is issued only if COMPLAIN is
6275 non-zero. Note that we must be relatively non-tolerant of
6276 extensions here, in order to preserve conformance; if we allow
6277 substitutions that should not be allowed, we may allow argument
6278 deductions that should not succeed, and therefore report ambiguous
6279 overload situations where there are none. In theory, we could
6280 allow the substitution, but indicate that it should have failed,
6281 and allow our caller to make sure that the right thing happens, but
6282 we don't try to do this yet.
6284 This function is used for dealing with types, decls and the like;
6285 for expressions, use tsubst_expr or tsubst_copy. */
6288 tsubst (t
, args
, complain
, in_decl
)
6295 if (t
== NULL_TREE
|| t
== error_mark_node
6296 || t
== integer_type_node
6297 || t
== void_type_node
6298 || t
== char_type_node
6299 || TREE_CODE (t
) == NAMESPACE_DECL
)
6302 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
6303 type
= IDENTIFIER_TYPE_VALUE (t
);
6305 type
= TREE_TYPE (t
);
6306 if (type
== unknown_type_node
)
6307 my_friendly_abort (42);
6309 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
6310 && TREE_CODE (t
) != TYPENAME_TYPE
6311 && TREE_CODE (t
) != TEMPLATE_DECL
6312 && TREE_CODE (t
) != IDENTIFIER_NODE
6313 && TREE_CODE (t
) != FUNCTION_TYPE
6314 && TREE_CODE (t
) != METHOD_TYPE
)
6315 type
= tsubst (type
, args
, complain
, in_decl
);
6316 if (type
== error_mark_node
)
6317 return error_mark_node
;
6319 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
6320 return tsubst_decl (t
, args
, type
, in_decl
);
6322 switch (TREE_CODE (t
))
6327 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
6328 /*entering_scope=*/0);
6331 case IDENTIFIER_NODE
:
6343 if (t
== integer_type_node
)
6346 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
6347 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
6351 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
6353 max
= tsubst_expr (omax
, args
, complain
, in_decl
);
6354 if (max
== error_mark_node
)
6355 return error_mark_node
;
6357 /* See if we can reduce this expression to something simpler. */
6358 max
= maybe_fold_nontype_arg (max
);
6359 if (!processing_template_decl
&& TREE_READONLY_DECL_P (max
))
6360 max
= decl_constant_value (max
);
6362 if (processing_template_decl
6363 /* When providing explicit arguments to a template
6364 function, but leaving some arguments for subsequent
6365 deduction, MAX may be template-dependent even if we're
6366 not PROCESSING_TEMPLATE_DECL. */
6367 || TREE_CODE (max
) != INTEGER_CST
)
6369 tree itype
= make_node (INTEGER_TYPE
);
6370 TYPE_MIN_VALUE (itype
) = size_zero_node
;
6371 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
6376 if (integer_zerop (omax
))
6378 /* Still allow an explicit array of size zero. */
6380 pedwarn ("creating array with size zero");
6382 else if (integer_zerop (max
) || INT_CST_LT (max
, integer_zero_node
))
6386 Type deduction may fail for any of the following
6389 Attempting to create an array with a size that is
6390 zero or negative. */
6392 cp_error ("creating array with size `%E'", max
);
6394 return error_mark_node
;
6397 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
));
6398 return build_index_type (max
);
6401 case TEMPLATE_TYPE_PARM
:
6402 case TEMPLATE_TEMPLATE_PARM
:
6403 case TEMPLATE_PARM_INDEX
:
6411 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
6412 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6414 idx
= TEMPLATE_TYPE_IDX (t
);
6415 level
= TEMPLATE_TYPE_LEVEL (t
);
6419 idx
= TEMPLATE_PARM_IDX (t
);
6420 level
= TEMPLATE_PARM_LEVEL (t
);
6423 if (TREE_VEC_LENGTH (args
) > 0)
6425 tree arg
= NULL_TREE
;
6427 levels
= TMPL_ARGS_DEPTH (args
);
6428 if (level
<= levels
)
6429 arg
= TMPL_ARG (args
, level
, idx
);
6431 if (arg
== error_mark_node
)
6432 return error_mark_node
;
6433 else if (arg
!= NULL_TREE
)
6435 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
6437 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg
))
6439 return cp_build_qualified_type_real
6440 (arg
, CP_TYPE_QUALS (arg
) | CP_TYPE_QUALS (t
),
6443 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
6445 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6447 /* We are processing a type constructed from
6448 a template template parameter */
6449 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
6450 args
, complain
, in_decl
);
6451 if (argvec
== error_mark_node
)
6452 return error_mark_node
;
6454 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6455 we are resolving nested-types in the signature of
6456 a member function templates.
6457 Otherwise ARG is a TEMPLATE_DECL and is the real
6458 template to be instantiated. */
6459 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
6460 arg
= TYPE_NAME (arg
);
6462 r
= lookup_template_class (DECL_NAME (arg
),
6465 /*entering_scope=*/0);
6466 return cp_build_qualified_type_real (r
,
6471 /* We are processing a template argument list. */
6479 my_friendly_abort (981018);
6482 /* This can happen during the attempted tsubst'ing in
6483 unify. This means that we don't yet have any information
6484 about the template parameter in question. */
6487 /* If we get here, we must have been looking at a parm for a
6488 more deeply nested template. Make a new version of this
6489 template parameter, but with a lower level. */
6490 switch (TREE_CODE (t
))
6492 case TEMPLATE_TYPE_PARM
:
6493 case TEMPLATE_TEMPLATE_PARM
:
6495 TEMPLATE_TYPE_PARM_INDEX (r
)
6496 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
6498 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
6499 TYPE_MAIN_VARIANT (r
) = r
;
6500 TYPE_POINTER_TO (r
) = NULL_TREE
;
6501 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
6503 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
6504 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
))
6506 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
6508 if (argvec
== error_mark_node
)
6509 return error_mark_node
;
6511 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
6512 = tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
6516 case TEMPLATE_PARM_INDEX
:
6517 r
= reduce_template_parm_level (t
, type
, levels
);
6521 my_friendly_abort (0);
6529 tree purpose
, value
, chain
, result
;
6531 if (t
== void_list_node
)
6534 purpose
= TREE_PURPOSE (t
);
6537 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
6538 if (purpose
== error_mark_node
)
6539 return error_mark_node
;
6541 value
= TREE_VALUE (t
);
6544 value
= tsubst (value
, args
, complain
, in_decl
);
6545 if (value
== error_mark_node
)
6546 return error_mark_node
;
6548 chain
= TREE_CHAIN (t
);
6549 if (chain
&& chain
!= void_type_node
)
6551 chain
= tsubst (chain
, args
, complain
, in_decl
);
6552 if (chain
== error_mark_node
)
6553 return error_mark_node
;
6555 if (purpose
== TREE_PURPOSE (t
)
6556 && value
== TREE_VALUE (t
)
6557 && chain
== TREE_CHAIN (t
))
6559 result
= hash_tree_cons (purpose
, value
, chain
);
6560 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
6564 if (type
!= NULL_TREE
)
6566 /* A binfo node. We always need to make a copy, of the node
6567 itself and of its BINFO_BASETYPES. */
6571 /* Make sure type isn't a typedef copy. */
6572 type
= BINFO_TYPE (TYPE_BINFO (type
));
6574 TREE_TYPE (t
) = complete_type (type
);
6575 if (IS_AGGR_TYPE (type
))
6577 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
6578 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
6579 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
6580 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
6585 /* Otherwise, a vector of template arguments. */
6586 return tsubst_template_arg_vector (t
, args
, complain
);
6589 case REFERENCE_TYPE
:
6591 enum tree_code code
;
6593 if (type
== TREE_TYPE (t
))
6596 code
= TREE_CODE (t
);
6601 Type deduction may fail for any of the following
6604 -- Attempting to create a pointer to reference type.
6605 -- Attempting to create a reference to a reference type or
6606 a reference to void. */
6607 if (TREE_CODE (type
) == REFERENCE_TYPE
6608 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
6610 static int last_line
= 0;
6611 static char* last_file
= 0;
6613 /* We keep track of the last time we issued this error
6614 message to avoid spewing a ton of messages during a
6615 single bad template instantiation. */
6616 if (complain
&& (last_line
!= lineno
||
6617 last_file
!= input_filename
))
6619 if (TREE_CODE (type
) == VOID_TYPE
)
6620 cp_error ("forming reference to void");
6622 cp_error ("forming %s to reference type `%T'",
6623 (code
== POINTER_TYPE
) ? "pointer" : "reference",
6626 last_file
= input_filename
;
6629 return error_mark_node
;
6631 else if (code
== POINTER_TYPE
)
6632 r
= build_pointer_type (type
);
6634 r
= build_reference_type (type
);
6635 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6637 /* Will this ever be needed for TYPE_..._TO values? */
6643 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
6644 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
6648 Type deduction may fail for any of the following
6651 -- Attempting to create "pointer to member of T" when T
6652 is not a class type. */
6654 cp_error ("creating pointer to member of non-class type `%T'",
6656 return error_mark_node
;
6658 return build_offset_type (r
, type
);
6666 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
6667 if (fntype
== error_mark_node
)
6668 return error_mark_node
;
6670 /* Substitue the exception specification. */
6671 raises
= TYPE_RAISES_EXCEPTIONS (t
);
6674 tree list
= NULL_TREE
;
6676 if (! TREE_VALUE (raises
))
6679 for (; raises
!= NULL_TREE
; raises
= TREE_CHAIN (raises
))
6681 tree spec
= TREE_VALUE (raises
);
6683 spec
= tsubst (spec
, args
, complain
, in_decl
);
6684 if (spec
== error_mark_node
)
6686 list
= add_exception_specifier (list
, spec
, complain
);
6688 fntype
= build_exception_variant (fntype
, list
);
6694 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
6695 if (domain
== error_mark_node
)
6696 return error_mark_node
;
6698 /* As an optimization, we avoid regenerating the array type if
6699 it will obviously be the same as T. */
6700 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
6703 /* These checks should match the ones in grokdeclarator.
6707 The deduction may fail for any of the following reasons:
6709 -- Attempting to create an array with an element type that
6710 is void, a function type, or a reference type. */
6711 if (TREE_CODE (type
) == VOID_TYPE
6712 || TREE_CODE (type
) == FUNCTION_TYPE
6713 || TREE_CODE (type
) == REFERENCE_TYPE
)
6716 cp_error ("creating array of `%T'", type
);
6717 return error_mark_node
;
6720 r
= build_cplus_array_type (type
, domain
);
6727 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6729 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
,
6732 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6733 return error_mark_node
;
6735 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
));
6741 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6743 if (e
== error_mark_node
)
6744 return error_mark_node
;
6746 return fold (build (TREE_CODE (t
), TREE_TYPE (t
), e
));
6751 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
6752 in_decl
, /*entering_scope=*/1);
6753 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
6756 if (ctx
== error_mark_node
|| f
== error_mark_node
)
6757 return error_mark_node
;
6759 if (!IS_AGGR_TYPE (ctx
))
6762 cp_error ("`%T' is not a class, struct, or union type",
6764 return error_mark_node
;
6766 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
6768 /* Normally, make_typename_type does not require that the CTX
6769 have complete type in order to allow things like:
6771 template <class T> struct S { typename S<T>::X Y; };
6773 But, such constructs have already been resolved by this
6774 point, so here CTX really should have complete type, unless
6775 it's a partial instantiation. */
6776 ctx
= complete_type (ctx
);
6777 if (!TYPE_SIZE (ctx
))
6780 incomplete_type_error (NULL_TREE
, ctx
);
6781 return error_mark_node
;
6785 f
= make_typename_type (ctx
, f
);
6786 if (f
== error_mark_node
)
6788 return cp_build_qualified_type_real (f
,
6790 | CP_TYPE_QUALS (t
),
6796 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6798 if (e
== error_mark_node
)
6799 return error_mark_node
;
6800 return make_pointer_declarator (type
, e
);
6805 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6807 if (e
== error_mark_node
)
6808 return error_mark_node
;
6809 return make_reference_declarator (type
, e
);
6814 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6816 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
,
6818 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6819 return error_mark_node
;
6821 return build_parse_node (ARRAY_REF
, e1
, e2
, tsubst_expr
);
6826 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6828 tree e2
= (tsubst_call_declarator_parms
6829 (CALL_DECLARATOR_PARMS (t
), args
, complain
, in_decl
));
6830 tree e3
= tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t
), args
,
6833 if (e1
== error_mark_node
|| e2
== error_mark_node
6834 || e3
== error_mark_node
)
6835 return error_mark_node
;
6837 return make_call_declarator (e1
, e2
, CALL_DECLARATOR_QUALS (t
), e3
);
6842 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
,
6844 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
6845 if (e1
== error_mark_node
|| e2
== error_mark_node
)
6846 return error_mark_node
;
6848 return build_parse_node (TREE_CODE (t
), e1
, e2
);
6853 tree e1
= tsubst_expr (TYPE_FIELDS (t
), args
, complain
,
6855 if (e1
== error_mark_node
)
6856 return error_mark_node
;
6858 return TREE_TYPE (e1
);
6862 sorry ("use of `%s' in template",
6863 tree_code_name
[(int) TREE_CODE (t
)]);
6864 return error_mark_node
;
6868 /* Like tsubst, but deals with expressions. This function just replaces
6869 template parms; to finish processing the resultant expression, use
6873 tsubst_copy (t
, args
, complain
, in_decl
)
6878 enum tree_code code
;
6881 if (t
== NULL_TREE
|| t
== error_mark_node
)
6884 code
= TREE_CODE (t
);
6889 return do_identifier (DECL_NAME (t
), 0, NULL_TREE
);
6896 if (!DECL_CONTEXT (t
))
6897 /* This is a global enumeration constant. */
6900 /* Unfortunately, we cannot just call lookup_name here.
6903 template <int I> int f() {
6905 struct S { void g() { E e = a; } };
6908 When we instantiate f<7>::S::g(), say, lookup_name is not
6909 clever enough to find f<7>::a. */
6911 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
6912 /*entering_scope=*/0);
6914 for (v
= TYPE_VALUES (enum_type
);
6917 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
6918 return TREE_VALUE (v
);
6920 /* We didn't find the name. That should never happen; if
6921 name-lookup found it during preliminary parsing, we
6922 should find it again here during instantiation. */
6923 my_friendly_abort (0);
6928 if (DECL_CONTEXT (t
))
6932 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
6933 /*entering_scope=*/1);
6934 if (ctx
!= DECL_CONTEXT (t
))
6935 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
6941 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
6942 t
= tsubst (t
, args
, complain
, in_decl
);
6947 if (is_member_template (t
))
6948 return tsubst (t
, args
, complain
, in_decl
);
6954 /* We must tsbust into a LOOKUP_EXPR in case the names to
6955 which it refers is a conversion operator; in that case the
6956 name will change. We avoid making unnecessary copies,
6959 tree id
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
6961 if (id
!= TREE_OPERAND (t
, 0))
6963 r
= build_nt (LOOKUP_EXPR
, id
);
6964 LOOKUP_EXPR_GLOBAL (r
) = LOOKUP_EXPR_GLOBAL (t
);
6972 case REINTERPRET_CAST_EXPR
:
6973 case CONST_CAST_EXPR
:
6974 case STATIC_CAST_EXPR
:
6975 case DYNAMIC_CAST_EXPR
:
6978 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6979 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
6982 case PREDECREMENT_EXPR
:
6983 case PREINCREMENT_EXPR
:
6984 case POSTDECREMENT_EXPR
:
6985 case POSTINCREMENT_EXPR
:
6987 case TRUTH_NOT_EXPR
:
6990 case CONVERT_EXPR
: /* Unary + */
6997 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
6998 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
7003 case TRUNC_DIV_EXPR
:
7005 case FLOOR_DIV_EXPR
:
7006 case ROUND_DIV_EXPR
:
7007 case EXACT_DIV_EXPR
:
7009 case BIT_ANDTC_EXPR
:
7012 case TRUNC_MOD_EXPR
:
7013 case FLOOR_MOD_EXPR
:
7014 case TRUTH_ANDIF_EXPR
:
7015 case TRUTH_ORIF_EXPR
:
7016 case TRUTH_AND_EXPR
:
7037 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7038 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7042 tree fn
= TREE_OPERAND (t
, 0);
7043 if (is_overloaded_fn (fn
))
7044 fn
= tsubst_copy (get_first_fn (fn
), args
, complain
, in_decl
);
7046 /* Sometimes FN is a LOOKUP_EXPR. */
7047 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
7049 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7054 case METHOD_CALL_EXPR
:
7056 tree name
= TREE_OPERAND (t
, 0);
7057 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
7059 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7061 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7063 else if (TREE_CODE (name
) == SCOPE_REF
7064 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
7066 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7068 name
= TREE_OPERAND (name
, 1);
7069 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
7071 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
7072 name
= build_nt (SCOPE_REF
, base
, name
);
7075 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7077 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
,
7079 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
),
7084 /* This processing should really occur in tsubst_expr, However,
7085 tsubst_expr does not recurse into expressions, since it
7086 assumes that there aren't any statements inside them.
7087 Instead, it simply calls build_expr_from_tree. So, we need
7088 to expand the STMT_EXPR here. */
7089 if (!processing_template_decl
)
7091 tree rtl_expr
= begin_stmt_expr ();
7092 tree block
= tsubst_expr (STMT_EXPR_STMT (t
), args
,
7094 return finish_stmt_expr (rtl_expr
, block
);
7101 case PSEUDO_DTOR_EXPR
:
7104 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7105 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7106 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7113 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7114 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
7115 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
7116 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
7123 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
7124 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
7125 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
7126 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
7130 case TEMPLATE_ID_EXPR
:
7132 /* Substituted template arguments */
7133 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
7136 if (targs
&& TREE_CODE (targs
) == TREE_LIST
)
7139 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
7140 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
7145 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); ++i
)
7146 TREE_VEC_ELT (targs
, i
)
7147 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs
, i
));
7150 return lookup_template_function
7151 (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
), targs
);
7156 tree purpose
, value
, chain
;
7158 if (t
== void_list_node
)
7161 purpose
= TREE_PURPOSE (t
);
7163 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
7164 value
= TREE_VALUE (t
);
7166 value
= tsubst_copy (value
, args
, complain
, in_decl
);
7167 chain
= TREE_CHAIN (t
);
7168 if (chain
&& chain
!= void_type_node
)
7169 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
7170 if (purpose
== TREE_PURPOSE (t
)
7171 && value
== TREE_VALUE (t
)
7172 && chain
== TREE_CHAIN (t
))
7174 return tree_cons (purpose
, value
, chain
);
7181 case TEMPLATE_TYPE_PARM
:
7182 case TEMPLATE_TEMPLATE_PARM
:
7183 case TEMPLATE_PARM_INDEX
:
7185 case REFERENCE_TYPE
:
7192 return tsubst (t
, args
, complain
, in_decl
);
7194 case IDENTIFIER_NODE
:
7195 if (IDENTIFIER_TYPENAME_P (t
)
7196 /* Make sure it's not just a variable named `__opr', for instance,
7197 which can occur in some existing code. */
7199 return build_typename_overload
7200 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
7207 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
7208 NULL_TREE
, tsubst_copy (CONSTRUCTOR_ELTS (t
), args
,
7209 complain
, in_decl
));
7210 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
7219 /* Like tsubst_copy, but also does semantic processing. */
7222 tsubst_expr (t
, args
, complain
, in_decl
)
7229 if (t
== NULL_TREE
|| t
== error_mark_node
)
7232 if (processing_template_decl
)
7233 return tsubst_copy (t
, args
, complain
, in_decl
);
7235 switch (TREE_CODE (t
))
7238 finish_named_return_value
7239 (TREE_OPERAND (t
, 0),
7240 tsubst_expr (TREE_OPERAND (t
, 1), args
, /*complain=*/1, in_decl
));
7241 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7244 case CTOR_INITIALIZER
:
7245 current_member_init_list
7246 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
7247 current_base_init_list
7248 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
7250 tsubst_expr (TREE_CHAIN (t
), args
, complain
, in_decl
);
7254 lineno
= STMT_LINENO (t
);
7255 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
7256 args
, complain
, in_decl
));
7260 lineno
= STMT_LINENO (t
);
7261 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
7262 args
, complain
, in_decl
));
7267 int i
= suspend_momentary ();
7271 lineno
= STMT_LINENO (t
);
7272 decl
= DECL_STMT_DECL (t
);
7273 if (TREE_CODE (decl
) == LABEL_DECL
)
7274 finish_label_decl (DECL_NAME (decl
));
7277 init
= DECL_INITIAL (decl
);
7278 decl
= tsubst (decl
, args
, complain
, in_decl
);
7279 init
= tsubst_expr (init
, args
, complain
, in_decl
);
7280 DECL_INITIAL (decl
) = init
;
7281 /* By marking the declaration as instantiated, we avoid
7282 trying to instantiate it. Since instantiate_decl can't
7283 handle local variables, and since we've already done
7284 all that needs to be done, that's the right thing to
7286 if (TREE_CODE (decl
) == VAR_DECL
)
7287 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
7288 maybe_push_decl (decl
);
7289 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0, 0);
7290 add_decl_stmt (decl
);
7292 resume_momentary (i
);
7299 lineno
= STMT_LINENO (t
);
7301 stmt
= begin_for_stmt ();
7302 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
7303 tsubst_expr (tmp
, args
, complain
, in_decl
);
7304 finish_for_init_stmt (stmt
);
7305 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
7308 tmp
= tsubst_expr (FOR_EXPR (t
), args
, complain
, in_decl
);
7309 finish_for_expr (tmp
, stmt
);
7310 tsubst_expr (FOR_BODY (t
), args
, complain
, in_decl
);
7311 finish_for_stmt (tmp
, stmt
);
7317 lineno
= STMT_LINENO (t
);
7318 stmt
= begin_while_stmt ();
7319 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
7320 args
, complain
, in_decl
),
7322 tsubst_expr (WHILE_BODY (t
), args
, complain
, in_decl
);
7323 finish_while_stmt (stmt
);
7329 lineno
= STMT_LINENO (t
);
7330 stmt
= begin_do_stmt ();
7331 tsubst_expr (DO_BODY (t
), args
, complain
, in_decl
);
7332 finish_do_body (stmt
);
7333 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
7343 lineno
= STMT_LINENO (t
);
7344 stmt
= begin_if_stmt ();
7345 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
7346 args
, complain
, in_decl
),
7349 if (tmp
= THEN_CLAUSE (t
), tmp
)
7351 tsubst_expr (tmp
, args
, complain
, in_decl
);
7352 finish_then_clause (stmt
);
7355 if (tmp
= ELSE_CLAUSE (t
), tmp
)
7357 begin_else_clause ();
7358 tsubst_expr (tmp
, args
, complain
, in_decl
);
7359 finish_else_clause (stmt
);
7370 lineno
= STMT_LINENO (t
);
7371 stmt
= begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
7372 for (substmt
= COMPOUND_BODY (t
);
7373 substmt
!= NULL_TREE
;
7374 substmt
= TREE_CHAIN (substmt
))
7375 tsubst_expr (substmt
, args
, complain
, in_decl
);
7376 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
), stmt
);
7381 lineno
= STMT_LINENO (t
);
7382 finish_break_stmt ();
7386 lineno
= STMT_LINENO (t
);
7387 finish_continue_stmt ();
7394 lineno
= STMT_LINENO (t
);
7395 stmt
= begin_switch_stmt ();
7396 val
= tsubst_expr (SWITCH_COND (t
), args
, complain
, in_decl
);
7397 finish_switch_cond (val
, stmt
);
7398 tsubst_expr (SWITCH_BODY (t
), args
, complain
, in_decl
);
7399 finish_switch_stmt (val
, stmt
);
7404 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, complain
, in_decl
),
7405 tsubst_expr (CASE_HIGH (t
), args
, complain
, in_decl
));
7409 lineno
= STMT_LINENO (t
);
7410 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t
)));
7414 lineno
= STMT_LINENO (t
);
7415 t
= GOTO_DESTINATION (t
);
7416 if (TREE_CODE (t
) != LABEL_DECL
)
7417 /* Computed goto's must be tsubst'd into. On the other hand,
7418 non-computed gotos must not be; the identifier in question
7419 will have no binding. */
7420 t
= tsubst_expr (t
, args
, complain
, in_decl
);
7423 finish_goto_stmt (t
);
7427 lineno
= STMT_LINENO (t
);
7428 finish_asm_stmt (ASM_CV_QUAL (t
),
7429 tsubst_expr (ASM_STRING (t
), args
, complain
, in_decl
),
7430 tsubst_expr (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
7431 tsubst_expr (ASM_INPUTS (t
), args
, complain
, in_decl
),
7432 tsubst_expr (ASM_CLOBBERS (t
), args
, complain
,
7437 lineno
= STMT_LINENO (t
);
7438 stmt
= begin_try_block ();
7439 tsubst_expr (TRY_STMTS (t
), args
, complain
, in_decl
);
7440 finish_try_block (stmt
);
7442 finish_cleanup (tsubst_expr (TRY_HANDLERS (t
), args
,
7447 tree handler
= TRY_HANDLERS (t
);
7448 for (; handler
; handler
= TREE_CHAIN (handler
))
7449 tsubst_expr (handler
, args
, complain
, in_decl
);
7450 finish_handler_sequence (stmt
);
7455 lineno
= STMT_LINENO (t
);
7456 stmt
= begin_handler ();
7457 if (HANDLER_PARMS (t
))
7458 expand_start_catch_block
7459 (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t
)),
7460 args
, complain
, in_decl
));
7462 expand_start_catch_block (NULL_TREE
);
7463 finish_handler_parms (stmt
);
7464 tsubst_expr (HANDLER_BODY (t
), args
, complain
, in_decl
);
7465 finish_handler (stmt
);
7469 lineno
= STMT_LINENO (t
);
7471 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
7472 tsubst (t
, args
, complain
, NULL_TREE
);
7476 return build_expr_from_tree (tsubst_copy (t
, args
, complain
, in_decl
));
7481 /* Instantiate the indicated variable or function template TMPL with
7482 the template arguments in TARG_PTR. */
7485 instantiate_template (tmpl
, targ_ptr
)
7486 tree tmpl
, targ_ptr
;
7492 struct obstack
*old_fmp_obstack
;
7493 extern struct obstack
*function_maybepermanent_obstack
;
7496 if (tmpl
== error_mark_node
)
7497 return error_mark_node
;
7499 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
7501 /* Check to see if we already have this specialization. */
7502 spec
= retrieve_specialization (tmpl
, targ_ptr
);
7503 if (spec
!= NULL_TREE
)
7506 if (DECL_TEMPLATE_INFO (tmpl
))
7508 /* The TMPL is a partial instantiation. To get a full set of
7509 arguments we must add the arguments used to perform the
7510 partial instantiation. */
7511 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
7513 gen_tmpl
= most_general_template (tmpl
);
7515 /* Check to see if we already have this specialization. */
7516 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
);
7517 if (spec
!= NULL_TREE
)
7523 push_obstacks (&permanent_obstack
, &permanent_obstack
);
7524 old_fmp_obstack
= function_maybepermanent_obstack
;
7525 function_maybepermanent_obstack
= &permanent_obstack
;
7527 len
= DECL_NTPARMS (gen_tmpl
);
7528 inner_args
= innermost_args (targ_ptr
);
7532 tree t
= TREE_VEC_ELT (inner_args
, i
);
7533 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
7535 tree nt
= target_type (t
);
7536 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
7538 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
7539 cp_error (" trying to instantiate `%D'", gen_tmpl
);
7540 fndecl
= error_mark_node
;
7546 /* substitute template parameters */
7547 fndecl
= tsubst (DECL_RESULT (gen_tmpl
), targ_ptr
, /*complain=*/1, gen_tmpl
);
7548 /* The DECL_TI_TEMPLATE should always be the immediate parent
7549 template, not the most general template. */
7550 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
7552 if (flag_external_templates
)
7553 add_pending_template (fndecl
);
7556 function_maybepermanent_obstack
= old_fmp_obstack
;
7562 /* Push the name of the class template into the scope of the instantiation. */
7565 overload_template_name (type
)
7568 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
7571 if (IDENTIFIER_CLASS_VALUE (id
)
7572 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
7575 decl
= build_decl (TYPE_DECL
, id
, type
);
7576 SET_DECL_ARTIFICIAL (decl
);
7577 pushdecl_class_level (decl
);
7580 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7581 arguments that are being used when calling it. TARGS is a vector
7582 into which the deduced template arguments are placed.
7584 Return zero for success, 2 for an incomplete match that doesn't resolve
7585 all the types, and 1 for complete failure. An error message will be
7586 printed only for an incomplete match.
7588 If FN is a conversion operator, RETURN_TYPE is the type desired as
7589 the result of the conversion operator.
7591 TPARMS is a vector of template parameters.
7593 The EXPLICIT_TARGS are explicit template arguments provided via a
7596 The parameter STRICT is one of:
7599 We are deducing arguments for a function call, as in
7603 We are deducing arguments for a conversion function, as in
7607 We are deducing arguments when calculating the partial
7608 ordering between specializations of function or class
7609 templates, as in [temp.func.order] and [temp.class.order],
7610 when doing an explicit instantiation as in [temp.explicit],
7611 when determining an explicit specialization as in
7612 [temp.expl.spec], or when taking the address of a function
7613 template, as in [temp.deduct.funcaddr].
7615 The other arguments are as for type_unification. */
7618 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
7620 tree fn
, explicit_targs
, targs
, args
, return_type
;
7621 unification_kind_t strict
;
7627 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
7629 fntype
= TREE_TYPE (fn
);
7634 The specified template arguments must match the template
7635 parameters in kind (i.e., type, nontype, template), and there
7636 must not be more arguments than there are parameters;
7637 otherwise type deduction fails.
7639 Nontype arguments must match the types of the corresponding
7640 nontype template parameters, or must be convertible to the
7641 types of the corresponding nontype parameters as specified in
7642 _temp.arg.nontype_, otherwise type deduction fails.
7644 All references in the function type of the function template
7645 to the corresponding template parameters are replaced by the
7646 specified template argument values. If a substitution in a
7647 template parameter or in the function type of the function
7648 template results in an invalid type, type deduction fails. */
7650 tree converted_args
;
7653 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7654 explicit_targs
, NULL_TREE
, /*complain=*/0,
7655 /*require_all_arguments=*/0));
7656 if (converted_args
== error_mark_node
)
7659 fntype
= tsubst (fntype
, converted_args
, /*complain=*/0, NULL_TREE
);
7660 if (fntype
== error_mark_node
)
7663 /* Place the explicitly specified arguments in TARGS. */
7664 for (i
= 0; i
< TREE_VEC_LENGTH (targs
); i
++)
7665 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
7668 parms
= TYPE_ARG_TYPES (fntype
);
7670 if (DECL_CONV_FN_P (fn
))
7672 /* This is a template conversion operator. Use the return types
7673 as well as the argument types. We use it instead of 'this', since
7674 we could be comparing conversions from different classes. */
7675 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
),
7676 TREE_CHAIN (parms
));
7677 args
= tree_cons (NULL_TREE
, return_type
, TREE_CHAIN (args
));
7680 /* We allow incomplete unification without an error message here
7681 because the standard doesn't seem to explicitly prohibit it. Our
7682 callers must be ready to deal with unification failures in any
7684 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
7685 targs
, parms
, args
, /*subr=*/0,
7686 strict
, /*allow_incomplete*/1);
7689 /* All is well so far. Now, check:
7693 When all template arguments have been deduced, all uses of
7694 template parameters in nondeduced contexts are replaced with
7695 the corresponding deduced argument values. If the
7696 substitution results in an invalid type, as described above,
7697 type deduction fails. */
7698 if (tsubst (TREE_TYPE (fn
), targs
, /*complain=*/0, NULL_TREE
)
7705 /* Adjust types before performing type deduction, as described in
7706 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
7707 sections are symmetric. PARM is the type of a function parameter
7708 or the return type of the conversion function. ARG is the type of
7709 the argument passed to the call, or the type of the value
7710 intialized with the result of the conversion function. */
7713 maybe_adjust_types_for_deduction (strict
, parm
, arg
)
7714 unification_kind_t strict
;
7725 /* Swap PARM and ARG throughout the remainder of this
7726 function; the handling is precisely symmetric since PARM
7727 will initialize ARG rather than vice versa. */
7735 /* There is nothing to do in this case. */
7739 my_friendly_abort (0);
7742 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
7744 /* [temp.deduct.call]
7746 If P is not a reference type:
7748 --If A is an array type, the pointer type produced by the
7749 array-to-pointer standard conversion (_conv.array_) is
7750 used in place of A for type deduction; otherwise,
7752 --If A is a function type, the pointer type produced by
7753 the function-to-pointer standard conversion
7754 (_conv.func_) is used in place of A for type deduction;
7757 --If A is a cv-qualified type, the top level
7758 cv-qualifiers of A's type are ignored for type
7760 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
7761 *arg
= build_pointer_type (TREE_TYPE (*arg
));
7762 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
7763 *arg
= build_pointer_type (*arg
);
7765 *arg
= TYPE_MAIN_VARIANT (*arg
);
7768 /* [temp.deduct.call]
7770 If P is a cv-qualified type, the top level cv-qualifiers
7771 of P's type are ignored for type deduction. If P is a
7772 reference type, the type referred to by P is used for
7774 *parm
= TYPE_MAIN_VARIANT (*parm
);
7775 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
7776 *parm
= TREE_TYPE (*parm
);
7779 /* Like type_unfication.
7781 If SUBR is 1, we're being called recursively (to unify the
7782 arguments of a function or method parameter of a function
7786 type_unification_real (tparms
, targs
, parms
, args
, subr
,
7787 strict
, allow_incomplete
)
7788 tree tparms
, targs
, parms
, args
;
7790 unification_kind_t strict
;
7791 int allow_incomplete
;
7795 int ntparms
= TREE_VEC_LENGTH (tparms
);
7798 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
7799 my_friendly_assert (parms
== NULL_TREE
7800 || TREE_CODE (parms
) == TREE_LIST
, 290);
7801 /* ARGS could be NULL (via a call from parse.y to
7802 build_x_function_call). */
7804 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
7805 my_friendly_assert (ntparms
> 0, 292);
7810 sub_strict
= UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_DERIVED
;
7814 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
7818 sub_strict
= UNIFY_ALLOW_NONE
;
7822 my_friendly_abort (0);
7826 && parms
!= void_list_node
7828 && args
!= void_list_node
)
7830 parm
= TREE_VALUE (parms
);
7831 parms
= TREE_CHAIN (parms
);
7832 arg
= TREE_VALUE (args
);
7833 args
= TREE_CHAIN (args
);
7835 if (arg
== error_mark_node
)
7837 if (arg
== unknown_type_node
)
7838 /* We can't deduce anything from this, but we might get all the
7839 template args from other function args. */
7842 /* Conversions will be performed on a function argument that
7843 corresponds with a function parameter that contains only
7844 non-deducible template parameters and explicitly specified
7845 template parameters. */
7846 if (! uses_template_parms (parm
))
7850 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7851 type
= TREE_TYPE (arg
);
7858 if (strict
== DEDUCE_EXACT
)
7860 if (same_type_p (parm
, type
))
7864 /* It might work; we shouldn't check now, because we might
7865 get into infinite recursion. Overload resolution will
7872 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
7874 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
7875 if (type_unknown_p (arg
))
7877 /* [temp.deduct.type] A template-argument can be deduced from
7878 a pointer to function or pointer to member function
7879 argument if the set of overloaded functions does not
7880 contain function templates and at most one of a set of
7881 overloaded functions provides a unique match. */
7883 if (resolve_overloaded_unification
7884 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
7889 arg
= TREE_TYPE (arg
);
7893 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
7895 switch (unify (tparms
, targs
, parm
, arg
, sub_strict
))
7903 /* Fail if we've reached the end of the parm list, and more args
7904 are present, and the parm list isn't variadic. */
7905 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
7907 /* Fail if parms are left and they don't have default values. */
7909 && parms
!= void_list_node
7910 && TREE_PURPOSE (parms
) == NULL_TREE
)
7913 for (i
= 0; i
< ntparms
; i
++)
7914 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
7916 if (!allow_incomplete
)
7917 error ("incomplete type unification");
7923 /* Subroutine of type_unification_real. Args are like the variables at the
7924 call site. ARG is an overloaded function (or template-id); we try
7925 deducing template args from each of the overloads, and if only one
7926 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7929 resolve_overloaded_unification (tparms
, targs
, parm
, arg
, strict
,
7931 tree tparms
, targs
, parm
, arg
;
7932 unification_kind_t strict
;
7935 tree tempargs
= copy_node (targs
);
7938 if (TREE_CODE (arg
) == ADDR_EXPR
)
7939 arg
= TREE_OPERAND (arg
, 0);
7941 if (TREE_CODE (arg
) == COMPONENT_REF
)
7942 /* Handle `&x' where `x' is some static or non-static member
7944 arg
= TREE_OPERAND (arg
, 1);
7946 if (TREE_CODE (arg
) == OFFSET_REF
)
7947 arg
= TREE_OPERAND (arg
, 1);
7949 /* Strip baselink information. */
7950 while (TREE_CODE (arg
) == TREE_LIST
)
7951 arg
= TREE_VALUE (arg
);
7953 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
7955 /* If we got some explicit template args, we need to plug them into
7956 the affected templates before we try to unify, in case the
7957 explicit args will completely resolve the templates in question. */
7959 tree expl_subargs
= TREE_OPERAND (arg
, 1);
7960 arg
= TREE_OPERAND (arg
, 0);
7962 for (; arg
; arg
= OVL_NEXT (arg
))
7964 tree fn
= OVL_CURRENT (arg
);
7967 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7970 subargs
= get_bindings_overload (fn
, DECL_RESULT (fn
), expl_subargs
);
7973 elem
= tsubst (TREE_TYPE (fn
), subargs
, /*complain=*/0,
7975 if (TREE_CODE (elem
) == METHOD_TYPE
)
7976 elem
= build_ptrmemfunc_type (build_pointer_type (elem
));
7977 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
, elem
,
7978 strict
, sub_strict
);
7982 else if (TREE_CODE (arg
) == OVERLOAD
)
7984 for (; arg
; arg
= OVL_NEXT (arg
))
7986 tree type
= TREE_TYPE (OVL_CURRENT (arg
));
7987 if (TREE_CODE (type
) == METHOD_TYPE
)
7988 type
= build_ptrmemfunc_type (build_pointer_type (type
));
7989 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
7991 strict
, sub_strict
);
7995 my_friendly_abort (981006);
7997 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7998 to function or pointer to member function argument if the set of
7999 overloaded functions does not contain function templates and at most
8000 one of a set of overloaded functions provides a unique match.
8002 So if we found multiple possibilities, we return success but don't
8007 int i
= TREE_VEC_LENGTH (targs
);
8009 if (TREE_VEC_ELT (tempargs
, i
))
8010 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
8018 /* Subroutine of resolve_overloaded_unification; does deduction for a single
8019 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8020 different overloads deduce different arguments for a given parm.
8021 Returns 1 on success. */
8024 try_one_overload (tparms
, orig_targs
, targs
, parm
, arg
, strict
,
8026 tree tparms
, orig_targs
, targs
, parm
, arg
;
8027 unification_kind_t strict
;
8034 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8035 to function or pointer to member function argument if the set of
8036 overloaded functions does not contain function templates and at most
8037 one of a set of overloaded functions provides a unique match.
8039 So if this is a template, just return success. */
8041 if (uses_template_parms (arg
))
8044 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
8046 /* We don't copy orig_targs for this because if we have already deduced
8047 some template args from previous args, unify would complain when we
8048 try to deduce a template parameter for the same argument, even though
8049 there isn't really a conflict. */
8050 nargs
= TREE_VEC_LENGTH (targs
);
8051 tempargs
= make_scratch_vec (nargs
);
8053 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
8056 /* First make sure we didn't deduce anything that conflicts with
8057 explicitly specified args. */
8058 for (i
= nargs
; i
--; )
8060 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8061 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
8063 if (elt
== NULL_TREE
)
8065 else if (uses_template_parms (elt
))
8067 /* Since we're unifying against ourselves, we will fill in template
8068 args used in the function parm list with our own template parms.
8070 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
8073 else if (oldelt
&& ! template_args_equal (oldelt
, elt
))
8077 for (i
= nargs
; i
--; )
8079 tree elt
= TREE_VEC_ELT (tempargs
, i
);
8082 TREE_VEC_ELT (targs
, i
) = elt
;
8088 /* PARM is a template class (perhaps with unbound template
8089 parameters). ARG is a fully instantiated type. If ARG can be
8090 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8091 TARGS are as for unify. */
8094 try_class_unification (tparms
, targs
, parm
, arg
)
8103 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
8104 || CLASSTYPE_TI_TEMPLATE (arg
) != CLASSTYPE_TI_TEMPLATE (parm
))
8107 /* We need to make a new template argument vector for the call to
8108 unify. If we used TARGS, we'd clutter it up with the result of
8109 the attempted unification, even if this class didn't work out.
8110 We also don't want to commit ourselves to all the unifications
8111 we've already done, since unification is supposed to be done on
8112 an argument-by-argument basis. In other words, consider the
8113 following pathological case:
8115 template <int I, int J, int K>
8118 template <int I, int J>
8119 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8121 template <int I, int J, int K>
8122 void f(S<I, J, K>, S<I, I, I>);
8131 Now, by the time we consider the unification involving `s2', we
8132 already know that we must have `f<0, 0, 0>'. But, even though
8133 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8134 because there are two ways to unify base classes of S<0, 1, 2>
8135 with S<I, I, I>. If we kept the already deduced knowledge, we
8136 would reject the possibility I=1. */
8138 copy_of_targs
= make_temp_vec (TREE_VEC_LENGTH (targs
));
8139 i
= unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
8140 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
);
8143 /* If unification failed, we're done. */
8150 /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8151 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
8152 for the base class of ARG that we are currently examining. */
8155 get_template_base_recursive (tparms
, targs
, parm
,
8156 arg_binfo
, rval
, flags
)
8166 tree arg
= BINFO_TYPE (arg_binfo
);
8168 if (!(flags
& GTB_IGNORE_TYPE
))
8170 tree r
= try_class_unification (tparms
, targs
,
8173 /* If there is more than one satisfactory baseclass, then:
8177 If they yield more than one possible deduced A, the type
8181 if (r
&& rval
&& !same_type_p (r
, rval
))
8182 return error_mark_node
;
8187 binfos
= BINFO_BASETYPES (arg_binfo
);
8188 n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
8190 /* Process base types. */
8191 for (i
= 0; i
< n_baselinks
; i
++)
8193 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
8196 /* Skip this base, if we've already seen it. */
8197 if (BINFO_MARKED (base_binfo
))
8201 (flags
& GTB_VIA_VIRTUAL
) || TREE_VIA_VIRTUAL (base_binfo
);
8203 /* When searching for a non-virtual, we cannot mark virtually
8206 SET_BINFO_MARKED (base_binfo
);
8208 rval
= get_template_base_recursive (tparms
, targs
,
8212 GTB_VIA_VIRTUAL
* this_virtual
);
8214 /* If we discovered more than one matching base class, we can
8216 if (rval
== error_mark_node
)
8217 return error_mark_node
;
8223 /* Given a template type PARM and a class type ARG, find the unique
8224 base type in ARG that is an instance of PARM. We do not examine
8225 ARG itself; only its base-classes. If there is no appropriate base
8226 class, return NULL_TREE. If there is more than one, return
8227 error_mark_node. PARM may be the type of a partial specialization,
8228 as well as a plain template type. Used by unify. */
8231 get_template_base (tparms
, targs
, parm
, arg
)
8240 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)), 92);
8242 arg_binfo
= TYPE_BINFO (complete_type (arg
));
8243 rval
= get_template_base_recursive (tparms
, targs
,
8248 /* Since get_template_base_recursive marks the bases classes, we
8249 must unmark them here. */
8250 dfs_walk (arg_binfo
, dfs_unmark
, markedp
, 0);
8255 /* Returns the level of DECL, which declares a template parameter. */
8258 template_decl_level (decl
)
8261 switch (TREE_CODE (decl
))
8265 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
8268 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
8271 my_friendly_abort (0);
8276 /* Decide whether ARG can be unified with PARM, considering only the
8277 cv-qualifiers of each type, given STRICT as documented for unify.
8278 Returns non-zero iff the unification is OK on that basis.*/
8281 check_cv_quals_for_unify (strict
, arg
, parm
)
8286 if (!(strict
& UNIFY_ALLOW_MORE_CV_QUAL
)
8287 && !at_least_as_qualified_p (arg
, parm
))
8290 if (!(strict
& UNIFY_ALLOW_LESS_CV_QUAL
)
8291 && !at_least_as_qualified_p (parm
, arg
))
8297 /* Takes parameters as for type_unification. Returns 0 if the
8298 type deduction suceeds, 1 otherwise. The parameter STRICT is a
8299 bitwise or of the following flags:
8302 Require an exact match between PARM and ARG.
8303 UNIFY_ALLOW_MORE_CV_QUAL:
8304 Allow the deduced ARG to be more cv-qualified than ARG.
8305 UNIFY_ALLOW_LESS_CV_QUAL:
8306 Allow the deduced ARG to be less cv-qualified than ARG.
8307 UNIFY_ALLOW_DERIVED:
8308 Allow the deduced ARG to be a template base class of ARG,
8309 or a pointer to a template base class of the type pointed to by
8311 UNIFY_ALLOW_INTEGER:
8312 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8313 case for more information. */
8316 unify (tparms
, targs
, parm
, arg
, strict
)
8317 tree tparms
, targs
, parm
, arg
;
8324 /* I don't think this will do the right thing with respect to types.
8325 But the only case I've seen it in so far has been array bounds, where
8326 signedness is the only information lost, and I think that will be
8328 while (TREE_CODE (parm
) == NOP_EXPR
)
8329 parm
= TREE_OPERAND (parm
, 0);
8331 if (arg
== error_mark_node
)
8333 if (arg
== unknown_type_node
)
8334 /* We can't deduce anything from this, but we might get all the
8335 template args from other function args. */
8338 /* If PARM uses template parameters, then we can't bail out here,
8339 even if ARG == PARM, since we won't record unifications for the
8340 template parameters. We might need them if we're trying to
8341 figure out which of two things is more specialized. */
8342 if (arg
== parm
&& !uses_template_parms (parm
))
8345 /* Immediately reject some pairs that won't unify because of
8346 cv-qualification mismatches. */
8347 if (TREE_CODE (arg
) == TREE_CODE (parm
)
8348 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
8349 /* We check the cv-qualifiers when unifying with template type
8350 parameters below. We want to allow ARG `const T' to unify with
8351 PARM `T' for example, when computing which of two templates
8352 is more specialized, for example. */
8353 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
8354 && !check_cv_quals_for_unify (strict
, arg
, parm
))
8357 switch (TREE_CODE (parm
))
8360 /* In a type which contains a nested-name-specifier, template
8361 argument values cannot be deduced for template parameters used
8362 within the nested-name-specifier. */
8365 case TEMPLATE_TYPE_PARM
:
8366 case TEMPLATE_TEMPLATE_PARM
:
8367 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8369 if (TEMPLATE_TYPE_LEVEL (parm
)
8370 != template_decl_level (tparm
))
8371 /* The PARM is not one we're trying to unify. Just check
8372 to see if it matches ARG. */
8373 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8374 && same_type_p (parm
, arg
)) ? 0 : 1;
8375 idx
= TEMPLATE_TYPE_IDX (parm
);
8376 targ
= TREE_VEC_ELT (targs
, idx
);
8377 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
8379 /* Check for mixed types and values. */
8380 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
8381 && TREE_CODE (tparm
) != TYPE_DECL
)
8382 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
8383 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
8386 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
8388 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm
))
8390 /* We arrive here when PARM does not involve template
8393 /* ARG must be constructed from a template class. */
8394 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
8398 tree parmtmpl
= TYPE_TI_TEMPLATE (parm
);
8399 tree parmvec
= TYPE_TI_ARGS (parm
);
8400 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
8402 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
8405 /* The parameter and argument roles have to be switched here
8406 in order to handle default arguments properly. For example,
8407 template<template <class> class TT> void f(TT<int>)
8408 should be able to accept vector<int> which comes from
8409 template <class T, class Allocator = allocator>
8412 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 0, 1)
8416 /* Deduce arguments T, i from TT<T> or TT<i>.
8417 We check each element of PARMVEC and ARGVEC individually
8418 rather than the whole TREE_VEC since they can have
8419 different number of elements. */
8421 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
8423 tree t
= TREE_VEC_ELT (parmvec
, i
);
8425 if (unify (tparms
, targs
, t
,
8426 TREE_VEC_ELT (argvec
, i
),
8431 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
8436 /* If PARM is `const T' and ARG is only `int', we don't have
8437 a match unless we are allowing additional qualification.
8438 If ARG is `const int' and PARM is just `T' that's OK;
8439 that binds `const int' to `T'. */
8440 if (!check_cv_quals_for_unify (strict
| UNIFY_ALLOW_LESS_CV_QUAL
,
8444 /* Consider the case where ARG is `const volatile int' and
8445 PARM is `const T'. Then, T should be `volatile int'. */
8447 cp_build_qualified_type_real (arg
,
8449 & ~CP_TYPE_QUALS (parm
),
8451 if (arg
== error_mark_node
)
8455 /* Simple cases: Value already set, does match or doesn't. */
8456 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
8461 /* Make sure that ARG is not a variable-sized array. (Note that
8462 were talking about variable-sized arrays (like `int[n]'),
8463 rather than arrays of unknown size (like `int[]').) We'll
8464 get very confused by such a type since the bound of the array
8465 will not be computable in an instantiation. Besides, such
8466 types are not allowed in ISO C++, so we can do as we please
8468 if (TREE_CODE (arg
) == ARRAY_TYPE
8469 && !uses_template_parms (arg
)
8470 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg
)))
8474 TREE_VEC_ELT (targs
, idx
) = arg
;
8477 case TEMPLATE_PARM_INDEX
:
8478 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
8480 if (TEMPLATE_PARM_LEVEL (parm
)
8481 != template_decl_level (tparm
))
8482 /* The PARM is not one we're trying to unify. Just check
8483 to see if it matches ARG. */
8484 return (TREE_CODE (arg
) == TREE_CODE (parm
)
8485 && cp_tree_equal (parm
, arg
) > 0) ? 0 : 1;
8487 idx
= TEMPLATE_PARM_IDX (parm
);
8488 targ
= TREE_VEC_ELT (targs
, idx
);
8492 int i
= (cp_tree_equal (targ
, arg
) > 0);
8498 my_friendly_abort (42);
8501 /* [temp.deduct.type] If, in the declaration of a function template
8502 with a non-type template-parameter, the non-type
8503 template-parameter is used in an expression in the function
8504 parameter-list and, if the corresponding template-argument is
8505 deduced, the template-argument type shall match the type of the
8506 template-parameter exactly, except that a template-argument
8507 deduced from an array bound may be of any integral type. */
8508 if (same_type_p (TREE_TYPE (arg
), TREE_TYPE (parm
)))
8510 else if ((strict
& UNIFY_ALLOW_INTEGER
)
8511 && (TREE_CODE (TREE_TYPE (parm
)) == INTEGER_TYPE
8512 || TREE_CODE (TREE_TYPE (parm
)) == BOOLEAN_TYPE
))
8517 TREE_VEC_ELT (targs
, idx
) = arg
;
8524 if (TREE_CODE (arg
) != POINTER_TYPE
)
8527 /* [temp.deduct.call]
8529 A can be another pointer or pointer to member type that can
8530 be converted to the deduced A via a qualification
8531 conversion (_conv.qual_).
8533 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8534 This will allow for additional cv-qualification of the
8535 pointed-to types if appropriate. In general, this is a bit
8536 too generous; we are only supposed to allow qualification
8537 conversions and this method will allow an ARG of char** and
8538 a deduced ARG of const char**. However, overload
8539 resolution will subsequently invalidate the candidate, so
8540 this is probably OK. */
8541 sub_strict
= strict
;
8543 if (TREE_CODE (TREE_TYPE (arg
)) != RECORD_TYPE
)
8544 /* The derived-to-base conversion only persists through one
8545 level of pointers. */
8546 sub_strict
&= ~UNIFY_ALLOW_DERIVED
;
8548 return unify (tparms
, targs
, TREE_TYPE (parm
),
8549 TREE_TYPE (arg
), sub_strict
);
8552 case REFERENCE_TYPE
:
8553 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
8555 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8559 if (TREE_CODE (arg
) != ARRAY_TYPE
)
8561 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
8562 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
8564 if (TYPE_DOMAIN (parm
) != NULL_TREE
8565 && unify (tparms
, targs
, TYPE_DOMAIN (parm
),
8566 TYPE_DOMAIN (arg
), UNIFY_ALLOW_NONE
) != 0)
8568 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8576 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8579 if (TREE_CODE (parm
) == INTEGER_TYPE
8580 && TREE_CODE (TYPE_MAX_VALUE (parm
)) != INTEGER_CST
)
8582 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
8583 && unify (tparms
, targs
, TYPE_MIN_VALUE (parm
),
8584 TYPE_MIN_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8586 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
8587 && unify (tparms
, targs
, TYPE_MAX_VALUE (parm
),
8588 TYPE_MAX_VALUE (arg
), UNIFY_ALLOW_INTEGER
))
8591 /* We use the TYPE_MAIN_VARIANT since we have already
8592 checked cv-qualification at the top of the
8594 else if (!same_type_p (TYPE_MAIN_VARIANT (arg
),
8595 TYPE_MAIN_VARIANT (parm
)))
8598 /* As far as unification is concerned, this wins. Later checks
8599 will invalidate it if necessary. */
8602 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
8603 /* Type INTEGER_CST can come from ordinary constant template args. */
8605 while (TREE_CODE (arg
) == NOP_EXPR
)
8606 arg
= TREE_OPERAND (arg
, 0);
8608 if (TREE_CODE (arg
) != INTEGER_CST
)
8610 return !tree_int_cst_equal (parm
, arg
);
8615 if (TREE_CODE (arg
) != TREE_VEC
)
8617 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
8619 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
8620 if (unify (tparms
, targs
,
8621 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
8629 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8632 if (TYPE_PTRMEMFUNC_P (parm
))
8634 if (!TYPE_PTRMEMFUNC_P (arg
))
8637 return unify (tparms
, targs
,
8638 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
8639 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
8643 if (CLASSTYPE_TEMPLATE_INFO (parm
))
8647 if (strict
& UNIFY_ALLOW_DERIVED
)
8649 /* First, we try to unify the PARM and ARG directly. */
8650 t
= try_class_unification (tparms
, targs
,
8655 /* Fallback to the special case allowed in
8658 If P is a class, and P has the form
8659 template-id, then A can be a derived class of
8660 the deduced A. Likewise, if P is a pointer to
8661 a class of the form template-id, A can be a
8662 pointer to a derived class pointed to by the
8664 t
= get_template_base (tparms
, targs
,
8667 if (! t
|| t
== error_mark_node
)
8671 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
8672 && (CLASSTYPE_TI_TEMPLATE (parm
)
8673 == CLASSTYPE_TI_TEMPLATE (arg
)))
8674 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8675 Then, we should unify `int' and `U'. */
8678 /* There's no chance of unication succeeding. */
8681 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
8682 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
8684 else if (!same_type_p (TYPE_MAIN_VARIANT (parm
),
8685 TYPE_MAIN_VARIANT (arg
)))
8691 if (TREE_CODE (arg
) != TREE_CODE (parm
))
8694 if (unify (tparms
, targs
, TREE_TYPE (parm
),
8695 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
8697 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
8698 TYPE_ARG_TYPES (arg
), 1,
8702 if (TREE_CODE (arg
) != OFFSET_TYPE
)
8704 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
8705 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
8707 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
8711 if (arg
!= decl_constant_value (parm
))
8716 /* Matched cases are handled by the ARG == PARM test above. */
8720 if (TREE_CODE (TREE_OPERAND (parm
, 1)) == INTEGER_CST
)
8722 /* We handle this case specially, since it comes up with
8723 arrays. In particular, something like:
8725 template <int N> void f(int (&x)[N]);
8727 Here, we are trying to unify the range type, which
8728 looks like [0 ... (N - 1)]. */
8730 t1
= TREE_OPERAND (parm
, 0);
8731 t2
= TREE_OPERAND (parm
, 1);
8733 /* Should this be a regular fold? */
8734 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
8738 return unify (tparms
, targs
, t1
, t
, strict
);
8740 /* else fall through */
8743 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
8744 /* We're looking at an expression. This can happen with
8748 void foo(S<I>, S<I + 2>);
8750 This is a "nondeduced context":
8754 The nondeduced contexts are:
8756 --A type that is a template-id in which one or more of
8757 the template-arguments is an expression that references
8758 a template-parameter.
8760 In these cases, we assume deduction succeeded, but don't
8761 actually infer any unifications. */
8764 sorry ("use of `%s' in template type unification",
8765 tree_code_name
[(int) TREE_CODE (parm
)]);
8771 /* Called if RESULT is explicitly instantiated, or is a member of an
8772 explicitly instantiated class, or if using -frepo and the
8773 instantiation of RESULT has been assigned to this file. */
8776 mark_decl_instantiated (result
, extern_p
)
8780 if (TREE_CODE (result
) != FUNCTION_DECL
)
8781 /* The TREE_PUBLIC flag for function declarations will have been
8782 set correctly by tsubst. */
8783 TREE_PUBLIC (result
) = 1;
8787 DECL_INTERFACE_KNOWN (result
) = 1;
8788 DECL_NOT_REALLY_EXTERN (result
) = 1;
8790 /* Always make artificials weak. */
8791 if (DECL_ARTIFICIAL (result
) && flag_weak
)
8792 comdat_linkage (result
);
8793 /* For WIN32 we also want to put explicit instantiations in
8794 linkonce sections. */
8795 else if (TREE_PUBLIC (result
))
8796 maybe_make_one_only (result
);
8798 else if (TREE_CODE (result
) == FUNCTION_DECL
)
8799 mark_inline_for_output (result
);
8802 /* Given two function templates PAT1 and PAT2, and explicit template
8803 arguments EXPLICIT_ARGS return:
8805 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8806 -1 if PAT2 is more specialized than PAT1.
8807 0 if neither is more specialized. */
8810 more_specialized (pat1
, pat2
, explicit_args
)
8811 tree pat1
, pat2
, explicit_args
;
8816 targs
= get_bindings_overload (pat1
, DECL_RESULT (pat2
), explicit_args
);
8820 targs
= get_bindings_overload (pat2
, DECL_RESULT (pat1
), explicit_args
);
8827 /* Given two class template specialization list nodes PAT1 and PAT2, return:
8829 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8830 -1 if PAT2 is more specialized than PAT1.
8831 0 if neither is more specialized. */
8834 more_specialized_class (pat1
, pat2
)
8840 targs
= get_class_bindings (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
8841 TREE_PURPOSE (pat2
));
8845 targs
= get_class_bindings (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
8846 TREE_PURPOSE (pat1
));
8853 /* Return the template arguments that will produce the function signature
8854 DECL from the function template FN, with the explicit template
8855 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8856 also match. Return NULL_TREE if no satisfactory arguments could be
8860 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
8861 tree fn
, decl
, explicit_args
;
8864 int ntparms
= DECL_NTPARMS (fn
);
8865 tree targs
= make_scratch_vec (ntparms
);
8867 tree decl_arg_types
;
8870 /* Substitute the explicit template arguments into the type of DECL.
8871 The call to fn_type_unification will handle substitution into the
8873 decl_type
= TREE_TYPE (decl
);
8874 if (explicit_args
&& uses_template_parms (decl_type
))
8877 tree converted_args
;
8879 if (DECL_TEMPLATE_INFO (decl
))
8880 tmpl
= DECL_TI_TEMPLATE (decl
);
8882 /* We can get here for some illegal specializations. */
8886 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
8887 explicit_args
, NULL_TREE
,
8889 /*require_all_arguments=*/0));
8890 if (converted_args
== error_mark_node
)
8893 decl_type
= tsubst (decl_type
, converted_args
, /*complain=*/0,
8895 if (decl_type
== error_mark_node
)
8899 /* If FN is a static member function, adjust the type of DECL
8901 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
8902 if (DECL_STATIC_FUNCTION_P (fn
)
8903 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
8904 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
8906 i
= fn_type_unification (fn
, explicit_args
, targs
,
8908 TREE_TYPE (decl_type
),
8916 /* Check to see that the resulting return type is also OK. */
8917 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)), targs
,
8918 /*complain=*/0, NULL_TREE
);
8920 if (!same_type_p (t
, TREE_TYPE (TREE_TYPE (decl
))))
8927 /* For most uses, we want to check the return type. */
8930 get_bindings (fn
, decl
, explicit_args
)
8931 tree fn
, decl
, explicit_args
;
8933 return get_bindings_real (fn
, decl
, explicit_args
, 1);
8936 /* But for more_specialized, we only care about the parameter types. */
8939 get_bindings_overload (fn
, decl
, explicit_args
)
8940 tree fn
, decl
, explicit_args
;
8942 return get_bindings_real (fn
, decl
, explicit_args
, 0);
8945 /* Return the innermost template arguments that, when applied to a
8946 template specialization whose innermost template parameters are
8947 TPARMS, and whose specialization arguments are ARGS, yield the
8950 For example, suppose we have:
8952 template <class T, class U> struct S {};
8953 template <class T> struct S<T*, int> {};
8955 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8956 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8957 int}. The resulting vector will be {double}, indicating that `T'
8958 is bound to `double'. */
8961 get_class_bindings (tparms
, parms
, args
)
8962 tree tparms
, parms
, args
;
8964 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
8965 tree vec
= make_temp_vec (ntparms
);
8967 args
= innermost_args (args
);
8969 if (unify (tparms
, vec
, parms
, args
, UNIFY_ALLOW_NONE
))
8972 for (i
= 0; i
< ntparms
; ++i
)
8973 if (! TREE_VEC_ELT (vec
, i
))
8979 /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8980 Pick the most specialized template, and return the corresponding
8981 instantiation, or if there is no corresponding instantiation, the
8982 template itself. EXPLICIT_ARGS is any template arguments explicity
8983 mentioned in a template-id. If there is no most specialized
8984 tempalte, error_mark_node is returned. If there are no templates
8985 at all, NULL_TREE is returned. */
8988 most_specialized_instantiation (instantiations
, explicit_args
)
8989 tree instantiations
;
8995 if (!instantiations
)
8998 champ
= instantiations
;
8999 for (fn
= TREE_CHAIN (instantiations
); fn
; fn
= TREE_CHAIN (fn
))
9001 fate
= more_specialized (TREE_VALUE (champ
),
9002 TREE_VALUE (fn
), explicit_args
);
9009 fn
= TREE_CHAIN (fn
);
9011 return error_mark_node
;
9017 for (fn
= instantiations
; fn
&& fn
!= champ
; fn
= TREE_CHAIN (fn
))
9019 fate
= more_specialized (TREE_VALUE (champ
),
9020 TREE_VALUE (fn
), explicit_args
);
9022 return error_mark_node
;
9025 return TREE_PURPOSE (champ
) ? TREE_PURPOSE (champ
) : TREE_VALUE (champ
);
9028 /* Return the most specialized of the list of templates in FNS that can
9029 produce an instantiation matching DECL, given the explicit template
9030 arguments EXPLICIT_ARGS. */
9033 most_specialized (fns
, decl
, explicit_args
)
9034 tree fns
, decl
, explicit_args
;
9036 tree candidates
= NULL_TREE
;
9039 for (fn
= fns
; fn
; fn
= TREE_CHAIN (fn
))
9041 tree candidate
= TREE_VALUE (fn
);
9043 args
= get_bindings (candidate
, decl
, explicit_args
);
9045 candidates
= tree_cons (NULL_TREE
, candidate
, candidates
);
9048 return most_specialized_instantiation (candidates
, explicit_args
);
9051 /* If DECL is a specialization of some template, return the most
9052 general such template. For example, given:
9054 template <class T> struct S { template <class U> void f(U); };
9056 if TMPL is `template <class U> void S<int>::f(U)' this will return
9057 the full template. This function will not trace past partial
9058 specializations, however. For example, given in addition:
9060 template <class T> struct S<T*> { template <class U> void f(U); };
9062 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9063 `template <class T> template <class U> S<T*>::f(U)'. */
9066 most_general_template (decl
)
9069 while (DECL_TEMPLATE_INFO (decl
))
9070 decl
= DECL_TI_TEMPLATE (decl
);
9075 /* Return the most specialized of the class template specializations
9076 of TMPL which can produce an instantiation matching ARGS, or
9077 error_mark_node if the choice is ambiguous. */
9080 most_specialized_class (tmpl
, args
)
9084 tree list
= NULL_TREE
;
9089 tmpl
= most_general_template (tmpl
);
9090 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
9093 = get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
9096 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
9097 TREE_TYPE (list
) = TREE_TYPE (t
);
9107 for (; t
; t
= TREE_CHAIN (t
))
9109 fate
= more_specialized_class (champ
, t
);
9118 return error_mark_node
;
9124 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
9126 fate
= more_specialized_class (champ
, t
);
9128 return error_mark_node
;
9134 /* called from the parser. */
9137 do_decl_instantiation (declspecs
, declarator
, storage
)
9138 tree declspecs
, declarator
, storage
;
9140 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
9141 tree result
= NULL_TREE
;
9145 /* An error ocurred, for which grokdeclarator has already issued
9146 an appropriate message. */
9148 else if (! DECL_LANG_SPECIFIC (decl
))
9150 cp_error ("explicit instantiation of non-template `%#D'", decl
);
9153 else if (TREE_CODE (decl
) == VAR_DECL
)
9155 /* There is an asymmetry here in the way VAR_DECLs and
9156 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9157 the latter, the DECL we get back will be marked as a
9158 template instantiation, and the appropriate
9159 DECL_TEMPLATE_INFO will be set up. This does not happen for
9160 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9161 should handle VAR_DECLs as it currently handles
9163 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
9164 if (result
&& TREE_CODE (result
) != VAR_DECL
)
9166 cp_error ("no matching template for `%D' found", result
);
9170 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
9172 cp_error ("explicit instantiation of `%#D'", decl
);
9178 /* Check for various error cases. Note that if the explicit
9179 instantiation is legal the RESULT will currently be marked as an
9180 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9181 until we get here. */
9183 if (DECL_TEMPLATE_SPECIALIZATION (result
))
9187 No program shall both explicitly instantiate and explicitly
9188 specialize a template. */
9189 cp_pedwarn ("explicit instantiation of `%#D' after", result
);
9190 cp_pedwarn_at ("explicit specialization here", result
);
9193 else if (DECL_EXPLICIT_INSTANTIATION (result
))
9197 No program shall explicitly instantiate any template more
9200 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9201 instantiation was `extern' and the second is not, and EXTERN_P for
9202 the opposite case. If -frepo, chances are we already got marked
9203 as an explicit instantion because of the repo file. */
9204 if (DECL_INTERFACE_KNOWN (result
) && !extern_p
&& !flag_use_repository
)
9205 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result
);
9207 /* If we've already instantiated the template, just return now. */
9208 if (DECL_INTERFACE_KNOWN (result
))
9211 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
9213 cp_error ("no matching template for `%D' found", result
);
9216 else if (!DECL_TEMPLATE_INFO (result
))
9218 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
9222 if (flag_external_templates
)
9225 if (storage
== NULL_TREE
)
9227 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9230 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
9234 cp_error ("storage class `%D' applied to template instantiation",
9237 SET_DECL_EXPLICIT_INSTANTIATION (result
);
9238 mark_decl_instantiated (result
, extern_p
);
9239 repo_template_instantiated (result
, extern_p
);
9241 instantiate_decl (result
);
9245 mark_class_instantiated (t
, extern_p
)
9249 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
9250 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
9251 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
9252 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
9253 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
9256 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
9257 rest_of_type_compilation (t
, 1);
9262 do_type_instantiation (t
, storage
)
9269 if (TREE_CODE (t
) == TYPE_DECL
)
9272 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
9274 cp_error ("explicit instantiation of non-template type `%T'", t
);
9280 /* With -fexternal-templates, explicit instantiations are treated the same
9281 as implicit ones. */
9282 if (flag_external_templates
)
9285 if (TYPE_SIZE (t
) == NULL_TREE
)
9287 cp_error ("explicit instantiation of `%#T' before definition of template",
9292 if (storage
!= NULL_TREE
)
9295 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
9296 IDENTIFIER_POINTER (storage
));
9298 if (storage
== ridpointers
[(int) RID_INLINE
])
9300 else if (storage
== ridpointers
[(int) RID_EXTERN
])
9302 else if (storage
== ridpointers
[(int) RID_STATIC
])
9306 cp_error ("storage class `%D' applied to template instantiation",
9312 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
9316 No program shall both explicitly instantiate and explicitly
9317 specialize a template. */
9318 cp_error ("explicit instantiation of `%#T' after", t
);
9319 cp_error_at ("explicit specialization here", t
);
9322 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
9326 No program shall explicitly instantiate any template more
9329 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9330 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9331 are we already got marked as an explicit instantion because of the
9332 repo file. All these cases are OK. */
9333 if (!CLASSTYPE_INTERFACE_ONLY (t
) && !extern_p
&& !flag_use_repository
)
9334 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t
);
9336 /* If we've already instantiated the template, just return now. */
9337 if (!CLASSTYPE_INTERFACE_ONLY (t
))
9341 mark_class_instantiated (t
, extern_p
);
9342 repo_template_instantiated (t
, extern_p
);
9350 /* In contrast to implicit instantiation, where only the
9351 declarations, and not the definitions, of members are
9352 instantiated, we have here:
9356 The explicit instantiation of a class template specialization
9357 implies the instantiation of all of its members not
9358 previously explicitly specialized in the translation unit
9359 containing the explicit instantiation.
9361 Of course, we can't instantiate member template classes, since
9362 we don't have any arguments for them. Note that the standard
9363 is unclear on whether the instatiation of the members are
9364 *explicit* instantiations or not. We choose to be generous,
9365 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9366 the explicit instantiation of a class where some of the members
9367 have no definition in the current translation unit. */
9370 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9371 if (TREE_CODE (tmp
) == FUNCTION_DECL
9372 && DECL_TEMPLATE_INSTANTIATION (tmp
))
9374 mark_decl_instantiated (tmp
, extern_p
);
9375 repo_template_instantiated (tmp
, extern_p
);
9377 instantiate_decl (tmp
);
9380 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9381 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
9383 mark_decl_instantiated (tmp
, extern_p
);
9384 repo_template_instantiated (tmp
, extern_p
);
9386 instantiate_decl (tmp
);
9389 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
9390 if (IS_AGGR_TYPE (TREE_VALUE (tmp
))
9391 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp
))))
9392 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
9396 /* Given a function DECL, which is a specialization of TMPL, modify
9397 DECL to be a re-instantiation of TMPL with the same template
9398 arguments. TMPL should be the template into which tsubst'ing
9399 should occur for DECL, not the most general template.
9401 One reason for doing this is a scenario like this:
9404 void f(const T&, int i);
9406 void g() { f(3, 7); }
9409 void f(const T& t, const int i) { }
9411 Note that when the template is first instantiated, with
9412 instantiate_template, the resulting DECL will have no name for the
9413 first parameter, and the wrong type for the second. So, when we go
9414 to instantiate the DECL, we regenerate it. */
9417 regenerate_decl_from_template (decl
, tmpl
)
9427 args
= DECL_TI_ARGS (decl
);
9428 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9430 /* Unregister the specialization so that when we tsubst we will not
9431 just return DECL. We don't have to unregister DECL from TMPL
9432 because if would only be registered there if it were a partial
9433 instantiation of a specialization, which it isn't: it's a full
9435 gen_tmpl
= most_general_template (tmpl
);
9436 unregistered
= unregister_specialization (decl
, gen_tmpl
);
9438 /* If the DECL was not unregistered then something peculiar is
9439 happening: we created a specialization but did not call
9440 register_specialization for it. */
9441 my_friendly_assert (unregistered
, 0);
9443 if (TREE_CODE (decl
) == VAR_DECL
)
9444 /* Make sure that we can see identifiers, and compute access
9445 correctly, for the class members used in the declaration of
9446 this static variable. */
9447 pushclass (DECL_CONTEXT (decl
), 2);
9449 /* Do the substitution to get the new declaration. */
9450 new_decl
= tsubst (code_pattern
, args
, /*complain=*/1, NULL_TREE
);
9452 if (TREE_CODE (decl
) == VAR_DECL
)
9454 /* Set up DECL_INITIAL, since tsubst doesn't. */
9455 DECL_INITIAL (new_decl
) =
9456 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
9457 /*complain=*/1, DECL_TI_TEMPLATE (decl
));
9458 /* Pop the class context we pushed above. */
9461 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
9463 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9465 DECL_INITIAL (new_decl
) = error_mark_node
;
9466 /* And don't complain about a duplicate definition. */
9467 DECL_INITIAL (decl
) = NULL_TREE
;
9470 /* The immediate parent of the new template is still whatever it was
9471 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9472 general template. We also reset the DECL_ASSEMBLER_NAME since
9473 tsubst always calculates the name as if the function in question
9474 were really a template instance, and sometimes, with friend
9475 functions, this is not so. See tsubst_friend_function for
9477 DECL_TI_TEMPLATE (new_decl
) = DECL_TI_TEMPLATE (decl
);
9478 DECL_ASSEMBLER_NAME (new_decl
) = DECL_ASSEMBLER_NAME (decl
);
9479 DECL_RTL (new_decl
) = DECL_RTL (decl
);
9481 /* Call duplicate decls to merge the old and new declarations. */
9482 duplicate_decls (new_decl
, decl
);
9484 /* Now, re-register the specialization. */
9485 register_specialization (decl
, gen_tmpl
, args
);
9488 /* Produce the definition of D, a _DECL generated from a template. */
9491 instantiate_decl (d
)
9494 tree tmpl
= DECL_TI_TEMPLATE (d
);
9495 tree args
= DECL_TI_ARGS (d
);
9500 int nested
= in_function_p ();
9501 int pattern_defined
;
9503 char *file
= input_filename
;
9505 /* This function should only be used to instantiate templates for
9506 functions and static member variables. */
9507 my_friendly_assert (TREE_CODE (d
) == FUNCTION_DECL
9508 || TREE_CODE (d
) == VAR_DECL
, 0);
9510 if (DECL_TEMPLATE_INSTANTIATED (d
))
9511 /* D has already been instantiated. It might seem reasonable to
9512 check whether or not D is an explict instantiation, and, if so,
9513 stop here. But when an explicit instantiation is deferred
9514 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9515 is set, even though we still need to do the instantiation. */
9518 /* If we already have a specialization of this declaration, then
9519 there's no reason to instantiate it. Note that
9520 retrieve_specialization gives us both instantiations and
9521 specializations, so we must explicitly check
9522 DECL_TEMPLATE_SPECIALIZATION. */
9523 gen_tmpl
= most_general_template (tmpl
);
9524 spec
= retrieve_specialization (gen_tmpl
, args
);
9525 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
9528 /* This needs to happen before any tsubsting. */
9529 if (! push_tinst_level (d
))
9532 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9533 for the instantiation. This is not always the most general
9534 template. Consider, for example:
9537 struct S { template <class U> void f();
9538 template <> void f<int>(); };
9540 and an instantiation of S<double>::f<int>. We want TD to be the
9541 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9544 /* An instantiation cannot have a definition, so we need a
9545 more general template. */
9546 DECL_TEMPLATE_INSTANTIATION (td
)
9547 /* We must also deal with friend templates. Given:
9549 template <class T> struct S {
9550 template <class U> friend void f() {};
9553 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9554 so far as the language is concerned, but that's still
9555 where we get the pattern for the instantiation from. On
9556 ther hand, if the definition comes outside the class, say:
9558 template <class T> struct S {
9559 template <class U> friend void f();
9561 template <class U> friend void f() {}
9563 we don't need to look any further. That's what the check for
9564 DECL_INITIAL is for. */
9565 || (TREE_CODE (d
) == FUNCTION_DECL
9566 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td
)
9567 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td
)));
9570 /* The present template, TD, should not be a definition. If it
9571 were a definition, we should be using it! Note that we
9572 cannot restructure the loop to just keep going until we find
9573 a template with a definition, since that might go too far if
9574 a specialization was declared, but not defined. */
9575 my_friendly_assert (!(TREE_CODE (d
) == VAR_DECL
9576 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td
))),
9579 /* Fetch the more general template. */
9580 td
= DECL_TI_TEMPLATE (td
);
9583 code_pattern
= DECL_TEMPLATE_RESULT (td
);
9585 if (TREE_CODE (d
) == FUNCTION_DECL
)
9586 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
9588 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
9590 push_to_top_level ();
9591 lineno
= DECL_SOURCE_LINE (d
);
9592 input_filename
= DECL_SOURCE_FILE (d
);
9594 if (pattern_defined
)
9596 repo_template_used (d
);
9598 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
9600 if (flag_alt_external_templates
)
9602 if (interface_unknown
)
9603 warn_if_unknown_interface (d
);
9605 else if (DECL_INTERFACE_KNOWN (code_pattern
))
9607 DECL_INTERFACE_KNOWN (d
) = 1;
9608 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
9611 warn_if_unknown_interface (code_pattern
);
9615 import_export_decl (d
);
9618 /* Reject all external templates except inline functions. */
9619 if (DECL_INTERFACE_KNOWN (d
)
9620 && ! DECL_NOT_REALLY_EXTERN (d
)
9621 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
9624 if (TREE_CODE (d
) == VAR_DECL
9625 && TREE_READONLY (d
)
9626 && DECL_INITIAL (d
) == NULL_TREE
9627 && DECL_INITIAL (code_pattern
) != NULL_TREE
)
9628 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9629 the variable is a static const initialized in the class body. */;
9630 else if (! pattern_defined
9631 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
9634 /* Defer all templates except inline functions used in another
9635 function. We restore the source position here because it's used
9636 by add_pending_template. */
9638 input_filename
= file
;
9640 if (at_eof
&& !pattern_defined
9641 && DECL_EXPLICIT_INSTANTIATION (d
))
9644 The definition of a non-exported function template, a
9645 non-exported member function template, or a non-exported
9646 member function or static data member of a class template
9647 shall be present in every translation unit in which it is
9648 explicitly instantiated. */
9649 cp_error ("explicit instantiation of `%D' but no definition available",
9652 add_pending_template (d
);
9656 /* We're now committed to instantiating this template. Mark it as
9657 instantiated so that recursive calls to instantiate_decl do not
9658 try to instantiate it again. */
9659 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
9661 /* Regenerate the declaration in case the template has been modified
9662 by a subsequent redeclaration. */
9663 regenerate_decl_from_template (d
, td
);
9665 /* We already set the file and line above. Reset them now in case
9666 they changed as a result of calling regenerate_decl_from_template. */
9667 lineno
= DECL_SOURCE_LINE (d
);
9668 input_filename
= DECL_SOURCE_FILE (d
);
9670 if (TREE_CODE (d
) == VAR_DECL
)
9672 DECL_IN_AGGR_P (d
) = 0;
9673 if (DECL_INTERFACE_KNOWN (d
))
9674 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
9677 DECL_EXTERNAL (d
) = 1;
9678 DECL_NOT_REALLY_EXTERN (d
) = 1;
9680 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
9682 else if (TREE_CODE (d
) == FUNCTION_DECL
)
9684 extern struct obstack
*saveable_obstack
;
9685 extern struct obstack
*rtl_obstack
;
9687 /* Set up context. */
9688 start_function (NULL_TREE
, d
, NULL_TREE
, SF_PRE_PARSED
);
9689 store_parm_decls ();
9691 /* Anything we might
9692 want to save is going to have to be saved forever. Note that
9693 we don't want to save all kinds of temporary clutter that
9694 might end up on the temporary obstack so we don't want to
9695 call push_permanent_obstack. */
9696 push_obstacks_nochange ();
9697 saveable_obstack
= &permanent_obstack
;
9698 /* We only need this because of the cases where we generate
9699 RTL_EXPRs. We should really be generating RTL_EXPRs until
9700 final expansion time; when that is fixed, this can go. */
9701 rtl_obstack
= &permanent_obstack
;
9702 /* Substitute into the body of the function. */
9703 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
9704 /*complain=*/1, tmpl
);
9708 finish_function (lineno
, 0);
9710 /* Now, generate RTL for the function. */
9716 input_filename
= file
;
9718 pop_from_top_level ();
9724 /* Run through the list of templates that we wish we could
9725 instantiate, and instantiate any we can. */
9728 instantiate_pending_templates ()
9731 int instantiated_something
= 0;
9738 t
= &pending_templates
;
9741 tree srcloc
= TREE_PURPOSE (*t
);
9742 tree instantiation
= TREE_VALUE (*t
);
9744 input_filename
= SRCLOC_FILE (srcloc
);
9745 lineno
= SRCLOC_LINE (srcloc
);
9747 if (TREE_CODE_CLASS (TREE_CODE (instantiation
)) == 't')
9751 if (!TYPE_SIZE (instantiation
))
9753 instantiate_class_template (instantiation
);
9754 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
9755 for (fn
= TYPE_METHODS (instantiation
);
9757 fn
= TREE_CHAIN (fn
))
9758 if (! DECL_ARTIFICIAL (fn
))
9759 instantiate_decl (fn
);
9760 if (TYPE_SIZE (instantiation
))
9762 instantiated_something
= 1;
9767 if (TYPE_SIZE (instantiation
))
9768 /* If INSTANTIATION has been instantiated, then we don't
9769 need to consider it again in the future. */
9770 *t
= TREE_CHAIN (*t
);
9772 t
= &TREE_CHAIN (*t
);
9776 if (DECL_TEMPLATE_INSTANTIATION (instantiation
)
9777 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
9779 instantiation
= instantiate_decl (instantiation
);
9780 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
9782 instantiated_something
= 1;
9787 if (!DECL_TEMPLATE_INSTANTIATION (instantiation
)
9788 || DECL_TEMPLATE_INSTANTIATED (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
);
9798 /* Go through the things that are template instantiations if we are
9799 using guiding declarations. */
9800 t
= &maybe_templates
;
9807 fn
= TREE_VALUE (*t
);
9809 if (DECL_INITIAL (fn
))
9810 /* If the FN is already defined, then it was either already
9811 instantiated or, even though guiding declarations were
9812 allowed, a non-template definition was provided. */
9816 template = TREE_PURPOSE (*t
);
9817 args
= get_bindings (template, fn
, NULL_TREE
);
9818 fn
= instantiate_template (template, args
);
9819 instantiate_decl (fn
);
9823 /* Remove this entry from the chain. */
9824 *t
= TREE_CHAIN (*t
);
9826 maybe_template_tail
= t
;
9830 return instantiated_something
;
9833 /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9834 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9835 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9838 tsubst_expr_values (t
, argvec
)
9841 tree first
= NULL_TREE
;
9844 for (; t
; t
= TREE_CHAIN (t
))
9846 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
9847 /*complain=*/1, NULL_TREE
);
9848 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, /*complain=*/1,
9850 *p
= build_tree_list (pur
, val
);
9851 p
= &TREE_CHAIN (*p
);
9860 last_tree
= TREE_CHAIN (last_tree
) = t
;
9867 if (current_function
)
9869 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
9870 last_tree
= NULL_TREE
;
9873 saved_trees
= tree_cons (NULL_TREE
, NULL_TREE
, saved_trees
);
9880 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
9882 if (current_function
)
9883 last_tree
= TREE_VALUE (saved_trees
);
9884 saved_trees
= TREE_CHAIN (saved_trees
);
9887 /* D is an undefined function declaration in the presence of templates with
9888 the same name, listed in FNS. If one of them can produce D as an
9889 instantiation, remember this so we can instantiate it at EOF if D has
9890 not been defined by that time. */
9893 add_maybe_template (d
, fns
)
9898 if (DECL_MAYBE_TEMPLATE (d
))
9901 t
= most_specialized (fns
, d
, NULL_TREE
);
9904 if (t
== error_mark_node
)
9906 cp_error ("ambiguous template instantiation for `%D'", d
);
9910 *maybe_template_tail
= tree_cons (t
, d
, NULL_TREE
);
9911 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
9912 DECL_MAYBE_TEMPLATE (d
) = 1;
9915 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9918 set_current_access_from_decl (decl
)
9921 if (TREE_PRIVATE (decl
))
9922 current_access_specifier
= access_private_node
;
9923 else if (TREE_PROTECTED (decl
))
9924 current_access_specifier
= access_protected_node
;
9926 current_access_specifier
= access_public_node
;
9929 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
9930 is the instantiation (which should have been created with
9931 start_enum) and ARGS are the template arguments to use. */
9934 tsubst_enum (tag
, newtag
, args
)
9941 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
9946 /* Note that in a template enum, the TREE_VALUE is the
9947 CONST_DECL, not the corresponding INTEGER_CST. */
9948 value
= tsubst_expr (DECL_INITIAL (TREE_VALUE (e
)),
9949 args
, /*complain=*/1,
9952 /* Give this enumeration constant the correct access. */
9953 set_current_access_from_decl (TREE_VALUE (e
));
9955 /* Actually build the enumerator itself. */
9956 elt
= build_enumerator (TREE_PURPOSE (e
), value
, newtag
);
9958 /* We save the enumerators we have built so far in the
9959 TYPE_VALUES so that if the enumeration constants for
9960 subsequent enumerators involve those for previous ones,
9961 tsubst_copy will be able to find them. */
9962 TREE_CHAIN (elt
) = TYPE_VALUES (newtag
);
9963 TYPE_VALUES (newtag
) = elt
;
9966 finish_enum (newtag
);
9969 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9970 is either an instantiation or specialization of a template
9974 set_mangled_name_for_template_decl (decl
)
9977 tree saved_namespace
;
9978 tree context
= NULL_TREE
;
9987 my_friendly_assert (TREE_CODE (decl
) == FUNCTION_DECL
, 0);
9988 my_friendly_assert (DECL_TEMPLATE_INFO (decl
) != NULL_TREE
, 0);
9990 /* The names of template functions must be mangled so as to indicate
9991 what template is being specialized with what template arguments.
9992 For example, each of the following three functions must get
9993 different mangled names:
9996 template <> void f<7>(int);
9997 template <> void f<8>(int); */
9999 targs
= DECL_TI_ARGS (decl
);
10000 if (uses_template_parms (targs
))
10001 /* This DECL is for a partial instantiation. There's no need to
10002 mangle the name of such an entity. */
10005 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10006 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
10007 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
10009 /* There should be as many levels of arguments as there are levels
10011 my_friendly_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
), 0);
10013 /* We now compute the PARMS and RET_TYPE to give to
10014 build_decl_overload_real. The PARMS and RET_TYPE are the
10015 parameter and return types of the template, after all but the
10016 innermost template arguments have been substituted, not the
10017 parameter and return types of the function DECL. For example,
10020 template <class T> T f(T);
10022 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10023 A more subtle example is:
10025 template <class T> struct S { template <class U> void f(T, U); }
10027 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10028 {int, U}. Thus, the args that we want to subsitute into the
10029 return and parameter type for the function are those in TARGS,
10030 with the innermost level omitted. */
10031 fn_type
= TREE_TYPE (tmpl
);
10032 if (DECL_STATIC_FUNCTION_P (decl
))
10033 context
= DECL_CLASS_CONTEXT (decl
);
10035 if (parm_depth
== 1)
10036 /* No substitution is necessary. */
10043 /* Replace the innermost level of the TARGS with NULL_TREEs to
10044 let tsubst know not to subsitute for those parameters. */
10045 partial_args
= make_temp_vec (TREE_VEC_LENGTH (targs
));
10046 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
10047 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
10048 TMPL_ARGS_LEVEL (targs
, i
));
10049 SET_TMPL_ARGS_LEVEL (partial_args
,
10050 TMPL_ARGS_DEPTH (targs
),
10051 make_temp_vec (DECL_NTPARMS (tmpl
)));
10053 /* Now, do the (partial) substitution to figure out the
10054 appropriate function type. */
10055 fn_type
= tsubst (fn_type
, partial_args
, /*complain=*/1, NULL_TREE
);
10056 if (DECL_STATIC_FUNCTION_P (decl
))
10057 context
= tsubst (context
, partial_args
, /*complain=*/1, NULL_TREE
);
10059 /* Substitute into the template parameters to obtain the real
10060 innermost set of parameters. This step is important if the
10061 innermost set of template parameters contains value
10062 parameters whose types depend on outer template parameters. */
10063 TREE_VEC_LENGTH (partial_args
)--;
10064 tparms
= tsubst_template_parms (tparms
, partial_args
, /*complain=*/1);
10067 /* Now, get the innermost parameters and arguments, and figure out
10068 the parameter and return types. */
10069 tparms
= INNERMOST_TEMPLATE_PARMS (tparms
);
10070 targs
= innermost_args (targs
);
10071 ret_type
= TREE_TYPE (fn_type
);
10072 parm_types
= TYPE_ARG_TYPES (fn_type
);
10074 /* For a static member function, we generate a fake `this' pointer,
10075 for the purposes of mangling. This indicates of which class the
10076 function is a member. Because of:
10080 There shall not be a static and a nonstatic member function
10081 with the same name and the same parameter types
10083 we don't have to worry that this will result in a clash with a
10084 non-static member function. */
10085 if (DECL_STATIC_FUNCTION_P (decl
))
10086 parm_types
= hash_tree_chain (build_pointer_type (context
), parm_types
);
10088 /* There should be the same number of template parameters as
10089 template arguments. */
10090 my_friendly_assert (TREE_VEC_LENGTH (tparms
) == TREE_VEC_LENGTH (targs
),
10093 /* If the template is in a namespace, we need to put that into the
10094 mangled name. Unfortunately, build_decl_overload_real does not
10095 get the decl to mangle, so it relies on the current
10096 namespace. Therefore, we set that here temporarily. */
10097 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 'd', 980702);
10098 saved_namespace
= current_namespace
;
10099 current_namespace
= CP_DECL_CONTEXT (decl
);
10101 /* Actually set the DCL_ASSEMBLER_NAME. */
10102 DECL_ASSEMBLER_NAME (decl
)
10103 = build_decl_overload_real (DECL_NAME (decl
), parm_types
, ret_type
,
10105 DECL_FUNCTION_MEMBER_P (decl
)
10106 + DECL_CONSTRUCTOR_P (decl
));
10108 /* Restore the previously active namespace. */
10109 current_namespace
= saved_namespace
;