1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
42 /* The type of functions taking a tree, and some additional data, and
44 typedef int (*tree_fn_t
) PROTO((tree
, void*));
46 extern struct obstack permanent_obstack
;
49 extern char *input_filename
;
50 struct pending_inline
*pending_template_expansions
;
52 tree current_template_parms
;
53 HOST_WIDE_INT processing_template_decl
;
55 tree pending_templates
;
56 static tree
*template_tail
= &pending_templates
;
59 static tree
*maybe_template_tail
= &maybe_templates
;
61 int minimal_parse_mode
;
63 int processing_specialization
;
64 int processing_explicit_instantiation
;
65 static int template_header_count
;
67 static tree saved_trees
;
69 #define obstack_chunk_alloc xmalloc
70 #define obstack_chunk_free free
72 static int unify
PROTO((tree
, tree
, int, tree
, tree
, int, int*));
73 static void add_pending_template
PROTO((tree
));
74 static int push_tinst_level
PROTO((tree
));
75 static tree classtype_mangled_name
PROTO((tree
));
76 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
, tree
));
77 static tree tsubst_expr_values
PROTO((tree
, tree
));
78 static int comp_template_args
PROTO((tree
, tree
));
79 static int list_eq
PROTO((tree
, tree
));
80 static tree get_class_bindings
PROTO((tree
, tree
, tree
, tree
));
81 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int, int));
82 static tree tsubst_enum
PROTO((tree
, tree
, tree
*));
83 static tree add_to_template_args
PROTO((tree
, tree
));
84 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
85 int, int, int, int*));
86 static tree complete_template_args
PROTO((tree
, tree
, int));
87 static void note_template_header
PROTO((int));
88 static tree maybe_fold_nontype_arg
PROTO((tree
));
89 static tree convert_nontype_argument
PROTO((tree
, tree
));
90 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
91 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
92 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
93 static tree original_template
PROTO((tree
));
94 static int inline_needs_template_parms
PROTO((tree
));
95 static void push_inline_template_parms_recursive
PROTO((tree
, int));
96 static tree retrieve_specialization
PROTO((tree
, tree
));
97 static void register_specialization
PROTO((tree
, tree
, tree
));
98 static void print_candidates
PROTO((tree
));
99 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
100 static tree build_template_decl
PROTO((tree
, tree
));
101 static int mark_template_parm
PROTO((tree
, void *));
102 static tree tsubst_friend_function
PROTO((tree
, tree
));
103 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
104 static int template_decl_level
PROTO((tree
));
106 /* Do any processing required when DECL (a member template declaration
107 using TEMPLATE_PARAMETERS as its innermost parameter list) is
108 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
109 it is a specialization, in which case the DECL itself is returned. */
112 finish_member_template_decl (template_parameters
, decl
)
113 tree template_parameters
;
116 if (template_parameters
)
117 end_template_decl ();
119 end_specialization ();
121 if (decl
== NULL_TREE
|| decl
== void_type_node
)
123 else if (TREE_CODE (decl
) == TREE_LIST
)
125 /* Assume that the class is the only declspec. */
126 decl
= TREE_VALUE (decl
);
127 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
128 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
130 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
131 check_member_template (tmpl
);
136 else if (DECL_TEMPLATE_INFO (decl
))
138 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
140 check_member_template (DECL_TI_TEMPLATE (decl
));
141 return DECL_TI_TEMPLATE (decl
);
147 cp_error ("invalid member template declaration `%D'", decl
);
150 return error_mark_node
;
153 /* Returns the template nesting level of the indicated class TYPE.
163 A<T>::B<U> has depth two, while A<T> has depth one. Also,
164 both A<T>::B<int> and A<int>::B<U> have depth one. */
167 template_class_depth (type
)
172 for (depth
= 0; type
&& TREE_CODE (type
) != FUNCTION_DECL
;
173 type
= TYPE_CONTEXT (type
))
174 if (CLASSTYPE_TEMPLATE_INFO (type
)
175 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
176 && uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
182 /* Return the original template for this decl, disregarding any
186 original_template (decl
)
189 while (DECL_TEMPLATE_INFO (decl
))
190 decl
= DECL_TI_TEMPLATE (decl
);
194 /* Returns 1 if processing DECL as part of do_pending_inlines
195 needs us to push template parms. */
198 inline_needs_template_parms (decl
)
201 if (! DECL_TEMPLATE_INFO (decl
))
204 return (list_length (DECL_TEMPLATE_PARMS (original_template (decl
)))
205 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
208 /* Subroutine of maybe_begin_member_template_processing.
209 Push the template parms in PARMS, starting from LEVELS steps into the
210 chain, and ending at the beginning, since template parms are listed
214 push_inline_template_parms_recursive (parmlist
, levels
)
218 tree parms
= TREE_VALUE (parmlist
);
222 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
224 ++processing_template_decl
;
225 current_template_parms
226 = tree_cons (build_int_2 (0, processing_template_decl
),
227 parms
, current_template_parms
);
228 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
231 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
233 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
234 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
236 switch (TREE_CODE (parm
))
245 /* Make a CONST_DECL as is done in process_template_parm. */
246 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
248 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
254 my_friendly_abort (0);
259 /* Restore the template parameter context for a member template or
260 a friend template defined in a class definition. */
263 maybe_begin_member_template_processing (decl
)
269 if (! inline_needs_template_parms (decl
))
272 parms
= DECL_TEMPLATE_PARMS (original_template (decl
));
274 levels
= list_length (parms
) - processing_template_decl
;
276 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
279 parms
= TREE_CHAIN (parms
);
282 push_inline_template_parms_recursive (parms
, levels
);
285 /* Undo the effects of begin_member_template_processing. */
288 maybe_end_member_template_processing (decl
)
291 if (! processing_template_decl
)
294 while (current_template_parms
295 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
297 --processing_template_decl
;
298 current_template_parms
= TREE_CHAIN (current_template_parms
);
303 /* Returns non-zero iff T is a member template function. We must be
306 template <class T> class C { void f(); }
308 Here, f is a template function, and a member, but not a member
309 template. This function does not concern itself with the origin of
310 T, only its present state. So if we have
312 template <class T> class C { template <class U> void f(U); }
314 then neither C<int>::f<char> nor C<T>::f<double> is considered
315 to be a member template. */
318 is_member_template (t
)
321 if (TREE_CODE (t
) != FUNCTION_DECL
322 && !DECL_FUNCTION_TEMPLATE_P (t
))
323 /* Anything that isn't a function or a template function is
324 certainly not a member template. */
327 /* A local class can't have member templates. */
328 if (hack_decl_function_context (t
))
331 if ((DECL_FUNCTION_MEMBER_P (t
)
332 && !DECL_TEMPLATE_SPECIALIZATION (t
))
333 || (TREE_CODE (t
) == TEMPLATE_DECL
334 && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
338 if (DECL_FUNCTION_TEMPLATE_P (t
))
340 else if (DECL_TEMPLATE_INFO (t
)
341 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
342 tmpl
= DECL_TI_TEMPLATE (t
);
347 /* If there are more levels of template parameters than
348 there are template classes surrounding the declaration,
349 then we have a member template. */
350 && (list_length (DECL_TEMPLATE_PARMS (tmpl
)) >
351 template_class_depth (DECL_CLASS_CONTEXT (t
))))
358 /* Return a new template argument vector which contains all of ARGS
359 for all outer templates TYPE is contained in, but has as its
360 innermost set of arguments the EXTRA_ARGS. If UNBOUND_ONLY, we
361 are only interested in unbound template arguments, not arguments from
362 enclosing templates that have been instantiated already. */
365 complete_template_args (tmpl
, extra_args
, unbound_only
)
366 tree tmpl
, extra_args
;
369 /* depth is the number of levels of enclosing args we're adding. */
371 tree args
, new_args
, spec_args
= NULL_TREE
;
373 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
374 my_friendly_assert (TREE_CODE (extra_args
) == TREE_VEC
, 0);
376 if (DECL_TEMPLATE_INFO (tmpl
) && !unbound_only
)
378 /* A specialization of a member template of a template class shows up
379 as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set.
380 DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE
381 is the template being specialized. */
382 if (DECL_TEMPLATE_SPECIALIZATION (tmpl
))
384 spec_args
= DECL_TI_ARGS (tmpl
);
385 tmpl
= DECL_TI_TEMPLATE (tmpl
);
388 if (DECL_TEMPLATE_INFO (tmpl
))
390 /* A partial instantiation of a member template shows up as a
391 TEMPLATE_DECL with DECL_TEMPLATE_INFO. DECL_TI_ARGS is
392 all the bound template arguments. */
393 args
= DECL_TI_ARGS (tmpl
);
394 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
397 depth
= TREE_VEC_LENGTH (args
);
400 /* If we are a specialization, we might have no previously bound
404 new_args
= make_tree_vec (depth
+ 1 + (!!spec_args
));
407 TREE_VEC_ELT (new_args
, 0) = args
;
409 for (i
= 0; i
< depth
; ++i
)
410 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
417 /* For unbound args, we have to do more work. We are getting bindings
418 for the innermost args from extra_args, so we start from our
419 context and work out until we've seen all the args. We need to
420 do it this way to handle partial specialization. */
422 depth
= list_length (DECL_TEMPLATE_PARMS (tmpl
)) - 1;
426 new_args
= make_tree_vec (depth
+ 1);
428 /* If this isn't a member template, extra_args is for the innermost
429 template class, so skip over it. */
430 skip
= (! is_member_template (tmpl
));
432 type
= DECL_REAL_CONTEXT (tmpl
);
433 for (i
= depth
; i
; type
= TYPE_CONTEXT (type
))
434 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
441 TREE_VEC_ELT (new_args
, i
) = CLASSTYPE_TI_ARGS (type
);
446 TREE_VEC_ELT (new_args
, depth
) = extra_args
;
449 TREE_VEC_ELT (new_args
, depth
+ 1) = spec_args
;
454 /* Return a new template argument vector which contains all of ARGS,
455 but has as its innermost set of arguments the EXTRA_ARGS. */
458 add_to_template_args (args
, extra_args
)
464 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
466 new_args
= make_tree_vec (2);
467 TREE_VEC_ELT (new_args
, 0) = args
;
473 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) + 1);
475 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
476 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
479 TREE_VEC_ELT (new_args
,
480 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
485 /* We've got a template header coming up; push to a new level for storing
489 begin_template_parm_list ()
492 declare_pseudo_global_level ();
493 ++processing_template_decl
;
494 note_template_header (0);
498 /* We've just seen template <>. */
501 begin_specialization ()
503 note_template_header (1);
507 /* Called at then end of processing a declaration preceeded by
511 end_specialization ()
513 reset_specialization ();
517 /* Any template <>'s that we have seen thus far are not referring to a
518 function specialization. */
521 reset_specialization ()
523 processing_specialization
= 0;
524 template_header_count
= 0;
528 /* We've just seen a template header. If SPECIALIZATION is non-zero,
529 it was of the form template <>. */
532 note_template_header (specialization
)
535 processing_specialization
= specialization
;
536 template_header_count
++;
540 /* We're beginning an explicit instantiation. */
543 begin_explicit_instantiation ()
545 ++processing_explicit_instantiation
;
550 end_explicit_instantiation ()
552 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
553 --processing_explicit_instantiation
;
557 /* Retrieve the specialization (in the sense of [temp.spec] - a
558 specialization is either an instantiation or an explicit
559 specialization) of TMPL for the given template ARGS. If there is
560 no such specialization, return NULL_TREE. The ARGS are a vector of
561 arguments, or a vector of vectors of arguments, in the case of
562 templates with more than one level of parameters. */
565 retrieve_specialization (tmpl
, args
)
571 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
573 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
576 if (comp_template_args (TREE_PURPOSE (s
), args
))
577 return TREE_VALUE (s
);
584 /* Register the specialization SPEC as a specialization of TMPL with
585 the indicated ARGS. */
588 register_specialization (spec
, tmpl
, args
)
595 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
597 if (TREE_CODE (spec
) != TEMPLATE_DECL
598 && list_length (DECL_TEMPLATE_PARMS (tmpl
)) > 1)
599 /* Avoid registering function declarations as
600 specializations of member templates, as would otherwise
601 happen with out-of-class specializations of member
605 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
608 if (comp_template_args (TREE_PURPOSE (s
), args
))
610 tree fn
= TREE_VALUE (s
);
612 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
614 if (DECL_TEMPLATE_INSTANTIATION (fn
))
617 || DECL_EXPLICIT_INSTANTIATION (fn
))
619 cp_error ("specialization of %D after instantiation",
625 /* This situation should occur only if the first
626 specialization is an implicit instantiation,
627 the second is an explicit specialization, and
628 the implicit instantiation has not yet been
629 used. That situation can occur if we have
630 implicitly instantiated a member function of
631 class type, and then specialized it later. */
632 TREE_VALUE (s
) = spec
;
636 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
638 if (DECL_INITIAL (fn
))
639 cp_error ("duplicate specialization of %D", fn
);
641 TREE_VALUE (s
) = spec
;
647 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
648 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
652 /* Print the list of candidate FNS in an error message. */
655 print_candidates (fns
)
660 char* str
= "candidates are:";
662 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
664 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
669 /* Returns the template (one of the functions given by TEMPLATE_ID)
670 which can be specialized to match the indicated DECL with the
671 explicit template args given in TEMPLATE_ID. If
672 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
673 member template. The template args (those explicitly specified and
674 those deduced) are output in a newly created vector *TARGS_OUT. If
675 it is impossible to determine the result, an error message is
676 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
680 determine_specialization (template_id
, decl
, targs_out
,
681 need_member_template
,
686 int need_member_template
;
689 tree fns
= TREE_OPERAND (template_id
, 0);
690 tree targs_in
= TREE_OPERAND (template_id
, 1);
691 tree templates
= NULL_TREE
;
696 *targs_out
= NULL_TREE
;
698 if (is_overloaded_fn (fns
))
699 fn
= get_first_fn (fns
);
703 overloaded
= really_overloaded_fn (fns
);
704 for (; fn
!= NULL_TREE
;
705 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
709 if (!need_member_template
710 && TREE_CODE (fn
) == FUNCTION_DECL
711 && DECL_FUNCTION_MEMBER_P (fn
)
712 && DECL_USE_TEMPLATE (fn
)
713 && DECL_TI_TEMPLATE (fn
))
714 /* We can get here when processing something like:
715 template <class T> class X { void f(); }
716 template <> void X<int>::f() {}
717 We're specializing a member function, but not a member
719 tmpl
= DECL_TI_TEMPLATE (fn
);
720 else if (TREE_CODE (fn
) != TEMPLATE_DECL
721 || (need_member_template
&& !is_member_template (fn
)))
726 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
729 if (decl
== NULL_TREE
)
731 tree targs
= make_scratch_vec (DECL_NTPARMS (tmpl
));
733 /* We allow incomplete unification here, because we are going to
734 check all the functions. */
735 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
743 /* Unification was successful. */
744 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
747 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
750 if (decl
!= NULL_TREE
)
752 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
754 if (tmpl
== error_mark_node
)
756 else if (tmpl
== NULL_TREE
)
759 *targs_out
= get_bindings (tmpl
, decl
, targs_in
);
763 if (templates
== NULL_TREE
)
767 cp_error ("`%D' does not match any template declaration",
772 else if (TREE_CHAIN (templates
) != NULL_TREE
)
777 cp_error ("ambiguous template specialization `%D'",
779 print_candidates (templates
);
784 /* We have one, and exactly one, match. */
785 *targs_out
= TREE_PURPOSE (templates
);
786 return TREE_VALUE (templates
);
790 /* Check to see if the function just declared, as indicated in
791 DECLARATOR, and in DECL, is a specialization of a function
792 template. We may also discover that the declaration is an explicit
793 instantiation at this point.
795 Returns DECL, or an equivalent declaration that should be used
798 FLAGS is a bitmask consisting of the following flags:
800 1: We are being called by finish_struct. (We are unable to
801 determine what template is specialized by an in-class
802 declaration until the class definition is complete, so
803 finish_struct_methods calls this function again later to finish
805 2: The function has a definition.
806 4: The function is a friend.
807 8: The function is known to be a specialization of a member
810 The TEMPLATE_COUNT is the number of references to qualifying
811 template classes that appeared in the name of the function. For
814 template <class T> struct S { void f(); };
817 the TEMPLATE_COUNT would be 1. However, explicitly specialized
818 classes are not counted in the TEMPLATE_COUNT, so that in
820 template <class T> struct S {};
821 template <> struct S<int> { void f(); }
825 the TEMPLATE_COUNT would be 0. (Note that this declaration is
826 illegal; there should be no template <>.)
828 If the function is a specialization, it is marked as such via
829 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
830 is set up correctly, and it is added to the list of specializations
831 for that template. */
834 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
840 int finish_member
= flags
& 1;
841 int have_def
= flags
& 2;
842 int is_friend
= flags
& 4;
843 int specialization
= 0;
844 int explicit_instantiation
= 0;
845 int member_specialization
= flags
& 8;
847 tree ctype
= DECL_CLASS_CONTEXT (decl
);
848 tree dname
= DECL_NAME (decl
);
852 if (processing_specialization
)
854 /* The last template header was of the form template <>. */
856 if (template_header_count
> template_count
)
858 /* There were more template headers than qualifying template
860 if (template_header_count
- template_count
> 1)
861 /* There shouldn't be that many template parameter
862 lists. There can be at most one parameter list for
863 every qualifying class, plus one for the function
865 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
867 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
869 member_specialization
= 1;
873 else if (template_header_count
== template_count
)
875 /* The counts are equal. So, this might be a
876 specialization, but it is not a specialization of a
877 member template. It might be something like
879 template <class T> struct S {
883 void S<int>::f(int i) {} */
885 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
889 /* This cannot be an explicit specialization. There are not
890 enough headers for all of the qualifying classes. For
891 example, we might have:
894 void S<int>::T<char>::f();
896 But, we're missing another template <>. */
897 cp_error("too few template parameter lists in declaration of `%D'", decl
);
901 else if (processing_explicit_instantiation
)
903 if (template_header_count
)
904 cp_error ("template parameter list used in explicit instantiation");
907 cp_error ("definition provided for explicit instantiation");
909 explicit_instantiation
= 1;
911 else if (ctype
!= NULL_TREE
912 && !TYPE_BEING_DEFINED (ctype
)
913 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
915 /* This case catches outdated code that looks like this:
917 template <class T> struct S { void f(); };
918 void S<int>::f() {} // Missing template <>
920 We disable this check when the type is being defined to
921 avoid complaining about default compiler-generated
922 constructors, destructors, and assignment operators.
923 Since the type is an instantiation, not a specialization,
924 these are the only functions that can be defined before
925 the class is complete. */
928 template <class T> void S<int>::f() {}
930 if (template_header_count
)
932 cp_error ("template parameters specified in specialization");
938 ("explicit specialization not preceded by `template <>'");
940 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
942 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
944 /* This case handles bogus declarations like
945 template <> template <class T>
948 cp_error ("template-id `%D' in declaration of primary template",
954 if (specialization
|| member_specialization
)
956 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
957 for (; t
; t
= TREE_CHAIN (t
))
958 if (TREE_PURPOSE (t
))
961 ("default argument specified in explicit specialization");
966 if (specialization
|| member_specialization
|| explicit_instantiation
)
968 tree tmpl
= NULL_TREE
;
969 tree targs
= NULL_TREE
;
971 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
972 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
976 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
979 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
984 lookup_template_function (fns
, NULL_TREE
);
987 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
989 /* A friend declaration. We can't do much, because we don't
990 know what this resolves to, yet. */
991 my_friendly_assert (is_friend
!= 0, 0);
992 my_friendly_assert (!explicit_instantiation
, 0);
993 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
997 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
999 /* Since finish_struct_1 has not been called yet, we
1000 can't call lookup_fnfields. We note that this
1001 template is a specialization, and proceed, letting
1002 finish_struct fix this up later. */
1003 tree ti
= perm_tree_cons (NULL_TREE
,
1004 TREE_OPERAND (declarator
, 1),
1006 TI_PENDING_SPECIALIZATION_FLAG (ti
) = 1;
1007 DECL_TEMPLATE_INFO (decl
) = ti
;
1008 /* This should not be an instantiation; explicit
1009 instantiation directives can only occur at the top
1011 my_friendly_assert (!explicit_instantiation
, 0);
1014 else if (ctype
!= NULL_TREE
1015 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1018 /* Find the list of functions in ctype that have the same
1019 name as the declared function. */
1020 tree name
= TREE_OPERAND (declarator
, 0);
1023 if (name
== constructor_name (ctype
)
1024 || name
== constructor_name_full (ctype
))
1026 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1028 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1029 : !TYPE_HAS_DESTRUCTOR (ctype
))
1031 /* From [temp.expl.spec]:
1033 If such an explicit specialization for the member
1034 of a class template names an implicitly-declared
1035 special member function (clause _special_), the
1036 program is ill-formed.
1038 Similar language is found in [temp.explicit]. */
1039 cp_error ("specialization of implicitly-declared special member function");
1044 fns
= TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype
),
1045 is_constructor
? 0 : 1);
1048 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
,
1051 if (fns
== NULL_TREE
)
1053 cp_error ("no member function `%s' declared in `%T'",
1054 IDENTIFIER_POINTER (name
),
1059 TREE_OPERAND (declarator
, 0) = fns
;
1062 /* Figure out what exactly is being specialized at this point.
1063 Note that for an explicit instantiation, even one for a
1064 member function, we cannot tell apriori whether the the
1065 instantiation is for a member template, or just a member
1066 function of a template class. In particular, even in if the
1067 instantiation is for a member template, the template
1068 arguments could be deduced from the declaration. */
1069 tmpl
= determine_specialization (declarator
, decl
,
1071 member_specialization
,
1076 if (explicit_instantiation
)
1078 decl
= instantiate_template (tmpl
, targs
);
1079 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
1080 /* There doesn't seem to be anything in the draft to
1081 prevent a specialization from being explicitly
1082 instantiated. We're careful not to destroy the
1083 information indicating that this is a
1084 specialization here. */
1085 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
1088 else if (DECL_STATIC_FUNCTION_P (tmpl
)
1089 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1091 revert_static_member_fn (&decl
, 0, 0);
1092 last_function_parms
= TREE_CHAIN (last_function_parms
);
1095 /* Mangle the function name appropriately. Note that we do
1096 not mangle specializations of non-template member
1097 functions of template classes, e.g. with
1098 template <class T> struct S { void f(); }
1099 and given the specialization
1100 template <> void S<int>::f() {}
1101 we do not mangle S<int>::f() here. That's because it's
1102 just an ordinary member function and doesn't need special
1104 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1105 && name_mangling_version
>= 1)
1107 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
1110 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
1112 hash_tree_chain (build_pointer_type (ctype
),
1115 DECL_ASSEMBLER_NAME (decl
)
1116 = build_template_decl_overload
1119 TREE_TYPE (TREE_TYPE (tmpl
)),
1120 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
1121 targs
, ctype
!= NULL_TREE
);
1124 if (is_friend
&& !have_def
)
1126 /* This is not really a declaration of a specialization.
1127 It's just the name of an instantiation. But, it's not
1128 a request for an instantiation, either. */
1129 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1130 DECL_TEMPLATE_INFO (decl
)
1131 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1135 /* If DECL_TI_TEMPLATE (decl), the decl is an
1136 instantiation of a specialization of a member template.
1137 (In other words, there was a member template, in a
1138 class template. That member template was specialized.
1139 We then instantiated the class, so there is now an
1140 instance of that specialization.)
1142 According to the CD2,
1144 14.7.3.13 [tmpl.expl.spec]
1146 A specialization of a member function template or
1147 member class template of a non-specialized class
1148 template is itself a template.
1150 So, we just leave the template info alone in this case. */
1151 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
1152 DECL_TEMPLATE_INFO (decl
)
1153 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1155 register_specialization (decl
, tmpl
, targs
);
1165 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1166 parameters. These are represented in the same format used for
1167 DECL_TEMPLATE_PARMS. */
1169 int comp_template_parms (parms1
, parms2
)
1176 if (parms1
== parms2
)
1179 for (p1
= parms1
, p2
= parms2
;
1180 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1181 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1183 tree t1
= TREE_VALUE (p1
);
1184 tree t2
= TREE_VALUE (p2
);
1187 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1188 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1190 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1193 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1195 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1196 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1198 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1201 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1203 else if (!comptypes (TREE_TYPE (parm1
),
1204 TREE_TYPE (parm2
), 1))
1209 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1210 /* One set of parameters has more parameters lists than the
1218 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1219 ORIG_LEVEL, DECL, and TYPE. */
1222 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1229 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1230 TEMPLATE_PARM_IDX (t
) = index
;
1231 TEMPLATE_PARM_LEVEL (t
) = level
;
1232 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1233 TEMPLATE_PARM_DECL (t
) = decl
;
1234 TREE_TYPE (t
) = type
;
1240 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1241 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1242 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1243 new one is created. */
1246 reduce_template_parm_level (index
, type
, levels
)
1251 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1252 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1253 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1256 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1257 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1260 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1261 TEMPLATE_PARM_LEVEL (index
) - levels
,
1262 TEMPLATE_PARM_ORIG_LEVEL (index
),
1264 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1266 /* Template template parameters need this. */
1267 DECL_TEMPLATE_PARMS (decl
)
1268 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1271 return TEMPLATE_PARM_DESCENDANTS (index
);
1274 /* Process information from new template parameter NEXT and append it to the
1275 LIST being built. */
1278 process_template_parm (list
, next
)
1287 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1288 defval
= TREE_PURPOSE (parm
);
1289 parm
= TREE_VALUE (parm
);
1290 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1294 tree p
= TREE_VALUE (tree_last (list
));
1296 if (TREE_CODE (p
) == TYPE_DECL
)
1297 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1298 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1299 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1301 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1309 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1310 /* is a const-param */
1311 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1312 PARM
, 0, NULL_TREE
);
1313 /* A template parameter is not modifiable. */
1314 TREE_READONLY (parm
) = 1;
1315 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1316 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1318 cp_error ("`%#T' is not a valid type for a template constant parameter",
1320 if (DECL_NAME (parm
) == NULL_TREE
)
1321 error (" a template type parameter must begin with `class' or `typename'");
1322 TREE_TYPE (parm
) = void_type_node
;
1325 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1326 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1327 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1329 if (TREE_PERMANENT (parm
) == 0)
1331 parm
= copy_node (parm
);
1332 TREE_PERMANENT (parm
) = 1;
1334 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1335 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1336 = build_template_parm_index (idx
, processing_template_decl
,
1337 processing_template_decl
,
1338 decl
, TREE_TYPE (parm
));
1343 parm
= TREE_VALUE (parm
);
1345 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1347 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1348 /* This is for distinguishing between real templates and template
1349 template parameters */
1350 TREE_TYPE (parm
) = t
;
1351 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1356 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1357 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1358 decl
= build_decl (TYPE_DECL
, parm
, t
);
1361 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1362 TYPE_NAME (t
) = decl
;
1363 TYPE_STUB_DECL (t
) = decl
;
1365 TEMPLATE_TYPE_PARM_INDEX (t
)
1366 = build_template_parm_index (idx
, processing_template_decl
,
1367 processing_template_decl
,
1368 decl
, TREE_TYPE (parm
));
1370 SET_DECL_ARTIFICIAL (decl
);
1372 parm
= build_tree_list (defval
, parm
);
1373 return chainon (list
, parm
);
1376 /* The end of a template parameter list has been reached. Process the
1377 tree list into a parameter vector, converting each parameter into a more
1378 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1382 end_template_parm_list (parms
)
1387 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1389 current_template_parms
1390 = tree_cons (build_int_2 (0, processing_template_decl
),
1391 saved_parmlist
, current_template_parms
);
1393 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1394 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1396 return saved_parmlist
;
1399 /* end_template_decl is called after a template declaration is seen. */
1402 end_template_decl ()
1404 reset_specialization ();
1406 if (! processing_template_decl
)
1409 /* This matches the pushlevel in begin_template_parm_list. */
1412 --processing_template_decl
;
1413 current_template_parms
= TREE_CHAIN (current_template_parms
);
1414 (void) get_pending_sizes (); /* Why? */
1417 /* Generate a valid set of template args from current_template_parms. */
1420 current_template_args ()
1422 tree header
= current_template_parms
;
1423 int length
= list_length (header
);
1424 tree args
= make_tree_vec (length
);
1429 tree a
= copy_node (TREE_VALUE (header
));
1430 int i
= TREE_VEC_LENGTH (a
);
1431 TREE_TYPE (a
) = NULL_TREE
;
1434 tree t
= TREE_VEC_ELT (a
, i
);
1436 /* t will be a list if we are called from within a
1437 begin/end_template_parm_list pair, but a vector directly
1438 if within a begin/end_member_template_processing pair. */
1439 if (TREE_CODE (t
) == TREE_LIST
)
1443 if (TREE_CODE (t
) == TYPE_DECL
1444 || TREE_CODE (t
) == TEMPLATE_DECL
)
1447 t
= DECL_INITIAL (t
);
1450 TREE_VEC_ELT (a
, i
) = t
;
1452 TREE_VEC_ELT (args
, --l
) = a
;
1453 header
= TREE_CHAIN (header
);
1460 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1461 template PARMS. Used by push_template_decl below. */
1464 build_template_decl (decl
, parms
)
1468 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1469 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1470 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1471 if (DECL_LANG_SPECIFIC (decl
))
1473 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1474 DECL_STATIC_FUNCTION_P (tmpl
) =
1475 DECL_STATIC_FUNCTION_P (decl
);
1481 struct template_parm_data
1487 /* Subroutine of push_template_decl used to see if each template
1488 parameter in a partial specialization is used in the explicit
1489 argument list. If T is of the LEVEL given in DATA (which is
1490 treated as a template_parm_data*), then DATA->PARMS is marked
1494 mark_template_parm (t
, data
)
1500 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1502 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1504 level
= TEMPLATE_PARM_LEVEL (t
);
1505 idx
= TEMPLATE_PARM_IDX (t
);
1509 level
= TEMPLATE_TYPE_LEVEL (t
);
1510 idx
= TEMPLATE_TYPE_IDX (t
);
1513 if (level
== tpd
->level
)
1514 tpd
->parms
[idx
] = 1;
1516 /* Return zero so that for_each_template_parm will continue the
1517 traversal of the tree; we want to mark *every* template parm. */
1521 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1522 parameters given by current_template_args, or reuses a
1523 previously existing one, if appropriate. Returns the DECL, or an
1524 equivalent one, if it is replaced via a call to duplicate_decls. */
1527 push_template_decl (decl
)
1535 int is_friend
= (TREE_CODE (decl
) == FUNCTION_DECL
1536 && DECL_FRIEND_P (decl
));
1539 /* For a friend, we want the context of the friend function, not
1540 the type of which it is a friend. */
1541 ctx
= DECL_CONTEXT (decl
);
1542 else if (DECL_REAL_CONTEXT (decl
))
1543 /* In the case of a virtual function, we want the class in which
1545 ctx
= DECL_REAL_CONTEXT (decl
);
1547 /* Otherwise, if we're currently definining some class, the DECL
1548 is assumed to be a member of the class. */
1549 ctx
= current_class_type
;
1551 /* For determining whether this is a primary template or not, we're really
1552 interested in the lexical context, not the true context. */
1554 info
= DECL_CLASS_CONTEXT (decl
);
1558 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
1561 || (TYPE_BEING_DEFINED (info
) && template_header_count
1562 && ! processing_specialization
)
1563 || (template_header_count
> template_class_depth (info
)))
1570 if (current_lang_name
== lang_name_c
)
1571 cp_error ("template with C linkage");
1572 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
1573 cp_error ("template class without a name");
1576 /* Partial specialization. */
1577 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1578 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1580 tree type
= TREE_TYPE (decl
);
1581 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1582 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1583 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1585 /* We check that each of the template parameters given in the
1586 partial specialization is used in the argument list to the
1587 specialization. For example:
1589 template <class T> struct S;
1590 template <class T> struct S<T*>;
1592 The second declaration is OK because `T*' uses the template
1593 parameter T, whereas
1595 template <class T> struct S<int>;
1597 is no good. Even trickier is:
1608 The S2<T> declaration is actually illegal; it is a
1609 full-specialization. Of course,
1612 struct S2<T (*)(U)>;
1614 or some such would have been OK. */
1616 struct template_parm_data tpd
;
1617 int ntparms
= TREE_VEC_LENGTH (TREE_VALUE (current_template_parms
));
1618 int did_error_intro
= 0;
1620 tpd
.level
= TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms
));
1621 tpd
.parms
= alloca (sizeof (int) * ntparms
);
1622 for (i
= 0; i
< ntparms
; ++i
)
1624 for (i
= 0; i
< TREE_VEC_LENGTH (mainargs
); ++i
)
1625 for_each_template_parm (TREE_VEC_ELT (mainargs
, i
),
1626 &mark_template_parm
,
1628 for (i
= 0; i
< ntparms
; ++i
)
1629 if (tpd
.parms
[i
] == 0)
1631 /* One of the template parms was not used in the
1633 if (!did_error_intro
)
1635 cp_error ("template parameters not used in partial specialization:");
1636 did_error_intro
= 1;
1640 TREE_VALUE (TREE_VEC_ELT
1641 (TREE_VALUE (current_template_parms
),
1645 for (; spec
; spec
= TREE_CHAIN (spec
))
1647 /* purpose: args to main template
1648 value: spec template */
1649 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1653 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1654 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1655 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1656 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1660 args
= current_template_args ();
1663 || TREE_CODE (ctx
) == FUNCTION_DECL
1664 || TYPE_BEING_DEFINED (ctx
)
1665 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
1667 if (DECL_LANG_SPECIFIC (decl
)
1668 && DECL_TEMPLATE_INFO (decl
)
1669 && DECL_TI_TEMPLATE (decl
))
1670 tmpl
= DECL_TI_TEMPLATE (decl
);
1673 tmpl
= build_template_decl (decl
, current_template_parms
);
1675 if (DECL_LANG_SPECIFIC (decl
)
1676 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1678 /* A specialization of a member template of a template
1680 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1681 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1682 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1691 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1692 cp_error ("must specialize `%#T' before defining member `%#D'",
1694 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1695 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1696 else if (! DECL_TEMPLATE_INFO (decl
))
1698 cp_error ("template definition of non-template `%#D'", decl
);
1702 tmpl
= DECL_TI_TEMPLATE (decl
);
1704 if (is_member_template (tmpl
))
1706 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1707 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1711 /* The declaration is a specialization of a member
1712 template, declared outside the class. Therefore, the
1713 innermost template arguments will be NULL, so we
1714 replace them with the arguments determined by the
1715 earlier call to check_explicit_specialization. */
1716 args
= DECL_TI_ARGS (decl
);
1719 = build_template_decl (decl
, current_template_parms
);
1720 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1721 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1722 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1723 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1724 DECL_TEMPLATE_INFO (new_tmpl
) =
1725 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1727 register_specialization (new_tmpl
, tmpl
, args
);
1731 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1732 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1733 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1735 cp_error ("got %d template parameters for `%#D'",
1736 TREE_VEC_LENGTH (a
), decl
);
1737 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1739 if (TREE_VEC_LENGTH (args
) > 1)
1740 /* Get the template parameters for the enclosing template
1742 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1747 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1751 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1753 /* When processing an inline member template of a
1754 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1755 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1756 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1758 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1759 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1761 /* There should be template arguments if and only if there is a
1763 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1766 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1768 cp_error ("got %d template parameters for `%#D'",
1769 TREE_VEC_LENGTH (a
), decl
);
1770 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1773 /* Get the innermost set of template arguments. */
1774 args
= innermost_args (args
, 0);
1776 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1777 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1779 if (! ctx
&& primary
)
1780 /* The check of PRIMARY ensures that we do not try to push a
1781 global template friend declared in a template class; such a
1782 thing may well depend on the template parameters of the class. */
1783 tmpl
= pushdecl_top_level (tmpl
);
1786 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1788 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1790 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1792 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1793 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1794 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1796 else if (! DECL_LANG_SPECIFIC (decl
))
1797 cp_error ("template declaration of `%#D'", decl
);
1799 DECL_TEMPLATE_INFO (decl
) = info
;
1801 return DECL_TEMPLATE_RESULT (tmpl
);
1804 /* Called when a class template TYPE is redeclared, e.g.:
1806 template <class T> struct S;
1807 template <class T> struct S {}; */
1810 redeclare_class_template (type
)
1813 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1814 tree tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
1815 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1818 if (!PRIMARY_TEMPLATE_P (tmpl
))
1819 /* The type is nested in some template class. Nothing to worry
1820 about here; there are no new template parameters for the nested
1824 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
1826 cp_error_at ("previous declaration `%D'", tmpl
);
1827 cp_error ("used %d template parameter%s instead of %d",
1828 TREE_VEC_LENGTH (tmpl_parms
),
1829 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
1830 TREE_VEC_LENGTH (parms
));
1834 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
1836 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
1837 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1838 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
1839 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
1841 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
1843 cp_error_at ("template parameter `%#D'", tmpl_parm
);
1844 cp_error ("redeclared here as `%#D'", parm
);
1848 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
1850 /* We have in [temp.param]:
1852 A template-parameter may not be given default arguments
1853 by two different declarations in the same scope. */
1854 cp_error ("redefinition of default argument for `%#D'", parm
);
1858 if (parm_default
!= NULL_TREE
)
1859 /* Update the previous template parameters (which are the ones
1860 that will really count) with the new default value. */
1861 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
1865 /* Attempt to convert the non-type template parameter EXPR to the
1866 indicated TYPE. If the conversion is successful, return the
1867 converted value. If the conversion is unsuccesful, return
1868 NULL_TREE if we issued an error message, or error_mark_node if we
1869 did not. We issue error messages for out-and-out bad template
1870 parameters, but not simply because the conversion failed, since we
1871 might be just trying to do argument deduction. By the time this
1872 function is called, neither TYPE nor EXPR may make use of template
1876 convert_nontype_argument (type
, expr
)
1880 tree expr_type
= TREE_TYPE (expr
);
1882 /* A template-argument for a non-type, non-template
1883 template-parameter shall be one of:
1885 --an integral constant-expression of integral or enumeration
1888 --the name of a non-type template-parameter; or
1890 --the name of an object or function with external linkage,
1891 including function templates and function template-ids but
1892 excluding non-static class members, expressed as id-expression;
1895 --the address of an object or function with external linkage,
1896 including function templates and function template-ids but
1897 excluding non-static class members, expressed as & id-expression
1898 where the & is optional if the name refers to a function or
1901 --a pointer to member expressed as described in _expr.unary.op_. */
1903 /* An integral constant-expression can include const variables
1905 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1906 expr
= decl_constant_value (expr
);
1908 if (is_overloaded_fn (expr
))
1909 /* OK for now. We'll check that it has external linkage later.
1910 Check this first since if expr_type is the unknown_type_node
1911 we would otherwise complain below. */
1913 else if (INTEGRAL_TYPE_P (expr_type
)
1914 || TYPE_PTRMEM_P (expr_type
)
1915 || TYPE_PTRMEMFUNC_P (expr_type
)
1916 /* The next two are g++ extensions. */
1917 || TREE_CODE (expr_type
) == REAL_TYPE
1918 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1920 if (! TREE_CONSTANT (expr
))
1922 cp_error ("non-constant `%E' cannot be used as template argument",
1927 else if (TYPE_PTR_P (expr_type
)
1928 /* If expr is the address of an overloaded function, we
1929 will get the unknown_type_node at this point. */
1930 || expr_type
== unknown_type_node
)
1936 if (TREE_CODE (e
) != ADDR_EXPR
)
1939 cp_error ("`%E' is not a valid template argument", expr
);
1940 error ("it must be %s%s with external linkage",
1941 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1942 ? "a pointer to " : "",
1943 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1944 ? "a function" : "an object");
1948 referent
= TREE_OPERAND (e
, 0);
1949 STRIP_NOPS (referent
);
1951 if (TREE_CODE (referent
) == STRING_CST
)
1953 cp_error ("string literal %E is not a valid template argument",
1955 error ("because it is the address of an object with static linkage");
1959 if (is_overloaded_fn (referent
))
1960 /* We'll check that it has external linkage later. */
1962 else if (TREE_CODE (referent
) != VAR_DECL
)
1964 else if (!TREE_PUBLIC (referent
))
1966 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1967 return error_mark_node
;
1970 else if (TREE_CODE (expr
) == VAR_DECL
)
1972 if (!TREE_PUBLIC (expr
))
1977 cp_error ("object `%E' cannot be used as template argument", expr
);
1981 switch (TREE_CODE (type
))
1986 /* For a non-type template-parameter of integral or enumeration
1987 type, integral promotions (_conv.prom_) and integral
1988 conversions (_conv.integral_) are applied. */
1989 if (!INTEGRAL_TYPE_P (expr_type
))
1990 return error_mark_node
;
1992 /* It's safe to call digest_init in this case; we know we're
1993 just converting one integral constant expression to another. */
1994 return digest_init (type
, expr
, (tree
*) 0);
1998 /* These are g++ extensions. */
1999 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
2000 return error_mark_node
;
2002 return digest_init (type
, expr
, (tree
*) 0);
2006 tree type_pointed_to
= TREE_TYPE (type
);
2008 if (TYPE_PTRMEM_P (type
))
2009 /* For a non-type template-parameter of type pointer to data
2010 member, qualification conversions (_conv.qual_) are
2012 return perform_qualification_conversions (type
, expr
);
2013 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2015 /* For a non-type template-parameter of type pointer to
2016 function, only the function-to-pointer conversion
2017 (_conv.func_) is applied. If the template-argument
2018 represents a set of overloaded functions (or a pointer to
2019 such), the matching function is selected from the set
2024 if (TREE_CODE (expr
) == ADDR_EXPR
)
2025 fns
= TREE_OPERAND (expr
, 0);
2029 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2031 if (fn
== error_mark_node
)
2032 return error_mark_node
;
2034 if (!TREE_PUBLIC (fn
))
2036 if (really_overloaded_fn (fns
))
2037 return error_mark_node
;
2042 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2044 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2050 /* For a non-type template-parameter of type pointer to
2051 object, qualification conversions (_conv.qual_) and the
2052 array-to-pointer conversion (_conv.array_) are applied.
2053 [Note: In particular, neither the null pointer conversion
2054 (_conv.ptr_) nor the derived-to-base conversion
2055 (_conv.ptr_) are applied. Although 0 is a valid
2056 template-argument for a non-type template-parameter of
2057 integral type, it is not a valid template-argument for a
2058 non-type template-parameter of pointer type.]
2060 The call to decay_conversion performs the
2061 array-to-pointer conversion, if appropriate. */
2062 expr
= decay_conversion (expr
);
2064 if (expr
== error_mark_node
)
2065 return error_mark_node
;
2067 return perform_qualification_conversions (type
, expr
);
2072 case REFERENCE_TYPE
:
2074 tree type_referred_to
= TREE_TYPE (type
);
2076 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2078 /* For a non-type template-parameter of type reference to
2079 function, no conversions apply. If the
2080 template-argument represents a set of overloaded
2081 functions, the matching function is selected from the
2082 set (_over.over_). */
2086 fn
= instantiate_type (type_referred_to
, fns
, 0);
2088 if (!TREE_PUBLIC (fn
))
2090 if (really_overloaded_fn (fns
))
2091 /* Don't issue an error here; we might get a different
2092 function if the overloading had worked out
2094 return error_mark_node
;
2099 if (fn
== error_mark_node
)
2100 return error_mark_node
;
2102 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
2109 /* For a non-type template-parameter of type reference to
2110 object, no conversions apply. The type referred to by the
2111 reference may be more cv-qualified than the (otherwise
2112 identical) type of the template-argument. The
2113 template-parameter is bound directly to the
2114 template-argument, which must be an lvalue. */
2115 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
2116 TYPE_MAIN_VARIANT (type
), 1)
2117 || (TYPE_READONLY (expr_type
) >
2118 TYPE_READONLY (type_referred_to
))
2119 || (TYPE_VOLATILE (expr_type
) >
2120 TYPE_VOLATILE (type_referred_to
))
2121 || !real_lvalue_p (expr
))
2122 return error_mark_node
;
2134 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
2136 /* For a non-type template-parameter of type pointer to member
2137 function, no conversions apply. If the template-argument
2138 represents a set of overloaded member functions, the
2139 matching member function is selected from the set
2142 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2143 expr_type
!= unknown_type_node
)
2144 return error_mark_node
;
2146 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2148 /* A ptr-to-member constant. */
2149 if (!comptypes (type
, expr_type
, 1))
2150 return error_mark_node
;
2155 if (TREE_CODE (expr
) != ADDR_EXPR
)
2156 return error_mark_node
;
2158 fns
= TREE_OPERAND (expr
, 0);
2160 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
2163 if (fn
== error_mark_node
)
2164 return error_mark_node
;
2166 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2168 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2175 /* All non-type parameters must have one of these types. */
2176 my_friendly_abort (0);
2180 return error_mark_node
;
2183 /* Convert all template arguments to their appropriate types, and return
2184 a vector containing the resulting values. If any error occurs, return
2185 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
2186 Some error messages are issued even if COMPLAIN is zero; for
2187 instance, if a template argument is composed from a local class.
2189 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2190 provided in ARGLIST, or else trailing parameters must have default
2191 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2192 deduction for any unspecified trailing arguments.
2194 If IS_TMPL_PARM is non-zero, we will coercing parameters of template
2195 template arguments. In this case, ARGLIST is a chain of TREE_LIST
2196 nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */
2199 coerce_template_parms (parms
, arglist
, in_decl
,
2201 require_all_arguments
,
2203 tree parms
, arglist
;
2206 int require_all_arguments
;
2209 int nparms
, nargs
, i
, lost
= 0;
2210 tree vec
= NULL_TREE
;
2212 if (arglist
== NULL_TREE
)
2214 else if (TREE_CODE (arglist
) == TREE_VEC
)
2215 nargs
= TREE_VEC_LENGTH (arglist
);
2217 nargs
= list_length (arglist
);
2219 nparms
= TREE_VEC_LENGTH (parms
);
2223 && require_all_arguments
2224 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
2228 error ("incorrect number of parameters (%d, should be %d)",
2232 cp_error_at ("in template expansion for decl `%D'",
2236 return error_mark_node
;
2239 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
&& nargs
== nparms
)
2240 vec
= copy_node (arglist
);
2243 vec
= make_tree_vec (nparms
);
2245 for (i
= 0; i
< nparms
; i
++)
2248 tree parm
= TREE_VEC_ELT (parms
, i
);
2253 arglist
= TREE_CHAIN (arglist
);
2255 if (arg
== error_mark_node
)
2258 arg
= TREE_VALUE (arg
);
2260 else if (is_tmpl_parm
&& i
< nargs
)
2262 arg
= TREE_VEC_ELT (arglist
, i
);
2263 if (arg
== error_mark_node
)
2266 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
2268 my_friendly_assert (!require_all_arguments
, 0);
2271 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
2272 arg
= tsubst (TREE_PURPOSE (parm
), vec
, in_decl
);
2274 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, in_decl
);
2276 TREE_VEC_ELT (vec
, i
) = arg
;
2279 for (i
= 0; i
< nparms
; i
++)
2281 tree arg
= TREE_VEC_ELT (vec
, i
);
2282 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2284 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2286 if (is_tmpl_parm
&& i
< nargs
)
2288 /* In case we are checking arguments inside a template template
2289 parameter, ARG that does not come from default argument is
2290 also a TREE_LIST node. Note that ARG can also be a TREE_LIST
2291 in other cases such as overloaded functions. */
2292 if (arg
!= NULL_TREE
&& arg
!= error_mark_node
)
2293 arg
= TREE_VALUE (arg
);
2296 if (arg
== NULL_TREE
)
2297 /* We're out of arguments. */
2299 my_friendly_assert (!require_all_arguments
, 0);
2303 if (arg
== error_mark_node
)
2305 cp_error ("template argument %d is invalid", i
+ 1);
2310 if (TREE_CODE (arg
) == TREE_LIST
2311 && TREE_TYPE (arg
) != NULL_TREE
2312 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2314 /* The template argument was the name of some
2315 member function. That's usually
2316 illegal, but static members are OK. In any
2317 case, grab the underlying fields/functions
2318 and issue an error later if required. */
2319 arg
= TREE_VALUE (arg
);
2320 TREE_TYPE (arg
) = unknown_type_node
;
2323 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
2324 requires_type
= TREE_CODE (parm
) == TYPE_DECL
2325 || requires_tmpl_type
;
2327 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2328 we also accept implicitly created TYPE_DECL as a valid argument. */
2329 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
2330 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
2331 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
2332 && !CLASSTYPE_TEMPLATE_INFO (arg
))
2333 || (TREE_CODE (arg
) == RECORD_TYPE
2334 && CLASSTYPE_TEMPLATE_INFO (arg
)
2335 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
2336 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
2337 && requires_tmpl_type
);
2338 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
2339 arg
= TYPE_STUB_DECL (arg
);
2340 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
2341 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
2343 if (is_tmpl_parm
&& i
< nargs
)
2344 is_type
= TREE_CODE (arg
) == TYPE_DECL
|| is_tmpl_type
;
2346 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
2348 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
2349 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
2351 cp_pedwarn ("to refer to a type member of a template parameter,");
2352 cp_pedwarn (" use `typename %E'", arg
);
2354 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
2355 TREE_OPERAND (arg
, 1));
2358 if (is_type
!= requires_type
)
2364 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2367 cp_error (" expected a constant of type `%T', got `%T'",
2369 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2371 cp_error (" expected a type, got `%E'", arg
);
2375 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2378 if (is_tmpl_type
^ requires_tmpl_type
)
2382 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2385 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2387 cp_error (" expected a class template, got `%T'", arg
);
2390 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2395 if (requires_tmpl_type
)
2397 cp_error ("nested template template parameter not implemented");
2399 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2406 if (requires_tmpl_type
)
2408 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2409 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2411 /* The parameter and argument roles have to be switched
2412 here in order to handle default arguments properly.
2414 template<template <class> class TT> void f(TT<int>)
2415 should be able to accept vector<int> which comes from
2416 template <class T, class Allcator = allocator>
2419 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1, 1);
2420 if (val
!= error_mark_node
)
2423 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2425 if (val
!= error_mark_node
2426 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2427 val
= TREE_TYPE (val
);
2431 val
= groktypename (arg
);
2432 if (! processing_template_decl
)
2434 tree t
= target_type (val
);
2435 if (TREE_CODE (t
) != TYPENAME_TYPE
2437 && decl_function_context (TYPE_MAIN_DECL (t
)))
2439 cp_error ("type `%T' composed from a local class is not a valid template-argument",
2441 return error_mark_node
;
2448 tree t
= tsubst (TREE_TYPE (parm
), vec
, in_decl
);
2450 if (processing_template_decl
)
2451 arg
= maybe_fold_nontype_arg (arg
);
2453 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
2454 /* We used to call digest_init here. However, digest_init
2455 will report errors, which we don't want when complain
2456 is zero. More importantly, digest_init will try too
2457 hard to convert things: for example, `0' should not be
2458 converted to pointer type at this point according to
2459 the standard. Accepting this is not merely an
2460 extension, since deciding whether or not these
2461 conversions can occur is part of determining which
2462 function template to call, or whether a given epxlicit
2463 argument specification is legal. */
2464 val
= convert_nontype_argument (t
, arg
);
2468 if (val
== NULL_TREE
)
2469 val
= error_mark_node
;
2470 else if (val
== error_mark_node
&& complain
)
2471 cp_error ("could not convert template argument `%E' to `%T'",
2475 if (val
== error_mark_node
)
2478 TREE_VEC_ELT (vec
, i
) = val
;
2481 return error_mark_node
;
2485 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2486 of template arguments. Returns 0 otherwise. */
2489 comp_template_args (oldargs
, newargs
)
2490 tree oldargs
, newargs
;
2494 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
2497 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
2499 tree nt
= TREE_VEC_ELT (newargs
, i
);
2500 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2504 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2506 if (TREE_CODE (nt
) == TREE_VEC
)
2508 /* For member templates */
2509 if (comp_template_args (nt
, ot
))
2512 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2514 if (comptypes (ot
, nt
, 1))
2517 else if (cp_tree_equal (ot
, nt
) > 0)
2524 /* Given class template name and parameter list, produce a user-friendly name
2525 for the instantiation. */
2528 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2530 tree parms
, arglist
;
2533 static struct obstack scratch_obstack
;
2534 static char *scratch_firstobj
;
2537 if (!scratch_firstobj
)
2538 gcc_obstack_init (&scratch_obstack
);
2540 obstack_free (&scratch_obstack
, scratch_firstobj
);
2541 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2544 #define buflen sizeof(buf)
2545 #define check if (bufp >= buf+buflen-1) goto too_long
2546 #define ccat(c) *bufp++=(c); check
2547 #define advance bufp+=strlen(bufp); check
2548 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2551 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2553 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2560 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2561 s
= fndecl_as_string (ctx
, 0);
2562 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2563 s
= type_as_string_real (ctx
, 0, 1);
2565 my_friendly_abort (0);
2571 nparms
= TREE_VEC_LENGTH (parms
);
2572 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2573 for (i
= 0; i
< nparms
; i
++)
2575 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2576 tree arg
= TREE_VEC_ELT (arglist
, i
);
2581 if (TREE_CODE (parm
) == TYPE_DECL
)
2583 cat (type_as_string_real (arg
, 0, 1));
2586 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2588 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2589 /* Already substituted with real template. Just output
2590 the template name here */
2591 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2593 /* Output the parameter declaration */
2594 cat (type_as_string_real (arg
, 0, 1));
2598 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2600 if (TREE_CODE (arg
) == TREE_LIST
)
2602 /* New list cell was built because old chain link was in
2604 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2605 arg
= TREE_VALUE (arg
);
2607 /* No need to check arglist against parmlist here; we did that
2608 in coerce_template_parms, called from lookup_template_class. */
2609 cat (expr_as_string (arg
, 0));
2612 char *bufp
= obstack_next_free (&scratch_obstack
);
2614 while (bufp
[offset
- 1] == ' ')
2616 obstack_blank_fast (&scratch_obstack
, offset
);
2618 /* B<C<char> >, not B<C<char>> */
2619 if (bufp
[offset
- 1] == '>')
2624 return (char *) obstack_base (&scratch_obstack
);
2629 fatal ("out of (preallocated) string space creating template instantiation name");
2635 classtype_mangled_name (t
)
2638 if (CLASSTYPE_TEMPLATE_INFO (t
)
2639 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2641 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2642 char *mangled_name
= mangle_class_name_for_template
2643 (IDENTIFIER_POINTER (name
),
2644 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2645 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2646 tree id
= get_identifier (mangled_name
);
2647 IDENTIFIER_TEMPLATE (id
) = name
;
2651 return TYPE_IDENTIFIER (t
);
2655 add_pending_template (d
)
2660 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2661 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2663 ti
= DECL_TEMPLATE_INFO (d
);
2665 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2668 *template_tail
= perm_tree_cons
2669 (current_function_decl
, d
, NULL_TREE
);
2670 template_tail
= &TREE_CHAIN (*template_tail
);
2671 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2675 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2676 may be either a _DECL or an overloaded function or an
2677 IDENTIFIER_NODE), and ARGLIST. */
2680 lookup_template_function (fns
, arglist
)
2685 if (fns
== NULL_TREE
)
2687 cp_error ("non-template used as template");
2688 return error_mark_node
;
2691 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2692 copy_to_permanent (arglist
);
2694 return build_min (TEMPLATE_ID_EXPR
,
2696 ? TREE_TYPE (fns
) : unknown_type_node
,
2701 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2702 parameters, find the desired type.
2704 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2705 Since ARGLIST is build on the decl_obstack, we must copy it here
2706 to keep it from being reclaimed when the decl storage is reclaimed.
2708 IN_DECL, if non-NULL, is the template declaration we are trying to
2711 If the template class is really a local class in a template
2712 function, then the FUNCTION_CONTEXT is the function in which it is
2713 being instantiated. */
2716 lookup_template_class (d1
, arglist
, in_decl
, context
)
2721 tree
template = NULL_TREE
, parmlist
;
2725 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2727 if (IDENTIFIER_LOCAL_VALUE (d1
)
2728 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2729 template = IDENTIFIER_LOCAL_VALUE (d1
);
2732 template = IDENTIFIER_NAMESPACE_VALUE (d1
); /* XXX */
2734 template = IDENTIFIER_CLASS_VALUE (d1
);
2737 context
= DECL_CONTEXT (template);
2739 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2741 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2742 return error_mark_node
;
2743 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2744 d1
= DECL_NAME (template);
2746 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2748 template = CLASSTYPE_TI_TEMPLATE (d1
);
2749 d1
= DECL_NAME (template);
2751 else if (TREE_CODE (d1
) == TEMPLATE_DECL
2752 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
2755 d1
= DECL_NAME (template);
2756 context
= DECL_CONTEXT (template);
2759 my_friendly_abort (272);
2761 /* With something like `template <class T> class X class X { ... };'
2762 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2763 We don't want to do that, but we have to deal with the situation, so
2764 let's give them some syntax errors to chew on instead of a crash. */
2766 return error_mark_node
;
2767 if (TREE_CODE (template) != TEMPLATE_DECL
)
2769 cp_error ("non-template type `%T' used as a template", d1
);
2771 cp_error_at ("for template declaration `%D'", in_decl
);
2772 return error_mark_node
;
2775 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2777 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2778 template arguments */
2780 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2781 tree template2
= TYPE_STUB_DECL (parm
);
2784 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2785 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2787 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1, 0);
2788 if (arglist2
== error_mark_node
)
2789 return error_mark_node
;
2791 arglist2
= copy_to_permanent (arglist2
);
2792 CLASSTYPE_TEMPLATE_INFO (parm
)
2793 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2794 TYPE_SIZE (parm
) = 0;
2797 else if (PRIMARY_TEMPLATE_P (template)
2798 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2801 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2803 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2805 if (arglist
== error_mark_node
)
2806 return error_mark_node
;
2807 if (uses_template_parms (arglist
))
2810 if (comp_template_args
2811 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2812 found
= TREE_TYPE (template);
2815 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2816 found
; found
= TREE_CHAIN (found
))
2818 if (TI_USES_TEMPLATE_PARMS (found
)
2819 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2823 found
= TREE_VALUE (found
);
2828 if (can_free (&permanent_obstack
, arglist
))
2829 obstack_free (&permanent_obstack
, arglist
);
2834 /* FIXME avoid duplication. */
2835 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2839 id
= get_identifier (mangled_name
);
2840 IDENTIFIER_TEMPLATE (id
) = d1
;
2842 maybe_push_to_top_level (uses_template_parms (arglist
));
2843 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2845 if (context
!= NULL_TREE
)
2847 /* Set up the context for the type_decl correctly. Note
2848 that we must clear DECL_ASSEMBLER_NAME to fool
2849 build_overload_name into creating a new name. */
2850 tree type_decl
= TYPE_STUB_DECL (t
);
2852 TYPE_CONTEXT (t
) = context
;
2853 DECL_CONTEXT (type_decl
) = context
;
2854 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2855 DECL_ASSEMBLER_NAME (type_decl
) =
2856 get_identifier (build_overload_name (t
, 1, 1));
2859 pop_from_top_level ();
2863 tree type_ctx
= TYPE_CONTEXT (TREE_TYPE (template));
2864 tree args
= tsubst (CLASSTYPE_TI_ARGS (type_ctx
), arglist
, in_decl
);
2865 tree ctx
= lookup_template_class (type_ctx
, args
,
2866 in_decl
, NULL_TREE
);
2868 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2870 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2872 int save_temp
= processing_template_decl
;
2873 processing_template_decl
= 0;
2874 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2875 processing_template_decl
= save_temp
;
2879 t
= lookup_nested_type_by_name (ctx
, id
);
2880 my_friendly_assert (t
!= NULL_TREE
, 42);
2884 /* Seems to be wanted. */
2885 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2887 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2889 arglist
= copy_to_permanent (arglist
);
2890 CLASSTYPE_TEMPLATE_INFO (t
)
2891 = perm_tree_cons (template, arglist
, NULL_TREE
);
2892 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2893 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2894 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2895 = uses_template_parms (arglist
);
2897 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2899 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2900 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2901 if (! uses_template_parms (arglist
))
2902 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2903 = get_identifier (build_overload_name (t
, 1, 1));
2905 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2906 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2907 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2908 add_pending_template (t
);
2914 /* Should be defined in parse.h. */
2917 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
2918 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
2919 If FN returns non-zero, the iteration is terminated, and
2920 for_each_template_parm returns 1. Otherwise, the iteration
2921 continues. If FN never returns a non-zero value, the value
2922 returned by for_each_template_parm is 0. If FN is NULL, it is
2923 considered to be the function which always returns 1. */
2926 for_each_template_parm (t
, fn
, data
)
2933 switch (TREE_CODE (t
))
2937 /* We assume that the object must be instantiated in order to build
2938 the COMPONENT_REF, so we test only whether the type of the
2939 COMPONENT_REF uses template parms. */
2940 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2942 case IDENTIFIER_NODE
:
2943 if (!IDENTIFIER_TEMPLATE (t
))
2945 my_friendly_abort (42);
2947 /* aggregates of tree nodes */
2950 int i
= TREE_VEC_LENGTH (t
);
2952 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
2957 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
2958 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
2960 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
2962 /* constructed type nodes */
2964 case REFERENCE_TYPE
:
2965 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2967 if (TYPE_PTRMEMFUNC_FLAG (t
))
2968 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
2971 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2973 return for_each_template_parm (TREE_VALUE
2974 (CLASSTYPE_TEMPLATE_INFO (t
)),
2977 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
2979 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2981 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
2983 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2985 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
2987 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2989 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
2991 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
2993 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2997 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3000 /* A template template parameter is encountered */
3001 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3002 /* We are parsing a template declaration */
3004 /* We are instantiating templates with template template
3009 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
3011 goto check_type_and_context
;
3015 /* ??? What about FIELD_DECLs? */
3016 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
3017 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
3021 check_type_and_context
:
3022 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
3024 if (DECL_CONTEXT (t
)
3025 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
3030 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3032 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3034 /* template parm nodes */
3035 case TEMPLATE_TYPE_PARM
:
3036 case TEMPLATE_TEMPLATE_PARM
:
3037 case TEMPLATE_PARM_INDEX
:
3039 return (*fn
)(t
, data
);
3043 /* simple type nodes */
3045 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
3047 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
3059 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
3060 if (for_each_template_parm (TREE_VALUE (v
), fn
, data
))
3072 /* Non-error_mark_node ERROR_MARKs are bad things. */
3073 my_friendly_assert (t
== error_mark_node
, 274);
3082 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3085 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
3086 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3087 (TREE_TYPE (t
)), fn
, data
);
3088 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
3092 case REINTERPRET_CAST_EXPR
:
3093 case CONST_CAST_EXPR
:
3094 case STATIC_CAST_EXPR
:
3095 case DYNAMIC_CAST_EXPR
:
3103 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3106 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3114 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
3115 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
3122 sorry ("testing %s for template parms",
3123 tree_code_name
[(int) TREE_CODE (t
)]);
3124 my_friendly_abort (82);
3131 uses_template_parms (t
)
3134 return for_each_template_parm (t
, 0, 0);
3137 static struct tinst_level
*current_tinst_level
= 0;
3138 static struct tinst_level
*free_tinst_level
= 0;
3139 static int tinst_depth
= 0;
3140 extern int max_tinst_depth
;
3141 #ifdef GATHER_STATISTICS
3142 int depth_reached
= 0;
3145 /* Print out all the template instantiations that we are currently
3149 print_template_context ()
3151 struct tinst_level
*p
= current_tinst_level
;
3153 char *file
= input_filename
;
3155 for (; p
; p
= p
->next
)
3157 cp_error (" instantiated from `%D'", p
->decl
);
3159 input_filename
= p
->file
;
3161 error (" instantiated from here");
3164 input_filename
= file
;
3168 push_tinst_level (d
)
3171 struct tinst_level
*new;
3173 if (tinst_depth
>= max_tinst_depth
)
3175 /* If the instantiation in question still has unbound template parms,
3176 we don't really care if we can't instantiate it, so just return.
3177 This happens with base instantiation for implicit `typename'. */
3178 if (uses_template_parms (d
))
3181 error ("template instantiation depth exceeds maximum of %d",
3183 error (" (use -ftemplate-depth-NN to increase the maximum)");
3184 cp_error (" instantiating `%D'", d
);
3186 print_template_context ();
3191 if (free_tinst_level
)
3193 new = free_tinst_level
;
3194 free_tinst_level
= new->next
;
3197 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
3201 new->file
= input_filename
;
3202 new->next
= current_tinst_level
;
3203 current_tinst_level
= new;
3206 #ifdef GATHER_STATISTICS
3207 if (tinst_depth
> depth_reached
)
3208 depth_reached
= tinst_depth
;
3217 struct tinst_level
*old
= current_tinst_level
;
3219 current_tinst_level
= old
->next
;
3220 old
->next
= free_tinst_level
;
3221 free_tinst_level
= old
;
3225 struct tinst_level
*
3228 struct tinst_level
*p
= current_tinst_level
;
3231 for (; p
->next
; p
= p
->next
)
3237 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
3238 vector of template arguments, as for tsubst.
3240 Returns an appropriate tsbust'd friend declaration. */
3243 tsubst_friend_function (decl
, args
)
3249 if (TREE_CODE (decl
) == FUNCTION_DECL
3250 && DECL_TEMPLATE_INSTANTIATION (decl
)
3251 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
3252 /* This was a friend declared with an explicit template
3253 argument list, e.g.:
3257 to indicate that f was a template instantiation, not a new
3258 function declaration. Now, we have to figure out what
3259 instantiation of what template. */
3267 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
3269 tsubst (DECL_TI_ARGS (decl
),
3272 /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
3274 tinfo
= DECL_TEMPLATE_INFO (decl
);
3275 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3276 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3277 DECL_TEMPLATE_INFO (decl
) = tinfo
;
3279 tmpl
= determine_specialization (template_id
,
3283 return instantiate_template (tmpl
, new_args
);
3286 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3288 /* The new_friend will look like an instantiation, to the
3289 compiler, but is not an instantiation from the point of view of
3290 the language. For example, we might have had:
3292 template <class T> struct S {
3293 template <class U> friend void f(T, U);
3296 Then, in S<int>, template <class U> void f(int, U) is not an
3297 instantiation of anything. */
3298 DECL_USE_TEMPLATE (new_friend
) = 0;
3299 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3300 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
3302 if (DECL_CONTEXT (new_friend
) == NULL_TREE
)
3304 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
3305 /* This declaration is a `primary' template. */
3306 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend
))
3309 new_friend
= pushdecl_top_level (new_friend
);
3311 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
3313 /* Check to see that the declaration is really present, and,
3314 possibly obtain an improved declaration. */
3315 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
3327 instantiate_class_template (type
)
3330 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
3331 tree typedecl
, outer_args
;
3333 if (type
== error_mark_node
)
3334 return error_mark_node
;
3336 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
3338 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
3341 template = TI_TEMPLATE (template_info
);
3342 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
3343 args
= TI_ARGS (template_info
);
3345 if (DECL_TEMPLATE_INFO (template))
3347 outer_args
= DECL_TI_ARGS (template);
3348 while (DECL_TEMPLATE_INFO (template))
3349 template = DECL_TI_TEMPLATE (template);
3352 outer_args
= NULL_TREE
;
3354 t
= most_specialized_class
3355 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
, outer_args
);
3357 if (t
== error_mark_node
)
3359 char *str
= "candidates are:";
3360 cp_error ("ambiguous class template instantiation for `%#T'", type
);
3361 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
3363 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3366 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
3370 TYPE_BEING_DEFINED (type
) = 1;
3371 return error_mark_node
;
3374 pattern
= TREE_TYPE (t
);
3376 pattern
= TREE_TYPE (template);
3378 if (TYPE_SIZE (pattern
) == NULL_TREE
)
3382 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3385 if (pedantic
&& uses_template_parms (args
))
3386 /* If there are still template parameters amongst the args, then
3387 we can't instantiate the type; there's no telling whether or not one
3388 of the template parameters might eventually be instantiated to some
3389 value that results in a specialization being used. */
3392 /* We must copy the arguments to the permanent obstack since
3393 during the tsubst'ing below they may wind up in the
3394 DECL_TI_ARGS of some instantiated member template. */
3395 args
= copy_to_permanent (args
);
3397 TYPE_BEING_DEFINED (type
) = 1;
3399 if (! push_tinst_level (type
))
3402 maybe_push_to_top_level (uses_template_parms (type
));
3403 pushclass (type
, 0);
3406 args
= add_to_template_args (outer_args
, args
);
3408 if (flag_external_templates
)
3410 if (flag_alt_external_templates
)
3412 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
3413 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
3414 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3415 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3416 && CLASSTYPE_INTERFACE_KNOWN (type
));
3420 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
3421 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3422 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
3423 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3424 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3425 && CLASSTYPE_INTERFACE_KNOWN (type
));
3430 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
3431 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
3434 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
3435 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
3436 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
3437 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
3438 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
3439 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
3440 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
3441 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
3442 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
3443 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
3444 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
3445 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
3446 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
3447 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
3448 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
3449 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
3450 TYPE_USES_COMPLEX_INHERITANCE (type
)
3451 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
3452 TYPE_USES_MULTIPLE_INHERITANCE (type
)
3453 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
3454 TYPE_USES_VIRTUAL_BASECLASSES (type
)
3455 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
3456 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
3457 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
3459 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
3461 /* If this is a partial instantiation, don't tsubst anything. We will
3462 only use this type for implicit typename, so the actual contents don't
3463 matter. All that matters is whether a particular name is a type. */
3464 if (uses_template_parms (type
))
3466 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
3467 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
3468 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
3469 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
3470 TYPE_SIZE (type
) = integer_zero_node
;
3475 tree binfo
= TYPE_BINFO (type
);
3476 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
3482 int len
= TREE_VEC_LENGTH (pbases
);
3483 bases
= make_tree_vec (len
);
3484 for (i
= 0; i
< len
; ++i
)
3488 TREE_VEC_ELT (bases
, i
) = elt
3489 = tsubst (TREE_VEC_ELT (pbases
, i
), args
, NULL_TREE
);
3490 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
3492 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
3494 ("base type `%T' of `%T' fails to be a struct or class type",
3495 TREE_TYPE (elt
), type
);
3496 else if (TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
3497 cp_error ("base class `%T' of `%T' has incomplete type",
3498 TREE_TYPE (elt
), type
);
3500 /* Don't initialize this until the vector is filled out, or
3501 lookups will crash. */
3502 BINFO_BASETYPES (binfo
) = bases
;
3506 field_chain
= &TYPE_FIELDS (type
);
3508 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
3510 tree tag
= TREE_VALUE (t
);
3512 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
3513 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
3515 (void) tsubst_enum (tag
, args
, field_chain
);
3516 while (*field_chain
)
3518 DECL_FIELD_CONTEXT (*field_chain
) = type
;
3519 field_chain
= &TREE_CHAIN (*field_chain
);
3523 tsubst (tag
, args
, NULL_TREE
);
3526 /* Don't replace enum constants here. */
3527 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
3528 if (TREE_CODE (t
) != CONST_DECL
)
3530 tree r
= tsubst (t
, args
, NULL_TREE
);
3531 if (TREE_CODE (r
) == VAR_DECL
)
3533 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
3534 /* Perhaps we should do more of grokfield here. */
3536 DECL_IN_AGGR_P (r
) = 1;
3537 DECL_EXTERNAL (r
) = 1;
3538 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
3542 field_chain
= &TREE_CHAIN (r
);
3545 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
3546 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
3548 if (DECL_CONSTRUCTOR_P (t
))
3549 grok_ctor_properties (type
, t
);
3550 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
3551 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
3554 /* Construct the DECL_FRIENDLIST for the new class type. */
3555 typedecl
= TYPE_MAIN_DECL (type
);
3556 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
3562 DECL_FRIENDLIST (typedecl
)
3563 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
3564 DECL_FRIENDLIST (typedecl
));
3566 for (friends
= TREE_VALUE (t
);
3567 friends
!= NULL_TREE
;
3568 friends
= TREE_CHAIN (friends
))
3570 if (TREE_PURPOSE (friends
) == error_mark_node
)
3572 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3573 = tree_cons (error_mark_node
,
3574 tsubst_friend_function (TREE_VALUE (friends
),
3576 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3580 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3581 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
, NULL_TREE
),
3583 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3589 t
= CLASSTYPE_FRIEND_CLASSES (type
)
3590 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
, NULL_TREE
);
3592 /* This does injection for friend classes. */
3593 for (; t
; t
= TREE_CHAIN (t
))
3594 TREE_VALUE (t
) = xref_tag_from_type (TREE_VALUE (t
), NULL_TREE
, 1);
3596 /* This does injection for friend functions. */
3597 if (!processing_template_decl
)
3599 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
, NULL_TREE
);
3601 for (; t
; t
= TREE_CHAIN (t
))
3603 tree d
= TREE_VALUE (t
);
3605 if (TREE_CODE (d
) == TYPE_DECL
)
3606 /* Already injected. */;
3612 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
3613 if (TREE_CODE (t
) == FIELD_DECL
)
3615 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
3616 require_complete_type (t
);
3619 type
= finish_struct_1 (type
, 0);
3620 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
3622 repo_template_used (type
);
3623 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
3624 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
3627 TYPE_BEING_DEFINED (type
) = 0;
3630 pop_from_top_level ();
3640 if (t1
== NULL_TREE
)
3641 return t2
== NULL_TREE
;
3642 if (t2
== NULL_TREE
)
3644 /* Don't care if one declares its arg const and the other doesn't -- the
3645 main variant of the arg type is all that matters. */
3646 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
3647 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
3649 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
3653 lookup_nested_type_by_name (ctype
, name
)
3658 complete_type (ctype
);
3660 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
3662 if (name
== TREE_PURPOSE (t
)
3663 /* this catches typedef enum { foo } bar; */
3664 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
3665 return TREE_VALUE (t
);
3670 /* If arg is a non-type template parameter that does not depend on template
3671 arguments, fold it like we weren't in the body of a template. */
3674 maybe_fold_nontype_arg (arg
)
3677 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
3678 && !uses_template_parms (arg
))
3680 /* Sometimes, one of the args was an expression involving a
3681 template constant parameter, like N - 1. Now that we've
3682 tsubst'd, we might have something like 2 - 1. This will
3683 confuse lookup_template_class, so we do constant folding
3684 here. We have to unset processing_template_decl, to
3685 fool build_expr_from_tree() into building an actual
3688 int saved_processing_template_decl
= processing_template_decl
;
3689 processing_template_decl
= 0;
3690 arg
= fold (build_expr_from_tree (arg
));
3691 processing_template_decl
= saved_processing_template_decl
;
3696 /* Return the TREE_VEC with the arguments for the innermost template header,
3697 where ARGS is either that or the VEC of VECs for all the arguments.
3699 If is_spec, then we are dealing with a specialization of a member
3700 template, and want the second-innermost args, the innermost ones that
3701 are instantiated. */
3704 innermost_args (args
, is_spec
)
3708 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3709 return TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1 - is_spec
);
3713 /* Take the tree structure T and replace template parameters used therein
3714 with the argument vector ARGS. IN_DECL is an associated decl for
3717 tsubst is used for dealing with types, decls and the like; for
3718 expressions, use tsubst_expr or tsubst_copy. */
3721 tsubst (t
, args
, in_decl
)
3727 if (t
== NULL_TREE
|| t
== error_mark_node
3728 || t
== integer_type_node
3729 || t
== void_type_node
3730 || t
== char_type_node
)
3733 type
= TREE_TYPE (t
);
3734 if (type
== unknown_type_node
)
3735 my_friendly_abort (42);
3736 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3737 && TREE_CODE (t
) != TYPENAME_TYPE
3738 && TREE_CODE (t
) != TEMPLATE_DECL
)
3739 type
= tsubst (type
, args
, in_decl
);
3741 switch (TREE_CODE (t
))
3744 if (TYPE_PTRMEMFUNC_P (t
))
3746 tree r
= build_ptrmemfunc_type
3747 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, in_decl
));
3748 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3752 /* else fall through */
3754 if (uses_template_parms (t
))
3756 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3762 ? tsubst (TYPE_CONTEXT (t
), args
, in_decl
) : NULL_TREE
;
3764 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3766 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3770 /* else fall through */
3772 case IDENTIFIER_NODE
:
3785 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
3786 if (ctx
== NULL_TREE
)
3788 else if (ctx
== current_function_decl
)
3789 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3791 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3795 if (t
== integer_type_node
)
3798 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3799 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3803 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3804 max
= tsubst_expr (max
, args
, in_decl
);
3805 if (processing_template_decl
)
3807 tree itype
= make_node (INTEGER_TYPE
);
3808 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3809 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3814 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3815 return build_index_2_type (size_zero_node
, max
);
3818 case TEMPLATE_TYPE_PARM
:
3819 case TEMPLATE_TEMPLATE_PARM
:
3820 case TEMPLATE_PARM_INDEX
:
3827 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3828 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3830 idx
= TEMPLATE_TYPE_IDX (t
);
3831 level
= TEMPLATE_TYPE_LEVEL (t
);
3835 idx
= TEMPLATE_PARM_IDX (t
);
3836 level
= TEMPLATE_PARM_LEVEL (t
);
3839 if (TREE_VEC_LENGTH (args
) > 0)
3841 tree arg
= NULL_TREE
;
3843 if (TREE_VEC_ELT (args
, 0) != NULL_TREE
3844 && TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3846 levels
= TREE_VEC_LENGTH (args
);
3847 if (level
<= levels
)
3849 (TREE_VEC_ELT (args
, level
- 1), idx
);
3855 arg
= TREE_VEC_ELT (args
, idx
);
3858 if (arg
!= NULL_TREE
)
3860 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3861 return cp_build_type_variant
3862 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3863 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3864 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3866 if (CLASSTYPE_TEMPLATE_INFO (t
))
3868 /* We are processing a type constructed from
3869 a template template parameter */
3870 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3874 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3875 we are resolving nested-types in the signature of
3876 a member function templates.
3877 Otherwise ARG is a TEMPLATE_DECL and is the real
3878 template to be instantiated. */
3879 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3880 arg
= TYPE_NAME (arg
);
3882 r
= lookup_template_class (DECL_NAME (arg
),
3884 DECL_CONTEXT (arg
));
3885 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3889 /* We are processing a template argument list. */
3898 /* This can happen during the attempted tsubst'ing in
3899 unify. This means that we don't yet have any information
3900 about the template parameter in question. */
3903 /* If we get here, we must have been looking at a parm for a
3904 more deeply nested template. Make a new version of this
3905 template parameter, but with a lower level. */
3906 switch (TREE_CODE (t
))
3908 case TEMPLATE_TYPE_PARM
:
3909 case TEMPLATE_TEMPLATE_PARM
:
3911 TEMPLATE_TYPE_PARM_INDEX (r
)
3912 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
3914 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
3915 TYPE_MAIN_VARIANT (r
) = r
;
3916 TYPE_POINTER_TO (r
) = NULL_TREE
;
3917 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
3919 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3920 && CLASSTYPE_TEMPLATE_INFO (t
))
3922 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3923 CLASSTYPE_TEMPLATE_INFO (r
)
3924 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
3928 case TEMPLATE_PARM_INDEX
:
3929 r
= reduce_template_parm_level (t
, TREE_TYPE (t
), levels
);
3933 my_friendly_abort (0);
3941 /* We can get here when processing a member template function
3942 of a template class. */
3944 tree decl
= DECL_TEMPLATE_RESULT (t
);
3948 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
3950 if (!is_template_template_parm
)
3952 /* We might already have an instance of this template. */
3953 spec
= retrieve_specialization (t
, args
);
3954 if (spec
!= NULL_TREE
)
3958 /* Make a new template decl. It will be similar to the
3959 original, but will record the current template arguments.
3960 We also create a new function declaration, which is just
3961 like the old one, but points to this new template, rather
3962 than the old one. */
3963 tmpl
= copy_node (t
);
3964 copy_lang_decl (tmpl
);
3965 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3966 DECL_CHAIN (tmpl
) = NULL_TREE
;
3967 TREE_CHAIN (tmpl
) = NULL_TREE
;
3969 if (is_template_template_parm
)
3971 tree new_decl
= tsubst (decl
, args
, in_decl
);
3972 DECL_RESULT (tmpl
) = new_decl
;
3973 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3977 DECL_CONTEXT (tmpl
) = tsubst (DECL_CONTEXT (t
),
3979 DECL_CLASS_CONTEXT (tmpl
) = tsubst (DECL_CLASS_CONTEXT (t
),
3981 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
3983 if (TREE_CODE (decl
) == TYPE_DECL
)
3985 tree new_type
= tsubst (TREE_TYPE (t
), args
, in_decl
);
3986 TREE_TYPE (tmpl
) = new_type
;
3987 CLASSTYPE_TI_TEMPLATE (new_type
) = tmpl
;
3988 DECL_RESULT (tmpl
) = TYPE_MAIN_DECL (new_type
);
3992 tree new_decl
= tsubst (decl
, args
, in_decl
);
3993 DECL_RESULT (tmpl
) = new_decl
;
3994 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
3995 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3998 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
3999 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
4001 /* The template parameters for this new template are all the
4002 template parameters for the old template, except the
4003 outermost level of parameters. */
4004 for (new_parms
= &DECL_TEMPLATE_PARMS (tmpl
),
4005 parms
= DECL_TEMPLATE_PARMS (t
);
4006 TREE_CHAIN (parms
) != NULL_TREE
;
4007 new_parms
= &(TREE_CHAIN (*new_parms
)),
4008 parms
= TREE_CHAIN (parms
))
4011 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
4014 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
4016 tree default_value
=
4017 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4019 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4021 TREE_VEC_ELT (new_vec
, i
)
4022 = build_tree_list (tsubst (default_value
, args
, in_decl
),
4023 tsubst (parm_decl
, args
, in_decl
));
4028 tree_cons (build_int_2 (0,
4030 (TREE_PURPOSE (parms
)) - 1),
4035 if (PRIMARY_TEMPLATE_P (t
))
4036 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
4038 /* We don't partially instantiate partial specializations. */
4039 if (TREE_CODE (decl
) == TYPE_DECL
)
4042 /* What should we do with the specializations of this member
4043 template? Are they specializations of this new template,
4044 or instantiations of the templates they previously were?
4045 this new template? And where should their
4046 DECL_TI_TEMPLATES point? */
4047 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
4048 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
4050 spec
= TREE_CHAIN (spec
))
4052 /* It helps to consider example here. Consider:
4063 Now, for example, we are instantiating S<int>::f(U u).
4064 We want to make a template:
4069 It will have a specialization, for the case U = int*, of
4073 void S<int>::f<int*>(int*);
4075 This specialization will be an instantiation of
4076 the specialization given in the declaration of S, with
4077 argument list int*. */
4079 tree fn
= TREE_VALUE (spec
);
4083 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
4084 /* Instantiations are on the same list, but they're of
4085 no concern to us. */
4088 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
4090 new_fn
= tsubst (DECL_RESULT (fn
), args
,
4092 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
4093 perm_tree_cons (spec_args
, new_fn
,
4094 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
4097 /* Record this partial instantiation. */
4098 register_specialization (tmpl
, t
, args
);
4108 tree tmpl
= NULL_TREE
;
4111 if (DECL_CONTEXT (t
) != NULL_TREE
4112 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
4114 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
4118 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, t
);
4119 type
= tsubst (type
, args
, in_decl
);
4125 type
= tsubst (type
, args
, in_decl
);
4128 /* If we are instantiating a specialization, get the other args. */
4129 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4133 tmpl
= DECL_TI_TEMPLATE (t
);
4135 /* Start by getting the innermost args. */
4136 argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4138 if (DECL_TEMPLATE_INFO (tmpl
))
4139 argvec
= complete_template_args (tmpl
, argvec
, 0);
4141 /* Do we already have this instantiation? */
4142 spec
= retrieve_specialization (tmpl
, argvec
);
4147 /* We do NOT check for matching decls pushed separately at this
4148 point, as they may not represent instantiations of this
4149 template, and in any case are considered separate under the
4150 discrete model. Instead, see add_maybe_template. */
4154 DECL_USE_TEMPLATE (r
) = 0;
4155 TREE_TYPE (r
) = type
;
4158 = tsubst (DECL_CONTEXT (t
), args
, t
);
4159 DECL_CLASS_CONTEXT (r
) = ctx
;
4161 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
4162 IDENTIFIER_POINTER (DECL_NAME (r
)),
4163 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
4165 /* Type-conversion operator. Reconstruct the name, in
4166 case it's the name of one of the template's parameters. */
4167 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
4170 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
4172 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
4173 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
4174 buf
= (char *) alloca (strlen (dbuf
)
4175 + sizeof (DESTRUCTOR_DECL_PREFIX
));
4176 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
4179 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
4183 /* Instantiations of template functions must be mangled
4184 specially, in order to conform to 14.5.5.1
4185 [temp.over.link]. We use in_decl below rather than
4186 DECL_TI_TEMPLATE (r) because the latter is set to
4187 NULL_TREE in instantiate_decl. */
4191 if (DECL_TEMPLATE_INFO (r
))
4192 tmpl
= DECL_TI_TEMPLATE (r
);
4196 /* tmpl will be NULL if this is a specialization of a
4197 member function of a template class. */
4198 if (name_mangling_version
< 1
4199 || tmpl
== NULL_TREE
4200 || (member
&& !is_member_template (tmpl
)
4201 && !DECL_TEMPLATE_INFO (tmpl
)))
4203 arg_types
= TYPE_ARG_TYPES (type
);
4204 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4205 arg_types
= hash_tree_chain
4206 (build_pointer_type (DECL_CONTEXT (r
)),
4209 DECL_ASSEMBLER_NAME (r
)
4210 = build_decl_overload (DECL_NAME (r
), arg_types
,
4218 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4220 /* We pass the outermost template parameters to
4221 build_template_decl_overload, since the innermost
4222 template parameters are still just template
4223 parameters; there are no corresponding subsitution
4224 arguments. Levels of parms that have been bound
4225 before are not represented in DECL_TEMPLATE_PARMS. */
4226 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
4227 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
4228 tparms
= TREE_CHAIN (tparms
);
4230 targs
= innermost_args (args
, 0);
4234 /* If the template is a specialization, then it is
4235 a member template specialization. We have
4238 template <class T> struct S {
4239 template <int i> void f();
4240 template <> void f<7>();
4243 and now we are forming S<double>::f<7>.
4244 Therefore, the template parameters of interest
4245 are those that are specialized by the template
4246 (i.e., the int), not those we are using to
4247 instantiate the template, i.e. the double. */
4248 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
4249 targs
= DECL_TI_ARGS (tmpl
);
4252 my_friendly_assert (tparms
!= NULL_TREE
4253 && TREE_CODE (tparms
) == TREE_LIST
,
4255 tparms
= TREE_VALUE (tparms
);
4257 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
4258 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4259 arg_types
= hash_tree_chain
4260 (build_pointer_type (DECL_CONTEXT (r
)),
4263 DECL_ASSEMBLER_NAME (r
)
4264 = build_template_decl_overload
4265 (DECL_NAME (r
), arg_types
,
4266 TREE_TYPE (TREE_TYPE (tmpl
)),
4267 tparms
, targs
, member
);
4271 make_decl_rtl (r
, NULL_PTR
, 1);
4273 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, t
);
4274 DECL_MAIN_VARIANT (r
) = r
;
4275 DECL_RESULT (r
) = NULL_TREE
;
4276 DECL_INITIAL (r
) = NULL_TREE
;
4278 TREE_STATIC (r
) = 0;
4279 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
4280 DECL_EXTERNAL (r
) = 1;
4281 DECL_INTERFACE_KNOWN (r
) = 0;
4282 DECL_DEFER_OUTPUT (r
) = 0;
4283 TREE_CHAIN (r
) = NULL_TREE
;
4284 DECL_CHAIN (r
) = NULL_TREE
;
4285 DECL_PENDING_INLINE_INFO (r
) = 0;
4288 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
4289 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
4291 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4293 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4295 /* If we're not using ANSI overloading, then we might have
4296 called duplicate_decls above, and gotten back an
4297 preexisting version of this function. We treat such a
4298 function as a specialization. Otherwise, we cleared
4299 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
4300 this condition will be false. */
4301 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
4302 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
4304 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4306 register_specialization (r
, tmpl
, argvec
);
4309 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
4310 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
4312 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
4313 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
4320 tree r
= copy_node (t
);
4321 TREE_TYPE (r
) = type
;
4322 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
4323 DECL_INITIAL (r
) = TREE_TYPE (r
);
4325 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
, in_decl
);
4327 DECL_CONTEXT (r
) = NULL_TREE
;
4328 #ifdef PROMOTE_PROTOTYPES
4329 if ((TREE_CODE (type
) == INTEGER_TYPE
4330 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4331 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4332 DECL_ARG_TYPE (r
) = integer_type_node
;
4335 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, TREE_CHAIN (t
));
4341 tree r
= copy_node (t
);
4342 TREE_TYPE (r
) = type
;
4345 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, in_decl
);
4347 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, in_decl
);
4348 TREE_CHAIN (r
) = NULL_TREE
;
4354 tree r
= copy_node (t
);
4356 = tsubst_copy (DECL_INITIAL (t
), args
, in_decl
);
4357 TREE_CHAIN (r
) = NULL_TREE
;
4364 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, in_decl
);
4366 /* Do we already have this instantiation? */
4367 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4369 tree tmpl
= DECL_TI_TEMPLATE (t
);
4370 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4372 for (; decls
; decls
= TREE_CHAIN (decls
))
4373 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
4374 return TREE_VALUE (decls
);
4378 TREE_TYPE (r
) = type
;
4379 DECL_CONTEXT (r
) = ctx
;
4380 if (TREE_STATIC (r
))
4381 DECL_ASSEMBLER_NAME (r
)
4382 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
4384 /* Don't try to expand the initializer until someone tries to use
4385 this variable; otherwise we run into circular dependencies. */
4386 DECL_INITIAL (r
) = NULL_TREE
;
4391 if (DECL_LANG_SPECIFIC (r
))
4394 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
4397 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4399 tree tmpl
= DECL_TI_TEMPLATE (t
);
4400 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4401 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4403 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4404 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
4405 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4407 TREE_CHAIN (r
) = NULL_TREE
;
4412 if (t
== TYPE_NAME (TREE_TYPE (t
)))
4413 return TYPE_NAME (type
);
4416 tree r
= copy_node (t
);
4417 TREE_TYPE (r
) = type
;
4418 DECL_CONTEXT (r
) = current_class_type
;
4419 TREE_CHAIN (r
) = NULL_TREE
;
4425 tree purpose
, value
, chain
, result
;
4426 int via_public
, via_virtual
, via_protected
;
4428 if (t
== void_list_node
)
4431 via_public
= TREE_VIA_PUBLIC (t
);
4432 via_protected
= TREE_VIA_PROTECTED (t
);
4433 via_virtual
= TREE_VIA_VIRTUAL (t
);
4435 purpose
= TREE_PURPOSE (t
);
4437 purpose
= tsubst (purpose
, args
, in_decl
);
4438 value
= TREE_VALUE (t
);
4440 value
= tsubst (value
, args
, in_decl
);
4441 chain
= TREE_CHAIN (t
);
4442 if (chain
&& chain
!= void_type_node
)
4443 chain
= tsubst (chain
, args
, in_decl
);
4444 if (purpose
== TREE_PURPOSE (t
)
4445 && value
== TREE_VALUE (t
)
4446 && chain
== TREE_CHAIN (t
))
4448 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
4449 purpose
, value
, chain
);
4450 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
4454 if (type
!= NULL_TREE
)
4460 if (type
== TREE_TYPE (t
))
4463 TREE_TYPE (t
) = complete_type (type
);
4464 if (IS_AGGR_TYPE (type
))
4466 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
4467 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
4468 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
4469 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
4474 /* Otherwise, a vector of template arguments. */
4476 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4477 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4479 bzero ((char *) elts
, len
* sizeof (tree
));
4481 for (i
= 0; i
< len
; i
++)
4483 elts
[i
] = maybe_fold_nontype_arg
4484 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, in_decl
));
4486 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4493 t
= make_tree_vec (len
);
4494 for (i
= 0; i
< len
; i
++)
4495 TREE_VEC_ELT (t
, i
) = elts
[i
];
4500 case REFERENCE_TYPE
:
4503 enum tree_code code
;
4505 if (type
== TREE_TYPE (t
))
4508 code
= TREE_CODE (t
);
4509 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4511 static int last_line
= 0;
4512 static char* last_file
= 0;
4514 /* We keep track of the last time we issued this error
4515 message to avoid spewing a ton of messages during a
4516 single bad template instantiation. */
4517 if (last_line
!= lineno
||
4518 last_file
!= input_filename
)
4520 cp_error ("cannot form type %s to reference type %T during template instantiation",
4521 (code
== POINTER_TYPE
) ? "pointer" : "reference",
4524 last_file
= input_filename
;
4527 /* Use the underlying type in an attempt at error
4528 recovery; maybe the user meant vector<int> and wrote
4529 vector<int&>, or some such. */
4530 if (code
== REFERENCE_TYPE
)
4533 r
= build_pointer_type (TREE_TYPE (type
));
4535 else if (code
== POINTER_TYPE
)
4536 r
= build_pointer_type (type
);
4538 r
= build_reference_type (type
);
4539 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
4541 /* Will this ever be needed for TYPE_..._TO values? */
4546 return build_offset_type
4547 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, in_decl
), type
);
4551 tree values
= TYPE_ARG_TYPES (t
);
4552 tree context
= TYPE_CONTEXT (t
);
4553 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
4556 /* Don't bother recursing if we know it won't change anything. */
4557 if (values
!= void_list_node
)
4559 /* This should probably be rewritten to use hash_tree_cons for
4560 the memory savings. */
4561 tree first
= NULL_TREE
;
4562 tree last
= NULL_TREE
;
4564 for (; values
&& values
!= void_list_node
;
4565 values
= TREE_CHAIN (values
))
4567 tree value
= TYPE_MAIN_VARIANT (type_decays_to
4568 (tsubst (TREE_VALUE (values
), args
, in_decl
)));
4569 /* Don't instantiate default args unless they are used.
4570 Handle it in build_over_call instead. */
4571 tree purpose
= TREE_PURPOSE (values
);
4572 tree x
= build_tree_list (purpose
, value
);
4575 TREE_CHAIN (last
) = x
;
4581 if (values
== void_list_node
)
4582 TREE_CHAIN (last
) = void_list_node
;
4587 context
= tsubst (context
, args
, in_decl
);
4588 /* Could also optimize cases where return value and
4589 values have common elements (e.g., T min(const &T, const T&). */
4591 /* If the above parameters haven't changed, just return the type. */
4592 if (type
== TREE_TYPE (t
)
4593 && values
== TYPE_VALUES (t
)
4594 && context
== TYPE_CONTEXT (t
))
4597 /* Construct a new type node and return it. */
4598 if (TREE_CODE (t
) == FUNCTION_TYPE
4599 && context
== NULL_TREE
)
4601 fntype
= build_function_type (type
, values
);
4603 else if (context
== NULL_TREE
)
4605 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
4607 fntype
= build_cplus_method_type (base
, type
,
4608 TREE_CHAIN (values
));
4612 fntype
= make_node (TREE_CODE (t
));
4613 TREE_TYPE (fntype
) = type
;
4614 TYPE_CONTEXT (fntype
) = context
;
4615 TYPE_VALUES (fntype
) = values
;
4616 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
4617 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
4618 TYPE_MODE (fntype
) = TYPE_MODE (t
);
4619 if (TYPE_METHOD_BASETYPE (t
))
4620 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
4622 /* Need to generate hash value. */
4623 my_friendly_abort (84);
4625 fntype
= build_type_variant (fntype
,
4630 raises
= tsubst (raises
, args
, in_decl
);
4631 fntype
= build_exception_variant (fntype
, raises
);
4637 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, in_decl
);
4639 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
4641 r
= build_cplus_array_type (type
, domain
);
4647 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
4648 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4649 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
)));
4653 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
4654 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
)));
4658 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
4659 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
, in_decl
);
4660 f
= make_typename_type (ctx
, f
);
4661 return cp_build_type_variant
4662 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
4663 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
4667 return make_pointer_declarator
4668 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4671 return make_reference_declarator
4672 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4675 return build_parse_node
4676 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4677 tsubst_expr (TREE_OPERAND (t
, 1), args
, in_decl
));
4680 return make_call_declarator
4681 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4682 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
4683 TREE_OPERAND (t
, 2),
4684 tsubst (TREE_TYPE (t
), args
, in_decl
));
4687 return build_parse_node
4688 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4689 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
));
4692 sorry ("use of `%s' in template",
4693 tree_code_name
[(int) TREE_CODE (t
)]);
4694 return error_mark_node
;
4701 emit_line_note (input_filename
, lineno
);
4705 expand_start_bindings (0);
4712 int saved_warn_unused
= 0;
4714 if (processing_template_decl
)
4716 saved_warn_unused
= warn_unused
;
4719 expand_end_bindings (getdecls (), kept_level_p (), 0);
4720 if (processing_template_decl
)
4721 warn_unused
= saved_warn_unused
;
4722 t
= poplevel (kept_level_p (), 1, 0);
4727 /* Like tsubst, but deals with expressions. This function just replaces
4728 template parms; to finish processing the resultant expression, use
4732 tsubst_copy (t
, args
, in_decl
)
4736 enum tree_code code
;
4738 if (t
== NULL_TREE
|| t
== error_mark_node
)
4741 code
= TREE_CODE (t
);
4746 return do_identifier (DECL_NAME (t
), 0);
4750 if (DECL_CONTEXT (t
))
4753 if (TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
)
4754 return lookup_name (DECL_NAME (t
), 0);
4756 ctx
= tsubst (DECL_CONTEXT (t
), args
, in_decl
);
4757 if (ctx
!= DECL_CONTEXT (t
))
4758 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
4764 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4765 t
= tsubst (t
, args
, in_decl
);
4770 if (is_member_template (t
))
4771 return tsubst (t
, args
, in_decl
);
4776 case IDENTIFIER_NODE
:
4777 return do_identifier (t
, 0);
4781 case REINTERPRET_CAST_EXPR
:
4782 case CONST_CAST_EXPR
:
4783 case STATIC_CAST_EXPR
:
4784 case DYNAMIC_CAST_EXPR
:
4786 (code
, tsubst (TREE_TYPE (t
), args
, in_decl
),
4787 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4790 case PREDECREMENT_EXPR
:
4791 case PREINCREMENT_EXPR
:
4792 case POSTDECREMENT_EXPR
:
4793 case POSTINCREMENT_EXPR
:
4795 case TRUTH_NOT_EXPR
:
4798 case CONVERT_EXPR
: /* Unary + */
4806 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4811 case TRUNC_DIV_EXPR
:
4813 case FLOOR_DIV_EXPR
:
4814 case ROUND_DIV_EXPR
:
4815 case EXACT_DIV_EXPR
:
4817 case BIT_ANDTC_EXPR
:
4820 case TRUNC_MOD_EXPR
:
4821 case FLOOR_MOD_EXPR
:
4822 case TRUTH_ANDIF_EXPR
:
4823 case TRUTH_ORIF_EXPR
:
4824 case TRUTH_AND_EXPR
:
4845 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4846 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4850 tree fn
= TREE_OPERAND (t
, 0);
4851 if (is_overloaded_fn (fn
))
4852 fn
= tsubst_copy (get_first_fn (fn
), args
, in_decl
);
4854 /* Sometimes FN is a LOOKUP_EXPR. */
4855 fn
= tsubst_copy (fn
, args
, in_decl
);
4857 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4861 case METHOD_CALL_EXPR
:
4863 tree name
= TREE_OPERAND (t
, 0);
4864 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4866 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4867 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4868 name
= TYPE_MAIN_VARIANT (name
);
4869 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4871 else if (TREE_CODE (name
) == SCOPE_REF
4872 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4874 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4875 name
= TREE_OPERAND (name
, 1);
4876 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4877 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4878 name
= TYPE_MAIN_VARIANT (name
);
4879 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4880 name
= build_nt (SCOPE_REF
, base
, name
);
4883 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
4885 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4886 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
),
4895 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4896 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4897 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4899 if (code
== BIND_EXPR
&& !processing_template_decl
)
4901 /* This processing should really occur in tsubst_expr,
4902 However, tsubst_expr does not recurse into expressions,
4903 since it assumes that there aren't any statements
4904 inside them. Instead, it simply calls
4905 build_expr_from_tree. So, we need to expand the
4907 tree rtl_expr
= begin_stmt_expr ();
4908 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
, in_decl
);
4909 r
= finish_stmt_expr (rtl_expr
, block
);
4918 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4919 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4920 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4921 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4928 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4929 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4930 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4931 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4935 case TEMPLATE_ID_EXPR
:
4937 /* Substituted template arguments */
4938 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
);
4940 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4941 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4943 return lookup_template_function
4944 (tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
), targs
);
4949 tree purpose
, value
, chain
;
4951 if (t
== void_list_node
)
4954 purpose
= TREE_PURPOSE (t
);
4956 purpose
= tsubst_copy (purpose
, args
, in_decl
);
4957 value
= TREE_VALUE (t
);
4959 value
= tsubst_copy (value
, args
, in_decl
);
4960 chain
= TREE_CHAIN (t
);
4961 if (chain
&& chain
!= void_type_node
)
4962 chain
= tsubst_copy (chain
, args
, in_decl
);
4963 if (purpose
== TREE_PURPOSE (t
)
4964 && value
== TREE_VALUE (t
)
4965 && chain
== TREE_CHAIN (t
))
4967 return tree_cons (purpose
, value
, chain
);
4974 case TEMPLATE_TYPE_PARM
:
4975 case TEMPLATE_TEMPLATE_PARM
:
4976 case TEMPLATE_PARM_INDEX
:
4978 case REFERENCE_TYPE
:
4985 return tsubst (t
, args
, in_decl
);
4987 case IDENTIFIER_NODE
:
4988 if (IDENTIFIER_TYPENAME_P (t
))
4989 return build_typename_overload
4990 (tsubst (TREE_TYPE (t
), args
, in_decl
));
4996 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, in_decl
), NULL_TREE
,
4997 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, in_decl
));
5004 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
5007 tsubst_expr (t
, args
, in_decl
)
5011 if (t
== NULL_TREE
|| t
== error_mark_node
)
5014 if (processing_template_decl
)
5015 return tsubst_copy (t
, args
, in_decl
);
5017 switch (TREE_CODE (t
))
5020 lineno
= TREE_COMPLEXITY (t
);
5021 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
5026 lineno
= TREE_COMPLEXITY (t
);
5027 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
5033 int i
= suspend_momentary ();
5036 lineno
= TREE_COMPLEXITY (t
);
5037 emit_line_note (input_filename
, lineno
);
5039 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5040 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
5041 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
5042 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, in_decl
);
5044 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5045 resume_momentary (i
);
5052 lineno
= TREE_COMPLEXITY (t
);
5055 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5056 tsubst_expr (tmp
, args
, in_decl
);
5057 finish_for_init_stmt (NULL_TREE
);
5058 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
5061 tmp
= tsubst_expr (FOR_EXPR (t
), args
, in_decl
);
5062 finish_for_expr (tmp
, NULL_TREE
);
5063 tsubst_expr (FOR_BODY (t
), args
, in_decl
);
5064 finish_for_stmt (tmp
, NULL_TREE
);
5070 lineno
= TREE_COMPLEXITY (t
);
5071 begin_while_stmt ();
5072 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
5075 tsubst_expr (WHILE_BODY (t
), args
, in_decl
);
5076 finish_while_stmt (NULL_TREE
);
5082 lineno
= TREE_COMPLEXITY (t
);
5084 tsubst_expr (DO_BODY (t
), args
, in_decl
);
5085 finish_do_body (NULL_TREE
);
5086 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
5096 lineno
= TREE_COMPLEXITY (t
);
5098 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
5102 if (tmp
= THEN_CLAUSE (t
), tmp
)
5104 tsubst_expr (tmp
, args
, in_decl
);
5105 finish_then_clause (NULL_TREE
);
5108 if (tmp
= ELSE_CLAUSE (t
), tmp
)
5110 begin_else_clause ();
5111 tsubst_expr (tmp
, args
, in_decl
);
5112 finish_else_clause (NULL_TREE
);
5123 lineno
= TREE_COMPLEXITY (t
);
5124 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
5125 for (substmt
= COMPOUND_BODY (t
);
5126 substmt
!= NULL_TREE
;
5127 substmt
= TREE_CHAIN (substmt
))
5128 tsubst_expr (substmt
, args
, in_decl
);
5129 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
5135 lineno
= TREE_COMPLEXITY (t
);
5136 finish_break_stmt ();
5140 lineno
= TREE_COMPLEXITY (t
);
5141 finish_continue_stmt ();
5148 lineno
= TREE_COMPLEXITY (t
);
5149 begin_switch_stmt ();
5150 val
= tsubst_expr (SWITCH_COND (t
), args
, in_decl
);
5151 finish_switch_cond (val
);
5153 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
5154 tsubst_expr (tmp
, args
, in_decl
);
5156 finish_switch_stmt (val
, NULL_TREE
);
5161 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, in_decl
),
5162 tsubst_expr (CASE_HIGH (t
), args
, in_decl
));
5166 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
5173 lineno
= TREE_COMPLEXITY (t
);
5174 t
= GOTO_DESTINATION (t
);
5175 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
5176 /* Computed goto's must be tsubst'd into. On the other hand,
5177 non-computed gotos must not be; the identifier in question
5178 will have no binding. */
5179 t
= tsubst_expr (t
, args
, in_decl
);
5180 finish_goto_stmt (t
);
5184 lineno
= TREE_COMPLEXITY (t
);
5185 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, in_decl
),
5186 tsubst_expr (ASM_STRING (t
), args
, in_decl
),
5187 tsubst_expr (ASM_OUTPUTS (t
), args
, in_decl
),
5188 tsubst_expr (ASM_INPUTS (t
), args
, in_decl
),
5189 tsubst_expr (ASM_CLOBBERS (t
), args
, in_decl
));
5193 lineno
= TREE_COMPLEXITY (t
);
5195 tsubst_expr (TRY_STMTS (t
), args
, in_decl
);
5196 finish_try_block (NULL_TREE
);
5198 tree handler
= TRY_HANDLERS (t
);
5199 for (; handler
; handler
= TREE_CHAIN (handler
))
5200 tsubst_expr (handler
, args
, in_decl
);
5202 finish_handler_sequence (NULL_TREE
);
5206 lineno
= TREE_COMPLEXITY (t
);
5208 if (HANDLER_PARMS (t
))
5210 tree d
= HANDLER_PARMS (t
);
5211 expand_start_catch_block
5212 (tsubst (TREE_OPERAND (d
, 1), args
, in_decl
),
5213 tsubst (TREE_OPERAND (d
, 0), args
, in_decl
));
5216 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
5217 finish_handler_parms (NULL_TREE
);
5218 tsubst_expr (HANDLER_BODY (t
), args
, in_decl
);
5219 finish_handler (NULL_TREE
);
5223 lineno
= TREE_COMPLEXITY (t
);
5225 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
5226 tsubst_enum (t
, args
, NULL
);
5230 return build_expr_from_tree (tsubst_copy (t
, args
, in_decl
));
5236 instantiate_template (tmpl
, targ_ptr
)
5237 tree tmpl
, targ_ptr
;
5241 struct obstack
*old_fmp_obstack
;
5242 extern struct obstack
*function_maybepermanent_obstack
;
5244 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
5246 /* FIXME this won't work with member templates; we only have one level
5248 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
5250 /* Check to see if we already have this specialization. */
5251 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
5253 if (spec
!= NULL_TREE
)
5257 push_obstacks (&permanent_obstack
, &permanent_obstack
);
5258 old_fmp_obstack
= function_maybepermanent_obstack
;
5259 function_maybepermanent_obstack
= &permanent_obstack
;
5261 len
= DECL_NTPARMS (tmpl
);
5266 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
5267 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
5269 tree nt
= target_type (t
);
5270 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
5272 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
5273 cp_error (" trying to instantiate `%D'", tmpl
);
5274 fndecl
= error_mark_node
;
5278 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
5280 targ_ptr
= copy_to_permanent (targ_ptr
);
5282 /* substitute template parameters */
5283 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, tmpl
);
5285 if (flag_external_templates
)
5286 add_pending_template (fndecl
);
5289 function_maybepermanent_obstack
= old_fmp_obstack
;
5295 /* Push the name of the class template into the scope of the instantiation. */
5298 overload_template_name (type
)
5301 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
5304 if (IDENTIFIER_CLASS_VALUE (id
)
5305 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
5308 decl
= build_decl (TYPE_DECL
, id
, type
);
5309 SET_DECL_ARTIFICIAL (decl
);
5310 pushdecl_class_level (decl
);
5314 /* Like type_unification but designed specially to handle conversion
5315 operators. The EXTRA_FN_ARG, if any, is the type of an additional
5316 parameter to be added to the beginning of FN's parameter list. */
5319 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
5320 strict
, extra_fn_arg
)
5321 tree fn
, explicit_targs
, targs
, args
, return_type
;
5326 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5327 tree decl_arg_types
= args
;
5329 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
5331 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
5333 /* This is a template conversion operator. Use the return types
5334 as well as the argument types. */
5335 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
5336 TREE_TYPE (TREE_TYPE (fn
)),
5338 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
5343 if (extra_fn_arg
!= NULL_TREE
)
5344 fn_arg_types
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
,
5347 /* We allow incomplete unification without an error message here
5348 because the standard doesn't seem to explicitly prohibit it. Our
5349 callers must be ready to deal with unification failures in any
5351 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
5362 /* Type unification.
5364 We have a function template signature with one or more references to
5365 template parameters, and a parameter list we wish to fit to this
5366 template. If possible, produce a list of parameters for the template
5367 which will cause it to fit the supplied parameter list.
5369 Return zero for success, 2 for an incomplete match that doesn't resolve
5370 all the types, and 1 for complete failure. An error message will be
5371 printed only for an incomplete match.
5373 TPARMS[NTPARMS] is an array of template parameter types;
5374 TARGS[NTPARMS] is the array of template parameter values. PARMS is
5375 the function template's signature (using TEMPLATE_PARM_IDX nodes),
5376 and ARGS is the argument list we're trying to match against it.
5378 If SUBR is 1, we're being called recursively (to unify the arguments of
5379 a function or method parameter of a function template), so don't zero
5380 out targs and don't fail on an incomplete match.
5382 If STRICT is 1, the match must be exact (for casts of overloaded
5383 addresses, explicit instantiation, and more_specialized). */
5386 type_unification (tparms
, targs
, parms
, args
, targs_in
,
5387 strict
, allow_incomplete
)
5388 tree tparms
, targs
, parms
, args
, targs_in
;
5389 int strict
, allow_incomplete
;
5391 int ntparms
= TREE_VEC_LENGTH (tparms
);
5397 for (i
= 0; i
< ntparms
; i
++)
5398 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
5400 if (targs_in
!= NULL_TREE
)
5403 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
5406 if (arg_vec
== error_mark_node
)
5409 explicit_mask
= alloca (sizeof (int) * TREE_VEC_LENGTH (targs
));
5410 bzero (explicit_mask
, sizeof(int) * TREE_VEC_LENGTH (targs
));
5413 i
< TREE_VEC_LENGTH (arg_vec
)
5414 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
5417 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (arg_vec
, i
);
5418 /* Let unify know that this argument was explicit. */
5419 explicit_mask
[i
] = 1;
5425 r
= type_unification_real (tparms
, targs
, parms
, args
, 0,
5426 strict
, allow_incomplete
, explicit_mask
);
5431 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
5432 integers, with ones in positions corresponding to arguments in
5433 targs that were provided explicitly, and zeros elsewhere. */
5436 type_unification_real (tparms
, targs
, parms
, args
, subr
,
5437 strict
, allow_incomplete
, explicit_mask
)
5438 tree tparms
, targs
, parms
, args
;
5439 int subr
, strict
, allow_incomplete
;
5444 int ntparms
= TREE_VEC_LENGTH (tparms
);
5446 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
5447 my_friendly_assert (parms
== NULL_TREE
5448 || TREE_CODE (parms
) == TREE_LIST
, 290);
5449 /* ARGS could be NULL (via a call from parse.y to
5450 build_x_function_call). */
5452 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
5453 my_friendly_assert (ntparms
> 0, 292);
5456 && parms
!= void_list_node
5458 && args
!= void_list_node
)
5460 parm
= TREE_VALUE (parms
);
5461 parms
= TREE_CHAIN (parms
);
5462 arg
= TREE_VALUE (args
);
5463 args
= TREE_CHAIN (args
);
5465 if (arg
== error_mark_node
)
5467 if (arg
== unknown_type_node
)
5470 /* Conversions will be performed on a function argument that
5471 corresponds with a function parameter that contains only
5472 non-deducible template parameters and explicitly specified
5473 template parameters. */
5474 if (! uses_template_parms (parm
))
5478 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5479 type
= TREE_TYPE (arg
);
5488 if (comptypes (parm
, type
, 1))
5492 /* It might work; we shouldn't check now, because we might
5493 get into infinite recursion. Overload resolution will
5501 if (TREE_CODE (arg
) == VAR_DECL
)
5502 arg
= TREE_TYPE (arg
);
5503 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
5504 arg
= TREE_TYPE (arg
);
5506 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5508 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
5509 if (TREE_CODE (arg
) == TREE_LIST
5510 && TREE_TYPE (arg
) == unknown_type_node
5511 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
5516 /* Have to back unify here */
5517 arg
= TREE_VALUE (arg
);
5518 ntparms
= DECL_NTPARMS (arg
);
5519 targs
= make_scratch_vec (ntparms
);
5520 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
5522 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
5524 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
5525 parm
, NULL_TREE
, strict
,
5528 arg
= TREE_TYPE (arg
);
5531 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
5533 warning ("using type void* for NULL");
5534 arg
= ptr_type_node
;
5537 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
5538 arg
= TREE_TYPE (arg
);
5540 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
5542 if (TREE_CODE (arg
) == FUNCTION_TYPE
5543 || TREE_CODE (arg
) == METHOD_TYPE
)
5544 arg
= build_pointer_type (arg
);
5545 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
5546 arg
= build_pointer_type (TREE_TYPE (arg
));
5548 arg
= TYPE_MAIN_VARIANT (arg
);
5551 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, strict
,
5560 /* Fail if we've reached the end of the parm list, and more args
5561 are present, and the parm list isn't variadic. */
5562 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
5564 /* Fail if parms are left and they don't have default values. */
5566 && parms
!= void_list_node
5567 && TREE_PURPOSE (parms
) == NULL_TREE
)
5570 for (i
= 0; i
< ntparms
; i
++)
5571 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
5573 if (!allow_incomplete
)
5574 error ("incomplete type unification");
5580 /* Returns the level of DECL, which declares a template parameter. */
5583 template_decl_level (decl
)
5586 switch (TREE_CODE (decl
))
5590 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
5593 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
5596 my_friendly_abort (0);
5601 /* Tail recursion is your friend. */
5604 unify (tparms
, targs
, ntparms
, parm
, arg
, strict
, explicit_mask
)
5605 tree tparms
, targs
, parm
, arg
;
5606 int ntparms
, strict
;
5613 /* I don't think this will do the right thing with respect to types.
5614 But the only case I've seen it in so far has been array bounds, where
5615 signedness is the only information lost, and I think that will be
5617 while (TREE_CODE (parm
) == NOP_EXPR
)
5618 parm
= TREE_OPERAND (parm
, 0);
5620 if (arg
== error_mark_node
)
5622 if (arg
== unknown_type_node
)
5624 /* If PARM uses template parameters, then we can't bail out here,
5625 even in ARG == PARM, since we won't record unifications for the
5626 template parameters. We might need them if we're trying to
5627 figure out which of two things is more specialized. */
5628 if (arg
== parm
&& !uses_template_parms (parm
))
5631 /* We can't remove cv-quals when strict. */
5632 if (strict
&& TREE_CODE (arg
) == TREE_CODE (parm
)
5633 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
5634 && (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5635 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5638 switch (TREE_CODE (parm
))
5641 /* In a type which contains a nested-name-specifier, template
5642 argument values cannot be deduced for template parameters used
5643 within the nested-name-specifier. */
5646 case TEMPLATE_TYPE_PARM
:
5647 case TEMPLATE_TEMPLATE_PARM
:
5648 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5650 if (TEMPLATE_TYPE_LEVEL (parm
)
5651 != template_decl_level (tparm
))
5652 /* The PARM is not one we're trying to unify. Just check
5653 to see if it matches ARG. */
5654 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5655 && comptypes (parm
, arg
, 1)) ? 0 : 1;
5656 idx
= TEMPLATE_TYPE_IDX (parm
);
5657 targ
= TREE_VEC_ELT (targs
, idx
);
5658 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
5660 /* Check for mixed types and values. */
5661 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5662 && TREE_CODE (tparm
) != TYPE_DECL
)
5663 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
5664 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
5667 if (!strict
&& targ
!= NULL_TREE
5668 && explicit_mask
&& explicit_mask
[idx
])
5669 /* An explicit template argument. Don't even try to match
5670 here; the overload resolution code will manage check to
5671 see whether the call is legal. */
5674 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5676 if (CLASSTYPE_TEMPLATE_INFO (parm
))
5678 /* We arrive here when PARM does not involve template
5681 /* ARG must be constructed from a template class. */
5682 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
5686 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
5687 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
5688 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
5690 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
5693 /* The parameter and argument roles have to be switched here
5694 in order to handle default arguments properly. For example,
5695 template<template <class> class TT> void f(TT<int>)
5696 should be able to accept vector<int> which comes from
5697 template <class T, class Allcator = allocator>
5700 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1, 0)
5704 /* Deduce arguments T, i from TT<T> or TT<i>. */
5705 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
5707 tree t
= TREE_VEC_ELT (parmvec
, i
);
5708 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
5709 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
5710 && TREE_CODE (t
) != TEMPLATE_PARM_INDEX
)
5713 /* This argument can be deduced. */
5715 if (unify (tparms
, targs
, ntparms
, t
,
5716 TREE_VEC_ELT (argvec
, i
), strict
, explicit_mask
))
5720 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
5725 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5726 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5730 /* Template type parameters cannot contain cv-quals; i.e.
5731 template <class T> void f (T& a, T& b) will not generate
5732 void f (const int& a, const int& b). */
5733 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
5734 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
5736 arg
= TYPE_MAIN_VARIANT (arg
);
5739 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
5740 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
5741 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
5746 /* Simple cases: Value already set, does match or doesn't. */
5747 if (targ
!= NULL_TREE
5748 && (comptypes (targ
, arg
, 1)
5749 || (explicit_mask
&& explicit_mask
[idx
])))
5753 TREE_VEC_ELT (targs
, idx
) = arg
;
5756 case TEMPLATE_PARM_INDEX
:
5757 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5759 if (TEMPLATE_PARM_LEVEL (parm
)
5760 != template_decl_level (tparm
))
5761 /* The PARM is not one we're trying to unify. Just check
5762 to see if it matches ARG. */
5763 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5764 && cp_tree_equal (parm
, arg
)) ? 0 : 1;
5766 idx
= TEMPLATE_PARM_IDX (parm
);
5767 targ
= TREE_VEC_ELT (targs
, idx
);
5771 int i
= cp_tree_equal (targ
, arg
);
5777 my_friendly_abort (42);
5780 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
5784 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5785 return unify (tparms
, targs
, ntparms
, parm
,
5786 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
, explicit_mask
);
5788 if (TREE_CODE (arg
) != POINTER_TYPE
)
5790 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5791 strict
, explicit_mask
);
5793 case REFERENCE_TYPE
:
5794 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5795 arg
= TREE_TYPE (arg
);
5796 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5797 strict
, explicit_mask
);
5800 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5802 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5803 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5805 if (TYPE_DOMAIN (parm
) != NULL_TREE
5806 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5807 TYPE_DOMAIN (arg
), strict
, explicit_mask
) != 0)
5809 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5810 strict
, explicit_mask
);
5817 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5820 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5822 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5823 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5824 TYPE_MIN_VALUE (arg
), strict
, explicit_mask
))
5826 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5827 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5828 TYPE_MAX_VALUE (arg
), strict
, explicit_mask
))
5831 else if (TREE_CODE (parm
) == REAL_TYPE
5832 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5835 /* As far as unification is concerned, this wins. Later checks
5836 will invalidate it if necessary. */
5839 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5840 /* Type INTEGER_CST can come from ordinary constant template args. */
5842 while (TREE_CODE (arg
) == NOP_EXPR
)
5843 arg
= TREE_OPERAND (arg
, 0);
5845 if (TREE_CODE (arg
) != INTEGER_CST
)
5847 return !tree_int_cst_equal (parm
, arg
);
5852 if (TREE_CODE (arg
) != TREE_VEC
)
5854 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5856 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5857 if (unify (tparms
, targs
, ntparms
,
5858 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5859 strict
, explicit_mask
))
5865 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5866 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5867 arg
, strict
, explicit_mask
);
5869 /* Allow trivial conversions. */
5870 if (TREE_CODE (arg
) != RECORD_TYPE
5871 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5872 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5875 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5879 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5881 (CLASSTYPE_TEMPLATE_INFO (arg
)
5882 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5884 if (! t
|| t
== error_mark_node
)
5887 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5888 CLASSTYPE_TI_ARGS (t
), strict
, explicit_mask
);
5890 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5895 if (TREE_CODE (arg
) != METHOD_TYPE
)
5900 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5903 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5904 TREE_TYPE (arg
), strict
, explicit_mask
))
5906 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5907 TYPE_ARG_TYPES (arg
), 1,
5908 strict
, 0, explicit_mask
);
5911 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5913 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5914 TYPE_OFFSET_BASETYPE (arg
), strict
, explicit_mask
))
5916 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5917 TREE_TYPE (arg
), strict
, explicit_mask
);
5920 if (arg
!= decl_constant_value (parm
))
5925 /* Matched cases are handled by the ARG == PARM test above. */
5929 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
5931 /* We're looking at an expression. This can happen with
5935 void foo(S<I>, S<I + 2>);
5937 If the call looked like:
5939 foo(S<2>(), S<4>());
5941 we would have already matched `I' with `2'. Now, we'd
5942 like to know if `4' matches `I + 2'. So, we substitute
5943 into that expression, and fold constants, in the hope of
5946 maybe_fold_nontype_arg (tsubst_expr (parm
, targs
, NULL_TREE
));
5947 enum tree_code tc
= TREE_CODE (t
);
5949 if (tc
== MINUS_EXPR
5950 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
5951 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
5953 /* We handle this case specially, since it comes up with
5954 arrays. In particular, something like:
5956 template <int N> void f(int (&x)[N]);
5958 Here, we are trying to unify the range type, which
5959 looks like [0 ... (N - 1)]. */
5961 t1
= TREE_OPERAND (parm
, 0);
5962 t2
= TREE_OPERAND (parm
, 1);
5964 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
5968 return unify (tparms
, targs
, ntparms
, t1
, t
,
5969 strict
, explicit_mask
);
5972 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc
)))
5973 /* Good, we mangaged to simplify the exression. */
5974 return unify (tparms
, targs
, ntparms
, t
, arg
, strict
,
5977 /* Bad, we couldn't simplify this. Assume it doesn't
5982 sorry ("use of `%s' in template type unification",
5983 tree_code_name
[(int) TREE_CODE (parm
)]);
5990 mark_decl_instantiated (result
, extern_p
)
5994 if (DECL_TEMPLATE_INSTANTIATION (result
))
5995 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5997 if (TREE_CODE (result
) != FUNCTION_DECL
)
5998 /* The TREE_PUBLIC flag for function declarations will have been
5999 set correctly by tsubst. */
6000 TREE_PUBLIC (result
) = 1;
6004 DECL_INTERFACE_KNOWN (result
) = 1;
6005 DECL_NOT_REALLY_EXTERN (result
) = 1;
6007 /* For WIN32 we also want to put explicit instantiations in
6008 linkonce sections. */
6009 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
6010 make_decl_one_only (result
);
6012 else if (TREE_CODE (result
) == FUNCTION_DECL
)
6013 mark_inline_for_output (result
);
6016 /* Given two function templates PAT1 and PAT2, and explicit template
6017 arguments EXPLICIT_ARGS return:
6019 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
6020 -1 if PAT2 is more specialized than PAT1.
6021 0 if neither is more specialized. */
6024 more_specialized (pat1
, pat2
, explicit_args
)
6025 tree pat1
, pat2
, explicit_args
;
6030 targs
= get_bindings_overload (pat1
, pat2
, explicit_args
);
6036 targs
= get_bindings_overload (pat2
, pat1
, explicit_args
);
6045 /* Given two class template specialization list nodes PAT1 and PAT2, return:
6047 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
6048 -1 if PAT2 is more specialized than PAT1.
6049 0 if neither is more specialized. */
6052 more_specialized_class (pat1
, pat2
)
6058 targs
= get_class_bindings
6059 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
6060 TREE_PURPOSE (pat2
), NULL_TREE
);
6064 targs
= get_class_bindings
6065 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
6066 TREE_PURPOSE (pat1
), NULL_TREE
);
6073 /* Return the template arguments that will produce the function signature
6074 DECL from the function template FN, with the explicit template
6075 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
6079 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
6080 tree fn
, decl
, explicit_args
;
6083 int ntparms
= DECL_NTPARMS (fn
);
6084 tree targs
= make_scratch_vec (ntparms
);
6085 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
6086 tree extra_fn_arg
= NULL_TREE
;
6089 if (DECL_STATIC_FUNCTION_P (fn
)
6090 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
6092 /* Sometimes we are trying to figure out what's being
6093 specialized by a declaration that looks like a method, and it
6094 turns out to be a static member function. */
6095 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
6096 && !is_member_template (fn
))
6097 /* The natural thing to do here seems to be to remove the
6098 spurious `this' parameter from the DECL, but that prevents
6099 unification from making use of the class type. So,
6100 instead, we have fn_type_unification add to the parameters
6102 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
6104 /* In this case, though, adding the extra_fn_arg can confuse
6105 things, so we remove from decl_arg_types instead. */
6106 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
6109 i
= fn_type_unification (fn
, explicit_args
, targs
,
6111 TREE_TYPE (TREE_TYPE (decl
)),
6120 /* Check to see that the resulting return type is also OK. */
6121 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
6122 complete_template_args (fn
, targs
, 1),
6125 if (!comptypes (t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
6132 /* For most uses, we want to check the return type. */
6135 get_bindings (fn
, decl
, explicit_args
)
6136 tree fn
, decl
, explicit_args
;
6138 return get_bindings_real (fn
, decl
, explicit_args
, 1);
6141 /* But for more_specialized, we only care about the parameter types. */
6144 get_bindings_overload (fn
, decl
, explicit_args
)
6145 tree fn
, decl
, explicit_args
;
6147 return get_bindings_real (fn
, decl
, explicit_args
, 0);
6151 get_class_bindings (tparms
, parms
, args
, outer_args
)
6152 tree tparms
, parms
, args
, outer_args
;
6154 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6155 tree vec
= make_temp_vec (ntparms
);
6159 tparms
= tsubst (tparms
, outer_args
, NULL_TREE
);
6160 parms
= tsubst (parms
, outer_args
, NULL_TREE
);
6163 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
6165 switch (unify (tparms
, vec
, ntparms
,
6166 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
6176 for (i
= 0; i
< ntparms
; ++i
)
6177 if (! TREE_VEC_ELT (vec
, i
))
6183 /* Return the most specialized of the list of templates in FNS that can
6184 produce an instantiation matching DECL, given the explicit template
6185 arguments EXPLICIT_ARGS. */
6188 most_specialized (fns
, decl
, explicit_args
)
6189 tree fns
, decl
, explicit_args
;
6191 tree fn
, champ
, args
, *p
;
6194 for (p
= &fns
; *p
; )
6196 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
6199 p
= &TREE_CHAIN (*p
);
6202 *p
= TREE_CHAIN (*p
);
6209 champ
= TREE_VALUE (fn
);
6210 fn
= TREE_CHAIN (fn
);
6211 for (; fn
; fn
= TREE_CHAIN (fn
))
6213 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6220 fn
= TREE_CHAIN (fn
);
6222 return error_mark_node
;
6224 champ
= TREE_VALUE (fn
);
6228 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
6230 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6232 return error_mark_node
;
6238 /* Return the most specialized of the class template specializations in
6239 SPECS that can produce an instantiation matching ARGS. */
6242 most_specialized_class (specs
, mainargs
, outer_args
)
6243 tree specs
, mainargs
, outer_args
;
6245 tree list
= NULL_TREE
, t
, args
, champ
;
6248 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
6250 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
6251 mainargs
, outer_args
);
6254 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
6255 TREE_TYPE (list
) = TREE_TYPE (t
);
6265 for (; t
; t
= TREE_CHAIN (t
))
6267 fate
= more_specialized_class (champ
, t
);
6276 return error_mark_node
;
6282 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
6284 fate
= more_specialized_class (champ
, t
);
6286 return error_mark_node
;
6292 /* called from the parser. */
6295 do_decl_instantiation (declspecs
, declarator
, storage
)
6296 tree declspecs
, declarator
, storage
;
6298 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
6299 tree result
= NULL_TREE
;
6302 if (! DECL_LANG_SPECIFIC (decl
))
6304 cp_error ("explicit instantiation of non-template `%#D'", decl
);
6308 /* If we've already seen this template instance, use it. */
6309 if (TREE_CODE (decl
) == VAR_DECL
)
6311 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
6312 if (result
&& TREE_CODE (result
) != VAR_DECL
)
6315 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
6317 cp_error ("explicit instantiation of `%#D'", decl
);
6320 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
6325 cp_error ("no matching template for `%D' found", decl
);
6329 if (! DECL_TEMPLATE_INFO (result
))
6331 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
6335 if (flag_external_templates
)
6338 if (storage
== NULL_TREE
)
6340 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6343 cp_error ("storage class `%D' applied to template instantiation",
6346 mark_decl_instantiated (result
, extern_p
);
6347 repo_template_instantiated (result
, extern_p
);
6349 instantiate_decl (result
);
6353 mark_class_instantiated (t
, extern_p
)
6357 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
6358 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
6359 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
6360 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
6361 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
6364 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
6365 rest_of_type_compilation (t
, 1);
6370 do_type_instantiation (t
, storage
)
6377 if (TREE_CODE (t
) == TYPE_DECL
)
6380 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
6382 cp_error ("explicit instantiation of non-template type `%T'", t
);
6388 /* With -fexternal-templates, explicit instantiations are treated the same
6389 as implicit ones. */
6390 if (flag_external_templates
)
6393 if (TYPE_SIZE (t
) == NULL_TREE
)
6395 cp_error ("explicit instantiation of `%#T' before definition of template",
6400 if (storage
== NULL_TREE
)
6402 else if (storage
== ridpointers
[(int) RID_INLINE
])
6404 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6406 else if (storage
== ridpointers
[(int) RID_STATIC
])
6410 cp_error ("storage class `%D' applied to template instantiation",
6415 /* We've already instantiated this. */
6416 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
6420 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
6422 mark_class_instantiated (t
, extern_p
);
6423 repo_template_instantiated (t
, extern_p
);
6433 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6434 if (TREE_CODE (tmp
) == FUNCTION_DECL
6435 && DECL_TEMPLATE_INSTANTIATION (tmp
))
6437 mark_decl_instantiated (tmp
, extern_p
);
6438 repo_template_instantiated (tmp
, extern_p
);
6440 instantiate_decl (tmp
);
6443 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6444 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
6446 mark_decl_instantiated (tmp
, extern_p
);
6447 repo_template_instantiated (tmp
, extern_p
);
6449 instantiate_decl (tmp
);
6452 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6453 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
6454 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
6458 /* Produce the definition of D, a _DECL generated from a template. */
6461 instantiate_decl (d
)
6464 tree ti
= DECL_TEMPLATE_INFO (d
);
6465 tree tmpl
= TI_TEMPLATE (ti
);
6466 tree args
= TI_ARGS (ti
);
6468 tree decl_pattern
, code_pattern
;
6470 int nested
= in_function_p ();
6472 int pattern_defined
;
6474 char *file
= input_filename
;
6477 DECL_TEMPLATE_INSTANTIATION (td
)
6478 /* This next clause handles friend templates defined inside
6479 class templates. The friend templates are not really
6480 instantiations from the point of view of the language, but
6481 they are instantiations from the point of view of the
6483 || (DECL_TEMPLATE_INFO (td
) && !DECL_TEMPLATE_SPECIALIZATION (td
));
6485 td
= DECL_TI_TEMPLATE (td
);
6487 /* In the case of a member template, decl_pattern is the partially
6488 instantiated declaration (in the instantiated class), and code_pattern
6489 is the original template definition. */
6490 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
6491 code_pattern
= DECL_TEMPLATE_RESULT (td
);
6493 if (TREE_CODE (d
) == FUNCTION_DECL
)
6495 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
6496 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
6500 d_defined
= ! DECL_IN_AGGR_P (d
);
6501 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
6507 if (TREE_CODE (d
) == FUNCTION_DECL
)
6509 tree spec
= retrieve_specialization (tmpl
, args
);
6511 if (spec
!= NULL_TREE
6512 && DECL_TEMPLATE_SPECIALIZATION (spec
))
6516 /* This needs to happen before any tsubsting. */
6517 if (! push_tinst_level (d
))
6520 push_to_top_level ();
6521 lineno
= DECL_SOURCE_LINE (d
);
6522 input_filename
= DECL_SOURCE_FILE (d
);
6524 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
6525 variable is a static const initialized in the class body. */
6526 if (TREE_CODE (d
) == VAR_DECL
6527 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
6529 pushclass (DECL_CONTEXT (d
), 2);
6530 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6532 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, LOOKUP_NORMAL
);
6535 if (pattern_defined
)
6537 repo_template_used (d
);
6539 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
6541 if (flag_alt_external_templates
)
6543 if (interface_unknown
)
6544 warn_if_unknown_interface (d
);
6546 else if (DECL_INTERFACE_KNOWN (code_pattern
))
6548 DECL_INTERFACE_KNOWN (d
) = 1;
6549 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
6552 warn_if_unknown_interface (code_pattern
);
6556 import_export_decl (d
);
6559 /* Reject all external templates except inline functions. */
6560 if (DECL_INTERFACE_KNOWN (d
)
6561 && ! DECL_NOT_REALLY_EXTERN (d
)
6562 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
6565 /* Defer all templates except inline functions used in another function. */
6566 if (! pattern_defined
6567 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
6570 add_pending_template (d
);
6574 lineno
= DECL_SOURCE_LINE (d
);
6575 input_filename
= DECL_SOURCE_FILE (d
);
6577 /* Trick tsubst into giving us a new decl in case the template changed. */
6578 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
6579 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
6580 /* decl_pattern has all but one level of template parms bound. Only pass
6581 in that one level of args. */
6582 temp
= innermost_args (args
, DECL_TEMPLATE_SPECIALIZATION (decl_pattern
));
6583 td
= tsubst (decl_pattern
, temp
, tmpl
);
6584 SET_DECL_IMPLICIT_INSTANTIATION (td
);
6585 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
6587 /* And set up DECL_INITIAL, since tsubst doesn't. */
6588 if (TREE_CODE (td
) == VAR_DECL
)
6590 pushclass (DECL_CONTEXT (d
), 2);
6591 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6596 if (TREE_CODE (d
) == FUNCTION_DECL
)
6598 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
6600 DECL_INITIAL (td
) = error_mark_node
;
6602 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
6603 /* Set up the information about what is being specialized. */
6604 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
6606 duplicate_decls (td
, d
);
6607 if (TREE_CODE (d
) == FUNCTION_DECL
)
6608 DECL_INITIAL (td
) = 0;
6610 if (TREE_CODE (d
) == VAR_DECL
)
6612 DECL_IN_AGGR_P (d
) = 0;
6613 if (DECL_INTERFACE_KNOWN (d
))
6614 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
6617 DECL_EXTERNAL (d
) = 1;
6618 DECL_NOT_REALLY_EXTERN (d
) = 1;
6620 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
6622 else if (TREE_CODE (d
) == FUNCTION_DECL
)
6624 tree t
= DECL_SAVED_TREE (code_pattern
);
6626 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
6627 store_parm_decls ();
6629 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
6632 (TREE_OPERAND (t
, 0),
6633 tsubst_expr (TREE_OPERAND (t
, 1), args
, tmpl
));
6637 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
6639 current_member_init_list
6640 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
6641 current_base_init_list
6642 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
6647 /* Always keep the BLOCK node associated with the outermost
6648 pair of curly braces of a function. These are needed
6649 for correct operation of dwarfout.c. */
6652 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
6653 tsubst_expr (t
, args
, tmpl
);
6655 finish_function (lineno
, 0, nested
);
6660 input_filename
= file
;
6662 pop_from_top_level ();
6669 tsubst_chain (t
, argvec
)
6674 tree first
= tsubst (t
, argvec
, NULL_TREE
);
6677 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
6679 tree x
= tsubst (t
, argvec
, NULL_TREE
);
6680 TREE_CHAIN (last
) = x
;
6690 tsubst_expr_values (t
, argvec
)
6693 tree first
= NULL_TREE
;
6696 for (; t
; t
= TREE_CHAIN (t
))
6698 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
, NULL_TREE
);
6699 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, NULL_TREE
);
6700 *p
= build_tree_list (pur
, val
);
6701 p
= &TREE_CHAIN (*p
);
6712 last_tree
= TREE_CHAIN (last_tree
) = t
;
6719 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
6720 last_tree
= NULL_TREE
;
6727 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
6729 last_tree
= TREE_VALUE (saved_trees
);
6730 saved_trees
= TREE_CHAIN (saved_trees
);
6733 /* D is an undefined function declaration in the presence of templates with
6734 the same name, listed in FNS. If one of them can produce D as an
6735 instantiation, remember this so we can instantiate it at EOF if D has
6736 not been defined by that time. */
6739 add_maybe_template (d
, fns
)
6744 if (DECL_MAYBE_TEMPLATE (d
))
6747 t
= most_specialized (fns
, d
, NULL_TREE
);
6750 if (t
== error_mark_node
)
6752 cp_error ("ambiguous template instantiation for `%D'", d
);
6756 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
6757 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
6758 DECL_MAYBE_TEMPLATE (d
) = 1;
6761 /* Instantiate an enumerated type. Used by instantiate_class_template and
6765 tsubst_enum (tag
, args
, field_chain
)
6769 extern tree current_local_enum
;
6770 tree prev_local_enum
= current_local_enum
;
6772 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
6773 tree e
, values
= NULL_TREE
;
6775 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
6777 tree elt
= build_enumerator (TREE_PURPOSE (e
),
6778 tsubst_expr (TREE_VALUE (e
), args
,
6780 TREE_CHAIN (elt
) = values
;
6784 finish_enum (newtag
, values
);
6786 if (NULL
!= field_chain
)
6787 *field_chain
= grok_enum_decls (NULL_TREE
);
6789 current_local_enum
= prev_local_enum
;