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
)
1746 if (nargs
== nparms
)
1747 vec
= copy_node (arglist
);
1750 /* We arrive here when a template with some default arguments
1751 is used as template template argument. */
1753 vec
= make_tree_vec (nparms
);
1754 for (i
= 0; i
< nparms
; i
++)
1759 arg
= TREE_VEC_ELT (arglist
, i
);
1760 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
1762 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1765 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1768 TREE_VEC_ELT (vec
, i
) = arg
;
1774 vec
= make_tree_vec (nparms
);
1776 for (i
= 0; i
< nparms
; i
++)
1779 tree parm
= TREE_VEC_ELT (parms
, i
);
1784 arglist
= TREE_CHAIN (arglist
);
1786 if (arg
== error_mark_node
)
1789 arg
= TREE_VALUE (arg
);
1791 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
1793 my_friendly_assert (!require_all_arguments
, 0);
1796 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
1797 arg
= tsubst (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1799 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1801 TREE_VEC_ELT (vec
, i
) = arg
;
1804 for (i
= 0; i
< nparms
; i
++)
1806 tree arg
= TREE_VEC_ELT (vec
, i
);
1807 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1809 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
1811 if (arg
== NULL_TREE
)
1812 /* We're out of arguments. */
1814 my_friendly_assert (!require_all_arguments
, 0);
1818 if (arg
== error_mark_node
)
1820 cp_error ("template argument %d is invalid", i
+ 1);
1825 /* In case we are checking arguments inside a template template
1826 parameter, ARG that does not come from default argument is
1827 also a TREE_LIST node */
1828 if (TREE_CODE (arg
) == TREE_LIST
&& ! is_overloaded_fn (arg
))
1831 arg
= TREE_VALUE (arg
);
1834 /* Check if it is a class template. */
1835 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
1836 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
1837 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1838 && !CLASSTYPE_TEMPLATE_INFO (arg
));
1839 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
1840 arg
= TYPE_STUB_DECL (arg
);
1842 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
1843 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
1845 || (is_tmpl_parm
&& TREE_CODE (arg
) == TYPE_DECL
);
1846 requires_type
= TREE_CODE (parm
) == TYPE_DECL
1847 || requires_tmpl_type
;
1849 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1850 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1852 cp_pedwarn ("to refer to a type member of a template parameter,");
1853 cp_pedwarn (" use `typename %E'", arg
);
1855 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1856 TREE_OPERAND (arg
, 1));
1859 if (is_type
!= requires_type
)
1865 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1868 cp_error (" expected a constant of type `%T', got `%T'",
1870 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
1872 cp_error (" expected a type, got `%E'", arg
);
1876 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1879 if (is_tmpl_type
^ requires_tmpl_type
)
1883 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1886 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
1888 cp_error (" expected a class template, got `%T'", arg
);
1891 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1896 if (requires_tmpl_type
)
1898 cp_error ("nested template template parameter not implemented");
1900 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1907 if (requires_tmpl_type
)
1909 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
1910 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
1912 /* The parameter and argument roles have to be switched
1913 here in order to handle default arguments properly.
1915 template<template <class> class TT> void f(TT<int>)
1916 should be able to accept vector<int> which comes from
1917 template <class T, class Allcator = allocator>
1920 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1);
1921 if (val
!= error_mark_node
)
1924 /* TEMPLATE_TEMPLATE_PARM node is preferred over
1926 if (val
!= error_mark_node
1927 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
1928 val
= TREE_TYPE (val
);
1932 val
= groktypename (arg
);
1933 if (! processing_template_decl
)
1935 tree t
= target_type (val
);
1936 if (TREE_CODE (t
) != TYPENAME_TYPE
1938 && decl_function_context (TYPE_MAIN_DECL (t
)))
1940 cp_error ("type `%T' composed from a local class is not a valid template-argument",
1942 return error_mark_node
;
1949 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1950 TREE_VEC_LENGTH (vec
), in_decl
);
1952 if (processing_template_decl
)
1953 arg
= maybe_fold_nontype_arg (arg
);
1955 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
1956 /* We used to call digest_init here. However, digest_init
1957 will report errors, which we don't want when complain
1958 is zero. More importantly, digest_init will try too
1959 hard to convert things: for example, `0' should not be
1960 converted to pointer type at this point according to
1961 the standard. Accepting this is not merely an
1962 extension, since deciding whether or not these
1963 conversions can occur is part of determining which
1964 function template to call, or whether a given epxlicit
1965 argument specification is legal. */
1966 val
= convert_nontype_argument (t
, arg
);
1970 if (val
== NULL_TREE
)
1971 val
= error_mark_node
;
1972 else if (val
== error_mark_node
&& complain
)
1973 cp_error ("could not convert template argument `%E' to `%T'",
1977 if (val
== error_mark_node
)
1980 TREE_VEC_ELT (vec
, i
) = val
;
1983 return error_mark_node
;
1988 comp_template_args (oldargs
, newargs
)
1989 tree oldargs
, newargs
;
1993 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
1996 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
1998 tree nt
= TREE_VEC_ELT (newargs
, i
);
1999 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2003 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2005 if (TREE_CODE (nt
) == TREE_VEC
)
2007 /* For member templates */
2008 if (comp_template_args (nt
, ot
))
2011 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2013 if (comptypes (ot
, nt
, 1))
2016 else if (cp_tree_equal (ot
, nt
) > 0)
2023 /* Given class template name and parameter list, produce a user-friendly name
2024 for the instantiation. */
2027 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2029 tree parms
, arglist
;
2032 static struct obstack scratch_obstack
;
2033 static char *scratch_firstobj
;
2036 if (!scratch_firstobj
)
2037 gcc_obstack_init (&scratch_obstack
);
2039 obstack_free (&scratch_obstack
, scratch_firstobj
);
2040 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2043 #define buflen sizeof(buf)
2044 #define check if (bufp >= buf+buflen-1) goto too_long
2045 #define ccat(c) *bufp++=(c); check
2046 #define advance bufp+=strlen(bufp); check
2047 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2050 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2052 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2059 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2060 s
= fndecl_as_string(ctx
, 0);
2061 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2062 s
= type_as_string(ctx
, 0);
2064 my_friendly_abort (0);
2070 nparms
= TREE_VEC_LENGTH (parms
);
2071 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2072 for (i
= 0; i
< nparms
; i
++)
2074 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2075 tree arg
= TREE_VEC_ELT (arglist
, i
);
2080 if (TREE_CODE (parm
) == TYPE_DECL
)
2082 cat (type_as_string (arg
, 0));
2085 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2087 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2088 /* Already substituted with real template. Just output
2089 the template name here */
2090 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2092 /* Output the parameter declaration */
2093 cat (type_as_string (arg
, 0));
2097 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2099 if (TREE_CODE (arg
) == TREE_LIST
)
2101 /* New list cell was built because old chain link was in
2103 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2104 arg
= TREE_VALUE (arg
);
2106 /* No need to check arglist against parmlist here; we did that
2107 in coerce_template_parms, called from lookup_template_class. */
2108 cat (expr_as_string (arg
, 0));
2111 char *bufp
= obstack_next_free (&scratch_obstack
);
2113 while (bufp
[offset
- 1] == ' ')
2115 obstack_blank_fast (&scratch_obstack
, offset
);
2117 /* B<C<char> >, not B<C<char>> */
2118 if (bufp
[offset
- 1] == '>')
2123 return (char *) obstack_base (&scratch_obstack
);
2128 fatal ("out of (preallocated) string space creating template instantiation name");
2134 classtype_mangled_name (t
)
2137 if (CLASSTYPE_TEMPLATE_INFO (t
)
2138 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2140 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2141 char *mangled_name
= mangle_class_name_for_template
2142 (IDENTIFIER_POINTER (name
),
2143 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2144 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2145 tree id
= get_identifier (mangled_name
);
2146 IDENTIFIER_TEMPLATE (id
) = name
;
2150 return TYPE_IDENTIFIER (t
);
2154 add_pending_template (d
)
2159 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2160 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2162 ti
= DECL_TEMPLATE_INFO (d
);
2164 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2167 *template_tail
= perm_tree_cons
2168 (current_function_decl
, d
, NULL_TREE
);
2169 template_tail
= &TREE_CHAIN (*template_tail
);
2170 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2174 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2175 may be either a _DECL or an overloaded function or an
2176 IDENTIFIER_NODE), and ARGLIST. */
2179 lookup_template_function (fns
, arglist
)
2182 if (fns
== NULL_TREE
)
2184 cp_error ("non-template used as template");
2185 return error_mark_node
;
2188 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2189 copy_to_permanent (arglist
);
2191 return build_min (TEMPLATE_ID_EXPR
,
2193 ? TREE_TYPE (fns
) : unknown_type_node
,
2198 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2199 parameters, find the desired type.
2201 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2202 Since ARGLIST is build on the decl_obstack, we must copy it here
2203 to keep it from being reclaimed when the decl storage is reclaimed.
2205 IN_DECL, if non-NULL, is the template declaration we are trying to
2208 If the template class is really a local class in a template
2209 function, then the FUNCTION_CONTEXT is the function in which it is
2210 being instantiated. */
2213 lookup_template_class (d1
, arglist
, in_decl
, context
)
2218 tree
template = NULL_TREE
, parmlist
;
2222 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2224 if (IDENTIFIER_LOCAL_VALUE (d1
)
2225 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2226 template = IDENTIFIER_LOCAL_VALUE (d1
);
2229 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
2231 template = IDENTIFIER_CLASS_VALUE (d1
);
2234 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2236 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2237 return error_mark_node
;
2238 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2239 d1
= DECL_NAME (template);
2241 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2243 template = CLASSTYPE_TI_TEMPLATE (d1
);
2244 d1
= DECL_NAME (template);
2247 my_friendly_abort (272);
2249 /* With something like `template <class T> class X class X { ... };'
2250 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2251 We don't want to do that, but we have to deal with the situation, so
2252 let's give them some syntax errors to chew on instead of a crash. */
2254 return error_mark_node
;
2255 if (TREE_CODE (template) != TEMPLATE_DECL
)
2257 cp_error ("non-template type `%T' used as a template", d1
);
2259 cp_error_at ("for template declaration `%D'", in_decl
);
2260 return error_mark_node
;
2263 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2265 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2266 template arguments */
2268 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2269 tree template2
= TYPE_STUB_DECL (parm
);
2272 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2273 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2275 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
2276 if (arglist2
== error_mark_node
)
2277 return error_mark_node
;
2279 arglist2
= copy_to_permanent (arglist2
);
2280 CLASSTYPE_TEMPLATE_INFO (parm
)
2281 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2282 TYPE_SIZE (parm
) = 0;
2285 else if (PRIMARY_TEMPLATE_P (template)
2286 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2289 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2291 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2293 if (arglist
== error_mark_node
)
2294 return error_mark_node
;
2295 if (uses_template_parms (arglist
))
2298 if (comp_template_args
2299 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2300 found
= TREE_TYPE (template);
2303 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2304 found
; found
= TREE_CHAIN (found
))
2306 if (TI_USES_TEMPLATE_PARMS (found
)
2307 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2311 found
= TREE_VALUE (found
);
2316 if (can_free (&permanent_obstack
, arglist
))
2317 obstack_free (&permanent_obstack
, arglist
);
2322 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2326 id
= get_identifier (mangled_name
);
2327 IDENTIFIER_TEMPLATE (id
) = d1
;
2329 maybe_push_to_top_level (uses_template_parms (arglist
));
2330 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2332 if (context
!= NULL_TREE
)
2334 /* Set up the context for the type_decl correctly. Note
2335 that we must clear DECL_ASSEMBLER_NAME to fool
2336 build_overload_name into creating a new name. */
2337 tree type_decl
= TYPE_STUB_DECL (t
);
2339 TYPE_CONTEXT (t
) = context
;
2340 DECL_CONTEXT (type_decl
) = context
;
2341 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2342 DECL_ASSEMBLER_NAME (type_decl
) =
2343 get_identifier (build_overload_name (t
, 1, 1));
2346 pop_from_top_level ();
2350 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
2351 arglist
, in_decl
, NULL_TREE
);
2353 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2355 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2357 int save_temp
= processing_template_decl
;
2358 processing_template_decl
= 0;
2359 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2360 processing_template_decl
= save_temp
;
2364 t
= lookup_nested_type_by_name (ctx
, id
);
2365 my_friendly_assert (t
!= NULL_TREE
, 42);
2369 /* Seems to be wanted. */
2370 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2372 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2374 arglist
= copy_to_permanent (arglist
);
2375 CLASSTYPE_TEMPLATE_INFO (t
)
2376 = perm_tree_cons (template, arglist
, NULL_TREE
);
2377 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2378 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2379 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2380 = uses_template_parms (arglist
);
2382 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2384 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2385 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2386 /* if (! uses_template_parms (arglist)) */
2387 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2388 = get_identifier (build_overload_name (t
, 1, 1));
2390 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2391 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2392 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2393 add_pending_template (t
);
2399 /* Should be defined in parse.h. */
2403 uses_template_parms (t
)
2408 switch (TREE_CODE (t
))
2412 /* We assume that the object must be instantiated in order to build
2413 the COMPONENT_REF, so we test only whether the type of the
2414 COMPONENT_REF uses template parms. */
2415 return uses_template_parms (TREE_TYPE (t
));
2417 case IDENTIFIER_NODE
:
2418 if (!IDENTIFIER_TEMPLATE (t
))
2420 my_friendly_abort (42);
2422 /* aggregates of tree nodes */
2425 int i
= TREE_VEC_LENGTH (t
);
2427 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
2432 if (uses_template_parms (TREE_PURPOSE (t
))
2433 || uses_template_parms (TREE_VALUE (t
)))
2435 return uses_template_parms (TREE_CHAIN (t
));
2437 /* constructed type nodes */
2439 case REFERENCE_TYPE
:
2440 return uses_template_parms (TREE_TYPE (t
));
2442 if (TYPE_PTRMEMFUNC_FLAG (t
))
2443 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
2445 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2447 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
2449 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2451 return uses_template_parms (TREE_TYPE (t
));
2453 if (uses_template_parms (TYPE_DOMAIN (t
)))
2455 return uses_template_parms (TREE_TYPE (t
));
2457 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
2459 return uses_template_parms (TREE_TYPE (t
));
2461 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
2463 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2465 return uses_template_parms (TREE_TYPE (t
));
2469 return uses_template_parms (TREE_TYPE (t
));
2472 /* A template template parameter is encountered */
2473 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
2474 /* We are parsing a template declaration */
2476 /* We are instantiating templates with template template
2482 /* ??? What about FIELD_DECLs? */
2483 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
2484 && uses_template_parms (DECL_TI_ARGS (t
)))
2489 if (uses_template_parms (TREE_TYPE (t
)))
2491 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
2496 return uses_template_parms (TREE_TYPE (t
));
2498 return uses_template_parms (TREE_OPERAND (t
, 0));
2500 /* template parm nodes */
2501 case TEMPLATE_TYPE_PARM
:
2502 case TEMPLATE_TEMPLATE_PARM
:
2503 case TEMPLATE_CONST_PARM
:
2506 /* simple type nodes */
2508 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
2510 return uses_template_parms (TYPE_MAX_VALUE (t
));
2522 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
2523 if (uses_template_parms (TREE_VALUE (v
)))
2535 /* Non-error_mark_node ERROR_MARKs are bad things. */
2536 my_friendly_assert (t
== error_mark_node
, 274);
2545 return uses_template_parms (TREE_OPERAND (t
, 0));
2548 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2549 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2550 return uses_template_parms (TREE_OPERAND (t
, 1));
2554 case REINTERPRET_CAST_EXPR
:
2555 case CONST_CAST_EXPR
:
2556 case STATIC_CAST_EXPR
:
2557 case DYNAMIC_CAST_EXPR
:
2565 return uses_template_parms (TREE_OPERAND (t
, 0));
2568 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
2576 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
2577 if (uses_template_parms (TREE_OPERAND (t
, i
)))
2584 sorry ("testing %s for template parms",
2585 tree_code_name
[(int) TREE_CODE (t
)]);
2586 my_friendly_abort (82);
2592 static struct tinst_level
*current_tinst_level
= 0;
2593 static struct tinst_level
*free_tinst_level
= 0;
2594 static int tinst_depth
= 0;
2595 extern int max_tinst_depth
;
2596 #ifdef GATHER_STATISTICS
2597 int depth_reached
= 0;
2601 push_tinst_level (d
)
2604 struct tinst_level
*new;
2606 if (tinst_depth
>= max_tinst_depth
)
2608 struct tinst_level
*p
= current_tinst_level
;
2610 char *file
= input_filename
;
2612 error ("template instantiation depth exceeds maximum of %d",
2614 error (" (use -ftemplate-depth-NN to increase the maximum)");
2615 cp_error (" instantiating `%D'", d
);
2617 for (; p
; p
= p
->next
)
2619 cp_error (" instantiated from `%D'", p
->decl
);
2621 input_filename
= p
->file
;
2623 error (" instantiated from here");
2626 input_filename
= file
;
2631 if (free_tinst_level
)
2633 new = free_tinst_level
;
2634 free_tinst_level
= new->next
;
2637 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
2641 new->file
= input_filename
;
2642 new->next
= current_tinst_level
;
2643 current_tinst_level
= new;
2646 #ifdef GATHER_STATISTICS
2647 if (tinst_depth
> depth_reached
)
2648 depth_reached
= tinst_depth
;
2657 struct tinst_level
*old
= current_tinst_level
;
2659 current_tinst_level
= old
->next
;
2660 old
->next
= free_tinst_level
;
2661 free_tinst_level
= old
;
2665 struct tinst_level
*
2668 struct tinst_level
*p
= current_tinst_level
;
2671 for (; p
->next
; p
= p
->next
)
2677 instantiate_class_template (type
)
2680 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
2682 if (type
== error_mark_node
)
2683 return error_mark_node
;
2685 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
2687 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
2690 template = TI_TEMPLATE (template_info
);
2691 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
2692 args
= TI_ARGS (template_info
);
2694 t
= most_specialized_class
2695 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
2697 if (t
== error_mark_node
)
2699 char *str
= "candidates are:";
2700 cp_error ("ambiguous class template instantiation for `%#T'", type
);
2701 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
2703 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
2705 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
2709 TYPE_BEING_DEFINED (type
) = 1;
2710 return error_mark_node
;
2713 pattern
= TREE_TYPE (t
);
2715 pattern
= TREE_TYPE (template);
2717 if (TYPE_SIZE (pattern
) == NULL_TREE
)
2721 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
2723 TYPE_BEING_DEFINED (type
) = 1;
2725 if (! push_tinst_level (type
))
2728 maybe_push_to_top_level (uses_template_parms (type
));
2729 pushclass (type
, 0);
2731 if (flag_external_templates
)
2733 if (flag_alt_external_templates
)
2735 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
2736 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
2737 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2738 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2739 && CLASSTYPE_INTERFACE_KNOWN (type
);
2743 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
2744 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2745 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
2746 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2747 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2748 && CLASSTYPE_INTERFACE_KNOWN (type
);
2753 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
2754 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
2757 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
2758 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
2759 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
2760 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
2761 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
2762 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
2763 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
2764 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
2765 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
2766 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
2767 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
2768 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
2769 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
2770 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
2771 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
2772 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
2773 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
2774 TYPE_USES_COMPLEX_INHERITANCE (type
)
2775 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
2776 TYPE_USES_MULTIPLE_INHERITANCE (type
)
2777 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
2778 TYPE_USES_VIRTUAL_BASECLASSES (type
)
2779 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
2780 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
2781 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
2784 tree binfo
= TYPE_BINFO (type
);
2785 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
2791 int len
= TREE_VEC_LENGTH (pbases
);
2792 bases
= make_tree_vec (len
);
2793 for (i
= 0; i
< len
; ++i
)
2797 TREE_VEC_ELT (bases
, i
) = elt
2798 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
2799 TREE_VEC_LENGTH (args
), NULL_TREE
);
2800 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
2802 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
2804 ("base type `%T' of `%T' fails to be a struct or class type",
2805 TREE_TYPE (elt
), type
);
2806 else if (! uses_template_parms (type
)
2807 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
2809 cp_error ("base class `%T' of `%T' has incomplete type",
2810 TREE_TYPE (elt
), type
);
2812 /* Don't initialize this until the vector is filled out, or
2813 lookups will crash. */
2814 BINFO_BASETYPES (binfo
) = bases
;
2818 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
2820 field_chain
= &TYPE_FIELDS (type
);
2822 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
2824 tree tag
= TREE_VALUE (t
);
2826 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
2827 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
2830 tsubst_enum (tag
, args
, TREE_VEC_LENGTH (args
), field_chain
);
2832 while (*field_chain
)
2834 DECL_FIELD_CONTEXT (*field_chain
) = type
;
2835 field_chain
= &TREE_CHAIN (*field_chain
);
2840 TREE_VEC_LENGTH (args
), NULL_TREE
);
2843 /* Don't replace enum constants here. */
2844 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
2845 if (TREE_CODE (t
) != CONST_DECL
)
2847 tree r
= tsubst (t
, args
,
2848 TREE_VEC_LENGTH (args
), NULL_TREE
);
2849 if (TREE_CODE (r
) == VAR_DECL
)
2851 if (! uses_template_parms (r
))
2852 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
2853 /* Perhaps I should do more of grokfield here. */
2855 DECL_IN_AGGR_P (r
) = 1;
2856 DECL_EXTERNAL (r
) = 1;
2857 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
2861 field_chain
= &TREE_CHAIN (r
);
2864 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
2865 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
2867 if (DECL_CONSTRUCTOR_P (t
))
2868 grok_ctor_properties (type
, t
);
2869 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
2870 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
2873 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
2874 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
2875 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
2878 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
2879 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
2880 TREE_VEC_LENGTH (args
), NULL_TREE
);
2882 /* This does injection for friend classes. */
2883 for (; d
; d
= TREE_CHAIN (d
))
2884 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
2886 /* This does injection for friend functions. */
2887 if (!processing_template_decl
)
2889 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
2890 TREE_VEC_LENGTH (args
), NULL_TREE
);
2892 for (; d
; d
= TREE_CHAIN (d
))
2894 tree t
= TREE_VALUE (d
);
2896 if (TREE_CODE (t
) == TYPE_DECL
)
2897 /* Already injected. */;
2904 if (! uses_template_parms (type
))
2907 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
2908 if (TREE_CODE (tmp
) == FIELD_DECL
)
2910 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
2911 require_complete_type (tmp
);
2914 type
= finish_struct_1 (type
, 0);
2915 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
2917 repo_template_used (type
);
2918 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
2919 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
2923 TYPE_SIZE (type
) = integer_zero_node
;
2924 CLASSTYPE_METHOD_VEC (type
)
2925 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
2928 TYPE_BEING_DEFINED (type
) = 0;
2931 pop_from_top_level ();
2941 if (t1
== NULL_TREE
)
2942 return t2
== NULL_TREE
;
2943 if (t2
== NULL_TREE
)
2945 /* Don't care if one declares its arg const and the other doesn't -- the
2946 main variant of the arg type is all that matters. */
2947 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2948 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2950 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2954 lookup_nested_type_by_name (ctype
, name
)
2959 complete_type (ctype
);
2961 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2963 if (name
== TREE_PURPOSE (t
)
2964 /* this catches typedef enum { foo } bar; */
2965 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2966 return TREE_VALUE (t
);
2971 /* If arg is a non-type template parameter that does not depend on template
2972 arguments, fold it like we weren't in the body of a template. */
2975 maybe_fold_nontype_arg (arg
)
2978 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2979 && !uses_template_parms (arg
))
2981 /* Sometimes, one of the args was an expression involving a
2982 template constant parameter, like N - 1. Now that we've
2983 tsubst'd, we might have something like 2 - 1. This will
2984 confuse lookup_template_class, so we do constant folding
2985 here. We have to unset processing_template_decl, to
2986 fool build_expr_from_tree() into building an actual
2989 int saved_processing_template_decl
= processing_template_decl
;
2990 processing_template_decl
= 0;
2991 arg
= fold (build_expr_from_tree (arg
));
2992 processing_template_decl
= saved_processing_template_decl
;
2997 /* Take the tree structure T and replace template parameters used therein
2998 with the argument vector ARGS. NARGS is the number of args; should
2999 be removed. IN_DECL is an associated decl for diagnostics.
3001 tsubst is used for dealing with types, decls and the like; for
3002 expressions, use tsubst_expr or tsubst_copy. */
3005 tsubst (t
, args
, nargs
, in_decl
)
3012 if (t
== NULL_TREE
|| t
== error_mark_node
3013 || t
== integer_type_node
3014 || t
== void_type_node
3015 || t
== char_type_node
)
3018 type
= TREE_TYPE (t
);
3019 if (type
== unknown_type_node
)
3020 my_friendly_abort (42);
3021 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3022 && TREE_CODE (t
) != TYPENAME_TYPE
)
3023 type
= tsubst (type
, args
, nargs
, in_decl
);
3025 switch (TREE_CODE (t
))
3028 if (TYPE_PTRMEMFUNC_P (t
))
3030 tree r
= build_ptrmemfunc_type
3031 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
3032 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3036 /* else fall through */
3038 if (uses_template_parms (t
))
3040 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
3046 ? tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
) : NULL_TREE
;
3048 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3050 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3054 /* else fall through */
3056 case IDENTIFIER_NODE
:
3069 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3070 if (ctx
== NULL_TREE
)
3072 else if (ctx
== current_function_decl
)
3073 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3075 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3079 if (t
== integer_type_node
)
3082 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3083 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3087 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3088 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
3089 if (processing_template_decl
)
3091 tree itype
= make_node (INTEGER_TYPE
);
3092 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3093 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3098 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3099 return build_index_2_type (size_zero_node
, max
);
3102 case TEMPLATE_TYPE_PARM
:
3103 case TEMPLATE_TEMPLATE_PARM
:
3104 case TEMPLATE_CONST_PARM
:
3109 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3110 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3112 idx
= TEMPLATE_TYPE_IDX (t
);
3113 level
= TEMPLATE_TYPE_LEVEL (t
);
3117 idx
= TEMPLATE_CONST_IDX (t
);
3118 level
= TEMPLATE_CONST_LEVEL (t
);
3121 if (TREE_VEC_LENGTH (args
) > 0)
3123 tree arg
= NULL_TREE
;
3125 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3127 if (TREE_VEC_LENGTH (args
) >= level
- 1)
3129 (TREE_VEC_ELT (args
, level
- 1), idx
);
3131 else if (level
== 1)
3132 arg
= TREE_VEC_ELT (args
, idx
);
3134 if (arg
!= NULL_TREE
)
3136 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3137 return cp_build_type_variant
3138 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3139 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3140 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3142 if (CLASSTYPE_TEMPLATE_INFO (t
))
3144 /* We are processing a type constructed from
3145 a template template parameter */
3146 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3147 args
, nargs
, in_decl
);
3150 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3151 we are resolving nested-types in the signature of
3152 a member function templates.
3153 Otherwise ARG is a TEMPLATE_DECL and is the real
3154 template to be instantiated. */
3155 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3156 arg
= TYPE_NAME (arg
);
3158 r
= lookup_template_class (DECL_NAME (arg
),
3160 DECL_CONTEXT (arg
));
3161 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3165 /* We are processing a template argument list. */
3173 /* If we get here, we must have been looking at a parm for a
3174 more deeply nested template. */
3175 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
3176 && TEMPLATE_CONST_LEVEL (t
) > 1)
3177 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3178 && TEMPLATE_TYPE_LEVEL (t
) > 1)
3179 || (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3180 && TEMPLATE_TYPE_LEVEL (t
) > 1),
3187 /* We can get here when processing a member template function
3188 of a template class. */
3190 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
;
3788 tree last
= 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
= 0;
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
);
4658 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
4660 if (targs_in
!= NULL_TREE
)
4663 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
4666 if (arg_vec
== error_mark_node
)
4670 i
< TREE_VEC_LENGTH (arg_vec
)
4671 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
4673 /* Insert the template argument. It is encoded as the operands
4674 of NOP_EXPRs so that unify can tell that it is an explicit
4676 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VEC_ELT (arg_vec
, i
));
4679 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
4680 strict
, allow_incomplete
);
4682 for (i
= 0, arg
= targs_in
;
4684 arg
= TREE_CHAIN (arg
), ++i
)
4685 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
4686 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
4693 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
4694 strict
, allow_incomplete
)
4695 tree tparms
, *targs
, parms
, args
;
4696 int *nsubsts
, subr
, strict
, allow_incomplete
;
4700 int ntparms
= TREE_VEC_LENGTH (tparms
);
4702 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
4703 my_friendly_assert (parms
== NULL_TREE
4704 || TREE_CODE (parms
) == TREE_LIST
, 290);
4705 /* ARGS could be NULL (via a call from parse.y to
4706 build_x_function_call). */
4708 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
4709 my_friendly_assert (ntparms
> 0, 292);
4712 && parms
!= void_list_node
4714 && args
!= void_list_node
)
4716 parm
= TREE_VALUE (parms
);
4717 parms
= TREE_CHAIN (parms
);
4718 arg
= TREE_VALUE (args
);
4719 args
= TREE_CHAIN (args
);
4721 if (arg
== error_mark_node
)
4723 if (arg
== unknown_type_node
)
4726 /* Conversions will be performed on a function argument that
4727 corresponds with a function parameter that contains only
4728 non-deducible template parameters and explicitly specified
4729 template parameters. */
4730 if (! uses_template_parms (parm
))
4734 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4735 type
= TREE_TYPE (arg
);
4744 if (comptypes (parm
, type
, 1))
4748 /* It might work; we shouldn't check now, because we might
4749 get into infinite recursion. Overload resolution will
4757 if (TREE_CODE (arg
) == VAR_DECL
)
4758 arg
= TREE_TYPE (arg
);
4759 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
4760 arg
= TREE_TYPE (arg
);
4762 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4764 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
4765 if (TREE_CODE (arg
) == TREE_LIST
4766 && TREE_TYPE (arg
) == unknown_type_node
4767 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
4769 int nsubsts
, ntparms
;
4772 /* Have to back unify here */
4773 arg
= TREE_VALUE (arg
);
4775 ntparms
= DECL_NTPARMS (arg
);
4776 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
4777 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
4779 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
4781 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
4782 parm
, NULL_TREE
, &nsubsts
, strict
,
4785 arg
= TREE_TYPE (arg
);
4788 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
4790 warning ("using type void* for NULL");
4791 arg
= ptr_type_node
;
4794 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
4795 arg
= TREE_TYPE (arg
);
4797 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
4799 if (TREE_CODE (arg
) == FUNCTION_TYPE
4800 || TREE_CODE (arg
) == METHOD_TYPE
)
4801 arg
= build_pointer_type (arg
);
4802 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
4803 arg
= build_pointer_type (TREE_TYPE (arg
));
4805 arg
= TYPE_MAIN_VARIANT (arg
);
4808 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
4816 /* Fail if we've reached the end of the parm list, and more args
4817 are present, and the parm list isn't variadic. */
4818 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
4820 /* Fail if parms are left and they don't have default values. */
4822 && parms
!= void_list_node
4823 && TREE_PURPOSE (parms
) == NULL_TREE
)
4826 for (i
= 0; i
< ntparms
; i
++)
4829 if (!allow_incomplete
)
4830 error ("incomplete type unification");
4836 /* Tail recursion is your friend. */
4839 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
4840 tree tparms
, *targs
, parm
, arg
;
4841 int *nsubsts
, ntparms
, strict
;
4845 /* I don't think this will do the right thing with respect to types.
4846 But the only case I've seen it in so far has been array bounds, where
4847 signedness is the only information lost, and I think that will be
4849 while (TREE_CODE (parm
) == NOP_EXPR
)
4850 parm
= TREE_OPERAND (parm
, 0);
4852 if (arg
== error_mark_node
)
4854 if (arg
== unknown_type_node
)
4859 switch (TREE_CODE (parm
))
4862 /* In a type which contains a nested-name-specifier, template
4863 argument values cannot be deduced for template parameters used
4864 within the nested-name-specifier. */
4867 case TEMPLATE_TYPE_PARM
:
4869 idx
= TEMPLATE_TYPE_IDX (parm
);
4870 /* Check for mixed types and values. */
4871 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
4874 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4875 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4876 /* An explicit template argument. Don't even try to match
4877 here; the overload resolution code will manage check to
4878 see whether the call is legal. */
4881 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
4882 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
4885 /* Template type parameters cannot contain cv-quals; i.e.
4886 template <class T> void f (T& a, T& b) will not generate
4887 void f (const int& a, const int& b). */
4888 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
4889 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
4891 arg
= TYPE_MAIN_VARIANT (arg
);
4894 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
4895 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
4896 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
4899 /* Simple cases: Value already set, does match or doesn't. */
4900 if (targs
[idx
] == arg
4902 && TREE_CODE (targs
[idx
]) == NOP_EXPR
4903 && TREE_OPERAND (targs
[idx
], 0) == arg
))
4905 else if (targs
[idx
])
4910 case TEMPLATE_TEMPLATE_PARM
:
4912 idx
= TEMPLATE_TYPE_IDX (parm
);
4913 /* Check for mixed types and values. */
4914 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TEMPLATE_DECL
)
4917 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4918 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4919 /* An explicit template argument. Don't even try to match
4920 here; the overload resolution code will manage check to
4921 see whether the call is legal. */
4924 if (CLASSTYPE_TEMPLATE_INFO (parm
))
4926 /* We arrive here when PARM does not involve template
4929 /* ARG must be constructed from a template class. */
4930 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
4934 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
4935 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
4936 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
4938 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
4941 /* The parameter and argument roles have to be switched here
4942 in order to handle default arguments properly. For example,
4943 template<template <class> class TT> void f(TT<int>)
4944 should be able to accept vector<int> which comes from
4945 template <class T, class Allcator = allocator>
4948 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1)
4952 /* Deduce arguments T, i from TT<T> or TT<i>. */
4953 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
4955 tree t
= TREE_VEC_ELT (parmvec
, i
);
4956 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
4957 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
4958 && TREE_CODE (t
) != TEMPLATE_CONST_PARM
)
4961 /* This argument can be deduced. */
4963 if (unify (tparms
, targs
, ntparms
, t
,
4964 TREE_VEC_ELT (argvec
, i
), nsubsts
, strict
))
4968 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
4971 /* Simple cases: Value already set, does match or doesn't. */
4972 if (targs
[idx
] == arg
4974 && TREE_CODE (targs
[idx
]) == NOP_EXPR
4975 && TREE_OPERAND (targs
[idx
], 0) == arg
))
4977 else if (targs
[idx
])
4982 case TEMPLATE_CONST_PARM
:
4984 idx
= TEMPLATE_CONST_IDX (parm
);
4987 int i
= cp_tree_equal (targs
[idx
], arg
);
4993 my_friendly_abort (42);
4996 targs
[idx
] = copy_to_permanent (arg
);
5000 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5001 return unify (tparms
, targs
, ntparms
, parm
,
5002 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
5004 if (TREE_CODE (arg
) != POINTER_TYPE
)
5006 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5009 case REFERENCE_TYPE
:
5010 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5011 arg
= TREE_TYPE (arg
);
5012 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5016 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5018 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5019 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5021 if (TYPE_DOMAIN (parm
) != NULL_TREE
5022 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5023 TYPE_DOMAIN (arg
), nsubsts
, strict
) != 0)
5025 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5033 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5036 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5038 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5039 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5040 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
5042 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5043 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5044 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
5047 else if (TREE_CODE (parm
) == REAL_TYPE
5048 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5051 /* As far as unification is concerned, this wins. Later checks
5052 will invalidate it if necessary. */
5055 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5056 /* Type INTEGER_CST can come from ordinary constant template args. */
5058 while (TREE_CODE (arg
) == NOP_EXPR
)
5059 arg
= TREE_OPERAND (arg
, 0);
5061 if (TREE_CODE (arg
) != INTEGER_CST
)
5063 return !tree_int_cst_equal (parm
, arg
);
5068 t1
= TREE_OPERAND (parm
, 0);
5069 t2
= TREE_OPERAND (parm
, 1);
5070 return unify (tparms
, targs
, ntparms
, t1
,
5071 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
5078 if (TREE_CODE (arg
) != TREE_VEC
)
5080 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5082 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5083 if (unify (tparms
, targs
, ntparms
,
5084 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5091 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5092 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5093 arg
, nsubsts
, strict
);
5095 /* Allow trivial conversions. */
5096 if (TREE_CODE (arg
) != RECORD_TYPE
5097 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5098 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5101 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5104 if (flag_ansi_overloading
&& ! strict
)
5105 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5107 (CLASSTYPE_TEMPLATE_INFO (arg
)
5108 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5110 if (! t
|| t
== error_mark_node
)
5113 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5114 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
5116 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5121 if (TREE_CODE (arg
) != METHOD_TYPE
)
5126 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5129 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5130 TREE_TYPE (arg
), nsubsts
, strict
))
5132 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5133 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
5137 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5139 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5140 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
5142 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5143 TREE_TYPE (arg
), nsubsts
, strict
);
5146 if (arg
!= decl_constant_value (parm
))
5151 sorry ("use of `%s' in template type unification",
5152 tree_code_name
[(int) TREE_CODE (parm
)]);
5158 mark_decl_instantiated (result
, extern_p
)
5162 if (DECL_TEMPLATE_INSTANTIATION (result
))
5163 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5165 if (TREE_CODE (result
) != FUNCTION_DECL
)
5166 /* The TREE_PUBLIC flag for function declarations will have been
5167 set correctly by tsubst. */
5168 TREE_PUBLIC (result
) = 1;
5172 DECL_INTERFACE_KNOWN (result
) = 1;
5173 DECL_NOT_REALLY_EXTERN (result
) = 1;
5175 /* For WIN32 we also want to put explicit instantiations in
5176 linkonce sections. */
5177 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
5178 make_decl_one_only (result
);
5180 else if (TREE_CODE (result
) == FUNCTION_DECL
)
5181 mark_inline_for_output (result
);
5184 /* Given two function templates PAT1 and PAT2, and explicit template
5185 arguments EXPLICIT_ARGS return:
5187 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
5188 -1 if PAT2 is more specialized than PAT1.
5189 0 if neither is more specialized. */
5192 more_specialized (pat1
, pat2
, explicit_args
)
5193 tree pat1
, pat2
, explicit_args
;
5198 targs
= get_bindings (pat1
, pat2
, explicit_args
);
5204 targs
= get_bindings (pat2
, pat1
, explicit_args
);
5213 /* Given two class template specialization list nodes PAT1 and PAT2, return:
5215 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
5216 -1 if PAT2 is more specialized than PAT1.
5217 0 if neither is more specialized. */
5220 more_specialized_class (pat1
, pat2
)
5226 targs
= get_class_bindings
5227 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
5231 targs
= get_class_bindings
5232 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
5239 /* Return the template arguments that will produce the function signature
5240 DECL from the function template FN, with the explicit template
5241 arguments EXPLICIT_ARGS. */
5244 get_bindings (fn
, decl
, explicit_args
)
5245 tree fn
, decl
, explicit_args
;
5247 int ntparms
= DECL_NTPARMS (fn
);
5248 tree targs
= make_scratch_vec (ntparms
);
5251 i
= fn_type_unification (fn
, explicit_args
, targs
,
5252 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
5253 TREE_TYPE (TREE_TYPE (decl
)),
5258 /* Check to see that the resulting return type is also OK. */
5259 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
5264 if (!comptypes(t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
5274 get_class_bindings (tparms
, parms
, args
)
5275 tree tparms
, parms
, args
;
5277 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
5278 tree vec
= make_temp_vec (ntparms
);
5280 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5282 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
5283 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
5293 for (i
= 0; i
< ntparms
; ++i
)
5294 if (! TREE_VEC_ELT (vec
, i
))
5300 /* Return the most specialized of the list of templates in FNS that can
5301 produce an instantiation matching DECL, given the explicit template
5302 arguments EXPLICIT_ARGS. */
5305 most_specialized (fns
, decl
, explicit_args
)
5306 tree fns
, decl
, explicit_args
;
5308 tree fn
, champ
, args
, *p
;
5311 for (p
= &fns
; *p
; )
5313 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
5316 p
= &TREE_CHAIN (*p
);
5319 *p
= TREE_CHAIN (*p
);
5326 champ
= TREE_VALUE (fn
);
5327 fn
= TREE_CHAIN (fn
);
5328 for (; fn
; fn
= TREE_CHAIN (fn
))
5330 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5337 fn
= TREE_CHAIN (fn
);
5339 return error_mark_node
;
5341 champ
= TREE_VALUE (fn
);
5345 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
5347 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5349 return error_mark_node
;
5355 /* Return the most specialized of the class template specializations in
5356 SPECS that can produce an instantiation matching ARGS. */
5359 most_specialized_class (specs
, mainargs
)
5360 tree specs
, mainargs
;
5362 tree list
= NULL_TREE
, t
, args
, champ
;
5365 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
5367 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
5370 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
5371 TREE_TYPE (list
) = TREE_TYPE (t
);
5381 for (; t
; t
= TREE_CHAIN (t
))
5383 fate
= more_specialized_class (champ
, t
);
5392 return error_mark_node
;
5398 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
5400 fate
= more_specialized_class (champ
, t
);
5402 return error_mark_node
;
5408 /* called from the parser. */
5411 do_decl_instantiation (declspecs
, declarator
, storage
)
5412 tree declspecs
, declarator
, storage
;
5414 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
5417 tree result
= NULL_TREE
;
5420 if (! DECL_LANG_SPECIFIC (decl
))
5422 cp_error ("explicit instantiation of non-template `%#D'", decl
);
5426 /* If we've already seen this template instance, use it. */
5427 if (TREE_CODE (decl
) == VAR_DECL
)
5429 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
5430 if (result
&& TREE_CODE (result
) != VAR_DECL
)
5433 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
5435 cp_error ("explicit instantiation of `%#D'", decl
);
5438 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
5443 cp_error ("no matching template for `%D' found", decl
);
5447 if (! DECL_TEMPLATE_INFO (result
))
5449 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
5453 if (flag_external_templates
)
5456 if (storage
== NULL_TREE
)
5458 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5461 cp_error ("storage class `%D' applied to template instantiation",
5464 mark_decl_instantiated (result
, extern_p
);
5465 repo_template_instantiated (result
, extern_p
);
5467 instantiate_decl (result
);
5471 mark_class_instantiated (t
, extern_p
)
5475 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
5476 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
5477 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
5478 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
5479 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
5482 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
5483 rest_of_type_compilation (t
, 1);
5488 do_type_instantiation (t
, storage
)
5495 if (TREE_CODE (t
) == TYPE_DECL
)
5498 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
5500 cp_error ("explicit instantiation of non-template type `%T'", t
);
5506 /* With -fexternal-templates, explicit instantiations are treated the same
5507 as implicit ones. */
5508 if (flag_external_templates
)
5511 if (TYPE_SIZE (t
) == NULL_TREE
)
5513 cp_error ("explicit instantiation of `%#T' before definition of template",
5518 if (storage
== NULL_TREE
)
5520 else if (storage
== ridpointers
[(int) RID_INLINE
])
5522 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5524 else if (storage
== ridpointers
[(int) RID_STATIC
])
5528 cp_error ("storage class `%D' applied to template instantiation",
5533 /* We've already instantiated this. */
5534 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
5538 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
5540 mark_class_instantiated (t
, extern_p
);
5541 repo_template_instantiated (t
, extern_p
);
5551 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5552 if (TREE_CODE (tmp
) == FUNCTION_DECL
5553 && DECL_TEMPLATE_INSTANTIATION (tmp
))
5555 mark_decl_instantiated (tmp
, extern_p
);
5556 repo_template_instantiated (tmp
, extern_p
);
5558 instantiate_decl (tmp
);
5561 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5562 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
5564 mark_decl_instantiated (tmp
, extern_p
);
5565 repo_template_instantiated (tmp
, extern_p
);
5567 instantiate_decl (tmp
);
5570 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5571 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
5572 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
5577 instantiate_decl (d
)
5580 tree ti
= DECL_TEMPLATE_INFO (d
);
5581 tree tmpl
= TI_TEMPLATE (ti
);
5582 tree args
= TI_ARGS (ti
);
5584 tree decl_pattern
, code_pattern
;
5586 int nested
= in_function_p ();
5588 int pattern_defined
;
5590 char *file
= input_filename
;
5592 for (td
= tmpl
; DECL_TEMPLATE_INSTANTIATION (td
); )
5593 td
= DECL_TI_TEMPLATE (td
);
5595 /* In the case of a member template, decl_pattern is the partially
5596 instantiated declaration (in the instantiated class), and code_pattern
5597 is the original template definition. */
5598 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
5599 code_pattern
= DECL_TEMPLATE_RESULT (td
);
5601 if (TREE_CODE (d
) == FUNCTION_DECL
)
5603 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
5604 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
5608 d_defined
= ! DECL_IN_AGGR_P (d
);
5609 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
5615 if (TREE_CODE (d
) == FUNCTION_DECL
)
5617 tree spec
= retrieve_specialization (tmpl
, args
);
5619 if (spec
!= NULL_TREE
5620 && DECL_TEMPLATE_SPECIALIZATION (spec
))
5624 /* This needs to happen before any tsubsting. */
5625 if (! push_tinst_level (d
))
5628 push_to_top_level ();
5629 lineno
= DECL_SOURCE_LINE (d
);
5630 input_filename
= DECL_SOURCE_FILE (d
);
5632 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
5633 variable is a static const initialized in the class body. */
5634 if (TREE_CODE (d
) == VAR_DECL
5635 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
5637 pushclass (DECL_CONTEXT (d
), 2);
5638 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5639 TREE_VEC_LENGTH (args
), tmpl
);
5643 if (pattern_defined
)
5645 repo_template_used (d
);
5647 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
5649 if (flag_alt_external_templates
)
5651 if (interface_unknown
)
5652 warn_if_unknown_interface (d
);
5654 else if (DECL_INTERFACE_KNOWN (code_pattern
))
5656 DECL_INTERFACE_KNOWN (d
) = 1;
5657 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
5660 warn_if_unknown_interface (code_pattern
);
5664 import_export_decl (d
);
5667 /* Reject all external templates except inline functions. */
5668 if (DECL_INTERFACE_KNOWN (d
)
5669 && ! DECL_NOT_REALLY_EXTERN (d
)
5670 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
5673 /* Defer all templates except inline functions used in another function. */
5674 if (! pattern_defined
5675 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
5678 add_pending_template (d
);
5682 lineno
= DECL_SOURCE_LINE (d
);
5683 input_filename
= DECL_SOURCE_FILE (d
);
5685 /* Trick tsubst into giving us a new decl in case the template changed. */
5686 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
5687 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
5688 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5689 SET_DECL_IMPLICIT_INSTANTIATION (td
);
5690 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
5692 /* And set up DECL_INITIAL, since tsubst doesn't. */
5693 if (TREE_CODE (td
) == VAR_DECL
)
5695 pushclass (DECL_CONTEXT (d
), 2);
5696 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5697 TREE_VEC_LENGTH (args
), tmpl
);
5701 if (TREE_CODE (d
) == FUNCTION_DECL
)
5703 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
5705 DECL_INITIAL (td
) = error_mark_node
;
5707 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
5708 /* Set up the information about what is being specialized. */
5709 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
5711 duplicate_decls (td
, d
);
5712 if (TREE_CODE (d
) == FUNCTION_DECL
)
5713 DECL_INITIAL (td
) = 0;
5715 if (TREE_CODE (d
) == VAR_DECL
)
5717 DECL_IN_AGGR_P (d
) = 0;
5718 if (DECL_INTERFACE_KNOWN (d
))
5719 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
5722 DECL_EXTERNAL (d
) = 1;
5723 DECL_NOT_REALLY_EXTERN (d
) = 1;
5725 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
5727 else if (TREE_CODE (d
) == FUNCTION_DECL
)
5729 tree t
= DECL_SAVED_TREE (code_pattern
);
5731 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
5732 store_parm_decls ();
5734 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
5737 (TREE_OPERAND (t
, 0),
5738 tsubst_expr (TREE_OPERAND (t
, 1), args
,
5739 TREE_VEC_LENGTH (args
), tmpl
));
5743 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
5745 current_member_init_list
5746 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
5747 current_base_init_list
5748 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
5753 /* Always keep the BLOCK node associated with the outermost
5754 pair of curly braces of a function. These are needed
5755 for correct operation of dwarfout.c. */
5758 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
5759 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5761 finish_function (lineno
, 0, nested
);
5766 input_filename
= file
;
5768 pop_from_top_level ();
5775 tsubst_chain (t
, argvec
)
5780 tree first
= tsubst (t
, argvec
,
5781 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5784 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
5786 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5787 TREE_CHAIN (last
) = x
;
5797 tsubst_expr_values (t
, argvec
)
5800 tree first
= NULL_TREE
;
5803 for (; t
; t
= TREE_CHAIN (t
))
5805 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
5806 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5807 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
5808 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5809 *p
= build_tree_list (pur
, val
);
5810 p
= &TREE_CHAIN (*p
);
5821 last_tree
= TREE_CHAIN (last_tree
) = t
;
5828 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
5829 last_tree
= NULL_TREE
;
5836 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
5838 last_tree
= TREE_VALUE (saved_trees
);
5839 saved_trees
= TREE_CHAIN (saved_trees
);
5842 /* D is an undefined function declaration in the presence of templates with
5843 the same name, listed in FNS. If one of them can produce D as an
5844 instantiation, remember this so we can instantiate it at EOF if D has
5845 not been defined by that time. */
5848 add_maybe_template (d
, fns
)
5853 if (DECL_MAYBE_TEMPLATE (d
))
5856 t
= most_specialized (fns
, d
, NULL_TREE
);
5859 if (t
== error_mark_node
)
5861 cp_error ("ambiguous template instantiation for `%D'", d
);
5865 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
5866 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
5867 DECL_MAYBE_TEMPLATE (d
) = 1;
5870 /* Instantiate an enumerated type. Used by instantiate_class_template and
5874 tsubst_enum (tag
, args
, nargs
, field_chain
)
5879 extern tree current_local_enum
;
5880 tree prev_local_enum
= current_local_enum
;
5882 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
5883 tree e
, values
= NULL_TREE
;
5885 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
5887 tree elt
= build_enumerator (TREE_PURPOSE (e
),
5888 tsubst_expr (TREE_VALUE (e
), args
,
5890 TREE_CHAIN (elt
) = values
;
5894 finish_enum (newtag
, values
);
5896 if (NULL
!= field_chain
)
5897 *field_chain
= grok_enum_decls (NULL_TREE
);
5899 current_local_enum
= prev_local_enum
;