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
)))
523 if (decls_match (decl
, fn
))
525 cp_error ("`template <>' applied to non-specialization `%D'",
534 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
537 if (decl
== NULL_TREE
)
539 tree targs
= make_scratch_vec (DECL_NTPARMS (tmpl
));
541 /* We allow incomplete unification here, because we are going to
542 check all the functions. */
543 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
544 &TREE_VEC_ELT (targs
, 0),
551 /* Unification was successful. */
552 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
555 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
558 if (decl
!= NULL_TREE
)
560 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
562 if (tmpl
== error_mark_node
)
564 else if (tmpl
== NULL_TREE
)
567 *targs_out
= get_bindings (tmpl
, decl
, targs_in
);
571 if (templates
== NULL_TREE
)
575 cp_error ("`%D' does not match any template declaration", decl
);
579 else if (TREE_CHAIN (templates
) != NULL_TREE
)
584 cp_error ("ambiguous template specialization `%D'", decl
);
585 print_candidates (templates
);
590 /* We have one, and exactly one, match. */
591 *targs_out
= TREE_PURPOSE (templates
);
592 return TREE_VALUE (templates
);
596 /* Check to see if the function just declared, as indicated in
597 DECLARATOR, and in DECL, is a specialization of a function
598 template. We may also discover that the declaration is an explicit
599 instantiation at this point.
601 Returns DECL, or an equivalent declaration that should be used
604 0: The function is not an explicit specialization or instantiation.
605 1: The function is an explicit specialization.
606 2: The function is an explicit instantiation.
608 FLAGS is a bitmask consisting of the following flags:
610 1: We are being called by finish_struct. (We are unable to
611 determine what template is specialized by an in-class
612 declaration until the class definition is complete, so
613 finish_struct_methods calls this function again later to finish
615 2: The function has a definition.
616 4: The function is a friend.
617 8: The function is known to be a specialization of a member
620 The TEMPLATE_COUNT is the number of references to qualifying
621 template classes that appeared in the name of the function. For
624 template <class T> struct S { void f(); };
627 the TEMPLATE_COUNT would be 1. However, explicitly specialized
628 classes are not counted in the TEMPLATE_COUNT, so that in
630 template <class T> struct S {};
631 template <> struct S<int> { void f(); }
635 the TEMPLATE_COUNT would be 0. (Note that this declaration is
636 illegal; there should be no template <>.)
638 If the function is a specialization, it is marked as such via
639 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
640 is set up correctly, and it is added to the list of specializations
641 for that template. */
644 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
650 int finish_member
= flags
& 1;
651 int have_def
= flags
& 2;
652 int is_friend
= flags
& 4;
653 int specialization
= 0;
654 int explicit_instantiation
= 0;
655 int member_specialization
= flags
& 8;
657 tree ctype
= DECL_CLASS_CONTEXT (decl
);
658 tree dname
= DECL_NAME (decl
);
662 if (processing_specialization
)
664 /* The last template header was of the form template <>. */
666 if (template_header_count
> template_count
)
668 /* There were more template headers than qualifying template
670 if (template_header_count
- template_count
> 1)
671 /* There shouldn't be that many template parameter
672 lists. There can be at most one parameter list for
673 every qualifying class, plus one for the function
675 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
677 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
679 member_specialization
= 1;
683 else if (template_header_count
== template_count
)
685 /* The counts are equal. So, this might be a
686 specialization, but it is not a specialization of a
687 member template. It might be something like
689 template <class T> struct S {
693 void S<int>::f(int i) {} */
695 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
699 /* This cannot be an explicit specialization. There are not
700 enough headers for all of the qualifying classes. For
701 example, we might have:
704 void S<int>::T<char>::f();
706 But, we're missing another template <>. */
707 cp_error("too few template parameter lists in declaration of `%D'", decl
);
711 else if (processing_explicit_instantiation
)
713 if (template_header_count
)
714 cp_error ("template parameter list used in explicit instantiation");
717 cp_error ("definition provided for explicit instantiation");
719 explicit_instantiation
= 1;
721 else if (ctype
!= NULL_TREE
722 && !TYPE_BEING_DEFINED (ctype
)
723 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
725 /* This case catches outdated code that looks like this:
727 template <class T> struct S { void f(); };
728 void S<int>::f() {} // Missing template <>
730 We disable this check when the type is being defined to
731 avoid complaining about default compiler-generated
732 constructors, destructors, and assignment operators.
733 Since the type is an instantiation, not a specialization,
734 these are the only functions that can be defined before
735 the class is complete. */
738 template <class T> void S<int>::f() {}
740 if (template_header_count
)
742 cp_error ("template parameters specified in specialization");
748 ("explicit specialization not preceded by `template <>'");
750 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
752 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
754 /* This case handles bogus declarations like
755 template <> template <class T>
758 cp_error ("template-id `%D' in declaration of primary template",
764 if (specialization
|| member_specialization
|| explicit_instantiation
)
766 tree tmpl
= NULL_TREE
;
767 tree targs
= NULL_TREE
;
769 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
770 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
774 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
777 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
782 lookup_template_function (fns
, NULL_TREE
);
785 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
787 /* A friend declaration. We can't do much, because we don't
788 know what this resolves to, yet. */
789 my_friendly_assert (is_friend
!= 0, 0);
790 my_friendly_assert (!explicit_instantiation
, 0);
791 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
795 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
797 /* Since finish_struct_1 has not been called yet, we
798 can't call lookup_fnfields. We note that this
799 template is a specialization, and proceed, letting
800 finish_struct fix this up later. */
801 tree ti
= perm_tree_cons (NULL_TREE
,
802 TREE_OPERAND (declarator
, 1),
804 TI_PENDING_SPECIALIZATION_FLAG (ti
) = 1;
805 DECL_TEMPLATE_INFO (decl
) = ti
;
806 /* This should not be an instantiation; explicit
807 instantiation directives can only occur at the top
809 my_friendly_assert (!explicit_instantiation
, 0);
812 else if (ctype
!= NULL_TREE
813 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
816 /* Find the list of functions in ctype that have the same
817 name as the declared function. */
818 tree name
= TREE_OPERAND (declarator
, 0);
821 if (name
== constructor_name (ctype
)
822 || name
== constructor_name_full (ctype
))
824 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
826 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
827 : !TYPE_HAS_DESTRUCTOR (ctype
))
829 /* From [temp.expl.spec]:
831 If such an explicit specialization for the member
832 of a class template names an implicitly-declared
833 special member function (clause _special_), the
834 program is ill-formed.
836 Similar language is found in [temp.explicit]. */
837 cp_error ("specialization of implicitly-declared special member function");
842 fns
= TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype
),
843 is_constructor
? 0 : 1);
846 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
,
849 if (fns
== NULL_TREE
)
851 cp_error ("no member function `%s' declared in `%T'",
852 IDENTIFIER_POINTER (name
),
857 TREE_OPERAND (declarator
, 0) = fns
;
860 /* Figure out what exactly is being specialized at this point.
861 Note that for an explicit instantiation, even one for a
862 member function, we cannot tell apriori whether the the
863 instantiation is for a member template, or just a member
864 function of a template class. In particular, even in if the
865 instantiation is for a member template, the template
866 arguments could be deduced from the declaration. */
867 tmpl
= determine_specialization (declarator
, decl
,
869 member_specialization
,
874 if (explicit_instantiation
)
876 decl
= instantiate_template (tmpl
, targs
);
877 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
878 /* There doesn't seem to be anything in the draft to
879 prevent a specialization from being explicitly
880 instantiated. We're careful not to destroy the
881 information indicating that this is a
882 specialization here. */
883 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
886 else if (DECL_STATIC_FUNCTION_P (tmpl
)
887 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
888 revert_static_member_fn (&decl
, 0, 0);
890 /* Mangle the function name appropriately. Note that we do
891 not mangle specializations of non-template member
892 functions of template classes, e.g. with
893 template <class T> struct S { void f(); }
894 and given the specialization
895 template <> void S<int>::f() {}
896 we do not mangle S<int>::f() here. That's because it's
897 just an ordinary member function and doesn't need special
899 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
900 && name_mangling_version
>= 1)
902 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
905 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
907 hash_tree_chain (build_pointer_type (ctype
),
910 DECL_ASSEMBLER_NAME (decl
)
911 = build_template_decl_overload
914 TREE_TYPE (TREE_TYPE (tmpl
)),
915 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
916 targs
, ctype
!= NULL_TREE
);
919 if (is_friend
&& !have_def
)
921 /* This is not really a declaration of a specialization.
922 It's just the name of an instantiation. But, it's not
923 a request for an instantiation, either. */
924 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
925 DECL_TEMPLATE_INFO (decl
)
926 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
930 /* If DECL_TI_TEMPLATE (decl), the decl is an
931 instantiation of a specialization of a member template.
932 (In other words, there was a member template, in a
933 class template. That member template was specialized.
934 We then instantiated the class, so there is now an
935 instance of that specialization.)
937 According to the CD2,
939 14.7.3.13 [tmpl.expl.spec]
941 A specialization of a member function template or
942 member class template of a non-specialized class
943 template is itself a template.
945 So, we just leave the template info alone in this case. */
946 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
947 DECL_TEMPLATE_INFO (decl
)
948 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
950 register_specialization (decl
, tmpl
, targs
);
960 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
961 parameters. These are represented in the same format used for
962 DECL_TEMPLATE_PARMS. */
964 int comp_template_parms (parms1
, parms2
)
971 if (parms1
== parms2
)
974 for (p1
= parms1
, p2
= parms2
;
975 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
976 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
978 tree t1
= TREE_VALUE (p1
);
979 tree t2
= TREE_VALUE (p2
);
982 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
983 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
985 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
988 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
990 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
991 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
993 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
996 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
998 else if (!comptypes (TREE_TYPE (parm1
),
999 TREE_TYPE (parm2
), 1))
1004 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1005 /* One set of parameters has more parameters lists than the
1013 /* Process information from new template parameter NEXT and append it to the
1014 LIST being built. */
1017 process_template_parm (list
, next
)
1025 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1026 defval
= TREE_PURPOSE (parm
);
1027 parm
= TREE_VALUE (parm
);
1028 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1032 tree p
= TREE_VALUE (tree_last (list
));
1034 if (TREE_CODE (p
) == TYPE_DECL
)
1035 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1036 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1037 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1039 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
1048 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1049 /* is a const-param */
1050 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1051 PARM
, 0, NULL_TREE
);
1052 /* A template parameter is not modifiable. */
1053 TREE_READONLY (parm
) = 1;
1054 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1055 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1057 cp_error ("`%#T' is not a valid type for a template constant parameter",
1059 if (DECL_NAME (parm
) == NULL_TREE
)
1060 error (" a template type parameter must begin with `class' or `typename'");
1061 TREE_TYPE (parm
) = void_type_node
;
1064 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1065 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1066 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1068 tinfo
= make_node (TEMPLATE_CONST_PARM
);
1069 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
1070 if (TREE_PERMANENT (parm
) == 0)
1072 parm
= copy_node (parm
);
1073 TREE_PERMANENT (parm
) = 1;
1075 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
1076 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1077 DECL_INITIAL (decl
) = tinfo
;
1078 DECL_INITIAL (parm
) = tinfo
;
1079 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
1084 parm
= TREE_VALUE (parm
);
1086 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1088 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1089 /* This is for distinguishing between real templates and template
1090 template parameters */
1091 TREE_TYPE (parm
) = t
;
1092 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1097 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1098 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1099 decl
= build_decl (TYPE_DECL
, parm
, t
);
1102 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1103 TYPE_NAME (t
) = decl
;
1104 TYPE_STUB_DECL (t
) = decl
;
1106 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
1108 SET_DECL_ARTIFICIAL (decl
);
1110 parm
= build_tree_list (defval
, parm
);
1111 return chainon (list
, parm
);
1114 /* The end of a template parameter list has been reached. Process the
1115 tree list into a parameter vector, converting each parameter into a more
1116 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1120 end_template_parm_list (parms
)
1125 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1127 current_template_parms
1128 = tree_cons (build_int_2 (0, processing_template_decl
),
1129 saved_parmlist
, current_template_parms
);
1131 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1132 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1134 return saved_parmlist
;
1137 /* end_template_decl is called after a template declaration is seen. */
1140 end_template_decl ()
1142 reset_specialization ();
1144 if (! processing_template_decl
)
1147 /* This matches the pushlevel in begin_template_parm_list. */
1150 --processing_template_decl
;
1151 current_template_parms
= TREE_CHAIN (current_template_parms
);
1152 (void) get_pending_sizes (); /* Why? */
1155 /* Generate a valid set of template args from current_template_parms. */
1158 current_template_args ()
1160 tree header
= current_template_parms
;
1161 int length
= list_length (header
);
1162 tree args
= make_tree_vec (length
);
1167 tree a
= copy_node (TREE_VALUE (header
));
1168 int i
= TREE_VEC_LENGTH (a
);
1169 TREE_TYPE (a
) = NULL_TREE
;
1172 tree t
= TREE_VEC_ELT (a
, i
);
1174 /* t will be a list if we are called from within a
1175 begin/end_template_parm_list pair, but a vector directly
1176 if within a begin/end_member_template_processing pair. */
1177 if (TREE_CODE (t
) == TREE_LIST
)
1181 if (TREE_CODE (t
) == TYPE_DECL
1182 || TREE_CODE (t
) == TEMPLATE_DECL
)
1185 t
= DECL_INITIAL (t
);
1188 TREE_VEC_ELT (a
, i
) = t
;
1190 TREE_VEC_ELT (args
, --l
) = a
;
1191 header
= TREE_CHAIN (header
);
1198 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1199 template PARMS. Used by push_template_decl below. */
1202 build_template_decl (decl
, parms
)
1206 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1207 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1208 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1209 if (DECL_LANG_SPECIFIC (decl
))
1211 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1212 DECL_STATIC_FUNCTION_P (tmpl
) =
1213 DECL_STATIC_FUNCTION_P (decl
);
1220 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1221 parameters given by current_template_args, or reuses a previously
1222 existing one, if appropriate. Returns the DECL, or an equivalent
1223 one, if it is replaced via a call to duplicate_decls. */
1226 push_template_decl (decl
)
1230 tree args
= NULL_TREE
;
1232 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
1236 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
1237 && DECL_CLASS_CONTEXT (decl
))
1239 /* Note that this template is a "primary template" */
1241 || (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't'
1242 && ! CLASSTYPE_TEMPLATE_INFO (ctx
))
1243 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
1246 /* Partial specialization. */
1247 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1248 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1250 tree type
= TREE_TYPE (decl
);
1251 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1252 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1253 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1255 for (; spec
; spec
= TREE_CHAIN (spec
))
1257 /* purpose: args to main template
1258 value: spec template */
1259 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1263 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1264 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1265 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1266 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1270 args
= current_template_args ();
1272 if (! ctx
|| TREE_CODE (ctx
) == FUNCTION_DECL
1273 || TYPE_BEING_DEFINED (ctx
))
1275 tmpl
= build_template_decl (decl
, current_template_parms
);
1277 if (DECL_LANG_SPECIFIC (decl
)
1278 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1280 /* A specialization of a member template of a template
1282 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1283 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1284 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1292 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1293 cp_error ("must specialize `%#T' before defining member `%#D'",
1295 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1296 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1297 else if (! DECL_TEMPLATE_INFO (decl
))
1299 cp_error ("template definition of non-template `%#D'", decl
);
1303 tmpl
= DECL_TI_TEMPLATE (decl
);
1305 if (is_member_template (tmpl
))
1307 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1308 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1312 /* The declaration is a specialization of a member
1313 template, declared outside the class. Therefore, the
1314 innermost template arguments will be NULL, so we
1315 replace them with the arguments determined by the
1316 earlier call to check_explicit_specialization. */
1317 args
= DECL_TI_ARGS (decl
);
1320 = build_template_decl (decl
, current_template_parms
);
1321 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1322 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1323 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1324 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1325 DECL_TEMPLATE_INFO (new_tmpl
) =
1326 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1328 register_specialization (new_tmpl
, tmpl
, args
);
1332 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1333 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1334 if (TREE_VEC_LENGTH (t
)
1335 != TREE_VEC_LENGTH (a
))
1337 cp_error ("got %d template parameters for `%#D'",
1338 TREE_VEC_LENGTH (a
), decl
);
1339 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1341 if (TREE_VEC_LENGTH (args
) > 1)
1342 /* Get the template parameters for the enclosing template
1344 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1349 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1353 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1355 /* When processing an inline member template of a
1356 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1357 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1358 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1360 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1361 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1363 /* There should be template arguments if and only if there is a
1365 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1368 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1370 cp_error ("got %d template parameters for `%#D'",
1371 TREE_VEC_LENGTH (a
), decl
);
1372 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1375 /* Get the innermost set of template arguments. */
1376 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1378 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1379 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1382 tmpl
= pushdecl_top_level (tmpl
);
1385 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1387 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1389 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1391 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1392 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1393 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1395 else if (! DECL_LANG_SPECIFIC (decl
))
1396 cp_error ("template declaration of `%#D'", decl
);
1398 DECL_TEMPLATE_INFO (decl
) = info
;
1400 return DECL_TEMPLATE_RESULT (tmpl
);
1404 /* Attempt to convert the non-type template parameter EXPR to the
1405 indicated TYPE. If the conversion is successful, return the
1406 converted value. If the conversion is unsuccesful, return
1407 NULL_TREE if we issued an error message, or error_mark_node if we
1408 did not. We issue error messages for out-and-out bad template
1409 parameters, but not simply because the conversion failed, since we
1410 might be just trying to do argument deduction. By the time this
1411 function is called, neither TYPE nor EXPR may make use of template
1415 convert_nontype_argument (type
, expr
)
1419 tree expr_type
= TREE_TYPE (expr
);
1421 /* A template-argument for a non-type, non-template
1422 template-parameter shall be one of:
1424 --an integral constant-expression of integral or enumeration
1427 --the name of a non-type template-parameter; or
1429 --the name of an object or function with external linkage,
1430 including function templates and function template-ids but
1431 excluding non-static class members, expressed as id-expression;
1434 --the address of an object or function with external linkage,
1435 including function templates and function template-ids but
1436 excluding non-static class members, expressed as & id-expression
1437 where the & is optional if the name refers to a function or
1440 --a pointer to member expressed as described in _expr.unary.op_. */
1442 /* An integral constant-expression can include const variables
1444 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1445 expr
= decl_constant_value (expr
);
1447 if (is_overloaded_fn (expr
))
1448 /* OK for now. We'll check that it has external linkage later.
1449 Check this first since if expr_type is the unknown_type_node
1450 we would otherwise complain below. */
1452 else if (INTEGRAL_TYPE_P (expr_type
)
1453 || TYPE_PTRMEM_P (expr_type
)
1454 || TYPE_PTRMEMFUNC_P (expr_type
)
1455 /* The next two are g++ extensions. */
1456 || TREE_CODE (expr_type
) == REAL_TYPE
1457 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1459 if (! TREE_CONSTANT (expr
))
1461 cp_error ("non-constant `%E' cannot be used as template argument",
1466 else if (TYPE_PTR_P (expr_type
)
1467 /* If expr is the address of an overloaded function, we
1468 will get the unknown_type_node at this point. */
1469 || expr_type
== unknown_type_node
)
1473 if (TREE_CODE (expr
) != ADDR_EXPR
)
1476 cp_error ("`%E' is not a valid template argument", expr
);
1477 error ("it must be %s%s with external linkage",
1478 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1479 ? "a pointer to " : "",
1480 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1481 ? "a function" : "an object");
1485 referent
= TREE_OPERAND (expr
, 0);
1486 STRIP_NOPS (referent
);
1488 if (TREE_CODE (referent
) == STRING_CST
)
1490 cp_error ("string literal %E is not a valid template argument",
1492 error ("because it is the address of an object with static linkage");
1496 if (is_overloaded_fn (referent
))
1497 /* We'll check that it has external linkage later. */
1499 else if (TREE_CODE (referent
) != VAR_DECL
)
1501 else if (!TREE_PUBLIC (referent
))
1503 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1504 return error_mark_node
;
1507 else if (TREE_CODE (expr
) == VAR_DECL
)
1509 if (!TREE_PUBLIC (expr
))
1514 cp_error ("object `%E' cannot be used as template argument", expr
);
1518 switch (TREE_CODE (type
))
1523 /* For a non-type template-parameter of integral or enumeration
1524 type, integral promotions (_conv.prom_) and integral
1525 conversions (_conv.integral_) are applied. */
1526 if (!INTEGRAL_TYPE_P (expr_type
))
1527 return error_mark_node
;
1529 /* It's safe to call digest_init in this case; we know we're
1530 just converting one integral constant expression to another. */
1531 return digest_init (type
, expr
, (tree
*) 0);
1535 /* These are g++ extensions. */
1536 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
1537 return error_mark_node
;
1539 return digest_init (type
, expr
, (tree
*) 0);
1543 tree type_pointed_to
= TREE_TYPE (type
);
1545 if (TYPE_PTRMEM_P (type
))
1546 /* For a non-type template-parameter of type pointer to data
1547 member, qualification conversions (_conv.qual_) are
1549 return perform_qualification_conversions (type
, expr
);
1550 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
1552 /* For a non-type template-parameter of type pointer to
1553 function, only the function-to-pointer conversion
1554 (_conv.func_) is applied. If the template-argument
1555 represents a set of overloaded functions (or a pointer to
1556 such), the matching function is selected from the set
1561 if (TREE_CODE (expr
) == ADDR_EXPR
)
1562 fns
= TREE_OPERAND (expr
, 0);
1566 fn
= instantiate_type (type_pointed_to
, fns
, 0);
1568 if (fn
== error_mark_node
)
1569 return error_mark_node
;
1571 if (!TREE_PUBLIC (fn
))
1573 if (really_overloaded_fn (fns
))
1574 return error_mark_node
;
1579 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1581 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1587 /* For a non-type template-parameter of type pointer to
1588 object, qualification conversions (_conv.qual_) and the
1589 array-to-pointer conversion (_conv.array_) are applied.
1590 [Note: In particular, neither the null pointer conversion
1591 (_conv.ptr_) nor the derived-to-base conversion
1592 (_conv.ptr_) are applied. Although 0 is a valid
1593 template-argument for a non-type template-parameter of
1594 integral type, it is not a valid template-argument for a
1595 non-type template-parameter of pointer type.]
1597 The call to decay_conversion performs the
1598 array-to-pointer conversion, if appropriate. */
1599 expr
= decay_conversion (expr
);
1601 if (expr
== error_mark_node
)
1602 return error_mark_node
;
1604 return perform_qualification_conversions (type
, expr
);
1609 case REFERENCE_TYPE
:
1611 tree type_referred_to
= TREE_TYPE (type
);
1613 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
1615 /* For a non-type template-parameter of type reference to
1616 function, no conversions apply. If the
1617 template-argument represents a set of overloaded
1618 functions, the matching function is selected from the
1619 set (_over.over_). */
1623 fn
= instantiate_type (type_referred_to
, fns
, 0);
1625 if (!TREE_PUBLIC (fn
))
1627 if (really_overloaded_fn (fns
))
1628 /* Don't issue an error here; we might get a different
1629 function if the overloading had worked out
1631 return error_mark_node
;
1636 if (fn
== error_mark_node
)
1637 return error_mark_node
;
1639 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
1646 /* For a non-type template-parameter of type reference to
1647 object, no conversions apply. The type referred to by the
1648 reference may be more cv-qualified than the (otherwise
1649 identical) type of the template-argument. The
1650 template-parameter is bound directly to the
1651 template-argument, which must be an lvalue. */
1652 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
1653 TYPE_MAIN_VARIANT (type
), 1)
1654 || (TYPE_READONLY (expr_type
) >
1655 TYPE_READONLY (type_referred_to
))
1656 || (TYPE_VOLATILE (expr_type
) >
1657 TYPE_VOLATILE (type_referred_to
))
1658 || !real_lvalue_p (expr
))
1659 return error_mark_node
;
1671 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
1673 /* For a non-type template-parameter of type pointer to member
1674 function, no conversions apply. If the template-argument
1675 represents a set of overloaded member functions, the
1676 matching member function is selected from the set
1679 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
1680 expr_type
!= unknown_type_node
)
1681 return error_mark_node
;
1683 if (TREE_CODE (expr
) == CONSTRUCTOR
)
1685 /* A ptr-to-member constant. */
1686 if (!comptypes (type
, expr_type
, 1))
1687 return error_mark_node
;
1692 if (TREE_CODE (expr
) != ADDR_EXPR
)
1693 return error_mark_node
;
1695 fns
= TREE_OPERAND (expr
, 0);
1697 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
1700 if (fn
== error_mark_node
)
1701 return error_mark_node
;
1703 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1705 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1712 /* All non-type parameters must have one of these types. */
1713 my_friendly_abort (0);
1717 return error_mark_node
;
1720 /* Convert all template arguments to their appropriate types, and return
1721 a vector containing the resulting values. If any error occurs, return
1722 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
1723 Some error messages are issued even if COMPLAIN is zero; for
1724 instance, if a template argument is composed from a local class.
1726 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
1727 provided in ARGLIST, or else trailing parameters must have default
1728 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
1729 deduction for any unspecified trailing arguments. */
1732 coerce_template_parms (parms
, arglist
, in_decl
,
1734 require_all_arguments
)
1735 tree parms
, arglist
;
1738 int require_all_arguments
;
1740 int nparms
, nargs
, i
, lost
= 0;
1741 int is_tmpl_parm
= 0;
1742 tree vec
= NULL_TREE
;
1744 if (arglist
== NULL_TREE
)
1746 else if (TREE_CODE (arglist
) == TREE_VEC
)
1747 nargs
= TREE_VEC_LENGTH (arglist
);
1749 nargs
= list_length (arglist
);
1751 nparms
= TREE_VEC_LENGTH (parms
);
1755 && require_all_arguments
1756 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
1760 error ("incorrect number of parameters (%d, should be %d)",
1764 cp_error_at ("in template expansion for decl `%D'",
1768 return error_mark_node
;
1771 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
&& nargs
== nparms
)
1772 vec
= copy_node (arglist
);
1775 /* We can arrive here with arglist being a TREE_VEC when a
1776 template with some default arguments is used as template
1777 template argument. */
1778 is_tmpl_parm
= TREE_CODE (arglist
) == TREE_VEC
;
1779 vec
= make_tree_vec (nparms
);
1781 for (i
= 0; i
< nparms
; i
++)
1784 tree parm
= TREE_VEC_ELT (parms
, i
);
1789 arglist
= TREE_CHAIN (arglist
);
1791 if (arg
== error_mark_node
)
1794 arg
= TREE_VALUE (arg
);
1796 else if (is_tmpl_parm
&& i
< nargs
)
1798 arg
= TREE_VEC_ELT (arglist
, i
);
1799 if (arg
== error_mark_node
)
1802 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
1804 my_friendly_assert (!require_all_arguments
, 0);
1807 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
1808 arg
= tsubst (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1810 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1812 TREE_VEC_ELT (vec
, i
) = arg
;
1815 for (i
= 0; i
< nparms
; i
++)
1817 tree arg
= TREE_VEC_ELT (vec
, i
);
1818 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1820 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
1822 if (arg
== NULL_TREE
)
1823 /* We're out of arguments. */
1825 my_friendly_assert (!require_all_arguments
, 0);
1829 if (arg
== error_mark_node
)
1831 cp_error ("template argument %d is invalid", i
+ 1);
1836 if (TREE_CODE (arg
) == TREE_LIST
1837 && TREE_TYPE (arg
) != NULL_TREE
1838 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
1840 /* The template argument was the name of some
1841 member function. That's usually
1842 illegal, but static members are OK. In any
1843 case, grab the underlying fields/functions
1844 and issue an error later if required. */
1845 arg
= TREE_VALUE (arg
);
1846 TREE_TYPE (arg
) = unknown_type_node
;
1848 else if (TREE_CODE (arg
) == TREE_LIST
&& ! is_overloaded_fn (arg
))
1850 /* In case we are checking arguments inside a template template
1851 parameter, ARG that does not come from default argument is
1852 also a TREE_LIST node */
1854 arg
= TREE_VALUE (arg
);
1857 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
1858 requires_type
= TREE_CODE (parm
) == TYPE_DECL
1859 || requires_tmpl_type
;
1861 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
1862 we also accept implicitly created TYPE_DECL as a valid argument. */
1863 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
1864 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
1865 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1866 && !CLASSTYPE_TEMPLATE_INFO (arg
))
1867 || (TREE_CODE (arg
) == RECORD_TYPE
1868 && CLASSTYPE_TEMPLATE_INFO (arg
)
1869 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
1870 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
1871 && requires_tmpl_type
);
1872 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
1873 arg
= TYPE_STUB_DECL (arg
);
1874 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
1875 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
1877 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
1879 || (is_tmpl_parm
&& TREE_CODE (arg
) == TYPE_DECL
);
1881 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1882 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1884 cp_pedwarn ("to refer to a type member of a template parameter,");
1885 cp_pedwarn (" use `typename %E'", arg
);
1887 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1888 TREE_OPERAND (arg
, 1));
1891 if (is_type
!= requires_type
)
1897 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1900 cp_error (" expected a constant of type `%T', got `%T'",
1902 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
1904 cp_error (" expected a type, got `%E'", arg
);
1908 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1911 if (is_tmpl_type
^ requires_tmpl_type
)
1915 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1918 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
1920 cp_error (" expected a class template, got `%T'", arg
);
1923 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1928 if (requires_tmpl_type
)
1930 cp_error ("nested template template parameter not implemented");
1932 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1939 if (requires_tmpl_type
)
1941 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
1942 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
1944 /* The parameter and argument roles have to be switched
1945 here in order to handle default arguments properly.
1947 template<template <class> class TT> void f(TT<int>)
1948 should be able to accept vector<int> which comes from
1949 template <class T, class Allcator = allocator>
1952 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1);
1953 if (val
!= error_mark_node
)
1956 /* TEMPLATE_TEMPLATE_PARM node is preferred over
1958 if (val
!= error_mark_node
1959 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
1960 val
= TREE_TYPE (val
);
1964 val
= groktypename (arg
);
1965 if (! processing_template_decl
)
1967 tree t
= target_type (val
);
1968 if (TREE_CODE (t
) != TYPENAME_TYPE
1970 && decl_function_context (TYPE_MAIN_DECL (t
)))
1972 cp_error ("type `%T' composed from a local class is not a valid template-argument",
1974 return error_mark_node
;
1981 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1982 TREE_VEC_LENGTH (vec
), in_decl
);
1984 if (processing_template_decl
)
1985 arg
= maybe_fold_nontype_arg (arg
);
1987 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
1988 /* We used to call digest_init here. However, digest_init
1989 will report errors, which we don't want when complain
1990 is zero. More importantly, digest_init will try too
1991 hard to convert things: for example, `0' should not be
1992 converted to pointer type at this point according to
1993 the standard. Accepting this is not merely an
1994 extension, since deciding whether or not these
1995 conversions can occur is part of determining which
1996 function template to call, or whether a given epxlicit
1997 argument specification is legal. */
1998 val
= convert_nontype_argument (t
, arg
);
2002 if (val
== NULL_TREE
)
2003 val
= error_mark_node
;
2004 else if (val
== error_mark_node
&& complain
)
2005 cp_error ("could not convert template argument `%E' to `%T'",
2009 if (val
== error_mark_node
)
2012 TREE_VEC_ELT (vec
, i
) = val
;
2015 return error_mark_node
;
2020 comp_template_args (oldargs
, newargs
)
2021 tree oldargs
, newargs
;
2025 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
2028 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
2030 tree nt
= TREE_VEC_ELT (newargs
, i
);
2031 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2035 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2037 if (TREE_CODE (nt
) == TREE_VEC
)
2039 /* For member templates */
2040 if (comp_template_args (nt
, ot
))
2043 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2045 if (comptypes (ot
, nt
, 1))
2048 else if (cp_tree_equal (ot
, nt
) > 0)
2055 /* Given class template name and parameter list, produce a user-friendly name
2056 for the instantiation. */
2059 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2061 tree parms
, arglist
;
2064 static struct obstack scratch_obstack
;
2065 static char *scratch_firstobj
;
2068 if (!scratch_firstobj
)
2069 gcc_obstack_init (&scratch_obstack
);
2071 obstack_free (&scratch_obstack
, scratch_firstobj
);
2072 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2075 #define buflen sizeof(buf)
2076 #define check if (bufp >= buf+buflen-1) goto too_long
2077 #define ccat(c) *bufp++=(c); check
2078 #define advance bufp+=strlen(bufp); check
2079 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2082 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2084 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2091 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2092 s
= fndecl_as_string(ctx
, 0);
2093 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2094 s
= type_as_string(ctx
, 0);
2096 my_friendly_abort (0);
2102 nparms
= TREE_VEC_LENGTH (parms
);
2103 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2104 for (i
= 0; i
< nparms
; i
++)
2106 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2107 tree arg
= TREE_VEC_ELT (arglist
, i
);
2112 if (TREE_CODE (parm
) == TYPE_DECL
)
2114 cat (type_as_string (arg
, 0));
2117 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2119 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2120 /* Already substituted with real template. Just output
2121 the template name here */
2122 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2124 /* Output the parameter declaration */
2125 cat (type_as_string (arg
, 0));
2129 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2131 if (TREE_CODE (arg
) == TREE_LIST
)
2133 /* New list cell was built because old chain link was in
2135 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2136 arg
= TREE_VALUE (arg
);
2138 /* No need to check arglist against parmlist here; we did that
2139 in coerce_template_parms, called from lookup_template_class. */
2140 cat (expr_as_string (arg
, 0));
2143 char *bufp
= obstack_next_free (&scratch_obstack
);
2145 while (bufp
[offset
- 1] == ' ')
2147 obstack_blank_fast (&scratch_obstack
, offset
);
2149 /* B<C<char> >, not B<C<char>> */
2150 if (bufp
[offset
- 1] == '>')
2155 return (char *) obstack_base (&scratch_obstack
);
2160 fatal ("out of (preallocated) string space creating template instantiation name");
2166 classtype_mangled_name (t
)
2169 if (CLASSTYPE_TEMPLATE_INFO (t
)
2170 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2172 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2173 char *mangled_name
= mangle_class_name_for_template
2174 (IDENTIFIER_POINTER (name
),
2175 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2176 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2177 tree id
= get_identifier (mangled_name
);
2178 IDENTIFIER_TEMPLATE (id
) = name
;
2182 return TYPE_IDENTIFIER (t
);
2186 add_pending_template (d
)
2191 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2192 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2194 ti
= DECL_TEMPLATE_INFO (d
);
2196 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2199 *template_tail
= perm_tree_cons
2200 (current_function_decl
, d
, NULL_TREE
);
2201 template_tail
= &TREE_CHAIN (*template_tail
);
2202 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2206 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2207 may be either a _DECL or an overloaded function or an
2208 IDENTIFIER_NODE), and ARGLIST. */
2211 lookup_template_function (fns
, arglist
)
2214 if (fns
== NULL_TREE
)
2216 cp_error ("non-template used as template");
2217 return error_mark_node
;
2220 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2221 copy_to_permanent (arglist
);
2223 return build_min (TEMPLATE_ID_EXPR
,
2225 ? TREE_TYPE (fns
) : unknown_type_node
,
2230 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2231 parameters, find the desired type.
2233 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2234 Since ARGLIST is build on the decl_obstack, we must copy it here
2235 to keep it from being reclaimed when the decl storage is reclaimed.
2237 IN_DECL, if non-NULL, is the template declaration we are trying to
2240 If the template class is really a local class in a template
2241 function, then the FUNCTION_CONTEXT is the function in which it is
2242 being instantiated. */
2245 lookup_template_class (d1
, arglist
, in_decl
, context
)
2250 tree
template = NULL_TREE
, parmlist
;
2254 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2256 if (IDENTIFIER_LOCAL_VALUE (d1
)
2257 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2258 template = IDENTIFIER_LOCAL_VALUE (d1
);
2261 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
2263 template = IDENTIFIER_CLASS_VALUE (d1
);
2266 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2268 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2269 return error_mark_node
;
2270 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2271 d1
= DECL_NAME (template);
2273 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2275 template = CLASSTYPE_TI_TEMPLATE (d1
);
2276 d1
= DECL_NAME (template);
2279 my_friendly_abort (272);
2281 /* With something like `template <class T> class X class X { ... };'
2282 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2283 We don't want to do that, but we have to deal with the situation, so
2284 let's give them some syntax errors to chew on instead of a crash. */
2286 return error_mark_node
;
2287 if (TREE_CODE (template) != TEMPLATE_DECL
)
2289 cp_error ("non-template type `%T' used as a template", d1
);
2291 cp_error_at ("for template declaration `%D'", in_decl
);
2292 return error_mark_node
;
2295 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2297 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2298 template arguments */
2300 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2301 tree template2
= TYPE_STUB_DECL (parm
);
2304 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2305 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2307 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1);
2308 if (arglist2
== error_mark_node
)
2309 return error_mark_node
;
2311 arglist2
= copy_to_permanent (arglist2
);
2312 CLASSTYPE_TEMPLATE_INFO (parm
)
2313 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2314 TYPE_SIZE (parm
) = 0;
2317 else if (PRIMARY_TEMPLATE_P (template)
2318 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2321 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2323 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2325 if (arglist
== error_mark_node
)
2326 return error_mark_node
;
2327 if (uses_template_parms (arglist
))
2330 if (comp_template_args
2331 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2332 found
= TREE_TYPE (template);
2335 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2336 found
; found
= TREE_CHAIN (found
))
2338 if (TI_USES_TEMPLATE_PARMS (found
)
2339 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2343 found
= TREE_VALUE (found
);
2348 if (can_free (&permanent_obstack
, arglist
))
2349 obstack_free (&permanent_obstack
, arglist
);
2354 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2358 id
= get_identifier (mangled_name
);
2359 IDENTIFIER_TEMPLATE (id
) = d1
;
2361 maybe_push_to_top_level (uses_template_parms (arglist
));
2362 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2364 if (context
!= NULL_TREE
)
2366 /* Set up the context for the type_decl correctly. Note
2367 that we must clear DECL_ASSEMBLER_NAME to fool
2368 build_overload_name into creating a new name. */
2369 tree type_decl
= TYPE_STUB_DECL (t
);
2371 TYPE_CONTEXT (t
) = context
;
2372 DECL_CONTEXT (type_decl
) = context
;
2373 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2374 DECL_ASSEMBLER_NAME (type_decl
) =
2375 get_identifier (build_overload_name (t
, 1, 1));
2378 pop_from_top_level ();
2382 tree type_ctx
= TYPE_CONTEXT (TREE_TYPE (template));
2383 tree args
= tsubst (CLASSTYPE_TI_ARGS (type_ctx
),
2385 TREE_VEC_LENGTH (arglist
),
2387 tree ctx
= lookup_template_class (type_ctx
, args
,
2388 in_decl
, NULL_TREE
);
2390 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2392 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2394 int save_temp
= processing_template_decl
;
2395 processing_template_decl
= 0;
2396 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2397 processing_template_decl
= save_temp
;
2401 t
= lookup_nested_type_by_name (ctx
, id
);
2402 my_friendly_assert (t
!= NULL_TREE
, 42);
2406 /* Seems to be wanted. */
2407 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2409 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2411 arglist
= copy_to_permanent (arglist
);
2412 CLASSTYPE_TEMPLATE_INFO (t
)
2413 = perm_tree_cons (template, arglist
, NULL_TREE
);
2414 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2415 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2416 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2417 = uses_template_parms (arglist
);
2419 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2421 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2422 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2423 /* if (! uses_template_parms (arglist)) */
2424 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2425 = get_identifier (build_overload_name (t
, 1, 1));
2427 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2428 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2429 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2430 add_pending_template (t
);
2436 /* Should be defined in parse.h. */
2440 uses_template_parms (t
)
2445 switch (TREE_CODE (t
))
2449 /* We assume that the object must be instantiated in order to build
2450 the COMPONENT_REF, so we test only whether the type of the
2451 COMPONENT_REF uses template parms. */
2452 return uses_template_parms (TREE_TYPE (t
));
2454 case IDENTIFIER_NODE
:
2455 if (!IDENTIFIER_TEMPLATE (t
))
2457 my_friendly_abort (42);
2459 /* aggregates of tree nodes */
2462 int i
= TREE_VEC_LENGTH (t
);
2464 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
2469 if (uses_template_parms (TREE_PURPOSE (t
))
2470 || uses_template_parms (TREE_VALUE (t
)))
2472 return uses_template_parms (TREE_CHAIN (t
));
2474 /* constructed type nodes */
2476 case REFERENCE_TYPE
:
2477 return uses_template_parms (TREE_TYPE (t
));
2479 if (TYPE_PTRMEMFUNC_FLAG (t
))
2480 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
2482 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2484 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
2486 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2488 return uses_template_parms (TREE_TYPE (t
));
2490 if (uses_template_parms (TYPE_DOMAIN (t
)))
2492 return uses_template_parms (TREE_TYPE (t
));
2494 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
2496 return uses_template_parms (TREE_TYPE (t
));
2498 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
2500 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2502 return uses_template_parms (TREE_TYPE (t
));
2506 return uses_template_parms (TREE_TYPE (t
));
2509 /* A template template parameter is encountered */
2510 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
2511 /* We are parsing a template declaration */
2513 /* We are instantiating templates with template template
2518 if (uses_template_parms (DECL_INITIAL (t
)))
2520 goto check_type_and_context
;
2524 /* ??? What about FIELD_DECLs? */
2525 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
2526 && uses_template_parms (DECL_TI_ARGS (t
)))
2530 check_type_and_context
:
2531 if (uses_template_parms (TREE_TYPE (t
)))
2533 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
2538 return uses_template_parms (TREE_TYPE (t
));
2540 return uses_template_parms (TREE_OPERAND (t
, 0));
2542 /* template parm nodes */
2543 case TEMPLATE_TYPE_PARM
:
2544 case TEMPLATE_TEMPLATE_PARM
:
2545 case TEMPLATE_CONST_PARM
:
2548 /* simple type nodes */
2550 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
2552 return uses_template_parms (TYPE_MAX_VALUE (t
));
2564 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
2565 if (uses_template_parms (TREE_VALUE (v
)))
2577 /* Non-error_mark_node ERROR_MARKs are bad things. */
2578 my_friendly_assert (t
== error_mark_node
, 274);
2587 return uses_template_parms (TREE_OPERAND (t
, 0));
2590 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2591 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2592 return uses_template_parms (TREE_OPERAND (t
, 1));
2596 case REINTERPRET_CAST_EXPR
:
2597 case CONST_CAST_EXPR
:
2598 case STATIC_CAST_EXPR
:
2599 case DYNAMIC_CAST_EXPR
:
2607 return uses_template_parms (TREE_OPERAND (t
, 0));
2610 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
2618 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
2619 if (uses_template_parms (TREE_OPERAND (t
, i
)))
2626 sorry ("testing %s for template parms",
2627 tree_code_name
[(int) TREE_CODE (t
)]);
2628 my_friendly_abort (82);
2634 static struct tinst_level
*current_tinst_level
= 0;
2635 static struct tinst_level
*free_tinst_level
= 0;
2636 static int tinst_depth
= 0;
2637 extern int max_tinst_depth
;
2638 #ifdef GATHER_STATISTICS
2639 int depth_reached
= 0;
2643 push_tinst_level (d
)
2646 struct tinst_level
*new;
2648 if (tinst_depth
>= max_tinst_depth
)
2650 struct tinst_level
*p
= current_tinst_level
;
2652 char *file
= input_filename
;
2654 error ("template instantiation depth exceeds maximum of %d",
2656 error (" (use -ftemplate-depth-NN to increase the maximum)");
2657 cp_error (" instantiating `%D'", d
);
2659 for (; p
; p
= p
->next
)
2661 cp_error (" instantiated from `%D'", p
->decl
);
2663 input_filename
= p
->file
;
2665 error (" instantiated from here");
2668 input_filename
= file
;
2673 if (free_tinst_level
)
2675 new = free_tinst_level
;
2676 free_tinst_level
= new->next
;
2679 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
2683 new->file
= input_filename
;
2684 new->next
= current_tinst_level
;
2685 current_tinst_level
= new;
2688 #ifdef GATHER_STATISTICS
2689 if (tinst_depth
> depth_reached
)
2690 depth_reached
= tinst_depth
;
2699 struct tinst_level
*old
= current_tinst_level
;
2701 current_tinst_level
= old
->next
;
2702 old
->next
= free_tinst_level
;
2703 free_tinst_level
= old
;
2707 struct tinst_level
*
2710 struct tinst_level
*p
= current_tinst_level
;
2713 for (; p
->next
; p
= p
->next
)
2719 instantiate_class_template (type
)
2722 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
2724 if (type
== error_mark_node
)
2725 return error_mark_node
;
2727 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
2729 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
2732 template = TI_TEMPLATE (template_info
);
2733 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
2734 args
= TI_ARGS (template_info
);
2736 t
= most_specialized_class
2737 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
2739 if (t
== error_mark_node
)
2741 char *str
= "candidates are:";
2742 cp_error ("ambiguous class template instantiation for `%#T'", type
);
2743 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
2745 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
2747 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
2751 TYPE_BEING_DEFINED (type
) = 1;
2752 return error_mark_node
;
2755 pattern
= TREE_TYPE (t
);
2757 pattern
= TREE_TYPE (template);
2759 if (TYPE_SIZE (pattern
) == NULL_TREE
)
2763 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
2765 TYPE_BEING_DEFINED (type
) = 1;
2767 if (! push_tinst_level (type
))
2770 maybe_push_to_top_level (uses_template_parms (type
));
2771 pushclass (type
, 0);
2773 if (flag_external_templates
)
2775 if (flag_alt_external_templates
)
2777 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
2778 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
2779 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2780 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2781 && CLASSTYPE_INTERFACE_KNOWN (type
);
2785 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
2786 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2787 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
2788 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2789 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2790 && CLASSTYPE_INTERFACE_KNOWN (type
);
2795 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
2796 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
2799 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
2800 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
2801 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
2802 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
2803 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
2804 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
2805 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
2806 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
2807 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
2808 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
2809 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
2810 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
2811 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
2812 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
2813 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
2814 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
2815 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
2816 TYPE_USES_COMPLEX_INHERITANCE (type
)
2817 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
2818 TYPE_USES_MULTIPLE_INHERITANCE (type
)
2819 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
2820 TYPE_USES_VIRTUAL_BASECLASSES (type
)
2821 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
2822 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
2823 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
2826 tree binfo
= TYPE_BINFO (type
);
2827 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
2833 int len
= TREE_VEC_LENGTH (pbases
);
2834 bases
= make_tree_vec (len
);
2835 for (i
= 0; i
< len
; ++i
)
2839 TREE_VEC_ELT (bases
, i
) = elt
2840 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
2841 TREE_VEC_LENGTH (args
), NULL_TREE
);
2842 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
2844 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
2846 ("base type `%T' of `%T' fails to be a struct or class type",
2847 TREE_TYPE (elt
), type
);
2848 else if (! uses_template_parms (type
)
2849 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
2851 cp_error ("base class `%T' of `%T' has incomplete type",
2852 TREE_TYPE (elt
), type
);
2854 /* Don't initialize this until the vector is filled out, or
2855 lookups will crash. */
2856 BINFO_BASETYPES (binfo
) = bases
;
2860 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
2862 field_chain
= &TYPE_FIELDS (type
);
2864 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
2866 tree tag
= TREE_VALUE (t
);
2868 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
2869 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
2872 tsubst_enum (tag
, args
, TREE_VEC_LENGTH (args
), field_chain
);
2874 while (*field_chain
)
2876 DECL_FIELD_CONTEXT (*field_chain
) = type
;
2877 field_chain
= &TREE_CHAIN (*field_chain
);
2882 TREE_VEC_LENGTH (args
), NULL_TREE
);
2885 /* Don't replace enum constants here. */
2886 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
2887 if (TREE_CODE (t
) != CONST_DECL
)
2889 tree r
= tsubst (t
, args
,
2890 TREE_VEC_LENGTH (args
), NULL_TREE
);
2891 if (TREE_CODE (r
) == VAR_DECL
)
2893 if (! uses_template_parms (r
))
2894 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
2895 /* Perhaps I should do more of grokfield here. */
2897 DECL_IN_AGGR_P (r
) = 1;
2898 DECL_EXTERNAL (r
) = 1;
2899 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
2903 field_chain
= &TREE_CHAIN (r
);
2906 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
2907 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
2909 if (DECL_CONSTRUCTOR_P (t
))
2910 grok_ctor_properties (type
, t
);
2911 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
2912 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
2915 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
2916 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
2917 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
2920 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
2921 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
2922 TREE_VEC_LENGTH (args
), NULL_TREE
);
2924 /* This does injection for friend classes. */
2925 for (; d
; d
= TREE_CHAIN (d
))
2926 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
2928 /* This does injection for friend functions. */
2929 if (!processing_template_decl
)
2931 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
2932 TREE_VEC_LENGTH (args
), NULL_TREE
);
2934 for (; d
; d
= TREE_CHAIN (d
))
2936 tree t
= TREE_VALUE (d
);
2938 if (TREE_CODE (t
) == TYPE_DECL
)
2939 /* Already injected. */;
2946 if (! uses_template_parms (type
))
2949 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
2950 if (TREE_CODE (tmp
) == FIELD_DECL
)
2952 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
2953 require_complete_type (tmp
);
2956 type
= finish_struct_1 (type
, 0);
2957 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
2959 repo_template_used (type
);
2960 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
2961 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
2965 TYPE_SIZE (type
) = integer_zero_node
;
2966 CLASSTYPE_METHOD_VEC (type
)
2967 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
2970 TYPE_BEING_DEFINED (type
) = 0;
2973 pop_from_top_level ();
2983 if (t1
== NULL_TREE
)
2984 return t2
== NULL_TREE
;
2985 if (t2
== NULL_TREE
)
2987 /* Don't care if one declares its arg const and the other doesn't -- the
2988 main variant of the arg type is all that matters. */
2989 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2990 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2992 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2996 lookup_nested_type_by_name (ctype
, name
)
3001 complete_type (ctype
);
3003 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
3005 if (name
== TREE_PURPOSE (t
)
3006 /* this catches typedef enum { foo } bar; */
3007 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
3008 return TREE_VALUE (t
);
3013 /* If arg is a non-type template parameter that does not depend on template
3014 arguments, fold it like we weren't in the body of a template. */
3017 maybe_fold_nontype_arg (arg
)
3020 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
3021 && !uses_template_parms (arg
))
3023 /* Sometimes, one of the args was an expression involving a
3024 template constant parameter, like N - 1. Now that we've
3025 tsubst'd, we might have something like 2 - 1. This will
3026 confuse lookup_template_class, so we do constant folding
3027 here. We have to unset processing_template_decl, to
3028 fool build_expr_from_tree() into building an actual
3031 int saved_processing_template_decl
= processing_template_decl
;
3032 processing_template_decl
= 0;
3033 arg
= fold (build_expr_from_tree (arg
));
3034 processing_template_decl
= saved_processing_template_decl
;
3039 /* Take the tree structure T and replace template parameters used therein
3040 with the argument vector ARGS. NARGS is the number of args; should
3041 be removed. IN_DECL is an associated decl for diagnostics.
3043 tsubst is used for dealing with types, decls and the like; for
3044 expressions, use tsubst_expr or tsubst_copy. */
3047 tsubst (t
, args
, nargs
, in_decl
)
3054 if (t
== NULL_TREE
|| t
== error_mark_node
3055 || t
== integer_type_node
3056 || t
== void_type_node
3057 || t
== char_type_node
)
3060 type
= TREE_TYPE (t
);
3061 if (type
== unknown_type_node
)
3062 my_friendly_abort (42);
3063 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3064 && TREE_CODE (t
) != TYPENAME_TYPE
)
3065 type
= tsubst (type
, args
, nargs
, in_decl
);
3067 switch (TREE_CODE (t
))
3070 if (TYPE_PTRMEMFUNC_P (t
))
3072 tree r
= build_ptrmemfunc_type
3073 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
3074 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3078 /* else fall through */
3080 if (uses_template_parms (t
))
3082 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
3088 ? tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
) : NULL_TREE
;
3090 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3092 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3096 /* else fall through */
3098 case IDENTIFIER_NODE
:
3111 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3112 if (ctx
== NULL_TREE
)
3114 else if (ctx
== current_function_decl
)
3115 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3117 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3121 if (t
== integer_type_node
)
3124 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3125 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3129 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3130 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
3131 if (processing_template_decl
)
3133 tree itype
= make_node (INTEGER_TYPE
);
3134 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3135 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3140 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3141 return build_index_2_type (size_zero_node
, max
);
3144 case TEMPLATE_TYPE_PARM
:
3145 case TEMPLATE_TEMPLATE_PARM
:
3146 case TEMPLATE_CONST_PARM
:
3151 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3152 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3154 idx
= TEMPLATE_TYPE_IDX (t
);
3155 level
= TEMPLATE_TYPE_LEVEL (t
);
3159 idx
= TEMPLATE_CONST_IDX (t
);
3160 level
= TEMPLATE_CONST_LEVEL (t
);
3163 if (TREE_VEC_LENGTH (args
) > 0)
3165 tree arg
= NULL_TREE
;
3167 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3169 if (TREE_VEC_LENGTH (args
) >= level
- 1)
3171 (TREE_VEC_ELT (args
, level
- 1), idx
);
3173 else if (level
== 1)
3174 arg
= TREE_VEC_ELT (args
, idx
);
3176 if (arg
!= NULL_TREE
)
3178 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3179 return cp_build_type_variant
3180 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3181 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3182 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3184 if (CLASSTYPE_TEMPLATE_INFO (t
))
3186 /* We are processing a type constructed from
3187 a template template parameter */
3188 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3189 args
, nargs
, in_decl
);
3192 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3193 we are resolving nested-types in the signature of
3194 a member function templates.
3195 Otherwise ARG is a TEMPLATE_DECL and is the real
3196 template to be instantiated. */
3197 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3198 arg
= TYPE_NAME (arg
);
3200 r
= lookup_template_class (DECL_NAME (arg
),
3202 DECL_CONTEXT (arg
));
3203 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3207 /* We are processing a template argument list. */
3215 /* If we get here, we must have been looking at a parm for a
3216 more deeply nested template. */
3217 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
3218 && TEMPLATE_CONST_LEVEL (t
) > 1)
3219 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3220 && TEMPLATE_TYPE_LEVEL (t
) > 1)
3221 || (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3222 && TEMPLATE_TYPE_LEVEL (t
) > 1),
3229 /* We can get here when processing a member template function
3230 of a template class. */
3232 tree decl
= DECL_TEMPLATE_RESULT (t
);
3237 /* We might already have an instance of this template. */
3238 spec
= retrieve_specialization (t
, args
);
3239 if (spec
!= NULL_TREE
)
3242 /* Make a new template decl. It will be similar to the
3243 original, but will record the current template arguments.
3244 We also create a new function declaration, which is just
3245 like the old one, but points to this new template, rather
3246 than the old one. */
3247 tmpl
= copy_node (t
);
3248 copy_lang_decl (tmpl
);
3249 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3250 DECL_CHAIN (tmpl
) = NULL_TREE
;
3251 TREE_CHAIN (tmpl
) = NULL_TREE
;
3252 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
3253 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
3254 DECL_RESULT (tmpl
) = new_decl
;
3255 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
3256 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3257 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
3258 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
3260 /* The template parameters for this new template are all the
3261 template parameters for the old template, except the
3262 outermost level of parameters. */
3263 DECL_TEMPLATE_PARMS (tmpl
)
3264 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
3265 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
3266 TREE_CHAIN (parms
) != NULL_TREE
;
3267 parms
= TREE_CHAIN (parms
))
3268 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
3270 /* What should we do with the specializations of this member
3271 template? Are they specializations of this new template,
3272 or instantiations of the templates they previously were?
3273 this new template? And where should their
3274 DECL_TI_TEMPLATES point? */
3275 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
3276 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
3278 spec
= TREE_CHAIN (spec
))
3280 /* It helps to consider example here. Consider:
3291 Now, for example, we are instantiating S<int>::f(U u).
3292 We want to make a template:
3297 It will have a specialization, for the case U = int*, of
3301 void S<int>::f<int*>(int*);
3303 This specialization will be an instantiation of
3304 the specialization given in the declaration of S, with
3305 argument list int*. */
3307 tree fn
= TREE_VALUE (spec
);
3311 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
3312 /* Instantiations are on the same list, but they're of
3313 no concern to us. */
3316 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
, nargs
,
3318 new_fn
= tsubst (DECL_RESULT (fn
), args
, nargs
,
3320 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
3321 perm_tree_cons (spec_args
, new_fn
,
3322 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
3325 /* Record this partial instantiation. */
3326 register_specialization (tmpl
, t
, args
);
3338 if (DECL_CONTEXT (t
) != NULL_TREE
3339 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
3341 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
3345 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
3346 type
= tsubst (type
, args
, nargs
, in_decl
);
3352 type
= tsubst (type
, args
, nargs
, in_decl
);
3355 /* Do we already have this instantiation? */
3356 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3358 tree tmpl
= DECL_TI_TEMPLATE (t
);
3359 tree spec
= retrieve_specialization (tmpl
, args
);
3365 /* We do NOT check for matching decls pushed separately at this
3366 point, as they may not represent instantiations of this
3367 template, and in any case are considered separate under the
3368 discrete model. Instead, see add_maybe_template. */
3372 DECL_USE_TEMPLATE (r
) = 0;
3373 TREE_TYPE (r
) = type
;
3376 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
3377 DECL_CLASS_CONTEXT (r
) = ctx
;
3379 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
3380 IDENTIFIER_POINTER (DECL_NAME (r
)),
3381 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
3383 /* Type-conversion operator. Reconstruct the name, in
3384 case it's the name of one of the template's parameters. */
3385 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
3388 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
3390 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
3391 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
3392 buf
= (char *) alloca (strlen (dbuf
)
3393 + sizeof (DESTRUCTOR_DECL_PREFIX
));
3394 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
3397 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
3401 /* Instantiations of template functions must be mangled
3402 specially, in order to conform to 14.5.5.1
3403 [temp.over.link]. We use in_decl below rather than
3404 DECL_TI_TEMPLATE (r) because the latter is set to
3405 NULL_TREE in instantiate_decl. */
3409 if (DECL_TEMPLATE_INFO (r
))
3410 tmpl
= DECL_TI_TEMPLATE (r
);
3414 /* tmpl will be NULL if this is a specialization of a
3415 member template of a template class. */
3416 if (name_mangling_version
< 1
3417 || tmpl
== NULL_TREE
3418 || (member
&& !is_member_template (tmpl
)
3419 && !DECL_TEMPLATE_INFO (tmpl
)))
3421 arg_types
= TYPE_ARG_TYPES (type
);
3422 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3423 arg_types
= hash_tree_chain
3424 (build_pointer_type (DECL_CONTEXT (r
)),
3427 DECL_ASSEMBLER_NAME (r
)
3428 = build_decl_overload (DECL_NAME (r
), arg_types
,
3433 /* We pass the outermost template parameters to
3434 build_template_decl_overload since the innermost
3435 template parameters are still just template
3436 parameters; there are no corresponding subsitution
3438 /* FIXME The messed up thing here is that we get here with
3439 full args and only one level of parms. This is necessary
3440 because when we partially instantiate a member template,
3441 even though there's really only one level of parms left
3442 we re-use the parms from the original template, which
3443 have level 2. When this is fixed we can remove the
3444 add_to_template_args from instantiate_template. */
3448 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3450 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
3452 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
3453 tparms
= TREE_CHAIN (tparms
);
3456 (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
3457 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1)
3462 /* If the template is a specialization, then it is
3463 a member template specialization. We have
3466 template <class T> struct S {
3467 template <int i> void f();
3468 template <> void f<7>();
3471 and now we are forming S<double>::f<7>.
3472 Therefore, the template parameters of interest
3473 are those that are specialized by the template
3474 (i.e., the int), not those we are using to
3475 instantiate the template, i.e. the double. */
3476 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
3477 targs
= DECL_TI_ARGS (tmpl
);
3480 my_friendly_assert (tparms
!= NULL_TREE
3481 && TREE_CODE (tparms
) == TREE_LIST
,
3483 tparms
= TREE_VALUE (tparms
);
3485 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
3486 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3487 arg_types
= hash_tree_chain
3488 (build_pointer_type (DECL_CONTEXT (r
)),
3491 DECL_ASSEMBLER_NAME (r
)
3492 = build_template_decl_overload
3493 (DECL_NAME (r
), arg_types
,
3494 TREE_TYPE (TREE_TYPE (tmpl
)),
3495 tparms
, targs
, member
);
3499 make_decl_rtl (r
, NULL_PTR
, 1);
3501 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
3502 DECL_MAIN_VARIANT (r
) = r
;
3503 DECL_RESULT (r
) = NULL_TREE
;
3504 DECL_INITIAL (r
) = NULL_TREE
;
3506 TREE_STATIC (r
) = 0;
3507 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
3508 DECL_EXTERNAL (r
) = 1;
3509 DECL_INTERFACE_KNOWN (r
) = 0;
3510 DECL_DEFER_OUTPUT (r
) = 0;
3511 TREE_CHAIN (r
) = NULL_TREE
;
3512 DECL_CHAIN (r
) = NULL_TREE
;
3514 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
3515 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
3517 /* Look for matching decls for the moment. */
3518 if (! member
&& ! flag_ansi_overloading
)
3520 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
3523 if (decls
== NULL_TREE
)
3525 else if (is_overloaded_fn (decls
))
3526 for (decls
= get_first_fn (decls
); decls
;
3527 decls
= DECL_CHAIN (decls
))
3529 if (TREE_CODE (decls
) == FUNCTION_DECL
3530 && TREE_TYPE (decls
) == type
)
3539 int dcl_only
= ! DECL_INITIAL (d
);
3541 DECL_INITIAL (r
) = error_mark_node
;
3542 duplicate_decls (r
, d
);
3545 DECL_INITIAL (r
) = 0;
3549 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3551 tree tmpl
= DECL_TI_TEMPLATE (t
);
3552 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3554 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3556 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3557 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
3559 /* In this case, we are instantiating a
3560 specialization. The innermost template args are
3561 already given by the specialization. */
3562 argvec
= add_to_template_args (argvec
, DECL_TI_ARGS (tmpl
));
3565 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3567 /* If we're not using ANSI overloading, then we might have
3568 called duplicate_decls above, and gotten back an
3569 preexisting version of this function. We treat such a
3570 function as a specialization. Otherwise, we cleared
3571 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
3572 this condition will be false. */
3573 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
3574 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
3576 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3578 register_specialization (r
, tmpl
, argvec
);
3581 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
3582 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
3584 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
3585 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
3592 tree r
= copy_node (t
);
3593 TREE_TYPE (r
) = type
;
3594 DECL_INITIAL (r
) = TREE_TYPE (r
);
3595 DECL_CONTEXT (r
) = NULL_TREE
;
3596 #ifdef PROMOTE_PROTOTYPES
3597 if ((TREE_CODE (type
) == INTEGER_TYPE
3598 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3599 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
3600 DECL_ARG_TYPE (r
) = integer_type_node
;
3603 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
3609 tree r
= copy_node (t
);
3610 TREE_TYPE (r
) = type
;
3613 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
3615 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3616 TREE_CHAIN (r
) = NULL_TREE
;
3622 tree r
= copy_node (t
);
3624 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3625 TREE_CHAIN (r
) = NULL_TREE
;
3632 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
3634 /* Do we already have this instantiation? */
3635 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3637 tree tmpl
= DECL_TI_TEMPLATE (t
);
3638 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3640 for (; decls
; decls
= TREE_CHAIN (decls
))
3641 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
3642 return TREE_VALUE (decls
);
3646 TREE_TYPE (r
) = type
;
3647 DECL_CONTEXT (r
) = ctx
;
3648 if (TREE_STATIC (r
))
3649 DECL_ASSEMBLER_NAME (r
)
3650 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
3652 /* Don't try to expand the initializer until someone tries to use
3653 this variable; otherwise we run into circular dependencies. */
3654 DECL_INITIAL (r
) = NULL_TREE
;
3659 if (DECL_LANG_SPECIFIC (r
))
3662 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
3665 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3667 tree tmpl
= DECL_TI_TEMPLATE (t
);
3668 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3669 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3671 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3672 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
3673 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3675 TREE_CHAIN (r
) = NULL_TREE
;
3680 if (t
== TYPE_NAME (TREE_TYPE (t
)))
3681 return TYPE_NAME (type
);
3684 tree r
= copy_node (t
);
3685 TREE_TYPE (r
) = type
;
3686 DECL_CONTEXT (r
) = current_class_type
;
3687 TREE_CHAIN (r
) = NULL_TREE
;
3693 tree purpose
, value
, chain
, result
;
3694 int via_public
, via_virtual
, via_protected
;
3696 if (t
== void_list_node
)
3699 via_public
= TREE_VIA_PUBLIC (t
);
3700 via_protected
= TREE_VIA_PROTECTED (t
);
3701 via_virtual
= TREE_VIA_VIRTUAL (t
);
3703 purpose
= TREE_PURPOSE (t
);
3705 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
3706 value
= TREE_VALUE (t
);
3708 value
= tsubst (value
, args
, nargs
, in_decl
);
3709 chain
= TREE_CHAIN (t
);
3710 if (chain
&& chain
!= void_type_node
)
3711 chain
= tsubst (chain
, args
, nargs
, in_decl
);
3712 if (purpose
== TREE_PURPOSE (t
)
3713 && value
== TREE_VALUE (t
)
3714 && chain
== TREE_CHAIN (t
))
3716 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
3717 purpose
, value
, chain
);
3718 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
3722 if (type
!= NULL_TREE
)
3728 if (type
== TREE_TYPE (t
))
3731 TREE_TYPE (t
) = complete_type (type
);
3732 if (IS_AGGR_TYPE (type
))
3734 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
3735 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
3736 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
3737 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
3742 /* Otherwise, a vector of template arguments. */
3744 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
3745 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
3747 bzero ((char *) elts
, len
* sizeof (tree
));
3749 for (i
= 0; i
< len
; i
++)
3751 elts
[i
] = maybe_fold_nontype_arg
3752 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
));
3754 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
3761 t
= make_tree_vec (len
);
3762 for (i
= 0; i
< len
; i
++)
3763 TREE_VEC_ELT (t
, i
) = elts
[i
];
3768 case REFERENCE_TYPE
:
3771 enum tree_code code
;
3773 if (type
== TREE_TYPE (t
))
3776 code
= TREE_CODE (t
);
3777 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3779 static int last_line
= 0;
3780 static char* last_file
= 0;
3782 /* We keep track of the last time we issued this error
3783 message to avoid spewing a ton of messages during a
3784 single bad template instantiation. */
3785 if (last_line
!= lineno
||
3786 last_file
!= input_filename
)
3788 cp_error ("cannot form type %s to reference type %T during template instantiation",
3789 (code
== POINTER_TYPE
) ? "pointer" : "reference",
3792 last_file
= input_filename
;
3795 /* Use the underlying type in an attempt at error
3796 recovery; maybe the user meant vector<int> and wrote
3797 vector<int&>, or some such. */
3798 if (code
== REFERENCE_TYPE
)
3801 r
= build_pointer_type (TREE_TYPE (type
));
3803 else if (code
== POINTER_TYPE
)
3804 r
= build_pointer_type (type
);
3806 r
= build_reference_type (type
);
3807 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
3809 /* Will this ever be needed for TYPE_..._TO values? */
3814 return build_offset_type
3815 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
3819 tree values
= TYPE_ARG_TYPES (t
);
3820 tree context
= TYPE_CONTEXT (t
);
3821 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
3824 /* Don't bother recursing if we know it won't change anything. */
3825 if (values
!= void_list_node
)
3827 /* This should probably be rewritten to use hash_tree_cons for
3828 the memory savings. */
3829 tree first
= NULL_TREE
;
3830 tree last
= NULL_TREE
;
3832 for (; values
&& values
!= void_list_node
;
3833 values
= TREE_CHAIN (values
))
3835 tree value
= TYPE_MAIN_VARIANT (type_decays_to
3836 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
3837 /* Don't instantiate default args unless they are used.
3838 Handle it in build_over_call instead. */
3839 tree purpose
= TREE_PURPOSE (values
);
3840 tree x
= build_tree_list (purpose
, value
);
3843 TREE_CHAIN (last
) = x
;
3849 if (values
== void_list_node
)
3850 TREE_CHAIN (last
) = void_list_node
;
3855 context
= tsubst (context
, args
, nargs
, in_decl
);
3856 /* Could also optimize cases where return value and
3857 values have common elements (e.g., T min(const &T, const T&). */
3859 /* If the above parameters haven't changed, just return the type. */
3860 if (type
== TREE_TYPE (t
)
3861 && values
== TYPE_VALUES (t
)
3862 && context
== TYPE_CONTEXT (t
))
3865 /* Construct a new type node and return it. */
3866 if (TREE_CODE (t
) == FUNCTION_TYPE
3867 && context
== NULL_TREE
)
3869 fntype
= build_function_type (type
, values
);
3871 else if (context
== NULL_TREE
)
3873 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
3874 args
, nargs
, in_decl
);
3875 fntype
= build_cplus_method_type (base
, type
,
3876 TREE_CHAIN (values
));
3880 fntype
= make_node (TREE_CODE (t
));
3881 TREE_TYPE (fntype
) = type
;
3882 TYPE_CONTEXT (fntype
) = context
;
3883 TYPE_VALUES (fntype
) = values
;
3884 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
3885 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
3886 TYPE_MODE (fntype
) = TYPE_MODE (t
);
3887 if (TYPE_METHOD_BASETYPE (t
))
3888 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
3889 args
, nargs
, in_decl
);
3890 /* Need to generate hash value. */
3891 my_friendly_abort (84);
3893 fntype
= build_type_variant (fntype
,
3898 raises
= tsubst (raises
, args
, nargs
, in_decl
);
3899 fntype
= build_exception_variant (fntype
, raises
);
3905 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
3907 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
3909 r
= build_cplus_array_type (type
, domain
);
3915 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
3916 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3917 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
3921 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
3922 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
3926 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3927 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
3928 return cp_build_type_variant
3929 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
3930 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
3934 return make_pointer_declarator
3935 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3938 return make_reference_declarator
3939 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3942 return build_parse_node
3943 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3944 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3947 return make_call_declarator
3948 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3949 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3950 TREE_OPERAND (t
, 2),
3951 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3954 return build_parse_node
3955 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3956 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3959 sorry ("use of `%s' in template",
3960 tree_code_name
[(int) TREE_CODE (t
)]);
3961 return error_mark_node
;
3968 emit_line_note (input_filename
, lineno
);
3972 expand_start_bindings (0);
3979 int saved_warn_unused
= 0;
3981 if (processing_template_decl
)
3983 saved_warn_unused
= warn_unused
;
3986 expand_end_bindings (getdecls (), kept_level_p (), 0);
3987 if (processing_template_decl
)
3988 warn_unused
= saved_warn_unused
;
3989 t
= poplevel (kept_level_p (), 1, 0);
3994 /* Like tsubst, but deals with expressions. This function just replaces
3995 template parms; to finish processing the resultant expression, use
3999 tsubst_copy (t
, args
, nargs
, in_decl
)
4004 enum tree_code code
;
4006 if (t
== NULL_TREE
|| t
== error_mark_node
)
4009 code
= TREE_CODE (t
);
4014 return do_identifier (DECL_NAME (t
), 0);
4018 if (DECL_CONTEXT (t
))
4020 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
4021 if (ctx
== current_function_decl
)
4022 return lookup_name (DECL_NAME (t
), 0);
4023 else if (ctx
!= DECL_CONTEXT (t
))
4024 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
4030 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4031 t
= tsubst (t
, args
, nargs
, in_decl
);
4036 if (is_member_template (t
))
4037 return tsubst (t
, args
, nargs
, in_decl
);
4042 case IDENTIFIER_NODE
:
4043 return do_identifier (t
, 0);
4047 case REINTERPRET_CAST_EXPR
:
4048 case CONST_CAST_EXPR
:
4049 case STATIC_CAST_EXPR
:
4050 case DYNAMIC_CAST_EXPR
:
4052 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
4053 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4056 case PREDECREMENT_EXPR
:
4057 case PREINCREMENT_EXPR
:
4058 case POSTDECREMENT_EXPR
:
4059 case POSTINCREMENT_EXPR
:
4061 case TRUTH_NOT_EXPR
:
4064 case CONVERT_EXPR
: /* Unary + */
4072 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4077 case TRUNC_DIV_EXPR
:
4079 case FLOOR_DIV_EXPR
:
4080 case ROUND_DIV_EXPR
:
4081 case EXACT_DIV_EXPR
:
4083 case BIT_ANDTC_EXPR
:
4086 case TRUNC_MOD_EXPR
:
4087 case FLOOR_MOD_EXPR
:
4088 case TRUTH_ANDIF_EXPR
:
4089 case TRUTH_ORIF_EXPR
:
4090 case TRUTH_AND_EXPR
:
4111 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4112 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4116 tree fn
= TREE_OPERAND (t
, 0);
4117 if (really_overloaded_fn (fn
))
4118 fn
= tsubst_copy (get_first_fn (fn
), args
, nargs
, in_decl
);
4120 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
4122 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4126 case METHOD_CALL_EXPR
:
4128 tree name
= TREE_OPERAND (t
, 0);
4129 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4131 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4132 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4134 else if (TREE_CODE (name
) == SCOPE_REF
4135 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4137 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4138 name
= TREE_OPERAND (name
, 1);
4139 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4140 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4141 name
= build_nt (SCOPE_REF
, base
, name
);
4144 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4146 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4147 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
4156 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4157 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4158 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4160 if (code
== BIND_EXPR
&& !processing_template_decl
)
4162 /* This processing should really occur in tsubst_expr,
4163 However, tsubst_expr does not recurse into expressions,
4164 since it assumes that there aren't any statements
4165 inside them. Instead, it simply calls
4166 build_expr_from_tree. So, we need to expand the
4168 tree rtl_exp
= expand_start_stmt_expr();
4169 tsubst_expr (TREE_OPERAND (r
, 1), args
, nargs
, in_decl
);
4170 rtl_exp
= expand_end_stmt_expr (rtl_exp
);
4171 TREE_SIDE_EFFECTS (rtl_exp
) = 1;
4172 return build (BIND_EXPR
, TREE_TYPE (rtl_exp
),
4173 NULL_TREE
, rtl_exp
, TREE_OPERAND (r
, 2));
4182 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4183 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4184 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4185 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4192 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4193 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4194 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4195 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4199 case TEMPLATE_ID_EXPR
:
4201 /* Substituted template arguments */
4202 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4204 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4205 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4207 return lookup_template_function
4208 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
), targs
);
4213 tree purpose
, value
, chain
;
4215 if (t
== void_list_node
)
4218 purpose
= TREE_PURPOSE (t
);
4220 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
4221 value
= TREE_VALUE (t
);
4223 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
4224 chain
= TREE_CHAIN (t
);
4225 if (chain
&& chain
!= void_type_node
)
4226 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
4227 if (purpose
== TREE_PURPOSE (t
)
4228 && value
== TREE_VALUE (t
)
4229 && chain
== TREE_CHAIN (t
))
4231 return tree_cons (purpose
, value
, chain
);
4238 case TEMPLATE_TYPE_PARM
:
4239 case TEMPLATE_TEMPLATE_PARM
:
4240 case TEMPLATE_CONST_PARM
:
4242 case REFERENCE_TYPE
:
4248 return tsubst (t
, args
, nargs
, in_decl
);
4250 case IDENTIFIER_NODE
:
4251 if (IDENTIFIER_TYPENAME_P (t
))
4252 return build_typename_overload
4253 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
4259 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
4260 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
4267 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
4270 tsubst_expr (t
, args
, nargs
, in_decl
)
4275 if (t
== NULL_TREE
|| t
== error_mark_node
)
4278 if (processing_template_decl
)
4279 return tsubst_copy (t
, args
, nargs
, in_decl
);
4281 switch (TREE_CODE (t
))
4284 lineno
= TREE_COMPLEXITY (t
);
4285 emit_line_note (input_filename
, lineno
);
4287 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4292 lineno
= TREE_COMPLEXITY (t
);
4293 emit_line_note (input_filename
, lineno
);
4294 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4295 /* Do default conversion if safe and possibly important,
4296 in case within ({...}). */
4297 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
4298 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
4299 t
= default_conversion (t
);
4300 cplus_expand_expr_stmt (t
);
4307 int i
= suspend_momentary ();
4310 lineno
= TREE_COMPLEXITY (t
);
4311 emit_line_note (input_filename
, lineno
);
4313 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4314 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4315 TREE_OPERAND (t
, 2) != 0);
4316 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4318 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
4319 resume_momentary (i
);
4326 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
4327 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4328 int cond_scope
= (TREE_OPERAND (t
, 1)
4329 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
4331 lineno
= TREE_COMPLEXITY (t
);
4332 emit_line_note (input_filename
, lineno
);
4335 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
4336 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4338 emit_line_note (input_filename
, lineno
);
4339 expand_start_loop_continue_elsewhere (1);
4343 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4344 emit_line_note (input_filename
, lineno
);
4346 expand_exit_loop_if_false (0, condition_conversion (tmp
));
4350 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
4353 emit_line_note (input_filename
, lineno
);
4354 expand_loop_continue_here ();
4355 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4357 cplus_expand_expr_stmt (tmp
);
4370 lineno
= TREE_COMPLEXITY (t
);
4372 emit_line_note (input_filename
, lineno
);
4373 expand_start_loop (1);
4375 cond
= TREE_OPERAND (t
, 0);
4376 if (TREE_CODE (cond
) == DECL_STMT
)
4378 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
4379 emit_line_note (input_filename
, lineno
);
4380 expand_exit_loop_if_false (0, condition_conversion (cond
));
4382 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
4384 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4396 lineno
= TREE_COMPLEXITY (t
);
4398 emit_line_note (input_filename
, lineno
);
4399 expand_start_loop_continue_elsewhere (1);
4401 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4402 expand_loop_continue_here ();
4404 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4405 emit_line_note (input_filename
, lineno
);
4406 expand_exit_loop_if_false (0, condition_conversion (cond
));
4417 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4419 lineno
= TREE_COMPLEXITY (t
);
4422 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4423 emit_line_note (input_filename
, lineno
);
4424 expand_start_cond (condition_conversion (tmp
), 0);
4426 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4427 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4429 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
4431 expand_start_else ();
4432 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4446 tree substmt
= TREE_OPERAND (t
, 0);
4448 lineno
= TREE_COMPLEXITY (t
);
4450 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4453 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
4454 tsubst_expr (substmt
, args
, nargs
, in_decl
);
4456 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4462 lineno
= TREE_COMPLEXITY (t
);
4463 emit_line_note (input_filename
, lineno
);
4464 if (! expand_exit_something ())
4465 error ("break statement not within loop or switch");
4469 lineno
= TREE_COMPLEXITY (t
);
4470 emit_line_note (input_filename
, lineno
);
4471 if (! expand_continue_loop (0))
4472 error ("continue statement not within a loop");
4478 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4480 lineno
= TREE_COMPLEXITY (t
);
4483 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4484 emit_line_note (input_filename
, lineno
);
4485 c_expand_start_case (val
);
4488 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4489 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4491 expand_end_case (val
);
4502 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4503 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4507 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
4514 lineno
= TREE_COMPLEXITY (t
);
4515 emit_line_note (input_filename
, lineno
);
4516 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
4518 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
4519 TREE_USED (decl
) = 1;
4523 expand_computed_goto
4524 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4528 lineno
= TREE_COMPLEXITY (t
);
4529 emit_line_note (input_filename
, lineno
);
4530 expand_start_try_stmts ();
4531 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4532 expand_start_all_catch ();
4534 tree handler
= TREE_OPERAND (t
, 1);
4535 for (; handler
; handler
= TREE_CHAIN (handler
))
4536 tsubst_expr (handler
, args
, nargs
, in_decl
);
4538 expand_end_all_catch ();
4542 lineno
= TREE_COMPLEXITY (t
);
4544 if (TREE_OPERAND (t
, 0))
4546 tree d
= TREE_OPERAND (t
, 0);
4547 expand_start_catch_block
4548 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
4549 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
4552 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
4553 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4554 expand_end_catch_block ();
4559 lineno
= TREE_COMPLEXITY (t
);
4561 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
4562 tsubst_enum (t
, args
, nargs
, NULL
);
4566 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
4572 instantiate_template (tmpl
, targ_ptr
)
4573 tree tmpl
, targ_ptr
;
4577 struct obstack
*old_fmp_obstack
;
4578 extern struct obstack
*function_maybepermanent_obstack
;
4580 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
4582 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
4584 /* Check to see if we already have this specialization. */
4585 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
4587 if (spec
!= NULL_TREE
)
4591 push_obstacks (&permanent_obstack
, &permanent_obstack
);
4592 old_fmp_obstack
= function_maybepermanent_obstack
;
4593 function_maybepermanent_obstack
= &permanent_obstack
;
4595 len
= DECL_NTPARMS (tmpl
);
4600 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
4601 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
4603 tree nt
= target_type (t
);
4604 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
4606 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
4607 cp_error (" trying to instantiate `%D'", tmpl
);
4608 fndecl
= error_mark_node
;
4612 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
4614 targ_ptr
= copy_to_permanent (targ_ptr
);
4616 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
4617 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
4619 /* substitute template parameters */
4620 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
4622 if (flag_external_templates
)
4623 add_pending_template (fndecl
);
4626 function_maybepermanent_obstack
= old_fmp_obstack
;
4632 /* Push the name of the class template into the scope of the instantiation. */
4635 overload_template_name (type
)
4638 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
4641 if (IDENTIFIER_CLASS_VALUE (id
)
4642 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
4645 decl
= build_decl (TYPE_DECL
, id
, type
);
4646 SET_DECL_ARTIFICIAL (decl
);
4647 pushdecl_class_level (decl
);
4651 /* Like type_unification but designed specially to handle conversion
4652 operators. The EXTRA_FN_ARG, if any, is the type of an additional
4653 parameter to be added to the beginning of FN's parameter list. */
4656 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
4657 strict
, extra_fn_arg
)
4658 tree fn
, explicit_targs
, targs
, args
, return_type
;
4663 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4664 tree decl_arg_types
= args
;
4666 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
4668 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
4670 /* This is a template conversion operator. Use the return types
4671 as well as the argument types. */
4672 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
4673 TREE_TYPE (TREE_TYPE (fn
)),
4675 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
4680 if (extra_fn_arg
!= NULL_TREE
)
4681 fn_arg_types
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
,
4684 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
4685 &TREE_VEC_ELT (targs
, 0),
4695 /* Type unification.
4697 We have a function template signature with one or more references to
4698 template parameters, and a parameter list we wish to fit to this
4699 template. If possible, produce a list of parameters for the template
4700 which will cause it to fit the supplied parameter list.
4702 Return zero for success, 2 for an incomplete match that doesn't resolve
4703 all the types, and 1 for complete failure. An error message will be
4704 printed only for an incomplete match.
4706 TPARMS[NTPARMS] is an array of template parameter types;
4707 TARGS[NTPARMS] is the array of template parameter values. PARMS is
4708 the function template's signature (using TEMPLATE_PARM_IDX nodes),
4709 and ARGS is the argument list we're trying to match against it.
4711 If SUBR is 1, we're being called recursively (to unify the arguments of
4712 a function or method parameter of a function template), so don't zero
4713 out targs and don't fail on an incomplete match.
4715 If STRICT is 1, the match must be exact (for casts of overloaded
4716 addresses, explicit instantiation, and more_specialized). */
4719 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
4720 strict
, allow_incomplete
)
4721 tree tparms
, *targs
, parms
, args
, targs_in
;
4722 int *nsubsts
, strict
, allow_incomplete
;
4724 int ntparms
= TREE_VEC_LENGTH (tparms
);
4729 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
4731 if (targs_in
!= NULL_TREE
)
4734 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
4737 if (arg_vec
== error_mark_node
)
4741 i
< TREE_VEC_LENGTH (arg_vec
)
4742 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
4744 /* Insert the template argument. It is encoded as the operands
4745 of NOP_EXPRs so that unify can tell that it is an explicit
4747 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VEC_ELT (arg_vec
, i
));
4750 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
4751 strict
, allow_incomplete
);
4753 for (i
= 0, arg
= targs_in
;
4755 arg
= TREE_CHAIN (arg
), ++i
)
4756 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
4757 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
4764 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
4765 strict
, allow_incomplete
)
4766 tree tparms
, *targs
, parms
, args
;
4767 int *nsubsts
, subr
, strict
, allow_incomplete
;
4771 int ntparms
= TREE_VEC_LENGTH (tparms
);
4773 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
4774 my_friendly_assert (parms
== NULL_TREE
4775 || TREE_CODE (parms
) == TREE_LIST
, 290);
4776 /* ARGS could be NULL (via a call from parse.y to
4777 build_x_function_call). */
4779 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
4780 my_friendly_assert (ntparms
> 0, 292);
4783 && parms
!= void_list_node
4785 && args
!= void_list_node
)
4787 parm
= TREE_VALUE (parms
);
4788 parms
= TREE_CHAIN (parms
);
4789 arg
= TREE_VALUE (args
);
4790 args
= TREE_CHAIN (args
);
4792 if (arg
== error_mark_node
)
4794 if (arg
== unknown_type_node
)
4797 /* Conversions will be performed on a function argument that
4798 corresponds with a function parameter that contains only
4799 non-deducible template parameters and explicitly specified
4800 template parameters. */
4801 if (! uses_template_parms (parm
))
4805 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4806 type
= TREE_TYPE (arg
);
4815 if (comptypes (parm
, type
, 1))
4819 /* It might work; we shouldn't check now, because we might
4820 get into infinite recursion. Overload resolution will
4828 if (TREE_CODE (arg
) == VAR_DECL
)
4829 arg
= TREE_TYPE (arg
);
4830 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
4831 arg
= TREE_TYPE (arg
);
4833 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
4835 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
4836 if (TREE_CODE (arg
) == TREE_LIST
4837 && TREE_TYPE (arg
) == unknown_type_node
4838 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
4840 int nsubsts
, ntparms
;
4843 /* Have to back unify here */
4844 arg
= TREE_VALUE (arg
);
4846 ntparms
= DECL_NTPARMS (arg
);
4847 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
4848 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
4850 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
4852 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
4853 parm
, NULL_TREE
, &nsubsts
, strict
,
4856 arg
= TREE_TYPE (arg
);
4859 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
4861 warning ("using type void* for NULL");
4862 arg
= ptr_type_node
;
4865 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
4866 arg
= TREE_TYPE (arg
);
4868 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
4870 if (TREE_CODE (arg
) == FUNCTION_TYPE
4871 || TREE_CODE (arg
) == METHOD_TYPE
)
4872 arg
= build_pointer_type (arg
);
4873 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
4874 arg
= build_pointer_type (TREE_TYPE (arg
));
4876 arg
= TYPE_MAIN_VARIANT (arg
);
4879 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
4887 /* Fail if we've reached the end of the parm list, and more args
4888 are present, and the parm list isn't variadic. */
4889 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
4891 /* Fail if parms are left and they don't have default values. */
4893 && parms
!= void_list_node
4894 && TREE_PURPOSE (parms
) == NULL_TREE
)
4897 for (i
= 0; i
< ntparms
; i
++)
4900 if (!allow_incomplete
)
4901 error ("incomplete type unification");
4907 /* Tail recursion is your friend. */
4910 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
4911 tree tparms
, *targs
, parm
, arg
;
4912 int *nsubsts
, ntparms
, strict
;
4916 /* I don't think this will do the right thing with respect to types.
4917 But the only case I've seen it in so far has been array bounds, where
4918 signedness is the only information lost, and I think that will be
4920 while (TREE_CODE (parm
) == NOP_EXPR
)
4921 parm
= TREE_OPERAND (parm
, 0);
4923 if (arg
== error_mark_node
)
4925 if (arg
== unknown_type_node
)
4930 switch (TREE_CODE (parm
))
4933 /* In a type which contains a nested-name-specifier, template
4934 argument values cannot be deduced for template parameters used
4935 within the nested-name-specifier. */
4938 case TEMPLATE_TYPE_PARM
:
4940 idx
= TEMPLATE_TYPE_IDX (parm
);
4941 /* Check for mixed types and values. */
4942 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
4945 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4946 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4947 /* An explicit template argument. Don't even try to match
4948 here; the overload resolution code will manage check to
4949 see whether the call is legal. */
4952 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
4953 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
4956 /* Template type parameters cannot contain cv-quals; i.e.
4957 template <class T> void f (T& a, T& b) will not generate
4958 void f (const int& a, const int& b). */
4959 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
4960 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
4962 arg
= TYPE_MAIN_VARIANT (arg
);
4965 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
4966 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
4967 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
4970 /* Simple cases: Value already set, does match or doesn't. */
4971 if (targs
[idx
] == arg
4973 && TREE_CODE (targs
[idx
]) == NOP_EXPR
4974 && TREE_OPERAND (targs
[idx
], 0) == arg
))
4976 else if (targs
[idx
])
4981 case TEMPLATE_TEMPLATE_PARM
:
4983 idx
= TEMPLATE_TYPE_IDX (parm
);
4984 /* Check for mixed types and values. */
4985 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TEMPLATE_DECL
)
4988 if (!strict
&& targs
[idx
] != NULL_TREE
&&
4989 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
4990 /* An explicit template argument. Don't even try to match
4991 here; the overload resolution code will manage check to
4992 see whether the call is legal. */
4995 if (CLASSTYPE_TEMPLATE_INFO (parm
))
4997 /* We arrive here when PARM does not involve template
5000 /* ARG must be constructed from a template class. */
5001 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
5005 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
5006 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
5007 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
5009 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
5012 /* The parameter and argument roles have to be switched here
5013 in order to handle default arguments properly. For example,
5014 template<template <class> class TT> void f(TT<int>)
5015 should be able to accept vector<int> which comes from
5016 template <class T, class Allcator = allocator>
5019 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1)
5023 /* Deduce arguments T, i from TT<T> or TT<i>. */
5024 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
5026 tree t
= TREE_VEC_ELT (parmvec
, i
);
5027 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
5028 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
5029 && TREE_CODE (t
) != TEMPLATE_CONST_PARM
)
5032 /* This argument can be deduced. */
5034 if (unify (tparms
, targs
, ntparms
, t
,
5035 TREE_VEC_ELT (argvec
, i
), nsubsts
, strict
))
5039 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
5042 /* Simple cases: Value already set, does match or doesn't. */
5043 if (targs
[idx
] == arg
5045 && TREE_CODE (targs
[idx
]) == NOP_EXPR
5046 && TREE_OPERAND (targs
[idx
], 0) == arg
))
5048 else if (targs
[idx
])
5053 case TEMPLATE_CONST_PARM
:
5055 idx
= TEMPLATE_CONST_IDX (parm
);
5058 int i
= cp_tree_equal (targs
[idx
], arg
);
5064 my_friendly_abort (42);
5067 targs
[idx
] = copy_to_permanent (arg
);
5071 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5072 return unify (tparms
, targs
, ntparms
, parm
,
5073 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
5075 if (TREE_CODE (arg
) != POINTER_TYPE
)
5077 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5080 case REFERENCE_TYPE
:
5081 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5082 arg
= TREE_TYPE (arg
);
5083 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5087 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5089 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5090 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5092 if (TYPE_DOMAIN (parm
) != NULL_TREE
5093 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5094 TYPE_DOMAIN (arg
), nsubsts
, strict
) != 0)
5096 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5104 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5107 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5109 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5110 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5111 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
5113 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5114 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5115 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
5118 else if (TREE_CODE (parm
) == REAL_TYPE
5119 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5122 /* As far as unification is concerned, this wins. Later checks
5123 will invalidate it if necessary. */
5126 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5127 /* Type INTEGER_CST can come from ordinary constant template args. */
5129 while (TREE_CODE (arg
) == NOP_EXPR
)
5130 arg
= TREE_OPERAND (arg
, 0);
5132 if (TREE_CODE (arg
) != INTEGER_CST
)
5134 return !tree_int_cst_equal (parm
, arg
);
5139 t1
= TREE_OPERAND (parm
, 0);
5140 t2
= TREE_OPERAND (parm
, 1);
5141 return unify (tparms
, targs
, ntparms
, t1
,
5142 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
5149 if (TREE_CODE (arg
) != TREE_VEC
)
5151 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5153 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5154 if (unify (tparms
, targs
, ntparms
,
5155 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5162 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5163 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5164 arg
, nsubsts
, strict
);
5166 /* Allow trivial conversions. */
5167 if (TREE_CODE (arg
) != RECORD_TYPE
5168 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5169 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5172 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5175 if (flag_ansi_overloading
&& ! strict
)
5176 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5178 (CLASSTYPE_TEMPLATE_INFO (arg
)
5179 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5181 if (! t
|| t
== error_mark_node
)
5184 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5185 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
5187 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5192 if (TREE_CODE (arg
) != METHOD_TYPE
)
5197 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5200 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5201 TREE_TYPE (arg
), nsubsts
, strict
))
5203 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5204 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
5208 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5210 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5211 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
5213 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5214 TREE_TYPE (arg
), nsubsts
, strict
);
5217 if (arg
!= decl_constant_value (parm
))
5222 sorry ("use of `%s' in template type unification",
5223 tree_code_name
[(int) TREE_CODE (parm
)]);
5229 mark_decl_instantiated (result
, extern_p
)
5233 if (DECL_TEMPLATE_INSTANTIATION (result
))
5234 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5236 if (TREE_CODE (result
) != FUNCTION_DECL
)
5237 /* The TREE_PUBLIC flag for function declarations will have been
5238 set correctly by tsubst. */
5239 TREE_PUBLIC (result
) = 1;
5243 DECL_INTERFACE_KNOWN (result
) = 1;
5244 DECL_NOT_REALLY_EXTERN (result
) = 1;
5246 /* For WIN32 we also want to put explicit instantiations in
5247 linkonce sections. */
5248 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
5249 make_decl_one_only (result
);
5251 else if (TREE_CODE (result
) == FUNCTION_DECL
)
5252 mark_inline_for_output (result
);
5255 /* Given two function templates PAT1 and PAT2, and explicit template
5256 arguments EXPLICIT_ARGS return:
5258 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
5259 -1 if PAT2 is more specialized than PAT1.
5260 0 if neither is more specialized. */
5263 more_specialized (pat1
, pat2
, explicit_args
)
5264 tree pat1
, pat2
, explicit_args
;
5269 targs
= get_bindings (pat1
, pat2
, explicit_args
);
5275 targs
= get_bindings (pat2
, pat1
, explicit_args
);
5284 /* Given two class template specialization list nodes PAT1 and PAT2, return:
5286 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
5287 -1 if PAT2 is more specialized than PAT1.
5288 0 if neither is more specialized. */
5291 more_specialized_class (pat1
, pat2
)
5297 targs
= get_class_bindings
5298 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
5302 targs
= get_class_bindings
5303 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
5310 /* Return the template arguments that will produce the function signature
5311 DECL from the function template FN, with the explicit template
5312 arguments EXPLICIT_ARGS. */
5315 get_bindings (fn
, decl
, explicit_args
)
5316 tree fn
, decl
, explicit_args
;
5318 int ntparms
= DECL_NTPARMS (fn
);
5319 tree targs
= make_scratch_vec (ntparms
);
5320 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
5321 tree extra_fn_arg
= NULL_TREE
;
5324 if (DECL_STATIC_FUNCTION_P (fn
)
5325 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
5327 /* Sometimes we are trying to figure out what's being
5328 specialized by a declaration that looks like a method, and it
5329 turns out to be a static member function. */
5330 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
5331 && !is_member_template (fn
))
5332 /* The natural thing to do here seems to be to remove the
5333 spurious `this' parameter from the DECL, but that prevents
5334 unification from making use of the class type. So,
5335 instead, we have fn_type_unification add to the parameters
5337 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
5339 /* In this case, though, adding the extra_fn_arg can confuse
5340 things, so we remove from decl_arg_types instead. */
5341 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
5344 i
= fn_type_unification (fn
, explicit_args
, targs
,
5346 TREE_TYPE (TREE_TYPE (decl
)),
5352 /* Check to see that the resulting return type is also OK. */
5353 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
5358 if (!comptypes(t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
5368 get_class_bindings (tparms
, parms
, args
)
5369 tree tparms
, parms
, args
;
5371 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
5372 tree vec
= make_temp_vec (ntparms
);
5374 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5376 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
5377 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
5387 for (i
= 0; i
< ntparms
; ++i
)
5388 if (! TREE_VEC_ELT (vec
, i
))
5394 /* Return the most specialized of the list of templates in FNS that can
5395 produce an instantiation matching DECL, given the explicit template
5396 arguments EXPLICIT_ARGS. */
5399 most_specialized (fns
, decl
, explicit_args
)
5400 tree fns
, decl
, explicit_args
;
5402 tree fn
, champ
, args
, *p
;
5405 for (p
= &fns
; *p
; )
5407 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
5410 p
= &TREE_CHAIN (*p
);
5413 *p
= TREE_CHAIN (*p
);
5420 champ
= TREE_VALUE (fn
);
5421 fn
= TREE_CHAIN (fn
);
5422 for (; fn
; fn
= TREE_CHAIN (fn
))
5424 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5431 fn
= TREE_CHAIN (fn
);
5433 return error_mark_node
;
5435 champ
= TREE_VALUE (fn
);
5439 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
5441 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5443 return error_mark_node
;
5449 /* Return the most specialized of the class template specializations in
5450 SPECS that can produce an instantiation matching ARGS. */
5453 most_specialized_class (specs
, mainargs
)
5454 tree specs
, mainargs
;
5456 tree list
= NULL_TREE
, t
, args
, champ
;
5459 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
5461 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
5464 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
5465 TREE_TYPE (list
) = TREE_TYPE (t
);
5475 for (; t
; t
= TREE_CHAIN (t
))
5477 fate
= more_specialized_class (champ
, t
);
5486 return error_mark_node
;
5492 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
5494 fate
= more_specialized_class (champ
, t
);
5496 return error_mark_node
;
5502 /* called from the parser. */
5505 do_decl_instantiation (declspecs
, declarator
, storage
)
5506 tree declspecs
, declarator
, storage
;
5508 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
5511 tree result
= NULL_TREE
;
5514 if (! DECL_LANG_SPECIFIC (decl
))
5516 cp_error ("explicit instantiation of non-template `%#D'", decl
);
5520 /* If we've already seen this template instance, use it. */
5521 if (TREE_CODE (decl
) == VAR_DECL
)
5523 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
5524 if (result
&& TREE_CODE (result
) != VAR_DECL
)
5527 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
5529 cp_error ("explicit instantiation of `%#D'", decl
);
5532 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
5537 cp_error ("no matching template for `%D' found", decl
);
5541 if (! DECL_TEMPLATE_INFO (result
))
5543 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
5547 if (flag_external_templates
)
5550 if (storage
== NULL_TREE
)
5552 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5555 cp_error ("storage class `%D' applied to template instantiation",
5558 mark_decl_instantiated (result
, extern_p
);
5559 repo_template_instantiated (result
, extern_p
);
5561 instantiate_decl (result
);
5565 mark_class_instantiated (t
, extern_p
)
5569 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
5570 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
5571 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
5572 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
5573 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
5576 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
5577 rest_of_type_compilation (t
, 1);
5582 do_type_instantiation (t
, storage
)
5589 if (TREE_CODE (t
) == TYPE_DECL
)
5592 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
5594 cp_error ("explicit instantiation of non-template type `%T'", t
);
5600 /* With -fexternal-templates, explicit instantiations are treated the same
5601 as implicit ones. */
5602 if (flag_external_templates
)
5605 if (TYPE_SIZE (t
) == NULL_TREE
)
5607 cp_error ("explicit instantiation of `%#T' before definition of template",
5612 if (storage
== NULL_TREE
)
5614 else if (storage
== ridpointers
[(int) RID_INLINE
])
5616 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5618 else if (storage
== ridpointers
[(int) RID_STATIC
])
5622 cp_error ("storage class `%D' applied to template instantiation",
5627 /* We've already instantiated this. */
5628 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
5632 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
5634 mark_class_instantiated (t
, extern_p
);
5635 repo_template_instantiated (t
, extern_p
);
5645 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5646 if (TREE_CODE (tmp
) == FUNCTION_DECL
5647 && DECL_TEMPLATE_INSTANTIATION (tmp
))
5649 mark_decl_instantiated (tmp
, extern_p
);
5650 repo_template_instantiated (tmp
, extern_p
);
5652 instantiate_decl (tmp
);
5655 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5656 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
5658 mark_decl_instantiated (tmp
, extern_p
);
5659 repo_template_instantiated (tmp
, extern_p
);
5661 instantiate_decl (tmp
);
5664 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5665 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
5666 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
5671 instantiate_decl (d
)
5674 tree ti
= DECL_TEMPLATE_INFO (d
);
5675 tree tmpl
= TI_TEMPLATE (ti
);
5676 tree args
= TI_ARGS (ti
);
5678 tree decl_pattern
, code_pattern
;
5680 int nested
= in_function_p ();
5682 int pattern_defined
;
5684 char *file
= input_filename
;
5686 for (td
= tmpl
; DECL_TEMPLATE_INSTANTIATION (td
); )
5687 td
= DECL_TI_TEMPLATE (td
);
5689 /* In the case of a member template, decl_pattern is the partially
5690 instantiated declaration (in the instantiated class), and code_pattern
5691 is the original template definition. */
5692 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
5693 code_pattern
= DECL_TEMPLATE_RESULT (td
);
5695 if (TREE_CODE (d
) == FUNCTION_DECL
)
5697 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
5698 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
5702 d_defined
= ! DECL_IN_AGGR_P (d
);
5703 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
5709 if (TREE_CODE (d
) == FUNCTION_DECL
)
5711 tree spec
= retrieve_specialization (tmpl
, args
);
5713 if (spec
!= NULL_TREE
5714 && DECL_TEMPLATE_SPECIALIZATION (spec
))
5718 /* This needs to happen before any tsubsting. */
5719 if (! push_tinst_level (d
))
5722 push_to_top_level ();
5723 lineno
= DECL_SOURCE_LINE (d
);
5724 input_filename
= DECL_SOURCE_FILE (d
);
5726 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
5727 variable is a static const initialized in the class body. */
5728 if (TREE_CODE (d
) == VAR_DECL
5729 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
5731 pushclass (DECL_CONTEXT (d
), 2);
5732 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5733 TREE_VEC_LENGTH (args
), tmpl
);
5734 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, LOOKUP_NORMAL
);
5737 if (pattern_defined
)
5739 repo_template_used (d
);
5741 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
5743 if (flag_alt_external_templates
)
5745 if (interface_unknown
)
5746 warn_if_unknown_interface (d
);
5748 else if (DECL_INTERFACE_KNOWN (code_pattern
))
5750 DECL_INTERFACE_KNOWN (d
) = 1;
5751 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
5754 warn_if_unknown_interface (code_pattern
);
5758 import_export_decl (d
);
5761 /* Reject all external templates except inline functions. */
5762 if (DECL_INTERFACE_KNOWN (d
)
5763 && ! DECL_NOT_REALLY_EXTERN (d
)
5764 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
5767 /* Defer all templates except inline functions used in another function. */
5768 if (! pattern_defined
5769 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
5772 add_pending_template (d
);
5776 lineno
= DECL_SOURCE_LINE (d
);
5777 input_filename
= DECL_SOURCE_FILE (d
);
5779 /* Trick tsubst into giving us a new decl in case the template changed. */
5780 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
5781 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
5782 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5783 SET_DECL_IMPLICIT_INSTANTIATION (td
);
5784 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
5786 /* And set up DECL_INITIAL, since tsubst doesn't. */
5787 if (TREE_CODE (td
) == VAR_DECL
)
5789 pushclass (DECL_CONTEXT (d
), 2);
5790 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
5791 TREE_VEC_LENGTH (args
), tmpl
);
5795 if (TREE_CODE (d
) == FUNCTION_DECL
)
5797 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
5799 DECL_INITIAL (td
) = error_mark_node
;
5801 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
5802 /* Set up the information about what is being specialized. */
5803 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
5805 duplicate_decls (td
, d
);
5806 if (TREE_CODE (d
) == FUNCTION_DECL
)
5807 DECL_INITIAL (td
) = 0;
5809 if (TREE_CODE (d
) == VAR_DECL
)
5811 DECL_IN_AGGR_P (d
) = 0;
5812 if (DECL_INTERFACE_KNOWN (d
))
5813 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
5816 DECL_EXTERNAL (d
) = 1;
5817 DECL_NOT_REALLY_EXTERN (d
) = 1;
5819 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
5821 else if (TREE_CODE (d
) == FUNCTION_DECL
)
5823 tree t
= DECL_SAVED_TREE (code_pattern
);
5825 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
5826 store_parm_decls ();
5828 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
5831 (TREE_OPERAND (t
, 0),
5832 tsubst_expr (TREE_OPERAND (t
, 1), args
,
5833 TREE_VEC_LENGTH (args
), tmpl
));
5837 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
5839 current_member_init_list
5840 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
5841 current_base_init_list
5842 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
5847 /* Always keep the BLOCK node associated with the outermost
5848 pair of curly braces of a function. These are needed
5849 for correct operation of dwarfout.c. */
5852 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
5853 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
5855 finish_function (lineno
, 0, nested
);
5860 input_filename
= file
;
5862 pop_from_top_level ();
5869 tsubst_chain (t
, argvec
)
5874 tree first
= tsubst (t
, argvec
,
5875 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5878 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
5880 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5881 TREE_CHAIN (last
) = x
;
5891 tsubst_expr_values (t
, argvec
)
5894 tree first
= NULL_TREE
;
5897 for (; t
; t
= TREE_CHAIN (t
))
5899 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
5900 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5901 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
5902 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
5903 *p
= build_tree_list (pur
, val
);
5904 p
= &TREE_CHAIN (*p
);
5915 last_tree
= TREE_CHAIN (last_tree
) = t
;
5922 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
5923 last_tree
= NULL_TREE
;
5930 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
5932 last_tree
= TREE_VALUE (saved_trees
);
5933 saved_trees
= TREE_CHAIN (saved_trees
);
5936 /* D is an undefined function declaration in the presence of templates with
5937 the same name, listed in FNS. If one of them can produce D as an
5938 instantiation, remember this so we can instantiate it at EOF if D has
5939 not been defined by that time. */
5942 add_maybe_template (d
, fns
)
5947 if (DECL_MAYBE_TEMPLATE (d
))
5950 t
= most_specialized (fns
, d
, NULL_TREE
);
5953 if (t
== error_mark_node
)
5955 cp_error ("ambiguous template instantiation for `%D'", d
);
5959 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
5960 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
5961 DECL_MAYBE_TEMPLATE (d
) = 1;
5964 /* Instantiate an enumerated type. Used by instantiate_class_template and
5968 tsubst_enum (tag
, args
, nargs
, field_chain
)
5973 extern tree current_local_enum
;
5974 tree prev_local_enum
= current_local_enum
;
5976 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
5977 tree e
, values
= NULL_TREE
;
5979 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
5981 tree elt
= build_enumerator (TREE_PURPOSE (e
),
5982 tsubst_expr (TREE_VALUE (e
), args
,
5984 TREE_CHAIN (elt
) = values
;
5988 finish_enum (newtag
, values
);
5990 if (NULL
!= field_chain
)
5991 *field_chain
= grok_enum_decls (NULL_TREE
);
5993 current_local_enum
= prev_local_enum
;