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". */
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
));
81 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int));
82 static tree tsubst_enum
PROTO((tree
, tree
, int, tree
*));
83 static tree add_to_template_args
PROTO((tree
, tree
));
84 static int type_unification_real
PROTO((tree
, tree
*, tree
, tree
, int*,
86 static void note_template_header
PROTO((int));
87 static tree maybe_fold_nontype_arg
PROTO((tree
));
88 static tree convert_nontype_argument
PROTO((tree
, tree
));
90 /* Do any processing required when DECL (a member template declaration
91 using TEMPLATE_PARAMETERS as its innermost parameter list) is
92 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
93 it is a specialization, in which case the DECL itself is returned. */
96 finish_member_template_decl (template_parameters
, decl
)
97 tree template_parameters
;
100 if (template_parameters
)
103 end_specialization();
105 if (decl
&& DECL_TEMPLATE_INFO (decl
) &&
106 !DECL_TEMPLATE_SPECIALIZATION (decl
))
108 check_member_template (DECL_TI_TEMPLATE (decl
));
109 return DECL_TI_TEMPLATE (decl
);
115 cp_error ("invalid member template declaration");
119 /* Restore the template parameter context. */
122 begin_member_template_processing (decl
)
128 parms
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
130 ++processing_template_decl
;
131 current_template_parms
132 = tree_cons (build_int_2 (0, processing_template_decl
),
133 parms
, current_template_parms
);
135 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
137 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
138 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
140 switch (TREE_CODE (parm
))
149 /* Make a CONST_DECL as is done in process_template_parm. */
150 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
152 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
158 my_friendly_abort (0);
163 /* Undo the effects of begin_member_template_processing. */
166 end_member_template_processing ()
168 if (! processing_template_decl
)
171 --processing_template_decl
;
172 current_template_parms
= TREE_CHAIN (current_template_parms
);
176 /* Returns non-zero iff T is a member template function. We must be
179 template <class T> class C { void f(); }
181 Here, f is a template function, and a member, but not a member
182 template. This function does not concern itself with the origin of
183 T, only its present state. So if we have
185 template <class T> class C { template <class U> void f(U); }
187 then neither C<int>::f<char> nor C<T>::f<double> is considered
188 to be a member template. */
191 is_member_template (t
)
196 if (TREE_CODE (t
) != FUNCTION_DECL
197 && !DECL_FUNCTION_TEMPLATE_P (t
))
198 /* Anything that isn't a function or a template function is
199 certainly not a member template. */
202 if ((DECL_FUNCTION_MEMBER_P (t
)
203 && !DECL_TEMPLATE_SPECIALIZATION (t
))
204 || (TREE_CODE (t
) == TEMPLATE_DECL
&&
205 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
207 tree tmpl
= NULL_TREE
;
209 if (DECL_FUNCTION_TEMPLATE_P (t
))
211 else if (DECL_TEMPLATE_INFO (t
)
212 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
213 tmpl
= DECL_TI_TEMPLATE (t
);
217 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
218 int parm_levels
= list_length (parms
);
219 int template_class_levels
= 0;
220 tree ctx
= DECL_CLASS_CONTEXT (t
);
222 /* NB - The code below does not yet handle template class
225 template <class T> class C { template <class U> class D; }}
227 correctly. In that case, the D should have level 2. */
229 if (CLASSTYPE_TEMPLATE_INFO (ctx
))
231 tree args
= CLASSTYPE_TI_ARGS (ctx
);
234 if (args
== NULL_TREE
)
235 template_class_levels
= 1;
237 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
238 if (uses_template_parms (TREE_VEC_ELT (args
, i
)))
240 template_class_levels
++;
245 if (parm_levels
> template_class_levels
)
253 /* Return a new template argument vector which contains all of ARGS,
254 but has as its innermost set of arguments the EXTRA_ARGS. */
257 add_to_template_args (args
, extra_args
)
263 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
265 new_args
= make_tree_vec (2);
266 TREE_VEC_ELT (new_args
, 0) = args
;
272 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) - 1);
274 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
275 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
278 TREE_VEC_ELT (new_args
,
279 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
284 /* We've got a template header coming up; push to a new level for storing
288 begin_template_parm_list ()
291 declare_pseudo_global_level ();
292 ++processing_template_decl
;
293 note_template_header (0);
297 /* We've just seen template <>. */
300 begin_specialization ()
302 note_template_header (1);
306 /* Called at then end of processing a declaration preceeded by
310 end_specialization ()
312 reset_specialization ();
316 /* Any template <>'s that we have seen thus far are not referring to a
317 function specialization. */
320 reset_specialization ()
322 processing_specialization
= 0;
323 template_header_count
= 0;
327 /* We've just seen a template header. If SPECIALIZATION is non-zero,
328 it was of the form template <>. */
331 note_template_header (specialization
)
334 processing_specialization
= specialization
;
335 template_header_count
++;
339 /* We're beginning an explicit instantiation. */
342 begin_explicit_instantiation ()
344 ++processing_explicit_instantiation
;
349 end_explicit_instantiation ()
351 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
352 --processing_explicit_instantiation
;
356 /* Retrieve the specialization (in the sense of [temp.spec] - a
357 specialization is either an instantiation or an explicit
358 specialization) of TMPL for the given template ARGS. If there is
359 no such specialization, return NULL_TREE. The ARGS are a vector of
360 arguments, or a vector of vectors of arguments, in the case of
361 templates with more than one level of parameters. */
364 retrieve_specialization (tmpl
, args
)
370 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
372 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
375 if (comp_template_args (TREE_PURPOSE (s
), args
))
376 return TREE_VALUE (s
);
383 /* Register the specialization SPEC as a specialization of TMPL with
384 the indicated ARGS. */
387 register_specialization (spec
, tmpl
, args
)
394 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
396 if (TREE_CODE (spec
) != TEMPLATE_DECL
397 && list_length (DECL_TEMPLATE_PARMS (tmpl
)) > 1)
398 /* Avoid registering function declarations as
399 specializations of member templates, as would otherwise
400 happen with out-of-class specializations of member
404 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
407 if (comp_template_args (TREE_PURPOSE (s
), args
))
409 tree fn
= TREE_VALUE (s
);
411 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
413 if (DECL_TEMPLATE_INSTANTIATION (fn
))
416 || DECL_EXPLICIT_INSTANTIATION (fn
))
418 cp_error ("specialization of %D after instantiation",
424 /* This situation should occur only if the first
425 specialization is an implicit instantiation,
426 the second is an explicit specialization, and
427 the implicit instantiation has not yet been
428 used. That situation can occur if we have
429 implicitly instantiated a member function of
430 class type, and then specialized it later. */
431 TREE_VALUE (s
) = spec
;
435 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
437 if (DECL_INITIAL (fn
))
438 cp_error ("duplicate specialization of %D", fn
);
440 TREE_VALUE (s
) = spec
;
446 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
447 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
451 /* Print the list of candidate FNS in an error message. */
454 print_candidates (fns
)
459 char* str
= "candidates are:";
461 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
463 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
468 /* Returns the template (one of the functions given by TEMPLATE_ID)
469 which can be specialized to match the indicated DECL with the
470 explicit template args given in TEMPLATE_ID. If
471 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
472 member template. The template args (those explicitly specified and
473 those deduced) are output in a newly created vector *TARGS_OUT. If
474 it is impossible to determine the result, an error message is
475 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
479 determine_specialization (template_id
, decl
, targs_out
,
480 need_member_template
,
485 int need_member_template
;
488 tree fns
= TREE_OPERAND (template_id
, 0);
489 tree targs_in
= TREE_OPERAND (template_id
, 1);
490 tree templates
= NULL_TREE
;
495 *targs_out
= NULL_TREE
;
497 if (is_overloaded_fn (fns
))
498 fn
= get_first_fn (fns
);
502 overloaded
= really_overloaded_fn (fns
);
503 for (; fn
!= NULL_TREE
;
504 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
509 if (!need_member_template
510 && TREE_CODE (fn
) == FUNCTION_DECL
511 && DECL_FUNCTION_MEMBER_P (fn
)
512 && DECL_USE_TEMPLATE (fn
)
513 && DECL_TI_TEMPLATE (fn
))
514 /* We can get here when processing something like:
515 template <class T> class X { void f(); }
516 template <> void X<int>::f() {}
517 We're specializing a member function, but not a member
519 tmpl
= DECL_TI_TEMPLATE (fn
);
520 else if (TREE_CODE (fn
) != TEMPLATE_DECL
521 || (need_member_template
&& !is_member_template (fn
)))
526 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
529 if (decl
== NULL_TREE
)
531 tree targs
= make_scratch_vec (DECL_NTPARMS (tmpl
));
533 /* We allow incomplete unification here, because we are going to
534 check all the functions. */
535 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
536 &TREE_VEC_ELT (targs
, 0),
543 /* Unification was successful. */
544 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
547 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
550 if (decl
!= NULL_TREE
)
552 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
554 if (tmpl
== error_mark_node
)
556 else if (tmpl
== NULL_TREE
)
559 *targs_out
= get_bindings (tmpl
, decl
, targs_in
);
563 if (templates
== NULL_TREE
)
567 cp_error ("`%D' does not match any template declaration",
572 else if (TREE_CHAIN (templates
) != NULL_TREE
)
577 cp_error ("ambiguous template specialization `%D'",
579 print_candidates (templates
);
584 /* We have one, and exactly one, match. */
585 *targs_out
= TREE_PURPOSE (templates
);
586 return TREE_VALUE (templates
);
590 /* Check to see if the function just declared, as indicated in
591 DECLARATOR, and in DECL, is a specialization of a function
592 template. We may also discover that the declaration is an explicit
593 instantiation at this point.
595 Returns DECL, or an equivalent declaration that should be used
598 0: The function is not an explicit specialization or instantiation.
599 1: The function is an explicit specialization.
600 2: The function is an explicit instantiation.
602 FLAGS is a bitmask consisting of the following flags:
604 1: We are being called by finish_struct. (We are unable to
605 determine what template is specialized by an in-class
606 declaration until the class definition is complete, so
607 finish_struct_methods calls this function again later to finish
609 2: The function has a definition.
610 4: The function is a friend.
611 8: The function is known to be a specialization of a member
614 The TEMPLATE_COUNT is the number of references to qualifying
615 template classes that appeared in the name of the function. For
618 template <class T> struct S { void f(); };
621 the TEMPLATE_COUNT would be 1. However, explicitly specialized
622 classes are not counted in the TEMPLATE_COUNT, so that in
624 template <class T> struct S {};
625 template <> struct S<int> { void f(); }
629 the TEMPLATE_COUNT would be 0. (Note that this declaration is
630 illegal; there should be no template <>.)
632 If the function is a specialization, it is marked as such via
633 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
634 is set up correctly, and it is added to the list of specializations
635 for that template. */
638 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
644 int finish_member
= flags
& 1;
645 int have_def
= flags
& 2;
646 int is_friend
= flags
& 4;
647 int specialization
= 0;
648 int explicit_instantiation
= 0;
649 int member_specialization
= flags
& 8;
651 tree ctype
= DECL_CLASS_CONTEXT (decl
);
652 tree dname
= DECL_NAME (decl
);
656 if (processing_specialization
)
658 /* The last template header was of the form template <>. */
660 if (template_header_count
> template_count
)
662 /* There were more template headers than qualifying template
664 if (template_header_count
- template_count
> 1)
665 /* There shouldn't be that many template parameter
666 lists. There can be at most one parameter list for
667 every qualifying class, plus one for the function
669 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
671 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
673 member_specialization
= 1;
677 else if (template_header_count
== template_count
)
679 /* The counts are equal. So, this might be a
680 specialization, but it is not a specialization of a
681 member template. It might be something like
683 template <class T> struct S {
687 void S<int>::f(int i) {} */
689 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
693 /* This cannot be an explicit specialization. There are not
694 enough headers for all of the qualifying classes. For
695 example, we might have:
698 void S<int>::T<char>::f();
700 But, we're missing another template <>. */
701 cp_error("too few template parameter lists in declaration of `%D'", decl
);
705 else if (processing_explicit_instantiation
)
707 if (template_header_count
)
708 cp_error ("template parameter list used in explicit instantiation");
711 cp_error ("definition provided for explicit instantiation");
713 explicit_instantiation
= 1;
715 else if ((ctype
!= NULL_TREE
716 && !TYPE_BEING_DEFINED (ctype
)
717 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
718 || TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
720 /* The first part of the above clause catches illegal code
721 that looks like this:
723 template <class T> struct S { void f(); }
724 void S<int>::f() {} // Missing template <>
726 We disable this check when the type is being defined to
727 avoid complaining about default compiler-generated
728 constructors, destructors, and assignment operators.
729 Since the type is an instantiation, not a specialization,
730 these are the only functions that can be defined before
731 the class is complete.
733 The second part handles bogus declarations like
734 template <> template <class T>
737 if (template_header_count
> template_count
)
738 cp_error ("template-id `%D' in declaration of primary template",
741 cp_error ("explicit specialization not preceded by `template <>'");
747 if (specialization
|| member_specialization
|| explicit_instantiation
)
749 tree tmpl
= NULL_TREE
;
750 tree targs
= NULL_TREE
;
752 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
753 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
757 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
760 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
765 lookup_template_function (fns
, NULL_TREE
);
768 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
770 /* A friend declaration. We can't do much, because we don't
771 know what this resolves to, yet. */
772 my_friendly_assert (is_friend
!= 0, 0);
773 my_friendly_assert (!explicit_instantiation
, 0);
774 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
778 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
780 /* Since finish_struct_1 has not been called yet, we
781 can't call lookup_fnfields. We note that this
782 template is a specialization, and proceed, letting
783 finish_struct fix this up later. */
784 tree ti
= perm_tree_cons (NULL_TREE
,
785 TREE_OPERAND (declarator
, 1),
787 TI_PENDING_SPECIALIZATION_FLAG (ti
) = 1;
788 DECL_TEMPLATE_INFO (decl
) = ti
;
789 /* This should not be an instantiation; explicit
790 instantiation directives can only occur at the top
792 my_friendly_assert (!explicit_instantiation
, 0);
795 else if (ctype
!= NULL_TREE
796 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
799 /* Find the list of functions in ctype that have the same
800 name as the declared function. */
801 tree name
= TREE_OPERAND (declarator
, 0);
804 if (name
== constructor_name (ctype
)
805 || name
== constructor_name_full (ctype
))
807 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
809 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
810 : !TYPE_HAS_DESTRUCTOR (ctype
))
812 /* From [temp.expl.spec]:
814 If such an explicit specialization for the member
815 of a class template names an implicitly-declared
816 special member function (clause _special_), the
817 program is ill-formed.
819 Similar language is found in [temp.explicit]. */
820 cp_error ("specialization of implicitly-declared special member function");
825 fns
= TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype
),
826 is_constructor
? 0 : 1);
829 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
,
832 if (fns
== NULL_TREE
)
834 cp_error ("no member function `%s' declared in `%T'",
835 IDENTIFIER_POINTER (name
),
840 TREE_OPERAND (declarator
, 0) = fns
;
843 /* Figure out what exactly is being specialized at this point.
844 Note that for an explicit instantiation, even one for a
845 member function, we cannot tell apriori whether the the
846 instantiation is for a member template, or just a member
847 function of a template class. In particular, even in if the
848 instantiation is for a member template, the template
849 arguments could be deduced from the declaration. */
850 tmpl
= determine_specialization (declarator
, decl
,
852 member_specialization
,
857 if (explicit_instantiation
)
859 decl
= instantiate_template (tmpl
, targs
);
860 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
861 /* There doesn't seem to be anything in the draft to
862 prevent a specialization from being explicitly
863 instantiated. We're careful not to destroy the
864 information indicating that this is a
865 specialization here. */
866 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
870 /* Mangle the function name appropriately. Note that we do
871 not mangle specializations of non-template member
872 functions of template classes, e.g. with
873 template <class T> struct S { void f(); }
874 and given the specialization
875 template <> void S<int>::f() {}
876 we do not mangle S<int>::f() here. That's because it's
877 just an ordinary member function and doesn't need special
879 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
880 && name_mangling_version
>= 1)
882 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
885 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
887 hash_tree_chain (build_pointer_type (ctype
),
890 DECL_ASSEMBLER_NAME (decl
)
891 = build_template_decl_overload
894 TREE_TYPE (TREE_TYPE (tmpl
)),
895 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
896 targs
, ctype
!= NULL_TREE
);
899 if (is_friend
&& !have_def
)
901 /* This is not really a declaration of a specialization.
902 It's just the name of an instantiation. But, it's not
903 a request for an instantiation, either. */
904 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
905 DECL_TEMPLATE_INFO (decl
)
906 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
910 /* If DECL_TI_TEMPLATE (decl), the decl is an
911 instantiation of a specialization of a member template.
912 (In other words, there was a member template, in a
913 class template. That member template was specialized.
914 We then instantiated the class, so there is now an
915 instance of that specialization.)
917 According to the CD2,
919 14.7.3.13 [tmpl.expl.spec]
921 A specialization of a member function template or
922 member class template of a non-specialized class
923 template is itself a template.
925 So, we just leave the template info alone in this case. */
926 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
927 DECL_TEMPLATE_INFO (decl
)
928 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
930 register_specialization (decl
, tmpl
, targs
);
940 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
941 parameters. These are represented in the same format used for
942 DECL_TEMPLATE_PARMS. */
944 int comp_template_parms (parms1
, parms2
)
951 if (parms1
== parms2
)
954 for (p1
= parms1
, p2
= parms2
;
955 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
956 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
958 tree t1
= TREE_VALUE (p1
);
959 tree t2
= TREE_VALUE (p2
);
962 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
963 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
965 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
968 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
970 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
971 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
973 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
976 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
978 else if (!comptypes (TREE_TYPE (parm1
),
979 TREE_TYPE (parm2
), 1))
984 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
985 /* One set of parameters has more parameters lists than the
993 /* Process information from new template parameter NEXT and append it to the
997 process_template_parm (list
, next
)
1005 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1006 defval
= TREE_PURPOSE (parm
);
1007 parm
= TREE_VALUE (parm
);
1008 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1012 tree p
= TREE_VALUE (tree_last (list
));
1014 if (TREE_CODE (p
) == TYPE_DECL
)
1015 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1016 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1017 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1019 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
1028 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1029 /* is a const-param */
1030 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1031 PARM
, 0, NULL_TREE
);
1032 /* A template parameter is not modifiable. */
1033 TREE_READONLY (parm
) = 1;
1034 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1035 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1037 cp_error ("`%#T' is not a valid type for a template constant parameter",
1039 if (DECL_NAME (parm
) == NULL_TREE
)
1040 error (" a template type parameter must begin with `class' or `typename'");
1041 TREE_TYPE (parm
) = void_type_node
;
1044 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1045 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1046 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1048 tinfo
= make_node (TEMPLATE_CONST_PARM
);
1049 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
1050 if (TREE_PERMANENT (parm
) == 0)
1052 parm
= copy_node (parm
);
1053 TREE_PERMANENT (parm
) = 1;
1055 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
1056 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1057 DECL_INITIAL (decl
) = tinfo
;
1058 DECL_INITIAL (parm
) = tinfo
;
1059 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
1064 parm
= TREE_VALUE (parm
);
1066 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1068 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1069 /* This is for distinguishing between real templates and template
1070 template parameters */
1071 TREE_TYPE (parm
) = t
;
1072 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1077 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1078 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1079 decl
= build_decl (TYPE_DECL
, parm
, t
);
1082 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1083 TYPE_NAME (t
) = decl
;
1084 TYPE_STUB_DECL (t
) = decl
;
1086 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
1088 SET_DECL_ARTIFICIAL (decl
);
1090 parm
= build_tree_list (defval
, parm
);
1091 return chainon (list
, parm
);
1094 /* The end of a template parameter list has been reached. Process the
1095 tree list into a parameter vector, converting each parameter into a more
1096 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1100 end_template_parm_list (parms
)
1105 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1107 current_template_parms
1108 = tree_cons (build_int_2 (0, processing_template_decl
),
1109 saved_parmlist
, current_template_parms
);
1111 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1112 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1114 return saved_parmlist
;
1117 /* end_template_decl is called after a template declaration is seen. */
1120 end_template_decl ()
1122 reset_specialization ();
1124 if (! processing_template_decl
)
1127 /* This matches the pushlevel in begin_template_parm_list. */
1130 --processing_template_decl
;
1131 current_template_parms
= TREE_CHAIN (current_template_parms
);
1132 (void) get_pending_sizes (); /* Why? */
1135 /* Generate a valid set of template args from current_template_parms. */
1138 current_template_args ()
1140 tree header
= current_template_parms
;
1141 int length
= list_length (header
);
1142 tree args
= make_tree_vec (length
);
1147 tree a
= copy_node (TREE_VALUE (header
));
1148 int i
= TREE_VEC_LENGTH (a
);
1149 TREE_TYPE (a
) = NULL_TREE
;
1152 tree t
= TREE_VEC_ELT (a
, i
);
1154 /* t will be a list if we are called from within a
1155 begin/end_template_parm_list pair, but a vector directly
1156 if within a begin/end_member_template_processing pair. */
1157 if (TREE_CODE (t
) == TREE_LIST
)
1161 if (TREE_CODE (t
) == TYPE_DECL
1162 || TREE_CODE (t
) == TEMPLATE_DECL
)
1165 t
= DECL_INITIAL (t
);
1168 TREE_VEC_ELT (a
, i
) = t
;
1170 TREE_VEC_ELT (args
, --l
) = a
;
1171 header
= TREE_CHAIN (header
);
1178 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1179 template PARMS. Used by push_template_decl below. */
1182 build_template_decl (decl
, parms
)
1186 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1187 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1188 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1189 if (DECL_LANG_SPECIFIC (decl
))
1191 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1192 DECL_STATIC_FUNCTION_P (tmpl
) =
1193 DECL_STATIC_FUNCTION_P (decl
);
1201 push_template_decl (decl
)
1205 tree args
= NULL_TREE
;
1207 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
1211 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
1212 && DECL_CLASS_CONTEXT (decl
))
1214 /* Note that this template is a "primary template" */
1216 || (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't'
1217 && ! CLASSTYPE_TEMPLATE_INFO (ctx
))
1218 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
1221 /* Partial specialization. */
1222 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1223 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1225 tree type
= TREE_TYPE (decl
);
1226 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1227 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1228 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1230 for (; spec
; spec
= TREE_CHAIN (spec
))
1232 /* purpose: args to main template
1233 value: spec template */
1234 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1238 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1239 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1240 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1241 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1245 args
= current_template_args ();
1247 if (! ctx
|| TREE_CODE (ctx
) == FUNCTION_DECL
1248 || TYPE_BEING_DEFINED (ctx
))
1250 tmpl
= build_template_decl (decl
, current_template_parms
);
1252 if (DECL_LANG_SPECIFIC (decl
)
1253 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1255 /* A specialization of a member template of a template
1257 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1258 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1259 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1267 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1268 cp_error ("must specialize `%#T' before defining member `%#D'",
1270 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1271 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1272 else if (! DECL_TEMPLATE_INFO (decl
))
1274 cp_error ("template definition of non-template `%#D'", decl
);
1278 tmpl
= DECL_TI_TEMPLATE (decl
);
1280 if (is_member_template (tmpl
))
1282 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1283 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1287 /* The declaration is a specialization of a member
1288 template, declared outside the class. Therefore, the
1289 innermost template arguments will be NULL, so we
1290 replace them with the arguments determined by the
1291 earlier call to check_explicit_specialization. */
1292 args
= DECL_TI_ARGS (decl
);
1295 = build_template_decl (decl
, current_template_parms
);
1296 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1297 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1298 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1299 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1300 DECL_TEMPLATE_INFO (new_tmpl
) =
1301 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1303 register_specialization (new_tmpl
, tmpl
, args
);
1307 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1308 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1309 if (TREE_VEC_LENGTH (t
)
1310 != TREE_VEC_LENGTH (a
))
1312 cp_error ("got %d template parameters for `%#D'",
1313 TREE_VEC_LENGTH (a
), decl
);
1314 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1316 if (TREE_VEC_LENGTH (args
) > 1)
1317 /* Get the template parameters for the enclosing template
1319 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1324 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1328 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1330 /* When processing an inline member template of a
1331 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1332 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1333 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1335 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1336 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1338 /* There should be template arguments if and only if there is a
1340 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1343 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1345 cp_error ("got %d template parameters for `%#D'",
1346 TREE_VEC_LENGTH (a
), decl
);
1347 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1350 /* Get the innermost set of template arguments. */
1351 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1353 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1354 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1357 tmpl
= pushdecl_top_level (tmpl
);
1360 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1362 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1364 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1366 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1367 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1368 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1370 else if (! DECL_LANG_SPECIFIC (decl
))
1371 cp_error ("template declaration of `%#D'", decl
);
1373 DECL_TEMPLATE_INFO (decl
) = info
;
1377 /* Attempt to convert the non-type template parameter EXPR to the
1378 indicated TYPE. If the conversion is successful, return the
1379 converted value. If the conversion is unsuccesful, return
1380 NULL_TREE if we issued an error message, or error_mark_node if we
1381 did not. We issue error messages for out-and-out bad template
1382 parameters, but not simply because the conversion failed, since we
1383 might be just trying to do argument deduction. By the time this
1384 function is called, neither TYPE nor EXPR may make use of template
1388 convert_nontype_argument (type
, expr
)
1392 tree expr_type
= TREE_TYPE (expr
);
1394 /* A template-argument for a non-type, non-template
1395 template-parameter shall be one of:
1397 --an integral constant-expression of integral or enumeration
1400 --the name of a non-type template-parameter; or
1402 --the name of an object or function with external linkage,
1403 including function templates and function template-ids but
1404 excluding non-static class members, expressed as id-expression;
1407 --the address of an object or function with external linkage,
1408 including function templates and function template-ids but
1409 excluding non-static class members, expressed as & id-expression
1410 where the & is optional if the name refers to a function or
1413 --a pointer to member expressed as described in _expr.unary.op_. */
1415 /* An integral constant-expression can include const variables
1417 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1418 expr
= decl_constant_value (expr
);
1420 if (is_overloaded_fn (expr
))
1421 /* OK for now. We'll check that it has external linkage later.
1422 Check this first since if expr_type is the unknown_type_node
1423 we would otherwise complain below. */
1425 else if (INTEGRAL_TYPE_P (expr_type
)
1426 || TYPE_PTRMEM_P (expr_type
)
1427 || TYPE_PTRMEMFUNC_P (expr_type
)
1428 /* The next two are g++ extensions. */
1429 || TREE_CODE (expr_type
) == REAL_TYPE
1430 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1432 if (! TREE_CONSTANT (expr
))
1434 cp_error ("non-constant `%E' cannot be used as template argument",
1439 else if (TYPE_PTR_P (expr_type
)
1440 /* If expr is the address of an overloaded function, we
1441 will get the unknown_type_node at this point. */
1442 || expr_type
== unknown_type_node
)
1446 if (TREE_CODE (expr
) != ADDR_EXPR
)
1449 cp_error ("`%E' is not a valid template argument", expr
);
1450 error ("it must be %s%s with external linkage",
1451 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1452 ? "a pointer to " : "",
1453 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1454 ? "a function" : "an object");
1458 referent
= TREE_OPERAND (expr
, 0);
1459 STRIP_NOPS (referent
);
1461 if (TREE_CODE (referent
) == STRING_CST
)
1463 cp_error ("string literal %E is not a valid template argument",
1465 error ("because it is the address of an object with static linkage");
1469 if (is_overloaded_fn (referent
))
1470 /* We'll check that it has external linkage later. */
1472 else if (TREE_CODE (referent
) != VAR_DECL
)
1474 else if (!TREE_PUBLIC (referent
))
1476 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1477 return error_mark_node
;
1480 else if (TREE_CODE (expr_type
) == VAR_DECL
)
1482 if (!TREE_PUBLIC (expr
))
1487 cp_error ("object `%E' cannot be used as template argument", expr
);
1491 switch (TREE_CODE (type
))
1496 /* For a non-type template-parameter of integral or enumeration
1497 type, integral promotions (_conv.prom_) and integral
1498 conversions (_conv.integral_) are applied. */
1499 if (!INTEGRAL_TYPE_P (expr_type
))
1500 return error_mark_node
;
1502 /* It's safe to call digest_init in this case; we know we're
1503 just converting one integral constant expression to another. */
1504 return digest_init (type
, expr
, (tree
*) 0);
1508 /* These are g++ extensions. */
1509 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
1510 return error_mark_node
;
1512 return digest_init (type
, expr
, (tree
*) 0);
1516 tree type_pointed_to
= TREE_TYPE (type
);
1518 if (TYPE_PTRMEM_P (type
))
1519 /* For a non-type template-parameter of type pointer to data
1520 member, qualification conversions (_conv.qual_) are
1522 return perform_qualification_conversions (type
, expr
);
1523 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
1525 /* For a non-type template-parameter of type pointer to
1526 function, only the function-to-pointer conversion
1527 (_conv.func_) is applied. If the template-argument
1528 represents a set of overloaded functions (or a pointer to
1529 such), the matching function is selected from the set
1534 if (TREE_CODE (expr
) == ADDR_EXPR
)
1535 fns
= TREE_OPERAND (expr
, 0);
1539 fn
= instantiate_type (type_pointed_to
, fns
, 0);
1541 if (fn
== error_mark_node
)
1542 return error_mark_node
;
1544 if (!TREE_PUBLIC (fn
))
1546 if (really_overloaded_fn (fns
))
1547 return error_mark_node
;
1552 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1554 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1560 /* For a non-type template-parameter of type pointer to
1561 object, qualification conversions (_conv.qual_) and the
1562 array-to-pointer conversion (_conv.array_) are applied.
1563 [Note: In particular, neither the null pointer conversion
1564 (_conv.ptr_) nor the derived-to-base conversion
1565 (_conv.ptr_) are applied. Although 0 is a valid
1566 template-argument for a non-type template-parameter of
1567 integral type, it is not a valid template-argument for a
1568 non-type template-parameter of pointer type.]
1570 The call to decay_conversion performs the
1571 array-to-pointer conversion, if appropriate. */
1572 expr
= decay_conversion (expr
);
1574 if (expr
== error_mark_node
)
1575 return error_mark_node
;
1577 return perform_qualification_conversions (type
, expr
);
1582 case REFERENCE_TYPE
:
1584 tree type_referred_to
= TREE_TYPE (type
);
1586 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
1588 /* For a non-type template-parameter of type reference to
1589 function, no conversions apply. If the
1590 template-argument represents a set of overloaded
1591 functions, the matching function is selected from the
1592 set (_over.over_). */
1596 fn
= instantiate_type (type_referred_to
, fns
, 0);
1598 if (!TREE_PUBLIC (fn
))
1600 if (really_overloaded_fn (fns
))
1601 /* Don't issue an error here; we might get a different
1602 function if the overloading had worked out
1604 return error_mark_node
;
1609 if (fn
== error_mark_node
)
1610 return error_mark_node
;
1612 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
1619 /* For a non-type template-parameter of type reference to
1620 object, no conversions apply. The type referred to by the
1621 reference may be more cv-qualified than the (otherwise
1622 identical) type of the template-argument. The
1623 template-parameter is bound directly to the
1624 template-argument, which must be an lvalue. */
1625 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
1626 TYPE_MAIN_VARIANT (type
), 1)
1627 || (TYPE_READONLY (expr_type
) >
1628 TYPE_READONLY (type_referred_to
))
1629 || (TYPE_VOLATILE (expr_type
) >
1630 TYPE_VOLATILE (type_referred_to
))
1631 || !real_lvalue_p (expr
))
1632 return error_mark_node
;
1644 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
1646 /* For a non-type template-parameter of type pointer to member
1647 function, no conversions apply. If the template-argument
1648 represents a set of overloaded member functions, the
1649 matching member function is selected from the set
1652 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
1653 expr_type
!= unknown_type_node
)
1654 return error_mark_node
;
1656 if (TREE_CODE (expr
) == CONSTRUCTOR
)
1658 /* A ptr-to-member constant. */
1659 if (!comptypes (type
, expr_type
, 1))
1660 return error_mark_node
;
1665 if (TREE_CODE (expr
) != ADDR_EXPR
)
1666 return error_mark_node
;
1668 fns
= TREE_OPERAND (expr
, 0);
1670 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
1673 if (fn
== error_mark_node
)
1674 return error_mark_node
;
1676 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1678 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1685 /* All non-type parameters must have one of these types. */
1686 my_friendly_abort (0);
1690 return error_mark_node
;
1693 /* Convert all template arguments to their appropriate types, and return
1694 a vector containing the resulting values. If any error occurs, return
1695 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
1696 Some error messages are issued even if COMPLAIN is zero; for
1697 instance, if a template argument is composed from a local class.
1699 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
1700 provided in ARGLIST, or else trailing parameters must have default
1701 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
1702 deduction for any unspecified trailing arguments. */
1705 coerce_template_parms (parms
, arglist
, in_decl
,
1707 require_all_arguments
)
1708 tree parms
, arglist
;
1711 int require_all_arguments
;
1713 int nparms
, nargs
, i
, lost
= 0;
1714 int is_tmpl_parm
= 0;
1715 tree vec
= NULL_TREE
;
1717 if (arglist
== NULL_TREE
)
1719 else if (TREE_CODE (arglist
) == TREE_VEC
)
1720 nargs
= TREE_VEC_LENGTH (arglist
);
1722 nargs
= list_length (arglist
);
1724 nparms
= TREE_VEC_LENGTH (parms
);
1728 && require_all_arguments
1729 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
1733 error ("incorrect number of parameters (%d, should be %d)",
1737 cp_error_at ("in template expansion for decl `%D'",
1741 return error_mark_node
;
1744 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
1745 if (nargs
== nparms
)
1746 vec
= copy_node (arglist
);
1749 /* We arrive here when a template with some default arguments
1750 is used as template template argument. */
1752 vec
= make_tree_vec (nparms
);
1753 for (i
= 0; i
< nparms
; i
++)
1758 arg
= TREE_VEC_ELT (arglist
, i
);
1759 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
1761 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1764 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1767 TREE_VEC_ELT (vec
, i
) = arg
;
1772 vec
= make_tree_vec (nparms
);
1774 for (i
= 0; i
< nparms
; i
++)
1777 tree parm
= TREE_VEC_ELT (parms
, i
);
1782 arglist
= TREE_CHAIN (arglist
);
1784 if (arg
== error_mark_node
)
1787 arg
= TREE_VALUE (arg
);
1789 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
1791 my_friendly_assert (!require_all_arguments
, 0);
1794 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
1795 arg
= tsubst (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1797 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1799 TREE_VEC_ELT (vec
, i
) = arg
;
1802 for (i
= 0; i
< nparms
; i
++)
1804 tree arg
= TREE_VEC_ELT (vec
, i
);
1805 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1807 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
1809 if (arg
== NULL_TREE
)
1810 /* We're out of arguments. */
1812 my_friendly_assert (!require_all_arguments
, 0);
1816 if (arg
== error_mark_node
)
1818 cp_error ("template argument %d is invalid", i
+ 1);
1823 /* In case we are checking arguments inside a template template
1824 parameter, ARG that does not come from default argument is
1825 also a TREE_LIST node */
1826 if (TREE_CODE (arg
) == TREE_LIST
&& ! is_overloaded_fn (arg
))
1829 arg
= TREE_VALUE (arg
);
1832 /* Check if it is a class template. */
1833 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
1834 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
1835 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1836 && !CLASSTYPE_TEMPLATE_INFO (arg
));
1837 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
1838 arg
= TYPE_STUB_DECL (arg
);
1840 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
1841 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
1843 || (is_tmpl_parm
&& TREE_CODE (arg
) == TYPE_DECL
);
1844 requires_type
= TREE_CODE (parm
) == TYPE_DECL
1845 || requires_tmpl_type
;
1847 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1848 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1850 cp_pedwarn ("to refer to a type member of a template parameter,");
1851 cp_pedwarn (" use `typename %E'", arg
);
1853 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1854 TREE_OPERAND (arg
, 1));
1857 if (is_type
!= requires_type
)
1863 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1866 cp_error (" expected a constant of type `%T', got `%T'",
1868 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
1870 cp_error (" expected a type, got `%E'", arg
);
1874 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1877 if (is_tmpl_type
^ requires_tmpl_type
)
1881 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1884 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
1886 cp_error (" expected a class template, got `%T'", arg
);
1889 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1894 if (requires_tmpl_type
)
1896 cp_error ("nested template template parameter not implemented");
1898 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1905 if (requires_tmpl_type
)
1907 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
1908 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
1910 /* The parameter and argument roles have to be switched
1911 here in order to handle default arguments properly.
1913 template<template <class> class TT> void f(TT<int>)
1914 should be able to accept vector<int> which comes from
1915 template <class T, class Allcator = allocator>
1918 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1);
1919 if (val
!= error_mark_node
)
1922 /* TEMPLATE_TEMPLATE_PARM node is preferred over
1924 if (val
!= error_mark_node
1925 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
1926 val
= TREE_TYPE (val
);
1930 val
= groktypename (arg
);
1931 if (! processing_template_decl
)
1933 tree t
= target_type (val
);
1934 if (TREE_CODE (t
) != TYPENAME_TYPE
1936 && decl_function_context (TYPE_MAIN_DECL (t
)))
1938 cp_error ("type `%T' composed from a local class is not a valid template-argument",
1940 return error_mark_node
;
1947 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1948 TREE_VEC_LENGTH (vec
), in_decl
);
1950 if (processing_template_decl
)
1951 arg
= maybe_fold_nontype_arg (arg
);
1953 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
1954 /* We used to call digest_init here. However, digest_init
1955 will report errors, which we don't want when complain
1956 is zero. More importantly, digest_init will try too
1957 hard to convert things: for example, `0' should not be
1958 converted to pointer type at this point according to
1959 the standard. Accepting this is not merely an
1960 extension, since deciding whether or not these
1961 conversions can occur is part of determining which
1962 function template to call, or whether a given epxlicit
1963 argument specification is legal. */
1964 val
= convert_nontype_argument (t
, arg
);
1968 if (val
== NULL_TREE
)
1969 val
= error_mark_node
;
1970 else if (val
== error_mark_node
&& complain
)
1971 cp_error ("could not convert template argument `%E' to `%T'",
1975 if (val
== error_mark_node
)
1978 TREE_VEC_ELT (vec
, i
) = val
;
1981 return error_mark_node
;
1986 comp_template_args (oldargs
, newargs
)
1987 tree oldargs
, newargs
;
1991 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
1994 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
1996 tree nt
= TREE_VEC_ELT (newargs
, i
);
1997 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2001 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2003 if (TREE_CODE (nt
) == TREE_VEC
)
2005 /* For member templates */
2006 if (comp_template_args (nt
, ot
))
2009 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2011 if (comptypes (ot
, nt
, 1))
2014 else if (cp_tree_equal (ot
, nt
) > 0)
2021 /* Given class template name and parameter list, produce a user-friendly name
2022 for the instantiation. */
2025 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2027 tree parms
, arglist
;
2030 static struct obstack scratch_obstack
;
2031 static char *scratch_firstobj
;
2034 if (!scratch_firstobj
)
2035 gcc_obstack_init (&scratch_obstack
);
2037 obstack_free (&scratch_obstack
, scratch_firstobj
);
2038 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2041 #define buflen sizeof(buf)
2042 #define check if (bufp >= buf+buflen-1) goto too_long
2043 #define ccat(c) *bufp++=(c); check
2044 #define advance bufp+=strlen(bufp); check
2045 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2048 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2050 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2057 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2058 s
= fndecl_as_string(ctx
, 0);
2059 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2060 s
= type_as_string(ctx
, 0);
2062 my_friendly_abort (0);
2068 nparms
= TREE_VEC_LENGTH (parms
);
2069 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2070 for (i
= 0; i
< nparms
; i
++)
2072 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2073 tree arg
= TREE_VEC_ELT (arglist
, i
);
2078 if (TREE_CODE (parm
) == TYPE_DECL
)
2080 cat (type_as_string (arg
, 0));
2083 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2085 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2086 /* Already substituted with real template. Just output
2087 the template name here */
2088 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2090 /* Output the parameter declaration */
2091 cat (type_as_string (arg
, 0));
2095 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2097 if (TREE_CODE (arg
) == TREE_LIST
)
2099 /* New list cell was built because old chain link was in
2101 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2102 arg
= TREE_VALUE (arg
);
2104 /* No need to check arglist against parmlist here; we did that
2105 in coerce_template_parms, called from lookup_template_class. */
2106 cat (expr_as_string (arg
, 0));
2109 char *bufp
= obstack_next_free (&scratch_obstack
);
2111 while (bufp
[offset
- 1] == ' ')
2113 obstack_blank_fast (&scratch_obstack
, offset
);
2115 /* B<C<char> >, not B<C<char>> */
2116 if (bufp
[offset
- 1] == '>')
2121 return (char *) obstack_base (&scratch_obstack
);
2126 fatal ("out of (preallocated) string space creating template instantiation name");
2132 classtype_mangled_name (t
)
2135 if (CLASSTYPE_TEMPLATE_INFO (t
)
2136 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2138 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2139 char *mangled_name
= mangle_class_name_for_template
2140 (IDENTIFIER_POINTER (name
),
2141 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2142 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2143 tree id
= get_identifier (mangled_name
);
2144 IDENTIFIER_TEMPLATE (id
) = name
;
2148 return TYPE_IDENTIFIER (t
);
2152 add_pending_template (d
)
2157 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2158 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2160 ti
= DECL_TEMPLATE_INFO (d
);
2162 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2165 *template_tail
= perm_tree_cons
2166 (current_function_decl
, d
, NULL_TREE
);
2167 template_tail
= &TREE_CHAIN (*template_tail
);
2168 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2172 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2173 may be either a _DECL or an overloaded function or an
2174 IDENTIFIER_NODE), and ARGLIST. */
2177 lookup_template_function (fns
, arglist
)
2180 if (fns
== NULL_TREE
)
2182 cp_error ("non-template used as template");
2183 return error_mark_node
;
2186 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2187 copy_to_permanent (arglist
);
2189 return build_min (TEMPLATE_ID_EXPR
,
2191 ? TREE_TYPE (fns
) : unknown_type_node
,
2196 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2197 parameters, find the desired type.
2199 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2200 Since ARGLIST is build on the decl_obstack, we must copy it here
2201 to keep it from being reclaimed when the decl storage is reclaimed.
2203 IN_DECL, if non-NULL, is the template declaration we are trying to
2206 If the template class is really a local class in a template
2207 function, then the FUNCTION_CONTEXT is the function in which it is
2208 being instantiated. */
2211 lookup_template_class (d1
, arglist
, in_decl
, context
)
2216 tree
template, parmlist
;
2220 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2222 if (IDENTIFIER_LOCAL_VALUE (d1
)
2223 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2224 template = IDENTIFIER_LOCAL_VALUE (d1
);
2227 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
2229 template = IDENTIFIER_CLASS_VALUE (d1
);
2232 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2234 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2235 return error_mark_node
;
2236 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2237 d1
= DECL_NAME (template);
2239 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2241 template = CLASSTYPE_TI_TEMPLATE (d1
);
2242 d1
= DECL_NAME (template);
2245 my_friendly_abort (272);
2247 /* With something like `template <class T> class X class X { ... };'
2248 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2249 We don't want to do that, but we have to deal with the situation, so
2250 let's give them some syntax errors to chew on instead of a crash. */
2252 return error_mark_node
;
2253 if (TREE_CODE (template) != TEMPLATE_DECL
)
2255 cp_error ("non-template type `%T' used as a template", d1
);
2257 cp_error_at ("for template declaration `%D'", in_decl
);
2258 return error_mark_node
;
2261 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2263 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2264 template arguments */
2266 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2267 tree template2
= TYPE_STUB_DECL (parm
);
2270 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2271 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2273 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
2274 if (arglist2
== error_mark_node
)
2275 return error_mark_node
;
2277 arglist2
= copy_to_permanent (arglist2
);
2278 CLASSTYPE_TEMPLATE_INFO (parm
)
2279 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2280 TYPE_SIZE (parm
) = 0;
2283 else if (PRIMARY_TEMPLATE_P (template)
2284 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2287 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2289 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2291 if (arglist
== error_mark_node
)
2292 return error_mark_node
;
2293 if (uses_template_parms (arglist
))
2296 if (comp_template_args
2297 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2298 found
= TREE_TYPE (template);
2301 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2302 found
; found
= TREE_CHAIN (found
))
2304 if (TI_USES_TEMPLATE_PARMS (found
)
2305 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2309 found
= TREE_VALUE (found
);
2314 if (can_free (&permanent_obstack
, arglist
))
2315 obstack_free (&permanent_obstack
, arglist
);
2320 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2324 id
= get_identifier (mangled_name
);
2325 IDENTIFIER_TEMPLATE (id
) = d1
;
2327 maybe_push_to_top_level (uses_template_parms (arglist
));
2328 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2330 if (context
!= NULL_TREE
)
2332 /* Set up the context for the type_decl correctly. Note
2333 that we must clear DECL_ASSEMBLER_NAME to fool
2334 build_overload_name into creating a new name. */
2335 tree type_decl
= TYPE_STUB_DECL (t
);
2337 TYPE_CONTEXT (t
) = context
;
2338 DECL_CONTEXT (type_decl
) = context
;
2339 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2340 DECL_ASSEMBLER_NAME (type_decl
) =
2341 get_identifier (build_overload_name (t
, 1, 1));
2344 pop_from_top_level ();
2348 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
2349 arglist
, in_decl
, NULL_TREE
);
2351 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2353 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2355 int save_temp
= processing_template_decl
;
2356 processing_template_decl
= 0;
2357 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2358 processing_template_decl
= save_temp
;
2362 t
= lookup_nested_type_by_name (ctx
, id
);
2363 my_friendly_assert (t
!= NULL_TREE
, 42);
2367 /* Seems to be wanted. */
2368 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2370 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2372 arglist
= copy_to_permanent (arglist
);
2373 CLASSTYPE_TEMPLATE_INFO (t
)
2374 = perm_tree_cons (template, arglist
, NULL_TREE
);
2375 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2376 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2377 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2378 = uses_template_parms (arglist
);
2380 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2382 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2383 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2384 /* if (! uses_template_parms (arglist)) */
2385 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2386 = get_identifier (build_overload_name (t
, 1, 1));
2388 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2389 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2390 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2391 add_pending_template (t
);
2397 /* Should be defined in parse.h. */
2401 uses_template_parms (t
)
2406 switch (TREE_CODE (t
))
2410 /* We assume that the object must be instantiated in order to build
2411 the COMPONENT_REF, so we test only whether the type of the
2412 COMPONENT_REF uses template parms. */
2413 return uses_template_parms (TREE_TYPE (t
));
2415 case IDENTIFIER_NODE
:
2416 if (!IDENTIFIER_TEMPLATE (t
))
2418 my_friendly_abort (42);
2420 /* aggregates of tree nodes */
2423 int i
= TREE_VEC_LENGTH (t
);
2425 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
2430 if (uses_template_parms (TREE_PURPOSE (t
))
2431 || uses_template_parms (TREE_VALUE (t
)))
2433 return uses_template_parms (TREE_CHAIN (t
));
2435 /* constructed type nodes */
2437 case REFERENCE_TYPE
:
2438 return uses_template_parms (TREE_TYPE (t
));
2440 if (TYPE_PTRMEMFUNC_FLAG (t
))
2441 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
2443 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2445 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
2447 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2449 return uses_template_parms (TREE_TYPE (t
));
2451 if (uses_template_parms (TYPE_DOMAIN (t
)))
2453 return uses_template_parms (TREE_TYPE (t
));
2455 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
2457 return uses_template_parms (TREE_TYPE (t
));
2459 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
2461 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2463 return uses_template_parms (TREE_TYPE (t
));
2467 return uses_template_parms (TREE_TYPE (t
));
2470 /* A template template parameter is encountered */
2471 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
2472 /* We are parsing a template declaration */
2474 /* We are instantiating templates with template template
2480 /* ??? What about FIELD_DECLs? */
2481 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
2482 && uses_template_parms (DECL_TI_ARGS (t
)))
2487 if (uses_template_parms (TREE_TYPE (t
)))
2489 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
2494 return uses_template_parms (TREE_TYPE (t
));
2496 return uses_template_parms (TREE_OPERAND (t
, 0));
2498 /* template parm nodes */
2499 case TEMPLATE_TYPE_PARM
:
2500 case TEMPLATE_TEMPLATE_PARM
:
2501 case TEMPLATE_CONST_PARM
:
2504 /* simple type nodes */
2506 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
2508 return uses_template_parms (TYPE_MAX_VALUE (t
));
2520 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
2521 if (uses_template_parms (TREE_VALUE (v
)))
2533 /* Non-error_mark_node ERROR_MARKs are bad things. */
2534 my_friendly_assert (t
== error_mark_node
, 274);
2543 return uses_template_parms (TREE_OPERAND (t
, 0));
2546 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2547 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2548 return uses_template_parms (TREE_OPERAND (t
, 1));
2552 case REINTERPRET_CAST_EXPR
:
2553 case CONST_CAST_EXPR
:
2554 case STATIC_CAST_EXPR
:
2555 case DYNAMIC_CAST_EXPR
:
2563 return uses_template_parms (TREE_OPERAND (t
, 0));
2566 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
2574 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
2575 if (uses_template_parms (TREE_OPERAND (t
, i
)))
2582 sorry ("testing %s for template parms",
2583 tree_code_name
[(int) TREE_CODE (t
)]);
2584 my_friendly_abort (82);
2590 static struct tinst_level
*current_tinst_level
= 0;
2591 static struct tinst_level
*free_tinst_level
= 0;
2592 static int tinst_depth
= 0;
2593 extern int max_tinst_depth
;
2594 #ifdef GATHER_STATISTICS
2595 int depth_reached
= 0;
2599 push_tinst_level (d
)
2602 struct tinst_level
*new;
2604 if (tinst_depth
>= max_tinst_depth
)
2606 struct tinst_level
*p
= current_tinst_level
;
2608 char *file
= input_filename
;
2610 error ("template instantiation depth exceeds maximum of %d",
2612 error (" (use -ftemplate-depth-NN to increase the maximum)");
2613 cp_error (" instantiating `%D'", d
);
2615 for (; p
; p
= p
->next
)
2617 cp_error (" instantiated from `%D'", p
->decl
);
2619 input_filename
= p
->file
;
2621 error (" instantiated from here");
2624 input_filename
= file
;
2629 if (free_tinst_level
)
2631 new = free_tinst_level
;
2632 free_tinst_level
= new->next
;
2635 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
2639 new->file
= input_filename
;
2640 new->next
= current_tinst_level
;
2641 current_tinst_level
= new;
2644 #ifdef GATHER_STATISTICS
2645 if (tinst_depth
> depth_reached
)
2646 depth_reached
= tinst_depth
;
2655 struct tinst_level
*old
= current_tinst_level
;
2657 current_tinst_level
= old
->next
;
2658 old
->next
= free_tinst_level
;
2659 free_tinst_level
= old
;
2663 struct tinst_level
*
2666 struct tinst_level
*p
= current_tinst_level
;
2669 for (; p
->next
; p
= p
->next
)
2675 instantiate_class_template (type
)
2678 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
2680 if (type
== error_mark_node
)
2681 return error_mark_node
;
2683 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
2685 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
2688 template = TI_TEMPLATE (template_info
);
2689 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
2690 args
= TI_ARGS (template_info
);
2692 t
= most_specialized_class
2693 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
2695 if (t
== error_mark_node
)
2697 char *str
= "candidates are:";
2698 cp_error ("ambiguous class template instantiation for `%#T'", type
);
2699 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
2701 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
2703 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
2707 TYPE_BEING_DEFINED (type
) = 1;
2708 return error_mark_node
;
2711 pattern
= TREE_TYPE (t
);
2713 pattern
= TREE_TYPE (template);
2715 if (TYPE_SIZE (pattern
) == NULL_TREE
)
2719 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
2721 TYPE_BEING_DEFINED (type
) = 1;
2723 if (! push_tinst_level (type
))
2726 maybe_push_to_top_level (uses_template_parms (type
));
2727 pushclass (type
, 0);
2729 if (flag_external_templates
)
2731 if (flag_alt_external_templates
)
2733 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
2734 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
2735 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2736 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2737 && CLASSTYPE_INTERFACE_KNOWN (type
);
2741 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
2742 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2743 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
2744 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2745 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2746 && CLASSTYPE_INTERFACE_KNOWN (type
);
2751 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
2752 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
2755 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
2756 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
2757 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
2758 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
2759 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
2760 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
2761 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
2762 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
2763 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
2764 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
2765 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
2766 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
2767 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
2768 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
2769 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
2770 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
2771 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
2772 TYPE_USES_COMPLEX_INHERITANCE (type
)
2773 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
2774 TYPE_USES_MULTIPLE_INHERITANCE (type
)
2775 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
2776 TYPE_USES_VIRTUAL_BASECLASSES (type
)
2777 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
2778 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
2779 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
2782 tree binfo
= TYPE_BINFO (type
);
2783 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
2789 int len
= TREE_VEC_LENGTH (pbases
);
2790 bases
= make_tree_vec (len
);
2791 for (i
= 0; i
< len
; ++i
)
2795 TREE_VEC_ELT (bases
, i
) = elt
2796 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
2797 TREE_VEC_LENGTH (args
), NULL_TREE
);
2798 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
2800 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
2802 ("base type `%T' of `%T' fails to be a struct or class type",
2803 TREE_TYPE (elt
), type
);
2804 else if (! uses_template_parms (type
)
2805 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
2807 cp_error ("base class `%T' of `%T' has incomplete type",
2808 TREE_TYPE (elt
), type
);
2810 /* Don't initialize this until the vector is filled out, or
2811 lookups will crash. */
2812 BINFO_BASETYPES (binfo
) = bases
;
2816 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
2818 field_chain
= &TYPE_FIELDS (type
);
2820 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
2822 tree name
= TREE_PURPOSE (t
);
2823 tree tag
= TREE_VALUE (t
);
2825 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
2826 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
2828 tree e
, newtag
= tsubst_enum (tag
, args
,
2829 TREE_VEC_LENGTH (args
), field_chain
);
2831 while (*field_chain
)
2833 DECL_FIELD_CONTEXT (*field_chain
) = type
;
2834 field_chain
= &TREE_CHAIN (*field_chain
);
2839 TREE_VEC_LENGTH (args
), NULL_TREE
);
2842 /* Don't replace enum constants here. */
2843 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
2844 if (TREE_CODE (t
) != CONST_DECL
)
2846 tree r
= tsubst (t
, args
,
2847 TREE_VEC_LENGTH (args
), NULL_TREE
);
2848 if (TREE_CODE (r
) == VAR_DECL
)
2850 if (! uses_template_parms (r
))
2851 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
2852 /* Perhaps I should do more of grokfield here. */
2854 DECL_IN_AGGR_P (r
) = 1;
2855 DECL_EXTERNAL (r
) = 1;
2856 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
2860 field_chain
= &TREE_CHAIN (r
);
2863 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
2864 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
2866 if (DECL_CONSTRUCTOR_P (t
))
2867 grok_ctor_properties (type
, t
);
2868 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
2869 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
2872 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
2873 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
2874 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
2877 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
2878 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
2879 TREE_VEC_LENGTH (args
), NULL_TREE
);
2881 /* This does injection for friend classes. */
2882 for (; d
; d
= TREE_CHAIN (d
))
2883 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
2885 /* This does injection for friend functions. */
2886 if (!processing_template_decl
)
2888 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
2889 TREE_VEC_LENGTH (args
), NULL_TREE
);
2891 for (; d
; d
= TREE_CHAIN (d
))
2893 tree t
= TREE_VALUE (d
);
2895 if (TREE_CODE (t
) == TYPE_DECL
)
2896 /* Already injected. */;
2903 if (! uses_template_parms (type
))
2906 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
2907 if (TREE_CODE (tmp
) == FIELD_DECL
)
2909 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
2910 require_complete_type (tmp
);
2913 type
= finish_struct_1 (type
, 0);
2914 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
2916 repo_template_used (type
);
2917 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
2918 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
2922 TYPE_SIZE (type
) = integer_zero_node
;
2923 CLASSTYPE_METHOD_VEC (type
)
2924 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
2927 TYPE_BEING_DEFINED (type
) = 0;
2930 pop_from_top_level ();
2940 if (t1
== NULL_TREE
)
2941 return t2
== NULL_TREE
;
2942 if (t2
== NULL_TREE
)
2944 /* Don't care if one declares its arg const and the other doesn't -- the
2945 main variant of the arg type is all that matters. */
2946 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2947 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2949 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2953 lookup_nested_type_by_name (ctype
, name
)
2958 complete_type (ctype
);
2960 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2962 if (name
== TREE_PURPOSE (t
)
2963 /* this catches typedef enum { foo } bar; */
2964 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2965 return TREE_VALUE (t
);
2970 /* If arg is a non-type template parameter that does not depend on template
2971 arguments, fold it like we weren't in the body of a template. */
2974 maybe_fold_nontype_arg (arg
)
2977 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2978 && !uses_template_parms (arg
))
2980 /* Sometimes, one of the args was an expression involving a
2981 template constant parameter, like N - 1. Now that we've
2982 tsubst'd, we might have something like 2 - 1. This will
2983 confuse lookup_template_class, so we do constant folding
2984 here. We have to unset processing_template_decl, to
2985 fool build_expr_from_tree() into building an actual
2988 int saved_processing_template_decl
= processing_template_decl
;
2989 processing_template_decl
= 0;
2990 arg
= fold (build_expr_from_tree (arg
));
2991 processing_template_decl
= saved_processing_template_decl
;
2996 /* Take the tree structure T and replace template parameters used therein
2997 with the argument vector ARGS. NARGS is the number of args; should
2998 be removed. IN_DECL is an associated decl for diagnostics.
3000 tsubst is used for dealing with types, decls and the like; for
3001 expressions, use tsubst_expr or tsubst_copy. */
3004 tsubst (t
, args
, nargs
, in_decl
)
3011 if (t
== NULL_TREE
|| t
== error_mark_node
3012 || t
== integer_type_node
3013 || t
== void_type_node
3014 || t
== char_type_node
)
3017 type
= TREE_TYPE (t
);
3018 if (type
== unknown_type_node
)
3019 my_friendly_abort (42);
3020 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3021 && TREE_CODE (t
) != TYPENAME_TYPE
)
3022 type
= tsubst (type
, args
, nargs
, in_decl
);
3024 switch (TREE_CODE (t
))
3027 if (TYPE_PTRMEMFUNC_P (t
))
3029 tree r
= build_ptrmemfunc_type
3030 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
3031 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3035 /* else fall through */
3037 if (uses_template_parms (t
))
3039 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
3045 ? tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
) : NULL_TREE
;
3047 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3049 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3053 /* else fall through */
3055 case IDENTIFIER_NODE
:
3068 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3069 if (ctx
== NULL_TREE
)
3071 else if (ctx
== current_function_decl
)
3072 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3074 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3078 if (t
== integer_type_node
)
3081 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3082 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3086 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3087 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
3088 if (processing_template_decl
)
3090 tree itype
= make_node (INTEGER_TYPE
);
3091 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3092 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3097 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3098 return build_index_2_type (size_zero_node
, max
);
3101 case TEMPLATE_TYPE_PARM
:
3102 case TEMPLATE_TEMPLATE_PARM
:
3103 case TEMPLATE_CONST_PARM
:
3108 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3109 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3111 idx
= TEMPLATE_TYPE_IDX (t
);
3112 level
= TEMPLATE_TYPE_LEVEL (t
);
3116 idx
= TEMPLATE_CONST_IDX (t
);
3117 level
= TEMPLATE_CONST_LEVEL (t
);
3120 if (TREE_VEC_LENGTH (args
) > 0)
3122 tree arg
= NULL_TREE
;
3124 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3126 if (TREE_VEC_LENGTH (args
) >= level
- 1)
3128 (TREE_VEC_ELT (args
, level
- 1), idx
);
3130 else if (level
== 1)
3131 arg
= TREE_VEC_ELT (args
, idx
);
3133 if (arg
!= NULL_TREE
)
3135 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3136 return cp_build_type_variant
3137 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3138 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3139 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3141 if (CLASSTYPE_TEMPLATE_INFO (t
))
3143 /* We are processing a type constructed from
3144 a template template parameter */
3145 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3146 args
, nargs
, in_decl
);
3149 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3150 we are resolving nested-types in the signature of
3151 a member function templates.
3152 Otherwise ARG is a TEMPLATE_DECL and is the real
3153 template to be instantiated. */
3154 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3155 arg
= TYPE_NAME (arg
);
3157 r
= lookup_template_class (DECL_NAME (arg
),
3159 DECL_CONTEXT (arg
));
3160 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3164 /* We are processing a template argument list. */
3172 /* If we get here, we must have been looking at a parm for a
3173 more deeply nested template. */
3174 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
3175 && TEMPLATE_CONST_LEVEL (t
) > 1)
3176 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3177 && TEMPLATE_TYPE_LEVEL (t
) > 1)
3178 || (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3179 && TEMPLATE_TYPE_LEVEL (t
) > 1),
3186 /* We can get here when processing a member template function
3187 of a template class. */
3189 tree decl
= DECL_TEMPLATE_RESULT (t
);
3195 /* We might already have an instance of this template. */
3196 spec
= retrieve_specialization (t
, args
);
3197 if (spec
!= NULL_TREE
)
3200 /* Make a new template decl. It will be similar to the
3201 original, but will record the current template arguments.
3202 We also create a new function declaration, which is just
3203 like the old one, but points to this new template, rather
3204 than the old one. */
3205 tmpl
= copy_node (t
);
3206 copy_lang_decl (tmpl
);
3207 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3208 DECL_CHAIN (tmpl
) = NULL_TREE
;
3209 TREE_CHAIN (tmpl
) = NULL_TREE
;
3210 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
3211 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
3212 DECL_RESULT (tmpl
) = new_decl
;
3213 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
3214 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3215 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
3216 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
3218 /* The template parameters for this new template are all the
3219 template parameters for the old template, except the
3220 outermost level of parameters. */
3221 DECL_TEMPLATE_PARMS (tmpl
)
3222 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
3223 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
3224 TREE_CHAIN (parms
) != NULL_TREE
;
3225 parms
= TREE_CHAIN (parms
))
3226 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
3228 /* What should we do with the specializations of this member
3229 template? Are they specializations of this new template,
3230 or instantiations of the templates they previously were?
3231 this new template? And where should their
3232 DECL_TI_TEMPLATES point? */
3233 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
3234 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
3236 spec
= TREE_CHAIN (spec
))
3238 /* It helps to consider example here. Consider:
3249 Now, for example, we are instantiating S<int>::f(U u).
3250 We want to make a template:
3255 It will have a specialization, for the case U = int*, of
3259 void S<int>::f<int*>(int*);
3261 This specialization will be an instantiation of
3262 the specialization given in the declaration of S, with
3263 argument list int*. */
3265 tree fn
= TREE_VALUE (spec
);
3269 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
3270 /* Instantiations are on the same list, but they're of
3271 no concern to us. */
3274 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
, nargs
,
3276 new_fn
= tsubst (DECL_RESULT (fn
), args
, nargs
,
3278 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
3279 perm_tree_cons (spec_args
, new_fn
,
3280 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
3283 /* Record this partial instantiation. */
3284 register_specialization (tmpl
, t
, args
);
3296 if (DECL_CONTEXT (t
) != NULL_TREE
3297 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
3299 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
3303 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
3304 type
= tsubst (type
, args
, nargs
, in_decl
);
3310 type
= tsubst (type
, args
, nargs
, in_decl
);
3313 /* Do we already have this instantiation? */
3314 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3316 tree tmpl
= DECL_TI_TEMPLATE (t
);
3317 tree spec
= retrieve_specialization (tmpl
, args
);
3323 /* We do NOT check for matching decls pushed separately at this
3324 point, as they may not represent instantiations of this
3325 template, and in any case are considered separate under the
3326 discrete model. Instead, see add_maybe_template. */
3330 DECL_USE_TEMPLATE (r
) = 0;
3331 TREE_TYPE (r
) = type
;
3334 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
3335 DECL_CLASS_CONTEXT (r
) = ctx
;
3337 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
3338 IDENTIFIER_POINTER (DECL_NAME (r
)),
3339 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
3341 /* Type-conversion operator. Reconstruct the name, in
3342 case it's the name of one of the template's parameters. */
3343 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
3346 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
3348 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
3349 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
3350 buf
= (char *) alloca (strlen (dbuf
)
3351 + sizeof (DESTRUCTOR_DECL_PREFIX
));
3352 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
3355 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
3359 /* Instantiations of template functions must be mangled
3360 specially, in order to conform to 14.5.5.1
3361 [temp.over.link]. We use in_decl below rather than
3362 DECL_TI_TEMPLATE (r) because the latter is set to
3363 NULL_TREE in instantiate_decl. */
3367 if (DECL_TEMPLATE_INFO (r
))
3368 tmpl
= DECL_TI_TEMPLATE (r
);
3372 /* tmpl will be NULL if this is a specialization of a
3373 member template of a template class. */
3374 if (name_mangling_version
< 1
3375 || tmpl
== NULL_TREE
3376 || (member
&& !is_member_template (tmpl
)
3377 && !DECL_TEMPLATE_INFO (tmpl
)))
3379 arg_types
= TYPE_ARG_TYPES (type
);
3380 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3381 arg_types
= hash_tree_chain
3382 (build_pointer_type (DECL_CONTEXT (r
)),
3385 DECL_ASSEMBLER_NAME (r
)
3386 = build_decl_overload (DECL_NAME (r
), arg_types
,
3391 /* We pass the outermost template parameters to
3392 build_template_decl_overload since the innermost
3393 template parameters are still just template
3394 parameters; there are no corresponding subsitution
3396 /* FIXME The messed up thing here is that we get here with
3397 full args and only one level of parms. This is necessary
3398 because when we partially instantiate a member template,
3399 even though there's really only one level of parms left
3400 we re-use the parms from the original template, which
3401 have level 2. When this is fixed we can remove the
3402 add_to_template_args from instantiate_template. */
3406 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3408 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
3410 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
3411 tparms
= TREE_CHAIN (tparms
);
3414 (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
3415 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1)
3420 /* If the template is a specialization, then it is
3421 a member template specialization. We have
3424 template <class T> struct S {
3425 template <int i> void f();
3426 template <> void f<7>();
3429 and now we are forming S<double>::f<7>.
3430 Therefore, the template parameters of interest
3431 are those that are specialized by the template
3432 (i.e., the int), not those we are using to
3433 instantiate the template, i.e. the double. */
3434 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
3435 targs
= DECL_TI_ARGS (tmpl
);
3438 my_friendly_assert (tparms
!= NULL_TREE
3439 && TREE_CODE (tparms
) == TREE_LIST
,
3441 tparms
= TREE_VALUE (tparms
);
3443 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
3444 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3445 arg_types
= hash_tree_chain
3446 (build_pointer_type (DECL_CONTEXT (r
)),
3449 DECL_ASSEMBLER_NAME (r
)
3450 = build_template_decl_overload
3451 (DECL_NAME (r
), arg_types
,
3452 TREE_TYPE (TREE_TYPE (tmpl
)),
3453 tparms
, targs
, member
);
3457 make_decl_rtl (r
, NULL_PTR
, 1);
3459 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
3460 DECL_MAIN_VARIANT (r
) = r
;
3461 DECL_RESULT (r
) = NULL_TREE
;
3462 DECL_INITIAL (r
) = NULL_TREE
;
3464 TREE_STATIC (r
) = 0;
3465 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
3466 DECL_EXTERNAL (r
) = 1;
3467 DECL_INTERFACE_KNOWN (r
) = 0;
3468 DECL_DEFER_OUTPUT (r
) = 0;
3469 TREE_CHAIN (r
) = NULL_TREE
;
3470 DECL_CHAIN (r
) = NULL_TREE
;
3472 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
3473 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
3475 /* Look for matching decls for the moment. */
3476 if (! member
&& ! flag_ansi_overloading
)
3478 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
3481 if (decls
== NULL_TREE
)
3483 else if (is_overloaded_fn (decls
))
3484 for (decls
= get_first_fn (decls
); decls
;
3485 decls
= DECL_CHAIN (decls
))
3487 if (TREE_CODE (decls
) == FUNCTION_DECL
3488 && TREE_TYPE (decls
) == type
)
3497 int dcl_only
= ! DECL_INITIAL (d
);
3499 DECL_INITIAL (r
) = error_mark_node
;
3500 duplicate_decls (r
, d
);
3503 DECL_INITIAL (r
) = 0;
3507 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3509 tree tmpl
= DECL_TI_TEMPLATE (t
);
3510 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3512 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3514 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3515 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
3517 /* In this case, we are instantiating a
3518 specialization. The innermost template args are
3519 already given by the specialization. */
3520 argvec
= add_to_template_args (argvec
, DECL_TI_ARGS (tmpl
));
3523 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3525 /* If we're not using ANSI overloading, then we might have
3526 called duplicate_decls above, and gotten back an
3527 preexisting version of this function. We treat such a
3528 function as a specialization. Otherwise, we cleared
3529 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
3530 this condition will be false. */
3531 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
3532 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
3534 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3536 register_specialization (r
, tmpl
, argvec
);
3539 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
3540 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
3542 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
3543 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
3550 tree r
= copy_node (t
);
3551 TREE_TYPE (r
) = type
;
3552 DECL_INITIAL (r
) = TREE_TYPE (r
);
3553 DECL_CONTEXT (r
) = NULL_TREE
;
3554 #ifdef PROMOTE_PROTOTYPES
3555 if ((TREE_CODE (type
) == INTEGER_TYPE
3556 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3557 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
3558 DECL_ARG_TYPE (r
) = integer_type_node
;
3561 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
3567 tree r
= copy_node (t
);
3568 TREE_TYPE (r
) = type
;
3571 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
3573 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3574 TREE_CHAIN (r
) = NULL_TREE
;
3580 tree r
= copy_node (t
);
3582 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3583 TREE_CHAIN (r
) = NULL_TREE
;
3590 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
3592 /* Do we already have this instantiation? */
3593 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3595 tree tmpl
= DECL_TI_TEMPLATE (t
);
3596 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3598 for (; decls
; decls
= TREE_CHAIN (decls
))
3599 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
3600 return TREE_VALUE (decls
);
3604 TREE_TYPE (r
) = type
;
3605 DECL_CONTEXT (r
) = ctx
;
3606 if (TREE_STATIC (r
))
3607 DECL_ASSEMBLER_NAME (r
)
3608 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
3610 /* Don't try to expand the initializer until someone tries to use
3611 this variable; otherwise we run into circular dependencies. */
3612 DECL_INITIAL (r
) = NULL_TREE
;
3617 if (DECL_LANG_SPECIFIC (r
))
3620 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
3623 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3625 tree tmpl
= DECL_TI_TEMPLATE (t
);
3626 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3627 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3629 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3630 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
3631 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3633 TREE_CHAIN (r
) = NULL_TREE
;
3638 if (t
== TYPE_NAME (TREE_TYPE (t
)))
3639 return TYPE_NAME (type
);
3642 tree r
= copy_node (t
);
3643 TREE_TYPE (r
) = type
;
3644 DECL_CONTEXT (r
) = current_class_type
;
3645 TREE_CHAIN (r
) = NULL_TREE
;
3651 tree purpose
, value
, chain
, result
;
3652 int via_public
, via_virtual
, via_protected
;
3654 if (t
== void_list_node
)
3657 via_public
= TREE_VIA_PUBLIC (t
);
3658 via_protected
= TREE_VIA_PROTECTED (t
);
3659 via_virtual
= TREE_VIA_VIRTUAL (t
);
3661 purpose
= TREE_PURPOSE (t
);
3663 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
3664 value
= TREE_VALUE (t
);
3666 value
= tsubst (value
, args
, nargs
, in_decl
);
3667 chain
= TREE_CHAIN (t
);
3668 if (chain
&& chain
!= void_type_node
)
3669 chain
= tsubst (chain
, args
, nargs
, in_decl
);
3670 if (purpose
== TREE_PURPOSE (t
)
3671 && value
== TREE_VALUE (t
)
3672 && chain
== TREE_CHAIN (t
))
3674 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
3675 purpose
, value
, chain
);
3676 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
3680 if (type
!= NULL_TREE
)
3686 if (type
== TREE_TYPE (t
))
3689 TREE_TYPE (t
) = complete_type (type
);
3690 if (IS_AGGR_TYPE (type
))
3692 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
3693 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
3694 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
3695 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
3700 /* Otherwise, a vector of template arguments. */
3702 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
3703 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
3705 bzero ((char *) elts
, len
* sizeof (tree
));
3707 for (i
= 0; i
< len
; i
++)
3709 elts
[i
] = maybe_fold_nontype_arg
3710 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
));
3712 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
3719 t
= make_tree_vec (len
);
3720 for (i
= 0; i
< len
; i
++)
3721 TREE_VEC_ELT (t
, i
) = elts
[i
];
3726 case REFERENCE_TYPE
:
3729 enum tree_code code
;
3731 if (type
== TREE_TYPE (t
))
3734 code
= TREE_CODE (t
);
3735 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3737 static int last_line
= 0;
3738 static char* last_file
= 0;
3740 /* We keep track of the last time we issued this error
3741 message to avoid spewing a ton of messages during a
3742 single bad template instantiation. */
3743 if (last_line
!= lineno
||
3744 last_file
!= input_filename
)
3746 cp_error ("cannot form type %s to reference type %T during template instantiation",
3747 (code
== POINTER_TYPE
) ? "pointer" : "reference",
3750 last_file
= input_filename
;
3753 /* Use the underlying type in an attempt at error
3754 recovery; maybe the user meant vector<int> and wrote
3755 vector<int&>, or some such. */
3756 if (code
== REFERENCE_TYPE
)
3759 r
= build_pointer_type (TREE_TYPE (type
));
3761 else if (code
== POINTER_TYPE
)
3762 r
= build_pointer_type (type
);
3764 r
= build_reference_type (type
);
3765 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
3767 /* Will this ever be needed for TYPE_..._TO values? */
3772 return build_offset_type
3773 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
3777 tree values
= TYPE_ARG_TYPES (t
);
3778 tree context
= TYPE_CONTEXT (t
);
3779 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
3782 /* Don't bother recursing if we know it won't change anything. */
3783 if (values
!= void_list_node
)
3785 /* This should probably be rewritten to use hash_tree_cons for
3786 the memory savings. */
3787 tree first
= NULL_TREE
;
3790 for (; values
&& values
!= void_list_node
;
3791 values
= TREE_CHAIN (values
))
3793 tree value
= TYPE_MAIN_VARIANT (type_decays_to
3794 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
3795 /* Don't instantiate default args unless they are used.
3796 Handle it in build_over_call instead. */
3797 tree purpose
= TREE_PURPOSE (values
);
3798 tree x
= build_tree_list (purpose
, value
);
3801 TREE_CHAIN (last
) = x
;
3807 if (values
== void_list_node
)
3808 TREE_CHAIN (last
) = void_list_node
;
3813 context
= tsubst (context
, args
, nargs
, in_decl
);
3814 /* Could also optimize cases where return value and
3815 values have common elements (e.g., T min(const &T, const T&). */
3817 /* If the above parameters haven't changed, just return the type. */
3818 if (type
== TREE_TYPE (t
)
3819 && values
== TYPE_VALUES (t
)
3820 && context
== TYPE_CONTEXT (t
))
3823 /* Construct a new type node and return it. */
3824 if (TREE_CODE (t
) == FUNCTION_TYPE
3825 && context
== NULL_TREE
)
3827 fntype
= build_function_type (type
, values
);
3829 else if (context
== NULL_TREE
)
3831 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
3832 args
, nargs
, in_decl
);
3833 fntype
= build_cplus_method_type (base
, type
,
3834 TREE_CHAIN (values
));
3838 fntype
= make_node (TREE_CODE (t
));
3839 TREE_TYPE (fntype
) = type
;
3840 TYPE_CONTEXT (fntype
) = context
;
3841 TYPE_VALUES (fntype
) = values
;
3842 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
3843 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
3844 TYPE_MODE (fntype
) = TYPE_MODE (t
);
3845 if (TYPE_METHOD_BASETYPE (t
))
3846 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
3847 args
, nargs
, in_decl
);
3848 /* Need to generate hash value. */
3849 my_friendly_abort (84);
3851 fntype
= build_type_variant (fntype
,
3856 raises
= tsubst (raises
, args
, nargs
, in_decl
);
3857 fntype
= build_exception_variant (fntype
, raises
);
3863 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
3865 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
3867 r
= build_cplus_array_type (type
, domain
);
3873 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
3874 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3875 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
3879 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
3880 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
3884 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3885 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
3886 return cp_build_type_variant
3887 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
3888 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
3892 return make_pointer_declarator
3893 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3896 return make_reference_declarator
3897 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3900 return build_parse_node
3901 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3902 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3905 return make_call_declarator
3906 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3907 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3908 TREE_OPERAND (t
, 2),
3909 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3912 return build_parse_node
3913 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3914 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3917 sorry ("use of `%s' in template",
3918 tree_code_name
[(int) TREE_CODE (t
)]);
3919 return error_mark_node
;
3926 emit_line_note (input_filename
, lineno
);
3930 expand_start_bindings (0);
3937 int saved_warn_unused
;
3939 if (processing_template_decl
)
3941 saved_warn_unused
= warn_unused
;
3944 expand_end_bindings (getdecls (), kept_level_p (), 0);
3945 if (processing_template_decl
)
3946 warn_unused
= saved_warn_unused
;
3947 t
= poplevel (kept_level_p (), 1, 0);
3952 /* Like tsubst, but deals with expressions. This function just replaces
3953 template parms; to finish processing the resultant expression, use
3957 tsubst_copy (t
, args
, nargs
, in_decl
)
3962 enum tree_code code
;
3964 if (t
== NULL_TREE
|| t
== error_mark_node
)
3967 code
= TREE_CODE (t
);
3972 return do_identifier (DECL_NAME (t
), 0);
3976 if (DECL_CONTEXT (t
))
3978 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
3979 if (ctx
== current_function_decl
)
3980 return lookup_name (DECL_NAME (t
), 0);
3981 else if (ctx
!= DECL_CONTEXT (t
))
3982 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
3988 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3989 t
= tsubst (t
, args
, nargs
, in_decl
);
3994 if (is_member_template (t
))
3995 return tsubst (t
, args
, nargs
, in_decl
);
4000 case IDENTIFIER_NODE
:
4001 return do_identifier (t
, 0);
4005 case REINTERPRET_CAST_EXPR
:
4006 case CONST_CAST_EXPR
:
4007 case STATIC_CAST_EXPR
:
4008 case DYNAMIC_CAST_EXPR
:
4010 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
4011 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4014 case PREDECREMENT_EXPR
:
4015 case PREINCREMENT_EXPR
:
4016 case POSTDECREMENT_EXPR
:
4017 case POSTINCREMENT_EXPR
:
4019 case TRUTH_NOT_EXPR
:
4022 case CONVERT_EXPR
: /* Unary + */
4030 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4035 case TRUNC_DIV_EXPR
:
4037 case FLOOR_DIV_EXPR
:
4038 case ROUND_DIV_EXPR
:
4039 case EXACT_DIV_EXPR
:
4041 case BIT_ANDTC_EXPR
:
4044 case TRUNC_MOD_EXPR
:
4045 case FLOOR_MOD_EXPR
:
4046 case TRUTH_ANDIF_EXPR
:
4047 case TRUTH_ORIF_EXPR
:
4048 case TRUTH_AND_EXPR
:
4069 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4070 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4074 tree fn
= TREE_OPERAND (t
, 0);
4075 if (really_overloaded_fn (fn
))
4076 fn
= tsubst_copy (get_first_fn (fn
), args
, nargs
, in_decl
);
4078 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
4080 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4084 case METHOD_CALL_EXPR
:
4086 tree name
= TREE_OPERAND (t
, 0);
4087 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4089 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4090 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4092 else if (TREE_CODE (name
) == SCOPE_REF
4093 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4095 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4096 name
= TREE_OPERAND (name
, 1);
4097 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4098 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4099 name
= build_nt (SCOPE_REF
, base
, name
);
4102 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4104 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4105 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
4112 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4113 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4114 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4119 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4120 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4121 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4122 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4129 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4130 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4131 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4132 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4136 case TEMPLATE_ID_EXPR
:
4138 /* Substituted template arguments */
4139 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4141 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4142 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4144 return lookup_template_function
4145 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
), targs
);
4150 tree purpose
, value
, chain
;
4152 if (t
== void_list_node
)
4155 purpose
= TREE_PURPOSE (t
);
4157 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
4158 value
= TREE_VALUE (t
);
4160 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
4161 chain
= TREE_CHAIN (t
);
4162 if (chain
&& chain
!= void_type_node
)
4163 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
4164 if (purpose
== TREE_PURPOSE (t
)
4165 && value
== TREE_VALUE (t
)
4166 && chain
== TREE_CHAIN (t
))
4168 return tree_cons (purpose
, value
, chain
);
4175 case TEMPLATE_TYPE_PARM
:
4176 case TEMPLATE_TEMPLATE_PARM
:
4177 case TEMPLATE_CONST_PARM
:
4179 case REFERENCE_TYPE
:
4185 return tsubst (t
, args
, nargs
, in_decl
);
4187 case IDENTIFIER_NODE
:
4188 if (IDENTIFIER_TYPENAME_P (t
))
4189 return build_typename_overload
4190 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
4196 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
4197 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
4204 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
4207 tsubst_expr (t
, args
, nargs
, in_decl
)
4212 if (t
== NULL_TREE
|| t
== error_mark_node
)
4215 if (processing_template_decl
)
4216 return tsubst_copy (t
, args
, nargs
, in_decl
);
4218 switch (TREE_CODE (t
))
4221 lineno
= TREE_COMPLEXITY (t
);
4222 emit_line_note (input_filename
, lineno
);
4224 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4229 lineno
= TREE_COMPLEXITY (t
);
4230 emit_line_note (input_filename
, lineno
);
4231 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4232 /* Do default conversion if safe and possibly important,
4233 in case within ({...}). */
4234 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
4235 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
4236 t
= default_conversion (t
);
4237 cplus_expand_expr_stmt (t
);
4244 int i
= suspend_momentary ();
4247 lineno
= TREE_COMPLEXITY (t
);
4248 emit_line_note (input_filename
, lineno
);
4250 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4251 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4252 TREE_OPERAND (t
, 2) != 0);
4253 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4255 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
4256 resume_momentary (i
);
4263 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
4264 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4265 int cond_scope
= (TREE_OPERAND (t
, 1)
4266 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
4268 lineno
= TREE_COMPLEXITY (t
);
4269 emit_line_note (input_filename
, lineno
);
4272 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
4273 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4275 emit_line_note (input_filename
, lineno
);
4276 expand_start_loop_continue_elsewhere (1);
4280 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4281 emit_line_note (input_filename
, lineno
);
4283 expand_exit_loop_if_false (0, condition_conversion (tmp
));
4287 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
4290 emit_line_note (input_filename
, lineno
);
4291 expand_loop_continue_here ();
4292 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4294 cplus_expand_expr_stmt (tmp
);
4307 lineno
= TREE_COMPLEXITY (t
);
4309 emit_line_note (input_filename
, lineno
);
4310 expand_start_loop (1);
4312 cond
= TREE_OPERAND (t
, 0);
4313 if (TREE_CODE (cond
) == DECL_STMT
)
4315 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
4316 emit_line_note (input_filename
, lineno
);
4317 expand_exit_loop_if_false (0, condition_conversion (cond
));
4319 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
4321 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4333 lineno
= TREE_COMPLEXITY (t
);
4335 emit_line_note (input_filename
, lineno
);
4336 expand_start_loop_continue_elsewhere (1);
4338 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4339 expand_loop_continue_here ();
4341 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4342 emit_line_note (input_filename
, lineno
);
4343 expand_exit_loop_if_false (0, condition_conversion (cond
));
4354 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4356 lineno
= TREE_COMPLEXITY (t
);
4359 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4360 emit_line_note (input_filename
, lineno
);
4361 expand_start_cond (condition_conversion (tmp
), 0);
4363 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4364 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4366 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
4368 expand_start_else ();
4369 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4383 tree substmt
= TREE_OPERAND (t
, 0);
4385 lineno
= TREE_COMPLEXITY (t
);
4387 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4390 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
4391 tsubst_expr (substmt
, args
, nargs
, in_decl
);
4393 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4399 lineno
= TREE_COMPLEXITY (t
);
4400 emit_line_note (input_filename
, lineno
);
4401 if (! expand_exit_something ())
4402 error ("break statement not within loop or switch");
4406 lineno
= TREE_COMPLEXITY (t
);
4407 emit_line_note (input_filename
, lineno
);
4408 if (! expand_continue_loop (0))
4409 error ("continue statement not within a loop");
4415 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4417 lineno
= TREE_COMPLEXITY (t
);
4420 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4421 emit_line_note (input_filename
, lineno
);
4422 c_expand_start_case (val
);
4425 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4426 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4428 expand_end_case (val
);
4439 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4440 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4444 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
4451 lineno
= TREE_COMPLEXITY (t
);
4452 emit_line_note (input_filename
, lineno
);
4453 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
4455 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
4456 TREE_USED (decl
) = 1;
4460 expand_computed_goto
4461 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4465 lineno
= TREE_COMPLEXITY (t
);
4466 emit_line_note (input_filename
, lineno
);
4467 expand_start_try_stmts ();
4468 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4469 expand_start_all_catch ();
4471 tree handler
= TREE_OPERAND (t
, 1);
4472 for (; handler
; handler
= TREE_CHAIN (handler
))
4473 tsubst_expr (handler
, args
, nargs
, in_decl
);
4475 expand_end_all_catch ();
4479 lineno
= TREE_COMPLEXITY (t
);
4481 if (TREE_OPERAND (t
, 0))
4483 tree d
= TREE_OPERAND (t
, 0);
4484 expand_start_catch_block
4485 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
4486 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
4489 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
4490 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4491 expand_end_catch_block ();
4496 lineno
= TREE_COMPLEXITY (t
);
4498 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
4499 tsubst_enum (t
, args
, nargs
, NULL
);
4503 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
4509 instantiate_template (tmpl
, targ_ptr
)
4510 tree tmpl
, targ_ptr
;
4514 struct obstack
*old_fmp_obstack
;
4515 extern struct obstack
*function_maybepermanent_obstack
;
4517 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
4519 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
4521 /* Check to see if we already have this specialization. */
4522 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
4524 if (spec
!= NULL_TREE
)
4528 push_obstacks (&permanent_obstack
, &permanent_obstack
);
4529 old_fmp_obstack
= function_maybepermanent_obstack
;
4530 function_maybepermanent_obstack
= &permanent_obstack
;
4532 len
= DECL_NTPARMS (tmpl
);
4537 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
4538 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
4540 tree nt
= target_type (t
);
4541 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
4543 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
4544 cp_error (" trying to instantiate `%D'", tmpl
);
4545 fndecl
= error_mark_node
;
4549 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
4551 targ_ptr
= copy_to_permanent (targ_ptr
);
4553 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
4554 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
4556 /* substitute template parameters */
4557 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
4559 if (flag_external_templates
)
4560 add_pending_template (fndecl
);
4563 function_maybepermanent_obstack
= old_fmp_obstack
;
4569 /* Push the name of the class template into the scope of the instantiation. */
4572 overload_template_name (type
)
4575 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
4578 if (IDENTIFIER_CLASS_VALUE (id
)
4579 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
4582 decl
= build_decl (TYPE_DECL
, id
, type
);
4583 SET_DECL_ARTIFICIAL (decl
);
4584 pushdecl_class_level (decl
);
4587 /* Like type_unification but designed specially to handle conversion
4591 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
, strict
)
4592 tree fn
, explicit_targs
, targs
, args
, return_type
;
4596 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4597 tree decl_arg_types
= args
;
4599 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
4601 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
4603 /* This is a template conversion operator. Use the return types
4604 as well as the argument types. */
4605 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
4606 TREE_TYPE (TREE_TYPE (fn
)),
4608 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
4613 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
4614 &TREE_VEC_ELT (targs
, 0),
4624 /* Type unification.
4626 We have a function template signature with one or more references to
4627 template parameters, and a parameter list we wish to fit to this
4628 template. If possible, produce a list of parameters for the template
4629 which will cause it to fit the supplied parameter list.
4631 Return zero for success, 2 for an incomplete match that doesn't resolve
4632 all the types, and 1 for complete failure. An error message will be
4633 printed only for an incomplete match.
4635 TPARMS[NTPARMS] is an array of template parameter types;
4636 TARGS[NTPARMS] is the array of template parameter values. PARMS is
4637 the function template's signature (using TEMPLATE_PARM_IDX nodes),
4638 and ARGS is the argument list we're trying to match against it.
4640 If SUBR is 1, we're being called recursively (to unify the arguments of
4641 a function or method parameter of a function template), so don't zero
4642 out targs and don't fail on an incomplete match.
4644 If STRICT is 1, the match must be exact (for casts of overloaded
4645 addresses, explicit instantiation, and more_specialized). */
4648 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
4649 strict
, allow_incomplete
)
4650 tree tparms
, *targs
, parms
, args
, targs_in
;
4651 int *nsubsts
, strict
, allow_incomplete
;
4653 int ntparms
= TREE_VEC_LENGTH (tparms
);
4659 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
4661 if (targs_in
!= NULL_TREE
)
4664 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
4667 if (arg_vec
== error_mark_node
)
4671 i
< TREE_VEC_LENGTH (arg_vec
)
4672 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
4674 /* Insert the template argument. It is encoded as the operands
4675 of NOP_EXPRs so that unify can tell that it is an explicit
4677 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VEC_ELT (arg_vec
, i
));
4680 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
4681 strict
, allow_incomplete
);
4683 for (i
= 0, arg
= targs_in
;
4685 arg
= TREE_CHAIN (arg
), ++i
)
4686 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
4687 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
4694 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
4695 strict
, allow_incomplete
)
4696 tree tparms
, *targs
, parms
, args
;
4697 int *nsubsts
, subr
, strict
, allow_incomplete
;
4701 int ntparms
= TREE_VEC_LENGTH (tparms
);
4703 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
4704 my_friendly_assert (parms
== NULL_TREE
4705 || TREE_CODE (parms
) == TREE_LIST
, 290);
4706 /* ARGS could be NULL (via a call from parse.y to
4707 build_x_function_call). */
4709 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
4710 my_friendly_assert (ntparms
> 0, 292);
4713 && parms
!= void_list_node
4715 && args
!= void_list_node
)
4717 parm
= TREE_VALUE (parms
);
4718 parms
= TREE_CHAIN (parms
);
4719 arg
= TREE_VALUE (args
);
4720 args
= TREE_CHAIN (args
);
4722 if (arg
== error_mark_node
)
4724 if (arg
== unknown_type_node
)
4727 /* Conversions will be performed on a function argument that
4728 corresponds with a function parameter that contains only
4729 non-deducible template parameters and explicitly specified
4730 template parameters. */
4731 if (! uses_template_parms (parm
))
4735 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4736 type
= TREE_TYPE (arg
);
4745 if (comptypes (parm
, type
, 1))
4749 /* It might work; we shouldn't check now, because we might
4750 get into infinite recursion. Overload resolution will
4758 if (TREE_CODE (arg
) == VAR_DECL
)
4759 arg
= TREE_TYPE (arg
);
4760 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
4761 arg
= TREE_TYPE (arg
);
4763 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4765 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
4766 if (TREE_CODE (arg
) == TREE_LIST
4767 && TREE_TYPE (arg
) == unknown_type_node
4768 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
4770 int nsubsts
, ntparms
;
4773 /* Have to back unify here */
4774 arg
= TREE_VALUE (arg
);
4776 ntparms
= DECL_NTPARMS (arg
);
4777 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
4778 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
4780 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
4782 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
4783 parm
, NULL_TREE
, &nsubsts
, strict
,
4786 arg
= TREE_TYPE (arg
);
4789 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
4791 warning ("using type void* for NULL");
4792 arg
= ptr_type_node
;
4795 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
4796 arg
= TREE_TYPE (arg
);
4798 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
4800 if (TREE_CODE (arg
) == FUNCTION_TYPE
4801 || TREE_CODE (arg
) == METHOD_TYPE
)
4802 arg
= build_pointer_type (arg
);
4803 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
4804 arg
= build_pointer_type (TREE_TYPE (arg
));
4806 arg
= TYPE_MAIN_VARIANT (arg
);
4809 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
4817 /* Fail if we've reached the end of the parm list, and more args
4818 are present, and the parm list isn't variadic. */
4819 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
4821 /* Fail if parms are left and they don't have default values. */
4823 && parms
!= void_list_node
4824 && TREE_PURPOSE (parms
) == NULL_TREE
)
4827 for (i
= 0; i
< ntparms
; i
++)
4830 if (!allow_incomplete
)
4831 error ("incomplete type unification");
4837 /* Tail recursion is your friend. */
4840 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
4841 tree tparms
, *targs
, parm
, arg
;
4842 int *nsubsts
, ntparms
, strict
;
4846 /* I don't think this will do the right thing with respect to types.
4847 But the only case I've seen it in so far has been array bounds, where
4848 signedness is the only information lost, and I think that will be
4850 while (TREE_CODE (parm
) == NOP_EXPR
)
4851 parm
= TREE_OPERAND (parm
, 0);
4853 if (arg
== error_mark_node
)
4855 if (arg
== unknown_type_node
)
4860 switch (TREE_CODE (parm
))
4863 /* In a type which contains a nested-name-specifier, template
4864 argument values cannot be deduced for template parameters used
4865 within the nested-name-specifier. */
4868 case TEMPLATE_TYPE_PARM
:
4870 idx
= TEMPLATE_TYPE_IDX (parm
);
4871 /* Check for mixed types and values. */
4872 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
4875 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4876 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4877 /* An explicit template argument. Don't even try to match
4878 here; the overload resolution code will manage check to
4879 see whether the call is legal. */
4882 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
4883 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
4886 /* Template type parameters cannot contain cv-quals; i.e.
4887 template <class T> void f (T& a, T& b) will not generate
4888 void f (const int& a, const int& b). */
4889 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
4890 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
4892 arg
= TYPE_MAIN_VARIANT (arg
);
4895 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
4896 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
4897 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
4900 /* Simple cases: Value already set, does match or doesn't. */
4901 if (targs
[idx
] == arg
4903 && TREE_CODE (targs
[idx
]) == NOP_EXPR
4904 && TREE_OPERAND (targs
[idx
], 0) == arg
))
4906 else if (targs
[idx
])
4911 case TEMPLATE_TEMPLATE_PARM
:
4913 idx
= TEMPLATE_TYPE_IDX (parm
);
4914 /* Check for mixed types and values. */
4915 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TEMPLATE_DECL
)
4918 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4919 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4920 /* An explicit template argument. Don't even try to match
4921 here; the overload resolution code will manage check to
4922 see whether the call is legal. */
4925 if (CLASSTYPE_TEMPLATE_INFO (parm
))
4927 /* We arrive here when PARM does not involve template
4930 /* ARG must be constructed from a template class. */
4931 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
4935 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
4936 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
4937 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
4939 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
4942 /* The parameter and argument roles have to be switched here
4943 in order to handle default arguments properly. For example,
4944 template<template <class> class TT> void f(TT<int>)
4945 should be able to accept vector<int> which comes from
4946 template <class T, class Allcator = allocator>
4949 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1)
4953 /* Deduce arguments T, i from TT<T> or TT<i>. */
4954 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
4956 tree t
= TREE_VEC_ELT (parmvec
, i
);
4957 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
4958 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
4959 && TREE_CODE (t
) != TEMPLATE_CONST_PARM
)
4962 /* This argument can be deduced. */
4964 if (unify (tparms
, targs
, ntparms
, t
,
4965 TREE_VEC_ELT (argvec
, i
), nsubsts
, strict
))
4969 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
4972 /* Simple cases: Value already set, does match or doesn't. */
4973 if (targs
[idx
] == arg
4975 && TREE_CODE (targs
[idx
]) == NOP_EXPR
4976 && TREE_OPERAND (targs
[idx
], 0) == arg
))
4978 else if (targs
[idx
])
4983 case TEMPLATE_CONST_PARM
:
4985 idx
= TEMPLATE_CONST_IDX (parm
);
4988 int i
= cp_tree_equal (targs
[idx
], arg
);
4994 my_friendly_abort (42);
4997 targs
[idx
] = copy_to_permanent (arg
);
5001 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5002 return unify (tparms
, targs
, ntparms
, parm
,
5003 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
5005 if (TREE_CODE (arg
) != POINTER_TYPE
)
5007 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5010 case REFERENCE_TYPE
:
5011 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5012 arg
= TREE_TYPE (arg
);
5013 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5017 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5019 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5020 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5022 if (TYPE_DOMAIN (parm
) != NULL_TREE
5023 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5024 TYPE_DOMAIN (arg
), nsubsts
, strict
) != 0)
5026 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5034 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5037 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5039 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5040 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5041 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
5043 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5044 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5045 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
5048 else if (TREE_CODE (parm
) == REAL_TYPE
5049 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5052 /* As far as unification is concerned, this wins. Later checks
5053 will invalidate it if necessary. */
5056 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5057 /* Type INTEGER_CST can come from ordinary constant template args. */
5059 while (TREE_CODE (arg
) == NOP_EXPR
)
5060 arg
= TREE_OPERAND (arg
, 0);
5062 if (TREE_CODE (arg
) != INTEGER_CST
)
5064 return !tree_int_cst_equal (parm
, arg
);
5069 t1
= TREE_OPERAND (parm
, 0);
5070 t2
= TREE_OPERAND (parm
, 1);
5071 return unify (tparms
, targs
, ntparms
, t1
,
5072 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
5079 if (TREE_CODE (arg
) != TREE_VEC
)
5081 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5083 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5084 if (unify (tparms
, targs
, ntparms
,
5085 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5092 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5093 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5094 arg
, nsubsts
, strict
);
5096 /* Allow trivial conversions. */
5097 if (TREE_CODE (arg
) != RECORD_TYPE
5098 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5099 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5102 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5105 if (flag_ansi_overloading
&& ! strict
)
5106 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5108 (CLASSTYPE_TEMPLATE_INFO (arg
)
5109 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5111 if (! t
|| t
== error_mark_node
)
5114 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5115 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
5117 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5122 if (TREE_CODE (arg
) != METHOD_TYPE
)
5127 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5130 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5131 TREE_TYPE (arg
), nsubsts
, strict
))
5133 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5134 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
5138 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5140 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5141 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
5143 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5144 TREE_TYPE (arg
), nsubsts
, strict
);
5147 if (arg
!= decl_constant_value (parm
))
5152 sorry ("use of `%s' in template type unification",
5153 tree_code_name
[(int) TREE_CODE (parm
)]);
5159 mark_decl_instantiated (result
, extern_p
)
5163 if (DECL_TEMPLATE_INSTANTIATION (result
))
5164 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5166 if (TREE_CODE (result
) != FUNCTION_DECL
)
5167 /* The TREE_PUBLIC flag for function declarations will have been
5168 set correctly by tsubst. */
5169 TREE_PUBLIC (result
) = 1;
5173 DECL_INTERFACE_KNOWN (result
) = 1;
5174 DECL_NOT_REALLY_EXTERN (result
) = 1;
5176 /* For WIN32 we also want to put explicit instantiations in
5177 linkonce sections. */
5178 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
5179 make_decl_one_only (result
);
5181 else if (TREE_CODE (result
) == FUNCTION_DECL
)
5182 mark_inline_for_output (result
);
5185 /* Given two function templates PAT1 and PAT2, and explicit template
5186 arguments EXPLICIT_ARGS return:
5188 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
5189 -1 if PAT2 is more specialized than PAT1.
5190 0 if neither is more specialized. */
5193 more_specialized (pat1
, pat2
, explicit_args
)
5194 tree pat1
, pat2
, explicit_args
;
5199 targs
= get_bindings (pat1
, pat2
, explicit_args
);
5205 targs
= get_bindings (pat2
, pat1
, explicit_args
);
5214 /* Given two class template specialization list nodes PAT1 and PAT2, return:
5216 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
5217 -1 if PAT2 is more specialized than PAT1.
5218 0 if neither is more specialized. */
5221 more_specialized_class (pat1
, pat2
)
5227 targs
= get_class_bindings
5228 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
5232 targs
= get_class_bindings
5233 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
5240 /* Return the template arguments that will produce the function signature
5241 DECL from the function template FN, with the explicit template
5242 arguments EXPLICIT_ARGS. */
5245 get_bindings (fn
, decl
, explicit_args
)
5246 tree fn
, decl
, explicit_args
;
5248 int ntparms
= DECL_NTPARMS (fn
);
5249 tree targs
= make_scratch_vec (ntparms
);
5252 i
= fn_type_unification (fn
, explicit_args
, targs
,
5253 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
5254 TREE_TYPE (TREE_TYPE (decl
)),
5259 /* Check to see that the resulting return type is also OK. */
5260 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
5265 if (!comptypes(t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
5275 get_class_bindings (tparms
, parms
, args
)
5276 tree tparms
, parms
, args
;
5278 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
5279 tree vec
= make_temp_vec (ntparms
);
5281 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5283 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
5284 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
5294 for (i
= 0; i
< ntparms
; ++i
)
5295 if (! TREE_VEC_ELT (vec
, i
))
5301 /* Return the most specialized of the list of templates in FNS that can
5302 produce an instantiation matching DECL, given the explicit template
5303 arguments EXPLICIT_ARGS. */
5306 most_specialized (fns
, decl
, explicit_args
)
5307 tree fns
, decl
, explicit_args
;
5309 tree fn
, champ
, args
, *p
;
5312 for (p
= &fns
; *p
; )
5314 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
5317 p
= &TREE_CHAIN (*p
);
5320 *p
= TREE_CHAIN (*p
);
5327 champ
= TREE_VALUE (fn
);
5328 fn
= TREE_CHAIN (fn
);
5329 for (; fn
; fn
= TREE_CHAIN (fn
))
5331 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5338 fn
= TREE_CHAIN (fn
);
5340 return error_mark_node
;
5342 champ
= TREE_VALUE (fn
);
5346 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
5348 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5350 return error_mark_node
;
5356 /* Return the most specialized of the class template specializations in
5357 SPECS that can produce an instantiation matching ARGS. */
5360 most_specialized_class (specs
, mainargs
)
5361 tree specs
, mainargs
;
5363 tree list
= NULL_TREE
, t
, args
, champ
;
5366 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
5368 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
5371 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
5372 TREE_TYPE (list
) = TREE_TYPE (t
);
5382 for (; t
; t
= TREE_CHAIN (t
))
5384 fate
= more_specialized_class (champ
, t
);
5393 return error_mark_node
;
5399 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
5401 fate
= more_specialized_class (champ
, t
);
5403 return error_mark_node
;
5409 /* called from the parser. */
5412 do_decl_instantiation (declspecs
, declarator
, storage
)
5413 tree declspecs
, declarator
, storage
;
5415 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
5418 tree result
= NULL_TREE
;
5421 if (! DECL_LANG_SPECIFIC (decl
))
5423 cp_error ("explicit instantiation of non-template `%#D'", decl
);
5427 /* If we've already seen this template instance, use it. */
5428 if (TREE_CODE (decl
) == VAR_DECL
)
5430 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
5431 if (result
&& TREE_CODE (result
) != VAR_DECL
)
5434 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
5436 cp_error ("explicit instantiation of `%#D'", decl
);
5439 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
5444 cp_error ("no matching template for `%D' found", decl
);
5448 if (! DECL_TEMPLATE_INFO (result
))
5450 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
5454 if (flag_external_templates
)
5457 if (storage
== NULL_TREE
)
5459 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5462 cp_error ("storage class `%D' applied to template instantiation",
5465 mark_decl_instantiated (result
, extern_p
);
5466 repo_template_instantiated (result
, extern_p
);
5468 instantiate_decl (result
);
5472 mark_class_instantiated (t
, extern_p
)
5476 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
5477 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
5478 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
5479 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
5480 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
5483 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
5484 rest_of_type_compilation (t
, 1);
5489 do_type_instantiation (t
, storage
)
5496 if (TREE_CODE (t
) == TYPE_DECL
)
5499 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
5501 cp_error ("explicit instantiation of non-template type `%T'", t
);
5507 /* With -fexternal-templates, explicit instantiations are treated the same
5508 as implicit ones. */
5509 if (flag_external_templates
)
5512 if (TYPE_SIZE (t
) == NULL_TREE
)
5514 cp_error ("explicit instantiation of `%#T' before definition of template",
5519 if (storage
== NULL_TREE
)
5521 else if (storage
== ridpointers
[(int) RID_INLINE
])
5523 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5525 else if (storage
== ridpointers
[(int) RID_STATIC
])
5529 cp_error ("storage class `%D' applied to template instantiation",
5534 /* We've already instantiated this. */
5535 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
5539 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
5541 mark_class_instantiated (t
, extern_p
);
5542 repo_template_instantiated (t
, extern_p
);
5552 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5553 if (TREE_CODE (tmp
) == FUNCTION_DECL
5554 && DECL_TEMPLATE_INSTANTIATION (tmp
))
5556 mark_decl_instantiated (tmp
, extern_p
);
5557 repo_template_instantiated (tmp
, extern_p
);
5559 instantiate_decl (tmp
);
5562 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5563 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
5565 mark_decl_instantiated (tmp
, extern_p
);
5566 repo_template_instantiated (tmp
, extern_p
);
5568 instantiate_decl (tmp
);
5571 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5572 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
5573 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
5578 instantiate_decl (d
)
5581 tree ti
= DECL_TEMPLATE_INFO (d
);
5582 tree tmpl
= TI_TEMPLATE (ti
);
5583 tree args
= TI_ARGS (ti
);
5585 tree decl_pattern
, code_pattern
;
5587 int nested
= in_function_p ();
5589 int pattern_defined
;
5591 char *file
= input_filename
;
5593 for (td
= tmpl
; DECL_TEMPLATE_INSTANTIATION (td
); )
5594 td
= DECL_TI_TEMPLATE (td
);
5596 /* In the case of a member template, decl_pattern is the partially
5597 instantiated declaration (in the instantiated class), and code_pattern
5598 is the original template definition. */
5599 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
5600 code_pattern
= DECL_TEMPLATE_RESULT (td
);
5602 if (TREE_CODE (d
) == FUNCTION_DECL
)
5604 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
5605 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
5609 d_defined
= ! DECL_IN_AGGR_P (d
);
5610 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
5616 if (TREE_CODE (d
) == FUNCTION_DECL
)
5618 tree spec
= retrieve_specialization (tmpl
, args
);
5620 if (spec
!= NULL_TREE
5621 && DECL_TEMPLATE_SPECIALIZATION (spec
))
5625 /* This needs to happen before any tsubsting. */
5626 if (! push_tinst_level (d
))
5629 push_to_top_level ();
5630 lineno
= DECL_SOURCE_LINE (d
);
5631 input_filename
= DECL_SOURCE_FILE (d
);
5633 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
5634 variable is a static const initialized in the class body. */
5635 if (TREE_CODE (d
) == VAR_DECL
5636 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
5638 pushclass (DECL_CONTEXT (d
), 2);
5639 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5640 TREE_VEC_LENGTH (args
), tmpl
);
5644 if (pattern_defined
)
5646 repo_template_used (d
);
5648 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
5650 if (flag_alt_external_templates
)
5652 if (interface_unknown
)
5653 warn_if_unknown_interface (d
);
5655 else if (DECL_INTERFACE_KNOWN (code_pattern
))
5657 DECL_INTERFACE_KNOWN (d
) = 1;
5658 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
5661 warn_if_unknown_interface (code_pattern
);
5665 import_export_decl (d
);
5668 /* Reject all external templates except inline functions. */
5669 if (DECL_INTERFACE_KNOWN (d
)
5670 && ! DECL_NOT_REALLY_EXTERN (d
)
5671 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
5674 /* Defer all templates except inline functions used in another function. */
5675 if (! pattern_defined
5676 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
5679 add_pending_template (d
);
5683 lineno
= DECL_SOURCE_LINE (d
);
5684 input_filename
= DECL_SOURCE_FILE (d
);
5686 /* Trick tsubst into giving us a new decl in case the template changed. */
5687 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
5688 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
5689 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5690 SET_DECL_IMPLICIT_INSTANTIATION (td
);
5691 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
5693 /* And set up DECL_INITIAL, since tsubst doesn't. */
5694 if (TREE_CODE (td
) == VAR_DECL
)
5696 pushclass (DECL_CONTEXT (d
), 2);
5697 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5698 TREE_VEC_LENGTH (args
), tmpl
);
5702 if (TREE_CODE (d
) == FUNCTION_DECL
)
5704 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
5706 DECL_INITIAL (td
) = error_mark_node
;
5708 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
5709 /* Set up the information about what is being specialized. */
5710 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
5712 duplicate_decls (td
, d
);
5713 if (TREE_CODE (d
) == FUNCTION_DECL
)
5714 DECL_INITIAL (td
) = 0;
5716 if (TREE_CODE (d
) == VAR_DECL
)
5718 DECL_IN_AGGR_P (d
) = 0;
5719 if (DECL_INTERFACE_KNOWN (d
))
5720 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
5723 DECL_EXTERNAL (d
) = 1;
5724 DECL_NOT_REALLY_EXTERN (d
) = 1;
5726 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
5728 else if (TREE_CODE (d
) == FUNCTION_DECL
)
5730 tree t
= DECL_SAVED_TREE (code_pattern
);
5732 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
5733 store_parm_decls ();
5735 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
5738 (TREE_OPERAND (t
, 0),
5739 tsubst_expr (TREE_OPERAND (t
, 1), args
,
5740 TREE_VEC_LENGTH (args
), tmpl
));
5744 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
5746 current_member_init_list
5747 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
5748 current_base_init_list
5749 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
5754 /* Always keep the BLOCK node associated with the outermost
5755 pair of curly braces of a function. These are needed
5756 for correct operation of dwarfout.c. */
5759 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
5760 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5762 finish_function (lineno
, 0, nested
);
5767 input_filename
= file
;
5769 pop_from_top_level ();
5776 tsubst_chain (t
, argvec
)
5781 tree first
= tsubst (t
, argvec
,
5782 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5785 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
5787 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5788 TREE_CHAIN (last
) = x
;
5798 tsubst_expr_values (t
, argvec
)
5801 tree first
= NULL_TREE
;
5804 for (; t
; t
= TREE_CHAIN (t
))
5806 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
5807 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5808 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
5809 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5810 *p
= build_tree_list (pur
, val
);
5811 p
= &TREE_CHAIN (*p
);
5822 last_tree
= TREE_CHAIN (last_tree
) = t
;
5829 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
5830 last_tree
= NULL_TREE
;
5837 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
5839 last_tree
= TREE_VALUE (saved_trees
);
5840 saved_trees
= TREE_CHAIN (saved_trees
);
5843 /* D is an undefined function declaration in the presence of templates with
5844 the same name, listed in FNS. If one of them can produce D as an
5845 instantiation, remember this so we can instantiate it at EOF if D has
5846 not been defined by that time. */
5849 add_maybe_template (d
, fns
)
5854 if (DECL_MAYBE_TEMPLATE (d
))
5857 t
= most_specialized (fns
, d
, NULL_TREE
);
5860 if (t
== error_mark_node
)
5862 cp_error ("ambiguous template instantiation for `%D'", d
);
5866 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
5867 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
5868 DECL_MAYBE_TEMPLATE (d
) = 1;
5871 /* Instantiate an enumerated type. Used by instantiate_class_template and
5875 tsubst_enum (tag
, args
, nargs
, field_chain
)
5880 extern tree current_local_enum
;
5881 tree prev_local_enum
= current_local_enum
;
5883 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
5884 tree e
, values
= NULL_TREE
;
5886 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
5888 tree elt
= build_enumerator (TREE_PURPOSE (e
),
5889 tsubst_expr (TREE_VALUE (e
), args
,
5891 TREE_CHAIN (elt
) = values
;
5895 finish_enum (newtag
, values
);
5897 if (NULL
!= field_chain
)
5898 *field_chain
= grok_enum_decls (NULL_TREE
);
5900 current_local_enum
= prev_local_enum
;