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 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1047 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
1049 if (fns
== NULL_TREE
)
1051 cp_error ("no member function `%s' declared in `%T'",
1052 IDENTIFIER_POINTER (name
),
1057 TREE_OPERAND (declarator
, 0) = fns
;
1060 /* Figure out what exactly is being specialized at this point.
1061 Note that for an explicit instantiation, even one for a
1062 member function, we cannot tell apriori whether the the
1063 instantiation is for a member template, or just a member
1064 function of a template class. In particular, even in if the
1065 instantiation is for a member template, the template
1066 arguments could be deduced from the declaration. */
1067 tmpl
= determine_specialization (declarator
, decl
,
1069 member_specialization
,
1074 if (explicit_instantiation
)
1076 decl
= instantiate_template (tmpl
, targs
);
1077 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
1078 /* There doesn't seem to be anything in the draft to
1079 prevent a specialization from being explicitly
1080 instantiated. We're careful not to destroy the
1081 information indicating that this is a
1082 specialization here. */
1083 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
1086 else if (DECL_STATIC_FUNCTION_P (tmpl
)
1087 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1089 revert_static_member_fn (&decl
, 0, 0);
1090 last_function_parms
= TREE_CHAIN (last_function_parms
);
1093 /* Mangle the function name appropriately. Note that we do
1094 not mangle specializations of non-template member
1095 functions of template classes, e.g. with
1096 template <class T> struct S { void f(); }
1097 and given the specialization
1098 template <> void S<int>::f() {}
1099 we do not mangle S<int>::f() here. That's because it's
1100 just an ordinary member function and doesn't need special
1102 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1103 && name_mangling_version
>= 1)
1105 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
1108 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
1110 hash_tree_chain (build_pointer_type (ctype
),
1113 DECL_ASSEMBLER_NAME (decl
)
1114 = build_template_decl_overload
1117 TREE_TYPE (TREE_TYPE (tmpl
)),
1118 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
1119 targs
, ctype
!= NULL_TREE
);
1122 if (is_friend
&& !have_def
)
1124 /* This is not really a declaration of a specialization.
1125 It's just the name of an instantiation. But, it's not
1126 a request for an instantiation, either. */
1127 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1128 DECL_TEMPLATE_INFO (decl
)
1129 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1133 /* If DECL_TI_TEMPLATE (decl), the decl is an
1134 instantiation of a specialization of a member template.
1135 (In other words, there was a member template, in a
1136 class template. That member template was specialized.
1137 We then instantiated the class, so there is now an
1138 instance of that specialization.)
1140 According to the CD2,
1142 14.7.3.13 [tmpl.expl.spec]
1144 A specialization of a member function template or
1145 member class template of a non-specialized class
1146 template is itself a template.
1148 So, we just leave the template info alone in this case. */
1149 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
1150 DECL_TEMPLATE_INFO (decl
)
1151 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1153 register_specialization (decl
, tmpl
, targs
);
1163 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1164 parameters. These are represented in the same format used for
1165 DECL_TEMPLATE_PARMS. */
1167 int comp_template_parms (parms1
, parms2
)
1174 if (parms1
== parms2
)
1177 for (p1
= parms1
, p2
= parms2
;
1178 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1179 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1181 tree t1
= TREE_VALUE (p1
);
1182 tree t2
= TREE_VALUE (p2
);
1185 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1186 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1188 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1191 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1193 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1194 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1196 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1199 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1201 else if (!comptypes (TREE_TYPE (parm1
),
1202 TREE_TYPE (parm2
), 1))
1207 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1208 /* One set of parameters has more parameters lists than the
1216 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1217 ORIG_LEVEL, DECL, and TYPE. */
1220 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1227 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1228 TEMPLATE_PARM_IDX (t
) = index
;
1229 TEMPLATE_PARM_LEVEL (t
) = level
;
1230 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1231 TEMPLATE_PARM_DECL (t
) = decl
;
1232 TREE_TYPE (t
) = type
;
1238 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1239 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1240 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1241 new one is created. */
1244 reduce_template_parm_level (index
, type
, levels
)
1249 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1250 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1251 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1254 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1255 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1258 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1259 TEMPLATE_PARM_LEVEL (index
) - levels
,
1260 TEMPLATE_PARM_ORIG_LEVEL (index
),
1262 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1264 /* Template template parameters need this. */
1265 DECL_TEMPLATE_PARMS (decl
)
1266 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1269 return TEMPLATE_PARM_DESCENDANTS (index
);
1272 /* Process information from new template parameter NEXT and append it to the
1273 LIST being built. */
1276 process_template_parm (list
, next
)
1285 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1286 defval
= TREE_PURPOSE (parm
);
1287 parm
= TREE_VALUE (parm
);
1288 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1292 tree p
= TREE_VALUE (tree_last (list
));
1294 if (TREE_CODE (p
) == TYPE_DECL
)
1295 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1296 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1297 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1299 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1307 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1308 /* is a const-param */
1309 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1310 PARM
, 0, NULL_TREE
);
1311 /* A template parameter is not modifiable. */
1312 TREE_READONLY (parm
) = 1;
1313 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1314 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1316 cp_error ("`%#T' is not a valid type for a template constant parameter",
1318 if (DECL_NAME (parm
) == NULL_TREE
)
1319 error (" a template type parameter must begin with `class' or `typename'");
1320 TREE_TYPE (parm
) = void_type_node
;
1323 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1324 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1325 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1327 if (TREE_PERMANENT (parm
) == 0)
1329 parm
= copy_node (parm
);
1330 TREE_PERMANENT (parm
) = 1;
1332 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1333 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1334 = build_template_parm_index (idx
, processing_template_decl
,
1335 processing_template_decl
,
1336 decl
, TREE_TYPE (parm
));
1341 parm
= TREE_VALUE (parm
);
1343 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1345 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1346 /* This is for distinguishing between real templates and template
1347 template parameters */
1348 TREE_TYPE (parm
) = t
;
1349 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1354 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1355 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1356 decl
= build_decl (TYPE_DECL
, parm
, t
);
1359 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1360 TYPE_NAME (t
) = decl
;
1361 TYPE_STUB_DECL (t
) = decl
;
1363 TEMPLATE_TYPE_PARM_INDEX (t
)
1364 = build_template_parm_index (idx
, processing_template_decl
,
1365 processing_template_decl
,
1366 decl
, TREE_TYPE (parm
));
1368 SET_DECL_ARTIFICIAL (decl
);
1370 parm
= build_tree_list (defval
, parm
);
1371 return chainon (list
, parm
);
1374 /* The end of a template parameter list has been reached. Process the
1375 tree list into a parameter vector, converting each parameter into a more
1376 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1380 end_template_parm_list (parms
)
1385 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1387 current_template_parms
1388 = tree_cons (build_int_2 (0, processing_template_decl
),
1389 saved_parmlist
, current_template_parms
);
1391 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1392 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1394 return saved_parmlist
;
1397 /* end_template_decl is called after a template declaration is seen. */
1400 end_template_decl ()
1402 reset_specialization ();
1404 if (! processing_template_decl
)
1407 /* This matches the pushlevel in begin_template_parm_list. */
1410 --processing_template_decl
;
1411 current_template_parms
= TREE_CHAIN (current_template_parms
);
1412 (void) get_pending_sizes (); /* Why? */
1415 /* Generate a valid set of template args from current_template_parms. */
1418 current_template_args ()
1420 tree header
= current_template_parms
;
1421 int length
= list_length (header
);
1422 tree args
= make_tree_vec (length
);
1427 tree a
= copy_node (TREE_VALUE (header
));
1428 int i
= TREE_VEC_LENGTH (a
);
1429 TREE_TYPE (a
) = NULL_TREE
;
1432 tree t
= TREE_VEC_ELT (a
, i
);
1434 /* t will be a list if we are called from within a
1435 begin/end_template_parm_list pair, but a vector directly
1436 if within a begin/end_member_template_processing pair. */
1437 if (TREE_CODE (t
) == TREE_LIST
)
1441 if (TREE_CODE (t
) == TYPE_DECL
1442 || TREE_CODE (t
) == TEMPLATE_DECL
)
1445 t
= DECL_INITIAL (t
);
1448 TREE_VEC_ELT (a
, i
) = t
;
1450 TREE_VEC_ELT (args
, --l
) = a
;
1451 header
= TREE_CHAIN (header
);
1458 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1459 template PARMS. Used by push_template_decl below. */
1462 build_template_decl (decl
, parms
)
1466 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1467 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1468 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1469 if (DECL_LANG_SPECIFIC (decl
))
1471 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1472 DECL_STATIC_FUNCTION_P (tmpl
) =
1473 DECL_STATIC_FUNCTION_P (decl
);
1479 struct template_parm_data
1485 /* Subroutine of push_template_decl used to see if each template
1486 parameter in a partial specialization is used in the explicit
1487 argument list. If T is of the LEVEL given in DATA (which is
1488 treated as a template_parm_data*), then DATA->PARMS is marked
1492 mark_template_parm (t
, data
)
1498 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1500 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1502 level
= TEMPLATE_PARM_LEVEL (t
);
1503 idx
= TEMPLATE_PARM_IDX (t
);
1507 level
= TEMPLATE_TYPE_LEVEL (t
);
1508 idx
= TEMPLATE_TYPE_IDX (t
);
1511 if (level
== tpd
->level
)
1512 tpd
->parms
[idx
] = 1;
1514 /* Return zero so that for_each_template_parm will continue the
1515 traversal of the tree; we want to mark *every* template parm. */
1519 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1520 parameters given by current_template_args, or reuses a
1521 previously existing one, if appropriate. Returns the DECL, or an
1522 equivalent one, if it is replaced via a call to duplicate_decls. */
1525 push_template_decl (decl
)
1533 int is_friend
= (TREE_CODE (decl
) == FUNCTION_DECL
1534 && DECL_FRIEND_P (decl
));
1537 /* For a friend, we want the context of the friend function, not
1538 the type of which it is a friend. */
1539 ctx
= DECL_CONTEXT (decl
);
1540 else if (DECL_REAL_CONTEXT (decl
))
1541 /* In the case of a virtual function, we want the class in which
1543 ctx
= DECL_REAL_CONTEXT (decl
);
1545 /* Otherwise, if we're currently definining some class, the DECL
1546 is assumed to be a member of the class. */
1547 ctx
= current_class_type
;
1549 /* For determining whether this is a primary template or not, we're really
1550 interested in the lexical context, not the true context. */
1552 info
= DECL_CLASS_CONTEXT (decl
);
1556 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
1559 || (TYPE_BEING_DEFINED (info
) && template_header_count
1560 && ! processing_specialization
)
1561 || (template_header_count
> template_class_depth (info
)))
1568 if (current_lang_name
== lang_name_c
)
1569 cp_error ("template with C linkage");
1570 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
1571 cp_error ("template class without a name");
1574 /* Partial specialization. */
1575 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1576 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1578 tree type
= TREE_TYPE (decl
);
1579 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1580 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1581 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1583 /* We check that each of the template parameters given in the
1584 partial specialization is used in the argument list to the
1585 specialization. For example:
1587 template <class T> struct S;
1588 template <class T> struct S<T*>;
1590 The second declaration is OK because `T*' uses the template
1591 parameter T, whereas
1593 template <class T> struct S<int>;
1595 is no good. Even trickier is:
1606 The S2<T> declaration is actually illegal; it is a
1607 full-specialization. Of course,
1610 struct S2<T (*)(U)>;
1612 or some such would have been OK. */
1614 struct template_parm_data tpd
;
1615 int ntparms
= TREE_VEC_LENGTH (TREE_VALUE (current_template_parms
));
1616 int did_error_intro
= 0;
1618 tpd
.level
= TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms
));
1619 tpd
.parms
= alloca (sizeof (int) * ntparms
);
1620 for (i
= 0; i
< ntparms
; ++i
)
1622 for (i
= 0; i
< TREE_VEC_LENGTH (mainargs
); ++i
)
1623 for_each_template_parm (TREE_VEC_ELT (mainargs
, i
),
1624 &mark_template_parm
,
1626 for (i
= 0; i
< ntparms
; ++i
)
1627 if (tpd
.parms
[i
] == 0)
1629 /* One of the template parms was not used in the
1631 if (!did_error_intro
)
1633 cp_error ("template parameters not used in partial specialization:");
1634 did_error_intro
= 1;
1638 TREE_VALUE (TREE_VEC_ELT
1639 (TREE_VALUE (current_template_parms
),
1643 for (; spec
; spec
= TREE_CHAIN (spec
))
1645 /* purpose: args to main template
1646 value: spec template */
1647 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1651 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1652 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1653 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1654 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1658 args
= current_template_args ();
1661 || TREE_CODE (ctx
) == FUNCTION_DECL
1662 || TYPE_BEING_DEFINED (ctx
)
1663 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
1665 if (DECL_LANG_SPECIFIC (decl
)
1666 && DECL_TEMPLATE_INFO (decl
)
1667 && DECL_TI_TEMPLATE (decl
))
1668 tmpl
= DECL_TI_TEMPLATE (decl
);
1671 tmpl
= build_template_decl (decl
, current_template_parms
);
1673 if (DECL_LANG_SPECIFIC (decl
)
1674 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1676 /* A specialization of a member template of a template
1678 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1679 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1680 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1689 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1690 cp_error ("must specialize `%#T' before defining member `%#D'",
1692 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1693 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1694 else if (! DECL_TEMPLATE_INFO (decl
))
1696 cp_error ("template definition of non-template `%#D'", decl
);
1700 tmpl
= DECL_TI_TEMPLATE (decl
);
1702 if (is_member_template (tmpl
))
1704 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1705 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1709 /* The declaration is a specialization of a member
1710 template, declared outside the class. Therefore, the
1711 innermost template arguments will be NULL, so we
1712 replace them with the arguments determined by the
1713 earlier call to check_explicit_specialization. */
1714 args
= DECL_TI_ARGS (decl
);
1717 = build_template_decl (decl
, current_template_parms
);
1718 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1719 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1720 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1721 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1722 DECL_TEMPLATE_INFO (new_tmpl
) =
1723 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1725 register_specialization (new_tmpl
, tmpl
, args
);
1729 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1730 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1731 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1733 cp_error ("got %d template parameters for `%#D'",
1734 TREE_VEC_LENGTH (a
), decl
);
1735 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1737 if (TREE_VEC_LENGTH (args
) > 1)
1738 /* Get the template parameters for the enclosing template
1740 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1745 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1749 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1751 /* When processing an inline member template of a
1752 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1753 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1754 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1756 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1757 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1759 /* There should be template arguments if and only if there is a
1761 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1764 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1766 cp_error ("got %d template parameters for `%#D'",
1767 TREE_VEC_LENGTH (a
), decl
);
1768 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1771 /* Get the innermost set of template arguments. */
1772 args
= innermost_args (args
, 0);
1774 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1775 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1777 if (! ctx
&& primary
)
1778 /* The check of PRIMARY ensures that we do not try to push a
1779 global template friend declared in a template class; such a
1780 thing may well depend on the template parameters of the class. */
1781 tmpl
= pushdecl_top_level (tmpl
);
1784 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1786 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1788 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1790 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1791 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1792 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1794 else if (! DECL_LANG_SPECIFIC (decl
))
1795 cp_error ("template declaration of `%#D'", decl
);
1797 DECL_TEMPLATE_INFO (decl
) = info
;
1799 return DECL_TEMPLATE_RESULT (tmpl
);
1802 /* Called when a class template TYPE is redeclared, e.g.:
1804 template <class T> struct S;
1805 template <class T> struct S {}; */
1808 redeclare_class_template (type
)
1811 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1812 tree tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
1813 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1816 if (!PRIMARY_TEMPLATE_P (tmpl
))
1817 /* The type is nested in some template class. Nothing to worry
1818 about here; there are no new template parameters for the nested
1822 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
1824 cp_error_at ("previous declaration `%D'", tmpl
);
1825 cp_error ("used %d template parameter%s instead of %d",
1826 TREE_VEC_LENGTH (tmpl_parms
),
1827 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
1828 TREE_VEC_LENGTH (parms
));
1832 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
1834 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
1835 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1836 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
1837 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
1839 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
1841 cp_error_at ("template parameter `%#D'", tmpl_parm
);
1842 cp_error ("redeclared here as `%#D'", parm
);
1846 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
1848 /* We have in [temp.param]:
1850 A template-parameter may not be given default arguments
1851 by two different declarations in the same scope. */
1852 cp_error ("redefinition of default argument for `%#D'", parm
);
1856 if (parm_default
!= NULL_TREE
)
1857 /* Update the previous template parameters (which are the ones
1858 that will really count) with the new default value. */
1859 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
1863 /* Attempt to convert the non-type template parameter EXPR to the
1864 indicated TYPE. If the conversion is successful, return the
1865 converted value. If the conversion is unsuccesful, return
1866 NULL_TREE if we issued an error message, or error_mark_node if we
1867 did not. We issue error messages for out-and-out bad template
1868 parameters, but not simply because the conversion failed, since we
1869 might be just trying to do argument deduction. By the time this
1870 function is called, neither TYPE nor EXPR may make use of template
1874 convert_nontype_argument (type
, expr
)
1878 tree expr_type
= TREE_TYPE (expr
);
1880 /* A template-argument for a non-type, non-template
1881 template-parameter shall be one of:
1883 --an integral constant-expression of integral or enumeration
1886 --the name of a non-type template-parameter; or
1888 --the name of an object or function with external linkage,
1889 including function templates and function template-ids but
1890 excluding non-static class members, expressed as id-expression;
1893 --the address of an object or function with external linkage,
1894 including function templates and function template-ids but
1895 excluding non-static class members, expressed as & id-expression
1896 where the & is optional if the name refers to a function or
1899 --a pointer to member expressed as described in _expr.unary.op_. */
1901 /* An integral constant-expression can include const variables
1903 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1904 expr
= decl_constant_value (expr
);
1906 if (is_overloaded_fn (expr
))
1907 /* OK for now. We'll check that it has external linkage later.
1908 Check this first since if expr_type is the unknown_type_node
1909 we would otherwise complain below. */
1911 else if (INTEGRAL_TYPE_P (expr_type
)
1912 || TYPE_PTRMEM_P (expr_type
)
1913 || TYPE_PTRMEMFUNC_P (expr_type
)
1914 /* The next two are g++ extensions. */
1915 || TREE_CODE (expr_type
) == REAL_TYPE
1916 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1918 if (! TREE_CONSTANT (expr
))
1920 cp_error ("non-constant `%E' cannot be used as template argument",
1925 else if (TYPE_PTR_P (expr_type
)
1926 /* If expr is the address of an overloaded function, we
1927 will get the unknown_type_node at this point. */
1928 || expr_type
== unknown_type_node
)
1934 if (TREE_CODE (e
) != ADDR_EXPR
)
1937 cp_error ("`%E' is not a valid template argument", expr
);
1938 error ("it must be %s%s with external linkage",
1939 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1940 ? "a pointer to " : "",
1941 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1942 ? "a function" : "an object");
1946 referent
= TREE_OPERAND (e
, 0);
1947 STRIP_NOPS (referent
);
1949 if (TREE_CODE (referent
) == STRING_CST
)
1951 cp_error ("string literal %E is not a valid template argument",
1953 error ("because it is the address of an object with static linkage");
1957 if (is_overloaded_fn (referent
))
1958 /* We'll check that it has external linkage later. */
1960 else if (TREE_CODE (referent
) != VAR_DECL
)
1962 else if (!TREE_PUBLIC (referent
))
1964 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1965 return error_mark_node
;
1968 else if (TREE_CODE (expr
) == VAR_DECL
)
1970 if (!TREE_PUBLIC (expr
))
1975 cp_error ("object `%E' cannot be used as template argument", expr
);
1979 switch (TREE_CODE (type
))
1984 /* For a non-type template-parameter of integral or enumeration
1985 type, integral promotions (_conv.prom_) and integral
1986 conversions (_conv.integral_) are applied. */
1987 if (!INTEGRAL_TYPE_P (expr_type
))
1988 return error_mark_node
;
1990 /* It's safe to call digest_init in this case; we know we're
1991 just converting one integral constant expression to another. */
1992 return digest_init (type
, expr
, (tree
*) 0);
1996 /* These are g++ extensions. */
1997 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
1998 return error_mark_node
;
2000 return digest_init (type
, expr
, (tree
*) 0);
2004 tree type_pointed_to
= TREE_TYPE (type
);
2006 if (TYPE_PTRMEM_P (type
))
2007 /* For a non-type template-parameter of type pointer to data
2008 member, qualification conversions (_conv.qual_) are
2010 return perform_qualification_conversions (type
, expr
);
2011 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2013 /* For a non-type template-parameter of type pointer to
2014 function, only the function-to-pointer conversion
2015 (_conv.func_) is applied. If the template-argument
2016 represents a set of overloaded functions (or a pointer to
2017 such), the matching function is selected from the set
2022 if (TREE_CODE (expr
) == ADDR_EXPR
)
2023 fns
= TREE_OPERAND (expr
, 0);
2027 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2029 if (fn
== error_mark_node
)
2030 return error_mark_node
;
2032 if (!TREE_PUBLIC (fn
))
2034 if (really_overloaded_fn (fns
))
2035 return error_mark_node
;
2040 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2042 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2048 /* For a non-type template-parameter of type pointer to
2049 object, qualification conversions (_conv.qual_) and the
2050 array-to-pointer conversion (_conv.array_) are applied.
2051 [Note: In particular, neither the null pointer conversion
2052 (_conv.ptr_) nor the derived-to-base conversion
2053 (_conv.ptr_) are applied. Although 0 is a valid
2054 template-argument for a non-type template-parameter of
2055 integral type, it is not a valid template-argument for a
2056 non-type template-parameter of pointer type.]
2058 The call to decay_conversion performs the
2059 array-to-pointer conversion, if appropriate. */
2060 expr
= decay_conversion (expr
);
2062 if (expr
== error_mark_node
)
2063 return error_mark_node
;
2065 return perform_qualification_conversions (type
, expr
);
2070 case REFERENCE_TYPE
:
2072 tree type_referred_to
= TREE_TYPE (type
);
2074 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2076 /* For a non-type template-parameter of type reference to
2077 function, no conversions apply. If the
2078 template-argument represents a set of overloaded
2079 functions, the matching function is selected from the
2080 set (_over.over_). */
2084 fn
= instantiate_type (type_referred_to
, fns
, 0);
2086 if (!TREE_PUBLIC (fn
))
2088 if (really_overloaded_fn (fns
))
2089 /* Don't issue an error here; we might get a different
2090 function if the overloading had worked out
2092 return error_mark_node
;
2097 if (fn
== error_mark_node
)
2098 return error_mark_node
;
2100 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
2107 /* For a non-type template-parameter of type reference to
2108 object, no conversions apply. The type referred to by the
2109 reference may be more cv-qualified than the (otherwise
2110 identical) type of the template-argument. The
2111 template-parameter is bound directly to the
2112 template-argument, which must be an lvalue. */
2113 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
2114 TYPE_MAIN_VARIANT (type
), 1)
2115 || (TYPE_READONLY (expr_type
) >
2116 TYPE_READONLY (type_referred_to
))
2117 || (TYPE_VOLATILE (expr_type
) >
2118 TYPE_VOLATILE (type_referred_to
))
2119 || !real_lvalue_p (expr
))
2120 return error_mark_node
;
2132 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
2134 /* For a non-type template-parameter of type pointer to member
2135 function, no conversions apply. If the template-argument
2136 represents a set of overloaded member functions, the
2137 matching member function is selected from the set
2140 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2141 expr_type
!= unknown_type_node
)
2142 return error_mark_node
;
2144 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2146 /* A ptr-to-member constant. */
2147 if (!comptypes (type
, expr_type
, 1))
2148 return error_mark_node
;
2153 if (TREE_CODE (expr
) != ADDR_EXPR
)
2154 return error_mark_node
;
2156 fns
= TREE_OPERAND (expr
, 0);
2158 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
2161 if (fn
== error_mark_node
)
2162 return error_mark_node
;
2164 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2166 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2173 /* All non-type parameters must have one of these types. */
2174 my_friendly_abort (0);
2178 return error_mark_node
;
2181 /* Convert all template arguments to their appropriate types, and return
2182 a vector containing the resulting values. If any error occurs, return
2183 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
2184 Some error messages are issued even if COMPLAIN is zero; for
2185 instance, if a template argument is composed from a local class.
2187 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2188 provided in ARGLIST, or else trailing parameters must have default
2189 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2190 deduction for any unspecified trailing arguments.
2192 If IS_TMPL_PARM is non-zero, we will coercing parameters of template
2193 template arguments. In this case, ARGLIST is a chain of TREE_LIST
2194 nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */
2197 coerce_template_parms (parms
, arglist
, in_decl
,
2199 require_all_arguments
,
2201 tree parms
, arglist
;
2204 int require_all_arguments
;
2207 int nparms
, nargs
, i
, lost
= 0;
2208 tree vec
= NULL_TREE
;
2210 if (arglist
== NULL_TREE
)
2212 else if (TREE_CODE (arglist
) == TREE_VEC
)
2213 nargs
= TREE_VEC_LENGTH (arglist
);
2215 nargs
= list_length (arglist
);
2217 nparms
= TREE_VEC_LENGTH (parms
);
2221 && require_all_arguments
2222 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
2226 error ("incorrect number of parameters (%d, should be %d)",
2230 cp_error_at ("in template expansion for decl `%D'",
2234 return error_mark_node
;
2237 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
&& nargs
== nparms
)
2238 vec
= copy_node (arglist
);
2241 vec
= make_tree_vec (nparms
);
2243 for (i
= 0; i
< nparms
; i
++)
2246 tree parm
= TREE_VEC_ELT (parms
, i
);
2251 arglist
= TREE_CHAIN (arglist
);
2253 if (arg
== error_mark_node
)
2256 arg
= TREE_VALUE (arg
);
2258 else if (is_tmpl_parm
&& i
< nargs
)
2260 arg
= TREE_VEC_ELT (arglist
, i
);
2261 if (arg
== error_mark_node
)
2264 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
2266 my_friendly_assert (!require_all_arguments
, 0);
2269 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
2270 arg
= tsubst (TREE_PURPOSE (parm
), vec
, in_decl
);
2272 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, in_decl
);
2274 TREE_VEC_ELT (vec
, i
) = arg
;
2277 for (i
= 0; i
< nparms
; i
++)
2279 tree arg
= TREE_VEC_ELT (vec
, i
);
2280 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2282 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2284 if (is_tmpl_parm
&& i
< nargs
)
2286 /* In case we are checking arguments inside a template template
2287 parameter, ARG that does not come from default argument is
2288 also a TREE_LIST node. Note that ARG can also be a TREE_LIST
2289 in other cases such as overloaded functions. */
2290 if (arg
!= NULL_TREE
&& arg
!= error_mark_node
)
2291 arg
= TREE_VALUE (arg
);
2294 if (arg
== NULL_TREE
)
2295 /* We're out of arguments. */
2297 my_friendly_assert (!require_all_arguments
, 0);
2301 if (arg
== error_mark_node
)
2303 cp_error ("template argument %d is invalid", i
+ 1);
2308 if (TREE_CODE (arg
) == TREE_LIST
2309 && TREE_TYPE (arg
) != NULL_TREE
2310 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2312 /* The template argument was the name of some
2313 member function. That's usually
2314 illegal, but static members are OK. In any
2315 case, grab the underlying fields/functions
2316 and issue an error later if required. */
2317 arg
= TREE_VALUE (arg
);
2318 TREE_TYPE (arg
) = unknown_type_node
;
2321 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
2322 requires_type
= TREE_CODE (parm
) == TYPE_DECL
2323 || requires_tmpl_type
;
2325 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2326 we also accept implicitly created TYPE_DECL as a valid argument. */
2327 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
2328 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
2329 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
2330 && !CLASSTYPE_TEMPLATE_INFO (arg
))
2331 || (TREE_CODE (arg
) == RECORD_TYPE
2332 && CLASSTYPE_TEMPLATE_INFO (arg
)
2333 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
2334 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
2335 && requires_tmpl_type
);
2336 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
2337 arg
= TYPE_STUB_DECL (arg
);
2338 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
2339 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
2341 if (is_tmpl_parm
&& i
< nargs
)
2342 is_type
= TREE_CODE (arg
) == TYPE_DECL
|| is_tmpl_type
;
2344 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
2346 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
2347 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
2349 cp_pedwarn ("to refer to a type member of a template parameter,");
2350 cp_pedwarn (" use `typename %E'", arg
);
2352 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
2353 TREE_OPERAND (arg
, 1));
2356 if (is_type
!= requires_type
)
2362 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2365 cp_error (" expected a constant of type `%T', got `%T'",
2367 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2369 cp_error (" expected a type, got `%E'", arg
);
2373 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2376 if (is_tmpl_type
^ requires_tmpl_type
)
2380 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2383 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2385 cp_error (" expected a class template, got `%T'", arg
);
2388 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2393 if (requires_tmpl_type
)
2395 cp_error ("nested template template parameter not implemented");
2397 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2404 if (requires_tmpl_type
)
2406 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2407 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2409 /* The parameter and argument roles have to be switched
2410 here in order to handle default arguments properly.
2412 template<template <class> class TT> void f(TT<int>)
2413 should be able to accept vector<int> which comes from
2414 template <class T, class Allcator = allocator>
2417 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1, 1);
2418 if (val
!= error_mark_node
)
2421 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2423 if (val
!= error_mark_node
2424 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2425 val
= TREE_TYPE (val
);
2429 val
= groktypename (arg
);
2430 if (! processing_template_decl
)
2432 tree t
= target_type (val
);
2433 if (TREE_CODE (t
) != TYPENAME_TYPE
2435 && decl_function_context (TYPE_MAIN_DECL (t
)))
2437 cp_error ("type `%T' composed from a local class is not a valid template-argument",
2439 return error_mark_node
;
2446 tree t
= tsubst (TREE_TYPE (parm
), vec
, in_decl
);
2448 if (processing_template_decl
)
2449 arg
= maybe_fold_nontype_arg (arg
);
2451 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
2452 /* We used to call digest_init here. However, digest_init
2453 will report errors, which we don't want when complain
2454 is zero. More importantly, digest_init will try too
2455 hard to convert things: for example, `0' should not be
2456 converted to pointer type at this point according to
2457 the standard. Accepting this is not merely an
2458 extension, since deciding whether or not these
2459 conversions can occur is part of determining which
2460 function template to call, or whether a given epxlicit
2461 argument specification is legal. */
2462 val
= convert_nontype_argument (t
, arg
);
2466 if (val
== NULL_TREE
)
2467 val
= error_mark_node
;
2468 else if (val
== error_mark_node
&& complain
)
2469 cp_error ("could not convert template argument `%E' to `%T'",
2473 if (val
== error_mark_node
)
2476 TREE_VEC_ELT (vec
, i
) = val
;
2479 return error_mark_node
;
2483 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2484 of template arguments. Returns 0 otherwise. */
2487 comp_template_args (oldargs
, newargs
)
2488 tree oldargs
, newargs
;
2492 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
2495 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
2497 tree nt
= TREE_VEC_ELT (newargs
, i
);
2498 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2502 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2504 if (TREE_CODE (nt
) == TREE_VEC
)
2506 /* For member templates */
2507 if (comp_template_args (nt
, ot
))
2510 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2512 if (comptypes (ot
, nt
, 1))
2515 else if (cp_tree_equal (ot
, nt
) > 0)
2522 /* Given class template name and parameter list, produce a user-friendly name
2523 for the instantiation. */
2526 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2528 tree parms
, arglist
;
2531 static struct obstack scratch_obstack
;
2532 static char *scratch_firstobj
;
2535 if (!scratch_firstobj
)
2536 gcc_obstack_init (&scratch_obstack
);
2538 obstack_free (&scratch_obstack
, scratch_firstobj
);
2539 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2542 #define buflen sizeof(buf)
2543 #define check if (bufp >= buf+buflen-1) goto too_long
2544 #define ccat(c) *bufp++=(c); check
2545 #define advance bufp+=strlen(bufp); check
2546 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2549 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2551 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2558 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2559 s
= fndecl_as_string (ctx
, 0);
2560 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2561 s
= type_as_string_real (ctx
, 0, 1);
2563 my_friendly_abort (0);
2569 nparms
= TREE_VEC_LENGTH (parms
);
2570 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2571 for (i
= 0; i
< nparms
; i
++)
2573 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2574 tree arg
= TREE_VEC_ELT (arglist
, i
);
2579 if (TREE_CODE (parm
) == TYPE_DECL
)
2581 cat (type_as_string_real (arg
, 0, 1));
2584 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2586 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2587 /* Already substituted with real template. Just output
2588 the template name here */
2589 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2591 /* Output the parameter declaration */
2592 cat (type_as_string_real (arg
, 0, 1));
2596 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2598 if (TREE_CODE (arg
) == TREE_LIST
)
2600 /* New list cell was built because old chain link was in
2602 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2603 arg
= TREE_VALUE (arg
);
2605 /* No need to check arglist against parmlist here; we did that
2606 in coerce_template_parms, called from lookup_template_class. */
2607 cat (expr_as_string (arg
, 0));
2610 char *bufp
= obstack_next_free (&scratch_obstack
);
2612 while (bufp
[offset
- 1] == ' ')
2614 obstack_blank_fast (&scratch_obstack
, offset
);
2616 /* B<C<char> >, not B<C<char>> */
2617 if (bufp
[offset
- 1] == '>')
2622 return (char *) obstack_base (&scratch_obstack
);
2627 fatal ("out of (preallocated) string space creating template instantiation name");
2633 classtype_mangled_name (t
)
2636 if (CLASSTYPE_TEMPLATE_INFO (t
)
2637 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2639 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2640 char *mangled_name
= mangle_class_name_for_template
2641 (IDENTIFIER_POINTER (name
),
2642 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2643 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2644 tree id
= get_identifier (mangled_name
);
2645 IDENTIFIER_TEMPLATE (id
) = name
;
2649 return TYPE_IDENTIFIER (t
);
2653 add_pending_template (d
)
2658 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2659 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2661 ti
= DECL_TEMPLATE_INFO (d
);
2663 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2666 *template_tail
= perm_tree_cons
2667 (current_function_decl
, d
, NULL_TREE
);
2668 template_tail
= &TREE_CHAIN (*template_tail
);
2669 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2673 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2674 may be either a _DECL or an overloaded function or an
2675 IDENTIFIER_NODE), and ARGLIST. */
2678 lookup_template_function (fns
, arglist
)
2683 if (fns
== NULL_TREE
)
2685 cp_error ("non-template used as template");
2686 return error_mark_node
;
2689 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2690 copy_to_permanent (arglist
);
2692 return build_min (TEMPLATE_ID_EXPR
,
2694 ? TREE_TYPE (fns
) : unknown_type_node
,
2699 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2700 parameters, find the desired type.
2702 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2703 Since ARGLIST is build on the decl_obstack, we must copy it here
2704 to keep it from being reclaimed when the decl storage is reclaimed.
2706 IN_DECL, if non-NULL, is the template declaration we are trying to
2709 If the template class is really a local class in a template
2710 function, then the FUNCTION_CONTEXT is the function in which it is
2711 being instantiated. */
2714 lookup_template_class (d1
, arglist
, in_decl
, context
)
2719 tree
template = NULL_TREE
, parmlist
;
2723 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2725 if (IDENTIFIER_LOCAL_VALUE (d1
)
2726 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2727 template = IDENTIFIER_LOCAL_VALUE (d1
);
2730 template = IDENTIFIER_NAMESPACE_VALUE (d1
); /* XXX */
2732 template = IDENTIFIER_CLASS_VALUE (d1
);
2735 context
= DECL_CONTEXT (template);
2737 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2739 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2740 return error_mark_node
;
2741 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2742 d1
= DECL_NAME (template);
2744 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2746 template = CLASSTYPE_TI_TEMPLATE (d1
);
2747 d1
= DECL_NAME (template);
2749 else if (TREE_CODE (d1
) == TEMPLATE_DECL
2750 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
2753 d1
= DECL_NAME (template);
2754 context
= DECL_CONTEXT (template);
2757 my_friendly_abort (272);
2759 /* With something like `template <class T> class X class X { ... };'
2760 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2761 We don't want to do that, but we have to deal with the situation, so
2762 let's give them some syntax errors to chew on instead of a crash. */
2764 return error_mark_node
;
2765 if (TREE_CODE (template) != TEMPLATE_DECL
)
2767 cp_error ("non-template type `%T' used as a template", d1
);
2769 cp_error_at ("for template declaration `%D'", in_decl
);
2770 return error_mark_node
;
2773 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2775 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2776 template arguments */
2778 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2779 tree template2
= TYPE_STUB_DECL (parm
);
2782 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2783 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2785 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1, 0);
2786 if (arglist2
== error_mark_node
)
2787 return error_mark_node
;
2789 arglist2
= copy_to_permanent (arglist2
);
2790 CLASSTYPE_TEMPLATE_INFO (parm
)
2791 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2792 TYPE_SIZE (parm
) = 0;
2795 else if (PRIMARY_TEMPLATE_P (template)
2796 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2799 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2801 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2803 if (arglist
== error_mark_node
)
2804 return error_mark_node
;
2805 if (uses_template_parms (arglist
))
2808 if (comp_template_args
2809 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2810 found
= TREE_TYPE (template);
2813 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2814 found
; found
= TREE_CHAIN (found
))
2816 if (TI_USES_TEMPLATE_PARMS (found
)
2817 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2821 found
= TREE_VALUE (found
);
2826 if (can_free (&permanent_obstack
, arglist
))
2827 obstack_free (&permanent_obstack
, arglist
);
2832 /* FIXME avoid duplication. */
2833 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2837 id
= get_identifier (mangled_name
);
2838 IDENTIFIER_TEMPLATE (id
) = d1
;
2840 maybe_push_to_top_level (uses_template_parms (arglist
));
2841 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2843 if (context
!= NULL_TREE
)
2845 /* Set up the context for the type_decl correctly. Note
2846 that we must clear DECL_ASSEMBLER_NAME to fool
2847 build_overload_name into creating a new name. */
2848 tree type_decl
= TYPE_STUB_DECL (t
);
2850 TYPE_CONTEXT (t
) = context
;
2851 DECL_CONTEXT (type_decl
) = context
;
2852 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2853 DECL_ASSEMBLER_NAME (type_decl
) =
2854 get_identifier (build_overload_name (t
, 1, 1));
2857 pop_from_top_level ();
2861 tree type_ctx
= TYPE_CONTEXT (TREE_TYPE (template));
2862 tree args
= tsubst (CLASSTYPE_TI_ARGS (type_ctx
), arglist
, in_decl
);
2863 tree ctx
= lookup_template_class (type_ctx
, args
,
2864 in_decl
, NULL_TREE
);
2866 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2868 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2870 int save_temp
= processing_template_decl
;
2871 processing_template_decl
= 0;
2872 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2873 processing_template_decl
= save_temp
;
2877 t
= lookup_nested_type_by_name (ctx
, id
);
2878 my_friendly_assert (t
!= NULL_TREE
, 42);
2882 /* Seems to be wanted. */
2883 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2885 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2887 arglist
= copy_to_permanent (arglist
);
2888 CLASSTYPE_TEMPLATE_INFO (t
)
2889 = perm_tree_cons (template, arglist
, NULL_TREE
);
2890 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2891 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2892 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2893 = uses_template_parms (arglist
);
2895 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2897 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2898 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2899 if (! uses_template_parms (arglist
))
2900 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2901 = get_identifier (build_overload_name (t
, 1, 1));
2903 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2904 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2905 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2906 add_pending_template (t
);
2912 /* Should be defined in parse.h. */
2915 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
2916 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
2917 If FN returns non-zero, the iteration is terminated, and
2918 for_each_template_parm returns 1. Otherwise, the iteration
2919 continues. If FN never returns a non-zero value, the value
2920 returned by for_each_template_parm is 0. If FN is NULL, it is
2921 considered to be the function which always returns 1. */
2924 for_each_template_parm (t
, fn
, data
)
2931 switch (TREE_CODE (t
))
2935 /* We assume that the object must be instantiated in order to build
2936 the COMPONENT_REF, so we test only whether the type of the
2937 COMPONENT_REF uses template parms. */
2938 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2940 case IDENTIFIER_NODE
:
2941 if (!IDENTIFIER_TEMPLATE (t
))
2943 my_friendly_abort (42);
2945 /* aggregates of tree nodes */
2948 int i
= TREE_VEC_LENGTH (t
);
2950 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
2955 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
2956 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
2958 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
2960 /* constructed type nodes */
2962 case REFERENCE_TYPE
:
2963 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2965 if (TYPE_PTRMEMFUNC_FLAG (t
))
2966 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
2969 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2971 return for_each_template_parm (TREE_VALUE
2972 (CLASSTYPE_TEMPLATE_INFO (t
)),
2975 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
2977 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2979 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
2981 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2983 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
2985 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2987 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
2989 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
2991 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2995 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2998 /* A template template parameter is encountered */
2999 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3000 /* We are parsing a template declaration */
3002 /* We are instantiating templates with template template
3007 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
3009 goto check_type_and_context
;
3013 /* ??? What about FIELD_DECLs? */
3014 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
3015 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
3019 check_type_and_context
:
3020 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
3022 if (DECL_CONTEXT (t
)
3023 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
3028 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3030 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3032 /* template parm nodes */
3033 case TEMPLATE_TYPE_PARM
:
3034 case TEMPLATE_TEMPLATE_PARM
:
3035 case TEMPLATE_PARM_INDEX
:
3037 return (*fn
)(t
, data
);
3041 /* simple type nodes */
3043 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
3045 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
3057 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
3058 if (for_each_template_parm (TREE_VALUE (v
), fn
, data
))
3070 /* Non-error_mark_node ERROR_MARKs are bad things. */
3071 my_friendly_assert (t
== error_mark_node
, 274);
3080 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3083 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
3084 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3085 (TREE_TYPE (t
)), fn
, data
);
3086 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
3090 case REINTERPRET_CAST_EXPR
:
3091 case CONST_CAST_EXPR
:
3092 case STATIC_CAST_EXPR
:
3093 case DYNAMIC_CAST_EXPR
:
3101 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3104 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3112 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
3113 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
3120 sorry ("testing %s for template parms",
3121 tree_code_name
[(int) TREE_CODE (t
)]);
3122 my_friendly_abort (82);
3129 uses_template_parms (t
)
3132 return for_each_template_parm (t
, 0, 0);
3135 static struct tinst_level
*current_tinst_level
= 0;
3136 static struct tinst_level
*free_tinst_level
= 0;
3137 static int tinst_depth
= 0;
3138 extern int max_tinst_depth
;
3139 #ifdef GATHER_STATISTICS
3140 int depth_reached
= 0;
3143 /* Print out all the template instantiations that we are currently
3147 print_template_context ()
3149 struct tinst_level
*p
= current_tinst_level
;
3151 char *file
= input_filename
;
3153 for (; p
; p
= p
->next
)
3155 cp_error (" instantiated from `%D'", p
->decl
);
3157 input_filename
= p
->file
;
3159 error (" instantiated from here");
3162 input_filename
= file
;
3166 push_tinst_level (d
)
3169 struct tinst_level
*new;
3171 if (tinst_depth
>= max_tinst_depth
)
3173 /* If the instantiation in question still has unbound template parms,
3174 we don't really care if we can't instantiate it, so just return.
3175 This happens with base instantiation for implicit `typename'. */
3176 if (uses_template_parms (d
))
3179 error ("template instantiation depth exceeds maximum of %d",
3181 error (" (use -ftemplate-depth-NN to increase the maximum)");
3182 cp_error (" instantiating `%D'", d
);
3184 print_template_context ();
3189 if (free_tinst_level
)
3191 new = free_tinst_level
;
3192 free_tinst_level
= new->next
;
3195 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
3199 new->file
= input_filename
;
3200 new->next
= current_tinst_level
;
3201 current_tinst_level
= new;
3204 #ifdef GATHER_STATISTICS
3205 if (tinst_depth
> depth_reached
)
3206 depth_reached
= tinst_depth
;
3215 struct tinst_level
*old
= current_tinst_level
;
3217 current_tinst_level
= old
->next
;
3218 old
->next
= free_tinst_level
;
3219 free_tinst_level
= old
;
3223 struct tinst_level
*
3226 struct tinst_level
*p
= current_tinst_level
;
3229 for (; p
->next
; p
= p
->next
)
3235 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
3236 vector of template arguments, as for tsubst.
3238 Returns an appropriate tsbust'd friend declaration. */
3241 tsubst_friend_function (decl
, args
)
3247 if (TREE_CODE (decl
) == FUNCTION_DECL
3248 && DECL_TEMPLATE_INSTANTIATION (decl
)
3249 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
3250 /* This was a friend declared with an explicit template
3251 argument list, e.g.:
3255 to indicate that f was a template instantiation, not a new
3256 function declaration. Now, we have to figure out what
3257 instantiation of what template. */
3265 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
3267 tsubst (DECL_TI_ARGS (decl
),
3270 /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
3272 tinfo
= DECL_TEMPLATE_INFO (decl
);
3273 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3274 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3275 DECL_TEMPLATE_INFO (decl
) = tinfo
;
3277 tmpl
= determine_specialization (template_id
,
3281 return instantiate_template (tmpl
, new_args
);
3284 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3286 /* The new_friend will look like an instantiation, to the
3287 compiler, but is not an instantiation from the point of view of
3288 the language. For example, we might have had:
3290 template <class T> struct S {
3291 template <class U> friend void f(T, U);
3294 Then, in S<int>, template <class U> void f(int, U) is not an
3295 instantiation of anything. */
3296 DECL_USE_TEMPLATE (new_friend
) = 0;
3297 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3298 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
3300 if (DECL_CONTEXT (new_friend
) == NULL_TREE
)
3302 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
3303 /* This declaration is a `primary' template. */
3304 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend
))
3307 new_friend
= pushdecl_top_level (new_friend
);
3309 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
3311 /* Check to see that the declaration is really present, and,
3312 possibly obtain an improved declaration. */
3313 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
3325 instantiate_class_template (type
)
3328 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
3329 tree typedecl
, outer_args
;
3331 if (type
== error_mark_node
)
3332 return error_mark_node
;
3334 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
3336 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
3339 template = TI_TEMPLATE (template_info
);
3340 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
3341 args
= TI_ARGS (template_info
);
3343 if (DECL_TEMPLATE_INFO (template))
3345 outer_args
= DECL_TI_ARGS (template);
3346 while (DECL_TEMPLATE_INFO (template))
3347 template = DECL_TI_TEMPLATE (template);
3350 outer_args
= NULL_TREE
;
3352 t
= most_specialized_class
3353 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
, outer_args
);
3355 if (t
== error_mark_node
)
3357 char *str
= "candidates are:";
3358 cp_error ("ambiguous class template instantiation for `%#T'", type
);
3359 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
3361 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3364 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
3368 TYPE_BEING_DEFINED (type
) = 1;
3369 return error_mark_node
;
3372 pattern
= TREE_TYPE (t
);
3374 pattern
= TREE_TYPE (template);
3376 if (TYPE_SIZE (pattern
) == NULL_TREE
)
3380 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3383 if (pedantic
&& uses_template_parms (args
))
3384 /* If there are still template parameters amongst the args, then
3385 we can't instantiate the type; there's no telling whether or not one
3386 of the template parameters might eventually be instantiated to some
3387 value that results in a specialization being used. */
3390 /* We must copy the arguments to the permanent obstack since
3391 during the tsubst'ing below they may wind up in the
3392 DECL_TI_ARGS of some instantiated member template. */
3393 args
= copy_to_permanent (args
);
3395 TYPE_BEING_DEFINED (type
) = 1;
3397 if (! push_tinst_level (type
))
3400 maybe_push_to_top_level (uses_template_parms (type
));
3401 pushclass (type
, 0);
3404 args
= add_to_template_args (outer_args
, args
);
3406 if (flag_external_templates
)
3408 if (flag_alt_external_templates
)
3410 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
3411 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
3412 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3413 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3414 && CLASSTYPE_INTERFACE_KNOWN (type
));
3418 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
3419 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3420 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
3421 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3422 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3423 && CLASSTYPE_INTERFACE_KNOWN (type
));
3428 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
3429 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
3432 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
3433 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
3434 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
3435 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
3436 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
3437 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
3438 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
3439 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
3440 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
3441 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
3442 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
3443 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
3444 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
3445 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
3446 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
3447 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
3448 TYPE_USES_COMPLEX_INHERITANCE (type
)
3449 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
3450 TYPE_USES_MULTIPLE_INHERITANCE (type
)
3451 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
3452 TYPE_USES_VIRTUAL_BASECLASSES (type
)
3453 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
3454 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
3455 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
3457 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
3459 /* If this is a partial instantiation, don't tsubst anything. We will
3460 only use this type for implicit typename, so the actual contents don't
3461 matter. All that matters is whether a particular name is a type. */
3462 if (uses_template_parms (type
))
3464 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
3465 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
3466 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
3467 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
3468 TYPE_SIZE (type
) = integer_zero_node
;
3473 tree binfo
= TYPE_BINFO (type
);
3474 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
3480 int len
= TREE_VEC_LENGTH (pbases
);
3481 bases
= make_tree_vec (len
);
3482 for (i
= 0; i
< len
; ++i
)
3486 TREE_VEC_ELT (bases
, i
) = elt
3487 = tsubst (TREE_VEC_ELT (pbases
, i
), args
, NULL_TREE
);
3488 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
3490 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
3492 ("base type `%T' of `%T' fails to be a struct or class type",
3493 TREE_TYPE (elt
), type
);
3494 else if (TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
3495 cp_error ("base class `%T' of `%T' has incomplete type",
3496 TREE_TYPE (elt
), type
);
3498 /* Don't initialize this until the vector is filled out, or
3499 lookups will crash. */
3500 BINFO_BASETYPES (binfo
) = bases
;
3504 field_chain
= &TYPE_FIELDS (type
);
3506 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
3508 tree tag
= TREE_VALUE (t
);
3510 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
3511 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
3513 (void) tsubst_enum (tag
, args
, field_chain
);
3514 while (*field_chain
)
3516 DECL_FIELD_CONTEXT (*field_chain
) = type
;
3517 field_chain
= &TREE_CHAIN (*field_chain
);
3521 tsubst (tag
, args
, NULL_TREE
);
3524 /* Don't replace enum constants here. */
3525 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
3526 if (TREE_CODE (t
) != CONST_DECL
)
3528 tree r
= tsubst (t
, args
, NULL_TREE
);
3529 if (TREE_CODE (r
) == VAR_DECL
)
3531 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
3532 /* Perhaps we should do more of grokfield here. */
3534 DECL_IN_AGGR_P (r
) = 1;
3535 DECL_EXTERNAL (r
) = 1;
3536 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
3540 field_chain
= &TREE_CHAIN (r
);
3543 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
3544 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
3546 if (DECL_CONSTRUCTOR_P (t
))
3547 grok_ctor_properties (type
, t
);
3548 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
3549 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
3552 /* Construct the DECL_FRIENDLIST for the new class type. */
3553 typedecl
= TYPE_MAIN_DECL (type
);
3554 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
3560 DECL_FRIENDLIST (typedecl
)
3561 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
3562 DECL_FRIENDLIST (typedecl
));
3564 for (friends
= TREE_VALUE (t
);
3565 friends
!= NULL_TREE
;
3566 friends
= TREE_CHAIN (friends
))
3568 if (TREE_PURPOSE (friends
) == error_mark_node
)
3570 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3571 = tree_cons (error_mark_node
,
3572 tsubst_friend_function (TREE_VALUE (friends
),
3574 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3578 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3579 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
, NULL_TREE
),
3581 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3587 t
= CLASSTYPE_FRIEND_CLASSES (type
)
3588 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
, NULL_TREE
);
3590 /* This does injection for friend classes. */
3591 for (; t
; t
= TREE_CHAIN (t
))
3592 TREE_VALUE (t
) = xref_tag_from_type (TREE_VALUE (t
), NULL_TREE
, 1);
3594 /* This does injection for friend functions. */
3595 if (!processing_template_decl
)
3597 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
, NULL_TREE
);
3599 for (; t
; t
= TREE_CHAIN (t
))
3601 tree d
= TREE_VALUE (t
);
3603 if (TREE_CODE (d
) == TYPE_DECL
)
3604 /* Already injected. */;
3610 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
3611 if (TREE_CODE (t
) == FIELD_DECL
)
3613 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
3614 require_complete_type (t
);
3617 type
= finish_struct_1 (type
, 0);
3618 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
3620 repo_template_used (type
);
3621 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
3622 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
3625 TYPE_BEING_DEFINED (type
) = 0;
3628 pop_from_top_level ();
3638 if (t1
== NULL_TREE
)
3639 return t2
== NULL_TREE
;
3640 if (t2
== NULL_TREE
)
3642 /* Don't care if one declares its arg const and the other doesn't -- the
3643 main variant of the arg type is all that matters. */
3644 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
3645 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
3647 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
3651 lookup_nested_type_by_name (ctype
, name
)
3656 complete_type (ctype
);
3658 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
3660 if (name
== TREE_PURPOSE (t
)
3661 /* this catches typedef enum { foo } bar; */
3662 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
3663 return TREE_VALUE (t
);
3668 /* If arg is a non-type template parameter that does not depend on template
3669 arguments, fold it like we weren't in the body of a template. */
3672 maybe_fold_nontype_arg (arg
)
3675 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
3676 && !uses_template_parms (arg
))
3678 /* Sometimes, one of the args was an expression involving a
3679 template constant parameter, like N - 1. Now that we've
3680 tsubst'd, we might have something like 2 - 1. This will
3681 confuse lookup_template_class, so we do constant folding
3682 here. We have to unset processing_template_decl, to
3683 fool build_expr_from_tree() into building an actual
3686 int saved_processing_template_decl
= processing_template_decl
;
3687 processing_template_decl
= 0;
3688 arg
= fold (build_expr_from_tree (arg
));
3689 processing_template_decl
= saved_processing_template_decl
;
3694 /* Return the TREE_VEC with the arguments for the innermost template header,
3695 where ARGS is either that or the VEC of VECs for all the arguments.
3697 If is_spec, then we are dealing with a specialization of a member
3698 template, and want the second-innermost args, the innermost ones that
3699 are instantiated. */
3702 innermost_args (args
, is_spec
)
3706 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3707 return TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1 - is_spec
);
3711 /* Take the tree structure T and replace template parameters used therein
3712 with the argument vector ARGS. IN_DECL is an associated decl for
3715 tsubst is used for dealing with types, decls and the like; for
3716 expressions, use tsubst_expr or tsubst_copy. */
3719 tsubst (t
, args
, in_decl
)
3725 if (t
== NULL_TREE
|| t
== error_mark_node
3726 || t
== integer_type_node
3727 || t
== void_type_node
3728 || t
== char_type_node
)
3731 type
= TREE_TYPE (t
);
3732 if (type
== unknown_type_node
)
3733 my_friendly_abort (42);
3734 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3735 && TREE_CODE (t
) != TYPENAME_TYPE
3736 && TREE_CODE (t
) != TEMPLATE_DECL
)
3737 type
= tsubst (type
, args
, in_decl
);
3739 switch (TREE_CODE (t
))
3742 if (TYPE_PTRMEMFUNC_P (t
))
3744 tree r
= build_ptrmemfunc_type
3745 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, in_decl
));
3746 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3750 /* else fall through */
3752 if (uses_template_parms (t
))
3754 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3760 ? tsubst (TYPE_CONTEXT (t
), args
, in_decl
) : NULL_TREE
;
3762 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3764 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3768 /* else fall through */
3770 case IDENTIFIER_NODE
:
3783 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
3784 if (ctx
== NULL_TREE
)
3786 else if (ctx
== current_function_decl
)
3787 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3789 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3793 if (t
== integer_type_node
)
3796 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3797 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3801 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3802 max
= tsubst_expr (max
, args
, in_decl
);
3803 if (processing_template_decl
)
3805 tree itype
= make_node (INTEGER_TYPE
);
3806 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3807 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3812 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3813 return build_index_2_type (size_zero_node
, max
);
3816 case TEMPLATE_TYPE_PARM
:
3817 case TEMPLATE_TEMPLATE_PARM
:
3818 case TEMPLATE_PARM_INDEX
:
3825 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3826 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3828 idx
= TEMPLATE_TYPE_IDX (t
);
3829 level
= TEMPLATE_TYPE_LEVEL (t
);
3833 idx
= TEMPLATE_PARM_IDX (t
);
3834 level
= TEMPLATE_PARM_LEVEL (t
);
3837 if (TREE_VEC_LENGTH (args
) > 0)
3839 tree arg
= NULL_TREE
;
3841 if (TREE_VEC_ELT (args
, 0) != NULL_TREE
3842 && TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3844 levels
= TREE_VEC_LENGTH (args
);
3845 if (level
<= levels
)
3847 (TREE_VEC_ELT (args
, level
- 1), idx
);
3853 arg
= TREE_VEC_ELT (args
, idx
);
3856 if (arg
!= NULL_TREE
)
3858 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3859 return cp_build_type_variant
3860 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3861 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3862 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3864 if (CLASSTYPE_TEMPLATE_INFO (t
))
3866 /* We are processing a type constructed from
3867 a template template parameter */
3868 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3872 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3873 we are resolving nested-types in the signature of
3874 a member function templates.
3875 Otherwise ARG is a TEMPLATE_DECL and is the real
3876 template to be instantiated. */
3877 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3878 arg
= TYPE_NAME (arg
);
3880 r
= lookup_template_class (DECL_NAME (arg
),
3882 DECL_CONTEXT (arg
));
3883 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3887 /* We are processing a template argument list. */
3896 /* This can happen during the attempted tsubst'ing in
3897 unify. This means that we don't yet have any information
3898 about the template parameter in question. */
3901 /* If we get here, we must have been looking at a parm for a
3902 more deeply nested template. Make a new version of this
3903 template parameter, but with a lower level. */
3904 switch (TREE_CODE (t
))
3906 case TEMPLATE_TYPE_PARM
:
3907 case TEMPLATE_TEMPLATE_PARM
:
3909 TEMPLATE_TYPE_PARM_INDEX (r
)
3910 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
3912 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
3913 TYPE_MAIN_VARIANT (r
) = r
;
3914 TYPE_POINTER_TO (r
) = NULL_TREE
;
3915 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
3917 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3918 && CLASSTYPE_TEMPLATE_INFO (t
))
3920 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3921 CLASSTYPE_TEMPLATE_INFO (r
)
3922 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
3926 case TEMPLATE_PARM_INDEX
:
3927 r
= reduce_template_parm_level (t
, TREE_TYPE (t
), levels
);
3931 my_friendly_abort (0);
3939 /* We can get here when processing a member template function
3940 of a template class. */
3942 tree decl
= DECL_TEMPLATE_RESULT (t
);
3946 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
3948 if (!is_template_template_parm
)
3950 /* We might already have an instance of this template. */
3951 spec
= retrieve_specialization (t
, args
);
3952 if (spec
!= NULL_TREE
)
3956 /* Make a new template decl. It will be similar to the
3957 original, but will record the current template arguments.
3958 We also create a new function declaration, which is just
3959 like the old one, but points to this new template, rather
3960 than the old one. */
3961 tmpl
= copy_node (t
);
3962 copy_lang_decl (tmpl
);
3963 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3964 DECL_CHAIN (tmpl
) = NULL_TREE
;
3965 TREE_CHAIN (tmpl
) = NULL_TREE
;
3967 if (is_template_template_parm
)
3969 tree new_decl
= tsubst (decl
, args
, in_decl
);
3970 DECL_RESULT (tmpl
) = new_decl
;
3971 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3975 DECL_CONTEXT (tmpl
) = tsubst (DECL_CONTEXT (t
),
3977 DECL_CLASS_CONTEXT (tmpl
) = tsubst (DECL_CLASS_CONTEXT (t
),
3979 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
3981 if (TREE_CODE (decl
) == TYPE_DECL
)
3983 tree new_type
= tsubst (TREE_TYPE (t
), args
, in_decl
);
3984 TREE_TYPE (tmpl
) = new_type
;
3985 CLASSTYPE_TI_TEMPLATE (new_type
) = tmpl
;
3986 DECL_RESULT (tmpl
) = TYPE_MAIN_DECL (new_type
);
3990 tree new_decl
= tsubst (decl
, args
, in_decl
);
3991 DECL_RESULT (tmpl
) = new_decl
;
3992 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
3993 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3996 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
3997 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
3999 /* The template parameters for this new template are all the
4000 template parameters for the old template, except the
4001 outermost level of parameters. */
4002 for (new_parms
= &DECL_TEMPLATE_PARMS (tmpl
),
4003 parms
= DECL_TEMPLATE_PARMS (t
);
4004 TREE_CHAIN (parms
) != NULL_TREE
;
4005 new_parms
= &(TREE_CHAIN (*new_parms
)),
4006 parms
= TREE_CHAIN (parms
))
4009 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
4012 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
4014 tree default_value
=
4015 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4017 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4019 TREE_VEC_ELT (new_vec
, i
)
4020 = build_tree_list (tsubst (default_value
, args
, in_decl
),
4021 tsubst (parm_decl
, args
, in_decl
));
4026 tree_cons (build_int_2 (0,
4028 (TREE_PURPOSE (parms
)) - 1),
4033 if (PRIMARY_TEMPLATE_P (t
))
4034 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
4036 /* We don't partially instantiate partial specializations. */
4037 if (TREE_CODE (decl
) == TYPE_DECL
)
4040 /* What should we do with the specializations of this member
4041 template? Are they specializations of this new template,
4042 or instantiations of the templates they previously were?
4043 this new template? And where should their
4044 DECL_TI_TEMPLATES point? */
4045 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
4046 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
4048 spec
= TREE_CHAIN (spec
))
4050 /* It helps to consider example here. Consider:
4061 Now, for example, we are instantiating S<int>::f(U u).
4062 We want to make a template:
4067 It will have a specialization, for the case U = int*, of
4071 void S<int>::f<int*>(int*);
4073 This specialization will be an instantiation of
4074 the specialization given in the declaration of S, with
4075 argument list int*. */
4077 tree fn
= TREE_VALUE (spec
);
4081 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
4082 /* Instantiations are on the same list, but they're of
4083 no concern to us. */
4086 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
4088 new_fn
= tsubst (DECL_RESULT (fn
), args
,
4090 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
4091 perm_tree_cons (spec_args
, new_fn
,
4092 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
4095 /* Record this partial instantiation. */
4096 register_specialization (tmpl
, t
, args
);
4106 tree tmpl
= NULL_TREE
;
4109 if (DECL_CONTEXT (t
) != NULL_TREE
4110 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
4112 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
4116 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, t
);
4117 type
= tsubst (type
, args
, in_decl
);
4123 type
= tsubst (type
, args
, in_decl
);
4126 /* If we are instantiating a specialization, get the other args. */
4127 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4131 tmpl
= DECL_TI_TEMPLATE (t
);
4133 /* Start by getting the innermost args. */
4134 argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4136 if (DECL_TEMPLATE_INFO (tmpl
))
4137 argvec
= complete_template_args (tmpl
, argvec
, 0);
4139 /* Do we already have this instantiation? */
4140 spec
= retrieve_specialization (tmpl
, argvec
);
4145 /* We do NOT check for matching decls pushed separately at this
4146 point, as they may not represent instantiations of this
4147 template, and in any case are considered separate under the
4148 discrete model. Instead, see add_maybe_template. */
4152 DECL_USE_TEMPLATE (r
) = 0;
4153 TREE_TYPE (r
) = type
;
4156 = tsubst (DECL_CONTEXT (t
), args
, t
);
4157 DECL_CLASS_CONTEXT (r
) = ctx
;
4159 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
4160 IDENTIFIER_POINTER (DECL_NAME (r
)),
4161 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
4163 /* Type-conversion operator. Reconstruct the name, in
4164 case it's the name of one of the template's parameters. */
4165 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
4168 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
4170 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
4171 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
4172 buf
= (char *) alloca (strlen (dbuf
)
4173 + sizeof (DESTRUCTOR_DECL_PREFIX
));
4174 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
4177 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
4181 /* Instantiations of template functions must be mangled
4182 specially, in order to conform to 14.5.5.1
4183 [temp.over.link]. We use in_decl below rather than
4184 DECL_TI_TEMPLATE (r) because the latter is set to
4185 NULL_TREE in instantiate_decl. */
4189 if (DECL_TEMPLATE_INFO (r
))
4190 tmpl
= DECL_TI_TEMPLATE (r
);
4194 /* tmpl will be NULL if this is a specialization of a
4195 member function of a template class. */
4196 if (name_mangling_version
< 1
4197 || tmpl
== NULL_TREE
4198 || (member
&& !is_member_template (tmpl
)
4199 && !DECL_TEMPLATE_INFO (tmpl
)))
4201 arg_types
= TYPE_ARG_TYPES (type
);
4202 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4203 arg_types
= hash_tree_chain
4204 (build_pointer_type (DECL_CONTEXT (r
)),
4207 DECL_ASSEMBLER_NAME (r
)
4208 = build_decl_overload (DECL_NAME (r
), arg_types
,
4216 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4218 /* We pass the outermost template parameters to
4219 build_template_decl_overload, since the innermost
4220 template parameters are still just template
4221 parameters; there are no corresponding subsitution
4222 arguments. Levels of parms that have been bound
4223 before are not represented in DECL_TEMPLATE_PARMS. */
4224 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
4225 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
4226 tparms
= TREE_CHAIN (tparms
);
4228 targs
= innermost_args (args
, 0);
4232 /* If the template is a specialization, then it is
4233 a member template specialization. We have
4236 template <class T> struct S {
4237 template <int i> void f();
4238 template <> void f<7>();
4241 and now we are forming S<double>::f<7>.
4242 Therefore, the template parameters of interest
4243 are those that are specialized by the template
4244 (i.e., the int), not those we are using to
4245 instantiate the template, i.e. the double. */
4246 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
4247 targs
= DECL_TI_ARGS (tmpl
);
4250 my_friendly_assert (tparms
!= NULL_TREE
4251 && TREE_CODE (tparms
) == TREE_LIST
,
4253 tparms
= TREE_VALUE (tparms
);
4255 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
4256 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4257 arg_types
= hash_tree_chain
4258 (build_pointer_type (DECL_CONTEXT (r
)),
4261 DECL_ASSEMBLER_NAME (r
)
4262 = build_template_decl_overload
4263 (DECL_NAME (r
), arg_types
,
4264 TREE_TYPE (TREE_TYPE (tmpl
)),
4265 tparms
, targs
, member
);
4269 make_decl_rtl (r
, NULL_PTR
, 1);
4271 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, t
);
4272 DECL_MAIN_VARIANT (r
) = r
;
4273 DECL_RESULT (r
) = NULL_TREE
;
4274 DECL_INITIAL (r
) = NULL_TREE
;
4276 TREE_STATIC (r
) = 0;
4277 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
4278 DECL_EXTERNAL (r
) = 1;
4279 DECL_INTERFACE_KNOWN (r
) = 0;
4280 DECL_DEFER_OUTPUT (r
) = 0;
4281 TREE_CHAIN (r
) = NULL_TREE
;
4282 DECL_CHAIN (r
) = NULL_TREE
;
4283 DECL_PENDING_INLINE_INFO (r
) = 0;
4286 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
4287 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
4289 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4291 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4293 /* If we're not using ANSI overloading, then we might have
4294 called duplicate_decls above, and gotten back an
4295 preexisting version of this function. We treat such a
4296 function as a specialization. Otherwise, we cleared
4297 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
4298 this condition will be false. */
4299 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
4300 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
4302 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4304 register_specialization (r
, tmpl
, argvec
);
4307 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
4308 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
4310 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
4311 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
4318 tree r
= copy_node (t
);
4319 TREE_TYPE (r
) = type
;
4320 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
4321 DECL_INITIAL (r
) = TREE_TYPE (r
);
4323 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
, in_decl
);
4325 DECL_CONTEXT (r
) = NULL_TREE
;
4326 #ifdef PROMOTE_PROTOTYPES
4327 if ((TREE_CODE (type
) == INTEGER_TYPE
4328 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4329 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4330 DECL_ARG_TYPE (r
) = integer_type_node
;
4333 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, TREE_CHAIN (t
));
4339 tree r
= copy_node (t
);
4340 TREE_TYPE (r
) = type
;
4343 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, in_decl
);
4345 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, in_decl
);
4346 TREE_CHAIN (r
) = NULL_TREE
;
4352 tree r
= copy_node (t
);
4354 = tsubst_copy (DECL_INITIAL (t
), args
, in_decl
);
4355 TREE_CHAIN (r
) = NULL_TREE
;
4362 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, in_decl
);
4364 /* Do we already have this instantiation? */
4365 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4367 tree tmpl
= DECL_TI_TEMPLATE (t
);
4368 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4370 for (; decls
; decls
= TREE_CHAIN (decls
))
4371 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
4372 return TREE_VALUE (decls
);
4376 TREE_TYPE (r
) = type
;
4377 DECL_CONTEXT (r
) = ctx
;
4378 if (TREE_STATIC (r
))
4379 DECL_ASSEMBLER_NAME (r
)
4380 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
4382 /* Don't try to expand the initializer until someone tries to use
4383 this variable; otherwise we run into circular dependencies. */
4384 DECL_INITIAL (r
) = NULL_TREE
;
4389 if (DECL_LANG_SPECIFIC (r
))
4392 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
4395 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4397 tree tmpl
= DECL_TI_TEMPLATE (t
);
4398 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4399 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4401 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4402 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
4403 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4405 TREE_CHAIN (r
) = NULL_TREE
;
4410 if (t
== TYPE_NAME (TREE_TYPE (t
)))
4411 return TYPE_NAME (type
);
4414 tree r
= copy_node (t
);
4415 TREE_TYPE (r
) = type
;
4416 DECL_CONTEXT (r
) = current_class_type
;
4417 TREE_CHAIN (r
) = NULL_TREE
;
4423 tree purpose
, value
, chain
, result
;
4424 int via_public
, via_virtual
, via_protected
;
4426 if (t
== void_list_node
)
4429 via_public
= TREE_VIA_PUBLIC (t
);
4430 via_protected
= TREE_VIA_PROTECTED (t
);
4431 via_virtual
= TREE_VIA_VIRTUAL (t
);
4433 purpose
= TREE_PURPOSE (t
);
4435 purpose
= tsubst (purpose
, args
, in_decl
);
4436 value
= TREE_VALUE (t
);
4438 value
= tsubst (value
, args
, in_decl
);
4439 chain
= TREE_CHAIN (t
);
4440 if (chain
&& chain
!= void_type_node
)
4441 chain
= tsubst (chain
, args
, in_decl
);
4442 if (purpose
== TREE_PURPOSE (t
)
4443 && value
== TREE_VALUE (t
)
4444 && chain
== TREE_CHAIN (t
))
4446 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
4447 purpose
, value
, chain
);
4448 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
4452 if (type
!= NULL_TREE
)
4458 if (type
== TREE_TYPE (t
))
4461 TREE_TYPE (t
) = complete_type (type
);
4462 if (IS_AGGR_TYPE (type
))
4464 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
4465 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
4466 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
4467 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
4472 /* Otherwise, a vector of template arguments. */
4474 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4475 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4477 bzero ((char *) elts
, len
* sizeof (tree
));
4479 for (i
= 0; i
< len
; i
++)
4481 elts
[i
] = maybe_fold_nontype_arg
4482 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, in_decl
));
4484 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4491 t
= make_tree_vec (len
);
4492 for (i
= 0; i
< len
; i
++)
4493 TREE_VEC_ELT (t
, i
) = elts
[i
];
4498 case REFERENCE_TYPE
:
4501 enum tree_code code
;
4503 if (type
== TREE_TYPE (t
))
4506 code
= TREE_CODE (t
);
4507 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4509 static int last_line
= 0;
4510 static char* last_file
= 0;
4512 /* We keep track of the last time we issued this error
4513 message to avoid spewing a ton of messages during a
4514 single bad template instantiation. */
4515 if (last_line
!= lineno
||
4516 last_file
!= input_filename
)
4518 cp_error ("cannot form type %s to reference type %T during template instantiation",
4519 (code
== POINTER_TYPE
) ? "pointer" : "reference",
4522 last_file
= input_filename
;
4525 /* Use the underlying type in an attempt at error
4526 recovery; maybe the user meant vector<int> and wrote
4527 vector<int&>, or some such. */
4528 if (code
== REFERENCE_TYPE
)
4531 r
= build_pointer_type (TREE_TYPE (type
));
4533 else if (code
== POINTER_TYPE
)
4534 r
= build_pointer_type (type
);
4536 r
= build_reference_type (type
);
4537 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
4539 /* Will this ever be needed for TYPE_..._TO values? */
4544 return build_offset_type
4545 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, in_decl
), type
);
4549 tree values
= TYPE_ARG_TYPES (t
);
4550 tree context
= TYPE_CONTEXT (t
);
4551 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
4554 /* Don't bother recursing if we know it won't change anything. */
4555 if (values
!= void_list_node
)
4557 /* This should probably be rewritten to use hash_tree_cons for
4558 the memory savings. */
4559 tree first
= NULL_TREE
;
4560 tree last
= NULL_TREE
;
4562 for (; values
&& values
!= void_list_node
;
4563 values
= TREE_CHAIN (values
))
4565 tree value
= TYPE_MAIN_VARIANT (type_decays_to
4566 (tsubst (TREE_VALUE (values
), args
, in_decl
)));
4567 /* Don't instantiate default args unless they are used.
4568 Handle it in build_over_call instead. */
4569 tree purpose
= TREE_PURPOSE (values
);
4570 tree x
= build_tree_list (purpose
, value
);
4573 TREE_CHAIN (last
) = x
;
4579 if (values
== void_list_node
)
4580 TREE_CHAIN (last
) = void_list_node
;
4585 context
= tsubst (context
, args
, in_decl
);
4586 /* Could also optimize cases where return value and
4587 values have common elements (e.g., T min(const &T, const T&). */
4589 /* If the above parameters haven't changed, just return the type. */
4590 if (type
== TREE_TYPE (t
)
4591 && values
== TYPE_VALUES (t
)
4592 && context
== TYPE_CONTEXT (t
))
4595 /* Construct a new type node and return it. */
4596 if (TREE_CODE (t
) == FUNCTION_TYPE
4597 && context
== NULL_TREE
)
4599 fntype
= build_function_type (type
, values
);
4601 else if (context
== NULL_TREE
)
4603 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
4605 fntype
= build_cplus_method_type (base
, type
,
4606 TREE_CHAIN (values
));
4610 fntype
= make_node (TREE_CODE (t
));
4611 TREE_TYPE (fntype
) = type
;
4612 TYPE_CONTEXT (fntype
) = context
;
4613 TYPE_VALUES (fntype
) = values
;
4614 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
4615 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
4616 TYPE_MODE (fntype
) = TYPE_MODE (t
);
4617 if (TYPE_METHOD_BASETYPE (t
))
4618 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
4620 /* Need to generate hash value. */
4621 my_friendly_abort (84);
4623 fntype
= build_type_variant (fntype
,
4628 raises
= tsubst (raises
, args
, in_decl
);
4629 fntype
= build_exception_variant (fntype
, raises
);
4635 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, in_decl
);
4637 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
4639 r
= build_cplus_array_type (type
, domain
);
4645 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
4646 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4647 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
)));
4651 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
4652 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
)));
4656 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
4657 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
, in_decl
);
4658 f
= make_typename_type (ctx
, f
);
4659 return cp_build_type_variant
4660 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
4661 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
4665 return make_pointer_declarator
4666 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4669 return make_reference_declarator
4670 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4673 return build_parse_node
4674 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4675 tsubst_expr (TREE_OPERAND (t
, 1), args
, in_decl
));
4678 return make_call_declarator
4679 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4680 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
4681 TREE_OPERAND (t
, 2),
4682 tsubst (TREE_TYPE (t
), args
, in_decl
));
4685 return build_parse_node
4686 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4687 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
));
4690 sorry ("use of `%s' in template",
4691 tree_code_name
[(int) TREE_CODE (t
)]);
4692 return error_mark_node
;
4699 emit_line_note (input_filename
, lineno
);
4703 expand_start_bindings (0);
4710 int saved_warn_unused
= 0;
4712 if (processing_template_decl
)
4714 saved_warn_unused
= warn_unused
;
4717 expand_end_bindings (getdecls (), kept_level_p (), 0);
4718 if (processing_template_decl
)
4719 warn_unused
= saved_warn_unused
;
4720 t
= poplevel (kept_level_p (), 1, 0);
4725 /* Like tsubst, but deals with expressions. This function just replaces
4726 template parms; to finish processing the resultant expression, use
4730 tsubst_copy (t
, args
, in_decl
)
4734 enum tree_code code
;
4736 if (t
== NULL_TREE
|| t
== error_mark_node
)
4739 code
= TREE_CODE (t
);
4744 return do_identifier (DECL_NAME (t
), 0);
4748 if (DECL_CONTEXT (t
))
4751 if (TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
)
4752 return lookup_name (DECL_NAME (t
), 0);
4754 ctx
= tsubst (DECL_CONTEXT (t
), args
, in_decl
);
4755 if (ctx
!= DECL_CONTEXT (t
))
4756 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
4762 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4763 t
= tsubst (t
, args
, in_decl
);
4768 if (is_member_template (t
))
4769 return tsubst (t
, args
, in_decl
);
4774 case IDENTIFIER_NODE
:
4775 return do_identifier (t
, 0);
4779 case REINTERPRET_CAST_EXPR
:
4780 case CONST_CAST_EXPR
:
4781 case STATIC_CAST_EXPR
:
4782 case DYNAMIC_CAST_EXPR
:
4784 (code
, tsubst (TREE_TYPE (t
), args
, in_decl
),
4785 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4788 case PREDECREMENT_EXPR
:
4789 case PREINCREMENT_EXPR
:
4790 case POSTDECREMENT_EXPR
:
4791 case POSTINCREMENT_EXPR
:
4793 case TRUTH_NOT_EXPR
:
4796 case CONVERT_EXPR
: /* Unary + */
4804 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4809 case TRUNC_DIV_EXPR
:
4811 case FLOOR_DIV_EXPR
:
4812 case ROUND_DIV_EXPR
:
4813 case EXACT_DIV_EXPR
:
4815 case BIT_ANDTC_EXPR
:
4818 case TRUNC_MOD_EXPR
:
4819 case FLOOR_MOD_EXPR
:
4820 case TRUTH_ANDIF_EXPR
:
4821 case TRUTH_ORIF_EXPR
:
4822 case TRUTH_AND_EXPR
:
4843 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4844 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4848 tree fn
= TREE_OPERAND (t
, 0);
4849 if (is_overloaded_fn (fn
))
4850 fn
= tsubst_copy (get_first_fn (fn
), args
, in_decl
);
4852 /* Sometimes FN is a LOOKUP_EXPR. */
4853 fn
= tsubst_copy (fn
, args
, in_decl
);
4855 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4859 case METHOD_CALL_EXPR
:
4861 tree name
= TREE_OPERAND (t
, 0);
4862 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4864 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4865 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4866 name
= TYPE_MAIN_VARIANT (name
);
4867 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4869 else if (TREE_CODE (name
) == SCOPE_REF
4870 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4872 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4873 name
= TREE_OPERAND (name
, 1);
4874 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4875 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4876 name
= TYPE_MAIN_VARIANT (name
);
4877 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4878 name
= build_nt (SCOPE_REF
, base
, name
);
4881 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
4883 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4884 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
),
4893 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4894 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4895 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4897 if (code
== BIND_EXPR
&& !processing_template_decl
)
4899 /* This processing should really occur in tsubst_expr,
4900 However, tsubst_expr does not recurse into expressions,
4901 since it assumes that there aren't any statements
4902 inside them. Instead, it simply calls
4903 build_expr_from_tree. So, we need to expand the
4905 tree rtl_expr
= begin_stmt_expr ();
4906 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
, in_decl
);
4907 r
= finish_stmt_expr (rtl_expr
, block
);
4916 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4917 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4918 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4919 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4926 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4927 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4928 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4929 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4933 case TEMPLATE_ID_EXPR
:
4935 /* Substituted template arguments */
4936 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
);
4938 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4939 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4941 return lookup_template_function
4942 (tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
), targs
);
4947 tree purpose
, value
, chain
;
4949 if (t
== void_list_node
)
4952 purpose
= TREE_PURPOSE (t
);
4954 purpose
= tsubst_copy (purpose
, args
, in_decl
);
4955 value
= TREE_VALUE (t
);
4957 value
= tsubst_copy (value
, args
, in_decl
);
4958 chain
= TREE_CHAIN (t
);
4959 if (chain
&& chain
!= void_type_node
)
4960 chain
= tsubst_copy (chain
, args
, in_decl
);
4961 if (purpose
== TREE_PURPOSE (t
)
4962 && value
== TREE_VALUE (t
)
4963 && chain
== TREE_CHAIN (t
))
4965 return tree_cons (purpose
, value
, chain
);
4972 case TEMPLATE_TYPE_PARM
:
4973 case TEMPLATE_TEMPLATE_PARM
:
4974 case TEMPLATE_PARM_INDEX
:
4976 case REFERENCE_TYPE
:
4983 return tsubst (t
, args
, in_decl
);
4985 case IDENTIFIER_NODE
:
4986 if (IDENTIFIER_TYPENAME_P (t
))
4987 return build_typename_overload
4988 (tsubst (TREE_TYPE (t
), args
, in_decl
));
4994 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, in_decl
), NULL_TREE
,
4995 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, in_decl
));
5002 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
5005 tsubst_expr (t
, args
, in_decl
)
5009 if (t
== NULL_TREE
|| t
== error_mark_node
)
5012 if (processing_template_decl
)
5013 return tsubst_copy (t
, args
, in_decl
);
5015 switch (TREE_CODE (t
))
5018 lineno
= TREE_COMPLEXITY (t
);
5019 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
5024 lineno
= TREE_COMPLEXITY (t
);
5025 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
5031 int i
= suspend_momentary ();
5034 lineno
= TREE_COMPLEXITY (t
);
5035 emit_line_note (input_filename
, lineno
);
5037 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5038 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
5039 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
5040 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, in_decl
);
5042 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5043 resume_momentary (i
);
5050 lineno
= TREE_COMPLEXITY (t
);
5053 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5054 tsubst_expr (tmp
, args
, in_decl
);
5055 finish_for_init_stmt (NULL_TREE
);
5056 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
5059 tmp
= tsubst_expr (FOR_EXPR (t
), args
, in_decl
);
5060 finish_for_expr (tmp
, NULL_TREE
);
5061 tsubst_expr (FOR_BODY (t
), args
, in_decl
);
5062 finish_for_stmt (tmp
, NULL_TREE
);
5068 lineno
= TREE_COMPLEXITY (t
);
5069 begin_while_stmt ();
5070 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
5073 tsubst_expr (WHILE_BODY (t
), args
, in_decl
);
5074 finish_while_stmt (NULL_TREE
);
5080 lineno
= TREE_COMPLEXITY (t
);
5082 tsubst_expr (DO_BODY (t
), args
, in_decl
);
5083 finish_do_body (NULL_TREE
);
5084 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
5094 lineno
= TREE_COMPLEXITY (t
);
5096 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
5100 if (tmp
= THEN_CLAUSE (t
), tmp
)
5102 tsubst_expr (tmp
, args
, in_decl
);
5103 finish_then_clause (NULL_TREE
);
5106 if (tmp
= ELSE_CLAUSE (t
), tmp
)
5108 begin_else_clause ();
5109 tsubst_expr (tmp
, args
, in_decl
);
5110 finish_else_clause (NULL_TREE
);
5121 lineno
= TREE_COMPLEXITY (t
);
5122 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
5123 for (substmt
= COMPOUND_BODY (t
);
5124 substmt
!= NULL_TREE
;
5125 substmt
= TREE_CHAIN (substmt
))
5126 tsubst_expr (substmt
, args
, in_decl
);
5127 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
5133 lineno
= TREE_COMPLEXITY (t
);
5134 finish_break_stmt ();
5138 lineno
= TREE_COMPLEXITY (t
);
5139 finish_continue_stmt ();
5146 lineno
= TREE_COMPLEXITY (t
);
5147 begin_switch_stmt ();
5148 val
= tsubst_expr (SWITCH_COND (t
), args
, in_decl
);
5149 finish_switch_cond (val
);
5151 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
5152 tsubst_expr (tmp
, args
, in_decl
);
5154 finish_switch_stmt (val
, NULL_TREE
);
5159 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, in_decl
),
5160 tsubst_expr (CASE_HIGH (t
), args
, in_decl
));
5164 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
5171 lineno
= TREE_COMPLEXITY (t
);
5172 t
= GOTO_DESTINATION (t
);
5173 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
5174 /* Computed goto's must be tsubst'd into. On the other hand,
5175 non-computed gotos must not be; the identifier in question
5176 will have no binding. */
5177 t
= tsubst_expr (t
, args
, in_decl
);
5178 finish_goto_stmt (t
);
5182 lineno
= TREE_COMPLEXITY (t
);
5183 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, in_decl
),
5184 tsubst_expr (ASM_STRING (t
), args
, in_decl
),
5185 tsubst_expr (ASM_OUTPUTS (t
), args
, in_decl
),
5186 tsubst_expr (ASM_INPUTS (t
), args
, in_decl
),
5187 tsubst_expr (ASM_CLOBBERS (t
), args
, in_decl
));
5191 lineno
= TREE_COMPLEXITY (t
);
5193 tsubst_expr (TRY_STMTS (t
), args
, in_decl
);
5194 finish_try_block (NULL_TREE
);
5196 tree handler
= TRY_HANDLERS (t
);
5197 for (; handler
; handler
= TREE_CHAIN (handler
))
5198 tsubst_expr (handler
, args
, in_decl
);
5200 finish_handler_sequence (NULL_TREE
);
5204 lineno
= TREE_COMPLEXITY (t
);
5206 if (HANDLER_PARMS (t
))
5208 tree d
= HANDLER_PARMS (t
);
5209 expand_start_catch_block
5210 (tsubst (TREE_OPERAND (d
, 1), args
, in_decl
),
5211 tsubst (TREE_OPERAND (d
, 0), args
, in_decl
));
5214 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
5215 finish_handler_parms (NULL_TREE
);
5216 tsubst_expr (HANDLER_BODY (t
), args
, in_decl
);
5217 finish_handler (NULL_TREE
);
5221 lineno
= TREE_COMPLEXITY (t
);
5223 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
5224 tsubst_enum (t
, args
, NULL
);
5228 return build_expr_from_tree (tsubst_copy (t
, args
, in_decl
));
5234 instantiate_template (tmpl
, targ_ptr
)
5235 tree tmpl
, targ_ptr
;
5239 struct obstack
*old_fmp_obstack
;
5240 extern struct obstack
*function_maybepermanent_obstack
;
5242 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
5244 /* FIXME this won't work with member templates; we only have one level
5246 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
5248 /* Check to see if we already have this specialization. */
5249 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
5251 if (spec
!= NULL_TREE
)
5255 push_obstacks (&permanent_obstack
, &permanent_obstack
);
5256 old_fmp_obstack
= function_maybepermanent_obstack
;
5257 function_maybepermanent_obstack
= &permanent_obstack
;
5259 len
= DECL_NTPARMS (tmpl
);
5264 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
5265 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
5267 tree nt
= target_type (t
);
5268 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
5270 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
5271 cp_error (" trying to instantiate `%D'", tmpl
);
5272 fndecl
= error_mark_node
;
5276 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
5278 targ_ptr
= copy_to_permanent (targ_ptr
);
5280 /* substitute template parameters */
5281 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, tmpl
);
5283 if (flag_external_templates
)
5284 add_pending_template (fndecl
);
5287 function_maybepermanent_obstack
= old_fmp_obstack
;
5293 /* Push the name of the class template into the scope of the instantiation. */
5296 overload_template_name (type
)
5299 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
5302 if (IDENTIFIER_CLASS_VALUE (id
)
5303 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
5306 decl
= build_decl (TYPE_DECL
, id
, type
);
5307 SET_DECL_ARTIFICIAL (decl
);
5308 pushdecl_class_level (decl
);
5312 /* Like type_unification but designed specially to handle conversion
5313 operators. The EXTRA_FN_ARG, if any, is the type of an additional
5314 parameter to be added to the beginning of FN's parameter list. */
5317 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
5318 strict
, extra_fn_arg
)
5319 tree fn
, explicit_targs
, targs
, args
, return_type
;
5324 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5325 tree decl_arg_types
= args
;
5327 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
5329 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
5331 /* This is a template conversion operator. Use the return types
5332 as well as the argument types. */
5333 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
5334 TREE_TYPE (TREE_TYPE (fn
)),
5336 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
5341 if (extra_fn_arg
!= NULL_TREE
)
5342 fn_arg_types
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
,
5345 /* We allow incomplete unification without an error message here
5346 because the standard doesn't seem to explicitly prohibit it. Our
5347 callers must be ready to deal with unification failures in any
5349 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
5360 /* Type unification.
5362 We have a function template signature with one or more references to
5363 template parameters, and a parameter list we wish to fit to this
5364 template. If possible, produce a list of parameters for the template
5365 which will cause it to fit the supplied parameter list.
5367 Return zero for success, 2 for an incomplete match that doesn't resolve
5368 all the types, and 1 for complete failure. An error message will be
5369 printed only for an incomplete match.
5371 TPARMS[NTPARMS] is an array of template parameter types;
5372 TARGS[NTPARMS] is the array of template parameter values. PARMS is
5373 the function template's signature (using TEMPLATE_PARM_IDX nodes),
5374 and ARGS is the argument list we're trying to match against it.
5376 If SUBR is 1, we're being called recursively (to unify the arguments of
5377 a function or method parameter of a function template), so don't zero
5378 out targs and don't fail on an incomplete match.
5380 If STRICT is 1, the match must be exact (for casts of overloaded
5381 addresses, explicit instantiation, and more_specialized). */
5384 type_unification (tparms
, targs
, parms
, args
, targs_in
,
5385 strict
, allow_incomplete
)
5386 tree tparms
, targs
, parms
, args
, targs_in
;
5387 int strict
, allow_incomplete
;
5389 int ntparms
= TREE_VEC_LENGTH (tparms
);
5395 for (i
= 0; i
< ntparms
; i
++)
5396 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
5398 if (targs_in
!= NULL_TREE
)
5401 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
5404 if (arg_vec
== error_mark_node
)
5407 explicit_mask
= alloca (sizeof (int) * TREE_VEC_LENGTH (targs
));
5408 bzero (explicit_mask
, sizeof(int) * TREE_VEC_LENGTH (targs
));
5411 i
< TREE_VEC_LENGTH (arg_vec
)
5412 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
5415 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (arg_vec
, i
);
5416 /* Let unify know that this argument was explicit. */
5417 explicit_mask
[i
] = 1;
5423 r
= type_unification_real (tparms
, targs
, parms
, args
, 0,
5424 strict
, allow_incomplete
, explicit_mask
);
5429 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
5430 integers, with ones in positions corresponding to arguments in
5431 targs that were provided explicitly, and zeros elsewhere. */
5434 type_unification_real (tparms
, targs
, parms
, args
, subr
,
5435 strict
, allow_incomplete
, explicit_mask
)
5436 tree tparms
, targs
, parms
, args
;
5437 int subr
, strict
, allow_incomplete
;
5442 int ntparms
= TREE_VEC_LENGTH (tparms
);
5444 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
5445 my_friendly_assert (parms
== NULL_TREE
5446 || TREE_CODE (parms
) == TREE_LIST
, 290);
5447 /* ARGS could be NULL (via a call from parse.y to
5448 build_x_function_call). */
5450 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
5451 my_friendly_assert (ntparms
> 0, 292);
5454 && parms
!= void_list_node
5456 && args
!= void_list_node
)
5458 parm
= TREE_VALUE (parms
);
5459 parms
= TREE_CHAIN (parms
);
5460 arg
= TREE_VALUE (args
);
5461 args
= TREE_CHAIN (args
);
5463 if (arg
== error_mark_node
)
5465 if (arg
== unknown_type_node
)
5468 /* Conversions will be performed on a function argument that
5469 corresponds with a function parameter that contains only
5470 non-deducible template parameters and explicitly specified
5471 template parameters. */
5472 if (! uses_template_parms (parm
))
5476 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5477 type
= TREE_TYPE (arg
);
5486 if (comptypes (parm
, type
, 1))
5490 /* It might work; we shouldn't check now, because we might
5491 get into infinite recursion. Overload resolution will
5499 if (TREE_CODE (arg
) == VAR_DECL
)
5500 arg
= TREE_TYPE (arg
);
5501 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
5502 arg
= TREE_TYPE (arg
);
5504 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5506 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
5507 if (TREE_CODE (arg
) == TREE_LIST
5508 && TREE_TYPE (arg
) == unknown_type_node
5509 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
5514 /* Have to back unify here */
5515 arg
= TREE_VALUE (arg
);
5516 ntparms
= DECL_NTPARMS (arg
);
5517 targs
= make_scratch_vec (ntparms
);
5518 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
5520 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
5522 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
5523 parm
, NULL_TREE
, strict
,
5526 arg
= TREE_TYPE (arg
);
5529 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
5531 warning ("using type void* for NULL");
5532 arg
= ptr_type_node
;
5535 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
5536 arg
= TREE_TYPE (arg
);
5538 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
5540 if (TREE_CODE (arg
) == FUNCTION_TYPE
5541 || TREE_CODE (arg
) == METHOD_TYPE
)
5542 arg
= build_pointer_type (arg
);
5543 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
5544 arg
= build_pointer_type (TREE_TYPE (arg
));
5546 arg
= TYPE_MAIN_VARIANT (arg
);
5549 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, strict
,
5558 /* Fail if we've reached the end of the parm list, and more args
5559 are present, and the parm list isn't variadic. */
5560 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
5562 /* Fail if parms are left and they don't have default values. */
5564 && parms
!= void_list_node
5565 && TREE_PURPOSE (parms
) == NULL_TREE
)
5568 for (i
= 0; i
< ntparms
; i
++)
5569 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
5571 if (!allow_incomplete
)
5572 error ("incomplete type unification");
5578 /* Returns the level of DECL, which declares a template parameter. */
5581 template_decl_level (decl
)
5584 switch (TREE_CODE (decl
))
5588 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
5591 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
5594 my_friendly_abort (0);
5599 /* Tail recursion is your friend. */
5602 unify (tparms
, targs
, ntparms
, parm
, arg
, strict
, explicit_mask
)
5603 tree tparms
, targs
, parm
, arg
;
5604 int ntparms
, strict
;
5611 /* I don't think this will do the right thing with respect to types.
5612 But the only case I've seen it in so far has been array bounds, where
5613 signedness is the only information lost, and I think that will be
5615 while (TREE_CODE (parm
) == NOP_EXPR
)
5616 parm
= TREE_OPERAND (parm
, 0);
5618 if (arg
== error_mark_node
)
5620 if (arg
== unknown_type_node
)
5622 /* If PARM uses template parameters, then we can't bail out here,
5623 even in ARG == PARM, since we won't record unifications for the
5624 template parameters. We might need them if we're trying to
5625 figure out which of two things is more specialized. */
5626 if (arg
== parm
&& !uses_template_parms (parm
))
5629 /* We can't remove cv-quals when strict. */
5630 if (strict
&& TREE_CODE (arg
) == TREE_CODE (parm
)
5631 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
5632 && (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5633 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5636 switch (TREE_CODE (parm
))
5639 /* In a type which contains a nested-name-specifier, template
5640 argument values cannot be deduced for template parameters used
5641 within the nested-name-specifier. */
5644 case TEMPLATE_TYPE_PARM
:
5645 case TEMPLATE_TEMPLATE_PARM
:
5646 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5648 if (TEMPLATE_TYPE_LEVEL (parm
)
5649 != template_decl_level (tparm
))
5650 /* The PARM is not one we're trying to unify. Just check
5651 to see if it matches ARG. */
5652 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5653 && comptypes (parm
, arg
, 1)) ? 0 : 1;
5654 idx
= TEMPLATE_TYPE_IDX (parm
);
5655 targ
= TREE_VEC_ELT (targs
, idx
);
5656 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
5658 /* Check for mixed types and values. */
5659 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5660 && TREE_CODE (tparm
) != TYPE_DECL
)
5661 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
5662 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
5665 if (!strict
&& targ
!= NULL_TREE
5666 && explicit_mask
&& explicit_mask
[idx
])
5667 /* An explicit template argument. Don't even try to match
5668 here; the overload resolution code will manage check to
5669 see whether the call is legal. */
5672 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5674 if (CLASSTYPE_TEMPLATE_INFO (parm
))
5676 /* We arrive here when PARM does not involve template
5679 /* ARG must be constructed from a template class. */
5680 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
5684 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
5685 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
5686 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
5688 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
5691 /* The parameter and argument roles have to be switched here
5692 in order to handle default arguments properly. For example,
5693 template<template <class> class TT> void f(TT<int>)
5694 should be able to accept vector<int> which comes from
5695 template <class T, class Allcator = allocator>
5698 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1, 0)
5702 /* Deduce arguments T, i from TT<T> or TT<i>. */
5703 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
5705 tree t
= TREE_VEC_ELT (parmvec
, i
);
5706 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
5707 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
5708 && TREE_CODE (t
) != TEMPLATE_PARM_INDEX
)
5711 /* This argument can be deduced. */
5713 if (unify (tparms
, targs
, ntparms
, t
,
5714 TREE_VEC_ELT (argvec
, i
), strict
, explicit_mask
))
5718 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
5723 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5724 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5728 /* Template type parameters cannot contain cv-quals; i.e.
5729 template <class T> void f (T& a, T& b) will not generate
5730 void f (const int& a, const int& b). */
5731 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
5732 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
5734 arg
= TYPE_MAIN_VARIANT (arg
);
5737 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
5738 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
5739 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
5744 /* Simple cases: Value already set, does match or doesn't. */
5745 if (targ
!= NULL_TREE
5746 && (comptypes (targ
, arg
, 1)
5747 || (explicit_mask
&& explicit_mask
[idx
])))
5751 TREE_VEC_ELT (targs
, idx
) = arg
;
5754 case TEMPLATE_PARM_INDEX
:
5755 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5757 if (TEMPLATE_PARM_LEVEL (parm
)
5758 != template_decl_level (tparm
))
5759 /* The PARM is not one we're trying to unify. Just check
5760 to see if it matches ARG. */
5761 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5762 && cp_tree_equal (parm
, arg
)) ? 0 : 1;
5764 idx
= TEMPLATE_PARM_IDX (parm
);
5765 targ
= TREE_VEC_ELT (targs
, idx
);
5769 int i
= cp_tree_equal (targ
, arg
);
5775 my_friendly_abort (42);
5778 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
5782 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5783 return unify (tparms
, targs
, ntparms
, parm
,
5784 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
, explicit_mask
);
5786 if (TREE_CODE (arg
) != POINTER_TYPE
)
5788 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5789 strict
, explicit_mask
);
5791 case REFERENCE_TYPE
:
5792 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5793 arg
= TREE_TYPE (arg
);
5794 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5795 strict
, explicit_mask
);
5798 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5800 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5801 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5803 if (TYPE_DOMAIN (parm
) != NULL_TREE
5804 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5805 TYPE_DOMAIN (arg
), strict
, explicit_mask
) != 0)
5807 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5808 strict
, explicit_mask
);
5815 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5818 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5820 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5821 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5822 TYPE_MIN_VALUE (arg
), strict
, explicit_mask
))
5824 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5825 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5826 TYPE_MAX_VALUE (arg
), strict
, explicit_mask
))
5829 else if (TREE_CODE (parm
) == REAL_TYPE
5830 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5833 /* As far as unification is concerned, this wins. Later checks
5834 will invalidate it if necessary. */
5837 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5838 /* Type INTEGER_CST can come from ordinary constant template args. */
5840 while (TREE_CODE (arg
) == NOP_EXPR
)
5841 arg
= TREE_OPERAND (arg
, 0);
5843 if (TREE_CODE (arg
) != INTEGER_CST
)
5845 return !tree_int_cst_equal (parm
, arg
);
5850 if (TREE_CODE (arg
) != TREE_VEC
)
5852 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5854 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5855 if (unify (tparms
, targs
, ntparms
,
5856 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5857 strict
, explicit_mask
))
5863 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5864 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5865 arg
, strict
, explicit_mask
);
5867 /* Allow trivial conversions. */
5868 if (TREE_CODE (arg
) != RECORD_TYPE
5869 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5870 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5873 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5877 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5879 (CLASSTYPE_TEMPLATE_INFO (arg
)
5880 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5882 if (! t
|| t
== error_mark_node
)
5885 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5886 CLASSTYPE_TI_ARGS (t
), strict
, explicit_mask
);
5888 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5893 if (TREE_CODE (arg
) != METHOD_TYPE
)
5898 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5901 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5902 TREE_TYPE (arg
), strict
, explicit_mask
))
5904 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5905 TYPE_ARG_TYPES (arg
), 1,
5906 strict
, 0, explicit_mask
);
5909 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5911 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5912 TYPE_OFFSET_BASETYPE (arg
), strict
, explicit_mask
))
5914 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5915 TREE_TYPE (arg
), strict
, explicit_mask
);
5918 if (arg
!= decl_constant_value (parm
))
5923 /* Matched cases are handled by the ARG == PARM test above. */
5927 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
5929 /* We're looking at an expression. This can happen with
5933 void foo(S<I>, S<I + 2>);
5935 If the call looked like:
5937 foo(S<2>(), S<4>());
5939 we would have already matched `I' with `2'. Now, we'd
5940 like to know if `4' matches `I + 2'. So, we substitute
5941 into that expression, and fold constants, in the hope of
5944 maybe_fold_nontype_arg (tsubst_expr (parm
, targs
, NULL_TREE
));
5945 enum tree_code tc
= TREE_CODE (t
);
5947 if (tc
== MINUS_EXPR
5948 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
5949 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
5951 /* We handle this case specially, since it comes up with
5952 arrays. In particular, something like:
5954 template <int N> void f(int (&x)[N]);
5956 Here, we are trying to unify the range type, which
5957 looks like [0 ... (N - 1)]. */
5959 t1
= TREE_OPERAND (parm
, 0);
5960 t2
= TREE_OPERAND (parm
, 1);
5962 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
5966 return unify (tparms
, targs
, ntparms
, t1
, t
,
5967 strict
, explicit_mask
);
5970 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc
)))
5971 /* Good, we mangaged to simplify the exression. */
5972 return unify (tparms
, targs
, ntparms
, t
, arg
, strict
,
5975 /* Bad, we couldn't simplify this. Assume it doesn't
5980 sorry ("use of `%s' in template type unification",
5981 tree_code_name
[(int) TREE_CODE (parm
)]);
5988 mark_decl_instantiated (result
, extern_p
)
5992 if (DECL_TEMPLATE_INSTANTIATION (result
))
5993 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5995 if (TREE_CODE (result
) != FUNCTION_DECL
)
5996 /* The TREE_PUBLIC flag for function declarations will have been
5997 set correctly by tsubst. */
5998 TREE_PUBLIC (result
) = 1;
6002 DECL_INTERFACE_KNOWN (result
) = 1;
6003 DECL_NOT_REALLY_EXTERN (result
) = 1;
6005 /* For WIN32 we also want to put explicit instantiations in
6006 linkonce sections. */
6007 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
6008 make_decl_one_only (result
);
6010 else if (TREE_CODE (result
) == FUNCTION_DECL
)
6011 mark_inline_for_output (result
);
6014 /* Given two function templates PAT1 and PAT2, and explicit template
6015 arguments EXPLICIT_ARGS return:
6017 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
6018 -1 if PAT2 is more specialized than PAT1.
6019 0 if neither is more specialized. */
6022 more_specialized (pat1
, pat2
, explicit_args
)
6023 tree pat1
, pat2
, explicit_args
;
6028 targs
= get_bindings_overload (pat1
, pat2
, explicit_args
);
6034 targs
= get_bindings_overload (pat2
, pat1
, explicit_args
);
6043 /* Given two class template specialization list nodes PAT1 and PAT2, return:
6045 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
6046 -1 if PAT2 is more specialized than PAT1.
6047 0 if neither is more specialized. */
6050 more_specialized_class (pat1
, pat2
)
6056 targs
= get_class_bindings
6057 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
6058 TREE_PURPOSE (pat2
), NULL_TREE
);
6062 targs
= get_class_bindings
6063 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
6064 TREE_PURPOSE (pat1
), NULL_TREE
);
6071 /* Return the template arguments that will produce the function signature
6072 DECL from the function template FN, with the explicit template
6073 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
6077 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
6078 tree fn
, decl
, explicit_args
;
6081 int ntparms
= DECL_NTPARMS (fn
);
6082 tree targs
= make_scratch_vec (ntparms
);
6083 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
6084 tree extra_fn_arg
= NULL_TREE
;
6087 if (DECL_STATIC_FUNCTION_P (fn
)
6088 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
6090 /* Sometimes we are trying to figure out what's being
6091 specialized by a declaration that looks like a method, and it
6092 turns out to be a static member function. */
6093 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
6094 && !is_member_template (fn
))
6095 /* The natural thing to do here seems to be to remove the
6096 spurious `this' parameter from the DECL, but that prevents
6097 unification from making use of the class type. So,
6098 instead, we have fn_type_unification add to the parameters
6100 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
6102 /* In this case, though, adding the extra_fn_arg can confuse
6103 things, so we remove from decl_arg_types instead. */
6104 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
6107 i
= fn_type_unification (fn
, explicit_args
, targs
,
6109 TREE_TYPE (TREE_TYPE (decl
)),
6118 /* Check to see that the resulting return type is also OK. */
6119 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
6120 complete_template_args (fn
, targs
, 1),
6123 if (!comptypes (t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
6130 /* For most uses, we want to check the return type. */
6133 get_bindings (fn
, decl
, explicit_args
)
6134 tree fn
, decl
, explicit_args
;
6136 return get_bindings_real (fn
, decl
, explicit_args
, 1);
6139 /* But for more_specialized, we only care about the parameter types. */
6142 get_bindings_overload (fn
, decl
, explicit_args
)
6143 tree fn
, decl
, explicit_args
;
6145 return get_bindings_real (fn
, decl
, explicit_args
, 0);
6149 get_class_bindings (tparms
, parms
, args
, outer_args
)
6150 tree tparms
, parms
, args
, outer_args
;
6152 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6153 tree vec
= make_temp_vec (ntparms
);
6157 tparms
= tsubst (tparms
, outer_args
, NULL_TREE
);
6158 parms
= tsubst (parms
, outer_args
, NULL_TREE
);
6161 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
6163 switch (unify (tparms
, vec
, ntparms
,
6164 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
6174 for (i
= 0; i
< ntparms
; ++i
)
6175 if (! TREE_VEC_ELT (vec
, i
))
6181 /* Return the most specialized of the list of templates in FNS that can
6182 produce an instantiation matching DECL, given the explicit template
6183 arguments EXPLICIT_ARGS. */
6186 most_specialized (fns
, decl
, explicit_args
)
6187 tree fns
, decl
, explicit_args
;
6189 tree fn
, champ
, args
, *p
;
6192 for (p
= &fns
; *p
; )
6194 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
6197 p
= &TREE_CHAIN (*p
);
6200 *p
= TREE_CHAIN (*p
);
6207 champ
= TREE_VALUE (fn
);
6208 fn
= TREE_CHAIN (fn
);
6209 for (; fn
; fn
= TREE_CHAIN (fn
))
6211 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6218 fn
= TREE_CHAIN (fn
);
6220 return error_mark_node
;
6222 champ
= TREE_VALUE (fn
);
6226 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
6228 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6230 return error_mark_node
;
6236 /* Return the most specialized of the class template specializations in
6237 SPECS that can produce an instantiation matching ARGS. */
6240 most_specialized_class (specs
, mainargs
, outer_args
)
6241 tree specs
, mainargs
, outer_args
;
6243 tree list
= NULL_TREE
, t
, args
, champ
;
6246 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
6248 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
6249 mainargs
, outer_args
);
6252 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
6253 TREE_TYPE (list
) = TREE_TYPE (t
);
6263 for (; t
; t
= TREE_CHAIN (t
))
6265 fate
= more_specialized_class (champ
, t
);
6274 return error_mark_node
;
6280 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
6282 fate
= more_specialized_class (champ
, t
);
6284 return error_mark_node
;
6290 /* called from the parser. */
6293 do_decl_instantiation (declspecs
, declarator
, storage
)
6294 tree declspecs
, declarator
, storage
;
6296 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
6297 tree result
= NULL_TREE
;
6300 if (! DECL_LANG_SPECIFIC (decl
))
6302 cp_error ("explicit instantiation of non-template `%#D'", decl
);
6306 /* If we've already seen this template instance, use it. */
6307 if (TREE_CODE (decl
) == VAR_DECL
)
6309 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
6310 if (result
&& TREE_CODE (result
) != VAR_DECL
)
6313 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
6315 cp_error ("explicit instantiation of `%#D'", decl
);
6318 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
6323 cp_error ("no matching template for `%D' found", decl
);
6327 if (! DECL_TEMPLATE_INFO (result
))
6329 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
6333 if (flag_external_templates
)
6336 if (storage
== NULL_TREE
)
6338 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6341 cp_error ("storage class `%D' applied to template instantiation",
6344 mark_decl_instantiated (result
, extern_p
);
6345 repo_template_instantiated (result
, extern_p
);
6347 instantiate_decl (result
);
6351 mark_class_instantiated (t
, extern_p
)
6355 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
6356 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
6357 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
6358 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
6359 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
6362 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
6363 rest_of_type_compilation (t
, 1);
6368 do_type_instantiation (t
, storage
)
6375 if (TREE_CODE (t
) == TYPE_DECL
)
6378 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
6380 cp_error ("explicit instantiation of non-template type `%T'", t
);
6386 /* With -fexternal-templates, explicit instantiations are treated the same
6387 as implicit ones. */
6388 if (flag_external_templates
)
6391 if (TYPE_SIZE (t
) == NULL_TREE
)
6393 cp_error ("explicit instantiation of `%#T' before definition of template",
6398 if (storage
== NULL_TREE
)
6400 else if (storage
== ridpointers
[(int) RID_INLINE
])
6402 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6404 else if (storage
== ridpointers
[(int) RID_STATIC
])
6408 cp_error ("storage class `%D' applied to template instantiation",
6413 /* We've already instantiated this. */
6414 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
6418 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
6420 mark_class_instantiated (t
, extern_p
);
6421 repo_template_instantiated (t
, extern_p
);
6431 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6432 if (TREE_CODE (tmp
) == FUNCTION_DECL
6433 && DECL_TEMPLATE_INSTANTIATION (tmp
))
6435 mark_decl_instantiated (tmp
, extern_p
);
6436 repo_template_instantiated (tmp
, extern_p
);
6438 instantiate_decl (tmp
);
6441 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6442 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
6444 mark_decl_instantiated (tmp
, extern_p
);
6445 repo_template_instantiated (tmp
, extern_p
);
6447 instantiate_decl (tmp
);
6450 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6451 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
6452 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
6456 /* Produce the definition of D, a _DECL generated from a template. */
6459 instantiate_decl (d
)
6462 tree ti
= DECL_TEMPLATE_INFO (d
);
6463 tree tmpl
= TI_TEMPLATE (ti
);
6464 tree args
= TI_ARGS (ti
);
6466 tree decl_pattern
, code_pattern
;
6468 int nested
= in_function_p ();
6470 int pattern_defined
;
6472 char *file
= input_filename
;
6475 DECL_TEMPLATE_INSTANTIATION (td
)
6476 /* This next clause handles friend templates defined inside
6477 class templates. The friend templates are not really
6478 instantiations from the point of view of the language, but
6479 they are instantiations from the point of view of the
6481 || (DECL_TEMPLATE_INFO (td
) && !DECL_TEMPLATE_SPECIALIZATION (td
));
6483 td
= DECL_TI_TEMPLATE (td
);
6485 /* In the case of a member template, decl_pattern is the partially
6486 instantiated declaration (in the instantiated class), and code_pattern
6487 is the original template definition. */
6488 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
6489 code_pattern
= DECL_TEMPLATE_RESULT (td
);
6491 if (TREE_CODE (d
) == FUNCTION_DECL
)
6493 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
6494 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
6498 d_defined
= ! DECL_IN_AGGR_P (d
);
6499 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
6505 if (TREE_CODE (d
) == FUNCTION_DECL
)
6507 tree spec
= retrieve_specialization (tmpl
, args
);
6509 if (spec
!= NULL_TREE
6510 && DECL_TEMPLATE_SPECIALIZATION (spec
))
6514 /* This needs to happen before any tsubsting. */
6515 if (! push_tinst_level (d
))
6518 push_to_top_level ();
6519 lineno
= DECL_SOURCE_LINE (d
);
6520 input_filename
= DECL_SOURCE_FILE (d
);
6522 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
6523 variable is a static const initialized in the class body. */
6524 if (TREE_CODE (d
) == VAR_DECL
6525 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
6527 pushclass (DECL_CONTEXT (d
), 2);
6528 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6530 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, LOOKUP_NORMAL
);
6533 if (pattern_defined
)
6535 repo_template_used (d
);
6537 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
6539 if (flag_alt_external_templates
)
6541 if (interface_unknown
)
6542 warn_if_unknown_interface (d
);
6544 else if (DECL_INTERFACE_KNOWN (code_pattern
))
6546 DECL_INTERFACE_KNOWN (d
) = 1;
6547 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
6550 warn_if_unknown_interface (code_pattern
);
6554 import_export_decl (d
);
6557 /* Reject all external templates except inline functions. */
6558 if (DECL_INTERFACE_KNOWN (d
)
6559 && ! DECL_NOT_REALLY_EXTERN (d
)
6560 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
6563 /* Defer all templates except inline functions used in another function. */
6564 if (! pattern_defined
6565 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
6568 add_pending_template (d
);
6572 lineno
= DECL_SOURCE_LINE (d
);
6573 input_filename
= DECL_SOURCE_FILE (d
);
6575 /* Trick tsubst into giving us a new decl in case the template changed. */
6576 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
6577 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
6578 /* decl_pattern has all but one level of template parms bound. Only pass
6579 in that one level of args. */
6580 temp
= innermost_args (args
, DECL_TEMPLATE_SPECIALIZATION (decl_pattern
));
6581 td
= tsubst (decl_pattern
, temp
, tmpl
);
6582 SET_DECL_IMPLICIT_INSTANTIATION (td
);
6583 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
6585 /* And set up DECL_INITIAL, since tsubst doesn't. */
6586 if (TREE_CODE (td
) == VAR_DECL
)
6588 pushclass (DECL_CONTEXT (d
), 2);
6589 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6594 if (TREE_CODE (d
) == FUNCTION_DECL
)
6596 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
6598 DECL_INITIAL (td
) = error_mark_node
;
6600 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
6601 /* Set up the information about what is being specialized. */
6602 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
6604 duplicate_decls (td
, d
);
6605 if (TREE_CODE (d
) == FUNCTION_DECL
)
6606 DECL_INITIAL (td
) = 0;
6608 if (TREE_CODE (d
) == VAR_DECL
)
6610 DECL_IN_AGGR_P (d
) = 0;
6611 if (DECL_INTERFACE_KNOWN (d
))
6612 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
6615 DECL_EXTERNAL (d
) = 1;
6616 DECL_NOT_REALLY_EXTERN (d
) = 1;
6618 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
6620 else if (TREE_CODE (d
) == FUNCTION_DECL
)
6622 tree t
= DECL_SAVED_TREE (code_pattern
);
6624 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
6625 store_parm_decls ();
6627 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
6630 (TREE_OPERAND (t
, 0),
6631 tsubst_expr (TREE_OPERAND (t
, 1), args
, tmpl
));
6635 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
6637 current_member_init_list
6638 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
6639 current_base_init_list
6640 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
6645 /* Always keep the BLOCK node associated with the outermost
6646 pair of curly braces of a function. These are needed
6647 for correct operation of dwarfout.c. */
6650 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
6651 tsubst_expr (t
, args
, tmpl
);
6653 finish_function (lineno
, 0, nested
);
6658 input_filename
= file
;
6660 pop_from_top_level ();
6667 tsubst_chain (t
, argvec
)
6672 tree first
= tsubst (t
, argvec
, NULL_TREE
);
6675 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
6677 tree x
= tsubst (t
, argvec
, NULL_TREE
);
6678 TREE_CHAIN (last
) = x
;
6688 tsubst_expr_values (t
, argvec
)
6691 tree first
= NULL_TREE
;
6694 for (; t
; t
= TREE_CHAIN (t
))
6696 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
, NULL_TREE
);
6697 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, NULL_TREE
);
6698 *p
= build_tree_list (pur
, val
);
6699 p
= &TREE_CHAIN (*p
);
6710 last_tree
= TREE_CHAIN (last_tree
) = t
;
6717 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
6718 last_tree
= NULL_TREE
;
6725 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
6727 last_tree
= TREE_VALUE (saved_trees
);
6728 saved_trees
= TREE_CHAIN (saved_trees
);
6731 /* D is an undefined function declaration in the presence of templates with
6732 the same name, listed in FNS. If one of them can produce D as an
6733 instantiation, remember this so we can instantiate it at EOF if D has
6734 not been defined by that time. */
6737 add_maybe_template (d
, fns
)
6742 if (DECL_MAYBE_TEMPLATE (d
))
6745 t
= most_specialized (fns
, d
, NULL_TREE
);
6748 if (t
== error_mark_node
)
6750 cp_error ("ambiguous template instantiation for `%D'", d
);
6754 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
6755 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
6756 DECL_MAYBE_TEMPLATE (d
) = 1;
6759 /* Instantiate an enumerated type. Used by instantiate_class_template and
6763 tsubst_enum (tag
, args
, field_chain
)
6767 extern tree current_local_enum
;
6768 tree prev_local_enum
= current_local_enum
;
6770 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
6771 tree e
, values
= NULL_TREE
;
6773 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
6775 tree elt
= build_enumerator (TREE_PURPOSE (e
),
6776 tsubst_expr (TREE_VALUE (e
), args
,
6778 TREE_CHAIN (elt
) = values
;
6782 finish_enum (newtag
, values
);
6784 if (NULL
!= field_chain
)
6785 *field_chain
= grok_enum_decls (NULL_TREE
);
6787 current_local_enum
= prev_local_enum
;